File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.17: download - view: text, annotated - select for diffs
Fri Jan 12 21:07:37 2007 UTC (17 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- missing parts of the full header

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

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