1: # The LearningOnline Network with CAPA
2: # Handler for displaying the course catalog interface
3: #
4: # $Id: coursecatalog.pm,v 1.88.2.1 2016/10/23 21:21:56 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: use LONCAPA::lonauthcgi;
42:
43: sub handler {
44: my ($r) = @_;
45: &Apache::loncommon::content_type($r,'text/html');
46: $r->send_http_header;
47: if ($r->header_only) {
48: return OK;
49: }
50: my $handle = &Apache::lonnet::check_for_valid_session($r);
51: my $lonidsdir=$r->dir_config('lonIDsDir');
52: if ($handle ne '') {
53: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
54: }
55: &Apache::lonacc::get_posted_cgi($r);
56: &Apache::lonlocal::get_language_handle($r);
57: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
58: ['sortby','showdom']);
59:
60: my $codedom = &Apache::lonnet::default_login_domain();
61:
62: if (($env{'user.domain'} ne '') && ($env{'user.domain'} ne 'public')) {
63: $codedom = $env{'user.domain'};
64: if ($env{'request.role.domain'} ne '') {
65: $codedom = $env{'request.role.domain'};
66: }
67: }
68: my $formname = 'coursecatalog';
69: if ($env{'form.showdom'} ne '') {
70: $env{'form.showdom'} = &LONCAPA::clean_domain($env{'form.showdom'});
71: if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
72: $codedom = $env{'form.showdom'};
73: } else {
74: $env{'form.showdom'} = '';
75: }
76: }
77: my $domdesc = &Apache::lonnet::domain($codedom,'description');
78: &Apache::lonhtmlcommon::clear_breadcrumbs();
79:
80: my %domconfig =
81: &Apache::lonnet::get_dom('configuration',['coursecategories'],$codedom);
82: my $knownuser = &user_is_known();
83: my $canviewall = &canview_all($knownuser,$codedom);
84:
85: my ($cathash,$cattype);
86: if (ref($domconfig{'coursecategories'}) eq 'HASH') {
87: $cathash = $domconfig{'coursecategories'}{'cats'};
88: if ($knownuser || $canviewall) {
89: $cattype = $domconfig{'coursecategories'}{'auth'};
90: } else {
91: $cattype = $domconfig{'coursecategories'}{'unauth'};
92: }
93: if ($cattype eq '') {
94: $cattype = 'std';
95: }
96: } else {
97: $cathash = {};
98: $cattype = 'std';
99: }
100: if ($cattype eq 'none') {
101: $r->print(&Apache::loncommon::start_page('Course/Community Catalog'));
102: &Apache::lonhtmlcommon::add_breadcrumb
103: ({href=>"/adm/coursecatalog",
104: text=>"Course/Community Catalog"});
105: $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
106: if ($knownuser || $canviewall) {
107: $r->print('<div>'.&mt('No catalog of LON-CAPA courses/communities is provided for: [_1]',$domdesc).'</div>');
108: } else {
109: if ($domconfig{'coursecategories'}{'auth'} eq 'none') {
110: $r->print('<div>'.&mt('No catalog of LON-CAPA courses/communities is provided for: [_1]',$domdesc).'</div>');
111: } else {
112: $r->print('<div>'.&mt('The catalog of LON-CAPA courses/communities provided for: "[_1]" is only available to users who are logged in.',$domdesc).'</div>');
113: }
114: }
115: $r->print(&Apache::loncommon::end_page());
116: return OK;
117: }
118:
119: my $cnum;
120: if ($cattype eq 'codesrch') {
121: my ($uniquecode,$codemsg,$brtext);
122: if ($env{'form.uniquecode'}) {
123: $uniquecode = $env{'form.uniquecode'};
124: $uniquecode =~ s/^\s+|\s+$//g;
125: }
126: my $js = '<script type="text/javascript">'."\n".
127: '// <![CDATA['."\n".
128: &courselink_javascript()."\n".
129: '// ]]>'."\n".
130: '</script>'."\n";
131: $r->print(&Apache::loncommon::start_page('Search for a Course/Community',$js));
132: if ($uniquecode =~ /^\w{6}$/) {
133: &Apache::lonhtmlcommon::add_breadcrumb
134: ({href=>"/adm/coursecatalog",
135: text=>"Course/Community Catalog"});
136: $brtext = 'Search Result';
137: } else {
138: $brtext = 'Course/Community Catalog';
139: }
140: &Apache::lonhtmlcommon::add_breadcrumb
141: ({href=>"/adm/coursecatalog",
142: text=>"$brtext"});
143: $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
144: $r->print(&coursesearch($codedom,$domdesc,$uniquecode));
145: if ($uniquecode =~ /^\w{6}$/) {
146: $r->print('<hr />');
147: my $confname = $codedom.'-domainconfig';
148: my %codes = &Apache::lonnet::get('uniquecodes',[$uniquecode],$codedom,$confname);
149: if ($codes{$uniquecode}) {
150: $cnum = $codes{$uniquecode};
151: my %courses = &Apache::lonnet::courseiddump($codedom,'.',1,'.','.',
152: $cnum,undef,undef,'.',1);
153: if (keys(%courses)) {
154: $env{'form.coursenum'} = $cnum;
155: my %courseinfo = &build_courseinfo_hash(\%courses,$knownuser,$codedom,$canviewall);
156: undef($env{'form.coursenum'});
157: if (ref($courseinfo{$codedom.'_'.$cnum}) eq 'HASH') {
158: $r->print(&Apache::lonhtmlcommon::start_pick_box());
159: my @cols = ('title','ownerlastnames','seclist','access');
160: my %lt = &Apache::lonlocal::texthash(
161: title => 'Title',
162: ownerlastnames => 'Owner & Co-owner(s)',
163: seclist => 'Sections',
164: access => 'Default Access Dates for Students',
165: );
166: my @shown;
167: foreach my $item (@cols) {
168: if ($courseinfo{$codedom.'_'.$cnum}{$item}) {
169: push(@shown,$item);
170: }
171: }
172: my $num = 0;
173: foreach my $item (@shown) {
174: $num ++;
175: $r->print(&Apache::lonhtmlcommon::row_title($lt{$item}).
176: $courseinfo{$codedom.'_'.$cnum}{$item});
177: if ($item eq 'title') {
178: if ($courseinfo{$codedom.'_'.$cnum}{'showsyllabus'}) {
179: $r->print(' <font size="-2">'.
180: '<a href="javascript:ToSyllabus('."'$codedom','$cnum'".')">'.
181: &mt('Syllabus').'</a></font>');
182: }
183: }
184: my $arg = ( $num == scalar(@shown) ? 1 : '' );
185: $r->print(&Apache::lonhtmlcommon::row_closure($arg));
186: }
187: $r->print(&Apache::lonhtmlcommon::end_pick_box());
188: my $selfenroll = &selfenroll_status($courseinfo{$codedom.'_'.$cnum},$codedom.'_'.$cnum);
189: if ($selfenroll) {
190: $r->print('<br />'.$selfenroll);
191: }
192: $r->print('<form name="linklaunch" method="post" action="">'."\n".
193: '<input type="hidden" name="backto" value="coursecatalog" />'."\n".
194: '<input type="hidden" name="courseid" value="" />'."\n".
195: '</form>'."\n");
196:
197: } else {
198: $codemsg = &mt('Code matched, but course ID to which this mapped is invalid.');
199: }
200: } else {
201: $codemsg = &mt('Code matched, but course ID to which this mapped is invalid.');
202: }
203: } else {
204: $codemsg = &mt('No match');
205: }
206: }
207: $r->print('<br />'.&Apache::loncommon::end_page());
208: return OK;
209: } else {
210: if ($env{'form.coursenum'}) {
211: $cnum = $env{'form.coursenum'};
212: }
213: }
214:
215: if ($env{'form.catalog_maxdepth'} ne '') {
216: $env{'form.catalog_maxdepth'} =~ s{\D}{}g;
217: }
218:
219: my (@cats,@trails,%allitems,%idx,@jsarray,%subcathash,$subcats);
220: if ($env{'form.withsubcats'}) {
221: $subcats = \%subcathash;
222: }
223: &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems,
224: \%idx,\@jsarray,$subcats);
225: my ($numtitles,@codetitles);
226: if (($env{'form.coursenum'} ne '') && ($knownuser)) {
227: &course_details($r,$codedom,$formname,$domdesc,\@trails,\%allitems,\@codetitles);
228: } else {
229: my ($catlinks,$has_subcats,$selitem) = &category_breadcrumbs($codedom,@cats);
230: my $wasacctext = &get_wasactive_text();
231: my $catjs = <<"ENDSCRIPT";
232:
233: function setCatDepth(depth) {
234: document.coursecats.catalog_maxdepth.value = depth;
235: if (depth == '') {
236: document.coursecats.currcat_0.value = '';
237: }
238: document.coursecats.submit();
239: return;
240: }
241:
242: function changeSort(caller) {
243: document.$formname.sortby.value = caller;
244: document.$formname.submit();
245: }
246:
247: function setCourseId(caller) {
248: document.$formname.coursenum.value = caller;
249: document.$formname.submit();
250: }
251:
252: ENDSCRIPT
253: $catjs .= &courselink_javascript();
254: if (&user_is_dc($codedom) || $canviewall) {
255: $catjs .= <<ENDTOGGJS
256:
257: function toggleStatuses() {
258: if (document.$formname.showdetails.checked) {
259: document.getElementById('statuschoice').style.display='block';
260: document.getElementById('statuscell').style.borderLeft='1px solid';
261: } else {
262: document.getElementById('statuschoice').style.display='none';
263: document.getElementById('statuscell').style.borderLeft='0px';
264: }
265: return;
266: }
267:
268: function toggleWasActive() {
269: if (document.getElementById('counts_Previous')) {
270: if (document.getElementById('counts_Previous').checked) {
271: document.getElementById('choosewasactive').style.display='block';
272: document.getElementById('choosewasacctext').innerHTML = '$wasacctext';
273: } else {
274: document.getElementById('choosewasactive').style.display='none';
275: document.getElementById('choosewasacctext').innerHTML = '';
276: }
277: }
278: return;
279: }
280:
281: ENDTOGGJS
282: }
283: if ($env{'form.currcat_0'} eq 'instcode::0') {
284: $numtitles = &instcode_course_selector($r,$codedom,$formname,$domdesc,
285: $catlinks,$catjs,\@codetitles,$cattype,$canviewall);
286: if ($env{'form.state'} eq 'listing') {
287: $r->print(&print_course_listing($codedom,$numtitles,undef,undef,undef,
288: \@codetitles,$canviewall));
289: }
290: } else {
291: my (%add_entries);
292: my ($currdepth,$deeper) = &get_depth_values();
293: if ($selitem) {
294: my $alert = &mt('Choose a subcategory to display');
295: if (!$deeper) {
296: $alert = &mt('Choose a category to display');
297: }
298: &js_escape(\$alert);
299: $catjs .= <<ENDJS;
300: function check_selected() {
301: if (document.coursecats.$selitem.options[document.coursecats.$selitem.selectedIndex].value == "") {
302: alert('$alert');
303: return false;
304: }
305: }
306: ENDJS
307: }
308: $catjs = '<script type="text/javascript">'."\n".$catjs."\n".'</script>';
309: &cat_header($r,$codedom,$catjs,\%add_entries,$catlinks,undef,$cattype);
310: if ($env{'form.currcat_0'} ne '') {
311: $r->print('<form name="'.$formname.
312: '" method="post" action="/adm/coursecatalog">'.
313: &additional_filters($codedom,$has_subcats,$canviewall)."\n");
314: $r->print('<input type="hidden" name="catalog_maxdepth" value="'.
315: $deeper.'" />'."\n");
316: for (my $i=0; $i<$deeper; $i++) {
317: $r->print('<input type="hidden" name="currcat_'.$i.'" value="'.$env{'form.currcat_'.$i}.'" />'."\n");
318: }
319: my $display_button;
320: if ($env{'form.currcat_0'} eq 'communities::0') {
321: $display_button = &mt('Display communities');
322: } else {
323: $display_button = &mt('Display courses');
324: }
325: $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
326: '<input type="hidden" name="sortby" value="" />'."\n".
327: '<input type="hidden" name="state" value="listing" />'."\n".
328: '<input type="hidden" name="showdom" value="'.
329: $env{'form.showdom'}.'" />'.
330: '<input type="submit" name="catalogfilter" value="'.
331: $display_button.'" /></form><br /><br />');
332: }
333: if ($env{'form.state'} eq 'listing') {
334: $r->print(&print_course_listing($codedom,undef,\@trails,\%allitems,$subcats,\@codetitles,
335: $canviewall));
336: }
337: }
338: }
339: $r->print('<br />'.&Apache::loncommon::end_page());
340: return OK;
341: }
342:
343: sub course_details {
344: my ($r,$codedom,$formname,$domdesc,$trails,$allitems,$codetitles) = @_;
345: my $output;
346: my %add_entries = (topmargin => "0",
347: marginheight => "0",);
348: my $js = '<script type="text/javascript">'."\n".
349: &courselink_javascript().'</script>'."\n";
350: my $start_page =
351: &Apache::loncommon::start_page('Course/Community Catalog',$js,
352: {'add_entries' => \%add_entries, });
353: $r->print($start_page);
354: if ($env{'form.numtitles'} > 0) {
355: &Apache::lonhtmlcommon::add_breadcrumb
356: ({href=>"/adm/coursecatalog",
357: text=>"Course/Community Catalog"});
358: }
359: my $brtextone = 'Course listing';
360: my $brtexttwo = 'Course details';
361: if ($env{'form.currcat_0'} eq 'communities::0') {
362: $brtextone = 'Community listing';
363: $brtexttwo = 'Community details';
364: }
365: &Apache::lonhtmlcommon::add_breadcrumb
366: ({href=>"javascript:document.$formname.submit()",
367: text=>$brtextone},
368: {text=>$brtexttwo});
369: $r->print(
370: &Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog').
371: '<h2>'.
372: (($env{'form.currcat_0'} eq 'communities::0') ?
373: &mt('Detailed community information:') :
374: &mt('Detailed course information:')).
375: '</h2>'.
376: &print_course_listing($codedom,undef,$trails,$allitems,undef,$codetitles).
377: '<br />'.
378: '<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
379: &Apache::lonhtmlcommon::actionbox([
380: '<a href = "javascript:document.coursecatalog.submit()">'.
381: (($env{'form.currcat_0'} eq 'communities::0') ?
382: &mt('Back to community listing') : &mt('Back to course listing')).
383: '</a>'
384: ]).
385: &Apache::lonhtmlcommon::echo_form_input(['coursenum','catalogfilter',
386: 'showdetails','courseid']).
387: '</form>');
388: return;
389: }
390:
391: sub coursesearch {
392: my ($codedom,$domdesc,$uniquecode) = @_;
393: my %lt = &Apache::lonlocal::texthash (
394: crlk => 'Course look-up',
395: code => 'Code',
396: ifyo => 'Enter the course code (six letters and numbers)',
397: srch => 'Find course',
398: );
399: return <<"END";
400: <h3>$lt{'crlk'} ($domdesc)</h3>
401: $lt{'ifyo'}
402: <form name="searchbycode" method="post" action="">
403: <span class="LC_nobreak">
404: <input type="text" value="$uniquecode" size="6" name="uniquecode" />
405: <br />
406: <input type="submit" value="$lt{'srch'}" name="srch" /></span>
407: </form>
408: END
409: }
410:
411: sub courselink_javascript {
412: return <<"END";
413:
414: function ToSyllabus(cdom,cnum) {
415: if (cdom == '' || cdom == null) {
416: return;
417: }
418: if (cnum == '' || cnum == null) {
419: return;
420: }
421: document.linklaunch.action = "/public/"+cdom+"/"+cnum+"/syllabus";
422: document.linklaunch.submit();
423: }
424:
425: function ToSelfenroll(courseid) {
426: if (courseid == '') {
427: return;
428: }
429: document.linklaunch.action = "/adm/selfenroll";
430: document.linklaunch.courseid.value = courseid;
431: document.linklaunch.submit();
432: }
433:
434: END
435: }
436:
437: sub instcode_course_selector {
438: my ($r,$codedom,$formname,$domdesc,$catlinks,$catjs,$codetitles,$cattype,$canviewall) = @_;
439: my %coursecodes = ();
440: my %codes = ();
441: my %cat_titles = ();
442: my %cat_order = ();
443: my %cat_items;
444: my $caller = 'global';
445: my $format_reply;
446: my %add_entries = (topmargin => "0",
447: marginheight => "0",);
448: my ($jscript,$totcodes,$numtitles,$lasttitle) =
449: &Apache::courseclassifier::instcode_selectors_data($codedom,$formname,
450: \%cat_items,$codetitles,\%cat_titles,\%cat_order);
451: my $js = '<script type="text/javascript">'."\n".
452: '// <![CDATA['."\n".
453: "$jscript\n$catjs\n".
454: '// ]]>'."\n".
455: '</script>';
456: if ($totcodes) {
457: if (($env{'form.state'} eq 'listing') && ($numtitles > 0)) {
458: $add_entries{'onload'} = 'setElements();';
459: }
460: if (&user_is_dc($codedom) || $canviewall) {
461: $add_entries{'onload'} .= ' toggleStatuses();toggleWasActive();'
462: }
463: &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles,$cattype);
464: my $cat_maxdepth = $env{'form.catalog_maxdepth'};
465: $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
466: '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'."\n".
467: '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'."\n".
468: '<input type="hidden" name="currcat_0" value="instcode::0" />'.
469: &additional_filters($codedom,undef,$canviewall));
470: if ($numtitles > 0) {
471: $r->print('<b>'.&mt('Choose which course(s) to list.').'</b><br />'.
472: &Apache::courseclassifier::build_instcode_selectors($numtitles,
473: $lasttitle,\%cat_items,$codetitles,\%cat_titles,\%cat_order));
474: }
475: $r->print('<input type="hidden" name="coursenum" value="" />'."\n".
476: '<input type="hidden" name="sortby" value="" />'."\n".
477: '<input type="hidden" name="state" value="listing" />'."\n".
478: '<input type="submit" name="catalogfilter" value="'.
479: &mt('Display courses').'" />'.
480: '<input type="hidden" name="numtitles" value="'.$numtitles.
481: '" /></form><br /><br />');
482: } else {
483: $js = '<script type="text/javascript">'."\n".
484: '// <![CDATA['."\n".
485: "$catjs\n".
486: '// ]]>'."\n".
487: '</script>';
488: &cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles,$cattype);
489: my $cat_maxdepth = $env{'form.catalog_maxdepth'};
490: $r->print('<form name="'.$formname.'" method="post" action="/adm/coursecatalog">'.
491: '<input type="hidden" name="catalog_maxdepth" value="'.$cat_maxdepth.'" />'.
492: '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'.
493: '<input type="hidden" name="currcat_0" value="instcode::0" />');
494: $r->print('<br />'.&mt('No official courses to display for [_1].',$domdesc).'</form>');
495: }
496: return $numtitles;
497: }
498:
499: sub cat_header {
500: my ($r,$codedom,$js,$add_entries,$catlinks,$numtitles,$cattype) = @_;
501: my $start_page =
502: &Apache::loncommon::start_page('Course/Community Catalog',$js,
503: { 'add_entries' => $add_entries, });
504: $r->print($start_page);
505: my $brtext = 'Course listing';
506: if ($env{'form.currcat_0'} eq 'communities::0') {
507: $brtext = 'Community listing';
508: }
509: if ($env{'form.state'} eq 'listing') {
510: if ($numtitles > 0) {
511: &Apache::lonhtmlcommon::add_breadcrumb
512: ({href=>"/adm/coursecatalog",
513: text=>"Course/Community Catalog"},
514: {text=>$brtext});
515: } else {
516: &Apache::lonhtmlcommon::add_breadcrumb
517: ({text=>$brtext});
518: }
519: } else {
520: &Apache::lonhtmlcommon::add_breadcrumb
521: ({href=>"/adm/coursecatalog",
522: text=>"Course/Community Catalog"});
523: }
524: $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course/Community Catalog'));
525: if ($cattype eq 'std') {
526: my $onchange = 'this.form.submit()';
527: $r->print('<form name="coursecatdom" method="post" action="/adm/coursecatalog">'.
528: '<table border="0"><tr><td><b>'.&mt('Domain:').'</b></td><td>'.
529: &Apache::loncommon::select_dom_form($codedom,'showdom','',1,$onchange));
530: if (!$onchange) {
531: $r->print(' <input type="submit" name="godom" value="'.&mt('Change').'" />');
532: }
533: $r->print('</td></tr></table></form>');
534: }
535: $r->print('<form name="coursecats" method="post" action="/adm/coursecatalog"'.
536: ' onsubmit="return check_selected();">'.
537: '<table border="0"><tr>'.$catlinks.'</tr></table></form>');
538: return;
539: }
540:
541: sub category_breadcrumbs {
542: my ($dom,@cats) = @_;
543: my $crumbsymbol = ' ▶ ';
544: my ($currdepth,$deeper) = &get_depth_values();
545: my $currcat_str =
546: '<input type="hidden" name="catalog_maxdepth" value="'.$deeper.'" />'.
547: '<input type="hidden" name="showdom" value="'.$dom.'" />';
548: my $catlinks = '<td valign="top"><b>'.&mt('Catalog:').'</b></td><td><table><tr><td>';
549: my $has_subcats;
550: my $selitem;
551: if (ref($cats[0]) eq 'ARRAY') {
552: if (@{$cats[0]} == 0) {
553: $catlinks .= &mt('No categories defined in this domain');
554: } elsif (@{$cats[0]} == 1) {
555: if ($cats[0][0] eq 'instcode') {
556: $catlinks .= &mt('Official courses (with institutional codes)');
557: $env{'form.currcat_0'} = 'instcode::0';
558: } elsif ($cats[0][0] eq 'communities') {
559: $catlinks .= &mt('Communities');
560: $env{'form.currcat_0'} = 'communities::0';
561: } else {
562: my $name = $cats[0][0];
563: my $item = &escape($name).'::0';
564: $catlinks .= $name;
565: $env{'form.currcat_0'} = $item;
566: }
567: $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
568: } else {
569: $catlinks .= &main_category_selector(@cats);
570: if (($env{'form.currcat_0'} ne '') &&
571: ($env{'form.currcat_0'} ne 'instcode::0')) {
572: $catlinks .= $crumbsymbol;
573: }
574: }
575: } else {
576: $catlinks .= &mt('Official courses (with institutional codes)');
577: $env{'form.currcat_0'} = 'instcode::0';
578: $currcat_str .= '<input type="hidden" name="currcat_0" value="'.$env{'form.currcat_0'}.'" />';
579: }
580: if ($deeper) {
581: for (my $i=1; $i<=$deeper; $i++) {
582: my $shallower = $i-1;
583: next if ($shallower == 0);
584: my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
585: if ($cat ne '') {
586: $catlinks .= '<td valign="top">'.
587: '<select name="currcat_'.$shallower.'" onchange="'.
588: 'setCatDepth('."'$shallower'".');this.form.submit();">';
589: if (ref($cats[$shallower]{$container}) eq 'ARRAY') {
590: $catlinks .= '<option value="">'.&mt('De-select').'</option>';
591: for (my $j=0; $j<@{$cats[$shallower]{$container}}; $j++) {
592: my $name = $cats[$shallower]{$container}[$j];
593: my $item = &escape($name).':'.&escape($container).':'.$shallower;
594: my $selected = '';
595: if ($item eq $env{'form.currcat_'.$shallower}) {
596: $selected = ' selected="selected"';
597: }
598: $catlinks .=
599: '<option value="'.$item.'"'.$selected.'>'.$name.'</option>';
600: }
601: }
602: $catlinks .= '</select>';
603: }
604: unless ($i == $deeper) {
605: $catlinks .= $crumbsymbol;
606: }
607: }
608: my ($cat,$container,$depth);
609: if ($env{'form.currcat_'.$currdepth} eq '') {
610: my $shallower = $currdepth - 1;
611: ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$shallower});
612: } else {
613: ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$env{'form.currcat_'.$currdepth});
614: }
615: my $deeperlevel = $depth +1;
616: if (ref($cats[$deeperlevel]{$cat}) eq 'ARRAY') {
617: $has_subcats = 1;
618: my $buttontext = &mt('Show subcategories');
619: my $selitem = 'currcat_'.$deeperlevel;
620: $catlinks .= ' <select name="'.$selitem.'" onchange="this.form.submit()">';
621: if (@{$cats[$deeperlevel]{$cat}}) {
622: $catlinks .= '<option value="" selected="selected">'.
623: &mt('Subcategory ...').'</option>';
624: }
625: for (my $k=0; $k<@{$cats[$deeperlevel]{$cat}}; $k++) {
626: my $name = $cats[$deeperlevel]{$cat}[$k];
627: my $item = &escape($name).':'.&escape($cat).':'.$deeperlevel;
628: $catlinks .= '<option value="'.$item.'">'.$name.'</option>'."\n";
629: }
630: $catlinks .= '</select>'."\n";
631: } elsif ($cat ne 'instcode') {
632: $catlinks .= ' '.&mt('(No subcategories)');
633: }
634: } else {
635: $selitem = 'currcat_0';
636: }
637: $catlinks .= $currcat_str.'</td></tr></table></td>';
638: return ($catlinks,$has_subcats,$selitem);
639: }
640:
641: sub main_category_selector {
642: my (@cats) = @_;
643: my $maincatlinks = '<select name="currcat_0" onchange="setCatDepth('."'0'".');this.form.submit();">'."\n";
644: if (ref($cats[0]) eq 'ARRAY') {
645: if (@{$cats[0]} > 1) {
646: my $selected = '';
647: if ($env{'form.currcat_0'} eq '') {
648: $selected = ' selected="selected"';
649: }
650: $maincatlinks .=
651: '<option value=""'.$selected.'>'.&mt('Select').'</option>'."\n";
652: }
653: for (my $i=0; $i<@{$cats[0]}; $i++) {
654: my $name = $cats[0][$i];
655: my $item = &escape($name).'::0';
656: my $selected;
657: if ($env{'form.currcat_0'} eq $item) {
658: $selected = ' selected="selected"';
659: }
660: $maincatlinks .= '<option value="'.$item.'"'.$selected.'>';
661: if ($name eq 'instcode') {
662: $maincatlinks .= &mt('Official courses (with institutional codes)');
663: } elsif ($name eq 'communities') {
664: $maincatlinks .= &mt('Communities');
665: } else {
666: $maincatlinks .= $name;
667: }
668: $maincatlinks .= '</option>'."\n";
669: }
670: $maincatlinks .= '</select>'."\n";
671: }
672: return $maincatlinks;
673: }
674:
675: sub get_depth_values {
676: my $currdepth = 0;
677: my $deeper = 0;
678: if ($env{'form.catalog_maxdepth'} ne '') {
679: $currdepth = $env{'form.catalog_maxdepth'};
680: if ($env{'form.currcat_'.$currdepth} eq '') {
681: $deeper = $currdepth;
682: } else {
683: $deeper = $currdepth + 1;
684: }
685: }
686: return ($currdepth,$deeper);
687: }
688:
689: sub additional_filters {
690: my ($codedom,$has_subcats,$canviewall) = @_;
691: my $is_dc = &user_is_dc($codedom);
692: my $output = '<div class="LC_left_float">';
693: if ($is_dc || $canviewall) {
694: $output .= '<fieldset><legend>'.&mt('Options').'</legend>';
695: }
696: $output .= '<table><tr><td valign="top">';
697: if (($env{'form.currcat_0'} ne 'instcode::0') &&
698: ($env{'form.currcat_0'} ne '') && ($has_subcats)) {
699: my $include_subcat_status;
700: if ($env{'form.withsubcats'}) {
701: $include_subcat_status = 'checked="checked" ';
702: }
703: my $counter = $env{'form.catalog_maxdepth'};
704: if ($counter > 0) {
705: if ($env{'form.state'} eq 'listing') {
706: $counter --;
707: } elsif ($env{'form.currcat_'.$counter} eq '') {
708: $counter --;
709: }
710: }
711: my ($catname) = split(/:/,$env{'form.currcat_'.$counter});
712: if ($catname ne '') {
713: $output .= '<span class="LC_nobreak"><label>'.
714: '<input type="checkbox" name="withsubcats" value="1" '.
715: $include_subcat_status.'/>'.
716: &mt('Include subcategories within "[_1]"',
717: &unescape($catname)).'</label></span><br />';
718: }
719: }
720: my $show_selfenroll_status;
721: if ($env{'form.showselfenroll'}) {
722: $show_selfenroll_status = 'checked="checked" ';
723: }
724: my $selfenroll_text;
725: if ($env{'form.currcat_0'} eq 'communities::0') {
726: $selfenroll_text = &mt('Only show communities which currently allow self-enrollment (or will allow it in the future)');
727: } else {
728: $selfenroll_text = &mt('Only show courses which currently allow self-enrollment (or will allow it in the future)');
729: }
730: $output .= '<span class="LC_nobreak">'.
731: '<label><input type="checkbox" name="showselfenroll" value="1" '.
732: $show_selfenroll_status.'/>'.$selfenroll_text.
733: '</label></span><br />';
734: if ($is_dc || $canviewall) {
735: my ($titlesref,$orderref) = &get_statustitles('filters');
736: my $showdetails_status;
737: if ($env{'form.showdetails'}) {
738: $showdetails_status = 'checked="checked" ';
739: }
740: my $showhidden_status;
741: if ($env{'form.showhidden'}) {
742: $showhidden_status = 'checked="checked" ';
743: }
744: my @currstatuses = &Apache::loncommon::get_env_multiple('form.showcounts');
745: my ($details_text,$hidden_text,$statusdisplay,$cellborder);
746: my $wasactivedisplay = 'none';
747: if ($env{'form.showdetails'}) {
748: $statusdisplay = 'block';
749: $cellborder = 'border-left: 1px solid;';
750: if (grep(/^Previous$/,@currstatuses)) {
751: $wasactivedisplay = 'block';
752: }
753: } else {
754: $statusdisplay = 'none';
755: $cellborder = 'border-left: 0px';
756: }
757: if ($env{'form.currcat_0'} eq 'communities::0') {
758: $details_text = &mt('Show full details for each community (domain staff only)');
759: $hidden_text = &mt('Include communities set to be hidden from catalog (domain staff only)');
760: } else {
761: $details_text = &mt('Show full details for each course (domain staff only)');
762: $hidden_text = &mt('Include courses set to be hidden from catalog (domain staff only)');
763: }
764: if ($is_dc) {
765: $output .= '<span class="LC_nobreak">'.
766: '<label><input type="checkbox" name="showhidden" value="1" '.
767: $showhidden_status.'/>'.$hidden_text.
768: '</label></span><br />'."\n";
769: }
770: $output .= '<span class="LC_nobreak">'.
771: '<label><input type="checkbox" name="showdetails" value="1" '.
772: $showdetails_status.'onclick="toggleStatuses();" />'.
773: $details_text.'</label></span></td>'."\n".
774: '<td id="statuscell" valign="top" style="'.$cellborder.'">'.
775: '<div id="statuschoice" style="display:'.$statusdisplay.'">';
776: if (ref($orderref) eq 'ARRAY') {
777: if (@{$orderref} > 0) {
778: foreach my $type (@{$orderref}) {
779: my $checked;
780: if (grep(/^\Q$type\E$/,@currstatuses)) {
781: $checked = ' checked="checked"';
782: }
783: my $title;
784: if (ref($titlesref) eq 'HASH') {
785: $title = $titlesref->{$type};
786: }
787: unless ($title) {
788: $title = &mt($type);
789: }
790: my $onclick;
791: if ($type eq 'Previous') {
792: $onclick = ' onclick="toggleWasActive();"';
793: }
794: $output .= '<span class="LC_nobreak">'.
795: '<label><input type="checkbox" id="counts_'.$type.'"'.
796: ' name="showcounts" value="'.$type.'"'.$checked.$onclick.
797: ' />'.$title.'</label></span>';
798: if ($type eq 'Previous') {
799: my %milestonetext = &Apache::lonlocal::texthash (
800: accessend => 'immediately prior to default end access date',
801: enrollend => 'immediately prior to end date for auto-enrollment',
802: date => 'immediately prior to specific date:',
803: );
804: my @statuses = &Apache::loncommon::get_env_multiple('form.showcounts');
805: $output .= '<span id="choosewasacctext" class="LC_nobreak">';
806: if ($checked) {
807: $output .= &get_wasactive_text();
808: }
809: $output .= '</span>'.
810: '<div id="choosewasactive" style="display:'.$wasactivedisplay.'">'.
811: '<table>';
812: my @milestones = ('accessend');
813: if (&Apache::lonnet::auto_run(undef,$codedom)) {
814: push(@milestones,'enrollend');
815: }
816: push(@milestones,'date');
817: foreach my $item (@milestones) {
818: my $checked;
819: if ($env{'form.state'} eq 'listing') {
820: if ($env{'form.wasactive'} eq $item) {
821: $checked = ' checked="checked"';
822: }
823: } elsif ($item eq 'accessend') {
824: $checked = ' checked="checked"';
825: }
826: $output .=
827: '<tr><td width="10"> </td><td>'.
828: '<span class="LC_nobreak"><label>'.
829: '<input type="radio" value="'.$item.'" name="wasactive"'.$checked.' />'.
830: $milestonetext{$item}.'</label></span>';
831: if ($item eq 'date') {
832: my $wasactiveon;
833: if (grep(/^Previous$/,@currstatuses)) {
834: $wasactiveon =
835: &Apache::lonhtmlcommon::get_date_from_form('wasactiveon');
836: } else {
837: $wasactiveon = 'now';
838: }
839: $output .= ' '.
840: &Apache::lonhtmlcommon::date_setter('coursecatalog',
841: 'wasactiveon',
842: $wasactiveon,
843: '','','',1,'',
844: '','',1);
845: }
846: $output .= '</td></tr>';
847: }
848: $output .= '</table></div>';
849: }
850: $output .= '<br />';
851: }
852: }
853: }
854: $output .= '</div></td>';
855: } else {
856: $output .= '</td>';
857: }
858: $output .= '</tr></table></fieldset></div>'.
859: '<div style="clear:both;margin:0;"></div>';
860: return $output;
861: }
862:
863: sub user_is_dc {
864: my ($codedom) = @_;
865: if (exists($env{'user.role.dc./'.$codedom.'/'})) {
866: my $livedc = 1;
867: my $now = time;
868: my ($start,$end)=split(/\./,$env{'user.role.dc./'.$codedom.'/'});
869: if ($start && $start>$now) { $livedc = 0; }
870: if ($end && $end <$now) { $livedc = 0; }
871: return $livedc;
872: }
873: return;
874: }
875:
876: sub canview_all {
877: my ($knownuser,$codedom) = @_;
878: my $canviewall = 0;
879: my $page = 'coursecatalog';
880: if (&LONCAPA::lonauthcgi::can_view($page)) {
881: $canviewall = 1;
882: } elsif (&LONCAPA::lonauthcgi::check_ipbased_access($page)) {
883: $canviewall= 1;
884: } elsif (($knownuser) && ($codedom ne '')) {
885: if (&Apache::lonnet::allowed('dcd',$codedom)) {
886: $canviewall = 1;
887: }
888: }
889: return $canviewall;
890: }
891:
892: sub get_statustitles {
893: my ($caller) = @_;
894: my @status_order = ('Active','Future','Previous');
895: my %status_title;
896: if ($caller eq 'filters') {
897: %status_title = &Apache::lonlocal::texthash(
898: Previous => 'Show count for past access',
899: Active => 'Show count for current student access',
900: Future => 'Show count for future student access',
901: );
902: if ($env{'form.currcat_0'} eq 'communities::0') {
903: $status_title{'Active'} = 'Show count for current member access';
904: $status_title{'Future'} = 'Show count for future member access';
905: }
906: } else {
907: %status_title = &Apache::lonlocal::texthash(
908: Previous => 'Previous access',
909: Active => 'Current access',
910: Future => 'Future access',
911: );
912: }
913: return (\%status_title,\@status_order);
914: }
915:
916: sub get_wasactive_text {
917: my $wasacctext = ' -- ';
918: if ($env{'form.currcat_0'} eq 'communities::0') {
919: $wasacctext .= &mt('where member access status was current ...');
920: } else {
921: $wasacctext .= &mt('where student access status was current ...');
922: }
923: return $wasacctext;
924: }
925:
926: sub search_official_courselist {
927: my ($domain,$numtitles,$codetitles) = @_;
928: my $instcode = &Apache::courseclassifier::instcode_search_str($domain,$numtitles,$codetitles);
929: my $showhidden;
930: if (&user_is_dc($domain)) {
931: $showhidden = $env{'form.showhidden'};
932: }
933: my %courses =
934: &Apache::lonnet::courseiddump($domain,'.',1,$instcode,'.','.',undef,undef,
935: 'Course',1,$env{'form.showselfenroll'},undef,
936: $showhidden,'coursecatalog');
937: return %courses;
938: }
939:
940: sub search_courselist {
941: my ($domain,$subcats) = @_;
942: my $cat_maxdepth = $env{'form.catalog_maxdepth'};
943: my $filter = $env{'form.currcat_'.$cat_maxdepth};
944: if (($filter eq '') && ($cat_maxdepth > 0)) {
945: my $shallower = $cat_maxdepth - 1;
946: $filter = $env{'form.currcat_'.$shallower};
947: }
948: my %courses;
949: my $filterstr;
950: if ($filter ne '') {
951: if ($env{'form.withsubcats'}) {
952: if (ref($subcats) eq 'HASH') {
953: if (ref($subcats->{$filter}) eq 'ARRAY') {
954: $filterstr = join('&',@{$subcats->{$filter}});
955: if ($filterstr ne '') {
956: $filterstr = $filter.'&'.$filterstr;
957: }
958: } else {
959: $filterstr = $filter;
960: }
961: } else {
962: $filterstr = $filter;
963: }
964: } else {
965: $filterstr = $filter;
966: }
967: my ($showhidden,$typefilter);
968: if (&user_is_dc($domain)) {
969: $showhidden = $env{'form.showhidden'};
970: }
971: if ($env{'form.currcat_0'} eq 'communities::0') {
972: $typefilter = 'Community';
973: } else {
974: $typefilter = '.';
975: }
976: %courses =
977: &Apache::lonnet::courseiddump($domain,'.',1,'.','.','.',undef,undef,
978: $typefilter,1,$env{'form.showselfenroll'},
979: $filterstr,$showhidden,'coursecatalog');
980: }
981: return %courses;
982: }
983:
984: sub print_course_listing {
985: my ($domain,$numtitles,$trails,$allitems,$subcats,$codetitles,$canviewall) = @_;
986: my $output;
987: my %courses;
988: my $knownuser = &user_is_known();
989: my $canviewall = &canview_all();
990: my $details = $env{'form.coursenum'};
991: if (&user_is_dc($domain) || $canviewall) {
992: if ($env{'form.showdetails'}) {
993: $details = 1;
994: }
995: }
996: if ($env{'form.coursenum'} ne '') {
997: %courses = &Apache::lonnet::courseiddump($domain,'.',1,'.','.',
998: $env{'form.coursenum'},
999: undef,undef,'.',1);
1000: if (keys(%courses) == 0) {
1001: $output = '<p class="LC_error">';
1002: if ($env{'form.currcat_0'} eq 'communities::0') {
1003: $output .= &mt('The courseID provided does not match a community in this domain.');
1004: } else {
1005: $output .= &mt('The courseID provided does not match a course in this domain.');
1006: }
1007: $output .= '</p>';
1008: return $output;
1009: }
1010: } else {
1011: if ($env{'form.currcat_0'} eq 'instcode::0') {
1012: %courses = &search_official_courselist($domain,$numtitles,$codetitles);
1013: } else {
1014: %courses = &search_courselist($domain,$subcats);
1015: }
1016: if (keys(%courses) == 0) {
1017: $output = '<p class="LC_info">';
1018: if ($env{'form.currcat_0'} eq 'communities::0') {
1019: $output .= &mt('No communities match the criteria you selected.');
1020: } else {
1021: $output .= &mt('No courses match the criteria you selected.');
1022: }
1023: $output .= '</p>';
1024: return $output;
1025: }
1026: if (($knownuser) && (!$env{'form.showdetails'}) && (!&user_is_dc($domain)) && (!$canviewall)) {
1027: $output = '<b>'.&mt('Note for students:').'</b> '
1028: .&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.')
1029: .'<br /><br />';
1030: }
1031: }
1032: my $now = time;
1033: $output .= &construct_data_table($knownuser,$domain,\%courses,$details,undef,
1034: $now,$trails,$allitems,$canviewall);
1035: $output .= "\n".'<form name="linklaunch" method="post" action="">'.
1036: '<input type="hidden" name="backto" value="coursecatalog" />'.
1037: '<input type="hidden" name="courseid" value="" />'.
1038: &Apache::lonhtmlcommon::echo_form_input(['catalogfilter','courseid']).'</form>';
1039: return $output;
1040: }
1041:
1042: sub construct_data_table {
1043: my ($knownuser,$domain,$courses,$details,$usersections,$now,$trails,$allitems,$canviewall) = @_;
1044: my %sortname;
1045: if (($details eq '') || ($env{'form.showdetails'})) {
1046: $sortname{'Code'} = 'code';
1047: $sortname{'Categories'} = 'cats';
1048: $sortname{'Title'} = 'title';
1049: $sortname{'Owner & Co-owner(s)'} = 'owner';
1050: }
1051: my $output = &Apache::loncommon::start_data_table().
1052: &Apache::loncommon::start_data_table_header_row();
1053: my @coltitles = ('Count');
1054: if ($env{'form.currcat_0'} eq 'instcode::0') {
1055: push(@coltitles,'Code');
1056: } else {
1057: push(@coltitles,'Categories');
1058: }
1059: push(@coltitles,('Sections','Crosslisted','Title','Owner & Co-owner(s)'));
1060: if (ref($usersections) eq 'HASH') {
1061: $coltitles[1] = 'Your Section';
1062: }
1063: foreach my $item (@coltitles) {
1064: $output .= '<th>';
1065: if (defined($sortname{$item})) {
1066: $output .= '<a href="javascript:changeSort('."'$sortname{$item}'".')">'.&mt($item).'</a>';
1067: } elsif ($item eq 'Count') {
1068: $output .= ' ';
1069: } else {
1070: $output .= &mt($item);
1071: }
1072: $output .= '</th>';
1073: }
1074: my (@fields,%fieldtitles,$wasactiveon);
1075: if ($knownuser || ($canviewall && $details)) {
1076: if ($details) {
1077: if ($env{'form.currcat_0'} eq 'communities::0') {
1078: $output .= '<th>'.&mt('Default Access Dates for Members').'</th>'.
1079: '<th>'.&mt('Member Counts').'</th>';
1080: } else {
1081: $output .=
1082: '<th>'.&mt('Default Access Dates for Students').'</th>'.
1083: '<th>'.&mt('Student Counts').'</th>'.
1084: '<th>'.&mt('Auto-enrollment of[_1]registered students','<br />').'</th>';
1085: }
1086: my ($titlesref,$orderref) = &get_statustitles();
1087: my @statuses;
1088: if (&user_is_dc($domain) || $canviewall) {
1089: @statuses = &Apache::loncommon::get_env_multiple('form.showcounts');
1090: if (grep(/^Previous$/,@statuses)) {
1091: if ($env{'form.wasactive'} eq 'date') {
1092: $wasactiveon =
1093: &Apache::lonhtmlcommon::get_date_from_form('wasactiveon');
1094: } else {
1095: $wasactiveon = $env{'form.wasactive'};
1096: }
1097: }
1098: if (ref($orderref) eq 'ARRAY') {
1099: foreach my $status (@{$orderref}) {
1100: if (grep(/^\Q$status\E$/,@statuses)) {
1101: push(@fields,$status);
1102: }
1103: }
1104: }
1105: } else {
1106: @fields = ('Active','Future');
1107: }
1108: foreach my $status (@fields) {
1109: my $title;
1110: if (ref($titlesref) eq 'HASH') {
1111: $title = $titlesref->{$status};
1112: }
1113: unless ($title) {
1114: $title = &mt($status);
1115: }
1116: $fieldtitles{$status} = $title;
1117: }
1118: } else {
1119: $output .= '<th>'.&mt('Details').'</th>';
1120: }
1121: }
1122: $output .= '<th>'.&mt('Self-enroll (if permitted)').'</th>';
1123: &Apache::loncommon::end_data_table_header_row();
1124: my (%numbers,%creditsum);
1125: my ($showcredits,$defofficial,$defunofficial,$deftextbook);
1126: my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
1127: unless ($env{'form.currcat_0'} eq 'communities::0') {
1128: if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'} || $domdefaults{'textbookcredits'}) {
1129: $showcredits = 1;
1130: $defofficial = $domdefaults{'officialcredits'};
1131: $defunofficial = $domdefaults{'unofficialcredits'};
1132: $deftextbook = $domdefaults{'textbookcredits'};
1133: }
1134: }
1135: my %courseinfo = &build_courseinfo_hash($courses,$knownuser,$domain,$canviewall,$details,
1136: $usersections,\@fields,\%fieldtitles,
1137: $wasactiveon,\%numbers,\%creditsum,
1138: $showcredits,$defofficial,$defunofficial,$deftextbook);
1139: my %Sortby;
1140: foreach my $course (sort(keys(%{$courses}))) {
1141: if ($env{'form.sortby'} eq 'code') {
1142: push(@{$Sortby{$courseinfo{$course}{'code'}}},$course);
1143: } elsif ($env{'form.sortby'} eq 'cats') {
1144: push(@{$Sortby{$courseinfo{$course}{'categories'}}},$course);
1145: } elsif ($env{'form.sortby'} eq 'owner') {
1146: push(@{$Sortby{$courseinfo{$course}{'ownerlastnames'}}},$course);
1147: } else {
1148: my $clean_title = $courseinfo{$course}{'title'};
1149: $clean_title =~ s/\W+//g;
1150: if ($clean_title eq '') {
1151: $clean_title = $courseinfo{$course}{'title'};
1152: }
1153: push(@{$Sortby{$clean_title}},$course);
1154: }
1155: }
1156: my @sorted_courses;
1157: if (($env{'form.sortby'} eq 'code') || ($env{'form.sortby'} eq 'owner') ||
1158: ($env{'form.sortby'} eq 'cats')) {
1159: @sorted_courses = sort(keys(%Sortby));
1160: } else {
1161: @sorted_courses = sort { lc($a) cmp lc($b) } (keys(%Sortby));
1162: }
1163: my $count = 1;
1164: my $totalsec = 0;
1165: foreach my $item (@sorted_courses) {
1166: foreach my $course (@{$Sortby{$item}}) {
1167: $output.=&Apache::loncommon::start_data_table_row();
1168: $output.=&courseinfo_row($courseinfo{$course},$knownuser,$details,
1169: \$count,$now,$course,$trails,$allitems,\%numbers,$canviewall);
1170: $output.=&Apache::loncommon::end_data_table_row();
1171: }
1172: }
1173: if (($knownuser || $canviewall) && ($count > 1) && $env{'form.showdetails'}) {
1174: if (&user_is_dc($domain) || $canviewall) {
1175: my %lt = &Apache::lonlocal::texthash (
1176: 'Active' => 'Total current students',
1177: 'Future' => 'Total future students',
1178: 'Previous' => 'Total previous students',
1179: 'courses' => 'Total unique codes and courses without codes',
1180: 'sections' => 'Total sections',
1181: 'xlists' => 'Total cross-listings',
1182: );
1183: if ($showcredits) {
1184: $lt{'cr_Active'} = &mt('Total current student credit hours');
1185: $lt{'cr_Future'} = &mt('Total future student credit hours');
1186: $lt{'cr_Previous'} = &mt('Total previous student credit hours');
1187: }
1188: if ($env{'form.currcat_0'} eq 'communities::0') {
1189: $lt{'courses'} = &mt('Total communities');
1190: $lt{'Active'} = &mt('Total current members');
1191: $lt{'Future'} = &mt('Total future members');
1192: $lt{'Previous'} = &mt('Total previous members');
1193: }
1194: my $colspan = 8;
1195: if ($showcredits) {
1196: $colspan = 4;
1197: }
1198: $output .= '<tr class="LC_footer_row">'.
1199: '<td colspan="2"> </td>'.
1200: '<td colspan="'.$colspan.'">'.
1201: '<table border="0">';
1202: foreach my $item ('courses','sections','xlists') {
1203: $output .= '<tr>'.
1204: '<td>'.$lt{$item}.'</td><td> </td>'.
1205: '<td align="right">'.$numbers{$item}.'</td>'.
1206: '</tr>'."\n";
1207: }
1208: if (@fields > 0) {
1209: foreach my $status (@fields) {
1210: $output .= '<tr>'.
1211: '<td>'.$lt{$status}.'</td><td> </td>'.
1212: '<td align="right">'.$numbers{$status}.'</td>'.
1213: '</tr>'."\n";
1214: }
1215: }
1216: $output .= '</table></td>';
1217: if ($showcredits) {
1218: $output .= '<td colspan="'.$colspan.'" valign="bottom"><table>';
1219: foreach my $status (@fields) {
1220: $output .= '<tr>'.
1221: '<td>'.$lt{'cr_'.$status}.'</td><td> </td>'.
1222: '<td align="right">'.$creditsum{$status}.'</td></tr>';
1223: }
1224: $output .= '</table></td></tr>';
1225: }
1226: }
1227: }
1228: $output .= &Apache::loncommon::end_data_table();
1229: return $output;
1230: }
1231:
1232: sub build_courseinfo_hash {
1233: my ($courses,$knownuser,$domain,$canviewall,$details,$usersections,$fields,$fieldtitles,
1234: $wasactiveon,$numbers,$creditsum,$showcredits,$defofficial,$defunofficial) = @_;
1235: my %courseinfo;
1236: my $now = time;
1237: my $gettotals;
1238: if ((keys(%{$courses}) > 0) && (&user_is_dc($domain) || $canviewall) && ($details)) {
1239: $gettotals = 1;
1240: }
1241: my (%uniquecodes,$nocodes,$defcreds);
1242: foreach my $course (keys(%{$courses})) {
1243: my $descr;
1244: if (ref($courses->{$course}) eq 'HASH') {
1245: $descr = $courses->{$course}{'description'};
1246: }
1247: my $cleandesc=&HTML::Entities::encode($descr,'<>&"');
1248: $cleandesc=~s/'/\\'/g;
1249: $cleandesc =~ s/^\s+//;
1250: my ($cdom,$cnum)=split(/\_/,$course);
1251: my ($instcode,$singleowner,$ttype,$selfenroll_types,
1252: $selfenroll_start,$selfenroll_end,@owners,%ownernames,$categories);
1253: if (ref($courses->{$course}) eq 'HASH') {
1254: $descr = $courses->{$course}{'description'};
1255: $instcode = $courses->{$course}{'inst_code'};
1256: $singleowner = $courses->{$course}{'owner'};
1257: $ttype = $courses->{$course}{'type'};
1258: $selfenroll_types = $courses->{$course}{'selfenroll_types'};
1259: $selfenroll_start = $courses->{$course}{'selfenroll_start_date'};
1260: $selfenroll_end = $courses->{$course}{'selfenroll_end_date'};
1261: $categories = $courses->{$course}{'categories'};
1262: push(@owners,$singleowner);
1263: if ($courses->{$course}{'co-owners'} ne '') {
1264: foreach my $item (split(/,/,$courses->{$course}{'co-owners'})) {
1265: push(@owners,$item);
1266: }
1267: }
1268: }
1269: if ($instcode ne '') {
1270: $uniquecodes{$instcode} = 1;
1271: } else {
1272: $nocodes ++;
1273: }
1274: foreach my $owner (@owners) {
1275: my ($ownername,$ownerdom);
1276: if ($owner =~ /:/) {
1277: ($ownername,$ownerdom) = split(/:/,$owner);
1278: } else {
1279: $ownername = $owner;
1280: if ($owner ne '') {
1281: $ownerdom = $cdom;
1282: }
1283: }
1284: if ($ownername ne '' && $ownerdom ne '') {
1285: my %namehash=&Apache::loncommon::getnames($ownername,$ownerdom);
1286: $ownernames{$ownername.':'.$ownerdom} = \%namehash;
1287: }
1288: }
1289: $courseinfo{$course}{'cdom'} = $cdom;
1290: $courseinfo{$course}{'cnum'} = $cnum;
1291: $courseinfo{$course}{'code'} = $instcode;
1292: my @lastnames;
1293: foreach my $owner (keys(%ownernames)) {
1294: if (ref($ownernames{$owner}) eq 'HASH') {
1295: push(@lastnames,$ownernames{$owner}{'lastname'});
1296: }
1297: }
1298: $courseinfo{$course}{'ownerlastnames'} = join(', ',sort(@lastnames));
1299: $courseinfo{$course}{'title'} = $cleandesc;
1300: $courseinfo{$course}{'owner'} = $singleowner;
1301: $courseinfo{$course}{'selfenroll_types'} = $selfenroll_types;
1302: $courseinfo{$course}{'selfenroll_start'} = $selfenroll_start;
1303: $courseinfo{$course}{'selfenroll_end'} = $selfenroll_end;
1304: $courseinfo{$course}{'categories'} = $categories;
1305:
1306: my %coursehash = &Apache::lonnet::dump('environment',$cdom,$cnum);
1307: my @classids;
1308: my @crosslistings;
1309: my ($seclist,$numsec) =
1310: &identify_sections($coursehash{'internal.sectionnums'});
1311: $courseinfo{$course}{'seclist'} = $seclist;
1312: my ($xlist_items,$numxlist) =
1313: &identify_sections($coursehash{'internal.crosslistings'});
1314: if (ref($numbers) eq 'HASH') {
1315: $numbers->{'sections'} += $numsec;
1316: $numbers->{'xlists'} += $numxlist;
1317: }
1318: my $showsyllabus = 1; # default is to include a syllabus link
1319: if (defined($coursehash{'showsyllabus'})) {
1320: $showsyllabus = $coursehash{'showsyllabus'};
1321: }
1322: $courseinfo{$course}{'showsyllabus'} = $showsyllabus;
1323: if ($showcredits) {
1324: if ($coursehash{'internal.defaultcredits'}) {
1325: $courseinfo{$course}{'defaultcredits'} = $coursehash{'internal.defaultcredits'};
1326: } elsif ($instcode ne '') {
1327: $courseinfo{$course}{'defaultcredits'} = $defofficial;
1328: } else {
1329: $courseinfo{$course}{'defaultcredits'} = $defunofficial;
1330: }
1331: $defcreds = $courseinfo{$course}{'defaultcredits'};
1332: }
1333: if (((defined($env{'form.coursenum'}) && ($cnum eq $env{'form.coursenum'}))) ||
1334: (($knownuser || $canviewall) && ($details == 1))) {
1335: my $milestone;
1336: if ($wasactiveon eq 'accessend') {
1337: if ($coursehash{'default_enrollment_end_date'}) {
1338: $milestone = $coursehash{'default_enrollment_end_date'};
1339: } else {
1340: $milestone = time;
1341: }
1342: } elsif ($wasactiveon eq 'enrollend') {
1343: if ($coursehash{'internal.autoend'}) {
1344: $milestone = $coursehash{'internal.autoend'};
1345: } else {
1346: $milestone = time;
1347: }
1348: } else {
1349: $milestone = $wasactiveon;
1350: }
1351: $courseinfo{$course}{'counts'} =
1352: &count_students($cdom,$cnum,$numsec,$fields,$fieldtitles,$gettotals,
1353: $numbers,$creditsum,$showcredits,$defcreds,$milestone);
1354: if ($instcode ne '') {
1355: $courseinfo{$course}{'autoenrollment'} =
1356: &autoenroll_info(\%coursehash,$now,$seclist,$xlist_items,
1357: $instcode,\@owners,$cdom,$cnum);
1358: }
1359: my $startaccess = '';
1360: my $endaccess = '';
1361: my $accessdates;
1362: if ( defined($coursehash{'default_enrollment_start_date'}) ) {
1363: $startaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_start_date'});
1364: }
1365: if ( defined($coursehash{'default_enrollment_end_date'}) ) {
1366: $endaccess = &Apache::lonlocal::locallocaltime($coursehash{'default_enrollment_end_date'});
1367: if ($coursehash{'default_enrollment_end_date'} == 0) {
1368: $endaccess = &mt('No ending date');
1369: }
1370: }
1371: if ($startaccess) {
1372: $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$startaccess).'<br />';
1373: }
1374: if ($endaccess) {
1375: $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$endaccess).'<br />';
1376: }
1377: if (($selfenroll_types ne '') &&
1378: ($selfenroll_end > 0 && $selfenroll_end > $now)) {
1379: my ($selfenroll_start_access,$selfenroll_end_access);
1380: if (($coursehash{'default_enrollment_start_date'} ne
1381: $coursehash{'internal.selfenroll_start_access'}) ||
1382: ($coursehash{'default_enrollment_end_date'} ne
1383: $coursehash{'internal.selfenroll_end_access'})) {
1384: if ( defined($coursehash{'internal.selfenroll_start_access'}) ) {
1385: $selfenroll_start_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_start_access'});
1386: }
1387: if ( defined($coursehash{'default_enrollment_end_date'}) ) {
1388: $selfenroll_end_access = &Apache::lonlocal::locallocaltime($coursehash{'internal.selfenroll_end_access'});
1389: if ($coursehash{'internal.selfenroll_end_access'} == 0) {
1390: $selfenroll_end_access = &mt('No ending date');
1391: }
1392: }
1393: if ($selfenroll_start_access || $selfenroll_end_access) {
1394: $accessdates .= '<br/><br /><i>'.&mt('Self-enrollers:').'</i><br />';
1395: if ($selfenroll_start_access) {
1396: $accessdates .= '<i>'.&mt('From:[_1]','</i> '.$selfenroll_start_access).'<br />';
1397: }
1398: if ($selfenroll_end_access) {
1399: $accessdates .= '<i>'.&mt('To:[_1]','</i> '.$selfenroll_end_access).'<br />';
1400: }
1401: }
1402: }
1403: }
1404: $courseinfo{$course}{'access'} = $accessdates;
1405: }
1406: if ($xlist_items eq '') {
1407: $xlist_items = &mt('No');
1408: }
1409: $courseinfo{$course}{'xlist'} = $xlist_items;
1410: }
1411: if (ref($numbers) eq 'HASH') {
1412: $numbers->{'courses'} = $nocodes + scalar(keys(%uniquecodes));
1413: }
1414: return %courseinfo;
1415: }
1416:
1417: sub count_students {
1418: my ($cdom,$cnum,$numsec,$fieldsref,$titlesref,$getcounts,$numbers,$creditsum,
1419: $showcredits,$defcreds,$wasactiveon) = @_;
1420: my $countslist = '<span class="LC_nobreak">'.
1421: &mt('[quant,_1,section,sections,No sections]',$numsec).'</span>';
1422: my (@fields,%titles,$showexpired);
1423: if ((ref($fieldsref) eq 'ARRAY') && (ref($titlesref) eq 'HASH') &&
1424: (ref($numbers) eq 'HASH')) {
1425: @fields = @{$fieldsref};
1426: %titles = %{$titlesref};
1427: if (grep(/^Previous$/,@fields)) {
1428: $showexpired = 1;
1429: }
1430: } else {
1431: return;
1432: }
1433: my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1434: my (%student_count,%credit_count);
1435: %student_count = (
1436: Active => 0,
1437: Future => 0,
1438: Previous => 0,
1439: );
1440: if ($showcredits) {
1441: %credit_count = (
1442: Active => 0,
1443: Future => 0,
1444: Previous => 0,
1445: );
1446: }
1447: my %idx;
1448: $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
1449: $idx{'end'} = &Apache::loncoursedata::CL_END();
1450: $idx{'credits'} = &Apache::loncoursedata::CL_CREDITS();
1451: while (my ($student,$data) = each(%$classlist)) {
1452: my $status = $data->[$idx{'status'}];
1453: my $credits = $data->[$idx{'credits'}];
1454: if ($credits eq '') {
1455: $credits = $defcreds;
1456: }
1457: if ($status eq 'Expired') {
1458: if (($showexpired) &&
1459: ($data->[$idx{'end'}] >= $wasactiveon)) {
1460: $student_count{'Previous'} ++;
1461: if ($showcredits) {
1462: $credit_count{'Previous'} += $credits;
1463: }
1464: }
1465: } else {
1466: $student_count{$status} ++;
1467: if ($showcredits) {
1468: $credit_count{$status} += $credits;
1469: }
1470: }
1471: }
1472: if (@fields) {
1473: $countslist .= ':<br />';
1474: foreach my $status (@fields) {
1475: $countslist .= '<span class="LC_nobreak">'.$titles{$status}.': '.
1476: $student_count{$status}.'</span><br />';
1477: $numbers->{$status} += $student_count{$status};
1478: if ($showcredits) {
1479: $creditsum->{$status} += $credit_count{$status};
1480: }
1481: }
1482: }
1483: return $countslist;
1484: }
1485:
1486: sub courseinfo_row {
1487: my ($info,$knownuser,$details,$countref,$now,$course,$trails,$allitems,$numbers,$canviewall) = @_;
1488: my ($cdom,$cnum,$title,$ownerlast,$code,$owner,$seclist,$xlist_items,
1489: $accessdates,$showsyllabus,$counts,$autoenrollment,$output,$categories);
1490: if (ref($info) eq 'HASH') {
1491: $cdom = $info->{'cdom'};
1492: $cnum = $info->{'cnum'};
1493: $title = $info->{'title'};
1494: $ownerlast = $info->{'ownerlastnames'};
1495: $code = $info->{'code'};
1496: $owner = $info->{'owner'};
1497: $seclist = $info->{'seclist'};
1498: $xlist_items = $info->{'xlist'};
1499: $accessdates = $info->{'access'};
1500: $counts = $info->{'counts'};
1501: $autoenrollment = $info->{'autoenrollment'};
1502: $showsyllabus = $info->{'showsyllabus'};
1503: $categories = $info->{'categories'};
1504: } else {
1505: $output = '<td colspan="8">'.&mt('No information available for [_1].',
1506: $code).'</td>';
1507: return $output;
1508: }
1509: $output .= '<td>'.$$countref.'</td>';
1510: if ($env{'form.currcat_0'} eq 'instcode::0') {
1511: $output .= '<td>'.$code.'</td>';
1512: } else {
1513: my ($categorylist,@cats);
1514: if ($categories ne '') {
1515: @cats = split('&',$categories);
1516: }
1517: if ((ref($trails) eq 'ARRAY') && (ref($allitems) eq 'HASH')) {
1518: my @categories = map { $trails->[$allitems->{$_}]; } @cats;
1519: $categorylist = join('<br />',@categories);
1520: }
1521: if ($categorylist eq '') {
1522: $categorylist = ' ';
1523: }
1524: $output .= '<td>'.$categorylist.'</td>';
1525: }
1526: $output .= '<td>'.$seclist.'</td>'.
1527: '<td>'.$xlist_items.'</td>'.
1528: '<td>'.$title.' <font size="-2">';
1529: if ($showsyllabus) {
1530: $output .= '<a href="javascript:ToSyllabus('."'$cdom','$cnum'".')">'.&mt('Syllabus').'</a>';
1531: } else {
1532: $output .= ' ';
1533: }
1534: $output .= '</font></td>'.
1535: '<td>'.$ownerlast.'</td>';
1536: if (($knownuser) || ($canviewall && $details)) {
1537: if ($details) {
1538: $output .=
1539: '<td>'.$accessdates.'</td>'.
1540: '<td>'.$counts.'</td>';
1541: unless ($env{'form.currcat_0'} eq 'communities::0') {
1542: $output .= '<td>'.$autoenrollment.'</td>';
1543: }
1544: } else {
1545: $output .= "<td><a href=\"javascript:setCourseId('$cnum')\">".&mt('Show more details').'</a></td>';
1546: }
1547: }
1548: my $selfenroll = &selfenroll_status($info,$course);
1549: if ($selfenroll) {
1550: $output .= '<td>'.$selfenroll.'</td>';
1551: } else {
1552: $output .= '<td> </td>';
1553: }
1554: $$countref ++;
1555: return $output;
1556: }
1557:
1558: sub selfenroll_status {
1559: my ($info,$course) = @_;
1560: my $now = time;
1561: my $output;
1562: if (ref($info) eq 'HASH') {
1563: if ($info->{'selfenroll_types'}) {
1564: my $showstart = &Apache::lonlocal::locallocaltime($info->{'selfenroll_start'});
1565: my $showend = &Apache::lonlocal::locallocaltime($info->{'selfenroll_end'});
1566: if (($info->{'selfenroll_end'} > 0) && ($info->{'selfenroll_end'} > $now)) {
1567: if (($info->{'selfenroll_start'} > 0) && ($info->{'selfenroll_start'} > $now)) {
1568: $output = &mt('Starts: [_1]','<span class="LC_cusr_emph">'.$showstart.'</span>').'<br />'.&mt('Ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>');
1569: } else {
1570: $output = '<a href="javascript:ToSelfenroll('."'$course'".')">'.
1571: &mt('Enroll in course').'</a><br />';
1572: if ($info->{'selfenroll_end'} == 0) {
1573: $output .= &mt('Available permanently');
1574: } elsif ($info->{'selfenroll_end'} > $now) {
1575: $output .= &mt('Self-enrollment ends: [_1]','<span class="LC_cusr_emph">'.$showend.'</span>');
1576: }
1577: }
1578: }
1579: }
1580: }
1581: return $output;
1582: }
1583:
1584: sub identify_sections {
1585: my ($seclist) = @_;
1586: my @secnums;
1587: if ($seclist =~ /,/) {
1588: my @sections = split(/,/,$seclist);
1589: foreach my $sec (@sections) {
1590: $sec =~ s/:[^:]*$//;
1591: push(@secnums,$sec);
1592: }
1593: } else {
1594: if ($seclist =~ m/^([^:]+):/) {
1595: my $sec = $1;
1596: if (!grep(/^\Q$sec\E$/,@secnums)) {
1597: push(@secnums,$sec);
1598: }
1599: }
1600: }
1601: @secnums = sort {$a <=> $b} @secnums;
1602: $seclist = join(', ',@secnums);
1603: my $numsec = @secnums;
1604: return ($seclist,$numsec);
1605: }
1606:
1607: sub get_valid_classes {
1608: my ($seclist,$xlist_items,$crscode,$owners,$cdom,$cnum) = @_;
1609: my $response;
1610: my %validations;
1611: @{$validations{'sections'}} = ();
1612: @{$validations{'xlists'}} = ();
1613: my $totalitems = 0;
1614: if ($seclist) {
1615: foreach my $sec (split(/, /,$seclist)) {
1616: my $class = $crscode.$sec;
1617: if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
1618: $class) eq 'ok') {
1619: if (!grep(/^\Q$sec$\E/,@{$validations{'sections'}})) {
1620: push(@{$validations{'sections'}},$sec);
1621: $totalitems ++;
1622: }
1623: }
1624: }
1625: }
1626: if ($xlist_items) {
1627: foreach my $item (split(/, /,$xlist_items)) {
1628: if (&Apache::lonnet::auto_validate_class_sec($cdom,$cnum,$owners,
1629: $item) eq 'ok') {
1630: if (!grep(/^\Q$item$\E/,@{$validations{'xlists'}})) {
1631: push(@{$validations{'xlists'}},$item);
1632: $totalitems ++;
1633: }
1634: }
1635: }
1636: }
1637: if ($totalitems > 0) {
1638: if (@{$validations{'sections'}}) {
1639: $response = &mt('Sections:').' '.
1640: join(', ',@{$validations{'sections'}}).'<br />';
1641: }
1642: if (@{$validations{'xlists'}}) {
1643: $response .= &mt('Courses:').' '.
1644: join(', ',@{$validations{'xlists'}});
1645: }
1646: }
1647: return $response;
1648: }
1649:
1650: sub autoenroll_info {
1651: my ($coursehash,$now,$seclist,$xlist_items,$code,$owners,$cdom,$cnum) = @_;
1652: my $autoenrolldates = &mt('Not enabled');
1653: if (defined($coursehash->{'internal.autoadds'}) && $coursehash->{'internal.autoadds'} == 1) {
1654: my ($autostart,$autoend);
1655: if ( defined($coursehash->{'internal.autostart'}) ) {
1656: $autostart = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autostart'});
1657: }
1658: if ( defined($coursehash->{'internal.autoend'}) ) {
1659: $autoend = &Apache::lonlocal::locallocaltime($coursehash->{'internal.autoend'});
1660: }
1661: if ($coursehash->{'internal.autostart'} > $now) {
1662: if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
1663: $autoenrolldates = &mt('Not enabled');
1664: } else {
1665: my $valid_classes =
1666: &get_valid_classes($seclist,$xlist_items,$code,
1667: $owners,$cdom,$cnum);
1668: if ($valid_classes ne '') {
1669: $autoenrolldates = &mt('Not enabled').'<br />'
1670: .&mt('Starts: [_1]',$autostart)
1671: .'<br />'.$valid_classes;
1672: }
1673: }
1674: } else {
1675: if ($coursehash->{'internal.autoend'} && $coursehash->{'internal.autoend'} < $now) {
1676: $autoenrolldates = &mt('Not enabled').'<br />'
1677: .&mt('Ended: [_1]',$autoend);
1678: } else {
1679: my $valid_classes = &get_valid_classes($seclist,$xlist_items,
1680: $code,$owners,$cdom,$cnum);
1681: if ($valid_classes ne '') {
1682: $autoenrolldates = &mt('Currently enabled').'<br />'.
1683: $valid_classes;
1684: }
1685: }
1686: }
1687: }
1688: return $autoenrolldates;
1689: }
1690:
1691: sub user_is_known {
1692: my $known = 0;
1693: if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public'
1694: && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
1695: $known = 1;
1696: }
1697: return $known;
1698: }
1699:
1700: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>