File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.108: download - view: text, annotated - select for diffs
Tue Jun 14 02:33:18 2005 UTC (18 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Saving my work: default parameter setting actions

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

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