File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.93: download - view: text, annotated - select for diffs
Mon Aug 7 20:22:12 2017 UTC (6 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Trust Settings
  Check if action is permissible based on trust settings for:
  catalog, domroles, enroll, reqcrs, msg, othcoau, or coaurem for current context.

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

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