File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.67: download - view: text, annotated - select for diffs
Fri Apr 23 17:49:25 2004 UTC (20 years, 1 month ago) by matthew
Branches: MAIN
CVS tags: HEAD
&date_setter: Added parameter no_hh_mm_ss, to omit the hrs, min, and
seconds fields.
&get_date_from_form: hrs, min, sec now default to 0 if not defined.
Still requires day, month, and year be defined.

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

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