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