Annotation of loncom/interface/lonhtmlcommon.pm, revision 1.123

1.2       www         1: # The LearningOnline Network with CAPA
                      2: # a pile of common html routines
                      3: #
1.123   ! albertel    4: # $Id: lonhtmlcommon.pm,v 1.122 2006/03/21 18:39:02 albertel Exp $
1.2       www         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: #
1.10      matthew    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: ######################################################################
1.2       www        55: 
1.1       stredwic   56: package Apache::lonhtmlcommon;
                     57: 
1.104     albertel   58: use strict;
1.10      matthew    59: use Time::Local;
1.47      sakharuk   60: use Time::HiRes;
1.30      www        61: use Apache::lonlocal;
1.104     albertel   62: use Apache::lonnet;
1.1       stredwic   63: 
1.40      www        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.
1.103     albertel   84: 		'"><img src="'.&Apache::loncommon::lonhttpdurl('/adm/lonMisc/bomb.gif').'" border="0" /></a>'.
1.40      www        85: 		&Apache::loncommon::help_open_topic('About_Bombs');
                     86: 	}
                     87:     }
                     88:     return '';
                     89: }
1.26      matthew    90: 
                     91: ##############################################
                     92: ##############################################
                     93: 
1.41      www        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);
1.111     www       103:     if (scalar(keys(%recent))>20) {
1.41      www       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: 
1.89      banghart  121: sub remove_recent {
                    122:     my ($area,$names)=@_;
                    123:     my $file=&recent_filename($area);
                    124:     return &Apache::lonnet::del($file,$names);
                    125: }
                    126: 
1.41      www       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'".
1.96      albertel  131: 	($event?" onchange='$event'":'').
1.41      www       132: 	">\n<option value=''>--- ".&mt('Recent')." ---</option>";
                    133:     foreach (sort keys %recent) {
                    134: 	unless ($_=~/^error\:/) {
1.94      foxr      135: 	    my $escaped = &Apache::loncommon::escape_url($_);
                    136: 	    $return.="\n<option value='$escaped'>".
1.41      www       137: 		&Apache::lonnet::unescape((split(/\&/,$recent{$_}))[1]).
                    138: 		'</option>';
                    139: 	}
                    140:     }
                    141:     $return.="\n</select>\n";
                    142:     return $return;
                    143: }
                    144: 
1.97      albertel  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: 
1.41      www       169: 
1.26      matthew   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 {
1.68      matthew   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 .= ' />';
1.26      matthew   207:     return $Str;
                    208: }
                    209: 
1.120     albertel  210: 
                    211: =pod
                    212: 
                    213: =item radiobutton
                    214: 
                    215: =cut
                    216: 
                    217: ##############################################
                    218: ##############################################
                    219: sub radio {
                    220:     my ($name,$checked,$value) = @_;
                    221:     my $Str = '<input type="radio" name="'.$name.'" ';
                    222:     if (defined($value)) {
                    223:         $Str .= 'value="'.$value.'"';
                    224:     } 
                    225:     if ($checked eq $value) {
                    226:         $Str .= ' checked="1"';
                    227:     }
                    228:     $Str .= ' />';
                    229:     return $Str;
                    230: }
                    231: 
1.10      matthew   232: ##############################################
                    233: ##############################################
                    234: 
                    235: =pod
                    236: 
                    237: =item &date_setter
                    238: 
1.22      matthew   239: &date_setter returns html and javascript for a compact date-setting form.
                    240: To retrieve values from it, use &get_date_from_form().
                    241: 
1.10      matthew   242: Inputs
                    243: 
                    244: =over 4
                    245: 
                    246: =item $dname 
                    247: 
                    248: The name to prepend to the form elements.  
                    249: The form elements defined will be dname_year, dname_month, dname_day,
                    250: dname_hour, dname_min, and dname_sec.
                    251: 
                    252: =item $currentvalue
                    253: 
                    254: The current setting for this time parameter.  A unix format time
                    255: (time in seconds since the beginning of Jan 1st, 1970, GMT.  
                    256: An undefined value is taken to indicate the value is the current time.
                    257: Also, to be explicit, a value of 'now' also indicates the current time.
                    258: 
1.26      matthew   259: =item $special
                    260: 
                    261: Additional html/javascript to be associated with each element in
                    262: the date_setter.  See lonparmset for example usage.
                    263: 
1.59      matthew   264: =item $includeempty 
                    265: 
                    266: =item $state
                    267: 
                    268: Specifies the initial state of the form elements.  Either 'disabled' or empty.
                    269: Defaults to empty, which indiciates the form elements are not disabled. 
                    270: 
1.22      matthew   271: =back
                    272: 
                    273: Bugs
                    274: 
                    275: The method used to restrict user input will fail in the year 2400.
                    276: 
1.10      matthew   277: =cut
                    278: 
                    279: ##############################################
                    280: ##############################################
                    281: sub date_setter {
1.67      matthew   282:     my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
1.108     www       283:         $no_hh_mm_ss,$defhour,$defmin,$defsec) = @_;
1.107     www       284:     my $wasdefined=1;
1.59      matthew   285:     if (! defined($state) || $state ne 'disabled') {
                    286:         $state = '';
                    287:     }
1.67      matthew   288:     if (! defined($no_hh_mm_ss)) {
                    289:         $no_hh_mm_ss = 0;
                    290:     }
1.110     www       291:     if ($currentvalue eq 'now') {
                    292: 	$currentvalue=time;
                    293:     }
                    294:     if ((!defined($currentvalue)) || ($currentvalue eq '')) {
                    295: 	$wasdefined=0;
                    296: 	if ($includeempty) {
                    297: 	    $currentvalue = 0;
                    298: 	} else {
1.39      www       299: 	    $currentvalue = time;
                    300: 	}
1.10      matthew   301:     }
                    302:     # other potentially useful values:     wkday,yrday,is_daylight_savings
1.65      albertel  303:     my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
1.39      www       304:     if ($currentvalue) {
                    305: 	($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
                    306: 	    localtime($currentvalue);
                    307: 	$year += 1900;
                    308:     }
1.107     www       309:     unless ($wasdefined) {
1.110     www       310: 	if (($defhour) || ($defmin) || ($defsec)) {
                    311: 	    ($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
                    312: 		localtime(time);
                    313: 	    $year += 1900;
                    314: 	    $sec=($defsec?$defsec:0);
                    315: 	    $min=($defmin?$defmin:0);
                    316: 	    $hour=($defhour?$defhour:0);
                    317: 	} elsif (!$includeempty) {
                    318: 	    $sec=0;
                    319: 	    $min=0;
                    320: 	    $hour=0;
                    321: 	}
1.107     www       322:     }
1.10      matthew   323:     my $result = "\n<!-- $dname date setting form -->\n";
                    324:     $result .= <<ENDJS;
                    325: <script language="Javascript">
                    326:     function $dname\_checkday() {
                    327:         var day   = document.$formname.$dname\_day.value;
                    328:         var month = document.$formname.$dname\_month.value;
                    329:         var year  = document.$formname.$dname\_year.value;
                    330:         var valid = true;
                    331:         if (day < 1) {
                    332:             document.$formname.$dname\_day.value = 1;
                    333:         } 
                    334:         if (day > 31) {
                    335:             document.$formname.$dname\_day.value = 31;
                    336:         }
                    337:         if ((month == 1)  || (month == 3)  || (month == 5)  ||
                    338:             (month == 7)  || (month == 8)  || (month == 10) ||
                    339:             (month == 12)) {
                    340:             if (day > 31) {
                    341:                 document.$formname.$dname\_day.value = 31;
                    342:                 day = 31;
                    343:             }
                    344:         } else if (month == 2 ) {
                    345:             if ((year % 4 == 0) && (year % 100 != 0)) {
                    346:                 if (day > 29) {
                    347:                     document.$formname.$dname\_day.value = 29;
                    348:                 }
                    349:             } else if (day > 29) {
                    350:                 document.$formname.$dname\_day.value = 28;
                    351:             }
                    352:         } else if (day > 30) {
                    353:             document.$formname.$dname\_day.value = 30;
                    354:         }
                    355:     }
1.95      matthew   356:     
1.59      matthew   357:     function $dname\_disable() {
                    358:         document.$formname.$dname\_month.disabled=true;
                    359:         document.$formname.$dname\_day.disabled=true;
                    360:         document.$formname.$dname\_year.disabled=true;
                    361:         document.$formname.$dname\_hour.disabled=true;
                    362:         document.$formname.$dname\_minute.disabled=true;
                    363:         document.$formname.$dname\_second.disabled=true;
                    364:     }
                    365: 
                    366:     function $dname\_enable() {
                    367:         document.$formname.$dname\_month.disabled=false;
                    368:         document.$formname.$dname\_day.disabled=false;
                    369:         document.$formname.$dname\_year.disabled=false;
                    370:         document.$formname.$dname\_hour.disabled=false;
                    371:         document.$formname.$dname\_minute.disabled=false;
                    372:         document.$formname.$dname\_second.disabled=false;        
                    373:     }
                    374: 
1.29      www       375:     function $dname\_opencalendar() {
1.59      matthew   376:         if (! document.$formname.$dname\_month.disabled) {
                    377:             var calwin=window.open(
1.29      www       378: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
                    379: document.$formname.$dname\_month.value+"&year="+
                    380: document.$formname.$dname\_year.value,
                    381:              "LONCAPAcal",
                    382:               "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
1.59      matthew   383:         }
1.29      www       384: 
                    385:     }
1.10      matthew   386: </script>
                    387: ENDJS
1.95      matthew   388:     $result .= '  <nobr>';
1.96      albertel  389:     my $monthselector = qq{<select name="$dname\_month" $special $state onchange="javascript:$dname\_checkday()" >};
1.67      matthew   390:     # Month
1.10      matthew   391:     my @Months = qw/January February  March     April   May      June 
                    392:                     July    August    September October November December/;
                    393:     # Pad @Months with a bogus value to make indexing easier
                    394:     unshift(@Months,'If you can read this an error occurred');
1.95      matthew   395:     if ($includeempty) { $monthselector.="<option value=''></option>"; }
1.10      matthew   396:     for(my $m = 1;$m <=$#Months;$m++) {
1.95      matthew   397:         $monthselector .= qq{      <option value="$m" };
                    398:         $monthselector .= "selected " if ($m-1 eq $month);
                    399:         $monthselector .= '> '.&mt($Months[$m]).' </option>';
1.10      matthew   400:     }
1.95      matthew   401:     $monthselector.= '  </select>';
1.67      matthew   402:     # Day
1.96      albertel  403:     my $dayselector = qq{<input type="text" name="$dname\_day" $state value="$mday" size="3" $special onchange="javascript:$dname\_checkday()" />};
1.67      matthew   404:     # Year
1.96      albertel  405:     my $yearselector = qq{<input type="year" name="$dname\_year" $state value="$year" size="5" $special onchange="javascript:$dname\_checkday()" />};
1.95      matthew   406:     #
                    407:     my $hourselector = qq{<select name="$dname\_hour" $special $state >};
                    408:     if ($includeempty) { 
                    409:         $hourselector.=qq{<option value=''></option>};
                    410:     }
                    411:     for (my $h = 0;$h<24;$h++) {
                    412:         $hourselector .= qq{<option value="$h" };
                    413:         $hourselector .= "selected " if (defined($hour) && $hour == $h);
                    414:         $hourselector .= ">";
                    415:         my $timest='';
                    416:         if ($h == 0) {
                    417:             $timest .= "12 am";
                    418:         } elsif($h == 12) {
                    419:             $timest .= "12 noon";
                    420:         } elsif($h < 12) {
                    421:             $timest .= "$h am";
                    422:         } else {
                    423:             $timest .= $h-12 ." pm";
                    424:         }
                    425:         $timest=&mt($timest);
                    426:         $hourselector .= $timest." </option>\n";
                    427:     }
                    428:     $hourselector .= "  </select>\n";
                    429:     my $minuteselector = qq{<input type="text" name="$dname\_minute" $special $state value="$min" size="3" />};
                    430:     my $secondselector= qq{<input type="text" name="$dname\_second" $special $state value="$sec" size="3" />};
                    431:     my $cal_link = qq{<a href="javascript:$dname\_opencalendar()">};
                    432:     #
                    433:     if ($no_hh_mm_ss) {
                    434:         $result .= &mt('[_1] [_2] [_3] [_4]Select Date[_5]',
                    435:                        $monthselector,$dayselector,$yearselector,
                    436:                        $cal_link,'</a>');
                    437:     } else {
                    438:         $result .= &mt('[_1] [_2] [_3] [_4] [_5]m [_6]s [_7]Select Date[_8]',
                    439:                        $monthselector,$dayselector,$yearselector,
                    440:                        $hourselector,$minuteselector,$secondselector,
                    441:                        $cal_link,'</a>');
1.67      matthew   442:     }
1.95      matthew   443:     $result .= "</nobr>\n<!-- end $dname date setting form -->\n";
1.10      matthew   444:     return $result;
                    445: }
                    446: 
                    447: ##############################################
                    448: ##############################################
                    449: 
1.22      matthew   450: =pod
                    451: 
1.10      matthew   452: =item &get_date_from_form
1.22      matthew   453: 
                    454: get_date_from_form retrieves the date specified in an &date_setter form.
1.10      matthew   455: 
                    456: Inputs:
                    457: 
                    458: =over 4
                    459: 
                    460: =item $dname
                    461: 
                    462: The name passed to &datesetter, which prefixes the form elements.
                    463: 
                    464: =item $defaulttime
                    465: 
                    466: The unix time to use as the default in case of poor inputs.
                    467: 
                    468: =back
                    469: 
                    470: Returns: Unix time represented in the form.
                    471: 
                    472: =cut
                    473: 
                    474: ##############################################
                    475: ##############################################
                    476: sub get_date_from_form {
                    477:     my ($dname) = @_;
                    478:     my ($sec,$min,$hour,$day,$month,$year);
                    479:     #
1.104     albertel  480:     if (defined($env{'form.'.$dname.'_second'})) {
                    481:         my $tmpsec = $env{'form.'.$dname.'_second'};
1.10      matthew   482:         if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
                    483:             $sec = $tmpsec;
                    484:         }
1.64      albertel  485: 	if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
1.67      matthew   486:     } else {
                    487:         $sec = 0;
1.10      matthew   488:     }
1.104     albertel  489:     if (defined($env{'form.'.$dname.'_minute'})) {
                    490:         my $tmpmin = $env{'form.'.$dname.'_minute'};
1.10      matthew   491:         if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
                    492:             $min = $tmpmin;
                    493:         }
1.64      albertel  494: 	if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
1.67      matthew   495:     } else {
                    496:         $min = 0;
1.10      matthew   497:     }
1.104     albertel  498:     if (defined($env{'form.'.$dname.'_hour'})) {
                    499:         my $tmphour = $env{'form.'.$dname.'_hour'};
1.33      matthew   500:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
1.10      matthew   501:             $hour = $tmphour;
                    502:         }
1.67      matthew   503:     } else {
                    504:         $hour = 0;
1.10      matthew   505:     }
1.104     albertel  506:     if (defined($env{'form.'.$dname.'_day'})) {
                    507:         my $tmpday = $env{'form.'.$dname.'_day'};
1.10      matthew   508:         if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
                    509:             $day = $tmpday;
                    510:         }
                    511:     }
1.104     albertel  512:     if (defined($env{'form.'.$dname.'_month'})) {
                    513:         my $tmpmonth = $env{'form.'.$dname.'_month'};
1.10      matthew   514:         if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
                    515:             $month = $tmpmonth - 1;
                    516:         }
                    517:     }
1.104     albertel  518:     if (defined($env{'form.'.$dname.'_year'})) {
                    519:         my $tmpyear = $env{'form.'.$dname.'_year'};
1.10      matthew   520:         if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
                    521:             $year = $tmpyear - 1900;
                    522:         }
                    523:     }
1.24      www       524:     if (($year<70) || ($year>137)) { return undef; }
1.33      matthew   525:     if (defined($sec) && defined($min)   && defined($hour) &&
                    526:         defined($day) && defined($month) && defined($year) &&
                    527:         eval(&timelocal($sec,$min,$hour,$day,$month,$year))) {
1.10      matthew   528:         return &timelocal($sec,$min,$hour,$day,$month,$year);
                    529:     } else {
                    530:         return undef;
                    531:     }
1.20      matthew   532: }
                    533: 
                    534: ##############################################
                    535: ##############################################
                    536: 
                    537: =pod
                    538: 
                    539: =item &pjump_javascript_definition()
                    540: 
                    541: Returns javascript defining the 'pjump' function, which opens up a
                    542: parameter setting wizard.
                    543: 
                    544: =cut
                    545: 
                    546: ##############################################
                    547: ##############################################
                    548: sub pjump_javascript_definition {
                    549:     my $Str = <<END;
1.109     www       550:     function pjump(type,dis,value,marker,ret,call,hour,min,sec) {
1.20      matthew   551:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
                    552:                  +"&value="+escape(value)+"&marker="+escape(marker)
                    553:                  +"&return="+escape(ret)
1.109     www       554:                  +"&call="+escape(call)+"&name="+escape(dis)
                    555:                  +"&defhour="+escape(hour)+"&defmin="+escape(min)
                    556:                  +"&defsec="+escape(sec),"LONCAPAparms",
1.20      matthew   557:                  "height=350,width=350,scrollbars=no,menubar=no");
                    558:     }
                    559: END
                    560:     return $Str;
1.10      matthew   561: }
                    562: 
                    563: ##############################################
                    564: ##############################################
1.17      matthew   565: 
                    566: =pod
                    567: 
                    568: =item &javascript_nothing()
                    569: 
                    570: Return an appropriate null for the users browser.  This is used
                    571: as the first arguement for window.open calls when you want a blank
                    572: window that you can then write to.
                    573: 
                    574: =cut
                    575: 
                    576: ##############################################
                    577: ##############################################
                    578: sub javascript_nothing {
                    579:     # mozilla and other browsers work with "''", but IE on mac does not.
                    580:     my $nothing = "''";
                    581:     my $user_browser;
                    582:     my $user_os;
1.104     albertel  583:     $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
                    584:     $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
1.17      matthew   585:     if (! defined($user_browser) || ! defined($user_os)) {
                    586:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    587:                            &Apache::loncommon::decode_user_agent();
                    588:     }
                    589:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                    590:         $nothing = "'javascript:void(0);'";
                    591:     }
                    592:     return $nothing;
                    593: }
                    594: 
1.90      www       595: ##############################################
                    596: ##############################################
                    597: sub javascript_docopen {
                    598:     # safari does not understand document.open() and loads "text/html"
                    599:     my $nothing = "''";
                    600:     my $user_browser;
                    601:     my $user_os;
1.104     albertel  602:     $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
                    603:     $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
1.90      www       604:     if (! defined($user_browser) || ! defined($user_os)) {
                    605:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    606:                            &Apache::loncommon::decode_user_agent();
                    607:     }
                    608:     if ($user_browser eq 'safari' && $user_os =~ 'mac') {
                    609:         $nothing = "document.clear()";
                    610:     } else {
                    611: 	$nothing = "document.open('text/html','replace')";
                    612:     }
                    613:     return $nothing;
                    614: }
                    615: 
1.21      matthew   616: 
1.17      matthew   617: ##############################################
                    618: ##############################################
                    619: 
1.21      matthew   620: =pod
1.17      matthew   621: 
1.21      matthew   622: =item &StatusOptions()
1.10      matthew   623: 
1.21      matthew   624: Returns html for a selection box which allows the user to choose the
                    625: enrollment status of students.  The selection box name is 'Status'.
1.6       stredwic  626: 
1.21      matthew   627: Inputs:
1.6       stredwic  628: 
1.21      matthew   629: $status: the currently selected status.  If undefined the value of
1.104     albertel  630: $env{'form.Status'} is taken.  If that is undefined, a value of 'Active'
1.21      matthew   631: is used.
1.6       stredwic  632: 
1.21      matthew   633: $formname: The name of the form.  If defined the onchange attribute of
                    634: the selection box is set to document.$formname.submit().
1.6       stredwic  635: 
1.21      matthew   636: $size: the size (number of lines) of the selection box.
1.6       stredwic  637: 
1.27      matthew   638: $onchange: javascript to use when the value is changed.  Enclosed in 
                    639: double quotes, ""s, not single quotes.
                    640: 
1.21      matthew   641: Returns: a perl string as described.
1.1       stredwic  642: 
1.21      matthew   643: =cut
1.9       stredwic  644: 
1.21      matthew   645: ##############################################
                    646: ##############################################
                    647: sub StatusOptions {
1.27      matthew   648:     my ($status, $formName,$size,$onchange)=@_;
1.21      matthew   649:     $size = 1 if (!defined($size));
                    650:     if (! defined($status)) {
                    651:         $status = 'Active';
1.104     albertel  652:         $status = $env{'form.Status'} if (exists($env{'form.Status'}));
1.9       stredwic  653:     }
1.1       stredwic  654: 
                    655:     my $OpSel1 = '';
                    656:     my $OpSel2 = '';
                    657:     my $OpSel3 = '';
                    658: 
                    659:     if($status eq 'Any')         { $OpSel3 = ' selected'; }
                    660:     elsif($status eq 'Expired' ) { $OpSel2 = ' selected'; }
                    661:     else                         { $OpSel1 = ' selected'; }
                    662: 
                    663:     my $Str = '';
                    664:     $Str .= '<select name="Status"';
1.27      matthew   665:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
1.1       stredwic  666:         $Str .= ' onchange="document.'.$formName.'.submit()"';
1.27      matthew   667:     }
                    668:     if (defined($onchange)) {
                    669:         $Str .= ' onchange="'.$onchange.'"';
1.1       stredwic  670:     }
1.21      matthew   671:     $Str .= ' size="'.$size.'" ';
1.1       stredwic  672:     $Str .= '>'."\n";
1.21      matthew   673:     $Str .= '<option value="Active" '.$OpSel1.'>'.
1.37      www       674:         &mt('Currently Enrolled').'</option>'."\n";
1.21      matthew   675:     $Str .= '<option value="Expired" '.$OpSel2.'>'.
1.37      www       676:         &mt('Previously Enrolled').'</option>'."\n";
1.21      matthew   677:     $Str .= '<option value="Any" '.$OpSel3.'>'.
1.37      www       678:         &mt('Any Enrollment Status').'</option>'."\n";
1.1       stredwic  679:     $Str .= '</select>'."\n";
1.7       stredwic  680: }
1.12      matthew   681: 
                    682: ########################################################
                    683: ########################################################
1.7       stredwic  684: 
1.23      matthew   685: =pod
                    686: 
                    687: =item Progess Window Handling Routines
                    688: 
                    689: These routines handle the creation, update, increment, and closure of 
                    690: progress windows.  The progress window reports to the user the number
                    691: of items completed and an estimate of the time required to complete the rest.
                    692: 
                    693: =over 4
                    694: 
                    695: 
                    696: =item &Create_PrgWin
                    697: 
                    698: Writes javascript to the client to open a progress window and returns a
                    699: data structure used for bookkeeping.
                    700: 
                    701: Inputs
                    702: 
                    703: =over 4
                    704: 
                    705: =item $r Apache request
                    706: 
                    707: =item $title The title of the progress window
                    708: 
                    709: =item $heading A description (usually 1 line) of the process being initiated.
                    710: 
                    711: =item $number_to_do The total number of items being processed.
1.50      albertel  712: 
                    713: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
                    714:        specified)
                    715: 
1.51      albertel  716: =item $width Specify the width in charaters of the input field.
                    717: 
1.50      albertel  718: =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
                    719: 
                    720: =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 
1.23      matthew   721: 
                    722: =back
                    723: 
                    724: Returns a hash containing the progress state data structure.
                    725: 
                    726: 
                    727: =item &Update_PrgWin
                    728: 
                    729: Updates the text in the progress indicator.  Does not increment the count.
                    730: See &Increment_PrgWin.
                    731: 
                    732: Inputs:
                    733: 
                    734: =over 4
                    735: 
                    736: =item $r Apache request
                    737: 
                    738: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
                    739: 
                    740: =item $displaystring The string to write to the status indicator
                    741: 
                    742: =back
                    743: 
                    744: Returns: none
                    745: 
                    746: 
                    747: =item Increment_PrgWin
                    748: 
                    749: Increment the count of items completed for the progress window by 1.  
                    750: 
                    751: Inputs:
                    752: 
                    753: =over 4
                    754: 
                    755: =item $r Apache request
                    756: 
                    757: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    758: 
                    759: =item $extraInfo A description of the items being iterated over.  Typically
                    760: 'student'.
                    761: 
                    762: =back
                    763: 
                    764: Returns: none
                    765: 
                    766: 
                    767: =item Close_PrgWin
                    768: 
                    769: Closes the progress window.
                    770: 
                    771: Inputs:
                    772: 
                    773: =over 4 
                    774: 
                    775: =item $r Apache request
                    776: 
                    777: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    778: 
                    779: =back
                    780: 
                    781: Returns: none
                    782: 
                    783: =back
                    784: 
                    785: =cut
                    786: 
                    787: ########################################################
                    788: ########################################################
                    789: 
1.51      albertel  790: my $uniq=0;
                    791: sub get_uniq_name {
                    792:     $uniq++;
                    793:     return 'uniquename'.$uniq;
                    794: }
                    795: 
1.7       stredwic  796: # Create progress
                    797: sub Create_PrgWin {
1.51      albertel  798:     my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
                    799: 	$inputname)=@_;
1.49      albertel  800:     if (!defined($type)) { $type='popup'; }
1.51      albertel  801:     if (!defined($width)) { $width=55; }
1.49      albertel  802:     my %prog_state;
                    803:     $prog_state{'type'}=$type;
                    804:     if ($type eq 'popup') {
                    805: 	$prog_state{'window'}='popwin';
1.122     albertel  806: 	my $start_page =
                    807: 	    &Apache::loncommon::start_page($title,undef,
                    808: 					   {'only_body' => 1,
                    809: 					    'bgcolor'   => '#88DDFF',
                    810: 					    'js_ready'  => 1});
                    811: 	my $end_page = &Apache::loncommon::end_page({'js_ready'  => 1});
                    812: 
1.49      albertel  813: 	#the whole function called through timeout is due to issues
                    814: 	#in mozilla Read BUG #2665 if you want to know the whole story
1.122     albertel  815: 	&r_print($r,'<script type="text/javascript">'.
1.49      albertel  816:         "var popwin;
                    817:          function openpopwin () {
                    818:          popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
1.122     albertel  819:         "popwin.document.writeln(\'".$start_page.
1.48      albertel  820:               "<h4>$heading</h4>".
                    821:               "<form name=popremain>".
1.51      albertel  822:               '<input type="text" size="'.$width.'" name="remaining" value="'.
1.122     albertel  823: 	      &mt('Starting').'"></form>'.$end_page.
                    824:               "\');".
1.49      albertel  825:         "popwin.document.close();}".
                    826:         "\nwindow.setTimeout(openpopwin,0)</script>");
                    827: 	$prog_state{'formname'}='popremain';
                    828: 	$prog_state{'inputname'}="remaining";
                    829:     } elsif ($type eq 'inline') {
                    830: 	$prog_state{'window'}='window';
                    831: 	if (!$formname) {
1.51      albertel  832: 	    $prog_state{'formname'}=&get_uniq_name();
                    833: 	    &r_print($r,'<form name="'.$prog_state{'formname'}.'">');
1.49      albertel  834: 	} else {
                    835: 	    $prog_state{'formname'}=$formname;
                    836: 	}
                    837: 	if (!$inputname) {
1.51      albertel  838: 	    $prog_state{'inputname'}=&get_uniq_name();
1.56      albertel  839: 	    &r_print($r,$heading.' <input type="text" name="'.$prog_state{'inputname'}.
1.51      albertel  840: 		     '" size="'.$width.'" />');
1.49      albertel  841: 	} else {
                    842: 	    $prog_state{'inputname'}=$inputname;
                    843: 	    
                    844: 	}
                    845: 	if (!$formname) { &r_print($r,'</form>'); }
                    846: 	&Update_PrgWin($r,\%prog_state,&mt('Starting'));
                    847:     }
1.7       stredwic  848: 
1.16      albertel  849:     $prog_state{'done'}=0;
1.23      matthew   850:     $prog_state{'firststart'}=&Time::HiRes::time();
                    851:     $prog_state{'laststart'}=&Time::HiRes::time();
1.16      albertel  852:     $prog_state{'max'}=$number_to_do;
1.49      albertel  853:     
1.14      albertel  854:     return %prog_state;
1.7       stredwic  855: }
                    856: 
                    857: # update progress
                    858: sub Update_PrgWin {
1.14      albertel  859:     my ($r,$prog_state,$displayString)=@_;
1.49      albertel  860:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
                    861: 	     $$prog_state{'formname'}.'.'.
                    862: 	     $$prog_state{'inputname'}.'.value="'.
1.48      albertel  863: 	     $displayString.'";</script>');
1.23      matthew   864:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.14      albertel  865: }
                    866: 
                    867: # increment progress state
                    868: sub Increment_PrgWin {
                    869:     my ($r,$prog_state,$extraInfo)=@_;
1.16      albertel  870:     $$prog_state{'done'}++;
1.23      matthew   871:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
                    872:         $$prog_state{'done'} *
1.16      albertel  873: 	($$prog_state{'max'}-$$prog_state{'done'});
                    874:     $time_est = int($time_est);
1.80      matthew   875:     #
                    876:     my $min = int($time_est/60);
                    877:     my $sec = $time_est % 60;
                    878:     # 
                    879:     my $str;
1.91      albertel  880:     if ($min == 0 && $sec > 1) {
1.80      matthew   881:         $str = '[_2] seconds';
1.91      albertel  882:     } elsif ($min == 1 && $sec > 1) {
                    883:         $str = '1 minute [_2] seconds';
1.80      matthew   884:     } elsif ($min == 1 && $sec < 2) {
                    885:         $str = '1 minute';
                    886:     } elsif ($min < 10 && $sec > 1) {
                    887:         $str = '[_1] minutes, [_2] seconds';
1.81      matthew   888:     } elsif ($min >= 10 || $sec < 2) {
1.80      matthew   889:         $str = '[_1] minutes';
1.16      albertel  890:     }
1.80      matthew   891:     $time_est = &mt($str,$min,$sec);
                    892:     #
1.23      matthew   893:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
                    894:     if ($lasttime > 9) {
                    895:         $lasttime = int($lasttime);
                    896:     } elsif ($lasttime < 0.01) {
                    897:         $lasttime = 0;
                    898:     } else {
                    899:         $lasttime = sprintf("%3.2f",$lasttime);
                    900:     }
1.19      matthew   901:     if ($lasttime == 1) {
1.32      www       902:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
1.19      matthew   903:     } else {
1.32      www       904:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
1.28      matthew   905:     }
                    906:     #
1.104     albertel  907:     my $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
                    908:     my $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
1.28      matthew   909:     if (! defined($user_browser) || ! defined($user_os)) {
                    910:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    911:                            &Apache::loncommon::decode_user_agent();
                    912:     }
                    913:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                    914:         $lasttime = '';
1.19      matthew   915:     }
1.49      albertel  916:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
                    917: 	     $$prog_state{'formname'}.'.'.
                    918: 	     $$prog_state{'inputname'}.'.value="'.
1.48      albertel  919: 	     $$prog_state{'done'}.'/'.$$prog_state{'max'}.
                    920: 	     ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
1.23      matthew   921:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.7       stredwic  922: }
                    923: 
                    924: # close Progress Line
                    925: sub Close_PrgWin {
1.14      albertel  926:     my ($r,$prog_state)=@_;
1.49      albertel  927:     if ($$prog_state{'type'} eq 'popup') {
                    928: 	&r_print($r,'<script>popwin.close()</script>'."\n");
                    929:     } elsif ($$prog_state{'type'} eq 'inline') {
                    930: 	&Update_PrgWin($r,$prog_state,&mt('Done'));
                    931:     }
1.48      albertel  932:     undef(%$prog_state);
                    933: }
                    934: 
                    935: sub r_print {
                    936:     my ($r,$to_print)=@_;
                    937:     if ($r) {
                    938: 	$r->print($to_print);
                    939: 	$r->rflush();
1.47      sakharuk  940:     } else {
1.48      albertel  941: 	print($to_print);
1.47      sakharuk  942:     }
1.1       stredwic  943: }
1.34      www       944: 
                    945: # ------------------------------------------------------- Puts directory header
                    946: 
                    947: sub crumbs {
1.78      www       948:     my ($uri,$target,$prefix,$form,$size,$noformat)=@_;
1.62      matthew   949:     if (! defined($size)) {
                    950:         $size = '+2';
                    951:     }
1.100     raeburn   952:     if ($target) {
                    953:         $target = ' target="'.
                    954:                   &Apache::loncommon::escape_single($target).'"';
                    955:     }
1.78      www       956:     my $output='';
                    957:     unless ($noformat) { $output.='<br /><tt><b>'; }
                    958:     $output.='<font size="'.$size.'">'.$prefix.'/';
1.104     albertel  959:     if ($env{'user.adv'}) {
1.43      www       960: 	my $path=$prefix.'/';
1.99      matthew   961: 	foreach my $dir (split('/',$uri)) {
                    962:             if (! $dir) { next; }
                    963:             $path .= $dir;
1.43      www       964: 	    unless ($path eq $uri) { $path.='/'; }
1.99      matthew   965:             my $linkpath = &Apache::loncommon::escape_single($path);
1.98      matthew   966:             if ($form) {
1.99      matthew   967: 		$linkpath=
                    968:                     qq{javascript:$form.action='$linkpath';$form.submit();};
1.98      matthew   969:             }
1.99      matthew   970: 	    $output.=qq{<a href="$linkpath" $target>$dir</a>/};
1.35      www       971: 	}
                    972:     } else {
                    973: 	$output.=$uri;
1.34      www       974:     }
1.36      www       975:     unless ($uri=~/\/$/) { $output=~s/\/$//; }
1.78      www       976:     return $output.'</font>'.($noformat?'':'</b></tt><br />');
1.34      www       977: }
                    978: 
1.85      www       979: # --------------------- A function that generates a window for the spellchecker
                    980: 
                    981: sub spellheader {
1.123   ! albertel  982:     my $start_page=
        !           983: 	&Apache::loncommon::start_page('Speller Suggestions',undef,
        !           984: 				       {'only_body' => 1,
        !           985: 					'js_ready'  => 1,
        !           986: 					'bgcolor'   => '#DDDDDD',});
        !           987:     my $end_page=
        !           988: 	&Apache::loncommon::end_page({'js_ready'  => 1}); 
        !           989: 
1.105     www       990:     my $nothing=&javascript_nothing();
1.85      www       991:     return (<<ENDCHECK);
                    992: <script type="text/javascript"> 
1.92      albertel  993: //<!-- BEGIN LON-CAPA Internal
1.85      www       994: var checkwin;
                    995: 
                    996: function spellcheckerwindow() {
1.105     www       997:     checkwin=window.open($nothing,'spellcheckwin','height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
1.123   ! albertel  998:     checkwin.document.writeln('$start_page<form name="spellcheckform" action="/adm/spellcheck" method="post"><input type="hidden" name="text" value="" /></form>$end_page');
1.85      www       999:     checkwin.document.close();
                   1000: }
1.92      albertel 1001: // END LON-CAPA Internal -->
1.85      www      1002: </script>
                   1003: ENDCHECK
                   1004: }
                   1005: 
                   1006: # ---------------------------------- Generate link to spell checker for a field
                   1007: 
                   1008: sub spelllink {
                   1009:     my ($form,$field)=@_;
                   1010:     my $linktext=&mt('Check Spelling');
                   1011:     return (<<ENDLINK);
1.105     www      1012: <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>
1.85      www      1013: ENDLINK
                   1014: }
                   1015: 
1.52      www      1016: # ------------------------------------------------- Output headers for HTMLArea
                   1017: 
                   1018: sub htmlareaheaders {
1.71      www      1019:     if (&htmlareablocked()) { return ''; }
1.76      www      1020:     unless (&htmlareabrowser()) { return ''; }
1.52      www      1021:     my $lang='en';
1.71      www      1022:     if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
                   1023: 	$lang=&mt('htmlarea_lang');
                   1024:     }
1.52      www      1025:     return (<<ENDHEADERS);
1.61      www      1026: <script type="text/javascript">
1.73      www      1027: _editor_url='/htmlarea/';
                   1028: _editor_lang='$lang';
1.61      www      1029: </script>
1.52      www      1030: <script type="text/javascript" src="/htmlarea/htmlarea.js"></script>
                   1031: ENDHEADERS
                   1032: }
                   1033: 
1.74      www      1034: # ------------------------------------------------- Activate additional buttons
                   1035: 
                   1036: sub htmlareaaddbuttons {
                   1037:     if (&htmlareablocked()) { return ''; }
1.76      www      1038:     unless (&htmlareabrowser()) { return ''; }
1.74      www      1039:     return (<<ENDADDBUTTON);
                   1040:     var config=new HTMLArea.Config();
                   1041:     config.registerButton('ed_math','LaTeX Inline',
                   1042: 			  '/htmlarea/images/ed_math.gif',false,
                   1043: 			    function(editor,id) {
1.88      albertel 1044: 			      editor.surroundHTML('&nbsp;<m>\$','\$</m>&nbsp;');
1.74      www      1045: 			    }
                   1046: 			  );
                   1047:     config.registerButton('ed_math_eqn','LaTeX Equation',
                   1048: 			  '/htmlarea/images/ed_math_eqn.gif',false,
                   1049: 			    function(editor,id) {
1.75      www      1050: 			      editor.surroundHTML(
1.88      albertel 1051: 				     '&nbsp;\\n<center><m>\\\\[','\\\\]</m></center>\\n&nbsp;');
1.74      www      1052: 			    }
                   1053: 			  );
                   1054:     config.toolbar.push(['ed_math','ed_math_eqn']);
                   1055: ENDADDBUTTON
                   1056: }
1.76      www      1057: 
                   1058: # ----------------------------------------------------------------- Preferences
                   1059: 
                   1060: sub disablelink {
1.77      www      1061:     my @fields=@_;
                   1062:     if (defined($#fields)) {
                   1063: 	unless ($#fields>=0) { return ''; }
                   1064:     }
1.93      albertel 1065:     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>';
1.76      www      1066: }
                   1067: 
                   1068: sub enablelink {
1.77      www      1069:     my @fields=@_;
                   1070:     if (defined($#fields)) {
                   1071: 	unless ($#fields>=0) { return ''; }
                   1072:     }
1.93      albertel 1073:     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>';
1.76      www      1074: }
                   1075: 
1.72      www      1076: # ----------------------------------------- Script to activate only some fields
                   1077: 
                   1078: sub htmlareaselectactive {
1.73      www      1079:     my @fields=@_;
1.76      www      1080:     unless (&htmlareabrowser()) { return ''; }
1.77      www      1081:     if (&htmlareablocked()) { return '<br />'.&enablelink(@fields); }
1.74      www      1082:     my $output='<script type="text/javascript" defer="1">'.
                   1083: 	&htmlareaaddbuttons();
1.73      www      1084:     foreach(@fields) {
1.74      www      1085: 	$output.="\nHTMLArea.replace('$_',config);";
1.72      www      1086:     }
1.76      www      1087:     $output.="\nwindow.status='Activated Editfields';\n</script><br />".
1.77      www      1088: 	&disablelink(@fields);
1.72      www      1089:     return $output;
                   1090: }
                   1091: 
1.61      www      1092: # --------------------------------------------------------------------- Blocked
                   1093: 
                   1094: sub htmlareablocked {
1.104     albertel 1095:     unless ($env{'environment.wysiwygeditor'} eq 'on') { return 1; }
1.71      www      1096:     return 0;
1.52      www      1097: }
                   1098: 
                   1099: # ---------------------------------------- Browser capable of running HTMLArea?
                   1100: 
                   1101: sub htmlareabrowser {
                   1102:     return 1;
                   1103: }
1.53      matthew  1104: 
                   1105: ############################################################
                   1106: ############################################################
                   1107: 
                   1108: =pod
                   1109: 
                   1110: =item breadcrumbs
                   1111: 
                   1112: Compiles the previously registered breadcrumbs into an series of links.
                   1113: FAQ and BUG links will be placed on the left side of the table if they
                   1114: are defined for the last registered breadcrumb.  
                   1115: Additionally supports a 'component', which will be displayed on the
                   1116: right side of the table (without a link).
                   1117: A link to help for the component will be included if one is specified.
                   1118: 
                   1119: All inputs can be undef without problems.
                   1120: 
                   1121: Inputs: $color (the background color of the table returned),
                   1122:         $component (the large text on the right side of the table),
                   1123:         $component_help
1.63      albertel 1124:         $function (role to get colors from)
                   1125:         $domain   (domian of role)
                   1126:         $menulink (boolean, controls whether to include a link to /adm/menu)
1.53      matthew  1127: 
                   1128: Returns a string containing breadcrumbs for the current page.
                   1129: 
                   1130: =item clear_breadcrumbs
                   1131: 
                   1132: Clears the previously stored breadcrumbs.
                   1133: 
                   1134: =item add_breadcrumb
                   1135: 
                   1136: Pushes a breadcrumb on the stack of crumbs.
                   1137: 
                   1138: input: $breadcrumb, a hash reference.  The keys 'href','title', and 'text'
                   1139: are required.  If present the keys 'faq' and 'bug' will be used to provide
                   1140: links to the FAQ and bug sites.
                   1141: 
                   1142: returns: nothing    
                   1143: 
                   1144: =cut
                   1145: 
                   1146: ############################################################
                   1147: ############################################################
                   1148: {
                   1149:     my @Crumbs;
1.57      matthew  1150:     
1.53      matthew  1151:     sub breadcrumbs {
1.87      albertel 1152:         my ($color,$component,$component_help,$function,$domain,$menulink,
                   1153: 	    $helplink) = @_;
1.55      matthew  1154:         if (! defined($color)) {
                   1155:             if (! defined($function)) {
                   1156:                 $function = &Apache::loncommon::get_users_function();
                   1157:             }
                   1158:             $color = &Apache::loncommon::designparm($function.'.tabbg',
                   1159:                                                     $domain);
                   1160:         }
1.53      matthew  1161:         #
                   1162:         my $Str = "\n".
                   1163:             '<table width="100%" border="0" cellpadding="0" cellspacing="0">'.
                   1164:             '<tr><td bgcolor="'.$color.'">'.
                   1165:             '<font size="-1">';
1.57      matthew  1166:         #
                   1167:         # Make the faq and bug data cascade
                   1168:         my $faq = '';
                   1169:         my $bug = '';
1.106     www      1170: 	my $help='';
1.60      www      1171:         # The last breadcrumb does not have a link, so handle it separately.
1.53      matthew  1172:         my $last = pop(@Crumbs);
1.57      matthew  1173:         #
1.70      matthew  1174:         # The first one should be the course or a menu link
1.63      albertel 1175: 	if (!defined($menulink)) { $menulink=1; }
1.70      matthew  1176:         if ($menulink) {
                   1177:             my $description = 'Menu';
1.104     albertel 1178:             if (exists($env{'request.course.id'}) && 
                   1179:                 $env{'request.course.id'} ne '') {
1.70      matthew  1180:                 $description = 
1.104     albertel 1181:                     $env{'course.'.$env{'request.course.id'}.'.description'};
1.70      matthew  1182:             }
1.57      matthew  1183:             unshift(@Crumbs,{
1.70      matthew  1184:                     href   =>'/adm/menu',
                   1185:                     title  =>'Go to main menu',
                   1186:                     target =>'_top',
                   1187:                     text   =>$description,
                   1188:                 });
1.53      matthew  1189:         }
                   1190:         my $links .= 
                   1191:             join('-&gt;',
                   1192:                  map {
1.57      matthew  1193:                      $faq = $_->{'faq'} if (exists($_->{'faq'}));
                   1194:                      $bug = $_->{'bug'} if (exists($_->{'bug'}));
1.106     www      1195:                      $help = $_->{'help'} if (exists($_->{'help'}));
1.69      matthew  1196:                      my $result = '<a href="'.$_->{'href'}.'" ';
                   1197:                      if (defined($_->{'target'}) && $_->{'target'} ne '') {
                   1198:                          $result .= 'target="'.$_->{'target'}.'" ';
                   1199:                      }
                   1200:                      $result .='title="'.&mt($_->{'title'}).'">'.
                   1201:                          &mt($_->{'text'}).'</a>';
                   1202:                      $result;
1.53      matthew  1203:                      } @Crumbs
                   1204:                  );
                   1205:         $links .= '-&gt;' if ($links ne '');
1.82      albertel 1206:         $links .= '<b>'.&mt($last->{'text'}).'</b>';
1.54      matthew  1207:         #
                   1208:         my $icons = '';
1.57      matthew  1209:         $faq = $last->{'faq'} if (exists($last->{'faq'}));
                   1210:         $bug = $last->{'bug'} if (exists($last->{'bug'}));
1.106     www      1211:         $help = $last->{'help'} if (exists($last->{'help'}));
                   1212:         $component_help=($component_help?$component_help:$help);
1.79      raeburn  1213: #        if ($faq ne '') {
                   1214: #            $icons .= &Apache::loncommon::help_open_faq($faq);
                   1215: #        }
                   1216: #        if ($bug ne '') {
                   1217: #            $icons .= &Apache::loncommon::help_open_bug($bug);
                   1218: #        }
1.87      albertel 1219: 	if ($helplink ne 'nohelp') {
                   1220: 	    $icons .= &Apache::loncommon::help_open_menu($color,$component,$component_help,$function,$faq,$bug);
                   1221: 	}
1.54      matthew  1222:         if ($icons ne '') {
                   1223:             $Str .= $icons.'&nbsp;';
1.53      matthew  1224:         }
1.54      matthew  1225:         #
1.53      matthew  1226:         $Str .= $links.'</font></td>';
1.54      matthew  1227:         #
1.53      matthew  1228:         if (defined($component)) {
                   1229:             $Str .= '<td align="right" bgcolor="'.$color.'">'.
1.83      raeburn  1230:                 '<font size="+1">'.&mt($component).'</font></td>';
1.53      matthew  1231:         }
                   1232:         $Str .= '</tr></table>'."\n";
                   1233:         #
                   1234:         # Return the @Crumbs stack to what we started with
                   1235:         push(@Crumbs,$last);
                   1236:         shift(@Crumbs);
                   1237:         #
                   1238:         return $Str;
                   1239:     }
                   1240: 
                   1241:     sub clear_breadcrumbs {
                   1242:         undef(@Crumbs);
                   1243:     }
                   1244: 
                   1245:     sub add_breadcrumb {
                   1246:         push (@Crumbs,@_);
                   1247:     }
                   1248: 
1.57      matthew  1249: } # End of scope for @Crumbs
1.53      matthew  1250: 
                   1251: ############################################################
                   1252: ############################################################
                   1253: 
1.112     raeburn  1254: # Nested table routines.
                   1255: #
                   1256: # Routines to display form items in a multi-row table with 2 columns.
                   1257: # Uses nested tables to divide form elements into segments.
                   1258: # For examples of use see loncom/interface/lonnotify.pm 
                   1259: #
                   1260: # Can be used in following order: ...
                   1261: # &start_pick_box()
                   1262: # row1
                   1263: # row2
                   1264: # row3   ... etc.
                   1265: # &submit_row(0
                   1266: # &end_pickbox()
                   1267: #
                   1268: # where row1, row 2 etc. are chosen from &role_select_row,&course_select_row,
                   1269: # &status_select_row and &email_default_row
                   1270: #
                   1271: # Can also be used in following order:
                   1272: #
                   1273: # &start_pick_box()
                   1274: # &row_title()
                   1275: # &row_closure()
                   1276: # &row_title()
                   1277: # &row_closure()  ... etc.
                   1278: # &submit_row()
                   1279: # &end_pick_box()
                   1280: #
                   1281: # In general a &submit_row() call should proceed the call to &end_pick_box(),
                   1282: # as this routine adds a button for form submission.
1.113     raeburn  1283: # &submit_row() does not require a &row_closure after it.
1.112     raeburn  1284: #  
                   1285: # &start_pick_box() creates a bounding table with 1-pixel wide black border.
                   1286: # rows should be placed between calls to &start_pick_box() and &end_pick_box.
                   1287: #
                   1288: # &row_title() adds a title in the left column for each segment.
                   1289: # &row_closure() closes a row with a 1-pixel wide black line.
                   1290: #
                   1291: # &role_select_row() provides a select box from which to choose 1 or more roles 
                   1292: # &course_select_row provides ways of picking groups of courses
                   1293: #    radio buttons: all, by category or by picking from a course picker pop-up
                   1294: #      note: by category option is only displayed if a domain has implemented 
                   1295: #                selection by year, semester, department, number etc.
                   1296: #
                   1297: # &status_select_row() provides a select box from which to choose 1 or more
                   1298: #  access types (current access, prior access, and future access)  
                   1299: #
                   1300: # &email_default_row() provides text boxes for default e-mail suffixes for
                   1301: #  different authentication types in a domain.
                   1302: #
                   1303: # &row_title() and &row_closure() are called internally by the &*_select_row
                   1304: # routines, but can also be called directly to start and end rows which have 
                   1305: # needs that are not accommodated by the *_select_row() routines.    
                   1306: 
                   1307: sub start_pick_box {
                   1308:     my ($table_width) = @_;
                   1309:     my $output = <<"END";
                   1310:  <table width="$table_width" border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">
                   1311:   <tr>
                   1312:       <td>
                   1313:        <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff">
                   1314:         <tr>
                   1315:          <td>
                   1316:           <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#ffffff">
                   1317: END
                   1318:     return $output;
                   1319: }
                   1320: 
                   1321: sub end_pick_box {
                   1322:     my $output = <<"END";
                   1323:        </table>
                   1324:       </td>
                   1325:      </tr>
                   1326:     </table>
                   1327:    </td>
                   1328:   </tr>
                   1329:  </table>
                   1330: END
                   1331:     return $output;
                   1332: }
                   1333: 
                   1334: sub row_title {
                   1335:     my ($col_width,$tablecolor,$title) = @_;
                   1336:     my $output = <<"ENDONE";
                   1337:            <tr>
                   1338:             <td width="$col_width" bgcolor="$tablecolor">
                   1339:              <table width="$col_width" border="0" cellpadding="8" cellspacing="0">
                   1340:               <tr>
                   1341:                <td align="right"><b>$title:</b>
                   1342:                </td>
                   1343:               </tr>
                   1344:              </table>
                   1345:             </td>
                   1346:             <td width="100%" valign="top">
                   1347:              <table width="100%" border="0" cellpadding="8" cellspacing="0">
                   1348:               <tr>
                   1349: ENDONE
                   1350:     return $output;
                   1351: }
                   1352: 
                   1353: sub row_closure {
1.113     raeburn  1354:     my $output = <<"ENDTWO";
1.112     raeburn  1355:               </tr>
                   1356:              </table>
                   1357:             </td>
                   1358:            </tr>
                   1359:            <tr>
                   1360:             <td width="100%" colspan="2" bgcolor="#000000">
                   1361:              <img src="/adm/lonMisc/blackdot.gif" /><br />
                   1362:             </td>
                   1363:            </tr>
                   1364: ENDTWO
                   1365:     return $output;
                   1366: }
                   1367: 
                   1368: sub role_select_row {
                   1369:     my ($roles,$col_width,$tablecolor,$title) = @_;
1.116     raeburn  1370:     my $output;
                   1371:     if (defined($title)) {
                   1372:         $output = &row_title($col_width,$tablecolor,$title);
                   1373:     }
1.119     raeburn  1374:     $output .= qq|               <td valign="top">
1.112     raeburn  1375:                                   <select name="roles" multiple >\n|;
1.113     raeburn  1376:     foreach my $role (@$roles) {
1.114     raeburn  1377:         my $plrole;
                   1378:         if ($role eq 'ow') {
                   1379:             $plrole = &mt('Course Owner');
                   1380:         } else {
                   1381:             $plrole=&Apache::lonnet::plaintext($role);
                   1382:         }
1.113     raeburn  1383:         $output .= '  <option value="'.$role.'">'.$plrole.'</option>';
1.112     raeburn  1384:     }
                   1385:     $output .= qq|                </select>
                   1386:                                  </td>\n|;
1.116     raeburn  1387:     if (defined($title)) {
                   1388:         $output .= &row_closure();
                   1389:     }
1.112     raeburn  1390:     return $output;
                   1391: }
                   1392: 
                   1393: sub course_select_row {
                   1394:     my ($col_width,$tablecolor,$title,$formname,$totcodes,$codetitles,$idlist,$idlist_titles) = @_;
                   1395:     my $output = &row_title($col_width,$tablecolor,$title);
                   1396:     $output .= "          <td>\n";
                   1397:     $output .= qq|
                   1398: <script type="text/javascript" language="Javascript" >
                   1399:     function coursePick (formname) {
                   1400:         for  (var i=0; i<formname.coursepick.length; i++) {
1.114     raeburn  1401:             if (formname.coursepick[i].value == 'category') {
                   1402:                 courseSet('');
                   1403:             }
1.112     raeburn  1404:             if (!formname.coursepick[i].checked) {
                   1405:                 if (formname.coursepick[i].value == 'specific') {
                   1406:                     formname.coursetotal.value = 0;
                   1407:                     formname.courselist = '';
                   1408:                 }
                   1409:             }
                   1410:         }
                   1411:     }
1.114     raeburn  1412:     function setPick (formname) {
                   1413:         for  (var i=0; i<formname.coursepick.length; i++) {
                   1414:             if (formname.coursepick[i].value == 'category') {
                   1415:                 formname.coursepick[i].checked = true;
                   1416:             }
                   1417:             formname.coursetotal.value = 0;
                   1418:             formname.courselist = '';
                   1419:         }
                   1420:     }
1.112     raeburn  1421: </script>
                   1422:     |;
                   1423:     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
1.118     raeburn  1424:                      ($formname,'pickcourse','pickdomain','coursedesc','',1).'</b>';
1.112     raeburn  1425:     if ($totcodes > 0) {
                   1426:         $output .= '<input type="radio" name="coursepick" value="all" onclick="coursePick(this.form)" />'.&mt('All courses');
                   1427:         my $numtitles = @$codetitles;
                   1428:         if ($numtitles > 0) {
                   1429:             $output .= '<br /><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 />';
                   1430:             $output .= '<table><tr><td>'.$$codetitles[0].'<br />'."\n".
                   1431:                '<select name="'.$$codetitles[0].
1.114     raeburn  1432:                '" onChange="setPick(this.form);courseSet('."'$$codetitles[0]'".')">'."\n".
1.112     raeburn  1433:                ' <option value="-1" />Select'."\n";
                   1434:             my @items = ();
                   1435:             my @longitems = ();
                   1436:             if ($$idlist{$$codetitles[0]} =~ /","/) {
1.113     raeburn  1437:                 @items = split(/","/,$$idlist{$$codetitles[0]});
1.112     raeburn  1438:             } else {
                   1439:                 $items[0] = $$idlist{$$codetitles[0]};
                   1440:             }
                   1441:             if (defined($$idlist_titles{$$codetitles[0]})) {
                   1442:                 if ($$idlist_titles{$$codetitles[0]} =~ /","/) {
1.113     raeburn  1443:                     @longitems = split(/","/,$$idlist_titles{$$codetitles[0]});
1.112     raeburn  1444:                 } else {
                   1445:                     $longitems[0] = $$idlist_titles{$$codetitles[0]};
                   1446:                 }
                   1447:                 for (my $i=0; $i<@longitems; $i++) {
                   1448:                     if ($longitems[$i] eq '') {
                   1449:                         $longitems[$i] = $items[$i];
                   1450:                     }
                   1451:                 }
                   1452:             } else {
                   1453:                 @longitems = @items;
                   1454:             }
                   1455:             for (my $i=0; $i<@items; $i++) {
                   1456:                 $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
                   1457:             }
                   1458:             $output .= '</select></td>';
                   1459:             for (my $i=1; $i<$numtitles; $i++) {
                   1460:                 $output .= '<td>'.$$codetitles[$i].'<br />'."\n".
                   1461:                           '<select name="'.$$codetitles[$i].
                   1462:                           '" onChange="courseSet('."'$$codetitles[$i]'".')">'."\n".
                   1463:                           '<option value="-1">&lt;-Pick '.$$codetitles[$i-1].'</option>'."\n".
                   1464:                           '</select>'."\n".
                   1465:                           '</td>';
                   1466:             }
                   1467:             $output .= '</tr></table><br />';
                   1468:         }
                   1469:     }
1.118     raeburn  1470:     $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 /></td>'."\n";
1.112     raeburn  1471:     $output .= &row_closure();
                   1472:     return $output;
                   1473: }
                   1474: 
                   1475: sub status_select_row {
                   1476:     my ($types,$col_width,$tablecolor,$title) = @_;
1.117     raeburn  1477:     my $output; 
                   1478:     if (defined($title)) {
                   1479:         $output = &row_title($col_width,$tablecolor,$title);
                   1480:     }
1.119     raeburn  1481:     $output .= qq|              <td valign="top">
1.112     raeburn  1482:                                     <select name="types" multiple>\n|;
1.113     raeburn  1483:     foreach my $status_type (sort(keys(%{$types}))) {
1.112     raeburn  1484:         $output .= '  <option value="'.$status_type.'">'.$$types{$status_type}.'</option>';
                   1485:     }
                   1486:     $output .= qq|                   </select>
                   1487:                                     </td>\n|; 
1.117     raeburn  1488:     if (defined($title)) {
                   1489:         $output .= &row_closure();
                   1490:     }
1.112     raeburn  1491:     return $output;
                   1492: }
                   1493: 
                   1494: sub email_default_row {
                   1495:     my ($authtypes,$col_width,$tablecolor,$title,$descrip) = @_;
                   1496:     my $output = &row_title($col_width,$tablecolor,$title);
                   1497:     my @rowcols = ('#eeeeee','#dddddd');
1.113     raeburn  1498:     $output .= '              <td>'.$descrip;
1.115     albertel 1499:     $output .= &start_pick_box(''); 
1.113     raeburn  1500:     $output .= '                <tr bgcolor="'.$tablecolor.'">
                   1501:                                  <td><b>'.&mt('Authentication Method').'</b></td><td align="right"><b>'.&mt('Username -> e-mail conversion').'</b></td>
1.112     raeburn  1502:                                 </tr>'."\n";
                   1503:     my $rownum = 0;
1.113     raeburn  1504:     foreach my $auth (sort(keys(%{$authtypes}))) {
1.112     raeburn  1505:         my ($userentry,$size);
                   1506:         my $rowiter = $rownum%2;
                   1507:         if ($auth =~ /^krb/) {
                   1508:             $userentry = '';
                   1509:             $size = 25;
                   1510:         } else {
                   1511:             $userentry = 'username@';
                   1512:             $size = 15;
                   1513:         }
1.113     raeburn  1514:         $output .= '<tr bgcolor="'.$rowcols[$rowiter].'"><td>  '.$$authtypes{$auth}.'</td><td align="right">'.$userentry.'<input type="text" name="'.$auth.'" size="'.$size.'" /></td></tr>';
1.112     raeburn  1515:         $rownum ++;
                   1516:     }
1.113     raeburn  1517:     $output .= &end_pick_box();
                   1518:     $output .= "                   <br /></td>\n"; 
1.112     raeburn  1519:     $output .= &row_closure();
                   1520:     return $output;
                   1521: }
                   1522: 
                   1523: 
                   1524: sub submit_row {
                   1525:     my ($col_width,$tablecolor,$title,$cmd,$submit_text) = @_;
1.113     raeburn  1526:     my $output = &row_title($col_width,$tablecolor,$title);
1.112     raeburn  1527:     $output .= qq|
                   1528:             <td width="100%" valign="top" align="right">
                   1529:              <br />
                   1530:              <input type="hidden" name="command" value="$cmd" />
                   1531:              <input type="submit" value="$submit_text"/> &nbsp;
                   1532:              <br /><br />
                   1533:             </td>\n|;
                   1534:     return $output;
                   1535: }
1.1       stredwic 1536: 
1.119     raeburn  1537: ##############################################
                   1538: ##############################################
                   1539:                                                                              
                   1540: # echo_form_input
                   1541: #
                   1542: # Generates html markup to add form elements from the referrer page
                   1543: # as hidden form elements (values encoded) in the new page.
                   1544: #
                   1545: # Intended to support two types of use 
                   1546: # (a) to allow backing up to earlier pages in a multi-page 
                   1547: # form submission process using a breadcrumb trail.
                   1548: #
                   1549: # (b) to allow the current page to be reloaded with form elements
                   1550: # set on previous page to remain unchanged.  An example would
                   1551: # be where the a page containing a dynamically-built table of data is 
                   1552: # is to be redisplayed, with only the sort order of the data changed. 
                   1553: #  
                   1554: # Inputs:
                   1555: # 1. Reference to array of form elements in the submitted form on 
                   1556: # the referrer page which are to be excluded from the echoed elements.
                   1557: #
                   1558: # 2. Reference to array of regular expressions, which if matched in the  
                   1559: # name of the form element n the referrer page will be omitted from echo. 
                   1560: #
                   1561: # Outputs: A scalar containing the html markup for the echoed form
                   1562: # elements (all as hidden elements, with values encoded). 
                   1563: 
                   1564: 
                   1565: sub echo_form_input {
                   1566:     my ($excluded,$regexps) = @_;
                   1567:     my $output = '';
                   1568:     foreach my $key (keys(%env)) {
                   1569:         if ($key =~ /^form\.(.+)$/) {
                   1570:             my $name = $1;
                   1571:             my $match = 0;
                   1572:             if ((!@{$excluded}) || (!grep/^$name$/,@{$excluded})) {
                   1573:                 if (defined($regexps)) {
                   1574:                     if (@{$regexps} > 0) {
                   1575:                         foreach my $regexp (@{$regexps}) {
                   1576:                             if ($name =~ /\Q$regexp\E/) {
                   1577:                                 $match = 1;
                   1578:                                 last;
                   1579:                             }
                   1580:                         }
                   1581:                     }
                   1582:                 }
                   1583:                 if (!$match) {
                   1584:                     if (ref($env{$key})) {
                   1585:                         foreach my $value (@{$env{$key}}) {
                   1586:                             $value = &HTML::Entities::encode($value,'<>&"');
                   1587:                             $output .= '<input type="hidden" name="'.$name.
                   1588:                                              '" value="'.$value.'" />'."\n";
                   1589:                         }
                   1590:                     } else {
                   1591:                         my $value = &HTML::Entities::encode($env{$key},'<>&"');
                   1592:                         $output .= '<input type="hidden" name="'.$name.
                   1593:                                              '" value="'.$value.'" />'."\n";
                   1594:                     }
                   1595:                 }
                   1596:             }
                   1597:         }
                   1598:     }
                   1599:     return $output;
                   1600: }
                   1601: 
                   1602: ##############################################
                   1603: ##############################################
                   1604:                                                                              
                   1605: # set_form_elements
                   1606: #
                   1607: # Generates javascript to set form elements to values based on
                   1608: # corresponding values for the same form elements when the page was
                   1609: # previously submitted.
                   1610: #     
                   1611: # Last submission values are read from hidden form elements in referring 
                   1612: # page which have the same name, i.e., generated by &echo_form_input(). 
                   1613: #
                   1614: # Intended to be called by onload event.
                   1615: #
1.121     raeburn  1616: # Inputs:
                   1617: # (a) Reference to hash of echoed form elements to be set.
1.119     raeburn  1618: #
                   1619: # In the hash, keys are the form element names, and the values are the
                   1620: # element type (selectbox, radio, checkbox or text -for textbox, textarea or
                   1621: # hidden).
1.121     raeburn  1622: #
                   1623: # (b) Optional reference to hash of stored elements to be set.
                   1624: #
                   1625: # If the page being displayed is a page which permits modification of
                   1626: # previously stored data, e.g., the first page in a multi-page submission,
                   1627: # then if stored is supplied, form elements will be set to the last stored
                   1628: # values.  If user supplied values are also available for the same elements
                   1629: # these will replace the stored values. 
                   1630: #        
1.119     raeburn  1631: # Output:
                   1632: #  
                   1633: # javascript function - set_form_elements() which sets form elements,
                   1634: # expects an argument: formname - the name of the form according to 
                   1635: # the DOM, e.g., document.compose
                   1636: 
                   1637: sub set_form_elements {
1.121     raeburn  1638:     my ($elements,$stored) = @_;
                   1639:     my %values;
1.119     raeburn  1640:     my $output .= 'function setFormElements(courseForm) {
1.121     raeburn  1641: ';
                   1642:     if (defined($stored)) {
                   1643:         foreach my $name (keys(%{$stored})) {
                   1644:             if (exists($$elements{$name})) {
                   1645:                 if (ref($$stored{$name}) eq 'ARRAY') {
                   1646:                     $values{$name} = $$stored{$name};
                   1647:                 } else {
                   1648:                     @{$values{$name}} = ($$stored{$name});
                   1649:                 }
                   1650:             }
                   1651:         }
                   1652:     }
                   1653: 
1.119     raeburn  1654:     foreach my $key (keys(%env)) {
                   1655:         if ($key =~ /^form\.(.+)$/) {
                   1656:             my $name = $1;
                   1657:             if (exists($$elements{$name})) {
1.121     raeburn  1658:                 @{$values{$name}} = &Apache::loncommon::get_env_multiple($key);
                   1659:             }
                   1660:         }
                   1661:     }
                   1662: 
                   1663:     foreach my $name (keys(%values)) {
                   1664:         for (my $i=0; $i<@{$values{$name}}; $i++) {
                   1665:             $values{$name}[$i] = &HTML::Entities::decode($values{$name}[$i],'<>&"');
                   1666:             $values{$name}[$i] =~ s/([\r\n\f]+)/\\n/g;
                   1667:             $values{$name}[$i] =~ s/"/\\"/g;
                   1668:         }
                   1669:         if ($$elements{$name} eq 'text') {
                   1670:             my $numvalues = @{$values{$name}};
                   1671:             if ($numvalues > 1) {
                   1672:                 my $valuestring = join('","',@{$values{$name}});
                   1673:                 $output .= qq|
1.119     raeburn  1674:   var textvalues = new Array ("$valuestring");
                   1675:   var total = courseForm.$name.length;
                   1676:   if (total > $numvalues) {
                   1677:       total = $numvalues;
                   1678:   }    
                   1679:   for (var i=0; i<total; i++) {
                   1680:       courseForm.$name\[i].value = textvalues[i];
                   1681:   }
                   1682: |;
1.121     raeburn  1683:             } else {
                   1684:                 $output .= qq|
                   1685:   courseForm.$name.value = "$values{$name}[0]";
1.119     raeburn  1686: |;
1.121     raeburn  1687:             }
                   1688:         } else {
                   1689:             $output .=  qq|
1.119     raeburn  1690:   var elementLength = courseForm.$name.length;
                   1691:   if (elementLength==undefined) {
                   1692: |;
1.121     raeburn  1693:             foreach my $value (@{$values{$name}}) {
                   1694:                 if ($$elements{$name} eq 'selectbox') {
                   1695:                     $output .=  qq|
1.119     raeburn  1696:       if (courseForm.$name.options[0].value == "$value") {
                   1697:           courseForm.$name.options[0].selected = true;
                   1698:       }|;
1.121     raeburn  1699:                 } elsif (($$elements{$name} eq 'radio') ||
                   1700:                          ($$elements{$name} eq 'checkbox')) {
                   1701:                     $output .= qq|
1.119     raeburn  1702:       if (courseForm.$name.value == "$value") {
                   1703:           courseForm.$name.checked = true;
                   1704:       }|;
1.121     raeburn  1705:                 }
                   1706:             }
                   1707:             $output .= qq|
1.119     raeburn  1708:   }
                   1709:   else {
                   1710:       for (var i=0; i<courseForm.$name.length; i++) {
                   1711: |;
1.121     raeburn  1712:             if ($$elements{$name} eq 'selectbox') {
                   1713:                 $output .=  qq|
1.119     raeburn  1714:           courseForm.$name.options[i].selected = false;|;
1.121     raeburn  1715:             } elsif (($$elements{$name} eq 'radio') || 
                   1716:                      ($$elements{$name} eq 'checkbox')) {
                   1717:                 $output .= qq|
1.119     raeburn  1718:           courseForm.$name\[i].checked = false;|; 
1.121     raeburn  1719:             }
                   1720:             $output .= qq|
1.119     raeburn  1721:       }
                   1722:       for (var j=0; j<courseForm.$name.length; j++) {
                   1723: |;
1.121     raeburn  1724:             foreach my $value (@{$values{$name}}) {
                   1725:                 if ($$elements{$name} eq 'selectbox') {
                   1726:                     $output .=  qq|
1.119     raeburn  1727:           if (courseForm.$name.options[j].value == "$value") {
                   1728:               courseForm.$name.options[j].selected = true;
                   1729:           }|;
1.121     raeburn  1730:                 } elsif (($$elements{$name} eq 'radio') ||
                   1731:                          ($$elements{$name} eq 'checkbox')) { 
                   1732:                       $output .= qq|
1.119     raeburn  1733:           if (courseForm.$name\[j].value == "$value") {
                   1734:               courseForm.$name\[j].checked = true;
                   1735:           }|;
1.121     raeburn  1736:                 }
                   1737:             }
                   1738:             $output .= qq|
1.119     raeburn  1739:       }
                   1740:   }
                   1741: |;
                   1742:         }
                   1743:     }
                   1744:     $output .= "
                   1745: }\n";
                   1746:     return $output;
                   1747: }
                   1748: 
1.1       stredwic 1749: 1;
1.23      matthew  1750: 
1.1       stredwic 1751: __END__

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