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

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

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