Annotation of loncom/interface/coursecatalog.pm, revision 1.102

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

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