1: # The LearningOnline Network with CAPA
2: # Handler for displaying the course catalog interface
3: #
4: # $Id: coursecatalog.pm,v 1.21 2007/10/02 01:10:27 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::coursecatalog;
30:
31: use strict;
32: use lib qw(/home/httpd/lib/perl);
33: use Apache::Constants qw(:common);
34: use Apache::loncommon;
35: use Apache::lonhtmlcommon;
36: use Apache::lonnet;
37: use Apache::lonlocal;
38: use Apache::courseclassifier;
39: use Apache::lonacc;
40: use LONCAPA;
41:
42: sub handler {
43: my ($r) = @_;
44: &Apache::loncommon::content_type($r,'text/html');
45: $r->send_http_header;
46: if ($r->header_only) {
47: return OK;
48: }
49: my $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,$totcodes);
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');
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'} = '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');
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> </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}{'ownerlastname'}}},$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 ($courses->{$course} =~ m/^([^:]*):/i) {
524: $descr = &unescape($1);
525: } else {
526: $descr = &unescape($courses->{$course});
527: }
528: my $cleandesc=&HTML::Entities::encode($descr,'<>&"');
529: $cleandesc=~s/'/\\'/g;
530: $cleandesc =~ s/^\s+//;
531: my ($cdom,$cnum)=split(/\_/,$course);
532:
533: my ($desc,$instcode,$owner,$ttype) = split(/:/,$courses->{$course});
534: $owner = &unescape($owner);
535: my ($ownername,$ownerdom);
536: if ($owner =~ /:/) {
537: ($ownername,$ownerdom) = split(/:/,$owner);
538: } else {
539: $ownername = $owner;
540: if ($owner ne '') {
541: $ownerdom = $cdom;
542: }
543: }
544: my %ownernames;
545: if ($ownername ne '' && $ownerdom ne '') {
546: %ownernames = &Apache::loncommon::getnames($ownername,$ownerdom);
547: }
548: $courseinfo{$course}{'cdom'} = $cdom;
549: $courseinfo{$course}{'cnum'} = $cnum;
550: $courseinfo{$course}{'code'} = $instcode;
551: $courseinfo{$course}{'ownerlastname'} = $ownernames{'lastname'};
552: $courseinfo{$course}{'title'} = $cleandesc;
553: $courseinfo{$course}{'owner'} = $owner;
554:
555: my %coursehash = &Apache::lonnet::dump('environment',$cdom,$cnum);
556: my @classids;
557: my @crosslistings;
558: my ($seclist,$numsec) =
559: &identify_sections($coursehash{'internal.sectionnums'});
560: if (ref($usersections) eq 'HASH') {
561: if (ref($usersections->{$course}) eq 'ARRAY') {
562: $seclist = join(', ',@{$usersections->{$course}});
563: }
564: }
565: $courseinfo{$course}{'seclist'} = $seclist;
566: my ($xlist_items,$numxlist) =
567: &identify_sections($coursehash{'internal.crosslistings'});
568: my $showsyllabus = 1; # default is to include a syllabus link
569: if (defined($coursehash{'showsyllabus'})) {
570: $showsyllabus = $coursehash{'showsyllabus'};
571: }
572: $courseinfo{$course}{'showsyllabus'} = $showsyllabus;
573: if (((defined($env{'form.coursenum'}) && ($cnum eq $env{'form.coursenum'}))) ||
574: ($knownuser && ($details == 1))) {
575: $courseinfo{$course}{'counts'} = &count_students($cdom,$cnum,$numsec);
576: $courseinfo{$course}{'autoenrollment'} =
577: &autoenroll_info(\%coursehash,$now,$seclist,$xlist_items,
578: $instcode,$owner,$cdom,$cnum);
579:
580: my $startaccess = '';
581: my $endaccess = '';
582: my $accessdates;
583: if ( defined($coursehash{'default_enrollment_start_date'}) ) {
584: $startaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_start_date'});
585: }
586: if ( defined($coursehash{'default_enrollment_end_date'}) ) {
587: $endaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_end_date'});
588: if ($coursehash{'default_enrollment_end_date'} == 0) {
589: $endaccess = "No ending date";
590: }
591: }
592: if ($startaccess) {
593: $accessdates .= &mt('<i>From:</i> ').$startaccess.'<br />';
594: }
595: if ($endaccess) {
596: $accessdates .= &mt('<i>To:</i> ').$endaccess.'<br />';
597: }
598: $courseinfo{$course}{'access'} = $accessdates;
599: }
600: if ($xlist_items eq '') {
601: $xlist_items = &mt('No');
602: }
603: $courseinfo{$course}{'xlist'} = $xlist_items;
604: }
605: return %courseinfo;
606: }
607:
608: sub count_students {
609: my ($cdom,$cnum,$numsec) = @_;
610: my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
611: my %student_count = (
612: Active => 0,
613: Future => 0,
614: Expired => 0,
615: );
616: my %idx;
617: $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
618: my %status_title = &Apache::lonlocal::texthash(
619: Expired => 'Previous access',
620: Active => 'Current access',
621: Future => 'Future access',
622: );
623:
624: while (my ($student,$data) = each(%$classlist)) {
625: $student_count{$data->[$idx{'status'}]} ++;
626: }
627:
628: my $countslist = &mt('[quant,_1,section]',$numsec).':<br />';
629: foreach my $status ('Active','Future') {
630: $countslist .= '<nobr>'.$status_title{$status}.': '.
631: $student_count{$status}.'</nobr><br />';
632: }
633: return $countslist;
634: }
635:
636: sub courseinfo_row {
637: my ($info,$knownuser,$details) = @_;
638: my ($cdom,$cnum,$title,$ownerlast,$code,$owner,$seclist,$xlist_items,
639: $accessdates,$showsyllabus,$counts,$autoenrollment,$output);
640: if (ref($info) eq 'HASH') {
641: $cdom = $info->{'cdom'};
642: $cnum = $info->{'cnum'};
643: $title = $info->{'title'};
644: $ownerlast = $info->{'ownerlastname'};
645: $code = $info->{'code'};
646: $owner = $info->{'owner'};
647: $seclist = $info->{'seclist'};
648: $xlist_items = $info->{'xlist'};
649: $accessdates = $info->{'access'};
650: $counts = $info->{'counts'};
651: $autoenrollment = $info->{'autoenrollment'};
652: $showsyllabus = $info->{'showsyllabus'};
653: } else {
654: $output = '<td colspan="8">'.&mt('No information available for [_1].',
655: $code).'</td>';
656: return $output;
657: }
658: $output .= '<td>'.$code.'</td>'.
659: '<td>'.$seclist.'</td>'.
660: '<td>'.$xlist_items.'</td>'.
661: '<td>'.$title.' <font size="-2">';
662: if ($showsyllabus) {
663: $output .= &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$cnum,$cdom);
664: } else {
665: $output .= ' ';
666: }
667: $output .= '</font></td>'.
668: '<td>'.$ownerlast.'</td>';
669: if ($knownuser) {
670: if ($details) {
671: $output .=
672: '<td>'.$accessdates.'</td>'.
673: '<td>'.$counts.'</td>'.
674: '<td>'.$autoenrollment.'</td>';
675: } else {
676: $output .= "<td><a href=\"javascript:setCourseId('$cnum')\">".&mt('Show more details').'</a></td>';
677: }
678: }
679: return $output;
680: }
681:
682: sub identify_sections {
683: my ($seclist) = @_;
684: my @secnums;
685: if ($seclist =~ /,/) {
686: my @sections = split(/,/,$seclist);
687: foreach my $sec (@sections) {
688: $sec =~ s/:[^:]*$//;
689: push(@secnums,$sec);
690: }
691: } else {
692: if ($seclist =~ m/^([^:]+):/) {
693: my $sec = $1;
694: if (!grep(/^\Q$sec\E$/,@secnums)) {
695: push(@secnums,$sec);
696: }
697: }
698: }
699: @secnums = sort {$a <=> $b} @secnums;
700: my $seclist = join(', ',@secnums);
701: my $numsec = @secnums;
702: return ($seclist,$numsec);
703: }
704:
705: sub get_valid_classes {
706: my ($seclist,$xlist_items,$crscode,$owner,$cdom,$cnum) = @_;
707: my $response;
708: my %validations;
709: @{$validations{'sections'}} = ();
710: @{$validations{'xlists'}} = ();
711: my $totalitems = 0;
712: if ($seclist) {
713: foreach my $sec (split(/, /,$seclist)) {
714: my $class = $crscode.$sec;
715: if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owner,
716: $class) eq 'ok') {
717: if (!grep(/^\Q$sec$\E/,@{$validations{'sections'}})) {
718: push(@{$validations{'sections'}},$sec);
719: $totalitems ++;
720: }
721: }
722: }
723: }
724: if ($xlist_items) {
725: foreach my $item (split(/, /,$xlist_items)) {
726: if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owner,
727: $item) eq 'ok') {
728: if (!grep(/^\Q$item$\E/,@{$validations{'xlists'}})) {
729: push(@{$validations{'xlists'}},$item);
730: $totalitems ++;
731: }
732: }
733: }
734: }
735: if ($totalitems > 0) {
736: if (@{$validations{'sections'}}) {
737: $response = &mt('Sections: ').
738: join(', ',@{$validations{'sections'}}).'<br />';
739: }
740: if (@{$validations{'xlists'}}) {
741: $response .= &mt('Courses: ').
742: join(', ',@{$validations{'xlists'}});
743: }
744: }
745: return $response;
746: }
747:
748: sub javascript_select_filler {
749: my ($formname,$scripttext,$codetitles,$longtitles_str,$allidlist) = @_;
750: my $output = <<END;
751: function courseSet() {
752: var longtitles = new Array ("$longtitles_str");
753: var valyr = document.$formname.Year.options[document.$formname.Year.selectedIndex].value
754: var valsem = document.$formname.Semester.options[document.$formname.Semester.selectedIndex].value
755: var valdept = document.$formname.Department.options[document.$formname.Department.selectedIndex].value
756: var valclass = document.$formname.Number.options[document.$formname.Number.selectedIndex].value
757: var idyears = new Array("$allidlist");
758: var idyr = -1;
759: var idsem = -1;
760: var iddept = -1;
761: document.$formname.Number.length = 0;
762:
763: $scripttext
764:
765: selYear = document.$formname.Year.selectedIndex-1;
766: selSemester = document.$formname.Semester.selectedIndex-1;
767: selDepartment = document.$formname.Department.selectedIndex-1;
768: if (selYear == -1) {
769: if (selSemester == -1) {
770: if (selDepartment > -1) {
771: document.$formname.Number.options[0] = new Option('All','0',false,false);
772: for (var k=0; k<idcse_by_dep[selDepartment].length; k++) {
773: document.$formname.Number.options[k+1] = new Option(idcse_by_dep[selDepartment][k],idcse_by_dep[selDepartment][k],false,false);
774:
775: }
776: }
777: else {
778: document.$formname.Number.options[0] = new Option("All","0",true,true);
779: }
780: }
781: else {
782: if (selDepartment > -1) {
783: for (var i=0; i<idcse_by_sem_sems.length; i++) {
784: if (idcse_by_sem_sems[i] == valsem) {
785: idsem = i;
786: }
787: }
788: if (idsem != -1) {
789: for (var i=0; i<idcse_by_sem_dept[idsem].length; i++) {
790: if (idcse_by_sem_dept[idsem][i] == valdept) {
791: iddept = i;
792: }
793: }
794: }
795: if (iddept != -1) {
796: document.$formname.Number.options[0] = new Option('All','0',false,false);
797: for (var k=0; k<idcse_by_sem_num[idsem][iddept].length; k++) {
798: document.$formname.Number.options[k+1] = new Option(idcse_by_sem_num[idsem][iddept][k],idcse_by_sem_num[idsem][iddept][k],false,false);
799: }
800: }
801: else {
802: document.$formname.Number.options[0] = new Option('No courses','0',true,true);
803: }
804: }
805: else {
806: document.$formname.Number.options[0] = new Option("All","0",true,true);
807: }
808: }
809: }
810: else {
811: if (selSemester == -1) {
812: if (selDepartment > -1) {
813: for (var i=0; i<idcse_by_yr_year.length; i++) {
814: if (idcse_by_yr_year[i] == valyr) {
815: idyr = i;
816: }
817: }
818: if (idyr != -1) {
819: for (var i=0; i<idcse_by_yr_dept[idyr].length; i++) {
820: if (idcse_by_yr_dept[idyr][i] == valdept) {
821: iddept = i;
822: }
823: }
824: }
825: if (iddept != -1) {
826: document.$formname.Number.options[0] = new Option('All','0',false,false);
827: for (var k=0; k<idcse_by_yr_num[idyr][iddept].length; k++) {
828: document.$formname.Number.options[k+1] = new Option(idcse_by_yr_num[idyr][iddept][k],idcse_by_yr_num[idyr][iddept][k],false,false);
829: }
830: }
831: else {
832: document.$formname.Number.options[0] = new Option('No courses','0',true,true);
833: }
834: }
835: else {
836: document.$formname.Number.options[0] = new Option("All","0",true,true);
837: }
838: }
839: else {
840: if (selDepartment > -1) {
841: for (var k=0; k<idyears.length; k++) {
842: if (idyears[k] == valyr) {
843: idyr = k;
844: }
845: }
846: if (idyr != -1) {
847: for (var k=0; k<idsems[idyr].length; k++) {
848: if (idsems[idyr][k] == valsem) {
849: idsem = k;
850: }
851: }
852: }
853: if (idsem != -1) {
854: for (var k=0; k<idcodes[idyr][idsem].length; k++) {
855: if (idcodes[idyr][idsem][k] == valdept) {
856: iddept = k;
857: }
858: }
859: }
860: if (iddept != -1) {
861: document.$formname.Number.options[0] = new Option('All','0',false,false);
862: for (var i=0; i<idcourses[idyr][idsem][iddept].length; i++) {
863: var display = idcourses[idyr][idsem][iddept][i];
864: if (longtitles[3] == 1) {
865: if (idcourseslongs[idyr][idsem][iddept][i] != "") {
866: display = idcourseslongs[idyr][idsem][iddept][i]
867: }
868: }
869: document.$formname.Number.options[i+1] = new Option(display,idcourses[idyr][idsem][iddept][i],false,false)
870: }
871: }
872: else {
873: document.$formname.Number.options[0] = new Option('No courses','0',true,true);
874: }
875: }
876: else {
877: document.$formname.Number.options[0] = new Option('All','0',true,true);
878: }
879: }
880: document.$formname.Number.selectedIndex = 0
881: }
882: }
883: END
884: return $output;
885: }
886:
887: sub autoenroll_info {
888: my ($coursehash,$now,$seclist,$xlist_items,$code,$owner,$cdom,$cnum) = @_;
889: my $autoenrolldates = &mt('Not enabled');
890: if (defined($coursehash->{'internal.autoadds'}) && $coursehash->{'internal.autoadds'} == 1) {
891: my ($autostart,$autoend);
892: if ( defined($coursehash->{'internal.autostart'}) ) {
893: $autostart = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autostart'});
894: }
895: if ( defined($coursehash->{'internal.autoend'}) ) {
896: $autoend = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autoend'});
897: }
898: if ($coursehash->{'internal.autostart'} > $now) {
899: if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
900: $autoenrolldates = &mt('Not enabled');
901: } else {
902: my $valid_classes =
903: &get_valid_classes($seclist,$xlist_items,$code,
904: $owner,$cdom,$cnum);
905: if ($valid_classes ne '') {
906: $autoenrolldates = &mt('Not enabled<br />Starts: ').
907: $autostart.'<br />'.$valid_classes; }
908: }
909: } else {
910: if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
911: $autoenrolldates = &mt('Not enabled<br />Ended: ').$autoend;
912: } else {
913: my $valid_classes = &get_valid_classes($seclist,$xlist_items,
914: $code,$owner,$cdom,$cnum);
915: if ($valid_classes ne '') {
916: $autoenrolldates = &mt('Currently enabled<br />').
917: $valid_classes;
918: }
919: }
920: }
921: }
922: return $autoenrolldates;
923: }
924:
925: sub user_is_known {
926: my $known = 0;
927: if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public'
928: && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
929: $known = 1;
930: }
931: return $known;
932: }
933:
934: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>