File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.93: download - view: text, annotated - select for diffs
Tue Oct 12 23:26:48 2004 UTC (19 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- except for the <nobr> and the selected="on" Edit mode on a homework problem is now xhtml 1.0 transtional compliant

    1: # The LearningOnline Network with CAPA
    2: # a pile of common html routines
    3: #
    4: # $Id: lonhtmlcommon.pm,v 1.93 2004/10/12 23:26:48 albertel 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 == 0 && $sec > 1) {
  808:         $str = '[_2] seconds';
  809:     } elsif ($min == 1 && $sec > 1) {
  810:         $str = '1 minute [_2] seconds';
  811:     } elsif ($min == 1 && $sec < 2) {
  812:         $str = '1 minute';
  813:     } elsif ($min < 10 && $sec > 1) {
  814:         $str = '[_1] minutes, [_2] seconds';
  815:     } elsif ($min >= 10 || $sec < 2) {
  816:         $str = '[_1] minutes';
  817:     }
  818:     $time_est = &mt($str,$min,$sec);
  819:     #
  820:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
  821:     if ($lasttime > 9) {
  822:         $lasttime = int($lasttime);
  823:     } elsif ($lasttime < 0.01) {
  824:         $lasttime = 0;
  825:     } else {
  826:         $lasttime = sprintf("%3.2f",$lasttime);
  827:     }
  828:     if ($lasttime == 1) {
  829:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
  830:     } else {
  831:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
  832:     }
  833:     #
  834:     my $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
  835:     my $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
  836:     if (! defined($user_browser) || ! defined($user_os)) {
  837:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  838:                            &Apache::loncommon::decode_user_agent();
  839:     }
  840:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
  841:         $lasttime = '';
  842:     }
  843:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
  844: 	     $$prog_state{'formname'}.'.'.
  845: 	     $$prog_state{'inputname'}.'.value="'.
  846: 	     $$prog_state{'done'}.'/'.$$prog_state{'max'}.
  847: 	     ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
  848:     $$prog_state{'laststart'}=&Time::HiRes::time();
  849: }
  850: 
  851: # close Progress Line
  852: sub Close_PrgWin {
  853:     my ($r,$prog_state)=@_;
  854:     if ($$prog_state{'type'} eq 'popup') {
  855: 	&r_print($r,'<script>popwin.close()</script>'."\n");
  856:     } elsif ($$prog_state{'type'} eq 'inline') {
  857: 	&Update_PrgWin($r,$prog_state,&mt('Done'));
  858:     }
  859:     undef(%$prog_state);
  860: }
  861: 
  862: sub r_print {
  863:     my ($r,$to_print)=@_;
  864:     if ($r) {
  865: 	$r->print($to_print);
  866: 	$r->rflush();
  867:     } else {
  868: 	print($to_print);
  869:     }
  870: }
  871: 
  872: # ------------------------------------------------------- Puts directory header
  873: 
  874: sub crumbs {
  875:     my ($uri,$target,$prefix,$form,$size,$noformat)=@_;
  876:     if (! defined($size)) {
  877:         $size = '+2';
  878:     }
  879:     my $output='';
  880:     unless ($noformat) { $output.='<br /><tt><b>'; }
  881:     $output.='<font size="'.$size.'">'.$prefix.'/';
  882:     if ($ENV{'user.adv'}) {
  883: 	my $path=$prefix.'/';
  884: 	foreach (split('/',$uri)) {
  885: 	    unless ($_) { next; }
  886: 	    $path.=$_;
  887: 	    unless ($path eq $uri) { $path.='/'; }
  888: 	    my $linkpath=$path;
  889: 	    if ($form) {
  890: 		$linkpath="javascript:$form.action='$path';$form.submit();";
  891: 	    }
  892: 	    $output.='<a href="'.$linkpath.'"'.($target?' target="'.$target.'"':'').'>'.$_.'</a>/';
  893: 	}
  894:     } else {
  895: 	$output.=$uri;
  896:     }
  897:     unless ($uri=~/\/$/) { $output=~s/\/$//; }
  898:     return $output.'</font>'.($noformat?'':'</b></tt><br />');
  899: }
  900: 
  901: # --------------------- A function that generates a window for the spellchecker
  902: 
  903: sub spellheader {
  904:     my $nothing = &javascript_nothing();
  905:     return (<<ENDCHECK);
  906: <script type="text/javascript"> 
  907: //<!-- BEGIN LON-CAPA Internal
  908: var checkwin;
  909: 
  910: function spellcheckerwindow() {
  911:     checkwin=window.open($nothing,'spellcheckwin','height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
  912:     checkwin.document.writeln('<html><body bgcolor="#DDDDDD"><form name="spellcheckform" action="/adm/spellcheck" method="post"><input type="hidden" name="text" value="" /></form></body></html>');
  913:     checkwin.document.close();
  914: }
  915: // END LON-CAPA Internal -->
  916: </script>
  917: ENDCHECK
  918: }
  919: 
  920: # ---------------------------------- Generate link to spell checker for a field
  921: 
  922: sub spelllink {
  923:     my ($form,$field)=@_;
  924:     my $linktext=&mt('Check Spelling');
  925:     return (<<ENDLINK);
  926: <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>
  927: ENDLINK
  928: }
  929: 
  930: # ------------------------------------------------- Output headers for HTMLArea
  931: 
  932: sub htmlareaheaders {
  933:     if (&htmlareablocked()) { return ''; }
  934:     unless (&htmlareabrowser()) { return ''; }
  935:     my $lang='en';
  936:     if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
  937: 	$lang=&mt('htmlarea_lang');
  938:     }
  939:     return (<<ENDHEADERS);
  940: <script type="text/javascript">
  941: _editor_url='/htmlarea/';
  942: _editor_lang='$lang';
  943: </script>
  944: <script type="text/javascript" src="/htmlarea/htmlarea.js"></script>
  945: ENDHEADERS
  946: }
  947: 
  948: # ------------------------------------------------- Activate additional buttons
  949: 
  950: sub htmlareaaddbuttons {
  951:     if (&htmlareablocked()) { return ''; }
  952:     unless (&htmlareabrowser()) { return ''; }
  953:     return (<<ENDADDBUTTON);
  954:     var config=new HTMLArea.Config();
  955:     config.registerButton('ed_math','LaTeX Inline',
  956: 			  '/htmlarea/images/ed_math.gif',false,
  957: 			    function(editor,id) {
  958: 			      editor.surroundHTML('&nbsp;<m>\$','\$</m>&nbsp;');
  959: 			    }
  960: 			  );
  961:     config.registerButton('ed_math_eqn','LaTeX Equation',
  962: 			  '/htmlarea/images/ed_math_eqn.gif',false,
  963: 			    function(editor,id) {
  964: 			      editor.surroundHTML(
  965: 				     '&nbsp;\\n<center><m>\\\\[','\\\\]</m></center>\\n&nbsp;');
  966: 			    }
  967: 			  );
  968:     config.toolbar.push(['ed_math','ed_math_eqn']);
  969: ENDADDBUTTON
  970: }
  971: 
  972: # ----------------------------------------------------------------- Preferences
  973: 
  974: sub disablelink {
  975:     my @fields=@_;
  976:     if (defined($#fields)) {
  977: 	unless ($#fields>=0) { return ''; }
  978:     }
  979:     return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=off&returnurl=','<>&"').&Apache::lonnet::escape($ENV{'REQUEST_URI'}).'">'.&mt('Disable WYSIWYG Editor').'</a>';
  980: }
  981: 
  982: sub enablelink {
  983:     my @fields=@_;
  984:     if (defined($#fields)) {
  985: 	unless ($#fields>=0) { return ''; }
  986:     }
  987:     return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=on&returnurl=','<>&"').&Apache::lonnet::escape($ENV{'REQUEST_URI'}).'">'.&mt('Enable WYSIWYG Editor').'</a>';
  988: }
  989: 
  990: # ----------------------------------------- Script to activate only some fields
  991: 
  992: sub htmlareaselectactive {
  993:     my @fields=@_;
  994:     unless (&htmlareabrowser()) { return ''; }
  995:     if (&htmlareablocked()) { return '<br />'.&enablelink(@fields); }
  996:     my $output='<script type="text/javascript" defer="1">'.
  997: 	&htmlareaaddbuttons();
  998:     foreach(@fields) {
  999: 	$output.="\nHTMLArea.replace('$_',config);";
 1000:     }
 1001:     $output.="\nwindow.status='Activated Editfields';\n</script><br />".
 1002: 	&disablelink(@fields);
 1003:     return $output;
 1004: }
 1005: 
 1006: # --------------------------------------------------------------------- Blocked
 1007: 
 1008: sub htmlareablocked {
 1009:     unless ($ENV{'environment.wysiwygeditor'} eq 'on') { return 1; }
 1010:     return 0;
 1011: }
 1012: 
 1013: # ---------------------------------------- Browser capable of running HTMLArea?
 1014: 
 1015: sub htmlareabrowser {
 1016:     return 1;
 1017: }
 1018: 
 1019: ############################################################
 1020: ############################################################
 1021: 
 1022: =pod
 1023: 
 1024: =item breadcrumbs
 1025: 
 1026: Compiles the previously registered breadcrumbs into an series of links.
 1027: FAQ and BUG links will be placed on the left side of the table if they
 1028: are defined for the last registered breadcrumb.  
 1029: Additionally supports a 'component', which will be displayed on the
 1030: right side of the table (without a link).
 1031: A link to help for the component will be included if one is specified.
 1032: 
 1033: All inputs can be undef without problems.
 1034: 
 1035: Inputs: $color (the background color of the table returned),
 1036:         $component (the large text on the right side of the table),
 1037:         $component_help
 1038:         $function (role to get colors from)
 1039:         $domain   (domian of role)
 1040:         $menulink (boolean, controls whether to include a link to /adm/menu)
 1041: 
 1042: Returns a string containing breadcrumbs for the current page.
 1043: 
 1044: =item clear_breadcrumbs
 1045: 
 1046: Clears the previously stored breadcrumbs.
 1047: 
 1048: =item add_breadcrumb
 1049: 
 1050: Pushes a breadcrumb on the stack of crumbs.
 1051: 
 1052: input: $breadcrumb, a hash reference.  The keys 'href','title', and 'text'
 1053: are required.  If present the keys 'faq' and 'bug' will be used to provide
 1054: links to the FAQ and bug sites.
 1055: 
 1056: returns: nothing    
 1057: 
 1058: =cut
 1059: 
 1060: ############################################################
 1061: ############################################################
 1062: {
 1063:     my @Crumbs;
 1064:     
 1065:     sub breadcrumbs {
 1066:         my ($color,$component,$component_help,$function,$domain,$menulink,
 1067: 	    $helplink) = @_;
 1068:         if (! defined($color)) {
 1069:             if (! defined($function)) {
 1070:                 $function = &Apache::loncommon::get_users_function();
 1071:             }
 1072:             $color = &Apache::loncommon::designparm($function.'.tabbg',
 1073:                                                     $domain);
 1074:         }
 1075:         #
 1076:         my $Str = "\n".
 1077:             '<table width="100%" border="0" cellpadding="0" cellspacing="0">'.
 1078:             '<tr><td bgcolor="'.$color.'">'.
 1079:             '<font size="-1">';
 1080:         #
 1081:         # Make the faq and bug data cascade
 1082:         my $faq = '';
 1083:         my $bug = '';
 1084:         # The last breadcrumb does not have a link, so handle it separately.
 1085:         my $last = pop(@Crumbs);
 1086:         #
 1087:         # The first one should be the course or a menu link
 1088: 	if (!defined($menulink)) { $menulink=1; }
 1089:         if ($menulink) {
 1090:             my $description = 'Menu';
 1091:             if (exists($ENV{'request.course.id'}) && 
 1092:                 $ENV{'request.course.id'} ne '') {
 1093:                 $description = 
 1094:                     $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
 1095:             }
 1096:             unshift(@Crumbs,{
 1097:                     href   =>'/adm/menu',
 1098:                     title  =>'Go to main menu',
 1099:                     target =>'_top',
 1100:                     text   =>$description,
 1101:                 });
 1102:         }
 1103:         my $links .= 
 1104:             join('-&gt;',
 1105:                  map {
 1106:                      $faq = $_->{'faq'} if (exists($_->{'faq'}));
 1107:                      $bug = $_->{'bug'} if (exists($_->{'bug'}));
 1108:                      my $result = '<a href="'.$_->{'href'}.'" ';
 1109:                      if (defined($_->{'target'}) && $_->{'target'} ne '') {
 1110:                          $result .= 'target="'.$_->{'target'}.'" ';
 1111:                      }
 1112:                      $result .='title="'.&mt($_->{'title'}).'">'.
 1113:                          &mt($_->{'text'}).'</a>';
 1114:                      $result;
 1115:                      } @Crumbs
 1116:                  );
 1117:         $links .= '-&gt;' if ($links ne '');
 1118:         $links .= '<b>'.&mt($last->{'text'}).'</b>';
 1119:         #
 1120:         my $icons = '';
 1121:         $faq = $last->{'faq'} if (exists($last->{'faq'}));
 1122:         $bug = $last->{'bug'} if (exists($last->{'bug'}));
 1123: #        if ($faq ne '') {
 1124: #            $icons .= &Apache::loncommon::help_open_faq($faq);
 1125: #        }
 1126: #        if ($bug ne '') {
 1127: #            $icons .= &Apache::loncommon::help_open_bug($bug);
 1128: #        }
 1129: 	if ($helplink ne 'nohelp') {
 1130: 	    $icons .= &Apache::loncommon::help_open_menu($color,$component,$component_help,$function,$faq,$bug);
 1131: 	}
 1132:         if ($icons ne '') {
 1133:             $Str .= $icons.'&nbsp;';
 1134:         }
 1135:         #
 1136:         $Str .= $links.'</font></td>';
 1137:         #
 1138:         if (defined($component)) {
 1139:             $Str .= '<td align="right" bgcolor="'.$color.'">'.
 1140:                 '<font size="+1">'.&mt($component).'</font></td>';
 1141:         }
 1142:         $Str .= '</tr></table>'."\n";
 1143:         #
 1144:         # Return the @Crumbs stack to what we started with
 1145:         push(@Crumbs,$last);
 1146:         shift(@Crumbs);
 1147:         #
 1148:         return $Str;
 1149:     }
 1150: 
 1151:     sub clear_breadcrumbs {
 1152:         undef(@Crumbs);
 1153:     }
 1154: 
 1155:     sub add_breadcrumb {
 1156:         push (@Crumbs,@_);
 1157:     }
 1158: 
 1159: } # End of scope for @Crumbs
 1160: 
 1161: ############################################################
 1162: ############################################################
 1163: 
 1164: 
 1165: 1;
 1166: 
 1167: __END__

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