Annotation of loncom/interface/coursecatalog.pm, revision 1.25
1.17 albertel 1: # The LearningOnline Network with CAPA
2: # Handler for displaying the course catalog interface
3: #
1.25 ! raeburn 4: # $Id: coursecatalog.pm,v 1.24 2007/12/10 23:54:40 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++) {
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: }
1.1 raeburn 242: } else {
1.6 raeburn 243: @longitems = @items;
1.1 raeburn 244: }
1.6 raeburn 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++) {
1.1 raeburn 249: if ($longitems[$i] eq '') {
250: $longitems[$i] = $items[$i];
251: }
1.6 raeburn 252: $r->print(' <option value="'.$items[$i].'">'.$longitems[$i].'</option>');
1.1 raeburn 253: }
1.6 raeburn 254: $r->print('</select></td>');
1.1 raeburn 255: }
1.6 raeburn 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".
1.1 raeburn 260: '</td>'
261: );
1.16 raeburn 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: }
1.1 raeburn 269: $r->print('</tr></table>');
270: if ($numtitles > 4) {
1.6 raeburn 271: $r->print('<br /><br />'.$codetitles[$numtitles-1].'<br />'."\n".
272: '<input type="text" name="'.$codetitles[$numtitles-1].'" /><br />'."\n");
1.1 raeburn 273: }
1.18 raeburn 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: }
1.19 raeburn 280: $r->print('<input type="hidden" name="numtitles" value="'.
281: $numtitles.'" /></form>');
1.18 raeburn 282: if (($numtitles > 0) && ($env{'form.state'} eq 'listing')) {
283: $r->print('<br /><br />');
1.1 raeburn 284: }
1.5 raeburn 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));
1.1 raeburn 290: }
1.18 raeburn 291: return $numtitles;
1.1 raeburn 292: }
293:
1.16 raeburn 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: }
1.7 raeburn 306:
1.6 raeburn 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:
1.7 raeburn 390: sub search_courselist {
1.18 raeburn 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: }
1.7 raeburn 408: }
1.18 raeburn 409: $instcode .= '$';
410: } else {
411: $instcode = '.';
1.7 raeburn 412: }
413: }
414: my %courses = &Apache::lonnet::courseiddump($domain,'.',1,$instcode,'.','.',
415: undef,undef,'Course',1);
416: return %courses;
417: }
418:
1.6 raeburn 419:
1.1 raeburn 420: sub print_course_listing {
1.18 raeburn 421: my ($domain,$numtitles) = @_;
1.1 raeburn 422: my $output;
1.7 raeburn 423: my %courses;
1.15 raeburn 424: my $knownuser = &user_is_known();
1.16 raeburn 425: my $details = $env{'form.coursenum'};
426: if (&user_is_dc($domain)) {
427: if ($env{'form.showdetails'}) {
428: $details = 1;
429: }
430: }
1.7 raeburn 431: if ($env{'form.coursenum'} ne '') {
432: %courses = &Apache::lonnet::courseiddump($domain,'.',1,'.','.',
433: $env{'form.coursenum'},
1.22 raeburn 434: undef,undef,'Course',1);
1.7 raeburn 435: if (keys(%courses) == 0) {
436: $output .= &mt('The courseID provided does not match a course in this domain.');
437: return $output;
438: }
1.6 raeburn 439: } else {
1.18 raeburn 440: %courses = &search_courselist($domain,$numtitles);
1.7 raeburn 441: if (keys(%courses) == 0) {
442: $output = &mt('No courses match the criteria you selected.');
443: return $output;
444: }
1.16 raeburn 445: if ($knownuser && !$env{'form.showdetails'}) {
1.8 raeburn 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: }
1.7 raeburn 448: }
1.16 raeburn 449: $output .= &construct_data_table($knownuser,\%courses,$details);
450: $output .= &Apache::lonhtmlcommon::echo_form_input(['coursenum','state','catalogfilter','sortby','showdetails']);
1.15 raeburn 451: return $output;
452: }
453:
454: sub construct_data_table {
455: my ($knownuser,$courses,$details,$usersections) = @_;
1.7 raeburn 456: my %sortname;
1.16 raeburn 457: if (($details eq '') || ($env{'form.showdetails'})) {
1.7 raeburn 458: $sortname{'Code'} = 'code';
459: $sortname{'Title'} = 'title';
1.22 raeburn 460: $sortname{'Owner(s)'} = 'owner';
1.7 raeburn 461: }
1.15 raeburn 462: my $output = &Apache::loncommon::start_data_table().
463: &Apache::loncommon::start_data_table_header_row();
1.24 raeburn 464: my @coltitles = ('Count','Code','Sections','Crosslisted','Title','Owner(s)');
1.15 raeburn 465: if (ref($usersections) eq 'HASH') {
466: $coltitles[1] = 'Your Section';
467: }
1.7 raeburn 468: foreach my $item (@coltitles) {
469: $output .= '<th>';
470: if (defined($sortname{$item})) {
471: $output .= '<a href="javascript:changeSort('."'$sortname{$item}'".')">'.&mt($item).'</a>';
1.24 raeburn 472: } elsif ($item eq 'Count') {
473: $output .= ' ';
1.7 raeburn 474: } else {
475: $output .= &mt($item);
476: }
477: $output .= '</th>';
1.1 raeburn 478: }
1.15 raeburn 479: if ($knownuser) {
480: if ($details) {
1.8 raeburn 481: $output .=
1.7 raeburn 482: '<th>'.&mt('Default Access Dates for Students').'</th>'.
483: '<th>'.&mt('Student Counts').'</th>'.
484: '<th>'.&mt('Auto-enrollment of <br />registered students').'</th>';
1.15 raeburn 485: } else {
486: $output .= '<th> </th>';
1.8 raeburn 487: }
1.1 raeburn 488: }
1.7 raeburn 489: &Apache::loncommon::end_data_table_header_row();
1.15 raeburn 490: my %courseinfo = &build_courseinfo_hash($courses,$knownuser,$details,
491: $usersections);
1.7 raeburn 492: my %Sortby;
1.15 raeburn 493: foreach my $course (sort(keys(%{$courses}))) {
1.7 raeburn 494: if ($env{'form.sortby'} eq 'code') {
495: push(@{$Sortby{$courseinfo{$course}{'code'}}},$course);
496: } elsif ($env{'form.sortby'} eq 'owner') {
1.22 raeburn 497: push(@{$Sortby{$courseinfo{$course}{'ownerlastnames'}}},$course);
1.7 raeburn 498: } else {
1.25 ! raeburn 499: my $clean_title = $courseinfo{$course}{'title'};
! 500: $clean_title =~ s/\W+//g;
! 501: if ($clean_title eq '') {
! 502: $clean_title = $courseinfo{$course}{'title'};
! 503: }
! 504: push(@{$Sortby{$clean_title}},$course);
1.7 raeburn 505: }
506: }
507: my @sorted_courses;
508: if (($env{'form.sortby'} eq 'code') || ($env{'form.sortby'} eq 'owner')) {
509: @sorted_courses = sort(keys(%Sortby));
1.6 raeburn 510: } else {
1.7 raeburn 511: @sorted_courses = sort { lc($a) cmp lc($b) } (keys(%Sortby));
1.1 raeburn 512: }
1.24 raeburn 513: my $count = 1;
1.7 raeburn 514: foreach my $item (@sorted_courses) {
515: foreach my $course (@{$Sortby{$item}}) {
516: $output.=&Apache::loncommon::start_data_table_row();
1.24 raeburn 517: $output.=&courseinfo_row($courseinfo{$course},$knownuser,$details,\$count);
1.7 raeburn 518: $output.=&Apache::loncommon::end_data_table_row();
519: }
1.1 raeburn 520: }
1.7 raeburn 521: $output .= &Apache::loncommon::end_data_table();
522: return $output;
523: }
524:
525: sub build_courseinfo_hash {
1.15 raeburn 526: my ($courses,$knownuser,$details,$usersections) = @_;
1.1 raeburn 527: my %courseinfo;
1.7 raeburn 528: my $now = time;
1.15 raeburn 529: foreach my $course (keys(%{$courses})) {
1.1 raeburn 530: my $descr;
1.22 raeburn 531: if (ref($courses->{$course}) eq 'HASH') {
532: $descr = $courses->{$course}{'description'};
1.1 raeburn 533: }
534: my $cleandesc=&HTML::Entities::encode($descr,'<>&"');
535: $cleandesc=~s/'/\\'/g;
1.10 raeburn 536: $cleandesc =~ s/^\s+//;
1.1 raeburn 537: my ($cdom,$cnum)=split(/\_/,$course);
1.22 raeburn 538: my ($descr,$instcode,$singleowner,$ttype,@owners,%ownernames);
539: if (ref($courses->{$course}) eq 'HASH') {
540: $descr = $courses->{$course}{'description'};
1.23 raeburn 541: $instcode = $courses->{$course}{'inst_code'};
1.22 raeburn 542: $singleowner = $courses->{$course}{'owner'};
543: $ttype = $courses->{$course}{'type'};
544: push(@owners,$singleowner);
545: if (ref($courses->{$course}{'co-owners'}) eq 'ARRAY') {
546: foreach my $item (@{$courses->{$course}{'co-owners'}}) {
547: push(@owners,$item);
548: }
549: }
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;
1.1 raeburn 564: }
565: }
566: $courseinfo{$course}{'cdom'} = $cdom;
567: $courseinfo{$course}{'cnum'} = $cnum;
568: $courseinfo{$course}{'code'} = $instcode;
1.22 raeburn 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));
1.1 raeburn 576: $courseinfo{$course}{'title'} = $cleandesc;
1.22 raeburn 577: $courseinfo{$course}{'owner'} = $singleowner;
1.7 raeburn 578:
579: my %coursehash = &Apache::lonnet::dump('environment',$cdom,$cnum);
580: my @classids;
581: my @crosslistings;
1.15 raeburn 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: }
1.7 raeburn 589: $courseinfo{$course}{'seclist'} = $seclist;
1.15 raeburn 590: my ($xlist_items,$numxlist) =
591: &identify_sections($coursehash{'internal.crosslistings'});
1.7 raeburn 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;
1.15 raeburn 597: if (((defined($env{'form.coursenum'}) && ($cnum eq $env{'form.coursenum'}))) ||
1.22 raeburn 598: ($knownuser && ($details == 1))) {
1.15 raeburn 599: $courseinfo{$course}{'counts'} = &count_students($cdom,$cnum,$numsec);
600: $courseinfo{$course}{'autoenrollment'} =
601: &autoenroll_info(\%coursehash,$now,$seclist,$xlist_items,
1.22 raeburn 602: $instcode,\@owners,$cdom,$cnum);
1.15 raeburn 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 />';
1.7 raeburn 618: }
1.15 raeburn 619: if ($endaccess) {
620: $accessdates .= &mt('<i>To:</i> ').$endaccess.'<br />';
621: }
622: $courseinfo{$course}{'access'} = $accessdates;
1.1 raeburn 623: }
1.7 raeburn 624: if ($xlist_items eq '') {
625: $xlist_items = &mt('No');
1.1 raeburn 626: }
1.7 raeburn 627: $courseinfo{$course}{'xlist'} = $xlist_items;
1.1 raeburn 628: }
1.7 raeburn 629: return %courseinfo;
1.1 raeburn 630: }
631:
1.7 raeburn 632: sub count_students {
1.15 raeburn 633: my ($cdom,$cnum,$numsec) = @_;
1.1 raeburn 634: my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1.7 raeburn 635: my %student_count = (
636: Active => 0,
637: Future => 0,
638: Expired => 0,
639: );
1.1 raeburn 640: my %idx;
641: $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
1.4 albertel 642: my %status_title = &Apache::lonlocal::texthash(
1.1 raeburn 643: Expired => 'Previous access',
644: Active => 'Current access',
645: Future => 'Future access',
646: );
1.7 raeburn 647:
1.4 albertel 648: while (my ($student,$data) = each(%$classlist)) {
1.1 raeburn 649: $student_count{$data->[$idx{'status'}]} ++;
650: }
1.7 raeburn 651:
1.15 raeburn 652: my $countslist = &mt('[quant,_1,section]',$numsec).':<br />';
1.7 raeburn 653: foreach my $status ('Active','Future') {
1.1 raeburn 654: $countslist .= '<nobr>'.$status_title{$status}.': '.
655: $student_count{$status}.'</nobr><br />';
656: }
1.7 raeburn 657: return $countslist;
658: }
659:
660: sub courseinfo_row {
1.24 raeburn 661: my ($info,$knownuser,$details,$countref) = @_;
1.7 raeburn 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'};
1.22 raeburn 668: $ownerlast = $info->{'ownerlastnames'};
1.7 raeburn 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;
1.2 raeburn 681: }
1.24 raeburn 682: $output .= '<td>'.$$countref.'</td>'.
683: '<td>'.$code.'</td>'.
1.7 raeburn 684: '<td>'.$seclist.'</td>'.
685: '<td>'.$xlist_items.'</td>'.
686: '<td>'.$title.' <font size="-2">';
1.2 raeburn 687: if ($showsyllabus) {
688: $output .= &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$cnum,$cdom);
1.7 raeburn 689: } else {
690: $output .= ' ';
1.2 raeburn 691: }
692: $output .= '</font></td>'.
1.7 raeburn 693: '<td>'.$ownerlast.'</td>';
1.15 raeburn 694: if ($knownuser) {
695: if ($details) {
1.8 raeburn 696: $output .=
1.7 raeburn 697: '<td>'.$accessdates.'</td>'.
698: '<td>'.$counts.'</td>'.
699: '<td>'.$autoenrollment.'</td>';
1.15 raeburn 700: } else {
701: $output .= "<td><a href=\"javascript:setCourseId('$cnum')\">".&mt('Show more details').'</a></td>';
1.8 raeburn 702: }
1.7 raeburn 703: }
1.24 raeburn 704: $$countref ++;
1.1 raeburn 705: return $output;
706: }
707:
708: sub identify_sections {
709: my ($seclist) = @_;
710: my @secnums;
711: if ($seclist =~ /,/) {
1.4 albertel 712: my @sections = split(/,/,$seclist);
1.1 raeburn 713: foreach my $sec (@sections) {
714: $sec =~ s/:[^:]*$//;
715: push(@secnums,$sec);
716: }
717: } else {
718: if ($seclist =~ m/^([^:]+):/) {
719: my $sec = $1;
1.4 albertel 720: if (!grep(/^\Q$sec\E$/,@secnums)) {
721: push(@secnums,$sec);
1.1 raeburn 722: }
723: }
724: }
725: @secnums = sort {$a <=> $b} @secnums;
726: my $seclist = join(', ',@secnums);
1.15 raeburn 727: my $numsec = @secnums;
728: return ($seclist,$numsec);
1.1 raeburn 729: }
730:
1.2 raeburn 731: sub get_valid_classes {
1.22 raeburn 732: my ($seclist,$xlist_items,$crscode,$owners,$cdom,$cnum) = @_;
1.2 raeburn 733: my $response;
734: my %validations;
735: @{$validations{'sections'}} = ();
736: @{$validations{'xlists'}} = ();
737: my $totalitems = 0;
738: if ($seclist) {
1.13 raeburn 739: foreach my $sec (split(/, /,$seclist)) {
1.2 raeburn 740: my $class = $crscode.$sec;
1.22 raeburn 741: if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
1.3 albertel 742: $class) eq 'ok') {
1.2 raeburn 743: if (!grep(/^\Q$sec$\E/,@{$validations{'sections'}})) {
1.4 albertel 744: push(@{$validations{'sections'}},$sec);
1.2 raeburn 745: $totalitems ++;
746: }
747: }
748: }
749: }
750: if ($xlist_items) {
1.13 raeburn 751: foreach my $item (split(/, /,$xlist_items)) {
1.22 raeburn 752: if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
1.3 albertel 753: $item) eq 'ok') {
1.2 raeburn 754: if (!grep(/^\Q$item$\E/,@{$validations{'xlists'}})) {
1.4 albertel 755: push(@{$validations{'xlists'}},$item);
1.2 raeburn 756: $totalitems ++;
757: }
758: }
759: }
760: }
761: if ($totalitems > 0) {
762: if (@{$validations{'sections'}}) {
763: $response = &mt('Sections: ').
1.14 raeburn 764: join(', ',@{$validations{'sections'}}).'<br />';
1.2 raeburn 765: }
766: if (@{$validations{'xlists'}}) {
767: $response .= &mt('Courses: ').
1.14 raeburn 768: join(', ',@{$validations{'xlists'}});
1.2 raeburn 769: }
770: }
771: return $response;
772: }
773:
1.6 raeburn 774: sub javascript_select_filler {
775: my ($formname,$scripttext,$codetitles,$longtitles_str,$allidlist) = @_;
776: my $output = <<END;
777: function courseSet() {
778: var longtitles = new Array ("$longtitles_str");
779: var valyr = document.$formname.Year.options[document.$formname.Year.selectedIndex].value
780: var valsem = document.$formname.Semester.options[document.$formname.Semester.selectedIndex].value
781: var valdept = document.$formname.Department.options[document.$formname.Department.selectedIndex].value
782: var valclass = document.$formname.Number.options[document.$formname.Number.selectedIndex].value
783: var idyears = new Array("$allidlist");
784: var idyr = -1;
785: var idsem = -1;
786: var iddept = -1;
787: document.$formname.Number.length = 0;
788:
789: $scripttext
790:
791: selYear = document.$formname.Year.selectedIndex-1;
792: selSemester = document.$formname.Semester.selectedIndex-1;
793: selDepartment = document.$formname.Department.selectedIndex-1;
794: if (selYear == -1) {
795: if (selSemester == -1) {
796: if (selDepartment > -1) {
797: document.$formname.Number.options[0] = new Option('All','0',false,false);
798: for (var k=0; k<idcse_by_dep[selDepartment].length; k++) {
799: document.$formname.Number.options[k+1] = new Option(idcse_by_dep[selDepartment][k],idcse_by_dep[selDepartment][k],false,false);
800:
801: }
802: }
803: else {
804: document.$formname.Number.options[0] = new Option("All","0",true,true);
805: }
806: }
807: else {
808: if (selDepartment > -1) {
809: for (var i=0; i<idcse_by_sem_sems.length; i++) {
810: if (idcse_by_sem_sems[i] == valsem) {
811: idsem = i;
812: }
813: }
814: if (idsem != -1) {
815: for (var i=0; i<idcse_by_sem_dept[idsem].length; i++) {
816: if (idcse_by_sem_dept[idsem][i] == valdept) {
817: iddept = i;
818: }
819: }
820: }
821: if (iddept != -1) {
822: document.$formname.Number.options[0] = new Option('All','0',false,false);
823: for (var k=0; k<idcse_by_sem_num[idsem][iddept].length; k++) {
824: document.$formname.Number.options[k+1] = new Option(idcse_by_sem_num[idsem][iddept][k],idcse_by_sem_num[idsem][iddept][k],false,false);
825: }
826: }
827: else {
828: document.$formname.Number.options[0] = new Option('No courses','0',true,true);
829: }
830: }
831: else {
832: document.$formname.Number.options[0] = new Option("All","0",true,true);
833: }
834: }
835: }
836: else {
837: if (selSemester == -1) {
838: if (selDepartment > -1) {
839: for (var i=0; i<idcse_by_yr_year.length; i++) {
840: if (idcse_by_yr_year[i] == valyr) {
841: idyr = i;
842: }
843: }
844: if (idyr != -1) {
845: for (var i=0; i<idcse_by_yr_dept[idyr].length; i++) {
846: if (idcse_by_yr_dept[idyr][i] == valdept) {
847: iddept = i;
848: }
849: }
850: }
851: if (iddept != -1) {
852: document.$formname.Number.options[0] = new Option('All','0',false,false);
853: for (var k=0; k<idcse_by_yr_num[idyr][iddept].length; k++) {
854: document.$formname.Number.options[k+1] = new Option(idcse_by_yr_num[idyr][iddept][k],idcse_by_yr_num[idyr][iddept][k],false,false);
855: }
856: }
857: else {
858: document.$formname.Number.options[0] = new Option('No courses','0',true,true);
859: }
860: }
861: else {
862: document.$formname.Number.options[0] = new Option("All","0",true,true);
863: }
864: }
865: else {
1.9 raeburn 866: if (selDepartment > -1) {
867: for (var k=0; k<idyears.length; k++) {
868: if (idyears[k] == valyr) {
869: idyr = k;
870: }
1.6 raeburn 871: }
1.9 raeburn 872: if (idyr != -1) {
873: for (var k=0; k<idsems[idyr].length; k++) {
874: if (idsems[idyr][k] == valsem) {
875: idsem = k;
876: }
1.6 raeburn 877: }
878: }
1.9 raeburn 879: if (idsem != -1) {
880: for (var k=0; k<idcodes[idyr][idsem].length; k++) {
881: if (idcodes[idyr][idsem][k] == valdept) {
882: iddept = k;
883: }
1.6 raeburn 884: }
885: }
1.9 raeburn 886: if (iddept != -1) {
887: document.$formname.Number.options[0] = new Option('All','0',false,false);
888: for (var i=0; i<idcourses[idyr][idsem][iddept].length; i++) {
889: var display = idcourses[idyr][idsem][iddept][i];
890: if (longtitles[3] == 1) {
891: if (idcourseslongs[idyr][idsem][iddept][i] != "") {
892: display = idcourseslongs[idyr][idsem][iddept][i]
893: }
1.6 raeburn 894: }
1.9 raeburn 895: document.$formname.Number.options[i+1] = new Option(display,idcourses[idyr][idsem][iddept][i],false,false)
1.6 raeburn 896: }
1.9 raeburn 897: }
898: else {
899: document.$formname.Number.options[0] = new Option('No courses','0',true,true);
1.6 raeburn 900: }
1.9 raeburn 901: }
1.6 raeburn 902: else {
1.9 raeburn 903: document.$formname.Number.options[0] = new Option('All','0',true,true);
1.6 raeburn 904: }
905: }
906: document.$formname.Number.selectedIndex = 0
907: }
908: }
909: END
910: return $output;
911: }
1.1 raeburn 912:
1.7 raeburn 913: sub autoenroll_info {
1.22 raeburn 914: my ($coursehash,$now,$seclist,$xlist_items,$code,$owners,$cdom,$cnum) = @_;
1.7 raeburn 915: my $autoenrolldates = &mt('Not enabled');
916: if (defined($coursehash->{'internal.autoadds'}) && $coursehash->{'internal.autoadds'} == 1) {
917: my ($autostart,$autoend);
918: if ( defined($coursehash->{'internal.autostart'}) ) {
919: $autostart = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autostart'});
920: }
921: if ( defined($coursehash->{'internal.autoend'}) ) {
922: $autoend = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autoend'});
923: }
924: if ($coursehash->{'internal.autostart'} > $now) {
925: if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
926: $autoenrolldates = &mt('Not enabled');
927: } else {
928: my $valid_classes =
929: &get_valid_classes($seclist,$xlist_items,$code,
1.22 raeburn 930: $owners,$cdom,$cnum);
1.7 raeburn 931: if ($valid_classes ne '') {
932: $autoenrolldates = &mt('Not enabled<br />Starts: ').
933: $autostart.'<br />'.$valid_classes; }
934: }
935: } else {
936: if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
937: $autoenrolldates = &mt('Not enabled<br />Ended: ').$autoend;
938: } else {
939: my $valid_classes = &get_valid_classes($seclist,$xlist_items,
1.22 raeburn 940: $code,$owners,$cdom,$cnum);
1.7 raeburn 941: if ($valid_classes ne '') {
942: $autoenrolldates = &mt('Currently enabled<br />').
943: $valid_classes;
944: }
945: }
946: }
947: }
948: return $autoenrolldates;
949: }
950:
1.8 raeburn 951: sub user_is_known {
952: my $known = 0;
953: if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public'
954: && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
955: $known = 1;
956: }
957: return $known;
958: }
959:
1.1 raeburn 960: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>