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