Annotation of loncom/interface/lonhtmlcommon.pm, revision 1.66

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>