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

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

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