File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.31: download - view: text, annotated - select for diffs
Tue Oct 14 18:36:54 2003 UTC (20 years, 7 months ago) by www
Branches: MAIN
CVS tags: HEAD
Internationalize/localize

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

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