File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.171: download - view: text, annotated - select for diffs
Sat Nov 17 01:46:03 2007 UTC (16 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: version_2_6_0, version_2_5_99_1, version_2_5_99_0, HEAD
- BUG#5476 add a mime type option to the <window>

    1: # The LearningOnline Network with CAPA
    2: # a pile of common html routines
    3: #
    4: # $Id: lonhtmlcommon.pm,v 1.171 2007/11/17 01:46:03 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ######################################################################
   29: ######################################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::lonhtmlcommon - routines to do common html things
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Referenced by other mod_perl Apache modules.
   40: 
   41: =head1 INTRODUCTION
   42: 
   43: lonhtmlcommon is a collection of subroutines used to present information
   44: in a consistent html format, or provide other functionality related to
   45: html.
   46: 
   47: =head2 General Subroutines
   48: 
   49: =over 4
   50: 
   51: =cut 
   52: 
   53: ######################################################################
   54: ######################################################################
   55: 
   56: package Apache::lonhtmlcommon;
   57: 
   58: use strict;
   59: use Time::Local;
   60: use Time::HiRes;
   61: use Apache::lonlocal;
   62: use Apache::lonnet;
   63: use LONCAPA;
   64: 
   65: ##############################################
   66: ##############################################
   67: 
   68: =pod
   69: 
   70: =item authorbombs
   71: 
   72: =cut
   73: 
   74: ##############################################
   75: ##############################################
   76: 
   77: sub authorbombs {
   78:     my $url=shift;
   79:     $url=&Apache::lonnet::declutter($url);
   80:     my ($udom,$uname)=($url=~m{^($LONCAPA::domain_re)/($LONCAPA::username_re)/});
   81:     my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
   82:     foreach (keys %bombs) {
   83: 	if ($_=~/^$udom\/$uname\//) {
   84: 	    return '<a href="/adm/bombs/'.$url.
   85: 		'"><img src="'.&Apache::loncommon::lonhttpdurl('/adm/lonMisc/bomb.gif').'" border="0" /></a>'.
   86: 		&Apache::loncommon::help_open_topic('About_Bombs');
   87: 	}
   88:     }
   89:     return '';
   90: }
   91: 
   92: ##############################################
   93: ##############################################
   94: 
   95: sub recent_filename {
   96:     my $area=shift;
   97:     return 'nohist_recent_'.&escape($area);
   98: }
   99: 
  100: sub store_recent {
  101:     my ($area,$name,$value,$freeze)=@_;
  102:     my $file=&recent_filename($area);
  103:     my %recent=&Apache::lonnet::dump($file);
  104:     if (scalar(keys(%recent))>20) {
  105: # remove oldest value
  106: 	my $oldest=time();
  107: 	my $delkey='';
  108: 	foreach my $item (keys(%recent)) {
  109: 	    my $thistime=(split(/\&/,$recent{$item}))[0];
  110: 	    if (($thistime ne "always_include") && ($thistime<$oldest)) {
  111: 		$oldest=$thistime;
  112: 		$delkey=$item;
  113: 	    }
  114: 	}
  115: 	&Apache::lonnet::del($file,[$delkey]);
  116:     }
  117: # store new value
  118:     my $timestamp;
  119:     if ($freeze) {
  120:         $timestamp = "always_include";
  121:     } else {
  122:         $timestamp = time();
  123:     }   
  124:     &Apache::lonnet::put($file,{ $name => 
  125: 				 $timestamp.'&'.&escape($value) });
  126: }
  127: 
  128: sub remove_recent {
  129:     my ($area,$names)=@_;
  130:     my $file=&recent_filename($area);
  131:     return &Apache::lonnet::del($file,$names);
  132: }
  133: 
  134: sub select_recent {
  135:     my ($area,$fieldname,$event)=@_;
  136:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
  137:     my $return="\n<select name='$fieldname'".
  138: 	($event?" onchange='$event'":'').
  139: 	">\n<option value=''>--- ".&mt('Recent')." ---</option>";
  140:     foreach my $value (sort(keys(%recent))) {
  141: 	unless ($value =~/^error\:/) {
  142: 	    my $escaped = &Apache::loncommon::escape_url($value);
  143: 	    &Apache::loncommon::inhibit_menu_check(\$escaped);
  144: 	    $return.="\n<option value='$escaped'>".
  145: 		&unescape((split(/\&/,$recent{$value}))[1]).
  146: 		'</option>';
  147: 	}
  148:     }
  149:     $return.="\n</select>\n";
  150:     return $return;
  151: }
  152: 
  153: sub get_recent {
  154:     my ($area, $n) = @_;
  155:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
  156: 
  157: # Create hash with key as time and recent as value
  158: # Begin filling return_hash with any 'always_include' option
  159:     my %time_hash = ();
  160:     my %return_hash = ();
  161:     foreach my $item (keys %recent) {
  162:         my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
  163:         if ($thistime eq 'always_include') {
  164:             $return_hash{$item} = &unescape($thisvalue);
  165:             $n--;
  166:         } else {
  167:             $time_hash{$thistime} = $item;
  168:         }
  169:     }
  170: 
  171: # Sort by decreasing time and return key value pairs
  172:     my $idx = 1;
  173:     foreach my $item (reverse(sort(keys(%time_hash)))) {
  174:        $return_hash{$time_hash{$item}} =
  175:                   &unescape((split(/\&/,$recent{$time_hash{$item}}))[1]);
  176:        if ($n && ($idx++ >= $n)) {last;}
  177:     }
  178: 
  179:     return %return_hash;
  180: }
  181: 
  182: sub get_recent_frozen {
  183:     my ($area) = @_;
  184:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
  185: 
  186: # Create hash with all 'frozen' items
  187:     my %return_hash = ();
  188:     foreach my $item (keys(%recent)) {
  189:         my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
  190:         if ($thistime eq 'always_include') {
  191:             $return_hash{$item} = &unescape($thisvalue);
  192:         }
  193:     }
  194:     return %return_hash;
  195: }
  196: 
  197: 
  198: 
  199: =pod
  200: 
  201: =item textbox
  202: 
  203: =cut
  204: 
  205: ##############################################
  206: ##############################################
  207: sub textbox {
  208:     my ($name,$value,$size,$special) = @_;
  209:     $size = 40 if (! defined($size));
  210:     $value = &HTML::Entities::encode($value,'<>&"');
  211:     my $Str = '<input type="text" name="'.$name.'" size="'.$size.'" '.
  212:         'value="'.$value.'" '.$special.' />';
  213:     return $Str;
  214: }
  215: 
  216: ##############################################
  217: ##############################################
  218: 
  219: =pod
  220: 
  221: =item checkbox
  222: 
  223: =cut
  224: 
  225: ##############################################
  226: ##############################################
  227: sub checkbox {
  228:     my ($name,$checked,$value) = @_;
  229:     my $Str = '<input type="checkbox" name="'.$name.'" ';
  230:     if (defined($value)) {
  231:         $Str .= 'value="'.$value.'"';
  232:     } 
  233:     if ($checked) {
  234:         $Str .= ' checked="1"';
  235:     }
  236:     $Str .= ' />';
  237:     return $Str;
  238: }
  239: 
  240: 
  241: =pod
  242: 
  243: =item radiobutton
  244: 
  245: =cut
  246: 
  247: ##############################################
  248: ##############################################
  249: sub radio {
  250:     my ($name,$checked,$value) = @_;
  251:     my $Str = '<input type="radio" name="'.$name.'" ';
  252:     if (defined($value)) {
  253:         $Str .= 'value="'.$value.'"';
  254:     } 
  255:     if ($checked eq $value) {
  256:         $Str .= ' checked="1"';
  257:     }
  258:     $Str .= ' />';
  259:     return $Str;
  260: }
  261: 
  262: ##############################################
  263: ##############################################
  264: 
  265: =pod
  266: 
  267: =item &date_setter
  268: 
  269: &date_setter returns html and javascript for a compact date-setting form.
  270: To retrieve values from it, use &get_date_from_form().
  271: 
  272: Inputs
  273: 
  274: =over 4
  275: 
  276: =item $dname 
  277: 
  278: The name to prepend to the form elements.  
  279: The form elements defined will be dname_year, dname_month, dname_day,
  280: dname_hour, dname_min, and dname_sec.
  281: 
  282: =item $currentvalue
  283: 
  284: The current setting for this time parameter.  A unix format time
  285: (time in seconds since the beginning of Jan 1st, 1970, GMT.  
  286: An undefined value is taken to indicate the value is the current time.
  287: Also, to be explicit, a value of 'now' also indicates the current time.
  288: 
  289: =item $special
  290: 
  291: Additional html/javascript to be associated with each element in
  292: the date_setter.  See lonparmset for example usage.
  293: 
  294: =item $includeempty 
  295: 
  296: =item $state
  297: 
  298: Specifies the initial state of the form elements.  Either 'disabled' or empty.
  299: Defaults to empty, which indiciates the form elements are not disabled. 
  300: 
  301: =back
  302: 
  303: Bugs
  304: 
  305: The method used to restrict user input will fail in the year 2400.
  306: 
  307: =cut
  308: 
  309: ##############################################
  310: ##############################################
  311: sub date_setter {
  312:     my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
  313:         $no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_;
  314:     my $wasdefined=1;
  315:     if (! defined($state) || $state ne 'disabled') {
  316:         $state = '';
  317:     }
  318:     if (! defined($no_hh_mm_ss)) {
  319:         $no_hh_mm_ss = 0;
  320:     }
  321:     if ($currentvalue eq 'now') {
  322: 	$currentvalue=time;
  323:     }
  324:     if ((!defined($currentvalue)) || ($currentvalue eq '')) {
  325: 	$wasdefined=0;
  326: 	if ($includeempty) {
  327: 	    $currentvalue = 0;
  328: 	} else {
  329: 	    $currentvalue = time;
  330: 	}
  331:     }
  332:     # other potentially useful values:     wkday,yrday,is_daylight_savings
  333:     my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
  334:     if ($currentvalue) {
  335: 	($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
  336: 	    localtime($currentvalue);
  337: 	$year += 1900;
  338:     }
  339:     unless ($wasdefined) {
  340: 	if (($defhour) || ($defmin) || ($defsec)) {
  341: 	    ($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
  342: 		localtime(time);
  343: 	    $year += 1900;
  344: 	    $sec=($defsec?$defsec:0);
  345: 	    $min=($defmin?$defmin:0);
  346: 	    $hour=($defhour?$defhour:0);
  347: 	} elsif (!$includeempty) {
  348: 	    $sec=0;
  349: 	    $min=0;
  350: 	    $hour=0;
  351: 	}
  352:     }
  353:     my $result = "\n<!-- $dname date setting form -->\n";
  354:     $result .= <<ENDJS;
  355: <script type="text/javascript">
  356:     function $dname\_checkday() {
  357:         var day   = document.$formname.$dname\_day.value;
  358:         var month = document.$formname.$dname\_month.value;
  359:         var year  = document.$formname.$dname\_year.value;
  360:         var valid = true;
  361:         if (day < 1) {
  362:             document.$formname.$dname\_day.value = 1;
  363:         } 
  364:         if (day > 31) {
  365:             document.$formname.$dname\_day.value = 31;
  366:         }
  367:         if ((month == 1)  || (month == 3)  || (month == 5)  ||
  368:             (month == 7)  || (month == 8)  || (month == 10) ||
  369:             (month == 12)) {
  370:             if (day > 31) {
  371:                 document.$formname.$dname\_day.value = 31;
  372:                 day = 31;
  373:             }
  374:         } else if (month == 2 ) {
  375:             if ((year % 4 == 0) && (year % 100 != 0)) {
  376:                 if (day > 29) {
  377:                     document.$formname.$dname\_day.value = 29;
  378:                 }
  379:             } else if (day > 29) {
  380:                 document.$formname.$dname\_day.value = 28;
  381:             }
  382:         } else if (day > 30) {
  383:             document.$formname.$dname\_day.value = 30;
  384:         }
  385:     }
  386:     
  387:     function $dname\_disable() {
  388:         document.$formname.$dname\_month.disabled=true;
  389:         document.$formname.$dname\_day.disabled=true;
  390:         document.$formname.$dname\_year.disabled=true;
  391:         document.$formname.$dname\_hour.disabled=true;
  392:         document.$formname.$dname\_minute.disabled=true;
  393:         document.$formname.$dname\_second.disabled=true;
  394:     }
  395: 
  396:     function $dname\_enable() {
  397:         document.$formname.$dname\_month.disabled=false;
  398:         document.$formname.$dname\_day.disabled=false;
  399:         document.$formname.$dname\_year.disabled=false;
  400:         document.$formname.$dname\_hour.disabled=false;
  401:         document.$formname.$dname\_minute.disabled=false;
  402:         document.$formname.$dname\_second.disabled=false;        
  403:     }
  404: 
  405:     function $dname\_opencalendar() {
  406:         if (! document.$formname.$dname\_month.disabled) {
  407:             var calwin=window.open(
  408: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
  409: document.$formname.$dname\_month.value+"&year="+
  410: document.$formname.$dname\_year.value,
  411:              "LONCAPAcal",
  412:               "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
  413:         }
  414: 
  415:     }
  416: </script>
  417: ENDJS
  418:     $result .= '  <span style="white-space: nowrap;">';
  419:     my $monthselector = qq{<select name="$dname\_month" $special $state onchange="javascript:$dname\_checkday()" >};
  420:     # Month
  421:     my @Months = qw/January February  March     April   May      June 
  422:                     July    August    September October November December/;
  423:     # Pad @Months with a bogus value to make indexing easier
  424:     unshift(@Months,'If you can read this an error occurred');
  425:     if ($includeempty) { $monthselector.="<option value=''></option>"; }
  426:     for(my $m = 1;$m <=$#Months;$m++) {
  427:         $monthselector .= qq{      <option value="$m" };
  428:         $monthselector .= "selected " if ($m-1 eq $month);
  429:         $monthselector .= '> '.&mt($Months[$m]).' </option>';
  430:     }
  431:     $monthselector.= '  </select>';
  432:     # Day
  433:     my $dayselector = qq{<input type="text" name="$dname\_day" $state value="$mday" size="3" $special onchange="javascript:$dname\_checkday()" />};
  434:     # Year
  435:     my $yearselector = qq{<input type="year" name="$dname\_year" $state value="$year" size="5" $special onchange="javascript:$dname\_checkday()" />};
  436:     #
  437:     my $hourselector = qq{<select name="$dname\_hour" $special $state >};
  438:     if ($includeempty) { 
  439:         $hourselector.=qq{<option value=''></option>};
  440:     }
  441:     for (my $h = 0;$h<24;$h++) {
  442:         $hourselector .= qq{<option value="$h" };
  443:         $hourselector .= "selected " if (defined($hour) && $hour == $h);
  444:         $hourselector .= ">";
  445:         my $timest='';
  446:         if ($h == 0) {
  447:             $timest .= "12 am";
  448:         } elsif($h == 12) {
  449:             $timest .= "12 noon";
  450:         } elsif($h < 12) {
  451:             $timest .= "$h am";
  452:         } else {
  453:             $timest .= $h-12 ." pm";
  454:         }
  455:         $timest=&mt($timest);
  456:         $hourselector .= $timest." </option>\n";
  457:     }
  458:     $hourselector .= "  </select>\n";
  459:     my $minuteselector = qq{<input type="text" name="$dname\_minute" $special $state value="$min" size="3" />};
  460:     my $secondselector= qq{<input type="text" name="$dname\_second" $special $state value="$sec" size="3" />};
  461:     my $cal_link;
  462:     if (!$nolink) {
  463:         $cal_link = qq{<a href="javascript:$dname\_opencalendar()">};
  464:     }
  465:     #
  466:     if ($no_hh_mm_ss) {
  467:         $result .= &mt('[_1] [_2] [_3] ',
  468:                        $monthselector,$dayselector,$yearselector);
  469:         if (!$nolink) {
  470:             $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
  471:         }
  472:     } else {
  473:         $result .= &mt('[_1] [_2] [_3] [_4] [_5]m [_6]s ',
  474:                       $monthselector,$dayselector,$yearselector,
  475:                       $hourselector,$minuteselector,$secondselector);
  476:         if (!$nolink) {
  477:             $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
  478:         }
  479:     }
  480:     $result .= "</span>\n<!-- end $dname date setting form -->\n";
  481:     return $result;
  482: }
  483: 
  484: 
  485: sub build_url {
  486:     my ($base, $fields)=@_;
  487:     my $url;
  488:     $url = $base.'?';
  489:     foreach my $key (keys(%$fields)) {
  490:         $url.=&escape($key).'='.&escape($$fields{$key}).'&amp;';
  491:     }
  492:     $url =~ s/&amp;$//;
  493:     return $url;
  494: }
  495: 
  496: 
  497: ##############################################
  498: ##############################################
  499: 
  500: =pod
  501: 
  502: =item &get_date_from_form
  503: 
  504: get_date_from_form retrieves the date specified in an &date_setter form.
  505: 
  506: Inputs:
  507: 
  508: =over 4
  509: 
  510: =item $dname
  511: 
  512: The name passed to &datesetter, which prefixes the form elements.
  513: 
  514: =item $defaulttime
  515: 
  516: The unix time to use as the default in case of poor inputs.
  517: 
  518: =back
  519: 
  520: Returns: Unix time represented in the form.
  521: 
  522: =cut
  523: 
  524: ##############################################
  525: ##############################################
  526: sub get_date_from_form {
  527:     my ($dname) = @_;
  528:     my ($sec,$min,$hour,$day,$month,$year);
  529:     #
  530:     if (defined($env{'form.'.$dname.'_second'})) {
  531:         my $tmpsec = $env{'form.'.$dname.'_second'};
  532:         if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
  533:             $sec = $tmpsec;
  534:         }
  535: 	if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
  536:     } else {
  537:         $sec = 0;
  538:     }
  539:     if (defined($env{'form.'.$dname.'_minute'})) {
  540:         my $tmpmin = $env{'form.'.$dname.'_minute'};
  541:         if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
  542:             $min = $tmpmin;
  543:         }
  544: 	if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
  545:     } else {
  546:         $min = 0;
  547:     }
  548:     if (defined($env{'form.'.$dname.'_hour'})) {
  549:         my $tmphour = $env{'form.'.$dname.'_hour'};
  550:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
  551:             $hour = $tmphour;
  552:         }
  553:     } else {
  554:         $hour = 0;
  555:     }
  556:     if (defined($env{'form.'.$dname.'_day'})) {
  557:         my $tmpday = $env{'form.'.$dname.'_day'};
  558:         if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
  559:             $day = $tmpday;
  560:         }
  561:     }
  562:     if (defined($env{'form.'.$dname.'_month'})) {
  563:         my $tmpmonth = $env{'form.'.$dname.'_month'};
  564:         if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
  565:             $month = $tmpmonth - 1;
  566:         }
  567:     }
  568:     if (defined($env{'form.'.$dname.'_year'})) {
  569:         my $tmpyear = $env{'form.'.$dname.'_year'};
  570:         if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
  571:             $year = $tmpyear - 1900;
  572:         }
  573:     }
  574:     if (($year<70) || ($year>137)) { return undef; }
  575:     if (defined($sec) && defined($min)   && defined($hour) &&
  576:         defined($day) && defined($month) && defined($year) &&
  577:         eval('&timelocal($sec,$min,$hour,$day,$month,$year)')) {
  578:         return &timelocal($sec,$min,$hour,$day,$month,$year);
  579:     } else {
  580:         return undef;
  581:     }
  582: }
  583: 
  584: ##############################################
  585: ##############################################
  586: 
  587: =pod
  588: 
  589: =item &pjump_javascript_definition()
  590: 
  591: Returns javascript defining the 'pjump' function, which opens up a
  592: parameter setting wizard.
  593: 
  594: =cut
  595: 
  596: ##############################################
  597: ##############################################
  598: sub pjump_javascript_definition {
  599:     my $Str = <<END;
  600:     function pjump(type,dis,value,marker,ret,call,hour,min,sec) {
  601:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
  602:                  +"&value="+escape(value)+"&marker="+escape(marker)
  603:                  +"&return="+escape(ret)
  604:                  +"&call="+escape(call)+"&name="+escape(dis)
  605:                  +"&defhour="+escape(hour)+"&defmin="+escape(min)
  606:                  +"&defsec="+escape(sec),"LONCAPAparms",
  607:                  "height=350,width=350,scrollbars=no,menubar=no");
  608:     }
  609: END
  610:     return $Str;
  611: }
  612: 
  613: ##############################################
  614: ##############################################
  615: 
  616: =pod
  617: 
  618: =item &javascript_nothing()
  619: 
  620: Return an appropriate null for the users browser.  This is used
  621: as the first arguement for window.open calls when you want a blank
  622: window that you can then write to.
  623: 
  624: =cut
  625: 
  626: ##############################################
  627: ##############################################
  628: sub javascript_nothing {
  629:     # mozilla and other browsers work with "''", but IE on mac does not.
  630:     my $nothing = "''";
  631:     my $user_browser;
  632:     my $user_os;
  633:     $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
  634:     $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
  635:     if (! defined($user_browser) || ! defined($user_os)) {
  636:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  637:                            &Apache::loncommon::decode_user_agent();
  638:     }
  639:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
  640:         $nothing = "'javascript:void(0);'";
  641:     }
  642:     return $nothing;
  643: }
  644: 
  645: ##############################################
  646: ##############################################
  647: sub javascript_docopen {
  648:     my ($mimetype) = @_;
  649:     $mimetype ||= 'text/html';
  650:     # safari does not understand document.open() and loads "text/html"
  651:     my $nothing = "''";
  652:     my $user_browser;
  653:     my $user_os;
  654:     $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
  655:     $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
  656:     if (! defined($user_browser) || ! defined($user_os)) {
  657:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  658:                            &Apache::loncommon::decode_user_agent();
  659:     }
  660:     if ($user_browser eq 'safari' && $user_os =~ 'mac') {
  661:         $nothing = "document.clear()";
  662:     } else {
  663: 	$nothing = "document.open('$mimetype','replace')";
  664:     }
  665:     return $nothing;
  666: }
  667: 
  668: 
  669: ##############################################
  670: ##############################################
  671: 
  672: =pod
  673: 
  674: =item &StatusOptions()
  675: 
  676: Returns html for a selection box which allows the user to choose the
  677: enrollment status of students.  The selection box name is 'Status'.
  678: 
  679: Inputs:
  680: 
  681: $status: the currently selected status.  If undefined the value of
  682: $env{'form.Status'} is taken.  If that is undefined, a value of 'Active'
  683: is used.
  684: 
  685: $formname: The name of the form.  If defined the onchange attribute of
  686: the selection box is set to document.$formname.submit().
  687: 
  688: $size: the size (number of lines) of the selection box.
  689: 
  690: $onchange: javascript to use when the value is changed.  Enclosed in 
  691: double quotes, ""s, not single quotes.
  692: 
  693: Returns: a perl string as described.
  694: 
  695: =cut
  696: 
  697: ##############################################
  698: ##############################################
  699: sub StatusOptions {
  700:     my ($status, $formName,$size,$onchange,$mult)=@_;
  701:     $size = 1 if (!defined($size));
  702:     if (! defined($status)) {
  703:         $status = 'Active';
  704:         $status = $env{'form.Status'} if (exists($env{'form.Status'}));
  705:     }
  706: 
  707:     my $Str = '';
  708:     $Str .= '<select name="Status"';
  709:     if (defined($mult)){
  710:         $Str .= ' multiple="multiple" ';
  711:     }
  712:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
  713:         $Str .= ' onchange="document.'.$formName.'.submit()"';
  714:     }
  715:     if (defined($onchange)) {
  716:         $Str .= ' onchange="'.$onchange.'"';
  717:     }
  718:     $Str .= ' size="'.$size.'" ';
  719:     $Str .= '>'."\n";
  720:     foreach my $type (['Active',  &mt('Currently Has Access')],
  721: 		      ['Future',  &mt('Will Have Future Access')],
  722: 		      ['Expired', &mt('Previously Had Access')],
  723: 		      ['Any',     &mt('Any Access Status')]) {
  724: 	my ($name,$label) = @$type;
  725: 	$Str .= '<option value="'.$name.'" ';
  726: 	if ($status eq $name) {
  727: 	    $Str .= 'selected="selected" ';
  728: 	}
  729: 	$Str .= '>'.$label.'</option>'."\n";
  730:     }
  731: 
  732:     $Str .= '</select>'."\n";
  733: }
  734: 
  735: ########################################################
  736: ########################################################
  737: 
  738: =pod
  739: 
  740: =item Progess Window Handling Routines
  741: 
  742: These routines handle the creation, update, increment, and closure of 
  743: progress windows.  The progress window reports to the user the number
  744: of items completed and an estimate of the time required to complete the rest.
  745: 
  746: =over 4
  747: 
  748: 
  749: =item &Create_PrgWin
  750: 
  751: Writes javascript to the client to open a progress window and returns a
  752: data structure used for bookkeeping.
  753: 
  754: Inputs
  755: 
  756: =over 4
  757: 
  758: =item $r Apache request
  759: 
  760: =item $title The title of the progress window
  761: 
  762: =item $heading A description (usually 1 line) of the process being initiated.
  763: 
  764: =item $number_to_do The total number of items being processed.
  765: 
  766: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
  767:        specified)
  768: 
  769: =item $width Specify the width in charaters of the input field.
  770: 
  771: =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
  772: 
  773: =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 
  774: 
  775: =back
  776: 
  777: Returns a hash containing the progress state data structure.
  778: 
  779: 
  780: =item &Update_PrgWin
  781: 
  782: Updates the text in the progress indicator.  Does not increment the count.
  783: See &Increment_PrgWin.
  784: 
  785: Inputs:
  786: 
  787: =over 4
  788: 
  789: =item $r Apache request
  790: 
  791: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
  792: 
  793: =item $displaystring The string to write to the status indicator
  794: 
  795: =back
  796: 
  797: Returns: none
  798: 
  799: 
  800: =item Increment_PrgWin
  801: 
  802: Increment the count of items completed for the progress window by 1.  
  803: 
  804: Inputs:
  805: 
  806: =over 4
  807: 
  808: =item $r Apache request
  809: 
  810: =item $prog_state Pointer to the data structure returned by Create_PrgWin
  811: 
  812: =item $extraInfo A description of the items being iterated over.  Typically
  813: 'student'.
  814: 
  815: =back
  816: 
  817: Returns: none
  818: 
  819: 
  820: =item Close_PrgWin
  821: 
  822: Closes the progress window.
  823: 
  824: Inputs:
  825: 
  826: =over 4 
  827: 
  828: =item $r Apache request
  829: 
  830: =item $prog_state Pointer to the data structure returned by Create_PrgWin
  831: 
  832: =back
  833: 
  834: Returns: none
  835: 
  836: =back
  837: 
  838: =cut
  839: 
  840: ########################################################
  841: ########################################################
  842: 
  843: my $uniq=0;
  844: sub get_uniq_name {
  845:     $uniq++;
  846:     return 'uniquename'.$uniq;
  847: }
  848: 
  849: # Create progress
  850: sub Create_PrgWin {
  851:     my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
  852: 	$inputname)=@_;
  853:     if (!defined($type)) { $type='popup'; }
  854:     if (!defined($width)) { $width=55; }
  855:     my %prog_state;
  856:     $prog_state{'type'}=$type;
  857:     if ($type eq 'popup') {
  858: 	$prog_state{'window'}='popwin';
  859: 	my $start_page =
  860: 	    &Apache::loncommon::start_page($title,undef,
  861: 					   {'only_body' => 1,
  862: 					    'bgcolor'   => '#88DDFF',
  863: 					    'js_ready'  => 1});
  864: 	my $end_page = &Apache::loncommon::end_page({'js_ready'  => 1});
  865: 
  866: 	#the whole function called through timeout is due to issues
  867: 	#in mozilla Read BUG #2665 if you want to know the whole story
  868: 	&r_print($r,'<script type="text/javascript">'.
  869:         "var popwin;
  870:          function openpopwin () {
  871:          popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
  872:         "popwin.document.writeln(\'".$start_page.
  873:               "<h4>".&mt("$heading")."<\/h4>".
  874:               "<form action= \"\" name=\"popremain\" method=\"post\">".
  875:               '<input type="text" size="'.$width.'" name="remaining" value="'.
  876: 	      &mt('Starting').'" /><\\/form>'.$end_page.
  877:               "\');".
  878:         "popwin.document.close();}".
  879:         "\nwindow.setTimeout(openpopwin,0)</script>");
  880: 	$prog_state{'formname'}='popremain';
  881: 	$prog_state{'inputname'}="remaining";
  882:     } elsif ($type eq 'inline') {
  883: 	$prog_state{'window'}='window';
  884: 	if (!$formname) {
  885: 	    $prog_state{'formname'}=&get_uniq_name();
  886: 	    &r_print($r,'<form action="" name="'.$prog_state{'formname'}.'">');
  887: 	} else {
  888: 	    $prog_state{'formname'}=$formname;
  889: 	}
  890: 	if (!$inputname) {
  891: 	    $prog_state{'inputname'}=&get_uniq_name();
  892: 	    &r_print($r,&mt("$heading [_1]",' <input type="text" name="'.$prog_state{'inputname'}.'" size="'.$width.'" />'));
  893: 	} else {
  894: 	    $prog_state{'inputname'}=$inputname;
  895: 	    
  896: 	}
  897: 	if (!$formname) { &r_print($r,'</form>'); }
  898: 	&Update_PrgWin($r,\%prog_state,&mt('Starting'));
  899:     }
  900: 
  901:     $prog_state{'done'}=0;
  902:     $prog_state{'firststart'}=&Time::HiRes::time();
  903:     $prog_state{'laststart'}=&Time::HiRes::time();
  904:     $prog_state{'max'}=$number_to_do;
  905:     
  906:     return %prog_state;
  907: }
  908: 
  909: # update progress
  910: sub Update_PrgWin {
  911:     my ($r,$prog_state,$displayString)=@_;
  912:     &r_print($r,'<script type="text/javascript">'.$$prog_state{'window'}.'.document.'.
  913: 	     $$prog_state{'formname'}.'.'.
  914: 	     $$prog_state{'inputname'}.'.value="'.
  915: 	     $displayString.'";</script>');
  916:     $$prog_state{'laststart'}=&Time::HiRes::time();
  917: }
  918: 
  919: # increment progress state
  920: sub Increment_PrgWin {
  921:     my ($r,$prog_state,$extraInfo)=@_;
  922:     $$prog_state{'done'}++;
  923:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
  924:         $$prog_state{'done'} *
  925: 	($$prog_state{'max'}-$$prog_state{'done'});
  926:     $time_est = int($time_est);
  927:     #
  928:     my $min = int($time_est/60);
  929:     my $sec = $time_est % 60;
  930:     # 
  931:     my $str;
  932:     if ($min == 0 && $sec > 1) {
  933:         $str = '[_2] seconds';
  934:     } elsif ($min == 1 && $sec > 1) {
  935:         $str = '1 minute [_2] seconds';
  936:     } elsif ($min == 1 && $sec < 2) {
  937:         $str = '1 minute';
  938:     } elsif ($min < 10 && $sec > 1) {
  939:         $str = '[_1] minutes, [_2] seconds';
  940:     } elsif ($min >= 10 || $sec < 2) {
  941:         $str = '[_1] minutes';
  942:     }
  943:     $time_est = &mt($str,$min,$sec);
  944:     #
  945:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
  946:     if ($lasttime > 9) {
  947:         $lasttime = int($lasttime);
  948:     } elsif ($lasttime < 0.01) {
  949:         $lasttime = 0;
  950:     } else {
  951:         $lasttime = sprintf("%3.2f",$lasttime);
  952:     }
  953:     if ($lasttime == 1) {
  954:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
  955:     } else {
  956:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
  957:     }
  958:     #
  959:     my $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
  960:     my $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
  961:     if (! defined($user_browser) || ! defined($user_os)) {
  962:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  963:                            &Apache::loncommon::decode_user_agent();
  964:     }
  965:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
  966:         $lasttime = '';
  967:     }
  968:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
  969: 	     $$prog_state{'formname'}.'.'.
  970: 	     $$prog_state{'inputname'}.'.value="'.
  971: 	     $$prog_state{'done'}.'/'.$$prog_state{'max'}.
  972: 	     ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
  973:     $$prog_state{'laststart'}=&Time::HiRes::time();
  974: }
  975: 
  976: # close Progress Line
  977: sub Close_PrgWin {
  978:     my ($r,$prog_state)=@_;
  979:     if ($$prog_state{'type'} eq 'popup') {
  980: 	&r_print($r,'<script>popwin.close()</script>'."\n");
  981:     } elsif ($$prog_state{'type'} eq 'inline') {
  982: 	&Update_PrgWin($r,$prog_state,&mt('Done'));
  983:     }
  984:     undef(%$prog_state);
  985: }
  986: 
  987: sub r_print {
  988:     my ($r,$to_print)=@_;
  989:     if ($r) {
  990: 	$r->print($to_print);
  991: 	$r->rflush();
  992:     } else {
  993: 	print($to_print);
  994:     }
  995: }
  996: 
  997: # ------------------------------------------------------- Puts directory header
  998: 
  999: sub crumbs {
 1000:     my ($uri,$target,$prefix,$form,$size,$noformat,$skiplast)=@_;
 1001:     if (! defined($size)) {
 1002:         $size = '+2';
 1003:     }
 1004:     if ($target) {
 1005:         $target = ' target="'.
 1006:                   &Apache::loncommon::escape_single($target).'"';
 1007:     }
 1008:     my $output='';
 1009:     unless ($noformat) { $output.='<br /><tt><b>'; }
 1010:     $output.='<font size="'.$size.'">'.$prefix.'/';
 1011:     if ($env{'user.adv'}) {
 1012: 	my $path=$prefix.'/';
 1013: 	foreach my $dir (split('/',$uri)) {
 1014:             if (! $dir) { next; }
 1015:             $path .= $dir;
 1016: 	    if ($path eq $uri) {
 1017: 		if ($skiplast) {
 1018: 		    $output.=$dir;
 1019:                     last;
 1020: 		} 
 1021: 	    } else {
 1022: 		$path.='/'; 
 1023: 	    }	    
 1024:             my $href_path = &HTML::Entities::encode($path,'<>&"');
 1025: 	    &Apache::loncommon::inhibit_menu_check(\$href_path);
 1026: 	    if ($form) {
 1027: 	        my $href = 'javascript:'.$form.".action='".$href_path."';".$form.'.submit();';
 1028: 	        $output.=qq{<a href="$href" $target>$dir</a>/};
 1029: 	    } else {
 1030: 	        $output.=qq{<a href="$href_path" $target>$dir</a>/};
 1031: 	    }
 1032: 	}
 1033:     } else {
 1034: 	foreach my $dir (split('/',$uri)) {
 1035:             if (! $dir) { next; }
 1036: 	    $output.=$dir.'/';
 1037: 	}
 1038:     }
 1039:     if ($uri !~ m|/$|) { $output=~s|/$||; }
 1040:     return $output.'</font>'.($noformat?'':'</b></tt><br />');
 1041: }
 1042: 
 1043: # --------------------- A function that generates a window for the spellchecker
 1044: 
 1045: sub spellheader {
 1046:     my $start_page=
 1047: 	&Apache::loncommon::start_page('Speller Suggestions',undef,
 1048: 				       {'only_body'   => 1,
 1049: 					'js_ready'    => 1,
 1050: 					'bgcolor'     => '#DDDDDD',
 1051: 				        'add_entries' => {
 1052: 					    'onload' => 
 1053:                                                'document.forms.spellcheckform.submit()',
 1054:                                              }
 1055: 				        });
 1056:     my $end_page=
 1057: 	&Apache::loncommon::end_page({'js_ready'  => 1}); 
 1058: 
 1059:     my $nothing=&javascript_nothing();
 1060:     return (<<ENDCHECK);
 1061: <script type="text/javascript"> 
 1062: //<!-- BEGIN LON-CAPA Internal
 1063: var checkwin;
 1064: 
 1065: function spellcheckerwindow(string) {
 1066:     var esc_string = string.replace(/\"/g,'&quot;');
 1067:     checkwin=window.open($nothing,'spellcheckwin','height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
 1068:     checkwin.document.writeln('$start_page<form name="spellcheckform" action="/adm/spellcheck" method="post"><input type="hidden" name="text" value="'+esc_string+'" /><\\/form>$end_page');
 1069:     checkwin.document.close();
 1070: }
 1071: // END LON-CAPA Internal -->
 1072: </script>
 1073: ENDCHECK
 1074: }
 1075: 
 1076: # ---------------------------------- Generate link to spell checker for a field
 1077: 
 1078: sub spelllink {
 1079:     my ($form,$field)=@_;
 1080:     my $linktext=&mt('Check Spelling');
 1081:     return (<<ENDLINK);
 1082: <a href="javascript:if (typeof(document.$form.onsubmit)!='undefined') { if (document.$form.onsubmit!=null) { document.$form.onsubmit();}};spellcheckerwindow(this.document.forms.$form.$field.value);">$linktext</a>
 1083: ENDLINK
 1084: }
 1085: 
 1086: # ------------------------------------------------- Output headers for HTMLArea
 1087: 
 1088: {
 1089:     my @htmlareafields;
 1090:     sub init_htmlareafields {
 1091: 	undef(@htmlareafields);
 1092:     }
 1093:     
 1094:     sub add_htmlareafields {
 1095: 	my (@newfields) = @_;
 1096: 	push(@htmlareafields,@newfields);
 1097:     }
 1098: 
 1099:     sub get_htmlareafields {
 1100: 	return @htmlareafields;
 1101:     }
 1102: }
 1103: 
 1104: sub htmlareaheaders {
 1105:     return if (&htmlareablocked());
 1106:     return if (!&htmlareabrowser());
 1107:     return (<<ENDHEADERS);
 1108: <script type="text/javascript" src="/fckeditor/fckeditor.js"></script>
 1109: ENDHEADERS
 1110: }
 1111: 
 1112: # ----------------------------------------------------------------- Preferences
 1113: 
 1114: sub disablelink {
 1115:     my @fields=@_;
 1116:     if (defined($#fields)) {
 1117: 	unless ($#fields>=0) { return ''; }
 1118:     }
 1119:     return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=off&returnurl=','<>&"').&escape($ENV{'REQUEST_URI'}).'">'.&mt('Disable WYSIWYG Editor').'</a>';
 1120: }
 1121: 
 1122: sub enablelink {
 1123:     my @fields=@_;
 1124:     if (defined($#fields)) {
 1125: 	unless ($#fields>=0) { return ''; }
 1126:     }
 1127:     return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=on&returnurl=','<>&"').&escape($ENV{'REQUEST_URI'}).'">'.&mt('Enable WYSIWYG Editor').'</a>';
 1128: }
 1129: 
 1130: # ------------------------------------------------- lang to use in html editor
 1131: sub htmlarea_lang {
 1132:     my $lang='en';
 1133:     if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
 1134: 	$lang=&mt('htmlarea_lang');
 1135:     }
 1136:     return $lang;
 1137: }
 1138: 
 1139: # ----------------------------------------- Script to activate only some fields
 1140: 
 1141: sub htmlareaselectactive {
 1142:     my @fields=@_;
 1143:     unless (&htmlareabrowser()) { return ''; }
 1144:     if (&htmlareablocked()) { return '<br />'.&enablelink(@fields); }
 1145:     my $output='<script type="text/javascript" defer="1">';
 1146:     my $lang = &htmlarea_lang();
 1147:     foreach my $field (@fields) {
 1148: 	$output.="
 1149: {
 1150:     var oFCKeditor = new FCKeditor('$field');
 1151:     oFCKeditor.Config['CustomConfigurationsPath'] = 
 1152: 	'/fckeditor/loncapaconfig.js';    
 1153:     oFCKeditor.ReplaceTextarea();
 1154:     oFCKeditor.Config['AutoDetectLanguage'] = false;
 1155:     oFCKeditor.Config['DefaultLanguage'] = '$lang';
 1156: }";
 1157:     }
 1158:     $output.="\nwindow.status='Activated Editfields';\n</script><br />".
 1159: 	&disablelink(@fields);
 1160:     return $output;
 1161: }
 1162: 
 1163: # --------------------------------------------------------------------- Blocked
 1164: 
 1165: sub htmlareablocked {
 1166:     unless ($env{'environment.wysiwygeditor'} eq 'on') { return 1; }
 1167:     return 0;
 1168: }
 1169: 
 1170: # ---------------------------------------- Browser capable of running HTMLArea?
 1171: 
 1172: sub htmlareabrowser {
 1173:     return 1;
 1174: }
 1175: 
 1176: ############################################################
 1177: ############################################################
 1178: 
 1179: =pod
 1180: 
 1181: =item breadcrumbs
 1182: 
 1183: Compiles the previously registered breadcrumbs into an series of links.
 1184: FAQ and BUG links will be placed on the left side of the table if they
 1185: are defined for the last registered breadcrumb.  
 1186: Additionally supports a 'component', which will be displayed on the
 1187: right side of the table (without a link).
 1188: A link to help for the component will be included if one is specified.
 1189: 
 1190: All inputs can be undef without problems.
 1191: 
 1192: Inputs: $component (the large text on the right side of the table),
 1193:         $component_help
 1194:         $menulink (boolean, controls whether to include a link to /adm/menu)
 1195:         $helplink (if 'nohelp' don't include the orange help link)
 1196:         $css_class (optional name for the class to apply to the table for CSS)
 1197: Returns a string containing breadcrumbs for the current page.
 1198: 
 1199: =item clear_breadcrumbs
 1200: 
 1201: Clears the previously stored breadcrumbs.
 1202: 
 1203: =item add_breadcrumb
 1204: 
 1205: Pushes a breadcrumb on the stack of crumbs.
 1206: 
 1207: input: $breadcrumb, a hash reference.  The keys 'href','title', and 'text'
 1208: are required.  If present the keys 'faq' and 'bug' will be used to provide
 1209: links to the FAQ and bug sites. If the key 'no_mt' is present the 'title' 
 1210: and 'text' values won't be sent through &mt()
 1211: 
 1212: returns: nothing    
 1213: 
 1214: =cut
 1215: 
 1216: ############################################################
 1217: ############################################################
 1218: {
 1219:     my @Crumbs;
 1220:     
 1221:     sub breadcrumbs {
 1222:         my ($component,$component_help,$menulink,$helplink,$css_class) = @_;
 1223:         #
 1224: 	$css_class ||= 'LC_breadcrumbs';
 1225:         my $Str = "\n".'<table class="'.$css_class.'"><tr><td>';
 1226:         #
 1227:         # Make the faq and bug data cascade
 1228:         my $faq = '';
 1229:         my $bug = '';
 1230: 	my $help='';
 1231:         # The last breadcrumb does not have a link, so handle it separately.
 1232:         my $last = pop(@Crumbs);
 1233:         #
 1234:         # The first one should be the course or a menu link
 1235: 	if (!defined($menulink)) { $menulink=1; }
 1236:         if ($menulink) {
 1237:             my $description = 'Menu';
 1238:             if (exists($env{'request.course.id'}) && 
 1239:                 $env{'request.course.id'} ne '') {
 1240:                 $description = 
 1241:                     $env{'course.'.$env{'request.course.id'}.'.description'};
 1242:             }
 1243:             unshift(@Crumbs,{
 1244:                     href   =>'/adm/menu',
 1245:                     title  =>'Go to main menu',
 1246:                     target =>'_top',
 1247:                     text   =>$description,
 1248:                 });
 1249:         }
 1250:         my $links .= 
 1251:             join('-&gt;',
 1252:                  map {
 1253:                      $faq = $_->{'faq'} if (exists($_->{'faq'}));
 1254:                      $bug = $_->{'bug'} if (exists($_->{'bug'}));
 1255:                      $help = $_->{'help'} if (exists($_->{'help'}));
 1256:                      my $result = '<a href="'.$_->{'href'}.'" ';
 1257:                      if (defined($_->{'target'}) && $_->{'target'} ne '') {
 1258:                          $result .= 'target="'.$_->{'target'}.'" ';
 1259:                      }
 1260: 		     if ($_->{'no_mt'}) {
 1261: 			 $result .='title="'.$_->{'title'}.'">'.
 1262: 			     $_->{'text'}.'</a>';
 1263: 		     } else {
 1264: 			 $result .='title="'.&mt($_->{'title'}).'">'.
 1265: 			     &mt($_->{'text'}).'</a>';
 1266: 		     }
 1267:                      $result;
 1268:                      } @Crumbs
 1269:                  );
 1270:         $links .= '-&gt;' if ($links ne '');
 1271: 	if ($last->{'no_mt'}) {
 1272: 	    $links .= '<b>'.$last->{'text'}.'</b>';
 1273: 	} else {
 1274: 	    $links .= '<b>'.&mt($last->{'text'}).'</b>';
 1275: 	}
 1276:         #
 1277:         my $icons = '';
 1278:         $faq = $last->{'faq'} if (exists($last->{'faq'}));
 1279:         $bug = $last->{'bug'} if (exists($last->{'bug'}));
 1280:         $help = $last->{'help'} if (exists($last->{'help'}));
 1281:         $component_help=($component_help?$component_help:$help);
 1282: #        if ($faq ne '') {
 1283: #            $icons .= &Apache::loncommon::help_open_faq($faq);
 1284: #        }
 1285: #        if ($bug ne '') {
 1286: #            $icons .= &Apache::loncommon::help_open_bug($bug);
 1287: #        }
 1288: 	if ($faq ne '' || $component_help ne '' || $bug ne '') {
 1289: 	    $icons .= &Apache::loncommon::help_open_menu($component,
 1290: 							 $component_help,
 1291: 							 $faq,$bug);
 1292: 	}
 1293:         #
 1294:         $Str .= $links.'</td>';
 1295:         #
 1296:         if (defined($component)) {
 1297:             $Str .= '<td class="'.$css_class.'_component">'.
 1298:                 &mt($component);
 1299: 	    if ($icons ne '') {
 1300: 		$Str .= '&nbsp;'.$icons;
 1301: 	    }
 1302: 	    $Str .= '</td>';
 1303:         }
 1304:         $Str .= '</tr></table>'."\n";
 1305:         #
 1306:         # Return the @Crumbs stack to what we started with
 1307:         push(@Crumbs,$last);
 1308:         shift(@Crumbs);
 1309:         #
 1310:         return $Str;
 1311:     }
 1312: 
 1313:     sub clear_breadcrumbs {
 1314:         undef(@Crumbs);
 1315:     }
 1316: 
 1317:     sub add_breadcrumb {
 1318:         push (@Crumbs,@_);
 1319:     }
 1320: 
 1321: } # End of scope for @Crumbs
 1322: 
 1323: ############################################################
 1324: ############################################################
 1325: 
 1326: # Nested table routines.
 1327: #
 1328: # Routines to display form items in a multi-row table with 2 columns.
 1329: # Uses nested tables to divide form elements into segments.
 1330: # For examples of use see loncom/interface/lonnotify.pm 
 1331: #
 1332: # Can be used in following order: ...
 1333: # &start_pick_box()
 1334: # row1
 1335: # row2
 1336: # row3   ... etc.
 1337: # &submit_row(0
 1338: # &end_pick_box()
 1339: #
 1340: # where row1, row 2 etc. are chosen from &role_select_row,&course_select_row,
 1341: # &status_select_row and &email_default_row
 1342: #
 1343: # Can also be used in following order:
 1344: #
 1345: # &start_pick_box()
 1346: # &row_title()
 1347: # &row_closure()
 1348: # &row_title()
 1349: # &row_closure()  ... etc.
 1350: # &submit_row()
 1351: # &end_pick_box()
 1352: #
 1353: # In general a &submit_row() call should proceed the call to &end_pick_box(),
 1354: # as this routine adds a button for form submission.
 1355: # &submit_row() does not require a &row_closure after it.
 1356: #  
 1357: # &start_pick_box() creates a bounding table with 1-pixel wide black border.
 1358: # rows should be placed between calls to &start_pick_box() and &end_pick_box.
 1359: #
 1360: # &row_title() adds a title in the left column for each segment.
 1361: # &row_closure() closes a row with a 1-pixel wide black line.
 1362: #
 1363: # &role_select_row() provides a select box from which to choose 1 or more roles 
 1364: # &course_select_row provides ways of picking groups of courses
 1365: #    radio buttons: all, by category or by picking from a course picker pop-up
 1366: #      note: by category option is only displayed if a domain has implemented 
 1367: #                selection by year, semester, department, number etc.
 1368: #
 1369: # &status_select_row() provides a select box from which to choose 1 or more
 1370: #  access types (current access, prior access, and future access)  
 1371: #
 1372: # &email_default_row() provides text boxes for default e-mail suffixes for
 1373: #  different authentication types in a domain.
 1374: #
 1375: # &row_title() and &row_closure() are called internally by the &*_select_row
 1376: # routines, but can also be called directly to start and end rows which have 
 1377: # needs that are not accommodated by the *_select_row() routines.    
 1378: 
 1379: sub start_pick_box {
 1380:     my ($css_class) = @_;
 1381:     if (defined($css_class)) {
 1382: 	$css_class = 'class="'.$css_class.'"';
 1383:     } else {
 1384: 	$css_class= 'class="LC_pick_box"';
 1385:     }
 1386:     my $output = <<"END";
 1387:  <table $css_class>
 1388: END
 1389:     return $output;
 1390: }
 1391: 
 1392: sub end_pick_box {
 1393:     my $output = <<"END";
 1394:        </table>
 1395: END
 1396:     return $output;
 1397: }
 1398: 
 1399: sub row_title {
 1400:     my ($title,$css_title_class,$css_value_class) = @_;
 1401:     $css_title_class ||= 'LC_pick_box_title';
 1402:     $css_title_class = 'class="'.$css_title_class.'"';
 1403: 
 1404:     $css_value_class ||= 'LC_pick_box_value';
 1405:     $css_value_class = 'class="'.$css_value_class.'"';
 1406: 
 1407:     my $output = <<"ENDONE";
 1408:            <tr class="LC_pick_box_row">
 1409:             <td $css_title_class>
 1410: 	       $title:
 1411:             </td>
 1412:             <td $css_value_class>
 1413: ENDONE
 1414:     return $output;
 1415: }
 1416: 
 1417: sub row_closure {
 1418:     my ($no_separator) =@_;
 1419:     my $output = <<"ENDTWO";
 1420:             </td>
 1421:            </tr>
 1422: ENDTWO
 1423:     if (!$no_separator) {
 1424:         $output .= <<"ENDTWO";
 1425:            <tr>
 1426:             <td colspan="2" class="LC_pick_box_separator">
 1427:             </td>
 1428:            </tr>
 1429: ENDTWO
 1430:     }
 1431:     return $output;
 1432: }
 1433: 
 1434: sub role_select_row {
 1435:     my ($roles,$title,$css_class,$show_separate_custom,$cdom,$cnum) = @_;
 1436:     my $output;
 1437:     if (defined($title)) {
 1438:         $output = &row_title($title,$css_class);
 1439:     }
 1440:     $output .= qq|
 1441:                                   <select name="roles" multiple >\n|;
 1442:     foreach my $role (@$roles) {
 1443:         my $plrole;
 1444:         if ($role eq 'ow') {
 1445:             $plrole = &mt('Course Owner');
 1446:         } elsif ($role eq 'cr') {
 1447:             if ($show_separate_custom) {
 1448:                 if ($cdom ne '' && $cnum ne '') {
 1449:                     my %course_customroles = &course_custom_roles($cdom,$cnum);
 1450:                     foreach my $crrole (sort(keys(%course_customroles))) {
 1451:                         my ($plcrrole) = ($crrole =~ m|^cr/[^/]+/[^/]+/(.+)$|);
 1452:                         $output .= '  <option value="'.$crrole.'">'.$plcrrole.
 1453:                                    '</option>';
 1454:                     }
 1455:                 }
 1456:             } else {
 1457:                 $plrole = &mt('Custom Role');
 1458:             }
 1459:         } else {
 1460:             $plrole=&Apache::lonnet::plaintext($role);
 1461:         }
 1462:         if (($role ne 'cr') || (!$show_separate_custom)) {
 1463:             $output .= '  <option value="'.$role.'">'.$plrole.'</option>';
 1464:         }
 1465:     }
 1466:     $output .= qq|                </select>\n|;
 1467:     if (defined($title)) {
 1468:         $output .= &row_closure();
 1469:     }
 1470:     return $output;
 1471: }
 1472: 
 1473: sub course_select_row {
 1474:     my ($title,$formname,$totcodes,$codetitles,$idlist,$idlist_titles,
 1475: 	$css_class) = @_;
 1476:     my $output = &row_title($title,$css_class);
 1477:     $output .= &course_selection($formname,$totcodes,$codetitles,$idlist,$idlist_titles);
 1478:     $output .= &row_closure();
 1479:     return $output;
 1480: }
 1481: 
 1482: sub course_selection {
 1483:     my ($formname,$totcodes,$codetitles,$idlist,$idlist_titles) = @_;
 1484:     my $output = qq|
 1485: <script type="text/javascript">
 1486:     function coursePick (formname) {
 1487:         for  (var i=0; i<formname.coursepick.length; i++) {
 1488:             if (formname.coursepick[i].value == 'category') {
 1489:                 courseSet('');
 1490:             }
 1491:             if (!formname.coursepick[i].checked) {
 1492:                 if (formname.coursepick[i].value == 'specific') {
 1493:                     formname.coursetotal.value = 0;
 1494:                     formname.courselist = '';
 1495:                 }
 1496:             }
 1497:         }
 1498:     }
 1499:     function setPick (formname) {
 1500:         for  (var i=0; i<formname.coursepick.length; i++) {
 1501:             if (formname.coursepick[i].value == 'category') {
 1502:                 formname.coursepick[i].checked = true;
 1503:             }
 1504:             formname.coursetotal.value = 0;
 1505:             formname.courselist = '';
 1506:         }
 1507:     }
 1508: </script>
 1509:     |;
 1510:     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
 1511:                      ($formname,'pickcourse','pickdomain','coursedesc','',1).'</b>';
 1512:         $output .= '<input type="radio" name="coursepick" value="all" onclick="coursePick(this.form)" />'.&mt('All courses').'<br />';
 1513:     if ($totcodes > 0) {
 1514:         my $numtitles = @$codetitles;
 1515:         if ($numtitles > 0) {
 1516:             $output .= '<input type="radio" name="coursepick" value="category" onclick="coursePick(this.form);alert('."'".&mt('Choose categories, from left to right')."'".')" />'.&mt('Pick courses by category:').' <br />';
 1517:             $output .= '<table><tr><td>'.$$codetitles[0].'<br />'."\n".
 1518:                '<select name="'.$$codetitles[0].
 1519:                '" onChange="setPick(this.form);courseSet('."'$$codetitles[0]'".')">'."\n".
 1520:                ' <option value="-1" />Select'."\n";
 1521:             my @items = ();
 1522:             my @longitems = ();
 1523:             if ($$idlist{$$codetitles[0]} =~ /","/) {
 1524:                 @items = split(/","/,$$idlist{$$codetitles[0]});
 1525:             } else {
 1526:                 $items[0] = $$idlist{$$codetitles[0]};
 1527:             }
 1528:             if (defined($$idlist_titles{$$codetitles[0]})) {
 1529:                 if ($$idlist_titles{$$codetitles[0]} =~ /","/) {
 1530:                     @longitems = split(/","/,$$idlist_titles{$$codetitles[0]});
 1531:                 } else {
 1532:                     $longitems[0] = $$idlist_titles{$$codetitles[0]};
 1533:                 }
 1534:                 for (my $i=0; $i<@longitems; $i++) {
 1535:                     if ($longitems[$i] eq '') {
 1536:                         $longitems[$i] = $items[$i];
 1537:                     }
 1538:                 }
 1539:             } else {
 1540:                 @longitems = @items;
 1541:             }
 1542:             for (my $i=0; $i<@items; $i++) {
 1543:                 $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
 1544:             }
 1545:             $output .= '</select></td>';
 1546:             for (my $i=1; $i<$numtitles; $i++) {
 1547:                 $output .= '<td>'.$$codetitles[$i].'<br />'."\n".
 1548:                           '<select name="'.$$codetitles[$i].
 1549:                           '" onChange="courseSet('."'$$codetitles[$i]'".')">'."\n".
 1550:                           '<option value="-1">&lt;-Pick '.$$codetitles[$i-1].'</option>'."\n".
 1551:                           '</select>'."\n".
 1552:                           '</td>';
 1553:             }
 1554:             $output .= '</tr></table><br />';
 1555:         }
 1556:     }
 1557:     $output .= '<input type="radio" name="coursepick" value="specific" onclick="coursePick(this.form);opencrsbrowser('."'".$formname."','dccourse','dcdomain','coursedesc','','1'".')" />'.&mt('Pick specific course(s):').' '.$courseform.'&nbsp;&nbsp;<input type="text" value="0" size="4" name="coursetotal" /><input type="hidden" name="courselist" value="" />selected.<br />'."\n";
 1558:     return $output;
 1559: }
 1560: 
 1561: sub status_select_row {
 1562:     my ($types,$title,$css_class) = @_;
 1563:     my $output; 
 1564:     if (defined($title)) {
 1565:         $output = &row_title($title,$css_class,'LC_pick_box_select');
 1566:     }
 1567:     $output .= qq|
 1568:                                     <select name="types" multiple>\n|;
 1569:     foreach my $status_type (sort(keys(%{$types}))) {
 1570:         $output .= '  <option value="'.$status_type.'">'.$$types{$status_type}.'</option>';
 1571:     }
 1572:     $output .= qq|                   </select>\n|; 
 1573:     if (defined($title)) {
 1574:         $output .= &row_closure();
 1575:     }
 1576:     return $output;
 1577: }
 1578: 
 1579: sub email_default_row {
 1580:     my ($authtypes,$title,$descrip,$css_class) = @_;
 1581:     my $output = &row_title($title,$css_class);
 1582:     $output .= $descrip.
 1583: 	&Apache::loncommon::start_data_table().
 1584: 	&Apache::loncommon::start_data_table_header_row().
 1585: 	'<th>'.&mt('Authentication Method').'</th>'.
 1586: 	'<th align="right">'.&mt('Username -> e-mail conversion').'</th>'."\n".
 1587: 	&Apache::loncommon::end_data_table_header_row();
 1588:     my $rownum = 0;
 1589:     foreach my $auth (sort(keys(%{$authtypes}))) {
 1590:         my ($userentry,$size);
 1591:         if ($auth =~ /^krb/) {
 1592:             $userentry = '';
 1593:             $size = 25;
 1594:         } else {
 1595:             $userentry = 'username@';
 1596:             $size = 15;
 1597:         }
 1598:         $output .= &Apache::loncommon::start_data_table_row().
 1599: 	    '<td>  '.$$authtypes{$auth}.'</td>'.
 1600: 	    '<td align="right">'.$userentry.
 1601: 	    '<input type="text" name="'.$auth.'" size="'.$size.'" /></td>'.
 1602: 	    &Apache::loncommon::end_data_table_row();
 1603:     }
 1604:     $output .= &Apache::loncommon::end_data_table();
 1605:     $output .= &row_closure();
 1606:     return $output;
 1607: }
 1608: 
 1609: 
 1610: sub submit_row {
 1611:     my ($title,$cmd,$submit_text,$css_class) = @_;
 1612:     my $output = &row_title($title,$css_class,'LC_pick_box_submit');
 1613:     $output .= qq|
 1614:              <br />
 1615:              <input type="hidden" name="command" value="$cmd" />
 1616:              <input type="submit" value="$submit_text"/> &nbsp;
 1617:              <br /><br />
 1618:             \n|;
 1619:     return $output;
 1620: }
 1621: 
 1622: sub course_custom_roles {
 1623:     my ($cdom,$cnum) = @_;
 1624:     my %returnhash=();
 1625:     my %coursepersonnel=&Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
 1626:     foreach my $person (sort(keys(%coursepersonnel))) {
 1627:         my ($role) = ($person =~ /^([^:]+):/);
 1628:         my ($end,$start) = split(/:/,$coursepersonnel{$person});
 1629:         if ($end == -1 && $start == -1) {
 1630:             next;
 1631:         }
 1632:         if ($role =~ m|^cr/[^/]+/[^/]+/[^/]|) {
 1633:             $returnhash{$role} ++;
 1634:         }
 1635:     }
 1636:     return %returnhash;
 1637: }
 1638: 
 1639: 
 1640: ##############################################
 1641: ##############################################
 1642:                                                                              
 1643: # echo_form_input
 1644: #
 1645: # Generates html markup to add form elements from the referrer page
 1646: # as hidden form elements (values encoded) in the new page.
 1647: #
 1648: # Intended to support two types of use 
 1649: # (a) to allow backing up to earlier pages in a multi-page 
 1650: # form submission process using a breadcrumb trail.
 1651: #
 1652: # (b) to allow the current page to be reloaded with form elements
 1653: # set on previous page to remain unchanged.  An example would
 1654: # be where the a page containing a dynamically-built table of data is 
 1655: # is to be redisplayed, with only the sort order of the data changed. 
 1656: #  
 1657: # Inputs:
 1658: # 1. Reference to array of form elements in the submitted form on 
 1659: # the referrer page which are to be excluded from the echoed elements.
 1660: #
 1661: # 2. Reference to array of regular expressions, which if matched in the  
 1662: # name of the form element n the referrer page will be omitted from echo. 
 1663: #
 1664: # Outputs: A scalar containing the html markup for the echoed form
 1665: # elements (all as hidden elements, with values encoded). 
 1666: 
 1667: 
 1668: sub echo_form_input {
 1669:     my ($excluded,$regexps) = @_;
 1670:     my $output = '';
 1671:     foreach my $key (keys(%env)) {
 1672:         if ($key =~ /^form\.(.+)$/) {
 1673:             my $name = $1;
 1674:             my $match = 0;
 1675:             if ((!@{$excluded}) || (!grep/^$name$/,@{$excluded})) {
 1676:                 if (defined($regexps)) {
 1677:                     if (@{$regexps} > 0) {
 1678:                         foreach my $regexp (@{$regexps}) {
 1679:                             if ($name =~ /\Q$regexp\E/) {
 1680:                                 $match = 1;
 1681:                                 last;
 1682:                             }
 1683:                         }
 1684:                     }
 1685:                 }
 1686:                 if (!$match) {
 1687:                     if (ref($env{$key})) {
 1688:                         foreach my $value (@{$env{$key}}) {
 1689:                             $value = &HTML::Entities::encode($value,'<>&"');
 1690:                             $output .= '<input type="hidden" name="'.$name.
 1691:                                              '" value="'.$value.'" />'."\n";
 1692:                         }
 1693:                     } else {
 1694:                         my $value = &HTML::Entities::encode($env{$key},'<>&"');
 1695:                         $output .= '<input type="hidden" name="'.$name.
 1696:                                              '" value="'.$value.'" />'."\n";
 1697:                     }
 1698:                 }
 1699:             }
 1700:         }
 1701:     }
 1702:     return $output;
 1703: }
 1704: 
 1705: ##############################################
 1706: ##############################################
 1707:                                                                              
 1708: # set_form_elements
 1709: #
 1710: # Generates javascript to set form elements to values based on
 1711: # corresponding values for the same form elements when the page was
 1712: # previously submitted.
 1713: #     
 1714: # Last submission values are read from hidden form elements in referring 
 1715: # page which have the same name, i.e., generated by &echo_form_input(). 
 1716: #
 1717: # Intended to be called by onload event.
 1718: #
 1719: # Inputs:
 1720: # (a) Reference to hash of echoed form elements to be set.
 1721: #
 1722: # In the hash, keys are the form element names, and the values are the
 1723: # element type (selectbox, radio, checkbox or text -for textbox, textarea or
 1724: # hidden).
 1725: #
 1726: # (b) Optional reference to hash of stored elements to be set.
 1727: #
 1728: # If the page being displayed is a page which permits modification of
 1729: # previously stored data, e.g., the first page in a multi-page submission,
 1730: # then if stored is supplied, form elements will be set to the last stored
 1731: # values.  If user supplied values are also available for the same elements
 1732: # these will replace the stored values. 
 1733: #        
 1734: # Output:
 1735: #  
 1736: # javascript function - set_form_elements() which sets form elements,
 1737: # expects an argument: formname - the name of the form according to 
 1738: # the DOM, e.g., document.compose
 1739: 
 1740: sub set_form_elements {
 1741:     my ($elements,$stored) = @_;
 1742:     my %values;
 1743:     my $output .= 'function setFormElements(courseForm) {
 1744: ';
 1745:     if (defined($stored)) {
 1746:         foreach my $name (keys(%{$stored})) {
 1747:             if (exists($$elements{$name})) {
 1748:                 if (ref($$stored{$name}) eq 'ARRAY') {
 1749:                     $values{$name} = $$stored{$name};
 1750:                 } else {
 1751:                     @{$values{$name}} = ($$stored{$name});
 1752:                 }
 1753:             }
 1754:         }
 1755:     }
 1756: 
 1757:     foreach my $key (keys(%env)) {
 1758:         if ($key =~ /^form\.(.+)$/) {
 1759:             my $name = $1;
 1760:             if (exists($$elements{$name})) {
 1761:                 @{$values{$name}} = &Apache::loncommon::get_env_multiple($key);
 1762:             }
 1763:         }
 1764:     }
 1765: 
 1766:     foreach my $name (keys(%values)) {
 1767:         for (my $i=0; $i<@{$values{$name}}; $i++) {
 1768:             $values{$name}[$i] = &HTML::Entities::decode($values{$name}[$i],'<>&"');
 1769:             $values{$name}[$i] =~ s/([\r\n\f]+)/\\n/g;
 1770:             $values{$name}[$i] =~ s/"/\\"/g;
 1771:         }
 1772:         if ($$elements{$name} eq 'text') {
 1773:             my $numvalues = @{$values{$name}};
 1774:             if ($numvalues > 1) {
 1775:                 my $valuestring = join('","',@{$values{$name}});
 1776:                 $output .= qq|
 1777:   var textvalues = new Array ("$valuestring");
 1778:   var total = courseForm.elements['$name'].length;
 1779:   if (total > $numvalues) {
 1780:       total = $numvalues;
 1781:   }    
 1782:   for (var i=0; i<total; i++) {
 1783:       courseForm.elements['$name']\[i].value = textvalues[i];
 1784:   }
 1785: |;
 1786:             } else {
 1787:                 $output .= qq|
 1788:   courseForm.elements['$name'].value = "$values{$name}[0]";
 1789: |;
 1790:             }
 1791:         } else {
 1792:             $output .=  qq|
 1793:   var elementLength = courseForm.elements['$name'].length;
 1794:   if (elementLength==undefined) {
 1795: |;
 1796:             foreach my $value (@{$values{$name}}) {
 1797:                 if ($$elements{$name} eq 'selectbox') {
 1798:                     $output .=  qq|
 1799:       if (courseForm.elements['$name'].options[0].value == "$value") {
 1800:           courseForm.elements['$name'].options[0].selected = true;
 1801:       }|;
 1802:                 } elsif (($$elements{$name} eq 'radio') ||
 1803:                          ($$elements{$name} eq 'checkbox')) {
 1804:                     $output .= qq|
 1805:       if (courseForm.elements['$name'].value == "$value") {
 1806:           courseForm.elements['$name'].checked = true;
 1807:       }|;
 1808:                 }
 1809:             }
 1810:             $output .= qq|
 1811:   }
 1812:   else {
 1813:       for (var i=0; i<courseForm.elements['$name'].length; i++) {
 1814: |;
 1815:             if ($$elements{$name} eq 'selectbox') {
 1816:                 $output .=  qq|
 1817:           courseForm.elements['$name'].options[i].selected = false;|;
 1818:             } elsif (($$elements{$name} eq 'radio') || 
 1819:                      ($$elements{$name} eq 'checkbox')) {
 1820:                 $output .= qq|
 1821:           courseForm.elements['$name']\[i].checked = false;|; 
 1822:             }
 1823:             $output .= qq|
 1824:       }
 1825:       for (var j=0; j<courseForm.elements['$name'].length; j++) {
 1826: |;
 1827:             foreach my $value (@{$values{$name}}) {
 1828:                 if ($$elements{$name} eq 'selectbox') {
 1829:                     $output .=  qq|
 1830:           if (courseForm.elements['$name'].options[j].value == "$value") {
 1831:               courseForm.elements['$name'].options[j].selected = true;
 1832:           }|;
 1833:                 } elsif (($$elements{$name} eq 'radio') ||
 1834:                          ($$elements{$name} eq 'checkbox')) { 
 1835:                       $output .= qq|
 1836:           if (courseForm.elements['$name']\[j].value == "$value") {
 1837:               courseForm.elements['$name']\[j].checked = true;
 1838:           }|;
 1839:                 }
 1840:             }
 1841:             $output .= qq|
 1842:       }
 1843:   }
 1844: |;
 1845:         }
 1846:     }
 1847:     $output .= "
 1848: }\n";
 1849:     return $output;
 1850: }
 1851: 
 1852: ##############################################
 1853: ##############################################
 1854: 
 1855: # javascript_valid_email
 1856: #
 1857: # Generates javascript to validate an e-mail address.
 1858: # Returns a javascript function which accetps a form field as argumnent, and
 1859: # returns false if field.value does not satisfy two regular expression matches
 1860: # for a valid e-mail address.  Backwards compatible with old browsers without
 1861: # support for javascript RegExp (just checks for @ in field.value in this case). 
 1862: 
 1863: sub javascript_valid_email {
 1864:     my $scripttag .= <<'END';
 1865: function validmail(field) {
 1866:     var str = field.value;
 1867:     if (window.RegExp) {
 1868:         var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
 1869:         var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"; //"
 1870:         var reg1 = new RegExp(reg1str);
 1871:         var reg2 = new RegExp(reg2str);
 1872:         if (!reg1.test(str) && reg2.test(str)) {
 1873:             return true;
 1874:         }
 1875:         return false;
 1876:     }
 1877:     else
 1878:     {
 1879:         if(str.indexOf("@") >= 0) {
 1880:             return true;
 1881:         }
 1882:         return false;
 1883:     }
 1884: }
 1885: END
 1886:     return $scripttag;
 1887: }
 1888: 
 1889: 1;
 1890: 
 1891: __END__

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