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