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

1.17      albertel    1: # The LearningOnline Network with CAPA
                      2: # Handler for displaying the course catalog interface
                      3: #
1.81    ! raeburn     4: # $Id: coursecatalog.pm,v 1.80 2014/03/17 18:20:32 bisitz 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;
                     41: 
                     42: sub handler {
                     43:     my ($r) = @_;
                     44:     &Apache::loncommon::content_type($r,'text/html');
                     45:     $r->send_http_header;
                     46:     if ($r->header_only) {
                     47:         return OK;
                     48:     }
1.21      albertel   49:     my $handle = &Apache::lonnet::check_for_valid_session($r);
1.8       raeburn    50:     my $lonidsdir=$r->dir_config('lonIDsDir');
1.21      albertel   51:     if ($handle ne '') {
1.8       raeburn    52:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
                     53:     }
1.1       raeburn    54:     &Apache::lonacc::get_posted_cgi($r);
                     55:     &Apache::lonlocal::get_language_handle($r);
1.38      raeburn    56:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                     57:                                             ['sortby','showdom']);
1.28      raeburn    58: 
                     59:     my $codedom = &Apache::lonnet::default_login_domain();
1.19      raeburn    60: 
                     61:     if (($env{'user.domain'} ne '') && ($env{'user.domain'} ne 'public')) { 
                     62:         $codedom = $env{'user.domain'};
                     63:         if ($env{'request.role.domain'} ne '') {
                     64:             $codedom = $env{'request.role.domain'};
                     65:         }
                     66:     }
1.7       raeburn    67:     my $formname = 'coursecatalog';
1.28      raeburn    68:     if ($env{'form.showdom'} ne '') {
1.75      raeburn    69:         $env{'form.showdom'} = &LONCAPA::clean_domain($env{'form.showdom'});
1.28      raeburn    70:         if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
                     71:             $codedom = $env{'form.showdom'};
1.75      raeburn    72:         } else {
                     73:             $env{'form.showdom'} = '';
1.28      raeburn    74:         }
                     75:     }
1.20      albertel   76:     my $domdesc = &Apache::lonnet::domain($codedom,'description');
1.7       raeburn    77:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.35      raeburn    78: 
1.75      raeburn    79:     if ($env{'form.catalog_maxdepth'} ne '') {
                     80:         $env{'form.catalog_maxdepth'} =~ s{\D}{}g;
                     81:     }
                     82: 
1.35      raeburn    83:     my %domconfig =
                     84:         &Apache::lonnet::get_dom('configuration',['coursecategories'],$codedom);
1.36      raeburn    85:     my (@cats,@trails,%allitems,%idx,@jsarray,%subcathash,$cathash);
1.35      raeburn    86:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                     87:         $cathash = $domconfig{'coursecategories'}{'cats'};
                     88:     } else {
                     89:         $cathash = {};
                     90:     }
1.36      raeburn    91:     my $subcats;
                     92:     if ($env{'form.withsubcats'}) {
                     93:         $subcats = \%subcathash;
                     94:     }
1.35      raeburn    95:     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems,
1.36      raeburn    96:                                            \%idx,\@jsarray,$subcats);
1.63      raeburn    97:     my ($numtitles,@codetitles);
1.8       raeburn    98:     if ($env{'form.coursenum'} ne '' && &user_is_known()) {
1.63      raeburn    99:         &course_details($r,$codedom,$formname,$domdesc,\@trails,\%allitems,\@codetitles);
1.7       raeburn   100:     } else {
1.46      raeburn   101:         my ($catlinks,$has_subcats,$selitem) = &category_breadcrumbs($codedom,@cats);
1.72      raeburn   102:         my $wasacctext = &get_wasactive_text();
1.28      raeburn   103:         my $catjs = <<"ENDSCRIPT";
                    104: 
                    105: function setCatDepth(depth) {
                    106:     document.coursecats.catalog_maxdepth.value = depth;
1.36      raeburn   107:     if (depth == '') {
                    108:         document.coursecats.currcat_0.value = '';
                    109:     }
1.28      raeburn   110:     document.coursecats.submit();
                    111:     return;
                    112: }
                    113: 
1.33      raeburn   114: function changeSort(caller) {
                    115:     document.$formname.sortby.value = caller;
                    116:     document.$formname.submit();
                    117: }
1.40      raeburn   118: 
1.33      raeburn   119: function setCourseId(caller) {
                    120:     document.$formname.coursenum.value = caller;
                    121:     document.$formname.submit();
1.37      raeburn   122: }
                    123: 
                    124: ENDSCRIPT
1.72      raeburn   125:         $catjs .= &courselink_javascript();
                    126:         if (&user_is_dc($codedom)) {
                    127:             $catjs .= <<ENDTOGGJS
                    128: 
                    129: function toggleStatuses() {
                    130:     if (document.$formname.showdetails.checked) {
                    131:         document.getElementById('statuschoice').style.display='block';
                    132:         document.getElementById('statuscell').style.borderLeft='1px solid'; 
                    133:     } else {
                    134:         document.getElementById('statuschoice').style.display='none';
                    135:         document.getElementById('statuscell').style.borderLeft='0px';
                    136:     }
                    137:     return;
                    138: }
                    139: 
                    140: function toggleWasActive() {
                    141:     if (document.getElementById('counts_Previous')) {
                    142:         if (document.getElementById('counts_Previous').checked) {
                    143:             document.getElementById('choosewasactive').style.display='block';
                    144:             document.getElementById('choosewasacctext').innerHTML = '$wasacctext';
                    145:         } else {
                    146:             document.getElementById('choosewasactive').style.display='none';
                    147:             document.getElementById('choosewasacctext').innerHTML = '';
                    148:         }
                    149:     }
                    150:     return;
                    151: }
                    152: 
                    153: ENDTOGGJS
                    154:         }
1.28      raeburn   155:         if ($env{'form.currcat_0'} eq 'instcode::0') {
                    156:             $numtitles = &instcode_course_selector($r,$codedom,$formname,$domdesc,
1.63      raeburn   157:                                                    $catlinks,$catjs,\@codetitles);
1.28      raeburn   158:             if ($env{'form.state'} eq 'listing') {
1.63      raeburn   159:                 $r->print(&print_course_listing($codedom,$numtitles,undef,undef,undef,
                    160:                                                 \@codetitles));
1.28      raeburn   161:             }
                    162:         } else {
                    163:             my (%add_entries);
1.50      raeburn   164:             my ($currdepth,$deeper) = &get_depth_values();
1.46      raeburn   165:             if ($selitem) {
1.50      raeburn   166:                 my $alert = &mt('Choose a subcategory to display');
                    167:                 if (!$deeper) {
                    168:                     $alert = &mt('Choose a category to display');
                    169:                 }
1.46      raeburn   170:                 $catjs .= <<ENDJS;
                    171: function check_selected() {
                    172:     if (document.coursecats.$selitem.options[document.coursecats.$selitem.selectedIndex].value == "") {
1.50      raeburn   173:         alert('$alert');
1.46      raeburn   174:         return false;
                    175:     }
                    176: }
                    177: ENDJS
                    178:             }
1.28      raeburn   179:             $catjs = '<script type="text/javascript">'."\n".$catjs."\n".'</script>';
                    180:             &cat_header($r,$codedom,$catjs,\%add_entries,$catlinks);
                    181:             if ($env{'form.currcat_0'} ne '') {
1.33      raeburn   182:                 $r->print('<form name="'.$formname.
                    183:                           '" method="post" action="/adm/coursecatalog">'.
1.36      raeburn   184:                           &additional_filters($codedom,$has_subcats)."\n");
1.33      raeburn   185:                 $r->print('<input type="hidden" name="catalog_maxdepth" value="'.
                    186:                           $deeper.'" />'."\n");
                    187:                 for (my $i=0; $i<$deeper; $i++) {
                    188:                     $r->print('<input type="hidden" name="currcat_'.$i.'" value="'.$env{'form.currcat_'.$i}.'" />'."\n");
                    189:                 }
1.57      raeburn   190:                 my $display_button;
                    191:                 if ($env{'form.currcat_0'} eq 'communities::0') {
                    192:                     $display_button = &mt('Display communities');
                    193:                 } else {
                    194:                     $display_button = &mt('Display courses');
                    195:                 }
1.33      raeburn   196:                 $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
                    197:                           '<input type="hidden" name="sortby" value="" />'."\n".
                    198:                           '<input type="hidden" name="state" value="listing" />'."\n".
                    199:                           '<input type="hidden" name="showdom" value="'.
                    200:                           $env{'form.showdom'}.'" />'.
                    201:                           '<input type="submit" name="catalogfilter" value="'.
1.57      raeburn   202:                           $display_button.'" /></form><br /><br />');
1.33      raeburn   203:             }
                    204:             if ($env{'form.state'} eq 'listing') {
1.63      raeburn   205:                 $r->print(&print_course_listing($codedom,undef,\@trails,\%allitems,$subcats,\@codetitles));
1.28      raeburn   206:             }
1.18      raeburn   207:         }
1.7       raeburn   208:     }
1.32      raeburn   209:     $r->print('<br />'.&Apache::loncommon::end_page());
1.7       raeburn   210:     return OK;
                    211: }
                    212: 
                    213: sub course_details {
1.63      raeburn   214:     my ($r,$codedom,$formname,$domdesc,$trails,$allitems,$codetitles) = @_;
1.7       raeburn   215:     my $output;
                    216:     my %add_entries = (topmargin    => "0",
                    217:                        marginheight => "0",);
1.40      raeburn   218:     my $js = '<script type="text/javascript">'."\n".
1.41      raeburn   219:              &courselink_javascript().'</script>'."\n";
1.7       raeburn   220:     my $start_page =
1.56      bisitz    221:         &Apache::loncommon::start_page('Course/Community Catalog',$js,
1.72      raeburn   222:                                        {'add_entries' => \%add_entries, });
1.7       raeburn   223:     $r->print($start_page);
1.19      raeburn   224:     if ($env{'form.numtitles'} > 0) {
                    225:         &Apache::lonhtmlcommon::add_breadcrumb
                    226:                 ({href=>"/adm/coursecatalog",
1.56      bisitz    227:                   text=>"Course/Community Catalog"});
1.19      raeburn   228:     }
1.60      raeburn   229:     my $brtextone = 'Course listing';
                    230:     my $brtexttwo = 'Course details';
                    231:     if ($env{'form.currcat_0'} eq 'communities::0') {
                    232:         $brtextone = 'Community listing';
1.61      raeburn   233:         $brtexttwo = 'Community details';
1.60      raeburn   234:     }
1.7       raeburn   235:     &Apache::lonhtmlcommon::add_breadcrumb
1.19      raeburn   236:              ({href=>"javascript:document.$formname.submit()",
1.60      raeburn   237:               text=>$brtextone},
                    238:              {text=>$brtexttwo});
1.78      bisitz    239:     $r->print(
                    240:         &Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog').
                    241:         '<h2>'.
                    242:         (($env{'form.currcat_0'} eq 'communities::0') ?
                    243:             &mt('Detailed community information:') :
                    244:             &mt('Detailed course information:')).
                    245:         '</h2>'.
                    246:         &print_course_listing($codedom,undef,$trails,$allitems,undef,$codetitles).
                    247:         '<br />'.
                    248:         '<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
                    249:         &Apache::lonhtmlcommon::actionbox([
                    250:             '<a href = "javascript:document.coursecatalog.submit()">'.
                    251:             (($env{'form.currcat_0'} eq 'communities::0') ?
                    252:                 &mt('Back to community listing') : &mt('Back to course listing')).
                    253:             '</a>'
                    254:         ]).
                    255:         &Apache::lonhtmlcommon::echo_form_input(['coursenum','catalogfilter',
                    256:                                                  'showdetails','courseid']).
                    257:         '</form>');
1.40      raeburn   258:     return;
                    259: }
                    260: 
1.41      raeburn   261: sub courselink_javascript {
1.40      raeburn   262:     return <<"END";
                    263: 
                    264: function ToSyllabus(cdom,cnum) {
                    265:     if (cdom == '' || cdom == null) {
                    266:         return;
                    267:     }
                    268:     if (cnum == '' || cnum == null) {
                    269:         return;
                    270:     }
1.41      raeburn   271:     document.linklaunch.action = "/public/"+cdom+"/"+cnum+"/syllabus";
                    272:     document.linklaunch.submit();
                    273: }
                    274: 
                    275: function ToSelfenroll(courseid) {
                    276:     if (courseid == '') {
                    277:         return;
                    278:     }
                    279:     document.linklaunch.action = "/adm/selfenroll";
                    280:     document.linklaunch.courseid.value = courseid;
                    281:     document.linklaunch.submit();
1.40      raeburn   282: }
                    283: 
                    284: END
1.7       raeburn   285: }
                    286: 
1.28      raeburn   287: sub instcode_course_selector {
1.63      raeburn   288:     my ($r,$codedom,$formname,$domdesc,$catlinks,$catjs,$codetitles) = @_;
1.1       raeburn   289:     my %coursecodes = ();
                    290:     my %codes = ();
                    291:     my %cat_titles = ();
                    292:     my %cat_order = ();
1.6       raeburn   293:     my %cat_items;
1.1       raeburn   294:     my $caller = 'global';
                    295:     my $format_reply;
1.30      raeburn   296:     my %add_entries = (topmargin    => "0",
                    297:                        marginheight => "0",);
1.51      raeburn   298:     my ($jscript,$totcodes,$numtitles,$lasttitle) = 
                    299:         &Apache::courseclassifier::instcode_selectors_data($codedom,$formname,
1.63      raeburn   300:                            \%cat_items,$codetitles,\%cat_titles,\%cat_order);
1.80      bisitz    301:     my $js = '<script type="text/javascript">'."\n".
1.79      raeburn   302:              '// <![CDATA['."\n".
                    303:              "$jscript\n$catjs\n".
                    304:              '// ]]>'."\n".
                    305:              '</script>';
1.51      raeburn   306:     if ($totcodes) {
1.18      raeburn   307:         if (($env{'form.state'} eq 'listing') && ($numtitles > 0)) {
1.72      raeburn   308:             $add_entries{'onLoad'} = 'setElements();';
                    309:         }
                    310:         if (&user_is_dc($codedom)) {
                    311:             $add_entries{'onLoad'} .= ' toggleStatuses();toggleWasActive();'
1.7       raeburn   312:         }
1.28      raeburn   313:         &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles);
                    314:         my $cat_maxdepth = $env{'form.catalog_maxdepth'};
                    315:         $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
1.32      raeburn   316:                   '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'."\n".
                    317:                   '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'."\n".
                    318:                   '<input type="hidden" name="currcat_0" value="instcode::0" />'.
1.33      raeburn   319:                   &additional_filters($codedom));
1.1       raeburn   320:         if ($numtitles > 0) {
1.51      raeburn   321:             $r->print('<b>'.&mt('Choose which course(s) to list.').'</b><br />'.
                    322:                       &Apache::courseclassifier::build_instcode_selectors($numtitles,
1.63      raeburn   323:                        $lasttitle,\%cat_items,$codetitles,\%cat_titles,\%cat_order));
1.18      raeburn   324:         }
1.33      raeburn   325:         $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
                    326:                   '<input type="hidden" name="sortby" value="" />'."\n".
                    327:                   '<input type="hidden" name="state" value="listing" />'."\n".
                    328:                   '<input type="submit" name="catalogfilter" value="'.
                    329:                   &mt('Display courses').'" />'.
                    330:                   '<input type="hidden" name="numtitles" value="'.$numtitles.
1.37      raeburn   331:                   '" /></form><br /><br />');
1.5       raeburn   332:     } else {
1.81    ! raeburn   333:         $js = '<script type="text/javascript">'."\n".
        !           334:               '// <![CDATA['."\n".
        !           335:               "$catjs\n".
        !           336:               '// ]]>'."\n".
        !           337:               '</script>';
1.30      raeburn   338:         &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles);
                    339:         my $cat_maxdepth = $env{'form.catalog_maxdepth'};
                    340:         $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
                    341:                   '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'.
                    342:                   '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'.
                    343:                   '<input type="hidden" name="currcat_0" value="instcode::0" />');
                    344:         $r->print('<br />'.&mt('No official courses to display for [_1].',$domdesc).'</form>');
1.1       raeburn   345:     }
1.18      raeburn   346:     return $numtitles;
1.1       raeburn   347: }
                    348: 
1.28      raeburn   349: sub cat_header {
                    350:     my ($r,$codedom,$js,$add_entries,$catlinks,$numtitles) = @_;
                    351:     my $start_page =
1.72      raeburn   352:         &Apache::loncommon::start_page('Course/Community Catalog',$js,
1.59      droeschl  353:                                        { 'add_entries' => $add_entries, });
1.28      raeburn   354:     $r->print($start_page);
1.60      raeburn   355:     my $brtext = 'Course listing';
                    356:     if ($env{'form.currcat_0'} eq 'communities::0') {
                    357:         $brtext = 'Community listing';
                    358:     }
1.28      raeburn   359:     if ($env{'form.state'} eq 'listing') {
                    360:         if ($numtitles > 0) {
                    361:             &Apache::lonhtmlcommon::add_breadcrumb
                    362:                 ({href=>"/adm/coursecatalog",
1.56      bisitz    363:                   text=>"Course/Community Catalog"},
1.60      raeburn   364:                  {text=>$brtext});
1.28      raeburn   365:         } else {
                    366:             &Apache::lonhtmlcommon::add_breadcrumb
1.60      raeburn   367:             ({text=>$brtext});
1.28      raeburn   368:         }
                    369:     } else {
                    370:         &Apache::lonhtmlcommon::add_breadcrumb
                    371:         ({href=>"/adm/coursecatalog",
1.56      bisitz    372:           text=>"Course/Community Catalog"});
1.28      raeburn   373:     }
1.55      raeburn   374:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
1.62      bisitz    375:     my $onchange = 'this.form.submit()';
1.34      raeburn   376:     $r->print('<form name="coursecatdom" method="post" action="/adm/coursecatalog">'.
                    377:               '<table border="0"><tr><td><b>'.&mt('Domain:').'</b></td><td>'.
1.47      raeburn   378:               &Apache::loncommon::select_dom_form($codedom,'showdom','',1,$onchange));
                    379:     if (!$onchange) {
                    380: 	   $r->print('&nbsp;<input type="submit" name="godom" value="'.&mt('Change').'" />');
                    381:     }
                    382:     $r->print('</td></tr></table></form>'.
1.46      raeburn   383: 	      '<form name="coursecats" method="post" action="/adm/coursecatalog"'.
1.48      raeburn   384:               ' onsubmit="return check_selected();">'.
1.34      raeburn   385:               '<table border="0"><tr>'.$catlinks.'</tr></table></form>');
1.28      raeburn   386:     return;
                    387: }
                    388: 
                    389: sub category_breadcrumbs {
1.35      raeburn   390:     my ($dom,@cats) = @_;
1.44      raeburn   391:     my $crumbsymbol = ' &#x25b6; ';
1.33      raeburn   392:     my ($currdepth,$deeper) = &get_depth_values();
1.57      raeburn   393:     my $currcat_str = 
                    394:         '<input type="hidden" name="catalog_maxdepth" value="'.$deeper.'" />'.
                    395:         '<input type="hidden" name="showdom" value="'.$dom.'" />';
1.58      raeburn   396:     my $catlinks = '<td valign="top"><b>'.&mt('Catalog:').'</b></td><td><table><tr><td>';
1.36      raeburn   397:     my $has_subcats;
1.46      raeburn   398:     my $selitem;
1.58      raeburn   399:     if (ref($cats[0]) eq 'ARRAY') {
                    400:         if (@{$cats[0]} == 0) {
                    401:             $catlinks .= &mt('No categories defined in this domain'); 
                    402:         } elsif (@{$cats[0]} == 1) {
                    403:             if ($cats[0][0] eq 'instcode') {
                    404:                 $catlinks .= &mt('Official courses (with institutional codes)');
                    405:                 $env{'form.currcat_0'} = 'instcode::0';
                    406:             } elsif ($cats[0][0] eq 'communities') {
                    407:                 $catlinks .= &mt('Communities');
                    408:                 $env{'form.currcat_0'} = 'communities::0';
                    409:             } else {
                    410:                 my $name = $cats[0][0];
                    411:                 my $item = &escape($name).'::0';
                    412:                 $catlinks .= $name;
                    413:                 $env{'form.currcat_0'} = $item;
1.46      raeburn   414:             }
1.58      raeburn   415:             $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
1.28      raeburn   416:         } else {
1.58      raeburn   417:             $catlinks .= &main_category_selector(@cats);
                    418:             if (($env{'form.currcat_0'} ne '') && 
                    419:                 ($env{'form.currcat_0'} ne 'instcode::0')) {
                    420:                 $catlinks .= $crumbsymbol;
                    421:             }
                    422:         }
                    423:     } else {
                    424:         $catlinks .= &mt('Official courses (with institutional codes)');
                    425:                      $env{'form.currcat_0'} = 'instcode::0';
                    426:          $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
                    427:     }
                    428:     if ($deeper) {
                    429:         for (my $i=1; $i<=$deeper; $i++) {
                    430:             my $shallower = $i-1;
                    431:             next if ($shallower == 0);
1.28      raeburn   432:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
1.58      raeburn   433:             if ($cat ne '') {
                    434:                 $catlinks .= '<td valign="top">'.
                    435:                              '<select name="currcat_'.$shallower.'" onchange="'.
                    436:                              'setCatDepth('."'$shallower'".');this.form.submit();">';
                    437:                 if (ref($cats[$shallower]{$container}) eq 'ARRAY') {
                    438:                     $catlinks .= '<option value="">'.&mt('De-select').'</option>';
                    439:                     for (my $j=0; $j<@{$cats[$shallower]{$container}}; $j++) {
                    440:                         my $name = $cats[$shallower]{$container}[$j];
1.65      raeburn   441:                         my $item = &escape($name).':'.&escape($container).':'.$shallower;
1.58      raeburn   442:                         my $selected = '';
                    443:                         if ($item eq $env{'form.currcat_'.$shallower}) {
                    444:                             $selected = ' selected="selected"';
                    445:                         }
                    446:                         $catlinks .= 
                    447:                            '<option value="'.$item.'"'.$selected.'>'.$name.'</option>';
1.28      raeburn   448:                     }
                    449:                 }
1.58      raeburn   450:                 $catlinks .= '</select>';
1.28      raeburn   451:             }
1.58      raeburn   452:             unless ($i == $deeper) {
                    453:                 $catlinks .= $crumbsymbol;
                    454:             } 
1.28      raeburn   455:         }
1.29      raeburn   456:         my ($cat,$container,$depth);
                    457:         if ($env{'form.currcat_'.$currdepth} eq '') {
                    458:             my $shallower = $currdepth - 1;
                    459:             ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
                    460:         } else {
                    461:             ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$currdepth});
                    462:         }
1.58      raeburn   463:         my $deeperlevel = $depth +1;
                    464:         if (ref($cats[$deeperlevel]{$cat}) eq 'ARRAY') {
1.36      raeburn   465:             $has_subcats = 1;
1.46      raeburn   466:             my $buttontext = &mt('Show subcategories');
1.58      raeburn   467:             my $selitem = 'currcat_'.$deeperlevel;
                    468:             $catlinks .= '&nbsp;<select name="'.$selitem.'" onchange="this.form.submit()">';
                    469:             if (@{$cats[$deeperlevel]{$cat}}) {
1.46      raeburn   470:                 $catlinks .= '<option value="" selected="selected">'.
1.58      raeburn   471:                              &mt('Subcategory ...').'</option>';
1.46      raeburn   472:             }
1.58      raeburn   473:             for (my $k=0; $k<@{$cats[$deeperlevel]{$cat}}; $k++) {
                    474:                 my $name = $cats[$deeperlevel]{$cat}[$k];
                    475:                 my $item = &escape($name).':'.&escape($cat).':'.$deeperlevel;
1.28      raeburn   476:                 $catlinks .= '<option value="'.$item.'">'.$name.'</option>'."\n";
                    477:             }
1.58      raeburn   478:             $catlinks .= '</select>'."\n";
1.46      raeburn   479:         } elsif ($cat ne 'instcode') {
                    480:             $catlinks .= '&nbsp;'.&mt('(No subcategories)');
1.28      raeburn   481:         }
1.58      raeburn   482:     } else {
                    483:         $selitem = 'currcat_0';
1.28      raeburn   484:     }
                    485:     $catlinks .= $currcat_str.'</td></tr></table></td>';
1.46      raeburn   486:     return ($catlinks,$has_subcats,$selitem);
1.28      raeburn   487: }
                    488: 
1.58      raeburn   489: sub main_category_selector {
                    490:     my (@cats) = @_;
                    491:     my $maincatlinks = '<select name="currcat_0" onchange="setCatDepth('."'0'".');this.form.submit();">'."\n";
                    492:     if (ref($cats[0]) eq 'ARRAY') {
                    493:         if (@{$cats[0]} > 1) {
                    494:             my $selected = '';
                    495:             if ($env{'form.currcat_0'} eq '') {
                    496:                 $selected = ' selected="selected"';    
                    497:             }
                    498:             $maincatlinks .= 
                    499:                 '<option value=""'.$selected.'>'.&mt('Select').'</option>'."\n";
                    500:         }
                    501:         for (my $i=0; $i<@{$cats[0]}; $i++) {
                    502:             my $name = $cats[0][$i];
                    503:             my $item = &escape($name).'::0';
                    504:             my $selected;
                    505:             if ($env{'form.currcat_0'} eq $item) {
                    506:                 $selected = ' selected="selected"';
                    507:             }
                    508:             $maincatlinks .= '<option value="'.$item.'"'.$selected.'>';
                    509:             if ($name eq 'instcode') {
                    510:                 $maincatlinks .= &mt('Official courses (with institutional codes)');
                    511:             } elsif ($name eq 'communities') {
                    512:                 $maincatlinks .= &mt('Communities');
                    513:             } else {
                    514:                 $maincatlinks .= $name;
                    515:             }
                    516:             $maincatlinks .= '</option>'."\n";
                    517:         }
                    518:         $maincatlinks .= '</select>'."\n";
                    519:     }
                    520:     return $maincatlinks;
                    521: }
                    522: 
1.33      raeburn   523: sub get_depth_values {
                    524:     my $currdepth = 0;
                    525:     my $deeper = 0;
                    526:     if ($env{'form.catalog_maxdepth'} ne '') {
                    527:         $currdepth = $env{'form.catalog_maxdepth'};
                    528:         if ($env{'form.currcat_'.$currdepth} eq '') {
                    529:             $deeper = $currdepth;
                    530:         } else {
                    531:             $deeper = $currdepth + 1;
                    532:         }
                    533:     }
                    534:     return ($currdepth,$deeper);
                    535: }
                    536: 
                    537: sub additional_filters {
1.36      raeburn   538:     my ($codedom,$has_subcats) = @_;
1.72      raeburn   539:     my $is_dc = &user_is_dc($codedom);
                    540:     my $output = '<div class="LC_left_float">';
                    541:     if ($is_dc) {
                    542:         $output .= '<fieldset><legend>'.&mt('Options').'</legend>';
                    543:     }
                    544:     $output .= '<table><tr><td valign="top">';
                    545:     if (($env{'form.currcat_0'} ne 'instcode::0') &&
1.36      raeburn   546:         ($env{'form.currcat_0'} ne '') && ($has_subcats)) {
                    547:         my $include_subcat_status;
                    548:         if ($env{'form.withsubcats'}) {
                    549:             $include_subcat_status = 'checked="checked" ';
                    550:         }
                    551:         my $counter = $env{'form.catalog_maxdepth'};
                    552:         if ($counter > 0) {
                    553:             if ($env{'form.state'} eq 'listing') {
                    554:                 $counter --;
                    555:             } elsif ($env{'form.currcat_'.$counter} eq '') {
                    556:                 $counter --;
                    557:             }
                    558:         }
                    559:         my ($catname) = split(/:/,$env{'form.currcat_'.$counter});
                    560:         if ($catname ne '') {
1.72      raeburn   561:             $output .= '<span class="LC_nobreak"><label>'.
1.36      raeburn   562:                        '<input type="checkbox" name="withsubcats" value="1" '.
                    563:                        $include_subcat_status.'/>'.
1.38      raeburn   564:                        &mt('Include subcategories within "[_1]"',
1.72      raeburn   565:                            &unescape($catname)).'</label></span><br />';
1.36      raeburn   566:         }
                    567:     }
1.33      raeburn   568:     my $show_selfenroll_status;
                    569:     if ($env{'form.showselfenroll'}) {
                    570:         $show_selfenroll_status = 'checked="checked" ';
                    571:     }
1.57      raeburn   572:     my $selfenroll_text;
                    573:     if ($env{'form.currcat_0'} eq 'communities::0') {
1.69      raeburn   574:         $selfenroll_text = &mt('Only show communities which currently allow self-enrollment (or will allow it in the future)');
1.57      raeburn   575:     } else {
1.69      raeburn   576:         $selfenroll_text = &mt('Only show courses which currently allow self-enrollment (or will allow it in the future)');
1.57      raeburn   577:     }
1.72      raeburn   578:     $output .= '<span class="LC_nobreak">'.
1.36      raeburn   579:                '<label><input type="checkbox" name="showselfenroll" value="1" '.
1.57      raeburn   580:                $show_selfenroll_status.'/>'.$selfenroll_text.
1.72      raeburn   581:                '</label></span><br />';
                    582:     if ($is_dc) {
                    583:         my ($titlesref,$orderref) = &get_statustitles('filters');
1.33      raeburn   584:         my $showdetails_status;
                    585:         if ($env{'form.showdetails'}) {
                    586:             $showdetails_status = 'checked="checked" ';
                    587:         }
                    588:         my $showhidden_status;
                    589:         if ($env{'form.showhidden'}) {
1.72      raeburn   590:             $showhidden_status = 'checked="checked" ';
1.33      raeburn   591:         }
1.72      raeburn   592:         my @currstatuses = &Apache::loncommon::get_env_multiple('form.showcounts');
1.33      raeburn   593:         my $dc_title = &Apache::lonnet::plaintext('dc');
1.72      raeburn   594:         my ($details_text,$hidden_text,$statusdisplay,$cellborder);
                    595:         my $wasactivedisplay = 'none';
                    596:         if ($env{'form.showdetails'}) {
                    597:             $statusdisplay = 'block';
                    598:             $cellborder = 'border-left: 1px solid;';
                    599:             if (grep(/^Previous$/,@currstatuses)) {
                    600:                 $wasactivedisplay = 'block';
                    601:             }
                    602:         } else {
                    603:             $statusdisplay = 'none';
                    604:             $cellborder = 'border-left: 0px';
                    605:         }
1.57      raeburn   606:         if ($env{'form.currcat_0'} eq 'communities::0') {
                    607:             $details_text = &mt('Show full details for each community ([_1] only)',$dc_title);
                    608:             $hidden_text = &mt('Include communities set to be hidden from catalog ([_1] only)',$dc_title);
                    609:         } else {
                    610:             $details_text = &mt('Show full details for each course ([_1] only)',$dc_title);
                    611:             $hidden_text = &mt('Include courses set to be hidden from catalog ([_1] only)',$dc_title);
                    612:         }
1.72      raeburn   613:         $output .= '<span class="LC_nobreak">'.
1.33      raeburn   614:                    '<label><input type="checkbox" name="showhidden" value="1" '.
1.57      raeburn   615:                    $showhidden_status.'/>'.$hidden_text.
1.72      raeburn   616:                    '</label></span><br />'."\n".
                    617:                    '<span class="LC_nobreak">'.
                    618:                    '<label><input type="checkbox" name="showdetails" value="1" '.
                    619:                    $showdetails_status.'onclick="toggleStatuses();" />'.
                    620:                    $details_text.'</label></span></td>'."\n".
                    621:                    '<td id="statuscell" valign="top" style="'.$cellborder.'">'.
                    622:                    '<div id="statuschoice" style="display:'.$statusdisplay.'">';
                    623:         if (ref($orderref) eq 'ARRAY') {
                    624:             if (@{$orderref} > 0) {
                    625:                 foreach my $type (@{$orderref}) {
                    626:                     my $checked;
                    627:                     if (grep(/^\Q$type\E$/,@currstatuses)) {
                    628:                         $checked = ' checked="checked"';
                    629:                     }
                    630:                     my $title;
                    631:                     if (ref($titlesref) eq 'HASH') {
                    632:                         $title = $titlesref->{$type};
                    633:                     }
                    634:                     unless ($title) {
                    635:                         $title = &mt($type);
                    636:                     }
                    637:                     my $onclick;
                    638:                     if ($type eq 'Previous') {
                    639:                         $onclick = ' onclick="toggleWasActive();"'; 
                    640:                     }
                    641:                     $output .= '<span class="LC_nobreak">'.
                    642:                                '<label><input type="checkbox" id="counts_'.$type.'"'.
                    643:                                ' name="showcounts" value="'.$type.'"'.$checked.$onclick.
                    644:                                ' />'.$title.'</label></span>';
                    645:                     if ($type eq 'Previous') {
                    646:                         my %milestonetext = &Apache::lonlocal::texthash (
1.74      raeburn   647:                             accessend => 'immediately prior to default end access date',
                    648:                             enrollend => 'immediately prior to end date for auto-enrollment',
                    649:                             date      => 'immediately prior to specific date:',
1.72      raeburn   650:                         );
                    651:                         my @statuses = &Apache::loncommon::get_env_multiple('form.showcounts');
1.77      raeburn   652:                         $output .= '<span id="choosewasacctext" class="LC_nobreak">';
1.72      raeburn   653:                         if ($checked) {
                    654:                             $output .= &get_wasactive_text();
                    655:                         }
                    656:                         $output .= '</span>'.
                    657:                                    '<div id="choosewasactive" style="display:'.$wasactivedisplay.'">'.
                    658:                                    '<table>';
                    659:                         my @milestones = ('accessend');
                    660:                         if (&Apache::lonnet::auto_run(undef,$codedom)) {
                    661:                             push(@milestones,'enrollend');
                    662:                         }
                    663:                         push(@milestones,'date');
                    664:                         foreach my $item (@milestones) {
                    665:                             my $checked;
                    666:                             if ($env{'form.state'} eq 'listing') {
                    667:                                 if ($env{'form.wasactive'} eq $item) {
                    668:                                     $checked = ' checked="checked"';
                    669:                                 }
                    670:                             } elsif ($item eq 'accessend') {
                    671:                                 $checked = ' checked="checked"';
                    672:                             }
                    673:                             $output .=
                    674:                                 '<tr><td width="10">&nbsp;</td><td>'.
                    675:                                 '<span class="LC_nobreak"><label>'.
                    676:                                 '<input type="radio" value="'.$item.'" name="wasactive"'.$checked.' />'.
                    677:                                 $milestonetext{$item}.'</label></span>';
                    678:                             if ($item eq 'date') {
                    679:                                 my $wasactiveon;
                    680:                                 if (grep(/^Previous$/,@currstatuses)) {
                    681:                                     $wasactiveon =
                    682:                                         &Apache::lonhtmlcommon::get_date_from_form('wasactiveon');
                    683:                                 } else {
                    684:                                     $wasactiveon = 'now';
                    685:                                 }
                    686:                                 $output .= ' '.
                    687:                                     &Apache::lonhtmlcommon::date_setter('coursecatalog',
                    688:                                                                         'wasactiveon',
                    689:                                                                         $wasactiveon,
                    690:                                                                         '','','',1,'',
                    691:                                                                         '','',1);
                    692:                             }
                    693:                             $output .= '</td></tr>';
                    694:                         }
                    695:                         $output .= '</table></div>';
                    696:                     }
                    697:                     $output .= '<br />';
                    698:                 }
                    699:             }
                    700:         }
                    701:         $output .= '</div></td>';
                    702:     } else {
                    703:         $output .= '</td>';  
1.33      raeburn   704:     }
1.72      raeburn   705:     $output .= '</tr></table></fieldset></div>'.
                    706:                '<div style="clear:both;margin:0;"></div>';  
1.33      raeburn   707:     return $output;
                    708: }
                    709: 
1.16      raeburn   710: sub user_is_dc {
                    711:     my ($codedom) = @_;
                    712:     if (exists($env{'user.role.dc./'.$codedom.'/'})) {
                    713:         my $livedc = 1;
                    714:         my $now = time;
                    715:         my ($start,$end)=split(/\./,$env{'user.role.dc./'.$codedom.'/'});
                    716:         if ($start && $start>$now) { $livedc = 0; }
                    717:         if ($end   && $end  <$now) { $livedc = 0; }
                    718:         return $livedc;
                    719:     }
                    720:     return;
                    721: }
1.7       raeburn   722: 
1.72      raeburn   723: sub get_statustitles {
                    724:     my ($caller) = @_;
                    725:     my @status_order = ('Active','Future','Previous');
                    726:     my %status_title;
                    727:     if ($caller eq 'filters') {
                    728:         %status_title = &Apache::lonlocal::texthash(
                    729:                            Previous => 'Show count for past access',
                    730:                            Active => 'Show count for current student access',
                    731:                            Future => 'Show count for future student access',
                    732:                         );
                    733:         if ($env{'form.currcat_0'} eq 'communities::0') {
                    734:             $status_title{'Active'} = 'Show count for current member access';
                    735:             $status_title{'Future'} = 'Show count for future member access'; 
                    736:         }
                    737:     } else {    
                    738:         %status_title = &Apache::lonlocal::texthash(
                    739:                            Previous => 'Previous access',
                    740:                            Active => 'Current access',
                    741:                            Future => 'Future access',
                    742:                         );
                    743:     }
                    744:     return (\%status_title,\@status_order);
                    745: }
                    746: 
                    747: sub get_wasactive_text {
                    748:     my $wasacctext = ' -- ';
                    749:     if ($env{'form.currcat_0'} eq 'communities::0') {
1.74      raeburn   750:         $wasacctext .= &mt('where member access status was current ...');
1.72      raeburn   751:     } else {
1.74      raeburn   752:         $wasacctext .= &mt('where student access status was current ...');
1.72      raeburn   753:     }
                    754:     return $wasacctext;
                    755: }
                    756: 
1.28      raeburn   757: sub search_official_courselist {
1.63      raeburn   758:     my ($domain,$numtitles,$codetitles) = @_;
                    759:     my $instcode = &Apache::courseclassifier::instcode_search_str($domain,$numtitles,$codetitles);
1.32      raeburn   760:     my $showhidden;
                    761:     if (&user_is_dc($domain)) {
                    762:         $showhidden = $env{'form.showhidden'};
                    763:     }
                    764:     my %courses = 
                    765:         &Apache::lonnet::courseiddump($domain,'.',1,$instcode,'.','.',undef,undef,
                    766:                                       'Course',1,$env{'form.showselfenroll'},undef,
                    767:                                       $showhidden,'coursecatalog');
1.7       raeburn   768:     return %courses;
                    769: }
                    770: 
1.28      raeburn   771: sub search_courselist {
1.36      raeburn   772:     my ($domain,$subcats) = @_;
1.28      raeburn   773:     my $cat_maxdepth = $env{'form.catalog_maxdepth'};
                    774:     my $filter = $env{'form.currcat_'.$cat_maxdepth};
1.29      raeburn   775:     if (($filter eq '') && ($cat_maxdepth > 0)) {
                    776:         my $shallower = $cat_maxdepth - 1;
                    777:         $filter = $env{'form.currcat_'.$shallower};
                    778:     }
1.28      raeburn   779:     my %courses;
1.36      raeburn   780:     my $filterstr;
1.28      raeburn   781:     if ($filter ne '') {
1.36      raeburn   782:         if ($env{'form.withsubcats'}) {
                    783:             if (ref($subcats) eq 'HASH') {
                    784:                 if (ref($subcats->{$filter}) eq 'ARRAY') {
                    785:                     $filterstr = join('&',@{$subcats->{$filter}});
                    786:                     if ($filterstr ne '') {
                    787:                         $filterstr = $filter.'&'.$filterstr;
                    788:                     }
                    789:                 } else {
                    790:                     $filterstr = $filter;
                    791:                 }
                    792:             } else {
                    793:                 $filterstr = $filter;
                    794:             }  
                    795:         } else {
                    796:             $filterstr = $filter; 
                    797:         }
1.57      raeburn   798:         my ($showhidden,$typefilter);
1.32      raeburn   799:         if (&user_is_dc($domain)) {
                    800:             $showhidden = $env{'form.showhidden'};
                    801:         }
1.57      raeburn   802:         if ($env{'form.currcat_0'} eq 'communities::0') {
                    803:             $typefilter = 'Community';
                    804:         } else {
                    805:             $typefilter = '.';
                    806:         }
1.32      raeburn   807:         %courses = 
                    808:             &Apache::lonnet::courseiddump($domain,'.',1,'.','.','.',undef,undef,
1.57      raeburn   809:                                           $typefilter,1,$env{'form.showselfenroll'},
1.36      raeburn   810:                                           $filterstr,$showhidden,'coursecatalog');
1.28      raeburn   811:     }
                    812:     return %courses;
                    813: }
1.6       raeburn   814: 
1.1       raeburn   815: sub print_course_listing {
1.63      raeburn   816:     my ($domain,$numtitles,$trails,$allitems,$subcats,$codetitles) = @_;
1.1       raeburn   817:     my $output;
1.7       raeburn   818:     my %courses;
1.15      raeburn   819:     my $knownuser = &user_is_known();
1.16      raeburn   820:     my $details = $env{'form.coursenum'};
                    821:     if (&user_is_dc($domain)) {
                    822:         if ($env{'form.showdetails'}) {
                    823:             $details = 1;
                    824:         }
                    825:     }
1.7       raeburn   826:     if ($env{'form.coursenum'} ne '') {
                    827:         %courses = &Apache::lonnet::courseiddump($domain,'.',1,'.','.',
                    828:                                                  $env{'form.coursenum'},
1.33      raeburn   829:                                                  undef,undef,'.',1);
1.7       raeburn   830:         if (keys(%courses) == 0) {
1.78      bisitz    831:             $output = '<p class="LC_error">';
1.60      raeburn   832:             if ($env{'form.currcat_0'} eq 'communities::0') {
                    833:                 $output .= &mt('The courseID provided does not match a community in this domain.');
                    834:             } else { 
                    835:                 $output .= &mt('The courseID provided does not match a course in this domain.');
                    836:             }
1.78      bisitz    837:             $output .= '</p>';
1.7       raeburn   838:             return $output;
                    839:         }
1.6       raeburn   840:     } else {
1.28      raeburn   841:         if ($env{'form.currcat_0'} eq 'instcode::0') {
1.63      raeburn   842:             %courses = &search_official_courselist($domain,$numtitles,$codetitles);
1.28      raeburn   843:         } else {
1.36      raeburn   844:             %courses = &search_courselist($domain,$subcats);
1.28      raeburn   845:         }
1.7       raeburn   846:         if (keys(%courses) == 0) {
1.78      bisitz    847:             $output = '<p class="LC_info">';
1.57      raeburn   848:             if ($env{'form.currcat_0'} eq 'communities::0') {
1.78      bisitz    849:                 $output .= &mt('No communities match the criteria you selected.');
1.57      raeburn   850:             } else {
1.78      bisitz    851:                 $output .= &mt('No courses match the criteria you selected.');
1.57      raeburn   852:             }
1.78      bisitz    853:             $output .= '</p>';
1.7       raeburn   854:             return $output;
                    855:         }
1.32      raeburn   856:         if (($knownuser) && (!$env{'form.showdetails'}) && (!&user_is_dc($domain))) {
1.31      bisitz    857:             $output = '<b>'.&mt('Note for students:').'</b> '
                    858:                      .&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.')
                    859:                      .'<br /><br />';
1.8       raeburn   860:         }
1.7       raeburn   861:     }
1.27      raeburn   862:     my $now = time;
                    863:     my %domconfig =
                    864:         &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
1.72      raeburn   865:     $output .= &construct_data_table($knownuser,$domain,\%courses,$details,undef,
                    866:                                      $now,\%domconfig,$trails,$allitems);
1.41      raeburn   867:     $output .= "\n".'<form name="linklaunch" method="post" action="">'.
1.40      raeburn   868:                '<input type="hidden" name="backto" value="coursecatalog" />'.
1.41      raeburn   869:                '<input type="hidden" name="courseid" value="" />'.
                    870:                &Apache::lonhtmlcommon::echo_form_input(['catalogfilter','courseid']).'</form>';
1.15      raeburn   871:     return $output;
                    872: }
                    873: 
                    874: sub construct_data_table {
1.72      raeburn   875:     my ($knownuser,$domain,$courses,$details,$usersections,$now,$domconfig,
                    876:         $trails,$allitems) = @_;
1.7       raeburn   877:     my %sortname;
1.16      raeburn   878:     if (($details eq '') || ($env{'form.showdetails'})) {
1.7       raeburn   879:         $sortname{'Code'} = 'code';
1.35      raeburn   880:         $sortname{'Categories'} = 'cats';
1.7       raeburn   881:         $sortname{'Title'} = 'title';
1.70      raeburn   882:         $sortname{'Owner & Co-owner(s)'} = 'owner';
1.7       raeburn   883:     }
1.15      raeburn   884:     my $output = &Apache::loncommon::start_data_table().
                    885:                  &Apache::loncommon::start_data_table_header_row();
1.35      raeburn   886:     my @coltitles = ('Count');
                    887:     if ($env{'form.currcat_0'} eq 'instcode::0') {
                    888:         push(@coltitles,'Code');
                    889:     } else {
                    890:         push(@coltitles,'Categories');
                    891:     }
1.70      raeburn   892:     push(@coltitles,('Sections','Crosslisted','Title','Owner & Co-owner(s)'));
1.15      raeburn   893:     if (ref($usersections) eq 'HASH') {
                    894:        $coltitles[1] = 'Your Section';
                    895:     }
1.7       raeburn   896:     foreach my $item (@coltitles) {
                    897:         $output .= '<th>';
                    898:         if (defined($sortname{$item})) {
                    899:             $output .= '<a href="javascript:changeSort('."'$sortname{$item}'".')">'.&mt($item).'</a>';
1.24      raeburn   900:         } elsif ($item eq 'Count') {
                    901:             $output .= '&nbsp;&nbsp;';
1.7       raeburn   902:         } else {
                    903:             $output .= &mt($item);
                    904:         }
                    905:         $output .= '</th>';
1.1       raeburn   906:     }
1.72      raeburn   907:     my (@fields,%fieldtitles,$wasactiveon);
1.15      raeburn   908:     if ($knownuser) {
                    909:         if ($details) {
1.60      raeburn   910:             if ($env{'form.currcat_0'} eq 'communities::0') {
1.72      raeburn   911:                 $output .= '<th>'.&mt('Default Access Dates for Members').'</th>'.
                    912:                            '<th>'.&mt('Member Counts').'</th>';
1.60      raeburn   913:             } else {
                    914:                 $output .=
                    915:                     '<th>'.&mt('Default Access Dates for Students').'</th>'.
                    916:                     '<th>'.&mt('Student Counts').'</th>'.
                    917:                     '<th>'.&mt('Auto-enrollment of[_1]registered students','<br />').'</th>';
                    918:             }
1.72      raeburn   919:             my ($titlesref,$orderref) = &get_statustitles();
                    920:             my @statuses;
                    921:             if (&user_is_dc($domain)) {
                    922:                 @statuses = &Apache::loncommon::get_env_multiple('form.showcounts');
                    923:                 if (grep(/^Previous$/,@statuses)) {
                    924:                     if ($env{'form.wasactive'} eq 'date') { 
                    925:                         $wasactiveon = 
                    926:                             &Apache::lonhtmlcommon::get_date_from_form('wasactiveon');
                    927:                     } else {
                    928:                         $wasactiveon = $env{'form.wasactive'};
                    929:                     }
                    930:                 }
                    931:                 if (ref($orderref) eq 'ARRAY') {
                    932:                     foreach my $status (@{$orderref}) {
                    933:                         if (grep(/^\Q$status\E$/,@statuses)) {
                    934:                             push(@fields,$status); 
                    935:                         }
                    936:                     }
                    937:                 }
                    938:             } else {
                    939:                 @fields = ('Active','Future');
                    940:             }
                    941:             foreach my $status (@fields) {
                    942:                 my $title;
                    943:                 if (ref($titlesref) eq 'HASH') {
                    944:                     $title = $titlesref->{$status};
                    945:                 }
                    946:                 unless ($title) {
                    947:                     $title = &mt($status);
                    948:                 }
                    949:                 $fieldtitles{$status} = $title;
                    950:             }
1.15      raeburn   951:         } else {
1.27      raeburn   952:             $output .= '<th>'.&mt('Details').'</th>';
1.8       raeburn   953:         }
1.1       raeburn   954:     }
1.27      raeburn   955:     $output .= '<th>'.&mt('Self-enroll (if permitted)').'</th>';
1.7       raeburn   956:     &Apache::loncommon::end_data_table_header_row();
1.73      raeburn   957:     my (%numbers,%creditsum);
1.76      raeburn   958:     my ($showcredits,$defofficial,$defunofficial,$deftextbook);
1.73      raeburn   959:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                    960:     unless ($env{'form.currcat_0'} eq 'communities::0') {
1.76      raeburn   961:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'} || $domdefaults{'textbookcredits'}) {
1.73      raeburn   962:             $showcredits = 1;
                    963:             $defofficial = $domdefaults{'officialcredits'};
1.76      raeburn   964:             $defunofficial = $domdefaults{'unofficialcredits'};
                    965:             $deftextbook = $domdefaults{'textbookcredits'};
1.73      raeburn   966:         }
                    967:     }
1.72      raeburn   968:     my %courseinfo = &build_courseinfo_hash($courses,$knownuser,$domain,$details,
                    969:                                             $usersections,\@fields,\%fieldtitles,
1.73      raeburn   970:                                             $wasactiveon,\%numbers,\%creditsum,
1.76      raeburn   971:                                             $showcredits,$defofficial,$defunofficial,$deftextbook);
1.7       raeburn   972:     my %Sortby;
1.15      raeburn   973:     foreach my $course (sort(keys(%{$courses}))) {
1.7       raeburn   974:         if ($env{'form.sortby'} eq 'code') {
                    975:             push(@{$Sortby{$courseinfo{$course}{'code'}}},$course);
1.35      raeburn   976:         } elsif ($env{'form.sortby'} eq 'cats') {
                    977:             push(@{$Sortby{$courseinfo{$course}{'categories'}}},$course);
1.7       raeburn   978:         } elsif ($env{'form.sortby'} eq 'owner') {
1.22      raeburn   979:             push(@{$Sortby{$courseinfo{$course}{'ownerlastnames'}}},$course);
1.7       raeburn   980:         } else {
1.25      raeburn   981:             my $clean_title = $courseinfo{$course}{'title'};
                    982:             $clean_title =~ s/\W+//g;
                    983:             if ($clean_title eq '') {
                    984:                 $clean_title = $courseinfo{$course}{'title'};
                    985:             }
                    986:             push(@{$Sortby{$clean_title}},$course);
1.7       raeburn   987:         }
                    988:     }
                    989:     my @sorted_courses;
1.35      raeburn   990:     if (($env{'form.sortby'} eq 'code') || ($env{'form.sortby'} eq 'owner') ||
                    991:         ($env{'form.sortby'} eq 'cats')) {
1.7       raeburn   992:         @sorted_courses = sort(keys(%Sortby));
1.6       raeburn   993:     } else {
1.7       raeburn   994:         @sorted_courses = sort { lc($a) cmp lc($b) } (keys(%Sortby));
1.1       raeburn   995:     }
1.24      raeburn   996:     my $count = 1;
1.72      raeburn   997:     my $totalsec = 0;
1.7       raeburn   998:     foreach my $item (@sorted_courses) {
                    999:         foreach my $course (@{$Sortby{$item}}) {
                   1000:             $output.=&Apache::loncommon::start_data_table_row(); 
1.35      raeburn  1001:             $output.=&courseinfo_row($courseinfo{$course},$knownuser,$details,
1.72      raeburn  1002:                                      \$count,$now,$course,$trails,$allitems,\%numbers);
1.7       raeburn  1003:             $output.=&Apache::loncommon::end_data_table_row();
                   1004:         }
1.1       raeburn  1005:     }
1.72      raeburn  1006:     if (($knownuser) && ($count > 1) && $env{'form.showdetails'}) {
                   1007:         if (&user_is_dc($domain)) {
                   1008:             my %lt = &Apache::lonlocal::texthash (
                   1009:                                                      'Active'   => 'Total current students',
                   1010:                                                      'Future'   => 'Total future students',
                   1011:                                                      'Previous' => 'Total previous students',
                   1012:                                                      'courses'  => 'Total unique codes and courses without codes',
                   1013:                                                      'sections' => 'Total sections',
1.73      raeburn  1014:                                                      'xlists'   => 'Total cross-listings',
1.72      raeburn  1015:                                                  );
1.73      raeburn  1016:             if ($showcredits) {
                   1017:                 $lt{'cr_Active'} = &mt('Total current student credit hours');
                   1018:                 $lt{'cr_Future'} = &mt('Total future student credit hours');
                   1019:                 $lt{'cr_Previous'} = &mt('Total previous student credit hours');
                   1020:             }
1.72      raeburn  1021:             if ($env{'form.currcat_0'} eq 'communities::0') {
                   1022:                 $lt{'courses'} = &mt('Total communities');
                   1023:                 $lt{'Active'} = &mt('Total current members'); 
                   1024:                 $lt{'Future'} = &mt('Total future members');
                   1025:                 $lt{'Previous'} = &mt('Total previous members');
1.73      raeburn  1026:             }
                   1027:             my $colspan = 8;
                   1028:             if ($showcredits) {
                   1029:                 $colspan = 4;
                   1030:             }
1.72      raeburn  1031:             $output .= '<tr class="LC_footer_row">'.
                   1032:                        '<td colspan="2">&nbsp;</td>'.
1.73      raeburn  1033:                        '<td colspan="'.$colspan.'">'.
1.72      raeburn  1034:                        '<table border="0">';
                   1035:             foreach my $item ('courses','sections','xlists') {
                   1036:                 $output .= '<tr>'.
1.73      raeburn  1037:                            '<td>'.$lt{$item}.'</td><td>&nbsp;</td>'.
1.72      raeburn  1038:                            '<td align="right">'.$numbers{$item}.'</td>'.
                   1039:                            '</tr>'."\n";
                   1040:             }
                   1041:             if (@fields > 0) { 
                   1042:                 foreach my $status (@fields) {
                   1043:                     $output .= '<tr>'.
1.73      raeburn  1044:                                '<td>'.$lt{$status}.'</td><td>&nbsp;</td>'.
1.72      raeburn  1045:                                '<td align="right">'.$numbers{$status}.'</td>'.
                   1046:                                '</tr>'."\n";
                   1047:                 }
                   1048:             }
1.73      raeburn  1049:             $output .= '</table></td>';
                   1050:             if ($showcredits) {
                   1051:                 $output .= '<td colspan="'.$colspan.'" valign="bottom"><table>';
                   1052:                 foreach my $status (@fields) {
                   1053:                     $output .= '<tr>'.
                   1054:                                '<td>'.$lt{'cr_'.$status}.'</td><td>&nbsp;</td>'.
                   1055:                                '<td align="right">'.$creditsum{$status}.'</td></tr>';
                   1056:                 }
                   1057:                 $output .= '</table></td></tr>';
                   1058:             }
1.72      raeburn  1059:         }
                   1060:     }
1.7       raeburn  1061:     $output .= &Apache::loncommon::end_data_table();
                   1062:     return $output;
                   1063: }
                   1064: 
                   1065: sub build_courseinfo_hash {
1.72      raeburn  1066:     my ($courses,$knownuser,$domain,$details,$usersections,$fields,$fieldtitles,
1.73      raeburn  1067:         $wasactiveon,$numbers,$creditsum,$showcredits,$defofficial,$defunofficial) = @_;
1.1       raeburn  1068:     my %courseinfo;
1.7       raeburn  1069:     my $now = time;
1.72      raeburn  1070:     my $gettotals;
                   1071:     if ((keys(%{$courses}) > 0) && (&user_is_dc($domain)) && ($details)) {
                   1072:         $gettotals = 1;
                   1073:     }
1.73      raeburn  1074:     my (%uniquecodes,$nocodes,$defcreds);
1.15      raeburn  1075:     foreach my $course (keys(%{$courses})) {
1.1       raeburn  1076:         my $descr;
1.22      raeburn  1077:         if (ref($courses->{$course}) eq 'HASH') {
                   1078:             $descr = $courses->{$course}{'description'}; 
1.1       raeburn  1079:         }
                   1080:         my $cleandesc=&HTML::Entities::encode($descr,'<>&"');
                   1081:         $cleandesc=~s/'/\\'/g;
1.10      raeburn  1082:         $cleandesc =~ s/^\s+//;
1.1       raeburn  1083:         my ($cdom,$cnum)=split(/\_/,$course);
1.39      raeburn  1084:         my ($instcode,$singleowner,$ttype,$selfenroll_types,
1.35      raeburn  1085:             $selfenroll_start,$selfenroll_end,@owners,%ownernames,$categories);
1.22      raeburn  1086:         if (ref($courses->{$course}) eq 'HASH') {
                   1087:             $descr = $courses->{$course}{'description'};
1.23      raeburn  1088:             $instcode =  $courses->{$course}{'inst_code'};
1.22      raeburn  1089:             $singleowner = $courses->{$course}{'owner'};
                   1090:             $ttype =  $courses->{$course}{'type'};
1.27      raeburn  1091:             $selfenroll_types = $courses->{$course}{'selfenroll_types'};
                   1092:             $selfenroll_start = $courses->{$course}{'selfenroll_start_date'};
                   1093:             $selfenroll_end = $courses->{$course}{'selfenroll_end_date'};
1.35      raeburn  1094:             $categories = $courses->{$course}{'categories'};
1.22      raeburn  1095:             push(@owners,$singleowner);
1.67      raeburn  1096:             if ($courses->{$course}{'co-owners'} ne '') {
                   1097:                 foreach my $item (split(/,/,$courses->{$course}{'co-owners'})) {
1.22      raeburn  1098:                     push(@owners,$item);
                   1099:                 }
                   1100:             }
                   1101:         }
1.72      raeburn  1102:         if ($instcode ne '') {
                   1103:             $uniquecodes{$instcode} = 1;
                   1104:         } else {
                   1105:             $nocodes ++;
                   1106:         } 
1.22      raeburn  1107:         foreach my $owner (@owners) {
1.54      raeburn  1108:             my ($ownername,$ownerdom); 
1.22      raeburn  1109:             if ($owner =~ /:/) {
                   1110:                 ($ownername,$ownerdom) = split(/:/,$owner);
                   1111:             } else {
                   1112:                 $ownername = $owner;
                   1113:                 if ($owner ne '') {
                   1114:                     $ownerdom = $cdom;
                   1115:                 }
                   1116:             }
                   1117:             if ($ownername ne '' && $ownerdom ne '') {
                   1118:                 my %namehash=&Apache::loncommon::getnames($ownername,$ownerdom);
                   1119:                 $ownernames{$ownername.':'.$ownerdom} = \%namehash; 
1.1       raeburn  1120:             }
                   1121:         }
                   1122:         $courseinfo{$course}{'cdom'} = $cdom;
                   1123:         $courseinfo{$course}{'cnum'} = $cnum;
                   1124:         $courseinfo{$course}{'code'} = $instcode;
1.22      raeburn  1125:         my @lastnames;
                   1126:         foreach my $owner (keys(%ownernames)) {
                   1127:             if (ref($ownernames{$owner}) eq 'HASH') {
                   1128:                 push(@lastnames,$ownernames{$owner}{'lastname'});
                   1129:             }
                   1130:         }
                   1131:         $courseinfo{$course}{'ownerlastnames'} = join(', ',sort(@lastnames));
1.1       raeburn  1132:         $courseinfo{$course}{'title'} = $cleandesc;
1.22      raeburn  1133:         $courseinfo{$course}{'owner'} = $singleowner;
1.27      raeburn  1134:         $courseinfo{$course}{'selfenroll_types'} = $selfenroll_types;
                   1135:         $courseinfo{$course}{'selfenroll_start'} = $selfenroll_start;
                   1136:         $courseinfo{$course}{'selfenroll_end'} = $selfenroll_end;
1.35      raeburn  1137:         $courseinfo{$course}{'categories'} = $categories;
1.7       raeburn  1138: 
                   1139:         my %coursehash = &Apache::lonnet::dump('environment',$cdom,$cnum);
                   1140:         my @classids;
                   1141:         my @crosslistings;
1.15      raeburn  1142:         my ($seclist,$numsec) = 
                   1143:             &identify_sections($coursehash{'internal.sectionnums'});
1.7       raeburn  1144:         $courseinfo{$course}{'seclist'} = $seclist;
1.15      raeburn  1145:         my ($xlist_items,$numxlist) = 
                   1146:             &identify_sections($coursehash{'internal.crosslistings'});
1.72      raeburn  1147:         if (ref($numbers) eq 'HASH') {
                   1148:             $numbers->{'sections'} += $numsec; 
                   1149:             $numbers->{'xlists'} += $numxlist;  
                   1150:         }
1.7       raeburn  1151:         my $showsyllabus = 1; # default is to include a syllabus link
                   1152:         if (defined($coursehash{'showsyllabus'})) {
                   1153:             $showsyllabus = $coursehash{'showsyllabus'};
                   1154:         }
                   1155:         $courseinfo{$course}{'showsyllabus'} = $showsyllabus;
1.73      raeburn  1156:         if ($showcredits) {
                   1157:             if ($coursehash{'internal.defaultcredits'}) {
                   1158:                 $courseinfo{$course}{'defaultcredits'} = $coursehash{'internal.defaultcredits'};
                   1159:             } elsif ($instcode ne '') {
                   1160:                 $courseinfo{$course}{'defaultcredits'} = $defofficial;
                   1161:             } else {
                   1162:                 $courseinfo{$course}{'defaultcredits'} = $defunofficial;
                   1163:             }
                   1164:             $defcreds = $courseinfo{$course}{'defaultcredits'};
                   1165:         }
1.15      raeburn  1166:         if (((defined($env{'form.coursenum'}) && ($cnum eq $env{'form.coursenum'}))) ||
1.22      raeburn  1167:             ($knownuser && ($details == 1))) {
1.72      raeburn  1168:             my $milestone;
                   1169:             if ($wasactiveon eq 'accessend') {
                   1170:                 if ($coursehash{'default_enrollment_end_date'}) {
                   1171:                     $milestone = $coursehash{'default_enrollment_end_date'};
                   1172:                 } else {
                   1173:                     $milestone = time;
                   1174:                 }
                   1175:             } elsif ($wasactiveon eq 'enrollend') {
                   1176:                 if ($coursehash{'internal.autoend'}) {
                   1177:                     $milestone = $coursehash{'internal.autoend'};
                   1178:                 } else {
                   1179:                     $milestone = time;
                   1180:                 }
                   1181:             } else {
                   1182:                 $milestone = $wasactiveon;
                   1183:             }
                   1184:             $courseinfo{$course}{'counts'} =  
                   1185:                 &count_students($cdom,$cnum,$numsec,$fields,$fieldtitles,$gettotals,
1.73      raeburn  1186:                                 $numbers,$creditsum,$showcredits,$defcreds,$milestone);
1.72      raeburn  1187:             if ($instcode ne '') {
                   1188:                 $courseinfo{$course}{'autoenrollment'} =
                   1189:                     &autoenroll_info(\%coursehash,$now,$seclist,$xlist_items,
                   1190:                                      $instcode,\@owners,$cdom,$cnum);
                   1191:             }
1.15      raeburn  1192:             my $startaccess = '';
                   1193:             my $endaccess = '';
                   1194:             my $accessdates;
                   1195:             if ( defined($coursehash{'default_enrollment_start_date'}) ) {
                   1196:                 $startaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_start_date'});
                   1197:             }
                   1198:             if ( defined($coursehash{'default_enrollment_end_date'}) ) {
                   1199:                 $endaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_end_date'});
                   1200:                 if ($coursehash{'default_enrollment_end_date'} == 0) {
1.34      raeburn  1201:                     $endaccess = &mt('No ending date');
1.15      raeburn  1202:                 }
                   1203:             }
                   1204:             if ($startaccess) {
1.42      bisitz   1205:                 $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$startaccess).'<br />';
1.7       raeburn  1206:             }
1.15      raeburn  1207:             if ($endaccess) {
1.42      bisitz   1208:                 $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$endaccess).'<br />';
1.15      raeburn  1209:             }
1.34      raeburn  1210:             if (($selfenroll_types ne '') && 
                   1211:                 ($selfenroll_end > 0 && $selfenroll_end > $now)) {
                   1212:                 my ($selfenroll_start_access,$selfenroll_end_access);
                   1213:                 if (($coursehash{'default_enrollment_start_date'} ne 
                   1214:                      $coursehash{'internal.selfenroll_start_access'}) ||
                   1215:                    ($coursehash{'default_enrollment_end_date'} ne 
                   1216:                     $coursehash{'internal.selfenroll_end_access'})) {
                   1217:                     if ( defined($coursehash{'internal.selfenroll_start_access'}) ) {
                   1218:                         $selfenroll_start_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_start_access'});
                   1219:                     }
                   1220:                     if ( defined($coursehash{'default_enrollment_end_date'}) ) {
                   1221:                         $selfenroll_end_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_end_access'});
                   1222:                         if ($coursehash{'internal.selfenroll_end_access'} == 0) {
                   1223:                             $selfenroll_end_access = &mt('No ending date');
                   1224:                         }
                   1225:                     }
                   1226:                     if ($selfenroll_start_access || $selfenroll_end_access) {
                   1227:                         $accessdates .= '<br/><br /><i>'.&mt('Self-enrollers:').'</i><br />';
                   1228:                         if ($selfenroll_start_access) {
1.42      bisitz   1229:                             $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$selfenroll_start_access).'<br />';
1.34      raeburn  1230:                         }
                   1231:                         if ($selfenroll_end_access) {
1.42      bisitz   1232:                             $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$selfenroll_end_access).'<br />';
1.34      raeburn  1233:                         }
                   1234:                     }
                   1235:                 }
                   1236:             }
1.15      raeburn  1237:             $courseinfo{$course}{'access'} = $accessdates;
1.1       raeburn  1238:         }
1.7       raeburn  1239:         if ($xlist_items eq '') {
                   1240:             $xlist_items = &mt('No');
1.1       raeburn  1241:         }
1.7       raeburn  1242:         $courseinfo{$course}{'xlist'} = $xlist_items;
1.1       raeburn  1243:     }
1.72      raeburn  1244:     if (ref($numbers) eq 'HASH') {
                   1245:         $numbers->{'courses'} = $nocodes + scalar(keys(%uniquecodes));
                   1246:     }
1.7       raeburn  1247:     return %courseinfo;
1.1       raeburn  1248: }
                   1249: 
1.7       raeburn  1250: sub count_students {
1.73      raeburn  1251:     my ($cdom,$cnum,$numsec,$fieldsref,$titlesref,$getcounts,$numbers,$creditsum,
                   1252:         $showcredits,$defcreds,$wasactiveon) = @_;
1.72      raeburn  1253:     my $countslist = '<span class="LC_nobreak">'.
                   1254:                      &mt('[quant,_1,section,sections,No sections]',$numsec).'</span>';
                   1255:     my (@fields,%titles,$showexpired);
                   1256:     if ((ref($fieldsref) eq 'ARRAY') && (ref($titlesref) eq 'HASH') &&
                   1257:         (ref($numbers) eq 'HASH')) {
                   1258:         @fields = @{$fieldsref};
                   1259:         %titles = %{$titlesref};
                   1260:         if (grep(/^Previous$/,@fields)) {
                   1261:             $showexpired = 1;
                   1262:         }
                   1263:     } else {
                   1264:         return;
                   1265:     }
1.1       raeburn  1266:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1.73      raeburn  1267:     my (%student_count,%credit_count);
                   1268:     %student_count = (
1.72      raeburn  1269:                            Active   => 0,
                   1270:                            Future   => 0,
                   1271:                            Previous => 0,
1.73      raeburn  1272:                      );
                   1273:     if ($showcredits) {
                   1274:         %credit_count = (
                   1275:                           Active   => 0,
                   1276:                           Future   => 0,
                   1277:                           Previous => 0,
                   1278:                         );
                   1279:     }
1.1       raeburn  1280:     my %idx;
                   1281:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
1.72      raeburn  1282:     $idx{'end'}    = &Apache::loncoursedata::CL_END();
1.73      raeburn  1283:     $idx{'credits'} = &Apache::loncoursedata::CL_CREDITS();
1.4       albertel 1284:     while (my ($student,$data) = each(%$classlist)) {
1.72      raeburn  1285:         my $status = $data->[$idx{'status'}];
1.73      raeburn  1286:         my $credits = $data->[$idx{'credits'}];
                   1287:         if ($credits eq '') {
                   1288:             $credits = $defcreds;  
                   1289:         }
1.72      raeburn  1290:         if ($status eq 'Expired') {
                   1291:             if (($showexpired) &&
                   1292:                 ($data->[$idx{'end'}] >= $wasactiveon)) {
                   1293:                 $student_count{'Previous'} ++;
1.73      raeburn  1294:                 if ($showcredits) {
                   1295:                     $credit_count{'Previous'} += $credits; 
                   1296:                 }
1.72      raeburn  1297:             }
                   1298:         } else {
                   1299:             $student_count{$status} ++;
1.73      raeburn  1300:             if ($showcredits) {
                   1301:                 $credit_count{$status} += $credits;
                   1302:             }
1.72      raeburn  1303:         }
1.1       raeburn  1304:     }
1.72      raeburn  1305:     if (@fields) {
                   1306:         $countslist .= ':<br />';
                   1307:         foreach my $status (@fields) {
                   1308:             $countslist .= '<span class="LC_nobreak">'.$titles{$status}.': '.
                   1309:                            $student_count{$status}.'</span><br />';
                   1310:             $numbers->{$status} += $student_count{$status};
1.73      raeburn  1311:             if ($showcredits) {
                   1312:                 $creditsum->{$status} += $credit_count{$status};
                   1313:             }
1.72      raeburn  1314:         }
1.1       raeburn  1315:     }
1.7       raeburn  1316:     return $countslist;
                   1317: }
                   1318: 
                   1319: sub courseinfo_row {
1.72      raeburn  1320:     my ($info,$knownuser,$details,$countref,$now,$course,$trails,$allitems,$numbers) = @_;
1.7       raeburn  1321:     my ($cdom,$cnum,$title,$ownerlast,$code,$owner,$seclist,$xlist_items,
1.35      raeburn  1322:         $accessdates,$showsyllabus,$counts,$autoenrollment,$output,$categories);
1.7       raeburn  1323:     if (ref($info) eq 'HASH') {
                   1324:         $cdom = $info->{'cdom'};
                   1325:         $cnum = $info->{'cnum'};
                   1326:         $title = $info->{'title'};
1.22      raeburn  1327:         $ownerlast = $info->{'ownerlastnames'};
1.7       raeburn  1328:         $code = $info->{'code'};
                   1329:         $owner = $info->{'owner'};
                   1330:         $seclist = $info->{'seclist'};
                   1331:         $xlist_items = $info->{'xlist'};
                   1332:         $accessdates = $info->{'access'};
                   1333:         $counts = $info->{'counts'};
                   1334:         $autoenrollment = $info->{'autoenrollment'};
                   1335:         $showsyllabus = $info->{'showsyllabus'};
1.35      raeburn  1336:         $categories = $info->{'categories'};
1.7       raeburn  1337:     } else {
                   1338:         $output = '<td colspan="8">'.&mt('No information available for [_1].',
                   1339:                                          $code).'</td>';
                   1340:         return $output;
1.2       raeburn  1341:     }
1.35      raeburn  1342:     $output .= '<td>'.$$countref.'</td>';
                   1343:     if ($env{'form.currcat_0'} eq 'instcode::0') {
                   1344:         $output .= '<td>'.$code.'</td>';
                   1345:     } else {
                   1346:         my ($categorylist,@cats);
                   1347:         if ($categories ne '') {
                   1348:             @cats = split('&',$categories);
                   1349:         }
                   1350:         if ((ref($trails) eq 'ARRAY') && (ref($allitems) eq 'HASH')) {
                   1351:             my @categories = map { $trails->[$allitems->{$_}]; } @cats;
                   1352:             $categorylist = join('<br />',@categories);
                   1353:         }
                   1354:         if ($categorylist eq '') {
                   1355:             $categorylist = '&nbsp;';
                   1356:         }
                   1357:         $output .= '<td>'.$categorylist.'</td>';
                   1358:     }
                   1359:     $output .= '<td>'.$seclist.'</td>'.
1.7       raeburn  1360:                '<td>'.$xlist_items.'</td>'.
                   1361:                '<td>'.$title.'&nbsp;<font size="-2">';
1.2       raeburn  1362:     if ($showsyllabus) {
1.40      raeburn  1363:         $output .= '<a href="javascript:ToSyllabus('."'$cdom','$cnum'".')">'.&mt('Syllabus').'</a>';
1.7       raeburn  1364:     } else {
                   1365:         $output .= '&nbsp;';
1.2       raeburn  1366:     }
                   1367:     $output .= '</font></td>'.
1.7       raeburn  1368:                '<td>'.$ownerlast.'</td>';
1.15      raeburn  1369:     if ($knownuser) {
                   1370:         if ($details) {
1.72      raeburn  1371:             $output .=
                   1372:                 '<td>'.$accessdates.'</td>'.
                   1373:                 '<td>'.$counts.'</td>';
                   1374:             unless ($env{'form.currcat_0'} eq 'communities::0') {
                   1375:                 $output .= '<td>'.$autoenrollment.'</td>';
1.60      raeburn  1376:             }
1.15      raeburn  1377:         } else {
                   1378:             $output .= "<td><a href=\"javascript:setCourseId('$cnum')\">".&mt('Show more details').'</a></td>';
1.8       raeburn  1379:         }
1.7       raeburn  1380:     }
1.27      raeburn  1381:     my $selfenroll;
                   1382:     if ($info->{'selfenroll_types'}) {
                   1383:         my $showstart = &Apache::lonlocal::locallocaltime($info->{'selfenroll_start'});
                   1384:         my $showend = &Apache::lonlocal::locallocaltime($info->{'selfenroll_end'});
                   1385:         if (($info->{'selfenroll_end'} > 0) && ($info->{'selfenroll_end'} > $now)) {
                   1386:             if (($info->{'selfenroll_start'} > 0) && ($info->{'selfenroll_start'} > $now)) {
                   1387:                 $output .= '<td>'.&mt('Starts: [_1]','<span class="LC_cusr_emph">'.$showstart.'</span>').'<br />'.&mt('Ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>').'</td>';
                   1388:             } else { 
1.66      raeburn  1389:                 $output .= '<td><a href="javascript:ToSelfenroll('."'$course'".')">'.&mt('Enroll in course').'</a><br />';
                   1390:                 if ($info->{'selfenroll_end'} == 0) {
                   1391:                     $output .= &mt('Available permanently');
                   1392:                 } elsif ($info->{'selfenroll_end'} > $now) {
                   1393:                     $output .= &mt('Self-enrollment ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>');
                   1394:                 }
                   1395:                 $output .= '</td>';
1.27      raeburn  1396:             }
                   1397:             $selfenroll = 1;
                   1398:         }
                   1399:     }
                   1400:     if (!$selfenroll) {
                   1401:         $output .= '<td>&nbsp;</td>';
                   1402:     }
1.24      raeburn  1403:     $$countref ++;
1.1       raeburn  1404:     return $output;
                   1405: }
                   1406: 
                   1407: sub identify_sections {
                   1408:     my ($seclist) = @_;
                   1409:     my @secnums;
                   1410:     if ($seclist =~ /,/) {
1.4       albertel 1411:         my @sections = split(/,/,$seclist);
1.1       raeburn  1412:         foreach my $sec (@sections) {
                   1413:             $sec =~ s/:[^:]*$//;
                   1414:             push(@secnums,$sec);
                   1415:         }
                   1416:     } else {
                   1417:         if ($seclist =~ m/^([^:]+):/) {
                   1418:             my $sec = $1;
1.4       albertel 1419:             if (!grep(/^\Q$sec\E$/,@secnums)) {
                   1420:                 push(@secnums,$sec);
1.1       raeburn  1421:             }
                   1422:         }
                   1423:     }
                   1424:     @secnums = sort {$a <=> $b} @secnums;
1.39      raeburn  1425:     $seclist = join(', ',@secnums);
1.15      raeburn  1426:     my $numsec = @secnums;
                   1427:     return ($seclist,$numsec);
1.1       raeburn  1428: }
                   1429: 
1.2       raeburn  1430: sub get_valid_classes {
1.22      raeburn  1431:     my ($seclist,$xlist_items,$crscode,$owners,$cdom,$cnum) = @_;
1.2       raeburn  1432:     my $response;
                   1433:     my %validations;
                   1434:     @{$validations{'sections'}} = ();
                   1435:     @{$validations{'xlists'}} = ();
                   1436:     my $totalitems = 0;
                   1437:     if ($seclist) {
1.13      raeburn  1438:         foreach my $sec (split(/, /,$seclist)) {
1.2       raeburn  1439:             my $class = $crscode.$sec;
1.22      raeburn  1440:             if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
1.3       albertel 1441: 							 $class) eq 'ok') {
1.2       raeburn  1442:                 if (!grep(/^\Q$sec$\E/,@{$validations{'sections'}})) {
1.4       albertel 1443:                     push(@{$validations{'sections'}},$sec);
1.2       raeburn  1444:                     $totalitems ++;
                   1445:                 }
                   1446:             }
                   1447:         }
                   1448:     }
                   1449:     if ($xlist_items) {
1.13      raeburn  1450:         foreach my $item (split(/, /,$xlist_items)) {
1.22      raeburn  1451:             if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
1.3       albertel 1452: 							 $item) eq 'ok') {
1.2       raeburn  1453:                 if (!grep(/^\Q$item$\E/,@{$validations{'xlists'}})) {
1.4       albertel 1454:                     push(@{$validations{'xlists'}},$item);
1.2       raeburn  1455:                     $totalitems ++;
                   1456:                 }
                   1457:             }
                   1458:         }
                   1459:     }
                   1460:     if ($totalitems > 0) {
                   1461:         if (@{$validations{'sections'}}) {
1.42      bisitz   1462:             $response = &mt('Sections:').' '.
1.14      raeburn  1463:                         join(', ',@{$validations{'sections'}}).'<br />';
1.2       raeburn  1464:         }
                   1465:         if (@{$validations{'xlists'}}) {
1.42      bisitz   1466:             $response .= &mt('Courses:').' '.
1.14      raeburn  1467:                         join(', ',@{$validations{'xlists'}});
1.2       raeburn  1468:         }
                   1469:     }
                   1470:     return $response;
                   1471: }
                   1472: 
1.7       raeburn  1473: sub autoenroll_info {
1.22      raeburn  1474:     my ($coursehash,$now,$seclist,$xlist_items,$code,$owners,$cdom,$cnum) = @_;
1.7       raeburn  1475:     my $autoenrolldates = &mt('Not enabled');
                   1476:     if (defined($coursehash->{'internal.autoadds'}) && $coursehash->{'internal.autoadds'} == 1) {
                   1477:         my ($autostart,$autoend);
                   1478:         if ( defined($coursehash->{'internal.autostart'}) ) {
                   1479:             $autostart = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autostart'});
                   1480:         }
                   1481:         if ( defined($coursehash->{'internal.autoend'}) ) {
                   1482:             $autoend = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autoend'});
                   1483:         }
                   1484:         if ($coursehash->{'internal.autostart'} > $now) {
                   1485:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
                   1486:                 $autoenrolldates = &mt('Not enabled');
                   1487:             } else {
                   1488:                 my $valid_classes = 
                   1489:                    &get_valid_classes($seclist,$xlist_items,$code,
1.22      raeburn  1490:                                       $owners,$cdom,$cnum);
1.7       raeburn  1491:                 if ($valid_classes ne '') {
1.42      bisitz   1492:                     $autoenrolldates = &mt('Not enabled').'<br />'
                   1493:                                       .&mt('Starts: [_1]',$autostart)
                   1494:                                       .'<br />'.$valid_classes;
                   1495:                 }
1.7       raeburn  1496:             }
                   1497:         } else {
                   1498:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
1.42      bisitz   1499:                 $autoenrolldates = &mt('Not enabled').'<br />'
                   1500:                                   .&mt('Ended: [_1]',$autoend);
1.7       raeburn  1501:             } else {
                   1502:                 my $valid_classes = &get_valid_classes($seclist,$xlist_items,
1.22      raeburn  1503:                                                        $code,$owners,$cdom,$cnum);
1.7       raeburn  1504:                 if ($valid_classes ne '') {
1.42      bisitz   1505:                     $autoenrolldates = &mt('Currently enabled').'<br />'.
1.7       raeburn  1506:                                        $valid_classes;
                   1507:                 }
                   1508:             }
                   1509:         }
                   1510:     }
                   1511:     return $autoenrolldates;
                   1512: }
                   1513: 
1.8       raeburn  1514: sub user_is_known {
                   1515:     my $known = 0;
                   1516:     if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public' 
                   1517:         && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
                   1518:         $known = 1;
                   1519:     }
                   1520:     return $known;
                   1521: }
                   1522: 
1.1       raeburn  1523: 1;

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