1: # The LearningOnline Network with CAPA
2: # a pile of common html routines
3: #
4: # $Id: lonhtmlcommon.pm,v 1.216 2009/05/20 17:39:06 bisitz 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: ######################################################################
30:
31: =pod
32:
33: =head1 NAME
34:
35: Apache::lonhtmlcommon - routines to do common html things
36:
37: =head1 SYNOPSIS
38:
39: Referenced by other mod_perl Apache modules.
40:
41: =head1 INTRODUCTION
42:
43: lonhtmlcommon is a collection of subroutines used to present information
44: in a consistent html format, or provide other functionality related to
45: html.
46:
47: =head2 General Subroutines
48:
49: =over 4
50:
51: =cut
52:
53: ######################################################################
54: ######################################################################
55:
56: package Apache::lonhtmlcommon;
57:
58: use strict;
59: use Time::Local;
60: use Time::HiRes;
61: use Apache::lonlocal;
62: use Apache::lonnet;
63: use LONCAPA;
64:
65: ##############################################
66: ##############################################
67:
68: =pod
69:
70: =item confirm_success
71:
72: Successful completion of an operation message
73:
74: =cut
75:
76: sub confirm_success {
77: my ($message,$failure)=@_;
78: if ($failure) {
79: return '<span class="LC_error">'."\n"
80: .'<img src="/adm/lonIcons/navmap.wrong.gif" /> '."\n"
81: .$message."\n"
82: .'</span>'."\n";
83: } else {
84: return '<span class="LC_success">'."\n"
85: .'<img src="/adm/lonIcons/navmap.correct.gif" /> '."\n"
86: .$message."\n"
87: .'</span>'."\n";
88: }
89: }
90:
91: ##############################################
92: ##############################################
93:
94: =pod
95:
96: =item dragmath_button
97:
98: Creates a button that launches a dragmath popup-window, in which an
99: expression can be edited and pasted as LaTeX into a specified textarea.
100:
101: textarea - Name of the textarea to edit.
102: helpicon - If true, show a help icon to the right of the button.
103:
104: =cut
105:
106: sub dragmath_button {
107: my ($textarea,$helpicon) = @_;
108: my $help_text;
109: if ($helpicon) {
110: $help_text = &Apache::loncommon::help_open_topic('Authoring_Math_Editor');
111: }
112: my $buttontext=&mt('Edit Math');
113: return <<ENDDRAGMATH;
114: <input type="button" value="$buttontext", onclick="javascript:mathedit('$textarea',document)" />$help_text
115: ENDDRAGMATH
116: }
117:
118: ##############################################
119:
120: =pod
121:
122: =item dragmath_js
123:
124: Javascript used to open pop-up window containing dragmath applet which
125: can be used to paste LaTeX into a textarea.
126:
127: =cut
128:
129: sub dragmath_js {
130: my ($popup) = @_;
131: return <<ENDDRAGMATHJS;
132: <script type="text/javascript">
133: function mathedit(textarea, doc) {
134: targetEntry = textarea;
135: targetDoc = doc;
136: newwin = window.open("/adm/dragmath/applet/$popup.html","","width=565,height=500,resizable");
137: }
138: </script>
139:
140: ENDDRAGMATHJS
141: }
142:
143:
144: ##############################################
145: ##############################################
146:
147: =pod
148:
149: =item authorbombs
150:
151: =cut
152:
153: ##############################################
154: ##############################################
155:
156: sub authorbombs {
157: my $url=shift;
158: $url=&Apache::lonnet::declutter($url);
159: my ($udom,$uname)=($url=~m{^($LONCAPA::domain_re)/($LONCAPA::username_re)/});
160: my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
161: foreach (keys %bombs) {
162: if ($_=~/^$udom\/$uname\//) {
163: return '<a href="/adm/bombs/'.$url.
164: '"><img src="'.&Apache::loncommon::lonhttpdurl('/adm/lonMisc/bomb.gif').'" border="0" /></a>'.
165: &Apache::loncommon::help_open_topic('About_Bombs');
166: }
167: }
168: return '';
169: }
170:
171: ##############################################
172: ##############################################
173:
174: sub recent_filename {
175: my $area=shift;
176: return 'nohist_recent_'.&escape($area);
177: }
178:
179: sub store_recent {
180: my ($area,$name,$value,$freeze)=@_;
181: my $file=&recent_filename($area);
182: my %recent=&Apache::lonnet::dump($file);
183: if (scalar(keys(%recent))>20) {
184: # remove oldest value
185: my $oldest=time();
186: my $delkey='';
187: foreach my $item (keys(%recent)) {
188: my $thistime=(split(/\&/,$recent{$item}))[0];
189: if (($thistime ne "always_include") && ($thistime<$oldest)) {
190: $oldest=$thistime;
191: $delkey=$item;
192: }
193: }
194: &Apache::lonnet::del($file,[$delkey]);
195: }
196: # store new value
197: my $timestamp;
198: if ($freeze) {
199: $timestamp = "always_include";
200: } else {
201: $timestamp = time();
202: }
203: &Apache::lonnet::put($file,{ $name =>
204: $timestamp.'&'.&escape($value) });
205: }
206:
207: sub remove_recent {
208: my ($area,$names)=@_;
209: my $file=&recent_filename($area);
210: return &Apache::lonnet::del($file,$names);
211: }
212:
213: sub select_recent {
214: my ($area,$fieldname,$event)=@_;
215: my %recent=&Apache::lonnet::dump(&recent_filename($area));
216: my $return="\n<select name='$fieldname'".
217: ($event?" onchange='$event'":'').
218: ">\n<option value=''>--- ".&mt('Recent')." ---</option>";
219: foreach my $value (sort(keys(%recent))) {
220: unless ($value =~/^error\:/) {
221: my $escaped = &Apache::loncommon::escape_url($value);
222: &Apache::loncommon::inhibit_menu_check(\$escaped);
223: $return.="\n<option value='$escaped'>".
224: &unescape((split(/\&/,$recent{$value}))[1]).
225: '</option>';
226: }
227: }
228: $return.="\n</select>\n";
229: return $return;
230: }
231:
232: sub get_recent {
233: my ($area, $n) = @_;
234: my %recent=&Apache::lonnet::dump(&recent_filename($area));
235:
236: # Create hash with key as time and recent as value
237: # Begin filling return_hash with any 'always_include' option
238: my %time_hash = ();
239: my %return_hash = ();
240: foreach my $item (keys %recent) {
241: my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
242: if ($thistime eq 'always_include') {
243: $return_hash{$item} = &unescape($thisvalue);
244: $n--;
245: } else {
246: $time_hash{$thistime} = $item;
247: }
248: }
249:
250: # Sort by decreasing time and return key value pairs
251: my $idx = 1;
252: foreach my $item (reverse(sort(keys(%time_hash)))) {
253: $return_hash{$time_hash{$item}} =
254: &unescape((split(/\&/,$recent{$time_hash{$item}}))[1]);
255: if ($n && ($idx++ >= $n)) {last;}
256: }
257:
258: return %return_hash;
259: }
260:
261: sub get_recent_frozen {
262: my ($area) = @_;
263: my %recent=&Apache::lonnet::dump(&recent_filename($area));
264:
265: # Create hash with all 'frozen' items
266: my %return_hash = ();
267: foreach my $item (keys(%recent)) {
268: my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
269: if ($thistime eq 'always_include') {
270: $return_hash{$item} = &unescape($thisvalue);
271: }
272: }
273: return %return_hash;
274: }
275:
276:
277:
278: =pod
279:
280: =item textbox
281:
282: =cut
283:
284: ##############################################
285: ##############################################
286: sub textbox {
287: my ($name,$value,$size,$special) = @_;
288: $size = 40 if (! defined($size));
289: $value = &HTML::Entities::encode($value,'<>&"');
290: my $Str = '<input type="text" name="'.$name.'" size="'.$size.'" '.
291: 'value="'.$value.'" '.$special.' />';
292: return $Str;
293: }
294:
295: ##############################################
296: ##############################################
297:
298: =pod
299:
300: =item checkbox
301:
302: =cut
303:
304: ##############################################
305: ##############################################
306: sub checkbox {
307: my ($name,$checked,$value) = @_;
308: my $Str = '<input type="checkbox" name="'.$name.'" ';
309: if (defined($value)) {
310: $Str .= 'value="'.$value.'"';
311: }
312: if ($checked) {
313: $Str .= ' checked="checked"';
314: }
315: $Str .= ' />';
316: return $Str;
317: }
318:
319:
320: =pod
321:
322: =item radiobutton
323:
324: =cut
325:
326: ##############################################
327: ##############################################
328: sub radio {
329: my ($name,$checked,$value) = @_;
330: my $Str = '<input type="radio" name="'.$name.'" ';
331: if (defined($value)) {
332: $Str .= 'value="'.$value.'"';
333: }
334: if ($checked eq $value) {
335: $Str .= ' checked="checked"';
336: }
337: $Str .= ' />';
338: return $Str;
339: }
340:
341: ##############################################
342: ##############################################
343:
344: =pod
345:
346: =item &date_setter
347:
348: &date_setter returns html and javascript for a compact date-setting form.
349: To retrieve values from it, use &get_date_from_form().
350:
351: Inputs
352:
353: =over 4
354:
355: =item $dname
356:
357: The name to prepend to the form elements.
358: The form elements defined will be dname_year, dname_month, dname_day,
359: dname_hour, dname_min, and dname_sec.
360:
361: =item $currentvalue
362:
363: The current setting for this time parameter. A unix format time
364: (time in seconds since the beginning of Jan 1st, 1970, GMT.
365: An undefined value is taken to indicate the value is the current time.
366: Also, to be explicit, a value of 'now' also indicates the current time.
367:
368: =item $special
369:
370: Additional html/javascript to be associated with each element in
371: the date_setter. See lonparmset for example usage.
372:
373: =item $includeempty
374:
375: =item $state
376:
377: Specifies the initial state of the form elements. Either 'disabled' or empty.
378: Defaults to empty, which indiciates the form elements are not disabled.
379:
380: =back
381:
382: Bugs
383:
384: The method used to restrict user input will fail in the year 2400.
385:
386: =cut
387:
388: ##############################################
389: ##############################################
390: sub date_setter {
391: my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
392: $no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_;
393: my $now = time;
394: my $wasdefined=1;
395: if (! defined($state) || $state ne 'disabled') {
396: $state = '';
397: }
398: if (! defined($no_hh_mm_ss)) {
399: $no_hh_mm_ss = 0;
400: }
401: if ($currentvalue eq 'now') {
402: $currentvalue = $now;
403: }
404: if ((!defined($currentvalue)) || ($currentvalue eq '')) {
405: $wasdefined=0;
406: if ($includeempty) {
407: $currentvalue = 0;
408: } else {
409: $currentvalue = $now;
410: }
411: }
412: # other potentially useful values: wkday,yrday,is_daylight_savings
413: my $tzname;
414: my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
415: if ($currentvalue) {
416: ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($currentvalue);
417: }
418: unless ($wasdefined) {
419: ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($now);
420: if (($defhour) || ($defmin) || ($defsec)) {
421: $sec=($defsec?$defsec:0);
422: $min=($defmin?$defmin:0);
423: $hour=($defhour?$defhour:0);
424: } elsif (!$includeempty) {
425: $sec=0;
426: $min=0;
427: $hour=0;
428: }
429: }
430: my $result = "\n<!-- $dname date setting form -->\n";
431: $result .= <<ENDJS;
432: <script type="text/javascript">
433: function $dname\_checkday() {
434: var day = document.$formname.$dname\_day.value;
435: var month = document.$formname.$dname\_month.value;
436: var year = document.$formname.$dname\_year.value;
437: var valid = true;
438: if (day < 1) {
439: document.$formname.$dname\_day.value = 1;
440: }
441: if (day > 31) {
442: document.$formname.$dname\_day.value = 31;
443: }
444: if ((month == 1) || (month == 3) || (month == 5) ||
445: (month == 7) || (month == 8) || (month == 10) ||
446: (month == 12)) {
447: if (day > 31) {
448: document.$formname.$dname\_day.value = 31;
449: day = 31;
450: }
451: } else if (month == 2 ) {
452: if ((year % 4 == 0) && (year % 100 != 0)) {
453: if (day > 29) {
454: document.$formname.$dname\_day.value = 29;
455: }
456: } else if (day > 29) {
457: document.$formname.$dname\_day.value = 28;
458: }
459: } else if (day > 30) {
460: document.$formname.$dname\_day.value = 30;
461: }
462: }
463:
464: function $dname\_disable() {
465: document.$formname.$dname\_month.disabled=true;
466: document.$formname.$dname\_day.disabled=true;
467: document.$formname.$dname\_year.disabled=true;
468: document.$formname.$dname\_hour.disabled=true;
469: document.$formname.$dname\_minute.disabled=true;
470: document.$formname.$dname\_second.disabled=true;
471: }
472:
473: function $dname\_enable() {
474: document.$formname.$dname\_month.disabled=false;
475: document.$formname.$dname\_day.disabled=false;
476: document.$formname.$dname\_year.disabled=false;
477: document.$formname.$dname\_hour.disabled=false;
478: document.$formname.$dname\_minute.disabled=false;
479: document.$formname.$dname\_second.disabled=false;
480: }
481:
482: function $dname\_opencalendar() {
483: if (! document.$formname.$dname\_month.disabled) {
484: var calwin=window.open(
485: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
486: document.$formname.$dname\_month.value+"&year="+
487: document.$formname.$dname\_year.value,
488: "LONCAPAcal",
489: "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
490: }
491:
492: }
493: </script>
494: ENDJS
495: $result .= ' <span class="LC_nobreak">';
496: my $monthselector = qq{<select name="$dname\_month" $special $state onchange="javascript:$dname\_checkday()" >};
497: # Month
498: my @Months = qw/January February March April May June
499: July August September October November December/;
500: # Pad @Months with a bogus value to make indexing easier
501: unshift(@Months,'If you can read this an error occurred');
502: if ($includeempty) { $monthselector.="<option value=''></option>"; }
503: for(my $m = 1;$m <=$#Months;$m++) {
504: $monthselector .= qq{ <option value="$m" };
505: $monthselector .= "selected " if ($m-1 eq $month);
506: $monthselector .= '> '.&mt($Months[$m]).' </option>';
507: }
508: $monthselector.= ' </select>';
509: # Day
510: my $dayselector = qq{<input type="text" name="$dname\_day" $state value="$mday" size="3" $special onchange="javascript:$dname\_checkday()" />};
511: # Year
512: my $yearselector = qq{<input type="year" name="$dname\_year" $state value="$year" size="5" $special onchange="javascript:$dname\_checkday()" />};
513: #
514: my $hourselector = qq{<select name="$dname\_hour" $special $state >};
515: if ($includeempty) {
516: $hourselector.=qq{<option value=''></option>};
517: }
518: for (my $h = 0;$h<24;$h++) {
519: $hourselector .= qq{<option value="$h" };
520: $hourselector .= "selected " if (defined($hour) && $hour == $h);
521: $hourselector .= ">";
522: my $timest='';
523: if ($h == 0) {
524: $timest .= "12 am";
525: } elsif($h == 12) {
526: $timest .= "12 noon";
527: } elsif($h < 12) {
528: $timest .= "$h am";
529: } else {
530: $timest .= $h-12 ." pm";
531: }
532: $timest=&mt($timest);
533: $hourselector .= $timest." </option>\n";
534: }
535: $hourselector .= " </select>\n";
536: my $minuteselector = qq{<input type="text" name="$dname\_minute" $special $state value="$min" size="3" />};
537: my $secondselector= qq{<input type="text" name="$dname\_second" $special $state value="$sec" size="3" />};
538: my $cal_link;
539: if (!$nolink) {
540: $cal_link = qq{<a href="javascript:$dname\_opencalendar()">};
541: }
542: #
543: my $tzone = ' '.$tzname.' ';
544: if ($no_hh_mm_ss) {
545: $result .= &mt('[_1] [_2] [_3] ',
546: $monthselector,$dayselector,$yearselector).
547: $tzone;
548: if (!$nolink) {
549: $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
550: }
551: } else {
552: $result .= &mt('[_1] [_2] [_3] [_4] [_5]m [_6]s ',
553: $monthselector,$dayselector,$yearselector,
554: $hourselector,$minuteselector,$secondselector).
555: $tzone;
556: if (!$nolink) {
557: $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
558: }
559: }
560: $result .= "</span>\n<!-- end $dname date setting form -->\n";
561: return $result;
562: }
563:
564: sub get_timedates {
565: my ($epoch) = @_;
566: my $dt = DateTime->from_epoch(epoch => $epoch)
567: ->set_time_zone(&Apache::lonlocal::gettimezone());
568: my $tzname = $dt->time_zone_short_name();
569: my $sec = $dt->second;
570: my $min = $dt->minute;
571: my $hour = $dt->hour;
572: my $mday = $dt->day;
573: my $month = $dt->month;
574: if ($month) {
575: $month --;
576: }
577: my $year = $dt->year;
578: return ($tzname,$sec,$min,$hour,$mday,$month,$year);
579: }
580:
581: sub build_url {
582: my ($base, $fields)=@_;
583: my $url;
584: $url = $base.'?';
585: foreach my $key (keys(%$fields)) {
586: $url.=&escape($key).'='.&escape($$fields{$key}).'&';
587: }
588: $url =~ s/&$//;
589: return $url;
590: }
591:
592:
593: ##############################################
594: ##############################################
595:
596: =pod
597:
598: =item &get_date_from_form
599:
600: get_date_from_form retrieves the date specified in an &date_setter form.
601:
602: Inputs:
603:
604: =over 4
605:
606: =item $dname
607:
608: The name passed to &datesetter, which prefixes the form elements.
609:
610: =item $defaulttime
611:
612: The unix time to use as the default in case of poor inputs.
613:
614: =back
615:
616: Returns: Unix time represented in the form.
617:
618: =cut
619:
620: ##############################################
621: ##############################################
622: sub get_date_from_form {
623: my ($dname) = @_;
624: my ($sec,$min,$hour,$day,$month,$year);
625: #
626: if (defined($env{'form.'.$dname.'_second'})) {
627: my $tmpsec = $env{'form.'.$dname.'_second'};
628: if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
629: $sec = $tmpsec;
630: }
631: if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
632: } else {
633: $sec = 0;
634: }
635: if (defined($env{'form.'.$dname.'_minute'})) {
636: my $tmpmin = $env{'form.'.$dname.'_minute'};
637: if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
638: $min = $tmpmin;
639: }
640: if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
641: } else {
642: $min = 0;
643: }
644: if (defined($env{'form.'.$dname.'_hour'})) {
645: my $tmphour = $env{'form.'.$dname.'_hour'};
646: if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
647: $hour = $tmphour;
648: }
649: } else {
650: $hour = 0;
651: }
652: if (defined($env{'form.'.$dname.'_day'})) {
653: my $tmpday = $env{'form.'.$dname.'_day'};
654: if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
655: $day = $tmpday;
656: }
657: }
658: if (defined($env{'form.'.$dname.'_month'})) {
659: my $tmpmonth = $env{'form.'.$dname.'_month'};
660: if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
661: $month = $tmpmonth;
662: }
663: }
664: if (defined($env{'form.'.$dname.'_year'})) {
665: my $tmpyear = $env{'form.'.$dname.'_year'};
666: if (($tmpyear =~ /^\d+$/) && ($tmpyear >= 1970)) {
667: $year = $tmpyear;
668: }
669: }
670: if (($year<1970) || ($year>2037)) { return undef; }
671: if (defined($sec) && defined($min) && defined($hour) &&
672: defined($day) && defined($month) && defined($year)) {
673: my $timezone = &Apache::lonlocal::gettimezone();
674: my $dt = DateTime->new( year => $year,
675: month => $month,
676: day => $day,
677: hour => $hour,
678: minute => $min,
679: second => $sec,
680: time_zone => $timezone,
681: );
682: my $epoch_time = $dt->epoch;
683: if ($epoch_time ne '') {
684: return $epoch_time;
685: } else {
686: return undef;
687: }
688: } else {
689: return undef;
690: }
691: }
692:
693: ##############################################
694: ##############################################
695:
696: =pod
697:
698: =item &pjump_javascript_definition()
699:
700: Returns javascript defining the 'pjump' function, which opens up a
701: parameter setting wizard.
702:
703: =cut
704:
705: ##############################################
706: ##############################################
707: sub pjump_javascript_definition {
708: my $Str = <<END;
709: function pjump(type,dis,value,marker,ret,call,hour,min,sec) {
710: parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
711: +"&value="+escape(value)+"&marker="+escape(marker)
712: +"&return="+escape(ret)
713: +"&call="+escape(call)+"&name="+escape(dis)
714: +"&defhour="+escape(hour)+"&defmin="+escape(min)
715: +"&defsec="+escape(sec),"LONCAPAparms",
716: "height=350,width=350,scrollbars=no,menubar=no");
717: }
718: END
719: return $Str;
720: }
721:
722: ##############################################
723: ##############################################
724:
725: =pod
726:
727: =item &javascript_nothing()
728:
729: Return an appropriate null for the users browser. This is used
730: as the first arguement for window.open calls when you want a blank
731: window that you can then write to.
732:
733: =cut
734:
735: ##############################################
736: ##############################################
737: sub javascript_nothing {
738: # mozilla and other browsers work with "''", but IE on mac does not.
739: my $nothing = "''";
740: my $user_browser;
741: my $user_os;
742: $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
743: $user_os = $env{'browser.os'} if (exists($env{'browser.os'}));
744: if (! defined($user_browser) || ! defined($user_os)) {
745: (undef,$user_browser,undef,undef,undef,$user_os) =
746: &Apache::loncommon::decode_user_agent();
747: }
748: if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
749: $nothing = "'javascript:void(0);'";
750: }
751: return $nothing;
752: }
753:
754: ##############################################
755: ##############################################
756: sub javascript_docopen {
757: my ($mimetype) = @_;
758: $mimetype ||= 'text/html';
759: # safari does not understand document.open() and loads "text/html"
760: my $nothing = "''";
761: my $user_browser;
762: my $user_os;
763: $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
764: $user_os = $env{'browser.os'} if (exists($env{'browser.os'}));
765: if (! defined($user_browser) || ! defined($user_os)) {
766: (undef,$user_browser,undef,undef,undef,$user_os) =
767: &Apache::loncommon::decode_user_agent();
768: }
769: if ($user_browser eq 'safari' && $user_os =~ 'mac') {
770: $nothing = "document.clear()";
771: } else {
772: $nothing = "document.open('$mimetype','replace')";
773: }
774: return $nothing;
775: }
776:
777:
778: ##############################################
779: ##############################################
780:
781: =pod
782:
783: =item &StatusOptions()
784:
785: Returns html for a selection box which allows the user to choose the
786: enrollment status of students. The selection box name is 'Status'.
787:
788: Inputs:
789:
790: $status: the currently selected status. If undefined the value of
791: $env{'form.Status'} is taken. If that is undefined, a value of 'Active'
792: is used.
793:
794: $formname: The name of the form. If defined the onchange attribute of
795: the selection box is set to document.$formname.submit().
796:
797: $size: the size (number of lines) of the selection box.
798:
799: $onchange: javascript to use when the value is changed. Enclosed in
800: double quotes, ""s, not single quotes.
801:
802: Returns: a perl string as described.
803:
804: =cut
805:
806: ##############################################
807: ##############################################
808: sub StatusOptions {
809: my ($status, $formName,$size,$onchange,$mult)=@_;
810: $size = 1 if (!defined($size));
811: if (! defined($status)) {
812: $status = 'Active';
813: $status = $env{'form.Status'} if (exists($env{'form.Status'}));
814: }
815:
816: my $Str = '';
817: $Str .= '<select name="Status"';
818: if (defined($mult)){
819: $Str .= ' multiple="multiple" ';
820: }
821: if(defined($formName) && $formName ne '' && ! defined($onchange)) {
822: $Str .= ' onchange="document.'.$formName.'.submit()"';
823: }
824: if (defined($onchange)) {
825: $Str .= ' onchange="'.$onchange.'"';
826: }
827: $Str .= ' size="'.$size.'" ';
828: $Str .= '>'."\n";
829: foreach my $type (['Active', &mt('Currently Has Access')],
830: ['Future', &mt('Will Have Future Access')],
831: ['Expired', &mt('Previously Had Access')],
832: ['Any', &mt('Any Access Status')]) {
833: my ($name,$label) = @$type;
834: $Str .= '<option value="'.$name.'" ';
835: if ($status eq $name) {
836: $Str .= 'selected="selected" ';
837: }
838: $Str .= '>'.$label.'</option>'."\n";
839: }
840:
841: $Str .= '</select>'."\n";
842: }
843:
844: ########################################################
845: ########################################################
846:
847: =pod
848:
849: =item Progess Window Handling Routines
850:
851: These routines handle the creation, update, increment, and closure of
852: progress windows. The progress window reports to the user the number
853: of items completed and an estimate of the time required to complete the rest.
854:
855: =over 4
856:
857:
858: =item &Create_PrgWin
859:
860: Writes javascript to the client to open a progress window and returns a
861: data structure used for bookkeeping.
862:
863: Inputs
864:
865: =over 4
866:
867: =item $r Apache request
868:
869: =item $title The title of the progress window
870:
871: =item $heading A description (usually 1 line) of the process being initiated.
872:
873: =item $number_to_do The total number of items being processed.
874:
875: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
876: specified)
877:
878: =item $width Specify the width in charaters of the input field.
879:
880: =item $formname Only useful in the inline case, if a form already exists, this needs to be used and specfiy the name of the form, otherwise the Progress line will be created in a new form of it's own
881:
882: =item $inputname Only useful in the inline case, if a form and an input of type text exists, use this to specify the name of the input field
883:
884: =back
885:
886: Returns a hash containing the progress state data structure.
887:
888:
889: =item &Update_PrgWin
890:
891: Updates the text in the progress indicator. Does not increment the count.
892: See &Increment_PrgWin.
893:
894: Inputs:
895:
896: =over 4
897:
898: =item $r Apache request
899:
900: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
901:
902: =item $displaystring The string to write to the status indicator
903:
904: =back
905:
906: Returns: none
907:
908:
909: =item Increment_PrgWin
910:
911: Increment the count of items completed for the progress window by 1.
912:
913: Inputs:
914:
915: =over 4
916:
917: =item $r Apache request
918:
919: =item $prog_state Pointer to the data structure returned by Create_PrgWin
920:
921: =item $extraInfo A description of the items being iterated over. Typically
922: 'student'.
923:
924: =back
925:
926: Returns: none
927:
928:
929: =item Close_PrgWin
930:
931: Closes the progress window.
932:
933: Inputs:
934:
935: =over 4
936:
937: =item $r Apache request
938:
939: =item $prog_state Pointer to the data structure returned by Create_PrgWin
940:
941: =back
942:
943: Returns: none
944:
945: =back
946:
947: =cut
948:
949: ########################################################
950: ########################################################
951:
952: my $uniq=0;
953: sub get_uniq_name {
954: $uniq++;
955: return 'uniquename'.$uniq;
956: }
957:
958: # Create progress
959: sub Create_PrgWin {
960: my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
961: $inputname)=@_;
962: if (!defined($type)) { $type='popup'; }
963: if (!defined($width)) { $width=55; }
964: my %prog_state;
965: $prog_state{'type'}=$type;
966: if ($type eq 'popup') {
967: $prog_state{'window'}='popwin';
968: my $start_page =
969: &Apache::loncommon::start_page($title,undef,
970: {'only_body' => 1,
971: 'bgcolor' => '#88DDFF',
972: 'js_ready' => 1});
973: my $end_page = &Apache::loncommon::end_page({'js_ready' => 1});
974:
975: #the whole function called through timeout is due to issues
976: #in mozilla Read BUG #2665 if you want to know the whole story
977: &r_print($r,'<script type="text/javascript">'.
978: "var popwin;
979: function openpopwin () {
980: popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
981: "popwin.document.writeln(\'".$start_page.
982: "<h4>".&mt("$heading")."<\/h4>".
983: "<form action=\"\" name=\"popremain\" method=\"post\">".
984: '<input type="text" size="'.$width.'" name="remaining" value="'.
985: &mt('Starting').'" /><\\/form>'.$end_page.
986: "\');".
987: "popwin.document.close();}".
988: "\nwindow.setTimeout(openpopwin,0)</script>");
989: $prog_state{'formname'}='popremain';
990: $prog_state{'inputname'}="remaining";
991: } elsif ($type eq 'inline') {
992: $prog_state{'window'}='window';
993: if (!$formname) {
994: $prog_state{'formname'}=&get_uniq_name();
995: &r_print($r,'<form action="" name="'.$prog_state{'formname'}.'">');
996: } else {
997: $prog_state{'formname'}=$formname;
998: }
999: if (!$inputname) {
1000: $prog_state{'inputname'}=&get_uniq_name();
1001: &r_print($r,&mt("$heading [_1]",' <input type="text" name="'.$prog_state{'inputname'}.'" size="'.$width.'" />'));
1002: } else {
1003: $prog_state{'inputname'}=$inputname;
1004:
1005: }
1006: if (!$formname) { &r_print($r,'</form>'); }
1007: &Update_PrgWin($r,\%prog_state,&mt('Starting'));
1008: }
1009:
1010: $prog_state{'done'}=0;
1011: $prog_state{'firststart'}=&Time::HiRes::time();
1012: $prog_state{'laststart'}=&Time::HiRes::time();
1013: $prog_state{'max'}=$number_to_do;
1014:
1015: return %prog_state;
1016: }
1017:
1018: # update progress
1019: sub Update_PrgWin {
1020: my ($r,$prog_state,$displayString)=@_;
1021: &r_print($r,'<script type="text/javascript">'.$$prog_state{'window'}.'.document.'.
1022: $$prog_state{'formname'}.'.'.
1023: $$prog_state{'inputname'}.'.value="'.
1024: $displayString.'";</script>');
1025: $$prog_state{'laststart'}=&Time::HiRes::time();
1026: }
1027:
1028: # increment progress state
1029: sub Increment_PrgWin {
1030: my ($r,$prog_state,$extraInfo)=@_;
1031: $$prog_state{'done'}++;
1032: my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
1033: $$prog_state{'done'} *
1034: ($$prog_state{'max'}-$$prog_state{'done'});
1035: $time_est = int($time_est);
1036: #
1037: my $min = int($time_est/60);
1038: my $sec = $time_est % 60;
1039: #
1040: my $str;
1041: if ($min == 0 && $sec > 1) {
1042: $str = '[_2] seconds';
1043: } elsif ($min == 1 && $sec > 1) {
1044: $str = '1 minute [_2] seconds';
1045: } elsif ($min == 1 && $sec < 2) {
1046: $str = '1 minute';
1047: } elsif ($min < 10 && $sec > 1) {
1048: $str = '[_1] minutes, [_2] seconds';
1049: } elsif ($min >= 10 || $sec < 2) {
1050: $str = '[_1] minutes';
1051: }
1052: $time_est = &mt($str,$min,$sec);
1053: #
1054: my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
1055: if ($lasttime > 9) {
1056: $lasttime = int($lasttime);
1057: } elsif ($lasttime < 0.01) {
1058: $lasttime = 0;
1059: } else {
1060: $lasttime = sprintf("%3.2f",$lasttime);
1061: }
1062: if ($lasttime == 1) {
1063: $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
1064: } else {
1065: $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
1066: }
1067: #
1068: my $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
1069: my $user_os = $env{'browser.os'} if (exists($env{'browser.os'}));
1070: if (! defined($user_browser) || ! defined($user_os)) {
1071: (undef,$user_browser,undef,undef,undef,$user_os) =
1072: &Apache::loncommon::decode_user_agent();
1073: }
1074: if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
1075: $lasttime = '';
1076: }
1077: &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
1078: $$prog_state{'formname'}.'.'.
1079: $$prog_state{'inputname'}.'.value="'.
1080: $$prog_state{'done'}.'/'.$$prog_state{'max'}.
1081: ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
1082: $$prog_state{'laststart'}=&Time::HiRes::time();
1083: }
1084:
1085: # close Progress Line
1086: sub Close_PrgWin {
1087: my ($r,$prog_state)=@_;
1088: if ($$prog_state{'type'} eq 'popup') {
1089: &r_print($r,'<script>popwin.close()</script>'."\n");
1090: } elsif ($$prog_state{'type'} eq 'inline') {
1091: &Update_PrgWin($r,$prog_state,&mt('Done'));
1092: }
1093: undef(%$prog_state);
1094: }
1095:
1096: sub r_print {
1097: my ($r,$to_print)=@_;
1098: if ($r) {
1099: $r->print($to_print);
1100: $r->rflush();
1101: } else {
1102: print($to_print);
1103: }
1104: }
1105:
1106: # ------------------------------------------------------- Puts directory header
1107:
1108: sub crumbs {
1109: my ($uri,$target,$prefix,$form,$size,$noformat,$skiplast)=@_;
1110: if (! defined($size)) {
1111: $size = '+2';
1112: }
1113: if ($target) {
1114: $target = ' target="'.
1115: &Apache::loncommon::escape_single($target).'"';
1116: }
1117: my $output='';
1118: unless ($noformat) { $output.='<br /><tt><b>'; }
1119: $output.='<font size="'.$size.'">'.$prefix.'/';
1120: if ($env{'user.adv'}) {
1121: my $path=$prefix.'/';
1122: foreach my $dir (split('/',$uri)) {
1123: if (! $dir) { next; }
1124: $path .= $dir;
1125: if ($path eq $uri) {
1126: if ($skiplast) {
1127: $output.=$dir;
1128: last;
1129: }
1130: } else {
1131: $path.='/';
1132: }
1133: my $href_path = &HTML::Entities::encode($path,'<>&"');
1134: &Apache::loncommon::inhibit_menu_check(\$href_path);
1135: if ($form) {
1136: my $href = 'javascript:'.$form.".action='".$href_path."';".$form.'.submit();';
1137: $output.=qq{<a href="$href" $target>$dir</a>/};
1138: } else {
1139: $output.=qq{<a href="$href_path" $target>$dir</a>/};
1140: }
1141: }
1142: } else {
1143: foreach my $dir (split('/',$uri)) {
1144: if (! $dir) { next; }
1145: $output.=$dir.'/';
1146: }
1147: }
1148: if ($uri !~ m|/$|) { $output=~s|/$||; }
1149: return $output.'</font>'.($noformat?'':'</b></tt><br />');
1150: }
1151:
1152: # --------------------- A function that generates a window for the spellchecker
1153:
1154: sub spellheader {
1155: my $start_page=
1156: &Apache::loncommon::start_page('Speller Suggestions',undef,
1157: {'only_body' => 1,
1158: 'js_ready' => 1,
1159: 'bgcolor' => '#DDDDDD',
1160: 'add_entries' => {
1161: 'onload' =>
1162: 'document.forms.spellcheckform.submit()',
1163: }
1164: });
1165: my $end_page=
1166: &Apache::loncommon::end_page({'js_ready' => 1});
1167:
1168: my $nothing=&javascript_nothing();
1169: return (<<ENDCHECK);
1170: <script type="text/javascript">
1171: //<!-- BEGIN LON-CAPA Internal
1172: var checkwin;
1173:
1174: function spellcheckerwindow(string) {
1175: var esc_string = string.replace(/\"/g,'"');
1176: checkwin=window.open($nothing,'spellcheckwin','height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
1177: checkwin.document.writeln('$start_page<form name="spellcheckform" action="/adm/spellcheck" method="post"><input type="hidden" name="text" value="'+esc_string+'" /><\\/form>$end_page');
1178: checkwin.document.close();
1179: }
1180: // END LON-CAPA Internal -->
1181: </script>
1182: ENDCHECK
1183: }
1184:
1185: # ---------------------------------- Generate link to spell checker for a field
1186:
1187: sub spelllink {
1188: my ($form,$field)=@_;
1189: my $linktext=&mt('Check Spelling');
1190: return (<<ENDLINK);
1191: <a href="javascript:if (typeof(document.$form.onsubmit)!='undefined') { if (document.$form.onsubmit!=null) { document.$form.onsubmit();}};spellcheckerwindow(this.document.forms.$form.$field.value);">$linktext</a>
1192: ENDLINK
1193: }
1194:
1195: # ------------------------------------------------- Output headers for HTMLArea
1196:
1197: {
1198: my @htmlareafields;
1199: sub init_htmlareafields {
1200: undef(@htmlareafields);
1201: }
1202:
1203: sub add_htmlareafields {
1204: my (@newfields) = @_;
1205: push(@htmlareafields,@newfields);
1206: }
1207:
1208: sub get_htmlareafields {
1209: return @htmlareafields;
1210: }
1211: }
1212:
1213: sub htmlareaheaders {
1214: return if (&htmlareablocked());
1215: return if (!&htmlareabrowser());
1216: return (<<ENDHEADERS);
1217: <script type="text/javascript" src="/fckeditor/fckeditor.js"></script>
1218: ENDHEADERS
1219: }
1220:
1221: # ----------------------------------------------------------------- Preferences
1222:
1223: sub disablelink {
1224: my @fields=@_;
1225: if (defined($#fields)) {
1226: unless ($#fields>=0) { return ''; }
1227: }
1228: return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=off&returnurl=','<>&"').&escape($ENV{'REQUEST_URI'}).'">'.&mt('Disable WYSIWYG Editor').'</a>';
1229: }
1230:
1231: sub enablelink {
1232: my @fields=@_;
1233: if (defined($#fields)) {
1234: unless ($#fields>=0) { return ''; }
1235: }
1236: return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=on&returnurl=','<>&"').&escape($ENV{'REQUEST_URI'}).'">'.&mt('Enable WYSIWYG Editor').'</a>';
1237: }
1238:
1239: # ------------------------------------------------- lang to use in html editor
1240: sub htmlarea_lang {
1241: my $lang='en';
1242: if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
1243: $lang=&mt('htmlarea_lang');
1244: }
1245: return $lang;
1246: }
1247:
1248: # ----------------------------------------- Script to activate only some fields
1249:
1250: sub htmlareaselectactive {
1251: my @fields=@_;
1252: unless (&htmlareabrowser()) { return ''; }
1253: if (&htmlareablocked()) { return '<br />'.&enablelink(@fields); }
1254: my $output='<script type="text/javascript" defer="1">';
1255: my $lang = &htmlarea_lang();
1256: foreach my $field (@fields) {
1257: $output.="
1258: {
1259: var oFCKeditor = new FCKeditor('$field');
1260: oFCKeditor.Config['CustomConfigurationsPath'] =
1261: '/fckeditor/loncapaconfig.js';
1262: oFCKeditor.ReplaceTextarea();
1263: oFCKeditor.Config['AutoDetectLanguage'] = false;
1264: oFCKeditor.Config['DefaultLanguage'] = '$lang';
1265: }";
1266: }
1267: $output.="\nwindow.status='Activated Editfields';\n</script><br />".
1268: &disablelink(@fields);
1269: return $output;
1270: }
1271:
1272: # --------------------------------------------------------------------- Blocked
1273:
1274: sub htmlareablocked {
1275: unless ($env{'environment.wysiwygeditor'} eq 'on') { return 1; }
1276: return 0;
1277: }
1278:
1279: # ---------------------------------------- Browser capable of running HTMLArea?
1280:
1281: sub htmlareabrowser {
1282: return 1;
1283: }
1284:
1285: ############################################################
1286: ############################################################
1287:
1288: =pod
1289:
1290: =item breadcrumbs
1291:
1292: Compiles the previously registered breadcrumbs into an series of links.
1293: FAQ and BUG links will be placed on the left side of the table if they
1294: are defined for the last registered breadcrumb.
1295: Additionally supports a 'component', which will be displayed on the
1296: right side of the table (without a link).
1297: A link to help for the component will be included if one is specified.
1298:
1299: All inputs can be undef without problems.
1300:
1301: Inputs: $component (the large text on the right side of the table),
1302: $component_help
1303: $menulink (boolean, controls whether to include a link to /adm/menu)
1304: $helplink (if 'nohelp' don't include the orange help link)
1305: $css_class (optional name for the class to apply to the table for CSS)
1306: $no_mt (optional flag, 1 if &mt() is _not_ to be applied to $component
1307: when including the text on the right.
1308: Returns a string containing breadcrumbs for the current page.
1309:
1310: =item clear_breadcrumbs
1311:
1312: Clears the previously stored breadcrumbs.
1313:
1314: =item add_breadcrumb
1315:
1316: Pushes a breadcrumb on the stack of crumbs.
1317:
1318: input: $breadcrumb, a hash reference. The keys 'href','title', and 'text'
1319: are required. If present the keys 'faq' and 'bug' will be used to provide
1320: links to the FAQ and bug sites. If the key 'no_mt' is present the 'title'
1321: and 'text' values won't be sent through &mt()
1322:
1323: returns: nothing
1324:
1325: =cut
1326:
1327: ############################################################
1328: ############################################################
1329: {
1330: my @Crumbs;
1331:
1332: sub breadcrumbs {
1333: my ($component,$component_help,$menulink,$helplink,$css_class,$no_mt, $CourseBreadcrumbs) = @_;
1334: #
1335: $css_class ||= 'LC_breadcrumbs';
1336: my $Str1 = '<div id="LC_head_subbox"><ol id="LC_MenuBreadcrumbs">';
1337:
1338: if($CourseBreadcrumbs){
1339: $Str1 = '<ul id="LC_CourseBreadcrumbs">';
1340: }
1341:
1342: my $Str = '';
1343: #
1344: # Make the faq and bug data cascade
1345: my $faq = '';
1346: my $bug = '';
1347: my $help='';
1348: # Crumb Symbol
1349: my $crumbsymbol = '» ';
1350: # The last breadcrumb does not have a link, so handle it separately.
1351: my $last = pop(@Crumbs);
1352: #
1353: # The first one should be the course or a menu link
1354: if (!defined($menulink)) { $menulink=1; }
1355: if ($menulink) {
1356: my $description = 'Menu';
1357: my $no_mt_descr = 0;
1358: if (exists($env{'request.course.id'}) &&
1359: $env{'request.course.id'} ne '') {
1360: $description =
1361: $env{'course.'.$env{'request.course.id'}.'.description'};
1362: $no_mt_descr = 1;
1363: }
1364: $menulink = { href =>'/adm/menu',
1365: title =>'Go to main menu',
1366: target =>'_top',
1367: text =>$description,
1368: no_mt =>$no_mt_descr, };
1369: if($last) {
1370: #$last set, so we have some crumbs
1371: unshift(@Crumbs,$menulink);
1372: } else {
1373: #only menulink crumb present
1374: $last = $menulink;
1375: }
1376: }
1377: my $links .= '<li>'.
1378: join('</li><li>'.$crumbsymbol,
1379: map {
1380: $faq = $_->{'faq'} if (exists($_->{'faq'}));
1381: $bug = $_->{'bug'} if (exists($_->{'bug'}));
1382: $help = $_->{'help'} if (exists($_->{'help'}));
1383: my $result = '<a href="'.$_->{'href'}.'" ';
1384: if (defined($_->{'target'}) && $_->{'target'} ne '') {
1385: $result .= 'target="'.$_->{'target'}.'" ';
1386: }
1387: # set the possible translation for title
1388: if ($_->{'no_mt'}) {
1389: $result .='title="'.$_->{'title'}.'">'.
1390: $_->{'text'}.'</a>';
1391: } else {
1392: $result .='title="'.&mt($_->{'title'}).'">'.
1393: &mt($_->{'text'}).'</a>';
1394: }
1395: $result;
1396: } @Crumbs
1397: ).'</li>';
1398: #Workaround for edit course.
1399: if(@Crumbs == 0 ){
1400: $links .= '<li>' if ($links ne '');
1401: } else {
1402: $links .= '<li>'.$crumbsymbol if ($links ne '');
1403: }
1404: #should the last Element be translated?
1405: if ($last->{'no_mt'}) {
1406: $links .= '<b>'.$last->{'text'}.'</b>';
1407: } else {
1408: $links .= '<b>'.&mt($last->{'text'}).'</b>';
1409: }
1410: $links .= '</li>';
1411: my $icons = '';
1412: $faq = $last->{'faq'} if (exists($last->{'faq'}));
1413: $bug = $last->{'bug'} if (exists($last->{'bug'}));
1414: $help = $last->{'help'} if (exists($last->{'help'}));
1415: $component_help=($component_help?$component_help:$help);
1416: # if ($faq ne '') {
1417: # $icons .= &Apache::loncommon::help_open_faq($faq);
1418: # }
1419: # if ($bug ne '') {
1420: # $icons .= &Apache::loncommon::help_open_bug($bug);
1421: # }
1422: if ($faq ne '' || $component_help ne '' || $bug ne '') {
1423: $icons .= &Apache::loncommon::help_open_menu($component,
1424: $component_help,
1425: $faq,$bug);
1426: }
1427: #
1428:
1429:
1430: if($CourseBreadcrumbs){
1431: $Str1 .= $links.'</ul>';
1432: } else {
1433: $Str1 .= $links.'</ol></div>';
1434: }
1435: #
1436: if (defined($component)) {
1437: $Str .= "\n".'<table class="'.$css_class.'">'
1438: .'<tr><td class="'.$css_class.'_component">';
1439: if ($no_mt) {
1440: $Str .= $component;
1441: } else {
1442: $Str .= &mt($component);
1443: }
1444: if ($icons ne '') {
1445: $Str .= ' '.$icons;
1446: }
1447: $Str .= '</td></tr></table>'."\n";
1448: }
1449: #
1450: # Return the @Crumbs stack to what we started with
1451: push(@Crumbs,$last);
1452: shift(@Crumbs);
1453: # Return a table and after that the breadcrumb's line
1454: return "$Str\n$Str1";
1455: }
1456:
1457: sub clear_breadcrumbs {
1458: undef(@Crumbs);
1459: }
1460:
1461: sub add_breadcrumb {
1462: push (@Crumbs,@_);
1463: }
1464:
1465: } # End of scope for @Crumbs
1466:
1467: ############################################################
1468: ############################################################
1469:
1470: # Nested table routines.
1471: #
1472: # Routines to display form items in a multi-row table with 2 columns.
1473: # Uses nested tables to divide form elements into segments.
1474: # For examples of use see loncom/interface/lonnotify.pm
1475: #
1476: # Can be used in following order: ...
1477: # &start_pick_box()
1478: # row1
1479: # row2
1480: # row3 ... etc.
1481: # &submit_row()
1482: # &end_pick_box()
1483: #
1484: # where row1, row 2 etc. are chosen from &role_select_row,&course_select_row,
1485: # &status_select_row and &email_default_row
1486: #
1487: # Can also be used in following order:
1488: #
1489: # &start_pick_box()
1490: # &row_title()
1491: # &row_closure()
1492: # &row_title()
1493: # &row_closure() ... etc.
1494: # &submit_row()
1495: # &end_pick_box()
1496: #
1497: # In general a &submit_row() call should proceed the call to &end_pick_box(),
1498: # as this routine adds a button for form submission.
1499: # &submit_row() does not require a &row_closure after it.
1500: #
1501: # &start_pick_box() creates a bounding table with 1-pixel wide black border.
1502: # rows should be placed between calls to &start_pick_box() and &end_pick_box.
1503: #
1504: # &row_title() adds a title in the left column for each segment.
1505: # &row_closure() closes a row with a 1-pixel wide black line.
1506: #
1507: # &role_select_row() provides a select box from which to choose 1 or more roles
1508: # &course_select_row provides ways of picking groups of courses
1509: # radio buttons: all, by category or by picking from a course picker pop-up
1510: # note: by category option is only displayed if a domain has implemented
1511: # selection by year, semester, department, number etc.
1512: #
1513: # &status_select_row() provides a select box from which to choose 1 or more
1514: # access types (current access, prior access, and future access)
1515: #
1516: # &email_default_row() provides text boxes for default e-mail suffixes for
1517: # different authentication types in a domain.
1518: #
1519: # &row_title() and &row_closure() are called internally by the &*_select_row
1520: # routines, but can also be called directly to start and end rows which have
1521: # needs that are not accommodated by the *_select_row() routines.
1522:
1523: { # Start: row_count block for pick_box
1524: my @row_count;
1525:
1526: sub start_pick_box {
1527: my ($css_class) = @_;
1528: if (defined($css_class)) {
1529: $css_class = 'class="'.$css_class.'"';
1530: } else {
1531: $css_class= 'class="LC_pick_box"';
1532: }
1533: unshift(@row_count,0);
1534: my $output = <<"END";
1535: <table $css_class>
1536: END
1537: return $output;
1538: }
1539:
1540: sub end_pick_box {
1541: shift(@row_count);
1542: my $output = <<"END";
1543: </table>
1544: END
1545: return $output;
1546: }
1547:
1548: sub row_headline {
1549: my $output = <<"END";
1550: <tr><td colspan="2">
1551: END
1552: return $output;
1553: }
1554:
1555: sub row_title {
1556: my ($title,$css_title_class,$css_value_class) = @_;
1557: $row_count[0]++;
1558: my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1559: $css_title_class ||= 'LC_pick_box_title';
1560: $css_title_class = 'class="'.$css_title_class.'"';
1561:
1562: $css_value_class ||= 'LC_pick_box_value';
1563:
1564: if ($title ne '') {
1565: $title .= ':';
1566: }
1567: my $output = <<"ENDONE";
1568: <tr class="LC_pick_box_row">
1569: <td $css_title_class>
1570: $title
1571: </td>
1572: <td class="$css_value_class $css_class">
1573: ENDONE
1574: return $output;
1575: }
1576:
1577: sub row_closure {
1578: my ($no_separator) =@_;
1579: my $output = <<"ENDTWO";
1580: </td>
1581: </tr>
1582: ENDTWO
1583: if (!$no_separator) {
1584: $output .= <<"ENDTWO";
1585: <tr>
1586: <td colspan="2" class="LC_pick_box_separator">
1587: </td>
1588: </tr>
1589: ENDTWO
1590: }
1591: return $output;
1592: }
1593:
1594: } # End: row_count block for pick_box
1595:
1596:
1597: sub role_select_row {
1598: my ($roles,$title,$css_class,$show_separate_custom,$cdom,$cnum) = @_;
1599: my $output;
1600: if (defined($title)) {
1601: $output = &row_title($title,$css_class);
1602: }
1603: $output .= qq|
1604: <select name="roles" multiple="multiple">\n|;
1605: foreach my $role (@$roles) {
1606: my $plrole;
1607: if ($role eq 'ow') {
1608: $plrole = &mt('Course Owner');
1609: } elsif ($role eq 'cr') {
1610: if ($show_separate_custom) {
1611: if ($cdom ne '' && $cnum ne '') {
1612: my %course_customroles = &course_custom_roles($cdom,$cnum);
1613: foreach my $crrole (sort(keys(%course_customroles))) {
1614: my ($plcrrole) = ($crrole =~ m|^cr/[^/]+/[^/]+/(.+)$|);
1615: $output .= ' <option value="'.$crrole.'">'.$plcrrole.
1616: '</option>';
1617: }
1618: }
1619: } else {
1620: $plrole = &mt('Custom Role');
1621: }
1622: } else {
1623: $plrole=&Apache::lonnet::plaintext($role);
1624: }
1625: if (($role ne 'cr') || (!$show_separate_custom)) {
1626: $output .= ' <option value="'.$role.'">'.$plrole.'</option>';
1627: }
1628: }
1629: $output .= qq| </select>\n|;
1630: if (defined($title)) {
1631: $output .= &row_closure();
1632: }
1633: return $output;
1634: }
1635:
1636: sub course_select_row {
1637: my ($title,$formname,$totcodes,$codetitles,$idlist,$idlist_titles,
1638: $css_class) = @_;
1639: my $output = &row_title($title,$css_class);
1640: $output .= &course_selection($formname,$totcodes,$codetitles,$idlist,$idlist_titles);
1641: $output .= &row_closure();
1642: return $output;
1643: }
1644:
1645: sub course_selection {
1646: my ($formname,$totcodes,$codetitles,$idlist,$idlist_titles) = @_;
1647: my $output = qq|
1648: <script type="text/javascript">
1649: function coursePick (formname) {
1650: for (var i=0; i<formname.coursepick.length; i++) {
1651: if (formname.coursepick[i].value == 'category') {
1652: courseSet('');
1653: }
1654: if (!formname.coursepick[i].checked) {
1655: if (formname.coursepick[i].value == 'specific') {
1656: formname.coursetotal.value = 0;
1657: formname.courselist = '';
1658: }
1659: }
1660: }
1661: }
1662: function setPick (formname) {
1663: for (var i=0; i<formname.coursepick.length; i++) {
1664: if (formname.coursepick[i].value == 'category') {
1665: formname.coursepick[i].checked = true;
1666: }
1667: formname.coursetotal.value = 0;
1668: formname.courselist = '';
1669: }
1670: }
1671: </script>
1672: |;
1673: my $courseform='<b>'.&Apache::loncommon::selectcourse_link
1674: ($formname,'pickcourse','pickdomain','coursedesc','',1).'</b>';
1675: $output .= '<input type="radio" name="coursepick" value="all" onclick="coursePick(this.form)" />'.&mt('All courses').'<br />';
1676: if ($totcodes > 0) {
1677: my $numtitles = @$codetitles;
1678: if ($numtitles > 0) {
1679: $output .= '<input type="radio" name="coursepick" value="category" onclick="coursePick(this.form);alert('."'".&mt('Choose categories, from left to right')."'".')" />'.&mt('Pick courses by category:').' <br />';
1680: $output .= '<table><tr><td>'.$$codetitles[0].'<br />'."\n".
1681: '<select name="'.$$codetitles[0].
1682: '" onChange="setPick(this.form);courseSet('."'$$codetitles[0]'".')">'."\n".
1683: ' <option value="-1" />Select'."\n";
1684: my @items = ();
1685: my @longitems = ();
1686: if ($$idlist{$$codetitles[0]} =~ /","/) {
1687: @items = split(/","/,$$idlist{$$codetitles[0]});
1688: } else {
1689: $items[0] = $$idlist{$$codetitles[0]};
1690: }
1691: if (defined($$idlist_titles{$$codetitles[0]})) {
1692: if ($$idlist_titles{$$codetitles[0]} =~ /","/) {
1693: @longitems = split(/","/,$$idlist_titles{$$codetitles[0]});
1694: } else {
1695: $longitems[0] = $$idlist_titles{$$codetitles[0]};
1696: }
1697: for (my $i=0; $i<@longitems; $i++) {
1698: if ($longitems[$i] eq '') {
1699: $longitems[$i] = $items[$i];
1700: }
1701: }
1702: } else {
1703: @longitems = @items;
1704: }
1705: for (my $i=0; $i<@items; $i++) {
1706: $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
1707: }
1708: $output .= '</select></td>';
1709: for (my $i=1; $i<$numtitles; $i++) {
1710: $output .= '<td>'.$$codetitles[$i].'<br />'."\n".
1711: '<select name="'.$$codetitles[$i].
1712: '" onChange="courseSet('."'$$codetitles[$i]'".')">'."\n".
1713: '<option value="-1"><-Pick '.$$codetitles[$i-1].'</option>'."\n".
1714: '</select>'."\n".
1715: '</td>';
1716: }
1717: $output .= '</tr></table><br />';
1718: }
1719: }
1720: $output .= '<input type="radio" name="coursepick" value="specific" onclick="coursePick(this.form);opencrsbrowser('."'".$formname."','dccourse','dcdomain','coursedesc','','1'".')" />'.&mt('Pick specific course(s):').' '.$courseform.' <input type="text" value="0" size="4" name="coursetotal" /><input type="hidden" name="courselist" value="" />selected.<br />'."\n";
1721: return $output;
1722: }
1723:
1724: sub status_select_row {
1725: my ($types,$title,$css_class) = @_;
1726: my $output;
1727: if (defined($title)) {
1728: $output = &row_title($title,$css_class,'LC_pick_box_select');
1729: }
1730: $output .= qq|
1731: <select name="types" multiple="multiple">\n|;
1732: foreach my $status_type (sort(keys(%{$types}))) {
1733: $output .= ' <option value="'.$status_type.'">'.$$types{$status_type}.'</option>';
1734: }
1735: $output .= qq| </select>\n|;
1736: if (defined($title)) {
1737: $output .= &row_closure();
1738: }
1739: return $output;
1740: }
1741:
1742: sub email_default_row {
1743: my ($authtypes,$title,$descrip,$css_class) = @_;
1744: my $output = &row_title($title,$css_class);
1745: $output .= $descrip.
1746: &Apache::loncommon::start_data_table().
1747: &Apache::loncommon::start_data_table_header_row().
1748: '<th>'.&mt('Authentication Method').'</th>'.
1749: '<th align="right">'.&mt('Username -> e-mail conversion').'</th>'."\n".
1750: &Apache::loncommon::end_data_table_header_row();
1751: my $rownum = 0;
1752: foreach my $auth (sort(keys(%{$authtypes}))) {
1753: my ($userentry,$size);
1754: if ($auth =~ /^krb/) {
1755: $userentry = '';
1756: $size = 25;
1757: } else {
1758: $userentry = 'username@';
1759: $size = 15;
1760: }
1761: $output .= &Apache::loncommon::start_data_table_row().
1762: '<td> '.$$authtypes{$auth}.'</td>'.
1763: '<td align="right">'.$userentry.
1764: '<input type="text" name="'.$auth.'" size="'.$size.'" /></td>'.
1765: &Apache::loncommon::end_data_table_row();
1766: }
1767: $output .= &Apache::loncommon::end_data_table();
1768: $output .= &row_closure();
1769: return $output;
1770: }
1771:
1772:
1773: sub submit_row {
1774: my ($title,$cmd,$submit_text,$css_class) = @_;
1775: $submit_text = &mt($submit_text);
1776: my $output = &row_title($title,$css_class,'LC_pick_box_submit');
1777: $output .= qq|
1778: <br />
1779: <input type="hidden" name="command" value="$cmd" />
1780: <input type="submit" value="$submit_text"/>
1781: <br /><br />
1782: \n|;
1783: return $output;
1784: }
1785:
1786: sub course_custom_roles {
1787: my ($cdom,$cnum) = @_;
1788: my %returnhash=();
1789: my %coursepersonnel=&Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
1790: foreach my $person (sort(keys(%coursepersonnel))) {
1791: my ($role) = ($person =~ /^([^:]+):/);
1792: my ($end,$start) = split(/:/,$coursepersonnel{$person});
1793: if ($end == -1 && $start == -1) {
1794: next;
1795: }
1796: if ($role =~ m|^cr/[^/]+/[^/]+/[^/]|) {
1797: $returnhash{$role} ++;
1798: }
1799: }
1800: return %returnhash;
1801: }
1802:
1803:
1804: ##############################################
1805: ##############################################
1806:
1807: # topic_bar
1808: #
1809: # Generates a div containing a numbered (static image) followed by a title
1810: # with a background color defined in the corresponding CSS: LC_topic_bar
1811: #
1812: sub topic_bar {
1813: my ($imgnum,$title) = @_;
1814: return '
1815: <div class="LC_topic_bar">
1816: <img alt="'.&mt('Step [_1]',$imgnum).
1817: ' "src="/res/adm/pages/bl_step'.$imgnum.'.gif" />
1818: <span>'.$title.'</span>
1819: </div>
1820: ';
1821: }
1822:
1823: ##############################################
1824: ##############################################
1825:
1826: # echo_form_input
1827: #
1828: # Generates html markup to add form elements from the referrer page
1829: # as hidden form elements (values encoded) in the new page.
1830: #
1831: # Intended to support two types of use
1832: # (a) to allow backing up to earlier pages in a multi-page
1833: # form submission process using a breadcrumb trail.
1834: #
1835: # (b) to allow the current page to be reloaded with form elements
1836: # set on previous page to remain unchanged. An example would
1837: # be where the a page containing a dynamically-built table of data is
1838: # is to be redisplayed, with only the sort order of the data changed.
1839: #
1840: # Inputs:
1841: # 1. Reference to array of form elements in the submitted form on
1842: # the referrer page which are to be excluded from the echoed elements.
1843: #
1844: # 2. Reference to array of regular expressions, which if matched in the
1845: # name of the form element n the referrer page will be omitted from echo.
1846: #
1847: # Outputs: A scalar containing the html markup for the echoed form
1848: # elements (all as hidden elements, with values encoded).
1849:
1850:
1851: sub echo_form_input {
1852: my ($excluded,$regexps) = @_;
1853: my $output = '';
1854: foreach my $key (keys(%env)) {
1855: if ($key =~ /^form\.(.+)$/) {
1856: my $name = $1;
1857: my $match = 0;
1858: if ((!@{$excluded}) || (!grep/^$name$/,@{$excluded})) {
1859: if (defined($regexps)) {
1860: if (@{$regexps} > 0) {
1861: foreach my $regexp (@{$regexps}) {
1862: if ($name =~ /\Q$regexp\E/) {
1863: $match = 1;
1864: last;
1865: }
1866: }
1867: }
1868: }
1869: if (!$match) {
1870: if (ref($env{$key})) {
1871: foreach my $value (@{$env{$key}}) {
1872: $value = &HTML::Entities::encode($value,'<>&"');
1873: $output .= '<input type="hidden" name="'.$name.
1874: '" value="'.$value.'" />'."\n";
1875: }
1876: } else {
1877: my $value = &HTML::Entities::encode($env{$key},'<>&"');
1878: $output .= '<input type="hidden" name="'.$name.
1879: '" value="'.$value.'" />'."\n";
1880: }
1881: }
1882: }
1883: }
1884: }
1885: return $output;
1886: }
1887:
1888: ##############################################
1889: ##############################################
1890:
1891: # set_form_elements
1892: #
1893: # Generates javascript to set form elements to values based on
1894: # corresponding values for the same form elements when the page was
1895: # previously submitted.
1896: #
1897: # Last submission values are read from hidden form elements in referring
1898: # page which have the same name, i.e., generated by &echo_form_input().
1899: #
1900: # Intended to be called by onload event.
1901: #
1902: # Inputs:
1903: # (a) Reference to hash of echoed form elements to be set.
1904: #
1905: # In the hash, keys are the form element names, and the values are the
1906: # element type (selectbox, radio, checkbox or text -for textbox, textarea or
1907: # hidden).
1908: #
1909: # (b) Optional reference to hash of stored elements to be set.
1910: #
1911: # If the page being displayed is a page which permits modification of
1912: # previously stored data, e.g., the first page in a multi-page submission,
1913: # then if stored is supplied, form elements will be set to the last stored
1914: # values. If user supplied values are also available for the same elements
1915: # these will replace the stored values.
1916: #
1917: # Output:
1918: #
1919: # javascript function - set_form_elements() which sets form elements,
1920: # expects an argument: formname - the name of the form according to
1921: # the DOM, e.g., document.compose
1922:
1923: sub set_form_elements {
1924: my ($elements,$stored) = @_;
1925: my %values;
1926: my $output .= 'function setFormElements(courseForm) {
1927: ';
1928: if (defined($stored)) {
1929: foreach my $name (keys(%{$stored})) {
1930: if (exists($$elements{$name})) {
1931: if (ref($$stored{$name}) eq 'ARRAY') {
1932: $values{$name} = $$stored{$name};
1933: } else {
1934: @{$values{$name}} = ($$stored{$name});
1935: }
1936: }
1937: }
1938: }
1939:
1940: foreach my $key (keys(%env)) {
1941: if ($key =~ /^form\.(.+)$/) {
1942: my $name = $1;
1943: if (exists($$elements{$name})) {
1944: @{$values{$name}} = &Apache::loncommon::get_env_multiple($key);
1945: }
1946: }
1947: }
1948:
1949: foreach my $name (keys(%values)) {
1950: for (my $i=0; $i<@{$values{$name}}; $i++) {
1951: $values{$name}[$i] = &HTML::Entities::decode($values{$name}[$i],'<>&"');
1952: $values{$name}[$i] =~ s/([\r\n\f]+)/\\n/g;
1953: $values{$name}[$i] =~ s/"/\\"/g;
1954: }
1955: if ($$elements{$name} eq 'text') {
1956: my $numvalues = @{$values{$name}};
1957: if ($numvalues > 1) {
1958: my $valuestring = join('","',@{$values{$name}});
1959: $output .= qq|
1960: var textvalues = new Array ("$valuestring");
1961: var total = courseForm.elements['$name'].length;
1962: if (total > $numvalues) {
1963: total = $numvalues;
1964: }
1965: for (var i=0; i<total; i++) {
1966: courseForm.elements['$name']\[i].value = textvalues[i];
1967: }
1968: |;
1969: } else {
1970: $output .= qq|
1971: courseForm.elements['$name'].value = "$values{$name}[0]";
1972: |;
1973: }
1974: } else {
1975: $output .= qq|
1976: var elementLength = courseForm.elements['$name'].length;
1977: if (elementLength==undefined) {
1978: |;
1979: foreach my $value (@{$values{$name}}) {
1980: if ($$elements{$name} eq 'selectbox') {
1981: $output .= qq|
1982: if (courseForm.elements['$name'].options[0].value == "$value") {
1983: courseForm.elements['$name'].options[0].selected = true;
1984: }|;
1985: } elsif (($$elements{$name} eq 'radio') ||
1986: ($$elements{$name} eq 'checkbox')) {
1987: $output .= qq|
1988: if (courseForm.elements['$name'].value == "$value") {
1989: courseForm.elements['$name'].checked = true;
1990: }|;
1991: }
1992: }
1993: $output .= qq|
1994: }
1995: else {
1996: for (var i=0; i<courseForm.elements['$name'].length; i++) {
1997: |;
1998: if ($$elements{$name} eq 'selectbox') {
1999: $output .= qq|
2000: courseForm.elements['$name'].options[i].selected = false;|;
2001: } elsif (($$elements{$name} eq 'radio') ||
2002: ($$elements{$name} eq 'checkbox')) {
2003: $output .= qq|
2004: courseForm.elements['$name']\[i].checked = false;|;
2005: }
2006: $output .= qq|
2007: }
2008: for (var j=0; j<courseForm.elements['$name'].length; j++) {
2009: |;
2010: foreach my $value (@{$values{$name}}) {
2011: if ($$elements{$name} eq 'selectbox') {
2012: $output .= qq|
2013: if (courseForm.elements['$name'].options[j].value == "$value") {
2014: courseForm.elements['$name'].options[j].selected = true;
2015: }|;
2016: } elsif (($$elements{$name} eq 'radio') ||
2017: ($$elements{$name} eq 'checkbox')) {
2018: $output .= qq|
2019: if (courseForm.elements['$name']\[j].value == "$value") {
2020: courseForm.elements['$name']\[j].checked = true;
2021: }|;
2022: }
2023: }
2024: $output .= qq|
2025: }
2026: }
2027: |;
2028: }
2029: }
2030: $output .= "
2031: }\n";
2032: return $output;
2033: }
2034:
2035: ##############################################
2036: ##############################################
2037:
2038: # javascript_valid_email
2039: #
2040: # Generates javascript to validate an e-mail address.
2041: # Returns a javascript function which accetps a form field as argumnent, and
2042: # returns false if field.value does not satisfy two regular expression matches
2043: # for a valid e-mail address. Backwards compatible with old browsers without
2044: # support for javascript RegExp (just checks for @ in field.value in this case).
2045:
2046: sub javascript_valid_email {
2047: my $scripttag .= <<'END';
2048: function validmail(field) {
2049: var str = field.value;
2050: if (window.RegExp) {
2051: var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
2052: var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"; //"
2053: var reg1 = new RegExp(reg1str);
2054: var reg2 = new RegExp(reg2str);
2055: if (!reg1.test(str) && reg2.test(str)) {
2056: return true;
2057: }
2058: return false;
2059: }
2060: else
2061: {
2062: if(str.indexOf("@") >= 0) {
2063: return true;
2064: }
2065: return false;
2066: }
2067: }
2068: END
2069: return $scripttag;
2070: }
2071:
2072: ##############################################
2073: ##############################################
2074:
2075: # generate_menu
2076: #
2077: # Generates html markup for a menu.
2078: #
2079: # Inputs:
2080: # An array of following structure:
2081: # ({ categorytitle => 'Categorytitle',
2082: # items => [
2083: # {
2084: # linktext => 'Text to be displayed',
2085: # url => 'URL the link is pointing to, i.e. /adm/site?action=dosomething',
2086: # permission => 'Contains permissions as returned from lonnet::allowed(),
2087: # must evaluate to true in order to activate the link',
2088: # icon => 'icon filename',
2089: # alttext => 'alt text for the icon',
2090: # help => 'Name of the corresponding helpfile',
2091: # linktitle => 'Description of the link (used for title tag)'
2092: # },
2093: # ...
2094: # ]
2095: # },
2096: # ...
2097: # )
2098: #
2099: # Outputs: A scalar containing the html markup for the menu.
2100:
2101: # ---- Remove when done ----
2102: # This routine is part of the redesign of LON-CAPA and it's
2103: # subject to change during this project.
2104: # Don't rely on its current functionality as it might be
2105: # changed or removed.
2106: # --------------------------
2107: sub generate_menu {
2108: my @menu = @_;
2109:
2110: # usage: $wrap->(element, content, {attribute => value,...});
2111: # output: content enclosed in html conform tags
2112: my $wrap = sub {
2113: return
2114: qq|<$_[0]|
2115: . join( '', map { qq| $_="${$_[2]}{$_}"| } keys %{ $_[2] } )
2116: . ($_[1] ? qq|>$_[1]</$_[0]>| : qq|/>|). "\n";
2117: };
2118:
2119: # subs for specific html elements
2120: my $h3 = sub { return $wrap->( "h3", @_ ) };
2121: my $div = sub { return $wrap->( "div", @_ ) };
2122: my $ul = sub { return $wrap->( "ul", @_ ) };
2123: my $li = sub { return $wrap->( "li", @_ ) };
2124: my $a = sub { return $wrap->( "a", @_ ) };
2125: my $img = sub { return $wrap->( "img", @_ ) };
2126:
2127: my @categories; # each element represents the entire markup for a category
2128:
2129: foreach my $category (@menu) {
2130: my @links; # contains the links for the current $category
2131: foreach my $link (@{$$category{items}}) {
2132: next unless $$link{permission};
2133:
2134: # create the markup for the current $link and push it into @links.
2135: # each entry consists of an image and a text optionally followed
2136: # by a help link.
2137: push @links, $li->(
2138: $a->(
2139: $img->("", {
2140: class => "LC_noBorder LC_middle",
2141: src => "/res/adm/pages/$$link{icon}",
2142: alt => mt(defined($$link{alttext}) ?
2143: $$link{alttext} : $$link{linktext})
2144: }), {
2145: href => $$link{url},
2146: title => mt($$link{linktitle})
2147: }).
2148: $a->(mt($$link{linktext}), {
2149: href => $$link{url},
2150: title => mt($$link{linktitle}),
2151: class => "LC_menubuttons_link"
2152: }).
2153: (defined($$link{help}) ?
2154: Apache::loncommon::help_open_topic($$link{help}) : ''),
2155: {class => "LC_menubuttons_inline_text"});
2156: }
2157:
2158: # wrap categorytitle in <h3>, concatenate with
2159: # joined and in <ul> tags wrapped @links
2160: # and wrap everything in an enclosing <div> and push it into
2161: # @categories
2162: # such that each element looks like:
2163: # <div><h3>title</h3><ul><li>...</li>...</ul></div>
2164: # the category won't be added if there aren't any links
2165: push @categories,
2166: $div->($h3->(mt($$category{categorytitle}), {class=>"LC_hcell"}).
2167: $ul->(join('' ,@links), {class =>"LC_ListStyleNormal" }),
2168: {class=>"LC_ContentBoxSpecial LC_400Box"}) if scalar(@links);
2169: }
2170:
2171: # wrap the joined @categories in another <div> (column layout)
2172: return $div->(join('', @categories), {class => "LC_columnSection"});
2173: }
2174:
2175: 1;
2176:
2177: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>