File:  [LON-CAPA] / loncom / interface / coursecatalog.pm
Revision 1.22: download - view: text, annotated - select for diffs
Wed Oct 3 19:57:29 2007 UTC (16 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Store data in nohist_courseids.db as a hash.  Maintain backwards compatibility with writes to the db from legacy lonnet.pm, and also reads from the db from legacy lonnet.pm.

Start towards inclusion of co-owners as well as a single owner, for the purposes of verifying access to official classlist data.

lond
- &put_course_id_hash_handler() added to handle storage of data in nohist_courseids.db as a frozen hash.
- &put_course_id_handler() will add to an existing hash if update is from a legacy lonnet which supplies : separated data.
- &dump_course_id_handler() $as_hash flag extracted from tail. If set, data are returned as a hash.
- validate_class_access_handler() will extract comma-separated owner and co-owners from ownerlist passed to it, and send array ref to localenroll::check_section() to determine if at least one on the owners is authorized to view classlist list data for the institutional section.

lonnet.pm
- storage of hash via call to courseidputhash in lond.  If command is unrecognized on the lond side, subset of the data sent as : separated values
- retrieval of data from nohist_courseids.db - additional $as_hash argument included in courseiddump call to indicate that data should be returned as a frozen hash.
- auto_validate_class_sec() - if third argument is an array ref, then an owner list is built from the supplied owners and co-owners.
- lasttime set on lonnet side (FIXME) - to be changed to lond side.

lomodifycourse.pm
- updated course settings sent as a ref to a hash to lonnet::courseidput()
- include LC_error in span for error messages
- eliminate duplicate error message
- display error if storage of changes fails
- form name corrected to cmod.

coursecatalog.pm
- eliminate unused arg from call to courseclassifier::retrieve_instcodes
- display owner and co-owners (sort by lastname)
- institutional section access for multiple owners

courseclassifier.pm
- course codes from course data if hash, or if : separated string.

lonpickcourse.pm
- course info extracted from hash
- owner info displayed in a list if owner and co-owners exist.

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

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