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

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

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