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

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

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