File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.88.2.4: download - view: text, annotated - select for diffs
Fri Jul 19 13:18:27 2019 UTC (5 years ago) by raeburn
Branches: version_2_11_X
- For 2.11
  Backport 1.96

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

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