File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.66: download - view: text, annotated - select for diffs
Mon Apr 19 16:43:03 2004 UTC (20 years, 1 month ago) by matthew
Branches: MAIN
CVS tags: HEAD
&breadcrumbs: Fixed logic in menu link output flag.

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

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