File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.105: download - view: text, annotated - select for diffs
Sat May 28 00:35:47 2005 UTC (19 years ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #4114: spellchecker not working on Mac, because somebody decided
that typeof(null)='object' ... null an object???
Anyway, works now.

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

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