File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.90: download - view: text, annotated - select for diffs
Wed Sep 29 14:50:43 2004 UTC (19 years, 8 months ago) by www
Branches: MAIN
CVS tags: HEAD
Work-around for Safari bug regarding 'document.open', Bug #3449

    1: # The LearningOnline Network with CAPA
    2: # a pile of common html routines
    3: #
    4: # $Id: lonhtmlcommon.pm,v 1.90 2004/09/29 14:50:43 www 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 remove_recent {
  121:     my ($area,$names)=@_;
  122:     my $file=&recent_filename($area);
  123:     return &Apache::lonnet::del($file,$names);
  124: }
  125: 
  126: sub select_recent {
  127:     my ($area,$fieldname,$event)=@_;
  128:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
  129:     my $return="\n<select name='$fieldname'".
  130: 	($event?" onChange='$event'":'').
  131: 	">\n<option value=''>--- ".&mt('Recent')." ---</option>";
  132:     foreach (sort keys %recent) {
  133: 	unless ($_=~/^error\:/) {
  134: 	    $return.="\n<option value='$_'>".
  135: 		&Apache::lonnet::unescape((split(/\&/,$recent{$_}))[1]).
  136: 		'</option>';
  137: 	}
  138:     }
  139:     $return.="\n</select>\n";
  140:     return $return;
  141: }
  142: 
  143: 
  144: =pod
  145: 
  146: =item textbox
  147: 
  148: =cut
  149: 
  150: ##############################################
  151: ##############################################
  152: sub textbox {
  153:     my ($name,$value,$size,$special) = @_;
  154:     $size = 40 if (! defined($size));
  155:     my $Str = '<input type="text" name="'.$name.'" size="'.$size.'" '.
  156:         'value="'.$value.'" '.$special.' />';
  157:     return $Str;
  158: }
  159: 
  160: ##############################################
  161: ##############################################
  162: 
  163: =pod
  164: 
  165: =item checkbox
  166: 
  167: =cut
  168: 
  169: ##############################################
  170: ##############################################
  171: sub checkbox {
  172:     my ($name,$checked,$value) = @_;
  173:     my $Str = '<input type="checkbox" name="'.$name.'" ';
  174:     if (defined($value)) {
  175:         $Str .= 'value="'.$value.'"';
  176:     } 
  177:     if ($checked) {
  178:         $Str .= ' checked="1"';
  179:     }
  180:     $Str .= ' />';
  181:     return $Str;
  182: }
  183: 
  184: ##############################################
  185: ##############################################
  186: 
  187: =pod
  188: 
  189: =item &date_setter
  190: 
  191: &date_setter returns html and javascript for a compact date-setting form.
  192: To retrieve values from it, use &get_date_from_form().
  193: 
  194: Inputs
  195: 
  196: =over 4
  197: 
  198: =item $dname 
  199: 
  200: The name to prepend to the form elements.  
  201: The form elements defined will be dname_year, dname_month, dname_day,
  202: dname_hour, dname_min, and dname_sec.
  203: 
  204: =item $currentvalue
  205: 
  206: The current setting for this time parameter.  A unix format time
  207: (time in seconds since the beginning of Jan 1st, 1970, GMT.  
  208: An undefined value is taken to indicate the value is the current time.
  209: Also, to be explicit, a value of 'now' also indicates the current time.
  210: 
  211: =item $special
  212: 
  213: Additional html/javascript to be associated with each element in
  214: the date_setter.  See lonparmset for example usage.
  215: 
  216: =item $includeempty 
  217: 
  218: =item $state
  219: 
  220: Specifies the initial state of the form elements.  Either 'disabled' or empty.
  221: Defaults to empty, which indiciates the form elements are not disabled. 
  222: 
  223: =back
  224: 
  225: Bugs
  226: 
  227: The method used to restrict user input will fail in the year 2400.
  228: 
  229: =cut
  230: 
  231: ##############################################
  232: ##############################################
  233: sub date_setter {
  234:     my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
  235:         $no_hh_mm_ss) = @_;
  236:     if (! defined($state) || $state ne 'disabled') {
  237:         $state = '';
  238:     }
  239:     if (! defined($no_hh_mm_ss)) {
  240:         $no_hh_mm_ss = 0;
  241:     }
  242:     if (! defined($currentvalue) || $currentvalue eq 'now') {
  243: 	unless ($includeempty) {
  244: 	    $currentvalue = time;
  245: 	} else {
  246: 	    $currentvalue = 0;
  247: 	}
  248:     }
  249:     # other potentially useful values:     wkday,yrday,is_daylight_savings
  250:     my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
  251:     if ($currentvalue) {
  252: 	($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
  253: 	    localtime($currentvalue);
  254: 	$year += 1900;
  255:     }
  256:     my $result = "\n<!-- $dname date setting form -->\n";
  257:     $result .= <<ENDJS;
  258: <script language="Javascript">
  259:     function $dname\_checkday() {
  260:         var day   = document.$formname.$dname\_day.value;
  261:         var month = document.$formname.$dname\_month.value;
  262:         var year  = document.$formname.$dname\_year.value;
  263:         var valid = true;
  264:         if (day < 1) {
  265:             document.$formname.$dname\_day.value = 1;
  266:         } 
  267:         if (day > 31) {
  268:             document.$formname.$dname\_day.value = 31;
  269:         }
  270:         if ((month == 1)  || (month == 3)  || (month == 5)  ||
  271:             (month == 7)  || (month == 8)  || (month == 10) ||
  272:             (month == 12)) {
  273:             if (day > 31) {
  274:                 document.$formname.$dname\_day.value = 31;
  275:                 day = 31;
  276:             }
  277:         } else if (month == 2 ) {
  278:             if ((year % 4 == 0) && (year % 100 != 0)) {
  279:                 if (day > 29) {
  280:                     document.$formname.$dname\_day.value = 29;
  281:                 }
  282:             } else if (day > 29) {
  283:                 document.$formname.$dname\_day.value = 28;
  284:             }
  285:         } else if (day > 30) {
  286:             document.$formname.$dname\_day.value = 30;
  287:         }
  288:     }
  289: 
  290:     function $dname\_disable() {
  291:         document.$formname.$dname\_month.disabled=true;
  292:         document.$formname.$dname\_day.disabled=true;
  293:         document.$formname.$dname\_year.disabled=true;
  294:         document.$formname.$dname\_hour.disabled=true;
  295:         document.$formname.$dname\_minute.disabled=true;
  296:         document.$formname.$dname\_second.disabled=true;
  297:     }
  298: 
  299:     function $dname\_enable() {
  300:         document.$formname.$dname\_month.disabled=false;
  301:         document.$formname.$dname\_day.disabled=false;
  302:         document.$formname.$dname\_year.disabled=false;
  303:         document.$formname.$dname\_hour.disabled=false;
  304:         document.$formname.$dname\_minute.disabled=false;
  305:         document.$formname.$dname\_second.disabled=false;        
  306:     }
  307: 
  308:     function $dname\_opencalendar() {
  309:         if (! document.$formname.$dname\_month.disabled) {
  310:             var calwin=window.open(
  311: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
  312: document.$formname.$dname\_month.value+"&year="+
  313: document.$formname.$dname\_year.value,
  314:              "LONCAPAcal",
  315:               "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
  316:         }
  317: 
  318:     }
  319: </script>
  320: ENDJS
  321:     $result .= "  <nobr><select name=\"$dname\_month\" ".$special.' '.
  322:         $state.' '.
  323:         "onChange=\"javascript:$dname\_checkday()\" >\n";
  324:     # Month
  325:     my @Months = qw/January February  March     April   May      June 
  326:                     July    August    September October November December/;
  327:     # Pad @Months with a bogus value to make indexing easier
  328:     unshift(@Months,'If you can read this an error occurred');
  329:     if ($includeempty) { $result.="<option value=''></option>"; }
  330:     for(my $m = 1;$m <=$#Months;$m++) {
  331:         $result .= "      <option value=\"$m\" ";
  332:         $result .= "selected " if ($m-1 eq $month);
  333:         $result .= "> ".&mt($Months[$m])." </option>\n";
  334:     }
  335:     $result .= "  </select>\n";
  336:     # Day
  337:     $result .= "  <input type=\"text\" name=\"$dname\_day\" ".$state.' '.
  338:             "value=\"$mday\" size=\"3\" ".$special.' '.
  339:             "onChange=\"javascript:$dname\_checkday()\" />\n";
  340:     # Year
  341:     $result .= "  <input type=\"year\" name=\"$dname\_year\" ".$state.' '.
  342:             "value=\"$year\" size=\"5\" ".$special.' '.
  343:             "onChange=\"javascript:$dname\_checkday()\" />\n";
  344:     $result .= "&nbsp;&nbsp;";
  345:     if (! $no_hh_mm_ss) {
  346:         # Hours
  347:         $result .= "  <select name=\"$dname\_hour\" ".$special." ".$state.' '.
  348:             ">\n";
  349:         if ($includeempty) { $result.="<option value=''></option>"; }
  350:         for (my $h = 0;$h<24;$h++) {
  351:             $result .= "      <option value=\"$h\" ";
  352:             $result .= "selected " if (defined($hour) && $hour == $h);
  353:             $result .= "> ";
  354:             my $timest='';
  355:             if ($h == 0) {
  356:                 $timest .= "12 am";
  357:             } elsif($h == 12) {
  358:                 $timest .= "12 noon";
  359:             } elsif($h < 12) {
  360:                 $timest .= "$h am";
  361:             } else {
  362:                 $timest .= $h-12 ." pm";
  363:             }
  364:             $timest=&mt($timest);
  365:             $result .= $timest." </option>\n";
  366:         } 
  367:         $result .= "  </select>\n";
  368:         $result .= "  <input type=\"text\" name=\"$dname\_minute\" ".
  369:             $special.' '.
  370:             $state.' '.
  371:             "value=\"$min\" size=\"3\" /> m\n";
  372:         $result .= "  <input type=\"text\" name=\"$dname\_second\" ".
  373:             $special.' '.
  374:             $state.' '.
  375:             "value=\"$sec\" size=\"3\" /> s\n";
  376:     }
  377:     $result .= "<a href=\"javascript:$dname\_opencalendar()\">".
  378:     &mt('Select Date')."</a></nobr>\n<!-- end $dname date setting form -->\n";
  379:     return $result;
  380: }
  381: 
  382: ##############################################
  383: ##############################################
  384: 
  385: =pod
  386: 
  387: =item &get_date_from_form
  388: 
  389: get_date_from_form retrieves the date specified in an &date_setter form.
  390: 
  391: Inputs:
  392: 
  393: =over 4
  394: 
  395: =item $dname
  396: 
  397: The name passed to &datesetter, which prefixes the form elements.
  398: 
  399: =item $defaulttime
  400: 
  401: The unix time to use as the default in case of poor inputs.
  402: 
  403: =back
  404: 
  405: Returns: Unix time represented in the form.
  406: 
  407: =cut
  408: 
  409: ##############################################
  410: ##############################################
  411: sub get_date_from_form {
  412:     my ($dname) = @_;
  413:     my ($sec,$min,$hour,$day,$month,$year);
  414:     #
  415:     if (defined($ENV{'form.'.$dname.'_second'})) {
  416:         my $tmpsec = $ENV{'form.'.$dname.'_second'};
  417:         if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
  418:             $sec = $tmpsec;
  419:         }
  420: 	if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
  421:     } else {
  422:         $sec = 0;
  423:     }
  424:     if (defined($ENV{'form.'.$dname.'_minute'})) {
  425:         my $tmpmin = $ENV{'form.'.$dname.'_minute'};
  426:         if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
  427:             $min = $tmpmin;
  428:         }
  429: 	if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
  430:     } else {
  431:         $min = 0;
  432:     }
  433:     if (defined($ENV{'form.'.$dname.'_hour'})) {
  434:         my $tmphour = $ENV{'form.'.$dname.'_hour'};
  435:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
  436:             $hour = $tmphour;
  437:         }
  438:     } else {
  439:         $hour = 0;
  440:     }
  441:     if (defined($ENV{'form.'.$dname.'_day'})) {
  442:         my $tmpday = $ENV{'form.'.$dname.'_day'};
  443:         if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
  444:             $day = $tmpday;
  445:         }
  446:     }
  447:     if (defined($ENV{'form.'.$dname.'_month'})) {
  448:         my $tmpmonth = $ENV{'form.'.$dname.'_month'};
  449:         if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
  450:             $month = $tmpmonth - 1;
  451:         }
  452:     }
  453:     if (defined($ENV{'form.'.$dname.'_year'})) {
  454:         my $tmpyear = $ENV{'form.'.$dname.'_year'};
  455:         if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
  456:             $year = $tmpyear - 1900;
  457:         }
  458:     }
  459:     if (($year<70) || ($year>137)) { return undef; }
  460:     if (defined($sec) && defined($min)   && defined($hour) &&
  461:         defined($day) && defined($month) && defined($year) &&
  462:         eval(&timelocal($sec,$min,$hour,$day,$month,$year))) {
  463:         return &timelocal($sec,$min,$hour,$day,$month,$year);
  464:     } else {
  465:         return undef;
  466:     }
  467: }
  468: 
  469: ##############################################
  470: ##############################################
  471: 
  472: =pod
  473: 
  474: =item &pjump_javascript_definition()
  475: 
  476: Returns javascript defining the 'pjump' function, which opens up a
  477: parameter setting wizard.
  478: 
  479: =cut
  480: 
  481: ##############################################
  482: ##############################################
  483: sub pjump_javascript_definition {
  484:     my $Str = <<END;
  485:     function pjump(type,dis,value,marker,ret,call) {
  486:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
  487:                  +"&value="+escape(value)+"&marker="+escape(marker)
  488:                  +"&return="+escape(ret)
  489:                  +"&call="+escape(call)+"&name="+escape(dis),"LONCAPAparms",
  490:                  "height=350,width=350,scrollbars=no,menubar=no");
  491:     }
  492: END
  493:     return $Str;
  494: }
  495: 
  496: ##############################################
  497: ##############################################
  498: 
  499: =pod
  500: 
  501: =item &javascript_nothing()
  502: 
  503: Return an appropriate null for the users browser.  This is used
  504: as the first arguement for window.open calls when you want a blank
  505: window that you can then write to.
  506: 
  507: =cut
  508: 
  509: ##############################################
  510: ##############################################
  511: sub javascript_nothing {
  512:     # mozilla and other browsers work with "''", but IE on mac does not.
  513:     my $nothing = "''";
  514:     my $user_browser;
  515:     my $user_os;
  516:     $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
  517:     $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
  518:     if (! defined($user_browser) || ! defined($user_os)) {
  519:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  520:                            &Apache::loncommon::decode_user_agent();
  521:     }
  522:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
  523:         $nothing = "'javascript:void(0);'";
  524:     }
  525:     return $nothing;
  526: }
  527: 
  528: ##############################################
  529: ##############################################
  530: sub javascript_docopen {
  531:     # safari does not understand document.open() and loads "text/html"
  532:     my $nothing = "''";
  533:     my $user_browser;
  534:     my $user_os;
  535:     $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
  536:     $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
  537:     if (! defined($user_browser) || ! defined($user_os)) {
  538:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  539:                            &Apache::loncommon::decode_user_agent();
  540:     }
  541:     if ($user_browser eq 'safari' && $user_os =~ 'mac') {
  542:         $nothing = "document.clear()";
  543:     } else {
  544: 	$nothing = "document.open('text/html','replace')";
  545:     }
  546:     return $nothing;
  547: }
  548: 
  549: 
  550: ##############################################
  551: ##############################################
  552: 
  553: =pod
  554: 
  555: =item &StatusOptions()
  556: 
  557: Returns html for a selection box which allows the user to choose the
  558: enrollment status of students.  The selection box name is 'Status'.
  559: 
  560: Inputs:
  561: 
  562: $status: the currently selected status.  If undefined the value of
  563: $ENV{'form.Status'} is taken.  If that is undefined, a value of 'Active'
  564: is used.
  565: 
  566: $formname: The name of the form.  If defined the onchange attribute of
  567: the selection box is set to document.$formname.submit().
  568: 
  569: $size: the size (number of lines) of the selection box.
  570: 
  571: $onchange: javascript to use when the value is changed.  Enclosed in 
  572: double quotes, ""s, not single quotes.
  573: 
  574: Returns: a perl string as described.
  575: 
  576: =cut
  577: 
  578: ##############################################
  579: ##############################################
  580: sub StatusOptions {
  581:     my ($status, $formName,$size,$onchange)=@_;
  582:     $size = 1 if (!defined($size));
  583:     if (! defined($status)) {
  584:         $status = 'Active';
  585:         $status = $ENV{'form.Status'} if (exists($ENV{'form.Status'}));
  586:     }
  587: 
  588:     my $OpSel1 = '';
  589:     my $OpSel2 = '';
  590:     my $OpSel3 = '';
  591: 
  592:     if($status eq 'Any')         { $OpSel3 = ' selected'; }
  593:     elsif($status eq 'Expired' ) { $OpSel2 = ' selected'; }
  594:     else                         { $OpSel1 = ' selected'; }
  595: 
  596:     my $Str = '';
  597:     $Str .= '<select name="Status"';
  598:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
  599:         $Str .= ' onchange="document.'.$formName.'.submit()"';
  600:     }
  601:     if (defined($onchange)) {
  602:         $Str .= ' onchange="'.$onchange.'"';
  603:     }
  604:     $Str .= ' size="'.$size.'" ';
  605:     $Str .= '>'."\n";
  606:     $Str .= '<option value="Active" '.$OpSel1.'>'.
  607:         &mt('Currently Enrolled').'</option>'."\n";
  608:     $Str .= '<option value="Expired" '.$OpSel2.'>'.
  609:         &mt('Previously Enrolled').'</option>'."\n";
  610:     $Str .= '<option value="Any" '.$OpSel3.'>'.
  611:         &mt('Any Enrollment Status').'</option>'."\n";
  612:     $Str .= '</select>'."\n";
  613: }
  614: 
  615: ########################################################
  616: ########################################################
  617: 
  618: =pod
  619: 
  620: =item Progess Window Handling Routines
  621: 
  622: These routines handle the creation, update, increment, and closure of 
  623: progress windows.  The progress window reports to the user the number
  624: of items completed and an estimate of the time required to complete the rest.
  625: 
  626: =over 4
  627: 
  628: 
  629: =item &Create_PrgWin
  630: 
  631: Writes javascript to the client to open a progress window and returns a
  632: data structure used for bookkeeping.
  633: 
  634: Inputs
  635: 
  636: =over 4
  637: 
  638: =item $r Apache request
  639: 
  640: =item $title The title of the progress window
  641: 
  642: =item $heading A description (usually 1 line) of the process being initiated.
  643: 
  644: =item $number_to_do The total number of items being processed.
  645: 
  646: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
  647:        specified)
  648: 
  649: =item $width Specify the width in charaters of the input field.
  650: 
  651: =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
  652: 
  653: =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 
  654: 
  655: =back
  656: 
  657: Returns a hash containing the progress state data structure.
  658: 
  659: 
  660: =item &Update_PrgWin
  661: 
  662: Updates the text in the progress indicator.  Does not increment the count.
  663: See &Increment_PrgWin.
  664: 
  665: Inputs:
  666: 
  667: =over 4
  668: 
  669: =item $r Apache request
  670: 
  671: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
  672: 
  673: =item $displaystring The string to write to the status indicator
  674: 
  675: =back
  676: 
  677: Returns: none
  678: 
  679: 
  680: =item Increment_PrgWin
  681: 
  682: Increment the count of items completed for the progress window by 1.  
  683: 
  684: Inputs:
  685: 
  686: =over 4
  687: 
  688: =item $r Apache request
  689: 
  690: =item $prog_state Pointer to the data structure returned by Create_PrgWin
  691: 
  692: =item $extraInfo A description of the items being iterated over.  Typically
  693: 'student'.
  694: 
  695: =back
  696: 
  697: Returns: none
  698: 
  699: 
  700: =item Close_PrgWin
  701: 
  702: Closes the progress window.
  703: 
  704: Inputs:
  705: 
  706: =over 4 
  707: 
  708: =item $r Apache request
  709: 
  710: =item $prog_state Pointer to the data structure returned by Create_PrgWin
  711: 
  712: =back
  713: 
  714: Returns: none
  715: 
  716: =back
  717: 
  718: =cut
  719: 
  720: ########################################################
  721: ########################################################
  722: 
  723: my $uniq=0;
  724: sub get_uniq_name {
  725:     $uniq++;
  726:     return 'uniquename'.$uniq;
  727: }
  728: 
  729: # Create progress
  730: sub Create_PrgWin {
  731:     my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
  732: 	$inputname)=@_;
  733:     if (!defined($type)) { $type='popup'; }
  734:     if (!defined($width)) { $width=55; }
  735:     my %prog_state;
  736:     $prog_state{'type'}=$type;
  737:     if ($type eq 'popup') {
  738: 	$prog_state{'window'}='popwin';
  739: 	#the whole function called through timeout is due to issues
  740: 	#in mozilla Read BUG #2665 if you want to know the whole story
  741: 	&r_print($r,'<script>'.
  742:         "var popwin;
  743:          function openpopwin () {
  744:          popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
  745:         "popwin.document.writeln(\'<html><head><title>$title</title></head>".
  746: 	      "<body bgcolor=\"#88DDFF\">".
  747:               "<h4>$heading</h4>".
  748:               "<form name=popremain>".
  749:               '<input type="text" size="'.$width.'" name="remaining" value="'.
  750: 	      &mt('Starting').'"></form>'.
  751:               "</body></html>\');".
  752:         "popwin.document.close();}".
  753:         "\nwindow.setTimeout(openpopwin,0)</script>");
  754: 	$prog_state{'formname'}='popremain';
  755: 	$prog_state{'inputname'}="remaining";
  756:     } elsif ($type eq 'inline') {
  757: 	$prog_state{'window'}='window';
  758: 	if (!$formname) {
  759: 	    $prog_state{'formname'}=&get_uniq_name();
  760: 	    &r_print($r,'<form name="'.$prog_state{'formname'}.'">');
  761: 	} else {
  762: 	    $prog_state{'formname'}=$formname;
  763: 	}
  764: 	if (!$inputname) {
  765: 	    $prog_state{'inputname'}=&get_uniq_name();
  766: 	    &r_print($r,$heading.' <input type="text" name="'.$prog_state{'inputname'}.
  767: 		     '" size="'.$width.'" />');
  768: 	} else {
  769: 	    $prog_state{'inputname'}=$inputname;
  770: 	    
  771: 	}
  772: 	if (!$formname) { &r_print($r,'</form>'); }
  773: 	&Update_PrgWin($r,\%prog_state,&mt('Starting'));
  774:     }
  775: 
  776:     $prog_state{'done'}=0;
  777:     $prog_state{'firststart'}=&Time::HiRes::time();
  778:     $prog_state{'laststart'}=&Time::HiRes::time();
  779:     $prog_state{'max'}=$number_to_do;
  780:     
  781:     return %prog_state;
  782: }
  783: 
  784: # update progress
  785: sub Update_PrgWin {
  786:     my ($r,$prog_state,$displayString)=@_;
  787:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
  788: 	     $$prog_state{'formname'}.'.'.
  789: 	     $$prog_state{'inputname'}.'.value="'.
  790: 	     $displayString.'";</script>');
  791:     $$prog_state{'laststart'}=&Time::HiRes::time();
  792: }
  793: 
  794: # increment progress state
  795: sub Increment_PrgWin {
  796:     my ($r,$prog_state,$extraInfo)=@_;
  797:     $$prog_state{'done'}++;
  798:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
  799:         $$prog_state{'done'} *
  800: 	($$prog_state{'max'}-$$prog_state{'done'});
  801:     $time_est = int($time_est);
  802:     #
  803:     my $min = int($time_est/60);
  804:     my $sec = $time_est % 60;
  805:     # 
  806:     my $str;
  807:     if ($min == 1 && $sec > 1) {
  808:         $str = '[_2] seconds';
  809:     } elsif ($min == 1 && $sec < 2) {
  810:         $str = '1 minute';
  811:     } elsif ($min < 10 && $sec > 1) {
  812:         $str = '[_1] minutes, [_2] seconds';
  813:     } elsif ($min >= 10 || $sec < 2) {
  814:         $str = '[_1] minutes';
  815:     }
  816:     $time_est = &mt($str,$min,$sec);
  817:     #
  818:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
  819:     if ($lasttime > 9) {
  820:         $lasttime = int($lasttime);
  821:     } elsif ($lasttime < 0.01) {
  822:         $lasttime = 0;
  823:     } else {
  824:         $lasttime = sprintf("%3.2f",$lasttime);
  825:     }
  826:     if ($lasttime == 1) {
  827:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
  828:     } else {
  829:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
  830:     }
  831:     #
  832:     my $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
  833:     my $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
  834:     if (! defined($user_browser) || ! defined($user_os)) {
  835:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  836:                            &Apache::loncommon::decode_user_agent();
  837:     }
  838:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
  839:         $lasttime = '';
  840:     }
  841:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
  842: 	     $$prog_state{'formname'}.'.'.
  843: 	     $$prog_state{'inputname'}.'.value="'.
  844: 	     $$prog_state{'done'}.'/'.$$prog_state{'max'}.
  845: 	     ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
  846:     $$prog_state{'laststart'}=&Time::HiRes::time();
  847: }
  848: 
  849: # close Progress Line
  850: sub Close_PrgWin {
  851:     my ($r,$prog_state)=@_;
  852:     if ($$prog_state{'type'} eq 'popup') {
  853: 	&r_print($r,'<script>popwin.close()</script>'."\n");
  854:     } elsif ($$prog_state{'type'} eq 'inline') {
  855: 	&Update_PrgWin($r,$prog_state,&mt('Done'));
  856:     }
  857:     undef(%$prog_state);
  858: }
  859: 
  860: sub r_print {
  861:     my ($r,$to_print)=@_;
  862:     if ($r) {
  863: 	$r->print($to_print);
  864: 	$r->rflush();
  865:     } else {
  866: 	print($to_print);
  867:     }
  868: }
  869: 
  870: # ------------------------------------------------------- Puts directory header
  871: 
  872: sub crumbs {
  873:     my ($uri,$target,$prefix,$form,$size,$noformat)=@_;
  874:     if (! defined($size)) {
  875:         $size = '+2';
  876:     }
  877:     my $output='';
  878:     unless ($noformat) { $output.='<br /><tt><b>'; }
  879:     $output.='<font size="'.$size.'">'.$prefix.'/';
  880:     if ($ENV{'user.adv'}) {
  881: 	my $path=$prefix.'/';
  882: 	foreach (split('/',$uri)) {
  883: 	    unless ($_) { next; }
  884: 	    $path.=$_;
  885: 	    unless ($path eq $uri) { $path.='/'; }
  886: 	    my $linkpath=$path;
  887: 	    if ($form) {
  888: 		$linkpath="javascript:$form.action='$path';$form.submit();";
  889: 	    }
  890: 	    $output.='<a href="'.$linkpath.'"'.($target?' target="'.$target.'"':'').'>'.$_.'</a>/';
  891: 	}
  892:     } else {
  893: 	$output.=$uri;
  894:     }
  895:     unless ($uri=~/\/$/) { $output=~s/\/$//; }
  896:     return $output.'</font>'.($noformat?'':'</b></tt><br />');
  897: }
  898: 
  899: # --------------------- A function that generates a window for the spellchecker
  900: 
  901: sub spellheader {
  902:     my $nothing = &javascript_nothing();
  903:     return (<<ENDCHECK);
  904: <script type="text/javascript"> 
  905: // BEGIN LON-CAPA Internal
  906: var checkwin;
  907: 
  908: function spellcheckerwindow() {
  909:     checkwin=window.open($nothing,'spellcheckwin','height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
  910:     checkwin.document.writeln('<html><body bgcolor="#DDDDDD"><form name="spellcheckform" action="/adm/spellcheck" method="post"><input type="hidden" name="text" value="" /></form></body></html>');
  911:     checkwin.document.close();
  912: }
  913: // END LON-CAPA Internal
  914: </script>
  915: ENDCHECK
  916: }
  917: 
  918: # ---------------------------------- Generate link to spell checker for a field
  919: 
  920: sub spelllink {
  921:     my ($form,$field)=@_;
  922:     my $linktext=&mt('Check Spelling');
  923:     return (<<ENDLINK);
  924: <a href="javascript:if (typeof(document.$form.onsubmit)!='undefined') { document.$form.onsubmit();};spellcheckerwindow();checkwin.document.forms.spellcheckform.text.value=this.document.forms.$form.$field.value;checkwin.document.forms.spellcheckform.submit();">$linktext</a>
  925: ENDLINK
  926: }
  927: 
  928: # ------------------------------------------------- Output headers for HTMLArea
  929: 
  930: sub htmlareaheaders {
  931:     if (&htmlareablocked()) { return ''; }
  932:     unless (&htmlareabrowser()) { return ''; }
  933:     my $lang='en';
  934:     if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
  935: 	$lang=&mt('htmlarea_lang');
  936:     }
  937:     return (<<ENDHEADERS);
  938: <script type="text/javascript">
  939: _editor_url='/htmlarea/';
  940: _editor_lang='$lang';
  941: </script>
  942: <script type="text/javascript" src="/htmlarea/htmlarea.js"></script>
  943: ENDHEADERS
  944: }
  945: 
  946: # ------------------------------------------------- Activate additional buttons
  947: 
  948: sub htmlareaaddbuttons {
  949:     if (&htmlareablocked()) { return ''; }
  950:     unless (&htmlareabrowser()) { return ''; }
  951:     return (<<ENDADDBUTTON);
  952:     var config=new HTMLArea.Config();
  953:     config.registerButton('ed_math','LaTeX Inline',
  954: 			  '/htmlarea/images/ed_math.gif',false,
  955: 			    function(editor,id) {
  956: 			      editor.surroundHTML('&nbsp;<m>\$','\$</m>&nbsp;');
  957: 			    }
  958: 			  );
  959:     config.registerButton('ed_math_eqn','LaTeX Equation',
  960: 			  '/htmlarea/images/ed_math_eqn.gif',false,
  961: 			    function(editor,id) {
  962: 			      editor.surroundHTML(
  963: 				     '&nbsp;\\n<center><m>\\\\[','\\\\]</m></center>\\n&nbsp;');
  964: 			    }
  965: 			  );
  966:     config.toolbar.push(['ed_math','ed_math_eqn']);
  967: ENDADDBUTTON
  968: }
  969: 
  970: # ----------------------------------------------------------------- Preferences
  971: 
  972: sub disablelink {
  973:     my @fields=@_;
  974:     if (defined($#fields)) {
  975: 	unless ($#fields>=0) { return ''; }
  976:     }
  977:     return '<a href="/adm/preferences?action=set_wysiwyg&wysiwyg=off&returnurl='.&Apache::lonnet::escape($ENV{'REQUEST_URI'}).'">'.&mt('Disable WYSIWYG Editor').'</a>';
  978: }
  979: 
  980: sub enablelink {
  981:     my @fields=@_;
  982:     if (defined($#fields)) {
  983: 	unless ($#fields>=0) { return ''; }
  984:     }
  985:     return '<a href="/adm/preferences?action=set_wysiwyg&wysiwyg=on&returnurl='.&Apache::lonnet::escape($ENV{'REQUEST_URI'}).'">'.&mt('Enable WYSIWYG Editor').'</a>';
  986: }
  987: 
  988: # ----------------------------------------- Script to activate only some fields
  989: 
  990: sub htmlareaselectactive {
  991:     my @fields=@_;
  992:     unless (&htmlareabrowser()) { return ''; }
  993:     if (&htmlareablocked()) { return '<br />'.&enablelink(@fields); }
  994:     my $output='<script type="text/javascript" defer="1">'.
  995: 	&htmlareaaddbuttons();
  996:     foreach(@fields) {
  997: 	$output.="\nHTMLArea.replace('$_',config);";
  998:     }
  999:     $output.="\nwindow.status='Activated Editfields';\n</script><br />".
 1000: 	&disablelink(@fields);
 1001:     return $output;
 1002: }
 1003: 
 1004: # --------------------------------------------------------------------- Blocked
 1005: 
 1006: sub htmlareablocked {
 1007:     unless ($ENV{'environment.wysiwygeditor'} eq 'on') { return 1; }
 1008:     return 0;
 1009: }
 1010: 
 1011: # ---------------------------------------- Browser capable of running HTMLArea?
 1012: 
 1013: sub htmlareabrowser {
 1014:     return 1;
 1015: }
 1016: 
 1017: ############################################################
 1018: ############################################################
 1019: 
 1020: =pod
 1021: 
 1022: =item breadcrumbs
 1023: 
 1024: Compiles the previously registered breadcrumbs into an series of links.
 1025: FAQ and BUG links will be placed on the left side of the table if they
 1026: are defined for the last registered breadcrumb.  
 1027: Additionally supports a 'component', which will be displayed on the
 1028: right side of the table (without a link).
 1029: A link to help for the component will be included if one is specified.
 1030: 
 1031: All inputs can be undef without problems.
 1032: 
 1033: Inputs: $color (the background color of the table returned),
 1034:         $component (the large text on the right side of the table),
 1035:         $component_help
 1036:         $function (role to get colors from)
 1037:         $domain   (domian of role)
 1038:         $menulink (boolean, controls whether to include a link to /adm/menu)
 1039: 
 1040: Returns a string containing breadcrumbs for the current page.
 1041: 
 1042: =item clear_breadcrumbs
 1043: 
 1044: Clears the previously stored breadcrumbs.
 1045: 
 1046: =item add_breadcrumb
 1047: 
 1048: Pushes a breadcrumb on the stack of crumbs.
 1049: 
 1050: input: $breadcrumb, a hash reference.  The keys 'href','title', and 'text'
 1051: are required.  If present the keys 'faq' and 'bug' will be used to provide
 1052: links to the FAQ and bug sites.
 1053: 
 1054: returns: nothing    
 1055: 
 1056: =cut
 1057: 
 1058: ############################################################
 1059: ############################################################
 1060: {
 1061:     my @Crumbs;
 1062:     
 1063:     sub breadcrumbs {
 1064:         my ($color,$component,$component_help,$function,$domain,$menulink,
 1065: 	    $helplink) = @_;
 1066:         if (! defined($color)) {
 1067:             if (! defined($function)) {
 1068:                 $function = &Apache::loncommon::get_users_function();
 1069:             }
 1070:             $color = &Apache::loncommon::designparm($function.'.tabbg',
 1071:                                                     $domain);
 1072:         }
 1073:         #
 1074:         my $Str = "\n".
 1075:             '<table width="100%" border="0" cellpadding="0" cellspacing="0">'.
 1076:             '<tr><td bgcolor="'.$color.'">'.
 1077:             '<font size="-1">';
 1078:         #
 1079:         # Make the faq and bug data cascade
 1080:         my $faq = '';
 1081:         my $bug = '';
 1082:         # The last breadcrumb does not have a link, so handle it separately.
 1083:         my $last = pop(@Crumbs);
 1084:         #
 1085:         # The first one should be the course or a menu link
 1086: 	if (!defined($menulink)) { $menulink=1; }
 1087:         if ($menulink) {
 1088:             my $description = 'Menu';
 1089:             if (exists($ENV{'request.course.id'}) && 
 1090:                 $ENV{'request.course.id'} ne '') {
 1091:                 $description = 
 1092:                     $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
 1093:             }
 1094:             unshift(@Crumbs,{
 1095:                     href   =>'/adm/menu',
 1096:                     title  =>'Go to main menu',
 1097:                     target =>'_top',
 1098:                     text   =>$description,
 1099:                 });
 1100:         }
 1101:         my $links .= 
 1102:             join('-&gt;',
 1103:                  map {
 1104:                      $faq = $_->{'faq'} if (exists($_->{'faq'}));
 1105:                      $bug = $_->{'bug'} if (exists($_->{'bug'}));
 1106:                      my $result = '<a href="'.$_->{'href'}.'" ';
 1107:                      if (defined($_->{'target'}) && $_->{'target'} ne '') {
 1108:                          $result .= 'target="'.$_->{'target'}.'" ';
 1109:                      }
 1110:                      $result .='title="'.&mt($_->{'title'}).'">'.
 1111:                          &mt($_->{'text'}).'</a>';
 1112:                      $result;
 1113:                      } @Crumbs
 1114:                  );
 1115:         $links .= '-&gt;' if ($links ne '');
 1116:         $links .= '<b>'.&mt($last->{'text'}).'</b>';
 1117:         #
 1118:         my $icons = '';
 1119:         $faq = $last->{'faq'} if (exists($last->{'faq'}));
 1120:         $bug = $last->{'bug'} if (exists($last->{'bug'}));
 1121: #        if ($faq ne '') {
 1122: #            $icons .= &Apache::loncommon::help_open_faq($faq);
 1123: #        }
 1124: #        if ($bug ne '') {
 1125: #            $icons .= &Apache::loncommon::help_open_bug($bug);
 1126: #        }
 1127: 	if ($helplink ne 'nohelp') {
 1128: 	    $icons .= &Apache::loncommon::help_open_menu($color,$component,$component_help,$function,$faq,$bug);
 1129: 	}
 1130:         if ($icons ne '') {
 1131:             $Str .= $icons.'&nbsp;';
 1132:         }
 1133:         #
 1134:         $Str .= $links.'</font></td>';
 1135:         #
 1136:         if (defined($component)) {
 1137:             $Str .= '<td align="right" bgcolor="'.$color.'">'.
 1138:                 '<font size="+1">'.&mt($component).'</font></td>';
 1139:         }
 1140:         $Str .= '</tr></table>'."\n";
 1141:         #
 1142:         # Return the @Crumbs stack to what we started with
 1143:         push(@Crumbs,$last);
 1144:         shift(@Crumbs);
 1145:         #
 1146:         return $Str;
 1147:     }
 1148: 
 1149:     sub clear_breadcrumbs {
 1150:         undef(@Crumbs);
 1151:     }
 1152: 
 1153:     sub add_breadcrumb {
 1154:         push (@Crumbs,@_);
 1155:     }
 1156: 
 1157: } # End of scope for @Crumbs
 1158: 
 1159: ############################################################
 1160: ############################################################
 1161: 
 1162: 
 1163: 1;
 1164: 
 1165: __END__

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