Annotation of loncom/interface/coursecatalog.pm, revision 1.88.2.5

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

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