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

1.2       www         1: # The LearningOnline Network with CAPA
                      2: # a pile of common html routines
                      3: #
1.75    ! www         4: # $Id: lonhtmlcommon.pm,v 1.74 2004/06/04 16:05:47 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.52      www       872:     my $lang='en';
1.71      www       873:     if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
                    874: 	$lang=&mt('htmlarea_lang');
                    875:     }
1.52      www       876:     return (<<ENDHEADERS);
1.61      www       877: <script type="text/javascript">
1.73      www       878: _editor_url='/htmlarea/';
                    879: _editor_lang='$lang';
1.61      www       880: </script>
1.52      www       881: <script type="text/javascript" src="/htmlarea/htmlarea.js"></script>
                    882: ENDHEADERS
                    883: }
                    884: 
1.74      www       885: # ------------------------------------------------- Activate additional buttons
                    886: 
                    887: sub htmlareaaddbuttons {
                    888:     if (&htmlareablocked()) { return ''; }
                    889:     return (<<ENDADDBUTTON);
                    890:     var config=new HTMLArea.Config();
                    891:     config.registerButton('ed_math','LaTeX Inline',
                    892: 			  '/htmlarea/images/ed_math.gif',false,
                    893: 			    function(editor,id) {
                    894: 			      editor.surroundHTML('<m>\$','\$</m>');
                    895: 			    }
                    896: 			  );
                    897:     config.registerButton('ed_math_eqn','LaTeX Equation',
                    898: 			  '/htmlarea/images/ed_math_eqn.gif',false,
                    899: 			    function(editor,id) {
1.75    ! www       900: 			      editor.surroundHTML(
        !           901: 				     '<center><m>\\\\[','\\\\]</m></center>');
1.74      www       902: 			    }
                    903: 			  );
                    904:     config.toolbar.push(['ed_math','ed_math_eqn']);
                    905: ENDADDBUTTON
                    906: }
1.52      www       907: # ---------------------------------------------------------- Script to activate
                    908: 
                    909: sub htmlareaactive {
1.71      www       910:     if (&htmlareablocked()) { return ''; }
1.74      www       911:     my $addbuttons=&htmlareaaddbuttons();
1.52      www       912:     return (<<ENDSCRIPT);
                    913: <script type="text/javascript" defer="1">
1.74      www       914:     $addbuttons
                    915:     HTMLArea.replaceAll(config);
1.52      www       916: </script>
                    917: ENDSCRIPT
1.61      www       918: }
                    919: 
1.72      www       920: # ----------------------------------------- Script to activate only some fields
                    921: 
                    922: sub htmlareaselectactive {
1.73      www       923:     my @fields=@_;
1.72      www       924:     if (&htmlareablocked()) { return ''; }
1.74      www       925:     my $output='<script type="text/javascript" defer="1">'.
                    926: 	&htmlareaaddbuttons();
1.73      www       927:     foreach(@fields) {
1.74      www       928: 	$output.="\nHTMLArea.replace('$_',config);";
1.72      www       929:     }
1.73      www       930:     $output.="\nwindow.status='Activated Editfields';\n</script>";
1.72      www       931:     return $output;
                    932: }
                    933: 
1.61      www       934: # --------------------------------------------------------------------- Blocked
                    935: 
                    936: sub htmlareablocked {
1.71      www       937:     unless (&htmlareabrowser()) { return 1; }
                    938:     return 0;
1.52      www       939: }
                    940: 
                    941: # ---------------------------------------- Browser capable of running HTMLArea?
                    942: 
                    943: sub htmlareabrowser {
                    944:     return 1;
                    945: }
1.53      matthew   946: 
                    947: ############################################################
                    948: ############################################################
                    949: 
                    950: =pod
                    951: 
                    952: =item breadcrumbs
                    953: 
                    954: Compiles the previously registered breadcrumbs into an series of links.
                    955: FAQ and BUG links will be placed on the left side of the table if they
                    956: are defined for the last registered breadcrumb.  
                    957: Additionally supports a 'component', which will be displayed on the
                    958: right side of the table (without a link).
                    959: A link to help for the component will be included if one is specified.
                    960: 
                    961: All inputs can be undef without problems.
                    962: 
                    963: Inputs: $color (the background color of the table returned),
                    964:         $component (the large text on the right side of the table),
                    965:         $component_help
1.63      albertel  966:         $function (role to get colors from)
                    967:         $domain   (domian of role)
                    968:         $menulink (boolean, controls whether to include a link to /adm/menu)
1.53      matthew   969: 
                    970: Returns a string containing breadcrumbs for the current page.
                    971: 
                    972: =item clear_breadcrumbs
                    973: 
                    974: Clears the previously stored breadcrumbs.
                    975: 
                    976: =item add_breadcrumb
                    977: 
                    978: Pushes a breadcrumb on the stack of crumbs.
                    979: 
                    980: input: $breadcrumb, a hash reference.  The keys 'href','title', and 'text'
                    981: are required.  If present the keys 'faq' and 'bug' will be used to provide
                    982: links to the FAQ and bug sites.
                    983: 
                    984: returns: nothing    
                    985: 
                    986: =cut
                    987: 
                    988: ############################################################
                    989: ############################################################
                    990: {
                    991:     my @Crumbs;
1.57      matthew   992:     
1.53      matthew   993:     sub breadcrumbs {
1.63      albertel  994:         my ($color,$component,$component_help,$function,$domain,$menulink) =
                    995: 	    @_;
1.55      matthew   996:         if (! defined($color)) {
                    997:             if (! defined($function)) {
                    998:                 $function = &Apache::loncommon::get_users_function();
                    999:             }
                   1000:             $color = &Apache::loncommon::designparm($function.'.tabbg',
                   1001:                                                     $domain);
                   1002:         }
1.53      matthew  1003:         #
                   1004:         my $Str = "\n".
                   1005:             '<table width="100%" border="0" cellpadding="0" cellspacing="0">'.
                   1006:             '<tr><td bgcolor="'.$color.'">'.
                   1007:             '<font size="-1">';
1.57      matthew  1008:         #
                   1009:         # Make the faq and bug data cascade
                   1010:         my $faq = '';
                   1011:         my $bug = '';
1.60      www      1012:         # The last breadcrumb does not have a link, so handle it separately.
1.53      matthew  1013:         my $last = pop(@Crumbs);
1.57      matthew  1014:         #
1.70      matthew  1015:         # The first one should be the course or a menu link
1.63      albertel 1016: 	if (!defined($menulink)) { $menulink=1; }
1.70      matthew  1017:         if ($menulink) {
                   1018:             my $description = 'Menu';
                   1019:             if (exists($ENV{'request.course.id'}) && 
                   1020:                 $ENV{'request.course.id'} ne '') {
                   1021:                 $description = 
                   1022:                     $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
                   1023:             }
1.57      matthew  1024:             unshift(@Crumbs,{
1.70      matthew  1025:                     href   =>'/adm/menu',
                   1026:                     title  =>'Go to main menu',
                   1027:                     target =>'_top',
                   1028:                     text   =>$description,
                   1029:                 });
1.53      matthew  1030:         }
                   1031:         my $links .= 
                   1032:             join('-&gt;',
                   1033:                  map {
1.57      matthew  1034:                      $faq = $_->{'faq'} if (exists($_->{'faq'}));
                   1035:                      $bug = $_->{'bug'} if (exists($_->{'bug'}));
1.69      matthew  1036:                      my $result = '<a href="'.$_->{'href'}.'" ';
                   1037:                      if (defined($_->{'target'}) && $_->{'target'} ne '') {
                   1038:                          $result .= 'target="'.$_->{'target'}.'" ';
                   1039:                      }
                   1040:                      $result .='title="'.&mt($_->{'title'}).'">'.
                   1041:                          &mt($_->{'text'}).'</a>';
                   1042:                      $result;
1.53      matthew  1043:                      } @Crumbs
                   1044:                  );
                   1045:         $links .= '-&gt;' if ($links ne '');
                   1046:         $links .= '<b>'.$last->{'text'}.'</b>';
1.54      matthew  1047:         #
                   1048:         my $icons = '';
1.57      matthew  1049:         $faq = $last->{'faq'} if (exists($last->{'faq'}));
                   1050:         $bug = $last->{'bug'} if (exists($last->{'bug'}));
                   1051:         if ($faq ne '') {
                   1052:             $icons .= &Apache::loncommon::help_open_faq($faq);
1.54      matthew  1053:         }
1.57      matthew  1054:         if ($bug ne '') {
                   1055:             $icons .= &Apache::loncommon::help_open_bug($bug);
1.53      matthew  1056:         }
1.54      matthew  1057:         if ($icons ne '') {
                   1058:             $Str .= $icons.'&nbsp;';
1.53      matthew  1059:         }
1.54      matthew  1060:         #
1.53      matthew  1061:         $Str .= $links.'</font></td>';
1.54      matthew  1062:         #
1.53      matthew  1063:         if (defined($component)) {
                   1064:             $Str .= '<td align="right" bgcolor="'.$color.'">'.
1.58      www      1065:                 '<font size="+1">'.&mt($component).'</font>';
1.53      matthew  1066:             if (defined($component_help)) {
                   1067:                 $Str .= 
                   1068:                     &Apache::loncommon::help_open_topic($component_help);
                   1069:             }
                   1070:             $Str.= '</td>';
                   1071:         }
                   1072:         $Str .= '</tr></table>'."\n";
                   1073:         #
                   1074:         # Return the @Crumbs stack to what we started with
                   1075:         push(@Crumbs,$last);
                   1076:         shift(@Crumbs);
                   1077:         #
                   1078:         return $Str;
                   1079:     }
                   1080: 
                   1081:     sub clear_breadcrumbs {
                   1082:         undef(@Crumbs);
                   1083:     }
                   1084: 
                   1085:     sub add_breadcrumb {
                   1086:         push (@Crumbs,@_);
                   1087:     }
                   1088: 
1.57      matthew  1089: } # End of scope for @Crumbs
1.53      matthew  1090: 
                   1091: ############################################################
                   1092: ############################################################
                   1093: 
1.1       stredwic 1094: 
                   1095: 1;
1.23      matthew  1096: 
1.1       stredwic 1097: __END__

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