Annotation of loncom/interface/lonhtmlcommon.pm, revision 1.31
1.2 www 1: # The LearningOnline Network with CAPA
2: # a pile of common html routines
3: #
1.31 ! www 4: # $Id: lonhtmlcommon.pm,v 1.30 2003/09/29 13:49:31 www 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.30 www 59: use Apache::lonlocal;
1.1 stredwic 60: use strict;
61:
1.26 matthew 62:
63: ##############################################
64: ##############################################
65:
66: =pod
67:
68: =item textbox
69:
70: =cut
71:
72: ##############################################
73: ##############################################
74: sub textbox {
75: my ($name,$value,$size,$special) = @_;
76: $size = 40 if (! defined($size));
77: my $Str = '<input type="text" name="'.$name.'" size="'.$size.'" '.
78: 'value="'.$value.'" '.$special.' />';
79: return $Str;
80: }
81:
82: ##############################################
83: ##############################################
84:
85: =pod
86:
87: =item checkbox
88:
89: =cut
90:
91: ##############################################
92: ##############################################
93: sub checkbox {
94: my ($name) = @_;
95: my $Str = '<input type="checkbox" name="'.$name.'" />';
96: return $Str;
97: }
98:
99:
100:
1.10 matthew 101: ##############################################
102: ##############################################
103:
104: =pod
105:
106: =item &date_setter
107:
1.22 matthew 108: &date_setter returns html and javascript for a compact date-setting form.
109: To retrieve values from it, use &get_date_from_form().
110:
1.10 matthew 111: Inputs
112:
113: =over 4
114:
115: =item $dname
116:
117: The name to prepend to the form elements.
118: The form elements defined will be dname_year, dname_month, dname_day,
119: dname_hour, dname_min, and dname_sec.
120:
121: =item $currentvalue
122:
123: The current setting for this time parameter. A unix format time
124: (time in seconds since the beginning of Jan 1st, 1970, GMT.
125: An undefined value is taken to indicate the value is the current time.
126: Also, to be explicit, a value of 'now' also indicates the current time.
127:
1.26 matthew 128: =item $special
129:
130: Additional html/javascript to be associated with each element in
131: the date_setter. See lonparmset for example usage.
132:
1.22 matthew 133: =back
134:
135: Bugs
136:
137: The method used to restrict user input will fail in the year 2400.
138:
1.10 matthew 139: =cut
140:
141: ##############################################
142: ##############################################
143: sub date_setter {
1.26 matthew 144: my ($formname,$dname,$currentvalue,$special) = @_;
1.10 matthew 145: if (! defined($currentvalue) || $currentvalue eq 'now') {
146: $currentvalue = time;
147: }
148: # other potentially useful values: wkday,yrday,is_daylight_savings
149: my ($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) =
150: localtime($currentvalue);
151: $year += 1900;
152: my $result = "\n<!-- $dname date setting form -->\n";
153: $result .= <<ENDJS;
154: <script language="Javascript">
155: function $dname\_checkday() {
156: var day = document.$formname.$dname\_day.value;
157: var month = document.$formname.$dname\_month.value;
158: var year = document.$formname.$dname\_year.value;
159: var valid = true;
160: if (day < 1) {
161: document.$formname.$dname\_day.value = 1;
162: }
163: if (day > 31) {
164: document.$formname.$dname\_day.value = 31;
165: }
166: if ((month == 1) || (month == 3) || (month == 5) ||
167: (month == 7) || (month == 8) || (month == 10) ||
168: (month == 12)) {
169: if (day > 31) {
170: document.$formname.$dname\_day.value = 31;
171: day = 31;
172: }
173: } else if (month == 2 ) {
174: if ((year % 4 == 0) && (year % 100 != 0)) {
175: if (day > 29) {
176: document.$formname.$dname\_day.value = 29;
177: }
178: } else if (day > 29) {
179: document.$formname.$dname\_day.value = 28;
180: }
181: } else if (day > 30) {
182: document.$formname.$dname\_day.value = 30;
183: }
184: }
1.29 www 185:
186: function $dname\_opencalendar() {
187: var calwin=window.open(
188: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
189: document.$formname.$dname\_month.value+"&year="+
190: document.$formname.$dname\_year.value,
191: "LONCAPAcal",
192: "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
193:
194: }
1.10 matthew 195: </script>
196: ENDJS
1.26 matthew 197: $result .= " <nobr><select name=\"$dname\_month\" ".$special.' '.
1.10 matthew 198: "onChange=\"javascript:$dname\_checkday()\" >\n";
199: my @Months = qw/January February March April May June
200: July August September October November December/;
201: # Pad @Months with a bogus value to make indexing easier
202: unshift(@Months,'If you can read this an error occurred');
203: for(my $m = 1;$m <=$#Months;$m++) {
204: $result .= " <option value=\"$m\" ";
205: $result .= "selected " if ($m-1 == $month);
1.30 www 206: $result .= "> ".&mt($Months[$m])." </option>\n";
1.10 matthew 207: }
208: $result .= " </select>\n";
209: $result .= " <input type=\"text\" name=\"$dname\_day\" ".
1.26 matthew 210: "value=\"$mday\" size=\"3\" ".$special.' '.
1.10 matthew 211: "onChange=\"javascript:$dname\_checkday()\" />\n";
212: $result .= " <input type=\"year\" name=\"$dname\_year\" ".
1.26 matthew 213: "value=\"$year\" size=\"5\" ".$special.' '.
1.10 matthew 214: "onChange=\"javascript:$dname\_checkday()\" />\n";
215: $result .= " ";
1.26 matthew 216: $result .= " <select name=\"$dname\_hour\" ".$special." >\n";
1.10 matthew 217: for (my $h = 0;$h<24;$h++) {
218: $result .= " <option value=\"$h\" ";
219: $result .= "selected " if ($hour == $h);
220: $result .= "> ";
1.30 www 221: my $timest='';
1.10 matthew 222: if ($h == 0) {
1.30 www 223: $timest .= "12 am";
1.10 matthew 224: } elsif($h == 12) {
1.30 www 225: $timest .= "12 noon";
1.10 matthew 226: } elsif($h < 12) {
1.30 www 227: $timest .= "$h am";
1.10 matthew 228: } else {
1.30 www 229: $timest .= $h-12 ." pm";
1.10 matthew 230: }
1.30 www 231: $timest=&mt($timest);
232: $result .= $timest." </option>\n";
1.10 matthew 233: }
234: $result .= " </select>\n";
1.26 matthew 235: $result .= " <input type=\"text\" name=\"$dname\_minute\" ".$special.' '.
1.10 matthew 236: "value=\"$min\" size=\"3\" /> m\n";
1.26 matthew 237: $result .= " <input type=\"text\" name=\"$dname\_second\" ".$special.' '.
1.10 matthew 238: "value=\"$sec\" size=\"3\" /> s\n";
1.30 www 239: $result .= "<a href=\"javascript:$dname\_opencalendar()\">".
240: &mt('Select Date')."</a></nobr>\n<!-- end $dname date setting form -->\n";
1.10 matthew 241: return $result;
242: }
243:
244: ##############################################
245: ##############################################
246:
1.22 matthew 247: =pod
248:
1.10 matthew 249: =item &get_date_from_form
1.22 matthew 250:
251: get_date_from_form retrieves the date specified in an &date_setter form.
1.10 matthew 252:
253: Inputs:
254:
255: =over 4
256:
257: =item $dname
258:
259: The name passed to &datesetter, which prefixes the form elements.
260:
261: =item $defaulttime
262:
263: The unix time to use as the default in case of poor inputs.
264:
265: =back
266:
267: Returns: Unix time represented in the form.
268:
269: =cut
270:
271: ##############################################
272: ##############################################
273: sub get_date_from_form {
274: my ($dname) = @_;
275: my ($sec,$min,$hour,$day,$month,$year);
276: #
277: if (defined($ENV{'form.'.$dname.'_second'})) {
278: my $tmpsec = $ENV{'form.'.$dname.'_second'};
279: if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
280: $sec = $tmpsec;
281: }
282: }
283: if (defined($ENV{'form.'.$dname.'_minute'})) {
284: my $tmpmin = $ENV{'form.'.$dname.'_minute'};
285: if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
286: $min = $tmpmin;
287: }
288: }
289: if (defined($ENV{'form.'.$dname.'_hour'})) {
290: my $tmphour = $ENV{'form.'.$dname.'_hour'};
291: if (($tmphour =~ /^\d+$/) && ($tmphour > 0) && ($tmphour < 32)) {
292: $hour = $tmphour;
293: }
294: }
295: if (defined($ENV{'form.'.$dname.'_day'})) {
296: my $tmpday = $ENV{'form.'.$dname.'_day'};
297: if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
298: $day = $tmpday;
299: }
300: }
301: if (defined($ENV{'form.'.$dname.'_month'})) {
302: my $tmpmonth = $ENV{'form.'.$dname.'_month'};
303: if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
304: $month = $tmpmonth - 1;
305: }
306: }
307: if (defined($ENV{'form.'.$dname.'_year'})) {
308: my $tmpyear = $ENV{'form.'.$dname.'_year'};
309: if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
310: $year = $tmpyear - 1900;
311: }
312: }
1.24 www 313: if (($year<70) || ($year>137)) { return undef; }
1.10 matthew 314: if (eval(&timelocal($sec,$min,$hour,$day,$month,$year))) {
315: return &timelocal($sec,$min,$hour,$day,$month,$year);
316: } else {
317: return undef;
318: }
1.20 matthew 319: }
320:
321: ##############################################
322: ##############################################
323:
324: =pod
325:
326: =item &pjump_javascript_definition()
327:
328: Returns javascript defining the 'pjump' function, which opens up a
329: parameter setting wizard.
330:
331: =cut
332:
333: ##############################################
334: ##############################################
335: sub pjump_javascript_definition {
336: my $Str = <<END;
337: function pjump(type,dis,value,marker,ret,call) {
338: parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
339: +"&value="+escape(value)+"&marker="+escape(marker)
340: +"&return="+escape(ret)
341: +"&call="+escape(call)+"&name="+escape(dis),"LONCAPAparms",
342: "height=350,width=350,scrollbars=no,menubar=no");
343: }
344: END
345: return $Str;
1.10 matthew 346: }
347:
348: ##############################################
349: ##############################################
1.17 matthew 350:
351: =pod
352:
353: =item &javascript_nothing()
354:
355: Return an appropriate null for the users browser. This is used
356: as the first arguement for window.open calls when you want a blank
357: window that you can then write to.
358:
359: =cut
360:
361: ##############################################
362: ##############################################
363: sub javascript_nothing {
364: # mozilla and other browsers work with "''", but IE on mac does not.
365: my $nothing = "''";
366: my $user_browser;
367: my $user_os;
368: $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
369: $user_os = $ENV{'browser.os'} if (exists($ENV{'browser.os'}));
370: if (! defined($user_browser) || ! defined($user_os)) {
371: (undef,$user_browser,undef,undef,undef,$user_os) =
372: &Apache::loncommon::decode_user_agent();
373: }
374: if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
375: $nothing = "'javascript:void(0);'";
376: }
377: return $nothing;
378: }
379:
1.21 matthew 380:
1.17 matthew 381: ##############################################
382: ##############################################
383:
1.21 matthew 384: =pod
1.17 matthew 385:
1.21 matthew 386: =item &StatusOptions()
1.10 matthew 387:
1.21 matthew 388: Returns html for a selection box which allows the user to choose the
389: enrollment status of students. The selection box name is 'Status'.
1.6 stredwic 390:
1.21 matthew 391: Inputs:
1.6 stredwic 392:
1.21 matthew 393: $status: the currently selected status. If undefined the value of
394: $ENV{'form.Status'} is taken. If that is undefined, a value of 'Active'
395: is used.
1.6 stredwic 396:
1.21 matthew 397: $formname: The name of the form. If defined the onchange attribute of
398: the selection box is set to document.$formname.submit().
1.6 stredwic 399:
1.21 matthew 400: $size: the size (number of lines) of the selection box.
1.6 stredwic 401:
1.27 matthew 402: $onchange: javascript to use when the value is changed. Enclosed in
403: double quotes, ""s, not single quotes.
404:
1.21 matthew 405: Returns: a perl string as described.
1.1 stredwic 406:
1.21 matthew 407: =cut
1.9 stredwic 408:
1.21 matthew 409: ##############################################
410: ##############################################
411: sub StatusOptions {
1.27 matthew 412: my ($status, $formName,$size,$onchange)=@_;
1.21 matthew 413: $size = 1 if (!defined($size));
414: if (! defined($status)) {
415: $status = 'Active';
416: $status = $ENV{'form.Status'} if (exists($ENV{'form.Status'}));
1.9 stredwic 417: }
1.1 stredwic 418:
419: my $OpSel1 = '';
420: my $OpSel2 = '';
421: my $OpSel3 = '';
422:
423: if($status eq 'Any') { $OpSel3 = ' selected'; }
424: elsif($status eq 'Expired' ) { $OpSel2 = ' selected'; }
425: else { $OpSel1 = ' selected'; }
426:
427: my $Str = '';
428: $Str .= '<select name="Status"';
1.27 matthew 429: if(defined($formName) && $formName ne '' && ! defined($onchange)) {
1.1 stredwic 430: $Str .= ' onchange="document.'.$formName.'.submit()"';
1.27 matthew 431: }
432: if (defined($onchange)) {
433: $Str .= ' onchange="'.$onchange.'"';
1.1 stredwic 434: }
1.21 matthew 435: $Str .= ' size="'.$size.'" ';
1.1 stredwic 436: $Str .= '>'."\n";
1.21 matthew 437: $Str .= '<option value="Active" '.$OpSel1.'>'.
438: 'Currently Enrolled</option>'."\n";
439: $Str .= '<option value="Expired" '.$OpSel2.'>'.
440: 'Previously Enrolled</option>'."\n";
441: $Str .= '<option value="Any" '.$OpSel3.'>'.
442: 'Any Enrollment Status</option>'."\n";
1.1 stredwic 443: $Str .= '</select>'."\n";
444: }
445:
1.12 matthew 446:
447: ########################################################
448: ########################################################
449:
450: =pod
451:
452: =item &MultipleSectionSelect()
453:
454: Inputs:
455:
456: =over 4
457:
458: =item $sections A references to an array containing the names of all the
459: sections used in a class.
460:
461: =item $selectedSections A reference to an array containing the names of the
462: currently selected sections.
463:
464: =back
465:
466: Returns: a string containing HTML for a multiple select box for
467: selecting sections of a course.
468:
469: The form element name is 'Section'. @$sections is sorted prior to output.
470:
471: =cut
472:
473: ########################################################
474: ########################################################
1.5 stredwic 475: sub MultipleSectionSelect {
476: my ($sections,$selectedSections)=@_;
477:
478: my $Str = '';
1.7 stredwic 479: $Str .= '<select name="Section" multiple="true" size="4">'."\n";
1.5 stredwic 480:
1.11 minaeibi 481: foreach (sort @$sections) {
1.5 stredwic 482: $Str .= '<option';
483: foreach my $selected (@$selectedSections) {
484: if($_ eq $selected) {
485: $Str .= ' selected=""';
486: }
487: }
488: $Str .= '>'.$_.'</option>'."\n";
489: }
490: $Str .= '</select>'."\n";
1.12 matthew 491:
1.5 stredwic 492: return $Str;
493: }
494:
1.12 matthew 495: ########################################################
496: ########################################################
497:
498: =pod
499:
500: =item &Title()
501:
502: Inputs: $pageName a string containing the name of the page to be sent
503: to &Apache::loncommon::bodytag.
504:
505: Returns: string containing being <html> and complete <head> and <title>
506: as well as a <script> to focus the current window and change its width
507: and height to 500. Why? I do not know. If you find out, please update
508: this documentation.
509:
510: =cut
511:
512: ########################################################
513: ########################################################
1.1 stredwic 514: sub Title {
515: my ($pageName)=@_;
516:
517: my $Str = '';
518:
519: $Str .= '<html><head><title>'.$pageName.'</title></head>'."\n";
1.8 www 520: $Str .= &Apache::loncommon::bodytag($pageName)."\n";
1.1 stredwic 521: $Str .= '<script>window.focus(); window.width=500;window.height=500;';
522: $Str .= '</script>'."\n";
523:
524: return $Str;
525: }
526:
1.12 matthew 527: ########################################################
528: ########################################################
529:
1.1 stredwic 530: =pod
531:
1.13 matthew 532: =item &CreateHeadings()
1.1 stredwic 533:
534: This function generates the column headings for the chart.
535:
536: =over 4
537:
1.4 stredwic 538: Inputs: $CacheData, $keyID, $headings, $spacePadding
1.1 stredwic 539:
540: $CacheData: pointer to a hash tied to the cached data database
541:
1.4 stredwic 542: $keyID: a pointer to an array containing the names of the data
1.1 stredwic 543: held in a column and is used as part of a key into $CacheData
544:
545: $headings: The names of the headings for the student information
546:
547: $spacePadding: The spaces to go between columns
548:
549: Output: $Str
550:
551: $Str: A formatted string of the table column headings.
552:
553: =back
554:
555: =cut
556:
1.12 matthew 557: ########################################################
558: ########################################################
1.4 stredwic 559: sub CreateHeadings {
560: my ($data,$keyID,$headings,$displayString,$format)=@_;
1.1 stredwic 561: my $Str='';
1.4 stredwic 562: my $formatting = '';
1.1 stredwic 563:
564: for(my $index=0; $index<(scalar @$headings); $index++) {
1.4 stredwic 565: my $currentHeading=$headings->[$index];
566: if($format eq 'preformatted') {
567: my @dataLength=split(//,$currentHeading);
568: my $length=scalar @dataLength;
569: $formatting = (' 'x
570: ($data->{$keyID->[$index].':columnWidth'}-$length));
571: }
572: my $linkdata=$keyID->[$index];
573:
1.1 stredwic 574: my $tempString = $displayString;
575: $tempString =~ s/LINKDATA/$linkdata/;
1.4 stredwic 576: $tempString =~ s/DISPLAYDATA/$currentHeading/;
577: $tempString =~ s/FORMATTING/$formatting/;
578:
1.1 stredwic 579: $Str .= $tempString;
580: }
581:
582: return $Str;
583: }
584:
1.12 matthew 585: ########################################################
586: ########################################################
587:
1.1 stredwic 588: =pod
589:
590: =item &FormatStudentInformation()
591:
1.10 matthew 592: This function produces a formatted string of the student\'s information:
1.1 stredwic 593: username, domain, section, full name, and PID.
594:
595: =over 4
596:
1.4 stredwic 597: Input: $cache, $name, $keyID, $spacePadding
1.1 stredwic 598:
599: $cache: This is a pointer to a hash that is tied to the cached data
600:
601: $name: The name and domain of the current student in name:domain format
602:
1.4 stredwic 603: $keyID: A pointer to an array holding the names used to
1.1 stredwic 604:
605: remove data from the hash. They represent the name of the data to be removed.
606:
607: $spacePadding: Extra spaces that represent the space between columns
608:
609: Output: $Str
610:
611: $Str: Formatted string.
612:
613: =back
614:
615: =cut
616:
1.12 matthew 617: ########################################################
618: ########################################################
1.1 stredwic 619: sub FormatStudentInformation {
1.4 stredwic 620: my ($data,$name,$keyID,$displayString,$format)=@_;
1.1 stredwic 621: my $Str='';
1.4 stredwic 622: my $currentColumn;
623:
624: for(my $index=0; $index<(scalar @$keyID); $index++) {
625: $currentColumn=$data->{$name.':'.$keyID->[$index]};
1.1 stredwic 626:
1.4 stredwic 627: if($format eq 'preformatted') {
628: my @dataLength=split(//,$currentColumn);
629: my $length=scalar @dataLength;
630: $currentColumn.= (' 'x
631: ($data->{$keyID->[$index].':columnWidth'}-$length));
1.1 stredwic 632: }
633:
1.4 stredwic 634: my $tempString = $displayString;
635: $tempString =~ s/DISPLAYDATA/$currentColumn/;
636:
637: $Str .= $tempString;
1.1 stredwic 638: }
639:
640: return $Str;
1.7 stredwic 641: }
1.12 matthew 642:
643: ########################################################
644: ########################################################
1.7 stredwic 645:
1.23 matthew 646: =pod
647:
648: =item Progess Window Handling Routines
649:
650: These routines handle the creation, update, increment, and closure of
651: progress windows. The progress window reports to the user the number
652: of items completed and an estimate of the time required to complete the rest.
653:
654: =over 4
655:
656:
657: =item &Create_PrgWin
658:
659: Writes javascript to the client to open a progress window and returns a
660: data structure used for bookkeeping.
661:
662: Inputs
663:
664: =over 4
665:
666: =item $r Apache request
667:
668: =item $title The title of the progress window
669:
670: =item $heading A description (usually 1 line) of the process being initiated.
671:
672: =item $number_to_do The total number of items being processed.
673:
674: =back
675:
676: Returns a hash containing the progress state data structure.
677:
678:
679: =item &Update_PrgWin
680:
681: Updates the text in the progress indicator. Does not increment the count.
682: See &Increment_PrgWin.
683:
684: Inputs:
685:
686: =over 4
687:
688: =item $r Apache request
689:
690: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
691:
692: =item $displaystring The string to write to the status indicator
693:
694: =back
695:
696: Returns: none
697:
698:
699: =item Increment_PrgWin
700:
701: Increment the count of items completed for the progress window by 1.
702:
703: Inputs:
704:
705: =over 4
706:
707: =item $r Apache request
708:
709: =item $prog_state Pointer to the data structure returned by Create_PrgWin
710:
711: =item $extraInfo A description of the items being iterated over. Typically
712: 'student'.
713:
714: =back
715:
716: Returns: none
717:
718:
719: =item Close_PrgWin
720:
721: Closes the progress window.
722:
723: Inputs:
724:
725: =over 4
726:
727: =item $r Apache request
728:
729: =item $prog_state Pointer to the data structure returned by Create_PrgWin
730:
731: =back
732:
733: Returns: none
734:
735: =back
736:
737: =cut
738:
739: ########################################################
740: ########################################################
741:
1.7 stredwic 742: # Create progress
743: sub Create_PrgWin {
1.14 albertel 744: my ($r, $title, $heading, $number_to_do)=@_;
1.7 stredwic 745: $r->print('<script>'.
746: "popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
1.14 albertel 747: "popwin.document.writeln(\'<html><head><title>$title</title></head>".
748: "<body bgcolor=\"#88DDFF\">".
1.7 stredwic 749: "<h4>$heading</h4>".
750: "<form name=popremain>".
1.28 matthew 751: '<input type="text" size="55" name="remaining" value="Starting"></form>'.
1.7 stredwic 752: "</body></html>\');".
753: "popwin.document.close();".
754: "</script>");
755:
1.14 albertel 756: my %prog_state;
1.16 albertel 757: $prog_state{'done'}=0;
1.23 matthew 758: $prog_state{'firststart'}=&Time::HiRes::time();
759: $prog_state{'laststart'}=&Time::HiRes::time();
1.16 albertel 760: $prog_state{'max'}=$number_to_do;
1.14 albertel 761:
1.7 stredwic 762: $r->rflush();
1.14 albertel 763: return %prog_state;
1.7 stredwic 764: }
765:
766: # update progress
767: sub Update_PrgWin {
1.14 albertel 768: my ($r,$prog_state,$displayString)=@_;
1.7 stredwic 769: $r->print('<script>popwin.document.popremain.remaining.value="'.
770: $displayString.'";</script>');
1.23 matthew 771: $$prog_state{'laststart'}=&Time::HiRes::time();
1.14 albertel 772: $r->rflush();
773: }
774:
775: # increment progress state
776: sub Increment_PrgWin {
777: my ($r,$prog_state,$extraInfo)=@_;
1.16 albertel 778: $$prog_state{'done'}++;
1.23 matthew 779: my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
780: $$prog_state{'done'} *
1.16 albertel 781: ($$prog_state{'max'}-$$prog_state{'done'});
782: $time_est = int($time_est);
783: if (int ($time_est/60) > 0) {
784: my $min = int($time_est/60);
785: my $sec = $time_est % 60;
1.31 ! www 786: $time_est = $min.' '.&mt('minutes');
1.25 matthew 787: if ($min < 10) {
788: if ($sec > 1) {
1.31 ! www 789: $time_est.= ', '.$sec.' '.&mt('seconds');
1.25 matthew 790: } elsif ($sec > 0) {
1.31 ! www 791: $time_est.= ', '.$sec.' '.&mt('second');
1.25 matthew 792: }
793: }
1.16 albertel 794: } else {
1.31 ! www 795: $time_est .= ' '.&mt('seconds');
1.16 albertel 796: }
1.23 matthew 797: my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
798: if ($lasttime > 9) {
799: $lasttime = int($lasttime);
800: } elsif ($lasttime < 0.01) {
801: $lasttime = 0;
802: } else {
803: $lasttime = sprintf("%3.2f",$lasttime);
804: }
1.19 matthew 805: if ($lasttime == 1) {
1.31 ! www 806: $lasttime = '('.$lasttime.' '.&mt('second for').' '.&mt($extraInfo).')';
1.19 matthew 807: } else {
1.31 ! www 808: $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.&mt($extraInfo).')';
1.28 matthew 809: }
810: #
811: my $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
812: my $user_os = $ENV{'browser.os'} if (exists($ENV{'browser.os'}));
813: if (! defined($user_browser) || ! defined($user_os)) {
814: (undef,$user_browser,undef,undef,undef,$user_os) =
815: &Apache::loncommon::decode_user_agent();
816: }
817: if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
818: $lasttime = '';
1.19 matthew 819: }
1.14 albertel 820: $r->print('<script>popwin.document.popremain.remaining.value="'.
1.16 albertel 821: $$prog_state{'done'}.'/'.$$prog_state{'max'}.
1.31 ! www 822: ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
1.23 matthew 823: $$prog_state{'laststart'}=&Time::HiRes::time();
1.7 stredwic 824: $r->rflush();
825: }
826:
827: # close Progress Line
828: sub Close_PrgWin {
1.14 albertel 829: my ($r,$prog_state)=@_;
1.7 stredwic 830: $r->print('<script>popwin.close()</script>'."\n");
1.14 albertel 831: undef(%$prog_state);
1.7 stredwic 832: $r->rflush();
1.1 stredwic 833: }
834:
835: 1;
1.23 matthew 836:
1.1 stredwic 837: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>