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

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

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