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

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

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