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

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

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