File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.55: download - view: text, annotated - select for diffs
Fri Feb 20 17:03:38 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
loncommon.pm: Moved code from &bodytag which determines users primary function
              to new subrountine &get_users_function.
lonhtmlcommon.pm: &bodytag now gets the default background color for the
                  domain.

    1: # The LearningOnline Network with CAPA
    2: # a pile of common html routines
    3: #
    4: # $Id: lonhtmlcommon.pm,v 1.55 2004/02/20 17:03:38 matthew Exp $
    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: #
   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: ######################################################################
   55: 
   56: package Apache::lonhtmlcommon;
   57: 
   58: use Time::Local;
   59: use Time::HiRes;
   60: use Apache::lonlocal;
   61: use strict;
   62: 
   63: ##############################################
   64: ##############################################
   65: 
   66: =pod
   67: 
   68: =item authorbombs
   69: 
   70: =cut
   71: 
   72: ##############################################
   73: ##############################################
   74: 
   75: sub authorbombs {
   76:     my $url=shift;
   77:     $url=&Apache::lonnet::declutter($url);
   78:     my ($udom,$uname)=($url=~/^(\w+)\/(\w+)\//);
   79:     my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
   80:     foreach (keys %bombs) {
   81: 	if ($_=~/^$udom\/$uname\//) {
   82: 	    return '<a href="/adm/bombs/'.$url.
   83: 		'"><img src="/adm/lonMisc/bomb.gif" border="0" /></a>'.
   84: 		&Apache::loncommon::help_open_topic('About_Bombs');
   85: 	}
   86:     }
   87:     return '';
   88: }
   89: 
   90: ##############################################
   91: ##############################################
   92: 
   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: 
  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 {
  166:     my ($name,$value) = @_;
  167:     my $Str = '<input type="checkbox" name="'.$name.'"'.
  168: 	($value?' checked="1"':'').' />';
  169:     return $Str;
  170: }
  171: 
  172: ##############################################
  173: ##############################################
  174: 
  175: =pod
  176: 
  177: =item &date_setter
  178: 
  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: 
  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: 
  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: 
  204: =back
  205: 
  206: Bugs
  207: 
  208: The method used to restrict user input will fail in the year 2400.
  209: 
  210: =cut
  211: 
  212: ##############################################
  213: ##############################################
  214: sub date_setter {
  215:     my ($formname,$dname,$currentvalue,$special,$includeempty) = @_;
  216:     if (! defined($currentvalue) || $currentvalue eq 'now') {
  217: 	unless ($includeempty) {
  218: 	    $currentvalue = time;
  219: 	} else {
  220: 	    $currentvalue = 0;
  221: 	}
  222:     }
  223:     # other potentially useful values:     wkday,yrday,is_daylight_savings
  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:     }
  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:     }
  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:     }
  273: </script>
  274: ENDJS
  275:     $result .= "  <nobr><select name=\"$dname\_month\" ".$special.' '.
  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');
  281:     if ($includeempty) { $result.="<option value=''></option>"; }
  282:     for(my $m = 1;$m <=$#Months;$m++) {
  283:         $result .= "      <option value=\"$m\" ";
  284:         $result .= "selected " if ($m-1 eq $month);
  285:         $result .= "> ".&mt($Months[$m])." </option>\n";
  286:     }
  287:     $result .= "  </select>\n";
  288:     $result .= "  <input type=\"text\" name=\"$dname\_day\" ".
  289:             "value=\"$mday\" size=\"3\" ".$special.' '.
  290:             "onChange=\"javascript:$dname\_checkday()\" />\n";
  291:     $result .= "  <input type=\"year\" name=\"$dname\_year\" ".
  292:             "value=\"$year\" size=\"5\" ".$special.' '.
  293:             "onChange=\"javascript:$dname\_checkday()\" />\n";
  294:     $result .= "&nbsp;&nbsp;";
  295:     $result .= "  <select name=\"$dname\_hour\" ".$special." >\n";
  296:     if ($includeempty) { $result.="<option value=''></option>"; }
  297:     for (my $h = 0;$h<24;$h++) {
  298:         $result .= "      <option value=\"$h\" ";
  299:         $result .= "selected " if ($hour == $h);
  300:         $result .= "> ";
  301: 	my $timest='';
  302:         if ($h == 0) {
  303:             $timest .= "12 am";
  304:         } elsif($h == 12) {
  305:             $timest .= "12 noon";
  306:         } elsif($h < 12) {
  307:             $timest .= "$h am";
  308:         } else {
  309:             $timest .= $h-12 ." pm";
  310:         }
  311: 	$timest=&mt($timest);
  312:         $result .= $timest." </option>\n";
  313:     } 
  314:     $result .= "  </select>\n";
  315:     $result .= "  <input type=\"text\" name=\"$dname\_minute\" ".$special.' '.
  316:         "value=\"$min\" size=\"3\" /> m\n";
  317:     $result .= "  <input type=\"text\" name=\"$dname\_second\" ".$special.' '.
  318:         "value=\"$sec\" size=\"3\" /> s\n";
  319:     $result .= "<a href=\"javascript:$dname\_opencalendar()\">".
  320:     &mt('Select Date')."</a></nobr>\n<!-- end $dname date setting form -->\n";
  321:     return $result;
  322: }
  323: 
  324: ##############################################
  325: ##############################################
  326: 
  327: =pod
  328: 
  329: =item &get_date_from_form
  330: 
  331: get_date_from_form retrieves the date specified in an &date_setter form.
  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'};
  371:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
  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:     }
  393:     if (($year<70) || ($year>137)) { return undef; }
  394:     if (defined($sec) && defined($min)   && defined($hour) &&
  395:         defined($day) && defined($month) && defined($year) &&
  396:         eval(&timelocal($sec,$min,$hour,$day,$month,$year))) {
  397:         return &timelocal($sec,$min,$hour,$day,$month,$year);
  398:     } else {
  399:         return undef;
  400:     }
  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;
  428: }
  429: 
  430: ##############################################
  431: ##############################################
  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: 
  462: 
  463: ##############################################
  464: ##############################################
  465: 
  466: =pod
  467: 
  468: =item &StatusOptions()
  469: 
  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'.
  472: 
  473: Inputs:
  474: 
  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.
  478: 
  479: $formname: The name of the form.  If defined the onchange attribute of
  480: the selection box is set to document.$formname.submit().
  481: 
  482: $size: the size (number of lines) of the selection box.
  483: 
  484: $onchange: javascript to use when the value is changed.  Enclosed in 
  485: double quotes, ""s, not single quotes.
  486: 
  487: Returns: a perl string as described.
  488: 
  489: =cut
  490: 
  491: ##############################################
  492: ##############################################
  493: sub StatusOptions {
  494:     my ($status, $formName,$size,$onchange)=@_;
  495:     $size = 1 if (!defined($size));
  496:     if (! defined($status)) {
  497:         $status = 'Active';
  498:         $status = $ENV{'form.Status'} if (exists($ENV{'form.Status'}));
  499:     }
  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"';
  511:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
  512:         $Str .= ' onchange="document.'.$formName.'.submit()"';
  513:     }
  514:     if (defined($onchange)) {
  515:         $Str .= ' onchange="'.$onchange.'"';
  516:     }
  517:     $Str .= ' size="'.$size.'" ';
  518:     $Str .= '>'."\n";
  519:     $Str .= '<option value="Active" '.$OpSel1.'>'.
  520:         &mt('Currently Enrolled').'</option>'."\n";
  521:     $Str .= '<option value="Expired" '.$OpSel2.'>'.
  522:         &mt('Previously Enrolled').'</option>'."\n";
  523:     $Str .= '<option value="Any" '.$OpSel3.'>'.
  524:         &mt('Any Enrollment Status').'</option>'."\n";
  525:     $Str .= '</select>'."\n";
  526: }
  527: 
  528: ########################################################
  529: ########################################################
  530: 
  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.
  558: 
  559: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
  560:        specified)
  561: 
  562: =item $width Specify the width in charaters of the input field.
  563: 
  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 
  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: 
  636: my $uniq=0;
  637: sub get_uniq_name {
  638:     $uniq++;
  639:     return 'uniquename'.$uniq;
  640: }
  641: 
  642: # Create progress
  643: sub Create_PrgWin {
  644:     my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
  645: 	$inputname)=@_;
  646:     if (!defined($type)) { $type='popup'; }
  647:     if (!defined($width)) { $width=55; }
  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>".
  659: 	      "<body bgcolor=\"#88DDFF\">".
  660:               "<h4>$heading</h4>".
  661:               "<form name=popremain>".
  662:               '<input type="text" size="'.$width.'" name="remaining" value="'.
  663: 	      &mt('Starting').'"></form>'.
  664:               "</body></html>\');".
  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) {
  672: 	    $prog_state{'formname'}=&get_uniq_name();
  673: 	    &r_print($r,'<form name="'.$prog_state{'formname'}.'">');
  674: 	} else {
  675: 	    $prog_state{'formname'}=$formname;
  676: 	}
  677: 	if (!$inputname) {
  678: 	    $prog_state{'inputname'}=&get_uniq_name();
  679: 	    &r_print($r,'<input type="text" name="'.$prog_state{'inputname'}.
  680: 		     '" size="'.$width.'" />');
  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:     }
  688: 
  689:     $prog_state{'done'}=0;
  690:     $prog_state{'firststart'}=&Time::HiRes::time();
  691:     $prog_state{'laststart'}=&Time::HiRes::time();
  692:     $prog_state{'max'}=$number_to_do;
  693:     
  694:     return %prog_state;
  695: }
  696: 
  697: # update progress
  698: sub Update_PrgWin {
  699:     my ($r,$prog_state,$displayString)=@_;
  700:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
  701: 	     $$prog_state{'formname'}.'.'.
  702: 	     $$prog_state{'inputname'}.'.value="'.
  703: 	     $displayString.'";</script>');
  704:     $$prog_state{'laststart'}=&Time::HiRes::time();
  705: }
  706: 
  707: # increment progress state
  708: sub Increment_PrgWin {
  709:     my ($r,$prog_state,$extraInfo)=@_;
  710:     $$prog_state{'done'}++;
  711:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
  712:         $$prog_state{'done'} *
  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;
  718: 	$time_est = $min.' '.&mt('minutes');
  719:         if ($min < 10)  {
  720:             if ($sec > 1) {
  721:                 $time_est.= ', '.$sec.' '.&mt('seconds');
  722:             } elsif ($sec > 0) {
  723:                 $time_est.= ', '.$sec.' '.&mt('second');
  724:             }
  725:         }
  726:     } else {
  727: 	$time_est .= ' '.&mt('seconds');
  728:     }
  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:     }
  737:     if ($lasttime == 1) {
  738:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
  739:     } else {
  740:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
  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 = '';
  751:     }
  752:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
  753: 	     $$prog_state{'formname'}.'.'.
  754: 	     $$prog_state{'inputname'}.'.value="'.
  755: 	     $$prog_state{'done'}.'/'.$$prog_state{'max'}.
  756: 	     ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
  757:     $$prog_state{'laststart'}=&Time::HiRes::time();
  758: }
  759: 
  760: # close Progress Line
  761: sub Close_PrgWin {
  762:     my ($r,$prog_state)=@_;
  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:     }
  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();
  776:     } else {
  777: 	print($to_print);
  778:     }
  779: }
  780: 
  781: # ------------------------------------------------------- Puts directory header
  782: 
  783: sub crumbs {
  784:     my ($uri,$target,$prefix,$form)=@_;
  785:     my $output='<br /><tt><b><font size="+2">'.$prefix.'/';
  786:     if ($ENV{'user.adv'}) {
  787: 	my $path=$prefix.'/';
  788: 	foreach (split('/',$uri)) {
  789: 	    unless ($_) { next; }
  790: 	    $path.=$_;
  791: 	    unless ($path eq $uri) { $path.='/'; }
  792: 	    my $linkpath=$path;
  793: 	    if ($form) {
  794: 		$linkpath="javascript:$form.action='$path';$form.submit();";
  795: 	    }
  796: 	    $output.='<a href="'.$linkpath.'"'.($target?' target="'.$target.'"':'').'>'.$_.'</a>/';
  797: 	}
  798:     } else {
  799: 	$output.=$uri;
  800:     }
  801:     unless ($uri=~/\/$/) { $output=~s/\/$//; }
  802:     return $output.'</font></b></tt><br />';
  803: }
  804: 
  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: }
  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 {
  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:         }
  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>';
  913:         #
  914:         my $icons = '';
  915:         if (exists($last->{'faq'})) {
  916:             $icons .= &Apache::loncommon::help_open_faq($last->{'faq'});
  917:         }
  918:         if (exists($last->{'bug'})) {
  919:             $icons .= &Apache::loncommon::help_open_bug($last->{'bug'});
  920:         }
  921:         if ($icons ne '') {
  922:             $Str .= $icons.'&nbsp;';
  923:         }
  924:         #
  925:         $Str .= $links.'</font></td>';
  926:         #
  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: 
  958: 
  959: 1;
  960: 
  961: __END__

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