File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.88.2.5: download - view: text, annotated - select for diffs
Fri Jul 26 15:32:34 2019 UTC (4 years, 10 months ago) by raeburn
Branches: version_2_11_X
- For 2.11
  Backport 1.99, 1.100, 1.101, 1.102

    1: # The LearningOnline Network with CAPA
    2: # Handler for displaying the course catalog interface
    3: #
    4: # $Id: coursecatalog.pm,v 1.88.2.5 2019/07/26 15:32:34 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::coursecatalog;
   30: 
   31: use strict;
   32: use lib qw(/home/httpd/lib/perl);
   33: use Apache::Constants qw(:common);
   34: use Apache::loncommon;
   35: use Apache::lonhtmlcommon;
   36: use Apache::lonnet;
   37: use Apache::lonlocal;
   38: use Apache::courseclassifier;
   39: use Apache::lonacc;
   40: use LONCAPA;
   41: use LONCAPA::lonauthcgi;
   42: 
   43: sub handler {
   44:     my ($r) = @_;
   45:     &Apache::loncommon::content_type($r,'text/html');
   46:     $r->send_http_header;
   47:     if ($r->header_only) {
   48:         return OK;
   49:     }
   50:     my $handle = &Apache::lonnet::check_for_valid_session($r);
   51:     my $lonidsdir=$r->dir_config('lonIDsDir');
   52:     if ($handle ne '') {
   53:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
   54:     }
   55:     &Apache::lonacc::get_posted_cgi($r);
   56:     &Apache::lonlocal::get_language_handle($r);
   57:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   58:                                             ['sortby','showdom']);
   59: 
   60:     my $codedom = &Apache::lonnet::default_login_domain();
   61: 
   62:     if (($env{'user.domain'} ne '') && ($env{'user.domain'} ne 'public')) { 
   63:         $codedom = $env{'user.domain'};
   64:         if ($env{'request.role.domain'} ne '') {
   65:             $codedom = $env{'request.role.domain'};
   66:         }
   67:     }
   68:     my $formname = 'coursecatalog';
   69:     if ($env{'form.showdom'} ne '') {
   70:         $env{'form.showdom'} = &LONCAPA::clean_domain($env{'form.showdom'});
   71:         if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
   72:             $codedom = $env{'form.showdom'};
   73:         } else {
   74:             $env{'form.showdom'} = '';
   75:         }
   76:     }
   77:     my $domdesc = &Apache::lonnet::domain($codedom,'description');
   78:     &Apache::lonhtmlcommon::clear_breadcrumbs();
   79: 
   80:     my $crscats = &Apache::lonnet::get_dom_cats($codedom);
   81:     my %domdefaults = &Apache::lonnet::get_domain_defaults($codedom);
   82:     my $knownuser = &user_is_known();
   83:     my $canviewall = &canview_all($knownuser,$codedom);
   84:     
   85:     my ($cathash,$cattype);
   86:     if (ref($crscats) eq 'HASH') {
   87:         %{$cathash} = %{$crscats};
   88:     } else {
   89:         $cathash = {};
   90:     }
   91:     if ($knownuser || $canviewall) {
   92:         $cattype = $domdefaults{'catauth'};
   93:     } else {
   94:         $cattype = $domdefaults{'catunauth'};
   95:     }
   96:     if ($cattype eq '') {
   97:         $cattype = 'std';
   98:     }
   99:     if ($cattype eq 'none') {
  100:         $r->print(&Apache::loncommon::start_page('Course/Community Catalog'));
  101:         &Apache::lonhtmlcommon::add_breadcrumb
  102:         ({href=>"/adm/coursecatalog",
  103:           text=>"Course/Community Catalog"});
  104:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
  105:         if ($knownuser || $canviewall) {
  106:             $r->print('<div>'.&mt('No catalog of LON-CAPA courses/communities is provided for: [_1]',$domdesc).'</div>');
  107:         } else {
  108:             if ($domdefaults{'catauth'} eq 'none') {
  109:                 $r->print('<div>'.&mt('No catalog of LON-CAPA courses/communities is provided for: [_1]',$domdesc).'</div>');
  110:             } else {
  111:                 $r->print('<div>'.&mt('The catalog of LON-CAPA courses/communities provided for: "[_1]" is only available to users who are logged in.',$domdesc).'</div>');
  112:             }
  113:         }      
  114:         $r->print(&Apache::loncommon::end_page());
  115:         return OK;
  116:     }
  117: 
  118:     my $cnum;
  119:     if ($cattype eq 'codesrch') {
  120:         my ($uniquecode,$codemsg,$brtext);
  121:         if ($env{'form.uniquecode'}) {
  122:             $uniquecode = $env{'form.uniquecode'};
  123:             $uniquecode =~ s/^\s+|\s+$//g;
  124:         }
  125:         my $js = '<script type="text/javascript">'."\n".
  126:                  '// <![CDATA['."\n".
  127:                  &courselink_javascript()."\n".
  128:                  '// ]]>'."\n".
  129:                  '</script>'."\n";
  130:         $r->print(&Apache::loncommon::start_page('Search for a Course/Community',$js));
  131:         if ($uniquecode =~ /^\w{6}$/) {
  132:             &Apache::lonhtmlcommon::add_breadcrumb
  133:             ({href=>"/adm/coursecatalog",
  134:               text=>"Course/Community Catalog"});
  135:             $brtext = 'Search Result';
  136:         } else {
  137:             $brtext = 'Course/Community Catalog';
  138:         }
  139:         &Apache::lonhtmlcommon::add_breadcrumb
  140:         ({href=>"/adm/coursecatalog",
  141:           text=>"$brtext"});
  142:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
  143:         $r->print(&coursesearch($codedom,$domdesc,$uniquecode));
  144:         if ($uniquecode =~ /^\w{6}$/) {
  145:             $r->print('<hr />');
  146:             my $confname = $codedom.'-domainconfig';
  147:             my %codes = &Apache::lonnet::get('uniquecodes',[$uniquecode],$codedom,$confname);
  148:             if ($codes{$uniquecode}) {
  149:                 $cnum = $codes{$uniquecode};
  150:                 my %courses = &Apache::lonnet::courseiddump($codedom,'.',1,'.','.',
  151:                                                             $cnum,undef,undef,'.',1);
  152:                 if (keys(%courses)) {
  153:                     $env{'form.coursenum'} = $cnum;
  154:                     my %courseinfo = &build_courseinfo_hash(\%courses,$knownuser,$codedom,$canviewall);
  155:                     undef($env{'form.coursenum'});
  156:                     if (ref($courseinfo{$codedom.'_'.$cnum}) eq 'HASH') {
  157:                         $r->print(&Apache::lonhtmlcommon::start_pick_box());
  158:                         my @cols = ('title','ownerlastnames','seclist','access');
  159:                         my %lt = &Apache::lonlocal::texthash(
  160:                                      title          => 'Title',
  161:                                      ownerlastnames => 'Owner & Co-owner(s)',
  162:                                      seclist        => 'Sections',
  163:                                      access         => 'Default Access Dates for Students',
  164:                                  );
  165:                         my @shown;
  166:                         foreach my $item (@cols) {
  167:                             if ($courseinfo{$codedom.'_'.$cnum}{$item}) {
  168:                                 push(@shown,$item);
  169:                             }
  170:                         }
  171:                         my $num = 0;
  172:                         foreach my $item (@shown) {
  173:                             $num ++;
  174:                             $r->print(&Apache::lonhtmlcommon::row_title($lt{$item}).
  175:                                       $courseinfo{$codedom.'_'.$cnum}{$item});
  176:                             if ($item eq 'title') {
  177:                                 if ($courseinfo{$codedom.'_'.$cnum}{'showsyllabus'}) {
  178:                                     $r->print('&nbsp;<font size="-2">'.
  179:                                               '<a href="javascript:ToSyllabus('."'$codedom','$cnum'".')">'.
  180:                                               &mt('Syllabus').'</a></font>');
  181:                                 }
  182:                             }
  183:                             my $arg = ( $num == scalar(@shown) ? 1 : '' );
  184:                             $r->print(&Apache::lonhtmlcommon::row_closure($arg));
  185:                         }
  186:                         $r->print(&Apache::lonhtmlcommon::end_pick_box());
  187:                         my $selfenroll = &selfenroll_status($courseinfo{$codedom.'_'.$cnum},$codedom.'_'.$cnum);
  188:                         if ($selfenroll) {
  189:                             $r->print('<br />'.$selfenroll);
  190:                         }
  191:                         $r->print('<form name="linklaunch" method="post" action="">'."\n".
  192:                                   '<input type="hidden" name="backto" value="coursecatalog" />'."\n".
  193:                                   '<input type="hidden" name="courseid" value="" />'."\n".
  194:                                   '</form>'."\n");
  195: 
  196:                     } else {
  197:                         $codemsg = &mt('Code matched, but course ID to which this mapped is invalid.');
  198:                     }
  199:                 } else {
  200:                     $codemsg = &mt('Code matched, but course ID to which this mapped is invalid.');              
  201:                 } 
  202:             } else {
  203:                 $codemsg = &mt('No match');
  204:             }
  205:         }
  206:         $r->print('<br />'.&Apache::loncommon::end_page());
  207:         return OK;
  208:     } else {
  209:         if ($env{'form.coursenum'} ne '') {
  210:             if ($env{'form.coursenum'} =~ /^$LONCAPA::match_courseid$/) {
  211:                 $cnum = $env{'form.coursenum'};
  212:             } else {
  213:                 delete($env{'form.coursenum'});
  214:             }
  215:         }
  216:     }
  217: 
  218:     my (@cats,@trails,%allitems,%idx,@jsarray,%subcathash,$subcats,%maxd,
  219:         $toplevelstr,$maxdepthstr);
  220:     if ($env{'form.withsubcats'}) {
  221:         $subcats = \%subcathash;
  222:     }
  223:     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems,
  224:                                            \%idx,\@jsarray,$subcats,\%maxd);
  225:     if (ref($cats[0]) eq 'ARRAY') {
  226:         foreach my $item (@{$cats[0]}) {
  227:             $toplevelstr .= "'".&js_escape($item)."::0',";
  228:             $maxdepthstr .= "'$maxd{$item}',";
  229:         }
  230:         $toplevelstr =~ s/,$//;
  231:         $maxdepthstr =~ s/,$//;
  232:     }
  233:     &validate_input($codedom,\@cats,\%maxd,$cathash);
  234:     my ($numtitles,@codetitles);
  235:     if (($env{'form.coursenum'} ne '') && ($knownuser)) {
  236:         &course_details($r,$codedom,$formname,$domdesc,\@trails,\%allitems,\@codetitles);
  237:     } else {
  238:         my ($catlinks,$has_subcats,$selitem) = &category_breadcrumbs($codedom,@cats);
  239:         my $wasacctext = &get_wasactive_text();
  240:         my $catjs = <<"ENDSCRIPT";
  241: 
  242: function setCatDepth(depth) {
  243:     var depth = parseInt(depth);
  244:     if (depth !== NaN) {
  245:         if (depth > 0) {
  246:             var possmaxd = 0;
  247:             var toplevel = new Array($toplevelstr);
  248:             var maxdepths = new Array($maxdepthstr);
  249:             if (toplevel.length) {
  250:                 for (var i=0; i<toplevel.length; i++) {
  251:                     var item = unescape(toplevel[i]);
  252:                     if (item == document.coursecats.currcat_0.value) {
  253:                         possmaxd = maxdepths[i];
  254:                         break;
  255:                     }
  256:                 }
  257:             }
  258:             if (depth > possmaxd) {
  259:                 depth = possmaxd;
  260:             }
  261:         }
  262:         document.coursecats.catalog_maxdepth.value = depth;
  263:     } else {
  264:         document.coursecats.currcat_0.value = '';
  265:         document.coursecats.catalog_maxdepth.value = '';
  266:     }
  267:     document.coursecats.submit();
  268:     return;
  269: }
  270: 
  271: function changeSort(caller) {
  272:     document.$formname.sortby.value = caller;
  273:     document.$formname.submit();
  274: }
  275: 
  276: function setCourseId(caller) {
  277:     document.$formname.coursenum.value = caller;
  278:     document.$formname.submit();
  279: }
  280: 
  281: ENDSCRIPT
  282:         $catjs .= &courselink_javascript();
  283:         if (&user_is_dc($codedom) || $canviewall) {
  284:             $catjs .= <<ENDTOGGJS
  285: 
  286: function toggleStatuses() {
  287:     if (document.$formname.showdetails.checked) {
  288:         document.getElementById('statuschoice').style.display='block';
  289:         document.getElementById('statuscell').style.borderLeft='1px solid'; 
  290:     } else {
  291:         document.getElementById('statuschoice').style.display='none';
  292:         document.getElementById('statuscell').style.borderLeft='0px';
  293:     }
  294:     return;
  295: }
  296: 
  297: function toggleWasActive() {
  298:     if (document.getElementById('counts_Previous')) {
  299:         if (document.getElementById('counts_Previous').checked) {
  300:             document.getElementById('choosewasactive').style.display='block';
  301:             document.getElementById('choosewasacctext').innerHTML = '$wasacctext';
  302:         } else {
  303:             document.getElementById('choosewasactive').style.display='none';
  304:             document.getElementById('choosewasacctext').innerHTML = '';
  305:         }
  306:     }
  307:     return;
  308: }
  309: 
  310: ENDTOGGJS
  311:         }
  312:         if ($env{'form.currcat_0'} eq 'instcode::0') {
  313:             $numtitles = &instcode_course_selector($r,$codedom,$formname,$domdesc,
  314:                                                    $catlinks,$catjs,\@codetitles,$cattype,$canviewall);
  315:             if ($env{'form.state'} eq 'listing') {
  316:                 $r->print(&print_course_listing($codedom,$numtitles,undef,undef,undef,
  317:                                                 \@codetitles,$canviewall));
  318:             }
  319:         } else {
  320:             my (%add_entries);
  321:             my ($currdepth,$deeper) = &get_depth_values();
  322:             if ($selitem) {
  323:                 my $alert = &mt('Choose a subcategory to display');
  324:                 if (!$deeper) {
  325:                     $alert = &mt('Choose a category to display');
  326:                 }
  327:                 &js_escape(\$alert);
  328:                 $catjs .= <<ENDJS;
  329: function check_selected() {
  330:     if (document.coursecats.$selitem.options[document.coursecats.$selitem.selectedIndex].value == "") {
  331:         alert('$alert');
  332:         return false;
  333:     }
  334: }
  335: ENDJS
  336:             }
  337:             $catjs = '<script type="text/javascript">'."\n".$catjs."\n".'</script>';
  338:             &cat_header($r,$codedom,$catjs,\%add_entries,$catlinks,undef,$cattype);
  339:             if ($env{'form.currcat_0'} ne '') {
  340:                 $r->print('<form name="'.$formname.
  341:                           '" method="post" action="/adm/coursecatalog">'.
  342:                           &additional_filters($codedom,$has_subcats,$canviewall)."\n");
  343:                 $r->print('<input type="hidden" name="catalog_maxdepth" value="'.
  344:                           $deeper.'" />'."\n");
  345:                 for (my $i=0; $i<$deeper; $i++) {
  346:                     $r->print('<input type="hidden" name="currcat_'.$i.'" value="'.$env{'form.currcat_'.$i}.'" />'."\n");
  347:                 }
  348:                 my $display_button;
  349:                 if ($env{'form.currcat_0'} eq 'communities::0') {
  350:                     $display_button = &mt('Display communities');
  351:                 } else {
  352:                     $display_button = &mt('Display courses');
  353:                 }
  354:                 $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
  355:                           '<input type="hidden" name="sortby" value="" />'."\n".
  356:                           '<input type="hidden" name="state" value="listing" />'."\n".
  357:                           '<input type="hidden" name="showdom" value="'.
  358:                           $env{'form.showdom'}.'" />'.
  359:                           '<input type="submit" name="catalogfilter" value="'.
  360:                           $display_button.'" /></form><br /><br />');
  361:             }
  362:             if ($env{'form.state'} eq 'listing') {
  363:                 $r->print(&print_course_listing($codedom,undef,\@trails,\%allitems,$subcats,\@codetitles,
  364:                                                 $canviewall));
  365:             }
  366:         }
  367:     }
  368:     $r->print('<br />'.&Apache::loncommon::end_page());
  369:     return OK;
  370: }
  371: 
  372: sub validate_input {
  373:     my ($codedom,$cats,$maxd,$configref) = @_;
  374:     my $currcat = '';
  375:     my $depth = 0;
  376:     if ($env{'form.catalog_maxdepth'} ne '') {
  377:         $env{'form.catalog_maxdepth'} =~ s{\D}{}g;
  378:     }
  379:     if ((ref($cats) eq 'ARRAY') && (ref($maxd) eq 'HASH')) {
  380:         if (ref($cats->[0]) eq 'ARRAY') {
  381:             if (@{$cats->[0]} == 1) {
  382:                 if ($cats->[0][0] eq 'instcode') {
  383:                     $currcat = 'instcode::0';
  384:                 } elsif ($cats->[0][0] eq 'communities') {
  385:                     $currcat = 'communities::0';
  386:                 } elsif ($cats->[0][0] eq 'placement') {
  387:                     $currcat = 'placement::0';
  388:                 } else {
  389:                     my $name = $cats->[0][0];
  390:                     $currcat = &escape($name).'::0';
  391:                 }
  392:                 if (exists($maxd->{$cats->[0][0]})) {
  393:                     if ($env{'form.catalog_maxdepth'} <= $maxd->{$cats->[0][0]}) {
  394:                         $depth = $env{'form.catalog_maxdepth'};
  395:                     } else {
  396:                         $depth = $maxd->{$cats->[0][0]};
  397:                     }
  398:                 }
  399:             } elsif ((@{$cats->[0]} > 1) && ($env{'form.currcat_0'} ne '')) {
  400:                 my ($escname) = ($env{'form.currcat_0'} =~ /^([^:]+)\:\:0$/);
  401:                 if ($escname =~ /^instcode|communities|placement$/) {
  402:                     $currcat = $env{'form.currcat_0'};
  403:                     if (exists($maxd->{$escname})) {
  404:                         if ($env{'form.catalog_maxdepth'} <= $maxd->{$escname}) {
  405:                             $depth = $env{'form.catalog_maxdepth'};
  406:                         } else {
  407:                             $depth = $maxd->{$escname};
  408:                         }
  409:                     } else {
  410:                         $depth = 1;
  411:                     }
  412:                 } elsif ($escname ne '') {
  413:                     my $name = &unescape($escname);
  414:                     if (grep(/^\Q$name\E$/,@{$cats->[0]})) {
  415:                         $currcat = $env{'form.currcat_0'};
  416:                         if (exists($maxd->{$name})) {
  417:                             if ($env{'form.catalog_maxdepth'} <= $maxd->{$name}) {
  418:                                 $depth = $env{'form.catalog_maxdepth'};
  419:                             } else {
  420:                                 $depth = $maxd->{$name};
  421:                             }
  422:                         }
  423:                     }
  424:                 }
  425:             }
  426:         }
  427:     }
  428:     $env{'form.currcat_0'} = $currcat;
  429:     $env{'form.catalog_maxdepth'} = $depth;
  430: 
  431:     my %possibles = (
  432:         backto => 1,
  433:         catalogfilter => 1,
  434:         catalog_maxdepth => 1,
  435:         courseid => 1,
  436:         coursenum => 1,
  437:         currcat_0 => 1,
  438:         numtitles => 1,
  439:         Year => 1,
  440:         Semester => 1,
  441:         Department => 1,
  442:         Number => 1,
  443:         showdom => 1,
  444:         sortby => 1,
  445:         showcounts => 1,
  446:         showdetails => 1,
  447:         showhidden => 1,
  448:         showselfenroll => 1,
  449:         state => 1,
  450:         uniquecode => 1,
  451:         wasactive => 1,
  452:         wasactiveon_day => 1,
  453:         wasactiveon_month => 1,
  454:         wasactiveon_year => 1,
  455:         withsubcats => 1,
  456:     );
  457:     my %currcats;
  458:     if ($env{'form.catalog_maxdepth'} > 0) {
  459:         for (my $i=1; $i<=$env{'form.catalog_maxdepth'}; $i++) {
  460:             $currcats{'currcat_'.$i} = 1;
  461:             $possibles{'currcat_'.$i} = 1;
  462:         }
  463:     }
  464: 
  465:     foreach my $key (sort(keys(%env))) {
  466:         if ($key =~ /^form\.(.+)$/) {
  467:             unless ($possibles{$1}) {
  468:                 delete($env{$key});
  469:             }
  470:         }
  471:     }
  472:     if (exists($env{'form.backto'})) {
  473:         unless ($env{'form.backto'} eq 'coursecatalog') {
  474:             delete($env{'form.backto'});
  475:         }
  476:     }
  477:     if (exists($env{'form.catalogfilter'})) {
  478:         unless (($env{'form.catalogfilter'} eq &mt('Display courses')) ||
  479:                 ($env{'form.catalogfilter'} eq &mt('Display communities')) ||
  480:                 ($env{'form.catalogfilter'} eq &mt('Display placement tests'))) {
  481:             delete($env{'form.catalogfilter'});
  482:         }
  483:     }
  484:     if (exists($env{'form.courseid'})) {
  485:         if ($env{'form.courseid'} ne '') {
  486:             unless ($env{'form.courseid'} =~ /^\Q$codedom\E_$LONCAPA::match_courseid$/) {
  487:                 $env{'form.courseid'} = '';
  488:             }
  489:         }
  490:     }
  491:     if (exists($env{'form.coursenum'})) {
  492:         unless ($env{'form.coursenum'} =~ /^$LONCAPA::match_courseid$/) {
  493:             $env{'form.coursenum'} = '';
  494:         }
  495:     }
  496:     if (exists($env{'form.currcat_0'})) {
  497:         unless ($env{'form.currcat_0'} =~ /^(instcode|communities|placement)\Q::0\E$/) {
  498:             if ($env{'form.currcat_0'} =~ /^.+\Q::0\E$/) {
  499:                 if (ref($crscatsref) eq 'HASH') {
  500:                     unless (exists($crscatsref->{$env{'form.currcat_0'}})) {
  501:                         delete($env{'form.currcat_0'});
  502:                     }
  503:                 } else {
  504:                     delete($env{'form.currcat_0'});
  505:                 }
  506:             } else {
  507:                 delete($env{'form.currcat_0'});
  508:             }
  509:         }
  510:     }
  511:     if (exists($env{'form.numtitles'})) {
  512:         unless ($env{'form.numtitles'} =~ /^\d+$/) {
  513:             delete($env{'form.numtitles'});
  514:         }
  515:     }
  516:     my ($gotcodes,%possvals);
  517:     foreach my $item ('Year','Semester','Department','Number') {
  518:         if (exists($env{'form.'.$item})) {
  519:             unless ($env{'form.'.$item} eq '0') {
  520:                 unless ($gotcodes) {
  521:                     $gotcodes = &get_instcode_allowable($codedom,\%possvals);
  522:                 }
  523:                 if (ref($possvals{$item}) eq 'HASH') {
  524:                     unless (exists($possvals{$item}{$env{'form.'.$item}})) {
  525:                         delete($env{'form.'.$item});
  526:                     }
  527:                 } else {
  528:                     delete($env{'form.'.$item});
  529:                 }
  530:             }
  531:         }
  532:     }
  533:     if (exists($env{'form.showdom'})) {
  534:         unless ($env{'form.showdom'} =~ /^$LONCAPA::match_domain$/) {
  535:             delete($env{'form.showdom'});
  536:         }
  537:     }
  538:     if (exists($env{'form.sortby'})) {
  539:         unless ($env{'form.sortby'} =~ /^(title|code|owner|cats)$/) {
  540:             delete($env{'form.sortby'});
  541:         }
  542:     }
  543:     if (exists($env{'form.showcounts'})) {
  544:         if (ref($env{'form.showcounts'}) eq 'ARRAY') {
  545:             foreach my $item (@{$env{'form.showcounts'}}) {
  546:                 unless ($item =~ /^(Active|Future|Previous)$/) {
  547:                     delete($env{'form.showcounts'});
  548:                     last;
  549:                 }
  550:             }
  551:         } else {
  552:             unless ($env{'form.showcounts'} =~ /^(Active|Future|Previous)$/) {
  553:                 delete($env{'form.showcounts'});
  554:             }
  555:         }
  556:     }
  557:     foreach my $item ('showhidden','showdetails','showselfenroll','withsubcats') {
  558:         if (exists($env{'form.'.$item})) {
  559:             unless ($env{'form.'.$item} eq '1') {
  560:                 delete($env{'form.'.$item});
  561:             }
  562:         }
  563:     }
  564:     if (exists($env{'form.state'})) {
  565:         unless ($env{'form.state'} eq 'listing') {
  566:             delete($env{'form.state'});
  567:         }
  568:     }
  569:     if (exists($env{'form.uniquecode'})) {
  570:         unless ($env{'form.uniquecode'} =~ /^\w{6}$/) {
  571:             delete($env{'form.uniquecode'});
  572:         }
  573:     }
  574:     if (exists($env{'form.wasactive'})) {
  575:         unless (($env{'form.wasactive'} eq 'accessend') || ($env{'form.wasactive'} eq 'enrollend')) {
  576:             delete($env{'form.wasactive'});
  577:         }
  578:     }
  579:     if (exists($env{'form.wasactiveon_day'})) {
  580:         my $tmpday = $env{'form.wasactiveon_day'};
  581:         unless (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
  582:             delete($env{'form.wasactiveon_day'});
  583:         }
  584:     }
  585:     if (exists($env{'form.wasactiveon_month'})) {
  586:         my $tmpmonth = $env{'form.wasactiveon_month'};
  587:         unless (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
  588:             delete($env{'form.wasactiveon_month'});
  589:         }
  590:     }
  591:     if (exists($env{'form.wasactiveon_year'})) {
  592:         my $tmpyear = $env{'form.wasactiveon_year'};
  593:         unless (($tmpyear =~ /^\d+$/) && ($tmpyear >= 1970)) {
  594:             delete($env{'form.wasactiveon_year'});
  595:         }
  596:     }
  597:     if (keys(%currcats)) {
  598:         foreach my $key (keys(%currcats)) {
  599:             if ($env{'form.'.$key} eq '') {
  600:                 delete($env{'form.'.$key});
  601:             } elsif (ref($crscatsref) eq 'HASH') {
  602:                 unless (exists($crscatsref->{$env{'form.'.$key}})) {
  603:                     delete($env{'form.'.$key});
  604:                 }
  605:             } else {
  606:                 delete($env{'form.'.$key});
  607:             }
  608:         }
  609:     }
  610:     return;
  611: }
  612: 
  613: sub get_instcode_allowable {
  614:     my ($codedom,$possvalref) = @_;
  615:     return 1 unless (ref($possvalref) eq 'HASH');
  616:     my $instcats = &Apache::lonnet::get_dom_instcats($codedom);
  617:     if (ref($instcats) eq 'HASH') {
  618:         if ((ref($instcats->{'codetitles'}) eq 'ARRAY') && (ref($instcats->{'codes'}) eq 'HASH')) {
  619:             my %codes = %{$instcats->{'codes'}};
  620:             my @codetitles = @{$instcats->{'codetitles'}};
  621:             my @standardnames = &Apache::loncommon::get_standard_codeitems();
  622:             my %local_to_standard;
  623:             for (my $i=0; $i<@codetitles; $i++) {
  624:                 $local_to_standard{$codetitles[$i]} = $standardnames[$i];
  625:                 $possvalref->{$standardnames[$i]} = {};
  626:             }
  627:             foreach my $cid (sort(keys(%codes))) {
  628:                 if (ref($codes{$cid}) eq 'HASH') {
  629:                     foreach my $item (keys(%{$codes{$cid}})) {
  630:                         $possvalref->{$local_to_standard{$item}}{$codes{$cid}{$item}} = 1;
  631:                     }
  632:                 }
  633:             }
  634:         }
  635:     }
  636:     return 1;
  637: }
  638: 
  639: sub course_details {
  640:     my ($r,$codedom,$formname,$domdesc,$trails,$allitems,$codetitles) = @_;
  641:     my $output;
  642:     my %add_entries = (topmargin    => "0",
  643:                        marginheight => "0",);
  644:     my $js = '<script type="text/javascript">'."\n".
  645:              &courselink_javascript().'</script>'."\n";
  646:     my $start_page =
  647:         &Apache::loncommon::start_page('Course/Community Catalog',$js,
  648:                                        {'add_entries' => \%add_entries, });
  649:     $r->print($start_page);
  650:     if ($env{'form.numtitles'} > 0) {
  651:         &Apache::lonhtmlcommon::add_breadcrumb
  652:                 ({href=>"/adm/coursecatalog",
  653:                   text=>"Course/Community Catalog"});
  654:     }
  655:     my $brtextone = 'Course listing';
  656:     my $brtexttwo = 'Course details';
  657:     if ($env{'form.currcat_0'} eq 'communities::0') {
  658:         $brtextone = 'Community listing';
  659:         $brtexttwo = 'Community details';
  660:     }
  661:     &Apache::lonhtmlcommon::add_breadcrumb
  662:              ({href=>"javascript:document.$formname.submit()",
  663:               text=>$brtextone},
  664:              {text=>$brtexttwo});
  665:     $r->print(
  666:         &Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog').
  667:         '<h2>'.
  668:         (($env{'form.currcat_0'} eq 'communities::0') ?
  669:             &mt('Detailed community information:') :
  670:             &mt('Detailed course information:')).
  671:         '</h2>'.
  672:         &print_course_listing($codedom,undef,$trails,$allitems,undef,$codetitles).
  673:         '<br />'.
  674:         '<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
  675:         &Apache::lonhtmlcommon::actionbox([
  676:             '<a href = "javascript:document.coursecatalog.submit()">'.
  677:             (($env{'form.currcat_0'} eq 'communities::0') ?
  678:                 &mt('Back to community listing') : &mt('Back to course listing')).
  679:             '</a>'
  680:         ]).
  681:         &Apache::lonhtmlcommon::echo_form_input(['coursenum','catalogfilter',
  682:                                                  'showdetails','courseid']).
  683:         '</form>');
  684:     return;
  685: }
  686: 
  687: sub coursesearch {
  688:     my ($codedom,$domdesc,$uniquecode) = @_;
  689:     my %lt = &Apache::lonlocal::texthash (
  690:                                            crlk => 'Course look-up',  
  691:                                            code => 'Code',
  692:                                            ifyo => 'Enter the course code (six letters and numbers)',
  693:                                            srch => 'Find course',
  694:                                          );
  695:     return <<"END";
  696: <h3>$lt{'crlk'} ($domdesc)</h3>
  697: $lt{'ifyo'}
  698: <form name="searchbycode" method="post" action="">
  699: <span class="LC_nobreak">
  700: <input type="text" value="$uniquecode" size="6" name="uniquecode" />&nbsp;
  701: <br />
  702: <input type="submit" value="$lt{'srch'}" name="srch" /></span>
  703: </form>
  704: END
  705: }
  706: 
  707: sub courselink_javascript {
  708:     return <<"END";
  709: 
  710: function ToSyllabus(cdom,cnum) {
  711:     if (cdom == '' || cdom == null) {
  712:         return;
  713:     }
  714:     if (cnum == '' || cnum == null) {
  715:         return;
  716:     }
  717:     document.linklaunch.action = "/public/"+cdom+"/"+cnum+"/syllabus";
  718:     document.linklaunch.submit();
  719: }
  720: 
  721: function ToSelfenroll(courseid) {
  722:     if (courseid == '') {
  723:         return;
  724:     }
  725:     document.linklaunch.action = "/adm/selfenroll";
  726:     document.linklaunch.courseid.value = courseid;
  727:     document.linklaunch.submit();
  728: }
  729: 
  730: END
  731: }
  732: 
  733: sub instcode_course_selector {
  734:     my ($r,$codedom,$formname,$domdesc,$catlinks,$catjs,$codetitles,$cattype,$canviewall) = @_;
  735:     my %cat_titles = ();
  736:     my %cat_order = ();
  737:     my %cat_items = ();
  738:     my %add_entries = (topmargin    => "0",
  739:                        marginheight => "0",);
  740:     my ($jscript,$totcodes,$numtitles,$lasttitle) = 
  741:         &Apache::courseclassifier::instcode_selectors_data($codedom,$formname,
  742:                            \%cat_items,$codetitles,\%cat_titles,\%cat_order);
  743:     my $js = '<script type="text/javascript">'."\n".
  744:              '// <![CDATA['."\n".
  745:              "$jscript\n$catjs\n".
  746:              '// ]]>'."\n".
  747:              '</script>';
  748:     if ($totcodes) {
  749:         if (($env{'form.state'} eq 'listing') && ($numtitles > 0)) {
  750:             $add_entries{'onload'} = 'setElements();';
  751:         }
  752:         if (&user_is_dc($codedom) || $canviewall) {
  753:             $add_entries{'onload'} .= ' toggleStatuses();toggleWasActive();'
  754:         }
  755:         &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles,$cattype);
  756:         my $cat_maxdepth = $env{'form.catalog_maxdepth'};
  757:         $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
  758:                   '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'."\n".
  759:                   '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'."\n".
  760:                   '<input type="hidden" name="currcat_0" value="instcode::0" />'.
  761:                   &additional_filters($codedom,undef,$canviewall));
  762:         if ($numtitles > 0) {
  763:             $r->print('<b>'.&mt('Choose which course(s) to list.').'</b><br />'.
  764:                       &Apache::courseclassifier::build_instcode_selectors($numtitles,
  765:                        $lasttitle,\%cat_items,$codetitles,\%cat_titles,\%cat_order));
  766:         }
  767:         $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
  768:                   '<input type="hidden" name="sortby" value="" />'."\n".
  769:                   '<input type="hidden" name="state" value="listing" />'."\n".
  770:                   '<input type="submit" name="catalogfilter" value="'.
  771:                   &mt('Display courses').'" />'.
  772:                   '<input type="hidden" name="numtitles" value="'.$numtitles.
  773:                   '" /></form><br /><br />');
  774:     } else {
  775:         $js = '<script type="text/javascript">'."\n".
  776:               '// <![CDATA['."\n".
  777:               "$catjs\n".
  778:               '// ]]>'."\n".
  779:               '</script>';
  780:         &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles,$cattype);
  781:         my $cat_maxdepth = $env{'form.catalog_maxdepth'};
  782:         $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
  783:                   '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'.
  784:                   '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'.
  785:                   '<input type="hidden" name="currcat_0" value="instcode::0" />');
  786:         $r->print('<br />'.&mt('No official courses to display for [_1].',$domdesc).'</form>');
  787:     }
  788:     return $numtitles;
  789: }
  790: 
  791: sub cat_header {
  792:     my ($r,$codedom,$js,$add_entries,$catlinks,$numtitles,$cattype) = @_;
  793:     my $start_page =
  794:         &Apache::loncommon::start_page('Course/Community Catalog',$js,
  795:                                        { 'add_entries' => $add_entries, });
  796:     $r->print($start_page);
  797:     my $brtext = 'Course listing';
  798:     if ($env{'form.currcat_0'} eq 'communities::0') {
  799:         $brtext = 'Community listing';
  800:     }
  801:     if ($env{'form.state'} eq 'listing') {
  802:         if ($numtitles > 0) {
  803:             &Apache::lonhtmlcommon::add_breadcrumb
  804:                 ({href=>"/adm/coursecatalog",
  805:                   text=>"Course/Community Catalog"},
  806:                  {text=>$brtext});
  807:         } else {
  808:             &Apache::lonhtmlcommon::add_breadcrumb
  809:             ({text=>$brtext});
  810:         }
  811:     } else {
  812:         &Apache::lonhtmlcommon::add_breadcrumb
  813:         ({href=>"/adm/coursecatalog",
  814:           text=>"Course/Community Catalog"});
  815:     }
  816:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
  817:     if ($cattype eq 'std') {
  818:         my $onchange = 'this.form.submit()';
  819:         $r->print('<form name="coursecatdom" method="post" action="/adm/coursecatalog">'.
  820:                   '<table border="0"><tr><td><b>'.&mt('Domain:').'</b></td><td>'.
  821:                   &Apache::loncommon::select_dom_form($codedom,'showdom','',1,$onchange));
  822:         if (!$onchange) {
  823: 	   $r->print('&nbsp;<input type="submit" name="godom" value="'.&mt('Change').'" />');
  824:         }
  825:         $r->print('</td></tr></table></form>');
  826:     }
  827:     $r->print('<form name="coursecats" method="post" action="/adm/coursecatalog"'.
  828:               ' onsubmit="return check_selected();">'.
  829:               '<table border="0"><tr>'.$catlinks.'</tr></table></form>');
  830:     return;
  831: }
  832: 
  833: sub category_breadcrumbs {
  834:     my ($dom,@cats) = @_;
  835:     my $crumbsymbol = ' &#x25b6; ';
  836:     my ($currdepth,$deeper) = &get_depth_values();
  837:     my $currcat_str = 
  838:         '<input type="hidden" name="catalog_maxdepth" value="'.$deeper.'" />'.
  839:         '<input type="hidden" name="showdom" value="'.$dom.'" />';
  840:     my $catlinks = '<td valign="top"><b>'.&mt('Catalog:').'</b></td><td><table><tr><td>';
  841:     my $has_subcats;
  842:     my $selitem;
  843:     if (ref($cats[0]) eq 'ARRAY') {
  844:         if (@{$cats[0]} == 0) {
  845:             $catlinks .= &mt('No categories defined in this domain'); 
  846:         } elsif (@{$cats[0]} == 1) {
  847:             if ($cats[0][0] eq 'instcode') {
  848:                 $catlinks .= &mt('Official courses (with institutional codes)');
  849:                 $env{'form.currcat_0'} = 'instcode::0';
  850:             } elsif ($cats[0][0] eq 'communities') {
  851:                 $catlinks .= &mt('Communities');
  852:                 $env{'form.currcat_0'} = 'communities::0';
  853:             } else {
  854:                 my $name = $cats[0][0];
  855:                 my $item = &escape($name).'::0';
  856:                 $catlinks .= $name;
  857:                 $env{'form.currcat_0'} = $item;
  858:             }
  859:             $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
  860:         } else {
  861:             $catlinks .= &main_category_selector(@cats);
  862:             if (($env{'form.currcat_0'} ne '') && 
  863:                 ($env{'form.currcat_0'} ne 'instcode::0')) {
  864:                 $catlinks .= $crumbsymbol;
  865:             }
  866:         }
  867:     } else {
  868:         $catlinks .= &mt('Official courses (with institutional codes)');
  869:                      $env{'form.currcat_0'} = 'instcode::0';
  870:          $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
  871:     }
  872:     if ($deeper) {
  873:         for (my $i=1; $i<=$deeper; $i++) {
  874:             my $shallower = $i-1;
  875:             next if ($shallower == 0);
  876:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
  877:             if ($cat ne '') {
  878:                 $catlinks .= '<td valign="top">'.
  879:                              '<select name="currcat_'.$shallower.'" onchange="'.
  880:                              'setCatDepth('."'$shallower'".');this.form.submit();">';
  881:                 if (ref($cats[$shallower]{$container}) eq 'ARRAY') {
  882:                     $catlinks .= '<option value="">'.&mt('De-select').'</option>';
  883:                     for (my $j=0; $j<@{$cats[$shallower]{$container}}; $j++) {
  884:                         my $name = $cats[$shallower]{$container}[$j];
  885:                         my $item = &escape($name).':'.&escape($container).':'.$shallower;
  886:                         my $selected = '';
  887:                         if ($item eq $env{'form.currcat_'.$shallower}) {
  888:                             $selected = ' selected="selected"';
  889:                         }
  890:                         $catlinks .= 
  891:                            '<option value="'.$item.'"'.$selected.'>'.$name.'</option>';
  892:                     }
  893:                 }
  894:                 $catlinks .= '</select>';
  895:             }
  896:             unless ($i == $deeper) {
  897:                 $catlinks .= $crumbsymbol;
  898:             } 
  899:         }
  900:         my ($cat,$container,$depth);
  901:         if ($env{'form.currcat_'.$currdepth} eq '') {
  902:             my $shallower = $currdepth - 1;
  903:             ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
  904:         } else {
  905:             ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$currdepth});
  906:         }
  907:         my $deeperlevel = $depth +1;
  908:         if (ref($cats[$deeperlevel]{$cat}) eq 'ARRAY') {
  909:             $has_subcats = 1;
  910:             my $buttontext = &mt('Show subcategories');
  911:             my $selitem = 'currcat_'.$deeperlevel;
  912:             $catlinks .= '&nbsp;<select name="'.$selitem.'" onchange="this.form.submit()">';
  913:             if (@{$cats[$deeperlevel]{$cat}}) {
  914:                 $catlinks .= '<option value="" selected="selected">'.
  915:                              &mt('Subcategory ...').'</option>';
  916:             }
  917:             for (my $k=0; $k<@{$cats[$deeperlevel]{$cat}}; $k++) {
  918:                 my $name = $cats[$deeperlevel]{$cat}[$k];
  919:                 my $item = &escape($name).':'.&escape($cat).':'.$deeperlevel;
  920:                 $catlinks .= '<option value="'.$item.'">'.$name.'</option>'."\n";
  921:             }
  922:             $catlinks .= '</select>'."\n";
  923:         } elsif ($cat ne 'instcode') {
  924:             $catlinks .= '&nbsp;'.&mt('(No subcategories)');
  925:         }
  926:     } else {
  927:         $selitem = 'currcat_0';
  928:     }
  929:     $catlinks .= $currcat_str.'</td></tr></table></td>';
  930:     return ($catlinks,$has_subcats,$selitem);
  931: }
  932: 
  933: sub main_category_selector {
  934:     my (@cats) = @_;
  935:     my $maincatlinks = '<select name="currcat_0" onchange="setCatDepth('."'0'".');this.form.submit();">'."\n";
  936:     if (ref($cats[0]) eq 'ARRAY') {
  937:         if (@{$cats[0]} > 1) {
  938:             my $selected = '';
  939:             if ($env{'form.currcat_0'} eq '') {
  940:                 $selected = ' selected="selected"';    
  941:             }
  942:             $maincatlinks .= 
  943:                 '<option value=""'.$selected.'>'.&mt('Select').'</option>'."\n";
  944:         }
  945:         for (my $i=0; $i<@{$cats[0]}; $i++) {
  946:             my $name = $cats[0][$i];
  947:             my $item = &escape($name).'::0';
  948:             my $selected;
  949:             if ($env{'form.currcat_0'} eq $item) {
  950:                 $selected = ' selected="selected"';
  951:             }
  952:             $maincatlinks .= '<option value="'.$item.'"'.$selected.'>';
  953:             if ($name eq 'instcode') {
  954:                 $maincatlinks .= &mt('Official courses (with institutional codes)');
  955:             } elsif ($name eq 'communities') {
  956:                 $maincatlinks .= &mt('Communities');
  957:             } else {
  958:                 $maincatlinks .= $name;
  959:             }
  960:             $maincatlinks .= '</option>'."\n";
  961:         }
  962:         $maincatlinks .= '</select>'."\n";
  963:     }
  964:     return $maincatlinks;
  965: }
  966: 
  967: sub get_depth_values {
  968:     my $currdepth = 0;
  969:     my $deeper = 0;
  970:     if ($env{'form.catalog_maxdepth'} ne '') {
  971:         $currdepth = $env{'form.catalog_maxdepth'};
  972:         if ($env{'form.currcat_'.$currdepth} eq '') {
  973:             $deeper = $currdepth;
  974:         } else {
  975:             $deeper = $currdepth + 1;
  976:         }
  977:     }
  978:     return ($currdepth,$deeper);
  979: }
  980: 
  981: sub additional_filters {
  982:     my ($codedom,$has_subcats,$canviewall) = @_;
  983:     my $is_dc = &user_is_dc($codedom);
  984:     my $output = '<div class="LC_left_float">';
  985:     if ($is_dc || $canviewall) {
  986:         $output .= '<fieldset><legend>'.&mt('Options').'</legend>';
  987:     }
  988:     $output .= '<table><tr><td valign="top">';
  989:     if (($env{'form.currcat_0'} ne 'instcode::0') &&
  990:         ($env{'form.currcat_0'} ne '') && ($has_subcats)) {
  991:         my $include_subcat_status;
  992:         if ($env{'form.withsubcats'}) {
  993:             $include_subcat_status = 'checked="checked" ';
  994:         }
  995:         my $counter = $env{'form.catalog_maxdepth'};
  996:         if ($counter > 0) {
  997:             if ($env{'form.state'} eq 'listing') {
  998:                 $counter --;
  999:             } elsif ($env{'form.currcat_'.$counter} eq '') {
 1000:                 $counter --;
 1001:             }
 1002:         }
 1003:         my ($catname) = split(/:/,$env{'form.currcat_'.$counter});
 1004:         if ($catname ne '') {
 1005:             $output .= '<span class="LC_nobreak"><label>'.
 1006:                        '<input type="checkbox" name="withsubcats" value="1" '.
 1007:                        $include_subcat_status.'/>'.
 1008:                        &mt('Include subcategories within "[_1]"',
 1009:                            &unescape($catname)).'</label></span><br />';
 1010:         }
 1011:     }
 1012:     my $show_selfenroll_status;
 1013:     if ($env{'form.showselfenroll'}) {
 1014:         $show_selfenroll_status = 'checked="checked" ';
 1015:     }
 1016:     my $selfenroll_text;
 1017:     if ($env{'form.currcat_0'} eq 'communities::0') {
 1018:         $selfenroll_text = &mt('Only show communities which currently allow self-enrollment (or will allow it in the future)');
 1019:     } else {
 1020:         $selfenroll_text = &mt('Only show courses which currently allow self-enrollment (or will allow it in the future)');
 1021:     }
 1022:     $output .= '<span class="LC_nobreak">'.
 1023:                '<label><input type="checkbox" name="showselfenroll" value="1" '.
 1024:                $show_selfenroll_status.'/>'.$selfenroll_text.
 1025:                '</label></span><br />';
 1026:     if ($is_dc || $canviewall) {
 1027:         my ($titlesref,$orderref) = &get_statustitles('filters');
 1028:         my $showdetails_status;
 1029:         if ($env{'form.showdetails'}) {
 1030:             $showdetails_status = 'checked="checked" ';
 1031:         }
 1032:         my $showhidden_status;
 1033:         if ($env{'form.showhidden'}) {
 1034:             $showhidden_status = 'checked="checked" ';
 1035:         }
 1036:         my @currstatuses = &Apache::loncommon::get_env_multiple('form.showcounts');
 1037:         my ($details_text,$hidden_text,$statusdisplay,$cellborder);
 1038:         my $wasactivedisplay = 'none';
 1039:         if ($env{'form.showdetails'}) {
 1040:             $statusdisplay = 'block';
 1041:             $cellborder = 'border-left: 1px solid;';
 1042:             if (grep(/^Previous$/,@currstatuses)) {
 1043:                 $wasactivedisplay = 'block';
 1044:             }
 1045:         } else {
 1046:             $statusdisplay = 'none';
 1047:             $cellborder = 'border-left: 0px';
 1048:         }
 1049:         if ($env{'form.currcat_0'} eq 'communities::0') {
 1050:             $details_text = &mt('Show full details for each community (domain staff only)');
 1051:             $hidden_text = &mt('Include communities set to be hidden from catalog (domain staff only)');
 1052:         } else {
 1053:             $details_text = &mt('Show full details for each course (domain staff only)');
 1054:             $hidden_text = &mt('Include courses set to be hidden from catalog (domain staff only)');
 1055:         }
 1056:         if ($is_dc) {
 1057:             $output .= '<span class="LC_nobreak">'.
 1058:                        '<label><input type="checkbox" name="showhidden" value="1" '.
 1059:                        $showhidden_status.'/>'.$hidden_text.
 1060:                        '</label></span><br />'."\n";
 1061:         }
 1062:         $output .= '<span class="LC_nobreak">'.
 1063:                    '<label><input type="checkbox" name="showdetails" value="1" '.
 1064:                    $showdetails_status.'onclick="toggleStatuses();" />'.
 1065:                    $details_text.'</label></span></td>'."\n".
 1066:                    '<td id="statuscell" valign="top" style="'.$cellborder.'">'.
 1067:                    '<div id="statuschoice" style="display:'.$statusdisplay.'">';
 1068:         if (ref($orderref) eq 'ARRAY') {
 1069:             if (@{$orderref} > 0) {
 1070:                 foreach my $type (@{$orderref}) {
 1071:                     my $checked;
 1072:                     if (grep(/^\Q$type\E$/,@currstatuses)) {
 1073:                         $checked = ' checked="checked"';
 1074:                     }
 1075:                     my $title;
 1076:                     if (ref($titlesref) eq 'HASH') {
 1077:                         $title = $titlesref->{$type};
 1078:                     }
 1079:                     unless ($title) {
 1080:                         $title = &mt($type);
 1081:                     }
 1082:                     my $onclick;
 1083:                     if ($type eq 'Previous') {
 1084:                         $onclick = ' onclick="toggleWasActive();"'; 
 1085:                     }
 1086:                     $output .= '<span class="LC_nobreak">'.
 1087:                                '<label><input type="checkbox" id="counts_'.$type.'"'.
 1088:                                ' name="showcounts" value="'.$type.'"'.$checked.$onclick.
 1089:                                ' />'.$title.'</label></span>';
 1090:                     if ($type eq 'Previous') {
 1091:                         my %milestonetext = &Apache::lonlocal::texthash (
 1092:                             accessend => 'immediately prior to default end access date',
 1093:                             enrollend => 'immediately prior to end date for auto-enrollment',
 1094:                             date      => 'immediately prior to specific date:',
 1095:                         );
 1096:                         my @statuses = &Apache::loncommon::get_env_multiple('form.showcounts');
 1097:                         $output .= '<span id="choosewasacctext" class="LC_nobreak">';
 1098:                         if ($checked) {
 1099:                             $output .= &get_wasactive_text();
 1100:                         }
 1101:                         $output .= '</span>'.
 1102:                                    '<div id="choosewasactive" style="display:'.$wasactivedisplay.'">'.
 1103:                                    '<table>';
 1104:                         my @milestones = ('accessend');
 1105:                         if (&Apache::lonnet::auto_run(undef,$codedom)) {
 1106:                             push(@milestones,'enrollend');
 1107:                         }
 1108:                         push(@milestones,'date');
 1109:                         foreach my $item (@milestones) {
 1110:                             my $checked;
 1111:                             if ($env{'form.state'} eq 'listing') {
 1112:                                 if ($env{'form.wasactive'} eq $item) {
 1113:                                     $checked = ' checked="checked"';
 1114:                                 }
 1115:                             } elsif ($item eq 'accessend') {
 1116:                                 $checked = ' checked="checked"';
 1117:                             }
 1118:                             $output .=
 1119:                                 '<tr><td width="10">&nbsp;</td><td>'.
 1120:                                 '<span class="LC_nobreak"><label>'.
 1121:                                 '<input type="radio" value="'.$item.'" name="wasactive"'.$checked.' />'.
 1122:                                 $milestonetext{$item}.'</label></span>';
 1123:                             if ($item eq 'date') {
 1124:                                 my $wasactiveon;
 1125:                                 if (grep(/^Previous$/,@currstatuses)) {
 1126:                                     $wasactiveon =
 1127:                                         &Apache::lonhtmlcommon::get_date_from_form('wasactiveon');
 1128:                                 } else {
 1129:                                     $wasactiveon = 'now';
 1130:                                 }
 1131:                                 $output .= ' '.
 1132:                                     &Apache::lonhtmlcommon::date_setter('coursecatalog',
 1133:                                                                         'wasactiveon',
 1134:                                                                         $wasactiveon,
 1135:                                                                         '','','',1,'',
 1136:                                                                         '','',1);
 1137:                             }
 1138:                             $output .= '</td></tr>';
 1139:                         }
 1140:                         $output .= '</table></div>';
 1141:                     }
 1142:                     $output .= '<br />';
 1143:                 }
 1144:             }
 1145:         }
 1146:         $output .= '</div></td>';
 1147:     } else {
 1148:         $output .= '</td>';  
 1149:     }
 1150:     $output .= '</tr></table></fieldset></div>'.
 1151:                '<div style="clear:both;margin:0;"></div>';  
 1152:     return $output;
 1153: }
 1154: 
 1155: sub user_is_dc {
 1156:     my ($codedom) = @_;
 1157:     if (exists($env{'user.role.dc./'.$codedom.'/'})) {
 1158:         my $livedc = 1;
 1159:         my $now = time;
 1160:         my ($start,$end)=split(/\./,$env{'user.role.dc./'.$codedom.'/'});
 1161:         if ($start && $start>$now) { $livedc = 0; }
 1162:         if ($end   && $end  <$now) { $livedc = 0; }
 1163:         return $livedc;
 1164:     }
 1165:     return;
 1166: }
 1167: 
 1168: sub canview_all {
 1169:     my ($knownuser,$codedom) = @_;
 1170:     my $canviewall = 0;
 1171:     my $page = 'coursecatalog';
 1172:     if (&LONCAPA::lonauthcgi::can_view($page)) {
 1173:         $canviewall = 1;
 1174:     } elsif (&LONCAPA::lonauthcgi::check_ipbased_access($page)) {
 1175:         $canviewall= 1;
 1176:     } elsif (($knownuser) && ($codedom ne '')) {
 1177:         if (&Apache::lonnet::allowed('dcd',$codedom)) {
 1178:             $canviewall = 1;
 1179:         }
 1180:     }
 1181:     return $canviewall;
 1182: }
 1183: 
 1184: sub get_statustitles {
 1185:     my ($caller) = @_;
 1186:     my @status_order = ('Active','Future','Previous');
 1187:     my %status_title;
 1188:     if ($caller eq 'filters') {
 1189:         %status_title = &Apache::lonlocal::texthash(
 1190:                            Previous => 'Show count for past access',
 1191:                            Active => 'Show count for current student access',
 1192:                            Future => 'Show count for future student access',
 1193:                         );
 1194:         if ($env{'form.currcat_0'} eq 'communities::0') {
 1195:             $status_title{'Active'} = 'Show count for current member access';
 1196:             $status_title{'Future'} = 'Show count for future member access'; 
 1197:         }
 1198:     } else {    
 1199:         %status_title = &Apache::lonlocal::texthash(
 1200:                            Previous => 'Previous access',
 1201:                            Active => 'Current access',
 1202:                            Future => 'Future access',
 1203:                         );
 1204:     }
 1205:     return (\%status_title,\@status_order);
 1206: }
 1207: 
 1208: sub get_wasactive_text {
 1209:     my $wasacctext = ' -- ';
 1210:     if ($env{'form.currcat_0'} eq 'communities::0') {
 1211:         $wasacctext .= &mt('where member access status was current ...');
 1212:     } else {
 1213:         $wasacctext .= &mt('where student access status was current ...');
 1214:     }
 1215:     return $wasacctext;
 1216: }
 1217: 
 1218: sub search_official_courselist {
 1219:     my ($domain,$numtitles,$codetitles) = @_;
 1220:     my $instcode = &Apache::courseclassifier::instcode_search_str($domain,$numtitles,$codetitles);
 1221:     my $showhidden;
 1222:     if (&user_is_dc($domain)) {
 1223:         $showhidden = $env{'form.showhidden'};
 1224:     }
 1225:     my %courses = 
 1226:         &Apache::lonnet::courseiddump($domain,'.',1,$instcode,'.','.',undef,undef,
 1227:                                       'Course',1,$env{'form.showselfenroll'},undef,
 1228:                                       $showhidden,'coursecatalog');
 1229:     return %courses;
 1230: }
 1231: 
 1232: sub search_courselist {
 1233:     my ($domain,$subcats) = @_;
 1234:     my $cat_maxdepth = $env{'form.catalog_maxdepth'};
 1235:     my $filter = $env{'form.currcat_'.$cat_maxdepth};
 1236:     if (($filter eq '') && ($cat_maxdepth > 0)) {
 1237:         my $shallower = $cat_maxdepth - 1;
 1238:         $filter = $env{'form.currcat_'.$shallower};
 1239:     }
 1240:     my %courses;
 1241:     my $filterstr;
 1242:     if ($filter ne '') {
 1243:         if ($env{'form.withsubcats'}) {
 1244:             if (ref($subcats) eq 'HASH') {
 1245:                 if (ref($subcats->{$filter}) eq 'ARRAY') {
 1246:                     $filterstr = join('&',@{$subcats->{$filter}});
 1247:                     if ($filterstr ne '') {
 1248:                         $filterstr = $filter.'&'.$filterstr;
 1249:                     }
 1250:                 } else {
 1251:                     $filterstr = $filter;
 1252:                 }
 1253:             } else {
 1254:                 $filterstr = $filter;
 1255:             }  
 1256:         } else {
 1257:             $filterstr = $filter; 
 1258:         }
 1259:         my ($showhidden,$typefilter);
 1260:         if (&user_is_dc($domain)) {
 1261:             $showhidden = $env{'form.showhidden'};
 1262:         }
 1263:         if ($env{'form.currcat_0'} eq 'communities::0') {
 1264:             $typefilter = 'Community';
 1265:         } else {
 1266:             $typefilter = '.';
 1267:         }
 1268:         %courses = 
 1269:             &Apache::lonnet::courseiddump($domain,'.',1,'.','.','.',undef,undef,
 1270:                                           $typefilter,1,$env{'form.showselfenroll'},
 1271:                                           $filterstr,$showhidden,'coursecatalog');
 1272:     }
 1273:     return %courses;
 1274: }
 1275: 
 1276: sub print_course_listing {
 1277:     my ($domain,$numtitles,$trails,$allitems,$subcats,$codetitles,$canviewall) = @_;
 1278:     my $output;
 1279:     my %courses;
 1280:     my $knownuser = &user_is_known();
 1281:     my $canviewall = &canview_all();
 1282:     my $details = $env{'form.coursenum'};
 1283:     if (&user_is_dc($domain) || $canviewall) {
 1284:         if ($env{'form.showdetails'}) {
 1285:             $details = 1;
 1286:         }
 1287:     }
 1288:     if ($env{'form.coursenum'} ne '') {
 1289:         %courses = &Apache::lonnet::courseiddump($domain,'.',1,'.','.',
 1290:                                                  $env{'form.coursenum'},
 1291:                                                  undef,undef,'.',1);
 1292:         if (keys(%courses) == 0) {
 1293:             $output = '<p class="LC_error">';
 1294:             if ($env{'form.currcat_0'} eq 'communities::0') {
 1295:                 $output .= &mt('The courseID provided does not match a community in this domain.');
 1296:             } else { 
 1297:                 $output .= &mt('The courseID provided does not match a course in this domain.');
 1298:             }
 1299:             $output .= '</p>';
 1300:             return $output;
 1301:         }
 1302:     } else {
 1303:         if ($env{'form.currcat_0'} eq 'instcode::0') {
 1304:             %courses = &search_official_courselist($domain,$numtitles,$codetitles);
 1305:         } else {
 1306:             %courses = &search_courselist($domain,$subcats);
 1307:         }
 1308:         if (keys(%courses) == 0) {
 1309:             $output = '<p class="LC_info">';
 1310:             if ($env{'form.currcat_0'} eq 'communities::0') {
 1311:                 $output .= &mt('No communities match the criteria you selected.');
 1312:             } else {
 1313:                 $output .= &mt('No courses match the criteria you selected.');
 1314:             }
 1315:             $output .= '</p>';
 1316:             return $output;
 1317:         }
 1318:         if (($knownuser) && (!$env{'form.showdetails'}) && (!&user_is_dc($domain)) && (!$canviewall)) {
 1319:             $output = '<b>'.&mt('Note for students:').'</b> '
 1320:                      .&mt('If you are officially enrolled in a course but the course is not listed in your LON-CAPA courses, click the "Show more details" link for the specific course and check the default access dates and/or automated enrollment settings.')
 1321:                      .'<br /><br />';
 1322:         }
 1323:     }
 1324:     my $now = time;
 1325:     $output .= &construct_data_table($knownuser,$domain,\%courses,$details,undef,
 1326:                                      $now,$trails,$allitems,$canviewall);
 1327:     $output .= "\n".'<form name="linklaunch" method="post" action="">'.
 1328:                '<input type="hidden" name="backto" value="coursecatalog" />'.
 1329:                '<input type="hidden" name="courseid" value="" />'.
 1330:                &Apache::lonhtmlcommon::echo_form_input(['catalogfilter','courseid']).'</form>';
 1331:     return $output;
 1332: }
 1333: 
 1334: sub construct_data_table {
 1335:     my ($knownuser,$domain,$courses,$details,$usersections,$now,$trails,$allitems,$canviewall) = @_;
 1336:     my %sortname;
 1337:     if (($details eq '') || ($env{'form.showdetails'})) {
 1338:         $sortname{'Code'} = 'code';
 1339:         $sortname{'Categories'} = 'cats';
 1340:         $sortname{'Title'} = 'title';
 1341:         $sortname{'Owner & Co-owner(s)'} = 'owner';
 1342:     }
 1343:     my $output = &Apache::loncommon::start_data_table().
 1344:                  &Apache::loncommon::start_data_table_header_row();
 1345:     my @coltitles = ('Count');
 1346:     if ($env{'form.currcat_0'} eq 'instcode::0') {
 1347:         push(@coltitles,'Code');
 1348:     } else {
 1349:         push(@coltitles,'Categories');
 1350:     }
 1351:     push(@coltitles,('Sections','Crosslisted','Title','Owner & Co-owner(s)'));
 1352:     if (ref($usersections) eq 'HASH') {
 1353:        $coltitles[1] = 'Your Section';
 1354:     }
 1355:     foreach my $item (@coltitles) {
 1356:         $output .= '<th>';
 1357:         if (defined($sortname{$item})) {
 1358:             $output .= '<a href="javascript:changeSort('."'$sortname{$item}'".')">'.&mt($item).'</a>';
 1359:         } elsif ($item eq 'Count') {
 1360:             $output .= '&nbsp;&nbsp;';
 1361:         } else {
 1362:             $output .= &mt($item);
 1363:         }
 1364:         $output .= '</th>';
 1365:     }
 1366:     my (@fields,%fieldtitles,$wasactiveon);
 1367:     if ($knownuser || ($canviewall && $details)) {
 1368:         if ($details) {
 1369:             if ($env{'form.currcat_0'} eq 'communities::0') {
 1370:                 $output .= '<th>'.&mt('Default Access Dates for Members').'</th>'.
 1371:                            '<th>'.&mt('Member Counts').'</th>';
 1372:             } else {
 1373:                 $output .=
 1374:                     '<th>'.&mt('Default Access Dates for Students').'</th>'.
 1375:                     '<th>'.&mt('Student Counts').'</th>'.
 1376:                     '<th>'.&mt('Auto-enrollment of[_1]registered students','<br />').'</th>';
 1377:             }
 1378:             my ($titlesref,$orderref) = &get_statustitles();
 1379:             my @statuses;
 1380:             if (&user_is_dc($domain) || $canviewall) {
 1381:                 @statuses = &Apache::loncommon::get_env_multiple('form.showcounts');
 1382:                 if (grep(/^Previous$/,@statuses)) {
 1383:                     if ($env{'form.wasactive'} eq 'date') { 
 1384:                         $wasactiveon = 
 1385:                             &Apache::lonhtmlcommon::get_date_from_form('wasactiveon');
 1386:                     } else {
 1387:                         $wasactiveon = $env{'form.wasactive'};
 1388:                     }
 1389:                 }
 1390:                 if (ref($orderref) eq 'ARRAY') {
 1391:                     foreach my $status (@{$orderref}) {
 1392:                         if (grep(/^\Q$status\E$/,@statuses)) {
 1393:                             push(@fields,$status); 
 1394:                         }
 1395:                     }
 1396:                 }
 1397:             } else {
 1398:                 @fields = ('Active','Future');
 1399:             }
 1400:             foreach my $status (@fields) {
 1401:                 my $title;
 1402:                 if (ref($titlesref) eq 'HASH') {
 1403:                     $title = $titlesref->{$status};
 1404:                 }
 1405:                 unless ($title) {
 1406:                     $title = &mt($status);
 1407:                 }
 1408:                 $fieldtitles{$status} = $title;
 1409:             }
 1410:         } else {
 1411:             $output .= '<th>'.&mt('Details').'</th>';
 1412:         }
 1413:     }
 1414:     $output .= '<th>'.&mt('Self-enroll (if permitted)').'</th>';
 1415:     &Apache::loncommon::end_data_table_header_row();
 1416:     my (%numbers,%creditsum);
 1417:     my ($showcredits,$defofficial,$defunofficial,$deftextbook);
 1418:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
 1419:     unless ($env{'form.currcat_0'} eq 'communities::0') {
 1420:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'} || $domdefaults{'textbookcredits'}) {
 1421:             $showcredits = 1;
 1422:             $defofficial = $domdefaults{'officialcredits'};
 1423:             $defunofficial = $domdefaults{'unofficialcredits'};
 1424:             $deftextbook = $domdefaults{'textbookcredits'};
 1425:         }
 1426:     }
 1427:     my %courseinfo = &build_courseinfo_hash($courses,$knownuser,$domain,$canviewall,$details,
 1428:                                             $usersections,\@fields,\%fieldtitles,
 1429:                                             $wasactiveon,\%numbers,\%creditsum,
 1430:                                             $showcredits,$defofficial,$defunofficial,$deftextbook);
 1431:     my %Sortby;
 1432:     foreach my $course (sort(keys(%{$courses}))) {
 1433:         if ($env{'form.sortby'} eq 'code') {
 1434:             push(@{$Sortby{$courseinfo{$course}{'code'}}},$course);
 1435:         } elsif ($env{'form.sortby'} eq 'cats') {
 1436:             push(@{$Sortby{$courseinfo{$course}{'categories'}}},$course);
 1437:         } elsif ($env{'form.sortby'} eq 'owner') {
 1438:             push(@{$Sortby{$courseinfo{$course}{'ownerlastnames'}}},$course);
 1439:         } else {
 1440:             my $clean_title = $courseinfo{$course}{'title'};
 1441:             $clean_title =~ s/\W+//g;
 1442:             if ($clean_title eq '') {
 1443:                 $clean_title = $courseinfo{$course}{'title'};
 1444:             }
 1445:             push(@{$Sortby{$clean_title}},$course);
 1446:         }
 1447:     }
 1448:     my @sorted_courses;
 1449:     if (($env{'form.sortby'} eq 'code') || ($env{'form.sortby'} eq 'owner') ||
 1450:         ($env{'form.sortby'} eq 'cats')) {
 1451:         @sorted_courses = sort(keys(%Sortby));
 1452:     } else {
 1453:         @sorted_courses = sort { lc($a) cmp lc($b) } (keys(%Sortby));
 1454:     }
 1455:     my $count = 1;
 1456:     my $totalsec = 0;
 1457:     foreach my $item (@sorted_courses) {
 1458:         foreach my $course (@{$Sortby{$item}}) {
 1459:             $output.=&Apache::loncommon::start_data_table_row(); 
 1460:             $output.=&courseinfo_row($courseinfo{$course},$knownuser,$details,
 1461:                                      \$count,$now,$course,$trails,$allitems,\%numbers,$canviewall);
 1462:             $output.=&Apache::loncommon::end_data_table_row();
 1463:         }
 1464:     }
 1465:     if (($knownuser || $canviewall) && ($count > 1) && $env{'form.showdetails'}) {
 1466:         if (&user_is_dc($domain) || $canviewall) {
 1467:             my %lt = &Apache::lonlocal::texthash (
 1468:                                                      'Active'   => 'Total current students',
 1469:                                                      'Future'   => 'Total future students',
 1470:                                                      'Previous' => 'Total previous students',
 1471:                                                      'courses'  => 'Total unique codes and courses without codes',
 1472:                                                      'sections' => 'Total sections',
 1473:                                                      'xlists'   => 'Total cross-listings',
 1474:                                                  );
 1475:             if ($showcredits) {
 1476:                 $lt{'cr_Active'} = &mt('Total current student credit hours');
 1477:                 $lt{'cr_Future'} = &mt('Total future student credit hours');
 1478:                 $lt{'cr_Previous'} = &mt('Total previous student credit hours');
 1479:             }
 1480:             if ($env{'form.currcat_0'} eq 'communities::0') {
 1481:                 $lt{'courses'} = &mt('Total communities');
 1482:                 $lt{'Active'} = &mt('Total current members'); 
 1483:                 $lt{'Future'} = &mt('Total future members');
 1484:                 $lt{'Previous'} = &mt('Total previous members');
 1485:             }
 1486:             my $colspan = 8;
 1487:             if ($showcredits) {
 1488:                 $colspan = 4;
 1489:             }
 1490:             $output .= '<tr class="LC_footer_row">'.
 1491:                        '<td colspan="2">&nbsp;</td>'.
 1492:                        '<td colspan="'.$colspan.'">'.
 1493:                        '<table border="0">';
 1494:             foreach my $item ('courses','sections','xlists') {
 1495:                 $output .= '<tr>'.
 1496:                            '<td>'.$lt{$item}.'</td><td>&nbsp;</td>'.
 1497:                            '<td align="right">'.$numbers{$item}.'</td>'.
 1498:                            '</tr>'."\n";
 1499:             }
 1500:             if (@fields > 0) { 
 1501:                 foreach my $status (@fields) {
 1502:                     $output .= '<tr>'.
 1503:                                '<td>'.$lt{$status}.'</td><td>&nbsp;</td>'.
 1504:                                '<td align="right">'.$numbers{$status}.'</td>'.
 1505:                                '</tr>'."\n";
 1506:                 }
 1507:             }
 1508:             $output .= '</table></td>';
 1509:             if ($showcredits) {
 1510:                 $output .= '<td colspan="'.$colspan.'" valign="bottom"><table>';
 1511:                 foreach my $status (@fields) {
 1512:                     $output .= '<tr>'.
 1513:                                '<td>'.$lt{'cr_'.$status}.'</td><td>&nbsp;</td>'.
 1514:                                '<td align="right">'.$creditsum{$status}.'</td></tr>';
 1515:                 }
 1516:                 $output .= '</table></td></tr>';
 1517:             }
 1518:         }
 1519:     }
 1520:     $output .= &Apache::loncommon::end_data_table();
 1521:     return $output;
 1522: }
 1523: 
 1524: sub build_courseinfo_hash {
 1525:     my ($courses,$knownuser,$domain,$canviewall,$details,$usersections,$fields,$fieldtitles,
 1526:         $wasactiveon,$numbers,$creditsum,$showcredits,$defofficial,$defunofficial) = @_;
 1527:     my %courseinfo;
 1528:     my $now = time;
 1529:     my $gettotals;
 1530:     if ((keys(%{$courses}) > 0) && (&user_is_dc($domain) || $canviewall) && ($details)) {
 1531:         $gettotals = 1;
 1532:     }
 1533:     my (%uniquecodes,$nocodes,$defcreds);
 1534:     foreach my $course (keys(%{$courses})) {
 1535:         my $descr;
 1536:         if (ref($courses->{$course}) eq 'HASH') {
 1537:             $descr = $courses->{$course}{'description'}; 
 1538:         }
 1539:         my $cleandesc=&HTML::Entities::encode($descr,'<>&"');
 1540:         $cleandesc=~s/'/\\'/g;
 1541:         $cleandesc =~ s/^\s+//;
 1542:         my ($cdom,$cnum)=split(/\_/,$course);
 1543:         my ($instcode,$singleowner,$ttype,$selfenroll_types,
 1544:             $selfenroll_start,$selfenroll_end,@owners,%ownernames,$categories);
 1545:         if (ref($courses->{$course}) eq 'HASH') {
 1546:             $descr = $courses->{$course}{'description'};
 1547:             $instcode =  $courses->{$course}{'inst_code'};
 1548:             $singleowner = $courses->{$course}{'owner'};
 1549:             $ttype =  $courses->{$course}{'type'};
 1550:             $selfenroll_types = $courses->{$course}{'selfenroll_types'};
 1551:             $selfenroll_start = $courses->{$course}{'selfenroll_start_date'};
 1552:             $selfenroll_end = $courses->{$course}{'selfenroll_end_date'};
 1553:             $categories = $courses->{$course}{'categories'};
 1554:             push(@owners,$singleowner);
 1555:             if ($courses->{$course}{'co-owners'} ne '') {
 1556:                 foreach my $item (split(/,/,$courses->{$course}{'co-owners'})) {
 1557:                     push(@owners,$item);
 1558:                 }
 1559:             }
 1560:         }
 1561:         if ($instcode ne '') {
 1562:             $uniquecodes{$instcode} = 1;
 1563:         } else {
 1564:             $nocodes ++;
 1565:         } 
 1566:         foreach my $owner (@owners) {
 1567:             my ($ownername,$ownerdom); 
 1568:             if ($owner =~ /:/) {
 1569:                 ($ownername,$ownerdom) = split(/:/,$owner);
 1570:             } else {
 1571:                 $ownername = $owner;
 1572:                 if ($owner ne '') {
 1573:                     $ownerdom = $cdom;
 1574:                 }
 1575:             }
 1576:             if ($ownername ne '' && $ownerdom ne '') {
 1577:                 my %namehash=&Apache::loncommon::getnames($ownername,$ownerdom);
 1578:                 $ownernames{$ownername.':'.$ownerdom} = \%namehash; 
 1579:             }
 1580:         }
 1581:         $courseinfo{$course}{'cdom'} = $cdom;
 1582:         $courseinfo{$course}{'cnum'} = $cnum;
 1583:         $courseinfo{$course}{'code'} = $instcode;
 1584:         my @lastnames;
 1585:         foreach my $owner (keys(%ownernames)) {
 1586:             if (ref($ownernames{$owner}) eq 'HASH') {
 1587:                 push(@lastnames,$ownernames{$owner}{'lastname'});
 1588:             }
 1589:         }
 1590:         $courseinfo{$course}{'ownerlastnames'} = join(', ',sort(@lastnames));
 1591:         $courseinfo{$course}{'title'} = $cleandesc;
 1592:         $courseinfo{$course}{'owner'} = $singleowner;
 1593:         $courseinfo{$course}{'selfenroll_types'} = $selfenroll_types;
 1594:         $courseinfo{$course}{'selfenroll_start'} = $selfenroll_start;
 1595:         $courseinfo{$course}{'selfenroll_end'} = $selfenroll_end;
 1596:         $courseinfo{$course}{'categories'} = $categories;
 1597: 
 1598:         my %coursehash = &Apache::lonnet::dump('environment',$cdom,$cnum);
 1599:         my @classids;
 1600:         my @crosslistings;
 1601:         my ($seclist,$numsec) = 
 1602:             &identify_sections($coursehash{'internal.sectionnums'});
 1603:         $courseinfo{$course}{'seclist'} = $seclist;
 1604:         my ($xlist_items,$numxlist) = 
 1605:             &identify_sections($coursehash{'internal.crosslistings'});
 1606:         if (ref($numbers) eq 'HASH') {
 1607:             $numbers->{'sections'} += $numsec; 
 1608:             $numbers->{'xlists'} += $numxlist;  
 1609:         }
 1610:         my $showsyllabus = 1; # default is to include a syllabus link
 1611:         if (defined($coursehash{'showsyllabus'})) {
 1612:             $showsyllabus = $coursehash{'showsyllabus'};
 1613:         }
 1614:         $courseinfo{$course}{'showsyllabus'} = $showsyllabus;
 1615:         if ($showcredits) {
 1616:             if ($coursehash{'internal.defaultcredits'}) {
 1617:                 $courseinfo{$course}{'defaultcredits'} = $coursehash{'internal.defaultcredits'};
 1618:             } elsif ($instcode ne '') {
 1619:                 $courseinfo{$course}{'defaultcredits'} = $defofficial;
 1620:             } else {
 1621:                 $courseinfo{$course}{'defaultcredits'} = $defunofficial;
 1622:             }
 1623:             $defcreds = $courseinfo{$course}{'defaultcredits'};
 1624:         }
 1625:         if (((defined($env{'form.coursenum'}) && ($cnum eq $env{'form.coursenum'}))) ||
 1626:             (($knownuser || $canviewall) && ($details == 1))) {
 1627:             my $milestone;
 1628:             if ($wasactiveon eq 'accessend') {
 1629:                 if ($coursehash{'default_enrollment_end_date'}) {
 1630:                     $milestone = $coursehash{'default_enrollment_end_date'};
 1631:                 } else {
 1632:                     $milestone = time;
 1633:                 }
 1634:             } elsif ($wasactiveon eq 'enrollend') {
 1635:                 if ($coursehash{'internal.autoend'}) {
 1636:                     $milestone = $coursehash{'internal.autoend'};
 1637:                 } else {
 1638:                     $milestone = time;
 1639:                 }
 1640:             } else {
 1641:                 $milestone = $wasactiveon;
 1642:             }
 1643:             $courseinfo{$course}{'counts'} =  
 1644:                 &count_students($cdom,$cnum,$numsec,$fields,$fieldtitles,$gettotals,
 1645:                                 $numbers,$creditsum,$showcredits,$defcreds,$milestone);
 1646:             if ($instcode ne '') {
 1647:                 $courseinfo{$course}{'autoenrollment'} =
 1648:                     &autoenroll_info(\%coursehash,$now,$seclist,$xlist_items,
 1649:                                      $instcode,\@owners,$cdom,$cnum);
 1650:             }
 1651:             my $startaccess = '';
 1652:             my $endaccess = '';
 1653:             my $accessdates;
 1654:             if ( defined($coursehash{'default_enrollment_start_date'}) ) {
 1655:                 $startaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_start_date'});
 1656:             }
 1657:             if ( defined($coursehash{'default_enrollment_end_date'}) ) {
 1658:                 $endaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_end_date'});
 1659:                 if ($coursehash{'default_enrollment_end_date'} == 0) {
 1660:                     $endaccess = &mt('No ending date');
 1661:                 }
 1662:             }
 1663:             if ($startaccess) {
 1664:                 $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$startaccess).'<br />';
 1665:             }
 1666:             if ($endaccess) {
 1667:                 $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$endaccess).'<br />';
 1668:             }
 1669:             if (($selfenroll_types ne '') && 
 1670:                 ($selfenroll_end > 0 && $selfenroll_end > $now)) {
 1671:                 my ($selfenroll_start_access,$selfenroll_end_access);
 1672:                 if (($coursehash{'default_enrollment_start_date'} ne 
 1673:                      $coursehash{'internal.selfenroll_start_access'}) ||
 1674:                    ($coursehash{'default_enrollment_end_date'} ne 
 1675:                     $coursehash{'internal.selfenroll_end_access'})) {
 1676:                     if ( defined($coursehash{'internal.selfenroll_start_access'}) ) {
 1677:                         $selfenroll_start_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_start_access'});
 1678:                     }
 1679:                     if ( defined($coursehash{'default_enrollment_end_date'}) ) {
 1680:                         $selfenroll_end_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_end_access'});
 1681:                         if ($coursehash{'internal.selfenroll_end_access'} == 0) {
 1682:                             $selfenroll_end_access = &mt('No ending date');
 1683:                         }
 1684:                     }
 1685:                     if ($selfenroll_start_access || $selfenroll_end_access) {
 1686:                         $accessdates .= '<br/><br /><i>'.&mt('Self-enrollers:').'</i><br />';
 1687:                         if ($selfenroll_start_access) {
 1688:                             $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$selfenroll_start_access).'<br />';
 1689:                         }
 1690:                         if ($selfenroll_end_access) {
 1691:                             $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$selfenroll_end_access).'<br />';
 1692:                         }
 1693:                     }
 1694:                 }
 1695:             }
 1696:             $courseinfo{$course}{'access'} = $accessdates;
 1697:         }
 1698:         if ($xlist_items eq '') {
 1699:             $xlist_items = &mt('No');
 1700:         }
 1701:         $courseinfo{$course}{'xlist'} = $xlist_items;
 1702:     }
 1703:     if (ref($numbers) eq 'HASH') {
 1704:         $numbers->{'courses'} = $nocodes + scalar(keys(%uniquecodes));
 1705:     }
 1706:     return %courseinfo;
 1707: }
 1708: 
 1709: sub count_students {
 1710:     my ($cdom,$cnum,$numsec,$fieldsref,$titlesref,$getcounts,$numbers,$creditsum,
 1711:         $showcredits,$defcreds,$wasactiveon) = @_;
 1712:     my $countslist = '<span class="LC_nobreak">'.
 1713:                      &mt('[quant,_1,section,sections,No sections]',$numsec).'</span>';
 1714:     my (@fields,%titles,$showexpired);
 1715:     if ((ref($fieldsref) eq 'ARRAY') && (ref($titlesref) eq 'HASH') &&
 1716:         (ref($numbers) eq 'HASH')) {
 1717:         @fields = @{$fieldsref};
 1718:         %titles = %{$titlesref};
 1719:         if (grep(/^Previous$/,@fields)) {
 1720:             $showexpired = 1;
 1721:         }
 1722:     } else {
 1723:         return;
 1724:     }
 1725:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
 1726:     my (%student_count,%credit_count);
 1727:     %student_count = (
 1728:                            Active   => 0,
 1729:                            Future   => 0,
 1730:                            Previous => 0,
 1731:                      );
 1732:     if ($showcredits) {
 1733:         %credit_count = (
 1734:                           Active   => 0,
 1735:                           Future   => 0,
 1736:                           Previous => 0,
 1737:                         );
 1738:     }
 1739:     my %idx;
 1740:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
 1741:     $idx{'end'}    = &Apache::loncoursedata::CL_END();
 1742:     $idx{'credits'} = &Apache::loncoursedata::CL_CREDITS();
 1743:     while (my ($student,$data) = each(%$classlist)) {
 1744:         my $status = $data->[$idx{'status'}];
 1745:         my $credits = $data->[$idx{'credits'}];
 1746:         if ($credits eq '') {
 1747:             $credits = $defcreds;  
 1748:         }
 1749:         if ($status eq 'Expired') {
 1750:             if (($showexpired) &&
 1751:                 ($data->[$idx{'end'}] >= $wasactiveon)) {
 1752:                 $student_count{'Previous'} ++;
 1753:                 if ($showcredits) {
 1754:                     $credit_count{'Previous'} += $credits; 
 1755:                 }
 1756:             }
 1757:         } else {
 1758:             $student_count{$status} ++;
 1759:             if ($showcredits) {
 1760:                 $credit_count{$status} += $credits;
 1761:             }
 1762:         }
 1763:     }
 1764:     if (@fields) {
 1765:         $countslist .= ':<br />';
 1766:         foreach my $status (@fields) {
 1767:             $countslist .= '<span class="LC_nobreak">'.$titles{$status}.': '.
 1768:                            $student_count{$status}.'</span><br />';
 1769:             $numbers->{$status} += $student_count{$status};
 1770:             if ($showcredits) {
 1771:                 $creditsum->{$status} += $credit_count{$status};
 1772:             }
 1773:         }
 1774:     }
 1775:     return $countslist;
 1776: }
 1777: 
 1778: sub courseinfo_row {
 1779:     my ($info,$knownuser,$details,$countref,$now,$course,$trails,$allitems,$numbers,$canviewall) = @_;
 1780:     my ($cdom,$cnum,$title,$ownerlast,$code,$owner,$seclist,$xlist_items,
 1781:         $accessdates,$showsyllabus,$counts,$autoenrollment,$output,$categories);
 1782:     if (ref($info) eq 'HASH') {
 1783:         $cdom = $info->{'cdom'};
 1784:         $cnum = $info->{'cnum'};
 1785:         $title = $info->{'title'};
 1786:         $ownerlast = $info->{'ownerlastnames'};
 1787:         $code = $info->{'code'};
 1788:         $owner = $info->{'owner'};
 1789:         $seclist = $info->{'seclist'};
 1790:         $xlist_items = $info->{'xlist'};
 1791:         $accessdates = $info->{'access'};
 1792:         $counts = $info->{'counts'};
 1793:         $autoenrollment = $info->{'autoenrollment'};
 1794:         $showsyllabus = $info->{'showsyllabus'};
 1795:         $categories = $info->{'categories'};
 1796:     } else {
 1797:         $output = '<td colspan="8">'.&mt('No information available for [_1].',
 1798:                                          $code).'</td>';
 1799:         return $output;
 1800:     }
 1801:     $output .= '<td>'.$$countref.'</td>';
 1802:     if ($env{'form.currcat_0'} eq 'instcode::0') {
 1803:         $output .= '<td>'.$code.'</td>';
 1804:     } else {
 1805:         my ($categorylist,@cats);
 1806:         if ($categories ne '') {
 1807:             @cats = split('&',$categories);
 1808:         }
 1809:         if ((ref($trails) eq 'ARRAY') && (ref($allitems) eq 'HASH')) {
 1810:             my @categories = map { $trails->[$allitems->{$_}]; } @cats;
 1811:             $categorylist = join('<br />',@categories);
 1812:         }
 1813:         if ($categorylist eq '') {
 1814:             $categorylist = '&nbsp;';
 1815:         }
 1816:         $output .= '<td>'.$categorylist.'</td>';
 1817:     }
 1818:     $output .= '<td>'.$seclist.'</td>'.
 1819:                '<td>'.$xlist_items.'</td>'.
 1820:                '<td>'.$title.'&nbsp;<font size="-2">';
 1821:     if ($showsyllabus) {
 1822:         $output .= '<a href="javascript:ToSyllabus('."'$cdom','$cnum'".')">'.&mt('Syllabus').'</a>';
 1823:     } else {
 1824:         $output .= '&nbsp;';
 1825:     }
 1826:     $output .= '</font></td>'.
 1827:                '<td>'.$ownerlast.'</td>';
 1828:     if (($knownuser) || ($canviewall && $details)) {
 1829:         if ($details) {
 1830:             $output .=
 1831:                 '<td>'.$accessdates.'</td>'.
 1832:                 '<td>'.$counts.'</td>';
 1833:             unless ($env{'form.currcat_0'} eq 'communities::0') {
 1834:                 $output .= '<td>'.$autoenrollment.'</td>';
 1835:             }
 1836:         } else {
 1837:             $output .= "<td><a href=\"javascript:setCourseId('$cnum')\">".&mt('Show more details').'</a></td>';
 1838:         }
 1839:     }
 1840:     my $selfenroll = &selfenroll_status($info,$course);
 1841:     if ($selfenroll) {
 1842:         $output .= '<td>'.$selfenroll.'</td>';
 1843:     } else {
 1844:         $output .= '<td>&nbsp;</td>';
 1845:     }
 1846:     $$countref ++;
 1847:     return $output;
 1848: }
 1849: 
 1850: sub selfenroll_status {
 1851:     my ($info,$course) = @_;
 1852:     my $now = time;
 1853:     my $output;
 1854:     if (ref($info) eq 'HASH') {
 1855:         if ($info->{'selfenroll_types'}) {
 1856:             my $showstart = &Apache::lonlocal::locallocaltime($info->{'selfenroll_start'});
 1857:             my $showend = &Apache::lonlocal::locallocaltime($info->{'selfenroll_end'});
 1858:             if (($info->{'selfenroll_end'} > 0) && ($info->{'selfenroll_end'} > $now)) {
 1859:                 if (($info->{'selfenroll_start'} > 0) && ($info->{'selfenroll_start'} > $now)) {
 1860:                     $output = &mt('Starts: [_1]','<span class="LC_cusr_emph">'.$showstart.'</span>').'<br />'.&mt('Ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>');
 1861:                 } else {
 1862:                     $output = '<a href="javascript:ToSelfenroll('."'$course'".')">'.
 1863:                               &mt('Enroll in course').'</a><br />';
 1864:                     if ($info->{'selfenroll_end'} == 0) {
 1865:                         $output .= &mt('Available permanently');
 1866:                     } elsif ($info->{'selfenroll_end'} > $now) {
 1867:                         $output .= &mt('Self-enrollment ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>');
 1868:                     }
 1869:                 }
 1870:             }
 1871:         }
 1872:     }
 1873:     return $output;
 1874: }
 1875: 
 1876: sub identify_sections {
 1877:     my ($seclist) = @_;
 1878:     my @secnums;
 1879:     if ($seclist =~ /,/) {
 1880:         my @sections = split(/,/,$seclist);
 1881:         foreach my $sec (@sections) {
 1882:             $sec =~ s/:[^:]*$//;
 1883:             push(@secnums,$sec);
 1884:         }
 1885:     } else {
 1886:         if ($seclist =~ m/^([^:]+):/) {
 1887:             my $sec = $1;
 1888:             if (!grep(/^\Q$sec\E$/,@secnums)) {
 1889:                 push(@secnums,$sec);
 1890:             }
 1891:         }
 1892:     }
 1893:     @secnums = sort {$a <=> $b} @secnums;
 1894:     $seclist = join(', ',@secnums);
 1895:     my $numsec = @secnums;
 1896:     return ($seclist,$numsec);
 1897: }
 1898: 
 1899: sub get_valid_classes {
 1900:     my ($seclist,$xlist_items,$crscode,$owners,$cdom,$cnum) = @_;
 1901:     my $response;
 1902:     my (@sections,@xlists,%possclasses,%okclasses,%validations);
 1903:     @{$validations{'sections'}} = ();
 1904:     @{$validations{'xlists'}} = ();
 1905:     my $totalitems = 0;
 1906:     if ($seclist) {
 1907:         @sections = split(/,\s+/,$seclist);
 1908:         map { $possclasses{$crscode.$_} = 1; } @sections;
 1909:     }
 1910:     if ($xlist_items) {
 1911:         @xlists = split(/,\s+/,$xlist_items);
 1912:         map { $possclasses{$_} = 1; } @xlists;
 1913:     }
 1914:     my %okclasses = &Apache::lonnet::auto_validate_instclasses($cdom,$cnum,$owners,
 1915:                                                                \%possclasses);
 1916:     if (keys(%okclasses)) {
 1917:         foreach my $sec (@sections) {
 1918:             if ($okclasses{$crscode.$sec}) {
 1919:                 if (!grep(/^\Q$sec$\E/,@{$validations{'sections'}})) {
 1920:                     push(@{$validations{'sections'}},$sec);
 1921:                     $totalitems ++;
 1922:                 }
 1923:             }
 1924:         }
 1925:         foreach my $item (@xlists) {
 1926:             if ($okclasses{$item}) {
 1927:                 if (!grep(/^\Q$item\E$/,@{$validations{'xlists'}})) {
 1928:                     push(@{$validations{'xlists'}},$item);
 1929:                     $totalitems ++;
 1930:                 }
 1931:             }
 1932:         }
 1933:     }
 1934:     if ($totalitems > 0) {
 1935:         if (@{$validations{'sections'}}) {
 1936:             $response = &mt('Sections:').' '.
 1937:                         join(', ',@{$validations{'sections'}}).'<br />';
 1938:         }
 1939:         if (@{$validations{'xlists'}}) {
 1940:             $response .= &mt('Courses:').' '.
 1941:                         join(', ',@{$validations{'xlists'}});
 1942:         }
 1943:     }
 1944:     return $response;
 1945: }
 1946: 
 1947: sub autoenroll_info {
 1948:     my ($coursehash,$now,$seclist,$xlist_items,$code,$owners,$cdom,$cnum) = @_;
 1949:     my $autoenrolldates = &mt('Not enabled');
 1950:     if (defined($coursehash->{'internal.autoadds'}) && $coursehash->{'internal.autoadds'} == 1) {
 1951:         my ($autostart,$autoend);
 1952:         if ( defined($coursehash->{'internal.autostart'}) ) {
 1953:             $autostart = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autostart'});
 1954:         }
 1955:         if ( defined($coursehash->{'internal.autoend'}) ) {
 1956:             $autoend = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autoend'});
 1957:         }
 1958:         if ($coursehash->{'internal.autostart'} > $now) {
 1959:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
 1960:                 $autoenrolldates = &mt('Not enabled');
 1961:             } else {
 1962:                 my $valid_classes = 
 1963:                    &get_valid_classes($seclist,$xlist_items,$code,
 1964:                                       $owners,$cdom,$cnum);
 1965:                 if ($valid_classes ne '') {
 1966:                     $autoenrolldates = &mt('Not enabled').'<br />'
 1967:                                       .&mt('Starts: [_1]',$autostart)
 1968:                                       .'<br />'.$valid_classes;
 1969:                 }
 1970:             }
 1971:         } else {
 1972:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
 1973:                 $autoenrolldates = &mt('Not enabled').'<br />'
 1974:                                   .&mt('Ended: [_1]',$autoend);
 1975:             } else {
 1976:                 my $valid_classes = &get_valid_classes($seclist,$xlist_items,
 1977:                                                        $code,$owners,$cdom,$cnum);
 1978:                 if ($valid_classes ne '') {
 1979:                     $autoenrolldates = &mt('Currently enabled').'<br />'.
 1980:                                        $valid_classes;
 1981:                 }
 1982:             }
 1983:         }
 1984:     }
 1985:     return $autoenrolldates;
 1986: }
 1987: 
 1988: sub user_is_known {
 1989:     my $known = 0;
 1990:     if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public' 
 1991:         && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
 1992:         $known = 1;
 1993:     }
 1994:     return $known;
 1995: }
 1996: 
 1997: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>