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