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

1.2       www         1: # The LearningOnline Network with CAPA
                      2: # a pile of common html routines
                      3: #
1.50    ! albertel    4: # $Id: lonhtmlcommon.pm,v 1.49 2004/02/16 22:28:55 albertel 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.
1.50    ! albertel  559: 
        !           560: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
        !           561:        specified)
        !           562: 
        !           563: =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
        !           564: 
        !           565: =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 
1.23      matthew   566: 
                    567: =back
                    568: 
                    569: Returns a hash containing the progress state data structure.
                    570: 
                    571: 
                    572: =item &Update_PrgWin
                    573: 
                    574: Updates the text in the progress indicator.  Does not increment the count.
                    575: See &Increment_PrgWin.
                    576: 
                    577: Inputs:
                    578: 
                    579: =over 4
                    580: 
                    581: =item $r Apache request
                    582: 
                    583: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
                    584: 
                    585: =item $displaystring The string to write to the status indicator
                    586: 
                    587: =back
                    588: 
                    589: Returns: none
                    590: 
                    591: 
                    592: =item Increment_PrgWin
                    593: 
                    594: Increment the count of items completed for the progress window by 1.  
                    595: 
                    596: Inputs:
                    597: 
                    598: =over 4
                    599: 
                    600: =item $r Apache request
                    601: 
                    602: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    603: 
                    604: =item $extraInfo A description of the items being iterated over.  Typically
                    605: 'student'.
                    606: 
                    607: =back
                    608: 
                    609: Returns: none
                    610: 
                    611: 
                    612: =item Close_PrgWin
                    613: 
                    614: Closes the progress window.
                    615: 
                    616: Inputs:
                    617: 
                    618: =over 4 
                    619: 
                    620: =item $r Apache request
                    621: 
                    622: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    623: 
                    624: =back
                    625: 
                    626: Returns: none
                    627: 
                    628: =back
                    629: 
                    630: =cut
                    631: 
                    632: ########################################################
                    633: ########################################################
                    634: 
1.7       stredwic  635: # Create progress
                    636: sub Create_PrgWin {
1.49      albertel  637:     my ($r, $title, $heading, $number_to_do,$type,$formname,$inputname)=@_;
                    638:     if (!defined($type)) { $type='popup'; }
                    639:     my %prog_state;
                    640:     $prog_state{'type'}=$type;
                    641:     if ($type eq 'popup') {
                    642: 	$prog_state{'window'}='popwin';
                    643: 	#the whole function called through timeout is due to issues
                    644: 	#in mozilla Read BUG #2665 if you want to know the whole story
                    645: 	&r_print($r,'<script>'.
                    646:         "var popwin;
                    647:          function openpopwin () {
                    648:          popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
                    649:         "popwin.document.writeln(\'<html><head><title>$title</title></head>".
1.48      albertel  650: 	      "<body bgcolor=\"#88DDFF\">".
                    651:               "<h4>$heading</h4>".
                    652:               "<form name=popremain>".
                    653:               '<input type="text" size="55" name="remaining" value="'.
                    654: 	      &mt('Starting').'"></form>'.
                    655:               "</body></html>\');".
1.49      albertel  656:         "popwin.document.close();}".
                    657:         "\nwindow.setTimeout(openpopwin,0)</script>");
                    658: 	$prog_state{'formname'}='popremain';
                    659: 	$prog_state{'inputname'}="remaining";
                    660:     } elsif ($type eq 'inline') {
                    661: 	$prog_state{'window'}='window';
                    662: 	if (!$formname) {
                    663: 	    &r_print($r,'<form name="progresswindow">');
                    664: 	    $prog_state{'formname'}='progresswindow';
                    665: 	} else {
                    666: 	    $prog_state{'formname'}=$formname;
                    667: 	}
                    668: 	if (!$inputname) {
                    669: 	    &r_print($r,'<input type="text" name="progressline" />');
                    670: 	    $prog_state{'inputname'}="progressline";
                    671: 	} else {
                    672: 	    $prog_state{'inputname'}=$inputname;
                    673: 	    
                    674: 	}
                    675: 	if (!$formname) { &r_print($r,'</form>'); }
                    676: 	&Update_PrgWin($r,\%prog_state,&mt('Starting'));
                    677:     }
1.7       stredwic  678: 
1.16      albertel  679:     $prog_state{'done'}=0;
1.23      matthew   680:     $prog_state{'firststart'}=&Time::HiRes::time();
                    681:     $prog_state{'laststart'}=&Time::HiRes::time();
1.16      albertel  682:     $prog_state{'max'}=$number_to_do;
1.49      albertel  683:     
1.14      albertel  684:     return %prog_state;
1.7       stredwic  685: }
                    686: 
                    687: # update progress
                    688: sub Update_PrgWin {
1.14      albertel  689:     my ($r,$prog_state,$displayString)=@_;
1.49      albertel  690:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
                    691: 	     $$prog_state{'formname'}.'.'.
                    692: 	     $$prog_state{'inputname'}.'.value="'.
1.48      albertel  693: 	     $displayString.'";</script>');
1.23      matthew   694:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.14      albertel  695: }
                    696: 
                    697: # increment progress state
                    698: sub Increment_PrgWin {
                    699:     my ($r,$prog_state,$extraInfo)=@_;
1.16      albertel  700:     $$prog_state{'done'}++;
1.23      matthew   701:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
                    702:         $$prog_state{'done'} *
1.16      albertel  703: 	($$prog_state{'max'}-$$prog_state{'done'});
                    704:     $time_est = int($time_est);
                    705:     if (int ($time_est/60) > 0) {
                    706: 	my $min = int($time_est/60);
                    707: 	my $sec = $time_est % 60;
1.31      www       708: 	$time_est = $min.' '.&mt('minutes');
1.25      matthew   709:         if ($min < 10)  {
                    710:             if ($sec > 1) {
1.31      www       711:                 $time_est.= ', '.$sec.' '.&mt('seconds');
1.25      matthew   712:             } elsif ($sec > 0) {
1.31      www       713:                 $time_est.= ', '.$sec.' '.&mt('second');
1.25      matthew   714:             }
                    715:         }
1.16      albertel  716:     } else {
1.31      www       717: 	$time_est .= ' '.&mt('seconds');
1.16      albertel  718:     }
1.23      matthew   719:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
                    720:     if ($lasttime > 9) {
                    721:         $lasttime = int($lasttime);
                    722:     } elsif ($lasttime < 0.01) {
                    723:         $lasttime = 0;
                    724:     } else {
                    725:         $lasttime = sprintf("%3.2f",$lasttime);
                    726:     }
1.19      matthew   727:     if ($lasttime == 1) {
1.32      www       728:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
1.19      matthew   729:     } else {
1.32      www       730:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
1.28      matthew   731:     }
                    732:     #
                    733:     my $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
                    734:     my $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
                    735:     if (! defined($user_browser) || ! defined($user_os)) {
                    736:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    737:                            &Apache::loncommon::decode_user_agent();
                    738:     }
                    739:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                    740:         $lasttime = '';
1.19      matthew   741:     }
1.49      albertel  742:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
                    743: 	     $$prog_state{'formname'}.'.'.
                    744: 	     $$prog_state{'inputname'}.'.value="'.
1.48      albertel  745: 	     $$prog_state{'done'}.'/'.$$prog_state{'max'}.
                    746: 	     ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
1.23      matthew   747:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.7       stredwic  748: }
                    749: 
                    750: # close Progress Line
                    751: sub Close_PrgWin {
1.14      albertel  752:     my ($r,$prog_state)=@_;
1.49      albertel  753:     if ($$prog_state{'type'} eq 'popup') {
                    754: 	&r_print($r,'<script>popwin.close()</script>'."\n");
                    755:     } elsif ($$prog_state{'type'} eq 'inline') {
                    756: 	&Update_PrgWin($r,$prog_state,&mt('Done'));
                    757:     }
1.48      albertel  758:     undef(%$prog_state);
                    759: }
                    760: 
                    761: sub r_print {
                    762:     my ($r,$to_print)=@_;
                    763:     if ($r) {
                    764: 	$r->print($to_print);
                    765: 	$r->rflush();
1.47      sakharuk  766:     } else {
1.48      albertel  767: 	print($to_print);
1.47      sakharuk  768:     }
1.1       stredwic  769: }
1.34      www       770: 
                    771: # ------------------------------------------------------- Puts directory header
                    772: 
                    773: sub crumbs {
1.41      www       774:     my ($uri,$target,$prefix,$form)=@_;
1.34      www       775:     my $output='<br /><tt><b><font size="+2">'.$prefix.'/';
1.35      www       776:     if ($ENV{'user.adv'}) {
1.43      www       777: 	my $path=$prefix.'/';
1.35      www       778: 	foreach (split('/',$uri)) {
                    779: 	    unless ($_) { next; }
1.43      www       780: 	    $path.=$_;
                    781: 	    unless ($path eq $uri) { $path.='/'; }
1.41      www       782: 	    my $linkpath=$path;
                    783: 	    if ($form) {
1.43      www       784: 		$linkpath="javascript:$form.action='$path';$form.submit();";
1.41      www       785: 	    }
                    786: 	    $output.='<a href="'.$linkpath.'"'.($target?' target="'.$target.'"':'').'>'.$_.'</a>/';
1.35      www       787: 	}
                    788:     } else {
                    789: 	$output.=$uri;
1.34      www       790:     }
1.36      www       791:     unless ($uri=~/\/$/) { $output=~s/\/$//; }
1.34      www       792:     return $output.'</font></b></tt><br />';
                    793: }
                    794: 
1.1       stredwic  795: 
                    796: 1;
1.23      matthew   797: 
1.1       stredwic  798: __END__

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