Annotation of loncom/interface/lonhtmlcommon.pm, revision 1.48

1.2       www         1: # The LearningOnline Network with CAPA
                      2: # a pile of common html routines
                      3: #
1.47      sakharuk    4: # $Id: lonhtmlcommon.pm,v 1.46 2004/02/12 22:23:30 matthew Exp $
1.2       www         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: #
1.10      matthew    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: ######################################################################
1.2       www        55: 
1.1       stredwic   56: package Apache::lonhtmlcommon;
                     57: 
1.10      matthew    58: use Time::Local;
1.47      sakharuk   59: use Time::HiRes;
1.30      www        60: use Apache::lonlocal;
1.1       stredwic   61: use strict;
                     62: 
1.40      www        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: }
1.26      matthew    90: 
                     91: ##############################################
                     92: ##############################################
                     93: 
1.41      www        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: 
1.26      matthew   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 {
1.38      www       167:     my ($name,$value) = @_;
                    168:     my $Str = '<input type="checkbox" name="'.$name.'"'.
                    169: 	($value?' checked="1"':'').' />';
1.26      matthew   170:     return $Str;
                    171: }
                    172: 
1.10      matthew   173: ##############################################
                    174: ##############################################
                    175: 
                    176: =pod
                    177: 
                    178: =item &date_setter
                    179: 
1.22      matthew   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: 
1.10      matthew   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: 
1.26      matthew   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: 
1.22      matthew   205: =back
                    206: 
                    207: Bugs
                    208: 
                    209: The method used to restrict user input will fail in the year 2400.
                    210: 
1.10      matthew   211: =cut
                    212: 
                    213: ##############################################
                    214: ##############################################
                    215: sub date_setter {
1.39      www       216:     my ($formname,$dname,$currentvalue,$special,$includeempty) = @_;
1.10      matthew   217:     if (! defined($currentvalue) || $currentvalue eq 'now') {
1.39      www       218: 	unless ($includeempty) {
                    219: 	    $currentvalue = time;
                    220: 	} else {
                    221: 	    $currentvalue = 0;
                    222: 	}
1.10      matthew   223:     }
                    224:     # other potentially useful values:     wkday,yrday,is_daylight_savings
1.39      www       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:     }
1.10      matthew   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:     }
1.29      www       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:     }
1.10      matthew   274: </script>
                    275: ENDJS
1.26      matthew   276:     $result .= "  <nobr><select name=\"$dname\_month\" ".$special.' '.
1.10      matthew   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');
1.39      www       282:     if ($includeempty) { $result.="<option value=''></option>"; }
1.10      matthew   283:     for(my $m = 1;$m <=$#Months;$m++) {
                    284:         $result .= "      <option value=\"$m\" ";
1.39      www       285:         $result .= "selected " if ($m-1 eq $month);
1.30      www       286:         $result .= "> ".&mt($Months[$m])." </option>\n";
1.10      matthew   287:     }
                    288:     $result .= "  </select>\n";
                    289:     $result .= "  <input type=\"text\" name=\"$dname\_day\" ".
1.26      matthew   290:             "value=\"$mday\" size=\"3\" ".$special.' '.
1.10      matthew   291:             "onChange=\"javascript:$dname\_checkday()\" />\n";
                    292:     $result .= "  <input type=\"year\" name=\"$dname\_year\" ".
1.26      matthew   293:             "value=\"$year\" size=\"5\" ".$special.' '.
1.10      matthew   294:             "onChange=\"javascript:$dname\_checkday()\" />\n";
                    295:     $result .= "&nbsp;&nbsp;";
1.26      matthew   296:     $result .= "  <select name=\"$dname\_hour\" ".$special." >\n";
1.39      www       297:     if ($includeempty) { $result.="<option value=''></option>"; }
1.10      matthew   298:     for (my $h = 0;$h<24;$h++) {
                    299:         $result .= "      <option value=\"$h\" ";
                    300:         $result .= "selected " if ($hour == $h);
                    301:         $result .= "> ";
1.30      www       302: 	my $timest='';
1.10      matthew   303:         if ($h == 0) {
1.30      www       304:             $timest .= "12 am";
1.10      matthew   305:         } elsif($h == 12) {
1.30      www       306:             $timest .= "12 noon";
1.10      matthew   307:         } elsif($h < 12) {
1.30      www       308:             $timest .= "$h am";
1.10      matthew   309:         } else {
1.30      www       310:             $timest .= $h-12 ." pm";
1.10      matthew   311:         }
1.30      www       312: 	$timest=&mt($timest);
                    313:         $result .= $timest." </option>\n";
1.10      matthew   314:     } 
                    315:     $result .= "  </select>\n";
1.26      matthew   316:     $result .= "  <input type=\"text\" name=\"$dname\_minute\" ".$special.' '.
1.10      matthew   317:         "value=\"$min\" size=\"3\" /> m\n";
1.26      matthew   318:     $result .= "  <input type=\"text\" name=\"$dname\_second\" ".$special.' '.
1.10      matthew   319:         "value=\"$sec\" size=\"3\" /> s\n";
1.30      www       320:     $result .= "<a href=\"javascript:$dname\_opencalendar()\">".
                    321:     &mt('Select Date')."</a></nobr>\n<!-- end $dname date setting form -->\n";
1.10      matthew   322:     return $result;
                    323: }
                    324: 
                    325: ##############################################
                    326: ##############################################
                    327: 
1.22      matthew   328: =pod
                    329: 
1.10      matthew   330: =item &get_date_from_form
1.22      matthew   331: 
                    332: get_date_from_form retrieves the date specified in an &date_setter form.
1.10      matthew   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'};
1.33      matthew   372:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
1.10      matthew   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:     }
1.24      www       394:     if (($year<70) || ($year>137)) { return undef; }
1.33      matthew   395:     if (defined($sec) && defined($min)   && defined($hour) &&
                    396:         defined($day) && defined($month) && defined($year) &&
                    397:         eval(&timelocal($sec,$min,$hour,$day,$month,$year))) {
1.10      matthew   398:         return &timelocal($sec,$min,$hour,$day,$month,$year);
                    399:     } else {
                    400:         return undef;
                    401:     }
1.20      matthew   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;
1.10      matthew   429: }
                    430: 
                    431: ##############################################
                    432: ##############################################
1.17      matthew   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: 
1.21      matthew   463: 
1.17      matthew   464: ##############################################
                    465: ##############################################
                    466: 
1.21      matthew   467: =pod
1.17      matthew   468: 
1.21      matthew   469: =item &StatusOptions()
1.10      matthew   470: 
1.21      matthew   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'.
1.6       stredwic  473: 
1.21      matthew   474: Inputs:
1.6       stredwic  475: 
1.21      matthew   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.
1.6       stredwic  479: 
1.21      matthew   480: $formname: The name of the form.  If defined the onchange attribute of
                    481: the selection box is set to document.$formname.submit().
1.6       stredwic  482: 
1.21      matthew   483: $size: the size (number of lines) of the selection box.
1.6       stredwic  484: 
1.27      matthew   485: $onchange: javascript to use when the value is changed.  Enclosed in 
                    486: double quotes, ""s, not single quotes.
                    487: 
1.21      matthew   488: Returns: a perl string as described.
1.1       stredwic  489: 
1.21      matthew   490: =cut
1.9       stredwic  491: 
1.21      matthew   492: ##############################################
                    493: ##############################################
                    494: sub StatusOptions {
1.27      matthew   495:     my ($status, $formName,$size,$onchange)=@_;
1.21      matthew   496:     $size = 1 if (!defined($size));
                    497:     if (! defined($status)) {
                    498:         $status = 'Active';
                    499:         $status = $ENV{'form.Status'} if (exists($ENV{'form.Status'}));
1.9       stredwic  500:     }
1.1       stredwic  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"';
1.27      matthew   512:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
1.1       stredwic  513:         $Str .= ' onchange="document.'.$formName.'.submit()"';
1.27      matthew   514:     }
                    515:     if (defined($onchange)) {
                    516:         $Str .= ' onchange="'.$onchange.'"';
1.1       stredwic  517:     }
1.21      matthew   518:     $Str .= ' size="'.$size.'" ';
1.1       stredwic  519:     $Str .= '>'."\n";
1.21      matthew   520:     $Str .= '<option value="Active" '.$OpSel1.'>'.
1.37      www       521:         &mt('Currently Enrolled').'</option>'."\n";
1.21      matthew   522:     $Str .= '<option value="Expired" '.$OpSel2.'>'.
1.37      www       523:         &mt('Previously Enrolled').'</option>'."\n";
1.21      matthew   524:     $Str .= '<option value="Any" '.$OpSel3.'>'.
1.37      www       525:         &mt('Any Enrollment Status').'</option>'."\n";
1.1       stredwic  526:     $Str .= '</select>'."\n";
1.7       stredwic  527: }
1.12      matthew   528: 
                    529: ########################################################
                    530: ########################################################
1.7       stredwic  531: 
1.23      matthew   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: =back
                    561: 
                    562: Returns a hash containing the progress state data structure.
                    563: 
                    564: 
                    565: =item &Update_PrgWin
                    566: 
                    567: Updates the text in the progress indicator.  Does not increment the count.
                    568: See &Increment_PrgWin.
                    569: 
                    570: Inputs:
                    571: 
                    572: =over 4
                    573: 
                    574: =item $r Apache request
                    575: 
                    576: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
                    577: 
                    578: =item $displaystring The string to write to the status indicator
                    579: 
                    580: =back
                    581: 
                    582: Returns: none
                    583: 
                    584: 
                    585: =item Increment_PrgWin
                    586: 
                    587: Increment the count of items completed for the progress window by 1.  
                    588: 
                    589: Inputs:
                    590: 
                    591: =over 4
                    592: 
                    593: =item $r Apache request
                    594: 
                    595: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    596: 
                    597: =item $extraInfo A description of the items being iterated over.  Typically
                    598: 'student'.
                    599: 
                    600: =back
                    601: 
                    602: Returns: none
                    603: 
                    604: 
                    605: =item Close_PrgWin
                    606: 
                    607: Closes the progress window.
                    608: 
                    609: Inputs:
                    610: 
                    611: =over 4 
                    612: 
                    613: =item $r Apache request
                    614: 
                    615: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    616: 
                    617: =back
                    618: 
                    619: Returns: none
                    620: 
                    621: =back
                    622: 
                    623: =cut
                    624: 
                    625: ########################################################
                    626: ########################################################
                    627: 
1.7       stredwic  628: # Create progress
                    629: sub Create_PrgWin {
1.14      albertel  630:     my ($r, $title, $heading, $number_to_do)=@_;
1.45      albertel  631:     #the whole function called through timeout is due to issues
                    632:     #in mozilla Read BUG #2665 if you want to know the whole story
1.48    ! albertel  633:     &r_print($r,'<script>'.
        !           634:     "var popwin;
        !           635:      function openpopwin () {
        !           636:      popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
        !           637:     "popwin.document.writeln(\'<html><head><title>$title</title></head>".
        !           638: 	      "<body bgcolor=\"#88DDFF\">".
        !           639:               "<h4>$heading</h4>".
        !           640:               "<form name=popremain>".
        !           641:               '<input type="text" size="55" name="remaining" value="'.
        !           642: 	      &mt('Starting').'"></form>'.
        !           643:               "</body></html>\');".
        !           644:     "popwin.document.close();}".
        !           645:     "\nwindow.setTimeout(openpopwin,0)</script>");
1.7       stredwic  646: 
1.14      albertel  647:     my %prog_state;
1.16      albertel  648:     $prog_state{'done'}=0;
1.23      matthew   649:     $prog_state{'firststart'}=&Time::HiRes::time();
                    650:     $prog_state{'laststart'}=&Time::HiRes::time();
1.16      albertel  651:     $prog_state{'max'}=$number_to_do;
1.14      albertel  652: 
                    653:     return %prog_state;
1.7       stredwic  654: }
                    655: 
                    656: # update progress
                    657: sub Update_PrgWin {
1.14      albertel  658:     my ($r,$prog_state,$displayString)=@_;
1.48    ! albertel  659:     &r_print($r,'<script>popwin.document.popremain.remaining.value="'.
        !           660: 	     $displayString.'";</script>');
1.23      matthew   661:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.14      albertel  662: }
                    663: 
                    664: # increment progress state
                    665: sub Increment_PrgWin {
                    666:     my ($r,$prog_state,$extraInfo)=@_;
1.16      albertel  667:     $$prog_state{'done'}++;
1.23      matthew   668:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
                    669:         $$prog_state{'done'} *
1.16      albertel  670: 	($$prog_state{'max'}-$$prog_state{'done'});
                    671:     $time_est = int($time_est);
                    672:     if (int ($time_est/60) > 0) {
                    673: 	my $min = int($time_est/60);
                    674: 	my $sec = $time_est % 60;
1.31      www       675: 	$time_est = $min.' '.&mt('minutes');
1.25      matthew   676:         if ($min < 10)  {
                    677:             if ($sec > 1) {
1.31      www       678:                 $time_est.= ', '.$sec.' '.&mt('seconds');
1.25      matthew   679:             } elsif ($sec > 0) {
1.31      www       680:                 $time_est.= ', '.$sec.' '.&mt('second');
1.25      matthew   681:             }
                    682:         }
1.16      albertel  683:     } else {
1.31      www       684: 	$time_est .= ' '.&mt('seconds');
1.16      albertel  685:     }
1.23      matthew   686:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
                    687:     if ($lasttime > 9) {
                    688:         $lasttime = int($lasttime);
                    689:     } elsif ($lasttime < 0.01) {
                    690:         $lasttime = 0;
                    691:     } else {
                    692:         $lasttime = sprintf("%3.2f",$lasttime);
                    693:     }
1.19      matthew   694:     if ($lasttime == 1) {
1.32      www       695:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
1.19      matthew   696:     } else {
1.32      www       697:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
1.28      matthew   698:     }
                    699:     #
                    700:     my $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
                    701:     my $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
                    702:     if (! defined($user_browser) || ! defined($user_os)) {
                    703:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    704:                            &Apache::loncommon::decode_user_agent();
                    705:     }
                    706:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                    707:         $lasttime = '';
1.19      matthew   708:     }
1.48    ! albertel  709:     &r_print($r,'<script>popwin.document.popremain.remaining.value="'.
        !           710: 	     $$prog_state{'done'}.'/'.$$prog_state{'max'}.
        !           711: 	     ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
1.23      matthew   712:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.7       stredwic  713: }
                    714: 
                    715: # close Progress Line
                    716: sub Close_PrgWin {
1.14      albertel  717:     my ($r,$prog_state)=@_;
1.48    ! albertel  718:     &r_print($r,'<script>popwin.close()</script>'."\n");
        !           719:     undef(%$prog_state);
        !           720: }
        !           721: 
        !           722: sub r_print {
        !           723:     my ($r,$to_print)=@_;
        !           724:     if ($r) {
        !           725: 	$r->print($to_print);
        !           726: 	$r->rflush();
1.47      sakharuk  727:     } else {
1.48    ! albertel  728: 	print($to_print);
1.47      sakharuk  729:     }
1.1       stredwic  730: }
1.34      www       731: 
                    732: # ------------------------------------------------------- Puts directory header
                    733: 
                    734: sub crumbs {
1.41      www       735:     my ($uri,$target,$prefix,$form)=@_;
1.34      www       736:     my $output='<br /><tt><b><font size="+2">'.$prefix.'/';
1.35      www       737:     if ($ENV{'user.adv'}) {
1.43      www       738: 	my $path=$prefix.'/';
1.35      www       739: 	foreach (split('/',$uri)) {
                    740: 	    unless ($_) { next; }
1.43      www       741: 	    $path.=$_;
                    742: 	    unless ($path eq $uri) { $path.='/'; }
1.41      www       743: 	    my $linkpath=$path;
                    744: 	    if ($form) {
1.43      www       745: 		$linkpath="javascript:$form.action='$path';$form.submit();";
1.41      www       746: 	    }
                    747: 	    $output.='<a href="'.$linkpath.'"'.($target?' target="'.$target.'"':'').'>'.$_.'</a>/';
1.35      www       748: 	}
                    749:     } else {
                    750: 	$output.=$uri;
1.34      www       751:     }
1.36      www       752:     unless ($uri=~/\/$/) { $output=~s/\/$//; }
1.34      www       753:     return $output.'</font></b></tt><br />';
                    754: }
                    755: 
1.1       stredwic  756: 
                    757: 1;
1.23      matthew   758: 
1.1       stredwic  759: __END__

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