File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.95: download - view: text, annotated - select for diffs
Thu Nov 11 18:19:41 2004 UTC (19 years, 6 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Bug 2889: "datesetter needs in8l".

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

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