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

1.2       www         1: # The LearningOnline Network with CAPA
                      2: # a pile of common html routines
                      3: #
1.76    ! www         4: # $Id: lonhtmlcommon.pm,v 1.75 2004/06/05 14:46:12 www 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.10      matthew    58: use Time::Local;
1.47      sakharuk   59: use Time::HiRes;
1.30      www        60: use Apache::lonlocal;
1.1       stredwic   61: use strict;
                     62: 
1.40      www        63: ##############################################
                     64: ##############################################
                     65: 
                     66: =pod
                     67: 
                     68: =item authorbombs
                     69: 
                     70: =cut
                     71: 
                     72: ##############################################
                     73: ##############################################
                     74: 
                     75: sub authorbombs {
                     76:     my $url=shift;
                     77:     $url=&Apache::lonnet::declutter($url);
                     78:     my ($udom,$uname)=($url=~/^(\w+)\/(\w+)\//);
                     79:     my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
                     80:     foreach (keys %bombs) {
                     81: 	if ($_=~/^$udom\/$uname\//) {
                     82: 	    return '<a href="/adm/bombs/'.$url.
                     83: 		'"><img src="/adm/lonMisc/bomb.gif" border="0" /></a>'.
                     84: 		&Apache::loncommon::help_open_topic('About_Bombs');
                     85: 	}
                     86:     }
                     87:     return '';
                     88: }
1.26      matthew    89: 
                     90: ##############################################
                     91: ##############################################
                     92: 
1.41      www        93: sub recent_filename {
                     94:     my $area=shift;
                     95:     return 'nohist_recent_'.&Apache::lonnet::escape($area);
                     96: }
                     97: 
                     98: sub store_recent {
                     99:     my ($area,$name,$value)=@_;
                    100:     my $file=&recent_filename($area);
                    101:     my %recent=&Apache::lonnet::dump($file);
                    102:     if (scalar(keys(%recent))>10) {
                    103: # remove oldest value
                    104: 	my $oldest=time;
                    105: 	my $delkey='';
                    106: 	foreach (keys %recent) {
                    107: 	    my $thistime=(split(/\&/,$recent{$_}))[0];
                    108: 	    if ($thistime<$oldest) {
                    109: 		$oldest=$thistime;
                    110: 		$delkey=$_;
                    111: 	    }
                    112: 	}
                    113: 	&Apache::lonnet::del($file,[$delkey]);
                    114:     }
                    115: # store new value
                    116:     &Apache::lonnet::put($file,{ $name => 
                    117: 				 time.'&'.&Apache::lonnet::escape($value) });
                    118: }
                    119: 
                    120: sub select_recent {
                    121:     my ($area,$fieldname,$event)=@_;
                    122:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
                    123:     my $return="\n<select name='$fieldname'".
                    124: 	($event?" onChange='$event'":'').
                    125: 	">\n<option value=''>--- ".&mt('Recent')." ---</option>";
                    126:     foreach (sort keys %recent) {
                    127: 	unless ($_=~/^error\:/) {
                    128: 	    $return.="\n<option value='$_'>".
                    129: 		&Apache::lonnet::unescape((split(/\&/,$recent{$_}))[1]).
                    130: 		'</option>';
                    131: 	}
                    132:     }
                    133:     $return.="\n</select>\n";
                    134:     return $return;
                    135: }
                    136: 
                    137: 
1.26      matthew   138: =pod
                    139: 
                    140: =item textbox
                    141: 
                    142: =cut
                    143: 
                    144: ##############################################
                    145: ##############################################
                    146: sub textbox {
                    147:     my ($name,$value,$size,$special) = @_;
                    148:     $size = 40 if (! defined($size));
                    149:     my $Str = '<input type="text" name="'.$name.'" size="'.$size.'" '.
                    150:         'value="'.$value.'" '.$special.' />';
                    151:     return $Str;
                    152: }
                    153: 
                    154: ##############################################
                    155: ##############################################
                    156: 
                    157: =pod
                    158: 
                    159: =item checkbox
                    160: 
                    161: =cut
                    162: 
                    163: ##############################################
                    164: ##############################################
                    165: sub checkbox {
1.68      matthew   166:     my ($name,$checked,$value) = @_;
                    167:     my $Str = '<input type="checkbox" name="'.$name.'" ';
                    168:     if (defined($value)) {
                    169:         $Str .= 'value="'.$value.'"';
                    170:     } 
                    171:     if ($checked) {
                    172:         $Str .= ' checked="1"';
                    173:     }
                    174:     $Str .= ' />';
1.26      matthew   175:     return $Str;
                    176: }
                    177: 
1.10      matthew   178: ##############################################
                    179: ##############################################
                    180: 
                    181: =pod
                    182: 
                    183: =item &date_setter
                    184: 
1.22      matthew   185: &date_setter returns html and javascript for a compact date-setting form.
                    186: To retrieve values from it, use &get_date_from_form().
                    187: 
1.10      matthew   188: Inputs
                    189: 
                    190: =over 4
                    191: 
                    192: =item $dname 
                    193: 
                    194: The name to prepend to the form elements.  
                    195: The form elements defined will be dname_year, dname_month, dname_day,
                    196: dname_hour, dname_min, and dname_sec.
                    197: 
                    198: =item $currentvalue
                    199: 
                    200: The current setting for this time parameter.  A unix format time
                    201: (time in seconds since the beginning of Jan 1st, 1970, GMT.  
                    202: An undefined value is taken to indicate the value is the current time.
                    203: Also, to be explicit, a value of 'now' also indicates the current time.
                    204: 
1.26      matthew   205: =item $special
                    206: 
                    207: Additional html/javascript to be associated with each element in
                    208: the date_setter.  See lonparmset for example usage.
                    209: 
1.59      matthew   210: =item $includeempty 
                    211: 
                    212: =item $state
                    213: 
                    214: Specifies the initial state of the form elements.  Either 'disabled' or empty.
                    215: Defaults to empty, which indiciates the form elements are not disabled. 
                    216: 
1.22      matthew   217: =back
                    218: 
                    219: Bugs
                    220: 
                    221: The method used to restrict user input will fail in the year 2400.
                    222: 
1.10      matthew   223: =cut
                    224: 
                    225: ##############################################
                    226: ##############################################
                    227: sub date_setter {
1.67      matthew   228:     my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
                    229:         $no_hh_mm_ss) = @_;
1.59      matthew   230:     if (! defined($state) || $state ne 'disabled') {
                    231:         $state = '';
                    232:     }
1.67      matthew   233:     if (! defined($no_hh_mm_ss)) {
                    234:         $no_hh_mm_ss = 0;
                    235:     }
1.10      matthew   236:     if (! defined($currentvalue) || $currentvalue eq 'now') {
1.39      www       237: 	unless ($includeempty) {
                    238: 	    $currentvalue = time;
                    239: 	} else {
                    240: 	    $currentvalue = 0;
                    241: 	}
1.10      matthew   242:     }
                    243:     # other potentially useful values:     wkday,yrday,is_daylight_savings
1.65      albertel  244:     my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
1.39      www       245:     if ($currentvalue) {
                    246: 	($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
                    247: 	    localtime($currentvalue);
                    248: 	$year += 1900;
                    249:     }
1.10      matthew   250:     my $result = "\n<!-- $dname date setting form -->\n";
                    251:     $result .= <<ENDJS;
                    252: <script language="Javascript">
                    253:     function $dname\_checkday() {
                    254:         var day   = document.$formname.$dname\_day.value;
                    255:         var month = document.$formname.$dname\_month.value;
                    256:         var year  = document.$formname.$dname\_year.value;
                    257:         var valid = true;
                    258:         if (day < 1) {
                    259:             document.$formname.$dname\_day.value = 1;
                    260:         } 
                    261:         if (day > 31) {
                    262:             document.$formname.$dname\_day.value = 31;
                    263:         }
                    264:         if ((month == 1)  || (month == 3)  || (month == 5)  ||
                    265:             (month == 7)  || (month == 8)  || (month == 10) ||
                    266:             (month == 12)) {
                    267:             if (day > 31) {
                    268:                 document.$formname.$dname\_day.value = 31;
                    269:                 day = 31;
                    270:             }
                    271:         } else if (month == 2 ) {
                    272:             if ((year % 4 == 0) && (year % 100 != 0)) {
                    273:                 if (day > 29) {
                    274:                     document.$formname.$dname\_day.value = 29;
                    275:                 }
                    276:             } else if (day > 29) {
                    277:                 document.$formname.$dname\_day.value = 28;
                    278:             }
                    279:         } else if (day > 30) {
                    280:             document.$formname.$dname\_day.value = 30;
                    281:         }
                    282:     }
1.29      www       283: 
1.59      matthew   284:     function $dname\_disable() {
                    285:         document.$formname.$dname\_month.disabled=true;
                    286:         document.$formname.$dname\_day.disabled=true;
                    287:         document.$formname.$dname\_year.disabled=true;
                    288:         document.$formname.$dname\_hour.disabled=true;
                    289:         document.$formname.$dname\_minute.disabled=true;
                    290:         document.$formname.$dname\_second.disabled=true;
                    291:     }
                    292: 
                    293:     function $dname\_enable() {
                    294:         document.$formname.$dname\_month.disabled=false;
                    295:         document.$formname.$dname\_day.disabled=false;
                    296:         document.$formname.$dname\_year.disabled=false;
                    297:         document.$formname.$dname\_hour.disabled=false;
                    298:         document.$formname.$dname\_minute.disabled=false;
                    299:         document.$formname.$dname\_second.disabled=false;        
                    300:     }
                    301: 
1.29      www       302:     function $dname\_opencalendar() {
1.59      matthew   303:         if (! document.$formname.$dname\_month.disabled) {
                    304:             var calwin=window.open(
1.29      www       305: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
                    306: document.$formname.$dname\_month.value+"&year="+
                    307: document.$formname.$dname\_year.value,
                    308:              "LONCAPAcal",
                    309:               "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
1.59      matthew   310:         }
1.29      www       311: 
                    312:     }
1.10      matthew   313: </script>
                    314: ENDJS
1.26      matthew   315:     $result .= "  <nobr><select name=\"$dname\_month\" ".$special.' '.
1.59      matthew   316:         $state.' '.
1.10      matthew   317:         "onChange=\"javascript:$dname\_checkday()\" >\n";
1.67      matthew   318:     # Month
1.10      matthew   319:     my @Months = qw/January February  March     April   May      June 
                    320:                     July    August    September October November December/;
                    321:     # Pad @Months with a bogus value to make indexing easier
                    322:     unshift(@Months,'If you can read this an error occurred');
1.39      www       323:     if ($includeempty) { $result.="<option value=''></option>"; }
1.10      matthew   324:     for(my $m = 1;$m <=$#Months;$m++) {
                    325:         $result .= "      <option value=\"$m\" ";
1.39      www       326:         $result .= "selected " if ($m-1 eq $month);
1.30      www       327:         $result .= "> ".&mt($Months[$m])." </option>\n";
1.10      matthew   328:     }
                    329:     $result .= "  </select>\n";
1.67      matthew   330:     # Day
1.59      matthew   331:     $result .= "  <input type=\"text\" name=\"$dname\_day\" ".$state.' '.
1.26      matthew   332:             "value=\"$mday\" size=\"3\" ".$special.' '.
1.10      matthew   333:             "onChange=\"javascript:$dname\_checkday()\" />\n";
1.67      matthew   334:     # Year
1.59      matthew   335:     $result .= "  <input type=\"year\" name=\"$dname\_year\" ".$state.' '.
1.26      matthew   336:             "value=\"$year\" size=\"5\" ".$special.' '.
1.10      matthew   337:             "onChange=\"javascript:$dname\_checkday()\" />\n";
                    338:     $result .= "&nbsp;&nbsp;";
1.67      matthew   339:     if (! $no_hh_mm_ss) {
                    340:         # Hours
                    341:         $result .= "  <select name=\"$dname\_hour\" ".$special." ".$state.' '.
                    342:             ">\n";
                    343:         if ($includeempty) { $result.="<option value=''></option>"; }
                    344:         for (my $h = 0;$h<24;$h++) {
                    345:             $result .= "      <option value=\"$h\" ";
                    346:             $result .= "selected " if (defined($hour) && $hour == $h);
                    347:             $result .= "> ";
                    348:             my $timest='';
                    349:             if ($h == 0) {
                    350:                 $timest .= "12 am";
                    351:             } elsif($h == 12) {
                    352:                 $timest .= "12 noon";
                    353:             } elsif($h < 12) {
                    354:                 $timest .= "$h am";
                    355:             } else {
                    356:                 $timest .= $h-12 ." pm";
                    357:             }
                    358:             $timest=&mt($timest);
                    359:             $result .= $timest." </option>\n";
                    360:         } 
                    361:         $result .= "  </select>\n";
                    362:         $result .= "  <input type=\"text\" name=\"$dname\_minute\" ".
                    363:             $special.' '.
                    364:             $state.' '.
                    365:             "value=\"$min\" size=\"3\" /> m\n";
                    366:         $result .= "  <input type=\"text\" name=\"$dname\_second\" ".
                    367:             $special.' '.
                    368:             $state.' '.
                    369:             "value=\"$sec\" size=\"3\" /> s\n";
                    370:     }
1.30      www       371:     $result .= "<a href=\"javascript:$dname\_opencalendar()\">".
                    372:     &mt('Select Date')."</a></nobr>\n<!-- end $dname date setting form -->\n";
1.10      matthew   373:     return $result;
                    374: }
                    375: 
                    376: ##############################################
                    377: ##############################################
                    378: 
1.22      matthew   379: =pod
                    380: 
1.10      matthew   381: =item &get_date_from_form
1.22      matthew   382: 
                    383: get_date_from_form retrieves the date specified in an &date_setter form.
1.10      matthew   384: 
                    385: Inputs:
                    386: 
                    387: =over 4
                    388: 
                    389: =item $dname
                    390: 
                    391: The name passed to &datesetter, which prefixes the form elements.
                    392: 
                    393: =item $defaulttime
                    394: 
                    395: The unix time to use as the default in case of poor inputs.
                    396: 
                    397: =back
                    398: 
                    399: Returns: Unix time represented in the form.
                    400: 
                    401: =cut
                    402: 
                    403: ##############################################
                    404: ##############################################
                    405: sub get_date_from_form {
                    406:     my ($dname) = @_;
                    407:     my ($sec,$min,$hour,$day,$month,$year);
                    408:     #
                    409:     if (defined($ENV{'form.'.$dname.'_second'})) {
                    410:         my $tmpsec = $ENV{'form.'.$dname.'_second'};
                    411:         if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
                    412:             $sec = $tmpsec;
                    413:         }
1.64      albertel  414: 	if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
1.67      matthew   415:     } else {
                    416:         $sec = 0;
1.10      matthew   417:     }
                    418:     if (defined($ENV{'form.'.$dname.'_minute'})) {
                    419:         my $tmpmin = $ENV{'form.'.$dname.'_minute'};
                    420:         if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
                    421:             $min = $tmpmin;
                    422:         }
1.64      albertel  423: 	if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
1.67      matthew   424:     } else {
                    425:         $min = 0;
1.10      matthew   426:     }
                    427:     if (defined($ENV{'form.'.$dname.'_hour'})) {
                    428:         my $tmphour = $ENV{'form.'.$dname.'_hour'};
1.33      matthew   429:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
1.10      matthew   430:             $hour = $tmphour;
                    431:         }
1.67      matthew   432:     } else {
                    433:         $hour = 0;
1.10      matthew   434:     }
                    435:     if (defined($ENV{'form.'.$dname.'_day'})) {
                    436:         my $tmpday = $ENV{'form.'.$dname.'_day'};
                    437:         if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
                    438:             $day = $tmpday;
                    439:         }
                    440:     }
                    441:     if (defined($ENV{'form.'.$dname.'_month'})) {
                    442:         my $tmpmonth = $ENV{'form.'.$dname.'_month'};
                    443:         if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
                    444:             $month = $tmpmonth - 1;
                    445:         }
                    446:     }
                    447:     if (defined($ENV{'form.'.$dname.'_year'})) {
                    448:         my $tmpyear = $ENV{'form.'.$dname.'_year'};
                    449:         if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
                    450:             $year = $tmpyear - 1900;
                    451:         }
                    452:     }
1.24      www       453:     if (($year<70) || ($year>137)) { return undef; }
1.33      matthew   454:     if (defined($sec) && defined($min)   && defined($hour) &&
                    455:         defined($day) && defined($month) && defined($year) &&
                    456:         eval(&timelocal($sec,$min,$hour,$day,$month,$year))) {
1.10      matthew   457:         return &timelocal($sec,$min,$hour,$day,$month,$year);
                    458:     } else {
                    459:         return undef;
                    460:     }
1.20      matthew   461: }
                    462: 
                    463: ##############################################
                    464: ##############################################
                    465: 
                    466: =pod
                    467: 
                    468: =item &pjump_javascript_definition()
                    469: 
                    470: Returns javascript defining the 'pjump' function, which opens up a
                    471: parameter setting wizard.
                    472: 
                    473: =cut
                    474: 
                    475: ##############################################
                    476: ##############################################
                    477: sub pjump_javascript_definition {
                    478:     my $Str = <<END;
                    479:     function pjump(type,dis,value,marker,ret,call) {
                    480:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
                    481:                  +"&value="+escape(value)+"&marker="+escape(marker)
                    482:                  +"&return="+escape(ret)
                    483:                  +"&call="+escape(call)+"&name="+escape(dis),"LONCAPAparms",
                    484:                  "height=350,width=350,scrollbars=no,menubar=no");
                    485:     }
                    486: END
                    487:     return $Str;
1.10      matthew   488: }
                    489: 
                    490: ##############################################
                    491: ##############################################
1.17      matthew   492: 
                    493: =pod
                    494: 
                    495: =item &javascript_nothing()
                    496: 
                    497: Return an appropriate null for the users browser.  This is used
                    498: as the first arguement for window.open calls when you want a blank
                    499: window that you can then write to.
                    500: 
                    501: =cut
                    502: 
                    503: ##############################################
                    504: ##############################################
                    505: sub javascript_nothing {
                    506:     # mozilla and other browsers work with "''", but IE on mac does not.
                    507:     my $nothing = "''";
                    508:     my $user_browser;
                    509:     my $user_os;
                    510:     $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
                    511:     $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
                    512:     if (! defined($user_browser) || ! defined($user_os)) {
                    513:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    514:                            &Apache::loncommon::decode_user_agent();
                    515:     }
                    516:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                    517:         $nothing = "'javascript:void(0);'";
                    518:     }
                    519:     return $nothing;
                    520: }
                    521: 
1.21      matthew   522: 
1.17      matthew   523: ##############################################
                    524: ##############################################
                    525: 
1.21      matthew   526: =pod
1.17      matthew   527: 
1.21      matthew   528: =item &StatusOptions()
1.10      matthew   529: 
1.21      matthew   530: Returns html for a selection box which allows the user to choose the
                    531: enrollment status of students.  The selection box name is 'Status'.
1.6       stredwic  532: 
1.21      matthew   533: Inputs:
1.6       stredwic  534: 
1.21      matthew   535: $status: the currently selected status.  If undefined the value of
                    536: $ENV{'form.Status'} is taken.  If that is undefined, a value of 'Active'
                    537: is used.
1.6       stredwic  538: 
1.21      matthew   539: $formname: The name of the form.  If defined the onchange attribute of
                    540: the selection box is set to document.$formname.submit().
1.6       stredwic  541: 
1.21      matthew   542: $size: the size (number of lines) of the selection box.
1.6       stredwic  543: 
1.27      matthew   544: $onchange: javascript to use when the value is changed.  Enclosed in 
                    545: double quotes, ""s, not single quotes.
                    546: 
1.21      matthew   547: Returns: a perl string as described.
1.1       stredwic  548: 
1.21      matthew   549: =cut
1.9       stredwic  550: 
1.21      matthew   551: ##############################################
                    552: ##############################################
                    553: sub StatusOptions {
1.27      matthew   554:     my ($status, $formName,$size,$onchange)=@_;
1.21      matthew   555:     $size = 1 if (!defined($size));
                    556:     if (! defined($status)) {
                    557:         $status = 'Active';
                    558:         $status = $ENV{'form.Status'} if (exists($ENV{'form.Status'}));
1.9       stredwic  559:     }
1.1       stredwic  560: 
                    561:     my $OpSel1 = '';
                    562:     my $OpSel2 = '';
                    563:     my $OpSel3 = '';
                    564: 
                    565:     if($status eq 'Any')         { $OpSel3 = ' selected'; }
                    566:     elsif($status eq 'Expired' ) { $OpSel2 = ' selected'; }
                    567:     else                         { $OpSel1 = ' selected'; }
                    568: 
                    569:     my $Str = '';
                    570:     $Str .= '<select name="Status"';
1.27      matthew   571:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
1.1       stredwic  572:         $Str .= ' onchange="document.'.$formName.'.submit()"';
1.27      matthew   573:     }
                    574:     if (defined($onchange)) {
                    575:         $Str .= ' onchange="'.$onchange.'"';
1.1       stredwic  576:     }
1.21      matthew   577:     $Str .= ' size="'.$size.'" ';
1.1       stredwic  578:     $Str .= '>'."\n";
1.21      matthew   579:     $Str .= '<option value="Active" '.$OpSel1.'>'.
1.37      www       580:         &mt('Currently Enrolled').'</option>'."\n";
1.21      matthew   581:     $Str .= '<option value="Expired" '.$OpSel2.'>'.
1.37      www       582:         &mt('Previously Enrolled').'</option>'."\n";
1.21      matthew   583:     $Str .= '<option value="Any" '.$OpSel3.'>'.
1.37      www       584:         &mt('Any Enrollment Status').'</option>'."\n";
1.1       stredwic  585:     $Str .= '</select>'."\n";
1.7       stredwic  586: }
1.12      matthew   587: 
                    588: ########################################################
                    589: ########################################################
1.7       stredwic  590: 
1.23      matthew   591: =pod
                    592: 
                    593: =item Progess Window Handling Routines
                    594: 
                    595: These routines handle the creation, update, increment, and closure of 
                    596: progress windows.  The progress window reports to the user the number
                    597: of items completed and an estimate of the time required to complete the rest.
                    598: 
                    599: =over 4
                    600: 
                    601: 
                    602: =item &Create_PrgWin
                    603: 
                    604: Writes javascript to the client to open a progress window and returns a
                    605: data structure used for bookkeeping.
                    606: 
                    607: Inputs
                    608: 
                    609: =over 4
                    610: 
                    611: =item $r Apache request
                    612: 
                    613: =item $title The title of the progress window
                    614: 
                    615: =item $heading A description (usually 1 line) of the process being initiated.
                    616: 
                    617: =item $number_to_do The total number of items being processed.
1.50      albertel  618: 
                    619: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
                    620:        specified)
                    621: 
1.51      albertel  622: =item $width Specify the width in charaters of the input field.
                    623: 
1.50      albertel  624: =item $formname Only useful in the inline case, if a form already exists, this needs to be used and specfiy the name of the form, otherwise the Progress line will be created in a new form of it's own
                    625: 
                    626: =item $inputname Only useful in the inline case, if a form and an input of type text exists, use this to specify the name of the input field 
1.23      matthew   627: 
                    628: =back
                    629: 
                    630: Returns a hash containing the progress state data structure.
                    631: 
                    632: 
                    633: =item &Update_PrgWin
                    634: 
                    635: Updates the text in the progress indicator.  Does not increment the count.
                    636: See &Increment_PrgWin.
                    637: 
                    638: Inputs:
                    639: 
                    640: =over 4
                    641: 
                    642: =item $r Apache request
                    643: 
                    644: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
                    645: 
                    646: =item $displaystring The string to write to the status indicator
                    647: 
                    648: =back
                    649: 
                    650: Returns: none
                    651: 
                    652: 
                    653: =item Increment_PrgWin
                    654: 
                    655: Increment the count of items completed for the progress window by 1.  
                    656: 
                    657: Inputs:
                    658: 
                    659: =over 4
                    660: 
                    661: =item $r Apache request
                    662: 
                    663: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    664: 
                    665: =item $extraInfo A description of the items being iterated over.  Typically
                    666: 'student'.
                    667: 
                    668: =back
                    669: 
                    670: Returns: none
                    671: 
                    672: 
                    673: =item Close_PrgWin
                    674: 
                    675: Closes the progress window.
                    676: 
                    677: Inputs:
                    678: 
                    679: =over 4 
                    680: 
                    681: =item $r Apache request
                    682: 
                    683: =item $prog_state Pointer to the data structure returned by Create_PrgWin
                    684: 
                    685: =back
                    686: 
                    687: Returns: none
                    688: 
                    689: =back
                    690: 
                    691: =cut
                    692: 
                    693: ########################################################
                    694: ########################################################
                    695: 
1.51      albertel  696: my $uniq=0;
                    697: sub get_uniq_name {
                    698:     $uniq++;
                    699:     return 'uniquename'.$uniq;
                    700: }
                    701: 
1.7       stredwic  702: # Create progress
                    703: sub Create_PrgWin {
1.51      albertel  704:     my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
                    705: 	$inputname)=@_;
1.49      albertel  706:     if (!defined($type)) { $type='popup'; }
1.51      albertel  707:     if (!defined($width)) { $width=55; }
1.49      albertel  708:     my %prog_state;
                    709:     $prog_state{'type'}=$type;
                    710:     if ($type eq 'popup') {
                    711: 	$prog_state{'window'}='popwin';
                    712: 	#the whole function called through timeout is due to issues
                    713: 	#in mozilla Read BUG #2665 if you want to know the whole story
                    714: 	&r_print($r,'<script>'.
                    715:         "var popwin;
                    716:          function openpopwin () {
                    717:          popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
                    718:         "popwin.document.writeln(\'<html><head><title>$title</title></head>".
1.48      albertel  719: 	      "<body bgcolor=\"#88DDFF\">".
                    720:               "<h4>$heading</h4>".
                    721:               "<form name=popremain>".
1.51      albertel  722:               '<input type="text" size="'.$width.'" name="remaining" value="'.
1.48      albertel  723: 	      &mt('Starting').'"></form>'.
                    724:               "</body></html>\');".
1.49      albertel  725:         "popwin.document.close();}".
                    726:         "\nwindow.setTimeout(openpopwin,0)</script>");
                    727: 	$prog_state{'formname'}='popremain';
                    728: 	$prog_state{'inputname'}="remaining";
                    729:     } elsif ($type eq 'inline') {
                    730: 	$prog_state{'window'}='window';
                    731: 	if (!$formname) {
1.51      albertel  732: 	    $prog_state{'formname'}=&get_uniq_name();
                    733: 	    &r_print($r,'<form name="'.$prog_state{'formname'}.'">');
1.49      albertel  734: 	} else {
                    735: 	    $prog_state{'formname'}=$formname;
                    736: 	}
                    737: 	if (!$inputname) {
1.51      albertel  738: 	    $prog_state{'inputname'}=&get_uniq_name();
1.56      albertel  739: 	    &r_print($r,$heading.' <input type="text" name="'.$prog_state{'inputname'}.
1.51      albertel  740: 		     '" size="'.$width.'" />');
1.49      albertel  741: 	} else {
                    742: 	    $prog_state{'inputname'}=$inputname;
                    743: 	    
                    744: 	}
                    745: 	if (!$formname) { &r_print($r,'</form>'); }
                    746: 	&Update_PrgWin($r,\%prog_state,&mt('Starting'));
                    747:     }
1.7       stredwic  748: 
1.16      albertel  749:     $prog_state{'done'}=0;
1.23      matthew   750:     $prog_state{'firststart'}=&Time::HiRes::time();
                    751:     $prog_state{'laststart'}=&Time::HiRes::time();
1.16      albertel  752:     $prog_state{'max'}=$number_to_do;
1.49      albertel  753:     
1.14      albertel  754:     return %prog_state;
1.7       stredwic  755: }
                    756: 
                    757: # update progress
                    758: sub Update_PrgWin {
1.14      albertel  759:     my ($r,$prog_state,$displayString)=@_;
1.49      albertel  760:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
                    761: 	     $$prog_state{'formname'}.'.'.
                    762: 	     $$prog_state{'inputname'}.'.value="'.
1.48      albertel  763: 	     $displayString.'";</script>');
1.23      matthew   764:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.14      albertel  765: }
                    766: 
                    767: # increment progress state
                    768: sub Increment_PrgWin {
                    769:     my ($r,$prog_state,$extraInfo)=@_;
1.16      albertel  770:     $$prog_state{'done'}++;
1.23      matthew   771:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
                    772:         $$prog_state{'done'} *
1.16      albertel  773: 	($$prog_state{'max'}-$$prog_state{'done'});
                    774:     $time_est = int($time_est);
                    775:     if (int ($time_est/60) > 0) {
                    776: 	my $min = int($time_est/60);
                    777: 	my $sec = $time_est % 60;
1.31      www       778: 	$time_est = $min.' '.&mt('minutes');
1.25      matthew   779:         if ($min < 10)  {
                    780:             if ($sec > 1) {
1.31      www       781:                 $time_est.= ', '.$sec.' '.&mt('seconds');
1.25      matthew   782:             } elsif ($sec > 0) {
1.31      www       783:                 $time_est.= ', '.$sec.' '.&mt('second');
1.25      matthew   784:             }
                    785:         }
1.16      albertel  786:     } else {
1.31      www       787: 	$time_est .= ' '.&mt('seconds');
1.16      albertel  788:     }
1.23      matthew   789:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
                    790:     if ($lasttime > 9) {
                    791:         $lasttime = int($lasttime);
                    792:     } elsif ($lasttime < 0.01) {
                    793:         $lasttime = 0;
                    794:     } else {
                    795:         $lasttime = sprintf("%3.2f",$lasttime);
                    796:     }
1.19      matthew   797:     if ($lasttime == 1) {
1.32      www       798:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
1.19      matthew   799:     } else {
1.32      www       800:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
1.28      matthew   801:     }
                    802:     #
                    803:     my $user_browser = $ENV{'browser.type'} if (exists($ENV{'browser.type'}));
                    804:     my $user_os      = $ENV{'browser.os'}   if (exists($ENV{'browser.os'}));
                    805:     if (! defined($user_browser) || ! defined($user_os)) {
                    806:         (undef,$user_browser,undef,undef,undef,$user_os) = 
                    807:                            &Apache::loncommon::decode_user_agent();
                    808:     }
                    809:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
                    810:         $lasttime = '';
1.19      matthew   811:     }
1.49      albertel  812:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
                    813: 	     $$prog_state{'formname'}.'.'.
                    814: 	     $$prog_state{'inputname'}.'.value="'.
1.48      albertel  815: 	     $$prog_state{'done'}.'/'.$$prog_state{'max'}.
                    816: 	     ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
1.23      matthew   817:     $$prog_state{'laststart'}=&Time::HiRes::time();
1.7       stredwic  818: }
                    819: 
                    820: # close Progress Line
                    821: sub Close_PrgWin {
1.14      albertel  822:     my ($r,$prog_state)=@_;
1.49      albertel  823:     if ($$prog_state{'type'} eq 'popup') {
                    824: 	&r_print($r,'<script>popwin.close()</script>'."\n");
                    825:     } elsif ($$prog_state{'type'} eq 'inline') {
                    826: 	&Update_PrgWin($r,$prog_state,&mt('Done'));
                    827:     }
1.48      albertel  828:     undef(%$prog_state);
                    829: }
                    830: 
                    831: sub r_print {
                    832:     my ($r,$to_print)=@_;
                    833:     if ($r) {
                    834: 	$r->print($to_print);
                    835: 	$r->rflush();
1.47      sakharuk  836:     } else {
1.48      albertel  837: 	print($to_print);
1.47      sakharuk  838:     }
1.1       stredwic  839: }
1.34      www       840: 
                    841: # ------------------------------------------------------- Puts directory header
                    842: 
                    843: sub crumbs {
1.62      matthew   844:     my ($uri,$target,$prefix,$form,$size)=@_;
                    845:     if (! defined($size)) {
                    846:         $size = '+2';
                    847:     }
                    848:     my $output='<br /><tt><b><font size="'.$size.'">'.$prefix.'/';
1.35      www       849:     if ($ENV{'user.adv'}) {
1.43      www       850: 	my $path=$prefix.'/';
1.35      www       851: 	foreach (split('/',$uri)) {
                    852: 	    unless ($_) { next; }
1.43      www       853: 	    $path.=$_;
                    854: 	    unless ($path eq $uri) { $path.='/'; }
1.41      www       855: 	    my $linkpath=$path;
                    856: 	    if ($form) {
1.43      www       857: 		$linkpath="javascript:$form.action='$path';$form.submit();";
1.41      www       858: 	    }
                    859: 	    $output.='<a href="'.$linkpath.'"'.($target?' target="'.$target.'"':'').'>'.$_.'</a>/';
1.35      www       860: 	}
                    861:     } else {
                    862: 	$output.=$uri;
1.34      www       863:     }
1.36      www       864:     unless ($uri=~/\/$/) { $output=~s/\/$//; }
1.34      www       865:     return $output.'</font></b></tt><br />';
                    866: }
                    867: 
1.52      www       868: # ------------------------------------------------- Output headers for HTMLArea
                    869: 
                    870: sub htmlareaheaders {
1.71      www       871:     if (&htmlareablocked()) { return ''; }
1.76    ! www       872:     unless (&htmlareabrowser()) { return ''; }
1.52      www       873:     my $lang='en';
1.71      www       874:     if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
                    875: 	$lang=&mt('htmlarea_lang');
                    876:     }
1.52      www       877:     return (<<ENDHEADERS);
1.61      www       878: <script type="text/javascript">
1.73      www       879: _editor_url='/htmlarea/';
                    880: _editor_lang='$lang';
1.61      www       881: </script>
1.52      www       882: <script type="text/javascript" src="/htmlarea/htmlarea.js"></script>
                    883: ENDHEADERS
                    884: }
                    885: 
1.74      www       886: # ------------------------------------------------- Activate additional buttons
                    887: 
                    888: sub htmlareaaddbuttons {
                    889:     if (&htmlareablocked()) { return ''; }
1.76    ! www       890:     unless (&htmlareabrowser()) { return ''; }
1.74      www       891:     return (<<ENDADDBUTTON);
                    892:     var config=new HTMLArea.Config();
                    893:     config.registerButton('ed_math','LaTeX Inline',
                    894: 			  '/htmlarea/images/ed_math.gif',false,
                    895: 			    function(editor,id) {
                    896: 			      editor.surroundHTML('<m>\$','\$</m>');
                    897: 			    }
                    898: 			  );
                    899:     config.registerButton('ed_math_eqn','LaTeX Equation',
                    900: 			  '/htmlarea/images/ed_math_eqn.gif',false,
                    901: 			    function(editor,id) {
1.75      www       902: 			      editor.surroundHTML(
                    903: 				     '<center><m>\\\\[','\\\\]</m></center>');
1.74      www       904: 			    }
                    905: 			  );
                    906:     config.toolbar.push(['ed_math','ed_math_eqn']);
                    907: ENDADDBUTTON
                    908: }
1.76    ! www       909: 
        !           910: # ----------------------------------------------------------------- Preferences
        !           911: 
        !           912: sub disablelink {
        !           913:     return '<a href="/adm/preferences?action=set_wysiwyg&wysiwyg=off&returnurl='.&Apache::lonnet::escape($ENV{'REQUEST_URI'}).'">'.&mt('Disable WYSIWYG Editor').'</a>';
        !           914: }
        !           915: 
        !           916: sub enablelink {
        !           917:     return '<a href="/adm/preferences?action=set_wysiwyg&wysiwyg=on&returnurl='.&Apache::lonnet::escape($ENV{'REQUEST_URI'}).'">'.&mt('Enable WYSIWYG Editor').'</a>';
        !           918: }
        !           919: 
1.52      www       920: # ---------------------------------------------------------- Script to activate
                    921: 
                    922: sub htmlareaactive {
1.76    ! www       923:     unless (&htmlareabrowser()) { return ''; }
        !           924:     if (&htmlareablocked()) { return '<br />'.&enablelink(); }
1.74      www       925:     my $addbuttons=&htmlareaaddbuttons();
1.76    ! www       926:     my $disable=&disablelink();
1.52      www       927:     return (<<ENDSCRIPT);
                    928: <script type="text/javascript" defer="1">
1.74      www       929:     $addbuttons
                    930:     HTMLArea.replaceAll(config);
1.76    ! www       931: </script><br />
        !           932: $disable
1.52      www       933: ENDSCRIPT
1.61      www       934: }
                    935: 
1.72      www       936: # ----------------------------------------- Script to activate only some fields
                    937: 
                    938: sub htmlareaselectactive {
1.73      www       939:     my @fields=@_;
1.76    ! www       940:     unless (&htmlareabrowser()) { return ''; }
        !           941:     if (&htmlareablocked()) { return '<br />'.&enablelink(); }
1.74      www       942:     my $output='<script type="text/javascript" defer="1">'.
                    943: 	&htmlareaaddbuttons();
1.73      www       944:     foreach(@fields) {
1.74      www       945: 	$output.="\nHTMLArea.replace('$_',config);";
1.72      www       946:     }
1.76    ! www       947:     $output.="\nwindow.status='Activated Editfields';\n</script><br />".
        !           948: 	&disablelink();
1.72      www       949:     return $output;
                    950: }
                    951: 
1.61      www       952: # --------------------------------------------------------------------- Blocked
                    953: 
                    954: sub htmlareablocked {
1.76    ! www       955:     unless ($ENV{'environment.wysiwygeditor'} eq 'on') { return 1; }
1.71      www       956:     return 0;
1.52      www       957: }
                    958: 
                    959: # ---------------------------------------- Browser capable of running HTMLArea?
                    960: 
                    961: sub htmlareabrowser {
                    962:     return 1;
                    963: }
1.53      matthew   964: 
                    965: ############################################################
                    966: ############################################################
                    967: 
                    968: =pod
                    969: 
                    970: =item breadcrumbs
                    971: 
                    972: Compiles the previously registered breadcrumbs into an series of links.
                    973: FAQ and BUG links will be placed on the left side of the table if they
                    974: are defined for the last registered breadcrumb.  
                    975: Additionally supports a 'component', which will be displayed on the
                    976: right side of the table (without a link).
                    977: A link to help for the component will be included if one is specified.
                    978: 
                    979: All inputs can be undef without problems.
                    980: 
                    981: Inputs: $color (the background color of the table returned),
                    982:         $component (the large text on the right side of the table),
                    983:         $component_help
1.63      albertel  984:         $function (role to get colors from)
                    985:         $domain   (domian of role)
                    986:         $menulink (boolean, controls whether to include a link to /adm/menu)
1.53      matthew   987: 
                    988: Returns a string containing breadcrumbs for the current page.
                    989: 
                    990: =item clear_breadcrumbs
                    991: 
                    992: Clears the previously stored breadcrumbs.
                    993: 
                    994: =item add_breadcrumb
                    995: 
                    996: Pushes a breadcrumb on the stack of crumbs.
                    997: 
                    998: input: $breadcrumb, a hash reference.  The keys 'href','title', and 'text'
                    999: are required.  If present the keys 'faq' and 'bug' will be used to provide
                   1000: links to the FAQ and bug sites.
                   1001: 
                   1002: returns: nothing    
                   1003: 
                   1004: =cut
                   1005: 
                   1006: ############################################################
                   1007: ############################################################
                   1008: {
                   1009:     my @Crumbs;
1.57      matthew  1010:     
1.53      matthew  1011:     sub breadcrumbs {
1.63      albertel 1012:         my ($color,$component,$component_help,$function,$domain,$menulink) =
                   1013: 	    @_;
1.55      matthew  1014:         if (! defined($color)) {
                   1015:             if (! defined($function)) {
                   1016:                 $function = &Apache::loncommon::get_users_function();
                   1017:             }
                   1018:             $color = &Apache::loncommon::designparm($function.'.tabbg',
                   1019:                                                     $domain);
                   1020:         }
1.53      matthew  1021:         #
                   1022:         my $Str = "\n".
                   1023:             '<table width="100%" border="0" cellpadding="0" cellspacing="0">'.
                   1024:             '<tr><td bgcolor="'.$color.'">'.
                   1025:             '<font size="-1">';
1.57      matthew  1026:         #
                   1027:         # Make the faq and bug data cascade
                   1028:         my $faq = '';
                   1029:         my $bug = '';
1.60      www      1030:         # The last breadcrumb does not have a link, so handle it separately.
1.53      matthew  1031:         my $last = pop(@Crumbs);
1.57      matthew  1032:         #
1.70      matthew  1033:         # The first one should be the course or a menu link
1.63      albertel 1034: 	if (!defined($menulink)) { $menulink=1; }
1.70      matthew  1035:         if ($menulink) {
                   1036:             my $description = 'Menu';
                   1037:             if (exists($ENV{'request.course.id'}) && 
                   1038:                 $ENV{'request.course.id'} ne '') {
                   1039:                 $description = 
                   1040:                     $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
                   1041:             }
1.57      matthew  1042:             unshift(@Crumbs,{
1.70      matthew  1043:                     href   =>'/adm/menu',
                   1044:                     title  =>'Go to main menu',
                   1045:                     target =>'_top',
                   1046:                     text   =>$description,
                   1047:                 });
1.53      matthew  1048:         }
                   1049:         my $links .= 
                   1050:             join('-&gt;',
                   1051:                  map {
1.57      matthew  1052:                      $faq = $_->{'faq'} if (exists($_->{'faq'}));
                   1053:                      $bug = $_->{'bug'} if (exists($_->{'bug'}));
1.69      matthew  1054:                      my $result = '<a href="'.$_->{'href'}.'" ';
                   1055:                      if (defined($_->{'target'}) && $_->{'target'} ne '') {
                   1056:                          $result .= 'target="'.$_->{'target'}.'" ';
                   1057:                      }
                   1058:                      $result .='title="'.&mt($_->{'title'}).'">'.
                   1059:                          &mt($_->{'text'}).'</a>';
                   1060:                      $result;
1.53      matthew  1061:                      } @Crumbs
                   1062:                  );
                   1063:         $links .= '-&gt;' if ($links ne '');
                   1064:         $links .= '<b>'.$last->{'text'}.'</b>';
1.54      matthew  1065:         #
                   1066:         my $icons = '';
1.57      matthew  1067:         $faq = $last->{'faq'} if (exists($last->{'faq'}));
                   1068:         $bug = $last->{'bug'} if (exists($last->{'bug'}));
                   1069:         if ($faq ne '') {
                   1070:             $icons .= &Apache::loncommon::help_open_faq($faq);
1.54      matthew  1071:         }
1.57      matthew  1072:         if ($bug ne '') {
                   1073:             $icons .= &Apache::loncommon::help_open_bug($bug);
1.53      matthew  1074:         }
1.54      matthew  1075:         if ($icons ne '') {
                   1076:             $Str .= $icons.'&nbsp;';
1.53      matthew  1077:         }
1.54      matthew  1078:         #
1.53      matthew  1079:         $Str .= $links.'</font></td>';
1.54      matthew  1080:         #
1.53      matthew  1081:         if (defined($component)) {
                   1082:             $Str .= '<td align="right" bgcolor="'.$color.'">'.
1.58      www      1083:                 '<font size="+1">'.&mt($component).'</font>';
1.53      matthew  1084:             if (defined($component_help)) {
                   1085:                 $Str .= 
                   1086:                     &Apache::loncommon::help_open_topic($component_help);
                   1087:             }
                   1088:             $Str.= '</td>';
                   1089:         }
                   1090:         $Str .= '</tr></table>'."\n";
                   1091:         #
                   1092:         # Return the @Crumbs stack to what we started with
                   1093:         push(@Crumbs,$last);
                   1094:         shift(@Crumbs);
                   1095:         #
                   1096:         return $Str;
                   1097:     }
                   1098: 
                   1099:     sub clear_breadcrumbs {
                   1100:         undef(@Crumbs);
                   1101:     }
                   1102: 
                   1103:     sub add_breadcrumb {
                   1104:         push (@Crumbs,@_);
                   1105:     }
                   1106: 
1.57      matthew  1107: } # End of scope for @Crumbs
1.53      matthew  1108: 
                   1109: ############################################################
                   1110: ############################################################
                   1111: 
1.1       stredwic 1112: 
                   1113: 1;
1.23      matthew  1114: 
1.1       stredwic 1115: __END__

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