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

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

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