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

1.10      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # a pile of common routines
1.10      albertel    3: #
1.561   ! www         4: # $Id: loncommon.pm,v 1.560 2007/08/11 14:22:14 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
                   3400: 
1.112     bowersj2 3401: =back
                   3402: 
1.60      matthew  3403: Returns: A uniform header for LON-CAPA web pages.  
                   3404: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
                   3405: If $bodyonly is undef or zero, an html string containing a <body> tag and 
                   3406: other decorations will be returned.
                   3407: 
                   3408: =cut
                   3409: 
1.54      www      3410: sub bodytag {
1.309     albertel 3411:     my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,$customtitle,
1.460     albertel 3412: 	$notopbar,$bgcolor,$notitle,$no_inline_link,$args)=@_;
1.339     albertel 3413: 
1.460     albertel 3414:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
1.339     albertel 3415: 
1.183     matthew  3416:     $function = &get_users_function() if (!$function);
1.339     albertel 3417:     my $img =    &designparm($function.'.img',$domain);
                   3418:     my $font =   &designparm($function.'.font',$domain);
                   3419:     my $pgbg   = $bgcolor || &designparm($function.'.pgbg',$domain);
                   3420: 
                   3421:     my %design = ( 'style'   => 'margin-top: 0px',
1.535     albertel 3422: 		   'bgcolor' => $pgbg,
1.339     albertel 3423: 		   'text'    => $font,
                   3424:                    'alink'   => &designparm($function.'.alink',$domain),
                   3425: 		   'vlink'   => &designparm($function.'.vlink',$domain),
                   3426: 		   'link'    => &designparm($function.'.link',$domain),);
1.438     albertel 3427:     @design{keys(%$addentries)} = @$addentries{keys(%$addentries)}; 
1.339     albertel 3428: 
1.63      www      3429:  # role and realm
1.378     raeburn  3430:     my ($role,$realm) = split(/\./,$env{'request.role'},2);
                   3431:     if ($role  eq 'ca') {
1.479     albertel 3432:         my ($rdom,$rname) = ($realm =~ m{^/($match_domain)/($match_username)$});
1.500     albertel 3433:         $realm = &plainname($rname,$rdom);
1.378     raeburn  3434:     } 
1.55      www      3435: # realm
1.258     albertel 3436:     if ($env{'request.course.id'}) {
1.378     raeburn  3437:         if ($env{'request.role'} !~ /^cr/) {
                   3438:             $role = &Apache::lonnet::plaintext($role,&course_type());
                   3439:         }
1.359     albertel 3440: 	$realm = $env{'course.'.$env{'request.course.id'}.'.description'};
1.378     raeburn  3441:     } else {
                   3442:         $role = &Apache::lonnet::plaintext($role);
1.54      www      3443:     }
1.433     albertel 3444: 
1.359     albertel 3445:     if (!$realm) { $realm='&nbsp;'; }
1.55      www      3446: # Set messages
1.60      matthew  3447:     my $messages=&domainlogo($domain);
1.101     www      3448: # Port for miniserver
1.83      albertel 3449:     my $lonhttpdPort=$Apache::lonnet::perlvar{'lonhttpdPort'};
                   3450:     if (!defined($lonhttpdPort)) { $lonhttpdPort='8080'; }
1.330     albertel 3451: 
1.438     albertel 3452:     my $extra_body_attr = &make_attr_string($forcereg,\%design);
1.329     albertel 3453: 
1.101     www      3454: # construct main body tag
1.359     albertel 3455:     my $bodytag = "<body $extra_body_attr>".
                   3456: 	&Apache::lontexconvert::init_math_support();
1.252     albertel 3457: 
1.530     albertel 3458:     if ($bodyonly) {
1.60      matthew  3459:         return $bodytag;
1.258     albertel 3460:     } elsif ($env{'browser.interface'} eq 'textual') {
1.95      www      3461: # Accessibility
1.224     raeburn  3462:           
1.337     albertel 3463: 	$bodytag.=&Apache::lonmenu::menubuttons($forcereg,$forcereg);
1.338     albertel 3464: 	if (!$notitle) {
1.337     albertel 3465: 	    $bodytag.='<h1>LON-CAPA: '.$title.'</h1>';
                   3466: 	}
                   3467: 	return $bodytag;
1.359     albertel 3468:     }
                   3469: 
1.410     albertel 3470:     my $name = &plainname($env{'user.name'},$env{'user.domain'});
1.433     albertel 3471:     if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   3472: 	undef($role);
1.434     albertel 3473:     } else {
                   3474: 	$name = &aboutmewrapper($name,$env{'user.name'},$env{'user.domain'});
1.433     albertel 3475:     }
1.359     albertel 3476:     
                   3477:     my $roleinfo=(<<ENDROLE);
                   3478: <td class="LC_title_bar_who">
                   3479: <div class="LC_title_bar_name">
1.410     albertel 3480:     $name
1.361     albertel 3481:     &nbsp;
1.359     albertel 3482: </div>
                   3483: <div class="LC_title_bar_role">
1.361     albertel 3484: $role&nbsp;
1.359     albertel 3485: </div>
                   3486: <div class="LC_title_bar_realm">
1.361     albertel 3487: $realm&nbsp;
1.359     albertel 3488: </div>
1.206     albertel 3489: </td>
                   3490: ENDROLE
1.235     raeburn  3491: 
1.359     albertel 3492:     my $titleinfo = '<span class="LC_title_bar_title">'.$title.'</span>';
                   3493:     if ($customtitle) {
                   3494:         $titleinfo = $customtitle;
                   3495:     }
                   3496:     #
                   3497:     # Extra info if you are the DC
                   3498:     my $dc_info = '';
                   3499:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
                   3500:                         $env{'course.'.$env{'request.course.id'}.
                   3501:                                  '.domain'}.'/'})) {
                   3502:         my $cid = $env{'request.course.id'};
                   3503:         $dc_info.= $cid.' '.$env{'course.'.$cid.'.internal.coursecode'};
1.380     www      3504:         $dc_info =~ s/\s+$//;
1.359     albertel 3505:         $dc_info = '('.$dc_info.')';
                   3506:     }
                   3507: 
                   3508:     if ($env{'environment.remote'} eq 'off') {
                   3509:         # No Remote
1.258     albertel 3510: 	if ($env{'request.state'} eq 'construct') {
1.359     albertel 3511: 	    $forcereg=1;
                   3512: 	}
                   3513: 
                   3514: 	if (!$customtitle && $env{'request.state'} eq 'construct') {
                   3515: 	    # this is for resources; directories have customtitle, and crumbs
                   3516:             # and select recent are created in lonpubdir.pm  
1.229     albertel 3517: 	    my ($uname,$thisdisfn)=
1.258     albertel 3518: 		($env{'request.filename'} =~ m|^/home/([^/]+)/public_html/(.*)|);
1.229     albertel 3519: 	    my $formaction='/priv/'.$uname.'/'.$thisdisfn;
                   3520: 	    $formaction=~s/\/+/\//g;
                   3521: 
1.359     albertel 3522: 	    my $parentpath = '';
                   3523: 	    my $lastitem = '';
                   3524: 	    if ($thisdisfn =~ m-(.+/)([^/]*)$-) {
                   3525: 		$parentpath = $1;
                   3526: 		$lastitem = $2;
                   3527: 	    } else {
                   3528: 		$lastitem = $thisdisfn;
                   3529: 	    }
                   3530: 	    $titleinfo = 
1.401     albertel 3531: 		&Apache::loncommon::help_open_menu('','',3,'Authoring').
1.359     albertel 3532: 		'<b>Construction Space</b>:&nbsp;'. 
                   3533: 		'<form name="dirs" method="post" action="'.$formaction
                   3534: 		.'" target="_top"><tt><b>'
                   3535: 		.&Apache::lonhtmlcommon::crumbs($uname.'/'.$parentpath,'_top','/priv','','+1',1)."<font size=\"+1\">$lastitem</font></b></tt><br />"
                   3536: 		.&Apache::lonhtmlcommon::select_recent('construct','recent','this.form.action=this.form.recent.value;this.form.submit()')
                   3537: 		.'</form>'
                   3538: 		.&Apache::lonmenu::constspaceform();
1.235     raeburn  3539:         }
1.359     albertel 3540: 
1.337     albertel 3541:         my $titletable;
1.338     albertel 3542: 	if (!$notitle) {
1.337     albertel 3543: 	    $titletable =
1.359     albertel 3544: 		'<table id="LC_title_bar">'.
                   3545:                          "<tr><td> $titleinfo $dc_info</td>".$roleinfo.
                   3546: 			 '</tr></table>';
1.337     albertel 3547: 	}
1.359     albertel 3548: 	if ($notopbar) {
                   3549: 	    $bodytag .= $titletable;
                   3550: 	} else {
                   3551: 	    if ($env{'request.state'} eq 'construct') {
1.337     albertel 3552:                 $bodytag .= &Apache::lonmenu::menubuttons($forcereg,$forcereg,
                   3553: 							  $titletable);
1.272     raeburn  3554:             } else {
1.336     albertel 3555:                 $bodytag .= &Apache::lonmenu::menubuttons($forcereg,$forcereg).
1.359     albertel 3556: 		    $titletable;
1.272     raeburn  3557:             }
1.235     raeburn  3558:         }
                   3559:         return $bodytag;
1.94      www      3560:     }
1.95      www      3561: 
1.93      www      3562: #
1.95      www      3563: # Top frame rendering, Remote is up
1.93      www      3564: #
1.359     albertel 3565: 
1.517     raeburn  3566:     my $imgsrc = $img;
                   3567:     if ($img =~ /^\/adm/) {
                   3568:         $imgsrc = 'http://'.$ENV{'HTTP_HOST'}.':'.$lonhttpdPort.$img;
                   3569:     }
                   3570:     my $upperleft='<img src="'.$imgsrc.'" alt="'.$function.'" />';
1.359     albertel 3571: 
1.305     www      3572:     # Explicit link to get inline menu
1.361     albertel 3573:     my $menu= ($no_inline_link?''
                   3574: 	       :'<br /><a href="/adm/remote?action=collapse">'.&mt('Switch to Inline Menu Mode').'</a>');
1.245     matthew  3575:     #
1.338     albertel 3576:     if ($notitle) {
1.337     albertel 3577: 	return $bodytag;
                   3578:     }
1.94      www      3579:     return(<<ENDBODY);
1.60      matthew  3580: $bodytag
1.359     albertel 3581: <table id="LC_title_bar" class="LC_with_remote">
1.368     albertel 3582: <tr><td class="LC_title_bar_role_logo">$upperleft</td>
1.359     albertel 3583:     <td class="LC_title_bar_domain_logo">$messages&nbsp;</td>
1.54      www      3584: </tr>
1.359     albertel 3585: <tr><td>$titleinfo $dc_info $menu</td>
                   3586: $roleinfo
1.368     albertel 3587: </tr>
1.356     albertel 3588: </table>
1.54      www      3589: ENDBODY
1.182     matthew  3590: }
                   3591: 
1.330     albertel 3592: sub make_attr_string {
                   3593:     my ($register,$attr_ref) = @_;
                   3594: 
                   3595:     if ($attr_ref && !ref($attr_ref)) {
                   3596: 	die("addentries Must be a hash ref ".
                   3597: 	    join(':',caller(1))." ".
                   3598: 	    join(':',caller(0))." ");
                   3599:     }
                   3600: 
                   3601:     if ($register) {
1.339     albertel 3602: 	my ($on_load,$on_unload);
                   3603: 	foreach my $key (keys(%{$attr_ref})) {
                   3604: 	    if      (lc($key) eq 'onload') {
                   3605: 		$on_load.=$attr_ref->{$key}.';';
                   3606: 		delete($attr_ref->{$key});
                   3607: 
                   3608: 	    } elsif (lc($key) eq 'onunload') {
                   3609: 		$on_unload.=$attr_ref->{$key}.';';
                   3610: 		delete($attr_ref->{$key});
                   3611: 	    }
                   3612: 	}
                   3613: 	$attr_ref->{'onload'}  =
                   3614: 	    &Apache::lonmenu::loadevents().  $on_load;
                   3615: 	$attr_ref->{'onunload'}=
                   3616: 	    &Apache::lonmenu::unloadevents().$on_unload;
                   3617:     }
                   3618: 
                   3619: # Accessibility font enhance
                   3620:     if ($env{'browser.fontenhance'} eq 'on') {
                   3621: 	my $style;
                   3622: 	foreach my $key (keys(%{$attr_ref})) {
                   3623: 	    if (lc($key) eq 'style') {
                   3624: 		$style.=$attr_ref->{$key}.';';
                   3625: 		delete($attr_ref->{$key});
                   3626: 	    }
                   3627: 	}
                   3628: 	$attr_ref->{'style'}=$style.'; font-size: x-large;';
1.330     albertel 3629:     }
1.339     albertel 3630: 
                   3631:     if ($env{'browser.blackwhite'} eq 'on') {
                   3632: 	delete($attr_ref->{'font'});
                   3633: 	delete($attr_ref->{'link'});
                   3634: 	delete($attr_ref->{'alink'});
                   3635: 	delete($attr_ref->{'vlink'});
                   3636: 	delete($attr_ref->{'bgcolor'});
                   3637: 	delete($attr_ref->{'background'});
                   3638:     }
                   3639: 
1.330     albertel 3640:     my $attr_string;
                   3641:     foreach my $attr (keys(%$attr_ref)) {
                   3642: 	$attr_string .= " $attr=\"".$attr_ref->{$attr}.'" ';
                   3643:     }
                   3644:     return $attr_string;
                   3645: }
                   3646: 
                   3647: 
1.182     matthew  3648: ###############################################
1.251     albertel 3649: ###############################################
                   3650: 
                   3651: =pod
                   3652: 
                   3653: =item * &endbodytag()
                   3654: 
                   3655: Returns a uniform footer for LON-CAPA web pages.
                   3656: 
1.306     albertel 3657: Inputs: none
1.251     albertel 3658: 
                   3659: =cut
                   3660: 
                   3661: sub endbodytag {
                   3662:     my $endbodytag='</body>';
1.269     albertel 3663:     $endbodytag=&Apache::lontexconvert::jsMath_process()."\n".$endbodytag;
1.315     albertel 3664:     if ( exists( $env{'internal.head.redirect'} ) ) {
                   3665: 	$endbodytag=
                   3666: 	    "<br /><a href=\"$env{'internal.head.redirect'}\">".
                   3667: 	    &mt('Continue').'</a>'.
                   3668: 	    $endbodytag;
                   3669:     }
1.251     albertel 3670:     return $endbodytag;
                   3671: }
                   3672: 
1.352     albertel 3673: =pod
                   3674: 
                   3675: =item * &standard_css()
                   3676: 
                   3677: Returns a style sheet
                   3678: 
                   3679: Inputs: (all optional)
                   3680:             domain         -> force to color decorate a page for a specific
                   3681:                                domain
                   3682:             function       -> force usage of a specific rolish color scheme
                   3683:             bgcolor        -> override the default page bgcolor
                   3684: 
                   3685: =cut
                   3686: 
1.343     albertel 3687: sub standard_css {
1.345     albertel 3688:     my ($function,$domain,$bgcolor) = @_;
1.352     albertel 3689:     $function  = &get_users_function() if (!$function);
                   3690:     my $img    = &designparm($function.'.img',   $domain);
                   3691:     my $tabbg  = &designparm($function.'.tabbg', $domain);
                   3692:     my $font   = &designparm($function.'.font',  $domain);
1.345     albertel 3693:     my $sidebg = &designparm($function.'.sidebg',$domain);
1.382     albertel 3694:     my $pgbg_or_bgcolor =
                   3695: 	         $bgcolor ||
1.352     albertel 3696: 	         &designparm($function.'.pgbg',  $domain);
1.382     albertel 3697:     my $pgbg   = &designparm($function.'.pgbg',  $domain);
1.352     albertel 3698:     my $alink  = &designparm($function.'.alink', $domain);
                   3699:     my $vlink  = &designparm($function.'.vlink', $domain);
                   3700:     my $link   = &designparm($function.'.link',  $domain);
                   3701: 
                   3702:     my $sans                 = 'Arial,Helvetica,sans-serif';
1.395     albertel 3703:     my $mono                 = 'monospace';
1.352     albertel 3704:     my $data_table_head      = $tabbg;
                   3705:     my $data_table_light     = '#EEEEEE';
1.470     banghart 3706:     my $data_table_dark      = '#DDDDDD';
                   3707:     my $data_table_darker    = '#CCCCCC';
1.349     albertel 3708:     my $data_table_highlight = '#FFFF00';
1.352     albertel 3709:     my $mail_new             = '#FFBB77';
                   3710:     my $mail_new_hover       = '#DD9955';
                   3711:     my $mail_read            = '#BBBB77';
                   3712:     my $mail_read_hover      = '#999944';
                   3713:     my $mail_replied         = '#AAAA88';
                   3714:     my $mail_replied_hover   = '#888855';
                   3715:     my $mail_other           = '#99BBBB';
                   3716:     my $mail_other_hover     = '#669999';
1.391     albertel 3717:     my $table_header         = '#DDDDDD';
1.489     raeburn  3718:     my $feedback_link_bg     = '#BBBBBB';
1.392     albertel 3719: 
                   3720:     my $border = ($env{'browser.type'} eq 'explorer') ? '0px 2px 0px 2px'
                   3721: 	                                              : '0px 3px 0px 4px';
1.448     albertel 3722: 
1.523     albertel 3723: 
1.343     albertel 3724:     return <<END;
1.345     albertel 3725: h1, h2, h3, th { font-family: $sans }
1.343     albertel 3726: a:focus { color: red; background: yellow } 
1.510     albertel 3727: table.thinborder,
1.523     albertel 3728: 
1.510     albertel 3729: table.thinborder tr th {
                   3730:   border-style: solid;
                   3731:   border-width: 1px;
                   3732:   background: $tabbg;
                   3733: }
1.523     albertel 3734: table.thinborder tr td {
1.510     albertel 3735:   border-style: solid;
                   3736:   border-width: 1px
                   3737: }
1.426     albertel 3738: 
1.343     albertel 3739: form, .inline { display: inline; }
                   3740: .center { text-align: center; }
1.395     albertel 3741: .LC_filename {font-family: $mono;}
1.350     albertel 3742: .LC_error {
                   3743:   color: red;
                   3744:   font-size: larger;
                   3745: }
1.457     albertel 3746: .LC_warning,
                   3747: .LC_diff_removed {
1.394     albertel 3748:   color: red;
                   3749: }
1.532     albertel 3750: 
                   3751: .LC_info,
1.457     albertel 3752: .LC_success,
                   3753: .LC_diff_added {
1.350     albertel 3754:   color: green;
                   3755: }
1.543     albertel 3756: .LC_unknown {
                   3757:   color: yellow;
                   3758: }
                   3759: 
1.440     albertel 3760: .LC_icon {
                   3761:   border: 0px;
                   3762: }
1.539     albertel 3763: .LC_indexer_icon {
                   3764:   border: 0px;
                   3765:   height: 22px;
                   3766: }
1.543     albertel 3767: .LC_docs_spacer {
                   3768:   width: 25px;
                   3769:   height: 1px;
                   3770:   border: 0px;
                   3771: }
1.346     albertel 3772: 
1.532     albertel 3773: .LC_internal_info {
                   3774:   color: #999;
                   3775: }
                   3776: 
1.458     albertel 3777: table.LC_pastsubmission {
                   3778:   border: 1px solid black;
                   3779:   margin: 2px;
                   3780: }
                   3781: 
1.392     albertel 3782: table#LC_top_nav, table#LC_menubuttons {
1.345     albertel 3783:   width: 100%;
                   3784:   background: $pgbg;
1.392     albertel 3785:   border: 2px;
1.402     albertel 3786:   border-collapse: separate;
1.403     albertel 3787:   padding: 0px;
1.345     albertel 3788: }
1.392     albertel 3789: 
1.409     albertel 3790: table#LC_title_bar, table.LC_breadcrumbs, table#LC_nav_location,
1.393     albertel 3791: table#LC_title_bar.LC_with_remote {
1.359     albertel 3792:   width: 100%;
1.392     albertel 3793:   border-color: $pgbg;
                   3794:   border-style: solid;
                   3795:   border-width: $border;
                   3796: 
1.379     albertel 3797:   background: $pgbg;
                   3798:   font-family: $sans;
1.392     albertel 3799:   border-collapse: collapse;
1.403     albertel 3800:   padding: 0px;
1.359     albertel 3801: }
1.392     albertel 3802: 
1.409     albertel 3803: table.LC_docs_path {
                   3804:   width: 100%;
                   3805:   border: 0;
                   3806:   background: $pgbg;
                   3807:   font-family: $sans;
                   3808:   border-collapse: collapse;
                   3809:   padding: 0px;
                   3810: }
                   3811: 
1.359     albertel 3812: table#LC_title_bar td {
                   3813:   background: $tabbg;
                   3814: }
                   3815: table#LC_title_bar td.LC_title_bar_who {
                   3816:   background: $tabbg;
                   3817:   color: $font;
1.427     albertel 3818:   font: small $sans;
1.359     albertel 3819:   text-align: right;
                   3820: }
1.469     banghart 3821: span.LC_metadata {
                   3822:     font-family: $sans;
                   3823: }
1.359     albertel 3824: span.LC_title_bar_title {
1.416     albertel 3825:   font: bold x-large $sans;
1.359     albertel 3826: }
                   3827: table#LC_title_bar td.LC_title_bar_domain_logo {
                   3828:   background: $sidebg;
                   3829:   text-align: right;
1.368     albertel 3830:   padding: 0px;
                   3831: }
                   3832: table#LC_title_bar td.LC_title_bar_role_logo {
                   3833:   background: $sidebg;
                   3834:   padding: 0px;
1.359     albertel 3835: }
                   3836: 
1.346     albertel 3837: table#LC_menubuttons_mainmenu {
1.526     www      3838:   width: 100%;
1.346     albertel 3839:   border: 0px;
                   3840:   border-spacing: 1px;
1.372     albertel 3841:   padding: 0px 1px;
1.346     albertel 3842:   margin: 0px;
                   3843:   border-collapse: separate;
                   3844: }
                   3845: table#LC_menubuttons img, table#LC_menubuttons_mainmenu img {
                   3846:   border: 0px;
                   3847: }
1.345     albertel 3848: table#LC_top_nav td {
                   3849:   background: $tabbg;
1.392     albertel 3850:   border: 0px;
1.407     albertel 3851:   font-size: small;
1.345     albertel 3852: }
                   3853: table#LC_top_nav td a, div#LC_top_nav a {
                   3854:   color: $font;
                   3855:   font-family: $sans;
                   3856: }
1.364     albertel 3857: table#LC_top_nav td.LC_top_nav_logo {
                   3858:   background: $tabbg;
1.432     albertel 3859:   text-align: left;
1.408     albertel 3860:   white-space: nowrap;
1.432     albertel 3861:   width: 31px;
1.408     albertel 3862: }
                   3863: table#LC_top_nav td.LC_top_nav_logo img {
1.432     albertel 3864:   border: 0px;
1.408     albertel 3865:   vertical-align: bottom;
1.364     albertel 3866: }
1.432     albertel 3867: table#LC_top_nav td.LC_top_nav_exit,
                   3868: table#LC_top_nav td.LC_top_nav_help {
                   3869:   width: 2.0em;
                   3870: }
1.442     albertel 3871: table#LC_top_nav td.LC_top_nav_login {
                   3872:   width: 4.0em;
                   3873:   text-align: center;
                   3874: }
1.409     albertel 3875: table.LC_breadcrumbs td, table.LC_docs_path td  {
1.357     albertel 3876:   background: $tabbg;
                   3877:   color: $font;
                   3878:   font-family: $sans;
1.358     albertel 3879:   font-size: smaller;
1.357     albertel 3880: }
1.411     albertel 3881: table.LC_breadcrumbs td.LC_breadcrumbs_component,
1.409     albertel 3882: table.LC_docs_path td.LC_docs_path_component {
1.357     albertel 3883:   background: $tabbg;
                   3884:   color: $font;
                   3885:   font-family: $sans;
                   3886:   font-size: larger;
                   3887:   text-align: right;
                   3888: }
1.383     albertel 3889: td.LC_table_cell_checkbox {
                   3890:   text-align: center;
                   3891: }
                   3892: 
1.522     albertel 3893: table#LC_mainmenu td.LC_mainmenu_column {
                   3894:     vertical-align: top;
                   3895: }
                   3896: 
1.346     albertel 3897: .LC_menubuttons_inline_text {
                   3898:   color: $font;
                   3899:   font-family: $sans;
                   3900:   font-size: smaller;
                   3901: }
                   3902: 
1.526     www      3903: .LC_menubuttons_link {
                   3904:   text-decoration: none;
                   3905: }
                   3906: 
1.522     albertel 3907: .LC_menubuttons_category {
1.521     www      3908:   color: $font;
1.526     www      3909:   background: $pgbg;
1.521     www      3910:   font-family: $sans;
                   3911:   font-size: larger;
                   3912:   font-weight: bold;
                   3913: }
                   3914: 
1.346     albertel 3915: td.LC_menubuttons_text {
1.526     www      3916:   width: 90%;
1.346     albertel 3917:   color: $font;
                   3918:   font-family: $sans;
                   3919: }
1.526     www      3920: 
1.346     albertel 3921: td.LC_menubuttons_img {
                   3922: }
1.526     www      3923: 
1.346     albertel 3924: .LC_current_location {
                   3925:   font-family: $sans;
                   3926:   background: $tabbg;
                   3927: }
                   3928: .LC_new_mail {
                   3929:   font-family: $sans;
                   3930:   font-weight: bold;
                   3931: }
1.347     albertel 3932: 
1.526     www      3933: .LC_rolesmenu_is {
                   3934:   font-family: $sans;
                   3935: }
                   3936: 
                   3937: .LC_rolesmenu_selected {
                   3938:   font-family: $sans;
                   3939: }
                   3940: 
                   3941: .LC_rolesmenu_future {
                   3942:   font-family: $sans;
                   3943: }
                   3944: 
                   3945: 
                   3946: .LC_rolesmenu_will {
                   3947:   font-family: $sans;
                   3948: }
                   3949: 
                   3950: .LC_rolesmenu_will_not {
                   3951:   font-family: $sans;
                   3952: }
                   3953: 
                   3954: .LC_rolesmenu_expired {
                   3955:   font-family: $sans;
                   3956: }
                   3957: 
                   3958: .LC_rolesinfo {
                   3959:   font-family: $sans;
                   3960: }
                   3961: 
1.527     www      3962: .LC_dropadd_labeltext {
                   3963:   font-family: $sans;
                   3964:   text-align: right;
                   3965: }
                   3966: 
                   3967: .LC_preferences_labeltext {
                   3968:   font-family: $sans;
                   3969:   text-align: right;
                   3970: }
                   3971: 
1.440     albertel 3972: table.LC_aboutme_port {
                   3973:   border: 0px;
                   3974:   border-collapse: collapse;
                   3975:   border-spacing: 0px;
                   3976: }
1.349     albertel 3977: table.LC_data_table, table.LC_mail_list {
1.347     albertel 3978:   border: 1px solid #000000;
1.402     albertel 3979:   border-collapse: separate;
1.426     albertel 3980:   border-spacing: 1px;
1.347     albertel 3981: }
1.422     albertel 3982: .LC_data_table_dense {
                   3983:   font-size: small;
                   3984: }
1.507     raeburn  3985: table.LC_nested_outer {
                   3986:   border: 1px solid #000000;
                   3987:   border-collapse: separate;
                   3988:   border-spacing: 0px;
                   3989:   width: 100%;
                   3990: }
                   3991: table.LC_nested {
                   3992:   border: 0px;
                   3993:   border-collapse: separate;
                   3994:   border-spacing: 0px;
                   3995:   width: 100%;
                   3996: }
1.523     albertel 3997: table.LC_data_table tr th, table.LC_calendar tr th, table.LC_mail_list tr th,
                   3998: table.LC_prior_tries tr th {
1.349     albertel 3999:   font-weight: bold;
                   4000:   background-color: $data_table_head;
1.421     albertel 4001:   font-size: smaller;
1.347     albertel 4002: }
1.440     albertel 4003: table.LC_data_table tr td, 
                   4004: table.LC_aboutme_port tr td {
1.349     albertel 4005:   background-color: $data_table_light;
1.425     albertel 4006:   padding: 2px;
1.347     albertel 4007: }
1.440     albertel 4008: table.LC_data_table tr.LC_even_row td,
                   4009: table.LC_aboutme_port tr.LC_even_row td {
1.349     albertel 4010:   background-color: $data_table_dark;
1.347     albertel 4011: }
1.425     albertel 4012: table.LC_data_table tr.LC_data_table_highlight td {
                   4013:   background-color: $data_table_darker;
                   4014: }
1.451     albertel 4015: table.LC_data_table tr.LC_empty_row td,
1.507     raeburn  4016: table.LC_nested tr.LC_empty_row td {
1.347     albertel 4017:   background-color: #FFFFFF;
1.421     albertel 4018:   font-weight: bold;
                   4019:   font-style: italic;
                   4020:   text-align: center;
                   4021:   padding: 8px;
1.347     albertel 4022: }
1.507     raeburn  4023: table.LC_nested tr.LC_empty_row td {
1.465     albertel 4024:   padding: 4ex
                   4025: }
1.507     raeburn  4026: table.LC_nested_outer tr th {
                   4027:   font-weight: bold;
                   4028:   background-color: $data_table_head;
                   4029:   font-size: smaller;
                   4030:   border-bottom: 1px solid #000000;
                   4031: }
                   4032: table.LC_nested_outer tr td.LC_subheader {
                   4033:   background-color: $data_table_head;
                   4034:   font-weight: bold;
                   4035:   font-size: small;
                   4036:   border-bottom: 1px solid #000000;
                   4037:   text-align: right;
1.451     albertel 4038: }
1.507     raeburn  4039: table.LC_nested tr.LC_info_row td {
1.451     albertel 4040:   background-color: #CCC;
                   4041:   font-weight: bold;
                   4042:   font-size: small;
1.507     raeburn  4043:   text-align: center;
                   4044: }
                   4045: table.LC_nested tr.LC_info_row td.LC_left_item {
                   4046:   text-align: left;
1.451     albertel 4047: }
1.507     raeburn  4048: table.LC_nested td {
1.451     albertel 4049:   background-color: #FFF;
                   4050:   font-size: small;
1.507     raeburn  4051: }
                   4052: table.LC_nested_outer tr th.LC_right_item,
                   4053: table.LC_nested tr.LC_info_row td.LC_right_item,
                   4054: table.LC_nested tr.LC_odd_row td.LC_right_item,
                   4055: table.LC_nested tr td.LC_right_item {
1.451     albertel 4056:   text-align: right;
                   4057: }
                   4058: 
1.507     raeburn  4059: table.LC_nested tr.LC_odd_row td {
1.451     albertel 4060:   background-color: #EEE;
                   4061: }
                   4062: 
1.473     raeburn  4063: table.LC_createuser {
                   4064: }
                   4065: 
                   4066: table.LC_createuser tr.LC_section_row td {
                   4067:   font-size: smaller;
                   4068: }
                   4069: 
                   4070: table.LC_createuser tr.LC_info_row td  {
                   4071:   background-color: #CCC;
                   4072:   font-weight: bold;
                   4073:   text-align: center;
                   4074: }
                   4075: 
1.349     albertel 4076: table.LC_calendar {
                   4077:   border: 1px solid #000000;
                   4078:   border-collapse: collapse;
                   4079: }
                   4080: table.LC_calendar_pickdate {
                   4081:   font-size: xx-small;
                   4082: }
                   4083: table.LC_calendar tr td {
                   4084:   border: 1px solid #000000;
                   4085:   vertical-align: top;
                   4086: }
                   4087: table.LC_calendar tr td.LC_calendar_day_empty {
                   4088:   background-color: $data_table_dark;
                   4089: }
                   4090: table.LC_calendar tr td.LC_calendar_day_current {
                   4091:   background-color: $data_table_highlight;
                   4092: }
                   4093: 
                   4094: table.LC_mail_list tr.LC_mail_new {
                   4095:   background-color: $mail_new;
                   4096: }
                   4097: table.LC_mail_list tr.LC_mail_new:hover {
                   4098:   background-color: $mail_new_hover;
                   4099: }
                   4100: table.LC_mail_list tr.LC_mail_read {
                   4101:   background-color: $mail_read;
                   4102: }
                   4103: table.LC_mail_list tr.LC_mail_read:hover {
                   4104:   background-color: $mail_read_hover;
                   4105: }
                   4106: table.LC_mail_list tr.LC_mail_replied {
                   4107:   background-color: $mail_replied;
                   4108: }
                   4109: table.LC_mail_list tr.LC_mail_replied:hover {
                   4110:   background-color: $mail_replied_hover;
                   4111: }
                   4112: table.LC_mail_list tr.LC_mail_other {
                   4113:   background-color: $mail_other;
                   4114: }
                   4115: table.LC_mail_list tr.LC_mail_other:hover {
                   4116:   background-color: $mail_other_hover;
                   4117: }
1.494     raeburn  4118: table.LC_mail_list tr.LC_mail_even {
                   4119: }
                   4120: table.LC_mail_list tr.LC_mail_odd {
                   4121: }
                   4122: 
1.385     albertel 4123: 
1.386     albertel 4124: table#LC_portfolio_actions {
                   4125:   width: auto;
                   4126:   background: $pgbg;
                   4127:   border: 0px;
                   4128:   border-spacing: 2px 2px;
                   4129:   padding: 0px;
                   4130:   margin: 0px;
                   4131:   border-collapse: separate;
                   4132: }
                   4133: table#LC_portfolio_actions td.LC_label {
                   4134:   background: $tabbg;
                   4135:   text-align: right;
                   4136: }
                   4137: table#LC_portfolio_actions td.LC_value {
                   4138:   background: $tabbg;
                   4139: }
1.385     albertel 4140: 
1.391     albertel 4141: table#LC_cstr_controls {
                   4142:   width: 100%;
                   4143:   border-collapse: collapse;
                   4144: }
                   4145: table#LC_cstr_controls tr td {
                   4146:   border: 4px solid $pgbg;
                   4147:   padding: 4px;
                   4148:   text-align: center;
                   4149:   background: $tabbg;
                   4150: }
                   4151: table#LC_cstr_controls tr th {
                   4152:   border: 4px solid $pgbg;
                   4153:   background: $table_header;
                   4154:   text-align: center;
                   4155:   font-family: $sans;
                   4156:   font-size: smaller;
                   4157: }
                   4158: 
1.389     albertel 4159: table#LC_browser {
                   4160:  
                   4161: }
                   4162: table#LC_browser tr th {
1.391     albertel 4163:   background: $table_header;
1.389     albertel 4164: }
1.390     albertel 4165: table#LC_browser tr td {
                   4166:   padding: 2px;
                   4167: }
1.389     albertel 4168: table#LC_browser tr.LC_browser_file,
                   4169: table#LC_browser tr.LC_browser_file_published {
                   4170:   background: #CCFF88;
                   4171: }
                   4172: table#LC_browser tr.LC_browser_file_locked,
                   4173: table#LC_browser tr.LC_browser_file_unpublished {
                   4174:   background: #FFAA99;
1.387     albertel 4175: }
1.389     albertel 4176: table#LC_browser tr.LC_browser_file_obsolete {
                   4177:   background: #AAAAAA;
1.387     albertel 4178: }
1.455     albertel 4179: table#LC_browser tr.LC_browser_file_modified,
                   4180: table#LC_browser tr.LC_browser_file_metamodified {
1.389     albertel 4181:   background: #FFFF77;
1.387     albertel 4182: }
1.389     albertel 4183: table#LC_browser tr.LC_browser_folder {
                   4184:   background: #CCCCFF;
1.387     albertel 4185: }
1.388     albertel 4186: span.LC_current_location {
                   4187:   font-size: x-large;
                   4188:   background: $pgbg;
                   4189: }
1.387     albertel 4190: 
1.395     albertel 4191: span.LC_parm_menu_item {
                   4192:   font-size: larger;
                   4193:   font-family: $sans;
                   4194: }
                   4195: span.LC_parm_scope_all {
                   4196:   color: red;
                   4197: }
                   4198: span.LC_parm_scope_folder {
                   4199:   color: green;
                   4200: }
                   4201: span.LC_parm_scope_resource {
                   4202:   color: orange;
                   4203: }
                   4204: span.LC_parm_part {
                   4205:   color: blue;
                   4206: }
                   4207: span.LC_parm_folder, span.LC_parm_symb {
                   4208:   font-size: x-small;
                   4209:   font-family: $mono;
                   4210:   color: #AAAAAA;
                   4211: }
                   4212: 
1.396     albertel 4213: td.LC_parm_overview_level_menu, td.LC_parm_overview_map_menu,
                   4214: td.LC_parm_overview_parm_selectors, td.LC_parm_overview_parm_restrictions {
                   4215:   border: 1px solid black;
                   4216:   border-collapse: collapse;
                   4217: }
                   4218: table.LC_parm_overview_restrictions td {
                   4219:   border-width: 1px 4px 1px 4px;
                   4220:   border-style: solid;
                   4221:   border-color: $pgbg;
                   4222:   text-align: center;
                   4223: }
                   4224: table.LC_parm_overview_restrictions th {
                   4225:   background: $tabbg;
                   4226:   border-width: 1px 4px 1px 4px;
                   4227:   border-style: solid;
                   4228:   border-color: $pgbg;
                   4229: }
1.398     albertel 4230: table#LC_helpmenu {
                   4231:   border: 0px;
                   4232:   height: 55px;
                   4233:   border-spacing: 0px;
                   4234: }
                   4235: 
                   4236: table#LC_helpmenu fieldset legend {
                   4237:   font-size: larger;
                   4238:   font-weight: bold;
                   4239: }
1.397     albertel 4240: table#LC_helpmenu_links {
                   4241:   width: 100%;
                   4242:   border: 1px solid black;
                   4243:   background: $pgbg;
                   4244:   padding: 0px;
                   4245:   border-spacing: 1px;
                   4246: }
                   4247: table#LC_helpmenu_links tr td {
                   4248:   padding: 1px;
                   4249:   background: $tabbg;
1.399     albertel 4250:   text-align: center;
                   4251:   font-weight: bold;
1.397     albertel 4252: }
1.396     albertel 4253: 
1.397     albertel 4254: table#LC_helpmenu_links a:link, table#LC_helpmenu_links a:visited,
                   4255: table#LC_helpmenu_links a:active {
                   4256:   text-decoration: none;
                   4257:   color: $font;
                   4258: }
                   4259: table#LC_helpmenu_links a:hover {
                   4260:   text-decoration: underline;
                   4261:   color: $vlink;
                   4262: }
1.396     albertel 4263: 
1.417     albertel 4264: .LC_chrt_popup_exists {
                   4265:   border: 1px solid #339933;
                   4266:   margin: -1px;
                   4267: }
                   4268: .LC_chrt_popup_up {
                   4269:   border: 1px solid yellow;
                   4270:   margin: -1px;
                   4271: }
                   4272: .LC_chrt_popup {
                   4273:   border: 1px solid #8888FF;
                   4274:   background: #CCCCFF;
                   4275: }
                   4276: 
1.421     albertel 4277: table.LC_pick_box {
                   4278:   width: 100%;
                   4279:   border-collapse: separate;
                   4280:   background: white;
                   4281:   border: 1px solid black;
                   4282:   border-spacing: 1px;
                   4283: }
                   4284: table.LC_pick_box td.LC_pick_box_title {
                   4285:   background: $tabbg;
                   4286:   font-weight: bold;
                   4287:   text-align: right;
                   4288:   width: 184px;
                   4289:   padding: 8px;
                   4290: }
1.424     albertel 4291: table.LC_pick_box td.LC_pick_box_separator {
1.421     albertel 4292:   padding: 0px;
                   4293:   height: 1px;
                   4294:   background: black;
                   4295: }
                   4296: table.LC_pick_box td.LC_pick_box_submit {
                   4297:   text-align: right;
                   4298: }
                   4299: 
1.424     albertel 4300: table.LC_group_priv_box {
                   4301:   background: white;
                   4302:   border: 1px solid black;
                   4303:   border-spacing: 1px;
                   4304: }
                   4305: table.LC_group_priv_box td.LC_pick_box_title {
                   4306:   background: $tabbg;
                   4307:   font-weight: bold;
                   4308:   text-align: right;
                   4309:   width: 184px;
                   4310: }
                   4311: table.LC_group_priv_box td.LC_groups_fixed {
                   4312:   background: $data_table_light;
                   4313:   text-align: center;
                   4314: }
                   4315: table.LC_group_priv_box td.LC_groups_optional {
                   4316:   background: $data_table_dark;
                   4317:   text-align: center;
                   4318: }
                   4319: table.LC_group_priv_box td.LC_groups_functionality {
                   4320:   background: $data_table_darker;
                   4321:   text-align: center;
                   4322:   font-weight: bold;
                   4323: }
                   4324: table.LC_group_priv td {
                   4325:   text-align: left;
                   4326:   padding: 0px;
                   4327: }
                   4328: 
1.421     albertel 4329: table.LC_notify_front_page {
                   4330:   background: white;
                   4331:   border: 1px solid black;
                   4332:   padding: 8px;
                   4333: }
                   4334: table.LC_notify_front_page td {
                   4335:   padding: 8px;
                   4336: }
1.424     albertel 4337: .LC_navbuttons {
                   4338:   margin: 2ex 0ex 2ex 0ex;
                   4339: }
1.423     albertel 4340: .LC_topic_bar {
                   4341:   font-family: $sans;
                   4342:   font-weight: bold;
                   4343:   width: 100%;
                   4344:   background: $tabbg;
                   4345:   vertical-align: middle;
                   4346:   margin: 2ex 0ex 2ex 0ex;
                   4347: }
                   4348: .LC_topic_bar span {
                   4349:   vertical-align: middle;
                   4350: }
                   4351: .LC_topic_bar img {
                   4352:   vertical-align: bottom;
                   4353: }
                   4354: table.LC_course_group_status {
                   4355:   margin: 20px;
                   4356: }
                   4357: table.LC_status_selector td {
                   4358:   vertical-align: top;
                   4359:   text-align: center;
1.424     albertel 4360:   padding: 4px;
                   4361: }
                   4362: table.LC_descriptive_input td.LC_description {
                   4363:   vertical-align: top;
                   4364:   text-align: right;
                   4365:   font-weight: bold;
1.423     albertel 4366: }
1.489     raeburn  4367: table.LC_feedback_link {
                   4368:     background: $feedback_link_bg;
                   4369: }
                   4370: span.LC_feedback_link {
                   4371:     background: $feedback_link_bg;
                   4372:     font-size: larger;
                   4373: }
1.421     albertel 4374: 
1.515     albertel 4375: table.LC_prior_tries {
1.524     albertel 4376:   border: 1px solid #000000;
                   4377:   border-collapse: separate;
                   4378:   border-spacing: 1px;
1.515     albertel 4379: }
1.523     albertel 4380: 
1.515     albertel 4381: table.LC_prior_tries td {
1.524     albertel 4382:   padding: 2px;
1.515     albertel 4383: }
1.523     albertel 4384: 
                   4385: .LC_answer_correct {
                   4386:   background: #AAFFAA;
                   4387:   color: black;
                   4388: }
                   4389: .LC_answer_charged_try {
                   4390:   background: #FFAAAA ! important;
                   4391:   color: black;
                   4392: }
                   4393: .LC_answer_not_charged_try, 
                   4394: .LC_answer_no_grade,
                   4395: .LC_answer_late {
                   4396:   background: #FFFFAA;
                   4397:   color: black;
                   4398: }
                   4399: .LC_answer_previous {
                   4400:   background: #AAAAFF;
                   4401:   color: black;
                   4402: }
                   4403: .LC_answer_no_message {
                   4404:   background: #FFFFFF;
                   4405:   color: black;
                   4406: }
                   4407: .LC_answer_unknown {
                   4408:   background: orange;
                   4409:   color: black;
                   4410: }
                   4411: 
                   4412: 
1.529     albertel 4413: span.LC_prior_numerical,
                   4414: span.LC_prior_string,
                   4415: span.LC_prior_custom,
                   4416: span.LC_prior_reaction,
                   4417: span.LC_prior_math {
1.523     albertel 4418:   font-family: monospace;
                   4419:   white-space: pre;
                   4420: }
                   4421: 
1.525     albertel 4422: span.LC_prior_string {
                   4423:   font-family: monospace;
                   4424:   white-space: pre;
                   4425: }
                   4426: 
1.523     albertel 4427: table.LC_prior_option {
                   4428:   width: 100%;
                   4429:   border-collapse: collapse;
                   4430: }
1.528     albertel 4431: table.LC_prior_rank, table.LC_prior_match {
                   4432:   border-collapse: collapse;
                   4433: }
                   4434: table.LC_prior_option tr td,
                   4435: table.LC_prior_rank tr td,
                   4436: table.LC_prior_match tr td {
1.524     albertel 4437:   border: 1px solid #000000;
1.515     albertel 4438: }
                   4439: 
1.519     raeburn  4440: span.LC_nobreak {
1.544     albertel 4441:   white-space: nowrap;
1.519     raeburn  4442: }
                   4443: 
1.545     albertel 4444: table.LC_docs_documents {
                   4445:   background: #BBBBBB;
1.547     albertel 4446:   border-width: 0px;
1.545     albertel 4447:   border-collapse: collapse;
                   4448: }
                   4449: 
                   4450: table.LC_docs_documents td.LC_docs_document {
                   4451:   border: 2px solid black;
                   4452:   padding: 4px;
                   4453: }
                   4454: 
                   4455: .LC_docs_course_commands div {
                   4456:   float: left;
                   4457:   border: 4px solid #AAAAAA;
                   4458:   padding: 4px;
                   4459:   background: #DDDDCC;
                   4460: }
                   4461: 
                   4462: .LC_docs_entry_move {
                   4463:   border: 0px;
                   4464:   border-collapse: collapse;
1.544     albertel 4465: }
                   4466: 
1.545     albertel 4467: .LC_docs_entry_move td {
                   4468:   border: 2px solid #BBBBBB;
                   4469:   background: #DDDDDD;
                   4470: }
                   4471: 
                   4472: .LC_docs_editor td.LC_docs_entry_commands {
                   4473:   background: #DDDDDD;
                   4474:   font-size: x-small;
                   4475: }
1.544     albertel 4476: .LC_docs_copy {
1.545     albertel 4477:   color: #000099;
1.544     albertel 4478: }
                   4479: .LC_docs_cut {
1.545     albertel 4480:   color: #550044;
1.544     albertel 4481: }
                   4482: .LC_docs_rename {
1.545     albertel 4483:   color: #009900;
1.544     albertel 4484: }
                   4485: .LC_docs_remove {
1.545     albertel 4486:   color: #990000;
                   4487: }
                   4488: 
1.547     albertel 4489: .LC_docs_reinit_warn,
                   4490: .LC_docs_ext_edit {
                   4491:   font-size: x-small;
                   4492: }
                   4493: 
1.545     albertel 4494: .LC_docs_editor td.LC_docs_entry_title,
                   4495: .LC_docs_editor td.LC_docs_entry_icon {
                   4496:   background: #FFFFBB;
                   4497: }
                   4498: .LC_docs_editor td.LC_docs_entry_parameter {
                   4499:   background: #BBBBFF;
                   4500:   font-size: x-small;
                   4501:   white-space: nowrap;
                   4502: }
                   4503: 
                   4504: table.LC_docs_adddocs td,
                   4505: table.LC_docs_adddocs th {
                   4506:   border: 1px solid #BBBBBB;
                   4507:   padding: 4px;
                   4508:   background: #DDDDDD;
1.543     albertel 4509: }
                   4510: 
1.343     albertel 4511: END
                   4512: }
                   4513: 
1.306     albertel 4514: =pod
                   4515: 
                   4516: =item * &headtag()
                   4517: 
                   4518: Returns a uniform footer for LON-CAPA web pages.
                   4519: 
1.307     albertel 4520: Inputs: $title - optional title for the head
                   4521:         $head_extra - optional extra HTML to put inside the <head>
1.315     albertel 4522:         $args - optional arguments
1.319     albertel 4523:             force_register - if is true call registerurl so the remote is 
                   4524:                              informed
1.415     albertel 4525:             redirect       -> array ref of
                   4526:                                    1- seconds before redirect occurs
                   4527:                                    2- url to redirect to
                   4528:                                    3- whether the side effect should occur
1.315     albertel 4529:                            (side effect of setting 
                   4530:                                $env{'internal.head.redirect'} to the url 
                   4531:                                redirected too)
1.352     albertel 4532:             domain         -> force to color decorate a page for a specific
                   4533:                                domain
                   4534:             function       -> force usage of a specific rolish color scheme
                   4535:             bgcolor        -> override the default page bgcolor
1.460     albertel 4536:             no_auto_mt_title
                   4537:                            -> prevent &mt()ing the title arg
1.464     albertel 4538: 
1.306     albertel 4539: =cut
                   4540: 
                   4541: sub headtag {
1.313     albertel 4542:     my ($title,$head_extra,$args) = @_;
1.306     albertel 4543:     
1.363     albertel 4544:     my $function = $args->{'function'} || &get_users_function();
                   4545:     my $domain   = $args->{'domain'}   || &determinedomain();
                   4546:     my $bgcolor  = $args->{'bgcolor'}  || &designparm($function.'.pgbg',$domain);
1.418     albertel 4547:     my $url = join(':',$env{'user.name'},$env{'user.domain'},
1.458     albertel 4548: 		   $Apache::lonnet::perlvar{'lonVersion'},
1.531     albertel 4549: 		   #time(),
1.418     albertel 4550: 		   $env{'environment.color.timestamp'},
1.363     albertel 4551: 		   $function,$domain,$bgcolor);
                   4552: 
1.369     www      4553:     $url = '/adm/css/'.&escape($url).'.css';
1.363     albertel 4554: 
1.308     albertel 4555:     my $result =
                   4556: 	'<head>'.
1.461     albertel 4557: 	&font_settings();
1.319     albertel 4558: 
1.461     albertel 4559:     if (!$args->{'frameset'}) {
                   4560: 	$result .= &Apache::lonhtmlcommon::htmlareaheaders();
                   4561:     }
1.319     albertel 4562:     if ($args->{'force_register'}) {
                   4563: 	$result .= &Apache::lonmenu::registerurl(1);
                   4564:     }
1.436     albertel 4565:     if (!$args->{'no_nav_bar'} 
                   4566: 	&& !$args->{'only_body'}
                   4567: 	&& !$args->{'frameset'}) {
                   4568: 	$result .= &help_menu_js();
                   4569:     }
1.319     albertel 4570: 
1.314     albertel 4571:     if (ref($args->{'redirect'})) {
1.414     albertel 4572: 	my ($time,$url,$inhibit_continue) = @{$args->{'redirect'}};
1.315     albertel 4573: 	$url = &Apache::lonenc::check_encrypt($url);
1.414     albertel 4574: 	if (!$inhibit_continue) {
                   4575: 	    $env{'internal.head.redirect'} = $url;
                   4576: 	}
1.313     albertel 4577: 	$result.=<<ADDMETA
                   4578: <meta http-equiv="pragma" content="no-cache" />
1.344     albertel 4579: <meta http-equiv="Refresh" content="$time; url=$url" />
1.313     albertel 4580: ADDMETA
                   4581:     }
1.306     albertel 4582:     if (!defined($title)) {
                   4583: 	$title = 'The LearningOnline Network with CAPA';
                   4584:     }
1.460     albertel 4585:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
                   4586:     $result .= '<title> LON-CAPA '.$title.'</title>'
1.414     albertel 4587: 	.'<link rel="stylesheet" type="text/css" href="'.$url.'" />'
                   4588: 	.$head_extra;
1.306     albertel 4589:     return $result;
                   4590: }
                   4591: 
                   4592: =pod
                   4593: 
1.340     albertel 4594: =item * &font_settings()
                   4595: 
                   4596: Returns neccessary <meta> to set the proper encoding
                   4597: 
                   4598: Inputs: none
                   4599: 
                   4600: =cut
                   4601: 
                   4602: sub font_settings {
                   4603:     my $headerstring='';
                   4604:     if (($env{'browser.os'} eq 'mac') && (!$env{'browser.mathml'})) { 
                   4605: 	$headerstring.=
                   4606: 	    '<meta Content-Type="text/html; charset=x-mac-roman" />';
                   4607:     } elsif (!$env{'browser.mathml'} && $env{'browser.unicode'}) {
                   4608: 	$headerstring.=
                   4609: 	    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
                   4610:     }
                   4611:     return $headerstring;
                   4612: }
                   4613: 
1.341     albertel 4614: =pod
                   4615: 
                   4616: =item * &xml_begin()
                   4617: 
                   4618: Returns the needed doctype and <html>
                   4619: 
                   4620: Inputs: none
                   4621: 
                   4622: =cut
                   4623: 
                   4624: sub xml_begin {
                   4625:     my $output='';
                   4626: 
1.342     albertel 4627:     &Apache::lonhtmlcommon::init_htmlareafields();
                   4628: 
1.341     albertel 4629:     if ($env{'browser.mathml'}) {
                   4630: 	$output='<?xml version="1.0"?>'
                   4631:             #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
                   4632: #            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                   4633:             
                   4634: #	    .'<!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">] >'
                   4635: 	    .'<!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">'
                   4636:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                   4637: 	    .'xmlns="http://www.w3.org/1999/xhtml">';
                   4638:     } else {
                   4639: 	$output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>';
                   4640:     }
                   4641:     return $output;
                   4642: }
1.340     albertel 4643: 
                   4644: =pod
                   4645: 
1.306     albertel 4646: =item * &endheadtag()
                   4647: 
                   4648: Returns a uniform </head> for LON-CAPA web pages.
                   4649: 
                   4650: Inputs: none
                   4651: 
                   4652: =cut
                   4653: 
                   4654: sub endheadtag {
                   4655:     return '</head>';
                   4656: }
                   4657: 
                   4658: =pod
                   4659: 
                   4660: =item * &head()
                   4661: 
                   4662: Returns a uniform complete <head>..</head> section for LON-CAPA web pages.
                   4663: 
                   4664: Inputs: $title - optional title for the page
1.307     albertel 4665:         $head_extra - optional extra HTML to put inside the <head>
1.405     albertel 4666: 
1.306     albertel 4667: =cut
                   4668: 
                   4669: sub head {
1.325     albertel 4670:     my ($title,$head_extra,$args) = @_;
                   4671:     return &headtag($title,$head_extra,$args).&endheadtag();
1.306     albertel 4672: }
                   4673: 
                   4674: =pod
                   4675: 
                   4676: =item * &start_page()
                   4677: 
                   4678: Returns a complete <html> .. <body> section for LON-CAPA web pages.
                   4679: 
                   4680: Inputs: $title - optional title for the page
                   4681:         $head_extra - optional extra HTML to incude inside the <head>
1.315     albertel 4682:         $args - additional optional args supported are:
1.317     albertel 4683:                   only_body      -> is true will set &bodytag() onlybodytag
                   4684:                                     arg on
                   4685:                   no_nav_bar     -> is true will set &bodytag() notopbar arg on
                   4686:                   add_entries    -> additional attributes to add to the  <body>
                   4687:                   domain         -> force to color decorate a page for a 
                   4688:                                     specific domain
                   4689:                   function       -> force usage of a specific rolish color
                   4690:                                     scheme
                   4691:                   redirect       -> see &headtag()
                   4692:                   bgcolor        -> override the default page bg color
                   4693:                   js_ready       -> return a string ready for being used in 
                   4694:                                     a javascript writeln
1.320     albertel 4695:                   html_encode    -> return a string ready for being used in 
                   4696:                                     a html attribute
1.317     albertel 4697:                   force_register -> if is true will turn on the &bodytag()
                   4698:                                     $forcereg arg
1.326     albertel 4699:                   body_title     -> alternate text to use instead of $title
                   4700:                                     in the title box that appears, this text
                   4701:                                     is not auto translated like the $title is
1.330     albertel 4702:                   frameset       -> if true will start with a <frameset>
                   4703:                                     rather than <body>
1.338     albertel 4704:                   no_title       -> if true the title bar won't be shown
                   4705:                   skip_phases    -> hash ref of 
                   4706:                                     head -> skip the <html><head> generation
                   4707:                                     body -> skip all <body> generation
1.337     albertel 4708: 
1.361     albertel 4709:                   no_inline_link -> if true and in remote mode, don't show the 
                   4710:                                     'Switch To Inline Menu' link
                   4711: 
1.460     albertel 4712:                   no_auto_mt_title -> prevent &mt()ing the title arg
                   4713: 
1.306     albertel 4714: =cut
                   4715: 
                   4716: sub start_page {
1.309     albertel 4717:     my ($title,$head_extra,$args) = @_;
1.318     albertel 4718:     #&Apache::lonnet::logthis("start_page ".join(':',caller(0)));
1.313     albertel 4719:     my %head_args;
1.352     albertel 4720:     foreach my $arg ('redirect','force_register','domain','function',
1.460     albertel 4721: 		     'bgcolor','frameset','no_nav_bar','only_body',
                   4722: 		     'no_auto_mt_title') {
1.319     albertel 4723: 	if (defined($args->{$arg})) {
1.324     raeburn  4724: 	    $head_args{$arg} = $args->{$arg};
1.319     albertel 4725: 	}
1.313     albertel 4726:     }
1.319     albertel 4727: 
1.315     albertel 4728:     $env{'internal.start_page'}++;
1.338     albertel 4729:     my $result;
                   4730:     if (! exists($args->{'skip_phases'}{'head'}) ) {
                   4731: 	$result.=
1.341     albertel 4732: 	    &xml_begin().
1.338     albertel 4733: 	    &headtag($title,$head_extra,\%head_args).&endheadtag();
                   4734:     }
                   4735:     
                   4736:     if (! exists($args->{'skip_phases'}{'body'}) ) {
                   4737: 	if ($args->{'frameset'}) {
                   4738: 	    my $attr_string = &make_attr_string($args->{'force_register'},
                   4739: 						$args->{'add_entries'});
                   4740: 	    $result .= "\n<frameset $attr_string>\n";
                   4741: 	} else {
                   4742: 	    $result .=
                   4743: 		&bodytag($title, 
                   4744: 			 $args->{'function'},       $args->{'add_entries'},
                   4745: 			 $args->{'only_body'},      $args->{'domain'},
                   4746: 			 $args->{'force_register'}, $args->{'body_title'},
                   4747: 			 $args->{'no_nav_bar'},     $args->{'bgcolor'},
1.460     albertel 4748: 			 $args->{'no_title'},       $args->{'no_inline_link'},
                   4749: 			 $args);
1.338     albertel 4750: 	}
1.330     albertel 4751:     }
1.338     albertel 4752: 
1.315     albertel 4753:     if ($args->{'js_ready'}) {
1.317     albertel 4754: 	$result = &js_ready($result);
1.315     albertel 4755:     }
1.320     albertel 4756:     if ($args->{'html_encode'}) {
                   4757: 	$result = &html_encode($result);
                   4758:     }
1.315     albertel 4759:     return $result;
1.306     albertel 4760: }
                   4761: 
1.330     albertel 4762: 
1.306     albertel 4763: =pod
                   4764: 
                   4765: =item * &head()
                   4766: 
                   4767: Returns a complete </body></html> section for LON-CAPA web pages.
                   4768: 
1.315     albertel 4769: Inputs:         $args - additional optional args supported are:
                   4770:                  js_ready     -> return a string ready for being used in 
                   4771:                                  a javascript writeln
1.320     albertel 4772:                  html_encode  -> return a string ready for being used in 
                   4773:                                  a html attribute
1.330     albertel 4774:                  frameset     -> if true will start with a <frameset>
                   4775:                                  rather than <body>
1.493     albertel 4776:                  dicsussion   -> if true will get discussion from
                   4777:                                   lonxml::xmlend
                   4778:                                  (you can pass the target and parser arguments
                   4779:                                   through optional 'target' and 'parser' args
                   4780:                                   to this routine)
1.306     albertel 4781: 
                   4782: =cut
                   4783: 
                   4784: sub end_page {
1.315     albertel 4785:     my ($args) = @_;
                   4786:     $env{'internal.end_page'}++;
1.330     albertel 4787:     my $result;
1.335     albertel 4788:     if ($args->{'discussion'}) {
                   4789: 	my ($target,$parser);
                   4790: 	if (ref($args->{'discussion'})) {
                   4791: 	    ($target,$parser) =($args->{'discussion'}{'target'},
                   4792: 				$args->{'discussion'}{'parser'});
                   4793: 	}
                   4794: 	$result .= &Apache::lonxml::xmlend($target,$parser);
                   4795:     }
                   4796: 
1.330     albertel 4797:     if ($args->{'frameset'}) {
                   4798: 	$result .= '</frameset>';
                   4799:     } else {
                   4800: 	$result .= &endbodytag();
                   4801:     }
                   4802:     $result .= "\n</html>";
                   4803: 
1.315     albertel 4804:     if ($args->{'js_ready'}) {
1.317     albertel 4805: 	$result = &js_ready($result);
1.315     albertel 4806:     }
1.335     albertel 4807: 
1.320     albertel 4808:     if ($args->{'html_encode'}) {
                   4809: 	$result = &html_encode($result);
                   4810:     }
1.335     albertel 4811: 
1.315     albertel 4812:     return $result;
                   4813: }
                   4814: 
1.320     albertel 4815: sub html_encode {
                   4816:     my ($result) = @_;
                   4817: 
1.322     albertel 4818:     $result = &HTML::Entities::encode($result,'<>&"');
1.320     albertel 4819:     
                   4820:     return $result;
                   4821: }
1.317     albertel 4822: sub js_ready {
                   4823:     my ($result) = @_;
                   4824: 
1.323     albertel 4825:     $result =~ s/[\n\r]/ /xmsg;
                   4826:     $result =~ s/\\/\\\\/xmsg;
                   4827:     $result =~ s/'/\\'/xmsg;
1.372     albertel 4828:     $result =~ s{</}{<\\/}xmsg;
1.317     albertel 4829:     
                   4830:     return $result;
                   4831: }
                   4832: 
1.315     albertel 4833: sub validate_page {
                   4834:     if (  exists($env{'internal.start_page'})
1.316     albertel 4835: 	  &&     $env{'internal.start_page'} > 1) {
                   4836: 	&Apache::lonnet::logthis('start_page called multiple times '.
1.318     albertel 4837: 				 $env{'internal.start_page'}.' '.
1.316     albertel 4838: 				 $ENV{'request.filename'});
1.315     albertel 4839:     }
                   4840:     if (  exists($env{'internal.end_page'})
1.316     albertel 4841: 	  &&     $env{'internal.end_page'} > 1) {
                   4842: 	&Apache::lonnet::logthis('end_page called multiple times '.
1.318     albertel 4843: 				 $env{'internal.end_page'}.' '.
1.316     albertel 4844: 				 $env{'request.filename'});
1.315     albertel 4845:     }
                   4846:     if (     exists($env{'internal.start_page'})
                   4847: 	&& ! exists($env{'internal.end_page'})) {
1.316     albertel 4848: 	&Apache::lonnet::logthis('start_page called without end_page '.
                   4849: 				 $env{'request.filename'});
1.315     albertel 4850:     }
                   4851:     if (   ! exists($env{'internal.start_page'})
                   4852: 	&&   exists($env{'internal.end_page'})) {
1.316     albertel 4853: 	&Apache::lonnet::logthis('end_page called without start_page'.
                   4854: 				 $env{'request.filename'});
1.315     albertel 4855:     }
1.306     albertel 4856: }
1.315     albertel 4857: 
1.318     albertel 4858: sub simple_error_page {
                   4859:     my ($r,$title,$msg) = @_;
                   4860:     my $page =
                   4861: 	&Apache::loncommon::start_page($title).
                   4862: 	&mt($msg).
                   4863: 	&Apache::loncommon::end_page();
                   4864:     if (ref($r)) {
                   4865: 	$r->print($page);
1.327     albertel 4866: 	return;
1.318     albertel 4867:     }
                   4868:     return $page;
                   4869: }
1.347     albertel 4870: 
                   4871: {
                   4872:     my $row_count;
                   4873:     sub start_data_table {
1.422     albertel 4874: 	my ($add_class) = @_;
                   4875: 	my $css_class = (join(' ','LC_data_table',$add_class));
1.347     albertel 4876: 	undef($row_count);
1.422     albertel 4877: 	return '<table class="'.$css_class.'">'."\n";
1.347     albertel 4878:     }
                   4879: 
                   4880:     sub end_data_table {
                   4881: 	undef($row_count);
1.389     albertel 4882: 	return '</table>'."\n";;
1.347     albertel 4883:     }
                   4884: 
                   4885:     sub start_data_table_row {
1.422     albertel 4886: 	my ($add_class) = @_;
1.347     albertel 4887: 	$row_count++;
1.422     albertel 4888: 	my $css_class = ($row_count % 2)?'':'LC_even_row';
1.428     albertel 4889: 	$css_class = (join(' ',$css_class,$add_class));
1.422     albertel 4890: 	return  '<tr class="'.$css_class.'">'."\n";;
1.347     albertel 4891:     }
1.471     banghart 4892:     
                   4893:     sub continue_data_table_row {
                   4894: 	my ($add_class) = @_;
                   4895: 	my $css_class = ($row_count % 2)?'':'LC_even_row';
                   4896: 	$css_class = (join(' ',$css_class,$add_class));
                   4897: 	return  '<tr class="'.$css_class.'">'."\n";;
                   4898:     }
1.347     albertel 4899: 
                   4900:     sub end_data_table_row {
1.389     albertel 4901: 	return '</tr>'."\n";;
1.347     albertel 4902:     }
1.367     www      4903: 
1.421     albertel 4904:     sub start_data_table_empty_row {
                   4905: 	$row_count++;
                   4906: 	return  '<tr class="LC_empty_row" >'."\n";;
                   4907:     }
                   4908: 
                   4909:     sub end_data_table_empty_row {
                   4910: 	return '</tr>'."\n";;
                   4911:     }
                   4912: 
1.367     www      4913:     sub start_data_table_header_row {
1.389     albertel 4914: 	return  '<tr class="LC_header_row">'."\n";;
1.367     www      4915:     }
                   4916: 
                   4917:     sub end_data_table_header_row {
1.389     albertel 4918: 	return '</tr>'."\n";;
1.367     www      4919:     }
1.347     albertel 4920: }
                   4921: 
1.548     albertel 4922: =pod
                   4923: 
                   4924: =item * &inhibit_menu_check($arg)
                   4925: 
                   4926: Checks for a inhibitmenu state and generates output to preserve it
                   4927: 
                   4928: Inputs:         $arg - can be any of
                   4929:                      - undef - in which case the return value is a string 
                   4930:                                to add  into arguments list of a uri
                   4931:                      - 'input' - in which case the return value is a HTML
                   4932:                                  <form> <input> field of type hidden to
                   4933:                                  preserve the value
                   4934:                      - a url - in which case the return value is the url with
                   4935:                                the neccesary cgi args added to preserve the
                   4936:                                inhibitmenu state
                   4937:                      - a ref to a url - no return value, but the string is
                   4938:                                         updated to include the neccessary cgi
                   4939:                                         args to preserve the inhibitmenu state
                   4940: 
                   4941: =cut
                   4942: 
                   4943: sub inhibit_menu_check {
                   4944:     my ($arg) = @_;
                   4945:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
                   4946:     if ($arg eq 'input') {
                   4947: 	if ($env{'form.inhibitmenu'}) {
                   4948: 	    return '<input type="hidden" name="inhibitmenu" value="'.$env{'form.inhibitmenu'}.'" />';
                   4949: 	} else {
                   4950: 	    return
                   4951: 	}
                   4952:     }
                   4953:     if ($env{'form.inhibitmenu'}) {
                   4954: 	if (ref($arg)) {
                   4955: 	    $$arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   4956: 	} elsif ($arg eq '') {
                   4957: 	    $arg .= 'inhibitmenu='.$env{'form.inhibitmenu'};
                   4958: 	} else {
                   4959: 	    $arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   4960: 	}
                   4961:     }
                   4962:     if (!ref($arg)) {
                   4963: 	return $arg;
                   4964:     }
                   4965: }
                   4966: 
1.251     albertel 4967: ###############################################
1.182     matthew  4968: 
                   4969: =pod
                   4970: 
1.549     albertel 4971: =back
                   4972: 
                   4973: =head1 User Information Routines
                   4974: 
                   4975: =over 4
                   4976: 
1.405     albertel 4977: =item * &get_users_function()
1.182     matthew  4978: 
                   4979: Used by &bodytag to determine the current users primary role.
                   4980: Returns either 'student','coordinator','admin', or 'author'.
                   4981: 
                   4982: =cut
                   4983: 
                   4984: ###############################################
                   4985: sub get_users_function {
                   4986:     my $function = 'student';
1.258     albertel 4987:     if ($env{'request.role'}=~/^(cc|in|ta|ep)/) {
1.182     matthew  4988:         $function='coordinator';
                   4989:     }
1.258     albertel 4990:     if ($env{'request.role'}=~/^(su|dc|ad|li)/) {
1.182     matthew  4991:         $function='admin';
                   4992:     }
1.258     albertel 4993:     if (($env{'request.role'}=~/^(au|ca)/) ||
1.182     matthew  4994:         ($ENV{'REQUEST_URI'}=~/^(\/priv|\~)/)) {
                   4995:         $function='author';
                   4996:     }
                   4997:     return $function;
1.54      www      4998: }
1.99      www      4999: 
                   5000: ###############################################
                   5001: 
1.233     raeburn  5002: =pod
                   5003: 
1.542     raeburn  5004: =item * &check_user_status()
1.274     raeburn  5005: 
                   5006: Determines current status of supplied role for a
                   5007: specific user. Roles can be active, previous or future.
                   5008: 
                   5009: Inputs: 
                   5010: user's domain, user's username, course's domain,
1.375     raeburn  5011: course's number, optional section ID.
1.274     raeburn  5012: 
                   5013: Outputs:
                   5014: role status: active, previous or future. 
                   5015: 
                   5016: =cut
                   5017: 
                   5018: sub check_user_status {
1.412     raeburn  5019:     my ($udom,$uname,$cdom,$crs,$role,$sec) = @_;
1.274     raeburn  5020:     my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
                   5021:     my @uroles = keys %userinfo;
                   5022:     my $srchstr;
                   5023:     my $active_chk = 'none';
1.412     raeburn  5024:     my $now = time;
1.274     raeburn  5025:     if (@uroles > 0) {
1.412     raeburn  5026:         if (($role eq 'cc') || ($sec eq '') || (!defined($sec))) {
1.274     raeburn  5027:             $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
                   5028:         } else {
1.412     raeburn  5029:             $srchstr = '/'.$cdom.'/'.$crs.'/'.$sec.'_'.$role;
                   5030:         }
                   5031:         if (grep/^\Q$srchstr\E$/,@uroles) {
1.274     raeburn  5032:             my $role_end = 0;
                   5033:             my $role_start = 0;
                   5034:             $active_chk = 'active';
1.412     raeburn  5035:             if ($userinfo{$srchstr} =~ m/^\Q$role\E_(\d+)/) {
                   5036:                 $role_end = $1;
                   5037:                 if ($userinfo{$srchstr} =~ m/^\Q$role\E_\Q$role_end\E_(\d+)$/) {
                   5038:                     $role_start = $1;
1.274     raeburn  5039:                 }
                   5040:             }
                   5041:             if ($role_start > 0) {
1.412     raeburn  5042:                 if ($now < $role_start) {
1.274     raeburn  5043:                     $active_chk = 'future';
                   5044:                 }
                   5045:             }
                   5046:             if ($role_end > 0) {
1.412     raeburn  5047:                 if ($now > $role_end) {
1.274     raeburn  5048:                     $active_chk = 'previous';
                   5049:                 }
                   5050:             }
                   5051:         }
                   5052:     }
                   5053:     return $active_chk;
                   5054: }
                   5055: 
                   5056: ###############################################
                   5057: 
                   5058: =pod
                   5059: 
1.405     albertel 5060: =item * &get_sections()
1.233     raeburn  5061: 
                   5062: Determines all the sections for a course including
                   5063: sections with students and sections containing other roles.
1.419     raeburn  5064: Incoming parameters: 
                   5065: 
                   5066: 1. domain
                   5067: 2. course number 
                   5068: 3. reference to array containing roles for which sections should 
                   5069: be gathered (optional).
                   5070: 4. reference to array containing status types for which sections 
                   5071: should be gathered (optional).
                   5072: 
                   5073: If the third argument is undefined, sections are gathered for any role. 
                   5074: If the fourth argument is undefined, sections are gathered for any status.
                   5075: Permissible values are 'active' or 'future' or 'previous'.
1.233     raeburn  5076:  
1.374     raeburn  5077: Returns section hash (keys are section IDs, values are
                   5078: number of users in each section), subject to the
1.419     raeburn  5079: optional roles filter, optional status filter 
1.233     raeburn  5080: 
                   5081: =cut
                   5082: 
                   5083: ###############################################
                   5084: sub get_sections {
1.419     raeburn  5085:     my ($cdom,$cnum,$possible_roles,$possible_status) = @_;
1.366     albertel 5086:     if (!defined($cdom) || !defined($cnum)) {
                   5087:         my $cid =  $env{'request.course.id'};
                   5088: 
                   5089: 	return if (!defined($cid));
                   5090: 
                   5091:         $cdom = $env{'course.'.$cid.'.domain'};
                   5092:         $cnum = $env{'course.'.$cid.'.num'};
                   5093:     }
                   5094: 
                   5095:     my %sectioncount;
1.419     raeburn  5096:     my $now = time;
1.240     albertel 5097: 
1.366     albertel 5098:     if (!defined($possible_roles) || (grep(/^st$/,@$possible_roles))) {
1.276     albertel 5099: 	my ($classlist) = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1.240     albertel 5100: 	my $sec_index = &Apache::loncoursedata::CL_SECTION();
                   5101: 	my $status_index = &Apache::loncoursedata::CL_STATUS();
1.419     raeburn  5102:         my $start_index = &Apache::loncoursedata::CL_START();
                   5103:         my $end_index = &Apache::loncoursedata::CL_END();
                   5104:         my $status;
1.366     albertel 5105: 	while (my ($student,$data) = each(%$classlist)) {
1.419     raeburn  5106: 	    my ($section,$stu_status,$start,$end) = ($data->[$sec_index],
                   5107: 				                     $data->[$status_index],
                   5108:                                                      $data->[$start_index],
                   5109:                                                      $data->[$end_index]);
                   5110:             if ($stu_status eq 'Active') {
                   5111:                 $status = 'active';
                   5112:             } elsif ($end < $now) {
                   5113:                 $status = 'previous';
                   5114:             } elsif ($start > $now) {
                   5115:                 $status = 'future';
                   5116:             } 
                   5117: 	    if ($section ne '-1' && $section !~ /^\s*$/) {
                   5118:                 if ((!defined($possible_status)) || (($status ne '') && 
                   5119:                     (grep/^\Q$status\E$/,@{$possible_status}))) { 
                   5120: 		    $sectioncount{$section}++;
                   5121:                 }
1.240     albertel 5122: 	    }
                   5123: 	}
                   5124:     }
                   5125:     my %courseroles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   5126:     foreach my $user (sort(keys(%courseroles))) {
                   5127: 	if ($user !~ /^(\w{2})/) { next; }
                   5128: 	my ($role) = ($user =~ /^(\w{2})/);
                   5129: 	if ($possible_roles && !(grep(/^$role$/,@$possible_roles))) { next; }
1.419     raeburn  5130: 	my ($section,$status);
1.240     albertel 5131: 	if ($role eq 'cr' &&
                   5132: 	    $user =~ m-^$role/[^/]*/[^/]*/[^/]*:[^:]*:[^:]*:(\w+)-) {
                   5133: 	    $section=$1;
                   5134: 	}
                   5135: 	if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
                   5136: 	if (!defined($section) || $section eq '-1') { next; }
1.419     raeburn  5137:         my ($end,$start) = ($courseroles{$user} =~ /^([^:]*):([^:]*)$/);
                   5138:         if ($end == -1 && $start == -1) {
                   5139:             next; #deleted role
                   5140:         }
                   5141:         if (!defined($possible_status)) { 
                   5142:             $sectioncount{$section}++;
                   5143:         } else {
                   5144:             if ((!$end || $end >= $now) && (!$start || $start <= $now)) {
                   5145:                 $status = 'active';
                   5146:             } elsif ($end < $now) {
                   5147:                 $status = 'future';
                   5148:             } elsif ($start > $now) {
                   5149:                 $status = 'previous';
                   5150:             }
                   5151:             if (($status ne '') && (grep/^\Q$status\E$/,@{$possible_status})) {
                   5152:                 $sectioncount{$section}++;
                   5153:             }
                   5154:         }
1.233     raeburn  5155:     }
1.366     albertel 5156:     return %sectioncount;
1.233     raeburn  5157: }
                   5158: 
1.274     raeburn  5159: ###############################################
1.294     raeburn  5160: 
                   5161: =pod
1.405     albertel 5162: 
                   5163: =item * &get_course_users()
                   5164: 
1.275     raeburn  5165: Retrieves usernames:domains for users in the specified course
                   5166: with specific role(s), and access status. 
                   5167: 
                   5168: Incoming parameters:
1.277     albertel 5169: 1. course domain
                   5170: 2. course number
                   5171: 3. access status: users must have - either active, 
1.275     raeburn  5172: previous, future, or all.
1.277     albertel 5173: 4. reference to array of permissible roles
1.288     raeburn  5174: 5. reference to array of section restrictions (optional)
                   5175: 6. reference to results object (hash of hashes).
                   5176: 7. reference to optional userdata hash
1.275     raeburn  5177: Keys of top level hash are roles.
                   5178: Keys of inner hashes are username:domain, with 
                   5179: values set to access type.
1.288     raeburn  5180: Optional userdata hash returns an array with arguments in the 
                   5181: same order as loncoursedata::get_classlist() for student data.
                   5182: 
                   5183: Entries for end, start, section and status are blank because
                   5184: of the possibility of multiple values for non-student roles.
                   5185: 
1.275     raeburn  5186: =cut
1.405     albertel 5187: 
1.275     raeburn  5188: ###############################################
1.405     albertel 5189: 
1.275     raeburn  5190: sub get_course_users {
1.288     raeburn  5191:     my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata) = @_;
                   5192:     my %idx = ();
1.419     raeburn  5193:     my %seclists;
1.288     raeburn  5194: 
                   5195:     $idx{udom} = &Apache::loncoursedata::CL_SDOM();
                   5196:     $idx{uname} =  &Apache::loncoursedata::CL_SNAME();
                   5197:     $idx{end} = &Apache::loncoursedata::CL_END();
                   5198:     $idx{start} = &Apache::loncoursedata::CL_START();
                   5199:     $idx{id} = &Apache::loncoursedata::CL_ID();
                   5200:     $idx{section} = &Apache::loncoursedata::CL_SECTION();
                   5201:     $idx{fullname} = &Apache::loncoursedata::CL_FULLNAME();
                   5202:     $idx{status} = &Apache::loncoursedata::CL_STATUS();
                   5203: 
1.290     albertel 5204:     if (grep(/^st$/,@{$roles})) {
1.276     albertel 5205:         my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist($cdom,$cnum);
1.278     raeburn  5206:         my $now = time;
1.277     albertel 5207:         foreach my $student (keys(%{$classlist})) {
1.288     raeburn  5208:             my $match = 0;
1.412     raeburn  5209:             my $secmatch = 0;
1.419     raeburn  5210:             my $section = $$classlist{$student}[$idx{section}];
                   5211:             if ($section eq '') {
                   5212:                 $section = 'none';
                   5213:             }
1.291     albertel 5214:             if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 5215:                 if (grep(/^all$/,@{$sections})) {
1.412     raeburn  5216:                     $secmatch = 1;
                   5217:                 } elsif ($$classlist{$student}[$idx{section}] eq '') {
1.420     albertel 5218:                     if (grep(/^none$/,@{$sections})) {
1.412     raeburn  5219:                         $secmatch = 1;
                   5220:                     }
                   5221:                 } else {  
1.419     raeburn  5222: 		    if (grep(/^\Q$section\E$/,@{$sections})) {
1.412     raeburn  5223: 		        $secmatch = 1;
                   5224:                     }
1.290     albertel 5225: 		}
1.412     raeburn  5226:                 if (!$secmatch) {
                   5227:                     next;
                   5228:                 }
1.419     raeburn  5229:             }
1.420     albertel 5230:             push(@{$seclists{$student}},$section); 
1.275     raeburn  5231:             if (defined($$types{'active'})) {
1.288     raeburn  5232:                 if ($$classlist{$student}[$idx{status}] eq 'Active') {
1.275     raeburn  5233:                     push(@{$$users{st}{$student}},'active');
1.288     raeburn  5234:                     $match = 1;
1.275     raeburn  5235:                 }
                   5236:             }
                   5237:             if (defined($$types{'previous'})) {
1.288     raeburn  5238:                 if ($$classlist{$student}[$idx{end}] <= $now) {
1.275     raeburn  5239:                     push(@{$$users{st}{$student}},'previous');
1.288     raeburn  5240:                     $match = 1;
1.275     raeburn  5241:                 }
                   5242:             }
                   5243:             if (defined($$types{'future'})) {
1.288     raeburn  5244:                 if (($$classlist{$student}[$idx{start}] > $now) && ($$classlist{$student}[$idx{end}] > $now) || ($$classlist{$student}[$idx{end}] == 0) || ($$classlist{$student}[$idx{end}] eq '')) {
1.275     raeburn  5245:                     push(@{$$users{st}{$student}},'future');
1.288     raeburn  5246:                     $match = 1;
1.275     raeburn  5247:                 }
                   5248:             }
1.412     raeburn  5249:             if ($match && ref($userdata) eq 'HASH') {
1.288     raeburn  5250:                 $$userdata{$student} = $$classlist{$student};
                   5251:             }
1.275     raeburn  5252:         }
                   5253:     }
1.412     raeburn  5254:     if ((@{$roles} > 1) || ((@{$roles} == 1) && ($$roles[0] ne "st"))) {
1.439     raeburn  5255:         my %coursepersonnel = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   5256:         my $now = time;
                   5257:         foreach my $person (sort(keys(%coursepersonnel))) {
1.288     raeburn  5258:             my $match = 0;
1.412     raeburn  5259:             my $secmatch = 0;
1.439     raeburn  5260:             my $status;
1.412     raeburn  5261:             my ($role,$user,$usec) = ($person =~ /^([^:]*):([^:]+:[^:]+):([^:]*)/);
1.275     raeburn  5262:             $user =~ s/:$//;
1.439     raeburn  5263:             my ($end,$start) = split(/:/,$coursepersonnel{$person});
                   5264:             if ($end == -1 || $start == -1) {
                   5265:                 next;
                   5266:             }
                   5267:             if (($role) && ((grep(/^\Q$role\E$/,@{$roles})) ||
                   5268:                 (grep(/^cr$/,@{$roles}) && $role =~ /^cr\//))) {
1.412     raeburn  5269:                 my ($uname,$udom) = split(/:/,$user);
                   5270:                 if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 5271:                     if (grep(/^all$/,@{$sections})) {
1.412     raeburn  5272:                         $secmatch = 1;
                   5273:                     } elsif ($usec eq '') {
1.420     albertel 5274:                         if (grep(/^none$/,@{$sections})) {
1.412     raeburn  5275:                             $secmatch = 1;
                   5276:                         }
                   5277:                     } else {
                   5278:                         if (grep(/^\Q$usec\E$/,@{$sections})) {
                   5279:                             $secmatch = 1;
                   5280:                         }
                   5281:                     }
                   5282:                     if (!$secmatch) {
                   5283:                         next;
                   5284:                     }
1.288     raeburn  5285:                 }
1.419     raeburn  5286:                 if ($usec eq '') {
                   5287:                     $usec = 'none';
                   5288:                 }
1.275     raeburn  5289:                 if ($uname ne '' && $udom ne '') {
1.503     raeburn  5290:                     if ($end > 0 && $end < $now) {
1.439     raeburn  5291:                         $status = 'previous';
                   5292:                     } elsif ($start > $now) {
                   5293:                         $status = 'future';
                   5294:                     } else {
                   5295:                         $status = 'active';
                   5296:                     }
1.277     albertel 5297:                     foreach my $type (keys(%{$types})) { 
1.275     raeburn  5298:                         if ($status eq $type) {
1.420     albertel 5299:                             if (!grep(/^\Q$type\E$/,@{$$users{$role}{$user}})) {
1.419     raeburn  5300:                                 push(@{$$users{$role}{$user}},$type);
                   5301:                             }
1.288     raeburn  5302:                             $match = 1;
                   5303:                         }
                   5304:                     }
1.419     raeburn  5305:                     if (($match) && (ref($userdata) eq 'HASH')) {
                   5306:                         if (!exists($$userdata{$uname.':'.$udom})) {
                   5307: 			    &get_user_info($udom,$uname,\%idx,$userdata);
                   5308:                         }
1.420     albertel 5309:                         if (!grep(/^\Q$usec\E$/,@{$seclists{$uname.':'.$udom}})) {
1.419     raeburn  5310:                             push(@{$seclists{$uname.':'.$udom}},$usec);
                   5311:                         }
1.275     raeburn  5312:                     }
                   5313:                 }
                   5314:             }
                   5315:         }
1.290     albertel 5316:         if (grep(/^ow$/,@{$roles})) {
1.279     raeburn  5317:             if ((defined($cdom)) && (defined($cnum))) {
                   5318:                 my %csettings = &Apache::lonnet::get('environment',['internal.courseowner'],$cdom,$cnum);
                   5319:                 if ( defined($csettings{'internal.courseowner'}) ) {
                   5320:                     my $owner = $csettings{'internal.courseowner'};
1.439     raeburn  5321:                     if ($owner !~ /^[^:]+:[^:]+$/) {
                   5322:                         $owner = $owner.':'.$cdom;
                   5323:                     }
                   5324:                     @{$$users{'ow'}{$owner}} = 'any';
1.290     albertel 5325:                     if (defined($userdata) && 
                   5326: 			!exists($$userdata{$owner.':'.$cdom})) {
                   5327: 			&get_user_info($cdom,$owner,\%idx,$userdata);
1.420     albertel 5328:                         if (!grep(/^none$/,@{$seclists{$owner.':'.$cdom}})) {
1.419     raeburn  5329:                             push(@{$seclists{$owner.':'.$cdom}},'none');
                   5330:                         }
1.290     albertel 5331: 		    }
1.279     raeburn  5332:                 }
                   5333:             }
                   5334:         }
1.419     raeburn  5335:         foreach my $user (keys(%seclists)) {
                   5336:             @{$seclists{$user}} = (sort {$a <=> $b} @{$seclists{$user}});
                   5337:             $$userdata{$user}[$idx{section}] = join(',',@{$seclists{$user}});
                   5338:         }
1.275     raeburn  5339:     }
                   5340:     return;
                   5341: }
                   5342: 
1.288     raeburn  5343: sub get_user_info {
                   5344:     my ($udom,$uname,$idx,$userdata) = @_;
1.289     albertel 5345:     $$userdata{$uname.':'.$udom}[$$idx{fullname}] = 
                   5346: 	&plainname($uname,$udom,'lastname');
1.291     albertel 5347:     $$userdata{$uname.':'.$udom}[$$idx{uname}] = $uname;
1.297     raeburn  5348:     $$userdata{$uname.':'.$udom}[$$idx{udom}] = $udom;
1.288     raeburn  5349:     return;
                   5350: }
1.275     raeburn  5351: 
1.472     raeburn  5352: ###############################################
                   5353: 
                   5354: =pod
                   5355: 
                   5356: =item * &get_user_quota()
                   5357: 
                   5358: Retrieves quota assigned for storage of portfolio files for a user  
                   5359: 
                   5360: Incoming parameters:
                   5361: 1. user's username
                   5362: 2. user's domain
                   5363: 
                   5364: Returns:
1.536     raeburn  5365: 1. Disk quota (in Mb) assigned to student.
                   5366: 2. (Optional) Type of setting: custom or default
                   5367:    (individually assigned or default for user's 
                   5368:    institutional status).
                   5369: 3. (Optional) - User's institutional status (e.g., faculty, staff
                   5370:    or student - types as defined in localenroll::inst_usertypes 
                   5371:    for user's domain, which determines default quota for user.
                   5372: 4. (Optional) - Default quota which would apply to the user.
1.472     raeburn  5373: 
                   5374: If a value has been stored in the user's environment, 
1.536     raeburn  5375: it will return that, otherwise it returns the maximal default
                   5376: defined for the user's instituional status(es) in the domain.
1.472     raeburn  5377: 
                   5378: =cut
                   5379: 
                   5380: ###############################################
                   5381: 
                   5382: 
                   5383: sub get_user_quota {
                   5384:     my ($uname,$udom) = @_;
1.536     raeburn  5385:     my ($quota,$quotatype,$settingstatus,$defquota);
1.472     raeburn  5386:     if (!defined($udom)) {
                   5387:         $udom = $env{'user.domain'};
                   5388:     }
                   5389:     if (!defined($uname)) {
                   5390:         $uname = $env{'user.name'};
                   5391:     }
                   5392:     if (($udom eq '' || $uname eq '') ||
                   5393:         ($udom eq 'public') && ($uname eq 'public')) {
                   5394:         $quota = 0;
1.536     raeburn  5395:         $quotatype = 'default';
                   5396:         $defquota = 0; 
1.472     raeburn  5397:     } else {
1.536     raeburn  5398:         my $inststatus;
1.472     raeburn  5399:         if ($udom eq $env{'user.domain'} && $uname eq $env{'user.name'}) {
                   5400:             $quota = $env{'environment.portfolioquota'};
1.536     raeburn  5401:             $inststatus = $env{'environment.inststatus'};
1.472     raeburn  5402:         } else {
1.536     raeburn  5403:             my %userenv = 
                   5404:                 &Apache::lonnet::get('environment',['portfolioquota',
                   5405:                                      'inststatus'],$udom,$uname);
1.472     raeburn  5406:             my ($tmp) = keys(%userenv);
                   5407:             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   5408:                 $quota = $userenv{'portfolioquota'};
1.536     raeburn  5409:                 $inststatus = $userenv{'inststatus'};
1.472     raeburn  5410:             } else {
                   5411:                 undef(%userenv);
                   5412:             }
                   5413:         }
1.536     raeburn  5414:         ($defquota,$settingstatus) = &default_quota($udom,$inststatus);
1.472     raeburn  5415:         if ($quota eq '') {
1.536     raeburn  5416:             $quota = $defquota;
                   5417:             $quotatype = 'default';
                   5418:         } else {
                   5419:             $quotatype = 'custom';
1.472     raeburn  5420:         }
                   5421:     }
1.536     raeburn  5422:     if (wantarray) {
                   5423:         return ($quota,$quotatype,$settingstatus,$defquota);
                   5424:     } else {
                   5425:         return $quota;
                   5426:     }
1.472     raeburn  5427: }
                   5428: 
                   5429: ###############################################
                   5430: 
                   5431: =pod
                   5432: 
                   5433: =item * &default_quota()
                   5434: 
1.536     raeburn  5435: Retrieves default quota assigned for storage of user portfolio files,
                   5436: given an (optional) user's institutional status.
1.472     raeburn  5437: 
                   5438: Incoming parameters:
                   5439: 1. domain
1.536     raeburn  5440: 2. (Optional) institutional status(es).  This is a : separated list of 
                   5441:    status types (e.g., faculty, staff, student etc.)
                   5442:    which apply to the user for whom the default is being retrieved.
                   5443:    If the institutional status string in undefined, the domain
                   5444:    default quota will be returned. 
1.472     raeburn  5445: 
                   5446: Returns:
                   5447: 1. Default disk quota (in Mb) for user portfolios in the domain.
1.536     raeburn  5448: 2. (Optional) institutional type which determined the value of the
                   5449:    default quota.
1.472     raeburn  5450: 
                   5451: If a value has been stored in the domain's configuration db,
                   5452: it will return that, otherwise it returns 20 (for backwards 
                   5453: compatibility with domains which have not set up a configuration
                   5454: db file; the original statically defined portfolio quota was 20 Mb). 
                   5455: 
1.536     raeburn  5456: If the user's status includes multiple types (e.g., staff and student),
                   5457: the largest default quota which applies to the user determines the
                   5458: default quota returned.
                   5459: 
1.472     raeburn  5460: =cut
                   5461: 
                   5462: ###############################################
                   5463: 
                   5464: 
                   5465: sub default_quota {
1.536     raeburn  5466:     my ($udom,$inststatus) = @_;
                   5467:     my ($defquota,$settingstatus);
                   5468:     my %quotahash = &Apache::lonnet::get_dom('configuration',
                   5469:                                             ['quota'],$udom);
                   5470:     if (ref($quotahash{'quota'}) eq 'HASH') {
                   5471:         if ($inststatus ne '') {
                   5472:             my @statuses = split(/:/,$inststatus);
                   5473:             foreach my $item (@statuses) {
                   5474:                 if ($quotahash{'quota'}{$item} ne '') {
                   5475:                     if ($defquota eq '') {
                   5476:                         $defquota = $quotahash{'quota'}{$item};
                   5477:                         $settingstatus = $item;
                   5478:                     } elsif ($quotahash{'quota'}{$item} > $defquota) {
                   5479:                         $defquota = $quotahash{'quota'}{$item};
                   5480:                         $settingstatus = $item;
                   5481:                     }
                   5482:                 }
                   5483:             }
                   5484:         }
                   5485:         if ($defquota eq '') {
                   5486:             $defquota = $quotahash{'quota'}{'default'};
                   5487:             $settingstatus = 'default';
                   5488:         }
                   5489:     } else {
                   5490:         $settingstatus = 'default';
                   5491:         $defquota = 20;
                   5492:     }
                   5493:     if (wantarray) {
                   5494:         return ($defquota,$settingstatus);
1.472     raeburn  5495:     } else {
1.536     raeburn  5496:         return $defquota;
1.472     raeburn  5497:     }
                   5498: }
                   5499: 
1.384     raeburn  5500: sub get_secgrprole_info {
                   5501:     my ($cdom,$cnum,$needroles,$type)  = @_;
                   5502:     my %sections_count = &get_sections($cdom,$cnum);
                   5503:     my @sections =  (sort {$a <=> $b} keys(%sections_count));
                   5504:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
                   5505:     my @groups = sort(keys(%curr_groups));
                   5506:     my $allroles = [];
                   5507:     my $rolehash;
                   5508:     my $accesshash = {
                   5509:                      active => 'Currently has access',
                   5510:                      future => 'Will have future access',
                   5511:                      previous => 'Previously had access',
                   5512:                   };
                   5513:     if ($needroles) {
                   5514:         $rolehash = {'all' => 'all'};
1.385     albertel 5515:         my %user_roles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   5516: 	if (&Apache::lonnet::error(%user_roles)) {
                   5517: 	    undef(%user_roles);
                   5518: 	}
                   5519:         foreach my $item (keys(%user_roles)) {
1.384     raeburn  5520:             my ($role)=split(/\:/,$item,2);
                   5521:             if ($role eq 'cr') { next; }
                   5522:             if ($role =~ /^cr/) {
                   5523:                 $$rolehash{$role} = (split('/',$role))[3];
                   5524:             } else {
                   5525:                 $$rolehash{$role} = &Apache::lonnet::plaintext($role,$type);
                   5526:             }
                   5527:         }
                   5528:         foreach my $key (sort(keys(%{$rolehash}))) {
                   5529:             push(@{$allroles},$key);
                   5530:         }
                   5531:         push (@{$allroles},'st');
                   5532:         $$rolehash{'st'} = &Apache::lonnet::plaintext('st',$type);
                   5533:     }
                   5534:     return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
                   5535: }
                   5536: 
1.555     raeburn  5537: sub user_picker {
                   5538:     my ($dom,$srch,$forcenewuser) = @_;
                   5539:     my $currdom = $dom;
                   5540:     my %curr_selected = (
                   5541:                         srchin => 'dom',
                   5542:                         srchby => 'uname',
                   5543:                       );
                   5544:     my $srchterm;
                   5545:     if (ref($srch) eq 'HASH') {
                   5546:         if ($srch->{'srchby'} ne '') {
                   5547:             $curr_selected{'srchby'} = $srch->{'srchby'};
                   5548:         }
                   5549:         if ($srch->{'srchin'} ne '') {
                   5550:             $curr_selected{'srchin'} = $srch->{'srchin'};
                   5551:         }
                   5552:         if ($srch->{'srchtype'} ne '') {
                   5553:             $curr_selected{'srchtype'} = $srch->{'srchtype'};
                   5554:         }
                   5555:         if ($srch->{'srchdomain'} ne '') {
                   5556:             $currdom = $srch->{'srchdomain'};
                   5557:         }
                   5558:         $srchterm = $srch->{'srchterm'};
                   5559:     }
                   5560:     my %lt=&Apache::lonlocal::texthash(
1.558     albertel 5561:                     'usr'       => 'Search for',
                   5562:                     'or'        => 'or',
                   5563:                     'doma'      => 'domain',
                   5564:                     'uname'     => 'username',
                   5565:                     'lastname'  => 'last name',
1.555     raeburn  5566:                     'lastfirst' => 'last name, first name',
1.558     albertel 5567:                     'crs'       => 'in this course',
                   5568:                     'dom'       => 'in this domain', 
                   5569:                     'alc'       => 'all LON-CAPA',
                   5570:                     'instd'     => 'in institutional directory',
                   5571:                     'exact'     => 'is',
                   5572:                     'contains'  => 'contains',
1.555     raeburn  5573:                                        );
                   5574:     my $domform = &select_dom_form($currdom,'srchdomain',1);
                   5575: 
1.558     albertel 5576:     my $srchin;
1.555     raeburn  5577: 
                   5578:     my @srchins = ('crs','dom','alc','instd');
                   5579: 
                   5580:     foreach my $option (@srchins) {
                   5581:         # FIXME 'alc' option unavailable until 
                   5582:         #       loncreateuser::print_user_query_page()
                   5583:         #       has been completed.
                   5584:         next if ($option eq 'alc');
                   5585:         next if ($option eq 'crs' && !$env{'request.course.id'});
1.558     albertel 5586:         my $checked =($curr_selected{'srchin'} eq $option) ?'checked="checked"'
                   5587: 	                                                   :'';
                   5588: 	my $extra = ($option eq 'dom') ? $domform
                   5589: 	                               : '';
                   5590: 	$srchin.=<<ROW
                   5591:  <tr>
                   5592:    <td>
                   5593:      <label><input type="radio" name="srchin" value="$option" $checked /> $lt{$option}</label> $extra
                   5594:    </td>
                   5595: </tr>
                   5596: ROW
1.555     raeburn  5597:     }
                   5598: 
                   5599:     my $srchbysel =  ' <select name="srchby">';
                   5600:     foreach my $option ('uname','lastname','lastfirst') {
                   5601:         if ($curr_selected{'srchby'} eq $option) {
                   5602:             $srchbysel .= '
                   5603:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   5604:         } else {
                   5605:             $srchbysel .= '
                   5606:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   5607:          }
                   5608:     }
                   5609:     $srchbysel .= "\n  </select>\n";
                   5610: 
                   5611:     my $srchtypesel = ' <select name="srchtype">';
                   5612:     foreach my $option ('exact','contains') {
                   5613:         if ($curr_selected{'srchtype'} eq $option) {
                   5614:             $srchtypesel .= '
                   5615:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   5616:         } else {
                   5617:             $srchtypesel .= '
                   5618:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   5619:         }
                   5620:     }
                   5621:     $srchtypesel .= "\n  </select>\n";
                   5622: 
1.558     albertel 5623:     my ($newuserscript,$new_user_create);
1.556     raeburn  5624: 
                   5625:     if ($forcenewuser) {
1.558     albertel 5626: 	$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  5627:         $newuserscript = <<"ENDSCRIPT";
                   5628: 
1.558     albertel 5629: function setSearch(createnew) {
1.556     raeburn  5630:     if (createnew == 1) {
                   5631:         for (var i=0; i<document.crtuser.srchby.length; i++) {
                   5632:             if (document.crtuser.srchby.options[i].value == 'uname') {
                   5633:                 document.crtuser.srchby.selectedIndex = i;
                   5634:             }
                   5635:         }
                   5636:         for (var i=0; i<document.crtuser.srchin.length; i++) {
1.558     albertel 5637:             if ( document.crtuser.srchin[i].value == 'dom') {
                   5638: 		document.crtuser.srchin[i].checked = 1;
1.556     raeburn  5639:             }
                   5640:         }
                   5641:         for (var i=0; i<document.crtuser.srchtype.length; i++) {
                   5642:             if (document.crtuser.srchtype.options[i].value == 'exact') {
                   5643:                 document.crtuser.srchtype.selectedIndex = i;
                   5644:             }
                   5645:         }
                   5646:         for (var i=0; i<document.crtuser.srchdomain.length; i++) {
                   5647:             if (document.crtuser.srchdomain.options[i].value == '$env{'request.role.domain'}') {
                   5648:                 document.crtuser.srchdomain.selectedIndex = i;
                   5649:             }
                   5650:         }
                   5651:     }
                   5652: }
                   5653: ENDSCRIPT
1.558     albertel 5654: 
1.556     raeburn  5655:     }
                   5656: 
1.555     raeburn  5657:     my $output = <<"END_BLOCK";
1.556     raeburn  5658: <script type="text/javascript">
                   5659: function validateEntry() {
1.558     albertel 5660: 
1.556     raeburn  5661:     var checkok = 1;
1.558     albertel 5662:     var srchin;
                   5663:     for (var i=0; i<document.crtuser.srchin.length; i++) {
                   5664: 	if ( document.crtuser.srchin[i].checked ) {
                   5665: 	    srchin = document.crtuser.srchin[i].value;
                   5666: 	}
                   5667:     }
                   5668: 
1.556     raeburn  5669:     var srchtype = document.crtuser.srchtype.options[document.crtuser.srchtype.selectedIndex].value;
                   5670:     var srchby = document.crtuser.srchby.options[document.crtuser.srchby.selectedIndex].value;
                   5671:     var srchdomain = document.crtuser.srchdomain.options[document.crtuser.srchdomain.selectedIndex].value;
                   5672:     var srchterm =  document.crtuser.srchterm.value;
                   5673:     var msg = "";
                   5674: 
                   5675:     if (srchterm == "") {
                   5676:         checkok = 0;
                   5677:         msg += "You must include some text to search for.\\n";
                   5678:     }
                   5679: 
                   5680:     if (srchtype== 'contains') {
                   5681:         if (srchterm.length < 3) {
                   5682:             checkok = 0;
                   5683:             msg += "The text you are searching for must contain at least three characters when using a 'contained in' type search.\\n";
                   5684:         }
                   5685:     }
                   5686:     if (srchin == 'instd') {
                   5687:         if (srchdomain == '') {
                   5688:             checkok = 0;
                   5689:             msg += "You must choose a domain when using an institutional directory search.\\n";
                   5690:         }
                   5691:     }
                   5692:     if (srchin == 'dom') {
                   5693:         if (srchdomain == '') {
                   5694:             checkok = 0;
                   5695:             msg += "You must choose a domain when using a domain search.\\n";
                   5696:         }
                   5697:     }
                   5698:     if (srchby == 'lastfirst') {
                   5699:         if (srchterm.indexOf(",") == -1) {
                   5700:             checkok = 0;
                   5701:             msg += "When using searching by last,first you must include a comma as separator between last name and first name.\\n";
                   5702:         }
                   5703:         if (srchterm.indexOf(",") == srchterm.length -1) {
                   5704:             checkok = 0;
                   5705:             msg += "When searching by last,first you must include at least one character in the first name.\\n";
                   5706:         }
                   5707:     }
                   5708:     if (checkok == 0) {
                   5709:         alert("The following need to be corrected before the search can be run:\\n"+msg);
                   5710:         return;
                   5711:     }
                   5712:     if (checkok == 1) {
                   5713:         document.crtuser.submit();
                   5714:     }
                   5715: }
                   5716: 
                   5717: $newuserscript
                   5718: 
                   5719: </script>
1.558     albertel 5720: 
                   5721: $new_user_create
                   5722: 
1.555     raeburn  5723: <table>
1.558     albertel 5724:  <tr>
                   5725:   <td> $srchbysel
                   5726:        $srchtypesel 
                   5727:        <input type="text" size="15" name="srchterm" value="$srchterm" />
                   5728:   </td>
                   5729:  </tr>
                   5730: $srchin
1.555     raeburn  5731: </table>
                   5732: <br />
                   5733: END_BLOCK
1.558     albertel 5734: 
1.555     raeburn  5735:     return $output;
                   5736: }
                   5737: 
1.556     raeburn  5738: 
                   5739: 
1.112     bowersj2 5740: =pod
                   5741: 
1.549     albertel 5742: =back
                   5743: 
                   5744: =head1 HTTP Helpers
                   5745: 
                   5746: =over 4
                   5747: 
1.112     bowersj2 5748: =item * get_unprocessed_cgi($query,$possible_names)
                   5749: 
1.258     albertel 5750: Modify the %env hash to contain unprocessed CGI form parameters held in
1.112     bowersj2 5751: $query.  The parameters listed in $possible_names (an array reference),
1.258     albertel 5752: will be set in $env{'form.name'} if they do not already exist.
1.112     bowersj2 5753: 
                   5754: Typically called with $ENV{'QUERY_STRING'} as the first parameter.  
                   5755: $possible_names is an ref to an array of form element names.  As an example:
                   5756: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
1.258     albertel 5757: will result in $env{'form.uname'} and $env{'form.udom'} being set.
1.112     bowersj2 5758: 
                   5759: =cut
1.1       albertel 5760: 
1.6       albertel 5761: sub get_unprocessed_cgi {
1.25      albertel 5762:   my ($query,$possible_names)= @_;
1.26      matthew  5763:   # $Apache::lonxml::debug=1;
1.356     albertel 5764:   foreach my $pair (split(/&/,$query)) {
                   5765:     my ($name, $value) = split(/=/,$pair);
1.369     www      5766:     $name = &unescape($name);
1.25      albertel 5767:     if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
                   5768:       $value =~ tr/+/ /;
                   5769:       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.258     albertel 5770:       unless (defined($env{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
1.25      albertel 5771:     }
1.16      harris41 5772:   }
1.6       albertel 5773: }
                   5774: 
1.112     bowersj2 5775: =pod
                   5776: 
                   5777: =item * cacheheader() 
                   5778: 
                   5779: returns cache-controlling header code
                   5780: 
                   5781: =cut
                   5782: 
1.7       albertel 5783: sub cacheheader {
1.258     albertel 5784:     unless ($env{'request.method'} eq 'GET') { return ''; }
1.216     albertel 5785:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
                   5786:     my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
1.7       albertel 5787:                 <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
                   5788:                 <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
1.216     albertel 5789:     return $output;
1.7       albertel 5790: }
                   5791: 
1.112     bowersj2 5792: =pod
                   5793: 
                   5794: =item * no_cache($r) 
                   5795: 
                   5796: specifies header code to not have cache
                   5797: 
                   5798: =cut
                   5799: 
1.9       albertel 5800: sub no_cache {
1.216     albertel 5801:     my ($r) = @_;
                   5802:     if ($ENV{'REQUEST_METHOD'} ne 'GET' &&
1.258     albertel 5803: 	$env{'request.method'} ne 'GET') { return ''; }
1.216     albertel 5804:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time));
                   5805:     $r->no_cache(1);
                   5806:     $r->header_out("Expires" => $date);
                   5807:     $r->header_out("Pragma" => "no-cache");
1.123     www      5808: }
                   5809: 
                   5810: sub content_type {
1.181     albertel 5811:     my ($r,$type,$charset) = @_;
1.299     foxr     5812:     if ($r) {
                   5813: 	#  Note that printout.pl calls this with undef for $r.
                   5814: 	&no_cache($r);
                   5815:     }
1.258     albertel 5816:     if ($env{'browser.mathml'} && $type eq 'text/html') { $type='text/xml'; }
1.181     albertel 5817:     unless ($charset) {
                   5818: 	$charset=&Apache::lonlocal::current_encoding;
                   5819:     }
                   5820:     if ($charset) { $type.='; charset='.$charset; }
                   5821:     if ($r) {
                   5822: 	$r->content_type($type);
                   5823:     } else {
                   5824: 	print("Content-type: $type\n\n");
                   5825:     }
1.9       albertel 5826: }
1.25      albertel 5827: 
1.112     bowersj2 5828: =pod
                   5829: 
                   5830: =item * add_to_env($name,$value) 
                   5831: 
1.258     albertel 5832: adds $name to the %env hash with value
1.112     bowersj2 5833: $value, if $name already exists, the entry is converted to an array
                   5834: reference and $value is added to the array.
                   5835: 
                   5836: =cut
                   5837: 
1.25      albertel 5838: sub add_to_env {
                   5839:   my ($name,$value)=@_;
1.258     albertel 5840:   if (defined($env{$name})) {
                   5841:     if (ref($env{$name})) {
1.25      albertel 5842:       #already have multiple values
1.258     albertel 5843:       push(@{ $env{$name} },$value);
1.25      albertel 5844:     } else {
                   5845:       #first time seeing multiple values, convert hash entry to an arrayref
1.258     albertel 5846:       my $first=$env{$name};
                   5847:       undef($env{$name});
                   5848:       push(@{ $env{$name} },$first,$value);
1.25      albertel 5849:     }
                   5850:   } else {
1.258     albertel 5851:     $env{$name}=$value;
1.25      albertel 5852:   }
1.31      albertel 5853: }
1.149     albertel 5854: 
                   5855: =pod
                   5856: 
                   5857: =item * get_env_multiple($name) 
                   5858: 
1.258     albertel 5859: gets $name from the %env hash, it seemlessly handles the cases where multiple
1.149     albertel 5860: values may be defined and end up as an array ref.
                   5861: 
                   5862: returns an array of values
                   5863: 
                   5864: =cut
                   5865: 
                   5866: sub get_env_multiple {
                   5867:     my ($name) = @_;
                   5868:     my @values;
1.258     albertel 5869:     if (defined($env{$name})) {
1.149     albertel 5870:         # exists is it an array
1.258     albertel 5871:         if (ref($env{$name})) {
                   5872:             @values=@{ $env{$name} };
1.149     albertel 5873:         } else {
1.258     albertel 5874:             $values[0]=$env{$name};
1.149     albertel 5875:         }
                   5876:     }
                   5877:     return(@values);
                   5878: }
                   5879: 
1.31      albertel 5880: 
1.41      ng       5881: =pod
1.45      matthew  5882: 
1.464     albertel 5883: =back
1.41      ng       5884: 
1.112     bowersj2 5885: =head1 CSV Upload/Handling functions
1.38      albertel 5886: 
1.41      ng       5887: =over 4
                   5888: 
1.112     bowersj2 5889: =item * upfile_store($r)
1.41      ng       5890: 
                   5891: Store uploaded file, $r should be the HTTP Request object,
1.258     albertel 5892: needs $env{'form.upfile'}
1.41      ng       5893: returns $datatoken to be put into hidden field
                   5894: 
                   5895: =cut
1.31      albertel 5896: 
                   5897: sub upfile_store {
                   5898:     my $r=shift;
1.258     albertel 5899:     $env{'form.upfile'}=~s/\r/\n/gs;
                   5900:     $env{'form.upfile'}=~s/\f/\n/gs;
                   5901:     $env{'form.upfile'}=~s/\n+/\n/gs;
                   5902:     $env{'form.upfile'}=~s/\n+$//gs;
1.31      albertel 5903: 
1.258     albertel 5904:     my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
                   5905: 	'_enroll_'.$env{'request.course.id'}.'_'.time.'_'.$$;
1.31      albertel 5906:     {
1.158     raeburn  5907:         my $datafile = $r->dir_config('lonDaemons').
                   5908:                            '/tmp/'.$datatoken.'.tmp';
                   5909:         if ( open(my $fh,">$datafile") ) {
1.258     albertel 5910:             print $fh $env{'form.upfile'};
1.158     raeburn  5911:             close($fh);
                   5912:         }
1.31      albertel 5913:     }
                   5914:     return $datatoken;
                   5915: }
                   5916: 
1.56      matthew  5917: =pod
                   5918: 
1.112     bowersj2 5919: =item * load_tmp_file($r)
1.41      ng       5920: 
                   5921: Load uploaded file from tmp, $r should be the HTTP Request object,
1.258     albertel 5922: needs $env{'form.datatoken'},
                   5923: sets $env{'form.upfile'} to the contents of the file
1.41      ng       5924: 
                   5925: =cut
1.31      albertel 5926: 
                   5927: sub load_tmp_file {
                   5928:     my $r=shift;
                   5929:     my @studentdata=();
                   5930:     {
1.158     raeburn  5931:         my $studentfile = $r->dir_config('lonDaemons').
1.258     albertel 5932:                               '/tmp/'.$env{'form.datatoken'}.'.tmp';
1.158     raeburn  5933:         if ( open(my $fh,"<$studentfile") ) {
                   5934:             @studentdata=<$fh>;
                   5935:             close($fh);
                   5936:         }
1.31      albertel 5937:     }
1.258     albertel 5938:     $env{'form.upfile'}=join('',@studentdata);
1.31      albertel 5939: }
                   5940: 
1.56      matthew  5941: =pod
                   5942: 
1.112     bowersj2 5943: =item * upfile_record_sep()
1.41      ng       5944: 
                   5945: Separate uploaded file into records
                   5946: returns array of records,
1.258     albertel 5947: needs $env{'form.upfile'} and $env{'form.upfiletype'}
1.41      ng       5948: 
                   5949: =cut
1.31      albertel 5950: 
                   5951: sub upfile_record_sep {
1.258     albertel 5952:     if ($env{'form.upfiletype'} eq 'xml') {
1.31      albertel 5953:     } else {
1.248     albertel 5954: 	my @records;
1.258     albertel 5955: 	foreach my $line (split(/\n/,$env{'form.upfile'})) {
1.248     albertel 5956: 	    if ($line=~/^\s*$/) { next; }
                   5957: 	    push(@records,$line);
                   5958: 	}
                   5959: 	return @records;
1.31      albertel 5960:     }
                   5961: }
                   5962: 
1.56      matthew  5963: =pod
                   5964: 
1.112     bowersj2 5965: =item * record_sep($record)
1.41      ng       5966: 
1.258     albertel 5967: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $env{'form.upfiletype'}
1.41      ng       5968: 
                   5969: =cut
                   5970: 
1.263     www      5971: sub takeleft {
                   5972:     my $index=shift;
                   5973:     return substr('0000'.$index,-4,4);
                   5974: }
                   5975: 
1.31      albertel 5976: sub record_sep {
                   5977:     my $record=shift;
                   5978:     my %components=();
1.258     albertel 5979:     if ($env{'form.upfiletype'} eq 'xml') {
                   5980:     } elsif ($env{'form.upfiletype'} eq 'space') {
1.31      albertel 5981:         my $i=0;
1.356     albertel 5982:         foreach my $field (split(/\s+/,$record)) {
1.31      albertel 5983:             $field=~s/^(\"|\')//;
                   5984:             $field=~s/(\"|\')$//;
1.263     www      5985:             $components{&takeleft($i)}=$field;
1.31      albertel 5986:             $i++;
                   5987:         }
1.258     albertel 5988:     } elsif ($env{'form.upfiletype'} eq 'tab') {
1.31      albertel 5989:         my $i=0;
1.356     albertel 5990:         foreach my $field (split(/\t/,$record)) {
1.31      albertel 5991:             $field=~s/^(\"|\')//;
                   5992:             $field=~s/(\"|\')$//;
1.263     www      5993:             $components{&takeleft($i)}=$field;
1.31      albertel 5994:             $i++;
                   5995:         }
                   5996:     } else {
1.561   ! www      5997:         my $separator=',';
1.480     banghart 5998:         if ($env{'form.upfiletype'} eq 'semisv') {
1.561   ! www      5999:             $separator=';';
1.480     banghart 6000:         }
1.31      albertel 6001:         my $i=0;
1.561   ! www      6002: # the character we are looking for to indicate the end of a quote or a record 
        !          6003:         my $looking_for=$separator;
        !          6004: # do not add the characters to the fields
        !          6005:         my $ignore=0;
        !          6006: # we just encountered a separator (or the beginning of the record)
        !          6007:         my $just_found_separator=1;
        !          6008: # store the field we are working on here
        !          6009:         my $field='';
        !          6010: # work our way through all characters in record
        !          6011:         foreach my $character ($record=~/(.)/g) {
        !          6012:             if ($character eq $looking_for) {
        !          6013:                if ($character ne $separator) {
        !          6014: # Found the end of a quote, again looking for separator
        !          6015:                   $looking_for=$separator;
        !          6016:                   $ignore=1;
        !          6017:                } else {
        !          6018: # Found a separator, store away what we got
        !          6019:                   $components{&takeleft($i)}=$field;
        !          6020: 	          $i++;
        !          6021:                   $just_found_separator=1;
        !          6022:                   $ignore=0;
        !          6023:                   $field='';
        !          6024:                }
        !          6025:                next;
        !          6026:             }
        !          6027: # single or double quotation marks after a separator indicate beginning of a quote
        !          6028: # we are now looking for the end of the quote and need to ignore separators
        !          6029:             if ((($character eq '"') || ($character eq "'")) && ($just_found_separator))  {
        !          6030:                $looking_for=$character;
        !          6031:                next;
        !          6032:             }
        !          6033: # ignore would be true after we reached the end of a quote
        !          6034:             if ($ignore) { next; }
        !          6035:             if (($just_found_separator) && ($character=~/\s/)) { next; }
        !          6036:             $field.=$character;
        !          6037:             $just_found_separator=0; 
1.31      albertel 6038:         }
1.561   ! www      6039: # catch the very last entry, since we never encountered the separator
        !          6040:         $components{&takeleft($i)}=$field;
1.31      albertel 6041:     }
                   6042:     return %components;
                   6043: }
                   6044: 
1.144     matthew  6045: ######################################################
                   6046: ######################################################
                   6047: 
1.56      matthew  6048: =pod
                   6049: 
1.112     bowersj2 6050: =item * upfile_select_html()
1.41      ng       6051: 
1.144     matthew  6052: Return HTML code to select a file from the users machine and specify 
                   6053: the file type.
1.41      ng       6054: 
                   6055: =cut
                   6056: 
1.144     matthew  6057: ######################################################
                   6058: ######################################################
1.31      albertel 6059: sub upfile_select_html {
1.144     matthew  6060:     my %Types = (
                   6061:                  csv   => &mt('CSV (comma separated values, spreadsheet)'),
1.480     banghart 6062:                  semisv => &mt('Semicolon separated values'),
1.144     matthew  6063:                  space => &mt('Space separated'),
                   6064:                  tab   => &mt('Tabulator separated'),
                   6065: #                 xml   => &mt('HTML/XML'),
                   6066:                  );
                   6067:     my $Str = '<input type="file" name="upfile" size="50" />'.
                   6068:         '<br />Type: <select name="upfiletype">';
                   6069:     foreach my $type (sort(keys(%Types))) {
                   6070:         $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
                   6071:     }
                   6072:     $Str .= "</select>\n";
                   6073:     return $Str;
1.31      albertel 6074: }
                   6075: 
1.301     albertel 6076: sub get_samples {
                   6077:     my ($records,$toget) = @_;
                   6078:     my @samples=({});
                   6079:     my $got=0;
                   6080:     foreach my $rec (@$records) {
                   6081: 	my %temp = &record_sep($rec);
                   6082: 	if (! grep(/\S/, values(%temp))) { next; }
                   6083: 	if (%temp) {
                   6084: 	    $samples[$got]=\%temp;
                   6085: 	    $got++;
                   6086: 	    if ($got == $toget) { last; }
                   6087: 	}
                   6088:     }
                   6089:     return \@samples;
                   6090: }
                   6091: 
1.144     matthew  6092: ######################################################
                   6093: ######################################################
                   6094: 
1.56      matthew  6095: =pod
                   6096: 
1.112     bowersj2 6097: =item * csv_print_samples($r,$records)
1.41      ng       6098: 
                   6099: Prints a table of sample values from each column uploaded $r is an
                   6100: Apache Request ref, $records is an arrayref from
                   6101: &Apache::loncommon::upfile_record_sep
                   6102: 
                   6103: =cut
                   6104: 
1.144     matthew  6105: ######################################################
                   6106: ######################################################
1.31      albertel 6107: sub csv_print_samples {
                   6108:     my ($r,$records) = @_;
1.301     albertel 6109:     my $samples = &get_samples($records,3);
                   6110: 
1.144     matthew  6111:     $r->print(&mt('Samples').'<br /><table border="2"><tr>');
1.356     albertel 6112:     foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) { 
                   6113:         $r->print('<th>'.&mt('Column&nbsp;[_1]',($sample+1)).'</th>'); }
1.31      albertel 6114:     $r->print('</tr>');
1.301     albertel 6115:     foreach my $hash (@$samples) {
1.31      albertel 6116: 	$r->print('<tr>');
1.356     albertel 6117: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
1.31      albertel 6118: 	    $r->print('<td>');
1.356     albertel 6119: 	    if (defined($$hash{$sample})) { $r->print($$hash{$sample}); }
1.31      albertel 6120: 	    $r->print('</td>');
                   6121: 	}
                   6122: 	$r->print('</tr>');
                   6123:     }
                   6124:     $r->print('</tr></table><br />'."\n");
                   6125: }
                   6126: 
1.144     matthew  6127: ######################################################
                   6128: ######################################################
                   6129: 
1.56      matthew  6130: =pod
                   6131: 
1.112     bowersj2 6132: =item * csv_print_select_table($r,$records,$d)
1.41      ng       6133: 
                   6134: Prints a table to create associations between values and table columns.
1.144     matthew  6135: 
1.41      ng       6136: $r is an Apache Request ref,
                   6137: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
1.174     matthew  6138: $d is an array of 2 element arrays (internal name, displayed name,defaultcol)
1.41      ng       6139: 
                   6140: =cut
                   6141: 
1.144     matthew  6142: ######################################################
                   6143: ######################################################
1.31      albertel 6144: sub csv_print_select_table {
                   6145:     my ($r,$records,$d) = @_;
1.301     albertel 6146:     my $i=0;
                   6147:     my $samples = &get_samples($records,1);
1.144     matthew  6148:     $r->print(&mt('Associate columns with student attributes.')."\n".
                   6149: 	     '<table border="2"><tr>'.
                   6150:               '<th>'.&mt('Attribute').'</th>'.
                   6151:               '<th>'.&mt('Column').'</th></tr>'."\n");
1.356     albertel 6152:     foreach my $array_ref (@$d) {
                   6153: 	my ($value,$display,$defaultcol)=@{ $array_ref };
1.31      albertel 6154: 	$r->print('<tr><td>'.$display.'</td>');
                   6155: 
                   6156: 	$r->print('<td><select name=f'.$i.
1.32      matthew  6157: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.31      albertel 6158: 	$r->print('<option value="none"></option>');
1.356     albertel 6159: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
                   6160: 	    $r->print('<option value="'.$sample.'"'.
                   6161:                       ($sample eq $defaultcol ? ' selected="selected" ' : '').
                   6162:                       '>Column '.($sample+1).'</option>');
1.31      albertel 6163: 	}
                   6164: 	$r->print('</select></td></tr>'."\n");
                   6165: 	$i++;
                   6166:     }
                   6167:     $i--;
                   6168:     return $i;
                   6169: }
1.56      matthew  6170: 
1.144     matthew  6171: ######################################################
                   6172: ######################################################
                   6173: 
1.56      matthew  6174: =pod
1.31      albertel 6175: 
1.112     bowersj2 6176: =item * csv_samples_select_table($r,$records,$d)
1.41      ng       6177: 
                   6178: Prints a table of sample values from the upload and can make associate samples to internal names.
                   6179: 
                   6180: $r is an Apache Request ref,
                   6181: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
                   6182: $d is an array of 2 element arrays (internal name, displayed name)
                   6183: 
                   6184: =cut
                   6185: 
1.144     matthew  6186: ######################################################
                   6187: ######################################################
1.31      albertel 6188: sub csv_samples_select_table {
                   6189:     my ($r,$records,$d) = @_;
                   6190:     my $i=0;
1.144     matthew  6191:     #
1.301     albertel 6192:     my $samples = &get_samples($records,3);
1.144     matthew  6193:     $r->print('<table border=2><tr><th>'.
                   6194:               &mt('Field').'</th><th>'.&mt('Samples').'</th></tr>');
1.301     albertel 6195: 
                   6196:     foreach my $key (sort(keys(%{ $samples->[0] }))) {
1.144     matthew  6197: 	$r->print('<tr><td><select name="f'.$i.'"'.
1.32      matthew  6198: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.301     albertel 6199: 	foreach my $option (@$d) {
                   6200: 	    my ($value,$display,$defaultcol)=@{ $option };
1.174     matthew  6201: 	    $r->print('<option value="'.$value.'"'.
1.253     albertel 6202:                       ($i eq $defaultcol ? ' selected="selected" ':'').'>'.
1.174     matthew  6203:                       $display.'</option>');
1.31      albertel 6204: 	}
                   6205: 	$r->print('</select></td><td>');
1.301     albertel 6206: 	foreach my $line (0..2) {
                   6207: 	    if (defined($samples->[$line]{$key})) { 
                   6208: 		$r->print($samples->[$line]{$key}."<br />\n"); 
                   6209: 	    }
                   6210: 	}
1.31      albertel 6211: 	$r->print('</td></tr>');
                   6212: 	$i++;
                   6213:     }
                   6214:     $i--;
                   6215:     return($i);
1.115     matthew  6216: }
                   6217: 
1.144     matthew  6218: ######################################################
                   6219: ######################################################
                   6220: 
1.115     matthew  6221: =pod
                   6222: 
                   6223: =item clean_excel_name($name)
                   6224: 
                   6225: Returns a replacement for $name which does not contain any illegal characters.
                   6226: 
                   6227: =cut
                   6228: 
1.144     matthew  6229: ######################################################
                   6230: ######################################################
1.115     matthew  6231: sub clean_excel_name {
                   6232:     my ($name) = @_;
                   6233:     $name =~ s/[:\*\?\/\\]//g;
                   6234:     if (length($name) > 31) {
                   6235:         $name = substr($name,0,31);
                   6236:     }
                   6237:     return $name;
1.25      albertel 6238: }
1.84      albertel 6239: 
1.85      albertel 6240: =pod
                   6241: 
1.112     bowersj2 6242: =item * check_if_partid_hidden($id,$symb,$udom,$uname)
1.85      albertel 6243: 
                   6244: Returns either 1 or undef
                   6245: 
                   6246: 1 if the part is to be hidden, undef if it is to be shown
                   6247: 
                   6248: Arguments are:
                   6249: 
                   6250: $id the id of the part to be checked
                   6251: $symb, optional the symb of the resource to check
                   6252: $udom, optional the domain of the user to check for
                   6253: $uname, optional the username of the user to check for
                   6254: 
                   6255: =cut
1.84      albertel 6256: 
                   6257: sub check_if_partid_hidden {
                   6258:     my ($id,$symb,$udom,$uname) = @_;
1.133     albertel 6259:     my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
1.84      albertel 6260: 					 $symb,$udom,$uname);
1.141     albertel 6261:     my $truth=1;
                   6262:     #if the string starts with !, then the list is the list to show not hide
                   6263:     if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
1.84      albertel 6264:     my @hiddenlist=split(/,/,$hiddenparts);
                   6265:     foreach my $checkid (@hiddenlist) {
1.141     albertel 6266: 	if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
1.84      albertel 6267:     }
1.141     albertel 6268:     return !$truth;
1.84      albertel 6269: }
1.127     matthew  6270: 
1.138     matthew  6271: 
                   6272: ############################################################
                   6273: ############################################################
                   6274: 
                   6275: =pod
                   6276: 
1.157     matthew  6277: =back 
                   6278: 
1.138     matthew  6279: =head1 cgi-bin script and graphing routines
                   6280: 
1.157     matthew  6281: =over 4
                   6282: 
1.138     matthew  6283: =item get_cgi_id
                   6284: 
                   6285: Inputs: none
                   6286: 
                   6287: Returns an id which can be used to pass environment variables
                   6288: to various cgi-bin scripts.  These environment variables will
                   6289: be removed from the users environment after a given time by
                   6290: the routine &Apache::lonnet::transfer_profile_to_env.
                   6291: 
                   6292: =cut
                   6293: 
                   6294: ############################################################
                   6295: ############################################################
1.152     albertel 6296: my $uniq=0;
1.136     matthew  6297: sub get_cgi_id {
1.154     albertel 6298:     $uniq=($uniq+1)%100000;
1.280     albertel 6299:     return (time.'_'.$$.'_'.$uniq);
1.136     matthew  6300: }
                   6301: 
1.127     matthew  6302: ############################################################
                   6303: ############################################################
                   6304: 
                   6305: =pod
                   6306: 
1.134     matthew  6307: =item DrawBarGraph
1.127     matthew  6308: 
1.138     matthew  6309: Facilitates the plotting of data in a (stacked) bar graph.
                   6310: Puts plot definition data into the users environment in order for 
                   6311: graph.png to plot it.  Returns an <img> tag for the plot.
                   6312: The bars on the plot are labeled '1','2',...,'n'.
                   6313: 
                   6314: Inputs:
                   6315: 
                   6316: =over 4
                   6317: 
                   6318: =item $Title: string, the title of the plot
                   6319: 
                   6320: =item $xlabel: string, text describing the X-axis of the plot
                   6321: 
                   6322: =item $ylabel: string, text describing the Y-axis of the plot
                   6323: 
                   6324: =item $Max: scalar, the maximum Y value to use in the plot
                   6325: If $Max is < any data point, the graph will not be rendered.
                   6326: 
1.140     matthew  6327: =item $colors: array ref holding the colors to be used for the data sets when
1.138     matthew  6328: they are plotted.  If undefined, default values will be used.
                   6329: 
1.178     matthew  6330: =item $labels: array ref holding the labels to use on the x-axis for the bars.
                   6331: 
1.138     matthew  6332: =item @Values: An array of array references.  Each array reference holds data
                   6333: to be plotted in a stacked bar chart.
                   6334: 
1.239     matthew  6335: =item If the final element of @Values is a hash reference the key/value
                   6336: pairs will be added to the graph definition.
                   6337: 
1.138     matthew  6338: =back
                   6339: 
                   6340: Returns:
                   6341: 
                   6342: An <img> tag which references graph.png and the appropriate identifying
                   6343: information for the plot.
                   6344: 
1.127     matthew  6345: =cut
                   6346: 
                   6347: ############################################################
                   6348: ############################################################
1.134     matthew  6349: sub DrawBarGraph {
1.178     matthew  6350:     my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
1.134     matthew  6351:     #
                   6352:     if (! defined($colors)) {
                   6353:         $colors = ['#33ff00', 
                   6354:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
                   6355:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
                   6356:                   ]; 
                   6357:     }
1.228     matthew  6358:     my $extra_settings = {};
                   6359:     if (ref($Values[-1]) eq 'HASH') {
                   6360:         $extra_settings = pop(@Values);
                   6361:     }
1.127     matthew  6362:     #
1.136     matthew  6363:     my $identifier = &get_cgi_id();
                   6364:     my $id = 'cgi.'.$identifier;        
1.129     matthew  6365:     if (! @Values || ref($Values[0]) ne 'ARRAY') {
1.127     matthew  6366:         return '';
                   6367:     }
1.225     matthew  6368:     #
                   6369:     my @Labels;
                   6370:     if (defined($labels)) {
                   6371:         @Labels = @$labels;
                   6372:     } else {
                   6373:         for (my $i=0;$i<@{$Values[0]};$i++) {
                   6374:             push (@Labels,$i+1);
                   6375:         }
                   6376:     }
                   6377:     #
1.129     matthew  6378:     my $NumBars = scalar(@{$Values[0]});
1.225     matthew  6379:     if ($NumBars < scalar(@Labels)) { $NumBars = scalar(@Labels); }
1.129     matthew  6380:     my %ValuesHash;
                   6381:     my $NumSets=1;
                   6382:     foreach my $array (@Values) {
                   6383:         next if (! ref($array));
1.136     matthew  6384:         $ValuesHash{$id.'.data.'.$NumSets++} = 
1.132     matthew  6385:             join(',',@$array);
1.129     matthew  6386:     }
1.127     matthew  6387:     #
1.136     matthew  6388:     my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
1.225     matthew  6389:     if ($NumBars < 3) {
                   6390:         $width = 120+$NumBars*32;
1.220     matthew  6391:         $xskip = 1;
1.225     matthew  6392:         $bar_width = 30;
                   6393:     } elsif ($NumBars < 5) {
                   6394:         $width = 120+$NumBars*20;
                   6395:         $xskip = 1;
                   6396:         $bar_width = 20;
1.220     matthew  6397:     } elsif ($NumBars < 10) {
1.136     matthew  6398:         $width = 120+$NumBars*15;
                   6399:         $xskip = 1;
                   6400:         $bar_width = 15;
                   6401:     } elsif ($NumBars <= 25) {
                   6402:         $width = 120+$NumBars*11;
                   6403:         $xskip = 5;
                   6404:         $bar_width = 8;
                   6405:     } elsif ($NumBars <= 50) {
                   6406:         $width = 120+$NumBars*8;
                   6407:         $xskip = 5;
                   6408:         $bar_width = 4;
                   6409:     } else {
                   6410:         $width = 120+$NumBars*8;
                   6411:         $xskip = 5;
                   6412:         $bar_width = 4;
                   6413:     }
                   6414:     #
1.137     matthew  6415:     $Max = 1 if ($Max < 1);
                   6416:     if ( int($Max) < $Max ) {
                   6417:         $Max++;
                   6418:         $Max = int($Max);
                   6419:     }
1.127     matthew  6420:     $Title  = '' if (! defined($Title));
                   6421:     $xlabel = '' if (! defined($xlabel));
                   6422:     $ylabel = '' if (! defined($ylabel));
1.369     www      6423:     $ValuesHash{$id.'.title'}    = &escape($Title);
                   6424:     $ValuesHash{$id.'.xlabel'}   = &escape($xlabel);
                   6425:     $ValuesHash{$id.'.ylabel'}   = &escape($ylabel);
1.137     matthew  6426:     $ValuesHash{$id.'.y_max_value'} = $Max;
1.136     matthew  6427:     $ValuesHash{$id.'.NumBars'}  = $NumBars;
                   6428:     $ValuesHash{$id.'.NumSets'}  = $NumSets;
                   6429:     $ValuesHash{$id.'.PlotType'} = 'bar';
                   6430:     $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   6431:     $ValuesHash{$id.'.height'}   = $height;
                   6432:     $ValuesHash{$id.'.width'}    = $width;
                   6433:     $ValuesHash{$id.'.xskip'}    = $xskip;
                   6434:     $ValuesHash{$id.'.bar_width'} = $bar_width;
                   6435:     $ValuesHash{$id.'.labels'} = join(',',@Labels);
1.127     matthew  6436:     #
1.228     matthew  6437:     # Deal with other parameters
                   6438:     while (my ($key,$value) = each(%$extra_settings)) {
                   6439:         $ValuesHash{$id.'.'.$key} = $value;
                   6440:     }
                   6441:     #
1.137     matthew  6442:     &Apache::lonnet::appenv(%ValuesHash);
                   6443:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   6444: }
                   6445: 
                   6446: ############################################################
                   6447: ############################################################
                   6448: 
                   6449: =pod
                   6450: 
                   6451: =item DrawXYGraph
                   6452: 
1.138     matthew  6453: Facilitates the plotting of data in an XY graph.
                   6454: Puts plot definition data into the users environment in order for 
                   6455: graph.png to plot it.  Returns an <img> tag for the plot.
                   6456: 
                   6457: Inputs:
                   6458: 
                   6459: =over 4
                   6460: 
                   6461: =item $Title: string, the title of the plot
                   6462: 
                   6463: =item $xlabel: string, text describing the X-axis of the plot
                   6464: 
                   6465: =item $ylabel: string, text describing the Y-axis of the plot
                   6466: 
                   6467: =item $Max: scalar, the maximum Y value to use in the plot
                   6468: If $Max is < any data point, the graph will not be rendered.
                   6469: 
                   6470: =item $colors: Array ref containing the hex color codes for the data to be 
                   6471: plotted in.  If undefined, default values will be used.
                   6472: 
                   6473: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   6474: 
                   6475: =item $Ydata: Array ref containing Array refs.  
1.185     www      6476: Each of the contained arrays will be plotted as a separate curve.
1.138     matthew  6477: 
                   6478: =item %Values: hash indicating or overriding any default values which are 
                   6479: passed to graph.png.  
                   6480: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   6481: 
                   6482: =back
                   6483: 
                   6484: Returns:
                   6485: 
                   6486: An <img> tag which references graph.png and the appropriate identifying
                   6487: information for the plot.
                   6488: 
1.137     matthew  6489: =cut
                   6490: 
                   6491: ############################################################
                   6492: ############################################################
                   6493: sub DrawXYGraph {
                   6494:     my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
                   6495:     #
                   6496:     # Create the identifier for the graph
                   6497:     my $identifier = &get_cgi_id();
                   6498:     my $id = 'cgi.'.$identifier;
                   6499:     #
                   6500:     $Title  = '' if (! defined($Title));
                   6501:     $xlabel = '' if (! defined($xlabel));
                   6502:     $ylabel = '' if (! defined($ylabel));
                   6503:     my %ValuesHash = 
                   6504:         (
1.369     www      6505:          $id.'.title'  => &escape($Title),
                   6506:          $id.'.xlabel' => &escape($xlabel),
                   6507:          $id.'.ylabel' => &escape($ylabel),
1.137     matthew  6508:          $id.'.y_max_value'=> $Max,
                   6509:          $id.'.labels'     => join(',',@$Xlabels),
                   6510:          $id.'.PlotType'   => 'XY',
                   6511:          );
                   6512:     #
                   6513:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   6514:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   6515:     }
                   6516:     #
                   6517:     if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
                   6518:         return '';
                   6519:     }
                   6520:     my $NumSets=1;
1.138     matthew  6521:     foreach my $array (@{$Ydata}){
1.137     matthew  6522:         next if (! ref($array));
                   6523:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
                   6524:     }
1.138     matthew  6525:     $ValuesHash{$id.'.NumSets'} = $NumSets-1;
1.137     matthew  6526:     #
                   6527:     # Deal with other parameters
                   6528:     while (my ($key,$value) = each(%Values)) {
                   6529:         $ValuesHash{$id.'.'.$key} = $value;
1.127     matthew  6530:     }
                   6531:     #
1.136     matthew  6532:     &Apache::lonnet::appenv(%ValuesHash);
                   6533:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   6534: }
                   6535: 
                   6536: ############################################################
                   6537: ############################################################
                   6538: 
                   6539: =pod
                   6540: 
1.138     matthew  6541: =item DrawXYYGraph
                   6542: 
                   6543: Facilitates the plotting of data in an XY graph with two Y axes.
                   6544: Puts plot definition data into the users environment in order for 
                   6545: graph.png to plot it.  Returns an <img> tag for the plot.
                   6546: 
                   6547: Inputs:
                   6548: 
                   6549: =over 4
                   6550: 
                   6551: =item $Title: string, the title of the plot
                   6552: 
                   6553: =item $xlabel: string, text describing the X-axis of the plot
                   6554: 
                   6555: =item $ylabel: string, text describing the Y-axis of the plot
                   6556: 
                   6557: =item $colors: Array ref containing the hex color codes for the data to be 
                   6558: plotted in.  If undefined, default values will be used.
                   6559: 
                   6560: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   6561: 
                   6562: =item $Ydata1: The first data set
                   6563: 
                   6564: =item $Min1: The minimum value of the left Y-axis
                   6565: 
                   6566: =item $Max1: The maximum value of the left Y-axis
                   6567: 
                   6568: =item $Ydata2: The second data set
                   6569: 
                   6570: =item $Min2: The minimum value of the right Y-axis
                   6571: 
                   6572: =item $Max2: The maximum value of the left Y-axis
                   6573: 
                   6574: =item %Values: hash indicating or overriding any default values which are 
                   6575: passed to graph.png.  
                   6576: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   6577: 
                   6578: =back
                   6579: 
                   6580: Returns:
                   6581: 
                   6582: An <img> tag which references graph.png and the appropriate identifying
                   6583: information for the plot.
1.136     matthew  6584: 
                   6585: =cut
                   6586: 
                   6587: ############################################################
                   6588: ############################################################
1.137     matthew  6589: sub DrawXYYGraph {
                   6590:     my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
                   6591:                                         $Ydata2,$Min2,$Max2,%Values)=@_;
1.136     matthew  6592:     #
                   6593:     # Create the identifier for the graph
                   6594:     my $identifier = &get_cgi_id();
                   6595:     my $id = 'cgi.'.$identifier;
                   6596:     #
                   6597:     $Title  = '' if (! defined($Title));
                   6598:     $xlabel = '' if (! defined($xlabel));
                   6599:     $ylabel = '' if (! defined($ylabel));
                   6600:     my %ValuesHash = 
                   6601:         (
1.369     www      6602:          $id.'.title'  => &escape($Title),
                   6603:          $id.'.xlabel' => &escape($xlabel),
                   6604:          $id.'.ylabel' => &escape($ylabel),
1.136     matthew  6605:          $id.'.labels' => join(',',@$Xlabels),
                   6606:          $id.'.PlotType' => 'XY',
                   6607:          $id.'.NumSets' => 2,
1.137     matthew  6608:          $id.'.two_axes' => 1,
                   6609:          $id.'.y1_max_value' => $Max1,
                   6610:          $id.'.y1_min_value' => $Min1,
                   6611:          $id.'.y2_max_value' => $Max2,
                   6612:          $id.'.y2_min_value' => $Min2,
1.136     matthew  6613:          );
                   6614:     #
1.137     matthew  6615:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   6616:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   6617:     }
                   6618:     #
                   6619:     if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
                   6620:         ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
1.136     matthew  6621:         return '';
                   6622:     }
                   6623:     my $NumSets=1;
1.137     matthew  6624:     foreach my $array ($Ydata1,$Ydata2){
1.136     matthew  6625:         next if (! ref($array));
                   6626:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
1.137     matthew  6627:     }
                   6628:     #
                   6629:     # Deal with other parameters
                   6630:     while (my ($key,$value) = each(%Values)) {
                   6631:         $ValuesHash{$id.'.'.$key} = $value;
1.136     matthew  6632:     }
                   6633:     #
                   6634:     &Apache::lonnet::appenv(%ValuesHash);
1.130     albertel 6635:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
1.139     matthew  6636: }
                   6637: 
                   6638: ############################################################
                   6639: ############################################################
                   6640: 
                   6641: =pod
                   6642: 
1.157     matthew  6643: =back 
                   6644: 
1.139     matthew  6645: =head1 Statistics helper routines?  
                   6646: 
                   6647: Bad place for them but what the hell.
                   6648: 
1.157     matthew  6649: =over 4
                   6650: 
1.139     matthew  6651: =item &chartlink
                   6652: 
                   6653: Returns a link to the chart for a specific student.  
                   6654: 
                   6655: Inputs:
                   6656: 
                   6657: =over 4
                   6658: 
                   6659: =item $linktext: The text of the link
                   6660: 
                   6661: =item $sname: The students username
                   6662: 
                   6663: =item $sdomain: The students domain
                   6664: 
                   6665: =back
                   6666: 
1.157     matthew  6667: =back
                   6668: 
1.139     matthew  6669: =cut
                   6670: 
                   6671: ############################################################
                   6672: ############################################################
                   6673: sub chartlink {
                   6674:     my ($linktext, $sname, $sdomain) = @_;
                   6675:     my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
1.369     www      6676:         '&amp;SelectedStudent='.&escape($sname.':'.$sdomain).
1.219     albertel 6677:         '&amp;chartoutputmode='.HTML::Entities::encode('html, with all links').
1.139     matthew  6678:        '">'.$linktext.'</a>';
1.153     matthew  6679: }
                   6680: 
                   6681: #######################################################
                   6682: #######################################################
                   6683: 
                   6684: =pod
                   6685: 
                   6686: =head1 Course Environment Routines
1.157     matthew  6687: 
                   6688: =over 4
1.153     matthew  6689: 
                   6690: =item &restore_course_settings 
                   6691: 
                   6692: =item &store_course_settings
                   6693: 
                   6694: Restores/Store indicated form parameters from the course environment.
                   6695: Will not overwrite existing values of the form parameters.
                   6696: 
                   6697: Inputs: 
                   6698: a scalar describing the data (e.g. 'chart', 'problem_analysis')
                   6699: 
                   6700: a hash ref describing the data to be stored.  For example:
                   6701:    
                   6702: %Save_Parameters = ('Status' => 'scalar',
                   6703:     'chartoutputmode' => 'scalar',
                   6704:     'chartoutputdata' => 'scalar',
                   6705:     'Section' => 'array',
1.373     raeburn  6706:     'Group' => 'array',
1.153     matthew  6707:     'StudentData' => 'array',
                   6708:     'Maps' => 'array');
                   6709: 
                   6710: Returns: both routines return nothing
                   6711: 
                   6712: =cut
                   6713: 
                   6714: #######################################################
                   6715: #######################################################
                   6716: sub store_course_settings {
1.496     albertel 6717:     return &store_settings($env{'request.course.id'},@_);
                   6718: }
                   6719: 
                   6720: sub store_settings {
1.153     matthew  6721:     # save to the environment
                   6722:     # appenv the same items, just to be safe
1.300     albertel 6723:     my $udom  = $env{'user.domain'};
                   6724:     my $uname = $env{'user.name'};
1.496     albertel 6725:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  6726:     my %SaveHash;
                   6727:     my %AppHash;
                   6728:     while (my ($setting,$type) = each(%$Settings)) {
1.496     albertel 6729:         my $basename = join('.','internal',$context,$prefix,$setting);
1.300     albertel 6730:         my $envname = 'environment.'.$basename;
1.258     albertel 6731:         if (exists($env{'form.'.$setting})) {
1.153     matthew  6732:             # Save this value away
                   6733:             if ($type eq 'scalar' &&
1.258     albertel 6734:                 (! exists($env{$envname}) || 
                   6735:                  $env{$envname} ne $env{'form.'.$setting})) {
                   6736:                 $SaveHash{$basename} = $env{'form.'.$setting};
                   6737:                 $AppHash{$envname}   = $env{'form.'.$setting};
1.153     matthew  6738:             } elsif ($type eq 'array') {
                   6739:                 my $stored_form;
1.258     albertel 6740:                 if (ref($env{'form.'.$setting})) {
1.153     matthew  6741:                     $stored_form = join(',',
                   6742:                                         map {
1.369     www      6743:                                             &escape($_);
1.258     albertel 6744:                                         } sort(@{$env{'form.'.$setting}}));
1.153     matthew  6745:                 } else {
                   6746:                     $stored_form = 
1.369     www      6747:                         &escape($env{'form.'.$setting});
1.153     matthew  6748:                 }
                   6749:                 # Determine if the array contents are the same.
1.258     albertel 6750:                 if ($stored_form ne $env{$envname}) {
1.153     matthew  6751:                     $SaveHash{$basename} = $stored_form;
                   6752:                     $AppHash{$envname}   = $stored_form;
                   6753:                 }
                   6754:             }
                   6755:         }
                   6756:     }
                   6757:     my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
1.300     albertel 6758:                                           $udom,$uname);
1.153     matthew  6759:     if ($put_result !~ /^(ok|delayed)/) {
                   6760:         &Apache::lonnet::logthis('unable to save form parameters, '.
                   6761:                                  'got error:'.$put_result);
                   6762:     }
                   6763:     # Make sure these settings stick around in this session, too
                   6764:     &Apache::lonnet::appenv(%AppHash);
                   6765:     return;
                   6766: }
                   6767: 
                   6768: sub restore_course_settings {
1.499     albertel 6769:     return &restore_settings($env{'request.course.id'},@_);
1.496     albertel 6770: }
                   6771: 
                   6772: sub restore_settings {
                   6773:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  6774:     while (my ($setting,$type) = each(%$Settings)) {
1.258     albertel 6775:         next if (exists($env{'form.'.$setting}));
1.496     albertel 6776:         my $envname = 'environment.internal.'.$context.'.'.$prefix.
1.153     matthew  6777:             '.'.$setting;
1.258     albertel 6778:         if (exists($env{$envname})) {
1.153     matthew  6779:             if ($type eq 'scalar') {
1.258     albertel 6780:                 $env{'form.'.$setting} = $env{$envname};
1.153     matthew  6781:             } elsif ($type eq 'array') {
1.258     albertel 6782:                 $env{'form.'.$setting} = [ 
1.153     matthew  6783:                                            map { 
1.369     www      6784:                                                &unescape($_); 
1.258     albertel 6785:                                            } split(',',$env{$envname})
1.153     matthew  6786:                                            ];
                   6787:             }
                   6788:         }
                   6789:     }
1.127     matthew  6790: }
                   6791: 
                   6792: ############################################################
                   6793: ############################################################
1.154     albertel 6794: 
1.443     albertel 6795: sub commit_customrole {
                   6796:     my ($udom,$uname,$url,$three,$four,$five,$start,$end) = @_;
                   6797:     my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.'@'.$three.' in '.$url.
                   6798:                          ($start?', '.&mt('starting').' '.localtime($start):'').
                   6799:                          ($end?', ending '.localtime($end):'').': <b>'.
                   6800:               &Apache::lonnet::assigncustomrole(
                   6801:                  $udom,$uname,$url,$three,$four,$five,$end,$start).
                   6802:                  '</b><br />';
                   6803:     return $output;
                   6804: }
                   6805: 
                   6806: sub commit_standardrole {
1.541     raeburn  6807:     my ($udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
                   6808:     my ($output,$logmsg,$linefeed);
                   6809:     if ($context eq 'auto') {
                   6810:         $linefeed = "\n";
                   6811:     } else {
                   6812:         $linefeed = "<br />\n";
                   6813:     }  
1.443     albertel 6814:     if ($three eq 'st') {
1.541     raeburn  6815:         my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$three,$start,$end,
                   6816:                                          $one,$two,$sec,$context);
                   6817:         if (($result =~ /^error/) || ($result eq 'not_in_class') || 
                   6818:             ($result eq 'unknown_course')) {
1.443     albertel 6819:             $output = "Error: $result\n"; 
                   6820:         } else {
1.541     raeburn  6821:             $output = $logmsg.$linefeed.&mt('Assigning').' '.$three.' in '.$url.
1.443     albertel 6822:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  6823:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
                   6824:             if ($context eq 'auto') {
                   6825:                 $output .= $result.$linefeed.&mt('Add to classlist').': ok';
                   6826:             } else {
                   6827:                $output .= '<b>'.$result.'</b>'.$linefeed.
                   6828:                &mt('Add to classlist').': <b>ok</b>';
                   6829:             }
                   6830:             $output .= $linefeed;
1.443     albertel 6831:         }
                   6832:     } else {
                   6833:         $output = &mt('Assigning').' '.$three.' in '.$url.
                   6834:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  6835:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
                   6836:         my $result = &Apache::lonnet::assignrole($udom,$uname,$url,$three,$end,$start);
                   6837:         if ($context eq 'auto') {
                   6838:             $output .= $result.$linefeed;
                   6839:         } else {
                   6840:             $output .= '<b>'.$result.'</b>'.$linefeed;
                   6841:         }
1.443     albertel 6842:     }
                   6843:     return $output;
                   6844: }
                   6845: 
                   6846: sub commit_studentrole {
1.541     raeburn  6847:     my ($logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
                   6848:     my ($result,$linefeed);
                   6849:     if ($context eq 'auto') {
                   6850:         $linefeed = "\n";
                   6851:     } else {
                   6852:         $linefeed = '<br />'."\n";
                   6853:     }
1.443     albertel 6854:     if (defined($one) && defined($two)) {
                   6855:         my $cid=$one.'_'.$two;
                   6856:         my $oldsec=&Apache::lonnet::getsection($udom,$uname,$cid);
                   6857:         my $secchange = 0;
                   6858:         my $expire_role_result;
                   6859:         my $modify_section_result;
                   6860:         unless ($oldsec eq '-1') {
                   6861:             unless ($sec eq $oldsec) {
                   6862:                 $secchange = 1;
                   6863:                 my $uurl='/'.$cid;
                   6864:                 $uurl=~s/\_/\//g;
                   6865:                 if ($oldsec) {
                   6866:                     $uurl.='/'.$oldsec;
                   6867:                 }
                   6868:                 $expire_role_result = &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',time);
                   6869:                 $result = $expire_role_result;
                   6870:             }
                   6871:         }
                   6872:         if (($expire_role_result eq 'ok') || ($secchange == 0)) {
                   6873:             $modify_section_result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,'','',$cid);
                   6874:             if ($modify_section_result =~ /^ok/) {
                   6875:                 if ($secchange == 1) {
                   6876:                     $$logmsg .= "Section for $uname switched from old section: $oldsec to new section: $sec".$linefeed;
                   6877:                 } elsif ($oldsec eq '-1') {
                   6878:                     $$logmsg .= "New student role for $uname in section $sec in course $cid".$linefeed;
                   6879:                 } else {
                   6880:                     $$logmsg .= "Student $uname assigned to unchanged section $sec in course $cid".$linefeed;
                   6881:                 }
                   6882:             } else {
                   6883:                 $$logmsg .= "Error when attempting section change for $uname from old section $oldsec to new section: $sec in course $cid -error: $modify_section_result".$linefeed;
                   6884:             }
                   6885:             $result = $modify_section_result;
                   6886:         } elsif ($secchange == 1) {
                   6887:             $$logmsg .= "Error when attempting to expire role for $uname in old section $oldsec in course $cid -error: $expire_role_result".$linefeed;
                   6888:         }
                   6889:     } else {
                   6890:         $$logmsg .= "Incomplete course id defined.  Addition of user $uname from domain $udom to course $one\_$two, section $sec not completed.$linefeed";
                   6891:         $result = "error: incomplete course id\n";
                   6892:     }
                   6893:     return $result;
                   6894: }
                   6895: 
                   6896: ############################################################
                   6897: ############################################################
                   6898: 
1.444     albertel 6899: sub construct_course {
1.541     raeburn  6900:     my ($args,$logmsg,$courseid,$crsudom,$crsunum,$udom,$uname,$context) = @_;
1.444     albertel 6901:     my $outcome;
1.541     raeburn  6902:     my $linefeed =  '<br />'."\n";
                   6903:     if ($context eq 'auto') {
                   6904:         $linefeed = "\n";
                   6905:     }
1.444     albertel 6906: #
                   6907: # Open course
                   6908: #
                   6909:     my $crstype = lc($args->{'crstype'});
                   6910:     my %cenv=();
                   6911:     $$courseid=&Apache::lonnet::createcourse($args->{'course_domain'},
                   6912:                                              $args->{'cdescr'},
                   6913:                                              $args->{'curl'},
                   6914:                                              $args->{'course_home'},
                   6915:                                              $args->{'nonstandard'},
                   6916:                                              $args->{'crscode'},
                   6917:                                              $args->{'ccuname'}.':'.
                   6918:                                              $args->{'ccdomain'},
                   6919:                                              $args->{'crstype'});
                   6920: 
                   6921:     # Note: The testing routines depend on this being output; see 
                   6922:     # Utils::Course. This needs to at least be output as a comment
                   6923:     # if anyone ever decides to not show this, and Utils::Course::new
                   6924:     # will need to be suitably modified.
1.541     raeburn  6925:     $outcome .= &mt('New LON-CAPA [_1] ID: [_2]',$crstype,$$courseid).$linefeed;
                   6926: 
1.444     albertel 6927: #
                   6928: # Check if created correctly
                   6929: #
1.479     albertel 6930:     ($$crsudom,$$crsunum)= &LONCAPA::split_courseid($$courseid);
1.444     albertel 6931:     my $crsuhome=&Apache::lonnet::homeserver($$crsunum,$$crsudom);
1.541     raeburn  6932:     $outcome .= &mt('Created on').': '.$crsuhome.$linefeed;
1.444     albertel 6933: #
                   6934: # Are we cloning?
                   6935: #
                   6936:     my $cloneid='';
                   6937:     if (($args->{'clonecourse'}) && ($args->{'clonedomain'})) {
1.541     raeburn  6938:         my $can_clone = 0;
1.444     albertel 6939: 	$cloneid='/'.$args->{'clonedomain'}.'/'.$args->{'clonecourse'};
1.479     albertel 6940:         my ($clonecrsudom,$clonecrsunum)= &LONCAPA::split_courseid($cloneid);
1.444     albertel 6941: 	my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
1.541     raeburn  6942:         my $clonemsg;
1.444     albertel 6943: 	if ($clonehome eq 'no_host') {
1.541     raeburn  6944:             $clonemsg = &mt('Attempting to clone non-existing [_1]',$crstype);
                   6945:             if ($context eq 'auto') {
                   6946:                 $outcome .= $clonemsg;
                   6947:             } else {
                   6948: 	        $outcome .= '<font color="red">'.$clonemsg.'</font>';
                   6949:             }
                   6950:             $outcome .= $linefeed;
1.444     albertel 6951: 	} else {
1.541     raeburn  6952:             my %clonedesc = &Apache::lonnet::coursedescription($cloneid,{'one_time' => 1});
                   6953:             if ($env{'request.role.domain'} eq $args->{'form.clonedomain'}) {
                   6954:                 $can_clone = 1;
                   6955:             } else {
                   6956:                 my %clonehash = &Apache::lonnet::get('environment',['cloners'],
                   6957:                             $args->{'clonedomain'},$args->{'clonecourse'});
                   6958:                 my @cloners = split(/,/,$clonehash{'cloners'});
                   6959:                 my %roleshash =
                   6960:                     &Apache::lonnet::get_my_roles($args->{'ccuname'},
                   6961:                         $args->{'ccdomain'},'userroles',['active'],['cc'],
                   6962:                         [$args->{'clonedomain'}]);
                   6963:                 if (($roleshash{$args->{'clonecourse'}.':'.$args->{'clonedomain'}.':cc'}) || (grep(/^\Q$args->{'ccuname'}\E:\Q$args->{'ccdomain'}\E$/,@cloners))) {
                   6964:                     $can_clone = 1;
                   6965:                 } else {
                   6966:                     $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'});
                   6967:                     if ($context eq 'auto') {
                   6968:                         $outcome .= $clonemsg;
                   6969:                     } else {
                   6970:                         $outcome .= '<font color="red">'.$clonemsg.'</font>';
                   6971:                     }
                   6972:                     $outcome .= $linefeed;
                   6973:                 }
                   6974:             }
                   6975:         }
                   6976:         if ($can_clone) {
                   6977: 	    $clonemsg = &mt('Cloning [_1] from [_2]',$crstype,$clonehome);
                   6978:             if ($context eq 'auto') {
                   6979:                 $outcome = $clonemsg;
                   6980:             } else { 
                   6981:                 $outcome .= '<font color="green">'.$clonemsg.'</font>';
                   6982:             }
                   6983:             $outcome .= $linefeed;
1.444     albertel 6984: 	    my %oldcenv=&Apache::lonnet::dump('environment',$$crsudom,$$crsunum);
                   6985: # Copy all files
                   6986: 	    &Apache::lonclonecourse::copycoursefiles($cloneid,$$courseid);
                   6987: # Restore URL
                   6988: 	    $cenv{'url'}=$oldcenv{'url'};
                   6989: # Restore title
                   6990: 	    $cenv{'description'}=$oldcenv{'description'};
                   6991: # restore grading mode
                   6992: 	    if (defined($oldcenv{'grading'})) {
                   6993: 		$cenv{'grading'}=$oldcenv{'grading'};
                   6994: 	    }
                   6995: # Mark as cloned
                   6996: 	    $cenv{'clonedfrom'}=$cloneid;
                   6997: 	    delete($cenv{'default_enrollment_start_date'});
                   6998: 	    delete($cenv{'default_enrollment_end_date'});
                   6999: 	}
                   7000:     }
                   7001: #
                   7002: # Set environment (will override cloned, if existing)
                   7003: #
                   7004:     my @sections = ();
                   7005:     my @xlists = ();
                   7006:     if ($args->{'crstype'}) {
                   7007:         $cenv{'type'}=$args->{'crstype'};
                   7008:     }
                   7009:     if ($args->{'crsid'}) {
                   7010:         $cenv{'courseid'}=$args->{'crsid'};
                   7011:     }
                   7012:     if ($args->{'crscode'}) {
                   7013:         $cenv{'internal.coursecode'}=$args->{'crscode'};
                   7014:     }
                   7015:     if ($args->{'crsquota'} ne '') {
                   7016:         $cenv{'internal.coursequota'}=$args->{'crsquota'};
                   7017:     } else {
                   7018:         $cenv{'internal.coursequota'}=$args->{'crsquota'} = 20;
                   7019:     }
                   7020:     if ($args->{'ccuname'}) {
                   7021:         $cenv{'internal.courseowner'} = $args->{'ccuname'}.
                   7022:                                         ':'.$args->{'ccdomain'};
                   7023:     } else {
                   7024:         $cenv{'internal.courseowner'} = $args->{'curruser'};
                   7025:     }
                   7026: 
                   7027:     my @badclasses = (); # Used to accumulate sections/crosslistings that did not pass classlist access check for course owner.
                   7028:     if ($args->{'crssections'}) {
                   7029:         $cenv{'internal.sectionnums'} = '';
                   7030:         if ($args->{'crssections'} =~ m/,/) {
                   7031:             @sections = split/,/,$args->{'crssections'};
                   7032:         } else {
                   7033:             $sections[0] = $args->{'crssections'};
                   7034:         }
                   7035:         if (@sections > 0) {
                   7036:             foreach my $item (@sections) {
                   7037:                 my ($sec,$gp) = split/:/,$item;
                   7038:                 my $class = $args->{'crscode'}.$sec;
                   7039:                 my $addcheck = &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$class,$cenv{'internal.courseowner'});
                   7040:                 $cenv{'internal.sectionnums'} .= $item.',';
                   7041:                 unless ($addcheck eq 'ok') {
                   7042:                     push @badclasses, $class;
                   7043:                 }
                   7044:             }
                   7045:             $cenv{'internal.sectionnums'} =~ s/,$//;
                   7046:         }
                   7047:     }
                   7048: # do not hide course coordinator from staff listing, 
                   7049: # even if privileged
                   7050:     $cenv{'nothideprivileged'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   7051: # add crosslistings
                   7052:     if ($args->{'crsxlist'}) {
                   7053:         $cenv{'internal.crosslistings'}='';
                   7054:         if ($args->{'crsxlist'} =~ m/,/) {
                   7055:             @xlists = split/,/,$args->{'crsxlist'};
                   7056:         } else {
                   7057:             $xlists[0] = $args->{'crsxlist'};
                   7058:         }
                   7059:         if (@xlists > 0) {
                   7060:             foreach my $item (@xlists) {
                   7061:                 my ($xl,$gp) = split/:/,$item;
                   7062:                 my $addcheck =  &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$xl,$cenv{'internal.courseowner'});
                   7063:                 $cenv{'internal.crosslistings'} .= $item.',';
                   7064:                 unless ($addcheck eq 'ok') {
                   7065:                     push @badclasses, $xl;
                   7066:                 }
                   7067:             }
                   7068:             $cenv{'internal.crosslistings'} =~ s/,$//;
                   7069:         }
                   7070:     }
                   7071:     if ($args->{'autoadds'}) {
                   7072:         $cenv{'internal.autoadds'}=$args->{'autoadds'};
                   7073:     }
                   7074:     if ($args->{'autodrops'}) {
                   7075:         $cenv{'internal.autodrops'}=$args->{'autodrops'};
                   7076:     }
                   7077: # check for notification of enrollment changes
                   7078:     my @notified = ();
                   7079:     if ($args->{'notify_owner'}) {
                   7080:         if ($args->{'ccuname'} ne '') {
                   7081:             push(@notified,$args->{'ccuname'}.':'.$args->{'ccdomain'});
                   7082:         }
                   7083:     }
                   7084:     if ($args->{'notify_dc'}) {
                   7085:         if ($uname ne '') { 
                   7086:             push(@notified,$uname.'@'.$udom);
                   7087:         }
                   7088:     }
                   7089:     if (@notified > 0) {
                   7090:         my $notifylist;
                   7091:         if (@notified > 1) {
                   7092:             $notifylist = join(',',@notified);
                   7093:         } else {
                   7094:             $notifylist = $notified[0];
                   7095:         }
                   7096:         $cenv{'internal.notifylist'} = $notifylist;
                   7097:     }
                   7098:     if (@badclasses > 0) {
                   7099:         my %lt=&Apache::lonlocal::texthash(
                   7100:                 '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',
                   7101:                 'dnhr' => 'does not have rights to access enrollment in these classes',
                   7102:                 'adby' => 'as determined by the policies of your institution on access to official classlists'
                   7103:         );
1.541     raeburn  7104:         my $badclass_msg = $cenv{'internal.courseowner'}.') - '.$lt{'dnhr'}.
                   7105:                            ' ('.$lt{'adby'}.')';
                   7106:         if ($context eq 'auto') {
                   7107:             $outcome .= $badclass_msg.$linefeed;
                   7108:             $outcome .= '<font color="red">'.$badclass_msg.$linefeed.'<ul>'."\n";
                   7109:             foreach my $item (@badclasses) {
                   7110:                 if ($context eq 'auto') {
                   7111:                     $outcome .= " - $item\n";
                   7112:                 } else {
                   7113:                     $outcome .= "<li>$item</li>\n";
                   7114:                 }
                   7115:             }
                   7116:             if ($context eq 'auto') {
                   7117:                 $outcome .= $linefeed;
                   7118:             } else {
                   7119:                 $outcome .= "</ul><br /><br /></font>\n";
                   7120:             }
                   7121:         } 
1.444     albertel 7122:     }
                   7123:     if ($args->{'no_end_date'}) {
                   7124:         $args->{'endaccess'} = 0;
                   7125:     }
                   7126:     $cenv{'internal.autostart'}=$args->{'enrollstart'};
                   7127:     $cenv{'internal.autoend'}=$args->{'enrollend'};
                   7128:     $cenv{'default_enrollment_start_date'}=$args->{'startaccess'};
                   7129:     $cenv{'default_enrollment_end_date'}=$args->{'endaccess'};
                   7130:     if ($args->{'showphotos'}) {
                   7131:       $cenv{'internal.showphotos'}=$args->{'showphotos'};
                   7132:     }
                   7133:     $cenv{'internal.authtype'} = $args->{'authtype'};
                   7134:     $cenv{'internal.autharg'} = $args->{'autharg'}; 
                   7135:     if ( ($cenv{'internal.authtype'} =~ /^krb/) && ($cenv{'internal.autoadds'} == 1)) {
                   7136:         if (! defined($cenv{'internal.autharg'}) || $cenv{'internal.autharg'}  eq '') {
1.541     raeburn  7137:             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'); 
                   7138:             if ($context eq 'auto') {
                   7139:                 $outcome .= $krb_msg;
                   7140:             } else {
                   7141:                 $outcome .= '<font color="red" size="+1">'.$krb_msg.'</font>';
                   7142:             }
                   7143:             $outcome .= $linefeed;
1.444     albertel 7144:         }
                   7145:     }
                   7146:     if (($args->{'ccdomain'}) && ($args->{'ccuname'})) {
                   7147:        if ($args->{'setpolicy'}) {
                   7148:            $cenv{'policy.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   7149:        }
                   7150:        if ($args->{'setcontent'}) {
                   7151:            $cenv{'question.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   7152:        }
                   7153:     }
                   7154:     if ($args->{'reshome'}) {
                   7155: 	$cenv{'reshome'}=$args->{'reshome'}.'/';
                   7156: 	$cenv{'reshome'}=~s/\/+$/\//;
                   7157:     }
                   7158: #
                   7159: # course has keyed access
                   7160: #
                   7161:     if ($args->{'setkeys'}) {
                   7162:        $cenv{'keyaccess'}='yes';
                   7163:     }
                   7164: # if specified, key authority is not course, but user
                   7165: # only active if keyaccess is yes
                   7166:     if ($args->{'keyauth'}) {
1.487     albertel 7167: 	my ($user,$domain) = split(':',$args->{'keyauth'});
                   7168: 	$user = &LONCAPA::clean_username($user);
                   7169: 	$domain = &LONCAPA::clean_username($domain);
1.488     foxr     7170: 	if ($user ne '' && $domain ne '') {
1.487     albertel 7171: 	    $cenv{'keyauth'}=$user.':'.$domain;
1.444     albertel 7172: 	}
                   7173:     }
                   7174: 
                   7175:     if ($args->{'disresdis'}) {
                   7176:         $cenv{'pch.roles.denied'}='st';
                   7177:     }
                   7178:     if ($args->{'disablechat'}) {
                   7179:         $cenv{'plc.roles.denied'}='st';
                   7180:     }
                   7181: 
                   7182:     # Record we've not yet viewed the Course Initialization Helper for this 
                   7183:     # course
                   7184:     $cenv{'course.helper.not.run'} = 1;
                   7185:     #
                   7186:     # Use new Randomseed
                   7187:     #
                   7188:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
                   7189:     $cenv{'receiptalg'}=&Apache::lonnet::latest_receipt_algorithm_id();;
                   7190:     #
                   7191:     # The encryption code and receipt prefix for this course
                   7192:     #
                   7193:     $cenv{'internal.encseed'}=$Apache::lonnet::perlvar{'lonReceipt'}.$$.time.int(rand(9999));
                   7194:     $cenv{'internal.encpref'}=100+int(9*rand(99));
                   7195:     #
                   7196:     # By default, use standard grading
                   7197:     if (!defined($cenv{'grading'})) { $cenv{'grading'} = 'standard'; }
                   7198: 
1.541     raeburn  7199:     $outcome .= $linefeed.&mt('Setting environment').': '.                 
                   7200:           &Apache::lonnet::put('environment',\%cenv,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 7201: #
                   7202: # Open all assignments
                   7203: #
                   7204:     if ($args->{'openall'}) {
                   7205:        my $storeunder=$$crsudom.'_'.$$crsunum.'.0.opendate';
                   7206:        my %storecontent = ($storeunder         => time,
                   7207:                            $storeunder.'.type' => 'date_start');
                   7208:        
                   7209:        $outcome .= &mt('Opening all assignments').': '.&Apache::lonnet::cput
1.541     raeburn  7210:                  ('resourcedata',\%storecontent,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 7211:    }
                   7212: #
                   7213: # Set first page
                   7214: #
                   7215:     unless (($args->{'nonstandard'}) || ($args->{'firstres'} eq 'blank')
                   7216: 	    || ($cloneid)) {
1.445     albertel 7217: 	use LONCAPA::map;
1.444     albertel 7218: 	$outcome .= &mt('Setting first resource').': ';
1.445     albertel 7219: 
                   7220: 	my $map = '/uploaded/'.$$crsudom.'/'.$$crsunum.'/default.sequence';
                   7221:         my ($errtext,$fatal)=&LONCAPA::map::mapread($map);
                   7222: 
1.444     albertel 7223:         $outcome .= ($fatal?$errtext:'read ok').' - ';
                   7224:         my $title; my $url;
                   7225:         if ($args->{'firstres'} eq 'syl') {
                   7226: 	    $title='Syllabus';
                   7227:             $url='/public/'.$$crsudom.'/'.$$crsunum.'/syllabus';
                   7228:         } else {
                   7229:             $title='Navigate Contents';
                   7230:             $url='/adm/navmaps';
                   7231:         }
1.445     albertel 7232: 
                   7233:         $LONCAPA::map::resources[1]=$title.':'.$url.':false:start:res';
                   7234: 	(my $outtext,$errtext) = &LONCAPA::map::storemap($map,1);
                   7235: 
                   7236: 	if ($errtext) { $fatal=2; }
1.541     raeburn  7237:         $outcome .= ($fatal?$errtext:'write ok').$linefeed;
1.444     albertel 7238:     }
                   7239:     return $outcome;
                   7240: }
                   7241: 
                   7242: ############################################################
                   7243: ############################################################
                   7244: 
1.378     raeburn  7245: sub course_type {
                   7246:     my ($cid) = @_;
                   7247:     if (!defined($cid)) {
                   7248:         $cid = $env{'request.course.id'};
                   7249:     }
1.404     albertel 7250:     if (defined($env{'course.'.$cid.'.type'})) {
                   7251:         return $env{'course.'.$cid.'.type'};
1.378     raeburn  7252:     } else {
                   7253:         return 'Course';
1.377     raeburn  7254:     }
                   7255: }
1.156     albertel 7256: 
1.406     raeburn  7257: sub group_term {
                   7258:     my $crstype = &course_type();
                   7259:     my %names = (
                   7260:                   'Course' => 'group',
                   7261:                   'Group' => 'team',
                   7262:                 );
                   7263:     return $names{$crstype};
                   7264: }
                   7265: 
1.156     albertel 7266: sub icon {
                   7267:     my ($file)=@_;
1.505     albertel 7268:     my $curfext = lc((split(/\./,$file))[-1]);
1.168     albertel 7269:     my $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/unknown.gif';
1.156     albertel 7270:     my $embstyle = &Apache::loncommon::fileembstyle($curfext);
1.168     albertel 7271:     if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) {
                   7272: 	if (-e  $Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
                   7273: 	          $Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   7274: 	            $curfext.".gif") {
                   7275: 	    $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   7276: 		$curfext.".gif";
                   7277: 	}
                   7278:     }
1.249     albertel 7279:     return &lonhttpdurl($iconname);
1.154     albertel 7280: } 
1.84      albertel 7281: 
1.215     albertel 7282: sub lonhttpdurl {
                   7283:     my ($url)=@_;
                   7284:     my $lonhttpd_port=$Apache::lonnet::perlvar{'lonhttpdPort'};
                   7285:     if (!defined($lonhttpd_port)) { $lonhttpd_port='8080'; }
                   7286:     return 'http://'.$ENV{'SERVER_NAME'}.':'.$lonhttpd_port.$url;
                   7287: }
                   7288: 
1.213     albertel 7289: sub connection_aborted {
                   7290:     my ($r)=@_;
                   7291:     $r->print(" ");$r->rflush();
                   7292:     my $c = $r->connection;
                   7293:     return $c->aborted();
                   7294: }
                   7295: 
1.221     foxr     7296: #    Escapes strings that may have embedded 's that will be put into
1.222     foxr     7297: #    strings as 'strings'.
                   7298: sub escape_single {
1.221     foxr     7299:     my ($input) = @_;
1.223     albertel 7300:     $input =~ s/\\/\\\\/g;	# Escape the \'s..(must be first)>
1.221     foxr     7301:     $input =~ s/\'/\\\'/g;	# Esacpe the 's....
                   7302:     return $input;
                   7303: }
1.223     albertel 7304: 
1.222     foxr     7305: #  Same as escape_single, but escape's "'s  This 
                   7306: #  can be used for  "strings"
                   7307: sub escape_double {
                   7308:     my ($input) = @_;
                   7309:     $input =~ s/\\/\\\\/g;	# Escape the /'s..(must be first)>
                   7310:     $input =~ s/\"/\\\"/g;	# Esacpe the "s....
                   7311:     return $input;
                   7312: }
1.223     albertel 7313:  
1.222     foxr     7314: #   Escapes the last element of a full URL.
                   7315: sub escape_url {
                   7316:     my ($url)   = @_;
1.238     raeburn  7317:     my @urlslices = split(/\//, $url,-1);
1.369     www      7318:     my $lastitem = &escape(pop(@urlslices));
1.223     albertel 7319:     return join('/',@urlslices).'/'.$lastitem;
1.222     foxr     7320: }
1.462     albertel 7321: 
                   7322: # -------------------------------------------------------- Initliaze user login
                   7323: sub init_user_environment {
1.463     albertel 7324:     my ($r, $username, $domain, $authhost, $form, $args) = @_;
1.462     albertel 7325:     my $lonids=$Apache::lonnet::perlvar{'lonIDsDir'};
                   7326: 
                   7327:     my $public=($username eq 'public' && $domain eq 'public');
                   7328: 
                   7329: # See if old ID present, if so, remove
                   7330: 
                   7331:     my ($filename,$cookie,$userroles);
                   7332:     my $now=time;
                   7333: 
                   7334:     if ($public) {
                   7335: 	my $max_public=100;
                   7336: 	my $oldest;
                   7337: 	my $oldest_time=0;
                   7338: 	for(my $next=1;$next<=$max_public;$next++) {
                   7339: 	    if (-e $lonids."/publicuser_$next.id") {
                   7340: 		my $mtime=(stat($lonids."/publicuser_$next.id"))[9];
                   7341: 		if ($mtime<$oldest_time || !$oldest_time) {
                   7342: 		    $oldest_time=$mtime;
                   7343: 		    $oldest=$next;
                   7344: 		}
                   7345: 	    } else {
                   7346: 		$cookie="publicuser_$next";
                   7347: 		last;
                   7348: 	    }
                   7349: 	}
                   7350: 	if (!$cookie) { $cookie="publicuser_$oldest"; }
                   7351:     } else {
1.463     albertel 7352: 	# if this isn't a robot, kill any existing non-robot sessions
                   7353: 	if (!$args->{'robot'}) {
                   7354: 	    opendir(DIR,$lonids);
                   7355: 	    while ($filename=readdir(DIR)) {
                   7356: 		if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
                   7357: 		    unlink($lonids.'/'.$filename);
                   7358: 		}
1.462     albertel 7359: 	    }
1.463     albertel 7360: 	    closedir(DIR);
1.462     albertel 7361: 	}
                   7362: # Give them a new cookie
1.463     albertel 7363: 	my $id = ($args->{'robot'} ? 'robot'.$args->{'robot'}
                   7364: 		                   : $now);
                   7365: 	$cookie="$username\_$id\_$domain\_$authhost";
1.462     albertel 7366:     
                   7367: # Initialize roles
                   7368: 
                   7369: 	$userroles=&Apache::lonnet::rolesinit($domain,$username,$authhost);
                   7370:     }
                   7371: # ------------------------------------ Check browser type and MathML capability
                   7372: 
                   7373:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                   7374:         $clientunicode,$clientos) = &decode_user_agent($r);
                   7375: 
                   7376: # -------------------------------------- Any accessibility options to remember?
                   7377:     if (($form->{'interface'}) && ($form->{'remember'} eq 'true')) {
                   7378: 	foreach my $option ('imagesuppress','appletsuppress',
                   7379: 			    'embedsuppress','fontenhance','blackwhite') {
                   7380: 	    if ($form->{$option} eq 'true') {
                   7381: 		&Apache::lonnet::put('environment',{$option => 'on'},
                   7382: 				     $domain,$username);
                   7383: 	    } else {
                   7384: 		&Apache::lonnet::del('environment',[$option],
                   7385: 				     $domain,$username);
                   7386: 	    }
                   7387: 	}
                   7388:     }
                   7389: # ------------------------------------------------------------- Get environment
                   7390: 
                   7391:     my %userenv = &Apache::lonnet::dump('environment',$domain,$username);
                   7392:     my ($tmp) = keys(%userenv);
                   7393:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   7394: 	# default remote control to off
                   7395: 	if ($userenv{'remote'} ne 'on') { $userenv{'remote'} = 'off'; }
                   7396:     } else {
                   7397: 	undef(%userenv);
                   7398:     }
                   7399:     if (($userenv{'interface'}) && (!$form->{'interface'})) {
                   7400: 	$form->{'interface'}=$userenv{'interface'};
                   7401:     }
                   7402:     $env{'environment.remote'}=$userenv{'remote'};
                   7403:     if ($userenv{'texengine'} eq 'ttm') { $clientmathml=1; }
                   7404: 
                   7405: # --------------- Do not trust query string to be put directly into environment
                   7406:     foreach my $option ('imagesuppress','appletsuppress',
                   7407: 			'embedsuppress','fontenhance','blackwhite',
                   7408: 			'interface','localpath','localres') {
                   7409: 	$form->{$option}=~s/[\n\r\=]//gs;
                   7410:     }
                   7411: # --------------------------------------------------------- Write first profile
                   7412: 
                   7413:     {
                   7414: 	my %initial_env = 
                   7415: 	    ("user.name"          => $username,
                   7416: 	     "user.domain"        => $domain,
                   7417: 	     "user.home"          => $authhost,
                   7418: 	     "browser.type"       => $clientbrowser,
                   7419: 	     "browser.version"    => $clientversion,
                   7420: 	     "browser.mathml"     => $clientmathml,
                   7421: 	     "browser.unicode"    => $clientunicode,
                   7422: 	     "browser.os"         => $clientos,
                   7423: 	     "server.domain"      => $Apache::lonnet::perlvar{'lonDefDomain'},
                   7424: 	     "request.course.fn"  => '',
                   7425: 	     "request.course.uri" => '',
                   7426: 	     "request.course.sec" => '',
                   7427: 	     "request.role"       => 'cm',
                   7428: 	     "request.role.adv"   => $env{'user.adv'},
                   7429: 	     "request.host"       => $ENV{'REMOTE_ADDR'},);
                   7430: 
                   7431:         if ($form->{'localpath'}) {
                   7432: 	    $initial_env{"browser.localpath"}  = $form->{'localpath'};
                   7433: 	    $initial_env{"browser.localres"}   = $form->{'localres'};
                   7434:         }
                   7435: 	
                   7436: 	if ($public) {
                   7437: 	    $initial_env{"environment.remote"} = "off";
                   7438: 	}
                   7439: 	if ($form->{'interface'}) {
                   7440: 	    $form->{'interface'}=~s/\W//gs;
                   7441: 	    $initial_env{"browser.interface"} = $form->{'interface'};
                   7442: 	    $env{'browser.interface'}=$form->{'interface'};
                   7443: 	    foreach my $option ('imagesuppress','appletsuppress',
                   7444: 				'embedsuppress','fontenhance','blackwhite') {
                   7445: 		if (($form->{$option} eq 'true') ||
                   7446: 		    ($userenv{$option} eq 'on')) {
                   7447: 		    $initial_env{"browser.$option"} = "on";
                   7448: 		}
                   7449: 	    }
                   7450: 	}
                   7451: 
                   7452: 	$env{'user.environment'} = "$lonids/$cookie.id";
                   7453: 	
                   7454: 	if (tie(my %disk_env,'GDBM_File',"$lonids/$cookie.id",
                   7455: 		 &GDBM_WRCREAT(),0640)) {
                   7456: 	    &_add_to_env(\%disk_env,\%initial_env);
                   7457: 	    &_add_to_env(\%disk_env,\%userenv,'environment.');
                   7458: 	    &_add_to_env(\%disk_env,$userroles);
1.463     albertel 7459: 	    if (ref($args->{'extra_env'})) {
                   7460: 		&_add_to_env(\%disk_env,$args->{'extra_env'});
                   7461: 	    }
1.462     albertel 7462: 	    untie(%disk_env);
                   7463: 	} else {
                   7464: 	    &Apache::lonnet::logthis("<font color=\"blue\">WARNING: ".
                   7465: 			   'Could not create environment storage in lonauth: '.$!.'</font>');
                   7466: 	    return 'error: '.$!;
                   7467: 	}
                   7468:     }
                   7469:     $env{'request.role'}='cm';
                   7470:     $env{'request.role.adv'}=$env{'user.adv'};
                   7471:     $env{'browser.type'}=$clientbrowser;
                   7472: 
                   7473:     return $cookie;
                   7474: 
                   7475: }
                   7476: 
                   7477: sub _add_to_env {
                   7478:     my ($idf,$env_data,$prefix) = @_;
                   7479:     while (my ($key,$value) = each(%$env_data)) {
                   7480: 	$idf->{$prefix.$key} = $value;
                   7481: 	$env{$prefix.$key}   = $value;
                   7482:     }
                   7483: }
                   7484: 
                   7485: 
1.41      ng       7486: =pod
                   7487: 
                   7488: =back
                   7489: 
1.112     bowersj2 7490: =cut
1.41      ng       7491: 
1.112     bowersj2 7492: 1;
                   7493: __END__;
1.41      ng       7494: 

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