File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.89: download - view: text, annotated - select for diffs
Fri Sep 10 17:58:01 2004 UTC (19 years, 8 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Added remove_recent routine to remove deleted directories
	from list that populates recent dropdown.

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

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