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