File:
[LON-CAPA] /
loncom /
interface /
lonhtmlcommon.pm
Revision
1.211:
download - view:
text,
annotated -
select for diffs
Tue May 5 16:01:50 2009 UTC (15 years, 5 months ago) by
bisitz
Branches:
MAIN
CVS tags:
HEAD
Optimized and enhanced confirm messages:
(currently only used in lonpreferences.pm)
- Introduce new sub routine "confirmwrapper" and related style "LC_confirm_box"
Wraps option changes confirmation messages in a box (better separation from rest of screen)
- Moved trailing confirm icons (success/error) to begin of the confirm texts
- Added confirmwrapper to preference settings; consistent output (removed some br, hr, etc.)
- Added HTML new line codes ("\n") to lines in &confirm_success
Localization and Wording:
- Optimized &mt usage und optimized localization file usage:
Re-use existing phrases and always use same structure of messages ("Set [_1] to [_2]" and "Reset [_1]")
- Optimized wording (eg., Screenname and Nickname)
- Some optimized/corrected German translations and removed now unused phrases
- Optimized confirm text output for author option: "Block access to construction space"
1: # The LearningOnline Network with CAPA
2: # a pile of common html routines
3: #
4: # $Id: lonhtmlcommon.pm,v 1.211 2009/05/05 16:01:50 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, $no_realBreadcrumb) = @_;
1334: #
1335: $css_class ||= 'LC_breadcrumbs';
1336: my $Str1 = '<ol id="LC_MenuBreadcrumbs">';
1337:
1338: if($no_realBreadcrumb){
1339: $Str1 = '<ul class="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: unshift(@Crumbs,{
1365: href =>'/adm/menu',
1366: title =>'Go to main menu',
1367: target =>'_top',
1368: text =>$description,
1369: no_mt =>$no_mt_descr,
1370: });
1371: }
1372: my $links .= '<li>'.
1373: join('</li><li>'.$crumbsymbol,
1374: map {
1375: $faq = $_->{'faq'} if (exists($_->{'faq'}));
1376: $bug = $_->{'bug'} if (exists($_->{'bug'}));
1377: $help = $_->{'help'} if (exists($_->{'help'}));
1378: my $result = '<a href="'.$_->{'href'}.'" ';
1379: if (defined($_->{'target'}) && $_->{'target'} ne '') {
1380: $result .= 'target="'.$_->{'target'}.'" ';
1381: }
1382: # set the possible translation for title
1383: if ($_->{'no_mt'}) {
1384: $result .='title="'.$_->{'title'}.'">'.
1385: $_->{'text'}.'</a>';
1386: } else {
1387: $result .='title="'.&mt($_->{'title'}).'">'.
1388: &mt($_->{'text'}).'</a>';
1389: }
1390: $result;
1391: } @Crumbs
1392: ).'</li>';
1393: #Workaround for edit course.
1394: if(@Crumbs == 0 ){
1395: $links .= '<li>' if ($links ne '');
1396: } else {
1397: $links .= '<li>'.$crumbsymbol if ($links ne '');
1398: }
1399: #should the last Element be translated?
1400: if ($last->{'no_mt'}) {
1401: $links .= '<b>'.$last->{'text'}.'</b>';
1402: } else {
1403: $links .= '<b>'.&mt($last->{'text'}).'</b>';
1404: }
1405: $links .= '</li>';
1406: my $icons = '';
1407: $faq = $last->{'faq'} if (exists($last->{'faq'}));
1408: $bug = $last->{'bug'} if (exists($last->{'bug'}));
1409: $help = $last->{'help'} if (exists($last->{'help'}));
1410: $component_help=($component_help?$component_help:$help);
1411: # if ($faq ne '') {
1412: # $icons .= &Apache::loncommon::help_open_faq($faq);
1413: # }
1414: # if ($bug ne '') {
1415: # $icons .= &Apache::loncommon::help_open_bug($bug);
1416: # }
1417: if ($faq ne '' || $component_help ne '' || $bug ne '') {
1418: $icons .= &Apache::loncommon::help_open_menu($component,
1419: $component_help,
1420: $faq,$bug);
1421: }
1422: #
1423:
1424:
1425: if($no_realBreadcrumb){
1426: $Str1 .= $links.'</ul>';
1427: } else {
1428: $Str1 .= $links.'</ol>';
1429: }
1430: #
1431: if (defined($component)) {
1432: $Str .= "\n".'<table class="'.$css_class.'">'
1433: .'<tr><td class="'.$css_class.'_component">';
1434: if ($no_mt) {
1435: $Str .= $component;
1436: } else {
1437: $Str .= &mt($component);
1438: }
1439: if ($icons ne '') {
1440: $Str .= ' '.$icons;
1441: }
1442: $Str .= '</td></tr></table>'."\n";
1443: }
1444: #
1445: # Return the @Crumbs stack to what we started with
1446: push(@Crumbs,$last);
1447: shift(@Crumbs);
1448: # Return a table and after that the breadcrumb's line
1449: return "$Str\n$Str1";
1450: }
1451:
1452: sub clear_breadcrumbs {
1453: undef(@Crumbs);
1454: }
1455:
1456: sub add_breadcrumb {
1457: push (@Crumbs,@_);
1458: }
1459:
1460: } # End of scope for @Crumbs
1461:
1462: ############################################################
1463: ############################################################
1464:
1465: # Nested table routines.
1466: #
1467: # Routines to display form items in a multi-row table with 2 columns.
1468: # Uses nested tables to divide form elements into segments.
1469: # For examples of use see loncom/interface/lonnotify.pm
1470: #
1471: # Can be used in following order: ...
1472: # &start_pick_box()
1473: # row1
1474: # row2
1475: # row3 ... etc.
1476: # &submit_row()
1477: # &end_pick_box()
1478: #
1479: # where row1, row 2 etc. are chosen from &role_select_row,&course_select_row,
1480: # &status_select_row and &email_default_row
1481: #
1482: # Can also be used in following order:
1483: #
1484: # &start_pick_box()
1485: # &row_title()
1486: # &row_closure()
1487: # &row_title()
1488: # &row_closure() ... etc.
1489: # &submit_row()
1490: # &end_pick_box()
1491: #
1492: # In general a &submit_row() call should proceed the call to &end_pick_box(),
1493: # as this routine adds a button for form submission.
1494: # &submit_row() does not require a &row_closure after it.
1495: #
1496: # &start_pick_box() creates a bounding table with 1-pixel wide black border.
1497: # rows should be placed between calls to &start_pick_box() and &end_pick_box.
1498: #
1499: # &row_title() adds a title in the left column for each segment.
1500: # &row_closure() closes a row with a 1-pixel wide black line.
1501: #
1502: # &role_select_row() provides a select box from which to choose 1 or more roles
1503: # &course_select_row provides ways of picking groups of courses
1504: # radio buttons: all, by category or by picking from a course picker pop-up
1505: # note: by category option is only displayed if a domain has implemented
1506: # selection by year, semester, department, number etc.
1507: #
1508: # &status_select_row() provides a select box from which to choose 1 or more
1509: # access types (current access, prior access, and future access)
1510: #
1511: # &email_default_row() provides text boxes for default e-mail suffixes for
1512: # different authentication types in a domain.
1513: #
1514: # &row_title() and &row_closure() are called internally by the &*_select_row
1515: # routines, but can also be called directly to start and end rows which have
1516: # needs that are not accommodated by the *_select_row() routines.
1517:
1518: { # Start: row_count block for pick_box
1519: my @row_count;
1520:
1521: sub start_pick_box {
1522: my ($css_class) = @_;
1523: if (defined($css_class)) {
1524: $css_class = 'class="'.$css_class.'"';
1525: } else {
1526: $css_class= 'class="LC_pick_box"';
1527: }
1528: unshift(@row_count,0);
1529: my $output = <<"END";
1530: <table $css_class>
1531: END
1532: return $output;
1533: }
1534:
1535: sub end_pick_box {
1536: shift(@row_count);
1537: my $output = <<"END";
1538: </table>
1539: END
1540: return $output;
1541: }
1542:
1543: sub row_headline {
1544: my $output = <<"END";
1545: <tr><td colspan="2">
1546: END
1547: return $output;
1548: }
1549:
1550: sub row_title {
1551: my ($title,$css_title_class,$css_value_class) = @_;
1552: $row_count[0]++;
1553: my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1554: $css_title_class ||= 'LC_pick_box_title';
1555: $css_title_class = 'class="'.$css_title_class.'"';
1556:
1557: $css_value_class ||= 'LC_pick_box_value';
1558:
1559: if ($title ne '') {
1560: $title .= ':';
1561: }
1562: my $output = <<"ENDONE";
1563: <tr class="LC_pick_box_row">
1564: <td $css_title_class>
1565: $title
1566: </td>
1567: <td class="$css_value_class $css_class">
1568: ENDONE
1569: return $output;
1570: }
1571:
1572: sub row_closure {
1573: my ($no_separator) =@_;
1574: my $output = <<"ENDTWO";
1575: </td>
1576: </tr>
1577: ENDTWO
1578: if (!$no_separator) {
1579: $output .= <<"ENDTWO";
1580: <tr>
1581: <td colspan="2" class="LC_pick_box_separator">
1582: </td>
1583: </tr>
1584: ENDTWO
1585: }
1586: return $output;
1587: }
1588:
1589: } # End: row_count block for pick_box
1590:
1591:
1592: sub role_select_row {
1593: my ($roles,$title,$css_class,$show_separate_custom,$cdom,$cnum) = @_;
1594: my $output;
1595: if (defined($title)) {
1596: $output = &row_title($title,$css_class);
1597: }
1598: $output .= qq|
1599: <select name="roles" multiple="multiple">\n|;
1600: foreach my $role (@$roles) {
1601: my $plrole;
1602: if ($role eq 'ow') {
1603: $plrole = &mt('Course Owner');
1604: } elsif ($role eq 'cr') {
1605: if ($show_separate_custom) {
1606: if ($cdom ne '' && $cnum ne '') {
1607: my %course_customroles = &course_custom_roles($cdom,$cnum);
1608: foreach my $crrole (sort(keys(%course_customroles))) {
1609: my ($plcrrole) = ($crrole =~ m|^cr/[^/]+/[^/]+/(.+)$|);
1610: $output .= ' <option value="'.$crrole.'">'.$plcrrole.
1611: '</option>';
1612: }
1613: }
1614: } else {
1615: $plrole = &mt('Custom Role');
1616: }
1617: } else {
1618: $plrole=&Apache::lonnet::plaintext($role);
1619: }
1620: if (($role ne 'cr') || (!$show_separate_custom)) {
1621: $output .= ' <option value="'.$role.'">'.$plrole.'</option>';
1622: }
1623: }
1624: $output .= qq| </select>\n|;
1625: if (defined($title)) {
1626: $output .= &row_closure();
1627: }
1628: return $output;
1629: }
1630:
1631: sub course_select_row {
1632: my ($title,$formname,$totcodes,$codetitles,$idlist,$idlist_titles,
1633: $css_class) = @_;
1634: my $output = &row_title($title,$css_class);
1635: $output .= &course_selection($formname,$totcodes,$codetitles,$idlist,$idlist_titles);
1636: $output .= &row_closure();
1637: return $output;
1638: }
1639:
1640: sub course_selection {
1641: my ($formname,$totcodes,$codetitles,$idlist,$idlist_titles) = @_;
1642: my $output = qq|
1643: <script type="text/javascript">
1644: function coursePick (formname) {
1645: for (var i=0; i<formname.coursepick.length; i++) {
1646: if (formname.coursepick[i].value == 'category') {
1647: courseSet('');
1648: }
1649: if (!formname.coursepick[i].checked) {
1650: if (formname.coursepick[i].value == 'specific') {
1651: formname.coursetotal.value = 0;
1652: formname.courselist = '';
1653: }
1654: }
1655: }
1656: }
1657: function setPick (formname) {
1658: for (var i=0; i<formname.coursepick.length; i++) {
1659: if (formname.coursepick[i].value == 'category') {
1660: formname.coursepick[i].checked = true;
1661: }
1662: formname.coursetotal.value = 0;
1663: formname.courselist = '';
1664: }
1665: }
1666: </script>
1667: |;
1668: my $courseform='<b>'.&Apache::loncommon::selectcourse_link
1669: ($formname,'pickcourse','pickdomain','coursedesc','',1).'</b>';
1670: $output .= '<input type="radio" name="coursepick" value="all" onclick="coursePick(this.form)" />'.&mt('All courses').'<br />';
1671: if ($totcodes > 0) {
1672: my $numtitles = @$codetitles;
1673: if ($numtitles > 0) {
1674: $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 />';
1675: $output .= '<table><tr><td>'.$$codetitles[0].'<br />'."\n".
1676: '<select name="'.$$codetitles[0].
1677: '" onChange="setPick(this.form);courseSet('."'$$codetitles[0]'".')">'."\n".
1678: ' <option value="-1" />Select'."\n";
1679: my @items = ();
1680: my @longitems = ();
1681: if ($$idlist{$$codetitles[0]} =~ /","/) {
1682: @items = split(/","/,$$idlist{$$codetitles[0]});
1683: } else {
1684: $items[0] = $$idlist{$$codetitles[0]};
1685: }
1686: if (defined($$idlist_titles{$$codetitles[0]})) {
1687: if ($$idlist_titles{$$codetitles[0]} =~ /","/) {
1688: @longitems = split(/","/,$$idlist_titles{$$codetitles[0]});
1689: } else {
1690: $longitems[0] = $$idlist_titles{$$codetitles[0]};
1691: }
1692: for (my $i=0; $i<@longitems; $i++) {
1693: if ($longitems[$i] eq '') {
1694: $longitems[$i] = $items[$i];
1695: }
1696: }
1697: } else {
1698: @longitems = @items;
1699: }
1700: for (my $i=0; $i<@items; $i++) {
1701: $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
1702: }
1703: $output .= '</select></td>';
1704: for (my $i=1; $i<$numtitles; $i++) {
1705: $output .= '<td>'.$$codetitles[$i].'<br />'."\n".
1706: '<select name="'.$$codetitles[$i].
1707: '" onChange="courseSet('."'$$codetitles[$i]'".')">'."\n".
1708: '<option value="-1"><-Pick '.$$codetitles[$i-1].'</option>'."\n".
1709: '</select>'."\n".
1710: '</td>';
1711: }
1712: $output .= '</tr></table><br />';
1713: }
1714: }
1715: $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";
1716: return $output;
1717: }
1718:
1719: sub status_select_row {
1720: my ($types,$title,$css_class) = @_;
1721: my $output;
1722: if (defined($title)) {
1723: $output = &row_title($title,$css_class,'LC_pick_box_select');
1724: }
1725: $output .= qq|
1726: <select name="types" multiple="multiple">\n|;
1727: foreach my $status_type (sort(keys(%{$types}))) {
1728: $output .= ' <option value="'.$status_type.'">'.$$types{$status_type}.'</option>';
1729: }
1730: $output .= qq| </select>\n|;
1731: if (defined($title)) {
1732: $output .= &row_closure();
1733: }
1734: return $output;
1735: }
1736:
1737: sub email_default_row {
1738: my ($authtypes,$title,$descrip,$css_class) = @_;
1739: my $output = &row_title($title,$css_class);
1740: $output .= $descrip.
1741: &Apache::loncommon::start_data_table().
1742: &Apache::loncommon::start_data_table_header_row().
1743: '<th>'.&mt('Authentication Method').'</th>'.
1744: '<th align="right">'.&mt('Username -> e-mail conversion').'</th>'."\n".
1745: &Apache::loncommon::end_data_table_header_row();
1746: my $rownum = 0;
1747: foreach my $auth (sort(keys(%{$authtypes}))) {
1748: my ($userentry,$size);
1749: if ($auth =~ /^krb/) {
1750: $userentry = '';
1751: $size = 25;
1752: } else {
1753: $userentry = 'username@';
1754: $size = 15;
1755: }
1756: $output .= &Apache::loncommon::start_data_table_row().
1757: '<td> '.$$authtypes{$auth}.'</td>'.
1758: '<td align="right">'.$userentry.
1759: '<input type="text" name="'.$auth.'" size="'.$size.'" /></td>'.
1760: &Apache::loncommon::end_data_table_row();
1761: }
1762: $output .= &Apache::loncommon::end_data_table();
1763: $output .= &row_closure();
1764: return $output;
1765: }
1766:
1767:
1768: sub submit_row {
1769: my ($title,$cmd,$submit_text,$css_class) = @_;
1770: $submit_text = &mt($submit_text);
1771: my $output = &row_title($title,$css_class,'LC_pick_box_submit');
1772: $output .= qq|
1773: <br />
1774: <input type="hidden" name="command" value="$cmd" />
1775: <input type="submit" value="$submit_text"/>
1776: <br /><br />
1777: \n|;
1778: return $output;
1779: }
1780:
1781: sub course_custom_roles {
1782: my ($cdom,$cnum) = @_;
1783: my %returnhash=();
1784: my %coursepersonnel=&Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
1785: foreach my $person (sort(keys(%coursepersonnel))) {
1786: my ($role) = ($person =~ /^([^:]+):/);
1787: my ($end,$start) = split(/:/,$coursepersonnel{$person});
1788: if ($end == -1 && $start == -1) {
1789: next;
1790: }
1791: if ($role =~ m|^cr/[^/]+/[^/]+/[^/]|) {
1792: $returnhash{$role} ++;
1793: }
1794: }
1795: return %returnhash;
1796: }
1797:
1798:
1799: ##############################################
1800: ##############################################
1801:
1802: # topic_bar
1803: #
1804: # Generates a div containing a numbered (static image) followed by a title
1805: # with a background color defined in the corresponding CSS: LC_topic_bar
1806: #
1807: sub topic_bar {
1808: my ($imgnum,$title) = @_;
1809: return '
1810: <div class="LC_topic_bar">
1811: <img alt="'.&mt('Step [_1]',$imgnum).
1812: '"src="/res/adm/pages/bl_step'.$imgnum.'.gif" />
1813: <span>'.$title.'</span>
1814: </div>
1815: ';
1816: }
1817:
1818: ##############################################
1819: ##############################################
1820:
1821: # echo_form_input
1822: #
1823: # Generates html markup to add form elements from the referrer page
1824: # as hidden form elements (values encoded) in the new page.
1825: #
1826: # Intended to support two types of use
1827: # (a) to allow backing up to earlier pages in a multi-page
1828: # form submission process using a breadcrumb trail.
1829: #
1830: # (b) to allow the current page to be reloaded with form elements
1831: # set on previous page to remain unchanged. An example would
1832: # be where the a page containing a dynamically-built table of data is
1833: # is to be redisplayed, with only the sort order of the data changed.
1834: #
1835: # Inputs:
1836: # 1. Reference to array of form elements in the submitted form on
1837: # the referrer page which are to be excluded from the echoed elements.
1838: #
1839: # 2. Reference to array of regular expressions, which if matched in the
1840: # name of the form element n the referrer page will be omitted from echo.
1841: #
1842: # Outputs: A scalar containing the html markup for the echoed form
1843: # elements (all as hidden elements, with values encoded).
1844:
1845:
1846: sub echo_form_input {
1847: my ($excluded,$regexps) = @_;
1848: my $output = '';
1849: foreach my $key (keys(%env)) {
1850: if ($key =~ /^form\.(.+)$/) {
1851: my $name = $1;
1852: my $match = 0;
1853: if ((!@{$excluded}) || (!grep/^$name$/,@{$excluded})) {
1854: if (defined($regexps)) {
1855: if (@{$regexps} > 0) {
1856: foreach my $regexp (@{$regexps}) {
1857: if ($name =~ /\Q$regexp\E/) {
1858: $match = 1;
1859: last;
1860: }
1861: }
1862: }
1863: }
1864: if (!$match) {
1865: if (ref($env{$key})) {
1866: foreach my $value (@{$env{$key}}) {
1867: $value = &HTML::Entities::encode($value,'<>&"');
1868: $output .= '<input type="hidden" name="'.$name.
1869: '" value="'.$value.'" />'."\n";
1870: }
1871: } else {
1872: my $value = &HTML::Entities::encode($env{$key},'<>&"');
1873: $output .= '<input type="hidden" name="'.$name.
1874: '" value="'.$value.'" />'."\n";
1875: }
1876: }
1877: }
1878: }
1879: }
1880: return $output;
1881: }
1882:
1883: ##############################################
1884: ##############################################
1885:
1886: # set_form_elements
1887: #
1888: # Generates javascript to set form elements to values based on
1889: # corresponding values for the same form elements when the page was
1890: # previously submitted.
1891: #
1892: # Last submission values are read from hidden form elements in referring
1893: # page which have the same name, i.e., generated by &echo_form_input().
1894: #
1895: # Intended to be called by onload event.
1896: #
1897: # Inputs:
1898: # (a) Reference to hash of echoed form elements to be set.
1899: #
1900: # In the hash, keys are the form element names, and the values are the
1901: # element type (selectbox, radio, checkbox or text -for textbox, textarea or
1902: # hidden).
1903: #
1904: # (b) Optional reference to hash of stored elements to be set.
1905: #
1906: # If the page being displayed is a page which permits modification of
1907: # previously stored data, e.g., the first page in a multi-page submission,
1908: # then if stored is supplied, form elements will be set to the last stored
1909: # values. If user supplied values are also available for the same elements
1910: # these will replace the stored values.
1911: #
1912: # Output:
1913: #
1914: # javascript function - set_form_elements() which sets form elements,
1915: # expects an argument: formname - the name of the form according to
1916: # the DOM, e.g., document.compose
1917:
1918: sub set_form_elements {
1919: my ($elements,$stored) = @_;
1920: my %values;
1921: my $output .= 'function setFormElements(courseForm) {
1922: ';
1923: if (defined($stored)) {
1924: foreach my $name (keys(%{$stored})) {
1925: if (exists($$elements{$name})) {
1926: if (ref($$stored{$name}) eq 'ARRAY') {
1927: $values{$name} = $$stored{$name};
1928: } else {
1929: @{$values{$name}} = ($$stored{$name});
1930: }
1931: }
1932: }
1933: }
1934:
1935: foreach my $key (keys(%env)) {
1936: if ($key =~ /^form\.(.+)$/) {
1937: my $name = $1;
1938: if (exists($$elements{$name})) {
1939: @{$values{$name}} = &Apache::loncommon::get_env_multiple($key);
1940: }
1941: }
1942: }
1943:
1944: foreach my $name (keys(%values)) {
1945: for (my $i=0; $i<@{$values{$name}}; $i++) {
1946: $values{$name}[$i] = &HTML::Entities::decode($values{$name}[$i],'<>&"');
1947: $values{$name}[$i] =~ s/([\r\n\f]+)/\\n/g;
1948: $values{$name}[$i] =~ s/"/\\"/g;
1949: }
1950: if ($$elements{$name} eq 'text') {
1951: my $numvalues = @{$values{$name}};
1952: if ($numvalues > 1) {
1953: my $valuestring = join('","',@{$values{$name}});
1954: $output .= qq|
1955: var textvalues = new Array ("$valuestring");
1956: var total = courseForm.elements['$name'].length;
1957: if (total > $numvalues) {
1958: total = $numvalues;
1959: }
1960: for (var i=0; i<total; i++) {
1961: courseForm.elements['$name']\[i].value = textvalues[i];
1962: }
1963: |;
1964: } else {
1965: $output .= qq|
1966: courseForm.elements['$name'].value = "$values{$name}[0]";
1967: |;
1968: }
1969: } else {
1970: $output .= qq|
1971: var elementLength = courseForm.elements['$name'].length;
1972: if (elementLength==undefined) {
1973: |;
1974: foreach my $value (@{$values{$name}}) {
1975: if ($$elements{$name} eq 'selectbox') {
1976: $output .= qq|
1977: if (courseForm.elements['$name'].options[0].value == "$value") {
1978: courseForm.elements['$name'].options[0].selected = true;
1979: }|;
1980: } elsif (($$elements{$name} eq 'radio') ||
1981: ($$elements{$name} eq 'checkbox')) {
1982: $output .= qq|
1983: if (courseForm.elements['$name'].value == "$value") {
1984: courseForm.elements['$name'].checked = true;
1985: }|;
1986: }
1987: }
1988: $output .= qq|
1989: }
1990: else {
1991: for (var i=0; i<courseForm.elements['$name'].length; i++) {
1992: |;
1993: if ($$elements{$name} eq 'selectbox') {
1994: $output .= qq|
1995: courseForm.elements['$name'].options[i].selected = false;|;
1996: } elsif (($$elements{$name} eq 'radio') ||
1997: ($$elements{$name} eq 'checkbox')) {
1998: $output .= qq|
1999: courseForm.elements['$name']\[i].checked = false;|;
2000: }
2001: $output .= qq|
2002: }
2003: for (var j=0; j<courseForm.elements['$name'].length; j++) {
2004: |;
2005: foreach my $value (@{$values{$name}}) {
2006: if ($$elements{$name} eq 'selectbox') {
2007: $output .= qq|
2008: if (courseForm.elements['$name'].options[j].value == "$value") {
2009: courseForm.elements['$name'].options[j].selected = true;
2010: }|;
2011: } elsif (($$elements{$name} eq 'radio') ||
2012: ($$elements{$name} eq 'checkbox')) {
2013: $output .= qq|
2014: if (courseForm.elements['$name']\[j].value == "$value") {
2015: courseForm.elements['$name']\[j].checked = true;
2016: }|;
2017: }
2018: }
2019: $output .= qq|
2020: }
2021: }
2022: |;
2023: }
2024: }
2025: $output .= "
2026: }\n";
2027: return $output;
2028: }
2029:
2030: ##############################################
2031: ##############################################
2032:
2033: # javascript_valid_email
2034: #
2035: # Generates javascript to validate an e-mail address.
2036: # Returns a javascript function which accetps a form field as argumnent, and
2037: # returns false if field.value does not satisfy two regular expression matches
2038: # for a valid e-mail address. Backwards compatible with old browsers without
2039: # support for javascript RegExp (just checks for @ in field.value in this case).
2040:
2041: sub javascript_valid_email {
2042: my $scripttag .= <<'END';
2043: function validmail(field) {
2044: var str = field.value;
2045: if (window.RegExp) {
2046: var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
2047: var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"; //"
2048: var reg1 = new RegExp(reg1str);
2049: var reg2 = new RegExp(reg2str);
2050: if (!reg1.test(str) && reg2.test(str)) {
2051: return true;
2052: }
2053: return false;
2054: }
2055: else
2056: {
2057: if(str.indexOf("@") >= 0) {
2058: return true;
2059: }
2060: return false;
2061: }
2062: }
2063: END
2064: return $scripttag;
2065: }
2066:
2067: ##############################################
2068: ##############################################
2069:
2070: # generate_menu
2071: #
2072: # Generates html markup for a menu.
2073: #
2074: # Inputs:
2075: # An array of following structure:
2076: # ({ categorytitle => 'Categorytitle',
2077: # items => [
2078: # {
2079: # linktext => 'Text to be displayed',
2080: # url => 'URL the link is pointing to, i.e. /adm/site?action=dosomething',
2081: # permission => 'Contains permissions as returned from lonnet::allowed(),
2082: # must evaluate to true in order to activate the link',
2083: # icon => 'icon filename',
2084: # alttext => 'alt text for the icon',
2085: # help => 'Name of the corresponding helpfile',
2086: # linktitle => 'Description of the link (used for title tag)'
2087: # },
2088: # ...
2089: # ]
2090: # },
2091: # ...
2092: # )
2093: #
2094: # Outputs: A scalar containing the html markup for the menu.
2095:
2096: # ---- Remove when done ----
2097: # This routine is part of the redesign of LON-CAPA and it's
2098: # subject to change during this project.
2099: # Don't rely on its current functionality as it might be
2100: # changed or removed.
2101: # --------------------------
2102: sub generate_menu {
2103: my @menu = @_;
2104:
2105: # usage: $wrap->(element, content, {attribute => value,...});
2106: # output: content enclosed in html conform tags
2107: my $wrap = sub {
2108: return
2109: qq|<$_[0]|
2110: . join( '', map { qq| $_="${$_[2]}{$_}"| } keys %{ $_[2] } )
2111: . ($_[1] ? qq|>$_[1]</$_[0]>| : qq|/>|). "\n";
2112: };
2113:
2114: # subs for specific html elements
2115: my $h3 = sub { return $wrap->( "h3", @_ ) };
2116: my $div = sub { return $wrap->( "div", @_ ) };
2117: my $ul = sub { return $wrap->( "ul", @_ ) };
2118: my $li = sub { return $wrap->( "li", @_ ) };
2119: my $a = sub { return $wrap->( "a", @_ ) };
2120: my $img = sub { return $wrap->( "img", @_ ) };
2121:
2122: my @categories; # each element represents the entire markup for a category
2123:
2124: foreach my $category (@menu) {
2125: my @links; # contains the links for the current $category
2126: foreach my $link (@{$$category{items}}) {
2127: next unless $$link{permission};
2128:
2129: # create the markup for the current $link and push it into @links.
2130: # each entry consists of an image and a text optionally followed
2131: # by a help link.
2132: push @links, $li->(
2133: $a->(
2134: $img->("", {
2135: class => "LC_noBorder LC_middle",
2136: src => "/res/adm/pages/$$link{icon}",
2137: alt => mt(defined($$link{alttext}) ?
2138: $$link{alttext} : $$link{linktext})
2139: }), {
2140: href => $$link{url},
2141: title => mt($$link{linktitle})
2142: }).
2143: $a->(mt($$link{linktext}), {
2144: href => $$link{url},
2145: title => mt($$link{linktitle}),
2146: class => "LC_menubuttons_link"
2147: }).
2148: (defined($$link{help}) ?
2149: Apache::loncommon::help_open_topic($$link{help}) : ''),
2150: {class => "LC_menubuttons_inline_text"});
2151: }
2152:
2153: # wrap categorytitle in <h3>, concatenate with
2154: # joined and in <ul> tags wrapped @links
2155: # and wrap everything in an enclosing <div> and push it into
2156: # @categories
2157: # such that each element looks like:
2158: # <div><h3>title</h3><ul><li>...</li>...</ul></div>
2159: # the category won't be added if there aren't any links
2160: push @categories,
2161: $div->($h3->(mt($$category{categorytitle}), {class=>"LC_hcell"}).
2162: $ul->(join('' ,@links), {class =>"LC_ListStyleNormal" }),
2163: {class=>"LC_ContentBoxSpecial LC_400Box"}) if scalar(@links);
2164: }
2165:
2166: # wrap the joined @categories in another <div> (column layout)
2167: return $div->(join('', @categories), {class => "LC_columnSection"});
2168: }
2169:
2170: 1;
2171:
2172: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>