File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.18: download - view: text, annotated - select for diffs
Fri Jan 12 23:37:22 2007 UTC (17 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
bug 5135.  Addresses case where a domain has assigned institutional codes to at least one course, but has not customized the instcode_format() subroutine in localenroll.pm (provided as a stub).

If no category titles have been defined in localenroll::instcode_format(), coursecatalog now displays a listing of all courses in the domain which have
institutional codes.

In the absence of defined categories, there is no "Choose which course(s) to list.", no select boxes and no "Display courses" button.  Sorting by code, title or owner no longer generates javascript errors in this case.

The prior behavior - display of "No official courses to display for <Domain Name>" - when there are no courses with institutional codes, is still the same.

    1: # The LearningOnline Network with CAPA
    2: # Handler for displaying the course catalog interface
    3: #
    4: # $Id: coursecatalog.pm,v 1.18 2007/01/12 23:37:22 raeburn Exp $
    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;
   35: use Apache::lonhtmlcommon;
   36: use Apache::lonnet;
   37: use Apache::lonlocal;
   38: use Apache::courseclassifier;
   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:     }
   49:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
   50:     my $lonid=$cookies{'lonID'};
   51:     my $lonidsdir=$r->dir_config('lonIDsDir');
   52:     my $handle;
   53:     if ($lonid) {
   54: 	$handle=&LONCAPA::clean_handle($lonid->value);
   55:     }
   56:     if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
   57:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
   58:     }
   59:     &Apache::lonacc::get_posted_cgi($r);
   60:     &Apache::lonlocal::get_language_handle($r);
   61:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['sortby']);
   62:     my $codedom = $Apache::lonnet::perlvar{'lonDefDomain'};
   63:     my $formname = 'coursecatalog';
   64:     my $domdesc = $Apache::lonnet::domaindescription{$codedom};
   65: 
   66:     &Apache::lonhtmlcommon::clear_breadcrumbs();
   67:     if ($env{'form.coursenum'} ne '' && &user_is_known()) {
   68:         &course_details($r,$codedom,$formname,$domdesc);
   69:     } else {
   70:         my $numtitles = &course_selector($r,$codedom,$formname,$domdesc);
   71:         if ($env{'form.state'} eq 'listing') {
   72:             $r->print(&print_course_listing($codedom,$numtitles).'<br />');
   73:         }
   74:     }
   75:     $r->print(&Apache::loncommon::end_page());
   76:     return OK;
   77: }
   78: 
   79: sub course_details {
   80:     my ($r,$codedom,$formname,$domdesc) = @_;
   81:     my $output;
   82:     my %add_entries = (topmargin    => "0",
   83:                        marginheight => "0",);
   84:     my $start_page =
   85:         &Apache::loncommon::start_page('Course Catalog','',
   86:                                            {
   87:                                              'add_entries' => \%add_entries,
   88:                                              'no_inline_link'   => 1,});
   89:     $r->print($start_page);
   90:     &Apache::lonhtmlcommon::add_breadcrumb
   91:             ({href=>"/adm/coursecatalog",
   92:               text=>"Select courses"},
   93:              {href=>"javascript:document.$formname.submit()",
   94:               text=>"Course listing"},
   95:              {text=>"Course details"});
   96:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course Details'));
   97:     $r->print('<br />'.&mt('Detailed course information:').'<br /><br />'.
   98:               '<form name="coursecatalog" method="post">'.
   99:               &print_course_listing($codedom).'<br /><br />');
  100:     $r->print('<a href = "javascript:document.coursecatalog.submit()">'.
  101:               &mt('Back to course listing').'</a>'.
  102:               '<input type="hidden" name="sortby" value="'.
  103:               $env{'form.sortby'}.'" />'.
  104:               '<input type="hidden" name="state" value="listing" /></form>');
  105: }
  106: 
  107: sub course_selector {
  108:     my ($r,$codedom,$formname,$domdesc) = @_;
  109:     my %coursecodes = ();
  110:     my %codes = ();
  111:     my @codetitles = ();
  112:     my %cat_titles = ();
  113:     my %cat_order = ();
  114:     my %idlist = ();
  115:     my %idnums = ();
  116:     my %idlist_titles = ();
  117:     my %by_year;
  118:     my %by_sem;
  119:     my %by_dept;
  120:     my %cat_items;
  121:     my $caller = 'global';
  122:     my $format_reply;
  123:     my $totcodes = 0;
  124:     my $jscript = '';
  125:     my ($numtitles,$lasttitle);
  126:     $totcodes = &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,$codedom,$totcodes);
  127:     if ($totcodes > 0) {
  128:         $format_reply = &Apache::lonnet::auto_instcode_format($caller,$codedom,\%coursecodes,\%codes,\@codetitles,\%cat_titles,\%cat_order);
  129:         if ($format_reply eq 'ok') {
  130:             my $numtypes = @codetitles;
  131:             &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
  132:             my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
  133:             my $longtitles_str = join('","',@{$longtitles});
  134:             my $allidlist = $idlist{$codetitles[0]};
  135:             $numtitles = @codetitles;
  136:             $lasttitle = $numtitles;
  137:             if ($numtitles > 4) {
  138:                 $lasttitle = 4;
  139:             }
  140:             if ($numtitles == 0) {
  141:                 if (!defined($env{'form.state'})) {
  142:                     $env{'form.state'} = 'listing';
  143:                 }
  144:             } else {
  145:                 my @data = ('top');
  146:                 for (my $k=0; $k<$lasttitle; $k++) {
  147:                     my $cat = $codetitles[$k];
  148:                     my $level = 1;
  149:                     $level = &recurse_options($codetitles[$k],$idlist{$codetitles[$k]},$level,$cat,\%cat_items,\@data,\%by_year,\%by_sem,\%by_dept);
  150:                 }
  151:                 $scripttext .= &build_javascript(\%by_year,\%by_sem,\%by_dept,\%cat_order,\@codetitles);
  152:                 $jscript .= &javascript_select_filler($formname,$scripttext,\@codetitles,$longtitles_str,$allidlist);
  153:                 if ($env{'form.state'} eq 'listing') {
  154:                     $jscript .= '
  155: function setElements() {
  156: ';
  157:                     for (my $i=0; $i<@codetitles-1; $i++) {
  158:                         if ($env{'form.'.$codetitles[$i]} != -1) {
  159:                             $jscript .= '
  160:     for (var j=0; j<document.'.$formname.'.'.$codetitles[$i].'.length; j++) {
  161:         if (document.'.$formname.'.'.$codetitles[$i].'[j].value == "'.$env{'form.'.$codetitles[$i]}.'") {
  162:             document.'.$formname.'.'.$codetitles[$i].'.selectedIndex = j;
  163:         }
  164:     }
  165: ';
  166:                         }
  167:                     }
  168:                     $jscript .= '   courseSet()'."\n";
  169:                     if ($env{'form.'.$codetitles[-1]} != -1) {
  170:                         $jscript .= '
  171:     for (var j=0; j<document.'.$formname.'.'.$codetitles[-1].'.length; j++) {
  172:         if (document.'.$formname.'.'.$codetitles[-1].'[j].value == "'.$env{'form.'.$codetitles[-1]}.'") {
  173:             document.'.$formname.'.'.$codetitles[-1].'.selectedIndex = j;
  174:         }
  175:     }
  176: ';
  177:                     }
  178:                     $jscript .= '}';
  179:                 }
  180:             }
  181:             if ($env{'form.state'} eq 'listing') {
  182:                 $jscript .= qq|
  183: function changeSort(caller) {
  184:     document.$formname.sortby.value = caller;
  185:     document.$formname.submit();
  186: }
  187: function setCourseId(caller) {
  188:    document.$formname.coursenum.value = caller;
  189:    document.$formname.submit(); 
  190: }\n|;
  191:             }
  192:         }
  193:         my $js = '<script type"text/javascript">'."\n$jscript\n".
  194:                  '</script>';
  195:         my %add_entries = (topmargin    => "0",
  196:                            marginheight => "0",);
  197:         if (($env{'form.state'} eq 'listing') && ($numtitles > 0)) {
  198:             $add_entries{'onLoad'} = 'setElements()';
  199:         }
  200:         my $start_page =
  201:             &Apache::loncommon::start_page('Course Catalog',$js,
  202:                                            {
  203:                                              'add_entries' => \%add_entries,
  204:                                              'no_inline_link'   => 1,});
  205:         $r->print($start_page);
  206:         if ($env{'form.state'} eq 'listing') {
  207:             &Apache::lonhtmlcommon::add_breadcrumb
  208:             ({href=>"/adm/coursecatalog",
  209:               text=>"Select courses"},
  210:              {text=>"Course listing"});
  211:              $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course Listing'));
  212:         } else {
  213:             &Apache::lonhtmlcommon::add_breadcrumb
  214:             ({href=>"/adm/coursecatalog",
  215:               text=>"Select courses"});
  216:             $r->print(&Apache::lonhtmlcommon::breadcrumbs('Select courses'));
  217:         }
  218:         $r->print('<h3>'.&mt('Display information about official [_1] classes for which LON-CAPA courses have been created:',$domdesc).'</h3>');
  219:         $r->print('<form name="coursecatalog" method="post">');
  220:         if ($numtitles > 0) {
  221:             $r->print(&mt('<b>Choose which course(s) to list.</b><br />'));
  222:             $r->print('<table><tr>');
  223:             for (my $k=0; $k<$lasttitle-1; $k++) {
  224:                 my @unsorted = @{$cat_items{$codetitles[$k]}};
  225:                 my @items;
  226:                 &Apache::courseclassifier::sort_cats($k,\%cat_order,\@codetitles,\@unsorted,\@items);
  227:                 my @longitems;
  228:                 if (defined($cat_titles{$codetitles[$k]})) {
  229:                     foreach my $item (@items) {
  230:                         push(@longitems,$cat_titles{$codetitles[$k]}{$item});
  231:                     }
  232:                 } else {
  233:                     @longitems = @items;
  234:                 }
  235:                 $r->print('<td align="center">'.$codetitles[$k].'<br />'."\n".
  236:                           '<select name="'.$codetitles[$k].'" onChange="courseSet()"');
  237:                 $r->print('>'."\n".'<option value="0" />All'."\n");
  238:                 for (my $i=0; $i<@items; $i++) {
  239:                     if ($longitems[$i] eq '') {
  240:                         $longitems[$i] = $items[$i];
  241:                     }
  242:                     $r->print(' <option value="'.$items[$i].'">'.$longitems[$i].'</option>');
  243:                 }
  244:                 $r->print('</select></td>');
  245:             }
  246:             $r->print('<td align="center">'.$codetitles[$lasttitle-1].'<br />'."\n".
  247:                       '<select name="'.$codetitles[$lasttitle-1].'">'."\n".
  248:                       '<option value="0">All'."\n".
  249:                       '</option>'."\n".'</select>'."\n".
  250:                  '</td>'
  251:                 );
  252:             if (&user_is_dc($codedom)) {
  253:                 my $showdetails_status;
  254:                 if ($env{'form.showdetails'}) {
  255:                     $showdetails_status = 'checked="checked" ';
  256:                 } 
  257:                 $r->print('<td></td><td><input type="checkbox" name="showdetails" value="1" '.$showdetails_status.'/>'.&mt('Show full details for each course (DC only)').'</td>');
  258:             }
  259:             $r->print('</tr></table>');
  260:             if ($numtitles > 4) {
  261:                 $r->print('<br /><br />'.$codetitles[$numtitles-1].'<br />'."\n".
  262:                 '<input type="text" name="'.$codetitles[$numtitles-1].'" /><br />'."\n");
  263:             }
  264:             $r->print('<br />');
  265:         }
  266:         $r->print('<input type="hidden" name="coursenum" value="" /><input type="hidden" name="sortby" value="" /><input type="hidden" name="state" value="listing" />');
  267:         if ($numtitles > 0) {
  268:             $r->print('<input type="submit" name="catalogfilter" value="'.&mt('Display courses').'" />');
  269:         }
  270:         $r->print('</form>');
  271:         if (($numtitles > 0) && ($env{'form.state'} eq 'listing')) {
  272:             $r->print('<br /><br />');
  273:         }
  274:     } else {
  275:         $r->print(&Apache::loncommon::start_page('Course Catalog','',
  276:                   {
  277:                    'no_inline_link'   => 1,}));
  278:         $r->print('<br />'.&mt('No official courses to display for [_1].',$domdesc));
  279:     }
  280:     return $numtitles;
  281: }
  282: 
  283: sub user_is_dc {
  284:     my ($codedom) = @_;
  285:     if (exists($env{'user.role.dc./'.$codedom.'/'})) {
  286:         my $livedc = 1;
  287:         my $now = time;
  288:         my ($start,$end)=split(/\./,$env{'user.role.dc./'.$codedom.'/'});
  289:         if ($start && $start>$now) { $livedc = 0; }
  290:         if ($end   && $end  <$now) { $livedc = 0; }
  291:         return $livedc;
  292:     }
  293:     return;
  294: }
  295: 
  296: sub recurse_options {
  297:     my ($currkey,$currlist,$level,$cat,$cat_options,$data,$by_year,$by_sem,$by_dept) = @_;
  298:     if (ref($currlist) eq 'HASH') {
  299:         $level ++;
  300:         foreach my $key (sort(keys(%{$currlist}))) {
  301:             $$data[$level-1]= $key;
  302:             &recurse_options($key,$currlist->{$key},$level,$cat,$cat_options,$data,$by_year,$by_sem,$by_dept);
  303:         }
  304:     } else {
  305:         $level --;
  306:         my @contents = split(/","/,$currlist);
  307:         foreach my $item (@contents) {
  308:             if (!grep(/^\Q$item\E$/,@{$cat_options->{$cat}})) {
  309:                 push(@{$cat_options->{$cat}},$item);
  310:             }
  311:             if ($level == 3) {
  312:                 if (!grep/^\Q$item\E$/,@{$by_year->{$data->[1]}->{$currkey}}) {
  313:                     push(@{$by_year->{$data->[1]}->{$currkey}},$item);                 
  314:                 }
  315:                 if (!grep/^\Q$item\E$/,@{$by_sem->{$data->[2]}->{$currkey}}) {
  316:                     push(@{$by_sem->{$data->[2]}->{$currkey}},$item);
  317:                 }
  318:                 if (!grep/^\Q$item\E$/,@{$by_dept->{$currkey}}) {
  319:                     push(@{$by_dept->{$currkey}},$item);
  320:                 }
  321: 
  322:             }
  323:         }
  324:     }
  325:     return $level;
  326: }
  327: 
  328: sub build_javascript {
  329:     my ($by_year,$by_sem,$by_dept,$cat_order,$codetitles) = @_;
  330:     my @unsorted = keys(%{$by_year});
  331:     my @sorted_yrs; 
  332:     &Apache::courseclassifier::sort_cats('0',$cat_order,$codetitles,\@unsorted,\@sorted_yrs);
  333:     my $output = 'var idcse_by_yr_year = new Array("'.join('","',@sorted_yrs).'");'."\n".
  334:                  'var idcse_by_yr_dept = new Array('.scalar(@sorted_yrs).');'."\n".
  335:                  'var idcse_by_yr_num = new Array('.scalar(@sorted_yrs).');'."\n";
  336:     for (my $i=0; $i<@sorted_yrs; $i++) {
  337:         my $numkeys = keys(%{$by_year->{$sorted_yrs[$i]}});
  338:         $output .= " idcse_by_yr_num[$i] = new Array($numkeys);\n";
  339:         if (ref($by_year->{$sorted_yrs[$i]}) eq 'HASH') {
  340:             @unsorted = keys(%{$by_year->{$sorted_yrs[$i]}});
  341:             my @sorted_depts;
  342:             &Apache::courseclassifier::sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_depts);
  343:             $output .= qq| idcse_by_yr_dept[$i] = new Array ("|.join('","',@sorted_depts).'");'."\n";
  344:             for (my $j=0; $j<@sorted_depts; $j++) {
  345:                 $output .= qq| idcse_by_yr_num[$i][$j] = new Array ("|;
  346:                 $output .= join('","',sort(@{$by_year->{$sorted_yrs[$i]}->{$sorted_depts[$j]}})).'");'."\n";
  347:             }
  348:         }
  349:     }
  350:     @unsorted = keys(%{$by_sem});
  351:     my @sorted_sems;
  352:     &Apache::courseclassifier::sort_cats('1',$cat_order,$codetitles,\@unsorted,\@sorted_sems);
  353:     $output .=  'idcse_by_sem_sems = new Array("'.join('","',@sorted_sems).'");'."\n".
  354:                 'idcse_by_sem_dept = new Array('.scalar(@sorted_sems).');'."\n".
  355:                 'idcse_by_sem_num = new Array('.scalar(@sorted_sems).');'."\n";
  356:     for (my $i=0; $i<@sorted_sems; $i++) {
  357:         my $numkeys = keys(%{$by_sem->{$sorted_sems[$i]}});
  358:         $output .= " idcse_by_sem_num[$i] = new Array($numkeys);\n";
  359:         if (ref($by_sem->{$sorted_sems[$i]}) eq 'HASH') {
  360:             @unsorted = keys(%{$by_sem->{$sorted_sems[$i]}});
  361:             my @sorted_depts;
  362:             &Apache::courseclassifier::sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_depts);
  363:             $output .= qq| idcse_by_sem_dept[$i] = new Array("|.join('","',@sorted_depts).'");'."\n";
  364:             for (my $j=0; $j<@sorted_depts; $j++) {
  365:                 $output .= qq| idcse_by_sem_num[$i][$j] = new Array ("|.join('","',sort(@{$by_sem->{$sorted_sems[$i]}->{$sorted_depts[$j]}})).'");'."\n";
  366:             }
  367:         }
  368:     }
  369:     @unsorted = keys(%{$by_dept});
  370:     my @sorted_deps;
  371:     &Apache::courseclassifier::sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_deps);
  372:     $output .= 'idcse_by_dep = new Array('.scalar(@sorted_deps).');'."\n"; 
  373:     for (my $k=0; $k<@sorted_deps; $k++) {
  374:         $output .= qq| idcse_by_dep[$k] = new Array ("|.join('","',sort(@{$by_dept->{$sorted_deps[$k]}})).'");'."\n";
  375:     }
  376:     return $output;
  377: }
  378: 
  379: sub search_courselist {
  380:     my ($domain,$numtitles) = @_;
  381:     my $instcode;
  382:     if (defined($numtitles) && $numtitles == 0) {
  383:         $instcode = '.+';
  384:     } else {
  385:         my (%codedefaults,@code_order);
  386:         my $defaults_result = 
  387:             &Apache::lonnet::auto_instcode_defaults($domain,\%codedefaults,
  388:                                                     \@code_order);
  389:         if ($defaults_result eq 'ok') {
  390:             $instcode ='^';
  391:             foreach my $item (@code_order) {
  392:                 if ($env{'form.'.$item} eq '0' ) {
  393:                     $instcode .= $codedefaults{$item}; 
  394:                 } else {
  395:                     $instcode .= $env{'form.'.$item};
  396:                 }
  397:             }
  398:             $instcode .= '$';
  399:         } else {
  400:             $instcode = '.';
  401:         }
  402:     }
  403:     my %courses = &Apache::lonnet::courseiddump($domain,'.',1,$instcode,'.','.',
  404:                                                 undef,undef,'Course',1);
  405:     return %courses;
  406: }
  407: 
  408: 
  409: sub print_course_listing {
  410:     my ($domain,$numtitles) = @_;
  411:     my $output;
  412:     my %courses;
  413:     my $knownuser = &user_is_known();
  414:     my $details = $env{'form.coursenum'};
  415:     if (&user_is_dc($domain)) {
  416:         if ($env{'form.showdetails'}) {
  417:             $details = 1;
  418:         }
  419:     }
  420:     if ($env{'form.coursenum'} ne '') {
  421:         %courses = &Apache::lonnet::courseiddump($domain,'.',1,'.','.',
  422:                                                  $env{'form.coursenum'},
  423:                                                  undef,undef,'Course');
  424:         if (keys(%courses) == 0) {
  425:             $output .= &mt('The courseID provided does not match a course in this domain.');
  426:             return $output;
  427:         }
  428:     } else {
  429:         %courses = &search_courselist($domain,$numtitles);
  430:         if (keys(%courses) == 0) {
  431:             $output = &mt('No courses match the criteria you selected.');
  432:             return $output;
  433:         }
  434:         if ($knownuser && !$env{'form.showdetails'}) {
  435:             $output = &mt('<b>Note for students:</b> 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.<br /><br />');
  436:         }
  437:     }
  438:     $output .= &construct_data_table($knownuser,\%courses,$details);
  439:     $output .= &Apache::lonhtmlcommon::echo_form_input(['coursenum','state','catalogfilter','sortby','showdetails']);
  440:     return $output;
  441: }
  442: 
  443: sub construct_data_table {
  444:     my ($knownuser,$courses,$details,$usersections) = @_;
  445:     my %sortname;
  446:     if (($details eq '') || ($env{'form.showdetails'})) {
  447:         $sortname{'Code'} = 'code';
  448:         $sortname{'Title'} = 'title';
  449:         $sortname{'Owner'} = 'owner';
  450:     }
  451:     my $output = &Apache::loncommon::start_data_table().
  452:                  &Apache::loncommon::start_data_table_header_row();
  453:     my @coltitles = ('Code','Sections','Crosslisted','Title','Owner');
  454:     if (ref($usersections) eq 'HASH') {
  455:        $coltitles[1] = 'Your Section';
  456:     }
  457:     foreach my $item (@coltitles) {
  458:         $output .= '<th>';
  459:         if (defined($sortname{$item})) {
  460:             $output .= '<a href="javascript:changeSort('."'$sortname{$item}'".')">'.&mt($item).'</a>';
  461:         } else {
  462:             $output .= &mt($item);
  463:         }
  464:         $output .= '</th>';
  465:     }
  466:     if ($knownuser) {
  467:         if ($details) {
  468:             $output .=
  469:               '<th>'.&mt('Default Access Dates for Students').'</th>'.
  470:               '<th>'.&mt('Student Counts').'</th>'.
  471:               '<th>'.&mt('Auto-enrollment of <br />registered students').'</th>';
  472:         } else {
  473:             $output .= '<th>&nbsp;</th>';
  474:         }
  475:     }
  476:     &Apache::loncommon::end_data_table_header_row();
  477:     my %courseinfo = &build_courseinfo_hash($courses,$knownuser,$details,
  478:                                             $usersections);
  479:     my %Sortby;
  480:     foreach my $course (sort(keys(%{$courses}))) {
  481:         if ($env{'form.sortby'} eq 'code') {
  482:             push(@{$Sortby{$courseinfo{$course}{'code'}}},$course);
  483:         } elsif ($env{'form.sortby'} eq 'owner') {
  484:             push(@{$Sortby{$courseinfo{$course}{'ownerlastname'}}},$course);
  485:         } else {
  486:             push(@{$Sortby{$courseinfo{$course}{'title'}}},$course);
  487:         }
  488:     }
  489:     my @sorted_courses;
  490:     if (($env{'form.sortby'} eq 'code') || ($env{'form.sortby'} eq 'owner')) {
  491:         @sorted_courses = sort(keys(%Sortby));
  492:     } else {
  493:         @sorted_courses = sort { lc($a) cmp lc($b) } (keys(%Sortby));
  494:     }
  495:     foreach my $item (@sorted_courses) {
  496:         foreach my $course (@{$Sortby{$item}}) {
  497:             $output.=&Apache::loncommon::start_data_table_row(); 
  498:             $output.=&courseinfo_row($courseinfo{$course},$knownuser,$details);
  499:             $output.=&Apache::loncommon::end_data_table_row();
  500:         }
  501:     }
  502:     $output .= &Apache::loncommon::end_data_table();
  503:     return $output;
  504: }
  505: 
  506: sub build_courseinfo_hash {
  507:     my ($courses,$knownuser,$details,$usersections) = @_;
  508:     my %courseinfo;
  509:     my $now = time;
  510:     foreach my $course (keys(%{$courses})) {
  511:         my $descr;
  512:         if ($courses->{$course} =~ m/^([^:]*):/i) {
  513:             $descr = &unescape($1);
  514:         } else {
  515:             $descr = &unescape($courses->{$course});
  516:         }
  517:         my $cleandesc=&HTML::Entities::encode($descr,'<>&"');
  518:         $cleandesc=~s/'/\\'/g;
  519:         $cleandesc =~ s/^\s+//;
  520:         my ($cdom,$cnum)=split(/\_/,$course);
  521: 
  522:         my ($desc,$instcode,$owner,$ttype) = split(/:/,$courses->{$course});
  523:         $owner = &unescape($owner);
  524:         my ($ownername,$ownerdom);
  525:         if ($owner =~ /:/) {
  526:             ($ownername,$ownerdom) = split(/:/,$owner);
  527:         } else {
  528:             $ownername = $owner;
  529:             if ($owner ne '') {
  530:                 $ownerdom = $cdom;
  531:             }
  532:         }
  533:         my %ownernames;
  534:         if ($ownername ne '' && $ownerdom ne '') {
  535:             %ownernames = &Apache::loncommon::getnames($ownername,$ownerdom);
  536:         }
  537:         $courseinfo{$course}{'cdom'} = $cdom;
  538:         $courseinfo{$course}{'cnum'} = $cnum;
  539:         $courseinfo{$course}{'code'} = $instcode;
  540:         $courseinfo{$course}{'ownerlastname'} = $ownernames{'lastname'};
  541:         $courseinfo{$course}{'title'} = $cleandesc;
  542:         $courseinfo{$course}{'owner'} = $owner;
  543: 
  544:         my %coursehash = &Apache::lonnet::dump('environment',$cdom,$cnum);
  545:         my @classids;
  546:         my @crosslistings;
  547:         my ($seclist,$numsec) = 
  548:             &identify_sections($coursehash{'internal.sectionnums'});
  549:         if (ref($usersections) eq 'HASH') {
  550:             if (ref($usersections->{$course}) eq 'ARRAY') {
  551:                 $seclist = join(', ',@{$usersections->{$course}});
  552:             }
  553:         }
  554:         $courseinfo{$course}{'seclist'} = $seclist;
  555:         my ($xlist_items,$numxlist) = 
  556:             &identify_sections($coursehash{'internal.crosslistings'});
  557:         my $showsyllabus = 1; # default is to include a syllabus link
  558:         if (defined($coursehash{'showsyllabus'})) {
  559:             $showsyllabus = $coursehash{'showsyllabus'};
  560:         }
  561:         $courseinfo{$course}{'showsyllabus'} = $showsyllabus;
  562:         if (((defined($env{'form.coursenum'}) && ($cnum eq $env{'form.coursenum'}))) ||
  563:             ($knownuser && ($details == 1))) {  
  564:             $courseinfo{$course}{'counts'} =  &count_students($cdom,$cnum,$numsec);
  565:             $courseinfo{$course}{'autoenrollment'} =
  566:                 &autoenroll_info(\%coursehash,$now,$seclist,$xlist_items,
  567:                                  $instcode,$owner,$cdom,$cnum);
  568: 
  569:             my $startaccess = '';
  570:             my $endaccess = '';
  571:             my $accessdates;
  572:             if ( defined($coursehash{'default_enrollment_start_date'}) ) {
  573:                 $startaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_start_date'});
  574:             }
  575:             if ( defined($coursehash{'default_enrollment_end_date'}) ) {
  576:                 $endaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_end_date'});
  577:                 if ($coursehash{'default_enrollment_end_date'} == 0) {
  578:                     $endaccess = "No ending date";
  579:                 }
  580:             }
  581:             if ($startaccess) {
  582:                 $accessdates .= &mt('<i>From:</i> ').$startaccess.'<br />';
  583:             }
  584:             if ($endaccess) {
  585:                 $accessdates .= &mt('<i>To:</i> ').$endaccess.'<br />';
  586:             }
  587:             $courseinfo{$course}{'access'} = $accessdates;
  588:         }
  589:         if ($xlist_items eq '') {
  590:             $xlist_items = &mt('No');
  591:         }
  592:         $courseinfo{$course}{'xlist'} = $xlist_items;
  593:     }
  594:     return %courseinfo;
  595: }
  596: 
  597: sub count_students {
  598:     my ($cdom,$cnum,$numsec) = @_;
  599:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
  600:     my %student_count = (
  601:                            Active => 0,
  602:                            Future => 0,
  603:                            Expired => 0,
  604:                        );
  605:     my %idx;
  606:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
  607:     my %status_title = &Apache::lonlocal::texthash(
  608:                            Expired => 'Previous access',
  609:                            Active => 'Current access',
  610:                            Future => 'Future access',
  611:                        );
  612: 
  613:     while (my ($student,$data) = each(%$classlist)) {
  614:         $student_count{$data->[$idx{'status'}]} ++;
  615:     }
  616: 
  617:     my $countslist = &mt('[quant,_1,section]',$numsec).':<br />';
  618:     foreach my $status ('Active','Future') {
  619:         $countslist .= '<nobr>'.$status_title{$status}.': '.
  620:                        $student_count{$status}.'</nobr><br />';
  621:     }
  622:     return $countslist;
  623: }
  624: 
  625: sub courseinfo_row {
  626:     my ($info,$knownuser,$details) = @_;
  627:     my ($cdom,$cnum,$title,$ownerlast,$code,$owner,$seclist,$xlist_items,
  628:         $accessdates,$showsyllabus,$counts,$autoenrollment,$output);
  629:     if (ref($info) eq 'HASH') {
  630:         $cdom = $info->{'cdom'};
  631:         $cnum = $info->{'cnum'};
  632:         $title = $info->{'title'};
  633:         $ownerlast = $info->{'ownerlastname'};
  634:         $code = $info->{'code'};
  635:         $owner = $info->{'owner'};
  636:         $seclist = $info->{'seclist'};
  637:         $xlist_items = $info->{'xlist'};
  638:         $accessdates = $info->{'access'};
  639:         $counts = $info->{'counts'};
  640:         $autoenrollment = $info->{'autoenrollment'};
  641:         $showsyllabus = $info->{'showsyllabus'};
  642:     } else {
  643:         $output = '<td colspan="8">'.&mt('No information available for [_1].',
  644:                                          $code).'</td>';
  645:         return $output;
  646:     }
  647:     $output .= '<td>'.$code.'</td>'.
  648:                '<td>'.$seclist.'</td>'.
  649:                '<td>'.$xlist_items.'</td>'.
  650:                '<td>'.$title.'&nbsp;<font size="-2">';
  651:     if ($showsyllabus) {
  652:         $output .= &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$cnum,$cdom);
  653:     } else {
  654:         $output .= '&nbsp;';
  655:     }
  656:     $output .= '</font></td>'.
  657:                '<td>'.$ownerlast.'</td>';
  658:     if ($knownuser) {
  659:         if ($details) {
  660:             $output .=
  661:                '<td>'.$accessdates.'</td>'. 
  662:                '<td>'.$counts.'</td>'.
  663:                '<td>'.$autoenrollment.'</td>';
  664:         } else {
  665:             $output .= "<td><a href=\"javascript:setCourseId('$cnum')\">".&mt('Show more details').'</a></td>';
  666:         }
  667:     }
  668:     return $output;
  669: }
  670: 
  671: sub identify_sections {
  672:     my ($seclist) = @_;
  673:     my @secnums;
  674:     if ($seclist =~ /,/) {
  675:         my @sections = split(/,/,$seclist);
  676:         foreach my $sec (@sections) {
  677:             $sec =~ s/:[^:]*$//;
  678:             push(@secnums,$sec);
  679:         }
  680:     } else {
  681:         if ($seclist =~ m/^([^:]+):/) {
  682:             my $sec = $1;
  683:             if (!grep(/^\Q$sec\E$/,@secnums)) {
  684:                 push(@secnums,$sec);
  685:             }
  686:         }
  687:     }
  688:     @secnums = sort {$a <=> $b} @secnums;
  689:     my $seclist = join(', ',@secnums);
  690:     my $numsec = @secnums;
  691:     return ($seclist,$numsec);
  692: }
  693: 
  694: sub get_valid_classes {
  695:     my ($seclist,$xlist_items,$crscode,$owner,$cdom,$cnum) = @_;
  696:     my $response;
  697:     my %validations;
  698:     @{$validations{'sections'}} = ();
  699:     @{$validations{'xlists'}} = ();
  700:     my $totalitems = 0;
  701:     if ($seclist) {
  702:         foreach my $sec (split(/, /,$seclist)) {
  703:             my $class = $crscode.$sec;
  704:             if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owner,
  705: 							 $class) eq 'ok') {
  706:                 if (!grep(/^\Q$sec$\E/,@{$validations{'sections'}})) {
  707:                     push(@{$validations{'sections'}},$sec);
  708:                     $totalitems ++;
  709:                 }
  710:             }
  711:         }
  712:     }
  713:     if ($xlist_items) {
  714:         foreach my $item (split(/, /,$xlist_items)) {
  715:             if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owner,
  716: 							 $item) eq 'ok') {
  717:                 if (!grep(/^\Q$item$\E/,@{$validations{'xlists'}})) {
  718:                     push(@{$validations{'xlists'}},$item);
  719:                     $totalitems ++;
  720:                 }
  721:             }
  722:         }
  723:     }
  724:     if ($totalitems > 0) {
  725:         if (@{$validations{'sections'}}) {
  726:             $response = &mt('Sections: ').
  727:                         join(', ',@{$validations{'sections'}}).'<br />';
  728:         }
  729:         if (@{$validations{'xlists'}}) {
  730:             $response .= &mt('Courses: ').
  731:                         join(', ',@{$validations{'xlists'}});
  732:         }
  733:     }
  734:     return $response;
  735: }
  736: 
  737: sub javascript_select_filler {
  738:     my ($formname,$scripttext,$codetitles,$longtitles_str,$allidlist) = @_;
  739:     my $output = <<END;
  740: function courseSet() {
  741:     var longtitles = new Array ("$longtitles_str");
  742:     var valyr = document.$formname.Year.options[document.$formname.Year.selectedIndex].value
  743:     var valsem  = document.$formname.Semester.options[document.$formname.Semester.selectedIndex].value
  744:     var valdept = document.$formname.Department.options[document.$formname.Department.selectedIndex].value
  745:     var valclass = document.$formname.Number.options[document.$formname.Number.selectedIndex].value
  746:     var idyears = new Array("$allidlist");
  747:     var idyr = -1;
  748:     var idsem = -1;
  749:     var iddept = -1;
  750:     document.$formname.Number.length = 0;
  751: 
  752:     $scripttext
  753: 
  754:     selYear = document.$formname.Year.selectedIndex-1;
  755:     selSemester = document.$formname.Semester.selectedIndex-1;
  756:     selDepartment = document.$formname.Department.selectedIndex-1;
  757:     if (selYear == -1) {
  758:         if (selSemester == -1) {
  759:             if (selDepartment > -1) {
  760:                 document.$formname.Number.options[0] =  new Option('All','0',false,false);
  761:                 for (var k=0; k<idcse_by_dep[selDepartment].length; k++) {
  762:                     document.$formname.Number.options[k+1] = new Option(idcse_by_dep[selDepartment][k],idcse_by_dep[selDepartment][k],false,false);
  763: 
  764:                 }
  765:             } 
  766:             else {
  767:                 document.$formname.Number.options[0] = new Option("All","0",true,true);
  768:             }
  769:         }
  770:         else {
  771:             if (selDepartment > -1) {
  772:                 for (var i=0; i<idcse_by_sem_sems.length; i++) {
  773:                     if (idcse_by_sem_sems[i] == valsem) {
  774:                         idsem = i;
  775:                     }
  776:                 }
  777:                 if (idsem != -1) {
  778:                     for (var i=0; i<idcse_by_sem_dept[idsem].length; i++) {
  779:                         if (idcse_by_sem_dept[idsem][i] == valdept) {
  780:                             iddept = i;
  781:                         }
  782:                     }
  783:                 }
  784:                 if (iddept != -1) {
  785:                     document.$formname.Number.options[0] =  new Option('All','0',false,false);
  786:                     for (var k=0; k<idcse_by_sem_num[idsem][iddept].length; k++) {
  787:                         document.$formname.Number.options[k+1] = new Option(idcse_by_sem_num[idsem][iddept][k],idcse_by_sem_num[idsem][iddept][k],false,false);
  788:                     }
  789:                 }
  790:                 else {
  791:                     document.$formname.Number.options[0] =  new Option('No courses','0',true,true);
  792:                 }
  793:             }
  794:             else {
  795:                 document.$formname.Number.options[0] = new Option("All","0",true,true);
  796:             }
  797:         }
  798:     }
  799:     else {
  800:         if (selSemester == -1) {
  801:             if (selDepartment > -1) {
  802:                 for (var i=0; i<idcse_by_yr_year.length; i++) {
  803:                     if (idcse_by_yr_year[i] == valyr) {
  804:                         idyr = i;
  805:                     }
  806:                 }
  807:                 if (idyr != -1) {      
  808:                     for (var i=0; i<idcse_by_yr_dept[idyr].length; i++) {
  809:                         if (idcse_by_yr_dept[idyr][i] == valdept) {
  810:                             iddept = i;
  811:                         }
  812:                     }
  813:                 }
  814:                 if (iddept != -1) {
  815:                     document.$formname.Number.options[0] =  new Option('All','0',false,false);
  816:                     for (var k=0; k<idcse_by_yr_num[idyr][iddept].length; k++) {
  817:                         document.$formname.Number.options[k+1] = new Option(idcse_by_yr_num[idyr][iddept][k],idcse_by_yr_num[idyr][iddept][k],false,false);
  818:                     }
  819:                 } 
  820:                 else {
  821:                     document.$formname.Number.options[0] =  new Option('No courses','0',true,true);
  822:                 }
  823:             }
  824:             else {
  825:                 document.$formname.Number.options[0] = new Option("All","0",true,true);
  826:             }
  827:         }
  828:         else {
  829:             if (selDepartment > -1) {
  830:                 for (var k=0; k<idyears.length; k++) {
  831:                     if (idyears[k] == valyr) {
  832:                         idyr = k;
  833:                     }
  834:                 }
  835:                 if (idyr != -1) {
  836:                     for (var k=0; k<idsems[idyr].length; k++) {
  837:                         if (idsems[idyr][k] == valsem) {
  838:                             idsem = k;
  839:                         }
  840:                     }
  841:                 }
  842:                 if (idsem != -1) {
  843:                     for (var k=0; k<idcodes[idyr][idsem].length; k++) {
  844:                         if (idcodes[idyr][idsem][k] == valdept) {
  845:                             iddept = k;
  846:                         }
  847:                     }
  848:                 }
  849:                 if (iddept != -1) {
  850:                     document.$formname.Number.options[0] =  new Option('All','0',false,false);
  851:                     for (var i=0; i<idcourses[idyr][idsem][iddept].length; i++) {
  852:                         var display = idcourses[idyr][idsem][iddept][i];
  853:                         if (longtitles[3] == 1) {
  854:                             if (idcourseslongs[idyr][idsem][iddept][i] != "") {
  855:                                 display = idcourseslongs[idyr][idsem][iddept][i]
  856:                             }
  857:                         }
  858:                         document.$formname.Number.options[i+1] = new Option(display,idcourses[idyr][idsem][iddept][i],false,false)
  859:                     }
  860:                 } 
  861:                 else {
  862:                     document.$formname.Number.options[0] =  new Option('No courses','0',true,true);
  863:                 }
  864:             } 
  865:             else {
  866:                 document.$formname.Number.options[0] =  new Option('All','0',true,true);
  867:             }
  868:         }
  869:         document.$formname.Number.selectedIndex = 0
  870:     }
  871: }
  872: END
  873:     return $output;
  874: }
  875: 
  876: sub autoenroll_info {
  877:     my ($coursehash,$now,$seclist,$xlist_items,$code,$owner,$cdom,$cnum) = @_;
  878:     my $autoenrolldates = &mt('Not enabled');
  879:     if (defined($coursehash->{'internal.autoadds'}) && $coursehash->{'internal.autoadds'} == 1) {
  880:         my ($autostart,$autoend);
  881:         if ( defined($coursehash->{'internal.autostart'}) ) {
  882:             $autostart = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autostart'});
  883:         }
  884:         if ( defined($coursehash->{'internal.autoend'}) ) {
  885:             $autoend = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autoend'});
  886:         }
  887:         if ($coursehash->{'internal.autostart'} > $now) {
  888:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
  889:                 $autoenrolldates = &mt('Not enabled');
  890:             } else {
  891:                 my $valid_classes = 
  892:                    &get_valid_classes($seclist,$xlist_items,$code,
  893:                                       $owner,$cdom,$cnum);
  894:                 if ($valid_classes ne '') {
  895:                     $autoenrolldates = &mt('Not enabled<br />Starts: ').
  896:                                        $autostart.'<br />'.$valid_classes;                }
  897:             }
  898:         } else {
  899:             if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
  900:                 $autoenrolldates = &mt('Not enabled<br />Ended: ').$autoend;
  901:             } else {
  902:                 my $valid_classes = &get_valid_classes($seclist,$xlist_items,
  903:                                                        $code,$owner,$cdom,$cnum);
  904:                 if ($valid_classes ne '') {
  905:                     $autoenrolldates = &mt('Currently enabled<br />').
  906:                                        $valid_classes;
  907:                 }
  908:             }
  909:         }
  910:     }
  911:     return $autoenrolldates;
  912: }
  913: 
  914: sub user_is_known {
  915:     my $known = 0;
  916:     if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public' 
  917:         && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
  918:         $known = 1;
  919:     }
  920:     return $known;
  921: }
  922: 
  923: 1;

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