Annotation of loncom/interface/lonhtmlcommon.pm, revision 1.285.2.1
1.2 www 1: # The LearningOnline Network with CAPA
2: # a pile of common html routines
3: #
1.285.2.1! raeburn 4: # $Id: lonhtmlcommon.pm,v 1.285 2011/01/03 13:10:12 raeburn Exp $
1.2 www 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: #
1.10 matthew 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: ######################################################################
1.2 www 55:
1.1 stredwic 56: package Apache::lonhtmlcommon;
57:
1.104 albertel 58: use strict;
1.10 matthew 59: use Time::Local;
1.47 sakharuk 60: use Time::HiRes;
1.30 www 61: use Apache::lonlocal;
1.104 albertel 62: use Apache::lonnet;
1.130 www 63: use LONCAPA;
1.1 stredwic 64:
1.284 www 65: sub java_not_enabled {
66: return "\n".'<span class="LC_error">'.
67: &mt('The required Java applet could not be started. Please make sure to have Java installed and active in your browser.').
68: "</span>\n";
69: }
1.247 www 70:
71: sub coursepreflink {
72: my ($text,$category)=@_;
73: if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
1.262 raeburn 74: return '<a href="'.&HTML::Entities::encode("/adm/courseprefs?phase=display&actions=$category",'<>&"').'">'.$text.'</a>';
1.247 www 75: } else {
76: return '';
77: }
78: }
79:
1.253 www 80: sub raw_href_to_link {
81: my ($message)=@_;
1.264 faziophi 82: $message=~s/(https?\:\/\/[^\s\'\"\<]+)([\s\<]|$)/<a href="$1"><tt>$1<\/tt><\/a>$2/gi;
1.253 www 83: return $message;
84: }
85:
1.208 www 86: ##############################################
87: ##############################################
88:
89: =item confirm_success
90:
91: Successful completion of an operation message
92:
93: =cut
94:
95: sub confirm_success {
1.209 www 96: my ($message,$failure)=@_;
97: if ($failure) {
1.265 wenzelju 98: return '<span class="LC_error" style="font-size: inherit;">'."\n"
1.218 bisitz 99: .'<img src="/adm/lonIcons/navmap.wrong.gif" alt="'.&mt('Error').'" /> '."\n"
1.211 bisitz 100: .$message."\n"
101: .'</span>'."\n";
1.209 www 102: } else {
1.211 bisitz 103: return '<span class="LC_success">'."\n"
1.233 raeburn 104: .'<img src="/adm/lonIcons/navmap.correct.gif" alt="'.&mt('OK').'" /> '."\n"
1.211 bisitz 105: .$message."\n"
106: .'</span>'."\n";
1.209 www 107: }
1.208 www 108: }
1.176 foxr 109:
110: ##############################################
111: ##############################################
112:
113: =pod
114:
1.177 raeburn 115: =item dragmath_button
1.176 foxr 116:
1.177 raeburn 117: Creates a button that launches a dragmath popup-window, in which an
118: expression can be edited and pasted as LaTeX into a specified textarea.
119:
120: textarea - Name of the textarea to edit.
121: helpicon - If true, show a help icon to the right of the button.
1.176 foxr 122:
123: =cut
124:
1.177 raeburn 125: sub dragmath_button {
126: my ($textarea,$helpicon) = @_;
127: my $help_text;
128: if ($helpicon) {
1.282 raeburn 129: $help_text = &Apache::loncommon::help_open_topic('Authoring_Math_Editor',undef,undef,undef,undef,'mathhelpicon_'.$textarea);
1.177 raeburn 130: }
1.178 bisitz 131: my $buttontext=&mt('Edit Math');
1.177 raeburn 132: return <<ENDDRAGMATH;
1.246 bisitz 133: <input type="button" value="$buttontext" onclick="javascript:mathedit('$textarea',document)" />$help_text
1.177 raeburn 134: ENDDRAGMATH
135: }
136:
1.176 foxr 137: ##############################################
138:
1.177 raeburn 139: =pod
140:
141: =item dragmath_js
142:
143: Javascript used to open pop-up window containing dragmath applet which
144: can be used to paste LaTeX into a textarea.
145: =cut
1.176 foxr 146:
1.177 raeburn 147: sub dragmath_js {
1.182 foxr 148: my ($popup) = @_;
1.177 raeburn 149: return <<ENDDRAGMATHJS;
150: <script type="text/javascript">
1.218 bisitz 151: // <![CDATA[
1.176 foxr 152: function mathedit(textarea, doc) {
153: targetEntry = textarea;
1.177 raeburn 154: targetDoc = doc;
1.182 foxr 155: newwin = window.open("/adm/dragmath/applet/$popup.html","","width=565,height=500,resizable");
1.176 foxr 156: }
1.218 bisitz 157: // ]]>
1.176 foxr 158: </script>
1.177 raeburn 159:
160: ENDDRAGMATHJS
1.176 foxr 161: }
162:
1.182 foxr 163:
1.40 www 164: ##############################################
165: ##############################################
166:
167: =pod
168:
169: =item authorbombs
170:
171: =cut
172:
173: ##############################################
174: ##############################################
175:
176: sub authorbombs {
177: my $url=shift;
178: $url=&Apache::lonnet::declutter($url);
1.155 albertel 179: my ($udom,$uname)=($url=~m{^($LONCAPA::domain_re)/($LONCAPA::username_re)/});
1.40 www 180: my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
1.232 raeburn 181: foreach my $bomb (keys(%bombs)) {
182: if ($bomb =~ /^$udom\/$uname\//) {
1.40 www 183: return '<a href="/adm/bombs/'.$url.
1.218 bisitz 184: '"><img src="'.&Apache::loncommon::lonhttpdurl('/adm/lonMisc/bomb.gif').'" alt="'.&mt('Bomb').'" border="0" /></a>'.
1.40 www 185: &Apache::loncommon::help_open_topic('About_Bombs');
186: }
187: }
188: return '';
189: }
1.26 matthew 190:
191: ##############################################
192: ##############################################
193:
1.41 www 194: sub recent_filename {
195: my $area=shift;
1.130 www 196: return 'nohist_recent_'.&escape($area);
1.41 www 197: }
198:
199: sub store_recent {
1.136 albertel 200: my ($area,$name,$value,$freeze)=@_;
1.41 www 201: my $file=&recent_filename($area);
202: my %recent=&Apache::lonnet::dump($file);
1.111 www 203: if (scalar(keys(%recent))>20) {
1.41 www 204: # remove oldest value
1.136 albertel 205: my $oldest=time();
1.41 www 206: my $delkey='';
1.136 albertel 207: foreach my $item (keys(%recent)) {
208: my $thistime=(split(/\&/,$recent{$item}))[0];
209: if (($thistime ne "always_include") && ($thistime<$oldest)) {
1.41 www 210: $oldest=$thistime;
1.136 albertel 211: $delkey=$item;
1.41 www 212: }
213: }
214: &Apache::lonnet::del($file,[$delkey]);
215: }
216: # store new value
1.136 albertel 217: my $timestamp;
218: if ($freeze) {
219: $timestamp = "always_include";
220: } else {
221: $timestamp = time();
222: }
1.41 www 223: &Apache::lonnet::put($file,{ $name =>
1.136 albertel 224: $timestamp.'&'.&escape($value) });
1.41 www 225: }
226:
1.89 banghart 227: sub remove_recent {
228: my ($area,$names)=@_;
229: my $file=&recent_filename($area);
230: return &Apache::lonnet::del($file,$names);
231: }
232:
1.41 www 233: sub select_recent {
234: my ($area,$fieldname,$event)=@_;
235: my %recent=&Apache::lonnet::dump(&recent_filename($area));
236: my $return="\n<select name='$fieldname'".
1.96 albertel 237: ($event?" onchange='$event'":'').
1.41 www 238: ">\n<option value=''>--- ".&mt('Recent')." ---</option>";
1.136 albertel 239: foreach my $value (sort(keys(%recent))) {
240: unless ($value =~/^error\:/) {
241: my $escaped = &Apache::loncommon::escape_url($value);
1.160 albertel 242: &Apache::loncommon::inhibit_menu_check(\$escaped);
1.251 raeburn 243: if ($area eq 'residx') {
244: next if ((!&Apache::lonnet::allowed('bre',$value)) && (!&Apache::lonnet::allowed('bro',$value)));
245: }
1.94 foxr 246: $return.="\n<option value='$escaped'>".
1.136 albertel 247: &unescape((split(/\&/,$recent{$value}))[1]).
1.41 www 248: '</option>';
249: }
250: }
251: $return.="\n</select>\n";
252: return $return;
253: }
254:
1.97 albertel 255: sub get_recent {
256: my ($area, $n) = @_;
257: my %recent=&Apache::lonnet::dump(&recent_filename($area));
258:
259: # Create hash with key as time and recent as value
1.136 albertel 260: # Begin filling return_hash with any 'always_include' option
1.97 albertel 261: my %time_hash = ();
1.136 albertel 262: my %return_hash = ();
1.232 raeburn 263: foreach my $item (keys(%recent)) {
1.136 albertel 264: my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
265: if ($thistime eq 'always_include') {
266: $return_hash{$item} = &unescape($thisvalue);
267: $n--;
268: } else {
269: $time_hash{$thistime} = $item;
1.133 albertel 270: }
1.97 albertel 271: }
272:
273: # Sort by decreasing time and return key value pairs
274: my $idx = 1;
1.136 albertel 275: foreach my $item (reverse(sort(keys(%time_hash)))) {
276: $return_hash{$time_hash{$item}} =
277: &unescape((split(/\&/,$recent{$time_hash{$item}}))[1]);
1.97 albertel 278: if ($n && ($idx++ >= $n)) {last;}
279: }
280:
281: return %return_hash;
282: }
283:
1.136 albertel 284: sub get_recent_frozen {
285: my ($area) = @_;
286: my %recent=&Apache::lonnet::dump(&recent_filename($area));
287:
288: # Create hash with all 'frozen' items
289: my %return_hash = ();
290: foreach my $item (keys(%recent)) {
291: my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
292: if ($thistime eq 'always_include') {
293: $return_hash{$item} = &unescape($thisvalue);
294: }
295: }
296: return %return_hash;
297: }
298:
1.97 albertel 299:
1.41 www 300:
1.26 matthew 301: =pod
302:
303: =item textbox
304:
305: =cut
306:
307: ##############################################
308: ##############################################
309: sub textbox {
310: my ($name,$value,$size,$special) = @_;
311: $size = 40 if (! defined($size));
1.128 albertel 312: $value = &HTML::Entities::encode($value,'<>&"');
1.26 matthew 313: my $Str = '<input type="text" name="'.$name.'" size="'.$size.'" '.
314: 'value="'.$value.'" '.$special.' />';
315: return $Str;
316: }
317:
318: ##############################################
319: ##############################################
320:
321: =pod
322:
323: =item checkbox
324:
325: =cut
326:
327: ##############################################
328: ##############################################
329: sub checkbox {
1.68 matthew 330: my ($name,$checked,$value) = @_;
331: my $Str = '<input type="checkbox" name="'.$name.'" ';
332: if (defined($value)) {
333: $Str .= 'value="'.$value.'"';
334: }
335: if ($checked) {
1.206 bisitz 336: $Str .= ' checked="checked"';
1.68 matthew 337: }
338: $Str .= ' />';
1.26 matthew 339: return $Str;
340: }
341:
1.120 albertel 342:
343: =pod
344:
345: =item radiobutton
346:
347: =cut
348:
349: ##############################################
350: ##############################################
351: sub radio {
352: my ($name,$checked,$value) = @_;
353: my $Str = '<input type="radio" name="'.$name.'" ';
354: if (defined($value)) {
355: $Str .= 'value="'.$value.'"';
356: }
357: if ($checked eq $value) {
1.206 bisitz 358: $Str .= ' checked="checked"';
1.120 albertel 359: }
360: $Str .= ' />';
361: return $Str;
362: }
363:
1.10 matthew 364: ##############################################
365: ##############################################
366:
367: =pod
368:
369: =item &date_setter
370:
1.22 matthew 371: &date_setter returns html and javascript for a compact date-setting form.
372: To retrieve values from it, use &get_date_from_form().
373:
1.10 matthew 374: Inputs
375:
376: =over 4
377:
378: =item $dname
379:
380: The name to prepend to the form elements.
381: The form elements defined will be dname_year, dname_month, dname_day,
382: dname_hour, dname_min, and dname_sec.
383:
384: =item $currentvalue
385:
386: The current setting for this time parameter. A unix format time
387: (time in seconds since the beginning of Jan 1st, 1970, GMT.
1.257 faziophi 388: An undefined value is taken to indicate the value is the current time
389: unless it is requested to leave it empty. See $includeempty.
1.10 matthew 390: Also, to be explicit, a value of 'now' also indicates the current time.
391:
1.26 matthew 392: =item $special
393:
394: Additional html/javascript to be associated with each element in
395: the date_setter. See lonparmset for example usage.
396:
1.59 matthew 397: =item $includeempty
398:
1.257 faziophi 399: If it is set (true) and no date/time value is provided,
400: the date/time fields are left empty.
401:
1.59 matthew 402: =item $state
403:
404: Specifies the initial state of the form elements. Either 'disabled' or empty.
405: Defaults to empty, which indiciates the form elements are not disabled.
406:
1.22 matthew 407: =back
408:
409: Bugs
410:
411: The method used to restrict user input will fail in the year 2400.
412:
1.10 matthew 413: =cut
414:
415: ##############################################
416: ##############################################
417: sub date_setter {
1.67 matthew 418: my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
1.134 raeburn 419: $no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_;
1.175 raeburn 420: my $now = time;
1.257 faziophi 421:
422: my $tzname;
423: my ($sec,$min,$hour,$mday,$month,$year) = ('', '', undef,''.''.'');
424: #other potentially useful values: wkday,yrday,is_daylight_savings
425:
1.59 matthew 426: if (! defined($state) || $state ne 'disabled') {
427: $state = '';
428: }
1.67 matthew 429: if (! defined($no_hh_mm_ss)) {
430: $no_hh_mm_ss = 0;
431: }
1.110 www 432: if ($currentvalue eq 'now') {
1.257 faziophi 433: $currentvalue = $now;
1.110 www 434: }
1.257 faziophi 435:
436: # Default value: Set empty date field to current time
437: # unless empty inclusion is requested
438: if ((!$includeempty) && (!$currentvalue)) {
439: $currentvalue = $now;
1.10 matthew 440: }
1.257 faziophi 441: # Do we have a date? Split it!
1.39 www 442: if ($currentvalue) {
1.257 faziophi 443: ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($currentvalue);
444:
445: #No values provided for hour, min, sec? Use default 0
446: if (($defhour) || ($defmin) || ($defsec)) {
447: $sec = ($defsec ? $defsec : 0);
448: $min = ($defmin ? $defmin : 0);
449: $hour = ($defhour ? $defhour : 0);
450: }
1.107 www 451: }
1.10 matthew 452: my $result = "\n<!-- $dname date setting form -->\n";
453: $result .= <<ENDJS;
1.135 albertel 454: <script type="text/javascript">
1.218 bisitz 455: // <![CDATA[
1.10 matthew 456: function $dname\_checkday() {
457: var day = document.$formname.$dname\_day.value;
458: var month = document.$formname.$dname\_month.value;
459: var year = document.$formname.$dname\_year.value;
460: var valid = true;
461: if (day < 1) {
462: document.$formname.$dname\_day.value = 1;
463: }
464: if (day > 31) {
465: document.$formname.$dname\_day.value = 31;
466: }
467: if ((month == 1) || (month == 3) || (month == 5) ||
468: (month == 7) || (month == 8) || (month == 10) ||
469: (month == 12)) {
470: if (day > 31) {
471: document.$formname.$dname\_day.value = 31;
472: day = 31;
473: }
474: } else if (month == 2 ) {
475: if ((year % 4 == 0) && (year % 100 != 0)) {
476: if (day > 29) {
477: document.$formname.$dname\_day.value = 29;
478: }
479: } else if (day > 29) {
480: document.$formname.$dname\_day.value = 28;
481: }
482: } else if (day > 30) {
483: document.$formname.$dname\_day.value = 30;
484: }
485: }
1.95 matthew 486:
1.59 matthew 487: function $dname\_disable() {
488: document.$formname.$dname\_month.disabled=true;
489: document.$formname.$dname\_day.disabled=true;
490: document.$formname.$dname\_year.disabled=true;
491: document.$formname.$dname\_hour.disabled=true;
492: document.$formname.$dname\_minute.disabled=true;
493: document.$formname.$dname\_second.disabled=true;
494: }
495:
496: function $dname\_enable() {
497: document.$formname.$dname\_month.disabled=false;
498: document.$formname.$dname\_day.disabled=false;
499: document.$formname.$dname\_year.disabled=false;
500: document.$formname.$dname\_hour.disabled=false;
501: document.$formname.$dname\_minute.disabled=false;
502: document.$formname.$dname\_second.disabled=false;
503: }
504:
1.29 www 505: function $dname\_opencalendar() {
1.59 matthew 506: if (! document.$formname.$dname\_month.disabled) {
507: var calwin=window.open(
1.29 www 508: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
509: document.$formname.$dname\_month.value+"&year="+
510: document.$formname.$dname\_year.value,
511: "LONCAPAcal",
512: "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
1.59 matthew 513: }
1.29 www 514:
515: }
1.218 bisitz 516: // ]]>
1.10 matthew 517: </script>
518: ENDJS
1.192 bisitz 519: $result .= ' <span class="LC_nobreak">';
1.96 albertel 520: my $monthselector = qq{<select name="$dname\_month" $special $state onchange="javascript:$dname\_checkday()" >};
1.67 matthew 521: # Month
1.10 matthew 522: my @Months = qw/January February March April May June
523: July August September October November December/;
524: # Pad @Months with a bogus value to make indexing easier
525: unshift(@Months,'If you can read this an error occurred');
1.95 matthew 526: if ($includeempty) { $monthselector.="<option value=''></option>"; }
1.10 matthew 527: for(my $m = 1;$m <=$#Months;$m++) {
1.228 bisitz 528: $monthselector .= qq{ <option value="$m"};
529: $monthselector .= ' selected="selected"' if ($m-1 eq $month);
530: $monthselector .= '> '.&mt($Months[$m]).' </option>'."\n";
1.10 matthew 531: }
1.95 matthew 532: $monthselector.= ' </select>';
1.67 matthew 533: # Day
1.96 albertel 534: my $dayselector = qq{<input type="text" name="$dname\_day" $state value="$mday" size="3" $special onchange="javascript:$dname\_checkday()" />};
1.67 matthew 535: # Year
1.226 bisitz 536: my $yearselector = qq{<input type="text" name="$dname\_year" $state value="$year" size="5" $special onchange="javascript:$dname\_checkday()" />};
1.95 matthew 537: #
538: my $hourselector = qq{<select name="$dname\_hour" $special $state >};
539: if ($includeempty) {
540: $hourselector.=qq{<option value=''></option>};
541: }
542: for (my $h = 0;$h<24;$h++) {
1.228 bisitz 543: $hourselector .= qq{<option value="$h"};
544: $hourselector .= ' selected="selected"' if (defined($hour) && $hour == $h);
1.95 matthew 545: $hourselector .= ">";
546: my $timest='';
547: if ($h == 0) {
548: $timest .= "12 am";
549: } elsif($h == 12) {
550: $timest .= "12 noon";
551: } elsif($h < 12) {
552: $timest .= "$h am";
553: } else {
554: $timest .= $h-12 ." pm";
555: }
556: $timest=&mt($timest);
557: $hourselector .= $timest." </option>\n";
558: }
559: $hourselector .= " </select>\n";
560: my $minuteselector = qq{<input type="text" name="$dname\_minute" $special $state value="$min" size="3" />};
561: my $secondselector= qq{<input type="text" name="$dname\_second" $special $state value="$sec" size="3" />};
1.134 raeburn 562: my $cal_link;
563: if (!$nolink) {
564: $cal_link = qq{<a href="javascript:$dname\_opencalendar()">};
565: }
1.95 matthew 566: #
1.175 raeburn 567: my $tzone = ' '.$tzname.' ';
1.95 matthew 568: if ($no_hh_mm_ss) {
1.134 raeburn 569: $result .= &mt('[_1] [_2] [_3] ',
1.174 raeburn 570: $monthselector,$dayselector,$yearselector).
571: $tzone;
1.134 raeburn 572: if (!$nolink) {
1.141 albertel 573: $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
1.134 raeburn 574: }
1.95 matthew 575: } else {
1.134 raeburn 576: $result .= &mt('[_1] [_2] [_3] [_4] [_5]m [_6]s ',
577: $monthselector,$dayselector,$yearselector,
1.174 raeburn 578: $hourselector,$minuteselector,$secondselector).
579: $tzone;
1.134 raeburn 580: if (!$nolink) {
1.141 albertel 581: $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
1.134 raeburn 582: }
1.67 matthew 583: }
1.135 albertel 584: $result .= "</span>\n<!-- end $dname date setting form -->\n";
1.10 matthew 585: return $result;
586: }
587:
1.175 raeburn 588: sub get_timedates {
589: my ($epoch) = @_;
590: my $dt = DateTime->from_epoch(epoch => $epoch)
591: ->set_time_zone(&Apache::lonlocal::gettimezone());
592: my $tzname = $dt->time_zone_short_name();
593: my $sec = $dt->second;
594: my $min = $dt->minute;
595: my $hour = $dt->hour;
596: my $mday = $dt->day;
597: my $month = $dt->month;
598: if ($month) {
599: $month --;
600: }
601: my $year = $dt->year;
602: return ($tzname,$sec,$min,$hour,$mday,$month,$year);
603: }
1.166 banghart 604:
605: sub build_url {
606: my ($base, $fields)=@_;
607: my $url;
608: $url = $base.'?';
1.168 albertel 609: foreach my $key (keys(%$fields)) {
610: $url.=&escape($key).'='.&escape($$fields{$key}).'&';
1.166 banghart 611: }
612: $url =~ s/&$//;
613: return $url;
614: }
615:
616:
1.10 matthew 617: ##############################################
618: ##############################################
619:
1.22 matthew 620: =pod
621:
1.10 matthew 622: =item &get_date_from_form
1.22 matthew 623:
624: get_date_from_form retrieves the date specified in an &date_setter form.
1.10 matthew 625:
626: Inputs:
627:
628: =over 4
629:
630: =item $dname
631:
1.226 bisitz 632: The name passed to &date_setter, which prefixes the form elements.
1.10 matthew 633:
634: =item $defaulttime
635:
636: The unix time to use as the default in case of poor inputs.
637:
638: =back
639:
640: Returns: Unix time represented in the form.
641:
642: =cut
643:
644: ##############################################
645: ##############################################
646: sub get_date_from_form {
647: my ($dname) = @_;
648: my ($sec,$min,$hour,$day,$month,$year);
649: #
1.104 albertel 650: if (defined($env{'form.'.$dname.'_second'})) {
651: my $tmpsec = $env{'form.'.$dname.'_second'};
1.10 matthew 652: if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
653: $sec = $tmpsec;
654: }
1.64 albertel 655: if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
1.67 matthew 656: } else {
657: $sec = 0;
1.10 matthew 658: }
1.104 albertel 659: if (defined($env{'form.'.$dname.'_minute'})) {
660: my $tmpmin = $env{'form.'.$dname.'_minute'};
1.10 matthew 661: if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
662: $min = $tmpmin;
663: }
1.64 albertel 664: if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
1.67 matthew 665: } else {
666: $min = 0;
1.10 matthew 667: }
1.104 albertel 668: if (defined($env{'form.'.$dname.'_hour'})) {
669: my $tmphour = $env{'form.'.$dname.'_hour'};
1.33 matthew 670: if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
1.10 matthew 671: $hour = $tmphour;
672: }
1.67 matthew 673: } else {
674: $hour = 0;
1.10 matthew 675: }
1.104 albertel 676: if (defined($env{'form.'.$dname.'_day'})) {
677: my $tmpday = $env{'form.'.$dname.'_day'};
1.10 matthew 678: if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
679: $day = $tmpday;
680: }
681: }
1.104 albertel 682: if (defined($env{'form.'.$dname.'_month'})) {
683: my $tmpmonth = $env{'form.'.$dname.'_month'};
1.10 matthew 684: if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
1.175 raeburn 685: $month = $tmpmonth;
1.10 matthew 686: }
687: }
1.104 albertel 688: if (defined($env{'form.'.$dname.'_year'})) {
689: my $tmpyear = $env{'form.'.$dname.'_year'};
1.175 raeburn 690: if (($tmpyear =~ /^\d+$/) && ($tmpyear >= 1970)) {
691: $year = $tmpyear;
1.10 matthew 692: }
693: }
1.175 raeburn 694: if (($year<1970) || ($year>2037)) { return undef; }
1.33 matthew 695: if (defined($sec) && defined($min) && defined($hour) &&
1.175 raeburn 696: defined($day) && defined($month) && defined($year)) {
697: my $timezone = &Apache::lonlocal::gettimezone();
698: my $dt = DateTime->new( year => $year,
699: month => $month,
700: day => $day,
701: hour => $hour,
702: minute => $min,
703: second => $sec,
704: time_zone => $timezone,
705: );
706: my $epoch_time = $dt->epoch;
707: if ($epoch_time ne '') {
708: return $epoch_time;
709: } else {
710: return undef;
711: }
1.10 matthew 712: } else {
713: return undef;
714: }
1.20 matthew 715: }
716:
717: ##############################################
718: ##############################################
719:
720: =pod
721:
722: =item &pjump_javascript_definition()
723:
724: Returns javascript defining the 'pjump' function, which opens up a
725: parameter setting wizard.
726:
727: =cut
728:
729: ##############################################
730: ##############################################
731: sub pjump_javascript_definition {
732: my $Str = <<END;
1.109 www 733: function pjump(type,dis,value,marker,ret,call,hour,min,sec) {
1.20 matthew 734: parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
735: +"&value="+escape(value)+"&marker="+escape(marker)
736: +"&return="+escape(ret)
1.109 www 737: +"&call="+escape(call)+"&name="+escape(dis)
738: +"&defhour="+escape(hour)+"&defmin="+escape(min)
739: +"&defsec="+escape(sec),"LONCAPAparms",
1.20 matthew 740: "height=350,width=350,scrollbars=no,menubar=no");
741: }
742: END
743: return $Str;
1.10 matthew 744: }
745:
746: ##############################################
747: ##############################################
1.17 matthew 748:
749: =pod
750:
751: =item &javascript_nothing()
752:
753: Return an appropriate null for the users browser. This is used
754: as the first arguement for window.open calls when you want a blank
755: window that you can then write to.
756:
757: =cut
758:
759: ##############################################
760: ##############################################
761: sub javascript_nothing {
762: # mozilla and other browsers work with "''", but IE on mac does not.
763: my $nothing = "''";
764: my $user_browser;
765: my $user_os;
1.104 albertel 766: $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
767: $user_os = $env{'browser.os'} if (exists($env{'browser.os'}));
1.17 matthew 768: if (! defined($user_browser) || ! defined($user_os)) {
769: (undef,$user_browser,undef,undef,undef,$user_os) =
770: &Apache::loncommon::decode_user_agent();
771: }
772: if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
773: $nothing = "'javascript:void(0);'";
774: }
775: return $nothing;
776: }
777:
1.90 www 778: ##############################################
779: ##############################################
780: sub javascript_docopen {
1.171 albertel 781: my ($mimetype) = @_;
782: $mimetype ||= 'text/html';
1.90 www 783: # safari does not understand document.open() and loads "text/html"
784: my $nothing = "''";
785: my $user_browser;
786: my $user_os;
1.104 albertel 787: $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
788: $user_os = $env{'browser.os'} if (exists($env{'browser.os'}));
1.90 www 789: if (! defined($user_browser) || ! defined($user_os)) {
790: (undef,$user_browser,undef,undef,undef,$user_os) =
791: &Apache::loncommon::decode_user_agent();
792: }
793: if ($user_browser eq 'safari' && $user_os =~ 'mac') {
794: $nothing = "document.clear()";
795: } else {
1.171 albertel 796: $nothing = "document.open('$mimetype','replace')";
1.90 www 797: }
798: return $nothing;
799: }
800:
1.21 matthew 801:
1.17 matthew 802: ##############################################
803: ##############################################
804:
1.21 matthew 805: =pod
1.17 matthew 806:
1.21 matthew 807: =item &StatusOptions()
1.10 matthew 808:
1.21 matthew 809: Returns html for a selection box which allows the user to choose the
810: enrollment status of students. The selection box name is 'Status'.
1.6 stredwic 811:
1.21 matthew 812: Inputs:
1.6 stredwic 813:
1.21 matthew 814: $status: the currently selected status. If undefined the value of
1.104 albertel 815: $env{'form.Status'} is taken. If that is undefined, a value of 'Active'
1.21 matthew 816: is used.
1.6 stredwic 817:
1.21 matthew 818: $formname: The name of the form. If defined the onchange attribute of
819: the selection box is set to document.$formname.submit().
1.6 stredwic 820:
1.21 matthew 821: $size: the size (number of lines) of the selection box.
1.6 stredwic 822:
1.27 matthew 823: $onchange: javascript to use when the value is changed. Enclosed in
824: double quotes, ""s, not single quotes.
825:
1.21 matthew 826: Returns: a perl string as described.
1.1 stredwic 827:
1.21 matthew 828: =cut
1.9 stredwic 829:
1.21 matthew 830: ##############################################
831: ##############################################
832: sub StatusOptions {
1.165 banghart 833: my ($status, $formName,$size,$onchange,$mult)=@_;
1.21 matthew 834: $size = 1 if (!defined($size));
835: if (! defined($status)) {
836: $status = 'Active';
1.104 albertel 837: $status = $env{'form.Status'} if (exists($env{'form.Status'}));
1.9 stredwic 838: }
1.1 stredwic 839:
840: my $Str = '';
841: $Str .= '<select name="Status"';
1.165 banghart 842: if (defined($mult)){
843: $Str .= ' multiple="multiple" ';
844: }
1.27 matthew 845: if(defined($formName) && $formName ne '' && ! defined($onchange)) {
1.1 stredwic 846: $Str .= ' onchange="document.'.$formName.'.submit()"';
1.27 matthew 847: }
848: if (defined($onchange)) {
849: $Str .= ' onchange="'.$onchange.'"';
1.1 stredwic 850: }
1.21 matthew 851: $Str .= ' size="'.$size.'" ';
1.1 stredwic 852: $Str .= '>'."\n";
1.153 raeburn 853: foreach my $type (['Active', &mt('Currently Has Access')],
854: ['Future', &mt('Will Have Future Access')],
855: ['Expired', &mt('Previously Had Access')],
856: ['Any', &mt('Any Access Status')]) {
1.151 albertel 857: my ($name,$label) = @$type;
858: $Str .= '<option value="'.$name.'" ';
859: if ($status eq $name) {
860: $Str .= 'selected="selected" ';
861: }
862: $Str .= '>'.$label.'</option>'."\n";
863: }
864:
1.1 stredwic 865: $Str .= '</select>'."\n";
1.7 stredwic 866: }
1.12 matthew 867:
868: ########################################################
869: ########################################################
1.7 stredwic 870:
1.23 matthew 871: =pod
872:
873: =item Progess Window Handling Routines
874:
875: These routines handle the creation, update, increment, and closure of
876: progress windows. The progress window reports to the user the number
877: of items completed and an estimate of the time required to complete the rest.
878:
879: =over 4
880:
881:
882: =item &Create_PrgWin
883:
884: Writes javascript to the client to open a progress window and returns a
885: data structure used for bookkeeping.
886:
887: Inputs
888:
889: =over 4
890:
891: =item $r Apache request
892:
893: =item $title The title of the progress window
894:
895: =item $heading A description (usually 1 line) of the process being initiated.
896:
897: =item $number_to_do The total number of items being processed.
1.50 albertel 898:
899: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
900: specified)
901:
1.51 albertel 902: =item $width Specify the width in charaters of the input field.
903:
1.50 albertel 904: =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
905:
906: =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
1.23 matthew 907:
908: =back
909:
910: Returns a hash containing the progress state data structure.
911:
912:
913: =item &Update_PrgWin
914:
915: Updates the text in the progress indicator. Does not increment the count.
916: See &Increment_PrgWin.
917:
918: Inputs:
919:
920: =over 4
921:
922: =item $r Apache request
923:
924: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
925:
926: =item $displaystring The string to write to the status indicator
927:
928: =back
929:
930: Returns: none
931:
932:
933: =item Increment_PrgWin
934:
1.276 bisitz 935: Increment the count of items completed for the progress window by $step or 1 if no step is provided.
1.23 matthew 936:
937: Inputs:
938:
939: =over 4
940:
941: =item $r Apache request
942:
943: =item $prog_state Pointer to the data structure returned by Create_PrgWin
944:
945: =item $extraInfo A description of the items being iterated over. Typically
946: 'student'.
947:
1.279 bisitz 948: =item $step (optional) counter step. Will be set to default 1 if ommited. step must be greater than 0 or empty.
1.276 bisitz 949:
1.23 matthew 950: =back
951:
952: Returns: none
953:
954:
955: =item Close_PrgWin
956:
957: Closes the progress window.
958:
959: Inputs:
960:
961: =over 4
962:
963: =item $r Apache request
964:
965: =item $prog_state Pointer to the data structure returned by Create_PrgWin
966:
967: =back
968:
969: Returns: none
970:
971: =back
972:
973: =cut
974:
975: ########################################################
976: ########################################################
977:
1.51 albertel 978: my $uniq=0;
979: sub get_uniq_name {
980: $uniq++;
981: return 'uniquename'.$uniq;
982: }
983:
1.7 stredwic 984: # Create progress
985: sub Create_PrgWin {
1.51 albertel 986: my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
987: $inputname)=@_;
1.49 albertel 988: if (!defined($type)) { $type='popup'; }
1.51 albertel 989: if (!defined($width)) { $width=55; }
1.49 albertel 990: my %prog_state;
991: $prog_state{'type'}=$type;
992: if ($type eq 'popup') {
993: $prog_state{'window'}='popwin';
1.122 albertel 994: my $start_page =
995: &Apache::loncommon::start_page($title,undef,
996: {'only_body' => 1,
997: 'bgcolor' => '#88DDFF',
998: 'js_ready' => 1});
999: my $end_page = &Apache::loncommon::end_page({'js_ready' => 1});
1000:
1.49 albertel 1001: #the whole function called through timeout is due to issues
1002: #in mozilla Read BUG #2665 if you want to know the whole story
1.230 bisitz 1003: &r_print($r,&Apache::lonhtmlcommon::scripttag(
1.49 albertel 1004: "var popwin;
1005: function openpopwin () {
1006: popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
1.122 albertel 1007: "popwin.document.writeln(\'".$start_page.
1.170 albertel 1008: "<h4>".&mt("$heading")."<\/h4>".
1.212 bisitz 1009: "<form action=\"\" name=\"popremain\" method=\"post\">".
1.51 albertel 1010: '<input type="text" size="'.$width.'" name="remaining" value="'.
1.131 albertel 1011: &mt('Starting').'" /><\\/form>'.$end_page.
1.122 albertel 1012: "\');".
1.49 albertel 1013: "popwin.document.close();}".
1.230 bisitz 1014: "\nwindow.setTimeout(openpopwin,0)"
1015: ));
1.49 albertel 1016: $prog_state{'formname'}='popremain';
1017: $prog_state{'inputname'}="remaining";
1018: } elsif ($type eq 'inline') {
1019: $prog_state{'window'}='window';
1020: if (!$formname) {
1.51 albertel 1021: $prog_state{'formname'}=&get_uniq_name();
1.159 banghart 1022: &r_print($r,'<form action="" name="'.$prog_state{'formname'}.'">');
1.49 albertel 1023: } else {
1024: $prog_state{'formname'}=$formname;
1025: }
1026: if (!$inputname) {
1.51 albertel 1027: $prog_state{'inputname'}=&get_uniq_name();
1.170 albertel 1028: &r_print($r,&mt("$heading [_1]",' <input type="text" name="'.$prog_state{'inputname'}.'" size="'.$width.'" />'));
1.49 albertel 1029: } else {
1030: $prog_state{'inputname'}=$inputname;
1031:
1032: }
1033: if (!$formname) { &r_print($r,'</form>'); }
1034: &Update_PrgWin($r,\%prog_state,&mt('Starting'));
1035: }
1.7 stredwic 1036:
1.16 albertel 1037: $prog_state{'done'}=0;
1.23 matthew 1038: $prog_state{'firststart'}=&Time::HiRes::time();
1039: $prog_state{'laststart'}=&Time::HiRes::time();
1.16 albertel 1040: $prog_state{'max'}=$number_to_do;
1.49 albertel 1041:
1.14 albertel 1042: return %prog_state;
1.7 stredwic 1043: }
1044:
1045: # update progress
1046: sub Update_PrgWin {
1.14 albertel 1047: my ($r,$prog_state,$displayString)=@_;
1.230 bisitz 1048: &r_print($r,&Apache::lonhtmlcommon::scripttag(
1.218 bisitz 1049: $$prog_state{'window'}.'.document.'.
1.230 bisitz 1050: $$prog_state{'formname'}.'.'.
1051: $$prog_state{'inputname'}.'.value="'.
1052: $displayString.'";'
1053: ));
1.23 matthew 1054: $$prog_state{'laststart'}=&Time::HiRes::time();
1.14 albertel 1055: }
1056:
1057: # increment progress state
1058: sub Increment_PrgWin {
1.275 bisitz 1059: my ($r,$prog_state,$extraInfo,$step)=@_;
1.279 bisitz 1060: $step = $step > 0 ? $step : 1;
1.275 bisitz 1061: $$prog_state{'done'} += $step;
1062:
1063: # Catch (max modulo step) <> 0
1064: my $current = $$prog_state{'done'};
1065: my $last = ($$prog_state{'max'} - $current);
1066: if ($last <= 0) {
1067: $last = 1;
1068: $current = $$prog_state{'max'};
1069: }
1070:
1.23 matthew 1071: my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
1.275 bisitz 1072: $current * $last;
1.16 albertel 1073: $time_est = int($time_est);
1.80 matthew 1074: #
1075: my $min = int($time_est/60);
1076: my $sec = $time_est % 60;
1.278 bisitz 1077:
1.23 matthew 1078: my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
1079: if ($lasttime > 9) {
1080: $lasttime = int($lasttime);
1081: } elsif ($lasttime < 0.01) {
1082: $lasttime = 0;
1083: } else {
1084: $lasttime = sprintf("%3.2f",$lasttime);
1085: }
1.278 bisitz 1086:
1087: $sec = 0 if ($min >= 10); # Don't show seconds if remaining time >= 10 min.
1088: $sec = 1 if ( ($min == 0) && ($sec == 0) ); # Little cheating: pretend to have 1 second remaining instead of 0 to have something to display
1089:
1090: my $timeinfo =
1091: &mt('[_1]/[_2]:'
1092: .' [quant,_3,minute,minutes,] [quant,_4,second ,seconds ,]remaining'
1093: .' ([quant,_5,second] for '.$extraInfo.')',
1094: $current,
1095: $$prog_state{'max'},
1096: $min,
1097: $sec,
1098: $lasttime);
1099:
1.230 bisitz 1100: &r_print($r,&Apache::lonhtmlcommon::scripttag(
1.218 bisitz 1101: $$prog_state{'window'}.'.document.'.
1.230 bisitz 1102: $$prog_state{'formname'}.'.'.
1.278 bisitz 1103: $$prog_state{'inputname'}.'.value="'.$timeinfo.'";'
1.230 bisitz 1104: ));
1.23 matthew 1105: $$prog_state{'laststart'}=&Time::HiRes::time();
1.7 stredwic 1106: }
1107:
1108: # close Progress Line
1109: sub Close_PrgWin {
1.14 albertel 1110: my ($r,$prog_state)=@_;
1.49 albertel 1111: if ($$prog_state{'type'} eq 'popup') {
1.230 bisitz 1112: &r_print($r,&Apache::lonhtmlcommon::scripttag(
1113: 'popwin.close()'
1114: ));
1.49 albertel 1115: } elsif ($$prog_state{'type'} eq 'inline') {
1116: &Update_PrgWin($r,$prog_state,&mt('Done'));
1117: }
1.48 albertel 1118: undef(%$prog_state);
1119: }
1120:
1121: sub r_print {
1122: my ($r,$to_print)=@_;
1123: if ($r) {
1124: $r->print($to_print);
1125: $r->rflush();
1.47 sakharuk 1126: } else {
1.48 albertel 1127: print($to_print);
1.47 sakharuk 1128: }
1.1 stredwic 1129: }
1.34 www 1130:
1131: # ------------------------------------------------------- Puts directory header
1132:
1133: sub crumbs {
1.252 bisitz 1134: my ($uri,$target,$prefix,$form,$skiplast)=@_;
1.100 raeburn 1135: if ($target) {
1136: $target = ' target="'.
1137: &Apache::loncommon::escape_single($target).'"';
1138: }
1.252 bisitz 1139: my $output='<span class="LC_filename">';
1140: $output.=$prefix.'/';
1.249 raeburn 1141: if (($env{'user.adv'}) || ($env{'user.author'})) {
1.252 bisitz 1142: my $path=$prefix.'/';
1143: foreach my $dir (split('/',$uri)) {
1.99 matthew 1144: if (! $dir) { next; }
1145: $path .= $dir;
1.252 bisitz 1146: if ($path eq $uri) {
1147: if ($skiplast) {
1148: $output.=$dir;
1.132 www 1149: last;
1.252 bisitz 1150: }
1151: } else {
1152: $path.='/';
1153: }
1.157 albertel 1154: my $href_path = &HTML::Entities::encode($path,'<>&"');
1.252 bisitz 1155: &Apache::loncommon::inhibit_menu_check(\$href_path);
1156: if ($form) {
1157: my $href = 'javascript:'.$form.".action='".$href_path."';".$form.'.submit();';
1158: $output.=qq{<a href="$href"$target>$dir</a>/};
1159: } else {
1160: $output.=qq{<a href="$href_path"$target>$dir</a>/};
1161: }
1162: }
1.35 www 1163: } else {
1.252 bisitz 1164: foreach my $dir (split('/',$uri)) {
1.149 albertel 1165: if (! $dir) { next; }
1.252 bisitz 1166: $output.=$dir.'/';
1167: }
1.34 www 1168: }
1.149 albertel 1169: if ($uri !~ m|/$|) { $output=~s|/$||; }
1.252 bisitz 1170: $output.='</span>';
1171:
1172: return $output;
1.34 www 1173: }
1174:
1.85 www 1175: # --------------------- A function that generates a window for the spellchecker
1176:
1177: sub spellheader {
1.123 albertel 1178: my $start_page=
1179: &Apache::loncommon::start_page('Speller Suggestions',undef,
1.140 albertel 1180: {'only_body' => 1,
1181: 'js_ready' => 1,
1182: 'bgcolor' => '#DDDDDD',
1183: 'add_entries' => {
1184: 'onload' =>
1185: 'document.forms.spellcheckform.submit()',
1186: }
1187: });
1.123 albertel 1188: my $end_page=
1189: &Apache::loncommon::end_page({'js_ready' => 1});
1190:
1.105 www 1191: my $nothing=&javascript_nothing();
1.85 www 1192: return (<<ENDCHECK);
1193: <script type="text/javascript">
1.218 bisitz 1194: // <![CDATA[
1.92 albertel 1195: //<!-- BEGIN LON-CAPA Internal
1.85 www 1196: var checkwin;
1197:
1.140 albertel 1198: function spellcheckerwindow(string) {
1199: var esc_string = string.replace(/\"/g,'"');
1.105 www 1200: checkwin=window.open($nothing,'spellcheckwin','height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
1.154 albertel 1201: checkwin.document.writeln('$start_page<form name="spellcheckform" action="/adm/spellcheck" method="post"><input type="hidden" name="text" value="'+esc_string+'" /><\\/form>$end_page');
1.85 www 1202: checkwin.document.close();
1203: }
1.92 albertel 1204: // END LON-CAPA Internal -->
1.218 bisitz 1205: // ]]>
1.85 www 1206: </script>
1207: ENDCHECK
1208: }
1209:
1210: # ---------------------------------- Generate link to spell checker for a field
1211:
1212: sub spelllink {
1213: my ($form,$field)=@_;
1214: my $linktext=&mt('Check Spelling');
1215: return (<<ENDLINK);
1.140 albertel 1216: <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>
1.85 www 1217: ENDLINK
1218: }
1219:
1.281 raeburn 1220: # ------------------------------------------------- Output headers for CKEditor
1.124 albertel 1221:
1.52 www 1222: sub htmlareaheaders {
1.255 faziophi 1223: my $s="";
1.260 faziophi 1224: if (&htmlareabrowser()) {
1.255 faziophi 1225: $s.=(<<ENDEDITOR);
1226: <script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
1227: ENDEDITOR
1228: }
1229: $s.=(<<ENDJQUERY);
1230: <script type="text/javascript" src="/adm/jQuery/js/jquery-1.3.2.min.js"></script>
1231: <script type="text/javascript" src="/adm/jQuery/js/jquery-ui-1.7.2.custom.min.js"></script>
1.259 faziophi 1232: <link rel="stylesheet" type="text/css" href="/adm/jQuery/css/smoothness/jquery-ui-1.7.2.custom.css" />
1.255 faziophi 1233: ENDJQUERY
1234: return $s;
1.52 www 1235: }
1236:
1.76 www 1237: # ----------------------------------------------------------------- Preferences
1238:
1.167 albertel 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:
1.72 www 1248: # ----------------------------------------- Script to activate only some fields
1249:
1250: sub htmlareaselectactive {
1.281 raeburn 1251: my ($args) = @_;
1.76 www 1252: unless (&htmlareabrowser()) { return ''; }
1.262 raeburn 1253: my $output='<script type="text/javascript" defer="defer">'."\n"
1.230 bisitz 1254: .'// <![CDATA['."\n";
1.167 albertel 1255: my $lang = &htmlarea_lang();
1.281 raeburn 1256: my $fullpage = 'false';
1.282 raeburn 1257: my ($dragmath_prefix,$dragmath_helpicon,$dragmath_whitespace);
1.281 raeburn 1258: if (ref($args) eq 'HASH') {
1259: if (exists($args->{'lang'})) {
1260: if ($args->{'lang'} ne '') {
1261: $lang = $args->{'lang'};
1262: }
1263: }
1264: if (exists($args->{'fullpage'})) {
1265: if ($args->{'fullpage'} eq 'true') {
1266: $fullpage = $args->{'fullpage'};
1267: }
1268: }
1269: if (exists($args->{'dragmath'})) {
1270: if ($args->{'dragmath'} ne '') {
1271: $dragmath_prefix = $args->{'dragmath'};
1.282 raeburn 1272: $dragmath_helpicon=&Apache::loncommon::lonhttpdurl("/adm/help/help.png");
1273: $dragmath_whitespace=&Apache::loncommon::lonhttpdurl("/adm/lonIcons/transparent1x1.gif");
1.281 raeburn 1274: }
1275: }
1276: }
1.255 faziophi 1277: $output.='
1278:
1279: function containsBlockHtml(id) {
1.281 raeburn 1280: var re = $("#"+id).html().search(/(?:\<\;|\<)(br|h1|h2|h3|h4|h5|h6|p|ol|ul|table|pre|address|blockquote|center|div)[\s]*((?:[\/]*[\s]*(?:\>\;|\>)|(?:\>\;|\>)[\s\S]*(?:\<\;|\<)\/[\s]*\1[\s]*\(?:\>\;|\>))/im);
1.255 faziophi 1281: return (re >= 0);
1282: }
1283:
1284: function startRichEditor(id) {
1285: CKEDITOR.replace(id,
1286: {
1.281 raeburn 1287: customConfig: "/ckeditor/loncapaconfig.js",
1288: language : "'.$lang.'",
1289: fullPage : '.$fullpage.',
1.255 faziophi 1290: }
1291: );
1292: }
1293:
1294: function destroyRichEditor(id) {
1295: CKEDITOR.instances[id].destroy();
1.72 www 1296: }
1.255 faziophi 1297:
1298: function editorHandler(event) {
1299: var rawid = $(this).attr("id");
1.281 raeburn 1300: var id = new RegExp("LC_rt_(.*)").exec(rawid)[1];
1.255 faziophi 1301: event.preventDefault();
1.281 raeburn 1302: var rt_enabled = $(this).hasClass("LC_enable_rt");
1303: if (rt_enabled) {
1.255 faziophi 1304: startRichEditor(id);
1305: $("#LC_rt_"+id).html("<b>« Plain text</b>");
1306: $("#LC_rt_"+id).attr("title", "Disable rich text formatting and edit in plain text");
1307: $("#LC_rt_"+id).addClass("LC_disable_rt");
1308: $("#LC_rt_"+id).removeClass("LC_enable_rt");
1309: } else {
1310: destroyRichEditor(id);
1311: $("#LC_rt_"+id).html("<b>Rich formatting »</b>");
1312: $("#LC_rt_"+id).attr("title", "Enable rich text formatting (bold, italic, etc.)");
1313: $("#LC_rt_"+id).addClass("LC_enable_rt");
1314: $("#LC_rt_"+id).removeClass("LC_disable_rt");
1.281 raeburn 1315: }';
1316: if ($dragmath_prefix ne '') {
1317: $output .= "\n var visible = '';
1318: if (rt_enabled) {
1319: visible = 'none';
1320: }
1321: editmath_visibility(id,visible);\n";
1322: }
1323: $output .= '
1324: }
1.255 faziophi 1325: $(document).ready(function(){
1326: $(".LC_richAlwaysOn").each(function() {
1327: startRichEditor($(this).attr("id"));
1328: });
1329: $(".LC_richDetectHtml").each(function() {
1330: var id = $(this).attr("id");
1.281 raeburn 1331: var rt_enabled = containsBlockHtml(id);
1332: if(rt_enabled) {
1.255 faziophi 1333: $(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Disable rich text formatting and edit in plain text\" class=\"LC_disable_rt\"><b>« Plain text</b></a></div>");
1334: startRichEditor(id);
1.281 raeburn 1335: $("#LC_rt_"+id).click(editorHandler);
1.255 faziophi 1336: }
1337: else {
1338: $(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Enable rich text formatting (bold, italic, etc.)\" class=\"LC_enable_rt\"><b>Rich formatting »</b></a></div>");
1339: $("#LC_rt_"+id).click(editorHandler);
1.281 raeburn 1340: }';
1341: if ($dragmath_prefix ne '') {
1342: $output .= "\n var visible = '';
1343: if (rt_enabled) {
1344: visible = 'none';
1345: }
1346: editmath_visibility(id,visible);\n";
1347: }
1348: $output .= '
1.255 faziophi 1349: });
1350: $(".LC_richDefaultOn").each(function() {
1351: var id = $(this).attr("id");
1352: $(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Disable rich text formatting and edit in plain text\" class=\"LC_disable_rt\"><b>« Plain text</b></a></div>");
1353: startRichEditor(id);
1354: $("#LC_rt_"+id).click(editorHandler);
1355: });
1356: $(".LC_richDefaultOff").each(function() {
1357: var id = $(this).attr("id");
1358: $(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Enable rich text formatting (bold, italic, etc.)\" class=\"LC_enable_rt\"><b>Rich formatting »</b></a></div>");
1.281 raeburn 1359: $("#LC_rt_"+id).click(editorHandler);
1.255 faziophi 1360: });
1361: });
1.281 raeburn 1362: ';
1363: if ($dragmath_prefix ne '') {
1364: $output .= '
1365:
1366: function editmath_visibility(id,value) {
1367:
1368: if ((id == "") || (id == null)) {
1369: return;
1370: }
1371: var mathid = "'.$dragmath_prefix.'_"+id;
1372: mathele = document.getElementById(mathid);
1373: if (mathele == null) {
1374: return;
1375: }
1376: mathele.style.display = value;
1.282 raeburn 1377: var mathhelpicon = "'.$dragmath_prefix.'helpicon'.'_"+id;
1378: mathhelpiconele = document.getElementById(mathhelpicon);
1379: if (mathhelpiconele == null) {
1380: return;
1381: }
1382: if (value == "none") {
1383: mathhelpiconele.src = "'.$dragmath_whitespace.'";
1384: } else {
1385: mathhelpiconele.src = "'.$dragmath_helpicon.'";
1386: }
1.281 raeburn 1387: }
1388: ';
1389:
1390: }
1.218 bisitz 1391: $output.="\nwindow.status='Activated Editfields';\n"
1.230 bisitz 1392: .'// ]]>'."\n"
1.281 raeburn 1393: .'</script>';
1.72 www 1394: return $output;
1395: }
1396:
1.61 www 1397: # --------------------------------------------------------------------- Blocked
1398:
1399: sub htmlareablocked {
1.104 albertel 1400: unless ($env{'environment.wysiwygeditor'} eq 'on') { return 1; }
1.71 www 1401: return 0;
1.52 www 1402: }
1403:
1404: # ---------------------------------------- Browser capable of running HTMLArea?
1405:
1406: sub htmlareabrowser {
1407: return 1;
1408: }
1.53 matthew 1409:
1410: ############################################################
1411: ############################################################
1412:
1413: =pod
1414:
1415: =item breadcrumbs
1416:
1417: Compiles the previously registered breadcrumbs into an series of links.
1418: Additionally supports a 'component', which will be displayed on the
1.223 droeschl 1419: right side of the breadcrumbs enclosing div (without a link).
1.53 matthew 1420: A link to help for the component will be included if one is specified.
1421:
1422: All inputs can be undef without problems.
1423:
1.223 droeschl 1424: Inputs: $component (the text on the right side of the breadcrumbs trail),
1.53 matthew 1425: $component_help
1.63 albertel 1426: $menulink (boolean, controls whether to include a link to /adm/menu)
1.138 albertel 1427: $helplink (if 'nohelp' don't include the orange help link)
1428: $css_class (optional name for the class to apply to the table for CSS)
1.197 raeburn 1429: $no_mt (optional flag, 1 if &mt() is _not_ to be applied to $component
1430: when including the text on the right.
1.53 matthew 1431: Returns a string containing breadcrumbs for the current page.
1432:
1433: =item clear_breadcrumbs
1434:
1435: Clears the previously stored breadcrumbs.
1436:
1437: =item add_breadcrumb
1438:
1439: Pushes a breadcrumb on the stack of crumbs.
1440:
1441: input: $breadcrumb, a hash reference. The keys 'href','title', and 'text'
1442: are required. If present the keys 'faq' and 'bug' will be used to provide
1.156 albertel 1443: links to the FAQ and bug sites. If the key 'no_mt' is present the 'title'
1444: and 'text' values won't be sent through &mt()
1.53 matthew 1445:
1446: returns: nothing
1447:
1448: =cut
1449:
1450: ############################################################
1451: ############################################################
1452: {
1453: my @Crumbs;
1.242 droeschl 1454: my %tools = ();
1.57 matthew 1455:
1.53 matthew 1456: sub breadcrumbs {
1.216 bisitz 1457: my ($component,$component_help,$menulink,$helplink,$css_class,$no_mt, $CourseBreadcrumbs) = @_;
1.53 matthew 1458: #
1.215 droeschl 1459: $css_class ||= 'LC_breadcrumbs';
1.205 amueller 1460:
1.57 matthew 1461: # Make the faq and bug data cascade
1.223 droeschl 1462: my $faq = '';
1463: my $bug = '';
1464: my $help = '';
1.215 droeschl 1465: # Crumb Symbol
1.223 droeschl 1466: my $crumbsymbol = '»';
1.60 www 1467: # The last breadcrumb does not have a link, so handle it separately.
1.53 matthew 1468: my $last = pop(@Crumbs);
1.57 matthew 1469: #
1.70 matthew 1470: # The first one should be the course or a menu link
1.215 droeschl 1471: if (!defined($menulink)) { $menulink=1; }
1.70 matthew 1472: if ($menulink) {
1473: my $description = 'Menu';
1.172 raeburn 1474: my $no_mt_descr = 0;
1.269 raeburn 1475: if ((exists($env{'request.course.id'})) &&
1476: ($env{'request.course.id'} ne '') &&
1477: ($env{'course.'.$env{'request.course.id'}.'.description'} ne '')) {
1.70 matthew 1478: $description =
1.104 albertel 1479: $env{'course.'.$env{'request.course.id'}.'.description'};
1.172 raeburn 1480: $no_mt_descr = 1;
1.70 matthew 1481: }
1.215 droeschl 1482: $menulink = { href =>'/adm/menu',
1483: title =>'Go to main menu',
1484: target =>'_top',
1485: text =>$description,
1486: no_mt =>$no_mt_descr, };
1487: if($last) {
1488: #$last set, so we have some crumbs
1489: unshift(@Crumbs,$menulink);
1490: } else {
1491: #only menulink crumb present
1492: $last = $menulink;
1493: }
1.53 matthew 1494: }
1.219 droeschl 1495: my $links = join "",
1.261 droeschl 1496: map {
1497: $faq = $_->{'faq'} if (exists($_->{'faq'}));
1498: $bug = $_->{'bug'} if (exists($_->{'bug'}));
1499: $help = $_->{'help'} if (exists($_->{'help'}));
1500:
1501: my $result = $_->{no_mt} ? $_->{text} : mt($_->{text});
1502:
1503: if ($_->{href}){
1504: $result = htmltag( 'a', $result,
1505: { href => $_->{href},
1506: title => $_->{no_mt} ? $_->{title} : mt($_->{title}),
1507: target => $_->{target}, });
1508: }
1509:
1510: $result = htmltag( 'li', "$result $crumbsymbol");
1511: } @Crumbs;
1.223 droeschl 1512:
1513: #should the last Element be translated?
1.261 droeschl 1514:
1515: my $lasttext = $last->{'no_mt'} ? $last->{'text'}
1516: : mt( $last->{'text'} );
1517:
1.274 droeschl 1518: # last breadcrumb is the first order heading of a page
1519: # for course breadcrumbs it's just bold
1520: $links .= htmltag( 'li', htmltag($CourseBreadcrumbs ? 'b' : 'h1',
1521: $lasttext), {title => $lasttext});
1.223 droeschl 1522:
1.54 matthew 1523: my $icons = '';
1.223 droeschl 1524: $faq = $last->{'faq'} if (exists($last->{'faq'}));
1525: $bug = $last->{'bug'} if (exists($last->{'bug'}));
1.106 www 1526: $help = $last->{'help'} if (exists($last->{'help'}));
1527: $component_help=($component_help?$component_help:$help);
1.145 albertel 1528: # if ($faq ne '') {
1529: # $icons .= &Apache::loncommon::help_open_faq($faq);
1530: # }
1.79 raeburn 1531: # if ($bug ne '') {
1532: # $icons .= &Apache::loncommon::help_open_bug($bug);
1533: # }
1.223 droeschl 1534: if ($faq ne '' || $component_help ne '' || $bug ne '') {
1535: $icons .= &Apache::loncommon::help_open_menu($component,
1536: $component_help,
1537: $faq,$bug);
1538: }
1.54 matthew 1539: #
1.205 amueller 1540:
1541:
1.223 droeschl 1542: unless ($CourseBreadcrumbs) {
1543: $links = htmltag('ol', $links, { id => "LC_MenuBreadcrumbs" });
1544: } else {
1.227 bisitz 1545: $links = htmltag('ul', $links, { class => "LC_CourseBreadcrumbs" });
1.53 matthew 1546: }
1.223 droeschl 1547:
1548: if ($component) {
1549: $links = htmltag('span',
1550: ( $no_mt ? $component : mt($component) ).
1551: ( $icons ? $icons : '' ),
1552: { class => 'LC_breadcrumbs_component' } )
1553: .$links;
1554: }
1555:
1.261 droeschl 1556: render_tools(\$links);
1.223 droeschl 1557: $links = htmltag('div', $links,
1.225 bisitz 1558: { id => "LC_breadcrumbs" }) unless ($CourseBreadcrumbs) ;
1.261 droeschl 1559: render_advtools(\$links);
1.223 droeschl 1560:
1.53 matthew 1561: # Return the @Crumbs stack to what we started with
1562: push(@Crumbs,$last);
1563: shift(@Crumbs);
1.223 droeschl 1564: # Return the breadcrumb's line
1565: return "$links";
1.53 matthew 1566: }
1567:
1568: sub clear_breadcrumbs {
1569: undef(@Crumbs);
1.242 droeschl 1570: undef(%tools);
1.53 matthew 1571: }
1572:
1573: sub add_breadcrumb {
1.232 raeburn 1574: push(@Crumbs,@_);
1.53 matthew 1575: }
1.242 droeschl 1576:
1.261 droeschl 1577: =item add_breadcrumb_tool($category, $html)
1578:
1579: Adds $html to $category of the breadcrumb toolbar container.
1580:
1581: $html is usually a link to a page that invokes a function on the currently
1582: displayed data (e.g. print when viewing a problem)
1583:
1584: Currently there are 3 possible values for $category:
1585:
1586: =over
1587:
1588: =item navigation
1589: left of breadcrumbs line
1590:
1591: =item tools
1592: right of breadcrumbs line
1593:
1594: =item advtools
1595: advanced tools shown in a separate box below breadcrumbs line
1596:
1597: =back
1598:
1599: returns: nothing
1600:
1601: =cut
1.242 droeschl 1602:
1603: sub add_breadcrumb_tool {
1.261 droeschl 1604: my ($category, @html) = @_;
1605: return unless @html;
1.285 raeburn 1606: if (!keys(%tools)) {
1.261 droeschl 1607: %tools = ( navigation => [], tools => [], advtools => []);
1.242 droeschl 1608: }
1.261 droeschl 1609:
1610: #this cleans data received from lonmenu::innerregister
1611: @html = grep {defined $_ && $_ ne ''} @html;
1612: for (@html) {
1613: s/align="(right|left)"//;
1.285.2.1! raeburn 1614: if (($category ne 'advtools') && ($category ne 'tools')) {
! 1615: s/<span.*?\/span>//;
! 1616: }
1.261 droeschl 1617: }
1618:
1619: push @{$tools{$category}}, @html;
1.242 droeschl 1620: }
1621:
1.261 droeschl 1622: =item clear_breadcrumb_tools()
1623:
1624: Clears the breadcrumb toolbar container.
1625:
1626: returns: nothing
1627:
1628: =cut
1629:
1.245 droeschl 1630: sub clear_breadcrumb_tools {
1631: undef(%tools);
1632: }
1633:
1.261 droeschl 1634: =item render_tools(\$breadcrumbs)
1635:
1636: Creates html for breadcrumb tools (categories navigation and tools) and inserts
1637: \$breadcrumbs at the correct position.
1638:
1639: input: \$breadcrumbs - a reference to the string containing prepared
1640: breadcrumbs.
1641:
1642: returns: nothing
1643: =cut
1644:
1645: #TODO might split this in separate functions for each category
1646: sub render_tools {
1647: my ($breadcrumbs) = @_;
1.285 raeburn 1648: return unless (keys(%tools));
1.261 droeschl 1649:
1650: my $navigation = list_from_array($tools{navigation},
1651: { listattr => { class=>"LC_breadcrumb_tools_navigation" } });
1652: my $tools = list_from_array($tools{tools},
1653: { listattr => { class=>"LC_breadcrumb_tools_tools" } });
1654: $$breadcrumbs = list_from_array([$navigation, $tools, $$breadcrumbs],
1655: { listattr => { class=>'LC_breadcrumb_tools_outerlist' } });
1656: }
1657:
1658: =item render_advtools(\$breadcrumbs)
1659:
1660: Creates html for advanced tools (category advtools) and inserts \$breadcrumbs
1661: at the correct position.
1662:
1663: input: \$breadcrumbs - a reference to the string containing prepared
1664: breadcrumbs (after render_tools call).
1665:
1666: returns: nothing
1667: =cut
1668:
1669: sub render_advtools {
1670: my ($breadcrumbs) = @_;
1671: return unless (defined $tools{'advtools'})
1672: and (scalar(@{$tools{'advtools'}}) > 0);
1673:
1674: $$breadcrumbs .= Apache::loncommon::head_subbox(
1675: funclist_from_array($tools{'advtools'}) );
1.242 droeschl 1676: }
1.53 matthew 1677:
1.57 matthew 1678: } # End of scope for @Crumbs
1.53 matthew 1679:
1680: ############################################################
1681: ############################################################
1682:
1.112 raeburn 1683: # Nested table routines.
1684: #
1685: # Routines to display form items in a multi-row table with 2 columns.
1686: # Uses nested tables to divide form elements into segments.
1687: # For examples of use see loncom/interface/lonnotify.pm
1688: #
1689: # Can be used in following order: ...
1690: # &start_pick_box()
1691: # row1
1692: # row2
1693: # row3 ... etc.
1.173 raeburn 1694: # &submit_row()
1.161 raeburn 1695: # &end_pick_box()
1.112 raeburn 1696: #
1697: # where row1, row 2 etc. are chosen from &role_select_row,&course_select_row,
1698: # &status_select_row and &email_default_row
1699: #
1700: # Can also be used in following order:
1701: #
1702: # &start_pick_box()
1703: # &row_title()
1704: # &row_closure()
1705: # &row_title()
1706: # &row_closure() ... etc.
1707: # &submit_row()
1708: # &end_pick_box()
1709: #
1710: # In general a &submit_row() call should proceed the call to &end_pick_box(),
1711: # as this routine adds a button for form submission.
1.113 raeburn 1712: # &submit_row() does not require a &row_closure after it.
1.112 raeburn 1713: #
1714: # &start_pick_box() creates a bounding table with 1-pixel wide black border.
1715: # rows should be placed between calls to &start_pick_box() and &end_pick_box.
1716: #
1717: # &row_title() adds a title in the left column for each segment.
1718: # &row_closure() closes a row with a 1-pixel wide black line.
1719: #
1720: # &role_select_row() provides a select box from which to choose 1 or more roles
1721: # &course_select_row provides ways of picking groups of courses
1722: # radio buttons: all, by category or by picking from a course picker pop-up
1723: # note: by category option is only displayed if a domain has implemented
1724: # selection by year, semester, department, number etc.
1725: #
1726: # &status_select_row() provides a select box from which to choose 1 or more
1727: # access types (current access, prior access, and future access)
1728: #
1729: # &email_default_row() provides text boxes for default e-mail suffixes for
1730: # different authentication types in a domain.
1731: #
1732: # &row_title() and &row_closure() are called internally by the &*_select_row
1733: # routines, but can also be called directly to start and end rows which have
1734: # needs that are not accommodated by the *_select_row() routines.
1735:
1.193 bisitz 1736: { # Start: row_count block for pick_box
1737: my @row_count;
1738:
1.112 raeburn 1739: sub start_pick_box {
1.142 albertel 1740: my ($css_class) = @_;
1741: if (defined($css_class)) {
1742: $css_class = 'class="'.$css_class.'"';
1743: } else {
1744: $css_class= 'class="LC_pick_box"';
1745: }
1.193 bisitz 1746: unshift(@row_count,0);
1.112 raeburn 1747: my $output = <<"END";
1.142 albertel 1748: <table $css_class>
1.112 raeburn 1749: END
1750: return $output;
1751: }
1752:
1753: sub end_pick_box {
1.193 bisitz 1754: shift(@row_count);
1.112 raeburn 1755: my $output = <<"END";
1756: </table>
1757: END
1758: return $output;
1759: }
1760:
1.181 bisitz 1761: sub row_headline {
1762: my $output = <<"END";
1763: <tr><td colspan="2">
1764: END
1765: return $output;
1766: }
1767:
1.112 raeburn 1768: sub row_title {
1.243 amueller 1769: my ($title,$css_title_class,$css_value_class, $css_value_furtherAttributes) = @_;
1.193 bisitz 1770: $row_count[0]++;
1771: my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.142 albertel 1772: $css_title_class ||= 'LC_pick_box_title';
1773: $css_title_class = 'class="'.$css_title_class.'"';
1774:
1775: $css_value_class ||= 'LC_pick_box_value';
1776:
1.173 raeburn 1777: if ($title ne '') {
1778: $title .= ':';
1779: }
1.112 raeburn 1780: my $output = <<"ENDONE";
1.243 amueller 1781: <tr class="LC_pick_box_row" $css_value_furtherAttributes>
1.142 albertel 1782: <td $css_title_class>
1.173 raeburn 1783: $title
1.112 raeburn 1784: </td>
1.193 bisitz 1785: <td class="$css_value_class $css_class">
1.112 raeburn 1786: ENDONE
1787: return $output;
1788: }
1789:
1790: sub row_closure {
1.143 albertel 1791: my ($no_separator) =@_;
1.113 raeburn 1792: my $output = <<"ENDTWO";
1.112 raeburn 1793: </td>
1794: </tr>
1.143 albertel 1795: ENDTWO
1796: if (!$no_separator) {
1797: $output .= <<"ENDTWO";
1.112 raeburn 1798: <tr>
1.143 albertel 1799: <td colspan="2" class="LC_pick_box_separator">
1.112 raeburn 1800: </td>
1801: </tr>
1802: ENDTWO
1.143 albertel 1803: }
1.112 raeburn 1804: return $output;
1805: }
1806:
1.193 bisitz 1807: } # End: row_count block for pick_box
1808:
1.112 raeburn 1809: sub role_select_row {
1.147 raeburn 1810: my ($roles,$title,$css_class,$show_separate_custom,$cdom,$cnum) = @_;
1.236 raeburn 1811: my $crstype = 'Course';
1812: if ($cdom ne '' && $cnum ne '') {
1813: $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
1814: }
1.116 raeburn 1815: my $output;
1816: if (defined($title)) {
1.142 albertel 1817: $output = &row_title($title,$css_class);
1.116 raeburn 1818: }
1.142 albertel 1819: $output .= qq|
1.198 bisitz 1820: <select name="roles" multiple="multiple">\n|;
1.113 raeburn 1821: foreach my $role (@$roles) {
1.114 raeburn 1822: my $plrole;
1823: if ($role eq 'ow') {
1824: $plrole = &mt('Course Owner');
1.147 raeburn 1825: } elsif ($role eq 'cr') {
1826: if ($show_separate_custom) {
1827: if ($cdom ne '' && $cnum ne '') {
1828: my %course_customroles = &course_custom_roles($cdom,$cnum);
1829: foreach my $crrole (sort(keys(%course_customroles))) {
1830: my ($plcrrole) = ($crrole =~ m|^cr/[^/]+/[^/]+/(.+)$|);
1831: $output .= ' <option value="'.$crrole.'">'.$plcrrole.
1832: '</option>';
1833: }
1834: }
1835: } else {
1836: $plrole = &mt('Custom Role');
1837: }
1.114 raeburn 1838: } else {
1.236 raeburn 1839: $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.114 raeburn 1840: }
1.147 raeburn 1841: if (($role ne 'cr') || (!$show_separate_custom)) {
1842: $output .= ' <option value="'.$role.'">'.$plrole.'</option>';
1843: }
1.112 raeburn 1844: }
1.142 albertel 1845: $output .= qq| </select>\n|;
1.116 raeburn 1846: if (defined($title)) {
1847: $output .= &row_closure();
1848: }
1.112 raeburn 1849: return $output;
1850: }
1851:
1852: sub course_select_row {
1.142 albertel 1853: my ($title,$formname,$totcodes,$codetitles,$idlist,$idlist_titles,
1.280 raeburn 1854: $css_class,$crstype,$standardnames) = @_;
1.142 albertel 1855: my $output = &row_title($title,$css_class);
1.280 raeburn 1856: $output .= &course_selection($formname,$totcodes,$codetitles,$idlist,$idlist_titles,$crstype,$standardnames);
1.169 raeburn 1857: $output .= &row_closure();
1858: return $output;
1859: }
1860:
1861: sub course_selection {
1.280 raeburn 1862: my ($formname,$totcodes,$codetitles,$idlist,$idlist_titles,$crstype,$standardnames) = @_;
1.169 raeburn 1863: my $output = qq|
1.142 albertel 1864: <script type="text/javascript">
1.218 bisitz 1865: // <![CDATA[
1.112 raeburn 1866: function coursePick (formname) {
1867: for (var i=0; i<formname.coursepick.length; i++) {
1.114 raeburn 1868: if (formname.coursepick[i].value == 'category') {
1869: courseSet('');
1870: }
1.112 raeburn 1871: if (!formname.coursepick[i].checked) {
1872: if (formname.coursepick[i].value == 'specific') {
1873: formname.coursetotal.value = 0;
1874: formname.courselist = '';
1875: }
1876: }
1877: }
1878: }
1.114 raeburn 1879: function setPick (formname) {
1880: for (var i=0; i<formname.coursepick.length; i++) {
1881: if (formname.coursepick[i].value == 'category') {
1882: formname.coursepick[i].checked = true;
1883: }
1884: formname.coursetotal.value = 0;
1885: formname.courselist = '';
1886: }
1887: }
1.218 bisitz 1888: // ]]>
1.112 raeburn 1889: </script>
1890: |;
1.237 raeburn 1891:
1892: my ($allcrs,$pickspec);
1893: if ($crstype eq 'Community') {
1894: $allcrs = &mt('All communities');
1895: $pickspec = &mt('Pick specific communities:');
1896: } else {
1897: $allcrs = &mt('All courses');
1898: $pickspec = &mt('Pick specific course(s):');
1899: }
1900:
1.112 raeburn 1901: my $courseform='<b>'.&Apache::loncommon::selectcourse_link
1.237 raeburn 1902: ($formname,'pickcourse','pickdomain','coursedesc','',1,$crstype).'</b>';
1903: $output .= '<input type="radio" name="coursepick" value="all" onclick="coursePick(this.form)" />'.$allcrs.'<br />';
1.112 raeburn 1904: if ($totcodes > 0) {
1905: my $numtitles = @$codetitles;
1906: if ($numtitles > 0) {
1.129 raeburn 1907: $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 />';
1.112 raeburn 1908: $output .= '<table><tr><td>'.$$codetitles[0].'<br />'."\n".
1.280 raeburn 1909: '<select name="'.$standardnames->[0].
1.114 raeburn 1910: '" onChange="setPick(this.form);courseSet('."'$$codetitles[0]'".')">'."\n".
1.112 raeburn 1911: ' <option value="-1" />Select'."\n";
1912: my @items = ();
1913: my @longitems = ();
1914: if ($$idlist{$$codetitles[0]} =~ /","/) {
1.113 raeburn 1915: @items = split(/","/,$$idlist{$$codetitles[0]});
1.112 raeburn 1916: } else {
1917: $items[0] = $$idlist{$$codetitles[0]};
1918: }
1919: if (defined($$idlist_titles{$$codetitles[0]})) {
1920: if ($$idlist_titles{$$codetitles[0]} =~ /","/) {
1.113 raeburn 1921: @longitems = split(/","/,$$idlist_titles{$$codetitles[0]});
1.112 raeburn 1922: } else {
1923: $longitems[0] = $$idlist_titles{$$codetitles[0]};
1924: }
1925: for (my $i=0; $i<@longitems; $i++) {
1926: if ($longitems[$i] eq '') {
1927: $longitems[$i] = $items[$i];
1928: }
1929: }
1930: } else {
1931: @longitems = @items;
1932: }
1933: for (my $i=0; $i<@items; $i++) {
1934: $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
1935: }
1936: $output .= '</select></td>';
1937: for (my $i=1; $i<$numtitles; $i++) {
1938: $output .= '<td>'.$$codetitles[$i].'<br />'."\n".
1.280 raeburn 1939: '<select name="'.$standardnames->[$i].
1.112 raeburn 1940: '" onChange="courseSet('."'$$codetitles[$i]'".')">'."\n".
1941: '<option value="-1"><-Pick '.$$codetitles[$i-1].'</option>'."\n".
1942: '</select>'."\n".
1943: '</td>';
1944: }
1945: $output .= '</tr></table><br />';
1946: }
1947: }
1.238 raeburn 1948: $output .= '<input type="radio" name="coursepick" value="specific" onclick="coursePick(this.form);opencrsbrowser('."'".$formname."','dccourse','dcdomain','coursedesc','','1','$crstype'".')" />'.$pickspec.' '.$courseform.' <input type="text" value="0" size="4" name="coursetotal" /><input type="hidden" name="courselist" value="" />selected.<br />'."\n";
1.112 raeburn 1949: return $output;
1950: }
1951:
1952: sub status_select_row {
1.142 albertel 1953: my ($types,$title,$css_class) = @_;
1.117 raeburn 1954: my $output;
1955: if (defined($title)) {
1.142 albertel 1956: $output = &row_title($title,$css_class,'LC_pick_box_select');
1.117 raeburn 1957: }
1.142 albertel 1958: $output .= qq|
1.198 bisitz 1959: <select name="types" multiple="multiple">\n|;
1.113 raeburn 1960: foreach my $status_type (sort(keys(%{$types}))) {
1.112 raeburn 1961: $output .= ' <option value="'.$status_type.'">'.$$types{$status_type}.'</option>';
1962: }
1.142 albertel 1963: $output .= qq| </select>\n|;
1.117 raeburn 1964: if (defined($title)) {
1965: $output .= &row_closure();
1966: }
1.112 raeburn 1967: return $output;
1968: }
1969:
1970: sub email_default_row {
1.142 albertel 1971: my ($authtypes,$title,$descrip,$css_class) = @_;
1972: my $output = &row_title($title,$css_class);
1973: $output .= $descrip.
1974: &Apache::loncommon::start_data_table().
1975: &Apache::loncommon::start_data_table_header_row().
1976: '<th>'.&mt('Authentication Method').'</th>'.
1977: '<th align="right">'.&mt('Username -> e-mail conversion').'</th>'."\n".
1978: &Apache::loncommon::end_data_table_header_row();
1.112 raeburn 1979: my $rownum = 0;
1.113 raeburn 1980: foreach my $auth (sort(keys(%{$authtypes}))) {
1.112 raeburn 1981: my ($userentry,$size);
1982: if ($auth =~ /^krb/) {
1983: $userentry = '';
1984: $size = 25;
1985: } else {
1986: $userentry = 'username@';
1987: $size = 15;
1988: }
1.142 albertel 1989: $output .= &Apache::loncommon::start_data_table_row().
1990: '<td> '.$$authtypes{$auth}.'</td>'.
1991: '<td align="right">'.$userentry.
1992: '<input type="text" name="'.$auth.'" size="'.$size.'" /></td>'.
1993: &Apache::loncommon::end_data_table_row();
1.112 raeburn 1994: }
1.142 albertel 1995: $output .= &Apache::loncommon::end_data_table();
1.112 raeburn 1996: $output .= &row_closure();
1997: return $output;
1998: }
1999:
2000:
2001: sub submit_row {
1.142 albertel 2002: my ($title,$cmd,$submit_text,$css_class) = @_;
2003: my $output = &row_title($title,$css_class,'LC_pick_box_submit');
1.112 raeburn 2004: $output .= qq|
2005: <br />
2006: <input type="hidden" name="command" value="$cmd" />
2007: <input type="submit" value="$submit_text"/>
2008: <br /><br />
1.142 albertel 2009: \n|;
1.112 raeburn 2010: return $output;
2011: }
1.1 stredwic 2012:
1.147 raeburn 2013: sub course_custom_roles {
2014: my ($cdom,$cnum) = @_;
2015: my %returnhash=();
2016: my %coursepersonnel=&Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
2017: foreach my $person (sort(keys(%coursepersonnel))) {
2018: my ($role) = ($person =~ /^([^:]+):/);
2019: my ($end,$start) = split(/:/,$coursepersonnel{$person});
2020: if ($end == -1 && $start == -1) {
2021: next;
2022: }
2023: if ($role =~ m|^cr/[^/]+/[^/]+/[^/]|) {
2024: $returnhash{$role} ++;
2025: }
2026: }
2027: return %returnhash;
2028: }
2029:
2030:
1.270 www 2031: sub resource_info_box {
2032: my ($symb,$onlyfolderflag)=@_;
2033: my $return='';
2034: if ($symb) {
2035: $return=&Apache::loncommon::start_data_table();
1.271 www 2036: my ($map,$id,$resource)=&Apache::lonnet::decode_symb($symb);
2037: my $folder=&Apache::lonnet::gettitle($map);
2038: $return.=&Apache::loncommon::start_data_table_row().
2039: '<th>'.&mt('Folder:').'</th><td>'.$folder.'</td>'.
2040: &Apache::loncommon::end_data_table_row();
1.270 www 2041: unless ($onlyfolderflag) {
2042: $return.=&Apache::loncommon::start_data_table_row().
1.271 www 2043: '<th>'.&mt('Resource:').'</th><td>'.&Apache::lonnet::gettitle($symb).'</td>'.
1.270 www 2044: &Apache::loncommon::end_data_table_row();
2045: }
1.271 www 2046: $return.=&Apache::loncommon::end_data_table();
1.270 www 2047: } else {
2048: $return='<p><span class="LC_error">'.&mt('No context provided.').'</span></p>';
2049: }
2050: return $return;
2051:
2052: }
2053:
1.119 raeburn 2054: ##############################################
2055: ##############################################
1.179 raeburn 2056:
2057: # topic_bar
2058: #
1.248 wenzelju 2059: # Generates a div containing an (optional) number with a white background followed by a
1.240 raeburn 2060: # title with a background color defined in the corresponding CSS: LC_topic_bar
2061: # Inputs:
1.248 wenzelju 2062: # 1. number to display.
2063: # If input for number is empty only the title will be displayed.
1.240 raeburn 2064: # 2. title text to display.
2065: # Outputs - a scalar containing html mark-up for the div.
2066:
1.179 raeburn 2067: sub topic_bar {
1.248 wenzelju 2068: my ($num,$title) = @_;
2069: my $number = '';
2070: if ($num ne '') {
2071: $number = '<span>'.$num.'</span>';
1.239 amueller 2072: }
1.248 wenzelju 2073: return '<div class="LC_topic_bar">'.$number.$title.'</div>';
1.179 raeburn 2074: }
2075:
2076: ##############################################
2077: ##############################################
1.119 raeburn 2078: # echo_form_input
2079: #
2080: # Generates html markup to add form elements from the referrer page
2081: # as hidden form elements (values encoded) in the new page.
2082: #
2083: # Intended to support two types of use
2084: # (a) to allow backing up to earlier pages in a multi-page
2085: # form submission process using a breadcrumb trail.
2086: #
2087: # (b) to allow the current page to be reloaded with form elements
2088: # set on previous page to remain unchanged. An example would
2089: # be where the a page containing a dynamically-built table of data is
2090: # is to be redisplayed, with only the sort order of the data changed.
2091: #
2092: # Inputs:
2093: # 1. Reference to array of form elements in the submitted form on
2094: # the referrer page which are to be excluded from the echoed elements.
2095: #
2096: # 2. Reference to array of regular expressions, which if matched in the
2097: # name of the form element n the referrer page will be omitted from echo.
2098: #
2099: # Outputs: A scalar containing the html markup for the echoed form
2100: # elements (all as hidden elements, with values encoded).
2101:
2102:
2103: sub echo_form_input {
2104: my ($excluded,$regexps) = @_;
2105: my $output = '';
2106: foreach my $key (keys(%env)) {
2107: if ($key =~ /^form\.(.+)$/) {
2108: my $name = $1;
2109: my $match = 0;
1.285 raeburn 2110: if (ref($excluded) eq 'ARRAY') {
2111: next if (grep(/^\Q$name\E$/,@{$excluded}));
2112: }
2113: if (ref($regexps) eq 'ARRAY') {
2114: if (@{$regexps} > 0) {
2115: foreach my $regexp (@{$regexps}) {
2116: if ($name =~ /$regexp/) {
2117: $match = 1;
2118: last;
1.119 raeburn 2119: }
2120: }
2121: }
1.285 raeburn 2122: }
2123: next if ($match);
2124: if (ref($env{$key}) eq 'ARRAY') {
2125: foreach my $value (@{$env{$key}}) {
2126: $value = &HTML::Entities::encode($value,'<>&"');
2127: $output .= '<input type="hidden" name="'.$name.
2128: '" value="'.$value.'" />'."\n";
1.119 raeburn 2129: }
1.285 raeburn 2130: } else {
2131: my $value = &HTML::Entities::encode($env{$key},'<>&"');
2132: $output .= '<input type="hidden" name="'.$name.
2133: '" value="'.$value.'" />'."\n";
1.119 raeburn 2134: }
2135: }
2136: }
2137: return $output;
2138: }
2139:
2140: ##############################################
2141: ##############################################
2142: # set_form_elements
2143: #
2144: # Generates javascript to set form elements to values based on
2145: # corresponding values for the same form elements when the page was
2146: # previously submitted.
2147: #
2148: # Last submission values are read from hidden form elements in referring
2149: # page which have the same name, i.e., generated by &echo_form_input().
2150: #
2151: # Intended to be called by onload event.
2152: #
1.121 raeburn 2153: # Inputs:
2154: # (a) Reference to hash of echoed form elements to be set.
1.119 raeburn 2155: #
2156: # In the hash, keys are the form element names, and the values are the
2157: # element type (selectbox, radio, checkbox or text -for textbox, textarea or
2158: # hidden).
1.121 raeburn 2159: #
2160: # (b) Optional reference to hash of stored elements to be set.
2161: #
2162: # If the page being displayed is a page which permits modification of
2163: # previously stored data, e.g., the first page in a multi-page submission,
2164: # then if stored is supplied, form elements will be set to the last stored
2165: # values. If user supplied values are also available for the same elements
2166: # these will replace the stored values.
2167: #
1.119 raeburn 2168: # Output:
2169: #
2170: # javascript function - set_form_elements() which sets form elements,
2171: # expects an argument: formname - the name of the form according to
2172: # the DOM, e.g., document.compose
2173:
2174: sub set_form_elements {
1.121 raeburn 2175: my ($elements,$stored) = @_;
2176: my %values;
1.119 raeburn 2177: my $output .= 'function setFormElements(courseForm) {
1.121 raeburn 2178: ';
2179: if (defined($stored)) {
2180: foreach my $name (keys(%{$stored})) {
2181: if (exists($$elements{$name})) {
2182: if (ref($$stored{$name}) eq 'ARRAY') {
2183: $values{$name} = $$stored{$name};
2184: } else {
2185: @{$values{$name}} = ($$stored{$name});
2186: }
2187: }
2188: }
2189: }
2190:
1.119 raeburn 2191: foreach my $key (keys(%env)) {
2192: if ($key =~ /^form\.(.+)$/) {
2193: my $name = $1;
2194: if (exists($$elements{$name})) {
1.121 raeburn 2195: @{$values{$name}} = &Apache::loncommon::get_env_multiple($key);
2196: }
2197: }
2198: }
2199:
2200: foreach my $name (keys(%values)) {
2201: for (my $i=0; $i<@{$values{$name}}; $i++) {
2202: $values{$name}[$i] = &HTML::Entities::decode($values{$name}[$i],'<>&"');
2203: $values{$name}[$i] =~ s/([\r\n\f]+)/\\n/g;
2204: $values{$name}[$i] =~ s/"/\\"/g;
2205: }
1.234 raeburn 2206: if (($$elements{$name} eq 'text') || ($$elements{$name} eq 'hidden')) {
1.121 raeburn 2207: my $numvalues = @{$values{$name}};
2208: if ($numvalues > 1) {
2209: my $valuestring = join('","',@{$values{$name}});
2210: $output .= qq|
1.119 raeburn 2211: var textvalues = new Array ("$valuestring");
1.147 raeburn 2212: var total = courseForm.elements['$name'].length;
1.119 raeburn 2213: if (total > $numvalues) {
2214: total = $numvalues;
2215: }
2216: for (var i=0; i<total; i++) {
1.147 raeburn 2217: courseForm.elements['$name']\[i].value = textvalues[i];
1.119 raeburn 2218: }
2219: |;
1.121 raeburn 2220: } else {
2221: $output .= qq|
1.147 raeburn 2222: courseForm.elements['$name'].value = "$values{$name}[0]";
1.119 raeburn 2223: |;
1.121 raeburn 2224: }
2225: } else {
2226: $output .= qq|
1.147 raeburn 2227: var elementLength = courseForm.elements['$name'].length;
1.119 raeburn 2228: if (elementLength==undefined) {
2229: |;
1.121 raeburn 2230: foreach my $value (@{$values{$name}}) {
2231: if ($$elements{$name} eq 'selectbox') {
2232: $output .= qq|
1.147 raeburn 2233: if (courseForm.elements['$name'].options[0].value == "$value") {
2234: courseForm.elements['$name'].options[0].selected = true;
1.119 raeburn 2235: }|;
1.121 raeburn 2236: } elsif (($$elements{$name} eq 'radio') ||
2237: ($$elements{$name} eq 'checkbox')) {
2238: $output .= qq|
1.147 raeburn 2239: if (courseForm.elements['$name'].value == "$value") {
1.148 albertel 2240: courseForm.elements['$name'].checked = true;
1.234 raeburn 2241: } else {
2242: courseForm.elements['$name'].checked = false;
1.119 raeburn 2243: }|;
1.121 raeburn 2244: }
2245: }
2246: $output .= qq|
1.119 raeburn 2247: }
2248: else {
1.147 raeburn 2249: for (var i=0; i<courseForm.elements['$name'].length; i++) {
1.119 raeburn 2250: |;
1.121 raeburn 2251: if ($$elements{$name} eq 'selectbox') {
2252: $output .= qq|
1.147 raeburn 2253: courseForm.elements['$name'].options[i].selected = false;|;
1.121 raeburn 2254: } elsif (($$elements{$name} eq 'radio') ||
2255: ($$elements{$name} eq 'checkbox')) {
2256: $output .= qq|
1.147 raeburn 2257: courseForm.elements['$name']\[i].checked = false;|;
1.121 raeburn 2258: }
2259: $output .= qq|
1.119 raeburn 2260: }
1.147 raeburn 2261: for (var j=0; j<courseForm.elements['$name'].length; j++) {
1.119 raeburn 2262: |;
1.121 raeburn 2263: foreach my $value (@{$values{$name}}) {
2264: if ($$elements{$name} eq 'selectbox') {
2265: $output .= qq|
1.147 raeburn 2266: if (courseForm.elements['$name'].options[j].value == "$value") {
2267: courseForm.elements['$name'].options[j].selected = true;
1.119 raeburn 2268: }|;
1.121 raeburn 2269: } elsif (($$elements{$name} eq 'radio') ||
2270: ($$elements{$name} eq 'checkbox')) {
2271: $output .= qq|
1.147 raeburn 2272: if (courseForm.elements['$name']\[j].value == "$value") {
2273: courseForm.elements['$name']\[j].checked = true;
1.119 raeburn 2274: }|;
1.121 raeburn 2275: }
2276: }
2277: $output .= qq|
1.119 raeburn 2278: }
2279: }
2280: |;
2281: }
2282: }
2283: $output .= "
1.235 raeburn 2284: return;
1.119 raeburn 2285: }\n";
2286: return $output;
2287: }
2288:
1.158 raeburn 2289: ##############################################
2290: ##############################################
2291:
2292: # javascript_valid_email
2293: #
2294: # Generates javascript to validate an e-mail address.
2295: # Returns a javascript function which accetps a form field as argumnent, and
2296: # returns false if field.value does not satisfy two regular expression matches
2297: # for a valid e-mail address. Backwards compatible with old browsers without
2298: # support for javascript RegExp (just checks for @ in field.value in this case).
2299:
2300: sub javascript_valid_email {
2301: my $scripttag .= <<'END';
2302: function validmail(field) {
2303: var str = field.value;
2304: if (window.RegExp) {
2305: var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
2306: var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"; //"
2307: var reg1 = new RegExp(reg1str);
2308: var reg2 = new RegExp(reg2str);
2309: if (!reg1.test(str) && reg2.test(str)) {
2310: return true;
2311: }
2312: return false;
2313: }
2314: else
2315: {
2316: if(str.indexOf("@") >= 0) {
2317: return true;
2318: }
2319: return false;
2320: }
2321: }
2322: END
2323: return $scripttag;
2324: }
2325:
1.219 droeschl 2326:
2327: # USAGE: htmltag(element, content, {attribute => value,...});
2328: #
2329: # EXAMPLES:
2330: # - htmltag('a', 'this is an anchor', {href => 'www.example.com',
2331: # title => 'this is a title'})
2332: #
2333: # - You might want to set up needed tags like:
2334: #
2335: # my $h3 = sub { return htmltag( "h3", @_ ) };
2336: #
2337: # ... and use them: $h3->("This is a headline")
2338: #
2339: # - To set up a couple of tags, see sub inittags
2340: #
2341: # NOTES:
2342: # - Empty elements, such as <br/> are correctly terminated,
2343: # i.e. htmltag('br') returns <br/>
2344: # - Empty attributes (title="") are filtered out.
2345: # - The function will not check for deprecated attributes.
2346: #
2347: # OUTPUT: content enclosed in xhtml conform tags
2348: sub htmltag{
2349: return
2350: qq|<$_[0]|
2351: . join( '', map { qq| $_="${$_[2]}{$_}"| if ${$_[2]}{$_} } keys %{ $_[2] } )
2352: . ($_[1] ? qq|>$_[1]</$_[0]>| : qq|/>|). "\n";
2353: };
2354:
2355:
2356: # USAGE: inittags(@tags);
2357: #
2358: # EXAMPLES:
1.261 droeschl 2359: # - my ($h1, $h2, $h3) = inittags( qw( h1 h2 h3 ) )
1.219 droeschl 2360: # $h1->("This is a headline") #Returns: <h1>This is a headline</h1>
2361: #
2362: # NOTES: See sub htmltag for further information.
2363: #
2364: # OUTPUT: List of subroutines.
2365: sub inittags {
2366: my @tags = @_;
2367: return map { my $tag = $_;
2368: sub { return htmltag( $tag, @_ ) }
2369: } @tags;
2370: }
2371:
2372:
1.231 droeschl 2373: # USAGE: scripttag(scriptcode, [start|end|both]);
1.229 droeschl 2374: #
2375: # EXAMPLES:
1.231 droeschl 2376: # - scripttag("alert('Hello World!')", 'both')
2377: # returns:
2378: # <script type="text/javascript">
2379: # // BEGIN LON-CAPA Internal
2380: # alert(Hello World!')
2381: # // END LON-CAPA Internal
2382: # </script>
1.229 droeschl 2383: #
2384: # NOTES:
2385: # - works currently only for javascripts
2386: #
1.231 droeschl 2387: # OUTPUT:
2388: # Scriptcode properly enclosed in <script> and CDATA tags (and LC
2389: # Internal markers if 2nd argument is given)
1.229 droeschl 2390: sub scripttag {
1.231 droeschl 2391: my ( $content, $marker ) = @_;
2392: return unless defined $content;
2393:
2394: my $begin = "\n// BEGIN LON-CAPA Internal\n";
2395: my $end = "\n// END LON-CAPA Internal\n";
2396:
2397: if ($marker) {
2398: $content = $begin . $content if $marker eq 'start' or $marker eq 'both';
2399: $content .= $end if $marker eq 'end' or $marker eq 'both';
2400: }
2401:
1.229 droeschl 2402: $content = "\n// <![CDATA[\n$content\n// ]]>\n";
1.231 droeschl 2403:
2404: return htmltag('script', $content, {type => 'text/javascript'});
1.229 droeschl 2405: };
2406:
2407:
1.261 droeschl 2408: =item list_from_array( \@array, { listattr =>{}, itemattr =>{} } )
2409:
2410: Constructs a XHTML list from \@array.
2411:
2412: input:
2413:
2414: =over
2415:
2416: =item \@array
2417:
2418: A reference to the array containing text that will be wrapped in <li></li> tags.
2419:
2420: =item { listattr => {}, itemattr =>{} }
2421:
2422: Attributes for <ul> and <li> passed in as hash references.
2423: See htmltag() for more details.
2424:
2425: =back
2426:
2427: returns: XHTML list as String.
2428:
2429: =cut
2430:
2431: # \@items, {listattr => { class => 'abc', id => 'xyx' }, itemattr => {class => 'abc', id => 'xyx'}}
2432: sub list_from_array {
2433: my ($items, $args) = @_;
1.285 raeburn 2434: return unless (ref($items) eq 'ARRAY');
1.273 droeschl 2435: return unless scalar @$items;
1.261 droeschl 2436: my ($ul, $li) = inittags( qw(ul li) );
2437: my $listitems = join '', map { $li->($_, $args->{itemattr}) } @$items;
2438: return $ul->( $listitems, $args->{listattr} );
2439: }
2440:
2441:
1.183 droeschl 2442: ##############################################
2443: ##############################################
2444:
2445: # generate_menu
2446: #
2447: # Generates html markup for a menu.
2448: #
2449: # Inputs:
2450: # An array of following structure:
2451: # ({ categorytitle => 'Categorytitle',
2452: # items => [
1.201 droeschl 2453: # {
2454: # linktext => 'Text to be displayed',
2455: # url => 'URL the link is pointing to, i.e. /adm/site?action=dosomething',
1.183 droeschl 2456: # permission => 'Contains permissions as returned from lonnet::allowed(),
1.201 droeschl 2457: # must evaluate to true in order to activate the link',
1.184 droeschl 2458: # icon => 'icon filename',
1.186 droeschl 2459: # alttext => 'alt text for the icon',
1.183 droeschl 2460: # help => 'Name of the corresponding helpfile',
2461: # linktitle => 'Description of the link (used for title tag)'
2462: # },
2463: # ...
2464: # ]
2465: # },
2466: # ...
2467: # )
2468: #
2469: # Outputs: A scalar containing the html markup for the menu.
2470:
2471: sub generate_menu {
2472: my @menu = @_;
1.201 droeschl 2473: # subs for specific html elements
1.219 droeschl 2474: my ($h3, $div, $ul, $li, $a, $img) = inittags( qw(h3 div ul li a img) );
1.201 droeschl 2475:
2476: my @categories; # each element represents the entire markup for a category
2477:
2478: foreach my $category (@menu) {
2479: my @links; # contains the links for the current $category
2480: foreach my $link (@{$$category{items}}) {
2481: next unless $$link{permission};
2482:
2483: # create the markup for the current $link and push it into @links.
2484: # each entry consists of an image and a text optionally followed
2485: # by a help link.
1.283 raeburn 2486: my $src;
2487: if ($$link{icon} ne '') {
2488: $src = '/res/adm/pages/'.$$link{icon};
2489: }
1.232 raeburn 2490: push(@links,$li->(
1.201 droeschl 2491: $a->(
2492: $img->("", {
2493: class => "LC_noBorder LC_middle",
1.283 raeburn 2494: src => $src,
1.202 droeschl 2495: alt => mt(defined($$link{alttext}) ?
2496: $$link{alttext} : $$link{linktext})
1.201 droeschl 2497: }), {
2498: href => $$link{url},
1.202 droeschl 2499: title => mt($$link{linktitle})
1.201 droeschl 2500: }).
1.202 droeschl 2501: $a->(mt($$link{linktext}), {
1.201 droeschl 2502: href => $$link{url},
1.202 droeschl 2503: title => mt($$link{linktitle}),
1.201 droeschl 2504: class => "LC_menubuttons_link"
2505: }).
2506: (defined($$link{help}) ?
2507: Apache::loncommon::help_open_topic($$link{help}) : ''),
1.232 raeburn 2508: {class => "LC_menubuttons_inline_text"}));
1.201 droeschl 2509: }
2510:
2511: # wrap categorytitle in <h3>, concatenate with
2512: # joined and in <ul> tags wrapped @links
2513: # and wrap everything in an enclosing <div> and push it into
2514: # @categories
2515: # such that each element looks like:
2516: # <div><h3>title</h3><ul><li>...</li>...</ul></div>
2517: # the category won't be added if there aren't any links
1.232 raeburn 2518: push(@categories,
1.202 droeschl 2519: $div->($h3->(mt($$category{categorytitle}), {class=>"LC_hcell"}).
1.201 droeschl 2520: $ul->(join('' ,@links), {class =>"LC_ListStyleNormal" }),
1.232 raeburn 2521: {class=>"LC_Box LC_400Box"})) if scalar(@links);
1.183 droeschl 2522: }
1.201 droeschl 2523:
2524: # wrap the joined @categories in another <div> (column layout)
2525: return $div->(join('', @categories), {class => "LC_columnSection"});
1.183 droeschl 2526: }
1.176 foxr 2527:
1.224 bisitz 2528: ##############################################
2529: ##############################################
2530:
2531: =pod
2532:
2533: =item &start_funclist
2534:
2535: Start list of available functions
2536:
2537: Typically used to offer a simple list of available functions
2538: at top or bottom of page.
2539: All available functions/actions for the current page
2540: should be included in this list.
2541:
2542: If the optional headline text is not provided, a default text will be used.
2543:
2544:
2545: Related routines:
2546: =over 4
2547: add_item_funclist
2548: end_funclist
2549: =back
2550:
2551:
2552: Inputs: (optional) headline text
2553:
2554: Returns: HTML code with function list start
2555:
2556: =cut
2557:
2558: ##############################################
2559: ##############################################
2560:
2561: sub start_funclist {
2562: my($legendtext)=@_;
2563: $legendtext=&mt('Functions') if !$legendtext;
1.244 droeschl 2564: return '<ul class="LC_funclist"><li style="font-weight:bold; margin-left:0.8em;">'.$legendtext.'</li>'."\n";
1.224 bisitz 2565: }
2566:
2567:
2568: ##############################################
2569: ##############################################
2570:
2571: =pod
2572:
2573: =item &add_item_funclist
2574:
2575: Adds an item to the list of available functions
2576:
2577: Related routines:
2578: =over 4
2579: start_funclist
2580: end_funclist
2581: =back
2582:
2583: Inputs: content item with text and link to function
2584:
2585: Returns: HTML code with list item for funclist
2586:
2587: =cut
2588:
2589: ##############################################
2590: ##############################################
2591:
2592: sub add_item_funclist {
2593: my($content) = @_;
2594: return '<li>'.$content.'</li>'."\n";
2595: }
2596:
2597: =pod
2598:
2599: =item &end_funclist
2600:
2601: End list of available functions
2602:
2603: Related routines:
2604: =over 4
2605: start_funclist
2606: add_item_funclist
2607: =back
2608:
2609: Inputs: ./.
2610:
2611: Returns: HTML code with function list end
2612: =cut
2613:
2614: sub end_funclist {
1.246 bisitz 2615: return "</ul>\n";
1.224 bisitz 2616: }
2617:
1.261 droeschl 2618: =pod
2619:
2620: =item funclist_from_array( \@array, {legend => 'text for legend'} )
2621:
2622: Constructs a XHTML list from \@array with the first item being visually
2623: highlighted and set to the value of legend or 'Functions' if legend is
2624: empty.
2625:
2626: =over
2627:
2628: =item \@array
2629:
2630: A reference to the array containing text that will be wrapped in <li></li> tags.
2631:
2632: =item { legend => 'text' }
2633:
2634: A string that's used as visually highlighted first item. 'Functions' is used if
2635: it's value evaluates to false.
2636:
2637: =back
2638:
2639: returns: XHTML list as string.
2640:
2641: =back
2642:
2643: =cut
2644:
2645: sub funclist_from_array {
2646: my ($items, $args) = @_;
1.285 raeburn 2647: return unless(ref($items) eq 'ARRAY');
1.261 droeschl 2648: $args->{legend} ||= mt('Functions');
2649: return list_from_array( [$args->{legend}, @$items],
2650: { listattr => {class => 'LC_funclist'} });
2651: }
2652:
1.1 stredwic 2653: 1;
1.23 matthew 2654:
1.1 stredwic 2655: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>