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

1.2       www         1: # The LearningOnline Network with CAPA
                      2: # a pile of common html routines
                      3: #
1.56    ! albertel    4: # $Id: lonhtmlcommon.pm,v 1.55 2004/02/20 17:03:38 matthew Exp $
1.2       www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.10      matthew    28: ######################################################################
                     29: ######################################################################
                     30: 
                     31: =pod
                     32: 
                     33: =head1 NAME
                     34: 
                     35: Apache::lonhtmlcommon - routines to do common html things
                     36: 
                     37: =head1 SYNOPSIS
                     38: 
                     39: Referenced by other mod_perl Apache modules.
                     40: 
                     41: =head1 INTRODUCTION
                     42: 
                     43: lonhtmlcommon is a collection of subroutines used to present information
                     44: in a consistent html format, or provide other functionality related to
                     45: html.
                     46: 
                     47: =head2 General Subroutines
                     48: 
                     49: =over 4
                     50: 
                     51: =cut 
                     52: 
                     53: ######################################################################
                     54: ######################################################################
1.2       www        55: 
1.1       stredwic   56: package Apache::lonhtmlcommon;
                     57: 
1.10      matthew    58: use Time::Local;
1.47      sakharuk   59: use Time::HiRes;
1.30      www        60: use Apache::lonlocal;
1.1       stredwic   61: use strict;
                     62: 
1.40      www        63: ##############################################
                     64: ##############################################
                     65: 
                     66: =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: 
1.10      matthew   172: ##############################################
                    173: ##############################################
                    174: 
                    175: =pod
                    176: 
                    177: =item &date_setter
                    178: 
1.22      matthew   179: &date_setter returns html and javascript for a compact date-setting form.
                    180: To retrieve values from it, use &get_date_from_form().
                    181: 
1.10      matthew   182: Inputs
                    183: 
                    184: =over 4
                    185: 
                    186: =item $dname 
                    187: 
                    188: The name to prepend to the form elements.  
                    189: The form elements defined will be dname_year, dname_month, dname_day,
                    190: dname_hour, dname_min, and dname_sec.
                    191: 
                    192: =item $currentvalue
                    193: 
                    194: The current setting for this time parameter.  A unix format time
                    195: (time in seconds since the beginning of Jan 1st, 1970, GMT.  
                    196: An undefined value is taken to indicate the value is the current time.
                    197: Also, to be explicit, a value of 'now' also indicates the current time.
                    198: 
1.26      matthew   199: =item $special
                    200: 
                    201: Additional html/javascript to be associated with each element in
                    202: the date_setter.  See lonparmset for example usage.
                    203: 
1.22      matthew   204: =back
                    205: 
                    206: Bugs
                    207: 
                    208: The method used to restrict user input will fail in the year 2400.
                    209: 
1.10      matthew   210: =cut
                    211: 
                    212: ##############################################
                    213: ##############################################
                    214: sub date_setter {
1.39      www       215:     my ($formname,$dname,$currentvalue,$special,$includeempty) = @_;
1.10      matthew   216:     if (! defined($currentvalue) || $currentvalue eq 'now') {
1.39      www       217: 	unless ($includeempty) {
                    218: 	    $currentvalue = time;
                    219: 	} else {
                    220: 	    $currentvalue = 0;
                    221: 	}
1.10      matthew   222:     }
                    223:     # other potentially useful values:     wkday,yrday,is_daylight_savings
1.39      www       224:     my ($sec,$min,$hour,$mday,$month,$year)=('','','','','','');
                    225:     if ($currentvalue) {
                    226: 	($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
                    227: 	    localtime($currentvalue);
                    228: 	$year += 1900;
                    229:     }
1.10      matthew   230:     my $result = "\n<!-- $dname date setting form -->\n";
                    231:     $result .= <<ENDJS;
                    232: <script language="Javascript">
                    233:     function $dname\_checkday() {
                    234:         var day   = document.$formname.$dname\_day.value;
                    235:         var month = document.$formname.$dname\_month.value;
                    236:         var year  = document.$formname.$dname\_year.value;
                    237:         var valid = true;
                    238:         if (day < 1) {
                    239:             document.$formname.$dname\_day.value = 1;
                    240:         } 
                    241:         if (day > 31) {
                    242:             document.$formname.$dname\_day.value = 31;
                    243:         }
                    244:         if ((month == 1)  || (month == 3)  || (month == 5)  ||
                    245:             (month == 7)  || (month == 8)  || (month == 10) ||
                    246:             (month == 12)) {
                    247:             if (day > 31) {
                    248:                 document.$formname.$dname\_day.value = 31;
                    249:                 day = 31;
                    250:             }
                    251:         } else if (month == 2 ) {
                    252:             if ((year % 4 == 0) && (year % 100 != 0)) {
                    253:                 if (day > 29) {
                    254:                     document.$formname.$dname\_day.value = 29;
                    255:                 }
                    256:             } else if (day > 29) {
                    257:                 document.$formname.$dname\_day.value = 28;
                    258:             }
                    259:         } else if (day > 30) {
                    260:             document.$formname.$dname\_day.value = 30;
                    261:         }
                    262:     }
1.29      www       263: 
                    264:     function $dname\_opencalendar() {
                    265:        var calwin=window.open(
                    266: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
                    267: document.$formname.$dname\_month.value+"&year="+
                    268: document.$formname.$dname\_year.value,
                    269:              "LONCAPAcal",
                    270:               "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
                    271: 
                    272:     }
1.10      matthew   273: </script>
                    274: ENDJS
1.26      matthew   275:     $result .= "  <nobr><select name=\"$dname\_month\" ".$special.' '.
1.10      matthew   276:         "onChange=\"javascript:$dname\_checkday()\" >\n";
                    277:     my @Months = qw/January February  March     April   May      June 
                    278:                     July    August    September October November December/;
                    279:     # Pad @Months with a bogus value to make indexing easier
                    280:     unshift(@Months,'If you can read this an error occurred');
1.39      www       281:     if ($includeempty) { $result.="<option value=''></option>"; }
1.10      matthew   282:     for(my $m = 1;$m <=$#Months;$m++) {
                    283:         $result .= "      <option value=\"$m\" ";
1.39      www       284:         $result .= "selected " if ($m-1 eq $month);
1.30      www       285:         $result .= "> ".&mt($Months[$m])." </option>\n";
1.10      matthew   286:     }
                    287:     $result .= "  </select>\n";
                    288:     $result .= "  <input type=\"text\" name=\"$dname\_day\" ".
1.26      matthew   289:             "value=\"$mday\" size=\"3\" ".$special.' '.
1.10      matthew   290:             "onChange=\"javascript:$dname\_checkday()\" />\n";
                    291:     $result .= "  <input type=\"year\" name=\"$dname\_year\" ".
1.26      matthew   292:             "value=\"$year\" size=\"5\" ".$special.' '.
1.10      matthew   293:             "onChange=\"javascript:$dname\_checkday()\" />\n";
                    294:     $result .= "&nbsp;&nbsp;";
1.26      matthew   295:     $result .= "  <select name=\"$dname\_hour\" ".$special." >\n";
1.39      www       296:     if ($includeempty) { $result.="<option value=''></option>"; }
1.10      matthew   297:     for (my $h = 0;$h<24;$h++) {
                    298:         $result .= "      <option value=\"$h\" ";
                    299:         $result .= "selected " if ($hour == $h);
                    300:         $result .= "> ";
1.30      www       301: 	my $timest='';
1.10      matthew   302:         if ($h == 0) {
1.30      www       303:             $timest .= "12 am";
1.10      matthew   304:         } elsif($h == 12) {
1.30      www       305:             $timest .= "12 noon";
1.10      matthew   306:         } elsif($h < 12) {
1.30      www       307:             $timest .= "$h am";
1.10      matthew   308:         } else {
1.30      www       309:             $timest .= $h-12 ." pm";
1.10      matthew   310:         }
1.30      www       311: 	$timest=&mt($timest);
                    312:         $result .= $timest." </option>\n";
1.10      matthew   313:     } 
                    314:     $result .= "  </select>\n";
1.26      matthew   315:     $result .= "  <input type=\"text\" name=\"$dname\_minute\" ".$special.' '.
1.10      matthew   316:         "value=\"$min\" size=\"3\" /> m\n";
1.26      matthew   317:     $result .= "  <input type=\"text\" name=\"$dname\_second\" ".$special.' '.
1.10      matthew   318:         "value=\"$sec\" size=\"3\" /> s\n";
1.30      www       319:     $result .= "<a href=\"javascript:$dname\_opencalendar()\">".
                    320:     &mt('Select Date')."</a></nobr>\n<!-- end $dname date setting form -->\n";
1.10      matthew   321:     return $result;
                    322: }
                    323: 
                    324: ##############################################
                    325: ##############################################
                    326: 
1.22      matthew   327: =pod
                    328: 
1.10      matthew   329: =item &get_date_from_form
1.22      matthew   330: 
                    331: get_date_from_form retrieves the date specified in an &date_setter form.
1.10      matthew   332: 
                    333: Inputs:
                    334: 
                    335: =over 4
                    336: 
                    337: =item $dname
                    338: 
                    339: The name passed to &datesetter, which prefixes the form elements.
                    340: 
                    341: =item $defaulttime
                    342: 
                    343: The unix time to use as the default in case of poor inputs.
                    344: 
                    345: =back
                    346: 
                    347: Returns: Unix time represented in the form.
                    348: 
                    349: =cut
                    350: 
                    351: ##############################################
                    352: ##############################################
                    353: sub get_date_from_form {
                    354:     my ($dname) = @_;
                    355:     my ($sec,$min,$hour,$day,$month,$year);
                    356:     #
                    357:     if (defined($ENV{'form.'.$dname.'_second'})) {
                    358:         my $tmpsec = $ENV{'form.'.$dname.'_second'};
                    359:         if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
                    360:             $sec = $tmpsec;
                    361:         }
                    362:     }
                    363:     if (defined($ENV{'form.'.$dname.'_minute'})) {
                    364:         my $tmpmin = $ENV{'form.'.$dname.'_minute'};
                    365:         if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
                    366:             $min = $tmpmin;
                    367:         }
                    368:     }
                    369:     if (defined($ENV{'form.'.$dname.'_hour'})) {
                    370:         my $tmphour = $ENV{'form.'.$dname.'_hour'};
1.33      matthew   371:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
1.10      matthew   372:             $hour = $tmphour;
                    373:         }
                    374:     }
                    375:     if (defined($ENV{'form.'.$dname.'_day'})) {
                    376:         my $tmpday = $ENV{'form.'.$dname.'_day'};
                    377:         if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
                    378:             $day = $tmpday;
                    379:         }
                    380:     }
                    381:     if (defined($ENV{'form.'.$dname.'_month'})) {
                    382:         my $tmpmonth = $ENV{'form.'.$dname.'_month'};
                    383:         if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
                    384:             $month = $tmpmonth - 1;
                    385:         }
                    386:     }
                    387:     if (defined($ENV{'form.'.$dname.'_year'})) {
                    388:         my $tmpyear = $ENV{'form.'.$dname.'_year'};
                    389:         if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
                    390:             $year = $tmpyear - 1900;
                    391:         }
                    392:     }
1.24      www       393:     if (($year<70) || ($year>137)) { return undef; }
1.33      matthew   394:     if (defined($sec) && defined($min)   && defined($hour) &&
                    395:         defined($day) && defined($month) && defined($year) &&
                    396:         eval(&timelocal($sec,$min,$hour,$day,$month,$year))) {
1.10      matthew   397:         return &timelocal($sec,$min,$hour,$day,$month,$year);
                    398:     } else {
                    399:         return undef;
                    400:     }
1.20      matthew   401: }
                    402: 
                    403: ##############################################
                    404: ##############################################
                    405: 
                    406: =pod
                    407: 
                    408: =item &pjump_javascript_definition()
                    409: 
                    410: Returns javascript defining the 'pjump' function, which opens up a
                    411: parameter setting wizard.
                    412: 
                    413: =cut
                    414: 
                    415: ##############################################
                    416: ##############################################
                    417: sub pjump_javascript_definition {
                    418:     my $Str = <<END;
                    419:     function pjump(type,dis,value,marker,ret,call) {
                    420:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
                    421:                  +"&value="+escape(value)+"&marker="+escape(marker)
                    422:                  +"&return="+escape(ret)
                    423:                  +"&call="+escape(call)+"&name="+escape(dis),"LONCAPAparms",
                    424:                  "height=350,width=350,scrollbars=no,menubar=no");
                    425:     }
                    426: END
                    427:     return $Str;
1.10      matthew   428: }
                    429: 
                    430: ##############################################
                    431: ##############################################
1.17      matthew   432: 
                    433: =pod
                    434: 
                    435: =item &javascript_nothing()
                    436: 
                    437: Return an appropriate null for the users browser.  This is used
                    438: as the first arguement for window.open calls when you want a blank
                    439: window that you can then write to.
                    440: 
                    441: =cut
                    442: 
                    443: ##############################################
                    444: ##############################################
                    445: sub javascript_nothing {
                    446:     # mozilla and other browsers work with "''", but IE on mac does not.
                    447:     my $nothing = "''";
                    448:     my $user_browser;
                    449:     my $user_os;
                    450:     $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
                    451:     $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
                    452:     if (! defined($user_browser) || ! defined($user_os)) {
                    453:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    454:                            &Apache::loncommon::decode_user_agent();
                    455:     }
                    456:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                    457:         $nothing = "'javascript:void(0);'";
                    458:     }
                    459:     return $nothing;
                    460: }
                    461: 
1.21      matthew   462: 
1.17      matthew   463: ##############################################
                    464: ##############################################
                    465: 
1.21      matthew   466: =pod
1.17      matthew   467: 
1.21      matthew   468: =item &StatusOptions()
1.10      matthew   469: 
1.21      matthew   470: Returns html for a selection box which allows the user to choose the
                    471: enrollment status of students.  The selection box name is 'Status'.
1.6       stredwic  472: 
1.21      matthew   473: Inputs:
1.6       stredwic  474: 
1.21      matthew   475: $status: the currently selected status.  If undefined the value of
                    476: $ENV{'form.Status'} is taken.  If that is undefined, a value of 'Active'
                    477: is used.
1.6       stredwic  478: 
1.21      matthew   479: $formname: The name of the form.  If defined the onchange attribute of
                    480: the selection box is set to document.$formname.submit().
1.6       stredwic  481: 
1.21      matthew   482: $size: the size (number of lines) of the selection box.
1.6       stredwic  483: 
1.27      matthew   484: $onchange: javascript to use when the value is changed.  Enclosed in 
                    485: double quotes, ""s, not single quotes.
                    486: 
1.21      matthew   487: Returns: a perl string as described.
1.1       stredwic  488: 
1.21      matthew   489: =cut
1.9       stredwic  490: 
1.21      matthew   491: ##############################################
                    492: ##############################################
                    493: sub StatusOptions {
1.27      matthew   494:     my ($status, $formName,$size,$onchange)=@_;
1.21      matthew   495:     $size = 1 if (!defined($size));
                    496:     if (! defined($status)) {
                    497:         $status = 'Active';
                    498:         $status = $ENV{'form.Status'} if (exists($ENV{'form.Status'}));
1.9       stredwic  499:     }
1.1       stredwic  500: 
                    501:     my $OpSel1 = '';
                    502:     my $OpSel2 = '';
                    503:     my $OpSel3 = '';
                    504: 
                    505:     if($status eq 'Any')         { $OpSel3 = ' selected'; }
                    506:     elsif($status eq 'Expired' ) { $OpSel2 = ' selected'; }
                    507:     else                         { $OpSel1 = ' selected'; }
                    508: 
                    509:     my $Str = '';
                    510:     $Str .= '<select name="Status"';
1.27      matthew   511:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
1.1       stredwic  512:         $Str .= ' onchange="document.'.$formName.'.submit()"';
1.27      matthew   513:     }
                    514:     if (defined($onchange)) {
                    515:         $Str .= ' onchange="'.$onchange.'"';
1.1       stredwic  516:     }
1.21      matthew   517:     $Str .= ' size="'.$size.'" ';
1.1       stredwic  518:     $Str .= '>'."\n";
1.21      matthew   519:     $Str .= '<option value="Active" '.$OpSel1.'>'.
1.37      www       520:         &mt('Currently Enrolled').'</option>'."\n";
1.21      matthew   521:     $Str .= '<option value="Expired" '.$OpSel2.'>'.
1.37      www       522:         &mt('Previously Enrolled').'</option>'."\n";
1.21      matthew   523:     $Str .= '<option value="Any" '.$OpSel3.'>'.
1.37      www       524:         &mt('Any Enrollment Status').'</option>'."\n";
1.1       stredwic  525:     $Str .= '</select>'."\n";
1.7       stredwic  526: }
1.12      matthew   527: 
                    528: ########################################################
                    529: ########################################################
1.7       stredwic  530: 
1.23      matthew   531: =pod
                    532: 
                    533: =item Progess Window Handling Routines
                    534: 
                    535: These routines handle the creation, update, increment, and closure of 
                    536: progress windows.  The progress window reports to the user the number
                    537: of items completed and an estimate of the time required to complete the rest.
                    538: 
                    539: =over 4
                    540: 
                    541: 
                    542: =item &Create_PrgWin
                    543: 
                    544: Writes javascript to the client to open a progress window and returns a
                    545: data structure used for bookkeeping.
                    546: 
                    547: Inputs
                    548: 
                    549: =over 4
                    550: 
                    551: =item $r Apache request
                    552: 
                    553: =item $title The title of the progress window
                    554: 
                    555: =item $heading A description (usually 1 line) of the process being initiated.
                    556: 
                    557: =item $number_to_do The total number of items being processed.
1.50      albertel  558: 
                    559: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
                    560:        specified)
                    561: 
1.51      albertel  562: =item $width Specify the width in charaters of the input field.
                    563: 
1.50      albertel  564: =item $formname Only useful in the inline case, if a form already exists, this needs to be used and specfiy the name of the form, otherwise the Progress line will be created in a new form of it's own
                    565: 
                    566: =item $inputname Only useful in the inline case, if a form and an input of type text exists, use this to specify the name of the input field 
1.23      matthew   567: 
                    568: =back
                    569: 
                    570: Returns a hash containing the progress state data structure.
                    571: 
                    572: 
                    573: =item &Update_PrgWin
                    574: 
                    575: Updates the text in the progress indicator.  Does not increment the count.
                    576: See &Increment_PrgWin.
                    577: 
                    578: Inputs:
                    579: 
                    580: =over 4
                    581: 
                    582: =item $r Apache request
                    583: 
                    584: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
                    585: 
                    586: =item $displaystring The string to write to the status indicator
                    587: 
                    588: =back
                    589: 
                    590: Returns: none
                    591: 
                    592: 
                    593: =item Increment_PrgWin
                    594: 
                    595: Increment the count of items completed for the progress window by 1.  
                    596: 
                    597: Inputs:
                    598: 
                    599: =over 4
                    600: 
                    601: =item $r Apache request
                    602: 
                    603: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    604: 
                    605: =item $extraInfo A description of the items being iterated over.  Typically
                    606: 'student'.
                    607: 
                    608: =back
                    609: 
                    610: Returns: none
                    611: 
                    612: 
                    613: =item Close_PrgWin
                    614: 
                    615: Closes the progress window.
                    616: 
                    617: Inputs:
                    618: 
                    619: =over 4 
                    620: 
                    621: =item $r Apache request
                    622: 
                    623: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    624: 
                    625: =back
                    626: 
                    627: Returns: none
                    628: 
                    629: =back
                    630: 
                    631: =cut
                    632: 
                    633: ########################################################
                    634: ########################################################
                    635: 
1.51      albertel  636: my $uniq=0;
                    637: sub get_uniq_name {
                    638:     $uniq++;
                    639:     return 'uniquename'.$uniq;
                    640: }
                    641: 
1.7       stredwic  642: # Create progress
                    643: sub Create_PrgWin {
1.51      albertel  644:     my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
                    645: 	$inputname)=@_;
1.49      albertel  646:     if (!defined($type)) { $type='popup'; }
1.51      albertel  647:     if (!defined($width)) { $width=55; }
1.49      albertel  648:     my %prog_state;
                    649:     $prog_state{'type'}=$type;
                    650:     if ($type eq 'popup') {
                    651: 	$prog_state{'window'}='popwin';
                    652: 	#the whole function called through timeout is due to issues
                    653: 	#in mozilla Read BUG #2665 if you want to know the whole story
                    654: 	&r_print($r,'<script>'.
                    655:         "var popwin;
                    656:          function openpopwin () {
                    657:          popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
                    658:         "popwin.document.writeln(\'<html><head><title>$title</title></head>".
1.48      albertel  659: 	      "<body bgcolor=\"#88DDFF\">".
                    660:               "<h4>$heading</h4>".
                    661:               "<form name=popremain>".
1.51      albertel  662:               '<input type="text" size="'.$width.'" name="remaining" value="'.
1.48      albertel  663: 	      &mt('Starting').'"></form>'.
                    664:               "</body></html>\');".
1.49      albertel  665:         "popwin.document.close();}".
                    666:         "\nwindow.setTimeout(openpopwin,0)</script>");
                    667: 	$prog_state{'formname'}='popremain';
                    668: 	$prog_state{'inputname'}="remaining";
                    669:     } elsif ($type eq 'inline') {
                    670: 	$prog_state{'window'}='window';
                    671: 	if (!$formname) {
1.51      albertel  672: 	    $prog_state{'formname'}=&get_uniq_name();
                    673: 	    &r_print($r,'<form name="'.$prog_state{'formname'}.'">');
1.49      albertel  674: 	} else {
                    675: 	    $prog_state{'formname'}=$formname;
                    676: 	}
                    677: 	if (!$inputname) {
1.51      albertel  678: 	    $prog_state{'inputname'}=&get_uniq_name();
1.56    ! albertel  679: 	    &r_print($r,$heading.' <input type="text" name="'.$prog_state{'inputname'}.
1.51      albertel  680: 		     '" size="'.$width.'" />');
1.49      albertel  681: 	} else {
                    682: 	    $prog_state{'inputname'}=$inputname;
                    683: 	    
                    684: 	}
                    685: 	if (!$formname) { &r_print($r,'</form>'); }
                    686: 	&Update_PrgWin($r,\%prog_state,&mt('Starting'));
                    687:     }
1.7       stredwic  688: 
1.16      albertel  689:     $prog_state{'done'}=0;
1.23      matthew   690:     $prog_state{'firststart'}=&Time::HiRes::time();
                    691:     $prog_state{'laststart'}=&Time::HiRes::time();
1.16      albertel  692:     $prog_state{'max'}=$number_to_do;
1.49      albertel  693:     
1.14      albertel  694:     return %prog_state;
1.7       stredwic  695: }
                    696: 
                    697: # update progress
                    698: sub Update_PrgWin {
1.14      albertel  699:     my ($r,$prog_state,$displayString)=@_;
1.49      albertel  700:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
                    701: 	     $$prog_state{'formname'}.'.'.
                    702: 	     $$prog_state{'inputname'}.'.value="'.
1.48      albertel  703: 	     $displayString.'";</script>');
1.23      matthew   704:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.14      albertel  705: }
                    706: 
                    707: # increment progress state
                    708: sub Increment_PrgWin {
                    709:     my ($r,$prog_state,$extraInfo)=@_;
1.16      albertel  710:     $$prog_state{'done'}++;
1.23      matthew   711:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
                    712:         $$prog_state{'done'} *
1.16      albertel  713: 	($$prog_state{'max'}-$$prog_state{'done'});
                    714:     $time_est = int($time_est);
                    715:     if (int ($time_est/60) > 0) {
                    716: 	my $min = int($time_est/60);
                    717: 	my $sec = $time_est % 60;
1.31      www       718: 	$time_est = $min.' '.&mt('minutes');
1.25      matthew   719:         if ($min < 10)  {
                    720:             if ($sec > 1) {
1.31      www       721:                 $time_est.= ', '.$sec.' '.&mt('seconds');
1.25      matthew   722:             } elsif ($sec > 0) {
1.31      www       723:                 $time_est.= ', '.$sec.' '.&mt('second');
1.25      matthew   724:             }
                    725:         }
1.16      albertel  726:     } else {
1.31      www       727: 	$time_est .= ' '.&mt('seconds');
1.16      albertel  728:     }
1.23      matthew   729:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
                    730:     if ($lasttime > 9) {
                    731:         $lasttime = int($lasttime);
                    732:     } elsif ($lasttime < 0.01) {
                    733:         $lasttime = 0;
                    734:     } else {
                    735:         $lasttime = sprintf("%3.2f",$lasttime);
                    736:     }
1.19      matthew   737:     if ($lasttime == 1) {
1.32      www       738:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
1.19      matthew   739:     } else {
1.32      www       740:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
1.28      matthew   741:     }
                    742:     #
                    743:     my $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
                    744:     my $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
                    745:     if (! defined($user_browser) || ! defined($user_os)) {
                    746:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    747:                            &Apache::loncommon::decode_user_agent();
                    748:     }
                    749:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                    750:         $lasttime = '';
1.19      matthew   751:     }
1.49      albertel  752:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
                    753: 	     $$prog_state{'formname'}.'.'.
                    754: 	     $$prog_state{'inputname'}.'.value="'.
1.48      albertel  755: 	     $$prog_state{'done'}.'/'.$$prog_state{'max'}.
                    756: 	     ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
1.23      matthew   757:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.7       stredwic  758: }
                    759: 
                    760: # close Progress Line
                    761: sub Close_PrgWin {
1.14      albertel  762:     my ($r,$prog_state)=@_;
1.49      albertel  763:     if ($$prog_state{'type'} eq 'popup') {
                    764: 	&r_print($r,'<script>popwin.close()</script>'."\n");
                    765:     } elsif ($$prog_state{'type'} eq 'inline') {
                    766: 	&Update_PrgWin($r,$prog_state,&mt('Done'));
                    767:     }
1.48      albertel  768:     undef(%$prog_state);
                    769: }
                    770: 
                    771: sub r_print {
                    772:     my ($r,$to_print)=@_;
                    773:     if ($r) {
                    774: 	$r->print($to_print);
                    775: 	$r->rflush();
1.47      sakharuk  776:     } else {
1.48      albertel  777: 	print($to_print);
1.47      sakharuk  778:     }
1.1       stredwic  779: }
1.34      www       780: 
                    781: # ------------------------------------------------------- Puts directory header
                    782: 
                    783: sub crumbs {
1.41      www       784:     my ($uri,$target,$prefix,$form)=@_;
1.34      www       785:     my $output='<br /><tt><b><font size="+2">'.$prefix.'/';
1.35      www       786:     if ($ENV{'user.adv'}) {
1.43      www       787: 	my $path=$prefix.'/';
1.35      www       788: 	foreach (split('/',$uri)) {
                    789: 	    unless ($_) { next; }
1.43      www       790: 	    $path.=$_;
                    791: 	    unless ($path eq $uri) { $path.='/'; }
1.41      www       792: 	    my $linkpath=$path;
                    793: 	    if ($form) {
1.43      www       794: 		$linkpath="javascript:$form.action='$path';$form.submit();";
1.41      www       795: 	    }
                    796: 	    $output.='<a href="'.$linkpath.'"'.($target?' target="'.$target.'"':'').'>'.$_.'</a>/';
1.35      www       797: 	}
                    798:     } else {
                    799: 	$output.=$uri;
1.34      www       800:     }
1.36      www       801:     unless ($uri=~/\/$/) { $output=~s/\/$//; }
1.34      www       802:     return $output.'</font></b></tt><br />';
                    803: }
                    804: 
1.52      www       805: # ------------------------------------------------- Output headers for HTMLArea
                    806: 
                    807: sub htmlareaheaders {
                    808:     unless (&htmlareabrowser()) { return ''; }
                    809:     my $lang='en';
                    810:     return (<<ENDHEADERS);
                    811: <script type="text/javascript" src="/htmlarea/htmlarea.js"></script>
                    812: <script type="text/javascript" src="/htmlarea/lang/$lang.js"></script>
                    813: <script type="text/javascript" src="/htmlarea/dialog.js"></script>
                    814: <style type="text/css">
                    815: \@import url(/htmlarea/htmlarea.css);
                    816: </style>
                    817: ENDHEADERS
                    818: }
                    819: 
                    820: # ---------------------------------------------------------- Script to activate
                    821: 
                    822: sub htmlareaactive {
                    823:     unless (&htmlareabrowser()) { return ''; }
                    824:     return (<<ENDSCRIPT);
                    825: <script type="text/javascript" defer="1">
                    826:     HTMLArea.replaceAll();
                    827: </script>
                    828: ENDSCRIPT
                    829: }
                    830: 
                    831: # ---------------------------------------- Browser capable of running HTMLArea?
                    832: 
                    833: sub htmlareabrowser {
                    834:     return 1;
                    835: }
1.53      matthew   836: 
                    837: ############################################################
                    838: ############################################################
                    839: 
                    840: =pod
                    841: 
                    842: =item breadcrumbs
                    843: 
                    844: Compiles the previously registered breadcrumbs into an series of links.
                    845: FAQ and BUG links will be placed on the left side of the table if they
                    846: are defined for the last registered breadcrumb.  
                    847: Additionally supports a 'component', which will be displayed on the
                    848: right side of the table (without a link).
                    849: A link to help for the component will be included if one is specified.
                    850: 
                    851: All inputs can be undef without problems.
                    852: 
                    853: Inputs: $color (the background color of the table returned),
                    854:         $component (the large text on the right side of the table),
                    855:         $component_help
                    856: 
                    857: Returns a string containing breadcrumbs for the current page.
                    858: 
                    859: =item clear_breadcrumbs
                    860: 
                    861: Clears the previously stored breadcrumbs.
                    862: 
                    863: =item add_breadcrumb
                    864: 
                    865: Pushes a breadcrumb on the stack of crumbs.
                    866: 
                    867: input: $breadcrumb, a hash reference.  The keys 'href','title', and 'text'
                    868: are required.  If present the keys 'faq' and 'bug' will be used to provide
                    869: links to the FAQ and bug sites.
                    870: 
                    871: returns: nothing    
                    872: 
                    873: =cut
                    874: 
                    875: ############################################################
                    876: ############################################################
                    877: {
                    878:     my @Crumbs;
                    879: 
                    880:     sub breadcrumbs {
1.55      matthew   881:         my ($color,$component,$component_help,$function,$domain) = @_;
                    882:         if (! defined($color)) {
                    883:             if (! defined($function)) {
                    884:                 $function = &Apache::loncommon::get_users_function();
                    885:             }
                    886:             $color = &Apache::loncommon::designparm($function.'.tabbg',
                    887:                                                     $domain);
                    888:         }
1.53      matthew   889:         #
                    890:         my $Str = "\n".
                    891:             '<table width="100%" border="0" cellpadding="0" cellspacing="0">'.
                    892:             '<tr><td bgcolor="'.$color.'">'.
                    893:             '<font size="-1">';
                    894:         # The last breadcrumb does not have a link, so handle it seperately.
                    895:         my $last = pop(@Crumbs);
                    896:         # The first one should be the course, I guess.
                    897:         if (exists($ENV{'request.course.id'})) {
                    898:             my $cid = $ENV{'request.course.id'};
                    899:             unshift(@Crumbs,{href=>'/adm/menu',
                    900:                              title=>'Go to main menu',
                    901:                              text=>$ENV{'course.'.$cid.'.description'},
                    902:                          });
                    903:         }
                    904:         my $links .= 
                    905:             join('-&gt;',
                    906:                  map {
                    907:                      '<a href="'.$_->{'href'}.'" title="'.$_->{'title'}.'">'.
                    908:                          $_->{'text'}.'</a>' 
                    909:                      } @Crumbs
                    910:                  );
                    911:         $links .= '-&gt;' if ($links ne '');
                    912:         $links .= '<b>'.$last->{'text'}.'</b>';
1.54      matthew   913:         #
                    914:         my $icons = '';
                    915:         if (exists($last->{'faq'})) {
                    916:             $icons .= &Apache::loncommon::help_open_faq($last->{'faq'});
                    917:         }
1.53      matthew   918:         if (exists($last->{'bug'})) {
1.54      matthew   919:             $icons .= &Apache::loncommon::help_open_bug($last->{'bug'});
1.53      matthew   920:         }
1.54      matthew   921:         if ($icons ne '') {
                    922:             $Str .= $icons.'&nbsp;';
1.53      matthew   923:         }
1.54      matthew   924:         #
1.53      matthew   925:         $Str .= $links.'</font></td>';
1.54      matthew   926:         #
1.53      matthew   927:         if (defined($component)) {
                    928:             $Str .= '<td align="right" bgcolor="'.$color.'">'.
                    929:                 '<font size="+1">'.$component.'</font>';
                    930:             if (defined($component_help)) {
                    931:                 $Str .= 
                    932:                     &Apache::loncommon::help_open_topic($component_help);
                    933:             }
                    934:             $Str.= '</td>';
                    935:         }
                    936:         $Str .= '</tr></table>'."\n";
                    937:         #
                    938:         # Return the @Crumbs stack to what we started with
                    939:         push(@Crumbs,$last);
                    940:         shift(@Crumbs);
                    941:         #
                    942:         return $Str;
                    943:     }
                    944: 
                    945:     sub clear_breadcrumbs {
                    946:         undef(@Crumbs);
                    947:     }
                    948: 
                    949:     sub add_breadcrumb {
                    950:         push (@Crumbs,@_);
                    951:     }
                    952: 
                    953: }
                    954: 
                    955: ############################################################
                    956: ############################################################
                    957: 
1.1       stredwic  958: 
                    959: 1;
1.23      matthew   960: 
1.1       stredwic  961: __END__

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