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

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

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