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

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

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