Annotation of loncom/interface/loncommon.pm, revision 1.410

1.10      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # a pile of common routines
1.10      albertel    3: #
1.410   ! albertel    4: # $Id: loncommon.pm,v 1.409 2006/06/30 00:47:14 albertel Exp $
1.10      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.1       albertel   28: 
                     29: # Makes a table out of the previous attempts
1.2       albertel   30: # Inputs result_from_symbread, user, domain, course_id
1.16      harris41   31: # Reads in non-network-related .tab files
1.1       albertel   32: 
1.35      matthew    33: # POD header:
                     34: 
1.45      matthew    35: =pod
                     36: 
1.35      matthew    37: =head1 NAME
                     38: 
                     39: Apache::loncommon - pile of common routines
                     40: 
                     41: =head1 SYNOPSIS
                     42: 
1.112     bowersj2   43: Common routines for manipulating connections, student answers,
                     44:     domains, common Javascript fragments, etc.
1.35      matthew    45: 
1.112     bowersj2   46: =head1 OVERVIEW
1.35      matthew    47: 
1.112     bowersj2   48: A collection of commonly used subroutines that don't have a natural
                     49: home anywhere else. This collection helps remove
1.35      matthew    50: redundancy from other modules and increase efficiency of memory usage.
                     51: 
                     52: =cut 
                     53: 
                     54: # End of POD header
1.1       albertel   55: package Apache::loncommon;
                     56: 
                     57: use strict;
1.258     albertel   58: use Apache::lonnet;
1.46      matthew    59: use GDBM_File;
1.51      www        60: use POSIX qw(strftime mktime);
1.82      www        61: use Apache::lonmenu();
1.117     www        62: use Apache::lonlocal;
1.139     matthew    63: use HTML::Entities;
1.334     albertel   64: use Apache::lonhtmlcommon();
                     65: use Apache::loncoursedata();
1.344     albertel   66: use Apache::lontexconvert();
1.369     www        67: use LONCAPA;
1.117     www        68: 
1.22      www        69: my $readit;
                     70: 
1.157     matthew    71: ##
                     72: ## Global Variables
                     73: ##
1.46      matthew    74: 
1.20      www        75: # ----------------------------------------------- Filetypes/Languages/Copyright
1.12      harris41   76: my %language;
1.124     www        77: my %supported_language;
1.12      harris41   78: my %cprtag;
1.192     taceyjo1   79: my %scprtag;
1.351     www        80: my %fe; my %fd; my %fm;
1.41      ng         81: my %category_extensions;
1.12      harris41   82: 
1.63      www        83: # ---------------------------------------------- Designs
                     84: 
                     85: my %designhash;
                     86: 
1.46      matthew    87: # ---------------------------------------------- Thesaurus variables
1.144     matthew    88: #
                     89: # %Keywords:
                     90: #      A hash used by &keyword to determine if a word is considered a keyword.
                     91: # $thesaurus_db_file 
                     92: #      Scalar containing the full path to the thesaurus database.
1.46      matthew    93: 
                     94: my %Keywords;
                     95: my $thesaurus_db_file;
                     96: 
1.144     matthew    97: #
                     98: # Initialize values from language.tab, copyright.tab, filetypes.tab,
                     99: # thesaurus.tab, and filecategories.tab.
                    100: #
1.18      www       101: BEGIN {
1.46      matthew   102:     # Variable initialization
                    103:     $thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
                    104:     #
1.22      www       105:     unless ($readit) {
1.12      harris41  106: # ------------------------------------------------------------------- languages
                    107:     {
1.158     raeburn   108:         my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    109:                                    '/language.tab';
                    110:         if ( open(my $fh,"<$langtabfile") ) {
1.356     albertel  111:             while (my $line = <$fh>) {
                    112:                 next if ($line=~/^\#/);
                    113:                 chomp($line);
                    114:                 my ($key,$two,$country,$three,$enc,$val,$sup)=(split(/\t/,$line));
1.158     raeburn   115:                 $language{$key}=$val.' - '.$enc;
                    116:                 if ($sup) {
                    117:                     $supported_language{$key}=$sup;
                    118:                 }
                    119:             }
                    120:             close($fh);
                    121:         }
1.12      harris41  122:     }
                    123: # ------------------------------------------------------------------ copyrights
                    124:     {
1.158     raeburn   125:         my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    126:                                   '/copyright.tab';
                    127:         if ( open (my $fh,"<$copyrightfile") ) {
1.356     albertel  128:             while (my $line = <$fh>) {
                    129:                 next if ($line=~/^\#/);
                    130:                 chomp($line);
                    131:                 my ($key,$val)=(split(/\s+/,$line,2));
1.158     raeburn   132:                 $cprtag{$key}=$val;
                    133:             }
                    134:             close($fh);
                    135:         }
1.12      harris41  136:     }
1.351     www       137: # ----------------------------------------------------------- source copyrights
1.192     taceyjo1  138:     {
                    139:         my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    140:                                   '/source_copyright.tab';
                    141:         if ( open (my $fh,"<$sourcecopyrightfile") ) {
1.356     albertel  142:             while (my $line = <$fh>) {
                    143:                 next if ($line =~ /^\#/);
                    144:                 chomp($line);
                    145:                 my ($key,$val)=(split(/\s+/,$line,2));
1.192     taceyjo1  146:                 $scprtag{$key}=$val;
                    147:             }
                    148:             close($fh);
                    149:         }
                    150:     }
1.63      www       151: 
                    152: # -------------------------------------------------------------- domain designs
                    153: 
                    154:     my $filename;
                    155:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
                    156:     opendir(DIR,$designdir);
                    157:     while ($filename=readdir(DIR)) {
1.271     albertel  158: 	if ($filename!~/\.tab$/) { next; }
1.63      www       159: 	my ($domain)=($filename=~/^(\w+)\./);
1.271     albertel  160: 	{
                    161: 	    my $designfile = $designdir.'/'.$filename;
                    162: 	    if ( open (my $fh,"<$designfile") ) {
1.356     albertel  163: 		while (my $line = <$fh>) {
                    164: 		    next if ($line =~ /^\#/);
                    165: 		    chomp($line);
                    166: 		    my ($key,$val)=(split(/\=/,$line));
1.271     albertel  167: 		    if ($val) { $designhash{$domain.'.'.$key}=$val; }
                    168: 		}
                    169: 		close($fh);
                    170: 	    }
                    171: 	}
1.63      www       172: 
                    173:     }
                    174:     closedir(DIR);
                    175: 
                    176: 
1.15      harris41  177: # ------------------------------------------------------------- file categories
                    178:     {
1.158     raeburn   179:         my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    180:                                   '/filecategories.tab';
                    181:         if ( open (my $fh,"<$categoryfile") ) {
1.356     albertel  182: 	    while (my $line = <$fh>) {
                    183: 		next if ($line =~ /^\#/);
                    184: 		chomp($line);
                    185:                 my ($extension,$category)=(split(/\s+/,$line,2));
1.158     raeburn   186:                 push @{$category_extensions{lc($category)}},$extension;
                    187:             }
                    188:             close($fh);
                    189:         }
                    190: 
1.15      harris41  191:     }
1.12      harris41  192: # ------------------------------------------------------------------ file types
                    193:     {
1.158     raeburn   194:         my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    195:                '/filetypes.tab';
                    196:         if ( open (my $fh,"<$typesfile") ) {
1.356     albertel  197:             while (my $line = <$fh>) {
                    198: 		next if ($line =~ /^\#/);
                    199: 		chomp($line);
                    200:                 my ($ending,$emb,$mime,$descr)=split(/\s+/,$line,4);
1.158     raeburn   201:                 if ($descr ne '') {
                    202:                     $fe{$ending}=lc($emb);
                    203:                     $fd{$ending}=$descr;
1.351     www       204:                     if ($mime ne 'unk') { $fm{$ending}=$mime; }
1.158     raeburn   205:                 }
                    206:             }
                    207:             close($fh);
                    208:         }
1.12      harris41  209:     }
1.22      www       210:     &Apache::lonnet::logthis(
1.46      matthew   211:               "<font color=yellow>INFO: Read file types</font>");
1.22      www       212:     $readit=1;
1.46      matthew   213:     }  # end of unless($readit) 
1.32      matthew   214:     
                    215: }
1.112     bowersj2  216: 
1.42      matthew   217: ###############################################################
                    218: ##           HTML and Javascript Helper Functions            ##
                    219: ###############################################################
                    220: 
                    221: =pod 
                    222: 
1.112     bowersj2  223: =head1 HTML and Javascript Functions
1.42      matthew   224: 
1.112     bowersj2  225: =over 4
                    226: 
                    227: =item * browser_and_searcher_javascript ()
                    228: 
                    229: X<browsing, javascript>X<searching, javascript>Returns a string
                    230: containing javascript with two functions, C<openbrowser> and
                    231: C<opensearcher>. Returned string does not contain E<lt>scriptE<gt>
                    232: tags.
1.42      matthew   233: 
1.112     bowersj2  234: =item * openbrowser(formname,elementname,only,omit) [javascript]
1.42      matthew   235: 
                    236: inputs: formname, elementname, only, omit
                    237: 
                    238: formname and elementname indicate the name of the html form and name of
                    239: the element that the results of the browsing selection are to be placed in. 
                    240: 
                    241: Specifying 'only' will restrict the browser to displaying only files
1.185     www       242: with the given extension.  Can be a comma separated list.
1.42      matthew   243: 
                    244: Specifying 'omit' will restrict the browser to NOT displaying files
1.185     www       245: with the given extension.  Can be a comma separated list.
1.42      matthew   246: 
1.112     bowersj2  247: =item * opensearcher(formname, elementname) [javascript]
1.42      matthew   248: 
                    249: Inputs: formname, elementname
                    250: 
                    251: formname and elementname specify the name of the html form and the name
                    252: of the element the selection from the search results will be placed in.
                    253: 
                    254: =cut
                    255: 
                    256: sub browser_and_searcher_javascript {
1.199     albertel  257:     my ($mode)=@_;
                    258:     if (!defined($mode)) { $mode='edit'; }
1.170     www       259:     my $resurl=&lastresurl();
1.42      matthew   260:     return <<END;
1.219     albertel  261: // <!-- BEGIN LON-CAPA Internal
1.50      matthew   262:     var editbrowser = null;
1.135     albertel  263:     function openbrowser(formname,elementname,only,omit,titleelement) {
1.170     www       264:         var url = '$resurl/?';
1.42      matthew   265:         if (editbrowser == null) {
                    266:             url += 'launch=1&';
                    267:         }
                    268:         url += 'catalogmode=interactive&';
1.199     albertel  269:         url += 'mode=$mode&';
1.42      matthew   270:         url += 'form=' + formname + '&';
                    271:         if (only != null) {
                    272:             url += 'only=' + only + '&';
1.217     albertel  273:         } else {
                    274:             url += 'only=&';
                    275: 	}
1.42      matthew   276:         if (omit != null) {
                    277:             url += 'omit=' + omit + '&';
1.217     albertel  278:         } else {
                    279:             url += 'omit=&';
                    280: 	}
1.135     albertel  281:         if (titleelement != null) {
                    282:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  283:         } else {
                    284: 	    url += 'titleelement=&';
                    285: 	}
1.42      matthew   286:         url += 'element=' + elementname + '';
                    287:         var title = 'Browser';
1.217     albertel  288:         var options = 'scrollbars=1,resizable=1,menubar=1,location=1';
1.42      matthew   289:         options += ',width=700,height=600';
                    290:         editbrowser = open(url,title,options,'1');
                    291:         editbrowser.focus();
                    292:     }
                    293:     var editsearcher;
1.135     albertel  294:     function opensearcher(formname,elementname,titleelement) {
1.42      matthew   295:         var url = '/adm/searchcat?';
                    296:         if (editsearcher == null) {
                    297:             url += 'launch=1&';
                    298:         }
                    299:         url += 'catalogmode=interactive&';
1.199     albertel  300:         url += 'mode=$mode&';
1.42      matthew   301:         url += 'form=' + formname + '&';
1.135     albertel  302:         if (titleelement != null) {
                    303:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  304:         } else {
                    305: 	    url += 'titleelement=&';
                    306: 	}
1.42      matthew   307:         url += 'element=' + elementname + '';
                    308:         var title = 'Search';
                    309:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    310:         options += ',width=700,height=600';
                    311:         editsearcher = open(url,title,options,'1');
                    312:         editsearcher.focus();
                    313:     }
1.219     albertel  314: // END LON-CAPA Internal -->
1.42      matthew   315: END
1.170     www       316: }
                    317: 
                    318: sub lastresurl {
1.258     albertel  319:     if ($env{'environment.lastresurl'}) {
                    320: 	return $env{'environment.lastresurl'}
1.170     www       321:     } else {
                    322: 	return '/res';
                    323:     }
                    324: }
                    325: 
                    326: sub storeresurl {
                    327:     my $resurl=&Apache::lonnet::clutter(shift);
                    328:     unless ($resurl=~/^\/res/) { return 0; }
                    329:     $resurl=~s/\/$//;
                    330:     &Apache::lonnet::put('environment',{'lastresurl' => $resurl});
                    331:     &Apache::lonnet::appenv('environment.lastresurl' => $resurl);
                    332:     return 1;
1.42      matthew   333: }
                    334: 
1.74      www       335: sub studentbrowser_javascript {
1.111     www       336:    unless (
1.258     albertel  337:             (($env{'request.course.id'}) && 
1.302     albertel  338:              (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    339: 	      || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    340: 					  '/'.$env{'request.course.sec'})
                    341: 	      ))
1.258     albertel  342:          || ($env{'request.role'}=~/^(au|dc|su)/)
1.111     www       343:           ) { return ''; }  
1.74      www       344:    return (<<'ENDSTDBRW');
                    345: <script type="text/javascript" language="Javascript" >
                    346:     var stdeditbrowser;
1.111     www       347:     function openstdbrowser(formname,uname,udom,roleflag) {
1.74      www       348:         var url = '/adm/pickstudent?';
                    349:         var filter;
                    350:         eval('filter=document.'+formname+'.'+uname+'.value;');
                    351:         if (filter != null) {
                    352:            if (filter != '') {
                    353:                url += 'filter='+filter+'&';
                    354: 	   }
                    355:         }
                    356:         url += 'form=' + formname + '&unameelement='+uname+
                    357:                                     '&udomelement='+udom;
1.111     www       358: 	if (roleflag) { url+="&roles=1"; }
1.102     www       359:         var title = 'Student_Browser';
1.74      www       360:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    361:         options += ',width=700,height=600';
                    362:         stdeditbrowser = open(url,title,options,'1');
                    363:         stdeditbrowser.focus();
                    364:     }
                    365: </script>
                    366: ENDSTDBRW
                    367: }
1.42      matthew   368: 
1.74      www       369: sub selectstudent_link {
1.111     www       370:    my ($form,$unameele,$udomele)=@_;
1.258     albertel  371:    if ($env{'request.course.id'}) {  
1.302     albertel  372:        if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    373: 	   && !&Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    374: 					'/'.$env{'request.course.sec'})) {
1.111     www       375: 	   return '';
                    376:        }
                    377:        return "<a href='".'javascript:openstdbrowser("'.$form.'","'.$unameele.
1.119     www       378:         '","'.$udomele.'");'."'>".&mt('Select User')."</a>";
1.74      www       379:    }
1.258     albertel  380:    if ($env{'request.role'}=~/^(au|dc|su)/) {
1.111     www       381:        return "<a href='".'javascript:openstdbrowser("'.$form.'","'.$unameele.
1.119     www       382:         '","'.$udomele.'",1);'."'>".&mt('Select User')."</a>";
1.111     www       383:    }
                    384:    return '';
1.91      www       385: }
                    386: 
                    387: sub coursebrowser_javascript {
1.234     raeburn   388:     my ($domainfilter)=@_;
1.377     raeburn   389:     my $crs_or_grp_alert = &mt('Please select the type of LON-CAPA entity - Course or Group - for which you wish to add/modify a user role');
1.128     albertel  390:    return (<<ENDSTDBRW);
1.91      www       391: <script type="text/javascript" language="Javascript" >
                    392:     var stdeditbrowser;
1.377     raeburn   393:     function opencrsbrowser(formname,uname,udom,desc,extra_element,multflag,crstype) {
1.91      www       394:         var url = '/adm/pickcourse?';
                    395:         var filter;
                    396:         if (filter != null) {
                    397:            if (filter != '') {
                    398:                url += 'filter='+filter+'&';
                    399: 	   }
                    400:         }
1.128     albertel  401:         var domainfilter='$domainfilter';
                    402:         if (domainfilter != null) {
                    403:            if (domainfilter != '') {
                    404:                url += 'domainfilter='+domainfilter+'&';
                    405: 	   }
                    406:         }
1.91      www       407:         url += 'form=' + formname + '&cnumelement='+uname+
1.187     albertel  408: 	                            '&cdomelement='+udom+
                    409:                                     '&cnameelement='+desc;
1.234     raeburn   410:         if (extra_element !=null && extra_element != '' && formname == 'rolechoice') {
                    411:             url += '&roleelement='+extra_element;
                    412:             if (domainfilter == null || domainfilter == '') {
                    413:                 url += '&domainfilter='+extra_element;
                    414:             }
1.230     raeburn   415:         }
1.293     raeburn   416:         if (multflag !=null && multflag != '') {
                    417:             url += '&multiple='+multflag;
                    418:         }
1.377     raeburn   419:         if (crstype == 'Course/Group') {
                    420:             if (formname == 'cu') {
                    421:                 crstype = document.cu.crstype.options[document.cu.crstype.selectedIndex].value; 
                    422:                 if (crstype == "") {
                    423:                     alert("$crs_or_grp_alert");
                    424:                     return;
                    425:                 }
                    426:             }
                    427:         }
                    428:         if (crstype !=null && crstype != '') {
                    429:             url += '&type='+crstype;
                    430:         }
1.102     www       431:         var title = 'Course_Browser';
1.91      www       432:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    433:         options += ',width=700,height=600';
                    434:         stdeditbrowser = open(url,title,options,'1');
                    435:         stdeditbrowser.focus();
                    436:     }
                    437: </script>
                    438: ENDSTDBRW
                    439: }
                    440: 
                    441: sub selectcourse_link {
1.377     raeburn   442:    my ($form,$unameele,$udomele,$desc,$extra_element,$multflag,$selecttype)=@_;
1.91      www       443:     return "<a href='".'javascript:opencrsbrowser("'.$form.'","'.$unameele.
1.377     raeburn   444:         '","'.$udomele.'","'.$desc.'","'.$extra_element.'","'.$multflag.'","'.$selecttype.'");'."'>".&mt('Select [_1]',$selecttype)."</a>";
1.74      www       445: }
1.42      matthew   446: 
1.273     raeburn   447: sub check_uncheck_jscript {
                    448:     my $jscript = <<"ENDSCRT";
                    449: function checkAll(field) {
                    450:     if (field.length > 0) {
                    451:         for (i = 0; i < field.length; i++) {
                    452:             field[i].checked = true ;
                    453:         }
                    454:     } else {
                    455:         field.checked = true
                    456:     }
                    457: }
                    458:  
                    459: function uncheckAll(field) {
                    460:     if (field.length > 0) {
                    461:         for (i = 0; i < field.length; i++) {
                    462:             field[i].checked = false ;
                    463:         }     } else {
                    464:         field.checked = false ;
                    465:     }
                    466: }
                    467: ENDSCRT
                    468:     return $jscript;
                    469: }
                    470: 
                    471: 
1.42      matthew   472: =pod
1.36      matthew   473: 
1.112     bowersj2  474: =item * linked_select_forms(...)
1.36      matthew   475: 
                    476: linked_select_forms returns a string containing a <script></script> block
                    477: and html for two <select> menus.  The select menus will be linked in that
                    478: changing the value of the first menu will result in new values being placed
                    479: in the second menu.  The values in the select menu will appear in alphabetical
                    480: order.
                    481: 
                    482: linked_select_forms takes the following ordered inputs:
                    483: 
                    484: =over 4
                    485: 
1.112     bowersj2  486: =item * $formname, the name of the <form> tag
1.36      matthew   487: 
1.112     bowersj2  488: =item * $middletext, the text which appears between the <select> tags
1.36      matthew   489: 
1.112     bowersj2  490: =item * $firstdefault, the default value for the first menu
1.36      matthew   491: 
1.112     bowersj2  492: =item * $firstselectname, the name of the first <select> tag
1.36      matthew   493: 
1.112     bowersj2  494: =item * $secondselectname, the name of the second <select> tag
1.36      matthew   495: 
1.112     bowersj2  496: =item * $hashref, a reference to a hash containing the data for the menus.
1.36      matthew   497: 
1.41      ng        498: =back 
                    499: 
1.36      matthew   500: Below is an example of such a hash.  Only the 'text', 'default', and 
                    501: 'select2' keys must appear as stated.  keys(%menu) are the possible 
                    502: values for the first select menu.  The text that coincides with the 
1.41      ng        503: first menu value is given in $menu{$choice1}->{'text'}.  The values 
1.36      matthew   504: and text for the second menu are given in the hash pointed to by 
                    505: $menu{$choice1}->{'select2'}.  
                    506: 
1.112     bowersj2  507:  my %menu = ( A1 => { text =>"Choice A1" ,
                    508:                        default => "B3",
                    509:                        select2 => { 
                    510:                            B1 => "Choice B1",
                    511:                            B2 => "Choice B2",
                    512:                            B3 => "Choice B3",
                    513:                            B4 => "Choice B4"
                    514:                            }
                    515:                    },
                    516:                A2 => { text =>"Choice A2" ,
                    517:                        default => "C2",
                    518:                        select2 => { 
                    519:                            C1 => "Choice C1",
                    520:                            C2 => "Choice C2",
                    521:                            C3 => "Choice C3"
                    522:                            }
                    523:                    },
                    524:                A3 => { text =>"Choice A3" ,
                    525:                        default => "D6",
                    526:                        select2 => { 
                    527:                            D1 => "Choice D1",
                    528:                            D2 => "Choice D2",
                    529:                            D3 => "Choice D3",
                    530:                            D4 => "Choice D4",
                    531:                            D5 => "Choice D5",
                    532:                            D6 => "Choice D6",
                    533:                            D7 => "Choice D7"
                    534:                            }
                    535:                    }
                    536:                );
1.36      matthew   537: 
                    538: =cut
                    539: 
                    540: sub linked_select_forms {
                    541:     my ($formname,
                    542:         $middletext,
                    543:         $firstdefault,
                    544:         $firstselectname,
                    545:         $secondselectname, 
                    546:         $hashref
                    547:         ) = @_;
                    548:     my $second = "document.$formname.$secondselectname";
                    549:     my $first = "document.$formname.$firstselectname";
                    550:     # output the javascript to do the changing
                    551:     my $result = '';
1.219     albertel  552:     $result.="<script type=\"text/javascript\">\n";
1.36      matthew   553:     $result.="var select2data = new Object();\n";
                    554:     $" = '","';
                    555:     my $debug = '';
                    556:     foreach my $s1 (sort(keys(%$hashref))) {
                    557:         $result.="select2data.d_$s1 = new Object();\n";        
                    558:         $result.="select2data.d_$s1.def = new String('".
                    559:             $hashref->{$s1}->{'default'}."');\n";
                    560:         $result.="select2data.d_$s1.values = new Array(";        
                    561:         my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
                    562:         $result.="\"@s2values\");\n";
                    563:         $result.="select2data.d_$s1.texts = new Array(";        
                    564:         my @s2texts;
                    565:         foreach my $value (@s2values) {
                    566:             push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
                    567:         }
                    568:         $result.="\"@s2texts\");\n";
                    569:     }
                    570:     $"=' ';
                    571:     $result.= <<"END";
                    572: 
                    573: function select1_changed() {
                    574:     // Determine new choice
                    575:     var newvalue = "d_" + $first.value;
                    576:     // update select2
                    577:     var values     = select2data[newvalue].values;
                    578:     var texts      = select2data[newvalue].texts;
                    579:     var select2def = select2data[newvalue].def;
                    580:     var i;
                    581:     // out with the old
                    582:     for (i = 0; i < $second.options.length; i++) {
                    583:         $second.options[i] = null;
                    584:     }
                    585:     // in with the nuclear
                    586:     for (i=0;i<values.length; i++) {
                    587:         $second.options[i] = new Option(values[i]);
1.143     matthew   588:         $second.options[i].value = values[i];
1.36      matthew   589:         $second.options[i].text = texts[i];
                    590:         if (values[i] == select2def) {
                    591:             $second.options[i].selected = true;
                    592:         }
                    593:     }
                    594: }
                    595: </script>
                    596: END
                    597:     # output the initial values for the selection lists
                    598:     $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed()\">\n";
                    599:     foreach my $value (sort(keys(%$hashref))) {
                    600:         $result.="    <option value=\"$value\" ";
1.253     albertel  601:         $result.=" selected=\"selected\" " if ($value eq $firstdefault);
1.119     www       602:         $result.=">".&mt($hashref->{$value}->{'text'})."</option>\n";
1.36      matthew   603:     }
                    604:     $result .= "</select>\n";
                    605:     my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
                    606:     $result .= $middletext;
                    607:     $result .= "<select size=\"1\" name=\"$secondselectname\">\n";
                    608:     my $seconddefault = $hashref->{$firstdefault}->{'default'};
                    609:     foreach my $value (sort(keys(%select2))) {
                    610:         $result.="    <option value=\"$value\" ";        
1.253     albertel  611:         $result.=" selected=\"selected\" " if ($value eq $seconddefault);
1.119     www       612:         $result.=">".&mt($select2{$value})."</option>\n";
1.36      matthew   613:     }
                    614:     $result .= "</select>\n";
                    615:     #    return $debug;
                    616:     return $result;
                    617: }   #  end of sub linked_select_forms {
                    618: 
1.45      matthew   619: =pod
1.44      bowersj2  620: 
1.112     bowersj2  621: =item * help_open_topic($topic, $text, $stayOnPage, $width, $height)
1.44      bowersj2  622: 
1.112     bowersj2  623: Returns a string corresponding to an HTML link to the given help
                    624: $topic, where $topic corresponds to the name of a .tex file in
                    625: /home/httpd/html/adm/help/tex, with underscores replaced by
                    626: spaces. 
                    627: 
                    628: $text will optionally be linked to the same topic, allowing you to
                    629: link text in addition to the graphic. If you do not want to link
                    630: text, but wish to specify one of the later parameters, pass an
                    631: empty string. 
                    632: 
                    633: $stayOnPage is a value that will be interpreted as a boolean. If true,
                    634: the link will not open a new window. If false, the link will open
                    635: a new window using Javascript. (Default is false.) 
                    636: 
                    637: $width and $height are optional numerical parameters that will
                    638: override the width and height of the popped up window, which may
                    639: be useful for certain help topics with big pictures included. 
1.44      bowersj2  640: 
                    641: =cut
                    642: 
                    643: sub help_open_topic {
1.48      bowersj2  644:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
                    645:     $text = "" if (not defined $text);
1.44      bowersj2  646:     $stayOnPage = 0 if (not defined $stayOnPage);
1.258     albertel  647:     if ($env{'browser.interface'} eq 'textual' ||
                    648: 	$env{'environment.remote'} eq 'off' ) {
1.79      www       649: 	$stayOnPage=1;
                    650:     }
1.44      bowersj2  651:     $width = 350 if (not defined $width);
                    652:     $height = 400 if (not defined $height);
                    653:     my $filename = $topic;
                    654:     $filename =~ s/ /_/g;
                    655: 
1.48      bowersj2  656:     my $template = "";
                    657:     my $link;
1.159     www       658: 
                    659:     $topic=~s/\W/\_/g;
1.44      bowersj2  660: 
                    661:     if (!$stayOnPage)
                    662:     {
1.72      bowersj2  663: 	$link = "javascript:void(open('/adm/help/${filename}.hlp', 'Help_for_$topic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
1.44      bowersj2  664:     }
                    665:     else
                    666:     {
1.48      bowersj2  667: 	$link = "/adm/help/${filename}.hlp";
                    668:     }
                    669: 
                    670:     # Add the text
                    671:     if ($text ne "")
                    672:     {
1.77      www       673: 	$template .= 
                    674:   "<table bgcolor='#3333AA' cellspacing='1' cellpadding='1' border='0'><tr>".
1.78      www       675:   "<td bgcolor='#5555FF'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.48      bowersj2  676:     }
                    677: 
                    678:     # Add the graphic
1.179     matthew   679:     my $title = &mt('Online Help');
1.215     albertel  680:     my $helpicon=&lonhttpdurl("/adm/help/gif/smallHelp.gif");
1.48      bowersj2  681:     $template .= <<"ENDTEMPLATE";
1.218     albertel  682:  <a href="$link" title="$title"><img src="$helpicon" border="0" alt="(Help: $topic)" /></a>
1.44      bowersj2  683: ENDTEMPLATE
1.78      www       684:     if ($text ne '') { $template.='</td></tr></table>' };
1.44      bowersj2  685:     return $template;
                    686: 
1.106     bowersj2  687: }
                    688: 
                    689: # This is a quicky function for Latex cheatsheet editing, since it 
                    690: # appears in at least four places
                    691: sub helpLatexCheatsheet {
                    692:     my $other = shift;
                    693:     my $addOther = '';
                    694:     if ($other) {
                    695: 	$addOther = Apache::loncommon::help_open_topic($other, shift,
                    696: 						       undef, undef, 600) .
                    697: 							   '</td><td>';
                    698:     }
                    699:     return '<table><tr><td>'.
                    700: 	$addOther .
                    701: 	&Apache::loncommon::help_open_topic("Greek_Symbols",'Greek Symbols',
                    702: 					    undef,undef,600)
                    703: 	.'</td><td>'.
                    704: 	&Apache::loncommon::help_open_topic("Other_Symbols",'Other Symbols',
                    705: 					    undef,undef,600)
                    706: 	.'</td></tr></table>';
1.172     www       707: }
                    708: 
1.193     raeburn   709: sub help_open_menu {
1.400     albertel  710:     my ($topic,$component_help,$faq,$bug,$stayOnPage,$width,$height,$text) = @_;
1.193     raeburn   711:     $text = "" if (not defined $text);
                    712:     $stayOnPage = 0 if (not defined $stayOnPage);
1.258     albertel  713:     if ($env{'browser.interface'} eq 'textual' ||
                    714:         $env{'environment.remote'} eq 'off' ) {
1.193     raeburn   715:         $stayOnPage=1;
                    716:     }
                    717:     $width = 620 if (not defined $width);
                    718:     $height = 600 if (not defined $height);
                    719:     my $link='';
1.201     raeburn   720:     my $title = &mt('Get help');
1.193     raeburn   721:     my $origurl = $ENV{'REQUEST_URI'};
1.227     albertel  722:     $origurl=~s|^/~|/priv/|;
1.193     raeburn   723:     my $timestamp = time;
1.400     albertel  724:     foreach my $datum (\$topic,\$component_help,\$faq,\$bug,\$origurl) {
1.369     www       725:         $$datum = &escape($$datum);
1.193     raeburn   726:     }
1.195     albertel  727:     if (!$stayOnPage) {
1.193     raeburn   728:          $link = "javascript:helpMenu('open')";
1.195     albertel  729:     } else {
1.193     raeburn   730:         $link = "javascript:helpMenu('display')";
                    731:     }
1.400     albertel  732:     my $banner_link = "/adm/helpmenu?page=banner&amp;topic=$topic&amp;component_help=$component_help&amp;faq=$faq&amp;bug=$bug&amp;origurl=$origurl&amp;stamp=$timestamp&amp;stayonpage=$stayOnPage";
                    733:     my $details_link = "/adm/helpmenu?page=body&amp;topic=$topic&amp;component_help=$component_help&amp;faq=$faq&amp;bug=$bug&amp;origurl=$origurl&amp;stamp=$timestamp";
1.196     albertel  734:     my $template;
                    735:     if ($text ne "") {
                    736: 	$template .= 
1.265     albertel  737:   "<table bgcolor='#CC3300' cellspacing='1' cellpadding='1' border='0'><tr>".
                    738:   "<td bgcolor='#CC6600'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.196     albertel  739:     }
1.261     albertel  740:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.215     albertel  741:     my $helpicon=&lonhttpdurl("/adm/lonIcons/helpgateway.gif");
1.331     albertel  742:     my $start_page =
                    743:         &Apache::loncommon::start_page('Help Menu', undef,
                    744: 				       {'frameset'    => 1,
                    745: 					'js_ready'    => 1,
                    746: 					'add_entries' => {
                    747: 					    'border' => '0',
                    748: 					    'rows'   => "105,*",},});
                    749:     my $end_page =
                    750:         &Apache::loncommon::end_page({'frameset' => 1,
                    751: 				      'js_ready' => 1,});
                    752: 
1.196     albertel  753:     $template .= <<"ENDTEMPLATE";
1.219     albertel  754:  <script type="text/javascript">
1.253     albertel  755: // <!-- BEGIN LON-CAPA Internal
                    756: // <![CDATA[
1.243     raeburn   757: function helpMenu(target) {
                    758:     var caller = this;
                    759:     if (target == 'open') {
                    760:         var newWindow = null;
                    761:         try {
1.262     albertel  762:             newWindow =  window.open($nothing,"helpmenu","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" )
1.243     raeburn   763:         }
                    764:         catch(error) {
                    765:             writeHelp(caller);
                    766:             return;
                    767:         }
                    768:         if (newWindow) {
                    769:             caller = newWindow;
                    770:         }
1.193     raeburn   771:     }
1.243     raeburn   772:     writeHelp(caller);
                    773:     return;
                    774: }
                    775: function writeHelp(caller) {
1.331     albertel  776:     caller.document.writeln('$start_page<frame name="bannerframe"  src="$banner_link" /><frame name="bodyframe" src="$details_link" /> $end_page')
1.243     raeburn   777:     caller.document.close()
                    778:     caller.focus()
1.193     raeburn   779: }
1.253     albertel  780: // ]]>
1.219     albertel  781: // END LON-CAPA Internal -->
1.193     raeburn   782:  </script>
1.218     albertel  783:  <a href="$link" title="$title"><img src="$helpicon" border="0" alt="(Help Menu)" /></a>
1.193     raeburn   784: ENDTEMPLATE
1.203     albertel  785:     if ($component_help) {
                    786: 	if (!$text) {
                    787: 	    $template=&help_open_topic($component_help,undef,$stayOnPage,
                    788: 				       $width,$height).' '.$template;
                    789: 	} else {
                    790: 	    my $help_text;
1.369     www       791: 	    $help_text=&unescape($topic);
1.203     albertel  792: 	    $template='<table><tr><td>'.
                    793: 		&help_open_topic($component_help,$help_text,$stayOnPage,
                    794: 				 $width,$height).'</td><td>'.$template.
                    795: 				 '</td></tr></table>';
                    796: 	}
                    797:     }
1.196     albertel  798:     if ($text ne '') { $template.='</td></tr></table>' };
1.193     raeburn   799:     return $template;
                    800: }
                    801: 
1.172     www       802: sub help_open_bug {
                    803:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel  804:     unless ($env{'user.adv'}) { return ''; }
1.172     www       805:     unless ($Apache::lonnet::perlvar{'BugzillaHost'}) { return ''; }
                    806:     $text = "" if (not defined $text);
                    807:     $stayOnPage = 0 if (not defined $stayOnPage);
1.258     albertel  808:     if ($env{'browser.interface'} eq 'textual' ||
                    809: 	$env{'environment.remote'} eq 'off' ) {
1.172     www       810: 	$stayOnPage=1;
                    811:     }
1.184     albertel  812:     $width = 600 if (not defined $width);
                    813:     $height = 600 if (not defined $height);
1.172     www       814: 
                    815:     $topic=~s/\W+/\+/g;
                    816:     my $link='';
                    817:     my $template='';
1.379     albertel  818:     my $url=$Apache::lonnet::perlvar{'BugzillaHost'}.'enter_bug.cgi?product=LON-CAPA&amp;bug_file_loc='.
                    819: 	&escape($ENV{'REQUEST_URI'}).'&amp;component='.$topic;
1.172     www       820:     if (!$stayOnPage)
                    821:     {
                    822: 	$link = "javascript:void(open('$url', 'Bugzilla', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                    823:     }
                    824:     else
                    825:     {
                    826: 	$link = $url;
                    827:     }
                    828:     # Add the text
                    829:     if ($text ne "")
                    830:     {
                    831: 	$template .= 
                    832:   "<table bgcolor='#AA3333' cellspacing='1' cellpadding='1' border='0'><tr>".
                    833:   "<td bgcolor='#FF5555'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
                    834:     }
                    835: 
                    836:     # Add the graphic
1.179     matthew   837:     my $title = &mt('Report a Bug');
1.215     albertel  838:     my $bugicon=&lonhttpdurl("/adm/lonMisc/smallBug.gif");
1.172     www       839:     $template .= <<"ENDTEMPLATE";
1.218     albertel  840:  <a href="$link" title="$title"><img src="$bugicon" border="0" alt="(Bug: $topic)" /></a>
1.172     www       841: ENDTEMPLATE
                    842:     if ($text ne '') { $template.='</td></tr></table>' };
                    843:     return $template;
                    844: 
                    845: }
                    846: 
                    847: sub help_open_faq {
                    848:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel  849:     unless ($env{'user.adv'}) { return ''; }
1.172     www       850:     unless ($Apache::lonnet::perlvar{'FAQHost'}) { return ''; }
                    851:     $text = "" if (not defined $text);
                    852:     $stayOnPage = 0 if (not defined $stayOnPage);
1.258     albertel  853:     if ($env{'browser.interface'} eq 'textual' ||
                    854: 	$env{'environment.remote'} eq 'off' ) {
1.172     www       855: 	$stayOnPage=1;
                    856:     }
                    857:     $width = 350 if (not defined $width);
                    858:     $height = 400 if (not defined $height);
                    859: 
                    860:     $topic=~s/\W+/\+/g;
                    861:     my $link='';
                    862:     my $template='';
                    863:     my $url=$Apache::lonnet::perlvar{'FAQHost'}.'/fom/cache/'.$topic.'.html';
                    864:     if (!$stayOnPage)
                    865:     {
                    866: 	$link = "javascript:void(open('$url', 'FAQ-O-Matic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                    867:     }
                    868:     else
                    869:     {
                    870: 	$link = $url;
                    871:     }
                    872: 
                    873:     # Add the text
                    874:     if ($text ne "")
                    875:     {
                    876: 	$template .= 
1.173     www       877:   "<table bgcolor='#337733' cellspacing='1' cellpadding='1' border='0'><tr>".
                    878:   "<td bgcolor='#448844'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.172     www       879:     }
                    880: 
                    881:     # Add the graphic
1.179     matthew   882:     my $title = &mt('View the FAQ');
1.215     albertel  883:     my $faqicon=&lonhttpdurl("/adm/lonMisc/smallFAQ.gif");
1.172     www       884:     $template .= <<"ENDTEMPLATE";
1.218     albertel  885:  <a href="$link" title="$title"><img src="$faqicon" border="0" alt="(FAQ: $topic)" /></a>
1.172     www       886: ENDTEMPLATE
                    887:     if ($text ne '') { $template.='</td></tr></table>' };
                    888:     return $template;
                    889: 
1.44      bowersj2  890: }
1.37      matthew   891: 
1.180     matthew   892: ###############################################################
                    893: ###############################################################
                    894: 
1.45      matthew   895: =pod
                    896: 
1.256     matthew   897: =item * change_content_javascript():
                    898: 
                    899: This and the next function allow you to create small sections of an
                    900: otherwise static HTML page that you can update on the fly with
                    901: Javascript, even in Netscape 4.
                    902: 
                    903: The Javascript fragment returned by this function (no E<lt>scriptE<gt> tag)
                    904: must be written to the HTML page once. It will prove the Javascript
                    905: function "change(name, content)". Calling the change function with the
                    906: name of the section 
                    907: you want to update, matching the name passed to C<changable_area>, and
                    908: the new content you want to put in there, will put the content into
                    909: that area.
                    910: 
                    911: B<Note>: Netscape 4 only reserves enough space for the changable area
                    912: to contain room for the original contents. You need to "make space"
                    913: for whatever changes you wish to make, and be B<sure> to check your
                    914: code in Netscape 4. This feature in Netscape 4 is B<not> powerful;
                    915: it's adequate for updating a one-line status display, but little more.
                    916: This script will set the space to 100% width, so you only need to
                    917: worry about height in Netscape 4.
                    918: 
                    919: Modern browsers are much less limiting, and if you can commit to the
                    920: user not using Netscape 4, this feature may be used freely with
                    921: pretty much any HTML.
                    922: 
                    923: =cut
                    924: 
                    925: sub change_content_javascript {
                    926:     # If we're on Netscape 4, we need to use Layer-based code
1.258     albertel  927:     if ($env{'browser.type'} eq 'netscape' &&
                    928: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew   929: 	return (<<NETSCAPE4);
                    930: 	function change(name, content) {
                    931: 	    doc = document.layers[name+"___escape"].layers[0].document;
                    932: 	    doc.open();
                    933: 	    doc.write(content);
                    934: 	    doc.close();
                    935: 	}
                    936: NETSCAPE4
                    937:     } else {
                    938: 	# Otherwise, we need to use semi-standards-compliant code
                    939: 	# (technically, "innerHTML" isn't standard but the equivalent
                    940: 	# is really scary, and every useful browser supports it
                    941: 	return (<<DOMBASED);
                    942: 	function change(name, content) {
                    943: 	    element = document.getElementById(name);
                    944: 	    element.innerHTML = content;
                    945: 	}
                    946: DOMBASED
                    947:     }
                    948: }
                    949: 
                    950: =pod
                    951: 
                    952: =item * changable_area($name, $origContent):
                    953: 
                    954: This provides a "changable area" that can be modified on the fly via
                    955: the Javascript code provided in C<change_content_javascript>. $name is
                    956: the name you will use to reference the area later; do not repeat the
                    957: same name on a given HTML page more then once. $origContent is what
                    958: the area will originally contain, which can be left blank.
                    959: 
                    960: =cut
                    961: 
                    962: sub changable_area {
                    963:     my ($name, $origContent) = @_;
                    964: 
1.258     albertel  965:     if ($env{'browser.type'} eq 'netscape' &&
                    966: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew   967: 	# If this is netscape 4, we need to use the Layer tag
                    968: 	return "<ilayer width='100%' id='${name}___escape' overflow='none'><layer width='100%' id='$name' overflow='none'>$origContent</layer></ilayer>";
                    969:     } else {
                    970: 	return "<span id='$name'>$origContent</span>";
                    971:     }
                    972: }
                    973: 
                    974: =pod
                    975: 
                    976: =back
                    977: 
                    978: =head1 Excel and CSV file utility routines
                    979: 
                    980: =over 4
                    981: 
                    982: =cut
                    983: 
                    984: ###############################################################
                    985: ###############################################################
                    986: 
                    987: =pod
                    988: 
1.112     bowersj2  989: =item * csv_translate($text) 
1.37      matthew   990: 
1.185     www       991: Translate $text to allow it to be output as a 'comma separated values' 
1.37      matthew   992: format.
                    993: 
                    994: =cut
                    995: 
1.180     matthew   996: ###############################################################
                    997: ###############################################################
1.37      matthew   998: sub csv_translate {
                    999:     my $text = shift;
                   1000:     $text =~ s/\"/\"\"/g;
1.209     albertel 1001:     $text =~ s/\n/ /g;
1.37      matthew  1002:     return $text;
                   1003: }
1.180     matthew  1004: 
                   1005: ###############################################################
                   1006: ###############################################################
                   1007: 
                   1008: =pod
                   1009: 
                   1010: =item * define_excel_formats
                   1011: 
                   1012: Define some commonly used Excel cell formats.
                   1013: 
                   1014: Currently supported formats:
                   1015: 
                   1016: =over 4
                   1017: 
                   1018: =item header
                   1019: 
                   1020: =item bold
                   1021: 
                   1022: =item h1
                   1023: 
                   1024: =item h2
                   1025: 
                   1026: =item h3
                   1027: 
1.256     matthew  1028: =item h4
                   1029: 
                   1030: =item i
                   1031: 
1.180     matthew  1032: =item date
                   1033: 
                   1034: =back
                   1035: 
                   1036: Inputs: $workbook
                   1037: 
                   1038: Returns: $format, a hash reference.
                   1039: 
                   1040: =cut
                   1041: 
                   1042: ###############################################################
                   1043: ###############################################################
                   1044: sub define_excel_formats {
                   1045:     my ($workbook) = @_;
                   1046:     my $format;
                   1047:     $format->{'header'} = $workbook->add_format(bold      => 1, 
                   1048:                                                 bottom    => 1,
                   1049:                                                 align     => 'center');
                   1050:     $format->{'bold'} = $workbook->add_format(bold=>1);
                   1051:     $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
                   1052:     $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
                   1053:     $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
1.255     matthew  1054:     $format->{'h4'}   = $workbook->add_format(bold=>1, size=>12);
1.246     matthew  1055:     $format->{'i'}    = $workbook->add_format(italic=>1);
1.180     matthew  1056:     $format->{'date'} = $workbook->add_format(num_format=>
1.207     matthew  1057:                                             'mm/dd/yyyy hh:mm:ss');
1.180     matthew  1058:     return $format;
                   1059: }
                   1060: 
                   1061: ###############################################################
                   1062: ###############################################################
1.113     bowersj2 1063: 
                   1064: =pod
                   1065: 
1.256     matthew  1066: =item * create_workbook
1.255     matthew  1067: 
                   1068: Create an Excel worksheet.  If it fails, output message on the
                   1069: request object and return undefs.
                   1070: 
                   1071: Inputs: Apache request object
                   1072: 
                   1073: Returns (undef) on failure, 
                   1074:     Excel worksheet object, scalar with filename, and formats 
                   1075:     from &Apache::loncommon::define_excel_formats on success
                   1076: 
                   1077: =cut
                   1078: 
                   1079: ###############################################################
                   1080: ###############################################################
                   1081: sub create_workbook {
                   1082:     my ($r) = @_;
                   1083:         #
                   1084:     # Create the excel spreadsheet
                   1085:     my $filename = '/prtspool/'.
1.258     albertel 1086:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.255     matthew  1087:         time.'_'.rand(1000000000).'.xls';
                   1088:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
                   1089:     if (! defined($workbook)) {
                   1090:         $r->log_error("Error creating excel spreadsheet $filename: $!");
                   1091:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
                   1092:                             "This error has been logged.  ".
                   1093:                             "Please alert your LON-CAPA administrator").
                   1094:                   '</p>');
                   1095:         return (undef);
                   1096:     }
                   1097:     #
                   1098:     $workbook->set_tempdir('/home/httpd/perl/tmp');
                   1099:     #
                   1100:     my $format = &Apache::loncommon::define_excel_formats($workbook);
                   1101:     return ($workbook,$filename,$format);
                   1102: }
                   1103: 
                   1104: ###############################################################
                   1105: ###############################################################
                   1106: 
                   1107: =pod
                   1108: 
1.256     matthew  1109: =item * create_text_file
1.113     bowersj2 1110: 
1.256     matthew  1111: Create a file to write to and eventually make available to the usre.
                   1112: If file creation fails, outputs an error message on the request object and 
                   1113: return undefs.
1.113     bowersj2 1114: 
1.256     matthew  1115: Inputs: Apache request object, and file suffix
1.113     bowersj2 1116: 
1.256     matthew  1117: Returns (undef) on failure, 
                   1118:     Filehandle and filename on success.
1.113     bowersj2 1119: 
                   1120: =cut
                   1121: 
1.256     matthew  1122: ###############################################################
                   1123: ###############################################################
                   1124: sub create_text_file {
                   1125:     my ($r,$suffix) = @_;
                   1126:     if (! defined($suffix)) { $suffix = 'txt'; };
                   1127:     my $fh;
                   1128:     my $filename = '/prtspool/'.
1.258     albertel 1129:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.256     matthew  1130:         time.'_'.rand(1000000000).'.'.$suffix;
                   1131:     $fh = Apache::File->new('>/home/httpd'.$filename);
                   1132:     if (! defined($fh)) {
                   1133:         $r->log_error("Couldn't open $filename for output $!");
                   1134:         $r->print("Problems occured in creating the output file.  ".
                   1135:                   "This error has been logged.  ".
                   1136:                   "Please alert your LON-CAPA administrator.");
1.113     bowersj2 1137:     }
1.256     matthew  1138:     return ($fh,$filename)
1.113     bowersj2 1139: }
                   1140: 
                   1141: 
1.256     matthew  1142: =pod 
1.113     bowersj2 1143: 
                   1144: =back
                   1145: 
                   1146: =cut
1.37      matthew  1147: 
                   1148: ###############################################################
1.33      matthew  1149: ##        Home server <option> list generating code          ##
                   1150: ###############################################################
1.35      matthew  1151: 
1.45      matthew  1152: =pod
                   1153: 
1.112     bowersj2 1154: =head1 Home Server option list generating code
                   1155: 
                   1156: =over 4
                   1157: 
                   1158: =item * get_domains()
1.35      matthew  1159: 
                   1160: Returns an array containing each of the domains listed in the hosts.tab
                   1161: file.
                   1162: 
                   1163: =cut
                   1164: 
                   1165: #-------------------------------------------
1.34      matthew  1166: sub get_domains {
                   1167:     # The code below was stolen from "The Perl Cookbook", p 102, 1st ed.
                   1168:     my @domains;
                   1169:     my %seen;
1.356     albertel 1170:     foreach my $dom (sort(values(%Apache::lonnet::hostdom))) {
                   1171: 	push(@domains,$dom) unless $seen{$dom}++;
1.34      matthew  1172:     }
                   1173:     return @domains;
                   1174: }
1.88      www      1175: 
1.169     www      1176: # ------------------------------------------
                   1177: 
                   1178: sub domain_select {
                   1179:     my ($name,$value,$multiple)=@_;
                   1180:     my %domains=map { 
                   1181: 	$_ => $_.' '.$Apache::lonnet::domaindescription{$_} 
                   1182:     } &get_domains;
                   1183:     if ($multiple) {
                   1184: 	$domains{''}=&mt('Any domain');
1.287     albertel 1185: 	return &multiple_select_form($name,$value,4,\%domains);
1.169     www      1186:     } else {
                   1187: 	return &select_form($name,$value,%domains);
                   1188:     }
                   1189: }
                   1190: 
1.282     albertel 1191: #-------------------------------------------
                   1192: 
                   1193: =pod
                   1194: 
1.287     albertel 1195: =item * multiple_select_form($name,$value,$size,$hash,$order)
1.282     albertel 1196: 
                   1197: Returns a string containing a <select> element int multiple mode
                   1198: 
                   1199: 
                   1200: Args:
                   1201:   $name - name of the <select> element
                   1202:   $value - sclara or array ref of values that should already be selected
                   1203:   $size - number of rows long the select element is
1.283     albertel 1204:   $hash - the elements should be 'option' => 'shown text'
1.282     albertel 1205:           (shown text should already have been &mt())
1.284     albertel 1206:   $order - (optional) array ref of the order to show the elments in
1.283     albertel 1207: 
1.282     albertel 1208: =cut
                   1209: 
                   1210: #-------------------------------------------
1.169     www      1211: sub multiple_select_form {
1.284     albertel 1212:     my ($name,$value,$size,$hash,$order)=@_;
1.169     www      1213:     my %selected = map { $_ => 1 } ref($value)?@{$value}:($value);
                   1214:     my $output='';
1.191     matthew  1215:     if (! defined($size)) {
                   1216:         $size = 4;
1.283     albertel 1217:         if (scalar(keys(%$hash))<4) {
                   1218:             $size = scalar(keys(%$hash));
1.191     matthew  1219:         }
                   1220:     }
1.169     www      1221:     $output.="\n<select name='$name' size='$size' multiple='1'>";
1.286     banghart 1222:     my @order = ref($order) ? @$order
1.284     albertel 1223:                             : sort(keys(%$hash));
                   1224:     foreach my $key (@order) {
1.356     albertel 1225:         $output.='<option value="'.&HTML::Entities::encode($key,'"<>&').'" ';
1.284     albertel 1226:         $output.='selected="selected" ' if ($selected{$key});
                   1227:         $output.='>'.$hash->{$key}."</option>\n";
1.169     www      1228:     }
                   1229:     $output.="</select>\n";
                   1230:     return $output;
                   1231: }
                   1232: 
1.88      www      1233: #-------------------------------------------
                   1234: 
                   1235: =pod
                   1236: 
1.112     bowersj2 1237: =item * select_form($defdom,$name,%hash)
1.88      www      1238: 
                   1239: Returns a string containing a <select name='$name' size='1'> form to 
                   1240: allow a user to select options from a hash option_name => displayed text.  
                   1241: See lonrights.pm for an example invocation and use.
                   1242: 
                   1243: =cut
                   1244: 
                   1245: #-------------------------------------------
                   1246: sub select_form {
                   1247:     my ($def,$name,%hash) = @_;
                   1248:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
1.128     albertel 1249:     my @keys;
                   1250:     if (exists($hash{'select_form_order'})) {
                   1251: 	@keys=@{$hash{'select_form_order'}};
                   1252:     } else {
                   1253: 	@keys=sort(keys(%hash));
                   1254:     }
1.356     albertel 1255:     foreach my $key (@keys) {
                   1256:         $selectform.=
                   1257: 	    '<option value="'.&HTML::Entities::encode($key,'"<>&').'" '.
                   1258:             ($key eq $def ? 'selected="selected" ' : '').
                   1259:                 ">".&mt($hash{$key})."</option>\n";
1.88      www      1260:     }
                   1261:     $selectform.="</select>";
                   1262:     return $selectform;
                   1263: }
                   1264: 
1.167     www      1265: sub gradeleveldescription {
                   1266:     my $gradelevel=shift;
                   1267:     my %gradelevels=(0 => 'Not specified',
                   1268: 		     1 => 'Grade 1',
                   1269: 		     2 => 'Grade 2',
                   1270: 		     3 => 'Grade 3',
                   1271: 		     4 => 'Grade 4',
                   1272: 		     5 => 'Grade 5',
                   1273: 		     6 => 'Grade 6',
                   1274: 		     7 => 'Grade 7',
                   1275: 		     8 => 'Grade 8',
                   1276: 		     9 => 'Grade 9',
                   1277: 		     10 => 'Grade 10',
                   1278: 		     11 => 'Grade 11',
                   1279: 		     12 => 'Grade 12',
                   1280: 		     13 => 'Grade 13',
                   1281: 		     14 => '100 Level',
                   1282: 		     15 => '200 Level',
                   1283: 		     16 => '300 Level',
                   1284: 		     17 => '400 Level',
                   1285: 		     18 => 'Graduate Level');
                   1286:     return &mt($gradelevels{$gradelevel});
                   1287: }
                   1288: 
1.163     www      1289: sub select_level_form {
                   1290:     my ($deflevel,$name)=@_;
                   1291:     unless ($deflevel) { $deflevel=0; }
1.167     www      1292:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
                   1293:     for (my $i=0; $i<=18; $i++) {
                   1294:         $selectform.="<option value=\"$i\" ".
1.253     albertel 1295:             ($i==$deflevel ? 'selected="selected" ' : '').
1.167     www      1296:                 ">".&gradeleveldescription($i)."</option>\n";
                   1297:     }
                   1298:     $selectform.="</select>";
                   1299:     return $selectform;
1.163     www      1300: }
1.167     www      1301: 
1.35      matthew  1302: #-------------------------------------------
                   1303: 
1.45      matthew  1304: =pod
                   1305: 
1.112     bowersj2 1306: =item * select_dom_form($defdom,$name,$includeempty)
1.35      matthew  1307: 
                   1308: Returns a string containing a <select name='$name' size='1'> form to 
                   1309: allow a user to select the domain to preform an operation in.  
                   1310: See loncreateuser.pm for an example invocation and use.
                   1311: 
1.90      www      1312: If the $includeempty flag is set, it also includes an empty choice ("no domain
                   1313: selected");
                   1314: 
1.35      matthew  1315: =cut
                   1316: 
                   1317: #-------------------------------------------
1.34      matthew  1318: sub select_dom_form {
1.90      www      1319:     my ($defdom,$name,$includeempty) = @_;
1.34      matthew  1320:     my @domains = get_domains();
1.90      www      1321:     if ($includeempty) { @domains=('',@domains); }
1.34      matthew  1322:     my $selectdomain = "<select name=\"$name\" size=\"1\">\n";
1.356     albertel 1323:     foreach my $dom (@domains) {
                   1324:         $selectdomain.="<option value=\"$dom\" ".
                   1325:             ($dom eq $defdom ? 'selected="selected" ' : '').
                   1326:                 ">$dom</option>\n";
1.34      matthew  1327:     }
                   1328:     $selectdomain.="</select>";
                   1329:     return $selectdomain;
                   1330: }
                   1331: 
1.35      matthew  1332: #-------------------------------------------
                   1333: 
1.45      matthew  1334: =pod
                   1335: 
1.112     bowersj2 1336: =item * get_library_servers($domain)
1.35      matthew  1337: 
                   1338: Returns a hash which contains keys like '103l3' and values like 
                   1339: 'kirk.lite.msu.edu'.  All of the keys will be for machines in the
                   1340: given $domain.
                   1341: 
                   1342: =cut
                   1343: 
                   1344: #-------------------------------------------
1.52      matthew  1345: sub get_library_servers {
1.33      matthew  1346:     my $domain = shift;
1.52      matthew  1347:     my %library_servers;
1.356     albertel 1348:     foreach my $hostid (keys(%Apache::lonnet::libserv)) {
                   1349:         if ($Apache::lonnet::hostdom{$hostid} eq $domain) {
                   1350:             $library_servers{$hostid} = $Apache::lonnet::hostname{$hostid};
1.33      matthew  1351:         }
                   1352:     }
1.52      matthew  1353:     return %library_servers;
1.33      matthew  1354: }
                   1355: 
1.35      matthew  1356: #-------------------------------------------
                   1357: 
1.45      matthew  1358: =pod
                   1359: 
1.112     bowersj2 1360: =item * home_server_option_list($domain)
1.35      matthew  1361: 
                   1362: returns a string which contains an <option> list to be used in a 
                   1363: <select> form input.  See loncreateuser.pm for an example.
                   1364: 
                   1365: =cut
                   1366: 
                   1367: #-------------------------------------------
1.33      matthew  1368: sub home_server_option_list {
                   1369:     my $domain = shift;
1.52      matthew  1370:     my %servers = &get_library_servers($domain);
1.33      matthew  1371:     my $result = '';
1.356     albertel 1372:     foreach my $hostid (sort(keys(%servers))) {
1.33      matthew  1373:         $result.=
1.356     albertel 1374:             '<option value="'.$hostid.'">'.
                   1375: 	    $hostid.' '.$servers{$hostid}."</option>\n";
1.33      matthew  1376:     }
                   1377:     return $result;
                   1378: }
1.112     bowersj2 1379: 
                   1380: =pod
                   1381: 
                   1382: =back
                   1383: 
                   1384: =cut
1.87      matthew  1385: 
                   1386: ###############################################################
1.112     bowersj2 1387: ##                  Decoding User Agent                      ##
1.87      matthew  1388: ###############################################################
                   1389: 
                   1390: =pod
                   1391: 
1.112     bowersj2 1392: =head1 Decoding the User Agent
                   1393: 
                   1394: =over 4
                   1395: 
                   1396: =item * &decode_user_agent()
1.87      matthew  1397: 
                   1398: Inputs: $r
                   1399: 
                   1400: Outputs:
                   1401: 
                   1402: =over 4
                   1403: 
1.112     bowersj2 1404: =item * $httpbrowser
1.87      matthew  1405: 
1.112     bowersj2 1406: =item * $clientbrowser
1.87      matthew  1407: 
1.112     bowersj2 1408: =item * $clientversion
1.87      matthew  1409: 
1.112     bowersj2 1410: =item * $clientmathml
1.87      matthew  1411: 
1.112     bowersj2 1412: =item * $clientunicode
1.87      matthew  1413: 
1.112     bowersj2 1414: =item * $clientos
1.87      matthew  1415: 
                   1416: =back
                   1417: 
1.157     matthew  1418: =back 
                   1419: 
1.87      matthew  1420: =cut
                   1421: 
                   1422: ###############################################################
                   1423: ###############################################################
                   1424: sub decode_user_agent {
1.247     albertel 1425:     my ($r)=@_;
1.87      matthew  1426:     my @browsertype=split(/\&/,$Apache::lonnet::perlvar{"lonBrowsDet"});
                   1427:     my %mathcap=split(/\&/,$$Apache::lonnet::perlvar{"lonMathML"});
                   1428:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
1.247     albertel 1429:     if (!$httpbrowser && $r) { $httpbrowser=$r->header_in('User-Agent'); }
1.87      matthew  1430:     my $clientbrowser='unknown';
                   1431:     my $clientversion='0';
                   1432:     my $clientmathml='';
                   1433:     my $clientunicode='0';
                   1434:     for (my $i=0;$i<=$#browsertype;$i++) {
                   1435:         my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\:/,$browsertype[$i]);
                   1436: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
                   1437: 	    $clientbrowser=$bname;
                   1438:             $httpbrowser=~/$vreg/i;
                   1439: 	    $clientversion=$1;
                   1440:             $clientmathml=($clientversion>=$minv);
                   1441:             $clientunicode=($clientversion>=$univ);
                   1442: 	}
                   1443:     }
                   1444:     my $clientos='unknown';
                   1445:     if (($httpbrowser=~/linux/i) ||
                   1446:         ($httpbrowser=~/unix/i) ||
                   1447:         ($httpbrowser=~/ux/i) ||
                   1448:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
                   1449:     if (($httpbrowser=~/vax/i) ||
                   1450:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
                   1451:     if ($httpbrowser=~/next/i) { $clientos='next'; }
                   1452:     if (($httpbrowser=~/mac/i) ||
                   1453:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
                   1454:     if ($httpbrowser=~/win/i) { $clientos='win'; }
                   1455:     if ($httpbrowser=~/embed/i) { $clientos='pda'; }
                   1456:     return ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                   1457:             $clientunicode,$clientos,);
                   1458: }
                   1459: 
1.32      matthew  1460: ###############################################################
                   1461: ##    Authentication changing form generation subroutines    ##
                   1462: ###############################################################
                   1463: ##
                   1464: ## All of the authform_xxxxxxx subroutines take their inputs in a
                   1465: ## hash, and have reasonable default values.
                   1466: ##
                   1467: ##    formname = the name given in the <form> tag.
1.35      matthew  1468: #-------------------------------------------
                   1469: 
1.45      matthew  1470: =pod
                   1471: 
1.112     bowersj2 1472: =head1 Authentication Routines
                   1473: 
                   1474: =over 4
                   1475: 
                   1476: =item * authform_xxxxxx
1.35      matthew  1477: 
                   1478: The authform_xxxxxx subroutines provide javascript and html forms which 
                   1479: handle some of the conveniences required for authentication forms.  
                   1480: This is not an optimal method, but it works.  
                   1481: 
                   1482: See loncreateuser.pm for invocation and use examples.
                   1483: 
                   1484: =over 4
                   1485: 
1.112     bowersj2 1486: =item * authform_header
1.35      matthew  1487: 
1.112     bowersj2 1488: =item * authform_authorwarning
1.35      matthew  1489: 
1.112     bowersj2 1490: =item * authform_nochange
1.35      matthew  1491: 
1.112     bowersj2 1492: =item * authform_kerberos
1.35      matthew  1493: 
1.112     bowersj2 1494: =item * authform_internal
1.35      matthew  1495: 
1.112     bowersj2 1496: =item * authform_filesystem
1.35      matthew  1497: 
                   1498: =back
                   1499: 
1.157     matthew  1500: =back 
                   1501: 
1.35      matthew  1502: =cut
                   1503: 
                   1504: #-------------------------------------------
1.32      matthew  1505: sub authform_header{  
                   1506:     my %in = (
                   1507:         formname => 'cu',
1.80      albertel 1508:         kerb_def_dom => '',
1.32      matthew  1509:         @_,
                   1510:     );
                   1511:     $in{'formname'} = 'document.' . $in{'formname'};
                   1512:     my $result='';
1.80      albertel 1513: 
                   1514: #---------------------------------------------- Code for upper case translation
                   1515:     my $Javascript_toUpperCase;
                   1516:     unless ($in{kerb_def_dom}) {
                   1517:         $Javascript_toUpperCase =<<"END";
                   1518:         switch (choice) {
                   1519:            case 'krb': currentform.elements[choicearg].value =
                   1520:                currentform.elements[choicearg].value.toUpperCase();
                   1521:                break;
                   1522:            default:
                   1523:         }
                   1524: END
                   1525:     } else {
                   1526:         $Javascript_toUpperCase = "";
                   1527:     }
                   1528: 
1.165     raeburn  1529:     my $radioval = "'nochange'";
1.174     matthew  1530:     if (exists($in{'curr_authtype'}) &&
                   1531:         defined($in{'curr_authtype'}) &&
                   1532:         $in{'curr_authtype'} ne '') {
                   1533:         $radioval = "'$in{'curr_authtype'}arg'";
                   1534:     }
1.165     raeburn  1535:     my $argfield = 'null';
                   1536:     if ( grep/^mode$/,(keys %in) ) {
                   1537:         if ($in{'mode'} eq 'modifycourse')  {
                   1538:             if ( grep/^curr_authtype$/,(keys %in) ) {
                   1539:                 $radioval = "'$in{'curr_authtype'}'";
                   1540:             }
                   1541:             if ( grep/^curr_autharg$/,(keys %in) ) {
                   1542:                 unless ($in{'curr_autharg'} eq '') {
                   1543:                     $argfield = "'$in{'curr_autharg'}'";
                   1544:                 }
                   1545:             }
                   1546:         }
                   1547:     }
                   1548: 
1.32      matthew  1549:     $result.=<<"END";
                   1550: var current = new Object();
1.165     raeburn  1551: current.radiovalue = $radioval;
                   1552: current.argfield = $argfield;
1.32      matthew  1553: 
                   1554: function changed_radio(choice,currentform) {
                   1555:     var choicearg = choice + 'arg';
                   1556:     // If a radio button in changed, we need to change the argfield
                   1557:     if (current.radiovalue != choice) {
                   1558:         current.radiovalue = choice;
                   1559:         if (current.argfield != null) {
                   1560:             currentform.elements[current.argfield].value = '';
                   1561:         }
                   1562:         if (choice == 'nochange') {
                   1563:             current.argfield = null;
                   1564:         } else {
                   1565:             current.argfield = choicearg;
                   1566:             switch(choice) {
                   1567:                 case 'krb': 
                   1568:                     currentform.elements[current.argfield].value = 
                   1569:                         "$in{'kerb_def_dom'}";
                   1570:                 break;
                   1571:               default:
                   1572:                 break;
                   1573:             }
                   1574:         }
                   1575:     }
                   1576:     return;
                   1577: }
1.22      www      1578: 
1.32      matthew  1579: function changed_text(choice,currentform) {
                   1580:     var choicearg = choice + 'arg';
                   1581:     if (currentform.elements[choicearg].value !='') {
1.80      albertel 1582:         $Javascript_toUpperCase
1.32      matthew  1583:         // clear old field
                   1584:         if ((current.argfield != choicearg) && (current.argfield != null)) {
                   1585:             currentform.elements[current.argfield].value = '';
                   1586:         }
                   1587:         current.argfield = choicearg;
                   1588:     }
                   1589:     set_auth_radio_buttons(choice,currentform);
                   1590:     return;
1.20      www      1591: }
1.32      matthew  1592: 
                   1593: function set_auth_radio_buttons(newvalue,currentform) {
                   1594:     var i=0;
                   1595:     while (i < currentform.login.length) {
                   1596:         if (currentform.login[i].value == newvalue) { break; }
                   1597:         i++;
                   1598:     }
                   1599:     if (i == currentform.login.length) {
                   1600:         return;
                   1601:     }
                   1602:     current.radiovalue = newvalue;
                   1603:     currentform.login[i].checked = true;
                   1604:     return;
                   1605: }
                   1606: END
                   1607:     return $result;
                   1608: }
                   1609: 
                   1610: sub authform_authorwarning{
                   1611:     my $result='';
1.144     matthew  1612:     $result='<i>'.
                   1613:         &mt('As a general rule, only authors or co-authors should be '.
                   1614:             'filesystem authenticated '.
                   1615:             '(which allows access to the server filesystem).')."</i>\n";
1.32      matthew  1616:     return $result;
                   1617: }
                   1618: 
                   1619: sub authform_nochange{  
                   1620:     my %in = (
                   1621:               formname => 'document.cu',
                   1622:               kerb_def_dom => 'MSU.EDU',
                   1623:               @_,
                   1624:           );
1.281     albertel 1625:     my $result = '<label>'.&mt('[_1] Do not change login data',
1.144     matthew  1626:                      '<input type="radio" name="login" value="nochange" '.
                   1627:                      'checked="checked" onclick="'.
1.281     albertel 1628:             "javascript:changed_radio('nochange',$in{'formname'});".'" />').
                   1629: 	    '</label>';
1.32      matthew  1630:     return $result;
                   1631: }
                   1632: 
                   1633: sub authform_kerberos{  
                   1634:     my %in = (
                   1635:               formname => 'document.cu',
                   1636:               kerb_def_dom => 'MSU.EDU',
1.80      albertel 1637:               kerb_def_auth => 'krb4',
1.32      matthew  1638:               @_,
                   1639:               );
1.165     raeburn  1640:     my ($check4,$check5,$krbarg);
1.80      albertel 1641:     if ($in{'kerb_def_auth'} eq 'krb5') {
                   1642:        $check5 = " checked=\"on\"";
                   1643:     } else {
                   1644:        $check4 = " checked=\"on\"";
                   1645:     }
1.165     raeburn  1646:     $krbarg = $in{'kerb_def_dom'};
                   1647: 
                   1648:     my $krbcheck = "";
                   1649:     if ( grep/^curr_authtype$/,(keys %in) ) {
                   1650:         if ($in{'curr_authtype'} =~ m/^krb/) {
                   1651:             $krbcheck = " checked=\"on\"";
                   1652:             if ( grep/^curr_autharg$/,(keys %in) ) {
                   1653:                 $krbarg = $in{'curr_autharg'};
                   1654:             }
                   1655:         }
                   1656:     }
                   1657: 
1.144     matthew  1658:     my $jscall = "javascript:changed_radio('krb',$in{'formname'});";
                   1659:     my $result .= &mt
                   1660:         ('[_1] Kerberos authenticated with domain [_2] '.
1.281     albertel 1661:          '[_3] Version 4 [_4] Version 5 [_5]',
                   1662:          '<label><input type="radio" name="login" value="krb" '.
1.165     raeburn  1663:              'onclick="'.$jscall.'" onchange="'.$jscall.'"'.$krbcheck.' />',
1.281     albertel 1664:          '</label><input type="text" size="10" name="krbarg" '.
1.165     raeburn  1665:              'value="'.$krbarg.'" '.
1.144     matthew  1666:              'onchange="'.$jscall.'" />',
1.281     albertel 1667:          '<label><input type="radio" name="krbver" value="4" '.$check4.' />',
                   1668:          '</label><label><input type="radio" name="krbver" value="5" '.$check5.' />',
                   1669: 	 '</label>');
1.32      matthew  1670:     return $result;
                   1671: }
                   1672: 
                   1673: sub authform_internal{  
                   1674:     my %args = (
                   1675:                 formname => 'document.cu',
                   1676:                 kerb_def_dom => 'MSU.EDU',
                   1677:                 @_,
                   1678:                 );
1.165     raeburn  1679: 
                   1680:     my $intcheck = "";
                   1681:     my $intarg = 'value=""';
                   1682:     if ( grep/^curr_authtype$/,(keys %args) ) {
                   1683:         if ($args{'curr_authtype'} eq 'int') {
                   1684:             $intcheck = " checked=\"on\"";
                   1685:             if ( grep/^curr_autharg$/,(keys %args) ) {
                   1686:                 $intarg = "value=\"$args{'curr_autharg'}\"";
                   1687:             }
                   1688:         }
                   1689:     }
                   1690: 
1.144     matthew  1691:     my $jscall = "javascript:changed_radio('int',$args{'formname'});";
                   1692:     my $result.=&mt
                   1693:         ('[_1] Internally authenticated (with initial password [_2])',
1.281     albertel 1694:          '<label><input type="radio" name="login" value="int" '.$intcheck.
1.165     raeburn  1695:              ' onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1.281     albertel 1696:          '</label><input type="text" size="10" name="intarg" '.$intarg.
1.165     raeburn  1697:              ' onchange="'.$jscall.'" />');
1.32      matthew  1698:     return $result;
                   1699: }
                   1700: 
                   1701: sub authform_local{  
                   1702:     my %in = (
                   1703:               formname => 'document.cu',
                   1704:               kerb_def_dom => 'MSU.EDU',
                   1705:               @_,
                   1706:               );
1.165     raeburn  1707: 
                   1708:     my $loccheck = "";
                   1709:     my $locarg = 'value=""';
                   1710:     if ( grep/^curr_authtype$/,(keys %in) ) {
                   1711:         if ($in{'curr_authtype'} eq 'loc') {
                   1712:             $loccheck = " checked=\"on\"";
                   1713:             if ( grep/^curr_autharg$/,(keys %in) ) {
                   1714:                 $locarg = "value=\"$in{'curr_autharg'}\"";
                   1715:             }
                   1716:         }
                   1717:     }
                   1718: 
1.144     matthew  1719:     my $jscall = "javascript:changed_radio('loc',$in{'formname'});";
1.160     matthew  1720:     my $result.=&mt('[_1] Local Authentication with argument [_2]',
1.281     albertel 1721:                     '<label><input type="radio" name="login" value="loc" '.$loccheck.
1.165     raeburn  1722:                         ' onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1.281     albertel 1723:                     '</label><input type="text" size="10" name="locarg" '.$locarg.
1.165     raeburn  1724:                         ' onchange="'.$jscall.'" />');
1.32      matthew  1725:     return $result;
                   1726: }
                   1727: 
                   1728: sub authform_filesystem{  
                   1729:     my %in = (
                   1730:               formname => 'document.cu',
                   1731:               kerb_def_dom => 'MSU.EDU',
                   1732:               @_,
                   1733:               );
1.144     matthew  1734:     my $jscall = "javascript:changed_radio('fsys',$in{'formname'});";
                   1735:     my $result.= &mt
                   1736:         ('[_1] Filesystem Authenticated (with initial password [_2])',
1.281     albertel 1737:          '<label><input type="radio" name="login" value="fsys" '.
1.144     matthew  1738:          'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1.281     albertel 1739:          '</label><input type="text" size="10" name="fsysarg" value="" '.
1.144     matthew  1740:                   'onchange="'.$jscall.'" />');
1.32      matthew  1741:     return $result;
                   1742: }
                   1743: 
1.80      albertel 1744: ###############################################################
                   1745: ##    Get Authentication Defaults for Domain                 ##
                   1746: ###############################################################
                   1747: 
                   1748: =pod
                   1749: 
1.112     bowersj2 1750: =head1 Domains and Authentication
                   1751: 
                   1752: Returns default authentication type and an associated argument as
                   1753: listed in file 'domain.tab'.
                   1754: 
                   1755: =over 4
                   1756: 
                   1757: =item * get_auth_defaults
1.80      albertel 1758: 
                   1759: get_auth_defaults($target_domain) returns the default authentication
                   1760: type and an associated argument (initial password or a kerberos domain).
                   1761: These values are stored in lonTabs/domain.tab
                   1762: 
                   1763: ($def_auth, $def_arg) = &get_auth_defaults($target_domain);
                   1764: 
                   1765: If target_domain is not found in domain.tab, returns nothing ('').
                   1766: 
                   1767: =cut
                   1768: 
                   1769: #-------------------------------------------
                   1770: sub get_auth_defaults {
                   1771:     my $domain=shift;
                   1772:     return ($Apache::lonnet::domain_auth_def{$domain},$Apache::lonnet::domain_auth_arg_def{$domain});
                   1773: }
                   1774: ###############################################################
                   1775: ##   End Get Authentication Defaults for Domain              ##
                   1776: ###############################################################
                   1777: 
                   1778: ###############################################################
                   1779: ##    Get Kerberos Defaults for Domain                 ##
                   1780: ###############################################################
                   1781: ##
                   1782: ## Returns default kerberos version and an associated argument
                   1783: ## as listed in file domain.tab. If not listed, provides
                   1784: ## appropriate default domain and kerberos version.
                   1785: ##
                   1786: #-------------------------------------------
                   1787: 
                   1788: =pod
                   1789: 
1.112     bowersj2 1790: =item * get_kerberos_defaults
1.80      albertel 1791: 
                   1792: get_kerberos_defaults($target_domain) returns the default kerberos
                   1793: version and domain. If not found in domain.tabs, it defaults to
                   1794: version 4 and the domain of the server.
                   1795: 
                   1796: ($def_version, $def_krb_domain) = &get_kerberos_defaults($target_domain);
                   1797: 
                   1798: =cut
                   1799: 
                   1800: #-------------------------------------------
                   1801: sub get_kerberos_defaults {
                   1802:     my $domain=shift;
                   1803:     my ($krbdef,$krbdefdom) =
                   1804:         &Apache::loncommon::get_auth_defaults($domain);
                   1805:     unless ($krbdef =~/^krb/ && $krbdefdom) {
                   1806:         $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
                   1807:         my $krbdefdom=$1;
                   1808:         $krbdefdom=~tr/a-z/A-Z/;
                   1809:         $krbdef = "krb4";
                   1810:     }
                   1811:     return ($krbdef,$krbdefdom);
                   1812: }
1.112     bowersj2 1813: 
                   1814: =pod
                   1815: 
                   1816: =back
                   1817: 
                   1818: =cut
1.32      matthew  1819: 
1.46      matthew  1820: ###############################################################
                   1821: ##                Thesaurus Functions                        ##
                   1822: ###############################################################
1.20      www      1823: 
1.46      matthew  1824: =pod
1.20      www      1825: 
1.112     bowersj2 1826: =head1 Thesaurus Functions
                   1827: 
                   1828: =over 4
                   1829: 
                   1830: =item * initialize_keywords
1.46      matthew  1831: 
                   1832: Initializes the package variable %Keywords if it is empty.  Uses the
                   1833: package variable $thesaurus_db_file.
                   1834: 
                   1835: =cut
                   1836: 
                   1837: ###################################################
                   1838: 
                   1839: sub initialize_keywords {
                   1840:     return 1 if (scalar keys(%Keywords));
                   1841:     # If we are here, %Keywords is empty, so fill it up
                   1842:     #   Make sure the file we need exists...
                   1843:     if (! -e $thesaurus_db_file) {
                   1844:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file".
                   1845:                                  " failed because it does not exist");
                   1846:         return 0;
                   1847:     }
                   1848:     #   Set up the hash as a database
                   1849:     my %thesaurus_db;
                   1850:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 1851:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  1852:         &Apache::lonnet::logthis("Could not tie \%thesaurus_db to ".
                   1853:                                  $thesaurus_db_file);
                   1854:         return 0;
                   1855:     } 
                   1856:     #  Get the average number of appearances of a word.
                   1857:     my $avecount = $thesaurus_db{'average.count'};
                   1858:     #  Put keywords (those that appear > average) into %Keywords
                   1859:     while (my ($word,$data)=each (%thesaurus_db)) {
                   1860:         my ($count,undef) = split /:/,$data;
                   1861:         $Keywords{$word}++ if ($count > $avecount);
                   1862:     }
                   1863:     untie %thesaurus_db;
                   1864:     # Remove special values from %Keywords.
1.356     albertel 1865:     foreach my $value ('total.count','average.count') {
                   1866:         delete($Keywords{$value}) if (exists($Keywords{$value}));
1.46      matthew  1867:     }
                   1868:     return 1;
                   1869: }
                   1870: 
                   1871: ###################################################
                   1872: 
                   1873: =pod
                   1874: 
1.112     bowersj2 1875: =item * keyword($word)
1.46      matthew  1876: 
                   1877: Returns true if $word is a keyword.  A keyword is a word that appears more 
                   1878: than the average number of times in the thesaurus database.  Calls 
                   1879: &initialize_keywords
                   1880: 
                   1881: =cut
                   1882: 
                   1883: ###################################################
1.20      www      1884: 
                   1885: sub keyword {
1.46      matthew  1886:     return if (!&initialize_keywords());
                   1887:     my $word=lc(shift());
                   1888:     $word=~s/\W//g;
                   1889:     return exists($Keywords{$word});
1.20      www      1890: }
1.46      matthew  1891: 
                   1892: ###############################################################
                   1893: 
                   1894: =pod 
1.20      www      1895: 
1.112     bowersj2 1896: =item * get_related_words
1.46      matthew  1897: 
1.160     matthew  1898: Look up a word in the thesaurus.  Takes a scalar argument and returns
1.46      matthew  1899: an array of words.  If the keyword is not in the thesaurus, an empty array
                   1900: will be returned.  The order of the words returned is determined by the
                   1901: database which holds them.
                   1902: 
                   1903: Uses global $thesaurus_db_file.
                   1904: 
                   1905: =cut
                   1906: 
                   1907: ###############################################################
                   1908: sub get_related_words {
                   1909:     my $keyword = shift;
                   1910:     my %thesaurus_db;
                   1911:     if (! -e $thesaurus_db_file) {
                   1912:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file ".
                   1913:                                  "failed because the file does not exist");
                   1914:         return ();
                   1915:     }
                   1916:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 1917:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  1918:         return ();
                   1919:     } 
                   1920:     my @Words=();
                   1921:     if (exists($thesaurus_db{$keyword})) {
1.356     albertel 1922: 	# The first element is the number of times
                   1923: 	# the word appears.  We do not need it now.
                   1924: 	(undef,@Words) = (split(/:/,$thesaurus_db{$keyword}));
1.46      matthew  1925:         for (my $i=0;$i<=$#Words;$i++) {
1.356     albertel 1926:             ($Words[$i],undef)= split(/\,/,$Words[$i]);
1.20      www      1927:         }
                   1928:     }
1.46      matthew  1929:     untie %thesaurus_db;
                   1930:     return @Words;
1.14      harris41 1931: }
1.46      matthew  1932: 
1.112     bowersj2 1933: =pod
                   1934: 
                   1935: =back
                   1936: 
                   1937: =cut
1.61      www      1938: 
                   1939: # -------------------------------------------------------------- Plaintext name
1.81      albertel 1940: =pod
                   1941: 
1.112     bowersj2 1942: =head1 User Name Functions
                   1943: 
                   1944: =over 4
                   1945: 
1.226     albertel 1946: =item * plainname($uname,$udom,$first)
1.81      albertel 1947: 
1.112     bowersj2 1948: Takes a users logon name and returns it as a string in
1.226     albertel 1949: "first middle last generation" form 
                   1950: if $first is set to 'lastname' then it returns it as
                   1951: 'lastname generation, firstname middlename' if their is a lastname
1.81      albertel 1952: 
                   1953: =cut
1.61      www      1954: 
1.295     www      1955: 
1.81      albertel 1956: ###############################################################
1.61      www      1957: sub plainname {
1.226     albertel 1958:     my ($uname,$udom,$first)=@_;
1.295     www      1959:     my %names=&getnames($uname,$udom);
1.226     albertel 1960:     my $name=&Apache::lonnet::format_name($names{'firstname'},
                   1961: 					  $names{'middlename'},
                   1962: 					  $names{'lastname'},
                   1963: 					  $names{'generation'},$first);
                   1964:     $name=~s/^\s+//;
1.62      www      1965:     $name=~s/\s+$//;
                   1966:     $name=~s/\s+/ /g;
1.353     albertel 1967:     if ($name !~ /\S/) { $name=$uname.':'.$udom; }
1.62      www      1968:     return $name;
1.61      www      1969: }
1.66      www      1970: 
                   1971: # -------------------------------------------------------------------- Nickname
1.81      albertel 1972: =pod
                   1973: 
1.112     bowersj2 1974: =item * nickname($uname,$udom)
1.81      albertel 1975: 
                   1976: Gets a users name and returns it as a string as
                   1977: 
                   1978: "&quot;nickname&quot;"
1.66      www      1979: 
1.81      albertel 1980: if the user has a nickname or
                   1981: 
                   1982: "first middle last generation"
                   1983: 
                   1984: if the user does not
                   1985: 
                   1986: =cut
1.66      www      1987: 
                   1988: sub nickname {
                   1989:     my ($uname,$udom)=@_;
1.295     www      1990:     my %names=&getnames($uname,$udom);
1.68      albertel 1991:     my $name=$names{'nickname'};
1.66      www      1992:     if ($name) {
                   1993:        $name='&quot;'.$name.'&quot;'; 
                   1994:     } else {
                   1995:        $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
                   1996: 	     $names{'lastname'}.' '.$names{'generation'};
                   1997:        $name=~s/\s+$//;
                   1998:        $name=~s/\s+/ /g;
                   1999:     }
                   2000:     return $name;
                   2001: }
                   2002: 
1.295     www      2003: sub getnames {
                   2004:     my ($uname,$udom)=@_;
                   2005:     my $id=$uname.':'.$udom;
                   2006:     my ($names,$cached)=&Apache::lonnet::is_cached_new('namescache',$id);
                   2007:     if ($cached) {
                   2008: 	return %{$names};
                   2009:     } else {
                   2010: 	my %loadnames=&Apache::lonnet::get('environment',
                   2011:                     ['firstname','middlename','lastname','generation','nickname'],
                   2012: 					 $udom,$uname);
                   2013: 	&Apache::lonnet::do_cache_new('namescache',$id,\%loadnames);
                   2014: 	return %loadnames;
                   2015:     }
                   2016: }
1.61      www      2017: 
                   2018: # ------------------------------------------------------------------ Screenname
1.81      albertel 2019: 
                   2020: =pod
                   2021: 
1.112     bowersj2 2022: =item * screenname($uname,$udom)
1.81      albertel 2023: 
                   2024: Gets a users screenname and returns it as a string
                   2025: 
                   2026: =cut
1.61      www      2027: 
                   2028: sub screenname {
                   2029:     my ($uname,$udom)=@_;
1.258     albertel 2030:     if ($uname eq $env{'user.name'} &&
                   2031: 	$udom eq $env{'user.domain'}) {return $env{'environment.screenname'};}
1.212     albertel 2032:     my %names=&Apache::lonnet::get('environment',['screenname'],$udom,$uname);
1.68      albertel 2033:     return $names{'screenname'};
1.62      www      2034: }
                   2035: 
1.212     albertel 2036: 
1.62      www      2037: # ------------------------------------------------------------- Message Wrapper
                   2038: 
                   2039: sub messagewrapper {
1.369     www      2040:     my ($link,$username,$domain,$subject,$text)=@_;
1.62      www      2041:     return 
1.200     matthew  2042:         '<a href="/adm/email?compose=individual&'.
1.369     www      2043:         'recname='.$username.'&recdom='.$domain.
                   2044: 	'&subject='.&escape($subject).'&text='.&escape($text).'" '.
1.200     matthew  2045:         'title="'.&mt('Send message').'">'.$link.'</a>';
1.74      www      2046: }
                   2047: # --------------------------------------------------------------- Notes Wrapper
                   2048: 
                   2049: sub noteswrapper {
                   2050:     my ($link,$un,$do)=@_;
                   2051:     return 
                   2052: "<a href='/adm/email?recordftf=retrieve&recname=$un&recdom=$do'>$link</a>";
1.62      www      2053: }
                   2054: # ------------------------------------------------------------- Aboutme Wrapper
                   2055: 
                   2056: sub aboutmewrapper {
1.166     www      2057:     my ($link,$username,$domain,$target)=@_;
1.205     www      2058:     return '<a href="/adm/'.$domain.'/'.$username.'/aboutme"'.
1.200     matthew  2059: 	($target?' target="$target"':'').' title="'.&mt('View this users personal page').'">'.$link.'</a>';
1.62      www      2060: }
                   2061: 
                   2062: # ------------------------------------------------------------ Syllabus Wrapper
                   2063: 
                   2064: 
                   2065: sub syllabuswrapper {
1.109     matthew  2066:     my ($linktext,$coursedir,$domain,$fontcolor)=@_;
                   2067:     if ($fontcolor) { 
                   2068:         $linktext='<font color="'.$fontcolor.'">'.$linktext.'</font>'; 
                   2069:     }
1.208     matthew  2070:     return qq{<a href="/public/$domain/$coursedir/syllabus">$linktext</a>};
1.61      www      2071: }
1.14      harris41 2072: 
1.208     matthew  2073: sub track_student_link {
1.268     albertel 2074:     my ($linktext,$sname,$sdom,$target,$start) = @_;
                   2075:     my $link ="/adm/trackstudent?";
1.208     matthew  2076:     my $title = 'View recent activity';
                   2077:     if (defined($sname) && $sname !~ /^\s*$/ &&
                   2078:         defined($sdom)  && $sdom  !~ /^\s*$/) {
1.268     albertel 2079:         $link .= "selected_student=$sname:$sdom";
1.208     matthew  2080:         $title .= ' of this student';
1.268     albertel 2081:     } 
1.208     matthew  2082:     if (defined($target) && $target !~ /^\s*$/) {
                   2083:         $target = qq{target="$target"};
                   2084:     } else {
                   2085:         $target = '';
                   2086:     }
1.268     albertel 2087:     if ($start) { $link.='&amp;start='.$start; }
1.208     matthew  2088:     return qq{<a href="$link" title="$title" $target>$linktext</a>};
                   2089: }
                   2090: 
1.112     bowersj2 2091: =pod
                   2092: 
                   2093: =back
                   2094: 
                   2095: =head1 Access .tab File Data
                   2096: 
                   2097: =over 4
                   2098: 
                   2099: =item * languageids() 
                   2100: 
                   2101: returns list of all language ids
                   2102: 
                   2103: =cut
                   2104: 
1.14      harris41 2105: sub languageids {
1.16      harris41 2106:     return sort(keys(%language));
1.14      harris41 2107: }
                   2108: 
1.112     bowersj2 2109: =pod
                   2110: 
                   2111: =item * languagedescription() 
                   2112: 
                   2113: returns description of a specified language id
                   2114: 
                   2115: =cut
                   2116: 
1.14      harris41 2117: sub languagedescription {
1.125     www      2118:     my $code=shift;
                   2119:     return  ($supported_language{$code}?'* ':'').
                   2120:             $language{$code}.
1.126     www      2121: 	    ($supported_language{$code}?' ('.&mt('interface available').')':'');
1.145     www      2122: }
                   2123: 
                   2124: sub plainlanguagedescription {
                   2125:     my $code=shift;
                   2126:     return $language{$code};
                   2127: }
                   2128: 
                   2129: sub supportedlanguagecode {
                   2130:     my $code=shift;
                   2131:     return $supported_language{$code};
1.97      www      2132: }
                   2133: 
1.112     bowersj2 2134: =pod
                   2135: 
                   2136: =item * copyrightids() 
                   2137: 
                   2138: returns list of all copyrights
                   2139: 
                   2140: =cut
                   2141: 
                   2142: sub copyrightids {
                   2143:     return sort(keys(%cprtag));
                   2144: }
                   2145: 
                   2146: =pod
                   2147: 
                   2148: =item * copyrightdescription() 
                   2149: 
                   2150: returns description of a specified copyright id
                   2151: 
                   2152: =cut
                   2153: 
                   2154: sub copyrightdescription {
1.166     www      2155:     return &mt($cprtag{shift(@_)});
1.112     bowersj2 2156: }
1.197     matthew  2157: 
                   2158: =pod
                   2159: 
1.192     taceyjo1 2160: =item * source_copyrightids() 
                   2161: 
                   2162: returns list of all source copyrights
                   2163: 
                   2164: =cut
                   2165: 
                   2166: sub source_copyrightids {
                   2167:     return sort(keys(%scprtag));
                   2168: }
                   2169: 
                   2170: =pod
                   2171: 
                   2172: =item * source_copyrightdescription() 
                   2173: 
                   2174: returns description of a specified source copyright id
                   2175: 
                   2176: =cut
                   2177: 
                   2178: sub source_copyrightdescription {
                   2179:     return &mt($scprtag{shift(@_)});
                   2180: }
1.112     bowersj2 2181: 
                   2182: =pod
                   2183: 
                   2184: =item * filecategories() 
                   2185: 
                   2186: returns list of all file categories
                   2187: 
                   2188: =cut
                   2189: 
                   2190: sub filecategories {
                   2191:     return sort(keys(%category_extensions));
                   2192: }
                   2193: 
                   2194: =pod
                   2195: 
                   2196: =item * filecategorytypes() 
                   2197: 
                   2198: returns list of file types belonging to a given file
                   2199: category
                   2200: 
                   2201: =cut
                   2202: 
                   2203: sub filecategorytypes {
1.356     albertel 2204:     my ($cat) = @_;
                   2205:     return @{$category_extensions{lc($cat)}};
1.112     bowersj2 2206: }
                   2207: 
                   2208: =pod
                   2209: 
                   2210: =item * fileembstyle() 
                   2211: 
                   2212: returns embedding style for a specified file type
                   2213: 
                   2214: =cut
                   2215: 
                   2216: sub fileembstyle {
                   2217:     return $fe{lc(shift(@_))};
1.169     www      2218: }
                   2219: 
1.351     www      2220: sub filemimetype {
                   2221:     return $fm{lc(shift(@_))};
                   2222: }
                   2223: 
1.169     www      2224: 
                   2225: sub filecategoryselect {
                   2226:     my ($name,$value)=@_;
1.189     matthew  2227:     return &select_form($value,$name,
1.169     www      2228: 			'' => &mt('Any category'),
                   2229: 			map { $_,$_ } sort(keys(%category_extensions)));
1.112     bowersj2 2230: }
                   2231: 
                   2232: =pod
                   2233: 
                   2234: =item * filedescription() 
                   2235: 
                   2236: returns description for a specified file type
                   2237: 
                   2238: =cut
                   2239: 
                   2240: sub filedescription {
1.188     matthew  2241:     my $file_description = $fd{lc(shift())};
                   2242:     $file_description =~ s:([\[\]]):~$1:g;
                   2243:     return &mt($file_description);
1.112     bowersj2 2244: }
                   2245: 
                   2246: =pod
                   2247: 
                   2248: =item * filedescriptionex() 
                   2249: 
                   2250: returns description for a specified file type with
                   2251: extra formatting
                   2252: 
                   2253: =cut
                   2254: 
                   2255: sub filedescriptionex {
                   2256:     my $ex=shift;
1.188     matthew  2257:     my $file_description = $fd{lc($ex)};
                   2258:     $file_description =~ s:([\[\]]):~$1:g;
                   2259:     return '.'.$ex.' '.&mt($file_description);
1.112     bowersj2 2260: }
                   2261: 
                   2262: # End of .tab access
                   2263: =pod
                   2264: 
                   2265: =back
                   2266: 
                   2267: =cut
                   2268: 
                   2269: # ------------------------------------------------------------------ File Types
                   2270: sub fileextensions {
                   2271:     return sort(keys(%fe));
                   2272: }
                   2273: 
1.97      www      2274: # ----------------------------------------------------------- Display Languages
                   2275: # returns a hash with all desired display languages
                   2276: #
                   2277: 
                   2278: sub display_languages {
                   2279:     my %languages=();
1.356     albertel 2280:     foreach my $lang (&preferred_languages()) {
                   2281: 	$languages{$lang}=1;
1.97      www      2282:     }
                   2283:     &get_unprocessed_cgi($ENV{'QUERY_STRING'},['displaylanguage']);
1.258     albertel 2284:     if ($env{'form.displaylanguage'}) {
1.356     albertel 2285: 	foreach my $lang (split(/\s*(\,|\;|\:)\s*/,$env{'form.displaylanguage'})) {
                   2286: 	    $languages{$lang}=1;
1.97      www      2287:         }
                   2288:     }
                   2289:     return %languages;
1.14      harris41 2290: }
                   2291: 
1.117     www      2292: sub preferred_languages {
                   2293:     my @languages=();
1.258     albertel 2294:     if ($env{'course.'.$env{'request.course.id'}.'.languages'}) {
1.117     www      2295: 	@languages=(@languages,split(/\s*(\,|\;|\:)\s*/,
1.258     albertel 2296: 	         $env{'course.'.$env{'request.course.id'}.'.languages'}));
1.177     www      2297:     }
1.258     albertel 2298:     if ($env{'environment.languages'}) {
                   2299: 	@languages=split(/\s*(\,|\;|\:)\s*/,$env{'environment.languages'});
1.118     www      2300:     }
1.162     www      2301:     my $browser=(split(/\;/,$ENV{'HTTP_ACCEPT_LANGUAGE'}))[0];
                   2302:     if ($browser) {
                   2303: 	@languages=(@languages,split(/\s*(\,|\;|\:)\s*/,$browser));
                   2304:     }
1.258     albertel 2305:     if ($Apache::lonnet::domain_lang_def{$env{'user.domain'}}) {
1.118     www      2306: 	@languages=(@languages,
1.258     albertel 2307: 		$Apache::lonnet::domain_lang_def{$env{'user.domain'}});
1.118     www      2308:     }
1.258     albertel 2309:     if ($Apache::lonnet::domain_lang_def{$env{'request.role.domain'}}) {
1.118     www      2310: 	@languages=(@languages,
1.258     albertel 2311: 		$Apache::lonnet::domain_lang_def{$env{'request.role.domain'}});
1.118     www      2312:     }
                   2313:     if ($Apache::lonnet::domain_lang_def{
                   2314: 	                          $Apache::lonnet::perlvar{'lonDefDomain'}}) {
                   2315: 	@languages=(@languages,
                   2316: 		$Apache::lonnet::domain_lang_def{
                   2317:                                   $Apache::lonnet::perlvar{'lonDefDomain'}});
                   2318:     }
                   2319: # turn "en-ca" into "en-ca,en"
                   2320:     my @genlanguages;
1.356     albertel 2321:     foreach my $lang (@languages) {
                   2322: 	unless ($lang=~/\w/) { next; }
                   2323: 	push (@genlanguages,$lang);
                   2324: 	if ($lang=~/(\-|\_)/) {
                   2325: 	    push(@genlanguages,(split(/(\-|\_)/,$lang))[0]);
1.118     www      2326: 	}
                   2327:     }
                   2328:     return @genlanguages;
1.117     www      2329: }
                   2330: 
1.112     bowersj2 2331: ###############################################################
                   2332: ##               Student Answer Attempts                     ##
                   2333: ###############################################################
                   2334: 
                   2335: =pod
                   2336: 
                   2337: =head1 Alternate Problem Views
                   2338: 
                   2339: =over 4
                   2340: 
                   2341: =item * get_previous_attempt($symb, $username, $domain, $course,
                   2342:     $getattempt, $regexp, $gradesub)
                   2343: 
                   2344: Return string with previous attempt on problem. Arguments:
                   2345: 
                   2346: =over 4
                   2347: 
                   2348: =item * $symb: Problem, including path
                   2349: 
                   2350: =item * $username: username of the desired student
                   2351: 
                   2352: =item * $domain: domain of the desired student
1.14      harris41 2353: 
1.112     bowersj2 2354: =item * $course: Course ID
1.14      harris41 2355: 
1.112     bowersj2 2356: =item * $getattempt: Leave blank for all attempts, otherwise put
                   2357:     something
1.14      harris41 2358: 
1.112     bowersj2 2359: =item * $regexp: if string matches this regexp, the string will be
                   2360:     sent to $gradesub
1.14      harris41 2361: 
1.112     bowersj2 2362: =item * $gradesub: routine that processes the string if it matches $regexp
1.14      harris41 2363: 
1.112     bowersj2 2364: =back
1.14      harris41 2365: 
1.112     bowersj2 2366: The output string is a table containing all desired attempts, if any.
1.16      harris41 2367: 
1.112     bowersj2 2368: =cut
1.1       albertel 2369: 
                   2370: sub get_previous_attempt {
1.43      ng       2371:   my ($symb,$username,$domain,$course,$getattempt,$regexp,$gradesub)=@_;
1.1       albertel 2372:   my $prevattempts='';
1.43      ng       2373:   no strict 'refs';
1.1       albertel 2374:   if ($symb) {
1.3       albertel 2375:     my (%returnhash)=
                   2376:       &Apache::lonnet::restore($symb,$course,$domain,$username);
1.1       albertel 2377:     if ($returnhash{'version'}) {
                   2378:       my %lasthash=();
                   2379:       my $version;
                   2380:       for ($version=1;$version<=$returnhash{'version'};$version++) {
1.356     albertel 2381:         foreach my $key (sort(split(/\:/,$returnhash{$version.':keys'}))) {
                   2382: 	  $lasthash{$key}=$returnhash{$version.':'.$key};
1.19      harris41 2383:         }
1.1       albertel 2384:       }
1.43      ng       2385:       $prevattempts='<table border="0" width="100%"><tr><td bgcolor="#777777">';
1.40      ng       2386:       $prevattempts.='<table border="0" width="100%"><tr bgcolor="#e6ffff"><td>History</td>';
1.356     albertel 2387:       foreach my $key (sort(keys(%lasthash))) {
                   2388: 	my ($ign,@parts) = split(/\./,$key);
1.41      ng       2389: 	if ($#parts > 0) {
1.31      albertel 2390: 	  my $data=$parts[-1];
                   2391: 	  pop(@parts);
1.40      ng       2392: 	  $prevattempts.='<td>Part '.join('.',@parts).'<br />'.$data.'&nbsp;</td>';
1.31      albertel 2393: 	} else {
1.41      ng       2394: 	  if ($#parts == 0) {
                   2395: 	    $prevattempts.='<th>'.$parts[0].'</th>';
                   2396: 	  } else {
                   2397: 	    $prevattempts.='<th>'.$ign.'</th>';
                   2398: 	  }
1.31      albertel 2399: 	}
1.16      harris41 2400:       }
1.40      ng       2401:       if ($getattempt eq '') {
                   2402: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
                   2403: 	  $prevattempts.='</tr><tr bgcolor="#ffffe6"><td>Transaction '.$version.'</td>';
1.356     albertel 2404: 	    foreach my $key (sort(keys(%lasthash))) {
1.40      ng       2405: 	       my $value;
1.356     albertel 2406: 	       if ($key =~ /timestamp/) {
                   2407: 		  $value=scalar(localtime($returnhash{$version.':'.$key}));
1.40      ng       2408: 	       } else {
1.356     albertel 2409: 		  $value=$returnhash{$version.':'.$key};
1.40      ng       2410: 	       }
1.369     www      2411: 	       $prevattempts.='<td>'.&unescape($value).'&nbsp;</td>';   
1.40      ng       2412: 	    }
                   2413: 	 }
1.1       albertel 2414:       }
1.40      ng       2415:       $prevattempts.='</tr><tr bgcolor="#ffffe6"><td>Current</td>';
1.356     albertel 2416:       foreach my $key (sort(keys(%lasthash))) {
1.5       albertel 2417: 	my $value;
1.356     albertel 2418: 	if ($key =~ /timestamp/) {
                   2419: 	  $value=scalar(localtime($lasthash{$key}));
1.5       albertel 2420: 	} else {
1.356     albertel 2421: 	  $value=$lasthash{$key};
1.5       albertel 2422: 	}
1.369     www      2423: 	$value=&unescape($value);
1.356     albertel 2424: 	if ($key =~/$regexp$/ && (defined &$gradesub)) {$value = &$gradesub($value)}
1.40      ng       2425: 	$prevattempts.='<td>'.$value.'&nbsp;</td>';
1.16      harris41 2426:       }
1.40      ng       2427:       $prevattempts.='</tr></table></td></tr></table>';
1.1       albertel 2428:     } else {
                   2429:       $prevattempts='Nothing submitted - no attempts.';
                   2430:     }
                   2431:   } else {
                   2432:     $prevattempts='No data.';
                   2433:   }
1.10      albertel 2434: }
                   2435: 
1.107     albertel 2436: sub relative_to_absolute {
                   2437:     my ($url,$output)=@_;
                   2438:     my $parser=HTML::TokeParser->new(\$output);
                   2439:     my $token;
                   2440:     my $thisdir=$url;
                   2441:     my @rlinks=();
                   2442:     while ($token=$parser->get_token) {
                   2443: 	if ($token->[0] eq 'S') {
                   2444: 	    if ($token->[1] eq 'a') {
                   2445: 		if ($token->[2]->{'href'}) {
                   2446: 		    $rlinks[$#rlinks+1]=$token->[2]->{'href'};
                   2447: 		}
                   2448: 	    } elsif ($token->[1] eq 'img' || $token->[1] eq 'embed' ) {
                   2449: 		$rlinks[$#rlinks+1]=$token->[2]->{'src'};
                   2450: 	    } elsif ($token->[1] eq 'base') {
                   2451: 		$thisdir=$token->[2]->{'href'};
                   2452: 	    }
                   2453: 	}
                   2454:     }
                   2455:     $thisdir=~s-/[^/]*$--;
1.356     albertel 2456:     foreach my $link (@rlinks) {
                   2457: 	unless (($link=~/^http:\/\//i) ||
                   2458: 		($link=~/^\//) ||
                   2459: 		($link=~/^javascript:/i) ||
                   2460: 		($link=~/^mailto:/i) ||
                   2461: 		($link=~/^\#/)) {
                   2462: 	    my $newlocation=&Apache::lonnet::hreflocation($thisdir,$link);
                   2463: 	    $output=~s/(\"|\'|\=\s*)\Q$link\E(\"|\'|\s|\>)/$1$newlocation$2/;
1.107     albertel 2464: 	}
                   2465:     }
                   2466: # -------------------------------------------------- Deal with Applet codebases
                   2467:     $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
                   2468:     return $output;
                   2469: }
                   2470: 
1.112     bowersj2 2471: =pod
                   2472: 
                   2473: =item * get_student_view
                   2474: 
                   2475: show a snapshot of what student was looking at
                   2476: 
                   2477: =cut
                   2478: 
1.10      albertel 2479: sub get_student_view {
1.186     albertel 2480:   my ($symb,$username,$domain,$courseid,$target,$moreenv) = @_;
1.114     www      2481:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 2482:   my (%form);
1.10      albertel 2483:   my @elements=('symb','courseid','domain','username');
                   2484:   foreach my $element (@elements) {
1.186     albertel 2485:       $form{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 2486:   }
1.186     albertel 2487:   if (defined($moreenv)) {
                   2488:       %form=(%form,%{$moreenv});
                   2489:   }
1.236     albertel 2490:   if (defined($target)) { $form{'grade_target'} = $target; }
1.107     albertel 2491:   $feedurl=&Apache::lonnet::clutter($feedurl);
1.186     albertel 2492:   my $userview=&Apache::lonnet::ssi_body($feedurl,%form);
1.11      albertel 2493:   $userview=~s/\<body[^\>]*\>//gi;
                   2494:   $userview=~s/\<\/body\>//gi;
                   2495:   $userview=~s/\<html\>//gi;
                   2496:   $userview=~s/\<\/html\>//gi;
                   2497:   $userview=~s/\<head\>//gi;
                   2498:   $userview=~s/\<\/head\>//gi;
                   2499:   $userview=~s/action\s*\=/would_be_action\=/gi;
1.107     albertel 2500:   $userview=&relative_to_absolute($feedurl,$userview);
1.11      albertel 2501:   return $userview;
                   2502: }
                   2503: 
1.112     bowersj2 2504: =pod
                   2505: 
                   2506: =item * get_student_answers() 
                   2507: 
                   2508: show a snapshot of how student was answering problem
                   2509: 
                   2510: =cut
                   2511: 
1.11      albertel 2512: sub get_student_answers {
1.100     sakharuk 2513:   my ($symb,$username,$domain,$courseid,%form) = @_;
1.114     www      2514:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 2515:   my (%moreenv);
1.11      albertel 2516:   my @elements=('symb','courseid','domain','username');
                   2517:   foreach my $element (@elements) {
1.186     albertel 2518:     $moreenv{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 2519:   }
1.186     albertel 2520:   $moreenv{'grade_target'}='answer';
                   2521:   %moreenv=(%form,%moreenv);
                   2522:   my $userview=&Apache::lonnet::ssi('/res/'.$feedurl,%moreenv);
1.10      albertel 2523:   return $userview;
1.1       albertel 2524: }
1.116     albertel 2525: 
                   2526: =pod
                   2527: 
                   2528: =item * &submlink()
                   2529: 
1.242     albertel 2530: Inputs: $text $uname $udom $symb $target
1.116     albertel 2531: 
                   2532: Returns: A link to grades.pm such as to see the SUBM view of a student
                   2533: 
                   2534: =cut
                   2535: 
                   2536: ###############################################
                   2537: sub submlink {
1.242     albertel 2538:     my ($text,$uname,$udom,$symb,$target)=@_;
1.116     albertel 2539:     if (!($uname && $udom)) {
                   2540: 	(my $cursymb, my $courseid,$udom,$uname)=
                   2541: 	    &Apache::lonxml::whichuser($symb);
                   2542: 	if (!$symb) { $symb=$cursymb; }
                   2543:     }
1.254     matthew  2544:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      2545:     $symb=&escape($symb);
1.242     albertel 2546:     if ($target) { $target="target=\"$target\""; }
                   2547:     return '<a href="/adm/grades?&command=submission&'.
                   2548: 	'symb='.$symb.'&student='.$uname.
                   2549: 	'&userdom='.$udom.'" '.$target.'>'.$text.'</a>';
                   2550: }
                   2551: ##############################################
                   2552: 
                   2553: =pod
                   2554: 
                   2555: =item * &pgrdlink()
                   2556: 
                   2557: Inputs: $text $uname $udom $symb $target
                   2558: 
                   2559: Returns: A link to grades.pm such as to see the PGRD view of a student
                   2560: 
                   2561: =cut
                   2562: 
                   2563: ###############################################
                   2564: sub pgrdlink {
                   2565:     my $link=&submlink(@_);
                   2566:     $link=~s/(&command=submission)/$1&showgrading=yes/;
                   2567:     return $link;
                   2568: }
                   2569: ##############################################
                   2570: 
                   2571: =pod
                   2572: 
                   2573: =item * &pprmlink()
                   2574: 
                   2575: Inputs: $text $uname $udom $symb $target
                   2576: 
                   2577: Returns: A link to parmset.pm such as to see the PPRM view of a
1.283     albertel 2578: student and a specific resource
1.242     albertel 2579: 
                   2580: =cut
                   2581: 
                   2582: ###############################################
                   2583: sub pprmlink {
                   2584:     my ($text,$uname,$udom,$symb,$target)=@_;
                   2585:     if (!($uname && $udom)) {
                   2586: 	(my $cursymb, my $courseid,$udom,$uname)=
                   2587: 	    &Apache::lonxml::whichuser($symb);
                   2588: 	if (!$symb) { $symb=$cursymb; }
                   2589:     }
1.254     matthew  2590:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      2591:     $symb=&escape($symb);
1.242     albertel 2592:     if ($target) { $target="target=\"$target\""; }
                   2593:     return '<a href="/adm/parmset?&command=set&'.
                   2594: 	'symb='.$symb.'&uname='.$uname.
                   2595: 	'&udom='.$udom.'" '.$target.'>'.$text.'</a>';
1.116     albertel 2596: }
                   2597: ##############################################
1.37      matthew  2598: 
1.112     bowersj2 2599: =pod
                   2600: 
                   2601: =back
                   2602: 
                   2603: =cut
                   2604: 
1.37      matthew  2605: ###############################################
1.51      www      2606: 
                   2607: 
                   2608: sub timehash {
                   2609:     my @ltime=localtime(shift);
                   2610:     return ( 'seconds' => $ltime[0],
                   2611:              'minutes' => $ltime[1],
                   2612:              'hours'   => $ltime[2],
                   2613:              'day'     => $ltime[3],
                   2614:              'month'   => $ltime[4]+1,
                   2615:              'year'    => $ltime[5]+1900,
                   2616:              'weekday' => $ltime[6],
                   2617:              'dayyear' => $ltime[7]+1,
                   2618:              'dlsav'   => $ltime[8] );
                   2619: }
                   2620: 
1.370     www      2621: sub utc_string {
                   2622:     my ($date)=@_;
1.371     www      2623:     return strftime("%Y%m%dT%H%M%SZ",gmtime($date));
1.370     www      2624: }
                   2625: 
1.51      www      2626: sub maketime {
                   2627:     my %th=@_;
                   2628:     return POSIX::mktime(
                   2629:         ($th{'seconds'},$th{'minutes'},$th{'hours'},
1.210     www      2630:          $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,-1));
1.70      www      2631: }
                   2632: 
                   2633: #########################################
1.51      www      2634: 
                   2635: sub findallcourses {
1.355     albertel 2636:     my ($roles) = @_;
                   2637:     my %roles;
                   2638:     if (ref($roles)) { %roles = map { $_ => 1 } @{$roles}; }
1.348     albertel 2639:     my %courses;
1.51      www      2640:     my $now=time;
1.348     albertel 2641:     foreach my $key (keys(%env)) {
                   2642: 	if ( $key=~m{^user\.role\.(\w+)\./(\w+)/(\w+)} ) {
                   2643: 	    my ($role,$domain,$id) = ($1,$2,$3);
                   2644: 	    next if ($role eq 'ca' || $role eq 'aa');
1.355     albertel 2645: 	    next if (%roles && !exists($roles{$role}));
1.354     albertel 2646: 	    my ($starttime,$endtime)=split(/\./,$env{$key});
1.51      www      2647:             my $active=1;
                   2648:             if ($starttime) {
                   2649: 		if ($now<$starttime) { $active=0; }
                   2650:             }
                   2651:             if ($endtime) {
                   2652:                 if ($now>$endtime) { $active=0; }
                   2653:             }
1.348     albertel 2654:             if ($active) { $courses{$domain.'_'.$id}=1; }
1.51      www      2655:         }
                   2656:     }
1.348     albertel 2657:     return keys(%courses);
1.51      www      2658: }
1.37      matthew  2659: 
1.54      www      2660: ###############################################
1.60      matthew  2661: ###############################################
                   2662: 
                   2663: =pod
                   2664: 
1.112     bowersj2 2665: =head1 Domain Template Functions
                   2666: 
                   2667: =over 4
                   2668: 
                   2669: =item * &determinedomain()
1.60      matthew  2670: 
                   2671: Inputs: $domain (usually will be undef)
                   2672: 
1.63      www      2673: Returns: Determines which domain should be used for designs
1.60      matthew  2674: 
                   2675: =cut
1.54      www      2676: 
1.60      matthew  2677: ###############################################
1.63      www      2678: sub determinedomain {
                   2679:     my $domain=shift;
                   2680:    if (! $domain) {
1.60      matthew  2681:         # Determine domain if we have not been given one
                   2682:         $domain = $Apache::lonnet::perlvar{'lonDefDomain'};
1.258     albertel 2683:         if ($env{'user.domain'}) { $domain=$env{'user.domain'}; }
                   2684:         if ($env{'request.role.domain'}) { 
                   2685:             $domain=$env{'request.role.domain'}; 
1.60      matthew  2686:         }
                   2687:     }
1.63      www      2688:     return $domain;
                   2689: }
                   2690: ###############################################
                   2691: =pod
                   2692: 
1.112     bowersj2 2693: =item * &domainlogo()
1.63      www      2694: 
                   2695: Inputs: $domain (usually will be undef)
                   2696: 
                   2697: Returns: A link to a domain logo, if the domain logo exists.
                   2698: If the domain logo does not exist, a description of the domain.
                   2699: 
                   2700: =cut
1.112     bowersj2 2701: 
1.63      www      2702: ###############################################
                   2703: sub domainlogo {
                   2704:     my $domain = &determinedomain(shift);    
                   2705:      # See if there is a logo
1.59      www      2706:     if (-e '/home/httpd/html/adm/lonDomLogos/'.$domain.'.gif') {
1.215     albertel 2707: 	my $logo=&lonhttpdurl("/adm/lonDomLogos/$domain.gif");
                   2708:         return '<img src="'.$logo.'" alt="'.$domain.'" />';
1.60      matthew  2709:     } elsif(exists($Apache::lonnet::domaindescription{$domain})) {
                   2710:         return $Apache::lonnet::domaindescription{$domain};
1.59      www      2711:     } else {
1.60      matthew  2712:         return '';
1.59      www      2713:     }
                   2714: }
1.63      www      2715: ##############################################
                   2716: 
                   2717: =pod
                   2718: 
1.112     bowersj2 2719: =item * &designparm()
1.63      www      2720: 
                   2721: Inputs: $which parameter; $domain (usually will be undef)
                   2722: 
                   2723: Returns: value of designparamter $which
                   2724: 
                   2725: =cut
1.112     bowersj2 2726: 
1.397     albertel 2727: 
1.400     albertel 2728: ##############################################
1.397     albertel 2729: sub designparm {
                   2730:     my ($which,$domain)=@_;
1.258     albertel 2731:     if ($env{'browser.blackwhite'} eq 'on') {
1.110     www      2732: 	if ($which=~/\.(font|alink|vlink|link)$/) {
                   2733: 	    return '#000000';
                   2734: 	}
                   2735: 	if ($which=~/\.(pgbg|sidebg)$/) {
                   2736: 	    return '#FFFFFF';
                   2737: 	}
                   2738: 	if ($which=~/\.tabbg$/) {
                   2739: 	    return '#CCCCCC';
                   2740: 	}
                   2741:     }
1.397     albertel 2742:     if (exists($env{'environment.color.'.$which})) {
1.258     albertel 2743: 	return $env{'environment.color.'.$which};
1.96      www      2744:     }
1.63      www      2745:     $domain=&determinedomain($domain);
1.397     albertel 2746:     if (exists($designhash{$domain.'.'.$which})) {
1.63      www      2747: 	return $designhash{$domain.'.'.$which};
                   2748:     } else {
                   2749:         return $designhash{'default.'.$which};
                   2750:     }
                   2751: }
1.59      www      2752: 
1.60      matthew  2753: ###############################################
                   2754: ###############################################
                   2755: 
                   2756: =pod
                   2757: 
1.112     bowersj2 2758: =back
                   2759: 
                   2760: =head1 HTTP Helpers
                   2761: 
                   2762: =over 4
                   2763: 
                   2764: =item * &bodytag()
1.60      matthew  2765: 
                   2766: Returns a uniform header for LON-CAPA web pages.
                   2767: 
                   2768: Inputs: 
                   2769: 
1.112     bowersj2 2770: =over 4
                   2771: 
                   2772: =item * $title, A title to be displayed on the page.
                   2773: 
                   2774: =item * $function, the current role (can be undef).
                   2775: 
                   2776: =item * $addentries, extra parameters for the <body> tag.
                   2777: 
                   2778: =item * $bodyonly, if defined, only return the <body> tag.
                   2779: 
                   2780: =item * $domain, if defined, force a given domain.
                   2781: 
                   2782: =item * $forcereg, if page should register as content page (relevant for 
1.86      www      2783:             text interface only)
1.60      matthew  2784: 
1.326     albertel 2785: =item * $customtitle, alternate text to use instead of $title
                   2786:                       in the title box that appears, this text
                   2787:                       is not auto translated like the $title is
1.309     albertel 2788: 
                   2789: =item * $notopbar, if true, keep the 'what is this' info but remove the
                   2790:                    navigational links
1.317     albertel 2791: 
1.338     albertel 2792: =item * $bgcolor, used to override the bgcolor on a webpage to a specific value
                   2793: 
                   2794: =item * $notitle, if true keep the nav controls, but remove the title bar
                   2795: 
1.361     albertel 2796: =item * $no_inline_link, if true and in remote mode, don't show the 
                   2797:          'Switch To Inline Menu' link
                   2798: 
1.112     bowersj2 2799: =back
                   2800: 
1.60      matthew  2801: Returns: A uniform header for LON-CAPA web pages.  
                   2802: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
                   2803: If $bodyonly is undef or zero, an html string containing a <body> tag and 
                   2804: other decorations will be returned.
                   2805: 
                   2806: =cut
                   2807: 
1.54      www      2808: sub bodytag {
1.309     albertel 2809:     my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,$customtitle,
1.361     albertel 2810: 	$notopbar,$bgcolor,$notitle,$no_inline_link)=@_;
1.339     albertel 2811: 
1.117     www      2812:     $title=&mt($title);
1.339     albertel 2813: 
1.183     matthew  2814:     $function = &get_users_function() if (!$function);
1.339     albertel 2815:     my $img =    &designparm($function.'.img',$domain);
                   2816:     my $font =   &designparm($function.'.font',$domain);
                   2817:     my $pgbg   = $bgcolor || &designparm($function.'.pgbg',$domain);
                   2818: 
                   2819:     my %design = ( 'style'   => 'margin-top: 0px',
                   2820: 		   'bgcolor' => $pgbg,
                   2821: 		   'text'    => $font,
                   2822:                    'alink'   => &designparm($function.'.alink',$domain),
                   2823: 		   'vlink'   => &designparm($function.'.vlink',$domain),
                   2824: 		   'link'    => &designparm($function.'.link',$domain),);
                   2825:     @$addentries{keys(%design)} = @design{keys(%design)};
                   2826: 
1.63      www      2827:  # role and realm
1.378     raeburn  2828:     my ($role,$realm) = split(/\./,$env{'request.role'},2);
                   2829:     if ($role  eq 'ca') {
                   2830:         my ($rdom,$rname) = ($realm =~ m-^/(\w+)/(\w+)$-);
                   2831:         $realm = &plainname($rname,$rdom).':'.$rdom;
                   2832:     } 
1.55      www      2833: # realm
1.258     albertel 2834:     if ($env{'request.course.id'}) {
1.378     raeburn  2835:         if ($env{'request.role'} !~ /^cr/) {
                   2836:             $role = &Apache::lonnet::plaintext($role,&course_type());
                   2837:         }
1.359     albertel 2838: 	$realm = $env{'course.'.$env{'request.course.id'}.'.description'};
1.378     raeburn  2839:     } else {
                   2840:         $role = &Apache::lonnet::plaintext($role);
1.54      www      2841:     }
1.359     albertel 2842:     if (!$realm) { $realm='&nbsp;'; }
1.55      www      2843: # Set messages
1.60      matthew  2844:     my $messages=&domainlogo($domain);
1.101     www      2845: # Port for miniserver
1.83      albertel 2846:     my $lonhttpdPort=$Apache::lonnet::perlvar{'lonhttpdPort'};
                   2847:     if (!defined($lonhttpdPort)) { $lonhttpdPort='8080'; }
1.330     albertel 2848: 
                   2849:     my $extra_body_attr = &make_attr_string($forcereg,$addentries);
1.329     albertel 2850: 
1.101     www      2851: # construct main body tag
1.359     albertel 2852:     my $bodytag = "<body $extra_body_attr>".
                   2853: 	&Apache::lontexconvert::init_math_support();
1.252     albertel 2854: 
1.332     albertel 2855:     if ($bodyonly 
                   2856: 	|| ($env{'request.state'} eq 'construct' 
                   2857: 	    && $env{'environment.remote'} ne 'off' )) {
1.60      matthew  2858:         return $bodytag;
1.258     albertel 2859:     } elsif ($env{'browser.interface'} eq 'textual') {
1.95      www      2860: # Accessibility
1.224     raeburn  2861:           
1.337     albertel 2862: 	$bodytag.=&Apache::lonmenu::menubuttons($forcereg,$forcereg);
1.338     albertel 2863: 	if (!$notitle) {
1.337     albertel 2864: 	    $bodytag.='<h1>LON-CAPA: '.$title.'</h1>';
                   2865: 	}
                   2866: 	return $bodytag;
1.359     albertel 2867:     }
                   2868: 
1.410   ! albertel 2869:     my $name = &plainname($env{'user.name'},$env{'user.domain'});
1.359     albertel 2870:     
                   2871:     my $roleinfo=(<<ENDROLE);
                   2872: <td class="LC_title_bar_who">
                   2873: <div class="LC_title_bar_name">
1.410   ! albertel 2874:     $name
1.361     albertel 2875:     &nbsp;
1.359     albertel 2876: </div>
                   2877: <div class="LC_title_bar_role">
1.361     albertel 2878: $role&nbsp;
1.359     albertel 2879: </div>
                   2880: <div class="LC_title_bar_realm">
1.361     albertel 2881: $realm&nbsp;
1.359     albertel 2882: </div>
1.206     albertel 2883: </td>
                   2884: ENDROLE
1.235     raeburn  2885: 
1.359     albertel 2886:     my $titleinfo = '<span class="LC_title_bar_title">'.$title.'</span>';
                   2887:     if ($customtitle) {
                   2888:         $titleinfo = $customtitle;
                   2889:     }
                   2890:     #
                   2891:     # Extra info if you are the DC
                   2892:     my $dc_info = '';
                   2893:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
                   2894:                         $env{'course.'.$env{'request.course.id'}.
                   2895:                                  '.domain'}.'/'})) {
                   2896:         my $cid = $env{'request.course.id'};
                   2897:         $dc_info.= $cid.' '.$env{'course.'.$cid.'.internal.coursecode'};
1.380     www      2898:         $dc_info =~ s/\s+$//;
1.359     albertel 2899:         $dc_info = '('.$dc_info.')';
                   2900:     }
                   2901: 
                   2902:     if ($env{'environment.remote'} eq 'off') {
                   2903:         # No Remote
1.258     albertel 2904: 	if ($env{'request.state'} eq 'construct') {
1.359     albertel 2905: 	    $forcereg=1;
                   2906: 	}
                   2907: 
                   2908: 	if (!$customtitle && $env{'request.state'} eq 'construct') {
                   2909: 	    # this is for resources; directories have customtitle, and crumbs
                   2910:             # and select recent are created in lonpubdir.pm  
1.229     albertel 2911: 	    my ($uname,$thisdisfn)=
1.258     albertel 2912: 		($env{'request.filename'} =~ m|^/home/([^/]+)/public_html/(.*)|);
1.229     albertel 2913: 	    my $formaction='/priv/'.$uname.'/'.$thisdisfn;
                   2914: 	    $formaction=~s/\/+/\//g;
                   2915: 
1.359     albertel 2916: 	    my $parentpath = '';
                   2917: 	    my $lastitem = '';
                   2918: 	    if ($thisdisfn =~ m-(.+/)([^/]*)$-) {
                   2919: 		$parentpath = $1;
                   2920: 		$lastitem = $2;
                   2921: 	    } else {
                   2922: 		$lastitem = $thisdisfn;
                   2923: 	    }
                   2924: 	    $titleinfo = 
1.401     albertel 2925: 		&Apache::loncommon::help_open_menu('','',3,'Authoring').
1.359     albertel 2926: 		'<b>Construction Space</b>:&nbsp;'. 
                   2927: 		'<form name="dirs" method="post" action="'.$formaction
                   2928: 		.'" target="_top"><tt><b>'
                   2929: 		.&Apache::lonhtmlcommon::crumbs($uname.'/'.$parentpath,'_top','/priv','','+1',1)."<font size=\"+1\">$lastitem</font></b></tt><br />"
                   2930: 		.&Apache::lonhtmlcommon::select_recent('construct','recent','this.form.action=this.form.recent.value;this.form.submit()')
                   2931: 		.'</form>'
                   2932: 		.&Apache::lonmenu::constspaceform();
1.235     raeburn  2933:         }
1.359     albertel 2934: 
1.337     albertel 2935:         my $titletable;
1.338     albertel 2936: 	if (!$notitle) {
1.337     albertel 2937: 	    $titletable =
1.359     albertel 2938: 		'<table id="LC_title_bar">'.
                   2939:                          "<tr><td> $titleinfo $dc_info</td>".$roleinfo.
                   2940: 			 '</tr></table>';
1.337     albertel 2941: 	}
1.359     albertel 2942: 	if ($notopbar) {
                   2943: 	    $bodytag .= $titletable;
                   2944: 	} else {
                   2945: 	    if ($env{'request.state'} eq 'construct') {
1.337     albertel 2946:                 $bodytag .= &Apache::lonmenu::menubuttons($forcereg,$forcereg,
                   2947: 							  $titletable);
1.272     raeburn  2948:             } else {
1.336     albertel 2949:                 $bodytag .= &Apache::lonmenu::menubuttons($forcereg,$forcereg).
1.359     albertel 2950: 		    $titletable;
1.272     raeburn  2951:             }
1.235     raeburn  2952:         }
                   2953:         return $bodytag;
1.94      www      2954:     }
1.95      www      2955: 
1.93      www      2956: #
1.95      www      2957: # Top frame rendering, Remote is up
1.93      www      2958: #
1.359     albertel 2959: 
                   2960:     my $upperleft='<img src="http://'.$ENV{'HTTP_HOST'}.':'.
                   2961:         $lonhttpdPort.$img.'" alt="'.$function.'" />';
                   2962: 
1.305     www      2963:     # Explicit link to get inline menu
1.361     albertel 2964:     my $menu= ($no_inline_link?''
                   2965: 	       :'<br /><a href="/adm/remote?action=collapse">'.&mt('Switch to Inline Menu Mode').'</a>');
1.245     matthew  2966:     #
1.338     albertel 2967:     if ($notitle) {
1.337     albertel 2968: 	return $bodytag;
                   2969:     }
1.94      www      2970:     return(<<ENDBODY);
1.60      matthew  2971: $bodytag
1.359     albertel 2972: <table id="LC_title_bar" class="LC_with_remote">
1.368     albertel 2973: <tr><td class="LC_title_bar_role_logo">$upperleft</td>
1.359     albertel 2974:     <td class="LC_title_bar_domain_logo">$messages&nbsp;</td>
1.54      www      2975: </tr>
1.359     albertel 2976: <tr><td>$titleinfo $dc_info $menu</td>
                   2977: $roleinfo
1.368     albertel 2978: </tr>
1.356     albertel 2979: </table>
1.54      www      2980: ENDBODY
1.182     matthew  2981: }
                   2982: 
1.330     albertel 2983: sub make_attr_string {
                   2984:     my ($register,$attr_ref) = @_;
                   2985: 
                   2986:     if ($attr_ref && !ref($attr_ref)) {
                   2987: 	die("addentries Must be a hash ref ".
                   2988: 	    join(':',caller(1))." ".
                   2989: 	    join(':',caller(0))." ");
                   2990:     }
                   2991: 
                   2992:     if ($register) {
1.339     albertel 2993: 	my ($on_load,$on_unload);
                   2994: 	foreach my $key (keys(%{$attr_ref})) {
                   2995: 	    if      (lc($key) eq 'onload') {
                   2996: 		$on_load.=$attr_ref->{$key}.';';
                   2997: 		delete($attr_ref->{$key});
                   2998: 
                   2999: 	    } elsif (lc($key) eq 'onunload') {
                   3000: 		$on_unload.=$attr_ref->{$key}.';';
                   3001: 		delete($attr_ref->{$key});
                   3002: 	    }
                   3003: 	}
                   3004: 	$attr_ref->{'onload'}  =
                   3005: 	    &Apache::lonmenu::loadevents().  $on_load;
                   3006: 	$attr_ref->{'onunload'}=
                   3007: 	    &Apache::lonmenu::unloadevents().$on_unload;
                   3008:     }
                   3009: 
                   3010: # Accessibility font enhance
                   3011:     if ($env{'browser.fontenhance'} eq 'on') {
                   3012: 	my $style;
                   3013: 	foreach my $key (keys(%{$attr_ref})) {
                   3014: 	    if (lc($key) eq 'style') {
                   3015: 		$style.=$attr_ref->{$key}.';';
                   3016: 		delete($attr_ref->{$key});
                   3017: 	    }
                   3018: 	}
                   3019: 	$attr_ref->{'style'}=$style.'; font-size: x-large;';
1.330     albertel 3020:     }
1.339     albertel 3021: 
                   3022:     if ($env{'browser.blackwhite'} eq 'on') {
                   3023: 	delete($attr_ref->{'font'});
                   3024: 	delete($attr_ref->{'link'});
                   3025: 	delete($attr_ref->{'alink'});
                   3026: 	delete($attr_ref->{'vlink'});
                   3027: 	delete($attr_ref->{'bgcolor'});
                   3028: 	delete($attr_ref->{'background'});
                   3029:     }
                   3030: 
1.330     albertel 3031:     my $attr_string;
                   3032:     foreach my $attr (keys(%$attr_ref)) {
                   3033: 	$attr_string .= " $attr=\"".$attr_ref->{$attr}.'" ';
                   3034:     }
                   3035:     return $attr_string;
                   3036: }
                   3037: 
                   3038: 
1.182     matthew  3039: ###############################################
1.251     albertel 3040: ###############################################
                   3041: 
                   3042: =pod
                   3043: 
                   3044: =back
                   3045: 
1.306     albertel 3046: =head1 HTML Helpers
1.251     albertel 3047: 
                   3048: =over 4
                   3049: 
                   3050: =item * &endbodytag()
                   3051: 
                   3052: Returns a uniform footer for LON-CAPA web pages.
                   3053: 
1.306     albertel 3054: Inputs: none
1.251     albertel 3055: 
                   3056: =back
                   3057: 
                   3058: =cut
                   3059: 
                   3060: sub endbodytag {
                   3061:     my $endbodytag='</body>';
1.269     albertel 3062:     $endbodytag=&Apache::lontexconvert::jsMath_process()."\n".$endbodytag;
1.315     albertel 3063:     if ( exists( $env{'internal.head.redirect'} ) ) {
                   3064: 	$endbodytag=
                   3065: 	    "<br /><a href=\"$env{'internal.head.redirect'}\">".
                   3066: 	    &mt('Continue').'</a>'.
                   3067: 	    $endbodytag;
                   3068:     }
1.251     albertel 3069:     return $endbodytag;
                   3070: }
                   3071: 
1.352     albertel 3072: =pod
                   3073: 
                   3074: =over 4
                   3075: 
                   3076: =item * &standard_css()
                   3077: 
                   3078: Returns a style sheet
                   3079: 
                   3080: Inputs: (all optional)
                   3081:             domain         -> force to color decorate a page for a specific
                   3082:                                domain
                   3083:             function       -> force usage of a specific rolish color scheme
                   3084:             bgcolor        -> override the default page bgcolor
                   3085: 
                   3086: =back
                   3087: 
                   3088: =cut
                   3089: 
1.343     albertel 3090: sub standard_css {
1.345     albertel 3091:     my ($function,$domain,$bgcolor) = @_;
1.352     albertel 3092:     $function  = &get_users_function() if (!$function);
                   3093:     my $img    = &designparm($function.'.img',   $domain);
                   3094:     my $tabbg  = &designparm($function.'.tabbg', $domain);
                   3095:     my $font   = &designparm($function.'.font',  $domain);
1.345     albertel 3096:     my $sidebg = &designparm($function.'.sidebg',$domain);
1.382     albertel 3097:     my $pgbg_or_bgcolor =
                   3098: 	         $bgcolor ||
1.352     albertel 3099: 	         &designparm($function.'.pgbg',  $domain);
1.382     albertel 3100:     my $pgbg   = &designparm($function.'.pgbg',  $domain);
1.352     albertel 3101:     my $alink  = &designparm($function.'.alink', $domain);
                   3102:     my $vlink  = &designparm($function.'.vlink', $domain);
                   3103:     my $link   = &designparm($function.'.link',  $domain);
                   3104: 
                   3105:     my $sans                 = 'Arial,Helvetica,sans-serif';
1.395     albertel 3106:     my $mono                 = 'monospace';
1.352     albertel 3107:     my $data_table_head      = $tabbg;
                   3108:     my $data_table_light     = '#EEEEEE';
                   3109:     my $data_table_dark      = '#DDD';
1.349     albertel 3110:     my $data_table_highlight = '#FFFF00';
1.352     albertel 3111:     my $mail_new             = '#FFBB77';
                   3112:     my $mail_new_hover       = '#DD9955';
                   3113:     my $mail_read            = '#BBBB77';
                   3114:     my $mail_read_hover      = '#999944';
                   3115:     my $mail_replied         = '#AAAA88';
                   3116:     my $mail_replied_hover   = '#888855';
                   3117:     my $mail_other           = '#99BBBB';
                   3118:     my $mail_other_hover     = '#669999';
1.391     albertel 3119:     my $table_header         = '#DDDDDD';
1.392     albertel 3120: 
                   3121:     my $border = ($env{'browser.type'} eq 'explorer') ? '0px 2px 0px 2px'
                   3122: 	                                              : '0px 3px 0px 4px';
1.343     albertel 3123:     return <<END;
1.345     albertel 3124: h1, h2, h3, th { font-family: $sans }
1.343     albertel 3125: a:focus { color: red; background: yellow } 
                   3126: table.thinborder { border-collapse: collapse; }
                   3127: table.thinborder tr th, table.thinborder tr td { border-style: solid; border-width: 1px}
                   3128: form, .inline { display: inline; }
                   3129: .center { text-align: center; }
1.395     albertel 3130: .LC_filename {font-family: $mono;}
1.350     albertel 3131: .LC_error {
                   3132:   color: red;
                   3133:   font-size: larger;
                   3134: }
1.394     albertel 3135: .LC_warning {
                   3136:   color: red;
                   3137: }
1.350     albertel 3138: .LC_success {
                   3139:   color: green;
                   3140: }
1.346     albertel 3141: 
1.392     albertel 3142: table#LC_top_nav, table#LC_menubuttons {
1.345     albertel 3143:   width: 100%;
                   3144:   background: $pgbg;
1.392     albertel 3145:   border: 2px;
1.402     albertel 3146:   border-collapse: separate;
1.403     albertel 3147:   padding: 0px;
1.345     albertel 3148: }
1.392     albertel 3149: 
1.409     albertel 3150: table#LC_title_bar, table.LC_breadcrumbs, table#LC_nav_location,
1.393     albertel 3151: table#LC_title_bar.LC_with_remote {
1.359     albertel 3152:   width: 100%;
1.392     albertel 3153:   border-color: $pgbg;
                   3154:   border-style: solid;
                   3155:   border-width: $border;
                   3156: 
1.379     albertel 3157:   background: $pgbg;
                   3158:   font-family: $sans;
1.392     albertel 3159:   border-collapse: collapse;
1.403     albertel 3160:   padding: 0px;
1.359     albertel 3161: }
1.392     albertel 3162: 
1.409     albertel 3163: table.LC_docs_path {
                   3164:   width: 100%;
                   3165:   border: 0;
                   3166:   background: $pgbg;
                   3167:   font-family: $sans;
                   3168:   border-collapse: collapse;
                   3169:   padding: 0px;
                   3170: }
                   3171: 
1.359     albertel 3172: table#LC_title_bar td {
                   3173:   background: $tabbg;
                   3174: }
                   3175: table#LC_title_bar td.LC_title_bar_who {
                   3176:   background: $tabbg;
                   3177:   color: $font;
                   3178:   font: medium $sans;
                   3179:   text-align: right;
                   3180: }
                   3181: span.LC_title_bar_title {
                   3182:   font: bold xx-large $sans;
                   3183: }
                   3184: table#LC_title_bar td.LC_title_bar_domain_logo {
                   3185:   background: $sidebg;
                   3186:   text-align: right;
1.368     albertel 3187:   padding: 0px;
                   3188: }
                   3189: table#LC_title_bar td.LC_title_bar_role_logo {
                   3190:   background: $sidebg;
                   3191:   padding: 0px;
1.359     albertel 3192: }
                   3193: 
1.346     albertel 3194: table#LC_menubuttons_mainmenu {
                   3195:   background: $pgbg;
                   3196:   border: 0px;
                   3197:   border-spacing: 1px;
1.372     albertel 3198:   padding: 0px 1px;
1.346     albertel 3199:   margin: 0px;
                   3200:   border-collapse: separate;
                   3201: }
                   3202: table#LC_menubuttons img, table#LC_menubuttons_mainmenu img {
                   3203:   border: 0px;
                   3204: }
1.345     albertel 3205: table#LC_top_nav td {
                   3206:   background: $tabbg;
1.392     albertel 3207:   border: 0px;
1.407     albertel 3208:   font-size: small;
1.345     albertel 3209: }
                   3210: table#LC_top_nav td a, div#LC_top_nav a {
                   3211:   color: $font;
                   3212:   font-family: $sans;
                   3213: }
1.364     albertel 3214: table#LC_top_nav td.LC_top_nav_logo {
                   3215:   background: $tabbg;
                   3216:   text-align: right;
1.408     albertel 3217:   white-space: nowrap;
                   3218:   font-weight: bold;
                   3219: }
                   3220: table#LC_top_nav td.LC_top_nav_logo img {
                   3221:   margin-left: 0.2em;
                   3222:   vertical-align: bottom;
1.364     albertel 3223: }
1.409     albertel 3224: table.LC_breadcrumbs td, table.LC_docs_path td  {
1.357     albertel 3225:   background: $tabbg;
                   3226:   color: $font;
                   3227:   font-family: $sans;
1.358     albertel 3228:   font-size: smaller;
1.357     albertel 3229: }
1.409     albertel 3230: table.LC_breadcrumbs td.LC_breadcrumb_component,
                   3231: table.LC_docs_path td.LC_docs_path_component {
1.357     albertel 3232:   background: $tabbg;
                   3233:   color: $font;
                   3234:   font-family: $sans;
                   3235:   font-size: larger;
                   3236:   text-align: right;
                   3237: }
1.383     albertel 3238: td.LC_table_cell_checkbox {
                   3239:   text-align: center;
                   3240: }
                   3241: 
1.346     albertel 3242: .LC_menubuttons_inline_text {
                   3243:   color: $font;
                   3244:   font-family: $sans;
                   3245:   font-size: smaller;
                   3246: }
                   3247: 
                   3248: td.LC_menubuttons_text {
                   3249:   color: $font;
                   3250:   font-family: $sans;
                   3251: }
                   3252: td.LC_menubuttons_img {
                   3253:   background: $tabbg;
                   3254: }
                   3255: .LC_current_location {
                   3256:   font-family: $sans;
                   3257:   background: $tabbg;
                   3258: }
                   3259: .LC_new_mail {
                   3260:   font-family: $sans;
                   3261:   font-weight: bold;
                   3262: }
1.347     albertel 3263: 
1.349     albertel 3264: table.LC_data_table, table.LC_mail_list {
1.347     albertel 3265:   border: 1px solid #000000;
1.402     albertel 3266:   border-collapse: separate;
1.347     albertel 3267: }
1.349     albertel 3268: table.LC_data_table tr th, table.LC_calendar tr th, table.LC_mail_list tr th {
                   3269:   font-weight: bold;
                   3270:   background-color: $data_table_head;
1.347     albertel 3271: }
                   3272: table.LC_data_table tr td {
1.349     albertel 3273:   background-color: $data_table_light;
1.347     albertel 3274: }
                   3275: table.LC_data_table tr.LC_even_row td {
1.349     albertel 3276:   background-color: $data_table_dark;
1.347     albertel 3277: }
                   3278: table.LC_data_table tr.LC_empty td {
                   3279:   background-color: #FFFFFF;
                   3280: }
                   3281: 
1.349     albertel 3282: table.LC_calendar {
                   3283:   border: 1px solid #000000;
                   3284:   border-collapse: collapse;
                   3285: }
                   3286: table.LC_calendar_pickdate {
                   3287:   font-size: xx-small;
                   3288: }
                   3289: table.LC_calendar tr td {
                   3290:   border: 1px solid #000000;
                   3291:   vertical-align: top;
                   3292: }
                   3293: table.LC_calendar tr td.LC_calendar_day_empty {
                   3294:   background-color: $data_table_dark;
                   3295: }
                   3296: table.LC_calendar tr td.LC_calendar_day_current {
                   3297:   background-color: $data_table_highlight;
                   3298: }
                   3299: 
                   3300: table.LC_mail_list tr.LC_mail_new {
                   3301:   background-color: $mail_new;
                   3302: }
                   3303: table.LC_mail_list tr.LC_mail_new:hover {
                   3304:   background-color: $mail_new_hover;
                   3305: }
                   3306: table.LC_mail_list tr.LC_mail_read {
                   3307:   background-color: $mail_read;
                   3308: }
                   3309: table.LC_mail_list tr.LC_mail_read:hover {
                   3310:   background-color: $mail_read_hover;
                   3311: }
                   3312: table.LC_mail_list tr.LC_mail_replied {
                   3313:   background-color: $mail_replied;
                   3314: }
                   3315: table.LC_mail_list tr.LC_mail_replied:hover {
                   3316:   background-color: $mail_replied_hover;
                   3317: }
                   3318: table.LC_mail_list tr.LC_mail_other {
                   3319:   background-color: $mail_other;
                   3320: }
                   3321: table.LC_mail_list tr.LC_mail_other:hover {
                   3322:   background-color: $mail_other_hover;
                   3323: }
1.385     albertel 3324: 
1.386     albertel 3325: table#LC_portfolio_actions {
                   3326:   width: auto;
                   3327:   background: $pgbg;
                   3328:   border: 0px;
                   3329:   border-spacing: 2px 2px;
                   3330:   padding: 0px;
                   3331:   margin: 0px;
                   3332:   border-collapse: separate;
                   3333: }
                   3334: table#LC_portfolio_actions td.LC_label {
                   3335:   background: $tabbg;
                   3336:   text-align: right;
                   3337: }
                   3338: table#LC_portfolio_actions td.LC_value {
                   3339:   background: $tabbg;
                   3340: }
1.385     albertel 3341: 
1.391     albertel 3342: table#LC_cstr_controls {
                   3343:   width: 100%;
                   3344:   border-collapse: collapse;
                   3345: }
                   3346: table#LC_cstr_controls tr td {
                   3347:   border: 4px solid $pgbg;
                   3348:   padding: 4px;
                   3349:   text-align: center;
                   3350:   background: $tabbg;
                   3351: }
                   3352: table#LC_cstr_controls tr th {
                   3353:   border: 4px solid $pgbg;
                   3354:   background: $table_header;
                   3355:   text-align: center;
                   3356:   font-family: $sans;
                   3357:   font-size: smaller;
                   3358: }
                   3359: 
1.389     albertel 3360: table#LC_browser {
                   3361:  
                   3362: }
                   3363: table#LC_browser tr th {
1.391     albertel 3364:   background: $table_header;
1.389     albertel 3365: }
1.390     albertel 3366: table#LC_browser tr td {
                   3367:   padding: 2px;
                   3368: }
1.389     albertel 3369: table#LC_browser tr.LC_browser_file,
                   3370: table#LC_browser tr.LC_browser_file_published {
                   3371:   background: #CCFF88;
                   3372: }
                   3373: table#LC_browser tr.LC_browser_file_locked,
                   3374: table#LC_browser tr.LC_browser_file_unpublished {
                   3375:   background: #FFAA99;
1.387     albertel 3376: }
1.389     albertel 3377: table#LC_browser tr.LC_browser_file_obsolete {
                   3378:   background: #AAAAAA;
1.387     albertel 3379: }
1.389     albertel 3380: table#LC_browser tr.LC_browser_file_modified {
                   3381:   background: #FFFF77;
1.387     albertel 3382: }
1.389     albertel 3383: table#LC_browser tr.LC_browser_folder {
                   3384:   background: #CCCCFF;
1.387     albertel 3385: }
1.388     albertel 3386: span.LC_current_location {
                   3387:   font-size: x-large;
                   3388:   background: $pgbg;
                   3389: }
1.387     albertel 3390: 
1.395     albertel 3391: span.LC_parm_menu_item {
                   3392:   font-size: larger;
                   3393:   font-family: $sans;
                   3394: }
                   3395: span.LC_parm_scope_all {
                   3396:   color: red;
                   3397: }
                   3398: span.LC_parm_scope_folder {
                   3399:   color: green;
                   3400: }
                   3401: span.LC_parm_scope_resource {
                   3402:   color: orange;
                   3403: }
                   3404: span.LC_parm_part {
                   3405:   color: blue;
                   3406: }
                   3407: span.LC_parm_folder, span.LC_parm_symb {
                   3408:   font-size: x-small;
                   3409:   font-family: $mono;
                   3410:   color: #AAAAAA;
                   3411: }
                   3412: 
1.396     albertel 3413: td.LC_parm_overview_level_menu, td.LC_parm_overview_map_menu,
                   3414: td.LC_parm_overview_parm_selectors, td.LC_parm_overview_parm_restrictions {
                   3415:   border: 1px solid black;
                   3416:   border-collapse: collapse;
                   3417: }
                   3418: table.LC_parm_overview_restrictions td {
                   3419:   border-width: 1px 4px 1px 4px;
                   3420:   border-style: solid;
                   3421:   border-color: $pgbg;
                   3422:   text-align: center;
                   3423: }
                   3424: table.LC_parm_overview_restrictions th {
                   3425:   background: $tabbg;
                   3426:   border-width: 1px 4px 1px 4px;
                   3427:   border-style: solid;
                   3428:   border-color: $pgbg;
                   3429: }
1.398     albertel 3430: table#LC_helpmenu {
                   3431:   border: 0px;
                   3432:   height: 55px;
                   3433:   border-spacing: 0px;
                   3434: }
                   3435: 
                   3436: table#LC_helpmenu fieldset legend {
                   3437:   font-size: larger;
                   3438:   font-weight: bold;
                   3439: }
1.397     albertel 3440: table#LC_helpmenu_links {
                   3441:   width: 100%;
                   3442:   border: 1px solid black;
                   3443:   background: $pgbg;
                   3444:   padding: 0px;
                   3445:   border-spacing: 1px;
                   3446: }
                   3447: table#LC_helpmenu_links tr td {
                   3448:   padding: 1px;
                   3449:   background: $tabbg;
1.399     albertel 3450:   text-align: center;
                   3451:   font-weight: bold;
1.397     albertel 3452: }
1.396     albertel 3453: 
1.397     albertel 3454: table#LC_helpmenu_links a:link, table#LC_helpmenu_links a:visited,
                   3455: table#LC_helpmenu_links a:active {
                   3456:   text-decoration: none;
                   3457:   color: $font;
                   3458: }
                   3459: table#LC_helpmenu_links a:hover {
                   3460:   text-decoration: underline;
                   3461:   color: $vlink;
                   3462: }
1.396     albertel 3463: 
1.343     albertel 3464: END
                   3465: }
                   3466: 
1.306     albertel 3467: =pod
                   3468: 
                   3469: =over 4
                   3470: 
                   3471: =item * &headtag()
                   3472: 
                   3473: Returns a uniform footer for LON-CAPA web pages.
                   3474: 
1.307     albertel 3475: Inputs: $title - optional title for the head
                   3476:         $head_extra - optional extra HTML to put inside the <head>
1.315     albertel 3477:         $args - optional arguments
1.319     albertel 3478:             force_register - if is true call registerurl so the remote is 
                   3479:                              informed
1.352     albertel 3480:             redirect       -> array ref of seconds before redirect occurs
1.315     albertel 3481:                                     url to redirect to
                   3482:                            (side effect of setting 
                   3483:                                $env{'internal.head.redirect'} to the url 
                   3484:                                redirected too)
1.352     albertel 3485:             domain         -> force to color decorate a page for a specific
                   3486:                                domain
                   3487:             function       -> force usage of a specific rolish color scheme
                   3488:             bgcolor        -> override the default page bgcolor
                   3489: 
1.306     albertel 3490: =back
                   3491: 
                   3492: =cut
                   3493: 
                   3494: sub headtag {
1.313     albertel 3495:     my ($title,$head_extra,$args) = @_;
1.306     albertel 3496:     
1.363     albertel 3497:     my $function = $args->{'function'} || &get_users_function();
                   3498:     my $domain   = $args->{'domain'}   || &determinedomain();
                   3499:     my $bgcolor  = $args->{'bgcolor'}  || &designparm($function.'.pgbg',$domain);
1.391     albertel 3500:     my $url = join(':',$env{'user.name'},$env{'user.domain'},time(),
                   3501: 		   #$env{'environment.color.timestamp'},
1.363     albertel 3502: 		   $function,$domain,$bgcolor);
                   3503: 
1.369     www      3504:     $url = '/adm/css/'.&escape($url).'.css';
1.363     albertel 3505: 
1.308     albertel 3506:     my $result =
                   3507: 	'<head>'.
1.363     albertel 3508: 	'<link rel="stylesheet" type="text/css" href="'.$url.'" />'.
1.340     albertel 3509: 	&font_settings().
1.308     albertel 3510: 	&Apache::lonhtmlcommon::htmlareaheaders();
1.319     albertel 3511: 
                   3512:     if ($args->{'force_register'}) {
                   3513: 	$result .= &Apache::lonmenu::registerurl(1);
                   3514:     }
                   3515: 
1.314     albertel 3516:     if (ref($args->{'redirect'})) {
                   3517: 	my ($time,$url) = @{$args->{'redirect'}};
1.315     albertel 3518: 	$url = &Apache::lonenc::check_encrypt($url);
                   3519: 	$env{'internal.head.redirect'} = $url;
1.313     albertel 3520: 	$result.=<<ADDMETA
                   3521: <meta http-equiv="pragma" content="no-cache" />
1.344     albertel 3522: <meta http-equiv="Refresh" content="$time; url=$url" />
1.313     albertel 3523: ADDMETA
                   3524:     }
1.306     albertel 3525:     if (!defined($title)) {
                   3526: 	$title = 'The LearningOnline Network with CAPA';
                   3527:     }
                   3528:     
1.315     albertel 3529:     $result .= '<title> LON-CAPA '.&mt($title).'</title>'.$head_extra;
1.306     albertel 3530:     return $result;
                   3531: }
                   3532: 
                   3533: =pod
                   3534: 
                   3535: =over 4
                   3536: 
1.340     albertel 3537: =item * &font_settings()
                   3538: 
                   3539: Returns neccessary <meta> to set the proper encoding
                   3540: 
                   3541: Inputs: none
                   3542: 
                   3543: =back
                   3544: 
                   3545: =cut
                   3546: 
                   3547: sub font_settings {
                   3548:     my $headerstring='';
                   3549:     if (($env{'browser.os'} eq 'mac') && (!$env{'browser.mathml'})) { 
                   3550: 	$headerstring.=
                   3551: 	    '<meta Content-Type="text/html; charset=x-mac-roman" />';
                   3552:     } elsif (!$env{'browser.mathml'} && $env{'browser.unicode'}) {
                   3553: 	$headerstring.=
                   3554: 	    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
                   3555:     }
                   3556:     return $headerstring;
                   3557: }
                   3558: 
1.341     albertel 3559: =pod
                   3560: 
                   3561: =over 4
                   3562: 
                   3563: =item * &xml_begin()
                   3564: 
                   3565: Returns the needed doctype and <html>
                   3566: 
                   3567: Inputs: none
                   3568: 
                   3569: =back
                   3570: 
                   3571: =cut
                   3572: 
                   3573: sub xml_begin {
                   3574:     my $output='';
                   3575: 
1.342     albertel 3576:     &Apache::lonhtmlcommon::init_htmlareafields();
                   3577: 
1.341     albertel 3578:     if ($env{'browser.mathml'}) {
                   3579: 	$output='<?xml version="1.0"?>'
                   3580:             #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
                   3581: #            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                   3582:             
                   3583: #	    .'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">] >'
                   3584: 	    .'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">'
                   3585:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                   3586: 	    .'xmlns="http://www.w3.org/1999/xhtml">';
                   3587:     } else {
                   3588: 	$output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>';
                   3589:     }
                   3590:     return $output;
                   3591: }
1.340     albertel 3592: 
                   3593: =pod
                   3594: 
                   3595: =over 4
                   3596: 
1.306     albertel 3597: =item * &endheadtag()
                   3598: 
                   3599: Returns a uniform </head> for LON-CAPA web pages.
                   3600: 
                   3601: Inputs: none
                   3602: 
                   3603: =back
                   3604: 
                   3605: =cut
                   3606: 
                   3607: sub endheadtag {
                   3608:     return '</head>';
                   3609: }
                   3610: 
                   3611: =pod
                   3612: 
                   3613: =over 4
                   3614: 
                   3615: =item * &head()
                   3616: 
                   3617: Returns a uniform complete <head>..</head> section for LON-CAPA web pages.
                   3618: 
                   3619: Inputs: $title - optional title for the page
1.307     albertel 3620:         $head_extra - optional extra HTML to put inside the <head>
1.405     albertel 3621: 
1.306     albertel 3622: =back
                   3623: 
                   3624: =cut
                   3625: 
                   3626: sub head {
1.325     albertel 3627:     my ($title,$head_extra,$args) = @_;
                   3628:     return &headtag($title,$head_extra,$args).&endheadtag();
1.306     albertel 3629: }
                   3630: 
                   3631: =pod
                   3632: 
                   3633: =over 4
                   3634: 
                   3635: =item * &start_page()
                   3636: 
                   3637: Returns a complete <html> .. <body> section for LON-CAPA web pages.
                   3638: 
                   3639: Inputs: $title - optional title for the page
                   3640:         $head_extra - optional extra HTML to incude inside the <head>
1.315     albertel 3641:         $args - additional optional args supported are:
1.317     albertel 3642:                   only_body      -> is true will set &bodytag() onlybodytag
                   3643:                                     arg on
                   3644:                   no_nav_bar     -> is true will set &bodytag() notopbar arg on
                   3645:                   add_entries    -> additional attributes to add to the  <body>
                   3646:                   domain         -> force to color decorate a page for a 
                   3647:                                     specific domain
                   3648:                   function       -> force usage of a specific rolish color
                   3649:                                     scheme
                   3650:                   redirect       -> see &headtag()
                   3651:                   bgcolor        -> override the default page bg color
                   3652:                   js_ready       -> return a string ready for being used in 
                   3653:                                     a javascript writeln
1.320     albertel 3654:                   html_encode    -> return a string ready for being used in 
                   3655:                                     a html attribute
1.317     albertel 3656:                   force_register -> if is true will turn on the &bodytag()
                   3657:                                     $forcereg arg
1.326     albertel 3658:                   body_title     -> alternate text to use instead of $title
                   3659:                                     in the title box that appears, this text
                   3660:                                     is not auto translated like the $title is
1.330     albertel 3661:                   frameset       -> if true will start with a <frameset>
                   3662:                                     rather than <body>
1.338     albertel 3663:                   no_title       -> if true the title bar won't be shown
                   3664:                   skip_phases    -> hash ref of 
                   3665:                                     head -> skip the <html><head> generation
                   3666:                                     body -> skip all <body> generation
1.337     albertel 3667: 
1.361     albertel 3668:                   no_inline_link -> if true and in remote mode, don't show the 
                   3669:                                     'Switch To Inline Menu' link
                   3670: 
1.306     albertel 3671: =back
                   3672: 
                   3673: =cut
                   3674: 
                   3675: sub start_page {
1.309     albertel 3676:     my ($title,$head_extra,$args) = @_;
1.318     albertel 3677:     #&Apache::lonnet::logthis("start_page ".join(':',caller(0)));
1.313     albertel 3678:     my %head_args;
1.352     albertel 3679:     foreach my $arg ('redirect','force_register','domain','function',
                   3680: 		     'bgcolor') {
1.319     albertel 3681: 	if (defined($args->{$arg})) {
1.324     raeburn  3682: 	    $head_args{$arg} = $args->{$arg};
1.319     albertel 3683: 	}
1.313     albertel 3684:     }
1.319     albertel 3685: 
1.315     albertel 3686:     $env{'internal.start_page'}++;
1.338     albertel 3687:     my $result;
                   3688:     if (! exists($args->{'skip_phases'}{'head'}) ) {
                   3689: 	$result.=
1.341     albertel 3690: 	    &xml_begin().
1.338     albertel 3691: 	    &headtag($title,$head_extra,\%head_args).&endheadtag();
                   3692:     }
                   3693:     
                   3694:     if (! exists($args->{'skip_phases'}{'body'}) ) {
                   3695: 	if ($args->{'frameset'}) {
                   3696: 	    my $attr_string = &make_attr_string($args->{'force_register'},
                   3697: 						$args->{'add_entries'});
                   3698: 	    $result .= "\n<frameset $attr_string>\n";
                   3699: 	} else {
                   3700: 	    $result .=
                   3701: 		&bodytag($title, 
                   3702: 			 $args->{'function'},       $args->{'add_entries'},
                   3703: 			 $args->{'only_body'},      $args->{'domain'},
                   3704: 			 $args->{'force_register'}, $args->{'body_title'},
                   3705: 			 $args->{'no_nav_bar'},     $args->{'bgcolor'},
1.361     albertel 3706: 			 $args->{'no_title'},       $args->{'no_inline_link'});
1.338     albertel 3707: 	}
1.330     albertel 3708:     }
1.338     albertel 3709: 
1.315     albertel 3710:     if ($args->{'js_ready'}) {
1.317     albertel 3711: 	$result = &js_ready($result);
1.315     albertel 3712:     }
1.320     albertel 3713:     if ($args->{'html_encode'}) {
                   3714: 	$result = &html_encode($result);
                   3715:     }
1.315     albertel 3716:     return $result;
1.306     albertel 3717: }
                   3718: 
1.330     albertel 3719: 
1.306     albertel 3720: =pod
                   3721: 
                   3722: =over 4
                   3723: 
                   3724: =item * &head()
                   3725: 
                   3726: Returns a complete </body></html> section for LON-CAPA web pages.
                   3727: 
1.315     albertel 3728: Inputs:         $args - additional optional args supported are:
                   3729:                  js_ready     -> return a string ready for being used in 
                   3730:                                  a javascript writeln
1.320     albertel 3731:                  html_encode  -> return a string ready for being used in 
                   3732:                                  a html attribute
1.330     albertel 3733:                  frameset     -> if true will start with a <frameset>
                   3734:                                  rather than <body>
1.306     albertel 3735: 
                   3736: =cut
                   3737: 
                   3738: sub end_page {
1.315     albertel 3739:     my ($args) = @_;
                   3740:     $env{'internal.end_page'}++;
1.330     albertel 3741:     my $result;
1.335     albertel 3742:     if ($args->{'discussion'}) {
                   3743: 	my ($target,$parser);
                   3744: 	if (ref($args->{'discussion'})) {
                   3745: 	    ($target,$parser) =($args->{'discussion'}{'target'},
                   3746: 				$args->{'discussion'}{'parser'});
                   3747: 	}
                   3748: 	$result .= &Apache::lonxml::xmlend($target,$parser);
                   3749:     }
                   3750: 
1.330     albertel 3751:     if ($args->{'frameset'}) {
                   3752: 	$result .= '</frameset>';
                   3753:     } else {
                   3754: 	$result .= &endbodytag();
                   3755:     }
                   3756:     $result .= "\n</html>";
                   3757: 
1.315     albertel 3758:     if ($args->{'js_ready'}) {
1.317     albertel 3759: 	$result = &js_ready($result);
1.315     albertel 3760:     }
1.335     albertel 3761: 
1.320     albertel 3762:     if ($args->{'html_encode'}) {
                   3763: 	$result = &html_encode($result);
                   3764:     }
1.335     albertel 3765: 
1.315     albertel 3766:     return $result;
                   3767: }
                   3768: 
1.320     albertel 3769: sub html_encode {
                   3770:     my ($result) = @_;
                   3771: 
1.322     albertel 3772:     $result = &HTML::Entities::encode($result,'<>&"');
1.320     albertel 3773:     
                   3774:     return $result;
                   3775: }
1.317     albertel 3776: sub js_ready {
                   3777:     my ($result) = @_;
                   3778: 
1.323     albertel 3779:     $result =~ s/[\n\r]/ /xmsg;
                   3780:     $result =~ s/\\/\\\\/xmsg;
                   3781:     $result =~ s/'/\\'/xmsg;
1.372     albertel 3782:     $result =~ s{</}{<\\/}xmsg;
1.317     albertel 3783:     
                   3784:     return $result;
                   3785: }
                   3786: 
1.315     albertel 3787: sub validate_page {
                   3788:     if (  exists($env{'internal.start_page'})
1.316     albertel 3789: 	  &&     $env{'internal.start_page'} > 1) {
                   3790: 	&Apache::lonnet::logthis('start_page called multiple times '.
1.318     albertel 3791: 				 $env{'internal.start_page'}.' '.
1.316     albertel 3792: 				 $ENV{'request.filename'});
1.315     albertel 3793:     }
                   3794:     if (  exists($env{'internal.end_page'})
1.316     albertel 3795: 	  &&     $env{'internal.end_page'} > 1) {
                   3796: 	&Apache::lonnet::logthis('end_page called multiple times '.
1.318     albertel 3797: 				 $env{'internal.end_page'}.' '.
1.316     albertel 3798: 				 $env{'request.filename'});
1.315     albertel 3799:     }
                   3800:     if (     exists($env{'internal.start_page'})
                   3801: 	&& ! exists($env{'internal.end_page'})) {
1.316     albertel 3802: 	&Apache::lonnet::logthis('start_page called without end_page '.
                   3803: 				 $env{'request.filename'});
1.315     albertel 3804:     }
                   3805:     if (   ! exists($env{'internal.start_page'})
                   3806: 	&&   exists($env{'internal.end_page'})) {
1.316     albertel 3807: 	&Apache::lonnet::logthis('end_page called without start_page'.
                   3808: 				 $env{'request.filename'});
1.315     albertel 3809:     }
1.306     albertel 3810: }
1.315     albertel 3811: 
1.318     albertel 3812: sub simple_error_page {
                   3813:     my ($r,$title,$msg) = @_;
                   3814:     my $page =
                   3815: 	&Apache::loncommon::start_page($title).
                   3816: 	&mt($msg).
                   3817: 	&Apache::loncommon::end_page();
                   3818:     if (ref($r)) {
                   3819: 	$r->print($page);
1.327     albertel 3820: 	return;
1.318     albertel 3821:     }
                   3822:     return $page;
                   3823: }
1.347     albertel 3824: 
                   3825: {
                   3826:     my $row_count;
                   3827:     sub start_data_table {
                   3828: 	undef($row_count);
1.389     albertel 3829: 	return '<table class="LC_data_table">'."\n";
1.347     albertel 3830:     }
                   3831: 
                   3832:     sub end_data_table {
                   3833: 	undef($row_count);
1.389     albertel 3834: 	return '</table>'."\n";;
1.347     albertel 3835:     }
                   3836: 
                   3837:     sub start_data_table_row {
                   3838: 	$row_count++;
1.389     albertel 3839: 	return  '<tr '.(($row_count % 2)?'':'class="LC_even_row"').'>'."\n";;
1.347     albertel 3840:     }
                   3841: 
                   3842:     sub end_data_table_row {
1.389     albertel 3843: 	return '</tr>'."\n";;
1.347     albertel 3844:     }
1.367     www      3845: 
                   3846:     sub start_data_table_header_row {
1.389     albertel 3847: 	return  '<tr class="LC_header_row">'."\n";;
1.367     www      3848:     }
                   3849: 
                   3850:     sub end_data_table_header_row {
1.389     albertel 3851: 	return '</tr>'."\n";;
1.367     www      3852:     }
1.347     albertel 3853: }
                   3854: 
1.251     albertel 3855: ###############################################
1.182     matthew  3856: 
                   3857: =pod
                   3858: 
1.405     albertel 3859: =item * &get_users_function()
1.182     matthew  3860: 
                   3861: Used by &bodytag to determine the current users primary role.
                   3862: Returns either 'student','coordinator','admin', or 'author'.
                   3863: 
                   3864: =cut
                   3865: 
                   3866: ###############################################
                   3867: sub get_users_function {
                   3868:     my $function = 'student';
1.258     albertel 3869:     if ($env{'request.role'}=~/^(cc|in|ta|ep)/) {
1.182     matthew  3870:         $function='coordinator';
                   3871:     }
1.258     albertel 3872:     if ($env{'request.role'}=~/^(su|dc|ad|li)/) {
1.182     matthew  3873:         $function='admin';
                   3874:     }
1.258     albertel 3875:     if (($env{'request.role'}=~/^(au|ca)/) ||
1.182     matthew  3876:         ($ENV{'REQUEST_URI'}=~/^(\/priv|\~)/)) {
                   3877:         $function='author';
                   3878:     }
                   3879:     return $function;
1.54      www      3880: }
1.99      www      3881: 
                   3882: ###############################################
                   3883: 
1.233     raeburn  3884: =pod
                   3885: 
1.405     albertel 3886: =item * &check_user_status
1.274     raeburn  3887: 
                   3888: Determines current status of supplied role for a
                   3889: specific user. Roles can be active, previous or future.
                   3890: 
                   3891: Inputs: 
                   3892: user's domain, user's username, course's domain,
1.375     raeburn  3893: course's number, optional section ID.
1.274     raeburn  3894: 
                   3895: Outputs:
                   3896: role status: active, previous or future. 
                   3897: 
                   3898: =cut
                   3899: 
                   3900: sub check_user_status {
                   3901:     my ($udom,$uname,$cdom,$crs,$role,$secgrp) = @_;
                   3902:     my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
                   3903:     my @uroles = keys %userinfo;
                   3904:     my $srchstr;
                   3905:     my $active_chk = 'none';
                   3906:     if (@uroles > 0) {
                   3907:         if (($role eq 'cc') || ($secgrp eq '') || (!defined($secgrp))) {
                   3908:             $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
                   3909:         } else {
                   3910:             $srchstr = '/'.$cdom.'/'.$crs.'/'.$secgrp.'_'.$role;         }
                   3911:         if (grep/^$srchstr$/,@uroles) {
                   3912:             my $role_end = 0;
                   3913:             my $role_start = 0;
                   3914:             $active_chk = 'active';
                   3915:             if ($userinfo{$srchstr} =~ m/^($role)_(\d+)/) {
                   3916:                 $role_end = $2;
                   3917:                 if ($userinfo{$srchstr} =~ m/^($role)_($role_end)_(\d+)$/) {
                   3918:                     $role_start = $3;
                   3919:                 }
                   3920:             }
                   3921:             if ($role_start > 0) {
                   3922:                 if (time < $role_start) {
                   3923:                     $active_chk = 'future';
                   3924:                 }
                   3925:             }
                   3926:             if ($role_end > 0) {
                   3927:                 if (time > $role_end) {
                   3928:                     $active_chk = 'previous';
                   3929:                 }
                   3930:             }
                   3931:         }
                   3932:     }
                   3933:     return $active_chk;
                   3934: }
                   3935: 
                   3936: ###############################################
                   3937: 
                   3938: =pod
                   3939: 
1.405     albertel 3940: =item * &get_sections()
1.233     raeburn  3941: 
                   3942: Determines all the sections for a course including
                   3943: sections with students and sections containing other roles.
1.374     raeburn  3944: Incoming parameters: domain, course number, 
                   3945: reference to array containing roles for which sections should 
                   3946: be gathered (optional). If the third argument is undefined,
                   3947: sections are gathered for any role.
1.233     raeburn  3948:  
1.374     raeburn  3949: Returns section hash (keys are section IDs, values are
                   3950: number of users in each section), subject to the
                   3951: optional roles filter.
1.233     raeburn  3952: 
                   3953: =cut
                   3954: 
                   3955: ###############################################
                   3956: sub get_sections {
1.366     albertel 3957:     my ($cdom,$cnum,$possible_roles) = @_;
                   3958:     if (!defined($cdom) || !defined($cnum)) {
                   3959:         my $cid =  $env{'request.course.id'};
                   3960: 
                   3961: 	return if (!defined($cid));
                   3962: 
                   3963:         $cdom = $env{'course.'.$cid.'.domain'};
                   3964:         $cnum = $env{'course.'.$cid.'.num'};
                   3965:     }
                   3966: 
                   3967:     my %sectioncount;
1.240     albertel 3968: 
1.366     albertel 3969:     if (!defined($possible_roles) || (grep(/^st$/,@$possible_roles))) {
1.276     albertel 3970: 	my ($classlist) = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1.240     albertel 3971: 	my $sec_index = &Apache::loncoursedata::CL_SECTION();
                   3972: 	my $status_index = &Apache::loncoursedata::CL_STATUS();
1.366     albertel 3973: 	while (my ($student,$data) = each(%$classlist)) {
1.240     albertel 3974: 	    my ($section,$status) = ($data->[$sec_index],
                   3975: 				     $data->[$status_index]);
                   3976: 	    unless ($section eq '-1' || $section =~ /^\s*$/) {
1.366     albertel 3977: 		$sectioncount{$section}++;
1.240     albertel 3978: 	    }
                   3979: 	}
                   3980:     }
                   3981:     my %courseroles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   3982:     foreach my $user (sort(keys(%courseroles))) {
                   3983: 	if ($user !~ /^(\w{2})/) { next; }
                   3984: 	my ($role) = ($user =~ /^(\w{2})/);
                   3985: 	if ($possible_roles && !(grep(/^$role$/,@$possible_roles))) { next; }
                   3986: 	my $section;
                   3987: 	if ($role eq 'cr' &&
                   3988: 	    $user =~ m-^$role/[^/]*/[^/]*/[^/]*:[^:]*:[^:]*:(\w+)-) {
                   3989: 	    $section=$1;
                   3990: 	}
                   3991: 	if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
                   3992: 	if (!defined($section) || $section eq '-1') { next; }
1.366     albertel 3993: 	$sectioncount{$section}++;
1.233     raeburn  3994:     }
1.366     albertel 3995:     return %sectioncount;
1.233     raeburn  3996: }
                   3997: 
1.274     raeburn  3998: ###############################################
1.294     raeburn  3999: 
                   4000: =pod
1.405     albertel 4001: 
                   4002: =item * &get_course_users()
                   4003: 
1.275     raeburn  4004: Retrieves usernames:domains for users in the specified course
                   4005: with specific role(s), and access status. 
                   4006: 
                   4007: Incoming parameters:
1.277     albertel 4008: 1. course domain
                   4009: 2. course number
                   4010: 3. access status: users must have - either active, 
1.275     raeburn  4011: previous, future, or all.
1.277     albertel 4012: 4. reference to array of permissible roles
1.288     raeburn  4013: 5. reference to array of section restrictions (optional)
                   4014: 6. reference to results object (hash of hashes).
                   4015: 7. reference to optional userdata hash
1.275     raeburn  4016: Keys of top level hash are roles.
                   4017: Keys of inner hashes are username:domain, with 
                   4018: values set to access type.
1.288     raeburn  4019: Optional userdata hash returns an array with arguments in the 
                   4020: same order as loncoursedata::get_classlist() for student data.
                   4021: 
                   4022: Entries for end, start, section and status are blank because
                   4023: of the possibility of multiple values for non-student roles.
                   4024: 
1.275     raeburn  4025: =cut
1.405     albertel 4026: 
1.275     raeburn  4027: ###############################################
1.405     albertel 4028: 
1.275     raeburn  4029: sub get_course_users {
1.288     raeburn  4030:     my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata) = @_;
                   4031:     my %idx = ();
                   4032: 
                   4033:     $idx{udom} = &Apache::loncoursedata::CL_SDOM();
                   4034:     $idx{uname} =  &Apache::loncoursedata::CL_SNAME();
                   4035:     $idx{end} = &Apache::loncoursedata::CL_END();
                   4036:     $idx{start} = &Apache::loncoursedata::CL_START();
                   4037:     $idx{id} = &Apache::loncoursedata::CL_ID();
                   4038:     $idx{section} = &Apache::loncoursedata::CL_SECTION();
                   4039:     $idx{fullname} = &Apache::loncoursedata::CL_FULLNAME();
                   4040:     $idx{status} = &Apache::loncoursedata::CL_STATUS();
                   4041: 
1.290     albertel 4042:     if (grep(/^st$/,@{$roles})) {
1.276     albertel 4043:         my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist($cdom,$cnum);
1.278     raeburn  4044:         my $now = time;
1.277     albertel 4045:         foreach my $student (keys(%{$classlist})) {
1.288     raeburn  4046:             my $match = 0;
1.291     albertel 4047:             if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.290     albertel 4048: 		unless(grep(/^\Q$$classlist{$student}[$idx{section}]\E$/,
                   4049: 			    @{$sections})) {
                   4050: 		    next;
                   4051: 		}
1.288     raeburn  4052:             } 
1.275     raeburn  4053:             if (defined($$types{'active'})) {
1.288     raeburn  4054:                 if ($$classlist{$student}[$idx{status}] eq 'Active') {
1.275     raeburn  4055:                     push(@{$$users{st}{$student}},'active');
1.288     raeburn  4056:                     $match = 1;
1.275     raeburn  4057:                 }
                   4058:             }
                   4059:             if (defined($$types{'previous'})) {
1.288     raeburn  4060:                 if ($$classlist{$student}[$idx{end}] <= $now) {
1.275     raeburn  4061:                     push(@{$$users{st}{$student}},'previous');
1.288     raeburn  4062:                     $match = 1;
1.275     raeburn  4063:                 }
                   4064:             }
                   4065:             if (defined($$types{'future'})) {
1.288     raeburn  4066:                 if (($$classlist{$student}[$idx{start}] > $now) && ($$classlist{$student}[$idx{end}] > $now) || ($$classlist{$student}[$idx{end}] == 0) || ($$classlist{$student}[$idx{end}] eq '')) {
1.275     raeburn  4067:                     push(@{$$users{st}{$student}},'future');
1.288     raeburn  4068:                     $match = 1;
1.275     raeburn  4069:                 }
                   4070:             }
1.288     raeburn  4071:             if ($match && defined($userdata)) {
                   4072:                 $$userdata{$student} = $$classlist{$student};
                   4073:             }
1.275     raeburn  4074:         }
                   4075:     }
                   4076:     if ((@{$roles} > 0) && (@{$roles} ne "st")) {
                   4077:         my @coursepersonnel = &Apache::lonnet::getkeys('nohist_userroles',$cdom,$cnum);
                   4078:         foreach my $person (@coursepersonnel) {
1.288     raeburn  4079:             my $match = 0;
1.275     raeburn  4080:             my ($role,$user) = ($person =~ /^([^:]*):([^:]+:[^:]+)/);
                   4081:             $user =~ s/:$//;
1.290     albertel 4082:             if (($role) && (grep(/^\Q$role\E$/,@{$roles}))) {
1.288     raeburn  4083:                 my ($uname,$udom,$usec) = split(/:/,$user);
1.290     albertel 4084:                 if ($usec ne '' && (ref($sections) eq 'ARRAY') && 
                   4085: 		    @{$sections} > 0) {
                   4086: 		    unless(grep(/^\Q$usec\E$/,@{$sections})) {
                   4087: 			next;
                   4088: 		    }
1.288     raeburn  4089:                 }
1.275     raeburn  4090:                 if ($uname ne '' && $udom ne '') {
                   4091:                     my $status = &check_user_status($udom,$uname,$cdom,$cnum,$role);
1.277     albertel 4092:                     foreach my $type (keys(%{$types})) { 
1.275     raeburn  4093:                         if ($status eq $type) {
1.288     raeburn  4094:                             @{$$users{$role}{$user}} = $type;
                   4095:                             $match = 1;
                   4096:                         }
                   4097:                     }
1.290     albertel 4098:                     if ($match && defined($userdata) &&
                   4099:                         !exists($$userdata{$uname.':'.$udom})) {
                   4100: 			&get_user_info($udom,$uname,\%idx,$userdata);
1.275     raeburn  4101:                     }
                   4102:                 }
                   4103:             }
                   4104:         }
1.290     albertel 4105:         if (grep(/^ow$/,@{$roles})) {
1.279     raeburn  4106:             if ((defined($cdom)) && (defined($cnum))) {
                   4107:                 my %csettings = &Apache::lonnet::get('environment',['internal.courseowner'],$cdom,$cnum);
                   4108:                 if ( defined($csettings{'internal.courseowner'}) ) {
                   4109:                     my $owner = $csettings{'internal.courseowner'};
1.288     raeburn  4110:                     @{$$users{'ow'}{$owner.':'.$cdom}} = 'any';
1.290     albertel 4111:                     if (defined($userdata) && 
                   4112: 			!exists($$userdata{$owner.':'.$cdom})) {
                   4113: 			&get_user_info($cdom,$owner,\%idx,$userdata);
                   4114: 		    }
1.279     raeburn  4115:                 }
                   4116:             }
                   4117:         }
1.275     raeburn  4118:     }
                   4119:     return;
                   4120: }
                   4121: 
1.288     raeburn  4122: sub get_user_info {
                   4123:     my ($udom,$uname,$idx,$userdata) = @_;
1.289     albertel 4124:     $$userdata{$uname.':'.$udom}[$$idx{fullname}] = 
                   4125: 	&plainname($uname,$udom,'lastname');
1.291     albertel 4126:     $$userdata{$uname.':'.$udom}[$$idx{uname}] = $uname;
1.297     raeburn  4127:     $$userdata{$uname.':'.$udom}[$$idx{udom}] = $udom;
1.288     raeburn  4128:     return;
                   4129: }
1.275     raeburn  4130: 
1.384     raeburn  4131: sub get_secgrprole_info {
                   4132:     my ($cdom,$cnum,$needroles,$type)  = @_;
                   4133:     my %sections_count = &get_sections($cdom,$cnum);
                   4134:     my @sections =  (sort {$a <=> $b} keys(%sections_count));
                   4135:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
                   4136:     my @groups = sort(keys(%curr_groups));
                   4137:     my $allroles = [];
                   4138:     my $rolehash;
                   4139:     my $accesshash = {
                   4140:                      active => 'Currently has access',
                   4141:                      future => 'Will have future access',
                   4142:                      previous => 'Previously had access',
                   4143:                   };
                   4144:     if ($needroles) {
                   4145:         $rolehash = {'all' => 'all'};
1.385     albertel 4146:         my %user_roles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   4147: 	if (&Apache::lonnet::error(%user_roles)) {
                   4148: 	    undef(%user_roles);
                   4149: 	}
                   4150:         foreach my $item (keys(%user_roles)) {
1.384     raeburn  4151:             my ($role)=split(/\:/,$item,2);
                   4152:             if ($role eq 'cr') { next; }
                   4153:             if ($role =~ /^cr/) {
                   4154:                 $$rolehash{$role} = (split('/',$role))[3];
                   4155:             } else {
                   4156:                 $$rolehash{$role} = &Apache::lonnet::plaintext($role,$type);
                   4157:             }
                   4158:         }
                   4159:         foreach my $key (sort(keys(%{$rolehash}))) {
                   4160:             push(@{$allroles},$key);
                   4161:         }
                   4162:         push (@{$allroles},'st');
                   4163:         $$rolehash{'st'} = &Apache::lonnet::plaintext('st',$type);
                   4164:     }
                   4165:     return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
                   4166: }
                   4167: 
1.112     bowersj2 4168: =pod
                   4169: 
                   4170: =item * get_unprocessed_cgi($query,$possible_names)
                   4171: 
1.258     albertel 4172: Modify the %env hash to contain unprocessed CGI form parameters held in
1.112     bowersj2 4173: $query.  The parameters listed in $possible_names (an array reference),
1.258     albertel 4174: will be set in $env{'form.name'} if they do not already exist.
1.112     bowersj2 4175: 
                   4176: Typically called with $ENV{'QUERY_STRING'} as the first parameter.  
                   4177: $possible_names is an ref to an array of form element names.  As an example:
                   4178: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
1.258     albertel 4179: will result in $env{'form.uname'} and $env{'form.udom'} being set.
1.112     bowersj2 4180: 
                   4181: =cut
1.1       albertel 4182: 
1.6       albertel 4183: sub get_unprocessed_cgi {
1.25      albertel 4184:   my ($query,$possible_names)= @_;
1.26      matthew  4185:   # $Apache::lonxml::debug=1;
1.356     albertel 4186:   foreach my $pair (split(/&/,$query)) {
                   4187:     my ($name, $value) = split(/=/,$pair);
1.369     www      4188:     $name = &unescape($name);
1.25      albertel 4189:     if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
                   4190:       $value =~ tr/+/ /;
                   4191:       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.258     albertel 4192:       unless (defined($env{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
1.25      albertel 4193:     }
1.16      harris41 4194:   }
1.6       albertel 4195: }
                   4196: 
1.112     bowersj2 4197: =pod
                   4198: 
                   4199: =item * cacheheader() 
                   4200: 
                   4201: returns cache-controlling header code
                   4202: 
                   4203: =cut
                   4204: 
1.7       albertel 4205: sub cacheheader {
1.258     albertel 4206:     unless ($env{'request.method'} eq 'GET') { return ''; }
1.216     albertel 4207:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
                   4208:     my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
1.7       albertel 4209:                 <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
                   4210:                 <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
1.216     albertel 4211:     return $output;
1.7       albertel 4212: }
                   4213: 
1.112     bowersj2 4214: =pod
                   4215: 
                   4216: =item * no_cache($r) 
                   4217: 
                   4218: specifies header code to not have cache
                   4219: 
                   4220: =cut
                   4221: 
1.9       albertel 4222: sub no_cache {
1.216     albertel 4223:     my ($r) = @_;
                   4224:     if ($ENV{'REQUEST_METHOD'} ne 'GET' &&
1.258     albertel 4225: 	$env{'request.method'} ne 'GET') { return ''; }
1.216     albertel 4226:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time));
                   4227:     $r->no_cache(1);
                   4228:     $r->header_out("Expires" => $date);
                   4229:     $r->header_out("Pragma" => "no-cache");
1.123     www      4230: }
                   4231: 
                   4232: sub content_type {
1.181     albertel 4233:     my ($r,$type,$charset) = @_;
1.299     foxr     4234:     if ($r) {
                   4235: 	#  Note that printout.pl calls this with undef for $r.
                   4236: 	&no_cache($r);
                   4237:     }
1.258     albertel 4238:     if ($env{'browser.mathml'} && $type eq 'text/html') { $type='text/xml'; }
1.181     albertel 4239:     unless ($charset) {
                   4240: 	$charset=&Apache::lonlocal::current_encoding;
                   4241:     }
                   4242:     if ($charset) { $type.='; charset='.$charset; }
                   4243:     if ($r) {
                   4244: 	$r->content_type($type);
                   4245:     } else {
                   4246: 	print("Content-type: $type\n\n");
                   4247:     }
1.9       albertel 4248: }
1.25      albertel 4249: 
1.112     bowersj2 4250: =pod
                   4251: 
                   4252: =item * add_to_env($name,$value) 
                   4253: 
1.258     albertel 4254: adds $name to the %env hash with value
1.112     bowersj2 4255: $value, if $name already exists, the entry is converted to an array
                   4256: reference and $value is added to the array.
                   4257: 
                   4258: =cut
                   4259: 
1.25      albertel 4260: sub add_to_env {
                   4261:   my ($name,$value)=@_;
1.258     albertel 4262:   if (defined($env{$name})) {
                   4263:     if (ref($env{$name})) {
1.25      albertel 4264:       #already have multiple values
1.258     albertel 4265:       push(@{ $env{$name} },$value);
1.25      albertel 4266:     } else {
                   4267:       #first time seeing multiple values, convert hash entry to an arrayref
1.258     albertel 4268:       my $first=$env{$name};
                   4269:       undef($env{$name});
                   4270:       push(@{ $env{$name} },$first,$value);
1.25      albertel 4271:     }
                   4272:   } else {
1.258     albertel 4273:     $env{$name}=$value;
1.25      albertel 4274:   }
1.31      albertel 4275: }
1.149     albertel 4276: 
                   4277: =pod
                   4278: 
                   4279: =item * get_env_multiple($name) 
                   4280: 
1.258     albertel 4281: gets $name from the %env hash, it seemlessly handles the cases where multiple
1.149     albertel 4282: values may be defined and end up as an array ref.
                   4283: 
                   4284: returns an array of values
                   4285: 
                   4286: =cut
                   4287: 
                   4288: sub get_env_multiple {
                   4289:     my ($name) = @_;
                   4290:     my @values;
1.258     albertel 4291:     if (defined($env{$name})) {
1.149     albertel 4292:         # exists is it an array
1.258     albertel 4293:         if (ref($env{$name})) {
                   4294:             @values=@{ $env{$name} };
1.149     albertel 4295:         } else {
1.258     albertel 4296:             $values[0]=$env{$name};
1.149     albertel 4297:         }
                   4298:     }
                   4299:     return(@values);
                   4300: }
                   4301: 
1.31      albertel 4302: 
1.41      ng       4303: =pod
1.45      matthew  4304: 
                   4305: =back 
1.41      ng       4306: 
1.112     bowersj2 4307: =head1 CSV Upload/Handling functions
1.38      albertel 4308: 
1.41      ng       4309: =over 4
                   4310: 
1.112     bowersj2 4311: =item * upfile_store($r)
1.41      ng       4312: 
                   4313: Store uploaded file, $r should be the HTTP Request object,
1.258     albertel 4314: needs $env{'form.upfile'}
1.41      ng       4315: returns $datatoken to be put into hidden field
                   4316: 
                   4317: =cut
1.31      albertel 4318: 
                   4319: sub upfile_store {
                   4320:     my $r=shift;
1.258     albertel 4321:     $env{'form.upfile'}=~s/\r/\n/gs;
                   4322:     $env{'form.upfile'}=~s/\f/\n/gs;
                   4323:     $env{'form.upfile'}=~s/\n+/\n/gs;
                   4324:     $env{'form.upfile'}=~s/\n+$//gs;
1.31      albertel 4325: 
1.258     albertel 4326:     my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
                   4327: 	'_enroll_'.$env{'request.course.id'}.'_'.time.'_'.$$;
1.31      albertel 4328:     {
1.158     raeburn  4329:         my $datafile = $r->dir_config('lonDaemons').
                   4330:                            '/tmp/'.$datatoken.'.tmp';
                   4331:         if ( open(my $fh,">$datafile") ) {
1.258     albertel 4332:             print $fh $env{'form.upfile'};
1.158     raeburn  4333:             close($fh);
                   4334:         }
1.31      albertel 4335:     }
                   4336:     return $datatoken;
                   4337: }
                   4338: 
1.56      matthew  4339: =pod
                   4340: 
1.112     bowersj2 4341: =item * load_tmp_file($r)
1.41      ng       4342: 
                   4343: Load uploaded file from tmp, $r should be the HTTP Request object,
1.258     albertel 4344: needs $env{'form.datatoken'},
                   4345: sets $env{'form.upfile'} to the contents of the file
1.41      ng       4346: 
                   4347: =cut
1.31      albertel 4348: 
                   4349: sub load_tmp_file {
                   4350:     my $r=shift;
                   4351:     my @studentdata=();
                   4352:     {
1.158     raeburn  4353:         my $studentfile = $r->dir_config('lonDaemons').
1.258     albertel 4354:                               '/tmp/'.$env{'form.datatoken'}.'.tmp';
1.158     raeburn  4355:         if ( open(my $fh,"<$studentfile") ) {
                   4356:             @studentdata=<$fh>;
                   4357:             close($fh);
                   4358:         }
1.31      albertel 4359:     }
1.258     albertel 4360:     $env{'form.upfile'}=join('',@studentdata);
1.31      albertel 4361: }
                   4362: 
1.56      matthew  4363: =pod
                   4364: 
1.112     bowersj2 4365: =item * upfile_record_sep()
1.41      ng       4366: 
                   4367: Separate uploaded file into records
                   4368: returns array of records,
1.258     albertel 4369: needs $env{'form.upfile'} and $env{'form.upfiletype'}
1.41      ng       4370: 
                   4371: =cut
1.31      albertel 4372: 
                   4373: sub upfile_record_sep {
1.258     albertel 4374:     if ($env{'form.upfiletype'} eq 'xml') {
1.31      albertel 4375:     } else {
1.248     albertel 4376: 	my @records;
1.258     albertel 4377: 	foreach my $line (split(/\n/,$env{'form.upfile'})) {
1.248     albertel 4378: 	    if ($line=~/^\s*$/) { next; }
                   4379: 	    push(@records,$line);
                   4380: 	}
                   4381: 	return @records;
1.31      albertel 4382:     }
                   4383: }
                   4384: 
1.56      matthew  4385: =pod
                   4386: 
1.112     bowersj2 4387: =item * record_sep($record)
1.41      ng       4388: 
1.258     albertel 4389: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $env{'form.upfiletype'}
1.41      ng       4390: 
                   4391: =cut
                   4392: 
1.263     www      4393: sub takeleft {
                   4394:     my $index=shift;
                   4395:     return substr('0000'.$index,-4,4);
                   4396: }
                   4397: 
1.31      albertel 4398: sub record_sep {
                   4399:     my $record=shift;
                   4400:     my %components=();
1.258     albertel 4401:     if ($env{'form.upfiletype'} eq 'xml') {
                   4402:     } elsif ($env{'form.upfiletype'} eq 'space') {
1.31      albertel 4403:         my $i=0;
1.356     albertel 4404:         foreach my $field (split(/\s+/,$record)) {
1.31      albertel 4405:             $field=~s/^(\"|\')//;
                   4406:             $field=~s/(\"|\')$//;
1.263     www      4407:             $components{&takeleft($i)}=$field;
1.31      albertel 4408:             $i++;
                   4409:         }
1.258     albertel 4410:     } elsif ($env{'form.upfiletype'} eq 'tab') {
1.31      albertel 4411:         my $i=0;
1.356     albertel 4412:         foreach my $field (split(/\t/,$record)) {
1.31      albertel 4413:             $field=~s/^(\"|\')//;
                   4414:             $field=~s/(\"|\')$//;
1.263     www      4415:             $components{&takeleft($i)}=$field;
1.31      albertel 4416:             $i++;
                   4417:         }
                   4418:     } else {
                   4419:         my @allfields=split(/\,/,$record);
                   4420:         my $i=0;
                   4421:         my $j;
                   4422:         for ($j=0;$j<=$#allfields;$j++) {
                   4423:             my $field=$allfields[$j];
                   4424:             if ($field=~/^\s*(\"|\')/) {
                   4425: 		my $delimiter=$1;
                   4426:                 while (($field!~/$delimiter$/) && ($j<$#allfields)) {
                   4427: 		    $j++;
                   4428: 		    $field.=','.$allfields[$j];
                   4429: 		}
                   4430:                 $field=~s/^\s*$delimiter//;
                   4431:                 $field=~s/$delimiter\s*$//;
                   4432:             }
1.263     www      4433:             $components{&takeleft($i)}=$field;
1.31      albertel 4434: 	    $i++;
                   4435:         }
                   4436:     }
                   4437:     return %components;
                   4438: }
                   4439: 
1.144     matthew  4440: ######################################################
                   4441: ######################################################
                   4442: 
1.56      matthew  4443: =pod
                   4444: 
1.112     bowersj2 4445: =item * upfile_select_html()
1.41      ng       4446: 
1.144     matthew  4447: Return HTML code to select a file from the users machine and specify 
                   4448: the file type.
1.41      ng       4449: 
                   4450: =cut
                   4451: 
1.144     matthew  4452: ######################################################
                   4453: ######################################################
1.31      albertel 4454: sub upfile_select_html {
1.144     matthew  4455:     my %Types = (
                   4456:                  csv   => &mt('CSV (comma separated values, spreadsheet)'),
                   4457:                  space => &mt('Space separated'),
                   4458:                  tab   => &mt('Tabulator separated'),
                   4459: #                 xml   => &mt('HTML/XML'),
                   4460:                  );
                   4461:     my $Str = '<input type="file" name="upfile" size="50" />'.
                   4462:         '<br />Type: <select name="upfiletype">';
                   4463:     foreach my $type (sort(keys(%Types))) {
                   4464:         $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
                   4465:     }
                   4466:     $Str .= "</select>\n";
                   4467:     return $Str;
1.31      albertel 4468: }
                   4469: 
1.301     albertel 4470: sub get_samples {
                   4471:     my ($records,$toget) = @_;
                   4472:     my @samples=({});
                   4473:     my $got=0;
                   4474:     foreach my $rec (@$records) {
                   4475: 	my %temp = &record_sep($rec);
                   4476: 	if (! grep(/\S/, values(%temp))) { next; }
                   4477: 	if (%temp) {
                   4478: 	    $samples[$got]=\%temp;
                   4479: 	    $got++;
                   4480: 	    if ($got == $toget) { last; }
                   4481: 	}
                   4482:     }
                   4483:     return \@samples;
                   4484: }
                   4485: 
1.144     matthew  4486: ######################################################
                   4487: ######################################################
                   4488: 
1.56      matthew  4489: =pod
                   4490: 
1.112     bowersj2 4491: =item * csv_print_samples($r,$records)
1.41      ng       4492: 
                   4493: Prints a table of sample values from each column uploaded $r is an
                   4494: Apache Request ref, $records is an arrayref from
                   4495: &Apache::loncommon::upfile_record_sep
                   4496: 
                   4497: =cut
                   4498: 
1.144     matthew  4499: ######################################################
                   4500: ######################################################
1.31      albertel 4501: sub csv_print_samples {
                   4502:     my ($r,$records) = @_;
1.301     albertel 4503:     my $samples = &get_samples($records,3);
                   4504: 
1.144     matthew  4505:     $r->print(&mt('Samples').'<br /><table border="2"><tr>');
1.356     albertel 4506:     foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) { 
                   4507:         $r->print('<th>'.&mt('Column&nbsp;[_1]',($sample+1)).'</th>'); }
1.31      albertel 4508:     $r->print('</tr>');
1.301     albertel 4509:     foreach my $hash (@$samples) {
1.31      albertel 4510: 	$r->print('<tr>');
1.356     albertel 4511: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
1.31      albertel 4512: 	    $r->print('<td>');
1.356     albertel 4513: 	    if (defined($$hash{$sample})) { $r->print($$hash{$sample}); }
1.31      albertel 4514: 	    $r->print('</td>');
                   4515: 	}
                   4516: 	$r->print('</tr>');
                   4517:     }
                   4518:     $r->print('</tr></table><br />'."\n");
                   4519: }
                   4520: 
1.144     matthew  4521: ######################################################
                   4522: ######################################################
                   4523: 
1.56      matthew  4524: =pod
                   4525: 
1.112     bowersj2 4526: =item * csv_print_select_table($r,$records,$d)
1.41      ng       4527: 
                   4528: Prints a table to create associations between values and table columns.
1.144     matthew  4529: 
1.41      ng       4530: $r is an Apache Request ref,
                   4531: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
1.174     matthew  4532: $d is an array of 2 element arrays (internal name, displayed name,defaultcol)
1.41      ng       4533: 
                   4534: =cut
                   4535: 
1.144     matthew  4536: ######################################################
                   4537: ######################################################
1.31      albertel 4538: sub csv_print_select_table {
                   4539:     my ($r,$records,$d) = @_;
1.301     albertel 4540:     my $i=0;
                   4541:     my $samples = &get_samples($records,1);
1.144     matthew  4542:     $r->print(&mt('Associate columns with student attributes.')."\n".
                   4543: 	     '<table border="2"><tr>'.
                   4544:               '<th>'.&mt('Attribute').'</th>'.
                   4545:               '<th>'.&mt('Column').'</th></tr>'."\n");
1.356     albertel 4546:     foreach my $array_ref (@$d) {
                   4547: 	my ($value,$display,$defaultcol)=@{ $array_ref };
1.31      albertel 4548: 	$r->print('<tr><td>'.$display.'</td>');
                   4549: 
                   4550: 	$r->print('<td><select name=f'.$i.
1.32      matthew  4551: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.31      albertel 4552: 	$r->print('<option value="none"></option>');
1.356     albertel 4553: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
                   4554: 	    $r->print('<option value="'.$sample.'"'.
                   4555:                       ($sample eq $defaultcol ? ' selected="selected" ' : '').
                   4556:                       '>Column '.($sample+1).'</option>');
1.31      albertel 4557: 	}
                   4558: 	$r->print('</select></td></tr>'."\n");
                   4559: 	$i++;
                   4560:     }
                   4561:     $i--;
                   4562:     return $i;
                   4563: }
1.56      matthew  4564: 
1.144     matthew  4565: ######################################################
                   4566: ######################################################
                   4567: 
1.56      matthew  4568: =pod
1.31      albertel 4569: 
1.112     bowersj2 4570: =item * csv_samples_select_table($r,$records,$d)
1.41      ng       4571: 
                   4572: Prints a table of sample values from the upload and can make associate samples to internal names.
                   4573: 
                   4574: $r is an Apache Request ref,
                   4575: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
                   4576: $d is an array of 2 element arrays (internal name, displayed name)
                   4577: 
                   4578: =cut
                   4579: 
1.144     matthew  4580: ######################################################
                   4581: ######################################################
1.31      albertel 4582: sub csv_samples_select_table {
                   4583:     my ($r,$records,$d) = @_;
                   4584:     my $i=0;
1.144     matthew  4585:     #
1.301     albertel 4586:     my $samples = &get_samples($records,3);
1.144     matthew  4587:     $r->print('<table border=2><tr><th>'.
                   4588:               &mt('Field').'</th><th>'.&mt('Samples').'</th></tr>');
1.301     albertel 4589: 
                   4590:     foreach my $key (sort(keys(%{ $samples->[0] }))) {
1.144     matthew  4591: 	$r->print('<tr><td><select name="f'.$i.'"'.
1.32      matthew  4592: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.301     albertel 4593: 	foreach my $option (@$d) {
                   4594: 	    my ($value,$display,$defaultcol)=@{ $option };
1.174     matthew  4595: 	    $r->print('<option value="'.$value.'"'.
1.253     albertel 4596:                       ($i eq $defaultcol ? ' selected="selected" ':'').'>'.
1.174     matthew  4597:                       $display.'</option>');
1.31      albertel 4598: 	}
                   4599: 	$r->print('</select></td><td>');
1.301     albertel 4600: 	foreach my $line (0..2) {
                   4601: 	    if (defined($samples->[$line]{$key})) { 
                   4602: 		$r->print($samples->[$line]{$key}."<br />\n"); 
                   4603: 	    }
                   4604: 	}
1.31      albertel 4605: 	$r->print('</td></tr>');
                   4606: 	$i++;
                   4607:     }
                   4608:     $i--;
                   4609:     return($i);
1.115     matthew  4610: }
                   4611: 
1.144     matthew  4612: ######################################################
                   4613: ######################################################
                   4614: 
1.115     matthew  4615: =pod
                   4616: 
                   4617: =item clean_excel_name($name)
                   4618: 
                   4619: Returns a replacement for $name which does not contain any illegal characters.
                   4620: 
                   4621: =cut
                   4622: 
1.144     matthew  4623: ######################################################
                   4624: ######################################################
1.115     matthew  4625: sub clean_excel_name {
                   4626:     my ($name) = @_;
                   4627:     $name =~ s/[:\*\?\/\\]//g;
                   4628:     if (length($name) > 31) {
                   4629:         $name = substr($name,0,31);
                   4630:     }
                   4631:     return $name;
1.25      albertel 4632: }
1.84      albertel 4633: 
1.85      albertel 4634: =pod
                   4635: 
1.112     bowersj2 4636: =item * check_if_partid_hidden($id,$symb,$udom,$uname)
1.85      albertel 4637: 
                   4638: Returns either 1 or undef
                   4639: 
                   4640: 1 if the part is to be hidden, undef if it is to be shown
                   4641: 
                   4642: Arguments are:
                   4643: 
                   4644: $id the id of the part to be checked
                   4645: $symb, optional the symb of the resource to check
                   4646: $udom, optional the domain of the user to check for
                   4647: $uname, optional the username of the user to check for
                   4648: 
                   4649: =cut
1.84      albertel 4650: 
                   4651: sub check_if_partid_hidden {
                   4652:     my ($id,$symb,$udom,$uname) = @_;
1.133     albertel 4653:     my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
1.84      albertel 4654: 					 $symb,$udom,$uname);
1.141     albertel 4655:     my $truth=1;
                   4656:     #if the string starts with !, then the list is the list to show not hide
                   4657:     if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
1.84      albertel 4658:     my @hiddenlist=split(/,/,$hiddenparts);
                   4659:     foreach my $checkid (@hiddenlist) {
1.141     albertel 4660: 	if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
1.84      albertel 4661:     }
1.141     albertel 4662:     return !$truth;
1.84      albertel 4663: }
1.127     matthew  4664: 
1.138     matthew  4665: 
                   4666: ############################################################
                   4667: ############################################################
                   4668: 
                   4669: =pod
                   4670: 
1.157     matthew  4671: =back 
                   4672: 
1.138     matthew  4673: =head1 cgi-bin script and graphing routines
                   4674: 
1.157     matthew  4675: =over 4
                   4676: 
1.138     matthew  4677: =item get_cgi_id
                   4678: 
                   4679: Inputs: none
                   4680: 
                   4681: Returns an id which can be used to pass environment variables
                   4682: to various cgi-bin scripts.  These environment variables will
                   4683: be removed from the users environment after a given time by
                   4684: the routine &Apache::lonnet::transfer_profile_to_env.
                   4685: 
                   4686: =cut
                   4687: 
                   4688: ############################################################
                   4689: ############################################################
1.152     albertel 4690: my $uniq=0;
1.136     matthew  4691: sub get_cgi_id {
1.154     albertel 4692:     $uniq=($uniq+1)%100000;
1.280     albertel 4693:     return (time.'_'.$$.'_'.$uniq);
1.136     matthew  4694: }
                   4695: 
1.127     matthew  4696: ############################################################
                   4697: ############################################################
                   4698: 
                   4699: =pod
                   4700: 
1.134     matthew  4701: =item DrawBarGraph
1.127     matthew  4702: 
1.138     matthew  4703: Facilitates the plotting of data in a (stacked) bar graph.
                   4704: Puts plot definition data into the users environment in order for 
                   4705: graph.png to plot it.  Returns an <img> tag for the plot.
                   4706: The bars on the plot are labeled '1','2',...,'n'.
                   4707: 
                   4708: Inputs:
                   4709: 
                   4710: =over 4
                   4711: 
                   4712: =item $Title: string, the title of the plot
                   4713: 
                   4714: =item $xlabel: string, text describing the X-axis of the plot
                   4715: 
                   4716: =item $ylabel: string, text describing the Y-axis of the plot
                   4717: 
                   4718: =item $Max: scalar, the maximum Y value to use in the plot
                   4719: If $Max is < any data point, the graph will not be rendered.
                   4720: 
1.140     matthew  4721: =item $colors: array ref holding the colors to be used for the data sets when
1.138     matthew  4722: they are plotted.  If undefined, default values will be used.
                   4723: 
1.178     matthew  4724: =item $labels: array ref holding the labels to use on the x-axis for the bars.
                   4725: 
1.138     matthew  4726: =item @Values: An array of array references.  Each array reference holds data
                   4727: to be plotted in a stacked bar chart.
                   4728: 
1.239     matthew  4729: =item If the final element of @Values is a hash reference the key/value
                   4730: pairs will be added to the graph definition.
                   4731: 
1.138     matthew  4732: =back
                   4733: 
                   4734: Returns:
                   4735: 
                   4736: An <img> tag which references graph.png and the appropriate identifying
                   4737: information for the plot.
                   4738: 
1.127     matthew  4739: =cut
                   4740: 
                   4741: ############################################################
                   4742: ############################################################
1.134     matthew  4743: sub DrawBarGraph {
1.178     matthew  4744:     my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
1.134     matthew  4745:     #
                   4746:     if (! defined($colors)) {
                   4747:         $colors = ['#33ff00', 
                   4748:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
                   4749:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
                   4750:                   ]; 
                   4751:     }
1.228     matthew  4752:     my $extra_settings = {};
                   4753:     if (ref($Values[-1]) eq 'HASH') {
                   4754:         $extra_settings = pop(@Values);
                   4755:     }
1.127     matthew  4756:     #
1.136     matthew  4757:     my $identifier = &get_cgi_id();
                   4758:     my $id = 'cgi.'.$identifier;        
1.129     matthew  4759:     if (! @Values || ref($Values[0]) ne 'ARRAY') {
1.127     matthew  4760:         return '';
                   4761:     }
1.225     matthew  4762:     #
                   4763:     my @Labels;
                   4764:     if (defined($labels)) {
                   4765:         @Labels = @$labels;
                   4766:     } else {
                   4767:         for (my $i=0;$i<@{$Values[0]};$i++) {
                   4768:             push (@Labels,$i+1);
                   4769:         }
                   4770:     }
                   4771:     #
1.129     matthew  4772:     my $NumBars = scalar(@{$Values[0]});
1.225     matthew  4773:     if ($NumBars < scalar(@Labels)) { $NumBars = scalar(@Labels); }
1.129     matthew  4774:     my %ValuesHash;
                   4775:     my $NumSets=1;
                   4776:     foreach my $array (@Values) {
                   4777:         next if (! ref($array));
1.136     matthew  4778:         $ValuesHash{$id.'.data.'.$NumSets++} = 
1.132     matthew  4779:             join(',',@$array);
1.129     matthew  4780:     }
1.127     matthew  4781:     #
1.136     matthew  4782:     my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
1.225     matthew  4783:     if ($NumBars < 3) {
                   4784:         $width = 120+$NumBars*32;
1.220     matthew  4785:         $xskip = 1;
1.225     matthew  4786:         $bar_width = 30;
                   4787:     } elsif ($NumBars < 5) {
                   4788:         $width = 120+$NumBars*20;
                   4789:         $xskip = 1;
                   4790:         $bar_width = 20;
1.220     matthew  4791:     } elsif ($NumBars < 10) {
1.136     matthew  4792:         $width = 120+$NumBars*15;
                   4793:         $xskip = 1;
                   4794:         $bar_width = 15;
                   4795:     } elsif ($NumBars <= 25) {
                   4796:         $width = 120+$NumBars*11;
                   4797:         $xskip = 5;
                   4798:         $bar_width = 8;
                   4799:     } elsif ($NumBars <= 50) {
                   4800:         $width = 120+$NumBars*8;
                   4801:         $xskip = 5;
                   4802:         $bar_width = 4;
                   4803:     } else {
                   4804:         $width = 120+$NumBars*8;
                   4805:         $xskip = 5;
                   4806:         $bar_width = 4;
                   4807:     }
                   4808:     #
1.137     matthew  4809:     $Max = 1 if ($Max < 1);
                   4810:     if ( int($Max) < $Max ) {
                   4811:         $Max++;
                   4812:         $Max = int($Max);
                   4813:     }
1.127     matthew  4814:     $Title  = '' if (! defined($Title));
                   4815:     $xlabel = '' if (! defined($xlabel));
                   4816:     $ylabel = '' if (! defined($ylabel));
1.369     www      4817:     $ValuesHash{$id.'.title'}    = &escape($Title);
                   4818:     $ValuesHash{$id.'.xlabel'}   = &escape($xlabel);
                   4819:     $ValuesHash{$id.'.ylabel'}   = &escape($ylabel);
1.137     matthew  4820:     $ValuesHash{$id.'.y_max_value'} = $Max;
1.136     matthew  4821:     $ValuesHash{$id.'.NumBars'}  = $NumBars;
                   4822:     $ValuesHash{$id.'.NumSets'}  = $NumSets;
                   4823:     $ValuesHash{$id.'.PlotType'} = 'bar';
                   4824:     $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   4825:     $ValuesHash{$id.'.height'}   = $height;
                   4826:     $ValuesHash{$id.'.width'}    = $width;
                   4827:     $ValuesHash{$id.'.xskip'}    = $xskip;
                   4828:     $ValuesHash{$id.'.bar_width'} = $bar_width;
                   4829:     $ValuesHash{$id.'.labels'} = join(',',@Labels);
1.127     matthew  4830:     #
1.228     matthew  4831:     # Deal with other parameters
                   4832:     while (my ($key,$value) = each(%$extra_settings)) {
                   4833:         $ValuesHash{$id.'.'.$key} = $value;
                   4834:     }
                   4835:     #
1.137     matthew  4836:     &Apache::lonnet::appenv(%ValuesHash);
                   4837:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   4838: }
                   4839: 
                   4840: ############################################################
                   4841: ############################################################
                   4842: 
                   4843: =pod
                   4844: 
                   4845: =item DrawXYGraph
                   4846: 
1.138     matthew  4847: Facilitates the plotting of data in an XY graph.
                   4848: Puts plot definition data into the users environment in order for 
                   4849: graph.png to plot it.  Returns an <img> tag for the plot.
                   4850: 
                   4851: Inputs:
                   4852: 
                   4853: =over 4
                   4854: 
                   4855: =item $Title: string, the title of the plot
                   4856: 
                   4857: =item $xlabel: string, text describing the X-axis of the plot
                   4858: 
                   4859: =item $ylabel: string, text describing the Y-axis of the plot
                   4860: 
                   4861: =item $Max: scalar, the maximum Y value to use in the plot
                   4862: If $Max is < any data point, the graph will not be rendered.
                   4863: 
                   4864: =item $colors: Array ref containing the hex color codes for the data to be 
                   4865: plotted in.  If undefined, default values will be used.
                   4866: 
                   4867: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   4868: 
                   4869: =item $Ydata: Array ref containing Array refs.  
1.185     www      4870: Each of the contained arrays will be plotted as a separate curve.
1.138     matthew  4871: 
                   4872: =item %Values: hash indicating or overriding any default values which are 
                   4873: passed to graph.png.  
                   4874: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   4875: 
                   4876: =back
                   4877: 
                   4878: Returns:
                   4879: 
                   4880: An <img> tag which references graph.png and the appropriate identifying
                   4881: information for the plot.
                   4882: 
1.137     matthew  4883: =cut
                   4884: 
                   4885: ############################################################
                   4886: ############################################################
                   4887: sub DrawXYGraph {
                   4888:     my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
                   4889:     #
                   4890:     # Create the identifier for the graph
                   4891:     my $identifier = &get_cgi_id();
                   4892:     my $id = 'cgi.'.$identifier;
                   4893:     #
                   4894:     $Title  = '' if (! defined($Title));
                   4895:     $xlabel = '' if (! defined($xlabel));
                   4896:     $ylabel = '' if (! defined($ylabel));
                   4897:     my %ValuesHash = 
                   4898:         (
1.369     www      4899:          $id.'.title'  => &escape($Title),
                   4900:          $id.'.xlabel' => &escape($xlabel),
                   4901:          $id.'.ylabel' => &escape($ylabel),
1.137     matthew  4902:          $id.'.y_max_value'=> $Max,
                   4903:          $id.'.labels'     => join(',',@$Xlabels),
                   4904:          $id.'.PlotType'   => 'XY',
                   4905:          );
                   4906:     #
                   4907:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   4908:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   4909:     }
                   4910:     #
                   4911:     if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
                   4912:         return '';
                   4913:     }
                   4914:     my $NumSets=1;
1.138     matthew  4915:     foreach my $array (@{$Ydata}){
1.137     matthew  4916:         next if (! ref($array));
                   4917:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
                   4918:     }
1.138     matthew  4919:     $ValuesHash{$id.'.NumSets'} = $NumSets-1;
1.137     matthew  4920:     #
                   4921:     # Deal with other parameters
                   4922:     while (my ($key,$value) = each(%Values)) {
                   4923:         $ValuesHash{$id.'.'.$key} = $value;
1.127     matthew  4924:     }
                   4925:     #
1.136     matthew  4926:     &Apache::lonnet::appenv(%ValuesHash);
                   4927:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   4928: }
                   4929: 
                   4930: ############################################################
                   4931: ############################################################
                   4932: 
                   4933: =pod
                   4934: 
1.138     matthew  4935: =item DrawXYYGraph
                   4936: 
                   4937: Facilitates the plotting of data in an XY graph with two Y axes.
                   4938: Puts plot definition data into the users environment in order for 
                   4939: graph.png to plot it.  Returns an <img> tag for the plot.
                   4940: 
                   4941: Inputs:
                   4942: 
                   4943: =over 4
                   4944: 
                   4945: =item $Title: string, the title of the plot
                   4946: 
                   4947: =item $xlabel: string, text describing the X-axis of the plot
                   4948: 
                   4949: =item $ylabel: string, text describing the Y-axis of the plot
                   4950: 
                   4951: =item $colors: Array ref containing the hex color codes for the data to be 
                   4952: plotted in.  If undefined, default values will be used.
                   4953: 
                   4954: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   4955: 
                   4956: =item $Ydata1: The first data set
                   4957: 
                   4958: =item $Min1: The minimum value of the left Y-axis
                   4959: 
                   4960: =item $Max1: The maximum value of the left Y-axis
                   4961: 
                   4962: =item $Ydata2: The second data set
                   4963: 
                   4964: =item $Min2: The minimum value of the right Y-axis
                   4965: 
                   4966: =item $Max2: The maximum value of the left Y-axis
                   4967: 
                   4968: =item %Values: hash indicating or overriding any default values which are 
                   4969: passed to graph.png.  
                   4970: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   4971: 
                   4972: =back
                   4973: 
                   4974: Returns:
                   4975: 
                   4976: An <img> tag which references graph.png and the appropriate identifying
                   4977: information for the plot.
1.136     matthew  4978: 
                   4979: =cut
                   4980: 
                   4981: ############################################################
                   4982: ############################################################
1.137     matthew  4983: sub DrawXYYGraph {
                   4984:     my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
                   4985:                                         $Ydata2,$Min2,$Max2,%Values)=@_;
1.136     matthew  4986:     #
                   4987:     # Create the identifier for the graph
                   4988:     my $identifier = &get_cgi_id();
                   4989:     my $id = 'cgi.'.$identifier;
                   4990:     #
                   4991:     $Title  = '' if (! defined($Title));
                   4992:     $xlabel = '' if (! defined($xlabel));
                   4993:     $ylabel = '' if (! defined($ylabel));
                   4994:     my %ValuesHash = 
                   4995:         (
1.369     www      4996:          $id.'.title'  => &escape($Title),
                   4997:          $id.'.xlabel' => &escape($xlabel),
                   4998:          $id.'.ylabel' => &escape($ylabel),
1.136     matthew  4999:          $id.'.labels' => join(',',@$Xlabels),
                   5000:          $id.'.PlotType' => 'XY',
                   5001:          $id.'.NumSets' => 2,
1.137     matthew  5002:          $id.'.two_axes' => 1,
                   5003:          $id.'.y1_max_value' => $Max1,
                   5004:          $id.'.y1_min_value' => $Min1,
                   5005:          $id.'.y2_max_value' => $Max2,
                   5006:          $id.'.y2_min_value' => $Min2,
1.136     matthew  5007:          );
                   5008:     #
1.137     matthew  5009:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   5010:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   5011:     }
                   5012:     #
                   5013:     if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
                   5014:         ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
1.136     matthew  5015:         return '';
                   5016:     }
                   5017:     my $NumSets=1;
1.137     matthew  5018:     foreach my $array ($Ydata1,$Ydata2){
1.136     matthew  5019:         next if (! ref($array));
                   5020:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
1.137     matthew  5021:     }
                   5022:     #
                   5023:     # Deal with other parameters
                   5024:     while (my ($key,$value) = each(%Values)) {
                   5025:         $ValuesHash{$id.'.'.$key} = $value;
1.136     matthew  5026:     }
                   5027:     #
                   5028:     &Apache::lonnet::appenv(%ValuesHash);
1.130     albertel 5029:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
1.139     matthew  5030: }
                   5031: 
                   5032: ############################################################
                   5033: ############################################################
                   5034: 
                   5035: =pod
                   5036: 
1.157     matthew  5037: =back 
                   5038: 
1.139     matthew  5039: =head1 Statistics helper routines?  
                   5040: 
                   5041: Bad place for them but what the hell.
                   5042: 
1.157     matthew  5043: =over 4
                   5044: 
1.139     matthew  5045: =item &chartlink
                   5046: 
                   5047: Returns a link to the chart for a specific student.  
                   5048: 
                   5049: Inputs:
                   5050: 
                   5051: =over 4
                   5052: 
                   5053: =item $linktext: The text of the link
                   5054: 
                   5055: =item $sname: The students username
                   5056: 
                   5057: =item $sdomain: The students domain
                   5058: 
                   5059: =back
                   5060: 
1.157     matthew  5061: =back
                   5062: 
1.139     matthew  5063: =cut
                   5064: 
                   5065: ############################################################
                   5066: ############################################################
                   5067: sub chartlink {
                   5068:     my ($linktext, $sname, $sdomain) = @_;
                   5069:     my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
1.369     www      5070:         '&amp;SelectedStudent='.&escape($sname.':'.$sdomain).
1.219     albertel 5071:         '&amp;chartoutputmode='.HTML::Entities::encode('html, with all links').
1.139     matthew  5072:        '">'.$linktext.'</a>';
1.153     matthew  5073: }
                   5074: 
                   5075: #######################################################
                   5076: #######################################################
                   5077: 
                   5078: =pod
                   5079: 
                   5080: =head1 Course Environment Routines
1.157     matthew  5081: 
                   5082: =over 4
1.153     matthew  5083: 
                   5084: =item &restore_course_settings 
                   5085: 
                   5086: =item &store_course_settings
                   5087: 
                   5088: Restores/Store indicated form parameters from the course environment.
                   5089: Will not overwrite existing values of the form parameters.
                   5090: 
                   5091: Inputs: 
                   5092: a scalar describing the data (e.g. 'chart', 'problem_analysis')
                   5093: 
                   5094: a hash ref describing the data to be stored.  For example:
                   5095:    
                   5096: %Save_Parameters = ('Status' => 'scalar',
                   5097:     'chartoutputmode' => 'scalar',
                   5098:     'chartoutputdata' => 'scalar',
                   5099:     'Section' => 'array',
1.373     raeburn  5100:     'Group' => 'array',
1.153     matthew  5101:     'StudentData' => 'array',
                   5102:     'Maps' => 'array');
                   5103: 
                   5104: Returns: both routines return nothing
                   5105: 
                   5106: =cut
                   5107: 
                   5108: #######################################################
                   5109: #######################################################
                   5110: sub store_course_settings {
                   5111:     # save to the environment
                   5112:     # appenv the same items, just to be safe
1.258     albertel 5113:     my $courseid = $env{'request.course.id'};
1.300     albertel 5114:     my $udom  = $env{'user.domain'};
                   5115:     my $uname = $env{'user.name'};
1.153     matthew  5116:     my ($prefix,$Settings) = @_;
                   5117:     my %SaveHash;
                   5118:     my %AppHash;
                   5119:     while (my ($setting,$type) = each(%$Settings)) {
1.300     albertel 5120:         my $basename = join('.','internal',$courseid,$prefix,$setting);
                   5121:         my $envname = 'environment.'.$basename;
1.258     albertel 5122:         if (exists($env{'form.'.$setting})) {
1.153     matthew  5123:             # Save this value away
                   5124:             if ($type eq 'scalar' &&
1.258     albertel 5125:                 (! exists($env{$envname}) || 
                   5126:                  $env{$envname} ne $env{'form.'.$setting})) {
                   5127:                 $SaveHash{$basename} = $env{'form.'.$setting};
                   5128:                 $AppHash{$envname}   = $env{'form.'.$setting};
1.153     matthew  5129:             } elsif ($type eq 'array') {
                   5130:                 my $stored_form;
1.258     albertel 5131:                 if (ref($env{'form.'.$setting})) {
1.153     matthew  5132:                     $stored_form = join(',',
                   5133:                                         map {
1.369     www      5134:                                             &escape($_);
1.258     albertel 5135:                                         } sort(@{$env{'form.'.$setting}}));
1.153     matthew  5136:                 } else {
                   5137:                     $stored_form = 
1.369     www      5138:                         &escape($env{'form.'.$setting});
1.153     matthew  5139:                 }
                   5140:                 # Determine if the array contents are the same.
1.258     albertel 5141:                 if ($stored_form ne $env{$envname}) {
1.153     matthew  5142:                     $SaveHash{$basename} = $stored_form;
                   5143:                     $AppHash{$envname}   = $stored_form;
                   5144:                 }
                   5145:             }
                   5146:         }
                   5147:     }
                   5148:     my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
1.300     albertel 5149:                                           $udom,$uname);
1.153     matthew  5150:     if ($put_result !~ /^(ok|delayed)/) {
                   5151:         &Apache::lonnet::logthis('unable to save form parameters, '.
                   5152:                                  'got error:'.$put_result);
                   5153:     }
                   5154:     # Make sure these settings stick around in this session, too
                   5155:     &Apache::lonnet::appenv(%AppHash);
                   5156:     return;
                   5157: }
                   5158: 
                   5159: sub restore_course_settings {
1.258     albertel 5160:     my $courseid = $env{'request.course.id'};
1.153     matthew  5161:     my ($prefix,$Settings) = @_;
                   5162:     while (my ($setting,$type) = each(%$Settings)) {
1.258     albertel 5163:         next if (exists($env{'form.'.$setting}));
1.300     albertel 5164:         my $envname = 'environment.internal.'.$courseid.'.'.$prefix.
1.153     matthew  5165:             '.'.$setting;
1.258     albertel 5166:         if (exists($env{$envname})) {
1.153     matthew  5167:             if ($type eq 'scalar') {
1.258     albertel 5168:                 $env{'form.'.$setting} = $env{$envname};
1.153     matthew  5169:             } elsif ($type eq 'array') {
1.258     albertel 5170:                 $env{'form.'.$setting} = [ 
1.153     matthew  5171:                                            map { 
1.369     www      5172:                                                &unescape($_); 
1.258     albertel 5173:                                            } split(',',$env{$envname})
1.153     matthew  5174:                                            ];
                   5175:             }
                   5176:         }
                   5177:     }
1.127     matthew  5178: }
                   5179: 
                   5180: ############################################################
                   5181: ############################################################
1.154     albertel 5182: 
1.378     raeburn  5183: sub course_type {
                   5184:     my ($cid) = @_;
                   5185:     if (!defined($cid)) {
                   5186:         $cid = $env{'request.course.id'};
                   5187:     }
1.404     albertel 5188:     if (defined($env{'course.'.$cid.'.type'})) {
                   5189:         return $env{'course.'.$cid.'.type'};
1.378     raeburn  5190:     } else {
                   5191:         return 'Course';
1.377     raeburn  5192:     }
                   5193: }
1.156     albertel 5194: 
1.406     raeburn  5195: sub group_term {
                   5196:     my $crstype = &course_type();
                   5197:     my %names = (
                   5198:                   'Course' => 'group',
                   5199:                   'Group' => 'team',
                   5200:                 );
                   5201:     return $names{$crstype};
                   5202: }
                   5203: 
1.156     albertel 5204: sub icon {
                   5205:     my ($file)=@_;
1.168     albertel 5206:     my $curfext = (split(/\./,$file))[-1];
                   5207:     my $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/unknown.gif';
1.156     albertel 5208:     my $embstyle = &Apache::loncommon::fileembstyle($curfext);
1.168     albertel 5209:     if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) {
                   5210: 	if (-e  $Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
                   5211: 	          $Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   5212: 	            $curfext.".gif") {
                   5213: 	    $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   5214: 		$curfext.".gif";
                   5215: 	}
                   5216:     }
1.249     albertel 5217:     return &lonhttpdurl($iconname);
1.154     albertel 5218: } 
1.84      albertel 5219: 
1.215     albertel 5220: sub lonhttpdurl {
                   5221:     my ($url)=@_;
                   5222:     my $lonhttpd_port=$Apache::lonnet::perlvar{'lonhttpdPort'};
                   5223:     if (!defined($lonhttpd_port)) { $lonhttpd_port='8080'; }
                   5224:     return 'http://'.$ENV{'SERVER_NAME'}.':'.$lonhttpd_port.$url;
                   5225: }
                   5226: 
1.213     albertel 5227: sub connection_aborted {
                   5228:     my ($r)=@_;
                   5229:     $r->print(" ");$r->rflush();
                   5230:     my $c = $r->connection;
                   5231:     return $c->aborted();
                   5232: }
                   5233: 
1.221     foxr     5234: #    Escapes strings that may have embedded 's that will be put into
1.222     foxr     5235: #    strings as 'strings'.
                   5236: sub escape_single {
1.221     foxr     5237:     my ($input) = @_;
1.223     albertel 5238:     $input =~ s/\\/\\\\/g;	# Escape the \'s..(must be first)>
1.221     foxr     5239:     $input =~ s/\'/\\\'/g;	# Esacpe the 's....
                   5240:     return $input;
                   5241: }
1.223     albertel 5242: 
1.222     foxr     5243: #  Same as escape_single, but escape's "'s  This 
                   5244: #  can be used for  "strings"
                   5245: sub escape_double {
                   5246:     my ($input) = @_;
                   5247:     $input =~ s/\\/\\\\/g;	# Escape the /'s..(must be first)>
                   5248:     $input =~ s/\"/\\\"/g;	# Esacpe the "s....
                   5249:     return $input;
                   5250: }
1.223     albertel 5251:  
1.222     foxr     5252: #   Escapes the last element of a full URL.
                   5253: sub escape_url {
                   5254:     my ($url)   = @_;
1.238     raeburn  5255:     my @urlslices = split(/\//, $url,-1);
1.369     www      5256:     my $lastitem = &escape(pop(@urlslices));
1.223     albertel 5257:     return join('/',@urlslices).'/'.$lastitem;
1.222     foxr     5258: }
1.41      ng       5259: =pod
                   5260: 
                   5261: =back
                   5262: 
1.112     bowersj2 5263: =cut
1.41      ng       5264: 
1.112     bowersj2 5265: 1;
                   5266: __END__;
1.41      ng       5267: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>
500 Internal Server Error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.