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

1.2       www         1: # The LearningOnline Network with CAPA
                      2: # a pile of common html routines
                      3: #
1.46    ! matthew     4: # $Id: lonhtmlcommon.pm,v 1.45 2004/02/03 21:52:22 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.30      www        59: use Apache::lonlocal;
1.1       stredwic   60: use strict;
                     61: 
1.40      www        62: 
                     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.22      matthew   204: =back
                    205: 
                    206: Bugs
                    207: 
                    208: The method used to restrict user input will fail in the year 2400.
                    209: 
1.10      matthew   210: =cut
                    211: 
                    212: ##############################################
                    213: ##############################################
                    214: sub date_setter {
1.39      www       215:     my ($formname,$dname,$currentvalue,$special,$includeempty) = @_;
1.10      matthew   216:     if (! defined($currentvalue) || $currentvalue eq 'now') {
1.39      www       217: 	unless ($includeempty) {
                    218: 	    $currentvalue = time;
                    219: 	} else {
                    220: 	    $currentvalue = 0;
                    221: 	}
1.10      matthew   222:     }
                    223:     # other potentially useful values:     wkday,yrday,is_daylight_savings
1.39      www       224:     my ($sec,$min,$hour,$mday,$month,$year)=('','','','','','');
                    225:     if ($currentvalue) {
                    226: 	($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
                    227: 	    localtime($currentvalue);
                    228: 	$year += 1900;
                    229:     }
1.10      matthew   230:     my $result = "\n<!-- $dname date setting form -->\n";
                    231:     $result .= <<ENDJS;
                    232: <script language="Javascript">
                    233:     function $dname\_checkday() {
                    234:         var day   = document.$formname.$dname\_day.value;
                    235:         var month = document.$formname.$dname\_month.value;
                    236:         var year  = document.$formname.$dname\_year.value;
                    237:         var valid = true;
                    238:         if (day < 1) {
                    239:             document.$formname.$dname\_day.value = 1;
                    240:         } 
                    241:         if (day > 31) {
                    242:             document.$formname.$dname\_day.value = 31;
                    243:         }
                    244:         if ((month == 1)  || (month == 3)  || (month == 5)  ||
                    245:             (month == 7)  || (month == 8)  || (month == 10) ||
                    246:             (month == 12)) {
                    247:             if (day > 31) {
                    248:                 document.$formname.$dname\_day.value = 31;
                    249:                 day = 31;
                    250:             }
                    251:         } else if (month == 2 ) {
                    252:             if ((year % 4 == 0) && (year % 100 != 0)) {
                    253:                 if (day > 29) {
                    254:                     document.$formname.$dname\_day.value = 29;
                    255:                 }
                    256:             } else if (day > 29) {
                    257:                 document.$formname.$dname\_day.value = 28;
                    258:             }
                    259:         } else if (day > 30) {
                    260:             document.$formname.$dname\_day.value = 30;
                    261:         }
                    262:     }
1.29      www       263: 
                    264:     function $dname\_opencalendar() {
                    265:        var calwin=window.open(
                    266: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
                    267: document.$formname.$dname\_month.value+"&year="+
                    268: document.$formname.$dname\_year.value,
                    269:              "LONCAPAcal",
                    270:               "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
                    271: 
                    272:     }
1.10      matthew   273: </script>
                    274: ENDJS
1.26      matthew   275:     $result .= "  <nobr><select name=\"$dname\_month\" ".$special.' '.
1.10      matthew   276:         "onChange=\"javascript:$dname\_checkday()\" >\n";
                    277:     my @Months = qw/January February  March     April   May      June 
                    278:                     July    August    September October November December/;
                    279:     # Pad @Months with a bogus value to make indexing easier
                    280:     unshift(@Months,'If you can read this an error occurred');
1.39      www       281:     if ($includeempty) { $result.="<option value=''></option>"; }
1.10      matthew   282:     for(my $m = 1;$m <=$#Months;$m++) {
                    283:         $result .= "      <option value=\"$m\" ";
1.39      www       284:         $result .= "selected " if ($m-1 eq $month);
1.30      www       285:         $result .= "> ".&mt($Months[$m])." </option>\n";
1.10      matthew   286:     }
                    287:     $result .= "  </select>\n";
                    288:     $result .= "  <input type=\"text\" name=\"$dname\_day\" ".
1.26      matthew   289:             "value=\"$mday\" size=\"3\" ".$special.' '.
1.10      matthew   290:             "onChange=\"javascript:$dname\_checkday()\" />\n";
                    291:     $result .= "  <input type=\"year\" name=\"$dname\_year\" ".
1.26      matthew   292:             "value=\"$year\" size=\"5\" ".$special.' '.
1.10      matthew   293:             "onChange=\"javascript:$dname\_checkday()\" />\n";
                    294:     $result .= "&nbsp;&nbsp;";
1.26      matthew   295:     $result .= "  <select name=\"$dname\_hour\" ".$special." >\n";
1.39      www       296:     if ($includeempty) { $result.="<option value=''></option>"; }
1.10      matthew   297:     for (my $h = 0;$h<24;$h++) {
                    298:         $result .= "      <option value=\"$h\" ";
                    299:         $result .= "selected " if ($hour == $h);
                    300:         $result .= "> ";
1.30      www       301: 	my $timest='';
1.10      matthew   302:         if ($h == 0) {
1.30      www       303:             $timest .= "12 am";
1.10      matthew   304:         } elsif($h == 12) {
1.30      www       305:             $timest .= "12 noon";
1.10      matthew   306:         } elsif($h < 12) {
1.30      www       307:             $timest .= "$h am";
1.10      matthew   308:         } else {
1.30      www       309:             $timest .= $h-12 ." pm";
1.10      matthew   310:         }
1.30      www       311: 	$timest=&mt($timest);
                    312:         $result .= $timest." </option>\n";
1.10      matthew   313:     } 
                    314:     $result .= "  </select>\n";
1.26      matthew   315:     $result .= "  <input type=\"text\" name=\"$dname\_minute\" ".$special.' '.
1.10      matthew   316:         "value=\"$min\" size=\"3\" /> m\n";
1.26      matthew   317:     $result .= "  <input type=\"text\" name=\"$dname\_second\" ".$special.' '.
1.10      matthew   318:         "value=\"$sec\" size=\"3\" /> s\n";
1.30      www       319:     $result .= "<a href=\"javascript:$dname\_opencalendar()\">".
                    320:     &mt('Select Date')."</a></nobr>\n<!-- end $dname date setting form -->\n";
1.10      matthew   321:     return $result;
                    322: }
                    323: 
                    324: ##############################################
                    325: ##############################################
                    326: 
1.22      matthew   327: =pod
                    328: 
1.10      matthew   329: =item &get_date_from_form
1.22      matthew   330: 
                    331: get_date_from_form retrieves the date specified in an &date_setter form.
1.10      matthew   332: 
                    333: Inputs:
                    334: 
                    335: =over 4
                    336: 
                    337: =item $dname
                    338: 
                    339: The name passed to &datesetter, which prefixes the form elements.
                    340: 
                    341: =item $defaulttime
                    342: 
                    343: The unix time to use as the default in case of poor inputs.
                    344: 
                    345: =back
                    346: 
                    347: Returns: Unix time represented in the form.
                    348: 
                    349: =cut
                    350: 
                    351: ##############################################
                    352: ##############################################
                    353: sub get_date_from_form {
                    354:     my ($dname) = @_;
                    355:     my ($sec,$min,$hour,$day,$month,$year);
                    356:     #
                    357:     if (defined($ENV{'form.'.$dname.'_second'})) {
                    358:         my $tmpsec = $ENV{'form.'.$dname.'_second'};
                    359:         if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
                    360:             $sec = $tmpsec;
                    361:         }
                    362:     }
                    363:     if (defined($ENV{'form.'.$dname.'_minute'})) {
                    364:         my $tmpmin = $ENV{'form.'.$dname.'_minute'};
                    365:         if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
                    366:             $min = $tmpmin;
                    367:         }
                    368:     }
                    369:     if (defined($ENV{'form.'.$dname.'_hour'})) {
                    370:         my $tmphour = $ENV{'form.'.$dname.'_hour'};
1.33      matthew   371:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
1.10      matthew   372:             $hour = $tmphour;
                    373:         }
                    374:     }
                    375:     if (defined($ENV{'form.'.$dname.'_day'})) {
                    376:         my $tmpday = $ENV{'form.'.$dname.'_day'};
                    377:         if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
                    378:             $day = $tmpday;
                    379:         }
                    380:     }
                    381:     if (defined($ENV{'form.'.$dname.'_month'})) {
                    382:         my $tmpmonth = $ENV{'form.'.$dname.'_month'};
                    383:         if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
                    384:             $month = $tmpmonth - 1;
                    385:         }
                    386:     }
                    387:     if (defined($ENV{'form.'.$dname.'_year'})) {
                    388:         my $tmpyear = $ENV{'form.'.$dname.'_year'};
                    389:         if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
                    390:             $year = $tmpyear - 1900;
                    391:         }
                    392:     }
1.24      www       393:     if (($year<70) || ($year>137)) { return undef; }
1.33      matthew   394:     if (defined($sec) && defined($min)   && defined($hour) &&
                    395:         defined($day) && defined($month) && defined($year) &&
                    396:         eval(&timelocal($sec,$min,$hour,$day,$month,$year))) {
1.10      matthew   397:         return &timelocal($sec,$min,$hour,$day,$month,$year);
                    398:     } else {
                    399:         return undef;
                    400:     }
1.20      matthew   401: }
                    402: 
                    403: ##############################################
                    404: ##############################################
                    405: 
                    406: =pod
                    407: 
                    408: =item &pjump_javascript_definition()
                    409: 
                    410: Returns javascript defining the 'pjump' function, which opens up a
                    411: parameter setting wizard.
                    412: 
                    413: =cut
                    414: 
                    415: ##############################################
                    416: ##############################################
                    417: sub pjump_javascript_definition {
                    418:     my $Str = <<END;
                    419:     function pjump(type,dis,value,marker,ret,call) {
                    420:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
                    421:                  +"&value="+escape(value)+"&marker="+escape(marker)
                    422:                  +"&return="+escape(ret)
                    423:                  +"&call="+escape(call)+"&name="+escape(dis),"LONCAPAparms",
                    424:                  "height=350,width=350,scrollbars=no,menubar=no");
                    425:     }
                    426: END
                    427:     return $Str;
1.10      matthew   428: }
                    429: 
                    430: ##############################################
                    431: ##############################################
1.17      matthew   432: 
                    433: =pod
                    434: 
                    435: =item &javascript_nothing()
                    436: 
                    437: Return an appropriate null for the users browser.  This is used
                    438: as the first arguement for window.open calls when you want a blank
                    439: window that you can then write to.
                    440: 
                    441: =cut
                    442: 
                    443: ##############################################
                    444: ##############################################
                    445: sub javascript_nothing {
                    446:     # mozilla and other browsers work with "''", but IE on mac does not.
                    447:     my $nothing = "''";
                    448:     my $user_browser;
                    449:     my $user_os;
                    450:     $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
                    451:     $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
                    452:     if (! defined($user_browser) || ! defined($user_os)) {
                    453:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    454:                            &Apache::loncommon::decode_user_agent();
                    455:     }
                    456:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                    457:         $nothing = "'javascript:void(0);'";
                    458:     }
                    459:     return $nothing;
                    460: }
                    461: 
1.21      matthew   462: 
1.17      matthew   463: ##############################################
                    464: ##############################################
                    465: 
1.21      matthew   466: =pod
1.17      matthew   467: 
1.21      matthew   468: =item &StatusOptions()
1.10      matthew   469: 
1.21      matthew   470: Returns html for a selection box which allows the user to choose the
                    471: enrollment status of students.  The selection box name is 'Status'.
1.6       stredwic  472: 
1.21      matthew   473: Inputs:
1.6       stredwic  474: 
1.21      matthew   475: $status: the currently selected status.  If undefined the value of
                    476: $ENV{'form.Status'} is taken.  If that is undefined, a value of 'Active'
                    477: is used.
1.6       stredwic  478: 
1.21      matthew   479: $formname: The name of the form.  If defined the onchange attribute of
                    480: the selection box is set to document.$formname.submit().
1.6       stredwic  481: 
1.21      matthew   482: $size: the size (number of lines) of the selection box.
1.6       stredwic  483: 
1.27      matthew   484: $onchange: javascript to use when the value is changed.  Enclosed in 
                    485: double quotes, ""s, not single quotes.
                    486: 
1.21      matthew   487: Returns: a perl string as described.
1.1       stredwic  488: 
1.21      matthew   489: =cut
1.9       stredwic  490: 
1.21      matthew   491: ##############################################
                    492: ##############################################
                    493: sub StatusOptions {
1.27      matthew   494:     my ($status, $formName,$size,$onchange)=@_;
1.21      matthew   495:     $size = 1 if (!defined($size));
                    496:     if (! defined($status)) {
                    497:         $status = 'Active';
                    498:         $status = $ENV{'form.Status'} if (exists($ENV{'form.Status'}));
1.9       stredwic  499:     }
1.1       stredwic  500: 
                    501:     my $OpSel1 = '';
                    502:     my $OpSel2 = '';
                    503:     my $OpSel3 = '';
                    504: 
                    505:     if($status eq 'Any')         { $OpSel3 = ' selected'; }
                    506:     elsif($status eq 'Expired' ) { $OpSel2 = ' selected'; }
                    507:     else                         { $OpSel1 = ' selected'; }
                    508: 
                    509:     my $Str = '';
                    510:     $Str .= '<select name="Status"';
1.27      matthew   511:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
1.1       stredwic  512:         $Str .= ' onchange="document.'.$formName.'.submit()"';
1.27      matthew   513:     }
                    514:     if (defined($onchange)) {
                    515:         $Str .= ' onchange="'.$onchange.'"';
1.1       stredwic  516:     }
1.21      matthew   517:     $Str .= ' size="'.$size.'" ';
1.1       stredwic  518:     $Str .= '>'."\n";
1.21      matthew   519:     $Str .= '<option value="Active" '.$OpSel1.'>'.
1.37      www       520:         &mt('Currently Enrolled').'</option>'."\n";
1.21      matthew   521:     $Str .= '<option value="Expired" '.$OpSel2.'>'.
1.37      www       522:         &mt('Previously Enrolled').'</option>'."\n";
1.21      matthew   523:     $Str .= '<option value="Any" '.$OpSel3.'>'.
1.37      www       524:         &mt('Any Enrollment Status').'</option>'."\n";
1.1       stredwic  525:     $Str .= '</select>'."\n";
1.7       stredwic  526: }
1.12      matthew   527: 
                    528: ########################################################
                    529: ########################################################
1.7       stredwic  530: 
1.23      matthew   531: =pod
                    532: 
                    533: =item Progess Window Handling Routines
                    534: 
                    535: These routines handle the creation, update, increment, and closure of 
                    536: progress windows.  The progress window reports to the user the number
                    537: of items completed and an estimate of the time required to complete the rest.
                    538: 
                    539: =over 4
                    540: 
                    541: 
                    542: =item &Create_PrgWin
                    543: 
                    544: Writes javascript to the client to open a progress window and returns a
                    545: data structure used for bookkeeping.
                    546: 
                    547: Inputs
                    548: 
                    549: =over 4
                    550: 
                    551: =item $r Apache request
                    552: 
                    553: =item $title The title of the progress window
                    554: 
                    555: =item $heading A description (usually 1 line) of the process being initiated.
                    556: 
                    557: =item $number_to_do The total number of items being processed.
                    558: 
                    559: =back
                    560: 
                    561: Returns a hash containing the progress state data structure.
                    562: 
                    563: 
                    564: =item &Update_PrgWin
                    565: 
                    566: Updates the text in the progress indicator.  Does not increment the count.
                    567: See &Increment_PrgWin.
                    568: 
                    569: Inputs:
                    570: 
                    571: =over 4
                    572: 
                    573: =item $r Apache request
                    574: 
                    575: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
                    576: 
                    577: =item $displaystring The string to write to the status indicator
                    578: 
                    579: =back
                    580: 
                    581: Returns: none
                    582: 
                    583: 
                    584: =item Increment_PrgWin
                    585: 
                    586: Increment the count of items completed for the progress window by 1.  
                    587: 
                    588: Inputs:
                    589: 
                    590: =over 4
                    591: 
                    592: =item $r Apache request
                    593: 
                    594: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    595: 
                    596: =item $extraInfo A description of the items being iterated over.  Typically
                    597: 'student'.
                    598: 
                    599: =back
                    600: 
                    601: Returns: none
                    602: 
                    603: 
                    604: =item Close_PrgWin
                    605: 
                    606: Closes the progress window.
                    607: 
                    608: Inputs:
                    609: 
                    610: =over 4 
                    611: 
                    612: =item $r Apache request
                    613: 
                    614: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    615: 
                    616: =back
                    617: 
                    618: Returns: none
                    619: 
                    620: =back
                    621: 
                    622: =cut
                    623: 
                    624: ########################################################
                    625: ########################################################
                    626: 
1.7       stredwic  627: # Create progress
                    628: sub Create_PrgWin {
1.14      albertel  629:     my ($r, $title, $heading, $number_to_do)=@_;
1.45      albertel  630:     #the whole function called through timeout is due to issues
                    631:     #in mozilla Read BUG #2665 if you want to know the whole story
1.7       stredwic  632:     $r->print('<script>'.
1.44      albertel  633:     "var popwin;
                    634:      function openpopwin () {
                    635:      popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
1.14      albertel  636:     "popwin.document.writeln(\'<html><head><title>$title</title></head>".
                    637: 	      "<body bgcolor=\"#88DDFF\">".
1.7       stredwic  638:               "<h4>$heading</h4>".
                    639:               "<form name=popremain>".
1.32      www       640:               '<input type="text" size="55" name="remaining" value="'.
                    641: 	      &mt('Starting').'"></form>'.
1.7       stredwic  642:               "</body></html>\');".
1.44      albertel  643:     "popwin.document.close();}".
                    644:     "\nwindow.setTimeout(openpopwin,0)</script>");
1.7       stredwic  645: 
1.14      albertel  646:     my %prog_state;
1.16      albertel  647:     $prog_state{'done'}=0;
1.23      matthew   648:     $prog_state{'firststart'}=&Time::HiRes::time();
                    649:     $prog_state{'laststart'}=&Time::HiRes::time();
1.16      albertel  650:     $prog_state{'max'}=$number_to_do;
1.14      albertel  651: 
1.7       stredwic  652:     $r->rflush();
1.14      albertel  653:     return %prog_state;
1.7       stredwic  654: }
                    655: 
                    656: # update progress
                    657: sub Update_PrgWin {
1.14      albertel  658:     my ($r,$prog_state,$displayString)=@_;
1.7       stredwic  659:     $r->print('<script>popwin.document.popremain.remaining.value="'.
                    660:               $displayString.'";</script>');
1.23      matthew   661:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.14      albertel  662:     $r->rflush();
                    663: }
                    664: 
                    665: # increment progress state
                    666: sub Increment_PrgWin {
                    667:     my ($r,$prog_state,$extraInfo)=@_;
1.16      albertel  668:     $$prog_state{'done'}++;
1.23      matthew   669:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
                    670:         $$prog_state{'done'} *
1.16      albertel  671: 	($$prog_state{'max'}-$$prog_state{'done'});
                    672:     $time_est = int($time_est);
                    673:     if (int ($time_est/60) > 0) {
                    674: 	my $min = int($time_est/60);
                    675: 	my $sec = $time_est % 60;
1.31      www       676: 	$time_est = $min.' '.&mt('minutes');
1.25      matthew   677:         if ($min < 10)  {
                    678:             if ($sec > 1) {
1.31      www       679:                 $time_est.= ', '.$sec.' '.&mt('seconds');
1.25      matthew   680:             } elsif ($sec > 0) {
1.31      www       681:                 $time_est.= ', '.$sec.' '.&mt('second');
1.25      matthew   682:             }
                    683:         }
1.16      albertel  684:     } else {
1.31      www       685: 	$time_est .= ' '.&mt('seconds');
1.16      albertel  686:     }
1.23      matthew   687:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
                    688:     if ($lasttime > 9) {
                    689:         $lasttime = int($lasttime);
                    690:     } elsif ($lasttime < 0.01) {
                    691:         $lasttime = 0;
                    692:     } else {
                    693:         $lasttime = sprintf("%3.2f",$lasttime);
                    694:     }
1.19      matthew   695:     if ($lasttime == 1) {
1.32      www       696:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
1.19      matthew   697:     } else {
1.32      www       698:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
1.28      matthew   699:     }
                    700:     #
                    701:     my $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
                    702:     my $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
                    703:     if (! defined($user_browser) || ! defined($user_os)) {
                    704:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    705:                            &Apache::loncommon::decode_user_agent();
                    706:     }
                    707:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                    708:         $lasttime = '';
1.19      matthew   709:     }
1.14      albertel  710:     $r->print('<script>popwin.document.popremain.remaining.value="'.
1.16      albertel  711: 	      $$prog_state{'done'}.'/'.$$prog_state{'max'}.
1.31      www       712: 	      ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
1.23      matthew   713:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.7       stredwic  714:     $r->rflush();
                    715: }
                    716: 
                    717: # close Progress Line
                    718: sub Close_PrgWin {
1.14      albertel  719:     my ($r,$prog_state)=@_;
1.7       stredwic  720:     $r->print('<script>popwin.close()</script>'."\n");
1.14      albertel  721:     undef(%$prog_state);
1.7       stredwic  722:     $r->rflush(); 
1.1       stredwic  723: }
1.34      www       724: 
                    725: 
                    726: # ------------------------------------------------------- Puts directory header
                    727: 
                    728: sub crumbs {
1.41      www       729:     my ($uri,$target,$prefix,$form)=@_;
1.34      www       730:     my $output='<br /><tt><b><font size="+2">'.$prefix.'/';
1.35      www       731:     if ($ENV{'user.adv'}) {
1.43      www       732: 	my $path=$prefix.'/';
1.35      www       733: 	foreach (split('/',$uri)) {
                    734: 	    unless ($_) { next; }
1.43      www       735: 	    $path.=$_;
                    736: 	    unless ($path eq $uri) { $path.='/'; }
1.41      www       737: 	    my $linkpath=$path;
                    738: 	    if ($form) {
1.43      www       739: 		$linkpath="javascript:$form.action='$path';$form.submit();";
1.41      www       740: 	    }
                    741: 	    $output.='<a href="'.$linkpath.'"'.($target?' target="'.$target.'"':'').'>'.$_.'</a>/';
1.35      www       742: 	}
                    743:     } else {
                    744: 	$output.=$uri;
1.34      www       745:     }
1.36      www       746:     unless ($uri=~/\/$/) { $output=~s/\/$//; }
1.34      www       747:     return $output.'</font></b></tt><br />';
                    748: }
                    749: 
1.1       stredwic  750: 
                    751: 1;
1.23      matthew   752: 
1.1       stredwic  753: __END__

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