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

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

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