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

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

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