File:
[LON-CAPA] /
loncom /
interface /
coursecatalog.pm
Revision
1.57:
download - view:
text,
annotated -
select for diffs
Mon Nov 23 21:45:18 2009 UTC (14 years, 7 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Accommodate Communities
- Top Level Category: "Communities" defined in system
(Domain control to hide/unhide).
- Can only assign communities to this category (and its subcategories).
- Domain Coordinator definition of new top level categories:
instcode and communities are reserved for system use
(may not be used for names of additional top level categories).
1: # The LearningOnline Network with CAPA
2: # Handler for displaying the course catalog interface
3: #
4: # $Id: coursecatalog.pm,v 1.57 2009/11/23 21:45:18 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'},
57: ['sortby','showdom']);
58:
59: my $codedom = &Apache::lonnet::default_login_domain();
60:
61: if (($env{'user.domain'} ne '') && ($env{'user.domain'} ne 'public')) {
62: $codedom = $env{'user.domain'};
63: if ($env{'request.role.domain'} ne '') {
64: $codedom = $env{'request.role.domain'};
65: }
66: }
67: my $formname = 'coursecatalog';
68: if ($env{'form.showdom'} ne '') {
69: if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
70: $codedom = $env{'form.showdom'};
71: }
72: }
73: my $domdesc = &Apache::lonnet::domain($codedom,'description');
74: &Apache::lonhtmlcommon::clear_breadcrumbs();
75:
76: my %domconfig =
77: &Apache::lonnet::get_dom('configuration',['coursecategories'],$codedom);
78: my (@cats,@trails,%allitems,%idx,@jsarray,%subcathash,$cathash);
79: if (ref($domconfig{'coursecategories'}) eq 'HASH') {
80: $cathash = $domconfig{'coursecategories'}{'cats'};
81: } else {
82: $cathash = {};
83: }
84: my $subcats;
85: if ($env{'form.withsubcats'}) {
86: $subcats = \%subcathash;
87: }
88: &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems,
89: \%idx,\@jsarray,$subcats);
90: if ($env{'form.coursenum'} ne '' && &user_is_known()) {
91: &course_details($r,$codedom,$formname,$domdesc,\@trails,\%allitems);
92: } else {
93: my ($catlinks,$has_subcats,$selitem) = &category_breadcrumbs($codedom,@cats);
94: my $catjs = <<"ENDSCRIPT";
95:
96: function setCatDepth(depth) {
97: document.coursecats.catalog_maxdepth.value = depth;
98: if (depth == '') {
99: document.coursecats.currcat_0.value = '';
100: }
101: document.coursecats.submit();
102: return;
103: }
104:
105: function changeSort(caller) {
106: document.$formname.sortby.value = caller;
107: document.$formname.submit();
108: }
109:
110: function setCourseId(caller) {
111: document.$formname.coursenum.value = caller;
112: document.$formname.submit();
113: }
114:
115: ENDSCRIPT
116: $catjs .= &courselink_javascript();
117: my $numtitles;
118: if ($env{'form.currcat_0'} eq 'instcode::0') {
119: $numtitles = &instcode_course_selector($r,$codedom,$formname,$domdesc,
120: $catlinks,$catjs);
121: if ($env{'form.state'} eq 'listing') {
122: $r->print(&print_course_listing($codedom,$numtitles));
123: }
124: } else {
125: my (%add_entries);
126: my ($currdepth,$deeper) = &get_depth_values();
127: if ($selitem) {
128: my $alert = &mt('Choose a subcategory to display');
129: if (!$deeper) {
130: $alert = &mt('Choose a category to display');
131: }
132: $catjs .= <<ENDJS;
133: function check_selected() {
134: if (document.coursecats.$selitem.options[document.coursecats.$selitem.selectedIndex].value == "") {
135: alert('$alert');
136: return false;
137: }
138: }
139: ENDJS
140: }
141: $catjs = '<script type="text/javascript">'."\n".$catjs."\n".'</script>';
142: &cat_header($r,$codedom,$catjs,\%add_entries,$catlinks);
143: if ($env{'form.currcat_0'} ne '') {
144: $r->print('<form name="'.$formname.
145: '" method="post" action="/adm/coursecatalog">'.
146: &additional_filters($codedom,$has_subcats)."\n");
147: $r->print('<input type="hidden" name="catalog_maxdepth" value="'.
148: $deeper.'" />'."\n");
149: for (my $i=0; $i<$deeper; $i++) {
150: $r->print('<input type="hidden" name="currcat_'.$i.'" value="'.$env{'form.currcat_'.$i}.'" />'."\n");
151: }
152: my $display_button;
153: if ($env{'form.currcat_0'} eq 'communities::0') {
154: $display_button = &mt('Display communities');
155: } else {
156: $display_button = &mt('Display courses');
157: }
158: $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
159: '<input type="hidden" name="sortby" value="" />'."\n".
160: '<input type="hidden" name="state" value="listing" />'."\n".
161: '<input type="hidden" name="showdom" value="'.
162: $env{'form.showdom'}.'" />'.
163: '<input type="submit" name="catalogfilter" value="'.
164: $display_button.'" /></form><br /><br />');
165: }
166: if ($env{'form.state'} eq 'listing') {
167: $r->print(&print_course_listing($codedom,undef,\@trails,\%allitems,$subcats));
168: }
169: }
170: }
171: $r->print('<br />'.&Apache::loncommon::end_page());
172: return OK;
173: }
174:
175: sub course_details {
176: my ($r,$codedom,$formname,$domdesc,$trails,$allitems) = @_;
177: my $output;
178: my %add_entries = (topmargin => "0",
179: marginheight => "0",);
180: my $js = '<script type="text/javascript">'."\n".
181: &courselink_javascript().'</script>'."\n";
182: my $start_page =
183: &Apache::loncommon::start_page('Course/Community Catalog',$js,
184: {
185: 'add_entries' => \%add_entries,
186: 'no_inline_link' => 1,});
187: $r->print($start_page);
188: if ($env{'form.numtitles'} > 0) {
189: &Apache::lonhtmlcommon::add_breadcrumb
190: ({href=>"/adm/coursecatalog",
191: text=>"Course/Community Catalog"});
192: }
193: &Apache::lonhtmlcommon::add_breadcrumb
194: ({href=>"javascript:document.$formname.submit()",
195: text=>"Course listing"},
196: {text=>"Course details"});
197: $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
198: $r->print('<br />'.&mt('Detailed course information:').'<br /><br />'.
199: &print_course_listing($codedom,undef,$trails,$allitems).
200: '<br /><br />');
201: $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
202: '<a href = "javascript:document.coursecatalog.submit()">'.
203: &mt('Back to course listing').'</a>'.
204: &Apache::lonhtmlcommon::echo_form_input(['coursenum','catalogfilter',
205: 'showdetails','courseid']).'</form>');
206: return;
207: }
208:
209: sub courselink_javascript {
210: return <<"END";
211:
212: function ToSyllabus(cdom,cnum) {
213: if (cdom == '' || cdom == null) {
214: return;
215: }
216: if (cnum == '' || cnum == null) {
217: return;
218: }
219: document.linklaunch.action = "/public/"+cdom+"/"+cnum+"/syllabus";
220: document.linklaunch.submit();
221: }
222:
223: function ToSelfenroll(courseid) {
224: if (courseid == '') {
225: return;
226: }
227: document.linklaunch.action = "/adm/selfenroll";
228: document.linklaunch.courseid.value = courseid;
229: document.linklaunch.submit();
230: }
231:
232: END
233: }
234:
235: sub instcode_course_selector {
236: my ($r,$codedom,$formname,$domdesc,$catlinks,$catjs) = @_;
237: my %coursecodes = ();
238: my %codes = ();
239: my @codetitles = ();
240: my %cat_titles = ();
241: my %cat_order = ();
242: my %cat_items;
243: my $caller = 'global';
244: my $format_reply;
245: my %add_entries = (topmargin => "0",
246: marginheight => "0",);
247: my ($jscript,$totcodes,$numtitles,$lasttitle) =
248: &Apache::courseclassifier::instcode_selectors_data($codedom,$formname,
249: \%cat_items,\@codetitles,\%cat_titles,\%cat_order);
250: my $js = '<script type"text/javascript">'."\n$jscript\n$catjs\n".
251: '</script>';
252: if ($totcodes) {
253: if (($env{'form.state'} eq 'listing') && ($numtitles > 0)) {
254: $add_entries{'onLoad'} = 'setElements()';
255: }
256: &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles);
257: my $cat_maxdepth = $env{'form.catalog_maxdepth'};
258: $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
259: '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'."\n".
260: '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'."\n".
261: '<input type="hidden" name="currcat_0" value="instcode::0" />'.
262: &additional_filters($codedom));
263: if ($numtitles > 0) {
264: $r->print('<b>'.&mt('Choose which course(s) to list.').'</b><br />'.
265: &Apache::courseclassifier::build_instcode_selectors($numtitles,
266: $lasttitle,\%cat_items,\@codetitles,\%cat_titles,\%cat_order));
267: }
268: $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
269: '<input type="hidden" name="sortby" value="" />'."\n".
270: '<input type="hidden" name="state" value="listing" />'."\n".
271: '<input type="hidden" name="form.currcat_0" value="instcode::0" />'."\n".
272: '<input type="submit" name="catalogfilter" value="'.
273: &mt('Display courses').'" />'.
274: '<input type="hidden" name="numtitles" value="'.$numtitles.
275: '" /></form><br /><br />');
276: } else {
277: $js = '<script type"text/javascript">'."\n$catjs\n".'</script>';
278: &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles);
279: my $cat_maxdepth = $env{'form.catalog_maxdepth'};
280: $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
281: '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'.
282: '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'.
283: '<input type="hidden" name="currcat_0" value="instcode::0" />');
284: $r->print('<br />'.&mt('No official courses to display for [_1].',$domdesc).'</form>');
285: }
286: return $numtitles;
287: }
288:
289: sub cat_header {
290: my ($r,$codedom,$js,$add_entries,$catlinks,$numtitles) = @_;
291: my $start_page =
292: &Apache::loncommon::start_page('Other',$js,
293: {
294: 'add_entries' => $add_entries,
295: 'no_inline_link' => 1,});
296: $r->print($start_page);
297: if ($env{'form.state'} eq 'listing') {
298: if ($numtitles > 0) {
299: &Apache::lonhtmlcommon::add_breadcrumb
300: ({href=>"/adm/coursecatalog",
301: text=>"Course/Community Catalog"},
302: {text=>"Course listing"});
303: } else {
304: &Apache::lonhtmlcommon::add_breadcrumb
305: ({text=>"Course listing"});
306: }
307: } else {
308: &Apache::lonhtmlcommon::add_breadcrumb
309: ({href=>"/adm/coursecatalog",
310: text=>"Course/Community Catalog"});
311: }
312: $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
313: my $onchange;
314: unless ($env{'form.interface'} eq 'textual') {
315: $onchange = 'this.form.submit()';
316: }
317: $r->print('<form name="coursecatdom" method="post" action="/adm/coursecatalog">'.
318: '<table border="0"><tr><td><b>'.&mt('Domain:').'</b></td><td>'.
319: &Apache::loncommon::select_dom_form($codedom,'showdom','',1,$onchange));
320: if (!$onchange) {
321: $r->print(' <input type="submit" name="godom" value="'.&mt('Change').'" />');
322: }
323: $r->print('</td></tr></table></form>'.
324: '<form name="coursecats" method="post" action="/adm/coursecatalog"'.
325: ' onsubmit="return check_selected();">'.
326: '<table border="0"><tr>'.$catlinks.'</tr></table></form>');
327: return;
328: }
329:
330: sub category_breadcrumbs {
331: my ($dom,@cats) = @_;
332: my $crumbsymbol = ' ▶ ';
333: my ($currdepth,$deeper) = &get_depth_values();
334: my $currcat_str =
335: '<input type="hidden" name="catalog_maxdepth" value="'.$deeper.'" />'.
336: '<input type="hidden" name="showdom" value="'.$dom.'" />';
337: my $catlinks = '<td valign="top"><b>'.&mt('Catalog:').'</b></td><td><table><tr>';
338: my $has_subcats;
339: my $selitem;
340: for (my $i=0; $i<$deeper; $i++) {
341: $currcat_str .= '<input type="hidden" name="currcat_'.$i.'" value="'.$env{'form.currcat_'.$i}.'" />';
342: my ($cattitle,$shallower);
343: if ($i == 0) {
344: if (ref($cats[0]) eq 'ARRAY') {
345: if (@{$cats[0]} > 1) {
346: $cattitle = &mt('Main Categories');
347: }
348: }
349: } else {
350: $shallower = $i-1;
351: my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
352: $cattitle = $cat;
353: }
354: if ($cattitle ne '') {
355: $catlinks .= '<td valign="top"><a href="javascript:setCatDepth('."'$shallower'".')">'.$cattitle.'</a>'.$crumbsymbol.'</td>';
356: }
357: }
358: if ($deeper == 0) {
359: $catlinks .= '<td>';
360: if (ref($cats[0]) eq 'ARRAY') {
361: if ((@{$cats[0]} == 1) && (@cats == 1)) {
362: if ($cats[0][0] eq 'instcode') {
363: $catlinks .= &mt('Official courses (with institutional codes)').
364: '<input type="hidden" name="currcat_0" value="instcode::0" />';
365: $env{'form.currcat_0'} = 'instcode::0';
366: } elsif ($cats[0][0] eq 'communities') {
367: $catlinks .= &mt('Communities').
368: '<input type="hidden" name="currcat_0" value="instcode::0" />';
369: $env{'form.currcat_0'} = 'communities::0';
370: } else {
371: my $name = $cats[0][0];
372: my $item = &escape($name).'::0';
373: $catlinks .= $name.
374: '<input type="hidden" name="currcat_0" value="'.$item.'" />';
375: $env{'form.currcat_0'} = $item;
376: }
377: } else {
378: $has_subcats = 1;
379: my $buttontext = &mt('Show subcategories');
380: $selitem = 'currcat_0';
381: $catlinks .= '<select name="'.$selitem.'">'."\n";
382: if (@{$cats[0]} > 1) {
383: $catlinks .= '<option value="" selected="selected">'.&mt('Select').'</option>'."\n";
384: $buttontext = &mt('Pick main category');
385: }
386: for (my $i=0; $i<@{$cats[0]}; $i++) {
387: my $name = $cats[0][$i];
388: my $item = &escape($name).'::0';
389: $catlinks .= '<option value="'.$item.'">';
390: if ($name eq 'instcode') {
391: $catlinks .= &mt('Official courses (with institutional codes)');
392: } elsif ($name eq 'communities') {
393: $catlinks .= &mt('Communities');
394: } else {
395: $catlinks .= $name;
396: }
397: $catlinks .= '</option>'."\n";
398: }
399: $catlinks .= '</select>'."\n".
400: ' <input type="submit" name="gocats" value="'.
401: $buttontext.'" />';
402: }
403: } else {
404: $catlinks .= &mt('Official courses (with institutional codes)').
405: '<input type="hidden" name="currcat_0" value="instcode::0" />';
406: $env{'form.currcat_0'} = 'instcode::0';
407: }
408: } else {
409: my ($cat,$container,$depth);
410: if ($env{'form.currcat_'.$currdepth} eq '') {
411: my $shallower = $currdepth - 1;
412: ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
413: } else {
414: ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$currdepth});
415: }
416: my $deeper = $depth +1;
417: my $currcat = $cat;
418: if ($cat eq 'instcode') {
419: $currcat = &mt('Official courses (with institutional codes)');
420: } elsif ($cat eq 'communities') {
421: $currcat = &mt('Communities');
422: }
423: $catlinks .= '<td><b>'.$currcat.'</b>';
424: if (ref($cats[$deeper]{$cat}) eq 'ARRAY') {
425: $has_subcats = 1;
426: my $buttontext = &mt('Show subcategories');
427: $selitem = 'currcat_'.$deeper;
428: $catlinks .= ': <select name="'.$selitem.'">';
429: if (@{$cats[$deeper]{$cat}} > 1) {
430: $catlinks .= '<option value="" selected="selected">'.
431: &mt('Select').'</option>';
432: $buttontext = &mt('Pick subcategory');
433: }
434: for (my $k=0; $k<@{$cats[$deeper]{$cat}}; $k++) {
435: my $name = $cats[$deeper]{$cat}[$k];
436: my $item = &escape($name).':'.&escape($cat).':'.$deeper;
437: $catlinks .= '<option value="'.$item.'">'.$name.'</option>'."\n";
438: }
439: $catlinks .= '</select>'."\n".
440: ' <input type="submit" name="gocats" value="'.
441: $buttontext.'" />';
442: } elsif ($cat ne 'instcode') {
443: $catlinks .= ' '.&mt('(No subcategories)');
444: }
445: }
446: $catlinks .= $currcat_str.'</td></tr></table></td>';
447: return ($catlinks,$has_subcats,$selitem);
448: }
449:
450: sub get_depth_values {
451: my $currdepth = 0;
452: my $deeper = 0;
453: if ($env{'form.catalog_maxdepth'} ne '') {
454: $currdepth = $env{'form.catalog_maxdepth'};
455: if ($env{'form.currcat_'.$currdepth} eq '') {
456: $deeper = $currdepth;
457: } else {
458: $deeper = $currdepth + 1;
459: }
460: }
461: return ($currdepth,$deeper);
462: }
463:
464: sub additional_filters {
465: my ($codedom,$has_subcats) = @_;
466: my $output = '<table>';
467: if (($env{'form.currcat_0'} ne 'instcode::0') &&
468: ($env{'form.currcat_0'} ne '') && ($has_subcats)) {
469: my $include_subcat_status;
470: if ($env{'form.withsubcats'}) {
471: $include_subcat_status = 'checked="checked" ';
472: }
473: my $counter = $env{'form.catalog_maxdepth'};
474: if ($counter > 0) {
475: if ($env{'form.state'} eq 'listing') {
476: $counter --;
477: } elsif ($env{'form.currcat_'.$counter} eq '') {
478: $counter --;
479: }
480: }
481: my ($catname) = split(/:/,$env{'form.currcat_'.$counter});
482: if ($catname ne '') {
483: $output .= '<tr><td><label>'.
484: '<input type="checkbox" name="withsubcats" value="1" '.
485: $include_subcat_status.'/>'.
486: &mt('Include subcategories within "[_1]"',
487: &unescape($catname)).'</label></td></tr>';
488: }
489: }
490: my $show_selfenroll_status;
491: if ($env{'form.showselfenroll'}) {
492: $show_selfenroll_status = 'checked="checked" ';
493: }
494: my $selfenroll_text;
495: if ($env{'form.currcat_0'} eq 'communities::0') {
496: $selfenroll_text = &mt('Only show communities which allow self-enrollment');
497: } else {
498: $selfenroll_text = &mt('Only show courses which allow self-enrollment');
499: }
500: $output .= '<tr><td>'.
501: '<label><input type="checkbox" name="showselfenroll" value="1" '.
502: $show_selfenroll_status.'/>'.$selfenroll_text.
503: '</label></td></tr>';
504: if (&user_is_dc($codedom)) {
505: my $showdetails_status;
506: if ($env{'form.showdetails'}) {
507: $showdetails_status = 'checked="checked" ';
508: }
509: my $showhidden_status;
510: if ($env{'form.showhidden'}) {
511: $showhidden_status = 'checked="checked" ';
512: }
513: my $dc_title = &Apache::lonnet::plaintext('dc');
514: my ($details_text,$hidden_text);
515: if ($env{'form.currcat_0'} eq 'communities::0') {
516: $details_text = &mt('Show full details for each community ([_1] only)',$dc_title);
517: $hidden_text = &mt('Include communities set to be hidden from catalog ([_1] only)',$dc_title);
518: } else {
519: $details_text = &mt('Show full details for each course ([_1] only)',$dc_title);
520: $hidden_text = &mt('Include courses set to be hidden from catalog ([_1] only)',$dc_title);
521: }
522: $output .= '<tr><td>'."\n".
523: '<label><input type="checkbox" name="showdetails" value="1" '.
524: $showdetails_status.'/>'.$details_text.
525: '</label>'."\n".'</td></tr><tr><td>'.
526: '<label><input type="checkbox" name="showhidden" value="1" '.
527: $showhidden_status.'/>'.$hidden_text.
528: '</label>'."\n".'</td></tr>';
529: }
530: $output .= '</table><br />';
531: return $output;
532: }
533:
534: sub user_is_dc {
535: my ($codedom) = @_;
536: if (exists($env{'user.role.dc./'.$codedom.'/'})) {
537: my $livedc = 1;
538: my $now = time;
539: my ($start,$end)=split(/\./,$env{'user.role.dc./'.$codedom.'/'});
540: if ($start && $start>$now) { $livedc = 0; }
541: if ($end && $end <$now) { $livedc = 0; }
542: return $livedc;
543: }
544: return;
545: }
546:
547: sub search_official_courselist {
548: my ($domain,$numtitles) = @_;
549: my $instcode = &Apache::courseclassifier::instcode_search_str($domain,$numtitles);
550: my $showhidden;
551: if (&user_is_dc($domain)) {
552: $showhidden = $env{'form.showhidden'};
553: }
554: my %courses =
555: &Apache::lonnet::courseiddump($domain,'.',1,$instcode,'.','.',undef,undef,
556: 'Course',1,$env{'form.showselfenroll'},undef,
557: $showhidden,'coursecatalog');
558: return %courses;
559: }
560:
561: sub search_courselist {
562: my ($domain,$subcats) = @_;
563: my $cat_maxdepth = $env{'form.catalog_maxdepth'};
564: my $filter = $env{'form.currcat_'.$cat_maxdepth};
565: if (($filter eq '') && ($cat_maxdepth > 0)) {
566: my $shallower = $cat_maxdepth - 1;
567: $filter = $env{'form.currcat_'.$shallower};
568: }
569: my %courses;
570: my $filterstr;
571: if ($filter ne '') {
572: if ($env{'form.withsubcats'}) {
573: if (ref($subcats) eq 'HASH') {
574: if (ref($subcats->{$filter}) eq 'ARRAY') {
575: $filterstr = join('&',@{$subcats->{$filter}});
576: if ($filterstr ne '') {
577: $filterstr = $filter.'&'.$filterstr;
578: }
579: } else {
580: $filterstr = $filter;
581: }
582: } else {
583: $filterstr = $filter;
584: }
585: } else {
586: $filterstr = $filter;
587: }
588: my ($showhidden,$typefilter);
589: if (&user_is_dc($domain)) {
590: $showhidden = $env{'form.showhidden'};
591: }
592: if ($env{'form.currcat_0'} eq 'communities::0') {
593: $typefilter = 'Community';
594: } else {
595: $typefilter = '.';
596: }
597: %courses =
598: &Apache::lonnet::courseiddump($domain,'.',1,'.','.','.',undef,undef,
599: $typefilter,1,$env{'form.showselfenroll'},
600: $filterstr,$showhidden,'coursecatalog');
601: }
602: return %courses;
603: }
604:
605: sub print_course_listing {
606: my ($domain,$numtitles,$trails,$allitems,$subcats) = @_;
607: my $output;
608: my %courses;
609: my $knownuser = &user_is_known();
610: my $details = $env{'form.coursenum'};
611: if (&user_is_dc($domain)) {
612: if ($env{'form.showdetails'}) {
613: $details = 1;
614: }
615: }
616: if ($env{'form.coursenum'} ne '') {
617: %courses = &Apache::lonnet::courseiddump($domain,'.',1,'.','.',
618: $env{'form.coursenum'},
619: undef,undef,'.',1);
620: if (keys(%courses) == 0) {
621: $output .= &mt('The courseID provided does not match a course in this domain.');
622: return $output;
623: }
624: } else {
625: if ($env{'form.currcat_0'} eq 'instcode::0') {
626: %courses = &search_official_courselist($domain,$numtitles);
627: } else {
628: %courses = &search_courselist($domain,$subcats);
629: }
630: if (keys(%courses) == 0) {
631: if ($env{'form.currcat_0'} eq 'communities::0') {
632: $output = &mt('No communities match the criteria you selected.');
633: } else {
634: $output = &mt('No courses match the criteria you selected.');
635: }
636: return $output;
637: }
638: if (($knownuser) && (!$env{'form.showdetails'}) && (!&user_is_dc($domain))) {
639: $output = '<b>'.&mt('Note for students:').'</b> '
640: .&mt('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.')
641: .'<br /><br />';
642: }
643: }
644: my $now = time;
645: my %domconfig =
646: &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
647: $output .= &construct_data_table($knownuser,\%courses,$details,undef,$now,\%domconfig,$trails,$allitems);
648: $output .= "\n".'<form name="linklaunch" method="post" action="">'.
649: '<input type="hidden" name="backto" value="coursecatalog" />'.
650: '<input type="hidden" name="courseid" value="" />'.
651: &Apache::lonhtmlcommon::echo_form_input(['catalogfilter','courseid']).'</form>';
652: return $output;
653: }
654:
655: sub construct_data_table {
656: my ($knownuser,$courses,$details,$usersections,$now,$domconfig,$trails,
657: $allitems) = @_;
658: my %sortname;
659: if (($details eq '') || ($env{'form.showdetails'})) {
660: $sortname{'Code'} = 'code';
661: $sortname{'Categories'} = 'cats';
662: $sortname{'Title'} = 'title';
663: $sortname{'Owner(s)'} = 'owner';
664: }
665: my $output = &Apache::loncommon::start_data_table().
666: &Apache::loncommon::start_data_table_header_row();
667: my @coltitles = ('Count');
668: if ($env{'form.currcat_0'} eq 'instcode::0') {
669: push(@coltitles,'Code');
670: } else {
671: push(@coltitles,'Categories');
672: }
673: push(@coltitles,('Sections','Crosslisted','Title','Owner(s)'));
674: if (ref($usersections) eq 'HASH') {
675: $coltitles[1] = 'Your Section';
676: }
677: foreach my $item (@coltitles) {
678: $output .= '<th>';
679: if (defined($sortname{$item})) {
680: $output .= '<a href="javascript:changeSort('."'$sortname{$item}'".')">'.&mt($item).'</a>';
681: } elsif ($item eq 'Count') {
682: $output .= ' ';
683: } else {
684: $output .= &mt($item);
685: }
686: $output .= '</th>';
687: }
688: if ($knownuser) {
689: if ($details) {
690: $output .=
691: '<th>'.&mt('Default Access Dates for Students').'</th>'.
692: '<th>'.&mt('Student Counts').'</th>'.
693: '<th>'.&mt('Auto-enrollment of[_1]registered students','<br />').'</th>';
694: } else {
695: $output .= '<th>'.&mt('Details').'</th>';
696: }
697: }
698: $output .= '<th>'.&mt('Self-enroll (if permitted)').'</th>';
699: &Apache::loncommon::end_data_table_header_row();
700: my %courseinfo = &build_courseinfo_hash($courses,$knownuser,$details,
701: $usersections);
702: my %Sortby;
703: foreach my $course (sort(keys(%{$courses}))) {
704: if ($env{'form.sortby'} eq 'code') {
705: push(@{$Sortby{$courseinfo{$course}{'code'}}},$course);
706: } elsif ($env{'form.sortby'} eq 'cats') {
707: push(@{$Sortby{$courseinfo{$course}{'categories'}}},$course);
708: } elsif ($env{'form.sortby'} eq 'owner') {
709: push(@{$Sortby{$courseinfo{$course}{'ownerlastnames'}}},$course);
710: } else {
711: my $clean_title = $courseinfo{$course}{'title'};
712: $clean_title =~ s/\W+//g;
713: if ($clean_title eq '') {
714: $clean_title = $courseinfo{$course}{'title'};
715: }
716: push(@{$Sortby{$clean_title}},$course);
717: }
718: }
719: my @sorted_courses;
720: if (($env{'form.sortby'} eq 'code') || ($env{'form.sortby'} eq 'owner') ||
721: ($env{'form.sortby'} eq 'cats')) {
722: @sorted_courses = sort(keys(%Sortby));
723: } else {
724: @sorted_courses = sort { lc($a) cmp lc($b) } (keys(%Sortby));
725: }
726: my $count = 1;
727: foreach my $item (@sorted_courses) {
728: foreach my $course (@{$Sortby{$item}}) {
729: $output.=&Apache::loncommon::start_data_table_row();
730: $output.=&courseinfo_row($courseinfo{$course},$knownuser,$details,
731: \$count,$now,$course,$trails,$allitems);
732: $output.=&Apache::loncommon::end_data_table_row();
733: }
734: }
735: $output .= &Apache::loncommon::end_data_table();
736: return $output;
737: }
738:
739: sub build_courseinfo_hash {
740: my ($courses,$knownuser,$details,$usersections) = @_;
741: my %courseinfo;
742: my $now = time;
743: foreach my $course (keys(%{$courses})) {
744: my $descr;
745: if (ref($courses->{$course}) eq 'HASH') {
746: $descr = $courses->{$course}{'description'};
747: }
748: my $cleandesc=&HTML::Entities::encode($descr,'<>&"');
749: $cleandesc=~s/'/\\'/g;
750: $cleandesc =~ s/^\s+//;
751: my ($cdom,$cnum)=split(/\_/,$course);
752: my ($instcode,$singleowner,$ttype,$selfenroll_types,
753: $selfenroll_start,$selfenroll_end,@owners,%ownernames,$categories);
754: if (ref($courses->{$course}) eq 'HASH') {
755: $descr = $courses->{$course}{'description'};
756: $instcode = $courses->{$course}{'inst_code'};
757: $singleowner = $courses->{$course}{'owner'};
758: $ttype = $courses->{$course}{'type'};
759: $selfenroll_types = $courses->{$course}{'selfenroll_types'};
760: $selfenroll_start = $courses->{$course}{'selfenroll_start_date'};
761: $selfenroll_end = $courses->{$course}{'selfenroll_end_date'};
762: $categories = $courses->{$course}{'categories'};
763: push(@owners,$singleowner);
764: if (ref($courses->{$course}{'co-owners'}) eq 'ARRAY') {
765: foreach my $item (@{$courses->{$course}{'co-owners'}}) {
766: push(@owners,$item);
767: }
768: }
769: }
770: foreach my $owner (@owners) {
771: my ($ownername,$ownerdom);
772: if ($owner =~ /:/) {
773: ($ownername,$ownerdom) = split(/:/,$owner);
774: } else {
775: $ownername = $owner;
776: if ($owner ne '') {
777: $ownerdom = $cdom;
778: }
779: }
780: if ($ownername ne '' && $ownerdom ne '') {
781: my %namehash=&Apache::loncommon::getnames($ownername,$ownerdom);
782: $ownernames{$ownername.':'.$ownerdom} = \%namehash;
783: }
784: }
785: $courseinfo{$course}{'cdom'} = $cdom;
786: $courseinfo{$course}{'cnum'} = $cnum;
787: $courseinfo{$course}{'code'} = $instcode;
788: my @lastnames;
789: foreach my $owner (keys(%ownernames)) {
790: if (ref($ownernames{$owner}) eq 'HASH') {
791: push(@lastnames,$ownernames{$owner}{'lastname'});
792: }
793: }
794: $courseinfo{$course}{'ownerlastnames'} = join(', ',sort(@lastnames));
795: $courseinfo{$course}{'title'} = $cleandesc;
796: $courseinfo{$course}{'owner'} = $singleowner;
797: $courseinfo{$course}{'selfenroll_types'} = $selfenroll_types;
798: $courseinfo{$course}{'selfenroll_start'} = $selfenroll_start;
799: $courseinfo{$course}{'selfenroll_end'} = $selfenroll_end;
800: $courseinfo{$course}{'categories'} = $categories;
801:
802: my %coursehash = &Apache::lonnet::dump('environment',$cdom,$cnum);
803: my @classids;
804: my @crosslistings;
805: my ($seclist,$numsec) =
806: &identify_sections($coursehash{'internal.sectionnums'});
807: if (ref($usersections) eq 'HASH') {
808: if (ref($usersections->{$course}) eq 'ARRAY') {
809: $seclist = join(', ',@{$usersections->{$course}});
810: }
811: }
812: $courseinfo{$course}{'seclist'} = $seclist;
813: my ($xlist_items,$numxlist) =
814: &identify_sections($coursehash{'internal.crosslistings'});
815: my $showsyllabus = 1; # default is to include a syllabus link
816: if (defined($coursehash{'showsyllabus'})) {
817: $showsyllabus = $coursehash{'showsyllabus'};
818: }
819: $courseinfo{$course}{'showsyllabus'} = $showsyllabus;
820: if (((defined($env{'form.coursenum'}) && ($cnum eq $env{'form.coursenum'}))) ||
821: ($knownuser && ($details == 1))) {
822: $courseinfo{$course}{'counts'} = &count_students($cdom,$cnum,$numsec);
823: $courseinfo{$course}{'autoenrollment'} =
824: &autoenroll_info(\%coursehash,$now,$seclist,$xlist_items,
825: $instcode,\@owners,$cdom,$cnum);
826:
827: my $startaccess = '';
828: my $endaccess = '';
829: my $accessdates;
830: if ( defined($coursehash{'default_enrollment_start_date'}) ) {
831: $startaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_start_date'});
832: }
833: if ( defined($coursehash{'default_enrollment_end_date'}) ) {
834: $endaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_end_date'});
835: if ($coursehash{'default_enrollment_end_date'} == 0) {
836: $endaccess = &mt('No ending date');
837: }
838: }
839: if ($startaccess) {
840: $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$startaccess).'<br />';
841: }
842: if ($endaccess) {
843: $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$endaccess).'<br />';
844: }
845: if (($selfenroll_types ne '') &&
846: ($selfenroll_end > 0 && $selfenroll_end > $now)) {
847: my ($selfenroll_start_access,$selfenroll_end_access);
848: if (($coursehash{'default_enrollment_start_date'} ne
849: $coursehash{'internal.selfenroll_start_access'}) ||
850: ($coursehash{'default_enrollment_end_date'} ne
851: $coursehash{'internal.selfenroll_end_access'})) {
852: if ( defined($coursehash{'internal.selfenroll_start_access'}) ) {
853: $selfenroll_start_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_start_access'});
854: }
855: if ( defined($coursehash{'default_enrollment_end_date'}) ) {
856: $selfenroll_end_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_end_access'});
857: if ($coursehash{'internal.selfenroll_end_access'} == 0) {
858: $selfenroll_end_access = &mt('No ending date');
859: }
860: }
861: if ($selfenroll_start_access || $selfenroll_end_access) {
862: $accessdates .= '<br/><br /><i>'.&mt('Self-enrollers:').'</i><br />';
863: if ($selfenroll_start_access) {
864: $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$selfenroll_start_access).'<br />';
865: }
866: if ($selfenroll_end_access) {
867: $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$selfenroll_end_access).'<br />';
868: }
869: }
870: }
871: }
872: $courseinfo{$course}{'access'} = $accessdates;
873: }
874: if ($xlist_items eq '') {
875: $xlist_items = &mt('No');
876: }
877: $courseinfo{$course}{'xlist'} = $xlist_items;
878: }
879: return %courseinfo;
880: }
881:
882: sub count_students {
883: my ($cdom,$cnum,$numsec) = @_;
884: my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
885: my %student_count = (
886: Active => 0,
887: Future => 0,
888: Expired => 0,
889: );
890: my %idx;
891: $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
892: my %status_title = &Apache::lonlocal::texthash(
893: Expired => 'Previous access',
894: Active => 'Current access',
895: Future => 'Future access',
896: );
897:
898: while (my ($student,$data) = each(%$classlist)) {
899: $student_count{$data->[$idx{'status'}]} ++;
900: }
901:
902: my $countslist = &mt('[quant,_1,section:,sections:,No sections]',$numsec).'<br />';
903: foreach my $status ('Active','Future') {
904: $countslist .= '<span class="LC_nobreak">'.$status_title{$status}.': '.
905: $student_count{$status}.'</span><br />';
906: }
907: return $countslist;
908: }
909:
910: sub courseinfo_row {
911: my ($info,$knownuser,$details,$countref,$now,$course,$trails,$allitems) = @_;
912: my ($cdom,$cnum,$title,$ownerlast,$code,$owner,$seclist,$xlist_items,
913: $accessdates,$showsyllabus,$counts,$autoenrollment,$output,$categories);
914: if (ref($info) eq 'HASH') {
915: $cdom = $info->{'cdom'};
916: $cnum = $info->{'cnum'};
917: $title = $info->{'title'};
918: $ownerlast = $info->{'ownerlastnames'};
919: $code = $info->{'code'};
920: $owner = $info->{'owner'};
921: $seclist = $info->{'seclist'};
922: $xlist_items = $info->{'xlist'};
923: $accessdates = $info->{'access'};
924: $counts = $info->{'counts'};
925: $autoenrollment = $info->{'autoenrollment'};
926: $showsyllabus = $info->{'showsyllabus'};
927: $categories = $info->{'categories'};
928: } else {
929: $output = '<td colspan="8">'.&mt('No information available for [_1].',
930: $code).'</td>';
931: return $output;
932: }
933: $output .= '<td>'.$$countref.'</td>';
934: if ($env{'form.currcat_0'} eq 'instcode::0') {
935: $output .= '<td>'.$code.'</td>';
936: } else {
937: my ($categorylist,@cats);
938: if ($categories ne '') {
939: @cats = split('&',$categories);
940: }
941: if ((ref($trails) eq 'ARRAY') && (ref($allitems) eq 'HASH')) {
942: my @categories = map { $trails->[$allitems->{$_}]; } @cats;
943: $categorylist = join('<br />',@categories);
944: }
945: if ($categorylist eq '') {
946: $categorylist = ' ';
947: }
948: $output .= '<td>'.$categorylist.'</td>';
949: }
950: $output .= '<td>'.$seclist.'</td>'.
951: '<td>'.$xlist_items.'</td>'.
952: '<td>'.$title.' <font size="-2">';
953: if ($showsyllabus) {
954: $output .= '<a href="javascript:ToSyllabus('."'$cdom','$cnum'".')">'.&mt('Syllabus').'</a>';
955: } else {
956: $output .= ' ';
957: }
958: $output .= '</font></td>'.
959: '<td>'.$ownerlast.'</td>';
960: if ($knownuser) {
961: if ($details) {
962: $output .=
963: '<td>'.$accessdates.'</td>'.
964: '<td>'.$counts.'</td>'.
965: '<td>'.$autoenrollment.'</td>';
966: } else {
967: $output .= "<td><a href=\"javascript:setCourseId('$cnum')\">".&mt('Show more details').'</a></td>';
968: }
969: }
970: my $selfenroll;
971: if ($info->{'selfenroll_types'}) {
972: my $showstart = &Apache::lonlocal::locallocaltime($info->{'selfenroll_start'});
973: my $showend = &Apache::lonlocal::locallocaltime($info->{'selfenroll_end'});
974: if (($info->{'selfenroll_end'} > 0) && ($info->{'selfenroll_end'} > $now)) {
975: if (($info->{'selfenroll_start'} > 0) && ($info->{'selfenroll_start'} > $now)) {
976: $output .= '<td>'.&mt('Starts: [_1]','<span class="LC_cusr_emph">'.$showstart.'</span>').'<br />'.&mt('Ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>').'</td>';
977: } else {
978: $output .= '<td><a href="javascript:ToSelfenroll('."'$course'".')">'.&mt('Enroll in course').'</a></td>';
979: }
980: $selfenroll = 1;
981: }
982: }
983: if (!$selfenroll) {
984: $output .= '<td> </td>';
985: }
986: $$countref ++;
987: return $output;
988: }
989:
990: sub identify_sections {
991: my ($seclist) = @_;
992: my @secnums;
993: if ($seclist =~ /,/) {
994: my @sections = split(/,/,$seclist);
995: foreach my $sec (@sections) {
996: $sec =~ s/:[^:]*$//;
997: push(@secnums,$sec);
998: }
999: } else {
1000: if ($seclist =~ m/^([^:]+):/) {
1001: my $sec = $1;
1002: if (!grep(/^\Q$sec\E$/,@secnums)) {
1003: push(@secnums,$sec);
1004: }
1005: }
1006: }
1007: @secnums = sort {$a <=> $b} @secnums;
1008: $seclist = join(', ',@secnums);
1009: my $numsec = @secnums;
1010: return ($seclist,$numsec);
1011: }
1012:
1013: sub get_valid_classes {
1014: my ($seclist,$xlist_items,$crscode,$owners,$cdom,$cnum) = @_;
1015: my $response;
1016: my %validations;
1017: @{$validations{'sections'}} = ();
1018: @{$validations{'xlists'}} = ();
1019: my $totalitems = 0;
1020: if ($seclist) {
1021: foreach my $sec (split(/, /,$seclist)) {
1022: my $class = $crscode.$sec;
1023: if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
1024: $class) eq 'ok') {
1025: if (!grep(/^\Q$sec$\E/,@{$validations{'sections'}})) {
1026: push(@{$validations{'sections'}},$sec);
1027: $totalitems ++;
1028: }
1029: }
1030: }
1031: }
1032: if ($xlist_items) {
1033: foreach my $item (split(/, /,$xlist_items)) {
1034: if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
1035: $item) eq 'ok') {
1036: if (!grep(/^\Q$item$\E/,@{$validations{'xlists'}})) {
1037: push(@{$validations{'xlists'}},$item);
1038: $totalitems ++;
1039: }
1040: }
1041: }
1042: }
1043: if ($totalitems > 0) {
1044: if (@{$validations{'sections'}}) {
1045: $response = &mt('Sections:').' '.
1046: join(', ',@{$validations{'sections'}}).'<br />';
1047: }
1048: if (@{$validations{'xlists'}}) {
1049: $response .= &mt('Courses:').' '.
1050: join(', ',@{$validations{'xlists'}});
1051: }
1052: }
1053: return $response;
1054: }
1055:
1056: sub autoenroll_info {
1057: my ($coursehash,$now,$seclist,$xlist_items,$code,$owners,$cdom,$cnum) = @_;
1058: my $autoenrolldates = &mt('Not enabled');
1059: if (defined($coursehash->{'internal.autoadds'}) && $coursehash->{'internal.autoadds'} == 1) {
1060: my ($autostart,$autoend);
1061: if ( defined($coursehash->{'internal.autostart'}) ) {
1062: $autostart = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autostart'});
1063: }
1064: if ( defined($coursehash->{'internal.autoend'}) ) {
1065: $autoend = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autoend'});
1066: }
1067: if ($coursehash->{'internal.autostart'} > $now) {
1068: if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
1069: $autoenrolldates = &mt('Not enabled');
1070: } else {
1071: my $valid_classes =
1072: &get_valid_classes($seclist,$xlist_items,$code,
1073: $owners,$cdom,$cnum);
1074: if ($valid_classes ne '') {
1075: $autoenrolldates = &mt('Not enabled').'<br />'
1076: .&mt('Starts: [_1]',$autostart)
1077: .'<br />'.$valid_classes;
1078: }
1079: }
1080: } else {
1081: if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
1082: $autoenrolldates = &mt('Not enabled').'<br />'
1083: .&mt('Ended: [_1]',$autoend);
1084: } else {
1085: my $valid_classes = &get_valid_classes($seclist,$xlist_items,
1086: $code,$owners,$cdom,$cnum);
1087: if ($valid_classes ne '') {
1088: $autoenrolldates = &mt('Currently enabled').'<br />'.
1089: $valid_classes;
1090: }
1091: }
1092: }
1093: }
1094: return $autoenrolldates;
1095: }
1096:
1097: sub user_is_known {
1098: my $known = 0;
1099: if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public'
1100: && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
1101: $known = 1;
1102: }
1103: return $known;
1104: }
1105:
1106: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>