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