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

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

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