File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.29: download - view: text, annotated - select for diffs
Wed Sep 3 01:53:46 2003 UTC (20 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Use calendar function to more intuitively pick dates.

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

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