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

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