File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.40: download - view: text, annotated - select for diffs
Thu Jan 15 20:22:47 2004 UTC (20 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Even better handling of bombs.

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

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