File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.37: download - view: text, annotated - select for diffs
Tue Jul 8 09:23:38 2008 UTC (15 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_6_99_0, HEAD
- Add some white space after "Display courses" button.
- setCourseId() and changeSort() javascript functions always available on course listing page

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

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