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

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

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