File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.89: download - view: text, annotated - select for diffs
Mon Apr 4 01:09:47 2016 UTC (8 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6808. New course container -- "Placement" for Placement Tests.

    1: # The LearningOnline Network with CAPA
    2: # Handler for displaying the course catalog interface
    3: #
    4: # $Id: coursecatalog.pm,v 1.89 2016/04/04 01:09:47 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: use LONCAPA::lonauthcgi;
   42: 
   43: sub handler {
   44:     my ($r) = @_;
   45:     &Apache::loncommon::content_type($r,'text/html');
   46:     $r->send_http_header;
   47:     if ($r->header_only) {
   48:         return OK;
   49:     }
   50:     my $handle = &Apache::lonnet::check_for_valid_session($r);
   51:     my $lonidsdir=$r->dir_config('lonIDsDir');
   52:     if ($handle ne '') {
   53:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
   54:     }
   55:     &Apache::lonacc::get_posted_cgi($r);
   56:     &Apache::lonlocal::get_language_handle($r);
   57:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   58:                                             ['sortby','showdom']);
   59: 
   60:     my $codedom = &Apache::lonnet::default_login_domain();
   61: 
   62:     if (($env{'user.domain'} ne '') && ($env{'user.domain'} ne 'public')) { 
   63:         $codedom = $env{'user.domain'};
   64:         if ($env{'request.role.domain'} ne '') {
   65:             $codedom = $env{'request.role.domain'};
   66:         }
   67:     }
   68:     my $formname = 'coursecatalog';
   69:     if ($env{'form.showdom'} ne '') {
   70:         $env{'form.showdom'} = &LONCAPA::clean_domain($env{'form.showdom'});
   71:         if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
   72:             $codedom = $env{'form.showdom'};
   73:         } else {
   74:             $env{'form.showdom'} = '';
   75:         }
   76:     }
   77:     my $domdesc = &Apache::lonnet::domain($codedom,'description');
   78:     &Apache::lonhtmlcommon::clear_breadcrumbs();
   79: 
   80:     my %domconfig =
   81:         &Apache::lonnet::get_dom('configuration',['coursecategories'],$codedom);
   82:     my $knownuser = &user_is_known();
   83:     my $canviewall = &canview_all();
   84:     
   85:     my ($cathash,$cattype);
   86:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
   87:         $cathash = $domconfig{'coursecategories'}{'cats'};
   88:         if ($knownuser || $canviewall) {
   89:             $cattype = $domconfig{'coursecategories'}{'auth'};
   90:         } else {
   91:             $cattype = $domconfig{'coursecategories'}{'unauth'};
   92:         }
   93:         if ($cattype eq '') {
   94:             $cattype = 'std';
   95:         }
   96:     } else {
   97:         $cathash = {};
   98:         $cattype = 'std';
   99:     }
  100:     if ($cattype eq 'none') {
  101:         $r->print(&Apache::loncommon::start_page('Course/Community Catalog'));
  102:         &Apache::lonhtmlcommon::add_breadcrumb
  103:         ({href=>"/adm/coursecatalog",
  104:           text=>"Course/Community Catalog"});
  105:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
  106:         if ($knownuser || $canviewall) {
  107:             $r->print('<div>'.&mt('No catalog of LON-CAPA courses/communities is provided for: [_1]',$domdesc).'</div>');
  108:         } else {
  109:             if ($domconfig{'coursecategories'}{'auth'} eq 'none') {
  110:                 $r->print('<div>'.&mt('No catalog of LON-CAPA courses/communities is provided for: [_1]',$domdesc).'</div>');
  111:             } else {
  112:                 $r->print('<div>'.&mt('The catalog of LON-CAPA courses/communities provided for: "[_1]" is only available to users who are logged in.',$domdesc).'</div>');
  113:             }
  114:         }      
  115:         $r->print(&Apache::loncommon::end_page());
  116:         return OK;
  117:     }
  118: 
  119:     my $cnum; 
  120:     if ($cattype eq 'codesrch') {
  121:         my ($uniquecode,$codemsg,$brtext);
  122:         if ($env{'form.uniquecode'}) {
  123:             $uniquecode = $env{'form.uniquecode'};
  124:             $uniquecode =~ s/^\s+|\s+$//g;
  125:         }
  126:         my $js = '<script type="text/javascript">'."\n".
  127:                  '// <![CDATA['."\n".
  128:                  &courselink_javascript()."\n".
  129:                  '// ]]>'."\n".
  130:                  '</script>'."\n";
  131:         $r->print(&Apache::loncommon::start_page('Search for a Course/Community',$js));
  132:         if ($uniquecode =~ /^\w{6}$/) {
  133:             &Apache::lonhtmlcommon::add_breadcrumb
  134:             ({href=>"/adm/coursecatalog",
  135:               text=>"Course/Community Catalog"});
  136:             $brtext = 'Search Result';
  137:         } else {
  138:             $brtext = 'Course/Community Catalog';
  139:         }
  140:         &Apache::lonhtmlcommon::add_breadcrumb
  141:         ({href=>"/adm/coursecatalog",
  142:           text=>"$brtext"});
  143:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
  144:         $r->print(&coursesearch($codedom,$domdesc,$uniquecode));
  145:         if ($uniquecode =~ /^\w{6}$/) {
  146:             $r->print('<hr />');
  147:             my $confname = $codedom.'-domainconfig';
  148:             my %codes = &Apache::lonnet::get('uniquecodes',[$uniquecode],$codedom,$confname);
  149:             if ($codes{$uniquecode}) {
  150:                 $cnum = $codes{$uniquecode};
  151:                 my %courses = &Apache::lonnet::courseiddump($codedom,'.',1,'.','.',
  152:                                                             $cnum,undef,undef,'.',1);
  153:                 if (keys(%courses)) {
  154:                     $env{'form.coursenum'} = $cnum;
  155:                     my %courseinfo = &build_courseinfo_hash(\%courses,$knownuser,$codedom,$canviewall);
  156:                     undef($env{'form.coursenum'});
  157:                     if (ref($courseinfo{$codedom.'_'.$cnum}) eq 'HASH') {
  158:                         $r->print(&Apache::lonhtmlcommon::start_pick_box());
  159:                         my @cols = ('title','ownerlastnames','seclist','access');
  160:                         my %lt = &Apache::lonlocal::texthash(
  161:                                      title          => 'Title',
  162:                                      ownerlastnames => 'Owner & Co-owner(s)',
  163:                                      seclist        => 'Sections',
  164:                                      access         => 'Default Access Dates for Students',
  165:                                  );
  166:                         my @shown;
  167:                         foreach my $item (@cols) {
  168:                             if ($courseinfo{$codedom.'_'.$cnum}{$item}) {
  169:                                 push(@shown,$item);
  170:                             }
  171:                         }
  172:                         my $num = 0;
  173:                         foreach my $item (@shown) {
  174:                             $num ++;
  175:                             $r->print(&Apache::lonhtmlcommon::row_title($lt{$item}).
  176:                                       $courseinfo{$codedom.'_'.$cnum}{$item});
  177:                             if ($item eq 'title') {
  178:                                 if ($courseinfo{$codedom.'_'.$cnum}{'showsyllabus'}) {
  179:                                     $r->print('&nbsp;<font size="-2">'.
  180:                                               '<a href="javascript:ToSyllabus('."'$codedom','$cnum'".')">'.
  181:                                               &mt('Syllabus').'</a></font>');
  182:                                 }
  183:                             }
  184:                             my $arg = ( $num == scalar(@shown) ? 1 : '' );
  185:                             $r->print(&Apache::lonhtmlcommon::row_closure($arg));
  186:                         }
  187:                         $r->print(&Apache::lonhtmlcommon::end_pick_box());
  188:                         my $selfenroll = &selfenroll_status($courseinfo{$codedom.'_'.$cnum},$codedom.'_'.$cnum);
  189:                         if ($selfenroll) {
  190:                             $r->print('<br />'.$selfenroll);
  191:                         }
  192:                         $r->print('<form name="linklaunch" method="post" action="">'."\n".
  193:                                   '<input type="hidden" name="backto" value="coursecatalog" />'."\n".
  194:                                   '<input type="hidden" name="courseid" value="" />'."\n".
  195:                                   '</form>'."\n");
  196: 
  197:                     } else {
  198:                         $codemsg = &mt('Code matched, but course ID to which this mapped is invalid.');
  199:                     }
  200:                 } else {
  201:                     $codemsg = &mt('Code matched, but course ID to which this mapped is invalid.');              
  202:                 } 
  203:             } else {
  204:                 $codemsg = &mt('No match');
  205:             }
  206:         }
  207:         $r->print('<br />'.&Apache::loncommon::end_page());
  208:         return OK;
  209:     } else {
  210:         if ($env{'form.coursenum'}) {
  211:             $cnum = $env{'form.coursenum'};
  212:         }
  213:     }
  214: 
  215:     if ($env{'form.catalog_maxdepth'} ne '') {
  216:         $env{'form.catalog_maxdepth'} =~ s{\D}{}g;
  217:     }
  218: 
  219:     my (@cats,@trails,%allitems,%idx,@jsarray,%subcathash,$subcats);
  220:     if ($env{'form.withsubcats'}) {
  221:         $subcats = \%subcathash;
  222:     }
  223:     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems,
  224:                                            \%idx,\@jsarray,$subcats);
  225:     my ($numtitles,@codetitles);
  226:     if (($env{'form.coursenum'} ne '') && ($knownuser)) {
  227:         &course_details($r,$codedom,$formname,$domdesc,\@trails,\%allitems,\@codetitles);
  228:     } else {
  229:         my ($catlinks,$has_subcats,$selitem) = &category_breadcrumbs($codedom,@cats);
  230:         my $wasacctext = &get_wasactive_text();
  231:         my $catjs = <<"ENDSCRIPT";
  232: 
  233: function setCatDepth(depth) {
  234:     document.coursecats.catalog_maxdepth.value = depth;
  235:     if (depth == '') {
  236:         document.coursecats.currcat_0.value = '';
  237:     }
  238:     document.coursecats.submit();
  239:     return;
  240: }
  241: 
  242: function changeSort(caller) {
  243:     document.$formname.sortby.value = caller;
  244:     document.$formname.submit();
  245: }
  246: 
  247: function setCourseId(caller) {
  248:     document.$formname.coursenum.value = caller;
  249:     document.$formname.submit();
  250: }
  251: 
  252: ENDSCRIPT
  253:         $catjs .= &courselink_javascript();
  254:         if (&user_is_dc($codedom) || $canviewall) {
  255:             $catjs .= <<ENDTOGGJS
  256: 
  257: function toggleStatuses() {
  258:     if (document.$formname.showdetails.checked) {
  259:         document.getElementById('statuschoice').style.display='block';
  260:         document.getElementById('statuscell').style.borderLeft='1px solid'; 
  261:     } else {
  262:         document.getElementById('statuschoice').style.display='none';
  263:         document.getElementById('statuscell').style.borderLeft='0px';
  264:     }
  265:     return;
  266: }
  267: 
  268: function toggleWasActive() {
  269:     if (document.getElementById('counts_Previous')) {
  270:         if (document.getElementById('counts_Previous').checked) {
  271:             document.getElementById('choosewasactive').style.display='block';
  272:             document.getElementById('choosewasacctext').innerHTML = '$wasacctext';
  273:         } else {
  274:             document.getElementById('choosewasactive').style.display='none';
  275:             document.getElementById('choosewasacctext').innerHTML = '';
  276:         }
  277:     }
  278:     return;
  279: }
  280: 
  281: ENDTOGGJS
  282:         }
  283:         if ($env{'form.currcat_0'} eq 'instcode::0') {
  284:             $numtitles = &instcode_course_selector($r,$codedom,$formname,$domdesc,
  285:                                                    $catlinks,$catjs,\@codetitles,$cattype,$canviewall);
  286:             if ($env{'form.state'} eq 'listing') {
  287:                 $r->print(&print_course_listing($codedom,$numtitles,undef,undef,undef,
  288:                                                 \@codetitles,$canviewall));
  289:             }
  290:         } else {
  291:             my (%add_entries);
  292:             my ($currdepth,$deeper) = &get_depth_values();
  293:             if ($selitem) {
  294:                 my $alert = &mt('Choose a subcategory to display');
  295:                 if (!$deeper) {
  296:                     $alert = &mt('Choose a category to display');
  297:                 }
  298:                 &js_escape(\$alert);
  299:                 $catjs .= <<ENDJS;
  300: function check_selected() {
  301:     if (document.coursecats.$selitem.options[document.coursecats.$selitem.selectedIndex].value == "") {
  302:         alert('$alert');
  303:         return false;
  304:     }
  305: }
  306: ENDJS
  307:             }
  308:             $catjs = '<script type="text/javascript">'."\n".$catjs."\n".'</script>';
  309:             &cat_header($r,$codedom,$catjs,\%add_entries,$catlinks,undef,$cattype);
  310:             if ($env{'form.currcat_0'} ne '') {
  311:                 $r->print('<form name="'.$formname.
  312:                           '" method="post" action="/adm/coursecatalog">'.
  313:                           &additional_filters($codedom,$has_subcats,$canviewall)."\n");
  314:                 $r->print('<input type="hidden" name="catalog_maxdepth" value="'.
  315:                           $deeper.'" />'."\n");
  316:                 for (my $i=0; $i<$deeper; $i++) {
  317:                     $r->print('<input type="hidden" name="currcat_'.$i.'" value="'.$env{'form.currcat_'.$i}.'" />'."\n");
  318:                 }
  319:                 my $display_button;
  320:                 if ($env{'form.currcat_0'} eq 'communities::0') {
  321:                     $display_button = &mt('Display communities');
  322:                 } elsif ($env{'form.currcat_0'} eq 'placement::0') {
  323:                     $display_button = &mt('Display placement tests');
  324:                 } else {
  325:                     $display_button = &mt('Display courses');
  326:                 }
  327:                 $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
  328:                           '<input type="hidden" name="sortby" value="" />'."\n".
  329:                           '<input type="hidden" name="state" value="listing" />'."\n".
  330:                           '<input type="hidden" name="showdom" value="'.
  331:                           $env{'form.showdom'}.'" />'.
  332:                           '<input type="submit" name="catalogfilter" value="'.
  333:                           $display_button.'" /></form><br /><br />');
  334:             }
  335:             if ($env{'form.state'} eq 'listing') {
  336:                 $r->print(&print_course_listing($codedom,undef,\@trails,\%allitems,$subcats,\@codetitles,
  337:                                                 $canviewall));
  338:             }
  339:         }
  340:     }
  341:     $r->print('<br />'.&Apache::loncommon::end_page());
  342:     return OK;
  343: }
  344: 
  345: sub course_details {
  346:     my ($r,$codedom,$formname,$domdesc,$trails,$allitems,$codetitles) = @_;
  347:     my $output;
  348:     my %add_entries = (topmargin    => "0",
  349:                        marginheight => "0",);
  350:     my $js = '<script type="text/javascript">'."\n".
  351:              &courselink_javascript().'</script>'."\n";
  352:     my $start_page =
  353:         &Apache::loncommon::start_page('Course/Community Catalog',$js,
  354:                                        {'add_entries' => \%add_entries, });
  355:     $r->print($start_page);
  356:     if ($env{'form.numtitles'} > 0) {
  357:         &Apache::lonhtmlcommon::add_breadcrumb
  358:                 ({href=>"/adm/coursecatalog",
  359:                   text=>"Course/Community Catalog"});
  360:     }
  361:     my $brtextone = 'Course listing';
  362:     my $brtexttwo = 'Course details';
  363:     my $textthree = &mt('Detailed course information:');
  364:     my $textfour = &mt('Back to course listing');
  365:     if ($env{'form.currcat_0'} eq 'communities::0') {
  366:         $brtextone = 'Community listing';
  367:         $brtexttwo = 'Community details';
  368:         $textthree = &mt('Detailed community information:');
  369:         $textfour = &mt('Back to community listing');
  370:     } elsif ($env{'form.currcat_0'} eq 'placement::0') {
  371:         $brtextone = 'Placement test listing';
  372:         $brtexttwo = 'Placement test details';
  373:         $textthree = &mt('Detailed placement test information:');
  374:         $textfour = &mt('Back to placement test listing');
  375:     }
  376:     &Apache::lonhtmlcommon::add_breadcrumb
  377:              ({href=>"javascript:document.$formname.submit()",
  378:               text=>$brtextone},
  379:              {text=>$brtexttwo});
  380:     $r->print(
  381:         &Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog').
  382:         '<h2>'.$textthree.'</h2>'.
  383:         &print_course_listing($codedom,undef,$trails,$allitems,undef,$codetitles).
  384:         '<br />'.
  385:         '<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
  386:         &Apache::lonhtmlcommon::actionbox([
  387:             '<a href = "javascript:document.coursecatalog.submit()">'.$textfour.'</a>']).
  388:         &Apache::lonhtmlcommon::echo_form_input(['coursenum','catalogfilter',
  389:                                                  'showdetails','courseid']).
  390:         '</form>');
  391:     return;
  392: }
  393: 
  394: sub coursesearch {
  395:     my ($codedom,$domdesc,$uniquecode) = @_;
  396:     my %lt = &Apache::lonlocal::texthash (
  397:                                            crlk => 'Course look-up',  
  398:                                            code => 'Code',
  399:                                            ifyo => 'Enter the course code (six letters and numbers)',
  400:                                            srch => 'Find course',
  401:                                          );
  402:     return <<"END";
  403: <h3>$lt{'crlk'} ($domdesc)</h3>
  404: $lt{'ifyo'}
  405: <form name="searchbycode" method="post" action="">
  406: <span class="LC_nobreak">
  407: <input type="text" value="$uniquecode" size="6" name="uniquecode" />&nbsp;
  408: <br />
  409: <input type="submit" value="$lt{'srch'}" name="srch" /></span>
  410: </form>
  411: END
  412: }
  413: 
  414: sub courselink_javascript {
  415:     return <<"END";
  416: 
  417: function ToSyllabus(cdom,cnum) {
  418:     if (cdom == '' || cdom == null) {
  419:         return;
  420:     }
  421:     if (cnum == '' || cnum == null) {
  422:         return;
  423:     }
  424:     document.linklaunch.action = "/public/"+cdom+"/"+cnum+"/syllabus";
  425:     document.linklaunch.submit();
  426: }
  427: 
  428: function ToSelfenroll(courseid) {
  429:     if (courseid == '') {
  430:         return;
  431:     }
  432:     document.linklaunch.action = "/adm/selfenroll";
  433:     document.linklaunch.courseid.value = courseid;
  434:     document.linklaunch.submit();
  435: }
  436: 
  437: END
  438: }
  439: 
  440: sub instcode_course_selector {
  441:     my ($r,$codedom,$formname,$domdesc,$catlinks,$catjs,$codetitles,$cattype,$canviewall) = @_;
  442:     my %coursecodes = ();
  443:     my %codes = ();
  444:     my %cat_titles = ();
  445:     my %cat_order = ();
  446:     my %cat_items;
  447:     my $caller = 'global';
  448:     my $format_reply;
  449:     my %add_entries = (topmargin    => "0",
  450:                        marginheight => "0",);
  451:     my ($jscript,$totcodes,$numtitles,$lasttitle) = 
  452:         &Apache::courseclassifier::instcode_selectors_data($codedom,$formname,
  453:                            \%cat_items,$codetitles,\%cat_titles,\%cat_order);
  454:     my $js = '<script type="text/javascript">'."\n".
  455:              '// <![CDATA['."\n".
  456:              "$jscript\n$catjs\n".
  457:              '// ]]>'."\n".
  458:              '</script>';
  459:     if ($totcodes) {
  460:         if (($env{'form.state'} eq 'listing') && ($numtitles > 0)) {
  461:             $add_entries{'onload'} = 'setElements();';
  462:         }
  463:         if (&user_is_dc($codedom) || $canviewall) {
  464:             $add_entries{'onload'} .= ' toggleStatuses();toggleWasActive();'
  465:         }
  466:         &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles,$cattype);
  467:         my $cat_maxdepth = $env{'form.catalog_maxdepth'};
  468:         $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
  469:                   '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'."\n".
  470:                   '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'."\n".
  471:                   '<input type="hidden" name="currcat_0" value="instcode::0" />'.
  472:                   &additional_filters($codedom,undef,$canviewall));
  473:         if ($numtitles > 0) {
  474:             $r->print('<b>'.&mt('Choose which course(s) to list.').'</b><br />'.
  475:                       &Apache::courseclassifier::build_instcode_selectors($numtitles,
  476:                        $lasttitle,\%cat_items,$codetitles,\%cat_titles,\%cat_order));
  477:         }
  478:         $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
  479:                   '<input type="hidden" name="sortby" value="" />'."\n".
  480:                   '<input type="hidden" name="state" value="listing" />'."\n".
  481:                   '<input type="submit" name="catalogfilter" value="'.
  482:                   &mt('Display courses').'" />'.
  483:                   '<input type="hidden" name="numtitles" value="'.$numtitles.
  484:                   '" /></form><br /><br />');
  485:     } else {
  486:         $js = '<script type="text/javascript">'."\n".
  487:               '// <![CDATA['."\n".
  488:               "$catjs\n".
  489:               '// ]]>'."\n".
  490:               '</script>';
  491:         &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles,$cattype);
  492:         my $cat_maxdepth = $env{'form.catalog_maxdepth'};
  493:         $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
  494:                   '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'.
  495:                   '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'.
  496:                   '<input type="hidden" name="currcat_0" value="instcode::0" />');
  497:         $r->print('<br />'.&mt('No official courses to display for [_1].',$domdesc).'</form>');
  498:     }
  499:     return $numtitles;
  500: }
  501: 
  502: sub cat_header {
  503:     my ($r,$codedom,$js,$add_entries,$catlinks,$numtitles,$cattype) = @_;
  504:     my $start_page =
  505:         &Apache::loncommon::start_page('Course/Community Catalog',$js,
  506:                                        { 'add_entries' => $add_entries, });
  507:     $r->print($start_page);
  508:     my $brtext = 'Course listing';
  509:     if ($env{'form.currcat_0'} eq 'communities::0') {
  510:         $brtext = 'Community listing';
  511:     } elsif ($env{'form.currcat_0'} eq 'placement::0') {
  512:         $brtext = 'Placement test listing';
  513:     }
  514:     if ($env{'form.state'} eq 'listing') {
  515:         if ($numtitles > 0) {
  516:             &Apache::lonhtmlcommon::add_breadcrumb
  517:                 ({href=>"/adm/coursecatalog",
  518:                   text=>"Course/Community Catalog"},
  519:                  {text=>$brtext});
  520:         } else {
  521:             &Apache::lonhtmlcommon::add_breadcrumb
  522:             ({text=>$brtext});
  523:         }
  524:     } else {
  525:         &Apache::lonhtmlcommon::add_breadcrumb
  526:         ({href=>"/adm/coursecatalog",
  527:           text=>"Course/Community Catalog"});
  528:     }
  529:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
  530:     if ($cattype eq 'std') {
  531:         my $onchange = 'this.form.submit()';
  532:         $r->print('<form name="coursecatdom" method="post" action="/adm/coursecatalog">'.
  533:                   '<table border="0"><tr><td><b>'.&mt('Domain:').'</b></td><td>'.
  534:                   &Apache::loncommon::select_dom_form($codedom,'showdom','',1,$onchange));
  535:         if (!$onchange) {
  536: 	   $r->print('&nbsp;<input type="submit" name="godom" value="'.&mt('Change').'" />');
  537:         }
  538:         $r->print('</td></tr></table></form>');
  539:     }
  540:     $r->print('<form name="coursecats" method="post" action="/adm/coursecatalog"'.
  541:               ' onsubmit="return check_selected();">'.
  542:               '<table border="0"><tr>'.$catlinks.'</tr></table></form>');
  543:     return;
  544: }
  545: 
  546: sub category_breadcrumbs {
  547:     my ($dom,@cats) = @_;
  548:     my $crumbsymbol = ' &#x25b6; ';
  549:     my ($currdepth,$deeper) = &get_depth_values();
  550:     my $currcat_str = 
  551:         '<input type="hidden" name="catalog_maxdepth" value="'.$deeper.'" />'.
  552:         '<input type="hidden" name="showdom" value="'.$dom.'" />';
  553:     my $catlinks = '<td valign="top"><b>'.&mt('Catalog:').'</b></td><td><table><tr><td>';
  554:     my $has_subcats;
  555:     my $selitem;
  556:     if (ref($cats[0]) eq 'ARRAY') {
  557:         if (@{$cats[0]} == 0) {
  558:             $catlinks .= &mt('No categories defined in this domain'); 
  559:         } elsif (@{$cats[0]} == 1) {
  560:             if ($cats[0][0] eq 'instcode') {
  561:                 $catlinks .= &mt('Official courses (with institutional codes)');
  562:                 $env{'form.currcat_0'} = 'instcode::0';
  563:             } elsif ($cats[0][0] eq 'communities') {
  564:                 $catlinks .= &mt('Communities');
  565:                 $env{'form.currcat_0'} = 'communities::0';
  566:             } elsif ($cats[0][0] eq 'placement') {
  567:                 $catlinks .= &mt('Placement Tests');
  568:                 $env{'form.currcat_0'} = 'placement::0';
  569:             } else {
  570:                 my $name = $cats[0][0];
  571:                 my $item = &escape($name).'::0';
  572:                 $catlinks .= $name;
  573:                 $env{'form.currcat_0'} = $item;
  574:             }
  575:             $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
  576:         } else {
  577:             $catlinks .= &main_category_selector(@cats);
  578:             if (($env{'form.currcat_0'} ne '') && 
  579:                 ($env{'form.currcat_0'} ne 'instcode::0')) {
  580:                 $catlinks .= $crumbsymbol;
  581:             }
  582:         }
  583:     } else {
  584:         $catlinks .= &mt('Official courses (with institutional codes)');
  585:                      $env{'form.currcat_0'} = 'instcode::0';
  586:          $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
  587:     }
  588:     if ($deeper) {
  589:         for (my $i=1; $i<=$deeper; $i++) {
  590:             my $shallower = $i-1;
  591:             next if ($shallower == 0);
  592:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
  593:             if ($cat ne '') {
  594:                 $catlinks .= '<td valign="top">'.
  595:                              '<select name="currcat_'.$shallower.'" onchange="'.
  596:                              'setCatDepth('."'$shallower'".');this.form.submit();">';
  597:                 if (ref($cats[$shallower]{$container}) eq 'ARRAY') {
  598:                     $catlinks .= '<option value="">'.&mt('De-select').'</option>';
  599:                     for (my $j=0; $j<@{$cats[$shallower]{$container}}; $j++) {
  600:                         my $name = $cats[$shallower]{$container}[$j];
  601:                         my $item = &escape($name).':'.&escape($container).':'.$shallower;
  602:                         my $selected = '';
  603:                         if ($item eq $env{'form.currcat_'.$shallower}) {
  604:                             $selected = ' selected="selected"';
  605:                         }
  606:                         $catlinks .= 
  607:                            '<option value="'.$item.'"'.$selected.'>'.$name.'</option>';
  608:                     }
  609:                 }
  610:                 $catlinks .= '</select>';
  611:             }
  612:             unless ($i == $deeper) {
  613:                 $catlinks .= $crumbsymbol;
  614:             } 
  615:         }
  616:         my ($cat,$container,$depth);
  617:         if ($env{'form.currcat_'.$currdepth} eq '') {
  618:             my $shallower = $currdepth - 1;
  619:             ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
  620:         } else {
  621:             ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$currdepth});
  622:         }
  623:         my $deeperlevel = $depth +1;
  624:         if (ref($cats[$deeperlevel]{$cat}) eq 'ARRAY') {
  625:             $has_subcats = 1;
  626:             my $buttontext = &mt('Show subcategories');
  627:             my $selitem = 'currcat_'.$deeperlevel;
  628:             $catlinks .= '&nbsp;<select name="'.$selitem.'" onchange="this.form.submit()">';
  629:             if (@{$cats[$deeperlevel]{$cat}}) {
  630:                 $catlinks .= '<option value="" selected="selected">'.
  631:                              &mt('Subcategory ...').'</option>';
  632:             }
  633:             for (my $k=0; $k<@{$cats[$deeperlevel]{$cat}}; $k++) {
  634:                 my $name = $cats[$deeperlevel]{$cat}[$k];
  635:                 my $item = &escape($name).':'.&escape($cat).':'.$deeperlevel;
  636:                 $catlinks .= '<option value="'.$item.'">'.$name.'</option>'."\n";
  637:             }
  638:             $catlinks .= '</select>'."\n";
  639:         } elsif ($cat ne 'instcode') {
  640:             $catlinks .= '&nbsp;'.&mt('(No subcategories)');
  641:         }
  642:     } else {
  643:         $selitem = 'currcat_0';
  644:     }
  645:     $catlinks .= $currcat_str.'</td></tr></table></td>';
  646:     return ($catlinks,$has_subcats,$selitem);
  647: }
  648: 
  649: sub main_category_selector {
  650:     my (@cats) = @_;
  651:     my $maincatlinks = '<select name="currcat_0" onchange="setCatDepth('."'0'".');this.form.submit();">'."\n";
  652:     if (ref($cats[0]) eq 'ARRAY') {
  653:         if (@{$cats[0]} > 1) {
  654:             my $selected = '';
  655:             if ($env{'form.currcat_0'} eq '') {
  656:                 $selected = ' selected="selected"';    
  657:             }
  658:             $maincatlinks .= 
  659:                 '<option value=""'.$selected.'>'.&mt('Select').'</option>'."\n";
  660:         }
  661:         for (my $i=0; $i<@{$cats[0]}; $i++) {
  662:             my $name = $cats[0][$i];
  663:             my $item = &escape($name).'::0';
  664:             my $selected;
  665:             if ($env{'form.currcat_0'} eq $item) {
  666:                 $selected = ' selected="selected"';
  667:             }
  668:             $maincatlinks .= '<option value="'.$item.'"'.$selected.'>';
  669:             if ($name eq 'instcode') {
  670:                 $maincatlinks .= &mt('Official courses (with institutional codes)');
  671:             } elsif ($name eq 'communities') {
  672:                 $maincatlinks .= &mt('Communities');
  673:             } elsif ($name eq 'placement') {
  674:                 $maincatlinks .= &mt('Placement Tests');
  675:             } else {
  676:                 $maincatlinks .= $name;
  677:             }
  678:             $maincatlinks .= '</option>'."\n";
  679:         }
  680:         $maincatlinks .= '</select>'."\n";
  681:     }
  682:     return $maincatlinks;
  683: }
  684: 
  685: sub get_depth_values {
  686:     my $currdepth = 0;
  687:     my $deeper = 0;
  688:     if ($env{'form.catalog_maxdepth'} ne '') {
  689:         $currdepth = $env{'form.catalog_maxdepth'};
  690:         if ($env{'form.currcat_'.$currdepth} eq '') {
  691:             $deeper = $currdepth;
  692:         } else {
  693:             $deeper = $currdepth + 1;
  694:         }
  695:     }
  696:     return ($currdepth,$deeper);
  697: }
  698: 
  699: sub additional_filters {
  700:     my ($codedom,$has_subcats,$canviewall) = @_;
  701:     my $is_dc = &user_is_dc($codedom);
  702:     my $output = '<div class="LC_left_float">';
  703:     if ($is_dc || $canviewall) {
  704:         $output .= '<fieldset><legend>'.&mt('Options').'</legend>';
  705:     }
  706:     $output .= '<table><tr><td valign="top">';
  707:     if (($env{'form.currcat_0'} ne 'instcode::0') &&
  708:         ($env{'form.currcat_0'} ne '') && ($has_subcats)) {
  709:         my $include_subcat_status;
  710:         if ($env{'form.withsubcats'}) {
  711:             $include_subcat_status = 'checked="checked" ';
  712:         }
  713:         my $counter = $env{'form.catalog_maxdepth'};
  714:         if ($counter > 0) {
  715:             if ($env{'form.state'} eq 'listing') {
  716:                 $counter --;
  717:             } elsif ($env{'form.currcat_'.$counter} eq '') {
  718:                 $counter --;
  719:             }
  720:         }
  721:         my ($catname) = split(/:/,$env{'form.currcat_'.$counter});
  722:         if ($catname ne '') {
  723:             $output .= '<span class="LC_nobreak"><label>'.
  724:                        '<input type="checkbox" name="withsubcats" value="1" '.
  725:                        $include_subcat_status.'/>'.
  726:                        &mt('Include subcategories within "[_1]"',
  727:                            &unescape($catname)).'</label></span><br />';
  728:         }
  729:     }
  730:     my $show_selfenroll_status;
  731:     if ($env{'form.showselfenroll'}) {
  732:         $show_selfenroll_status = 'checked="checked" ';
  733:     }
  734:     my $selfenroll_text;
  735:     if ($env{'form.currcat_0'} eq 'communities::0') {
  736:         $selfenroll_text = &mt('Only show communities which currently allow self-enrollment (or will allow it in the future)');
  737:     } elsif ($env{'form.currcat_0'} eq 'placement::0') {
  738:         $selfenroll_text = &mt('Only show placement tests which currently allow self-enrollment (or will allow it in the future)');
  739:     } else {
  740:         $selfenroll_text = &mt('Only show courses which currently allow self-enrollment (or will allow it in the future)');
  741:     }
  742:     $output .= '<span class="LC_nobreak">'.
  743:                '<label><input type="checkbox" name="showselfenroll" value="1" '.
  744:                $show_selfenroll_status.'/>'.$selfenroll_text.
  745:                '</label></span><br />';
  746:     if ($is_dc || $canviewall) {
  747:         my ($titlesref,$orderref) = &get_statustitles('filters');
  748:         my $showdetails_status;
  749:         if ($env{'form.showdetails'}) {
  750:             $showdetails_status = 'checked="checked" ';
  751:         }
  752:         my $showhidden_status;
  753:         if ($env{'form.showhidden'}) {
  754:             $showhidden_status = 'checked="checked" ';
  755:         }
  756:         my @currstatuses = &Apache::loncommon::get_env_multiple('form.showcounts');
  757:         my $dc_title = &Apache::lonnet::plaintext('dc');
  758:         my ($details_text,$hidden_text,$statusdisplay,$cellborder);
  759:         my $wasactivedisplay = 'none';
  760:         if ($env{'form.showdetails'}) {
  761:             $statusdisplay = 'block';
  762:             $cellborder = 'border-left: 1px solid;';
  763:             if (grep(/^Previous$/,@currstatuses)) {
  764:                 $wasactivedisplay = 'block';
  765:             }
  766:         } else {
  767:             $statusdisplay = 'none';
  768:             $cellborder = 'border-left: 0px';
  769:         }
  770:         if ($env{'form.currcat_0'} eq 'communities::0') {
  771:             $details_text = &mt('Show full details for each community ([_1] only)',$dc_title);
  772:             $hidden_text = &mt('Include communities set to be hidden from catalog ([_1] only)',$dc_title);
  773:         } elsif ($env{'form.currcat_0'} eq 'placement::0') {
  774:             $details_text = &mt('Show full details for each placement test ([_1] only)',$dc_title);
  775:             $hidden_text = &mt('Include placement tests set to be hidden from catalog ([_1] only)',$dc_title);
  776:         } else {
  777:             $details_text = &mt('Show full details for each course ([_1] only)',$dc_title);
  778:             $hidden_text = &mt('Include courses set to be hidden from catalog ([_1] only)',$dc_title);
  779:         }
  780:         if ($is_dc) {
  781:             $output .= '<span class="LC_nobreak">'.
  782:                        '<label><input type="checkbox" name="showhidden" value="1" '.
  783:                        $showhidden_status.'/>'.$hidden_text.
  784:                        '</label></span><br />'."\n";
  785:         }
  786:         $output .= '<span class="LC_nobreak">'.
  787:                    '<label><input type="checkbox" name="showdetails" value="1" '.
  788:                    $showdetails_status.'onclick="toggleStatuses();" />'.
  789:                    $details_text.'</label></span></td>'."\n".
  790:                    '<td id="statuscell" valign="top" style="'.$cellborder.'">'.
  791:                    '<div id="statuschoice" style="display:'.$statusdisplay.'">';
  792:         if (ref($orderref) eq 'ARRAY') {
  793:             if (@{$orderref} > 0) {
  794:                 foreach my $type (@{$orderref}) {
  795:                     my $checked;
  796:                     if (grep(/^\Q$type\E$/,@currstatuses)) {
  797:                         $checked = ' checked="checked"';
  798:                     }
  799:                     my $title;
  800:                     if (ref($titlesref) eq 'HASH') {
  801:                         $title = $titlesref->{$type};
  802:                     }
  803:                     unless ($title) {
  804:                         $title = &mt($type);
  805:                     }
  806:                     my $onclick;
  807:                     if ($type eq 'Previous') {
  808:                         $onclick = ' onclick="toggleWasActive();"'; 
  809:                     }
  810:                     $output .= '<span class="LC_nobreak">'.
  811:                                '<label><input type="checkbox" id="counts_'.$type.'"'.
  812:                                ' name="showcounts" value="'.$type.'"'.$checked.$onclick.
  813:                                ' />'.$title.'</label></span>';
  814:                     if ($type eq 'Previous') {
  815:                         my %milestonetext = &Apache::lonlocal::texthash (
  816:                             accessend => 'immediately prior to default end access date',
  817:                             enrollend => 'immediately prior to end date for auto-enrollment',
  818:                             date      => 'immediately prior to specific date:',
  819:                         );
  820:                         my @statuses = &Apache::loncommon::get_env_multiple('form.showcounts');
  821:                         $output .= '<span id="choosewasacctext" class="LC_nobreak">';
  822:                         if ($checked) {
  823:                             $output .= &get_wasactive_text();
  824:                         }
  825:                         $output .= '</span>'.
  826:                                    '<div id="choosewasactive" style="display:'.$wasactivedisplay.'">'.
  827:                                    '<table>';
  828:                         my @milestones = ('accessend');
  829:                         if (&Apache::lonnet::auto_run(undef,$codedom)) {
  830:                             push(@milestones,'enrollend');
  831:                         }
  832:                         push(@milestones,'date');
  833:                         foreach my $item (@milestones) {
  834:                             my $checked;
  835:                             if ($env{'form.state'} eq 'listing') {
  836:                                 if ($env{'form.wasactive'} eq $item) {
  837:                                     $checked = ' checked="checked"';
  838:                                 }
  839:                             } elsif ($item eq 'accessend') {
  840:                                 $checked = ' checked="checked"';
  841:                             }
  842:                             $output .=
  843:                                 '<tr><td width="10">&nbsp;</td><td>'.
  844:                                 '<span class="LC_nobreak"><label>'.
  845:                                 '<input type="radio" value="'.$item.'" name="wasactive"'.$checked.' />'.
  846:                                 $milestonetext{$item}.'</label></span>';
  847:                             if ($item eq 'date') {
  848:                                 my $wasactiveon;
  849:                                 if (grep(/^Previous$/,@currstatuses)) {
  850:                                     $wasactiveon =
  851:                                         &Apache::lonhtmlcommon::get_date_from_form('wasactiveon');
  852:                                 } else {
  853:                                     $wasactiveon = 'now';
  854:                                 }
  855:                                 $output .= ' '.
  856:                                     &Apache::lonhtmlcommon::date_setter('coursecatalog',
  857:                                                                         'wasactiveon',
  858:                                                                         $wasactiveon,
  859:                                                                         '','','',1,'',
  860:                                                                         '','',1);
  861:                             }
  862:                             $output .= '</td></tr>';
  863:                         }
  864:                         $output .= '</table></div>';
  865:                     }
  866:                     $output .= '<br />';
  867:                 }
  868:             }
  869:         }
  870:         $output .= '</div></td>';
  871:     } else {
  872:         $output .= '</td>';  
  873:     }
  874:     $output .= '</tr></table></fieldset></div>'.
  875:                '<div style="clear:both;margin:0;"></div>';  
  876:     return $output;
  877: }
  878: 
  879: sub user_is_dc {
  880:     my ($codedom) = @_;
  881:     if (exists($env{'user.role.dc./'.$codedom.'/'})) {
  882:         my $livedc = 1;
  883:         my $now = time;
  884:         my ($start,$end)=split(/\./,$env{'user.role.dc./'.$codedom.'/'});
  885:         if ($start && $start>$now) { $livedc = 0; }
  886:         if ($end   && $end  <$now) { $livedc = 0; }
  887:         return $livedc;
  888:     }
  889:     return;
  890: }
  891: 
  892: sub canview_all {
  893:     my $canviewall = 0;
  894:     my $page = 'coursecatalog';
  895:     if (&LONCAPA::lonauthcgi::can_view($page)) {
  896:         $canviewall = 1;
  897:     } elsif (&LONCAPA::lonauthcgi::check_ipbased_access($page)) {
  898:         $canviewall= 1;
  899:     }
  900:     return $canviewall;
  901: }
  902: 
  903: sub get_statustitles {
  904:     my ($caller) = @_;
  905:     my @status_order = ('Active','Future','Previous');
  906:     my %status_title;
  907:     if ($caller eq 'filters') {
  908:         %status_title = &Apache::lonlocal::texthash(
  909:                            Previous => 'Show count for past access',
  910:                            Active => 'Show count for current student access',
  911:                            Future => 'Show count for future student access',
  912:                         );
  913:         if ($env{'form.currcat_0'} eq 'communities::0') {
  914:             $status_title{'Active'} = 'Show count for current member access';
  915:             $status_title{'Future'} = 'Show count for future member access'; 
  916:         }
  917:     } else {    
  918:         %status_title = &Apache::lonlocal::texthash(
  919:                            Previous => 'Previous access',
  920:                            Active => 'Current access',
  921:                            Future => 'Future access',
  922:                         );
  923:     }
  924:     return (\%status_title,\@status_order);
  925: }
  926: 
  927: sub get_wasactive_text {
  928:     my $wasacctext = ' -- ';
  929:     if ($env{'form.currcat_0'} eq 'communities::0') {
  930:         $wasacctext .= &mt('where member access status was current ...');
  931:     } else {
  932:         $wasacctext .= &mt('where student access status was current ...');
  933:     }
  934:     return $wasacctext;
  935: }
  936: 
  937: sub search_official_courselist {
  938:     my ($domain,$numtitles,$codetitles) = @_;
  939:     my $instcode = &Apache::courseclassifier::instcode_search_str($domain,$numtitles,$codetitles);
  940:     my $showhidden;
  941:     if (&user_is_dc($domain)) {
  942:         $showhidden = $env{'form.showhidden'};
  943:     }
  944:     my %courses = 
  945:         &Apache::lonnet::courseiddump($domain,'.',1,$instcode,'.','.',undef,undef,
  946:                                       'Course',1,$env{'form.showselfenroll'},undef,
  947:                                       $showhidden,'coursecatalog');
  948:     return %courses;
  949: }
  950: 
  951: sub search_courselist {
  952:     my ($domain,$subcats) = @_;
  953:     my $cat_maxdepth = $env{'form.catalog_maxdepth'};
  954:     my $filter = $env{'form.currcat_'.$cat_maxdepth};
  955:     if (($filter eq '') && ($cat_maxdepth > 0)) {
  956:         my $shallower = $cat_maxdepth - 1;
  957:         $filter = $env{'form.currcat_'.$shallower};
  958:     }
  959:     my %courses;
  960:     my $filterstr;
  961:     if ($filter ne '') {
  962:         if ($env{'form.withsubcats'}) {
  963:             if (ref($subcats) eq 'HASH') {
  964:                 if (ref($subcats->{$filter}) eq 'ARRAY') {
  965:                     $filterstr = join('&',@{$subcats->{$filter}});
  966:                     if ($filterstr ne '') {
  967:                         $filterstr = $filter.'&'.$filterstr;
  968:                     }
  969:                 } else {
  970:                     $filterstr = $filter;
  971:                 }
  972:             } else {
  973:                 $filterstr = $filter;
  974:             }  
  975:         } else {
  976:             $filterstr = $filter; 
  977:         }
  978:         my ($showhidden,$typefilter);
  979:         if (&user_is_dc($domain)) {
  980:             $showhidden = $env{'form.showhidden'};
  981:         }
  982:         if ($env{'form.currcat_0'} eq 'communities::0') {
  983:             $typefilter = 'Community';
  984:         } elsif ($env{'form.currcat_0'} eq 'placement::0') {
  985:             $typefilter = 'Placement';
  986:         } else {
  987:             $typefilter = '.';
  988:         }
  989:         %courses = 
  990:             &Apache::lonnet::courseiddump($domain,'.',1,'.','.','.',undef,undef,
  991:                                           $typefilter,1,$env{'form.showselfenroll'},
  992:                                           $filterstr,$showhidden,'coursecatalog');
  993:     }
  994:     return %courses;
  995: }
  996: 
  997: sub print_course_listing {
  998:     my ($domain,$numtitles,$trails,$allitems,$subcats,$codetitles,$canviewall) = @_;
  999:     my $output;
 1000:     my %courses;
 1001:     my $knownuser = &user_is_known();
 1002:     my $canviewall = &canview_all();
 1003:     my $details = $env{'form.coursenum'};
 1004:     if (&user_is_dc($domain) || $canviewall) {
 1005:         if ($env{'form.showdetails'}) {
 1006:             $details = 1;
 1007:         }
 1008:     }
 1009:     if ($env{'form.coursenum'} ne '') {
 1010:         %courses = &Apache::lonnet::courseiddump($domain,'.',1,'.','.',
 1011:                                                  $env{'form.coursenum'},
 1012:                                                  undef,undef,'.',1);
 1013:         if (keys(%courses) == 0) {
 1014:             $output = '<p class="LC_error">';
 1015:             if ($env{'form.currcat_0'} eq 'communities::0') {
 1016:                 $output .= &mt('The courseID provided does not match a community in this domain.');
 1017:             } elsif ($env{'form.currcat_0'} eq 'placement::0') {
 1018:                 $output .= &mt('The courseID provided does not match a placement test in this domain.');
 1019:             } else {
 1020:                 $output .= &mt('The courseID provided does not match a course in this domain.');
 1021:             }
 1022:             $output .= '</p>';
 1023:             return $output;
 1024:         }
 1025:     } else {
 1026:         if ($env{'form.currcat_0'} eq 'instcode::0') {
 1027:             %courses = &search_official_courselist($domain,$numtitles,$codetitles);
 1028:         } else {
 1029:             %courses = &search_courselist($domain,$subcats);
 1030:         }
 1031:         if (keys(%courses) == 0) {
 1032:             $output = '<p class="LC_info">';
 1033:             if ($env{'form.currcat_0'} eq 'communities::0') {
 1034:                 $output .= &mt('No communities match the criteria you selected.');
 1035:             } elsif ($env{'form.currcat_0'} eq 'placement::0') {
 1036:                 $output .= &mt('No placement tests match the criteria you selected.');
 1037:             } else {
 1038:                 $output .= &mt('No courses match the criteria you selected.');
 1039:             }
 1040:             $output .= '</p>';
 1041:             return $output;
 1042:         }
 1043:         if (($knownuser) && (!$env{'form.showdetails'}) && (!&user_is_dc($domain)) && (!$canviewall)) {
 1044:             $output = '<b>'.&mt('Note for students:').'</b> '
 1045:                      .&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.')
 1046:                      .'<br /><br />';
 1047:         }
 1048:     }
 1049:     my $now = time;
 1050:     $output .= &construct_data_table($knownuser,$domain,\%courses,$details,undef,
 1051:                                      $now,$trails,$allitems,$canviewall);
 1052:     $output .= "\n".'<form name="linklaunch" method="post" action="">'.
 1053:                '<input type="hidden" name="backto" value="coursecatalog" />'.
 1054:                '<input type="hidden" name="courseid" value="" />'.
 1055:                &Apache::lonhtmlcommon::echo_form_input(['catalogfilter','courseid']).'</form>';
 1056:     return $output;
 1057: }
 1058: 
 1059: sub construct_data_table {
 1060:     my ($knownuser,$domain,$courses,$details,$usersections,$now,$trails,$allitems,$canviewall) = @_;
 1061:     my %sortname;
 1062:     if (($details eq '') || ($env{'form.showdetails'})) {
 1063:         $sortname{'Code'} = 'code';
 1064:         $sortname{'Categories'} = 'cats';
 1065:         $sortname{'Title'} = 'title';
 1066:         $sortname{'Owner & Co-owner(s)'} = 'owner';
 1067:     }
 1068:     my $output = &Apache::loncommon::start_data_table().
 1069:                  &Apache::loncommon::start_data_table_header_row();
 1070:     my @coltitles = ('Count');
 1071:     if ($env{'form.currcat_0'} eq 'instcode::0') {
 1072:         push(@coltitles,'Code');
 1073:     } else {
 1074:         push(@coltitles,'Categories');
 1075:     }
 1076:     push(@coltitles,('Sections','Crosslisted','Title','Owner & Co-owner(s)'));
 1077:     if (ref($usersections) eq 'HASH') {
 1078:        $coltitles[1] = 'Your Section';
 1079:     }
 1080:     foreach my $item (@coltitles) {
 1081:         $output .= '<th>';
 1082:         if (defined($sortname{$item})) {
 1083:             $output .= '<a href="javascript:changeSort('."'$sortname{$item}'".')">'.&mt($item).'</a>';
 1084:         } elsif ($item eq 'Count') {
 1085:             $output .= '&nbsp;&nbsp;';
 1086:         } else {
 1087:             $output .= &mt($item);
 1088:         }
 1089:         $output .= '</th>';
 1090:     }
 1091:     my (@fields,%fieldtitles,$wasactiveon);
 1092:     if ($knownuser || ($canviewall && $details)) {
 1093:         if ($details) {
 1094:             if ($env{'form.currcat_0'} eq 'communities::0') {
 1095:                 $output .= '<th>'.&mt('Default Access Dates for Members').'</th>'.
 1096:                            '<th>'.&mt('Member Counts').'</th>';
 1097:             } elsif ($env{'form.currcat_0'} eq 'placement::0') {
 1098:                 $output .=
 1099:                     '<th>'.&mt('Default Access Dates for Students').'</th>'.
 1100:                     '<th>'.&mt('Student Counts').'</th>';               
 1101:             } else {
 1102:                 $output .=
 1103:                     '<th>'.&mt('Default Access Dates for Students').'</th>'.
 1104:                     '<th>'.&mt('Student Counts').'</th>'.
 1105:                     '<th>'.&mt('Auto-enrollment of[_1]registered students','<br />').'</th>';
 1106:             }
 1107:             my ($titlesref,$orderref) = &get_statustitles();
 1108:             my @statuses;
 1109:             if (&user_is_dc($domain) || $canviewall) {
 1110:                 @statuses = &Apache::loncommon::get_env_multiple('form.showcounts');
 1111:                 if (grep(/^Previous$/,@statuses)) {
 1112:                     if ($env{'form.wasactive'} eq 'date') { 
 1113:                         $wasactiveon = 
 1114:                             &Apache::lonhtmlcommon::get_date_from_form('wasactiveon');
 1115:                     } else {
 1116:                         $wasactiveon = $env{'form.wasactive'};
 1117:                     }
 1118:                 }
 1119:                 if (ref($orderref) eq 'ARRAY') {
 1120:                     foreach my $status (@{$orderref}) {
 1121:                         if (grep(/^\Q$status\E$/,@statuses)) {
 1122:                             push(@fields,$status); 
 1123:                         }
 1124:                     }
 1125:                 }
 1126:             } else {
 1127:                 @fields = ('Active','Future');
 1128:             }
 1129:             foreach my $status (@fields) {
 1130:                 my $title;
 1131:                 if (ref($titlesref) eq 'HASH') {
 1132:                     $title = $titlesref->{$status};
 1133:                 }
 1134:                 unless ($title) {
 1135:                     $title = &mt($status);
 1136:                 }
 1137:                 $fieldtitles{$status} = $title;
 1138:             }
 1139:         } else {
 1140:             $output .= '<th>'.&mt('Details').'</th>';
 1141:         }
 1142:     }
 1143:     $output .= '<th>'.&mt('Self-enroll (if permitted)').'</th>';
 1144:     &Apache::loncommon::end_data_table_header_row();
 1145:     my (%numbers,%creditsum);
 1146:     my ($showcredits,$defofficial,$defunofficial,$deftextbook);
 1147:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
 1148:     unless (($env{'form.currcat_0'} eq 'communities::0') || ($env{'form.currcat_0'} eq 'placement::0')) {
 1149:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'} || $domdefaults{'textbookcredits'}) {
 1150:             $showcredits = 1;
 1151:             $defofficial = $domdefaults{'officialcredits'};
 1152:             $defunofficial = $domdefaults{'unofficialcredits'};
 1153:             $deftextbook = $domdefaults{'textbookcredits'};
 1154:         }
 1155:     }
 1156:     my %courseinfo = &build_courseinfo_hash($courses,$knownuser,$domain,$canviewall,$details,
 1157:                                             $usersections,\@fields,\%fieldtitles,
 1158:                                             $wasactiveon,\%numbers,\%creditsum,
 1159:                                             $showcredits,$defofficial,$defunofficial,$deftextbook);
 1160:     my %Sortby;
 1161:     foreach my $course (sort(keys(%{$courses}))) {
 1162:         if ($env{'form.sortby'} eq 'code') {
 1163:             push(@{$Sortby{$courseinfo{$course}{'code'}}},$course);
 1164:         } elsif ($env{'form.sortby'} eq 'cats') {
 1165:             push(@{$Sortby{$courseinfo{$course}{'categories'}}},$course);
 1166:         } elsif ($env{'form.sortby'} eq 'owner') {
 1167:             push(@{$Sortby{$courseinfo{$course}{'ownerlastnames'}}},$course);
 1168:         } else {
 1169:             my $clean_title = $courseinfo{$course}{'title'};
 1170:             $clean_title =~ s/\W+//g;
 1171:             if ($clean_title eq '') {
 1172:                 $clean_title = $courseinfo{$course}{'title'};
 1173:             }
 1174:             push(@{$Sortby{$clean_title}},$course);
 1175:         }
 1176:     }
 1177:     my @sorted_courses;
 1178:     if (($env{'form.sortby'} eq 'code') || ($env{'form.sortby'} eq 'owner') ||
 1179:         ($env{'form.sortby'} eq 'cats')) {
 1180:         @sorted_courses = sort(keys(%Sortby));
 1181:     } else {
 1182:         @sorted_courses = sort { lc($a) cmp lc($b) } (keys(%Sortby));
 1183:     }
 1184:     my $count = 1;
 1185:     my $totalsec = 0;
 1186:     foreach my $item (@sorted_courses) {
 1187:         foreach my $course (@{$Sortby{$item}}) {
 1188:             $output.=&Apache::loncommon::start_data_table_row(); 
 1189:             $output.=&courseinfo_row($courseinfo{$course},$knownuser,$details,
 1190:                                      \$count,$now,$course,$trails,$allitems,\%numbers,$canviewall);
 1191:             $output.=&Apache::loncommon::end_data_table_row();
 1192:         }
 1193:     }
 1194:     if (($knownuser || $canviewall) && ($count > 1) && $env{'form.showdetails'}) {
 1195:         if (&user_is_dc($domain) || $canviewall) {
 1196:             my %lt = &Apache::lonlocal::texthash (
 1197:                                                      'Active'   => 'Total current students',
 1198:                                                      'Future'   => 'Total future students',
 1199:                                                      'Previous' => 'Total previous students',
 1200:                                                      'courses'  => 'Total unique codes and courses without codes',
 1201:                                                      'sections' => 'Total sections',
 1202:                                                      'xlists'   => 'Total cross-listings',
 1203:                                                  );
 1204:             if ($showcredits) {
 1205:                 $lt{'cr_Active'} = &mt('Total current student credit hours');
 1206:                 $lt{'cr_Future'} = &mt('Total future student credit hours');
 1207:                 $lt{'cr_Previous'} = &mt('Total previous student credit hours');
 1208:             }
 1209:             if ($env{'form.currcat_0'} eq 'communities::0') {
 1210:                 $lt{'courses'} = &mt('Total communities');
 1211:                 $lt{'Active'} = &mt('Total current members'); 
 1212:                 $lt{'Future'} = &mt('Total future members');
 1213:                 $lt{'Previous'} = &mt('Total previous members');
 1214:             } elsif ($env{'form.currcat_0'} eq 'placement::0') {
 1215:                 $lt{'courses'} = &mt('Total placement tests');
 1216:             }
 1217:             my $colspan = 8;
 1218:             if ($showcredits) {
 1219:                 $colspan = 4;
 1220:             }
 1221:             $output .= '<tr class="LC_footer_row">'.
 1222:                        '<td colspan="2">&nbsp;</td>'.
 1223:                        '<td colspan="'.$colspan.'">'.
 1224:                        '<table border="0">';
 1225:             foreach my $item ('courses','sections','xlists') {
 1226:                 $output .= '<tr>'.
 1227:                            '<td>'.$lt{$item}.'</td><td>&nbsp;</td>'.
 1228:                            '<td align="right">'.$numbers{$item}.'</td>'.
 1229:                            '</tr>'."\n";
 1230:             }
 1231:             if (@fields > 0) { 
 1232:                 foreach my $status (@fields) {
 1233:                     $output .= '<tr>'.
 1234:                                '<td>'.$lt{$status}.'</td><td>&nbsp;</td>'.
 1235:                                '<td align="right">'.$numbers{$status}.'</td>'.
 1236:                                '</tr>'."\n";
 1237:                 }
 1238:             }
 1239:             $output .= '</table></td>';
 1240:             if ($showcredits) {
 1241:                 $output .= '<td colspan="'.$colspan.'" valign="bottom"><table>';
 1242:                 foreach my $status (@fields) {
 1243:                     $output .= '<tr>'.
 1244:                                '<td>'.$lt{'cr_'.$status}.'</td><td>&nbsp;</td>'.
 1245:                                '<td align="right">'.$creditsum{$status}.'</td></tr>';
 1246:                 }
 1247:                 $output .= '</table></td></tr>';
 1248:             }
 1249:         }
 1250:     }
 1251:     $output .= &Apache::loncommon::end_data_table();
 1252:     return $output;
 1253: }
 1254: 
 1255: sub build_courseinfo_hash {
 1256:     my ($courses,$knownuser,$domain,$canviewall,$details,$usersections,$fields,$fieldtitles,
 1257:         $wasactiveon,$numbers,$creditsum,$showcredits,$defofficial,$defunofficial) = @_;
 1258:     my %courseinfo;
 1259:     my $now = time;
 1260:     my $gettotals;
 1261:     if ((keys(%{$courses}) > 0) && (&user_is_dc($domain) || $canviewall) && ($details)) {
 1262:         $gettotals = 1;
 1263:     }
 1264:     my (%uniquecodes,$nocodes,$defcreds);
 1265:     foreach my $course (keys(%{$courses})) {
 1266:         my $descr;
 1267:         if (ref($courses->{$course}) eq 'HASH') {
 1268:             $descr = $courses->{$course}{'description'}; 
 1269:         }
 1270:         my $cleandesc=&HTML::Entities::encode($descr,'<>&"');
 1271:         $cleandesc=~s/'/\\'/g;
 1272:         $cleandesc =~ s/^\s+//;
 1273:         my ($cdom,$cnum)=split(/\_/,$course);
 1274:         my ($instcode,$singleowner,$ttype,$selfenroll_types,
 1275:             $selfenroll_start,$selfenroll_end,@owners,%ownernames,$categories);
 1276:         if (ref($courses->{$course}) eq 'HASH') {
 1277:             $descr = $courses->{$course}{'description'};
 1278:             $instcode =  $courses->{$course}{'inst_code'};
 1279:             $singleowner = $courses->{$course}{'owner'};
 1280:             $ttype =  $courses->{$course}{'type'};
 1281:             $selfenroll_types = $courses->{$course}{'selfenroll_types'};
 1282:             $selfenroll_start = $courses->{$course}{'selfenroll_start_date'};
 1283:             $selfenroll_end = $courses->{$course}{'selfenroll_end_date'};
 1284:             $categories = $courses->{$course}{'categories'};
 1285:             push(@owners,$singleowner);
 1286:             if ($courses->{$course}{'co-owners'} ne '') {
 1287:                 foreach my $item (split(/,/,$courses->{$course}{'co-owners'})) {
 1288:                     push(@owners,$item);
 1289:                 }
 1290:             }
 1291:         }
 1292:         if ($instcode ne '') {
 1293:             $uniquecodes{$instcode} = 1;
 1294:         } else {
 1295:             $nocodes ++;
 1296:         } 
 1297:         foreach my $owner (@owners) {
 1298:             my ($ownername,$ownerdom); 
 1299:             if ($owner =~ /:/) {
 1300:                 ($ownername,$ownerdom) = split(/:/,$owner);
 1301:             } else {
 1302:                 $ownername = $owner;
 1303:                 if ($owner ne '') {
 1304:                     $ownerdom = $cdom;
 1305:                 }
 1306:             }
 1307:             if ($ownername ne '' && $ownerdom ne '') {
 1308:                 my %namehash=&Apache::loncommon::getnames($ownername,$ownerdom);
 1309:                 $ownernames{$ownername.':'.$ownerdom} = \%namehash; 
 1310:             }
 1311:         }
 1312:         $courseinfo{$course}{'cdom'} = $cdom;
 1313:         $courseinfo{$course}{'cnum'} = $cnum;
 1314:         $courseinfo{$course}{'code'} = $instcode;
 1315:         my @lastnames;
 1316:         foreach my $owner (keys(%ownernames)) {
 1317:             if (ref($ownernames{$owner}) eq 'HASH') {
 1318:                 push(@lastnames,$ownernames{$owner}{'lastname'});
 1319:             }
 1320:         }
 1321:         $courseinfo{$course}{'ownerlastnames'} = join(', ',sort(@lastnames));
 1322:         $courseinfo{$course}{'title'} = $cleandesc;
 1323:         $courseinfo{$course}{'owner'} = $singleowner;
 1324:         $courseinfo{$course}{'selfenroll_types'} = $selfenroll_types;
 1325:         $courseinfo{$course}{'selfenroll_start'} = $selfenroll_start;
 1326:         $courseinfo{$course}{'selfenroll_end'} = $selfenroll_end;
 1327:         $courseinfo{$course}{'categories'} = $categories;
 1328: 
 1329:         my %coursehash = &Apache::lonnet::dump('environment',$cdom,$cnum);
 1330:         my @classids;
 1331:         my @crosslistings;
 1332:         my ($seclist,$numsec) = 
 1333:             &identify_sections($coursehash{'internal.sectionnums'});
 1334:         $courseinfo{$course}{'seclist'} = $seclist;
 1335:         my ($xlist_items,$numxlist) = 
 1336:             &identify_sections($coursehash{'internal.crosslistings'});
 1337:         if (ref($numbers) eq 'HASH') {
 1338:             $numbers->{'sections'} += $numsec; 
 1339:             $numbers->{'xlists'} += $numxlist;  
 1340:         }
 1341:         my $showsyllabus = 1; # default is to include a syllabus link
 1342:         if (defined($coursehash{'showsyllabus'})) {
 1343:             $showsyllabus = $coursehash{'showsyllabus'};
 1344:         }
 1345:         $courseinfo{$course}{'showsyllabus'} = $showsyllabus;
 1346:         if ($showcredits) {
 1347:             if ($coursehash{'internal.defaultcredits'}) {
 1348:                 $courseinfo{$course}{'defaultcredits'} = $coursehash{'internal.defaultcredits'};
 1349:             } elsif ($instcode ne '') {
 1350:                 $courseinfo{$course}{'defaultcredits'} = $defofficial;
 1351:             } else {
 1352:                 $courseinfo{$course}{'defaultcredits'} = $defunofficial;
 1353:             }
 1354:             $defcreds = $courseinfo{$course}{'defaultcredits'};
 1355:         }
 1356:         if (((defined($env{'form.coursenum'}) && ($cnum eq $env{'form.coursenum'}))) ||
 1357:             (($knownuser || $canviewall) && ($details == 1))) {
 1358:             my $milestone;
 1359:             if ($wasactiveon eq 'accessend') {
 1360:                 if ($coursehash{'default_enrollment_end_date'}) {
 1361:                     $milestone = $coursehash{'default_enrollment_end_date'};
 1362:                 } else {
 1363:                     $milestone = time;
 1364:                 }
 1365:             } elsif ($wasactiveon eq 'enrollend') {
 1366:                 if ($coursehash{'internal.autoend'}) {
 1367:                     $milestone = $coursehash{'internal.autoend'};
 1368:                 } else {
 1369:                     $milestone = time;
 1370:                 }
 1371:             } else {
 1372:                 $milestone = $wasactiveon;
 1373:             }
 1374:             $courseinfo{$course}{'counts'} =  
 1375:                 &count_students($cdom,$cnum,$numsec,$fields,$fieldtitles,$gettotals,
 1376:                                 $numbers,$creditsum,$showcredits,$defcreds,$milestone);
 1377:             if ($instcode ne '') {
 1378:                 $courseinfo{$course}{'autoenrollment'} =
 1379:                     &autoenroll_info(\%coursehash,$now,$seclist,$xlist_items,
 1380:                                      $instcode,\@owners,$cdom,$cnum);
 1381:             }
 1382:             my $startaccess = '';
 1383:             my $endaccess = '';
 1384:             my $accessdates;
 1385:             if ( defined($coursehash{'default_enrollment_start_date'}) ) {
 1386:                 $startaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_start_date'});
 1387:             }
 1388:             if ( defined($coursehash{'default_enrollment_end_date'}) ) {
 1389:                 $endaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_end_date'});
 1390:                 if ($coursehash{'default_enrollment_end_date'} == 0) {
 1391:                     $endaccess = &mt('No ending date');
 1392:                 }
 1393:             }
 1394:             if ($startaccess) {
 1395:                 $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$startaccess).'<br />';
 1396:             }
 1397:             if ($endaccess) {
 1398:                 $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$endaccess).'<br />';
 1399:             }
 1400:             if (($selfenroll_types ne '') && 
 1401:                 ($selfenroll_end > 0 && $selfenroll_end > $now)) {
 1402:                 my ($selfenroll_start_access,$selfenroll_end_access);
 1403:                 if (($coursehash{'default_enrollment_start_date'} ne 
 1404:                      $coursehash{'internal.selfenroll_start_access'}) ||
 1405:                    ($coursehash{'default_enrollment_end_date'} ne 
 1406:                     $coursehash{'internal.selfenroll_end_access'})) {
 1407:                     if ( defined($coursehash{'internal.selfenroll_start_access'}) ) {
 1408:                         $selfenroll_start_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_start_access'});
 1409:                     }
 1410:                     if ( defined($coursehash{'default_enrollment_end_date'}) ) {
 1411:                         $selfenroll_end_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_end_access'});
 1412:                         if ($coursehash{'internal.selfenroll_end_access'} == 0) {
 1413:                             $selfenroll_end_access = &mt('No ending date');
 1414:                         }
 1415:                     }
 1416:                     if ($selfenroll_start_access || $selfenroll_end_access) {
 1417:                         $accessdates .= '<br/><br /><i>'.&mt('Self-enrollers:').'</i><br />';
 1418:                         if ($selfenroll_start_access) {
 1419:                             $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$selfenroll_start_access).'<br />';
 1420:                         }
 1421:                         if ($selfenroll_end_access) {
 1422:                             $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$selfenroll_end_access).'<br />';
 1423:                         }
 1424:                     }
 1425:                 }
 1426:             }
 1427:             $courseinfo{$course}{'access'} = $accessdates;
 1428:         }
 1429:         if ($xlist_items eq '') {
 1430:             $xlist_items = &mt('No');
 1431:         }
 1432:         $courseinfo{$course}{'xlist'} = $xlist_items;
 1433:     }
 1434:     if (ref($numbers) eq 'HASH') {
 1435:         $numbers->{'courses'} = $nocodes + scalar(keys(%uniquecodes));
 1436:     }
 1437:     return %courseinfo;
 1438: }
 1439: 
 1440: sub count_students {
 1441:     my ($cdom,$cnum,$numsec,$fieldsref,$titlesref,$getcounts,$numbers,$creditsum,
 1442:         $showcredits,$defcreds,$wasactiveon) = @_;
 1443:     my $countslist = '<span class="LC_nobreak">'.
 1444:                      &mt('[quant,_1,section,sections,No sections]',$numsec).'</span>';
 1445:     my (@fields,%titles,$showexpired);
 1446:     if ((ref($fieldsref) eq 'ARRAY') && (ref($titlesref) eq 'HASH') &&
 1447:         (ref($numbers) eq 'HASH')) {
 1448:         @fields = @{$fieldsref};
 1449:         %titles = %{$titlesref};
 1450:         if (grep(/^Previous$/,@fields)) {
 1451:             $showexpired = 1;
 1452:         }
 1453:     } else {
 1454:         return;
 1455:     }
 1456:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
 1457:     my (%student_count,%credit_count);
 1458:     %student_count = (
 1459:                            Active   => 0,
 1460:                            Future   => 0,
 1461:                            Previous => 0,
 1462:                      );
 1463:     if ($showcredits) {
 1464:         %credit_count = (
 1465:                           Active   => 0,
 1466:                           Future   => 0,
 1467:                           Previous => 0,
 1468:                         );
 1469:     }
 1470:     my %idx;
 1471:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
 1472:     $idx{'end'}    = &Apache::loncoursedata::CL_END();
 1473:     $idx{'credits'} = &Apache::loncoursedata::CL_CREDITS();
 1474:     while (my ($student,$data) = each(%$classlist)) {
 1475:         my $status = $data->[$idx{'status'}];
 1476:         my $credits = $data->[$idx{'credits'}];
 1477:         if ($credits eq '') {
 1478:             $credits = $defcreds;  
 1479:         }
 1480:         if ($status eq 'Expired') {
 1481:             if (($showexpired) &&
 1482:                 ($data->[$idx{'end'}] >= $wasactiveon)) {
 1483:                 $student_count{'Previous'} ++;
 1484:                 if ($showcredits) {
 1485:                     $credit_count{'Previous'} += $credits; 
 1486:                 }
 1487:             }
 1488:         } else {
 1489:             $student_count{$status} ++;
 1490:             if ($showcredits) {
 1491:                 $credit_count{$status} += $credits;
 1492:             }
 1493:         }
 1494:     }
 1495:     if (@fields) {
 1496:         $countslist .= ':<br />';
 1497:         foreach my $status (@fields) {
 1498:             $countslist .= '<span class="LC_nobreak">'.$titles{$status}.': '.
 1499:                            $student_count{$status}.'</span><br />';
 1500:             $numbers->{$status} += $student_count{$status};
 1501:             if ($showcredits) {
 1502:                 $creditsum->{$status} += $credit_count{$status};
 1503:             }
 1504:         }
 1505:     }
 1506:     return $countslist;
 1507: }
 1508: 
 1509: sub courseinfo_row {
 1510:     my ($info,$knownuser,$details,$countref,$now,$course,$trails,$allitems,$numbers,$canviewall) = @_;
 1511:     my ($cdom,$cnum,$title,$ownerlast,$code,$owner,$seclist,$xlist_items,
 1512:         $accessdates,$showsyllabus,$counts,$autoenrollment,$output,$categories);
 1513:     if (ref($info) eq 'HASH') {
 1514:         $cdom = $info->{'cdom'};
 1515:         $cnum = $info->{'cnum'};
 1516:         $title = $info->{'title'};
 1517:         $ownerlast = $info->{'ownerlastnames'};
 1518:         $code = $info->{'code'};
 1519:         $owner = $info->{'owner'};
 1520:         $seclist = $info->{'seclist'};
 1521:         $xlist_items = $info->{'xlist'};
 1522:         $accessdates = $info->{'access'};
 1523:         $counts = $info->{'counts'};
 1524:         $autoenrollment = $info->{'autoenrollment'};
 1525:         $showsyllabus = $info->{'showsyllabus'};
 1526:         $categories = $info->{'categories'};
 1527:     } else {
 1528:         $output = '<td colspan="8">'.&mt('No information available for [_1].',
 1529:                                          $code).'</td>';
 1530:         return $output;
 1531:     }
 1532:     $output .= '<td>'.$$countref.'</td>';
 1533:     if ($env{'form.currcat_0'} eq 'instcode::0') {
 1534:         $output .= '<td>'.$code.'</td>';
 1535:     } else {
 1536:         my ($categorylist,@cats);
 1537:         if ($categories ne '') {
 1538:             @cats = split('&',$categories);
 1539:         }
 1540:         if ((ref($trails) eq 'ARRAY') && (ref($allitems) eq 'HASH')) {
 1541:             my @categories = map { $trails->[$allitems->{$_}]; } @cats;
 1542:             $categorylist = join('<br />',@categories);
 1543:         }
 1544:         if ($categorylist eq '') {
 1545:             $categorylist = '&nbsp;';
 1546:         }
 1547:         $output .= '<td>'.$categorylist.'</td>';
 1548:     }
 1549:     $output .= '<td>'.$seclist.'</td>'.
 1550:                '<td>'.$xlist_items.'</td>'.
 1551:                '<td>'.$title.'&nbsp;<font size="-2">';
 1552:     if ($showsyllabus) {
 1553:         $output .= '<a href="javascript:ToSyllabus('."'$cdom','$cnum'".')">'.&mt('Syllabus').'</a>';
 1554:     } else {
 1555:         $output .= '&nbsp;';
 1556:     }
 1557:     $output .= '</font></td>'.
 1558:                '<td>'.$ownerlast.'</td>';
 1559:     if (($knownuser) || ($canviewall && $details)) {
 1560:         if ($details) {
 1561:             $output .=
 1562:                 '<td>'.$accessdates.'</td>'.
 1563:                 '<td>'.$counts.'</td>';
 1564:             unless (($env{'form.currcat_0'} eq 'communities::0') || ($env{'form.currcat_0'} eq 'placement::0')) {
 1565:                 $output .= '<td>'.$autoenrollment.'</td>';
 1566:             }
 1567:         } else {
 1568:             $output .= "<td><a href=\"javascript:setCourseId('$cnum')\">".&mt('Show more details').'</a></td>';
 1569:         }
 1570:     }
 1571:     my $selfenroll = &selfenroll_status($info,$course);
 1572:     if ($selfenroll) {
 1573:         $output .= '<td>'.$selfenroll.'</td>';
 1574:     } else {
 1575:         $output .= '<td>&nbsp;</td>';
 1576:     }
 1577:     $$countref ++;
 1578:     return $output;
 1579: }
 1580: 
 1581: sub selfenroll_status {
 1582:     my ($info,$course) = @_;
 1583:     my $now = time;
 1584:     my $output;
 1585:     if (ref($info) eq 'HASH') {
 1586:         if ($info->{'selfenroll_types'}) {
 1587:             my $showstart = &Apache::lonlocal::locallocaltime($info->{'selfenroll_start'});
 1588:             my $showend = &Apache::lonlocal::locallocaltime($info->{'selfenroll_end'});
 1589:             if (($info->{'selfenroll_end'} > 0) && ($info->{'selfenroll_end'} > $now)) {
 1590:                 if (($info->{'selfenroll_start'} > 0) && ($info->{'selfenroll_start'} > $now)) {
 1591:                     $output = &mt('Starts: [_1]','<span class="LC_cusr_emph">'.$showstart.'</span>').'<br />'.&mt('Ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>');
 1592:                 } else {
 1593:                     $output = '<a href="javascript:ToSelfenroll('."'$course'".')">'.
 1594:                               &mt('Enroll in course').'</a><br />';
 1595:                     if ($info->{'selfenroll_end'} == 0) {
 1596:                         $output .= &mt('Available permanently');
 1597:                     } elsif ($info->{'selfenroll_end'} > $now) {
 1598:                         $output .= &mt('Self-enrollment ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>');
 1599:                     }
 1600:                 }
 1601:             }
 1602:         }
 1603:     }
 1604:     return $output;
 1605: }
 1606: 
 1607: sub identify_sections {
 1608:     my ($seclist) = @_;
 1609:     my @secnums;
 1610:     if ($seclist =~ /,/) {
 1611:         my @sections = split(/,/,$seclist);
 1612:         foreach my $sec (@sections) {
 1613:             $sec =~ s/:[^:]*$//;
 1614:             push(@secnums,$sec);
 1615:         }
 1616:     } else {
 1617:         if ($seclist =~ m/^([^:]+):/) {
 1618:             my $sec = $1;
 1619:             if (!grep(/^\Q$sec\E$/,@secnums)) {
 1620:                 push(@secnums,$sec);
 1621:             }
 1622:         }
 1623:     }
 1624:     @secnums = sort {$a <=> $b} @secnums;
 1625:     $seclist = join(', ',@secnums);
 1626:     my $numsec = @secnums;
 1627:     return ($seclist,$numsec);
 1628: }
 1629: 
 1630: sub get_valid_classes {
 1631:     my ($seclist,$xlist_items,$crscode,$owners,$cdom,$cnum) = @_;
 1632:     my $response;
 1633:     my %validations;
 1634:     @{$validations{'sections'}} = ();
 1635:     @{$validations{'xlists'}} = ();
 1636:     my $totalitems = 0;
 1637:     if ($seclist) {
 1638:         foreach my $sec (split(/, /,$seclist)) {
 1639:             my $class = $crscode.$sec;
 1640:             if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
 1641: 							 $class) eq 'ok') {
 1642:                 if (!grep(/^\Q$sec$\E/,@{$validations{'sections'}})) {
 1643:                     push(@{$validations{'sections'}},$sec);
 1644:                     $totalitems ++;
 1645:                 }
 1646:             }
 1647:         }
 1648:     }
 1649:     if ($xlist_items) {
 1650:         foreach my $item (split(/, /,$xlist_items)) {
 1651:             if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
 1652: 							 $item) eq 'ok') {
 1653:                 if (!grep(/^\Q$item$\E/,@{$validations{'xlists'}})) {
 1654:                     push(@{$validations{'xlists'}},$item);
 1655:                     $totalitems ++;
 1656:                 }
 1657:             }
 1658:         }
 1659:     }
 1660:     if ($totalitems > 0) {
 1661:         if (@{$validations{'sections'}}) {
 1662:             $response = &mt('Sections:').' '.
 1663:                         join(', ',@{$validations{'sections'}}).'<br />';
 1664:         }
 1665:         if (@{$validations{'xlists'}}) {
 1666:             $response .= &mt('Courses:').' '.
 1667:                         join(', ',@{$validations{'xlists'}});
 1668:         }
 1669:     }
 1670:     return $response;
 1671: }
 1672: 
 1673: sub autoenroll_info {
 1674:     my ($coursehash,$now,$seclist,$xlist_items,$code,$owners,$cdom,$cnum) = @_;
 1675:     my $autoenrolldates = &mt('Not enabled');
 1676:     if (defined($coursehash->{'internal.autoadds'}) && $coursehash->{'internal.autoadds'} == 1) {
 1677:         my ($autostart,$autoend);
 1678:         if ( defined($coursehash->{'internal.autostart'}) ) {
 1679:             $autostart = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autostart'});
 1680:         }
 1681:         if ( defined($coursehash->{'internal.autoend'}) ) {
 1682:             $autoend = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autoend'});
 1683:         }
 1684:         if ($coursehash->{'internal.autostart'} > $now) {
 1685:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
 1686:                 $autoenrolldates = &mt('Not enabled');
 1687:             } else {
 1688:                 my $valid_classes = 
 1689:                    &get_valid_classes($seclist,$xlist_items,$code,
 1690:                                       $owners,$cdom,$cnum);
 1691:                 if ($valid_classes ne '') {
 1692:                     $autoenrolldates = &mt('Not enabled').'<br />'
 1693:                                       .&mt('Starts: [_1]',$autostart)
 1694:                                       .'<br />'.$valid_classes;
 1695:                 }
 1696:             }
 1697:         } else {
 1698:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
 1699:                 $autoenrolldates = &mt('Not enabled').'<br />'
 1700:                                   .&mt('Ended: [_1]',$autoend);
 1701:             } else {
 1702:                 my $valid_classes = &get_valid_classes($seclist,$xlist_items,
 1703:                                                        $code,$owners,$cdom,$cnum);
 1704:                 if ($valid_classes ne '') {
 1705:                     $autoenrolldates = &mt('Currently enabled').'<br />'.
 1706:                                        $valid_classes;
 1707:                 }
 1708:             }
 1709:         }
 1710:     }
 1711:     return $autoenrolldates;
 1712: }
 1713: 
 1714: sub user_is_known {
 1715:     my $known = 0;
 1716:     if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public' 
 1717:         && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
 1718:         $known = 1;
 1719:     }
 1720:     return $known;
 1721: }
 1722: 
 1723: 1;

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