File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.74: download - view: text, annotated - select for diffs
Mon Mar 4 02:33:21 2013 UTC (11 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0_RC1, HEAD
- Wording changes.
  - Clarify what "count for past access" means for student counts and totals.

    1: # The LearningOnline Network with CAPA
    2: # Handler for displaying the course catalog interface
    3: #
    4: # $Id: coursecatalog.pm,v 1.74 2013/03/04 02:33:21 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::coursecatalog;
   30: 
   31: use strict;
   32: use lib qw(/home/httpd/lib/perl);
   33: use Apache::Constants qw(:common);
   34: use Apache::loncommon;
   35: use Apache::lonhtmlcommon;
   36: use Apache::lonnet;
   37: use Apache::lonlocal;
   38: use Apache::courseclassifier;
   39: use Apache::lonacc;
   40: use LONCAPA;
   41: 
   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:     }
   49:     my $handle = &Apache::lonnet::check_for_valid_session($r);
   50:     my $lonidsdir=$r->dir_config('lonIDsDir');
   51:     if ($handle ne '') {
   52:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
   53:     }
   54:     &Apache::lonacc::get_posted_cgi($r);
   55:     &Apache::lonlocal::get_language_handle($r);
   56:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   57:                                             ['sortby','showdom']);
   58: 
   59:     my $codedom = &Apache::lonnet::default_login_domain();
   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:     }
   67:     my $formname = 'coursecatalog';
   68:     if ($env{'form.showdom'} ne '') {
   69:         if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
   70:             $codedom = $env{'form.showdom'};
   71:         }
   72:     }
   73:     my $domdesc = &Apache::lonnet::domain($codedom,'description');
   74:     &Apache::lonhtmlcommon::clear_breadcrumbs();
   75: 
   76:     my %domconfig =
   77:         &Apache::lonnet::get_dom('configuration',['coursecategories'],$codedom);
   78:     my (@cats,@trails,%allitems,%idx,@jsarray,%subcathash,$cathash);
   79:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
   80:         $cathash = $domconfig{'coursecategories'}{'cats'};
   81:     } else {
   82:         $cathash = {};
   83:     }
   84:     my $subcats;
   85:     if ($env{'form.withsubcats'}) {
   86:         $subcats = \%subcathash;
   87:     }
   88:     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems,
   89:                                            \%idx,\@jsarray,$subcats);
   90:     my ($numtitles,@codetitles);
   91:     if ($env{'form.coursenum'} ne '' && &user_is_known()) {
   92:         &course_details($r,$codedom,$formname,$domdesc,\@trails,\%allitems,\@codetitles);
   93:     } else {
   94:         my ($catlinks,$has_subcats,$selitem) = &category_breadcrumbs($codedom,@cats);
   95:         my $wasacctext = &get_wasactive_text();
   96:         my $catjs = <<"ENDSCRIPT";
   97: 
   98: function setCatDepth(depth) {
   99:     document.coursecats.catalog_maxdepth.value = depth;
  100:     if (depth == '') {
  101:         document.coursecats.currcat_0.value = '';
  102:     }
  103:     document.coursecats.submit();
  104:     return;
  105: }
  106: 
  107: function changeSort(caller) {
  108:     document.$formname.sortby.value = caller;
  109:     document.$formname.submit();
  110: }
  111: 
  112: function setCourseId(caller) {
  113:     document.$formname.coursenum.value = caller;
  114:     document.$formname.submit();
  115: }
  116: 
  117: ENDSCRIPT
  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:         }
  148:         if ($env{'form.currcat_0'} eq 'instcode::0') {
  149:             $numtitles = &instcode_course_selector($r,$codedom,$formname,$domdesc,
  150:                                                    $catlinks,$catjs,\@codetitles);
  151:             if ($env{'form.state'} eq 'listing') {
  152:                 $r->print(&print_course_listing($codedom,$numtitles,undef,undef,undef,
  153:                                                 \@codetitles));
  154:             }
  155:         } else {
  156:             my (%add_entries);
  157:             my ($currdepth,$deeper) = &get_depth_values();
  158:             if ($selitem) {
  159:                 my $alert = &mt('Choose a subcategory to display');
  160:                 if (!$deeper) {
  161:                     $alert = &mt('Choose a category to display');
  162:                 }
  163:                 $catjs .= <<ENDJS;
  164: function check_selected() {
  165:     if (document.coursecats.$selitem.options[document.coursecats.$selitem.selectedIndex].value == "") {
  166:         alert('$alert');
  167:         return false;
  168:     }
  169: }
  170: ENDJS
  171:             }
  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 '') {
  175:                 $r->print('<form name="'.$formname.
  176:                           '" method="post" action="/adm/coursecatalog">'.
  177:                           &additional_filters($codedom,$has_subcats)."\n");
  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:                 }
  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:                 }
  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="'.
  195:                           $display_button.'" /></form><br /><br />');
  196:             }
  197:             if ($env{'form.state'} eq 'listing') {
  198:                 $r->print(&print_course_listing($codedom,undef,\@trails,\%allitems,$subcats,\@codetitles));
  199:             }
  200:         }
  201:     }
  202:     $r->print('<br />'.&Apache::loncommon::end_page());
  203:     return OK;
  204: }
  205: 
  206: sub course_details {
  207:     my ($r,$codedom,$formname,$domdesc,$trails,$allitems,$codetitles) = @_;
  208:     my $output;
  209:     my %add_entries = (topmargin    => "0",
  210:                        marginheight => "0",);
  211:     my $js = '<script type="text/javascript">'."\n".
  212:              &courselink_javascript().'</script>'."\n";
  213:     my $start_page =
  214:         &Apache::loncommon::start_page('Course/Community Catalog',$js,
  215:                                        {'add_entries' => \%add_entries, });
  216:     $r->print($start_page);
  217:     if ($env{'form.numtitles'} > 0) {
  218:         &Apache::lonhtmlcommon::add_breadcrumb
  219:                 ({href=>"/adm/coursecatalog",
  220:                   text=>"Course/Community Catalog"});
  221:     }
  222:     my $brtextone = 'Course listing';
  223:     my $brtexttwo = 'Course details';
  224:     if ($env{'form.currcat_0'} eq 'communities::0') {
  225:         $brtextone = 'Community listing';
  226:         $brtexttwo = 'Community details';
  227:     }
  228:     &Apache::lonhtmlcommon::add_breadcrumb
  229:              ({href=>"javascript:document.$formname.submit()",
  230:               text=>$brtextone},
  231:              {text=>$brtexttwo});
  232:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
  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 />'.
  240:               &print_course_listing($codedom,undef,$trails,$allitems,undef,$codetitles).
  241:               '<br /><br />');
  242:     $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
  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>'.
  250:               &Apache::lonhtmlcommon::echo_form_input(['coursenum','catalogfilter',
  251:                                                        'showdetails','courseid']).'</form>');
  252:     return;
  253: }
  254: 
  255: sub courselink_javascript {
  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:     }
  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();
  276: }
  277: 
  278: END
  279: }
  280: 
  281: sub instcode_course_selector {
  282:     my ($r,$codedom,$formname,$domdesc,$catlinks,$catjs,$codetitles) = @_;
  283:     my %coursecodes = ();
  284:     my %codes = ();
  285:     my %cat_titles = ();
  286:     my %cat_order = ();
  287:     my %cat_items;
  288:     my $caller = 'global';
  289:     my $format_reply;
  290:     my %add_entries = (topmargin    => "0",
  291:                        marginheight => "0",);
  292:     my ($jscript,$totcodes,$numtitles,$lasttitle) = 
  293:         &Apache::courseclassifier::instcode_selectors_data($codedom,$formname,
  294:                            \%cat_items,$codetitles,\%cat_titles,\%cat_order);
  295:     my $js = '<script type"text/javascript">'."\n$jscript\n$catjs\n".
  296:               '</script>';
  297:     if ($totcodes) {
  298:         if (($env{'form.state'} eq 'listing') && ($numtitles > 0)) {
  299:             $add_entries{'onLoad'} = 'setElements();';
  300:         }
  301:         if (&user_is_dc($codedom)) {
  302:             $add_entries{'onLoad'} .= ' toggleStatuses();toggleWasActive();'
  303:         }
  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">'.
  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" />'.
  310:                   &additional_filters($codedom));
  311:         if ($numtitles > 0) {
  312:             $r->print('<b>'.&mt('Choose which course(s) to list.').'</b><br />'.
  313:                       &Apache::courseclassifier::build_instcode_selectors($numtitles,
  314:                        $lasttitle,\%cat_items,$codetitles,\%cat_titles,\%cat_order));
  315:         }
  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.
  322:                   '" /></form><br /><br />');
  323:     } else {
  324:         $js = '<script type"text/javascript">'."\n$catjs\n".'</script>';
  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>');
  332:     }
  333:     return $numtitles;
  334: }
  335: 
  336: sub cat_header {
  337:     my ($r,$codedom,$js,$add_entries,$catlinks,$numtitles) = @_;
  338:     my $start_page =
  339:         &Apache::loncommon::start_page('Course/Community Catalog',$js,
  340:                                        { 'add_entries' => $add_entries, });
  341:     $r->print($start_page);
  342:     my $brtext = 'Course listing';
  343:     if ($env{'form.currcat_0'} eq 'communities::0') {
  344:         $brtext = 'Community listing';
  345:     }
  346:     if ($env{'form.state'} eq 'listing') {
  347:         if ($numtitles > 0) {
  348:             &Apache::lonhtmlcommon::add_breadcrumb
  349:                 ({href=>"/adm/coursecatalog",
  350:                   text=>"Course/Community Catalog"},
  351:                  {text=>$brtext});
  352:         } else {
  353:             &Apache::lonhtmlcommon::add_breadcrumb
  354:             ({text=>$brtext});
  355:         }
  356:     } else {
  357:         &Apache::lonhtmlcommon::add_breadcrumb
  358:         ({href=>"/adm/coursecatalog",
  359:           text=>"Course/Community Catalog"});
  360:     }
  361:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
  362:     my $onchange = 'this.form.submit()';
  363:     $r->print('<form name="coursecatdom" method="post" action="/adm/coursecatalog">'.
  364:               '<table border="0"><tr><td><b>'.&mt('Domain:').'</b></td><td>'.
  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>'.
  370: 	      '<form name="coursecats" method="post" action="/adm/coursecatalog"'.
  371:               ' onsubmit="return check_selected();">'.
  372:               '<table border="0"><tr>'.$catlinks.'</tr></table></form>');
  373:     return;
  374: }
  375: 
  376: sub category_breadcrumbs {
  377:     my ($dom,@cats) = @_;
  378:     my $crumbsymbol = ' &#x25b6; ';
  379:     my ($currdepth,$deeper) = &get_depth_values();
  380:     my $currcat_str = 
  381:         '<input type="hidden" name="catalog_maxdepth" value="'.$deeper.'" />'.
  382:         '<input type="hidden" name="showdom" value="'.$dom.'" />';
  383:     my $catlinks = '<td valign="top"><b>'.&mt('Catalog:').'</b></td><td><table><tr><td>';
  384:     my $has_subcats;
  385:     my $selitem;
  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;
  401:             }
  402:             $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
  403:         } else {
  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);
  419:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
  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];
  428:                         my $item = &escape($name).':'.&escape($container).':'.$shallower;
  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>';
  435:                     }
  436:                 }
  437:                 $catlinks .= '</select>';
  438:             }
  439:             unless ($i == $deeper) {
  440:                 $catlinks .= $crumbsymbol;
  441:             } 
  442:         }
  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:         }
  450:         my $deeperlevel = $depth +1;
  451:         if (ref($cats[$deeperlevel]{$cat}) eq 'ARRAY') {
  452:             $has_subcats = 1;
  453:             my $buttontext = &mt('Show subcategories');
  454:             my $selitem = 'currcat_'.$deeperlevel;
  455:             $catlinks .= '&nbsp;<select name="'.$selitem.'" onchange="this.form.submit()">';
  456:             if (@{$cats[$deeperlevel]{$cat}}) {
  457:                 $catlinks .= '<option value="" selected="selected">'.
  458:                              &mt('Subcategory ...').'</option>';
  459:             }
  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;
  463:                 $catlinks .= '<option value="'.$item.'">'.$name.'</option>'."\n";
  464:             }
  465:             $catlinks .= '</select>'."\n";
  466:         } elsif ($cat ne 'instcode') {
  467:             $catlinks .= '&nbsp;'.&mt('(No subcategories)');
  468:         }
  469:     } else {
  470:         $selitem = 'currcat_0';
  471:     }
  472:     $catlinks .= $currcat_str.'</td></tr></table></td>';
  473:     return ($catlinks,$has_subcats,$selitem);
  474: }
  475: 
  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: 
  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 {
  525:     my ($codedom,$has_subcats) = @_;
  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') &&
  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 '') {
  548:             $output .= '<span class="LC_nobreak"><label>'.
  549:                        '<input type="checkbox" name="withsubcats" value="1" '.
  550:                        $include_subcat_status.'/>'.
  551:                        &mt('Include subcategories within "[_1]"',
  552:                            &unescape($catname)).'</label></span><br />';
  553:         }
  554:     }
  555:     my $show_selfenroll_status;
  556:     if ($env{'form.showselfenroll'}) {
  557:         $show_selfenroll_status = 'checked="checked" ';
  558:     }
  559:     my $selfenroll_text;
  560:     if ($env{'form.currcat_0'} eq 'communities::0') {
  561:         $selfenroll_text = &mt('Only show communities which currently allow self-enrollment (or will allow it in the future)');
  562:     } else {
  563:         $selfenroll_text = &mt('Only show courses which currently allow self-enrollment (or will allow it in the future)');
  564:     }
  565:     $output .= '<span class="LC_nobreak">'.
  566:                '<label><input type="checkbox" name="showselfenroll" value="1" '.
  567:                $show_selfenroll_status.'/>'.$selfenroll_text.
  568:                '</label></span><br />';
  569:     if ($is_dc) {
  570:         my ($titlesref,$orderref) = &get_statustitles('filters');
  571:         my $showdetails_status;
  572:         if ($env{'form.showdetails'}) {
  573:             $showdetails_status = 'checked="checked" ';
  574:         }
  575:         my $showhidden_status;
  576:         if ($env{'form.showhidden'}) {
  577:             $showhidden_status = 'checked="checked" ';
  578:         }
  579:         my @currstatuses = &Apache::loncommon::get_env_multiple('form.showcounts');
  580:         my $dc_title = &Apache::lonnet::plaintext('dc');
  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:         }
  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:         }
  600:         $output .= '<span class="LC_nobreak">'.
  601:                    '<label><input type="checkbox" name="showhidden" value="1" '.
  602:                    $showhidden_status.'/>'.$hidden_text.
  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 (
  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:',
  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>';  
  691:     }
  692:     $output .= '</tr></table></fieldset></div>'.
  693:                '<div style="clear:both;margin:0;"></div>';  
  694:     return $output;
  695: }
  696: 
  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: }
  709: 
  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') {
  737:         $wasacctext .= &mt('where member access status was current ...');
  738:     } else {
  739:         $wasacctext .= &mt('where student access status was current ...');
  740:     }
  741:     return $wasacctext;
  742: }
  743: 
  744: sub search_official_courselist {
  745:     my ($domain,$numtitles,$codetitles) = @_;
  746:     my $instcode = &Apache::courseclassifier::instcode_search_str($domain,$numtitles,$codetitles);
  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');
  755:     return %courses;
  756: }
  757: 
  758: sub search_courselist {
  759:     my ($domain,$subcats) = @_;
  760:     my $cat_maxdepth = $env{'form.catalog_maxdepth'};
  761:     my $filter = $env{'form.currcat_'.$cat_maxdepth};
  762:     if (($filter eq '') && ($cat_maxdepth > 0)) {
  763:         my $shallower = $cat_maxdepth - 1;
  764:         $filter = $env{'form.currcat_'.$shallower};
  765:     }
  766:     my %courses;
  767:     my $filterstr;
  768:     if ($filter ne '') {
  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:         }
  785:         my ($showhidden,$typefilter);
  786:         if (&user_is_dc($domain)) {
  787:             $showhidden = $env{'form.showhidden'};
  788:         }
  789:         if ($env{'form.currcat_0'} eq 'communities::0') {
  790:             $typefilter = 'Community';
  791:         } else {
  792:             $typefilter = '.';
  793:         }
  794:         %courses = 
  795:             &Apache::lonnet::courseiddump($domain,'.',1,'.','.','.',undef,undef,
  796:                                           $typefilter,1,$env{'form.showselfenroll'},
  797:                                           $filterstr,$showhidden,'coursecatalog');
  798:     }
  799:     return %courses;
  800: }
  801: 
  802: sub print_course_listing {
  803:     my ($domain,$numtitles,$trails,$allitems,$subcats,$codetitles) = @_;
  804:     my $output;
  805:     my %courses;
  806:     my $knownuser = &user_is_known();
  807:     my $details = $env{'form.coursenum'};
  808:     if (&user_is_dc($domain)) {
  809:         if ($env{'form.showdetails'}) {
  810:             $details = 1;
  811:         }
  812:     }
  813:     if ($env{'form.coursenum'} ne '') {
  814:         %courses = &Apache::lonnet::courseiddump($domain,'.',1,'.','.',
  815:                                                  $env{'form.coursenum'},
  816:                                                  undef,undef,'.',1);
  817:         if (keys(%courses) == 0) {
  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:             }
  823:             return $output;
  824:         }
  825:     } else {
  826:         if ($env{'form.currcat_0'} eq 'instcode::0') {
  827:             %courses = &search_official_courselist($domain,$numtitles,$codetitles);
  828:         } else {
  829:             %courses = &search_courselist($domain,$subcats);
  830:         }
  831:         if (keys(%courses) == 0) {
  832:             if ($env{'form.currcat_0'} eq 'communities::0') {
  833:                 $output =
  834:                     '<p class="LC_info">'
  835:                    .&mt('No communities match the criteria you selected.')
  836:                    .'</p>';
  837:             } else {
  838:                 $output =
  839:                     '<p class="LC_info">'
  840:                    .&mt('No courses match the criteria you selected.')
  841:                    .'</p>';
  842:             }
  843:             return $output;
  844:         }
  845:         if (($knownuser) && (!$env{'form.showdetails'}) && (!&user_is_dc($domain))) {
  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 />';
  849:         }
  850:     }
  851:     my $now = time;
  852:     my %domconfig =
  853:         &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
  854:     $output .= &construct_data_table($knownuser,$domain,\%courses,$details,undef,
  855:                                      $now,\%domconfig,$trails,$allitems);
  856:     $output .= "\n".'<form name="linklaunch" method="post" action="">'.
  857:                '<input type="hidden" name="backto" value="coursecatalog" />'.
  858:                '<input type="hidden" name="courseid" value="" />'.
  859:                &Apache::lonhtmlcommon::echo_form_input(['catalogfilter','courseid']).'</form>';
  860:     return $output;
  861: }
  862: 
  863: sub construct_data_table {
  864:     my ($knownuser,$domain,$courses,$details,$usersections,$now,$domconfig,
  865:         $trails,$allitems) = @_;
  866:     my %sortname;
  867:     if (($details eq '') || ($env{'form.showdetails'})) {
  868:         $sortname{'Code'} = 'code';
  869:         $sortname{'Categories'} = 'cats';
  870:         $sortname{'Title'} = 'title';
  871:         $sortname{'Owner & Co-owner(s)'} = 'owner';
  872:     }
  873:     my $output = &Apache::loncommon::start_data_table().
  874:                  &Apache::loncommon::start_data_table_header_row();
  875:     my @coltitles = ('Count');
  876:     if ($env{'form.currcat_0'} eq 'instcode::0') {
  877:         push(@coltitles,'Code');
  878:     } else {
  879:         push(@coltitles,'Categories');
  880:     }
  881:     push(@coltitles,('Sections','Crosslisted','Title','Owner & Co-owner(s)'));
  882:     if (ref($usersections) eq 'HASH') {
  883:        $coltitles[1] = 'Your Section';
  884:     }
  885:     foreach my $item (@coltitles) {
  886:         $output .= '<th>';
  887:         if (defined($sortname{$item})) {
  888:             $output .= '<a href="javascript:changeSort('."'$sortname{$item}'".')">'.&mt($item).'</a>';
  889:         } elsif ($item eq 'Count') {
  890:             $output .= '&nbsp;&nbsp;';
  891:         } else {
  892:             $output .= &mt($item);
  893:         }
  894:         $output .= '</th>';
  895:     }
  896:     my (@fields,%fieldtitles,$wasactiveon);
  897:     if ($knownuser) {
  898:         if ($details) {
  899:             if ($env{'form.currcat_0'} eq 'communities::0') {
  900:                 $output .= '<th>'.&mt('Default Access Dates for Members').'</th>'.
  901:                            '<th>'.&mt('Member Counts').'</th>';
  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:             }
  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:             }
  940:         } else {
  941:             $output .= '<th>'.&mt('Details').'</th>';
  942:         }
  943:     }
  944:     $output .= '<th>'.&mt('Self-enroll (if permitted)').'</th>';
  945:     &Apache::loncommon::end_data_table_header_row();
  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:     }
  956:     my %courseinfo = &build_courseinfo_hash($courses,$knownuser,$domain,$details,
  957:                                             $usersections,\@fields,\%fieldtitles,
  958:                                             $wasactiveon,\%numbers,\%creditsum,
  959:                                             $showcredits,$defofficial,$defunofficial);
  960:     my %Sortby;
  961:     foreach my $course (sort(keys(%{$courses}))) {
  962:         if ($env{'form.sortby'} eq 'code') {
  963:             push(@{$Sortby{$courseinfo{$course}{'code'}}},$course);
  964:         } elsif ($env{'form.sortby'} eq 'cats') {
  965:             push(@{$Sortby{$courseinfo{$course}{'categories'}}},$course);
  966:         } elsif ($env{'form.sortby'} eq 'owner') {
  967:             push(@{$Sortby{$courseinfo{$course}{'ownerlastnames'}}},$course);
  968:         } else {
  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);
  975:         }
  976:     }
  977:     my @sorted_courses;
  978:     if (($env{'form.sortby'} eq 'code') || ($env{'form.sortby'} eq 'owner') ||
  979:         ($env{'form.sortby'} eq 'cats')) {
  980:         @sorted_courses = sort(keys(%Sortby));
  981:     } else {
  982:         @sorted_courses = sort { lc($a) cmp lc($b) } (keys(%Sortby));
  983:     }
  984:     my $count = 1;
  985:     my $totalsec = 0;
  986:     foreach my $item (@sorted_courses) {
  987:         foreach my $course (@{$Sortby{$item}}) {
  988:             $output.=&Apache::loncommon::start_data_table_row(); 
  989:             $output.=&courseinfo_row($courseinfo{$course},$knownuser,$details,
  990:                                      \$count,$now,$course,$trails,$allitems,\%numbers);
  991:             $output.=&Apache::loncommon::end_data_table_row();
  992:         }
  993:     }
  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',
 1002:                                                      'xlists'   => 'Total cross-listings',
 1003:                                                  );
 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:             }
 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');
 1014:             }
 1015:             my $colspan = 8;
 1016:             if ($showcredits) {
 1017:                 $colspan = 4;
 1018:             }
 1019:             $output .= '<tr class="LC_footer_row">'.
 1020:                        '<td colspan="2">&nbsp;</td>'.
 1021:                        '<td colspan="'.$colspan.'">'.
 1022:                        '<table border="0">';
 1023:             foreach my $item ('courses','sections','xlists') {
 1024:                 $output .= '<tr>'.
 1025:                            '<td>'.$lt{$item}.'</td><td>&nbsp;</td>'.
 1026:                            '<td align="right">'.$numbers{$item}.'</td>'.
 1027:                            '</tr>'."\n";
 1028:             }
 1029:             if (@fields > 0) { 
 1030:                 foreach my $status (@fields) {
 1031:                     $output .= '<tr>'.
 1032:                                '<td>'.$lt{$status}.'</td><td>&nbsp;</td>'.
 1033:                                '<td align="right">'.$numbers{$status}.'</td>'.
 1034:                                '</tr>'."\n";
 1035:                 }
 1036:             }
 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:             }
 1047:         }
 1048:     }
 1049:     $output .= &Apache::loncommon::end_data_table();
 1050:     return $output;
 1051: }
 1052: 
 1053: sub build_courseinfo_hash {
 1054:     my ($courses,$knownuser,$domain,$details,$usersections,$fields,$fieldtitles,
 1055:         $wasactiveon,$numbers,$creditsum,$showcredits,$defofficial,$defunofficial) = @_;
 1056:     my %courseinfo;
 1057:     my $now = time;
 1058:     my $gettotals;
 1059:     if ((keys(%{$courses}) > 0) && (&user_is_dc($domain)) && ($details)) {
 1060:         $gettotals = 1;
 1061:     }
 1062:     my (%uniquecodes,$nocodes,$defcreds);
 1063:     foreach my $course (keys(%{$courses})) {
 1064:         my $descr;
 1065:         if (ref($courses->{$course}) eq 'HASH') {
 1066:             $descr = $courses->{$course}{'description'}; 
 1067:         }
 1068:         my $cleandesc=&HTML::Entities::encode($descr,'<>&"');
 1069:         $cleandesc=~s/'/\\'/g;
 1070:         $cleandesc =~ s/^\s+//;
 1071:         my ($cdom,$cnum)=split(/\_/,$course);
 1072:         my ($instcode,$singleowner,$ttype,$selfenroll_types,
 1073:             $selfenroll_start,$selfenroll_end,@owners,%ownernames,$categories);
 1074:         if (ref($courses->{$course}) eq 'HASH') {
 1075:             $descr = $courses->{$course}{'description'};
 1076:             $instcode =  $courses->{$course}{'inst_code'};
 1077:             $singleowner = $courses->{$course}{'owner'};
 1078:             $ttype =  $courses->{$course}{'type'};
 1079:             $selfenroll_types = $courses->{$course}{'selfenroll_types'};
 1080:             $selfenroll_start = $courses->{$course}{'selfenroll_start_date'};
 1081:             $selfenroll_end = $courses->{$course}{'selfenroll_end_date'};
 1082:             $categories = $courses->{$course}{'categories'};
 1083:             push(@owners,$singleowner);
 1084:             if ($courses->{$course}{'co-owners'} ne '') {
 1085:                 foreach my $item (split(/,/,$courses->{$course}{'co-owners'})) {
 1086:                     push(@owners,$item);
 1087:                 }
 1088:             }
 1089:         }
 1090:         if ($instcode ne '') {
 1091:             $uniquecodes{$instcode} = 1;
 1092:         } else {
 1093:             $nocodes ++;
 1094:         } 
 1095:         foreach my $owner (@owners) {
 1096:             my ($ownername,$ownerdom); 
 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; 
 1108:             }
 1109:         }
 1110:         $courseinfo{$course}{'cdom'} = $cdom;
 1111:         $courseinfo{$course}{'cnum'} = $cnum;
 1112:         $courseinfo{$course}{'code'} = $instcode;
 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));
 1120:         $courseinfo{$course}{'title'} = $cleandesc;
 1121:         $courseinfo{$course}{'owner'} = $singleowner;
 1122:         $courseinfo{$course}{'selfenroll_types'} = $selfenroll_types;
 1123:         $courseinfo{$course}{'selfenroll_start'} = $selfenroll_start;
 1124:         $courseinfo{$course}{'selfenroll_end'} = $selfenroll_end;
 1125:         $courseinfo{$course}{'categories'} = $categories;
 1126: 
 1127:         my %coursehash = &Apache::lonnet::dump('environment',$cdom,$cnum);
 1128:         my @classids;
 1129:         my @crosslistings;
 1130:         my ($seclist,$numsec) = 
 1131:             &identify_sections($coursehash{'internal.sectionnums'});
 1132:         $courseinfo{$course}{'seclist'} = $seclist;
 1133:         my ($xlist_items,$numxlist) = 
 1134:             &identify_sections($coursehash{'internal.crosslistings'});
 1135:         if (ref($numbers) eq 'HASH') {
 1136:             $numbers->{'sections'} += $numsec; 
 1137:             $numbers->{'xlists'} += $numxlist;  
 1138:         }
 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;
 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:         }
 1154:         if (((defined($env{'form.coursenum'}) && ($cnum eq $env{'form.coursenum'}))) ||
 1155:             ($knownuser && ($details == 1))) {
 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,
 1174:                                 $numbers,$creditsum,$showcredits,$defcreds,$milestone);
 1175:             if ($instcode ne '') {
 1176:                 $courseinfo{$course}{'autoenrollment'} =
 1177:                     &autoenroll_info(\%coursehash,$now,$seclist,$xlist_items,
 1178:                                      $instcode,\@owners,$cdom,$cnum);
 1179:             }
 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) {
 1189:                     $endaccess = &mt('No ending date');
 1190:                 }
 1191:             }
 1192:             if ($startaccess) {
 1193:                 $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$startaccess).'<br />';
 1194:             }
 1195:             if ($endaccess) {
 1196:                 $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$endaccess).'<br />';
 1197:             }
 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) {
 1217:                             $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$selfenroll_start_access).'<br />';
 1218:                         }
 1219:                         if ($selfenroll_end_access) {
 1220:                             $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$selfenroll_end_access).'<br />';
 1221:                         }
 1222:                     }
 1223:                 }
 1224:             }
 1225:             $courseinfo{$course}{'access'} = $accessdates;
 1226:         }
 1227:         if ($xlist_items eq '') {
 1228:             $xlist_items = &mt('No');
 1229:         }
 1230:         $courseinfo{$course}{'xlist'} = $xlist_items;
 1231:     }
 1232:     if (ref($numbers) eq 'HASH') {
 1233:         $numbers->{'courses'} = $nocodes + scalar(keys(%uniquecodes));
 1234:     }
 1235:     return %courseinfo;
 1236: }
 1237: 
 1238: sub count_students {
 1239:     my ($cdom,$cnum,$numsec,$fieldsref,$titlesref,$getcounts,$numbers,$creditsum,
 1240:         $showcredits,$defcreds,$wasactiveon) = @_;
 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:     }
 1254:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
 1255:     my (%student_count,%credit_count);
 1256:     %student_count = (
 1257:                            Active   => 0,
 1258:                            Future   => 0,
 1259:                            Previous => 0,
 1260:                      );
 1261:     if ($showcredits) {
 1262:         %credit_count = (
 1263:                           Active   => 0,
 1264:                           Future   => 0,
 1265:                           Previous => 0,
 1266:                         );
 1267:     }
 1268:     my %idx;
 1269:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
 1270:     $idx{'end'}    = &Apache::loncoursedata::CL_END();
 1271:     $idx{'credits'} = &Apache::loncoursedata::CL_CREDITS();
 1272:     while (my ($student,$data) = each(%$classlist)) {
 1273:         my $status = $data->[$idx{'status'}];
 1274:         my $credits = $data->[$idx{'credits'}];
 1275:         if ($credits eq '') {
 1276:             $credits = $defcreds;  
 1277:         }
 1278:         if ($status eq 'Expired') {
 1279:             if (($showexpired) &&
 1280:                 ($data->[$idx{'end'}] >= $wasactiveon)) {
 1281:                 $student_count{'Previous'} ++;
 1282:                 if ($showcredits) {
 1283:                     $credit_count{'Previous'} += $credits; 
 1284:                 }
 1285:             }
 1286:         } else {
 1287:             $student_count{$status} ++;
 1288:             if ($showcredits) {
 1289:                 $credit_count{$status} += $credits;
 1290:             }
 1291:         }
 1292:     }
 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};
 1299:             if ($showcredits) {
 1300:                 $creditsum->{$status} += $credit_count{$status};
 1301:             }
 1302:         }
 1303:     }
 1304:     return $countslist;
 1305: }
 1306: 
 1307: sub courseinfo_row {
 1308:     my ($info,$knownuser,$details,$countref,$now,$course,$trails,$allitems,$numbers) = @_;
 1309:     my ($cdom,$cnum,$title,$ownerlast,$code,$owner,$seclist,$xlist_items,
 1310:         $accessdates,$showsyllabus,$counts,$autoenrollment,$output,$categories);
 1311:     if (ref($info) eq 'HASH') {
 1312:         $cdom = $info->{'cdom'};
 1313:         $cnum = $info->{'cnum'};
 1314:         $title = $info->{'title'};
 1315:         $ownerlast = $info->{'ownerlastnames'};
 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'};
 1324:         $categories = $info->{'categories'};
 1325:     } else {
 1326:         $output = '<td colspan="8">'.&mt('No information available for [_1].',
 1327:                                          $code).'</td>';
 1328:         return $output;
 1329:     }
 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>'.
 1348:                '<td>'.$xlist_items.'</td>'.
 1349:                '<td>'.$title.'&nbsp;<font size="-2">';
 1350:     if ($showsyllabus) {
 1351:         $output .= '<a href="javascript:ToSyllabus('."'$cdom','$cnum'".')">'.&mt('Syllabus').'</a>';
 1352:     } else {
 1353:         $output .= '&nbsp;';
 1354:     }
 1355:     $output .= '</font></td>'.
 1356:                '<td>'.$ownerlast.'</td>';
 1357:     if ($knownuser) {
 1358:         if ($details) {
 1359:             $output .=
 1360:                 '<td>'.$accessdates.'</td>'.
 1361:                 '<td>'.$counts.'</td>';
 1362:             unless ($env{'form.currcat_0'} eq 'communities::0') {
 1363:                 $output .= '<td>'.$autoenrollment.'</td>';
 1364:             }
 1365:         } else {
 1366:             $output .= "<td><a href=\"javascript:setCourseId('$cnum')\">".&mt('Show more details').'</a></td>';
 1367:         }
 1368:     }
 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 { 
 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>';
 1384:             }
 1385:             $selfenroll = 1;
 1386:         }
 1387:     }
 1388:     if (!$selfenroll) {
 1389:         $output .= '<td>&nbsp;</td>';
 1390:     }
 1391:     $$countref ++;
 1392:     return $output;
 1393: }
 1394: 
 1395: sub identify_sections {
 1396:     my ($seclist) = @_;
 1397:     my @secnums;
 1398:     if ($seclist =~ /,/) {
 1399:         my @sections = split(/,/,$seclist);
 1400:         foreach my $sec (@sections) {
 1401:             $sec =~ s/:[^:]*$//;
 1402:             push(@secnums,$sec);
 1403:         }
 1404:     } else {
 1405:         if ($seclist =~ m/^([^:]+):/) {
 1406:             my $sec = $1;
 1407:             if (!grep(/^\Q$sec\E$/,@secnums)) {
 1408:                 push(@secnums,$sec);
 1409:             }
 1410:         }
 1411:     }
 1412:     @secnums = sort {$a <=> $b} @secnums;
 1413:     $seclist = join(', ',@secnums);
 1414:     my $numsec = @secnums;
 1415:     return ($seclist,$numsec);
 1416: }
 1417: 
 1418: sub get_valid_classes {
 1419:     my ($seclist,$xlist_items,$crscode,$owners,$cdom,$cnum) = @_;
 1420:     my $response;
 1421:     my %validations;
 1422:     @{$validations{'sections'}} = ();
 1423:     @{$validations{'xlists'}} = ();
 1424:     my $totalitems = 0;
 1425:     if ($seclist) {
 1426:         foreach my $sec (split(/, /,$seclist)) {
 1427:             my $class = $crscode.$sec;
 1428:             if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
 1429: 							 $class) eq 'ok') {
 1430:                 if (!grep(/^\Q$sec$\E/,@{$validations{'sections'}})) {
 1431:                     push(@{$validations{'sections'}},$sec);
 1432:                     $totalitems ++;
 1433:                 }
 1434:             }
 1435:         }
 1436:     }
 1437:     if ($xlist_items) {
 1438:         foreach my $item (split(/, /,$xlist_items)) {
 1439:             if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
 1440: 							 $item) eq 'ok') {
 1441:                 if (!grep(/^\Q$item$\E/,@{$validations{'xlists'}})) {
 1442:                     push(@{$validations{'xlists'}},$item);
 1443:                     $totalitems ++;
 1444:                 }
 1445:             }
 1446:         }
 1447:     }
 1448:     if ($totalitems > 0) {
 1449:         if (@{$validations{'sections'}}) {
 1450:             $response = &mt('Sections:').' '.
 1451:                         join(', ',@{$validations{'sections'}}).'<br />';
 1452:         }
 1453:         if (@{$validations{'xlists'}}) {
 1454:             $response .= &mt('Courses:').' '.
 1455:                         join(', ',@{$validations{'xlists'}});
 1456:         }
 1457:     }
 1458:     return $response;
 1459: }
 1460: 
 1461: sub autoenroll_info {
 1462:     my ($coursehash,$now,$seclist,$xlist_items,$code,$owners,$cdom,$cnum) = @_;
 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,
 1478:                                       $owners,$cdom,$cnum);
 1479:                 if ($valid_classes ne '') {
 1480:                     $autoenrolldates = &mt('Not enabled').'<br />'
 1481:                                       .&mt('Starts: [_1]',$autostart)
 1482:                                       .'<br />'.$valid_classes;
 1483:                 }
 1484:             }
 1485:         } else {
 1486:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
 1487:                 $autoenrolldates = &mt('Not enabled').'<br />'
 1488:                                   .&mt('Ended: [_1]',$autoend);
 1489:             } else {
 1490:                 my $valid_classes = &get_valid_classes($seclist,$xlist_items,
 1491:                                                        $code,$owners,$cdom,$cnum);
 1492:                 if ($valid_classes ne '') {
 1493:                     $autoenrolldates = &mt('Currently enabled').'<br />'.
 1494:                                        $valid_classes;
 1495:                 }
 1496:             }
 1497:         }
 1498:     }
 1499:     return $autoenrolldates;
 1500: }
 1501: 
 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: 
 1511: 1;

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