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

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

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