File:
[LON-CAPA] /
loncom /
interface /
lonhtmlcommon.pm
Revision
1.134:
download - view:
text,
annotated -
select for diffs
Fri Jun 16 22:37:10 2006 UTC (18 years, 2 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
Enable users to set access controls on portfolio files to permit public viewing. Third iteration of data structure in file_permissions.db for storage of access contol records. Now uses key = value for individual access control records and a key = anonymous hash for fast look-ups of all records for a single file. Use of a temporary lock record to prevent others modifying access control records for a specific portfolio file while the record for the same file is being updated. Additional argument added to call to lonhtmlcommon::datesetter() to optionally suppress display of calendar link.
1: # The LearningOnline Network with CAPA
2: # a pile of common html routines
3: #
4: # $Id: lonhtmlcommon.pm,v 1.134 2006/06/16 22:37:10 raeburn Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: ######################################################################
29: ######################################################################
30:
31: =pod
32:
33: =head1 NAME
34:
35: Apache::lonhtmlcommon - routines to do common html things
36:
37: =head1 SYNOPSIS
38:
39: Referenced by other mod_perl Apache modules.
40:
41: =head1 INTRODUCTION
42:
43: lonhtmlcommon is a collection of subroutines used to present information
44: in a consistent html format, or provide other functionality related to
45: html.
46:
47: =head2 General Subroutines
48:
49: =over 4
50:
51: =cut
52:
53: ######################################################################
54: ######################################################################
55:
56: package Apache::lonhtmlcommon;
57:
58: use strict;
59: use Time::Local;
60: use Time::HiRes;
61: use Apache::lonlocal;
62: use Apache::lonnet;
63: use lib '/home/httpd/lib/perl/';
64: use LONCAPA;
65:
66: ##############################################
67: ##############################################
68:
69: =pod
70:
71: =item authorbombs
72:
73: =cut
74:
75: ##############################################
76: ##############################################
77:
78: sub authorbombs {
79: my $url=shift;
80: $url=&Apache::lonnet::declutter($url);
81: my ($udom,$uname)=($url=~/^(\w+)\/(\w+)\//);
82: my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
83: foreach (keys %bombs) {
84: if ($_=~/^$udom\/$uname\//) {
85: return '<a href="/adm/bombs/'.$url.
86: '"><img src="'.&Apache::loncommon::lonhttpdurl('/adm/lonMisc/bomb.gif').'" border="0" /></a>'.
87: &Apache::loncommon::help_open_topic('About_Bombs');
88: }
89: }
90: return '';
91: }
92:
93: ##############################################
94: ##############################################
95:
96: sub recent_filename {
97: my $area=shift;
98: return 'nohist_recent_'.&escape($area);
99: }
100:
101: sub store_recent {
102: my ($area,$name,$value)=@_;
103: my $file=&recent_filename($area);
104: my %recent=&Apache::lonnet::dump($file);
105: if (scalar(keys(%recent))>20) {
106: # remove oldest value
107: my $oldest=time;
108: my $delkey='';
109: foreach (keys %recent) {
110: my $thistime=(split(/\&/,$recent{$_}))[0];
111: if ($thistime<$oldest) {
112: $oldest=$thistime;
113: $delkey=$_;
114: }
115: }
116: &Apache::lonnet::del($file,[$delkey]);
117: }
118: # store new value
119: &Apache::lonnet::put($file,{ $name =>
120: time.'&'.&escape($value) });
121: }
122:
123: sub remove_recent {
124: my ($area,$names)=@_;
125: my $file=&recent_filename($area);
126: return &Apache::lonnet::del($file,$names);
127: }
128:
129: sub select_recent {
130: my ($area,$fieldname,$event)=@_;
131: my %recent=&Apache::lonnet::dump(&recent_filename($area));
132: my $return="\n<select name='$fieldname'".
133: ($event?" onchange='$event'":'').
134: ">\n<option value=''>--- ".&mt('Recent')." ---</option>";
135: foreach (sort keys %recent) {
136: unless ($_=~/^error\:/) {
137: my $escaped = &Apache::loncommon::escape_url($_);
138: $return.="\n<option value='$escaped'>".
139: &unescape((split(/\&/,$recent{$_}))[1]).
140: '</option>';
141: }
142: }
143: $return.="\n</select>\n";
144: return $return;
145: }
146:
147: sub get_recent {
148: my ($area, $n) = @_;
149: my %recent=&Apache::lonnet::dump(&recent_filename($area));
150:
151: # Create hash with key as time and recent as value
152: my %time_hash = ();
153: my $nfrozen = 0;
154: foreach (keys %recent) {
155: my ($thistime,$thisvalue)=(split(/\&/,$recent{$_}));
156: if (($thisvalue eq 'role_frozen') && ($area='roles')) {
157: $thistime=time+$nfrozen;
158: $nfrozen++;
159: }
160: $time_hash{$thistime} = $_;
161: }
162:
163: # Sort by decreasing time and return key value pairs
164: my %return_hash = ();
165: my $idx = 1;
166: foreach (reverse sort keys %time_hash) {
167: $return_hash{$time_hash{$_}} =
168: &unescape((split(/\&/,$recent{$time_hash{$_}}))[1]);
169: if ($n && ($idx++ >= $n)) {last;}
170: }
171:
172: return %return_hash;
173: }
174:
175:
176:
177: =pod
178:
179: =item textbox
180:
181: =cut
182:
183: ##############################################
184: ##############################################
185: sub textbox {
186: my ($name,$value,$size,$special) = @_;
187: $size = 40 if (! defined($size));
188: $value = &HTML::Entities::encode($value,'<>&"');
189: my $Str = '<input type="text" name="'.$name.'" size="'.$size.'" '.
190: 'value="'.$value.'" '.$special.' />';
191: return $Str;
192: }
193:
194: ##############################################
195: ##############################################
196:
197: =pod
198:
199: =item checkbox
200:
201: =cut
202:
203: ##############################################
204: ##############################################
205: sub checkbox {
206: my ($name,$checked,$value) = @_;
207: my $Str = '<input type="checkbox" name="'.$name.'" ';
208: if (defined($value)) {
209: $Str .= 'value="'.$value.'"';
210: }
211: if ($checked) {
212: $Str .= ' checked="1"';
213: }
214: $Str .= ' />';
215: return $Str;
216: }
217:
218:
219: =pod
220:
221: =item radiobutton
222:
223: =cut
224:
225: ##############################################
226: ##############################################
227: sub radio {
228: my ($name,$checked,$value) = @_;
229: my $Str = '<input type="radio" name="'.$name.'" ';
230: if (defined($value)) {
231: $Str .= 'value="'.$value.'"';
232: }
233: if ($checked eq $value) {
234: $Str .= ' checked="1"';
235: }
236: $Str .= ' />';
237: return $Str;
238: }
239:
240: ##############################################
241: ##############################################
242:
243: =pod
244:
245: =item &date_setter
246:
247: &date_setter returns html and javascript for a compact date-setting form.
248: To retrieve values from it, use &get_date_from_form().
249:
250: Inputs
251:
252: =over 4
253:
254: =item $dname
255:
256: The name to prepend to the form elements.
257: The form elements defined will be dname_year, dname_month, dname_day,
258: dname_hour, dname_min, and dname_sec.
259:
260: =item $currentvalue
261:
262: The current setting for this time parameter. A unix format time
263: (time in seconds since the beginning of Jan 1st, 1970, GMT.
264: An undefined value is taken to indicate the value is the current time.
265: Also, to be explicit, a value of 'now' also indicates the current time.
266:
267: =item $special
268:
269: Additional html/javascript to be associated with each element in
270: the date_setter. See lonparmset for example usage.
271:
272: =item $includeempty
273:
274: =item $state
275:
276: Specifies the initial state of the form elements. Either 'disabled' or empty.
277: Defaults to empty, which indiciates the form elements are not disabled.
278:
279: =back
280:
281: Bugs
282:
283: The method used to restrict user input will fail in the year 2400.
284:
285: =cut
286:
287: ##############################################
288: ##############################################
289: sub date_setter {
290: my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
291: $no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_;
292: my $wasdefined=1;
293: if (! defined($state) || $state ne 'disabled') {
294: $state = '';
295: }
296: if (! defined($no_hh_mm_ss)) {
297: $no_hh_mm_ss = 0;
298: }
299: if ($currentvalue eq 'now') {
300: $currentvalue=time;
301: }
302: if ((!defined($currentvalue)) || ($currentvalue eq '')) {
303: $wasdefined=0;
304: if ($includeempty) {
305: $currentvalue = 0;
306: } else {
307: $currentvalue = time;
308: }
309: }
310: # other potentially useful values: wkday,yrday,is_daylight_savings
311: my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
312: if ($currentvalue) {
313: ($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) =
314: localtime($currentvalue);
315: $year += 1900;
316: }
317: unless ($wasdefined) {
318: if (($defhour) || ($defmin) || ($defsec)) {
319: ($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) =
320: localtime(time);
321: $year += 1900;
322: $sec=($defsec?$defsec:0);
323: $min=($defmin?$defmin:0);
324: $hour=($defhour?$defhour:0);
325: } elsif (!$includeempty) {
326: $sec=0;
327: $min=0;
328: $hour=0;
329: }
330: }
331: my $result = "\n<!-- $dname date setting form -->\n";
332: $result .= <<ENDJS;
333: <script language="Javascript">
334: function $dname\_checkday() {
335: var day = document.$formname.$dname\_day.value;
336: var month = document.$formname.$dname\_month.value;
337: var year = document.$formname.$dname\_year.value;
338: var valid = true;
339: if (day < 1) {
340: document.$formname.$dname\_day.value = 1;
341: }
342: if (day > 31) {
343: document.$formname.$dname\_day.value = 31;
344: }
345: if ((month == 1) || (month == 3) || (month == 5) ||
346: (month == 7) || (month == 8) || (month == 10) ||
347: (month == 12)) {
348: if (day > 31) {
349: document.$formname.$dname\_day.value = 31;
350: day = 31;
351: }
352: } else if (month == 2 ) {
353: if ((year % 4 == 0) && (year % 100 != 0)) {
354: if (day > 29) {
355: document.$formname.$dname\_day.value = 29;
356: }
357: } else if (day > 29) {
358: document.$formname.$dname\_day.value = 28;
359: }
360: } else if (day > 30) {
361: document.$formname.$dname\_day.value = 30;
362: }
363: }
364:
365: function $dname\_disable() {
366: document.$formname.$dname\_month.disabled=true;
367: document.$formname.$dname\_day.disabled=true;
368: document.$formname.$dname\_year.disabled=true;
369: document.$formname.$dname\_hour.disabled=true;
370: document.$formname.$dname\_minute.disabled=true;
371: document.$formname.$dname\_second.disabled=true;
372: }
373:
374: function $dname\_enable() {
375: document.$formname.$dname\_month.disabled=false;
376: document.$formname.$dname\_day.disabled=false;
377: document.$formname.$dname\_year.disabled=false;
378: document.$formname.$dname\_hour.disabled=false;
379: document.$formname.$dname\_minute.disabled=false;
380: document.$formname.$dname\_second.disabled=false;
381: }
382:
383: function $dname\_opencalendar() {
384: if (! document.$formname.$dname\_month.disabled) {
385: var calwin=window.open(
386: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
387: document.$formname.$dname\_month.value+"&year="+
388: document.$formname.$dname\_year.value,
389: "LONCAPAcal",
390: "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
391: }
392:
393: }
394: </script>
395: ENDJS
396: $result .= ' <nobr>';
397: my $monthselector = qq{<select name="$dname\_month" $special $state onchange="javascript:$dname\_checkday()" >};
398: # Month
399: my @Months = qw/January February March April May June
400: July August September October November December/;
401: # Pad @Months with a bogus value to make indexing easier
402: unshift(@Months,'If you can read this an error occurred');
403: if ($includeempty) { $monthselector.="<option value=''></option>"; }
404: for(my $m = 1;$m <=$#Months;$m++) {
405: $monthselector .= qq{ <option value="$m" };
406: $monthselector .= "selected " if ($m-1 eq $month);
407: $monthselector .= '> '.&mt($Months[$m]).' </option>';
408: }
409: $monthselector.= ' </select>';
410: # Day
411: my $dayselector = qq{<input type="text" name="$dname\_day" $state value="$mday" size="3" $special onchange="javascript:$dname\_checkday()" />};
412: # Year
413: my $yearselector = qq{<input type="year" name="$dname\_year" $state value="$year" size="5" $special onchange="javascript:$dname\_checkday()" />};
414: #
415: my $hourselector = qq{<select name="$dname\_hour" $special $state >};
416: if ($includeempty) {
417: $hourselector.=qq{<option value=''></option>};
418: }
419: for (my $h = 0;$h<24;$h++) {
420: $hourselector .= qq{<option value="$h" };
421: $hourselector .= "selected " if (defined($hour) && $hour == $h);
422: $hourselector .= ">";
423: my $timest='';
424: if ($h == 0) {
425: $timest .= "12 am";
426: } elsif($h == 12) {
427: $timest .= "12 noon";
428: } elsif($h < 12) {
429: $timest .= "$h am";
430: } else {
431: $timest .= $h-12 ." pm";
432: }
433: $timest=&mt($timest);
434: $hourselector .= $timest." </option>\n";
435: }
436: $hourselector .= " </select>\n";
437: my $minuteselector = qq{<input type="text" name="$dname\_minute" $special $state value="$min" size="3" />};
438: my $secondselector= qq{<input type="text" name="$dname\_second" $special $state value="$sec" size="3" />};
439: my $cal_link;
440: if (!$nolink) {
441: $cal_link = qq{<a href="javascript:$dname\_opencalendar()">};
442: }
443: #
444: if ($no_hh_mm_ss) {
445: $result .= &mt('[_1] [_2] [_3] ',
446: $monthselector,$dayselector,$yearselector);
447: if (!$nolink) {
448: $result .= &mt('[_4]Select Date[_5]',$cal_link,'</a>');
449: }
450: } else {
451: $result .= &mt('[_1] [_2] [_3] [_4] [_5]m [_6]s ',
452: $monthselector,$dayselector,$yearselector,
453: $hourselector,$minuteselector,$secondselector);
454: if (!$nolink) {
455: $result .= &mt('[_7]Select Date[_8]',$cal_link,'</a>');
456: }
457: }
458: $result .= "</nobr>\n<!-- end $dname date setting form -->\n";
459: return $result;
460: }
461:
462: ##############################################
463: ##############################################
464:
465: =pod
466:
467: =item &get_date_from_form
468:
469: get_date_from_form retrieves the date specified in an &date_setter form.
470:
471: Inputs:
472:
473: =over 4
474:
475: =item $dname
476:
477: The name passed to &datesetter, which prefixes the form elements.
478:
479: =item $defaulttime
480:
481: The unix time to use as the default in case of poor inputs.
482:
483: =back
484:
485: Returns: Unix time represented in the form.
486:
487: =cut
488:
489: ##############################################
490: ##############################################
491: sub get_date_from_form {
492: my ($dname) = @_;
493: my ($sec,$min,$hour,$day,$month,$year);
494: #
495: if (defined($env{'form.'.$dname.'_second'})) {
496: my $tmpsec = $env{'form.'.$dname.'_second'};
497: if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
498: $sec = $tmpsec;
499: }
500: if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
501: } else {
502: $sec = 0;
503: }
504: if (defined($env{'form.'.$dname.'_minute'})) {
505: my $tmpmin = $env{'form.'.$dname.'_minute'};
506: if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
507: $min = $tmpmin;
508: }
509: if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
510: } else {
511: $min = 0;
512: }
513: if (defined($env{'form.'.$dname.'_hour'})) {
514: my $tmphour = $env{'form.'.$dname.'_hour'};
515: if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
516: $hour = $tmphour;
517: }
518: } else {
519: $hour = 0;
520: }
521: if (defined($env{'form.'.$dname.'_day'})) {
522: my $tmpday = $env{'form.'.$dname.'_day'};
523: if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
524: $day = $tmpday;
525: }
526: }
527: if (defined($env{'form.'.$dname.'_month'})) {
528: my $tmpmonth = $env{'form.'.$dname.'_month'};
529: if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
530: $month = $tmpmonth - 1;
531: }
532: }
533: if (defined($env{'form.'.$dname.'_year'})) {
534: my $tmpyear = $env{'form.'.$dname.'_year'};
535: if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
536: $year = $tmpyear - 1900;
537: }
538: }
539: if (($year<70) || ($year>137)) { return undef; }
540: if (defined($sec) && defined($min) && defined($hour) &&
541: defined($day) && defined($month) && defined($year) &&
542: eval('&timelocal($sec,$min,$hour,$day,$month,$year)')) {
543: return &timelocal($sec,$min,$hour,$day,$month,$year);
544: } else {
545: return undef;
546: }
547: }
548:
549: ##############################################
550: ##############################################
551:
552: =pod
553:
554: =item &pjump_javascript_definition()
555:
556: Returns javascript defining the 'pjump' function, which opens up a
557: parameter setting wizard.
558:
559: =cut
560:
561: ##############################################
562: ##############################################
563: sub pjump_javascript_definition {
564: my $Str = <<END;
565: function pjump(type,dis,value,marker,ret,call,hour,min,sec) {
566: parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
567: +"&value="+escape(value)+"&marker="+escape(marker)
568: +"&return="+escape(ret)
569: +"&call="+escape(call)+"&name="+escape(dis)
570: +"&defhour="+escape(hour)+"&defmin="+escape(min)
571: +"&defsec="+escape(sec),"LONCAPAparms",
572: "height=350,width=350,scrollbars=no,menubar=no");
573: }
574: END
575: return $Str;
576: }
577:
578: ##############################################
579: ##############################################
580:
581: =pod
582:
583: =item &javascript_nothing()
584:
585: Return an appropriate null for the users browser. This is used
586: as the first arguement for window.open calls when you want a blank
587: window that you can then write to.
588:
589: =cut
590:
591: ##############################################
592: ##############################################
593: sub javascript_nothing {
594: # mozilla and other browsers work with "''", but IE on mac does not.
595: my $nothing = "''";
596: my $user_browser;
597: my $user_os;
598: $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
599: $user_os = $env{'browser.os'} if (exists($env{'browser.os'}));
600: if (! defined($user_browser) || ! defined($user_os)) {
601: (undef,$user_browser,undef,undef,undef,$user_os) =
602: &Apache::loncommon::decode_user_agent();
603: }
604: if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
605: $nothing = "'javascript:void(0);'";
606: }
607: return $nothing;
608: }
609:
610: ##############################################
611: ##############################################
612: sub javascript_docopen {
613: # safari does not understand document.open() and loads "text/html"
614: my $nothing = "''";
615: my $user_browser;
616: my $user_os;
617: $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
618: $user_os = $env{'browser.os'} if (exists($env{'browser.os'}));
619: if (! defined($user_browser) || ! defined($user_os)) {
620: (undef,$user_browser,undef,undef,undef,$user_os) =
621: &Apache::loncommon::decode_user_agent();
622: }
623: if ($user_browser eq 'safari' && $user_os =~ 'mac') {
624: $nothing = "document.clear()";
625: } else {
626: $nothing = "document.open('text/html','replace')";
627: }
628: return $nothing;
629: }
630:
631:
632: ##############################################
633: ##############################################
634:
635: =pod
636:
637: =item &StatusOptions()
638:
639: Returns html for a selection box which allows the user to choose the
640: enrollment status of students. The selection box name is 'Status'.
641:
642: Inputs:
643:
644: $status: the currently selected status. If undefined the value of
645: $env{'form.Status'} is taken. If that is undefined, a value of 'Active'
646: is used.
647:
648: $formname: The name of the form. If defined the onchange attribute of
649: the selection box is set to document.$formname.submit().
650:
651: $size: the size (number of lines) of the selection box.
652:
653: $onchange: javascript to use when the value is changed. Enclosed in
654: double quotes, ""s, not single quotes.
655:
656: Returns: a perl string as described.
657:
658: =cut
659:
660: ##############################################
661: ##############################################
662: sub StatusOptions {
663: my ($status, $formName,$size,$onchange)=@_;
664: $size = 1 if (!defined($size));
665: if (! defined($status)) {
666: $status = 'Active';
667: $status = $env{'form.Status'} if (exists($env{'form.Status'}));
668: }
669:
670: my $OpSel1 = '';
671: my $OpSel2 = '';
672: my $OpSel3 = '';
673:
674: if($status eq 'Any') { $OpSel3 = ' selected'; }
675: elsif($status eq 'Expired' ) { $OpSel2 = ' selected'; }
676: else { $OpSel1 = ' selected'; }
677:
678: my $Str = '';
679: $Str .= '<select name="Status"';
680: if(defined($formName) && $formName ne '' && ! defined($onchange)) {
681: $Str .= ' onchange="document.'.$formName.'.submit()"';
682: }
683: if (defined($onchange)) {
684: $Str .= ' onchange="'.$onchange.'"';
685: }
686: $Str .= ' size="'.$size.'" ';
687: $Str .= '>'."\n";
688: $Str .= '<option value="Active" '.$OpSel1.'>'.
689: &mt('Currently Enrolled').'</option>'."\n";
690: $Str .= '<option value="Expired" '.$OpSel2.'>'.
691: &mt('Previously Enrolled').'</option>'."\n";
692: $Str .= '<option value="Any" '.$OpSel3.'>'.
693: &mt('Any Enrollment Status').'</option>'."\n";
694: $Str .= '</select>'."\n";
695: }
696:
697: ########################################################
698: ########################################################
699:
700: =pod
701:
702: =item Progess Window Handling Routines
703:
704: These routines handle the creation, update, increment, and closure of
705: progress windows. The progress window reports to the user the number
706: of items completed and an estimate of the time required to complete the rest.
707:
708: =over 4
709:
710:
711: =item &Create_PrgWin
712:
713: Writes javascript to the client to open a progress window and returns a
714: data structure used for bookkeeping.
715:
716: Inputs
717:
718: =over 4
719:
720: =item $r Apache request
721:
722: =item $title The title of the progress window
723:
724: =item $heading A description (usually 1 line) of the process being initiated.
725:
726: =item $number_to_do The total number of items being processed.
727:
728: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
729: specified)
730:
731: =item $width Specify the width in charaters of the input field.
732:
733: =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
734:
735: =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
736:
737: =back
738:
739: Returns a hash containing the progress state data structure.
740:
741:
742: =item &Update_PrgWin
743:
744: Updates the text in the progress indicator. Does not increment the count.
745: See &Increment_PrgWin.
746:
747: Inputs:
748:
749: =over 4
750:
751: =item $r Apache request
752:
753: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
754:
755: =item $displaystring The string to write to the status indicator
756:
757: =back
758:
759: Returns: none
760:
761:
762: =item Increment_PrgWin
763:
764: Increment the count of items completed for the progress window by 1.
765:
766: Inputs:
767:
768: =over 4
769:
770: =item $r Apache request
771:
772: =item $prog_state Pointer to the data structure returned by Create_PrgWin
773:
774: =item $extraInfo A description of the items being iterated over. Typically
775: 'student'.
776:
777: =back
778:
779: Returns: none
780:
781:
782: =item Close_PrgWin
783:
784: Closes the progress window.
785:
786: Inputs:
787:
788: =over 4
789:
790: =item $r Apache request
791:
792: =item $prog_state Pointer to the data structure returned by Create_PrgWin
793:
794: =back
795:
796: Returns: none
797:
798: =back
799:
800: =cut
801:
802: ########################################################
803: ########################################################
804:
805: my $uniq=0;
806: sub get_uniq_name {
807: $uniq++;
808: return 'uniquename'.$uniq;
809: }
810:
811: # Create progress
812: sub Create_PrgWin {
813: my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
814: $inputname)=@_;
815: if (!defined($type)) { $type='popup'; }
816: if (!defined($width)) { $width=55; }
817: my %prog_state;
818: $prog_state{'type'}=$type;
819: if ($type eq 'popup') {
820: $prog_state{'window'}='popwin';
821: my $start_page =
822: &Apache::loncommon::start_page($title,undef,
823: {'only_body' => 1,
824: 'bgcolor' => '#88DDFF',
825: 'js_ready' => 1});
826: my $end_page = &Apache::loncommon::end_page({'js_ready' => 1});
827:
828: #the whole function called through timeout is due to issues
829: #in mozilla Read BUG #2665 if you want to know the whole story
830: &r_print($r,'<script type="text/javascript">'.
831: "var popwin;
832: function openpopwin () {
833: popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
834: "popwin.document.writeln(\'".$start_page.
835: "<h4>$heading<\/h4>".
836: "<form name=\"popremain\" method=\"post\">".
837: '<input type="text" size="'.$width.'" name="remaining" value="'.
838: &mt('Starting').'" /><\\/form>'.$end_page.
839: "\');".
840: "popwin.document.close();}".
841: "\nwindow.setTimeout(openpopwin,0)</script>");
842: $prog_state{'formname'}='popremain';
843: $prog_state{'inputname'}="remaining";
844: } elsif ($type eq 'inline') {
845: $prog_state{'window'}='window';
846: if (!$formname) {
847: $prog_state{'formname'}=&get_uniq_name();
848: &r_print($r,'<form name="'.$prog_state{'formname'}.'">');
849: } else {
850: $prog_state{'formname'}=$formname;
851: }
852: if (!$inputname) {
853: $prog_state{'inputname'}=&get_uniq_name();
854: &r_print($r,$heading.' <input type="text" name="'.$prog_state{'inputname'}.
855: '" size="'.$width.'" />');
856: } else {
857: $prog_state{'inputname'}=$inputname;
858:
859: }
860: if (!$formname) { &r_print($r,'</form>'); }
861: &Update_PrgWin($r,\%prog_state,&mt('Starting'));
862: }
863:
864: $prog_state{'done'}=0;
865: $prog_state{'firststart'}=&Time::HiRes::time();
866: $prog_state{'laststart'}=&Time::HiRes::time();
867: $prog_state{'max'}=$number_to_do;
868:
869: return %prog_state;
870: }
871:
872: # update progress
873: sub Update_PrgWin {
874: my ($r,$prog_state,$displayString)=@_;
875: &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
876: $$prog_state{'formname'}.'.'.
877: $$prog_state{'inputname'}.'.value="'.
878: $displayString.'";</script>');
879: $$prog_state{'laststart'}=&Time::HiRes::time();
880: }
881:
882: # increment progress state
883: sub Increment_PrgWin {
884: my ($r,$prog_state,$extraInfo)=@_;
885: $$prog_state{'done'}++;
886: my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
887: $$prog_state{'done'} *
888: ($$prog_state{'max'}-$$prog_state{'done'});
889: $time_est = int($time_est);
890: #
891: my $min = int($time_est/60);
892: my $sec = $time_est % 60;
893: #
894: my $str;
895: if ($min == 0 && $sec > 1) {
896: $str = '[_2] seconds';
897: } elsif ($min == 1 && $sec > 1) {
898: $str = '1 minute [_2] seconds';
899: } elsif ($min == 1 && $sec < 2) {
900: $str = '1 minute';
901: } elsif ($min < 10 && $sec > 1) {
902: $str = '[_1] minutes, [_2] seconds';
903: } elsif ($min >= 10 || $sec < 2) {
904: $str = '[_1] minutes';
905: }
906: $time_est = &mt($str,$min,$sec);
907: #
908: my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
909: if ($lasttime > 9) {
910: $lasttime = int($lasttime);
911: } elsif ($lasttime < 0.01) {
912: $lasttime = 0;
913: } else {
914: $lasttime = sprintf("%3.2f",$lasttime);
915: }
916: if ($lasttime == 1) {
917: $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
918: } else {
919: $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
920: }
921: #
922: my $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
923: my $user_os = $env{'browser.os'} if (exists($env{'browser.os'}));
924: if (! defined($user_browser) || ! defined($user_os)) {
925: (undef,$user_browser,undef,undef,undef,$user_os) =
926: &Apache::loncommon::decode_user_agent();
927: }
928: if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
929: $lasttime = '';
930: }
931: &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
932: $$prog_state{'formname'}.'.'.
933: $$prog_state{'inputname'}.'.value="'.
934: $$prog_state{'done'}.'/'.$$prog_state{'max'}.
935: ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
936: $$prog_state{'laststart'}=&Time::HiRes::time();
937: }
938:
939: # close Progress Line
940: sub Close_PrgWin {
941: my ($r,$prog_state)=@_;
942: if ($$prog_state{'type'} eq 'popup') {
943: &r_print($r,'<script>popwin.close()</script>'."\n");
944: } elsif ($$prog_state{'type'} eq 'inline') {
945: &Update_PrgWin($r,$prog_state,&mt('Done'));
946: }
947: undef(%$prog_state);
948: }
949:
950: sub r_print {
951: my ($r,$to_print)=@_;
952: if ($r) {
953: $r->print($to_print);
954: $r->rflush();
955: } else {
956: print($to_print);
957: }
958: }
959:
960: # ------------------------------------------------------- Puts directory header
961:
962: sub crumbs {
963: my ($uri,$target,$prefix,$form,$size,$noformat,$skiplast)=@_;
964: if (! defined($size)) {
965: $size = '+2';
966: }
967: if ($target) {
968: $target = ' target="'.
969: &Apache::loncommon::escape_single($target).'"';
970: }
971: my $output='';
972: unless ($noformat) { $output.='<br /><tt><b>'; }
973: $output.='<font size="'.$size.'">'.$prefix.'/';
974: if ($env{'user.adv'}) {
975: my $path=$prefix.'/';
976: foreach my $dir (split('/',$uri)) {
977: if (! $dir) { next; }
978: $path .= $dir;
979: if ($path eq $uri) {
980: if ($skiplast) {
981: $output.=$dir;
982: last;
983: }
984: } else {
985: $path.='/';
986: }
987: my $linkpath = &Apache::loncommon::escape_single($path);
988: if ($form) {
989: $linkpath=
990: qq{javascript:$form.action='$linkpath';$form.submit();};
991: }
992: $output.=qq{<a href="$linkpath" $target>$dir</a>/};
993: }
994: } else {
995: $output.=$uri;
996: }
997: unless ($uri=~/\/$/) { $output=~s/\/$//; }
998: return $output.'</font>'.($noformat?'':'</b></tt><br />');
999: }
1000:
1001: # --------------------- A function that generates a window for the spellchecker
1002:
1003: sub spellheader {
1004: my $start_page=
1005: &Apache::loncommon::start_page('Speller Suggestions',undef,
1006: {'only_body' => 1,
1007: 'js_ready' => 1,
1008: 'bgcolor' => '#DDDDDD',});
1009: my $end_page=
1010: &Apache::loncommon::end_page({'js_ready' => 1});
1011:
1012: my $nothing=&javascript_nothing();
1013: return (<<ENDCHECK);
1014: <script type="text/javascript">
1015: //<!-- BEGIN LON-CAPA Internal
1016: var checkwin;
1017:
1018: function spellcheckerwindow() {
1019: checkwin=window.open($nothing,'spellcheckwin','height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
1020: checkwin.document.writeln('$start_page<form name="spellcheckform" action="/adm/spellcheck" method="post"><input type="hidden" name="text" value="" /><\\/form>$end_page');
1021: checkwin.document.close();
1022: }
1023: // END LON-CAPA Internal -->
1024: </script>
1025: ENDCHECK
1026: }
1027:
1028: # ---------------------------------- Generate link to spell checker for a field
1029:
1030: sub spelllink {
1031: my ($form,$field)=@_;
1032: my $linktext=&mt('Check Spelling');
1033: return (<<ENDLINK);
1034: <a href="javascript:if (typeof(document.$form.onsubmit)!='undefined') { if (document.$form.onsubmit!=null) { document.$form.onsubmit();}};spellcheckerwindow();checkwin.document.forms.spellcheckform.text.value=this.document.forms.$form.$field.value;checkwin.document.forms.spellcheckform.submit();">$linktext</a>
1035: ENDLINK
1036: }
1037:
1038: # ------------------------------------------------- Output headers for HTMLArea
1039:
1040: {
1041: my @htmlareafields;
1042: sub init_htmlareafields {
1043: undef(@htmlareafields);
1044: }
1045:
1046: sub add_htmlareafields {
1047: my (@newfields) = @_;
1048: push(@htmlareafields,@newfields);
1049: }
1050:
1051: sub get_htmlareafields {
1052: return @htmlareafields;
1053: }
1054: }
1055:
1056: sub htmlareaheaders {
1057: if (&htmlareablocked()) { return ''; }
1058: unless (&htmlareabrowser()) { return ''; }
1059: my $lang='en';
1060: if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
1061: $lang=&mt('htmlarea_lang');
1062: }
1063: return (<<ENDHEADERS);
1064: <script type="text/javascript">
1065: _editor_url='/htmlarea/';
1066: _editor_lang='$lang';
1067: </script>
1068: <script type="text/javascript" src="/htmlarea/htmlarea.js"></script>
1069: ENDHEADERS
1070: }
1071:
1072: # ------------------------------------------------- Activate additional buttons
1073:
1074: sub htmlareaaddbuttons {
1075: if (&htmlareablocked()) { return ''; }
1076: unless (&htmlareabrowser()) { return ''; }
1077: return (<<ENDADDBUTTON);
1078: var config=new HTMLArea.Config();
1079: config.registerButton('ed_math','LaTeX Inline',
1080: '/htmlarea/images/ed_math.gif',false,
1081: function(editor,id) {
1082: editor.surroundHTML(' <m>\$','\$</m> ');
1083: }
1084: );
1085: config.registerButton('ed_math_eqn','LaTeX Equation',
1086: '/htmlarea/images/ed_math_eqn.gif',false,
1087: function(editor,id) {
1088: editor.surroundHTML(
1089: ' \\n<center><m>\\\\[','\\\\]</m></center>\\n ');
1090: }
1091: );
1092: config.toolbar.push(['ed_math','ed_math_eqn']);
1093: ENDADDBUTTON
1094: }
1095:
1096: # ----------------------------------------------------------------- Preferences
1097:
1098: sub disablelink {
1099: my @fields=@_;
1100: if (defined($#fields)) {
1101: unless ($#fields>=0) { return ''; }
1102: }
1103: return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=off&returnurl=','<>&"').&escape($ENV{'REQUEST_URI'}).'">'.&mt('Disable WYSIWYG Editor').'</a>';
1104: }
1105:
1106: sub enablelink {
1107: my @fields=@_;
1108: if (defined($#fields)) {
1109: unless ($#fields>=0) { return ''; }
1110: }
1111: return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=on&returnurl=','<>&"').&escape($ENV{'REQUEST_URI'}).'">'.&mt('Enable WYSIWYG Editor').'</a>';
1112: }
1113:
1114: # ----------------------------------------- Script to activate only some fields
1115:
1116: sub htmlareaselectactive {
1117: my @fields=@_;
1118: unless (&htmlareabrowser()) { return ''; }
1119: if (&htmlareablocked()) { return '<br />'.&enablelink(@fields); }
1120: my $output='<script type="text/javascript" defer="1">'.
1121: &htmlareaaddbuttons();
1122: foreach(@fields) {
1123: $output.="\nHTMLArea.replace('$_',config);";
1124: }
1125: $output.="\nwindow.status='Activated Editfields';\n</script><br />".
1126: &disablelink(@fields);
1127: return $output;
1128: }
1129:
1130: # --------------------------------------------------------------------- Blocked
1131:
1132: sub htmlareablocked {
1133: unless ($env{'environment.wysiwygeditor'} eq 'on') { return 1; }
1134: return 0;
1135: }
1136:
1137: # ---------------------------------------- Browser capable of running HTMLArea?
1138:
1139: sub htmlareabrowser {
1140: return 1;
1141: }
1142:
1143: ############################################################
1144: ############################################################
1145:
1146: =pod
1147:
1148: =item breadcrumbs
1149:
1150: Compiles the previously registered breadcrumbs into an series of links.
1151: FAQ and BUG links will be placed on the left side of the table if they
1152: are defined for the last registered breadcrumb.
1153: Additionally supports a 'component', which will be displayed on the
1154: right side of the table (without a link).
1155: A link to help for the component will be included if one is specified.
1156:
1157: All inputs can be undef without problems.
1158:
1159: Inputs: $color (the background color of the table returned),
1160: $component (the large text on the right side of the table),
1161: $component_help
1162: $function (role to get colors from)
1163: $domain (domian of role)
1164: $menulink (boolean, controls whether to include a link to /adm/menu)
1165:
1166: Returns a string containing breadcrumbs for the current page.
1167:
1168: =item clear_breadcrumbs
1169:
1170: Clears the previously stored breadcrumbs.
1171:
1172: =item add_breadcrumb
1173:
1174: Pushes a breadcrumb on the stack of crumbs.
1175:
1176: input: $breadcrumb, a hash reference. The keys 'href','title', and 'text'
1177: are required. If present the keys 'faq' and 'bug' will be used to provide
1178: links to the FAQ and bug sites.
1179:
1180: returns: nothing
1181:
1182: =cut
1183:
1184: ############################################################
1185: ############################################################
1186: {
1187: my @Crumbs;
1188:
1189: sub breadcrumbs {
1190: my ($component,$component_help,$menulink,$helplink) = @_;
1191: #
1192: my $Str = "\n".'<table id="LC_breadcrumbs"><tr><td>';
1193: #
1194: # Make the faq and bug data cascade
1195: my $faq = '';
1196: my $bug = '';
1197: my $help='';
1198: # The last breadcrumb does not have a link, so handle it separately.
1199: my $last = pop(@Crumbs);
1200: #
1201: # The first one should be the course or a menu link
1202: if (!defined($menulink)) { $menulink=1; }
1203: if ($menulink) {
1204: my $description = 'Menu';
1205: if (exists($env{'request.course.id'}) &&
1206: $env{'request.course.id'} ne '') {
1207: $description =
1208: $env{'course.'.$env{'request.course.id'}.'.description'};
1209: }
1210: unshift(@Crumbs,{
1211: href =>'/adm/menu',
1212: title =>'Go to main menu',
1213: target =>'_top',
1214: text =>$description,
1215: });
1216: }
1217: my $links .=
1218: join('->',
1219: map {
1220: $faq = $_->{'faq'} if (exists($_->{'faq'}));
1221: $bug = $_->{'bug'} if (exists($_->{'bug'}));
1222: $help = $_->{'help'} if (exists($_->{'help'}));
1223: my $result = '<a href="'.$_->{'href'}.'" ';
1224: if (defined($_->{'target'}) && $_->{'target'} ne '') {
1225: $result .= 'target="'.$_->{'target'}.'" ';
1226: }
1227: $result .='title="'.&mt($_->{'title'}).'">'.
1228: &mt($_->{'text'}).'</a>';
1229: $result;
1230: } @Crumbs
1231: );
1232: $links .= '->' if ($links ne '');
1233: $links .= '<b>'.&mt($last->{'text'}).'</b>';
1234: #
1235: my $icons = '';
1236: $faq = $last->{'faq'} if (exists($last->{'faq'}));
1237: $bug = $last->{'bug'} if (exists($last->{'bug'}));
1238: $help = $last->{'help'} if (exists($last->{'help'}));
1239: $component_help=($component_help?$component_help:$help);
1240: # if ($faq ne '') {
1241: # $icons .= &Apache::loncommon::help_open_faq($faq);
1242: # }
1243: # if ($bug ne '') {
1244: # $icons .= &Apache::loncommon::help_open_bug($bug);
1245: # }
1246: if ($helplink ne 'nohelp') {
1247: $icons .= &Apache::loncommon::help_open_menu(undef,$component,
1248: $component_help,
1249: undef,$faq,$bug);
1250: }
1251: if ($icons ne '') {
1252: $Str .= $icons.' ';
1253: }
1254: #
1255: $Str .= $links.'</td>';
1256: #
1257: if (defined($component)) {
1258: $Str .= '<td class="LC_breadcrumb_component">'.
1259: &mt($component).'</td>';
1260: }
1261: $Str .= '</tr></table>'."\n";
1262: #
1263: # Return the @Crumbs stack to what we started with
1264: push(@Crumbs,$last);
1265: shift(@Crumbs);
1266: #
1267: return $Str;
1268: }
1269:
1270: sub clear_breadcrumbs {
1271: undef(@Crumbs);
1272: }
1273:
1274: sub add_breadcrumb {
1275: push (@Crumbs,@_);
1276: }
1277:
1278: } # End of scope for @Crumbs
1279:
1280: ############################################################
1281: ############################################################
1282:
1283: # Nested table routines.
1284: #
1285: # Routines to display form items in a multi-row table with 2 columns.
1286: # Uses nested tables to divide form elements into segments.
1287: # For examples of use see loncom/interface/lonnotify.pm
1288: #
1289: # Can be used in following order: ...
1290: # &start_pick_box()
1291: # row1
1292: # row2
1293: # row3 ... etc.
1294: # &submit_row(0
1295: # &end_pickbox()
1296: #
1297: # where row1, row 2 etc. are chosen from &role_select_row,&course_select_row,
1298: # &status_select_row and &email_default_row
1299: #
1300: # Can also be used in following order:
1301: #
1302: # &start_pick_box()
1303: # &row_title()
1304: # &row_closure()
1305: # &row_title()
1306: # &row_closure() ... etc.
1307: # &submit_row()
1308: # &end_pick_box()
1309: #
1310: # In general a &submit_row() call should proceed the call to &end_pick_box(),
1311: # as this routine adds a button for form submission.
1312: # &submit_row() does not require a &row_closure after it.
1313: #
1314: # &start_pick_box() creates a bounding table with 1-pixel wide black border.
1315: # rows should be placed between calls to &start_pick_box() and &end_pick_box.
1316: #
1317: # &row_title() adds a title in the left column for each segment.
1318: # &row_closure() closes a row with a 1-pixel wide black line.
1319: #
1320: # &role_select_row() provides a select box from which to choose 1 or more roles
1321: # &course_select_row provides ways of picking groups of courses
1322: # radio buttons: all, by category or by picking from a course picker pop-up
1323: # note: by category option is only displayed if a domain has implemented
1324: # selection by year, semester, department, number etc.
1325: #
1326: # &status_select_row() provides a select box from which to choose 1 or more
1327: # access types (current access, prior access, and future access)
1328: #
1329: # &email_default_row() provides text boxes for default e-mail suffixes for
1330: # different authentication types in a domain.
1331: #
1332: # &row_title() and &row_closure() are called internally by the &*_select_row
1333: # routines, but can also be called directly to start and end rows which have
1334: # needs that are not accommodated by the *_select_row() routines.
1335:
1336: sub start_pick_box {
1337: my ($table_width) = @_;
1338: my $output = <<"END";
1339: <table width="$table_width" border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">
1340: <tr>
1341: <td>
1342: <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff">
1343: <tr>
1344: <td>
1345: <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#ffffff">
1346: END
1347: return $output;
1348: }
1349:
1350: sub end_pick_box {
1351: my $output = <<"END";
1352: </table>
1353: </td>
1354: </tr>
1355: </table>
1356: </td>
1357: </tr>
1358: </table>
1359: END
1360: return $output;
1361: }
1362:
1363: sub row_title {
1364: my ($col_width,$tablecolor,$title) = @_;
1365: my $output = <<"ENDONE";
1366: <tr>
1367: <td width="$col_width" bgcolor="$tablecolor">
1368: <table width="$col_width" border="0" cellpadding="8" cellspacing="0">
1369: <tr>
1370: <td align="right"><b>$title:</b>
1371: </td>
1372: </tr>
1373: </table>
1374: </td>
1375: <td width="100%" valign="top">
1376: <table width="100%" border="0" cellpadding="8" cellspacing="0">
1377: <tr>
1378: ENDONE
1379: return $output;
1380: }
1381:
1382: sub row_closure {
1383: my $output = <<"ENDTWO";
1384: </tr>
1385: </table>
1386: </td>
1387: </tr>
1388: <tr>
1389: <td width="100%" colspan="2" bgcolor="#000000">
1390: <img src="/adm/lonMisc/blackdot.gif" /><br />
1391: </td>
1392: </tr>
1393: ENDTWO
1394: return $output;
1395: }
1396:
1397: sub role_select_row {
1398: my ($roles,$col_width,$tablecolor,$title) = @_;
1399: my $output;
1400: if (defined($title)) {
1401: $output = &row_title($col_width,$tablecolor,$title);
1402: }
1403: $output .= qq| <td valign="top">
1404: <select name="roles" multiple >\n|;
1405: foreach my $role (@$roles) {
1406: my $plrole;
1407: if ($role eq 'ow') {
1408: $plrole = &mt('Course Owner');
1409: } else {
1410: $plrole=&Apache::lonnet::plaintext($role);
1411: }
1412: $output .= ' <option value="'.$role.'">'.$plrole.'</option>';
1413: }
1414: $output .= qq| </select>
1415: </td>\n|;
1416: if (defined($title)) {
1417: $output .= &row_closure();
1418: }
1419: return $output;
1420: }
1421:
1422: sub course_select_row {
1423: my ($col_width,$tablecolor,$title,$formname,$totcodes,$codetitles,$idlist,$idlist_titles) = @_;
1424: my $output = &row_title($col_width,$tablecolor,$title);
1425: $output .= " <td>\n";
1426: $output .= qq|
1427: <script type="text/javascript" language="Javascript" >
1428: function coursePick (formname) {
1429: for (var i=0; i<formname.coursepick.length; i++) {
1430: if (formname.coursepick[i].value == 'category') {
1431: courseSet('');
1432: }
1433: if (!formname.coursepick[i].checked) {
1434: if (formname.coursepick[i].value == 'specific') {
1435: formname.coursetotal.value = 0;
1436: formname.courselist = '';
1437: }
1438: }
1439: }
1440: }
1441: function setPick (formname) {
1442: for (var i=0; i<formname.coursepick.length; i++) {
1443: if (formname.coursepick[i].value == 'category') {
1444: formname.coursepick[i].checked = true;
1445: }
1446: formname.coursetotal.value = 0;
1447: formname.courselist = '';
1448: }
1449: }
1450: </script>
1451: |;
1452: my $courseform='<b>'.&Apache::loncommon::selectcourse_link
1453: ($formname,'pickcourse','pickdomain','coursedesc','',1).'</b>';
1454: $output .= '<input type="radio" name="coursepick" value="all" onclick="coursePick(this.form)" />'.&mt('All courses').'<br />';
1455: if ($totcodes > 0) {
1456: my $numtitles = @$codetitles;
1457: if ($numtitles > 0) {
1458: $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 />';
1459: $output .= '<table><tr><td>'.$$codetitles[0].'<br />'."\n".
1460: '<select name="'.$$codetitles[0].
1461: '" onChange="setPick(this.form);courseSet('."'$$codetitles[0]'".')">'."\n".
1462: ' <option value="-1" />Select'."\n";
1463: my @items = ();
1464: my @longitems = ();
1465: if ($$idlist{$$codetitles[0]} =~ /","/) {
1466: @items = split(/","/,$$idlist{$$codetitles[0]});
1467: } else {
1468: $items[0] = $$idlist{$$codetitles[0]};
1469: }
1470: if (defined($$idlist_titles{$$codetitles[0]})) {
1471: if ($$idlist_titles{$$codetitles[0]} =~ /","/) {
1472: @longitems = split(/","/,$$idlist_titles{$$codetitles[0]});
1473: } else {
1474: $longitems[0] = $$idlist_titles{$$codetitles[0]};
1475: }
1476: for (my $i=0; $i<@longitems; $i++) {
1477: if ($longitems[$i] eq '') {
1478: $longitems[$i] = $items[$i];
1479: }
1480: }
1481: } else {
1482: @longitems = @items;
1483: }
1484: for (my $i=0; $i<@items; $i++) {
1485: $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
1486: }
1487: $output .= '</select></td>';
1488: for (my $i=1; $i<$numtitles; $i++) {
1489: $output .= '<td>'.$$codetitles[$i].'<br />'."\n".
1490: '<select name="'.$$codetitles[$i].
1491: '" onChange="courseSet('."'$$codetitles[$i]'".')">'."\n".
1492: '<option value="-1"><-Pick '.$$codetitles[$i-1].'</option>'."\n".
1493: '</select>'."\n".
1494: '</td>';
1495: }
1496: $output .= '</tr></table><br />';
1497: }
1498: }
1499: $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 /></td>'."\n";
1500: $output .= &row_closure();
1501: return $output;
1502: }
1503:
1504: sub status_select_row {
1505: my ($types,$col_width,$tablecolor,$title) = @_;
1506: my $output;
1507: if (defined($title)) {
1508: $output = &row_title($col_width,$tablecolor,$title);
1509: }
1510: $output .= qq| <td valign="top">
1511: <select name="types" multiple>\n|;
1512: foreach my $status_type (sort(keys(%{$types}))) {
1513: $output .= ' <option value="'.$status_type.'">'.$$types{$status_type}.'</option>';
1514: }
1515: $output .= qq| </select>
1516: </td>\n|;
1517: if (defined($title)) {
1518: $output .= &row_closure();
1519: }
1520: return $output;
1521: }
1522:
1523: sub email_default_row {
1524: my ($authtypes,$col_width,$tablecolor,$title,$descrip) = @_;
1525: my $output = &row_title($col_width,$tablecolor,$title);
1526: my @rowcols = ('#eeeeee','#dddddd');
1527: $output .= ' <td>'.$descrip;
1528: $output .= &start_pick_box('');
1529: $output .= ' <tr bgcolor="'.$tablecolor.'">
1530: <td><b>'.&mt('Authentication Method').'</b></td><td align="right"><b>'.&mt('Username -> e-mail conversion').'</b></td>
1531: </tr>'."\n";
1532: my $rownum = 0;
1533: foreach my $auth (sort(keys(%{$authtypes}))) {
1534: my ($userentry,$size);
1535: my $rowiter = $rownum%2;
1536: if ($auth =~ /^krb/) {
1537: $userentry = '';
1538: $size = 25;
1539: } else {
1540: $userentry = 'username@';
1541: $size = 15;
1542: }
1543: $output .= '<tr bgcolor="'.$rowcols[$rowiter].'"><td> '.$$authtypes{$auth}.'</td><td align="right">'.$userentry.'<input type="text" name="'.$auth.'" size="'.$size.'" /></td></tr>';
1544: $rownum ++;
1545: }
1546: $output .= &end_pick_box();
1547: $output .= " <br /></td>\n";
1548: $output .= &row_closure();
1549: return $output;
1550: }
1551:
1552:
1553: sub submit_row {
1554: my ($col_width,$tablecolor,$title,$cmd,$submit_text) = @_;
1555: my $output = &row_title($col_width,$tablecolor,$title);
1556: $output .= qq|
1557: <td width="100%" valign="top" align="right">
1558: <br />
1559: <input type="hidden" name="command" value="$cmd" />
1560: <input type="submit" value="$submit_text"/>
1561: <br /><br />
1562: </td>\n|;
1563: return $output;
1564: }
1565:
1566: ##############################################
1567: ##############################################
1568:
1569: # echo_form_input
1570: #
1571: # Generates html markup to add form elements from the referrer page
1572: # as hidden form elements (values encoded) in the new page.
1573: #
1574: # Intended to support two types of use
1575: # (a) to allow backing up to earlier pages in a multi-page
1576: # form submission process using a breadcrumb trail.
1577: #
1578: # (b) to allow the current page to be reloaded with form elements
1579: # set on previous page to remain unchanged. An example would
1580: # be where the a page containing a dynamically-built table of data is
1581: # is to be redisplayed, with only the sort order of the data changed.
1582: #
1583: # Inputs:
1584: # 1. Reference to array of form elements in the submitted form on
1585: # the referrer page which are to be excluded from the echoed elements.
1586: #
1587: # 2. Reference to array of regular expressions, which if matched in the
1588: # name of the form element n the referrer page will be omitted from echo.
1589: #
1590: # Outputs: A scalar containing the html markup for the echoed form
1591: # elements (all as hidden elements, with values encoded).
1592:
1593:
1594: sub echo_form_input {
1595: my ($excluded,$regexps) = @_;
1596: my $output = '';
1597: foreach my $key (keys(%env)) {
1598: if ($key =~ /^form\.(.+)$/) {
1599: my $name = $1;
1600: my $match = 0;
1601: if ((!@{$excluded}) || (!grep/^$name$/,@{$excluded})) {
1602: if (defined($regexps)) {
1603: if (@{$regexps} > 0) {
1604: foreach my $regexp (@{$regexps}) {
1605: if ($name =~ /\Q$regexp\E/) {
1606: $match = 1;
1607: last;
1608: }
1609: }
1610: }
1611: }
1612: if (!$match) {
1613: if (ref($env{$key})) {
1614: foreach my $value (@{$env{$key}}) {
1615: $value = &HTML::Entities::encode($value,'<>&"');
1616: $output .= '<input type="hidden" name="'.$name.
1617: '" value="'.$value.'" />'."\n";
1618: }
1619: } else {
1620: my $value = &HTML::Entities::encode($env{$key},'<>&"');
1621: $output .= '<input type="hidden" name="'.$name.
1622: '" value="'.$value.'" />'."\n";
1623: }
1624: }
1625: }
1626: }
1627: }
1628: return $output;
1629: }
1630:
1631: ##############################################
1632: ##############################################
1633:
1634: # set_form_elements
1635: #
1636: # Generates javascript to set form elements to values based on
1637: # corresponding values for the same form elements when the page was
1638: # previously submitted.
1639: #
1640: # Last submission values are read from hidden form elements in referring
1641: # page which have the same name, i.e., generated by &echo_form_input().
1642: #
1643: # Intended to be called by onload event.
1644: #
1645: # Inputs:
1646: # (a) Reference to hash of echoed form elements to be set.
1647: #
1648: # In the hash, keys are the form element names, and the values are the
1649: # element type (selectbox, radio, checkbox or text -for textbox, textarea or
1650: # hidden).
1651: #
1652: # (b) Optional reference to hash of stored elements to be set.
1653: #
1654: # If the page being displayed is a page which permits modification of
1655: # previously stored data, e.g., the first page in a multi-page submission,
1656: # then if stored is supplied, form elements will be set to the last stored
1657: # values. If user supplied values are also available for the same elements
1658: # these will replace the stored values.
1659: #
1660: # Output:
1661: #
1662: # javascript function - set_form_elements() which sets form elements,
1663: # expects an argument: formname - the name of the form according to
1664: # the DOM, e.g., document.compose
1665:
1666: sub set_form_elements {
1667: my ($elements,$stored) = @_;
1668: my %values;
1669: my $output .= 'function setFormElements(courseForm) {
1670: ';
1671: if (defined($stored)) {
1672: foreach my $name (keys(%{$stored})) {
1673: if (exists($$elements{$name})) {
1674: if (ref($$stored{$name}) eq 'ARRAY') {
1675: $values{$name} = $$stored{$name};
1676: } else {
1677: @{$values{$name}} = ($$stored{$name});
1678: }
1679: }
1680: }
1681: }
1682:
1683: foreach my $key (keys(%env)) {
1684: if ($key =~ /^form\.(.+)$/) {
1685: my $name = $1;
1686: if (exists($$elements{$name})) {
1687: @{$values{$name}} = &Apache::loncommon::get_env_multiple($key);
1688: }
1689: }
1690: }
1691:
1692: foreach my $name (keys(%values)) {
1693: for (my $i=0; $i<@{$values{$name}}; $i++) {
1694: $values{$name}[$i] = &HTML::Entities::decode($values{$name}[$i],'<>&"');
1695: $values{$name}[$i] =~ s/([\r\n\f]+)/\\n/g;
1696: $values{$name}[$i] =~ s/"/\\"/g;
1697: }
1698: if ($$elements{$name} eq 'text') {
1699: my $numvalues = @{$values{$name}};
1700: if ($numvalues > 1) {
1701: my $valuestring = join('","',@{$values{$name}});
1702: $output .= qq|
1703: var textvalues = new Array ("$valuestring");
1704: var total = courseForm.$name.length;
1705: if (total > $numvalues) {
1706: total = $numvalues;
1707: }
1708: for (var i=0; i<total; i++) {
1709: courseForm.$name\[i].value = textvalues[i];
1710: }
1711: |;
1712: } else {
1713: $output .= qq|
1714: courseForm.$name.value = "$values{$name}[0]";
1715: |;
1716: }
1717: } else {
1718: $output .= qq|
1719: var elementLength = courseForm.$name.length;
1720: if (elementLength==undefined) {
1721: |;
1722: foreach my $value (@{$values{$name}}) {
1723: if ($$elements{$name} eq 'selectbox') {
1724: $output .= qq|
1725: if (courseForm.$name.options[0].value == "$value") {
1726: courseForm.$name.options[0].selected = true;
1727: }|;
1728: } elsif (($$elements{$name} eq 'radio') ||
1729: ($$elements{$name} eq 'checkbox')) {
1730: $output .= qq|
1731: if (courseForm.$name.value == "$value") {
1732: courseForm.$name.checked = true;
1733: }|;
1734: }
1735: }
1736: $output .= qq|
1737: }
1738: else {
1739: for (var i=0; i<courseForm.$name.length; i++) {
1740: |;
1741: if ($$elements{$name} eq 'selectbox') {
1742: $output .= qq|
1743: courseForm.$name.options[i].selected = false;|;
1744: } elsif (($$elements{$name} eq 'radio') ||
1745: ($$elements{$name} eq 'checkbox')) {
1746: $output .= qq|
1747: courseForm.$name\[i].checked = false;|;
1748: }
1749: $output .= qq|
1750: }
1751: for (var j=0; j<courseForm.$name.length; j++) {
1752: |;
1753: foreach my $value (@{$values{$name}}) {
1754: if ($$elements{$name} eq 'selectbox') {
1755: $output .= qq|
1756: if (courseForm.$name.options[j].value == "$value") {
1757: courseForm.$name.options[j].selected = true;
1758: }|;
1759: } elsif (($$elements{$name} eq 'radio') ||
1760: ($$elements{$name} eq 'checkbox')) {
1761: $output .= qq|
1762: if (courseForm.$name\[j].value == "$value") {
1763: courseForm.$name\[j].checked = true;
1764: }|;
1765: }
1766: }
1767: $output .= qq|
1768: }
1769: }
1770: |;
1771: }
1772: }
1773: $output .= "
1774: }\n";
1775: return $output;
1776: }
1777:
1778: 1;
1779:
1780: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>