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

1.10      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # a pile of common routines
1.10      albertel    3: #
1.652   ! raeburn     4: # $Id: loncommon.pm,v 1.651 2008/03/28 21:05:28 www Exp $
1.10      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.1       albertel   28: 
                     29: # Makes a table out of the previous attempts
1.2       albertel   30: # Inputs result_from_symbread, user, domain, course_id
1.16      harris41   31: # Reads in non-network-related .tab files
1.1       albertel   32: 
1.35      matthew    33: # POD header:
                     34: 
1.45      matthew    35: =pod
                     36: 
1.35      matthew    37: =head1 NAME
                     38: 
                     39: Apache::loncommon - pile of common routines
                     40: 
                     41: =head1 SYNOPSIS
                     42: 
1.112     bowersj2   43: Common routines for manipulating connections, student answers,
                     44:     domains, common Javascript fragments, etc.
1.35      matthew    45: 
1.112     bowersj2   46: =head1 OVERVIEW
1.35      matthew    47: 
1.112     bowersj2   48: A collection of commonly used subroutines that don't have a natural
                     49: home anywhere else. This collection helps remove
1.35      matthew    50: redundancy from other modules and increase efficiency of memory usage.
                     51: 
                     52: =cut 
                     53: 
                     54: # End of POD header
1.1       albertel   55: package Apache::loncommon;
                     56: 
                     57: use strict;
1.258     albertel   58: use Apache::lonnet;
1.46      matthew    59: use GDBM_File;
1.51      www        60: use POSIX qw(strftime mktime);
1.82      www        61: use Apache::lonmenu();
1.498     albertel   62: use Apache::lonenc();
1.117     www        63: use Apache::lonlocal;
1.139     matthew    64: use HTML::Entities;
1.334     albertel   65: use Apache::lonhtmlcommon();
                     66: use Apache::loncoursedata();
1.344     albertel   67: use Apache::lontexconvert();
1.444     albertel   68: use Apache::lonclonecourse();
1.479     albertel   69: use LONCAPA qw(:DEFAULT :match);
1.117     www        70: 
1.517     raeburn    71: # ---------------------------------------------- Designs
                     72: use vars qw(%defaultdesign);
                     73: 
1.22      www        74: my $readit;
                     75: 
1.517     raeburn    76: 
1.157     matthew    77: ##
                     78: ## Global Variables
                     79: ##
1.46      matthew    80: 
1.643     foxr       81: 
                     82: # ----------------------------------------------- SSI with retries:
                     83: #
                     84: 
                     85: =pod
                     86: 
1.648     raeburn    87: =head1 Server Side include with retries:
1.643     foxr       88: 
                     89: =over 4
                     90: 
1.648     raeburn    91: =item * &ssi_with_retries(resource,retries form)
1.643     foxr       92: 
                     93: Performs an ssi with some number of retries.  Retries continue either
                     94: until the result is ok or until the retry count supplied by the
                     95: caller is exhausted.  
                     96: 
                     97: Inputs:
1.648     raeburn    98: 
                     99: =over 4
                    100: 
1.643     foxr      101: resource   - Identifies the resource to insert.
1.648     raeburn   102: 
1.643     foxr      103: retries    - Count of the number of retries allowed.
1.648     raeburn   104: 
1.643     foxr      105: form       - Hash that identifies the rendering options.
                    106: 
1.648     raeburn   107: =back
                    108: 
                    109: Returns:
                    110: 
                    111: =over 4
                    112: 
1.643     foxr      113: content    - The content of the response.  If retries were exhausted this is empty.
1.648     raeburn   114: 
1.643     foxr      115: response   - The response from the last attempt (which may or may not have been successful.
                    116: 
1.648     raeburn   117: =back
                    118: 
                    119: =back
                    120: 
1.643     foxr      121: =cut
                    122: 
                    123: sub ssi_with_retries {
                    124:     my ($resource, $retries, %form) = @_;
                    125: 
                    126: 
                    127:     my $ok = 0;			# True if we got a good response.
                    128:     my $content;
                    129:     my $response;
                    130: 
                    131:     # Try to get the ssi done. within the retries count:
                    132: 
                    133:     do {
                    134: 	($content, $response) = &Apache::lonnet::ssi($resource, %form);
                    135: 	$ok      = $response->is_success;
1.650     www       136:         if (!$ok) {
                    137:             &Apache::lonnet::logthis("Failed ssi_with_retries on $resource: ".$response->is_success.', '.$response->code.', '.$response->message);
                    138:         }
1.643     foxr      139: 	$retries--;
                    140:     } while (!$ok && ($retries > 0));
                    141: 
                    142:     if (!$ok) {
                    143: 	$content = '';		# On error return an empty content.
                    144:     }
                    145:     return ($content, $response);
                    146: 
                    147: }
                    148: 
                    149: 
                    150: 
1.20      www       151: # ----------------------------------------------- Filetypes/Languages/Copyright
1.12      harris41  152: my %language;
1.124     www       153: my %supported_language;
1.12      harris41  154: my %cprtag;
1.192     taceyjo1  155: my %scprtag;
1.351     www       156: my %fe; my %fd; my %fm;
1.41      ng        157: my %category_extensions;
1.12      harris41  158: 
1.46      matthew   159: # ---------------------------------------------- Thesaurus variables
1.144     matthew   160: #
                    161: # %Keywords:
                    162: #      A hash used by &keyword to determine if a word is considered a keyword.
                    163: # $thesaurus_db_file 
                    164: #      Scalar containing the full path to the thesaurus database.
1.46      matthew   165: 
                    166: my %Keywords;
                    167: my $thesaurus_db_file;
                    168: 
1.144     matthew   169: #
                    170: # Initialize values from language.tab, copyright.tab, filetypes.tab,
                    171: # thesaurus.tab, and filecategories.tab.
                    172: #
1.18      www       173: BEGIN {
1.46      matthew   174:     # Variable initialization
                    175:     $thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
                    176:     #
1.22      www       177:     unless ($readit) {
1.12      harris41  178: # ------------------------------------------------------------------- languages
                    179:     {
1.158     raeburn   180:         my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    181:                                    '/language.tab';
                    182:         if ( open(my $fh,"<$langtabfile") ) {
1.356     albertel  183:             while (my $line = <$fh>) {
                    184:                 next if ($line=~/^\#/);
                    185:                 chomp($line);
                    186:                 my ($key,$two,$country,$three,$enc,$val,$sup)=(split(/\t/,$line));
1.158     raeburn   187:                 $language{$key}=$val.' - '.$enc;
                    188:                 if ($sup) {
                    189:                     $supported_language{$key}=$sup;
                    190:                 }
                    191:             }
                    192:             close($fh);
                    193:         }
1.12      harris41  194:     }
                    195: # ------------------------------------------------------------------ copyrights
                    196:     {
1.158     raeburn   197:         my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    198:                                   '/copyright.tab';
                    199:         if ( open (my $fh,"<$copyrightfile") ) {
1.356     albertel  200:             while (my $line = <$fh>) {
                    201:                 next if ($line=~/^\#/);
                    202:                 chomp($line);
                    203:                 my ($key,$val)=(split(/\s+/,$line,2));
1.158     raeburn   204:                 $cprtag{$key}=$val;
                    205:             }
                    206:             close($fh);
                    207:         }
1.12      harris41  208:     }
1.351     www       209: # ----------------------------------------------------------- source copyrights
1.192     taceyjo1  210:     {
                    211:         my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    212:                                   '/source_copyright.tab';
                    213:         if ( open (my $fh,"<$sourcecopyrightfile") ) {
1.356     albertel  214:             while (my $line = <$fh>) {
                    215:                 next if ($line =~ /^\#/);
                    216:                 chomp($line);
                    217:                 my ($key,$val)=(split(/\s+/,$line,2));
1.192     taceyjo1  218:                 $scprtag{$key}=$val;
                    219:             }
                    220:             close($fh);
                    221:         }
                    222:     }
1.63      www       223: 
1.517     raeburn   224: # -------------------------------------------------------------- default domain designs
1.63      www       225:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
1.517     raeburn   226:     my $designfile = $designdir.'/default.tab';
                    227:     if ( open (my $fh,"<$designfile") ) {
                    228:         while (my $line = <$fh>) {
                    229:             next if ($line =~ /^\#/);
                    230:             chomp($line);
                    231:             my ($key,$val)=(split(/\=/,$line));
                    232:             if ($val) { $defaultdesign{$key}=$val; }
                    233:         }
                    234:         close($fh);
1.63      www       235:     }
                    236: 
1.15      harris41  237: # ------------------------------------------------------------- file categories
                    238:     {
1.158     raeburn   239:         my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    240:                                   '/filecategories.tab';
                    241:         if ( open (my $fh,"<$categoryfile") ) {
1.356     albertel  242: 	    while (my $line = <$fh>) {
                    243: 		next if ($line =~ /^\#/);
                    244: 		chomp($line);
                    245:                 my ($extension,$category)=(split(/\s+/,$line,2));
1.158     raeburn   246:                 push @{$category_extensions{lc($category)}},$extension;
                    247:             }
                    248:             close($fh);
                    249:         }
                    250: 
1.15      harris41  251:     }
1.12      harris41  252: # ------------------------------------------------------------------ file types
                    253:     {
1.158     raeburn   254:         my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    255:                '/filetypes.tab';
                    256:         if ( open (my $fh,"<$typesfile") ) {
1.356     albertel  257:             while (my $line = <$fh>) {
                    258: 		next if ($line =~ /^\#/);
                    259: 		chomp($line);
                    260:                 my ($ending,$emb,$mime,$descr)=split(/\s+/,$line,4);
1.158     raeburn   261:                 if ($descr ne '') {
                    262:                     $fe{$ending}=lc($emb);
                    263:                     $fd{$ending}=$descr;
1.351     www       264:                     if ($mime ne 'unk') { $fm{$ending}=$mime; }
1.158     raeburn   265:                 }
                    266:             }
                    267:             close($fh);
                    268:         }
1.12      harris41  269:     }
1.22      www       270:     &Apache::lonnet::logthis(
1.46      matthew   271:               "<font color=yellow>INFO: Read file types</font>");
1.22      www       272:     $readit=1;
1.46      matthew   273:     }  # end of unless($readit) 
1.32      matthew   274:     
                    275: }
1.112     bowersj2  276: 
1.42      matthew   277: ###############################################################
                    278: ##           HTML and Javascript Helper Functions            ##
                    279: ###############################################################
                    280: 
                    281: =pod 
                    282: 
1.112     bowersj2  283: =head1 HTML and Javascript Functions
1.42      matthew   284: 
1.112     bowersj2  285: =over 4
                    286: 
1.648     raeburn   287: =item * &browser_and_searcher_javascript()
1.112     bowersj2  288: 
                    289: X<browsing, javascript>X<searching, javascript>Returns a string
                    290: containing javascript with two functions, C<openbrowser> and
                    291: C<opensearcher>. Returned string does not contain E<lt>scriptE<gt>
                    292: tags.
1.42      matthew   293: 
1.648     raeburn   294: =item * &openbrowser(formname,elementname,only,omit) [javascript]
1.42      matthew   295: 
                    296: inputs: formname, elementname, only, omit
                    297: 
                    298: formname and elementname indicate the name of the html form and name of
                    299: the element that the results of the browsing selection are to be placed in. 
                    300: 
                    301: Specifying 'only' will restrict the browser to displaying only files
1.185     www       302: with the given extension.  Can be a comma separated list.
1.42      matthew   303: 
                    304: Specifying 'omit' will restrict the browser to NOT displaying files
1.185     www       305: with the given extension.  Can be a comma separated list.
1.42      matthew   306: 
1.648     raeburn   307: =item * &opensearcher(formname,elementname) [javascript]
1.42      matthew   308: 
                    309: Inputs: formname, elementname
                    310: 
                    311: formname and elementname specify the name of the html form and the name
                    312: of the element the selection from the search results will be placed in.
1.542     raeburn   313: 
1.42      matthew   314: =cut
                    315: 
                    316: sub browser_and_searcher_javascript {
1.199     albertel  317:     my ($mode)=@_;
                    318:     if (!defined($mode)) { $mode='edit'; }
1.453     albertel  319:     my $resurl=&escape_single(&lastresurl());
1.42      matthew   320:     return <<END;
1.219     albertel  321: // <!-- BEGIN LON-CAPA Internal
1.50      matthew   322:     var editbrowser = null;
1.135     albertel  323:     function openbrowser(formname,elementname,only,omit,titleelement) {
1.170     www       324:         var url = '$resurl/?';
1.42      matthew   325:         if (editbrowser == null) {
                    326:             url += 'launch=1&';
                    327:         }
                    328:         url += 'catalogmode=interactive&';
1.199     albertel  329:         url += 'mode=$mode&';
1.611     albertel  330:         url += 'inhibitmenu=yes&';
1.42      matthew   331:         url += 'form=' + formname + '&';
                    332:         if (only != null) {
                    333:             url += 'only=' + only + '&';
1.217     albertel  334:         } else {
                    335:             url += 'only=&';
                    336: 	}
1.42      matthew   337:         if (omit != null) {
                    338:             url += 'omit=' + omit + '&';
1.217     albertel  339:         } else {
                    340:             url += 'omit=&';
                    341: 	}
1.135     albertel  342:         if (titleelement != null) {
                    343:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  344:         } else {
                    345: 	    url += 'titleelement=&';
                    346: 	}
1.42      matthew   347:         url += 'element=' + elementname + '';
                    348:         var title = 'Browser';
1.435     albertel  349:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
1.42      matthew   350:         options += ',width=700,height=600';
                    351:         editbrowser = open(url,title,options,'1');
                    352:         editbrowser.focus();
                    353:     }
                    354:     var editsearcher;
1.135     albertel  355:     function opensearcher(formname,elementname,titleelement) {
1.42      matthew   356:         var url = '/adm/searchcat?';
                    357:         if (editsearcher == null) {
                    358:             url += 'launch=1&';
                    359:         }
                    360:         url += 'catalogmode=interactive&';
1.199     albertel  361:         url += 'mode=$mode&';
1.42      matthew   362:         url += 'form=' + formname + '&';
1.135     albertel  363:         if (titleelement != null) {
                    364:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  365:         } else {
                    366: 	    url += 'titleelement=&';
                    367: 	}
1.42      matthew   368:         url += 'element=' + elementname + '';
                    369:         var title = 'Search';
1.435     albertel  370:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
1.42      matthew   371:         options += ',width=700,height=600';
                    372:         editsearcher = open(url,title,options,'1');
                    373:         editsearcher.focus();
                    374:     }
1.219     albertel  375: // END LON-CAPA Internal -->
1.42      matthew   376: END
1.170     www       377: }
                    378: 
                    379: sub lastresurl {
1.258     albertel  380:     if ($env{'environment.lastresurl'}) {
                    381: 	return $env{'environment.lastresurl'}
1.170     www       382:     } else {
                    383: 	return '/res';
                    384:     }
                    385: }
                    386: 
                    387: sub storeresurl {
                    388:     my $resurl=&Apache::lonnet::clutter(shift);
                    389:     unless ($resurl=~/^\/res/) { return 0; }
                    390:     $resurl=~s/\/$//;
                    391:     &Apache::lonnet::put('environment',{'lastresurl' => $resurl});
1.646     raeburn   392:     &Apache::lonnet::appenv({'environment.lastresurl' => $resurl});
1.170     www       393:     return 1;
1.42      matthew   394: }
                    395: 
1.74      www       396: sub studentbrowser_javascript {
1.111     www       397:    unless (
1.258     albertel  398:             (($env{'request.course.id'}) && 
1.302     albertel  399:              (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    400: 	      || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    401: 					  '/'.$env{'request.course.sec'})
                    402: 	      ))
1.258     albertel  403:          || ($env{'request.role'}=~/^(au|dc|su)/)
1.111     www       404:           ) { return ''; }  
1.74      www       405:    return (<<'ENDSTDBRW');
                    406: <script type="text/javascript" language="Javascript" >
                    407:     var stdeditbrowser;
1.558     albertel  408:     function openstdbrowser(formname,uname,udom,roleflag,ignorefilter) {
1.74      www       409:         var url = '/adm/pickstudent?';
                    410:         var filter;
1.558     albertel  411: 	if (!ignorefilter) {
                    412: 	    eval('filter=document.'+formname+'.'+uname+'.value;');
                    413: 	}
1.74      www       414:         if (filter != null) {
                    415:            if (filter != '') {
                    416:                url += 'filter='+filter+'&';
                    417: 	   }
                    418:         }
                    419:         url += 'form=' + formname + '&unameelement='+uname+
                    420:                                     '&udomelement='+udom;
1.111     www       421: 	if (roleflag) { url+="&roles=1"; }
1.102     www       422:         var title = 'Student_Browser';
1.74      www       423:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    424:         options += ',width=700,height=600';
                    425:         stdeditbrowser = open(url,title,options,'1');
                    426:         stdeditbrowser.focus();
                    427:     }
                    428: </script>
                    429: ENDSTDBRW
                    430: }
1.42      matthew   431: 
1.74      www       432: sub selectstudent_link {
1.111     www       433:    my ($form,$unameele,$udomele)=@_;
1.258     albertel  434:    if ($env{'request.course.id'}) {  
1.302     albertel  435:        if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    436: 	   && !&Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    437: 					'/'.$env{'request.course.sec'})) {
1.111     www       438: 	   return '';
                    439:        }
                    440:        return "<a href='".'javascript:openstdbrowser("'.$form.'","'.$unameele.
1.607     albertel  441:         '","'.$udomele.'");'."'>".&mt('Select User')."</a>";
1.74      www       442:    }
1.258     albertel  443:    if ($env{'request.role'}=~/^(au|dc|su)/) {
1.111     www       444:        return "<a href='".'javascript:openstdbrowser("'.$form.'","'.$unameele.
1.119     www       445:         '","'.$udomele.'",1);'."'>".&mt('Select User')."</a>";
1.111     www       446:    }
                    447:    return '';
1.91      www       448: }
                    449: 
                    450: sub coursebrowser_javascript {
1.468     raeburn   451:     my ($domainfilter,$sec_element,$formname)=@_;
1.377     raeburn   452:     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   453:    my $output = '
1.538     albertel  454: <script type="text/javascript">
1.468     raeburn   455:     var stdeditbrowser;'."\n";
                    456:    $output .= <<"ENDSTDBRW";
1.377     raeburn   457:     function opencrsbrowser(formname,uname,udom,desc,extra_element,multflag,crstype) {
1.91      www       458:         var url = '/adm/pickcourse?';
1.468     raeburn   459:         var domainfilter = '';
                    460:         var formid = getFormIdByName(formname);
                    461:         if (formid > -1) {
                    462:             var domid = getIndexByName(formid,udom);
                    463:             if (domid > -1) {
                    464:                 if (document.forms[formid].elements[domid].type == 'select-one') {
                    465:                     domainfilter=document.forms[formid].elements[domid].options[document.forms[formid].elements[domid].selectedIndex].value;
                    466:                 }
                    467:                 if (document.forms[formid].elements[domid].type == 'hidden') {
                    468:                     domainfilter=document.forms[formid].elements[domid].value;
                    469:                 }
                    470:             }
1.91      www       471:         }
1.128     albertel  472:         if (domainfilter != null) {
                    473:            if (domainfilter != '') {
                    474:                url += 'domainfilter='+domainfilter+'&';
                    475: 	   }
                    476:         }
1.91      www       477:         url += 'form=' + formname + '&cnumelement='+uname+
1.187     albertel  478: 	                            '&cdomelement='+udom+
                    479:                                     '&cnameelement='+desc;
1.468     raeburn   480:         if (extra_element !=null && extra_element != '') {
1.594     raeburn   481:             if (formname == 'rolechoice' || formname == 'studentform') {
1.468     raeburn   482:                 url += '&roleelement='+extra_element;
                    483:                 if (domainfilter == null || domainfilter == '') {
                    484:                     url += '&domainfilter='+extra_element;
                    485:                 }
1.234     raeburn   486:             }
1.468     raeburn   487:             else {
                    488:                 if (formname == 'portform') {
                    489:                     url += '&setroles='+extra_element;
                    490:                 }
                    491:             }     
1.230     raeburn   492:         }
1.293     raeburn   493:         if (multflag !=null && multflag != '') {
                    494:             url += '&multiple='+multflag;
                    495:         }
1.377     raeburn   496:         if (crstype == 'Course/Group') {
                    497:             if (formname == 'cu') {
                    498:                 crstype = document.cu.crstype.options[document.cu.crstype.selectedIndex].value; 
                    499:                 if (crstype == "") {
                    500:                     alert("$crs_or_grp_alert");
                    501:                     return;
                    502:                 }
                    503:             }
                    504:         }
                    505:         if (crstype !=null && crstype != '') {
                    506:             url += '&type='+crstype;
                    507:         }
1.102     www       508:         var title = 'Course_Browser';
1.91      www       509:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    510:         options += ',width=700,height=600';
                    511:         stdeditbrowser = open(url,title,options,'1');
                    512:         stdeditbrowser.focus();
                    513:     }
1.468     raeburn   514: 
                    515:     function getFormIdByName(formname) {
                    516:         for (var i=0;i<document.forms.length;i++) {
                    517:             if (document.forms[i].name == formname) {
                    518:                 return i;
                    519:             }
                    520:         }
                    521:         return -1; 
                    522:     }
                    523: 
                    524:     function getIndexByName(formid,item) {
                    525:         for (var i=0;i<document.forms[formid].elements.length;i++) {
                    526:             if (document.forms[formid].elements[i].name == item) {
                    527:                 return i;
                    528:             }
                    529:         }
                    530:         return -1;
                    531:     }
1.91      www       532: ENDSTDBRW
1.468     raeburn   533:     if ($sec_element ne '') {
                    534:         $output .= &setsec_javascript($sec_element,$formname);
                    535:     }
                    536:     $output .= '
                    537: </script>';
                    538:     return $output;
                    539: }
                    540: 
                    541: sub setsec_javascript {
                    542:     my ($sec_element,$formname) = @_;
                    543:     my $setsections = qq|
                    544: function setSect(sectionlist) {
1.629     raeburn   545:     var sectionsArray = new Array();
                    546:     if ((sectionlist != '') && (typeof sectionlist != "undefined")) {
                    547:         sectionsArray = sectionlist.split(",");
                    548:     }
1.468     raeburn   549:     var numSections = sectionsArray.length;
                    550:     document.$formname.$sec_element.length = 0;
                    551:     if (numSections == 0) {
                    552:         document.$formname.$sec_element.multiple=false;
                    553:         document.$formname.$sec_element.size=1;
                    554:         document.$formname.$sec_element.options[0] = new Option('No existing sections','',false,false)
                    555:     } else {
                    556:         if (numSections == 1) {
                    557:             document.$formname.$sec_element.multiple=false;
                    558:             document.$formname.$sec_element.size=1;
                    559:             document.$formname.$sec_element.options[0] = new Option('Select','',true,true);
                    560:             document.$formname.$sec_element.options[1] = new Option('No section','',false,false)
                    561:             document.$formname.$sec_element.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
                    562:         } else {
                    563:             for (var i=0; i<numSections; i++) {
                    564:                 document.$formname.$sec_element.options[i] = new Option(sectionsArray[i],sectionsArray[i],false,false)
                    565:             }
                    566:             document.$formname.$sec_element.multiple=true
                    567:             if (numSections < 3) {
                    568:                 document.$formname.$sec_element.size=numSections;
                    569:             } else {
                    570:                 document.$formname.$sec_element.size=3;
                    571:             }
                    572:             document.$formname.$sec_element.options[0].selected = false
                    573:         }
                    574:     }
1.91      www       575: }
1.468     raeburn   576: |;
                    577:     return $setsections;
                    578: }
                    579: 
1.91      www       580: 
                    581: sub selectcourse_link {
1.377     raeburn   582:    my ($form,$unameele,$udomele,$desc,$extra_element,$multflag,$selecttype)=@_;
1.492     albertel  583:    return "<a href='".'javascript:opencrsbrowser("'.$form.'","'.$unameele.
                    584:         '","'.$udomele.'","'.$desc.'","'.$extra_element.'","'.$multflag.'","'.$selecttype.'");'."'>".&mt('Select Course')."</a>";
1.74      www       585: }
1.42      matthew   586: 
1.273     raeburn   587: sub check_uncheck_jscript {
                    588:     my $jscript = <<"ENDSCRT";
                    589: function checkAll(field) {
                    590:     if (field.length > 0) {
                    591:         for (i = 0; i < field.length; i++) {
                    592:             field[i].checked = true ;
                    593:         }
                    594:     } else {
                    595:         field.checked = true
                    596:     }
                    597: }
                    598:  
                    599: function uncheckAll(field) {
                    600:     if (field.length > 0) {
                    601:         for (i = 0; i < field.length; i++) {
                    602:             field[i].checked = false ;
1.543     albertel  603:         }
                    604:     } else {
1.273     raeburn   605:         field.checked = false ;
                    606:     }
                    607: }
                    608: ENDSCRT
                    609:     return $jscript;
                    610: }
                    611: 
                    612: 
1.42      matthew   613: =pod
1.36      matthew   614: 
1.648     raeburn   615: =item * &linked_select_forms(...)
1.36      matthew   616: 
                    617: linked_select_forms returns a string containing a <script></script> block
                    618: and html for two <select> menus.  The select menus will be linked in that
                    619: changing the value of the first menu will result in new values being placed
                    620: in the second menu.  The values in the select menu will appear in alphabetical
1.609     raeburn   621: order unless a defined order is provided.
1.36      matthew   622: 
                    623: linked_select_forms takes the following ordered inputs:
                    624: 
                    625: =over 4
                    626: 
1.112     bowersj2  627: =item * $formname, the name of the <form> tag
1.36      matthew   628: 
1.112     bowersj2  629: =item * $middletext, the text which appears between the <select> tags
1.36      matthew   630: 
1.112     bowersj2  631: =item * $firstdefault, the default value for the first menu
1.36      matthew   632: 
1.112     bowersj2  633: =item * $firstselectname, the name of the first <select> tag
1.36      matthew   634: 
1.112     bowersj2  635: =item * $secondselectname, the name of the second <select> tag
1.36      matthew   636: 
1.112     bowersj2  637: =item * $hashref, a reference to a hash containing the data for the menus.
1.36      matthew   638: 
1.609     raeburn   639: =item * $menuorder, the order of values in the first menu
                    640: 
1.41      ng        641: =back 
                    642: 
1.36      matthew   643: Below is an example of such a hash.  Only the 'text', 'default', and 
                    644: 'select2' keys must appear as stated.  keys(%menu) are the possible 
                    645: values for the first select menu.  The text that coincides with the 
1.41      ng        646: first menu value is given in $menu{$choice1}->{'text'}.  The values 
1.36      matthew   647: and text for the second menu are given in the hash pointed to by 
                    648: $menu{$choice1}->{'select2'}.  
                    649: 
1.112     bowersj2  650:  my %menu = ( A1 => { text =>"Choice A1" ,
                    651:                        default => "B3",
                    652:                        select2 => { 
                    653:                            B1 => "Choice B1",
                    654:                            B2 => "Choice B2",
                    655:                            B3 => "Choice B3",
                    656:                            B4 => "Choice B4"
1.609     raeburn   657:                            },
                    658:                        order => ['B4','B3','B1','B2'],
1.112     bowersj2  659:                    },
                    660:                A2 => { text =>"Choice A2" ,
                    661:                        default => "C2",
                    662:                        select2 => { 
                    663:                            C1 => "Choice C1",
                    664:                            C2 => "Choice C2",
                    665:                            C3 => "Choice C3"
1.609     raeburn   666:                            },
                    667:                        order => ['C2','C1','C3'],
1.112     bowersj2  668:                    },
                    669:                A3 => { text =>"Choice A3" ,
                    670:                        default => "D6",
                    671:                        select2 => { 
                    672:                            D1 => "Choice D1",
                    673:                            D2 => "Choice D2",
                    674:                            D3 => "Choice D3",
                    675:                            D4 => "Choice D4",
                    676:                            D5 => "Choice D5",
                    677:                            D6 => "Choice D6",
                    678:                            D7 => "Choice D7"
1.609     raeburn   679:                            },
                    680:                        order => ['D4','D3','D2','D1','D7','D6','D5'],
1.112     bowersj2  681:                    }
                    682:                );
1.36      matthew   683: 
                    684: =cut
                    685: 
                    686: sub linked_select_forms {
                    687:     my ($formname,
                    688:         $middletext,
                    689:         $firstdefault,
                    690:         $firstselectname,
                    691:         $secondselectname, 
1.609     raeburn   692:         $hashref,
                    693:         $menuorder,
1.36      matthew   694:         ) = @_;
                    695:     my $second = "document.$formname.$secondselectname";
                    696:     my $first = "document.$formname.$firstselectname";
                    697:     # output the javascript to do the changing
                    698:     my $result = '';
1.219     albertel  699:     $result.="<script type=\"text/javascript\">\n";
1.36      matthew   700:     $result.="var select2data = new Object();\n";
                    701:     $" = '","';
                    702:     my $debug = '';
                    703:     foreach my $s1 (sort(keys(%$hashref))) {
                    704:         $result.="select2data.d_$s1 = new Object();\n";        
                    705:         $result.="select2data.d_$s1.def = new String('".
                    706:             $hashref->{$s1}->{'default'}."');\n";
1.609     raeburn   707:         $result.="select2data.d_$s1.values = new Array(";
1.36      matthew   708:         my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
1.609     raeburn   709:         if (ref($hashref->{$s1}->{'order'}) eq 'ARRAY') {
                    710:             @s2values = @{$hashref->{$s1}->{'order'}};
                    711:         }
1.36      matthew   712:         $result.="\"@s2values\");\n";
                    713:         $result.="select2data.d_$s1.texts = new Array(";        
                    714:         my @s2texts;
                    715:         foreach my $value (@s2values) {
                    716:             push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
                    717:         }
                    718:         $result.="\"@s2texts\");\n";
                    719:     }
                    720:     $"=' ';
                    721:     $result.= <<"END";
                    722: 
                    723: function select1_changed() {
                    724:     // Determine new choice
                    725:     var newvalue = "d_" + $first.value;
                    726:     // update select2
                    727:     var values     = select2data[newvalue].values;
                    728:     var texts      = select2data[newvalue].texts;
                    729:     var select2def = select2data[newvalue].def;
                    730:     var i;
                    731:     // out with the old
                    732:     for (i = 0; i < $second.options.length; i++) {
                    733:         $second.options[i] = null;
                    734:     }
                    735:     // in with the nuclear
                    736:     for (i=0;i<values.length; i++) {
                    737:         $second.options[i] = new Option(values[i]);
1.143     matthew   738:         $second.options[i].value = values[i];
1.36      matthew   739:         $second.options[i].text = texts[i];
                    740:         if (values[i] == select2def) {
                    741:             $second.options[i].selected = true;
                    742:         }
                    743:     }
                    744: }
                    745: </script>
                    746: END
                    747:     # output the initial values for the selection lists
                    748:     $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed()\">\n";
1.609     raeburn   749:     my @order = sort(keys(%{$hashref}));
                    750:     if (ref($menuorder) eq 'ARRAY') {
                    751:         @order = @{$menuorder};
                    752:     }
                    753:     foreach my $value (@order) {
1.36      matthew   754:         $result.="    <option value=\"$value\" ";
1.253     albertel  755:         $result.=" selected=\"selected\" " if ($value eq $firstdefault);
1.119     www       756:         $result.=">".&mt($hashref->{$value}->{'text'})."</option>\n";
1.36      matthew   757:     }
                    758:     $result .= "</select>\n";
                    759:     my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
                    760:     $result .= $middletext;
                    761:     $result .= "<select size=\"1\" name=\"$secondselectname\">\n";
                    762:     my $seconddefault = $hashref->{$firstdefault}->{'default'};
1.609     raeburn   763:     
                    764:     my @secondorder = sort(keys(%select2));
                    765:     if (ref($hashref->{$firstdefault}->{'order'}) eq 'ARRAY') {
                    766:         @secondorder = @{$hashref->{$firstdefault}->{'order'}};
                    767:     }
                    768:     foreach my $value (@secondorder) {
1.36      matthew   769:         $result.="    <option value=\"$value\" ";        
1.253     albertel  770:         $result.=" selected=\"selected\" " if ($value eq $seconddefault);
1.119     www       771:         $result.=">".&mt($select2{$value})."</option>\n";
1.36      matthew   772:     }
                    773:     $result .= "</select>\n";
                    774:     #    return $debug;
                    775:     return $result;
                    776: }   #  end of sub linked_select_forms {
                    777: 
1.45      matthew   778: =pod
1.44      bowersj2  779: 
1.648     raeburn   780: =item * &help_open_topic($topic,$text,$stayOnPage,$width,$height)
1.44      bowersj2  781: 
1.112     bowersj2  782: Returns a string corresponding to an HTML link to the given help
                    783: $topic, where $topic corresponds to the name of a .tex file in
                    784: /home/httpd/html/adm/help/tex, with underscores replaced by
                    785: spaces. 
                    786: 
                    787: $text will optionally be linked to the same topic, allowing you to
                    788: link text in addition to the graphic. If you do not want to link
                    789: text, but wish to specify one of the later parameters, pass an
                    790: empty string. 
                    791: 
                    792: $stayOnPage is a value that will be interpreted as a boolean. If true,
                    793: the link will not open a new window. If false, the link will open
                    794: a new window using Javascript. (Default is false.) 
                    795: 
                    796: $width and $height are optional numerical parameters that will
                    797: override the width and height of the popped up window, which may
                    798: be useful for certain help topics with big pictures included. 
1.44      bowersj2  799: 
                    800: =cut
                    801: 
                    802: sub help_open_topic {
1.48      bowersj2  803:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
                    804:     $text = "" if (not defined $text);
1.44      bowersj2  805:     $stayOnPage = 0 if (not defined $stayOnPage);
1.552     banghart  806:     if ($env{'browser.interface'} eq 'textual') {
1.79      www       807: 	$stayOnPage=1;
                    808:     }
1.44      bowersj2  809:     $width = 350 if (not defined $width);
                    810:     $height = 400 if (not defined $height);
                    811:     my $filename = $topic;
                    812:     $filename =~ s/ /_/g;
                    813: 
1.48      bowersj2  814:     my $template = "";
                    815:     my $link;
1.572     banghart  816:     
1.159     www       817:     $topic=~s/\W/\_/g;
1.44      bowersj2  818: 
1.572     banghart  819:     if (!$stayOnPage) {
1.72      bowersj2  820: 	$link = "javascript:void(open('/adm/help/${filename}.hlp', 'Help_for_$topic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
1.572     banghart  821:     } else {
1.48      bowersj2  822: 	$link = "/adm/help/${filename}.hlp";
                    823:     }
                    824: 
                    825:     # Add the text
1.572     banghart  826:     if ($text ne "") {
1.77      www       827: 	$template .= 
1.572     banghart  828:             "<table bgcolor='#3333AA' cellspacing='1' cellpadding='1' border='0'><tr>".
                    829:             "<td bgcolor='#5555FF'><a target=\"_top\" href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.48      bowersj2  830:     }
                    831: 
                    832:     # Add the graphic
1.179     matthew   833:     my $title = &mt('Online Help');
1.649     www       834:     my $helpicon=&lonhttpdurl("/res/adm/pages/help.png");
1.48      bowersj2  835:     $template .= <<"ENDTEMPLATE";
1.436     albertel  836:  <a target="_top" href="$link" title="$title"><img src="$helpicon" border="0" alt="(Help: $topic)" /></a>
1.44      bowersj2  837: ENDTEMPLATE
1.78      www       838:     if ($text ne '') { $template.='</td></tr></table>' };
1.44      bowersj2  839:     return $template;
                    840: 
1.106     bowersj2  841: }
                    842: 
                    843: # This is a quicky function for Latex cheatsheet editing, since it 
                    844: # appears in at least four places
                    845: sub helpLatexCheatsheet {
                    846:     my $other = shift;
                    847:     my $addOther = '';
                    848:     if ($other) {
                    849: 	$addOther = Apache::loncommon::help_open_topic($other, shift,
                    850: 						       undef, undef, 600) .
                    851: 							   '</td><td>';
                    852:     }
                    853:     return '<table><tr><td>'.
                    854: 	$addOther .
1.636     raeburn   855: 	&Apache::loncommon::help_open_topic("Greek_Symbols",&mt('Greek Symbols'),
1.106     bowersj2  856: 					    undef,undef,600)
                    857: 	.'</td><td>'.
1.636     raeburn   858: 	&Apache::loncommon::help_open_topic("Other_Symbols",&mt('Other Symbols'),
1.106     bowersj2  859: 					    undef,undef,600)
                    860: 	.'</td></tr></table>';
1.172     www       861: }
                    862: 
1.430     albertel  863: sub general_help {
                    864:     my $helptopic='Student_Intro';
                    865:     if ($env{'request.role'}=~/^(ca|au)/) {
                    866: 	$helptopic='Authoring_Intro';
                    867:     } elsif ($env{'request.role'}=~/^cc/) {
                    868: 	$helptopic='Course_Coordination_Intro';
                    869:     }
                    870:     return $helptopic;
                    871: }
                    872: 
                    873: sub update_help_link {
                    874:     my ($topic,$component_help,$faq,$bug,$stayOnPage) = @_;
                    875:     my $origurl = $ENV{'REQUEST_URI'};
                    876:     $origurl=~s|^/~|/priv/|;
                    877:     my $timestamp = time;
                    878:     foreach my $datum (\$topic,\$component_help,\$faq,\$bug,\$origurl) {
                    879:         $$datum = &escape($$datum);
                    880:     }
                    881: 
                    882:     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";
                    883:     my $output .= <<"ENDOUTPUT";
                    884: <script type="text/javascript">
                    885: banner_link = '$banner_link';
                    886: </script>
                    887: ENDOUTPUT
                    888:     return $output;
                    889: }
                    890: 
                    891: # now just updates the help link and generates a blue icon
1.193     raeburn   892: sub help_open_menu {
1.430     albertel  893:     my ($topic,$component_help,$faq,$bug,$stayOnPage,$width,$height,$text) 
1.552     banghart  894: 	= @_;    
1.430     albertel  895:     $stayOnPage = 0 if (not defined $stayOnPage);
1.572     banghart  896:     # only use pop-up help (stayOnPage == 0)
1.552     banghart  897:     # if environment.remote is on (using remote control UI)
1.572     banghart  898:     if ($env{'browser.interface'} eq 'textual' ||
                    899:     	$env{'environment.remote'} eq 'off' ) {
1.552     banghart  900:         $stayOnPage=1;
1.430     albertel  901:     }
                    902:     my $output;
                    903:     if ($component_help) {
                    904: 	if (!$text) {
                    905: 	    $output=&help_open_topic($component_help,undef,$stayOnPage,
                    906: 				       $width,$height);
                    907: 	} else {
                    908: 	    my $help_text;
                    909: 	    $help_text=&unescape($topic);
                    910: 	    $output='<table><tr><td>'.
                    911: 		&help_open_topic($component_help,$help_text,$stayOnPage,
                    912: 				 $width,$height).'</td></tr></table>';
                    913: 	}
                    914:     }
                    915:     my $banner_link = &update_help_link($topic,$component_help,$faq,$bug,$stayOnPage);
                    916:     return $output.$banner_link;
                    917: }
                    918: 
                    919: sub top_nav_help {
                    920:     my ($text) = @_;
1.436     albertel  921:     $text = &mt($text);
1.572     banghart  922:     my $stay_on_page = 
1.436     albertel  923: 	($env{'browser.interface'}  eq 'textual' ||
                    924: 	 $env{'environment.remote'} eq 'off' );
1.572     banghart  925:     my $link = ($stay_on_page) ? "javascript:helpMenu('display')"
1.436     albertel  926: 	                     : "javascript:helpMenu('open')";
1.572     banghart  927:     my $banner_link = &update_help_link(undef,undef,undef,undef,$stay_on_page);
1.436     albertel  928: 
1.201     raeburn   929:     my $title = &mt('Get help');
1.436     albertel  930: 
                    931:     return <<"END";
                    932: $banner_link
                    933:  <a href="$link" title="$title">$text</a>
                    934: END
                    935: }
                    936: 
                    937: sub help_menu_js {
                    938:     my ($text) = @_;
                    939: 
                    940:     my $stayOnPage = 
                    941: 	($env{'browser.interface'}  eq 'textual' ||
                    942: 	 $env{'environment.remote'} eq 'off' );
                    943: 
                    944:     my $width = 620;
                    945:     my $height = 600;
1.430     albertel  946:     my $helptopic=&general_help();
                    947:     my $details_link = '/adm/help/'.$helptopic.'.hlp';
1.261     albertel  948:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.331     albertel  949:     my $start_page =
                    950:         &Apache::loncommon::start_page('Help Menu', undef,
                    951: 				       {'frameset'    => 1,
                    952: 					'js_ready'    => 1,
                    953: 					'add_entries' => {
                    954: 					    'border' => '0',
1.579     raeburn   955: 					    'rows'   => "110,*",},});
1.331     albertel  956:     my $end_page =
                    957:         &Apache::loncommon::end_page({'frameset' => 1,
                    958: 				      'js_ready' => 1,});
                    959: 
1.436     albertel  960:     my $template .= <<"ENDTEMPLATE";
                    961: <script type="text/javascript">
1.253     albertel  962: // <!-- BEGIN LON-CAPA Internal
                    963: // <![CDATA[
1.430     albertel  964: var banner_link = '';
1.243     raeburn   965: function helpMenu(target) {
                    966:     var caller = this;
                    967:     if (target == 'open') {
                    968:         var newWindow = null;
                    969:         try {
1.262     albertel  970:             newWindow =  window.open($nothing,"helpmenu","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" )
1.243     raeburn   971:         }
                    972:         catch(error) {
                    973:             writeHelp(caller);
                    974:             return;
                    975:         }
                    976:         if (newWindow) {
                    977:             caller = newWindow;
                    978:         }
1.193     raeburn   979:     }
1.243     raeburn   980:     writeHelp(caller);
                    981:     return;
                    982: }
                    983: function writeHelp(caller) {
1.430     albertel  984:     caller.document.writeln('$start_page<frame name="bannerframe"  src="'+banner_link+'" /><frame name="bodyframe" src="$details_link" /> $end_page')
1.243     raeburn   985:     caller.document.close()
                    986:     caller.focus()
1.193     raeburn   987: }
1.253     albertel  988: // ]]>
1.219     albertel  989: // END LON-CAPA Internal -->
1.436     albertel  990: </script>
1.193     raeburn   991: ENDTEMPLATE
                    992:     return $template;
                    993: }
                    994: 
1.172     www       995: sub help_open_bug {
                    996:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel  997:     unless ($env{'user.adv'}) { return ''; }
1.172     www       998:     unless ($Apache::lonnet::perlvar{'BugzillaHost'}) { return ''; }
                    999:     $text = "" if (not defined $text);
                   1000:     $stayOnPage = 0 if (not defined $stayOnPage);
1.258     albertel 1001:     if ($env{'browser.interface'} eq 'textual' ||
                   1002: 	$env{'environment.remote'} eq 'off' ) {
1.172     www      1003: 	$stayOnPage=1;
                   1004:     }
1.184     albertel 1005:     $width = 600 if (not defined $width);
                   1006:     $height = 600 if (not defined $height);
1.172     www      1007: 
                   1008:     $topic=~s/\W+/\+/g;
                   1009:     my $link='';
                   1010:     my $template='';
1.379     albertel 1011:     my $url=$Apache::lonnet::perlvar{'BugzillaHost'}.'enter_bug.cgi?product=LON-CAPA&amp;bug_file_loc='.
                   1012: 	&escape($ENV{'REQUEST_URI'}).'&amp;component='.$topic;
1.172     www      1013:     if (!$stayOnPage)
                   1014:     {
                   1015: 	$link = "javascript:void(open('$url', 'Bugzilla', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                   1016:     }
                   1017:     else
                   1018:     {
                   1019: 	$link = $url;
                   1020:     }
                   1021:     # Add the text
                   1022:     if ($text ne "")
                   1023:     {
                   1024: 	$template .= 
                   1025:   "<table bgcolor='#AA3333' cellspacing='1' cellpadding='1' border='0'><tr>".
1.436     albertel 1026:   "<td bgcolor='#FF5555'><a target=\"_top\" href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.172     www      1027:     }
                   1028: 
                   1029:     # Add the graphic
1.179     matthew  1030:     my $title = &mt('Report a Bug');
1.215     albertel 1031:     my $bugicon=&lonhttpdurl("/adm/lonMisc/smallBug.gif");
1.172     www      1032:     $template .= <<"ENDTEMPLATE";
1.436     albertel 1033:  <a target="_top" href="$link" title="$title"><img src="$bugicon" border="0" alt="(Bug: $topic)" /></a>
1.172     www      1034: ENDTEMPLATE
                   1035:     if ($text ne '') { $template.='</td></tr></table>' };
                   1036:     return $template;
                   1037: 
                   1038: }
                   1039: 
                   1040: sub help_open_faq {
                   1041:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel 1042:     unless ($env{'user.adv'}) { return ''; }
1.172     www      1043:     unless ($Apache::lonnet::perlvar{'FAQHost'}) { return ''; }
                   1044:     $text = "" if (not defined $text);
                   1045:     $stayOnPage = 0 if (not defined $stayOnPage);
1.258     albertel 1046:     if ($env{'browser.interface'} eq 'textual' ||
                   1047: 	$env{'environment.remote'} eq 'off' ) {
1.172     www      1048: 	$stayOnPage=1;
                   1049:     }
                   1050:     $width = 350 if (not defined $width);
                   1051:     $height = 400 if (not defined $height);
                   1052: 
                   1053:     $topic=~s/\W+/\+/g;
                   1054:     my $link='';
                   1055:     my $template='';
                   1056:     my $url=$Apache::lonnet::perlvar{'FAQHost'}.'/fom/cache/'.$topic.'.html';
                   1057:     if (!$stayOnPage)
                   1058:     {
                   1059: 	$link = "javascript:void(open('$url', 'FAQ-O-Matic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                   1060:     }
                   1061:     else
                   1062:     {
                   1063: 	$link = $url;
                   1064:     }
                   1065: 
                   1066:     # Add the text
                   1067:     if ($text ne "")
                   1068:     {
                   1069: 	$template .= 
1.173     www      1070:   "<table bgcolor='#337733' cellspacing='1' cellpadding='1' border='0'><tr>".
1.436     albertel 1071:   "<td bgcolor='#448844'><a target=\"_top\" href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.172     www      1072:     }
                   1073: 
                   1074:     # Add the graphic
1.179     matthew  1075:     my $title = &mt('View the FAQ');
1.215     albertel 1076:     my $faqicon=&lonhttpdurl("/adm/lonMisc/smallFAQ.gif");
1.172     www      1077:     $template .= <<"ENDTEMPLATE";
1.436     albertel 1078:  <a target="_top" href="$link" title="$title"><img src="$faqicon" border="0" alt="(FAQ: $topic)" /></a>
1.172     www      1079: ENDTEMPLATE
                   1080:     if ($text ne '') { $template.='</td></tr></table>' };
                   1081:     return $template;
                   1082: 
1.44      bowersj2 1083: }
1.37      matthew  1084: 
1.180     matthew  1085: ###############################################################
                   1086: ###############################################################
                   1087: 
1.45      matthew  1088: =pod
                   1089: 
1.648     raeburn  1090: =item * &change_content_javascript():
1.256     matthew  1091: 
                   1092: This and the next function allow you to create small sections of an
                   1093: otherwise static HTML page that you can update on the fly with
                   1094: Javascript, even in Netscape 4.
                   1095: 
                   1096: The Javascript fragment returned by this function (no E<lt>scriptE<gt> tag)
                   1097: must be written to the HTML page once. It will prove the Javascript
                   1098: function "change(name, content)". Calling the change function with the
                   1099: name of the section 
                   1100: you want to update, matching the name passed to C<changable_area>, and
                   1101: the new content you want to put in there, will put the content into
                   1102: that area.
                   1103: 
                   1104: B<Note>: Netscape 4 only reserves enough space for the changable area
                   1105: to contain room for the original contents. You need to "make space"
                   1106: for whatever changes you wish to make, and be B<sure> to check your
                   1107: code in Netscape 4. This feature in Netscape 4 is B<not> powerful;
                   1108: it's adequate for updating a one-line status display, but little more.
                   1109: This script will set the space to 100% width, so you only need to
                   1110: worry about height in Netscape 4.
                   1111: 
                   1112: Modern browsers are much less limiting, and if you can commit to the
                   1113: user not using Netscape 4, this feature may be used freely with
                   1114: pretty much any HTML.
                   1115: 
                   1116: =cut
                   1117: 
                   1118: sub change_content_javascript {
                   1119:     # If we're on Netscape 4, we need to use Layer-based code
1.258     albertel 1120:     if ($env{'browser.type'} eq 'netscape' &&
                   1121: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew  1122: 	return (<<NETSCAPE4);
                   1123: 	function change(name, content) {
                   1124: 	    doc = document.layers[name+"___escape"].layers[0].document;
                   1125: 	    doc.open();
                   1126: 	    doc.write(content);
                   1127: 	    doc.close();
                   1128: 	}
                   1129: NETSCAPE4
                   1130:     } else {
                   1131: 	# Otherwise, we need to use semi-standards-compliant code
                   1132: 	# (technically, "innerHTML" isn't standard but the equivalent
                   1133: 	# is really scary, and every useful browser supports it
                   1134: 	return (<<DOMBASED);
                   1135: 	function change(name, content) {
                   1136: 	    element = document.getElementById(name);
                   1137: 	    element.innerHTML = content;
                   1138: 	}
                   1139: DOMBASED
                   1140:     }
                   1141: }
                   1142: 
                   1143: =pod
                   1144: 
1.648     raeburn  1145: =item * &changable_area($name,$origContent):
1.256     matthew  1146: 
                   1147: This provides a "changable area" that can be modified on the fly via
                   1148: the Javascript code provided in C<change_content_javascript>. $name is
                   1149: the name you will use to reference the area later; do not repeat the
                   1150: same name on a given HTML page more then once. $origContent is what
                   1151: the area will originally contain, which can be left blank.
                   1152: 
                   1153: =cut
                   1154: 
                   1155: sub changable_area {
                   1156:     my ($name, $origContent) = @_;
                   1157: 
1.258     albertel 1158:     if ($env{'browser.type'} eq 'netscape' &&
                   1159: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew  1160: 	# If this is netscape 4, we need to use the Layer tag
                   1161: 	return "<ilayer width='100%' id='${name}___escape' overflow='none'><layer width='100%' id='$name' overflow='none'>$origContent</layer></ilayer>";
                   1162:     } else {
                   1163: 	return "<span id='$name'>$origContent</span>";
                   1164:     }
                   1165: }
                   1166: 
                   1167: =pod
                   1168: 
1.648     raeburn  1169: =item * &viewport_geometry_js 
1.590     raeburn  1170: 
                   1171: Provides javascript object (Geometry) which can provide information about the viewport geometry for the client browser.
                   1172: 
                   1173: =cut
                   1174: 
                   1175: 
                   1176: sub viewport_geometry_js { 
                   1177:     return <<"GEOMETRY";
                   1178: var Geometry = {};
                   1179: function init_geometry() {
                   1180:     if (Geometry.init) { return };
                   1181:     Geometry.init=1;
                   1182:     if (window.innerHeight) {
                   1183:         Geometry.getViewportHeight   = function() { return window.innerHeight; };
                   1184:         Geometry.getViewportWidth   = function() { return window.innerWidth; };
                   1185:         Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
                   1186:         Geometry.getVerticalScroll   = function() { return window.pageYOffset; };
                   1187:     }
                   1188:     else if (document.documentElement && document.documentElement.clientHeight) {
                   1189:         Geometry.getViewportHeight =
                   1190:             function() { return document.documentElement.clientHeight; };
                   1191:         Geometry.getViewportWidth =
                   1192:             function() { return document.documentElement.clientWidth; };
                   1193: 
                   1194:         Geometry.getHorizontalScroll =
                   1195:             function() { return document.documentElement.scrollLeft; };
                   1196:         Geometry.getVerticalScroll =
                   1197:             function() { return document.documentElement.scrollTop; };
                   1198:     }
                   1199:     else if (document.body.clientHeight) {
                   1200:         Geometry.getViewportHeight =
                   1201:             function() { return document.body.clientHeight; };
                   1202:         Geometry.getViewportWidth =
                   1203:             function() { return document.body.clientWidth; };
                   1204:         Geometry.getHorizontalScroll =
                   1205:             function() { return document.body.scrollLeft; };
                   1206:         Geometry.getVerticalScroll =
                   1207:             function() { return document.body.scrollTop; };
                   1208:     }
                   1209: }
                   1210: 
                   1211: GEOMETRY
                   1212: }
                   1213: 
                   1214: =pod
                   1215: 
1.648     raeburn  1216: =item * &viewport_size_js()
1.590     raeburn  1217: 
                   1218: Provides a javascript function to set values of two form elements - width and height (elements are passed in as arguments to the javascript function) to the dimensions of the user's browser window. 
                   1219: 
                   1220: =cut
                   1221: 
                   1222: sub viewport_size_js {
                   1223:     my $geometry = &viewport_geometry_js();
                   1224:     return <<"DIMS";
                   1225: 
                   1226: $geometry
                   1227: 
                   1228: function getViewportDims(width,height) {
                   1229:     init_geometry();
                   1230:     width.value = Geometry.getViewportWidth();
                   1231:     height.value = Geometry.getViewportHeight();
                   1232:     return;
                   1233: }
                   1234: 
                   1235: DIMS
                   1236: }
                   1237: 
                   1238: =pod
                   1239: 
1.648     raeburn  1240: =item * &resize_textarea_js()
1.565     albertel 1241: 
                   1242: emits the needed javascript to resize a textarea to be as big as possible
                   1243: 
                   1244: creates a function resize_textrea that takes two IDs first should be
                   1245: the id of the element to resize, second should be the id of a div that
                   1246: surrounds everything that comes after the textarea, this routine needs
                   1247: to be attached to the <body> for the onload and onresize events.
                   1248: 
1.648     raeburn  1249: =back
1.565     albertel 1250: 
                   1251: =cut
                   1252: 
                   1253: sub resize_textarea_js {
1.590     raeburn  1254:     my $geometry = &viewport_geometry_js();
1.565     albertel 1255:     return <<"RESIZE";
                   1256:     <script type="text/javascript">
1.590     raeburn  1257: $geometry
1.565     albertel 1258: 
1.588     albertel 1259: function getX(element) {
                   1260:     var x = 0;
                   1261:     while (element) {
                   1262: 	x += element.offsetLeft;
                   1263: 	element = element.offsetParent;
                   1264:     }
                   1265:     return x;
                   1266: }
                   1267: function getY(element) {
                   1268:     var y = 0;
                   1269:     while (element) {
                   1270: 	y += element.offsetTop;
                   1271: 	element = element.offsetParent;
                   1272:     }
                   1273:     return y;
                   1274: }
                   1275: 
                   1276: 
1.565     albertel 1277: function resize_textarea(textarea_id,bottom_id) {
                   1278:     init_geometry();
                   1279:     var textarea        = document.getElementById(textarea_id);
                   1280:     //alert(textarea);
                   1281: 
1.588     albertel 1282:     var textarea_top    = getY(textarea);
1.565     albertel 1283:     var textarea_height = textarea.offsetHeight;
                   1284:     var bottom          = document.getElementById(bottom_id);
1.588     albertel 1285:     var bottom_top      = getY(bottom);
1.565     albertel 1286:     var bottom_height   = bottom.offsetHeight;
                   1287:     var window_height   = Geometry.getViewportHeight();
1.588     albertel 1288:     var fudge           = 23;
1.565     albertel 1289:     var new_height      = window_height-fudge-textarea_top-bottom_height;
                   1290:     if (new_height < 300) {
                   1291: 	new_height = 300;
                   1292:     }
                   1293:     textarea.style.height=new_height+'px';
                   1294: }
                   1295: </script>
                   1296: RESIZE
                   1297: 
                   1298: }
                   1299: 
                   1300: =pod
                   1301: 
1.256     matthew  1302: =head1 Excel and CSV file utility routines
                   1303: 
                   1304: =over 4
                   1305: 
                   1306: =cut
                   1307: 
                   1308: ###############################################################
                   1309: ###############################################################
                   1310: 
                   1311: =pod
                   1312: 
1.648     raeburn  1313: =item * &csv_translate($text) 
1.37      matthew  1314: 
1.185     www      1315: Translate $text to allow it to be output as a 'comma separated values' 
1.37      matthew  1316: format.
                   1317: 
                   1318: =cut
                   1319: 
1.180     matthew  1320: ###############################################################
                   1321: ###############################################################
1.37      matthew  1322: sub csv_translate {
                   1323:     my $text = shift;
                   1324:     $text =~ s/\"/\"\"/g;
1.209     albertel 1325:     $text =~ s/\n/ /g;
1.37      matthew  1326:     return $text;
                   1327: }
1.180     matthew  1328: 
                   1329: ###############################################################
                   1330: ###############################################################
                   1331: 
                   1332: =pod
                   1333: 
1.648     raeburn  1334: =item * &define_excel_formats()
1.180     matthew  1335: 
                   1336: Define some commonly used Excel cell formats.
                   1337: 
                   1338: Currently supported formats:
                   1339: 
                   1340: =over 4
                   1341: 
                   1342: =item header
                   1343: 
                   1344: =item bold
                   1345: 
                   1346: =item h1
                   1347: 
                   1348: =item h2
                   1349: 
                   1350: =item h3
                   1351: 
1.256     matthew  1352: =item h4
                   1353: 
                   1354: =item i
                   1355: 
1.180     matthew  1356: =item date
                   1357: 
                   1358: =back
                   1359: 
                   1360: Inputs: $workbook
                   1361: 
                   1362: Returns: $format, a hash reference.
                   1363: 
                   1364: =cut
                   1365: 
                   1366: ###############################################################
                   1367: ###############################################################
                   1368: sub define_excel_formats {
                   1369:     my ($workbook) = @_;
                   1370:     my $format;
                   1371:     $format->{'header'} = $workbook->add_format(bold      => 1, 
                   1372:                                                 bottom    => 1,
                   1373:                                                 align     => 'center');
                   1374:     $format->{'bold'} = $workbook->add_format(bold=>1);
                   1375:     $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
                   1376:     $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
                   1377:     $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
1.255     matthew  1378:     $format->{'h4'}   = $workbook->add_format(bold=>1, size=>12);
1.246     matthew  1379:     $format->{'i'}    = $workbook->add_format(italic=>1);
1.180     matthew  1380:     $format->{'date'} = $workbook->add_format(num_format=>
1.207     matthew  1381:                                             'mm/dd/yyyy hh:mm:ss');
1.180     matthew  1382:     return $format;
                   1383: }
                   1384: 
                   1385: ###############################################################
                   1386: ###############################################################
1.113     bowersj2 1387: 
                   1388: =pod
                   1389: 
1.648     raeburn  1390: =item * &create_workbook()
1.255     matthew  1391: 
                   1392: Create an Excel worksheet.  If it fails, output message on the
                   1393: request object and return undefs.
                   1394: 
                   1395: Inputs: Apache request object
                   1396: 
                   1397: Returns (undef) on failure, 
                   1398:     Excel worksheet object, scalar with filename, and formats 
                   1399:     from &Apache::loncommon::define_excel_formats on success
                   1400: 
                   1401: =cut
                   1402: 
                   1403: ###############################################################
                   1404: ###############################################################
                   1405: sub create_workbook {
                   1406:     my ($r) = @_;
                   1407:         #
                   1408:     # Create the excel spreadsheet
                   1409:     my $filename = '/prtspool/'.
1.258     albertel 1410:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.255     matthew  1411:         time.'_'.rand(1000000000).'.xls';
                   1412:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
                   1413:     if (! defined($workbook)) {
                   1414:         $r->log_error("Error creating excel spreadsheet $filename: $!");
                   1415:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
                   1416:                             "This error has been logged.  ".
                   1417:                             "Please alert your LON-CAPA administrator").
                   1418:                   '</p>');
                   1419:         return (undef);
                   1420:     }
                   1421:     #
                   1422:     $workbook->set_tempdir('/home/httpd/perl/tmp');
                   1423:     #
                   1424:     my $format = &Apache::loncommon::define_excel_formats($workbook);
                   1425:     return ($workbook,$filename,$format);
                   1426: }
                   1427: 
                   1428: ###############################################################
                   1429: ###############################################################
                   1430: 
                   1431: =pod
                   1432: 
1.648     raeburn  1433: =item * &create_text_file()
1.113     bowersj2 1434: 
1.542     raeburn  1435: Create a file to write to and eventually make available to the user.
1.256     matthew  1436: If file creation fails, outputs an error message on the request object and 
                   1437: return undefs.
1.113     bowersj2 1438: 
1.256     matthew  1439: Inputs: Apache request object, and file suffix
1.113     bowersj2 1440: 
1.256     matthew  1441: Returns (undef) on failure, 
                   1442:     Filehandle and filename on success.
1.113     bowersj2 1443: 
                   1444: =cut
                   1445: 
1.256     matthew  1446: ###############################################################
                   1447: ###############################################################
                   1448: sub create_text_file {
                   1449:     my ($r,$suffix) = @_;
                   1450:     if (! defined($suffix)) { $suffix = 'txt'; };
                   1451:     my $fh;
                   1452:     my $filename = '/prtspool/'.
1.258     albertel 1453:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.256     matthew  1454:         time.'_'.rand(1000000000).'.'.$suffix;
                   1455:     $fh = Apache::File->new('>/home/httpd'.$filename);
                   1456:     if (! defined($fh)) {
                   1457:         $r->log_error("Couldn't open $filename for output $!");
                   1458:         $r->print("Problems occured in creating the output file.  ".
                   1459:                   "This error has been logged.  ".
                   1460:                   "Please alert your LON-CAPA administrator.");
1.113     bowersj2 1461:     }
1.256     matthew  1462:     return ($fh,$filename)
1.113     bowersj2 1463: }
                   1464: 
                   1465: 
1.256     matthew  1466: =pod 
1.113     bowersj2 1467: 
                   1468: =back
                   1469: 
                   1470: =cut
1.37      matthew  1471: 
                   1472: ###############################################################
1.33      matthew  1473: ##        Home server <option> list generating code          ##
                   1474: ###############################################################
1.35      matthew  1475: 
1.169     www      1476: # ------------------------------------------
                   1477: 
                   1478: sub domain_select {
                   1479:     my ($name,$value,$multiple)=@_;
                   1480:     my %domains=map { 
1.514     albertel 1481: 	$_ => $_.' '. &Apache::lonnet::domain($_,'description') 
1.512     albertel 1482:     } &Apache::lonnet::all_domains();
1.169     www      1483:     if ($multiple) {
                   1484: 	$domains{''}=&mt('Any domain');
1.550     albertel 1485: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
1.287     albertel 1486: 	return &multiple_select_form($name,$value,4,\%domains);
1.169     www      1487:     } else {
1.550     albertel 1488: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
1.169     www      1489: 	return &select_form($name,$value,%domains);
                   1490:     }
                   1491: }
                   1492: 
1.282     albertel 1493: #-------------------------------------------
                   1494: 
                   1495: =pod
                   1496: 
1.519     raeburn  1497: =head1 Routines for form select boxes
                   1498: 
                   1499: =over 4
                   1500: 
1.648     raeburn  1501: =item * &multiple_select_form($name,$value,$size,$hash,$order)
1.282     albertel 1502: 
                   1503: Returns a string containing a <select> element int multiple mode
                   1504: 
                   1505: 
                   1506: Args:
                   1507:   $name - name of the <select> element
1.506     raeburn  1508:   $value - scalar or array ref of values that should already be selected
1.282     albertel 1509:   $size - number of rows long the select element is
1.283     albertel 1510:   $hash - the elements should be 'option' => 'shown text'
1.282     albertel 1511:           (shown text should already have been &mt())
1.506     raeburn  1512:   $order - (optional) array ref of the order to show the elements in
1.283     albertel 1513: 
1.282     albertel 1514: =cut
                   1515: 
                   1516: #-------------------------------------------
1.169     www      1517: sub multiple_select_form {
1.284     albertel 1518:     my ($name,$value,$size,$hash,$order)=@_;
1.169     www      1519:     my %selected = map { $_ => 1 } ref($value)?@{$value}:($value);
                   1520:     my $output='';
1.191     matthew  1521:     if (! defined($size)) {
                   1522:         $size = 4;
1.283     albertel 1523:         if (scalar(keys(%$hash))<4) {
                   1524:             $size = scalar(keys(%$hash));
1.191     matthew  1525:         }
                   1526:     }
1.169     www      1527:     $output.="\n<select name='$name' size='$size' multiple='1'>";
1.501     banghart 1528:     my @order;
1.506     raeburn  1529:     if (ref($order) eq 'ARRAY')  {
                   1530:         @order = @{$order};
                   1531:     } else {
                   1532:         @order = sort(keys(%$hash));
1.501     banghart 1533:     }
                   1534:     if (exists($$hash{'select_form_order'})) {
                   1535:         @order = @{$$hash{'select_form_order'}};
                   1536:     }
                   1537:         
1.284     albertel 1538:     foreach my $key (@order) {
1.356     albertel 1539:         $output.='<option value="'.&HTML::Entities::encode($key,'"<>&').'" ';
1.284     albertel 1540:         $output.='selected="selected" ' if ($selected{$key});
                   1541:         $output.='>'.$hash->{$key}."</option>\n";
1.169     www      1542:     }
                   1543:     $output.="</select>\n";
                   1544:     return $output;
                   1545: }
                   1546: 
1.88      www      1547: #-------------------------------------------
                   1548: 
                   1549: =pod
                   1550: 
1.648     raeburn  1551: =item * &select_form($defdom,$name,%hash)
1.88      www      1552: 
                   1553: Returns a string containing a <select name='$name' size='1'> form to 
                   1554: allow a user to select options from a hash option_name => displayed text.  
                   1555: See lonrights.pm for an example invocation and use.
                   1556: 
                   1557: =cut
                   1558: 
                   1559: #-------------------------------------------
                   1560: sub select_form {
                   1561:     my ($def,$name,%hash) = @_;
                   1562:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
1.128     albertel 1563:     my @keys;
                   1564:     if (exists($hash{'select_form_order'})) {
                   1565: 	@keys=@{$hash{'select_form_order'}};
                   1566:     } else {
                   1567: 	@keys=sort(keys(%hash));
                   1568:     }
1.356     albertel 1569:     foreach my $key (@keys) {
                   1570:         $selectform.=
                   1571: 	    '<option value="'.&HTML::Entities::encode($key,'"<>&').'" '.
                   1572:             ($key eq $def ? 'selected="selected" ' : '').
                   1573:                 ">".&mt($hash{$key})."</option>\n";
1.88      www      1574:     }
                   1575:     $selectform.="</select>";
                   1576:     return $selectform;
                   1577: }
                   1578: 
1.475     www      1579: # For display filters
                   1580: 
                   1581: sub display_filter {
                   1582:     if (!$env{'form.show'}) { $env{'form.show'}=10; }
1.477     www      1583:     if (!$env{'form.displayfilter'}) { $env{'form.displayfilter'}='currentfolder'; }
1.475     www      1584:     return '<nobr><label>'.&mt('Records [_1]',
                   1585: 			       &Apache::lonmeta::selectbox('show',$env{'form.show'},undef,
                   1586: 							   (&mt('all'),10,20,50,100,1000,10000))).
1.478     www      1587: 	   '</label></nobr> <nobr>'.
1.475     www      1588:            &mt('Filter [_1]',
1.477     www      1589: 	   &select_form($env{'form.displayfilter'},
                   1590: 			'displayfilter',
                   1591: 			('currentfolder' => 'Current folder/page',
                   1592: 			 'containing' => 'Containing phrase',
                   1593: 			 'none' => 'None'))).
1.478     www      1594: 			 '<input type="text" name="containingphrase" size="30" value="'.&HTML::Entities::encode($env{'form.containingphrase'}).'" /></nobr>';
1.475     www      1595: }
                   1596: 
1.167     www      1597: sub gradeleveldescription {
                   1598:     my $gradelevel=shift;
                   1599:     my %gradelevels=(0 => 'Not specified',
                   1600: 		     1 => 'Grade 1',
                   1601: 		     2 => 'Grade 2',
                   1602: 		     3 => 'Grade 3',
                   1603: 		     4 => 'Grade 4',
                   1604: 		     5 => 'Grade 5',
                   1605: 		     6 => 'Grade 6',
                   1606: 		     7 => 'Grade 7',
                   1607: 		     8 => 'Grade 8',
                   1608: 		     9 => 'Grade 9',
                   1609: 		     10 => 'Grade 10',
                   1610: 		     11 => 'Grade 11',
                   1611: 		     12 => 'Grade 12',
                   1612: 		     13 => 'Grade 13',
                   1613: 		     14 => '100 Level',
                   1614: 		     15 => '200 Level',
                   1615: 		     16 => '300 Level',
                   1616: 		     17 => '400 Level',
                   1617: 		     18 => 'Graduate Level');
                   1618:     return &mt($gradelevels{$gradelevel});
                   1619: }
                   1620: 
1.163     www      1621: sub select_level_form {
                   1622:     my ($deflevel,$name)=@_;
                   1623:     unless ($deflevel) { $deflevel=0; }
1.167     www      1624:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
                   1625:     for (my $i=0; $i<=18; $i++) {
                   1626:         $selectform.="<option value=\"$i\" ".
1.253     albertel 1627:             ($i==$deflevel ? 'selected="selected" ' : '').
1.167     www      1628:                 ">".&gradeleveldescription($i)."</option>\n";
                   1629:     }
                   1630:     $selectform.="</select>";
                   1631:     return $selectform;
1.163     www      1632: }
1.167     www      1633: 
1.35      matthew  1634: #-------------------------------------------
                   1635: 
1.45      matthew  1636: =pod
                   1637: 
1.648     raeburn  1638: =item * &select_dom_form($defdom,$name,$includeempty,$showdomdesc)
1.35      matthew  1639: 
                   1640: Returns a string containing a <select name='$name' size='1'> form to 
                   1641: allow a user to select the domain to preform an operation in.  
                   1642: See loncreateuser.pm for an example invocation and use.
                   1643: 
1.90      www      1644: If the $includeempty flag is set, it also includes an empty choice ("no domain
                   1645: selected");
                   1646: 
1.563     raeburn  1647: If the $showdomdesc flag is set, the domain name is followed by the domain description. 
                   1648: 
1.35      matthew  1649: =cut
                   1650: 
                   1651: #-------------------------------------------
1.34      matthew  1652: sub select_dom_form {
1.563     raeburn  1653:     my ($defdom,$name,$includeempty,$showdomdesc) = @_;
1.550     albertel 1654:     my @domains = sort {lc($a) cmp lc($b)} (&Apache::lonnet::all_domains());
1.90      www      1655:     if ($includeempty) { @domains=('',@domains); }
1.34      matthew  1656:     my $selectdomain = "<select name=\"$name\" size=\"1\">\n";
1.356     albertel 1657:     foreach my $dom (@domains) {
                   1658:         $selectdomain.="<option value=\"$dom\" ".
1.563     raeburn  1659:             ($dom eq $defdom ? 'selected="selected" ' : '').'>'.$dom;
                   1660:         if ($showdomdesc) {
                   1661:             if ($dom ne '') {
                   1662:                 my $domdesc = &Apache::lonnet::domain($dom,'description');
                   1663:                 if ($domdesc ne '') {
                   1664:                     $selectdomain .= ' ('.$domdesc.')';
                   1665:                 }
                   1666:             } 
                   1667:         }
                   1668:         $selectdomain .= "</option>\n";
1.34      matthew  1669:     }
                   1670:     $selectdomain.="</select>";
                   1671:     return $selectdomain;
                   1672: }
                   1673: 
1.35      matthew  1674: #-------------------------------------------
                   1675: 
1.45      matthew  1676: =pod
                   1677: 
1.648     raeburn  1678: =item * &home_server_form_item($domain,$name,$defaultflag)
1.35      matthew  1679: 
1.586     raeburn  1680: input: 4 arguments (two required, two optional) - 
                   1681:     $domain - domain of new user
                   1682:     $name - name of form element
                   1683:     $default - Value of 'default' causes a default item to be first 
                   1684:                             option, and selected by default. 
                   1685:     $hide - Value of 'hide' causes hiding of the name of the server, 
                   1686:                             if 1 server found, or default, if 0 found.
1.594     raeburn  1687: output: returns 2 items: 
1.586     raeburn  1688: (a) form element which contains either:
                   1689:    (i) <select name="$name">
                   1690:         <option value="$hostid1">$hostid $servers{$hostid}</option>
                   1691:         <option value="$hostid2">$hostid $servers{$hostid}</option>       
                   1692:        </select>
                   1693:        form item if there are multiple library servers in $domain, or
                   1694:    (ii) an <input type="hidden" name="$name" value="$hostid" /> form item 
                   1695:        if there is only one library server in $domain.
                   1696: 
                   1697: (b) number of library servers found.
                   1698: 
                   1699: See loncreateuser.pm for example of use.
1.35      matthew  1700: 
                   1701: =cut
                   1702: 
                   1703: #-------------------------------------------
1.586     raeburn  1704: sub home_server_form_item {
                   1705:     my ($domain,$name,$default,$hide) = @_;
1.513     albertel 1706:     my %servers = &Apache::lonnet::get_servers($domain,'library');
1.586     raeburn  1707:     my $result;
                   1708:     my $numlib = keys(%servers);
                   1709:     if ($numlib > 1) {
                   1710:         $result .= '<select name="'.$name.'" />'."\n";
                   1711:         if ($default) {
                   1712:             $result .= '<option value="default" selected>'.&mt('default').
                   1713:                        '</option>'."\n";
                   1714:         }
                   1715:         foreach my $hostid (sort(keys(%servers))) {
                   1716:             $result.= '<option value="'.$hostid.'">'.
                   1717: 	              $hostid.' '.$servers{$hostid}."</option>\n";
                   1718:         }
                   1719:         $result .= '</select>'."\n";
                   1720:     } elsif ($numlib == 1) {
                   1721:         my $hostid;
                   1722:         foreach my $item (keys(%servers)) {
                   1723:             $hostid = $item;
                   1724:         }
                   1725:         $result .= '<input type="hidden" name="'.$name.'" value="'.
                   1726:                    $hostid.'" />';
                   1727:                    if (!$hide) {
                   1728:                        $result .= $hostid.' '.$servers{$hostid};
                   1729:                    }
                   1730:                    $result .= "\n";
                   1731:     } elsif ($default) {
                   1732:         $result .= '<input type="hidden" name="'.$name.
                   1733:                    '" value="default" />';
                   1734:                    if (!$hide) {
                   1735:                        $result .= &mt('default');
                   1736:                    }
                   1737:                    $result .= "\n";
1.33      matthew  1738:     }
1.586     raeburn  1739:     return ($result,$numlib);
1.33      matthew  1740: }
1.112     bowersj2 1741: 
                   1742: =pod
                   1743: 
1.534     albertel 1744: =back 
                   1745: 
1.112     bowersj2 1746: =cut
1.87      matthew  1747: 
                   1748: ###############################################################
1.112     bowersj2 1749: ##                  Decoding User Agent                      ##
1.87      matthew  1750: ###############################################################
                   1751: 
                   1752: =pod
                   1753: 
1.112     bowersj2 1754: =head1 Decoding the User Agent
                   1755: 
                   1756: =over 4
                   1757: 
                   1758: =item * &decode_user_agent()
1.87      matthew  1759: 
                   1760: Inputs: $r
                   1761: 
                   1762: Outputs:
                   1763: 
                   1764: =over 4
                   1765: 
1.112     bowersj2 1766: =item * $httpbrowser
1.87      matthew  1767: 
1.112     bowersj2 1768: =item * $clientbrowser
1.87      matthew  1769: 
1.112     bowersj2 1770: =item * $clientversion
1.87      matthew  1771: 
1.112     bowersj2 1772: =item * $clientmathml
1.87      matthew  1773: 
1.112     bowersj2 1774: =item * $clientunicode
1.87      matthew  1775: 
1.112     bowersj2 1776: =item * $clientos
1.87      matthew  1777: 
                   1778: =back
                   1779: 
1.157     matthew  1780: =back 
                   1781: 
1.87      matthew  1782: =cut
                   1783: 
                   1784: ###############################################################
                   1785: ###############################################################
                   1786: sub decode_user_agent {
1.247     albertel 1787:     my ($r)=@_;
1.87      matthew  1788:     my @browsertype=split(/\&/,$Apache::lonnet::perlvar{"lonBrowsDet"});
                   1789:     my %mathcap=split(/\&/,$$Apache::lonnet::perlvar{"lonMathML"});
                   1790:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
1.247     albertel 1791:     if (!$httpbrowser && $r) { $httpbrowser=$r->header_in('User-Agent'); }
1.87      matthew  1792:     my $clientbrowser='unknown';
                   1793:     my $clientversion='0';
                   1794:     my $clientmathml='';
                   1795:     my $clientunicode='0';
                   1796:     for (my $i=0;$i<=$#browsertype;$i++) {
                   1797:         my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\:/,$browsertype[$i]);
                   1798: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
                   1799: 	    $clientbrowser=$bname;
                   1800:             $httpbrowser=~/$vreg/i;
                   1801: 	    $clientversion=$1;
                   1802:             $clientmathml=($clientversion>=$minv);
                   1803:             $clientunicode=($clientversion>=$univ);
                   1804: 	}
                   1805:     }
                   1806:     my $clientos='unknown';
                   1807:     if (($httpbrowser=~/linux/i) ||
                   1808:         ($httpbrowser=~/unix/i) ||
                   1809:         ($httpbrowser=~/ux/i) ||
                   1810:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
                   1811:     if (($httpbrowser=~/vax/i) ||
                   1812:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
                   1813:     if ($httpbrowser=~/next/i) { $clientos='next'; }
                   1814:     if (($httpbrowser=~/mac/i) ||
                   1815:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
                   1816:     if ($httpbrowser=~/win/i) { $clientos='win'; }
                   1817:     if ($httpbrowser=~/embed/i) { $clientos='pda'; }
                   1818:     return ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                   1819:             $clientunicode,$clientos,);
                   1820: }
                   1821: 
1.32      matthew  1822: ###############################################################
                   1823: ##    Authentication changing form generation subroutines    ##
                   1824: ###############################################################
                   1825: ##
                   1826: ## All of the authform_xxxxxxx subroutines take their inputs in a
                   1827: ## hash, and have reasonable default values.
                   1828: ##
                   1829: ##    formname = the name given in the <form> tag.
1.35      matthew  1830: #-------------------------------------------
                   1831: 
1.45      matthew  1832: =pod
                   1833: 
1.112     bowersj2 1834: =head1 Authentication Routines
                   1835: 
                   1836: =over 4
                   1837: 
1.648     raeburn  1838: =item * &authform_xxxxxx()
1.35      matthew  1839: 
                   1840: The authform_xxxxxx subroutines provide javascript and html forms which 
                   1841: handle some of the conveniences required for authentication forms.  
                   1842: This is not an optimal method, but it works.  
                   1843: 
                   1844: =over 4
                   1845: 
1.112     bowersj2 1846: =item * authform_header
1.35      matthew  1847: 
1.112     bowersj2 1848: =item * authform_authorwarning
1.35      matthew  1849: 
1.112     bowersj2 1850: =item * authform_nochange
1.35      matthew  1851: 
1.112     bowersj2 1852: =item * authform_kerberos
1.35      matthew  1853: 
1.112     bowersj2 1854: =item * authform_internal
1.35      matthew  1855: 
1.112     bowersj2 1856: =item * authform_filesystem
1.35      matthew  1857: 
                   1858: =back
                   1859: 
1.648     raeburn  1860: See loncreateuser.pm for invocation and use examples.
1.157     matthew  1861: 
1.35      matthew  1862: =cut
                   1863: 
                   1864: #-------------------------------------------
1.32      matthew  1865: sub authform_header{  
                   1866:     my %in = (
                   1867:         formname => 'cu',
1.80      albertel 1868:         kerb_def_dom => '',
1.32      matthew  1869:         @_,
                   1870:     );
                   1871:     $in{'formname'} = 'document.' . $in{'formname'};
                   1872:     my $result='';
1.80      albertel 1873: 
                   1874: #---------------------------------------------- Code for upper case translation
                   1875:     my $Javascript_toUpperCase;
                   1876:     unless ($in{kerb_def_dom}) {
                   1877:         $Javascript_toUpperCase =<<"END";
                   1878:         switch (choice) {
                   1879:            case 'krb': currentform.elements[choicearg].value =
                   1880:                currentform.elements[choicearg].value.toUpperCase();
                   1881:                break;
                   1882:            default:
                   1883:         }
                   1884: END
                   1885:     } else {
                   1886:         $Javascript_toUpperCase = "";
                   1887:     }
                   1888: 
1.165     raeburn  1889:     my $radioval = "'nochange'";
1.591     raeburn  1890:     if (defined($in{'curr_authtype'})) {
                   1891:         if ($in{'curr_authtype'} ne '') {
                   1892:             $radioval = "'".$in{'curr_authtype'}."arg'";
                   1893:         }
1.174     matthew  1894:     }
1.165     raeburn  1895:     my $argfield = 'null';
1.591     raeburn  1896:     if (defined($in{'mode'})) {
1.165     raeburn  1897:         if ($in{'mode'} eq 'modifycourse')  {
1.591     raeburn  1898:             if (defined($in{'curr_autharg'})) {
                   1899:                 if ($in{'curr_autharg'} ne '') {
1.165     raeburn  1900:                     $argfield = "'$in{'curr_autharg'}'";
                   1901:                 }
                   1902:             }
                   1903:         }
                   1904:     }
                   1905: 
1.32      matthew  1906:     $result.=<<"END";
                   1907: var current = new Object();
1.165     raeburn  1908: current.radiovalue = $radioval;
                   1909: current.argfield = $argfield;
1.32      matthew  1910: 
                   1911: function changed_radio(choice,currentform) {
                   1912:     var choicearg = choice + 'arg';
                   1913:     // If a radio button in changed, we need to change the argfield
                   1914:     if (current.radiovalue != choice) {
                   1915:         current.radiovalue = choice;
                   1916:         if (current.argfield != null) {
                   1917:             currentform.elements[current.argfield].value = '';
                   1918:         }
                   1919:         if (choice == 'nochange') {
                   1920:             current.argfield = null;
                   1921:         } else {
                   1922:             current.argfield = choicearg;
                   1923:             switch(choice) {
                   1924:                 case 'krb': 
                   1925:                     currentform.elements[current.argfield].value = 
                   1926:                         "$in{'kerb_def_dom'}";
                   1927:                 break;
                   1928:               default:
                   1929:                 break;
                   1930:             }
                   1931:         }
                   1932:     }
                   1933:     return;
                   1934: }
1.22      www      1935: 
1.32      matthew  1936: function changed_text(choice,currentform) {
                   1937:     var choicearg = choice + 'arg';
                   1938:     if (currentform.elements[choicearg].value !='') {
1.80      albertel 1939:         $Javascript_toUpperCase
1.32      matthew  1940:         // clear old field
                   1941:         if ((current.argfield != choicearg) && (current.argfield != null)) {
                   1942:             currentform.elements[current.argfield].value = '';
                   1943:         }
                   1944:         current.argfield = choicearg;
                   1945:     }
                   1946:     set_auth_radio_buttons(choice,currentform);
                   1947:     return;
1.20      www      1948: }
1.32      matthew  1949: 
                   1950: function set_auth_radio_buttons(newvalue,currentform) {
                   1951:     var i=0;
                   1952:     while (i < currentform.login.length) {
                   1953:         if (currentform.login[i].value == newvalue) { break; }
                   1954:         i++;
                   1955:     }
                   1956:     if (i == currentform.login.length) {
                   1957:         return;
                   1958:     }
                   1959:     current.radiovalue = newvalue;
                   1960:     currentform.login[i].checked = true;
                   1961:     return;
                   1962: }
                   1963: END
                   1964:     return $result;
                   1965: }
                   1966: 
                   1967: sub authform_authorwarning{
                   1968:     my $result='';
1.144     matthew  1969:     $result='<i>'.
                   1970:         &mt('As a general rule, only authors or co-authors should be '.
                   1971:             'filesystem authenticated '.
                   1972:             '(which allows access to the server filesystem).')."</i>\n";
1.32      matthew  1973:     return $result;
                   1974: }
                   1975: 
                   1976: sub authform_nochange{  
                   1977:     my %in = (
                   1978:               formname => 'document.cu',
                   1979:               kerb_def_dom => 'MSU.EDU',
                   1980:               @_,
                   1981:           );
1.586     raeburn  1982:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'}); 
                   1983:     my $result;
                   1984:     if (keys(%can_assign) == 0) {
                   1985:         $result = &mt('Under you current role you are not permitted to change login settings for this user');  
                   1986:     } else {
                   1987:         $result = '<label>'.&mt('[_1] Do not change login data',
                   1988:                   '<input type="radio" name="login" value="nochange" '.
                   1989:                   'checked="checked" onclick="'.
1.281     albertel 1990:             "javascript:changed_radio('nochange',$in{'formname'});".'" />').
                   1991: 	    '</label>';
1.586     raeburn  1992:     }
1.32      matthew  1993:     return $result;
                   1994: }
                   1995: 
1.591     raeburn  1996: sub authform_kerberos {
1.32      matthew  1997:     my %in = (
                   1998:               formname => 'document.cu',
                   1999:               kerb_def_dom => 'MSU.EDU',
1.80      albertel 2000:               kerb_def_auth => 'krb4',
1.32      matthew  2001:               @_,
                   2002:               );
1.586     raeburn  2003:     my ($check4,$check5,$krbcheck,$krbarg,$krbver,$result,$authtype,
                   2004:         $autharg,$jscall);
                   2005:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
1.80      albertel 2006:     if ($in{'kerb_def_auth'} eq 'krb5') {
1.586     raeburn  2007:        $check5 = ' checked="on"';
1.80      albertel 2008:     } else {
1.586     raeburn  2009:        $check4 = ' checked="on"';
1.80      albertel 2010:     }
1.165     raeburn  2011:     $krbarg = $in{'kerb_def_dom'};
1.591     raeburn  2012:     if (defined($in{'curr_authtype'})) {
                   2013:         if ($in{'curr_authtype'} eq 'krb') {
1.586     raeburn  2014:             $krbcheck = ' checked="on"';
1.623     raeburn  2015:             if (defined($in{'mode'})) {
                   2016:                 if ($in{'mode'} eq 'modifyuser') {
                   2017:                     $krbcheck = '';
                   2018:                 }
                   2019:             }
1.591     raeburn  2020:             if (defined($in{'curr_kerb_ver'})) {
                   2021:                 if ($in{'curr_krb_ver'} eq '5') {
                   2022:                     $check5 = ' checked="on"';
                   2023:                     $check4 = '';
                   2024:                 } else {
                   2025:                     $check4 = ' checked="on"';
                   2026:                     $check5 = '';
                   2027:                 }
1.586     raeburn  2028:             }
1.591     raeburn  2029:             if (defined($in{'curr_autharg'})) {
1.165     raeburn  2030:                 $krbarg = $in{'curr_autharg'};
                   2031:             }
1.586     raeburn  2032:             if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
1.591     raeburn  2033:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2034:                     $result = 
                   2035:     &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
                   2036:         $in{'curr_autharg'},$krbver);
                   2037:                 } else {
                   2038:                     $result =
                   2039:     &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
                   2040:                 }
                   2041:                 return $result; 
                   2042:             }
                   2043:         }
                   2044:     } else {
                   2045:         if ($authnum == 1) {
                   2046:             $authtype = '<input type="hidden" name="login" value="krb">';
1.165     raeburn  2047:         }
                   2048:     }
1.586     raeburn  2049:     if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
                   2050:         return;
1.587     raeburn  2051:     } elsif ($authtype eq '') {
1.591     raeburn  2052:         if (defined($in{'mode'})) {
1.587     raeburn  2053:             if ($in{'mode'} eq 'modifycourse') {
                   2054:                 if ($authnum == 1) {
                   2055:                     $authtype = '<input type="hidden" name="login" value="krb">';
                   2056:                 }
                   2057:             }
                   2058:         }
1.586     raeburn  2059:     }
                   2060:     $jscall = "javascript:changed_radio('krb',$in{'formname'});";
                   2061:     if ($authtype eq '') {
                   2062:         $authtype = '<input type="radio" name="login" value="krb" '.
                   2063:                     'onclick="'.$jscall.'" onchange="'.$jscall.'"'.
                   2064:                     $krbcheck.' />';
                   2065:     }
                   2066:     if (($can_assign{'krb4'} && $can_assign{'krb5'}) ||
                   2067:         ($can_assign{'krb4'} && !$can_assign{'krb5'} && 
                   2068:          $in{'curr_authtype'} eq 'krb5') ||
                   2069:         (!$can_assign{'krb4'} && $can_assign{'krb5'} && 
                   2070:          $in{'curr_authtype'} eq 'krb4')) {
                   2071:         $result .= &mt
1.144     matthew  2072:         ('[_1] Kerberos authenticated with domain [_2] '.
1.281     albertel 2073:          '[_3] Version 4 [_4] Version 5 [_5]',
1.586     raeburn  2074:          '<label>'.$authtype,
1.281     albertel 2075:          '</label><input type="text" size="10" name="krbarg" '.
1.165     raeburn  2076:              'value="'.$krbarg.'" '.
1.144     matthew  2077:              'onchange="'.$jscall.'" />',
1.281     albertel 2078:          '<label><input type="radio" name="krbver" value="4" '.$check4.' />',
                   2079:          '</label><label><input type="radio" name="krbver" value="5" '.$check5.' />',
                   2080: 	 '</label>');
1.586     raeburn  2081:     } elsif ($can_assign{'krb4'}) {
                   2082:         $result .= &mt
                   2083:         ('[_1] Kerberos authenticated with domain [_2] '.
                   2084:          '[_3] Version 4 [_4]',
                   2085:          '<label>'.$authtype,
                   2086:          '</label><input type="text" size="10" name="krbarg" '.
                   2087:              'value="'.$krbarg.'" '.
                   2088:              'onchange="'.$jscall.'" />',
                   2089:          '<label><input type="hidden" name="krbver" value="4" />',
                   2090:          '</label>');
                   2091:     } elsif ($can_assign{'krb5'}) {
                   2092:         $result .= &mt
                   2093:         ('[_1] Kerberos authenticated with domain [_2] '.
                   2094:          '[_3] Version 5 [_4]',
                   2095:          '<label>'.$authtype,
                   2096:          '</label><input type="text" size="10" name="krbarg" '.
                   2097:              'value="'.$krbarg.'" '.
                   2098:              'onchange="'.$jscall.'" />',
                   2099:          '<label><input type="hidden" name="krbver" value="5" />',
                   2100:          '</label>');
                   2101:     }
1.32      matthew  2102:     return $result;
                   2103: }
                   2104: 
                   2105: sub authform_internal{  
1.586     raeburn  2106:     my %in = (
1.32      matthew  2107:                 formname => 'document.cu',
                   2108:                 kerb_def_dom => 'MSU.EDU',
                   2109:                 @_,
                   2110:                 );
1.586     raeburn  2111:     my ($intcheck,$intarg,$result,$authtype,$autharg,$jscall);
                   2112:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
1.591     raeburn  2113:     if (defined($in{'curr_authtype'})) {
                   2114:         if ($in{'curr_authtype'} eq 'int') {
1.586     raeburn  2115:             if ($can_assign{'int'}) {
                   2116:                 $intcheck = 'checked="on" ';
1.623     raeburn  2117:                 if (defined($in{'mode'})) {
                   2118:                     if ($in{'mode'} eq 'modifyuser') {
                   2119:                         $intcheck = '';
                   2120:                     }
                   2121:                 }
1.591     raeburn  2122:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2123:                     $intarg = $in{'curr_autharg'};
                   2124:                 }
                   2125:             } else {
                   2126:                 $result = &mt('Currently internally authenticated.');
                   2127:                 return $result;
1.165     raeburn  2128:             }
                   2129:         }
1.586     raeburn  2130:     } else {
                   2131:         if ($authnum == 1) {
                   2132:             $authtype = '<input type="hidden" name="login" value="int">';
                   2133:         }
                   2134:     }
                   2135:     if (!$can_assign{'int'}) {
                   2136:         return;
1.587     raeburn  2137:     } elsif ($authtype eq '') {
1.591     raeburn  2138:         if (defined($in{'mode'})) {
1.587     raeburn  2139:             if ($in{'mode'} eq 'modifycourse') {
                   2140:                 if ($authnum == 1) {
                   2141:                     $authtype = '<input type="hidden" name="login" value="int">';
                   2142:                 }
                   2143:             }
                   2144:         }
1.165     raeburn  2145:     }
1.586     raeburn  2146:     $jscall = "javascript:changed_radio('int',$in{'formname'});";
                   2147:     if ($authtype eq '') {
                   2148:         $authtype = '<input type="radio" name="login" value="int" '.$intcheck.
                   2149:                     ' onchange="'.$jscall.'" onclick="'.$jscall.'" />';
                   2150:     }
1.605     bisitz   2151:     $autharg = '<input type="password" size="10" name="intarg" value="'.
1.586     raeburn  2152:                $intarg.'" onchange="'.$jscall.'" />';
                   2153:     $result = &mt
1.144     matthew  2154:         ('[_1] Internally authenticated (with initial password [_2])',
1.586     raeburn  2155:          '<label>'.$authtype,'</label>'.$autharg);
1.620     www      2156:     $result.="<label><input type=\"checkbox\" name=\"visible\" onClick='if (this.checked) { this.form.intarg.type=\"text\" } else { this.form.intarg.type=\"password\" }' />".&mt('Visible input').'</label>';
1.32      matthew  2157:     return $result;
                   2158: }
                   2159: 
                   2160: sub authform_local{  
                   2161:     my %in = (
                   2162:               formname => 'document.cu',
                   2163:               kerb_def_dom => 'MSU.EDU',
                   2164:               @_,
                   2165:               );
1.586     raeburn  2166:     my ($loccheck,$locarg,$result,$authtype,$autharg,$jscall);
                   2167:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
1.591     raeburn  2168:     if (defined($in{'curr_authtype'})) {
                   2169:         if ($in{'curr_authtype'} eq 'loc') {
1.586     raeburn  2170:             if ($can_assign{'loc'}) {
                   2171:                 $loccheck = 'checked="on" ';
1.623     raeburn  2172:                 if (defined($in{'mode'})) {
                   2173:                     if ($in{'mode'} eq 'modifyuser') {
                   2174:                         $loccheck = '';
                   2175:                     }
                   2176:                 }
1.591     raeburn  2177:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2178:                     $locarg = $in{'curr_autharg'};
                   2179:                 }
                   2180:             } else {
                   2181:                 $result = &mt('Currently using local (institutional) authentication.');
                   2182:                 return $result;
1.165     raeburn  2183:             }
                   2184:         }
1.586     raeburn  2185:     } else {
                   2186:         if ($authnum == 1) {
                   2187:             $authtype = '<input type="hidden" name="login" value="loc">';
                   2188:         }
                   2189:     }
                   2190:     if (!$can_assign{'loc'}) {
                   2191:         return;
1.587     raeburn  2192:     } elsif ($authtype eq '') {
1.591     raeburn  2193:         if (defined($in{'mode'})) {
1.587     raeburn  2194:             if ($in{'mode'} eq 'modifycourse') {
                   2195:                 if ($authnum == 1) {
                   2196:                     $authtype = '<input type="hidden" name="login" value="loc">';
                   2197:                 }
                   2198:             }
                   2199:         }
1.165     raeburn  2200:     }
1.586     raeburn  2201:     $jscall = "javascript:changed_radio('loc',$in{'formname'});";
                   2202:     if ($authtype eq '') {
                   2203:         $authtype = '<input type="radio" name="login" value="loc" '.
                   2204:                     $loccheck.' onchange="'.$jscall.'" onclick="'.
                   2205:                     $jscall.'" />';
                   2206:     }
                   2207:     $autharg = '<input type="text" size="10" name="locarg" value="'.
                   2208:                $locarg.'" onchange="'.$jscall.'" />';
                   2209:     $result = &mt('[_1] Local Authentication with argument [_2]',
                   2210:                   '<label>'.$authtype,'</label>'.$autharg);
1.32      matthew  2211:     return $result;
                   2212: }
                   2213: 
                   2214: sub authform_filesystem{  
                   2215:     my %in = (
                   2216:               formname => 'document.cu',
                   2217:               kerb_def_dom => 'MSU.EDU',
                   2218:               @_,
                   2219:               );
1.586     raeburn  2220:     my ($fsyscheck,$result,$authtype,$autharg,$jscall);
                   2221:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
1.591     raeburn  2222:     if (defined($in{'curr_authtype'})) {
                   2223:         if ($in{'curr_authtype'} eq 'fsys') {
1.586     raeburn  2224:             if ($can_assign{'fsys'}) {
                   2225:                 $fsyscheck = 'checked="on" ';
1.623     raeburn  2226:                 if (defined($in{'mode'})) {
                   2227:                     if ($in{'mode'} eq 'modifyuser') {
                   2228:                         $fsyscheck = '';
                   2229:                     }
                   2230:                 }
1.586     raeburn  2231:             } else {
                   2232:                 $result = &mt('Currently Filesystem Authenticated.');
                   2233:                 return $result;
                   2234:             }           
                   2235:         }
                   2236:     } else {
                   2237:         if ($authnum == 1) {
                   2238:             $authtype = '<input type="hidden" name="login" value="fsys">';
                   2239:         }
                   2240:     }
                   2241:     if (!$can_assign{'fsys'}) {
                   2242:         return;
1.587     raeburn  2243:     } elsif ($authtype eq '') {
1.591     raeburn  2244:         if (defined($in{'mode'})) {
1.587     raeburn  2245:             if ($in{'mode'} eq 'modifycourse') {
                   2246:                 if ($authnum == 1) {
                   2247:                     $authtype = '<input type="hidden" name="login" value="fsys">';
                   2248:                 }
                   2249:             }
                   2250:         }
1.586     raeburn  2251:     }
                   2252:     $jscall = "javascript:changed_radio('fsys',$in{'formname'});";
                   2253:     if ($authtype eq '') {
                   2254:         $authtype = '<input type="radio" name="login" value="fsys" '.
                   2255:                     $fsyscheck.' onchange="'.$jscall.'" onclick="'.
                   2256:                     $jscall.'" />';
                   2257:     }
                   2258:     $autharg = '<input type="text" size="10" name="fsysarg" value=""'.
                   2259:                ' onchange="'.$jscall.'" />';
                   2260:     $result = &mt
1.144     matthew  2261:         ('[_1] Filesystem Authenticated (with initial password [_2])',
1.281     albertel 2262:          '<label><input type="radio" name="login" value="fsys" '.
1.586     raeburn  2263:          $fsyscheck.'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1.605     bisitz   2264:          '</label><input type="password" size="10" name="fsysarg" value="" '.
1.144     matthew  2265:                   'onchange="'.$jscall.'" />');
1.32      matthew  2266:     return $result;
                   2267: }
                   2268: 
1.586     raeburn  2269: sub get_assignable_auth {
                   2270:     my ($dom) = @_;
                   2271:     if ($dom eq '') {
                   2272:         $dom = $env{'request.role.domain'};
                   2273:     }
                   2274:     my %can_assign = (
                   2275:                           krb4 => 1,
                   2276:                           krb5 => 1,
                   2277:                           int  => 1,
                   2278:                           loc  => 1,
                   2279:                      );
                   2280:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2281:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   2282:         if (ref($domconfig{'usercreation'}{'authtypes'}) eq 'HASH') {
                   2283:             my $authhash = $domconfig{'usercreation'}{'authtypes'};
                   2284:             my $context;
                   2285:             if ($env{'request.role'} =~ /^au/) {
                   2286:                 $context = 'author';
                   2287:             } elsif ($env{'request.role'} =~ /^dc/) {
                   2288:                 $context = 'domain';
                   2289:             } elsif ($env{'request.course.id'}) {
                   2290:                 $context = 'course';
                   2291:             }
                   2292:             if ($context) {
                   2293:                 if (ref($authhash->{$context}) eq 'HASH') {
                   2294:                    %can_assign = %{$authhash->{$context}}; 
                   2295:                 }
                   2296:             }
                   2297:         }
                   2298:     }
                   2299:     my $authnum = 0;
                   2300:     foreach my $key (keys(%can_assign)) {
                   2301:         if ($can_assign{$key}) {
                   2302:             $authnum ++;
                   2303:         }
                   2304:     }
                   2305:     if ($can_assign{'krb4'} && $can_assign{'krb5'}) {
                   2306:         $authnum --;
                   2307:     }
                   2308:     return ($authnum,%can_assign);
                   2309: }
                   2310: 
1.80      albertel 2311: ###############################################################
                   2312: ##    Get Kerberos Defaults for Domain                 ##
                   2313: ###############################################################
                   2314: ##
                   2315: ## Returns default kerberos version and an associated argument
                   2316: ## as listed in file domain.tab. If not listed, provides
                   2317: ## appropriate default domain and kerberos version.
                   2318: ##
                   2319: #-------------------------------------------
                   2320: 
                   2321: =pod
                   2322: 
1.648     raeburn  2323: =item * &get_kerberos_defaults()
1.80      albertel 2324: 
                   2325: get_kerberos_defaults($target_domain) returns the default kerberos
1.641     raeburn  2326: version and domain. If not found, it defaults to version 4 and the 
                   2327: domain of the server.
1.80      albertel 2328: 
1.648     raeburn  2329: =over 4
                   2330: 
1.80      albertel 2331: ($def_version, $def_krb_domain) = &get_kerberos_defaults($target_domain);
                   2332: 
1.648     raeburn  2333: =back
                   2334: 
                   2335: =back
                   2336: 
1.80      albertel 2337: =cut
                   2338: 
                   2339: #-------------------------------------------
                   2340: sub get_kerberos_defaults {
                   2341:     my $domain=shift;
1.641     raeburn  2342:     my ($krbdef,$krbdefdom);
                   2343:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                   2344:     if (($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) {
                   2345:         $krbdef = $domdefaults{'auth_def'};
                   2346:         $krbdefdom = $domdefaults{'auth_arg_def'};
                   2347:     } else {
1.80      albertel 2348:         $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
                   2349:         my $krbdefdom=$1;
                   2350:         $krbdefdom=~tr/a-z/A-Z/;
                   2351:         $krbdef = "krb4";
                   2352:     }
                   2353:     return ($krbdef,$krbdefdom);
                   2354: }
1.112     bowersj2 2355: 
1.32      matthew  2356: 
1.46      matthew  2357: ###############################################################
                   2358: ##                Thesaurus Functions                        ##
                   2359: ###############################################################
1.20      www      2360: 
1.46      matthew  2361: =pod
1.20      www      2362: 
1.112     bowersj2 2363: =head1 Thesaurus Functions
                   2364: 
                   2365: =over 4
                   2366: 
1.648     raeburn  2367: =item * &initialize_keywords()
1.46      matthew  2368: 
                   2369: Initializes the package variable %Keywords if it is empty.  Uses the
                   2370: package variable $thesaurus_db_file.
                   2371: 
                   2372: =cut
                   2373: 
                   2374: ###################################################
                   2375: 
                   2376: sub initialize_keywords {
                   2377:     return 1 if (scalar keys(%Keywords));
                   2378:     # If we are here, %Keywords is empty, so fill it up
                   2379:     #   Make sure the file we need exists...
                   2380:     if (! -e $thesaurus_db_file) {
                   2381:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file".
                   2382:                                  " failed because it does not exist");
                   2383:         return 0;
                   2384:     }
                   2385:     #   Set up the hash as a database
                   2386:     my %thesaurus_db;
                   2387:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 2388:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  2389:         &Apache::lonnet::logthis("Could not tie \%thesaurus_db to ".
                   2390:                                  $thesaurus_db_file);
                   2391:         return 0;
                   2392:     } 
                   2393:     #  Get the average number of appearances of a word.
                   2394:     my $avecount = $thesaurus_db{'average.count'};
                   2395:     #  Put keywords (those that appear > average) into %Keywords
                   2396:     while (my ($word,$data)=each (%thesaurus_db)) {
                   2397:         my ($count,undef) = split /:/,$data;
                   2398:         $Keywords{$word}++ if ($count > $avecount);
                   2399:     }
                   2400:     untie %thesaurus_db;
                   2401:     # Remove special values from %Keywords.
1.356     albertel 2402:     foreach my $value ('total.count','average.count') {
                   2403:         delete($Keywords{$value}) if (exists($Keywords{$value}));
1.586     raeburn  2404:   }
1.46      matthew  2405:     return 1;
                   2406: }
                   2407: 
                   2408: ###################################################
                   2409: 
                   2410: =pod
                   2411: 
1.648     raeburn  2412: =item * &keyword($word)
1.46      matthew  2413: 
                   2414: Returns true if $word is a keyword.  A keyword is a word that appears more 
                   2415: than the average number of times in the thesaurus database.  Calls 
                   2416: &initialize_keywords
                   2417: 
                   2418: =cut
                   2419: 
                   2420: ###################################################
1.20      www      2421: 
                   2422: sub keyword {
1.46      matthew  2423:     return if (!&initialize_keywords());
                   2424:     my $word=lc(shift());
                   2425:     $word=~s/\W//g;
                   2426:     return exists($Keywords{$word});
1.20      www      2427: }
1.46      matthew  2428: 
                   2429: ###############################################################
                   2430: 
                   2431: =pod 
1.20      www      2432: 
1.648     raeburn  2433: =item * &get_related_words()
1.46      matthew  2434: 
1.160     matthew  2435: Look up a word in the thesaurus.  Takes a scalar argument and returns
1.46      matthew  2436: an array of words.  If the keyword is not in the thesaurus, an empty array
                   2437: will be returned.  The order of the words returned is determined by the
                   2438: database which holds them.
                   2439: 
                   2440: Uses global $thesaurus_db_file.
                   2441: 
                   2442: =cut
                   2443: 
                   2444: ###############################################################
                   2445: sub get_related_words {
                   2446:     my $keyword = shift;
                   2447:     my %thesaurus_db;
                   2448:     if (! -e $thesaurus_db_file) {
                   2449:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file ".
                   2450:                                  "failed because the file does not exist");
                   2451:         return ();
                   2452:     }
                   2453:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 2454:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  2455:         return ();
                   2456:     } 
                   2457:     my @Words=();
1.429     www      2458:     my $count=0;
1.46      matthew  2459:     if (exists($thesaurus_db{$keyword})) {
1.356     albertel 2460: 	# The first element is the number of times
                   2461: 	# the word appears.  We do not need it now.
1.429     www      2462: 	my (undef,@RelatedWords) = (split(/:/,$thesaurus_db{$keyword}));
                   2463: 	my (undef,$mostfrequentcount)=split(/\,/,$RelatedWords[0]);
                   2464: 	my $threshold=$mostfrequentcount/10;
                   2465:         foreach my $possibleword (@RelatedWords) {
                   2466:             my ($word,$wordcount)=split(/\,/,$possibleword);
                   2467:             if ($wordcount>$threshold) {
                   2468: 		push(@Words,$word);
                   2469:                 $count++;
                   2470:                 if ($count>10) { last; }
                   2471: 	    }
1.20      www      2472:         }
                   2473:     }
1.46      matthew  2474:     untie %thesaurus_db;
                   2475:     return @Words;
1.14      harris41 2476: }
1.46      matthew  2477: 
1.112     bowersj2 2478: =pod
                   2479: 
                   2480: =back
                   2481: 
                   2482: =cut
1.61      www      2483: 
                   2484: # -------------------------------------------------------------- Plaintext name
1.81      albertel 2485: =pod
                   2486: 
1.112     bowersj2 2487: =head1 User Name Functions
                   2488: 
                   2489: =over 4
                   2490: 
1.648     raeburn  2491: =item * &plainname($uname,$udom,$first)
1.81      albertel 2492: 
1.112     bowersj2 2493: Takes a users logon name and returns it as a string in
1.226     albertel 2494: "first middle last generation" form 
                   2495: if $first is set to 'lastname' then it returns it as
                   2496: 'lastname generation, firstname middlename' if their is a lastname
1.81      albertel 2497: 
                   2498: =cut
1.61      www      2499: 
1.295     www      2500: 
1.81      albertel 2501: ###############################################################
1.61      www      2502: sub plainname {
1.226     albertel 2503:     my ($uname,$udom,$first)=@_;
1.537     albertel 2504:     return if (!defined($uname) || !defined($udom));
1.295     www      2505:     my %names=&getnames($uname,$udom);
1.226     albertel 2506:     my $name=&Apache::lonnet::format_name($names{'firstname'},
                   2507: 					  $names{'middlename'},
                   2508: 					  $names{'lastname'},
                   2509: 					  $names{'generation'},$first);
                   2510:     $name=~s/^\s+//;
1.62      www      2511:     $name=~s/\s+$//;
                   2512:     $name=~s/\s+/ /g;
1.353     albertel 2513:     if ($name !~ /\S/) { $name=$uname.':'.$udom; }
1.62      www      2514:     return $name;
1.61      www      2515: }
1.66      www      2516: 
                   2517: # -------------------------------------------------------------------- Nickname
1.81      albertel 2518: =pod
                   2519: 
1.648     raeburn  2520: =item * &nickname($uname,$udom)
1.81      albertel 2521: 
                   2522: Gets a users name and returns it as a string as
                   2523: 
                   2524: "&quot;nickname&quot;"
1.66      www      2525: 
1.81      albertel 2526: if the user has a nickname or
                   2527: 
                   2528: "first middle last generation"
                   2529: 
                   2530: if the user does not
                   2531: 
                   2532: =cut
1.66      www      2533: 
                   2534: sub nickname {
                   2535:     my ($uname,$udom)=@_;
1.537     albertel 2536:     return if (!defined($uname) || !defined($udom));
1.295     www      2537:     my %names=&getnames($uname,$udom);
1.68      albertel 2538:     my $name=$names{'nickname'};
1.66      www      2539:     if ($name) {
                   2540:        $name='&quot;'.$name.'&quot;'; 
                   2541:     } else {
                   2542:        $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
                   2543: 	     $names{'lastname'}.' '.$names{'generation'};
                   2544:        $name=~s/\s+$//;
                   2545:        $name=~s/\s+/ /g;
                   2546:     }
                   2547:     return $name;
                   2548: }
                   2549: 
1.295     www      2550: sub getnames {
                   2551:     my ($uname,$udom)=@_;
1.537     albertel 2552:     return if (!defined($uname) || !defined($udom));
1.433     albertel 2553:     if ($udom eq 'public' && $uname eq 'public') {
                   2554: 	return ('lastname' => &mt('Public'));
                   2555:     }
1.295     www      2556:     my $id=$uname.':'.$udom;
                   2557:     my ($names,$cached)=&Apache::lonnet::is_cached_new('namescache',$id);
                   2558:     if ($cached) {
                   2559: 	return %{$names};
                   2560:     } else {
                   2561: 	my %loadnames=&Apache::lonnet::get('environment',
                   2562:                     ['firstname','middlename','lastname','generation','nickname'],
                   2563: 					 $udom,$uname);
                   2564: 	&Apache::lonnet::do_cache_new('namescache',$id,\%loadnames);
                   2565: 	return %loadnames;
                   2566:     }
                   2567: }
1.61      www      2568: 
1.542     raeburn  2569: # -------------------------------------------------------------------- getemails
1.648     raeburn  2570: 
1.542     raeburn  2571: =pod
                   2572: 
1.648     raeburn  2573: =item * &getemails($uname,$udom)
1.542     raeburn  2574: 
                   2575: Gets a user's email information and returns it as a hash with keys:
                   2576: notification, critnotification, permanentemail
                   2577: 
                   2578: For notification and critnotification, values are comma-separated lists 
1.648     raeburn  2579: of e-mail addresses; for permanentemail, value is a single e-mail address.
1.542     raeburn  2580:  
1.648     raeburn  2581: 
1.542     raeburn  2582: =cut
                   2583: 
1.648     raeburn  2584: 
1.466     albertel 2585: sub getemails {
                   2586:     my ($uname,$udom)=@_;
                   2587:     if ($udom eq 'public' && $uname eq 'public') {
                   2588: 	return;
                   2589:     }
1.467     www      2590:     if (!$udom) { $udom=$env{'user.domain'}; }
                   2591:     if (!$uname) { $uname=$env{'user.name'}; }
1.466     albertel 2592:     my $id=$uname.':'.$udom;
                   2593:     my ($names,$cached)=&Apache::lonnet::is_cached_new('emailscache',$id);
                   2594:     if ($cached) {
                   2595: 	return %{$names};
                   2596:     } else {
                   2597: 	my %loadnames=&Apache::lonnet::get('environment',
                   2598:                     			   ['notification','critnotification',
                   2599: 					    'permanentemail'],
                   2600: 					   $udom,$uname);
                   2601: 	&Apache::lonnet::do_cache_new('emailscache',$id,\%loadnames);
                   2602: 	return %loadnames;
                   2603:     }
                   2604: }
                   2605: 
1.551     albertel 2606: sub flush_email_cache {
                   2607:     my ($uname,$udom)=@_;
                   2608:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   2609:     if (!$uname) { $uname=$env{'user.name'};   }
                   2610:     return if ($udom eq 'public' && $uname eq 'public');
                   2611:     my $id=$uname.':'.$udom;
                   2612:     &Apache::lonnet::devalidate_cache_new('emailscache',$id);
                   2613: }
                   2614: 
1.61      www      2615: # ------------------------------------------------------------------ Screenname
1.81      albertel 2616: 
                   2617: =pod
                   2618: 
1.648     raeburn  2619: =item * &screenname($uname,$udom)
1.81      albertel 2620: 
                   2621: Gets a users screenname and returns it as a string
                   2622: 
                   2623: =cut
1.61      www      2624: 
                   2625: sub screenname {
                   2626:     my ($uname,$udom)=@_;
1.258     albertel 2627:     if ($uname eq $env{'user.name'} &&
                   2628: 	$udom eq $env{'user.domain'}) {return $env{'environment.screenname'};}
1.212     albertel 2629:     my %names=&Apache::lonnet::get('environment',['screenname'],$udom,$uname);
1.68      albertel 2630:     return $names{'screenname'};
1.62      www      2631: }
                   2632: 
1.212     albertel 2633: 
1.62      www      2634: # ------------------------------------------------------------- Message Wrapper
                   2635: 
                   2636: sub messagewrapper {
1.369     www      2637:     my ($link,$username,$domain,$subject,$text)=@_;
1.62      www      2638:     return 
1.441     albertel 2639:         '<a href="/adm/email?compose=individual&amp;'.
                   2640:         'recname='.$username.'&amp;recdom='.$domain.
                   2641: 	'&amp;subject='.&escape($subject).'&amp;text='.&escape($text).'" '.
1.200     matthew  2642:         'title="'.&mt('Send message').'">'.$link.'</a>';
1.74      www      2643: }
                   2644: # --------------------------------------------------------------- Notes Wrapper
                   2645: 
                   2646: sub noteswrapper {
                   2647:     my ($link,$un,$do)=@_;
                   2648:     return 
                   2649: "<a href='/adm/email?recordftf=retrieve&recname=$un&recdom=$do'>$link</a>";
1.62      www      2650: }
                   2651: # ------------------------------------------------------------- Aboutme Wrapper
                   2652: 
                   2653: sub aboutmewrapper {
1.166     www      2654:     my ($link,$username,$domain,$target)=@_;
1.447     raeburn  2655:     if (!defined($username)  && !defined($domain)) {
                   2656:         return;
                   2657:     }
1.205     www      2658:     return '<a href="/adm/'.$domain.'/'.$username.'/aboutme"'.
1.454     banghart 2659: 	($target?' target="$target"':'').' title="'.&mt("View this user's personal page").'">'.$link.'</a>';
1.62      www      2660: }
                   2661: 
                   2662: # ------------------------------------------------------------ Syllabus Wrapper
                   2663: 
                   2664: 
                   2665: sub syllabuswrapper {
1.109     matthew  2666:     my ($linktext,$coursedir,$domain,$fontcolor)=@_;
                   2667:     if ($fontcolor) { 
                   2668:         $linktext='<font color="'.$fontcolor.'">'.$linktext.'</font>'; 
                   2669:     }
1.208     matthew  2670:     return qq{<a href="/public/$domain/$coursedir/syllabus">$linktext</a>};
1.61      www      2671: }
1.14      harris41 2672: 
1.208     matthew  2673: sub track_student_link {
1.268     albertel 2674:     my ($linktext,$sname,$sdom,$target,$start) = @_;
                   2675:     my $link ="/adm/trackstudent?";
1.208     matthew  2676:     my $title = 'View recent activity';
                   2677:     if (defined($sname) && $sname !~ /^\s*$/ &&
                   2678:         defined($sdom)  && $sdom  !~ /^\s*$/) {
1.268     albertel 2679:         $link .= "selected_student=$sname:$sdom";
1.208     matthew  2680:         $title .= ' of this student';
1.268     albertel 2681:     } 
1.208     matthew  2682:     if (defined($target) && $target !~ /^\s*$/) {
                   2683:         $target = qq{target="$target"};
                   2684:     } else {
                   2685:         $target = '';
                   2686:     }
1.268     albertel 2687:     if ($start) { $link.='&amp;start='.$start; }
1.554     albertel 2688:     $title = &mt($title);
                   2689:     $linktext = &mt($linktext);
1.448     albertel 2690:     return qq{<a href="$link" title="$title" $target>$linktext</a>}.
                   2691: 	&help_open_topic('View_recent_activity');
1.208     matthew  2692: }
                   2693: 
1.508     www      2694: # ===================================================== Display a student photo
                   2695: 
                   2696: 
1.509     albertel 2697: sub student_image_tag {
1.508     www      2698:     my ($domain,$user)=@_;
                   2699:     my $imgsrc=&Apache::lonnet::studentphoto($domain,$user,'jpg');
                   2700:     if (($imgsrc) && ($imgsrc ne '/adm/lonKaputt/lonlogo_broken.gif')) {
                   2701: 	return '<img src="'.$imgsrc.'" align="right" />';
                   2702:     } else {
                   2703: 	return '';
                   2704:     }
                   2705: }
                   2706: 
1.112     bowersj2 2707: =pod
                   2708: 
                   2709: =back
                   2710: 
                   2711: =head1 Access .tab File Data
                   2712: 
                   2713: =over 4
                   2714: 
1.648     raeburn  2715: =item * &languageids() 
1.112     bowersj2 2716: 
                   2717: returns list of all language ids
                   2718: 
                   2719: =cut
                   2720: 
1.14      harris41 2721: sub languageids {
1.16      harris41 2722:     return sort(keys(%language));
1.14      harris41 2723: }
                   2724: 
1.112     bowersj2 2725: =pod
                   2726: 
1.648     raeburn  2727: =item * &languagedescription() 
1.112     bowersj2 2728: 
                   2729: returns description of a specified language id
                   2730: 
                   2731: =cut
                   2732: 
1.14      harris41 2733: sub languagedescription {
1.125     www      2734:     my $code=shift;
                   2735:     return  ($supported_language{$code}?'* ':'').
                   2736:             $language{$code}.
1.126     www      2737: 	    ($supported_language{$code}?' ('.&mt('interface available').')':'');
1.145     www      2738: }
                   2739: 
                   2740: sub plainlanguagedescription {
                   2741:     my $code=shift;
                   2742:     return $language{$code};
                   2743: }
                   2744: 
                   2745: sub supportedlanguagecode {
                   2746:     my $code=shift;
                   2747:     return $supported_language{$code};
1.97      www      2748: }
                   2749: 
1.112     bowersj2 2750: =pod
                   2751: 
1.648     raeburn  2752: =item * &copyrightids() 
1.112     bowersj2 2753: 
                   2754: returns list of all copyrights
                   2755: 
                   2756: =cut
                   2757: 
                   2758: sub copyrightids {
                   2759:     return sort(keys(%cprtag));
                   2760: }
                   2761: 
                   2762: =pod
                   2763: 
1.648     raeburn  2764: =item * &copyrightdescription() 
1.112     bowersj2 2765: 
                   2766: returns description of a specified copyright id
                   2767: 
                   2768: =cut
                   2769: 
                   2770: sub copyrightdescription {
1.166     www      2771:     return &mt($cprtag{shift(@_)});
1.112     bowersj2 2772: }
1.197     matthew  2773: 
                   2774: =pod
                   2775: 
1.648     raeburn  2776: =item * &source_copyrightids() 
1.192     taceyjo1 2777: 
                   2778: returns list of all source copyrights
                   2779: 
                   2780: =cut
                   2781: 
                   2782: sub source_copyrightids {
                   2783:     return sort(keys(%scprtag));
                   2784: }
                   2785: 
                   2786: =pod
                   2787: 
1.648     raeburn  2788: =item * &source_copyrightdescription() 
1.192     taceyjo1 2789: 
                   2790: returns description of a specified source copyright id
                   2791: 
                   2792: =cut
                   2793: 
                   2794: sub source_copyrightdescription {
                   2795:     return &mt($scprtag{shift(@_)});
                   2796: }
1.112     bowersj2 2797: 
                   2798: =pod
                   2799: 
1.648     raeburn  2800: =item * &filecategories() 
1.112     bowersj2 2801: 
                   2802: returns list of all file categories
                   2803: 
                   2804: =cut
                   2805: 
                   2806: sub filecategories {
                   2807:     return sort(keys(%category_extensions));
                   2808: }
                   2809: 
                   2810: =pod
                   2811: 
1.648     raeburn  2812: =item * &filecategorytypes() 
1.112     bowersj2 2813: 
                   2814: returns list of file types belonging to a given file
                   2815: category
                   2816: 
                   2817: =cut
                   2818: 
                   2819: sub filecategorytypes {
1.356     albertel 2820:     my ($cat) = @_;
                   2821:     return @{$category_extensions{lc($cat)}};
1.112     bowersj2 2822: }
                   2823: 
                   2824: =pod
                   2825: 
1.648     raeburn  2826: =item * &fileembstyle() 
1.112     bowersj2 2827: 
                   2828: returns embedding style for a specified file type
                   2829: 
                   2830: =cut
                   2831: 
                   2832: sub fileembstyle {
                   2833:     return $fe{lc(shift(@_))};
1.169     www      2834: }
                   2835: 
1.351     www      2836: sub filemimetype {
                   2837:     return $fm{lc(shift(@_))};
                   2838: }
                   2839: 
1.169     www      2840: 
                   2841: sub filecategoryselect {
                   2842:     my ($name,$value)=@_;
1.189     matthew  2843:     return &select_form($value,$name,
1.169     www      2844: 			'' => &mt('Any category'),
                   2845: 			map { $_,$_ } sort(keys(%category_extensions)));
1.112     bowersj2 2846: }
                   2847: 
                   2848: =pod
                   2849: 
1.648     raeburn  2850: =item * &filedescription() 
1.112     bowersj2 2851: 
                   2852: returns description for a specified file type
                   2853: 
                   2854: =cut
                   2855: 
                   2856: sub filedescription {
1.188     matthew  2857:     my $file_description = $fd{lc(shift())};
                   2858:     $file_description =~ s:([\[\]]):~$1:g;
                   2859:     return &mt($file_description);
1.112     bowersj2 2860: }
                   2861: 
                   2862: =pod
                   2863: 
1.648     raeburn  2864: =item * &filedescriptionex() 
1.112     bowersj2 2865: 
                   2866: returns description for a specified file type with
                   2867: extra formatting
                   2868: 
                   2869: =cut
                   2870: 
                   2871: sub filedescriptionex {
                   2872:     my $ex=shift;
1.188     matthew  2873:     my $file_description = $fd{lc($ex)};
                   2874:     $file_description =~ s:([\[\]]):~$1:g;
                   2875:     return '.'.$ex.' '.&mt($file_description);
1.112     bowersj2 2876: }
                   2877: 
                   2878: # End of .tab access
                   2879: =pod
                   2880: 
                   2881: =back
                   2882: 
                   2883: =cut
                   2884: 
                   2885: # ------------------------------------------------------------------ File Types
                   2886: sub fileextensions {
                   2887:     return sort(keys(%fe));
                   2888: }
                   2889: 
1.97      www      2890: # ----------------------------------------------------------- Display Languages
                   2891: # returns a hash with all desired display languages
                   2892: #
                   2893: 
                   2894: sub display_languages {
                   2895:     my %languages=();
1.356     albertel 2896:     foreach my $lang (&preferred_languages()) {
                   2897: 	$languages{$lang}=1;
1.97      www      2898:     }
                   2899:     &get_unprocessed_cgi($ENV{'QUERY_STRING'},['displaylanguage']);
1.258     albertel 2900:     if ($env{'form.displaylanguage'}) {
1.356     albertel 2901: 	foreach my $lang (split(/\s*(\,|\;|\:)\s*/,$env{'form.displaylanguage'})) {
                   2902: 	    $languages{$lang}=1;
1.97      www      2903:         }
                   2904:     }
                   2905:     return %languages;
1.14      harris41 2906: }
                   2907: 
1.117     www      2908: sub preferred_languages {
                   2909:     my @languages=();
1.258     albertel 2910:     if ($env{'course.'.$env{'request.course.id'}.'.languages'}) {
1.117     www      2911: 	@languages=(@languages,split(/\s*(\,|\;|\:)\s*/,
1.258     albertel 2912: 	         $env{'course.'.$env{'request.course.id'}.'.languages'}));
1.177     www      2913:     }
1.258     albertel 2914:     if ($env{'environment.languages'}) {
1.459     albertel 2915: 	@languages=(@languages,
                   2916: 		    split(/\s*(\,|\;|\:)\s*/,$env{'environment.languages'}));
1.118     www      2917:     }
1.583     albertel 2918:     my $browser=$ENV{'HTTP_ACCEPT_LANGUAGE'};
1.162     www      2919:     if ($browser) {
1.583     albertel 2920: 	my @browser = 
                   2921: 	    map { (split(/\s*;\s*/,$_))[0] } (split(/\s*,\s*/,$browser));
                   2922: 	push(@languages,@browser);
1.162     www      2923:     }
1.641     raeburn  2924: 
                   2925:     foreach my $domtype ($env{'user.domain'},$env{'request.role.domain'},
                   2926:                          $Apache::lonnet::perlvar{'lonDefDomain'}) {
                   2927:         if ($domtype ne '') {
                   2928:             my %domdefs = &Apache::lonnet::get_domain_defaults($domtype);
                   2929:             if ($domdefs{'lang_def'} ne '') {
                   2930:                 push(@languages,$domdefs{'lang_def'});
                   2931:             }
                   2932:         }
1.118     www      2933:     }
                   2934: # turn "en-ca" into "en-ca,en"
                   2935:     my @genlanguages;
1.356     albertel 2936:     foreach my $lang (@languages) {
                   2937: 	unless ($lang=~/\w/) { next; }
1.583     albertel 2938: 	push(@genlanguages,$lang);
1.356     albertel 2939: 	if ($lang=~/(\-|\_)/) {
                   2940: 	    push(@genlanguages,(split(/(\-|\_)/,$lang))[0]);
1.118     www      2941: 	}
                   2942:     }
1.583     albertel 2943:     #uniqueify the languages list
                   2944:     my %count;
                   2945:     @genlanguages = map { $count{$_}++ == 0 ? $_ : () } @genlanguages;
1.118     www      2946:     return @genlanguages;
1.117     www      2947: }
                   2948: 
1.582     albertel 2949: sub languages {
                   2950:     my ($possible_langs) = @_;
                   2951:     my @preferred_langs = &preferred_languages();
                   2952:     if (!ref($possible_langs)) {
                   2953: 	if( wantarray ) {
                   2954: 	    return @preferred_langs;
                   2955: 	} else {
                   2956: 	    return $preferred_langs[0];
                   2957: 	}
                   2958:     }
                   2959:     my %possibilities = map { $_ => 1 } (@$possible_langs);
                   2960:     my @preferred_possibilities;
                   2961:     foreach my $preferred_lang (@preferred_langs) {
                   2962: 	if (exists($possibilities{$preferred_lang})) {
                   2963: 	    push(@preferred_possibilities, $preferred_lang);
                   2964: 	}
                   2965:     }
                   2966:     if( wantarray ) {
                   2967: 	return @preferred_possibilities;
                   2968:     }
                   2969:     return $preferred_possibilities[0];
                   2970: }
                   2971: 
1.112     bowersj2 2972: ###############################################################
                   2973: ##               Student Answer Attempts                     ##
                   2974: ###############################################################
                   2975: 
                   2976: =pod
                   2977: 
                   2978: =head1 Alternate Problem Views
                   2979: 
                   2980: =over 4
                   2981: 
1.648     raeburn  2982: =item * &get_previous_attempt($symb, $username, $domain, $course,
1.112     bowersj2 2983:     $getattempt, $regexp, $gradesub)
                   2984: 
                   2985: Return string with previous attempt on problem. Arguments:
                   2986: 
                   2987: =over 4
                   2988: 
                   2989: =item * $symb: Problem, including path
                   2990: 
                   2991: =item * $username: username of the desired student
                   2992: 
                   2993: =item * $domain: domain of the desired student
1.14      harris41 2994: 
1.112     bowersj2 2995: =item * $course: Course ID
1.14      harris41 2996: 
1.112     bowersj2 2997: =item * $getattempt: Leave blank for all attempts, otherwise put
                   2998:     something
1.14      harris41 2999: 
1.112     bowersj2 3000: =item * $regexp: if string matches this regexp, the string will be
                   3001:     sent to $gradesub
1.14      harris41 3002: 
1.112     bowersj2 3003: =item * $gradesub: routine that processes the string if it matches $regexp
1.14      harris41 3004: 
1.112     bowersj2 3005: =back
1.14      harris41 3006: 
1.112     bowersj2 3007: The output string is a table containing all desired attempts, if any.
1.16      harris41 3008: 
1.112     bowersj2 3009: =cut
1.1       albertel 3010: 
                   3011: sub get_previous_attempt {
1.43      ng       3012:   my ($symb,$username,$domain,$course,$getattempt,$regexp,$gradesub)=@_;
1.1       albertel 3013:   my $prevattempts='';
1.43      ng       3014:   no strict 'refs';
1.1       albertel 3015:   if ($symb) {
1.3       albertel 3016:     my (%returnhash)=
                   3017:       &Apache::lonnet::restore($symb,$course,$domain,$username);
1.1       albertel 3018:     if ($returnhash{'version'}) {
                   3019:       my %lasthash=();
                   3020:       my $version;
                   3021:       for ($version=1;$version<=$returnhash{'version'};$version++) {
1.356     albertel 3022:         foreach my $key (sort(split(/\:/,$returnhash{$version.':keys'}))) {
                   3023: 	  $lasthash{$key}=$returnhash{$version.':'.$key};
1.19      harris41 3024:         }
1.1       albertel 3025:       }
1.596     albertel 3026:       $prevattempts=&start_data_table().&start_data_table_header_row();
                   3027:       $prevattempts.='<th>'.&mt('History').'</th>';
1.356     albertel 3028:       foreach my $key (sort(keys(%lasthash))) {
                   3029: 	my ($ign,@parts) = split(/\./,$key);
1.41      ng       3030: 	if ($#parts > 0) {
1.31      albertel 3031: 	  my $data=$parts[-1];
                   3032: 	  pop(@parts);
1.596     albertel 3033: 	  $prevattempts.='<th>'.&mt('Part ').join('.',@parts).'<br />'.$data.'&nbsp;</th>';
1.31      albertel 3034: 	} else {
1.41      ng       3035: 	  if ($#parts == 0) {
                   3036: 	    $prevattempts.='<th>'.$parts[0].'</th>';
                   3037: 	  } else {
                   3038: 	    $prevattempts.='<th>'.$ign.'</th>';
                   3039: 	  }
1.31      albertel 3040: 	}
1.16      harris41 3041:       }
1.596     albertel 3042:       $prevattempts.=&end_data_table_header_row();
1.40      ng       3043:       if ($getattempt eq '') {
                   3044: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
1.596     albertel 3045: 	  $prevattempts.=&start_data_table_row().
                   3046: 	      '<td>'.&mt('Transaction [_1]',$version).'</td>';
1.356     albertel 3047: 	    foreach my $key (sort(keys(%lasthash))) {
1.581     albertel 3048: 		my $value = &format_previous_attempt_value($key,
                   3049: 							   $returnhash{$version.':'.$key});
                   3050: 		$prevattempts.='<td>'.$value.'&nbsp;</td>';   
1.40      ng       3051: 	    }
1.596     albertel 3052: 	  $prevattempts.=&end_data_table_row();
1.40      ng       3053: 	 }
1.1       albertel 3054:       }
1.596     albertel 3055:       $prevattempts.=&start_data_table_row().'<td>'.&mt('Current').'</td>';
1.356     albertel 3056:       foreach my $key (sort(keys(%lasthash))) {
1.581     albertel 3057: 	my $value = &format_previous_attempt_value($key,$lasthash{$key});
1.356     albertel 3058: 	if ($key =~/$regexp$/ && (defined &$gradesub)) {$value = &$gradesub($value)}
1.40      ng       3059: 	$prevattempts.='<td>'.$value.'&nbsp;</td>';
1.16      harris41 3060:       }
1.596     albertel 3061:       $prevattempts.= &end_data_table_row().&end_data_table();
1.1       albertel 3062:     } else {
1.596     albertel 3063:       $prevattempts=
                   3064: 	  &start_data_table().&start_data_table_row().
                   3065: 	  '<td>'.&mt('Nothing submitted - no attempts.').'</td>'.
                   3066: 	  &end_data_table_row().&end_data_table();
1.1       albertel 3067:     }
                   3068:   } else {
1.596     albertel 3069:     $prevattempts=
                   3070: 	  &start_data_table().&start_data_table_row().
                   3071: 	  '<td>'.&mt('No data.').'</td>'.
                   3072: 	  &end_data_table_row().&end_data_table();
1.1       albertel 3073:   }
1.10      albertel 3074: }
                   3075: 
1.581     albertel 3076: sub format_previous_attempt_value {
                   3077:     my ($key,$value) = @_;
                   3078:     if ($key =~ /timestamp/) {
                   3079: 	$value = &Apache::lonlocal::locallocaltime($value);
                   3080:     } elsif (ref($value) eq 'ARRAY') {
                   3081: 	$value = '('.join(', ', @{ $value }).')';
                   3082:     } else {
                   3083: 	$value = &unescape($value);
                   3084:     }
                   3085:     return $value;
                   3086: }
                   3087: 
                   3088: 
1.107     albertel 3089: sub relative_to_absolute {
                   3090:     my ($url,$output)=@_;
                   3091:     my $parser=HTML::TokeParser->new(\$output);
                   3092:     my $token;
                   3093:     my $thisdir=$url;
                   3094:     my @rlinks=();
                   3095:     while ($token=$parser->get_token) {
                   3096: 	if ($token->[0] eq 'S') {
                   3097: 	    if ($token->[1] eq 'a') {
                   3098: 		if ($token->[2]->{'href'}) {
                   3099: 		    $rlinks[$#rlinks+1]=$token->[2]->{'href'};
                   3100: 		}
                   3101: 	    } elsif ($token->[1] eq 'img' || $token->[1] eq 'embed' ) {
                   3102: 		$rlinks[$#rlinks+1]=$token->[2]->{'src'};
                   3103: 	    } elsif ($token->[1] eq 'base') {
                   3104: 		$thisdir=$token->[2]->{'href'};
                   3105: 	    }
                   3106: 	}
                   3107:     }
                   3108:     $thisdir=~s-/[^/]*$--;
1.356     albertel 3109:     foreach my $link (@rlinks) {
                   3110: 	unless (($link=~/^http:\/\//i) ||
                   3111: 		($link=~/^\//) ||
                   3112: 		($link=~/^javascript:/i) ||
                   3113: 		($link=~/^mailto:/i) ||
                   3114: 		($link=~/^\#/)) {
                   3115: 	    my $newlocation=&Apache::lonnet::hreflocation($thisdir,$link);
                   3116: 	    $output=~s/(\"|\'|\=\s*)\Q$link\E(\"|\'|\s|\>)/$1$newlocation$2/;
1.107     albertel 3117: 	}
                   3118:     }
                   3119: # -------------------------------------------------- Deal with Applet codebases
                   3120:     $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
                   3121:     return $output;
                   3122: }
                   3123: 
1.112     bowersj2 3124: =pod
                   3125: 
1.648     raeburn  3126: =item * &get_student_view()
1.112     bowersj2 3127: 
                   3128: show a snapshot of what student was looking at
                   3129: 
                   3130: =cut
                   3131: 
1.10      albertel 3132: sub get_student_view {
1.186     albertel 3133:   my ($symb,$username,$domain,$courseid,$target,$moreenv) = @_;
1.114     www      3134:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 3135:   my (%form);
1.10      albertel 3136:   my @elements=('symb','courseid','domain','username');
                   3137:   foreach my $element (@elements) {
1.186     albertel 3138:       $form{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 3139:   }
1.186     albertel 3140:   if (defined($moreenv)) {
                   3141:       %form=(%form,%{$moreenv});
                   3142:   }
1.236     albertel 3143:   if (defined($target)) { $form{'grade_target'} = $target; }
1.107     albertel 3144:   $feedurl=&Apache::lonnet::clutter($feedurl);
1.650     www      3145:   my ($userview,$response)=&Apache::lonnet::ssi_body($feedurl,%form);
1.11      albertel 3146:   $userview=~s/\<body[^\>]*\>//gi;
                   3147:   $userview=~s/\<\/body\>//gi;
                   3148:   $userview=~s/\<html\>//gi;
                   3149:   $userview=~s/\<\/html\>//gi;
                   3150:   $userview=~s/\<head\>//gi;
                   3151:   $userview=~s/\<\/head\>//gi;
                   3152:   $userview=~s/action\s*\=/would_be_action\=/gi;
1.107     albertel 3153:   $userview=&relative_to_absolute($feedurl,$userview);
1.650     www      3154:   if (wantarray) {
                   3155:      return ($userview,$response);
                   3156:   } else {
                   3157:      return $userview;
                   3158:   }
                   3159: }
                   3160: 
                   3161: sub get_student_view_with_retries {
                   3162:   my ($symb,$retries,$username,$domain,$courseid,$target,$moreenv) = @_;
                   3163: 
                   3164:     my $ok = 0;                 # True if we got a good response.
                   3165:     my $content;
                   3166:     my $response;
                   3167: 
                   3168:     # Try to get the student_view done. within the retries count:
                   3169:     
                   3170:     do {
                   3171:          ($content, $response) = &get_student_view($symb,$username,$domain,$courseid,$target,$moreenv);
                   3172:          $ok      = $response->is_success;
                   3173:          if (!$ok) {
                   3174:             &Apache::lonnet::logthis("Failed get_student_view_with_retries on $symb: ".$response->is_success.', '.$response->code.', '.$response->message);
                   3175:          }
                   3176:          $retries--;
                   3177:     } while (!$ok && ($retries > 0));
                   3178:     
                   3179:     if (!$ok) {
                   3180:        $content = '';          # On error return an empty content.
                   3181:     }
1.651     www      3182:     if (wantarray) {
                   3183:        return ($content, $response);
                   3184:     } else {
                   3185:        return $content;
                   3186:     }
1.11      albertel 3187: }
                   3188: 
1.112     bowersj2 3189: =pod
                   3190: 
1.648     raeburn  3191: =item * &get_student_answers() 
1.112     bowersj2 3192: 
                   3193: show a snapshot of how student was answering problem
                   3194: 
                   3195: =cut
                   3196: 
1.11      albertel 3197: sub get_student_answers {
1.100     sakharuk 3198:   my ($symb,$username,$domain,$courseid,%form) = @_;
1.114     www      3199:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 3200:   my (%moreenv);
1.11      albertel 3201:   my @elements=('symb','courseid','domain','username');
                   3202:   foreach my $element (@elements) {
1.186     albertel 3203:     $moreenv{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 3204:   }
1.186     albertel 3205:   $moreenv{'grade_target'}='answer';
                   3206:   %moreenv=(%form,%moreenv);
1.497     raeburn  3207:   $feedurl = &Apache::lonnet::clutter($feedurl);
                   3208:   my $userview=&Apache::lonnet::ssi($feedurl,%moreenv);
1.10      albertel 3209:   return $userview;
1.1       albertel 3210: }
1.116     albertel 3211: 
                   3212: =pod
                   3213: 
                   3214: =item * &submlink()
                   3215: 
1.242     albertel 3216: Inputs: $text $uname $udom $symb $target
1.116     albertel 3217: 
                   3218: Returns: A link to grades.pm such as to see the SUBM view of a student
                   3219: 
                   3220: =cut
                   3221: 
                   3222: ###############################################
                   3223: sub submlink {
1.242     albertel 3224:     my ($text,$uname,$udom,$symb,$target)=@_;
1.116     albertel 3225:     if (!($uname && $udom)) {
                   3226: 	(my $cursymb, my $courseid,$udom,$uname)=
1.463     albertel 3227: 	    &Apache::lonnet::whichuser($symb);
1.116     albertel 3228: 	if (!$symb) { $symb=$cursymb; }
                   3229:     }
1.254     matthew  3230:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      3231:     $symb=&escape($symb);
1.242     albertel 3232:     if ($target) { $target="target=\"$target\""; }
                   3233:     return '<a href="/adm/grades?&command=submission&'.
                   3234: 	'symb='.$symb.'&student='.$uname.
                   3235: 	'&userdom='.$udom.'" '.$target.'>'.$text.'</a>';
                   3236: }
                   3237: ##############################################
                   3238: 
                   3239: =pod
                   3240: 
                   3241: =item * &pgrdlink()
                   3242: 
                   3243: Inputs: $text $uname $udom $symb $target
                   3244: 
                   3245: Returns: A link to grades.pm such as to see the PGRD view of a student
                   3246: 
                   3247: =cut
                   3248: 
                   3249: ###############################################
                   3250: sub pgrdlink {
                   3251:     my $link=&submlink(@_);
                   3252:     $link=~s/(&command=submission)/$1&showgrading=yes/;
                   3253:     return $link;
                   3254: }
                   3255: ##############################################
                   3256: 
                   3257: =pod
                   3258: 
                   3259: =item * &pprmlink()
                   3260: 
                   3261: Inputs: $text $uname $udom $symb $target
                   3262: 
                   3263: Returns: A link to parmset.pm such as to see the PPRM view of a
1.283     albertel 3264: student and a specific resource
1.242     albertel 3265: 
                   3266: =cut
                   3267: 
                   3268: ###############################################
                   3269: sub pprmlink {
                   3270:     my ($text,$uname,$udom,$symb,$target)=@_;
                   3271:     if (!($uname && $udom)) {
                   3272: 	(my $cursymb, my $courseid,$udom,$uname)=
1.463     albertel 3273: 	    &Apache::lonnet::whichuser($symb);
1.242     albertel 3274: 	if (!$symb) { $symb=$cursymb; }
                   3275:     }
1.254     matthew  3276:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      3277:     $symb=&escape($symb);
1.242     albertel 3278:     if ($target) { $target="target=\"$target\""; }
1.595     albertel 3279:     return '<a href="/adm/parmset?command=set&amp;'.
                   3280: 	'symb='.$symb.'&amp;uname='.$uname.
                   3281: 	'&amp;udom='.$udom.'" '.$target.'>'.$text.'</a>';
1.116     albertel 3282: }
                   3283: ##############################################
1.37      matthew  3284: 
1.112     bowersj2 3285: =pod
                   3286: 
                   3287: =back
                   3288: 
                   3289: =cut
                   3290: 
1.37      matthew  3291: ###############################################
1.51      www      3292: 
                   3293: 
                   3294: sub timehash {
                   3295:     my @ltime=localtime(shift);
                   3296:     return ( 'seconds' => $ltime[0],
                   3297:              'minutes' => $ltime[1],
                   3298:              'hours'   => $ltime[2],
                   3299:              'day'     => $ltime[3],
                   3300:              'month'   => $ltime[4]+1,
                   3301:              'year'    => $ltime[5]+1900,
                   3302:              'weekday' => $ltime[6],
                   3303:              'dayyear' => $ltime[7]+1,
                   3304:              'dlsav'   => $ltime[8] );
                   3305: }
                   3306: 
1.370     www      3307: sub utc_string {
                   3308:     my ($date)=@_;
1.371     www      3309:     return strftime("%Y%m%dT%H%M%SZ",gmtime($date));
1.370     www      3310: }
                   3311: 
1.51      www      3312: sub maketime {
                   3313:     my %th=@_;
                   3314:     return POSIX::mktime(
                   3315:         ($th{'seconds'},$th{'minutes'},$th{'hours'},
1.210     www      3316:          $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,-1));
1.70      www      3317: }
                   3318: 
                   3319: #########################################
1.51      www      3320: 
                   3321: sub findallcourses {
1.482     raeburn  3322:     my ($roles,$uname,$udom) = @_;
1.355     albertel 3323:     my %roles;
                   3324:     if (ref($roles)) { %roles = map { $_ => 1 } @{$roles}; }
1.348     albertel 3325:     my %courses;
1.51      www      3326:     my $now=time;
1.482     raeburn  3327:     if (!defined($uname)) {
                   3328:         $uname = $env{'user.name'};
                   3329:     }
                   3330:     if (!defined($udom)) {
                   3331:         $udom = $env{'user.domain'};
                   3332:     }
                   3333:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
                   3334:         my %roleshash = &Apache::lonnet::dump('roles',$udom,$uname);
                   3335:         if (!%roles) {
                   3336:             %roles = (
                   3337:                        cc => 1,
                   3338:                        in => 1,
                   3339:                        ep => 1,
                   3340:                        ta => 1,
                   3341:                        cr => 1,
                   3342:                        st => 1,
                   3343:              );
                   3344:         }
                   3345:         foreach my $entry (keys(%roleshash)) {
                   3346:             my ($trole,$tend,$tstart) = split(/_/,$roleshash{$entry});
                   3347:             if ($trole =~ /^cr/) { 
                   3348:                 next if (!exists($roles{$trole}) && !exists($roles{'cr'}));
                   3349:             } else {
                   3350:                 next if (!exists($roles{$trole}));
                   3351:             }
                   3352:             if ($tend) {
                   3353:                 next if ($tend < $now);
                   3354:             }
                   3355:             if ($tstart) {
                   3356:                 next if ($tstart > $now);
                   3357:             }
                   3358:             my ($cdom,$cnum,$sec,$cnumpart,$secpart,$role,$realsec);
                   3359:             (undef,$cdom,$cnumpart,$secpart) = split(/\//,$entry);
                   3360:             if ($secpart eq '') {
                   3361:                 ($cnum,$role) = split(/_/,$cnumpart); 
                   3362:                 $sec = 'none';
                   3363:                 $realsec = '';
                   3364:             } else {
                   3365:                 $cnum = $cnumpart;
                   3366:                 ($sec,$role) = split(/_/,$secpart);
                   3367:                 $realsec = $sec;
1.490     raeburn  3368:             }
1.482     raeburn  3369:             $courses{$cdom.'_'.$cnum}{$sec} = $trole.'/'.$cdom.'/'.$cnum.'/'.$realsec;
                   3370:         }
                   3371:     } else {
                   3372:         foreach my $key (keys(%env)) {
1.483     albertel 3373: 	    if ( $key=~m{^user\.role\.(\w+)\./($match_domain)/($match_courseid)/?(\w*)$} ||
                   3374:                  $key=~m{^user\.role\.(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_courseid)/?(\w*)$}) {
1.482     raeburn  3375: 	        my ($role,$cdom,$cnum,$sec) = ($1,$2,$3,$4);
                   3376: 	        next if ($role eq 'ca' || $role eq 'aa');
                   3377: 	        next if (%roles && !exists($roles{$role}));
                   3378: 	        my ($starttime,$endtime)=split(/\./,$env{$key});
                   3379:                 my $active=1;
                   3380:                 if ($starttime) {
                   3381: 		    if ($now<$starttime) { $active=0; }
                   3382:                 }
                   3383:                 if ($endtime) {
                   3384:                     if ($now>$endtime) { $active=0; }
                   3385:                 }
                   3386:                 if ($active) {
                   3387:                     if ($sec eq '') {
                   3388:                         $sec = 'none';
                   3389:                     }
                   3390:                     $courses{$cdom.'_'.$cnum}{$sec} = 
                   3391:                                      $role.'/'.$cdom.'/'.$cnum.'/'.$sec;
1.474     raeburn  3392:                 }
                   3393:             }
1.51      www      3394:         }
                   3395:     }
1.474     raeburn  3396:     return %courses;
1.51      www      3397: }
1.37      matthew  3398: 
1.54      www      3399: ###############################################
1.474     raeburn  3400: 
                   3401: sub blockcheck {
1.482     raeburn  3402:     my ($setters,$activity,$uname,$udom) = @_;
1.490     raeburn  3403: 
                   3404:     if (!defined($udom)) {
                   3405:         $udom = $env{'user.domain'};
                   3406:     }
                   3407:     if (!defined($uname)) {
                   3408:         $uname = $env{'user.name'};
                   3409:     }
                   3410: 
                   3411:     # If uname and udom are for a course, check for blocks in the course.
                   3412: 
                   3413:     if (&Apache::lonnet::is_course($udom,$uname)) {
                   3414:         my %records = &Apache::lonnet::dump('comm_block',$udom,$uname);
1.502     raeburn  3415:         my ($startblock,$endblock)=&get_blocks($setters,$activity,$udom,$uname);
1.490     raeburn  3416:         return ($startblock,$endblock);
                   3417:     }
1.474     raeburn  3418: 
1.502     raeburn  3419:     my $startblock = 0;
                   3420:     my $endblock = 0;
1.482     raeburn  3421:     my %live_courses = &findallcourses(undef,$uname,$udom);
1.474     raeburn  3422: 
1.490     raeburn  3423:     # If uname is for a user, and activity is course-specific, i.e.,
                   3424:     # boards, chat or groups, check for blocking in current course only.
1.474     raeburn  3425: 
1.490     raeburn  3426:     if (($activity eq 'boards' || $activity eq 'chat' ||
                   3427:          $activity eq 'groups') && ($env{'request.course.id'})) {
                   3428:         foreach my $key (keys(%live_courses)) {
                   3429:             if ($key ne $env{'request.course.id'}) {
                   3430:                 delete($live_courses{$key});
                   3431:             }
                   3432:         }
                   3433:     }
                   3434: 
                   3435:     my $otheruser = 0;
                   3436:     my %own_courses;
                   3437:     if ((($uname ne $env{'user.name'})) || ($udom ne $env{'user.domain'})) {
                   3438:         # Resource belongs to user other than current user.
                   3439:         $otheruser = 1;
                   3440:         # Gather courses for current user
                   3441:         %own_courses = 
                   3442:             &findallcourses(undef,$env{'user.name'},$env{'user.domain'});
                   3443:     }
                   3444: 
                   3445:     # Gather active course roles - course coordinator, instructor, 
                   3446:     # exam proctor, ta, student, or custom role.
1.474     raeburn  3447: 
                   3448:     foreach my $course (keys(%live_courses)) {
1.482     raeburn  3449:         my ($cdom,$cnum);
                   3450:         if ((defined($env{'course.'.$course.'.domain'})) && (defined($env{'course.'.$course.'.num'}))) {
                   3451:             $cdom = $env{'course.'.$course.'.domain'};
                   3452:             $cnum = $env{'course.'.$course.'.num'};
                   3453:         } else {
1.490     raeburn  3454:             ($cdom,$cnum) = split(/_/,$course); 
1.482     raeburn  3455:         }
                   3456:         my $no_ownblock = 0;
                   3457:         my $no_userblock = 0;
1.533     raeburn  3458:         if ($otheruser && $activity ne 'com') {
1.490     raeburn  3459:             # Check if current user has 'evb' priv for this
                   3460:             if (defined($own_courses{$course})) {
                   3461:                 foreach my $sec (keys(%{$own_courses{$course}})) {
                   3462:                     my $checkrole = 'cm./'.$cdom.'/'.$cnum;
                   3463:                     if ($sec ne 'none') {
                   3464:                         $checkrole .= '/'.$sec;
                   3465:                     }
                   3466:                     if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
                   3467:                         $no_ownblock = 1;
                   3468:                         last;
                   3469:                     }
                   3470:                 }
                   3471:             }
                   3472:             # if they have 'evb' priv and are currently not playing student
                   3473:             next if (($no_ownblock) &&
                   3474:                  ($env{'request.role'} !~ m{^st\./$cdom/$cnum}));
                   3475:         }
1.474     raeburn  3476:         foreach my $sec (keys(%{$live_courses{$course}})) {
1.482     raeburn  3477:             my $checkrole = 'cm./'.$cdom.'/'.$cnum;
1.474     raeburn  3478:             if ($sec ne 'none') {
1.482     raeburn  3479:                 $checkrole .= '/'.$sec;
1.474     raeburn  3480:             }
1.490     raeburn  3481:             if ($otheruser) {
                   3482:                 # Resource belongs to user other than current user.
                   3483:                 # Assemble privs for that user, and check for 'evb' priv.
1.482     raeburn  3484:                 my ($trole,$tdom,$tnum,$tsec);
                   3485:                 my $entry = $live_courses{$course}{$sec};
                   3486:                 if ($entry =~ /^cr/) {
                   3487:                     ($trole,$tdom,$tnum,$tsec) = 
                   3488:                       ($entry =~ m|^(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_username)/?(\w*)$|);
                   3489:                 } else {
                   3490:                     ($trole,$tdom,$tnum,$tsec) = split(/\//,$entry);
                   3491:                 }
                   3492:                 my ($spec,$area,$trest,%allroles,%userroles);
                   3493:                 $area = '/'.$tdom.'/'.$tnum;
                   3494:                 $trest = $tnum;
                   3495:                 if ($tsec ne '') {
                   3496:                     $area .= '/'.$tsec;
                   3497:                     $trest .= '/'.$tsec;
                   3498:                 }
                   3499:                 $spec = $trole.'.'.$area;
                   3500:                 if ($trole =~ /^cr/) {
                   3501:                     &Apache::lonnet::custom_roleprivs(\%allroles,$trole,
                   3502:                                                       $tdom,$spec,$trest,$area);
                   3503:                 } else {
                   3504:                     &Apache::lonnet::standard_roleprivs(\%allroles,$trole,
                   3505:                                                        $tdom,$spec,$trest,$area);
                   3506:                 }
                   3507:                 my ($author,$adv) = &Apache::lonnet::set_userprivs(\%userroles,\%allroles);
1.486     raeburn  3508:                 if ($userroles{'user.priv.'.$checkrole} =~ /evb\&([^\:]*)/) {
                   3509:                     if ($1) {
                   3510:                         $no_userblock = 1;
                   3511:                         last;
                   3512:                     }
                   3513:                 }
1.490     raeburn  3514:             } else {
                   3515:                 # Resource belongs to current user
                   3516:                 # Check for 'evb' priv via lonnet::allowed().
1.482     raeburn  3517:                 if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
                   3518:                     $no_ownblock = 1;
                   3519:                     last;
                   3520:                 }
1.474     raeburn  3521:             }
                   3522:         }
                   3523:         # if they have the evb priv and are currently not playing student
1.482     raeburn  3524:         next if (($no_ownblock) &&
1.491     albertel 3525:                  ($env{'request.role'} !~ m{^st\./\Q$cdom\E/\Q$cnum\E}));
1.482     raeburn  3526:         next if ($no_userblock);
1.474     raeburn  3527: 
1.490     raeburn  3528:         # Retrieve blocking times and identity of blocker for course
                   3529:         # of specified user, unless user has 'evb' privilege.
1.502     raeburn  3530:         
                   3531:         my ($start,$end)=&get_blocks($setters,$activity,$cdom,$cnum);
                   3532:         if (($start != 0) && 
                   3533:             (($startblock == 0) || ($startblock > $start))) {
                   3534:             $startblock = $start;
                   3535:         }
                   3536:         if (($end != 0)  &&
                   3537:             (($endblock == 0) || ($endblock < $end))) {
                   3538:             $endblock = $end;
                   3539:         }
1.490     raeburn  3540:     }
                   3541:     return ($startblock,$endblock);
                   3542: }
                   3543: 
                   3544: sub get_blocks {
                   3545:     my ($setters,$activity,$cdom,$cnum) = @_;
                   3546:     my $startblock = 0;
                   3547:     my $endblock = 0;
                   3548:     my $course = $cdom.'_'.$cnum;
                   3549:     $setters->{$course} = {};
                   3550:     $setters->{$course}{'staff'} = [];
                   3551:     $setters->{$course}{'times'} = [];
                   3552:     my %records = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
                   3553:     foreach my $record (keys(%records)) {
                   3554:         my ($start,$end) = ($record =~ m/^(\d+)____(\d+)$/);
                   3555:         if ($start <= time && $end >= time) {
                   3556:             my ($staff_name,$staff_dom,$title,$blocks) =
                   3557:                 &parse_block_record($records{$record});
                   3558:             if ($blocks->{$activity} eq 'on') {
                   3559:                 push(@{$$setters{$course}{'staff'}},[$staff_name,$staff_dom]);
                   3560:                 push(@{$$setters{$course}{'times'}}, [$start,$end]);
1.491     albertel 3561:                 if ( ($startblock == 0) || ($startblock > $start) ) {
                   3562:                     $startblock = $start;
1.490     raeburn  3563:                 }
1.491     albertel 3564:                 if ( ($endblock == 0) || ($endblock < $end) ) {
                   3565:                     $endblock = $end;
1.474     raeburn  3566:                 }
                   3567:             }
                   3568:         }
                   3569:     }
                   3570:     return ($startblock,$endblock);
                   3571: }
                   3572: 
                   3573: sub parse_block_record {
                   3574:     my ($record) = @_;
                   3575:     my ($setuname,$setudom,$title,$blocks);
                   3576:     if (ref($record) eq 'HASH') {
                   3577:         ($setuname,$setudom) = split(/:/,$record->{'setter'});
                   3578:         $title = &unescape($record->{'event'});
                   3579:         $blocks = $record->{'blocks'};
                   3580:     } else {
                   3581:         my @data = split(/:/,$record,3);
                   3582:         if (scalar(@data) eq 2) {
                   3583:             $title = $data[1];
                   3584:             ($setuname,$setudom) = split(/@/,$data[0]);
                   3585:         } else {
                   3586:             ($setuname,$setudom,$title) = @data;
                   3587:         }
                   3588:         $blocks = { 'com' => 'on' };
                   3589:     }
                   3590:     return ($setuname,$setudom,$title,$blocks);
                   3591: }
                   3592: 
                   3593: sub build_block_table {
                   3594:     my ($startblock,$endblock,$setters) = @_;
                   3595:     my %lt = &Apache::lonlocal::texthash(
                   3596:         'cacb' => 'Currently active communication blocks',
                   3597:         'cour' => 'Course',
                   3598:         'dura' => 'Duration',
                   3599:         'blse' => 'Block set by'
                   3600:     );
                   3601:     my $output;
1.476     raeburn  3602:     $output = '<br />'.$lt{'cacb'}.':<br />';
1.474     raeburn  3603:     $output .= &start_data_table();
                   3604:     $output .= '
                   3605: <tr>
                   3606:  <th>'.$lt{'cour'}.'</th>
                   3607:  <th>'.$lt{'dura'}.'</th>
                   3608:  <th>'.$lt{'blse'}.'</th>
                   3609: </tr>
                   3610: ';
                   3611:     foreach my $course (keys(%{$setters})) {
                   3612:         my %courseinfo=&Apache::lonnet::coursedescription($course);
                   3613:         for (my $i=0; $i<@{$$setters{$course}{staff}}; $i++) {
                   3614:             my ($uname,$udom) = @{$$setters{$course}{staff}[$i]};
1.490     raeburn  3615:             my $fullname = &plainname($uname,$udom);
                   3616:             if (defined($env{'user.name'}) && defined($env{'user.domain'})
                   3617:                 && $env{'user.name'} ne 'public' 
                   3618:                 && $env{'user.domain'} ne 'public') {
                   3619:                 $fullname = &aboutmewrapper($fullname,$uname,$udom);
                   3620:             }
1.474     raeburn  3621:             my ($openblock,$closeblock) = @{$$setters{$course}{times}[$i]};
                   3622:             $openblock = &Apache::lonlocal::locallocaltime($openblock);
                   3623:             $closeblock= &Apache::lonlocal::locallocaltime($closeblock);
                   3624:             $output .= &Apache::loncommon::start_data_table_row().
                   3625:                        '<td>'.$courseinfo{'description'}.'</td>'.
                   3626:                        '<td>'.$openblock.' to '.$closeblock.'</td>'.
1.490     raeburn  3627:                        '<td>'.$fullname.'</td>'.
1.474     raeburn  3628:                         &Apache::loncommon::end_data_table_row();
                   3629:         }
                   3630:     }
                   3631:     $output .= &end_data_table();
                   3632: }
                   3633: 
1.490     raeburn  3634: sub blocking_status {
                   3635:     my ($activity,$uname,$udom) = @_;
                   3636:     my %setters;
                   3637:     my ($blocked,$output,$ownitem,$is_course);
                   3638:     my ($startblock,$endblock)=&blockcheck(\%setters,$activity,$uname,$udom);
                   3639:     if ($startblock && $endblock) {
                   3640:         $blocked = 1;
                   3641:         if (wantarray) {
                   3642:             my $category;
                   3643:             if ($activity eq 'boards') {
                   3644:                 $category = 'Discussion posts in this course';
                   3645:             } elsif ($activity eq 'blogs') {
                   3646:                 $category = 'Blogs';
                   3647:             } elsif ($activity eq 'port') {
                   3648:                 if (defined($uname) && defined($udom)) {
                   3649:                     if ($uname eq $env{'user.name'} &&
                   3650:                         $udom eq $env{'user.domain'}) {
                   3651:                         $ownitem = 1;
                   3652:                     }
                   3653:                 }
                   3654:                 $is_course = &Apache::lonnet::is_course($udom,$uname);
                   3655:                 if ($ownitem) { 
                   3656:                     $category = 'Your portfolio files';  
                   3657:                 } elsif ($is_course) {
                   3658:                     my $coursedesc;
                   3659:                     foreach my $course (keys(%setters)) {
                   3660:                         my %courseinfo =
                   3661:                              &Apache::lonnet::coursedescription($course);
                   3662:                         $coursedesc = $courseinfo{'description'};
                   3663:                     }
                   3664:                     $category = "Group files in the course '$coursedesc'";
                   3665:                 } else {
                   3666:                     $category = 'Portfolio files belonging to ';
                   3667:                     if ($env{'user.name'} eq 'public' && 
                   3668:                         $env{'user.domain'} eq 'public') {
                   3669:                         $category .= &plainname($uname,$udom);
                   3670:                     } else {
                   3671:                         $category .= &aboutmewrapper(&plainname($uname,$udom),$uname,$udom);  
                   3672:                     }
                   3673:                 }
                   3674:             } elsif ($activity eq 'groups') {
                   3675:                 $category = 'Groups in this course';
                   3676:             }
                   3677:             my $showstart = &Apache::lonlocal::locallocaltime($startblock);
                   3678:             my $showend = &Apache::lonlocal::locallocaltime($endblock);
                   3679:             $output = '<br />'.&mt('[_1] will be inaccessible between [_2] and [_3] because communication is being blocked.',$category,$showstart,$showend).'<br />';
                   3680:             if (!($activity eq 'port' && !($ownitem) && !($is_course))) { 
                   3681:                 $output .= &build_block_table($startblock,$endblock,\%setters);
                   3682:             }
                   3683:         }
                   3684:     }
                   3685:     if (wantarray) {
                   3686:         return ($blocked,$output);
                   3687:     } else {
                   3688:         return $blocked;
                   3689:     }
                   3690: }
                   3691: 
1.60      matthew  3692: ###############################################
                   3693: 
                   3694: =pod
                   3695: 
1.112     bowersj2 3696: =head1 Domain Template Functions
                   3697: 
                   3698: =over 4
                   3699: 
                   3700: =item * &determinedomain()
1.60      matthew  3701: 
                   3702: Inputs: $domain (usually will be undef)
                   3703: 
1.63      www      3704: Returns: Determines which domain should be used for designs
1.60      matthew  3705: 
                   3706: =cut
1.54      www      3707: 
1.60      matthew  3708: ###############################################
1.63      www      3709: sub determinedomain {
                   3710:     my $domain=shift;
1.531     albertel 3711:     if (! $domain) {
1.60      matthew  3712:         # Determine domain if we have not been given one
                   3713:         $domain = $Apache::lonnet::perlvar{'lonDefDomain'};
1.258     albertel 3714:         if ($env{'user.domain'}) { $domain=$env{'user.domain'}; }
                   3715:         if ($env{'request.role.domain'}) { 
                   3716:             $domain=$env{'request.role.domain'}; 
1.60      matthew  3717:         }
                   3718:     }
1.63      www      3719:     return $domain;
                   3720: }
                   3721: ###############################################
1.517     raeburn  3722: 
1.518     albertel 3723: sub devalidate_domconfig_cache {
                   3724:     my ($udom)=@_;
                   3725:     &Apache::lonnet::devalidate_cache_new('domainconfig',$udom);
                   3726: }
                   3727: 
                   3728: # ---------------------- Get domain configuration for a domain
                   3729: sub get_domainconf {
                   3730:     my ($udom) = @_;
                   3731:     my $cachetime=1800;
                   3732:     my ($result,$cached)=&Apache::lonnet::is_cached_new('domainconfig',$udom);
                   3733:     if (defined($cached)) { return %{$result}; }
                   3734: 
                   3735:     my %domconfig = &Apache::lonnet::get_dom('configuration',
                   3736: 					     ['login','rolecolors'],$udom);
1.632     raeburn  3737:     my (%designhash,%legacy);
1.518     albertel 3738:     if (keys(%domconfig) > 0) {
                   3739:         if (ref($domconfig{'login'}) eq 'HASH') {
1.632     raeburn  3740:             if (keys(%{$domconfig{'login'}})) {
                   3741:                 foreach my $key (keys(%{$domconfig{'login'}})) {
                   3742:                     $designhash{$udom.'.login.'.$key}=$domconfig{'login'}{$key};
                   3743:                 }
                   3744:             } else {
                   3745:                 $legacy{'login'} = 1;
1.518     albertel 3746:             }
1.632     raeburn  3747:         } else {
                   3748:             $legacy{'login'} = 1;
1.518     albertel 3749:         }
                   3750:         if (ref($domconfig{'rolecolors'}) eq 'HASH') {
1.632     raeburn  3751:             if (keys(%{$domconfig{'rolecolors'}})) {
                   3752:                 foreach my $role (keys(%{$domconfig{'rolecolors'}})) {
                   3753:                     if (ref($domconfig{'rolecolors'}{$role}) eq 'HASH') {
                   3754:                         foreach my $item (keys(%{$domconfig{'rolecolors'}{$role}})) {
                   3755:                             $designhash{$udom.'.'.$role.'.'.$item}=$domconfig{'rolecolors'}{$role}{$item};
                   3756:                         }
1.518     albertel 3757:                     }
                   3758:                 }
1.632     raeburn  3759:             } else {
                   3760:                 $legacy{'rolecolors'} = 1;
1.518     albertel 3761:             }
1.632     raeburn  3762:         } else {
                   3763:             $legacy{'rolecolors'} = 1;
1.518     albertel 3764:         }
1.632     raeburn  3765:         if (keys(%legacy) > 0) {
                   3766:             my %legacyhash = &get_legacy_domconf($udom);
                   3767:             foreach my $item (keys(%legacyhash)) {
                   3768:                 if ($item =~ /^\Q$udom\E\.login/) {
                   3769:                     if ($legacy{'login'}) { 
                   3770:                         $designhash{$item} = $legacyhash{$item};
                   3771:                     }
                   3772:                 } else {
                   3773:                     if ($legacy{'rolecolors'}) {
                   3774:                         $designhash{$item} = $legacyhash{$item};
                   3775:                     }
1.518     albertel 3776:                 }
                   3777:             }
                   3778:         }
1.632     raeburn  3779:     } else {
                   3780:         %designhash = &get_legacy_domconf($udom); 
1.518     albertel 3781:     }
                   3782:     &Apache::lonnet::do_cache_new('domainconfig',$udom,\%designhash,
                   3783: 				  $cachetime);
                   3784:     return %designhash;
                   3785: }
                   3786: 
1.632     raeburn  3787: sub get_legacy_domconf {
                   3788:     my ($udom) = @_;
                   3789:     my %legacyhash;
                   3790:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
                   3791:     my $designfile =  $designdir.'/'.$udom.'.tab';
                   3792:     if (-e $designfile) {
                   3793:         if ( open (my $fh,"<$designfile") ) {
                   3794:             while (my $line = <$fh>) {
                   3795:                 next if ($line =~ /^\#/);
                   3796:                 chomp($line);
                   3797:                 my ($key,$val)=(split(/\=/,$line));
                   3798:                 if ($val) { $legacyhash{$udom.'.'.$key}=$val; }
                   3799:             }
                   3800:             close($fh);
                   3801:         }
                   3802:     }
                   3803:     if (-e '/home/httpd/html/adm/lonDomLogos/'.$udom.'.gif') {
                   3804:         $legacyhash{$udom.'.login.domlogo'} = "/adm/lonDomLogos/$udom.gif";
                   3805:     }
                   3806:     return %legacyhash;
                   3807: }
                   3808: 
1.63      www      3809: =pod
                   3810: 
1.112     bowersj2 3811: =item * &domainlogo()
1.63      www      3812: 
                   3813: Inputs: $domain (usually will be undef)
                   3814: 
                   3815: Returns: A link to a domain logo, if the domain logo exists.
                   3816: If the domain logo does not exist, a description of the domain.
                   3817: 
                   3818: =cut
1.112     bowersj2 3819: 
1.63      www      3820: ###############################################
                   3821: sub domainlogo {
1.517     raeburn  3822:     my $domain = &determinedomain(shift);
1.518     albertel 3823:     my %designhash = &get_domainconf($domain);    
1.517     raeburn  3824:     # See if there is a logo
                   3825:     if ($designhash{$domain.'.login.domlogo'} ne '') {
1.519     raeburn  3826:         my $imgsrc = $designhash{$domain.'.login.domlogo'};
1.538     albertel 3827:         if ($imgsrc =~ m{^/(adm|res)/}) {
                   3828: 	    if ($imgsrc =~ m{^/res/}) {
                   3829: 		my $local_name = &Apache::lonnet::filelocation('',$imgsrc);
                   3830: 		&Apache::lonnet::repcopy($local_name);
                   3831: 	    }
                   3832: 	   $imgsrc = &lonhttpdurl($imgsrc);
1.519     raeburn  3833:         } 
                   3834:         return '<img src="'.$imgsrc.'" alt="'.$domain.'" />';
1.514     albertel 3835:     } elsif (defined(&Apache::lonnet::domain($domain,'description'))) {
                   3836:         return &Apache::lonnet::domain($domain,'description');
1.59      www      3837:     } else {
1.60      matthew  3838:         return '';
1.59      www      3839:     }
                   3840: }
1.63      www      3841: ##############################################
                   3842: 
                   3843: =pod
                   3844: 
1.112     bowersj2 3845: =item * &designparm()
1.63      www      3846: 
                   3847: Inputs: $which parameter; $domain (usually will be undef)
                   3848: 
                   3849: Returns: value of designparamter $which
                   3850: 
                   3851: =cut
1.112     bowersj2 3852: 
1.397     albertel 3853: 
1.400     albertel 3854: ##############################################
1.397     albertel 3855: sub designparm {
                   3856:     my ($which,$domain)=@_;
1.258     albertel 3857:     if ($env{'browser.blackwhite'} eq 'on') {
1.635     raeburn  3858: 	if ($which=~/\.(font|alink|vlink|link|textcol)$/) {
1.110     www      3859: 	    return '#000000';
                   3860: 	}
1.635     raeburn  3861: 	if ($which=~/\.(pgbg|sidebg|bgcol)$/) {
1.110     www      3862: 	    return '#FFFFFF';
                   3863: 	}
                   3864: 	if ($which=~/\.tabbg$/) {
                   3865: 	    return '#CCCCCC';
                   3866: 	}
                   3867:     }
1.397     albertel 3868:     if (exists($env{'environment.color.'.$which})) {
1.258     albertel 3869: 	return $env{'environment.color.'.$which};
1.96      www      3870:     }
1.63      www      3871:     $domain=&determinedomain($domain);
1.518     albertel 3872:     my %domdesign = &get_domainconf($domain);
1.520     raeburn  3873:     my $output;
1.517     raeburn  3874:     if ($domdesign{$domain.'.'.$which} ne '') {
1.520     raeburn  3875: 	$output = $domdesign{$domain.'.'.$which};
1.63      www      3876:     } else {
1.520     raeburn  3877:         $output = $defaultdesign{$which};
                   3878:     }
                   3879:     if (($which =~ /^(student|coordinator|author|admin)\.img$/) ||
1.635     raeburn  3880:         ($which =~ /login\.(img|logo|domlogo|login)/)) {
1.538     albertel 3881:         if ($output =~ m{^/(adm|res)/}) {
                   3882: 	    if ($output =~ m{^/res/}) {
                   3883: 		my $local_name = &Apache::lonnet::filelocation('',$output);
                   3884: 		&Apache::lonnet::repcopy($local_name);
                   3885: 	    }
1.520     raeburn  3886:             $output = &lonhttpdurl($output);
                   3887:         }
1.63      www      3888:     }
1.520     raeburn  3889:     return $output;
1.63      www      3890: }
1.59      www      3891: 
1.60      matthew  3892: ###############################################
                   3893: ###############################################
                   3894: 
                   3895: =pod
                   3896: 
1.112     bowersj2 3897: =back
                   3898: 
1.549     albertel 3899: =head1 HTML Helpers
1.112     bowersj2 3900: 
                   3901: =over 4
                   3902: 
                   3903: =item * &bodytag()
1.60      matthew  3904: 
                   3905: Returns a uniform header for LON-CAPA web pages.
                   3906: 
                   3907: Inputs: 
                   3908: 
1.112     bowersj2 3909: =over 4
                   3910: 
                   3911: =item * $title, A title to be displayed on the page.
                   3912: 
                   3913: =item * $function, the current role (can be undef).
                   3914: 
                   3915: =item * $addentries, extra parameters for the <body> tag.
                   3916: 
                   3917: =item * $bodyonly, if defined, only return the <body> tag.
                   3918: 
                   3919: =item * $domain, if defined, force a given domain.
                   3920: 
                   3921: =item * $forcereg, if page should register as content page (relevant for 
1.86      www      3922:             text interface only)
1.60      matthew  3923: 
1.326     albertel 3924: =item * $customtitle, alternate text to use instead of $title
                   3925:                       in the title box that appears, this text
                   3926:                       is not auto translated like the $title is
1.309     albertel 3927: 
                   3928: =item * $notopbar, if true, keep the 'what is this' info but remove the
                   3929:                    navigational links
1.317     albertel 3930: 
1.338     albertel 3931: =item * $bgcolor, used to override the bgcolor on a webpage to a specific value
                   3932: 
                   3933: =item * $notitle, if true keep the nav controls, but remove the title bar
                   3934: 
1.361     albertel 3935: =item * $no_inline_link, if true and in remote mode, don't show the 
                   3936:          'Switch To Inline Menu' link
                   3937: 
1.460     albertel 3938: =item * $args, optional argument valid values are
                   3939:             no_auto_mt_title -> prevents &mt()ing the title arg
1.562     albertel 3940:             inherit_jsmath -> when creating popup window in a page,
                   3941:                               should it have jsmath forced on by the
                   3942:                               current page
1.460     albertel 3943: 
1.112     bowersj2 3944: =back
                   3945: 
1.60      matthew  3946: Returns: A uniform header for LON-CAPA web pages.  
                   3947: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
                   3948: If $bodyonly is undef or zero, an html string containing a <body> tag and 
                   3949: other decorations will be returned.
                   3950: 
                   3951: =cut
                   3952: 
1.54      www      3953: sub bodytag {
1.309     albertel 3954:     my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,$customtitle,
1.460     albertel 3955: 	$notopbar,$bgcolor,$notitle,$no_inline_link,$args)=@_;
1.339     albertel 3956: 
1.460     albertel 3957:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
1.339     albertel 3958: 
1.183     matthew  3959:     $function = &get_users_function() if (!$function);
1.339     albertel 3960:     my $img =    &designparm($function.'.img',$domain);
                   3961:     my $font =   &designparm($function.'.font',$domain);
                   3962:     my $pgbg   = $bgcolor || &designparm($function.'.pgbg',$domain);
                   3963: 
                   3964:     my %design = ( 'style'   => 'margin-top: 0px',
1.535     albertel 3965: 		   'bgcolor' => $pgbg,
1.339     albertel 3966: 		   'text'    => $font,
                   3967:                    'alink'   => &designparm($function.'.alink',$domain),
                   3968: 		   'vlink'   => &designparm($function.'.vlink',$domain),
                   3969: 		   'link'    => &designparm($function.'.link',$domain),);
1.438     albertel 3970:     @design{keys(%$addentries)} = @$addentries{keys(%$addentries)}; 
1.339     albertel 3971: 
1.63      www      3972:  # role and realm
1.378     raeburn  3973:     my ($role,$realm) = split(/\./,$env{'request.role'},2);
                   3974:     if ($role  eq 'ca') {
1.479     albertel 3975:         my ($rdom,$rname) = ($realm =~ m{^/($match_domain)/($match_username)$});
1.500     albertel 3976:         $realm = &plainname($rname,$rdom);
1.378     raeburn  3977:     } 
1.55      www      3978: # realm
1.258     albertel 3979:     if ($env{'request.course.id'}) {
1.378     raeburn  3980:         if ($env{'request.role'} !~ /^cr/) {
                   3981:             $role = &Apache::lonnet::plaintext($role,&course_type());
                   3982:         }
1.359     albertel 3983: 	$realm = $env{'course.'.$env{'request.course.id'}.'.description'};
1.378     raeburn  3984:     } else {
                   3985:         $role = &Apache::lonnet::plaintext($role);
1.54      www      3986:     }
1.433     albertel 3987: 
1.359     albertel 3988:     if (!$realm) { $realm='&nbsp;'; }
1.55      www      3989: # Set messages
1.60      matthew  3990:     my $messages=&domainlogo($domain);
1.330     albertel 3991: 
1.438     albertel 3992:     my $extra_body_attr = &make_attr_string($forcereg,\%design);
1.329     albertel 3993: 
1.101     www      3994: # construct main body tag
1.359     albertel 3995:     my $bodytag = "<body $extra_body_attr>".
1.562     albertel 3996: 	&Apache::lontexconvert::init_math_support($args->{'inherit_jsmath'});
1.252     albertel 3997: 
1.530     albertel 3998:     if ($bodyonly) {
1.60      matthew  3999:         return $bodytag;
1.258     albertel 4000:     } elsif ($env{'browser.interface'} eq 'textual') {
1.95      www      4001: # Accessibility
1.224     raeburn  4002:           
1.337     albertel 4003: 	$bodytag.=&Apache::lonmenu::menubuttons($forcereg,$forcereg);
1.338     albertel 4004: 	if (!$notitle) {
1.337     albertel 4005: 	    $bodytag.='<h1>LON-CAPA: '.$title.'</h1>';
                   4006: 	}
                   4007: 	return $bodytag;
1.359     albertel 4008:     }
                   4009: 
1.410     albertel 4010:     my $name = &plainname($env{'user.name'},$env{'user.domain'});
1.433     albertel 4011:     if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   4012: 	undef($role);
1.434     albertel 4013:     } else {
                   4014: 	$name = &aboutmewrapper($name,$env{'user.name'},$env{'user.domain'});
1.433     albertel 4015:     }
1.359     albertel 4016:     
                   4017:     my $roleinfo=(<<ENDROLE);
                   4018: <td class="LC_title_bar_who">
                   4019: <div class="LC_title_bar_name">
1.410     albertel 4020:     $name
1.361     albertel 4021:     &nbsp;
1.359     albertel 4022: </div>
                   4023: <div class="LC_title_bar_role">
1.361     albertel 4024: $role&nbsp;
1.359     albertel 4025: </div>
                   4026: <div class="LC_title_bar_realm">
1.361     albertel 4027: $realm&nbsp;
1.359     albertel 4028: </div>
1.206     albertel 4029: </td>
                   4030: ENDROLE
1.235     raeburn  4031: 
1.359     albertel 4032:     my $titleinfo = '<span class="LC_title_bar_title">'.$title.'</span>';
                   4033:     if ($customtitle) {
                   4034:         $titleinfo = $customtitle;
                   4035:     }
                   4036:     #
                   4037:     # Extra info if you are the DC
                   4038:     my $dc_info = '';
                   4039:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
                   4040:                         $env{'course.'.$env{'request.course.id'}.
                   4041:                                  '.domain'}.'/'})) {
                   4042:         my $cid = $env{'request.course.id'};
                   4043:         $dc_info.= $cid.' '.$env{'course.'.$cid.'.internal.coursecode'};
1.380     www      4044:         $dc_info =~ s/\s+$//;
1.359     albertel 4045:         $dc_info = '('.$dc_info.')';
                   4046:     }
                   4047: 
1.644     www      4048:     if (($env{'environment.remote'} eq 'off') || ($args->{'suppress_header_logos'})) {
1.359     albertel 4049:         # No Remote
1.258     albertel 4050: 	if ($env{'request.state'} eq 'construct') {
1.359     albertel 4051: 	    $forcereg=1;
                   4052: 	}
                   4053: 
                   4054: 	if (!$customtitle && $env{'request.state'} eq 'construct') {
                   4055: 	    # this is for resources; directories have customtitle, and crumbs
                   4056:             # and select recent are created in lonpubdir.pm  
1.229     albertel 4057: 	    my ($uname,$thisdisfn)=
1.258     albertel 4058: 		($env{'request.filename'} =~ m|^/home/([^/]+)/public_html/(.*)|);
1.229     albertel 4059: 	    my $formaction='/priv/'.$uname.'/'.$thisdisfn;
                   4060: 	    $formaction=~s/\/+/\//g;
                   4061: 
1.359     albertel 4062: 	    my $parentpath = '';
                   4063: 	    my $lastitem = '';
                   4064: 	    if ($thisdisfn =~ m-(.+/)([^/]*)$-) {
                   4065: 		$parentpath = $1;
                   4066: 		$lastitem = $2;
                   4067: 	    } else {
                   4068: 		$lastitem = $thisdisfn;
                   4069: 	    }
                   4070: 	    $titleinfo = 
1.640     bisitz   4071: 		&Apache::loncommon::help_open_menu('','',3,'Authoring')
                   4072: 		.'<b>'.&mt('Construction Space').'</b>:&nbsp;'
                   4073: 		.'<form name="dirs" method="post" action="'.$formaction
1.359     albertel 4074: 		.'" target="_top"><tt><b>'
                   4075: 		.&Apache::lonhtmlcommon::crumbs($uname.'/'.$parentpath,'_top','/priv','','+1',1)."<font size=\"+1\">$lastitem</font></b></tt><br />"
                   4076: 		.&Apache::lonhtmlcommon::select_recent('construct','recent','this.form.action=this.form.recent.value;this.form.submit()')
                   4077: 		.'</form>'
                   4078: 		.&Apache::lonmenu::constspaceform();
1.235     raeburn  4079:         }
1.359     albertel 4080: 
1.337     albertel 4081:         my $titletable;
1.338     albertel 4082: 	if (!$notitle) {
1.337     albertel 4083: 	    $titletable =
1.359     albertel 4084: 		'<table id="LC_title_bar">'.
                   4085:                          "<tr><td> $titleinfo $dc_info</td>".$roleinfo.
                   4086: 			 '</tr></table>';
1.337     albertel 4087: 	}
1.359     albertel 4088: 	if ($notopbar) {
                   4089: 	    $bodytag .= $titletable;
                   4090: 	} else {
                   4091: 	    if ($env{'request.state'} eq 'construct') {
1.337     albertel 4092:                 $bodytag .= &Apache::lonmenu::menubuttons($forcereg,$forcereg,
                   4093: 							  $titletable);
1.272     raeburn  4094:             } else {
1.336     albertel 4095:                 $bodytag .= &Apache::lonmenu::menubuttons($forcereg,$forcereg).
1.359     albertel 4096: 		    $titletable;
1.272     raeburn  4097:             }
1.235     raeburn  4098:         }
                   4099:         return $bodytag;
1.94      www      4100:     }
1.95      www      4101: 
1.93      www      4102: #
1.95      www      4103: # Top frame rendering, Remote is up
1.93      www      4104: #
1.359     albertel 4105: 
1.517     raeburn  4106:     my $imgsrc = $img;
                   4107:     if ($img =~ /^\/adm/) {
1.575     albertel 4108:         $imgsrc = &lonhttpdurl($img);
1.517     raeburn  4109:     }
                   4110:     my $upperleft='<img src="'.$imgsrc.'" alt="'.$function.'" />';
1.359     albertel 4111: 
1.305     www      4112:     # Explicit link to get inline menu
1.361     albertel 4113:     my $menu= ($no_inline_link?''
                   4114: 	       :'<br /><a href="/adm/remote?action=collapse">'.&mt('Switch to Inline Menu Mode').'</a>');
1.245     matthew  4115:     #
1.338     albertel 4116:     if ($notitle) {
1.337     albertel 4117: 	return $bodytag;
                   4118:     }
1.94      www      4119:     return(<<ENDBODY);
1.60      matthew  4120: $bodytag
1.359     albertel 4121: <table id="LC_title_bar" class="LC_with_remote">
1.368     albertel 4122: <tr><td class="LC_title_bar_role_logo">$upperleft</td>
1.359     albertel 4123:     <td class="LC_title_bar_domain_logo">$messages&nbsp;</td>
1.54      www      4124: </tr>
1.359     albertel 4125: <tr><td>$titleinfo $dc_info $menu</td>
                   4126: $roleinfo
1.368     albertel 4127: </tr>
1.356     albertel 4128: </table>
1.54      www      4129: ENDBODY
1.182     matthew  4130: }
                   4131: 
1.330     albertel 4132: sub make_attr_string {
                   4133:     my ($register,$attr_ref) = @_;
                   4134: 
                   4135:     if ($attr_ref && !ref($attr_ref)) {
                   4136: 	die("addentries Must be a hash ref ".
                   4137: 	    join(':',caller(1))." ".
                   4138: 	    join(':',caller(0))." ");
                   4139:     }
                   4140: 
                   4141:     if ($register) {
1.339     albertel 4142: 	my ($on_load,$on_unload);
                   4143: 	foreach my $key (keys(%{$attr_ref})) {
                   4144: 	    if      (lc($key) eq 'onload') {
                   4145: 		$on_load.=$attr_ref->{$key}.';';
                   4146: 		delete($attr_ref->{$key});
                   4147: 
                   4148: 	    } elsif (lc($key) eq 'onunload') {
                   4149: 		$on_unload.=$attr_ref->{$key}.';';
                   4150: 		delete($attr_ref->{$key});
                   4151: 	    }
                   4152: 	}
                   4153: 	$attr_ref->{'onload'}  =
                   4154: 	    &Apache::lonmenu::loadevents().  $on_load;
                   4155: 	$attr_ref->{'onunload'}=
                   4156: 	    &Apache::lonmenu::unloadevents().$on_unload;
                   4157:     }
                   4158: 
                   4159: # Accessibility font enhance
                   4160:     if ($env{'browser.fontenhance'} eq 'on') {
                   4161: 	my $style;
                   4162: 	foreach my $key (keys(%{$attr_ref})) {
                   4163: 	    if (lc($key) eq 'style') {
                   4164: 		$style.=$attr_ref->{$key}.';';
                   4165: 		delete($attr_ref->{$key});
                   4166: 	    }
                   4167: 	}
                   4168: 	$attr_ref->{'style'}=$style.'; font-size: x-large;';
1.330     albertel 4169:     }
1.339     albertel 4170: 
                   4171:     if ($env{'browser.blackwhite'} eq 'on') {
                   4172: 	delete($attr_ref->{'font'});
                   4173: 	delete($attr_ref->{'link'});
                   4174: 	delete($attr_ref->{'alink'});
                   4175: 	delete($attr_ref->{'vlink'});
                   4176: 	delete($attr_ref->{'bgcolor'});
                   4177: 	delete($attr_ref->{'background'});
                   4178:     }
                   4179: 
1.330     albertel 4180:     my $attr_string;
                   4181:     foreach my $attr (keys(%$attr_ref)) {
                   4182: 	$attr_string .= " $attr=\"".$attr_ref->{$attr}.'" ';
                   4183:     }
                   4184:     return $attr_string;
                   4185: }
                   4186: 
                   4187: 
1.182     matthew  4188: ###############################################
1.251     albertel 4189: ###############################################
                   4190: 
                   4191: =pod
                   4192: 
                   4193: =item * &endbodytag()
                   4194: 
                   4195: Returns a uniform footer for LON-CAPA web pages.
                   4196: 
1.635     raeburn  4197: Inputs: 1 - optional reference to an args hash
                   4198: If in the hash, key for noredirectlink has a value which evaluates to true,
                   4199: a 'Continue' link is not displayed if the page contains an
                   4200: internal redirect in the <head></head> section,
                   4201: i.e., $env{'internal.head.redirect'} exists   
1.251     albertel 4202: 
                   4203: =cut
                   4204: 
                   4205: sub endbodytag {
1.635     raeburn  4206:     my ($args) = @_;
1.251     albertel 4207:     my $endbodytag='</body>';
1.269     albertel 4208:     $endbodytag=&Apache::lontexconvert::jsMath_process()."\n".$endbodytag;
1.315     albertel 4209:     if ( exists( $env{'internal.head.redirect'} ) ) {
1.635     raeburn  4210:         if (!(ref($args) eq 'HASH' && $args->{'noredirectlink'})) {
                   4211: 	    $endbodytag=
                   4212: 	        "<br /><a href=\"$env{'internal.head.redirect'}\">".
                   4213: 	        &mt('Continue').'</a>'.
                   4214: 	        $endbodytag;
                   4215:         }
1.315     albertel 4216:     }
1.251     albertel 4217:     return $endbodytag;
                   4218: }
                   4219: 
1.352     albertel 4220: =pod
                   4221: 
                   4222: =item * &standard_css()
                   4223: 
                   4224: Returns a style sheet
                   4225: 
                   4226: Inputs: (all optional)
                   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
                   4231: 
                   4232: =cut
                   4233: 
1.343     albertel 4234: sub standard_css {
1.345     albertel 4235:     my ($function,$domain,$bgcolor) = @_;
1.352     albertel 4236:     $function  = &get_users_function() if (!$function);
                   4237:     my $img    = &designparm($function.'.img',   $domain);
                   4238:     my $tabbg  = &designparm($function.'.tabbg', $domain);
                   4239:     my $font   = &designparm($function.'.font',  $domain);
1.345     albertel 4240:     my $sidebg = &designparm($function.'.sidebg',$domain);
1.382     albertel 4241:     my $pgbg_or_bgcolor =
                   4242: 	         $bgcolor ||
1.352     albertel 4243: 	         &designparm($function.'.pgbg',  $domain);
1.382     albertel 4244:     my $pgbg   = &designparm($function.'.pgbg',  $domain);
1.352     albertel 4245:     my $alink  = &designparm($function.'.alink', $domain);
                   4246:     my $vlink  = &designparm($function.'.vlink', $domain);
                   4247:     my $link   = &designparm($function.'.link',  $domain);
                   4248: 
1.602     albertel 4249:     my $sans                 = 'Verdana,Arial,Helvetica,sans-serif';
1.395     albertel 4250:     my $mono                 = 'monospace';
1.352     albertel 4251:     my $data_table_head      = $tabbg;
                   4252:     my $data_table_light     = '#EEEEEE';
1.470     banghart 4253:     my $data_table_dark      = '#DDDDDD';
                   4254:     my $data_table_darker    = '#CCCCCC';
1.349     albertel 4255:     my $data_table_highlight = '#FFFF00';
1.352     albertel 4256:     my $mail_new             = '#FFBB77';
                   4257:     my $mail_new_hover       = '#DD9955';
                   4258:     my $mail_read            = '#BBBB77';
                   4259:     my $mail_read_hover      = '#999944';
                   4260:     my $mail_replied         = '#AAAA88';
                   4261:     my $mail_replied_hover   = '#888855';
                   4262:     my $mail_other           = '#99BBBB';
                   4263:     my $mail_other_hover     = '#669999';
1.391     albertel 4264:     my $table_header         = '#DDDDDD';
1.489     raeburn  4265:     my $feedback_link_bg     = '#BBBBBB';
1.392     albertel 4266: 
1.608     albertel 4267:     my $border = ($env{'browser.type'} eq 'explorer' ||
                   4268: 		  $env{'browser.type'} eq 'safari'     ) ? '0px 2px 0px 2px'
                   4269: 	                                                 : '0px 3px 0px 4px';
1.448     albertel 4270: 
1.523     albertel 4271: 
1.343     albertel 4272:     return <<END;
1.345     albertel 4273: h1, h2, h3, th { font-family: $sans }
1.343     albertel 4274: a:focus { color: red; background: yellow } 
1.510     albertel 4275: table.thinborder,
1.523     albertel 4276: 
1.510     albertel 4277: table.thinborder tr th {
                   4278:   border-style: solid;
                   4279:   border-width: 1px;
                   4280:   background: $tabbg;
                   4281: }
1.523     albertel 4282: table.thinborder tr td {
1.510     albertel 4283:   border-style: solid;
                   4284:   border-width: 1px
                   4285: }
1.426     albertel 4286: 
1.343     albertel 4287: form, .inline { display: inline; }
                   4288: .center { text-align: center; }
1.593     albertel 4289: .LC_filename {font-family: $mono; white-space:pre;}
1.350     albertel 4290: .LC_error {
                   4291:   color: red;
                   4292:   font-size: larger;
                   4293: }
1.457     albertel 4294: .LC_warning,
                   4295: .LC_diff_removed {
1.394     albertel 4296:   color: red;
                   4297: }
1.532     albertel 4298: 
                   4299: .LC_info,
1.457     albertel 4300: .LC_success,
                   4301: .LC_diff_added {
1.350     albertel 4302:   color: green;
                   4303: }
1.543     albertel 4304: .LC_unknown {
                   4305:   color: yellow;
                   4306: }
                   4307: 
1.440     albertel 4308: .LC_icon {
                   4309:   border: 0px;
                   4310: }
1.539     albertel 4311: .LC_indexer_icon {
                   4312:   border: 0px;
                   4313:   height: 22px;
                   4314: }
1.543     albertel 4315: .LC_docs_spacer {
                   4316:   width: 25px;
                   4317:   height: 1px;
                   4318:   border: 0px;
                   4319: }
1.346     albertel 4320: 
1.532     albertel 4321: .LC_internal_info {
                   4322:   color: #999;
                   4323: }
                   4324: 
1.458     albertel 4325: table.LC_pastsubmission {
                   4326:   border: 1px solid black;
                   4327:   margin: 2px;
                   4328: }
                   4329: 
1.606     albertel 4330: table#LC_top_nav, table#LC_menubuttons,table#LC_nav_location {
1.345     albertel 4331:   width: 100%;
                   4332:   background: $pgbg;
1.392     albertel 4333:   border: 2px;
1.402     albertel 4334:   border-collapse: separate;
1.403     albertel 4335:   padding: 0px;
1.345     albertel 4336: }
1.392     albertel 4337: 
1.606     albertel 4338: table#LC_title_bar, table.LC_breadcrumbs, 
1.393     albertel 4339: table#LC_title_bar.LC_with_remote {
1.359     albertel 4340:   width: 100%;
1.392     albertel 4341:   border-color: $pgbg;
                   4342:   border-style: solid;
                   4343:   border-width: $border;
                   4344: 
1.379     albertel 4345:   background: $pgbg;
                   4346:   font-family: $sans;
1.392     albertel 4347:   border-collapse: collapse;
1.403     albertel 4348:   padding: 0px;
1.359     albertel 4349: }
1.392     albertel 4350: 
1.409     albertel 4351: table.LC_docs_path {
                   4352:   width: 100%;
                   4353:   border: 0;
                   4354:   background: $pgbg;
                   4355:   font-family: $sans;
                   4356:   border-collapse: collapse;
                   4357:   padding: 0px;
                   4358: }
                   4359: 
1.359     albertel 4360: table#LC_title_bar td {
                   4361:   background: $tabbg;
                   4362: }
                   4363: table#LC_title_bar td.LC_title_bar_who {
                   4364:   background: $tabbg;
                   4365:   color: $font;
1.427     albertel 4366:   font: small $sans;
1.359     albertel 4367:   text-align: right;
                   4368: }
1.469     banghart 4369: span.LC_metadata {
                   4370:     font-family: $sans;
                   4371: }
1.359     albertel 4372: span.LC_title_bar_title {
1.416     albertel 4373:   font: bold x-large $sans;
1.359     albertel 4374: }
                   4375: table#LC_title_bar td.LC_title_bar_domain_logo {
                   4376:   background: $sidebg;
                   4377:   text-align: right;
1.368     albertel 4378:   padding: 0px;
                   4379: }
                   4380: table#LC_title_bar td.LC_title_bar_role_logo {
                   4381:   background: $sidebg;
                   4382:   padding: 0px;
1.359     albertel 4383: }
                   4384: 
1.346     albertel 4385: table#LC_menubuttons_mainmenu {
1.526     www      4386:   width: 100%;
1.346     albertel 4387:   border: 0px;
                   4388:   border-spacing: 1px;
1.372     albertel 4389:   padding: 0px 1px;
1.346     albertel 4390:   margin: 0px;
                   4391:   border-collapse: separate;
                   4392: }
                   4393: table#LC_menubuttons img, table#LC_menubuttons_mainmenu img {
                   4394:   border: 0px;
                   4395: }
1.345     albertel 4396: table#LC_top_nav td {
                   4397:   background: $tabbg;
1.392     albertel 4398:   border: 0px;
1.407     albertel 4399:   font-size: small;
1.345     albertel 4400: }
                   4401: table#LC_top_nav td a, div#LC_top_nav a {
                   4402:   color: $font;
                   4403:   font-family: $sans;
                   4404: }
1.364     albertel 4405: table#LC_top_nav td.LC_top_nav_logo {
                   4406:   background: $tabbg;
1.432     albertel 4407:   text-align: left;
1.408     albertel 4408:   white-space: nowrap;
1.432     albertel 4409:   width: 31px;
1.408     albertel 4410: }
                   4411: table#LC_top_nav td.LC_top_nav_logo img {
1.432     albertel 4412:   border: 0px;
1.408     albertel 4413:   vertical-align: bottom;
1.364     albertel 4414: }
1.432     albertel 4415: table#LC_top_nav td.LC_top_nav_exit,
                   4416: table#LC_top_nav td.LC_top_nav_help {
                   4417:   width: 2.0em;
                   4418: }
1.442     albertel 4419: table#LC_top_nav td.LC_top_nav_login {
                   4420:   width: 4.0em;
                   4421:   text-align: center;
                   4422: }
1.409     albertel 4423: table.LC_breadcrumbs td, table.LC_docs_path td  {
1.357     albertel 4424:   background: $tabbg;
                   4425:   color: $font;
                   4426:   font-family: $sans;
1.358     albertel 4427:   font-size: smaller;
1.357     albertel 4428: }
1.411     albertel 4429: table.LC_breadcrumbs td.LC_breadcrumbs_component,
1.409     albertel 4430: table.LC_docs_path td.LC_docs_path_component {
1.357     albertel 4431:   background: $tabbg;
                   4432:   color: $font;
                   4433:   font-family: $sans;
                   4434:   font-size: larger;
                   4435:   text-align: right;
                   4436: }
1.383     albertel 4437: td.LC_table_cell_checkbox {
                   4438:   text-align: center;
                   4439: }
                   4440: 
1.522     albertel 4441: table#LC_mainmenu td.LC_mainmenu_column {
                   4442:     vertical-align: top;
                   4443: }
                   4444: 
1.346     albertel 4445: .LC_menubuttons_inline_text {
                   4446:   color: $font;
                   4447:   font-family: $sans;
                   4448:   font-size: smaller;
                   4449: }
                   4450: 
1.526     www      4451: .LC_menubuttons_link {
                   4452:   text-decoration: none;
                   4453: }
                   4454: 
1.522     albertel 4455: .LC_menubuttons_category {
1.521     www      4456:   color: $font;
1.526     www      4457:   background: $pgbg;
1.521     www      4458:   font-family: $sans;
                   4459:   font-size: larger;
                   4460:   font-weight: bold;
                   4461: }
                   4462: 
1.346     albertel 4463: td.LC_menubuttons_text {
1.526     www      4464:   width: 90%;
1.346     albertel 4465:   color: $font;
                   4466:   font-family: $sans;
                   4467: }
1.526     www      4468: 
1.346     albertel 4469: td.LC_menubuttons_img {
                   4470: }
1.526     www      4471: 
1.346     albertel 4472: .LC_current_location {
                   4473:   font-family: $sans;
                   4474:   background: $tabbg;
                   4475: }
                   4476: .LC_new_mail {
                   4477:   font-family: $sans;
1.634     www      4478:   background: $tabbg;
1.346     albertel 4479:   font-weight: bold;
                   4480: }
1.347     albertel 4481: 
1.526     www      4482: .LC_rolesmenu_is {
                   4483:   font-family: $sans;
                   4484: }
                   4485: 
                   4486: .LC_rolesmenu_selected {
                   4487:   font-family: $sans;
                   4488: }
                   4489: 
                   4490: .LC_rolesmenu_future {
                   4491:   font-family: $sans;
                   4492: }
                   4493: 
                   4494: 
                   4495: .LC_rolesmenu_will {
                   4496:   font-family: $sans;
                   4497: }
                   4498: 
                   4499: .LC_rolesmenu_will_not {
                   4500:   font-family: $sans;
                   4501: }
                   4502: 
                   4503: .LC_rolesmenu_expired {
                   4504:   font-family: $sans;
                   4505: }
                   4506: 
                   4507: .LC_rolesinfo {
                   4508:   font-family: $sans;
                   4509: }
                   4510: 
1.527     www      4511: .LC_dropadd_labeltext {
                   4512:   font-family: $sans;
                   4513:   text-align: right;
                   4514: }
                   4515: 
                   4516: .LC_preferences_labeltext {
                   4517:   font-family: $sans;
                   4518:   text-align: right;
                   4519: }
                   4520: 
1.440     albertel 4521: table.LC_aboutme_port {
                   4522:   border: 0px;
                   4523:   border-collapse: collapse;
                   4524:   border-spacing: 0px;
                   4525: }
1.349     albertel 4526: table.LC_data_table, table.LC_mail_list {
1.347     albertel 4527:   border: 1px solid #000000;
1.402     albertel 4528:   border-collapse: separate;
1.426     albertel 4529:   border-spacing: 1px;
1.610     albertel 4530:   background: $pgbg;
1.347     albertel 4531: }
1.422     albertel 4532: .LC_data_table_dense {
                   4533:   font-size: small;
                   4534: }
1.507     raeburn  4535: table.LC_nested_outer {
                   4536:   border: 1px solid #000000;
1.589     raeburn  4537:   border-collapse: collapse;
1.507     raeburn  4538:   border-spacing: 0px;
                   4539:   width: 100%;
                   4540: }
                   4541: table.LC_nested {
                   4542:   border: 0px;
1.589     raeburn  4543:   border-collapse: collapse;
1.507     raeburn  4544:   border-spacing: 0px;
                   4545:   width: 100%;
                   4546: }
1.523     albertel 4547: table.LC_data_table tr th, table.LC_calendar tr th, table.LC_mail_list tr th,
                   4548: table.LC_prior_tries tr th {
1.349     albertel 4549:   font-weight: bold;
                   4550:   background-color: $data_table_head;
1.421     albertel 4551:   font-size: smaller;
1.347     albertel 4552: }
1.610     albertel 4553: table.LC_data_table tr.LC_odd_row > td, 
1.440     albertel 4554: table.LC_aboutme_port tr td {
1.349     albertel 4555:   background-color: $data_table_light;
1.425     albertel 4556:   padding: 2px;
1.347     albertel 4557: }
1.610     albertel 4558: table.LC_data_table tr.LC_even_row > td,
1.440     albertel 4559: table.LC_aboutme_port tr.LC_even_row td {
1.349     albertel 4560:   background-color: $data_table_dark;
1.347     albertel 4561: }
1.425     albertel 4562: table.LC_data_table tr.LC_data_table_highlight td {
                   4563:   background-color: $data_table_darker;
                   4564: }
1.639     raeburn  4565: table.LC_data_table tr td.LC_leftcol_header {
                   4566:   background-color: $data_table_head;
                   4567:   font-weight: bold;
                   4568: }
1.451     albertel 4569: table.LC_data_table tr.LC_empty_row td,
1.507     raeburn  4570: table.LC_nested tr.LC_empty_row td {
1.347     albertel 4571:   background-color: #FFFFFF;
1.421     albertel 4572:   font-weight: bold;
                   4573:   font-style: italic;
                   4574:   text-align: center;
                   4575:   padding: 8px;
1.347     albertel 4576: }
1.507     raeburn  4577: table.LC_nested tr.LC_empty_row td {
1.465     albertel 4578:   padding: 4ex
                   4579: }
1.507     raeburn  4580: table.LC_nested_outer tr th {
                   4581:   font-weight: bold;
                   4582:   background-color: $data_table_head;
                   4583:   font-size: smaller;
                   4584:   border-bottom: 1px solid #000000;
                   4585: }
                   4586: table.LC_nested_outer tr td.LC_subheader {
                   4587:   background-color: $data_table_head;
                   4588:   font-weight: bold;
                   4589:   font-size: small;
                   4590:   border-bottom: 1px solid #000000;
                   4591:   text-align: right;
1.451     albertel 4592: }
1.507     raeburn  4593: table.LC_nested tr.LC_info_row td {
1.451     albertel 4594:   background-color: #CCC;
                   4595:   font-weight: bold;
                   4596:   font-size: small;
1.507     raeburn  4597:   text-align: center;
                   4598: }
1.589     raeburn  4599: table.LC_nested tr.LC_info_row td.LC_left_item,
                   4600: table.LC_nested_outer tr th.LC_left_item {
1.507     raeburn  4601:   text-align: left;
1.451     albertel 4602: }
1.507     raeburn  4603: table.LC_nested td {
1.451     albertel 4604:   background-color: #FFF;
                   4605:   font-size: small;
1.507     raeburn  4606: }
                   4607: table.LC_nested_outer tr th.LC_right_item,
                   4608: table.LC_nested tr.LC_info_row td.LC_right_item,
                   4609: table.LC_nested tr.LC_odd_row td.LC_right_item,
                   4610: table.LC_nested tr td.LC_right_item {
1.451     albertel 4611:   text-align: right;
                   4612: }
                   4613: 
1.507     raeburn  4614: table.LC_nested tr.LC_odd_row td {
1.451     albertel 4615:   background-color: #EEE;
                   4616: }
                   4617: 
1.473     raeburn  4618: table.LC_createuser {
                   4619: }
                   4620: 
                   4621: table.LC_createuser tr.LC_section_row td {
                   4622:   font-size: smaller;
                   4623: }
                   4624: 
                   4625: table.LC_createuser tr.LC_info_row td  {
                   4626:   background-color: #CCC;
                   4627:   font-weight: bold;
                   4628:   text-align: center;
                   4629: }
                   4630: 
1.349     albertel 4631: table.LC_calendar {
                   4632:   border: 1px solid #000000;
                   4633:   border-collapse: collapse;
                   4634: }
                   4635: table.LC_calendar_pickdate {
                   4636:   font-size: xx-small;
                   4637: }
                   4638: table.LC_calendar tr td {
                   4639:   border: 1px solid #000000;
                   4640:   vertical-align: top;
                   4641: }
                   4642: table.LC_calendar tr td.LC_calendar_day_empty {
                   4643:   background-color: $data_table_dark;
                   4644: }
                   4645: table.LC_calendar tr td.LC_calendar_day_current {
                   4646:   background-color: $data_table_highlight;
                   4647: }
                   4648: 
                   4649: table.LC_mail_list tr.LC_mail_new {
                   4650:   background-color: $mail_new;
                   4651: }
                   4652: table.LC_mail_list tr.LC_mail_new:hover {
                   4653:   background-color: $mail_new_hover;
                   4654: }
                   4655: table.LC_mail_list tr.LC_mail_read {
                   4656:   background-color: $mail_read;
                   4657: }
                   4658: table.LC_mail_list tr.LC_mail_read:hover {
                   4659:   background-color: $mail_read_hover;
                   4660: }
                   4661: table.LC_mail_list tr.LC_mail_replied {
                   4662:   background-color: $mail_replied;
                   4663: }
                   4664: table.LC_mail_list tr.LC_mail_replied:hover {
                   4665:   background-color: $mail_replied_hover;
                   4666: }
                   4667: table.LC_mail_list tr.LC_mail_other {
                   4668:   background-color: $mail_other;
                   4669: }
                   4670: table.LC_mail_list tr.LC_mail_other:hover {
                   4671:   background-color: $mail_other_hover;
                   4672: }
1.494     raeburn  4673: table.LC_mail_list tr.LC_mail_even {
                   4674: }
                   4675: table.LC_mail_list tr.LC_mail_odd {
                   4676: }
                   4677: 
1.385     albertel 4678: 
1.386     albertel 4679: table#LC_portfolio_actions {
                   4680:   width: auto;
                   4681:   background: $pgbg;
                   4682:   border: 0px;
                   4683:   border-spacing: 2px 2px;
                   4684:   padding: 0px;
                   4685:   margin: 0px;
                   4686:   border-collapse: separate;
                   4687: }
                   4688: table#LC_portfolio_actions td.LC_label {
                   4689:   background: $tabbg;
                   4690:   text-align: right;
                   4691: }
                   4692: table#LC_portfolio_actions td.LC_value {
                   4693:   background: $tabbg;
                   4694: }
1.385     albertel 4695: 
1.391     albertel 4696: table#LC_cstr_controls {
                   4697:   width: 100%;
                   4698:   border-collapse: collapse;
                   4699: }
                   4700: table#LC_cstr_controls tr td {
                   4701:   border: 4px solid $pgbg;
                   4702:   padding: 4px;
                   4703:   text-align: center;
                   4704:   background: $tabbg;
                   4705: }
                   4706: table#LC_cstr_controls tr th {
                   4707:   border: 4px solid $pgbg;
                   4708:   background: $table_header;
                   4709:   text-align: center;
                   4710:   font-family: $sans;
                   4711:   font-size: smaller;
                   4712: }
                   4713: 
1.389     albertel 4714: table#LC_browser {
                   4715:  
                   4716: }
                   4717: table#LC_browser tr th {
1.391     albertel 4718:   background: $table_header;
1.389     albertel 4719: }
1.390     albertel 4720: table#LC_browser tr td {
                   4721:   padding: 2px;
                   4722: }
1.389     albertel 4723: table#LC_browser tr.LC_browser_file,
                   4724: table#LC_browser tr.LC_browser_file_published {
                   4725:   background: #CCFF88;
                   4726: }
                   4727: table#LC_browser tr.LC_browser_file_locked,
                   4728: table#LC_browser tr.LC_browser_file_unpublished {
                   4729:   background: #FFAA99;
1.387     albertel 4730: }
1.389     albertel 4731: table#LC_browser tr.LC_browser_file_obsolete {
                   4732:   background: #AAAAAA;
1.387     albertel 4733: }
1.455     albertel 4734: table#LC_browser tr.LC_browser_file_modified,
                   4735: table#LC_browser tr.LC_browser_file_metamodified {
1.389     albertel 4736:   background: #FFFF77;
1.387     albertel 4737: }
1.389     albertel 4738: table#LC_browser tr.LC_browser_folder {
                   4739:   background: #CCCCFF;
1.387     albertel 4740: }
1.388     albertel 4741: span.LC_current_location {
                   4742:   font-size: x-large;
                   4743:   background: $pgbg;
                   4744: }
1.387     albertel 4745: 
1.395     albertel 4746: span.LC_parm_menu_item {
                   4747:   font-size: larger;
                   4748:   font-family: $sans;
                   4749: }
                   4750: span.LC_parm_scope_all {
                   4751:   color: red;
                   4752: }
                   4753: span.LC_parm_scope_folder {
                   4754:   color: green;
                   4755: }
                   4756: span.LC_parm_scope_resource {
                   4757:   color: orange;
                   4758: }
                   4759: span.LC_parm_part {
                   4760:   color: blue;
                   4761: }
                   4762: span.LC_parm_folder, span.LC_parm_symb {
                   4763:   font-size: x-small;
                   4764:   font-family: $mono;
                   4765:   color: #AAAAAA;
                   4766: }
                   4767: 
1.396     albertel 4768: td.LC_parm_overview_level_menu, td.LC_parm_overview_map_menu,
                   4769: td.LC_parm_overview_parm_selectors, td.LC_parm_overview_parm_restrictions {
                   4770:   border: 1px solid black;
                   4771:   border-collapse: collapse;
                   4772: }
                   4773: table.LC_parm_overview_restrictions td {
                   4774:   border-width: 1px 4px 1px 4px;
                   4775:   border-style: solid;
                   4776:   border-color: $pgbg;
                   4777:   text-align: center;
                   4778: }
                   4779: table.LC_parm_overview_restrictions th {
                   4780:   background: $tabbg;
                   4781:   border-width: 1px 4px 1px 4px;
                   4782:   border-style: solid;
                   4783:   border-color: $pgbg;
                   4784: }
1.398     albertel 4785: table#LC_helpmenu {
                   4786:   border: 0px;
                   4787:   height: 55px;
                   4788:   border-spacing: 0px;
                   4789: }
                   4790: 
                   4791: table#LC_helpmenu fieldset legend {
                   4792:   font-size: larger;
                   4793:   font-weight: bold;
                   4794: }
1.397     albertel 4795: table#LC_helpmenu_links {
                   4796:   width: 100%;
                   4797:   border: 1px solid black;
                   4798:   background: $pgbg;
                   4799:   padding: 0px;
                   4800:   border-spacing: 1px;
                   4801: }
                   4802: table#LC_helpmenu_links tr td {
                   4803:   padding: 1px;
                   4804:   background: $tabbg;
1.399     albertel 4805:   text-align: center;
                   4806:   font-weight: bold;
1.397     albertel 4807: }
1.396     albertel 4808: 
1.397     albertel 4809: table#LC_helpmenu_links a:link, table#LC_helpmenu_links a:visited,
                   4810: table#LC_helpmenu_links a:active {
                   4811:   text-decoration: none;
                   4812:   color: $font;
                   4813: }
                   4814: table#LC_helpmenu_links a:hover {
                   4815:   text-decoration: underline;
                   4816:   color: $vlink;
                   4817: }
1.396     albertel 4818: 
1.417     albertel 4819: .LC_chrt_popup_exists {
                   4820:   border: 1px solid #339933;
                   4821:   margin: -1px;
                   4822: }
                   4823: .LC_chrt_popup_up {
                   4824:   border: 1px solid yellow;
                   4825:   margin: -1px;
                   4826: }
                   4827: .LC_chrt_popup {
                   4828:   border: 1px solid #8888FF;
                   4829:   background: #CCCCFF;
                   4830: }
1.421     albertel 4831: table.LC_pick_box {
                   4832:   border-collapse: separate;
                   4833:   background: white;
                   4834:   border: 1px solid black;
                   4835:   border-spacing: 1px;
                   4836: }
                   4837: table.LC_pick_box td.LC_pick_box_title {
                   4838:   background: $tabbg;
                   4839:   font-weight: bold;
                   4840:   text-align: right;
                   4841:   width: 184px;
                   4842:   padding: 8px;
                   4843: }
1.645     raeburn  4844: table.LC_pick_box td.LC_selfenroll_pick_box_title {
                   4845:   background: $tabbg;
                   4846:   font-weight: bold;
                   4847:   text-align: right;
                   4848:   width: 350px;
                   4849:   padding: 8px;
                   4850: }
                   4851: 
1.579     raeburn  4852: table.LC_pick_box td.LC_pick_box_value {
                   4853:   text-align: left;
                   4854:   padding: 8px;
                   4855: }
                   4856: table.LC_pick_box td.LC_pick_box_select {
                   4857:   text-align: left;
                   4858:   padding: 8px;
                   4859: }
1.424     albertel 4860: table.LC_pick_box td.LC_pick_box_separator {
1.421     albertel 4861:   padding: 0px;
                   4862:   height: 1px;
                   4863:   background: black;
                   4864: }
                   4865: table.LC_pick_box td.LC_pick_box_submit {
                   4866:   text-align: right;
                   4867: }
1.579     raeburn  4868: table.LC_pick_box td.LC_evenrow_value {
                   4869:   text-align: left;
                   4870:   padding: 8px;
                   4871:   background-color: $data_table_light;
                   4872: }
                   4873: table.LC_pick_box td.LC_oddrow_value {
                   4874:   text-align: left;
                   4875:   padding: 8px;
                   4876:   background-color: $data_table_light;
                   4877: }
                   4878: table.LC_helpform_receipt {
                   4879:   width: 620px;
                   4880:   border-collapse: separate;
                   4881:   background: white;
                   4882:   border: 1px solid black;
                   4883:   border-spacing: 1px;
                   4884: }
                   4885: table.LC_helpform_receipt td.LC_pick_box_title {
                   4886:   background: $tabbg;
                   4887:   font-weight: bold;
                   4888:   text-align: right;
                   4889:   width: 184px;
                   4890:   padding: 8px;
                   4891: }
                   4892: table.LC_helpform_receipt td.LC_evenrow_value {
                   4893:   text-align: left;
                   4894:   padding: 8px;
                   4895:   background-color: $data_table_light;
                   4896: }
                   4897: table.LC_helpform_receipt td.LC_oddrow_value {
                   4898:   text-align: left;
                   4899:   padding: 8px;
                   4900:   background-color: $data_table_light;
                   4901: }
                   4902: table.LC_helpform_receipt td.LC_pick_box_separator {
                   4903:   padding: 0px;
                   4904:   height: 1px;
                   4905:   background: black;
                   4906: }
                   4907: span.LC_helpform_receipt_cat {
                   4908:   font-weight: bold;
                   4909: }
1.424     albertel 4910: table.LC_group_priv_box {
                   4911:   background: white;
                   4912:   border: 1px solid black;
                   4913:   border-spacing: 1px;
                   4914: }
                   4915: table.LC_group_priv_box td.LC_pick_box_title {
                   4916:   background: $tabbg;
                   4917:   font-weight: bold;
                   4918:   text-align: right;
                   4919:   width: 184px;
                   4920: }
                   4921: table.LC_group_priv_box td.LC_groups_fixed {
                   4922:   background: $data_table_light;
                   4923:   text-align: center;
                   4924: }
                   4925: table.LC_group_priv_box td.LC_groups_optional {
                   4926:   background: $data_table_dark;
                   4927:   text-align: center;
                   4928: }
                   4929: table.LC_group_priv_box td.LC_groups_functionality {
                   4930:   background: $data_table_darker;
                   4931:   text-align: center;
                   4932:   font-weight: bold;
                   4933: }
                   4934: table.LC_group_priv td {
                   4935:   text-align: left;
                   4936:   padding: 0px;
                   4937: }
                   4938: 
1.421     albertel 4939: table.LC_notify_front_page {
                   4940:   background: white;
                   4941:   border: 1px solid black;
                   4942:   padding: 8px;
                   4943: }
                   4944: table.LC_notify_front_page td {
                   4945:   padding: 8px;
                   4946: }
1.424     albertel 4947: .LC_navbuttons {
                   4948:   margin: 2ex 0ex 2ex 0ex;
                   4949: }
1.423     albertel 4950: .LC_topic_bar {
                   4951:   font-family: $sans;
                   4952:   font-weight: bold;
                   4953:   width: 100%;
                   4954:   background: $tabbg;
                   4955:   vertical-align: middle;
                   4956:   margin: 2ex 0ex 2ex 0ex;
                   4957: }
                   4958: .LC_topic_bar span {
                   4959:   vertical-align: middle;
                   4960: }
                   4961: .LC_topic_bar img {
                   4962:   vertical-align: bottom;
                   4963: }
                   4964: table.LC_course_group_status {
                   4965:   margin: 20px;
                   4966: }
                   4967: table.LC_status_selector td {
                   4968:   vertical-align: top;
                   4969:   text-align: center;
1.424     albertel 4970:   padding: 4px;
                   4971: }
                   4972: table.LC_descriptive_input td.LC_description {
                   4973:   vertical-align: top;
                   4974:   text-align: right;
                   4975:   font-weight: bold;
1.423     albertel 4976: }
1.599     albertel 4977: div.LC_feedback_link {
1.616     albertel 4978:   clear: both;
1.599     albertel 4979:   background: white;
                   4980:   width: 100%;  
1.489     raeburn  4981: }
                   4982: span.LC_feedback_link {
1.599     albertel 4983:   background: $feedback_link_bg;
                   4984:   font-size: larger;
                   4985: }
                   4986: span.LC_message_link {
                   4987:   background: $feedback_link_bg;
                   4988:   font-size: larger;
                   4989:   position: absolute;
                   4990:   right: 1em;
1.489     raeburn  4991: }
1.421     albertel 4992: 
1.515     albertel 4993: table.LC_prior_tries {
1.524     albertel 4994:   border: 1px solid #000000;
                   4995:   border-collapse: separate;
                   4996:   border-spacing: 1px;
1.515     albertel 4997: }
1.523     albertel 4998: 
1.515     albertel 4999: table.LC_prior_tries td {
1.524     albertel 5000:   padding: 2px;
1.515     albertel 5001: }
1.523     albertel 5002: 
                   5003: .LC_answer_correct {
                   5004:   background: #AAFFAA;
                   5005:   color: black;
                   5006: }
                   5007: .LC_answer_charged_try {
                   5008:   background: #FFAAAA ! important;
                   5009:   color: black;
                   5010: }
                   5011: .LC_answer_not_charged_try, 
                   5012: .LC_answer_no_grade,
                   5013: .LC_answer_late {
                   5014:   background: #FFFFAA;
                   5015:   color: black;
                   5016: }
                   5017: .LC_answer_previous {
                   5018:   background: #AAAAFF;
                   5019:   color: black;
                   5020: }
                   5021: .LC_answer_no_message {
                   5022:   background: #FFFFFF;
                   5023:   color: black;
                   5024: }
                   5025: .LC_answer_unknown {
                   5026:   background: orange;
                   5027:   color: black;
                   5028: }
                   5029: 
                   5030: 
1.529     albertel 5031: span.LC_prior_numerical,
                   5032: span.LC_prior_string,
                   5033: span.LC_prior_custom,
                   5034: span.LC_prior_reaction,
                   5035: span.LC_prior_math {
1.523     albertel 5036:   font-family: monospace;
                   5037:   white-space: pre;
                   5038: }
                   5039: 
1.525     albertel 5040: span.LC_prior_string {
                   5041:   font-family: monospace;
                   5042:   white-space: pre;
                   5043: }
                   5044: 
1.523     albertel 5045: table.LC_prior_option {
                   5046:   width: 100%;
                   5047:   border-collapse: collapse;
                   5048: }
1.528     albertel 5049: table.LC_prior_rank, table.LC_prior_match {
                   5050:   border-collapse: collapse;
                   5051: }
                   5052: table.LC_prior_option tr td,
                   5053: table.LC_prior_rank tr td,
                   5054: table.LC_prior_match tr td {
1.524     albertel 5055:   border: 1px solid #000000;
1.515     albertel 5056: }
                   5057: 
1.519     raeburn  5058: span.LC_nobreak {
1.544     albertel 5059:   white-space: nowrap;
1.519     raeburn  5060: }
                   5061: 
1.576     raeburn  5062: span.LC_cusr_emph {
                   5063:   font-style: italic;
                   5064: }
                   5065: 
1.633     raeburn  5066: span.LC_cusr_subheading {
                   5067:   font-weight: normal;
                   5068:   font-size: 85%;
                   5069: }
                   5070: 
1.545     albertel 5071: table.LC_docs_documents {
                   5072:   background: #BBBBBB;
1.547     albertel 5073:   border-width: 0px;
1.545     albertel 5074:   border-collapse: collapse;
                   5075: }
                   5076: 
                   5077: table.LC_docs_documents td.LC_docs_document {
                   5078:   border: 2px solid black;
                   5079:   padding: 4px;
                   5080: }
                   5081: 
                   5082: .LC_docs_course_commands div {
                   5083:   float: left;
                   5084:   border: 4px solid #AAAAAA;
                   5085:   padding: 4px;
                   5086:   background: #DDDDCC;
                   5087: }
                   5088: 
                   5089: .LC_docs_entry_move {
                   5090:   border: 0px;
                   5091:   border-collapse: collapse;
1.544     albertel 5092: }
                   5093: 
1.545     albertel 5094: .LC_docs_entry_move td {
                   5095:   border: 2px solid #BBBBBB;
                   5096:   background: #DDDDDD;
                   5097: }
                   5098: 
                   5099: .LC_docs_editor td.LC_docs_entry_commands {
                   5100:   background: #DDDDDD;
                   5101:   font-size: x-small;
                   5102: }
1.544     albertel 5103: .LC_docs_copy {
1.545     albertel 5104:   color: #000099;
1.544     albertel 5105: }
                   5106: .LC_docs_cut {
1.545     albertel 5107:   color: #550044;
1.544     albertel 5108: }
                   5109: .LC_docs_rename {
1.545     albertel 5110:   color: #009900;
1.544     albertel 5111: }
                   5112: .LC_docs_remove {
1.545     albertel 5113:   color: #990000;
                   5114: }
                   5115: 
1.547     albertel 5116: .LC_docs_reinit_warn,
                   5117: .LC_docs_ext_edit {
                   5118:   font-size: x-small;
                   5119: }
                   5120: 
1.545     albertel 5121: .LC_docs_editor td.LC_docs_entry_title,
                   5122: .LC_docs_editor td.LC_docs_entry_icon {
                   5123:   background: #FFFFBB;
                   5124: }
                   5125: .LC_docs_editor td.LC_docs_entry_parameter {
                   5126:   background: #BBBBFF;
                   5127:   font-size: x-small;
                   5128:   white-space: nowrap;
                   5129: }
                   5130: 
                   5131: table.LC_docs_adddocs td,
                   5132: table.LC_docs_adddocs th {
                   5133:   border: 1px solid #BBBBBB;
                   5134:   padding: 4px;
                   5135:   background: #DDDDDD;
1.543     albertel 5136: }
                   5137: 
1.584     albertel 5138: table.LC_sty_begin {
                   5139:   background: #BBFFBB;
                   5140: }
                   5141: table.LC_sty_end {
                   5142:   background: #FFBBBB;
                   5143: }
                   5144: 
1.589     raeburn  5145: table.LC_double_column {
                   5146:   border-width: 0px;
                   5147:   border-collapse: collapse;
                   5148:   width: 100%;
                   5149:   padding: 2px;
                   5150: }
                   5151: 
                   5152: table.LC_double_column tr td.LC_left_col {
1.590     raeburn  5153:   top: 2px;
1.589     raeburn  5154:   left: 2px;
                   5155:   width: 47%;
                   5156:   vertical-align: top;
                   5157: }
                   5158: 
                   5159: table.LC_double_column tr td.LC_right_col {
                   5160:   top: 2px;
                   5161:   right: 2px; 
                   5162:   width: 47%;
                   5163:   vertical-align: top;
                   5164: }
                   5165: 
1.594     raeburn  5166: span.LC_role_level {
                   5167:   font-weight: bold;
                   5168: }
                   5169: 
1.591     raeburn  5170: div.LC_left_float {
                   5171:   float: left;
                   5172:   padding-right: 5%;
1.597     albertel 5173:   padding-bottom: 4px;
1.591     raeburn  5174: }
                   5175: 
                   5176: div.LC_clear_float_header {
1.597     albertel 5177:   padding-bottom: 2px;
1.591     raeburn  5178: }
                   5179: 
                   5180: div.LC_clear_float_footer {
1.597     albertel 5181:   padding-top: 10px;
1.591     raeburn  5182:   clear: both;
                   5183: }
                   5184: 
1.597     albertel 5185: 
1.601     albertel 5186: div.LC_grade_select_mode {
1.604     albertel 5187:   font-family: $sans;
1.601     albertel 5188: }
                   5189: div.LC_grade_select_mode div div {
                   5190:   margin: 5px;
                   5191: }
                   5192: div.LC_grade_select_mode_selector {
                   5193:   margin: 5px;
                   5194:   float: left;
                   5195: }
                   5196: div.LC_grade_select_mode_selector_header {
                   5197:   font: bold medium $sans;
                   5198: }
                   5199: div.LC_grade_select_mode_type {
                   5200:   clear: left;
                   5201: }
                   5202: 
1.597     albertel 5203: div.LC_grade_show_user {
                   5204:   margin-top: 20px;
                   5205:   border: 1px solid black;
                   5206: }
                   5207: div.LC_grade_user_name {
                   5208:   background: #DDDDEE;
                   5209:   border-bottom: 1px solid black;
                   5210:   font: bold large $sans;
                   5211: }
                   5212: div.LC_grade_show_user_odd_row div.LC_grade_user_name {
                   5213:   background: #DDEEDD;
                   5214: }
                   5215: 
                   5216: div.LC_grade_show_problem,
                   5217: div.LC_grade_submissions,
                   5218: div.LC_grade_message_center,
                   5219: div.LC_grade_info_links,
                   5220: div.LC_grade_assign {
                   5221:   margin: 5px;
                   5222:   width: 99%;
                   5223:   background: #FFFFFF;
                   5224: }
                   5225: div.LC_grade_show_problem_header,
                   5226: div.LC_grade_submissions_header,
                   5227: div.LC_grade_message_center_header,
                   5228: div.LC_grade_assign_header {
                   5229:   font: bold large $sans;
                   5230: }
                   5231: div.LC_grade_show_problem_problem,
                   5232: div.LC_grade_submissions_body,
                   5233: div.LC_grade_message_center_body,
                   5234: div.LC_grade_assign_body {
                   5235:   border: 1px solid black;
                   5236:   width: 99%;
                   5237:   background: #FFFFFF;
                   5238: }
1.598     albertel 5239: span.LC_grade_check_note {
                   5240:   font: normal medium $sans;
                   5241:   display: inline;
                   5242:   position: absolute;
                   5243:   right: 1em;
                   5244: }
1.597     albertel 5245: 
1.613     albertel 5246: table.LC_scantron_action {
                   5247:   width: 100%;
                   5248: }
                   5249: table.LC_scantron_action tr th {
                   5250:   font: normal bold $sans;
                   5251: }
1.600     albertel 5252: 
1.614     albertel 5253: div.LC_edit_problem_header, 
                   5254: div.LC_edit_problem_footer {
1.600     albertel 5255:   font: normal medium $sans;
1.602     albertel 5256:   margin: 2px;
1.600     albertel 5257: }
                   5258: div.LC_edit_problem_header,
1.602     albertel 5259: div.LC_edit_problem_header div,
1.614     albertel 5260: div.LC_edit_problem_footer,
                   5261: div.LC_edit_problem_footer div,
1.602     albertel 5262: div.LC_edit_problem_editxml_header,
                   5263: div.LC_edit_problem_editxml_header div {
1.600     albertel 5264:   margin-top: 5px;
                   5265: }
1.602     albertel 5266: div.LC_edit_problem_header_edit_row {
                   5267:   background: $tabbg;
                   5268:   padding: 3px;
                   5269:   margin-bottom: 5px;
                   5270: }
1.600     albertel 5271: div.LC_edit_problem_header_title {
1.602     albertel 5272:   font: larger bold $sans;
                   5273:   background: $tabbg;
                   5274:   padding: 3px;
                   5275: }
                   5276: table.LC_edit_problem_header_title {
                   5277:   font: larger bold $sans;
                   5278:   width: 100%;
                   5279:   border-color: $pgbg;
                   5280:   border-style: solid;
                   5281:   border-width: $border;
                   5282: 
1.600     albertel 5283:   background: $tabbg;
1.602     albertel 5284:   border-collapse: collapse;
                   5285:   padding: 0px
                   5286: }
                   5287: 
                   5288: div.LC_edit_problem_discards {
                   5289:   float: left;
                   5290:   padding-bottom: 5px;
                   5291: }
                   5292: div.LC_edit_problem_saves {
                   5293:   float: right;
                   5294:   padding-bottom: 5px;
1.600     albertel 5295: }
                   5296: hr.LC_edit_problem_divide {
1.602     albertel 5297:   clear: both;
1.600     albertel 5298:   color: $tabbg;
                   5299:   background-color: $tabbg;
                   5300:   height: 3px;
                   5301:   border: 0px;
                   5302: }
1.343     albertel 5303: END
                   5304: }
                   5305: 
1.306     albertel 5306: =pod
                   5307: 
                   5308: =item * &headtag()
                   5309: 
                   5310: Returns a uniform footer for LON-CAPA web pages.
                   5311: 
1.307     albertel 5312: Inputs: $title - optional title for the head
                   5313:         $head_extra - optional extra HTML to put inside the <head>
1.315     albertel 5314:         $args - optional arguments
1.319     albertel 5315:             force_register - if is true call registerurl so the remote is 
                   5316:                              informed
1.415     albertel 5317:             redirect       -> array ref of
                   5318:                                    1- seconds before redirect occurs
                   5319:                                    2- url to redirect to
                   5320:                                    3- whether the side effect should occur
1.315     albertel 5321:                            (side effect of setting 
                   5322:                                $env{'internal.head.redirect'} to the url 
                   5323:                                redirected too)
1.352     albertel 5324:             domain         -> force to color decorate a page for a specific
                   5325:                                domain
                   5326:             function       -> force usage of a specific rolish color scheme
                   5327:             bgcolor        -> override the default page bgcolor
1.460     albertel 5328:             no_auto_mt_title
                   5329:                            -> prevent &mt()ing the title arg
1.464     albertel 5330: 
1.306     albertel 5331: =cut
                   5332: 
                   5333: sub headtag {
1.313     albertel 5334:     my ($title,$head_extra,$args) = @_;
1.306     albertel 5335:     
1.363     albertel 5336:     my $function = $args->{'function'} || &get_users_function();
                   5337:     my $domain   = $args->{'domain'}   || &determinedomain();
                   5338:     my $bgcolor  = $args->{'bgcolor'}  || &designparm($function.'.pgbg',$domain);
1.418     albertel 5339:     my $url = join(':',$env{'user.name'},$env{'user.domain'},
1.458     albertel 5340: 		   $Apache::lonnet::perlvar{'lonVersion'},
1.531     albertel 5341: 		   #time(),
1.418     albertel 5342: 		   $env{'environment.color.timestamp'},
1.363     albertel 5343: 		   $function,$domain,$bgcolor);
                   5344: 
1.369     www      5345:     $url = '/adm/css/'.&escape($url).'.css';
1.363     albertel 5346: 
1.308     albertel 5347:     my $result =
                   5348: 	'<head>'.
1.461     albertel 5349: 	&font_settings();
1.319     albertel 5350: 
1.461     albertel 5351:     if (!$args->{'frameset'}) {
                   5352: 	$result .= &Apache::lonhtmlcommon::htmlareaheaders();
                   5353:     }
1.319     albertel 5354:     if ($args->{'force_register'}) {
                   5355: 	$result .= &Apache::lonmenu::registerurl(1);
                   5356:     }
1.436     albertel 5357:     if (!$args->{'no_nav_bar'} 
                   5358: 	&& !$args->{'only_body'}
                   5359: 	&& !$args->{'frameset'}) {
                   5360: 	$result .= &help_menu_js();
                   5361:     }
1.319     albertel 5362: 
1.314     albertel 5363:     if (ref($args->{'redirect'})) {
1.414     albertel 5364: 	my ($time,$url,$inhibit_continue) = @{$args->{'redirect'}};
1.315     albertel 5365: 	$url = &Apache::lonenc::check_encrypt($url);
1.414     albertel 5366: 	if (!$inhibit_continue) {
                   5367: 	    $env{'internal.head.redirect'} = $url;
                   5368: 	}
1.313     albertel 5369: 	$result.=<<ADDMETA
                   5370: <meta http-equiv="pragma" content="no-cache" />
1.344     albertel 5371: <meta http-equiv="Refresh" content="$time; url=$url" />
1.313     albertel 5372: ADDMETA
                   5373:     }
1.306     albertel 5374:     if (!defined($title)) {
                   5375: 	$title = 'The LearningOnline Network with CAPA';
                   5376:     }
1.460     albertel 5377:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
                   5378:     $result .= '<title> LON-CAPA '.$title.'</title>'
1.414     albertel 5379: 	.'<link rel="stylesheet" type="text/css" href="'.$url.'" />'
                   5380: 	.$head_extra;
1.306     albertel 5381:     return $result;
                   5382: }
                   5383: 
                   5384: =pod
                   5385: 
1.340     albertel 5386: =item * &font_settings()
                   5387: 
                   5388: Returns neccessary <meta> to set the proper encoding
                   5389: 
                   5390: Inputs: none
                   5391: 
                   5392: =cut
                   5393: 
                   5394: sub font_settings {
                   5395:     my $headerstring='';
1.647     www      5396:     if (!$env{'browser.mathml'} && $env{'browser.unicode'}) {
1.340     albertel 5397: 	$headerstring.=
                   5398: 	    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
                   5399:     }
                   5400:     return $headerstring;
                   5401: }
                   5402: 
1.341     albertel 5403: =pod
                   5404: 
                   5405: =item * &xml_begin()
                   5406: 
                   5407: Returns the needed doctype and <html>
                   5408: 
                   5409: Inputs: none
                   5410: 
                   5411: =cut
                   5412: 
                   5413: sub xml_begin {
                   5414:     my $output='';
                   5415: 
1.592     albertel 5416:     if ($env{'internal.start_page'}==1) {
                   5417: 	&Apache::lonhtmlcommon::init_htmlareafields();
                   5418:     }
1.342     albertel 5419: 
1.341     albertel 5420:     if ($env{'browser.mathml'}) {
                   5421: 	$output='<?xml version="1.0"?>'
                   5422:             #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
                   5423: #            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                   5424:             
                   5425: #	    .'<!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">] >'
                   5426: 	    .'<!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">'
                   5427:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                   5428: 	    .'xmlns="http://www.w3.org/1999/xhtml">';
                   5429:     } else {
                   5430: 	$output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>';
                   5431:     }
                   5432:     return $output;
                   5433: }
1.340     albertel 5434: 
                   5435: =pod
                   5436: 
1.306     albertel 5437: =item * &endheadtag()
                   5438: 
                   5439: Returns a uniform </head> for LON-CAPA web pages.
                   5440: 
                   5441: Inputs: none
                   5442: 
                   5443: =cut
                   5444: 
                   5445: sub endheadtag {
                   5446:     return '</head>';
                   5447: }
                   5448: 
                   5449: =pod
                   5450: 
                   5451: =item * &head()
                   5452: 
                   5453: Returns a uniform complete <head>..</head> section for LON-CAPA web pages.
                   5454: 
1.648     raeburn  5455: Inputs:
                   5456: 
                   5457: =over 4
                   5458: 
                   5459: $title - optional title for the page
                   5460: 
                   5461: $head_extra - optional extra HTML to put inside the <head>
                   5462: 
                   5463: =back
1.405     albertel 5464: 
1.306     albertel 5465: =cut
                   5466: 
                   5467: sub head {
1.325     albertel 5468:     my ($title,$head_extra,$args) = @_;
                   5469:     return &headtag($title,$head_extra,$args).&endheadtag();
1.306     albertel 5470: }
                   5471: 
                   5472: =pod
                   5473: 
                   5474: =item * &start_page()
                   5475: 
                   5476: Returns a complete <html> .. <body> section for LON-CAPA web pages.
                   5477: 
1.648     raeburn  5478: Inputs:
                   5479: 
                   5480: =over 4
                   5481: 
                   5482: $title - optional title for the page
                   5483: 
                   5484: $head_extra - optional extra HTML to incude inside the <head>
                   5485: 
                   5486: $args - additional optional args supported are:
                   5487: 
                   5488: =over 8
                   5489: 
                   5490:              only_body      -> is true will set &bodytag() onlybodytag
1.317     albertel 5491:                                     arg on
1.648     raeburn  5492:              no_nav_bar     -> is true will set &bodytag() notopbar arg on
                   5493:              add_entries    -> additional attributes to add to the  <body>
                   5494:              domain         -> force to color decorate a page for a 
1.317     albertel 5495:                                     specific domain
1.648     raeburn  5496:              function       -> force usage of a specific rolish color
1.317     albertel 5497:                                     scheme
1.648     raeburn  5498:              redirect       -> see &headtag()
                   5499:              bgcolor        -> override the default page bg color
                   5500:              js_ready       -> return a string ready for being used in 
1.317     albertel 5501:                                     a javascript writeln
1.648     raeburn  5502:              html_encode    -> return a string ready for being used in 
1.320     albertel 5503:                                     a html attribute
1.648     raeburn  5504:              force_register -> if is true will turn on the &bodytag()
1.317     albertel 5505:                                     $forcereg arg
1.648     raeburn  5506:              body_title     -> alternate text to use instead of $title
1.326     albertel 5507:                                     in the title box that appears, this text
                   5508:                                     is not auto translated like the $title is
1.648     raeburn  5509:              frameset       -> if true will start with a <frameset>
1.330     albertel 5510:                                     rather than <body>
1.648     raeburn  5511:              no_title       -> if true the title bar won't be shown
                   5512:              skip_phases    -> hash ref of 
1.338     albertel 5513:                                     head -> skip the <html><head> generation
                   5514:                                     body -> skip all <body> generation
1.648     raeburn  5515:              no_inline_link -> if true and in remote mode, don't show the 
1.361     albertel 5516:                                     'Switch To Inline Menu' link
1.648     raeburn  5517:              no_auto_mt_title -> prevent &mt()ing the title arg
                   5518:              inherit_jsmath -> when creating popup window in a page,
                   5519:                                     should it have jsmath forced on by the
                   5520:                                     current page
1.361     albertel 5521: 
1.648     raeburn  5522: =back
1.460     albertel 5523: 
1.648     raeburn  5524: =back
1.562     albertel 5525: 
1.306     albertel 5526: =cut
                   5527: 
                   5528: sub start_page {
1.309     albertel 5529:     my ($title,$head_extra,$args) = @_;
1.318     albertel 5530:     #&Apache::lonnet::logthis("start_page ".join(':',caller(0)));
1.313     albertel 5531:     my %head_args;
1.352     albertel 5532:     foreach my $arg ('redirect','force_register','domain','function',
1.460     albertel 5533: 		     'bgcolor','frameset','no_nav_bar','only_body',
                   5534: 		     'no_auto_mt_title') {
1.319     albertel 5535: 	if (defined($args->{$arg})) {
1.324     raeburn  5536: 	    $head_args{$arg} = $args->{$arg};
1.319     albertel 5537: 	}
1.313     albertel 5538:     }
1.319     albertel 5539: 
1.315     albertel 5540:     $env{'internal.start_page'}++;
1.338     albertel 5541:     my $result;
                   5542:     if (! exists($args->{'skip_phases'}{'head'}) ) {
                   5543: 	$result.=
1.341     albertel 5544: 	    &xml_begin().
1.338     albertel 5545: 	    &headtag($title,$head_extra,\%head_args).&endheadtag();
                   5546:     }
                   5547:     
                   5548:     if (! exists($args->{'skip_phases'}{'body'}) ) {
                   5549: 	if ($args->{'frameset'}) {
                   5550: 	    my $attr_string = &make_attr_string($args->{'force_register'},
                   5551: 						$args->{'add_entries'});
                   5552: 	    $result .= "\n<frameset $attr_string>\n";
                   5553: 	} else {
                   5554: 	    $result .=
                   5555: 		&bodytag($title, 
                   5556: 			 $args->{'function'},       $args->{'add_entries'},
                   5557: 			 $args->{'only_body'},      $args->{'domain'},
                   5558: 			 $args->{'force_register'}, $args->{'body_title'},
                   5559: 			 $args->{'no_nav_bar'},     $args->{'bgcolor'},
1.460     albertel 5560: 			 $args->{'no_title'},       $args->{'no_inline_link'},
                   5561: 			 $args);
1.338     albertel 5562: 	}
1.330     albertel 5563:     }
1.338     albertel 5564: 
1.315     albertel 5565:     if ($args->{'js_ready'}) {
1.317     albertel 5566: 	$result = &js_ready($result);
1.315     albertel 5567:     }
1.320     albertel 5568:     if ($args->{'html_encode'}) {
                   5569: 	$result = &html_encode($result);
                   5570:     }
1.315     albertel 5571:     return $result;
1.306     albertel 5572: }
                   5573: 
1.330     albertel 5574: 
1.306     albertel 5575: =pod
                   5576: 
                   5577: =item * &head()
                   5578: 
                   5579: Returns a complete </body></html> section for LON-CAPA web pages.
                   5580: 
1.315     albertel 5581: Inputs:         $args - additional optional args supported are:
                   5582:                  js_ready     -> return a string ready for being used in 
                   5583:                                  a javascript writeln
1.320     albertel 5584:                  html_encode  -> return a string ready for being used in 
                   5585:                                  a html attribute
1.330     albertel 5586:                  frameset     -> if true will start with a <frameset>
                   5587:                                  rather than <body>
1.493     albertel 5588:                  dicsussion   -> if true will get discussion from
                   5589:                                   lonxml::xmlend
                   5590:                                  (you can pass the target and parser arguments
                   5591:                                   through optional 'target' and 'parser' args
                   5592:                                   to this routine)
1.306     albertel 5593: 
                   5594: =cut
                   5595: 
                   5596: sub end_page {
1.315     albertel 5597:     my ($args) = @_;
                   5598:     $env{'internal.end_page'}++;
1.330     albertel 5599:     my $result;
1.335     albertel 5600:     if ($args->{'discussion'}) {
                   5601: 	my ($target,$parser);
                   5602: 	if (ref($args->{'discussion'})) {
                   5603: 	    ($target,$parser) =($args->{'discussion'}{'target'},
                   5604: 				$args->{'discussion'}{'parser'});
                   5605: 	}
                   5606: 	$result .= &Apache::lonxml::xmlend($target,$parser);
                   5607:     }
                   5608: 
1.330     albertel 5609:     if ($args->{'frameset'}) {
                   5610: 	$result .= '</frameset>';
                   5611:     } else {
1.635     raeburn  5612: 	$result .= &endbodytag($args);
1.330     albertel 5613:     }
                   5614:     $result .= "\n</html>";
                   5615: 
1.315     albertel 5616:     if ($args->{'js_ready'}) {
1.317     albertel 5617: 	$result = &js_ready($result);
1.315     albertel 5618:     }
1.335     albertel 5619: 
1.320     albertel 5620:     if ($args->{'html_encode'}) {
                   5621: 	$result = &html_encode($result);
                   5622:     }
1.335     albertel 5623: 
1.315     albertel 5624:     return $result;
                   5625: }
                   5626: 
1.320     albertel 5627: sub html_encode {
                   5628:     my ($result) = @_;
                   5629: 
1.322     albertel 5630:     $result = &HTML::Entities::encode($result,'<>&"');
1.320     albertel 5631:     
                   5632:     return $result;
                   5633: }
1.317     albertel 5634: sub js_ready {
                   5635:     my ($result) = @_;
                   5636: 
1.323     albertel 5637:     $result =~ s/[\n\r]/ /xmsg;
                   5638:     $result =~ s/\\/\\\\/xmsg;
                   5639:     $result =~ s/'/\\'/xmsg;
1.372     albertel 5640:     $result =~ s{</}{<\\/}xmsg;
1.317     albertel 5641:     
                   5642:     return $result;
                   5643: }
                   5644: 
1.315     albertel 5645: sub validate_page {
                   5646:     if (  exists($env{'internal.start_page'})
1.316     albertel 5647: 	  &&     $env{'internal.start_page'} > 1) {
                   5648: 	&Apache::lonnet::logthis('start_page called multiple times '.
1.318     albertel 5649: 				 $env{'internal.start_page'}.' '.
1.316     albertel 5650: 				 $ENV{'request.filename'});
1.315     albertel 5651:     }
                   5652:     if (  exists($env{'internal.end_page'})
1.316     albertel 5653: 	  &&     $env{'internal.end_page'} > 1) {
                   5654: 	&Apache::lonnet::logthis('end_page called multiple times '.
1.318     albertel 5655: 				 $env{'internal.end_page'}.' '.
1.316     albertel 5656: 				 $env{'request.filename'});
1.315     albertel 5657:     }
                   5658:     if (     exists($env{'internal.start_page'})
                   5659: 	&& ! exists($env{'internal.end_page'})) {
1.316     albertel 5660: 	&Apache::lonnet::logthis('start_page called without end_page '.
                   5661: 				 $env{'request.filename'});
1.315     albertel 5662:     }
                   5663:     if (   ! exists($env{'internal.start_page'})
                   5664: 	&&   exists($env{'internal.end_page'})) {
1.316     albertel 5665: 	&Apache::lonnet::logthis('end_page called without start_page'.
                   5666: 				 $env{'request.filename'});
1.315     albertel 5667:     }
1.306     albertel 5668: }
1.315     albertel 5669: 
1.318     albertel 5670: sub simple_error_page {
                   5671:     my ($r,$title,$msg) = @_;
                   5672:     my $page =
                   5673: 	&Apache::loncommon::start_page($title).
                   5674: 	&mt($msg).
                   5675: 	&Apache::loncommon::end_page();
                   5676:     if (ref($r)) {
                   5677: 	$r->print($page);
1.327     albertel 5678: 	return;
1.318     albertel 5679:     }
                   5680:     return $page;
                   5681: }
1.347     albertel 5682: 
                   5683: {
1.610     albertel 5684:     my @row_count;
1.347     albertel 5685:     sub start_data_table {
1.422     albertel 5686: 	my ($add_class) = @_;
                   5687: 	my $css_class = (join(' ','LC_data_table',$add_class));
1.610     albertel 5688: 	unshift(@row_count,0);
1.422     albertel 5689: 	return '<table class="'.$css_class.'">'."\n";
1.347     albertel 5690:     }
                   5691: 
                   5692:     sub end_data_table {
1.610     albertel 5693: 	shift(@row_count);
1.389     albertel 5694: 	return '</table>'."\n";;
1.347     albertel 5695:     }
                   5696: 
                   5697:     sub start_data_table_row {
1.422     albertel 5698: 	my ($add_class) = @_;
1.610     albertel 5699: 	$row_count[0]++;
                   5700: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.428     albertel 5701: 	$css_class = (join(' ',$css_class,$add_class));
1.422     albertel 5702: 	return  '<tr class="'.$css_class.'">'."\n";;
1.347     albertel 5703:     }
1.471     banghart 5704:     
                   5705:     sub continue_data_table_row {
                   5706: 	my ($add_class) = @_;
1.610     albertel 5707: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.471     banghart 5708: 	$css_class = (join(' ',$css_class,$add_class));
                   5709: 	return  '<tr class="'.$css_class.'">'."\n";;
                   5710:     }
1.347     albertel 5711: 
                   5712:     sub end_data_table_row {
1.389     albertel 5713: 	return '</tr>'."\n";;
1.347     albertel 5714:     }
1.367     www      5715: 
1.421     albertel 5716:     sub start_data_table_empty_row {
1.610     albertel 5717: 	$row_count[0]++;
1.421     albertel 5718: 	return  '<tr class="LC_empty_row" >'."\n";;
                   5719:     }
                   5720: 
                   5721:     sub end_data_table_empty_row {
                   5722: 	return '</tr>'."\n";;
                   5723:     }
                   5724: 
1.367     www      5725:     sub start_data_table_header_row {
1.389     albertel 5726: 	return  '<tr class="LC_header_row">'."\n";;
1.367     www      5727:     }
                   5728: 
                   5729:     sub end_data_table_header_row {
1.389     albertel 5730: 	return '</tr>'."\n";;
1.367     www      5731:     }
1.347     albertel 5732: }
                   5733: 
1.548     albertel 5734: =pod
                   5735: 
                   5736: =item * &inhibit_menu_check($arg)
                   5737: 
                   5738: Checks for a inhibitmenu state and generates output to preserve it
                   5739: 
                   5740: Inputs:         $arg - can be any of
                   5741:                      - undef - in which case the return value is a string 
                   5742:                                to add  into arguments list of a uri
                   5743:                      - 'input' - in which case the return value is a HTML
                   5744:                                  <form> <input> field of type hidden to
                   5745:                                  preserve the value
                   5746:                      - a url - in which case the return value is the url with
                   5747:                                the neccesary cgi args added to preserve the
                   5748:                                inhibitmenu state
                   5749:                      - a ref to a url - no return value, but the string is
                   5750:                                         updated to include the neccessary cgi
                   5751:                                         args to preserve the inhibitmenu state
                   5752: 
                   5753: =cut
                   5754: 
                   5755: sub inhibit_menu_check {
                   5756:     my ($arg) = @_;
                   5757:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
                   5758:     if ($arg eq 'input') {
                   5759: 	if ($env{'form.inhibitmenu'}) {
                   5760: 	    return '<input type="hidden" name="inhibitmenu" value="'.$env{'form.inhibitmenu'}.'" />';
                   5761: 	} else {
                   5762: 	    return
                   5763: 	}
                   5764:     }
                   5765:     if ($env{'form.inhibitmenu'}) {
                   5766: 	if (ref($arg)) {
                   5767: 	    $$arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   5768: 	} elsif ($arg eq '') {
                   5769: 	    $arg .= 'inhibitmenu='.$env{'form.inhibitmenu'};
                   5770: 	} else {
                   5771: 	    $arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   5772: 	}
                   5773:     }
                   5774:     if (!ref($arg)) {
                   5775: 	return $arg;
                   5776:     }
                   5777: }
                   5778: 
1.251     albertel 5779: ###############################################
1.182     matthew  5780: 
                   5781: =pod
                   5782: 
1.549     albertel 5783: =back
                   5784: 
                   5785: =head1 User Information Routines
                   5786: 
                   5787: =over 4
                   5788: 
1.405     albertel 5789: =item * &get_users_function()
1.182     matthew  5790: 
                   5791: Used by &bodytag to determine the current users primary role.
                   5792: Returns either 'student','coordinator','admin', or 'author'.
                   5793: 
                   5794: =cut
                   5795: 
                   5796: ###############################################
                   5797: sub get_users_function {
                   5798:     my $function = 'student';
1.258     albertel 5799:     if ($env{'request.role'}=~/^(cc|in|ta|ep)/) {
1.182     matthew  5800:         $function='coordinator';
                   5801:     }
1.258     albertel 5802:     if ($env{'request.role'}=~/^(su|dc|ad|li)/) {
1.182     matthew  5803:         $function='admin';
                   5804:     }
1.258     albertel 5805:     if (($env{'request.role'}=~/^(au|ca)/) ||
1.182     matthew  5806:         ($ENV{'REQUEST_URI'}=~/^(\/priv|\~)/)) {
                   5807:         $function='author';
                   5808:     }
                   5809:     return $function;
1.54      www      5810: }
1.99      www      5811: 
                   5812: ###############################################
                   5813: 
1.233     raeburn  5814: =pod
                   5815: 
1.542     raeburn  5816: =item * &check_user_status()
1.274     raeburn  5817: 
                   5818: Determines current status of supplied role for a
                   5819: specific user. Roles can be active, previous or future.
                   5820: 
                   5821: Inputs: 
                   5822: user's domain, user's username, course's domain,
1.375     raeburn  5823: course's number, optional section ID.
1.274     raeburn  5824: 
                   5825: Outputs:
                   5826: role status: active, previous or future. 
                   5827: 
                   5828: =cut
                   5829: 
                   5830: sub check_user_status {
1.412     raeburn  5831:     my ($udom,$uname,$cdom,$crs,$role,$sec) = @_;
1.274     raeburn  5832:     my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
                   5833:     my @uroles = keys %userinfo;
                   5834:     my $srchstr;
                   5835:     my $active_chk = 'none';
1.412     raeburn  5836:     my $now = time;
1.274     raeburn  5837:     if (@uroles > 0) {
1.412     raeburn  5838:         if (($role eq 'cc') || ($sec eq '') || (!defined($sec))) {
1.274     raeburn  5839:             $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
                   5840:         } else {
1.412     raeburn  5841:             $srchstr = '/'.$cdom.'/'.$crs.'/'.$sec.'_'.$role;
                   5842:         }
                   5843:         if (grep/^\Q$srchstr\E$/,@uroles) {
1.274     raeburn  5844:             my $role_end = 0;
                   5845:             my $role_start = 0;
                   5846:             $active_chk = 'active';
1.412     raeburn  5847:             if ($userinfo{$srchstr} =~ m/^\Q$role\E_(\d+)/) {
                   5848:                 $role_end = $1;
                   5849:                 if ($userinfo{$srchstr} =~ m/^\Q$role\E_\Q$role_end\E_(\d+)$/) {
                   5850:                     $role_start = $1;
1.274     raeburn  5851:                 }
                   5852:             }
                   5853:             if ($role_start > 0) {
1.412     raeburn  5854:                 if ($now < $role_start) {
1.274     raeburn  5855:                     $active_chk = 'future';
                   5856:                 }
                   5857:             }
                   5858:             if ($role_end > 0) {
1.412     raeburn  5859:                 if ($now > $role_end) {
1.274     raeburn  5860:                     $active_chk = 'previous';
                   5861:                 }
                   5862:             }
                   5863:         }
                   5864:     }
                   5865:     return $active_chk;
                   5866: }
                   5867: 
                   5868: ###############################################
                   5869: 
                   5870: =pod
                   5871: 
1.405     albertel 5872: =item * &get_sections()
1.233     raeburn  5873: 
                   5874: Determines all the sections for a course including
                   5875: sections with students and sections containing other roles.
1.419     raeburn  5876: Incoming parameters: 
                   5877: 
                   5878: 1. domain
                   5879: 2. course number 
                   5880: 3. reference to array containing roles for which sections should 
                   5881: be gathered (optional).
                   5882: 4. reference to array containing status types for which sections 
                   5883: should be gathered (optional).
                   5884: 
                   5885: If the third argument is undefined, sections are gathered for any role. 
                   5886: If the fourth argument is undefined, sections are gathered for any status.
                   5887: Permissible values are 'active' or 'future' or 'previous'.
1.233     raeburn  5888:  
1.374     raeburn  5889: Returns section hash (keys are section IDs, values are
                   5890: number of users in each section), subject to the
1.419     raeburn  5891: optional roles filter, optional status filter 
1.233     raeburn  5892: 
                   5893: =cut
                   5894: 
                   5895: ###############################################
                   5896: sub get_sections {
1.419     raeburn  5897:     my ($cdom,$cnum,$possible_roles,$possible_status) = @_;
1.366     albertel 5898:     if (!defined($cdom) || !defined($cnum)) {
                   5899:         my $cid =  $env{'request.course.id'};
                   5900: 
                   5901: 	return if (!defined($cid));
                   5902: 
                   5903:         $cdom = $env{'course.'.$cid.'.domain'};
                   5904:         $cnum = $env{'course.'.$cid.'.num'};
                   5905:     }
                   5906: 
                   5907:     my %sectioncount;
1.419     raeburn  5908:     my $now = time;
1.240     albertel 5909: 
1.366     albertel 5910:     if (!defined($possible_roles) || (grep(/^st$/,@$possible_roles))) {
1.276     albertel 5911: 	my ($classlist) = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1.240     albertel 5912: 	my $sec_index = &Apache::loncoursedata::CL_SECTION();
                   5913: 	my $status_index = &Apache::loncoursedata::CL_STATUS();
1.419     raeburn  5914:         my $start_index = &Apache::loncoursedata::CL_START();
                   5915:         my $end_index = &Apache::loncoursedata::CL_END();
                   5916:         my $status;
1.366     albertel 5917: 	while (my ($student,$data) = each(%$classlist)) {
1.419     raeburn  5918: 	    my ($section,$stu_status,$start,$end) = ($data->[$sec_index],
                   5919: 				                     $data->[$status_index],
                   5920:                                                      $data->[$start_index],
                   5921:                                                      $data->[$end_index]);
                   5922:             if ($stu_status eq 'Active') {
                   5923:                 $status = 'active';
                   5924:             } elsif ($end < $now) {
                   5925:                 $status = 'previous';
                   5926:             } elsif ($start > $now) {
                   5927:                 $status = 'future';
                   5928:             } 
                   5929: 	    if ($section ne '-1' && $section !~ /^\s*$/) {
                   5930:                 if ((!defined($possible_status)) || (($status ne '') && 
                   5931:                     (grep/^\Q$status\E$/,@{$possible_status}))) { 
                   5932: 		    $sectioncount{$section}++;
                   5933:                 }
1.240     albertel 5934: 	    }
                   5935: 	}
                   5936:     }
                   5937:     my %courseroles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   5938:     foreach my $user (sort(keys(%courseroles))) {
                   5939: 	if ($user !~ /^(\w{2})/) { next; }
                   5940: 	my ($role) = ($user =~ /^(\w{2})/);
                   5941: 	if ($possible_roles && !(grep(/^$role$/,@$possible_roles))) { next; }
1.419     raeburn  5942: 	my ($section,$status);
1.240     albertel 5943: 	if ($role eq 'cr' &&
                   5944: 	    $user =~ m-^$role/[^/]*/[^/]*/[^/]*:[^:]*:[^:]*:(\w+)-) {
                   5945: 	    $section=$1;
                   5946: 	}
                   5947: 	if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
                   5948: 	if (!defined($section) || $section eq '-1') { next; }
1.419     raeburn  5949:         my ($end,$start) = ($courseroles{$user} =~ /^([^:]*):([^:]*)$/);
                   5950:         if ($end == -1 && $start == -1) {
                   5951:             next; #deleted role
                   5952:         }
                   5953:         if (!defined($possible_status)) { 
                   5954:             $sectioncount{$section}++;
                   5955:         } else {
                   5956:             if ((!$end || $end >= $now) && (!$start || $start <= $now)) {
                   5957:                 $status = 'active';
                   5958:             } elsif ($end < $now) {
                   5959:                 $status = 'future';
                   5960:             } elsif ($start > $now) {
                   5961:                 $status = 'previous';
                   5962:             }
                   5963:             if (($status ne '') && (grep/^\Q$status\E$/,@{$possible_status})) {
                   5964:                 $sectioncount{$section}++;
                   5965:             }
                   5966:         }
1.233     raeburn  5967:     }
1.366     albertel 5968:     return %sectioncount;
1.233     raeburn  5969: }
                   5970: 
1.274     raeburn  5971: ###############################################
1.294     raeburn  5972: 
                   5973: =pod
1.405     albertel 5974: 
                   5975: =item * &get_course_users()
                   5976: 
1.275     raeburn  5977: Retrieves usernames:domains for users in the specified course
                   5978: with specific role(s), and access status. 
                   5979: 
                   5980: Incoming parameters:
1.277     albertel 5981: 1. course domain
                   5982: 2. course number
                   5983: 3. access status: users must have - either active, 
1.275     raeburn  5984: previous, future, or all.
1.277     albertel 5985: 4. reference to array of permissible roles
1.288     raeburn  5986: 5. reference to array of section restrictions (optional)
                   5987: 6. reference to results object (hash of hashes).
                   5988: 7. reference to optional userdata hash
1.609     raeburn  5989: 8. reference to optional statushash
1.630     raeburn  5990: 9. flag if privileged users (except those set to unhide in
                   5991:    course settings) should be excluded    
1.609     raeburn  5992: Keys of top level results hash are roles.
1.275     raeburn  5993: Keys of inner hashes are username:domain, with 
                   5994: values set to access type.
1.288     raeburn  5995: Optional userdata hash returns an array with arguments in the 
                   5996: same order as loncoursedata::get_classlist() for student data.
                   5997: 
1.609     raeburn  5998: Optional statushash returns
                   5999: 
1.288     raeburn  6000: Entries for end, start, section and status are blank because
                   6001: of the possibility of multiple values for non-student roles.
                   6002: 
1.275     raeburn  6003: =cut
1.405     albertel 6004: 
1.275     raeburn  6005: ###############################################
1.405     albertel 6006: 
1.275     raeburn  6007: sub get_course_users {
1.630     raeburn  6008:     my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata,$statushash,$hidepriv) = @_;
1.288     raeburn  6009:     my %idx = ();
1.419     raeburn  6010:     my %seclists;
1.288     raeburn  6011: 
                   6012:     $idx{udom} = &Apache::loncoursedata::CL_SDOM();
                   6013:     $idx{uname} =  &Apache::loncoursedata::CL_SNAME();
                   6014:     $idx{end} = &Apache::loncoursedata::CL_END();
                   6015:     $idx{start} = &Apache::loncoursedata::CL_START();
                   6016:     $idx{id} = &Apache::loncoursedata::CL_ID();
                   6017:     $idx{section} = &Apache::loncoursedata::CL_SECTION();
                   6018:     $idx{fullname} = &Apache::loncoursedata::CL_FULLNAME();
                   6019:     $idx{status} = &Apache::loncoursedata::CL_STATUS();
                   6020: 
1.290     albertel 6021:     if (grep(/^st$/,@{$roles})) {
1.276     albertel 6022:         my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist($cdom,$cnum);
1.278     raeburn  6023:         my $now = time;
1.277     albertel 6024:         foreach my $student (keys(%{$classlist})) {
1.288     raeburn  6025:             my $match = 0;
1.412     raeburn  6026:             my $secmatch = 0;
1.419     raeburn  6027:             my $section = $$classlist{$student}[$idx{section}];
1.609     raeburn  6028:             my $status = $$classlist{$student}[$idx{status}];
1.419     raeburn  6029:             if ($section eq '') {
                   6030:                 $section = 'none';
                   6031:             }
1.291     albertel 6032:             if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 6033:                 if (grep(/^all$/,@{$sections})) {
1.412     raeburn  6034:                     $secmatch = 1;
                   6035:                 } elsif ($$classlist{$student}[$idx{section}] eq '') {
1.420     albertel 6036:                     if (grep(/^none$/,@{$sections})) {
1.412     raeburn  6037:                         $secmatch = 1;
                   6038:                     }
                   6039:                 } else {  
1.419     raeburn  6040: 		    if (grep(/^\Q$section\E$/,@{$sections})) {
1.412     raeburn  6041: 		        $secmatch = 1;
                   6042:                     }
1.290     albertel 6043: 		}
1.412     raeburn  6044:                 if (!$secmatch) {
                   6045:                     next;
                   6046:                 }
1.419     raeburn  6047:             }
1.275     raeburn  6048:             if (defined($$types{'active'})) {
1.288     raeburn  6049:                 if ($$classlist{$student}[$idx{status}] eq 'Active') {
1.275     raeburn  6050:                     push(@{$$users{st}{$student}},'active');
1.288     raeburn  6051:                     $match = 1;
1.275     raeburn  6052:                 }
                   6053:             }
                   6054:             if (defined($$types{'previous'})) {
1.609     raeburn  6055:                 if ($$classlist{$student}[$idx{status}] eq 'Expired') {
1.275     raeburn  6056:                     push(@{$$users{st}{$student}},'previous');
1.288     raeburn  6057:                     $match = 1;
1.275     raeburn  6058:                 }
                   6059:             }
                   6060:             if (defined($$types{'future'})) {
1.609     raeburn  6061:                 if ($$classlist{$student}[$idx{status}] eq 'Future') {
1.275     raeburn  6062:                     push(@{$$users{st}{$student}},'future');
1.288     raeburn  6063:                     $match = 1;
1.275     raeburn  6064:                 }
                   6065:             }
1.609     raeburn  6066:             if ($match) {
                   6067:                 push(@{$seclists{$student}},$section);
                   6068:                 if (ref($userdata) eq 'HASH') {
                   6069:                     $$userdata{$student} = $$classlist{$student};
                   6070:                 }
                   6071:                 if (ref($statushash) eq 'HASH') {
                   6072:                     $statushash->{$student}{'st'}{$section} = $status;
                   6073:                 }
1.288     raeburn  6074:             }
1.275     raeburn  6075:         }
                   6076:     }
1.412     raeburn  6077:     if ((@{$roles} > 1) || ((@{$roles} == 1) && ($$roles[0] ne "st"))) {
1.439     raeburn  6078:         my %coursepersonnel = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   6079:         my $now = time;
1.609     raeburn  6080:         my %displaystatus = ( previous => 'Expired',
                   6081:                               active   => 'Active',
                   6082:                               future   => 'Future',
                   6083:                             );
1.630     raeburn  6084:         my %nothide;
                   6085:         if ($hidepriv) {
                   6086:             my %coursehash=&Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                   6087:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   6088:                 if ($user !~ /:/) {
                   6089:                     $nothide{join(':',split(/[\@]/,$user))}=1;
                   6090:                 } else {
                   6091:                     $nothide{$user} = 1;
                   6092:                 }
                   6093:             }
                   6094:         }
1.439     raeburn  6095:         foreach my $person (sort(keys(%coursepersonnel))) {
1.288     raeburn  6096:             my $match = 0;
1.412     raeburn  6097:             my $secmatch = 0;
1.439     raeburn  6098:             my $status;
1.412     raeburn  6099:             my ($role,$user,$usec) = ($person =~ /^([^:]*):([^:]+:[^:]+):([^:]*)/);
1.275     raeburn  6100:             $user =~ s/:$//;
1.439     raeburn  6101:             my ($end,$start) = split(/:/,$coursepersonnel{$person});
                   6102:             if ($end == -1 || $start == -1) {
                   6103:                 next;
                   6104:             }
                   6105:             if (($role) && ((grep(/^\Q$role\E$/,@{$roles})) ||
                   6106:                 (grep(/^cr$/,@{$roles}) && $role =~ /^cr\//))) {
1.412     raeburn  6107:                 my ($uname,$udom) = split(/:/,$user);
                   6108:                 if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 6109:                     if (grep(/^all$/,@{$sections})) {
1.412     raeburn  6110:                         $secmatch = 1;
                   6111:                     } elsif ($usec eq '') {
1.420     albertel 6112:                         if (grep(/^none$/,@{$sections})) {
1.412     raeburn  6113:                             $secmatch = 1;
                   6114:                         }
                   6115:                     } else {
                   6116:                         if (grep(/^\Q$usec\E$/,@{$sections})) {
                   6117:                             $secmatch = 1;
                   6118:                         }
                   6119:                     }
                   6120:                     if (!$secmatch) {
                   6121:                         next;
                   6122:                     }
1.288     raeburn  6123:                 }
1.419     raeburn  6124:                 if ($usec eq '') {
                   6125:                     $usec = 'none';
                   6126:                 }
1.275     raeburn  6127:                 if ($uname ne '' && $udom ne '') {
1.630     raeburn  6128:                     if ($hidepriv) {
                   6129:                         if ((&Apache::lonnet::privileged($uname,$udom)) &&
                   6130:                             (!$nothide{$uname.':'.$udom})) {
                   6131:                             next;
                   6132:                         }
                   6133:                     }
1.503     raeburn  6134:                     if ($end > 0 && $end < $now) {
1.439     raeburn  6135:                         $status = 'previous';
                   6136:                     } elsif ($start > $now) {
                   6137:                         $status = 'future';
                   6138:                     } else {
                   6139:                         $status = 'active';
                   6140:                     }
1.277     albertel 6141:                     foreach my $type (keys(%{$types})) { 
1.275     raeburn  6142:                         if ($status eq $type) {
1.420     albertel 6143:                             if (!grep(/^\Q$type\E$/,@{$$users{$role}{$user}})) {
1.419     raeburn  6144:                                 push(@{$$users{$role}{$user}},$type);
                   6145:                             }
1.288     raeburn  6146:                             $match = 1;
                   6147:                         }
                   6148:                     }
1.419     raeburn  6149:                     if (($match) && (ref($userdata) eq 'HASH')) {
                   6150:                         if (!exists($$userdata{$uname.':'.$udom})) {
                   6151: 			    &get_user_info($udom,$uname,\%idx,$userdata);
                   6152:                         }
1.420     albertel 6153:                         if (!grep(/^\Q$usec\E$/,@{$seclists{$uname.':'.$udom}})) {
1.419     raeburn  6154:                             push(@{$seclists{$uname.':'.$udom}},$usec);
                   6155:                         }
1.609     raeburn  6156:                         if (ref($statushash) eq 'HASH') {
                   6157:                             $statushash->{$uname.':'.$udom}{$role}{$usec} = $displaystatus{$status};
                   6158:                         }
1.275     raeburn  6159:                     }
                   6160:                 }
                   6161:             }
                   6162:         }
1.290     albertel 6163:         if (grep(/^ow$/,@{$roles})) {
1.279     raeburn  6164:             if ((defined($cdom)) && (defined($cnum))) {
                   6165:                 my %csettings = &Apache::lonnet::get('environment',['internal.courseowner'],$cdom,$cnum);
                   6166:                 if ( defined($csettings{'internal.courseowner'}) ) {
                   6167:                     my $owner = $csettings{'internal.courseowner'};
1.609     raeburn  6168:                     next if ($owner eq '');
                   6169:                     my ($ownername,$ownerdom);
                   6170:                     if ($owner =~ /^([^:]+):([^:]+)$/) {
                   6171:                         $ownername = $1;
                   6172:                         $ownerdom = $2;
                   6173:                     } else {
                   6174:                         $ownername = $owner;
                   6175:                         $ownerdom = $cdom;
                   6176:                         $owner = $ownername.':'.$ownerdom;
1.439     raeburn  6177:                     }
                   6178:                     @{$$users{'ow'}{$owner}} = 'any';
1.290     albertel 6179:                     if (defined($userdata) && 
1.609     raeburn  6180: 			!exists($$userdata{$owner})) {
                   6181: 			&get_user_info($ownerdom,$ownername,\%idx,$userdata);
                   6182:                         if (!grep(/^none$/,@{$seclists{$owner}})) {
                   6183:                             push(@{$seclists{$owner}},'none');
                   6184:                         }
                   6185:                         if (ref($statushash) eq 'HASH') {
                   6186:                             $statushash->{$owner}{'ow'}{'none'} = 'Any';
1.419     raeburn  6187:                         }
1.290     albertel 6188: 		    }
1.279     raeburn  6189:                 }
                   6190:             }
                   6191:         }
1.419     raeburn  6192:         foreach my $user (keys(%seclists)) {
                   6193:             @{$seclists{$user}} = (sort {$a <=> $b} @{$seclists{$user}});
                   6194:             $$userdata{$user}[$idx{section}] = join(',',@{$seclists{$user}});
                   6195:         }
1.275     raeburn  6196:     }
                   6197:     return;
                   6198: }
                   6199: 
1.288     raeburn  6200: sub get_user_info {
                   6201:     my ($udom,$uname,$idx,$userdata) = @_;
1.289     albertel 6202:     $$userdata{$uname.':'.$udom}[$$idx{fullname}] = 
                   6203: 	&plainname($uname,$udom,'lastname');
1.291     albertel 6204:     $$userdata{$uname.':'.$udom}[$$idx{uname}] = $uname;
1.297     raeburn  6205:     $$userdata{$uname.':'.$udom}[$$idx{udom}] = $udom;
1.609     raeburn  6206:     my %idhash =  &Apache::lonnet::idrget($udom,($uname));
                   6207:     $$userdata{$uname.':'.$udom}[$$idx{id}] = $idhash{$uname}; 
1.288     raeburn  6208:     return;
                   6209: }
1.275     raeburn  6210: 
1.472     raeburn  6211: ###############################################
                   6212: 
                   6213: =pod
                   6214: 
                   6215: =item * &get_user_quota()
                   6216: 
                   6217: Retrieves quota assigned for storage of portfolio files for a user  
                   6218: 
                   6219: Incoming parameters:
                   6220: 1. user's username
                   6221: 2. user's domain
                   6222: 
                   6223: Returns:
1.536     raeburn  6224: 1. Disk quota (in Mb) assigned to student.
                   6225: 2. (Optional) Type of setting: custom or default
                   6226:    (individually assigned or default for user's 
                   6227:    institutional status).
                   6228: 3. (Optional) - User's institutional status (e.g., faculty, staff
                   6229:    or student - types as defined in localenroll::inst_usertypes 
                   6230:    for user's domain, which determines default quota for user.
                   6231: 4. (Optional) - Default quota which would apply to the user.
1.472     raeburn  6232: 
                   6233: If a value has been stored in the user's environment, 
1.536     raeburn  6234: it will return that, otherwise it returns the maximal default
                   6235: defined for the user's instituional status(es) in the domain.
1.472     raeburn  6236: 
                   6237: =cut
                   6238: 
                   6239: ###############################################
                   6240: 
                   6241: 
                   6242: sub get_user_quota {
                   6243:     my ($uname,$udom) = @_;
1.536     raeburn  6244:     my ($quota,$quotatype,$settingstatus,$defquota);
1.472     raeburn  6245:     if (!defined($udom)) {
                   6246:         $udom = $env{'user.domain'};
                   6247:     }
                   6248:     if (!defined($uname)) {
                   6249:         $uname = $env{'user.name'};
                   6250:     }
                   6251:     if (($udom eq '' || $uname eq '') ||
                   6252:         ($udom eq 'public') && ($uname eq 'public')) {
                   6253:         $quota = 0;
1.536     raeburn  6254:         $quotatype = 'default';
                   6255:         $defquota = 0; 
1.472     raeburn  6256:     } else {
1.536     raeburn  6257:         my $inststatus;
1.472     raeburn  6258:         if ($udom eq $env{'user.domain'} && $uname eq $env{'user.name'}) {
                   6259:             $quota = $env{'environment.portfolioquota'};
1.536     raeburn  6260:             $inststatus = $env{'environment.inststatus'};
1.472     raeburn  6261:         } else {
1.536     raeburn  6262:             my %userenv = 
                   6263:                 &Apache::lonnet::get('environment',['portfolioquota',
                   6264:                                      'inststatus'],$udom,$uname);
1.472     raeburn  6265:             my ($tmp) = keys(%userenv);
                   6266:             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   6267:                 $quota = $userenv{'portfolioquota'};
1.536     raeburn  6268:                 $inststatus = $userenv{'inststatus'};
1.472     raeburn  6269:             } else {
                   6270:                 undef(%userenv);
                   6271:             }
                   6272:         }
1.536     raeburn  6273:         ($defquota,$settingstatus) = &default_quota($udom,$inststatus);
1.472     raeburn  6274:         if ($quota eq '') {
1.536     raeburn  6275:             $quota = $defquota;
                   6276:             $quotatype = 'default';
                   6277:         } else {
                   6278:             $quotatype = 'custom';
1.472     raeburn  6279:         }
                   6280:     }
1.536     raeburn  6281:     if (wantarray) {
                   6282:         return ($quota,$quotatype,$settingstatus,$defquota);
                   6283:     } else {
                   6284:         return $quota;
                   6285:     }
1.472     raeburn  6286: }
                   6287: 
                   6288: ###############################################
                   6289: 
                   6290: =pod
                   6291: 
                   6292: =item * &default_quota()
                   6293: 
1.536     raeburn  6294: Retrieves default quota assigned for storage of user portfolio files,
                   6295: given an (optional) user's institutional status.
1.472     raeburn  6296: 
                   6297: Incoming parameters:
                   6298: 1. domain
1.536     raeburn  6299: 2. (Optional) institutional status(es).  This is a : separated list of 
                   6300:    status types (e.g., faculty, staff, student etc.)
                   6301:    which apply to the user for whom the default is being retrieved.
                   6302:    If the institutional status string in undefined, the domain
                   6303:    default quota will be returned. 
1.472     raeburn  6304: 
                   6305: Returns:
                   6306: 1. Default disk quota (in Mb) for user portfolios in the domain.
1.536     raeburn  6307: 2. (Optional) institutional type which determined the value of the
                   6308:    default quota.
1.472     raeburn  6309: 
                   6310: If a value has been stored in the domain's configuration db,
                   6311: it will return that, otherwise it returns 20 (for backwards 
                   6312: compatibility with domains which have not set up a configuration
                   6313: db file; the original statically defined portfolio quota was 20 Mb). 
                   6314: 
1.536     raeburn  6315: If the user's status includes multiple types (e.g., staff and student),
                   6316: the largest default quota which applies to the user determines the
                   6317: default quota returned.
                   6318: 
1.472     raeburn  6319: =cut
                   6320: 
                   6321: ###############################################
                   6322: 
                   6323: 
                   6324: sub default_quota {
1.536     raeburn  6325:     my ($udom,$inststatus) = @_;
                   6326:     my ($defquota,$settingstatus);
                   6327:     my %quotahash = &Apache::lonnet::get_dom('configuration',
1.622     raeburn  6328:                                             ['quotas'],$udom);
                   6329:     if (ref($quotahash{'quotas'}) eq 'HASH') {
1.536     raeburn  6330:         if ($inststatus ne '') {
                   6331:             my @statuses = split(/:/,$inststatus);
                   6332:             foreach my $item (@statuses) {
1.622     raeburn  6333:                 if ($quotahash{'quotas'}{$item} ne '') {
1.536     raeburn  6334:                     if ($defquota eq '') {
1.622     raeburn  6335:                         $defquota = $quotahash{'quotas'}{$item};
1.536     raeburn  6336:                         $settingstatus = $item;
1.622     raeburn  6337:                     } elsif ($quotahash{'quotas'}{$item} > $defquota) {
                   6338:                         $defquota = $quotahash{'quotas'}{$item};
1.536     raeburn  6339:                         $settingstatus = $item;
                   6340:                     }
                   6341:                 }
                   6342:             }
                   6343:         }
                   6344:         if ($defquota eq '') {
1.622     raeburn  6345:             $defquota = $quotahash{'quotas'}{'default'};
1.536     raeburn  6346:             $settingstatus = 'default';
                   6347:         }
                   6348:     } else {
                   6349:         $settingstatus = 'default';
                   6350:         $defquota = 20;
                   6351:     }
                   6352:     if (wantarray) {
                   6353:         return ($defquota,$settingstatus);
1.472     raeburn  6354:     } else {
1.536     raeburn  6355:         return $defquota;
1.472     raeburn  6356:     }
                   6357: }
                   6358: 
1.384     raeburn  6359: sub get_secgrprole_info {
                   6360:     my ($cdom,$cnum,$needroles,$type)  = @_;
                   6361:     my %sections_count = &get_sections($cdom,$cnum);
                   6362:     my @sections =  (sort {$a <=> $b} keys(%sections_count));
                   6363:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
                   6364:     my @groups = sort(keys(%curr_groups));
                   6365:     my $allroles = [];
                   6366:     my $rolehash;
                   6367:     my $accesshash = {
                   6368:                      active => 'Currently has access',
                   6369:                      future => 'Will have future access',
                   6370:                      previous => 'Previously had access',
                   6371:                   };
                   6372:     if ($needroles) {
                   6373:         $rolehash = {'all' => 'all'};
1.385     albertel 6374:         my %user_roles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   6375: 	if (&Apache::lonnet::error(%user_roles)) {
                   6376: 	    undef(%user_roles);
                   6377: 	}
                   6378:         foreach my $item (keys(%user_roles)) {
1.384     raeburn  6379:             my ($role)=split(/\:/,$item,2);
                   6380:             if ($role eq 'cr') { next; }
                   6381:             if ($role =~ /^cr/) {
                   6382:                 $$rolehash{$role} = (split('/',$role))[3];
                   6383:             } else {
                   6384:                 $$rolehash{$role} = &Apache::lonnet::plaintext($role,$type);
                   6385:             }
                   6386:         }
                   6387:         foreach my $key (sort(keys(%{$rolehash}))) {
                   6388:             push(@{$allroles},$key);
                   6389:         }
                   6390:         push (@{$allroles},'st');
                   6391:         $$rolehash{'st'} = &Apache::lonnet::plaintext('st',$type);
                   6392:     }
                   6393:     return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
                   6394: }
                   6395: 
1.555     raeburn  6396: sub user_picker {
1.627     raeburn  6397:     my ($dom,$srch,$forcenewuser,$caller,$cancreate,$usertype) = @_;
1.555     raeburn  6398:     my $currdom = $dom;
                   6399:     my %curr_selected = (
                   6400:                         srchin => 'dom',
1.580     raeburn  6401:                         srchby => 'lastname',
1.555     raeburn  6402:                       );
                   6403:     my $srchterm;
1.625     raeburn  6404:     if ((ref($srch) eq 'HASH') && ($env{'form.origform'} ne 'crtusername')) {
1.555     raeburn  6405:         if ($srch->{'srchby'} ne '') {
                   6406:             $curr_selected{'srchby'} = $srch->{'srchby'};
                   6407:         }
                   6408:         if ($srch->{'srchin'} ne '') {
                   6409:             $curr_selected{'srchin'} = $srch->{'srchin'};
                   6410:         }
                   6411:         if ($srch->{'srchtype'} ne '') {
                   6412:             $curr_selected{'srchtype'} = $srch->{'srchtype'};
                   6413:         }
                   6414:         if ($srch->{'srchdomain'} ne '') {
                   6415:             $currdom = $srch->{'srchdomain'};
                   6416:         }
                   6417:         $srchterm = $srch->{'srchterm'};
                   6418:     }
                   6419:     my %lt=&Apache::lonlocal::texthash(
1.573     raeburn  6420:                     'usr'       => 'Search criteria',
1.563     raeburn  6421:                     'doma'      => 'Domain/institution to search',
1.558     albertel 6422:                     'uname'     => 'username',
                   6423:                     'lastname'  => 'last name',
1.555     raeburn  6424:                     'lastfirst' => 'last name, first name',
1.558     albertel 6425:                     'crs'       => 'in this course',
1.576     raeburn  6426:                     'dom'       => 'in selected LON-CAPA domain', 
1.558     albertel 6427:                     'alc'       => 'all LON-CAPA',
1.573     raeburn  6428:                     'instd'     => 'in institutional directory for selected domain',
1.558     albertel 6429:                     'exact'     => 'is',
                   6430:                     'contains'  => 'contains',
1.569     raeburn  6431:                     'begins'    => 'begins with',
1.571     raeburn  6432:                     'youm'      => "You must include some text to search for.",
                   6433:                     'thte'      => "The text you are searching for must contain at least two characters when using a 'begins' type search.",
                   6434:                     'thet'      => "The text you are searching for must contain at least three characters when using a 'contains' type search.",
                   6435:                     'yomc'      => "You must choose a domain when using an institutional directory search.",
                   6436:                     'ymcd'      => "You must choose a domain when using a domain search.",
                   6437:                     'whus'      => "When using searching by last,first you must include a comma as separator between last name and first name.",
                   6438:                     'whse'      => "When searching by last,first you must include at least one character in the first name.",
                   6439:                      'thfo'     => "The following need to be corrected before the search can be run:",
1.555     raeburn  6440:                                        );
1.563     raeburn  6441:     my $domform = &select_dom_form($currdom,'srchdomain',1,1);
                   6442:     my $srchinsel = ' <select name="srchin">';
1.555     raeburn  6443: 
                   6444:     my @srchins = ('crs','dom','alc','instd');
                   6445: 
                   6446:     foreach my $option (@srchins) {
                   6447:         # FIXME 'alc' option unavailable until 
                   6448:         #       loncreateuser::print_user_query_page()
                   6449:         #       has been completed.
                   6450:         next if ($option eq 'alc');
                   6451:         next if ($option eq 'crs' && !$env{'request.course.id'});
1.563     raeburn  6452:         if ($curr_selected{'srchin'} eq $option) {
                   6453:             $srchinsel .= ' 
                   6454:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   6455:         } else {
                   6456:             $srchinsel .= '
                   6457:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   6458:         }
1.555     raeburn  6459:     }
1.563     raeburn  6460:     $srchinsel .= "\n  </select>\n";
1.555     raeburn  6461: 
                   6462:     my $srchbysel =  ' <select name="srchby">';
1.580     raeburn  6463:     foreach my $option ('lastname','lastfirst','uname') {
1.555     raeburn  6464:         if ($curr_selected{'srchby'} eq $option) {
                   6465:             $srchbysel .= '
                   6466:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   6467:         } else {
                   6468:             $srchbysel .= '
                   6469:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   6470:          }
                   6471:     }
                   6472:     $srchbysel .= "\n  </select>\n";
                   6473: 
                   6474:     my $srchtypesel = ' <select name="srchtype">';
1.580     raeburn  6475:     foreach my $option ('begins','contains','exact') {
1.555     raeburn  6476:         if ($curr_selected{'srchtype'} eq $option) {
                   6477:             $srchtypesel .= '
                   6478:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   6479:         } else {
                   6480:             $srchtypesel .= '
                   6481:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   6482:         }
                   6483:     }
                   6484:     $srchtypesel .= "\n  </select>\n";
                   6485: 
1.558     albertel 6486:     my ($newuserscript,$new_user_create);
1.556     raeburn  6487: 
                   6488:     if ($forcenewuser) {
1.576     raeburn  6489:         if (ref($srch) eq 'HASH') {
                   6490:             if ($srch->{'srchby'} eq 'uname' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchin'} eq 'dom' && $srch->{'srchdomain'} eq $env{'request.role.domain'}) {
1.627     raeburn  6491:                 if ($cancreate) {
                   6492:                     $new_user_create = '<p> <input type="submit" name="forcenew" value="'.&HTML::Entities::encode(&mt('Make new user "[_1]"',$srchterm),'<>&"').'" onclick="javascript:setSearch(\'1\','.$caller.');" /> </p>';
                   6493:                 } else {
                   6494:                     my $helplink = ' href="javascript:helpMenu('."'display'".')"';
                   6495:                     my %usertypetext = (
                   6496:                         official   => 'institutional',
                   6497:                         unofficial => 'non-institutional',
                   6498:                     );
                   6499:                     $new_user_create = '<br /><span class="LC_warning">'.&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.").' '.&mt('Contact the <a[_1]>helpdesk</a> for assistance.',$helplink).'</span><br /><br />';
                   6500:                 }
1.576     raeburn  6501:             }
                   6502:         }
                   6503: 
1.556     raeburn  6504:         $newuserscript = <<"ENDSCRIPT";
                   6505: 
1.570     raeburn  6506: function setSearch(createnew,callingForm) {
1.556     raeburn  6507:     if (createnew == 1) {
1.570     raeburn  6508:         for (var i=0; i<callingForm.srchby.length; i++) {
                   6509:             if (callingForm.srchby.options[i].value == 'uname') {
                   6510:                 callingForm.srchby.selectedIndex = i;
1.556     raeburn  6511:             }
                   6512:         }
1.570     raeburn  6513:         for (var i=0; i<callingForm.srchin.length; i++) {
                   6514:             if ( callingForm.srchin.options[i].value == 'dom') {
                   6515: 		callingForm.srchin.selectedIndex = i;
1.556     raeburn  6516:             }
                   6517:         }
1.570     raeburn  6518:         for (var i=0; i<callingForm.srchtype.length; i++) {
                   6519:             if (callingForm.srchtype.options[i].value == 'exact') {
                   6520:                 callingForm.srchtype.selectedIndex = i;
1.556     raeburn  6521:             }
                   6522:         }
1.570     raeburn  6523:         for (var i=0; i<callingForm.srchdomain.length; i++) {
                   6524:             if (callingForm.srchdomain.options[i].value == '$env{'request.role.domain'}') {
                   6525:                 callingForm.srchdomain.selectedIndex = i;
1.556     raeburn  6526:             }
                   6527:         }
                   6528:     }
                   6529: }
                   6530: ENDSCRIPT
1.558     albertel 6531: 
1.556     raeburn  6532:     }
                   6533: 
1.555     raeburn  6534:     my $output = <<"END_BLOCK";
1.556     raeburn  6535: <script type="text/javascript">
1.570     raeburn  6536: function validateEntry(callingForm) {
1.558     albertel 6537: 
1.556     raeburn  6538:     var checkok = 1;
1.558     albertel 6539:     var srchin;
1.570     raeburn  6540:     for (var i=0; i<callingForm.srchin.length; i++) {
                   6541: 	if ( callingForm.srchin[i].checked ) {
                   6542: 	    srchin = callingForm.srchin[i].value;
1.558     albertel 6543: 	}
                   6544:     }
                   6545: 
1.570     raeburn  6546:     var srchtype = callingForm.srchtype.options[callingForm.srchtype.selectedIndex].value;
                   6547:     var srchby = callingForm.srchby.options[callingForm.srchby.selectedIndex].value;
                   6548:     var srchdomain = callingForm.srchdomain.options[callingForm.srchdomain.selectedIndex].value;
                   6549:     var srchterm =  callingForm.srchterm.value;
                   6550:     var srchin = callingForm.srchin.options[callingForm.srchin.selectedIndex].value;
1.556     raeburn  6551:     var msg = "";
                   6552: 
                   6553:     if (srchterm == "") {
                   6554:         checkok = 0;
1.571     raeburn  6555:         msg += "$lt{'youm'}\\n";
1.556     raeburn  6556:     }
                   6557: 
1.569     raeburn  6558:     if (srchtype== 'begins') {
                   6559:         if (srchterm.length < 2) {
                   6560:             checkok = 0;
1.571     raeburn  6561:             msg += "$lt{'thte'}\\n";
1.569     raeburn  6562:         }
                   6563:     }
                   6564: 
1.556     raeburn  6565:     if (srchtype== 'contains') {
                   6566:         if (srchterm.length < 3) {
                   6567:             checkok = 0;
1.571     raeburn  6568:             msg += "$lt{'thet'}\\n";
1.556     raeburn  6569:         }
                   6570:     }
                   6571:     if (srchin == 'instd') {
                   6572:         if (srchdomain == '') {
                   6573:             checkok = 0;
1.571     raeburn  6574:             msg += "$lt{'yomc'}\\n";
1.556     raeburn  6575:         }
                   6576:     }
                   6577:     if (srchin == 'dom') {
                   6578:         if (srchdomain == '') {
                   6579:             checkok = 0;
1.571     raeburn  6580:             msg += "$lt{'ymcd'}\\n";
1.556     raeburn  6581:         }
                   6582:     }
                   6583:     if (srchby == 'lastfirst') {
                   6584:         if (srchterm.indexOf(",") == -1) {
                   6585:             checkok = 0;
1.571     raeburn  6586:             msg += "$lt{'whus'}\\n";
1.556     raeburn  6587:         }
                   6588:         if (srchterm.indexOf(",") == srchterm.length -1) {
                   6589:             checkok = 0;
1.571     raeburn  6590:             msg += "$lt{'whse'}\\n";
1.556     raeburn  6591:         }
                   6592:     }
                   6593:     if (checkok == 0) {
1.571     raeburn  6594:         alert("$lt{'thfo'}\\n"+msg);
1.556     raeburn  6595:         return;
                   6596:     }
                   6597:     if (checkok == 1) {
1.570     raeburn  6598:         callingForm.submit();
1.556     raeburn  6599:     }
                   6600: }
                   6601: 
                   6602: $newuserscript
                   6603: 
                   6604: </script>
1.558     albertel 6605: 
                   6606: $new_user_create
                   6607: 
1.555     raeburn  6608: <table>
1.558     albertel 6609:  <tr>
1.573     raeburn  6610:   <td>$lt{'doma'}:</td>
                   6611:   <td>$domform</td>
                   6612:   </td>
                   6613:  </tr>
                   6614:  <tr>
                   6615:   <td>$lt{'usr'}:</td>
1.563     raeburn  6616:   <td>$srchbysel
                   6617:       $srchtypesel 
                   6618:       <input type="text" size="15" name="srchterm" value="$srchterm" />
1.564     albertel 6619:       $srchinsel 
1.563     raeburn  6620:   </td>
                   6621:  </tr>
1.555     raeburn  6622: </table>
                   6623: <br />
                   6624: END_BLOCK
1.558     albertel 6625: 
1.555     raeburn  6626:     return $output;
                   6627: }
                   6628: 
1.612     raeburn  6629: sub user_rule_check {
1.615     raeburn  6630:     my ($usershash,$checks,$alerts,$rulematch,$inst_results,$curr_rules,$got_rules) = @_;
1.612     raeburn  6631:     my $response;
                   6632:     if (ref($usershash) eq 'HASH') {
                   6633:         foreach my $user (keys(%{$usershash})) {
                   6634:             my ($uname,$udom) = split(/:/,$user);
                   6635:             next if ($udom eq '' || $uname eq '');
1.615     raeburn  6636:             my ($id,$newuser);
1.612     raeburn  6637:             if (ref($usershash->{$user}) eq 'HASH') {
1.615     raeburn  6638:                 $newuser = $usershash->{$user}->{'newuser'};
1.612     raeburn  6639:                 $id = $usershash->{$user}->{'id'};
                   6640:             }
                   6641:             my $inst_response;
                   6642:             if (ref($checks) eq 'HASH') {
                   6643:                 if (defined($checks->{'username'})) {
1.615     raeburn  6644:                     ($inst_response,%{$inst_results->{$user}}) = 
1.612     raeburn  6645:                         &Apache::lonnet::get_instuser($udom,$uname);
                   6646:                 } elsif (defined($checks->{'id'})) {
1.615     raeburn  6647:                     ($inst_response,%{$inst_results->{$user}}) =
1.612     raeburn  6648:                         &Apache::lonnet::get_instuser($udom,undef,$id);
                   6649:                 }
1.615     raeburn  6650:             } else {
                   6651:                 ($inst_response,%{$inst_results->{$user}}) =
                   6652:                     &Apache::lonnet::get_instuser($udom,$uname);
                   6653:                 return;
1.612     raeburn  6654:             }
1.615     raeburn  6655:             if (!$got_rules->{$udom}) {
1.612     raeburn  6656:                 my %domconfig = &Apache::lonnet::get_dom('configuration',
                   6657:                                                   ['usercreation'],$udom);
                   6658:                 if (ref($domconfig{'usercreation'}) eq 'HASH') {
1.615     raeburn  6659:                     foreach my $item ('username','id') {
1.612     raeburn  6660:                         if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
                   6661:                             $$curr_rules{$udom}{$item} = 
                   6662:                                 $domconfig{'usercreation'}{$item.'_rule'};
1.585     raeburn  6663:                         }
                   6664:                     }
                   6665:                 }
1.615     raeburn  6666:                 $got_rules->{$udom} = 1;  
1.585     raeburn  6667:             }
1.612     raeburn  6668:             foreach my $item (keys(%{$checks})) {
                   6669:                 if (ref($$curr_rules{$udom}) eq 'HASH') {
                   6670:                     if (ref($$curr_rules{$udom}{$item}) eq 'ARRAY') {
                   6671:                         if (@{$$curr_rules{$udom}{$item}} > 0) {
                   6672:                             my %rule_check = &Apache::lonnet::inst_rulecheck($udom,$uname,$id,$item,$$curr_rules{$udom}{$item});
                   6673:                             foreach my $rule (@{$$curr_rules{$udom}{$item}}) {
                   6674:                                 if ($rule_check{$rule}) {
                   6675:                                     $$rulematch{$user}{$item} = $rule;
                   6676:                                     if ($inst_response eq 'ok') {
1.615     raeburn  6677:                                         if (ref($inst_results) eq 'HASH') {
                   6678:                                             if (ref($inst_results->{$user}) eq 'HASH') {
                   6679:                                                 if (keys(%{$inst_results->{$user}}) == 0) {
                   6680:                                                     $$alerts{$item}{$udom}{$uname} = 1;
                   6681:                                                 }
1.612     raeburn  6682:                                             }
                   6683:                                         }
1.615     raeburn  6684:                                     }
                   6685:                                     last;
1.585     raeburn  6686:                                 }
                   6687:                             }
                   6688:                         }
                   6689:                     }
                   6690:                 }
                   6691:             }
                   6692:         }
                   6693:     }
1.612     raeburn  6694:     return;
                   6695: }
                   6696: 
                   6697: sub user_rule_formats {
                   6698:     my ($domain,$domdesc,$curr_rules,$check) = @_;
                   6699:     my %text = ( 
                   6700:                  'username' => 'Usernames',
                   6701:                  'id'       => 'IDs',
                   6702:                );
                   6703:     my $output;
                   6704:     my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($domain,$check);
                   6705:     if ((ref($rules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
                   6706:         if (@{$ruleorder} > 0) {
                   6707:             $output = '<br />'.&mt("$text{$check} with the following format(s) may <span class=\"LC_cusr_emph\">only</span> be used for verified users at [_1]:",$domdesc).' <ul>';
                   6708:             foreach my $rule (@{$ruleorder}) {
                   6709:                 if (ref($curr_rules) eq 'ARRAY') {
                   6710:                     if (grep(/^\Q$rule\E$/,@{$curr_rules})) {
                   6711:                         if (ref($rules->{$rule}) eq 'HASH') {
                   6712:                             $output .= '<li>'.$rules->{$rule}{'name'}.': '.
                   6713:                                         $rules->{$rule}{'desc'}.'</li>';
                   6714:                         }
                   6715:                     }
                   6716:                 }
                   6717:             }
                   6718:             $output .= '</ul>';
                   6719:         }
                   6720:     }
                   6721:     return $output;
                   6722: }
                   6723: 
                   6724: sub instrule_disallow_msg {
1.615     raeburn  6725:     my ($checkitem,$domdesc,$count,$mode) = @_;
1.612     raeburn  6726:     my $response;
                   6727:     my %text = (
                   6728:                   item   => 'username',
                   6729:                   items  => 'usernames',
                   6730:                   match  => 'matches',
                   6731:                   do     => 'does',
                   6732:                   action => 'a username',
                   6733:                   one    => 'one',
                   6734:                );
                   6735:     if ($count > 1) {
                   6736:         $text{'item'} = 'usernames';
                   6737:         $text{'match'} ='match';
                   6738:         $text{'do'} = 'do';
                   6739:         $text{'action'} = 'usernames',
                   6740:         $text{'one'} = 'ones';
                   6741:     }
                   6742:     if ($checkitem eq 'id') {
                   6743:         $text{'items'} = 'IDs';
                   6744:         $text{'item'} = 'ID';
                   6745:         $text{'action'} = 'an ID';
1.615     raeburn  6746:         if ($count > 1) {
                   6747:             $text{'item'} = 'IDs';
                   6748:             $text{'action'} = 'IDs';
                   6749:         }
1.612     raeburn  6750:     }
                   6751:     $response = &mt("The $text{'item'} you chose $text{'match'} the format of $text{'items'} defined for <span class=\"LC_cusr_emph\">[_1]</span>, but the $text{'item'} $text{'do'} not exist in the institutional directory.",$domdesc).'<br />';
1.615     raeburn  6752:     if ($mode eq 'upload') {
                   6753:         if ($checkitem eq 'username') {
                   6754:             $response .= &mt("You will need to modify your upload file so it will include $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}.");
                   6755:         } elsif ($checkitem eq 'id') {
                   6756:             $response .= &mt("Either upload a file which includes $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}, or when associating fields with data columns, omit an association for the ID/Student Number field.");
                   6757:         }
                   6758:     } else {
                   6759:         if ($checkitem eq 'username') {
                   6760:             $response .= &mt("You must choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}.");
                   6761:         } elsif ($checkitem eq 'id') {
                   6762:             $response .= &mt("You must either choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}, or leave the ID field blank.");
                   6763:         }
1.612     raeburn  6764:     }
                   6765:     return $response;
1.585     raeburn  6766: }
                   6767: 
1.624     raeburn  6768: sub personal_data_fieldtitles {
                   6769:     my %fieldtitles = &Apache::lonlocal::texthash (
                   6770:                         id => 'Student/Employee ID',
                   6771:                         permanentemail => 'E-mail address',
                   6772:                         lastname => 'Last Name',
                   6773:                         firstname => 'First Name',
                   6774:                         middlename => 'Middle Name',
                   6775:                         generation => 'Generation',
                   6776:                         gen => 'Generation',
                   6777:                    );
                   6778:     return %fieldtitles;
                   6779: }
                   6780: 
1.642     raeburn  6781: sub sorted_inst_types {
                   6782:     my ($dom) = @_;
                   6783:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($dom);
                   6784:     my $othertitle = &mt('All users');
                   6785:     if ($env{'request.course.id'}) {
                   6786:         $othertitle  = 'any';
                   6787:     }
                   6788:     my @types;
                   6789:     if (ref($order) eq 'ARRAY') {
                   6790:         @types = @{$order};
                   6791:     }
                   6792:     if (@types == 0) {
                   6793:         if (ref($usertypes) eq 'HASH') {
                   6794:             @types = sort(keys(%{$usertypes}));
                   6795:         }
                   6796:     }
                   6797:     if (keys(%{$usertypes}) > 0) {
                   6798:         $othertitle = &mt('Other users');
                   6799:         if ($env{'request.course.id'}) {
                   6800:             $othertitle = 'other';
                   6801:         }
                   6802:     }
                   6803:     return ($othertitle,$usertypes,\@types);
                   6804: }
                   6805: 
1.645     raeburn  6806: sub get_institutional_codes {
                   6807:     my ($settings,$allcourses,$LC_code) = @_;
                   6808: # Get complete list of course sections to update
                   6809:     my @currsections = ();
                   6810:     my @currxlists = ();
                   6811:     my $coursecode = $$settings{'internal.coursecode'};
                   6812: 
                   6813:     if ($$settings{'internal.sectionnums'} ne '') {
                   6814:         @currsections = split(/,/,$$settings{'internal.sectionnums'});
                   6815:     }
                   6816: 
                   6817:     if ($$settings{'internal.crosslistings'} ne '') {
                   6818:         @currxlists = split(/,/,$$settings{'internal.crosslistings'});
                   6819:     }
                   6820: 
                   6821:     if (@currxlists > 0) {
                   6822:         foreach (@currxlists) {
                   6823:             if (m/^([^:]+):(\w*)$/) {
                   6824:                 unless (grep/^$1$/,@{$allcourses}) {
                   6825:                     push @{$allcourses},$1;
                   6826:                     $$LC_code{$1} = $2;
                   6827:                 }
                   6828:             }
                   6829:         }
                   6830:     }
                   6831:  
                   6832:     if (@currsections > 0) {
                   6833:         foreach (@currsections) {
                   6834:             if (m/^(\w+):(\w*)$/) {
                   6835:                 my $sec = $coursecode.$1;
                   6836:                 my $lc_sec = $2;
                   6837:                 unless (grep/^$sec$/,@{$allcourses}) {
                   6838:                     push @{$allcourses},$sec;
                   6839:                     $$LC_code{$sec} = $lc_sec;
                   6840:                 }
                   6841:             }
                   6842:         }
                   6843:     }
                   6844:     return;
                   6845: }
                   6846: 
1.112     bowersj2 6847: =pod
                   6848: 
1.549     albertel 6849: =back
                   6850: 
                   6851: =head1 HTTP Helpers
                   6852: 
                   6853: =over 4
                   6854: 
1.648     raeburn  6855: =item * &get_unprocessed_cgi($query,$possible_names)
1.112     bowersj2 6856: 
1.258     albertel 6857: Modify the %env hash to contain unprocessed CGI form parameters held in
1.112     bowersj2 6858: $query.  The parameters listed in $possible_names (an array reference),
1.258     albertel 6859: will be set in $env{'form.name'} if they do not already exist.
1.112     bowersj2 6860: 
                   6861: Typically called with $ENV{'QUERY_STRING'} as the first parameter.  
                   6862: $possible_names is an ref to an array of form element names.  As an example:
                   6863: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
1.258     albertel 6864: will result in $env{'form.uname'} and $env{'form.udom'} being set.
1.112     bowersj2 6865: 
                   6866: =cut
1.1       albertel 6867: 
1.6       albertel 6868: sub get_unprocessed_cgi {
1.25      albertel 6869:   my ($query,$possible_names)= @_;
1.26      matthew  6870:   # $Apache::lonxml::debug=1;
1.356     albertel 6871:   foreach my $pair (split(/&/,$query)) {
                   6872:     my ($name, $value) = split(/=/,$pair);
1.369     www      6873:     $name = &unescape($name);
1.25      albertel 6874:     if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
                   6875:       $value =~ tr/+/ /;
                   6876:       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.258     albertel 6877:       unless (defined($env{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
1.25      albertel 6878:     }
1.16      harris41 6879:   }
1.6       albertel 6880: }
                   6881: 
1.112     bowersj2 6882: =pod
                   6883: 
1.648     raeburn  6884: =item * &cacheheader() 
1.112     bowersj2 6885: 
                   6886: returns cache-controlling header code
                   6887: 
                   6888: =cut
                   6889: 
1.7       albertel 6890: sub cacheheader {
1.258     albertel 6891:     unless ($env{'request.method'} eq 'GET') { return ''; }
1.216     albertel 6892:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
                   6893:     my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
1.7       albertel 6894:                 <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
                   6895:                 <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
1.216     albertel 6896:     return $output;
1.7       albertel 6897: }
                   6898: 
1.112     bowersj2 6899: =pod
                   6900: 
1.648     raeburn  6901: =item * &no_cache($r) 
1.112     bowersj2 6902: 
                   6903: specifies header code to not have cache
                   6904: 
                   6905: =cut
                   6906: 
1.9       albertel 6907: sub no_cache {
1.216     albertel 6908:     my ($r) = @_;
                   6909:     if ($ENV{'REQUEST_METHOD'} ne 'GET' &&
1.258     albertel 6910: 	$env{'request.method'} ne 'GET') { return ''; }
1.216     albertel 6911:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time));
                   6912:     $r->no_cache(1);
                   6913:     $r->header_out("Expires" => $date);
                   6914:     $r->header_out("Pragma" => "no-cache");
1.123     www      6915: }
                   6916: 
                   6917: sub content_type {
1.181     albertel 6918:     my ($r,$type,$charset) = @_;
1.299     foxr     6919:     if ($r) {
                   6920: 	#  Note that printout.pl calls this with undef for $r.
                   6921: 	&no_cache($r);
                   6922:     }
1.258     albertel 6923:     if ($env{'browser.mathml'} && $type eq 'text/html') { $type='text/xml'; }
1.181     albertel 6924:     unless ($charset) {
                   6925: 	$charset=&Apache::lonlocal::current_encoding;
                   6926:     }
                   6927:     if ($charset) { $type.='; charset='.$charset; }
                   6928:     if ($r) {
                   6929: 	$r->content_type($type);
                   6930:     } else {
                   6931: 	print("Content-type: $type\n\n");
                   6932:     }
1.9       albertel 6933: }
1.25      albertel 6934: 
1.112     bowersj2 6935: =pod
                   6936: 
1.648     raeburn  6937: =item * &add_to_env($name,$value) 
1.112     bowersj2 6938: 
1.258     albertel 6939: adds $name to the %env hash with value
1.112     bowersj2 6940: $value, if $name already exists, the entry is converted to an array
                   6941: reference and $value is added to the array.
                   6942: 
                   6943: =cut
                   6944: 
1.25      albertel 6945: sub add_to_env {
                   6946:   my ($name,$value)=@_;
1.258     albertel 6947:   if (defined($env{$name})) {
                   6948:     if (ref($env{$name})) {
1.25      albertel 6949:       #already have multiple values
1.258     albertel 6950:       push(@{ $env{$name} },$value);
1.25      albertel 6951:     } else {
                   6952:       #first time seeing multiple values, convert hash entry to an arrayref
1.258     albertel 6953:       my $first=$env{$name};
                   6954:       undef($env{$name});
                   6955:       push(@{ $env{$name} },$first,$value);
1.25      albertel 6956:     }
                   6957:   } else {
1.258     albertel 6958:     $env{$name}=$value;
1.25      albertel 6959:   }
1.31      albertel 6960: }
1.149     albertel 6961: 
                   6962: =pod
                   6963: 
1.648     raeburn  6964: =item * &get_env_multiple($name) 
1.149     albertel 6965: 
1.258     albertel 6966: gets $name from the %env hash, it seemlessly handles the cases where multiple
1.149     albertel 6967: values may be defined and end up as an array ref.
                   6968: 
                   6969: returns an array of values
                   6970: 
                   6971: =cut
                   6972: 
                   6973: sub get_env_multiple {
                   6974:     my ($name) = @_;
                   6975:     my @values;
1.258     albertel 6976:     if (defined($env{$name})) {
1.149     albertel 6977:         # exists is it an array
1.258     albertel 6978:         if (ref($env{$name})) {
                   6979:             @values=@{ $env{$name} };
1.149     albertel 6980:         } else {
1.258     albertel 6981:             $values[0]=$env{$name};
1.149     albertel 6982:         }
                   6983:     }
                   6984:     return(@values);
                   6985: }
                   6986: 
1.31      albertel 6987: 
1.41      ng       6988: =pod
1.45      matthew  6989: 
1.464     albertel 6990: =back
1.41      ng       6991: 
1.112     bowersj2 6992: =head1 CSV Upload/Handling functions
1.38      albertel 6993: 
1.41      ng       6994: =over 4
                   6995: 
1.648     raeburn  6996: =item * &upfile_store($r)
1.41      ng       6997: 
                   6998: Store uploaded file, $r should be the HTTP Request object,
1.258     albertel 6999: needs $env{'form.upfile'}
1.41      ng       7000: returns $datatoken to be put into hidden field
                   7001: 
                   7002: =cut
1.31      albertel 7003: 
                   7004: sub upfile_store {
                   7005:     my $r=shift;
1.258     albertel 7006:     $env{'form.upfile'}=~s/\r/\n/gs;
                   7007:     $env{'form.upfile'}=~s/\f/\n/gs;
                   7008:     $env{'form.upfile'}=~s/\n+/\n/gs;
                   7009:     $env{'form.upfile'}=~s/\n+$//gs;
1.31      albertel 7010: 
1.258     albertel 7011:     my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
                   7012: 	'_enroll_'.$env{'request.course.id'}.'_'.time.'_'.$$;
1.31      albertel 7013:     {
1.158     raeburn  7014:         my $datafile = $r->dir_config('lonDaemons').
                   7015:                            '/tmp/'.$datatoken.'.tmp';
                   7016:         if ( open(my $fh,">$datafile") ) {
1.258     albertel 7017:             print $fh $env{'form.upfile'};
1.158     raeburn  7018:             close($fh);
                   7019:         }
1.31      albertel 7020:     }
                   7021:     return $datatoken;
                   7022: }
                   7023: 
1.56      matthew  7024: =pod
                   7025: 
1.648     raeburn  7026: =item * &load_tmp_file($r)
1.41      ng       7027: 
                   7028: Load uploaded file from tmp, $r should be the HTTP Request object,
1.258     albertel 7029: needs $env{'form.datatoken'},
                   7030: sets $env{'form.upfile'} to the contents of the file
1.41      ng       7031: 
                   7032: =cut
1.31      albertel 7033: 
                   7034: sub load_tmp_file {
                   7035:     my $r=shift;
                   7036:     my @studentdata=();
                   7037:     {
1.158     raeburn  7038:         my $studentfile = $r->dir_config('lonDaemons').
1.258     albertel 7039:                               '/tmp/'.$env{'form.datatoken'}.'.tmp';
1.158     raeburn  7040:         if ( open(my $fh,"<$studentfile") ) {
                   7041:             @studentdata=<$fh>;
                   7042:             close($fh);
                   7043:         }
1.31      albertel 7044:     }
1.258     albertel 7045:     $env{'form.upfile'}=join('',@studentdata);
1.31      albertel 7046: }
                   7047: 
1.56      matthew  7048: =pod
                   7049: 
1.648     raeburn  7050: =item * &upfile_record_sep()
1.41      ng       7051: 
                   7052: Separate uploaded file into records
                   7053: returns array of records,
1.258     albertel 7054: needs $env{'form.upfile'} and $env{'form.upfiletype'}
1.41      ng       7055: 
                   7056: =cut
1.31      albertel 7057: 
                   7058: sub upfile_record_sep {
1.258     albertel 7059:     if ($env{'form.upfiletype'} eq 'xml') {
1.31      albertel 7060:     } else {
1.248     albertel 7061: 	my @records;
1.258     albertel 7062: 	foreach my $line (split(/\n/,$env{'form.upfile'})) {
1.248     albertel 7063: 	    if ($line=~/^\s*$/) { next; }
                   7064: 	    push(@records,$line);
                   7065: 	}
                   7066: 	return @records;
1.31      albertel 7067:     }
                   7068: }
                   7069: 
1.56      matthew  7070: =pod
                   7071: 
1.648     raeburn  7072: =item * &record_sep($record)
1.41      ng       7073: 
1.258     albertel 7074: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $env{'form.upfiletype'}
1.41      ng       7075: 
                   7076: =cut
                   7077: 
1.263     www      7078: sub takeleft {
                   7079:     my $index=shift;
                   7080:     return substr('0000'.$index,-4,4);
                   7081: }
                   7082: 
1.31      albertel 7083: sub record_sep {
                   7084:     my $record=shift;
                   7085:     my %components=();
1.258     albertel 7086:     if ($env{'form.upfiletype'} eq 'xml') {
                   7087:     } elsif ($env{'form.upfiletype'} eq 'space') {
1.31      albertel 7088:         my $i=0;
1.356     albertel 7089:         foreach my $field (split(/\s+/,$record)) {
1.31      albertel 7090:             $field=~s/^(\"|\')//;
                   7091:             $field=~s/(\"|\')$//;
1.263     www      7092:             $components{&takeleft($i)}=$field;
1.31      albertel 7093:             $i++;
                   7094:         }
1.258     albertel 7095:     } elsif ($env{'form.upfiletype'} eq 'tab') {
1.31      albertel 7096:         my $i=0;
1.356     albertel 7097:         foreach my $field (split(/\t/,$record)) {
1.31      albertel 7098:             $field=~s/^(\"|\')//;
                   7099:             $field=~s/(\"|\')$//;
1.263     www      7100:             $components{&takeleft($i)}=$field;
1.31      albertel 7101:             $i++;
                   7102:         }
                   7103:     } else {
1.561     www      7104:         my $separator=',';
1.480     banghart 7105:         if ($env{'form.upfiletype'} eq 'semisv') {
1.561     www      7106:             $separator=';';
1.480     banghart 7107:         }
1.31      albertel 7108:         my $i=0;
1.561     www      7109: # the character we are looking for to indicate the end of a quote or a record 
                   7110:         my $looking_for=$separator;
                   7111: # do not add the characters to the fields
                   7112:         my $ignore=0;
                   7113: # we just encountered a separator (or the beginning of the record)
                   7114:         my $just_found_separator=1;
                   7115: # store the field we are working on here
                   7116:         my $field='';
                   7117: # work our way through all characters in record
                   7118:         foreach my $character ($record=~/(.)/g) {
                   7119:             if ($character eq $looking_for) {
                   7120:                if ($character ne $separator) {
                   7121: # Found the end of a quote, again looking for separator
                   7122:                   $looking_for=$separator;
                   7123:                   $ignore=1;
                   7124:                } else {
                   7125: # Found a separator, store away what we got
                   7126:                   $components{&takeleft($i)}=$field;
                   7127: 	          $i++;
                   7128:                   $just_found_separator=1;
                   7129:                   $ignore=0;
                   7130:                   $field='';
                   7131:                }
                   7132:                next;
                   7133:             }
                   7134: # single or double quotation marks after a separator indicate beginning of a quote
                   7135: # we are now looking for the end of the quote and need to ignore separators
                   7136:             if ((($character eq '"') || ($character eq "'")) && ($just_found_separator))  {
                   7137:                $looking_for=$character;
                   7138:                next;
                   7139:             }
                   7140: # ignore would be true after we reached the end of a quote
                   7141:             if ($ignore) { next; }
                   7142:             if (($just_found_separator) && ($character=~/\s/)) { next; }
                   7143:             $field.=$character;
                   7144:             $just_found_separator=0; 
1.31      albertel 7145:         }
1.561     www      7146: # catch the very last entry, since we never encountered the separator
                   7147:         $components{&takeleft($i)}=$field;
1.31      albertel 7148:     }
                   7149:     return %components;
                   7150: }
                   7151: 
1.144     matthew  7152: ######################################################
                   7153: ######################################################
                   7154: 
1.56      matthew  7155: =pod
                   7156: 
1.648     raeburn  7157: =item * &upfile_select_html()
1.41      ng       7158: 
1.144     matthew  7159: Return HTML code to select a file from the users machine and specify 
                   7160: the file type.
1.41      ng       7161: 
                   7162: =cut
                   7163: 
1.144     matthew  7164: ######################################################
                   7165: ######################################################
1.31      albertel 7166: sub upfile_select_html {
1.144     matthew  7167:     my %Types = (
                   7168:                  csv   => &mt('CSV (comma separated values, spreadsheet)'),
1.480     banghart 7169:                  semisv => &mt('Semicolon separated values'),
1.144     matthew  7170:                  space => &mt('Space separated'),
                   7171:                  tab   => &mt('Tabulator separated'),
                   7172: #                 xml   => &mt('HTML/XML'),
                   7173:                  );
                   7174:     my $Str = '<input type="file" name="upfile" size="50" />'.
                   7175:         '<br />Type: <select name="upfiletype">';
                   7176:     foreach my $type (sort(keys(%Types))) {
                   7177:         $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
                   7178:     }
                   7179:     $Str .= "</select>\n";
                   7180:     return $Str;
1.31      albertel 7181: }
                   7182: 
1.301     albertel 7183: sub get_samples {
                   7184:     my ($records,$toget) = @_;
                   7185:     my @samples=({});
                   7186:     my $got=0;
                   7187:     foreach my $rec (@$records) {
                   7188: 	my %temp = &record_sep($rec);
                   7189: 	if (! grep(/\S/, values(%temp))) { next; }
                   7190: 	if (%temp) {
                   7191: 	    $samples[$got]=\%temp;
                   7192: 	    $got++;
                   7193: 	    if ($got == $toget) { last; }
                   7194: 	}
                   7195:     }
                   7196:     return \@samples;
                   7197: }
                   7198: 
1.144     matthew  7199: ######################################################
                   7200: ######################################################
                   7201: 
1.56      matthew  7202: =pod
                   7203: 
1.648     raeburn  7204: =item * &csv_print_samples($r,$records)
1.41      ng       7205: 
                   7206: Prints a table of sample values from each column uploaded $r is an
                   7207: Apache Request ref, $records is an arrayref from
                   7208: &Apache::loncommon::upfile_record_sep
                   7209: 
                   7210: =cut
                   7211: 
1.144     matthew  7212: ######################################################
                   7213: ######################################################
1.31      albertel 7214: sub csv_print_samples {
                   7215:     my ($r,$records) = @_;
1.301     albertel 7216:     my $samples = &get_samples($records,3);
                   7217: 
1.594     raeburn  7218:     $r->print(&mt('Samples').'<br />'.&start_data_table().
                   7219:               &start_data_table_header_row());
1.356     albertel 7220:     foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) { 
                   7221:         $r->print('<th>'.&mt('Column&nbsp;[_1]',($sample+1)).'</th>'); }
1.594     raeburn  7222:     $r->print(&end_data_table_header_row());
1.301     albertel 7223:     foreach my $hash (@$samples) {
1.594     raeburn  7224: 	$r->print(&start_data_table_row());
1.356     albertel 7225: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
1.31      albertel 7226: 	    $r->print('<td>');
1.356     albertel 7227: 	    if (defined($$hash{$sample})) { $r->print($$hash{$sample}); }
1.31      albertel 7228: 	    $r->print('</td>');
                   7229: 	}
1.594     raeburn  7230: 	$r->print(&end_data_table_row());
1.31      albertel 7231:     }
1.594     raeburn  7232:     $r->print(&end_data_table().'<br />'."\n");
1.31      albertel 7233: }
                   7234: 
1.144     matthew  7235: ######################################################
                   7236: ######################################################
                   7237: 
1.56      matthew  7238: =pod
                   7239: 
1.648     raeburn  7240: =item * &csv_print_select_table($r,$records,$d)
1.41      ng       7241: 
                   7242: Prints a table to create associations between values and table columns.
1.144     matthew  7243: 
1.41      ng       7244: $r is an Apache Request ref,
                   7245: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
1.174     matthew  7246: $d is an array of 2 element arrays (internal name, displayed name,defaultcol)
1.41      ng       7247: 
                   7248: =cut
                   7249: 
1.144     matthew  7250: ######################################################
                   7251: ######################################################
1.31      albertel 7252: sub csv_print_select_table {
                   7253:     my ($r,$records,$d) = @_;
1.301     albertel 7254:     my $i=0;
                   7255:     my $samples = &get_samples($records,1);
1.144     matthew  7256:     $r->print(&mt('Associate columns with student attributes.')."\n".
1.594     raeburn  7257: 	      &start_data_table().&start_data_table_header_row().
1.144     matthew  7258:               '<th>'.&mt('Attribute').'</th>'.
1.594     raeburn  7259:               '<th>'.&mt('Column').'</th>'.
                   7260:               &end_data_table_header_row()."\n");
1.356     albertel 7261:     foreach my $array_ref (@$d) {
                   7262: 	my ($value,$display,$defaultcol)=@{ $array_ref };
1.594     raeburn  7263: 	$r->print(&start_data_table_row().'<tr><td>'.$display.'</td>');
1.31      albertel 7264: 
                   7265: 	$r->print('<td><select name=f'.$i.
1.32      matthew  7266: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.31      albertel 7267: 	$r->print('<option value="none"></option>');
1.356     albertel 7268: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
                   7269: 	    $r->print('<option value="'.$sample.'"'.
                   7270:                       ($sample eq $defaultcol ? ' selected="selected" ' : '').
                   7271:                       '>Column '.($sample+1).'</option>');
1.31      albertel 7272: 	}
1.594     raeburn  7273: 	$r->print('</select></td>'.&end_data_table_row()."\n");
1.31      albertel 7274: 	$i++;
                   7275:     }
1.594     raeburn  7276:     $r->print(&end_data_table());
1.31      albertel 7277:     $i--;
                   7278:     return $i;
                   7279: }
1.56      matthew  7280: 
1.144     matthew  7281: ######################################################
                   7282: ######################################################
                   7283: 
1.56      matthew  7284: =pod
1.31      albertel 7285: 
1.648     raeburn  7286: =item * &csv_samples_select_table($r,$records,$d)
1.41      ng       7287: 
                   7288: Prints a table of sample values from the upload and can make associate samples to internal names.
                   7289: 
                   7290: $r is an Apache Request ref,
                   7291: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
                   7292: $d is an array of 2 element arrays (internal name, displayed name)
                   7293: 
                   7294: =cut
                   7295: 
1.144     matthew  7296: ######################################################
                   7297: ######################################################
1.31      albertel 7298: sub csv_samples_select_table {
                   7299:     my ($r,$records,$d) = @_;
                   7300:     my $i=0;
1.144     matthew  7301:     #
1.301     albertel 7302:     my $samples = &get_samples($records,3);
1.594     raeburn  7303:     $r->print(&start_data_table().
                   7304:               &start_data_table_header_row().'<th>'.
                   7305:               &mt('Field').'</th><th>'.&mt('Samples').'</th>'.
                   7306:               &end_data_table_header_row());
1.301     albertel 7307: 
                   7308:     foreach my $key (sort(keys(%{ $samples->[0] }))) {
1.594     raeburn  7309: 	$r->print(&start_data_table_row().'<td><select name="f'.$i.'"'.
1.32      matthew  7310: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.301     albertel 7311: 	foreach my $option (@$d) {
                   7312: 	    my ($value,$display,$defaultcol)=@{ $option };
1.174     matthew  7313: 	    $r->print('<option value="'.$value.'"'.
1.253     albertel 7314:                       ($i eq $defaultcol ? ' selected="selected" ':'').'>'.
1.174     matthew  7315:                       $display.'</option>');
1.31      albertel 7316: 	}
                   7317: 	$r->print('</select></td><td>');
1.301     albertel 7318: 	foreach my $line (0..2) {
                   7319: 	    if (defined($samples->[$line]{$key})) { 
                   7320: 		$r->print($samples->[$line]{$key}."<br />\n"); 
                   7321: 	    }
                   7322: 	}
1.594     raeburn  7323: 	$r->print('</td>'.&end_data_table_row());
1.31      albertel 7324: 	$i++;
                   7325:     }
1.594     raeburn  7326:     $r->print(&end_data_table());
1.31      albertel 7327:     $i--;
                   7328:     return($i);
1.115     matthew  7329: }
                   7330: 
1.144     matthew  7331: ######################################################
                   7332: ######################################################
                   7333: 
1.115     matthew  7334: =pod
                   7335: 
1.648     raeburn  7336: =item * &clean_excel_name($name)
1.115     matthew  7337: 
                   7338: Returns a replacement for $name which does not contain any illegal characters.
                   7339: 
                   7340: =cut
                   7341: 
1.144     matthew  7342: ######################################################
                   7343: ######################################################
1.115     matthew  7344: sub clean_excel_name {
                   7345:     my ($name) = @_;
                   7346:     $name =~ s/[:\*\?\/\\]//g;
                   7347:     if (length($name) > 31) {
                   7348:         $name = substr($name,0,31);
                   7349:     }
                   7350:     return $name;
1.25      albertel 7351: }
1.84      albertel 7352: 
1.85      albertel 7353: =pod
                   7354: 
1.648     raeburn  7355: =item * &check_if_partid_hidden($id,$symb,$udom,$uname)
1.85      albertel 7356: 
                   7357: Returns either 1 or undef
                   7358: 
                   7359: 1 if the part is to be hidden, undef if it is to be shown
                   7360: 
                   7361: Arguments are:
                   7362: 
                   7363: $id the id of the part to be checked
                   7364: $symb, optional the symb of the resource to check
                   7365: $udom, optional the domain of the user to check for
                   7366: $uname, optional the username of the user to check for
                   7367: 
                   7368: =cut
1.84      albertel 7369: 
                   7370: sub check_if_partid_hidden {
                   7371:     my ($id,$symb,$udom,$uname) = @_;
1.133     albertel 7372:     my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
1.84      albertel 7373: 					 $symb,$udom,$uname);
1.141     albertel 7374:     my $truth=1;
                   7375:     #if the string starts with !, then the list is the list to show not hide
                   7376:     if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
1.84      albertel 7377:     my @hiddenlist=split(/,/,$hiddenparts);
                   7378:     foreach my $checkid (@hiddenlist) {
1.141     albertel 7379: 	if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
1.84      albertel 7380:     }
1.141     albertel 7381:     return !$truth;
1.84      albertel 7382: }
1.127     matthew  7383: 
1.138     matthew  7384: 
                   7385: ############################################################
                   7386: ############################################################
                   7387: 
                   7388: =pod
                   7389: 
1.157     matthew  7390: =back 
                   7391: 
1.138     matthew  7392: =head1 cgi-bin script and graphing routines
                   7393: 
1.157     matthew  7394: =over 4
                   7395: 
1.648     raeburn  7396: =item * &get_cgi_id()
1.138     matthew  7397: 
                   7398: Inputs: none
                   7399: 
                   7400: Returns an id which can be used to pass environment variables
                   7401: to various cgi-bin scripts.  These environment variables will
                   7402: be removed from the users environment after a given time by
                   7403: the routine &Apache::lonnet::transfer_profile_to_env.
                   7404: 
                   7405: =cut
                   7406: 
                   7407: ############################################################
                   7408: ############################################################
1.152     albertel 7409: my $uniq=0;
1.136     matthew  7410: sub get_cgi_id {
1.154     albertel 7411:     $uniq=($uniq+1)%100000;
1.280     albertel 7412:     return (time.'_'.$$.'_'.$uniq);
1.136     matthew  7413: }
                   7414: 
1.127     matthew  7415: ############################################################
                   7416: ############################################################
                   7417: 
                   7418: =pod
                   7419: 
1.648     raeburn  7420: =item * &DrawBarGraph()
1.127     matthew  7421: 
1.138     matthew  7422: Facilitates the plotting of data in a (stacked) bar graph.
                   7423: Puts plot definition data into the users environment in order for 
                   7424: graph.png to plot it.  Returns an <img> tag for the plot.
                   7425: The bars on the plot are labeled '1','2',...,'n'.
                   7426: 
                   7427: Inputs:
                   7428: 
                   7429: =over 4
                   7430: 
                   7431: =item $Title: string, the title of the plot
                   7432: 
                   7433: =item $xlabel: string, text describing the X-axis of the plot
                   7434: 
                   7435: =item $ylabel: string, text describing the Y-axis of the plot
                   7436: 
                   7437: =item $Max: scalar, the maximum Y value to use in the plot
                   7438: If $Max is < any data point, the graph will not be rendered.
                   7439: 
1.140     matthew  7440: =item $colors: array ref holding the colors to be used for the data sets when
1.138     matthew  7441: they are plotted.  If undefined, default values will be used.
                   7442: 
1.178     matthew  7443: =item $labels: array ref holding the labels to use on the x-axis for the bars.
                   7444: 
1.138     matthew  7445: =item @Values: An array of array references.  Each array reference holds data
                   7446: to be plotted in a stacked bar chart.
                   7447: 
1.239     matthew  7448: =item If the final element of @Values is a hash reference the key/value
                   7449: pairs will be added to the graph definition.
                   7450: 
1.138     matthew  7451: =back
                   7452: 
                   7453: Returns:
                   7454: 
                   7455: An <img> tag which references graph.png and the appropriate identifying
                   7456: information for the plot.
                   7457: 
1.127     matthew  7458: =cut
                   7459: 
                   7460: ############################################################
                   7461: ############################################################
1.134     matthew  7462: sub DrawBarGraph {
1.178     matthew  7463:     my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
1.134     matthew  7464:     #
                   7465:     if (! defined($colors)) {
                   7466:         $colors = ['#33ff00', 
                   7467:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
                   7468:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
                   7469:                   ]; 
                   7470:     }
1.228     matthew  7471:     my $extra_settings = {};
                   7472:     if (ref($Values[-1]) eq 'HASH') {
                   7473:         $extra_settings = pop(@Values);
                   7474:     }
1.127     matthew  7475:     #
1.136     matthew  7476:     my $identifier = &get_cgi_id();
                   7477:     my $id = 'cgi.'.$identifier;        
1.129     matthew  7478:     if (! @Values || ref($Values[0]) ne 'ARRAY') {
1.127     matthew  7479:         return '';
                   7480:     }
1.225     matthew  7481:     #
                   7482:     my @Labels;
                   7483:     if (defined($labels)) {
                   7484:         @Labels = @$labels;
                   7485:     } else {
                   7486:         for (my $i=0;$i<@{$Values[0]};$i++) {
                   7487:             push (@Labels,$i+1);
                   7488:         }
                   7489:     }
                   7490:     #
1.129     matthew  7491:     my $NumBars = scalar(@{$Values[0]});
1.225     matthew  7492:     if ($NumBars < scalar(@Labels)) { $NumBars = scalar(@Labels); }
1.129     matthew  7493:     my %ValuesHash;
                   7494:     my $NumSets=1;
                   7495:     foreach my $array (@Values) {
                   7496:         next if (! ref($array));
1.136     matthew  7497:         $ValuesHash{$id.'.data.'.$NumSets++} = 
1.132     matthew  7498:             join(',',@$array);
1.129     matthew  7499:     }
1.127     matthew  7500:     #
1.136     matthew  7501:     my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
1.225     matthew  7502:     if ($NumBars < 3) {
                   7503:         $width = 120+$NumBars*32;
1.220     matthew  7504:         $xskip = 1;
1.225     matthew  7505:         $bar_width = 30;
                   7506:     } elsif ($NumBars < 5) {
                   7507:         $width = 120+$NumBars*20;
                   7508:         $xskip = 1;
                   7509:         $bar_width = 20;
1.220     matthew  7510:     } elsif ($NumBars < 10) {
1.136     matthew  7511:         $width = 120+$NumBars*15;
                   7512:         $xskip = 1;
                   7513:         $bar_width = 15;
                   7514:     } elsif ($NumBars <= 25) {
                   7515:         $width = 120+$NumBars*11;
                   7516:         $xskip = 5;
                   7517:         $bar_width = 8;
                   7518:     } elsif ($NumBars <= 50) {
                   7519:         $width = 120+$NumBars*8;
                   7520:         $xskip = 5;
                   7521:         $bar_width = 4;
                   7522:     } else {
                   7523:         $width = 120+$NumBars*8;
                   7524:         $xskip = 5;
                   7525:         $bar_width = 4;
                   7526:     }
                   7527:     #
1.137     matthew  7528:     $Max = 1 if ($Max < 1);
                   7529:     if ( int($Max) < $Max ) {
                   7530:         $Max++;
                   7531:         $Max = int($Max);
                   7532:     }
1.127     matthew  7533:     $Title  = '' if (! defined($Title));
                   7534:     $xlabel = '' if (! defined($xlabel));
                   7535:     $ylabel = '' if (! defined($ylabel));
1.369     www      7536:     $ValuesHash{$id.'.title'}    = &escape($Title);
                   7537:     $ValuesHash{$id.'.xlabel'}   = &escape($xlabel);
                   7538:     $ValuesHash{$id.'.ylabel'}   = &escape($ylabel);
1.137     matthew  7539:     $ValuesHash{$id.'.y_max_value'} = $Max;
1.136     matthew  7540:     $ValuesHash{$id.'.NumBars'}  = $NumBars;
                   7541:     $ValuesHash{$id.'.NumSets'}  = $NumSets;
                   7542:     $ValuesHash{$id.'.PlotType'} = 'bar';
                   7543:     $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   7544:     $ValuesHash{$id.'.height'}   = $height;
                   7545:     $ValuesHash{$id.'.width'}    = $width;
                   7546:     $ValuesHash{$id.'.xskip'}    = $xskip;
                   7547:     $ValuesHash{$id.'.bar_width'} = $bar_width;
                   7548:     $ValuesHash{$id.'.labels'} = join(',',@Labels);
1.127     matthew  7549:     #
1.228     matthew  7550:     # Deal with other parameters
                   7551:     while (my ($key,$value) = each(%$extra_settings)) {
                   7552:         $ValuesHash{$id.'.'.$key} = $value;
                   7553:     }
                   7554:     #
1.646     raeburn  7555:     &Apache::lonnet::appenv(\%ValuesHash);
1.137     matthew  7556:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   7557: }
                   7558: 
                   7559: ############################################################
                   7560: ############################################################
                   7561: 
                   7562: =pod
                   7563: 
1.648     raeburn  7564: =item * &DrawXYGraph()
1.137     matthew  7565: 
1.138     matthew  7566: Facilitates the plotting of data in an XY graph.
                   7567: Puts plot definition data into the users environment in order for 
                   7568: graph.png to plot it.  Returns an <img> tag for the plot.
                   7569: 
                   7570: Inputs:
                   7571: 
                   7572: =over 4
                   7573: 
                   7574: =item $Title: string, the title of the plot
                   7575: 
                   7576: =item $xlabel: string, text describing the X-axis of the plot
                   7577: 
                   7578: =item $ylabel: string, text describing the Y-axis of the plot
                   7579: 
                   7580: =item $Max: scalar, the maximum Y value to use in the plot
                   7581: If $Max is < any data point, the graph will not be rendered.
                   7582: 
                   7583: =item $colors: Array ref containing the hex color codes for the data to be 
                   7584: plotted in.  If undefined, default values will be used.
                   7585: 
                   7586: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   7587: 
                   7588: =item $Ydata: Array ref containing Array refs.  
1.185     www      7589: Each of the contained arrays will be plotted as a separate curve.
1.138     matthew  7590: 
                   7591: =item %Values: hash indicating or overriding any default values which are 
                   7592: passed to graph.png.  
                   7593: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   7594: 
                   7595: =back
                   7596: 
                   7597: Returns:
                   7598: 
                   7599: An <img> tag which references graph.png and the appropriate identifying
                   7600: information for the plot.
                   7601: 
1.137     matthew  7602: =cut
                   7603: 
                   7604: ############################################################
                   7605: ############################################################
                   7606: sub DrawXYGraph {
                   7607:     my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
                   7608:     #
                   7609:     # Create the identifier for the graph
                   7610:     my $identifier = &get_cgi_id();
                   7611:     my $id = 'cgi.'.$identifier;
                   7612:     #
                   7613:     $Title  = '' if (! defined($Title));
                   7614:     $xlabel = '' if (! defined($xlabel));
                   7615:     $ylabel = '' if (! defined($ylabel));
                   7616:     my %ValuesHash = 
                   7617:         (
1.369     www      7618:          $id.'.title'  => &escape($Title),
                   7619:          $id.'.xlabel' => &escape($xlabel),
                   7620:          $id.'.ylabel' => &escape($ylabel),
1.137     matthew  7621:          $id.'.y_max_value'=> $Max,
                   7622:          $id.'.labels'     => join(',',@$Xlabels),
                   7623:          $id.'.PlotType'   => 'XY',
                   7624:          );
                   7625:     #
                   7626:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   7627:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   7628:     }
                   7629:     #
                   7630:     if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
                   7631:         return '';
                   7632:     }
                   7633:     my $NumSets=1;
1.138     matthew  7634:     foreach my $array (@{$Ydata}){
1.137     matthew  7635:         next if (! ref($array));
                   7636:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
                   7637:     }
1.138     matthew  7638:     $ValuesHash{$id.'.NumSets'} = $NumSets-1;
1.137     matthew  7639:     #
                   7640:     # Deal with other parameters
                   7641:     while (my ($key,$value) = each(%Values)) {
                   7642:         $ValuesHash{$id.'.'.$key} = $value;
1.127     matthew  7643:     }
                   7644:     #
1.646     raeburn  7645:     &Apache::lonnet::appenv(\%ValuesHash);
1.136     matthew  7646:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   7647: }
                   7648: 
                   7649: ############################################################
                   7650: ############################################################
                   7651: 
                   7652: =pod
                   7653: 
1.648     raeburn  7654: =item * &DrawXYYGraph()
1.138     matthew  7655: 
                   7656: Facilitates the plotting of data in an XY graph with two Y axes.
                   7657: Puts plot definition data into the users environment in order for 
                   7658: graph.png to plot it.  Returns an <img> tag for the plot.
                   7659: 
                   7660: Inputs:
                   7661: 
                   7662: =over 4
                   7663: 
                   7664: =item $Title: string, the title of the plot
                   7665: 
                   7666: =item $xlabel: string, text describing the X-axis of the plot
                   7667: 
                   7668: =item $ylabel: string, text describing the Y-axis of the plot
                   7669: 
                   7670: =item $colors: Array ref containing the hex color codes for the data to be 
                   7671: plotted in.  If undefined, default values will be used.
                   7672: 
                   7673: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   7674: 
                   7675: =item $Ydata1: The first data set
                   7676: 
                   7677: =item $Min1: The minimum value of the left Y-axis
                   7678: 
                   7679: =item $Max1: The maximum value of the left Y-axis
                   7680: 
                   7681: =item $Ydata2: The second data set
                   7682: 
                   7683: =item $Min2: The minimum value of the right Y-axis
                   7684: 
                   7685: =item $Max2: The maximum value of the left Y-axis
                   7686: 
                   7687: =item %Values: hash indicating or overriding any default values which are 
                   7688: passed to graph.png.  
                   7689: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   7690: 
                   7691: =back
                   7692: 
                   7693: Returns:
                   7694: 
                   7695: An <img> tag which references graph.png and the appropriate identifying
                   7696: information for the plot.
1.136     matthew  7697: 
                   7698: =cut
                   7699: 
                   7700: ############################################################
                   7701: ############################################################
1.137     matthew  7702: sub DrawXYYGraph {
                   7703:     my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
                   7704:                                         $Ydata2,$Min2,$Max2,%Values)=@_;
1.136     matthew  7705:     #
                   7706:     # Create the identifier for the graph
                   7707:     my $identifier = &get_cgi_id();
                   7708:     my $id = 'cgi.'.$identifier;
                   7709:     #
                   7710:     $Title  = '' if (! defined($Title));
                   7711:     $xlabel = '' if (! defined($xlabel));
                   7712:     $ylabel = '' if (! defined($ylabel));
                   7713:     my %ValuesHash = 
                   7714:         (
1.369     www      7715:          $id.'.title'  => &escape($Title),
                   7716:          $id.'.xlabel' => &escape($xlabel),
                   7717:          $id.'.ylabel' => &escape($ylabel),
1.136     matthew  7718:          $id.'.labels' => join(',',@$Xlabels),
                   7719:          $id.'.PlotType' => 'XY',
                   7720:          $id.'.NumSets' => 2,
1.137     matthew  7721:          $id.'.two_axes' => 1,
                   7722:          $id.'.y1_max_value' => $Max1,
                   7723:          $id.'.y1_min_value' => $Min1,
                   7724:          $id.'.y2_max_value' => $Max2,
                   7725:          $id.'.y2_min_value' => $Min2,
1.136     matthew  7726:          );
                   7727:     #
1.137     matthew  7728:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   7729:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   7730:     }
                   7731:     #
                   7732:     if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
                   7733:         ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
1.136     matthew  7734:         return '';
                   7735:     }
                   7736:     my $NumSets=1;
1.137     matthew  7737:     foreach my $array ($Ydata1,$Ydata2){
1.136     matthew  7738:         next if (! ref($array));
                   7739:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
1.137     matthew  7740:     }
                   7741:     #
                   7742:     # Deal with other parameters
                   7743:     while (my ($key,$value) = each(%Values)) {
                   7744:         $ValuesHash{$id.'.'.$key} = $value;
1.136     matthew  7745:     }
                   7746:     #
1.646     raeburn  7747:     &Apache::lonnet::appenv(\%ValuesHash);
1.130     albertel 7748:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
1.139     matthew  7749: }
                   7750: 
                   7751: ############################################################
                   7752: ############################################################
                   7753: 
                   7754: =pod
                   7755: 
1.157     matthew  7756: =back 
                   7757: 
1.139     matthew  7758: =head1 Statistics helper routines?  
                   7759: 
                   7760: Bad place for them but what the hell.
                   7761: 
1.157     matthew  7762: =over 4
                   7763: 
1.648     raeburn  7764: =item * &chartlink()
1.139     matthew  7765: 
                   7766: Returns a link to the chart for a specific student.  
                   7767: 
                   7768: Inputs:
                   7769: 
                   7770: =over 4
                   7771: 
                   7772: =item $linktext: The text of the link
                   7773: 
                   7774: =item $sname: The students username
                   7775: 
                   7776: =item $sdomain: The students domain
                   7777: 
                   7778: =back
                   7779: 
1.157     matthew  7780: =back
                   7781: 
1.139     matthew  7782: =cut
                   7783: 
                   7784: ############################################################
                   7785: ############################################################
                   7786: sub chartlink {
                   7787:     my ($linktext, $sname, $sdomain) = @_;
                   7788:     my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
1.369     www      7789:         '&amp;SelectedStudent='.&escape($sname.':'.$sdomain).
1.219     albertel 7790:         '&amp;chartoutputmode='.HTML::Entities::encode('html, with all links').
1.139     matthew  7791:        '">'.$linktext.'</a>';
1.153     matthew  7792: }
                   7793: 
                   7794: #######################################################
                   7795: #######################################################
                   7796: 
                   7797: =pod
                   7798: 
                   7799: =head1 Course Environment Routines
1.157     matthew  7800: 
                   7801: =over 4
1.153     matthew  7802: 
1.648     raeburn  7803: =item * &restore_course_settings()
1.153     matthew  7804: 
1.648     raeburn  7805: =item * &store_course_settings()
1.153     matthew  7806: 
                   7807: Restores/Store indicated form parameters from the course environment.
                   7808: Will not overwrite existing values of the form parameters.
                   7809: 
                   7810: Inputs: 
                   7811: a scalar describing the data (e.g. 'chart', 'problem_analysis')
                   7812: 
                   7813: a hash ref describing the data to be stored.  For example:
                   7814:    
                   7815: %Save_Parameters = ('Status' => 'scalar',
                   7816:     'chartoutputmode' => 'scalar',
                   7817:     'chartoutputdata' => 'scalar',
                   7818:     'Section' => 'array',
1.373     raeburn  7819:     'Group' => 'array',
1.153     matthew  7820:     'StudentData' => 'array',
                   7821:     'Maps' => 'array');
                   7822: 
                   7823: Returns: both routines return nothing
                   7824: 
1.631     raeburn  7825: =back
                   7826: 
1.153     matthew  7827: =cut
                   7828: 
                   7829: #######################################################
                   7830: #######################################################
                   7831: sub store_course_settings {
1.496     albertel 7832:     return &store_settings($env{'request.course.id'},@_);
                   7833: }
                   7834: 
                   7835: sub store_settings {
1.153     matthew  7836:     # save to the environment
                   7837:     # appenv the same items, just to be safe
1.300     albertel 7838:     my $udom  = $env{'user.domain'};
                   7839:     my $uname = $env{'user.name'};
1.496     albertel 7840:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  7841:     my %SaveHash;
                   7842:     my %AppHash;
                   7843:     while (my ($setting,$type) = each(%$Settings)) {
1.496     albertel 7844:         my $basename = join('.','internal',$context,$prefix,$setting);
1.300     albertel 7845:         my $envname = 'environment.'.$basename;
1.258     albertel 7846:         if (exists($env{'form.'.$setting})) {
1.153     matthew  7847:             # Save this value away
                   7848:             if ($type eq 'scalar' &&
1.258     albertel 7849:                 (! exists($env{$envname}) || 
                   7850:                  $env{$envname} ne $env{'form.'.$setting})) {
                   7851:                 $SaveHash{$basename} = $env{'form.'.$setting};
                   7852:                 $AppHash{$envname}   = $env{'form.'.$setting};
1.153     matthew  7853:             } elsif ($type eq 'array') {
                   7854:                 my $stored_form;
1.258     albertel 7855:                 if (ref($env{'form.'.$setting})) {
1.153     matthew  7856:                     $stored_form = join(',',
                   7857:                                         map {
1.369     www      7858:                                             &escape($_);
1.258     albertel 7859:                                         } sort(@{$env{'form.'.$setting}}));
1.153     matthew  7860:                 } else {
                   7861:                     $stored_form = 
1.369     www      7862:                         &escape($env{'form.'.$setting});
1.153     matthew  7863:                 }
                   7864:                 # Determine if the array contents are the same.
1.258     albertel 7865:                 if ($stored_form ne $env{$envname}) {
1.153     matthew  7866:                     $SaveHash{$basename} = $stored_form;
                   7867:                     $AppHash{$envname}   = $stored_form;
                   7868:                 }
                   7869:             }
                   7870:         }
                   7871:     }
                   7872:     my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
1.300     albertel 7873:                                           $udom,$uname);
1.153     matthew  7874:     if ($put_result !~ /^(ok|delayed)/) {
                   7875:         &Apache::lonnet::logthis('unable to save form parameters, '.
                   7876:                                  'got error:'.$put_result);
                   7877:     }
                   7878:     # Make sure these settings stick around in this session, too
1.646     raeburn  7879:     &Apache::lonnet::appenv(\%AppHash);
1.153     matthew  7880:     return;
                   7881: }
                   7882: 
                   7883: sub restore_course_settings {
1.499     albertel 7884:     return &restore_settings($env{'request.course.id'},@_);
1.496     albertel 7885: }
                   7886: 
                   7887: sub restore_settings {
                   7888:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  7889:     while (my ($setting,$type) = each(%$Settings)) {
1.258     albertel 7890:         next if (exists($env{'form.'.$setting}));
1.496     albertel 7891:         my $envname = 'environment.internal.'.$context.'.'.$prefix.
1.153     matthew  7892:             '.'.$setting;
1.258     albertel 7893:         if (exists($env{$envname})) {
1.153     matthew  7894:             if ($type eq 'scalar') {
1.258     albertel 7895:                 $env{'form.'.$setting} = $env{$envname};
1.153     matthew  7896:             } elsif ($type eq 'array') {
1.258     albertel 7897:                 $env{'form.'.$setting} = [ 
1.153     matthew  7898:                                            map { 
1.369     www      7899:                                                &unescape($_); 
1.258     albertel 7900:                                            } split(',',$env{$envname})
1.153     matthew  7901:                                            ];
                   7902:             }
                   7903:         }
                   7904:     }
1.127     matthew  7905: }
                   7906: 
1.618     raeburn  7907: #######################################################
                   7908: #######################################################
                   7909: 
                   7910: =pod
                   7911: 
                   7912: =head1 Domain E-mail Routines  
                   7913: 
                   7914: =over 4
                   7915: 
1.648     raeburn  7916: =item * &build_recipient_list()
1.618     raeburn  7917: 
                   7918: Build recipient lists for three types of e-mail:
                   7919: (a) Error Reports, (b) Package Updates, (c) Help requests, generated by
1.619     raeburn  7920: lonerrorhandler.pm, CHECKRPMS and lonsupportreq.pm respectively.
1.618     raeburn  7921: 
                   7922: Inputs:
1.619     raeburn  7923: defmail (scalar - email address of default recipient), 
1.618     raeburn  7924: mailing type (scalar - errormail, packagesmail, or helpdeskmail), 
1.619     raeburn  7925: defdom (domain for which to retrieve configuration settings),
                   7926: origmail (scalar - email address of recipient from loncapa.conf, 
                   7927: i.e., predates configuration by DC via domainprefs.pm 
1.618     raeburn  7928: 
                   7929: Returns: comma separated list of addresses to which to send e-mail.   
                   7930: 
                   7931: =cut
                   7932: 
                   7933: ############################################################
                   7934: ############################################################
                   7935: sub build_recipient_list {
1.619     raeburn  7936:     my ($defmail,$mailing,$defdom,$origmail) = @_;
1.618     raeburn  7937:     my @recipients;
                   7938:     my $otheremails;
                   7939:     my %domconfig =
                   7940:          &Apache::lonnet::get_dom('configuration',['contacts'],$defdom);
                   7941:     if (ref($domconfig{'contacts'}) eq 'HASH') {
                   7942:         if (ref($domconfig{'contacts'}{$mailing}) eq 'HASH') {
                   7943:             my @contacts = ('adminemail','supportemail');
                   7944:             foreach my $item (@contacts) {
                   7945:                 if ($domconfig{'contacts'}{$mailing}{$item}) {
1.619     raeburn  7946:                     my $addr = $domconfig{'contacts'}{$item}; 
                   7947:                     if (!grep(/^\Q$addr\E$/,@recipients)) {
                   7948:                         push(@recipients,$addr);
                   7949:                     }
1.618     raeburn  7950:                 }
                   7951:                 $otheremails = $domconfig{'contacts'}{$mailing}{'others'};
                   7952:             }
                   7953:         }
1.619     raeburn  7954:     } elsif ($origmail ne '') {
                   7955:         push(@recipients,$origmail);
1.618     raeburn  7956:     }
                   7957:     if ($defmail ne '') {
                   7958:         push(@recipients,$defmail);
                   7959:     }
                   7960:     if ($otheremails) {
1.619     raeburn  7961:         my @others;
                   7962:         if ($otheremails =~ /,/) {
                   7963:             @others = split(/,/,$otheremails);
1.618     raeburn  7964:         } else {
1.619     raeburn  7965:             push(@others,$otheremails);
                   7966:         }
                   7967:         foreach my $addr (@others) {
                   7968:             if (!grep(/^\Q$addr\E$/,@recipients)) {
                   7969:                 push(@recipients,$addr);
                   7970:             }
1.618     raeburn  7971:         }
                   7972:     }
1.619     raeburn  7973:     my $recipientlist = join(',',@recipients); 
1.618     raeburn  7974:     return $recipientlist;
                   7975: }
                   7976: 
1.127     matthew  7977: ############################################################
                   7978: ############################################################
1.154     albertel 7979: 
1.443     albertel 7980: sub commit_customrole {
                   7981:     my ($udom,$uname,$url,$three,$four,$five,$start,$end) = @_;
1.630     raeburn  7982:     my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.':'.$three.' in '.$url.
1.443     albertel 7983:                          ($start?', '.&mt('starting').' '.localtime($start):'').
                   7984:                          ($end?', ending '.localtime($end):'').': <b>'.
                   7985:               &Apache::lonnet::assigncustomrole(
                   7986:                  $udom,$uname,$url,$three,$four,$five,$end,$start).
                   7987:                  '</b><br />';
                   7988:     return $output;
                   7989: }
                   7990: 
                   7991: sub commit_standardrole {
1.541     raeburn  7992:     my ($udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
                   7993:     my ($output,$logmsg,$linefeed);
                   7994:     if ($context eq 'auto') {
                   7995:         $linefeed = "\n";
                   7996:     } else {
                   7997:         $linefeed = "<br />\n";
                   7998:     }  
1.443     albertel 7999:     if ($three eq 'st') {
1.541     raeburn  8000:         my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$three,$start,$end,
                   8001:                                          $one,$two,$sec,$context);
                   8002:         if (($result =~ /^error/) || ($result eq 'not_in_class') || 
1.626     raeburn  8003:             ($result eq 'unknown_course') || ($result eq 'refused')) {
                   8004:             $output = $logmsg.' '.&mt('Error: ').$result."\n"; 
1.443     albertel 8005:         } else {
1.541     raeburn  8006:             $output = $logmsg.$linefeed.&mt('Assigning').' '.$three.' in '.$url.
1.443     albertel 8007:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  8008:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
                   8009:             if ($context eq 'auto') {
                   8010:                 $output .= $result.$linefeed.&mt('Add to classlist').': ok';
                   8011:             } else {
                   8012:                $output .= '<b>'.$result.'</b>'.$linefeed.
                   8013:                &mt('Add to classlist').': <b>ok</b>';
                   8014:             }
                   8015:             $output .= $linefeed;
1.443     albertel 8016:         }
                   8017:     } else {
                   8018:         $output = &mt('Assigning').' '.$three.' in '.$url.
                   8019:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  8020:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
1.652   ! raeburn  8021:         my $result = &Apache::lonnet::assignrole($udom,$uname,$url,$three,$end,$start,'','',$context);
1.541     raeburn  8022:         if ($context eq 'auto') {
                   8023:             $output .= $result.$linefeed;
                   8024:         } else {
                   8025:             $output .= '<b>'.$result.'</b>'.$linefeed;
                   8026:         }
1.443     albertel 8027:     }
                   8028:     return $output;
                   8029: }
                   8030: 
                   8031: sub commit_studentrole {
1.541     raeburn  8032:     my ($logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
1.626     raeburn  8033:     my ($result,$linefeed,$oldsecurl,$newsecurl);
1.541     raeburn  8034:     if ($context eq 'auto') {
                   8035:         $linefeed = "\n";
                   8036:     } else {
                   8037:         $linefeed = '<br />'."\n";
                   8038:     }
1.443     albertel 8039:     if (defined($one) && defined($two)) {
                   8040:         my $cid=$one.'_'.$two;
                   8041:         my $oldsec=&Apache::lonnet::getsection($udom,$uname,$cid);
                   8042:         my $secchange = 0;
                   8043:         my $expire_role_result;
                   8044:         my $modify_section_result;
1.628     raeburn  8045:         if ($oldsec ne '-1') { 
                   8046:             if ($oldsec ne $sec) {
1.443     albertel 8047:                 $secchange = 1;
1.628     raeburn  8048:                 my $now = time;
1.443     albertel 8049:                 my $uurl='/'.$cid;
                   8050:                 $uurl=~s/\_/\//g;
                   8051:                 if ($oldsec) {
                   8052:                     $uurl.='/'.$oldsec;
                   8053:                 }
1.626     raeburn  8054:                 $oldsecurl = $uurl;
1.628     raeburn  8055:                 $expire_role_result = 
1.652   ! raeburn  8056:                     &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',$now,'','',$context);
1.628     raeburn  8057:                 if ($env{'request.course.sec'} ne '') { 
                   8058:                     if ($expire_role_result eq 'refused') {
                   8059:                         my @roles = ('st');
                   8060:                         my @statuses = ('previous');
                   8061:                         my @roledoms = ($one);
                   8062:                         my $withsec = 1;
                   8063:                         my %roleshash = 
                   8064:                             &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   8065:                                               \@statuses,\@roles,\@roledoms,$withsec);
                   8066:                         if (defined ($roleshash{$two.':'.$one.':st:'.$oldsec})) {
                   8067:                             my ($oldstart,$oldend) = 
                   8068:                                 split(':',$roleshash{$two.':'.$one.':st:'.$oldsec});
                   8069:                             if ($oldend > 0 && $oldend <= $now) {
                   8070:                                 $expire_role_result = 'ok';
                   8071:                             }
                   8072:                         }
                   8073:                     }
                   8074:                 }
1.443     albertel 8075:                 $result = $expire_role_result;
                   8076:             }
                   8077:         }
                   8078:         if (($expire_role_result eq 'ok') || ($secchange == 0)) {
1.652   ! raeburn  8079:             $modify_section_result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,'','',$cid,'',$context);
1.443     albertel 8080:             if ($modify_section_result =~ /^ok/) {
                   8081:                 if ($secchange == 1) {
1.628     raeburn  8082:                     if ($sec eq '') {
                   8083:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to student role without a section.',$uname,$oldsec).$linefeed;
                   8084:                     } else {
                   8085:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to new section: [_3].',$uname,$oldsec,$sec).$linefeed;
                   8086:                     }
1.443     albertel 8087:                 } elsif ($oldsec eq '-1') {
1.628     raeburn  8088:                     if ($sec eq '') {
                   8089:                         $$logmsg .= &mt('New student role without a section for [_1] in course [_2].',$uname,$cid).$linefeed;
                   8090:                     } else {
                   8091:                         $$logmsg .= &mt('New student role for [_1] in section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
                   8092:                     }
1.443     albertel 8093:                 } else {
1.628     raeburn  8094:                     if ($sec eq '') {
                   8095:                         $$logmsg .= &mt('Student [_1] assigned to course [_2] without a section.',$uname,$cid).$linefeed;
                   8096:                     } else {
                   8097:                         $$logmsg .= &mt('Student [_1] assigned to section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
                   8098:                     }
1.443     albertel 8099:                 }
                   8100:             } else {
1.628     raeburn  8101:                 if ($secchange) {       
                   8102:                     $$logmsg .= &mt('Error when attempting section change for [_1] from old section "[_2]" to new section: "[_3]" in course [_4] -error:',$uname,$oldsec,$sec,$cid).' '.$modify_section_result.$linefeed;
                   8103:                 } else {
                   8104:                     $$logmsg .= &mt('Error when attempting to modify role for [_1] for section: "[_2]" in course [_3] -error:',$uname,$sec,$cid).' '.$modify_section_result.$linefeed;
                   8105:                 }
1.443     albertel 8106:             }
                   8107:             $result = $modify_section_result;
                   8108:         } elsif ($secchange == 1) {
1.628     raeburn  8109:             if ($oldsec eq '') {
                   8110:                 $$logmsg .= &mt('Error when attempting to expire existing role without a section for [_1] in course [_3] -error: ',$uname,$cid).' '.$expire_role_result.$linefeed;
                   8111:             } else {
                   8112:                 $$logmsg .= &mt('Error when attempting to expire existing role for [_1] in section [_2] in course [_3] -error: ',$uname,$oldsec,$cid).' '.$expire_role_result.$linefeed;
                   8113:             }
1.626     raeburn  8114:             if ($expire_role_result eq 'refused') {
                   8115:                 my $newsecurl = '/'.$cid;
                   8116:                 $newsecurl =~ s/\_/\//g;
                   8117:                 if ($sec ne '') {
                   8118:                     $newsecurl.='/'.$sec;
                   8119:                 }
                   8120:                 if (&Apache::lonnet::allowed('cst',$newsecurl) && !(&Apache::lonnet::allowed('cst',$oldsecurl))) {
                   8121:                     if ($sec eq '') {
                   8122:                         $$logmsg .= &mt('Although your current role has privileges to add students to section "[_1]", you do not have privileges to modify existing enrollments unaffiliated with any section.',$sec).$linefeed;
                   8123:                     } else {
                   8124:                         $$logmsg .= &mt('Although your current role has privileges to add students to section "[_1]", you do not have privileges to modify existing enrollments in other sections.',$sec).$linefeed;
                   8125:                     }
                   8126:                 }
                   8127:             }
1.443     albertel 8128:         }
                   8129:     } else {
1.626     raeburn  8130:         $$logmsg .= &mt('Incomplete course id defined.').$linefeed.&mt('Addition of user [_1] from domain [_2] to course [_3], section [_4] not completed.',$uname,$udom,$one.'_'.$two,$sec).$linefeed;
1.443     albertel 8131:         $result = "error: incomplete course id\n";
                   8132:     }
                   8133:     return $result;
                   8134: }
                   8135: 
                   8136: ############################################################
                   8137: ############################################################
                   8138: 
1.566     albertel 8139: sub check_clone {
1.578     raeburn  8140:     my ($args,$linefeed) = @_;
1.566     albertel 8141:     my $cloneid='/'.$args->{'clonedomain'}.'/'.$args->{'clonecourse'};
                   8142:     my ($clonecrsudom,$clonecrsunum)= &LONCAPA::split_courseid($cloneid);
                   8143:     my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
                   8144:     my $clonemsg;
                   8145:     my $can_clone = 0;
                   8146: 
                   8147:     if ($clonehome eq 'no_host') {
1.578     raeburn  8148:         $clonemsg = &mt('No new course created.').$linefeed.&mt('A new course could not be cloned from the specified original - [_1] - because it is a non-existent course.',$args->{'clonecourse'}.':'.$args->{'clonedomain'});     
1.566     albertel 8149:     } else {
                   8150: 	my %clonedesc = &Apache::lonnet::coursedescription($cloneid,{'one_time' => 1});
1.568     albertel 8151: 	if ($env{'request.role.domain'} eq $args->{'clonedomain'}) {
1.566     albertel 8152: 	    $can_clone = 1;
                   8153: 	} else {
                   8154: 	    my %clonehash = &Apache::lonnet::get('environment',['cloners'],
                   8155: 						 $args->{'clonedomain'},$args->{'clonecourse'});
                   8156: 	    my @cloners = split(/,/,$clonehash{'cloners'});
1.578     raeburn  8157:             if (grep(/^\*$/,@cloners)) {
                   8158:                 $can_clone = 1;
                   8159:             } elsif (grep(/^\*\:\Q$args->{'ccdomain'}\E$/,@cloners)) {
                   8160:                 $can_clone = 1;
                   8161:             } else {
                   8162: 	        my %roleshash =
                   8163: 		    &Apache::lonnet::get_my_roles($args->{'ccuname'},
                   8164: 					 $args->{'ccdomain'},
                   8165:                                          'userroles',['active'],['cc'],
                   8166: 					 [$args->{'clonedomain'}]);
                   8167: 	        if (($roleshash{$args->{'clonecourse'}.':'.$args->{'clonedomain'}.':cc'}) || (grep(/^\Q$args->{'ccuname'}\E:\Q$args->{'ccdomain'}\E$/,@cloners))) {
                   8168: 		    $can_clone = 1;
                   8169: 	        } else {
                   8170:                     $clonemsg = &mt('No new course created.').$linefeed.&mt('The new course could not be cloned from the existing course because the new course owner ([_1]) does not have cloning rights in the existing course ([_2]).',$args->{'ccuname'}.':'.$args->{'ccdomain'},$clonedesc{'description'});
                   8171: 	        }
1.566     albertel 8172: 	    }
1.578     raeburn  8173:         }
1.566     albertel 8174:     }
                   8175:     return ($can_clone, $clonemsg, $cloneid, $clonehome);
                   8176: }
                   8177: 
1.444     albertel 8178: sub construct_course {
1.541     raeburn  8179:     my ($args,$logmsg,$courseid,$crsudom,$crsunum,$udom,$uname,$context) = @_;
1.444     albertel 8180:     my $outcome;
1.541     raeburn  8181:     my $linefeed =  '<br />'."\n";
                   8182:     if ($context eq 'auto') {
                   8183:         $linefeed = "\n";
                   8184:     }
1.566     albertel 8185: 
                   8186: #
                   8187: # Are we cloning?
                   8188: #
                   8189:     my ($can_clone, $clonemsg, $cloneid, $clonehome);
                   8190:     if (($args->{'clonecourse'}) && ($args->{'clonedomain'})) {
1.578     raeburn  8191: 	($can_clone, $clonemsg, $cloneid, $clonehome) = &check_clone($args,$linefeed);
1.566     albertel 8192: 	if ($context ne 'auto') {
1.578     raeburn  8193:             if ($clonemsg ne '') {
                   8194: 	        $clonemsg = '<span class="LC_error">'.$clonemsg.'</span>';
                   8195:             }
1.566     albertel 8196: 	}
                   8197: 	$outcome .= $clonemsg.$linefeed;
                   8198: 
                   8199:         if (!$can_clone) {
                   8200: 	    return (0,$outcome);
                   8201: 	}
                   8202:     }
                   8203: 
1.444     albertel 8204: #
                   8205: # Open course
                   8206: #
                   8207:     my $crstype = lc($args->{'crstype'});
                   8208:     my %cenv=();
                   8209:     $$courseid=&Apache::lonnet::createcourse($args->{'course_domain'},
                   8210:                                              $args->{'cdescr'},
                   8211:                                              $args->{'curl'},
                   8212:                                              $args->{'course_home'},
                   8213:                                              $args->{'nonstandard'},
                   8214:                                              $args->{'crscode'},
                   8215:                                              $args->{'ccuname'}.':'.
                   8216:                                              $args->{'ccdomain'},
                   8217:                                              $args->{'crstype'});
                   8218: 
                   8219:     # Note: The testing routines depend on this being output; see 
                   8220:     # Utils::Course. This needs to at least be output as a comment
                   8221:     # if anyone ever decides to not show this, and Utils::Course::new
                   8222:     # will need to be suitably modified.
1.541     raeburn  8223:     $outcome .= &mt('New LON-CAPA [_1] ID: [_2]',$crstype,$$courseid).$linefeed;
1.444     albertel 8224: #
                   8225: # Check if created correctly
                   8226: #
1.479     albertel 8227:     ($$crsudom,$$crsunum)= &LONCAPA::split_courseid($$courseid);
1.444     albertel 8228:     my $crsuhome=&Apache::lonnet::homeserver($$crsunum,$$crsudom);
1.541     raeburn  8229:     $outcome .= &mt('Created on').': '.$crsuhome.$linefeed;
1.566     albertel 8230: 
1.444     albertel 8231: #
1.566     albertel 8232: # Do the cloning
                   8233: #   
                   8234:     if ($can_clone && $cloneid) {
                   8235: 	$clonemsg = &mt('Cloning [_1] from [_2]',$crstype,$clonehome);
                   8236: 	if ($context ne 'auto') {
                   8237: 	    $clonemsg = '<span class="LC_success">'.$clonemsg.'</span>';
                   8238: 	}
                   8239: 	$outcome .= $clonemsg.$linefeed;
                   8240: 	my %oldcenv=&Apache::lonnet::dump('environment',$$crsudom,$$crsunum);
1.444     albertel 8241: # Copy all files
1.637     www      8242: 	&Apache::lonclonecourse::copycoursefiles($cloneid,$$courseid,$args->{'datemode'},$args->{'dateshift'});
1.444     albertel 8243: # Restore URL
1.566     albertel 8244: 	$cenv{'url'}=$oldcenv{'url'};
1.444     albertel 8245: # Restore title
1.566     albertel 8246: 	$cenv{'description'}=$oldcenv{'description'};
1.444     albertel 8247: # Mark as cloned
1.566     albertel 8248: 	$cenv{'clonedfrom'}=$cloneid;
1.638     www      8249: # Need to clone grading mode
                   8250:         my %newenv=&Apache::lonnet::get('environment',['grading'],$$crsudom,$$crsunum);
                   8251:         $cenv{'grading'}=$newenv{'grading'};
                   8252: # Do not clone these environment entries
                   8253:         &Apache::lonnet::del('environment',
                   8254:                   ['default_enrollment_start_date',
                   8255:                    'default_enrollment_end_date',
                   8256:                    'question.email',
                   8257:                    'policy.email',
                   8258:                    'comment.email',
                   8259:                    'pch.users.denied',
                   8260:                    'plc.users.denied'],
                   8261:                    $$crsudom,$$crsunum);
1.444     albertel 8262:     }
1.566     albertel 8263: 
1.444     albertel 8264: #
                   8265: # Set environment (will override cloned, if existing)
                   8266: #
                   8267:     my @sections = ();
                   8268:     my @xlists = ();
                   8269:     if ($args->{'crstype'}) {
                   8270:         $cenv{'type'}=$args->{'crstype'};
                   8271:     }
                   8272:     if ($args->{'crsid'}) {
                   8273:         $cenv{'courseid'}=$args->{'crsid'};
                   8274:     }
                   8275:     if ($args->{'crscode'}) {
                   8276:         $cenv{'internal.coursecode'}=$args->{'crscode'};
                   8277:     }
                   8278:     if ($args->{'crsquota'} ne '') {
                   8279:         $cenv{'internal.coursequota'}=$args->{'crsquota'};
                   8280:     } else {
                   8281:         $cenv{'internal.coursequota'}=$args->{'crsquota'} = 20;
                   8282:     }
                   8283:     if ($args->{'ccuname'}) {
                   8284:         $cenv{'internal.courseowner'} = $args->{'ccuname'}.
                   8285:                                         ':'.$args->{'ccdomain'};
                   8286:     } else {
                   8287:         $cenv{'internal.courseowner'} = $args->{'curruser'};
                   8288:     }
                   8289:     my @badclasses = (); # Used to accumulate sections/crosslistings that did not pass classlist access check for course owner.
                   8290:     if ($args->{'crssections'}) {
                   8291:         $cenv{'internal.sectionnums'} = '';
                   8292:         if ($args->{'crssections'} =~ m/,/) {
                   8293:             @sections = split/,/,$args->{'crssections'};
                   8294:         } else {
                   8295:             $sections[0] = $args->{'crssections'};
                   8296:         }
                   8297:         if (@sections > 0) {
                   8298:             foreach my $item (@sections) {
                   8299:                 my ($sec,$gp) = split/:/,$item;
                   8300:                 my $class = $args->{'crscode'}.$sec;
                   8301:                 my $addcheck = &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$class,$cenv{'internal.courseowner'});
                   8302:                 $cenv{'internal.sectionnums'} .= $item.',';
                   8303:                 unless ($addcheck eq 'ok') {
                   8304:                     push @badclasses, $class;
                   8305:                 }
                   8306:             }
                   8307:             $cenv{'internal.sectionnums'} =~ s/,$//;
                   8308:         }
                   8309:     }
                   8310: # do not hide course coordinator from staff listing, 
                   8311: # even if privileged
                   8312:     $cenv{'nothideprivileged'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   8313: # add crosslistings
                   8314:     if ($args->{'crsxlist'}) {
                   8315:         $cenv{'internal.crosslistings'}='';
                   8316:         if ($args->{'crsxlist'} =~ m/,/) {
                   8317:             @xlists = split/,/,$args->{'crsxlist'};
                   8318:         } else {
                   8319:             $xlists[0] = $args->{'crsxlist'};
                   8320:         }
                   8321:         if (@xlists > 0) {
                   8322:             foreach my $item (@xlists) {
                   8323:                 my ($xl,$gp) = split/:/,$item;
                   8324:                 my $addcheck =  &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$xl,$cenv{'internal.courseowner'});
                   8325:                 $cenv{'internal.crosslistings'} .= $item.',';
                   8326:                 unless ($addcheck eq 'ok') {
                   8327:                     push @badclasses, $xl;
                   8328:                 }
                   8329:             }
                   8330:             $cenv{'internal.crosslistings'} =~ s/,$//;
                   8331:         }
                   8332:     }
                   8333:     if ($args->{'autoadds'}) {
                   8334:         $cenv{'internal.autoadds'}=$args->{'autoadds'};
                   8335:     }
                   8336:     if ($args->{'autodrops'}) {
                   8337:         $cenv{'internal.autodrops'}=$args->{'autodrops'};
                   8338:     }
                   8339: # check for notification of enrollment changes
                   8340:     my @notified = ();
                   8341:     if ($args->{'notify_owner'}) {
                   8342:         if ($args->{'ccuname'} ne '') {
                   8343:             push(@notified,$args->{'ccuname'}.':'.$args->{'ccdomain'});
                   8344:         }
                   8345:     }
                   8346:     if ($args->{'notify_dc'}) {
                   8347:         if ($uname ne '') { 
1.630     raeburn  8348:             push(@notified,$uname.':'.$udom);
1.444     albertel 8349:         }
                   8350:     }
                   8351:     if (@notified > 0) {
                   8352:         my $notifylist;
                   8353:         if (@notified > 1) {
                   8354:             $notifylist = join(',',@notified);
                   8355:         } else {
                   8356:             $notifylist = $notified[0];
                   8357:         }
                   8358:         $cenv{'internal.notifylist'} = $notifylist;
                   8359:     }
                   8360:     if (@badclasses > 0) {
                   8361:         my %lt=&Apache::lonlocal::texthash(
                   8362:                 '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',
                   8363:                 'dnhr' => 'does not have rights to access enrollment in these classes',
                   8364:                 'adby' => 'as determined by the policies of your institution on access to official classlists'
                   8365:         );
1.541     raeburn  8366:         my $badclass_msg = $cenv{'internal.courseowner'}.') - '.$lt{'dnhr'}.
                   8367:                            ' ('.$lt{'adby'}.')';
                   8368:         if ($context eq 'auto') {
                   8369:             $outcome .= $badclass_msg.$linefeed;
1.566     albertel 8370:             $outcome .= '<div class="LC_warning">'.$badclass_msg.$linefeed.'<ul>'."\n";
1.541     raeburn  8371:             foreach my $item (@badclasses) {
                   8372:                 if ($context eq 'auto') {
                   8373:                     $outcome .= " - $item\n";
                   8374:                 } else {
                   8375:                     $outcome .= "<li>$item</li>\n";
                   8376:                 }
                   8377:             }
                   8378:             if ($context eq 'auto') {
                   8379:                 $outcome .= $linefeed;
                   8380:             } else {
1.566     albertel 8381:                 $outcome .= "</ul><br /><br /></div>\n";
1.541     raeburn  8382:             }
                   8383:         } 
1.444     albertel 8384:     }
                   8385:     if ($args->{'no_end_date'}) {
                   8386:         $args->{'endaccess'} = 0;
                   8387:     }
                   8388:     $cenv{'internal.autostart'}=$args->{'enrollstart'};
                   8389:     $cenv{'internal.autoend'}=$args->{'enrollend'};
                   8390:     $cenv{'default_enrollment_start_date'}=$args->{'startaccess'};
                   8391:     $cenv{'default_enrollment_end_date'}=$args->{'endaccess'};
                   8392:     if ($args->{'showphotos'}) {
                   8393:       $cenv{'internal.showphotos'}=$args->{'showphotos'};
                   8394:     }
                   8395:     $cenv{'internal.authtype'} = $args->{'authtype'};
                   8396:     $cenv{'internal.autharg'} = $args->{'autharg'}; 
                   8397:     if ( ($cenv{'internal.authtype'} =~ /^krb/) && ($cenv{'internal.autoadds'} == 1)) {
                   8398:         if (! defined($cenv{'internal.autharg'}) || $cenv{'internal.autharg'}  eq '') {
1.541     raeburn  8399:             my $krb_msg = &mt('As you did not include the default Kerberos domain to be used for authentication in this class, the institutional data used by the automated enrollment process must include the Kerberos domain for each new student'); 
                   8400:             if ($context eq 'auto') {
                   8401:                 $outcome .= $krb_msg;
                   8402:             } else {
1.566     albertel 8403:                 $outcome .= '<span class="LC_error">'.$krb_msg.'</span>';
1.541     raeburn  8404:             }
                   8405:             $outcome .= $linefeed;
1.444     albertel 8406:         }
                   8407:     }
                   8408:     if (($args->{'ccdomain'}) && ($args->{'ccuname'})) {
                   8409:        if ($args->{'setpolicy'}) {
                   8410:            $cenv{'policy.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   8411:        }
                   8412:        if ($args->{'setcontent'}) {
                   8413:            $cenv{'question.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   8414:        }
                   8415:     }
                   8416:     if ($args->{'reshome'}) {
                   8417: 	$cenv{'reshome'}=$args->{'reshome'}.'/';
                   8418: 	$cenv{'reshome'}=~s/\/+$/\//;
                   8419:     }
                   8420: #
                   8421: # course has keyed access
                   8422: #
                   8423:     if ($args->{'setkeys'}) {
                   8424:        $cenv{'keyaccess'}='yes';
                   8425:     }
                   8426: # if specified, key authority is not course, but user
                   8427: # only active if keyaccess is yes
                   8428:     if ($args->{'keyauth'}) {
1.487     albertel 8429: 	my ($user,$domain) = split(':',$args->{'keyauth'});
                   8430: 	$user = &LONCAPA::clean_username($user);
                   8431: 	$domain = &LONCAPA::clean_username($domain);
1.488     foxr     8432: 	if ($user ne '' && $domain ne '') {
1.487     albertel 8433: 	    $cenv{'keyauth'}=$user.':'.$domain;
1.444     albertel 8434: 	}
                   8435:     }
                   8436: 
                   8437:     if ($args->{'disresdis'}) {
                   8438:         $cenv{'pch.roles.denied'}='st';
                   8439:     }
                   8440:     if ($args->{'disablechat'}) {
                   8441:         $cenv{'plc.roles.denied'}='st';
                   8442:     }
                   8443: 
                   8444:     # Record we've not yet viewed the Course Initialization Helper for this 
                   8445:     # course
                   8446:     $cenv{'course.helper.not.run'} = 1;
                   8447:     #
                   8448:     # Use new Randomseed
                   8449:     #
                   8450:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
                   8451:     $cenv{'receiptalg'}=&Apache::lonnet::latest_receipt_algorithm_id();;
                   8452:     #
                   8453:     # The encryption code and receipt prefix for this course
                   8454:     #
                   8455:     $cenv{'internal.encseed'}=$Apache::lonnet::perlvar{'lonReceipt'}.$$.time.int(rand(9999));
                   8456:     $cenv{'internal.encpref'}=100+int(9*rand(99));
                   8457:     #
                   8458:     # By default, use standard grading
                   8459:     if (!defined($cenv{'grading'})) { $cenv{'grading'} = 'standard'; }
                   8460: 
1.541     raeburn  8461:     $outcome .= $linefeed.&mt('Setting environment').': '.                 
                   8462:           &Apache::lonnet::put('environment',\%cenv,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 8463: #
                   8464: # Open all assignments
                   8465: #
                   8466:     if ($args->{'openall'}) {
                   8467:        my $storeunder=$$crsudom.'_'.$$crsunum.'.0.opendate';
                   8468:        my %storecontent = ($storeunder         => time,
                   8469:                            $storeunder.'.type' => 'date_start');
                   8470:        
                   8471:        $outcome .= &mt('Opening all assignments').': '.&Apache::lonnet::cput
1.541     raeburn  8472:                  ('resourcedata',\%storecontent,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 8473:    }
                   8474: #
                   8475: # Set first page
                   8476: #
                   8477:     unless (($args->{'nonstandard'}) || ($args->{'firstres'} eq 'blank')
                   8478: 	    || ($cloneid)) {
1.445     albertel 8479: 	use LONCAPA::map;
1.444     albertel 8480: 	$outcome .= &mt('Setting first resource').': ';
1.445     albertel 8481: 
                   8482: 	my $map = '/uploaded/'.$$crsudom.'/'.$$crsunum.'/default.sequence';
                   8483:         my ($errtext,$fatal)=&LONCAPA::map::mapread($map);
                   8484: 
1.444     albertel 8485:         $outcome .= ($fatal?$errtext:'read ok').' - ';
                   8486:         my $title; my $url;
                   8487:         if ($args->{'firstres'} eq 'syl') {
                   8488: 	    $title='Syllabus';
                   8489:             $url='/public/'.$$crsudom.'/'.$$crsunum.'/syllabus';
                   8490:         } else {
                   8491:             $title='Navigate Contents';
                   8492:             $url='/adm/navmaps';
                   8493:         }
1.445     albertel 8494: 
                   8495:         $LONCAPA::map::resources[1]=$title.':'.$url.':false:start:res';
                   8496: 	(my $outtext,$errtext) = &LONCAPA::map::storemap($map,1);
                   8497: 
                   8498: 	if ($errtext) { $fatal=2; }
1.541     raeburn  8499:         $outcome .= ($fatal?$errtext:'write ok').$linefeed;
1.444     albertel 8500:     }
1.566     albertel 8501: 
                   8502:     return (1,$outcome);
1.444     albertel 8503: }
                   8504: 
                   8505: ############################################################
                   8506: ############################################################
                   8507: 
1.378     raeburn  8508: sub course_type {
                   8509:     my ($cid) = @_;
                   8510:     if (!defined($cid)) {
                   8511:         $cid = $env{'request.course.id'};
                   8512:     }
1.404     albertel 8513:     if (defined($env{'course.'.$cid.'.type'})) {
                   8514:         return $env{'course.'.$cid.'.type'};
1.378     raeburn  8515:     } else {
                   8516:         return 'Course';
1.377     raeburn  8517:     }
                   8518: }
1.156     albertel 8519: 
1.406     raeburn  8520: sub group_term {
                   8521:     my $crstype = &course_type();
                   8522:     my %names = (
                   8523:                   'Course' => 'group',
                   8524:                   'Group' => 'team',
                   8525:                 );
                   8526:     return $names{$crstype};
                   8527: }
                   8528: 
1.156     albertel 8529: sub icon {
                   8530:     my ($file)=@_;
1.505     albertel 8531:     my $curfext = lc((split(/\./,$file))[-1]);
1.168     albertel 8532:     my $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/unknown.gif';
1.156     albertel 8533:     my $embstyle = &Apache::loncommon::fileembstyle($curfext);
1.168     albertel 8534:     if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) {
                   8535: 	if (-e  $Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
                   8536: 	          $Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   8537: 	            $curfext.".gif") {
                   8538: 	    $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   8539: 		$curfext.".gif";
                   8540: 	}
                   8541:     }
1.249     albertel 8542:     return &lonhttpdurl($iconname);
1.154     albertel 8543: } 
1.84      albertel 8544: 
1.575     albertel 8545: sub lonhttpd_port {
1.215     albertel 8546:     my $lonhttpd_port=$Apache::lonnet::perlvar{'lonhttpdPort'};
                   8547:     if (!defined($lonhttpd_port)) { $lonhttpd_port='8080'; }
1.574     albertel 8548:     # IE doesn't like a secure page getting images from a non-secure
                   8549:     # port (when logging we haven't parsed the browser type so default
                   8550:     # back to secure
                   8551:     if ((!exists($env{'browser.type'}) || $env{'browser.type'} eq 'explorer')
                   8552: 	&& $ENV{'SERVER_PORT'} == 443) {
1.575     albertel 8553: 	return 443;
                   8554:     }
                   8555:     return $lonhttpd_port;
                   8556: 
                   8557: }
                   8558: 
                   8559: sub lonhttpdurl {
                   8560:     my ($url)=@_;
                   8561: 
                   8562:     my $lonhttpd_port = &lonhttpd_port();
                   8563:     if ($lonhttpd_port == 443) {
1.574     albertel 8564: 	return 'https://'.$ENV{'SERVER_NAME'}.$url;
                   8565:     }
1.215     albertel 8566:     return 'http://'.$ENV{'SERVER_NAME'}.':'.$lonhttpd_port.$url;
                   8567: }
                   8568: 
1.213     albertel 8569: sub connection_aborted {
                   8570:     my ($r)=@_;
                   8571:     $r->print(" ");$r->rflush();
                   8572:     my $c = $r->connection;
                   8573:     return $c->aborted();
                   8574: }
                   8575: 
1.221     foxr     8576: #    Escapes strings that may have embedded 's that will be put into
1.222     foxr     8577: #    strings as 'strings'.
                   8578: sub escape_single {
1.221     foxr     8579:     my ($input) = @_;
1.223     albertel 8580:     $input =~ s/\\/\\\\/g;	# Escape the \'s..(must be first)>
1.221     foxr     8581:     $input =~ s/\'/\\\'/g;	# Esacpe the 's....
                   8582:     return $input;
                   8583: }
1.223     albertel 8584: 
1.222     foxr     8585: #  Same as escape_single, but escape's "'s  This 
                   8586: #  can be used for  "strings"
                   8587: sub escape_double {
                   8588:     my ($input) = @_;
                   8589:     $input =~ s/\\/\\\\/g;	# Escape the /'s..(must be first)>
                   8590:     $input =~ s/\"/\\\"/g;	# Esacpe the "s....
                   8591:     return $input;
                   8592: }
1.223     albertel 8593:  
1.222     foxr     8594: #   Escapes the last element of a full URL.
                   8595: sub escape_url {
                   8596:     my ($url)   = @_;
1.238     raeburn  8597:     my @urlslices = split(/\//, $url,-1);
1.369     www      8598:     my $lastitem = &escape(pop(@urlslices));
1.223     albertel 8599:     return join('/',@urlslices).'/'.$lastitem;
1.222     foxr     8600: }
1.462     albertel 8601: 
                   8602: # -------------------------------------------------------- Initliaze user login
                   8603: sub init_user_environment {
1.463     albertel 8604:     my ($r, $username, $domain, $authhost, $form, $args) = @_;
1.462     albertel 8605:     my $lonids=$Apache::lonnet::perlvar{'lonIDsDir'};
                   8606: 
                   8607:     my $public=($username eq 'public' && $domain eq 'public');
                   8608: 
                   8609: # See if old ID present, if so, remove
                   8610: 
                   8611:     my ($filename,$cookie,$userroles);
                   8612:     my $now=time;
                   8613: 
                   8614:     if ($public) {
                   8615: 	my $max_public=100;
                   8616: 	my $oldest;
                   8617: 	my $oldest_time=0;
                   8618: 	for(my $next=1;$next<=$max_public;$next++) {
                   8619: 	    if (-e $lonids."/publicuser_$next.id") {
                   8620: 		my $mtime=(stat($lonids."/publicuser_$next.id"))[9];
                   8621: 		if ($mtime<$oldest_time || !$oldest_time) {
                   8622: 		    $oldest_time=$mtime;
                   8623: 		    $oldest=$next;
                   8624: 		}
                   8625: 	    } else {
                   8626: 		$cookie="publicuser_$next";
                   8627: 		last;
                   8628: 	    }
                   8629: 	}
                   8630: 	if (!$cookie) { $cookie="publicuser_$oldest"; }
                   8631:     } else {
1.463     albertel 8632: 	# if this isn't a robot, kill any existing non-robot sessions
                   8633: 	if (!$args->{'robot'}) {
                   8634: 	    opendir(DIR,$lonids);
                   8635: 	    while ($filename=readdir(DIR)) {
                   8636: 		if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
                   8637: 		    unlink($lonids.'/'.$filename);
                   8638: 		}
1.462     albertel 8639: 	    }
1.463     albertel 8640: 	    closedir(DIR);
1.462     albertel 8641: 	}
                   8642: # Give them a new cookie
1.463     albertel 8643: 	my $id = ($args->{'robot'} ? 'robot'.$args->{'robot'}
                   8644: 		                   : $now);
                   8645: 	$cookie="$username\_$id\_$domain\_$authhost";
1.462     albertel 8646:     
                   8647: # Initialize roles
                   8648: 
                   8649: 	$userroles=&Apache::lonnet::rolesinit($domain,$username,$authhost);
                   8650:     }
                   8651: # ------------------------------------ Check browser type and MathML capability
                   8652: 
                   8653:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                   8654:         $clientunicode,$clientos) = &decode_user_agent($r);
                   8655: 
                   8656: # -------------------------------------- Any accessibility options to remember?
                   8657:     if (($form->{'interface'}) && ($form->{'remember'} eq 'true')) {
                   8658: 	foreach my $option ('imagesuppress','appletsuppress',
                   8659: 			    'embedsuppress','fontenhance','blackwhite') {
                   8660: 	    if ($form->{$option} eq 'true') {
                   8661: 		&Apache::lonnet::put('environment',{$option => 'on'},
                   8662: 				     $domain,$username);
                   8663: 	    } else {
                   8664: 		&Apache::lonnet::del('environment',[$option],
                   8665: 				     $domain,$username);
                   8666: 	    }
                   8667: 	}
                   8668:     }
                   8669: # ------------------------------------------------------------- Get environment
                   8670: 
                   8671:     my %userenv = &Apache::lonnet::dump('environment',$domain,$username);
                   8672:     my ($tmp) = keys(%userenv);
                   8673:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   8674: 	# default remote control to off
                   8675: 	if ($userenv{'remote'} ne 'on') { $userenv{'remote'} = 'off'; }
                   8676:     } else {
                   8677: 	undef(%userenv);
                   8678:     }
                   8679:     if (($userenv{'interface'}) && (!$form->{'interface'})) {
                   8680: 	$form->{'interface'}=$userenv{'interface'};
                   8681:     }
                   8682:     $env{'environment.remote'}=$userenv{'remote'};
                   8683:     if ($userenv{'texengine'} eq 'ttm') { $clientmathml=1; }
                   8684: 
                   8685: # --------------- Do not trust query string to be put directly into environment
                   8686:     foreach my $option ('imagesuppress','appletsuppress',
                   8687: 			'embedsuppress','fontenhance','blackwhite',
                   8688: 			'interface','localpath','localres') {
                   8689: 	$form->{$option}=~s/[\n\r\=]//gs;
                   8690:     }
                   8691: # --------------------------------------------------------- Write first profile
                   8692: 
                   8693:     {
                   8694: 	my %initial_env = 
                   8695: 	    ("user.name"          => $username,
                   8696: 	     "user.domain"        => $domain,
                   8697: 	     "user.home"          => $authhost,
                   8698: 	     "browser.type"       => $clientbrowser,
                   8699: 	     "browser.version"    => $clientversion,
                   8700: 	     "browser.mathml"     => $clientmathml,
                   8701: 	     "browser.unicode"    => $clientunicode,
                   8702: 	     "browser.os"         => $clientos,
                   8703: 	     "server.domain"      => $Apache::lonnet::perlvar{'lonDefDomain'},
                   8704: 	     "request.course.fn"  => '',
                   8705: 	     "request.course.uri" => '',
                   8706: 	     "request.course.sec" => '',
                   8707: 	     "request.role"       => 'cm',
                   8708: 	     "request.role.adv"   => $env{'user.adv'},
                   8709: 	     "request.host"       => $ENV{'REMOTE_ADDR'},);
                   8710: 
                   8711:         if ($form->{'localpath'}) {
                   8712: 	    $initial_env{"browser.localpath"}  = $form->{'localpath'};
                   8713: 	    $initial_env{"browser.localres"}   = $form->{'localres'};
                   8714:         }
                   8715: 	
                   8716: 	if ($public) {
                   8717: 	    $initial_env{"environment.remote"} = "off";
                   8718: 	}
                   8719: 	if ($form->{'interface'}) {
                   8720: 	    $form->{'interface'}=~s/\W//gs;
                   8721: 	    $initial_env{"browser.interface"} = $form->{'interface'};
                   8722: 	    $env{'browser.interface'}=$form->{'interface'};
                   8723: 	    foreach my $option ('imagesuppress','appletsuppress',
                   8724: 				'embedsuppress','fontenhance','blackwhite') {
                   8725: 		if (($form->{$option} eq 'true') ||
                   8726: 		    ($userenv{$option} eq 'on')) {
                   8727: 		    $initial_env{"browser.$option"} = "on";
                   8728: 		}
                   8729: 	    }
                   8730: 	}
                   8731: 
                   8732: 	$env{'user.environment'} = "$lonids/$cookie.id";
                   8733: 	
                   8734: 	if (tie(my %disk_env,'GDBM_File',"$lonids/$cookie.id",
                   8735: 		 &GDBM_WRCREAT(),0640)) {
                   8736: 	    &_add_to_env(\%disk_env,\%initial_env);
                   8737: 	    &_add_to_env(\%disk_env,\%userenv,'environment.');
                   8738: 	    &_add_to_env(\%disk_env,$userroles);
1.463     albertel 8739: 	    if (ref($args->{'extra_env'})) {
                   8740: 		&_add_to_env(\%disk_env,$args->{'extra_env'});
                   8741: 	    }
1.462     albertel 8742: 	    untie(%disk_env);
                   8743: 	} else {
                   8744: 	    &Apache::lonnet::logthis("<font color=\"blue\">WARNING: ".
                   8745: 			   'Could not create environment storage in lonauth: '.$!.'</font>');
                   8746: 	    return 'error: '.$!;
                   8747: 	}
                   8748:     }
                   8749:     $env{'request.role'}='cm';
                   8750:     $env{'request.role.adv'}=$env{'user.adv'};
                   8751:     $env{'browser.type'}=$clientbrowser;
                   8752: 
                   8753:     return $cookie;
                   8754: 
                   8755: }
                   8756: 
                   8757: sub _add_to_env {
                   8758:     my ($idf,$env_data,$prefix) = @_;
                   8759:     while (my ($key,$value) = each(%$env_data)) {
                   8760: 	$idf->{$prefix.$key} = $value;
                   8761: 	$env{$prefix.$key}   = $value;
                   8762:     }
                   8763: }
                   8764: 
                   8765: 
1.41      ng       8766: =pod
                   8767: 
                   8768: =back
                   8769: 
1.112     bowersj2 8770: =cut
1.41      ng       8771: 
1.112     bowersj2 8772: 1;
                   8773: __END__;
1.41      ng       8774: 

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