File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.16: download - view: text, annotated - select for diffs
Wed Jan 10 03:57:51 2007 UTC (17 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Make life easier for Domain Coordinators by allowing display of full details for several courses at once, if user has an active DC role in the domain.

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

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