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