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

1.17      albertel    1: # The LearningOnline Network with CAPA
                      2: # Handler for displaying the course catalog interface
                      3: #
1.58.4.9! raeburn     4: # $Id: coursecatalog.pm,v 1.58.4.8 2011/10/03 01:35:46 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;
                     41: 
                     42: sub handler {
                     43:     my ($r) = @_;
                     44:     &Apache::loncommon::content_type($r,'text/html');
                     45:     $r->send_http_header;
                     46:     if ($r->header_only) {
                     47:         return OK;
                     48:     }
1.21      albertel   49:     my $handle = &Apache::lonnet::check_for_valid_session($r);
1.8       raeburn    50:     my $lonidsdir=$r->dir_config('lonIDsDir');
1.21      albertel   51:     if ($handle ne '') {
1.8       raeburn    52:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
                     53:     }
1.1       raeburn    54:     &Apache::lonacc::get_posted_cgi($r);
                     55:     &Apache::lonlocal::get_language_handle($r);
1.38      raeburn    56:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                     57:                                             ['sortby','showdom']);
1.28      raeburn    58: 
                     59:     my $codedom = &Apache::lonnet::default_login_domain();
1.19      raeburn    60: 
                     61:     if (($env{'user.domain'} ne '') && ($env{'user.domain'} ne 'public')) { 
                     62:         $codedom = $env{'user.domain'};
                     63:         if ($env{'request.role.domain'} ne '') {
                     64:             $codedom = $env{'request.role.domain'};
                     65:         }
                     66:     }
1.7       raeburn    67:     my $formname = 'coursecatalog';
1.28      raeburn    68:     if ($env{'form.showdom'} ne '') {
1.58.4.9! raeburn    69:         $env{'form.showdom'} = &LONCAPA::clean_domain($env{'form.showdom'});
1.28      raeburn    70:         if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
                     71:             $codedom = $env{'form.showdom'};
1.58.4.9! raeburn    72:         } else {
        !            73:             $env{'form.showdom'} = '';
1.28      raeburn    74:         }
                     75:     }
1.20      albertel   76:     my $domdesc = &Apache::lonnet::domain($codedom,'description');
1.7       raeburn    77:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.35      raeburn    78: 
1.58.4.9! raeburn    79:     if ($env{'form.catalog_maxdepth'} ne '') {
        !            80:         $env{'form.catalog_maxdepth'} =~ s{\D}{}g;
        !            81:     }
        !            82: 
1.35      raeburn    83:     my %domconfig =
                     84:         &Apache::lonnet::get_dom('configuration',['coursecategories'],$codedom);
1.36      raeburn    85:     my (@cats,@trails,%allitems,%idx,@jsarray,%subcathash,$cathash);
1.35      raeburn    86:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                     87:         $cathash = $domconfig{'coursecategories'}{'cats'};
                     88:     } else {
                     89:         $cathash = {};
                     90:     }
1.36      raeburn    91:     my $subcats;
                     92:     if ($env{'form.withsubcats'}) {
                     93:         $subcats = \%subcathash;
                     94:     }
1.35      raeburn    95:     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems,
1.36      raeburn    96:                                            \%idx,\@jsarray,$subcats);
1.58.4.2  raeburn    97:     my ($numtitles,@codetitles);
1.8       raeburn    98:     if ($env{'form.coursenum'} ne '' && &user_is_known()) {
1.58.4.2  raeburn    99:         &course_details($r,$codedom,$formname,$domdesc,\@trails,\%allitems,\@codetitles);
1.7       raeburn   100:     } else {
1.46      raeburn   101:         my ($catlinks,$has_subcats,$selitem) = &category_breadcrumbs($codedom,@cats);
1.28      raeburn   102:         my $catjs = <<"ENDSCRIPT";
                    103: 
                    104: function setCatDepth(depth) {
                    105:     document.coursecats.catalog_maxdepth.value = depth;
1.36      raeburn   106:     if (depth == '') {
                    107:         document.coursecats.currcat_0.value = '';
                    108:     }
1.28      raeburn   109:     document.coursecats.submit();
                    110:     return;
                    111: }
                    112: 
1.33      raeburn   113: function changeSort(caller) {
                    114:     document.$formname.sortby.value = caller;
                    115:     document.$formname.submit();
                    116: }
1.40      raeburn   117: 
1.33      raeburn   118: function setCourseId(caller) {
                    119:     document.$formname.coursenum.value = caller;
                    120:     document.$formname.submit();
1.37      raeburn   121: }
                    122: 
                    123: ENDSCRIPT
1.41      raeburn   124:         $catjs .= &courselink_javascript(); 
1.28      raeburn   125:         if ($env{'form.currcat_0'} eq 'instcode::0') {
                    126:             $numtitles = &instcode_course_selector($r,$codedom,$formname,$domdesc,
1.58.4.2  raeburn   127:                                                    $catlinks,$catjs,\@codetitles);
1.28      raeburn   128:             if ($env{'form.state'} eq 'listing') {
1.58.4.2  raeburn   129:                 $r->print(&print_course_listing($codedom,$numtitles,undef,undef,undef,
                    130:                                                 \@codetitles));
1.28      raeburn   131:             }
                    132:         } else {
                    133:             my (%add_entries);
1.50      raeburn   134:             my ($currdepth,$deeper) = &get_depth_values();
1.46      raeburn   135:             if ($selitem) {
1.50      raeburn   136:                 my $alert = &mt('Choose a subcategory to display');
                    137:                 if (!$deeper) {
                    138:                     $alert = &mt('Choose a category to display');
                    139:                 }
1.46      raeburn   140:                 $catjs .= <<ENDJS;
                    141: function check_selected() {
                    142:     if (document.coursecats.$selitem.options[document.coursecats.$selitem.selectedIndex].value == "") {
1.50      raeburn   143:         alert('$alert');
1.46      raeburn   144:         return false;
                    145:     }
                    146: }
                    147: ENDJS
                    148:             }
1.28      raeburn   149:             $catjs = '<script type="text/javascript">'."\n".$catjs."\n".'</script>';
                    150:             &cat_header($r,$codedom,$catjs,\%add_entries,$catlinks);
                    151:             if ($env{'form.currcat_0'} ne '') {
1.33      raeburn   152:                 $r->print('<form name="'.$formname.
                    153:                           '" method="post" action="/adm/coursecatalog">'.
1.36      raeburn   154:                           &additional_filters($codedom,$has_subcats)."\n");
1.33      raeburn   155:                 $r->print('<input type="hidden" name="catalog_maxdepth" value="'.
                    156:                           $deeper.'" />'."\n");
                    157:                 for (my $i=0; $i<$deeper; $i++) {
                    158:                     $r->print('<input type="hidden" name="currcat_'.$i.'" value="'.$env{'form.currcat_'.$i}.'" />'."\n");
                    159:                 }
1.57      raeburn   160:                 my $display_button;
                    161:                 if ($env{'form.currcat_0'} eq 'communities::0') {
                    162:                     $display_button = &mt('Display communities');
                    163:                 } else {
                    164:                     $display_button = &mt('Display courses');
                    165:                 }
1.33      raeburn   166:                 $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
                    167:                           '<input type="hidden" name="sortby" value="" />'."\n".
                    168:                           '<input type="hidden" name="state" value="listing" />'."\n".
                    169:                           '<input type="hidden" name="showdom" value="'.
                    170:                           $env{'form.showdom'}.'" />'.
                    171:                           '<input type="submit" name="catalogfilter" value="'.
1.57      raeburn   172:                           $display_button.'" /></form><br /><br />');
1.33      raeburn   173:             }
                    174:             if ($env{'form.state'} eq 'listing') {
1.58.4.2  raeburn   175:                 $r->print(&print_course_listing($codedom,undef,\@trails,\%allitems,$subcats,\@codetitles));
1.28      raeburn   176:             }
1.18      raeburn   177:         }
1.7       raeburn   178:     }
1.32      raeburn   179:     $r->print('<br />'.&Apache::loncommon::end_page());
1.7       raeburn   180:     return OK;
                    181: }
                    182: 
                    183: sub course_details {
1.58.4.2  raeburn   184:     my ($r,$codedom,$formname,$domdesc,$trails,$allitems,$codetitles) = @_;
1.7       raeburn   185:     my $output;
                    186:     my %add_entries = (topmargin    => "0",
                    187:                        marginheight => "0",);
1.40      raeburn   188:     my $js = '<script type="text/javascript">'."\n".
1.41      raeburn   189:              &courselink_javascript().'</script>'."\n";
1.7       raeburn   190:     my $start_page =
1.56      bisitz    191:         &Apache::loncommon::start_page('Course/Community Catalog',$js,
1.7       raeburn   192:                                            {
                    193:                                              'add_entries' => \%add_entries,
                    194:                                              'no_inline_link'   => 1,});
                    195:     $r->print($start_page);
1.19      raeburn   196:     if ($env{'form.numtitles'} > 0) {
                    197:         &Apache::lonhtmlcommon::add_breadcrumb
                    198:                 ({href=>"/adm/coursecatalog",
1.56      bisitz    199:                   text=>"Course/Community Catalog"});
1.19      raeburn   200:     }
1.58.4.1  raeburn   201:     my $brtextone = 'Course listing'; 
                    202:     my $brtexttwo = 'Course details';
                    203:     if ($env{'form.currcat_0'} eq 'communities::0') {
                    204:         $brtextone = 'Community listing';
                    205:         $brtexttwo = 'Community details';
                    206:     }
1.7       raeburn   207:     &Apache::lonhtmlcommon::add_breadcrumb
1.19      raeburn   208:              ({href=>"javascript:document.$formname.submit()",
1.58.4.1  raeburn   209:               text=>$brtextone},
                    210:              {text=>$brtexttwo});
                    211:     $r->print('<br />');
                    212:     if ($env{'form.currcat_0'} eq 'communities::0') {
                    213:         $r->print(&mt('Detailed community information:'));
                    214:     } else {
                    215:         $r->print(&mt('Detailed course information:'));
                    216:     }
                    217:     $r->print('<br /><br />'.
1.58.4.2  raeburn   218:               &print_course_listing($codedom,undef,$trails,$allitems,undef,$codetitles).
1.35      raeburn   219:               '<br /><br />');
1.40      raeburn   220:     $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
1.58.4.1  raeburn   221:               '<a href = "javascript:document.coursecatalog.submit()">');
                    222:     if ($env{'form.currcat_0'} eq 'communities::0') {
                    223:         $r->print(&mt('Back to community listing'));
                    224:     } else {
                    225:         $r->print(&mt('Back to course listing'));
                    226:     }
                    227:     $r->print('</a>'.
1.41      raeburn   228:               &Apache::lonhtmlcommon::echo_form_input(['coursenum','catalogfilter',
                    229:                                                        'showdetails','courseid']).'</form>');
1.40      raeburn   230:     return;
                    231: }
                    232: 
1.41      raeburn   233: sub courselink_javascript {
1.40      raeburn   234:     return <<"END";
                    235: 
                    236: function ToSyllabus(cdom,cnum) {
                    237:     if (cdom == '' || cdom == null) {
                    238:         return;
                    239:     }
                    240:     if (cnum == '' || cnum == null) {
                    241:         return;
                    242:     }
1.41      raeburn   243:     document.linklaunch.action = "/public/"+cdom+"/"+cnum+"/syllabus";
                    244:     document.linklaunch.submit();
                    245: }
                    246: 
                    247: function ToSelfenroll(courseid) {
                    248:     if (courseid == '') {
                    249:         return;
                    250:     }
                    251:     document.linklaunch.action = "/adm/selfenroll";
                    252:     document.linklaunch.courseid.value = courseid;
                    253:     document.linklaunch.submit();
1.40      raeburn   254: }
                    255: 
                    256: END
1.7       raeburn   257: }
                    258: 
1.28      raeburn   259: sub instcode_course_selector {
1.58.4.2  raeburn   260:     my ($r,$codedom,$formname,$domdesc,$catlinks,$catjs,$codetitles) = @_;
1.1       raeburn   261:     my %coursecodes = ();
                    262:     my %codes = ();
                    263:     my %cat_titles = ();
                    264:     my %cat_order = ();
1.6       raeburn   265:     my %cat_items;
1.1       raeburn   266:     my $caller = 'global';
                    267:     my $format_reply;
1.30      raeburn   268:     my %add_entries = (topmargin    => "0",
                    269:                        marginheight => "0",);
1.51      raeburn   270:     my ($jscript,$totcodes,$numtitles,$lasttitle) = 
                    271:         &Apache::courseclassifier::instcode_selectors_data($codedom,$formname,
1.58.4.2  raeburn   272:                            \%cat_items,$codetitles,\%cat_titles,\%cat_order);
1.51      raeburn   273:     my $js = '<script type"text/javascript">'."\n$jscript\n$catjs\n".
1.30      raeburn   274:               '</script>';
1.51      raeburn   275:     if ($totcodes) {
1.18      raeburn   276:         if (($env{'form.state'} eq 'listing') && ($numtitles > 0)) {
1.7       raeburn   277:             $add_entries{'onLoad'} = 'setElements()';
                    278:         }
1.28      raeburn   279:         &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles);
                    280:         my $cat_maxdepth = $env{'form.catalog_maxdepth'};
                    281:         $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
1.32      raeburn   282:                   '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'."\n".
                    283:                   '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'."\n".
                    284:                   '<input type="hidden" name="currcat_0" value="instcode::0" />'.
1.33      raeburn   285:                   &additional_filters($codedom));
1.1       raeburn   286:         if ($numtitles > 0) {
1.51      raeburn   287:             $r->print('<b>'.&mt('Choose which course(s) to list.').'</b><br />'.
                    288:                       &Apache::courseclassifier::build_instcode_selectors($numtitles,
1.58.4.2  raeburn   289:                        $lasttitle,\%cat_items,$codetitles,\%cat_titles,\%cat_order));
1.18      raeburn   290:         }
1.33      raeburn   291:         $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
                    292:                   '<input type="hidden" name="sortby" value="" />'."\n".
                    293:                   '<input type="hidden" name="state" value="listing" />'."\n".
                    294:                   '<input type="submit" name="catalogfilter" value="'.
                    295:                   &mt('Display courses').'" />'.
                    296:                   '<input type="hidden" name="numtitles" value="'.$numtitles.
1.37      raeburn   297:                   '" /></form><br /><br />');
1.5       raeburn   298:     } else {
1.45      raeburn   299:         $js = '<script type"text/javascript">'."\n$catjs\n".'</script>';
1.30      raeburn   300:         &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles);
                    301:         my $cat_maxdepth = $env{'form.catalog_maxdepth'};
                    302:         $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
                    303:                   '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'.
                    304:                   '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'.
                    305:                   '<input type="hidden" name="currcat_0" value="instcode::0" />');
                    306:         $r->print('<br />'.&mt('No official courses to display for [_1].',$domdesc).'</form>');
1.1       raeburn   307:     }
1.18      raeburn   308:     return $numtitles;
1.1       raeburn   309: }
                    310: 
1.28      raeburn   311: sub cat_header {
                    312:     my ($r,$codedom,$js,$add_entries,$catlinks,$numtitles) = @_;
                    313:     my $start_page =
1.49      schafran  314:         &Apache::loncommon::start_page('Other',$js,
1.28      raeburn   315:                                        {
                    316:                                          'add_entries' => $add_entries,
                    317:                                          'no_inline_link'   => 1,});
                    318:     $r->print($start_page);
1.58.4.1  raeburn   319:     my $brtext = 'Course listing';
                    320:     if ($env{'form.currcat_0'} eq 'communities::0') {
                    321:         $brtext = 'Community listing';
                    322:     }
1.28      raeburn   323:     if ($env{'form.state'} eq 'listing') {
                    324:         if ($numtitles > 0) {
                    325:             &Apache::lonhtmlcommon::add_breadcrumb
                    326:                 ({href=>"/adm/coursecatalog",
1.56      bisitz    327:                   text=>"Course/Community Catalog"},
1.58.4.1  raeburn   328:                  {text=>$brtext});
1.28      raeburn   329:         } else {
                    330:             &Apache::lonhtmlcommon::add_breadcrumb
1.58.4.1  raeburn   331:             ({text=>$brtext});
1.28      raeburn   332:         }
                    333:     } else {
                    334:         &Apache::lonhtmlcommon::add_breadcrumb
                    335:         ({href=>"/adm/coursecatalog",
1.56      bisitz    336:           text=>"Course/Community Catalog"});
1.28      raeburn   337:     }
1.55      raeburn   338:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
1.47      raeburn   339:     my $onchange;
1.52      www       340:     unless ($env{'form.interface'} eq 'textual') {
1.53      raeburn   341:         $onchange = 'this.form.submit()';
1.47      raeburn   342:     }
1.34      raeburn   343:     $r->print('<form name="coursecatdom" method="post" action="/adm/coursecatalog">'.
                    344:               '<table border="0"><tr><td><b>'.&mt('Domain:').'</b></td><td>'.
1.47      raeburn   345:               &Apache::loncommon::select_dom_form($codedom,'showdom','',1,$onchange));
                    346:     if (!$onchange) {
                    347: 	   $r->print('&nbsp;<input type="submit" name="godom" value="'.&mt('Change').'" />');
                    348:     }
                    349:     $r->print('</td></tr></table></form>'.
1.46      raeburn   350: 	      '<form name="coursecats" method="post" action="/adm/coursecatalog"'.
1.48      raeburn   351:               ' onsubmit="return check_selected();">'.
1.34      raeburn   352:               '<table border="0"><tr>'.$catlinks.'</tr></table></form>');
1.28      raeburn   353:     return;
                    354: }
                    355: 
                    356: sub category_breadcrumbs {
1.35      raeburn   357:     my ($dom,@cats) = @_;
1.44      raeburn   358:     my $crumbsymbol = ' &#x25b6; ';
1.33      raeburn   359:     my ($currdepth,$deeper) = &get_depth_values();
1.57      raeburn   360:     my $currcat_str = 
                    361:         '<input type="hidden" name="catalog_maxdepth" value="'.$deeper.'" />'.
                    362:         '<input type="hidden" name="showdom" value="'.$dom.'" />';
1.58      raeburn   363:     my $catlinks = '<td valign="top"><b>'.&mt('Catalog:').'</b></td><td><table><tr><td>';
1.36      raeburn   364:     my $has_subcats;
1.46      raeburn   365:     my $selitem;
1.58      raeburn   366:     if (ref($cats[0]) eq 'ARRAY') {
                    367:         if (@{$cats[0]} == 0) {
                    368:             $catlinks .= &mt('No categories defined in this domain'); 
                    369:         } elsif (@{$cats[0]} == 1) {
                    370:             if ($cats[0][0] eq 'instcode') {
                    371:                 $catlinks .= &mt('Official courses (with institutional codes)');
                    372:                 $env{'form.currcat_0'} = 'instcode::0';
                    373:             } elsif ($cats[0][0] eq 'communities') {
                    374:                 $catlinks .= &mt('Communities');
                    375:                 $env{'form.currcat_0'} = 'communities::0';
                    376:             } else {
                    377:                 my $name = $cats[0][0];
                    378:                 my $item = &escape($name).'::0';
                    379:                 $catlinks .= $name;
                    380:                 $env{'form.currcat_0'} = $item;
1.46      raeburn   381:             }
1.58      raeburn   382:             $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
1.28      raeburn   383:         } else {
1.58      raeburn   384:             $catlinks .= &main_category_selector(@cats);
                    385:             if (($env{'form.currcat_0'} ne '') && 
                    386:                 ($env{'form.currcat_0'} ne 'instcode::0')) {
                    387:                 $catlinks .= $crumbsymbol;
                    388:             } else {
                    389:                 $catlinks .= '</td>';
                    390:             }
                    391:         }
                    392:     } else {
                    393:         $catlinks .= &mt('Official courses (with institutional codes)');
                    394:                      $env{'form.currcat_0'} = 'instcode::0';
                    395:          $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
                    396:     }
                    397:     if ($deeper) {
                    398:         for (my $i=1; $i<=$deeper; $i++) {
                    399:             my $shallower = $i-1;
                    400:             next if ($shallower == 0);
1.28      raeburn   401:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
1.58      raeburn   402:             if ($cat ne '') {
                    403:                 $catlinks .= '<td valign="top">'.
                    404:                              '<select name="currcat_'.$shallower.'" onchange="'.
                    405:                              'setCatDepth('."'$shallower'".');this.form.submit();">';
                    406:                 if (ref($cats[$shallower]{$container}) eq 'ARRAY') {
                    407:                     $catlinks .= '<option value="">'.&mt('De-select').'</option>';
                    408:                     for (my $j=0; $j<@{$cats[$shallower]{$container}}; $j++) {
                    409:                         my $name = $cats[$shallower]{$container}[$j];
1.58.4.5  raeburn   410:                         my $item = &escape($name).':'.&escape($container).':'.$shallower;
1.58      raeburn   411:                         my $selected = '';
                    412:                         if ($item eq $env{'form.currcat_'.$shallower}) {
                    413:                             $selected = ' selected="selected"';
                    414:                         }
                    415:                         $catlinks .= 
                    416:                            '<option value="'.$item.'"'.$selected.'>'.$name.'</option>';
1.28      raeburn   417:                     }
                    418:                 }
1.58      raeburn   419:                 $catlinks .= '</select>';
1.28      raeburn   420:             }
1.58      raeburn   421:             unless ($i == $deeper) {
                    422:                 $catlinks .= $crumbsymbol;
                    423:             } 
1.28      raeburn   424:         }
1.29      raeburn   425:         my ($cat,$container,$depth);
                    426:         if ($env{'form.currcat_'.$currdepth} eq '') {
                    427:             my $shallower = $currdepth - 1;
                    428:             ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
                    429:         } else {
                    430:             ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$currdepth});
                    431:         }
1.58      raeburn   432:         my $deeperlevel = $depth +1;
                    433:         if (ref($cats[$deeperlevel]{$cat}) eq 'ARRAY') {
1.36      raeburn   434:             $has_subcats = 1;
1.46      raeburn   435:             my $buttontext = &mt('Show subcategories');
1.58      raeburn   436:             my $selitem = 'currcat_'.$deeperlevel;
                    437:             $catlinks .= '&nbsp;<select name="'.$selitem.'" onchange="this.form.submit()">';
                    438:             if (@{$cats[$deeperlevel]{$cat}}) {
1.46      raeburn   439:                 $catlinks .= '<option value="" selected="selected">'.
1.58      raeburn   440:                              &mt('Subcategory ...').'</option>';
1.46      raeburn   441:             }
1.58      raeburn   442:             for (my $k=0; $k<@{$cats[$deeperlevel]{$cat}}; $k++) {
                    443:                 my $name = $cats[$deeperlevel]{$cat}[$k];
                    444:                 my $item = &escape($name).':'.&escape($cat).':'.$deeperlevel;
1.28      raeburn   445:                 $catlinks .= '<option value="'.$item.'">'.$name.'</option>'."\n";
                    446:             }
1.58      raeburn   447:             $catlinks .= '</select>'."\n";
1.46      raeburn   448:         } elsif ($cat ne 'instcode') {
                    449:             $catlinks .= '&nbsp;'.&mt('(No subcategories)');
1.28      raeburn   450:         }
1.58      raeburn   451:     } else {
                    452:         $selitem = 'currcat_0';
1.28      raeburn   453:     }
                    454:     $catlinks .= $currcat_str.'</td></tr></table></td>';
1.46      raeburn   455:     return ($catlinks,$has_subcats,$selitem);
1.28      raeburn   456: }
                    457: 
1.58      raeburn   458: sub main_category_selector {
                    459:     my (@cats) = @_;
                    460:     my $maincatlinks = '<select name="currcat_0" onchange="setCatDepth('."'0'".');this.form.submit();">'."\n";
                    461:     if (ref($cats[0]) eq 'ARRAY') {
                    462:         if (@{$cats[0]} > 1) {
                    463:             my $selected = '';
                    464:             if ($env{'form.currcat_0'} eq '') {
                    465:                 $selected = ' selected="selected"';    
                    466:             }
                    467:             $maincatlinks .= 
                    468:                 '<option value=""'.$selected.'>'.&mt('Select').'</option>'."\n";
                    469:         }
                    470:         for (my $i=0; $i<@{$cats[0]}; $i++) {
                    471:             my $name = $cats[0][$i];
                    472:             my $item = &escape($name).'::0';
                    473:             my $selected;
                    474:             if ($env{'form.currcat_0'} eq $item) {
                    475:                 $selected = ' selected="selected"';
                    476:             }
                    477:             $maincatlinks .= '<option value="'.$item.'"'.$selected.'>';
                    478:             if ($name eq 'instcode') {
                    479:                 $maincatlinks .= &mt('Official courses (with institutional codes)');
                    480:             } elsif ($name eq 'communities') {
                    481:                 $maincatlinks .= &mt('Communities');
                    482:             } else {
                    483:                 $maincatlinks .= $name;
                    484:             }
                    485:             $maincatlinks .= '</option>'."\n";
                    486:         }
                    487:         $maincatlinks .= '</select>'."\n";
                    488:     }
                    489:     return $maincatlinks;
                    490: }
                    491: 
1.33      raeburn   492: sub get_depth_values {
                    493:     my $currdepth = 0;
                    494:     my $deeper = 0;
                    495:     if ($env{'form.catalog_maxdepth'} ne '') {
                    496:         $currdepth = $env{'form.catalog_maxdepth'};
                    497:         if ($env{'form.currcat_'.$currdepth} eq '') {
                    498:             $deeper = $currdepth;
                    499:         } else {
                    500:             $deeper = $currdepth + 1;
                    501:         }
                    502:     }
                    503:     return ($currdepth,$deeper);
                    504: }
                    505: 
                    506: sub additional_filters {
1.36      raeburn   507:     my ($codedom,$has_subcats) = @_;
1.33      raeburn   508:     my $output = '<table>';
1.36      raeburn   509:     if (($env{'form.currcat_0'} ne 'instcode::0') && 
                    510:         ($env{'form.currcat_0'} ne '') && ($has_subcats)) {
                    511:         my $include_subcat_status;
                    512:         if ($env{'form.withsubcats'}) {
                    513:             $include_subcat_status = 'checked="checked" ';
                    514:         }
                    515:         my $counter = $env{'form.catalog_maxdepth'};
                    516:         if ($counter > 0) {
                    517:             if ($env{'form.state'} eq 'listing') {
                    518:                 $counter --;
                    519:             } elsif ($env{'form.currcat_'.$counter} eq '') {
                    520:                 $counter --;
                    521:             }
                    522:         }
                    523:         my ($catname) = split(/:/,$env{'form.currcat_'.$counter});
                    524:         if ($catname ne '') {
                    525:             $output .= '<tr><td><label>'.
                    526:                        '<input type="checkbox" name="withsubcats" value="1" '.
                    527:                        $include_subcat_status.'/>'.
1.38      raeburn   528:                        &mt('Include subcategories within "[_1]"',
                    529:                            &unescape($catname)).'</label></td></tr>';
1.36      raeburn   530:         }
                    531:     }
1.33      raeburn   532:     my $show_selfenroll_status;
                    533:     if ($env{'form.showselfenroll'}) {
                    534:         $show_selfenroll_status = 'checked="checked" ';
                    535:     }
1.57      raeburn   536:     my $selfenroll_text;
                    537:     if ($env{'form.currcat_0'} eq 'communities::0') {
                    538:         $selfenroll_text = &mt('Only show communities which allow self-enrollment');
                    539:     } else {
                    540:         $selfenroll_text = &mt('Only show courses which allow self-enrollment');
                    541:     }
1.36      raeburn   542:     $output .= '<tr><td>'.
                    543:                '<label><input type="checkbox" name="showselfenroll" value="1" '.
1.57      raeburn   544:                $show_selfenroll_status.'/>'.$selfenroll_text.
1.36      raeburn   545:                '</label></td></tr>';
1.33      raeburn   546:     if (&user_is_dc($codedom)) {
                    547:         my $showdetails_status;
                    548:         if ($env{'form.showdetails'}) {
                    549:             $showdetails_status = 'checked="checked" ';
                    550:         }
                    551:         my $showhidden_status;
                    552:         if ($env{'form.showhidden'}) {
                    553:              $showhidden_status = 'checked="checked" ';
                    554:         }
                    555:         my $dc_title = &Apache::lonnet::plaintext('dc');
1.57      raeburn   556:         my ($details_text,$hidden_text);
                    557:         if ($env{'form.currcat_0'} eq 'communities::0') {
                    558:             $details_text = &mt('Show full details for each community ([_1] only)',$dc_title);
                    559:             $hidden_text = &mt('Include communities set to be hidden from catalog ([_1] only)',$dc_title);
                    560:         } else {
                    561:             $details_text = &mt('Show full details for each course ([_1] only)',$dc_title);
                    562:             $hidden_text = &mt('Include courses set to be hidden from catalog ([_1] only)',$dc_title);
                    563:         }
1.33      raeburn   564:         $output .= '<tr><td>'."\n".
                    565:                    '<label><input type="checkbox" name="showdetails" value="1" '.
1.57      raeburn   566:                    $showdetails_status.'/>'.$details_text.
1.33      raeburn   567:                    '</label>'."\n".'</td></tr><tr><td>'.
                    568:                    '<label><input type="checkbox" name="showhidden" value="1" '.
1.57      raeburn   569:                    $showhidden_status.'/>'.$hidden_text.
1.33      raeburn   570:                    '</label>'."\n".'</td></tr>';
                    571:     }
1.36      raeburn   572:     $output .= '</table><br />';
1.33      raeburn   573:     return $output;
                    574: }
                    575: 
1.16      raeburn   576: sub user_is_dc {
                    577:     my ($codedom) = @_;
                    578:     if (exists($env{'user.role.dc./'.$codedom.'/'})) {
                    579:         my $livedc = 1;
                    580:         my $now = time;
                    581:         my ($start,$end)=split(/\./,$env{'user.role.dc./'.$codedom.'/'});
                    582:         if ($start && $start>$now) { $livedc = 0; }
                    583:         if ($end   && $end  <$now) { $livedc = 0; }
                    584:         return $livedc;
                    585:     }
                    586:     return;
                    587: }
1.7       raeburn   588: 
1.28      raeburn   589: sub search_official_courselist {
1.58.4.2  raeburn   590:     my ($domain,$numtitles,$codetitles) = @_;
                    591:     my $instcode = &Apache::courseclassifier::instcode_search_str($domain,$numtitles,$codetitles);
1.32      raeburn   592:     my $showhidden;
                    593:     if (&user_is_dc($domain)) {
                    594:         $showhidden = $env{'form.showhidden'};
                    595:     }
                    596:     my %courses = 
                    597:         &Apache::lonnet::courseiddump($domain,'.',1,$instcode,'.','.',undef,undef,
                    598:                                       'Course',1,$env{'form.showselfenroll'},undef,
                    599:                                       $showhidden,'coursecatalog');
1.7       raeburn   600:     return %courses;
                    601: }
                    602: 
1.28      raeburn   603: sub search_courselist {
1.36      raeburn   604:     my ($domain,$subcats) = @_;
1.28      raeburn   605:     my $cat_maxdepth = $env{'form.catalog_maxdepth'};
                    606:     my $filter = $env{'form.currcat_'.$cat_maxdepth};
1.29      raeburn   607:     if (($filter eq '') && ($cat_maxdepth > 0)) {
                    608:         my $shallower = $cat_maxdepth - 1;
                    609:         $filter = $env{'form.currcat_'.$shallower};
                    610:     }
1.28      raeburn   611:     my %courses;
1.36      raeburn   612:     my $filterstr;
1.28      raeburn   613:     if ($filter ne '') {
1.36      raeburn   614:         if ($env{'form.withsubcats'}) {
                    615:             if (ref($subcats) eq 'HASH') {
                    616:                 if (ref($subcats->{$filter}) eq 'ARRAY') {
                    617:                     $filterstr = join('&',@{$subcats->{$filter}});
                    618:                     if ($filterstr ne '') {
                    619:                         $filterstr = $filter.'&'.$filterstr;
                    620:                     }
                    621:                 } else {
                    622:                     $filterstr = $filter;
                    623:                 }
                    624:             } else {
                    625:                 $filterstr = $filter;
                    626:             }  
                    627:         } else {
                    628:             $filterstr = $filter; 
                    629:         }
1.57      raeburn   630:         my ($showhidden,$typefilter);
1.32      raeburn   631:         if (&user_is_dc($domain)) {
                    632:             $showhidden = $env{'form.showhidden'};
                    633:         }
1.57      raeburn   634:         if ($env{'form.currcat_0'} eq 'communities::0') {
                    635:             $typefilter = 'Community';
                    636:         } else {
                    637:             $typefilter = '.';
                    638:         }
1.32      raeburn   639:         %courses = 
                    640:             &Apache::lonnet::courseiddump($domain,'.',1,'.','.','.',undef,undef,
1.57      raeburn   641:                                           $typefilter,1,$env{'form.showselfenroll'},
1.36      raeburn   642:                                           $filterstr,$showhidden,'coursecatalog');
1.28      raeburn   643:     }
                    644:     return %courses;
                    645: }
1.6       raeburn   646: 
1.1       raeburn   647: sub print_course_listing {
1.58.4.2  raeburn   648:     my ($domain,$numtitles,$trails,$allitems,$subcats,$codetitles) = @_;
1.1       raeburn   649:     my $output;
1.7       raeburn   650:     my %courses;
1.15      raeburn   651:     my $knownuser = &user_is_known();
1.16      raeburn   652:     my $details = $env{'form.coursenum'};
                    653:     if (&user_is_dc($domain)) {
                    654:         if ($env{'form.showdetails'}) {
                    655:             $details = 1;
                    656:         }
                    657:     }
1.7       raeburn   658:     if ($env{'form.coursenum'} ne '') {
                    659:         %courses = &Apache::lonnet::courseiddump($domain,'.',1,'.','.',
                    660:                                                  $env{'form.coursenum'},
1.33      raeburn   661:                                                  undef,undef,'.',1);
1.7       raeburn   662:         if (keys(%courses) == 0) {
1.58.4.1  raeburn   663:             if ($env{'form.currcat_0'} eq 'communities::0') {
                    664:                 $output .= &mt('The courseID provided does not match a community in this domain.');
                    665:             } else {
                    666:                 $output .= &mt('The courseID provided does not match a course in this domain.');
                    667:             }
1.7       raeburn   668:             return $output;
                    669:         }
1.6       raeburn   670:     } else {
1.28      raeburn   671:         if ($env{'form.currcat_0'} eq 'instcode::0') {
1.58.4.2  raeburn   672:             %courses = &search_official_courselist($domain,$numtitles,$codetitles);
1.28      raeburn   673:         } else {
1.36      raeburn   674:             %courses = &search_courselist($domain,$subcats);
1.28      raeburn   675:         }
1.7       raeburn   676:         if (keys(%courses) == 0) {
1.57      raeburn   677:             if ($env{'form.currcat_0'} eq 'communities::0') {
                    678:                 $output = &mt('No communities match the criteria you selected.');
                    679:             } else {
                    680:                 $output = &mt('No courses match the criteria you selected.');
                    681:             }
1.7       raeburn   682:             return $output;
                    683:         }
1.32      raeburn   684:         if (($knownuser) && (!$env{'form.showdetails'}) && (!&user_is_dc($domain))) {
1.31      bisitz    685:             $output = '<b>'.&mt('Note for students:').'</b> '
                    686:                      .&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.')
                    687:                      .'<br /><br />';
1.8       raeburn   688:         }
1.7       raeburn   689:     }
1.27      raeburn   690:     my $now = time;
                    691:     my %domconfig =
                    692:         &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
1.35      raeburn   693:     $output .= &construct_data_table($knownuser,\%courses,$details,undef,$now,\%domconfig,$trails,$allitems);
1.41      raeburn   694:     $output .= "\n".'<form name="linklaunch" method="post" action="">'.
1.40      raeburn   695:                '<input type="hidden" name="backto" value="coursecatalog" />'.
1.41      raeburn   696:                '<input type="hidden" name="courseid" value="" />'.
                    697:                &Apache::lonhtmlcommon::echo_form_input(['catalogfilter','courseid']).'</form>';
1.15      raeburn   698:     return $output;
                    699: }
                    700: 
                    701: sub construct_data_table {
1.35      raeburn   702:     my ($knownuser,$courses,$details,$usersections,$now,$domconfig,$trails,
                    703:         $allitems) = @_;
1.7       raeburn   704:     my %sortname;
1.16      raeburn   705:     if (($details eq '') || ($env{'form.showdetails'})) {
1.7       raeburn   706:         $sortname{'Code'} = 'code';
1.35      raeburn   707:         $sortname{'Categories'} = 'cats';
1.7       raeburn   708:         $sortname{'Title'} = 'title';
1.22      raeburn   709:         $sortname{'Owner(s)'} = 'owner';
1.7       raeburn   710:     }
1.15      raeburn   711:     my $output = &Apache::loncommon::start_data_table().
                    712:                  &Apache::loncommon::start_data_table_header_row();
1.35      raeburn   713:     my @coltitles = ('Count');
                    714:     if ($env{'form.currcat_0'} eq 'instcode::0') {
                    715:         push(@coltitles,'Code');
                    716:     } else {
                    717:         push(@coltitles,'Categories');
                    718:     }
                    719:     push(@coltitles,('Sections','Crosslisted','Title','Owner(s)'));
1.15      raeburn   720:     if (ref($usersections) eq 'HASH') {
                    721:        $coltitles[1] = 'Your Section';
                    722:     }
1.7       raeburn   723:     foreach my $item (@coltitles) {
                    724:         $output .= '<th>';
                    725:         if (defined($sortname{$item})) {
                    726:             $output .= '<a href="javascript:changeSort('."'$sortname{$item}'".')">'.&mt($item).'</a>';
1.24      raeburn   727:         } elsif ($item eq 'Count') {
                    728:             $output .= '&nbsp;&nbsp;';
1.7       raeburn   729:         } else {
                    730:             $output .= &mt($item);
                    731:         }
                    732:         $output .= '</th>';
1.1       raeburn   733:     }
1.15      raeburn   734:     if ($knownuser) {
                    735:         if ($details) {
1.58.4.1  raeburn   736:             if ($env{'form.currcat_0'} eq 'communities::0') {
                    737:                 $output .= '<th>'.&mt('Default Access Dates for Members').'</th>';
                    738:             } else {
                    739:                 $output .=
                    740:                     '<th>'.&mt('Default Access Dates for Students').'</th>'.
                    741:                     '<th>'.&mt('Student Counts').'</th>'.
                    742:                     '<th>'.&mt('Auto-enrollment of[_1]registered students','<br />').'</th>';
                    743:             }
1.15      raeburn   744:         } else {
1.27      raeburn   745:             $output .= '<th>'.&mt('Details').'</th>';
1.8       raeburn   746:         }
1.1       raeburn   747:     }
1.27      raeburn   748:     $output .= '<th>'.&mt('Self-enroll (if permitted)').'</th>';
1.7       raeburn   749:     &Apache::loncommon::end_data_table_header_row();
1.15      raeburn   750:     my %courseinfo = &build_courseinfo_hash($courses,$knownuser,$details,
                    751:                                             $usersections);
1.7       raeburn   752:     my %Sortby;
1.15      raeburn   753:     foreach my $course (sort(keys(%{$courses}))) {
1.7       raeburn   754:         if ($env{'form.sortby'} eq 'code') {
                    755:             push(@{$Sortby{$courseinfo{$course}{'code'}}},$course);
1.35      raeburn   756:         } elsif ($env{'form.sortby'} eq 'cats') {
                    757:             push(@{$Sortby{$courseinfo{$course}{'categories'}}},$course);
1.7       raeburn   758:         } elsif ($env{'form.sortby'} eq 'owner') {
1.22      raeburn   759:             push(@{$Sortby{$courseinfo{$course}{'ownerlastnames'}}},$course);
1.7       raeburn   760:         } else {
1.25      raeburn   761:             my $clean_title = $courseinfo{$course}{'title'};
                    762:             $clean_title =~ s/\W+//g;
                    763:             if ($clean_title eq '') {
                    764:                 $clean_title = $courseinfo{$course}{'title'};
                    765:             }
                    766:             push(@{$Sortby{$clean_title}},$course);
1.7       raeburn   767:         }
                    768:     }
                    769:     my @sorted_courses;
1.35      raeburn   770:     if (($env{'form.sortby'} eq 'code') || ($env{'form.sortby'} eq 'owner') ||
                    771:         ($env{'form.sortby'} eq 'cats')) {
1.7       raeburn   772:         @sorted_courses = sort(keys(%Sortby));
1.6       raeburn   773:     } else {
1.7       raeburn   774:         @sorted_courses = sort { lc($a) cmp lc($b) } (keys(%Sortby));
1.1       raeburn   775:     }
1.24      raeburn   776:     my $count = 1;
1.7       raeburn   777:     foreach my $item (@sorted_courses) {
                    778:         foreach my $course (@{$Sortby{$item}}) {
                    779:             $output.=&Apache::loncommon::start_data_table_row(); 
1.35      raeburn   780:             $output.=&courseinfo_row($courseinfo{$course},$knownuser,$details,
                    781:                                      \$count,$now,$course,$trails,$allitems);
1.7       raeburn   782:             $output.=&Apache::loncommon::end_data_table_row();
                    783:         }
1.1       raeburn   784:     }
1.7       raeburn   785:     $output .= &Apache::loncommon::end_data_table();
                    786:     return $output;
                    787: }
                    788: 
                    789: sub build_courseinfo_hash {
1.15      raeburn   790:     my ($courses,$knownuser,$details,$usersections) = @_;
1.1       raeburn   791:     my %courseinfo;
1.7       raeburn   792:     my $now = time;
1.15      raeburn   793:     foreach my $course (keys(%{$courses})) {
1.1       raeburn   794:         my $descr;
1.22      raeburn   795:         if (ref($courses->{$course}) eq 'HASH') {
                    796:             $descr = $courses->{$course}{'description'}; 
1.1       raeburn   797:         }
                    798:         my $cleandesc=&HTML::Entities::encode($descr,'<>&"');
                    799:         $cleandesc=~s/'/\\'/g;
1.10      raeburn   800:         $cleandesc =~ s/^\s+//;
1.1       raeburn   801:         my ($cdom,$cnum)=split(/\_/,$course);
1.39      raeburn   802:         my ($instcode,$singleowner,$ttype,$selfenroll_types,
1.35      raeburn   803:             $selfenroll_start,$selfenroll_end,@owners,%ownernames,$categories);
1.22      raeburn   804:         if (ref($courses->{$course}) eq 'HASH') {
                    805:             $descr = $courses->{$course}{'description'};
1.23      raeburn   806:             $instcode =  $courses->{$course}{'inst_code'};
1.22      raeburn   807:             $singleowner = $courses->{$course}{'owner'};
                    808:             $ttype =  $courses->{$course}{'type'};
1.27      raeburn   809:             $selfenroll_types = $courses->{$course}{'selfenroll_types'};
                    810:             $selfenroll_start = $courses->{$course}{'selfenroll_start_date'};
                    811:             $selfenroll_end = $courses->{$course}{'selfenroll_end_date'};
1.35      raeburn   812:             $categories = $courses->{$course}{'categories'};
1.22      raeburn   813:             push(@owners,$singleowner);
1.58.4.7  raeburn   814:             if ($courses->{$course}{'co-owners'} ne '') {
                    815:                 foreach my $item (split(/,/,$courses->{$course}{'co-owners'})) {
1.22      raeburn   816:                     push(@owners,$item);
                    817:                 }
                    818:             }
                    819:         }
                    820:         foreach my $owner (@owners) {
1.54      raeburn   821:             my ($ownername,$ownerdom); 
1.22      raeburn   822:             if ($owner =~ /:/) {
                    823:                 ($ownername,$ownerdom) = split(/:/,$owner);
                    824:             } else {
                    825:                 $ownername = $owner;
                    826:                 if ($owner ne '') {
                    827:                     $ownerdom = $cdom;
                    828:                 }
                    829:             }
                    830:             if ($ownername ne '' && $ownerdom ne '') {
                    831:                 my %namehash=&Apache::loncommon::getnames($ownername,$ownerdom);
                    832:                 $ownernames{$ownername.':'.$ownerdom} = \%namehash; 
1.1       raeburn   833:             }
                    834:         }
                    835:         $courseinfo{$course}{'cdom'} = $cdom;
                    836:         $courseinfo{$course}{'cnum'} = $cnum;
                    837:         $courseinfo{$course}{'code'} = $instcode;
1.22      raeburn   838:         my @lastnames;
                    839:         foreach my $owner (keys(%ownernames)) {
                    840:             if (ref($ownernames{$owner}) eq 'HASH') {
                    841:                 push(@lastnames,$ownernames{$owner}{'lastname'});
                    842:             }
                    843:         }
                    844:         $courseinfo{$course}{'ownerlastnames'} = join(', ',sort(@lastnames));
1.1       raeburn   845:         $courseinfo{$course}{'title'} = $cleandesc;
1.22      raeburn   846:         $courseinfo{$course}{'owner'} = $singleowner;
1.27      raeburn   847:         $courseinfo{$course}{'selfenroll_types'} = $selfenroll_types;
                    848:         $courseinfo{$course}{'selfenroll_start'} = $selfenroll_start;
                    849:         $courseinfo{$course}{'selfenroll_end'} = $selfenroll_end;
1.35      raeburn   850:         $courseinfo{$course}{'categories'} = $categories;
1.7       raeburn   851: 
                    852:         my %coursehash = &Apache::lonnet::dump('environment',$cdom,$cnum);
                    853:         my @classids;
                    854:         my @crosslistings;
1.15      raeburn   855:         my ($seclist,$numsec) = 
                    856:             &identify_sections($coursehash{'internal.sectionnums'});
                    857:         if (ref($usersections) eq 'HASH') {
                    858:             if (ref($usersections->{$course}) eq 'ARRAY') {
                    859:                 $seclist = join(', ',@{$usersections->{$course}});
                    860:             }
                    861:         }
1.7       raeburn   862:         $courseinfo{$course}{'seclist'} = $seclist;
1.15      raeburn   863:         my ($xlist_items,$numxlist) = 
                    864:             &identify_sections($coursehash{'internal.crosslistings'});
1.7       raeburn   865:         my $showsyllabus = 1; # default is to include a syllabus link
                    866:         if (defined($coursehash{'showsyllabus'})) {
                    867:             $showsyllabus = $coursehash{'showsyllabus'};
                    868:         }
                    869:         $courseinfo{$course}{'showsyllabus'} = $showsyllabus;
1.15      raeburn   870:         if (((defined($env{'form.coursenum'}) && ($cnum eq $env{'form.coursenum'}))) ||
1.22      raeburn   871:             ($knownuser && ($details == 1))) {
1.15      raeburn   872:             $courseinfo{$course}{'counts'} =  &count_students($cdom,$cnum,$numsec);
                    873:             $courseinfo{$course}{'autoenrollment'} =
                    874:                 &autoenroll_info(\%coursehash,$now,$seclist,$xlist_items,
1.22      raeburn   875:                                  $instcode,\@owners,$cdom,$cnum);
1.15      raeburn   876: 
                    877:             my $startaccess = '';
                    878:             my $endaccess = '';
                    879:             my $accessdates;
                    880:             if ( defined($coursehash{'default_enrollment_start_date'}) ) {
                    881:                 $startaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_start_date'});
                    882:             }
                    883:             if ( defined($coursehash{'default_enrollment_end_date'}) ) {
                    884:                 $endaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_end_date'});
                    885:                 if ($coursehash{'default_enrollment_end_date'} == 0) {
1.34      raeburn   886:                     $endaccess = &mt('No ending date');
1.15      raeburn   887:                 }
                    888:             }
                    889:             if ($startaccess) {
1.42      bisitz    890:                 $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$startaccess).'<br />';
1.7       raeburn   891:             }
1.15      raeburn   892:             if ($endaccess) {
1.42      bisitz    893:                 $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$endaccess).'<br />';
1.15      raeburn   894:             }
1.34      raeburn   895:             if (($selfenroll_types ne '') && 
                    896:                 ($selfenroll_end > 0 && $selfenroll_end > $now)) {
                    897:                 my ($selfenroll_start_access,$selfenroll_end_access);
                    898:                 if (($coursehash{'default_enrollment_start_date'} ne 
                    899:                      $coursehash{'internal.selfenroll_start_access'}) ||
                    900:                    ($coursehash{'default_enrollment_end_date'} ne 
                    901:                     $coursehash{'internal.selfenroll_end_access'})) {
                    902:                     if ( defined($coursehash{'internal.selfenroll_start_access'}) ) {
                    903:                         $selfenroll_start_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_start_access'});
                    904:                     }
                    905:                     if ( defined($coursehash{'default_enrollment_end_date'}) ) {
                    906:                         $selfenroll_end_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_end_access'});
                    907:                         if ($coursehash{'internal.selfenroll_end_access'} == 0) {
                    908:                             $selfenroll_end_access = &mt('No ending date');
                    909:                         }
                    910:                     }
                    911:                     if ($selfenroll_start_access || $selfenroll_end_access) {
                    912:                         $accessdates .= '<br/><br /><i>'.&mt('Self-enrollers:').'</i><br />';
                    913:                         if ($selfenroll_start_access) {
1.42      bisitz    914:                             $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$selfenroll_start_access).'<br />';
1.34      raeburn   915:                         }
                    916:                         if ($selfenroll_end_access) {
1.42      bisitz    917:                             $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$selfenroll_end_access).'<br />';
1.34      raeburn   918:                         }
                    919:                     }
                    920:                 }
                    921:             }
1.15      raeburn   922:             $courseinfo{$course}{'access'} = $accessdates;
1.1       raeburn   923:         }
1.7       raeburn   924:         if ($xlist_items eq '') {
                    925:             $xlist_items = &mt('No');
1.1       raeburn   926:         }
1.7       raeburn   927:         $courseinfo{$course}{'xlist'} = $xlist_items;
1.1       raeburn   928:     }
1.7       raeburn   929:     return %courseinfo;
1.1       raeburn   930: }
                    931: 
1.7       raeburn   932: sub count_students {
1.15      raeburn   933:     my ($cdom,$cnum,$numsec) = @_;
1.1       raeburn   934:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1.7       raeburn   935:     my %student_count = (
                    936:                            Active => 0,
                    937:                            Future => 0,
                    938:                            Expired => 0,
                    939:                        );
1.1       raeburn   940:     my %idx;
                    941:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
1.4       albertel  942:     my %status_title = &Apache::lonlocal::texthash(
1.1       raeburn   943:                            Expired => 'Previous access',
                    944:                            Active => 'Current access',
                    945:                            Future => 'Future access',
                    946:                        );
1.7       raeburn   947: 
1.4       albertel  948:     while (my ($student,$data) = each(%$classlist)) {
1.1       raeburn   949:         $student_count{$data->[$idx{'status'}]} ++;
                    950:     }
1.7       raeburn   951: 
1.43      bisitz    952:     my $countslist = &mt('[quant,_1,section:,sections:,No sections]',$numsec).'<br />';
1.7       raeburn   953:     foreach my $status ('Active','Future') {
1.43      bisitz    954:         $countslist .= '<span class="LC_nobreak">'.$status_title{$status}.': '.
                    955:                        $student_count{$status}.'</span><br />';
1.1       raeburn   956:     }
1.7       raeburn   957:     return $countslist;
                    958: }
                    959: 
                    960: sub courseinfo_row {
1.35      raeburn   961:     my ($info,$knownuser,$details,$countref,$now,$course,$trails,$allitems) = @_;
1.7       raeburn   962:     my ($cdom,$cnum,$title,$ownerlast,$code,$owner,$seclist,$xlist_items,
1.35      raeburn   963:         $accessdates,$showsyllabus,$counts,$autoenrollment,$output,$categories);
1.7       raeburn   964:     if (ref($info) eq 'HASH') {
                    965:         $cdom = $info->{'cdom'};
                    966:         $cnum = $info->{'cnum'};
                    967:         $title = $info->{'title'};
1.22      raeburn   968:         $ownerlast = $info->{'ownerlastnames'};
1.7       raeburn   969:         $code = $info->{'code'};
                    970:         $owner = $info->{'owner'};
                    971:         $seclist = $info->{'seclist'};
                    972:         $xlist_items = $info->{'xlist'};
                    973:         $accessdates = $info->{'access'};
                    974:         $counts = $info->{'counts'};
                    975:         $autoenrollment = $info->{'autoenrollment'};
                    976:         $showsyllabus = $info->{'showsyllabus'};
1.35      raeburn   977:         $categories = $info->{'categories'};
1.7       raeburn   978:     } else {
                    979:         $output = '<td colspan="8">'.&mt('No information available for [_1].',
                    980:                                          $code).'</td>';
                    981:         return $output;
1.2       raeburn   982:     }
1.35      raeburn   983:     $output .= '<td>'.$$countref.'</td>';
                    984:     if ($env{'form.currcat_0'} eq 'instcode::0') {
                    985:         $output .= '<td>'.$code.'</td>';
                    986:     } else {
                    987:         my ($categorylist,@cats);
                    988:         if ($categories ne '') {
                    989:             @cats = split('&',$categories);
                    990:         }
                    991:         if ((ref($trails) eq 'ARRAY') && (ref($allitems) eq 'HASH')) {
                    992:             my @categories = map { $trails->[$allitems->{$_}]; } @cats;
                    993:             $categorylist = join('<br />',@categories);
                    994:         }
                    995:         if ($categorylist eq '') {
                    996:             $categorylist = '&nbsp;';
                    997:         }
                    998:         $output .= '<td>'.$categorylist.'</td>';
                    999:     }
                   1000:     $output .= '<td>'.$seclist.'</td>'.
1.7       raeburn  1001:                '<td>'.$xlist_items.'</td>'.
                   1002:                '<td>'.$title.'&nbsp;<font size="-2">';
1.2       raeburn  1003:     if ($showsyllabus) {
1.40      raeburn  1004:         $output .= '<a href="javascript:ToSyllabus('."'$cdom','$cnum'".')">'.&mt('Syllabus').'</a>';
1.7       raeburn  1005:     } else {
                   1006:         $output .= '&nbsp;';
1.2       raeburn  1007:     }
                   1008:     $output .= '</font></td>'.
1.7       raeburn  1009:                '<td>'.$ownerlast.'</td>';
1.15      raeburn  1010:     if ($knownuser) {
                   1011:         if ($details) {
1.58.4.1  raeburn  1012:             if ($env{'form.currcat_0'} eq 'communities::0') {
                   1013:                 $output .= '<td>'.$accessdates.'</td>';
                   1014:             } else {
                   1015:                 $output .=
                   1016:                     '<td>'.$accessdates.'</td>'.
                   1017:                     '<td>'.$counts.'</td>'.
                   1018:                     '<td>'.$autoenrollment.'</td>';
                   1019:             }
1.15      raeburn  1020:         } else {
                   1021:             $output .= "<td><a href=\"javascript:setCourseId('$cnum')\">".&mt('Show more details').'</a></td>';
1.8       raeburn  1022:         }
1.7       raeburn  1023:     }
1.27      raeburn  1024:     my $selfenroll;
                   1025:     if ($info->{'selfenroll_types'}) {
                   1026:         my $showstart = &Apache::lonlocal::locallocaltime($info->{'selfenroll_start'});
                   1027:         my $showend = &Apache::lonlocal::locallocaltime($info->{'selfenroll_end'});
                   1028:         if (($info->{'selfenroll_end'} > 0) && ($info->{'selfenroll_end'} > $now)) {
                   1029:             if (($info->{'selfenroll_start'} > 0) && ($info->{'selfenroll_start'} > $now)) {
                   1030:                 $output .= '<td>'.&mt('Starts: [_1]','<span class="LC_cusr_emph">'.$showstart.'</span>').'<br />'.&mt('Ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>').'</td>';
                   1031:             } else { 
1.58.4.6  raeburn  1032:                 $output .= '<td><a href="javascript:ToSelfenroll('."'$course'".')">'.&mt('Enroll in course').'</a><br />';
                   1033:                 if ($info->{'selfenroll_end'} == 0) {
                   1034:                     $output .= &mt('Available permanently');
                   1035:                 } elsif ($info->{'selfenroll_end'} > $now) {
                   1036:                     $output .= &mt('Self-enrollment ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>');
                   1037:                 }
                   1038:                 $output .= '</td>';
1.27      raeburn  1039:             }
                   1040:             $selfenroll = 1;
                   1041:         }
                   1042:     }
                   1043:     if (!$selfenroll) {
                   1044:         $output .= '<td>&nbsp;</td>';
                   1045:     }
1.24      raeburn  1046:     $$countref ++;
1.1       raeburn  1047:     return $output;
                   1048: }
                   1049: 
                   1050: sub identify_sections {
                   1051:     my ($seclist) = @_;
                   1052:     my @secnums;
                   1053:     if ($seclist =~ /,/) {
1.4       albertel 1054:         my @sections = split(/,/,$seclist);
1.1       raeburn  1055:         foreach my $sec (@sections) {
                   1056:             $sec =~ s/:[^:]*$//;
                   1057:             push(@secnums,$sec);
                   1058:         }
                   1059:     } else {
                   1060:         if ($seclist =~ m/^([^:]+):/) {
                   1061:             my $sec = $1;
1.4       albertel 1062:             if (!grep(/^\Q$sec\E$/,@secnums)) {
                   1063:                 push(@secnums,$sec);
1.1       raeburn  1064:             }
                   1065:         }
                   1066:     }
                   1067:     @secnums = sort {$a <=> $b} @secnums;
1.39      raeburn  1068:     $seclist = join(', ',@secnums);
1.15      raeburn  1069:     my $numsec = @secnums;
                   1070:     return ($seclist,$numsec);
1.1       raeburn  1071: }
                   1072: 
1.2       raeburn  1073: sub get_valid_classes {
1.22      raeburn  1074:     my ($seclist,$xlist_items,$crscode,$owners,$cdom,$cnum) = @_;
1.2       raeburn  1075:     my $response;
                   1076:     my %validations;
                   1077:     @{$validations{'sections'}} = ();
                   1078:     @{$validations{'xlists'}} = ();
                   1079:     my $totalitems = 0;
                   1080:     if ($seclist) {
1.13      raeburn  1081:         foreach my $sec (split(/, /,$seclist)) {
1.2       raeburn  1082:             my $class = $crscode.$sec;
1.22      raeburn  1083:             if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
1.3       albertel 1084: 							 $class) eq 'ok') {
1.2       raeburn  1085:                 if (!grep(/^\Q$sec$\E/,@{$validations{'sections'}})) {
1.4       albertel 1086:                     push(@{$validations{'sections'}},$sec);
1.2       raeburn  1087:                     $totalitems ++;
                   1088:                 }
                   1089:             }
                   1090:         }
                   1091:     }
                   1092:     if ($xlist_items) {
1.13      raeburn  1093:         foreach my $item (split(/, /,$xlist_items)) {
1.22      raeburn  1094:             if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
1.3       albertel 1095: 							 $item) eq 'ok') {
1.2       raeburn  1096:                 if (!grep(/^\Q$item$\E/,@{$validations{'xlists'}})) {
1.4       albertel 1097:                     push(@{$validations{'xlists'}},$item);
1.2       raeburn  1098:                     $totalitems ++;
                   1099:                 }
                   1100:             }
                   1101:         }
                   1102:     }
                   1103:     if ($totalitems > 0) {
                   1104:         if (@{$validations{'sections'}}) {
1.42      bisitz   1105:             $response = &mt('Sections:').' '.
1.14      raeburn  1106:                         join(', ',@{$validations{'sections'}}).'<br />';
1.2       raeburn  1107:         }
                   1108:         if (@{$validations{'xlists'}}) {
1.42      bisitz   1109:             $response .= &mt('Courses:').' '.
1.14      raeburn  1110:                         join(', ',@{$validations{'xlists'}});
1.2       raeburn  1111:         }
                   1112:     }
                   1113:     return $response;
                   1114: }
                   1115: 
1.7       raeburn  1116: sub autoenroll_info {
1.22      raeburn  1117:     my ($coursehash,$now,$seclist,$xlist_items,$code,$owners,$cdom,$cnum) = @_;
1.7       raeburn  1118:     my $autoenrolldates = &mt('Not enabled');
                   1119:     if (defined($coursehash->{'internal.autoadds'}) && $coursehash->{'internal.autoadds'} == 1) {
                   1120:         my ($autostart,$autoend);
                   1121:         if ( defined($coursehash->{'internal.autostart'}) ) {
                   1122:             $autostart = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autostart'});
                   1123:         }
                   1124:         if ( defined($coursehash->{'internal.autoend'}) ) {
                   1125:             $autoend = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autoend'});
                   1126:         }
                   1127:         if ($coursehash->{'internal.autostart'} > $now) {
                   1128:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
                   1129:                 $autoenrolldates = &mt('Not enabled');
                   1130:             } else {
                   1131:                 my $valid_classes = 
                   1132:                    &get_valid_classes($seclist,$xlist_items,$code,
1.22      raeburn  1133:                                       $owners,$cdom,$cnum);
1.7       raeburn  1134:                 if ($valid_classes ne '') {
1.42      bisitz   1135:                     $autoenrolldates = &mt('Not enabled').'<br />'
                   1136:                                       .&mt('Starts: [_1]',$autostart)
                   1137:                                       .'<br />'.$valid_classes;
                   1138:                 }
1.7       raeburn  1139:             }
                   1140:         } else {
                   1141:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
1.42      bisitz   1142:                 $autoenrolldates = &mt('Not enabled').'<br />'
                   1143:                                   .&mt('Ended: [_1]',$autoend);
1.7       raeburn  1144:             } else {
                   1145:                 my $valid_classes = &get_valid_classes($seclist,$xlist_items,
1.22      raeburn  1146:                                                        $code,$owners,$cdom,$cnum);
1.7       raeburn  1147:                 if ($valid_classes ne '') {
1.42      bisitz   1148:                     $autoenrolldates = &mt('Currently enabled').'<br />'.
1.7       raeburn  1149:                                        $valid_classes;
                   1150:                 }
                   1151:             }
                   1152:         }
                   1153:     }
                   1154:     return $autoenrolldates;
                   1155: }
                   1156: 
1.8       raeburn  1157: sub user_is_known {
                   1158:     my $known = 0;
                   1159:     if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public' 
                   1160:         && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
                   1161:         $known = 1;
                   1162:     }
                   1163:     return $known;
                   1164: }
                   1165: 
1.1       raeburn  1166: 1;

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