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