Annotation of loncom/interface/lonhtmlcommon.pm, revision 1.103
1.2 www 1: # The LearningOnline Network with CAPA
2: # a pile of common html routines
3: #
1.103 ! albertel 4: # $Id: lonhtmlcommon.pm,v 1.102 2004/12/02 00:01:34 raeburn Exp $
1.2 www 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.10 matthew 28: ######################################################################
29: ######################################################################
30:
31: =pod
32:
33: =head1 NAME
34:
35: Apache::lonhtmlcommon - routines to do common html things
36:
37: =head1 SYNOPSIS
38:
39: Referenced by other mod_perl Apache modules.
40:
41: =head1 INTRODUCTION
42:
43: lonhtmlcommon is a collection of subroutines used to present information
44: in a consistent html format, or provide other functionality related to
45: html.
46:
47: =head2 General Subroutines
48:
49: =over 4
50:
51: =cut
52:
53: ######################################################################
54: ######################################################################
1.2 www 55:
1.1 stredwic 56: package Apache::lonhtmlcommon;
57:
1.10 matthew 58: use Time::Local;
1.47 sakharuk 59: use Time::HiRes;
1.30 www 60: use Apache::lonlocal;
1.1 stredwic 61: use strict;
62:
1.40 www 63: ##############################################
64: ##############################################
65:
66: =pod
67:
68: =item authorbombs
69:
70: =cut
71:
72: ##############################################
73: ##############################################
74:
75: sub authorbombs {
76: my $url=shift;
77: $url=&Apache::lonnet::declutter($url);
78: my ($udom,$uname)=($url=~/^(\w+)\/(\w+)\//);
79: my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
80: foreach (keys %bombs) {
81: if ($_=~/^$udom\/$uname\//) {
82: return '<a href="/adm/bombs/'.$url.
1.103 ! albertel 83: '"><img src="'.&Apache::loncommon::lonhttpdurl('/adm/lonMisc/bomb.gif').'" border="0" /></a>'.
1.40 www 84: &Apache::loncommon::help_open_topic('About_Bombs');
85: }
86: }
87: return '';
88: }
1.26 matthew 89:
90: ##############################################
91: ##############################################
92:
1.41 www 93: sub recent_filename {
94: my $area=shift;
95: return 'nohist_recent_'.&Apache::lonnet::escape($area);
96: }
97:
98: sub store_recent {
99: my ($area,$name,$value)=@_;
100: my $file=&recent_filename($area);
101: my %recent=&Apache::lonnet::dump($file);
102: if (scalar(keys(%recent))>10) {
103: # remove oldest value
104: my $oldest=time;
105: my $delkey='';
106: foreach (keys %recent) {
107: my $thistime=(split(/\&/,$recent{$_}))[0];
108: if ($thistime<$oldest) {
109: $oldest=$thistime;
110: $delkey=$_;
111: }
112: }
113: &Apache::lonnet::del($file,[$delkey]);
114: }
115: # store new value
116: &Apache::lonnet::put($file,{ $name =>
117: time.'&'.&Apache::lonnet::escape($value) });
118: }
119:
1.89 banghart 120: sub remove_recent {
121: my ($area,$names)=@_;
122: my $file=&recent_filename($area);
123: return &Apache::lonnet::del($file,$names);
124: }
125:
1.41 www 126: sub select_recent {
127: my ($area,$fieldname,$event)=@_;
128: my %recent=&Apache::lonnet::dump(&recent_filename($area));
129: my $return="\n<select name='$fieldname'".
1.96 albertel 130: ($event?" onchange='$event'":'').
1.41 www 131: ">\n<option value=''>--- ".&mt('Recent')." ---</option>";
132: foreach (sort keys %recent) {
133: unless ($_=~/^error\:/) {
1.94 foxr 134: my $escaped = &Apache::loncommon::escape_url($_);
135: $return.="\n<option value='$escaped'>".
1.41 www 136: &Apache::lonnet::unescape((split(/\&/,$recent{$_}))[1]).
137: '</option>';
138: }
139: }
140: $return.="\n</select>\n";
141: return $return;
142: }
143:
1.97 albertel 144: sub get_recent {
145: my ($area, $n) = @_;
146: my %recent=&Apache::lonnet::dump(&recent_filename($area));
147:
148: # Create hash with key as time and recent as value
149: my %time_hash = ();
150: foreach (keys %recent) {
151: my $thistime=(split(/\&/,$recent{$_}))[0];
152: $time_hash{$thistime} = $_;
153: }
154:
155: # Sort by decreasing time and return key value pairs
156: my %return_hash = ();
157: my $idx = 1;
158: foreach (reverse sort keys %time_hash) {
159: $return_hash{$time_hash{$_}} =
160: &Apache::lonnet::unescape((split(/\&/,$recent{$_}))[1]);
161: if ($n && ($idx++ >= $n)) {last;}
162: }
163:
164: return %return_hash;
165: }
166:
167:
1.41 www 168:
1.26 matthew 169: =pod
170:
171: =item textbox
172:
173: =cut
174:
175: ##############################################
176: ##############################################
177: sub textbox {
178: my ($name,$value,$size,$special) = @_;
179: $size = 40 if (! defined($size));
180: my $Str = '<input type="text" name="'.$name.'" size="'.$size.'" '.
181: 'value="'.$value.'" '.$special.' />';
182: return $Str;
183: }
184:
185: ##############################################
186: ##############################################
187:
188: =pod
189:
190: =item checkbox
191:
192: =cut
193:
194: ##############################################
195: ##############################################
196: sub checkbox {
1.68 matthew 197: my ($name,$checked,$value) = @_;
198: my $Str = '<input type="checkbox" name="'.$name.'" ';
199: if (defined($value)) {
200: $Str .= 'value="'.$value.'"';
201: }
202: if ($checked) {
203: $Str .= ' checked="1"';
204: }
205: $Str .= ' />';
1.26 matthew 206: return $Str;
207: }
208:
1.10 matthew 209: ##############################################
210: ##############################################
211:
212: =pod
213:
214: =item &date_setter
215:
1.22 matthew 216: &date_setter returns html and javascript for a compact date-setting form.
217: To retrieve values from it, use &get_date_from_form().
218:
1.10 matthew 219: Inputs
220:
221: =over 4
222:
223: =item $dname
224:
225: The name to prepend to the form elements.
226: The form elements defined will be dname_year, dname_month, dname_day,
227: dname_hour, dname_min, and dname_sec.
228:
229: =item $currentvalue
230:
231: The current setting for this time parameter. A unix format time
232: (time in seconds since the beginning of Jan 1st, 1970, GMT.
233: An undefined value is taken to indicate the value is the current time.
234: Also, to be explicit, a value of 'now' also indicates the current time.
235:
1.26 matthew 236: =item $special
237:
238: Additional html/javascript to be associated with each element in
239: the date_setter. See lonparmset for example usage.
240:
1.59 matthew 241: =item $includeempty
242:
243: =item $state
244:
245: Specifies the initial state of the form elements. Either 'disabled' or empty.
246: Defaults to empty, which indiciates the form elements are not disabled.
247:
1.22 matthew 248: =back
249:
250: Bugs
251:
252: The method used to restrict user input will fail in the year 2400.
253:
1.10 matthew 254: =cut
255:
256: ##############################################
257: ##############################################
258: sub date_setter {
1.67 matthew 259: my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
260: $no_hh_mm_ss) = @_;
1.59 matthew 261: if (! defined($state) || $state ne 'disabled') {
262: $state = '';
263: }
1.67 matthew 264: if (! defined($no_hh_mm_ss)) {
265: $no_hh_mm_ss = 0;
266: }
1.10 matthew 267: if (! defined($currentvalue) || $currentvalue eq 'now') {
1.39 www 268: unless ($includeempty) {
269: $currentvalue = time;
270: } else {
271: $currentvalue = 0;
272: }
1.10 matthew 273: }
274: # other potentially useful values: wkday,yrday,is_daylight_savings
1.65 albertel 275: my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
1.39 www 276: if ($currentvalue) {
277: ($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) =
278: localtime($currentvalue);
279: $year += 1900;
280: }
1.10 matthew 281: my $result = "\n<!-- $dname date setting form -->\n";
282: $result .= <<ENDJS;
283: <script language="Javascript">
284: function $dname\_checkday() {
285: var day = document.$formname.$dname\_day.value;
286: var month = document.$formname.$dname\_month.value;
287: var year = document.$formname.$dname\_year.value;
288: var valid = true;
289: if (day < 1) {
290: document.$formname.$dname\_day.value = 1;
291: }
292: if (day > 31) {
293: document.$formname.$dname\_day.value = 31;
294: }
295: if ((month == 1) || (month == 3) || (month == 5) ||
296: (month == 7) || (month == 8) || (month == 10) ||
297: (month == 12)) {
298: if (day > 31) {
299: document.$formname.$dname\_day.value = 31;
300: day = 31;
301: }
302: } else if (month == 2 ) {
303: if ((year % 4 == 0) && (year % 100 != 0)) {
304: if (day > 29) {
305: document.$formname.$dname\_day.value = 29;
306: }
307: } else if (day > 29) {
308: document.$formname.$dname\_day.value = 28;
309: }
310: } else if (day > 30) {
311: document.$formname.$dname\_day.value = 30;
312: }
313: }
1.95 matthew 314:
1.59 matthew 315: function $dname\_disable() {
316: document.$formname.$dname\_month.disabled=true;
317: document.$formname.$dname\_day.disabled=true;
318: document.$formname.$dname\_year.disabled=true;
319: document.$formname.$dname\_hour.disabled=true;
320: document.$formname.$dname\_minute.disabled=true;
321: document.$formname.$dname\_second.disabled=true;
322: }
323:
324: function $dname\_enable() {
325: document.$formname.$dname\_month.disabled=false;
326: document.$formname.$dname\_day.disabled=false;
327: document.$formname.$dname\_year.disabled=false;
328: document.$formname.$dname\_hour.disabled=false;
329: document.$formname.$dname\_minute.disabled=false;
330: document.$formname.$dname\_second.disabled=false;
331: }
332:
1.29 www 333: function $dname\_opencalendar() {
1.59 matthew 334: if (! document.$formname.$dname\_month.disabled) {
335: var calwin=window.open(
1.29 www 336: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
337: document.$formname.$dname\_month.value+"&year="+
338: document.$formname.$dname\_year.value,
339: "LONCAPAcal",
340: "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
1.59 matthew 341: }
1.29 www 342:
343: }
1.10 matthew 344: </script>
345: ENDJS
1.95 matthew 346: $result .= ' <nobr>';
1.96 albertel 347: my $monthselector = qq{<select name="$dname\_month" $special $state onchange="javascript:$dname\_checkday()" >};
1.67 matthew 348: # Month
1.10 matthew 349: my @Months = qw/January February March April May June
350: July August September October November December/;
351: # Pad @Months with a bogus value to make indexing easier
352: unshift(@Months,'If you can read this an error occurred');
1.95 matthew 353: if ($includeempty) { $monthselector.="<option value=''></option>"; }
1.10 matthew 354: for(my $m = 1;$m <=$#Months;$m++) {
1.95 matthew 355: $monthselector .= qq{ <option value="$m" };
356: $monthselector .= "selected " if ($m-1 eq $month);
357: $monthselector .= '> '.&mt($Months[$m]).' </option>';
1.10 matthew 358: }
1.95 matthew 359: $monthselector.= ' </select>';
1.67 matthew 360: # Day
1.96 albertel 361: my $dayselector = qq{<input type="text" name="$dname\_day" $state value="$mday" size="3" $special onchange="javascript:$dname\_checkday()" />};
1.67 matthew 362: # Year
1.96 albertel 363: my $yearselector = qq{<input type="year" name="$dname\_year" $state value="$year" size="5" $special onchange="javascript:$dname\_checkday()" />};
1.95 matthew 364: #
365: my $hourselector = qq{<select name="$dname\_hour" $special $state >};
366: if ($includeempty) {
367: $hourselector.=qq{<option value=''></option>};
368: }
369: for (my $h = 0;$h<24;$h++) {
370: $hourselector .= qq{<option value="$h" };
371: $hourselector .= "selected " if (defined($hour) && $hour == $h);
372: $hourselector .= ">";
373: my $timest='';
374: if ($h == 0) {
375: $timest .= "12 am";
376: } elsif($h == 12) {
377: $timest .= "12 noon";
378: } elsif($h < 12) {
379: $timest .= "$h am";
380: } else {
381: $timest .= $h-12 ." pm";
382: }
383: $timest=&mt($timest);
384: $hourselector .= $timest." </option>\n";
385: }
386: $hourselector .= " </select>\n";
387: my $minuteselector = qq{<input type="text" name="$dname\_minute" $special $state value="$min" size="3" />};
388: my $secondselector= qq{<input type="text" name="$dname\_second" $special $state value="$sec" size="3" />};
389: my $cal_link = qq{<a href="javascript:$dname\_opencalendar()">};
390: #
391: if ($no_hh_mm_ss) {
392: $result .= &mt('[_1] [_2] [_3] [_4]Select Date[_5]',
393: $monthselector,$dayselector,$yearselector,
394: $cal_link,'</a>');
395: } else {
396: $result .= &mt('[_1] [_2] [_3] [_4] [_5]m [_6]s [_7]Select Date[_8]',
397: $monthselector,$dayselector,$yearselector,
398: $hourselector,$minuteselector,$secondselector,
399: $cal_link,'</a>');
1.67 matthew 400: }
1.95 matthew 401: $result .= "</nobr>\n<!-- end $dname date setting form -->\n";
1.10 matthew 402: return $result;
403: }
404:
405: ##############################################
406: ##############################################
407:
1.22 matthew 408: =pod
409:
1.10 matthew 410: =item &get_date_from_form
1.22 matthew 411:
412: get_date_from_form retrieves the date specified in an &date_setter form.
1.10 matthew 413:
414: Inputs:
415:
416: =over 4
417:
418: =item $dname
419:
420: The name passed to &datesetter, which prefixes the form elements.
421:
422: =item $defaulttime
423:
424: The unix time to use as the default in case of poor inputs.
425:
426: =back
427:
428: Returns: Unix time represented in the form.
429:
430: =cut
431:
432: ##############################################
433: ##############################################
434: sub get_date_from_form {
435: my ($dname) = @_;
436: my ($sec,$min,$hour,$day,$month,$year);
437: #
438: if (defined($ENV{'form.'.$dname.'_second'})) {
439: my $tmpsec = $ENV{'form.'.$dname.'_second'};
440: if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
441: $sec = $tmpsec;
442: }
1.64 albertel 443: if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
1.67 matthew 444: } else {
445: $sec = 0;
1.10 matthew 446: }
447: if (defined($ENV{'form.'.$dname.'_minute'})) {
448: my $tmpmin = $ENV{'form.'.$dname.'_minute'};
449: if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
450: $min = $tmpmin;
451: }
1.64 albertel 452: if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
1.67 matthew 453: } else {
454: $min = 0;
1.10 matthew 455: }
456: if (defined($ENV{'form.'.$dname.'_hour'})) {
457: my $tmphour = $ENV{'form.'.$dname.'_hour'};
1.33 matthew 458: if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
1.10 matthew 459: $hour = $tmphour;
460: }
1.67 matthew 461: } else {
462: $hour = 0;
1.10 matthew 463: }
464: if (defined($ENV{'form.'.$dname.'_day'})) {
465: my $tmpday = $ENV{'form.'.$dname.'_day'};
466: if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
467: $day = $tmpday;
468: }
469: }
470: if (defined($ENV{'form.'.$dname.'_month'})) {
471: my $tmpmonth = $ENV{'form.'.$dname.'_month'};
472: if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
473: $month = $tmpmonth - 1;
474: }
475: }
476: if (defined($ENV{'form.'.$dname.'_year'})) {
477: my $tmpyear = $ENV{'form.'.$dname.'_year'};
478: if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
479: $year = $tmpyear - 1900;
480: }
481: }
1.24 www 482: if (($year<70) || ($year>137)) { return undef; }
1.33 matthew 483: if (defined($sec) && defined($min) && defined($hour) &&
484: defined($day) && defined($month) && defined($year) &&
485: eval(&timelocal($sec,$min,$hour,$day,$month,$year))) {
1.10 matthew 486: return &timelocal($sec,$min,$hour,$day,$month,$year);
487: } else {
488: return undef;
489: }
1.20 matthew 490: }
491:
492: ##############################################
493: ##############################################
494:
495: =pod
496:
497: =item &pjump_javascript_definition()
498:
499: Returns javascript defining the 'pjump' function, which opens up a
500: parameter setting wizard.
501:
502: =cut
503:
504: ##############################################
505: ##############################################
506: sub pjump_javascript_definition {
507: my $Str = <<END;
508: function pjump(type,dis,value,marker,ret,call) {
509: parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
510: +"&value="+escape(value)+"&marker="+escape(marker)
511: +"&return="+escape(ret)
512: +"&call="+escape(call)+"&name="+escape(dis),"LONCAPAparms",
513: "height=350,width=350,scrollbars=no,menubar=no");
514: }
515: END
516: return $Str;
1.10 matthew 517: }
518:
519: ##############################################
520: ##############################################
1.17 matthew 521:
522: =pod
523:
524: =item &javascript_nothing()
525:
526: Return an appropriate null for the users browser. This is used
527: as the first arguement for window.open calls when you want a blank
528: window that you can then write to.
529:
530: =cut
531:
532: ##############################################
533: ##############################################
534: sub javascript_nothing {
535: # mozilla and other browsers work with "''", but IE on mac does not.
536: my $nothing = "''";
537: my $user_browser;
538: my $user_os;
539: $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
540: $user_os = $ENV{'browser.os'} if (exists($ENV{'browser.os'}));
541: if (! defined($user_browser) || ! defined($user_os)) {
542: (undef,$user_browser,undef,undef,undef,$user_os) =
543: &Apache::loncommon::decode_user_agent();
544: }
545: if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
546: $nothing = "'javascript:void(0);'";
547: }
548: return $nothing;
549: }
550:
1.90 www 551: ##############################################
552: ##############################################
553: sub javascript_docopen {
554: # safari does not understand document.open() and loads "text/html"
555: my $nothing = "''";
556: my $user_browser;
557: my $user_os;
558: $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
559: $user_os = $ENV{'browser.os'} if (exists($ENV{'browser.os'}));
560: if (! defined($user_browser) || ! defined($user_os)) {
561: (undef,$user_browser,undef,undef,undef,$user_os) =
562: &Apache::loncommon::decode_user_agent();
563: }
564: if ($user_browser eq 'safari' && $user_os =~ 'mac') {
565: $nothing = "document.clear()";
566: } else {
567: $nothing = "document.open('text/html','replace')";
568: }
569: return $nothing;
570: }
571:
1.21 matthew 572:
1.17 matthew 573: ##############################################
574: ##############################################
575:
1.21 matthew 576: =pod
1.17 matthew 577:
1.21 matthew 578: =item &StatusOptions()
1.10 matthew 579:
1.21 matthew 580: Returns html for a selection box which allows the user to choose the
581: enrollment status of students. The selection box name is 'Status'.
1.6 stredwic 582:
1.21 matthew 583: Inputs:
1.6 stredwic 584:
1.21 matthew 585: $status: the currently selected status. If undefined the value of
586: $ENV{'form.Status'} is taken. If that is undefined, a value of 'Active'
587: is used.
1.6 stredwic 588:
1.21 matthew 589: $formname: The name of the form. If defined the onchange attribute of
590: the selection box is set to document.$formname.submit().
1.6 stredwic 591:
1.21 matthew 592: $size: the size (number of lines) of the selection box.
1.6 stredwic 593:
1.27 matthew 594: $onchange: javascript to use when the value is changed. Enclosed in
595: double quotes, ""s, not single quotes.
596:
1.21 matthew 597: Returns: a perl string as described.
1.1 stredwic 598:
1.21 matthew 599: =cut
1.9 stredwic 600:
1.21 matthew 601: ##############################################
602: ##############################################
603: sub StatusOptions {
1.27 matthew 604: my ($status, $formName,$size,$onchange)=@_;
1.21 matthew 605: $size = 1 if (!defined($size));
606: if (! defined($status)) {
607: $status = 'Active';
608: $status = $ENV{'form.Status'} if (exists($ENV{'form.Status'}));
1.9 stredwic 609: }
1.1 stredwic 610:
611: my $OpSel1 = '';
612: my $OpSel2 = '';
613: my $OpSel3 = '';
614:
615: if($status eq 'Any') { $OpSel3 = ' selected'; }
616: elsif($status eq 'Expired' ) { $OpSel2 = ' selected'; }
617: else { $OpSel1 = ' selected'; }
618:
619: my $Str = '';
620: $Str .= '<select name="Status"';
1.27 matthew 621: if(defined($formName) && $formName ne '' && ! defined($onchange)) {
1.1 stredwic 622: $Str .= ' onchange="document.'.$formName.'.submit()"';
1.27 matthew 623: }
624: if (defined($onchange)) {
625: $Str .= ' onchange="'.$onchange.'"';
1.1 stredwic 626: }
1.21 matthew 627: $Str .= ' size="'.$size.'" ';
1.1 stredwic 628: $Str .= '>'."\n";
1.21 matthew 629: $Str .= '<option value="Active" '.$OpSel1.'>'.
1.37 www 630: &mt('Currently Enrolled').'</option>'."\n";
1.21 matthew 631: $Str .= '<option value="Expired" '.$OpSel2.'>'.
1.37 www 632: &mt('Previously Enrolled').'</option>'."\n";
1.21 matthew 633: $Str .= '<option value="Any" '.$OpSel3.'>'.
1.37 www 634: &mt('Any Enrollment Status').'</option>'."\n";
1.1 stredwic 635: $Str .= '</select>'."\n";
1.7 stredwic 636: }
1.12 matthew 637:
638: ########################################################
639: ########################################################
1.7 stredwic 640:
1.23 matthew 641: =pod
642:
643: =item Progess Window Handling Routines
644:
645: These routines handle the creation, update, increment, and closure of
646: progress windows. The progress window reports to the user the number
647: of items completed and an estimate of the time required to complete the rest.
648:
649: =over 4
650:
651:
652: =item &Create_PrgWin
653:
654: Writes javascript to the client to open a progress window and returns a
655: data structure used for bookkeeping.
656:
657: Inputs
658:
659: =over 4
660:
661: =item $r Apache request
662:
663: =item $title The title of the progress window
664:
665: =item $heading A description (usually 1 line) of the process being initiated.
666:
667: =item $number_to_do The total number of items being processed.
1.50 albertel 668:
669: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
670: specified)
671:
1.51 albertel 672: =item $width Specify the width in charaters of the input field.
673:
1.50 albertel 674: =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
675:
676: =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 677:
678: =back
679:
680: Returns a hash containing the progress state data structure.
681:
682:
683: =item &Update_PrgWin
684:
685: Updates the text in the progress indicator. Does not increment the count.
686: See &Increment_PrgWin.
687:
688: Inputs:
689:
690: =over 4
691:
692: =item $r Apache request
693:
694: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
695:
696: =item $displaystring The string to write to the status indicator
697:
698: =back
699:
700: Returns: none
701:
702:
703: =item Increment_PrgWin
704:
705: Increment the count of items completed for the progress window by 1.
706:
707: Inputs:
708:
709: =over 4
710:
711: =item $r Apache request
712:
713: =item $prog_state Pointer to the data structure returned by Create_PrgWin
714:
715: =item $extraInfo A description of the items being iterated over. Typically
716: 'student'.
717:
718: =back
719:
720: Returns: none
721:
722:
723: =item Close_PrgWin
724:
725: Closes the progress window.
726:
727: Inputs:
728:
729: =over 4
730:
731: =item $r Apache request
732:
733: =item $prog_state Pointer to the data structure returned by Create_PrgWin
734:
735: =back
736:
737: Returns: none
738:
739: =back
740:
741: =cut
742:
743: ########################################################
744: ########################################################
745:
1.51 albertel 746: my $uniq=0;
747: sub get_uniq_name {
748: $uniq++;
749: return 'uniquename'.$uniq;
750: }
751:
1.7 stredwic 752: # Create progress
753: sub Create_PrgWin {
1.51 albertel 754: my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
755: $inputname)=@_;
1.49 albertel 756: if (!defined($type)) { $type='popup'; }
1.51 albertel 757: if (!defined($width)) { $width=55; }
1.49 albertel 758: my %prog_state;
759: $prog_state{'type'}=$type;
760: if ($type eq 'popup') {
761: $prog_state{'window'}='popwin';
1.103 ! albertel 762: my $html=&Apache::lonxml::xmlbegin();
1.49 albertel 763: #the whole function called through timeout is due to issues
764: #in mozilla Read BUG #2665 if you want to know the whole story
765: &r_print($r,'<script>'.
766: "var popwin;
767: function openpopwin () {
768: popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
1.103 ! albertel 769: "popwin.document.writeln(\'".$html."<head><title>$title</title></head>".
1.48 albertel 770: "<body bgcolor=\"#88DDFF\">".
771: "<h4>$heading</h4>".
772: "<form name=popremain>".
1.51 albertel 773: '<input type="text" size="'.$width.'" name="remaining" value="'.
1.48 albertel 774: &mt('Starting').'"></form>'.
775: "</body></html>\');".
1.49 albertel 776: "popwin.document.close();}".
777: "\nwindow.setTimeout(openpopwin,0)</script>");
778: $prog_state{'formname'}='popremain';
779: $prog_state{'inputname'}="remaining";
780: } elsif ($type eq 'inline') {
781: $prog_state{'window'}='window';
782: if (!$formname) {
1.51 albertel 783: $prog_state{'formname'}=&get_uniq_name();
784: &r_print($r,'<form name="'.$prog_state{'formname'}.'">');
1.49 albertel 785: } else {
786: $prog_state{'formname'}=$formname;
787: }
788: if (!$inputname) {
1.51 albertel 789: $prog_state{'inputname'}=&get_uniq_name();
1.56 albertel 790: &r_print($r,$heading.' <input type="text" name="'.$prog_state{'inputname'}.
1.51 albertel 791: '" size="'.$width.'" />');
1.49 albertel 792: } else {
793: $prog_state{'inputname'}=$inputname;
794:
795: }
796: if (!$formname) { &r_print($r,'</form>'); }
797: &Update_PrgWin($r,\%prog_state,&mt('Starting'));
798: }
1.7 stredwic 799:
1.16 albertel 800: $prog_state{'done'}=0;
1.23 matthew 801: $prog_state{'firststart'}=&Time::HiRes::time();
802: $prog_state{'laststart'}=&Time::HiRes::time();
1.16 albertel 803: $prog_state{'max'}=$number_to_do;
1.49 albertel 804:
1.14 albertel 805: return %prog_state;
1.7 stredwic 806: }
807:
808: # update progress
809: sub Update_PrgWin {
1.14 albertel 810: my ($r,$prog_state,$displayString)=@_;
1.49 albertel 811: &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
812: $$prog_state{'formname'}.'.'.
813: $$prog_state{'inputname'}.'.value="'.
1.48 albertel 814: $displayString.'";</script>');
1.23 matthew 815: $$prog_state{'laststart'}=&Time::HiRes::time();
1.14 albertel 816: }
817:
818: # increment progress state
819: sub Increment_PrgWin {
820: my ($r,$prog_state,$extraInfo)=@_;
1.16 albertel 821: $$prog_state{'done'}++;
1.23 matthew 822: my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
823: $$prog_state{'done'} *
1.16 albertel 824: ($$prog_state{'max'}-$$prog_state{'done'});
825: $time_est = int($time_est);
1.80 matthew 826: #
827: my $min = int($time_est/60);
828: my $sec = $time_est % 60;
829: #
830: my $str;
1.91 albertel 831: if ($min == 0 && $sec > 1) {
1.80 matthew 832: $str = '[_2] seconds';
1.91 albertel 833: } elsif ($min == 1 && $sec > 1) {
834: $str = '1 minute [_2] seconds';
1.80 matthew 835: } elsif ($min == 1 && $sec < 2) {
836: $str = '1 minute';
837: } elsif ($min < 10 && $sec > 1) {
838: $str = '[_1] minutes, [_2] seconds';
1.81 matthew 839: } elsif ($min >= 10 || $sec < 2) {
1.80 matthew 840: $str = '[_1] minutes';
1.16 albertel 841: }
1.80 matthew 842: $time_est = &mt($str,$min,$sec);
843: #
1.23 matthew 844: my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
845: if ($lasttime > 9) {
846: $lasttime = int($lasttime);
847: } elsif ($lasttime < 0.01) {
848: $lasttime = 0;
849: } else {
850: $lasttime = sprintf("%3.2f",$lasttime);
851: }
1.19 matthew 852: if ($lasttime == 1) {
1.32 www 853: $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
1.19 matthew 854: } else {
1.32 www 855: $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
1.28 matthew 856: }
857: #
858: my $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
859: my $user_os = $ENV{'browser.os'} if (exists($ENV{'browser.os'}));
860: if (! defined($user_browser) || ! defined($user_os)) {
861: (undef,$user_browser,undef,undef,undef,$user_os) =
862: &Apache::loncommon::decode_user_agent();
863: }
864: if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
865: $lasttime = '';
1.19 matthew 866: }
1.49 albertel 867: &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
868: $$prog_state{'formname'}.'.'.
869: $$prog_state{'inputname'}.'.value="'.
1.48 albertel 870: $$prog_state{'done'}.'/'.$$prog_state{'max'}.
871: ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
1.23 matthew 872: $$prog_state{'laststart'}=&Time::HiRes::time();
1.7 stredwic 873: }
874:
875: # close Progress Line
876: sub Close_PrgWin {
1.14 albertel 877: my ($r,$prog_state)=@_;
1.49 albertel 878: if ($$prog_state{'type'} eq 'popup') {
879: &r_print($r,'<script>popwin.close()</script>'."\n");
880: } elsif ($$prog_state{'type'} eq 'inline') {
881: &Update_PrgWin($r,$prog_state,&mt('Done'));
882: }
1.48 albertel 883: undef(%$prog_state);
884: }
885:
886: sub r_print {
887: my ($r,$to_print)=@_;
888: if ($r) {
889: $r->print($to_print);
890: $r->rflush();
1.47 sakharuk 891: } else {
1.48 albertel 892: print($to_print);
1.47 sakharuk 893: }
1.1 stredwic 894: }
1.34 www 895:
896: # ------------------------------------------------------- Puts directory header
897:
898: sub crumbs {
1.78 www 899: my ($uri,$target,$prefix,$form,$size,$noformat)=@_;
1.62 matthew 900: if (! defined($size)) {
901: $size = '+2';
902: }
1.100 raeburn 903: if ($target) {
904: $target = ' target="'.
905: &Apache::loncommon::escape_single($target).'"';
906: }
1.78 www 907: my $output='';
908: unless ($noformat) { $output.='<br /><tt><b>'; }
909: $output.='<font size="'.$size.'">'.$prefix.'/';
1.35 www 910: if ($ENV{'user.adv'}) {
1.43 www 911: my $path=$prefix.'/';
1.99 matthew 912: foreach my $dir (split('/',$uri)) {
913: if (! $dir) { next; }
914: $path .= $dir;
1.43 www 915: unless ($path eq $uri) { $path.='/'; }
1.99 matthew 916: my $linkpath = &Apache::loncommon::escape_single($path);
1.98 matthew 917: if ($form) {
1.99 matthew 918: $linkpath=
919: qq{javascript:$form.action='$linkpath';$form.submit();};
1.98 matthew 920: }
1.99 matthew 921: $output.=qq{<a href="$linkpath" $target>$dir</a>/};
1.35 www 922: }
923: } else {
924: $output.=$uri;
1.34 www 925: }
1.36 www 926: unless ($uri=~/\/$/) { $output=~s/\/$//; }
1.78 www 927: return $output.'</font>'.($noformat?'':'</b></tt><br />');
1.34 www 928: }
929:
1.85 www 930: # --------------------- A function that generates a window for the spellchecker
931:
932: sub spellheader {
1.103 ! albertel 933: my $html=&Apache::lonxml::xmlbegin();
1.85 www 934: return (<<ENDCHECK);
935: <script type="text/javascript">
1.92 albertel 936: //<!-- BEGIN LON-CAPA Internal
1.85 www 937: var checkwin;
938:
939: function spellcheckerwindow() {
1.103 ! albertel 940: checkwin=window.open('/adm/rat/empty.html','spellcheckwin','height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
! 941: checkwin.document.writeln('$html<head></head><body bgcolor="#DDDDDD"><form name="spellcheckform" action="/adm/spellcheck" method="post"><input type="hidden" name="text" value="" /></form></body></html>');
1.85 www 942: checkwin.document.close();
943: }
1.92 albertel 944: // END LON-CAPA Internal -->
1.85 www 945: </script>
946: ENDCHECK
947: }
948:
949: # ---------------------------------- Generate link to spell checker for a field
950:
951: sub spelllink {
952: my ($form,$field)=@_;
953: my $linktext=&mt('Check Spelling');
954: return (<<ENDLINK);
955: <a href="javascript:if (typeof(document.$form.onsubmit)!='undefined') { document.$form.onsubmit();};spellcheckerwindow();checkwin.document.forms.spellcheckform.text.value=this.document.forms.$form.$field.value;checkwin.document.forms.spellcheckform.submit();">$linktext</a>
956: ENDLINK
957: }
958:
1.52 www 959: # ------------------------------------------------- Output headers for HTMLArea
960:
961: sub htmlareaheaders {
1.71 www 962: if (&htmlareablocked()) { return ''; }
1.76 www 963: unless (&htmlareabrowser()) { return ''; }
1.52 www 964: my $lang='en';
1.71 www 965: if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
966: $lang=&mt('htmlarea_lang');
967: }
1.52 www 968: return (<<ENDHEADERS);
1.61 www 969: <script type="text/javascript">
1.73 www 970: _editor_url='/htmlarea/';
971: _editor_lang='$lang';
1.61 www 972: </script>
1.52 www 973: <script type="text/javascript" src="/htmlarea/htmlarea.js"></script>
974: ENDHEADERS
975: }
976:
1.74 www 977: # ------------------------------------------------- Activate additional buttons
978:
979: sub htmlareaaddbuttons {
980: if (&htmlareablocked()) { return ''; }
1.76 www 981: unless (&htmlareabrowser()) { return ''; }
1.74 www 982: return (<<ENDADDBUTTON);
983: var config=new HTMLArea.Config();
984: config.registerButton('ed_math','LaTeX Inline',
985: '/htmlarea/images/ed_math.gif',false,
986: function(editor,id) {
1.88 albertel 987: editor.surroundHTML(' <m>\$','\$</m> ');
1.74 www 988: }
989: );
990: config.registerButton('ed_math_eqn','LaTeX Equation',
991: '/htmlarea/images/ed_math_eqn.gif',false,
992: function(editor,id) {
1.75 www 993: editor.surroundHTML(
1.88 albertel 994: ' \\n<center><m>\\\\[','\\\\]</m></center>\\n ');
1.74 www 995: }
996: );
997: config.toolbar.push(['ed_math','ed_math_eqn']);
998: ENDADDBUTTON
999: }
1.76 www 1000:
1001: # ----------------------------------------------------------------- Preferences
1002:
1003: sub disablelink {
1.77 www 1004: my @fields=@_;
1005: if (defined($#fields)) {
1006: unless ($#fields>=0) { return ''; }
1007: }
1.93 albertel 1008: return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=off&returnurl=','<>&"').&Apache::lonnet::escape($ENV{'REQUEST_URI'}).'">'.&mt('Disable WYSIWYG Editor').'</a>';
1.76 www 1009: }
1010:
1011: sub enablelink {
1.77 www 1012: my @fields=@_;
1013: if (defined($#fields)) {
1014: unless ($#fields>=0) { return ''; }
1015: }
1.93 albertel 1016: return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=on&returnurl=','<>&"').&Apache::lonnet::escape($ENV{'REQUEST_URI'}).'">'.&mt('Enable WYSIWYG Editor').'</a>';
1.76 www 1017: }
1018:
1.72 www 1019: # ----------------------------------------- Script to activate only some fields
1020:
1021: sub htmlareaselectactive {
1.73 www 1022: my @fields=@_;
1.76 www 1023: unless (&htmlareabrowser()) { return ''; }
1.77 www 1024: if (&htmlareablocked()) { return '<br />'.&enablelink(@fields); }
1.74 www 1025: my $output='<script type="text/javascript" defer="1">'.
1026: &htmlareaaddbuttons();
1.73 www 1027: foreach(@fields) {
1.74 www 1028: $output.="\nHTMLArea.replace('$_',config);";
1.72 www 1029: }
1.76 www 1030: $output.="\nwindow.status='Activated Editfields';\n</script><br />".
1.77 www 1031: &disablelink(@fields);
1.72 www 1032: return $output;
1033: }
1034:
1.61 www 1035: # --------------------------------------------------------------------- Blocked
1036:
1037: sub htmlareablocked {
1.76 www 1038: unless ($ENV{'environment.wysiwygeditor'} eq 'on') { return 1; }
1.71 www 1039: return 0;
1.52 www 1040: }
1041:
1042: # ---------------------------------------- Browser capable of running HTMLArea?
1043:
1044: sub htmlareabrowser {
1045: return 1;
1046: }
1.53 matthew 1047:
1048: ############################################################
1049: ############################################################
1050:
1051: =pod
1052:
1053: =item breadcrumbs
1054:
1055: Compiles the previously registered breadcrumbs into an series of links.
1056: FAQ and BUG links will be placed on the left side of the table if they
1057: are defined for the last registered breadcrumb.
1058: Additionally supports a 'component', which will be displayed on the
1059: right side of the table (without a link).
1060: A link to help for the component will be included if one is specified.
1061:
1062: All inputs can be undef without problems.
1063:
1064: Inputs: $color (the background color of the table returned),
1065: $component (the large text on the right side of the table),
1066: $component_help
1.63 albertel 1067: $function (role to get colors from)
1068: $domain (domian of role)
1069: $menulink (boolean, controls whether to include a link to /adm/menu)
1.53 matthew 1070:
1071: Returns a string containing breadcrumbs for the current page.
1072:
1073: =item clear_breadcrumbs
1074:
1075: Clears the previously stored breadcrumbs.
1076:
1077: =item add_breadcrumb
1078:
1079: Pushes a breadcrumb on the stack of crumbs.
1080:
1081: input: $breadcrumb, a hash reference. The keys 'href','title', and 'text'
1082: are required. If present the keys 'faq' and 'bug' will be used to provide
1083: links to the FAQ and bug sites.
1084:
1085: returns: nothing
1086:
1087: =cut
1088:
1089: ############################################################
1090: ############################################################
1091: {
1092: my @Crumbs;
1.57 matthew 1093:
1.53 matthew 1094: sub breadcrumbs {
1.87 albertel 1095: my ($color,$component,$component_help,$function,$domain,$menulink,
1096: $helplink) = @_;
1.55 matthew 1097: if (! defined($color)) {
1098: if (! defined($function)) {
1099: $function = &Apache::loncommon::get_users_function();
1100: }
1101: $color = &Apache::loncommon::designparm($function.'.tabbg',
1102: $domain);
1103: }
1.53 matthew 1104: #
1105: my $Str = "\n".
1106: '<table width="100%" border="0" cellpadding="0" cellspacing="0">'.
1107: '<tr><td bgcolor="'.$color.'">'.
1108: '<font size="-1">';
1.57 matthew 1109: #
1110: # Make the faq and bug data cascade
1111: my $faq = '';
1112: my $bug = '';
1.60 www 1113: # The last breadcrumb does not have a link, so handle it separately.
1.53 matthew 1114: my $last = pop(@Crumbs);
1.57 matthew 1115: #
1.70 matthew 1116: # The first one should be the course or a menu link
1.63 albertel 1117: if (!defined($menulink)) { $menulink=1; }
1.70 matthew 1118: if ($menulink) {
1119: my $description = 'Menu';
1120: if (exists($ENV{'request.course.id'}) &&
1121: $ENV{'request.course.id'} ne '') {
1122: $description =
1123: $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
1124: }
1.57 matthew 1125: unshift(@Crumbs,{
1.70 matthew 1126: href =>'/adm/menu',
1127: title =>'Go to main menu',
1128: target =>'_top',
1129: text =>$description,
1130: });
1.53 matthew 1131: }
1132: my $links .=
1133: join('->',
1134: map {
1.57 matthew 1135: $faq = $_->{'faq'} if (exists($_->{'faq'}));
1136: $bug = $_->{'bug'} if (exists($_->{'bug'}));
1.69 matthew 1137: my $result = '<a href="'.$_->{'href'}.'" ';
1138: if (defined($_->{'target'}) && $_->{'target'} ne '') {
1139: $result .= 'target="'.$_->{'target'}.'" ';
1140: }
1141: $result .='title="'.&mt($_->{'title'}).'">'.
1142: &mt($_->{'text'}).'</a>';
1143: $result;
1.53 matthew 1144: } @Crumbs
1145: );
1146: $links .= '->' if ($links ne '');
1.82 albertel 1147: $links .= '<b>'.&mt($last->{'text'}).'</b>';
1.54 matthew 1148: #
1149: my $icons = '';
1.57 matthew 1150: $faq = $last->{'faq'} if (exists($last->{'faq'}));
1151: $bug = $last->{'bug'} if (exists($last->{'bug'}));
1.79 raeburn 1152: # if ($faq ne '') {
1153: # $icons .= &Apache::loncommon::help_open_faq($faq);
1154: # }
1155: # if ($bug ne '') {
1156: # $icons .= &Apache::loncommon::help_open_bug($bug);
1157: # }
1.87 albertel 1158: if ($helplink ne 'nohelp') {
1159: $icons .= &Apache::loncommon::help_open_menu($color,$component,$component_help,$function,$faq,$bug);
1160: }
1.54 matthew 1161: if ($icons ne '') {
1162: $Str .= $icons.' ';
1.53 matthew 1163: }
1.54 matthew 1164: #
1.53 matthew 1165: $Str .= $links.'</font></td>';
1.54 matthew 1166: #
1.53 matthew 1167: if (defined($component)) {
1168: $Str .= '<td align="right" bgcolor="'.$color.'">'.
1.83 raeburn 1169: '<font size="+1">'.&mt($component).'</font></td>';
1.53 matthew 1170: }
1171: $Str .= '</tr></table>'."\n";
1172: #
1173: # Return the @Crumbs stack to what we started with
1174: push(@Crumbs,$last);
1175: shift(@Crumbs);
1176: #
1177: return $Str;
1178: }
1179:
1180: sub clear_breadcrumbs {
1181: undef(@Crumbs);
1182: }
1183:
1184: sub add_breadcrumb {
1185: push (@Crumbs,@_);
1186: }
1187:
1.57 matthew 1188: } # End of scope for @Crumbs
1.53 matthew 1189:
1190: ############################################################
1191: ############################################################
1192:
1.1 stredwic 1193:
1194: 1;
1.23 matthew 1195:
1.1 stredwic 1196: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>