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