File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.38: download - view: text, annotated - select for diffs
Sun Jul 13 20:13:10 2008 UTC (15 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Add form action.
- showdom could be passed as an argument in a query string
- unescape() category in subcategories? prompt

    1: # The LearningOnline Network with CAPA
    2: # Handler for displaying the course catalog interface
    3: #
    4: # $Id: coursecatalog.pm,v 1.38 2008/07/13 20:13:10 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:     if ($env{'form.coursenum'} ne '' && &user_is_known()) {
   91:         &course_details($r,$codedom,$formname,$domdesc,\@trails,\%allitems);
   92:     } else {
   93:         my ($catlinks,$has_subcats) = &category_breadcrumbs($codedom,@cats);
   94:         my $catjs = <<"ENDSCRIPT";
   95: 
   96: function setCatDepth(depth) {
   97:     document.coursecats.catalog_maxdepth.value = depth;
   98:     if (depth == '') {
   99:         document.coursecats.currcat_0.value = '';
  100:     }
  101:     document.coursecats.submit();
  102:     return;
  103: }
  104: 
  105: function changeSort(caller) {
  106:     document.$formname.sortby.value = caller;
  107:     document.$formname.submit();
  108: }
  109: function setCourseId(caller) {
  110:     document.$formname.coursenum.value = caller;
  111:     document.$formname.submit();
  112: }
  113: 
  114: ENDSCRIPT
  115:         my $numtitles;
  116:         if ($env{'form.currcat_0'} eq 'instcode::0') {
  117:             $numtitles = &instcode_course_selector($r,$codedom,$formname,$domdesc,
  118:                                                    $catlinks,$catjs);
  119:             if ($env{'form.state'} eq 'listing') {
  120:                 $r->print(&print_course_listing($codedom,$numtitles));
  121:             }
  122:         } else {
  123:             my (%add_entries);
  124:             $catjs = '<script type="text/javascript">'."\n".$catjs."\n".'</script>';
  125:             &cat_header($r,$codedom,$catjs,\%add_entries,$catlinks);
  126:             if ($env{'form.currcat_0'} ne '') {
  127:                 $r->print('<form name="'.$formname.
  128:                           '" method="post" action="/adm/coursecatalog">'.
  129:                           &additional_filters($codedom,$has_subcats)."\n");
  130:                 my ($currdepth,$deeper) = &get_depth_values();
  131:                 $r->print('<input type="hidden" name="catalog_maxdepth" value="'.
  132:                           $deeper.'" />'."\n");
  133:                 for (my $i=0; $i<$deeper; $i++) {
  134:                     $r->print('<input type="hidden" name="currcat_'.$i.'" value="'.$env{'form.currcat_'.$i}.'" />'."\n");
  135:                 }
  136:                 $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
  137:                           '<input type="hidden" name="sortby" value="" />'."\n".
  138:                           '<input type="hidden" name="state" value="listing" />'."\n".
  139:                           '<input type="hidden" name="showdom" value="'.
  140:                           $env{'form.showdom'}.'" />'.
  141:                           '<input type="submit" name="catalogfilter" value="'.
  142:                           &mt('Display courses').'" /></form><br /><br />');
  143:             }
  144:             if ($env{'form.state'} eq 'listing') {
  145:                 $r->print(&print_course_listing($codedom,undef,\@trails,\%allitems,$subcats));
  146:             }
  147:         }
  148:     }
  149:     $r->print('<br />'.&Apache::loncommon::end_page());
  150:     return OK;
  151: }
  152: 
  153: sub course_details {
  154:     my ($r,$codedom,$formname,$domdesc,$trails,$allitems) = @_;
  155:     my $output;
  156:     my %add_entries = (topmargin    => "0",
  157:                        marginheight => "0",);
  158:     my $start_page =
  159:         &Apache::loncommon::start_page('Course Catalog','',
  160:                                            {
  161:                                              'add_entries' => \%add_entries,
  162:                                              'no_inline_link'   => 1,});
  163:     $r->print($start_page);
  164:     if ($env{'form.numtitles'} > 0) {
  165:         &Apache::lonhtmlcommon::add_breadcrumb
  166:                 ({href=>"/adm/coursecatalog",
  167:                   text=>"Select courses"});
  168:     }
  169:     &Apache::lonhtmlcommon::add_breadcrumb
  170:              ({href=>"javascript:document.$formname.submit()",
  171:               text=>"Course listing"},
  172:              {text=>"Course details"});
  173:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course Details'));
  174:     $r->print('<br />'.&mt('Detailed course information:').'<br /><br />'.
  175:               '<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
  176:               &print_course_listing($codedom,undef,$trails,$allitems).
  177:               '<br /><br />');
  178:     $r->print('<a href = "javascript:document.coursecatalog.submit()">'.
  179:               &mt('Back to course listing').'</a>'.
  180:               '<input type="hidden" name="sortby" value="'.
  181:               $env{'form.sortby'}.'" />'.
  182:               '<input type="hidden" name="state" value="listing" /></form>');
  183: }
  184: 
  185: sub instcode_course_selector {
  186:     my ($r,$codedom,$formname,$domdesc,$catlinks,$catjs) = @_;
  187:     my %coursecodes = ();
  188:     my %codes = ();
  189:     my @codetitles = ();
  190:     my %cat_titles = ();
  191:     my %cat_order = ();
  192:     my %idlist = ();
  193:     my %idnums = ();
  194:     my %idlist_titles = ();
  195:     my %by_year;
  196:     my %by_sem;
  197:     my %by_dept;
  198:     my %cat_items;
  199:     my $caller = 'global';
  200:     my $format_reply;
  201:     my $totcodes = 0;
  202:     my $jscript = '';
  203:     my ($numtitles,$lasttitle);
  204:     my %add_entries = (topmargin    => "0",
  205:                        marginheight => "0",);
  206:     my $js;
  207:     $totcodes = &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,$codedom);
  208:     if ($totcodes > 0) {
  209:         $format_reply = &Apache::lonnet::auto_instcode_format($caller,$codedom,\%coursecodes,\%codes,\@codetitles,\%cat_titles,\%cat_order);
  210:         if ($format_reply eq 'ok') {
  211:             my $numtypes = @codetitles;
  212:             &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
  213:             my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
  214:             my $longtitles_str = join('","',@{$longtitles});
  215:             my $allidlist = $idlist{$codetitles[0]};
  216:             $numtitles = @codetitles;
  217:             $lasttitle = $numtitles;
  218:             if ($numtitles > 4) {
  219:                 $lasttitle = 4;
  220:             }
  221:             if ($numtitles == 0) {
  222:                 if (!defined($env{'form.state'})) {
  223:                     $env{'form.state'} = 'listing';
  224:                 }
  225:             } else {
  226:                 my @data = ('top');
  227:                 for (my $k=0; $k<$lasttitle; $k++) {
  228:                     my $cat = $codetitles[$k];
  229:                     my $level = 1;
  230:                     $level = &recurse_options($codetitles[$k],$idlist{$codetitles[$k]},$level,$cat,\%cat_items,\@data,\%by_year,\%by_sem,\%by_dept);
  231:                 }
  232:                 $scripttext .= &build_javascript(\%by_year,\%by_sem,\%by_dept,\%cat_order,\@codetitles);
  233:                 $jscript .= &javascript_select_filler($formname,$scripttext,\@codetitles,$longtitles_str,$allidlist);
  234:                 if ($env{'form.state'} eq 'listing') {
  235:                     $jscript .= '
  236: function setElements() {
  237: ';
  238:                     for (my $i=0; $i<@codetitles-1; $i++) {
  239:                         if ($env{'form.'.$codetitles[$i]} != -1) {
  240:                             $jscript .= '
  241:     for (var j=0; j<document.'.$formname.'.'.$codetitles[$i].'.length; j++) {
  242:         if (document.'.$formname.'.'.$codetitles[$i].'[j].value == "'.$env{'form.'.$codetitles[$i]}.'") {
  243:             document.'.$formname.'.'.$codetitles[$i].'.selectedIndex = j;
  244:         }
  245:     }
  246: ';
  247:                         }
  248:                     }
  249:                     $jscript .= '   courseSet()'."\n";
  250:                     if ($env{'form.'.$codetitles[-1]} != -1) {
  251:                         $jscript .= '
  252:     for (var j=0; j<document.'.$formname.'.'.$codetitles[-1].'.length; j++) {
  253:         if (document.'.$formname.'.'.$codetitles[-1].'[j].value == "'.$env{'form.'.$codetitles[-1]}.'") {
  254:             document.'.$formname.'.'.$codetitles[-1].'.selectedIndex = j;
  255:         }
  256:     }
  257: ';
  258:                     }
  259:                     $jscript .= '}';
  260:                 }
  261:             }
  262:         }
  263:         $js = '<script type"text/javascript">'."\n$jscript\n$catjs\n".
  264:               '</script>';
  265:         if (($env{'form.state'} eq 'listing') && ($numtitles > 0)) {
  266:             $add_entries{'onLoad'} = 'setElements()';
  267:         }
  268:         &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles);
  269:         my $cat_maxdepth = $env{'form.catalog_maxdepth'};
  270:         $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
  271:                   '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'."\n".
  272:                   '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'."\n".
  273:                   '<input type="hidden" name="currcat_0" value="instcode::0" />'.
  274:                   &additional_filters($codedom));
  275:         if ($numtitles > 0) {
  276:             $r->print('<b>'.&mt('Choose which course(s) to list.').'</b><br />');
  277:             $r->print('<table><tr>');
  278:             for (my $k=0; $k<$lasttitle-1; $k++) {
  279:                 my (@items,@unsorted);
  280:                 if (ref($cat_items{$codetitles[$k]}) eq 'ARRAY') {
  281:                     @unsorted = @{$cat_items{$codetitles[$k]}};
  282:                 }
  283:                 &Apache::courseclassifier::sort_cats($k,\%cat_order,\@codetitles,\@unsorted,\@items);
  284:                 my @longitems;
  285:                 if (defined($cat_titles{$codetitles[$k]})) {
  286:                     foreach my $item (@items) {
  287:                         push(@longitems,$cat_titles{$codetitles[$k]}{$item});
  288:                     }
  289:                 } else {
  290:                     @longitems = @items;
  291:                 }
  292:                 $r->print('<td align="center">'.$codetitles[$k].'<br />'."\n".
  293:                           '<select name="'.$codetitles[$k].'" onChange="courseSet()"');
  294:                 $r->print('>'."\n".'<option value="0" />All'."\n");
  295:                 for (my $i=0; $i<@items; $i++) {
  296:                     if ($longitems[$i] eq '') {
  297:                         $longitems[$i] = $items[$i];
  298:                     }
  299:                     $r->print(' <option value="'.$items[$i].'">'.$longitems[$i].'</option>');
  300:                 }
  301:                 $r->print('</select></td>');
  302:             }
  303:             $r->print('<td align="center">'.$codetitles[$lasttitle-1].'<br />'."\n".
  304:                       '<select name="'.$codetitles[$lasttitle-1].'">'."\n".
  305:                       '<option value="0">All'."\n".
  306:                       '</option>'."\n".'</select>'."\n".
  307:                  '</td></tr></table>'."\n");
  308:             if ($numtitles > 4) {
  309:                 $r->print('<br /><br />'.$codetitles[$numtitles-1].'<br />'."\n".
  310:                 '<input type="text" name="'.$codetitles[$numtitles-1].'" /><br />'."\n");
  311:             }
  312:             $r->print('<br />');
  313:         }
  314:         $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
  315:                   '<input type="hidden" name="sortby" value="" />'."\n".
  316:                   '<input type="hidden" name="state" value="listing" />'."\n".
  317:                   '<input type="hidden" name="form.currcat_0" value="instcode::0" />'."\n".
  318:                   '<input type="submit" name="catalogfilter" value="'.
  319:                   &mt('Display courses').'" />'.
  320:                   '<input type="hidden" name="numtitles" value="'.$numtitles.
  321:                   '" /></form><br /><br />');
  322:     } else {
  323:         &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles);
  324:         my $cat_maxdepth = $env{'form.catalog_maxdepth'};
  325:         $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
  326:                   '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'.
  327:                   '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'.
  328:                   '<input type="hidden" name="currcat_0" value="instcode::0" />');
  329:         $r->print('<br />'.&mt('No official courses to display for [_1].',$domdesc).'</form>');
  330:     }
  331:     return $numtitles;
  332: }
  333: 
  334: sub cat_header {
  335:     my ($r,$codedom,$js,$add_entries,$catlinks,$numtitles) = @_;
  336:     my $start_page =
  337:         &Apache::loncommon::start_page('Course Catalog',$js,
  338:                                        {
  339:                                          'add_entries' => $add_entries,
  340:                                          'no_inline_link'   => 1,});
  341:     $r->print($start_page);
  342:     if ($env{'form.state'} eq 'listing') {
  343:         if ($numtitles > 0) {
  344:             &Apache::lonhtmlcommon::add_breadcrumb
  345:                 ({href=>"/adm/coursecatalog",
  346:                   text=>"Select courses"},
  347:                  {text=>"Course listing"});
  348:         } else {
  349:             &Apache::lonhtmlcommon::add_breadcrumb
  350:             ({text=>"Course listing"});
  351:         }
  352:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course Listing'));
  353:     } else {
  354:         &Apache::lonhtmlcommon::add_breadcrumb
  355:         ({href=>"/adm/coursecatalog",
  356:           text=>"Select courses"});
  357:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('Select courses'));
  358:     }
  359:     $r->print('<form name="coursecatdom" method="post" action="/adm/coursecatalog">'.
  360:               '<table border="0"><tr><td><b>'.&mt('Domain:').'</b></td><td>'.
  361:               &Apache::loncommon::select_dom_form($codedom,'showdom','',1).
  362: 	      '&nbsp;<input type="submit" name="godom" value="'.&mt('Change').'" /></td></tr></table></form>'.
  363: 	      '<form name="coursecats" method="post" action="/adm/coursecatalog">'.
  364:               '<table border="0"><tr>'.$catlinks.'</tr></table></form>');
  365:     return;
  366: }
  367: 
  368: sub category_breadcrumbs {
  369:     my ($dom,@cats) = @_;
  370:     my ($currdepth,$deeper) = &get_depth_values();
  371:     my $currcat_str = '<input type="hidden" name="catalog_maxdepth" value="'.$deeper.'" /><input type="hidden" name="showdom" value="'.$dom.'" />';
  372:     my $catlinks = '<td valign="top"><b>'.&mt('Catalog:').'</b></td><td><table><tr>';
  373:     my $has_subcats;
  374:     for (my $i=0; $i<$deeper; $i++) {
  375:         $currcat_str .= '<input type="hidden" name="currcat_'.$i.'" value="'.$env{'form.currcat_'.$i}.'" />';
  376:         my ($cattitle,$shallower);
  377:         if ($i == 0) {
  378:             $cattitle = &mt('Main Categories');
  379:         } else {
  380:             $shallower = $i-1;
  381:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
  382:             $cattitle = $cat;
  383:         }
  384:         $catlinks .= '<td valign="top"><a href="javascript:setCatDepth('."'$shallower'".')">'.$cattitle.'</a>-&gt;</td>';
  385:     }
  386:     if ($deeper == 0) {
  387:         $catlinks .= '<td>'.&mt('Main Categories').':&nbsp;';
  388:         if (ref($cats[0]) eq 'ARRAY') {
  389:             if ((@{$cats[0]} == 1) && ($cats[0][0] eq 'instcode')) {
  390:                 $catlinks .= &mt('Official courses (with institutional codes)').
  391:                              '<input type="hidden" name="currcat_0" value="instcode::0" />';
  392:                 $env{'form.currcat_0'} = 'instcode::0';
  393:             } else {
  394:                 $has_subcats = 1;
  395:                 $catlinks .= '<select name="currcat_0">'."\n";
  396:                 if (@{$cats[0]} > 1) {
  397:                     my $selstr;
  398:                     if ($env{'form.currcat_0'} eq '') {
  399:                         $selstr = ' selected="selected" ';
  400:                     }
  401:                     $catlinks .= '<option value=""  selected="selected">'.&mt('Select').'</option>'."\n";
  402:                 }
  403:                 for (my $i=0; $i<@{$cats[0]}; $i++) {
  404:                     my $name = $cats[0][$i];
  405:                     my $item = &escape($name).'::0';
  406:                     $catlinks .= '<option value="'.$item.'">';
  407:                     if ($name eq 'instcode') {
  408:                         $catlinks .= &mt('Official courses (with institutional codes)');
  409:                     } else {
  410:                         $catlinks .= $name;
  411:                     }
  412:                     $catlinks .= '</option>'."\n";
  413:                 }
  414:                 $catlinks .= '</select>'."\n".
  415:                              '&nbsp;<input type="submit" name="gocats" value="'.&mt('Change').'" />';
  416:             }
  417:         } else {
  418:             $catlinks .= &mt('Official courses (with institutional codes)').
  419:                          '<input type="hidden" name="currcat_0" value="instcode::0" />';
  420:             $env{'form.currcat_0'} = 'instcode::0';
  421:         }
  422:     } else {
  423:         my ($cat,$container,$depth);
  424:         if ($env{'form.currcat_'.$currdepth} eq '') {
  425:             my $shallower = $currdepth - 1;
  426:             ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
  427:         } else {
  428:             ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$currdepth});
  429:         }
  430:         my $deeper = $depth +1;
  431:         my $currcat = $cat;
  432:         if ($cat eq 'instcode') {
  433:             $currcat = &mt('Official courses (with institutional codes)');
  434:         }
  435:         $catlinks .= '<td>'.$currcat;
  436:         if (ref($cats[$deeper]{$cat}) eq 'ARRAY') {
  437:             $has_subcats = 1;
  438:             my $selstr;
  439:             $catlinks .= ':&nbsp;<select name="currcat_'.$deeper.'">'.
  440:                          '<option value="" selected="selected">'.
  441:                          &mt('Select').'</option>';
  442:             for (my $k=0; $k<@{$cats[$deeper]{$cat}}; $k++) {
  443:                 my $name = $cats[$deeper]{$cat}[$k];
  444:                 my $item = &escape($name).':'.&escape($cat).':'.$deeper;
  445:                 $catlinks .= '<option value="'.$item.'">'.$name.'</option>'."\n";
  446:             }
  447:             $catlinks .= '</select>'."\n".
  448:                          '&nbsp;<input type="submit" name="gocats" value="'.&mt('Change').'" />';
  449:         }
  450:     }
  451:     $catlinks .= $currcat_str.'</td></tr></table></td>';
  452:     return ($catlinks,$has_subcats);
  453: }
  454: 
  455: sub get_depth_values {
  456:     my $currdepth = 0;
  457:     my $deeper = 0;
  458:     if ($env{'form.catalog_maxdepth'} ne '') {
  459:         $currdepth = $env{'form.catalog_maxdepth'};
  460:         if ($env{'form.currcat_'.$currdepth} eq '') {
  461:             $deeper = $currdepth;
  462:         } else {
  463:             $deeper = $currdepth + 1;
  464:         }
  465:     }
  466:     return ($currdepth,$deeper);
  467: }
  468: 
  469: sub additional_filters {
  470:     my ($codedom,$has_subcats) = @_;
  471:     my $output = '<table>';
  472:     if (($env{'form.currcat_0'} ne 'instcode::0') && 
  473:         ($env{'form.currcat_0'} ne '') && ($has_subcats)) {
  474:         my $include_subcat_status;
  475:         if ($env{'form.withsubcats'}) {
  476:             $include_subcat_status = 'checked="checked" ';
  477:         }
  478:         my $counter = $env{'form.catalog_maxdepth'};
  479:         if ($counter > 0) {
  480:             if ($env{'form.state'} eq 'listing') {
  481:                 $counter --;
  482:             } elsif ($env{'form.currcat_'.$counter} eq '') {
  483:                 $counter --;
  484:             }
  485:         }
  486:         my ($catname) = split(/:/,$env{'form.currcat_'.$counter});
  487:         if ($catname ne '') {
  488:             $output .= '<tr><td><label>'.
  489:                        '<input type="checkbox" name="withsubcats" value="1" '.
  490:                        $include_subcat_status.'/>'.
  491:                        &mt('Include subcategories within "[_1]"',
  492:                            &unescape($catname)).'</label></td></tr>';
  493:         }
  494:     }
  495:     my $show_selfenroll_status;
  496:     if ($env{'form.showselfenroll'}) {
  497:         $show_selfenroll_status = 'checked="checked" ';
  498:     }
  499:     $output .= '<tr><td>'.
  500:                '<label><input type="checkbox" name="showselfenroll" value="1" '.
  501:                $show_selfenroll_status.'/>'.
  502:                &mt('Only show courses which allow self-enrollment').
  503:                '</label></td></tr>';
  504:     if (&user_is_dc($codedom)) {
  505:         my $showdetails_status;
  506:         if ($env{'form.showdetails'}) {
  507:             $showdetails_status = 'checked="checked" ';
  508:         }
  509:         my $showhidden_status;
  510:         if ($env{'form.showhidden'}) {
  511:              $showhidden_status = 'checked="checked" ';
  512:         }
  513:         my $dc_title = &Apache::lonnet::plaintext('dc');
  514:         $output .= '<tr><td>'."\n".
  515:                    '<label><input type="checkbox" name="showdetails" value="1" '.
  516:                    $showdetails_status.'/>'.
  517:                    &mt('Show full details for each course ([_1] only)',$dc_title).
  518:                    '</label>'."\n".'</td></tr><tr><td>'.
  519:                    '<label><input type="checkbox" name="showhidden" value="1" '.
  520:                    $showhidden_status.'/>'.
  521:                    &mt('Include courses set to be hidden from catalog ([_1] only)',$dc_title).
  522:                    '</label>'."\n".'</td></tr>';
  523:     }
  524:     $output .= '</table><br />';
  525:     return $output;
  526: }
  527: 
  528: sub user_is_dc {
  529:     my ($codedom) = @_;
  530:     if (exists($env{'user.role.dc./'.$codedom.'/'})) {
  531:         my $livedc = 1;
  532:         my $now = time;
  533:         my ($start,$end)=split(/\./,$env{'user.role.dc./'.$codedom.'/'});
  534:         if ($start && $start>$now) { $livedc = 0; }
  535:         if ($end   && $end  <$now) { $livedc = 0; }
  536:         return $livedc;
  537:     }
  538:     return;
  539: }
  540: 
  541: sub recurse_options {
  542:     my ($currkey,$currlist,$level,$cat,$cat_options,$data,$by_year,$by_sem,$by_dept) = @_;
  543:     if (ref($currlist) eq 'HASH') {
  544:         $level ++;
  545:         foreach my $key (sort(keys(%{$currlist}))) {
  546:             $$data[$level-1]= $key;
  547:             &recurse_options($key,$currlist->{$key},$level,$cat,$cat_options,$data,$by_year,$by_sem,$by_dept);
  548:         }
  549:     } else {
  550:         $level --;
  551:         my @contents = split(/","/,$currlist);
  552:         foreach my $item (@contents) {
  553:             if (!grep(/^\Q$item\E$/,@{$cat_options->{$cat}})) {
  554:                 push(@{$cat_options->{$cat}},$item);
  555:             }
  556:             if ($level == 3) {
  557:                 if (!grep/^\Q$item\E$/,@{$by_year->{$data->[1]}->{$currkey}}) {
  558:                     push(@{$by_year->{$data->[1]}->{$currkey}},$item);                 
  559:                 }
  560:                 if (!grep/^\Q$item\E$/,@{$by_sem->{$data->[2]}->{$currkey}}) {
  561:                     push(@{$by_sem->{$data->[2]}->{$currkey}},$item);
  562:                 }
  563:                 if (!grep/^\Q$item\E$/,@{$by_dept->{$currkey}}) {
  564:                     push(@{$by_dept->{$currkey}},$item);
  565:                 }
  566: 
  567:             }
  568:         }
  569:     }
  570:     return $level;
  571: }
  572: 
  573: sub build_javascript {
  574:     my ($by_year,$by_sem,$by_dept,$cat_order,$codetitles) = @_;
  575:     my @unsorted = keys(%{$by_year});
  576:     my @sorted_yrs; 
  577:     &Apache::courseclassifier::sort_cats('0',$cat_order,$codetitles,\@unsorted,\@sorted_yrs);
  578:     my $output = 'var idcse_by_yr_year = new Array("'.join('","',@sorted_yrs).'");'."\n".
  579:                  'var idcse_by_yr_dept = new Array('.scalar(@sorted_yrs).');'."\n".
  580:                  'var idcse_by_yr_num = new Array('.scalar(@sorted_yrs).');'."\n";
  581:     for (my $i=0; $i<@sorted_yrs; $i++) {
  582:         my $numkeys = keys(%{$by_year->{$sorted_yrs[$i]}});
  583:         $output .= " idcse_by_yr_num[$i] = new Array($numkeys);\n";
  584:         if (ref($by_year->{$sorted_yrs[$i]}) eq 'HASH') {
  585:             @unsorted = keys(%{$by_year->{$sorted_yrs[$i]}});
  586:             my @sorted_depts;
  587:             &Apache::courseclassifier::sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_depts);
  588:             $output .= qq| idcse_by_yr_dept[$i] = new Array ("|.join('","',@sorted_depts).'");'."\n";
  589:             for (my $j=0; $j<@sorted_depts; $j++) {
  590:                 $output .= qq| idcse_by_yr_num[$i][$j] = new Array ("|;
  591:                 $output .= join('","',sort(@{$by_year->{$sorted_yrs[$i]}->{$sorted_depts[$j]}})).'");'."\n";
  592:             }
  593:         }
  594:     }
  595:     @unsorted = keys(%{$by_sem});
  596:     my @sorted_sems;
  597:     &Apache::courseclassifier::sort_cats('1',$cat_order,$codetitles,\@unsorted,\@sorted_sems);
  598:     $output .=  'idcse_by_sem_sems = new Array("'.join('","',@sorted_sems).'");'."\n".
  599:                 'idcse_by_sem_dept = new Array('.scalar(@sorted_sems).');'."\n".
  600:                 'idcse_by_sem_num = new Array('.scalar(@sorted_sems).');'."\n";
  601:     for (my $i=0; $i<@sorted_sems; $i++) {
  602:         my $numkeys = keys(%{$by_sem->{$sorted_sems[$i]}});
  603:         $output .= " idcse_by_sem_num[$i] = new Array($numkeys);\n";
  604:         if (ref($by_sem->{$sorted_sems[$i]}) eq 'HASH') {
  605:             @unsorted = keys(%{$by_sem->{$sorted_sems[$i]}});
  606:             my @sorted_depts;
  607:             &Apache::courseclassifier::sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_depts);
  608:             $output .= qq| idcse_by_sem_dept[$i] = new Array("|.join('","',@sorted_depts).'");'."\n";
  609:             for (my $j=0; $j<@sorted_depts; $j++) {
  610:                 $output .= qq| idcse_by_sem_num[$i][$j] = new Array ("|.join('","',sort(@{$by_sem->{$sorted_sems[$i]}->{$sorted_depts[$j]}})).'");'."\n";
  611:             }
  612:         }
  613:     }
  614:     @unsorted = keys(%{$by_dept});
  615:     my @sorted_deps;
  616:     &Apache::courseclassifier::sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_deps);
  617:     $output .= 'idcse_by_dep = new Array('.scalar(@sorted_deps).');'."\n"; 
  618:     for (my $k=0; $k<@sorted_deps; $k++) {
  619:         $output .= qq| idcse_by_dep[$k] = new Array ("|.join('","',sort(@{$by_dept->{$sorted_deps[$k]}})).'");'."\n";
  620:     }
  621:     return $output;
  622: }
  623: 
  624: sub search_official_courselist {
  625:     my ($domain,$numtitles) = @_;
  626:     my $instcode;
  627:     if (defined($numtitles) && $numtitles == 0) {
  628:         $instcode = '.+';
  629:     } else {
  630:         my (%codedefaults,@code_order);
  631:         my $defaults_result = 
  632:             &Apache::lonnet::auto_instcode_defaults($domain,\%codedefaults,
  633:                                                     \@code_order);
  634:         if ($defaults_result eq 'ok') {
  635:             $instcode ='^';
  636:             foreach my $item (@code_order) {
  637:                 if ($env{'form.'.$item} eq '0' ) {
  638:                     $instcode .= $codedefaults{$item}; 
  639:                 } else {
  640:                     $instcode .= $env{'form.'.$item};
  641:                 }
  642:             }
  643:             $instcode .= '$';
  644:         } else {
  645:             $instcode = '.';
  646:         }
  647:     }
  648:     my $showhidden;
  649:     if (&user_is_dc($domain)) {
  650:         $showhidden = $env{'form.showhidden'};
  651:     }
  652:     my %courses = 
  653:         &Apache::lonnet::courseiddump($domain,'.',1,$instcode,'.','.',undef,undef,
  654:                                       'Course',1,$env{'form.showselfenroll'},undef,
  655:                                       $showhidden,'coursecatalog');
  656:     return %courses;
  657: }
  658: 
  659: sub search_courselist {
  660:     my ($domain,$subcats) = @_;
  661:     my $cat_maxdepth = $env{'form.catalog_maxdepth'};
  662:     my $filter = $env{'form.currcat_'.$cat_maxdepth};
  663:     if (($filter eq '') && ($cat_maxdepth > 0)) {
  664:         my $shallower = $cat_maxdepth - 1;
  665:         $filter = $env{'form.currcat_'.$shallower};
  666:     }
  667:     my %courses;
  668:     my $filterstr;
  669:     if ($filter ne '') {
  670:         if ($env{'form.withsubcats'}) {
  671:             if (ref($subcats) eq 'HASH') {
  672:                 if (ref($subcats->{$filter}) eq 'ARRAY') {
  673:                     $filterstr = join('&',@{$subcats->{$filter}});
  674:                     if ($filterstr ne '') {
  675:                         $filterstr = $filter.'&'.$filterstr;
  676:                     }
  677:                 } else {
  678:                     $filterstr = $filter;
  679:                 }
  680:             } else {
  681:                 $filterstr = $filter;
  682:             }  
  683:         } else {
  684:             $filterstr = $filter; 
  685:         }
  686:         my $showhidden;
  687:         if (&user_is_dc($domain)) {
  688:             $showhidden = $env{'form.showhidden'};
  689:         }
  690:         %courses = 
  691:             &Apache::lonnet::courseiddump($domain,'.',1,'.','.','.',undef,undef,
  692:                                           '.',1,$env{'form.showselfenroll'},
  693:                                           $filterstr,$showhidden,'coursecatalog');
  694:     }
  695:     return %courses;
  696: }
  697: 
  698: sub print_course_listing {
  699:     my ($domain,$numtitles,$trails,$allitems,$subcats) = @_;
  700:     my $output;
  701:     my %courses;
  702:     my $knownuser = &user_is_known();
  703:     my $details = $env{'form.coursenum'};
  704:     if (&user_is_dc($domain)) {
  705:         if ($env{'form.showdetails'}) {
  706:             $details = 1;
  707:         }
  708:     }
  709:     if ($env{'form.coursenum'} ne '') {
  710:         %courses = &Apache::lonnet::courseiddump($domain,'.',1,'.','.',
  711:                                                  $env{'form.coursenum'},
  712:                                                  undef,undef,'.',1);
  713:         if (keys(%courses) == 0) {
  714:             $output .= &mt('The courseID provided does not match a course in this domain.');
  715:             return $output;
  716:         }
  717:     } else {
  718:         if ($env{'form.currcat_0'} eq 'instcode::0') {
  719:             %courses = &search_official_courselist($domain,$numtitles);
  720:         } else {
  721:             %courses = &search_courselist($domain,$subcats);
  722:         }
  723:         if (keys(%courses) == 0) {
  724:             $output = &mt('No courses match the criteria you selected.');
  725:             return $output;
  726:         }
  727:         if (($knownuser) && (!$env{'form.showdetails'}) && (!&user_is_dc($domain))) {
  728:             $output = '<b>'.&mt('Note for students:').'</b> '
  729:                      .&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.')
  730:                      .'<br /><br />';
  731:         }
  732:     }
  733:     my $now = time;
  734:     my %domconfig =
  735:         &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
  736:     $output .= &construct_data_table($knownuser,\%courses,$details,undef,$now,\%domconfig,$trails,$allitems);
  737:     if ($env{'form.coursenum'} ne '') {
  738:         $output .= &Apache::lonhtmlcommon::echo_form_input(['coursenum','state','catalogfilter','sortby','showdetails']);
  739:     }
  740:     return $output;
  741: }
  742: 
  743: sub construct_data_table {
  744:     my ($knownuser,$courses,$details,$usersections,$now,$domconfig,$trails,
  745:         $allitems) = @_;
  746:     my %sortname;
  747:     if (($details eq '') || ($env{'form.showdetails'})) {
  748:         $sortname{'Code'} = 'code';
  749:         $sortname{'Categories'} = 'cats';
  750:         $sortname{'Title'} = 'title';
  751:         $sortname{'Owner(s)'} = 'owner';
  752:     }
  753:     my $output = &Apache::loncommon::start_data_table().
  754:                  &Apache::loncommon::start_data_table_header_row();
  755:     my @coltitles = ('Count');
  756:     if ($env{'form.currcat_0'} eq 'instcode::0') {
  757:         push(@coltitles,'Code');
  758:     } else {
  759:         push(@coltitles,'Categories');
  760:     }
  761:     push(@coltitles,('Sections','Crosslisted','Title','Owner(s)'));
  762:     if (ref($usersections) eq 'HASH') {
  763:        $coltitles[1] = 'Your Section';
  764:     }
  765:     foreach my $item (@coltitles) {
  766:         $output .= '<th>';
  767:         if (defined($sortname{$item})) {
  768:             $output .= '<a href="javascript:changeSort('."'$sortname{$item}'".')">'.&mt($item).'</a>';
  769:         } elsif ($item eq 'Count') {
  770:             $output .= '&nbsp;&nbsp;';
  771:         } else {
  772:             $output .= &mt($item);
  773:         }
  774:         $output .= '</th>';
  775:     }
  776:     if ($knownuser) {
  777:         if ($details) {
  778:             $output .=
  779:               '<th>'.&mt('Default Access Dates for Students').'</th>'.
  780:               '<th>'.&mt('Student Counts').'</th>'.
  781:               '<th>'.&mt('Auto-enrollment of <br />registered students').'</th>';
  782:         } else {
  783:             $output .= '<th>'.&mt('Details').'</th>';
  784:         }
  785:     }
  786:     $output .= '<th>'.&mt('Self-enroll (if permitted)').'</th>';
  787:     &Apache::loncommon::end_data_table_header_row();
  788:     my %courseinfo = &build_courseinfo_hash($courses,$knownuser,$details,
  789:                                             $usersections);
  790:     my %Sortby;
  791:     foreach my $course (sort(keys(%{$courses}))) {
  792:         if ($env{'form.sortby'} eq 'code') {
  793:             push(@{$Sortby{$courseinfo{$course}{'code'}}},$course);
  794:         } elsif ($env{'form.sortby'} eq 'cats') {
  795:             push(@{$Sortby{$courseinfo{$course}{'categories'}}},$course);
  796:         } elsif ($env{'form.sortby'} eq 'owner') {
  797:             push(@{$Sortby{$courseinfo{$course}{'ownerlastnames'}}},$course);
  798:         } else {
  799:             my $clean_title = $courseinfo{$course}{'title'};
  800:             $clean_title =~ s/\W+//g;
  801:             if ($clean_title eq '') {
  802:                 $clean_title = $courseinfo{$course}{'title'};
  803:             }
  804:             push(@{$Sortby{$clean_title}},$course);
  805:         }
  806:     }
  807:     my @sorted_courses;
  808:     if (($env{'form.sortby'} eq 'code') || ($env{'form.sortby'} eq 'owner') ||
  809:         ($env{'form.sortby'} eq 'cats')) {
  810:         @sorted_courses = sort(keys(%Sortby));
  811:     } else {
  812:         @sorted_courses = sort { lc($a) cmp lc($b) } (keys(%Sortby));
  813:     }
  814:     my $count = 1;
  815:     foreach my $item (@sorted_courses) {
  816:         foreach my $course (@{$Sortby{$item}}) {
  817:             $output.=&Apache::loncommon::start_data_table_row(); 
  818:             $output.=&courseinfo_row($courseinfo{$course},$knownuser,$details,
  819:                                      \$count,$now,$course,$trails,$allitems);
  820:             $output.=&Apache::loncommon::end_data_table_row();
  821:         }
  822:     }
  823:     $output .= &Apache::loncommon::end_data_table();
  824:     return $output;
  825: }
  826: 
  827: sub build_courseinfo_hash {
  828:     my ($courses,$knownuser,$details,$usersections) = @_;
  829:     my %courseinfo;
  830:     my $now = time;
  831:     foreach my $course (keys(%{$courses})) {
  832:         my $descr;
  833:         if (ref($courses->{$course}) eq 'HASH') {
  834:             $descr = $courses->{$course}{'description'}; 
  835:         }
  836:         my $cleandesc=&HTML::Entities::encode($descr,'<>&"');
  837:         $cleandesc=~s/'/\\'/g;
  838:         $cleandesc =~ s/^\s+//;
  839:         my ($cdom,$cnum)=split(/\_/,$course);
  840:         my ($descr,$instcode,$singleowner,$ttype,$selfenroll_types,
  841:             $selfenroll_start,$selfenroll_end,@owners,%ownernames,$categories);
  842:         if (ref($courses->{$course}) eq 'HASH') {
  843:             $descr = $courses->{$course}{'description'};
  844:             $instcode =  $courses->{$course}{'inst_code'};
  845:             $singleowner = $courses->{$course}{'owner'};
  846:             $ttype =  $courses->{$course}{'type'};
  847:             $selfenroll_types = $courses->{$course}{'selfenroll_types'};
  848:             $selfenroll_start = $courses->{$course}{'selfenroll_start_date'};
  849:             $selfenroll_end = $courses->{$course}{'selfenroll_end_date'};
  850:             $categories = $courses->{$course}{'categories'};
  851:             push(@owners,$singleowner);
  852:             if (ref($courses->{$course}{'co-owners'}) eq 'ARRAY') {
  853:                 foreach my $item (@{$courses->{$course}{'co-owners'}}) {
  854:                     push(@owners,$item);
  855:                 }
  856:             }
  857:         }
  858:         foreach my $owner (@owners) {
  859:             my ($ownername,$ownerdom) = @_; 
  860:             if ($owner =~ /:/) {
  861:                 ($ownername,$ownerdom) = split(/:/,$owner);
  862:             } else {
  863:                 $ownername = $owner;
  864:                 if ($owner ne '') {
  865:                     $ownerdom = $cdom;
  866:                 }
  867:             }
  868:             if ($ownername ne '' && $ownerdom ne '') {
  869:                 my %namehash=&Apache::loncommon::getnames($ownername,$ownerdom);
  870:                 $ownernames{$ownername.':'.$ownerdom} = \%namehash; 
  871:             }
  872:         }
  873:         $courseinfo{$course}{'cdom'} = $cdom;
  874:         $courseinfo{$course}{'cnum'} = $cnum;
  875:         $courseinfo{$course}{'code'} = $instcode;
  876:         my @lastnames;
  877:         foreach my $owner (keys(%ownernames)) {
  878:             if (ref($ownernames{$owner}) eq 'HASH') {
  879:                 push(@lastnames,$ownernames{$owner}{'lastname'});
  880:             }
  881:         }
  882:         $courseinfo{$course}{'ownerlastnames'} = join(', ',sort(@lastnames));
  883:         $courseinfo{$course}{'title'} = $cleandesc;
  884:         $courseinfo{$course}{'owner'} = $singleowner;
  885:         $courseinfo{$course}{'selfenroll_types'} = $selfenroll_types;
  886:         $courseinfo{$course}{'selfenroll_start'} = $selfenroll_start;
  887:         $courseinfo{$course}{'selfenroll_end'} = $selfenroll_end;
  888:         $courseinfo{$course}{'categories'} = $categories;
  889: 
  890:         my %coursehash = &Apache::lonnet::dump('environment',$cdom,$cnum);
  891:         my @classids;
  892:         my @crosslistings;
  893:         my ($seclist,$numsec) = 
  894:             &identify_sections($coursehash{'internal.sectionnums'});
  895:         if (ref($usersections) eq 'HASH') {
  896:             if (ref($usersections->{$course}) eq 'ARRAY') {
  897:                 $seclist = join(', ',@{$usersections->{$course}});
  898:             }
  899:         }
  900:         $courseinfo{$course}{'seclist'} = $seclist;
  901:         my ($xlist_items,$numxlist) = 
  902:             &identify_sections($coursehash{'internal.crosslistings'});
  903:         my $showsyllabus = 1; # default is to include a syllabus link
  904:         if (defined($coursehash{'showsyllabus'})) {
  905:             $showsyllabus = $coursehash{'showsyllabus'};
  906:         }
  907:         $courseinfo{$course}{'showsyllabus'} = $showsyllabus;
  908:         if (((defined($env{'form.coursenum'}) && ($cnum eq $env{'form.coursenum'}))) ||
  909:             ($knownuser && ($details == 1))) {
  910:             $courseinfo{$course}{'counts'} =  &count_students($cdom,$cnum,$numsec);
  911:             $courseinfo{$course}{'autoenrollment'} =
  912:                 &autoenroll_info(\%coursehash,$now,$seclist,$xlist_items,
  913:                                  $instcode,\@owners,$cdom,$cnum);
  914: 
  915:             my $startaccess = '';
  916:             my $endaccess = '';
  917:             my $accessdates;
  918:             if ( defined($coursehash{'default_enrollment_start_date'}) ) {
  919:                 $startaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_start_date'});
  920:             }
  921:             if ( defined($coursehash{'default_enrollment_end_date'}) ) {
  922:                 $endaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_end_date'});
  923:                 if ($coursehash{'default_enrollment_end_date'} == 0) {
  924:                     $endaccess = &mt('No ending date');
  925:                 }
  926:             }
  927:             if ($startaccess) {
  928:                 $accessdates .= &mt('<i>From:</i> ').$startaccess.'<br />';
  929:             }
  930:             if ($endaccess) {
  931:                 $accessdates .= &mt('<i>To:</i> ').$endaccess.'<br />';
  932:             }
  933:             if (($selfenroll_types ne '') && 
  934:                 ($selfenroll_end > 0 && $selfenroll_end > $now)) {
  935:                 my ($selfenroll_start_access,$selfenroll_end_access);
  936:                 if (($coursehash{'default_enrollment_start_date'} ne 
  937:                      $coursehash{'internal.selfenroll_start_access'}) ||
  938:                    ($coursehash{'default_enrollment_end_date'} ne 
  939:                     $coursehash{'internal.selfenroll_end_access'})) {
  940:                     if ( defined($coursehash{'internal.selfenroll_start_access'}) ) {
  941:                         $selfenroll_start_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_start_access'});
  942:                     }
  943:                     if ( defined($coursehash{'default_enrollment_end_date'}) ) {
  944:                         $selfenroll_end_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_end_access'});
  945:                         if ($coursehash{'internal.selfenroll_end_access'} == 0) {
  946:                             $selfenroll_end_access = &mt('No ending date');
  947:                         }
  948:                     }
  949:                     if ($selfenroll_start_access || $selfenroll_end_access) {
  950:                         $accessdates .= '<br/><br /><i>'.&mt('Self-enrollers:').'</i><br />';
  951:                         if ($selfenroll_start_access) {
  952:                             $accessdates .= &mt('<i>From:</i> ').$selfenroll_start_access.'<br />';
  953:                         }
  954:                         if ($selfenroll_end_access) {
  955:                             $accessdates .= &mt('<i>To:</i> ').$selfenroll_end_access.'<br />';
  956:                         }
  957:                     }
  958:                 }
  959:             }
  960:             $courseinfo{$course}{'access'} = $accessdates;
  961:         }
  962:         if ($xlist_items eq '') {
  963:             $xlist_items = &mt('No');
  964:         }
  965:         $courseinfo{$course}{'xlist'} = $xlist_items;
  966:     }
  967:     return %courseinfo;
  968: }
  969: 
  970: sub count_students {
  971:     my ($cdom,$cnum,$numsec) = @_;
  972:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
  973:     my %student_count = (
  974:                            Active => 0,
  975:                            Future => 0,
  976:                            Expired => 0,
  977:                        );
  978:     my %idx;
  979:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
  980:     my %status_title = &Apache::lonlocal::texthash(
  981:                            Expired => 'Previous access',
  982:                            Active => 'Current access',
  983:                            Future => 'Future access',
  984:                        );
  985: 
  986:     while (my ($student,$data) = each(%$classlist)) {
  987:         $student_count{$data->[$idx{'status'}]} ++;
  988:     }
  989: 
  990:     my $countslist = &mt('[quant,_1,section]',$numsec).':<br />';
  991:     foreach my $status ('Active','Future') {
  992:         $countslist .= '<nobr>'.$status_title{$status}.': '.
  993:                        $student_count{$status}.'</nobr><br />';
  994:     }
  995:     return $countslist;
  996: }
  997: 
  998: sub courseinfo_row {
  999:     my ($info,$knownuser,$details,$countref,$now,$course,$trails,$allitems) = @_;
 1000:     my ($cdom,$cnum,$title,$ownerlast,$code,$owner,$seclist,$xlist_items,
 1001:         $accessdates,$showsyllabus,$counts,$autoenrollment,$output,$categories);
 1002:     if (ref($info) eq 'HASH') {
 1003:         $cdom = $info->{'cdom'};
 1004:         $cnum = $info->{'cnum'};
 1005:         $title = $info->{'title'};
 1006:         $ownerlast = $info->{'ownerlastnames'};
 1007:         $code = $info->{'code'};
 1008:         $owner = $info->{'owner'};
 1009:         $seclist = $info->{'seclist'};
 1010:         $xlist_items = $info->{'xlist'};
 1011:         $accessdates = $info->{'access'};
 1012:         $counts = $info->{'counts'};
 1013:         $autoenrollment = $info->{'autoenrollment'};
 1014:         $showsyllabus = $info->{'showsyllabus'};
 1015:         $categories = $info->{'categories'};
 1016:     } else {
 1017:         $output = '<td colspan="8">'.&mt('No information available for [_1].',
 1018:                                          $code).'</td>';
 1019:         return $output;
 1020:     }
 1021:     $output .= '<td>'.$$countref.'</td>';
 1022:     if ($env{'form.currcat_0'} eq 'instcode::0') {
 1023:         $output .= '<td>'.$code.'</td>';
 1024:     } else {
 1025:         my ($categorylist,@cats);
 1026:         if ($categories ne '') {
 1027:             @cats = split('&',$categories);
 1028:         }
 1029:         if ((ref($trails) eq 'ARRAY') && (ref($allitems) eq 'HASH')) {
 1030:             my @categories = map { $trails->[$allitems->{$_}]; } @cats;
 1031:             $categorylist = join('<br />',@categories);
 1032:         }
 1033:         if ($categorylist eq '') {
 1034:             $categorylist = '&nbsp;';
 1035:         }
 1036:         $output .= '<td>'.$categorylist.'</td>';
 1037:     }
 1038:     $output .= '<td>'.$seclist.'</td>'.
 1039:                '<td>'.$xlist_items.'</td>'.
 1040:                '<td>'.$title.'&nbsp;<font size="-2">';
 1041:     if ($showsyllabus) {
 1042:         $output .= &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$cnum,$cdom);
 1043:     } else {
 1044:         $output .= '&nbsp;';
 1045:     }
 1046:     $output .= '</font></td>'.
 1047:                '<td>'.$ownerlast.'</td>';
 1048:     if ($knownuser) {
 1049:         if ($details) {
 1050:             $output .=
 1051:                '<td>'.$accessdates.'</td>'. 
 1052:                '<td>'.$counts.'</td>'.
 1053:                '<td>'.$autoenrollment.'</td>';
 1054:         } else {
 1055:             $output .= "<td><a href=\"javascript:setCourseId('$cnum')\">".&mt('Show more details').'</a></td>';
 1056:         }
 1057:     }
 1058:     my $selfenroll;
 1059:     if ($info->{'selfenroll_types'}) {
 1060:         my $showstart = &Apache::lonlocal::locallocaltime($info->{'selfenroll_start'});
 1061:         my $showend = &Apache::lonlocal::locallocaltime($info->{'selfenroll_end'});
 1062:         if (($info->{'selfenroll_end'} > 0) && ($info->{'selfenroll_end'} > $now)) {
 1063:             if (($info->{'selfenroll_start'} > 0) && ($info->{'selfenroll_start'} > $now)) {
 1064:                 $output .= '<td>'.&mt('Starts: [_1]','<span class="LC_cusr_emph">'.$showstart.'</span>').'<br />'.&mt('Ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>').'</td>';
 1065:             } else { 
 1066:                 $output .= '<td><a href="/adm/selfenroll?cid='.$course.'">'.&mt('Enroll in course').'</a></td>';
 1067:             }
 1068:             $selfenroll = 1;
 1069:         }
 1070:     }
 1071:     if (!$selfenroll) {
 1072:         $output .= '<td>&nbsp;</td>';
 1073:     }
 1074:     $$countref ++;
 1075:     return $output;
 1076: }
 1077: 
 1078: sub identify_sections {
 1079:     my ($seclist) = @_;
 1080:     my @secnums;
 1081:     if ($seclist =~ /,/) {
 1082:         my @sections = split(/,/,$seclist);
 1083:         foreach my $sec (@sections) {
 1084:             $sec =~ s/:[^:]*$//;
 1085:             push(@secnums,$sec);
 1086:         }
 1087:     } else {
 1088:         if ($seclist =~ m/^([^:]+):/) {
 1089:             my $sec = $1;
 1090:             if (!grep(/^\Q$sec\E$/,@secnums)) {
 1091:                 push(@secnums,$sec);
 1092:             }
 1093:         }
 1094:     }
 1095:     @secnums = sort {$a <=> $b} @secnums;
 1096:     my $seclist = join(', ',@secnums);
 1097:     my $numsec = @secnums;
 1098:     return ($seclist,$numsec);
 1099: }
 1100: 
 1101: sub get_valid_classes {
 1102:     my ($seclist,$xlist_items,$crscode,$owners,$cdom,$cnum) = @_;
 1103:     my $response;
 1104:     my %validations;
 1105:     @{$validations{'sections'}} = ();
 1106:     @{$validations{'xlists'}} = ();
 1107:     my $totalitems = 0;
 1108:     if ($seclist) {
 1109:         foreach my $sec (split(/, /,$seclist)) {
 1110:             my $class = $crscode.$sec;
 1111:             if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
 1112: 							 $class) eq 'ok') {
 1113:                 if (!grep(/^\Q$sec$\E/,@{$validations{'sections'}})) {
 1114:                     push(@{$validations{'sections'}},$sec);
 1115:                     $totalitems ++;
 1116:                 }
 1117:             }
 1118:         }
 1119:     }
 1120:     if ($xlist_items) {
 1121:         foreach my $item (split(/, /,$xlist_items)) {
 1122:             if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
 1123: 							 $item) eq 'ok') {
 1124:                 if (!grep(/^\Q$item$\E/,@{$validations{'xlists'}})) {
 1125:                     push(@{$validations{'xlists'}},$item);
 1126:                     $totalitems ++;
 1127:                 }
 1128:             }
 1129:         }
 1130:     }
 1131:     if ($totalitems > 0) {
 1132:         if (@{$validations{'sections'}}) {
 1133:             $response = &mt('Sections: ').
 1134:                         join(', ',@{$validations{'sections'}}).'<br />';
 1135:         }
 1136:         if (@{$validations{'xlists'}}) {
 1137:             $response .= &mt('Courses: ').
 1138:                         join(', ',@{$validations{'xlists'}});
 1139:         }
 1140:     }
 1141:     return $response;
 1142: }
 1143: 
 1144: sub javascript_select_filler {
 1145:     my ($formname,$scripttext,$codetitles,$longtitles_str,$allidlist) = @_;
 1146:     my $output = <<END;
 1147: function courseSet() {
 1148:     var longtitles = new Array ("$longtitles_str");
 1149:     var valyr = document.$formname.Year.options[document.$formname.Year.selectedIndex].value
 1150:     var valsem  = document.$formname.Semester.options[document.$formname.Semester.selectedIndex].value
 1151:     var valdept = document.$formname.Department.options[document.$formname.Department.selectedIndex].value
 1152:     var valclass = document.$formname.Number.options[document.$formname.Number.selectedIndex].value
 1153:     var idyears = new Array("$allidlist");
 1154:     var idyr = -1;
 1155:     var idsem = -1;
 1156:     var iddept = -1;
 1157:     document.$formname.Number.length = 0;
 1158: 
 1159:     $scripttext
 1160: 
 1161:     selYear = document.$formname.Year.selectedIndex-1;
 1162:     selSemester = document.$formname.Semester.selectedIndex-1;
 1163:     selDepartment = document.$formname.Department.selectedIndex-1;
 1164:     if (selYear == -1) {
 1165:         if (selSemester == -1) {
 1166:             if (selDepartment > -1) {
 1167:                 document.$formname.Number.options[0] =  new Option('All','0',false,false);
 1168:                 for (var k=0; k<idcse_by_dep[selDepartment].length; k++) {
 1169:                     document.$formname.Number.options[k+1] = new Option(idcse_by_dep[selDepartment][k],idcse_by_dep[selDepartment][k],false,false);
 1170: 
 1171:                 }
 1172:             } 
 1173:             else {
 1174:                 document.$formname.Number.options[0] = new Option("All","0",true,true);
 1175:             }
 1176:         }
 1177:         else {
 1178:             if (selDepartment > -1) {
 1179:                 for (var i=0; i<idcse_by_sem_sems.length; i++) {
 1180:                     if (idcse_by_sem_sems[i] == valsem) {
 1181:                         idsem = i;
 1182:                     }
 1183:                 }
 1184:                 if (idsem != -1) {
 1185:                     for (var i=0; i<idcse_by_sem_dept[idsem].length; i++) {
 1186:                         if (idcse_by_sem_dept[idsem][i] == valdept) {
 1187:                             iddept = i;
 1188:                         }
 1189:                     }
 1190:                 }
 1191:                 if (iddept != -1) {
 1192:                     document.$formname.Number.options[0] =  new Option('All','0',false,false);
 1193:                     for (var k=0; k<idcse_by_sem_num[idsem][iddept].length; k++) {
 1194:                         document.$formname.Number.options[k+1] = new Option(idcse_by_sem_num[idsem][iddept][k],idcse_by_sem_num[idsem][iddept][k],false,false);
 1195:                     }
 1196:                 }
 1197:                 else {
 1198:                     document.$formname.Number.options[0] =  new Option('No courses','0',true,true);
 1199:                 }
 1200:             }
 1201:             else {
 1202:                 document.$formname.Number.options[0] = new Option("All","0",true,true);
 1203:             }
 1204:         }
 1205:     }
 1206:     else {
 1207:         if (selSemester == -1) {
 1208:             if (selDepartment > -1) {
 1209:                 for (var i=0; i<idcse_by_yr_year.length; i++) {
 1210:                     if (idcse_by_yr_year[i] == valyr) {
 1211:                         idyr = i;
 1212:                     }
 1213:                 }
 1214:                 if (idyr != -1) {      
 1215:                     for (var i=0; i<idcse_by_yr_dept[idyr].length; i++) {
 1216:                         if (idcse_by_yr_dept[idyr][i] == valdept) {
 1217:                             iddept = i;
 1218:                         }
 1219:                     }
 1220:                 }
 1221:                 if (iddept != -1) {
 1222:                     document.$formname.Number.options[0] =  new Option('All','0',false,false);
 1223:                     for (var k=0; k<idcse_by_yr_num[idyr][iddept].length; k++) {
 1224:                         document.$formname.Number.options[k+1] = new Option(idcse_by_yr_num[idyr][iddept][k],idcse_by_yr_num[idyr][iddept][k],false,false);
 1225:                     }
 1226:                 } 
 1227:                 else {
 1228:                     document.$formname.Number.options[0] =  new Option('No courses','0',true,true);
 1229:                 }
 1230:             }
 1231:             else {
 1232:                 document.$formname.Number.options[0] = new Option("All","0",true,true);
 1233:             }
 1234:         }
 1235:         else {
 1236:             if (selDepartment > -1) {
 1237:                 for (var k=0; k<idyears.length; k++) {
 1238:                     if (idyears[k] == valyr) {
 1239:                         idyr = k;
 1240:                     }
 1241:                 }
 1242:                 if (idyr != -1) {
 1243:                     for (var k=0; k<idsems[idyr].length; k++) {
 1244:                         if (idsems[idyr][k] == valsem) {
 1245:                             idsem = k;
 1246:                         }
 1247:                     }
 1248:                 }
 1249:                 if (idsem != -1) {
 1250:                     for (var k=0; k<idcodes[idyr][idsem].length; k++) {
 1251:                         if (idcodes[idyr][idsem][k] == valdept) {
 1252:                             iddept = k;
 1253:                         }
 1254:                     }
 1255:                 }
 1256:                 if (iddept != -1) {
 1257:                     document.$formname.Number.options[0] =  new Option('All','0',false,false);
 1258:                     for (var i=0; i<idcourses[idyr][idsem][iddept].length; i++) {
 1259:                         var display = idcourses[idyr][idsem][iddept][i];
 1260:                         if (longtitles[3] == 1) {
 1261:                             if (idcourseslongs[idyr][idsem][iddept][i] != "") {
 1262:                                 display = idcourseslongs[idyr][idsem][iddept][i]
 1263:                             }
 1264:                         }
 1265:                         document.$formname.Number.options[i+1] = new Option(display,idcourses[idyr][idsem][iddept][i],false,false)
 1266:                     }
 1267:                 } 
 1268:                 else {
 1269:                     document.$formname.Number.options[0] =  new Option('No courses','0',true,true);
 1270:                 }
 1271:             } 
 1272:             else {
 1273:                 document.$formname.Number.options[0] =  new Option('All','0',true,true);
 1274:             }
 1275:         }
 1276:         document.$formname.Number.selectedIndex = 0
 1277:     }
 1278: }
 1279: END
 1280:     return $output;
 1281: }
 1282: 
 1283: sub autoenroll_info {
 1284:     my ($coursehash,$now,$seclist,$xlist_items,$code,$owners,$cdom,$cnum) = @_;
 1285:     my $autoenrolldates = &mt('Not enabled');
 1286:     if (defined($coursehash->{'internal.autoadds'}) && $coursehash->{'internal.autoadds'} == 1) {
 1287:         my ($autostart,$autoend);
 1288:         if ( defined($coursehash->{'internal.autostart'}) ) {
 1289:             $autostart = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autostart'});
 1290:         }
 1291:         if ( defined($coursehash->{'internal.autoend'}) ) {
 1292:             $autoend = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autoend'});
 1293:         }
 1294:         if ($coursehash->{'internal.autostart'} > $now) {
 1295:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
 1296:                 $autoenrolldates = &mt('Not enabled');
 1297:             } else {
 1298:                 my $valid_classes = 
 1299:                    &get_valid_classes($seclist,$xlist_items,$code,
 1300:                                       $owners,$cdom,$cnum);
 1301:                 if ($valid_classes ne '') {
 1302:                     $autoenrolldates = &mt('Not enabled<br />Starts: ').
 1303:                                        $autostart.'<br />'.$valid_classes;                }
 1304:             }
 1305:         } else {
 1306:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
 1307:                 $autoenrolldates = &mt('Not enabled<br />Ended: ').$autoend;
 1308:             } else {
 1309:                 my $valid_classes = &get_valid_classes($seclist,$xlist_items,
 1310:                                                        $code,$owners,$cdom,$cnum);
 1311:                 if ($valid_classes ne '') {
 1312:                     $autoenrolldates = &mt('Currently enabled<br />').
 1313:                                        $valid_classes;
 1314:                 }
 1315:             }
 1316:         }
 1317:     }
 1318:     return $autoenrolldates;
 1319: }
 1320: 
 1321: sub user_is_known {
 1322:     my $known = 0;
 1323:     if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public' 
 1324:         && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
 1325:         $known = 1;
 1326:     }
 1327:     return $known;
 1328: }
 1329: 
 1330: 1;

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