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

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

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