File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.108: download - view: text, annotated - select for diffs
Mon Feb 24 23:20:17 2025 UTC (4 months, 1 week ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- WCAG 2 compliance.

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

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