File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.51: download - view: text, annotated - select for diffs
Mon Feb 16 23:27:03 2004 UTC (20 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- add a width argument for the progress window
- support the possibility of multiple inline progress windows

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

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