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

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

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