File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.58.4.8: download - view: text, annotated - select for diffs
Mon Oct 3 01:35:46 2011 UTC (12 years, 7 months ago) by raeburn
Branches: version_2_10_X
CVS tags: version_2_10_1, loncapaMITrelate_1
Diff to branchpoint 1.58: preferred, unified
- Backport 1.68.

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

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