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

1.2       www         1: # The LearningOnline Network with CAPA
                      2: # a pile of common html routines
                      3: #
1.252   ! bisitz      4: # $Id: lonhtmlcommon.pm,v 1.251 2009/11/30 06:32:26 raeburn Exp $
1.2       www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.10      matthew    28: ######################################################################
                     29: ######################################################################
                     30: 
                     31: =pod
                     32: 
                     33: =head1 NAME
                     34: 
                     35: Apache::lonhtmlcommon - routines to do common html things
                     36: 
                     37: =head1 SYNOPSIS
                     38: 
                     39: Referenced by other mod_perl Apache modules.
                     40: 
                     41: =head1 INTRODUCTION
                     42: 
                     43: lonhtmlcommon is a collection of subroutines used to present information
                     44: in a consistent html format, or provide other functionality related to
                     45: html.
                     46: 
                     47: =head2 General Subroutines
                     48: 
                     49: =over 4
                     50: 
                     51: =cut 
                     52: 
                     53: ######################################################################
                     54: ######################################################################
1.2       www        55: 
1.1       stredwic   56: package Apache::lonhtmlcommon;
                     57: 
1.104     albertel   58: use strict;
1.10      matthew    59: use Time::Local;
1.47      sakharuk   60: use Time::HiRes;
1.30      www        61: use Apache::lonlocal;
1.104     albertel   62: use Apache::lonnet;
1.130     www        63: use LONCAPA;
1.1       stredwic   64: 
1.247     www        65: 
                     66: sub coursepreflink {
                     67:    my ($text,$category)=@_;
                     68:    if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
                     69:       return '<a href="/adm/courseprefs?phase=display&actions='.$category.'">'.$text.'</a>';
                     70:    } else {
                     71:       return '';
                     72:    }
                     73: }
                     74: 
1.208     www        75: ##############################################
                     76: ##############################################
                     77: 
                     78: =pod
                     79: 
                     80: =item confirm_success
                     81: 
                     82: Successful completion of an operation message
                     83: 
                     84: =cut
                     85: 
                     86: sub confirm_success {
1.209     www        87:    my ($message,$failure)=@_;
                     88:    if ($failure) {
1.211     bisitz     89:       return '<span class="LC_error">'."\n"
1.218     bisitz     90:             .'<img src="/adm/lonIcons/navmap.wrong.gif" alt="'.&mt('Error').'" /> '."\n"
1.211     bisitz     91:             .$message."\n"
                     92:             .'</span>'."\n";
1.209     www        93:    } else {
1.211     bisitz     94:       return '<span class="LC_success">'."\n"
1.233     raeburn    95:             .'<img src="/adm/lonIcons/navmap.correct.gif" alt="'.&mt('OK').'" /> '."\n"
1.211     bisitz     96:             .$message."\n"
                     97:             .'</span>'."\n";
1.209     www        98:    }
1.208     www        99: }
1.176     foxr      100: 
                    101: ##############################################
                    102: ##############################################
                    103: 
                    104: =pod
                    105: 
1.177     raeburn   106: =item dragmath_button
1.176     foxr      107: 
1.177     raeburn   108: Creates a button that launches a dragmath popup-window, in which an 
                    109: expression can be edited and pasted as LaTeX into a specified textarea. 
                    110: 
                    111:   textarea - Name of the textarea to edit.
                    112:   helpicon - If true, show a help icon to the right of the button.
1.176     foxr      113: 
                    114: =cut
                    115: 
1.177     raeburn   116: sub dragmath_button {
                    117:     my ($textarea,$helpicon) = @_;
                    118:     my $help_text; 
                    119:     if ($helpicon) {
                    120:         $help_text = &Apache::loncommon::help_open_topic('Authoring_Math_Editor');
                    121:     }
1.178     bisitz    122:     my $buttontext=&mt('Edit Math');
1.177     raeburn   123:     return <<ENDDRAGMATH;
1.246     bisitz    124:                 <input type="button" value="$buttontext" onclick="javascript:mathedit('$textarea',document)" />$help_text
1.177     raeburn   125: ENDDRAGMATH
                    126: }
                    127: 
1.176     foxr      128: ##############################################
                    129: 
1.177     raeburn   130: =pod
                    131: 
                    132: =item dragmath_js
                    133: 
                    134: Javascript used to open pop-up window containing dragmath applet which 
                    135: can be used to paste LaTeX into a textarea.
                    136:  
                    137: =cut
1.176     foxr      138: 
1.177     raeburn   139: sub dragmath_js {
1.182     foxr      140:     my ($popup) = @_;
1.177     raeburn   141:     return <<ENDDRAGMATHJS;
                    142:                 <script type="text/javascript">
1.218     bisitz    143:                 // <![CDATA[
1.176     foxr      144:                   function mathedit(textarea, doc) {
                    145:                      targetEntry = textarea;
1.177     raeburn   146:                      targetDoc   = doc;
1.182     foxr      147:                      newwin  = window.open("/adm/dragmath/applet/$popup.html","","width=565,height=500,resizable");
1.176     foxr      148:                   }
1.218     bisitz    149:                 // ]]>
1.176     foxr      150:                 </script>
1.177     raeburn   151: 
                    152: ENDDRAGMATHJS
1.176     foxr      153: }
                    154: 
1.182     foxr      155: 
1.40      www       156: ##############################################
                    157: ##############################################
                    158: 
                    159: =pod
                    160: 
                    161: =item authorbombs
                    162: 
                    163: =cut
                    164: 
                    165: ##############################################
                    166: ##############################################
                    167: 
                    168: sub authorbombs {
                    169:     my $url=shift;
                    170:     $url=&Apache::lonnet::declutter($url);
1.155     albertel  171:     my ($udom,$uname)=($url=~m{^($LONCAPA::domain_re)/($LONCAPA::username_re)/});
1.40      www       172:     my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
1.232     raeburn   173:     foreach my $bomb (keys(%bombs)) {
                    174: 	if ($bomb =~ /^$udom\/$uname\//) {
1.40      www       175: 	    return '<a href="/adm/bombs/'.$url.
1.218     bisitz    176: 		'"><img src="'.&Apache::loncommon::lonhttpdurl('/adm/lonMisc/bomb.gif').'" alt="'.&mt('Bomb').'" border="0" /></a>'.
1.40      www       177: 		&Apache::loncommon::help_open_topic('About_Bombs');
                    178: 	}
                    179:     }
                    180:     return '';
                    181: }
1.26      matthew   182: 
                    183: ##############################################
                    184: ##############################################
                    185: 
1.41      www       186: sub recent_filename {
                    187:     my $area=shift;
1.130     www       188:     return 'nohist_recent_'.&escape($area);
1.41      www       189: }
                    190: 
                    191: sub store_recent {
1.136     albertel  192:     my ($area,$name,$value,$freeze)=@_;
1.41      www       193:     my $file=&recent_filename($area);
                    194:     my %recent=&Apache::lonnet::dump($file);
1.111     www       195:     if (scalar(keys(%recent))>20) {
1.41      www       196: # remove oldest value
1.136     albertel  197: 	my $oldest=time();
1.41      www       198: 	my $delkey='';
1.136     albertel  199: 	foreach my $item (keys(%recent)) {
                    200: 	    my $thistime=(split(/\&/,$recent{$item}))[0];
                    201: 	    if (($thistime ne "always_include") && ($thistime<$oldest)) {
1.41      www       202: 		$oldest=$thistime;
1.136     albertel  203: 		$delkey=$item;
1.41      www       204: 	    }
                    205: 	}
                    206: 	&Apache::lonnet::del($file,[$delkey]);
                    207:     }
                    208: # store new value
1.136     albertel  209:     my $timestamp;
                    210:     if ($freeze) {
                    211:         $timestamp = "always_include";
                    212:     } else {
                    213:         $timestamp = time();
                    214:     }   
1.41      www       215:     &Apache::lonnet::put($file,{ $name => 
1.136     albertel  216: 				 $timestamp.'&'.&escape($value) });
1.41      www       217: }
                    218: 
1.89      banghart  219: sub remove_recent {
                    220:     my ($area,$names)=@_;
                    221:     my $file=&recent_filename($area);
                    222:     return &Apache::lonnet::del($file,$names);
                    223: }
                    224: 
1.41      www       225: sub select_recent {
                    226:     my ($area,$fieldname,$event)=@_;
                    227:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
                    228:     my $return="\n<select name='$fieldname'".
1.96      albertel  229: 	($event?" onchange='$event'":'').
1.41      www       230: 	">\n<option value=''>--- ".&mt('Recent')." ---</option>";
1.136     albertel  231:     foreach my $value (sort(keys(%recent))) {
                    232: 	unless ($value =~/^error\:/) {
                    233: 	    my $escaped = &Apache::loncommon::escape_url($value);
1.160     albertel  234: 	    &Apache::loncommon::inhibit_menu_check(\$escaped);
1.251     raeburn   235:             if ($area eq 'residx') {
                    236:                 next if ((!&Apache::lonnet::allowed('bre',$value)) && (!&Apache::lonnet::allowed('bro',$value)));
                    237:             }
1.94      foxr      238: 	    $return.="\n<option value='$escaped'>".
1.136     albertel  239: 		&unescape((split(/\&/,$recent{$value}))[1]).
1.41      www       240: 		'</option>';
                    241: 	}
                    242:     }
                    243:     $return.="\n</select>\n";
                    244:     return $return;
                    245: }
                    246: 
1.97      albertel  247: sub get_recent {
                    248:     my ($area, $n) = @_;
                    249:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
                    250: 
                    251: # Create hash with key as time and recent as value
1.136     albertel  252: # Begin filling return_hash with any 'always_include' option
1.97      albertel  253:     my %time_hash = ();
1.136     albertel  254:     my %return_hash = ();
1.232     raeburn   255:     foreach my $item (keys(%recent)) {
1.136     albertel  256:         my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
                    257:         if ($thistime eq 'always_include') {
                    258:             $return_hash{$item} = &unescape($thisvalue);
                    259:             $n--;
                    260:         } else {
                    261:             $time_hash{$thistime} = $item;
1.133     albertel  262:         }
1.97      albertel  263:     }
                    264: 
                    265: # Sort by decreasing time and return key value pairs
                    266:     my $idx = 1;
1.136     albertel  267:     foreach my $item (reverse(sort(keys(%time_hash)))) {
                    268:        $return_hash{$time_hash{$item}} =
                    269:                   &unescape((split(/\&/,$recent{$time_hash{$item}}))[1]);
1.97      albertel  270:        if ($n && ($idx++ >= $n)) {last;}
                    271:     }
                    272: 
                    273:     return %return_hash;
                    274: }
                    275: 
1.136     albertel  276: sub get_recent_frozen {
                    277:     my ($area) = @_;
                    278:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
                    279: 
                    280: # Create hash with all 'frozen' items
                    281:     my %return_hash = ();
                    282:     foreach my $item (keys(%recent)) {
                    283:         my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
                    284:         if ($thistime eq 'always_include') {
                    285:             $return_hash{$item} = &unescape($thisvalue);
                    286:         }
                    287:     }
                    288:     return %return_hash;
                    289: }
                    290: 
1.97      albertel  291: 
1.41      www       292: 
1.26      matthew   293: =pod
                    294: 
                    295: =item textbox
                    296: 
                    297: =cut
                    298: 
                    299: ##############################################
                    300: ##############################################
                    301: sub textbox {
                    302:     my ($name,$value,$size,$special) = @_;
                    303:     $size = 40 if (! defined($size));
1.128     albertel  304:     $value = &HTML::Entities::encode($value,'<>&"');
1.26      matthew   305:     my $Str = '<input type="text" name="'.$name.'" size="'.$size.'" '.
                    306:         'value="'.$value.'" '.$special.' />';
                    307:     return $Str;
                    308: }
                    309: 
                    310: ##############################################
                    311: ##############################################
                    312: 
                    313: =pod
                    314: 
                    315: =item checkbox
                    316: 
                    317: =cut
                    318: 
                    319: ##############################################
                    320: ##############################################
                    321: sub checkbox {
1.68      matthew   322:     my ($name,$checked,$value) = @_;
                    323:     my $Str = '<input type="checkbox" name="'.$name.'" ';
                    324:     if (defined($value)) {
                    325:         $Str .= 'value="'.$value.'"';
                    326:     } 
                    327:     if ($checked) {
1.206     bisitz    328:         $Str .= ' checked="checked"';
1.68      matthew   329:     }
                    330:     $Str .= ' />';
1.26      matthew   331:     return $Str;
                    332: }
                    333: 
1.120     albertel  334: 
                    335: =pod
                    336: 
                    337: =item radiobutton
                    338: 
                    339: =cut
                    340: 
                    341: ##############################################
                    342: ##############################################
                    343: sub radio {
                    344:     my ($name,$checked,$value) = @_;
                    345:     my $Str = '<input type="radio" name="'.$name.'" ';
                    346:     if (defined($value)) {
                    347:         $Str .= 'value="'.$value.'"';
                    348:     } 
                    349:     if ($checked eq $value) {
1.206     bisitz    350:         $Str .= ' checked="checked"';
1.120     albertel  351:     }
                    352:     $Str .= ' />';
                    353:     return $Str;
                    354: }
                    355: 
1.10      matthew   356: ##############################################
                    357: ##############################################
                    358: 
                    359: =pod
                    360: 
                    361: =item &date_setter
                    362: 
1.22      matthew   363: &date_setter returns html and javascript for a compact date-setting form.
                    364: To retrieve values from it, use &get_date_from_form().
                    365: 
1.10      matthew   366: Inputs
                    367: 
                    368: =over 4
                    369: 
                    370: =item $dname 
                    371: 
                    372: The name to prepend to the form elements.  
                    373: The form elements defined will be dname_year, dname_month, dname_day,
                    374: dname_hour, dname_min, and dname_sec.
                    375: 
                    376: =item $currentvalue
                    377: 
                    378: The current setting for this time parameter.  A unix format time
                    379: (time in seconds since the beginning of Jan 1st, 1970, GMT.  
                    380: An undefined value is taken to indicate the value is the current time.
                    381: Also, to be explicit, a value of 'now' also indicates the current time.
                    382: 
1.26      matthew   383: =item $special
                    384: 
                    385: Additional html/javascript to be associated with each element in
                    386: the date_setter.  See lonparmset for example usage.
                    387: 
1.59      matthew   388: =item $includeempty 
                    389: 
                    390: =item $state
                    391: 
                    392: Specifies the initial state of the form elements.  Either 'disabled' or empty.
                    393: Defaults to empty, which indiciates the form elements are not disabled. 
                    394: 
1.22      matthew   395: =back
                    396: 
                    397: Bugs
                    398: 
                    399: The method used to restrict user input will fail in the year 2400.
                    400: 
1.10      matthew   401: =cut
                    402: 
                    403: ##############################################
                    404: ##############################################
                    405: sub date_setter {
1.67      matthew   406:     my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
1.134     raeburn   407:         $no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_;
1.175     raeburn   408:     my $now = time;
1.107     www       409:     my $wasdefined=1;
1.59      matthew   410:     if (! defined($state) || $state ne 'disabled') {
                    411:         $state = '';
                    412:     }
1.67      matthew   413:     if (! defined($no_hh_mm_ss)) {
                    414:         $no_hh_mm_ss = 0;
                    415:     }
1.110     www       416:     if ($currentvalue eq 'now') {
1.175     raeburn   417: 	$currentvalue = $now;
1.110     www       418:     }
                    419:     if ((!defined($currentvalue)) || ($currentvalue eq '')) {
                    420: 	$wasdefined=0;
                    421: 	if ($includeempty) {
                    422: 	    $currentvalue = 0;
                    423: 	} else {
1.175     raeburn   424: 	    $currentvalue = $now;
1.39      www       425: 	}
1.10      matthew   426:     }
                    427:     # other potentially useful values:     wkday,yrday,is_daylight_savings
1.175     raeburn   428:     my $tzname;
1.65      albertel  429:     my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
1.39      www       430:     if ($currentvalue) {
1.175     raeburn   431:         ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($currentvalue); 
1.39      www       432:     }
1.107     www       433:     unless ($wasdefined) {
1.175     raeburn   434:         ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($now);
1.110     www       435: 	if (($defhour) || ($defmin) || ($defsec)) {
                    436: 	    $sec=($defsec?$defsec:0);
                    437: 	    $min=($defmin?$defmin:0);
                    438: 	    $hour=($defhour?$defhour:0);
                    439: 	} elsif (!$includeempty) {
                    440: 	    $sec=0;
                    441: 	    $min=0;
                    442: 	    $hour=0;
                    443: 	}
1.107     www       444:     }
1.10      matthew   445:     my $result = "\n<!-- $dname date setting form -->\n";
                    446:     $result .= <<ENDJS;
1.135     albertel  447: <script type="text/javascript">
1.218     bisitz    448: // <![CDATA[
1.10      matthew   449:     function $dname\_checkday() {
                    450:         var day   = document.$formname.$dname\_day.value;
                    451:         var month = document.$formname.$dname\_month.value;
                    452:         var year  = document.$formname.$dname\_year.value;
                    453:         var valid = true;
                    454:         if (day < 1) {
                    455:             document.$formname.$dname\_day.value = 1;
                    456:         } 
                    457:         if (day > 31) {
                    458:             document.$formname.$dname\_day.value = 31;
                    459:         }
                    460:         if ((month == 1)  || (month == 3)  || (month == 5)  ||
                    461:             (month == 7)  || (month == 8)  || (month == 10) ||
                    462:             (month == 12)) {
                    463:             if (day > 31) {
                    464:                 document.$formname.$dname\_day.value = 31;
                    465:                 day = 31;
                    466:             }
                    467:         } else if (month == 2 ) {
                    468:             if ((year % 4 == 0) && (year % 100 != 0)) {
                    469:                 if (day > 29) {
                    470:                     document.$formname.$dname\_day.value = 29;
                    471:                 }
                    472:             } else if (day > 29) {
                    473:                 document.$formname.$dname\_day.value = 28;
                    474:             }
                    475:         } else if (day > 30) {
                    476:             document.$formname.$dname\_day.value = 30;
                    477:         }
                    478:     }
1.95      matthew   479:     
1.59      matthew   480:     function $dname\_disable() {
                    481:         document.$formname.$dname\_month.disabled=true;
                    482:         document.$formname.$dname\_day.disabled=true;
                    483:         document.$formname.$dname\_year.disabled=true;
                    484:         document.$formname.$dname\_hour.disabled=true;
                    485:         document.$formname.$dname\_minute.disabled=true;
                    486:         document.$formname.$dname\_second.disabled=true;
                    487:     }
                    488: 
                    489:     function $dname\_enable() {
                    490:         document.$formname.$dname\_month.disabled=false;
                    491:         document.$formname.$dname\_day.disabled=false;
                    492:         document.$formname.$dname\_year.disabled=false;
                    493:         document.$formname.$dname\_hour.disabled=false;
                    494:         document.$formname.$dname\_minute.disabled=false;
                    495:         document.$formname.$dname\_second.disabled=false;        
                    496:     }
                    497: 
1.29      www       498:     function $dname\_opencalendar() {
1.59      matthew   499:         if (! document.$formname.$dname\_month.disabled) {
                    500:             var calwin=window.open(
1.29      www       501: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
                    502: document.$formname.$dname\_month.value+"&year="+
                    503: document.$formname.$dname\_year.value,
                    504:              "LONCAPAcal",
                    505:               "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
1.59      matthew   506:         }
1.29      www       507: 
                    508:     }
1.218     bisitz    509: // ]]>
1.10      matthew   510: </script>
                    511: ENDJS
1.192     bisitz    512:     $result .= '  <span class="LC_nobreak">';
1.96      albertel  513:     my $monthselector = qq{<select name="$dname\_month" $special $state onchange="javascript:$dname\_checkday()" >};
1.67      matthew   514:     # Month
1.10      matthew   515:     my @Months = qw/January February  March     April   May      June 
                    516:                     July    August    September October November December/;
                    517:     # Pad @Months with a bogus value to make indexing easier
                    518:     unshift(@Months,'If you can read this an error occurred');
1.95      matthew   519:     if ($includeempty) { $monthselector.="<option value=''></option>"; }
1.10      matthew   520:     for(my $m = 1;$m <=$#Months;$m++) {
1.228     bisitz    521:         $monthselector .= qq{      <option value="$m"};
                    522:         $monthselector .= ' selected="selected"' if ($m-1 eq $month);
                    523:         $monthselector .= '> '.&mt($Months[$m]).' </option>'."\n";
1.10      matthew   524:     }
1.95      matthew   525:     $monthselector.= '  </select>';
1.67      matthew   526:     # Day
1.96      albertel  527:     my $dayselector = qq{<input type="text" name="$dname\_day" $state value="$mday" size="3" $special onchange="javascript:$dname\_checkday()" />};
1.67      matthew   528:     # Year
1.226     bisitz    529:     my $yearselector = qq{<input type="text" name="$dname\_year" $state value="$year" size="5" $special onchange="javascript:$dname\_checkday()" />};
1.95      matthew   530:     #
                    531:     my $hourselector = qq{<select name="$dname\_hour" $special $state >};
                    532:     if ($includeempty) { 
                    533:         $hourselector.=qq{<option value=''></option>};
                    534:     }
                    535:     for (my $h = 0;$h<24;$h++) {
1.228     bisitz    536:         $hourselector .= qq{<option value="$h"};
                    537:         $hourselector .= ' selected="selected"' if (defined($hour) && $hour == $h);
1.95      matthew   538:         $hourselector .= ">";
                    539:         my $timest='';
                    540:         if ($h == 0) {
                    541:             $timest .= "12 am";
                    542:         } elsif($h == 12) {
                    543:             $timest .= "12 noon";
                    544:         } elsif($h < 12) {
                    545:             $timest .= "$h am";
                    546:         } else {
                    547:             $timest .= $h-12 ." pm";
                    548:         }
                    549:         $timest=&mt($timest);
                    550:         $hourselector .= $timest." </option>\n";
                    551:     }
                    552:     $hourselector .= "  </select>\n";
                    553:     my $minuteselector = qq{<input type="text" name="$dname\_minute" $special $state value="$min" size="3" />};
                    554:     my $secondselector= qq{<input type="text" name="$dname\_second" $special $state value="$sec" size="3" />};
1.134     raeburn   555:     my $cal_link;
                    556:     if (!$nolink) {
                    557:         $cal_link = qq{<a href="javascript:$dname\_opencalendar()">};
                    558:     }
1.95      matthew   559:     #
1.175     raeburn   560:     my $tzone = ' '.$tzname.' ';
1.95      matthew   561:     if ($no_hh_mm_ss) {
1.134     raeburn   562:         $result .= &mt('[_1] [_2] [_3] ',
1.174     raeburn   563:                        $monthselector,$dayselector,$yearselector).
                    564:                    $tzone;
1.134     raeburn   565:         if (!$nolink) {
1.141     albertel  566:             $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
1.134     raeburn   567:         }
1.95      matthew   568:     } else {
1.134     raeburn   569:         $result .= &mt('[_1] [_2] [_3] [_4] [_5]m [_6]s ',
                    570:                       $monthselector,$dayselector,$yearselector,
1.174     raeburn   571:                       $hourselector,$minuteselector,$secondselector).
                    572:                    $tzone;
1.134     raeburn   573:         if (!$nolink) {
1.141     albertel  574:             $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
1.134     raeburn   575:         }
1.67      matthew   576:     }
1.135     albertel  577:     $result .= "</span>\n<!-- end $dname date setting form -->\n";
1.10      matthew   578:     return $result;
                    579: }
                    580: 
1.175     raeburn   581: sub get_timedates {
                    582:     my ($epoch) = @_;
                    583:     my $dt = DateTime->from_epoch(epoch => $epoch)
                    584:                      ->set_time_zone(&Apache::lonlocal::gettimezone());
                    585:     my $tzname = $dt->time_zone_short_name();
                    586:     my $sec = $dt->second;
                    587:     my $min = $dt->minute;
                    588:     my $hour = $dt->hour;
                    589:     my $mday = $dt->day;
                    590:     my $month = $dt->month;
                    591:     if ($month) {
                    592:         $month --;
                    593:     }
                    594:     my $year = $dt->year;
                    595:     return ($tzname,$sec,$min,$hour,$mday,$month,$year);
                    596: }
1.166     banghart  597: 
                    598: sub build_url {
                    599:     my ($base, $fields)=@_;
                    600:     my $url;
                    601:     $url = $base.'?';
1.168     albertel  602:     foreach my $key (keys(%$fields)) {
                    603:         $url.=&escape($key).'='.&escape($$fields{$key}).'&amp;';
1.166     banghart  604:     }
                    605:     $url =~ s/&amp;$//;
                    606:     return $url;
                    607: }
                    608: 
                    609: 
1.10      matthew   610: ##############################################
                    611: ##############################################
                    612: 
1.22      matthew   613: =pod
                    614: 
1.10      matthew   615: =item &get_date_from_form
1.22      matthew   616: 
                    617: get_date_from_form retrieves the date specified in an &date_setter form.
1.10      matthew   618: 
                    619: Inputs:
                    620: 
                    621: =over 4
                    622: 
                    623: =item $dname
                    624: 
1.226     bisitz    625: The name passed to &date_setter, which prefixes the form elements.
1.10      matthew   626: 
                    627: =item $defaulttime
                    628: 
                    629: The unix time to use as the default in case of poor inputs.
                    630: 
                    631: =back
                    632: 
                    633: Returns: Unix time represented in the form.
                    634: 
                    635: =cut
                    636: 
                    637: ##############################################
                    638: ##############################################
                    639: sub get_date_from_form {
                    640:     my ($dname) = @_;
                    641:     my ($sec,$min,$hour,$day,$month,$year);
                    642:     #
1.104     albertel  643:     if (defined($env{'form.'.$dname.'_second'})) {
                    644:         my $tmpsec = $env{'form.'.$dname.'_second'};
1.10      matthew   645:         if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
                    646:             $sec = $tmpsec;
                    647:         }
1.64      albertel  648: 	if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
1.67      matthew   649:     } else {
                    650:         $sec = 0;
1.10      matthew   651:     }
1.104     albertel  652:     if (defined($env{'form.'.$dname.'_minute'})) {
                    653:         my $tmpmin = $env{'form.'.$dname.'_minute'};
1.10      matthew   654:         if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
                    655:             $min = $tmpmin;
                    656:         }
1.64      albertel  657: 	if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
1.67      matthew   658:     } else {
                    659:         $min = 0;
1.10      matthew   660:     }
1.104     albertel  661:     if (defined($env{'form.'.$dname.'_hour'})) {
                    662:         my $tmphour = $env{'form.'.$dname.'_hour'};
1.33      matthew   663:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
1.10      matthew   664:             $hour = $tmphour;
                    665:         }
1.67      matthew   666:     } else {
                    667:         $hour = 0;
1.10      matthew   668:     }
1.104     albertel  669:     if (defined($env{'form.'.$dname.'_day'})) {
                    670:         my $tmpday = $env{'form.'.$dname.'_day'};
1.10      matthew   671:         if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
                    672:             $day = $tmpday;
                    673:         }
                    674:     }
1.104     albertel  675:     if (defined($env{'form.'.$dname.'_month'})) {
                    676:         my $tmpmonth = $env{'form.'.$dname.'_month'};
1.10      matthew   677:         if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
1.175     raeburn   678:             $month = $tmpmonth;
1.10      matthew   679:         }
                    680:     }
1.104     albertel  681:     if (defined($env{'form.'.$dname.'_year'})) {
                    682:         my $tmpyear = $env{'form.'.$dname.'_year'};
1.175     raeburn   683:         if (($tmpyear =~ /^\d+$/) && ($tmpyear >= 1970)) {
                    684:             $year = $tmpyear;
1.10      matthew   685:         }
                    686:     }
1.175     raeburn   687:     if (($year<1970) || ($year>2037)) { return undef; }
1.33      matthew   688:     if (defined($sec) && defined($min)   && defined($hour) &&
1.175     raeburn   689:         defined($day) && defined($month) && defined($year)) {
                    690:         my $timezone = &Apache::lonlocal::gettimezone();
                    691:         my $dt = DateTime->new( year   => $year,
                    692:                                 month  => $month,
                    693:                                 day    => $day,
                    694:                                 hour   => $hour,
                    695:                                 minute => $min,
                    696:                                 second => $sec,
                    697:                                 time_zone => $timezone,
                    698:                               );
                    699:         my $epoch_time  = $dt->epoch;
                    700:         if ($epoch_time ne '') {
                    701:             return $epoch_time;
                    702:         } else {
                    703:             return undef;
                    704:         }
1.10      matthew   705:     } else {
                    706:         return undef;
                    707:     }
1.20      matthew   708: }
                    709: 
                    710: ##############################################
                    711: ##############################################
                    712: 
                    713: =pod
                    714: 
                    715: =item &pjump_javascript_definition()
                    716: 
                    717: Returns javascript defining the 'pjump' function, which opens up a
                    718: parameter setting wizard.
                    719: 
                    720: =cut
                    721: 
                    722: ##############################################
                    723: ##############################################
                    724: sub pjump_javascript_definition {
                    725:     my $Str = <<END;
1.109     www       726:     function pjump(type,dis,value,marker,ret,call,hour,min,sec) {
1.20      matthew   727:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
                    728:                  +"&value="+escape(value)+"&marker="+escape(marker)
                    729:                  +"&return="+escape(ret)
1.109     www       730:                  +"&call="+escape(call)+"&name="+escape(dis)
                    731:                  +"&defhour="+escape(hour)+"&defmin="+escape(min)
                    732:                  +"&defsec="+escape(sec),"LONCAPAparms",
1.20      matthew   733:                  "height=350,width=350,scrollbars=no,menubar=no");
                    734:     }
                    735: END
                    736:     return $Str;
1.10      matthew   737: }
                    738: 
                    739: ##############################################
                    740: ##############################################
1.17      matthew   741: 
                    742: =pod
                    743: 
                    744: =item &javascript_nothing()
                    745: 
                    746: Return an appropriate null for the users browser.  This is used
                    747: as the first arguement for window.open calls when you want a blank
                    748: window that you can then write to.
                    749: 
                    750: =cut
                    751: 
                    752: ##############################################
                    753: ##############################################
                    754: sub javascript_nothing {
                    755:     # mozilla and other browsers work with "''", but IE on mac does not.
                    756:     my $nothing = "''";
                    757:     my $user_browser;
                    758:     my $user_os;
1.104     albertel  759:     $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
                    760:     $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
1.17      matthew   761:     if (! defined($user_browser) || ! defined($user_os)) {
                    762:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    763:                            &Apache::loncommon::decode_user_agent();
                    764:     }
                    765:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                    766:         $nothing = "'javascript:void(0);'";
                    767:     }
                    768:     return $nothing;
                    769: }
                    770: 
1.90      www       771: ##############################################
                    772: ##############################################
                    773: sub javascript_docopen {
1.171     albertel  774:     my ($mimetype) = @_;
                    775:     $mimetype ||= 'text/html';
1.90      www       776:     # safari does not understand document.open() and loads "text/html"
                    777:     my $nothing = "''";
                    778:     my $user_browser;
                    779:     my $user_os;
1.104     albertel  780:     $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
                    781:     $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
1.90      www       782:     if (! defined($user_browser) || ! defined($user_os)) {
                    783:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    784:                            &Apache::loncommon::decode_user_agent();
                    785:     }
                    786:     if ($user_browser eq 'safari' && $user_os =~ 'mac') {
                    787:         $nothing = "document.clear()";
                    788:     } else {
1.171     albertel  789: 	$nothing = "document.open('$mimetype','replace')";
1.90      www       790:     }
                    791:     return $nothing;
                    792: }
                    793: 
1.21      matthew   794: 
1.17      matthew   795: ##############################################
                    796: ##############################################
                    797: 
1.21      matthew   798: =pod
1.17      matthew   799: 
1.21      matthew   800: =item &StatusOptions()
1.10      matthew   801: 
1.21      matthew   802: Returns html for a selection box which allows the user to choose the
                    803: enrollment status of students.  The selection box name is 'Status'.
1.6       stredwic  804: 
1.21      matthew   805: Inputs:
1.6       stredwic  806: 
1.21      matthew   807: $status: the currently selected status.  If undefined the value of
1.104     albertel  808: $env{'form.Status'} is taken.  If that is undefined, a value of 'Active'
1.21      matthew   809: is used.
1.6       stredwic  810: 
1.21      matthew   811: $formname: The name of the form.  If defined the onchange attribute of
                    812: the selection box is set to document.$formname.submit().
1.6       stredwic  813: 
1.21      matthew   814: $size: the size (number of lines) of the selection box.
1.6       stredwic  815: 
1.27      matthew   816: $onchange: javascript to use when the value is changed.  Enclosed in 
                    817: double quotes, ""s, not single quotes.
                    818: 
1.21      matthew   819: Returns: a perl string as described.
1.1       stredwic  820: 
1.21      matthew   821: =cut
1.9       stredwic  822: 
1.21      matthew   823: ##############################################
                    824: ##############################################
                    825: sub StatusOptions {
1.165     banghart  826:     my ($status, $formName,$size,$onchange,$mult)=@_;
1.21      matthew   827:     $size = 1 if (!defined($size));
                    828:     if (! defined($status)) {
                    829:         $status = 'Active';
1.104     albertel  830:         $status = $env{'form.Status'} if (exists($env{'form.Status'}));
1.9       stredwic  831:     }
1.1       stredwic  832: 
                    833:     my $Str = '';
                    834:     $Str .= '<select name="Status"';
1.165     banghart  835:     if (defined($mult)){
                    836:         $Str .= ' multiple="multiple" ';
                    837:     }
1.27      matthew   838:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
1.1       stredwic  839:         $Str .= ' onchange="document.'.$formName.'.submit()"';
1.27      matthew   840:     }
                    841:     if (defined($onchange)) {
                    842:         $Str .= ' onchange="'.$onchange.'"';
1.1       stredwic  843:     }
1.21      matthew   844:     $Str .= ' size="'.$size.'" ';
1.1       stredwic  845:     $Str .= '>'."\n";
1.153     raeburn   846:     foreach my $type (['Active',  &mt('Currently Has Access')],
                    847: 		      ['Future',  &mt('Will Have Future Access')],
                    848: 		      ['Expired', &mt('Previously Had Access')],
                    849: 		      ['Any',     &mt('Any Access Status')]) {
1.151     albertel  850: 	my ($name,$label) = @$type;
                    851: 	$Str .= '<option value="'.$name.'" ';
                    852: 	if ($status eq $name) {
                    853: 	    $Str .= 'selected="selected" ';
                    854: 	}
                    855: 	$Str .= '>'.$label.'</option>'."\n";
                    856:     }
                    857: 
1.1       stredwic  858:     $Str .= '</select>'."\n";
1.7       stredwic  859: }
1.12      matthew   860: 
                    861: ########################################################
                    862: ########################################################
1.7       stredwic  863: 
1.23      matthew   864: =pod
                    865: 
                    866: =item Progess Window Handling Routines
                    867: 
                    868: These routines handle the creation, update, increment, and closure of 
                    869: progress windows.  The progress window reports to the user the number
                    870: of items completed and an estimate of the time required to complete the rest.
                    871: 
                    872: =over 4
                    873: 
                    874: 
                    875: =item &Create_PrgWin
                    876: 
                    877: Writes javascript to the client to open a progress window and returns a
                    878: data structure used for bookkeeping.
                    879: 
                    880: Inputs
                    881: 
                    882: =over 4
                    883: 
                    884: =item $r Apache request
                    885: 
                    886: =item $title The title of the progress window
                    887: 
                    888: =item $heading A description (usually 1 line) of the process being initiated.
                    889: 
                    890: =item $number_to_do The total number of items being processed.
1.50      albertel  891: 
                    892: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
                    893:        specified)
                    894: 
1.51      albertel  895: =item $width Specify the width in charaters of the input field.
                    896: 
1.50      albertel  897: =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
                    898: 
                    899: =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   900: 
                    901: =back
                    902: 
                    903: Returns a hash containing the progress state data structure.
                    904: 
                    905: 
                    906: =item &Update_PrgWin
                    907: 
                    908: Updates the text in the progress indicator.  Does not increment the count.
                    909: See &Increment_PrgWin.
                    910: 
                    911: Inputs:
                    912: 
                    913: =over 4
                    914: 
                    915: =item $r Apache request
                    916: 
                    917: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
                    918: 
                    919: =item $displaystring The string to write to the status indicator
                    920: 
                    921: =back
                    922: 
                    923: Returns: none
                    924: 
                    925: 
                    926: =item Increment_PrgWin
                    927: 
                    928: Increment the count of items completed for the progress window by 1.  
                    929: 
                    930: Inputs:
                    931: 
                    932: =over 4
                    933: 
                    934: =item $r Apache request
                    935: 
                    936: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    937: 
                    938: =item $extraInfo A description of the items being iterated over.  Typically
                    939: 'student'.
                    940: 
                    941: =back
                    942: 
                    943: Returns: none
                    944: 
                    945: 
                    946: =item Close_PrgWin
                    947: 
                    948: Closes the progress window.
                    949: 
                    950: Inputs:
                    951: 
                    952: =over 4 
                    953: 
                    954: =item $r Apache request
                    955: 
                    956: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    957: 
                    958: =back
                    959: 
                    960: Returns: none
                    961: 
                    962: =back
                    963: 
                    964: =cut
                    965: 
                    966: ########################################################
                    967: ########################################################
                    968: 
1.51      albertel  969: my $uniq=0;
                    970: sub get_uniq_name {
                    971:     $uniq++;
                    972:     return 'uniquename'.$uniq;
                    973: }
                    974: 
1.7       stredwic  975: # Create progress
                    976: sub Create_PrgWin {
1.51      albertel  977:     my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
                    978: 	$inputname)=@_;
1.49      albertel  979:     if (!defined($type)) { $type='popup'; }
1.51      albertel  980:     if (!defined($width)) { $width=55; }
1.49      albertel  981:     my %prog_state;
                    982:     $prog_state{'type'}=$type;
                    983:     if ($type eq 'popup') {
                    984: 	$prog_state{'window'}='popwin';
1.122     albertel  985: 	my $start_page =
                    986: 	    &Apache::loncommon::start_page($title,undef,
                    987: 					   {'only_body' => 1,
                    988: 					    'bgcolor'   => '#88DDFF',
                    989: 					    'js_ready'  => 1});
                    990: 	my $end_page = &Apache::loncommon::end_page({'js_ready'  => 1});
                    991: 
1.49      albertel  992: 	#the whole function called through timeout is due to issues
                    993: 	#in mozilla Read BUG #2665 if you want to know the whole story
1.230     bisitz    994: 	&r_print($r,&Apache::lonhtmlcommon::scripttag(
1.49      albertel  995:         "var popwin;
                    996:          function openpopwin () {
                    997:          popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
1.122     albertel  998:         "popwin.document.writeln(\'".$start_page.
1.170     albertel  999:               "<h4>".&mt("$heading")."<\/h4>".
1.212     bisitz   1000:               "<form action=\"\" name=\"popremain\" method=\"post\">".
1.51      albertel 1001:               '<input type="text" size="'.$width.'" name="remaining" value="'.
1.131     albertel 1002: 	      &mt('Starting').'" /><\\/form>'.$end_page.
1.122     albertel 1003:               "\');".
1.49      albertel 1004:         "popwin.document.close();}".
1.230     bisitz   1005:         "\nwindow.setTimeout(openpopwin,0)"
                   1006:     ));
1.49      albertel 1007: 	$prog_state{'formname'}='popremain';
                   1008: 	$prog_state{'inputname'}="remaining";
                   1009:     } elsif ($type eq 'inline') {
                   1010: 	$prog_state{'window'}='window';
                   1011: 	if (!$formname) {
1.51      albertel 1012: 	    $prog_state{'formname'}=&get_uniq_name();
1.159     banghart 1013: 	    &r_print($r,'<form action="" name="'.$prog_state{'formname'}.'">');
1.49      albertel 1014: 	} else {
                   1015: 	    $prog_state{'formname'}=$formname;
                   1016: 	}
                   1017: 	if (!$inputname) {
1.51      albertel 1018: 	    $prog_state{'inputname'}=&get_uniq_name();
1.170     albertel 1019: 	    &r_print($r,&mt("$heading [_1]",' <input type="text" name="'.$prog_state{'inputname'}.'" size="'.$width.'" />'));
1.49      albertel 1020: 	} else {
                   1021: 	    $prog_state{'inputname'}=$inputname;
                   1022: 	    
                   1023: 	}
                   1024: 	if (!$formname) { &r_print($r,'</form>'); }
                   1025: 	&Update_PrgWin($r,\%prog_state,&mt('Starting'));
                   1026:     }
1.7       stredwic 1027: 
1.16      albertel 1028:     $prog_state{'done'}=0;
1.23      matthew  1029:     $prog_state{'firststart'}=&Time::HiRes::time();
                   1030:     $prog_state{'laststart'}=&Time::HiRes::time();
1.16      albertel 1031:     $prog_state{'max'}=$number_to_do;
1.49      albertel 1032:     
1.14      albertel 1033:     return %prog_state;
1.7       stredwic 1034: }
                   1035: 
                   1036: # update progress
                   1037: sub Update_PrgWin {
1.14      albertel 1038:     my ($r,$prog_state,$displayString)=@_;
1.230     bisitz   1039:     &r_print($r,&Apache::lonhtmlcommon::scripttag(
1.218     bisitz   1040:         $$prog_state{'window'}.'.document.'.
1.230     bisitz   1041:         $$prog_state{'formname'}.'.'.
                   1042:         $$prog_state{'inputname'}.'.value="'.
                   1043:         $displayString.'";'
                   1044:     ));
1.23      matthew  1045:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.14      albertel 1046: }
                   1047: 
                   1048: # increment progress state
                   1049: sub Increment_PrgWin {
                   1050:     my ($r,$prog_state,$extraInfo)=@_;
1.16      albertel 1051:     $$prog_state{'done'}++;
1.23      matthew  1052:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
                   1053:         $$prog_state{'done'} *
1.16      albertel 1054: 	($$prog_state{'max'}-$$prog_state{'done'});
                   1055:     $time_est = int($time_est);
1.80      matthew  1056:     #
                   1057:     my $min = int($time_est/60);
                   1058:     my $sec = $time_est % 60;
                   1059:     # 
                   1060:     my $str;
1.91      albertel 1061:     if ($min == 0 && $sec > 1) {
1.80      matthew  1062:         $str = '[_2] seconds';
1.91      albertel 1063:     } elsif ($min == 1 && $sec > 1) {
                   1064:         $str = '1 minute [_2] seconds';
1.80      matthew  1065:     } elsif ($min == 1 && $sec < 2) {
                   1066:         $str = '1 minute';
                   1067:     } elsif ($min < 10 && $sec > 1) {
                   1068:         $str = '[_1] minutes, [_2] seconds';
1.81      matthew  1069:     } elsif ($min >= 10 || $sec < 2) {
1.80      matthew  1070:         $str = '[_1] minutes';
1.16      albertel 1071:     }
1.80      matthew  1072:     $time_est = &mt($str,$min,$sec);
                   1073:     #
1.23      matthew  1074:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
                   1075:     if ($lasttime > 9) {
                   1076:         $lasttime = int($lasttime);
                   1077:     } elsif ($lasttime < 0.01) {
                   1078:         $lasttime = 0;
                   1079:     } else {
                   1080:         $lasttime = sprintf("%3.2f",$lasttime);
                   1081:     }
1.19      matthew  1082:     if ($lasttime == 1) {
1.32      www      1083:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
1.19      matthew  1084:     } else {
1.32      www      1085:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
1.28      matthew  1086:     }
                   1087:     #
1.104     albertel 1088:     my $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
                   1089:     my $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
1.28      matthew  1090:     if (! defined($user_browser) || ! defined($user_os)) {
                   1091:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                   1092:                            &Apache::loncommon::decode_user_agent();
                   1093:     }
                   1094:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                   1095:         $lasttime = '';
1.19      matthew  1096:     }
1.230     bisitz   1097:     &r_print($r,&Apache::lonhtmlcommon::scripttag(
1.218     bisitz   1098:         $$prog_state{'window'}.'.document.'.
1.230     bisitz   1099:         $$prog_state{'formname'}.'.'.
                   1100:         $$prog_state{'inputname'}.'.value="'.
                   1101:         $$prog_state{'done'}.'/'.$$prog_state{'max'}.
                   1102:         ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'
                   1103:     ));
1.23      matthew  1104:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.7       stredwic 1105: }
                   1106: 
                   1107: # close Progress Line
                   1108: sub Close_PrgWin {
1.14      albertel 1109:     my ($r,$prog_state)=@_;
1.49      albertel 1110:     if ($$prog_state{'type'} eq 'popup') {
1.230     bisitz   1111:         &r_print($r,&Apache::lonhtmlcommon::scripttag(
                   1112:             'popwin.close()'
                   1113:         ));
1.49      albertel 1114:     } elsif ($$prog_state{'type'} eq 'inline') {
                   1115: 	&Update_PrgWin($r,$prog_state,&mt('Done'));
                   1116:     }
1.48      albertel 1117:     undef(%$prog_state);
                   1118: }
                   1119: 
                   1120: sub r_print {
                   1121:     my ($r,$to_print)=@_;
                   1122:     if ($r) {
                   1123: 	$r->print($to_print);
                   1124: 	$r->rflush();
1.47      sakharuk 1125:     } else {
1.48      albertel 1126: 	print($to_print);
1.47      sakharuk 1127:     }
1.1       stredwic 1128: }
1.34      www      1129: 
                   1130: # ------------------------------------------------------- Puts directory header
                   1131: 
                   1132: sub crumbs {
1.252   ! bisitz   1133:     my ($uri,$target,$prefix,$form,$skiplast)=@_;
1.100     raeburn  1134:     if ($target) {
                   1135:         $target = ' target="'.
                   1136:                   &Apache::loncommon::escape_single($target).'"';
                   1137:     }
1.252   ! bisitz   1138:     my $output='<span class="LC_filename">';
        !          1139:     $output.=$prefix.'/';
1.249     raeburn  1140:     if (($env{'user.adv'}) || ($env{'user.author'})) {
1.252   ! bisitz   1141:         my $path=$prefix.'/';
        !          1142:         foreach my $dir (split('/',$uri)) {
1.99      matthew  1143:             if (! $dir) { next; }
                   1144:             $path .= $dir;
1.252   ! bisitz   1145:             if ($path eq $uri) {
        !          1146:                 if ($skiplast) {
        !          1147:                     $output.=$dir;
1.132     www      1148:                     last;
1.252   ! bisitz   1149:                 } 
        !          1150:             } else {
        !          1151:                 $path.='/'; 
        !          1152:             }
1.157     albertel 1153:             my $href_path = &HTML::Entities::encode($path,'<>&"');
1.252   ! bisitz   1154:             &Apache::loncommon::inhibit_menu_check(\$href_path);
        !          1155:             if ($form) {
        !          1156:                 my $href = 'javascript:'.$form.".action='".$href_path."';".$form.'.submit();';
        !          1157:                 $output.=qq{<a href="$href"$target>$dir</a>/};
        !          1158:             } else {
        !          1159:                 $output.=qq{<a href="$href_path"$target>$dir</a>/};
        !          1160:             }
        !          1161:         }
1.35      www      1162:     } else {
1.252   ! bisitz   1163:         foreach my $dir (split('/',$uri)) {
1.149     albertel 1164:             if (! $dir) { next; }
1.252   ! bisitz   1165:             $output.=$dir.'/';
        !          1166:         }
1.34      www      1167:     }
1.149     albertel 1168:     if ($uri !~ m|/$|) { $output=~s|/$||; }
1.252   ! bisitz   1169:     $output.='</span>';
        !          1170: 
        !          1171:     return $output;
1.34      www      1172: }
                   1173: 
1.85      www      1174: # --------------------- A function that generates a window for the spellchecker
                   1175: 
                   1176: sub spellheader {
1.123     albertel 1177:     my $start_page=
                   1178: 	&Apache::loncommon::start_page('Speller Suggestions',undef,
1.140     albertel 1179: 				       {'only_body'   => 1,
                   1180: 					'js_ready'    => 1,
                   1181: 					'bgcolor'     => '#DDDDDD',
                   1182: 				        'add_entries' => {
                   1183: 					    'onload' => 
                   1184:                                                'document.forms.spellcheckform.submit()',
                   1185:                                              }
                   1186: 				        });
1.123     albertel 1187:     my $end_page=
                   1188: 	&Apache::loncommon::end_page({'js_ready'  => 1}); 
                   1189: 
1.105     www      1190:     my $nothing=&javascript_nothing();
1.85      www      1191:     return (<<ENDCHECK);
                   1192: <script type="text/javascript"> 
1.218     bisitz   1193: // <![CDATA[
1.92      albertel 1194: //<!-- BEGIN LON-CAPA Internal
1.85      www      1195: var checkwin;
                   1196: 
1.140     albertel 1197: function spellcheckerwindow(string) {
                   1198:     var esc_string = string.replace(/\"/g,'&quot;');
1.105     www      1199:     checkwin=window.open($nothing,'spellcheckwin','height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
1.154     albertel 1200:     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      1201:     checkwin.document.close();
                   1202: }
1.92      albertel 1203: // END LON-CAPA Internal -->
1.218     bisitz   1204: // ]]>
1.85      www      1205: </script>
                   1206: ENDCHECK
                   1207: }
                   1208: 
                   1209: # ---------------------------------- Generate link to spell checker for a field
                   1210: 
                   1211: sub spelllink {
                   1212:     my ($form,$field)=@_;
                   1213:     my $linktext=&mt('Check Spelling');
                   1214:     return (<<ENDLINK);
1.140     albertel 1215: <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      1216: ENDLINK
                   1217: }
                   1218: 
1.52      www      1219: # ------------------------------------------------- Output headers for HTMLArea
                   1220: 
1.124     albertel 1221: {
                   1222:     my @htmlareafields;
                   1223:     sub init_htmlareafields {
                   1224: 	undef(@htmlareafields);
                   1225:     }
                   1226:     
                   1227:     sub add_htmlareafields {
                   1228: 	my (@newfields) = @_;
                   1229: 	push(@htmlareafields,@newfields);
                   1230:     }
                   1231: 
                   1232:     sub get_htmlareafields {
                   1233: 	return @htmlareafields;
                   1234:     }
                   1235: }
                   1236: 
1.52      www      1237: sub htmlareaheaders {
1.167     albertel 1238:     return if (&htmlareablocked());
                   1239:     return if (!&htmlareabrowser());
1.52      www      1240:     return (<<ENDHEADERS);
1.167     albertel 1241: <script type="text/javascript" src="/fckeditor/fckeditor.js"></script>
1.52      www      1242: ENDHEADERS
                   1243: }
                   1244: 
1.76      www      1245: # ----------------------------------------------------------------- Preferences
                   1246: 
                   1247: sub disablelink {
1.77      www      1248:     my @fields=@_;
                   1249:     if (defined($#fields)) {
                   1250: 	unless ($#fields>=0) { return ''; }
                   1251:     }
1.130     www      1252:     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      1253: }
                   1254: 
                   1255: sub enablelink {
1.77      www      1256:     my @fields=@_;
                   1257:     if (defined($#fields)) {
                   1258: 	unless ($#fields>=0) { return ''; }
                   1259:     }
1.130     www      1260:     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      1261: }
                   1262: 
1.167     albertel 1263: # ------------------------------------------------- lang to use in html editor
                   1264: sub htmlarea_lang {
                   1265:     my $lang='en';
                   1266:     if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
                   1267: 	$lang=&mt('htmlarea_lang');
                   1268:     }
                   1269:     return $lang;
                   1270: }
                   1271: 
1.72      www      1272: # ----------------------------------------- Script to activate only some fields
                   1273: 
                   1274: sub htmlareaselectactive {
1.73      www      1275:     my @fields=@_;
1.76      www      1276:     unless (&htmlareabrowser()) { return ''; }
1.77      www      1277:     if (&htmlareablocked()) { return '<br />'.&enablelink(@fields); }
1.230     bisitz   1278:     my $output='<script type="text/javascript" defer="1">'."\n"
                   1279:               .'// <![CDATA['."\n";
1.167     albertel 1280:     my $lang = &htmlarea_lang();
                   1281:     foreach my $field (@fields) {
                   1282: 	$output.="
                   1283: {
                   1284:     var oFCKeditor = new FCKeditor('$field');
                   1285:     oFCKeditor.Config['CustomConfigurationsPath'] = 
                   1286: 	'/fckeditor/loncapaconfig.js';    
                   1287:     oFCKeditor.ReplaceTextarea();
                   1288:     oFCKeditor.Config['AutoDetectLanguage'] = false;
                   1289:     oFCKeditor.Config['DefaultLanguage'] = '$lang';
                   1290: }";
1.72      www      1291:     }
1.218     bisitz   1292:     $output.="\nwindow.status='Activated Editfields';\n"
1.230     bisitz   1293:             .'// ]]>'."\n"
1.218     bisitz   1294:             .'</script><br />'.
1.77      www      1295: 	&disablelink(@fields);
1.72      www      1296:     return $output;
                   1297: }
                   1298: 
1.61      www      1299: # --------------------------------------------------------------------- Blocked
                   1300: 
                   1301: sub htmlareablocked {
1.104     albertel 1302:     unless ($env{'environment.wysiwygeditor'} eq 'on') { return 1; }
1.71      www      1303:     return 0;
1.52      www      1304: }
                   1305: 
                   1306: # ---------------------------------------- Browser capable of running HTMLArea?
                   1307: 
                   1308: sub htmlareabrowser {
                   1309:     return 1;
                   1310: }
1.53      matthew  1311: 
                   1312: ############################################################
                   1313: ############################################################
                   1314: 
                   1315: =pod
                   1316: 
                   1317: =item breadcrumbs
                   1318: 
                   1319: Compiles the previously registered breadcrumbs into an series of links.
                   1320: Additionally supports a 'component', which will be displayed on the
1.223     droeschl 1321: right side of the breadcrumbs enclosing div (without a link).
1.53      matthew  1322: A link to help for the component will be included if one is specified.
                   1323: 
                   1324: All inputs can be undef without problems.
                   1325: 
1.223     droeschl 1326: Inputs: $component (the text on the right side of the breadcrumbs trail),
1.53      matthew  1327:         $component_help
1.63      albertel 1328:         $menulink (boolean, controls whether to include a link to /adm/menu)
1.138     albertel 1329:         $helplink (if 'nohelp' don't include the orange help link)
                   1330:         $css_class (optional name for the class to apply to the table for CSS)
1.197     raeburn  1331:         $no_mt (optional flag, 1 if &mt() is _not_ to be applied to $component
                   1332:            when including the text on the right.
1.53      matthew  1333: Returns a string containing breadcrumbs for the current page.
                   1334: 
                   1335: =item clear_breadcrumbs
                   1336: 
                   1337: Clears the previously stored breadcrumbs.
                   1338: 
                   1339: =item add_breadcrumb
                   1340: 
                   1341: Pushes a breadcrumb on the stack of crumbs.
                   1342: 
                   1343: input: $breadcrumb, a hash reference.  The keys 'href','title', and 'text'
                   1344: are required.  If present the keys 'faq' and 'bug' will be used to provide
1.156     albertel 1345: links to the FAQ and bug sites. If the key 'no_mt' is present the 'title' 
                   1346: and 'text' values won't be sent through &mt()
1.53      matthew  1347: 
                   1348: returns: nothing    
                   1349: 
                   1350: =cut
                   1351: 
                   1352: ############################################################
                   1353: ############################################################
                   1354: {
                   1355:     my @Crumbs;
1.242     droeschl 1356:     my %tools = ();
1.57      matthew  1357:     
1.53      matthew  1358:     sub breadcrumbs {
1.216     bisitz   1359:         my ($component,$component_help,$menulink,$helplink,$css_class,$no_mt, $CourseBreadcrumbs) = @_;
1.53      matthew  1360:         #
1.215     droeschl 1361:         $css_class ||= 'LC_breadcrumbs';
1.205     amueller 1362: 
1.57      matthew  1363:         # Make the faq and bug data cascade
1.223     droeschl 1364:         my $faq  = '';
                   1365:         my $bug  = '';
                   1366:         my $help = '';
1.215     droeschl 1367:         # Crumb Symbol
1.223     droeschl 1368:         my $crumbsymbol = '&raquo;';
1.60      www      1369:         # The last breadcrumb does not have a link, so handle it separately.
1.53      matthew  1370:         my $last = pop(@Crumbs);
1.57      matthew  1371:         #
1.70      matthew  1372:         # The first one should be the course or a menu link
1.215     droeschl 1373:         if (!defined($menulink)) { $menulink=1; }
1.70      matthew  1374:         if ($menulink) {
                   1375:             my $description = 'Menu';
1.172     raeburn  1376:             my $no_mt_descr = 0;
1.104     albertel 1377:             if (exists($env{'request.course.id'}) && 
                   1378:                 $env{'request.course.id'} ne '') {
1.70      matthew  1379:                 $description = 
1.104     albertel 1380:                     $env{'course.'.$env{'request.course.id'}.'.description'};
1.172     raeburn  1381:                 $no_mt_descr = 1;
1.70      matthew  1382:             }
1.215     droeschl 1383:             $menulink =  {  href   =>'/adm/menu',
                   1384:                             title  =>'Go to main menu',
                   1385:                             target =>'_top',
                   1386:                             text   =>$description,
                   1387:                             no_mt  =>$no_mt_descr, };
                   1388:             if($last) {
                   1389:                 #$last set, so we have some crumbs
                   1390:                 unshift(@Crumbs,$menulink);
                   1391:             } else {
                   1392:                 #only menulink crumb present
                   1393:                 $last = $menulink;
                   1394:             }
1.53      matthew  1395:         }
1.219     droeschl 1396:         my $links = join "", 
1.53      matthew  1397:                  map {
1.219     droeschl 1398:                      $faq  = $_->{'faq'}  if (exists($_->{'faq'}));
                   1399:                      $bug  = $_->{'bug'}  if (exists($_->{'bug'}));
1.106     www      1400:                      $help = $_->{'help'} if (exists($_->{'help'}));
1.219     droeschl 1401: 
                   1402:                      my $result = htmltag( 'a', 
1.223     droeschl 1403:                                            $_->{no_mt} ? 
                   1404:                                             $_->{text} : mt($_->{text}), 
1.219     droeschl 1405:                                            { 
                   1406:                                                href   => $_->{href},
1.223     droeschl 1407:                                                title  => $_->{no_mt} ? 
                   1408:                                                 $_->{title} : mt($_->{title}),
1.219     droeschl 1409:                                                target => $_->{target},
                   1410:                                            });
1.223     droeschl 1411:                      $result = htmltag( 'li', "$result $crumbsymbol");
1.219     droeschl 1412:                      } @Crumbs;
1.223     droeschl 1413: 
                   1414:         #should the last Element be translated?
                   1415:         $links  .= htmltag( 'li', 
                   1416:                         htmltag( 'b', 
                   1417:                                  $last->{'no_mt'} ? 
                   1418:                                  $last->{'text'} : mt($last->{'text'}) ));
                   1419: 
1.54      matthew  1420:         my $icons = '';
1.223     droeschl 1421:         $faq  = $last->{'faq'}  if (exists($last->{'faq'}));
                   1422:         $bug  = $last->{'bug'}  if (exists($last->{'bug'}));
1.106     www      1423:         $help = $last->{'help'} if (exists($last->{'help'}));
                   1424:         $component_help=($component_help?$component_help:$help);
1.145     albertel 1425: #        if ($faq ne '') {
                   1426: #            $icons .= &Apache::loncommon::help_open_faq($faq);
                   1427: #        }
1.79      raeburn  1428: #        if ($bug ne '') {
                   1429: #            $icons .= &Apache::loncommon::help_open_bug($bug);
                   1430: #        }
1.223     droeschl 1431:         if ($faq ne '' || $component_help ne '' || $bug ne '') {
                   1432:             $icons .= &Apache::loncommon::help_open_menu($component,
                   1433:                                                          $component_help,
                   1434:                                                          $faq,$bug);
                   1435:         }
1.54      matthew  1436:         #
1.205     amueller 1437: 		
                   1438: 
1.223     droeschl 1439:         unless ($CourseBreadcrumbs) {
                   1440:             $links = htmltag('ol',  $links, { id => "LC_MenuBreadcrumbs"   });
                   1441:         } else {
1.227     bisitz   1442:             $links = htmltag('ul',  $links, { class => "LC_CourseBreadcrumbs" });
1.53      matthew  1443:         }
1.223     droeschl 1444: 
                   1445:         if ($component) {
                   1446:             $links = htmltag('span', 
                   1447:                              ( $no_mt ? $component : mt($component) ).
                   1448:                              ( $icons ? $icons : '' ),
                   1449:                              { class => 'LC_breadcrumbs_component' } )
                   1450:                              .$links;
                   1451:         }
                   1452:         
1.242     droeschl 1453:         #SD START (work in progress!)
                   1454:         add_tools(\$links);
                   1455:         #SD END
1.223     droeschl 1456:         $links = htmltag('div', $links, 
1.225     bisitz   1457:                         { id => "LC_breadcrumbs" }) unless ($CourseBreadcrumbs) ;
1.242     droeschl 1458:         add_advtools(\$links);
1.223     droeschl 1459: 
1.53      matthew  1460:         # Return the @Crumbs stack to what we started with
                   1461:         push(@Crumbs,$last);
                   1462:         shift(@Crumbs);
1.223     droeschl 1463:         # Return the breadcrumb's line
                   1464:         return "$links";
1.53      matthew  1465:     }
                   1466: 
                   1467:     sub clear_breadcrumbs {
                   1468:         undef(@Crumbs);
1.242     droeschl 1469:         undef(%tools);
1.53      matthew  1470:     }
                   1471: 
                   1472:     sub add_breadcrumb {
1.232     raeburn  1473:         push(@Crumbs,@_);
1.53      matthew  1474:     }
1.242     droeschl 1475:     
                   1476: 
                   1477:     #SD START (work in progress!)
                   1478:     sub add_breadcrumb_tool {
                   1479:         my ($category, $html) = @_;
                   1480:         return unless $html;
                   1481:         if (!defined(%tools)) { 
                   1482:             my %tools = ( A => [], B => [], C => []);
                   1483:         }
                   1484:         push @{$tools{$category}}, $html;
                   1485:     }
                   1486: 
1.245     droeschl 1487:     sub clear_breadcrumb_tools {
                   1488:         undef(%tools);
                   1489:     }
                   1490: 
1.242     droeschl 1491:     sub add_tools {
                   1492:         my ($links) = @_;
                   1493:         return unless defined %tools;
                   1494:         my $html = '<ul class="LC_bct">';
                   1495:         for my $category ('A','B') {
                   1496:             $html .= '<li class="LC_breadcrumb_tools">'
                   1497:                    . '<ul class="LC_breadcrumb_tools'
                   1498:                    . " LC_breadcrumb_tools_$category\">";
                   1499:             for my $item (@{$tools{$category}}){
                   1500:                 #SD ugly! I'll fix that later on
                   1501:                 $item =~ s/align="(right|left)"//;
                   1502:                 $item =~ s/<span(.*?)\/span>//;
                   1503:                 $html .= "<li>$item</li>";
                   1504:             }
1.250     droeschl 1505:             $html .= '</ul></li>';
1.242     droeschl 1506:             if ($category eq 'A') { $html .= "<li>$$links</li>"; }
                   1507:         }
                   1508:         $$links = $html.'</ul>';
                   1509:     }
                   1510: 
                   1511:     sub add_advtools {
                   1512:         my ($links) = @_;
                   1513:         return unless (defined $tools{'C'}) and (scalar (@{$tools{'C'}}) > 0);
                   1514:         my $html = start_funclist();
                   1515:         for my $item (@{$tools{'C'}}){
                   1516:                 next unless $item;
                   1517:                 $item =~ s/align="(right|left)"//;
                   1518:                 $html .= add_item_funclist($item);
                   1519:         }
                   1520:         $html   .= end_funclist();
                   1521:         $html    = Apache::loncommon::head_subbox($html);
                   1522:         $$links .= $html;
                   1523:     }
                   1524:     #SD END
1.53      matthew  1525: 
1.57      matthew  1526: } # End of scope for @Crumbs
1.53      matthew  1527: 
                   1528: ############################################################
                   1529: ############################################################
                   1530: 
1.112     raeburn  1531: # Nested table routines.
                   1532: #
                   1533: # Routines to display form items in a multi-row table with 2 columns.
                   1534: # Uses nested tables to divide form elements into segments.
                   1535: # For examples of use see loncom/interface/lonnotify.pm 
                   1536: #
                   1537: # Can be used in following order: ...
                   1538: # &start_pick_box()
                   1539: # row1
                   1540: # row2
                   1541: # row3   ... etc.
1.173     raeburn  1542: # &submit_row()
1.161     raeburn  1543: # &end_pick_box()
1.112     raeburn  1544: #
                   1545: # where row1, row 2 etc. are chosen from &role_select_row,&course_select_row,
                   1546: # &status_select_row and &email_default_row
                   1547: #
                   1548: # Can also be used in following order:
                   1549: #
                   1550: # &start_pick_box()
                   1551: # &row_title()
                   1552: # &row_closure()
                   1553: # &row_title()
                   1554: # &row_closure()  ... etc.
                   1555: # &submit_row()
                   1556: # &end_pick_box()
                   1557: #
                   1558: # In general a &submit_row() call should proceed the call to &end_pick_box(),
                   1559: # as this routine adds a button for form submission.
1.113     raeburn  1560: # &submit_row() does not require a &row_closure after it.
1.112     raeburn  1561: #  
                   1562: # &start_pick_box() creates a bounding table with 1-pixel wide black border.
                   1563: # rows should be placed between calls to &start_pick_box() and &end_pick_box.
                   1564: #
                   1565: # &row_title() adds a title in the left column for each segment.
                   1566: # &row_closure() closes a row with a 1-pixel wide black line.
                   1567: #
                   1568: # &role_select_row() provides a select box from which to choose 1 or more roles 
                   1569: # &course_select_row provides ways of picking groups of courses
                   1570: #    radio buttons: all, by category or by picking from a course picker pop-up
                   1571: #      note: by category option is only displayed if a domain has implemented 
                   1572: #                selection by year, semester, department, number etc.
                   1573: #
                   1574: # &status_select_row() provides a select box from which to choose 1 or more
                   1575: #  access types (current access, prior access, and future access)  
                   1576: #
                   1577: # &email_default_row() provides text boxes for default e-mail suffixes for
                   1578: #  different authentication types in a domain.
                   1579: #
                   1580: # &row_title() and &row_closure() are called internally by the &*_select_row
                   1581: # routines, but can also be called directly to start and end rows which have 
                   1582: # needs that are not accommodated by the *_select_row() routines.    
                   1583: 
1.193     bisitz   1584: { # Start: row_count block for pick_box
                   1585: my @row_count;
                   1586: 
1.112     raeburn  1587: sub start_pick_box {
1.142     albertel 1588:     my ($css_class) = @_;
                   1589:     if (defined($css_class)) {
                   1590: 	$css_class = 'class="'.$css_class.'"';
                   1591:     } else {
                   1592: 	$css_class= 'class="LC_pick_box"';
                   1593:     }
1.193     bisitz   1594:     unshift(@row_count,0);
1.112     raeburn  1595:     my $output = <<"END";
1.142     albertel 1596:  <table $css_class>
1.112     raeburn  1597: END
                   1598:     return $output;
                   1599: }
                   1600: 
                   1601: sub end_pick_box {
1.193     bisitz   1602:     shift(@row_count);
1.112     raeburn  1603:     my $output = <<"END";
                   1604:        </table>
                   1605: END
                   1606:     return $output;
                   1607: }
                   1608: 
1.181     bisitz   1609: sub row_headline {
                   1610:     my $output = <<"END";
                   1611:            <tr><td colspan="2">
                   1612: END
                   1613:     return $output;
                   1614: }
                   1615: 
1.112     raeburn  1616: sub row_title {
1.243     amueller 1617:     my ($title,$css_title_class,$css_value_class, $css_value_furtherAttributes) = @_;
1.193     bisitz   1618:     $row_count[0]++;
                   1619:     my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.142     albertel 1620:     $css_title_class ||= 'LC_pick_box_title';
                   1621:     $css_title_class = 'class="'.$css_title_class.'"';
                   1622: 
                   1623:     $css_value_class ||= 'LC_pick_box_value';
                   1624: 
1.173     raeburn  1625:     if ($title ne '') {
                   1626:         $title .= ':';
                   1627:     }
1.112     raeburn  1628:     my $output = <<"ENDONE";
1.243     amueller 1629:            <tr class="LC_pick_box_row" $css_value_furtherAttributes> 
1.142     albertel 1630:             <td $css_title_class>
1.173     raeburn  1631: 	       $title
1.112     raeburn  1632:             </td>
1.193     bisitz   1633:             <td class="$css_value_class $css_class">
1.112     raeburn  1634: ENDONE
                   1635:     return $output;
                   1636: }
                   1637: 
                   1638: sub row_closure {
1.143     albertel 1639:     my ($no_separator) =@_;
1.113     raeburn  1640:     my $output = <<"ENDTWO";
1.112     raeburn  1641:             </td>
                   1642:            </tr>
1.143     albertel 1643: ENDTWO
                   1644:     if (!$no_separator) {
                   1645:         $output .= <<"ENDTWO";
1.112     raeburn  1646:            <tr>
1.143     albertel 1647:             <td colspan="2" class="LC_pick_box_separator">
1.112     raeburn  1648:             </td>
                   1649:            </tr>
                   1650: ENDTWO
1.143     albertel 1651:     }
1.112     raeburn  1652:     return $output;
                   1653: }
                   1654: 
1.193     bisitz   1655: } # End: row_count block for pick_box
                   1656: 
                   1657: 
1.112     raeburn  1658: sub role_select_row {
1.147     raeburn  1659:     my ($roles,$title,$css_class,$show_separate_custom,$cdom,$cnum) = @_;
1.236     raeburn  1660:     my $crstype = 'Course';
                   1661:     if ($cdom ne '' && $cnum ne '') {
                   1662:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
                   1663:     }
1.116     raeburn  1664:     my $output;
                   1665:     if (defined($title)) {
1.142     albertel 1666:         $output = &row_title($title,$css_class);
1.116     raeburn  1667:     }
1.142     albertel 1668:     $output .= qq|
1.198     bisitz   1669:                                   <select name="roles" multiple="multiple">\n|;
1.113     raeburn  1670:     foreach my $role (@$roles) {
1.114     raeburn  1671:         my $plrole;
                   1672:         if ($role eq 'ow') {
                   1673:             $plrole = &mt('Course Owner');
1.147     raeburn  1674:         } elsif ($role eq 'cr') {
                   1675:             if ($show_separate_custom) {
                   1676:                 if ($cdom ne '' && $cnum ne '') {
                   1677:                     my %course_customroles = &course_custom_roles($cdom,$cnum);
                   1678:                     foreach my $crrole (sort(keys(%course_customroles))) {
                   1679:                         my ($plcrrole) = ($crrole =~ m|^cr/[^/]+/[^/]+/(.+)$|);
                   1680:                         $output .= '  <option value="'.$crrole.'">'.$plcrrole.
                   1681:                                    '</option>';
                   1682:                     }
                   1683:                 }
                   1684:             } else {
                   1685:                 $plrole = &mt('Custom Role');
                   1686:             }
1.114     raeburn  1687:         } else {
1.236     raeburn  1688:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.114     raeburn  1689:         }
1.147     raeburn  1690:         if (($role ne 'cr') || (!$show_separate_custom)) {
                   1691:             $output .= '  <option value="'.$role.'">'.$plrole.'</option>';
                   1692:         }
1.112     raeburn  1693:     }
1.142     albertel 1694:     $output .= qq|                </select>\n|;
1.116     raeburn  1695:     if (defined($title)) {
                   1696:         $output .= &row_closure();
                   1697:     }
1.112     raeburn  1698:     return $output;
                   1699: }
                   1700: 
                   1701: sub course_select_row {
1.142     albertel 1702:     my ($title,$formname,$totcodes,$codetitles,$idlist,$idlist_titles,
1.237     raeburn  1703: 	$css_class,$crstype) = @_;
1.142     albertel 1704:     my $output = &row_title($title,$css_class);
1.237     raeburn  1705:     $output .= &course_selection($formname,$totcodes,$codetitles,$idlist,$idlist_titles,$crstype);
1.169     raeburn  1706:     $output .= &row_closure();
                   1707:     return $output;
                   1708: }
                   1709: 
                   1710: sub course_selection {
1.237     raeburn  1711:     my ($formname,$totcodes,$codetitles,$idlist,$idlist_titles,$crstype) = @_;
1.169     raeburn  1712:     my $output = qq|
1.142     albertel 1713: <script type="text/javascript">
1.218     bisitz   1714: // <![CDATA[
1.112     raeburn  1715:     function coursePick (formname) {
                   1716:         for  (var i=0; i<formname.coursepick.length; i++) {
1.114     raeburn  1717:             if (formname.coursepick[i].value == 'category') {
                   1718:                 courseSet('');
                   1719:             }
1.112     raeburn  1720:             if (!formname.coursepick[i].checked) {
                   1721:                 if (formname.coursepick[i].value == 'specific') {
                   1722:                     formname.coursetotal.value = 0;
                   1723:                     formname.courselist = '';
                   1724:                 }
                   1725:             }
                   1726:         }
                   1727:     }
1.114     raeburn  1728:     function setPick (formname) {
                   1729:         for  (var i=0; i<formname.coursepick.length; i++) {
                   1730:             if (formname.coursepick[i].value == 'category') {
                   1731:                 formname.coursepick[i].checked = true;
                   1732:             }
                   1733:             formname.coursetotal.value = 0;
                   1734:             formname.courselist = '';
                   1735:         }
                   1736:     }
1.218     bisitz   1737: // ]]>
1.112     raeburn  1738: </script>
                   1739:     |;
1.237     raeburn  1740: 
                   1741:     my ($allcrs,$pickspec);
                   1742:     if ($crstype eq 'Community') {
                   1743:         $allcrs = &mt('All communities');
                   1744:         $pickspec = &mt('Pick specific communities:');
                   1745:     } else {
                   1746:         $allcrs = &mt('All courses');
                   1747:         $pickspec = &mt('Pick specific course(s):');
                   1748:     }
                   1749: 
1.112     raeburn  1750:     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
1.237     raeburn  1751:                      ($formname,'pickcourse','pickdomain','coursedesc','',1,$crstype).'</b>';
                   1752:         $output .= '<input type="radio" name="coursepick" value="all" onclick="coursePick(this.form)" />'.$allcrs.'<br />';
1.112     raeburn  1753:     if ($totcodes > 0) {
                   1754:         my $numtitles = @$codetitles;
                   1755:         if ($numtitles > 0) {
1.129     raeburn  1756:             $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  1757:             $output .= '<table><tr><td>'.$$codetitles[0].'<br />'."\n".
                   1758:                '<select name="'.$$codetitles[0].
1.114     raeburn  1759:                '" onChange="setPick(this.form);courseSet('."'$$codetitles[0]'".')">'."\n".
1.112     raeburn  1760:                ' <option value="-1" />Select'."\n";
                   1761:             my @items = ();
                   1762:             my @longitems = ();
                   1763:             if ($$idlist{$$codetitles[0]} =~ /","/) {
1.113     raeburn  1764:                 @items = split(/","/,$$idlist{$$codetitles[0]});
1.112     raeburn  1765:             } else {
                   1766:                 $items[0] = $$idlist{$$codetitles[0]};
                   1767:             }
                   1768:             if (defined($$idlist_titles{$$codetitles[0]})) {
                   1769:                 if ($$idlist_titles{$$codetitles[0]} =~ /","/) {
1.113     raeburn  1770:                     @longitems = split(/","/,$$idlist_titles{$$codetitles[0]});
1.112     raeburn  1771:                 } else {
                   1772:                     $longitems[0] = $$idlist_titles{$$codetitles[0]};
                   1773:                 }
                   1774:                 for (my $i=0; $i<@longitems; $i++) {
                   1775:                     if ($longitems[$i] eq '') {
                   1776:                         $longitems[$i] = $items[$i];
                   1777:                     }
                   1778:                 }
                   1779:             } else {
                   1780:                 @longitems = @items;
                   1781:             }
                   1782:             for (my $i=0; $i<@items; $i++) {
                   1783:                 $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
                   1784:             }
                   1785:             $output .= '</select></td>';
                   1786:             for (my $i=1; $i<$numtitles; $i++) {
                   1787:                 $output .= '<td>'.$$codetitles[$i].'<br />'."\n".
                   1788:                           '<select name="'.$$codetitles[$i].
                   1789:                           '" onChange="courseSet('."'$$codetitles[$i]'".')">'."\n".
                   1790:                           '<option value="-1">&lt;-Pick '.$$codetitles[$i-1].'</option>'."\n".
                   1791:                           '</select>'."\n".
                   1792:                           '</td>';
                   1793:             }
                   1794:             $output .= '</tr></table><br />';
                   1795:         }
                   1796:     }
1.238     raeburn  1797:     $output .= '<input type="radio" name="coursepick" value="specific" onclick="coursePick(this.form);opencrsbrowser('."'".$formname."','dccourse','dcdomain','coursedesc','','1','$crstype'".')" />'.$pickspec.' '.$courseform.'&nbsp;&nbsp;<input type="text" value="0" size="4" name="coursetotal" /><input type="hidden" name="courselist" value="" />selected.<br />'."\n";
1.112     raeburn  1798:     return $output;
                   1799: }
                   1800: 
                   1801: sub status_select_row {
1.142     albertel 1802:     my ($types,$title,$css_class) = @_;
1.117     raeburn  1803:     my $output; 
                   1804:     if (defined($title)) {
1.142     albertel 1805:         $output = &row_title($title,$css_class,'LC_pick_box_select');
1.117     raeburn  1806:     }
1.142     albertel 1807:     $output .= qq|
1.198     bisitz   1808:                                     <select name="types" multiple="multiple">\n|;
1.113     raeburn  1809:     foreach my $status_type (sort(keys(%{$types}))) {
1.112     raeburn  1810:         $output .= '  <option value="'.$status_type.'">'.$$types{$status_type}.'</option>';
                   1811:     }
1.142     albertel 1812:     $output .= qq|                   </select>\n|; 
1.117     raeburn  1813:     if (defined($title)) {
                   1814:         $output .= &row_closure();
                   1815:     }
1.112     raeburn  1816:     return $output;
                   1817: }
                   1818: 
                   1819: sub email_default_row {
1.142     albertel 1820:     my ($authtypes,$title,$descrip,$css_class) = @_;
                   1821:     my $output = &row_title($title,$css_class);
                   1822:     $output .= $descrip.
                   1823: 	&Apache::loncommon::start_data_table().
                   1824: 	&Apache::loncommon::start_data_table_header_row().
                   1825: 	'<th>'.&mt('Authentication Method').'</th>'.
                   1826: 	'<th align="right">'.&mt('Username -> e-mail conversion').'</th>'."\n".
                   1827: 	&Apache::loncommon::end_data_table_header_row();
1.112     raeburn  1828:     my $rownum = 0;
1.113     raeburn  1829:     foreach my $auth (sort(keys(%{$authtypes}))) {
1.112     raeburn  1830:         my ($userentry,$size);
                   1831:         if ($auth =~ /^krb/) {
                   1832:             $userentry = '';
                   1833:             $size = 25;
                   1834:         } else {
                   1835:             $userentry = 'username@';
                   1836:             $size = 15;
                   1837:         }
1.142     albertel 1838:         $output .= &Apache::loncommon::start_data_table_row().
                   1839: 	    '<td>  '.$$authtypes{$auth}.'</td>'.
                   1840: 	    '<td align="right">'.$userentry.
                   1841: 	    '<input type="text" name="'.$auth.'" size="'.$size.'" /></td>'.
                   1842: 	    &Apache::loncommon::end_data_table_row();
1.112     raeburn  1843:     }
1.142     albertel 1844:     $output .= &Apache::loncommon::end_data_table();
1.112     raeburn  1845:     $output .= &row_closure();
                   1846:     return $output;
                   1847: }
                   1848: 
                   1849: 
                   1850: sub submit_row {
1.142     albertel 1851:     my ($title,$cmd,$submit_text,$css_class) = @_;
                   1852:     my $output = &row_title($title,$css_class,'LC_pick_box_submit');
1.112     raeburn  1853:     $output .= qq|
                   1854:              <br />
                   1855:              <input type="hidden" name="command" value="$cmd" />
                   1856:              <input type="submit" value="$submit_text"/> &nbsp;
                   1857:              <br /><br />
1.142     albertel 1858:             \n|;
1.112     raeburn  1859:     return $output;
                   1860: }
1.1       stredwic 1861: 
1.147     raeburn  1862: sub course_custom_roles {
                   1863:     my ($cdom,$cnum) = @_;
                   1864:     my %returnhash=();
                   1865:     my %coursepersonnel=&Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   1866:     foreach my $person (sort(keys(%coursepersonnel))) {
                   1867:         my ($role) = ($person =~ /^([^:]+):/);
                   1868:         my ($end,$start) = split(/:/,$coursepersonnel{$person});
                   1869:         if ($end == -1 && $start == -1) {
                   1870:             next;
                   1871:         }
                   1872:         if ($role =~ m|^cr/[^/]+/[^/]+/[^/]|) {
                   1873:             $returnhash{$role} ++;
                   1874:         }
                   1875:     }
                   1876:     return %returnhash;
                   1877: }
                   1878: 
                   1879: 
1.119     raeburn  1880: ##############################################
                   1881: ##############################################
1.179     raeburn  1882: 
                   1883: # topic_bar
                   1884: #
1.248     wenzelju 1885: # Generates a div containing an (optional) number with a white background followed by a 
1.240     raeburn  1886: # title with a background color defined in the corresponding CSS: LC_topic_bar
                   1887: # Inputs:
1.248     wenzelju 1888: # 1. number to display.
                   1889: #    If input for number is empty only the title will be displayed. 
1.240     raeburn  1890: # 2. title text to display.
                   1891: # Outputs - a scalar containing html mark-up for the div.
                   1892: 
1.179     raeburn  1893: sub topic_bar {
1.248     wenzelju 1894:     my ($num,$title) = @_;
                   1895:     my $number = '';
                   1896:     if ($num ne '') {
                   1897:         $number = '<span>'.$num.'</span>';
1.239     amueller 1898:     }
1.248     wenzelju 1899:     return '<div class="LC_topic_bar">'.$number.$title.'</div>';
1.179     raeburn  1900: }
                   1901: 
                   1902: ##############################################
                   1903: ##############################################
1.119     raeburn  1904:                                                                              
                   1905: # echo_form_input
                   1906: #
                   1907: # Generates html markup to add form elements from the referrer page
                   1908: # as hidden form elements (values encoded) in the new page.
                   1909: #
                   1910: # Intended to support two types of use 
                   1911: # (a) to allow backing up to earlier pages in a multi-page 
                   1912: # form submission process using a breadcrumb trail.
                   1913: #
                   1914: # (b) to allow the current page to be reloaded with form elements
                   1915: # set on previous page to remain unchanged.  An example would
                   1916: # be where the a page containing a dynamically-built table of data is 
                   1917: # is to be redisplayed, with only the sort order of the data changed. 
                   1918: #  
                   1919: # Inputs:
                   1920: # 1. Reference to array of form elements in the submitted form on 
                   1921: # the referrer page which are to be excluded from the echoed elements.
                   1922: #
                   1923: # 2. Reference to array of regular expressions, which if matched in the  
                   1924: # name of the form element n the referrer page will be omitted from echo. 
                   1925: #
                   1926: # Outputs: A scalar containing the html markup for the echoed form
                   1927: # elements (all as hidden elements, with values encoded). 
                   1928: 
                   1929: 
                   1930: sub echo_form_input {
                   1931:     my ($excluded,$regexps) = @_;
                   1932:     my $output = '';
                   1933:     foreach my $key (keys(%env)) {
                   1934:         if ($key =~ /^form\.(.+)$/) {
                   1935:             my $name = $1;
                   1936:             my $match = 0;
                   1937:             if ((!@{$excluded}) || (!grep/^$name$/,@{$excluded})) {
                   1938:                 if (defined($regexps)) {
                   1939:                     if (@{$regexps} > 0) {
                   1940:                         foreach my $regexp (@{$regexps}) {
                   1941:                             if ($name =~ /\Q$regexp\E/) {
                   1942:                                 $match = 1;
                   1943:                                 last;
                   1944:                             }
                   1945:                         }
                   1946:                     }
                   1947:                 }
                   1948:                 if (!$match) {
                   1949:                     if (ref($env{$key})) {
                   1950:                         foreach my $value (@{$env{$key}}) {
                   1951:                             $value = &HTML::Entities::encode($value,'<>&"');
                   1952:                             $output .= '<input type="hidden" name="'.$name.
                   1953:                                              '" value="'.$value.'" />'."\n";
                   1954:                         }
                   1955:                     } else {
                   1956:                         my $value = &HTML::Entities::encode($env{$key},'<>&"');
                   1957:                         $output .= '<input type="hidden" name="'.$name.
                   1958:                                              '" value="'.$value.'" />'."\n";
                   1959:                     }
                   1960:                 }
                   1961:             }
                   1962:         }
                   1963:     }
                   1964:     return $output;
                   1965: }
                   1966: 
                   1967: ##############################################
                   1968: ##############################################
                   1969:                                                                              
                   1970: # set_form_elements
                   1971: #
                   1972: # Generates javascript to set form elements to values based on
                   1973: # corresponding values for the same form elements when the page was
                   1974: # previously submitted.
                   1975: #     
                   1976: # Last submission values are read from hidden form elements in referring 
                   1977: # page which have the same name, i.e., generated by &echo_form_input(). 
                   1978: #
                   1979: # Intended to be called by onload event.
                   1980: #
1.121     raeburn  1981: # Inputs:
                   1982: # (a) Reference to hash of echoed form elements to be set.
1.119     raeburn  1983: #
                   1984: # In the hash, keys are the form element names, and the values are the
                   1985: # element type (selectbox, radio, checkbox or text -for textbox, textarea or
                   1986: # hidden).
1.121     raeburn  1987: #
                   1988: # (b) Optional reference to hash of stored elements to be set.
                   1989: #
                   1990: # If the page being displayed is a page which permits modification of
                   1991: # previously stored data, e.g., the first page in a multi-page submission,
                   1992: # then if stored is supplied, form elements will be set to the last stored
                   1993: # values.  If user supplied values are also available for the same elements
                   1994: # these will replace the stored values. 
                   1995: #        
1.119     raeburn  1996: # Output:
                   1997: #  
                   1998: # javascript function - set_form_elements() which sets form elements,
                   1999: # expects an argument: formname - the name of the form according to 
                   2000: # the DOM, e.g., document.compose
                   2001: 
                   2002: sub set_form_elements {
1.121     raeburn  2003:     my ($elements,$stored) = @_;
                   2004:     my %values;
1.119     raeburn  2005:     my $output .= 'function setFormElements(courseForm) {
1.121     raeburn  2006: ';
                   2007:     if (defined($stored)) {
                   2008:         foreach my $name (keys(%{$stored})) {
                   2009:             if (exists($$elements{$name})) {
                   2010:                 if (ref($$stored{$name}) eq 'ARRAY') {
                   2011:                     $values{$name} = $$stored{$name};
                   2012:                 } else {
                   2013:                     @{$values{$name}} = ($$stored{$name});
                   2014:                 }
                   2015:             }
                   2016:         }
                   2017:     }
                   2018: 
1.119     raeburn  2019:     foreach my $key (keys(%env)) {
                   2020:         if ($key =~ /^form\.(.+)$/) {
                   2021:             my $name = $1;
                   2022:             if (exists($$elements{$name})) {
1.121     raeburn  2023:                 @{$values{$name}} = &Apache::loncommon::get_env_multiple($key);
                   2024:             }
                   2025:         }
                   2026:     }
                   2027: 
                   2028:     foreach my $name (keys(%values)) {
                   2029:         for (my $i=0; $i<@{$values{$name}}; $i++) {
                   2030:             $values{$name}[$i] = &HTML::Entities::decode($values{$name}[$i],'<>&"');
                   2031:             $values{$name}[$i] =~ s/([\r\n\f]+)/\\n/g;
                   2032:             $values{$name}[$i] =~ s/"/\\"/g;
                   2033:         }
1.234     raeburn  2034:         if (($$elements{$name} eq 'text') || ($$elements{$name} eq 'hidden')) {
1.121     raeburn  2035:             my $numvalues = @{$values{$name}};
                   2036:             if ($numvalues > 1) {
                   2037:                 my $valuestring = join('","',@{$values{$name}});
                   2038:                 $output .= qq|
1.119     raeburn  2039:   var textvalues = new Array ("$valuestring");
1.147     raeburn  2040:   var total = courseForm.elements['$name'].length;
1.119     raeburn  2041:   if (total > $numvalues) {
                   2042:       total = $numvalues;
                   2043:   }    
                   2044:   for (var i=0; i<total; i++) {
1.147     raeburn  2045:       courseForm.elements['$name']\[i].value = textvalues[i];
1.119     raeburn  2046:   }
                   2047: |;
1.121     raeburn  2048:             } else {
                   2049:                 $output .= qq|
1.147     raeburn  2050:   courseForm.elements['$name'].value = "$values{$name}[0]";
1.119     raeburn  2051: |;
1.121     raeburn  2052:             }
                   2053:         } else {
                   2054:             $output .=  qq|
1.147     raeburn  2055:   var elementLength = courseForm.elements['$name'].length;
1.119     raeburn  2056:   if (elementLength==undefined) {
                   2057: |;
1.121     raeburn  2058:             foreach my $value (@{$values{$name}}) {
                   2059:                 if ($$elements{$name} eq 'selectbox') {
                   2060:                     $output .=  qq|
1.147     raeburn  2061:       if (courseForm.elements['$name'].options[0].value == "$value") {
                   2062:           courseForm.elements['$name'].options[0].selected = true;
1.119     raeburn  2063:       }|;
1.121     raeburn  2064:                 } elsif (($$elements{$name} eq 'radio') ||
                   2065:                          ($$elements{$name} eq 'checkbox')) {
                   2066:                     $output .= qq|
1.147     raeburn  2067:       if (courseForm.elements['$name'].value == "$value") {
1.148     albertel 2068:           courseForm.elements['$name'].checked = true;
1.234     raeburn  2069:       } else {
                   2070:           courseForm.elements['$name'].checked = false;
1.119     raeburn  2071:       }|;
1.121     raeburn  2072:                 }
                   2073:             }
                   2074:             $output .= qq|
1.119     raeburn  2075:   }
                   2076:   else {
1.147     raeburn  2077:       for (var i=0; i<courseForm.elements['$name'].length; i++) {
1.119     raeburn  2078: |;
1.121     raeburn  2079:             if ($$elements{$name} eq 'selectbox') {
                   2080:                 $output .=  qq|
1.147     raeburn  2081:           courseForm.elements['$name'].options[i].selected = false;|;
1.121     raeburn  2082:             } elsif (($$elements{$name} eq 'radio') || 
                   2083:                      ($$elements{$name} eq 'checkbox')) {
                   2084:                 $output .= qq|
1.147     raeburn  2085:           courseForm.elements['$name']\[i].checked = false;|; 
1.121     raeburn  2086:             }
                   2087:             $output .= qq|
1.119     raeburn  2088:       }
1.147     raeburn  2089:       for (var j=0; j<courseForm.elements['$name'].length; j++) {
1.119     raeburn  2090: |;
1.121     raeburn  2091:             foreach my $value (@{$values{$name}}) {
                   2092:                 if ($$elements{$name} eq 'selectbox') {
                   2093:                     $output .=  qq|
1.147     raeburn  2094:           if (courseForm.elements['$name'].options[j].value == "$value") {
                   2095:               courseForm.elements['$name'].options[j].selected = true;
1.119     raeburn  2096:           }|;
1.121     raeburn  2097:                 } elsif (($$elements{$name} eq 'radio') ||
                   2098:                          ($$elements{$name} eq 'checkbox')) { 
                   2099:                       $output .= qq|
1.147     raeburn  2100:           if (courseForm.elements['$name']\[j].value == "$value") {
                   2101:               courseForm.elements['$name']\[j].checked = true;
1.119     raeburn  2102:           }|;
1.121     raeburn  2103:                 }
                   2104:             }
                   2105:             $output .= qq|
1.119     raeburn  2106:       }
                   2107:   }
                   2108: |;
                   2109:         }
                   2110:     }
                   2111:     $output .= "
1.235     raeburn  2112:     return;
1.119     raeburn  2113: }\n";
                   2114:     return $output;
                   2115: }
                   2116: 
1.158     raeburn  2117: ##############################################
                   2118: ##############################################
                   2119: 
                   2120: # javascript_valid_email
                   2121: #
                   2122: # Generates javascript to validate an e-mail address.
                   2123: # Returns a javascript function which accetps a form field as argumnent, and
                   2124: # returns false if field.value does not satisfy two regular expression matches
                   2125: # for a valid e-mail address.  Backwards compatible with old browsers without
                   2126: # support for javascript RegExp (just checks for @ in field.value in this case). 
                   2127: 
                   2128: sub javascript_valid_email {
                   2129:     my $scripttag .= <<'END';
                   2130: function validmail(field) {
                   2131:     var str = field.value;
                   2132:     if (window.RegExp) {
                   2133:         var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
                   2134:         var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"; //"
                   2135:         var reg1 = new RegExp(reg1str);
                   2136:         var reg2 = new RegExp(reg2str);
                   2137:         if (!reg1.test(str) && reg2.test(str)) {
                   2138:             return true;
                   2139:         }
                   2140:         return false;
                   2141:     }
                   2142:     else
                   2143:     {
                   2144:         if(str.indexOf("@") >= 0) {
                   2145:             return true;
                   2146:         }
                   2147:         return false;
                   2148:     }
                   2149: }
                   2150: END
                   2151:     return $scripttag;
                   2152: }
                   2153: 
1.219     droeschl 2154: 
                   2155: # USAGE: htmltag(element, content, {attribute => value,...});
                   2156: #
                   2157: # EXAMPLES: 
                   2158: #  - htmltag('a', 'this is an anchor', {href  => 'www.example.com', 
                   2159: #                                       title => 'this is a title'})
                   2160: #
                   2161: #  - You might want to set up needed tags like: 
                   2162: #
                   2163: #     my $h3  = sub { return htmltag( "h3",  @_ ) };
                   2164: #
                   2165: #    ... and use them: $h3->("This is a headline")
                   2166: #
                   2167: #  - To set up a couple of tags, see sub inittags
                   2168: #
                   2169: # NOTES:
                   2170: # - Empty elements, such as <br/> are correctly terminated, 
                   2171: #   i.e. htmltag('br') returns <br/> 
                   2172: # - Empty attributes (title="") are filtered out.
                   2173: # - The function will not check for deprecated attributes.
                   2174: #
                   2175: # OUTPUT: content enclosed in xhtml conform tags
                   2176: sub htmltag{
                   2177:     return
                   2178:         qq|<$_[0]|
                   2179:         . join( '', map { qq| $_="${$_[2]}{$_}"| if ${$_[2]}{$_} } keys %{ $_[2] } )
                   2180:         . ($_[1] ? qq|>$_[1]</$_[0]>| : qq|/>|). "\n";
                   2181: };
                   2182: 
                   2183: 
                   2184: # USAGE: inittags(@tags);
                   2185: #
                   2186: # EXAMPLES:
                   2187: #  - my ($h1, $h2, $h3) = initTags( qw( h1 h2 h3 ) )
                   2188: #    $h1->("This is a headline") #Returns: <h1>This is a headline</h1>
                   2189: #
                   2190: # NOTES: See sub htmltag for further information.
                   2191: #
                   2192: # OUTPUT: List of subroutines. 
                   2193: sub inittags {
                   2194:     my @tags = @_;
                   2195:     return map { my $tag = $_;
                   2196:                  sub { return htmltag( $tag, @_ ) }
                   2197:                } @tags;
                   2198: }
                   2199: 
                   2200: 
1.231     droeschl 2201: # USAGE: scripttag(scriptcode, [start|end|both]);
1.229     droeschl 2202: #
                   2203: # EXAMPLES: 
1.231     droeschl 2204: #  - scripttag("alert('Hello World!')", 'both') 
                   2205: #    returns:
                   2206: #    <script type="text/javascript">
                   2207: #    // BEGIN LON-CAPA Internal
                   2208: #    alert(Hello World!')
                   2209: #    // END LON-CAPA Internal
                   2210: #    </script>
1.229     droeschl 2211: #
                   2212: # NOTES:
                   2213: # - works currently only for javascripts
                   2214: #
1.231     droeschl 2215: # OUTPUT: 
                   2216: # Scriptcode properly enclosed in <script> and CDATA tags (and LC
                   2217: # Internal markers if 2nd argument is given)
1.229     droeschl 2218: sub scripttag {
1.231     droeschl 2219:     my ( $content, $marker ) = @_;
                   2220:     return unless defined $content;
                   2221: 
                   2222:     my $begin = "\n// BEGIN LON-CAPA Internal\n";
                   2223:     my $end   = "\n// END LON-CAPA Internal\n";
                   2224: 
                   2225:     if ($marker) {
                   2226:         $content  = $begin . $content if $marker eq 'start' or $marker eq 'both';
                   2227:         $content .= $end              if $marker eq 'end'   or $marker eq 'both';
                   2228:     }
                   2229: 
1.229     droeschl 2230:     $content = "\n// <![CDATA[\n$content\n// ]]>\n";
1.231     droeschl 2231: 
                   2232:     return htmltag('script', $content, {type => 'text/javascript'});
1.229     droeschl 2233: };
                   2234: 
                   2235: 
1.183     droeschl 2236: ##############################################
                   2237: ##############################################
                   2238: 
                   2239: # generate_menu
                   2240: #
                   2241: # Generates html markup for a menu. 
                   2242: #
                   2243: # Inputs:
                   2244: # An array of following structure:
                   2245: #   ({	categorytitle => 'Categorytitle',
                   2246: #	items => [
1.201     droeschl 2247: #		    {	
                   2248: #           linktext    =>	'Text to be displayed',
                   2249: #			url	        =>	'URL the link is pointing to, i.e. /adm/site?action=dosomething',
1.183     droeschl 2250: #			permission  =>	'Contains permissions as returned from lonnet::allowed(),
1.201     droeschl 2251: #					         must evaluate to true in order to activate the link',
1.184     droeschl 2252: #			icon        =>  'icon filename',
1.186     droeschl 2253: #			alttext	    =>	'alt text for the icon',
1.183     droeschl 2254: #			help	    =>	'Name of the corresponding helpfile',
                   2255: #			linktitle   =>	'Description of the link (used for title tag)'
                   2256: #		    },
                   2257: #		    ...
                   2258: #		]
                   2259: #   }, 
                   2260: #   ...
                   2261: #   )
                   2262: #
                   2263: # Outputs: A scalar containing the html markup for the menu.
                   2264: 
                   2265: # ---- Remove when done ----
                   2266: # This routine is part of the redesign of LON-CAPA and it's 
                   2267: # subject to change during this project.
                   2268: # Don't rely on its current functionality as it might be 
                   2269: # changed or removed.
                   2270: # --------------------------
                   2271: sub generate_menu {
                   2272:     my @menu = @_;
1.201     droeschl 2273:     # subs for specific html elements
1.219     droeschl 2274:     my ($h3, $div, $ul, $li, $a, $img) = inittags( qw(h3 div ul li a img) ); 
1.201     droeschl 2275:     
                   2276:     my @categories; # each element represents the entire markup for a category
                   2277:    
                   2278:     foreach my $category (@menu) {
                   2279:         my @links;  # contains the links for the current $category
                   2280:         foreach my $link (@{$$category{items}}) {
                   2281:             next unless $$link{permission};
                   2282:             
                   2283:             # create the markup for the current $link and push it into @links.
                   2284:             # each entry consists of an image and a text optionally followed 
                   2285:             # by a help link.
1.232     raeburn  2286:             push(@links,$li->(
1.201     droeschl 2287:                         $a->(
                   2288:                             $img->("", {
                   2289:                                 class => "LC_noBorder LC_middle",
                   2290:                                 src   => "/res/adm/pages/$$link{icon}",
1.202     droeschl 2291:                                 alt   => mt(defined($$link{alttext}) ?
                   2292:                                 $$link{alttext} : $$link{linktext})
1.201     droeschl 2293:                             }), {
                   2294:                             href  => $$link{url},
1.202     droeschl 2295:                             title => mt($$link{linktitle})
1.201     droeschl 2296:                             }).
1.202     droeschl 2297:                         $a->(mt($$link{linktext}), {
1.201     droeschl 2298:                             href  => $$link{url},
1.202     droeschl 2299:                             title => mt($$link{linktitle}),
1.201     droeschl 2300:                             class => "LC_menubuttons_link"
                   2301:                             }).
                   2302:                          (defined($$link{help}) ? 
                   2303:                          Apache::loncommon::help_open_topic($$link{help}) : ''),
1.232     raeburn  2304:                          {class => "LC_menubuttons_inline_text"}));
1.201     droeschl 2305:         }
                   2306: 
                   2307:         # wrap categorytitle in <h3>, concatenate with 
                   2308:         # joined and in <ul> tags wrapped @links
                   2309:         # and wrap everything in an enclosing <div> and push it into
                   2310:         # @categories
                   2311:         # such that each element looks like:
                   2312:         # <div><h3>title</h3><ul><li>...</li>...</ul></div>
                   2313:         # the category won't be added if there aren't any links
1.232     raeburn  2314:         push(@categories, 
1.202     droeschl 2315:             $div->($h3->(mt($$category{categorytitle}), {class=>"LC_hcell"}).
1.201     droeschl 2316:             $ul->(join('' ,@links),  {class =>"LC_ListStyleNormal" }),
1.232     raeburn  2317:             {class=>"LC_Box LC_400Box"})) if scalar(@links);
1.183     droeschl 2318:     }
1.201     droeschl 2319: 
                   2320:     # wrap the joined @categories in another <div> (column layout)
                   2321:     return $div->(join('', @categories), {class => "LC_columnSection"});
1.183     droeschl 2322: }
1.176     foxr     2323: 
1.224     bisitz   2324: ##############################################
                   2325: ##############################################
                   2326: 
                   2327: =pod
                   2328: 
                   2329: =item &start_funclist
                   2330: 
                   2331: Start list of available functions
                   2332: 
                   2333: Typically used to offer a simple list of available functions
                   2334: at top or bottom of page.
                   2335: All available functions/actions for the current page
                   2336: should be included in this list.
                   2337: 
                   2338: If the optional headline text is not provided, a default text will be used.
                   2339: 
                   2340: 
                   2341: Related routines:
                   2342: =over 4
                   2343: add_item_funclist
                   2344: end_funclist
                   2345: =back
                   2346: 
                   2347: 
                   2348: Inputs: (optional) headline text
                   2349: 
                   2350: Returns: HTML code with function list start
                   2351: 
                   2352: =cut
                   2353: 
                   2354: ##############################################
                   2355: ##############################################
                   2356: 
                   2357: sub start_funclist {
                   2358:     my($legendtext)=@_;
                   2359:     $legendtext=&mt('Functions') if !$legendtext;
1.244     droeschl 2360:     return '<ul class="LC_funclist"><li style="font-weight:bold; margin-left:0.8em;">'.$legendtext.'</li>'."\n";
1.224     bisitz   2361: }
                   2362: 
                   2363: 
                   2364: ##############################################
                   2365: ##############################################
                   2366: 
                   2367: =pod
                   2368: 
                   2369: =item &add_item_funclist
                   2370: 
                   2371: Adds an item to the list of available functions
                   2372: 
                   2373: Related routines:
                   2374: =over 4
                   2375: start_funclist
                   2376: end_funclist
                   2377: =back
                   2378: 
                   2379: Inputs: content item with text and link to function
                   2380: 
                   2381: Returns: HTML code with list item for funclist
                   2382: 
                   2383: =cut
                   2384: 
                   2385: ##############################################
                   2386: ##############################################
                   2387: 
                   2388: sub add_item_funclist {
                   2389:     my($content) = @_;
                   2390:     return '<li>'.$content.'</li>'."\n";
                   2391: }
                   2392: 
                   2393: ##############################################
                   2394: ##############################################
                   2395: 
                   2396: =pod
                   2397: 
                   2398: =item &end_funclist
                   2399: 
                   2400: End list of available functions
                   2401: 
                   2402: Related routines:
                   2403: =over 4
                   2404: start_funclist
                   2405: add_item_funclist
                   2406: =back
                   2407: 
                   2408: Inputs: ./.
                   2409: 
                   2410: Returns: HTML code with function list end
                   2411: 
                   2412: =cut
                   2413: 
                   2414: ##############################################
                   2415: ##############################################
                   2416: 
                   2417: sub end_funclist {
1.246     bisitz   2418:     return "</ul>\n";
1.224     bisitz   2419: }
                   2420: 
1.1       stredwic 2421: 1;
1.23      matthew  2422: 
1.1       stredwic 2423: __END__

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