File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.38: download - view: text, annotated - select for diffs
Thu Jan 1 20:13:17 2004 UTC (20 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Cleanup - saving my work

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

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