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

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

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