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

1.2       www         1: # The LearningOnline Network with CAPA
                      2: # a pile of common html routines
                      3: #
1.324   ! foxr        4: # $Id: lonhtmlcommon.pm,v 1.323 2012/09/10 10:49:27 foxr 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.286     www        63: use HTML::Entities();
1.130     www        64: use LONCAPA;
1.1       stredwic   65: 
1.284     www        66: sub java_not_enabled {
                     67:    return "\n".'<span class="LC_error">'.
                     68:           &mt('The required Java applet could not be started. Please make sure to have Java installed and active in your browser.').
                     69:           "</span>\n";
                     70: }
1.247     www        71: 
                     72: sub coursepreflink {
                     73:    my ($text,$category)=@_;
                     74:    if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
1.296     www        75:       return '<a target="_top" href="'.&HTML::Entities::encode("/adm/courseprefs?phase=display&actions=$category",'<>&"').'"><span class="LC_setting">'.$text.'</span></a>';
1.247     www        76:    } else {
                     77:       return '';
                     78:    }
                     79: }
                     80: 
1.253     www        81: sub raw_href_to_link {
                     82:    my ($message)=@_;
1.264     faziophi   83:    $message=~s/(https?\:\/\/[^\s\'\"\<]+)([\s\<]|$)/<a href="$1"><tt>$1<\/tt><\/a>$2/gi;
1.253     www        84:    return $message;
                     85: }
                     86: 
1.286     www        87: sub entity_encode {
                     88:     my ($text)=@_;
                     89:     return &HTML::Entities::encode($text, '<>&"');
                     90: }
                     91: 
                     92: sub direct_parm_link {
                     93:     my ($linktext,$symb,$filter,$part,$target)=@_;
                     94:     $symb=&entity_encode($symb);
                     95:     $filter=&entity_encode($filter);
                     96:     $part=&entity_encode($part);
                     97:     if (($symb) && (&Apache::lonnet::allowed('opa')) && ($target ne 'tex')) {
1.315     raeburn    98:        return "<a target='_top' href='/adm/parmset?symb=$symb&amp;filter=$filter&amp;part=$part'><span class='LC_setting'>$linktext</span></a>";
1.286     www        99:     } else {
                    100:        return $linktext;
                    101:     }
                    102: }
1.208     www       103: ##############################################
                    104: ##############################################
                    105: 
1.309     raeburn   106: =item &confirm_success()
1.208     www       107: 
                    108: Successful completion of an operation message
                    109: 
                    110: =cut
                    111: 
                    112: sub confirm_success {
1.209     www       113:    my ($message,$failure)=@_;
                    114:    if ($failure) {
1.265     wenzelju  115:       return '<span class="LC_error" style="font-size: inherit;">'."\n"
1.218     bisitz    116:             .'<img src="/adm/lonIcons/navmap.wrong.gif" alt="'.&mt('Error').'" /> '."\n"
1.211     bisitz    117:             .$message."\n"
                    118:             .'</span>'."\n";
1.209     www       119:    } else {
1.211     bisitz    120:       return '<span class="LC_success">'."\n"
1.233     raeburn   121:             .'<img src="/adm/lonIcons/navmap.correct.gif" alt="'.&mt('OK').'" /> '."\n"
1.211     bisitz    122:             .$message."\n"
                    123:             .'</span>'."\n";
1.209     www       124:    }
1.208     www       125: }
1.176     foxr      126: 
                    127: ##############################################
                    128: ##############################################
                    129: 
                    130: =pod
                    131: 
1.309     raeburn   132: =item &dragmath_button()
1.176     foxr      133: 
1.177     raeburn   134: Creates a button that launches a dragmath popup-window, in which an 
                    135: expression can be edited and pasted as LaTeX into a specified textarea. 
                    136: 
                    137:   textarea - Name of the textarea to edit.
                    138:   helpicon - If true, show a help icon to the right of the button.
1.176     foxr      139: 
                    140: =cut
                    141: 
1.177     raeburn   142: sub dragmath_button {
                    143:     my ($textarea,$helpicon) = @_;
                    144:     my $help_text; 
                    145:     if ($helpicon) {
1.282     raeburn   146:         $help_text = &Apache::loncommon::help_open_topic('Authoring_Math_Editor',undef,undef,undef,undef,'mathhelpicon_'.$textarea);
1.177     raeburn   147:     }
1.178     bisitz    148:     my $buttontext=&mt('Edit Math');
1.177     raeburn   149:     return <<ENDDRAGMATH;
1.246     bisitz    150:                 <input type="button" value="$buttontext" onclick="javascript:mathedit('$textarea',document)" />$help_text
1.177     raeburn   151: ENDDRAGMATH
                    152: }
                    153: 
1.176     foxr      154: ##############################################
                    155: 
1.177     raeburn   156: =pod
                    157: 
1.309     raeburn   158: =item &dragmath_js()
1.177     raeburn   159: 
                    160: Javascript used to open pop-up window containing dragmath applet which 
                    161: can be used to paste LaTeX into a textarea.
1.309     raeburn   162: 
1.177     raeburn   163: =cut
1.176     foxr      164: 
1.177     raeburn   165: sub dragmath_js {
1.182     foxr      166:     my ($popup) = @_;
1.177     raeburn   167:     return <<ENDDRAGMATHJS;
                    168:                 <script type="text/javascript">
1.218     bisitz    169:                 // <![CDATA[
1.176     foxr      170:                   function mathedit(textarea, doc) {
                    171:                      targetEntry = textarea;
1.177     raeburn   172:                      targetDoc   = doc;
1.182     foxr      173:                      newwin  = window.open("/adm/dragmath/applet/$popup.html","","width=565,height=500,resizable");
1.176     foxr      174:                   }
1.218     bisitz    175:                 // ]]>
1.176     foxr      176:                 </script>
1.177     raeburn   177: 
                    178: ENDDRAGMATHJS
1.176     foxr      179: }
                    180: 
1.309     raeburn   181: ##############################################
                    182: ##############################################
                    183: 
                    184: =pod
                    185: 
                    186: =item &dependencies_button()
                    187: 
                    188: Creates a button that launches a popup-window, in which dependencies  
                    189: for the web page in the main window can be added to, replaced or deleted.  
                    190: 
                    191: =cut
                    192: 
                    193: sub dependencies_button {
                    194:     my $buttontext=&mt('Manage Dependencies');
                    195:     return <<"END";
                    196:                 <input type="button" value="$buttontext" onclick="javascript:dependencycheck();" />
                    197: END
                    198: }
                    199: 
                    200: ##############################################
                    201: 
                    202: =pod
                    203: 
                    204: =item &dependencycheck_js()
                    205: 
                    206: Javascript used to open pop-up window containing interface to manage 
                    207: dependencies for a web page uploaded diretcly to a course.
                    208: 
                    209: =cut
                    210: 
                    211: sub dependencycheck_js {
1.319     raeburn   212:     my ($symb,$title,$url) = @_;
1.309     raeburn   213:     my $link = '/adm/dependencies?symb='.&HTML::Entities::encode($symb,'<>&"').
1.319     raeburn   214:                '&title='.&HTML::Entities::encode($title,'<>&"').
                    215:                '&url='.&HTML::Entities::encode($url,'<>&"');
1.309     raeburn   216:     return <<ENDJS;
                    217:                 <script type="text/javascript">
                    218:                 // <![CDATA[
                    219:                   function dependencycheck() {
                    220:                      depwin  = window.open("$link","","width=750,height=500,resizable,scrollbars=yes");
                    221:                   }
                    222:                 // ]]>
                    223:                 </script>
                    224: ENDJS
                    225: }
1.182     foxr      226: 
1.40      www       227: ##############################################
                    228: ##############################################
                    229: 
                    230: =pod
                    231: 
1.309     raeburn   232: =item &authorbombs()
1.40      www       233: 
                    234: =cut
                    235: 
                    236: ##############################################
                    237: ##############################################
                    238: 
                    239: sub authorbombs {
                    240:     my $url=shift;
                    241:     $url=&Apache::lonnet::declutter($url);
1.155     albertel  242:     my ($udom,$uname)=($url=~m{^($LONCAPA::domain_re)/($LONCAPA::username_re)/});
1.40      www       243:     my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
1.232     raeburn   244:     foreach my $bomb (keys(%bombs)) {
                    245: 	if ($bomb =~ /^$udom\/$uname\//) {
1.40      www       246: 	    return '<a href="/adm/bombs/'.$url.
1.218     bisitz    247: 		'"><img src="'.&Apache::loncommon::lonhttpdurl('/adm/lonMisc/bomb.gif').'" alt="'.&mt('Bomb').'" border="0" /></a>'.
1.40      www       248: 		&Apache::loncommon::help_open_topic('About_Bombs');
                    249: 	}
                    250:     }
                    251:     return '';
                    252: }
1.26      matthew   253: 
                    254: ##############################################
                    255: ##############################################
                    256: 
1.41      www       257: sub recent_filename {
                    258:     my $area=shift;
1.130     www       259:     return 'nohist_recent_'.&escape($area);
1.41      www       260: }
                    261: 
                    262: sub store_recent {
1.136     albertel  263:     my ($area,$name,$value,$freeze)=@_;
1.41      www       264:     my $file=&recent_filename($area);
                    265:     my %recent=&Apache::lonnet::dump($file);
1.111     www       266:     if (scalar(keys(%recent))>20) {
1.41      www       267: # remove oldest value
1.136     albertel  268: 	my $oldest=time();
1.41      www       269: 	my $delkey='';
1.136     albertel  270: 	foreach my $item (keys(%recent)) {
                    271: 	    my $thistime=(split(/\&/,$recent{$item}))[0];
                    272: 	    if (($thistime ne "always_include") && ($thistime<$oldest)) {
1.41      www       273: 		$oldest=$thistime;
1.136     albertel  274: 		$delkey=$item;
1.41      www       275: 	    }
                    276: 	}
                    277: 	&Apache::lonnet::del($file,[$delkey]);
                    278:     }
                    279: # store new value
1.136     albertel  280:     my $timestamp;
                    281:     if ($freeze) {
                    282:         $timestamp = "always_include";
                    283:     } else {
                    284:         $timestamp = time();
                    285:     }   
1.41      www       286:     &Apache::lonnet::put($file,{ $name => 
1.136     albertel  287: 				 $timestamp.'&'.&escape($value) });
1.41      www       288: }
                    289: 
1.89      banghart  290: sub remove_recent {
                    291:     my ($area,$names)=@_;
                    292:     my $file=&recent_filename($area);
                    293:     return &Apache::lonnet::del($file,$names);
                    294: }
                    295: 
1.41      www       296: sub select_recent {
                    297:     my ($area,$fieldname,$event)=@_;
                    298:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
                    299:     my $return="\n<select name='$fieldname'".
1.96      albertel  300: 	($event?" onchange='$event'":'').
1.41      www       301: 	">\n<option value=''>--- ".&mt('Recent')." ---</option>";
1.136     albertel  302:     foreach my $value (sort(keys(%recent))) {
                    303: 	unless ($value =~/^error\:/) {
                    304: 	    my $escaped = &Apache::loncommon::escape_url($value);
1.160     albertel  305: 	    &Apache::loncommon::inhibit_menu_check(\$escaped);
1.251     raeburn   306:             if ($area eq 'residx') {
                    307:                 next if ((!&Apache::lonnet::allowed('bre',$value)) && (!&Apache::lonnet::allowed('bro',$value)));
                    308:             }
1.94      foxr      309: 	    $return.="\n<option value='$escaped'>".
1.136     albertel  310: 		&unescape((split(/\&/,$recent{$value}))[1]).
1.41      www       311: 		'</option>';
                    312: 	}
                    313:     }
                    314:     $return.="\n</select>\n";
                    315:     return $return;
                    316: }
                    317: 
1.97      albertel  318: sub get_recent {
                    319:     my ($area, $n) = @_;
                    320:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
                    321: 
                    322: # Create hash with key as time and recent as value
1.136     albertel  323: # Begin filling return_hash with any 'always_include' option
1.97      albertel  324:     my %time_hash = ();
1.136     albertel  325:     my %return_hash = ();
1.232     raeburn   326:     foreach my $item (keys(%recent)) {
1.136     albertel  327:         my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
                    328:         if ($thistime eq 'always_include') {
                    329:             $return_hash{$item} = &unescape($thisvalue);
                    330:             $n--;
                    331:         } else {
                    332:             $time_hash{$thistime} = $item;
1.133     albertel  333:         }
1.97      albertel  334:     }
                    335: 
                    336: # Sort by decreasing time and return key value pairs
                    337:     my $idx = 1;
1.136     albertel  338:     foreach my $item (reverse(sort(keys(%time_hash)))) {
                    339:        $return_hash{$time_hash{$item}} =
                    340:                   &unescape((split(/\&/,$recent{$time_hash{$item}}))[1]);
1.97      albertel  341:        if ($n && ($idx++ >= $n)) {last;}
                    342:     }
                    343: 
                    344:     return %return_hash;
                    345: }
                    346: 
1.136     albertel  347: sub get_recent_frozen {
                    348:     my ($area) = @_;
                    349:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
                    350: 
                    351: # Create hash with all 'frozen' items
                    352:     my %return_hash = ();
                    353:     foreach my $item (keys(%recent)) {
                    354:         my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
                    355:         if ($thistime eq 'always_include') {
                    356:             $return_hash{$item} = &unescape($thisvalue);
                    357:         }
                    358:     }
                    359:     return %return_hash;
                    360: }
                    361: 
1.97      albertel  362: 
1.41      www       363: 
1.26      matthew   364: =pod
                    365: 
1.309     raeburn   366: =item &textbox()
1.26      matthew   367: 
                    368: =cut
                    369: 
                    370: ##############################################
                    371: ##############################################
                    372: sub textbox {
                    373:     my ($name,$value,$size,$special) = @_;
                    374:     $size = 40 if (! defined($size));
1.128     albertel  375:     $value = &HTML::Entities::encode($value,'<>&"');
1.26      matthew   376:     my $Str = '<input type="text" name="'.$name.'" size="'.$size.'" '.
                    377:         'value="'.$value.'" '.$special.' />';
                    378:     return $Str;
                    379: }
                    380: 
                    381: ##############################################
                    382: ##############################################
                    383: 
                    384: =pod
                    385: 
1.309     raeburn   386: =item &checkbox()
1.26      matthew   387: 
                    388: =cut
                    389: 
                    390: ##############################################
                    391: ##############################################
                    392: sub checkbox {
1.68      matthew   393:     my ($name,$checked,$value) = @_;
                    394:     my $Str = '<input type="checkbox" name="'.$name.'" ';
                    395:     if (defined($value)) {
                    396:         $Str .= 'value="'.$value.'"';
                    397:     } 
                    398:     if ($checked) {
1.206     bisitz    399:         $Str .= ' checked="checked"';
1.68      matthew   400:     }
                    401:     $Str .= ' />';
1.26      matthew   402:     return $Str;
                    403: }
                    404: 
1.120     albertel  405: 
                    406: =pod
                    407: 
1.309     raeburn   408: =item &radiobutton()
1.120     albertel  409: 
                    410: =cut
                    411: 
                    412: ##############################################
                    413: ##############################################
                    414: sub radio {
                    415:     my ($name,$checked,$value) = @_;
                    416:     my $Str = '<input type="radio" name="'.$name.'" ';
                    417:     if (defined($value)) {
                    418:         $Str .= 'value="'.$value.'"';
                    419:     } 
                    420:     if ($checked eq $value) {
1.206     bisitz    421:         $Str .= ' checked="checked"';
1.120     albertel  422:     }
                    423:     $Str .= ' />';
                    424:     return $Str;
                    425: }
                    426: 
1.10      matthew   427: ##############################################
                    428: ##############################################
                    429: 
                    430: =pod
                    431: 
1.309     raeburn   432: =item &date_setter()
1.10      matthew   433: 
1.22      matthew   434: &date_setter returns html and javascript for a compact date-setting form.
1.309     raeburn   435: To retrieve values from it, use &get_date_from_form.
1.22      matthew   436: 
1.10      matthew   437: Inputs
                    438: 
                    439: =over 4
                    440: 
                    441: =item $dname 
                    442: 
                    443: The name to prepend to the form elements.  
                    444: The form elements defined will be dname_year, dname_month, dname_day,
                    445: dname_hour, dname_min, and dname_sec.
                    446: 
                    447: =item $currentvalue
                    448: 
                    449: The current setting for this time parameter.  A unix format time
                    450: (time in seconds since the beginning of Jan 1st, 1970, GMT.  
1.257     faziophi  451: An undefined value is taken to indicate the value is the current time
                    452: unless it is requested to leave it empty. See $includeempty.
1.10      matthew   453: Also, to be explicit, a value of 'now' also indicates the current time.
                    454: 
1.26      matthew   455: =item $special
                    456: 
                    457: Additional html/javascript to be associated with each element in
                    458: the date_setter.  See lonparmset for example usage.
                    459: 
1.59      matthew   460: =item $includeempty 
                    461: 
1.257     faziophi  462: If it is set (true) and no date/time value is provided,
                    463: the date/time fields are left empty.
                    464: 
1.59      matthew   465: =item $state
                    466: 
                    467: Specifies the initial state of the form elements.  Either 'disabled' or empty.
                    468: Defaults to empty, which indiciates the form elements are not disabled. 
                    469: 
1.22      matthew   470: =back
                    471: 
                    472: Bugs
                    473: 
                    474: The method used to restrict user input will fail in the year 2400.
                    475: 
1.10      matthew   476: =cut
                    477: 
                    478: ##############################################
                    479: ##############################################
                    480: sub date_setter {
1.67      matthew   481:     my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
1.134     raeburn   482:         $no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_;
1.175     raeburn   483:     my $now = time;
1.257     faziophi  484: 
                    485:     my $tzname;
                    486:     my ($sec,$min,$hour,$mday,$month,$year) = ('', '', undef,''.''.'');
                    487:     #other potentially useful values:    wkday,yrday,is_daylight_savings
                    488: 
1.59      matthew   489:     if (! defined($state) || $state ne 'disabled') {
                    490:         $state = '';
                    491:     }
1.67      matthew   492:     if (! defined($no_hh_mm_ss)) {
                    493:         $no_hh_mm_ss = 0;
                    494:     }
1.110     www       495:     if ($currentvalue eq 'now') {
1.257     faziophi  496:         $currentvalue = $now;
1.110     www       497:     }
1.257     faziophi  498:     
                    499:     # Default value: Set empty date field to current time
                    500:     # unless empty inclusion is requested
                    501:     if ((!$includeempty) && (!$currentvalue)) {
                    502:         $currentvalue = $now;
1.10      matthew   503:     }
1.257     faziophi  504:     # Do we have a date? Split it!
1.39      www       505:     if ($currentvalue) {
1.257     faziophi  506: 	($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($currentvalue);
                    507: 
                    508:         #No values provided for hour, min, sec? Use default 0
                    509:         if (($defhour) || ($defmin) || ($defsec)) {
                    510:             $sec  = ($defsec  ? $defsec  : 0);
                    511:             $min  = ($defmin  ? $defmin  : 0);
                    512:             $hour = ($defhour ? $defhour : 0);
                    513:         }
1.107     www       514:     }
1.10      matthew   515:     my $result = "\n<!-- $dname date setting form -->\n";
                    516:     $result .= <<ENDJS;
1.135     albertel  517: <script type="text/javascript">
1.218     bisitz    518: // <![CDATA[
1.10      matthew   519:     function $dname\_checkday() {
                    520:         var day   = document.$formname.$dname\_day.value;
                    521:         var month = document.$formname.$dname\_month.value;
                    522:         var year  = document.$formname.$dname\_year.value;
                    523:         var valid = true;
                    524:         if (day < 1) {
                    525:             document.$formname.$dname\_day.value = 1;
                    526:         } 
                    527:         if (day > 31) {
                    528:             document.$formname.$dname\_day.value = 31;
                    529:         }
                    530:         if ((month == 1)  || (month == 3)  || (month == 5)  ||
                    531:             (month == 7)  || (month == 8)  || (month == 10) ||
                    532:             (month == 12)) {
                    533:             if (day > 31) {
                    534:                 document.$formname.$dname\_day.value = 31;
                    535:                 day = 31;
                    536:             }
                    537:         } else if (month == 2 ) {
                    538:             if ((year % 4 == 0) && (year % 100 != 0)) {
                    539:                 if (day > 29) {
                    540:                     document.$formname.$dname\_day.value = 29;
                    541:                 }
                    542:             } else if (day > 29) {
                    543:                 document.$formname.$dname\_day.value = 28;
                    544:             }
                    545:         } else if (day > 30) {
                    546:             document.$formname.$dname\_day.value = 30;
                    547:         }
                    548:     }
1.95      matthew   549:     
1.59      matthew   550:     function $dname\_disable() {
                    551:         document.$formname.$dname\_month.disabled=true;
                    552:         document.$formname.$dname\_day.disabled=true;
                    553:         document.$formname.$dname\_year.disabled=true;
                    554:         document.$formname.$dname\_hour.disabled=true;
                    555:         document.$formname.$dname\_minute.disabled=true;
                    556:         document.$formname.$dname\_second.disabled=true;
                    557:     }
                    558: 
                    559:     function $dname\_enable() {
                    560:         document.$formname.$dname\_month.disabled=false;
                    561:         document.$formname.$dname\_day.disabled=false;
                    562:         document.$formname.$dname\_year.disabled=false;
                    563:         document.$formname.$dname\_hour.disabled=false;
                    564:         document.$formname.$dname\_minute.disabled=false;
                    565:         document.$formname.$dname\_second.disabled=false;        
                    566:     }
                    567: 
1.29      www       568:     function $dname\_opencalendar() {
1.59      matthew   569:         if (! document.$formname.$dname\_month.disabled) {
                    570:             var calwin=window.open(
1.29      www       571: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
                    572: document.$formname.$dname\_month.value+"&year="+
                    573: document.$formname.$dname\_year.value,
                    574:              "LONCAPAcal",
                    575:               "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
1.59      matthew   576:         }
1.29      www       577: 
                    578:     }
1.218     bisitz    579: // ]]>
1.10      matthew   580: </script>
                    581: ENDJS
1.192     bisitz    582:     $result .= '  <span class="LC_nobreak">';
1.96      albertel  583:     my $monthselector = qq{<select name="$dname\_month" $special $state onchange="javascript:$dname\_checkday()" >};
1.67      matthew   584:     # Month
1.10      matthew   585:     my @Months = qw/January February  March     April   May      June 
                    586:                     July    August    September October November December/;
                    587:     # Pad @Months with a bogus value to make indexing easier
                    588:     unshift(@Months,'If you can read this an error occurred');
1.95      matthew   589:     if ($includeempty) { $monthselector.="<option value=''></option>"; }
1.10      matthew   590:     for(my $m = 1;$m <=$#Months;$m++) {
1.228     bisitz    591:         $monthselector .= qq{      <option value="$m"};
                    592:         $monthselector .= ' selected="selected"' if ($m-1 eq $month);
                    593:         $monthselector .= '> '.&mt($Months[$m]).' </option>'."\n";
1.10      matthew   594:     }
1.95      matthew   595:     $monthselector.= '  </select>';
1.67      matthew   596:     # Day
1.96      albertel  597:     my $dayselector = qq{<input type="text" name="$dname\_day" $state value="$mday" size="3" $special onchange="javascript:$dname\_checkday()" />};
1.67      matthew   598:     # Year
1.226     bisitz    599:     my $yearselector = qq{<input type="text" name="$dname\_year" $state value="$year" size="5" $special onchange="javascript:$dname\_checkday()" />};
1.95      matthew   600:     #
                    601:     my $hourselector = qq{<select name="$dname\_hour" $special $state >};
                    602:     if ($includeempty) { 
                    603:         $hourselector.=qq{<option value=''></option>};
                    604:     }
                    605:     for (my $h = 0;$h<24;$h++) {
1.228     bisitz    606:         $hourselector .= qq{<option value="$h"};
                    607:         $hourselector .= ' selected="selected"' if (defined($hour) && $hour == $h);
1.95      matthew   608:         $hourselector .= ">";
                    609:         my $timest='';
                    610:         if ($h == 0) {
                    611:             $timest .= "12 am";
                    612:         } elsif($h == 12) {
                    613:             $timest .= "12 noon";
                    614:         } elsif($h < 12) {
                    615:             $timest .= "$h am";
                    616:         } else {
                    617:             $timest .= $h-12 ." pm";
                    618:         }
                    619:         $timest=&mt($timest);
                    620:         $hourselector .= $timest." </option>\n";
                    621:     }
                    622:     $hourselector .= "  </select>\n";
                    623:     my $minuteselector = qq{<input type="text" name="$dname\_minute" $special $state value="$min" size="3" />};
                    624:     my $secondselector= qq{<input type="text" name="$dname\_second" $special $state value="$sec" size="3" />};
1.134     raeburn   625:     my $cal_link;
                    626:     if (!$nolink) {
                    627:         $cal_link = qq{<a href="javascript:$dname\_opencalendar()">};
                    628:     }
1.95      matthew   629:     #
1.175     raeburn   630:     my $tzone = ' '.$tzname.' ';
1.95      matthew   631:     if ($no_hh_mm_ss) {
1.134     raeburn   632:         $result .= &mt('[_1] [_2] [_3] ',
1.174     raeburn   633:                        $monthselector,$dayselector,$yearselector).
                    634:                    $tzone;
1.134     raeburn   635:         if (!$nolink) {
1.141     albertel  636:             $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
1.134     raeburn   637:         }
1.95      matthew   638:     } else {
1.134     raeburn   639:         $result .= &mt('[_1] [_2] [_3] [_4] [_5]m [_6]s ',
                    640:                       $monthselector,$dayselector,$yearselector,
1.174     raeburn   641:                       $hourselector,$minuteselector,$secondselector).
                    642:                    $tzone;
1.134     raeburn   643:         if (!$nolink) {
1.141     albertel  644:             $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
1.134     raeburn   645:         }
1.67      matthew   646:     }
1.135     albertel  647:     $result .= "</span>\n<!-- end $dname date setting form -->\n";
1.10      matthew   648:     return $result;
                    649: }
                    650: 
1.175     raeburn   651: sub get_timedates {
                    652:     my ($epoch) = @_;
                    653:     my $dt = DateTime->from_epoch(epoch => $epoch)
                    654:                      ->set_time_zone(&Apache::lonlocal::gettimezone());
                    655:     my $tzname = $dt->time_zone_short_name();
                    656:     my $sec = $dt->second;
                    657:     my $min = $dt->minute;
                    658:     my $hour = $dt->hour;
                    659:     my $mday = $dt->day;
                    660:     my $month = $dt->month;
                    661:     if ($month) {
                    662:         $month --;
                    663:     }
                    664:     my $year = $dt->year;
                    665:     return ($tzname,$sec,$min,$hour,$mday,$month,$year);
                    666: }
1.166     banghart  667: 
                    668: sub build_url {
                    669:     my ($base, $fields)=@_;
                    670:     my $url;
                    671:     $url = $base.'?';
1.168     albertel  672:     foreach my $key (keys(%$fields)) {
                    673:         $url.=&escape($key).'='.&escape($$fields{$key}).'&amp;';
1.166     banghart  674:     }
                    675:     $url =~ s/&amp;$//;
                    676:     return $url;
                    677: }
                    678: 
                    679: 
1.10      matthew   680: ##############################################
                    681: ##############################################
                    682: 
1.22      matthew   683: =pod
                    684: 
1.309     raeburn   685: =item &get_date_from_form()
1.22      matthew   686: 
                    687: get_date_from_form retrieves the date specified in an &date_setter form.
1.10      matthew   688: 
                    689: Inputs:
                    690: 
                    691: =over 4
                    692: 
                    693: =item $dname
                    694: 
1.226     bisitz    695: The name passed to &date_setter, which prefixes the form elements.
1.10      matthew   696: 
                    697: =item $defaulttime
                    698: 
                    699: The unix time to use as the default in case of poor inputs.
                    700: 
                    701: =back
                    702: 
                    703: Returns: Unix time represented in the form.
                    704: 
                    705: =cut
                    706: 
                    707: ##############################################
                    708: ##############################################
                    709: sub get_date_from_form {
                    710:     my ($dname) = @_;
                    711:     my ($sec,$min,$hour,$day,$month,$year);
                    712:     #
1.104     albertel  713:     if (defined($env{'form.'.$dname.'_second'})) {
                    714:         my $tmpsec = $env{'form.'.$dname.'_second'};
1.10      matthew   715:         if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
                    716:             $sec = $tmpsec;
                    717:         }
1.64      albertel  718: 	if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
1.67      matthew   719:     } else {
                    720:         $sec = 0;
1.10      matthew   721:     }
1.104     albertel  722:     if (defined($env{'form.'.$dname.'_minute'})) {
                    723:         my $tmpmin = $env{'form.'.$dname.'_minute'};
1.10      matthew   724:         if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
                    725:             $min = $tmpmin;
                    726:         }
1.64      albertel  727: 	if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
1.67      matthew   728:     } else {
                    729:         $min = 0;
1.10      matthew   730:     }
1.104     albertel  731:     if (defined($env{'form.'.$dname.'_hour'})) {
                    732:         my $tmphour = $env{'form.'.$dname.'_hour'};
1.33      matthew   733:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
1.10      matthew   734:             $hour = $tmphour;
                    735:         }
1.67      matthew   736:     } else {
                    737:         $hour = 0;
1.10      matthew   738:     }
1.104     albertel  739:     if (defined($env{'form.'.$dname.'_day'})) {
                    740:         my $tmpday = $env{'form.'.$dname.'_day'};
1.10      matthew   741:         if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
                    742:             $day = $tmpday;
                    743:         }
                    744:     }
1.104     albertel  745:     if (defined($env{'form.'.$dname.'_month'})) {
                    746:         my $tmpmonth = $env{'form.'.$dname.'_month'};
1.10      matthew   747:         if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
1.175     raeburn   748:             $month = $tmpmonth;
1.10      matthew   749:         }
                    750:     }
1.104     albertel  751:     if (defined($env{'form.'.$dname.'_year'})) {
                    752:         my $tmpyear = $env{'form.'.$dname.'_year'};
1.175     raeburn   753:         if (($tmpyear =~ /^\d+$/) && ($tmpyear >= 1970)) {
                    754:             $year = $tmpyear;
1.10      matthew   755:         }
                    756:     }
1.175     raeburn   757:     if (($year<1970) || ($year>2037)) { return undef; }
1.33      matthew   758:     if (defined($sec) && defined($min)   && defined($hour) &&
1.175     raeburn   759:         defined($day) && defined($month) && defined($year)) {
                    760:         my $timezone = &Apache::lonlocal::gettimezone();
                    761:         my $dt = DateTime->new( year   => $year,
                    762:                                 month  => $month,
                    763:                                 day    => $day,
                    764:                                 hour   => $hour,
                    765:                                 minute => $min,
                    766:                                 second => $sec,
                    767:                                 time_zone => $timezone,
                    768:                               );
                    769:         my $epoch_time  = $dt->epoch;
                    770:         if ($epoch_time ne '') {
                    771:             return $epoch_time;
                    772:         } else {
                    773:             return undef;
                    774:         }
1.10      matthew   775:     } else {
                    776:         return undef;
                    777:     }
1.20      matthew   778: }
                    779: 
                    780: ##############################################
                    781: ##############################################
                    782: 
                    783: =pod
                    784: 
                    785: =item &pjump_javascript_definition()
                    786: 
                    787: Returns javascript defining the 'pjump' function, which opens up a
                    788: parameter setting wizard.
                    789: 
                    790: =cut
                    791: 
                    792: ##############################################
                    793: ##############################################
                    794: sub pjump_javascript_definition {
                    795:     my $Str = <<END;
1.109     www       796:     function pjump(type,dis,value,marker,ret,call,hour,min,sec) {
1.295     www       797:         openMyModal("/adm/rat/parameter.html?type="+escape(type)
1.20      matthew   798:                  +"&value="+escape(value)+"&marker="+escape(marker)
                    799:                  +"&return="+escape(ret)
1.109     www       800:                  +"&call="+escape(call)+"&name="+escape(dis)
                    801:                  +"&defhour="+escape(hour)+"&defmin="+escape(min)
1.295     www       802:                  +"&defsec="+escape(sec)+"&modal=1",350,350,'no');
1.20      matthew   803:     }
                    804: END
                    805:     return $Str;
1.10      matthew   806: }
                    807: 
                    808: ##############################################
                    809: ##############################################
1.17      matthew   810: 
                    811: =pod
                    812: 
                    813: =item &javascript_nothing()
                    814: 
                    815: Return an appropriate null for the users browser.  This is used
                    816: as the first arguement for window.open calls when you want a blank
                    817: window that you can then write to.
                    818: 
                    819: =cut
                    820: 
                    821: ##############################################
                    822: ##############################################
                    823: sub javascript_nothing {
                    824:     # mozilla and other browsers work with "''", but IE on mac does not.
                    825:     my $nothing = "''";
                    826:     my $user_browser;
                    827:     my $user_os;
1.104     albertel  828:     $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
                    829:     $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
1.17      matthew   830:     if (! defined($user_browser) || ! defined($user_os)) {
                    831:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    832:                            &Apache::loncommon::decode_user_agent();
                    833:     }
                    834:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                    835:         $nothing = "'javascript:void(0);'";
                    836:     }
                    837:     return $nothing;
                    838: }
                    839: 
1.90      www       840: ##############################################
                    841: ##############################################
                    842: sub javascript_docopen {
1.171     albertel  843:     my ($mimetype) = @_;
                    844:     $mimetype ||= 'text/html';
1.90      www       845:     # safari does not understand document.open() and loads "text/html"
                    846:     my $nothing = "''";
                    847:     my $user_browser;
                    848:     my $user_os;
1.104     albertel  849:     $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
                    850:     $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
1.90      www       851:     if (! defined($user_browser) || ! defined($user_os)) {
                    852:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    853:                            &Apache::loncommon::decode_user_agent();
                    854:     }
                    855:     if ($user_browser eq 'safari' && $user_os =~ 'mac') {
                    856:         $nothing = "document.clear()";
                    857:     } else {
1.171     albertel  858: 	$nothing = "document.open('$mimetype','replace')";
1.90      www       859:     }
                    860:     return $nothing;
                    861: }
                    862: 
1.21      matthew   863: 
1.17      matthew   864: ##############################################
                    865: ##############################################
                    866: 
1.21      matthew   867: =pod
1.17      matthew   868: 
1.21      matthew   869: =item &StatusOptions()
1.10      matthew   870: 
1.21      matthew   871: Returns html for a selection box which allows the user to choose the
                    872: enrollment status of students.  The selection box name is 'Status'.
1.6       stredwic  873: 
1.21      matthew   874: Inputs:
1.6       stredwic  875: 
1.21      matthew   876: $status: the currently selected status.  If undefined the value of
1.104     albertel  877: $env{'form.Status'} is taken.  If that is undefined, a value of 'Active'
1.21      matthew   878: is used.
1.6       stredwic  879: 
1.21      matthew   880: $formname: The name of the form.  If defined the onchange attribute of
                    881: the selection box is set to document.$formname.submit().
1.6       stredwic  882: 
1.21      matthew   883: $size: the size (number of lines) of the selection box.
1.6       stredwic  884: 
1.27      matthew   885: $onchange: javascript to use when the value is changed.  Enclosed in 
                    886: double quotes, ""s, not single quotes.
                    887: 
1.21      matthew   888: Returns: a perl string as described.
1.1       stredwic  889: 
1.21      matthew   890: =cut
1.9       stredwic  891: 
1.21      matthew   892: ##############################################
                    893: ##############################################
                    894: sub StatusOptions {
1.165     banghart  895:     my ($status, $formName,$size,$onchange,$mult)=@_;
1.21      matthew   896:     $size = 1 if (!defined($size));
                    897:     if (! defined($status)) {
                    898:         $status = 'Active';
1.104     albertel  899:         $status = $env{'form.Status'} if (exists($env{'form.Status'}));
1.9       stredwic  900:     }
1.1       stredwic  901: 
                    902:     my $Str = '';
                    903:     $Str .= '<select name="Status"';
1.165     banghart  904:     if (defined($mult)){
                    905:         $Str .= ' multiple="multiple" ';
                    906:     }
1.27      matthew   907:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
1.1       stredwic  908:         $Str .= ' onchange="document.'.$formName.'.submit()"';
1.27      matthew   909:     }
                    910:     if (defined($onchange)) {
                    911:         $Str .= ' onchange="'.$onchange.'"';
1.1       stredwic  912:     }
1.21      matthew   913:     $Str .= ' size="'.$size.'" ';
1.1       stredwic  914:     $Str .= '>'."\n";
1.153     raeburn   915:     foreach my $type (['Active',  &mt('Currently Has Access')],
                    916: 		      ['Future',  &mt('Will Have Future Access')],
                    917: 		      ['Expired', &mt('Previously Had Access')],
                    918: 		      ['Any',     &mt('Any Access Status')]) {
1.151     albertel  919: 	my ($name,$label) = @$type;
                    920: 	$Str .= '<option value="'.$name.'" ';
                    921: 	if ($status eq $name) {
                    922: 	    $Str .= 'selected="selected" ';
                    923: 	}
                    924: 	$Str .= '>'.$label.'</option>'."\n";
                    925:     }
                    926: 
1.1       stredwic  927:     $Str .= '</select>'."\n";
1.7       stredwic  928: }
1.12      matthew   929: 
                    930: ########################################################
                    931: ########################################################
1.7       stredwic  932: 
1.23      matthew   933: =pod
                    934: 
                    935: =item Progess Window Handling Routines
                    936: 
                    937: These routines handle the creation, update, increment, and closure of 
                    938: progress windows.  The progress window reports to the user the number
                    939: of items completed and an estimate of the time required to complete the rest.
                    940: 
                    941: =over 4
                    942: 
                    943: 
1.309     raeburn   944: =item &Create_PrgWin()
1.23      matthew   945: 
                    946: Writes javascript to the client to open a progress window and returns a
                    947: data structure used for bookkeeping.
                    948: 
                    949: Inputs
                    950: 
                    951: =over 4
                    952: 
                    953: =item $r Apache request
                    954: 
                    955: =item $number_to_do The total number of items being processed.
1.50      albertel  956: 
1.23      matthew   957: =back
                    958: 
                    959: Returns a hash containing the progress state data structure.
                    960: 
                    961: 
1.309     raeburn   962: =item &Update_PrgWin()
1.23      matthew   963: 
                    964: Updates the text in the progress indicator.  Does not increment the count.
                    965: See &Increment_PrgWin.
                    966: 
                    967: Inputs:
                    968: 
                    969: =over 4
                    970: 
                    971: =item $r Apache request
                    972: 
                    973: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
                    974: 
                    975: =item $displaystring The string to write to the status indicator
                    976: 
                    977: =back
                    978: 
                    979: Returns: none
                    980: 
                    981: 
1.309     raeburn   982: =item Increment_PrgWin()
1.23      matthew   983: 
1.276     bisitz    984: Increment the count of items completed for the progress window by $step or 1 if no step is provided.
1.23      matthew   985: 
                    986: Inputs:
                    987: 
                    988: =over 4
                    989: 
                    990: =item $r Apache request
                    991: 
                    992: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    993: 
                    994: =item $extraInfo A description of the items being iterated over.  Typically
                    995: 'student'.
                    996: 
1.279     bisitz    997: =item $step (optional) counter step. Will be set to default 1 if ommited. step must be greater than 0 or empty.
1.276     bisitz    998: 
1.23      matthew   999: =back
                   1000: 
                   1001: Returns: none
                   1002: 
                   1003: 
1.309     raeburn  1004: =item &Close_PrgWin()
1.23      matthew  1005: 
                   1006: Closes the progress window.
                   1007: 
                   1008: Inputs:
                   1009: 
                   1010: =over 4 
                   1011: 
                   1012: =item $r Apache request
                   1013: 
                   1014: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                   1015: 
                   1016: =back
                   1017: 
                   1018: Returns: none
                   1019: 
                   1020: =back
                   1021: 
                   1022: =cut
                   1023: 
                   1024: ########################################################
                   1025: ########################################################
                   1026: 
1.51      albertel 1027: 
1.7       stredwic 1028: # Create progress
                   1029: sub Create_PrgWin {
1.297     www      1030:     my ($r,$number_to_do)=@_;
1.49      albertel 1031:     my %prog_state;
1.16      albertel 1032:     $prog_state{'done'}=0;
1.23      matthew  1033:     $prog_state{'firststart'}=&Time::HiRes::time();
                   1034:     $prog_state{'laststart'}=&Time::HiRes::time();
1.16      albertel 1035:     $prog_state{'max'}=$number_to_do;
1.297     www      1036:     &Apache::loncommon::LCprogressbar($r); 
1.14      albertel 1037:     return %prog_state;
1.7       stredwic 1038: }
                   1039: 
                   1040: # update progress
                   1041: sub Update_PrgWin {
1.14      albertel 1042:     my ($r,$prog_state,$displayString)=@_;
1.297     www      1043:     &Apache::loncommon::LCprogressbarUpdate($r,undef,$displayString);
1.23      matthew  1044:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.14      albertel 1045: }
                   1046: 
                   1047: # increment progress state
                   1048: sub Increment_PrgWin {
1.275     bisitz   1049:     my ($r,$prog_state,$extraInfo,$step)=@_;
1.279     bisitz   1050:     $step = $step > 0 ? $step : 1;
1.275     bisitz   1051:     $$prog_state{'done'} += $step;
                   1052: 
                   1053:     # Catch (max modulo step) <> 0
                   1054:     my $current = $$prog_state{'done'};
                   1055:     my $last = ($$prog_state{'max'} - $current);
                   1056:     if ($last <= 0) {
                   1057:         $last = 1;
                   1058:         $current = $$prog_state{'max'};
                   1059:     }
                   1060: 
1.23      matthew  1061:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
1.275     bisitz   1062:         $current * $last;
1.16      albertel 1063:     $time_est = int($time_est);
1.80      matthew  1064:     #
                   1065:     my $min = int($time_est/60);
                   1066:     my $sec = $time_est % 60;
1.278     bisitz   1067: 
1.23      matthew  1068:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
                   1069:     if ($lasttime > 9) {
                   1070:         $lasttime = int($lasttime);
                   1071:     } elsif ($lasttime < 0.01) {
                   1072:         $lasttime = 0;
                   1073:     } else {
                   1074:         $lasttime = sprintf("%3.2f",$lasttime);
                   1075:     }
1.278     bisitz   1076: 
                   1077:     $sec = 0 if ($min >= 10); # Don't show seconds if remaining time >= 10 min.
                   1078:     $sec = 1 if ( ($min == 0) && ($sec == 0) ); # Little cheating: pretend to have 1 second remaining instead of 0 to have something to display
                   1079: 
                   1080:     my $timeinfo =
                   1081:         &mt('[_1]/[_2]:'
                   1082:            .' [quant,_3,minute,minutes,] [quant,_4,second ,seconds ,]remaining'
                   1083:            .' ([quant,_5,second] for '.$extraInfo.')',
                   1084:             $current,
                   1085:             $$prog_state{'max'},
                   1086:             $min,
                   1087:             $sec,
                   1088:             $lasttime);
1.297     www      1089:     my $percent=0;
                   1090:     if ($$prog_state{'max'}) {
                   1091:        $percent=int(100.*$current/$$prog_state{'max'});
                   1092:     }
                   1093:     &Apache::loncommon::LCprogressbarUpdate($r,$percent,$timeinfo);
1.23      matthew  1094:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.7       stredwic 1095: }
                   1096: 
                   1097: # close Progress Line
                   1098: sub Close_PrgWin {
1.14      albertel 1099:     my ($r,$prog_state)=@_;
1.297     www      1100:     &Apache::loncommon::LCprogressbarClose($r);
1.48      albertel 1101:     undef(%$prog_state);
                   1102: }
                   1103: 
1.34      www      1104: # ------------------------------------------------------- Puts directory header
                   1105: 
                   1106: sub crumbs {
1.252     bisitz   1107:     my ($uri,$target,$prefix,$form,$skiplast)=@_;
1.303     www      1108: # You cannot crumbnify uploaded or adm resources
                   1109:     if ($uri=~/^\/*(uploaded|adm)\//) { return &mt('(Internal Course/Group Content)'); }
1.100     raeburn  1110:     if ($target) {
                   1111:         $target = ' target="'.
                   1112:                   &Apache::loncommon::escape_single($target).'"';
                   1113:     }
1.252     bisitz   1114:     my $output='<span class="LC_filename">';
                   1115:     $output.=$prefix.'/';
1.249     raeburn  1116:     if (($env{'user.adv'}) || ($env{'user.author'})) {
1.252     bisitz   1117:         my $path=$prefix.'/';
                   1118:         foreach my $dir (split('/',$uri)) {
1.99      matthew  1119:             if (! $dir) { next; }
                   1120:             $path .= $dir;
1.252     bisitz   1121:             if ($path eq $uri) {
                   1122:                 if ($skiplast) {
                   1123:                     $output.=$dir;
1.132     www      1124:                     last;
1.252     bisitz   1125:                 } 
                   1126:             } else {
                   1127:                 $path.='/'; 
                   1128:             }
1.157     albertel 1129:             my $href_path = &HTML::Entities::encode($path,'<>&"');
1.252     bisitz   1130:             &Apache::loncommon::inhibit_menu_check(\$href_path);
                   1131:             if ($form) {
                   1132:                 my $href = 'javascript:'.$form.".action='".$href_path."';".$form.'.submit();';
                   1133:                 $output.=qq{<a href="$href"$target>$dir</a>/};
                   1134:             } else {
                   1135:                 $output.=qq{<a href="$href_path"$target>$dir</a>/};
                   1136:             }
                   1137:         }
1.35      www      1138:     } else {
1.252     bisitz   1139:         foreach my $dir (split('/',$uri)) {
1.149     albertel 1140:             if (! $dir) { next; }
1.252     bisitz   1141:             $output.=$dir.'/';
                   1142:         }
1.34      www      1143:     }
1.149     albertel 1144:     if ($uri !~ m|/$|) { $output=~s|/$||; }
1.252     bisitz   1145:     $output.='</span>';
                   1146: 
1.304     foxr     1147: 
1.252     bisitz   1148:     return $output;
1.34      www      1149: }
                   1150: 
1.85      www      1151: # --------------------- A function that generates a window for the spellchecker
                   1152: 
                   1153: sub spellheader {
1.123     albertel 1154:     my $start_page=
                   1155: 	&Apache::loncommon::start_page('Speller Suggestions',undef,
1.140     albertel 1156: 				       {'only_body'   => 1,
                   1157: 					'js_ready'    => 1,
                   1158: 					'bgcolor'     => '#DDDDDD',
                   1159: 				        'add_entries' => {
                   1160: 					    'onload' => 
                   1161:                                                'document.forms.spellcheckform.submit()',
                   1162:                                              }
                   1163: 				        });
1.123     albertel 1164:     my $end_page=
                   1165: 	&Apache::loncommon::end_page({'js_ready'  => 1}); 
                   1166: 
1.105     www      1167:     my $nothing=&javascript_nothing();
1.85      www      1168:     return (<<ENDCHECK);
                   1169: <script type="text/javascript"> 
1.218     bisitz   1170: // <![CDATA[
1.92      albertel 1171: //<!-- BEGIN LON-CAPA Internal
1.85      www      1172: var checkwin;
                   1173: 
1.140     albertel 1174: function spellcheckerwindow(string) {
                   1175:     var esc_string = string.replace(/\"/g,'&quot;');
1.105     www      1176:     checkwin=window.open($nothing,'spellcheckwin','height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
1.154     albertel 1177:     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      1178:     checkwin.document.close();
                   1179: }
1.92      albertel 1180: // END LON-CAPA Internal -->
1.218     bisitz   1181: // ]]>
1.85      www      1182: </script>
                   1183: ENDCHECK
                   1184: }
                   1185: 
                   1186: # ---------------------------------- Generate link to spell checker for a field
                   1187: 
                   1188: sub spelllink {
                   1189:     my ($form,$field)=@_;
                   1190:     my $linktext=&mt('Check Spelling');
                   1191:     return (<<ENDLINK);
1.140     albertel 1192: <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      1193: ENDLINK
                   1194: }
                   1195: 
1.281     raeburn  1196: # ------------------------------------------------- Output headers for CKEditor
1.124     albertel 1197: 
1.52      www      1198: sub htmlareaheaders {
1.255     faziophi 1199: 	my $s="";
1.260     faziophi 1200: 	if (&htmlareabrowser()) {
1.255     faziophi 1201: 		$s.=(<<ENDEDITOR);
                   1202: <script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
                   1203: ENDEDITOR
                   1204: 	}
                   1205:     $s.=(<<ENDJQUERY);
1.294     raeburn  1206: <script type="text/javascript" src="/adm/jQuery/js/jquery-1.6.2.min.js"></script>
                   1207: <script type="text/javascript" src="/adm/jQuery/js/jquery-ui-1.8.16.custom.min.js"></script>
                   1208: <link rel="stylesheet" type="text/css" href="/adm/jQuery/css/smoothness/jquery-ui-1.8.16.custom.css" />
1.301     foxr     1209: <script type="text/javascript" src="/adm/jpicker/js/jpicker-1.1.6.min.js" >
                   1210: </script>
                   1211: <link rel="stylesheet" type="text/css" href="/adm/jpicker/css/jPicker-1.1.6.min.css" />
1.310     raeburn  1212: <script type="text/javascript" src="/adm/countdown/js/jquery.countdown.js"></script>
                   1213: <link rel="stylesheet" type="text/css" href="/adm/countdown/css/jquery.countdown.css" />
1.320     foxr     1214: 
1.323     foxr     1215: <script type="text/javascript" src="/adm/spellchecker/js/jquery.spellchecker.min.js"></script>
1.320     foxr     1216: <link rel="stylesheet" type="text/css" href="/adm/spellchecker/css/spellchecker.css" />
                   1217: 
1.255     faziophi 1218: ENDJQUERY
                   1219: 	return $s;
1.52      www      1220: }
                   1221: 
1.76      www      1222: # ----------------------------------------------------------------- Preferences
                   1223: 
1.167     albertel 1224: # ------------------------------------------------- lang to use in html editor
                   1225: sub htmlarea_lang {
                   1226:     my $lang='en';
                   1227:     if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
                   1228: 	$lang=&mt('htmlarea_lang');
                   1229:     }
                   1230:     return $lang;
                   1231: }
                   1232: 
1.72      www      1233: # ----------------------------------------- Script to activate only some fields
                   1234: 
                   1235: sub htmlareaselectactive {
1.281     raeburn  1236:     my ($args) = @_; 
1.76      www      1237:     unless (&htmlareabrowser()) { return ''; }
1.262     raeburn  1238:     my $output='<script type="text/javascript" defer="defer">'."\n"
1.230     bisitz   1239:               .'// <![CDATA['."\n";
1.167     albertel 1240:     my $lang = &htmlarea_lang();
1.281     raeburn  1241:     my $fullpage = 'false';
1.282     raeburn  1242:     my ($dragmath_prefix,$dragmath_helpicon,$dragmath_whitespace);
1.281     raeburn  1243:     if (ref($args) eq 'HASH') {
                   1244:         if (exists($args->{'lang'})) {
                   1245:             if ($args->{'lang'} ne '') {
                   1246:                 $lang = $args->{'lang'};
                   1247:             }
                   1248:         }
                   1249:         if (exists($args->{'fullpage'})) { 
                   1250:             if ($args->{'fullpage'} eq 'true') {
                   1251:                 $fullpage = $args->{'fullpage'};
                   1252:             }
                   1253:         }
                   1254:         if (exists($args->{'dragmath'})) {
                   1255:             if ($args->{'dragmath'} ne '') {
                   1256:                 $dragmath_prefix = $args->{'dragmath'};
1.282     raeburn  1257:                 $dragmath_helpicon=&Apache::loncommon::lonhttpdurl("/adm/help/help.png");
                   1258:                 $dragmath_whitespace=&Apache::loncommon::lonhttpdurl("/adm/lonIcons/transparent1x1.gif");
1.281     raeburn  1259:             }
                   1260:         }
                   1261:     }
1.255     faziophi 1262:     $output.='
                   1263:     
                   1264:     function containsBlockHtml(id) {
1.281     raeburn  1265: 		var re = $("#"+id).html().search(/(?:\&lt\;|\<)(br|h1|h2|h3|h4|h5|h6|p|ol|ul|table|pre|address|blockquote|center|div)[\s]*((?:[\/]*[\s]*(?:\&gt\;|\>)|(?:\&gt\;|\>)[\s\S]*(?:\&lt\;|\<)\/[\s]*\1[\s]*\(?:\&gt\;|\>))/im);
1.255     faziophi 1266:     	return (re >= 0);
                   1267:     }
                   1268:     
                   1269:     function startRichEditor(id) {
                   1270:     	CKEDITOR.replace(id, 
                   1271:     		{
1.281     raeburn  1272:     			customConfig: "/ckeditor/loncapaconfig.js",
                   1273:                         language : "'.$lang.'",
                   1274:                         fullPage : '.$fullpage.',
1.255     faziophi 1275:     		}
                   1276:     	);
                   1277:     }
                   1278:     
                   1279:     function destroyRichEditor(id) {
                   1280:     	CKEDITOR.instances[id].destroy();
1.72      www      1281:     }
1.255     faziophi 1282:     
                   1283:     function editorHandler(event) {
                   1284:     	var rawid = $(this).attr("id");
1.281     raeburn  1285:     	var id = new RegExp("LC_rt_(.*)").exec(rawid)[1];
1.255     faziophi 1286:     	event.preventDefault();
1.281     raeburn  1287:     	var rt_enabled  = $(this).hasClass("LC_enable_rt");
                   1288:         if (rt_enabled) {
1.255     faziophi 1289:     		startRichEditor(id);
                   1290: 			$("#LC_rt_"+id).html("<b>&laquo; Plain text</b>");
                   1291: 			$("#LC_rt_"+id).attr("title", "Disable rich text formatting and edit in plain text");
                   1292: 			$("#LC_rt_"+id).addClass("LC_disable_rt");
                   1293: 			$("#LC_rt_"+id).removeClass("LC_enable_rt");
                   1294:     	} else {
                   1295: 			destroyRichEditor(id);
                   1296: 			$("#LC_rt_"+id).html("<b>Rich formatting &raquo;</b>");
                   1297: 			$("#LC_rt_"+id).attr("title", "Enable rich text formatting (bold, italic, etc.)");
                   1298: 			$("#LC_rt_"+id).addClass("LC_enable_rt");
                   1299: 			$("#LC_rt_"+id).removeClass("LC_disable_rt");
1.281     raeburn  1300: 	}';
                   1301:     if ($dragmath_prefix ne '') {
                   1302:         $output .= "\n                 var visible = '';
                   1303:                                        if (rt_enabled) {
                   1304:                                            visible = 'none';
                   1305:                                        }
                   1306:                                        editmath_visibility(id,visible);\n";
                   1307:     }
                   1308:     $output .= '
                   1309:     }
1.255     faziophi 1310:     $(document).ready(function(){
                   1311: 		$(".LC_richAlwaysOn").each(function() {
                   1312: 			startRichEditor($(this).attr("id"));
                   1313: 		});
                   1314: 		$(".LC_richDetectHtml").each(function() {
                   1315: 			var id = $(this).attr("id");
1.281     raeburn  1316:                         var rt_enabled = containsBlockHtml(id);
                   1317: 			if(rt_enabled) {
1.255     faziophi 1318: 				$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Disable rich text formatting and edit in plain text\" class=\"LC_disable_rt\"><b>&laquo; Plain text</b></a></div>");				
                   1319: 				startRichEditor(id);
1.281     raeburn  1320: 				$("#LC_rt_"+id).click(editorHandler);
1.255     faziophi 1321: 			}
                   1322: 			else {
                   1323: 				$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Enable rich text formatting (bold, italic, etc.)\" class=\"LC_enable_rt\"><b>Rich formatting &raquo;</b></a></div>");
                   1324: 				$("#LC_rt_"+id).click(editorHandler);
1.281     raeburn  1325: 			}';
                   1326:     if ($dragmath_prefix ne '') {
                   1327:         $output .= "\n                 var visible = '';
                   1328:                                        if (rt_enabled) {
                   1329:                                            visible = 'none';
                   1330:                                        }
                   1331:                                        editmath_visibility(id,visible);\n";
                   1332:     }
                   1333:     $output .= '
1.255     faziophi 1334: 		});
                   1335: 		$(".LC_richDefaultOn").each(function() {
                   1336: 			var id = $(this).attr("id");
                   1337: 			$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Disable rich text formatting and edit in plain text\" class=\"LC_disable_rt\"><b>&laquo; Plain text</b></a></div>");				
                   1338: 			startRichEditor(id);
                   1339: 			$("#LC_rt_"+id).click(editorHandler);
                   1340: 		});
                   1341: 		$(".LC_richDefaultOff").each(function() {
                   1342: 			var id = $(this).attr("id");
                   1343: 			$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Enable rich text formatting (bold, italic, etc.)\" class=\"LC_enable_rt\"><b>Rich formatting &raquo;</b></a></div>");
1.281     raeburn  1344: 			$("#LC_rt_"+id).click(editorHandler);
1.255     faziophi 1345: 		});
1.301     foxr     1346:                 $.fn.jPicker.defaults.images.clientPath="/adm/jpicker/images/";
                   1347:                 $(".colorchooser").jPicker();
                   1348: 
1.304     foxr     1349: 
1.302     foxr     1350: 	});
1.281     raeburn  1351: ';
1.306     foxr     1352:     # Code to put a due date countdown in 'duedatecountdown' span.
                   1353:     # This is currently located in the breadcrumb headers.
                   1354:     # note that the dueDateLayout is internatinoalized below.
                   1355:     # Here document is used to support the substitution into the javascript below.
1.307     foxr     1356:     # ..which unforunately necessitates escaping the $'s in the javascript.
                   1357:     # There are several times of importance
                   1358:     #
                   1359:     # serverDueDate -  The absolute time at which the problem expires.
                   1360:     # serverTime    -  The server's time when the problem finished computing.
                   1361:     # clientTime    -  The client's time...as close to serverTime as possible.
                   1362:     #                  The clientTime will be slightly later due to
                   1363:     #                  1. The latency between problem computation and 
                   1364:     #                     the first network action.
                   1365:     #                  2. The time required between the page load-start and the actual
                   1366:     #                     initial javascript execution that got clientTime.
                   1367:     # These are used as follows:
                   1368:     #   The difference between clientTime and serverTime are used to 
                   1369:     #   correct for differences in clock settings between the browser's system and the
                   1370:     #   server's.
                   1371:     #
                   1372:     #   The difference between clientTime and the time at which the ready() method
                   1373:     #   starts executing is used to estimate latencies for page load and submission.
                   1374:     #   Since this is an estimate, it is doubled.  The latency estimate + one minute
                   1375:     #   is used to determine when the countdown timer turns red to warn the user
                   1376:     #   to think about submitting.
1.306     foxr     1377: 
1.314     raeburn  1378:     my $dueDateLayout = &mt('Due in: {dn} {dl} {hnn}{sep}{mnn}{sep}{snn} [_1]',"<span id='submitearly'></span>");
                   1379:     my $early = '- <b>'.&mt('Submit Early').'</b>';
                   1380:     my $pastdue = '- <b>'.&mt('Past Due').'</b>';
1.306     foxr     1381:     $output .= <<JAVASCRIPT;
1.307     foxr     1382: 
                   1383:     var documentReadyTime;
                   1384: 
1.306     foxr     1385: \$(document).ready(function() {
                   1386:    if (typeof(dueDate) != "undefined") {
1.307     foxr     1387:        documentReadyTime = (new Date()).getTime();
1.306     foxr     1388:       \$("#duedatecountdown").countdown({until: dueDate, compact: true, 
                   1389:          layout: "$dueDateLayout",
                   1390:          onTick: function (periods) {
1.307     foxr     1391: 	    var latencyEstimate = (documentReadyTime - clientTime) * 2;
1.314     raeburn  1392:             if(\$.countdown.periodsToSeconds(periods) < (300 + latencyEstimate)) {
                   1393:                \$("#submitearly").html("$early");
                   1394:                if (\$.countdown.periodsToSeconds(periods) < 1) {
                   1395:                     \$("#submitearly").html("$pastdue");
                   1396:                }
                   1397:             }
1.307     foxr     1398:             if(\$.countdown.periodsToSeconds(periods) < (60 + latencyEstimate)) {
1.306     foxr     1399:                \$(this).css("color", "red");   //Highlight last minute.
                   1400:             }
                   1401:          }
                   1402:       });
                   1403:    }
                   1404: });
1.322     foxr     1405: 
                   1406:     /* This code describes the spellcheck options that will be used for
                   1407:        items with class 'spellchecked'.  It is necessary for those objects'
                   1408:        to explicitly request checking (e.g. onblur is a nice event for that).
                   1409:      */
                   1410:      \$(document).ready(function() {
                   1411: 	 \$(".spellchecked").spellchecker({
                   1412: 	   url: "/ajax/spellcheck",
                   1413: 	   lang: "en",                      
                   1414: 	   engine: "pspell",
                   1415: 	   suggestionBoxPosition: "below",
                   1416: 	   innerDocument: true
                   1417: 					  });
                   1418: 	 \$("textarea.spellchecked").spellchecker({
                   1419: 	   url: "/ajax/spellcheck",
                   1420: 	   lang: "en",                      
                   1421: 	   engine: "pspell",
                   1422: 	   suggestionBoxPosition: "below",
                   1423: 	   innerDocument: true
                   1424: 					  });
                   1425: 
                   1426: 			});
                   1427: 
1.324   ! foxr     1428:     function doSpellcheck(element, lang) {
        !          1429: 	\$(element).spellchecker('option', {lang: lang});
        !          1430: 	\$(element).spellchecker('check');
        !          1431:     }
        !          1432: 
1.322     foxr     1433: 
1.306     foxr     1434: JAVASCRIPT
1.281     raeburn  1435:     if ($dragmath_prefix ne '') {
                   1436:         $output .= '
                   1437: 
                   1438:      function editmath_visibility(id,value) {
                   1439: 
                   1440:          if ((id == "") || (id == null)) {
                   1441:              return;
                   1442:          }
                   1443:          var mathid = "'.$dragmath_prefix.'_"+id;
                   1444:          mathele = document.getElementById(mathid);
                   1445:          if (mathele == null) {
                   1446:              return;
                   1447:          }
                   1448:          mathele.style.display = value;
1.282     raeburn  1449:          var mathhelpicon = "'.$dragmath_prefix.'helpicon'.'_"+id;
                   1450:          mathhelpiconele = document.getElementById(mathhelpicon);
                   1451:          if (mathhelpiconele == null) {
                   1452:              return;
                   1453:          }
                   1454:          if (value == "none") {
                   1455:              mathhelpiconele.src = "'.$dragmath_whitespace.'";
                   1456:          } else {
                   1457:              mathhelpiconele.src = "'.$dragmath_helpicon.'";
                   1458:          }
1.281     raeburn  1459:      }
                   1460: ';
                   1461: 
                   1462:     }
1.218     bisitz   1463:     $output.="\nwindow.status='Activated Editfields';\n"
1.230     bisitz   1464:             .'// ]]>'."\n"
1.281     raeburn  1465:             .'</script>';
1.72      www      1466:     return $output;
                   1467: }
                   1468: 
1.61      www      1469: # --------------------------------------------------------------------- Blocked
                   1470: 
                   1471: sub htmlareablocked {
1.104     albertel 1472:     unless ($env{'environment.wysiwygeditor'} eq 'on') { return 1; }
1.71      www      1473:     return 0;
1.52      www      1474: }
                   1475: 
                   1476: # ---------------------------------------- Browser capable of running HTMLArea?
                   1477: 
                   1478: sub htmlareabrowser {
                   1479:     return 1;
                   1480: }
1.53      matthew  1481: 
1.287     www      1482: #
                   1483: # Should the "return to content" link be shown?
                   1484: #
                   1485: 
                   1486: sub show_return_link {
1.289     www      1487: 
                   1488:     unless ($env{'request.course.id'}) { return 0; }
                   1489:     if ($env{'request.noversionuri'}=~m{^/priv/} ||
1.318     raeburn  1490:         $env{'request.uri'}=~m{^/priv/}) { return 1; }
1.289     www      1491: 
1.287     www      1492:     if (($env{'request.noversionuri'} =~ m{^/adm/(viewclasslist|navmaps)($|\?)})
                   1493:         || ($env{'request.noversionuri'} =~ m{^/adm/.*/aboutme($|\?)})) {
                   1494: 
                   1495:         return if ($env{'form.register'});
                   1496:     }
                   1497:     return (($env{'request.noversionuri'}=~m{^/(res|public)/} &&
                   1498:              $env{'request.symb'} eq '')
                   1499:             ||
                   1500:             ($env{'request.noversionuri'}=~ m{^/cgi-bin/printout.pl})
                   1501:             ||
                   1502:             (($env{'request.noversionuri'}=~/^\/adm\//) &&
                   1503:              ($env{'request.noversionuri'}!~/^\/adm\/wrapper\//) &&
                   1504:              ($env{'request.noversionuri'}!~
                   1505:               m{^/adm/.*/(smppg|bulletinboard)($|\?)})
                   1506:            ));
                   1507: }
                   1508: 
                   1509: 
1.304     foxr     1510: ##
                   1511: #   Set the dueDate variable...note this is done in the timezone
                   1512: #   of the browser.
                   1513: #
                   1514: # @param epoch relative time at which the problem is due.
                   1515: #
                   1516: # @return the javascript fragment to set the date:
                   1517: #
                   1518: sub set_due_date {
                   1519:     my $dueStamp = shift;
                   1520:     my $duems    = $dueStamp * 1000; # Javascript Date object needs ms not seconds.
                   1521: 
                   1522:     my $now = time()*1000;
                   1523: 
                   1524:     # This slightly obscure bit of javascript sets the dueDate variable
                   1525:     # to the time in the browser at which the problem was due.  
                   1526:     # The code should correct for gross differences between the server
                   1527:     # and client's time setting
                   1528: 
1.315     raeburn  1529:      return <<"END";
                   1530: 
                   1531: <script type="text/javascript">
1.304     foxr     1532:   //<![CDATA[
                   1533: var serverDueDate = $duems;
                   1534: var serverTime    = $now;
                   1535: var clientTime    = (new Date()).getTime();
                   1536: var dueDate       = new Date(serverDueDate + (clientTime - serverTime));
                   1537: 
                   1538:   //]]>
                   1539: </script>
                   1540: 
1.315     raeburn  1541: END
1.307     foxr     1542: }
                   1543: ##
                   1544: # Sets the time at which the problem finished computing.
                   1545: # This just updates the serverTime and clientTime variables above.
                   1546: # Calling this in e.g. end_problem provides a better estimate of the
                   1547: # difference beetween the server and client time setting as 
                   1548: # the difference contains less of the latency/problem compute time.
                   1549: #
                   1550: sub set_compute_end_time {
                   1551: 
                   1552:     my $now = time()*1000;	# Javascript times are in ms.
1.316     raeburn  1553:     return <<"END";
                   1554: 
                   1555: <script type="text/javascript">
1.307     foxr     1556: //<![CDATA[
                   1557: serverTime = $now;
                   1558: clientTime = (new Date()).getTime();
                   1559: //]]>
                   1560: </script>
                   1561: 
1.316     raeburn  1562: END
1.304     foxr     1563: }
                   1564: 
1.53      matthew  1565: ############################################################
                   1566: ############################################################
                   1567: 
                   1568: =pod
                   1569: 
1.309     raeburn  1570: =item &breadcrumbs()
1.53      matthew  1571: 
                   1572: Compiles the previously registered breadcrumbs into an series of links.
                   1573: Additionally supports a 'component', which will be displayed on the
1.223     droeschl 1574: right side of the breadcrumbs enclosing div (without a link).
1.53      matthew  1575: A link to help for the component will be included if one is specified.
                   1576: 
                   1577: All inputs can be undef without problems.
                   1578: 
1.223     droeschl 1579: Inputs: $component (the text on the right side of the breadcrumbs trail),
1.53      matthew  1580:         $component_help
1.63      albertel 1581:         $menulink (boolean, controls whether to include a link to /adm/menu)
1.138     albertel 1582:         $helplink (if 'nohelp' don't include the orange help link)
                   1583:         $css_class (optional name for the class to apply to the table for CSS)
1.197     raeburn  1584:         $no_mt (optional flag, 1 if &mt() is _not_ to be applied to $component
                   1585:            when including the text on the right.
1.53      matthew  1586: Returns a string containing breadcrumbs for the current page.
                   1587: 
1.309     raeburn  1588: =item &clear_breadcrumbs()
1.53      matthew  1589: 
                   1590: Clears the previously stored breadcrumbs.
                   1591: 
1.309     raeburn  1592: =item &add_breadcrumb()
1.53      matthew  1593: 
                   1594: Pushes a breadcrumb on the stack of crumbs.
                   1595: 
                   1596: input: $breadcrumb, a hash reference.  The keys 'href','title', and 'text'
                   1597: are required.  If present the keys 'faq' and 'bug' will be used to provide
1.156     albertel 1598: links to the FAQ and bug sites. If the key 'no_mt' is present the 'title' 
                   1599: and 'text' values won't be sent through &mt()
1.53      matthew  1600: 
                   1601: returns: nothing    
                   1602: 
                   1603: =cut
                   1604: 
                   1605: ############################################################
                   1606: ############################################################
                   1607: {
                   1608:     my @Crumbs;
1.242     droeschl 1609:     my %tools = ();
1.57      matthew  1610:     
1.53      matthew  1611:     sub breadcrumbs {
1.314     raeburn  1612:         my ($component,$component_help,$menulink,$helplink,$css_class,$no_mt, 
                   1613:             $CourseBreadcrumbs) = @_;
1.53      matthew  1614:         #
1.215     droeschl 1615:         $css_class ||= 'LC_breadcrumbs';
1.205     amueller 1616: 
1.57      matthew  1617:         # Make the faq and bug data cascade
1.223     droeschl 1618:         my $faq  = '';
                   1619:         my $bug  = '';
                   1620:         my $help = '';
1.215     droeschl 1621:         # Crumb Symbol
1.223     droeschl 1622:         my $crumbsymbol = '&raquo;';
1.60      www      1623:         # The last breadcrumb does not have a link, so handle it separately.
1.53      matthew  1624:         my $last = pop(@Crumbs);
1.57      matthew  1625:         #
1.70      matthew  1626:         # The first one should be the course or a menu link
1.215     droeschl 1627:         if (!defined($menulink)) { $menulink=1; }
1.70      matthew  1628:         if ($menulink) {
                   1629:             my $description = 'Menu';
1.172     raeburn  1630:             my $no_mt_descr = 0;
1.269     raeburn  1631:             if ((exists($env{'request.course.id'})) && 
                   1632:                 ($env{'request.course.id'} ne '') && 
                   1633:                 ($env{'course.'.$env{'request.course.id'}.'.description'} ne '')) {
1.70      matthew  1634:                 $description = 
1.104     albertel 1635:                     $env{'course.'.$env{'request.course.id'}.'.description'};
1.172     raeburn  1636:                 $no_mt_descr = 1;
1.70      matthew  1637:             }
1.215     droeschl 1638:             $menulink =  {  href   =>'/adm/menu',
                   1639:                             title  =>'Go to main menu',
                   1640:                             target =>'_top',
                   1641:                             text   =>$description,
                   1642:                             no_mt  =>$no_mt_descr, };
                   1643:             if($last) {
                   1644:                 #$last set, so we have some crumbs
                   1645:                 unshift(@Crumbs,$menulink);
                   1646:             } else {
                   1647:                 #only menulink crumb present
                   1648:                 $last = $menulink;
                   1649:             }
1.53      matthew  1650:         }
1.287     www      1651:         my $links;
                   1652:         if ((&show_return_link) && (!$CourseBreadcrumbs)) {
1.299     raeburn  1653:             my $alttext = &mt('Go Back');
1.317     raeburn  1654:             $links=&htmltag( 'a','<img src="/res/adm/pages/tolastloc.png" alt="'.$alttext.'" class="LC_icon" />',
1.287     www      1655:                             { href => '/adm/flip?postdata=return:',
1.317     raeburn  1656:                               title => &mt('Back to most recent content resource'),
                   1657:                               class => 'LC_menubuttons_link',
                   1658:                             });
1.299     raeburn  1659:             $links=&htmltag('li',$links);
1.287     www      1660:         }
                   1661:         $links.= join "", 
1.261     droeschl 1662:              map {
                   1663:                  $faq  = $_->{'faq'}  if (exists($_->{'faq'}));
                   1664:                  $bug  = $_->{'bug'}  if (exists($_->{'bug'}));
                   1665:                  $help = $_->{'help'} if (exists($_->{'help'}));
                   1666: 
1.287     www      1667:                  my $result = $_->{no_mt} ? $_->{text} : &mt($_->{text});
1.261     droeschl 1668: 
                   1669:                  if ($_->{href}){
1.287     www      1670:                      $result = &htmltag( 'a', $result, 
1.261     droeschl 1671:                        { href   => $_->{href},
1.287     www      1672:                          title  => $_->{no_mt} ? $_->{title} : &mt($_->{title}),
1.261     droeschl 1673:                          target => $_->{target}, });
                   1674:                  }
                   1675: 
1.287     www      1676:                  $result = &htmltag( 'li', "$result $crumbsymbol");
1.261     droeschl 1677:              } @Crumbs;
1.223     droeschl 1678: 
                   1679:         #should the last Element be translated?
1.261     droeschl 1680: 
                   1681:         my $lasttext = $last->{'no_mt'} ? $last->{'text'} 
                   1682:                      : mt( $last->{'text'} );
                   1683: 
1.274     droeschl 1684:         # last breadcrumb is the first order heading of a page
                   1685:         # for course breadcrumbs it's just bold
1.304     foxr     1686: 
1.287     www      1687:         $links .= &htmltag( 'li', htmltag($CourseBreadcrumbs ? 'b' : 'h1',
1.274     droeschl 1688:                 $lasttext), {title => $lasttext});
1.223     droeschl 1689: 
1.54      matthew  1690:         my $icons = '';
1.223     droeschl 1691:         $faq  = $last->{'faq'}  if (exists($last->{'faq'}));
                   1692:         $bug  = $last->{'bug'}  if (exists($last->{'bug'}));
1.106     www      1693:         $help = $last->{'help'} if (exists($last->{'help'}));
                   1694:         $component_help=($component_help?$component_help:$help);
1.145     albertel 1695: #        if ($faq ne '') {
                   1696: #            $icons .= &Apache::loncommon::help_open_faq($faq);
                   1697: #        }
1.79      raeburn  1698: #        if ($bug ne '') {
                   1699: #            $icons .= &Apache::loncommon::help_open_bug($bug);
                   1700: #        }
1.223     droeschl 1701:         if ($faq ne '' || $component_help ne '' || $bug ne '') {
                   1702:             $icons .= &Apache::loncommon::help_open_menu($component,
                   1703:                                                          $component_help,
                   1704:                                                          $faq,$bug);
                   1705:         }
1.54      matthew  1706:         #
1.304     foxr     1707: 
1.205     amueller 1708: 		
                   1709: 
1.223     droeschl 1710:         unless ($CourseBreadcrumbs) {
1.287     www      1711:             $links = &htmltag('ol',  $links, { id => "LC_MenuBreadcrumbs"   });
1.223     droeschl 1712:         } else {
1.287     www      1713:             $links = &htmltag('ul',  $links, { class => "LC_CourseBreadcrumbs" });
1.53      matthew  1714:         }
1.223     droeschl 1715: 
1.304     foxr     1716: 
1.223     droeschl 1717:         if ($component) {
1.287     www      1718:             $links = &htmltag('span', 
1.223     droeschl 1719:                              ( $no_mt ? $component : mt($component) ).
                   1720:                              ( $icons ? $icons : '' ),
                   1721:                              { class => 'LC_breadcrumbs_component' } )
1.304     foxr     1722:                              .$links 
                   1723: ;
1.223     droeschl 1724:         }
                   1725:         
1.287     www      1726:         &render_tools(\$links);
                   1727:         $links = &htmltag('div', $links, 
1.225     bisitz   1728:                         { id => "LC_breadcrumbs" }) unless ($CourseBreadcrumbs) ;
1.287     www      1729:         &render_advtools(\$links);
1.223     droeschl 1730: 
1.53      matthew  1731:         # Return the @Crumbs stack to what we started with
                   1732:         push(@Crumbs,$last);
                   1733:         shift(@Crumbs);
1.304     foxr     1734: 
                   1735: 
1.223     droeschl 1736:         # Return the breadcrumb's line
1.304     foxr     1737: 
                   1738:     
                   1739: 
1.223     droeschl 1740:         return "$links";
1.53      matthew  1741:     }
                   1742: 
                   1743:     sub clear_breadcrumbs {
                   1744:         undef(@Crumbs);
1.242     droeschl 1745:         undef(%tools);
1.53      matthew  1746:     }
                   1747: 
                   1748:     sub add_breadcrumb {
1.232     raeburn  1749:         push(@Crumbs,@_);
1.53      matthew  1750:     }
1.242     droeschl 1751:     
1.309     raeburn  1752: =item &add_breadcrumb_tool($category, $html)
1.261     droeschl 1753: 
                   1754: Adds $html to $category of the breadcrumb toolbar container.
                   1755: 
                   1756: $html is usually a link to a page that invokes a function on the currently 
                   1757: displayed data (e.g. print when viewing a problem)
                   1758: 
                   1759: Currently there are 3 possible values for $category: 
                   1760: 
                   1761: =over 
                   1762: 
                   1763: =item navigation 
                   1764: left of breadcrumbs line
                   1765: 
                   1766: =item tools 
1.314     raeburn  1767: remaining items in right of breadcrumbs line
1.261     droeschl 1768: 
                   1769: =item advtools 
                   1770: advanced tools shown in a separate box below breadcrumbs line 
                   1771: 
                   1772: =back
                   1773:  
                   1774: returns: nothing
                   1775: 
                   1776: =cut
1.242     droeschl 1777: 
                   1778:     sub add_breadcrumb_tool {
1.261     droeschl 1779:         my ($category, @html) = @_;
                   1780:         return unless @html;
1.285     raeburn  1781:         if (!keys(%tools)) { 
1.261     droeschl 1782:             %tools = ( navigation => [], tools => [], advtools => []);
1.242     droeschl 1783:         }
1.261     droeschl 1784: 
                   1785:         #this cleans data received from lonmenu::innerregister
                   1786:         @html = grep {defined $_ && $_ ne ''} @html;
                   1787:         for (@html) { 
                   1788:             s/align="(right|left)"//; 
1.288     www      1789: #            s/<span.*?\/span>// if $category ne 'advtools'; 
1.261     droeschl 1790:         } 
                   1791: 
                   1792:         push @{$tools{$category}}, @html;
1.242     droeschl 1793:     }
                   1794: 
1.309     raeburn  1795: =item &clear_breadcrumb_tools()
1.261     droeschl 1796: 
                   1797: Clears the breadcrumb toolbar container.
                   1798: 
                   1799: returns: nothing
                   1800: 
                   1801: =cut
                   1802: 
1.245     droeschl 1803:     sub clear_breadcrumb_tools {
                   1804:         undef(%tools);
                   1805:     }
                   1806: 
1.309     raeburn  1807: =item &render_tools(\$breadcrumbs)
1.261     droeschl 1808: 
                   1809: Creates html for breadcrumb tools (categories navigation and tools) and inserts 
                   1810: \$breadcrumbs at the correct position.
                   1811: 
                   1812: input: \$breadcrumbs - a reference to the string containing prepared 
                   1813: breadcrumbs.
                   1814: 
                   1815: returns: nothing
1.309     raeburn  1816: 
1.261     droeschl 1817: =cut
                   1818: 
                   1819: #TODO might split this in separate functions for each category
                   1820:     sub render_tools {
                   1821:         my ($breadcrumbs) = @_;
1.285     raeburn  1822:         return unless (keys(%tools));
1.261     droeschl 1823: 
                   1824:         my $navigation = list_from_array($tools{navigation}, 
                   1825:                    { listattr => { class=>"LC_breadcrumb_tools_navigation" } });
                   1826:         my $tools = list_from_array($tools{tools}, 
                   1827:                    { listattr => { class=>"LC_breadcrumb_tools_tools" } });
                   1828:         $$breadcrumbs = list_from_array([$navigation, $tools, $$breadcrumbs], 
                   1829:                    { listattr => { class=>'LC_breadcrumb_tools_outerlist' } });
                   1830:     }
                   1831: 
1.309     raeburn  1832: =pod
                   1833: 
                   1834: =item &render_advtools(\$breadcrumbs)
1.261     droeschl 1835: 
                   1836: Creates html for advanced tools (category advtools) and inserts \$breadcrumbs 
                   1837: at the correct position.
                   1838: 
                   1839: input: \$breadcrumbs - a reference to the string containing prepared 
                   1840: breadcrumbs (after render_tools call).
                   1841: 
                   1842: returns: nothing
1.309     raeburn  1843: 
1.261     droeschl 1844: =cut
                   1845: 
                   1846:     sub render_advtools {
                   1847:         my ($breadcrumbs) = @_;
                   1848:         return unless     (defined $tools{'advtools'}) 
                   1849:                       and (scalar(@{$tools{'advtools'}}) > 0);
                   1850: 
                   1851:         $$breadcrumbs .= Apache::loncommon::head_subbox(
                   1852:                             funclist_from_array($tools{'advtools'}) );
1.242     droeschl 1853:     }
1.53      matthew  1854: 
1.57      matthew  1855: } # End of scope for @Crumbs
1.53      matthew  1856: 
                   1857: ############################################################
                   1858: ############################################################
                   1859: 
1.112     raeburn  1860: # Nested table routines.
                   1861: #
                   1862: # Routines to display form items in a multi-row table with 2 columns.
                   1863: # Uses nested tables to divide form elements into segments.
                   1864: # For examples of use see loncom/interface/lonnotify.pm 
                   1865: #
                   1866: # Can be used in following order: ...
                   1867: # &start_pick_box()
                   1868: # row1
                   1869: # row2
                   1870: # row3   ... etc.
1.173     raeburn  1871: # &submit_row()
1.161     raeburn  1872: # &end_pick_box()
1.112     raeburn  1873: #
                   1874: # where row1, row 2 etc. are chosen from &role_select_row,&course_select_row,
                   1875: # &status_select_row and &email_default_row
                   1876: #
                   1877: # Can also be used in following order:
                   1878: #
                   1879: # &start_pick_box()
                   1880: # &row_title()
                   1881: # &row_closure()
                   1882: # &row_title()
                   1883: # &row_closure()  ... etc.
                   1884: # &submit_row()
                   1885: # &end_pick_box()
                   1886: #
                   1887: # In general a &submit_row() call should proceed the call to &end_pick_box(),
                   1888: # as this routine adds a button for form submission.
1.113     raeburn  1889: # &submit_row() does not require a &row_closure after it.
1.112     raeburn  1890: #  
                   1891: # &start_pick_box() creates a bounding table with 1-pixel wide black border.
                   1892: # rows should be placed between calls to &start_pick_box() and &end_pick_box.
                   1893: #
                   1894: # &row_title() adds a title in the left column for each segment.
                   1895: # &row_closure() closes a row with a 1-pixel wide black line.
                   1896: #
                   1897: # &role_select_row() provides a select box from which to choose 1 or more roles 
                   1898: # &course_select_row provides ways of picking groups of courses
                   1899: #    radio buttons: all, by category or by picking from a course picker pop-up
                   1900: #      note: by category option is only displayed if a domain has implemented 
                   1901: #                selection by year, semester, department, number etc.
                   1902: #
                   1903: # &status_select_row() provides a select box from which to choose 1 or more
                   1904: #  access types (current access, prior access, and future access)  
                   1905: #
                   1906: # &email_default_row() provides text boxes for default e-mail suffixes for
                   1907: #  different authentication types in a domain.
                   1908: #
                   1909: # &row_title() and &row_closure() are called internally by the &*_select_row
                   1910: # routines, but can also be called directly to start and end rows which have 
                   1911: # needs that are not accommodated by the *_select_row() routines.    
                   1912: 
1.193     bisitz   1913: { # Start: row_count block for pick_box
                   1914: my @row_count;
                   1915: 
1.112     raeburn  1916: sub start_pick_box {
1.313     raeburn  1917:     my ($css_class,$id) = @_;
1.142     albertel 1918:     if (defined($css_class)) {
                   1919: 	$css_class = 'class="'.$css_class.'"';
                   1920:     } else {
                   1921: 	$css_class= 'class="LC_pick_box"';
                   1922:     }
1.313     raeburn  1923:     my $table_id;
                   1924:     if (defined($id)) {
                   1925:         $table_id = ' id="'.$id.'"';
                   1926:     }
1.193     bisitz   1927:     unshift(@row_count,0);
1.112     raeburn  1928:     my $output = <<"END";
1.313     raeburn  1929:  <table $css_class $table_id>
1.112     raeburn  1930: END
                   1931:     return $output;
                   1932: }
                   1933: 
                   1934: sub end_pick_box {
1.193     bisitz   1935:     shift(@row_count);
1.112     raeburn  1936:     my $output = <<"END";
                   1937:        </table>
                   1938: END
                   1939:     return $output;
                   1940: }
                   1941: 
1.181     bisitz   1942: sub row_headline {
                   1943:     my $output = <<"END";
                   1944:            <tr><td colspan="2">
                   1945: END
                   1946:     return $output;
                   1947: }
                   1948: 
1.112     raeburn  1949: sub row_title {
1.243     amueller 1950:     my ($title,$css_title_class,$css_value_class, $css_value_furtherAttributes) = @_;
1.193     bisitz   1951:     $row_count[0]++;
                   1952:     my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.142     albertel 1953:     $css_title_class ||= 'LC_pick_box_title';
                   1954:     $css_title_class = 'class="'.$css_title_class.'"';
                   1955: 
                   1956:     $css_value_class ||= 'LC_pick_box_value';
                   1957: 
1.173     raeburn  1958:     if ($title ne '') {
                   1959:         $title .= ':';
                   1960:     }
1.112     raeburn  1961:     my $output = <<"ENDONE";
1.243     amueller 1962:            <tr class="LC_pick_box_row" $css_value_furtherAttributes> 
1.142     albertel 1963:             <td $css_title_class>
1.173     raeburn  1964: 	       $title
1.112     raeburn  1965:             </td>
1.193     bisitz   1966:             <td class="$css_value_class $css_class">
1.112     raeburn  1967: ENDONE
                   1968:     return $output;
                   1969: }
                   1970: 
                   1971: sub row_closure {
1.143     albertel 1972:     my ($no_separator) =@_;
1.113     raeburn  1973:     my $output = <<"ENDTWO";
1.112     raeburn  1974:             </td>
                   1975:            </tr>
1.143     albertel 1976: ENDTWO
                   1977:     if (!$no_separator) {
                   1978:         $output .= <<"ENDTWO";
1.112     raeburn  1979:            <tr>
1.143     albertel 1980:             <td colspan="2" class="LC_pick_box_separator">
1.112     raeburn  1981:             </td>
                   1982:            </tr>
                   1983: ENDTWO
1.143     albertel 1984:     }
1.112     raeburn  1985:     return $output;
                   1986: }
                   1987: 
1.193     bisitz   1988: } # End: row_count block for pick_box
                   1989: 
1.112     raeburn  1990: sub role_select_row {
1.147     raeburn  1991:     my ($roles,$title,$css_class,$show_separate_custom,$cdom,$cnum) = @_;
1.236     raeburn  1992:     my $crstype = 'Course';
                   1993:     if ($cdom ne '' && $cnum ne '') {
                   1994:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
                   1995:     }
1.116     raeburn  1996:     my $output;
                   1997:     if (defined($title)) {
1.142     albertel 1998:         $output = &row_title($title,$css_class);
1.116     raeburn  1999:     }
1.142     albertel 2000:     $output .= qq|
1.198     bisitz   2001:                                   <select name="roles" multiple="multiple">\n|;
1.113     raeburn  2002:     foreach my $role (@$roles) {
1.114     raeburn  2003:         my $plrole;
                   2004:         if ($role eq 'ow') {
                   2005:             $plrole = &mt('Course Owner');
1.147     raeburn  2006:         } elsif ($role eq 'cr') {
                   2007:             if ($show_separate_custom) {
                   2008:                 if ($cdom ne '' && $cnum ne '') {
                   2009:                     my %course_customroles = &course_custom_roles($cdom,$cnum);
                   2010:                     foreach my $crrole (sort(keys(%course_customroles))) {
                   2011:                         my ($plcrrole) = ($crrole =~ m|^cr/[^/]+/[^/]+/(.+)$|);
                   2012:                         $output .= '  <option value="'.$crrole.'">'.$plcrrole.
                   2013:                                    '</option>';
                   2014:                     }
                   2015:                 }
                   2016:             } else {
                   2017:                 $plrole = &mt('Custom Role');
                   2018:             }
1.114     raeburn  2019:         } else {
1.236     raeburn  2020:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.114     raeburn  2021:         }
1.147     raeburn  2022:         if (($role ne 'cr') || (!$show_separate_custom)) {
                   2023:             $output .= '  <option value="'.$role.'">'.$plrole.'</option>';
                   2024:         }
1.112     raeburn  2025:     }
1.142     albertel 2026:     $output .= qq|                </select>\n|;
1.116     raeburn  2027:     if (defined($title)) {
                   2028:         $output .= &row_closure();
                   2029:     }
1.112     raeburn  2030:     return $output;
                   2031: }
                   2032: 
                   2033: sub course_select_row {
1.142     albertel 2034:     my ($title,$formname,$totcodes,$codetitles,$idlist,$idlist_titles,
1.280     raeburn  2035: 	$css_class,$crstype,$standardnames) = @_;
1.142     albertel 2036:     my $output = &row_title($title,$css_class);
1.280     raeburn  2037:     $output .= &course_selection($formname,$totcodes,$codetitles,$idlist,$idlist_titles,$crstype,$standardnames);
1.169     raeburn  2038:     $output .= &row_closure();
                   2039:     return $output;
                   2040: }
                   2041: 
                   2042: sub course_selection {
1.280     raeburn  2043:     my ($formname,$totcodes,$codetitles,$idlist,$idlist_titles,$crstype,$standardnames) = @_;
1.169     raeburn  2044:     my $output = qq|
1.142     albertel 2045: <script type="text/javascript">
1.218     bisitz   2046: // <![CDATA[
1.112     raeburn  2047:     function coursePick (formname) {
                   2048:         for  (var i=0; i<formname.coursepick.length; i++) {
1.114     raeburn  2049:             if (formname.coursepick[i].value == 'category') {
                   2050:                 courseSet('');
                   2051:             }
1.112     raeburn  2052:             if (!formname.coursepick[i].checked) {
                   2053:                 if (formname.coursepick[i].value == 'specific') {
                   2054:                     formname.coursetotal.value = 0;
                   2055:                     formname.courselist = '';
                   2056:                 }
                   2057:             }
                   2058:         }
                   2059:     }
1.114     raeburn  2060:     function setPick (formname) {
                   2061:         for  (var i=0; i<formname.coursepick.length; i++) {
                   2062:             if (formname.coursepick[i].value == 'category') {
                   2063:                 formname.coursepick[i].checked = true;
                   2064:             }
                   2065:             formname.coursetotal.value = 0;
                   2066:             formname.courselist = '';
                   2067:         }
                   2068:     }
1.218     bisitz   2069: // ]]>
1.112     raeburn  2070: </script>
                   2071:     |;
1.237     raeburn  2072: 
                   2073:     my ($allcrs,$pickspec);
                   2074:     if ($crstype eq 'Community') {
                   2075:         $allcrs = &mt('All communities');
                   2076:         $pickspec = &mt('Pick specific communities:');
                   2077:     } else {
                   2078:         $allcrs = &mt('All courses');
                   2079:         $pickspec = &mt('Pick specific course(s):');
                   2080:     }
                   2081: 
1.112     raeburn  2082:     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
1.237     raeburn  2083:                      ($formname,'pickcourse','pickdomain','coursedesc','',1,$crstype).'</b>';
                   2084:         $output .= '<input type="radio" name="coursepick" value="all" onclick="coursePick(this.form)" />'.$allcrs.'<br />';
1.112     raeburn  2085:     if ($totcodes > 0) {
                   2086:         my $numtitles = @$codetitles;
                   2087:         if ($numtitles > 0) {
1.129     raeburn  2088:             $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  2089:             $output .= '<table><tr><td>'.$$codetitles[0].'<br />'."\n".
1.280     raeburn  2090:                '<select name="'.$standardnames->[0].
1.114     raeburn  2091:                '" onChange="setPick(this.form);courseSet('."'$$codetitles[0]'".')">'."\n".
1.112     raeburn  2092:                ' <option value="-1" />Select'."\n";
                   2093:             my @items = ();
                   2094:             my @longitems = ();
                   2095:             if ($$idlist{$$codetitles[0]} =~ /","/) {
1.113     raeburn  2096:                 @items = split(/","/,$$idlist{$$codetitles[0]});
1.112     raeburn  2097:             } else {
                   2098:                 $items[0] = $$idlist{$$codetitles[0]};
                   2099:             }
                   2100:             if (defined($$idlist_titles{$$codetitles[0]})) {
                   2101:                 if ($$idlist_titles{$$codetitles[0]} =~ /","/) {
1.113     raeburn  2102:                     @longitems = split(/","/,$$idlist_titles{$$codetitles[0]});
1.112     raeburn  2103:                 } else {
                   2104:                     $longitems[0] = $$idlist_titles{$$codetitles[0]};
                   2105:                 }
                   2106:                 for (my $i=0; $i<@longitems; $i++) {
                   2107:                     if ($longitems[$i] eq '') {
                   2108:                         $longitems[$i] = $items[$i];
                   2109:                     }
                   2110:                 }
                   2111:             } else {
                   2112:                 @longitems = @items;
                   2113:             }
                   2114:             for (my $i=0; $i<@items; $i++) {
                   2115:                 $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
                   2116:             }
                   2117:             $output .= '</select></td>';
                   2118:             for (my $i=1; $i<$numtitles; $i++) {
                   2119:                 $output .= '<td>'.$$codetitles[$i].'<br />'."\n".
1.280     raeburn  2120:                           '<select name="'.$standardnames->[$i].
1.112     raeburn  2121:                           '" onChange="courseSet('."'$$codetitles[$i]'".')">'."\n".
                   2122:                           '<option value="-1">&lt;-Pick '.$$codetitles[$i-1].'</option>'."\n".
                   2123:                           '</select>'."\n".
                   2124:                           '</td>';
                   2125:             }
                   2126:             $output .= '</tr></table><br />';
                   2127:         }
                   2128:     }
1.238     raeburn  2129:     $output .= '<input type="radio" name="coursepick" value="specific" onclick="coursePick(this.form);opencrsbrowser('."'".$formname."','dccourse','dcdomain','coursedesc','','1','$crstype'".')" />'.$pickspec.' '.$courseform.'&nbsp;&nbsp;<input type="text" value="0" size="4" name="coursetotal" /><input type="hidden" name="courselist" value="" />selected.<br />'."\n";
1.112     raeburn  2130:     return $output;
                   2131: }
                   2132: 
                   2133: sub status_select_row {
1.142     albertel 2134:     my ($types,$title,$css_class) = @_;
1.117     raeburn  2135:     my $output; 
                   2136:     if (defined($title)) {
1.142     albertel 2137:         $output = &row_title($title,$css_class,'LC_pick_box_select');
1.117     raeburn  2138:     }
1.142     albertel 2139:     $output .= qq|
1.198     bisitz   2140:                                     <select name="types" multiple="multiple">\n|;
1.113     raeburn  2141:     foreach my $status_type (sort(keys(%{$types}))) {
1.112     raeburn  2142:         $output .= '  <option value="'.$status_type.'">'.$$types{$status_type}.'</option>';
                   2143:     }
1.142     albertel 2144:     $output .= qq|                   </select>\n|; 
1.117     raeburn  2145:     if (defined($title)) {
                   2146:         $output .= &row_closure();
                   2147:     }
1.112     raeburn  2148:     return $output;
                   2149: }
                   2150: 
                   2151: sub email_default_row {
1.142     albertel 2152:     my ($authtypes,$title,$descrip,$css_class) = @_;
                   2153:     my $output = &row_title($title,$css_class);
                   2154:     $output .= $descrip.
                   2155: 	&Apache::loncommon::start_data_table().
                   2156: 	&Apache::loncommon::start_data_table_header_row().
                   2157: 	'<th>'.&mt('Authentication Method').'</th>'.
                   2158: 	'<th align="right">'.&mt('Username -> e-mail conversion').'</th>'."\n".
                   2159: 	&Apache::loncommon::end_data_table_header_row();
1.112     raeburn  2160:     my $rownum = 0;
1.113     raeburn  2161:     foreach my $auth (sort(keys(%{$authtypes}))) {
1.112     raeburn  2162:         my ($userentry,$size);
                   2163:         if ($auth =~ /^krb/) {
                   2164:             $userentry = '';
                   2165:             $size = 25;
                   2166:         } else {
                   2167:             $userentry = 'username@';
                   2168:             $size = 15;
                   2169:         }
1.142     albertel 2170:         $output .= &Apache::loncommon::start_data_table_row().
                   2171: 	    '<td>  '.$$authtypes{$auth}.'</td>'.
                   2172: 	    '<td align="right">'.$userentry.
                   2173: 	    '<input type="text" name="'.$auth.'" size="'.$size.'" /></td>'.
                   2174: 	    &Apache::loncommon::end_data_table_row();
1.112     raeburn  2175:     }
1.142     albertel 2176:     $output .= &Apache::loncommon::end_data_table();
1.112     raeburn  2177:     $output .= &row_closure();
                   2178:     return $output;
                   2179: }
                   2180: 
                   2181: 
                   2182: sub submit_row {
1.142     albertel 2183:     my ($title,$cmd,$submit_text,$css_class) = @_;
                   2184:     my $output = &row_title($title,$css_class,'LC_pick_box_submit');
1.112     raeburn  2185:     $output .= qq|
                   2186:              <br />
                   2187:              <input type="hidden" name="command" value="$cmd" />
                   2188:              <input type="submit" value="$submit_text"/> &nbsp;
                   2189:              <br /><br />
1.142     albertel 2190:             \n|;
1.112     raeburn  2191:     return $output;
                   2192: }
1.1       stredwic 2193: 
1.147     raeburn  2194: sub course_custom_roles {
                   2195:     my ($cdom,$cnum) = @_;
                   2196:     my %returnhash=();
                   2197:     my %coursepersonnel=&Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   2198:     foreach my $person (sort(keys(%coursepersonnel))) {
                   2199:         my ($role) = ($person =~ /^([^:]+):/);
                   2200:         my ($end,$start) = split(/:/,$coursepersonnel{$person});
                   2201:         if ($end == -1 && $start == -1) {
                   2202:             next;
                   2203:         }
                   2204:         if ($role =~ m|^cr/[^/]+/[^/]+/[^/]|) {
                   2205:             $returnhash{$role} ++;
                   2206:         }
                   2207:     }
                   2208:     return %returnhash;
                   2209: }
                   2210: 
                   2211: 
1.270     www      2212: sub resource_info_box {
1.300     raeburn  2213:    my ($symb,$onlyfolderflag,$stuvcurrent,$stuvdisp)=@_;
1.270     www      2214:    my $return='';
1.300     raeburn  2215:    if ($stuvcurrent ne '') {
                   2216:        $return = '<div class="LC_left_float">';
                   2217:    }
1.270     www      2218:    if ($symb) {
1.300     raeburn  2219:        $return.=&Apache::loncommon::start_data_table();
1.271     www      2220:        my ($map,$id,$resource)=&Apache::lonnet::decode_symb($symb);
                   2221:        my $folder=&Apache::lonnet::gettitle($map);
                   2222:        $return.=&Apache::loncommon::start_data_table_row().
1.300     raeburn  2223:                     '<th align="left">'.&mt('Folder:').'</th><td>'.$folder.'</td>'.
1.271     www      2224:                     &Apache::loncommon::end_data_table_row();
1.270     www      2225:        unless ($onlyfolderflag) {
                   2226:           $return.=&Apache::loncommon::start_data_table_row().
1.300     raeburn  2227:                     '<th align="left">'.&mt('Resource:').'</th><td>'.&Apache::lonnet::gettitle($symb).'</td>'.
                   2228:                     &Apache::loncommon::end_data_table_row();
                   2229:        }
                   2230:        if ($stuvcurrent ne '') {
                   2231:            $return .= &Apache::loncommon::start_data_table_row().
                   2232:                     '<th align="left">'.&mt("Student's current version:").'</th><td>'.$stuvcurrent.'</td>'.
                   2233:                     &Apache::loncommon::end_data_table_row();
                   2234:        }
                   2235:        if ($stuvdisp ne '') {
                   2236:            $return .= &Apache::loncommon::start_data_table_row().
                   2237:                     '<th align="left">'.&mt("Student's version displayed:").'</th><td>'.$stuvdisp.'</td>'.
1.270     www      2238:                     &Apache::loncommon::end_data_table_row();
                   2239:        }
1.271     www      2240:        $return.=&Apache::loncommon::end_data_table();
1.270     www      2241:     } else {
                   2242:        $return='<p><span class="LC_error">'.&mt('No context provided.').'</span></p>';
                   2243:     }
1.300     raeburn  2244:     if ($stuvcurrent ne '') {
                   2245:         $return .= '</div>';
                   2246:     }
1.270     www      2247:     return $return;
                   2248: }
                   2249: 
1.119     raeburn  2250: ##############################################
                   2251: ##############################################
1.179     raeburn  2252: 
                   2253: # topic_bar
                   2254: #
1.248     wenzelju 2255: # Generates a div containing an (optional) number with a white background followed by a 
1.240     raeburn  2256: # title with a background color defined in the corresponding CSS: LC_topic_bar
                   2257: # Inputs:
1.248     wenzelju 2258: # 1. number to display.
                   2259: #    If input for number is empty only the title will be displayed. 
1.240     raeburn  2260: # 2. title text to display.
1.313     raeburn  2261: # 3. optional id for the <div>
1.240     raeburn  2262: # Outputs - a scalar containing html mark-up for the div.
                   2263: 
1.179     raeburn  2264: sub topic_bar {
1.313     raeburn  2265:     my ($num,$title,$id) = @_;
1.248     wenzelju 2266:     my $number = '';
                   2267:     if ($num ne '') {
                   2268:         $number = '<span>'.$num.'</span>';
1.239     amueller 2269:     }
1.313     raeburn  2270:     if ($id ne '') {
                   2271:         $id = 'id="'.$id.'"';
                   2272:     }
                   2273:     return '<div class="LC_topic_bar" '.$id.'>'.$number.$title.'</div>';
1.179     raeburn  2274: }
                   2275: 
                   2276: ##############################################
                   2277: ##############################################
1.119     raeburn  2278: # echo_form_input
                   2279: #
                   2280: # Generates html markup to add form elements from the referrer page
                   2281: # as hidden form elements (values encoded) in the new page.
                   2282: #
                   2283: # Intended to support two types of use 
                   2284: # (a) to allow backing up to earlier pages in a multi-page 
                   2285: # form submission process using a breadcrumb trail.
                   2286: #
                   2287: # (b) to allow the current page to be reloaded with form elements
                   2288: # set on previous page to remain unchanged.  An example would
                   2289: # be where the a page containing a dynamically-built table of data is 
                   2290: # is to be redisplayed, with only the sort order of the data changed. 
                   2291: #  
                   2292: # Inputs:
                   2293: # 1. Reference to array of form elements in the submitted form on 
                   2294: # the referrer page which are to be excluded from the echoed elements.
                   2295: #
                   2296: # 2. Reference to array of regular expressions, which if matched in the  
                   2297: # name of the form element n the referrer page will be omitted from echo. 
                   2298: #
                   2299: # Outputs: A scalar containing the html markup for the echoed form
                   2300: # elements (all as hidden elements, with values encoded). 
                   2301: 
                   2302: 
                   2303: sub echo_form_input {
                   2304:     my ($excluded,$regexps) = @_;
                   2305:     my $output = '';
                   2306:     foreach my $key (keys(%env)) {
                   2307:         if ($key =~ /^form\.(.+)$/) {
                   2308:             my $name = $1;
                   2309:             my $match = 0;
1.285     raeburn  2310:             if (ref($excluded) eq 'ARRAY') {    
                   2311:                 next if (grep(/^\Q$name\E$/,@{$excluded}));
                   2312:             }
                   2313:             if (ref($regexps) eq 'ARRAY') {
                   2314:                 if (@{$regexps} > 0) {
                   2315:                     foreach my $regexp (@{$regexps}) {
                   2316:                         if ($name =~ /$regexp/) {
                   2317:                             $match = 1;
                   2318:                             last;
1.119     raeburn  2319:                         }
                   2320:                     }
                   2321:                 }
1.285     raeburn  2322:             }
                   2323:             next if ($match);
                   2324:             if (ref($env{$key}) eq 'ARRAY') {
                   2325:                 foreach my $value (@{$env{$key}}) {
                   2326:                     $value = &HTML::Entities::encode($value,'<>&"');
                   2327:                     $output .= '<input type="hidden" name="'.$name.
                   2328:                                '" value="'.$value.'" />'."\n";
1.119     raeburn  2329:                 }
1.285     raeburn  2330:             } else {
                   2331:                 my $value = &HTML::Entities::encode($env{$key},'<>&"');
                   2332:                 $output .= '<input type="hidden" name="'.$name.
                   2333:                            '" value="'.$value.'" />'."\n";
1.119     raeburn  2334:             }
                   2335:         }
                   2336:     }
                   2337:     return $output;
                   2338: }
                   2339: 
                   2340: ##############################################
                   2341: ##############################################
                   2342: # set_form_elements
                   2343: #
                   2344: # Generates javascript to set form elements to values based on
                   2345: # corresponding values for the same form elements when the page was
                   2346: # previously submitted.
                   2347: #     
                   2348: # Last submission values are read from hidden form elements in referring 
                   2349: # page which have the same name, i.e., generated by &echo_form_input(). 
                   2350: #
                   2351: # Intended to be called by onload event.
                   2352: #
1.121     raeburn  2353: # Inputs:
                   2354: # (a) Reference to hash of echoed form elements to be set.
1.119     raeburn  2355: #
                   2356: # In the hash, keys are the form element names, and the values are the
                   2357: # element type (selectbox, radio, checkbox or text -for textbox, textarea or
                   2358: # hidden).
1.121     raeburn  2359: #
                   2360: # (b) Optional reference to hash of stored elements to be set.
                   2361: #
                   2362: # If the page being displayed is a page which permits modification of
                   2363: # previously stored data, e.g., the first page in a multi-page submission,
                   2364: # then if stored is supplied, form elements will be set to the last stored
                   2365: # values.  If user supplied values are also available for the same elements
                   2366: # these will replace the stored values. 
                   2367: #        
1.119     raeburn  2368: # Output:
                   2369: #  
                   2370: # javascript function - set_form_elements() which sets form elements,
                   2371: # expects an argument: formname - the name of the form according to 
                   2372: # the DOM, e.g., document.compose
                   2373: 
                   2374: sub set_form_elements {
1.121     raeburn  2375:     my ($elements,$stored) = @_;
                   2376:     my %values;
1.119     raeburn  2377:     my $output .= 'function setFormElements(courseForm) {
1.121     raeburn  2378: ';
                   2379:     if (defined($stored)) {
                   2380:         foreach my $name (keys(%{$stored})) {
                   2381:             if (exists($$elements{$name})) {
                   2382:                 if (ref($$stored{$name}) eq 'ARRAY') {
                   2383:                     $values{$name} = $$stored{$name};
                   2384:                 } else {
                   2385:                     @{$values{$name}} = ($$stored{$name});
                   2386:                 }
                   2387:             }
                   2388:         }
                   2389:     }
                   2390: 
1.119     raeburn  2391:     foreach my $key (keys(%env)) {
                   2392:         if ($key =~ /^form\.(.+)$/) {
                   2393:             my $name = $1;
                   2394:             if (exists($$elements{$name})) {
1.121     raeburn  2395:                 @{$values{$name}} = &Apache::loncommon::get_env_multiple($key);
                   2396:             }
                   2397:         }
                   2398:     }
                   2399: 
                   2400:     foreach my $name (keys(%values)) {
                   2401:         for (my $i=0; $i<@{$values{$name}}; $i++) {
                   2402:             $values{$name}[$i] = &HTML::Entities::decode($values{$name}[$i],'<>&"');
                   2403:             $values{$name}[$i] =~ s/([\r\n\f]+)/\\n/g;
                   2404:             $values{$name}[$i] =~ s/"/\\"/g;
                   2405:         }
1.234     raeburn  2406:         if (($$elements{$name} eq 'text') || ($$elements{$name} eq 'hidden')) {
1.121     raeburn  2407:             my $numvalues = @{$values{$name}};
                   2408:             if ($numvalues > 1) {
                   2409:                 my $valuestring = join('","',@{$values{$name}});
                   2410:                 $output .= qq|
1.119     raeburn  2411:   var textvalues = new Array ("$valuestring");
1.147     raeburn  2412:   var total = courseForm.elements['$name'].length;
1.119     raeburn  2413:   if (total > $numvalues) {
                   2414:       total = $numvalues;
                   2415:   }    
                   2416:   for (var i=0; i<total; i++) {
1.147     raeburn  2417:       courseForm.elements['$name']\[i].value = textvalues[i];
1.119     raeburn  2418:   }
                   2419: |;
1.121     raeburn  2420:             } else {
                   2421:                 $output .= qq|
1.147     raeburn  2422:   courseForm.elements['$name'].value = "$values{$name}[0]";
1.119     raeburn  2423: |;
1.121     raeburn  2424:             }
                   2425:         } else {
                   2426:             $output .=  qq|
1.147     raeburn  2427:   var elementLength = courseForm.elements['$name'].length;
1.119     raeburn  2428:   if (elementLength==undefined) {
                   2429: |;
1.121     raeburn  2430:             foreach my $value (@{$values{$name}}) {
                   2431:                 if ($$elements{$name} eq 'selectbox') {
                   2432:                     $output .=  qq|
1.147     raeburn  2433:       if (courseForm.elements['$name'].options[0].value == "$value") {
                   2434:           courseForm.elements['$name'].options[0].selected = true;
1.119     raeburn  2435:       }|;
1.121     raeburn  2436:                 } elsif (($$elements{$name} eq 'radio') ||
                   2437:                          ($$elements{$name} eq 'checkbox')) {
                   2438:                     $output .= qq|
1.147     raeburn  2439:       if (courseForm.elements['$name'].value == "$value") {
1.148     albertel 2440:           courseForm.elements['$name'].checked = true;
1.234     raeburn  2441:       } else {
                   2442:           courseForm.elements['$name'].checked = false;
1.119     raeburn  2443:       }|;
1.121     raeburn  2444:                 }
                   2445:             }
                   2446:             $output .= qq|
1.119     raeburn  2447:   }
                   2448:   else {
1.147     raeburn  2449:       for (var i=0; i<courseForm.elements['$name'].length; i++) {
1.119     raeburn  2450: |;
1.121     raeburn  2451:             if ($$elements{$name} eq 'selectbox') {
                   2452:                 $output .=  qq|
1.147     raeburn  2453:           courseForm.elements['$name'].options[i].selected = false;|;
1.121     raeburn  2454:             } elsif (($$elements{$name} eq 'radio') || 
                   2455:                      ($$elements{$name} eq 'checkbox')) {
                   2456:                 $output .= qq|
1.147     raeburn  2457:           courseForm.elements['$name']\[i].checked = false;|; 
1.121     raeburn  2458:             }
                   2459:             $output .= qq|
1.119     raeburn  2460:       }
1.147     raeburn  2461:       for (var j=0; j<courseForm.elements['$name'].length; j++) {
1.119     raeburn  2462: |;
1.121     raeburn  2463:             foreach my $value (@{$values{$name}}) {
                   2464:                 if ($$elements{$name} eq 'selectbox') {
                   2465:                     $output .=  qq|
1.147     raeburn  2466:           if (courseForm.elements['$name'].options[j].value == "$value") {
                   2467:               courseForm.elements['$name'].options[j].selected = true;
1.119     raeburn  2468:           }|;
1.121     raeburn  2469:                 } elsif (($$elements{$name} eq 'radio') ||
                   2470:                          ($$elements{$name} eq 'checkbox')) { 
                   2471:                       $output .= qq|
1.147     raeburn  2472:           if (courseForm.elements['$name']\[j].value == "$value") {
                   2473:               courseForm.elements['$name']\[j].checked = true;
1.119     raeburn  2474:           }|;
1.121     raeburn  2475:                 }
                   2476:             }
                   2477:             $output .= qq|
1.119     raeburn  2478:       }
                   2479:   }
                   2480: |;
                   2481:         }
                   2482:     }
                   2483:     $output .= "
1.235     raeburn  2484:     return;
1.119     raeburn  2485: }\n";
                   2486:     return $output;
                   2487: }
                   2488: 
1.158     raeburn  2489: ##############################################
                   2490: ##############################################
                   2491: 
1.291     raeburn  2492: sub file_submissionchk_js {
                   2493:     my ($turninpaths,$multiples) = @_;
                   2494:     my $overwritewarn = &mt('File(s) you uploaded for your submission will overwrite existing file(s) submitted for this item').'\\n'.
                   2495:                       &mt('Continue submission and overwrite the file(s)?');
                   2496:     my $delfilewarn = &mt('You have indicated you wish to remove some files previously included in your submission.').'\\n'.
                   2497:                       &mt('Continue submission with these files removed?');
1.292     raeburn  2498:     my ($turninpathtext,$multtext,$arrayindexofjs);
1.291     raeburn  2499:     if (ref($turninpaths) eq 'HASH') {
                   2500:         foreach my $key (sort(keys(%{$turninpaths}))) {
                   2501:             $turninpathtext .= "    if (prefix == '$key') {\n".
                   2502:                                "        return '$turninpaths->{$key}';\n".
                   2503:                                "    }\n";
                   2504:         }
                   2505:     }
                   2506:     $turninpathtext .= "    return '';\n";
                   2507:     if (ref($multiples) eq 'HASH') {
                   2508:         foreach my $key (sort(keys(%{$multiples}))) {
                   2509:             $multtext .= "    if (prefix == '$key') {\n".
                   2510:                          "        return '$multiples->{$key}';\n".
                   2511:                          "    }\n";
                   2512:         }
                   2513:     }
                   2514:     $multtext .= "    return '';\n";
1.292     raeburn  2515: 
1.293     raeburn  2516:     $arrayindexofjs = &Apache::loncommon::javascript_array_indexof();
1.291     raeburn  2517:     return <<"ENDSCRIPT";
                   2518: <script type="text/javascript">
                   2519: // <![CDATA[
                   2520: 
                   2521: function file_submission_check(formname,path,multiresp) {
                   2522:     var elemnum = formname.elements.length;
                   2523:     if (elemnum == 0) {
                   2524:         return true;
                   2525:     }
                   2526:     var alloverwrites = [];
                   2527:     var alldelconfirm = [];
                   2528:     var result = [];
                   2529:     var submitter;
                   2530:     var subprefix;
                   2531:     var allsub = getIndexByName(formname,'all_submit');
                   2532:     if (allsub == -1) {
                   2533:         var idx = getIndexByName(formname,'submitted');
                   2534:         if (idx != -1) {
                   2535:             var subval = String(formname.elements[idx].value);
                   2536:             submitter = subval.replace(/^part_/,'');
                   2537:             result = overwritten_check(formname,path,multiresp,submitter);
                   2538:             alloverwrites.push.apply(alloverwrites,result['overwrite']);
                   2539:             alldelconfirm.push.apply(alldelconfirm,result['delete']);
                   2540:         }
                   2541:     } else {
                   2542:         if (formname.elements[allsub].type == 'submit') {
                   2543:             var partsub = /^\\d+\\.\\d+_submit_.+\$/;
                   2544:             var allprefixes = [];
                   2545:             var allparts = [];
                   2546:             for (var i=0; i<formname.elements.length; i++) {
                   2547:                 if (formname.elements[i].type == 'submit') {
                   2548:                     var elemname = formname.elements[i].name;
                   2549:                     var subname = String(elemname);
                   2550:                     var savesub = String(elemname);
                   2551:                     if (partsub.test(subname)) {
                   2552:                         var prefix = subname.replace(/_submit_.+\$/,'');
                   2553:                         if (allprefixes.indexOf(prefix) == -1) {
                   2554:                             allprefixes.push(prefix);
                   2555:                             allparts[prefix] = [];
                   2556:                         }
                   2557:                         var part = savesub.replace(/^\\d+\\.\\d+_submit_/,'');
                   2558:                         allparts[prefix].push(part);
                   2559:                     }
                   2560:                 }
                   2561:             }
                   2562:             for (var k=0; k<allprefixes.length; k++) {
                   2563:                 var idx = getIndexByName(formname,allprefixes[k]+'_submitted');
                   2564:                 if (idx > -1) {
                   2565:                     if (formname.elements[idx].value != 'yes') {
                   2566:                         submitterval = formname.elements[idx].value;
                   2567:                         submitter = submitterval.replace(/^part_/,'');
                   2568:                         subprefix = allprefixes[k];
                   2569:                         result = overwritten_check(formname,path,multiresp,submitter,subprefix);
                   2570:                         alloverwrites.push.apply(alloverwrites,result['overwrite']);
                   2571:                         alldelconfirm.push.apply(alldelconfirm,result['delete']);
                   2572:                         break;
                   2573:                     }
                   2574:                 }
                   2575:             }
                   2576:             if (submitter == '' || submitter == undefined) {
                   2577:                 for (var m=0; m<allprefixes.length; m++) {
                   2578:                     for (var n=0; n<allparts[allprefixes[m]].length; n++) {
                   2579:                         var result = overwritten_check(formname,path,multiresp,allparts[allprefixes[m]][n],allprefixes[m]);
                   2580:                         alloverwrites.push.apply(alloverwrites,result['overwrite']);
                   2581:                         alldelconfirm.push.apply(alldelconfirm,result['delete']);
                   2582:                     }
                   2583:                 }
                   2584:             }
                   2585:         }
                   2586:     }
                   2587:     if (alloverwrites.length > 0) {
                   2588:         if (!confirm("$overwritewarn")) {
                   2589:             for (var n=0; n<alloverwrites.length; n++) {
                   2590:                 formname.elements[alloverwrites[n]].value = "";
                   2591:             }
                   2592:             return false;
                   2593:         }
                   2594:     }
                   2595:     if (alldelconfirm.length > 0) {
                   2596:         if (!confirm("$delfilewarn")) {
                   2597:             for (var p=0; p<alldelconfirm.length; p++) {
                   2598:                 formname.elements[alldelconfirm[p]].checked = false;
                   2599:             }
                   2600:             return false;
                   2601:         }
                   2602:     }
                   2603:     return true;
                   2604: }
                   2605: 
                   2606: function getIndexByName(formname,item) {
                   2607:     for (var i=0;i<formname.elements.length;i++) {
                   2608:         if (formname.elements[i].name == item) {
                   2609:             return i;
                   2610:         }
                   2611:     }
                   2612:     return -1;
                   2613: }
                   2614: 
                   2615: function overwritten_check(formname,path,multiresp,part,prefix) {
                   2616:     var result = [];
                   2617:     result['overwrite'] = [];
                   2618:     result['delete'] = [];
                   2619:     var elemnum = formname.elements.length;
                   2620:     if (elemnum == 0) {
                   2621:         return result;
                   2622:     }
                   2623:     var uploadstr;
                   2624:     var deletestr;
                   2625:     if ((prefix != undefined) && (prefix != '')) {
                   2626:         var prepend = prefix+'_';
                   2627:         uploadstr = new RegExp("^"+prepend+"HWFILE"+part+".+\$");
                   2628:         deletestr = new RegExp("^"+prepend+"HWFILE"+part+".+_\\\\d+_delete\$");
                   2629:         multiresp = check_for_multiples(prepend);
                   2630:         path = check_for_turninpath(prepend);
                   2631:     } else {
                   2632:         uploadstr = new RegExp("^HWFILE"+part+".+\$");
                   2633:         deletestr = new RegExp("^HWFILE"+part+".+_\\\\d+_delete\$");
                   2634:     }
                   2635:     var alluploads = [];
                   2636:     var allchecked = [];
                   2637:     var allskipdel = [];
                   2638:     var fnametrim = /[^\\/\\\\]+\$/;
                   2639:     for (var i=0; i<formname.elements.length; i++) {
                   2640:         var id = formname.elements[i].id;
                   2641:         if (id != '') {
                   2642:             if (uploadstr.test(id)) {
                   2643:                 if (formname.elements[i].type == 'file') {
                   2644:                     alluploads.push(id);
                   2645:                 } else {
                   2646:                     if (deletestr.test(id)) {
                   2647:                         if (formname.elements[i].type == 'checkbox') {
                   2648:                             if (formname.elements[i].checked) {
                   2649:                                 allchecked.push(id);
                   2650:                             }
                   2651:                         }
                   2652:                     }
                   2653:                 }
                   2654:             }
                   2655:         }
                   2656:     }
                   2657:     for (var j=0; j<alluploads.length; j++) {
                   2658:         var delstr = new RegExp("^"+alluploads[j]+"_\\\\d+_delete\$");
                   2659:         var delboxes = [];
                   2660:         for (var k=0; k<formname.elements.length; k++) {
                   2661:             var id = formname.elements[k].id;
                   2662:             if ((id != '') && (id != undefined)) {
                   2663:                 if (delstr.test(id)) {
                   2664:                     if (formname.elements[k].type == 'checkbox') {
                   2665:                         delboxes.push(id);
                   2666:                     }
                   2667:                 }
                   2668:             }
                   2669:         }
                   2670:         if (delboxes.length > 0) {
                   2671:             if ((formname.elements[alluploads[j]].value != undefined) &&
                   2672:                 (formname.elements[alluploads[j]].value != '')) {
                   2673:                 var filepath = formname.elements[alluploads[j]].value;
                   2674:                 var newfilename = fnametrim.exec(filepath);
                   2675:                 if (newfilename != null) {
                   2676:                     var filename = String(newfilename);
                   2677:                     var nospaces = filename.replace(/\\s+/g,'_');
                   2678:                     var nospecials = nospaces.replace(/[^\\/\\w\\.\\-]/g,'');
                   2679:                     var cleanfilename = nospecials.replace(/\\.(\\d+\\.)/g,"_\$1");
                   2680:                     if (cleanfilename != '') {
                   2681:                         var fullpath = path+"/"+cleanfilename;
                   2682:                         if (multiresp == 1) {
                   2683:                             var partid = String(alluploads[i]);
                   2684:                             var subdir = partid.replace(/^\\d*.?\\d*_?HWFILE/,'');
                   2685:                             if (subdir != "" && subdir != undefined) {
                   2686:                                 fullpath = path+"/"+subdir+"/"+cleanfilename;
                   2687:                             }
                   2688:                         }
                   2689:                         for (var m=0; m<delboxes.length; m++) {
                   2690:                             if (fullpath == formname.elements[delboxes[m]].value) {
                   2691:                                 if (formname.elements[delboxes[m]].checked) {
                   2692:                                     allskipdel.push(delboxes[m]);
                   2693:                                 } else {
                   2694:                                     result['overwrite'].push(alluploads[j]);
                   2695:                                 }
                   2696:                                 break;
                   2697:                             }
                   2698:                         }
                   2699:                     }
                   2700:                 }
                   2701:             }
                   2702:         }
                   2703:     }
                   2704:     if (allchecked.length > 0) {
                   2705:         if (allskipdel.length > 0) {
                   2706:             for (var n=0; n<allchecked.length; n++) {
                   2707:                 if (allskipdel.indexOf(allchecked[n]) == -1) {
                   2708:                     result['delete'].push(allchecked[n]);
                   2709:                 }
                   2710:             }
                   2711:         } else {
                   2712:             result['delete'].push.apply(result['delete'],allchecked);
                   2713:         }
                   2714:     }
                   2715:     return result;
                   2716: }
                   2717: 
                   2718: function check_for_multiples(prefix) {
                   2719: $multtext
                   2720: }
                   2721: 
                   2722: function check_for_turninpath(prefix) {
                   2723: $turninpathtext
                   2724: }
                   2725: 
                   2726: // ]]>
                   2727: </script>
                   2728: 
1.292     raeburn  2729: $arrayindexofjs
                   2730: 
1.291     raeburn  2731: ENDSCRIPT
                   2732: }
                   2733: 
                   2734: ##############################################
                   2735: ##############################################
                   2736: 
1.313     raeburn  2737: sub resize_scrollbox_js {
                   2738:     my ($context,$tabidstr) = @_;
                   2739:     my (%names,$paddingwfrac,$offsetwfrac,$offsetv,$minw,$minv);
                   2740:     if ($context eq 'docs') {
                   2741:         %names = (
                   2742:                    boxw   => 'contenteditor',
                   2743:                    item   => 'contentlist',
                   2744:                    header => 'uploadfileresult',
                   2745:                    scroll => 'contentscroll',
                   2746:                    boxh   => 'contenteditor',
                   2747:                  );
                   2748:         $paddingwfrac = 0.09; 
                   2749:         $offsetwfrac = 0.015;
                   2750:         $offsetv = 20;
                   2751:         $minw = 250;
                   2752:         $minv = 200;
                   2753:     } elsif ($context eq 'params') {
                   2754:         %names = (
                   2755:                    boxw   => 'parameditor',
                   2756:                    item   => 'mapmenuinner',
                   2757:                    header => 'parmstep1',
                   2758:                    scroll => 'mapmenuscroll',
                   2759:                    boxh   => 'parmlevel',
                   2760:                  );
                   2761:         $paddingwfrac = 0.2;
                   2762:         $offsetwfrac = 0.015;
                   2763:         $offsetv = 80;
                   2764:         $minw = 100;
                   2765:         $minv = 100; 
                   2766:     }
                   2767:     my $viewport_js = &Apache::loncommon::viewport_geometry_js();
                   2768:     my $output = '
                   2769: 
                   2770: window.onresize=callResize;
                   2771: 
                   2772: ';
                   2773:     if ($context eq 'docs') {
                   2774:         $output .= '
                   2775: var activeTab;
                   2776: ';
                   2777:     }
                   2778:     $output .=  <<"FIRST";
                   2779: 
                   2780: $viewport_js
                   2781: 
                   2782: function resize_scrollbox(scrollboxname,chkw,chkh) {
                   2783:     var scrollboxid = 'div_'+scrollboxname;
                   2784:     var scrolltableid = 'table_'+scrollboxname;
                   2785:     var scrollbox;
                   2786:     var scrolltable;
                   2787: 
                   2788:     if (document.getElementById("$names{'boxw'}") == null) {
                   2789:         return;
                   2790:     }
                   2791: 
                   2792:     if (document.getElementById(scrollboxid) == null) {
                   2793:         return;
                   2794:     } else {
                   2795:         scrollbox = document.getElementById(scrollboxid);
                   2796:     }
                   2797: 
                   2798: 
                   2799:     if (document.getElementById(scrolltableid) == null) {
                   2800:         return;
                   2801:     } else {
                   2802:         scrolltable = document.getElementById(scrolltableid);
                   2803:     }
                   2804: 
                   2805:     init_geometry();
                   2806:     var vph = Geometry.getViewportHeight();
                   2807:     var vpw = Geometry.getViewportWidth();
                   2808: 
                   2809: FIRST
                   2810:     if ($context eq 'docs') {
                   2811:         $output .= "
                   2812:     var alltabs = ['$tabidstr'];
                   2813: ";
                   2814:     } elsif ($context eq 'params') {
                   2815:         $output .= "
                   2816:     if (document.getElementById('$names{'boxh'}') == null) {
                   2817:         return;
                   2818:     }
                   2819: ";
                   2820:     }
                   2821:     $output .= <<"SECOND";
                   2822:     var listwchange;
                   2823:     if (chkw == 1) {
                   2824:         var boxw = document.getElementById("$names{'boxw'}").offsetWidth;
                   2825:         var itemw;
                   2826:         var itemid = document.getElementById("$names{'item'}");
                   2827:         if (itemid != null) {
                   2828:             itemw = itemid.offsetWidth;
                   2829:         }
                   2830:         var itemwstart = itemw;
                   2831: 
                   2832:         var scrollboxw = scrollbox.offsetWidth;
                   2833:         var scrollboxscrollw = scrollbox.scrollWidth;
                   2834: 
                   2835:         var offsetw = parseInt(vpw * $offsetwfrac);
                   2836:         var paddingw = parseInt(vpw * $paddingwfrac);
                   2837: 
                   2838:         var minscrollboxw = $minw;
                   2839:         var maxcolw = 0;
                   2840: SECOND
                   2841:     if ($context eq 'docs') {
                   2842:         $output .= <<"DOCSONE";
                   2843:         var actabw = 0;
                   2844:         for (var i=0; i<alltabs.length; i++) {
                   2845:             if (activeTab == alltabs[i]) {
                   2846:                 actabw = document.getElementById(alltabs[i]).offsetWidth;
                   2847:                 if (actabw > maxcolw) {
                   2848:                     maxcolw = actabw;
                   2849:                 }
                   2850:             } else {
                   2851:                 if (document.getElementById(alltabs[i]) != null) {
                   2852:                     var thistab = document.getElementById(alltabs[i]);
                   2853:                     thistab.style.visibility = 'hidden';
                   2854:                     thistab.style.display = 'block';
                   2855:                     var tabw = document.getElementById(alltabs[i]).offsetWidth;
                   2856:                     thistab.style.display = 'none';
                   2857:                     thistab.style.visibility = '';
                   2858:                     if (tabw > maxcolw) {
                   2859:                         maxcolw = tabw;
                   2860:                     }
                   2861:                 }
                   2862:             }
                   2863:         }
                   2864: DOCSONE
                   2865:     } elsif ($context eq 'params') {
                   2866:         $output .= <<"PARAMSONE";
                   2867:         var parmlevelrows = new Array();
                   2868:         var mapmenucells = new Array();
                   2869:         parmlevelrows = document.getElementById("$names{'boxh'}").rows;
                   2870:         var numrows = parmlevelrows.length;
                   2871:         if (numrows > 1) {
                   2872:             mapmenucells = parmlevelrows[2].getElementsByTagName('td');
                   2873:         }
                   2874:         maxcolw = mapmenucells[0].offsetWidth;
                   2875: PARAMSONE
                   2876:     }
                   2877:     $output .= <<"THIRD";
                   2878:         if (maxcolw > 0) {
                   2879:             var newscrollboxw;
                   2880:             if (maxcolw+paddingw+scrollboxscrollw<boxw) {
                   2881:                 newscrollboxw = boxw-paddingw-maxcolw;
                   2882:                 if (newscrollboxw < minscrollboxw) {
                   2883:                     newscrollboxw = minscrollboxw;
                   2884:                 }
                   2885:                 scrollbox.style.width = newscrollboxw+"px";
                   2886:                 if (newscrollboxw != scrollboxw) {
                   2887:                     var newitemw = newscrollboxw-offsetw;
                   2888:                     itemid.style.width = newitemw+"px";
                   2889:                 }
                   2890:             } else {
                   2891:                 newscrollboxw = boxw-paddingw-maxcolw;
                   2892:                 if (newscrollboxw < minscrollboxw) {
                   2893:                     newscrollboxw = minscrollboxw;
                   2894:                 }
                   2895:                 scrollbox.style.width = newscrollboxw+"px";
                   2896:                 if (newscrollboxw != scrollboxw) {
                   2897:                     var newitemw = newscrollboxw-offsetw;
                   2898:                     itemid.style.width = newitemw+"px";
                   2899:                 }
                   2900:             }
                   2901: 
                   2902:             if (newscrollboxw != scrollboxw) {
                   2903:                 var newscrolltablew = newscrollboxw+offsetw;
                   2904:                 scrolltable.style.width = newscrolltablew+"px";
                   2905:             }
                   2906:         }
                   2907: 
                   2908:         if (itemid.offsetWidth != itemwstart) {
                   2909:             listwchange = 1;
                   2910:         }
                   2911: THIRD
                   2912:     if ($context eq 'docs') {
                   2913:         $output .= <<"DOCSTWO";
                   2914:         if (activeTab == 'cc1') {
                   2915:             if (document.getElementById('cc_hrule') != null) {
                   2916:                 document.getElementById('cc_hrule').style.width=actabw+"px";
                   2917:             }
                   2918:         } else {
                   2919:             if (activeTab == 'bb1') {
                   2920:                 if (document.getElementById('bb_hrule') != null) {
                   2921:                     document.getElementById('bb_hrule').style.width=actabw+"px";
                   2922:                 }
                   2923:             } else {
                   2924:                 if (activeTab == 'ee2') {
                   2925:                     if (document.getElementById('ee_hrule') != null) {
                   2926:                         document.getElementById('ee_hrule').style.width=actabw+"px";
                   2927:                     }
                   2928:                 }
                   2929:             }
                   2930:         }
                   2931: DOCSTWO
                   2932:     }
                   2933:     $output .= <<"FOURTH";
                   2934:     }
                   2935:     if ((chkh == 1) || (listwchange)) {
                   2936:         var primaryheight = document.getElementById('LC_nav_bar').offsetHeight;
                   2937:         var secondaryheight = document.getElementById('LC_secondary_menu').offsetHeight;
                   2938:         var crumbsheight = document.getElementById('LC_breadcrumbs').offsetHeight;
                   2939:         var dccidheight = 0;
                   2940:         if (document.getElementById('dccid') != null) {
                   2941:             dccidheight = document.getElementById('dccid').offsetHeight;
                   2942:         }
                   2943:         var headerheight = 0;
                   2944:         if (document.getElementById("$names{'header'}") != null) {
                   2945:             headerheight = document.getElementById("$names{'header'}").offsetHeight;
                   2946:         }
                   2947:         var tabbedheight = document.getElementById("tabbededitor").offsetHeight;
                   2948:         var boxheight = document.getElementById("$names{'boxh'}").offsetHeight;
                   2949:         var freevspace = vph-(primaryheight+secondaryheight+crumbsheight+dccidheight+headerheight+tabbedheight+boxheight);
                   2950: 
                   2951:         var scrollboxheight = scrollbox.offsetHeight;
                   2952:         var scrollboxscrollheight = scrollbox.scrollHeight;
                   2953: 
                   2954:         var minvscrollbox = $minv;
                   2955:         var offsetv = $offsetv;
                   2956:         var newscrollboxheight;
                   2957:         if (freevspace < 0) {
                   2958:             newscrollboxheight = scrollboxheight+freevspace-offsetv;
                   2959:             if (newscrollboxheight < minvscrollbox) {
                   2960:                 newscrollboxheight = minvscrollbox;
                   2961:             }
                   2962:             scrollbox.style.height = newscrollboxheight + "px";
                   2963:         } else {
                   2964:             if (scrollboxscrollheight > scrollboxheight) {
                   2965:                 if (freevspace > offsetv) {
                   2966:                     newscrollboxheight = scrollboxheight+freevspace-offsetv;
                   2967:                     if (newscrollboxheight < minvscrollbox) {
                   2968:                         newscrollboxheight = minvscrollbox;
                   2969:                     }
                   2970:                     scrollbox.style.height = newscrollboxheight+"px";
                   2971:                 }
                   2972:             }
                   2973:         }
                   2974:         scrollboxheight = scrollbox.offsetHeight;
                   2975:         var itemh = document.getElementById("$names{'item'}").offsetHeight;
                   2976: 
                   2977:         if (scrollboxscrollheight <= scrollboxheight) {
                   2978:             if ((itemh+offsetv)<scrollboxheight) {
                   2979:                 newscrollheight = itemh+offsetv;
                   2980:                 scrollbox.style.height = newscrollheight+"px";
                   2981:             }
                   2982:         }
                   2983:     }
                   2984:     return;
                   2985: }
                   2986: 
                   2987: function callResize() {
                   2988:     var timer;
                   2989:     clearTimeout(timer);
                   2990:     timer=setTimeout('resize_scrollbox("$names{'scroll'}","1","1")',500);
                   2991: }
                   2992: 
                   2993: FOURTH
                   2994:     return $output;
                   2995: }
                   2996: 
                   2997: 
                   2998: ##############################################
                   2999: ##############################################
                   3000: 
1.158     raeburn  3001: # javascript_valid_email
                   3002: #
                   3003: # Generates javascript to validate an e-mail address.
                   3004: # Returns a javascript function which accetps a form field as argumnent, and
                   3005: # returns false if field.value does not satisfy two regular expression matches
                   3006: # for a valid e-mail address.  Backwards compatible with old browsers without
                   3007: # support for javascript RegExp (just checks for @ in field.value in this case). 
                   3008: 
                   3009: sub javascript_valid_email {
                   3010:     my $scripttag .= <<'END';
                   3011: function validmail(field) {
                   3012:     var str = field.value;
                   3013:     if (window.RegExp) {
                   3014:         var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
                   3015:         var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"; //"
                   3016:         var reg1 = new RegExp(reg1str);
                   3017:         var reg2 = new RegExp(reg2str);
                   3018:         if (!reg1.test(str) && reg2.test(str)) {
                   3019:             return true;
                   3020:         }
                   3021:         return false;
                   3022:     }
                   3023:     else
                   3024:     {
                   3025:         if(str.indexOf("@") >= 0) {
                   3026:             return true;
                   3027:         }
                   3028:         return false;
                   3029:     }
                   3030: }
                   3031: END
                   3032:     return $scripttag;
                   3033: }
                   3034: 
1.219     droeschl 3035: 
                   3036: # USAGE: htmltag(element, content, {attribute => value,...});
                   3037: #
                   3038: # EXAMPLES: 
                   3039: #  - htmltag('a', 'this is an anchor', {href  => 'www.example.com', 
                   3040: #                                       title => 'this is a title'})
                   3041: #
                   3042: #  - You might want to set up needed tags like: 
                   3043: #
                   3044: #     my $h3  = sub { return htmltag( "h3",  @_ ) };
                   3045: #
                   3046: #    ... and use them: $h3->("This is a headline")
                   3047: #
                   3048: #  - To set up a couple of tags, see sub inittags
                   3049: #
                   3050: # NOTES:
                   3051: # - Empty elements, such as <br/> are correctly terminated, 
                   3052: #   i.e. htmltag('br') returns <br/> 
                   3053: # - Empty attributes (title="") are filtered out.
                   3054: # - The function will not check for deprecated attributes.
                   3055: #
                   3056: # OUTPUT: content enclosed in xhtml conform tags
                   3057: sub htmltag{
                   3058:     return
                   3059:         qq|<$_[0]|
                   3060:         . join( '', map { qq| $_="${$_[2]}{$_}"| if ${$_[2]}{$_} } keys %{ $_[2] } )
                   3061:         . ($_[1] ? qq|>$_[1]</$_[0]>| : qq|/>|). "\n";
                   3062: };
                   3063: 
                   3064: 
                   3065: # USAGE: inittags(@tags);
                   3066: #
                   3067: # EXAMPLES:
1.261     droeschl 3068: #  - my ($h1, $h2, $h3) = inittags( qw( h1 h2 h3 ) )
1.219     droeschl 3069: #    $h1->("This is a headline") #Returns: <h1>This is a headline</h1>
                   3070: #
                   3071: # NOTES: See sub htmltag for further information.
                   3072: #
                   3073: # OUTPUT: List of subroutines. 
                   3074: sub inittags {
                   3075:     my @tags = @_;
                   3076:     return map { my $tag = $_;
                   3077:                  sub { return htmltag( $tag, @_ ) }
                   3078:                } @tags;
                   3079: }
                   3080: 
                   3081: 
1.231     droeschl 3082: # USAGE: scripttag(scriptcode, [start|end|both]);
1.229     droeschl 3083: #
                   3084: # EXAMPLES: 
1.231     droeschl 3085: #  - scripttag("alert('Hello World!')", 'both') 
                   3086: #    returns:
                   3087: #    <script type="text/javascript">
                   3088: #    // BEGIN LON-CAPA Internal
                   3089: #    alert(Hello World!')
                   3090: #    // END LON-CAPA Internal
                   3091: #    </script>
1.229     droeschl 3092: #
                   3093: # NOTES:
                   3094: # - works currently only for javascripts
                   3095: #
1.231     droeschl 3096: # OUTPUT: 
                   3097: # Scriptcode properly enclosed in <script> and CDATA tags (and LC
                   3098: # Internal markers if 2nd argument is given)
1.229     droeschl 3099: sub scripttag {
1.231     droeschl 3100:     my ( $content, $marker ) = @_;
                   3101:     return unless defined $content;
                   3102: 
                   3103:     my $begin = "\n// BEGIN LON-CAPA Internal\n";
                   3104:     my $end   = "\n// END LON-CAPA Internal\n";
                   3105: 
                   3106:     if ($marker) {
                   3107:         $content  = $begin . $content if $marker eq 'start' or $marker eq 'both';
                   3108:         $content .= $end              if $marker eq 'end'   or $marker eq 'both';
                   3109:     }
                   3110: 
1.229     droeschl 3111:     $content = "\n// <![CDATA[\n$content\n// ]]>\n";
1.231     droeschl 3112: 
                   3113:     return htmltag('script', $content, {type => 'text/javascript'});
1.229     droeschl 3114: };
                   3115: 
1.309     raeburn  3116: =pod
1.229     droeschl 3117: 
1.309     raeburn  3118: =item &list_from_array( \@array, { listattr =>{}, itemattr =>{} } )
1.261     droeschl 3119: 
                   3120: Constructs a XHTML list from \@array.
                   3121: 
                   3122: input: 
                   3123: 
                   3124: =over
                   3125: 
                   3126: =item \@array 
                   3127: 
                   3128: A reference to the array containing text that will be wrapped in <li></li> tags.
                   3129: 
                   3130: =item { listattr => {}, itemattr =>{} } 
                   3131: 
                   3132: Attributes for <ul> and <li> passed in as hash references. 
                   3133: See htmltag() for more details.
                   3134: 
                   3135: =back
                   3136:  
                   3137: returns: XHTML list as String. 
                   3138: 
                   3139: =cut   
                   3140: 
                   3141: # \@items, {listattr => { class => 'abc', id => 'xyx' }, itemattr => {class => 'abc', id => 'xyx'}}
                   3142: sub list_from_array {
                   3143:     my ($items, $args) = @_;
1.285     raeburn  3144:     return unless (ref($items) eq 'ARRAY');
1.273     droeschl 3145:     return unless scalar @$items;
1.261     droeschl 3146:     my ($ul, $li) = inittags( qw(ul li) );
                   3147:     my $listitems = join '', map { $li->($_, $args->{itemattr}) } @$items;
                   3148:     return $ul->( $listitems, $args->{listattr} );
                   3149: }
                   3150: 
                   3151: 
1.183     droeschl 3152: ##############################################
                   3153: ##############################################
                   3154: 
                   3155: # generate_menu
                   3156: #
                   3157: # Generates html markup for a menu. 
                   3158: #
                   3159: # Inputs:
                   3160: # An array of following structure:
                   3161: #   ({	categorytitle => 'Categorytitle',
                   3162: #	items => [
1.201     droeschl 3163: #		    {	
                   3164: #           linktext    =>	'Text to be displayed',
                   3165: #			url	        =>	'URL the link is pointing to, i.e. /adm/site?action=dosomething',
1.183     droeschl 3166: #			permission  =>	'Contains permissions as returned from lonnet::allowed(),
1.201     droeschl 3167: #					         must evaluate to true in order to activate the link',
1.184     droeschl 3168: #			icon        =>  'icon filename',
1.186     droeschl 3169: #			alttext	    =>	'alt text for the icon',
1.183     droeschl 3170: #			help	    =>	'Name of the corresponding helpfile',
                   3171: #			linktitle   =>	'Description of the link (used for title tag)'
                   3172: #		    },
                   3173: #		    ...
                   3174: #		]
                   3175: #   }, 
                   3176: #   ...
                   3177: #   )
                   3178: #
                   3179: # Outputs: A scalar containing the html markup for the menu.
                   3180: 
                   3181: sub generate_menu {
                   3182:     my @menu = @_;
1.201     droeschl 3183:     # subs for specific html elements
1.219     droeschl 3184:     my ($h3, $div, $ul, $li, $a, $img) = inittags( qw(h3 div ul li a img) ); 
1.201     droeschl 3185:     
                   3186:     my @categories; # each element represents the entire markup for a category
                   3187:    
                   3188:     foreach my $category (@menu) {
                   3189:         my @links;  # contains the links for the current $category
                   3190:         foreach my $link (@{$$category{items}}) {
                   3191:             next unless $$link{permission};
                   3192:             
                   3193:             # create the markup for the current $link and push it into @links.
                   3194:             # each entry consists of an image and a text optionally followed 
                   3195:             # by a help link.
1.283     raeburn  3196:             my $src;
                   3197:             if ($$link{icon} ne '') {
                   3198:                 $src = '/res/adm/pages/'.$$link{icon};
                   3199:             }
1.232     raeburn  3200:             push(@links,$li->(
1.201     droeschl 3201:                         $a->(
                   3202:                             $img->("", {
                   3203:                                 class => "LC_noBorder LC_middle",
1.283     raeburn  3204:                                 src   => $src,
1.202     droeschl 3205:                                 alt   => mt(defined($$link{alttext}) ?
                   3206:                                 $$link{alttext} : $$link{linktext})
1.201     droeschl 3207:                             }), {
                   3208:                             href  => $$link{url},
1.308     raeburn  3209:                             title => mt($$link{linktitle}),
                   3210:                             class => 'LC_menubuttons_link'
1.201     droeschl 3211:                             }).
1.202     droeschl 3212:                         $a->(mt($$link{linktext}), {
1.201     droeschl 3213:                             href  => $$link{url},
1.202     droeschl 3214:                             title => mt($$link{linktitle}),
1.201     droeschl 3215:                             class => "LC_menubuttons_link"
                   3216:                             }).
                   3217:                          (defined($$link{help}) ? 
                   3218:                          Apache::loncommon::help_open_topic($$link{help}) : ''),
1.232     raeburn  3219:                          {class => "LC_menubuttons_inline_text"}));
1.201     droeschl 3220:         }
                   3221: 
                   3222:         # wrap categorytitle in <h3>, concatenate with 
                   3223:         # joined and in <ul> tags wrapped @links
                   3224:         # and wrap everything in an enclosing <div> and push it into
                   3225:         # @categories
                   3226:         # such that each element looks like:
                   3227:         # <div><h3>title</h3><ul><li>...</li>...</ul></div>
                   3228:         # the category won't be added if there aren't any links
1.232     raeburn  3229:         push(@categories, 
1.202     droeschl 3230:             $div->($h3->(mt($$category{categorytitle}), {class=>"LC_hcell"}).
1.201     droeschl 3231:             $ul->(join('' ,@links),  {class =>"LC_ListStyleNormal" }),
1.232     raeburn  3232:             {class=>"LC_Box LC_400Box"})) if scalar(@links);
1.183     droeschl 3233:     }
1.201     droeschl 3234: 
                   3235:     # wrap the joined @categories in another <div> (column layout)
                   3236:     return $div->(join('', @categories), {class => "LC_columnSection"});
1.183     droeschl 3237: }
1.176     foxr     3238: 
1.224     bisitz   3239: ##############################################
                   3240: ##############################################
                   3241: 
                   3242: =pod
                   3243: 
1.309     raeburn  3244: =item &start_funclist()
1.224     bisitz   3245: 
                   3246: Start list of available functions
                   3247: 
                   3248: Typically used to offer a simple list of available functions
                   3249: at top or bottom of page.
                   3250: All available functions/actions for the current page
                   3251: should be included in this list.
                   3252: 
                   3253: If the optional headline text is not provided, a default text will be used.
                   3254: 
                   3255: 
                   3256: Related routines:
                   3257: =over 4
                   3258: add_item_funclist
                   3259: end_funclist
                   3260: =back
                   3261: 
                   3262: 
                   3263: Inputs: (optional) headline text
                   3264: 
                   3265: Returns: HTML code with function list start
                   3266: 
                   3267: =cut
                   3268: 
                   3269: ##############################################
                   3270: ##############################################
                   3271: 
                   3272: sub start_funclist {
                   3273:     my($legendtext)=@_;
                   3274:     $legendtext=&mt('Functions') if !$legendtext;
1.244     droeschl 3275:     return '<ul class="LC_funclist"><li style="font-weight:bold; margin-left:0.8em;">'.$legendtext.'</li>'."\n";
1.224     bisitz   3276: }
                   3277: 
                   3278: 
                   3279: ##############################################
                   3280: ##############################################
                   3281: 
                   3282: =pod
                   3283: 
1.309     raeburn  3284: =item &add_item_funclist()
1.224     bisitz   3285: 
                   3286: Adds an item to the list of available functions
                   3287: 
                   3288: Related routines:
                   3289: =over 4
                   3290: start_funclist
                   3291: end_funclist
                   3292: =back
                   3293: 
                   3294: Inputs: content item with text and link to function
                   3295: 
                   3296: Returns: HTML code with list item for funclist
                   3297: 
                   3298: =cut
                   3299: 
                   3300: ##############################################
                   3301: ##############################################
                   3302: 
                   3303: sub add_item_funclist {
                   3304:     my($content) = @_;
                   3305:     return '<li>'.$content.'</li>'."\n";
                   3306: }
                   3307: 
                   3308: =pod
                   3309: 
1.309     raeburn  3310: =item &end_funclist()
1.224     bisitz   3311: 
                   3312: End list of available functions
                   3313: 
                   3314: Related routines:
                   3315: =over 4
                   3316: start_funclist
                   3317: add_item_funclist
                   3318: =back
                   3319: 
                   3320: Inputs: ./.
                   3321: 
                   3322: Returns: HTML code with function list end
                   3323: =cut
                   3324: 
                   3325: sub end_funclist {
1.246     bisitz   3326:     return "</ul>\n";
1.224     bisitz   3327: }
                   3328: 
1.261     droeschl 3329: =pod
                   3330: 
1.309     raeburn  3331: =item &funclist_from_array( \@array, {legend => 'text for legend'} )
1.261     droeschl 3332: 
                   3333: Constructs a XHTML list from \@array with the first item being visually
                   3334: highlighted and set to the value of legend or 'Functions' if legend is
                   3335: empty. 
                   3336: 
                   3337: =over
                   3338: 
                   3339: =item \@array
                   3340: 
                   3341: A reference to the array containing text that will be wrapped in <li></li> tags.
                   3342: 
                   3343: =item { legend => 'text' }
                   3344: 
                   3345: A string that's used as visually highlighted first item. 'Functions' is used if
                   3346: it's value evaluates to false.
                   3347: 
                   3348: =back
                   3349:  
                   3350: returns: XHTML list as string. 
                   3351: 
                   3352: =back
                   3353: 
                   3354: =cut  
                   3355: 
                   3356: sub funclist_from_array {
                   3357:     my ($items, $args) = @_;
1.285     raeburn  3358:     return unless(ref($items) eq 'ARRAY');
1.261     droeschl 3359:     $args->{legend} ||= mt('Functions');
                   3360:     return list_from_array( [$args->{legend}, @$items], 
                   3361:                { listattr => {class => 'LC_funclist'} });
                   3362: }   
                   3363: 
1.1       stredwic 3364: 1;
1.23      matthew  3365: 
1.1       stredwic 3366: __END__

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