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

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

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