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

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

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