File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.28: download - view: text, annotated - select for diffs
Fri Jul 25 19:56:37 2003 UTC (20 years, 10 months ago) by matthew
Branches: MAIN
CVS tags: version_1_0_3, version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, HEAD
Fix Bug 1382 - progress window scrolls when viewed on Mac/IE.
Do not output the last item time if the user is burdened
with both a Mac and IE.  Worth considering for 1.0, I guess.

    1: # The LearningOnline Network with CAPA
    2: # a pile of common html routines
    3: #
    4: # $Id: lonhtmlcommon.pm,v 1.28 2003/07/25 19:56:37 matthew Exp $
    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: #
   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: ######################################################################
   55: 
   56: package Apache::lonhtmlcommon;
   57: 
   58: use Time::Local;
   59: use strict;
   60: 
   61: 
   62: ##############################################
   63: ##############################################
   64: 
   65: =pod
   66: 
   67: =item textbox
   68: 
   69: =cut
   70: 
   71: ##############################################
   72: ##############################################
   73: sub textbox {
   74:     my ($name,$value,$size,$special) = @_;
   75:     $size = 40 if (! defined($size));
   76:     my $Str = '<input type="text" name="'.$name.'" size="'.$size.'" '.
   77:         'value="'.$value.'" '.$special.' />';
   78:     return $Str;
   79: }
   80: 
   81: ##############################################
   82: ##############################################
   83: 
   84: =pod
   85: 
   86: =item checkbox
   87: 
   88: =cut
   89: 
   90: ##############################################
   91: ##############################################
   92: sub checkbox {
   93:     my ($name) = @_;
   94:     my $Str = '<input type="checkbox" name="'.$name.'" />';
   95:     return $Str;
   96: }
   97: 
   98: 
   99: 
  100: ##############################################
  101: ##############################################
  102: 
  103: =pod
  104: 
  105: =item &date_setter
  106: 
  107: &date_setter returns html and javascript for a compact date-setting form.
  108: To retrieve values from it, use &get_date_from_form().
  109: 
  110: Inputs
  111: 
  112: =over 4
  113: 
  114: =item $dname 
  115: 
  116: The name to prepend to the form elements.  
  117: The form elements defined will be dname_year, dname_month, dname_day,
  118: dname_hour, dname_min, and dname_sec.
  119: 
  120: =item $currentvalue
  121: 
  122: The current setting for this time parameter.  A unix format time
  123: (time in seconds since the beginning of Jan 1st, 1970, GMT.  
  124: An undefined value is taken to indicate the value is the current time.
  125: Also, to be explicit, a value of 'now' also indicates the current time.
  126: 
  127: =item $special
  128: 
  129: Additional html/javascript to be associated with each element in
  130: the date_setter.  See lonparmset for example usage.
  131: 
  132: =back
  133: 
  134: Bugs
  135: 
  136: The method used to restrict user input will fail in the year 2400.
  137: 
  138: =cut
  139: 
  140: ##############################################
  141: ##############################################
  142: sub date_setter {
  143:     my ($formname,$dname,$currentvalue,$special) = @_;
  144:     if (! defined($currentvalue) || $currentvalue eq 'now') {
  145:         $currentvalue = time;
  146:     }
  147:     # other potentially useful values:     wkday,yrday,is_daylight_savings
  148:     my ($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
  149:         localtime($currentvalue);
  150:     $year += 1900;
  151:     my $result = "\n<!-- $dname date setting form -->\n";
  152:     $result .= <<ENDJS;
  153: <script language="Javascript">
  154:     function $dname\_checkday() {
  155:         var day   = document.$formname.$dname\_day.value;
  156:         var month = document.$formname.$dname\_month.value;
  157:         var year  = document.$formname.$dname\_year.value;
  158:         var valid = true;
  159:         if (day < 1) {
  160:             document.$formname.$dname\_day.value = 1;
  161:         } 
  162:         if (day > 31) {
  163:             document.$formname.$dname\_day.value = 31;
  164:         }
  165:         if ((month == 1)  || (month == 3)  || (month == 5)  ||
  166:             (month == 7)  || (month == 8)  || (month == 10) ||
  167:             (month == 12)) {
  168:             if (day > 31) {
  169:                 document.$formname.$dname\_day.value = 31;
  170:                 day = 31;
  171:             }
  172:         } else if (month == 2 ) {
  173:             if ((year % 4 == 0) && (year % 100 != 0)) {
  174:                 if (day > 29) {
  175:                     document.$formname.$dname\_day.value = 29;
  176:                 }
  177:             } else if (day > 29) {
  178:                 document.$formname.$dname\_day.value = 28;
  179:             }
  180:         } else if (day > 30) {
  181:             document.$formname.$dname\_day.value = 30;
  182:         }
  183:     }
  184: </script>
  185: ENDJS
  186:     $result .= "  <nobr><select name=\"$dname\_month\" ".$special.' '.
  187:         "onChange=\"javascript:$dname\_checkday()\" >\n";
  188:     my @Months = qw/January February  March     April   May      June 
  189:                     July    August    September October November December/;
  190:     # Pad @Months with a bogus value to make indexing easier
  191:     unshift(@Months,'If you can read this an error occurred');
  192:     for(my $m = 1;$m <=$#Months;$m++) {
  193:         $result .= "      <option value=\"$m\" ";
  194:         $result .= "selected " if ($m-1 == $month);
  195:         $result .= "> $Months[$m] </option>\n";
  196:     }
  197:     $result .= "  </select>\n";
  198:     $result .= "  <input type=\"text\" name=\"$dname\_day\" ".
  199:             "value=\"$mday\" size=\"3\" ".$special.' '.
  200:             "onChange=\"javascript:$dname\_checkday()\" />\n";
  201:     $result .= "  <input type=\"year\" name=\"$dname\_year\" ".
  202:             "value=\"$year\" size=\"5\" ".$special.' '.
  203:             "onChange=\"javascript:$dname\_checkday()\" />\n";
  204:     $result .= "&nbsp;&nbsp;";
  205:     $result .= "  <select name=\"$dname\_hour\" ".$special." >\n";
  206:     for (my $h = 0;$h<24;$h++) {
  207:         $result .= "      <option value=\"$h\" ";
  208:         $result .= "selected " if ($hour == $h);
  209:         $result .= "> ";
  210:         if ($h == 0) {
  211:             $result .= "12 am";
  212:         } elsif($h == 12) {
  213:             $result .= "12 noon";
  214:         } elsif($h < 12) {
  215:             $result .= "$h am";
  216:         } else {
  217:             $result .= $h-12 ." pm";
  218:         }
  219:         $result .= " </option>\n";
  220:     } 
  221:     $result .= "  </select>\n";
  222:     $result .= "  <input type=\"text\" name=\"$dname\_minute\" ".$special.' '.
  223:         "value=\"$min\" size=\"3\" /> m\n";
  224:     $result .= "  <input type=\"text\" name=\"$dname\_second\" ".$special.' '.
  225:         "value=\"$sec\" size=\"3\" /> s\n";
  226:     $result .= "</nobr>\n<!-- end $dname date setting form -->\n";
  227:     return $result;
  228: }
  229: 
  230: ##############################################
  231: ##############################################
  232: 
  233: =pod
  234: 
  235: =item &get_date_from_form
  236: 
  237: get_date_from_form retrieves the date specified in an &date_setter form.
  238: 
  239: Inputs:
  240: 
  241: =over 4
  242: 
  243: =item $dname
  244: 
  245: The name passed to &datesetter, which prefixes the form elements.
  246: 
  247: =item $defaulttime
  248: 
  249: The unix time to use as the default in case of poor inputs.
  250: 
  251: =back
  252: 
  253: Returns: Unix time represented in the form.
  254: 
  255: =cut
  256: 
  257: ##############################################
  258: ##############################################
  259: sub get_date_from_form {
  260:     my ($dname) = @_;
  261:     my ($sec,$min,$hour,$day,$month,$year);
  262:     #
  263:     if (defined($ENV{'form.'.$dname.'_second'})) {
  264:         my $tmpsec = $ENV{'form.'.$dname.'_second'};
  265:         if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
  266:             $sec = $tmpsec;
  267:         }
  268:     }
  269:     if (defined($ENV{'form.'.$dname.'_minute'})) {
  270:         my $tmpmin = $ENV{'form.'.$dname.'_minute'};
  271:         if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
  272:             $min = $tmpmin;
  273:         }
  274:     }
  275:     if (defined($ENV{'form.'.$dname.'_hour'})) {
  276:         my $tmphour = $ENV{'form.'.$dname.'_hour'};
  277:         if (($tmphour =~ /^\d+$/) && ($tmphour > 0) && ($tmphour < 32)) {
  278:             $hour = $tmphour;
  279:         }
  280:     }
  281:     if (defined($ENV{'form.'.$dname.'_day'})) {
  282:         my $tmpday = $ENV{'form.'.$dname.'_day'};
  283:         if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
  284:             $day = $tmpday;
  285:         }
  286:     }
  287:     if (defined($ENV{'form.'.$dname.'_month'})) {
  288:         my $tmpmonth = $ENV{'form.'.$dname.'_month'};
  289:         if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
  290:             $month = $tmpmonth - 1;
  291:         }
  292:     }
  293:     if (defined($ENV{'form.'.$dname.'_year'})) {
  294:         my $tmpyear = $ENV{'form.'.$dname.'_year'};
  295:         if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
  296:             $year = $tmpyear - 1900;
  297:         }
  298:     }
  299:     if (($year<70) || ($year>137)) { return undef; }
  300:     if (eval(&timelocal($sec,$min,$hour,$day,$month,$year))) {
  301:         return &timelocal($sec,$min,$hour,$day,$month,$year);
  302:     } else {
  303:         return undef;
  304:     }
  305: }
  306: 
  307: ##############################################
  308: ##############################################
  309: 
  310: =pod
  311: 
  312: =item &pjump_javascript_definition()
  313: 
  314: Returns javascript defining the 'pjump' function, which opens up a
  315: parameter setting wizard.
  316: 
  317: =cut
  318: 
  319: ##############################################
  320: ##############################################
  321: sub pjump_javascript_definition {
  322:     my $Str = <<END;
  323:     function pjump(type,dis,value,marker,ret,call) {
  324:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
  325:                  +"&value="+escape(value)+"&marker="+escape(marker)
  326:                  +"&return="+escape(ret)
  327:                  +"&call="+escape(call)+"&name="+escape(dis),"LONCAPAparms",
  328:                  "height=350,width=350,scrollbars=no,menubar=no");
  329:     }
  330: END
  331:     return $Str;
  332: }
  333: 
  334: ##############################################
  335: ##############################################
  336: 
  337: =pod
  338: 
  339: =item &javascript_nothing()
  340: 
  341: Return an appropriate null for the users browser.  This is used
  342: as the first arguement for window.open calls when you want a blank
  343: window that you can then write to.
  344: 
  345: =cut
  346: 
  347: ##############################################
  348: ##############################################
  349: sub javascript_nothing {
  350:     # mozilla and other browsers work with "''", but IE on mac does not.
  351:     my $nothing = "''";
  352:     my $user_browser;
  353:     my $user_os;
  354:     $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
  355:     $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
  356:     if (! defined($user_browser) || ! defined($user_os)) {
  357:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  358:                            &Apache::loncommon::decode_user_agent();
  359:     }
  360:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
  361:         $nothing = "'javascript:void(0);'";
  362:     }
  363:     return $nothing;
  364: }
  365: 
  366: 
  367: ##############################################
  368: ##############################################
  369: 
  370: =pod
  371: 
  372: =item &StatusOptions()
  373: 
  374: Returns html for a selection box which allows the user to choose the
  375: enrollment status of students.  The selection box name is 'Status'.
  376: 
  377: Inputs:
  378: 
  379: $status: the currently selected status.  If undefined the value of
  380: $ENV{'form.Status'} is taken.  If that is undefined, a value of 'Active'
  381: is used.
  382: 
  383: $formname: The name of the form.  If defined the onchange attribute of
  384: the selection box is set to document.$formname.submit().
  385: 
  386: $size: the size (number of lines) of the selection box.
  387: 
  388: $onchange: javascript to use when the value is changed.  Enclosed in 
  389: double quotes, ""s, not single quotes.
  390: 
  391: Returns: a perl string as described.
  392: 
  393: =cut
  394: 
  395: ##############################################
  396: ##############################################
  397: sub StatusOptions {
  398:     my ($status, $formName,$size,$onchange)=@_;
  399:     $size = 1 if (!defined($size));
  400:     if (! defined($status)) {
  401:         $status = 'Active';
  402:         $status = $ENV{'form.Status'} if (exists($ENV{'form.Status'}));
  403:     }
  404: 
  405:     my $OpSel1 = '';
  406:     my $OpSel2 = '';
  407:     my $OpSel3 = '';
  408: 
  409:     if($status eq 'Any')         { $OpSel3 = ' selected'; }
  410:     elsif($status eq 'Expired' ) { $OpSel2 = ' selected'; }
  411:     else                         { $OpSel1 = ' selected'; }
  412: 
  413:     my $Str = '';
  414:     $Str .= '<select name="Status"';
  415:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
  416:         $Str .= ' onchange="document.'.$formName.'.submit()"';
  417:     }
  418:     if (defined($onchange)) {
  419:         $Str .= ' onchange="'.$onchange.'"';
  420:     }
  421:     $Str .= ' size="'.$size.'" ';
  422:     $Str .= '>'."\n";
  423:     $Str .= '<option value="Active" '.$OpSel1.'>'.
  424:         'Currently Enrolled</option>'."\n";
  425:     $Str .= '<option value="Expired" '.$OpSel2.'>'.
  426:         'Previously Enrolled</option>'."\n";
  427:     $Str .= '<option value="Any" '.$OpSel3.'>'.
  428:         'Any Enrollment Status</option>'."\n";
  429:     $Str .= '</select>'."\n";
  430: }
  431: 
  432: 
  433: ########################################################
  434: ########################################################
  435: 
  436: =pod
  437: 
  438: =item &MultipleSectionSelect()
  439: 
  440: Inputs: 
  441: 
  442: =over 4
  443: 
  444: =item $sections A references to an array containing the names of all the
  445: sections used in a class.
  446: 
  447: =item $selectedSections A reference to an array containing the names of the
  448: currently selected sections.
  449: 
  450: =back 
  451: 
  452: Returns: a string containing HTML for a multiple select box for
  453: selecting sections of a course.  
  454: 
  455: The form element name is 'Section'.  @$sections is sorted prior to output.
  456: 
  457: =cut
  458: 
  459: ########################################################
  460: ########################################################
  461: sub MultipleSectionSelect {
  462:     my ($sections,$selectedSections)=@_;
  463: 
  464:     my $Str = '';
  465:     $Str .= '<select name="Section" multiple="true" size="4">'."\n";
  466: 
  467:     foreach (sort @$sections) {
  468:         $Str .= '<option';
  469:         foreach my $selected (@$selectedSections) {
  470:             if($_ eq $selected) {
  471:                 $Str .= ' selected=""';
  472:             }
  473:         }
  474:         $Str .= '>'.$_.'</option>'."\n";
  475:     }
  476:     $Str .= '</select>'."\n";
  477:     
  478:     return $Str;
  479: }
  480: 
  481: ########################################################
  482: ########################################################
  483: 
  484: =pod
  485: 
  486: =item &Title()
  487: 
  488: Inputs: $pageName a string containing the name of the page to be sent
  489: to &Apache::loncommon::bodytag.
  490: 
  491: Returns: string containing being <html> and complete <head> and <title>
  492: as well as a <script> to focus the current window and change its width
  493: and height to 500.  Why?  I do not know.  If you find out, please update
  494: this documentation.
  495: 
  496: =cut
  497: 
  498: ########################################################
  499: ########################################################
  500: sub Title {
  501:     my ($pageName)=@_;
  502: 
  503:     my $Str = '';
  504: 
  505:     $Str .= '<html><head><title>'.$pageName.'</title></head>'."\n";
  506:     $Str .= &Apache::loncommon::bodytag($pageName)."\n";
  507:     $Str .= '<script>window.focus(); window.width=500;window.height=500;';
  508:     $Str .= '</script>'."\n";
  509: 
  510:     return $Str;
  511: }
  512: 
  513: ########################################################
  514: ########################################################
  515: 
  516: =pod
  517: 
  518: =item &CreateHeadings()
  519: 
  520: This function generates the column headings for the chart.
  521: 
  522: =over 4
  523: 
  524: Inputs: $CacheData, $keyID, $headings, $spacePadding
  525: 
  526: $CacheData: pointer to a hash tied to the cached data database
  527: 
  528: $keyID: a pointer to an array containing the names of the data 
  529: held in a column and is used as part of a key into $CacheData
  530: 
  531: $headings: The names of the headings for the student information
  532: 
  533: $spacePadding: The spaces to go between columns
  534: 
  535: Output: $Str
  536: 
  537: $Str: A formatted string of the table column headings.
  538: 
  539: =back
  540: 
  541: =cut
  542: 
  543: ########################################################
  544: ########################################################
  545: sub CreateHeadings {
  546:     my ($data,$keyID,$headings,$displayString,$format)=@_;
  547:     my $Str='';
  548:     my $formatting = '';
  549: 
  550:     for(my $index=0; $index<(scalar @$headings); $index++) {
  551:  	my $currentHeading=$headings->[$index];
  552:         if($format eq 'preformatted') {
  553:             my @dataLength=split(//,$currentHeading);
  554:             my $length=scalar @dataLength;
  555:             $formatting = (' 'x
  556:                       ($data->{$keyID->[$index].':columnWidth'}-$length));
  557:         }
  558:         my $linkdata=$keyID->[$index];
  559: 
  560:         my $tempString = $displayString;
  561:         $tempString =~ s/LINKDATA/$linkdata/;
  562:         $tempString =~ s/DISPLAYDATA/$currentHeading/;
  563:         $tempString =~ s/FORMATTING/$formatting/;
  564: 
  565:         $Str .= $tempString;
  566:     }
  567: 
  568:     return $Str;
  569: }
  570: 
  571: ########################################################
  572: ########################################################
  573: 
  574: =pod
  575: 
  576: =item &FormatStudentInformation()
  577: 
  578: This function produces a formatted string of the student\'s information:
  579: username, domain, section, full name, and PID.
  580: 
  581: =over 4
  582: 
  583: Input: $cache, $name, $keyID, $spacePadding
  584: 
  585: $cache: This is a pointer to a hash that is tied to the cached data
  586: 
  587: $name:  The name and domain of the current student in name:domain format
  588: 
  589: $keyID: A pointer to an array holding the names used to
  590: 
  591: remove data from the hash.  They represent the name of the data to be removed.
  592: 
  593: $spacePadding: Extra spaces that represent the space between columns
  594: 
  595: Output: $Str
  596: 
  597: $Str: Formatted string.
  598: 
  599: =back
  600: 
  601: =cut
  602: 
  603: ########################################################
  604: ########################################################
  605: sub FormatStudentInformation {
  606:     my ($data,$name,$keyID,$displayString,$format)=@_;
  607:     my $Str='';
  608:     my $currentColumn;
  609: 
  610:     for(my $index=0; $index<(scalar @$keyID); $index++) {
  611:         $currentColumn=$data->{$name.':'.$keyID->[$index]};
  612: 
  613:         if($format eq 'preformatted') {
  614:             my @dataLength=split(//,$currentColumn);
  615:             my $length=scalar @dataLength;
  616:             $currentColumn.= (' 'x
  617:                      ($data->{$keyID->[$index].':columnWidth'}-$length));
  618:         }
  619: 
  620:         my $tempString = $displayString;
  621:         $tempString =~ s/DISPLAYDATA/$currentColumn/;
  622: 
  623:         $Str .= $tempString;
  624:     }
  625: 
  626:     return $Str;
  627: }
  628: 
  629: ########################################################
  630: ########################################################
  631: 
  632: =pod
  633: 
  634: =item Progess Window Handling Routines
  635: 
  636: These routines handle the creation, update, increment, and closure of 
  637: progress windows.  The progress window reports to the user the number
  638: of items completed and an estimate of the time required to complete the rest.
  639: 
  640: =over 4
  641: 
  642: 
  643: =item &Create_PrgWin
  644: 
  645: Writes javascript to the client to open a progress window and returns a
  646: data structure used for bookkeeping.
  647: 
  648: Inputs
  649: 
  650: =over 4
  651: 
  652: =item $r Apache request
  653: 
  654: =item $title The title of the progress window
  655: 
  656: =item $heading A description (usually 1 line) of the process being initiated.
  657: 
  658: =item $number_to_do The total number of items being processed.
  659: 
  660: =back
  661: 
  662: Returns a hash containing the progress state data structure.
  663: 
  664: 
  665: =item &Update_PrgWin
  666: 
  667: Updates the text in the progress indicator.  Does not increment the count.
  668: See &Increment_PrgWin.
  669: 
  670: Inputs:
  671: 
  672: =over 4
  673: 
  674: =item $r Apache request
  675: 
  676: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
  677: 
  678: =item $displaystring The string to write to the status indicator
  679: 
  680: =back
  681: 
  682: Returns: none
  683: 
  684: 
  685: =item Increment_PrgWin
  686: 
  687: Increment the count of items completed for the progress window by 1.  
  688: 
  689: Inputs:
  690: 
  691: =over 4
  692: 
  693: =item $r Apache request
  694: 
  695: =item $prog_state Pointer to the data structure returned by Create_PrgWin
  696: 
  697: =item $extraInfo A description of the items being iterated over.  Typically
  698: 'student'.
  699: 
  700: =back
  701: 
  702: Returns: none
  703: 
  704: 
  705: =item Close_PrgWin
  706: 
  707: Closes the progress window.
  708: 
  709: Inputs:
  710: 
  711: =over 4 
  712: 
  713: =item $r Apache request
  714: 
  715: =item $prog_state Pointer to the data structure returned by Create_PrgWin
  716: 
  717: =back
  718: 
  719: Returns: none
  720: 
  721: =back
  722: 
  723: =cut
  724: 
  725: ########################################################
  726: ########################################################
  727: 
  728: # Create progress
  729: sub Create_PrgWin {
  730:     my ($r, $title, $heading, $number_to_do)=@_;
  731:     $r->print('<script>'.
  732:     "popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
  733:     "popwin.document.writeln(\'<html><head><title>$title</title></head>".
  734: 	      "<body bgcolor=\"#88DDFF\">".
  735:               "<h4>$heading</h4>".
  736:               "<form name=popremain>".
  737:               '<input type="text" size="55" name="remaining" value="Starting"></form>'.
  738:               "</body></html>\');".
  739:     "popwin.document.close();".
  740:     "</script>");
  741: 
  742:     my %prog_state;
  743:     $prog_state{'done'}=0;
  744:     $prog_state{'firststart'}=&Time::HiRes::time();
  745:     $prog_state{'laststart'}=&Time::HiRes::time();
  746:     $prog_state{'max'}=$number_to_do;
  747: 
  748:     $r->rflush();
  749:     return %prog_state;
  750: }
  751: 
  752: # update progress
  753: sub Update_PrgWin {
  754:     my ($r,$prog_state,$displayString)=@_;
  755:     $r->print('<script>popwin.document.popremain.remaining.value="'.
  756:               $displayString.'";</script>');
  757:     $$prog_state{'laststart'}=&Time::HiRes::time();
  758:     $r->rflush();
  759: }
  760: 
  761: # increment progress state
  762: sub Increment_PrgWin {
  763:     my ($r,$prog_state,$extraInfo)=@_;
  764:     $$prog_state{'done'}++;
  765:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
  766:         $$prog_state{'done'} *
  767: 	($$prog_state{'max'}-$$prog_state{'done'});
  768:     $time_est = int($time_est);
  769:     if (int ($time_est/60) > 0) {
  770: 	my $min = int($time_est/60);
  771: 	my $sec = $time_est % 60;
  772: 	$time_est = $min.' minutes';
  773:         if ($min < 10)  {
  774:             if ($sec > 1) {
  775:                 $time_est.= ', '.$sec.' seconds';
  776:             } elsif ($sec > 0) {
  777:                 $time_est.= ', '.$sec.' second';
  778:             }
  779:         }
  780:     } else {
  781: 	$time_est .= ' seconds';
  782:     }
  783:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
  784:     if ($lasttime > 9) {
  785:         $lasttime = int($lasttime);
  786:     } elsif ($lasttime < 0.01) {
  787:         $lasttime = 0;
  788:     } else {
  789:         $lasttime = sprintf("%3.2f",$lasttime);
  790:     }
  791:     if ($lasttime == 1) {
  792:         $lasttime = '('.$lasttime.' second for '.$extraInfo.')';
  793:     } else {
  794:         $lasttime = '('.$lasttime.' seconds for '.$extraInfo.')';
  795:     }
  796:     #
  797:     my $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
  798:     my $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
  799:     if (! defined($user_browser) || ! defined($user_os)) {
  800:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  801:                            &Apache::loncommon::decode_user_agent();
  802:     }
  803:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
  804:         $lasttime = '';
  805:     }
  806:     $r->print('<script>popwin.document.popremain.remaining.value="'.
  807: 	      $$prog_state{'done'}.'/'.$$prog_state{'max'}.
  808: 	      ': '.$time_est.' remaining '.$lasttime.'";'.'</script>');
  809:     $$prog_state{'laststart'}=&Time::HiRes::time();
  810:     $r->rflush();
  811: }
  812: 
  813: # close Progress Line
  814: sub Close_PrgWin {
  815:     my ($r,$prog_state)=@_;
  816:     $r->print('<script>popwin.close()</script>'."\n");
  817:     undef(%$prog_state);
  818:     $r->rflush(); 
  819: }
  820: 
  821: 1;
  822: 
  823: __END__

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