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

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

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