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