File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.10: download - view: text, annotated - select for diffs
Wed Oct 18 14:07:47 2006 UTC (17 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Remove leading spaces from course titles.

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

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