File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.94: download - view: text, annotated - select for diffs
Tue Aug 8 15:32:30 2017 UTC (6 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Trust settings.
 - Use more versatile lonnet::will_trust() to determine if session-hosting
   server has catalalog access, according to domain's trust settings.
 - Eliminate lonnet::notcallable() -- no longer used.

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

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