File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.79: download - view: text, annotated - select for diffs
Sat Jul 3 18:49:42 2004 UTC (19 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, HEAD
Integration of help icons in breadcrumb trail into a single icon. Click on icon to open new window with gateway to help options within frameset (or within main window if pop-ups blocked).  Help options include inline topic help, support request form, FAQ-o-matic, and bug reporting (all contextualized).  Option to collect form parameter information from page displaying help icon currently disabled.

Some work required:
lonsupportreq.pm - replace call to Mail::Send with more sophisticated CPAN module (e.g., Mail::Sender that allows specification of from: address and attachments.

    1: # The LearningOnline Network with CAPA
    2: # a pile of common html routines
    3: #
    4: # $Id: lonhtmlcommon.pm,v 1.79 2004/07/03 18:49:42 raeburn 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 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: 
  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 {
  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 .= ' />';
  175:     return $Str;
  176: }
  177: 
  178: ##############################################
  179: ##############################################
  180: 
  181: =pod
  182: 
  183: =item &date_setter
  184: 
  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: 
  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: 
  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: 
  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: 
  217: =back
  218: 
  219: Bugs
  220: 
  221: The method used to restrict user input will fail in the year 2400.
  222: 
  223: =cut
  224: 
  225: ##############################################
  226: ##############################################
  227: sub date_setter {
  228:     my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
  229:         $no_hh_mm_ss) = @_;
  230:     if (! defined($state) || $state ne 'disabled') {
  231:         $state = '';
  232:     }
  233:     if (! defined($no_hh_mm_ss)) {
  234:         $no_hh_mm_ss = 0;
  235:     }
  236:     if (! defined($currentvalue) || $currentvalue eq 'now') {
  237: 	unless ($includeempty) {
  238: 	    $currentvalue = time;
  239: 	} else {
  240: 	    $currentvalue = 0;
  241: 	}
  242:     }
  243:     # other potentially useful values:     wkday,yrday,is_daylight_savings
  244:     my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
  245:     if ($currentvalue) {
  246: 	($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
  247: 	    localtime($currentvalue);
  248: 	$year += 1900;
  249:     }
  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:     }
  283: 
  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: 
  302:     function $dname\_opencalendar() {
  303:         if (! document.$formname.$dname\_month.disabled) {
  304:             var calwin=window.open(
  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");
  310:         }
  311: 
  312:     }
  313: </script>
  314: ENDJS
  315:     $result .= "  <nobr><select name=\"$dname\_month\" ".$special.' '.
  316:         $state.' '.
  317:         "onChange=\"javascript:$dname\_checkday()\" >\n";
  318:     # Month
  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');
  323:     if ($includeempty) { $result.="<option value=''></option>"; }
  324:     for(my $m = 1;$m <=$#Months;$m++) {
  325:         $result .= "      <option value=\"$m\" ";
  326:         $result .= "selected " if ($m-1 eq $month);
  327:         $result .= "> ".&mt($Months[$m])." </option>\n";
  328:     }
  329:     $result .= "  </select>\n";
  330:     # Day
  331:     $result .= "  <input type=\"text\" name=\"$dname\_day\" ".$state.' '.
  332:             "value=\"$mday\" size=\"3\" ".$special.' '.
  333:             "onChange=\"javascript:$dname\_checkday()\" />\n";
  334:     # Year
  335:     $result .= "  <input type=\"year\" name=\"$dname\_year\" ".$state.' '.
  336:             "value=\"$year\" size=\"5\" ".$special.' '.
  337:             "onChange=\"javascript:$dname\_checkday()\" />\n";
  338:     $result .= "&nbsp;&nbsp;";
  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:     }
  371:     $result .= "<a href=\"javascript:$dname\_opencalendar()\">".
  372:     &mt('Select Date')."</a></nobr>\n<!-- end $dname date setting form -->\n";
  373:     return $result;
  374: }
  375: 
  376: ##############################################
  377: ##############################################
  378: 
  379: =pod
  380: 
  381: =item &get_date_from_form
  382: 
  383: get_date_from_form retrieves the date specified in an &date_setter form.
  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:         }
  414: 	if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
  415:     } else {
  416:         $sec = 0;
  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:         }
  423: 	if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
  424:     } else {
  425:         $min = 0;
  426:     }
  427:     if (defined($ENV{'form.'.$dname.'_hour'})) {
  428:         my $tmphour = $ENV{'form.'.$dname.'_hour'};
  429:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
  430:             $hour = $tmphour;
  431:         }
  432:     } else {
  433:         $hour = 0;
  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:     }
  453:     if (($year<70) || ($year>137)) { return undef; }
  454:     if (defined($sec) && defined($min)   && defined($hour) &&
  455:         defined($day) && defined($month) && defined($year) &&
  456:         eval(&timelocal($sec,$min,$hour,$day,$month,$year))) {
  457:         return &timelocal($sec,$min,$hour,$day,$month,$year);
  458:     } else {
  459:         return undef;
  460:     }
  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;
  488: }
  489: 
  490: ##############################################
  491: ##############################################
  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: 
  522: 
  523: ##############################################
  524: ##############################################
  525: 
  526: =pod
  527: 
  528: =item &StatusOptions()
  529: 
  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'.
  532: 
  533: Inputs:
  534: 
  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.
  538: 
  539: $formname: The name of the form.  If defined the onchange attribute of
  540: the selection box is set to document.$formname.submit().
  541: 
  542: $size: the size (number of lines) of the selection box.
  543: 
  544: $onchange: javascript to use when the value is changed.  Enclosed in 
  545: double quotes, ""s, not single quotes.
  546: 
  547: Returns: a perl string as described.
  548: 
  549: =cut
  550: 
  551: ##############################################
  552: ##############################################
  553: sub StatusOptions {
  554:     my ($status, $formName,$size,$onchange)=@_;
  555:     $size = 1 if (!defined($size));
  556:     if (! defined($status)) {
  557:         $status = 'Active';
  558:         $status = $ENV{'form.Status'} if (exists($ENV{'form.Status'}));
  559:     }
  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"';
  571:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
  572:         $Str .= ' onchange="document.'.$formName.'.submit()"';
  573:     }
  574:     if (defined($onchange)) {
  575:         $Str .= ' onchange="'.$onchange.'"';
  576:     }
  577:     $Str .= ' size="'.$size.'" ';
  578:     $Str .= '>'."\n";
  579:     $Str .= '<option value="Active" '.$OpSel1.'>'.
  580:         &mt('Currently Enrolled').'</option>'."\n";
  581:     $Str .= '<option value="Expired" '.$OpSel2.'>'.
  582:         &mt('Previously Enrolled').'</option>'."\n";
  583:     $Str .= '<option value="Any" '.$OpSel3.'>'.
  584:         &mt('Any Enrollment Status').'</option>'."\n";
  585:     $Str .= '</select>'."\n";
  586: }
  587: 
  588: ########################################################
  589: ########################################################
  590: 
  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.
  618: 
  619: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
  620:        specified)
  621: 
  622: =item $width Specify the width in charaters of the input field.
  623: 
  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 
  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: 
  696: my $uniq=0;
  697: sub get_uniq_name {
  698:     $uniq++;
  699:     return 'uniquename'.$uniq;
  700: }
  701: 
  702: # Create progress
  703: sub Create_PrgWin {
  704:     my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
  705: 	$inputname)=@_;
  706:     if (!defined($type)) { $type='popup'; }
  707:     if (!defined($width)) { $width=55; }
  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>".
  719: 	      "<body bgcolor=\"#88DDFF\">".
  720:               "<h4>$heading</h4>".
  721:               "<form name=popremain>".
  722:               '<input type="text" size="'.$width.'" name="remaining" value="'.
  723: 	      &mt('Starting').'"></form>'.
  724:               "</body></html>\');".
  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) {
  732: 	    $prog_state{'formname'}=&get_uniq_name();
  733: 	    &r_print($r,'<form name="'.$prog_state{'formname'}.'">');
  734: 	} else {
  735: 	    $prog_state{'formname'}=$formname;
  736: 	}
  737: 	if (!$inputname) {
  738: 	    $prog_state{'inputname'}=&get_uniq_name();
  739: 	    &r_print($r,$heading.' <input type="text" name="'.$prog_state{'inputname'}.
  740: 		     '" size="'.$width.'" />');
  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:     }
  748: 
  749:     $prog_state{'done'}=0;
  750:     $prog_state{'firststart'}=&Time::HiRes::time();
  751:     $prog_state{'laststart'}=&Time::HiRes::time();
  752:     $prog_state{'max'}=$number_to_do;
  753:     
  754:     return %prog_state;
  755: }
  756: 
  757: # update progress
  758: sub Update_PrgWin {
  759:     my ($r,$prog_state,$displayString)=@_;
  760:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
  761: 	     $$prog_state{'formname'}.'.'.
  762: 	     $$prog_state{'inputname'}.'.value="'.
  763: 	     $displayString.'";</script>');
  764:     $$prog_state{'laststart'}=&Time::HiRes::time();
  765: }
  766: 
  767: # increment progress state
  768: sub Increment_PrgWin {
  769:     my ($r,$prog_state,$extraInfo)=@_;
  770:     $$prog_state{'done'}++;
  771:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
  772:         $$prog_state{'done'} *
  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;
  778: 	$time_est = $min.' '.&mt('minutes');
  779:         if ($min < 10)  {
  780:             if ($sec > 1) {
  781:                 $time_est.= ', '.$sec.' '.&mt('seconds');
  782:             } elsif ($sec > 0) {
  783:                 $time_est.= ', '.$sec.' '.&mt('second');
  784:             }
  785:         }
  786:     } else {
  787: 	$time_est .= ' '.&mt('seconds');
  788:     }
  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:     }
  797:     if ($lasttime == 1) {
  798:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
  799:     } else {
  800:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
  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 = '';
  811:     }
  812:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
  813: 	     $$prog_state{'formname'}.'.'.
  814: 	     $$prog_state{'inputname'}.'.value="'.
  815: 	     $$prog_state{'done'}.'/'.$$prog_state{'max'}.
  816: 	     ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
  817:     $$prog_state{'laststart'}=&Time::HiRes::time();
  818: }
  819: 
  820: # close Progress Line
  821: sub Close_PrgWin {
  822:     my ($r,$prog_state)=@_;
  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:     }
  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();
  836:     } else {
  837: 	print($to_print);
  838:     }
  839: }
  840: 
  841: # ------------------------------------------------------- Puts directory header
  842: 
  843: sub crumbs {
  844:     my ($uri,$target,$prefix,$form,$size,$noformat)=@_;
  845:     if (! defined($size)) {
  846:         $size = '+2';
  847:     }
  848:     my $output='';
  849:     unless ($noformat) { $output.='<br /><tt><b>'; }
  850:     $output.='<font size="'.$size.'">'.$prefix.'/';
  851:     if ($ENV{'user.adv'}) {
  852: 	my $path=$prefix.'/';
  853: 	foreach (split('/',$uri)) {
  854: 	    unless ($_) { next; }
  855: 	    $path.=$_;
  856: 	    unless ($path eq $uri) { $path.='/'; }
  857: 	    my $linkpath=$path;
  858: 	    if ($form) {
  859: 		$linkpath="javascript:$form.action='$path';$form.submit();";
  860: 	    }
  861: 	    $output.='<a href="'.$linkpath.'"'.($target?' target="'.$target.'"':'').'>'.$_.'</a>/';
  862: 	}
  863:     } else {
  864: 	$output.=$uri;
  865:     }
  866:     unless ($uri=~/\/$/) { $output=~s/\/$//; }
  867:     return $output.'</font>'.($noformat?'':'</b></tt><br />');
  868: }
  869: 
  870: # ------------------------------------------------- Output headers for HTMLArea
  871: 
  872: sub htmlareaheaders {
  873:     if (&htmlareablocked()) { return ''; }
  874:     unless (&htmlareabrowser()) { return ''; }
  875:     my $lang='en';
  876:     if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
  877: 	$lang=&mt('htmlarea_lang');
  878:     }
  879:     return (<<ENDHEADERS);
  880: <script type="text/javascript">
  881: _editor_url='/htmlarea/';
  882: _editor_lang='$lang';
  883: </script>
  884: <script type="text/javascript" src="/htmlarea/htmlarea.js"></script>
  885: ENDHEADERS
  886: }
  887: 
  888: # ------------------------------------------------- Activate additional buttons
  889: 
  890: sub htmlareaaddbuttons {
  891:     if (&htmlareablocked()) { return ''; }
  892:     unless (&htmlareabrowser()) { return ''; }
  893:     return (<<ENDADDBUTTON);
  894:     var config=new HTMLArea.Config();
  895:     config.registerButton('ed_math','LaTeX Inline',
  896: 			  '/htmlarea/images/ed_math.gif',false,
  897: 			    function(editor,id) {
  898: 			      editor.surroundHTML('<m>\$','\$</m>');
  899: 			    }
  900: 			  );
  901:     config.registerButton('ed_math_eqn','LaTeX Equation',
  902: 			  '/htmlarea/images/ed_math_eqn.gif',false,
  903: 			    function(editor,id) {
  904: 			      editor.surroundHTML(
  905: 				     '<center><m>\\\\[','\\\\]</m></center>');
  906: 			    }
  907: 			  );
  908:     config.toolbar.push(['ed_math','ed_math_eqn']);
  909: ENDADDBUTTON
  910: }
  911: 
  912: # ----------------------------------------------------------------- Preferences
  913: 
  914: sub disablelink {
  915:     my @fields=@_;
  916:     if (defined($#fields)) {
  917: 	unless ($#fields>=0) { return ''; }
  918:     }
  919:     return '<a href="/adm/preferences?action=set_wysiwyg&wysiwyg=off&returnurl='.&Apache::lonnet::escape($ENV{'REQUEST_URI'}).'">'.&mt('Disable WYSIWYG Editor').'</a>';
  920: }
  921: 
  922: sub enablelink {
  923:     my @fields=@_;
  924:     if (defined($#fields)) {
  925: 	unless ($#fields>=0) { return ''; }
  926:     }
  927:     return '<a href="/adm/preferences?action=set_wysiwyg&wysiwyg=on&returnurl='.&Apache::lonnet::escape($ENV{'REQUEST_URI'}).'">'.&mt('Enable WYSIWYG Editor').'</a>';
  928: }
  929: 
  930: # ----------------------------------------- Script to activate only some fields
  931: 
  932: sub htmlareaselectactive {
  933:     my @fields=@_;
  934:     unless (&htmlareabrowser()) { return ''; }
  935:     if (&htmlareablocked()) { return '<br />'.&enablelink(@fields); }
  936:     my $output='<script type="text/javascript" defer="1">'.
  937: 	&htmlareaaddbuttons();
  938:     foreach(@fields) {
  939: 	$output.="\nHTMLArea.replace('$_',config);";
  940:     }
  941:     $output.="\nwindow.status='Activated Editfields';\n</script><br />".
  942: 	&disablelink(@fields);
  943:     return $output;
  944: }
  945: 
  946: # --------------------------------------------------------------------- Blocked
  947: 
  948: sub htmlareablocked {
  949:     unless ($ENV{'environment.wysiwygeditor'} eq 'on') { return 1; }
  950:     return 0;
  951: }
  952: 
  953: # ---------------------------------------- Browser capable of running HTMLArea?
  954: 
  955: sub htmlareabrowser {
  956:     return 1;
  957: }
  958: 
  959: ############################################################
  960: ############################################################
  961: 
  962: =pod
  963: 
  964: =item breadcrumbs
  965: 
  966: Compiles the previously registered breadcrumbs into an series of links.
  967: FAQ and BUG links will be placed on the left side of the table if they
  968: are defined for the last registered breadcrumb.  
  969: Additionally supports a 'component', which will be displayed on the
  970: right side of the table (without a link).
  971: A link to help for the component will be included if one is specified.
  972: 
  973: All inputs can be undef without problems.
  974: 
  975: Inputs: $color (the background color of the table returned),
  976:         $component (the large text on the right side of the table),
  977:         $component_help
  978:         $function (role to get colors from)
  979:         $domain   (domian of role)
  980:         $menulink (boolean, controls whether to include a link to /adm/menu)
  981: 
  982: Returns a string containing breadcrumbs for the current page.
  983: 
  984: =item clear_breadcrumbs
  985: 
  986: Clears the previously stored breadcrumbs.
  987: 
  988: =item add_breadcrumb
  989: 
  990: Pushes a breadcrumb on the stack of crumbs.
  991: 
  992: input: $breadcrumb, a hash reference.  The keys 'href','title', and 'text'
  993: are required.  If present the keys 'faq' and 'bug' will be used to provide
  994: links to the FAQ and bug sites.
  995: 
  996: returns: nothing    
  997: 
  998: =cut
  999: 
 1000: ############################################################
 1001: ############################################################
 1002: {
 1003:     my @Crumbs;
 1004:     
 1005:     sub breadcrumbs {
 1006:         my ($color,$component,$component_help,$function,$domain,$menulink) =
 1007: 	    @_;
 1008:         if (! defined($color)) {
 1009:             if (! defined($function)) {
 1010:                 $function = &Apache::loncommon::get_users_function();
 1011:             }
 1012:             $color = &Apache::loncommon::designparm($function.'.tabbg',
 1013:                                                     $domain);
 1014:         }
 1015:         #
 1016:         my $Str = "\n".
 1017:             '<table width="100%" border="0" cellpadding="0" cellspacing="0">'.
 1018:             '<tr><td bgcolor="'.$color.'">'.
 1019:             '<font size="-1">';
 1020:         #
 1021:         # Make the faq and bug data cascade
 1022:         my $faq = '';
 1023:         my $bug = '';
 1024:         # The last breadcrumb does not have a link, so handle it separately.
 1025:         my $last = pop(@Crumbs);
 1026:         #
 1027:         # The first one should be the course or a menu link
 1028: 	if (!defined($menulink)) { $menulink=1; }
 1029:         if ($menulink) {
 1030:             my $description = 'Menu';
 1031:             if (exists($ENV{'request.course.id'}) && 
 1032:                 $ENV{'request.course.id'} ne '') {
 1033:                 $description = 
 1034:                     $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
 1035:             }
 1036:             unshift(@Crumbs,{
 1037:                     href   =>'/adm/menu',
 1038:                     title  =>'Go to main menu',
 1039:                     target =>'_top',
 1040:                     text   =>$description,
 1041:                 });
 1042:         }
 1043:         my $links .= 
 1044:             join('-&gt;',
 1045:                  map {
 1046:                      $faq = $_->{'faq'} if (exists($_->{'faq'}));
 1047:                      $bug = $_->{'bug'} if (exists($_->{'bug'}));
 1048:                      my $result = '<a href="'.$_->{'href'}.'" ';
 1049:                      if (defined($_->{'target'}) && $_->{'target'} ne '') {
 1050:                          $result .= 'target="'.$_->{'target'}.'" ';
 1051:                      }
 1052:                      $result .='title="'.&mt($_->{'title'}).'">'.
 1053:                          &mt($_->{'text'}).'</a>';
 1054:                      $result;
 1055:                      } @Crumbs
 1056:                  );
 1057:         $links .= '-&gt;' if ($links ne '');
 1058:         $links .= '<b>'.$last->{'text'}.'</b>';
 1059:         #
 1060:         my $icons = '';
 1061:         $faq = $last->{'faq'} if (exists($last->{'faq'}));
 1062:         $bug = $last->{'bug'} if (exists($last->{'bug'}));
 1063: #        if ($faq ne '') {
 1064: #            $icons .= &Apache::loncommon::help_open_faq($faq);
 1065: #        }
 1066: #        if ($bug ne '') {
 1067: #            $icons .= &Apache::loncommon::help_open_bug($bug);
 1068: #        }
 1069:         $icons .= &Apache::loncommon::help_open_menu($color,$component,$component_help,$function,$faq,$bug);
 1070:         if ($icons ne '') {
 1071:             $Str .= $icons.'&nbsp;';
 1072:         }
 1073:         #
 1074:         $Str .= $links.'</font></td>';
 1075:         #
 1076:         if (defined($component)) {
 1077:             $Str .= '<td align="right" bgcolor="'.$color.'">'.
 1078:                 '<font size="+1">'.&mt($component).'</font>';
 1079:             if (defined($component_help)) {
 1080:                 $Str .= 
 1081:                     &Apache::loncommon::help_open_topic($component_help);
 1082:             }
 1083:             $Str.= '</td>';
 1084:         }
 1085:         $Str .= '</tr></table>'."\n";
 1086:         #
 1087:         # Return the @Crumbs stack to what we started with
 1088:         push(@Crumbs,$last);
 1089:         shift(@Crumbs);
 1090:         #
 1091:         return $Str;
 1092:     }
 1093: 
 1094:     sub clear_breadcrumbs {
 1095:         undef(@Crumbs);
 1096:     }
 1097: 
 1098:     sub add_breadcrumb {
 1099:         push (@Crumbs,@_);
 1100:     }
 1101: 
 1102: } # End of scope for @Crumbs
 1103: 
 1104: ############################################################
 1105: ############################################################
 1106: 
 1107: 
 1108: 1;
 1109: 
 1110: __END__

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