File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.13: download - view: text, annotated - select for diffs
Mon Dec 11 17:21:04 2006 UTC (17 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_3_0, version_2_2_99_1, version_2_2_99_0, HEAD
Split list of sections and crosslistings on /, / instead of just /,/

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

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