1: # The LearningOnline Network with CAPA
2: # Handler for displaying the course catalog interface
3: #
4: # $Id: coursecatalog.pm,v 1.58 2009/11/23 22:02:50 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><td>';
338: my $has_subcats;
339: my $selitem;
340: if (ref($cats[0]) eq 'ARRAY') {
341: if (@{$cats[0]} == 0) {
342: $catlinks .= &mt('No categories defined in this domain');
343: } elsif (@{$cats[0]} == 1) {
344: if ($cats[0][0] eq 'instcode') {
345: $catlinks .= &mt('Official courses (with institutional codes)');
346: $env{'form.currcat_0'} = 'instcode::0';
347: } elsif ($cats[0][0] eq 'communities') {
348: $catlinks .= &mt('Communities');
349: $env{'form.currcat_0'} = 'communities::0';
350: } else {
351: my $name = $cats[0][0];
352: my $item = &escape($name).'::0';
353: $catlinks .= $name;
354: $env{'form.currcat_0'} = $item;
355: }
356: $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
357: } else {
358: $catlinks .= &main_category_selector(@cats);
359: if (($env{'form.currcat_0'} ne '') &&
360: ($env{'form.currcat_0'} ne 'instcode::0')) {
361: $catlinks .= $crumbsymbol;
362: } else {
363: $catlinks .= '</td>';
364: }
365: }
366: } else {
367: $catlinks .= &mt('Official courses (with institutional codes)');
368: $env{'form.currcat_0'} = 'instcode::0';
369: $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
370: }
371: if ($deeper) {
372: for (my $i=1; $i<=$deeper; $i++) {
373: my $shallower = $i-1;
374: next if ($shallower == 0);
375: my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
376: if ($cat ne '') {
377: $catlinks .= '<td valign="top">'.
378: '<select name="currcat_'.$shallower.'" onchange="'.
379: 'setCatDepth('."'$shallower'".');this.form.submit();">';
380: if (ref($cats[$shallower]{$container}) eq 'ARRAY') {
381: $catlinks .= '<option value="">'.&mt('De-select').'</option>';
382: for (my $j=0; $j<@{$cats[$shallower]{$container}}; $j++) {
383: my $name = $cats[$shallower]{$container}[$j];
384: my $item = &escape($name).':'.$container.':'.$shallower;
385: my $selected = '';
386: if ($item eq $env{'form.currcat_'.$shallower}) {
387: $selected = ' selected="selected"';
388: }
389: $catlinks .=
390: '<option value="'.$item.'"'.$selected.'>'.$name.'</option>';
391: }
392: }
393: $catlinks .= '</select>';
394: }
395: unless ($i == $deeper) {
396: $catlinks .= $crumbsymbol;
397: }
398: }
399: my ($cat,$container,$depth);
400: if ($env{'form.currcat_'.$currdepth} eq '') {
401: my $shallower = $currdepth - 1;
402: ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
403: } else {
404: ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$currdepth});
405: }
406: my $deeperlevel = $depth +1;
407: if (ref($cats[$deeperlevel]{$cat}) eq 'ARRAY') {
408: $has_subcats = 1;
409: my $buttontext = &mt('Show subcategories');
410: my $selitem = 'currcat_'.$deeperlevel;
411: $catlinks .= ' <select name="'.$selitem.'" onchange="this.form.submit()">';
412: if (@{$cats[$deeperlevel]{$cat}}) {
413: $catlinks .= '<option value="" selected="selected">'.
414: &mt('Subcategory ...').'</option>';
415: }
416: for (my $k=0; $k<@{$cats[$deeperlevel]{$cat}}; $k++) {
417: my $name = $cats[$deeperlevel]{$cat}[$k];
418: my $item = &escape($name).':'.&escape($cat).':'.$deeperlevel;
419: $catlinks .= '<option value="'.$item.'">'.$name.'</option>'."\n";
420: }
421: $catlinks .= '</select>'."\n";
422: } elsif ($cat ne 'instcode') {
423: $catlinks .= ' '.&mt('(No subcategories)');
424: }
425: } else {
426: $selitem = 'currcat_0';
427: }
428: $catlinks .= $currcat_str.'</td></tr></table></td>';
429: return ($catlinks,$has_subcats,$selitem);
430: }
431:
432: sub main_category_selector {
433: my (@cats) = @_;
434: my $maincatlinks = '<select name="currcat_0" onchange="setCatDepth('."'0'".');this.form.submit();">'."\n";
435: if (ref($cats[0]) eq 'ARRAY') {
436: if (@{$cats[0]} > 1) {
437: my $selected = '';
438: if ($env{'form.currcat_0'} eq '') {
439: $selected = ' selected="selected"';
440: }
441: $maincatlinks .=
442: '<option value=""'.$selected.'>'.&mt('Select').'</option>'."\n";
443: }
444: for (my $i=0; $i<@{$cats[0]}; $i++) {
445: my $name = $cats[0][$i];
446: my $item = &escape($name).'::0';
447: my $selected;
448: if ($env{'form.currcat_0'} eq $item) {
449: $selected = ' selected="selected"';
450: }
451: $maincatlinks .= '<option value="'.$item.'"'.$selected.'>';
452: if ($name eq 'instcode') {
453: $maincatlinks .= &mt('Official courses (with institutional codes)');
454: } elsif ($name eq 'communities') {
455: $maincatlinks .= &mt('Communities');
456: } else {
457: $maincatlinks .= $name;
458: }
459: $maincatlinks .= '</option>'."\n";
460: }
461: $maincatlinks .= '</select>'."\n";
462: }
463: return $maincatlinks;
464: }
465:
466: sub get_depth_values {
467: my $currdepth = 0;
468: my $deeper = 0;
469: if ($env{'form.catalog_maxdepth'} ne '') {
470: $currdepth = $env{'form.catalog_maxdepth'};
471: if ($env{'form.currcat_'.$currdepth} eq '') {
472: $deeper = $currdepth;
473: } else {
474: $deeper = $currdepth + 1;
475: }
476: }
477: return ($currdepth,$deeper);
478: }
479:
480: sub additional_filters {
481: my ($codedom,$has_subcats) = @_;
482: my $output = '<table>';
483: if (($env{'form.currcat_0'} ne 'instcode::0') &&
484: ($env{'form.currcat_0'} ne '') && ($has_subcats)) {
485: my $include_subcat_status;
486: if ($env{'form.withsubcats'}) {
487: $include_subcat_status = 'checked="checked" ';
488: }
489: my $counter = $env{'form.catalog_maxdepth'};
490: if ($counter > 0) {
491: if ($env{'form.state'} eq 'listing') {
492: $counter --;
493: } elsif ($env{'form.currcat_'.$counter} eq '') {
494: $counter --;
495: }
496: }
497: my ($catname) = split(/:/,$env{'form.currcat_'.$counter});
498: if ($catname ne '') {
499: $output .= '<tr><td><label>'.
500: '<input type="checkbox" name="withsubcats" value="1" '.
501: $include_subcat_status.'/>'.
502: &mt('Include subcategories within "[_1]"',
503: &unescape($catname)).'</label></td></tr>';
504: }
505: }
506: my $show_selfenroll_status;
507: if ($env{'form.showselfenroll'}) {
508: $show_selfenroll_status = 'checked="checked" ';
509: }
510: my $selfenroll_text;
511: if ($env{'form.currcat_0'} eq 'communities::0') {
512: $selfenroll_text = &mt('Only show communities which allow self-enrollment');
513: } else {
514: $selfenroll_text = &mt('Only show courses which allow self-enrollment');
515: }
516: $output .= '<tr><td>'.
517: '<label><input type="checkbox" name="showselfenroll" value="1" '.
518: $show_selfenroll_status.'/>'.$selfenroll_text.
519: '</label></td></tr>';
520: if (&user_is_dc($codedom)) {
521: my $showdetails_status;
522: if ($env{'form.showdetails'}) {
523: $showdetails_status = 'checked="checked" ';
524: }
525: my $showhidden_status;
526: if ($env{'form.showhidden'}) {
527: $showhidden_status = 'checked="checked" ';
528: }
529: my $dc_title = &Apache::lonnet::plaintext('dc');
530: my ($details_text,$hidden_text);
531: if ($env{'form.currcat_0'} eq 'communities::0') {
532: $details_text = &mt('Show full details for each community ([_1] only)',$dc_title);
533: $hidden_text = &mt('Include communities set to be hidden from catalog ([_1] only)',$dc_title);
534: } else {
535: $details_text = &mt('Show full details for each course ([_1] only)',$dc_title);
536: $hidden_text = &mt('Include courses set to be hidden from catalog ([_1] only)',$dc_title);
537: }
538: $output .= '<tr><td>'."\n".
539: '<label><input type="checkbox" name="showdetails" value="1" '.
540: $showdetails_status.'/>'.$details_text.
541: '</label>'."\n".'</td></tr><tr><td>'.
542: '<label><input type="checkbox" name="showhidden" value="1" '.
543: $showhidden_status.'/>'.$hidden_text.
544: '</label>'."\n".'</td></tr>';
545: }
546: $output .= '</table><br />';
547: return $output;
548: }
549:
550: sub user_is_dc {
551: my ($codedom) = @_;
552: if (exists($env{'user.role.dc./'.$codedom.'/'})) {
553: my $livedc = 1;
554: my $now = time;
555: my ($start,$end)=split(/\./,$env{'user.role.dc./'.$codedom.'/'});
556: if ($start && $start>$now) { $livedc = 0; }
557: if ($end && $end <$now) { $livedc = 0; }
558: return $livedc;
559: }
560: return;
561: }
562:
563: sub search_official_courselist {
564: my ($domain,$numtitles) = @_;
565: my $instcode = &Apache::courseclassifier::instcode_search_str($domain,$numtitles);
566: my $showhidden;
567: if (&user_is_dc($domain)) {
568: $showhidden = $env{'form.showhidden'};
569: }
570: my %courses =
571: &Apache::lonnet::courseiddump($domain,'.',1,$instcode,'.','.',undef,undef,
572: 'Course',1,$env{'form.showselfenroll'},undef,
573: $showhidden,'coursecatalog');
574: return %courses;
575: }
576:
577: sub search_courselist {
578: my ($domain,$subcats) = @_;
579: my $cat_maxdepth = $env{'form.catalog_maxdepth'};
580: my $filter = $env{'form.currcat_'.$cat_maxdepth};
581: if (($filter eq '') && ($cat_maxdepth > 0)) {
582: my $shallower = $cat_maxdepth - 1;
583: $filter = $env{'form.currcat_'.$shallower};
584: }
585: my %courses;
586: my $filterstr;
587: if ($filter ne '') {
588: if ($env{'form.withsubcats'}) {
589: if (ref($subcats) eq 'HASH') {
590: if (ref($subcats->{$filter}) eq 'ARRAY') {
591: $filterstr = join('&',@{$subcats->{$filter}});
592: if ($filterstr ne '') {
593: $filterstr = $filter.'&'.$filterstr;
594: }
595: } else {
596: $filterstr = $filter;
597: }
598: } else {
599: $filterstr = $filter;
600: }
601: } else {
602: $filterstr = $filter;
603: }
604: my ($showhidden,$typefilter);
605: if (&user_is_dc($domain)) {
606: $showhidden = $env{'form.showhidden'};
607: }
608: if ($env{'form.currcat_0'} eq 'communities::0') {
609: $typefilter = 'Community';
610: } else {
611: $typefilter = '.';
612: }
613: %courses =
614: &Apache::lonnet::courseiddump($domain,'.',1,'.','.','.',undef,undef,
615: $typefilter,1,$env{'form.showselfenroll'},
616: $filterstr,$showhidden,'coursecatalog');
617: }
618: return %courses;
619: }
620:
621: sub print_course_listing {
622: my ($domain,$numtitles,$trails,$allitems,$subcats) = @_;
623: my $output;
624: my %courses;
625: my $knownuser = &user_is_known();
626: my $details = $env{'form.coursenum'};
627: if (&user_is_dc($domain)) {
628: if ($env{'form.showdetails'}) {
629: $details = 1;
630: }
631: }
632: if ($env{'form.coursenum'} ne '') {
633: %courses = &Apache::lonnet::courseiddump($domain,'.',1,'.','.',
634: $env{'form.coursenum'},
635: undef,undef,'.',1);
636: if (keys(%courses) == 0) {
637: $output .= &mt('The courseID provided does not match a course in this domain.');
638: return $output;
639: }
640: } else {
641: if ($env{'form.currcat_0'} eq 'instcode::0') {
642: %courses = &search_official_courselist($domain,$numtitles);
643: } else {
644: %courses = &search_courselist($domain,$subcats);
645: }
646: if (keys(%courses) == 0) {
647: if ($env{'form.currcat_0'} eq 'communities::0') {
648: $output = &mt('No communities match the criteria you selected.');
649: } else {
650: $output = &mt('No courses match the criteria you selected.');
651: }
652: return $output;
653: }
654: if (($knownuser) && (!$env{'form.showdetails'}) && (!&user_is_dc($domain))) {
655: $output = '<b>'.&mt('Note for students:').'</b> '
656: .&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.')
657: .'<br /><br />';
658: }
659: }
660: my $now = time;
661: my %domconfig =
662: &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
663: $output .= &construct_data_table($knownuser,\%courses,$details,undef,$now,\%domconfig,$trails,$allitems);
664: $output .= "\n".'<form name="linklaunch" method="post" action="">'.
665: '<input type="hidden" name="backto" value="coursecatalog" />'.
666: '<input type="hidden" name="courseid" value="" />'.
667: &Apache::lonhtmlcommon::echo_form_input(['catalogfilter','courseid']).'</form>';
668: return $output;
669: }
670:
671: sub construct_data_table {
672: my ($knownuser,$courses,$details,$usersections,$now,$domconfig,$trails,
673: $allitems) = @_;
674: my %sortname;
675: if (($details eq '') || ($env{'form.showdetails'})) {
676: $sortname{'Code'} = 'code';
677: $sortname{'Categories'} = 'cats';
678: $sortname{'Title'} = 'title';
679: $sortname{'Owner(s)'} = 'owner';
680: }
681: my $output = &Apache::loncommon::start_data_table().
682: &Apache::loncommon::start_data_table_header_row();
683: my @coltitles = ('Count');
684: if ($env{'form.currcat_0'} eq 'instcode::0') {
685: push(@coltitles,'Code');
686: } else {
687: push(@coltitles,'Categories');
688: }
689: push(@coltitles,('Sections','Crosslisted','Title','Owner(s)'));
690: if (ref($usersections) eq 'HASH') {
691: $coltitles[1] = 'Your Section';
692: }
693: foreach my $item (@coltitles) {
694: $output .= '<th>';
695: if (defined($sortname{$item})) {
696: $output .= '<a href="javascript:changeSort('."'$sortname{$item}'".')">'.&mt($item).'</a>';
697: } elsif ($item eq 'Count') {
698: $output .= ' ';
699: } else {
700: $output .= &mt($item);
701: }
702: $output .= '</th>';
703: }
704: if ($knownuser) {
705: if ($details) {
706: $output .=
707: '<th>'.&mt('Default Access Dates for Students').'</th>'.
708: '<th>'.&mt('Student Counts').'</th>'.
709: '<th>'.&mt('Auto-enrollment of[_1]registered students','<br />').'</th>';
710: } else {
711: $output .= '<th>'.&mt('Details').'</th>';
712: }
713: }
714: $output .= '<th>'.&mt('Self-enroll (if permitted)').'</th>';
715: &Apache::loncommon::end_data_table_header_row();
716: my %courseinfo = &build_courseinfo_hash($courses,$knownuser,$details,
717: $usersections);
718: my %Sortby;
719: foreach my $course (sort(keys(%{$courses}))) {
720: if ($env{'form.sortby'} eq 'code') {
721: push(@{$Sortby{$courseinfo{$course}{'code'}}},$course);
722: } elsif ($env{'form.sortby'} eq 'cats') {
723: push(@{$Sortby{$courseinfo{$course}{'categories'}}},$course);
724: } elsif ($env{'form.sortby'} eq 'owner') {
725: push(@{$Sortby{$courseinfo{$course}{'ownerlastnames'}}},$course);
726: } else {
727: my $clean_title = $courseinfo{$course}{'title'};
728: $clean_title =~ s/\W+//g;
729: if ($clean_title eq '') {
730: $clean_title = $courseinfo{$course}{'title'};
731: }
732: push(@{$Sortby{$clean_title}},$course);
733: }
734: }
735: my @sorted_courses;
736: if (($env{'form.sortby'} eq 'code') || ($env{'form.sortby'} eq 'owner') ||
737: ($env{'form.sortby'} eq 'cats')) {
738: @sorted_courses = sort(keys(%Sortby));
739: } else {
740: @sorted_courses = sort { lc($a) cmp lc($b) } (keys(%Sortby));
741: }
742: my $count = 1;
743: foreach my $item (@sorted_courses) {
744: foreach my $course (@{$Sortby{$item}}) {
745: $output.=&Apache::loncommon::start_data_table_row();
746: $output.=&courseinfo_row($courseinfo{$course},$knownuser,$details,
747: \$count,$now,$course,$trails,$allitems);
748: $output.=&Apache::loncommon::end_data_table_row();
749: }
750: }
751: $output .= &Apache::loncommon::end_data_table();
752: return $output;
753: }
754:
755: sub build_courseinfo_hash {
756: my ($courses,$knownuser,$details,$usersections) = @_;
757: my %courseinfo;
758: my $now = time;
759: foreach my $course (keys(%{$courses})) {
760: my $descr;
761: if (ref($courses->{$course}) eq 'HASH') {
762: $descr = $courses->{$course}{'description'};
763: }
764: my $cleandesc=&HTML::Entities::encode($descr,'<>&"');
765: $cleandesc=~s/'/\\'/g;
766: $cleandesc =~ s/^\s+//;
767: my ($cdom,$cnum)=split(/\_/,$course);
768: my ($instcode,$singleowner,$ttype,$selfenroll_types,
769: $selfenroll_start,$selfenroll_end,@owners,%ownernames,$categories);
770: if (ref($courses->{$course}) eq 'HASH') {
771: $descr = $courses->{$course}{'description'};
772: $instcode = $courses->{$course}{'inst_code'};
773: $singleowner = $courses->{$course}{'owner'};
774: $ttype = $courses->{$course}{'type'};
775: $selfenroll_types = $courses->{$course}{'selfenroll_types'};
776: $selfenroll_start = $courses->{$course}{'selfenroll_start_date'};
777: $selfenroll_end = $courses->{$course}{'selfenroll_end_date'};
778: $categories = $courses->{$course}{'categories'};
779: push(@owners,$singleowner);
780: if (ref($courses->{$course}{'co-owners'}) eq 'ARRAY') {
781: foreach my $item (@{$courses->{$course}{'co-owners'}}) {
782: push(@owners,$item);
783: }
784: }
785: }
786: foreach my $owner (@owners) {
787: my ($ownername,$ownerdom);
788: if ($owner =~ /:/) {
789: ($ownername,$ownerdom) = split(/:/,$owner);
790: } else {
791: $ownername = $owner;
792: if ($owner ne '') {
793: $ownerdom = $cdom;
794: }
795: }
796: if ($ownername ne '' && $ownerdom ne '') {
797: my %namehash=&Apache::loncommon::getnames($ownername,$ownerdom);
798: $ownernames{$ownername.':'.$ownerdom} = \%namehash;
799: }
800: }
801: $courseinfo{$course}{'cdom'} = $cdom;
802: $courseinfo{$course}{'cnum'} = $cnum;
803: $courseinfo{$course}{'code'} = $instcode;
804: my @lastnames;
805: foreach my $owner (keys(%ownernames)) {
806: if (ref($ownernames{$owner}) eq 'HASH') {
807: push(@lastnames,$ownernames{$owner}{'lastname'});
808: }
809: }
810: $courseinfo{$course}{'ownerlastnames'} = join(', ',sort(@lastnames));
811: $courseinfo{$course}{'title'} = $cleandesc;
812: $courseinfo{$course}{'owner'} = $singleowner;
813: $courseinfo{$course}{'selfenroll_types'} = $selfenroll_types;
814: $courseinfo{$course}{'selfenroll_start'} = $selfenroll_start;
815: $courseinfo{$course}{'selfenroll_end'} = $selfenroll_end;
816: $courseinfo{$course}{'categories'} = $categories;
817:
818: my %coursehash = &Apache::lonnet::dump('environment',$cdom,$cnum);
819: my @classids;
820: my @crosslistings;
821: my ($seclist,$numsec) =
822: &identify_sections($coursehash{'internal.sectionnums'});
823: if (ref($usersections) eq 'HASH') {
824: if (ref($usersections->{$course}) eq 'ARRAY') {
825: $seclist = join(', ',@{$usersections->{$course}});
826: }
827: }
828: $courseinfo{$course}{'seclist'} = $seclist;
829: my ($xlist_items,$numxlist) =
830: &identify_sections($coursehash{'internal.crosslistings'});
831: my $showsyllabus = 1; # default is to include a syllabus link
832: if (defined($coursehash{'showsyllabus'})) {
833: $showsyllabus = $coursehash{'showsyllabus'};
834: }
835: $courseinfo{$course}{'showsyllabus'} = $showsyllabus;
836: if (((defined($env{'form.coursenum'}) && ($cnum eq $env{'form.coursenum'}))) ||
837: ($knownuser && ($details == 1))) {
838: $courseinfo{$course}{'counts'} = &count_students($cdom,$cnum,$numsec);
839: $courseinfo{$course}{'autoenrollment'} =
840: &autoenroll_info(\%coursehash,$now,$seclist,$xlist_items,
841: $instcode,\@owners,$cdom,$cnum);
842:
843: my $startaccess = '';
844: my $endaccess = '';
845: my $accessdates;
846: if ( defined($coursehash{'default_enrollment_start_date'}) ) {
847: $startaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_start_date'});
848: }
849: if ( defined($coursehash{'default_enrollment_end_date'}) ) {
850: $endaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_end_date'});
851: if ($coursehash{'default_enrollment_end_date'} == 0) {
852: $endaccess = &mt('No ending date');
853: }
854: }
855: if ($startaccess) {
856: $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$startaccess).'<br />';
857: }
858: if ($endaccess) {
859: $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$endaccess).'<br />';
860: }
861: if (($selfenroll_types ne '') &&
862: ($selfenroll_end > 0 && $selfenroll_end > $now)) {
863: my ($selfenroll_start_access,$selfenroll_end_access);
864: if (($coursehash{'default_enrollment_start_date'} ne
865: $coursehash{'internal.selfenroll_start_access'}) ||
866: ($coursehash{'default_enrollment_end_date'} ne
867: $coursehash{'internal.selfenroll_end_access'})) {
868: if ( defined($coursehash{'internal.selfenroll_start_access'}) ) {
869: $selfenroll_start_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_start_access'});
870: }
871: if ( defined($coursehash{'default_enrollment_end_date'}) ) {
872: $selfenroll_end_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_end_access'});
873: if ($coursehash{'internal.selfenroll_end_access'} == 0) {
874: $selfenroll_end_access = &mt('No ending date');
875: }
876: }
877: if ($selfenroll_start_access || $selfenroll_end_access) {
878: $accessdates .= '<br/><br /><i>'.&mt('Self-enrollers:').'</i><br />';
879: if ($selfenroll_start_access) {
880: $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$selfenroll_start_access).'<br />';
881: }
882: if ($selfenroll_end_access) {
883: $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$selfenroll_end_access).'<br />';
884: }
885: }
886: }
887: }
888: $courseinfo{$course}{'access'} = $accessdates;
889: }
890: if ($xlist_items eq '') {
891: $xlist_items = &mt('No');
892: }
893: $courseinfo{$course}{'xlist'} = $xlist_items;
894: }
895: return %courseinfo;
896: }
897:
898: sub count_students {
899: my ($cdom,$cnum,$numsec) = @_;
900: my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
901: my %student_count = (
902: Active => 0,
903: Future => 0,
904: Expired => 0,
905: );
906: my %idx;
907: $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
908: my %status_title = &Apache::lonlocal::texthash(
909: Expired => 'Previous access',
910: Active => 'Current access',
911: Future => 'Future access',
912: );
913:
914: while (my ($student,$data) = each(%$classlist)) {
915: $student_count{$data->[$idx{'status'}]} ++;
916: }
917:
918: my $countslist = &mt('[quant,_1,section:,sections:,No sections]',$numsec).'<br />';
919: foreach my $status ('Active','Future') {
920: $countslist .= '<span class="LC_nobreak">'.$status_title{$status}.': '.
921: $student_count{$status}.'</span><br />';
922: }
923: return $countslist;
924: }
925:
926: sub courseinfo_row {
927: my ($info,$knownuser,$details,$countref,$now,$course,$trails,$allitems) = @_;
928: my ($cdom,$cnum,$title,$ownerlast,$code,$owner,$seclist,$xlist_items,
929: $accessdates,$showsyllabus,$counts,$autoenrollment,$output,$categories);
930: if (ref($info) eq 'HASH') {
931: $cdom = $info->{'cdom'};
932: $cnum = $info->{'cnum'};
933: $title = $info->{'title'};
934: $ownerlast = $info->{'ownerlastnames'};
935: $code = $info->{'code'};
936: $owner = $info->{'owner'};
937: $seclist = $info->{'seclist'};
938: $xlist_items = $info->{'xlist'};
939: $accessdates = $info->{'access'};
940: $counts = $info->{'counts'};
941: $autoenrollment = $info->{'autoenrollment'};
942: $showsyllabus = $info->{'showsyllabus'};
943: $categories = $info->{'categories'};
944: } else {
945: $output = '<td colspan="8">'.&mt('No information available for [_1].',
946: $code).'</td>';
947: return $output;
948: }
949: $output .= '<td>'.$$countref.'</td>';
950: if ($env{'form.currcat_0'} eq 'instcode::0') {
951: $output .= '<td>'.$code.'</td>';
952: } else {
953: my ($categorylist,@cats);
954: if ($categories ne '') {
955: @cats = split('&',$categories);
956: }
957: if ((ref($trails) eq 'ARRAY') && (ref($allitems) eq 'HASH')) {
958: my @categories = map { $trails->[$allitems->{$_}]; } @cats;
959: $categorylist = join('<br />',@categories);
960: }
961: if ($categorylist eq '') {
962: $categorylist = ' ';
963: }
964: $output .= '<td>'.$categorylist.'</td>';
965: }
966: $output .= '<td>'.$seclist.'</td>'.
967: '<td>'.$xlist_items.'</td>'.
968: '<td>'.$title.' <font size="-2">';
969: if ($showsyllabus) {
970: $output .= '<a href="javascript:ToSyllabus('."'$cdom','$cnum'".')">'.&mt('Syllabus').'</a>';
971: } else {
972: $output .= ' ';
973: }
974: $output .= '</font></td>'.
975: '<td>'.$ownerlast.'</td>';
976: if ($knownuser) {
977: if ($details) {
978: $output .=
979: '<td>'.$accessdates.'</td>'.
980: '<td>'.$counts.'</td>'.
981: '<td>'.$autoenrollment.'</td>';
982: } else {
983: $output .= "<td><a href=\"javascript:setCourseId('$cnum')\">".&mt('Show more details').'</a></td>';
984: }
985: }
986: my $selfenroll;
987: if ($info->{'selfenroll_types'}) {
988: my $showstart = &Apache::lonlocal::locallocaltime($info->{'selfenroll_start'});
989: my $showend = &Apache::lonlocal::locallocaltime($info->{'selfenroll_end'});
990: if (($info->{'selfenroll_end'} > 0) && ($info->{'selfenroll_end'} > $now)) {
991: if (($info->{'selfenroll_start'} > 0) && ($info->{'selfenroll_start'} > $now)) {
992: $output .= '<td>'.&mt('Starts: [_1]','<span class="LC_cusr_emph">'.$showstart.'</span>').'<br />'.&mt('Ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>').'</td>';
993: } else {
994: $output .= '<td><a href="javascript:ToSelfenroll('."'$course'".')">'.&mt('Enroll in course').'</a></td>';
995: }
996: $selfenroll = 1;
997: }
998: }
999: if (!$selfenroll) {
1000: $output .= '<td> </td>';
1001: }
1002: $$countref ++;
1003: return $output;
1004: }
1005:
1006: sub identify_sections {
1007: my ($seclist) = @_;
1008: my @secnums;
1009: if ($seclist =~ /,/) {
1010: my @sections = split(/,/,$seclist);
1011: foreach my $sec (@sections) {
1012: $sec =~ s/:[^:]*$//;
1013: push(@secnums,$sec);
1014: }
1015: } else {
1016: if ($seclist =~ m/^([^:]+):/) {
1017: my $sec = $1;
1018: if (!grep(/^\Q$sec\E$/,@secnums)) {
1019: push(@secnums,$sec);
1020: }
1021: }
1022: }
1023: @secnums = sort {$a <=> $b} @secnums;
1024: $seclist = join(', ',@secnums);
1025: my $numsec = @secnums;
1026: return ($seclist,$numsec);
1027: }
1028:
1029: sub get_valid_classes {
1030: my ($seclist,$xlist_items,$crscode,$owners,$cdom,$cnum) = @_;
1031: my $response;
1032: my %validations;
1033: @{$validations{'sections'}} = ();
1034: @{$validations{'xlists'}} = ();
1035: my $totalitems = 0;
1036: if ($seclist) {
1037: foreach my $sec (split(/, /,$seclist)) {
1038: my $class = $crscode.$sec;
1039: if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
1040: $class) eq 'ok') {
1041: if (!grep(/^\Q$sec$\E/,@{$validations{'sections'}})) {
1042: push(@{$validations{'sections'}},$sec);
1043: $totalitems ++;
1044: }
1045: }
1046: }
1047: }
1048: if ($xlist_items) {
1049: foreach my $item (split(/, /,$xlist_items)) {
1050: if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
1051: $item) eq 'ok') {
1052: if (!grep(/^\Q$item$\E/,@{$validations{'xlists'}})) {
1053: push(@{$validations{'xlists'}},$item);
1054: $totalitems ++;
1055: }
1056: }
1057: }
1058: }
1059: if ($totalitems > 0) {
1060: if (@{$validations{'sections'}}) {
1061: $response = &mt('Sections:').' '.
1062: join(', ',@{$validations{'sections'}}).'<br />';
1063: }
1064: if (@{$validations{'xlists'}}) {
1065: $response .= &mt('Courses:').' '.
1066: join(', ',@{$validations{'xlists'}});
1067: }
1068: }
1069: return $response;
1070: }
1071:
1072: sub autoenroll_info {
1073: my ($coursehash,$now,$seclist,$xlist_items,$code,$owners,$cdom,$cnum) = @_;
1074: my $autoenrolldates = &mt('Not enabled');
1075: if (defined($coursehash->{'internal.autoadds'}) && $coursehash->{'internal.autoadds'} == 1) {
1076: my ($autostart,$autoend);
1077: if ( defined($coursehash->{'internal.autostart'}) ) {
1078: $autostart = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autostart'});
1079: }
1080: if ( defined($coursehash->{'internal.autoend'}) ) {
1081: $autoend = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autoend'});
1082: }
1083: if ($coursehash->{'internal.autostart'} > $now) {
1084: if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
1085: $autoenrolldates = &mt('Not enabled');
1086: } else {
1087: my $valid_classes =
1088: &get_valid_classes($seclist,$xlist_items,$code,
1089: $owners,$cdom,$cnum);
1090: if ($valid_classes ne '') {
1091: $autoenrolldates = &mt('Not enabled').'<br />'
1092: .&mt('Starts: [_1]',$autostart)
1093: .'<br />'.$valid_classes;
1094: }
1095: }
1096: } else {
1097: if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
1098: $autoenrolldates = &mt('Not enabled').'<br />'
1099: .&mt('Ended: [_1]',$autoend);
1100: } else {
1101: my $valid_classes = &get_valid_classes($seclist,$xlist_items,
1102: $code,$owners,$cdom,$cnum);
1103: if ($valid_classes ne '') {
1104: $autoenrolldates = &mt('Currently enabled').'<br />'.
1105: $valid_classes;
1106: }
1107: }
1108: }
1109: }
1110: return $autoenrolldates;
1111: }
1112:
1113: sub user_is_known {
1114: my $known = 0;
1115: if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public'
1116: && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
1117: $known = 1;
1118: }
1119: return $known;
1120: }
1121:
1122: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>