File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.97: download - view: text, annotated - select for diffs
Sun Dec 16 03:17:07 2018 UTC (5 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Unused after changes in rev. 1.95

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

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