File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.53.4.7: download - view: text, annotated - select for diffs
Thu Oct 7 14:45:04 2010 UTC (13 years, 7 months ago) by raeburn
Branches: version_2_9_X
CVS tags: version_2_9_1
Diff to branchpoint 1.53: preferred, unified
- Backport 1.65.

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

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