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

1.10      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # a pile of common routines
1.10      albertel    3: #
1.1075.2.35! raeburn     4: # $Id: loncommon.pm,v 1.1075.2.34 2013/04/30 13:25:45 raeburn Exp $
1.10      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.1       albertel   28: 
                     29: # Makes a table out of the previous attempts
1.2       albertel   30: # Inputs result_from_symbread, user, domain, course_id
1.16      harris41   31: # Reads in non-network-related .tab files
1.1       albertel   32: 
1.35      matthew    33: # POD header:
                     34: 
1.45      matthew    35: =pod
                     36: 
1.35      matthew    37: =head1 NAME
                     38: 
                     39: Apache::loncommon - pile of common routines
                     40: 
                     41: =head1 SYNOPSIS
                     42: 
1.112     bowersj2   43: Common routines for manipulating connections, student answers,
                     44:     domains, common Javascript fragments, etc.
1.35      matthew    45: 
1.112     bowersj2   46: =head1 OVERVIEW
1.35      matthew    47: 
1.112     bowersj2   48: A collection of commonly used subroutines that don't have a natural
                     49: home anywhere else. This collection helps remove
1.35      matthew    50: redundancy from other modules and increase efficiency of memory usage.
                     51: 
                     52: =cut 
                     53: 
                     54: # End of POD header
1.1       albertel   55: package Apache::loncommon;
                     56: 
                     57: use strict;
1.258     albertel   58: use Apache::lonnet;
1.46      matthew    59: use GDBM_File;
1.51      www        60: use POSIX qw(strftime mktime);
1.82      www        61: use Apache::lonmenu();
1.498     albertel   62: use Apache::lonenc();
1.117     www        63: use Apache::lonlocal;
1.685     tempelho   64: use Apache::lonnet();
1.139     matthew    65: use HTML::Entities;
1.334     albertel   66: use Apache::lonhtmlcommon();
                     67: use Apache::loncoursedata();
1.344     albertel   68: use Apache::lontexconvert();
1.444     albertel   69: use Apache::lonclonecourse();
1.1075.2.25  raeburn    70: use Apache::lonuserutils();
1.1075.2.27  raeburn    71: use Apache::lonuserstate();
1.479     albertel   72: use LONCAPA qw(:DEFAULT :match);
1.657     raeburn    73: use DateTime::TimeZone;
1.687     raeburn    74: use DateTime::Locale::Catalog;
1.1075.2.14  raeburn    75: use Authen::Captcha;
                     76: use Captcha::reCAPTCHA;
1.117     www        77: 
1.517     raeburn    78: # ---------------------------------------------- Designs
                     79: use vars qw(%defaultdesign);
                     80: 
1.22      www        81: my $readit;
                     82: 
1.517     raeburn    83: 
1.157     matthew    84: ##
                     85: ## Global Variables
                     86: ##
1.46      matthew    87: 
1.643     foxr       88: 
                     89: # ----------------------------------------------- SSI with retries:
                     90: #
                     91: 
                     92: =pod
                     93: 
1.648     raeburn    94: =head1 Server Side include with retries:
1.643     foxr       95: 
                     96: =over 4
                     97: 
1.648     raeburn    98: =item * &ssi_with_retries(resource,retries form)
1.643     foxr       99: 
                    100: Performs an ssi with some number of retries.  Retries continue either
                    101: until the result is ok or until the retry count supplied by the
                    102: caller is exhausted.  
                    103: 
                    104: Inputs:
1.648     raeburn   105: 
                    106: =over 4
                    107: 
1.643     foxr      108: resource   - Identifies the resource to insert.
1.648     raeburn   109: 
1.643     foxr      110: retries    - Count of the number of retries allowed.
1.648     raeburn   111: 
1.643     foxr      112: form       - Hash that identifies the rendering options.
                    113: 
1.648     raeburn   114: =back
                    115: 
                    116: Returns:
                    117: 
                    118: =over 4
                    119: 
1.643     foxr      120: content    - The content of the response.  If retries were exhausted this is empty.
1.648     raeburn   121: 
1.643     foxr      122: response   - The response from the last attempt (which may or may not have been successful.
                    123: 
1.648     raeburn   124: =back
                    125: 
                    126: =back
                    127: 
1.643     foxr      128: =cut
                    129: 
                    130: sub ssi_with_retries {
                    131:     my ($resource, $retries, %form) = @_;
                    132: 
                    133: 
                    134:     my $ok = 0;			# True if we got a good response.
                    135:     my $content;
                    136:     my $response;
                    137: 
                    138:     # Try to get the ssi done. within the retries count:
                    139: 
                    140:     do {
                    141: 	($content, $response) = &Apache::lonnet::ssi($resource, %form);
                    142: 	$ok      = $response->is_success;
1.650     www       143:         if (!$ok) {
                    144:             &Apache::lonnet::logthis("Failed ssi_with_retries on $resource: ".$response->is_success.', '.$response->code.', '.$response->message);
                    145:         }
1.643     foxr      146: 	$retries--;
                    147:     } while (!$ok && ($retries > 0));
                    148: 
                    149:     if (!$ok) {
                    150: 	$content = '';		# On error return an empty content.
                    151:     }
                    152:     return ($content, $response);
                    153: 
                    154: }
                    155: 
                    156: 
                    157: 
1.20      www       158: # ----------------------------------------------- Filetypes/Languages/Copyright
1.12      harris41  159: my %language;
1.124     www       160: my %supported_language;
1.1048    foxr      161: my %latex_language;		# For choosing hyphenation in <transl..>
                    162: my %latex_language_bykey;	# for choosing hyphenation from metadata
1.12      harris41  163: my %cprtag;
1.192     taceyjo1  164: my %scprtag;
1.351     www       165: my %fe; my %fd; my %fm;
1.41      ng        166: my %category_extensions;
1.12      harris41  167: 
1.46      matthew   168: # ---------------------------------------------- Thesaurus variables
1.144     matthew   169: #
                    170: # %Keywords:
                    171: #      A hash used by &keyword to determine if a word is considered a keyword.
                    172: # $thesaurus_db_file 
                    173: #      Scalar containing the full path to the thesaurus database.
1.46      matthew   174: 
                    175: my %Keywords;
                    176: my $thesaurus_db_file;
                    177: 
1.144     matthew   178: #
                    179: # Initialize values from language.tab, copyright.tab, filetypes.tab,
                    180: # thesaurus.tab, and filecategories.tab.
                    181: #
1.18      www       182: BEGIN {
1.46      matthew   183:     # Variable initialization
                    184:     $thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
                    185:     #
1.22      www       186:     unless ($readit) {
1.12      harris41  187: # ------------------------------------------------------------------- languages
                    188:     {
1.158     raeburn   189:         my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    190:                                    '/language.tab';
                    191:         if ( open(my $fh,"<$langtabfile") ) {
1.356     albertel  192:             while (my $line = <$fh>) {
                    193:                 next if ($line=~/^\#/);
                    194:                 chomp($line);
1.1048    foxr      195:                 my ($key,$two,$country,$three,$enc,$val,$sup,$latex)=(split(/\t/,$line));
1.158     raeburn   196:                 $language{$key}=$val.' - '.$enc;
                    197:                 if ($sup) {
                    198:                     $supported_language{$key}=$sup;
                    199:                 }
1.1048    foxr      200: 		if ($latex) {
                    201: 		    $latex_language_bykey{$key} = $latex;
                    202: 		    $latex_language{$two} = $latex;
                    203: 		}
1.158     raeburn   204:             }
                    205:             close($fh);
                    206:         }
1.12      harris41  207:     }
                    208: # ------------------------------------------------------------------ copyrights
                    209:     {
1.158     raeburn   210:         my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    211:                                   '/copyright.tab';
                    212:         if ( open (my $fh,"<$copyrightfile") ) {
1.356     albertel  213:             while (my $line = <$fh>) {
                    214:                 next if ($line=~/^\#/);
                    215:                 chomp($line);
                    216:                 my ($key,$val)=(split(/\s+/,$line,2));
1.158     raeburn   217:                 $cprtag{$key}=$val;
                    218:             }
                    219:             close($fh);
                    220:         }
1.12      harris41  221:     }
1.351     www       222: # ----------------------------------------------------------- source copyrights
1.192     taceyjo1  223:     {
                    224:         my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    225:                                   '/source_copyright.tab';
                    226:         if ( open (my $fh,"<$sourcecopyrightfile") ) {
1.356     albertel  227:             while (my $line = <$fh>) {
                    228:                 next if ($line =~ /^\#/);
                    229:                 chomp($line);
                    230:                 my ($key,$val)=(split(/\s+/,$line,2));
1.192     taceyjo1  231:                 $scprtag{$key}=$val;
                    232:             }
                    233:             close($fh);
                    234:         }
                    235:     }
1.63      www       236: 
1.517     raeburn   237: # -------------------------------------------------------------- default domain designs
1.63      www       238:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
1.517     raeburn   239:     my $designfile = $designdir.'/default.tab';
                    240:     if ( open (my $fh,"<$designfile") ) {
                    241:         while (my $line = <$fh>) {
                    242:             next if ($line =~ /^\#/);
                    243:             chomp($line);
                    244:             my ($key,$val)=(split(/\=/,$line));
                    245:             if ($val) { $defaultdesign{$key}=$val; }
                    246:         }
                    247:         close($fh);
1.63      www       248:     }
                    249: 
1.15      harris41  250: # ------------------------------------------------------------- file categories
                    251:     {
1.158     raeburn   252:         my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    253:                                   '/filecategories.tab';
                    254:         if ( open (my $fh,"<$categoryfile") ) {
1.356     albertel  255: 	    while (my $line = <$fh>) {
                    256: 		next if ($line =~ /^\#/);
                    257: 		chomp($line);
                    258:                 my ($extension,$category)=(split(/\s+/,$line,2));
1.158     raeburn   259:                 push @{$category_extensions{lc($category)}},$extension;
                    260:             }
                    261:             close($fh);
                    262:         }
                    263: 
1.15      harris41  264:     }
1.12      harris41  265: # ------------------------------------------------------------------ file types
                    266:     {
1.158     raeburn   267:         my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    268:                '/filetypes.tab';
                    269:         if ( open (my $fh,"<$typesfile") ) {
1.356     albertel  270:             while (my $line = <$fh>) {
                    271: 		next if ($line =~ /^\#/);
                    272: 		chomp($line);
                    273:                 my ($ending,$emb,$mime,$descr)=split(/\s+/,$line,4);
1.158     raeburn   274:                 if ($descr ne '') {
                    275:                     $fe{$ending}=lc($emb);
                    276:                     $fd{$ending}=$descr;
1.351     www       277:                     if ($mime ne 'unk') { $fm{$ending}=$mime; }
1.158     raeburn   278:                 }
                    279:             }
                    280:             close($fh);
                    281:         }
1.12      harris41  282:     }
1.22      www       283:     &Apache::lonnet::logthis(
1.705     tempelho  284:              "<span style='color:yellow;'>INFO: Read file types</span>");
1.22      www       285:     $readit=1;
1.46      matthew   286:     }  # end of unless($readit) 
1.32      matthew   287:     
                    288: }
1.112     bowersj2  289: 
1.42      matthew   290: ###############################################################
                    291: ##           HTML and Javascript Helper Functions            ##
                    292: ###############################################################
                    293: 
                    294: =pod 
                    295: 
1.112     bowersj2  296: =head1 HTML and Javascript Functions
1.42      matthew   297: 
1.112     bowersj2  298: =over 4
                    299: 
1.648     raeburn   300: =item * &browser_and_searcher_javascript()
1.112     bowersj2  301: 
                    302: X<browsing, javascript>X<searching, javascript>Returns a string
                    303: containing javascript with two functions, C<openbrowser> and
                    304: C<opensearcher>. Returned string does not contain E<lt>scriptE<gt>
                    305: tags.
1.42      matthew   306: 
1.648     raeburn   307: =item * &openbrowser(formname,elementname,only,omit) [javascript]
1.42      matthew   308: 
                    309: inputs: formname, elementname, only, omit
                    310: 
                    311: formname and elementname indicate the name of the html form and name of
                    312: the element that the results of the browsing selection are to be placed in. 
                    313: 
                    314: Specifying 'only' will restrict the browser to displaying only files
1.185     www       315: with the given extension.  Can be a comma separated list.
1.42      matthew   316: 
                    317: Specifying 'omit' will restrict the browser to NOT displaying files
1.185     www       318: with the given extension.  Can be a comma separated list.
1.42      matthew   319: 
1.648     raeburn   320: =item * &opensearcher(formname,elementname) [javascript]
1.42      matthew   321: 
                    322: Inputs: formname, elementname
                    323: 
                    324: formname and elementname specify the name of the html form and the name
                    325: of the element the selection from the search results will be placed in.
1.542     raeburn   326: 
1.42      matthew   327: =cut
                    328: 
                    329: sub browser_and_searcher_javascript {
1.199     albertel  330:     my ($mode)=@_;
                    331:     if (!defined($mode)) { $mode='edit'; }
1.453     albertel  332:     my $resurl=&escape_single(&lastresurl());
1.42      matthew   333:     return <<END;
1.219     albertel  334: // <!-- BEGIN LON-CAPA Internal
1.50      matthew   335:     var editbrowser = null;
1.135     albertel  336:     function openbrowser(formname,elementname,only,omit,titleelement) {
1.170     www       337:         var url = '$resurl/?';
1.42      matthew   338:         if (editbrowser == null) {
                    339:             url += 'launch=1&';
                    340:         }
                    341:         url += 'catalogmode=interactive&';
1.199     albertel  342:         url += 'mode=$mode&';
1.611     albertel  343:         url += 'inhibitmenu=yes&';
1.42      matthew   344:         url += 'form=' + formname + '&';
                    345:         if (only != null) {
                    346:             url += 'only=' + only + '&';
1.217     albertel  347:         } else {
                    348:             url += 'only=&';
                    349: 	}
1.42      matthew   350:         if (omit != null) {
                    351:             url += 'omit=' + omit + '&';
1.217     albertel  352:         } else {
                    353:             url += 'omit=&';
                    354: 	}
1.135     albertel  355:         if (titleelement != null) {
                    356:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  357:         } else {
                    358: 	    url += 'titleelement=&';
                    359: 	}
1.42      matthew   360:         url += 'element=' + elementname + '';
                    361:         var title = 'Browser';
1.435     albertel  362:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
1.42      matthew   363:         options += ',width=700,height=600';
                    364:         editbrowser = open(url,title,options,'1');
                    365:         editbrowser.focus();
                    366:     }
                    367:     var editsearcher;
1.135     albertel  368:     function opensearcher(formname,elementname,titleelement) {
1.42      matthew   369:         var url = '/adm/searchcat?';
                    370:         if (editsearcher == null) {
                    371:             url += 'launch=1&';
                    372:         }
                    373:         url += 'catalogmode=interactive&';
1.199     albertel  374:         url += 'mode=$mode&';
1.42      matthew   375:         url += 'form=' + formname + '&';
1.135     albertel  376:         if (titleelement != null) {
                    377:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  378:         } else {
                    379: 	    url += 'titleelement=&';
                    380: 	}
1.42      matthew   381:         url += 'element=' + elementname + '';
                    382:         var title = 'Search';
1.435     albertel  383:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
1.42      matthew   384:         options += ',width=700,height=600';
                    385:         editsearcher = open(url,title,options,'1');
                    386:         editsearcher.focus();
                    387:     }
1.219     albertel  388: // END LON-CAPA Internal -->
1.42      matthew   389: END
1.170     www       390: }
                    391: 
                    392: sub lastresurl {
1.258     albertel  393:     if ($env{'environment.lastresurl'}) {
                    394: 	return $env{'environment.lastresurl'}
1.170     www       395:     } else {
                    396: 	return '/res';
                    397:     }
                    398: }
                    399: 
                    400: sub storeresurl {
                    401:     my $resurl=&Apache::lonnet::clutter(shift);
                    402:     unless ($resurl=~/^\/res/) { return 0; }
                    403:     $resurl=~s/\/$//;
                    404:     &Apache::lonnet::put('environment',{'lastresurl' => $resurl});
1.646     raeburn   405:     &Apache::lonnet::appenv({'environment.lastresurl' => $resurl});
1.170     www       406:     return 1;
1.42      matthew   407: }
                    408: 
1.74      www       409: sub studentbrowser_javascript {
1.111     www       410:    unless (
1.258     albertel  411:             (($env{'request.course.id'}) && 
1.302     albertel  412:              (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    413: 	      || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    414: 					  '/'.$env{'request.course.sec'})
                    415: 	      ))
1.258     albertel  416:          || ($env{'request.role'}=~/^(au|dc|su)/)
1.111     www       417:           ) { return ''; }  
1.74      www       418:    return (<<'ENDSTDBRW');
1.776     bisitz    419: <script type="text/javascript" language="Javascript">
1.824     bisitz    420: // <![CDATA[
1.74      www       421:     var stdeditbrowser;
1.999     www       422:     function openstdbrowser(formname,uname,udom,clicker,roleflag,ignorefilter,courseadvonly) {
1.74      www       423:         var url = '/adm/pickstudent?';
                    424:         var filter;
1.558     albertel  425: 	if (!ignorefilter) {
                    426: 	    eval('filter=document.'+formname+'.'+uname+'.value;');
                    427: 	}
1.74      www       428:         if (filter != null) {
                    429:            if (filter != '') {
                    430:                url += 'filter='+filter+'&';
                    431: 	   }
                    432:         }
                    433:         url += 'form=' + formname + '&unameelement='+uname+
1.999     www       434:                                     '&udomelement='+udom+
                    435:                                     '&clicker='+clicker;
1.111     www       436: 	if (roleflag) { url+="&roles=1"; }
1.793     raeburn   437:         if (courseadvonly) { url+="&courseadvonly=1"; }
1.102     www       438:         var title = 'Student_Browser';
1.74      www       439:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    440:         options += ',width=700,height=600';
                    441:         stdeditbrowser = open(url,title,options,'1');
                    442:         stdeditbrowser.focus();
                    443:     }
1.824     bisitz    444: // ]]>
1.74      www       445: </script>
                    446: ENDSTDBRW
                    447: }
1.42      matthew   448: 
1.1003    www       449: sub resourcebrowser_javascript {
                    450:    unless ($env{'request.course.id'}) { return ''; }
1.1004    www       451:    return (<<'ENDRESBRW');
1.1003    www       452: <script type="text/javascript" language="Javascript">
                    453: // <![CDATA[
                    454:     var reseditbrowser;
1.1004    www       455:     function openresbrowser(formname,reslink) {
1.1005    www       456:         var url = '/adm/pickresource?form='+formname+'&reslink='+reslink;
1.1003    www       457:         var title = 'Resource_Browser';
                    458:         var options = 'scrollbars=1,resizable=1,menubar=0';
1.1005    www       459:         options += ',width=700,height=500';
1.1004    www       460:         reseditbrowser = open(url,title,options,'1');
                    461:         reseditbrowser.focus();
1.1003    www       462:     }
                    463: // ]]>
                    464: </script>
1.1004    www       465: ENDRESBRW
1.1003    www       466: }
                    467: 
1.74      www       468: sub selectstudent_link {
1.999     www       469:    my ($form,$unameele,$udomele,$courseadvonly,$clickerid)=@_;
                    470:    my $callargs = "'".&Apache::lonhtmlcommon::entity_encode($form)."','".
                    471:                       &Apache::lonhtmlcommon::entity_encode($unameele)."','".
                    472:                       &Apache::lonhtmlcommon::entity_encode($udomele)."'";
1.258     albertel  473:    if ($env{'request.course.id'}) {  
1.302     albertel  474:        if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    475: 	   && !&Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    476: 					'/'.$env{'request.course.sec'})) {
1.111     www       477: 	   return '';
                    478:        }
1.999     www       479:        $callargs.=",'".&Apache::lonhtmlcommon::entity_encode($clickerid)."'";
1.793     raeburn   480:        if ($courseadvonly)  {
                    481:            $callargs .= ",'',1,1";
                    482:        }
                    483:        return '<span class="LC_nobreak">'.
                    484:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
                    485:               &mt('Select User').'</a></span>';
1.74      www       486:    }
1.258     albertel  487:    if ($env{'request.role'}=~/^(au|dc|su)/) {
1.1012    www       488:        $callargs .= ",'',1"; 
1.793     raeburn   489:        return '<span class="LC_nobreak">'.
                    490:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
                    491:               &mt('Select User').'</a></span>';
1.111     www       492:    }
                    493:    return '';
1.91      www       494: }
                    495: 
1.1004    www       496: sub selectresource_link {
                    497:    my ($form,$reslink,$arg)=@_;
                    498:    
                    499:    my $callargs = "'".&Apache::lonhtmlcommon::entity_encode($form)."','".
                    500:                       &Apache::lonhtmlcommon::entity_encode($reslink)."'";
                    501:    unless ($env{'request.course.id'}) { return $arg; }
                    502:    return '<span class="LC_nobreak">'.
                    503:               '<a href="javascript:openresbrowser('.$callargs.');">'.
                    504:               $arg.'</a></span>';
                    505: }
                    506: 
                    507: 
                    508: 
1.653     raeburn   509: sub authorbrowser_javascript {
                    510:     return <<"ENDAUTHORBRW";
1.776     bisitz    511: <script type="text/javascript" language="JavaScript">
1.824     bisitz    512: // <![CDATA[
1.653     raeburn   513: var stdeditbrowser;
                    514: 
                    515: function openauthorbrowser(formname,udom) {
                    516:     var url = '/adm/pickauthor?';
                    517:     url += 'form='+formname+'&roledom='+udom;
                    518:     var title = 'Author_Browser';
                    519:     var options = 'scrollbars=1,resizable=1,menubar=0';
                    520:     options += ',width=700,height=600';
                    521:     stdeditbrowser = open(url,title,options,'1');
                    522:     stdeditbrowser.focus();
                    523: }
                    524: 
1.824     bisitz    525: // ]]>
1.653     raeburn   526: </script>
                    527: ENDAUTHORBRW
                    528: }
                    529: 
1.91      www       530: sub coursebrowser_javascript {
1.1075.2.31  raeburn   531:     my ($domainfilter,$sec_element,$formname,$role_element,$crstype,
                    532:         $credits_element) = @_;
1.932     raeburn   533:     my $wintitle = 'Course_Browser';
1.931     raeburn   534:     if ($crstype eq 'Community') {
1.932     raeburn   535:         $wintitle = 'Community_Browser';
1.909     raeburn   536:     }
1.876     raeburn   537:     my $id_functions = &javascript_index_functions();
                    538:     my $output = '
1.776     bisitz    539: <script type="text/javascript" language="JavaScript">
1.824     bisitz    540: // <![CDATA[
1.468     raeburn   541:     var stdeditbrowser;'."\n";
1.876     raeburn   542: 
                    543:     $output .= <<"ENDSTDBRW";
1.909     raeburn   544:     function opencrsbrowser(formname,uname,udom,desc,extra_element,multflag,type,type_elem) {
1.91      www       545:         var url = '/adm/pickcourse?';
1.895     raeburn   546:         var formid = getFormIdByName(formname);
1.876     raeburn   547:         var domainfilter = getDomainFromSelectbox(formname,udom);
1.128     albertel  548:         if (domainfilter != null) {
                    549:            if (domainfilter != '') {
                    550:                url += 'domainfilter='+domainfilter+'&';
                    551: 	   }
                    552:         }
1.91      www       553:         url += 'form=' + formname + '&cnumelement='+uname+
1.187     albertel  554: 	                            '&cdomelement='+udom+
                    555:                                     '&cnameelement='+desc;
1.468     raeburn   556:         if (extra_element !=null && extra_element != '') {
1.594     raeburn   557:             if (formname == 'rolechoice' || formname == 'studentform') {
1.468     raeburn   558:                 url += '&roleelement='+extra_element;
                    559:                 if (domainfilter == null || domainfilter == '') {
                    560:                     url += '&domainfilter='+extra_element;
                    561:                 }
1.234     raeburn   562:             }
1.468     raeburn   563:             else {
                    564:                 if (formname == 'portform') {
                    565:                     url += '&setroles='+extra_element;
1.800     raeburn   566:                 } else {
                    567:                     if (formname == 'rules') {
                    568:                         url += '&fixeddom='+extra_element; 
                    569:                     }
1.468     raeburn   570:                 }
                    571:             }     
1.230     raeburn   572:         }
1.909     raeburn   573:         if (type != null && type != '') {
                    574:             url += '&type='+type;
                    575:         }
                    576:         if (type_elem != null && type_elem != '') {
                    577:             url += '&typeelement='+type_elem;
                    578:         }
1.872     raeburn   579:         if (formname == 'ccrs') {
                    580:             var ownername = document.forms[formid].ccuname.value;
                    581:             var ownerdom =  document.forms[formid].ccdomain.options[document.forms[formid].ccdomain.selectedIndex].value;
                    582:             url += '&cloner='+ownername+':'+ownerdom;
                    583:         }
1.293     raeburn   584:         if (multflag !=null && multflag != '') {
                    585:             url += '&multiple='+multflag;
                    586:         }
1.909     raeburn   587:         var title = '$wintitle';
1.91      www       588:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    589:         options += ',width=700,height=600';
                    590:         stdeditbrowser = open(url,title,options,'1');
                    591:         stdeditbrowser.focus();
                    592:     }
1.876     raeburn   593: $id_functions
                    594: ENDSTDBRW
1.1075.2.31  raeburn   595:     if (($sec_element ne '') || ($role_element ne '') || ($credits_element ne '')) {
                    596:         $output .= &setsec_javascript($sec_element,$formname,$role_element,
                    597:                                       $credits_element);
1.876     raeburn   598:     }
                    599:     $output .= '
                    600: // ]]>
                    601: </script>';
                    602:     return $output;
                    603: }
                    604: 
                    605: sub javascript_index_functions {
                    606:     return <<"ENDJS";
                    607: 
                    608: function getFormIdByName(formname) {
                    609:     for (var i=0;i<document.forms.length;i++) {
                    610:         if (document.forms[i].name == formname) {
                    611:             return i;
                    612:         }
                    613:     }
                    614:     return -1;
                    615: }
                    616: 
                    617: function getIndexByName(formid,item) {
                    618:     for (var i=0;i<document.forms[formid].elements.length;i++) {
                    619:         if (document.forms[formid].elements[i].name == item) {
                    620:             return i;
                    621:         }
                    622:     }
                    623:     return -1;
                    624: }
1.468     raeburn   625: 
1.876     raeburn   626: function getDomainFromSelectbox(formname,udom) {
                    627:     var userdom;
                    628:     var formid = getFormIdByName(formname);
                    629:     if (formid > -1) {
                    630:         var domid = getIndexByName(formid,udom);
                    631:         if (domid > -1) {
                    632:             if (document.forms[formid].elements[domid].type == 'select-one') {
                    633:                 userdom=document.forms[formid].elements[domid].options[document.forms[formid].elements[domid].selectedIndex].value;
                    634:             }
                    635:             if (document.forms[formid].elements[domid].type == 'hidden') {
                    636:                 userdom=document.forms[formid].elements[domid].value;
1.468     raeburn   637:             }
                    638:         }
                    639:     }
1.876     raeburn   640:     return userdom;
                    641: }
                    642: 
                    643: ENDJS
1.468     raeburn   644: 
1.876     raeburn   645: }
                    646: 
1.1017    raeburn   647: sub javascript_array_indexof {
1.1018    raeburn   648:     return <<ENDJS;
1.1017    raeburn   649: <script type="text/javascript" language="JavaScript">
                    650: // <![CDATA[
                    651: 
                    652: if (!Array.prototype.indexOf) {
                    653:     Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
                    654:         "use strict";
                    655:         if (this === void 0 || this === null) {
                    656:             throw new TypeError();
                    657:         }
                    658:         var t = Object(this);
                    659:         var len = t.length >>> 0;
                    660:         if (len === 0) {
                    661:             return -1;
                    662:         }
                    663:         var n = 0;
                    664:         if (arguments.length > 0) {
                    665:             n = Number(arguments[1]);
                    666:             if (n !== n) { // shortcut for verifying if it's NaN
                    667:                 n = 0;
                    668:             } else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
                    669:                 n = (n > 0 || -1) * Math.floor(Math.abs(n));
                    670:             }
                    671:         }
                    672:         if (n >= len) {
                    673:             return -1;
                    674:         }
                    675:         var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
                    676:         for (; k < len; k++) {
                    677:             if (k in t && t[k] === searchElement) {
                    678:                 return k;
                    679:             }
                    680:         }
                    681:         return -1;
                    682:     }
                    683: }
                    684: 
                    685: // ]]>
                    686: </script>
                    687: 
                    688: ENDJS
                    689: 
                    690: }
                    691: 
1.876     raeburn   692: sub userbrowser_javascript {
                    693:     my $id_functions = &javascript_index_functions();
                    694:     return <<"ENDUSERBRW";
                    695: 
1.888     raeburn   696: function openuserbrowser(formname,uname,udom,ulast,ufirst,uemail,hideudom,crsdom,caller) {
1.876     raeburn   697:     var url = '/adm/pickuser?';
                    698:     var userdom = getDomainFromSelectbox(formname,udom);
                    699:     if (userdom != null) {
                    700:        if (userdom != '') {
                    701:            url += 'srchdom='+userdom+'&';
                    702:        }
                    703:     }
                    704:     url += 'form=' + formname + '&unameelement='+uname+
                    705:                                 '&udomelement='+udom+
                    706:                                 '&ulastelement='+ulast+
                    707:                                 '&ufirstelement='+ufirst+
                    708:                                 '&uemailelement='+uemail+
1.881     raeburn   709:                                 '&hideudomelement='+hideudom+
                    710:                                 '&coursedom='+crsdom;
1.888     raeburn   711:     if ((caller != null) && (caller != undefined)) {
                    712:         url += '&caller='+caller;
                    713:     }
1.876     raeburn   714:     var title = 'User_Browser';
                    715:     var options = 'scrollbars=1,resizable=1,menubar=0';
                    716:     options += ',width=700,height=600';
                    717:     var stdeditbrowser = open(url,title,options,'1');
                    718:     stdeditbrowser.focus();
                    719: }
                    720: 
1.888     raeburn   721: function fix_domain (formname,udom,origdom,uname) {
1.876     raeburn   722:     var formid = getFormIdByName(formname);
                    723:     if (formid > -1) {
1.888     raeburn   724:         var unameid = getIndexByName(formid,uname);
1.876     raeburn   725:         var domid = getIndexByName(formid,udom);
                    726:         var hidedomid = getIndexByName(formid,origdom);
                    727:         if (hidedomid > -1) {
                    728:             var fixeddom = document.forms[formid].elements[hidedomid].value;
1.888     raeburn   729:             var unameval = document.forms[formid].elements[unameid].value;
                    730:             if ((fixeddom != '') && (fixeddom != undefined) && (fixeddom != null) && (unameval != '') && (unameval != undefined) && (unameval != null)) {
                    731:                 if (domid > -1) {
                    732:                     var slct = document.forms[formid].elements[domid];
                    733:                     if (slct.type == 'select-one') {
                    734:                         var i;
                    735:                         for (i=0;i<slct.length;i++) {
                    736:                             if (slct.options[i].value==fixeddom) { slct.selectedIndex=i; }
                    737:                         }
                    738:                     }
                    739:                     if (slct.type == 'hidden') {
                    740:                         slct.value = fixeddom;
1.876     raeburn   741:                     }
                    742:                 }
1.468     raeburn   743:             }
                    744:         }
                    745:     }
1.876     raeburn   746:     return;
                    747: }
                    748: 
                    749: $id_functions
                    750: ENDUSERBRW
1.468     raeburn   751: }
                    752: 
                    753: sub setsec_javascript {
1.1075.2.31  raeburn   754:     my ($sec_element,$formname,$role_element,$credits_element) = @_;
1.905     raeburn   755:     my (@courserolenames,@communityrolenames,$rolestr,$courserolestr,
                    756:         $communityrolestr);
                    757:     if ($role_element ne '') {
                    758:         my @allroles = ('st','ta','ep','in','ad');
                    759:         foreach my $crstype ('Course','Community') {
                    760:             if ($crstype eq 'Community') {
                    761:                 foreach my $role (@allroles) {
                    762:                     push(@communityrolenames,&Apache::lonnet::plaintext($role,$crstype));
                    763:                 }
                    764:                 push(@communityrolenames,&Apache::lonnet::plaintext('co'));
                    765:             } else {
                    766:                 foreach my $role (@allroles) {
                    767:                     push(@courserolenames,&Apache::lonnet::plaintext($role,$crstype));
                    768:                 }
                    769:                 push(@courserolenames,&Apache::lonnet::plaintext('cc'));
                    770:             }
                    771:         }
                    772:         $rolestr = '"'.join('","',@allroles).'"';
                    773:         $courserolestr = '"'.join('","',@courserolenames).'"';
                    774:         $communityrolestr = '"'.join('","',@communityrolenames).'"';
                    775:     }
1.468     raeburn   776:     my $setsections = qq|
                    777: function setSect(sectionlist) {
1.629     raeburn   778:     var sectionsArray = new Array();
                    779:     if ((sectionlist != '') && (typeof sectionlist != "undefined")) {
                    780:         sectionsArray = sectionlist.split(",");
                    781:     }
1.468     raeburn   782:     var numSections = sectionsArray.length;
                    783:     document.$formname.$sec_element.length = 0;
                    784:     if (numSections == 0) {
                    785:         document.$formname.$sec_element.multiple=false;
                    786:         document.$formname.$sec_element.size=1;
                    787:         document.$formname.$sec_element.options[0] = new Option('No existing sections','',false,false)
                    788:     } else {
                    789:         if (numSections == 1) {
                    790:             document.$formname.$sec_element.multiple=false;
                    791:             document.$formname.$sec_element.size=1;
                    792:             document.$formname.$sec_element.options[0] = new Option('Select','',true,true);
                    793:             document.$formname.$sec_element.options[1] = new Option('No section','',false,false)
                    794:             document.$formname.$sec_element.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
                    795:         } else {
                    796:             for (var i=0; i<numSections; i++) {
                    797:                 document.$formname.$sec_element.options[i] = new Option(sectionsArray[i],sectionsArray[i],false,false)
                    798:             }
                    799:             document.$formname.$sec_element.multiple=true
                    800:             if (numSections < 3) {
                    801:                 document.$formname.$sec_element.size=numSections;
                    802:             } else {
                    803:                 document.$formname.$sec_element.size=3;
                    804:             }
                    805:             document.$formname.$sec_element.options[0].selected = false
                    806:         }
                    807:     }
1.91      www       808: }
1.905     raeburn   809: 
                    810: function setRole(crstype) {
1.468     raeburn   811: |;
1.905     raeburn   812:     if ($role_element eq '') {
                    813:         $setsections .= '    return;
                    814: }
                    815: ';
                    816:     } else {
                    817:         $setsections .= qq|
                    818:     var elementLength = document.$formname.$role_element.length;
                    819:     var allroles = Array($rolestr);
                    820:     var courserolenames = Array($courserolestr);
                    821:     var communityrolenames = Array($communityrolestr);
                    822:     if (elementLength != undefined) {
                    823:         if (document.$formname.$role_element.options[5].value == 'cc') {
                    824:             if (crstype == 'Course') {
                    825:                 return;
                    826:             } else {
                    827:                 allroles[5] = 'co';
                    828:                 for (var i=0; i<6; i++) {
                    829:                     document.$formname.$role_element.options[i].value = allroles[i];
                    830:                     document.$formname.$role_element.options[i].text = communityrolenames[i];
                    831:                 }
                    832:             }
                    833:         } else {
                    834:             if (crstype == 'Community') {
                    835:                 return;
                    836:             } else {
                    837:                 allroles[5] = 'cc';
                    838:                 for (var i=0; i<6; i++) {
                    839:                     document.$formname.$role_element.options[i].value = allroles[i];
                    840:                     document.$formname.$role_element.options[i].text = courserolenames[i];
                    841:                 }
                    842:             }
                    843:         }
                    844:     }
                    845:     return;
                    846: }
                    847: |;
                    848:     }
1.1075.2.31  raeburn   849:     if ($credits_element) {
                    850:         $setsections .= qq|
                    851: function setCredits(defaultcredits) {
                    852:     document.$formname.$credits_element.value = defaultcredits;
                    853:     return;
                    854: }
                    855: |;
                    856:     }
1.468     raeburn   857:     return $setsections;
                    858: }
                    859: 
1.91      www       860: sub selectcourse_link {
1.909     raeburn   861:    my ($form,$unameele,$udomele,$desc,$extra_element,$multflag,$selecttype,
                    862:        $typeelement) = @_;
                    863:    my $type = $selecttype;
1.871     raeburn   864:    my $linktext = &mt('Select Course');
                    865:    if ($selecttype eq 'Community') {
1.909     raeburn   866:        $linktext = &mt('Select Community');
1.906     raeburn   867:    } elsif ($selecttype eq 'Course/Community') {
                    868:        $linktext = &mt('Select Course/Community');
1.909     raeburn   869:        $type = '';
1.1019    raeburn   870:    } elsif ($selecttype eq 'Select') {
                    871:        $linktext = &mt('Select');
                    872:        $type = '';
1.871     raeburn   873:    }
1.787     bisitz    874:    return '<span class="LC_nobreak">'
                    875:          ."<a href='"
                    876:          .'javascript:opencrsbrowser("'.$form.'","'.$unameele
                    877:          .'","'.$udomele.'","'.$desc.'","'.$extra_element
1.909     raeburn   878:          .'","'.$multflag.'","'.$type.'","'.$typeelement.'");'
1.871     raeburn   879:          ."'>".$linktext.'</a>'
1.787     bisitz    880:          .'</span>';
1.74      www       881: }
1.42      matthew   882: 
1.653     raeburn   883: sub selectauthor_link {
                    884:    my ($form,$udom)=@_;
                    885:    return '<a href="javascript:openauthorbrowser('."'$form','$udom'".');">'.
                    886:           &mt('Select Author').'</a>';
                    887: }
                    888: 
1.876     raeburn   889: sub selectuser_link {
1.881     raeburn   890:     my ($form,$unameelem,$domelem,$lastelem,$firstelem,$emailelem,$hdomelem,
1.888     raeburn   891:         $coursedom,$linktext,$caller) = @_;
1.876     raeburn   892:     return '<a href="javascript:openuserbrowser('."'$form','$unameelem','$domelem',".
1.888     raeburn   893:            "'$lastelem','$firstelem','$emailelem','$hdomelem','$coursedom','$caller'".
1.881     raeburn   894:            ');">'.$linktext.'</a>';
1.876     raeburn   895: }
                    896: 
1.273     raeburn   897: sub check_uncheck_jscript {
                    898:     my $jscript = <<"ENDSCRT";
                    899: function checkAll(field) {
                    900:     if (field.length > 0) {
                    901:         for (i = 0; i < field.length; i++) {
1.1075.2.14  raeburn   902:             if (!field[i].disabled) {
                    903:                 field[i].checked = true;
                    904:             }
1.273     raeburn   905:         }
                    906:     } else {
1.1075.2.14  raeburn   907:         if (!field.disabled) {
                    908:             field.checked = true;
                    909:         }
1.273     raeburn   910:     }
                    911: }
                    912:  
                    913: function uncheckAll(field) {
                    914:     if (field.length > 0) {
                    915:         for (i = 0; i < field.length; i++) {
                    916:             field[i].checked = false ;
1.543     albertel  917:         }
                    918:     } else {
1.273     raeburn   919:         field.checked = false ;
                    920:     }
                    921: }
                    922: ENDSCRT
                    923:     return $jscript;
                    924: }
                    925: 
1.656     www       926: sub select_timezone {
1.659     raeburn   927:    my ($name,$selected,$onchange,$includeempty)=@_;
                    928:    my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
                    929:    if ($includeempty) {
                    930:        $output .= '<option value=""';
                    931:        if (($selected eq '') || ($selected eq 'local')) {
                    932:            $output .= ' selected="selected" ';
                    933:        }
                    934:        $output .= '> </option>';
                    935:    }
1.657     raeburn   936:    my @timezones = DateTime::TimeZone->all_names;
                    937:    foreach my $tzone (@timezones) {
                    938:        $output.= '<option value="'.$tzone.'"';
                    939:        if ($tzone eq $selected) {
                    940:            $output.=' selected="selected"';
                    941:        }
                    942:        $output.=">$tzone</option>\n";
1.656     www       943:    }
                    944:    $output.="</select>";
                    945:    return $output;
                    946: }
1.273     raeburn   947: 
1.687     raeburn   948: sub select_datelocale {
                    949:     my ($name,$selected,$onchange,$includeempty)=@_;
                    950:     my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
                    951:     if ($includeempty) {
                    952:         $output .= '<option value=""';
                    953:         if ($selected eq '') {
                    954:             $output .= ' selected="selected" ';
                    955:         }
                    956:         $output .= '> </option>';
                    957:     }
                    958:     my (@possibles,%locale_names);
                    959:     my @locales = DateTime::Locale::Catalog::Locales;
                    960:     foreach my $locale (@locales) {
                    961:         if (ref($locale) eq 'HASH') {
                    962:             my $id = $locale->{'id'};
                    963:             if ($id ne '') {
                    964:                 my $en_terr = $locale->{'en_territory'};
                    965:                 my $native_terr = $locale->{'native_territory'};
1.695     raeburn   966:                 my @languages = &Apache::lonlocal::preferred_languages();
1.687     raeburn   967:                 if (grep(/^en$/,@languages) || !@languages) {
                    968:                     if ($en_terr ne '') {
                    969:                         $locale_names{$id} = '('.$en_terr.')';
                    970:                     } elsif ($native_terr ne '') {
                    971:                         $locale_names{$id} = $native_terr;
                    972:                     }
                    973:                 } else {
                    974:                     if ($native_terr ne '') {
                    975:                         $locale_names{$id} = $native_terr.' ';
                    976:                     } elsif ($en_terr ne '') {
                    977:                         $locale_names{$id} = '('.$en_terr.')';
                    978:                     }
                    979:                 }
                    980:                 push (@possibles,$id);
                    981:             }
                    982:         }
                    983:     }
                    984:     foreach my $item (sort(@possibles)) {
                    985:         $output.= '<option value="'.$item.'"';
                    986:         if ($item eq $selected) {
                    987:             $output.=' selected="selected"';
                    988:         }
                    989:         $output.=">$item";
                    990:         if ($locale_names{$item} ne '') {
                    991:             $output.="  $locale_names{$item}</option>\n";
                    992:         }
                    993:         $output.="</option>\n";
                    994:     }
                    995:     $output.="</select>";
                    996:     return $output;
                    997: }
                    998: 
1.792     raeburn   999: sub select_language {
                   1000:     my ($name,$selected,$includeempty) = @_;
                   1001:     my %langchoices;
                   1002:     if ($includeempty) {
1.1075.2.32  raeburn  1003:         %langchoices = ('' => 'No language preference');
1.792     raeburn  1004:     }
                   1005:     foreach my $id (&languageids()) {
                   1006:         my $code = &supportedlanguagecode($id);
                   1007:         if ($code) {
                   1008:             $langchoices{$code} = &plainlanguagedescription($id);
                   1009:         }
                   1010:     }
1.1075.2.32  raeburn  1011:     %langchoices = &Apache::lonlocal::texthash(%langchoices);
1.970     raeburn  1012:     return &select_form($selected,$name,\%langchoices);
1.792     raeburn  1013: }
                   1014: 
1.42      matthew  1015: =pod
1.36      matthew  1016: 
1.648     raeburn  1017: =item * &linked_select_forms(...)
1.36      matthew  1018: 
                   1019: linked_select_forms returns a string containing a <script></script> block
                   1020: and html for two <select> menus.  The select menus will be linked in that
                   1021: changing the value of the first menu will result in new values being placed
                   1022: in the second menu.  The values in the select menu will appear in alphabetical
1.609     raeburn  1023: order unless a defined order is provided.
1.36      matthew  1024: 
                   1025: linked_select_forms takes the following ordered inputs:
                   1026: 
                   1027: =over 4
                   1028: 
1.112     bowersj2 1029: =item * $formname, the name of the <form> tag
1.36      matthew  1030: 
1.112     bowersj2 1031: =item * $middletext, the text which appears between the <select> tags
1.36      matthew  1032: 
1.112     bowersj2 1033: =item * $firstdefault, the default value for the first menu
1.36      matthew  1034: 
1.112     bowersj2 1035: =item * $firstselectname, the name of the first <select> tag
1.36      matthew  1036: 
1.112     bowersj2 1037: =item * $secondselectname, the name of the second <select> tag
1.36      matthew  1038: 
1.112     bowersj2 1039: =item * $hashref, a reference to a hash containing the data for the menus.
1.36      matthew  1040: 
1.609     raeburn  1041: =item * $menuorder, the order of values in the first menu
                   1042: 
1.1075.2.31  raeburn  1043: =item * $onchangefirst, additional javascript call to execute for an onchange
                   1044:         event for the first <select> tag
                   1045: 
                   1046: =item * $onchangesecond, additional javascript call to execute for an onchange
                   1047:         event for the second <select> tag
                   1048: 
1.41      ng       1049: =back 
                   1050: 
1.36      matthew  1051: Below is an example of such a hash.  Only the 'text', 'default', and 
                   1052: 'select2' keys must appear as stated.  keys(%menu) are the possible 
                   1053: values for the first select menu.  The text that coincides with the 
1.41      ng       1054: first menu value is given in $menu{$choice1}->{'text'}.  The values 
1.36      matthew  1055: and text for the second menu are given in the hash pointed to by 
                   1056: $menu{$choice1}->{'select2'}.  
                   1057: 
1.112     bowersj2 1058:  my %menu = ( A1 => { text =>"Choice A1" ,
                   1059:                        default => "B3",
                   1060:                        select2 => { 
                   1061:                            B1 => "Choice B1",
                   1062:                            B2 => "Choice B2",
                   1063:                            B3 => "Choice B3",
                   1064:                            B4 => "Choice B4"
1.609     raeburn  1065:                            },
                   1066:                        order => ['B4','B3','B1','B2'],
1.112     bowersj2 1067:                    },
                   1068:                A2 => { text =>"Choice A2" ,
                   1069:                        default => "C2",
                   1070:                        select2 => { 
                   1071:                            C1 => "Choice C1",
                   1072:                            C2 => "Choice C2",
                   1073:                            C3 => "Choice C3"
1.609     raeburn  1074:                            },
                   1075:                        order => ['C2','C1','C3'],
1.112     bowersj2 1076:                    },
                   1077:                A3 => { text =>"Choice A3" ,
                   1078:                        default => "D6",
                   1079:                        select2 => { 
                   1080:                            D1 => "Choice D1",
                   1081:                            D2 => "Choice D2",
                   1082:                            D3 => "Choice D3",
                   1083:                            D4 => "Choice D4",
                   1084:                            D5 => "Choice D5",
                   1085:                            D6 => "Choice D6",
                   1086:                            D7 => "Choice D7"
1.609     raeburn  1087:                            },
                   1088:                        order => ['D4','D3','D2','D1','D7','D6','D5'],
1.112     bowersj2 1089:                    }
                   1090:                );
1.36      matthew  1091: 
                   1092: =cut
                   1093: 
                   1094: sub linked_select_forms {
                   1095:     my ($formname,
                   1096:         $middletext,
                   1097:         $firstdefault,
                   1098:         $firstselectname,
                   1099:         $secondselectname, 
1.609     raeburn  1100:         $hashref,
                   1101:         $menuorder,
1.1075.2.31  raeburn  1102:         $onchangefirst,
                   1103:         $onchangesecond
1.36      matthew  1104:         ) = @_;
                   1105:     my $second = "document.$formname.$secondselectname";
                   1106:     my $first = "document.$formname.$firstselectname";
                   1107:     # output the javascript to do the changing
                   1108:     my $result = '';
1.776     bisitz   1109:     $result.='<script type="text/javascript" language="JavaScript">'."\n";
1.824     bisitz   1110:     $result.="// <![CDATA[\n";
1.36      matthew  1111:     $result.="var select2data = new Object();\n";
                   1112:     $" = '","';
                   1113:     my $debug = '';
                   1114:     foreach my $s1 (sort(keys(%$hashref))) {
                   1115:         $result.="select2data.d_$s1 = new Object();\n";        
                   1116:         $result.="select2data.d_$s1.def = new String('".
                   1117:             $hashref->{$s1}->{'default'}."');\n";
1.609     raeburn  1118:         $result.="select2data.d_$s1.values = new Array(";
1.36      matthew  1119:         my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
1.609     raeburn  1120:         if (ref($hashref->{$s1}->{'order'}) eq 'ARRAY') {
                   1121:             @s2values = @{$hashref->{$s1}->{'order'}};
                   1122:         }
1.36      matthew  1123:         $result.="\"@s2values\");\n";
                   1124:         $result.="select2data.d_$s1.texts = new Array(";        
                   1125:         my @s2texts;
                   1126:         foreach my $value (@s2values) {
                   1127:             push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
                   1128:         }
                   1129:         $result.="\"@s2texts\");\n";
                   1130:     }
                   1131:     $"=' ';
                   1132:     $result.= <<"END";
                   1133: 
                   1134: function select1_changed() {
                   1135:     // Determine new choice
                   1136:     var newvalue = "d_" + $first.value;
                   1137:     // update select2
                   1138:     var values     = select2data[newvalue].values;
                   1139:     var texts      = select2data[newvalue].texts;
                   1140:     var select2def = select2data[newvalue].def;
                   1141:     var i;
                   1142:     // out with the old
                   1143:     for (i = 0; i < $second.options.length; i++) {
                   1144:         $second.options[i] = null;
                   1145:     }
                   1146:     // in with the nuclear
                   1147:     for (i=0;i<values.length; i++) {
                   1148:         $second.options[i] = new Option(values[i]);
1.143     matthew  1149:         $second.options[i].value = values[i];
1.36      matthew  1150:         $second.options[i].text = texts[i];
                   1151:         if (values[i] == select2def) {
                   1152:             $second.options[i].selected = true;
                   1153:         }
                   1154:     }
                   1155: }
1.824     bisitz   1156: // ]]>
1.36      matthew  1157: </script>
                   1158: END
                   1159:     # output the initial values for the selection lists
1.1075.2.31  raeburn  1160:     $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed();$onchangefirst\">\n";
1.609     raeburn  1161:     my @order = sort(keys(%{$hashref}));
                   1162:     if (ref($menuorder) eq 'ARRAY') {
                   1163:         @order = @{$menuorder};
                   1164:     }
                   1165:     foreach my $value (@order) {
1.36      matthew  1166:         $result.="    <option value=\"$value\" ";
1.253     albertel 1167:         $result.=" selected=\"selected\" " if ($value eq $firstdefault);
1.119     www      1168:         $result.=">".&mt($hashref->{$value}->{'text'})."</option>\n";
1.36      matthew  1169:     }
                   1170:     $result .= "</select>\n";
                   1171:     my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
                   1172:     $result .= $middletext;
1.1075.2.31  raeburn  1173:     $result .= "<select size=\"1\" name=\"$secondselectname\"";
                   1174:     if ($onchangesecond) {
                   1175:         $result .= ' onchange="'.$onchangesecond.'"';
                   1176:     }
                   1177:     $result .= ">\n";
1.36      matthew  1178:     my $seconddefault = $hashref->{$firstdefault}->{'default'};
1.609     raeburn  1179:     
                   1180:     my @secondorder = sort(keys(%select2));
                   1181:     if (ref($hashref->{$firstdefault}->{'order'}) eq 'ARRAY') {
                   1182:         @secondorder = @{$hashref->{$firstdefault}->{'order'}};
                   1183:     }
                   1184:     foreach my $value (@secondorder) {
1.36      matthew  1185:         $result.="    <option value=\"$value\" ";        
1.253     albertel 1186:         $result.=" selected=\"selected\" " if ($value eq $seconddefault);
1.119     www      1187:         $result.=">".&mt($select2{$value})."</option>\n";
1.36      matthew  1188:     }
                   1189:     $result .= "</select>\n";
                   1190:     #    return $debug;
                   1191:     return $result;
                   1192: }   #  end of sub linked_select_forms {
                   1193: 
1.45      matthew  1194: =pod
1.44      bowersj2 1195: 
1.973     raeburn  1196: =item * &help_open_topic($topic,$text,$stayOnPage,$width,$height,$imgid)
1.44      bowersj2 1197: 
1.112     bowersj2 1198: Returns a string corresponding to an HTML link to the given help
                   1199: $topic, where $topic corresponds to the name of a .tex file in
                   1200: /home/httpd/html/adm/help/tex, with underscores replaced by
                   1201: spaces. 
                   1202: 
                   1203: $text will optionally be linked to the same topic, allowing you to
                   1204: link text in addition to the graphic. If you do not want to link
                   1205: text, but wish to specify one of the later parameters, pass an
                   1206: empty string. 
                   1207: 
                   1208: $stayOnPage is a value that will be interpreted as a boolean. If true,
                   1209: the link will not open a new window. If false, the link will open
                   1210: a new window using Javascript. (Default is false.) 
                   1211: 
                   1212: $width and $height are optional numerical parameters that will
                   1213: override the width and height of the popped up window, which may
1.973     raeburn  1214: be useful for certain help topics with big pictures included.
                   1215: 
                   1216: $imgid is the id of the img tag used for the help icon. This may be
                   1217: used in a javascript call to switch the image src.  See 
                   1218: lonhtmlcommon::htmlareaselectactive() for an example.
1.44      bowersj2 1219: 
                   1220: =cut
                   1221: 
                   1222: sub help_open_topic {
1.973     raeburn  1223:     my ($topic, $text, $stayOnPage, $width, $height, $imgid) = @_;
1.48      bowersj2 1224:     $text = "" if (not defined $text);
1.44      bowersj2 1225:     $stayOnPage = 0 if (not defined $stayOnPage);
1.1033    www      1226:     $width = 500 if (not defined $width);
1.44      bowersj2 1227:     $height = 400 if (not defined $height);
                   1228:     my $filename = $topic;
                   1229:     $filename =~ s/ /_/g;
                   1230: 
1.48      bowersj2 1231:     my $template = "";
                   1232:     my $link;
1.572     banghart 1233:     
1.159     www      1234:     $topic=~s/\W/\_/g;
1.44      bowersj2 1235: 
1.572     banghart 1236:     if (!$stayOnPage) {
1.1033    www      1237: 	$link = "javascript:openMyModal('/adm/help/${filename}.hlp',$width,$height,'yes');";
1.1037    www      1238:     } elsif ($stayOnPage eq 'popup') {
                   1239:         $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 1240:     } else {
1.48      bowersj2 1241: 	$link = "/adm/help/${filename}.hlp";
                   1242:     }
                   1243: 
                   1244:     # Add the text
1.755     neumanie 1245:     if ($text ne "") {	
1.763     bisitz   1246: 	$template.='<span class="LC_help_open_topic">'
                   1247:                   .'<a target="_top" href="'.$link.'">'
                   1248:                   .$text.'</a>';
1.48      bowersj2 1249:     }
                   1250: 
1.763     bisitz   1251:     # (Always) Add the graphic
1.179     matthew  1252:     my $title = &mt('Online Help');
1.667     raeburn  1253:     my $helpicon=&lonhttpdurl("/adm/help/help.png");
1.973     raeburn  1254:     if ($imgid ne '') {
                   1255:         $imgid = ' id="'.$imgid.'"';
                   1256:     }
1.763     bisitz   1257:     $template.=' <a target="_top" href="'.$link.'" title="'.$title.'">'
                   1258:               .'<img src="'.$helpicon.'" border="0"'
                   1259:               .' alt="'.&mt('Help: [_1]',$topic).'"'
1.973     raeburn  1260:               .' title="'.$title.'" style="vertical-align:middle;"'.$imgid 
1.763     bisitz   1261:               .' /></a>';
                   1262:     if ($text ne "") {	
                   1263:         $template.='</span>';
                   1264:     }
1.44      bowersj2 1265:     return $template;
                   1266: 
1.106     bowersj2 1267: }
                   1268: 
                   1269: # This is a quicky function for Latex cheatsheet editing, since it 
                   1270: # appears in at least four places
                   1271: sub helpLatexCheatsheet {
1.1037    www      1272:     my ($topic,$text,$not_author,$stayOnPage) = @_;
1.732     raeburn  1273:     my $out;
1.106     bowersj2 1274:     my $addOther = '';
1.732     raeburn  1275:     if ($topic) {
1.1037    www      1276: 	$addOther = '<span>'.&help_open_topic($topic,&mt($text),$stayOnPage, undef, 600).'</span> ';
1.763     bisitz   1277:     }
                   1278:     $out = '<span>' # Start cheatsheet
                   1279: 	  .$addOther
                   1280:           .'<span>'
1.1037    www      1281: 	  .&help_open_topic('Greek_Symbols',&mt('Greek Symbols'),$stayOnPage,undef,600)
1.763     bisitz   1282: 	  .'</span> <span>'
1.1037    www      1283: 	  .&help_open_topic('Other_Symbols',&mt('Other Symbols'),$stayOnPage,undef,600)
1.763     bisitz   1284: 	  .'</span>';
1.732     raeburn  1285:     unless ($not_author) {
1.763     bisitz   1286:         $out .= ' <span>'
1.1037    www      1287: 	       .&help_open_topic('Authoring_Output_Tags',&mt('Output Tags'),$stayOnPage,undef,600)
1.763     bisitz   1288: 	       .'</span>';
1.732     raeburn  1289:     }
1.763     bisitz   1290:     $out .= '</span>'; # End cheatsheet
1.732     raeburn  1291:     return $out;
1.172     www      1292: }
                   1293: 
1.430     albertel 1294: sub general_help {
                   1295:     my $helptopic='Student_Intro';
                   1296:     if ($env{'request.role'}=~/^(ca|au)/) {
                   1297: 	$helptopic='Authoring_Intro';
1.907     raeburn  1298:     } elsif ($env{'request.role'}=~/^(cc|co)/) {
1.430     albertel 1299: 	$helptopic='Course_Coordination_Intro';
1.672     raeburn  1300:     } elsif ($env{'request.role'}=~/^dc/) {
                   1301:         $helptopic='Domain_Coordination_Intro';
1.430     albertel 1302:     }
                   1303:     return $helptopic;
                   1304: }
                   1305: 
                   1306: sub update_help_link {
                   1307:     my ($topic,$component_help,$faq,$bug,$stayOnPage) = @_;
                   1308:     my $origurl = $ENV{'REQUEST_URI'};
                   1309:     $origurl=~s|^/~|/priv/|;
                   1310:     my $timestamp = time;
                   1311:     foreach my $datum (\$topic,\$component_help,\$faq,\$bug,\$origurl) {
                   1312:         $$datum = &escape($$datum);
                   1313:     }
                   1314: 
                   1315:     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";
                   1316:     my $output .= <<"ENDOUTPUT";
                   1317: <script type="text/javascript">
1.824     bisitz   1318: // <![CDATA[
1.430     albertel 1319: banner_link = '$banner_link';
1.824     bisitz   1320: // ]]>
1.430     albertel 1321: </script>
                   1322: ENDOUTPUT
                   1323:     return $output;
                   1324: }
                   1325: 
                   1326: # now just updates the help link and generates a blue icon
1.193     raeburn  1327: sub help_open_menu {
1.430     albertel 1328:     my ($topic,$component_help,$faq,$bug,$stayOnPage,$width,$height,$text) 
1.552     banghart 1329: 	= @_;    
1.949     droeschl 1330:     $stayOnPage = 1;
1.430     albertel 1331:     my $output;
                   1332:     if ($component_help) {
                   1333: 	if (!$text) {
                   1334: 	    $output=&help_open_topic($component_help,undef,$stayOnPage,
                   1335: 				       $width,$height);
                   1336: 	} else {
                   1337: 	    my $help_text;
                   1338: 	    $help_text=&unescape($topic);
                   1339: 	    $output='<table><tr><td>'.
                   1340: 		&help_open_topic($component_help,$help_text,$stayOnPage,
                   1341: 				 $width,$height).'</td></tr></table>';
                   1342: 	}
                   1343:     }
                   1344:     my $banner_link = &update_help_link($topic,$component_help,$faq,$bug,$stayOnPage);
                   1345:     return $output.$banner_link;
                   1346: }
                   1347: 
                   1348: sub top_nav_help {
                   1349:     my ($text) = @_;
1.436     albertel 1350:     $text = &mt($text);
1.949     droeschl 1351:     my $stay_on_page = 1;
                   1352: 
1.572     banghart 1353:     my $link = ($stay_on_page) ? "javascript:helpMenu('display')"
1.436     albertel 1354: 	                     : "javascript:helpMenu('open')";
1.572     banghart 1355:     my $banner_link = &update_help_link(undef,undef,undef,undef,$stay_on_page);
1.436     albertel 1356: 
1.201     raeburn  1357:     my $title = &mt('Get help');
1.436     albertel 1358: 
                   1359:     return <<"END";
                   1360: $banner_link
                   1361:  <a href="$link" title="$title">$text</a>
                   1362: END
                   1363: }
                   1364: 
                   1365: sub help_menu_js {
                   1366:     my ($text) = @_;
1.949     droeschl 1367:     my $stayOnPage = 1;
1.436     albertel 1368:     my $width = 620;
                   1369:     my $height = 600;
1.430     albertel 1370:     my $helptopic=&general_help();
                   1371:     my $details_link = '/adm/help/'.$helptopic.'.hlp';
1.261     albertel 1372:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.331     albertel 1373:     my $start_page =
                   1374:         &Apache::loncommon::start_page('Help Menu', undef,
                   1375: 				       {'frameset'    => 1,
                   1376: 					'js_ready'    => 1,
                   1377: 					'add_entries' => {
                   1378: 					    'border' => '0',
1.579     raeburn  1379: 					    'rows'   => "110,*",},});
1.331     albertel 1380:     my $end_page =
                   1381:         &Apache::loncommon::end_page({'frameset' => 1,
                   1382: 				      'js_ready' => 1,});
                   1383: 
1.436     albertel 1384:     my $template .= <<"ENDTEMPLATE";
                   1385: <script type="text/javascript">
1.877     bisitz   1386: // <![CDATA[
1.253     albertel 1387: // <!-- BEGIN LON-CAPA Internal
1.430     albertel 1388: var banner_link = '';
1.243     raeburn  1389: function helpMenu(target) {
                   1390:     var caller = this;
                   1391:     if (target == 'open') {
                   1392:         var newWindow = null;
                   1393:         try {
1.262     albertel 1394:             newWindow =  window.open($nothing,"helpmenu","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" )
1.243     raeburn  1395:         }
                   1396:         catch(error) {
                   1397:             writeHelp(caller);
                   1398:             return;
                   1399:         }
                   1400:         if (newWindow) {
                   1401:             caller = newWindow;
                   1402:         }
1.193     raeburn  1403:     }
1.243     raeburn  1404:     writeHelp(caller);
                   1405:     return;
                   1406: }
                   1407: function writeHelp(caller) {
1.1072    raeburn  1408:     caller.document.writeln('$start_page\\n<frame name="bannerframe" src="'+banner_link+'" />\\n<frame name="bodyframe" src="$details_link" />\\n$end_page')
1.243     raeburn  1409:     caller.document.close()
                   1410:     caller.focus()
1.193     raeburn  1411: }
1.877     bisitz   1412: // END LON-CAPA Internal -->
1.253     albertel 1413: // ]]>
1.436     albertel 1414: </script>
1.193     raeburn  1415: ENDTEMPLATE
                   1416:     return $template;
                   1417: }
                   1418: 
1.172     www      1419: sub help_open_bug {
                   1420:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel 1421:     unless ($env{'user.adv'}) { return ''; }
1.172     www      1422:     unless ($Apache::lonnet::perlvar{'BugzillaHost'}) { return ''; }
                   1423:     $text = "" if (not defined $text);
                   1424: 	$stayOnPage=1;
1.184     albertel 1425:     $width = 600 if (not defined $width);
                   1426:     $height = 600 if (not defined $height);
1.172     www      1427: 
                   1428:     $topic=~s/\W+/\+/g;
                   1429:     my $link='';
                   1430:     my $template='';
1.379     albertel 1431:     my $url=$Apache::lonnet::perlvar{'BugzillaHost'}.'enter_bug.cgi?product=LON-CAPA&amp;bug_file_loc='.
                   1432: 	&escape($ENV{'REQUEST_URI'}).'&amp;component='.$topic;
1.172     www      1433:     if (!$stayOnPage)
                   1434:     {
                   1435: 	$link = "javascript:void(open('$url', 'Bugzilla', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                   1436:     }
                   1437:     else
                   1438:     {
                   1439: 	$link = $url;
                   1440:     }
                   1441:     # Add the text
                   1442:     if ($text ne "")
                   1443:     {
                   1444: 	$template .= 
                   1445:   "<table bgcolor='#AA3333' cellspacing='1' cellpadding='1' border='0'><tr>".
1.705     tempelho 1446:   "<td bgcolor='#FF5555'><a target=\"_top\" href=\"$link\"><span style=\"color:#FFFFFF;font-size:10pt;\">$text</span></a>";
1.172     www      1447:     }
                   1448: 
                   1449:     # Add the graphic
1.179     matthew  1450:     my $title = &mt('Report a Bug');
1.215     albertel 1451:     my $bugicon=&lonhttpdurl("/adm/lonMisc/smallBug.gif");
1.172     www      1452:     $template .= <<"ENDTEMPLATE";
1.436     albertel 1453:  <a target="_top" href="$link" title="$title"><img src="$bugicon" border="0" alt="(Bug: $topic)" /></a>
1.172     www      1454: ENDTEMPLATE
                   1455:     if ($text ne '') { $template.='</td></tr></table>' };
                   1456:     return $template;
                   1457: 
                   1458: }
                   1459: 
                   1460: sub help_open_faq {
                   1461:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel 1462:     unless ($env{'user.adv'}) { return ''; }
1.172     www      1463:     unless ($Apache::lonnet::perlvar{'FAQHost'}) { return ''; }
                   1464:     $text = "" if (not defined $text);
                   1465: 	$stayOnPage=1;
                   1466:     $width = 350 if (not defined $width);
                   1467:     $height = 400 if (not defined $height);
                   1468: 
                   1469:     $topic=~s/\W+/\+/g;
                   1470:     my $link='';
                   1471:     my $template='';
                   1472:     my $url=$Apache::lonnet::perlvar{'FAQHost'}.'/fom/cache/'.$topic.'.html';
                   1473:     if (!$stayOnPage)
                   1474:     {
                   1475: 	$link = "javascript:void(open('$url', 'FAQ-O-Matic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                   1476:     }
                   1477:     else
                   1478:     {
                   1479: 	$link = $url;
                   1480:     }
                   1481: 
                   1482:     # Add the text
                   1483:     if ($text ne "")
                   1484:     {
                   1485: 	$template .= 
1.173     www      1486:   "<table bgcolor='#337733' cellspacing='1' cellpadding='1' border='0'><tr>".
1.705     tempelho 1487:   "<td bgcolor='#448844'><a target=\"_top\" href=\"$link\"><span style=\"color:#FFFFFF; font-size:10pt;\">$text</span></a>";
1.172     www      1488:     }
                   1489: 
                   1490:     # Add the graphic
1.179     matthew  1491:     my $title = &mt('View the FAQ');
1.215     albertel 1492:     my $faqicon=&lonhttpdurl("/adm/lonMisc/smallFAQ.gif");
1.172     www      1493:     $template .= <<"ENDTEMPLATE";
1.436     albertel 1494:  <a target="_top" href="$link" title="$title"><img src="$faqicon" border="0" alt="(FAQ: $topic)" /></a>
1.172     www      1495: ENDTEMPLATE
                   1496:     if ($text ne '') { $template.='</td></tr></table>' };
                   1497:     return $template;
                   1498: 
1.44      bowersj2 1499: }
1.37      matthew  1500: 
1.180     matthew  1501: ###############################################################
                   1502: ###############################################################
                   1503: 
1.45      matthew  1504: =pod
                   1505: 
1.648     raeburn  1506: =item * &change_content_javascript():
1.256     matthew  1507: 
                   1508: This and the next function allow you to create small sections of an
                   1509: otherwise static HTML page that you can update on the fly with
                   1510: Javascript, even in Netscape 4.
                   1511: 
                   1512: The Javascript fragment returned by this function (no E<lt>scriptE<gt> tag)
                   1513: must be written to the HTML page once. It will prove the Javascript
                   1514: function "change(name, content)". Calling the change function with the
                   1515: name of the section 
                   1516: you want to update, matching the name passed to C<changable_area>, and
                   1517: the new content you want to put in there, will put the content into
                   1518: that area.
                   1519: 
                   1520: B<Note>: Netscape 4 only reserves enough space for the changable area
                   1521: to contain room for the original contents. You need to "make space"
                   1522: for whatever changes you wish to make, and be B<sure> to check your
                   1523: code in Netscape 4. This feature in Netscape 4 is B<not> powerful;
                   1524: it's adequate for updating a one-line status display, but little more.
                   1525: This script will set the space to 100% width, so you only need to
                   1526: worry about height in Netscape 4.
                   1527: 
                   1528: Modern browsers are much less limiting, and if you can commit to the
                   1529: user not using Netscape 4, this feature may be used freely with
                   1530: pretty much any HTML.
                   1531: 
                   1532: =cut
                   1533: 
                   1534: sub change_content_javascript {
                   1535:     # If we're on Netscape 4, we need to use Layer-based code
1.258     albertel 1536:     if ($env{'browser.type'} eq 'netscape' &&
                   1537: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew  1538: 	return (<<NETSCAPE4);
                   1539: 	function change(name, content) {
                   1540: 	    doc = document.layers[name+"___escape"].layers[0].document;
                   1541: 	    doc.open();
                   1542: 	    doc.write(content);
                   1543: 	    doc.close();
                   1544: 	}
                   1545: NETSCAPE4
                   1546:     } else {
                   1547: 	# Otherwise, we need to use semi-standards-compliant code
                   1548: 	# (technically, "innerHTML" isn't standard but the equivalent
                   1549: 	# is really scary, and every useful browser supports it
                   1550: 	return (<<DOMBASED);
                   1551: 	function change(name, content) {
                   1552: 	    element = document.getElementById(name);
                   1553: 	    element.innerHTML = content;
                   1554: 	}
                   1555: DOMBASED
                   1556:     }
                   1557: }
                   1558: 
                   1559: =pod
                   1560: 
1.648     raeburn  1561: =item * &changable_area($name,$origContent):
1.256     matthew  1562: 
                   1563: This provides a "changable area" that can be modified on the fly via
                   1564: the Javascript code provided in C<change_content_javascript>. $name is
                   1565: the name you will use to reference the area later; do not repeat the
                   1566: same name on a given HTML page more then once. $origContent is what
                   1567: the area will originally contain, which can be left blank.
                   1568: 
                   1569: =cut
                   1570: 
                   1571: sub changable_area {
                   1572:     my ($name, $origContent) = @_;
                   1573: 
1.258     albertel 1574:     if ($env{'browser.type'} eq 'netscape' &&
                   1575: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew  1576: 	# If this is netscape 4, we need to use the Layer tag
                   1577: 	return "<ilayer width='100%' id='${name}___escape' overflow='none'><layer width='100%' id='$name' overflow='none'>$origContent</layer></ilayer>";
                   1578:     } else {
                   1579: 	return "<span id='$name'>$origContent</span>";
                   1580:     }
                   1581: }
                   1582: 
                   1583: =pod
                   1584: 
1.648     raeburn  1585: =item * &viewport_geometry_js 
1.590     raeburn  1586: 
                   1587: Provides javascript object (Geometry) which can provide information about the viewport geometry for the client browser.
                   1588: 
                   1589: =cut
                   1590: 
                   1591: 
                   1592: sub viewport_geometry_js { 
                   1593:     return <<"GEOMETRY";
                   1594: var Geometry = {};
                   1595: function init_geometry() {
                   1596:     if (Geometry.init) { return };
                   1597:     Geometry.init=1;
                   1598:     if (window.innerHeight) {
                   1599:         Geometry.getViewportHeight   = function() { return window.innerHeight; };
                   1600:         Geometry.getViewportWidth   = function() { return window.innerWidth; };
                   1601:         Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
                   1602:         Geometry.getVerticalScroll   = function() { return window.pageYOffset; };
                   1603:     }
                   1604:     else if (document.documentElement && document.documentElement.clientHeight) {
                   1605:         Geometry.getViewportHeight =
                   1606:             function() { return document.documentElement.clientHeight; };
                   1607:         Geometry.getViewportWidth =
                   1608:             function() { return document.documentElement.clientWidth; };
                   1609: 
                   1610:         Geometry.getHorizontalScroll =
                   1611:             function() { return document.documentElement.scrollLeft; };
                   1612:         Geometry.getVerticalScroll =
                   1613:             function() { return document.documentElement.scrollTop; };
                   1614:     }
                   1615:     else if (document.body.clientHeight) {
                   1616:         Geometry.getViewportHeight =
                   1617:             function() { return document.body.clientHeight; };
                   1618:         Geometry.getViewportWidth =
                   1619:             function() { return document.body.clientWidth; };
                   1620:         Geometry.getHorizontalScroll =
                   1621:             function() { return document.body.scrollLeft; };
                   1622:         Geometry.getVerticalScroll =
                   1623:             function() { return document.body.scrollTop; };
                   1624:     }
                   1625: }
                   1626: 
                   1627: GEOMETRY
                   1628: }
                   1629: 
                   1630: =pod
                   1631: 
1.648     raeburn  1632: =item * &viewport_size_js()
1.590     raeburn  1633: 
                   1634: 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. 
                   1635: 
                   1636: =cut
                   1637: 
                   1638: sub viewport_size_js {
                   1639:     my $geometry = &viewport_geometry_js();
                   1640:     return <<"DIMS";
                   1641: 
                   1642: $geometry
                   1643: 
                   1644: function getViewportDims(width,height) {
                   1645:     init_geometry();
                   1646:     width.value = Geometry.getViewportWidth();
                   1647:     height.value = Geometry.getViewportHeight();
                   1648:     return;
                   1649: }
                   1650: 
                   1651: DIMS
                   1652: }
                   1653: 
                   1654: =pod
                   1655: 
1.648     raeburn  1656: =item * &resize_textarea_js()
1.565     albertel 1657: 
                   1658: emits the needed javascript to resize a textarea to be as big as possible
                   1659: 
                   1660: creates a function resize_textrea that takes two IDs first should be
                   1661: the id of the element to resize, second should be the id of a div that
                   1662: surrounds everything that comes after the textarea, this routine needs
                   1663: to be attached to the <body> for the onload and onresize events.
                   1664: 
1.648     raeburn  1665: =back
1.565     albertel 1666: 
                   1667: =cut
                   1668: 
                   1669: sub resize_textarea_js {
1.590     raeburn  1670:     my $geometry = &viewport_geometry_js();
1.565     albertel 1671:     return <<"RESIZE";
                   1672:     <script type="text/javascript">
1.824     bisitz   1673: // <![CDATA[
1.590     raeburn  1674: $geometry
1.565     albertel 1675: 
1.588     albertel 1676: function getX(element) {
                   1677:     var x = 0;
                   1678:     while (element) {
                   1679: 	x += element.offsetLeft;
                   1680: 	element = element.offsetParent;
                   1681:     }
                   1682:     return x;
                   1683: }
                   1684: function getY(element) {
                   1685:     var y = 0;
                   1686:     while (element) {
                   1687: 	y += element.offsetTop;
                   1688: 	element = element.offsetParent;
                   1689:     }
                   1690:     return y;
                   1691: }
                   1692: 
                   1693: 
1.565     albertel 1694: function resize_textarea(textarea_id,bottom_id) {
                   1695:     init_geometry();
                   1696:     var textarea        = document.getElementById(textarea_id);
                   1697:     //alert(textarea);
                   1698: 
1.588     albertel 1699:     var textarea_top    = getY(textarea);
1.565     albertel 1700:     var textarea_height = textarea.offsetHeight;
                   1701:     var bottom          = document.getElementById(bottom_id);
1.588     albertel 1702:     var bottom_top      = getY(bottom);
1.565     albertel 1703:     var bottom_height   = bottom.offsetHeight;
                   1704:     var window_height   = Geometry.getViewportHeight();
1.588     albertel 1705:     var fudge           = 23;
1.565     albertel 1706:     var new_height      = window_height-fudge-textarea_top-bottom_height;
                   1707:     if (new_height < 300) {
                   1708: 	new_height = 300;
                   1709:     }
                   1710:     textarea.style.height=new_height+'px';
                   1711: }
1.824     bisitz   1712: // ]]>
1.565     albertel 1713: </script>
                   1714: RESIZE
                   1715: 
                   1716: }
                   1717: 
                   1718: =pod
                   1719: 
1.256     matthew  1720: =head1 Excel and CSV file utility routines
                   1721: 
                   1722: =over 4
                   1723: 
                   1724: =cut
                   1725: 
                   1726: ###############################################################
                   1727: ###############################################################
                   1728: 
                   1729: =pod
                   1730: 
1.648     raeburn  1731: =item * &csv_translate($text) 
1.37      matthew  1732: 
1.185     www      1733: Translate $text to allow it to be output as a 'comma separated values' 
1.37      matthew  1734: format.
                   1735: 
                   1736: =cut
                   1737: 
1.180     matthew  1738: ###############################################################
                   1739: ###############################################################
1.37      matthew  1740: sub csv_translate {
                   1741:     my $text = shift;
                   1742:     $text =~ s/\"/\"\"/g;
1.209     albertel 1743:     $text =~ s/\n/ /g;
1.37      matthew  1744:     return $text;
                   1745: }
1.180     matthew  1746: 
                   1747: ###############################################################
                   1748: ###############################################################
                   1749: 
                   1750: =pod
                   1751: 
1.648     raeburn  1752: =item * &define_excel_formats()
1.180     matthew  1753: 
                   1754: Define some commonly used Excel cell formats.
                   1755: 
                   1756: Currently supported formats:
                   1757: 
                   1758: =over 4
                   1759: 
                   1760: =item header
                   1761: 
                   1762: =item bold
                   1763: 
                   1764: =item h1
                   1765: 
                   1766: =item h2
                   1767: 
                   1768: =item h3
                   1769: 
1.256     matthew  1770: =item h4
                   1771: 
                   1772: =item i
                   1773: 
1.180     matthew  1774: =item date
                   1775: 
                   1776: =back
                   1777: 
                   1778: Inputs: $workbook
                   1779: 
                   1780: Returns: $format, a hash reference.
                   1781: 
1.1057    foxr     1782: 
1.180     matthew  1783: =cut
                   1784: 
                   1785: ###############################################################
                   1786: ###############################################################
                   1787: sub define_excel_formats {
                   1788:     my ($workbook) = @_;
                   1789:     my $format;
                   1790:     $format->{'header'} = $workbook->add_format(bold      => 1, 
                   1791:                                                 bottom    => 1,
                   1792:                                                 align     => 'center');
                   1793:     $format->{'bold'} = $workbook->add_format(bold=>1);
                   1794:     $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
                   1795:     $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
                   1796:     $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
1.255     matthew  1797:     $format->{'h4'}   = $workbook->add_format(bold=>1, size=>12);
1.246     matthew  1798:     $format->{'i'}    = $workbook->add_format(italic=>1);
1.180     matthew  1799:     $format->{'date'} = $workbook->add_format(num_format=>
1.207     matthew  1800:                                             'mm/dd/yyyy hh:mm:ss');
1.180     matthew  1801:     return $format;
                   1802: }
                   1803: 
                   1804: ###############################################################
                   1805: ###############################################################
1.113     bowersj2 1806: 
                   1807: =pod
                   1808: 
1.648     raeburn  1809: =item * &create_workbook()
1.255     matthew  1810: 
                   1811: Create an Excel worksheet.  If it fails, output message on the
                   1812: request object and return undefs.
                   1813: 
                   1814: Inputs: Apache request object
                   1815: 
                   1816: Returns (undef) on failure, 
                   1817:     Excel worksheet object, scalar with filename, and formats 
                   1818:     from &Apache::loncommon::define_excel_formats on success
                   1819: 
                   1820: =cut
                   1821: 
                   1822: ###############################################################
                   1823: ###############################################################
                   1824: sub create_workbook {
                   1825:     my ($r) = @_;
                   1826:         #
                   1827:     # Create the excel spreadsheet
                   1828:     my $filename = '/prtspool/'.
1.258     albertel 1829:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.255     matthew  1830:         time.'_'.rand(1000000000).'.xls';
                   1831:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
                   1832:     if (! defined($workbook)) {
                   1833:         $r->log_error("Error creating excel spreadsheet $filename: $!");
1.928     bisitz   1834:         $r->print(
                   1835:             '<p class="LC_error">'
                   1836:            .&mt('Problems occurred in creating the new Excel file.')
                   1837:            .' '.&mt('This error has been logged.')
                   1838:            .' '.&mt('Please alert your LON-CAPA administrator.')
                   1839:            .'</p>'
                   1840:         );
1.255     matthew  1841:         return (undef);
                   1842:     }
                   1843:     #
1.1014    foxr     1844:     $workbook->set_tempdir(LONCAPA::tempdir());
1.255     matthew  1845:     #
                   1846:     my $format = &Apache::loncommon::define_excel_formats($workbook);
                   1847:     return ($workbook,$filename,$format);
                   1848: }
                   1849: 
                   1850: ###############################################################
                   1851: ###############################################################
                   1852: 
                   1853: =pod
                   1854: 
1.648     raeburn  1855: =item * &create_text_file()
1.113     bowersj2 1856: 
1.542     raeburn  1857: Create a file to write to and eventually make available to the user.
1.256     matthew  1858: If file creation fails, outputs an error message on the request object and 
                   1859: return undefs.
1.113     bowersj2 1860: 
1.256     matthew  1861: Inputs: Apache request object, and file suffix
1.113     bowersj2 1862: 
1.256     matthew  1863: Returns (undef) on failure, 
                   1864:     Filehandle and filename on success.
1.113     bowersj2 1865: 
                   1866: =cut
                   1867: 
1.256     matthew  1868: ###############################################################
                   1869: ###############################################################
                   1870: sub create_text_file {
                   1871:     my ($r,$suffix) = @_;
                   1872:     if (! defined($suffix)) { $suffix = 'txt'; };
                   1873:     my $fh;
                   1874:     my $filename = '/prtspool/'.
1.258     albertel 1875:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.256     matthew  1876:         time.'_'.rand(1000000000).'.'.$suffix;
                   1877:     $fh = Apache::File->new('>/home/httpd'.$filename);
                   1878:     if (! defined($fh)) {
                   1879:         $r->log_error("Couldn't open $filename for output $!");
1.928     bisitz   1880:         $r->print(
                   1881:             '<p class="LC_error">'
                   1882:            .&mt('Problems occurred in creating the output file.')
                   1883:            .' '.&mt('This error has been logged.')
                   1884:            .' '.&mt('Please alert your LON-CAPA administrator.')
                   1885:            .'</p>'
                   1886:         );
1.113     bowersj2 1887:     }
1.256     matthew  1888:     return ($fh,$filename)
1.113     bowersj2 1889: }
                   1890: 
                   1891: 
1.256     matthew  1892: =pod 
1.113     bowersj2 1893: 
                   1894: =back
                   1895: 
                   1896: =cut
1.37      matthew  1897: 
                   1898: ###############################################################
1.33      matthew  1899: ##        Home server <option> list generating code          ##
                   1900: ###############################################################
1.35      matthew  1901: 
1.169     www      1902: # ------------------------------------------
                   1903: 
                   1904: sub domain_select {
                   1905:     my ($name,$value,$multiple)=@_;
                   1906:     my %domains=map { 
1.514     albertel 1907: 	$_ => $_.' '. &Apache::lonnet::domain($_,'description') 
1.512     albertel 1908:     } &Apache::lonnet::all_domains();
1.169     www      1909:     if ($multiple) {
                   1910: 	$domains{''}=&mt('Any domain');
1.550     albertel 1911: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
1.287     albertel 1912: 	return &multiple_select_form($name,$value,4,\%domains);
1.169     www      1913:     } else {
1.550     albertel 1914: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
1.970     raeburn  1915: 	return &select_form($name,$value,\%domains);
1.169     www      1916:     }
                   1917: }
                   1918: 
1.282     albertel 1919: #-------------------------------------------
                   1920: 
                   1921: =pod
                   1922: 
1.519     raeburn  1923: =head1 Routines for form select boxes
                   1924: 
                   1925: =over 4
                   1926: 
1.648     raeburn  1927: =item * &multiple_select_form($name,$value,$size,$hash,$order)
1.282     albertel 1928: 
                   1929: Returns a string containing a <select> element int multiple mode
                   1930: 
                   1931: 
                   1932: Args:
                   1933:   $name - name of the <select> element
1.506     raeburn  1934:   $value - scalar or array ref of values that should already be selected
1.282     albertel 1935:   $size - number of rows long the select element is
1.283     albertel 1936:   $hash - the elements should be 'option' => 'shown text'
1.282     albertel 1937:           (shown text should already have been &mt())
1.506     raeburn  1938:   $order - (optional) array ref of the order to show the elements in
1.283     albertel 1939: 
1.282     albertel 1940: =cut
                   1941: 
                   1942: #-------------------------------------------
1.169     www      1943: sub multiple_select_form {
1.284     albertel 1944:     my ($name,$value,$size,$hash,$order)=@_;
1.169     www      1945:     my %selected = map { $_ => 1 } ref($value)?@{$value}:($value);
                   1946:     my $output='';
1.191     matthew  1947:     if (! defined($size)) {
                   1948:         $size = 4;
1.283     albertel 1949:         if (scalar(keys(%$hash))<4) {
                   1950:             $size = scalar(keys(%$hash));
1.191     matthew  1951:         }
                   1952:     }
1.734     bisitz   1953:     $output.="\n".'<select name="'.$name.'" size="'.$size.'" multiple="multiple">';
1.501     banghart 1954:     my @order;
1.506     raeburn  1955:     if (ref($order) eq 'ARRAY')  {
                   1956:         @order = @{$order};
                   1957:     } else {
                   1958:         @order = sort(keys(%$hash));
1.501     banghart 1959:     }
                   1960:     if (exists($$hash{'select_form_order'})) {
                   1961:         @order = @{$$hash{'select_form_order'}};
                   1962:     }
                   1963:         
1.284     albertel 1964:     foreach my $key (@order) {
1.356     albertel 1965:         $output.='<option value="'.&HTML::Entities::encode($key,'"<>&').'" ';
1.284     albertel 1966:         $output.='selected="selected" ' if ($selected{$key});
                   1967:         $output.='>'.$hash->{$key}."</option>\n";
1.169     www      1968:     }
                   1969:     $output.="</select>\n";
                   1970:     return $output;
                   1971: }
                   1972: 
1.88      www      1973: #-------------------------------------------
                   1974: 
                   1975: =pod
                   1976: 
1.970     raeburn  1977: =item * &select_form($defdom,$name,$hashref,$onchange)
1.88      www      1978: 
                   1979: Returns a string containing a <select name='$name' size='1'> form to 
1.970     raeburn  1980: allow a user to select options from a ref to a hash containing:
                   1981: option_name => displayed text. An optional $onchange can include
                   1982: a javascript onchange item, e.g., onchange="this.form.submit();"  
                   1983: 
1.88      www      1984: See lonrights.pm for an example invocation and use.
                   1985: 
                   1986: =cut
                   1987: 
                   1988: #-------------------------------------------
                   1989: sub select_form {
1.970     raeburn  1990:     my ($def,$name,$hashref,$onchange) = @_;
                   1991:     return unless (ref($hashref) eq 'HASH');
                   1992:     if ($onchange) {
                   1993:         $onchange = ' onchange="'.$onchange.'"';
                   1994:     }
                   1995:     my $selectform = "<select name=\"$name\" size=\"1\"$onchange>\n";
1.128     albertel 1996:     my @keys;
1.970     raeburn  1997:     if (exists($hashref->{'select_form_order'})) {
                   1998: 	@keys=@{$hashref->{'select_form_order'}};
1.128     albertel 1999:     } else {
1.970     raeburn  2000: 	@keys=sort(keys(%{$hashref}));
1.128     albertel 2001:     }
1.356     albertel 2002:     foreach my $key (@keys) {
                   2003:         $selectform.=
                   2004: 	    '<option value="'.&HTML::Entities::encode($key,'"<>&').'" '.
                   2005:             ($key eq $def ? 'selected="selected" ' : '').
1.970     raeburn  2006:                 ">".$hashref->{$key}."</option>\n";
1.88      www      2007:     }
                   2008:     $selectform.="</select>";
                   2009:     return $selectform;
                   2010: }
                   2011: 
1.475     www      2012: # For display filters
                   2013: 
                   2014: sub display_filter {
1.1074    raeburn  2015:     my ($context) = @_;
1.475     www      2016:     if (!$env{'form.show'}) { $env{'form.show'}=10; }
1.477     www      2017:     if (!$env{'form.displayfilter'}) { $env{'form.displayfilter'}='currentfolder'; }
1.1074    raeburn  2018:     my $phraseinput = 'hidden';
                   2019:     my $includeinput = 'hidden';
                   2020:     my ($checked,$includetypestext);
                   2021:     if ($env{'form.displayfilter'} eq 'containing') {
                   2022:         $phraseinput = 'text'; 
                   2023:         if ($context eq 'parmslog') {
                   2024:             $includeinput = 'checkbox';
                   2025:             if ($env{'form.includetypes'}) {
                   2026:                 $checked = ' checked="checked"';
                   2027:             }
                   2028:             $includetypestext = &mt('Include parameter types');
                   2029:         }
                   2030:     } else {
                   2031:         $includetypestext = '&nbsp;';
                   2032:     }
                   2033:     my ($additional,$secondid,$thirdid);
                   2034:     if ($context eq 'parmslog') {
                   2035:         $additional = 
                   2036:             '<label><input type="'.$includeinput.'" name="includetypes"'. 
                   2037:             $checked.' name="includetypes" value="1" id="includetypes" />'.
                   2038:             '&nbsp;<span id="includetypestext">'.$includetypestext.'</span>'.
                   2039:             '</label>';
                   2040:         $secondid = 'includetypes';
                   2041:         $thirdid = 'includetypestext';
                   2042:     }
                   2043:     my $onchange = "javascript:toggleHistoryOptions(this,'containingphrase','$context',
                   2044:                                                     '$secondid','$thirdid')";
                   2045:     return '<span class="LC_nobreak"><label>'.&mt('Records: [_1]',
1.475     www      2046: 			       &Apache::lonmeta::selectbox('show',$env{'form.show'},undef,
                   2047: 							   (&mt('all'),10,20,50,100,1000,10000))).
1.714     bisitz   2048: 	   '</label></span> <span class="LC_nobreak">'.
1.1074    raeburn  2049:            &mt('Filter: [_1]',
1.477     www      2050: 	   &select_form($env{'form.displayfilter'},
                   2051: 			'displayfilter',
1.970     raeburn  2052: 			{'currentfolder' => 'Current folder/page',
1.477     www      2053: 			 'containing' => 'Containing phrase',
1.1074    raeburn  2054: 			 'none' => 'None'},$onchange)).'&nbsp;'.
                   2055: 			 '<input type="'.$phraseinput.'" name="containingphrase" id="containingphrase" size="30" value="'.
                   2056:                          &HTML::Entities::encode($env{'form.containingphrase'}).
                   2057:                          '" />'.$additional;
                   2058: }
                   2059: 
                   2060: sub display_filter_js {
                   2061:     my $includetext = &mt('Include parameter types');
                   2062:     return <<"ENDJS";
                   2063:   
                   2064: function toggleHistoryOptions(setter,firstid,context,secondid,thirdid) {
                   2065:     var firstType = 'hidden';
                   2066:     if (setter.options[setter.selectedIndex].value == 'containing') {
                   2067:         firstType = 'text';
                   2068:     }
                   2069:     firstObject = document.getElementById(firstid);
                   2070:     if (typeof(firstObject) == 'object') {
                   2071:         if (firstObject.type != firstType) {
                   2072:             changeInputType(firstObject,firstType);
                   2073:         }
                   2074:     }
                   2075:     if (context == 'parmslog') {
                   2076:         var secondType = 'hidden';
                   2077:         if (firstType == 'text') {
                   2078:             secondType = 'checkbox';
                   2079:         }
                   2080:         secondObject = document.getElementById(secondid);  
                   2081:         if (typeof(secondObject) == 'object') {
                   2082:             if (secondObject.type != secondType) {
                   2083:                 changeInputType(secondObject,secondType);
                   2084:             }
                   2085:         }
                   2086:         var textItem = document.getElementById(thirdid);
                   2087:         var currtext = textItem.innerHTML;
                   2088:         var newtext;
                   2089:         if (firstType == 'text') {
                   2090:             newtext = '$includetext';
                   2091:         } else {
                   2092:             newtext = '&nbsp;';
                   2093:         }
                   2094:         if (currtext != newtext) {
                   2095:             textItem.innerHTML = newtext;
                   2096:         }
                   2097:     }
                   2098:     return;
                   2099: }
                   2100: 
                   2101: function changeInputType(oldObject,newType) {
                   2102:     var newObject = document.createElement('input');
                   2103:     newObject.type = newType;
                   2104:     if (oldObject.size) {
                   2105:         newObject.size = oldObject.size;
                   2106:     }
                   2107:     if (oldObject.value) {
                   2108:         newObject.value = oldObject.value;
                   2109:     }
                   2110:     if (oldObject.name) {
                   2111:         newObject.name = oldObject.name;
                   2112:     }
                   2113:     if (oldObject.id) {
                   2114:         newObject.id = oldObject.id;
                   2115:     }
                   2116:     oldObject.parentNode.replaceChild(newObject,oldObject);
                   2117:     return;
                   2118: }
                   2119: 
                   2120: ENDJS
1.475     www      2121: }
                   2122: 
1.167     www      2123: sub gradeleveldescription {
                   2124:     my $gradelevel=shift;
                   2125:     my %gradelevels=(0 => 'Not specified',
                   2126: 		     1 => 'Grade 1',
                   2127: 		     2 => 'Grade 2',
                   2128: 		     3 => 'Grade 3',
                   2129: 		     4 => 'Grade 4',
                   2130: 		     5 => 'Grade 5',
                   2131: 		     6 => 'Grade 6',
                   2132: 		     7 => 'Grade 7',
                   2133: 		     8 => 'Grade 8',
                   2134: 		     9 => 'Grade 9',
                   2135: 		     10 => 'Grade 10',
                   2136: 		     11 => 'Grade 11',
                   2137: 		     12 => 'Grade 12',
                   2138: 		     13 => 'Grade 13',
                   2139: 		     14 => '100 Level',
                   2140: 		     15 => '200 Level',
                   2141: 		     16 => '300 Level',
                   2142: 		     17 => '400 Level',
                   2143: 		     18 => 'Graduate Level');
                   2144:     return &mt($gradelevels{$gradelevel});
                   2145: }
                   2146: 
1.163     www      2147: sub select_level_form {
                   2148:     my ($deflevel,$name)=@_;
                   2149:     unless ($deflevel) { $deflevel=0; }
1.167     www      2150:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
                   2151:     for (my $i=0; $i<=18; $i++) {
                   2152:         $selectform.="<option value=\"$i\" ".
1.253     albertel 2153:             ($i==$deflevel ? 'selected="selected" ' : '').
1.167     www      2154:                 ">".&gradeleveldescription($i)."</option>\n";
                   2155:     }
                   2156:     $selectform.="</select>";
                   2157:     return $selectform;
1.163     www      2158: }
1.167     www      2159: 
1.35      matthew  2160: #-------------------------------------------
                   2161: 
1.45      matthew  2162: =pod
                   2163: 
1.910     raeburn  2164: =item * &select_dom_form($defdom,$name,$includeempty,$showdomdesc,$onchange,$incdoms)
1.35      matthew  2165: 
                   2166: Returns a string containing a <select name='$name' size='1'> form to 
                   2167: allow a user to select the domain to preform an operation in.  
                   2168: See loncreateuser.pm for an example invocation and use.
                   2169: 
1.90      www      2170: If the $includeempty flag is set, it also includes an empty choice ("no domain
                   2171: selected");
                   2172: 
1.743     raeburn  2173: If the $showdomdesc flag is set, the domain name is followed by the domain description.
                   2174: 
1.910     raeburn  2175: The optional $onchange argument specifies what should occur if the domain selector is changed, e.g., 'this.form.submit()' if the form is to be automatically submitted.
                   2176: 
                   2177: The optional $incdoms is a reference to an array of domains which will be the only available options. 
1.563     raeburn  2178: 
1.35      matthew  2179: =cut
                   2180: 
                   2181: #-------------------------------------------
1.34      matthew  2182: sub select_dom_form {
1.910     raeburn  2183:     my ($defdom,$name,$includeempty,$showdomdesc,$onchange,$incdoms) = @_;
1.872     raeburn  2184:     if ($onchange) {
1.874     raeburn  2185:         $onchange = ' onchange="'.$onchange.'"';
1.743     raeburn  2186:     }
1.910     raeburn  2187:     my @domains;
                   2188:     if (ref($incdoms) eq 'ARRAY') {
                   2189:         @domains = sort {lc($a) cmp lc($b)} (@{$incdoms});
                   2190:     } else {
                   2191:         @domains = sort {lc($a) cmp lc($b)} (&Apache::lonnet::all_domains());
                   2192:     }
1.90      www      2193:     if ($includeempty) { @domains=('',@domains); }
1.743     raeburn  2194:     my $selectdomain = "<select name=\"$name\" size=\"1\"$onchange>\n";
1.356     albertel 2195:     foreach my $dom (@domains) {
                   2196:         $selectdomain.="<option value=\"$dom\" ".
1.563     raeburn  2197:             ($dom eq $defdom ? 'selected="selected" ' : '').'>'.$dom;
                   2198:         if ($showdomdesc) {
                   2199:             if ($dom ne '') {
                   2200:                 my $domdesc = &Apache::lonnet::domain($dom,'description');
                   2201:                 if ($domdesc ne '') {
                   2202:                     $selectdomain .= ' ('.$domdesc.')';
                   2203:                 }
                   2204:             } 
                   2205:         }
                   2206:         $selectdomain .= "</option>\n";
1.34      matthew  2207:     }
                   2208:     $selectdomain.="</select>";
                   2209:     return $selectdomain;
                   2210: }
                   2211: 
1.35      matthew  2212: #-------------------------------------------
                   2213: 
1.45      matthew  2214: =pod
                   2215: 
1.648     raeburn  2216: =item * &home_server_form_item($domain,$name,$defaultflag)
1.35      matthew  2217: 
1.586     raeburn  2218: input: 4 arguments (two required, two optional) - 
                   2219:     $domain - domain of new user
                   2220:     $name - name of form element
                   2221:     $default - Value of 'default' causes a default item to be first 
                   2222:                             option, and selected by default. 
                   2223:     $hide - Value of 'hide' causes hiding of the name of the server, 
                   2224:                             if 1 server found, or default, if 0 found.
1.594     raeburn  2225: output: returns 2 items: 
1.586     raeburn  2226: (a) form element which contains either:
                   2227:    (i) <select name="$name">
                   2228:         <option value="$hostid1">$hostid $servers{$hostid}</option>
                   2229:         <option value="$hostid2">$hostid $servers{$hostid}</option>       
                   2230:        </select>
                   2231:        form item if there are multiple library servers in $domain, or
                   2232:    (ii) an <input type="hidden" name="$name" value="$hostid" /> form item 
                   2233:        if there is only one library server in $domain.
                   2234: 
                   2235: (b) number of library servers found.
                   2236: 
                   2237: See loncreateuser.pm for example of use.
1.35      matthew  2238: 
                   2239: =cut
                   2240: 
                   2241: #-------------------------------------------
1.586     raeburn  2242: sub home_server_form_item {
                   2243:     my ($domain,$name,$default,$hide) = @_;
1.513     albertel 2244:     my %servers = &Apache::lonnet::get_servers($domain,'library');
1.586     raeburn  2245:     my $result;
                   2246:     my $numlib = keys(%servers);
                   2247:     if ($numlib > 1) {
                   2248:         $result .= '<select name="'.$name.'" />'."\n";
                   2249:         if ($default) {
1.804     bisitz   2250:             $result .= '<option value="default" selected="selected">'.&mt('default').
1.586     raeburn  2251:                        '</option>'."\n";
                   2252:         }
                   2253:         foreach my $hostid (sort(keys(%servers))) {
                   2254:             $result.= '<option value="'.$hostid.'">'.
                   2255: 	              $hostid.' '.$servers{$hostid}."</option>\n";
                   2256:         }
                   2257:         $result .= '</select>'."\n";
                   2258:     } elsif ($numlib == 1) {
                   2259:         my $hostid;
                   2260:         foreach my $item (keys(%servers)) {
                   2261:             $hostid = $item;
                   2262:         }
                   2263:         $result .= '<input type="hidden" name="'.$name.'" value="'.
                   2264:                    $hostid.'" />';
                   2265:                    if (!$hide) {
                   2266:                        $result .= $hostid.' '.$servers{$hostid};
                   2267:                    }
                   2268:                    $result .= "\n";
                   2269:     } elsif ($default) {
                   2270:         $result .= '<input type="hidden" name="'.$name.
                   2271:                    '" value="default" />';
                   2272:                    if (!$hide) {
                   2273:                        $result .= &mt('default');
                   2274:                    }
                   2275:                    $result .= "\n";
1.33      matthew  2276:     }
1.586     raeburn  2277:     return ($result,$numlib);
1.33      matthew  2278: }
1.112     bowersj2 2279: 
                   2280: =pod
                   2281: 
1.534     albertel 2282: =back 
                   2283: 
1.112     bowersj2 2284: =cut
1.87      matthew  2285: 
                   2286: ###############################################################
1.112     bowersj2 2287: ##                  Decoding User Agent                      ##
1.87      matthew  2288: ###############################################################
                   2289: 
                   2290: =pod
                   2291: 
1.112     bowersj2 2292: =head1 Decoding the User Agent
                   2293: 
                   2294: =over 4
                   2295: 
                   2296: =item * &decode_user_agent()
1.87      matthew  2297: 
                   2298: Inputs: $r
                   2299: 
                   2300: Outputs:
                   2301: 
                   2302: =over 4
                   2303: 
1.112     bowersj2 2304: =item * $httpbrowser
1.87      matthew  2305: 
1.112     bowersj2 2306: =item * $clientbrowser
1.87      matthew  2307: 
1.112     bowersj2 2308: =item * $clientversion
1.87      matthew  2309: 
1.112     bowersj2 2310: =item * $clientmathml
1.87      matthew  2311: 
1.112     bowersj2 2312: =item * $clientunicode
1.87      matthew  2313: 
1.112     bowersj2 2314: =item * $clientos
1.87      matthew  2315: 
                   2316: =back
                   2317: 
1.157     matthew  2318: =back 
                   2319: 
1.87      matthew  2320: =cut
                   2321: 
                   2322: ###############################################################
                   2323: ###############################################################
                   2324: sub decode_user_agent {
1.247     albertel 2325:     my ($r)=@_;
1.87      matthew  2326:     my @browsertype=split(/\&/,$Apache::lonnet::perlvar{"lonBrowsDet"});
                   2327:     my %mathcap=split(/\&/,$$Apache::lonnet::perlvar{"lonMathML"});
                   2328:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
1.247     albertel 2329:     if (!$httpbrowser && $r) { $httpbrowser=$r->header_in('User-Agent'); }
1.87      matthew  2330:     my $clientbrowser='unknown';
                   2331:     my $clientversion='0';
                   2332:     my $clientmathml='';
                   2333:     my $clientunicode='0';
                   2334:     for (my $i=0;$i<=$#browsertype;$i++) {
                   2335:         my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\:/,$browsertype[$i]);
                   2336: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
                   2337: 	    $clientbrowser=$bname;
                   2338:             $httpbrowser=~/$vreg/i;
                   2339: 	    $clientversion=$1;
                   2340:             $clientmathml=($clientversion>=$minv);
                   2341:             $clientunicode=($clientversion>=$univ);
                   2342: 	}
                   2343:     }
                   2344:     my $clientos='unknown';
                   2345:     if (($httpbrowser=~/linux/i) ||
                   2346:         ($httpbrowser=~/unix/i) ||
                   2347:         ($httpbrowser=~/ux/i) ||
                   2348:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
                   2349:     if (($httpbrowser=~/vax/i) ||
                   2350:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
                   2351:     if ($httpbrowser=~/next/i) { $clientos='next'; }
                   2352:     if (($httpbrowser=~/mac/i) ||
                   2353:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
                   2354:     if ($httpbrowser=~/win/i) { $clientos='win'; }
                   2355:     if ($httpbrowser=~/embed/i) { $clientos='pda'; }
                   2356:     return ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                   2357:             $clientunicode,$clientos,);
                   2358: }
                   2359: 
1.32      matthew  2360: ###############################################################
                   2361: ##    Authentication changing form generation subroutines    ##
                   2362: ###############################################################
                   2363: ##
                   2364: ## All of the authform_xxxxxxx subroutines take their inputs in a
                   2365: ## hash, and have reasonable default values.
                   2366: ##
                   2367: ##    formname = the name given in the <form> tag.
1.35      matthew  2368: #-------------------------------------------
                   2369: 
1.45      matthew  2370: =pod
                   2371: 
1.112     bowersj2 2372: =head1 Authentication Routines
                   2373: 
                   2374: =over 4
                   2375: 
1.648     raeburn  2376: =item * &authform_xxxxxx()
1.35      matthew  2377: 
                   2378: The authform_xxxxxx subroutines provide javascript and html forms which 
                   2379: handle some of the conveniences required for authentication forms.  
                   2380: This is not an optimal method, but it works.  
                   2381: 
                   2382: =over 4
                   2383: 
1.112     bowersj2 2384: =item * authform_header
1.35      matthew  2385: 
1.112     bowersj2 2386: =item * authform_authorwarning
1.35      matthew  2387: 
1.112     bowersj2 2388: =item * authform_nochange
1.35      matthew  2389: 
1.112     bowersj2 2390: =item * authform_kerberos
1.35      matthew  2391: 
1.112     bowersj2 2392: =item * authform_internal
1.35      matthew  2393: 
1.112     bowersj2 2394: =item * authform_filesystem
1.35      matthew  2395: 
                   2396: =back
                   2397: 
1.648     raeburn  2398: See loncreateuser.pm for invocation and use examples.
1.157     matthew  2399: 
1.35      matthew  2400: =cut
                   2401: 
                   2402: #-------------------------------------------
1.32      matthew  2403: sub authform_header{  
                   2404:     my %in = (
                   2405:         formname => 'cu',
1.80      albertel 2406:         kerb_def_dom => '',
1.32      matthew  2407:         @_,
                   2408:     );
                   2409:     $in{'formname'} = 'document.' . $in{'formname'};
                   2410:     my $result='';
1.80      albertel 2411: 
                   2412: #---------------------------------------------- Code for upper case translation
                   2413:     my $Javascript_toUpperCase;
                   2414:     unless ($in{kerb_def_dom}) {
                   2415:         $Javascript_toUpperCase =<<"END";
                   2416:         switch (choice) {
                   2417:            case 'krb': currentform.elements[choicearg].value =
                   2418:                currentform.elements[choicearg].value.toUpperCase();
                   2419:                break;
                   2420:            default:
                   2421:         }
                   2422: END
                   2423:     } else {
                   2424:         $Javascript_toUpperCase = "";
                   2425:     }
                   2426: 
1.165     raeburn  2427:     my $radioval = "'nochange'";
1.591     raeburn  2428:     if (defined($in{'curr_authtype'})) {
                   2429:         if ($in{'curr_authtype'} ne '') {
                   2430:             $radioval = "'".$in{'curr_authtype'}."arg'";
                   2431:         }
1.174     matthew  2432:     }
1.165     raeburn  2433:     my $argfield = 'null';
1.591     raeburn  2434:     if (defined($in{'mode'})) {
1.165     raeburn  2435:         if ($in{'mode'} eq 'modifycourse')  {
1.591     raeburn  2436:             if (defined($in{'curr_autharg'})) {
                   2437:                 if ($in{'curr_autharg'} ne '') {
1.165     raeburn  2438:                     $argfield = "'$in{'curr_autharg'}'";
                   2439:                 }
                   2440:             }
                   2441:         }
                   2442:     }
                   2443: 
1.32      matthew  2444:     $result.=<<"END";
                   2445: var current = new Object();
1.165     raeburn  2446: current.radiovalue = $radioval;
                   2447: current.argfield = $argfield;
1.32      matthew  2448: 
                   2449: function changed_radio(choice,currentform) {
                   2450:     var choicearg = choice + 'arg';
                   2451:     // If a radio button in changed, we need to change the argfield
                   2452:     if (current.radiovalue != choice) {
                   2453:         current.radiovalue = choice;
                   2454:         if (current.argfield != null) {
                   2455:             currentform.elements[current.argfield].value = '';
                   2456:         }
                   2457:         if (choice == 'nochange') {
                   2458:             current.argfield = null;
                   2459:         } else {
                   2460:             current.argfield = choicearg;
                   2461:             switch(choice) {
                   2462:                 case 'krb': 
                   2463:                     currentform.elements[current.argfield].value = 
                   2464:                         "$in{'kerb_def_dom'}";
                   2465:                 break;
                   2466:               default:
                   2467:                 break;
                   2468:             }
                   2469:         }
                   2470:     }
                   2471:     return;
                   2472: }
1.22      www      2473: 
1.32      matthew  2474: function changed_text(choice,currentform) {
                   2475:     var choicearg = choice + 'arg';
                   2476:     if (currentform.elements[choicearg].value !='') {
1.80      albertel 2477:         $Javascript_toUpperCase
1.32      matthew  2478:         // clear old field
                   2479:         if ((current.argfield != choicearg) && (current.argfield != null)) {
                   2480:             currentform.elements[current.argfield].value = '';
                   2481:         }
                   2482:         current.argfield = choicearg;
                   2483:     }
                   2484:     set_auth_radio_buttons(choice,currentform);
                   2485:     return;
1.20      www      2486: }
1.32      matthew  2487: 
                   2488: function set_auth_radio_buttons(newvalue,currentform) {
1.986     raeburn  2489:     var numauthchoices = currentform.login.length;
                   2490:     if (typeof numauthchoices  == "undefined") {
                   2491:         return;
                   2492:     } 
1.32      matthew  2493:     var i=0;
1.986     raeburn  2494:     while (i < numauthchoices) {
1.32      matthew  2495:         if (currentform.login[i].value == newvalue) { break; }
                   2496:         i++;
                   2497:     }
1.986     raeburn  2498:     if (i == numauthchoices) {
1.32      matthew  2499:         return;
                   2500:     }
                   2501:     current.radiovalue = newvalue;
                   2502:     currentform.login[i].checked = true;
                   2503:     return;
                   2504: }
                   2505: END
                   2506:     return $result;
                   2507: }
                   2508: 
1.1075.2.20  raeburn  2509: sub authform_authorwarning {
1.32      matthew  2510:     my $result='';
1.144     matthew  2511:     $result='<i>'.
                   2512:         &mt('As a general rule, only authors or co-authors should be '.
                   2513:             'filesystem authenticated '.
                   2514:             '(which allows access to the server filesystem).')."</i>\n";
1.32      matthew  2515:     return $result;
                   2516: }
                   2517: 
1.1075.2.20  raeburn  2518: sub authform_nochange {
1.32      matthew  2519:     my %in = (
                   2520:               formname => 'document.cu',
                   2521:               kerb_def_dom => 'MSU.EDU',
                   2522:               @_,
                   2523:           );
1.1075.2.20  raeburn  2524:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'}); 
1.586     raeburn  2525:     my $result;
1.1075.2.20  raeburn  2526:     if (!$authnum) {
                   2527:         $result = &mt('Under your current role you are not permitted to change login settings for this user');
1.586     raeburn  2528:     } else {
                   2529:         $result = '<label>'.&mt('[_1] Do not change login data',
                   2530:                   '<input type="radio" name="login" value="nochange" '.
                   2531:                   'checked="checked" onclick="'.
1.281     albertel 2532:             "javascript:changed_radio('nochange',$in{'formname'});".'" />').
                   2533: 	    '</label>';
1.586     raeburn  2534:     }
1.32      matthew  2535:     return $result;
                   2536: }
                   2537: 
1.591     raeburn  2538: sub authform_kerberos {
1.32      matthew  2539:     my %in = (
                   2540:               formname => 'document.cu',
                   2541:               kerb_def_dom => 'MSU.EDU',
1.80      albertel 2542:               kerb_def_auth => 'krb4',
1.32      matthew  2543:               @_,
                   2544:               );
1.586     raeburn  2545:     my ($check4,$check5,$krbcheck,$krbarg,$krbver,$result,$authtype,
                   2546:         $autharg,$jscall);
1.1075.2.20  raeburn  2547:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.80      albertel 2548:     if ($in{'kerb_def_auth'} eq 'krb5') {
1.772     bisitz   2549:        $check5 = ' checked="checked"';
1.80      albertel 2550:     } else {
1.772     bisitz   2551:        $check4 = ' checked="checked"';
1.80      albertel 2552:     }
1.165     raeburn  2553:     $krbarg = $in{'kerb_def_dom'};
1.591     raeburn  2554:     if (defined($in{'curr_authtype'})) {
                   2555:         if ($in{'curr_authtype'} eq 'krb') {
1.772     bisitz   2556:             $krbcheck = ' checked="checked"';
1.623     raeburn  2557:             if (defined($in{'mode'})) {
                   2558:                 if ($in{'mode'} eq 'modifyuser') {
                   2559:                     $krbcheck = '';
                   2560:                 }
                   2561:             }
1.591     raeburn  2562:             if (defined($in{'curr_kerb_ver'})) {
                   2563:                 if ($in{'curr_krb_ver'} eq '5') {
1.772     bisitz   2564:                     $check5 = ' checked="checked"';
1.591     raeburn  2565:                     $check4 = '';
                   2566:                 } else {
1.772     bisitz   2567:                     $check4 = ' checked="checked"';
1.591     raeburn  2568:                     $check5 = '';
                   2569:                 }
1.586     raeburn  2570:             }
1.591     raeburn  2571:             if (defined($in{'curr_autharg'})) {
1.165     raeburn  2572:                 $krbarg = $in{'curr_autharg'};
                   2573:             }
1.586     raeburn  2574:             if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
1.591     raeburn  2575:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2576:                     $result = 
                   2577:     &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
                   2578:         $in{'curr_autharg'},$krbver);
                   2579:                 } else {
                   2580:                     $result =
                   2581:     &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
                   2582:                 }
                   2583:                 return $result; 
                   2584:             }
                   2585:         }
                   2586:     } else {
                   2587:         if ($authnum == 1) {
1.784     bisitz   2588:             $authtype = '<input type="hidden" name="login" value="krb" />';
1.165     raeburn  2589:         }
                   2590:     }
1.586     raeburn  2591:     if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
                   2592:         return;
1.587     raeburn  2593:     } elsif ($authtype eq '') {
1.591     raeburn  2594:         if (defined($in{'mode'})) {
1.587     raeburn  2595:             if ($in{'mode'} eq 'modifycourse') {
                   2596:                 if ($authnum == 1) {
1.1075.2.20  raeburn  2597:                     $authtype = '<input type="radio" name="login" value="krb" />';
1.587     raeburn  2598:                 }
                   2599:             }
                   2600:         }
1.586     raeburn  2601:     }
                   2602:     $jscall = "javascript:changed_radio('krb',$in{'formname'});";
                   2603:     if ($authtype eq '') {
                   2604:         $authtype = '<input type="radio" name="login" value="krb" '.
                   2605:                     'onclick="'.$jscall.'" onchange="'.$jscall.'"'.
                   2606:                     $krbcheck.' />';
                   2607:     }
                   2608:     if (($can_assign{'krb4'} && $can_assign{'krb5'}) ||
1.1075.2.20  raeburn  2609:         ($can_assign{'krb4'} && !$can_assign{'krb5'} &&
1.586     raeburn  2610:          $in{'curr_authtype'} eq 'krb5') ||
1.1075.2.20  raeburn  2611:         (!$can_assign{'krb4'} && $can_assign{'krb5'} &&
1.586     raeburn  2612:          $in{'curr_authtype'} eq 'krb4')) {
                   2613:         $result .= &mt
1.144     matthew  2614:         ('[_1] Kerberos authenticated with domain [_2] '.
1.281     albertel 2615:          '[_3] Version 4 [_4] Version 5 [_5]',
1.586     raeburn  2616:          '<label>'.$authtype,
1.281     albertel 2617:          '</label><input type="text" size="10" name="krbarg" '.
1.165     raeburn  2618:              'value="'.$krbarg.'" '.
1.144     matthew  2619:              'onchange="'.$jscall.'" />',
1.281     albertel 2620:          '<label><input type="radio" name="krbver" value="4" '.$check4.' />',
                   2621:          '</label><label><input type="radio" name="krbver" value="5" '.$check5.' />',
                   2622: 	 '</label>');
1.586     raeburn  2623:     } elsif ($can_assign{'krb4'}) {
                   2624:         $result .= &mt
                   2625:         ('[_1] Kerberos authenticated with domain [_2] '.
                   2626:          '[_3] Version 4 [_4]',
                   2627:          '<label>'.$authtype,
                   2628:          '</label><input type="text" size="10" name="krbarg" '.
                   2629:              'value="'.$krbarg.'" '.
                   2630:              'onchange="'.$jscall.'" />',
                   2631:          '<label><input type="hidden" name="krbver" value="4" />',
                   2632:          '</label>');
                   2633:     } elsif ($can_assign{'krb5'}) {
                   2634:         $result .= &mt
                   2635:         ('[_1] Kerberos authenticated with domain [_2] '.
                   2636:          '[_3] Version 5 [_4]',
                   2637:          '<label>'.$authtype,
                   2638:          '</label><input type="text" size="10" name="krbarg" '.
                   2639:              'value="'.$krbarg.'" '.
                   2640:              'onchange="'.$jscall.'" />',
                   2641:          '<label><input type="hidden" name="krbver" value="5" />',
                   2642:          '</label>');
                   2643:     }
1.32      matthew  2644:     return $result;
                   2645: }
                   2646: 
1.1075.2.20  raeburn  2647: sub authform_internal {
1.586     raeburn  2648:     my %in = (
1.32      matthew  2649:                 formname => 'document.cu',
                   2650:                 kerb_def_dom => 'MSU.EDU',
                   2651:                 @_,
                   2652:                 );
1.586     raeburn  2653:     my ($intcheck,$intarg,$result,$authtype,$autharg,$jscall);
1.1075.2.20  raeburn  2654:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.591     raeburn  2655:     if (defined($in{'curr_authtype'})) {
                   2656:         if ($in{'curr_authtype'} eq 'int') {
1.586     raeburn  2657:             if ($can_assign{'int'}) {
1.772     bisitz   2658:                 $intcheck = 'checked="checked" ';
1.623     raeburn  2659:                 if (defined($in{'mode'})) {
                   2660:                     if ($in{'mode'} eq 'modifyuser') {
                   2661:                         $intcheck = '';
                   2662:                     }
                   2663:                 }
1.591     raeburn  2664:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2665:                     $intarg = $in{'curr_autharg'};
                   2666:                 }
                   2667:             } else {
                   2668:                 $result = &mt('Currently internally authenticated.');
                   2669:                 return $result;
1.165     raeburn  2670:             }
                   2671:         }
1.586     raeburn  2672:     } else {
                   2673:         if ($authnum == 1) {
1.784     bisitz   2674:             $authtype = '<input type="hidden" name="login" value="int" />';
1.586     raeburn  2675:         }
                   2676:     }
                   2677:     if (!$can_assign{'int'}) {
                   2678:         return;
1.587     raeburn  2679:     } elsif ($authtype eq '') {
1.591     raeburn  2680:         if (defined($in{'mode'})) {
1.587     raeburn  2681:             if ($in{'mode'} eq 'modifycourse') {
                   2682:                 if ($authnum == 1) {
1.1075.2.20  raeburn  2683:                     $authtype = '<input type="radio" name="login" value="int" />';
1.587     raeburn  2684:                 }
                   2685:             }
                   2686:         }
1.165     raeburn  2687:     }
1.586     raeburn  2688:     $jscall = "javascript:changed_radio('int',$in{'formname'});";
                   2689:     if ($authtype eq '') {
                   2690:         $authtype = '<input type="radio" name="login" value="int" '.$intcheck.
                   2691:                     ' onchange="'.$jscall.'" onclick="'.$jscall.'" />';
                   2692:     }
1.605     bisitz   2693:     $autharg = '<input type="password" size="10" name="intarg" value="'.
1.586     raeburn  2694:                $intarg.'" onchange="'.$jscall.'" />';
                   2695:     $result = &mt
1.144     matthew  2696:         ('[_1] Internally authenticated (with initial password [_2])',
1.586     raeburn  2697:          '<label>'.$authtype,'</label>'.$autharg);
1.824     bisitz   2698:     $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  2699:     return $result;
                   2700: }
                   2701: 
1.1075.2.20  raeburn  2702: sub authform_local {
1.32      matthew  2703:     my %in = (
                   2704:               formname => 'document.cu',
                   2705:               kerb_def_dom => 'MSU.EDU',
                   2706:               @_,
                   2707:               );
1.586     raeburn  2708:     my ($loccheck,$locarg,$result,$authtype,$autharg,$jscall);
1.1075.2.20  raeburn  2709:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.591     raeburn  2710:     if (defined($in{'curr_authtype'})) {
                   2711:         if ($in{'curr_authtype'} eq 'loc') {
1.586     raeburn  2712:             if ($can_assign{'loc'}) {
1.772     bisitz   2713:                 $loccheck = 'checked="checked" ';
1.623     raeburn  2714:                 if (defined($in{'mode'})) {
                   2715:                     if ($in{'mode'} eq 'modifyuser') {
                   2716:                         $loccheck = '';
                   2717:                     }
                   2718:                 }
1.591     raeburn  2719:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2720:                     $locarg = $in{'curr_autharg'};
                   2721:                 }
                   2722:             } else {
                   2723:                 $result = &mt('Currently using local (institutional) authentication.');
                   2724:                 return $result;
1.165     raeburn  2725:             }
                   2726:         }
1.586     raeburn  2727:     } else {
                   2728:         if ($authnum == 1) {
1.784     bisitz   2729:             $authtype = '<input type="hidden" name="login" value="loc" />';
1.586     raeburn  2730:         }
                   2731:     }
                   2732:     if (!$can_assign{'loc'}) {
                   2733:         return;
1.587     raeburn  2734:     } elsif ($authtype eq '') {
1.591     raeburn  2735:         if (defined($in{'mode'})) {
1.587     raeburn  2736:             if ($in{'mode'} eq 'modifycourse') {
                   2737:                 if ($authnum == 1) {
1.1075.2.20  raeburn  2738:                     $authtype = '<input type="radio" name="login" value="loc" />';
1.587     raeburn  2739:                 }
                   2740:             }
                   2741:         }
1.165     raeburn  2742:     }
1.586     raeburn  2743:     $jscall = "javascript:changed_radio('loc',$in{'formname'});";
                   2744:     if ($authtype eq '') {
                   2745:         $authtype = '<input type="radio" name="login" value="loc" '.
                   2746:                     $loccheck.' onchange="'.$jscall.'" onclick="'.
                   2747:                     $jscall.'" />';
                   2748:     }
                   2749:     $autharg = '<input type="text" size="10" name="locarg" value="'.
                   2750:                $locarg.'" onchange="'.$jscall.'" />';
                   2751:     $result = &mt('[_1] Local Authentication with argument [_2]',
                   2752:                   '<label>'.$authtype,'</label>'.$autharg);
1.32      matthew  2753:     return $result;
                   2754: }
                   2755: 
1.1075.2.20  raeburn  2756: sub authform_filesystem {
1.32      matthew  2757:     my %in = (
                   2758:               formname => 'document.cu',
                   2759:               kerb_def_dom => 'MSU.EDU',
                   2760:               @_,
                   2761:               );
1.586     raeburn  2762:     my ($fsyscheck,$result,$authtype,$autharg,$jscall);
1.1075.2.20  raeburn  2763:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.591     raeburn  2764:     if (defined($in{'curr_authtype'})) {
                   2765:         if ($in{'curr_authtype'} eq 'fsys') {
1.586     raeburn  2766:             if ($can_assign{'fsys'}) {
1.772     bisitz   2767:                 $fsyscheck = 'checked="checked" ';
1.623     raeburn  2768:                 if (defined($in{'mode'})) {
                   2769:                     if ($in{'mode'} eq 'modifyuser') {
                   2770:                         $fsyscheck = '';
                   2771:                     }
                   2772:                 }
1.586     raeburn  2773:             } else {
                   2774:                 $result = &mt('Currently Filesystem Authenticated.');
                   2775:                 return $result;
                   2776:             }           
                   2777:         }
                   2778:     } else {
                   2779:         if ($authnum == 1) {
1.784     bisitz   2780:             $authtype = '<input type="hidden" name="login" value="fsys" />';
1.586     raeburn  2781:         }
                   2782:     }
                   2783:     if (!$can_assign{'fsys'}) {
                   2784:         return;
1.587     raeburn  2785:     } elsif ($authtype eq '') {
1.591     raeburn  2786:         if (defined($in{'mode'})) {
1.587     raeburn  2787:             if ($in{'mode'} eq 'modifycourse') {
                   2788:                 if ($authnum == 1) {
1.1075.2.20  raeburn  2789:                     $authtype = '<input type="radio" name="login" value="fsys" />';
1.587     raeburn  2790:                 }
                   2791:             }
                   2792:         }
1.586     raeburn  2793:     }
                   2794:     $jscall = "javascript:changed_radio('fsys',$in{'formname'});";
                   2795:     if ($authtype eq '') {
                   2796:         $authtype = '<input type="radio" name="login" value="fsys" '.
                   2797:                     $fsyscheck.' onchange="'.$jscall.'" onclick="'.
                   2798:                     $jscall.'" />';
                   2799:     }
                   2800:     $autharg = '<input type="text" size="10" name="fsysarg" value=""'.
                   2801:                ' onchange="'.$jscall.'" />';
                   2802:     $result = &mt
1.144     matthew  2803:         ('[_1] Filesystem Authenticated (with initial password [_2])',
1.281     albertel 2804:          '<label><input type="radio" name="login" value="fsys" '.
1.586     raeburn  2805:          $fsyscheck.'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1.605     bisitz   2806:          '</label><input type="password" size="10" name="fsysarg" value="" '.
1.144     matthew  2807:                   'onchange="'.$jscall.'" />');
1.32      matthew  2808:     return $result;
                   2809: }
                   2810: 
1.586     raeburn  2811: sub get_assignable_auth {
                   2812:     my ($dom) = @_;
                   2813:     if ($dom eq '') {
                   2814:         $dom = $env{'request.role.domain'};
                   2815:     }
                   2816:     my %can_assign = (
                   2817:                           krb4 => 1,
                   2818:                           krb5 => 1,
                   2819:                           int  => 1,
                   2820:                           loc  => 1,
                   2821:                      );
                   2822:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2823:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   2824:         if (ref($domconfig{'usercreation'}{'authtypes'}) eq 'HASH') {
                   2825:             my $authhash = $domconfig{'usercreation'}{'authtypes'};
                   2826:             my $context;
                   2827:             if ($env{'request.role'} =~ /^au/) {
                   2828:                 $context = 'author';
                   2829:             } elsif ($env{'request.role'} =~ /^dc/) {
                   2830:                 $context = 'domain';
                   2831:             } elsif ($env{'request.course.id'}) {
                   2832:                 $context = 'course';
                   2833:             }
                   2834:             if ($context) {
                   2835:                 if (ref($authhash->{$context}) eq 'HASH') {
                   2836:                    %can_assign = %{$authhash->{$context}}; 
                   2837:                 }
                   2838:             }
                   2839:         }
                   2840:     }
                   2841:     my $authnum = 0;
                   2842:     foreach my $key (keys(%can_assign)) {
                   2843:         if ($can_assign{$key}) {
                   2844:             $authnum ++;
                   2845:         }
                   2846:     }
                   2847:     if ($can_assign{'krb4'} && $can_assign{'krb5'}) {
                   2848:         $authnum --;
                   2849:     }
                   2850:     return ($authnum,%can_assign);
                   2851: }
                   2852: 
1.80      albertel 2853: ###############################################################
                   2854: ##    Get Kerberos Defaults for Domain                 ##
                   2855: ###############################################################
                   2856: ##
                   2857: ## Returns default kerberos version and an associated argument
                   2858: ## as listed in file domain.tab. If not listed, provides
                   2859: ## appropriate default domain and kerberos version.
                   2860: ##
                   2861: #-------------------------------------------
                   2862: 
                   2863: =pod
                   2864: 
1.648     raeburn  2865: =item * &get_kerberos_defaults()
1.80      albertel 2866: 
                   2867: get_kerberos_defaults($target_domain) returns the default kerberos
1.641     raeburn  2868: version and domain. If not found, it defaults to version 4 and the 
                   2869: domain of the server.
1.80      albertel 2870: 
1.648     raeburn  2871: =over 4
                   2872: 
1.80      albertel 2873: ($def_version, $def_krb_domain) = &get_kerberos_defaults($target_domain);
                   2874: 
1.648     raeburn  2875: =back
                   2876: 
                   2877: =back
                   2878: 
1.80      albertel 2879: =cut
                   2880: 
                   2881: #-------------------------------------------
                   2882: sub get_kerberos_defaults {
                   2883:     my $domain=shift;
1.641     raeburn  2884:     my ($krbdef,$krbdefdom);
                   2885:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                   2886:     if (($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) {
                   2887:         $krbdef = $domdefaults{'auth_def'};
                   2888:         $krbdefdom = $domdefaults{'auth_arg_def'};
                   2889:     } else {
1.80      albertel 2890:         $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
                   2891:         my $krbdefdom=$1;
                   2892:         $krbdefdom=~tr/a-z/A-Z/;
                   2893:         $krbdef = "krb4";
                   2894:     }
                   2895:     return ($krbdef,$krbdefdom);
                   2896: }
1.112     bowersj2 2897: 
1.32      matthew  2898: 
1.46      matthew  2899: ###############################################################
                   2900: ##                Thesaurus Functions                        ##
                   2901: ###############################################################
1.20      www      2902: 
1.46      matthew  2903: =pod
1.20      www      2904: 
1.112     bowersj2 2905: =head1 Thesaurus Functions
                   2906: 
                   2907: =over 4
                   2908: 
1.648     raeburn  2909: =item * &initialize_keywords()
1.46      matthew  2910: 
                   2911: Initializes the package variable %Keywords if it is empty.  Uses the
                   2912: package variable $thesaurus_db_file.
                   2913: 
                   2914: =cut
                   2915: 
                   2916: ###################################################
                   2917: 
                   2918: sub initialize_keywords {
                   2919:     return 1 if (scalar keys(%Keywords));
                   2920:     # If we are here, %Keywords is empty, so fill it up
                   2921:     #   Make sure the file we need exists...
                   2922:     if (! -e $thesaurus_db_file) {
                   2923:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file".
                   2924:                                  " failed because it does not exist");
                   2925:         return 0;
                   2926:     }
                   2927:     #   Set up the hash as a database
                   2928:     my %thesaurus_db;
                   2929:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 2930:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  2931:         &Apache::lonnet::logthis("Could not tie \%thesaurus_db to ".
                   2932:                                  $thesaurus_db_file);
                   2933:         return 0;
                   2934:     } 
                   2935:     #  Get the average number of appearances of a word.
                   2936:     my $avecount = $thesaurus_db{'average.count'};
                   2937:     #  Put keywords (those that appear > average) into %Keywords
                   2938:     while (my ($word,$data)=each (%thesaurus_db)) {
                   2939:         my ($count,undef) = split /:/,$data;
                   2940:         $Keywords{$word}++ if ($count > $avecount);
                   2941:     }
                   2942:     untie %thesaurus_db;
                   2943:     # Remove special values from %Keywords.
1.356     albertel 2944:     foreach my $value ('total.count','average.count') {
                   2945:         delete($Keywords{$value}) if (exists($Keywords{$value}));
1.586     raeburn  2946:   }
1.46      matthew  2947:     return 1;
                   2948: }
                   2949: 
                   2950: ###################################################
                   2951: 
                   2952: =pod
                   2953: 
1.648     raeburn  2954: =item * &keyword($word)
1.46      matthew  2955: 
                   2956: Returns true if $word is a keyword.  A keyword is a word that appears more 
                   2957: than the average number of times in the thesaurus database.  Calls 
                   2958: &initialize_keywords
                   2959: 
                   2960: =cut
                   2961: 
                   2962: ###################################################
1.20      www      2963: 
                   2964: sub keyword {
1.46      matthew  2965:     return if (!&initialize_keywords());
                   2966:     my $word=lc(shift());
                   2967:     $word=~s/\W//g;
                   2968:     return exists($Keywords{$word});
1.20      www      2969: }
1.46      matthew  2970: 
                   2971: ###############################################################
                   2972: 
                   2973: =pod 
1.20      www      2974: 
1.648     raeburn  2975: =item * &get_related_words()
1.46      matthew  2976: 
1.160     matthew  2977: Look up a word in the thesaurus.  Takes a scalar argument and returns
1.46      matthew  2978: an array of words.  If the keyword is not in the thesaurus, an empty array
                   2979: will be returned.  The order of the words returned is determined by the
                   2980: database which holds them.
                   2981: 
                   2982: Uses global $thesaurus_db_file.
                   2983: 
1.1057    foxr     2984: 
1.46      matthew  2985: =cut
                   2986: 
                   2987: ###############################################################
                   2988: sub get_related_words {
                   2989:     my $keyword = shift;
                   2990:     my %thesaurus_db;
                   2991:     if (! -e $thesaurus_db_file) {
                   2992:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file ".
                   2993:                                  "failed because the file does not exist");
                   2994:         return ();
                   2995:     }
                   2996:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 2997:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  2998:         return ();
                   2999:     } 
                   3000:     my @Words=();
1.429     www      3001:     my $count=0;
1.46      matthew  3002:     if (exists($thesaurus_db{$keyword})) {
1.356     albertel 3003: 	# The first element is the number of times
                   3004: 	# the word appears.  We do not need it now.
1.429     www      3005: 	my (undef,@RelatedWords) = (split(/:/,$thesaurus_db{$keyword}));
                   3006: 	my (undef,$mostfrequentcount)=split(/\,/,$RelatedWords[0]);
                   3007: 	my $threshold=$mostfrequentcount/10;
                   3008:         foreach my $possibleword (@RelatedWords) {
                   3009:             my ($word,$wordcount)=split(/\,/,$possibleword);
                   3010:             if ($wordcount>$threshold) {
                   3011: 		push(@Words,$word);
                   3012:                 $count++;
                   3013:                 if ($count>10) { last; }
                   3014: 	    }
1.20      www      3015:         }
                   3016:     }
1.46      matthew  3017:     untie %thesaurus_db;
                   3018:     return @Words;
1.14      harris41 3019: }
1.46      matthew  3020: 
1.112     bowersj2 3021: =pod
                   3022: 
                   3023: =back
                   3024: 
                   3025: =cut
1.61      www      3026: 
                   3027: # -------------------------------------------------------------- Plaintext name
1.81      albertel 3028: =pod
                   3029: 
1.112     bowersj2 3030: =head1 User Name Functions
                   3031: 
                   3032: =over 4
                   3033: 
1.648     raeburn  3034: =item * &plainname($uname,$udom,$first)
1.81      albertel 3035: 
1.112     bowersj2 3036: Takes a users logon name and returns it as a string in
1.226     albertel 3037: "first middle last generation" form 
                   3038: if $first is set to 'lastname' then it returns it as
                   3039: 'lastname generation, firstname middlename' if their is a lastname
1.81      albertel 3040: 
                   3041: =cut
1.61      www      3042: 
1.295     www      3043: 
1.81      albertel 3044: ###############################################################
1.61      www      3045: sub plainname {
1.226     albertel 3046:     my ($uname,$udom,$first)=@_;
1.537     albertel 3047:     return if (!defined($uname) || !defined($udom));
1.295     www      3048:     my %names=&getnames($uname,$udom);
1.226     albertel 3049:     my $name=&Apache::lonnet::format_name($names{'firstname'},
                   3050: 					  $names{'middlename'},
                   3051: 					  $names{'lastname'},
                   3052: 					  $names{'generation'},$first);
                   3053:     $name=~s/^\s+//;
1.62      www      3054:     $name=~s/\s+$//;
                   3055:     $name=~s/\s+/ /g;
1.353     albertel 3056:     if ($name !~ /\S/) { $name=$uname.':'.$udom; }
1.62      www      3057:     return $name;
1.61      www      3058: }
1.66      www      3059: 
                   3060: # -------------------------------------------------------------------- Nickname
1.81      albertel 3061: =pod
                   3062: 
1.648     raeburn  3063: =item * &nickname($uname,$udom)
1.81      albertel 3064: 
                   3065: Gets a users name and returns it as a string as
                   3066: 
                   3067: "&quot;nickname&quot;"
1.66      www      3068: 
1.81      albertel 3069: if the user has a nickname or
                   3070: 
                   3071: "first middle last generation"
                   3072: 
                   3073: if the user does not
                   3074: 
                   3075: =cut
1.66      www      3076: 
                   3077: sub nickname {
                   3078:     my ($uname,$udom)=@_;
1.537     albertel 3079:     return if (!defined($uname) || !defined($udom));
1.295     www      3080:     my %names=&getnames($uname,$udom);
1.68      albertel 3081:     my $name=$names{'nickname'};
1.66      www      3082:     if ($name) {
                   3083:        $name='&quot;'.$name.'&quot;'; 
                   3084:     } else {
                   3085:        $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
                   3086: 	     $names{'lastname'}.' '.$names{'generation'};
                   3087:        $name=~s/\s+$//;
                   3088:        $name=~s/\s+/ /g;
                   3089:     }
                   3090:     return $name;
                   3091: }
                   3092: 
1.295     www      3093: sub getnames {
                   3094:     my ($uname,$udom)=@_;
1.537     albertel 3095:     return if (!defined($uname) || !defined($udom));
1.433     albertel 3096:     if ($udom eq 'public' && $uname eq 'public') {
                   3097: 	return ('lastname' => &mt('Public'));
                   3098:     }
1.295     www      3099:     my $id=$uname.':'.$udom;
                   3100:     my ($names,$cached)=&Apache::lonnet::is_cached_new('namescache',$id);
                   3101:     if ($cached) {
                   3102: 	return %{$names};
                   3103:     } else {
                   3104: 	my %loadnames=&Apache::lonnet::get('environment',
                   3105:                     ['firstname','middlename','lastname','generation','nickname'],
                   3106: 					 $udom,$uname);
                   3107: 	&Apache::lonnet::do_cache_new('namescache',$id,\%loadnames);
                   3108: 	return %loadnames;
                   3109:     }
                   3110: }
1.61      www      3111: 
1.542     raeburn  3112: # -------------------------------------------------------------------- getemails
1.648     raeburn  3113: 
1.542     raeburn  3114: =pod
                   3115: 
1.648     raeburn  3116: =item * &getemails($uname,$udom)
1.542     raeburn  3117: 
                   3118: Gets a user's email information and returns it as a hash with keys:
                   3119: notification, critnotification, permanentemail
                   3120: 
                   3121: For notification and critnotification, values are comma-separated lists 
1.648     raeburn  3122: of e-mail addresses; for permanentemail, value is a single e-mail address.
1.542     raeburn  3123:  
1.648     raeburn  3124: 
1.542     raeburn  3125: =cut
                   3126: 
1.648     raeburn  3127: 
1.466     albertel 3128: sub getemails {
                   3129:     my ($uname,$udom)=@_;
                   3130:     if ($udom eq 'public' && $uname eq 'public') {
                   3131: 	return;
                   3132:     }
1.467     www      3133:     if (!$udom) { $udom=$env{'user.domain'}; }
                   3134:     if (!$uname) { $uname=$env{'user.name'}; }
1.466     albertel 3135:     my $id=$uname.':'.$udom;
                   3136:     my ($names,$cached)=&Apache::lonnet::is_cached_new('emailscache',$id);
                   3137:     if ($cached) {
                   3138: 	return %{$names};
                   3139:     } else {
                   3140: 	my %loadnames=&Apache::lonnet::get('environment',
                   3141:                     			   ['notification','critnotification',
                   3142: 					    'permanentemail'],
                   3143: 					   $udom,$uname);
                   3144: 	&Apache::lonnet::do_cache_new('emailscache',$id,\%loadnames);
                   3145: 	return %loadnames;
                   3146:     }
                   3147: }
                   3148: 
1.551     albertel 3149: sub flush_email_cache {
                   3150:     my ($uname,$udom)=@_;
                   3151:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   3152:     if (!$uname) { $uname=$env{'user.name'};   }
                   3153:     return if ($udom eq 'public' && $uname eq 'public');
                   3154:     my $id=$uname.':'.$udom;
                   3155:     &Apache::lonnet::devalidate_cache_new('emailscache',$id);
                   3156: }
                   3157: 
1.728     raeburn  3158: # -------------------------------------------------------------------- getlangs
                   3159: 
                   3160: =pod
                   3161: 
                   3162: =item * &getlangs($uname,$udom)
                   3163: 
                   3164: Gets a user's language preference and returns it as a hash with key:
                   3165: language.
                   3166: 
                   3167: =cut
                   3168: 
                   3169: 
                   3170: sub getlangs {
                   3171:     my ($uname,$udom) = @_;
                   3172:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   3173:     if (!$uname) { $uname=$env{'user.name'};   }
                   3174:     my $id=$uname.':'.$udom;
                   3175:     my ($langs,$cached)=&Apache::lonnet::is_cached_new('userlangs',$id);
                   3176:     if ($cached) {
                   3177:         return %{$langs};
                   3178:     } else {
                   3179:         my %loadlangs=&Apache::lonnet::get('environment',['languages'],
                   3180:                                            $udom,$uname);
                   3181:         &Apache::lonnet::do_cache_new('userlangs',$id,\%loadlangs);
                   3182:         return %loadlangs;
                   3183:     }
                   3184: }
                   3185: 
                   3186: sub flush_langs_cache {
                   3187:     my ($uname,$udom)=@_;
                   3188:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   3189:     if (!$uname) { $uname=$env{'user.name'};   }
                   3190:     return if ($udom eq 'public' && $uname eq 'public');
                   3191:     my $id=$uname.':'.$udom;
                   3192:     &Apache::lonnet::devalidate_cache_new('userlangs',$id);
                   3193: }
                   3194: 
1.61      www      3195: # ------------------------------------------------------------------ Screenname
1.81      albertel 3196: 
                   3197: =pod
                   3198: 
1.648     raeburn  3199: =item * &screenname($uname,$udom)
1.81      albertel 3200: 
                   3201: Gets a users screenname and returns it as a string
                   3202: 
                   3203: =cut
1.61      www      3204: 
                   3205: sub screenname {
                   3206:     my ($uname,$udom)=@_;
1.258     albertel 3207:     if ($uname eq $env{'user.name'} &&
                   3208: 	$udom eq $env{'user.domain'}) {return $env{'environment.screenname'};}
1.212     albertel 3209:     my %names=&Apache::lonnet::get('environment',['screenname'],$udom,$uname);
1.68      albertel 3210:     return $names{'screenname'};
1.62      www      3211: }
                   3212: 
1.212     albertel 3213: 
1.802     bisitz   3214: # ------------------------------------------------------------- Confirm Wrapper
                   3215: =pod
                   3216: 
                   3217: =item confirmwrapper
                   3218: 
                   3219: Wrap messages about completion of operation in box
                   3220: 
                   3221: =cut
                   3222: 
                   3223: sub confirmwrapper {
                   3224:     my ($message)=@_;
                   3225:     if ($message) {
                   3226:         return "\n".'<div class="LC_confirm_box">'."\n"
                   3227:                .$message."\n"
                   3228:                .'</div>'."\n";
                   3229:     } else {
                   3230:         return $message;
                   3231:     }
                   3232: }
                   3233: 
1.62      www      3234: # ------------------------------------------------------------- Message Wrapper
                   3235: 
                   3236: sub messagewrapper {
1.369     www      3237:     my ($link,$username,$domain,$subject,$text)=@_;
1.62      www      3238:     return 
1.441     albertel 3239:         '<a href="/adm/email?compose=individual&amp;'.
                   3240:         'recname='.$username.'&amp;recdom='.$domain.
                   3241: 	'&amp;subject='.&escape($subject).'&amp;text='.&escape($text).'" '.
1.200     matthew  3242:         'title="'.&mt('Send message').'">'.$link.'</a>';
1.74      www      3243: }
1.802     bisitz   3244: 
1.74      www      3245: # --------------------------------------------------------------- Notes Wrapper
                   3246: 
                   3247: sub noteswrapper {
                   3248:     my ($link,$un,$do)=@_;
                   3249:     return 
1.896     amueller 3250: "<a href='/adm/email?recordftf=retrieve&amp;recname=$un&amp;recdom=$do'>$link</a>";
1.62      www      3251: }
1.802     bisitz   3252: 
1.62      www      3253: # ------------------------------------------------------------- Aboutme Wrapper
                   3254: 
                   3255: sub aboutmewrapper {
1.1070    raeburn  3256:     my ($link,$username,$domain,$target,$class)=@_;
1.447     raeburn  3257:     if (!defined($username)  && !defined($domain)) {
                   3258:         return;
                   3259:     }
1.1075.2.15  raeburn  3260:     return '<a href="/adm/'.$domain.'/'.$username.'/aboutme"'.
1.1070    raeburn  3261: 	($target?' target="'.$target.'"':'').($class?' class="'.$class.'"':'').' title="'.&mt("View this user's personal information page").'">'.$link.'</a>';
1.62      www      3262: }
                   3263: 
                   3264: # ------------------------------------------------------------ Syllabus Wrapper
                   3265: 
                   3266: sub syllabuswrapper {
1.707     bisitz   3267:     my ($linktext,$coursedir,$domain)=@_;
1.208     matthew  3268:     return qq{<a href="/public/$domain/$coursedir/syllabus">$linktext</a>};
1.61      www      3269: }
1.14      harris41 3270: 
1.802     bisitz   3271: # -----------------------------------------------------------------------------
                   3272: 
1.208     matthew  3273: sub track_student_link {
1.887     raeburn  3274:     my ($linktext,$sname,$sdom,$target,$start,$only_body) = @_;
1.268     albertel 3275:     my $link ="/adm/trackstudent?";
1.208     matthew  3276:     my $title = 'View recent activity';
                   3277:     if (defined($sname) && $sname !~ /^\s*$/ &&
                   3278:         defined($sdom)  && $sdom  !~ /^\s*$/) {
1.268     albertel 3279:         $link .= "selected_student=$sname:$sdom";
1.208     matthew  3280:         $title .= ' of this student';
1.268     albertel 3281:     } 
1.208     matthew  3282:     if (defined($target) && $target !~ /^\s*$/) {
                   3283:         $target = qq{target="$target"};
                   3284:     } else {
                   3285:         $target = '';
                   3286:     }
1.268     albertel 3287:     if ($start) { $link.='&amp;start='.$start; }
1.887     raeburn  3288:     if ($only_body) { $link .= '&amp;only_body=1'; }
1.554     albertel 3289:     $title = &mt($title);
                   3290:     $linktext = &mt($linktext);
1.448     albertel 3291:     return qq{<a href="$link" title="$title" $target>$linktext</a>}.
                   3292: 	&help_open_topic('View_recent_activity');
1.208     matthew  3293: }
                   3294: 
1.781     raeburn  3295: sub slot_reservations_link {
                   3296:     my ($linktext,$sname,$sdom,$target) = @_;
                   3297:     my $link ="/adm/slotrequest?command=showresv&amp;origin=aboutme";
                   3298:     my $title = 'View slot reservation history';
                   3299:     if (defined($sname) && $sname !~ /^\s*$/ &&
                   3300:         defined($sdom)  && $sdom  !~ /^\s*$/) {
                   3301:         $link .= "&amp;uname=$sname&amp;udom=$sdom";
                   3302:         $title .= ' of this student';
                   3303:     }
                   3304:     if (defined($target) && $target !~ /^\s*$/) {
                   3305:         $target = qq{target="$target"};
                   3306:     } else {
                   3307:         $target = '';
                   3308:     }
                   3309:     $title = &mt($title);
                   3310:     $linktext = &mt($linktext);
                   3311:     return qq{<a href="$link" title="$title" $target>$linktext</a>};
                   3312: # FIXME uncomment when help item created: &help_open_topic('Slot_Reservation_History');
                   3313: 
                   3314: }
                   3315: 
1.508     www      3316: # ===================================================== Display a student photo
                   3317: 
                   3318: 
1.509     albertel 3319: sub student_image_tag {
1.508     www      3320:     my ($domain,$user)=@_;
                   3321:     my $imgsrc=&Apache::lonnet::studentphoto($domain,$user,'jpg');
                   3322:     if (($imgsrc) && ($imgsrc ne '/adm/lonKaputt/lonlogo_broken.gif')) {
                   3323: 	return '<img src="'.$imgsrc.'" align="right" />';
                   3324:     } else {
                   3325: 	return '';
                   3326:     }
                   3327: }
                   3328: 
1.112     bowersj2 3329: =pod
                   3330: 
                   3331: =back
                   3332: 
                   3333: =head1 Access .tab File Data
                   3334: 
                   3335: =over 4
                   3336: 
1.648     raeburn  3337: =item * &languageids() 
1.112     bowersj2 3338: 
                   3339: returns list of all language ids
                   3340: 
                   3341: =cut
                   3342: 
1.14      harris41 3343: sub languageids {
1.16      harris41 3344:     return sort(keys(%language));
1.14      harris41 3345: }
                   3346: 
1.112     bowersj2 3347: =pod
                   3348: 
1.648     raeburn  3349: =item * &languagedescription() 
1.112     bowersj2 3350: 
                   3351: returns description of a specified language id
                   3352: 
                   3353: =cut
                   3354: 
1.14      harris41 3355: sub languagedescription {
1.125     www      3356:     my $code=shift;
                   3357:     return  ($supported_language{$code}?'* ':'').
                   3358:             $language{$code}.
1.126     www      3359: 	    ($supported_language{$code}?' ('.&mt('interface available').')':'');
1.145     www      3360: }
                   3361: 
1.1048    foxr     3362: =pod
                   3363: 
                   3364: =item * &plainlanguagedescription
                   3365: 
                   3366: Returns both the plain language description (e.g. 'Creoles and Pidgins, English-based (Other)')
                   3367: and the language character encoding (e.g. ISO) separated by a ' - ' string.
                   3368: 
                   3369: =cut
                   3370: 
1.145     www      3371: sub plainlanguagedescription {
                   3372:     my $code=shift;
                   3373:     return $language{$code};
                   3374: }
                   3375: 
1.1048    foxr     3376: =pod
                   3377: 
                   3378: =item * &supportedlanguagecode
                   3379: 
                   3380: Returns the supported language code (e.g. sptutf maps to pt) given a language
                   3381: code.
                   3382: 
                   3383: =cut
                   3384: 
1.145     www      3385: sub supportedlanguagecode {
                   3386:     my $code=shift;
                   3387:     return $supported_language{$code};
1.97      www      3388: }
                   3389: 
1.112     bowersj2 3390: =pod
                   3391: 
1.1048    foxr     3392: =item * &latexlanguage()
                   3393: 
                   3394: Given a language key code returns the correspondnig language to use
                   3395: to select the correct hyphenation on LaTeX printouts.  This is undef if there
                   3396: is no supported hyphenation for the language code.
                   3397: 
                   3398: =cut
                   3399: 
                   3400: sub latexlanguage {
                   3401:     my $code = shift;
                   3402:     return $latex_language{$code};
                   3403: }
                   3404: 
                   3405: =pod
                   3406: 
                   3407: =item * &latexhyphenation()
                   3408: 
                   3409: Same as above but what's supplied is the language as it might be stored
                   3410: in the metadata.
                   3411: 
                   3412: =cut
                   3413: 
                   3414: sub latexhyphenation {
                   3415:     my $key = shift;
                   3416:     return $latex_language_bykey{$key};
                   3417: }
                   3418: 
                   3419: =pod
                   3420: 
1.648     raeburn  3421: =item * &copyrightids() 
1.112     bowersj2 3422: 
                   3423: returns list of all copyrights
                   3424: 
                   3425: =cut
                   3426: 
                   3427: sub copyrightids {
                   3428:     return sort(keys(%cprtag));
                   3429: }
                   3430: 
                   3431: =pod
                   3432: 
1.648     raeburn  3433: =item * &copyrightdescription() 
1.112     bowersj2 3434: 
                   3435: returns description of a specified copyright id
                   3436: 
                   3437: =cut
                   3438: 
                   3439: sub copyrightdescription {
1.166     www      3440:     return &mt($cprtag{shift(@_)});
1.112     bowersj2 3441: }
1.197     matthew  3442: 
                   3443: =pod
                   3444: 
1.648     raeburn  3445: =item * &source_copyrightids() 
1.192     taceyjo1 3446: 
                   3447: returns list of all source copyrights
                   3448: 
                   3449: =cut
                   3450: 
                   3451: sub source_copyrightids {
                   3452:     return sort(keys(%scprtag));
                   3453: }
                   3454: 
                   3455: =pod
                   3456: 
1.648     raeburn  3457: =item * &source_copyrightdescription() 
1.192     taceyjo1 3458: 
                   3459: returns description of a specified source copyright id
                   3460: 
                   3461: =cut
                   3462: 
                   3463: sub source_copyrightdescription {
                   3464:     return &mt($scprtag{shift(@_)});
                   3465: }
1.112     bowersj2 3466: 
                   3467: =pod
                   3468: 
1.648     raeburn  3469: =item * &filecategories() 
1.112     bowersj2 3470: 
                   3471: returns list of all file categories
                   3472: 
                   3473: =cut
                   3474: 
                   3475: sub filecategories {
                   3476:     return sort(keys(%category_extensions));
                   3477: }
                   3478: 
                   3479: =pod
                   3480: 
1.648     raeburn  3481: =item * &filecategorytypes() 
1.112     bowersj2 3482: 
                   3483: returns list of file types belonging to a given file
                   3484: category
                   3485: 
                   3486: =cut
                   3487: 
                   3488: sub filecategorytypes {
1.356     albertel 3489:     my ($cat) = @_;
                   3490:     return @{$category_extensions{lc($cat)}};
1.112     bowersj2 3491: }
                   3492: 
                   3493: =pod
                   3494: 
1.648     raeburn  3495: =item * &fileembstyle() 
1.112     bowersj2 3496: 
                   3497: returns embedding style for a specified file type
                   3498: 
                   3499: =cut
                   3500: 
                   3501: sub fileembstyle {
                   3502:     return $fe{lc(shift(@_))};
1.169     www      3503: }
                   3504: 
1.351     www      3505: sub filemimetype {
                   3506:     return $fm{lc(shift(@_))};
                   3507: }
                   3508: 
1.169     www      3509: 
                   3510: sub filecategoryselect {
                   3511:     my ($name,$value)=@_;
1.189     matthew  3512:     return &select_form($value,$name,
1.970     raeburn  3513:                         {'' => &mt('Any category'), map { $_,$_ } sort(keys(%category_extensions))});
1.112     bowersj2 3514: }
                   3515: 
                   3516: =pod
                   3517: 
1.648     raeburn  3518: =item * &filedescription() 
1.112     bowersj2 3519: 
                   3520: returns description for a specified file type
                   3521: 
                   3522: =cut
                   3523: 
                   3524: sub filedescription {
1.188     matthew  3525:     my $file_description = $fd{lc(shift())};
                   3526:     $file_description =~ s:([\[\]]):~$1:g;
                   3527:     return &mt($file_description);
1.112     bowersj2 3528: }
                   3529: 
                   3530: =pod
                   3531: 
1.648     raeburn  3532: =item * &filedescriptionex() 
1.112     bowersj2 3533: 
                   3534: returns description for a specified file type with
                   3535: extra formatting
                   3536: 
                   3537: =cut
                   3538: 
                   3539: sub filedescriptionex {
                   3540:     my $ex=shift;
1.188     matthew  3541:     my $file_description = $fd{lc($ex)};
                   3542:     $file_description =~ s:([\[\]]):~$1:g;
                   3543:     return '.'.$ex.' '.&mt($file_description);
1.112     bowersj2 3544: }
                   3545: 
                   3546: # End of .tab access
                   3547: =pod
                   3548: 
                   3549: =back
                   3550: 
                   3551: =cut
                   3552: 
                   3553: # ------------------------------------------------------------------ File Types
                   3554: sub fileextensions {
                   3555:     return sort(keys(%fe));
                   3556: }
                   3557: 
1.97      www      3558: # ----------------------------------------------------------- Display Languages
                   3559: # returns a hash with all desired display languages
                   3560: #
                   3561: 
                   3562: sub display_languages {
                   3563:     my %languages=();
1.695     raeburn  3564:     foreach my $lang (&Apache::lonlocal::preferred_languages()) {
1.356     albertel 3565: 	$languages{$lang}=1;
1.97      www      3566:     }
                   3567:     &get_unprocessed_cgi($ENV{'QUERY_STRING'},['displaylanguage']);
1.258     albertel 3568:     if ($env{'form.displaylanguage'}) {
1.356     albertel 3569: 	foreach my $lang (split(/\s*(\,|\;|\:)\s*/,$env{'form.displaylanguage'})) {
                   3570: 	    $languages{$lang}=1;
1.97      www      3571:         }
                   3572:     }
                   3573:     return %languages;
1.14      harris41 3574: }
                   3575: 
1.582     albertel 3576: sub languages {
                   3577:     my ($possible_langs) = @_;
1.695     raeburn  3578:     my @preferred_langs = &Apache::lonlocal::preferred_languages();
1.582     albertel 3579:     if (!ref($possible_langs)) {
                   3580: 	if( wantarray ) {
                   3581: 	    return @preferred_langs;
                   3582: 	} else {
                   3583: 	    return $preferred_langs[0];
                   3584: 	}
                   3585:     }
                   3586:     my %possibilities = map { $_ => 1 } (@$possible_langs);
                   3587:     my @preferred_possibilities;
                   3588:     foreach my $preferred_lang (@preferred_langs) {
                   3589: 	if (exists($possibilities{$preferred_lang})) {
                   3590: 	    push(@preferred_possibilities, $preferred_lang);
                   3591: 	}
                   3592:     }
                   3593:     if( wantarray ) {
                   3594: 	return @preferred_possibilities;
                   3595:     }
                   3596:     return $preferred_possibilities[0];
                   3597: }
                   3598: 
1.742     raeburn  3599: sub user_lang {
                   3600:     my ($touname,$toudom,$fromcid) = @_;
                   3601:     my @userlangs;
                   3602:     if (($fromcid ne '') && ($env{'course.'.$fromcid.'.languages'} ne '')) {
                   3603:         @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
                   3604:                     $env{'course.'.$fromcid.'.languages'}));
                   3605:     } else {
                   3606:         my %langhash = &getlangs($touname,$toudom);
                   3607:         if ($langhash{'languages'} ne '') {
                   3608:             @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
                   3609:         } else {
                   3610:             my %domdefs = &Apache::lonnet::get_domain_defaults($toudom);
                   3611:             if ($domdefs{'lang_def'} ne '') {
                   3612:                 @userlangs = ($domdefs{'lang_def'});
                   3613:             }
                   3614:         }
                   3615:     }
                   3616:     my @languages=&Apache::lonlocal::get_genlanguages(@userlangs);
                   3617:     my $user_lh = Apache::localize->get_handle(@languages);
                   3618:     return $user_lh;
                   3619: }
                   3620: 
                   3621: 
1.112     bowersj2 3622: ###############################################################
                   3623: ##               Student Answer Attempts                     ##
                   3624: ###############################################################
                   3625: 
                   3626: =pod
                   3627: 
                   3628: =head1 Alternate Problem Views
                   3629: 
                   3630: =over 4
                   3631: 
1.648     raeburn  3632: =item * &get_previous_attempt($symb, $username, $domain, $course,
1.112     bowersj2 3633:     $getattempt, $regexp, $gradesub)
                   3634: 
                   3635: Return string with previous attempt on problem. Arguments:
                   3636: 
                   3637: =over 4
                   3638: 
                   3639: =item * $symb: Problem, including path
                   3640: 
                   3641: =item * $username: username of the desired student
                   3642: 
                   3643: =item * $domain: domain of the desired student
1.14      harris41 3644: 
1.112     bowersj2 3645: =item * $course: Course ID
1.14      harris41 3646: 
1.112     bowersj2 3647: =item * $getattempt: Leave blank for all attempts, otherwise put
                   3648:     something
1.14      harris41 3649: 
1.112     bowersj2 3650: =item * $regexp: if string matches this regexp, the string will be
                   3651:     sent to $gradesub
1.14      harris41 3652: 
1.112     bowersj2 3653: =item * $gradesub: routine that processes the string if it matches $regexp
1.14      harris41 3654: 
1.112     bowersj2 3655: =back
1.14      harris41 3656: 
1.112     bowersj2 3657: The output string is a table containing all desired attempts, if any.
1.16      harris41 3658: 
1.112     bowersj2 3659: =cut
1.1       albertel 3660: 
                   3661: sub get_previous_attempt {
1.43      ng       3662:   my ($symb,$username,$domain,$course,$getattempt,$regexp,$gradesub)=@_;
1.1       albertel 3663:   my $prevattempts='';
1.43      ng       3664:   no strict 'refs';
1.1       albertel 3665:   if ($symb) {
1.3       albertel 3666:     my (%returnhash)=
                   3667:       &Apache::lonnet::restore($symb,$course,$domain,$username);
1.1       albertel 3668:     if ($returnhash{'version'}) {
                   3669:       my %lasthash=();
                   3670:       my $version;
                   3671:       for ($version=1;$version<=$returnhash{'version'};$version++) {
1.356     albertel 3672:         foreach my $key (sort(split(/\:/,$returnhash{$version.':keys'}))) {
                   3673: 	  $lasthash{$key}=$returnhash{$version.':'.$key};
1.19      harris41 3674:         }
1.1       albertel 3675:       }
1.596     albertel 3676:       $prevattempts=&start_data_table().&start_data_table_header_row();
                   3677:       $prevattempts.='<th>'.&mt('History').'</th>';
1.978     raeburn  3678:       my (%typeparts,%lasthidden);
1.945     raeburn  3679:       my $showsurv=&Apache::lonnet::allowed('vas',$env{'request.course.id'});
1.356     albertel 3680:       foreach my $key (sort(keys(%lasthash))) {
                   3681: 	my ($ign,@parts) = split(/\./,$key);
1.41      ng       3682: 	if ($#parts > 0) {
1.31      albertel 3683: 	  my $data=$parts[-1];
1.989     raeburn  3684:           next if ($data eq 'foilorder');
1.31      albertel 3685: 	  pop(@parts);
1.1010    www      3686:           $prevattempts.='<th>'.&mt('Part ').join('.',@parts).'<br />'.$data.'&nbsp;</th>';
1.945     raeburn  3687:           if ($data eq 'type') {
                   3688:               unless ($showsurv) {
                   3689:                   my $id = join(',',@parts);
                   3690:                   $typeparts{$ign.'.'.$id} = $lasthash{$key};
1.978     raeburn  3691:                   if (($lasthash{$key} eq 'anonsurvey') || ($lasthash{$key} eq 'anonsurveycred')) {
                   3692:                       $lasthidden{$ign.'.'.$id} = 1;
                   3693:                   }
1.945     raeburn  3694:               }
1.1010    www      3695:           } 
1.31      albertel 3696: 	} else {
1.41      ng       3697: 	  if ($#parts == 0) {
                   3698: 	    $prevattempts.='<th>'.$parts[0].'</th>';
                   3699: 	  } else {
                   3700: 	    $prevattempts.='<th>'.$ign.'</th>';
                   3701: 	  }
1.31      albertel 3702: 	}
1.16      harris41 3703:       }
1.596     albertel 3704:       $prevattempts.=&end_data_table_header_row();
1.40      ng       3705:       if ($getattempt eq '') {
                   3706: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
1.945     raeburn  3707:             my @hidden;
                   3708:             if (%typeparts) {
                   3709:                 foreach my $id (keys(%typeparts)) {
                   3710:                     if (($returnhash{$version.':'.$id.'.type'} eq 'anonsurvey') || ($returnhash{$version.':'.$id.'.type'} eq 'anonsurveycred')) {
                   3711:                         push(@hidden,$id);
                   3712:                     }
                   3713:                 }
                   3714:             }
                   3715:             $prevattempts.=&start_data_table_row().
                   3716:                            '<td>'.&mt('Transaction [_1]',$version).'</td>';
                   3717:             if (@hidden) {
                   3718:                 foreach my $key (sort(keys(%lasthash))) {
1.989     raeburn  3719:                     next if ($key =~ /\.foilorder$/);
1.945     raeburn  3720:                     my $hide;
                   3721:                     foreach my $id (@hidden) {
                   3722:                         if ($key =~ /^\Q$id\E/) {
                   3723:                             $hide = 1;
                   3724:                             last;
                   3725:                         }
                   3726:                     }
                   3727:                     if ($hide) {
                   3728:                         my ($id,$data) = ($key =~ /^(.+)\.([^.]+)$/);
                   3729:                         if (($data eq 'award') || ($data eq 'awarddetail')) {
                   3730:                             my $value = &format_previous_attempt_value($key,
                   3731:                                              $returnhash{$version.':'.$key});
                   3732:                             $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3733:                         } else {
                   3734:                             $prevattempts.='<td>&nbsp;</td>';
                   3735:                         }
                   3736:                     } else {
                   3737:                         if ($key =~ /\./) {
                   3738:                             my $value = &format_previous_attempt_value($key,
                   3739:                                               $returnhash{$version.':'.$key});
                   3740:                             $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3741:                         } else {
                   3742:                             $prevattempts.='<td>&nbsp;</td>';
                   3743:                         }
                   3744:                     }
                   3745:                 }
                   3746:             } else {
                   3747: 	        foreach my $key (sort(keys(%lasthash))) {
1.989     raeburn  3748:                     next if ($key =~ /\.foilorder$/);
1.945     raeburn  3749: 		    my $value = &format_previous_attempt_value($key,
                   3750: 			            $returnhash{$version.':'.$key});
                   3751: 		    $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3752: 	        }
                   3753:             }
                   3754: 	    $prevattempts.=&end_data_table_row();
1.40      ng       3755: 	 }
1.1       albertel 3756:       }
1.945     raeburn  3757:       my @currhidden = keys(%lasthidden);
1.596     albertel 3758:       $prevattempts.=&start_data_table_row().'<td>'.&mt('Current').'</td>';
1.356     albertel 3759:       foreach my $key (sort(keys(%lasthash))) {
1.989     raeburn  3760:           next if ($key =~ /\.foilorder$/);
1.945     raeburn  3761:           if (%typeparts) {
                   3762:               my $hidden;
                   3763:               foreach my $id (@currhidden) {
                   3764:                   if ($key =~ /^\Q$id\E/) {
                   3765:                       $hidden = 1;
                   3766:                       last;
                   3767:                   }
                   3768:               }
                   3769:               if ($hidden) {
                   3770:                   my ($id,$data) = ($key =~ /^(.+)\.([^.]+)$/);
                   3771:                   if (($data eq 'award') || ($data eq 'awarddetail')) {
                   3772:                       my $value = &format_previous_attempt_value($key,$lasthash{$key});
                   3773:                       if ($key =~/$regexp$/ && (defined &$gradesub)) {
                   3774:                           $value = &$gradesub($value);
                   3775:                       }
                   3776:                       $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3777:                   } else {
                   3778:                       $prevattempts.='<td>&nbsp;</td>';
                   3779:                   }
                   3780:               } else {
                   3781:                   my $value = &format_previous_attempt_value($key,$lasthash{$key});
                   3782:                   if ($key =~/$regexp$/ && (defined &$gradesub)) {
                   3783:                       $value = &$gradesub($value);
                   3784:                   }
                   3785:                   $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3786:               }
                   3787:           } else {
                   3788: 	      my $value = &format_previous_attempt_value($key,$lasthash{$key});
                   3789: 	      if ($key =~/$regexp$/ && (defined &$gradesub)) {
                   3790:                   $value = &$gradesub($value);
                   3791:               }
                   3792: 	      $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3793:           }
1.16      harris41 3794:       }
1.596     albertel 3795:       $prevattempts.= &end_data_table_row().&end_data_table();
1.1       albertel 3796:     } else {
1.596     albertel 3797:       $prevattempts=
                   3798: 	  &start_data_table().&start_data_table_row().
                   3799: 	  '<td>'.&mt('Nothing submitted - no attempts.').'</td>'.
                   3800: 	  &end_data_table_row().&end_data_table();
1.1       albertel 3801:     }
                   3802:   } else {
1.596     albertel 3803:     $prevattempts=
                   3804: 	  &start_data_table().&start_data_table_row().
                   3805: 	  '<td>'.&mt('No data.').'</td>'.
                   3806: 	  &end_data_table_row().&end_data_table();
1.1       albertel 3807:   }
1.10      albertel 3808: }
                   3809: 
1.581     albertel 3810: sub format_previous_attempt_value {
                   3811:     my ($key,$value) = @_;
1.1011    www      3812:     if (($key =~ /timestamp/) || ($key=~/duedate/)) {
1.581     albertel 3813: 	$value = &Apache::lonlocal::locallocaltime($value);
                   3814:     } elsif (ref($value) eq 'ARRAY') {
                   3815: 	$value = '('.join(', ', @{ $value }).')';
1.988     raeburn  3816:     } elsif ($key =~ /answerstring$/) {
                   3817:         my %answers = &Apache::lonnet::str2hash($value);
                   3818:         my @anskeys = sort(keys(%answers));
                   3819:         if (@anskeys == 1) {
                   3820:             my $answer = $answers{$anskeys[0]};
1.1001    raeburn  3821:             if ($answer =~ m{\0}) {
                   3822:                 $answer =~ s{\0}{,}g;
1.988     raeburn  3823:             }
                   3824:             my $tag_internal_answer_name = 'INTERNAL';
                   3825:             if ($anskeys[0] eq $tag_internal_answer_name) {
                   3826:                 $value = $answer; 
                   3827:             } else {
                   3828:                 $value = $anskeys[0].'='.$answer;
                   3829:             }
                   3830:         } else {
                   3831:             foreach my $ans (@anskeys) {
                   3832:                 my $answer = $answers{$ans};
1.1001    raeburn  3833:                 if ($answer =~ m{\0}) {
                   3834:                     $answer =~ s{\0}{,}g;
1.988     raeburn  3835:                 }
                   3836:                 $value .=  $ans.'='.$answer.'<br />';;
                   3837:             } 
                   3838:         }
1.581     albertel 3839:     } else {
                   3840: 	$value = &unescape($value);
                   3841:     }
                   3842:     return $value;
                   3843: }
                   3844: 
                   3845: 
1.107     albertel 3846: sub relative_to_absolute {
                   3847:     my ($url,$output)=@_;
                   3848:     my $parser=HTML::TokeParser->new(\$output);
                   3849:     my $token;
                   3850:     my $thisdir=$url;
                   3851:     my @rlinks=();
                   3852:     while ($token=$parser->get_token) {
                   3853: 	if ($token->[0] eq 'S') {
                   3854: 	    if ($token->[1] eq 'a') {
                   3855: 		if ($token->[2]->{'href'}) {
                   3856: 		    $rlinks[$#rlinks+1]=$token->[2]->{'href'};
                   3857: 		}
                   3858: 	    } elsif ($token->[1] eq 'img' || $token->[1] eq 'embed' ) {
                   3859: 		$rlinks[$#rlinks+1]=$token->[2]->{'src'};
                   3860: 	    } elsif ($token->[1] eq 'base') {
                   3861: 		$thisdir=$token->[2]->{'href'};
                   3862: 	    }
                   3863: 	}
                   3864:     }
                   3865:     $thisdir=~s-/[^/]*$--;
1.356     albertel 3866:     foreach my $link (@rlinks) {
1.726     raeburn  3867: 	unless (($link=~/^https?\:\/\//i) ||
1.356     albertel 3868: 		($link=~/^\//) ||
                   3869: 		($link=~/^javascript:/i) ||
                   3870: 		($link=~/^mailto:/i) ||
                   3871: 		($link=~/^\#/)) {
                   3872: 	    my $newlocation=&Apache::lonnet::hreflocation($thisdir,$link);
                   3873: 	    $output=~s/(\"|\'|\=\s*)\Q$link\E(\"|\'|\s|\>)/$1$newlocation$2/;
1.107     albertel 3874: 	}
                   3875:     }
                   3876: # -------------------------------------------------- Deal with Applet codebases
                   3877:     $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
                   3878:     return $output;
                   3879: }
                   3880: 
1.112     bowersj2 3881: =pod
                   3882: 
1.648     raeburn  3883: =item * &get_student_view()
1.112     bowersj2 3884: 
                   3885: show a snapshot of what student was looking at
                   3886: 
                   3887: =cut
                   3888: 
1.10      albertel 3889: sub get_student_view {
1.186     albertel 3890:   my ($symb,$username,$domain,$courseid,$target,$moreenv) = @_;
1.114     www      3891:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 3892:   my (%form);
1.10      albertel 3893:   my @elements=('symb','courseid','domain','username');
                   3894:   foreach my $element (@elements) {
1.186     albertel 3895:       $form{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 3896:   }
1.186     albertel 3897:   if (defined($moreenv)) {
                   3898:       %form=(%form,%{$moreenv});
                   3899:   }
1.236     albertel 3900:   if (defined($target)) { $form{'grade_target'} = $target; }
1.107     albertel 3901:   $feedurl=&Apache::lonnet::clutter($feedurl);
1.650     www      3902:   my ($userview,$response)=&Apache::lonnet::ssi_body($feedurl,%form);
1.11      albertel 3903:   $userview=~s/\<body[^\>]*\>//gi;
                   3904:   $userview=~s/\<\/body\>//gi;
                   3905:   $userview=~s/\<html\>//gi;
                   3906:   $userview=~s/\<\/html\>//gi;
                   3907:   $userview=~s/\<head\>//gi;
                   3908:   $userview=~s/\<\/head\>//gi;
                   3909:   $userview=~s/action\s*\=/would_be_action\=/gi;
1.107     albertel 3910:   $userview=&relative_to_absolute($feedurl,$userview);
1.650     www      3911:   if (wantarray) {
                   3912:      return ($userview,$response);
                   3913:   } else {
                   3914:      return $userview;
                   3915:   }
                   3916: }
                   3917: 
                   3918: sub get_student_view_with_retries {
                   3919:   my ($symb,$retries,$username,$domain,$courseid,$target,$moreenv) = @_;
                   3920: 
                   3921:     my $ok = 0;                 # True if we got a good response.
                   3922:     my $content;
                   3923:     my $response;
                   3924: 
                   3925:     # Try to get the student_view done. within the retries count:
                   3926:     
                   3927:     do {
                   3928:          ($content, $response) = &get_student_view($symb,$username,$domain,$courseid,$target,$moreenv);
                   3929:          $ok      = $response->is_success;
                   3930:          if (!$ok) {
                   3931:             &Apache::lonnet::logthis("Failed get_student_view_with_retries on $symb: ".$response->is_success.', '.$response->code.', '.$response->message);
                   3932:          }
                   3933:          $retries--;
                   3934:     } while (!$ok && ($retries > 0));
                   3935:     
                   3936:     if (!$ok) {
                   3937:        $content = '';          # On error return an empty content.
                   3938:     }
1.651     www      3939:     if (wantarray) {
                   3940:        return ($content, $response);
                   3941:     } else {
                   3942:        return $content;
                   3943:     }
1.11      albertel 3944: }
                   3945: 
1.112     bowersj2 3946: =pod
                   3947: 
1.648     raeburn  3948: =item * &get_student_answers() 
1.112     bowersj2 3949: 
                   3950: show a snapshot of how student was answering problem
                   3951: 
                   3952: =cut
                   3953: 
1.11      albertel 3954: sub get_student_answers {
1.100     sakharuk 3955:   my ($symb,$username,$domain,$courseid,%form) = @_;
1.114     www      3956:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 3957:   my (%moreenv);
1.11      albertel 3958:   my @elements=('symb','courseid','domain','username');
                   3959:   foreach my $element (@elements) {
1.186     albertel 3960:     $moreenv{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 3961:   }
1.186     albertel 3962:   $moreenv{'grade_target'}='answer';
                   3963:   %moreenv=(%form,%moreenv);
1.497     raeburn  3964:   $feedurl = &Apache::lonnet::clutter($feedurl);
                   3965:   my $userview=&Apache::lonnet::ssi($feedurl,%moreenv);
1.10      albertel 3966:   return $userview;
1.1       albertel 3967: }
1.116     albertel 3968: 
                   3969: =pod
                   3970: 
                   3971: =item * &submlink()
                   3972: 
1.242     albertel 3973: Inputs: $text $uname $udom $symb $target
1.116     albertel 3974: 
                   3975: Returns: A link to grades.pm such as to see the SUBM view of a student
                   3976: 
                   3977: =cut
                   3978: 
                   3979: ###############################################
                   3980: sub submlink {
1.242     albertel 3981:     my ($text,$uname,$udom,$symb,$target)=@_;
1.116     albertel 3982:     if (!($uname && $udom)) {
                   3983: 	(my $cursymb, my $courseid,$udom,$uname)=
1.463     albertel 3984: 	    &Apache::lonnet::whichuser($symb);
1.116     albertel 3985: 	if (!$symb) { $symb=$cursymb; }
                   3986:     }
1.254     matthew  3987:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      3988:     $symb=&escape($symb);
1.960     bisitz   3989:     if ($target) { $target=" target=\"$target\""; }
                   3990:     return
                   3991:         '<a href="/adm/grades?command=submission'.
                   3992:         '&amp;symb='.$symb.
                   3993:         '&amp;student='.$uname.
                   3994:         '&amp;userdom='.$udom.'"'.
                   3995:         $target.'>'.$text.'</a>';
1.242     albertel 3996: }
                   3997: ##############################################
                   3998: 
                   3999: =pod
                   4000: 
                   4001: =item * &pgrdlink()
                   4002: 
                   4003: Inputs: $text $uname $udom $symb $target
                   4004: 
                   4005: Returns: A link to grades.pm such as to see the PGRD view of a student
                   4006: 
                   4007: =cut
                   4008: 
                   4009: ###############################################
                   4010: sub pgrdlink {
                   4011:     my $link=&submlink(@_);
                   4012:     $link=~s/(&command=submission)/$1&showgrading=yes/;
                   4013:     return $link;
                   4014: }
                   4015: ##############################################
                   4016: 
                   4017: =pod
                   4018: 
                   4019: =item * &pprmlink()
                   4020: 
                   4021: Inputs: $text $uname $udom $symb $target
                   4022: 
                   4023: Returns: A link to parmset.pm such as to see the PPRM view of a
1.283     albertel 4024: student and a specific resource
1.242     albertel 4025: 
                   4026: =cut
                   4027: 
                   4028: ###############################################
                   4029: sub pprmlink {
                   4030:     my ($text,$uname,$udom,$symb,$target)=@_;
                   4031:     if (!($uname && $udom)) {
                   4032: 	(my $cursymb, my $courseid,$udom,$uname)=
1.463     albertel 4033: 	    &Apache::lonnet::whichuser($symb);
1.242     albertel 4034: 	if (!$symb) { $symb=$cursymb; }
                   4035:     }
1.254     matthew  4036:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      4037:     $symb=&escape($symb);
1.242     albertel 4038:     if ($target) { $target="target=\"$target\""; }
1.595     albertel 4039:     return '<a href="/adm/parmset?command=set&amp;'.
                   4040: 	'symb='.$symb.'&amp;uname='.$uname.
                   4041: 	'&amp;udom='.$udom.'" '.$target.'>'.$text.'</a>';
1.116     albertel 4042: }
                   4043: ##############################################
1.37      matthew  4044: 
1.112     bowersj2 4045: =pod
                   4046: 
                   4047: =back
                   4048: 
                   4049: =cut
                   4050: 
1.37      matthew  4051: ###############################################
1.51      www      4052: 
                   4053: 
                   4054: sub timehash {
1.687     raeburn  4055:     my ($thistime) = @_;
                   4056:     my $timezone = &Apache::lonlocal::gettimezone();
                   4057:     my $dt = DateTime->from_epoch(epoch => $thistime)
                   4058:                      ->set_time_zone($timezone);
                   4059:     my $wday = $dt->day_of_week();
                   4060:     if ($wday == 7) { $wday = 0; }
                   4061:     return ( 'second' => $dt->second(),
                   4062:              'minute' => $dt->minute(),
                   4063:              'hour'   => $dt->hour(),
                   4064:              'day'     => $dt->day_of_month(),
                   4065:              'month'   => $dt->month(),
                   4066:              'year'    => $dt->year(),
                   4067:              'weekday' => $wday,
                   4068:              'dayyear' => $dt->day_of_year(),
                   4069:              'dlsav'   => $dt->is_dst() );
1.51      www      4070: }
                   4071: 
1.370     www      4072: sub utc_string {
                   4073:     my ($date)=@_;
1.371     www      4074:     return strftime("%Y%m%dT%H%M%SZ",gmtime($date));
1.370     www      4075: }
                   4076: 
1.51      www      4077: sub maketime {
                   4078:     my %th=@_;
1.687     raeburn  4079:     my ($epoch_time,$timezone,$dt);
                   4080:     $timezone = &Apache::lonlocal::gettimezone();
                   4081:     eval {
                   4082:         $dt = DateTime->new( year   => $th{'year'},
                   4083:                              month  => $th{'month'},
                   4084:                              day    => $th{'day'},
                   4085:                              hour   => $th{'hour'},
                   4086:                              minute => $th{'minute'},
                   4087:                              second => $th{'second'},
                   4088:                              time_zone => $timezone,
                   4089:                          );
                   4090:     };
                   4091:     if (!$@) {
                   4092:         $epoch_time = $dt->epoch;
                   4093:         if ($epoch_time) {
                   4094:             return $epoch_time;
                   4095:         }
                   4096:     }
1.51      www      4097:     return POSIX::mktime(
                   4098:         ($th{'seconds'},$th{'minutes'},$th{'hours'},
1.210     www      4099:          $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,-1));
1.70      www      4100: }
                   4101: 
                   4102: #########################################
1.51      www      4103: 
                   4104: sub findallcourses {
1.482     raeburn  4105:     my ($roles,$uname,$udom) = @_;
1.355     albertel 4106:     my %roles;
                   4107:     if (ref($roles)) { %roles = map { $_ => 1 } @{$roles}; }
1.348     albertel 4108:     my %courses;
1.51      www      4109:     my $now=time;
1.482     raeburn  4110:     if (!defined($uname)) {
                   4111:         $uname = $env{'user.name'};
                   4112:     }
                   4113:     if (!defined($udom)) {
                   4114:         $udom = $env{'user.domain'};
                   4115:     }
                   4116:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.1073    raeburn  4117:         my %roleshash = &Apache::lonnet::dump('roles',$udom,$uname);
1.482     raeburn  4118:         if (!%roles) {
                   4119:             %roles = (
                   4120:                        cc => 1,
1.907     raeburn  4121:                        co => 1,
1.482     raeburn  4122:                        in => 1,
                   4123:                        ep => 1,
                   4124:                        ta => 1,
                   4125:                        cr => 1,
                   4126:                        st => 1,
                   4127:              );
                   4128:         }
                   4129:         foreach my $entry (keys(%roleshash)) {
                   4130:             my ($trole,$tend,$tstart) = split(/_/,$roleshash{$entry});
                   4131:             if ($trole =~ /^cr/) { 
                   4132:                 next if (!exists($roles{$trole}) && !exists($roles{'cr'}));
                   4133:             } else {
                   4134:                 next if (!exists($roles{$trole}));
                   4135:             }
                   4136:             if ($tend) {
                   4137:                 next if ($tend < $now);
                   4138:             }
                   4139:             if ($tstart) {
                   4140:                 next if ($tstart > $now);
                   4141:             }
1.1058    raeburn  4142:             my ($cdom,$cnum,$sec,$cnumpart,$secpart,$role);
1.482     raeburn  4143:             (undef,$cdom,$cnumpart,$secpart) = split(/\//,$entry);
1.1058    raeburn  4144:             my $value = $trole.'/'.$cdom.'/';
1.482     raeburn  4145:             if ($secpart eq '') {
                   4146:                 ($cnum,$role) = split(/_/,$cnumpart); 
                   4147:                 $sec = 'none';
1.1058    raeburn  4148:                 $value .= $cnum.'/';
1.482     raeburn  4149:             } else {
                   4150:                 $cnum = $cnumpart;
                   4151:                 ($sec,$role) = split(/_/,$secpart);
1.1058    raeburn  4152:                 $value .= $cnum.'/'.$sec;
                   4153:             }
                   4154:             if (ref($courses{$cdom.'_'.$cnum}{$sec}) eq 'ARRAY') {
                   4155:                 unless (grep(/^\Q$value\E$/,@{$courses{$cdom.'_'.$cnum}{$sec}})) {
                   4156:                     push(@{$courses{$cdom.'_'.$cnum}{$sec}},$value);
                   4157:                 }
                   4158:             } else {
                   4159:                 @{$courses{$cdom.'_'.$cnum}{$sec}} = ($value);
1.490     raeburn  4160:             }
1.482     raeburn  4161:         }
                   4162:     } else {
                   4163:         foreach my $key (keys(%env)) {
1.483     albertel 4164: 	    if ( $key=~m{^user\.role\.(\w+)\./($match_domain)/($match_courseid)/?(\w*)$} ||
                   4165:                  $key=~m{^user\.role\.(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_courseid)/?(\w*)$}) {
1.482     raeburn  4166: 	        my ($role,$cdom,$cnum,$sec) = ($1,$2,$3,$4);
                   4167: 	        next if ($role eq 'ca' || $role eq 'aa');
                   4168: 	        next if (%roles && !exists($roles{$role}));
                   4169: 	        my ($starttime,$endtime)=split(/\./,$env{$key});
                   4170:                 my $active=1;
                   4171:                 if ($starttime) {
                   4172: 		    if ($now<$starttime) { $active=0; }
                   4173:                 }
                   4174:                 if ($endtime) {
                   4175:                     if ($now>$endtime) { $active=0; }
                   4176:                 }
                   4177:                 if ($active) {
1.1058    raeburn  4178:                     my $value = $role.'/'.$cdom.'/'.$cnum.'/';
1.482     raeburn  4179:                     if ($sec eq '') {
                   4180:                         $sec = 'none';
1.1058    raeburn  4181:                     } else {
                   4182:                         $value .= $sec;
                   4183:                     }
                   4184:                     if (ref($courses{$cdom.'_'.$cnum}{$sec}) eq 'ARRAY') {
                   4185:                         unless (grep(/^\Q$value\E$/,@{$courses{$cdom.'_'.$cnum}{$sec}})) {
                   4186:                             push(@{$courses{$cdom.'_'.$cnum}{$sec}},$value);
                   4187:                         }
                   4188:                     } else {
                   4189:                         @{$courses{$cdom.'_'.$cnum}{$sec}} = ($value);
1.482     raeburn  4190:                     }
1.474     raeburn  4191:                 }
                   4192:             }
1.51      www      4193:         }
                   4194:     }
1.474     raeburn  4195:     return %courses;
1.51      www      4196: }
1.37      matthew  4197: 
1.54      www      4198: ###############################################
1.474     raeburn  4199: 
                   4200: sub blockcheck {
1.1062    raeburn  4201:     my ($setters,$activity,$uname,$udom,$url) = @_;
1.490     raeburn  4202: 
                   4203:     if (!defined($udom)) {
                   4204:         $udom = $env{'user.domain'};
                   4205:     }
                   4206:     if (!defined($uname)) {
                   4207:         $uname = $env{'user.name'};
                   4208:     }
                   4209: 
                   4210:     # If uname and udom are for a course, check for blocks in the course.
                   4211: 
                   4212:     if (&Apache::lonnet::is_course($udom,$uname)) {
1.1062    raeburn  4213:         my ($startblock,$endblock,$triggerblock) = 
                   4214:             &get_blocks($setters,$activity,$udom,$uname,$url);
                   4215:         return ($startblock,$endblock,$triggerblock);
1.490     raeburn  4216:     }
1.474     raeburn  4217: 
1.502     raeburn  4218:     my $startblock = 0;
                   4219:     my $endblock = 0;
1.1062    raeburn  4220:     my $triggerblock = '';
1.482     raeburn  4221:     my %live_courses = &findallcourses(undef,$uname,$udom);
1.474     raeburn  4222: 
1.490     raeburn  4223:     # If uname is for a user, and activity is course-specific, i.e.,
                   4224:     # boards, chat or groups, check for blocking in current course only.
1.474     raeburn  4225: 
1.490     raeburn  4226:     if (($activity eq 'boards' || $activity eq 'chat' ||
                   4227:          $activity eq 'groups') && ($env{'request.course.id'})) {
                   4228:         foreach my $key (keys(%live_courses)) {
                   4229:             if ($key ne $env{'request.course.id'}) {
                   4230:                 delete($live_courses{$key});
                   4231:             }
                   4232:         }
                   4233:     }
                   4234: 
                   4235:     my $otheruser = 0;
                   4236:     my %own_courses;
                   4237:     if ((($uname ne $env{'user.name'})) || ($udom ne $env{'user.domain'})) {
                   4238:         # Resource belongs to user other than current user.
                   4239:         $otheruser = 1;
                   4240:         # Gather courses for current user
                   4241:         %own_courses = 
                   4242:             &findallcourses(undef,$env{'user.name'},$env{'user.domain'});
                   4243:     }
                   4244: 
                   4245:     # Gather active course roles - course coordinator, instructor, 
                   4246:     # exam proctor, ta, student, or custom role.
1.474     raeburn  4247: 
                   4248:     foreach my $course (keys(%live_courses)) {
1.482     raeburn  4249:         my ($cdom,$cnum);
                   4250:         if ((defined($env{'course.'.$course.'.domain'})) && (defined($env{'course.'.$course.'.num'}))) {
                   4251:             $cdom = $env{'course.'.$course.'.domain'};
                   4252:             $cnum = $env{'course.'.$course.'.num'};
                   4253:         } else {
1.490     raeburn  4254:             ($cdom,$cnum) = split(/_/,$course); 
1.482     raeburn  4255:         }
                   4256:         my $no_ownblock = 0;
                   4257:         my $no_userblock = 0;
1.533     raeburn  4258:         if ($otheruser && $activity ne 'com') {
1.490     raeburn  4259:             # Check if current user has 'evb' priv for this
                   4260:             if (defined($own_courses{$course})) {
                   4261:                 foreach my $sec (keys(%{$own_courses{$course}})) {
                   4262:                     my $checkrole = 'cm./'.$cdom.'/'.$cnum;
                   4263:                     if ($sec ne 'none') {
                   4264:                         $checkrole .= '/'.$sec;
                   4265:                     }
                   4266:                     if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
                   4267:                         $no_ownblock = 1;
                   4268:                         last;
                   4269:                     }
                   4270:                 }
                   4271:             }
                   4272:             # if they have 'evb' priv and are currently not playing student
                   4273:             next if (($no_ownblock) &&
                   4274:                  ($env{'request.role'} !~ m{^st\./$cdom/$cnum}));
                   4275:         }
1.474     raeburn  4276:         foreach my $sec (keys(%{$live_courses{$course}})) {
1.482     raeburn  4277:             my $checkrole = 'cm./'.$cdom.'/'.$cnum;
1.474     raeburn  4278:             if ($sec ne 'none') {
1.482     raeburn  4279:                 $checkrole .= '/'.$sec;
1.474     raeburn  4280:             }
1.490     raeburn  4281:             if ($otheruser) {
                   4282:                 # Resource belongs to user other than current user.
                   4283:                 # Assemble privs for that user, and check for 'evb' priv.
1.1058    raeburn  4284:                 my (%allroles,%userroles);
                   4285:                 if (ref($live_courses{$course}{$sec}) eq 'ARRAY') {
                   4286:                     foreach my $entry (@{$live_courses{$course}{$sec}}) { 
                   4287:                         my ($trole,$tdom,$tnum,$tsec);
                   4288:                         if ($entry =~ /^cr/) {
                   4289:                             ($trole,$tdom,$tnum,$tsec) = 
                   4290:                                 ($entry =~ m|^(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_username)/?(\w*)$|);
                   4291:                         } else {
                   4292:                            ($trole,$tdom,$tnum,$tsec) = split(/\//,$entry);
                   4293:                         }
                   4294:                         my ($spec,$area,$trest);
                   4295:                         $area = '/'.$tdom.'/'.$tnum;
                   4296:                         $trest = $tnum;
                   4297:                         if ($tsec ne '') {
                   4298:                             $area .= '/'.$tsec;
                   4299:                             $trest .= '/'.$tsec;
                   4300:                         }
                   4301:                         $spec = $trole.'.'.$area;
                   4302:                         if ($trole =~ /^cr/) {
                   4303:                             &Apache::lonnet::custom_roleprivs(\%allroles,$trole,
                   4304:                                                               $tdom,$spec,$trest,$area);
                   4305:                         } else {
                   4306:                             &Apache::lonnet::standard_roleprivs(\%allroles,$trole,
                   4307:                                                                 $tdom,$spec,$trest,$area);
                   4308:                         }
                   4309:                     }
                   4310:                     my ($author,$adv) = &Apache::lonnet::set_userprivs(\%userroles,\%allroles);
                   4311:                     if ($userroles{'user.priv.'.$checkrole} =~ /evb\&([^\:]*)/) {
                   4312:                         if ($1) {
                   4313:                             $no_userblock = 1;
                   4314:                             last;
                   4315:                         }
1.486     raeburn  4316:                     }
                   4317:                 }
1.490     raeburn  4318:             } else {
                   4319:                 # Resource belongs to current user
                   4320:                 # Check for 'evb' priv via lonnet::allowed().
1.482     raeburn  4321:                 if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
                   4322:                     $no_ownblock = 1;
                   4323:                     last;
                   4324:                 }
1.474     raeburn  4325:             }
                   4326:         }
                   4327:         # if they have the evb priv and are currently not playing student
1.482     raeburn  4328:         next if (($no_ownblock) &&
1.491     albertel 4329:                  ($env{'request.role'} !~ m{^st\./\Q$cdom\E/\Q$cnum\E}));
1.482     raeburn  4330:         next if ($no_userblock);
1.474     raeburn  4331: 
1.866     kalberla 4332:         # Retrieve blocking times and identity of locker for course
1.490     raeburn  4333:         # of specified user, unless user has 'evb' privilege.
1.502     raeburn  4334:         
1.1062    raeburn  4335:         my ($start,$end,$trigger) = 
                   4336:             &get_blocks($setters,$activity,$cdom,$cnum,$url);
1.502     raeburn  4337:         if (($start != 0) && 
                   4338:             (($startblock == 0) || ($startblock > $start))) {
                   4339:             $startblock = $start;
1.1062    raeburn  4340:             if ($trigger ne '') {
                   4341:                 $triggerblock = $trigger;
                   4342:             }
1.502     raeburn  4343:         }
                   4344:         if (($end != 0)  &&
                   4345:             (($endblock == 0) || ($endblock < $end))) {
                   4346:             $endblock = $end;
1.1062    raeburn  4347:             if ($trigger ne '') {
                   4348:                 $triggerblock = $trigger;
                   4349:             }
1.502     raeburn  4350:         }
1.490     raeburn  4351:     }
1.1062    raeburn  4352:     return ($startblock,$endblock,$triggerblock);
1.490     raeburn  4353: }
                   4354: 
                   4355: sub get_blocks {
1.1062    raeburn  4356:     my ($setters,$activity,$cdom,$cnum,$url) = @_;
1.490     raeburn  4357:     my $startblock = 0;
                   4358:     my $endblock = 0;
1.1062    raeburn  4359:     my $triggerblock = '';
1.490     raeburn  4360:     my $course = $cdom.'_'.$cnum;
                   4361:     $setters->{$course} = {};
                   4362:     $setters->{$course}{'staff'} = [];
                   4363:     $setters->{$course}{'times'} = [];
1.1062    raeburn  4364:     $setters->{$course}{'triggers'} = [];
                   4365:     my (@blockers,%triggered);
                   4366:     my $now = time;
                   4367:     my %commblocks = &Apache::lonnet::get_comm_blocks($cdom,$cnum);
                   4368:     if ($activity eq 'docs') {
                   4369:         @blockers = &Apache::lonnet::has_comm_blocking('bre',undef,$url,\%commblocks);
                   4370:         foreach my $block (@blockers) {
                   4371:             if ($block =~ /^firstaccess____(.+)$/) {
                   4372:                 my $item = $1;
                   4373:                 my $type = 'map';
                   4374:                 my $timersymb = $item;
                   4375:                 if ($item eq 'course') {
                   4376:                     $type = 'course';
                   4377:                 } elsif ($item =~ /___\d+___/) {
                   4378:                     $type = 'resource';
                   4379:                 } else {
                   4380:                     $timersymb = &Apache::lonnet::symbread($item);
                   4381:                 }
                   4382:                 my $start = $env{'course.'.$cdom.'_'.$cnum.'.firstaccess.'.$timersymb};
                   4383:                 my $end = $start + $env{'course.'.$cdom.'_'.$cnum.'.timerinterval.'.$timersymb};
                   4384:                 $triggered{$block} = {
                   4385:                                        start => $start,
                   4386:                                        end   => $end,
                   4387:                                        type  => $type,
                   4388:                                      };
                   4389:             }
                   4390:         }
                   4391:     } else {
                   4392:         foreach my $block (keys(%commblocks)) {
                   4393:             if ($block =~ m/^(\d+)____(\d+)$/) { 
                   4394:                 my ($start,$end) = ($1,$2);
                   4395:                 if ($start <= time && $end >= time) {
                   4396:                     if (ref($commblocks{$block}) eq 'HASH') {
                   4397:                         if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   4398:                             if ($commblocks{$block}{'blocks'}{$activity} eq 'on') {
                   4399:                                 unless(grep(/^\Q$block\E$/,@blockers)) {
                   4400:                                     push(@blockers,$block);
                   4401:                                 }
                   4402:                             }
                   4403:                         }
                   4404:                     }
                   4405:                 }
                   4406:             } elsif ($block =~ /^firstaccess____(.+)$/) {
                   4407:                 my $item = $1;
                   4408:                 my $timersymb = $item; 
                   4409:                 my $type = 'map';
                   4410:                 if ($item eq 'course') {
                   4411:                     $type = 'course';
                   4412:                 } elsif ($item =~ /___\d+___/) {
                   4413:                     $type = 'resource';
                   4414:                 } else {
                   4415:                     $timersymb = &Apache::lonnet::symbread($item);
                   4416:                 }
                   4417:                 my $start = $env{'course.'.$cdom.'_'.$cnum.'.firstaccess.'.$timersymb};
                   4418:                 my $end = $start + $env{'course.'.$cdom.'_'.$cnum.'.timerinterval.'.$timersymb}; 
                   4419:                 if ($start && $end) {
                   4420:                     if (($start <= time) && ($end >= time)) {
                   4421:                         unless (grep(/^\Q$block\E$/,@blockers)) {
                   4422:                             push(@blockers,$block);
                   4423:                             $triggered{$block} = {
                   4424:                                                    start => $start,
                   4425:                                                    end   => $end,
                   4426:                                                    type  => $type,
                   4427:                                                  };
                   4428:                         }
                   4429:                     }
1.490     raeburn  4430:                 }
1.1062    raeburn  4431:             }
                   4432:         }
                   4433:     }
                   4434:     foreach my $blocker (@blockers) {
                   4435:         my ($staff_name,$staff_dom,$title,$blocks) =
                   4436:             &parse_block_record($commblocks{$blocker});
                   4437:         push(@{$$setters{$course}{'staff'}},[$staff_name,$staff_dom]);
                   4438:         my ($start,$end,$triggertype);
                   4439:         if ($blocker =~ m/^(\d+)____(\d+)$/) {
                   4440:             ($start,$end) = ($1,$2);
                   4441:         } elsif (ref($triggered{$blocker}) eq 'HASH') {
                   4442:             $start = $triggered{$blocker}{'start'};
                   4443:             $end = $triggered{$blocker}{'end'};
                   4444:             $triggertype = $triggered{$blocker}{'type'};
                   4445:         }
                   4446:         if ($start) {
                   4447:             push(@{$$setters{$course}{'times'}}, [$start,$end]);
                   4448:             if ($triggertype) {
                   4449:                 push(@{$$setters{$course}{'triggers'}},$triggertype);
                   4450:             } else {
                   4451:                 push(@{$$setters{$course}{'triggers'}},0);
                   4452:             }
                   4453:             if ( ($startblock == 0) || ($startblock > $start) ) {
                   4454:                 $startblock = $start;
                   4455:                 if ($triggertype) {
                   4456:                     $triggerblock = $blocker;
1.474     raeburn  4457:                 }
                   4458:             }
1.1062    raeburn  4459:             if ( ($endblock == 0) || ($endblock < $end) ) {
                   4460:                $endblock = $end;
                   4461:                if ($triggertype) {
                   4462:                    $triggerblock = $blocker;
                   4463:                }
                   4464:             }
1.474     raeburn  4465:         }
                   4466:     }
1.1062    raeburn  4467:     return ($startblock,$endblock,$triggerblock);
1.474     raeburn  4468: }
                   4469: 
                   4470: sub parse_block_record {
                   4471:     my ($record) = @_;
                   4472:     my ($setuname,$setudom,$title,$blocks);
                   4473:     if (ref($record) eq 'HASH') {
                   4474:         ($setuname,$setudom) = split(/:/,$record->{'setter'});
                   4475:         $title = &unescape($record->{'event'});
                   4476:         $blocks = $record->{'blocks'};
                   4477:     } else {
                   4478:         my @data = split(/:/,$record,3);
                   4479:         if (scalar(@data) eq 2) {
                   4480:             $title = $data[1];
                   4481:             ($setuname,$setudom) = split(/@/,$data[0]);
                   4482:         } else {
                   4483:             ($setuname,$setudom,$title) = @data;
                   4484:         }
                   4485:         $blocks = { 'com' => 'on' };
                   4486:     }
                   4487:     return ($setuname,$setudom,$title,$blocks);
                   4488: }
                   4489: 
1.854     kalberla 4490: sub blocking_status {
1.1062    raeburn  4491:     my ($activity,$uname,$udom,$url) = @_;
1.1061    raeburn  4492:     my %setters;
1.890     droeschl 4493: 
1.1061    raeburn  4494: # check for active blocking
1.1062    raeburn  4495:     my ($startblock,$endblock,$triggerblock) = 
                   4496:         &blockcheck(\%setters,$activity,$uname,$udom,$url);
                   4497:     my $blocked = 0;
                   4498:     if ($startblock && $endblock) {
                   4499:         $blocked = 1;
                   4500:     }
1.890     droeschl 4501: 
1.1061    raeburn  4502: # caller just wants to know whether a block is active
                   4503:     if (!wantarray) { return $blocked; }
                   4504: 
                   4505: # build a link to a popup window containing the details
                   4506:     my $querystring  = "?activity=$activity";
                   4507: # $uname and $udom decide whose portfolio the user is trying to look at
1.1062    raeburn  4508:     if ($activity eq 'port') {
                   4509:         $querystring .= "&amp;udom=$udom"      if $udom;
                   4510:         $querystring .= "&amp;uname=$uname"    if $uname;
                   4511:     } elsif ($activity eq 'docs') {
                   4512:         $querystring .= '&amp;url='.&HTML::Entities::encode($url,'&"');
                   4513:     }
1.1061    raeburn  4514: 
                   4515:     my $output .= <<'END_MYBLOCK';
                   4516: function openWindow(url, wdwName, w, h, toolbar,scrollbar) {
                   4517:     var options = "width=" + w + ",height=" + h + ",";
                   4518:     options += "resizable=yes,scrollbars="+scrollbar+",status=no,";
                   4519:     options += "menubar=no,toolbar="+toolbar+",location=no,directories=no";
                   4520:     var newWin = window.open(url, wdwName, options);
                   4521:     newWin.focus();
                   4522: }
1.890     droeschl 4523: END_MYBLOCK
1.854     kalberla 4524: 
1.1061    raeburn  4525:     $output = Apache::lonhtmlcommon::scripttag($output);
1.890     droeschl 4526:   
1.1061    raeburn  4527:     my $popupUrl = "/adm/blockingstatus/$querystring";
1.1062    raeburn  4528:     my $text = &mt('Communication Blocked');
                   4529:     if ($activity eq 'docs') {
                   4530:         $text = &mt('Content Access Blocked');
1.1063    raeburn  4531:     } elsif ($activity eq 'printout') {
                   4532:         $text = &mt('Printing Blocked');
1.1062    raeburn  4533:     }
1.1061    raeburn  4534:     $output .= <<"END_BLOCK";
1.867     kalberla 4535: <div class='LC_comblock'>
1.869     kalberla 4536:   <a onclick='openWindow("$popupUrl","Blocking Table",600,300,"no","no");return false;' href='/adm/blockingstatus/$querystring'
1.890     droeschl 4537:   title='$text'>
                   4538:   <img class='LC_noBorder LC_middle' title='$text' src='/res/adm/pages/comblock.png' alt='$text'/></a>
1.869     kalberla 4539:   <a onclick='openWindow("$popupUrl","Blocking Table",600,300,"no","no");return false;' href='/adm/blockingstatus/$querystring' 
1.890     droeschl 4540:   title='$text'>$text</a>
1.867     kalberla 4541: </div>
                   4542: 
                   4543: END_BLOCK
1.474     raeburn  4544: 
1.1061    raeburn  4545:     return ($blocked, $output);
1.854     kalberla 4546: }
1.490     raeburn  4547: 
1.60      matthew  4548: ###############################################
                   4549: 
1.682     raeburn  4550: sub check_ip_acc {
                   4551:     my ($acc)=@_;
                   4552:     &Apache::lonxml::debug("acc is $acc");
                   4553:     if (!defined($acc) || $acc =~ /^\s*$/ || $acc =~/^\s*no\s*$/i) {
                   4554:         return 1;
                   4555:     }
                   4556:     my $allowed=0;
                   4557:     my $ip=$env{'request.host'} || $ENV{'REMOTE_ADDR'};
                   4558: 
                   4559:     my $name;
                   4560:     foreach my $pattern (split(',',$acc)) {
                   4561:         $pattern =~ s/^\s*//;
                   4562:         $pattern =~ s/\s*$//;
                   4563:         if ($pattern =~ /\*$/) {
                   4564:             #35.8.*
                   4565:             $pattern=~s/\*//;
                   4566:             if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
                   4567:         } elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {
                   4568:             #35.8.3.[34-56]
                   4569:             my $low=$2;
                   4570:             my $high=$3;
                   4571:             $pattern=$1;
                   4572:             if ($ip =~ /^\Q$pattern\E/) {
                   4573:                 my $last=(split(/\./,$ip))[3];
                   4574:                 if ($last <=$high && $last >=$low) { $allowed=1; }
                   4575:             }
                   4576:         } elsif ($pattern =~ /^\*/) {
                   4577:             #*.msu.edu
                   4578:             $pattern=~s/\*//;
                   4579:             if (!defined($name)) {
                   4580:                 use Socket;
                   4581:                 my $netaddr=inet_aton($ip);
                   4582:                 ($name)=gethostbyaddr($netaddr,AF_INET);
                   4583:             }
                   4584:             if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
                   4585:         } elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
                   4586:             #127.0.0.1
                   4587:             if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
                   4588:         } else {
                   4589:             #some.name.com
                   4590:             if (!defined($name)) {
                   4591:                 use Socket;
                   4592:                 my $netaddr=inet_aton($ip);
                   4593:                 ($name)=gethostbyaddr($netaddr,AF_INET);
                   4594:             }
                   4595:             if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
                   4596:         }
                   4597:         if ($allowed) { last; }
                   4598:     }
                   4599:     return $allowed;
                   4600: }
                   4601: 
                   4602: ###############################################
                   4603: 
1.60      matthew  4604: =pod
                   4605: 
1.112     bowersj2 4606: =head1 Domain Template Functions
                   4607: 
                   4608: =over 4
                   4609: 
                   4610: =item * &determinedomain()
1.60      matthew  4611: 
                   4612: Inputs: $domain (usually will be undef)
                   4613: 
1.63      www      4614: Returns: Determines which domain should be used for designs
1.60      matthew  4615: 
                   4616: =cut
1.54      www      4617: 
1.60      matthew  4618: ###############################################
1.63      www      4619: sub determinedomain {
                   4620:     my $domain=shift;
1.531     albertel 4621:     if (! $domain) {
1.60      matthew  4622:         # Determine domain if we have not been given one
1.893     raeburn  4623:         $domain = &Apache::lonnet::default_login_domain();
1.258     albertel 4624:         if ($env{'user.domain'}) { $domain=$env{'user.domain'}; }
                   4625:         if ($env{'request.role.domain'}) { 
                   4626:             $domain=$env{'request.role.domain'}; 
1.60      matthew  4627:         }
                   4628:     }
1.63      www      4629:     return $domain;
                   4630: }
                   4631: ###############################################
1.517     raeburn  4632: 
1.518     albertel 4633: sub devalidate_domconfig_cache {
                   4634:     my ($udom)=@_;
                   4635:     &Apache::lonnet::devalidate_cache_new('domainconfig',$udom);
                   4636: }
                   4637: 
                   4638: # ---------------------- Get domain configuration for a domain
                   4639: sub get_domainconf {
                   4640:     my ($udom) = @_;
                   4641:     my $cachetime=1800;
                   4642:     my ($result,$cached)=&Apache::lonnet::is_cached_new('domainconfig',$udom);
                   4643:     if (defined($cached)) { return %{$result}; }
                   4644: 
                   4645:     my %domconfig = &Apache::lonnet::get_dom('configuration',
1.948     raeburn  4646: 					     ['login','rolecolors','autoenroll'],$udom);
1.632     raeburn  4647:     my (%designhash,%legacy);
1.518     albertel 4648:     if (keys(%domconfig) > 0) {
                   4649:         if (ref($domconfig{'login'}) eq 'HASH') {
1.632     raeburn  4650:             if (keys(%{$domconfig{'login'}})) {
                   4651:                 foreach my $key (keys(%{$domconfig{'login'}})) {
1.699     raeburn  4652:                     if (ref($domconfig{'login'}{$key}) eq 'HASH') {
1.946     raeburn  4653:                         if ($key eq 'loginvia') {
                   4654:                             if (ref($domconfig{'login'}{'loginvia'}) eq 'HASH') {
1.1013    raeburn  4655:                                 foreach my $hostname (keys(%{$domconfig{'login'}{'loginvia'}})) {
1.948     raeburn  4656:                                     if (ref($domconfig{'login'}{'loginvia'}{$hostname}) eq 'HASH') {
                   4657:                                         if ($domconfig{'login'}{'loginvia'}{$hostname}{'server'}) {
                   4658:                                             my $server = $domconfig{'login'}{'loginvia'}{$hostname}{'server'};
                   4659:                                             $designhash{$udom.'.login.loginvia'} = $server;
                   4660:                                             if ($domconfig{'login'}{'loginvia'}{$hostname}{'serverpath'} eq 'custom') {
                   4661: 
                   4662:                                                 $designhash{$udom.'.login.loginvia_'.$hostname} = $server.':'.$domconfig{'login'}{'loginvia'}{$hostname}{'custompath'};
                   4663:                                             } else {
1.1013    raeburn  4664:                                                 $designhash{$udom.'.login.loginvia_'.$hostname} = $server.':'.$domconfig{'login'}{'loginvia'}{$hostname}{'serverpath'};
1.948     raeburn  4665:                                             }
                   4666:                                             if ($domconfig{'login'}{'loginvia'}{$hostname}{'exempt'}) {
                   4667:                                                 $designhash{$udom.'.login.loginvia_exempt_'.$hostname} = $domconfig{'login'}{'loginvia'}{$hostname}{'exempt'};
                   4668:                                             }
1.946     raeburn  4669:                                         }
                   4670:                                     }
                   4671:                                 }
                   4672:                             }
                   4673:                         } else {
                   4674:                             foreach my $img (keys(%{$domconfig{'login'}{$key}})) {
                   4675:                                 $designhash{$udom.'.login.'.$key.'_'.$img} = 
                   4676:                                     $domconfig{'login'}{$key}{$img};
                   4677:                             }
1.699     raeburn  4678:                         }
                   4679:                     } else {
                   4680:                         $designhash{$udom.'.login.'.$key}=$domconfig{'login'}{$key};
                   4681:                     }
1.632     raeburn  4682:                 }
                   4683:             } else {
                   4684:                 $legacy{'login'} = 1;
1.518     albertel 4685:             }
1.632     raeburn  4686:         } else {
                   4687:             $legacy{'login'} = 1;
1.518     albertel 4688:         }
                   4689:         if (ref($domconfig{'rolecolors'}) eq 'HASH') {
1.632     raeburn  4690:             if (keys(%{$domconfig{'rolecolors'}})) {
                   4691:                 foreach my $role (keys(%{$domconfig{'rolecolors'}})) {
                   4692:                     if (ref($domconfig{'rolecolors'}{$role}) eq 'HASH') {
                   4693:                         foreach my $item (keys(%{$domconfig{'rolecolors'}{$role}})) {
                   4694:                             $designhash{$udom.'.'.$role.'.'.$item}=$domconfig{'rolecolors'}{$role}{$item};
                   4695:                         }
1.518     albertel 4696:                     }
                   4697:                 }
1.632     raeburn  4698:             } else {
                   4699:                 $legacy{'rolecolors'} = 1;
1.518     albertel 4700:             }
1.632     raeburn  4701:         } else {
                   4702:             $legacy{'rolecolors'} = 1;
1.518     albertel 4703:         }
1.948     raeburn  4704:         if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   4705:             if ($domconfig{'autoenroll'}{'co-owners'}) {
                   4706:                 $designhash{$udom.'.autoassign.co-owners'}=$domconfig{'autoenroll'}{'co-owners'};
                   4707:             }
                   4708:         }
1.632     raeburn  4709:         if (keys(%legacy) > 0) {
                   4710:             my %legacyhash = &get_legacy_domconf($udom);
                   4711:             foreach my $item (keys(%legacyhash)) {
                   4712:                 if ($item =~ /^\Q$udom\E\.login/) {
                   4713:                     if ($legacy{'login'}) { 
                   4714:                         $designhash{$item} = $legacyhash{$item};
                   4715:                     }
                   4716:                 } else {
                   4717:                     if ($legacy{'rolecolors'}) {
                   4718:                         $designhash{$item} = $legacyhash{$item};
                   4719:                     }
1.518     albertel 4720:                 }
                   4721:             }
                   4722:         }
1.632     raeburn  4723:     } else {
                   4724:         %designhash = &get_legacy_domconf($udom); 
1.518     albertel 4725:     }
                   4726:     &Apache::lonnet::do_cache_new('domainconfig',$udom,\%designhash,
                   4727: 				  $cachetime);
                   4728:     return %designhash;
                   4729: }
                   4730: 
1.632     raeburn  4731: sub get_legacy_domconf {
                   4732:     my ($udom) = @_;
                   4733:     my %legacyhash;
                   4734:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
                   4735:     my $designfile =  $designdir.'/'.$udom.'.tab';
                   4736:     if (-e $designfile) {
                   4737:         if ( open (my $fh,"<$designfile") ) {
                   4738:             while (my $line = <$fh>) {
                   4739:                 next if ($line =~ /^\#/);
                   4740:                 chomp($line);
                   4741:                 my ($key,$val)=(split(/\=/,$line));
                   4742:                 if ($val) { $legacyhash{$udom.'.'.$key}=$val; }
                   4743:             }
                   4744:             close($fh);
                   4745:         }
                   4746:     }
1.1026    raeburn  4747:     if (-e $Apache::lonnet::perlvar{'lonDocRoot'}.'/adm/lonDomLogos/'.$udom.'.gif') {
1.632     raeburn  4748:         $legacyhash{$udom.'.login.domlogo'} = "/adm/lonDomLogos/$udom.gif";
                   4749:     }
                   4750:     return %legacyhash;
                   4751: }
                   4752: 
1.63      www      4753: =pod
                   4754: 
1.112     bowersj2 4755: =item * &domainlogo()
1.63      www      4756: 
                   4757: Inputs: $domain (usually will be undef)
                   4758: 
                   4759: Returns: A link to a domain logo, if the domain logo exists.
                   4760: If the domain logo does not exist, a description of the domain.
                   4761: 
                   4762: =cut
1.112     bowersj2 4763: 
1.63      www      4764: ###############################################
                   4765: sub domainlogo {
1.517     raeburn  4766:     my $domain = &determinedomain(shift);
1.518     albertel 4767:     my %designhash = &get_domainconf($domain);    
1.517     raeburn  4768:     # See if there is a logo
                   4769:     if ($designhash{$domain.'.login.domlogo'} ne '') {
1.519     raeburn  4770:         my $imgsrc = $designhash{$domain.'.login.domlogo'};
1.538     albertel 4771:         if ($imgsrc =~ m{^/(adm|res)/}) {
                   4772: 	    if ($imgsrc =~ m{^/res/}) {
                   4773: 		my $local_name = &Apache::lonnet::filelocation('',$imgsrc);
                   4774: 		&Apache::lonnet::repcopy($local_name);
                   4775: 	    }
                   4776: 	   $imgsrc = &lonhttpdurl($imgsrc);
1.519     raeburn  4777:         } 
                   4778:         return '<img src="'.$imgsrc.'" alt="'.$domain.'" />';
1.514     albertel 4779:     } elsif (defined(&Apache::lonnet::domain($domain,'description'))) {
                   4780:         return &Apache::lonnet::domain($domain,'description');
1.59      www      4781:     } else {
1.60      matthew  4782:         return '';
1.59      www      4783:     }
                   4784: }
1.63      www      4785: ##############################################
                   4786: 
                   4787: =pod
                   4788: 
1.112     bowersj2 4789: =item * &designparm()
1.63      www      4790: 
                   4791: Inputs: $which parameter; $domain (usually will be undef)
                   4792: 
                   4793: Returns: value of designparamter $which
                   4794: 
                   4795: =cut
1.112     bowersj2 4796: 
1.397     albertel 4797: 
1.400     albertel 4798: ##############################################
1.397     albertel 4799: sub designparm {
                   4800:     my ($which,$domain)=@_;
                   4801:     if (exists($env{'environment.color.'.$which})) {
1.817     bisitz   4802:         return $env{'environment.color.'.$which};
1.96      www      4803:     }
1.63      www      4804:     $domain=&determinedomain($domain);
1.1016    raeburn  4805:     my %domdesign;
                   4806:     unless ($domain eq 'public') {
                   4807:         %domdesign = &get_domainconf($domain);
                   4808:     }
1.520     raeburn  4809:     my $output;
1.517     raeburn  4810:     if ($domdesign{$domain.'.'.$which} ne '') {
1.817     bisitz   4811:         $output = $domdesign{$domain.'.'.$which};
1.63      www      4812:     } else {
1.520     raeburn  4813:         $output = $defaultdesign{$which};
                   4814:     }
                   4815:     if (($which =~ /^(student|coordinator|author|admin)\.img$/) ||
1.635     raeburn  4816:         ($which =~ /login\.(img|logo|domlogo|login)/)) {
1.538     albertel 4817:         if ($output =~ m{^/(adm|res)/}) {
1.817     bisitz   4818:             if ($output =~ m{^/res/}) {
                   4819:                 my $local_name = &Apache::lonnet::filelocation('',$output);
                   4820:                 &Apache::lonnet::repcopy($local_name);
                   4821:             }
1.520     raeburn  4822:             $output = &lonhttpdurl($output);
                   4823:         }
1.63      www      4824:     }
1.520     raeburn  4825:     return $output;
1.63      www      4826: }
1.59      www      4827: 
1.822     bisitz   4828: ##############################################
                   4829: =pod
                   4830: 
1.832     bisitz   4831: =item * &authorspace()
                   4832: 
1.1028    raeburn  4833: Inputs: $url (usually will be undef).
1.832     bisitz   4834: 
1.1028    raeburn  4835: Returns: Path to Construction Space containing the resource or 
                   4836:          directory being viewed (or for which action is being taken). 
                   4837:          If $url is provided, and begins /priv/<domain>/<uname>
                   4838:          the path will be that portion of the $context argument.
                   4839:          Otherwise the path will be for the author space of the current
                   4840:          user when the current role is author, or for that of the 
                   4841:          co-author/assistant co-author space when the current role 
                   4842:          is co-author or assistant co-author.
1.832     bisitz   4843: 
                   4844: =cut
                   4845: 
                   4846: sub authorspace {
1.1028    raeburn  4847:     my ($url) = @_;
                   4848:     if ($url ne '') {
                   4849:         if ($url =~ m{^(/priv/$match_domain/$match_username/)}) {
                   4850:            return $1;
                   4851:         }
                   4852:     }
1.832     bisitz   4853:     my $caname = '';
1.1024    www      4854:     my $cadom = '';
1.1028    raeburn  4855:     if ($env{'request.role'} =~ /^(?:ca|aa)/) {
1.1024    www      4856:         ($cadom,$caname) =
1.832     bisitz   4857:             ($env{'request.role'}=~/($match_domain)\/($match_username)$/);
1.1028    raeburn  4858:     } elsif ($env{'request.role'} =~ m{^au\./($match_domain)/}) {
1.832     bisitz   4859:         $caname = $env{'user.name'};
1.1024    www      4860:         $cadom = $env{'user.domain'};
1.832     bisitz   4861:     }
1.1028    raeburn  4862:     if (($caname ne '') && ($cadom ne '')) {
                   4863:         return "/priv/$cadom/$caname/";
                   4864:     }
                   4865:     return;
1.832     bisitz   4866: }
                   4867: 
                   4868: ##############################################
                   4869: =pod
                   4870: 
1.822     bisitz   4871: =item * &head_subbox()
                   4872: 
                   4873: Inputs: $content (contains HTML code with page functions, etc.)
                   4874: 
                   4875: Returns: HTML div with $content
                   4876:          To be included in page header
                   4877: 
                   4878: =cut
                   4879: 
                   4880: sub head_subbox {
                   4881:     my ($content)=@_;
                   4882:     my $output =
1.993     raeburn  4883:         '<div class="LC_head_subbox">'
1.822     bisitz   4884:        .$content
                   4885:        .'</div>'
                   4886: }
                   4887: 
                   4888: ##############################################
                   4889: =pod
                   4890: 
                   4891: =item * &CSTR_pageheader()
                   4892: 
1.1026    raeburn  4893: Input: (optional) filename from which breadcrumb trail is built.
                   4894:        In most cases no input as needed, as $env{'request.filename'}
                   4895:        is appropriate for use in building the breadcrumb trail.
1.822     bisitz   4896: 
                   4897: Returns: HTML div with CSTR path and recent box
                   4898:          To be included on Construction Space pages
                   4899: 
                   4900: =cut
                   4901: 
                   4902: sub CSTR_pageheader {
1.1026    raeburn  4903:     my ($trailfile) = @_;
                   4904:     if ($trailfile eq '') {
                   4905:         $trailfile = $env{'request.filename'};
                   4906:     }
                   4907: 
                   4908: # this is for resources; directories have customtitle, and crumbs
                   4909: # and select recent are created in lonpubdir.pm
                   4910: 
                   4911:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
1.1022    www      4912:     my ($udom,$uname,$thisdisfn)=
1.1075.2.29  raeburn  4913:         ($trailfile =~ m{^\Q$londocroot\E/priv/([^/]+)/([^/]+)(?:|/(.*))$});
1.1026    raeburn  4914:     my $formaction = "/priv/$udom/$uname/$thisdisfn";
                   4915:     $formaction =~ s{/+}{/}g;
1.822     bisitz   4916: 
                   4917:     my $parentpath = '';
                   4918:     my $lastitem = '';
                   4919:     if ($thisdisfn =~ m-(.+/)([^/]*)$-) {
                   4920:         $parentpath = $1;
                   4921:         $lastitem = $2;
                   4922:     } else {
                   4923:         $lastitem = $thisdisfn;
                   4924:     }
1.921     bisitz   4925: 
                   4926:     my $output =
1.822     bisitz   4927:          '<div>'
                   4928:         .&Apache::loncommon::help_open_menu('','',3,'Authoring') #FIXME: Broken? Where is it?
                   4929:         .'<b>'.&mt('Construction Space:').'</b> '
                   4930:         .'<form name="dirs" method="post" action="'.$formaction
1.921     bisitz   4931:         .'" target="_top">' #FIXME lonpubdir: target="_parent"
1.1024    www      4932:         .&Apache::lonhtmlcommon::crumbs($uname.'/'.$parentpath,'_top','/priv/'.$udom,undef,undef);
1.921     bisitz   4933: 
                   4934:     if ($lastitem) {
                   4935:         $output .=
                   4936:              '<span class="LC_filename">'
                   4937:             .$lastitem
                   4938:             .'</span>';
                   4939:     }
                   4940:     $output .=
                   4941:          '<br />'
1.822     bisitz   4942:         #FIXME lonpubdir: &Apache::lonhtmlcommon::crumbs($uname.$thisdisfn.'/','_top','/priv','','+1',1)."</b></tt><br />"
                   4943:         .&Apache::lonhtmlcommon::select_recent('construct','recent','this.form.action=this.form.recent.value;this.form.submit()')
                   4944:         .'</form>'
                   4945:         .&Apache::lonmenu::constspaceform()
                   4946:         .'</div>';
1.921     bisitz   4947: 
                   4948:     return $output;
1.822     bisitz   4949: }
                   4950: 
1.60      matthew  4951: ###############################################
                   4952: ###############################################
                   4953: 
                   4954: =pod
                   4955: 
1.112     bowersj2 4956: =back
                   4957: 
1.549     albertel 4958: =head1 HTML Helpers
1.112     bowersj2 4959: 
                   4960: =over 4
                   4961: 
                   4962: =item * &bodytag()
1.60      matthew  4963: 
                   4964: Returns a uniform header for LON-CAPA web pages.
                   4965: 
                   4966: Inputs: 
                   4967: 
1.112     bowersj2 4968: =over 4
                   4969: 
                   4970: =item * $title, A title to be displayed on the page.
                   4971: 
                   4972: =item * $function, the current role (can be undef).
                   4973: 
                   4974: =item * $addentries, extra parameters for the <body> tag.
                   4975: 
                   4976: =item * $bodyonly, if defined, only return the <body> tag.
                   4977: 
                   4978: =item * $domain, if defined, force a given domain.
                   4979: 
                   4980: =item * $forcereg, if page should register as content page (relevant for 
1.86      www      4981:             text interface only)
1.60      matthew  4982: 
1.814     bisitz   4983: =item * $no_nav_bar, if true, keep the 'what is this' info but remove the
                   4984:                      navigational links
1.317     albertel 4985: 
1.338     albertel 4986: =item * $bgcolor, used to override the bgcolor on a webpage to a specific value
                   4987: 
1.1075.2.12  raeburn  4988: =item * $no_inline_link, if true and in remote mode, don't show the
                   4989:          'Switch To Inline Menu' link
                   4990: 
1.460     albertel 4991: =item * $args, optional argument valid values are
                   4992:             no_auto_mt_title -> prevents &mt()ing the title arg
1.562     albertel 4993:             inherit_jsmath -> when creating popup window in a page,
                   4994:                               should it have jsmath forced on by the
                   4995:                               current page
1.460     albertel 4996: 
1.1075.2.15  raeburn  4997: =item * $advtoolsref, optional argument, ref to an array containing
                   4998:             inlineremote items to be added in "Functions" menu below
                   4999:             breadcrumbs.
                   5000: 
1.112     bowersj2 5001: =back
                   5002: 
1.60      matthew  5003: Returns: A uniform header for LON-CAPA web pages.  
                   5004: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
                   5005: If $bodyonly is undef or zero, an html string containing a <body> tag and 
                   5006: other decorations will be returned.
                   5007: 
                   5008: =cut
                   5009: 
1.54      www      5010: sub bodytag {
1.831     bisitz   5011:     my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,
1.1075.2.15  raeburn  5012:         $no_nav_bar,$bgcolor,$no_inline_link,$args,$advtoolsref)=@_;
1.339     albertel 5013: 
1.954     raeburn  5014:     my $public;
                   5015:     if ((($env{'user.name'} eq 'public') && ($env{'user.domain'} eq 'public'))
                   5016:         || ($env{'user.name'} eq '') && ($env{'user.domain'} eq '')) {
                   5017:         $public = 1;
                   5018:     }
1.460     albertel 5019:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
1.339     albertel 5020: 
1.183     matthew  5021:     $function = &get_users_function() if (!$function);
1.339     albertel 5022:     my $img =    &designparm($function.'.img',$domain);
                   5023:     my $font =   &designparm($function.'.font',$domain);
                   5024:     my $pgbg   = $bgcolor || &designparm($function.'.pgbg',$domain);
                   5025: 
1.803     bisitz   5026:     my %design = ( 'style'   => 'margin-top: 0',
1.535     albertel 5027: 		   'bgcolor' => $pgbg,
1.339     albertel 5028: 		   'text'    => $font,
                   5029:                    'alink'   => &designparm($function.'.alink',$domain),
                   5030: 		   'vlink'   => &designparm($function.'.vlink',$domain),
                   5031: 		   'link'    => &designparm($function.'.link',$domain),);
1.438     albertel 5032:     @design{keys(%$addentries)} = @$addentries{keys(%$addentries)}; 
1.339     albertel 5033: 
1.63      www      5034:  # role and realm
1.378     raeburn  5035:     my ($role,$realm) = split(/\./,$env{'request.role'},2);
                   5036:     if ($role  eq 'ca') {
1.479     albertel 5037:         my ($rdom,$rname) = ($realm =~ m{^/($match_domain)/($match_username)$});
1.500     albertel 5038:         $realm = &plainname($rname,$rdom);
1.378     raeburn  5039:     } 
1.55      www      5040: # realm
1.258     albertel 5041:     if ($env{'request.course.id'}) {
1.378     raeburn  5042:         if ($env{'request.role'} !~ /^cr/) {
                   5043:             $role = &Apache::lonnet::plaintext($role,&course_type());
                   5044:         }
1.898     raeburn  5045:         if ($env{'request.course.sec'}) {
                   5046:             $role .= ('&nbsp;'x2).'-&nbsp;'.&mt('section:').'&nbsp;'.$env{'request.course.sec'};
                   5047:         }   
1.359     albertel 5048: 	$realm = $env{'course.'.$env{'request.course.id'}.'.description'};
1.378     raeburn  5049:     } else {
                   5050:         $role = &Apache::lonnet::plaintext($role);
1.54      www      5051:     }
1.433     albertel 5052: 
1.359     albertel 5053:     if (!$realm) { $realm='&nbsp;'; }
1.330     albertel 5054: 
1.438     albertel 5055:     my $extra_body_attr = &make_attr_string($forcereg,\%design);
1.329     albertel 5056: 
1.101     www      5057: # construct main body tag
1.359     albertel 5058:     my $bodytag = "<body $extra_body_attr>".
1.562     albertel 5059: 	&Apache::lontexconvert::init_math_support($args->{'inherit_jsmath'});
1.252     albertel 5060: 
1.530     albertel 5061:     if ($bodyonly) {
1.60      matthew  5062:         return $bodytag;
1.798     tempelho 5063:     } 
1.359     albertel 5064: 
1.410     albertel 5065:     my $name = &plainname($env{'user.name'},$env{'user.domain'});
1.954     raeburn  5066:     if ($public) {
1.433     albertel 5067: 	undef($role);
1.434     albertel 5068:     } else {
1.1070    raeburn  5069: 	$name = &aboutmewrapper($name,$env{'user.name'},$env{'user.domain'},
                   5070:                                 undef,'LC_menubuttons_link');
1.433     albertel 5071:     }
1.359     albertel 5072:     
1.762     bisitz   5073:     my $titleinfo = '<h1>'.$title.'</h1>';
1.359     albertel 5074:     #
                   5075:     # Extra info if you are the DC
                   5076:     my $dc_info = '';
                   5077:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
                   5078:                         $env{'course.'.$env{'request.course.id'}.
                   5079:                                  '.domain'}.'/'})) {
                   5080:         my $cid = $env{'request.course.id'};
1.917     raeburn  5081:         $dc_info = $cid.' '.$env{'course.'.$cid.'.internal.coursecode'};
1.380     www      5082:         $dc_info =~ s/\s+$//;
1.359     albertel 5083:     }
                   5084: 
1.898     raeburn  5085:     $role = '<span class="LC_nobreak">('.$role.')</span>' if $role;
1.853     droeschl 5086:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
                   5087: 
1.1075.2.13  raeburn  5088:     if ($no_nav_bar || $env{'form.inhibitmenu'} eq 'yes') { 
                   5089:         return $bodytag; 
                   5090:     }
1.903     droeschl 5091: 
1.1075.2.13  raeburn  5092:     if ($env{'request.state'} eq 'construct') { $forcereg=1; }
                   5093: 
1.1075.2.21  raeburn  5094:     my $funclist;
                   5095:     if (($env{'environment.remote'} eq 'on') && ($env{'request.state'} ne 'construct')) {
                   5096:         $bodytag .= Apache::lonhtmlcommon::scripttag(Apache::lonmenu::utilityfunctions(), 'start')."\n".
                   5097:                     Apache::lonmenu::serverform();
                   5098:         my $forbodytag;
                   5099:         &Apache::lonmenu::prepare_functions($env{'request.noversionuri'},
                   5100:                                             $forcereg,$args->{'group'},
                   5101:                                             $args->{'bread_crumbs'},
                   5102:                                             $advtoolsref,'',\$forbodytag);
                   5103:         unless (ref($args->{'bread_crumbs'}) eq 'ARRAY') {
                   5104:             $funclist = $forbodytag;
                   5105:         }
                   5106:     } else {
1.903     droeschl 5107: 
                   5108:         #    if ($env{'request.state'} eq 'construct') {
                   5109:         #        $titleinfo = &CSTR_pageheader(); #FIXME: Will be removed once all scripts have their own calls
                   5110:         #    }
                   5111: 
1.359     albertel 5112: 
1.1075.2.2  raeburn  5113: 
1.916     droeschl 5114:         if ($env{'request.noversionuri'} =~ m{^/res/adm/pages/}) {
1.1075.2.22  raeburn  5115:             if ($dc_info) {
                   5116:                  $dc_info = qq|<span class="LC_cusr_subheading">$dc_info</span>|;
1.1075.2.1  raeburn  5117:             }
1.1075.2.22  raeburn  5118:             $bodytag .= qq|<div id="LC_nav_bar">$name $role<br />
                   5119:                            <em>$realm</em> $dc_info</div>|;
1.903     droeschl 5120:             return $bodytag;
                   5121:         }
1.894     droeschl 5122: 
1.927     raeburn  5123:         unless ($env{'request.symb'} =~ m/\.page___\d+___/) {
                   5124:             $bodytag .= qq|<div id="LC_nav_bar">$name $role</div>|;
                   5125:         }
1.916     droeschl 5126: 
1.903     droeschl 5127:         $bodytag .= Apache::lonhtmlcommon::scripttag(
                   5128:             Apache::lonmenu::utilityfunctions(), 'start');
1.816     bisitz   5129: 
1.903     droeschl 5130:         $bodytag .= Apache::lonmenu::primary_menu();
1.852     droeschl 5131: 
1.917     raeburn  5132:         if ($dc_info) {
                   5133:             $dc_info = &dc_courseid_toggle($dc_info);
                   5134:         }
                   5135:         $bodytag .= qq|<div id="LC_realm">$realm $dc_info</div>|;
1.916     droeschl 5136: 
1.903     droeschl 5137:         #don't show menus for public users
1.954     raeburn  5138:         if (!$public){
1.903     droeschl 5139:             $bodytag .= Apache::lonmenu::secondary_menu();
                   5140:             $bodytag .= Apache::lonmenu::serverform();
1.920     raeburn  5141:             $bodytag .= Apache::lonhtmlcommon::scripttag('', 'end');
                   5142:             if ($env{'request.state'} eq 'construct') {
1.962     droeschl 5143:                 $bodytag .= &Apache::lonmenu::innerregister($forcereg,
1.920     raeburn  5144:                                 $args->{'bread_crumbs'});
                   5145:             } elsif ($forcereg) { 
1.1075.2.22  raeburn  5146:                 $bodytag .= &Apache::lonmenu::innerregister($forcereg,undef,
                   5147:                                                             $args->{'group'});
1.1075.2.15  raeburn  5148:             } else {
1.1075.2.21  raeburn  5149:                 my $forbodytag;
                   5150:                 &Apache::lonmenu::prepare_functions($env{'request.noversionuri'},
                   5151:                                                     $forcereg,$args->{'group'},
                   5152:                                                     $args->{'bread_crumbs'},
                   5153:                                                     $advtoolsref,'',\$forbodytag);
                   5154:                 unless (ref($args->{'bread_crumbs'}) eq 'ARRAY') {
                   5155:                     $bodytag .= $forbodytag;
                   5156:                 }
1.920     raeburn  5157:             }
1.903     droeschl 5158:         }else{
                   5159:             # this is to seperate menu from content when there's no secondary
                   5160:             # menu. Especially needed for public accessible ressources.
                   5161:             $bodytag .= '<hr style="clear:both" />';
                   5162:             $bodytag .= Apache::lonhtmlcommon::scripttag('', 'end'); 
1.235     raeburn  5163:         }
1.903     droeschl 5164: 
1.235     raeburn  5165:         return $bodytag;
1.1075.2.12  raeburn  5166:     }
                   5167: 
                   5168: #
                   5169: # Top frame rendering, Remote is up
                   5170: #
                   5171: 
                   5172:     my $imgsrc = $img;
                   5173:     if ($img =~ /^\/adm/) {
                   5174:         $imgsrc = &lonhttpdurl($img);
                   5175:     }
                   5176:     my $upperleft='<img src="'.$imgsrc.'" alt="'.$function.'" />';
                   5177: 
                   5178:     # Explicit link to get inline menu
                   5179:     my $menu= ($no_inline_link?''
                   5180:                :'<a href="/adm/remote?action=collapse" target="_top">'.&mt('Switch to Inline Menu Mode').'</a>');
                   5181: 
                   5182:     if ($dc_info) {
                   5183:         $dc_info = qq|<span class="LC_cusr_subheading">($dc_info)</span>|;
                   5184:     }
                   5185: 
                   5186:     unless ($env{'form.inhibitmenu'}) {
                   5187:         $bodytag .= qq|<div id="LC_nav_bar">$name $role</div>
                   5188:                        <ol class="LC_primary_menu LC_right">
                   5189:                        <li>$menu</li>
                   5190:                        </ol><div id="LC_realm"> $realm $dc_info</div>|;
                   5191:     }
1.1075.2.13  raeburn  5192:     if ($env{'request.state'} eq 'construct') {
                   5193:         if (!$public){
                   5194:             if ($env{'request.state'} eq 'construct') {
                   5195:                 $funclist = &Apache::lonhtmlcommon::scripttag(
                   5196:                                 &Apache::lonmenu::utilityfunctions(), 'start').
                   5197:                             &Apache::lonhtmlcommon::scripttag('','end').
                   5198:                             &Apache::lonmenu::innerregister($forcereg,
                   5199:                                                             $args->{'bread_crumbs'});
                   5200:             }
                   5201:         }
                   5202:     }
1.1075.2.21  raeburn  5203:     return $bodytag."\n".$funclist;
1.182     matthew  5204: }
                   5205: 
1.917     raeburn  5206: sub dc_courseid_toggle {
                   5207:     my ($dc_info) = @_;
1.980     raeburn  5208:     return ' <span id="dccidtext" class="LC_cusr_subheading LC_nobreak">'.
1.1069    raeburn  5209:            '<a href="javascript:showCourseID();" class="LC_menubuttons_link">'.
1.917     raeburn  5210:            &mt('(More ...)').'</a></span>'.
                   5211:            '<div id="dccid" class="LC_dccid">'.$dc_info.'</div>';
                   5212: }
                   5213: 
1.330     albertel 5214: sub make_attr_string {
                   5215:     my ($register,$attr_ref) = @_;
                   5216: 
                   5217:     if ($attr_ref && !ref($attr_ref)) {
                   5218: 	die("addentries Must be a hash ref ".
                   5219: 	    join(':',caller(1))." ".
                   5220: 	    join(':',caller(0))." ");
                   5221:     }
                   5222: 
                   5223:     if ($register) {
1.339     albertel 5224: 	my ($on_load,$on_unload);
                   5225: 	foreach my $key (keys(%{$attr_ref})) {
                   5226: 	    if      (lc($key) eq 'onload') {
                   5227: 		$on_load.=$attr_ref->{$key}.';';
                   5228: 		delete($attr_ref->{$key});
                   5229: 
                   5230: 	    } elsif (lc($key) eq 'onunload') {
                   5231: 		$on_unload.=$attr_ref->{$key}.';';
                   5232: 		delete($attr_ref->{$key});
                   5233: 	    }
                   5234: 	}
1.1075.2.12  raeburn  5235:         if ($env{'environment.remote'} eq 'on') {
                   5236:             $attr_ref->{'onload'}  =
                   5237:                 &Apache::lonmenu::loadevents().  $on_load;
                   5238:             $attr_ref->{'onunload'}=
                   5239:                 &Apache::lonmenu::unloadevents().$on_unload;
                   5240:         } else {  
                   5241: 	    $attr_ref->{'onload'}  = $on_load;
                   5242: 	    $attr_ref->{'onunload'}= $on_unload;
                   5243:         }
1.330     albertel 5244:     }
1.339     albertel 5245: 
1.330     albertel 5246:     my $attr_string;
                   5247:     foreach my $attr (keys(%$attr_ref)) {
                   5248: 	$attr_string .= " $attr=\"".$attr_ref->{$attr}.'" ';
                   5249:     }
                   5250:     return $attr_string;
                   5251: }
                   5252: 
                   5253: 
1.182     matthew  5254: ###############################################
1.251     albertel 5255: ###############################################
                   5256: 
                   5257: =pod
                   5258: 
                   5259: =item * &endbodytag()
                   5260: 
                   5261: Returns a uniform footer for LON-CAPA web pages.
                   5262: 
1.635     raeburn  5263: Inputs: 1 - optional reference to an args hash
                   5264: If in the hash, key for noredirectlink has a value which evaluates to true,
                   5265: a 'Continue' link is not displayed if the page contains an
                   5266: internal redirect in the <head></head> section,
                   5267: i.e., $env{'internal.head.redirect'} exists   
1.251     albertel 5268: 
                   5269: =cut
                   5270: 
                   5271: sub endbodytag {
1.635     raeburn  5272:     my ($args) = @_;
1.1075.2.6  raeburn  5273:     my $endbodytag;
                   5274:     unless ((ref($args) eq 'HASH') && ($args->{'notbody'})) {
                   5275:         $endbodytag='</body>';
                   5276:     }
1.269     albertel 5277:     $endbodytag=&Apache::lontexconvert::jsMath_process()."\n".$endbodytag;
1.315     albertel 5278:     if ( exists( $env{'internal.head.redirect'} ) ) {
1.635     raeburn  5279:         if (!(ref($args) eq 'HASH' && $args->{'noredirectlink'})) {
                   5280: 	    $endbodytag=
                   5281: 	        "<br /><a href=\"$env{'internal.head.redirect'}\">".
                   5282: 	        &mt('Continue').'</a>'.
                   5283: 	        $endbodytag;
                   5284:         }
1.315     albertel 5285:     }
1.251     albertel 5286:     return $endbodytag;
                   5287: }
                   5288: 
1.352     albertel 5289: =pod
                   5290: 
                   5291: =item * &standard_css()
                   5292: 
                   5293: Returns a style sheet
                   5294: 
                   5295: Inputs: (all optional)
                   5296:             domain         -> force to color decorate a page for a specific
                   5297:                                domain
                   5298:             function       -> force usage of a specific rolish color scheme
                   5299:             bgcolor        -> override the default page bgcolor
                   5300: 
                   5301: =cut
                   5302: 
1.343     albertel 5303: sub standard_css {
1.345     albertel 5304:     my ($function,$domain,$bgcolor) = @_;
1.352     albertel 5305:     $function  = &get_users_function() if (!$function);
                   5306:     my $img    = &designparm($function.'.img',   $domain);
                   5307:     my $tabbg  = &designparm($function.'.tabbg', $domain);
                   5308:     my $font   = &designparm($function.'.font',  $domain);
1.801     tempelho 5309:     my $fontmenu = &designparm($function.'.fontmenu', $domain);
1.791     tempelho 5310: #second colour for later usage
1.345     albertel 5311:     my $sidebg = &designparm($function.'.sidebg',$domain);
1.382     albertel 5312:     my $pgbg_or_bgcolor =
                   5313: 	         $bgcolor ||
1.352     albertel 5314: 	         &designparm($function.'.pgbg',  $domain);
1.382     albertel 5315:     my $pgbg   = &designparm($function.'.pgbg',  $domain);
1.352     albertel 5316:     my $alink  = &designparm($function.'.alink', $domain);
                   5317:     my $vlink  = &designparm($function.'.vlink', $domain);
                   5318:     my $link   = &designparm($function.'.link',  $domain);
                   5319: 
1.602     albertel 5320:     my $sans                 = 'Verdana,Arial,Helvetica,sans-serif';
1.395     albertel 5321:     my $mono                 = 'monospace';
1.850     bisitz   5322:     my $data_table_head      = $sidebg;
                   5323:     my $data_table_light     = '#FAFAFA';
1.1060    bisitz   5324:     my $data_table_dark      = '#E0E0E0';
1.470     banghart 5325:     my $data_table_darker    = '#CCCCCC';
1.349     albertel 5326:     my $data_table_highlight = '#FFFF00';
1.352     albertel 5327:     my $mail_new             = '#FFBB77';
                   5328:     my $mail_new_hover       = '#DD9955';
                   5329:     my $mail_read            = '#BBBB77';
                   5330:     my $mail_read_hover      = '#999944';
                   5331:     my $mail_replied         = '#AAAA88';
                   5332:     my $mail_replied_hover   = '#888855';
                   5333:     my $mail_other           = '#99BBBB';
                   5334:     my $mail_other_hover     = '#669999';
1.391     albertel 5335:     my $table_header         = '#DDDDDD';
1.489     raeburn  5336:     my $feedback_link_bg     = '#BBBBBB';
1.911     bisitz   5337:     my $lg_border_color      = '#C8C8C8';
1.952     onken    5338:     my $button_hover         = '#BF2317';
1.392     albertel 5339: 
1.608     albertel 5340:     my $border = ($env{'browser.type'} eq 'explorer' ||
1.911     bisitz   5341:       $env{'browser.type'} eq 'safari'     ) ? '0 2px 0 2px'
                   5342:                                              : '0 3px 0 4px';
1.448     albertel 5343: 
1.523     albertel 5344: 
1.343     albertel 5345:     return <<END;
1.947     droeschl 5346: 
                   5347: /* needed for iframe to allow 100% height in FF */
                   5348: body, html { 
                   5349:     margin: 0;
                   5350:     padding: 0 0.5%;
                   5351:     height: 99%; /* to avoid scrollbars */
                   5352: }
                   5353: 
1.795     www      5354: body {
1.911     bisitz   5355:   font-family: $sans;
                   5356:   line-height:130%;
                   5357:   font-size:0.83em;
                   5358:   color:$font;
1.795     www      5359: }
                   5360: 
1.959     onken    5361: a:focus,
                   5362: a:focus img {
1.795     www      5363:   color: red;
                   5364: }
1.698     harmsja  5365: 
1.911     bisitz   5366: form, .inline {
                   5367:   display: inline;
1.795     www      5368: }
1.721     harmsja  5369: 
1.795     www      5370: .LC_right {
1.911     bisitz   5371:   text-align:right;
1.795     www      5372: }
                   5373: 
                   5374: .LC_middle {
1.911     bisitz   5375:   vertical-align:middle;
1.795     www      5376: }
1.721     harmsja  5377: 
1.911     bisitz   5378: .LC_400Box {
                   5379:   width:400px;
                   5380: }
1.721     harmsja  5381: 
1.947     droeschl 5382: .LC_iframecontainer {
                   5383:     width: 98%;
                   5384:     margin: 0;
                   5385:     position: fixed;
                   5386:     top: 8.5em;
                   5387:     bottom: 0;
                   5388: }
                   5389: 
                   5390: .LC_iframecontainer iframe{
                   5391:     border: none;
                   5392:     width: 100%;
                   5393:     height: 100%;
                   5394: }
                   5395: 
1.778     bisitz   5396: .LC_filename {
                   5397:   font-family: $mono;
                   5398:   white-space:pre;
1.921     bisitz   5399:   font-size: 120%;
1.778     bisitz   5400: }
                   5401: 
                   5402: .LC_fileicon {
                   5403:   border: none;
                   5404:   height: 1.3em;
                   5405:   vertical-align: text-bottom;
                   5406:   margin-right: 0.3em;
                   5407:   text-decoration:none;
                   5408: }
                   5409: 
1.1008    www      5410: .LC_setting {
                   5411:   text-decoration:underline;
                   5412: }
                   5413: 
1.350     albertel 5414: .LC_error {
                   5415:   color: red;
                   5416: }
1.795     www      5417: 
1.1075.2.15  raeburn  5418: .LC_warning {
                   5419:   color: darkorange;
                   5420: }
                   5421: 
1.457     albertel 5422: .LC_diff_removed {
1.733     bisitz   5423:   color: red;
1.394     albertel 5424: }
1.532     albertel 5425: 
                   5426: .LC_info,
1.457     albertel 5427: .LC_success,
                   5428: .LC_diff_added {
1.350     albertel 5429:   color: green;
                   5430: }
1.795     www      5431: 
1.802     bisitz   5432: div.LC_confirm_box {
                   5433:   background-color: #FAFAFA;
                   5434:   border: 1px solid $lg_border_color;
                   5435:   margin-right: 0;
                   5436:   padding: 5px;
                   5437: }
                   5438: 
                   5439: div.LC_confirm_box .LC_error img,
                   5440: div.LC_confirm_box .LC_success img {
                   5441:   vertical-align: middle;
                   5442: }
                   5443: 
1.440     albertel 5444: .LC_icon {
1.771     droeschl 5445:   border: none;
1.790     droeschl 5446:   vertical-align: middle;
1.771     droeschl 5447: }
                   5448: 
1.543     albertel 5449: .LC_docs_spacer {
                   5450:   width: 25px;
                   5451:   height: 1px;
1.771     droeschl 5452:   border: none;
1.543     albertel 5453: }
1.346     albertel 5454: 
1.532     albertel 5455: .LC_internal_info {
1.735     bisitz   5456:   color: #999999;
1.532     albertel 5457: }
                   5458: 
1.794     www      5459: .LC_discussion {
1.1050    www      5460:   background: $data_table_dark;
1.911     bisitz   5461:   border: 1px solid black;
                   5462:   margin: 2px;
1.794     www      5463: }
                   5464: 
                   5465: .LC_disc_action_left {
1.1050    www      5466:   background: $sidebg;
1.911     bisitz   5467:   text-align: left;
1.1050    www      5468:   padding: 4px;
                   5469:   margin: 2px;
1.794     www      5470: }
                   5471: 
                   5472: .LC_disc_action_right {
1.1050    www      5473:   background: $sidebg;
1.911     bisitz   5474:   text-align: right;
1.1050    www      5475:   padding: 4px;
                   5476:   margin: 2px;
1.794     www      5477: }
                   5478: 
                   5479: .LC_disc_new_item {
1.911     bisitz   5480:   background: white;
                   5481:   border: 2px solid red;
1.1050    www      5482:   margin: 4px;
                   5483:   padding: 4px;
1.794     www      5484: }
                   5485: 
                   5486: .LC_disc_old_item {
1.911     bisitz   5487:   background: white;
1.1050    www      5488:   margin: 4px;
                   5489:   padding: 4px;
1.794     www      5490: }
                   5491: 
1.458     albertel 5492: table.LC_pastsubmission {
                   5493:   border: 1px solid black;
                   5494:   margin: 2px;
                   5495: }
                   5496: 
1.924     bisitz   5497: table#LC_menubuttons {
1.345     albertel 5498:   width: 100%;
                   5499:   background: $pgbg;
1.392     albertel 5500:   border: 2px;
1.402     albertel 5501:   border-collapse: separate;
1.803     bisitz   5502:   padding: 0;
1.345     albertel 5503: }
1.392     albertel 5504: 
1.801     tempelho 5505: table#LC_title_bar a {
                   5506:   color: $fontmenu;
                   5507: }
1.836     bisitz   5508: 
1.807     droeschl 5509: table#LC_title_bar {
1.819     tempelho 5510:   clear: both;
1.836     bisitz   5511:   display: none;
1.807     droeschl 5512: }
                   5513: 
1.795     www      5514: table#LC_title_bar,
1.933     droeschl 5515: table.LC_breadcrumbs, /* obsolete? */
1.393     albertel 5516: table#LC_title_bar.LC_with_remote {
1.359     albertel 5517:   width: 100%;
1.392     albertel 5518:   border-color: $pgbg;
                   5519:   border-style: solid;
                   5520:   border-width: $border;
1.379     albertel 5521:   background: $pgbg;
1.801     tempelho 5522:   color: $fontmenu;
1.392     albertel 5523:   border-collapse: collapse;
1.803     bisitz   5524:   padding: 0;
1.819     tempelho 5525:   margin: 0;
1.359     albertel 5526: }
1.795     www      5527: 
1.933     droeschl 5528: ul.LC_breadcrumb_tools_outerlist {
1.913     droeschl 5529:     margin: 0;
                   5530:     padding: 0;
1.933     droeschl 5531:     position: relative;
                   5532:     list-style: none;
1.913     droeschl 5533: }
1.933     droeschl 5534: ul.LC_breadcrumb_tools_outerlist li {
1.913     droeschl 5535:     display: inline;
                   5536: }
1.933     droeschl 5537: 
                   5538: .LC_breadcrumb_tools_navigation {
1.913     droeschl 5539:     padding: 0;
1.933     droeschl 5540:     margin: 0;
                   5541:     float: left;
1.913     droeschl 5542: }
1.933     droeschl 5543: .LC_breadcrumb_tools_tools {
                   5544:     padding: 0;
                   5545:     margin: 0;
1.913     droeschl 5546:     float: right;
                   5547: }
                   5548: 
1.359     albertel 5549: table#LC_title_bar td {
                   5550:   background: $tabbg;
                   5551: }
1.795     www      5552: 
1.911     bisitz   5553: table#LC_menubuttons img {
1.803     bisitz   5554:   border: none;
1.346     albertel 5555: }
1.795     www      5556: 
1.842     droeschl 5557: .LC_breadcrumbs_component {
1.911     bisitz   5558:   float: right;
                   5559:   margin: 0 1em;
1.357     albertel 5560: }
1.842     droeschl 5561: .LC_breadcrumbs_component img {
1.911     bisitz   5562:   vertical-align: middle;
1.777     tempelho 5563: }
1.795     www      5564: 
1.383     albertel 5565: td.LC_table_cell_checkbox {
                   5566:   text-align: center;
                   5567: }
1.795     www      5568: 
                   5569: .LC_fontsize_small {
1.911     bisitz   5570:   font-size: 70%;
1.705     tempelho 5571: }
                   5572: 
1.844     bisitz   5573: #LC_breadcrumbs {
1.911     bisitz   5574:   clear:both;
                   5575:   background: $sidebg;
                   5576:   border-bottom: 1px solid $lg_border_color;
                   5577:   line-height: 2.5em;
1.933     droeschl 5578:   overflow: hidden;
1.911     bisitz   5579:   margin: 0;
                   5580:   padding: 0;
1.995     raeburn  5581:   text-align: left;
1.819     tempelho 5582: }
1.862     bisitz   5583: 
1.1075.2.16  raeburn  5584: .LC_head_subbox, .LC_actionbox {
1.911     bisitz   5585:   clear:both;
                   5586:   background: #F8F8F8; /* $sidebg; */
1.915     droeschl 5587:   border: 1px solid $sidebg;
1.1075.2.16  raeburn  5588:   margin: 0 0 10px 0;
1.966     bisitz   5589:   padding: 3px;
1.995     raeburn  5590:   text-align: left;
1.822     bisitz   5591: }
                   5592: 
1.795     www      5593: .LC_fontsize_medium {
1.911     bisitz   5594:   font-size: 85%;
1.705     tempelho 5595: }
                   5596: 
1.795     www      5597: .LC_fontsize_large {
1.911     bisitz   5598:   font-size: 120%;
1.705     tempelho 5599: }
                   5600: 
1.346     albertel 5601: .LC_menubuttons_inline_text {
                   5602:   color: $font;
1.698     harmsja  5603:   font-size: 90%;
1.701     harmsja  5604:   padding-left:3px;
1.346     albertel 5605: }
                   5606: 
1.934     droeschl 5607: .LC_menubuttons_inline_text img{
                   5608:   vertical-align: middle;
                   5609: }
                   5610: 
1.1051    www      5611: li.LC_menubuttons_inline_text img {
1.951     onken    5612:   cursor:pointer;
1.1002    droeschl 5613:   text-decoration: none;
1.951     onken    5614: }
                   5615: 
1.526     www      5616: .LC_menubuttons_link {
                   5617:   text-decoration: none;
                   5618: }
1.795     www      5619: 
1.522     albertel 5620: .LC_menubuttons_category {
1.521     www      5621:   color: $font;
1.526     www      5622:   background: $pgbg;
1.521     www      5623:   font-size: larger;
                   5624:   font-weight: bold;
                   5625: }
                   5626: 
1.346     albertel 5627: td.LC_menubuttons_text {
1.911     bisitz   5628:   color: $font;
1.346     albertel 5629: }
1.706     harmsja  5630: 
1.346     albertel 5631: .LC_current_location {
                   5632:   background: $tabbg;
                   5633: }
1.795     www      5634: 
1.938     bisitz   5635: table.LC_data_table {
1.347     albertel 5636:   border: 1px solid #000000;
1.402     albertel 5637:   border-collapse: separate;
1.426     albertel 5638:   border-spacing: 1px;
1.610     albertel 5639:   background: $pgbg;
1.347     albertel 5640: }
1.795     www      5641: 
1.422     albertel 5642: .LC_data_table_dense {
                   5643:   font-size: small;
                   5644: }
1.795     www      5645: 
1.507     raeburn  5646: table.LC_nested_outer {
                   5647:   border: 1px solid #000000;
1.589     raeburn  5648:   border-collapse: collapse;
1.803     bisitz   5649:   border-spacing: 0;
1.507     raeburn  5650:   width: 100%;
                   5651: }
1.795     www      5652: 
1.879     raeburn  5653: table.LC_innerpickbox,
1.507     raeburn  5654: table.LC_nested {
1.803     bisitz   5655:   border: none;
1.589     raeburn  5656:   border-collapse: collapse;
1.803     bisitz   5657:   border-spacing: 0;
1.507     raeburn  5658:   width: 100%;
                   5659: }
1.795     www      5660: 
1.911     bisitz   5661: table.LC_data_table tr th,
                   5662: table.LC_calendar tr th,
1.879     raeburn  5663: table.LC_prior_tries tr th,
                   5664: table.LC_innerpickbox tr th {
1.349     albertel 5665:   font-weight: bold;
                   5666:   background-color: $data_table_head;
1.801     tempelho 5667:   color:$fontmenu;
1.701     harmsja  5668:   font-size:90%;
1.347     albertel 5669: }
1.795     www      5670: 
1.879     raeburn  5671: table.LC_innerpickbox tr th,
                   5672: table.LC_innerpickbox tr td {
                   5673:   vertical-align: top;
                   5674: }
                   5675: 
1.711     raeburn  5676: table.LC_data_table tr.LC_info_row > td {
1.735     bisitz   5677:   background-color: #CCCCCC;
1.711     raeburn  5678:   font-weight: bold;
                   5679:   text-align: left;
                   5680: }
1.795     www      5681: 
1.912     bisitz   5682: table.LC_data_table tr.LC_odd_row > td {
                   5683:   background-color: $data_table_light;
                   5684:   padding: 2px;
                   5685:   vertical-align: top;
                   5686: }
                   5687: 
1.809     bisitz   5688: table.LC_pick_box tr > td.LC_odd_row {
1.349     albertel 5689:   background-color: $data_table_light;
1.912     bisitz   5690:   vertical-align: top;
                   5691: }
                   5692: 
                   5693: table.LC_data_table tr.LC_even_row > td {
                   5694:   background-color: $data_table_dark;
1.425     albertel 5695:   padding: 2px;
1.900     bisitz   5696:   vertical-align: top;
1.347     albertel 5697: }
1.795     www      5698: 
1.809     bisitz   5699: table.LC_pick_box tr > td.LC_even_row {
1.349     albertel 5700:   background-color: $data_table_dark;
1.900     bisitz   5701:   vertical-align: top;
1.347     albertel 5702: }
1.795     www      5703: 
1.425     albertel 5704: table.LC_data_table tr.LC_data_table_highlight td {
                   5705:   background-color: $data_table_darker;
                   5706: }
1.795     www      5707: 
1.639     raeburn  5708: table.LC_data_table tr td.LC_leftcol_header {
                   5709:   background-color: $data_table_head;
                   5710:   font-weight: bold;
                   5711: }
1.795     www      5712: 
1.451     albertel 5713: table.LC_data_table tr.LC_empty_row td,
1.507     raeburn  5714: table.LC_nested tr.LC_empty_row td {
1.421     albertel 5715:   font-weight: bold;
                   5716:   font-style: italic;
                   5717:   text-align: center;
                   5718:   padding: 8px;
1.347     albertel 5719: }
1.795     www      5720: 
1.1075.2.30  raeburn  5721: table.LC_data_table tr.LC_empty_row td,
                   5722: table.LC_data_table tr.LC_footer_row td {
1.940     bisitz   5723:   background-color: $sidebg;
                   5724: }
                   5725: 
                   5726: table.LC_nested tr.LC_empty_row td {
                   5727:   background-color: #FFFFFF;
                   5728: }
                   5729: 
1.890     droeschl 5730: table.LC_caption {
                   5731: }
                   5732: 
1.507     raeburn  5733: table.LC_nested tr.LC_empty_row td {
1.465     albertel 5734:   padding: 4ex
                   5735: }
1.795     www      5736: 
1.507     raeburn  5737: table.LC_nested_outer tr th {
                   5738:   font-weight: bold;
1.801     tempelho 5739:   color:$fontmenu;
1.507     raeburn  5740:   background-color: $data_table_head;
1.701     harmsja  5741:   font-size: small;
1.507     raeburn  5742:   border-bottom: 1px solid #000000;
                   5743: }
1.795     www      5744: 
1.507     raeburn  5745: table.LC_nested_outer tr td.LC_subheader {
                   5746:   background-color: $data_table_head;
                   5747:   font-weight: bold;
                   5748:   font-size: small;
                   5749:   border-bottom: 1px solid #000000;
                   5750:   text-align: right;
1.451     albertel 5751: }
1.795     www      5752: 
1.507     raeburn  5753: table.LC_nested tr.LC_info_row td {
1.735     bisitz   5754:   background-color: #CCCCCC;
1.451     albertel 5755:   font-weight: bold;
                   5756:   font-size: small;
1.507     raeburn  5757:   text-align: center;
                   5758: }
1.795     www      5759: 
1.589     raeburn  5760: table.LC_nested tr.LC_info_row td.LC_left_item,
                   5761: table.LC_nested_outer tr th.LC_left_item {
1.507     raeburn  5762:   text-align: left;
1.451     albertel 5763: }
1.795     www      5764: 
1.507     raeburn  5765: table.LC_nested td {
1.735     bisitz   5766:   background-color: #FFFFFF;
1.451     albertel 5767:   font-size: small;
1.507     raeburn  5768: }
1.795     www      5769: 
1.507     raeburn  5770: table.LC_nested_outer tr th.LC_right_item,
                   5771: table.LC_nested tr.LC_info_row td.LC_right_item,
                   5772: table.LC_nested tr.LC_odd_row td.LC_right_item,
                   5773: table.LC_nested tr td.LC_right_item {
1.451     albertel 5774:   text-align: right;
                   5775: }
                   5776: 
1.507     raeburn  5777: table.LC_nested tr.LC_odd_row td {
1.735     bisitz   5778:   background-color: #EEEEEE;
1.451     albertel 5779: }
                   5780: 
1.473     raeburn  5781: table.LC_createuser {
                   5782: }
                   5783: 
                   5784: table.LC_createuser tr.LC_section_row td {
1.701     harmsja  5785:   font-size: small;
1.473     raeburn  5786: }
                   5787: 
                   5788: table.LC_createuser tr.LC_info_row td  {
1.735     bisitz   5789:   background-color: #CCCCCC;
1.473     raeburn  5790:   font-weight: bold;
                   5791:   text-align: center;
                   5792: }
                   5793: 
1.349     albertel 5794: table.LC_calendar {
                   5795:   border: 1px solid #000000;
                   5796:   border-collapse: collapse;
1.917     raeburn  5797:   width: 98%;
1.349     albertel 5798: }
1.795     www      5799: 
1.349     albertel 5800: table.LC_calendar_pickdate {
                   5801:   font-size: xx-small;
                   5802: }
1.795     www      5803: 
1.349     albertel 5804: table.LC_calendar tr td {
                   5805:   border: 1px solid #000000;
                   5806:   vertical-align: top;
1.917     raeburn  5807:   width: 14%;
1.349     albertel 5808: }
1.795     www      5809: 
1.349     albertel 5810: table.LC_calendar tr td.LC_calendar_day_empty {
                   5811:   background-color: $data_table_dark;
                   5812: }
1.795     www      5813: 
1.779     bisitz   5814: table.LC_calendar tr td.LC_calendar_day_current {
                   5815:   background-color: $data_table_highlight;
1.777     tempelho 5816: }
1.795     www      5817: 
1.938     bisitz   5818: table.LC_data_table tr td.LC_mail_new {
1.349     albertel 5819:   background-color: $mail_new;
                   5820: }
1.795     www      5821: 
1.938     bisitz   5822: table.LC_data_table tr.LC_mail_new:hover {
1.349     albertel 5823:   background-color: $mail_new_hover;
                   5824: }
1.795     www      5825: 
1.938     bisitz   5826: table.LC_data_table tr td.LC_mail_read {
1.349     albertel 5827:   background-color: $mail_read;
                   5828: }
1.795     www      5829: 
1.938     bisitz   5830: /*
                   5831: table.LC_data_table tr.LC_mail_read:hover {
1.349     albertel 5832:   background-color: $mail_read_hover;
                   5833: }
1.938     bisitz   5834: */
1.795     www      5835: 
1.938     bisitz   5836: table.LC_data_table tr td.LC_mail_replied {
1.349     albertel 5837:   background-color: $mail_replied;
                   5838: }
1.795     www      5839: 
1.938     bisitz   5840: /*
                   5841: table.LC_data_table tr.LC_mail_replied:hover {
1.349     albertel 5842:   background-color: $mail_replied_hover;
                   5843: }
1.938     bisitz   5844: */
1.795     www      5845: 
1.938     bisitz   5846: table.LC_data_table tr td.LC_mail_other {
1.349     albertel 5847:   background-color: $mail_other;
                   5848: }
1.795     www      5849: 
1.938     bisitz   5850: /*
                   5851: table.LC_data_table tr.LC_mail_other:hover {
1.349     albertel 5852:   background-color: $mail_other_hover;
                   5853: }
1.938     bisitz   5854: */
1.494     raeburn  5855: 
1.777     tempelho 5856: table.LC_data_table tr > td.LC_browser_file,
                   5857: table.LC_data_table tr > td.LC_browser_file_published {
1.899     bisitz   5858:   background: #AAEE77;
1.389     albertel 5859: }
1.795     www      5860: 
1.777     tempelho 5861: table.LC_data_table tr > td.LC_browser_file_locked,
                   5862: table.LC_data_table tr > td.LC_browser_file_unpublished {
1.389     albertel 5863:   background: #FFAA99;
1.387     albertel 5864: }
1.795     www      5865: 
1.777     tempelho 5866: table.LC_data_table tr > td.LC_browser_file_obsolete {
1.899     bisitz   5867:   background: #888888;
1.779     bisitz   5868: }
1.795     www      5869: 
1.777     tempelho 5870: table.LC_data_table tr > td.LC_browser_file_modified,
1.779     bisitz   5871: table.LC_data_table tr > td.LC_browser_file_metamodified {
1.899     bisitz   5872:   background: #F8F866;
1.777     tempelho 5873: }
1.795     www      5874: 
1.696     bisitz   5875: table.LC_data_table tr.LC_browser_folder > td {
1.899     bisitz   5876:   background: #E0E8FF;
1.387     albertel 5877: }
1.696     bisitz   5878: 
1.707     bisitz   5879: table.LC_data_table tr > td.LC_roles_is {
1.911     bisitz   5880:   /* background: #77FF77; */
1.707     bisitz   5881: }
1.795     www      5882: 
1.707     bisitz   5883: table.LC_data_table tr > td.LC_roles_future {
1.939     bisitz   5884:   border-right: 8px solid #FFFF77;
1.707     bisitz   5885: }
1.795     www      5886: 
1.707     bisitz   5887: table.LC_data_table tr > td.LC_roles_will {
1.939     bisitz   5888:   border-right: 8px solid #FFAA77;
1.707     bisitz   5889: }
1.795     www      5890: 
1.707     bisitz   5891: table.LC_data_table tr > td.LC_roles_expired {
1.939     bisitz   5892:   border-right: 8px solid #FF7777;
1.707     bisitz   5893: }
1.795     www      5894: 
1.707     bisitz   5895: table.LC_data_table tr > td.LC_roles_will_not {
1.939     bisitz   5896:   border-right: 8px solid #AAFF77;
1.707     bisitz   5897: }
1.795     www      5898: 
1.707     bisitz   5899: table.LC_data_table tr > td.LC_roles_selected {
1.939     bisitz   5900:   border-right: 8px solid #11CC55;
1.707     bisitz   5901: }
                   5902: 
1.388     albertel 5903: span.LC_current_location {
1.701     harmsja  5904:   font-size:larger;
1.388     albertel 5905:   background: $pgbg;
                   5906: }
1.387     albertel 5907: 
1.1029    www      5908: span.LC_current_nav_location {
                   5909:   font-weight:bold;
                   5910:   background: $sidebg;
                   5911: }
                   5912: 
1.395     albertel 5913: span.LC_parm_menu_item {
                   5914:   font-size: larger;
                   5915: }
1.795     www      5916: 
1.395     albertel 5917: span.LC_parm_scope_all {
                   5918:   color: red;
                   5919: }
1.795     www      5920: 
1.395     albertel 5921: span.LC_parm_scope_folder {
                   5922:   color: green;
                   5923: }
1.795     www      5924: 
1.395     albertel 5925: span.LC_parm_scope_resource {
                   5926:   color: orange;
                   5927: }
1.795     www      5928: 
1.395     albertel 5929: span.LC_parm_part {
                   5930:   color: blue;
                   5931: }
1.795     www      5932: 
1.911     bisitz   5933: span.LC_parm_folder,
                   5934: span.LC_parm_symb {
1.395     albertel 5935:   font-size: x-small;
                   5936:   font-family: $mono;
                   5937:   color: #AAAAAA;
                   5938: }
                   5939: 
1.977     bisitz   5940: ul.LC_parm_parmlist li {
                   5941:   display: inline-block;
                   5942:   padding: 0.3em 0.8em;
                   5943:   vertical-align: top;
                   5944:   width: 150px;
                   5945:   border-top:1px solid $lg_border_color;
                   5946: }
                   5947: 
1.795     www      5948: td.LC_parm_overview_level_menu,
                   5949: td.LC_parm_overview_map_menu,
                   5950: td.LC_parm_overview_parm_selectors,
                   5951: td.LC_parm_overview_restrictions  {
1.396     albertel 5952:   border: 1px solid black;
                   5953:   border-collapse: collapse;
                   5954: }
1.795     www      5955: 
1.396     albertel 5956: table.LC_parm_overview_restrictions td {
                   5957:   border-width: 1px 4px 1px 4px;
                   5958:   border-style: solid;
                   5959:   border-color: $pgbg;
                   5960:   text-align: center;
                   5961: }
1.795     www      5962: 
1.396     albertel 5963: table.LC_parm_overview_restrictions th {
                   5964:   background: $tabbg;
                   5965:   border-width: 1px 4px 1px 4px;
                   5966:   border-style: solid;
                   5967:   border-color: $pgbg;
                   5968: }
1.795     www      5969: 
1.398     albertel 5970: table#LC_helpmenu {
1.803     bisitz   5971:   border: none;
1.398     albertel 5972:   height: 55px;
1.803     bisitz   5973:   border-spacing: 0;
1.398     albertel 5974: }
                   5975: 
                   5976: table#LC_helpmenu fieldset legend {
                   5977:   font-size: larger;
                   5978: }
1.795     www      5979: 
1.397     albertel 5980: table#LC_helpmenu_links {
                   5981:   width: 100%;
                   5982:   border: 1px solid black;
                   5983:   background: $pgbg;
1.803     bisitz   5984:   padding: 0;
1.397     albertel 5985:   border-spacing: 1px;
                   5986: }
1.795     www      5987: 
1.397     albertel 5988: table#LC_helpmenu_links tr td {
                   5989:   padding: 1px;
                   5990:   background: $tabbg;
1.399     albertel 5991:   text-align: center;
                   5992:   font-weight: bold;
1.397     albertel 5993: }
1.396     albertel 5994: 
1.795     www      5995: table#LC_helpmenu_links a:link,
                   5996: table#LC_helpmenu_links a:visited,
1.397     albertel 5997: table#LC_helpmenu_links a:active {
                   5998:   text-decoration: none;
                   5999:   color: $font;
                   6000: }
1.795     www      6001: 
1.397     albertel 6002: table#LC_helpmenu_links a:hover {
                   6003:   text-decoration: underline;
                   6004:   color: $vlink;
                   6005: }
1.396     albertel 6006: 
1.417     albertel 6007: .LC_chrt_popup_exists {
                   6008:   border: 1px solid #339933;
                   6009:   margin: -1px;
                   6010: }
1.795     www      6011: 
1.417     albertel 6012: .LC_chrt_popup_up {
                   6013:   border: 1px solid yellow;
                   6014:   margin: -1px;
                   6015: }
1.795     www      6016: 
1.417     albertel 6017: .LC_chrt_popup {
                   6018:   border: 1px solid #8888FF;
                   6019:   background: #CCCCFF;
                   6020: }
1.795     www      6021: 
1.421     albertel 6022: table.LC_pick_box {
                   6023:   border-collapse: separate;
                   6024:   background: white;
                   6025:   border: 1px solid black;
                   6026:   border-spacing: 1px;
                   6027: }
1.795     www      6028: 
1.421     albertel 6029: table.LC_pick_box td.LC_pick_box_title {
1.850     bisitz   6030:   background: $sidebg;
1.421     albertel 6031:   font-weight: bold;
1.900     bisitz   6032:   text-align: left;
1.740     bisitz   6033:   vertical-align: top;
1.421     albertel 6034:   width: 184px;
                   6035:   padding: 8px;
                   6036: }
1.795     www      6037: 
1.579     raeburn  6038: table.LC_pick_box td.LC_pick_box_value {
                   6039:   text-align: left;
                   6040:   padding: 8px;
                   6041: }
1.795     www      6042: 
1.579     raeburn  6043: table.LC_pick_box td.LC_pick_box_select {
                   6044:   text-align: left;
                   6045:   padding: 8px;
                   6046: }
1.795     www      6047: 
1.424     albertel 6048: table.LC_pick_box td.LC_pick_box_separator {
1.803     bisitz   6049:   padding: 0;
1.421     albertel 6050:   height: 1px;
                   6051:   background: black;
                   6052: }
1.795     www      6053: 
1.421     albertel 6054: table.LC_pick_box td.LC_pick_box_submit {
                   6055:   text-align: right;
                   6056: }
1.795     www      6057: 
1.579     raeburn  6058: table.LC_pick_box td.LC_evenrow_value {
                   6059:   text-align: left;
                   6060:   padding: 8px;
                   6061:   background-color: $data_table_light;
                   6062: }
1.795     www      6063: 
1.579     raeburn  6064: table.LC_pick_box td.LC_oddrow_value {
                   6065:   text-align: left;
                   6066:   padding: 8px;
                   6067:   background-color: $data_table_light;
                   6068: }
1.795     www      6069: 
1.579     raeburn  6070: span.LC_helpform_receipt_cat {
                   6071:   font-weight: bold;
                   6072: }
1.795     www      6073: 
1.424     albertel 6074: table.LC_group_priv_box {
                   6075:   background: white;
                   6076:   border: 1px solid black;
                   6077:   border-spacing: 1px;
                   6078: }
1.795     www      6079: 
1.424     albertel 6080: table.LC_group_priv_box td.LC_pick_box_title {
                   6081:   background: $tabbg;
                   6082:   font-weight: bold;
                   6083:   text-align: right;
                   6084:   width: 184px;
                   6085: }
1.795     www      6086: 
1.424     albertel 6087: table.LC_group_priv_box td.LC_groups_fixed {
                   6088:   background: $data_table_light;
                   6089:   text-align: center;
                   6090: }
1.795     www      6091: 
1.424     albertel 6092: table.LC_group_priv_box td.LC_groups_optional {
                   6093:   background: $data_table_dark;
                   6094:   text-align: center;
                   6095: }
1.795     www      6096: 
1.424     albertel 6097: table.LC_group_priv_box td.LC_groups_functionality {
                   6098:   background: $data_table_darker;
                   6099:   text-align: center;
                   6100:   font-weight: bold;
                   6101: }
1.795     www      6102: 
1.424     albertel 6103: table.LC_group_priv td {
                   6104:   text-align: left;
1.803     bisitz   6105:   padding: 0;
1.424     albertel 6106: }
                   6107: 
                   6108: .LC_navbuttons {
                   6109:   margin: 2ex 0ex 2ex 0ex;
                   6110: }
1.795     www      6111: 
1.423     albertel 6112: .LC_topic_bar {
                   6113:   font-weight: bold;
                   6114:   background: $tabbg;
1.918     wenzelju 6115:   margin: 1em 0em 1em 2em;
1.805     bisitz   6116:   padding: 3px;
1.918     wenzelju 6117:   font-size: 1.2em;
1.423     albertel 6118: }
1.795     www      6119: 
1.423     albertel 6120: .LC_topic_bar span {
1.918     wenzelju 6121:   left: 0.5em;
                   6122:   position: absolute;
1.423     albertel 6123:   vertical-align: middle;
1.918     wenzelju 6124:   font-size: 1.2em;
1.423     albertel 6125: }
1.795     www      6126: 
1.423     albertel 6127: table.LC_course_group_status {
                   6128:   margin: 20px;
                   6129: }
1.795     www      6130: 
1.423     albertel 6131: table.LC_status_selector td {
                   6132:   vertical-align: top;
                   6133:   text-align: center;
1.424     albertel 6134:   padding: 4px;
                   6135: }
1.795     www      6136: 
1.599     albertel 6137: div.LC_feedback_link {
1.616     albertel 6138:   clear: both;
1.829     kalberla 6139:   background: $sidebg;
1.779     bisitz   6140:   width: 100%;
1.829     kalberla 6141:   padding-bottom: 10px;
                   6142:   border: 1px $tabbg solid;
1.833     kalberla 6143:   height: 22px;
                   6144:   line-height: 22px;
                   6145:   padding-top: 5px;
                   6146: }
                   6147: 
                   6148: div.LC_feedback_link img {
                   6149:   height: 22px;
1.867     kalberla 6150:   vertical-align:middle;
1.829     kalberla 6151: }
                   6152: 
1.911     bisitz   6153: div.LC_feedback_link a {
1.829     kalberla 6154:   text-decoration: none;
1.489     raeburn  6155: }
1.795     www      6156: 
1.867     kalberla 6157: div.LC_comblock {
1.911     bisitz   6158:   display:inline;
1.867     kalberla 6159:   color:$font;
                   6160:   font-size:90%;
                   6161: }
                   6162: 
                   6163: div.LC_feedback_link div.LC_comblock {
                   6164:   padding-left:5px;
                   6165: }
                   6166: 
                   6167: div.LC_feedback_link div.LC_comblock a {
                   6168:   color:$font;
                   6169: }
                   6170: 
1.489     raeburn  6171: span.LC_feedback_link {
1.858     bisitz   6172:   /* background: $feedback_link_bg; */
1.599     albertel 6173:   font-size: larger;
                   6174: }
1.795     www      6175: 
1.599     albertel 6176: span.LC_message_link {
1.858     bisitz   6177:   /* background: $feedback_link_bg; */
1.599     albertel 6178:   font-size: larger;
                   6179:   position: absolute;
                   6180:   right: 1em;
1.489     raeburn  6181: }
1.421     albertel 6182: 
1.515     albertel 6183: table.LC_prior_tries {
1.524     albertel 6184:   border: 1px solid #000000;
                   6185:   border-collapse: separate;
                   6186:   border-spacing: 1px;
1.515     albertel 6187: }
1.523     albertel 6188: 
1.515     albertel 6189: table.LC_prior_tries td {
1.524     albertel 6190:   padding: 2px;
1.515     albertel 6191: }
1.523     albertel 6192: 
                   6193: .LC_answer_correct {
1.795     www      6194:   background: lightgreen;
                   6195:   color: darkgreen;
                   6196:   padding: 6px;
1.523     albertel 6197: }
1.795     www      6198: 
1.523     albertel 6199: .LC_answer_charged_try {
1.797     www      6200:   background: #FFAAAA;
1.795     www      6201:   color: darkred;
                   6202:   padding: 6px;
1.523     albertel 6203: }
1.795     www      6204: 
1.779     bisitz   6205: .LC_answer_not_charged_try,
1.523     albertel 6206: .LC_answer_no_grade,
                   6207: .LC_answer_late {
1.795     www      6208:   background: lightyellow;
1.523     albertel 6209:   color: black;
1.795     www      6210:   padding: 6px;
1.523     albertel 6211: }
1.795     www      6212: 
1.523     albertel 6213: .LC_answer_previous {
1.795     www      6214:   background: lightblue;
                   6215:   color: darkblue;
                   6216:   padding: 6px;
1.523     albertel 6217: }
1.795     www      6218: 
1.779     bisitz   6219: .LC_answer_no_message {
1.777     tempelho 6220:   background: #FFFFFF;
                   6221:   color: black;
1.795     www      6222:   padding: 6px;
1.779     bisitz   6223: }
1.795     www      6224: 
1.779     bisitz   6225: .LC_answer_unknown {
                   6226:   background: orange;
                   6227:   color: black;
1.795     www      6228:   padding: 6px;
1.777     tempelho 6229: }
1.795     www      6230: 
1.529     albertel 6231: span.LC_prior_numerical,
                   6232: span.LC_prior_string,
                   6233: span.LC_prior_custom,
                   6234: span.LC_prior_reaction,
                   6235: span.LC_prior_math {
1.925     bisitz   6236:   font-family: $mono;
1.523     albertel 6237:   white-space: pre;
                   6238: }
                   6239: 
1.525     albertel 6240: span.LC_prior_string {
1.925     bisitz   6241:   font-family: $mono;
1.525     albertel 6242:   white-space: pre;
                   6243: }
                   6244: 
1.523     albertel 6245: table.LC_prior_option {
                   6246:   width: 100%;
                   6247:   border-collapse: collapse;
                   6248: }
1.795     www      6249: 
1.911     bisitz   6250: table.LC_prior_rank,
1.795     www      6251: table.LC_prior_match {
1.528     albertel 6252:   border-collapse: collapse;
                   6253: }
1.795     www      6254: 
1.528     albertel 6255: table.LC_prior_option tr td,
                   6256: table.LC_prior_rank tr td,
                   6257: table.LC_prior_match tr td {
1.524     albertel 6258:   border: 1px solid #000000;
1.515     albertel 6259: }
                   6260: 
1.855     bisitz   6261: .LC_nobreak {
1.544     albertel 6262:   white-space: nowrap;
1.519     raeburn  6263: }
                   6264: 
1.576     raeburn  6265: span.LC_cusr_emph {
                   6266:   font-style: italic;
                   6267: }
                   6268: 
1.633     raeburn  6269: span.LC_cusr_subheading {
                   6270:   font-weight: normal;
                   6271:   font-size: 85%;
                   6272: }
                   6273: 
1.861     bisitz   6274: div.LC_docs_entry_move {
1.859     bisitz   6275:   border: 1px solid #BBBBBB;
1.545     albertel 6276:   background: #DDDDDD;
1.861     bisitz   6277:   width: 22px;
1.859     bisitz   6278:   padding: 1px;
                   6279:   margin: 0;
1.545     albertel 6280: }
                   6281: 
1.861     bisitz   6282: table.LC_data_table tr > td.LC_docs_entry_commands,
                   6283: table.LC_data_table tr > td.LC_docs_entry_parameter {
1.545     albertel 6284:   font-size: x-small;
                   6285: }
1.795     www      6286: 
1.861     bisitz   6287: .LC_docs_entry_parameter {
                   6288:   white-space: nowrap;
                   6289: }
                   6290: 
1.544     albertel 6291: .LC_docs_copy {
1.545     albertel 6292:   color: #000099;
1.544     albertel 6293: }
1.795     www      6294: 
1.544     albertel 6295: .LC_docs_cut {
1.545     albertel 6296:   color: #550044;
1.544     albertel 6297: }
1.795     www      6298: 
1.544     albertel 6299: .LC_docs_rename {
1.545     albertel 6300:   color: #009900;
1.544     albertel 6301: }
1.795     www      6302: 
1.544     albertel 6303: .LC_docs_remove {
1.545     albertel 6304:   color: #990000;
                   6305: }
                   6306: 
1.547     albertel 6307: .LC_docs_reinit_warn,
                   6308: .LC_docs_ext_edit {
                   6309:   font-size: x-small;
                   6310: }
                   6311: 
1.545     albertel 6312: table.LC_docs_adddocs td,
                   6313: table.LC_docs_adddocs th {
                   6314:   border: 1px solid #BBBBBB;
                   6315:   padding: 4px;
                   6316:   background: #DDDDDD;
1.543     albertel 6317: }
                   6318: 
1.584     albertel 6319: table.LC_sty_begin {
                   6320:   background: #BBFFBB;
                   6321: }
1.795     www      6322: 
1.584     albertel 6323: table.LC_sty_end {
                   6324:   background: #FFBBBB;
                   6325: }
                   6326: 
1.589     raeburn  6327: table.LC_double_column {
1.803     bisitz   6328:   border-width: 0;
1.589     raeburn  6329:   border-collapse: collapse;
                   6330:   width: 100%;
                   6331:   padding: 2px;
                   6332: }
                   6333: 
                   6334: table.LC_double_column tr td.LC_left_col {
1.590     raeburn  6335:   top: 2px;
1.589     raeburn  6336:   left: 2px;
                   6337:   width: 47%;
                   6338:   vertical-align: top;
                   6339: }
                   6340: 
                   6341: table.LC_double_column tr td.LC_right_col {
                   6342:   top: 2px;
1.779     bisitz   6343:   right: 2px;
1.589     raeburn  6344:   width: 47%;
                   6345:   vertical-align: top;
                   6346: }
                   6347: 
1.591     raeburn  6348: div.LC_left_float {
                   6349:   float: left;
                   6350:   padding-right: 5%;
1.597     albertel 6351:   padding-bottom: 4px;
1.591     raeburn  6352: }
                   6353: 
                   6354: div.LC_clear_float_header {
1.597     albertel 6355:   padding-bottom: 2px;
1.591     raeburn  6356: }
                   6357: 
                   6358: div.LC_clear_float_footer {
1.597     albertel 6359:   padding-top: 10px;
1.591     raeburn  6360:   clear: both;
                   6361: }
                   6362: 
1.597     albertel 6363: div.LC_grade_show_user {
1.941     bisitz   6364: /*  border-left: 5px solid $sidebg; */
                   6365:   border-top: 5px solid #000000;
                   6366:   margin: 50px 0 0 0;
1.936     bisitz   6367:   padding: 15px 0 5px 10px;
1.597     albertel 6368: }
1.795     www      6369: 
1.936     bisitz   6370: div.LC_grade_show_user_odd_row {
1.941     bisitz   6371: /*  border-left: 5px solid #000000; */
                   6372: }
                   6373: 
                   6374: div.LC_grade_show_user div.LC_Box {
                   6375:   margin-right: 50px;
1.597     albertel 6376: }
                   6377: 
                   6378: div.LC_grade_submissions,
                   6379: div.LC_grade_message_center,
1.936     bisitz   6380: div.LC_grade_info_links {
1.597     albertel 6381:   margin: 5px;
                   6382:   width: 99%;
                   6383:   background: #FFFFFF;
                   6384: }
1.795     www      6385: 
1.597     albertel 6386: div.LC_grade_submissions_header,
1.936     bisitz   6387: div.LC_grade_message_center_header {
1.705     tempelho 6388:   font-weight: bold;
                   6389:   font-size: large;
1.597     albertel 6390: }
1.795     www      6391: 
1.597     albertel 6392: div.LC_grade_submissions_body,
1.936     bisitz   6393: div.LC_grade_message_center_body {
1.597     albertel 6394:   border: 1px solid black;
                   6395:   width: 99%;
                   6396:   background: #FFFFFF;
                   6397: }
1.795     www      6398: 
1.613     albertel 6399: table.LC_scantron_action {
                   6400:   width: 100%;
                   6401: }
1.795     www      6402: 
1.613     albertel 6403: table.LC_scantron_action tr th {
1.698     harmsja  6404:   font-weight:bold;
                   6405:   font-style:normal;
1.613     albertel 6406: }
1.795     www      6407: 
1.779     bisitz   6408: .LC_edit_problem_header,
1.614     albertel 6409: div.LC_edit_problem_footer {
1.705     tempelho 6410:   font-weight: normal;
                   6411:   font-size:  medium;
1.602     albertel 6412:   margin: 2px;
1.1060    bisitz   6413:   background-color: $sidebg;
1.600     albertel 6414: }
1.795     www      6415: 
1.600     albertel 6416: div.LC_edit_problem_header,
1.602     albertel 6417: div.LC_edit_problem_header div,
1.614     albertel 6418: div.LC_edit_problem_footer,
                   6419: div.LC_edit_problem_footer div,
1.602     albertel 6420: div.LC_edit_problem_editxml_header,
                   6421: div.LC_edit_problem_editxml_header div {
1.600     albertel 6422:   margin-top: 5px;
                   6423: }
1.795     www      6424: 
1.600     albertel 6425: div.LC_edit_problem_header_title {
1.705     tempelho 6426:   font-weight: bold;
                   6427:   font-size: larger;
1.602     albertel 6428:   background: $tabbg;
                   6429:   padding: 3px;
1.1060    bisitz   6430:   margin: 0 0 5px 0;
1.602     albertel 6431: }
1.795     www      6432: 
1.602     albertel 6433: table.LC_edit_problem_header_title {
                   6434:   width: 100%;
1.600     albertel 6435:   background: $tabbg;
1.602     albertel 6436: }
                   6437: 
                   6438: div.LC_edit_problem_discards {
                   6439:   float: left;
                   6440:   padding-bottom: 5px;
                   6441: }
1.795     www      6442: 
1.602     albertel 6443: div.LC_edit_problem_saves {
                   6444:   float: right;
                   6445:   padding-bottom: 5px;
1.600     albertel 6446: }
1.795     www      6447: 
1.1075.2.34  raeburn  6448: .LC_edit_opt {
                   6449:   padding-left: 1em;
                   6450:   white-space: nowrap;
                   6451: }
                   6452: 
1.911     bisitz   6453: img.stift {
1.803     bisitz   6454:   border-width: 0;
                   6455:   vertical-align: middle;
1.677     riegler  6456: }
1.680     riegler  6457: 
1.923     bisitz   6458: table td.LC_mainmenu_col_fieldset {
1.680     riegler  6459:   vertical-align: top;
1.777     tempelho 6460: }
1.795     www      6461: 
1.716     raeburn  6462: div.LC_createcourse {
1.911     bisitz   6463:   margin: 10px 10px 10px 10px;
1.716     raeburn  6464: }
                   6465: 
1.917     raeburn  6466: .LC_dccid {
                   6467:   margin: 0.2em 0 0 0;
                   6468:   padding: 0;
                   6469:   font-size: 90%;
                   6470:   display:none;
                   6471: }
                   6472: 
1.897     wenzelju 6473: ol.LC_primary_menu a:hover,
1.721     harmsja  6474: ol#LC_MenuBreadcrumbs a:hover,
                   6475: ol#LC_PathBreadcrumbs a:hover,
1.897     wenzelju 6476: ul#LC_secondary_menu a:hover,
1.721     harmsja  6477: .LC_FormSectionClearButton input:hover
1.795     www      6478: ul.LC_TabContent   li:hover a {
1.952     onken    6479:   color:$button_hover;
1.911     bisitz   6480:   text-decoration:none;
1.693     droeschl 6481: }
                   6482: 
1.779     bisitz   6483: h1 {
1.911     bisitz   6484:   padding: 0;
                   6485:   line-height:130%;
1.693     droeschl 6486: }
1.698     harmsja  6487: 
1.911     bisitz   6488: h2,
                   6489: h3,
                   6490: h4,
                   6491: h5,
                   6492: h6 {
                   6493:   margin: 5px 0 5px 0;
                   6494:   padding: 0;
                   6495:   line-height:130%;
1.693     droeschl 6496: }
1.795     www      6497: 
                   6498: .LC_hcell {
1.911     bisitz   6499:   padding:3px 15px 3px 15px;
                   6500:   margin: 0;
                   6501:   background-color:$tabbg;
                   6502:   color:$fontmenu;
                   6503:   border-bottom:solid 1px $lg_border_color;
1.693     droeschl 6504: }
1.795     www      6505: 
1.840     bisitz   6506: .LC_Box > .LC_hcell {
1.911     bisitz   6507:   margin: 0 -10px 10px -10px;
1.835     bisitz   6508: }
                   6509: 
1.721     harmsja  6510: .LC_noBorder {
1.911     bisitz   6511:   border: 0;
1.698     harmsja  6512: }
1.693     droeschl 6513: 
1.721     harmsja  6514: .LC_FormSectionClearButton input {
1.911     bisitz   6515:   background-color:transparent;
                   6516:   border: none;
                   6517:   cursor:pointer;
                   6518:   text-decoration:underline;
1.693     droeschl 6519: }
1.763     bisitz   6520: 
                   6521: .LC_help_open_topic {
1.911     bisitz   6522:   color: #FFFFFF;
                   6523:   background-color: #EEEEFF;
                   6524:   margin: 1px;
                   6525:   padding: 4px;
                   6526:   border: 1px solid #000033;
                   6527:   white-space: nowrap;
                   6528:   /* vertical-align: middle; */
1.759     neumanie 6529: }
1.693     droeschl 6530: 
1.911     bisitz   6531: dl,
                   6532: ul,
                   6533: div,
                   6534: fieldset {
                   6535:   margin: 10px 10px 10px 0;
                   6536:   /* overflow: hidden; */
1.693     droeschl 6537: }
1.795     www      6538: 
1.838     bisitz   6539: fieldset > legend {
1.911     bisitz   6540:   font-weight: bold;
                   6541:   padding: 0 5px 0 5px;
1.838     bisitz   6542: }
                   6543: 
1.813     bisitz   6544: #LC_nav_bar {
1.911     bisitz   6545:   float: left;
1.995     raeburn  6546:   background-color: $pgbg_or_bgcolor;
1.966     bisitz   6547:   margin: 0 0 2px 0;
1.807     droeschl 6548: }
                   6549: 
1.916     droeschl 6550: #LC_realm {
                   6551:   margin: 0.2em 0 0 0;
                   6552:   padding: 0;
                   6553:   font-weight: bold;
                   6554:   text-align: center;
1.995     raeburn  6555:   background-color: $pgbg_or_bgcolor;
1.916     droeschl 6556: }
                   6557: 
1.911     bisitz   6558: #LC_nav_bar em {
                   6559:   font-weight: bold;
                   6560:   font-style: normal;
1.807     droeschl 6561: }
                   6562: 
1.897     wenzelju 6563: ol.LC_primary_menu {
1.911     bisitz   6564:   float: right;
1.934     droeschl 6565:   margin: 0;
1.1075.2.2  raeburn  6566:   padding: 0;
1.995     raeburn  6567:   background-color: $pgbg_or_bgcolor;
1.807     droeschl 6568: }
                   6569: 
1.852     droeschl 6570: ol#LC_PathBreadcrumbs {
1.911     bisitz   6571:   margin: 0;
1.693     droeschl 6572: }
                   6573: 
1.897     wenzelju 6574: ol.LC_primary_menu li {
1.1075.2.2  raeburn  6575:   color: RGB(80, 80, 80);
                   6576:   vertical-align: middle;
                   6577:   text-align: left;
                   6578:   list-style: none;
                   6579:   float: left;
                   6580: }
                   6581: 
                   6582: ol.LC_primary_menu li a {
                   6583:   display: block;
                   6584:   margin: 0;
                   6585:   padding: 0 5px 0 10px;
                   6586:   text-decoration: none;
                   6587: }
                   6588: 
                   6589: ol.LC_primary_menu li ul {
                   6590:   display: none;
                   6591:   width: 10em;
                   6592:   background-color: $data_table_light;
                   6593: }
                   6594: 
                   6595: ol.LC_primary_menu li:hover ul, ol.LC_primary_menu li.hover ul {
                   6596:   display: block;
                   6597:   position: absolute;
                   6598:   margin: 0;
                   6599:   padding: 0;
1.1075.2.5  raeburn  6600:   z-index: 2;
1.1075.2.2  raeburn  6601: }
                   6602: 
                   6603: ol.LC_primary_menu li:hover li, ol.LC_primary_menu li.hover li {
                   6604:   font-size: 90%;
1.911     bisitz   6605:   vertical-align: top;
1.1075.2.2  raeburn  6606:   float: none;
1.1075.2.5  raeburn  6607:   border-left: 1px solid black;
                   6608:   border-right: 1px solid black;
1.1075.2.2  raeburn  6609: }
                   6610: 
                   6611: ol.LC_primary_menu li:hover li a, ol.LC_primary_menu li.hover li a {
1.1075.2.5  raeburn  6612:   background-color:$data_table_light;
1.1075.2.2  raeburn  6613: }
                   6614: 
                   6615: ol.LC_primary_menu li li a:hover {
                   6616:    color:$button_hover;
                   6617:    background-color:$data_table_dark;
1.693     droeschl 6618: }
                   6619: 
1.897     wenzelju 6620: ol.LC_primary_menu li img {
1.911     bisitz   6621:   vertical-align: bottom;
1.934     droeschl 6622:   height: 1.1em;
1.1075.2.3  raeburn  6623:   margin: 0.2em 0 0 0;
1.693     droeschl 6624: }
                   6625: 
1.897     wenzelju 6626: ol.LC_primary_menu a {
1.911     bisitz   6627:   color: RGB(80, 80, 80);
                   6628:   text-decoration: none;
1.693     droeschl 6629: }
1.795     www      6630: 
1.949     droeschl 6631: ol.LC_primary_menu a.LC_new_message {
                   6632:   font-weight:bold;
                   6633:   color: darkred;
                   6634: }
                   6635: 
1.975     raeburn  6636: ol.LC_docs_parameters {
                   6637:   margin-left: 0;
                   6638:   padding: 0;
                   6639:   list-style: none;
                   6640: }
                   6641: 
                   6642: ol.LC_docs_parameters li {
                   6643:   margin: 0;
                   6644:   padding-right: 20px;
                   6645:   display: inline;
                   6646: }
                   6647: 
1.976     raeburn  6648: ol.LC_docs_parameters li:before {
                   6649:   content: "\\002022 \\0020";
                   6650: }
                   6651: 
                   6652: li.LC_docs_parameters_title {
                   6653:   font-weight: bold;
                   6654: }
                   6655: 
                   6656: ol.LC_docs_parameters li.LC_docs_parameters_title:before {
                   6657:   content: "";
                   6658: }
                   6659: 
1.897     wenzelju 6660: ul#LC_secondary_menu {
1.1075.2.23  raeburn  6661:   clear: right;
1.911     bisitz   6662:   color: $fontmenu;
                   6663:   background: $tabbg;
                   6664:   list-style: none;
                   6665:   padding: 0;
                   6666:   margin: 0;
                   6667:   width: 100%;
1.995     raeburn  6668:   text-align: left;
1.1075.2.4  raeburn  6669:   float: left;
1.808     droeschl 6670: }
                   6671: 
1.897     wenzelju 6672: ul#LC_secondary_menu li {
1.911     bisitz   6673:   font-weight: bold;
                   6674:   line-height: 1.8em;
                   6675:   border-right: 1px solid black;
                   6676:   vertical-align: middle;
1.1075.2.4  raeburn  6677:   float: left;
                   6678: }
                   6679: 
                   6680: ul#LC_secondary_menu li.LC_hoverable:hover, ul#LC_secondary_menu li.hover {
                   6681:   background-color: $data_table_light;
                   6682: }
                   6683: 
                   6684: ul#LC_secondary_menu li a {
                   6685:   padding: 0 0.8em;
                   6686: }
                   6687: 
                   6688: ul#LC_secondary_menu li ul {
                   6689:   display: none;
                   6690: }
                   6691: 
                   6692: ul#LC_secondary_menu li:hover ul, ul#LC_secondary_menu li.hover ul {
                   6693:   display: block;
                   6694:   position: absolute;
                   6695:   margin: 0;
                   6696:   padding: 0;
                   6697:   list-style:none;
                   6698:   float: none;
                   6699:   background-color: $data_table_light;
1.1075.2.5  raeburn  6700:   z-index: 2;
1.1075.2.10  raeburn  6701:   margin-left: -1px;
1.1075.2.4  raeburn  6702: }
                   6703: 
                   6704: ul#LC_secondary_menu li ul li {
                   6705:   font-size: 90%;
                   6706:   vertical-align: top;
                   6707:   border-left: 1px solid black;
                   6708:   border-right: 1px solid black;
1.1075.2.33  raeburn  6709:   background-color: $data_table_light;
1.1075.2.4  raeburn  6710:   list-style:none;
                   6711:   float: none;
                   6712: }
                   6713: 
                   6714: ul#LC_secondary_menu li ul li:hover, ul#LC_secondary_menu li ul li.hover {
                   6715:   background-color: $data_table_dark;
1.807     droeschl 6716: }
                   6717: 
1.847     tempelho 6718: ul.LC_TabContent {
1.911     bisitz   6719:   display:block;
                   6720:   background: $sidebg;
                   6721:   border-bottom: solid 1px $lg_border_color;
                   6722:   list-style:none;
1.1020    raeburn  6723:   margin: -1px -10px 0 -10px;
1.911     bisitz   6724:   padding: 0;
1.693     droeschl 6725: }
                   6726: 
1.795     www      6727: ul.LC_TabContent li,
                   6728: ul.LC_TabContentBigger li {
1.911     bisitz   6729:   float:left;
1.741     harmsja  6730: }
1.795     www      6731: 
1.897     wenzelju 6732: ul#LC_secondary_menu li a {
1.911     bisitz   6733:   color: $fontmenu;
                   6734:   text-decoration: none;
1.693     droeschl 6735: }
1.795     www      6736: 
1.721     harmsja  6737: ul.LC_TabContent {
1.952     onken    6738:   min-height:20px;
1.721     harmsja  6739: }
1.795     www      6740: 
                   6741: ul.LC_TabContent li {
1.911     bisitz   6742:   vertical-align:middle;
1.959     onken    6743:   padding: 0 16px 0 10px;
1.911     bisitz   6744:   background-color:$tabbg;
                   6745:   border-bottom:solid 1px $lg_border_color;
1.1020    raeburn  6746:   border-left: solid 1px $font;
1.721     harmsja  6747: }
1.795     www      6748: 
1.847     tempelho 6749: ul.LC_TabContent .right {
1.911     bisitz   6750:   float:right;
1.847     tempelho 6751: }
                   6752: 
1.911     bisitz   6753: ul.LC_TabContent li a,
                   6754: ul.LC_TabContent li {
                   6755:   color:rgb(47,47,47);
                   6756:   text-decoration:none;
                   6757:   font-size:95%;
                   6758:   font-weight:bold;
1.952     onken    6759:   min-height:20px;
                   6760: }
                   6761: 
1.959     onken    6762: ul.LC_TabContent li a:hover,
                   6763: ul.LC_TabContent li a:focus {
1.952     onken    6764:   color: $button_hover;
1.959     onken    6765:   background:none;
                   6766:   outline:none;
1.952     onken    6767: }
                   6768: 
                   6769: ul.LC_TabContent li:hover {
                   6770:   color: $button_hover;
                   6771:   cursor:pointer;
1.721     harmsja  6772: }
1.795     www      6773: 
1.911     bisitz   6774: ul.LC_TabContent li.active {
1.952     onken    6775:   color: $font;
1.911     bisitz   6776:   background:#FFFFFF url(/adm/lonIcons/open.gif) no-repeat scroll right center;
1.952     onken    6777:   border-bottom:solid 1px #FFFFFF;
                   6778:   cursor: default;
1.744     ehlerst  6779: }
1.795     www      6780: 
1.959     onken    6781: ul.LC_TabContent li.active a {
                   6782:   color:$font;
                   6783:   background:#FFFFFF;
                   6784:   outline: none;
                   6785: }
1.1047    raeburn  6786: 
                   6787: ul.LC_TabContent li.goback {
                   6788:   float: left;
                   6789:   border-left: none;
                   6790: }
                   6791: 
1.870     tempelho 6792: #maincoursedoc {
1.911     bisitz   6793:   clear:both;
1.870     tempelho 6794: }
                   6795: 
                   6796: ul.LC_TabContentBigger {
1.911     bisitz   6797:   display:block;
                   6798:   list-style:none;
                   6799:   padding: 0;
1.870     tempelho 6800: }
                   6801: 
1.795     www      6802: ul.LC_TabContentBigger li {
1.911     bisitz   6803:   vertical-align:bottom;
                   6804:   height: 30px;
                   6805:   font-size:110%;
                   6806:   font-weight:bold;
                   6807:   color: #737373;
1.841     tempelho 6808: }
                   6809: 
1.957     onken    6810: ul.LC_TabContentBigger li.active {
                   6811:   position: relative;
                   6812:   top: 1px;
                   6813: }
                   6814: 
1.870     tempelho 6815: ul.LC_TabContentBigger li a {
1.911     bisitz   6816:   background:url('/adm/lonIcons/tabbgleft.gif') left bottom no-repeat;
                   6817:   height: 30px;
                   6818:   line-height: 30px;
                   6819:   text-align: center;
                   6820:   display: block;
                   6821:   text-decoration: none;
1.958     onken    6822:   outline: none;  
1.741     harmsja  6823: }
1.795     www      6824: 
1.870     tempelho 6825: ul.LC_TabContentBigger li.active a {
1.911     bisitz   6826:   background:url('/adm/lonIcons/tabbgleft.gif') left top no-repeat;
                   6827:   color:$font;
1.744     ehlerst  6828: }
1.795     www      6829: 
1.870     tempelho 6830: ul.LC_TabContentBigger li b {
1.911     bisitz   6831:   background: url('/adm/lonIcons/tabbgright.gif') no-repeat right bottom;
                   6832:   display: block;
                   6833:   float: left;
                   6834:   padding: 0 30px;
1.957     onken    6835:   border-bottom: 1px solid $lg_border_color;
1.870     tempelho 6836: }
                   6837: 
1.956     onken    6838: ul.LC_TabContentBigger li:hover b {
                   6839:   color:$button_hover;
                   6840: }
                   6841: 
1.870     tempelho 6842: ul.LC_TabContentBigger li.active b {
1.911     bisitz   6843:   background:url('/adm/lonIcons/tabbgright.gif') right top no-repeat;
                   6844:   color:$font;
1.957     onken    6845:   border: 0;
1.741     harmsja  6846: }
1.693     droeschl 6847: 
1.870     tempelho 6848: 
1.862     bisitz   6849: ul.LC_CourseBreadcrumbs {
                   6850:   background: $sidebg;
1.1020    raeburn  6851:   height: 2em;
1.862     bisitz   6852:   padding-left: 10px;
1.1020    raeburn  6853:   margin: 0;
1.862     bisitz   6854:   list-style-position: inside;
                   6855: }
                   6856: 
1.911     bisitz   6857: ol#LC_MenuBreadcrumbs,
1.862     bisitz   6858: ol#LC_PathBreadcrumbs {
1.911     bisitz   6859:   padding-left: 10px;
                   6860:   margin: 0;
1.933     droeschl 6861:   height: 2.5em;  /* equal to #LC_breadcrumbs line-height */
1.693     droeschl 6862: }
                   6863: 
1.911     bisitz   6864: ol#LC_MenuBreadcrumbs li,
                   6865: ol#LC_PathBreadcrumbs li,
1.862     bisitz   6866: ul.LC_CourseBreadcrumbs li {
1.911     bisitz   6867:   display: inline;
1.933     droeschl 6868:   white-space: normal;  
1.693     droeschl 6869: }
                   6870: 
1.823     bisitz   6871: ol#LC_MenuBreadcrumbs li a,
1.862     bisitz   6872: ul.LC_CourseBreadcrumbs li a {
1.911     bisitz   6873:   text-decoration: none;
                   6874:   font-size:90%;
1.693     droeschl 6875: }
1.795     www      6876: 
1.969     droeschl 6877: ol#LC_MenuBreadcrumbs h1 {
                   6878:   display: inline;
                   6879:   font-size: 90%;
                   6880:   line-height: 2.5em;
                   6881:   margin: 0;
                   6882:   padding: 0;
                   6883: }
                   6884: 
1.795     www      6885: ol#LC_PathBreadcrumbs li a {
1.911     bisitz   6886:   text-decoration:none;
                   6887:   font-size:100%;
                   6888:   font-weight:bold;
1.693     droeschl 6889: }
1.795     www      6890: 
1.840     bisitz   6891: .LC_Box {
1.911     bisitz   6892:   border: solid 1px $lg_border_color;
                   6893:   padding: 0 10px 10px 10px;
1.746     neumanie 6894: }
1.795     www      6895: 
1.1020    raeburn  6896: .LC_DocsBox {
                   6897:   border: solid 1px $lg_border_color;
                   6898:   padding: 0 0 10px 10px;
                   6899: }
                   6900: 
1.795     www      6901: .LC_AboutMe_Image {
1.911     bisitz   6902:   float:left;
                   6903:   margin-right:10px;
1.747     neumanie 6904: }
1.795     www      6905: 
                   6906: .LC_Clear_AboutMe_Image {
1.911     bisitz   6907:   clear:left;
1.747     neumanie 6908: }
1.795     www      6909: 
1.721     harmsja  6910: dl.LC_ListStyleClean dt {
1.911     bisitz   6911:   padding-right: 5px;
                   6912:   display: table-header-group;
1.693     droeschl 6913: }
                   6914: 
1.721     harmsja  6915: dl.LC_ListStyleClean dd {
1.911     bisitz   6916:   display: table-row;
1.693     droeschl 6917: }
                   6918: 
1.721     harmsja  6919: .LC_ListStyleClean,
                   6920: .LC_ListStyleSimple,
                   6921: .LC_ListStyleNormal,
1.795     www      6922: .LC_ListStyleSpecial {
1.911     bisitz   6923:   /* display:block; */
                   6924:   list-style-position: inside;
                   6925:   list-style-type: none;
                   6926:   overflow: hidden;
                   6927:   padding: 0;
1.693     droeschl 6928: }
                   6929: 
1.721     harmsja  6930: .LC_ListStyleSimple li,
                   6931: .LC_ListStyleSimple dd,
                   6932: .LC_ListStyleNormal li,
                   6933: .LC_ListStyleNormal dd,
                   6934: .LC_ListStyleSpecial li,
1.795     www      6935: .LC_ListStyleSpecial dd {
1.911     bisitz   6936:   margin: 0;
                   6937:   padding: 5px 5px 5px 10px;
                   6938:   clear: both;
1.693     droeschl 6939: }
                   6940: 
1.721     harmsja  6941: .LC_ListStyleClean li,
                   6942: .LC_ListStyleClean dd {
1.911     bisitz   6943:   padding-top: 0;
                   6944:   padding-bottom: 0;
1.693     droeschl 6945: }
                   6946: 
1.721     harmsja  6947: .LC_ListStyleSimple dd,
1.795     www      6948: .LC_ListStyleSimple li {
1.911     bisitz   6949:   border-bottom: solid 1px $lg_border_color;
1.693     droeschl 6950: }
                   6951: 
1.721     harmsja  6952: .LC_ListStyleSpecial li,
                   6953: .LC_ListStyleSpecial dd {
1.911     bisitz   6954:   list-style-type: none;
                   6955:   background-color: RGB(220, 220, 220);
                   6956:   margin-bottom: 4px;
1.693     droeschl 6957: }
                   6958: 
1.721     harmsja  6959: table.LC_SimpleTable {
1.911     bisitz   6960:   margin:5px;
                   6961:   border:solid 1px $lg_border_color;
1.795     www      6962: }
1.693     droeschl 6963: 
1.721     harmsja  6964: table.LC_SimpleTable tr {
1.911     bisitz   6965:   padding: 0;
                   6966:   border:solid 1px $lg_border_color;
1.693     droeschl 6967: }
1.795     www      6968: 
                   6969: table.LC_SimpleTable thead {
1.911     bisitz   6970:   background:rgb(220,220,220);
1.693     droeschl 6971: }
                   6972: 
1.721     harmsja  6973: div.LC_columnSection {
1.911     bisitz   6974:   display: block;
                   6975:   clear: both;
                   6976:   overflow: hidden;
                   6977:   margin: 0;
1.693     droeschl 6978: }
                   6979: 
1.721     harmsja  6980: div.LC_columnSection>* {
1.911     bisitz   6981:   float: left;
                   6982:   margin: 10px 20px 10px 0;
                   6983:   overflow:hidden;
1.693     droeschl 6984: }
1.721     harmsja  6985: 
1.795     www      6986: table em {
1.911     bisitz   6987:   font-weight: bold;
                   6988:   font-style: normal;
1.748     schulted 6989: }
1.795     www      6990: 
1.779     bisitz   6991: table.LC_tableBrowseRes,
1.795     www      6992: table.LC_tableOfContent {
1.911     bisitz   6993:   border:none;
                   6994:   border-spacing: 1px;
                   6995:   padding: 3px;
                   6996:   background-color: #FFFFFF;
                   6997:   font-size: 90%;
1.753     droeschl 6998: }
1.789     droeschl 6999: 
1.911     bisitz   7000: table.LC_tableOfContent {
                   7001:   border-collapse: collapse;
1.789     droeschl 7002: }
                   7003: 
1.771     droeschl 7004: table.LC_tableBrowseRes a,
1.768     schulted 7005: table.LC_tableOfContent a {
1.911     bisitz   7006:   background-color: transparent;
                   7007:   text-decoration: none;
1.753     droeschl 7008: }
                   7009: 
1.795     www      7010: table.LC_tableOfContent img {
1.911     bisitz   7011:   border: none;
                   7012:   height: 1.3em;
                   7013:   vertical-align: text-bottom;
                   7014:   margin-right: 0.3em;
1.753     droeschl 7015: }
1.757     schulted 7016: 
1.795     www      7017: a#LC_content_toolbar_firsthomework {
1.911     bisitz   7018:   background-image:url(/res/adm/pages/open-first-problem.gif);
1.774     ehlerst  7019: }
                   7020: 
1.795     www      7021: a#LC_content_toolbar_everything {
1.911     bisitz   7022:   background-image:url(/res/adm/pages/show-all.gif);
1.774     ehlerst  7023: }
                   7024: 
1.795     www      7025: a#LC_content_toolbar_uncompleted {
1.911     bisitz   7026:   background-image:url(/res/adm/pages/show-incomplete-problems.gif);
1.774     ehlerst  7027: }
                   7028: 
1.795     www      7029: #LC_content_toolbar_clearbubbles {
1.911     bisitz   7030:   background-image:url(/res/adm/pages/mark-discussionentries-read.gif);
1.774     ehlerst  7031: }
                   7032: 
1.795     www      7033: a#LC_content_toolbar_changefolder {
1.911     bisitz   7034:   background : url(/res/adm/pages/close-all-folders.gif) top center ;
1.757     schulted 7035: }
                   7036: 
1.795     www      7037: a#LC_content_toolbar_changefolder_toggled {
1.911     bisitz   7038:   background-image:url(/res/adm/pages/open-all-folders.gif);
1.757     schulted 7039: }
                   7040: 
1.1043    raeburn  7041: a#LC_content_toolbar_edittoplevel {
                   7042:   background-image:url(/res/adm/pages/edittoplevel.gif);
                   7043: }
                   7044: 
1.795     www      7045: ul#LC_toolbar li a:hover {
1.911     bisitz   7046:   background-position: bottom center;
1.757     schulted 7047: }
                   7048: 
1.795     www      7049: ul#LC_toolbar {
1.911     bisitz   7050:   padding: 0;
                   7051:   margin: 2px;
                   7052:   list-style:none;
                   7053:   position:relative;
                   7054:   background-color:white;
1.1075.2.9  raeburn  7055:   overflow: auto;
1.757     schulted 7056: }
                   7057: 
1.795     www      7058: ul#LC_toolbar li {
1.911     bisitz   7059:   border:1px solid white;
                   7060:   padding: 0;
                   7061:   margin: 0;
                   7062:   float: left;
                   7063:   display:inline;
                   7064:   vertical-align:middle;
1.1075.2.9  raeburn  7065:   white-space: nowrap;
1.911     bisitz   7066: }
1.757     schulted 7067: 
1.783     amueller 7068: 
1.795     www      7069: a.LC_toolbarItem {
1.911     bisitz   7070:   display:block;
                   7071:   padding: 0;
                   7072:   margin: 0;
                   7073:   height: 32px;
                   7074:   width: 32px;
                   7075:   color:white;
                   7076:   border: none;
                   7077:   background-repeat:no-repeat;
                   7078:   background-color:transparent;
1.757     schulted 7079: }
                   7080: 
1.915     droeschl 7081: ul.LC_funclist {
                   7082:     margin: 0;
                   7083:     padding: 0.5em 1em 0.5em 0;
                   7084: }
                   7085: 
1.933     droeschl 7086: ul.LC_funclist > li:first-child {
                   7087:     font-weight:bold; 
                   7088:     margin-left:0.8em;
                   7089: }
                   7090: 
1.915     droeschl 7091: ul.LC_funclist + ul.LC_funclist {
                   7092:     /* 
                   7093:        left border as a seperator if we have more than
                   7094:        one list 
                   7095:     */
                   7096:     border-left: 1px solid $sidebg;
                   7097:     /* 
                   7098:        this hides the left border behind the border of the 
                   7099:        outer box if element is wrapped to the next 'line' 
                   7100:     */
                   7101:     margin-left: -1px;
                   7102: }
                   7103: 
1.843     bisitz   7104: ul.LC_funclist li {
1.915     droeschl 7105:   display: inline;
1.782     bisitz   7106:   white-space: nowrap;
1.915     droeschl 7107:   margin: 0 0 0 25px;
                   7108:   line-height: 150%;
1.782     bisitz   7109: }
                   7110: 
1.974     wenzelju 7111: .LC_hidden {
                   7112:   display: none;
                   7113: }
                   7114: 
1.1030    www      7115: .LCmodal-overlay {
                   7116: 		position:fixed;
                   7117: 		top:0;
                   7118: 		right:0;
                   7119: 		bottom:0;
                   7120: 		left:0;
                   7121: 		height:100%;
                   7122: 		width:100%;
                   7123: 		margin:0;
                   7124: 		padding:0;
                   7125: 		background:#999;
                   7126: 		opacity:.75;
                   7127: 		filter: alpha(opacity=75);
                   7128: 		-moz-opacity: 0.75;
                   7129: 		z-index:101;
                   7130: }
                   7131: 
                   7132: * html .LCmodal-overlay {   
                   7133: 		position: absolute;
                   7134: 		height: expression(document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + 'px');
                   7135: }
                   7136: 
                   7137: .LCmodal-window {
                   7138: 		position:fixed;
                   7139: 		top:50%;
                   7140: 		left:50%;
                   7141: 		margin:0;
                   7142: 		padding:0;
                   7143: 		z-index:102;
                   7144: 	}
                   7145: 
                   7146: * html .LCmodal-window {
                   7147: 		position:absolute;
                   7148: }
                   7149: 
                   7150: .LCclose-window {
                   7151: 		position:absolute;
                   7152: 		width:32px;
                   7153: 		height:32px;
                   7154: 		right:8px;
                   7155: 		top:8px;
                   7156: 		background:transparent url('/res/adm/pages/process-stop.png') no-repeat scroll right top;
                   7157: 		text-indent:-99999px;
                   7158: 		overflow:hidden;
                   7159: 		cursor:pointer;
                   7160: }
                   7161: 
1.1075.2.17  raeburn  7162: /*
                   7163:   styles used by TTH when "Default set of options to pass to tth/m
                   7164:   when converting TeX" in course settings has been set
                   7165: 
                   7166:   option passed: -t
                   7167: 
                   7168: */
                   7169: 
                   7170: td div.comp { margin-top: -0.6ex; margin-bottom: -1ex;}
                   7171: td div.comb { margin-top: -0.6ex; margin-bottom: -.6ex;}
                   7172: td div.hrcomp { line-height: 0.9; margin-top: -0.8ex; margin-bottom: -1ex;}
                   7173: td div.norm {line-height:normal;}
                   7174: 
                   7175: /*
                   7176:   option passed -y3
                   7177: */
                   7178: 
                   7179: span.roman {font-family: serif; font-style: normal; font-weight: normal;}
                   7180: span.overacc2 {position: relative;  left: .8em; top: -1.2ex;}
                   7181: span.overacc1 {position: relative;  left: .6em; top: -1.2ex;}
                   7182: 
1.343     albertel 7183: END
                   7184: }
                   7185: 
1.306     albertel 7186: =pod
                   7187: 
                   7188: =item * &headtag()
                   7189: 
                   7190: Returns a uniform footer for LON-CAPA web pages.
                   7191: 
1.307     albertel 7192: Inputs: $title - optional title for the head
                   7193:         $head_extra - optional extra HTML to put inside the <head>
1.315     albertel 7194:         $args - optional arguments
1.319     albertel 7195:             force_register - if is true call registerurl so the remote is 
                   7196:                              informed
1.415     albertel 7197:             redirect       -> array ref of
                   7198:                                    1- seconds before redirect occurs
                   7199:                                    2- url to redirect to
                   7200:                                    3- whether the side effect should occur
1.315     albertel 7201:                            (side effect of setting 
                   7202:                                $env{'internal.head.redirect'} to the url 
                   7203:                                redirected too)
1.352     albertel 7204:             domain         -> force to color decorate a page for a specific
                   7205:                                domain
                   7206:             function       -> force usage of a specific rolish color scheme
                   7207:             bgcolor        -> override the default page bgcolor
1.460     albertel 7208:             no_auto_mt_title
                   7209:                            -> prevent &mt()ing the title arg
1.464     albertel 7210: 
1.306     albertel 7211: =cut
                   7212: 
                   7213: sub headtag {
1.313     albertel 7214:     my ($title,$head_extra,$args) = @_;
1.306     albertel 7215:     
1.363     albertel 7216:     my $function = $args->{'function'} || &get_users_function();
                   7217:     my $domain   = $args->{'domain'}   || &determinedomain();
                   7218:     my $bgcolor  = $args->{'bgcolor'}  || &designparm($function.'.pgbg',$domain);
1.418     albertel 7219:     my $url = join(':',$env{'user.name'},$env{'user.domain'},
1.458     albertel 7220: 		   $Apache::lonnet::perlvar{'lonVersion'},
1.531     albertel 7221: 		   #time(),
1.418     albertel 7222: 		   $env{'environment.color.timestamp'},
1.363     albertel 7223: 		   $function,$domain,$bgcolor);
                   7224: 
1.369     www      7225:     $url = '/adm/css/'.&escape($url).'.css';
1.363     albertel 7226: 
1.308     albertel 7227:     my $result =
                   7228: 	'<head>'.
1.461     albertel 7229: 	&font_settings();
1.319     albertel 7230: 
1.1064    raeburn  7231:     my $inhibitprint = &print_suppression();
                   7232: 
1.461     albertel 7233:     if (!$args->{'frameset'}) {
                   7234: 	$result .= &Apache::lonhtmlcommon::htmlareaheaders();
                   7235:     }
1.1075.2.12  raeburn  7236:     if ($args->{'force_register'}) {
                   7237:         $result .= &Apache::lonmenu::registerurl(1);
1.319     albertel 7238:     }
1.436     albertel 7239:     if (!$args->{'no_nav_bar'} 
                   7240: 	&& !$args->{'only_body'}
                   7241: 	&& !$args->{'frameset'}) {
                   7242: 	$result .= &help_menu_js();
1.1032    www      7243:         $result.=&modal_window();
1.1038    www      7244:         $result.=&togglebox_script();
1.1034    www      7245:         $result.=&wishlist_window();
1.1041    www      7246:         $result.=&LCprogressbarUpdate_script();
1.1034    www      7247:     } else {
                   7248:         if ($args->{'add_modal'}) {
                   7249:            $result.=&modal_window();
                   7250:         }
                   7251:         if ($args->{'add_wishlist'}) {
                   7252:            $result.=&wishlist_window();
                   7253:         }
1.1038    www      7254:         if ($args->{'add_togglebox'}) {
                   7255:            $result.=&togglebox_script();
                   7256:         }
1.1041    www      7257:         if ($args->{'add_progressbar'}) {
                   7258:            $result.=&LCprogressbarUpdate_script();
                   7259:         }
1.436     albertel 7260:     }
1.314     albertel 7261:     if (ref($args->{'redirect'})) {
1.414     albertel 7262: 	my ($time,$url,$inhibit_continue) = @{$args->{'redirect'}};
1.315     albertel 7263: 	$url = &Apache::lonenc::check_encrypt($url);
1.414     albertel 7264: 	if (!$inhibit_continue) {
                   7265: 	    $env{'internal.head.redirect'} = $url;
                   7266: 	}
1.313     albertel 7267: 	$result.=<<ADDMETA
                   7268: <meta http-equiv="pragma" content="no-cache" />
1.344     albertel 7269: <meta http-equiv="Refresh" content="$time; url=$url" />
1.313     albertel 7270: ADDMETA
                   7271:     }
1.306     albertel 7272:     if (!defined($title)) {
                   7273: 	$title = 'The LearningOnline Network with CAPA';
                   7274:     }
1.460     albertel 7275:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
                   7276:     $result .= '<title> LON-CAPA '.$title.'</title>'
1.414     albertel 7277: 	.'<link rel="stylesheet" type="text/css" href="'.$url.'" />'
1.1064    raeburn  7278:         .$inhibitprint
1.414     albertel 7279: 	.$head_extra;
1.962     droeschl 7280:     return $result.'</head>';
1.306     albertel 7281: }
                   7282: 
                   7283: =pod
                   7284: 
1.340     albertel 7285: =item * &font_settings()
                   7286: 
                   7287: Returns neccessary <meta> to set the proper encoding
                   7288: 
                   7289: Inputs: none
                   7290: 
                   7291: =cut
                   7292: 
                   7293: sub font_settings {
                   7294:     my $headerstring='';
1.647     www      7295:     if (!$env{'browser.mathml'} && $env{'browser.unicode'}) {
1.340     albertel 7296: 	$headerstring.=
                   7297: 	    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
                   7298:     }
                   7299:     return $headerstring;
                   7300: }
                   7301: 
1.341     albertel 7302: =pod
                   7303: 
1.1064    raeburn  7304: =item * &print_suppression()
                   7305: 
                   7306: In course context returns css which causes the body to be blank when media="print",
                   7307: if printout generation is unavailable for the current resource.
                   7308: 
                   7309: This could be because:
                   7310: 
                   7311: (a) printstartdate is in the future
                   7312: 
                   7313: (b) printenddate is in the past
                   7314: 
                   7315: (c) there is an active exam block with "printout"
                   7316: functionality blocked
                   7317: 
                   7318: Users with pav, pfo or evb privileges are exempt.
                   7319: 
                   7320: Inputs: none
                   7321: 
                   7322: =cut
                   7323: 
                   7324: 
                   7325: sub print_suppression {
                   7326:     my $noprint;
                   7327:     if ($env{'request.course.id'}) {
                   7328:         my $scope = $env{'request.course.id'};
                   7329:         if ((&Apache::lonnet::allowed('pav',$scope)) ||
                   7330:             (&Apache::lonnet::allowed('pfo',$scope))) {
                   7331:             return;
                   7332:         }
                   7333:         if ($env{'request.course.sec'} ne '') {
                   7334:             $scope .= "/$env{'request.course.sec'}";
                   7335:             if ((&Apache::lonnet::allowed('pav',$scope)) ||
                   7336:                 (&Apache::lonnet::allowed('pfo',$scope))) {
1.1065    raeburn  7337:                 return;
1.1064    raeburn  7338:             }
                   7339:         }
                   7340:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7341:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.1065    raeburn  7342:         my $blocked = &blocking_status('printout',$cnum,$cdom);
1.1064    raeburn  7343:         if ($blocked) {
                   7344:             my $checkrole = "cm./$cdom/$cnum";
                   7345:             if ($env{'request.course.sec'} ne '') {
                   7346:                 $checkrole .= "/$env{'request.course.sec'}";
                   7347:             }
                   7348:             unless ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
                   7349:                     ($env{'request.role'} !~ m{^st\./$cdom/$cnum})) {
                   7350:                 $noprint = 1;
                   7351:             }
                   7352:         }
                   7353:         unless ($noprint) {
                   7354:             my $symb = &Apache::lonnet::symbread();
                   7355:             if ($symb ne '') {
                   7356:                 my $navmap = Apache::lonnavmaps::navmap->new();
                   7357:                 if (ref($navmap)) {
                   7358:                     my $res = $navmap->getBySymb($symb);
                   7359:                     if (ref($res)) {
                   7360:                         if (!$res->resprintable()) {
                   7361:                             $noprint = 1;
                   7362:                         }
                   7363:                     }
                   7364:                 }
                   7365:             }
                   7366:         }
                   7367:         if ($noprint) {
                   7368:             return <<"ENDSTYLE";
                   7369: <style type="text/css" media="print">
                   7370:     body { display:none }
                   7371: </style>
                   7372: ENDSTYLE
                   7373:         }
                   7374:     }
                   7375:     return;
                   7376: }
                   7377: 
                   7378: =pod
                   7379: 
1.341     albertel 7380: =item * &xml_begin()
                   7381: 
                   7382: Returns the needed doctype and <html>
                   7383: 
                   7384: Inputs: none
                   7385: 
                   7386: =cut
                   7387: 
                   7388: sub xml_begin {
                   7389:     my $output='';
                   7390: 
                   7391:     if ($env{'browser.mathml'}) {
                   7392: 	$output='<?xml version="1.0"?>'
                   7393:             #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
                   7394: #            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                   7395:             
                   7396: #	    .'<!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">] >'
                   7397: 	    .'<!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">'
                   7398:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                   7399: 	    .'xmlns="http://www.w3.org/1999/xhtml">';
                   7400:     } else {
1.849     bisitz   7401: 	$output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
                   7402:            .'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
1.341     albertel 7403:     }
                   7404:     return $output;
                   7405: }
1.340     albertel 7406: 
                   7407: =pod
                   7408: 
1.306     albertel 7409: =item * &start_page()
                   7410: 
                   7411: Returns a complete <html> .. <body> section for LON-CAPA web pages.
                   7412: 
1.648     raeburn  7413: Inputs:
                   7414: 
                   7415: =over 4
                   7416: 
                   7417: $title - optional title for the page
                   7418: 
                   7419: $head_extra - optional extra HTML to incude inside the <head>
                   7420: 
                   7421: $args - additional optional args supported are:
                   7422: 
                   7423: =over 8
                   7424: 
                   7425:              only_body      -> is true will set &bodytag() onlybodytag
1.317     albertel 7426:                                     arg on
1.814     bisitz   7427:              no_nav_bar     -> is true will set &bodytag() no_nav_bar arg on
1.648     raeburn  7428:              add_entries    -> additional attributes to add to the  <body>
                   7429:              domain         -> force to color decorate a page for a 
1.317     albertel 7430:                                     specific domain
1.648     raeburn  7431:              function       -> force usage of a specific rolish color
1.317     albertel 7432:                                     scheme
1.648     raeburn  7433:              redirect       -> see &headtag()
                   7434:              bgcolor        -> override the default page bg color
                   7435:              js_ready       -> return a string ready for being used in 
1.317     albertel 7436:                                     a javascript writeln
1.648     raeburn  7437:              html_encode    -> return a string ready for being used in 
1.320     albertel 7438:                                     a html attribute
1.648     raeburn  7439:              force_register -> if is true will turn on the &bodytag()
1.317     albertel 7440:                                     $forcereg arg
1.648     raeburn  7441:              frameset       -> if true will start with a <frameset>
1.330     albertel 7442:                                     rather than <body>
1.648     raeburn  7443:              skip_phases    -> hash ref of 
1.338     albertel 7444:                                     head -> skip the <html><head> generation
                   7445:                                     body -> skip all <body> generation
1.1075.2.12  raeburn  7446:              no_inline_link -> if true and in remote mode, don't show the
                   7447:                                     'Switch To Inline Menu' link
1.648     raeburn  7448:              no_auto_mt_title -> prevent &mt()ing the title arg
                   7449:              inherit_jsmath -> when creating popup window in a page,
                   7450:                                     should it have jsmath forced on by the
                   7451:                                     current page
1.867     kalberla 7452:              bread_crumbs ->             Array containing breadcrumbs
1.983     raeburn  7453:              bread_crumbs_component ->  if exists show it as headline else show only the breadcrumbs
1.1075.2.15  raeburn  7454:              group          -> includes the current group, if page is for a
                   7455:                                specific group
1.361     albertel 7456: 
1.648     raeburn  7457: =back
1.460     albertel 7458: 
1.648     raeburn  7459: =back
1.562     albertel 7460: 
1.306     albertel 7461: =cut
                   7462: 
                   7463: sub start_page {
1.309     albertel 7464:     my ($title,$head_extra,$args) = @_;
1.318     albertel 7465:     #&Apache::lonnet::logthis("start_page ".join(':',caller(0)));
1.319     albertel 7466: 
1.315     albertel 7467:     $env{'internal.start_page'}++;
1.1075.2.15  raeburn  7468:     my ($result,@advtools);
1.964     droeschl 7469: 
1.338     albertel 7470:     if (! exists($args->{'skip_phases'}{'head'}) ) {
1.1030    www      7471:         $result .= &xml_begin() . &headtag($title, $head_extra, $args);
1.338     albertel 7472:     }
                   7473:     
                   7474:     if (! exists($args->{'skip_phases'}{'body'}) ) {
                   7475: 	if ($args->{'frameset'}) {
                   7476: 	    my $attr_string = &make_attr_string($args->{'force_register'},
                   7477: 						$args->{'add_entries'});
                   7478: 	    $result .= "\n<frameset $attr_string>\n";
1.831     bisitz   7479:         } else {
                   7480:             $result .=
                   7481:                 &bodytag($title, 
                   7482:                          $args->{'function'},       $args->{'add_entries'},
                   7483:                          $args->{'only_body'},      $args->{'domain'},
                   7484:                          $args->{'force_register'}, $args->{'no_nav_bar'},
1.1075.2.12  raeburn  7485:                          $args->{'bgcolor'},        $args->{'no_inline_link'},
1.1075.2.15  raeburn  7486:                          $args,                     \@advtools);
1.831     bisitz   7487:         }
1.330     albertel 7488:     }
1.338     albertel 7489: 
1.315     albertel 7490:     if ($args->{'js_ready'}) {
1.713     kaisler  7491: 		$result = &js_ready($result);
1.315     albertel 7492:     }
1.320     albertel 7493:     if ($args->{'html_encode'}) {
1.713     kaisler  7494: 		$result = &html_encode($result);
                   7495:     }
                   7496: 
1.813     bisitz   7497:     # Preparation for new and consistent functionlist at top of screen
                   7498:     # if ($args->{'functionlist'}) {
                   7499:     #            $result .= &build_functionlist();
                   7500:     #}
                   7501: 
1.964     droeschl 7502:     # Don't add anything more if only_body wanted or in const space
                   7503:     return $result if    $args->{'only_body'} 
                   7504:                       || $env{'request.state'} eq 'construct';
1.813     bisitz   7505: 
                   7506:     #Breadcrumbs
1.758     kaisler  7507:     if (exists($args->{'bread_crumbs'}) or exists($args->{'bread_crumbs_component'})) {
                   7508: 		&Apache::lonhtmlcommon::clear_breadcrumbs();
                   7509: 		#if any br links exists, add them to the breadcrumbs
                   7510: 		if (exists($args->{'bread_crumbs'}) and ref($args->{'bread_crumbs'}) eq 'ARRAY') {         
                   7511: 			foreach my $crumb (@{$args->{'bread_crumbs'}}){
                   7512: 				&Apache::lonhtmlcommon::add_breadcrumb($crumb);
                   7513: 			}
                   7514: 		}
1.1075.2.19  raeburn  7515:                 # if @advtools array contains items add then to the breadcrumbs
                   7516:                 if (@advtools > 0) {
                   7517:                     &Apache::lonmenu::advtools_crumbs(@advtools);
                   7518:                 }
1.758     kaisler  7519: 
                   7520: 		#if bread_crumbs_component exists show it as headline else show only the breadcrumbs
                   7521: 		if(exists($args->{'bread_crumbs_component'})){
                   7522: 			$result .= &Apache::lonhtmlcommon::breadcrumbs($args->{'bread_crumbs_component'});
                   7523: 		}else{
                   7524: 			$result .= &Apache::lonhtmlcommon::breadcrumbs();
                   7525: 		}
1.1075.2.24  raeburn  7526:     } elsif (($env{'environment.remote'} eq 'on') &&
                   7527:              ($env{'form.inhibitmenu'} ne 'yes') &&
                   7528:              ($env{'request.noversionuri'} =~ m{^/res/}) &&
                   7529:              ($env{'request.noversionuri'} !~ m{^/res/adm/pages/})) {
1.1075.2.21  raeburn  7530:         $result .= '<div style="padding:0;margin:0;clear:both"><hr /></div>';
1.320     albertel 7531:     }
1.315     albertel 7532:     return $result;
1.306     albertel 7533: }
                   7534: 
                   7535: sub end_page {
1.315     albertel 7536:     my ($args) = @_;
                   7537:     $env{'internal.end_page'}++;
1.330     albertel 7538:     my $result;
1.335     albertel 7539:     if ($args->{'discussion'}) {
                   7540: 	my ($target,$parser);
                   7541: 	if (ref($args->{'discussion'})) {
                   7542: 	    ($target,$parser) =($args->{'discussion'}{'target'},
                   7543: 				$args->{'discussion'}{'parser'});
                   7544: 	}
                   7545: 	$result .= &Apache::lonxml::xmlend($target,$parser);
                   7546:     }
1.330     albertel 7547:     if ($args->{'frameset'}) {
                   7548: 	$result .= '</frameset>';
                   7549:     } else {
1.635     raeburn  7550: 	$result .= &endbodytag($args);
1.330     albertel 7551:     }
1.1075.2.6  raeburn  7552:     unless ($args->{'notbody'}) {
                   7553:         $result .= "\n</html>";
                   7554:     }
1.330     albertel 7555: 
1.315     albertel 7556:     if ($args->{'js_ready'}) {
1.317     albertel 7557: 	$result = &js_ready($result);
1.315     albertel 7558:     }
1.335     albertel 7559: 
1.320     albertel 7560:     if ($args->{'html_encode'}) {
                   7561: 	$result = &html_encode($result);
                   7562:     }
1.335     albertel 7563: 
1.315     albertel 7564:     return $result;
                   7565: }
                   7566: 
1.1034    www      7567: sub wishlist_window {
                   7568:     return(<<'ENDWISHLIST');
1.1046    raeburn  7569: <script type="text/javascript">
1.1034    www      7570: // <![CDATA[
                   7571: // <!-- BEGIN LON-CAPA Internal
                   7572: function set_wishlistlink(title, path) {
                   7573:     if (!title) {
                   7574:         title = document.title;
                   7575:         title = title.replace(/^LON-CAPA /,'');
                   7576:     }
                   7577:     if (!path) {
                   7578:         path = location.pathname;
                   7579:     }
                   7580:     Win = window.open('/adm/wishlist?mode=newLink&setTitle='+title+'&setPath='+path,
                   7581:                       'wishlistNewLink','width=560,height=350,scrollbars=0');
                   7582: }
                   7583: // END LON-CAPA Internal -->
                   7584: // ]]>
                   7585: </script>
                   7586: ENDWISHLIST
                   7587: }
                   7588: 
1.1030    www      7589: sub modal_window {
                   7590:     return(<<'ENDMODAL');
1.1046    raeburn  7591: <script type="text/javascript">
1.1030    www      7592: // <![CDATA[
                   7593: // <!-- BEGIN LON-CAPA Internal
                   7594: var modalWindow = {
                   7595: 	parent:"body",
                   7596: 	windowId:null,
                   7597: 	content:null,
                   7598: 	width:null,
                   7599: 	height:null,
                   7600: 	close:function()
                   7601: 	{
                   7602: 	        $(".LCmodal-window").remove();
                   7603: 	        $(".LCmodal-overlay").remove();
                   7604: 	},
                   7605: 	open:function()
                   7606: 	{
                   7607: 		var modal = "";
                   7608: 		modal += "<div class=\"LCmodal-overlay\"></div>";
                   7609: 		modal += "<div id=\"" + this.windowId + "\" class=\"LCmodal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
                   7610: 		modal += this.content;
                   7611: 		modal += "</div>";	
                   7612: 
                   7613: 		$(this.parent).append(modal);
                   7614: 
                   7615: 		$(".LCmodal-window").append("<a class=\"LCclose-window\"></a>");
                   7616: 		$(".LCclose-window").click(function(){modalWindow.close();});
                   7617: 		$(".LCmodal-overlay").click(function(){modalWindow.close();});
                   7618: 	}
                   7619: };
1.1031    www      7620: 	var openMyModal = function(source,width,height,scrolling)
1.1030    www      7621: 	{
                   7622: 		modalWindow.windowId = "myModal";
                   7623: 		modalWindow.width = width;
                   7624: 		modalWindow.height = height;
1.1031    www      7625: 		modalWindow.content = "<iframe width='"+width+"' height='"+height+"' frameborder='0' scrolling='"+scrolling+"' allowtransparency='true' src='" + source + "'>&lt/iframe>";
1.1030    www      7626: 		modalWindow.open();
                   7627: 	};	
                   7628: // END LON-CAPA Internal -->
                   7629: // ]]>
                   7630: </script>
                   7631: ENDMODAL
                   7632: }
                   7633: 
                   7634: sub modal_link {
1.1052    www      7635:     my ($link,$linktext,$width,$height,$target,$scrolling,$title)=@_;
1.1030    www      7636:     unless ($width) { $width=480; }
                   7637:     unless ($height) { $height=400; }
1.1031    www      7638:     unless ($scrolling) { $scrolling='yes'; }
1.1074    raeburn  7639:     my $target_attr;
                   7640:     if (defined($target)) {
                   7641:         $target_attr = 'target="'.$target.'"';
                   7642:     }
                   7643:     return <<"ENDLINK";
                   7644: <a href="$link" $target_attr title="$title" onclick="javascript:openMyModal('$link',$width,$height,'$scrolling'); return false;">
                   7645:            $linktext</a>
                   7646: ENDLINK
1.1030    www      7647: }
                   7648: 
1.1032    www      7649: sub modal_adhoc_script {
                   7650:     my ($funcname,$width,$height,$content)=@_;
                   7651:     return (<<ENDADHOC);
1.1046    raeburn  7652: <script type="text/javascript">
1.1032    www      7653: // <![CDATA[
                   7654:         var $funcname = function()
                   7655:         {
                   7656:                 modalWindow.windowId = "myModal";
                   7657:                 modalWindow.width = $width;
                   7658:                 modalWindow.height = $height;
                   7659:                 modalWindow.content = '$content';
                   7660:                 modalWindow.open();
                   7661:         };  
                   7662: // ]]>
                   7663: </script>
                   7664: ENDADHOC
                   7665: }
                   7666: 
1.1041    www      7667: sub modal_adhoc_inner {
                   7668:     my ($funcname,$width,$height,$content)=@_;
                   7669:     my $innerwidth=$width-20;
                   7670:     $content=&js_ready(
1.1042    www      7671:                &start_page('Dialog',undef,{'only_body'=>1,'bgcolor'=>'#FFFFFF'}).
1.1041    www      7672:                  &start_scrollbox($width.'px',$innerwidth.'px',$height.'px').
                   7673:                     $content.
                   7674:                  &end_scrollbox().
                   7675:                &end_page()
                   7676:              );
                   7677:     return &modal_adhoc_script($funcname,$width,$height,$content);
                   7678: }
                   7679: 
                   7680: sub modal_adhoc_window {
                   7681:     my ($funcname,$width,$height,$content,$linktext)=@_;
                   7682:     return &modal_adhoc_inner($funcname,$width,$height,$content).
                   7683:            "<a href=\"javascript:$funcname();void(0);\">".$linktext."</a>";
                   7684: }
                   7685: 
                   7686: sub modal_adhoc_launch {
                   7687:     my ($funcname,$width,$height,$content)=@_;
                   7688:     return &modal_adhoc_inner($funcname,$width,$height,$content).(<<ENDLAUNCH);
                   7689: <script type="text/javascript">
                   7690: // <![CDATA[
                   7691: $funcname();
                   7692: // ]]>
                   7693: </script>
                   7694: ENDLAUNCH
                   7695: }
                   7696: 
                   7697: sub modal_adhoc_close {
                   7698:     return (<<ENDCLOSE);
                   7699: <script type="text/javascript">
                   7700: // <![CDATA[
                   7701: modalWindow.close();
                   7702: // ]]>
                   7703: </script>
                   7704: ENDCLOSE
                   7705: }
                   7706: 
1.1038    www      7707: sub togglebox_script {
                   7708:    return(<<ENDTOGGLE);
                   7709: <script type="text/javascript"> 
                   7710: // <![CDATA[
                   7711: function LCtoggleDisplay(id,hidetext,showtext) {
                   7712:    link = document.getElementById(id + "link").childNodes[0];
                   7713:    with (document.getElementById(id).style) {
                   7714:       if (display == "none" ) {
                   7715:           display = "inline";
                   7716:           link.nodeValue = hidetext;
                   7717:         } else {
                   7718:           display = "none";
                   7719:           link.nodeValue = showtext;
                   7720:        }
                   7721:    }
                   7722: }
                   7723: // ]]>
                   7724: </script>
                   7725: ENDTOGGLE
                   7726: }
                   7727: 
1.1039    www      7728: sub start_togglebox {
                   7729:     my ($id,$heading,$headerbg,$hidetext,$showtext)=@_;
                   7730:     unless ($heading) { $heading=''; } else { $heading.=' '; }
                   7731:     unless ($showtext) { $showtext=&mt('show'); }
                   7732:     unless ($hidetext) { $hidetext=&mt('hide'); }
                   7733:     unless ($headerbg) { $headerbg='#FFFFFF'; }
                   7734:     return &start_data_table().
                   7735:            &start_data_table_header_row().
                   7736:            '<td bgcolor="'.$headerbg.'">'.$heading.
                   7737:            '[<a id="'.$id.'link" href="javascript:LCtoggleDisplay(\''.$id.'\',\''.$hidetext.'\',\''.
                   7738:            $showtext.'\')">'.$showtext.'</a>]</td>'.
                   7739:            &end_data_table_header_row().
                   7740:            '<tr id="'.$id.'" style="display:none""><td>';
                   7741: }
                   7742: 
                   7743: sub end_togglebox {
                   7744:     return '</td></tr>'.&end_data_table();
                   7745: }
                   7746: 
1.1041    www      7747: sub LCprogressbar_script {
1.1045    www      7748:    my ($id)=@_;
1.1041    www      7749:    return(<<ENDPROGRESS);
                   7750: <script type="text/javascript">
                   7751: // <![CDATA[
1.1045    www      7752: \$('#progressbar$id').progressbar({
1.1041    www      7753:   value: 0,
                   7754:   change: function(event, ui) {
                   7755:     var newVal = \$(this).progressbar('option', 'value');
                   7756:     \$('.pblabel', this).text(LCprogressTxt);
                   7757:   }
                   7758: });
                   7759: // ]]>
                   7760: </script>
                   7761: ENDPROGRESS
                   7762: }
                   7763: 
                   7764: sub LCprogressbarUpdate_script {
                   7765:    return(<<ENDPROGRESSUPDATE);
                   7766: <style type="text/css">
                   7767: .ui-progressbar { position:relative; }
                   7768: .pblabel { position: absolute; width: 100%; text-align: center; line-height: 1.9em; }
                   7769: </style>
                   7770: <script type="text/javascript">
                   7771: // <![CDATA[
1.1045    www      7772: var LCprogressTxt='---';
                   7773: 
                   7774: function LCupdateProgress(percent,progresstext,id) {
1.1041    www      7775:    LCprogressTxt=progresstext;
1.1045    www      7776:    \$('#progressbar'+id).progressbar('value',percent);
1.1041    www      7777: }
                   7778: // ]]>
                   7779: </script>
                   7780: ENDPROGRESSUPDATE
                   7781: }
                   7782: 
1.1042    www      7783: my $LClastpercent;
1.1045    www      7784: my $LCidcnt;
                   7785: my $LCcurrentid;
1.1042    www      7786: 
1.1041    www      7787: sub LCprogressbar {
1.1042    www      7788:     my ($r)=(@_);
                   7789:     $LClastpercent=0;
1.1045    www      7790:     $LCidcnt++;
                   7791:     $LCcurrentid=$$.'_'.$LCidcnt;
1.1041    www      7792:     my $starting=&mt('Starting');
                   7793:     my $content=(<<ENDPROGBAR);
1.1045    www      7794:   <div id="progressbar$LCcurrentid">
1.1041    www      7795:     <span class="pblabel">$starting</span>
                   7796:   </div>
                   7797: ENDPROGBAR
1.1045    www      7798:     &r_print($r,$content.&LCprogressbar_script($LCcurrentid));
1.1041    www      7799: }
                   7800: 
                   7801: sub LCprogressbarUpdate {
1.1042    www      7802:     my ($r,$val,$text)=@_;
                   7803:     unless ($val) { 
                   7804:        if ($LClastpercent) {
                   7805:            $val=$LClastpercent;
                   7806:        } else {
                   7807:            $val=0;
                   7808:        }
                   7809:     }
1.1041    www      7810:     if ($val<0) { $val=0; }
                   7811:     if ($val>100) { $val=0; }
1.1042    www      7812:     $LClastpercent=$val;
1.1041    www      7813:     unless ($text) { $text=$val.'%'; }
                   7814:     $text=&js_ready($text);
1.1044    www      7815:     &r_print($r,<<ENDUPDATE);
1.1041    www      7816: <script type="text/javascript">
                   7817: // <![CDATA[
1.1045    www      7818: LCupdateProgress($val,'$text','$LCcurrentid');
1.1041    www      7819: // ]]>
                   7820: </script>
                   7821: ENDUPDATE
1.1035    www      7822: }
                   7823: 
1.1042    www      7824: sub LCprogressbarClose {
                   7825:     my ($r)=@_;
                   7826:     $LClastpercent=0;
1.1044    www      7827:     &r_print($r,<<ENDCLOSE);
1.1042    www      7828: <script type="text/javascript">
                   7829: // <![CDATA[
1.1045    www      7830: \$("#progressbar$LCcurrentid").hide('slow'); 
1.1042    www      7831: // ]]>
                   7832: </script>
                   7833: ENDCLOSE
1.1044    www      7834: }
                   7835: 
                   7836: sub r_print {
                   7837:     my ($r,$to_print)=@_;
                   7838:     if ($r) {
                   7839:       $r->print($to_print);
                   7840:       $r->rflush();
                   7841:     } else {
                   7842:       print($to_print);
                   7843:     }
1.1042    www      7844: }
                   7845: 
1.320     albertel 7846: sub html_encode {
                   7847:     my ($result) = @_;
                   7848: 
1.322     albertel 7849:     $result = &HTML::Entities::encode($result,'<>&"');
1.320     albertel 7850:     
                   7851:     return $result;
                   7852: }
1.1044    www      7853: 
1.317     albertel 7854: sub js_ready {
                   7855:     my ($result) = @_;
                   7856: 
1.323     albertel 7857:     $result =~ s/[\n\r]/ /xmsg;
                   7858:     $result =~ s/\\/\\\\/xmsg;
                   7859:     $result =~ s/'/\\'/xmsg;
1.372     albertel 7860:     $result =~ s{</}{<\\/}xmsg;
1.317     albertel 7861:     
                   7862:     return $result;
                   7863: }
                   7864: 
1.315     albertel 7865: sub validate_page {
                   7866:     if (  exists($env{'internal.start_page'})
1.316     albertel 7867: 	  &&     $env{'internal.start_page'} > 1) {
                   7868: 	&Apache::lonnet::logthis('start_page called multiple times '.
1.318     albertel 7869: 				 $env{'internal.start_page'}.' '.
1.316     albertel 7870: 				 $ENV{'request.filename'});
1.315     albertel 7871:     }
                   7872:     if (  exists($env{'internal.end_page'})
1.316     albertel 7873: 	  &&     $env{'internal.end_page'} > 1) {
                   7874: 	&Apache::lonnet::logthis('end_page called multiple times '.
1.318     albertel 7875: 				 $env{'internal.end_page'}.' '.
1.316     albertel 7876: 				 $env{'request.filename'});
1.315     albertel 7877:     }
                   7878:     if (     exists($env{'internal.start_page'})
                   7879: 	&& ! exists($env{'internal.end_page'})) {
1.316     albertel 7880: 	&Apache::lonnet::logthis('start_page called without end_page '.
                   7881: 				 $env{'request.filename'});
1.315     albertel 7882:     }
                   7883:     if (   ! exists($env{'internal.start_page'})
                   7884: 	&&   exists($env{'internal.end_page'})) {
1.316     albertel 7885: 	&Apache::lonnet::logthis('end_page called without start_page'.
                   7886: 				 $env{'request.filename'});
1.315     albertel 7887:     }
1.306     albertel 7888: }
1.315     albertel 7889: 
1.996     www      7890: 
                   7891: sub start_scrollbox {
1.1075    raeburn  7892:     my ($outerwidth,$width,$height,$id,$bgcolor)=@_;
1.998     raeburn  7893:     unless ($outerwidth) { $outerwidth='520px'; }
                   7894:     unless ($width) { $width='500px'; }
                   7895:     unless ($height) { $height='200px'; }
1.1075    raeburn  7896:     my ($table_id,$div_id,$tdcol);
1.1018    raeburn  7897:     if ($id ne '') {
1.1020    raeburn  7898:         $table_id = " id='table_$id'";
                   7899:         $div_id = " id='div_$id'";
1.1018    raeburn  7900:     }
1.1075    raeburn  7901:     if ($bgcolor ne '') {
                   7902:         $tdcol = "background-color: $bgcolor;";
                   7903:     }
                   7904:     return <<"END";
                   7905: <table style="width: $outerwidth; border: 1px solid none;"$table_id><tr><td style="width: $width;$tdcol"><div style="overflow:auto; width:$width; height: $height;"$div_id>
                   7906: END
1.996     www      7907: }
                   7908: 
                   7909: sub end_scrollbox {
1.1036    www      7910:     return '</div></td></tr></table>';
1.996     www      7911: }
                   7912: 
1.318     albertel 7913: sub simple_error_page {
                   7914:     my ($r,$title,$msg) = @_;
                   7915:     my $page =
                   7916: 	&Apache::loncommon::start_page($title).
1.1075.2.15  raeburn  7917: 	'<p class="LC_error">'.&mt($msg).'</p>'.
1.318     albertel 7918: 	&Apache::loncommon::end_page();
                   7919:     if (ref($r)) {
                   7920: 	$r->print($page);
1.327     albertel 7921: 	return;
1.318     albertel 7922:     }
                   7923:     return $page;
                   7924: }
1.347     albertel 7925: 
                   7926: {
1.610     albertel 7927:     my @row_count;
1.961     onken    7928: 
                   7929:     sub start_data_table_count {
                   7930:         unshift(@row_count, 0);
                   7931:         return;
                   7932:     }
                   7933: 
                   7934:     sub end_data_table_count {
                   7935:         shift(@row_count);
                   7936:         return;
                   7937:     }
                   7938: 
1.347     albertel 7939:     sub start_data_table {
1.1018    raeburn  7940: 	my ($add_class,$id) = @_;
1.422     albertel 7941: 	my $css_class = (join(' ','LC_data_table',$add_class));
1.1018    raeburn  7942:         my $table_id;
                   7943:         if (defined($id)) {
                   7944:             $table_id = ' id="'.$id.'"';
                   7945:         }
1.961     onken    7946: 	&start_data_table_count();
1.1018    raeburn  7947: 	return '<table class="'.$css_class.'"'.$table_id.'>'."\n";
1.347     albertel 7948:     }
                   7949: 
                   7950:     sub end_data_table {
1.961     onken    7951: 	&end_data_table_count();
1.389     albertel 7952: 	return '</table>'."\n";;
1.347     albertel 7953:     }
                   7954: 
                   7955:     sub start_data_table_row {
1.974     wenzelju 7956: 	my ($add_class, $id) = @_;
1.610     albertel 7957: 	$row_count[0]++;
                   7958: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.900     bisitz   7959: 	$css_class = (join(' ',$css_class,$add_class)) unless ($add_class eq '');
1.974     wenzelju 7960:         $id = (' id="'.$id.'"') unless ($id eq '');
                   7961:         return  '<tr class="'.$css_class.'"'.$id.'>'."\n";
1.347     albertel 7962:     }
1.471     banghart 7963:     
                   7964:     sub continue_data_table_row {
1.974     wenzelju 7965: 	my ($add_class, $id) = @_;
1.610     albertel 7966: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.974     wenzelju 7967: 	$css_class = (join(' ',$css_class,$add_class)) unless ($add_class eq '');
                   7968:         $id = (' id="'.$id.'"') unless ($id eq '');
                   7969:         return  '<tr class="'.$css_class.'"'.$id.'>'."\n";
1.471     banghart 7970:     }
1.347     albertel 7971: 
                   7972:     sub end_data_table_row {
1.389     albertel 7973: 	return '</tr>'."\n";;
1.347     albertel 7974:     }
1.367     www      7975: 
1.421     albertel 7976:     sub start_data_table_empty_row {
1.707     bisitz   7977: #	$row_count[0]++;
1.421     albertel 7978: 	return  '<tr class="LC_empty_row" >'."\n";;
                   7979:     }
                   7980: 
                   7981:     sub end_data_table_empty_row {
                   7982: 	return '</tr>'."\n";;
                   7983:     }
                   7984: 
1.367     www      7985:     sub start_data_table_header_row {
1.389     albertel 7986: 	return  '<tr class="LC_header_row">'."\n";;
1.367     www      7987:     }
                   7988: 
                   7989:     sub end_data_table_header_row {
1.389     albertel 7990: 	return '</tr>'."\n";;
1.367     www      7991:     }
1.890     droeschl 7992: 
                   7993:     sub data_table_caption {
                   7994:         my $caption = shift;
                   7995:         return "<caption class=\"LC_caption\">$caption</caption>";
                   7996:     }
1.347     albertel 7997: }
                   7998: 
1.548     albertel 7999: =pod
                   8000: 
                   8001: =item * &inhibit_menu_check($arg)
                   8002: 
                   8003: Checks for a inhibitmenu state and generates output to preserve it
                   8004: 
                   8005: Inputs:         $arg - can be any of
                   8006:                      - undef - in which case the return value is a string 
                   8007:                                to add  into arguments list of a uri
                   8008:                      - 'input' - in which case the return value is a HTML
                   8009:                                  <form> <input> field of type hidden to
                   8010:                                  preserve the value
                   8011:                      - a url - in which case the return value is the url with
                   8012:                                the neccesary cgi args added to preserve the
                   8013:                                inhibitmenu state
                   8014:                      - a ref to a url - no return value, but the string is
                   8015:                                         updated to include the neccessary cgi
                   8016:                                         args to preserve the inhibitmenu state
                   8017: 
                   8018: =cut
                   8019: 
                   8020: sub inhibit_menu_check {
                   8021:     my ($arg) = @_;
                   8022:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
                   8023:     if ($arg eq 'input') {
                   8024: 	if ($env{'form.inhibitmenu'}) {
                   8025: 	    return '<input type="hidden" name="inhibitmenu" value="'.$env{'form.inhibitmenu'}.'" />';
                   8026: 	} else {
                   8027: 	    return
                   8028: 	}
                   8029:     }
                   8030:     if ($env{'form.inhibitmenu'}) {
                   8031: 	if (ref($arg)) {
                   8032: 	    $$arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   8033: 	} elsif ($arg eq '') {
                   8034: 	    $arg .= 'inhibitmenu='.$env{'form.inhibitmenu'};
                   8035: 	} else {
                   8036: 	    $arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   8037: 	}
                   8038:     }
                   8039:     if (!ref($arg)) {
                   8040: 	return $arg;
                   8041:     }
                   8042: }
                   8043: 
1.251     albertel 8044: ###############################################
1.182     matthew  8045: 
                   8046: =pod
                   8047: 
1.549     albertel 8048: =back
                   8049: 
                   8050: =head1 User Information Routines
                   8051: 
                   8052: =over 4
                   8053: 
1.405     albertel 8054: =item * &get_users_function()
1.182     matthew  8055: 
                   8056: Used by &bodytag to determine the current users primary role.
                   8057: Returns either 'student','coordinator','admin', or 'author'.
                   8058: 
                   8059: =cut
                   8060: 
                   8061: ###############################################
                   8062: sub get_users_function {
1.815     tempelho 8063:     my $function = 'norole';
1.818     tempelho 8064:     if ($env{'request.role'}=~/^(st)/) {
                   8065:         $function='student';
                   8066:     }
1.907     raeburn  8067:     if ($env{'request.role'}=~/^(cc|co|in|ta|ep)/) {
1.182     matthew  8068:         $function='coordinator';
                   8069:     }
1.258     albertel 8070:     if ($env{'request.role'}=~/^(su|dc|ad|li)/) {
1.182     matthew  8071:         $function='admin';
                   8072:     }
1.826     bisitz   8073:     if (($env{'request.role'}=~/^(au|ca|aa)/) ||
1.1025    raeburn  8074:         ($ENV{'REQUEST_URI'}=~ m{/^(/priv)})) {
1.182     matthew  8075:         $function='author';
                   8076:     }
                   8077:     return $function;
1.54      www      8078: }
1.99      www      8079: 
                   8080: ###############################################
                   8081: 
1.233     raeburn  8082: =pod
                   8083: 
1.821     raeburn  8084: =item * &show_course()
                   8085: 
                   8086: Used by lonmenu.pm and lonroles.pm to determine whether to use the word
                   8087: 'Courses' or 'Roles' in inline navigation and on screen displaying user's roles.
                   8088: 
                   8089: Inputs:
                   8090: None
                   8091: 
                   8092: Outputs:
                   8093: Scalar: 1 if 'Course' to be used, 0 otherwise.
                   8094: 
                   8095: =cut
                   8096: 
                   8097: ###############################################
                   8098: sub show_course {
                   8099:     my $course = !$env{'user.adv'};
                   8100:     if (!$env{'user.adv'}) {
                   8101:         foreach my $env (keys(%env)) {
                   8102:             next if ($env !~ m/^user\.priv\./);
                   8103:             if ($env !~ m/^user\.priv\.(?:st|cm)/) {
                   8104:                 $course = 0;
                   8105:                 last;
                   8106:             }
                   8107:         }
                   8108:     }
                   8109:     return $course;
                   8110: }
                   8111: 
                   8112: ###############################################
                   8113: 
                   8114: =pod
                   8115: 
1.542     raeburn  8116: =item * &check_user_status()
1.274     raeburn  8117: 
                   8118: Determines current status of supplied role for a
                   8119: specific user. Roles can be active, previous or future.
                   8120: 
                   8121: Inputs: 
                   8122: user's domain, user's username, course's domain,
1.375     raeburn  8123: course's number, optional section ID.
1.274     raeburn  8124: 
                   8125: Outputs:
                   8126: role status: active, previous or future. 
                   8127: 
                   8128: =cut
                   8129: 
                   8130: sub check_user_status {
1.412     raeburn  8131:     my ($udom,$uname,$cdom,$crs,$role,$sec) = @_;
1.1073    raeburn  8132:     my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
1.274     raeburn  8133:     my @uroles = keys %userinfo;
                   8134:     my $srchstr;
                   8135:     my $active_chk = 'none';
1.412     raeburn  8136:     my $now = time;
1.274     raeburn  8137:     if (@uroles > 0) {
1.908     raeburn  8138:         if (($role eq 'cc') || ($role eq 'co') || ($sec eq '') || (!defined($sec))) {
1.274     raeburn  8139:             $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
                   8140:         } else {
1.412     raeburn  8141:             $srchstr = '/'.$cdom.'/'.$crs.'/'.$sec.'_'.$role;
                   8142:         }
                   8143:         if (grep/^\Q$srchstr\E$/,@uroles) {
1.274     raeburn  8144:             my $role_end = 0;
                   8145:             my $role_start = 0;
                   8146:             $active_chk = 'active';
1.412     raeburn  8147:             if ($userinfo{$srchstr} =~ m/^\Q$role\E_(\d+)/) {
                   8148:                 $role_end = $1;
                   8149:                 if ($userinfo{$srchstr} =~ m/^\Q$role\E_\Q$role_end\E_(\d+)$/) {
                   8150:                     $role_start = $1;
1.274     raeburn  8151:                 }
                   8152:             }
                   8153:             if ($role_start > 0) {
1.412     raeburn  8154:                 if ($now < $role_start) {
1.274     raeburn  8155:                     $active_chk = 'future';
                   8156:                 }
                   8157:             }
                   8158:             if ($role_end > 0) {
1.412     raeburn  8159:                 if ($now > $role_end) {
1.274     raeburn  8160:                     $active_chk = 'previous';
                   8161:                 }
                   8162:             }
                   8163:         }
                   8164:     }
                   8165:     return $active_chk;
                   8166: }
                   8167: 
                   8168: ###############################################
                   8169: 
                   8170: =pod
                   8171: 
1.405     albertel 8172: =item * &get_sections()
1.233     raeburn  8173: 
                   8174: Determines all the sections for a course including
                   8175: sections with students and sections containing other roles.
1.419     raeburn  8176: Incoming parameters: 
                   8177: 
                   8178: 1. domain
                   8179: 2. course number 
                   8180: 3. reference to array containing roles for which sections should 
                   8181: be gathered (optional).
                   8182: 4. reference to array containing status types for which sections 
                   8183: should be gathered (optional).
                   8184: 
                   8185: If the third argument is undefined, sections are gathered for any role. 
                   8186: If the fourth argument is undefined, sections are gathered for any status.
                   8187: Permissible values are 'active' or 'future' or 'previous'.
1.233     raeburn  8188:  
1.374     raeburn  8189: Returns section hash (keys are section IDs, values are
                   8190: number of users in each section), subject to the
1.419     raeburn  8191: optional roles filter, optional status filter 
1.233     raeburn  8192: 
                   8193: =cut
                   8194: 
                   8195: ###############################################
                   8196: sub get_sections {
1.419     raeburn  8197:     my ($cdom,$cnum,$possible_roles,$possible_status) = @_;
1.366     albertel 8198:     if (!defined($cdom) || !defined($cnum)) {
                   8199:         my $cid =  $env{'request.course.id'};
                   8200: 
                   8201: 	return if (!defined($cid));
                   8202: 
                   8203:         $cdom = $env{'course.'.$cid.'.domain'};
                   8204:         $cnum = $env{'course.'.$cid.'.num'};
                   8205:     }
                   8206: 
                   8207:     my %sectioncount;
1.419     raeburn  8208:     my $now = time;
1.240     albertel 8209: 
1.1075.2.33  raeburn  8210:     my $check_students = 1;
                   8211:     my $only_students = 0;
                   8212:     if (ref($possible_roles) eq 'ARRAY') {
                   8213:         if (grep(/^st$/,@{$possible_roles})) {
                   8214:             if (@{$possible_roles} == 1) {
                   8215:                 $only_students = 1;
                   8216:             }
                   8217:         } else {
                   8218:             $check_students = 0;
                   8219:         }
                   8220:     }
                   8221: 
                   8222:     if ($check_students) {
1.276     albertel 8223: 	my ($classlist) = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1.240     albertel 8224: 	my $sec_index = &Apache::loncoursedata::CL_SECTION();
                   8225: 	my $status_index = &Apache::loncoursedata::CL_STATUS();
1.419     raeburn  8226:         my $start_index = &Apache::loncoursedata::CL_START();
                   8227:         my $end_index = &Apache::loncoursedata::CL_END();
                   8228:         my $status;
1.366     albertel 8229: 	while (my ($student,$data) = each(%$classlist)) {
1.419     raeburn  8230: 	    my ($section,$stu_status,$start,$end) = ($data->[$sec_index],
                   8231: 				                     $data->[$status_index],
                   8232:                                                      $data->[$start_index],
                   8233:                                                      $data->[$end_index]);
                   8234:             if ($stu_status eq 'Active') {
                   8235:                 $status = 'active';
                   8236:             } elsif ($end < $now) {
                   8237:                 $status = 'previous';
                   8238:             } elsif ($start > $now) {
                   8239:                 $status = 'future';
                   8240:             } 
                   8241: 	    if ($section ne '-1' && $section !~ /^\s*$/) {
                   8242:                 if ((!defined($possible_status)) || (($status ne '') && 
                   8243:                     (grep/^\Q$status\E$/,@{$possible_status}))) { 
                   8244: 		    $sectioncount{$section}++;
                   8245:                 }
1.240     albertel 8246: 	    }
                   8247: 	}
                   8248:     }
1.1075.2.33  raeburn  8249:     if ($only_students) {
                   8250:         return %sectioncount;
                   8251:     }
1.240     albertel 8252:     my %courseroles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   8253:     foreach my $user (sort(keys(%courseroles))) {
                   8254: 	if ($user !~ /^(\w{2})/) { next; }
                   8255: 	my ($role) = ($user =~ /^(\w{2})/);
                   8256: 	if ($possible_roles && !(grep(/^$role$/,@$possible_roles))) { next; }
1.419     raeburn  8257: 	my ($section,$status);
1.240     albertel 8258: 	if ($role eq 'cr' &&
                   8259: 	    $user =~ m-^$role/[^/]*/[^/]*/[^/]*:[^:]*:[^:]*:(\w+)-) {
                   8260: 	    $section=$1;
                   8261: 	}
                   8262: 	if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
                   8263: 	if (!defined($section) || $section eq '-1') { next; }
1.419     raeburn  8264:         my ($end,$start) = ($courseroles{$user} =~ /^([^:]*):([^:]*)$/);
                   8265:         if ($end == -1 && $start == -1) {
                   8266:             next; #deleted role
                   8267:         }
                   8268:         if (!defined($possible_status)) { 
                   8269:             $sectioncount{$section}++;
                   8270:         } else {
                   8271:             if ((!$end || $end >= $now) && (!$start || $start <= $now)) {
                   8272:                 $status = 'active';
                   8273:             } elsif ($end < $now) {
                   8274:                 $status = 'future';
                   8275:             } elsif ($start > $now) {
                   8276:                 $status = 'previous';
                   8277:             }
                   8278:             if (($status ne '') && (grep/^\Q$status\E$/,@{$possible_status})) {
                   8279:                 $sectioncount{$section}++;
                   8280:             }
                   8281:         }
1.233     raeburn  8282:     }
1.366     albertel 8283:     return %sectioncount;
1.233     raeburn  8284: }
                   8285: 
1.274     raeburn  8286: ###############################################
1.294     raeburn  8287: 
                   8288: =pod
1.405     albertel 8289: 
                   8290: =item * &get_course_users()
                   8291: 
1.275     raeburn  8292: Retrieves usernames:domains for users in the specified course
                   8293: with specific role(s), and access status. 
                   8294: 
                   8295: Incoming parameters:
1.277     albertel 8296: 1. course domain
                   8297: 2. course number
                   8298: 3. access status: users must have - either active, 
1.275     raeburn  8299: previous, future, or all.
1.277     albertel 8300: 4. reference to array of permissible roles
1.288     raeburn  8301: 5. reference to array of section restrictions (optional)
                   8302: 6. reference to results object (hash of hashes).
                   8303: 7. reference to optional userdata hash
1.609     raeburn  8304: 8. reference to optional statushash
1.630     raeburn  8305: 9. flag if privileged users (except those set to unhide in
                   8306:    course settings) should be excluded    
1.609     raeburn  8307: Keys of top level results hash are roles.
1.275     raeburn  8308: Keys of inner hashes are username:domain, with 
                   8309: values set to access type.
1.288     raeburn  8310: Optional userdata hash returns an array with arguments in the 
                   8311: same order as loncoursedata::get_classlist() for student data.
                   8312: 
1.609     raeburn  8313: Optional statushash returns
                   8314: 
1.288     raeburn  8315: Entries for end, start, section and status are blank because
                   8316: of the possibility of multiple values for non-student roles.
                   8317: 
1.275     raeburn  8318: =cut
1.405     albertel 8319: 
1.275     raeburn  8320: ###############################################
1.405     albertel 8321: 
1.275     raeburn  8322: sub get_course_users {
1.630     raeburn  8323:     my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata,$statushash,$hidepriv) = @_;
1.288     raeburn  8324:     my %idx = ();
1.419     raeburn  8325:     my %seclists;
1.288     raeburn  8326: 
                   8327:     $idx{udom} = &Apache::loncoursedata::CL_SDOM();
                   8328:     $idx{uname} =  &Apache::loncoursedata::CL_SNAME();
                   8329:     $idx{end} = &Apache::loncoursedata::CL_END();
                   8330:     $idx{start} = &Apache::loncoursedata::CL_START();
                   8331:     $idx{id} = &Apache::loncoursedata::CL_ID();
                   8332:     $idx{section} = &Apache::loncoursedata::CL_SECTION();
                   8333:     $idx{fullname} = &Apache::loncoursedata::CL_FULLNAME();
                   8334:     $idx{status} = &Apache::loncoursedata::CL_STATUS();
                   8335: 
1.290     albertel 8336:     if (grep(/^st$/,@{$roles})) {
1.276     albertel 8337:         my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist($cdom,$cnum);
1.278     raeburn  8338:         my $now = time;
1.277     albertel 8339:         foreach my $student (keys(%{$classlist})) {
1.288     raeburn  8340:             my $match = 0;
1.412     raeburn  8341:             my $secmatch = 0;
1.419     raeburn  8342:             my $section = $$classlist{$student}[$idx{section}];
1.609     raeburn  8343:             my $status = $$classlist{$student}[$idx{status}];
1.419     raeburn  8344:             if ($section eq '') {
                   8345:                 $section = 'none';
                   8346:             }
1.291     albertel 8347:             if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 8348:                 if (grep(/^all$/,@{$sections})) {
1.412     raeburn  8349:                     $secmatch = 1;
                   8350:                 } elsif ($$classlist{$student}[$idx{section}] eq '') {
1.420     albertel 8351:                     if (grep(/^none$/,@{$sections})) {
1.412     raeburn  8352:                         $secmatch = 1;
                   8353:                     }
                   8354:                 } else {  
1.419     raeburn  8355: 		    if (grep(/^\Q$section\E$/,@{$sections})) {
1.412     raeburn  8356: 		        $secmatch = 1;
                   8357:                     }
1.290     albertel 8358: 		}
1.412     raeburn  8359:                 if (!$secmatch) {
                   8360:                     next;
                   8361:                 }
1.419     raeburn  8362:             }
1.275     raeburn  8363:             if (defined($$types{'active'})) {
1.288     raeburn  8364:                 if ($$classlist{$student}[$idx{status}] eq 'Active') {
1.275     raeburn  8365:                     push(@{$$users{st}{$student}},'active');
1.288     raeburn  8366:                     $match = 1;
1.275     raeburn  8367:                 }
                   8368:             }
                   8369:             if (defined($$types{'previous'})) {
1.609     raeburn  8370:                 if ($$classlist{$student}[$idx{status}] eq 'Expired') {
1.275     raeburn  8371:                     push(@{$$users{st}{$student}},'previous');
1.288     raeburn  8372:                     $match = 1;
1.275     raeburn  8373:                 }
                   8374:             }
                   8375:             if (defined($$types{'future'})) {
1.609     raeburn  8376:                 if ($$classlist{$student}[$idx{status}] eq 'Future') {
1.275     raeburn  8377:                     push(@{$$users{st}{$student}},'future');
1.288     raeburn  8378:                     $match = 1;
1.275     raeburn  8379:                 }
                   8380:             }
1.609     raeburn  8381:             if ($match) {
                   8382:                 push(@{$seclists{$student}},$section);
                   8383:                 if (ref($userdata) eq 'HASH') {
                   8384:                     $$userdata{$student} = $$classlist{$student};
                   8385:                 }
                   8386:                 if (ref($statushash) eq 'HASH') {
                   8387:                     $statushash->{$student}{'st'}{$section} = $status;
                   8388:                 }
1.288     raeburn  8389:             }
1.275     raeburn  8390:         }
                   8391:     }
1.412     raeburn  8392:     if ((@{$roles} > 1) || ((@{$roles} == 1) && ($$roles[0] ne "st"))) {
1.439     raeburn  8393:         my %coursepersonnel = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   8394:         my $now = time;
1.609     raeburn  8395:         my %displaystatus = ( previous => 'Expired',
                   8396:                               active   => 'Active',
                   8397:                               future   => 'Future',
                   8398:                             );
1.630     raeburn  8399:         my %nothide;
                   8400:         if ($hidepriv) {
                   8401:             my %coursehash=&Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                   8402:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   8403:                 if ($user !~ /:/) {
                   8404:                     $nothide{join(':',split(/[\@]/,$user))}=1;
                   8405:                 } else {
                   8406:                     $nothide{$user} = 1;
                   8407:                 }
                   8408:             }
                   8409:         }
1.439     raeburn  8410:         foreach my $person (sort(keys(%coursepersonnel))) {
1.288     raeburn  8411:             my $match = 0;
1.412     raeburn  8412:             my $secmatch = 0;
1.439     raeburn  8413:             my $status;
1.412     raeburn  8414:             my ($role,$user,$usec) = ($person =~ /^([^:]*):([^:]+:[^:]+):([^:]*)/);
1.275     raeburn  8415:             $user =~ s/:$//;
1.439     raeburn  8416:             my ($end,$start) = split(/:/,$coursepersonnel{$person});
                   8417:             if ($end == -1 || $start == -1) {
                   8418:                 next;
                   8419:             }
                   8420:             if (($role) && ((grep(/^\Q$role\E$/,@{$roles})) ||
                   8421:                 (grep(/^cr$/,@{$roles}) && $role =~ /^cr\//))) {
1.412     raeburn  8422:                 my ($uname,$udom) = split(/:/,$user);
                   8423:                 if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 8424:                     if (grep(/^all$/,@{$sections})) {
1.412     raeburn  8425:                         $secmatch = 1;
                   8426:                     } elsif ($usec eq '') {
1.420     albertel 8427:                         if (grep(/^none$/,@{$sections})) {
1.412     raeburn  8428:                             $secmatch = 1;
                   8429:                         }
                   8430:                     } else {
                   8431:                         if (grep(/^\Q$usec\E$/,@{$sections})) {
                   8432:                             $secmatch = 1;
                   8433:                         }
                   8434:                     }
                   8435:                     if (!$secmatch) {
                   8436:                         next;
                   8437:                     }
1.288     raeburn  8438:                 }
1.419     raeburn  8439:                 if ($usec eq '') {
                   8440:                     $usec = 'none';
                   8441:                 }
1.275     raeburn  8442:                 if ($uname ne '' && $udom ne '') {
1.630     raeburn  8443:                     if ($hidepriv) {
                   8444:                         if ((&Apache::lonnet::privileged($uname,$udom)) &&
                   8445:                             (!$nothide{$uname.':'.$udom})) {
                   8446:                             next;
                   8447:                         }
                   8448:                     }
1.503     raeburn  8449:                     if ($end > 0 && $end < $now) {
1.439     raeburn  8450:                         $status = 'previous';
                   8451:                     } elsif ($start > $now) {
                   8452:                         $status = 'future';
                   8453:                     } else {
                   8454:                         $status = 'active';
                   8455:                     }
1.277     albertel 8456:                     foreach my $type (keys(%{$types})) { 
1.275     raeburn  8457:                         if ($status eq $type) {
1.420     albertel 8458:                             if (!grep(/^\Q$type\E$/,@{$$users{$role}{$user}})) {
1.419     raeburn  8459:                                 push(@{$$users{$role}{$user}},$type);
                   8460:                             }
1.288     raeburn  8461:                             $match = 1;
                   8462:                         }
                   8463:                     }
1.419     raeburn  8464:                     if (($match) && (ref($userdata) eq 'HASH')) {
                   8465:                         if (!exists($$userdata{$uname.':'.$udom})) {
                   8466: 			    &get_user_info($udom,$uname,\%idx,$userdata);
                   8467:                         }
1.420     albertel 8468:                         if (!grep(/^\Q$usec\E$/,@{$seclists{$uname.':'.$udom}})) {
1.419     raeburn  8469:                             push(@{$seclists{$uname.':'.$udom}},$usec);
                   8470:                         }
1.609     raeburn  8471:                         if (ref($statushash) eq 'HASH') {
                   8472:                             $statushash->{$uname.':'.$udom}{$role}{$usec} = $displaystatus{$status};
                   8473:                         }
1.275     raeburn  8474:                     }
                   8475:                 }
                   8476:             }
                   8477:         }
1.290     albertel 8478:         if (grep(/^ow$/,@{$roles})) {
1.279     raeburn  8479:             if ((defined($cdom)) && (defined($cnum))) {
                   8480:                 my %csettings = &Apache::lonnet::get('environment',['internal.courseowner'],$cdom,$cnum);
                   8481:                 if ( defined($csettings{'internal.courseowner'}) ) {
                   8482:                     my $owner = $csettings{'internal.courseowner'};
1.609     raeburn  8483:                     next if ($owner eq '');
                   8484:                     my ($ownername,$ownerdom);
                   8485:                     if ($owner =~ /^([^:]+):([^:]+)$/) {
                   8486:                         $ownername = $1;
                   8487:                         $ownerdom = $2;
                   8488:                     } else {
                   8489:                         $ownername = $owner;
                   8490:                         $ownerdom = $cdom;
                   8491:                         $owner = $ownername.':'.$ownerdom;
1.439     raeburn  8492:                     }
                   8493:                     @{$$users{'ow'}{$owner}} = 'any';
1.290     albertel 8494:                     if (defined($userdata) && 
1.609     raeburn  8495: 			!exists($$userdata{$owner})) {
                   8496: 			&get_user_info($ownerdom,$ownername,\%idx,$userdata);
                   8497:                         if (!grep(/^none$/,@{$seclists{$owner}})) {
                   8498:                             push(@{$seclists{$owner}},'none');
                   8499:                         }
                   8500:                         if (ref($statushash) eq 'HASH') {
                   8501:                             $statushash->{$owner}{'ow'}{'none'} = 'Any';
1.419     raeburn  8502:                         }
1.290     albertel 8503: 		    }
1.279     raeburn  8504:                 }
                   8505:             }
                   8506:         }
1.419     raeburn  8507:         foreach my $user (keys(%seclists)) {
                   8508:             @{$seclists{$user}} = (sort {$a <=> $b} @{$seclists{$user}});
                   8509:             $$userdata{$user}[$idx{section}] = join(',',@{$seclists{$user}});
                   8510:         }
1.275     raeburn  8511:     }
                   8512:     return;
                   8513: }
                   8514: 
1.288     raeburn  8515: sub get_user_info {
                   8516:     my ($udom,$uname,$idx,$userdata) = @_;
1.289     albertel 8517:     $$userdata{$uname.':'.$udom}[$$idx{fullname}] = 
                   8518: 	&plainname($uname,$udom,'lastname');
1.291     albertel 8519:     $$userdata{$uname.':'.$udom}[$$idx{uname}] = $uname;
1.297     raeburn  8520:     $$userdata{$uname.':'.$udom}[$$idx{udom}] = $udom;
1.609     raeburn  8521:     my %idhash =  &Apache::lonnet::idrget($udom,($uname));
                   8522:     $$userdata{$uname.':'.$udom}[$$idx{id}] = $idhash{$uname}; 
1.288     raeburn  8523:     return;
                   8524: }
1.275     raeburn  8525: 
1.472     raeburn  8526: ###############################################
                   8527: 
                   8528: =pod
                   8529: 
                   8530: =item * &get_user_quota()
                   8531: 
                   8532: Retrieves quota assigned for storage of portfolio files for a user  
                   8533: 
                   8534: Incoming parameters:
                   8535: 1. user's username
                   8536: 2. user's domain
                   8537: 
                   8538: Returns:
1.536     raeburn  8539: 1. Disk quota (in Mb) assigned to student.
                   8540: 2. (Optional) Type of setting: custom or default
                   8541:    (individually assigned or default for user's 
                   8542:    institutional status).
                   8543: 3. (Optional) - User's institutional status (e.g., faculty, staff
                   8544:    or student - types as defined in localenroll::inst_usertypes 
                   8545:    for user's domain, which determines default quota for user.
                   8546: 4. (Optional) - Default quota which would apply to the user.
1.472     raeburn  8547: 
                   8548: If a value has been stored in the user's environment, 
1.536     raeburn  8549: it will return that, otherwise it returns the maximal default
                   8550: defined for the user's instituional status(es) in the domain.
1.472     raeburn  8551: 
                   8552: =cut
                   8553: 
                   8554: ###############################################
                   8555: 
                   8556: 
                   8557: sub get_user_quota {
                   8558:     my ($uname,$udom) = @_;
1.536     raeburn  8559:     my ($quota,$quotatype,$settingstatus,$defquota);
1.472     raeburn  8560:     if (!defined($udom)) {
                   8561:         $udom = $env{'user.domain'};
                   8562:     }
                   8563:     if (!defined($uname)) {
                   8564:         $uname = $env{'user.name'};
                   8565:     }
                   8566:     if (($udom eq '' || $uname eq '') ||
                   8567:         ($udom eq 'public') && ($uname eq 'public')) {
                   8568:         $quota = 0;
1.536     raeburn  8569:         $quotatype = 'default';
                   8570:         $defquota = 0; 
1.472     raeburn  8571:     } else {
1.536     raeburn  8572:         my $inststatus;
1.472     raeburn  8573:         if ($udom eq $env{'user.domain'} && $uname eq $env{'user.name'}) {
                   8574:             $quota = $env{'environment.portfolioquota'};
1.536     raeburn  8575:             $inststatus = $env{'environment.inststatus'};
1.472     raeburn  8576:         } else {
1.536     raeburn  8577:             my %userenv = 
                   8578:                 &Apache::lonnet::get('environment',['portfolioquota',
                   8579:                                      'inststatus'],$udom,$uname);
1.472     raeburn  8580:             my ($tmp) = keys(%userenv);
                   8581:             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   8582:                 $quota = $userenv{'portfolioquota'};
1.536     raeburn  8583:                 $inststatus = $userenv{'inststatus'};
1.472     raeburn  8584:             } else {
                   8585:                 undef(%userenv);
                   8586:             }
                   8587:         }
1.536     raeburn  8588:         ($defquota,$settingstatus) = &default_quota($udom,$inststatus);
1.472     raeburn  8589:         if ($quota eq '') {
1.536     raeburn  8590:             $quota = $defquota;
                   8591:             $quotatype = 'default';
                   8592:         } else {
                   8593:             $quotatype = 'custom';
1.472     raeburn  8594:         }
                   8595:     }
1.536     raeburn  8596:     if (wantarray) {
                   8597:         return ($quota,$quotatype,$settingstatus,$defquota);
                   8598:     } else {
                   8599:         return $quota;
                   8600:     }
1.472     raeburn  8601: }
                   8602: 
                   8603: ###############################################
                   8604: 
                   8605: =pod
                   8606: 
                   8607: =item * &default_quota()
                   8608: 
1.536     raeburn  8609: Retrieves default quota assigned for storage of user portfolio files,
                   8610: given an (optional) user's institutional status.
1.472     raeburn  8611: 
                   8612: Incoming parameters:
                   8613: 1. domain
1.536     raeburn  8614: 2. (Optional) institutional status(es).  This is a : separated list of 
                   8615:    status types (e.g., faculty, staff, student etc.)
                   8616:    which apply to the user for whom the default is being retrieved.
                   8617:    If the institutional status string in undefined, the domain
                   8618:    default quota will be returned. 
1.472     raeburn  8619: 
                   8620: Returns:
                   8621: 1. Default disk quota (in Mb) for user portfolios in the domain.
1.536     raeburn  8622: 2. (Optional) institutional type which determined the value of the
                   8623:    default quota.
1.472     raeburn  8624: 
                   8625: If a value has been stored in the domain's configuration db,
                   8626: it will return that, otherwise it returns 20 (for backwards 
                   8627: compatibility with domains which have not set up a configuration
                   8628: db file; the original statically defined portfolio quota was 20 Mb). 
                   8629: 
1.536     raeburn  8630: If the user's status includes multiple types (e.g., staff and student),
                   8631: the largest default quota which applies to the user determines the
                   8632: default quota returned.
                   8633: 
1.780     raeburn  8634: =back
                   8635: 
1.472     raeburn  8636: =cut
                   8637: 
                   8638: ###############################################
                   8639: 
                   8640: 
                   8641: sub default_quota {
1.536     raeburn  8642:     my ($udom,$inststatus) = @_;
                   8643:     my ($defquota,$settingstatus);
                   8644:     my %quotahash = &Apache::lonnet::get_dom('configuration',
1.622     raeburn  8645:                                             ['quotas'],$udom);
                   8646:     if (ref($quotahash{'quotas'}) eq 'HASH') {
1.536     raeburn  8647:         if ($inststatus ne '') {
1.765     raeburn  8648:             my @statuses = map { &unescape($_); } split(/:/,$inststatus);
1.536     raeburn  8649:             foreach my $item (@statuses) {
1.711     raeburn  8650:                 if (ref($quotahash{'quotas'}{'defaultquota'}) eq 'HASH') {
                   8651:                     if ($quotahash{'quotas'}{'defaultquota'}{$item} ne '') {
                   8652:                         if ($defquota eq '') {
                   8653:                             $defquota = $quotahash{'quotas'}{'defaultquota'}{$item};
                   8654:                             $settingstatus = $item;
                   8655:                         } elsif ($quotahash{'quotas'}{'defaultquota'}{$item} > $defquota) {
                   8656:                             $defquota = $quotahash{'quotas'}{'defaultquota'}{$item};
                   8657:                             $settingstatus = $item;
                   8658:                         }
                   8659:                     }
                   8660:                 } else {
                   8661:                     if ($quotahash{'quotas'}{$item} ne '') {
                   8662:                         if ($defquota eq '') {
                   8663:                             $defquota = $quotahash{'quotas'}{$item};
                   8664:                             $settingstatus = $item;
                   8665:                         } elsif ($quotahash{'quotas'}{$item} > $defquota) {
                   8666:                             $defquota = $quotahash{'quotas'}{$item};
                   8667:                             $settingstatus = $item;
                   8668:                         }
1.536     raeburn  8669:                     }
                   8670:                 }
                   8671:             }
                   8672:         }
                   8673:         if ($defquota eq '') {
1.711     raeburn  8674:             if (ref($quotahash{'quotas'}{'defaultquota'}) eq 'HASH') {
                   8675:                 $defquota = $quotahash{'quotas'}{'defaultquota'}{'default'};
                   8676:             } else {
                   8677:                 $defquota = $quotahash{'quotas'}{'default'};
                   8678:             }
1.536     raeburn  8679:             $settingstatus = 'default';
                   8680:         }
                   8681:     } else {
                   8682:         $settingstatus = 'default';
                   8683:         $defquota = 20;
                   8684:     }
                   8685:     if (wantarray) {
                   8686:         return ($defquota,$settingstatus);
1.472     raeburn  8687:     } else {
1.536     raeburn  8688:         return $defquota;
1.472     raeburn  8689:     }
                   8690: }
                   8691: 
1.384     raeburn  8692: sub get_secgrprole_info {
                   8693:     my ($cdom,$cnum,$needroles,$type)  = @_;
                   8694:     my %sections_count = &get_sections($cdom,$cnum);
                   8695:     my @sections =  (sort {$a <=> $b} keys(%sections_count));
                   8696:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
                   8697:     my @groups = sort(keys(%curr_groups));
                   8698:     my $allroles = [];
                   8699:     my $rolehash;
                   8700:     my $accesshash = {
                   8701:                      active => 'Currently has access',
                   8702:                      future => 'Will have future access',
                   8703:                      previous => 'Previously had access',
                   8704:                   };
                   8705:     if ($needroles) {
                   8706:         $rolehash = {'all' => 'all'};
1.385     albertel 8707:         my %user_roles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   8708: 	if (&Apache::lonnet::error(%user_roles)) {
                   8709: 	    undef(%user_roles);
                   8710: 	}
                   8711:         foreach my $item (keys(%user_roles)) {
1.384     raeburn  8712:             my ($role)=split(/\:/,$item,2);
                   8713:             if ($role eq 'cr') { next; }
                   8714:             if ($role =~ /^cr/) {
                   8715:                 $$rolehash{$role} = (split('/',$role))[3];
                   8716:             } else {
                   8717:                 $$rolehash{$role} = &Apache::lonnet::plaintext($role,$type);
                   8718:             }
                   8719:         }
                   8720:         foreach my $key (sort(keys(%{$rolehash}))) {
                   8721:             push(@{$allroles},$key);
                   8722:         }
                   8723:         push (@{$allroles},'st');
                   8724:         $$rolehash{'st'} = &Apache::lonnet::plaintext('st',$type);
                   8725:     }
                   8726:     return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
                   8727: }
                   8728: 
1.555     raeburn  8729: sub user_picker {
1.994     raeburn  8730:     my ($dom,$srch,$forcenewuser,$caller,$cancreate,$usertype,$context) = @_;
1.555     raeburn  8731:     my $currdom = $dom;
                   8732:     my %curr_selected = (
                   8733:                         srchin => 'dom',
1.580     raeburn  8734:                         srchby => 'lastname',
1.555     raeburn  8735:                       );
                   8736:     my $srchterm;
1.625     raeburn  8737:     if ((ref($srch) eq 'HASH') && ($env{'form.origform'} ne 'crtusername')) {
1.555     raeburn  8738:         if ($srch->{'srchby'} ne '') {
                   8739:             $curr_selected{'srchby'} = $srch->{'srchby'};
                   8740:         }
                   8741:         if ($srch->{'srchin'} ne '') {
                   8742:             $curr_selected{'srchin'} = $srch->{'srchin'};
                   8743:         }
                   8744:         if ($srch->{'srchtype'} ne '') {
                   8745:             $curr_selected{'srchtype'} = $srch->{'srchtype'};
                   8746:         }
                   8747:         if ($srch->{'srchdomain'} ne '') {
                   8748:             $currdom = $srch->{'srchdomain'};
                   8749:         }
                   8750:         $srchterm = $srch->{'srchterm'};
                   8751:     }
                   8752:     my %lt=&Apache::lonlocal::texthash(
1.573     raeburn  8753:                     'usr'       => 'Search criteria',
1.563     raeburn  8754:                     'doma'      => 'Domain/institution to search',
1.558     albertel 8755:                     'uname'     => 'username',
                   8756:                     'lastname'  => 'last name',
1.555     raeburn  8757:                     'lastfirst' => 'last name, first name',
1.558     albertel 8758:                     'crs'       => 'in this course',
1.576     raeburn  8759:                     'dom'       => 'in selected LON-CAPA domain', 
1.558     albertel 8760:                     'alc'       => 'all LON-CAPA',
1.573     raeburn  8761:                     'instd'     => 'in institutional directory for selected domain',
1.558     albertel 8762:                     'exact'     => 'is',
                   8763:                     'contains'  => 'contains',
1.569     raeburn  8764:                     'begins'    => 'begins with',
1.571     raeburn  8765:                     'youm'      => "You must include some text to search for.",
                   8766:                     'thte'      => "The text you are searching for must contain at least two characters when using a 'begins' type search.",
                   8767:                     'thet'      => "The text you are searching for must contain at least three characters when using a 'contains' type search.",
                   8768:                     'yomc'      => "You must choose a domain when using an institutional directory search.",
                   8769:                     'ymcd'      => "You must choose a domain when using a domain search.",
                   8770:                     'whus'      => "When using searching by last,first you must include a comma as separator between last name and first name.",
                   8771:                     'whse'      => "When searching by last,first you must include at least one character in the first name.",
                   8772:                      'thfo'     => "The following need to be corrected before the search can be run:",
1.555     raeburn  8773:                                        );
1.563     raeburn  8774:     my $domform = &select_dom_form($currdom,'srchdomain',1,1);
                   8775:     my $srchinsel = ' <select name="srchin">';
1.555     raeburn  8776: 
                   8777:     my @srchins = ('crs','dom','alc','instd');
                   8778: 
                   8779:     foreach my $option (@srchins) {
                   8780:         # FIXME 'alc' option unavailable until 
                   8781:         #       loncreateuser::print_user_query_page()
                   8782:         #       has been completed.
                   8783:         next if ($option eq 'alc');
1.880     raeburn  8784:         next if (($option eq 'crs') && ($env{'form.form'} eq 'requestcrs'));  
1.555     raeburn  8785:         next if ($option eq 'crs' && !$env{'request.course.id'});
1.563     raeburn  8786:         if ($curr_selected{'srchin'} eq $option) {
                   8787:             $srchinsel .= ' 
                   8788:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   8789:         } else {
                   8790:             $srchinsel .= '
                   8791:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   8792:         }
1.555     raeburn  8793:     }
1.563     raeburn  8794:     $srchinsel .= "\n  </select>\n";
1.555     raeburn  8795: 
                   8796:     my $srchbysel =  ' <select name="srchby">';
1.580     raeburn  8797:     foreach my $option ('lastname','lastfirst','uname') {
1.555     raeburn  8798:         if ($curr_selected{'srchby'} eq $option) {
                   8799:             $srchbysel .= '
                   8800:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   8801:         } else {
                   8802:             $srchbysel .= '
                   8803:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   8804:          }
                   8805:     }
                   8806:     $srchbysel .= "\n  </select>\n";
                   8807: 
                   8808:     my $srchtypesel = ' <select name="srchtype">';
1.580     raeburn  8809:     foreach my $option ('begins','contains','exact') {
1.555     raeburn  8810:         if ($curr_selected{'srchtype'} eq $option) {
                   8811:             $srchtypesel .= '
                   8812:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   8813:         } else {
                   8814:             $srchtypesel .= '
                   8815:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   8816:         }
                   8817:     }
                   8818:     $srchtypesel .= "\n  </select>\n";
                   8819: 
1.558     albertel 8820:     my ($newuserscript,$new_user_create);
1.994     raeburn  8821:     my $context_dom = $env{'request.role.domain'};
                   8822:     if ($context eq 'requestcrs') {
                   8823:         if ($env{'form.coursedom'} ne '') { 
                   8824:             $context_dom = $env{'form.coursedom'};
                   8825:         }
                   8826:     }
1.556     raeburn  8827:     if ($forcenewuser) {
1.576     raeburn  8828:         if (ref($srch) eq 'HASH') {
1.994     raeburn  8829:             if ($srch->{'srchby'} eq 'uname' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchin'} eq 'dom' && $srch->{'srchdomain'} eq $context_dom) {
1.627     raeburn  8830:                 if ($cancreate) {
                   8831:                     $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>';
                   8832:                 } else {
1.799     bisitz   8833:                     my $helplink = 'javascript:helpMenu('."'display'".')';
1.627     raeburn  8834:                     my %usertypetext = (
                   8835:                         official   => 'institutional',
                   8836:                         unofficial => 'non-institutional',
                   8837:                     );
1.799     bisitz   8838:                     $new_user_create = '<p class="LC_warning">'
                   8839:                                       .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
                   8840:                                       .' '
                   8841:                                       .&mt('Please contact the [_1]helpdesk[_2] for assistance.'
                   8842:                                           ,'<a href="'.$helplink.'">','</a>')
                   8843:                                       .'</p><br />';
1.627     raeburn  8844:                 }
1.576     raeburn  8845:             }
                   8846:         }
                   8847: 
1.556     raeburn  8848:         $newuserscript = <<"ENDSCRIPT";
                   8849: 
1.570     raeburn  8850: function setSearch(createnew,callingForm) {
1.556     raeburn  8851:     if (createnew == 1) {
1.570     raeburn  8852:         for (var i=0; i<callingForm.srchby.length; i++) {
                   8853:             if (callingForm.srchby.options[i].value == 'uname') {
                   8854:                 callingForm.srchby.selectedIndex = i;
1.556     raeburn  8855:             }
                   8856:         }
1.570     raeburn  8857:         for (var i=0; i<callingForm.srchin.length; i++) {
                   8858:             if ( callingForm.srchin.options[i].value == 'dom') {
                   8859: 		callingForm.srchin.selectedIndex = i;
1.556     raeburn  8860:             }
                   8861:         }
1.570     raeburn  8862:         for (var i=0; i<callingForm.srchtype.length; i++) {
                   8863:             if (callingForm.srchtype.options[i].value == 'exact') {
                   8864:                 callingForm.srchtype.selectedIndex = i;
1.556     raeburn  8865:             }
                   8866:         }
1.570     raeburn  8867:         for (var i=0; i<callingForm.srchdomain.length; i++) {
1.994     raeburn  8868:             if (callingForm.srchdomain.options[i].value == '$context_dom') {
1.570     raeburn  8869:                 callingForm.srchdomain.selectedIndex = i;
1.556     raeburn  8870:             }
                   8871:         }
                   8872:     }
                   8873: }
                   8874: ENDSCRIPT
1.558     albertel 8875: 
1.556     raeburn  8876:     }
                   8877: 
1.555     raeburn  8878:     my $output = <<"END_BLOCK";
1.556     raeburn  8879: <script type="text/javascript">
1.824     bisitz   8880: // <![CDATA[
1.570     raeburn  8881: function validateEntry(callingForm) {
1.558     albertel 8882: 
1.556     raeburn  8883:     var checkok = 1;
1.558     albertel 8884:     var srchin;
1.570     raeburn  8885:     for (var i=0; i<callingForm.srchin.length; i++) {
                   8886: 	if ( callingForm.srchin[i].checked ) {
                   8887: 	    srchin = callingForm.srchin[i].value;
1.558     albertel 8888: 	}
                   8889:     }
                   8890: 
1.570     raeburn  8891:     var srchtype = callingForm.srchtype.options[callingForm.srchtype.selectedIndex].value;
                   8892:     var srchby = callingForm.srchby.options[callingForm.srchby.selectedIndex].value;
                   8893:     var srchdomain = callingForm.srchdomain.options[callingForm.srchdomain.selectedIndex].value;
                   8894:     var srchterm =  callingForm.srchterm.value;
                   8895:     var srchin = callingForm.srchin.options[callingForm.srchin.selectedIndex].value;
1.556     raeburn  8896:     var msg = "";
                   8897: 
                   8898:     if (srchterm == "") {
                   8899:         checkok = 0;
1.571     raeburn  8900:         msg += "$lt{'youm'}\\n";
1.556     raeburn  8901:     }
                   8902: 
1.569     raeburn  8903:     if (srchtype== 'begins') {
                   8904:         if (srchterm.length < 2) {
                   8905:             checkok = 0;
1.571     raeburn  8906:             msg += "$lt{'thte'}\\n";
1.569     raeburn  8907:         }
                   8908:     }
                   8909: 
1.556     raeburn  8910:     if (srchtype== 'contains') {
                   8911:         if (srchterm.length < 3) {
                   8912:             checkok = 0;
1.571     raeburn  8913:             msg += "$lt{'thet'}\\n";
1.556     raeburn  8914:         }
                   8915:     }
                   8916:     if (srchin == 'instd') {
                   8917:         if (srchdomain == '') {
                   8918:             checkok = 0;
1.571     raeburn  8919:             msg += "$lt{'yomc'}\\n";
1.556     raeburn  8920:         }
                   8921:     }
                   8922:     if (srchin == 'dom') {
                   8923:         if (srchdomain == '') {
                   8924:             checkok = 0;
1.571     raeburn  8925:             msg += "$lt{'ymcd'}\\n";
1.556     raeburn  8926:         }
                   8927:     }
                   8928:     if (srchby == 'lastfirst') {
                   8929:         if (srchterm.indexOf(",") == -1) {
                   8930:             checkok = 0;
1.571     raeburn  8931:             msg += "$lt{'whus'}\\n";
1.556     raeburn  8932:         }
                   8933:         if (srchterm.indexOf(",") == srchterm.length -1) {
                   8934:             checkok = 0;
1.571     raeburn  8935:             msg += "$lt{'whse'}\\n";
1.556     raeburn  8936:         }
                   8937:     }
                   8938:     if (checkok == 0) {
1.571     raeburn  8939:         alert("$lt{'thfo'}\\n"+msg);
1.556     raeburn  8940:         return;
                   8941:     }
                   8942:     if (checkok == 1) {
1.570     raeburn  8943:         callingForm.submit();
1.556     raeburn  8944:     }
                   8945: }
                   8946: 
                   8947: $newuserscript
                   8948: 
1.824     bisitz   8949: // ]]>
1.556     raeburn  8950: </script>
1.558     albertel 8951: 
                   8952: $new_user_create
                   8953: 
1.555     raeburn  8954: END_BLOCK
1.558     albertel 8955: 
1.876     raeburn  8956:     $output .= &Apache::lonhtmlcommon::start_pick_box().
                   8957:                &Apache::lonhtmlcommon::row_title($lt{'doma'}).
                   8958:                $domform.
                   8959:                &Apache::lonhtmlcommon::row_closure().
                   8960:                &Apache::lonhtmlcommon::row_title($lt{'usr'}).
                   8961:                $srchbysel.
                   8962:                $srchtypesel. 
                   8963:                '<input type="text" size="15" name="srchterm" value="'.$srchterm.'" />'.
                   8964:                $srchinsel.
                   8965:                &Apache::lonhtmlcommon::row_closure(1). 
                   8966:                &Apache::lonhtmlcommon::end_pick_box().
                   8967:                '<br />';
1.555     raeburn  8968:     return $output;
                   8969: }
                   8970: 
1.612     raeburn  8971: sub user_rule_check {
1.615     raeburn  8972:     my ($usershash,$checks,$alerts,$rulematch,$inst_results,$curr_rules,$got_rules) = @_;
1.612     raeburn  8973:     my $response;
                   8974:     if (ref($usershash) eq 'HASH') {
                   8975:         foreach my $user (keys(%{$usershash})) {
                   8976:             my ($uname,$udom) = split(/:/,$user);
                   8977:             next if ($udom eq '' || $uname eq '');
1.615     raeburn  8978:             my ($id,$newuser);
1.612     raeburn  8979:             if (ref($usershash->{$user}) eq 'HASH') {
1.615     raeburn  8980:                 $newuser = $usershash->{$user}->{'newuser'};
1.612     raeburn  8981:                 $id = $usershash->{$user}->{'id'};
                   8982:             }
                   8983:             my $inst_response;
                   8984:             if (ref($checks) eq 'HASH') {
                   8985:                 if (defined($checks->{'username'})) {
1.615     raeburn  8986:                     ($inst_response,%{$inst_results->{$user}}) = 
1.612     raeburn  8987:                         &Apache::lonnet::get_instuser($udom,$uname);
                   8988:                 } elsif (defined($checks->{'id'})) {
1.615     raeburn  8989:                     ($inst_response,%{$inst_results->{$user}}) =
1.612     raeburn  8990:                         &Apache::lonnet::get_instuser($udom,undef,$id);
                   8991:                 }
1.615     raeburn  8992:             } else {
                   8993:                 ($inst_response,%{$inst_results->{$user}}) =
                   8994:                     &Apache::lonnet::get_instuser($udom,$uname);
                   8995:                 return;
1.612     raeburn  8996:             }
1.615     raeburn  8997:             if (!$got_rules->{$udom}) {
1.612     raeburn  8998:                 my %domconfig = &Apache::lonnet::get_dom('configuration',
                   8999:                                                   ['usercreation'],$udom);
                   9000:                 if (ref($domconfig{'usercreation'}) eq 'HASH') {
1.615     raeburn  9001:                     foreach my $item ('username','id') {
1.612     raeburn  9002:                         if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
                   9003:                             $$curr_rules{$udom}{$item} = 
                   9004:                                 $domconfig{'usercreation'}{$item.'_rule'};
1.585     raeburn  9005:                         }
                   9006:                     }
                   9007:                 }
1.615     raeburn  9008:                 $got_rules->{$udom} = 1;  
1.585     raeburn  9009:             }
1.612     raeburn  9010:             foreach my $item (keys(%{$checks})) {
                   9011:                 if (ref($$curr_rules{$udom}) eq 'HASH') {
                   9012:                     if (ref($$curr_rules{$udom}{$item}) eq 'ARRAY') {
                   9013:                         if (@{$$curr_rules{$udom}{$item}} > 0) {
                   9014:                             my %rule_check = &Apache::lonnet::inst_rulecheck($udom,$uname,$id,$item,$$curr_rules{$udom}{$item});
                   9015:                             foreach my $rule (@{$$curr_rules{$udom}{$item}}) {
                   9016:                                 if ($rule_check{$rule}) {
                   9017:                                     $$rulematch{$user}{$item} = $rule;
                   9018:                                     if ($inst_response eq 'ok') {
1.615     raeburn  9019:                                         if (ref($inst_results) eq 'HASH') {
                   9020:                                             if (ref($inst_results->{$user}) eq 'HASH') {
                   9021:                                                 if (keys(%{$inst_results->{$user}}) == 0) {
                   9022:                                                     $$alerts{$item}{$udom}{$uname} = 1;
                   9023:                                                 }
1.612     raeburn  9024:                                             }
                   9025:                                         }
1.615     raeburn  9026:                                     }
                   9027:                                     last;
1.585     raeburn  9028:                                 }
                   9029:                             }
                   9030:                         }
                   9031:                     }
                   9032:                 }
                   9033:             }
                   9034:         }
                   9035:     }
1.612     raeburn  9036:     return;
                   9037: }
                   9038: 
                   9039: sub user_rule_formats {
                   9040:     my ($domain,$domdesc,$curr_rules,$check) = @_;
                   9041:     my %text = ( 
                   9042:                  'username' => 'Usernames',
                   9043:                  'id'       => 'IDs',
                   9044:                );
                   9045:     my $output;
                   9046:     my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($domain,$check);
                   9047:     if ((ref($rules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
                   9048:         if (@{$ruleorder} > 0) {
1.1075.2.20  raeburn  9049:             $output = '<br />'.
                   9050:                       &mt($text{$check}.' with the following format(s) may [_1]only[_2] be used for verified users at [_3]:',
                   9051:                           '<span class="LC_cusr_emph">','</span>',$domdesc).
                   9052:                       ' <ul>';
1.612     raeburn  9053:             foreach my $rule (@{$ruleorder}) {
                   9054:                 if (ref($curr_rules) eq 'ARRAY') {
                   9055:                     if (grep(/^\Q$rule\E$/,@{$curr_rules})) {
                   9056:                         if (ref($rules->{$rule}) eq 'HASH') {
                   9057:                             $output .= '<li>'.$rules->{$rule}{'name'}.': '.
                   9058:                                         $rules->{$rule}{'desc'}.'</li>';
                   9059:                         }
                   9060:                     }
                   9061:                 }
                   9062:             }
                   9063:             $output .= '</ul>';
                   9064:         }
                   9065:     }
                   9066:     return $output;
                   9067: }
                   9068: 
                   9069: sub instrule_disallow_msg {
1.615     raeburn  9070:     my ($checkitem,$domdesc,$count,$mode) = @_;
1.612     raeburn  9071:     my $response;
                   9072:     my %text = (
                   9073:                   item   => 'username',
                   9074:                   items  => 'usernames',
                   9075:                   match  => 'matches',
                   9076:                   do     => 'does',
                   9077:                   action => 'a username',
                   9078:                   one    => 'one',
                   9079:                );
                   9080:     if ($count > 1) {
                   9081:         $text{'item'} = 'usernames';
                   9082:         $text{'match'} ='match';
                   9083:         $text{'do'} = 'do';
                   9084:         $text{'action'} = 'usernames',
                   9085:         $text{'one'} = 'ones';
                   9086:     }
                   9087:     if ($checkitem eq 'id') {
                   9088:         $text{'items'} = 'IDs';
                   9089:         $text{'item'} = 'ID';
                   9090:         $text{'action'} = 'an ID';
1.615     raeburn  9091:         if ($count > 1) {
                   9092:             $text{'item'} = 'IDs';
                   9093:             $text{'action'} = 'IDs';
                   9094:         }
1.612     raeburn  9095:     }
1.674     bisitz   9096:     $response = &mt("The $text{'item'} you chose $text{'match'} the format of $text{'items'} defined for [_1], but the $text{'item'} $text{'do'} not exist in the institutional directory.",'<span class="LC_cusr_emph">'.$domdesc.'</span>').'<br />';
1.615     raeburn  9097:     if ($mode eq 'upload') {
                   9098:         if ($checkitem eq 'username') {
                   9099:             $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'}.");
                   9100:         } elsif ($checkitem eq 'id') {
1.674     bisitz   9101:             $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 Student/Employee ID field.");
1.615     raeburn  9102:         }
1.669     raeburn  9103:     } elsif ($mode eq 'selfcreate') {
                   9104:         if ($checkitem eq 'id') {
                   9105:             $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.");
                   9106:         }
1.615     raeburn  9107:     } else {
                   9108:         if ($checkitem eq 'username') {
                   9109:             $response .= &mt("You must choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}.");
                   9110:         } elsif ($checkitem eq 'id') {
                   9111:             $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.");
                   9112:         }
1.612     raeburn  9113:     }
                   9114:     return $response;
1.585     raeburn  9115: }
                   9116: 
1.624     raeburn  9117: sub personal_data_fieldtitles {
                   9118:     my %fieldtitles = &Apache::lonlocal::texthash (
                   9119:                         id => 'Student/Employee ID',
                   9120:                         permanentemail => 'E-mail address',
                   9121:                         lastname => 'Last Name',
                   9122:                         firstname => 'First Name',
                   9123:                         middlename => 'Middle Name',
                   9124:                         generation => 'Generation',
                   9125:                         gen => 'Generation',
1.765     raeburn  9126:                         inststatus => 'Affiliation',
1.624     raeburn  9127:                    );
                   9128:     return %fieldtitles;
                   9129: }
                   9130: 
1.642     raeburn  9131: sub sorted_inst_types {
                   9132:     my ($dom) = @_;
                   9133:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($dom);
                   9134:     my $othertitle = &mt('All users');
                   9135:     if ($env{'request.course.id'}) {
1.668     raeburn  9136:         $othertitle  = &mt('Any users');
1.642     raeburn  9137:     }
                   9138:     my @types;
                   9139:     if (ref($order) eq 'ARRAY') {
                   9140:         @types = @{$order};
                   9141:     }
                   9142:     if (@types == 0) {
                   9143:         if (ref($usertypes) eq 'HASH') {
                   9144:             @types = sort(keys(%{$usertypes}));
                   9145:         }
                   9146:     }
                   9147:     if (keys(%{$usertypes}) > 0) {
                   9148:         $othertitle = &mt('Other users');
                   9149:     }
                   9150:     return ($othertitle,$usertypes,\@types);
                   9151: }
                   9152: 
1.645     raeburn  9153: sub get_institutional_codes {
                   9154:     my ($settings,$allcourses,$LC_code) = @_;
                   9155: # Get complete list of course sections to update
                   9156:     my @currsections = ();
                   9157:     my @currxlists = ();
                   9158:     my $coursecode = $$settings{'internal.coursecode'};
                   9159: 
                   9160:     if ($$settings{'internal.sectionnums'} ne '') {
                   9161:         @currsections = split(/,/,$$settings{'internal.sectionnums'});
                   9162:     }
                   9163: 
                   9164:     if ($$settings{'internal.crosslistings'} ne '') {
                   9165:         @currxlists = split(/,/,$$settings{'internal.crosslistings'});
                   9166:     }
                   9167: 
                   9168:     if (@currxlists > 0) {
                   9169:         foreach (@currxlists) {
                   9170:             if (m/^([^:]+):(\w*)$/) {
                   9171:                 unless (grep/^$1$/,@{$allcourses}) {
                   9172:                     push @{$allcourses},$1;
                   9173:                     $$LC_code{$1} = $2;
                   9174:                 }
                   9175:             }
                   9176:         }
                   9177:     }
                   9178:  
                   9179:     if (@currsections > 0) {
                   9180:         foreach (@currsections) {
                   9181:             if (m/^(\w+):(\w*)$/) {
                   9182:                 my $sec = $coursecode.$1;
                   9183:                 my $lc_sec = $2;
                   9184:                 unless (grep/^$sec$/,@{$allcourses}) {
                   9185:                     push @{$allcourses},$sec;
                   9186:                     $$LC_code{$sec} = $lc_sec;
                   9187:                 }
                   9188:             }
                   9189:         }
                   9190:     }
                   9191:     return;
                   9192: }
                   9193: 
1.971     raeburn  9194: sub get_standard_codeitems {
                   9195:     return ('Year','Semester','Department','Number','Section');
                   9196: }
                   9197: 
1.112     bowersj2 9198: =pod
                   9199: 
1.780     raeburn  9200: =head1 Slot Helpers
                   9201: 
                   9202: =over 4
                   9203: 
                   9204: =item * sorted_slots()
                   9205: 
1.1040    raeburn  9206: Sorts an array of slot names in order of an optional sort key,
                   9207: default sort is by slot start time (earliest first). 
1.780     raeburn  9208: 
                   9209: Inputs:
                   9210: 
                   9211: =over 4
                   9212: 
                   9213: slotsarr  - Reference to array of unsorted slot names.
                   9214: 
                   9215: slots     - Reference to hash of hash, where outer hash keys are slot names.
                   9216: 
1.1040    raeburn  9217: sortkey   - Name of key in inner hash to be sorted on (e.g., starttime).
                   9218: 
1.549     albertel 9219: =back
                   9220: 
1.780     raeburn  9221: Returns:
                   9222: 
                   9223: =over 4
                   9224: 
1.1040    raeburn  9225: sorted   - An array of slot names sorted by a specified sort key 
                   9226:            (default sort key is start time of the slot).
1.780     raeburn  9227: 
                   9228: =back
                   9229: 
                   9230: =cut
                   9231: 
                   9232: 
                   9233: sub sorted_slots {
1.1040    raeburn  9234:     my ($slotsarr,$slots,$sortkey) = @_;
                   9235:     if ($sortkey eq '') {
                   9236:         $sortkey = 'starttime';
                   9237:     }
1.780     raeburn  9238:     my @sorted;
                   9239:     if ((ref($slotsarr) eq 'ARRAY') && (ref($slots) eq 'HASH')) {
                   9240:         @sorted =
                   9241:             sort {
                   9242:                      if (ref($slots->{$a}) && ref($slots->{$b})) {
1.1040    raeburn  9243:                          return $slots->{$a}{$sortkey} <=> $slots->{$b}{$sortkey}
1.780     raeburn  9244:                      }
                   9245:                      if (ref($slots->{$a})) { return -1;}
                   9246:                      if (ref($slots->{$b})) { return 1;}
                   9247:                      return 0;
                   9248:                  } @{$slotsarr};
                   9249:     }
                   9250:     return @sorted;
                   9251: }
                   9252: 
1.1040    raeburn  9253: =pod
                   9254: 
                   9255: =item * get_future_slots()
                   9256: 
                   9257: Inputs:
                   9258: 
                   9259: =over 4
                   9260: 
                   9261: cnum - course number
                   9262: 
                   9263: cdom - course domain
                   9264: 
                   9265: now - current UNIX time
                   9266: 
                   9267: symb - optional symb
                   9268: 
                   9269: =back
                   9270: 
                   9271: Returns:
                   9272: 
                   9273: =over 4
                   9274: 
                   9275: sorted_reservable - ref to array of student_schedulable slots currently 
                   9276:                     reservable, ordered by end date of reservation period.
                   9277: 
                   9278: reservable_now - ref to hash of student_schedulable slots currently
                   9279:                  reservable.
                   9280: 
                   9281:     Keys in inner hash are:
                   9282:     (a) symb: either blank or symb to which slot use is restricted.
                   9283:     (b) endreserve: end date of reservation period. 
                   9284: 
                   9285: sorted_future - ref to array of student_schedulable slots reservable in
                   9286:                 the future, ordered by start date of reservation period.
                   9287: 
                   9288: future_reservable - ref to hash of student_schedulable slots reservable
                   9289:                     in the future.
                   9290: 
                   9291:     Keys in inner hash are:
                   9292:     (a) symb: either blank or symb to which slot use is restricted.
                   9293:     (b) startreserve:  start date of reservation period.
                   9294: 
                   9295: =back
                   9296: 
                   9297: =cut
                   9298: 
                   9299: sub get_future_slots {
                   9300:     my ($cnum,$cdom,$now,$symb) = @_;
                   9301:     my (%reservable_now,%future_reservable,@sorted_reservable,@sorted_future);
                   9302:     my %slots = &Apache::lonnet::get_course_slots($cnum,$cdom);
                   9303:     foreach my $slot (keys(%slots)) {
                   9304:         next unless($slots{$slot}->{'type'} eq 'schedulable_student');
                   9305:         if ($symb) {
                   9306:             next if (($slots{$slot}->{'symb'} ne '') && 
                   9307:                      ($slots{$slot}->{'symb'} ne $symb));
                   9308:         }
                   9309:         if (($slots{$slot}->{'starttime'} > $now) &&
                   9310:             ($slots{$slot}->{'endtime'} > $now)) {
                   9311:             if (($slots{$slot}->{'allowedsections'}) || ($slots{$slot}->{'allowedusers'})) {
                   9312:                 my $userallowed = 0;
                   9313:                 if ($slots{$slot}->{'allowedsections'}) {
                   9314:                     my @allowed_sec = split(',',$slots{$slot}->{'allowedsections'});
                   9315:                     if (!defined($env{'request.role.sec'})
                   9316:                         && grep(/^No section assigned$/,@allowed_sec)) {
                   9317:                         $userallowed=1;
                   9318:                     } else {
                   9319:                         if (grep(/^\Q$env{'request.role.sec'}\E$/,@allowed_sec)) {
                   9320:                             $userallowed=1;
                   9321:                         }
                   9322:                     }
                   9323:                     unless ($userallowed) {
                   9324:                         if (defined($env{'request.course.groups'})) {
                   9325:                             my @groups = split(/:/,$env{'request.course.groups'});
                   9326:                             foreach my $group (@groups) {
                   9327:                                 if (grep(/^\Q$group\E$/,@allowed_sec)) {
                   9328:                                     $userallowed=1;
                   9329:                                     last;
                   9330:                                 }
                   9331:                             }
                   9332:                         }
                   9333:                     }
                   9334:                 }
                   9335:                 if ($slots{$slot}->{'allowedusers'}) {
                   9336:                     my @allowed_users = split(',',$slots{$slot}->{'allowedusers'});
                   9337:                     my $user = $env{'user.name'}.':'.$env{'user.domain'};
                   9338:                     if (grep(/^\Q$user\E$/,@allowed_users)) {
                   9339:                         $userallowed = 1;
                   9340:                     }
                   9341:                 }
                   9342:                 next unless($userallowed);
                   9343:             }
                   9344:             my $startreserve = $slots{$slot}->{'startreserve'};
                   9345:             my $endreserve = $slots{$slot}->{'endreserve'};
                   9346:             my $symb = $slots{$slot}->{'symb'};
                   9347:             if (($startreserve < $now) &&
                   9348:                 (!$endreserve || $endreserve > $now)) {
                   9349:                 my $lastres = $endreserve;
                   9350:                 if (!$lastres) {
                   9351:                     $lastres = $slots{$slot}->{'starttime'};
                   9352:                 }
                   9353:                 $reservable_now{$slot} = {
                   9354:                                            symb       => $symb,
                   9355:                                            endreserve => $lastres
                   9356:                                          };
                   9357:             } elsif (($startreserve > $now) &&
                   9358:                      (!$endreserve || $endreserve > $startreserve)) {
                   9359:                 $future_reservable{$slot} = {
                   9360:                                               symb         => $symb,
                   9361:                                               startreserve => $startreserve
                   9362:                                             };
                   9363:             }
                   9364:         }
                   9365:     }
                   9366:     my @unsorted_reservable = keys(%reservable_now);
                   9367:     if (@unsorted_reservable > 0) {
                   9368:         @sorted_reservable = 
                   9369:             &sorted_slots(\@unsorted_reservable,\%reservable_now,'endreserve');
                   9370:     }
                   9371:     my @unsorted_future = keys(%future_reservable);
                   9372:     if (@unsorted_future > 0) {
                   9373:         @sorted_future =
                   9374:             &sorted_slots(\@unsorted_future,\%future_reservable,'startreserve');
                   9375:     }
                   9376:     return (\@sorted_reservable,\%reservable_now,\@sorted_future,\%future_reservable);
                   9377: }
1.780     raeburn  9378: 
                   9379: =pod
                   9380: 
1.1057    foxr     9381: =back
                   9382: 
1.549     albertel 9383: =head1 HTTP Helpers
                   9384: 
                   9385: =over 4
                   9386: 
1.648     raeburn  9387: =item * &get_unprocessed_cgi($query,$possible_names)
1.112     bowersj2 9388: 
1.258     albertel 9389: Modify the %env hash to contain unprocessed CGI form parameters held in
1.112     bowersj2 9390: $query.  The parameters listed in $possible_names (an array reference),
1.258     albertel 9391: will be set in $env{'form.name'} if they do not already exist.
1.112     bowersj2 9392: 
                   9393: Typically called with $ENV{'QUERY_STRING'} as the first parameter.  
                   9394: $possible_names is an ref to an array of form element names.  As an example:
                   9395: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
1.258     albertel 9396: will result in $env{'form.uname'} and $env{'form.udom'} being set.
1.112     bowersj2 9397: 
                   9398: =cut
1.1       albertel 9399: 
1.6       albertel 9400: sub get_unprocessed_cgi {
1.25      albertel 9401:   my ($query,$possible_names)= @_;
1.26      matthew  9402:   # $Apache::lonxml::debug=1;
1.356     albertel 9403:   foreach my $pair (split(/&/,$query)) {
                   9404:     my ($name, $value) = split(/=/,$pair);
1.369     www      9405:     $name = &unescape($name);
1.25      albertel 9406:     if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
                   9407:       $value =~ tr/+/ /;
                   9408:       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.258     albertel 9409:       unless (defined($env{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
1.25      albertel 9410:     }
1.16      harris41 9411:   }
1.6       albertel 9412: }
                   9413: 
1.112     bowersj2 9414: =pod
                   9415: 
1.648     raeburn  9416: =item * &cacheheader() 
1.112     bowersj2 9417: 
                   9418: returns cache-controlling header code
                   9419: 
                   9420: =cut
                   9421: 
1.7       albertel 9422: sub cacheheader {
1.258     albertel 9423:     unless ($env{'request.method'} eq 'GET') { return ''; }
1.216     albertel 9424:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
                   9425:     my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
1.7       albertel 9426:                 <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
                   9427:                 <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
1.216     albertel 9428:     return $output;
1.7       albertel 9429: }
                   9430: 
1.112     bowersj2 9431: =pod
                   9432: 
1.648     raeburn  9433: =item * &no_cache($r) 
1.112     bowersj2 9434: 
                   9435: specifies header code to not have cache
                   9436: 
                   9437: =cut
                   9438: 
1.9       albertel 9439: sub no_cache {
1.216     albertel 9440:     my ($r) = @_;
                   9441:     if ($ENV{'REQUEST_METHOD'} ne 'GET' &&
1.258     albertel 9442: 	$env{'request.method'} ne 'GET') { return ''; }
1.216     albertel 9443:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time));
                   9444:     $r->no_cache(1);
                   9445:     $r->header_out("Expires" => $date);
                   9446:     $r->header_out("Pragma" => "no-cache");
1.123     www      9447: }
                   9448: 
                   9449: sub content_type {
1.181     albertel 9450:     my ($r,$type,$charset) = @_;
1.299     foxr     9451:     if ($r) {
                   9452: 	#  Note that printout.pl calls this with undef for $r.
                   9453: 	&no_cache($r);
                   9454:     }
1.258     albertel 9455:     if ($env{'browser.mathml'} && $type eq 'text/html') { $type='text/xml'; }
1.181     albertel 9456:     unless ($charset) {
                   9457: 	$charset=&Apache::lonlocal::current_encoding;
                   9458:     }
                   9459:     if ($charset) { $type.='; charset='.$charset; }
                   9460:     if ($r) {
                   9461: 	$r->content_type($type);
                   9462:     } else {
                   9463: 	print("Content-type: $type\n\n");
                   9464:     }
1.9       albertel 9465: }
1.25      albertel 9466: 
1.112     bowersj2 9467: =pod
                   9468: 
1.648     raeburn  9469: =item * &add_to_env($name,$value) 
1.112     bowersj2 9470: 
1.258     albertel 9471: adds $name to the %env hash with value
1.112     bowersj2 9472: $value, if $name already exists, the entry is converted to an array
                   9473: reference and $value is added to the array.
                   9474: 
                   9475: =cut
                   9476: 
1.25      albertel 9477: sub add_to_env {
                   9478:   my ($name,$value)=@_;
1.258     albertel 9479:   if (defined($env{$name})) {
                   9480:     if (ref($env{$name})) {
1.25      albertel 9481:       #already have multiple values
1.258     albertel 9482:       push(@{ $env{$name} },$value);
1.25      albertel 9483:     } else {
                   9484:       #first time seeing multiple values, convert hash entry to an arrayref
1.258     albertel 9485:       my $first=$env{$name};
                   9486:       undef($env{$name});
                   9487:       push(@{ $env{$name} },$first,$value);
1.25      albertel 9488:     }
                   9489:   } else {
1.258     albertel 9490:     $env{$name}=$value;
1.25      albertel 9491:   }
1.31      albertel 9492: }
1.149     albertel 9493: 
                   9494: =pod
                   9495: 
1.648     raeburn  9496: =item * &get_env_multiple($name) 
1.149     albertel 9497: 
1.258     albertel 9498: gets $name from the %env hash, it seemlessly handles the cases where multiple
1.149     albertel 9499: values may be defined and end up as an array ref.
                   9500: 
                   9501: returns an array of values
                   9502: 
                   9503: =cut
                   9504: 
                   9505: sub get_env_multiple {
                   9506:     my ($name) = @_;
                   9507:     my @values;
1.258     albertel 9508:     if (defined($env{$name})) {
1.149     albertel 9509:         # exists is it an array
1.258     albertel 9510:         if (ref($env{$name})) {
                   9511:             @values=@{ $env{$name} };
1.149     albertel 9512:         } else {
1.258     albertel 9513:             $values[0]=$env{$name};
1.149     albertel 9514:         }
                   9515:     }
                   9516:     return(@values);
                   9517: }
                   9518: 
1.660     raeburn  9519: sub ask_for_embedded_content {
                   9520:     my ($actionurl,$state,$allfiles,$codebase,$args)=@_;
1.1071    raeburn  9521:     my (%subdependencies,%dependencies,%mapping,%existing,%newfiles,%pathchanges,
1.1075.2.11  raeburn  9522:         %currsubfile,%unused,$rem);
1.1071    raeburn  9523:     my $counter = 0;
                   9524:     my $numnew = 0;
1.987     raeburn  9525:     my $numremref = 0;
                   9526:     my $numinvalid = 0;
                   9527:     my $numpathchg = 0;
                   9528:     my $numexisting = 0;
1.1071    raeburn  9529:     my $numunused = 0;
                   9530:     my ($output,$upload_output,$toplevel,$url,$udom,$uname,$getpropath,$cdom,$cnum,
                   9531:         $fileloc,$filename,$delete_output,$modify_output,$title,$symb,$path);
                   9532:     my $heading = &mt('Upload embedded files');
                   9533:     my $buttontext = &mt('Upload');
                   9534: 
1.1075.2.35! raeburn  9535:     my ($navmap,$cdom,$cnum);
1.1075.2.11  raeburn  9536:     if ($env{'request.course.id'}) {
1.1075.2.35! raeburn  9537:         if ($actionurl eq '/adm/dependencies') {
        !          9538:             $navmap = Apache::lonnavmaps::navmap->new();
        !          9539:         }
        !          9540:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
        !          9541:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.1075.2.11  raeburn  9542:     }
1.1075.2.35! raeburn  9543:     if (($actionurl eq '/adm/portfolio') ||
        !          9544:         ($actionurl eq '/adm/coursegrp_portfolio')) {
1.984     raeburn  9545:         my $current_path='/';
                   9546:         if ($env{'form.currentpath'}) {
                   9547:             $current_path = $env{'form.currentpath'};
                   9548:         }
                   9549:         if ($actionurl eq '/adm/coursegrp_portfolio') {
1.1075.2.35! raeburn  9550:             $udom = $cdom;
        !          9551:             $uname = $cnum;
1.984     raeburn  9552:             $url = '/userfiles/groups/'.$env{'form.group'}.'/portfolio';
                   9553:         } else {
                   9554:             $udom = $env{'user.domain'};
                   9555:             $uname = $env{'user.name'};
                   9556:             $url = '/userfiles/portfolio';
                   9557:         }
1.987     raeburn  9558:         $toplevel = $url.'/';
1.984     raeburn  9559:         $url .= $current_path;
                   9560:         $getpropath = 1;
1.987     raeburn  9561:     } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank') ||
                   9562:              ($actionurl eq '/adm/imsimport')) { 
1.1022    www      9563:         my ($udom,$uname,$rest) = ($args->{'current_path'} =~ m{/priv/($match_domain)/($match_username)/?(.*)$});
1.1026    raeburn  9564:         $url = $Apache::lonnet::perlvar{'lonDocRoot'}."/priv/$udom/$uname/";
1.987     raeburn  9565:         $toplevel = $url;
1.984     raeburn  9566:         if ($rest ne '') {
1.987     raeburn  9567:             $url .= $rest;
                   9568:         }
                   9569:     } elsif ($actionurl eq '/adm/coursedocs') {
                   9570:         if (ref($args) eq 'HASH') {
1.1071    raeburn  9571:             $url = $args->{'docs_url'};
                   9572:             $toplevel = $url;
1.1075.2.11  raeburn  9573:             if ($args->{'context'} eq 'paste') {
                   9574:                 ($cdom,$cnum) = ($url =~ m{^\Q/uploaded/\E($match_domain)/($match_courseid)/});
                   9575:                 ($path) =
                   9576:                     ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/\E(?:docs|supplemental)/(?:default|\d+)/\d+)/});
                   9577:                 $fileloc = &Apache::lonnet::filelocation('',$toplevel);
                   9578:                 $fileloc =~ s{^/}{};
                   9579:             }
1.1071    raeburn  9580:         }
                   9581:     } elsif ($actionurl eq '/adm/dependencies') {
                   9582:         if ($env{'request.course.id'} ne '') {
                   9583:             if (ref($args) eq 'HASH') {
                   9584:                 $url = $args->{'docs_url'};
                   9585:                 $title = $args->{'docs_title'};
1.1075.2.35! raeburn  9586:                 $toplevel = $url;
        !          9587:                 unless ($toplevel =~ m{^/}) {
        !          9588:                     $toplevel = "/$url";
        !          9589:                 }
1.1075.2.11  raeburn  9590:                 ($rem) = ($toplevel =~ m{^(.+/)[^/]+$});
1.1075.2.35! raeburn  9591:                 if ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/portfolio/syllabus\E)}) {
        !          9592:                     $path = $1;
        !          9593:                 } else {
        !          9594:                     ($path) =
        !          9595:                         ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/\E(?:docs|supplemental)/(?:default|\d+)/\d+)/});
        !          9596:                 }
1.1071    raeburn  9597:                 $fileloc = &Apache::lonnet::filelocation('',$toplevel);
                   9598:                 $fileloc =~ s{^/}{};
                   9599:                 ($filename) = ($fileloc =~ m{.+/([^/]+)$});
                   9600:                 $heading = &mt('Status of dependencies in [_1]',"$title ($filename)");
                   9601:             }
1.987     raeburn  9602:         }
1.1075.2.35! raeburn  9603:     } elsif ($actionurl eq "/public/$cdom/$cnum/syllabus") {
        !          9604:         $udom = $cdom;
        !          9605:         $uname = $cnum;
        !          9606:         $url = "/uploaded/$cdom/$cnum/portfolio/syllabus";
        !          9607:         $toplevel = $url;
        !          9608:         $path = $url;
        !          9609:         $fileloc = &Apache::lonnet::filelocation('',$toplevel).'/';
        !          9610:         $fileloc =~ s{^/}{};
        !          9611:     }
1.987     raeburn  9612:     }
1.1075.2.35! raeburn  9613:     foreach my $file (keys(%{$allfiles})) {
        !          9614:         my $embed_file;
        !          9615:         if (($path eq "/uploaded/$cdom/$cnum/portfolio/syllabus") && ($file =~ m{^\Q$path/\E(.+)$})) {
        !          9616:             $embed_file = $1;
        !          9617:         } else {
        !          9618:             $embed_file = $file;
        !          9619:         }
1.987     raeburn  9620:         my $absolutepath;
                   9621:         if ($embed_file =~ m{^\w+://}) {
                   9622:             $newfiles{$embed_file} = 1;
                   9623:             $mapping{$embed_file} = $embed_file;
                   9624:         } else {
                   9625:             if ($embed_file =~ m{^/}) {
                   9626:                 $absolutepath = $embed_file;
                   9627:                 $embed_file =~ s{^(/+)}{};
                   9628:             }
                   9629:             if ($embed_file =~ m{/}) {
                   9630:                 my ($path,$fname) = ($embed_file =~ m{^(.+)/([^/]*)$});
                   9631:                 $path = &check_for_traversal($path,$url,$toplevel);
                   9632:                 my $item = $fname;
                   9633:                 if ($path ne '') {
                   9634:                     $item = $path.'/'.$fname;
                   9635:                     $subdependencies{$path}{$fname} = 1;
                   9636:                 } else {
                   9637:                     $dependencies{$item} = 1;
                   9638:                 }
                   9639:                 if ($absolutepath) {
                   9640:                     $mapping{$item} = $absolutepath;
                   9641:                 } else {
                   9642:                     $mapping{$item} = $embed_file;
                   9643:                 }
                   9644:             } else {
                   9645:                 $dependencies{$embed_file} = 1;
                   9646:                 if ($absolutepath) {
                   9647:                     $mapping{$embed_file} = $absolutepath;
                   9648:                 } else {
                   9649:                     $mapping{$embed_file} = $embed_file;
                   9650:                 }
                   9651:             }
1.984     raeburn  9652:         }
                   9653:     }
1.1071    raeburn  9654:     my $dirptr = 16384;
1.984     raeburn  9655:     foreach my $path (keys(%subdependencies)) {
1.1071    raeburn  9656:         $currsubfile{$path} = {};
1.1075.2.35! raeburn  9657:         if (($actionurl eq '/adm/portfolio') ||
        !          9658:             ($actionurl eq '/adm/coursegrp_portfolio')) { 
1.1021    raeburn  9659:             my ($sublistref,$listerror) =
                   9660:                 &Apache::lonnet::dirlist($url.$path,$udom,$uname,$getpropath);
                   9661:             if (ref($sublistref) eq 'ARRAY') {
                   9662:                 foreach my $line (@{$sublistref}) {
                   9663:                     my ($file_name,$rest) = split(/\&/,$line,2);
1.1071    raeburn  9664:                     $currsubfile{$path}{$file_name} = 1;
1.1021    raeburn  9665:                 }
1.984     raeburn  9666:             }
1.987     raeburn  9667:         } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
1.984     raeburn  9668:             if (opendir(my $dir,$url.'/'.$path)) {
                   9669:                 my @subdir_list = grep(!/^\./,readdir($dir));
1.1071    raeburn  9670:                 map {$currsubfile{$path}{$_} = 1;} @subdir_list;
                   9671:             }
1.1075.2.11  raeburn  9672:         } elsif (($actionurl eq '/adm/dependencies') ||
                   9673:                  (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
1.1075.2.35! raeburn  9674:                   ($args->{'context'} eq 'paste')) ||
        !          9675:                  ($actionurl eq "/public/$cdom/$cnum/syllabus")) {
1.1071    raeburn  9676:             if ($env{'request.course.id'} ne '') {
1.1075.2.35! raeburn  9677:                 my $dir;
        !          9678:                 if ($actionurl eq "/public/$cdom/$cnum/syllabus") {
        !          9679:                     $dir = $fileloc;
        !          9680:                 } else {
        !          9681:                     ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
        !          9682:                 }
1.1071    raeburn  9683:                 if ($dir ne '') {
                   9684:                     my ($sublistref,$listerror) =
                   9685:                         &Apache::lonnet::dirlist($dir.$path,$cdom,$cnum,$getpropath,undef,'/');
                   9686:                     if (ref($sublistref) eq 'ARRAY') {
                   9687:                         foreach my $line (@{$sublistref}) {
                   9688:                             my ($file_name,$dom,undef,$testdir,undef,undef,undef,undef,$size,
                   9689:                                 undef,$mtime)=split(/\&/,$line,12);
                   9690:                             unless (($testdir&$dirptr) ||
                   9691:                                     ($file_name =~ /^\.\.?$/)) {
                   9692:                                 $currsubfile{$path}{$file_name} = [$size,$mtime];
                   9693:                             }
                   9694:                         }
                   9695:                     }
                   9696:                 }
1.984     raeburn  9697:             }
                   9698:         }
                   9699:         foreach my $file (keys(%{$subdependencies{$path}})) {
1.1071    raeburn  9700:             if (exists($currsubfile{$path}{$file})) {
1.987     raeburn  9701:                 my $item = $path.'/'.$file;
                   9702:                 unless ($mapping{$item} eq $item) {
                   9703:                     $pathchanges{$item} = 1;
                   9704:                 }
                   9705:                 $existing{$item} = 1;
                   9706:                 $numexisting ++;
                   9707:             } else {
                   9708:                 $newfiles{$path.'/'.$file} = 1;
1.984     raeburn  9709:             }
                   9710:         }
1.1071    raeburn  9711:         if ($actionurl eq '/adm/dependencies') {
                   9712:             foreach my $path (keys(%currsubfile)) {
                   9713:                 if (ref($currsubfile{$path}) eq 'HASH') {
                   9714:                     foreach my $file (keys(%{$currsubfile{$path}})) {
                   9715:                          unless ($subdependencies{$path}{$file}) {
1.1075.2.11  raeburn  9716:                              next if (($rem ne '') &&
                   9717:                                       (($env{"httpref.$rem"."$path/$file"} ne '') ||
                   9718:                                        (ref($navmap) &&
                   9719:                                        (($navmap->getResourceByUrl($rem."$path/$file") ne '') ||
                   9720:                                         (($file =~ /^(.*\.s?html?)\.bak$/i) &&
                   9721:                                          ($navmap->getResourceByUrl($rem."$path/$1")))))));
1.1071    raeburn  9722:                              $unused{$path.'/'.$file} = 1; 
                   9723:                          }
                   9724:                     }
                   9725:                 }
                   9726:             }
                   9727:         }
1.984     raeburn  9728:     }
1.987     raeburn  9729:     my %currfile;
1.1075.2.35! raeburn  9730:     if (($actionurl eq '/adm/portfolio') ||
        !          9731:         ($actionurl eq '/adm/coursegrp_portfolio')) {
1.1021    raeburn  9732:         my ($dirlistref,$listerror) =
                   9733:             &Apache::lonnet::dirlist($url,$udom,$uname,$getpropath);
                   9734:         if (ref($dirlistref) eq 'ARRAY') {
                   9735:             foreach my $line (@{$dirlistref}) {
                   9736:                 my ($file_name,$rest) = split(/\&/,$line,2);
                   9737:                 $currfile{$file_name} = 1;
                   9738:             }
1.984     raeburn  9739:         }
1.987     raeburn  9740:     } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
1.984     raeburn  9741:         if (opendir(my $dir,$url)) {
1.987     raeburn  9742:             my @dir_list = grep(!/^\./,readdir($dir));
1.984     raeburn  9743:             map {$currfile{$_} = 1;} @dir_list;
                   9744:         }
1.1075.2.11  raeburn  9745:     } elsif (($actionurl eq '/adm/dependencies') ||
                   9746:              (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
1.1075.2.35! raeburn  9747:               ($args->{'context'} eq 'paste')) ||
        !          9748:              ($actionurl eq "/public/$cdom/$cnum/syllabus")) {
1.1071    raeburn  9749:         if ($env{'request.course.id'} ne '') {
                   9750:             my ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
                   9751:             if ($dir ne '') {
                   9752:                 my ($dirlistref,$listerror) =
                   9753:                     &Apache::lonnet::dirlist($dir,$cdom,$cnum,$getpropath,undef,'/');
                   9754:                 if (ref($dirlistref) eq 'ARRAY') {
                   9755:                     foreach my $line (@{$dirlistref}) {
                   9756:                         my ($file_name,$dom,undef,$testdir,undef,undef,undef,undef,
                   9757:                             $size,undef,$mtime)=split(/\&/,$line,12);
                   9758:                         unless (($testdir&$dirptr) ||
                   9759:                                 ($file_name =~ /^\.\.?$/)) {
                   9760:                             $currfile{$file_name} = [$size,$mtime];
                   9761:                         }
                   9762:                     }
                   9763:                 }
                   9764:             }
                   9765:         }
1.984     raeburn  9766:     }
                   9767:     foreach my $file (keys(%dependencies)) {
1.1071    raeburn  9768:         if (exists($currfile{$file})) {
1.987     raeburn  9769:             unless ($mapping{$file} eq $file) {
                   9770:                 $pathchanges{$file} = 1;
                   9771:             }
                   9772:             $existing{$file} = 1;
                   9773:             $numexisting ++;
                   9774:         } else {
1.984     raeburn  9775:             $newfiles{$file} = 1;
                   9776:         }
                   9777:     }
1.1071    raeburn  9778:     foreach my $file (keys(%currfile)) {
                   9779:         unless (($file eq $filename) ||
                   9780:                 ($file eq $filename.'.bak') ||
                   9781:                 ($dependencies{$file})) {
1.1075.2.11  raeburn  9782:             if ($actionurl eq '/adm/dependencies') {
1.1075.2.35! raeburn  9783:                 unless ($toplevel =~ m{^\Q/uploaded/$cdom/$cnum/portfolio/syllabus\E}) {
        !          9784:                     next if (($rem ne '') &&
        !          9785:                              (($env{"httpref.$rem".$file} ne '') ||
        !          9786:                               (ref($navmap) &&
        !          9787:                               (($navmap->getResourceByUrl($rem.$file) ne '') ||
        !          9788:                                (($file =~ /^(.*\.s?html?)\.bak$/i) &&
        !          9789:                                 ($navmap->getResourceByUrl($rem.$1)))))));
        !          9790:                 }
1.1075.2.11  raeburn  9791:             }
1.1071    raeburn  9792:             $unused{$file} = 1;
                   9793:         }
                   9794:     }
1.1075.2.11  raeburn  9795:     if (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
                   9796:         ($args->{'context'} eq 'paste')) {
                   9797:         $counter = scalar(keys(%existing));
                   9798:         $numpathchg = scalar(keys(%pathchanges));
                   9799:         return ($output,$counter,$numpathchg,\%existing);
1.1075.2.35! raeburn  9800:     } elsif (($actionurl eq "/public/$cdom/$cnum/syllabus") &&
        !          9801:              (ref($args) eq 'HASH') && ($args->{'context'} eq 'rewrites')) {
        !          9802:         $counter = scalar(keys(%existing));
        !          9803:         $numpathchg = scalar(keys(%pathchanges));
        !          9804:         return ($output,$counter,$numpathchg,\%existing,\%mapping);
1.1075.2.11  raeburn  9805:     }
1.984     raeburn  9806:     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%newfiles)) {
1.1071    raeburn  9807:         if ($actionurl eq '/adm/dependencies') {
                   9808:             next if ($embed_file =~ m{^\w+://});
                   9809:         }
1.660     raeburn  9810:         $upload_output .= &start_data_table_row().
1.1075.2.35! raeburn  9811:                           '<td valign="top"><img src="'.&icon($embed_file).'" />&nbsp;'.
1.1071    raeburn  9812:                           '<span class="LC_filename">'.$embed_file.'</span>';
1.987     raeburn  9813:         unless ($mapping{$embed_file} eq $embed_file) {
1.1075.2.35! raeburn  9814:             $upload_output .= '<br /><span class="LC_info" style="font-size:smaller;">'.
        !          9815:                               &mt('changed from: [_1]',$mapping{$embed_file}).'</span>';
1.987     raeburn  9816:         }
1.1075.2.35! raeburn  9817:         $upload_output .= '</td>';
1.1071    raeburn  9818:         if ($args->{'ignore_remote_references'} && $embed_file =~ m{^\w+://}) { 
1.1075.2.35! raeburn  9819:             $upload_output.='<td align="right">'.
        !          9820:                             '<span class="LC_info LC_fontsize_medium">'.
        !          9821:                             &mt("URL points to web address").'</span>';
1.987     raeburn  9822:             $numremref++;
1.660     raeburn  9823:         } elsif ($args->{'error_on_invalid_names'}
                   9824:             && $embed_file ne &Apache::lonnet::clean_filename($embed_file,{'keep_path' => 1,})) {
1.1075.2.35! raeburn  9825:             $upload_output.='<td align="right"><span class="LC_warning">'.
        !          9826:                             &mt('Invalid characters').'</span>';
1.987     raeburn  9827:             $numinvalid++;
1.660     raeburn  9828:         } else {
1.1075.2.35! raeburn  9829:             $upload_output .= '<td>'.
        !          9830:                               &embedded_file_element('upload_embedded',$counter,
1.987     raeburn  9831:                                                      $embed_file,\%mapping,
1.1071    raeburn  9832:                                                      $allfiles,$codebase,'upload');
                   9833:             $counter ++;
                   9834:             $numnew ++;
1.987     raeburn  9835:         }
                   9836:         $upload_output .= '</td>'.&Apache::loncommon::end_data_table_row()."\n";
                   9837:     }
                   9838:     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%existing)) {
1.1071    raeburn  9839:         if ($actionurl eq '/adm/dependencies') {
                   9840:             my ($size,$mtime) = &get_dependency_details(\%currfile,\%currsubfile,$embed_file);
                   9841:             $modify_output .= &start_data_table_row().
                   9842:                               '<td><a href="'.$path.'/'.$embed_file.'" style="text-decoration:none;">'.
                   9843:                               '<img src="'.&icon($embed_file).'" border="0" />'.
                   9844:                               '&nbsp;<span class="LC_filename">'.$embed_file.'</span></a></td>'.
                   9845:                               '<td>'.$size.'</td>'.
                   9846:                               '<td>'.$mtime.'</td>'.
                   9847:                               '<td><label><input type="checkbox" name="mod_upload_dep" '.
                   9848:                               'onclick="toggleBrowse('."'$counter'".')" id="mod_upload_dep_'.
                   9849:                               $counter.'" value="'.$counter.'" />'.&mt('Yes').'</label>'.
                   9850:                               '<div id="moduploaddep_'.$counter.'" style="display:none;">'.
                   9851:                               &embedded_file_element('upload_embedded',$counter,
                   9852:                                                      $embed_file,\%mapping,
                   9853:                                                      $allfiles,$codebase,'modify').
                   9854:                               '</div></td>'.
                   9855:                               &end_data_table_row()."\n";
                   9856:             $counter ++;
                   9857:         } else {
                   9858:             $upload_output .= &start_data_table_row().
1.1075.2.35! raeburn  9859:                               '<td valign="top"><img src="'.&icon($embed_file).'" />&nbsp;'.
        !          9860:                               '<span class="LC_filename">'.$embed_file.'</span></td>'.
        !          9861:                               '<td align="right"><span class="LC_info LC_fontsize_medium">'.&mt('Already exists').'</span></td>'.
1.1071    raeburn  9862:                               &Apache::loncommon::end_data_table_row()."\n";
                   9863:         }
                   9864:     }
                   9865:     my $delidx = $counter;
                   9866:     foreach my $oldfile (sort {lc($a) cmp lc($b)} keys(%unused)) {
                   9867:         my ($size,$mtime) = &get_dependency_details(\%currfile,\%currsubfile,$oldfile);
                   9868:         $delete_output .= &start_data_table_row().
                   9869:                           '<td><img src="'.&icon($oldfile).'" />'.
                   9870:                           '&nbsp;<span class="LC_filename">'.$oldfile.'</span></td>'.
                   9871:                           '<td>'.$size.'</td>'.
                   9872:                           '<td>'.$mtime.'</td>'.
                   9873:                           '<td><label><input type="checkbox" name="del_upload_dep" '.
                   9874:                           ' value="'.$delidx.'" />'.&mt('Yes').'</label>'.
                   9875:                           &embedded_file_element('upload_embedded',$delidx,
                   9876:                                                  $oldfile,\%mapping,$allfiles,
                   9877:                                                  $codebase,'delete').'</td>'.
                   9878:                           &end_data_table_row()."\n"; 
                   9879:         $numunused ++;
                   9880:         $delidx ++;
1.987     raeburn  9881:     }
                   9882:     if ($upload_output) {
                   9883:         $upload_output = &start_data_table().
                   9884:                          $upload_output.
                   9885:                          &end_data_table()."\n";
                   9886:     }
1.1071    raeburn  9887:     if ($modify_output) {
                   9888:         $modify_output = &start_data_table().
                   9889:                          &start_data_table_header_row().
                   9890:                          '<th>'.&mt('File').'</th>'.
                   9891:                          '<th>'.&mt('Size (KB)').'</th>'.
                   9892:                          '<th>'.&mt('Modified').'</th>'.
                   9893:                          '<th>'.&mt('Upload replacement?').'</th>'.
                   9894:                          &end_data_table_header_row().
                   9895:                          $modify_output.
                   9896:                          &end_data_table()."\n";
                   9897:     }
                   9898:     if ($delete_output) {
                   9899:         $delete_output = &start_data_table().
                   9900:                          &start_data_table_header_row().
                   9901:                          '<th>'.&mt('File').'</th>'.
                   9902:                          '<th>'.&mt('Size (KB)').'</th>'.
                   9903:                          '<th>'.&mt('Modified').'</th>'.
                   9904:                          '<th>'.&mt('Delete?').'</th>'.
                   9905:                          &end_data_table_header_row().
                   9906:                          $delete_output.
                   9907:                          &end_data_table()."\n";
                   9908:     }
1.987     raeburn  9909:     my $applies = 0;
                   9910:     if ($numremref) {
                   9911:         $applies ++;
                   9912:     }
                   9913:     if ($numinvalid) {
                   9914:         $applies ++;
                   9915:     }
                   9916:     if ($numexisting) {
                   9917:         $applies ++;
                   9918:     }
1.1071    raeburn  9919:     if ($counter || $numunused) {
1.987     raeburn  9920:         $output = '<form name="upload_embedded" action="'.$actionurl.'"'.
                   9921:                   ' method="post" enctype="multipart/form-data">'."\n".
1.1071    raeburn  9922:                   $state.'<h3>'.$heading.'</h3>'; 
                   9923:         if ($actionurl eq '/adm/dependencies') {
                   9924:             if ($numnew) {
                   9925:                 $output .= '<h4>'.&mt('Missing dependencies').'</h4>'.
                   9926:                            '<p>'.&mt('The following files need to be uploaded.').'</p>'."\n".
                   9927:                            $upload_output.'<br />'."\n";
                   9928:             }
                   9929:             if ($numexisting) {
                   9930:                 $output .= '<h4>'.&mt('Uploaded dependencies (in use)').'</h4>'.
                   9931:                            '<p>'.&mt('Upload a new file to replace the one currently in use.').'</p>'."\n".
                   9932:                            $modify_output.'<br />'."\n";
                   9933:                            $buttontext = &mt('Save changes');
                   9934:             }
                   9935:             if ($numunused) {
                   9936:                 $output .= '<h4>'.&mt('Unused files').'</h4>'.
                   9937:                            '<p>'.&mt('The following uploaded files are no longer used.').'</p>'."\n".
                   9938:                            $delete_output.'<br />'."\n";
                   9939:                            $buttontext = &mt('Save changes');
                   9940:             }
                   9941:         } else {
                   9942:             $output .= $upload_output.'<br />'."\n";
                   9943:         }
                   9944:         $output .= '<input type ="hidden" name="number_embedded_items" value="'.
                   9945:                    $counter.'" />'."\n";
                   9946:         if ($actionurl eq '/adm/dependencies') { 
                   9947:             $output .= '<input type ="hidden" name="number_newemb_items" value="'.
                   9948:                        $numnew.'" />'."\n";
                   9949:         } elsif ($actionurl eq '') {
1.987     raeburn  9950:             $output .=  '<input type="hidden" name="phase" value="three" />';
                   9951:         }
                   9952:     } elsif ($applies) {
                   9953:         $output = '<b>'.&mt('Referenced files').'</b>:<br />';
                   9954:         if ($applies > 1) {
                   9955:             $output .=  
1.1075.2.35! raeburn  9956:                 &mt('No dependencies need to be uploaded, as one of the following applies to each reference:').'<ul>';
1.987     raeburn  9957:             if ($numremref) {
                   9958:                 $output .= '<li>'.&mt('reference is to a URL which points to another server').'</li>'."\n";
                   9959:             }
                   9960:             if ($numinvalid) {
                   9961:                 $output .= '<li>'.&mt('reference is to file with a name containing invalid characters').'</li>'."\n";
                   9962:             }
                   9963:             if ($numexisting) {
                   9964:                 $output .= '<li>'.&mt('reference is to an existing file at the specified location').'</li>'."\n";
                   9965:             }
                   9966:             $output .= '</ul><br />';
                   9967:         } elsif ($numremref) {
                   9968:             $output .= '<p>'.&mt('None to upload, as all references are to URLs pointing to another server.').'</p>';
                   9969:         } elsif ($numinvalid) {
                   9970:             $output .= '<p>'.&mt('None to upload, as all references are to files with names containing invalid characters.').'</p>';
                   9971:         } elsif ($numexisting) {
                   9972:             $output .= '<p>'.&mt('None to upload, as all references are to existing files.').'</p>';
                   9973:         }
                   9974:         $output .= $upload_output.'<br />';
                   9975:     }
                   9976:     my ($pathchange_output,$chgcount);
1.1071    raeburn  9977:     $chgcount = $counter;
1.987     raeburn  9978:     if (keys(%pathchanges) > 0) {
                   9979:         foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%pathchanges)) {
1.1071    raeburn  9980:             if ($counter) {
1.987     raeburn  9981:                 $output .= &embedded_file_element('pathchange',$chgcount,
                   9982:                                                   $embed_file,\%mapping,
1.1071    raeburn  9983:                                                   $allfiles,$codebase,'change');
1.987     raeburn  9984:             } else {
                   9985:                 $pathchange_output .= 
                   9986:                     &start_data_table_row().
                   9987:                     '<td><input type ="checkbox" name="namechange" value="'.
                   9988:                     $chgcount.'" checked="checked" /></td>'.
                   9989:                     '<td>'.$mapping{$embed_file}.'</td>'.
                   9990:                     '<td>'.$embed_file.
                   9991:                     &embedded_file_element('pathchange',$numpathchg,$embed_file,
1.1071    raeburn  9992:                                            \%mapping,$allfiles,$codebase,'change').
1.987     raeburn  9993:                     '</td>'.&end_data_table_row();
1.660     raeburn  9994:             }
1.987     raeburn  9995:             $numpathchg ++;
                   9996:             $chgcount ++;
1.660     raeburn  9997:         }
                   9998:     }
1.1075.2.35! raeburn  9999:     if (($counter) || ($numunused)) {
1.987     raeburn  10000:         if ($numpathchg) {
                   10001:             $output .= '<input type ="hidden" name="number_pathchange_items" value="'.
                   10002:                        $numpathchg.'" />'."\n";
                   10003:         }
                   10004:         if (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank') || 
                   10005:             ($actionurl eq '/adm/imsimport')) {
                   10006:             $output .= '<input type="hidden" name="phase" value="three" />'."\n";
                   10007:         } elsif ($actionurl eq '/adm/portfolio' || $actionurl eq '/adm/coursegrp_portfolio') {
                   10008:             $output .= '<input type="hidden" name="action" value="upload_embedded" />';
1.1071    raeburn  10009:         } elsif ($actionurl eq '/adm/dependencies') {
                   10010:             $output .= '<input type="hidden" name="action" value="process_changes" />';
1.987     raeburn  10011:         }
1.1075.2.35! raeburn  10012:         $output .= '<input type ="submit" value="'.$buttontext.'" />'."\n".'</form>'."\n";
1.987     raeburn  10013:     } elsif ($numpathchg) {
                   10014:         my %pathchange = ();
                   10015:         $output .= &modify_html_form('pathchange',$actionurl,$state,\%pathchange,$pathchange_output);
                   10016:         if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
                   10017:             $output .= '<p>'.&mt('or').'</p>'; 
1.1075.2.35! raeburn  10018:         }
1.987     raeburn  10019:     }
1.1071    raeburn  10020:     return ($output,$counter,$numpathchg);
1.987     raeburn  10021: }
                   10022: 
                   10023: sub embedded_file_element {
1.1071    raeburn  10024:     my ($context,$num,$embed_file,$mapping,$allfiles,$codebase,$type) = @_;
1.987     raeburn  10025:     return unless ((ref($mapping) eq 'HASH') && (ref($allfiles) eq 'HASH') &&
                   10026:                    (ref($codebase) eq 'HASH'));
                   10027:     my $output;
1.1071    raeburn  10028:     if (($context eq 'upload_embedded') && ($type ne 'delete')) {
1.987     raeburn  10029:        $output = '<input name="embedded_item_'.$num.'" type="file" value="" />'."\n";
                   10030:     }
                   10031:     $output .= '<input name="embedded_orig_'.$num.'" type="hidden" value="'.
                   10032:                &escape($embed_file).'" />';
                   10033:     unless (($context eq 'upload_embedded') && 
                   10034:             ($mapping->{$embed_file} eq $embed_file)) {
                   10035:         $output .='
                   10036:         <input name="embedded_ref_'.$num.'" type="hidden" value="'.&escape($mapping->{$embed_file}).'" />';
                   10037:     }
                   10038:     my $attrib;
                   10039:     if (ref($allfiles->{$mapping->{$embed_file}}) eq 'ARRAY') {
                   10040:         $attrib = &escape(join(':',@{$allfiles->{$mapping->{$embed_file}}}));
                   10041:     }
                   10042:     $output .=
                   10043:         "\n\t\t".
                   10044:         '<input name="embedded_attrib_'.$num.'" type="hidden" value="'.
                   10045:         $attrib.'" />';
                   10046:     if (exists($codebase->{$mapping->{$embed_file}})) {
                   10047:         $output .=
                   10048:             "\n\t\t".
                   10049:             '<input name="codebase_'.$num.'" type="hidden" value="'.
                   10050:             &escape($codebase->{$mapping->{$embed_file}}).'" />';
1.984     raeburn  10051:     }
1.987     raeburn  10052:     return $output;
1.660     raeburn  10053: }
                   10054: 
1.1071    raeburn  10055: sub get_dependency_details {
                   10056:     my ($currfile,$currsubfile,$embed_file) = @_;
                   10057:     my ($size,$mtime,$showsize,$showmtime);
                   10058:     if ((ref($currfile) eq 'HASH') && (ref($currsubfile))) {
                   10059:         if ($embed_file =~ m{/}) {
                   10060:             my ($path,$fname) = split(/\//,$embed_file);
                   10061:             if (ref($currsubfile->{$path}{$fname}) eq 'ARRAY') {
                   10062:                 ($size,$mtime) = @{$currsubfile->{$path}{$fname}};
                   10063:             }
                   10064:         } else {
                   10065:             if (ref($currfile->{$embed_file}) eq 'ARRAY') {
                   10066:                 ($size,$mtime) = @{$currfile->{$embed_file}};
                   10067:             }
                   10068:         }
                   10069:         $showsize = $size/1024.0;
                   10070:         $showsize = sprintf("%.1f",$showsize);
                   10071:         if ($mtime > 0) {
                   10072:             $showmtime = &Apache::lonlocal::locallocaltime($mtime);
                   10073:         }
                   10074:     }
                   10075:     return ($showsize,$showmtime);
                   10076: }
                   10077: 
                   10078: sub ask_embedded_js {
                   10079:     return <<"END";
                   10080: <script type="text/javascript"">
                   10081: // <![CDATA[
                   10082: function toggleBrowse(counter) {
                   10083:     var chkboxid = document.getElementById('mod_upload_dep_'+counter);
                   10084:     var fileid = document.getElementById('embedded_item_'+counter);
                   10085:     var uploaddivid = document.getElementById('moduploaddep_'+counter);
                   10086:     if (chkboxid.checked == true) {
                   10087:         uploaddivid.style.display='block';
                   10088:     } else {
                   10089:         uploaddivid.style.display='none';
                   10090:         fileid.value = '';
                   10091:     }
                   10092: }
                   10093: // ]]>
                   10094: </script>
                   10095: 
                   10096: END
                   10097: }
                   10098: 
1.661     raeburn  10099: sub upload_embedded {
                   10100:     my ($context,$dirpath,$uname,$udom,$dir_root,$url_root,$group,$disk_quota,
1.987     raeburn  10101:         $current_disk_usage,$hiddenstate,$actionurl) = @_;
                   10102:     my (%pathchange,$output,$modifyform,$footer,$returnflag);
1.661     raeburn  10103:     for (my $i=0; $i<$env{'form.number_embedded_items'}; $i++) {
                   10104:         next if (!exists($env{'form.embedded_item_'.$i.'.filename'}));
                   10105:         my $orig_uploaded_filename =
                   10106:             $env{'form.embedded_item_'.$i.'.filename'};
1.987     raeburn  10107:         foreach my $type ('orig','ref','attrib','codebase') {
                   10108:             if ($env{'form.embedded_'.$type.'_'.$i} ne '') {
                   10109:                 $env{'form.embedded_'.$type.'_'.$i} =
                   10110:                     &unescape($env{'form.embedded_'.$type.'_'.$i});
                   10111:             }
                   10112:         }
1.661     raeburn  10113:         my ($path,$fname) =
                   10114:             ($env{'form.embedded_orig_'.$i} =~ m{(.*/)([^/]*)});
                   10115:         # no path, whole string is fname
                   10116:         if (!$fname) { $fname = $env{'form.embedded_orig_'.$i} };
                   10117:         $fname = &Apache::lonnet::clean_filename($fname);
                   10118:         # See if there is anything left
                   10119:         next if ($fname eq '');
                   10120: 
                   10121:         # Check if file already exists as a file or directory.
                   10122:         my ($state,$msg);
                   10123:         if ($context eq 'portfolio') {
                   10124:             my $port_path = $dirpath;
                   10125:             if ($group ne '') {
                   10126:                 $port_path = "groups/$group/$port_path";
                   10127:             }
1.987     raeburn  10128:             ($state,$msg) = &check_for_upload($env{'form.currentpath'}.$path,
                   10129:                                               $fname,$group,'embedded_item_'.$i,
1.661     raeburn  10130:                                               $dir_root,$port_path,$disk_quota,
                   10131:                                               $current_disk_usage,$uname,$udom);
                   10132:             if ($state eq 'will_exceed_quota'
1.984     raeburn  10133:                 || $state eq 'file_locked') {
1.661     raeburn  10134:                 $output .= $msg;
                   10135:                 next;
                   10136:             }
                   10137:         } elsif (($context eq 'author') || ($context eq 'testbank')) {
                   10138:             ($state,$msg) = &check_for_existing($path,$fname,'embedded_item_'.$i);
                   10139:             if ($state eq 'exists') {
                   10140:                 $output .= $msg;
                   10141:                 next;
                   10142:             }
                   10143:         }
                   10144:         # Check if extension is valid
                   10145:         if (($fname =~ /\.(\w+)$/) &&
                   10146:             (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.987     raeburn  10147:             $output .= &mt('Invalid file extension ([_1]) - reserved for LONCAPA use - rename the file with a different extension and re-upload. ',$1).'<br />';
1.661     raeburn  10148:             next;
                   10149:         } elsif (($fname =~ /\.(\w+)$/) &&
                   10150:                  (!defined(&Apache::loncommon::fileembstyle($1)))) {
1.987     raeburn  10151:             $output .= &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1).'<br />';
1.661     raeburn  10152:             next;
                   10153:         } elsif ($fname=~/\.(\d+)\.(\w+)$/) {
1.1075.2.34  raeburn  10154:             $output .= &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2).'<br />';
1.661     raeburn  10155:             next;
                   10156:         }
                   10157:         $env{'form.embedded_item_'.$i.'.filename'}=$fname;
1.1075.2.35! raeburn  10158:         my $subdir = $path;
        !          10159:         $subdir =~ s{/+$}{};
1.661     raeburn  10160:         if ($context eq 'portfolio') {
1.984     raeburn  10161:             my $result;
                   10162:             if ($state eq 'existingfile') {
                   10163:                 $result=
                   10164:                     &Apache::lonnet::userfileupload('embedded_item_'.$i,'existingfile',
1.1075.2.35! raeburn  10165:                                                     $dirpath.$env{'form.currentpath'}.$subdir);
1.661     raeburn  10166:             } else {
1.984     raeburn  10167:                 $result=
                   10168:                     &Apache::lonnet::userfileupload('embedded_item_'.$i,'',
1.987     raeburn  10169:                                                     $dirpath.
1.1075.2.35! raeburn  10170:                                                     $env{'form.currentpath'}.$subdir);
1.984     raeburn  10171:                 if ($result !~ m|^/uploaded/|) {
                   10172:                     $output .= '<span class="LC_error">'
                   10173:                                .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
                   10174:                                ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
                   10175:                                .'</span><br />';
                   10176:                     next;
                   10177:                 } else {
1.987     raeburn  10178:                     $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                   10179:                                $path.$fname.'</span>').'<br />';     
1.984     raeburn  10180:                 }
1.661     raeburn  10181:             }
1.1075.2.35! raeburn  10182:         } elsif (($context eq 'coursedoc') || ($context eq 'syllabus')) {
        !          10183:             my $extendedsubdir = $dirpath.'/'.$subdir;
        !          10184:             $extendedsubdir =~ s{/+$}{};
1.987     raeburn  10185:             my $result =
1.1075.2.35! raeburn  10186:                 &Apache::lonnet::userfileupload('embedded_item_'.$i,$context,$extendedsubdir);
1.987     raeburn  10187:             if ($result !~ m|^/uploaded/|) {
                   10188:                 $output .= '<span class="LC_error">'
                   10189:                            .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
                   10190:                            ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
                   10191:                            .'</span><br />';
                   10192:                     next;
                   10193:             } else {
                   10194:                 $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                   10195:                            $path.$fname.'</span>').'<br />';
1.1075.2.35! raeburn  10196:                 if ($context eq 'syllabus') {
        !          10197:                     &Apache::lonnet::make_public_indefinitely($result);
        !          10198:                 }
1.987     raeburn  10199:             }
1.661     raeburn  10200:         } else {
                   10201: # Save the file
                   10202:             my $target = $env{'form.embedded_item_'.$i};
                   10203:             my $fullpath = $dir_root.$dirpath.'/'.$path;
                   10204:             my $dest = $fullpath.$fname;
                   10205:             my $url = $url_root.$dirpath.'/'.$path.$fname;
1.1027    raeburn  10206:             my @parts=split(/\//,"$dirpath/$path");
1.661     raeburn  10207:             my $count;
                   10208:             my $filepath = $dir_root;
1.1027    raeburn  10209:             foreach my $subdir (@parts) {
                   10210:                 $filepath .= "/$subdir";
                   10211:                 if (!-e $filepath) {
1.661     raeburn  10212:                     mkdir($filepath,0770);
                   10213:                 }
                   10214:             }
                   10215:             my $fh;
                   10216:             if (!open($fh,'>'.$dest)) {
                   10217:                 &Apache::lonnet::logthis('Failed to create '.$dest);
                   10218:                 $output .= '<span class="LC_error">'.
1.1071    raeburn  10219:                            &mt('An error occurred while trying to upload [_1] for embedded element [_2].',
                   10220:                                $orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
1.661     raeburn  10221:                            '</span><br />';
                   10222:             } else {
                   10223:                 if (!print $fh $env{'form.embedded_item_'.$i}) {
                   10224:                     &Apache::lonnet::logthis('Failed to write to '.$dest);
                   10225:                     $output .= '<span class="LC_error">'.
1.1071    raeburn  10226:                               &mt('An error occurred while writing the file [_1] for embedded element [_2].',
                   10227:                                   $orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
1.661     raeburn  10228:                               '</span><br />';
                   10229:                 } else {
1.987     raeburn  10230:                     $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                   10231:                                $url.'</span>').'<br />';
                   10232:                     unless ($context eq 'testbank') {
                   10233:                         $footer .= &mt('View embedded file: [_1]',
                   10234:                                        '<a href="'.$url.'">'.$fname.'</a>').'<br />';
                   10235:                     }
                   10236:                 }
                   10237:                 close($fh);
                   10238:             }
                   10239:         }
                   10240:         if ($env{'form.embedded_ref_'.$i}) {
                   10241:             $pathchange{$i} = 1;
                   10242:         }
                   10243:     }
                   10244:     if ($output) {
                   10245:         $output = '<p>'.$output.'</p>';
                   10246:     }
                   10247:     $output .= &modify_html_form('upload_embedded',$actionurl,$hiddenstate,\%pathchange);
                   10248:     $returnflag = 'ok';
1.1071    raeburn  10249:     my $numpathchgs = scalar(keys(%pathchange));
                   10250:     if ($numpathchgs > 0) {
1.987     raeburn  10251:         if ($context eq 'portfolio') {
                   10252:             $output .= '<p>'.&mt('or').'</p>';
                   10253:         } elsif ($context eq 'testbank') {
1.1071    raeburn  10254:             $output .=  '<p>'.&mt('Or [_1]continue[_2] the testbank import without modifying the reference(s).',
                   10255:                                   '<a href="javascript:document.testbankForm.submit();">','</a>').'</p>';
1.987     raeburn  10256:             $returnflag = 'modify_orightml';
                   10257:         }
                   10258:     }
1.1071    raeburn  10259:     return ($output.$footer,$returnflag,$numpathchgs);
1.987     raeburn  10260: }
                   10261: 
                   10262: sub modify_html_form {
                   10263:     my ($context,$actionurl,$hiddenstate,$pathchange,$pathchgtable) = @_;
                   10264:     my $end = 0;
                   10265:     my $modifyform;
                   10266:     if ($context eq 'upload_embedded') {
                   10267:         return unless (ref($pathchange) eq 'HASH');
                   10268:         if ($env{'form.number_embedded_items'}) {
                   10269:             $end += $env{'form.number_embedded_items'};
                   10270:         }
                   10271:         if ($env{'form.number_pathchange_items'}) {
                   10272:             $end += $env{'form.number_pathchange_items'};
                   10273:         }
                   10274:         if ($end) {
                   10275:             for (my $i=0; $i<$end; $i++) {
                   10276:                 if ($i < $env{'form.number_embedded_items'}) {
                   10277:                     next unless($pathchange->{$i});
                   10278:                 }
                   10279:                 $modifyform .=
                   10280:                     &start_data_table_row().
                   10281:                     '<td><input type ="checkbox" name="namechange" value="'.$i.'" '.
                   10282:                     'checked="checked" /></td>'.
                   10283:                     '<td>'.$env{'form.embedded_ref_'.$i}.
                   10284:                     '<input type="hidden" name="embedded_ref_'.$i.'" value="'.
                   10285:                     &escape($env{'form.embedded_ref_'.$i}).'" />'.
                   10286:                     '<input type="hidden" name="embedded_codebase_'.$i.'" value="'.
                   10287:                     &escape($env{'form.embedded_codebase_'.$i}).'" />'.
                   10288:                     '<input type="hidden" name="embedded_attrib_'.$i.'" value="'.
                   10289:                     &escape($env{'form.embedded_attrib_'.$i}).'" /></td>'.
                   10290:                     '<td>'.$env{'form.embedded_orig_'.$i}.
                   10291:                     '<input type="hidden" name="embedded_orig_'.$i.'" value="'.
                   10292:                     &escape($env{'form.embedded_orig_'.$i}).'" /></td>'.
                   10293:                     &end_data_table_row();
1.1071    raeburn  10294:             }
1.987     raeburn  10295:         }
                   10296:     } else {
                   10297:         $modifyform = $pathchgtable;
                   10298:         if (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
                   10299:             $hiddenstate .= '<input type="hidden" name="phase" value="four" />';
                   10300:         } elsif (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
                   10301:             $hiddenstate .= '<input type="hidden" name="action" value="modify_orightml" />';
                   10302:         }
                   10303:     }
                   10304:     if ($modifyform) {
1.1071    raeburn  10305:         if ($actionurl eq '/adm/dependencies') {
                   10306:             $hiddenstate .= '<input type="hidden" name="action" value="modifyhrefs" />';
                   10307:         }
1.987     raeburn  10308:         return '<h3>'.&mt('Changes in content of HTML file required').'</h3>'."\n".
                   10309:                '<p>'.&mt('Changes need to be made to the reference(s) used for one or more of the dependencies, if your HTML file is to work correctly:').'<ol>'."\n".
                   10310:                '<li>'.&mt('For consistency between the reference(s) and the location of the corresponding stored file within LON-CAPA.').'</li>'."\n".
                   10311:                '<li>'.&mt('To change absolute paths to relative paths, or replace directory traversal via "../" within the original reference.').'</li>'."\n".
                   10312:                '</ol></p>'."\n".'<p>'.
                   10313:                &mt('LON-CAPA can make the required changes to your HTML file.').'</p>'."\n".
                   10314:                '<form method="post" name="refchanger" action="'.$actionurl.'">'.
                   10315:                &start_data_table()."\n".
                   10316:                &start_data_table_header_row().
                   10317:                '<th>'.&mt('Change?').'</th>'.
                   10318:                '<th>'.&mt('Current reference').'</th>'.
                   10319:                '<th>'.&mt('Required reference').'</th>'.
                   10320:                &end_data_table_header_row()."\n".
                   10321:                $modifyform.
                   10322:                &end_data_table().'<br />'."\n".$hiddenstate.
                   10323:                '<input type="submit" name="pathchanges" value="'.&mt('Modify HTML file').'" />'.
                   10324:                '</form>'."\n";
                   10325:     }
                   10326:     return;
                   10327: }
                   10328: 
                   10329: sub modify_html_refs {
1.1075.2.35! raeburn  10330:     my ($context,$dirpath,$uname,$udom,$dir_root,$url) = @_;
1.987     raeburn  10331:     my $container;
                   10332:     if ($context eq 'portfolio') {
                   10333:         $container = $env{'form.container'};
                   10334:     } elsif ($context eq 'coursedoc') {
                   10335:         $container = $env{'form.primaryurl'};
1.1071    raeburn  10336:     } elsif ($context eq 'manage_dependencies') {
                   10337:         (undef,undef,$container) = &Apache::lonnet::decode_symb($env{'form.symb'});
                   10338:         $container = "/$container";
1.1075.2.35! raeburn  10339:     } elsif ($context eq 'syllabus') {
        !          10340:         $container = $url;
1.987     raeburn  10341:     } else {
1.1027    raeburn  10342:         $container = $Apache::lonnet::perlvar{'lonDocRoot'}.$env{'form.filename'};
1.987     raeburn  10343:     }
                   10344:     my (%allfiles,%codebase,$output,$content);
                   10345:     my @changes = &get_env_multiple('form.namechange');
1.1075.2.35! raeburn  10346:     unless ((@changes > 0)  || ($context eq 'syllabus')) {
1.1071    raeburn  10347:         if (wantarray) {
                   10348:             return ('',0,0); 
                   10349:         } else {
                   10350:             return;
                   10351:         }
                   10352:     }
                   10353:     if (($context eq 'portfolio') || ($context eq 'coursedoc') || 
1.1075.2.35! raeburn  10354:         ($context eq 'manage_dependencies') || ($context eq 'syllabus')) {
1.1071    raeburn  10355:         unless ($container =~ m{^/uploaded/\Q$udom\E/\Q$uname\E/}) {
                   10356:             if (wantarray) {
                   10357:                 return ('',0,0);
                   10358:             } else {
                   10359:                 return;
                   10360:             }
                   10361:         } 
1.987     raeburn  10362:         $content = &Apache::lonnet::getfile($container);
1.1071    raeburn  10363:         if ($content eq '-1') {
                   10364:             if (wantarray) {
                   10365:                 return ('',0,0);
                   10366:             } else {
                   10367:                 return;
                   10368:             }
                   10369:         }
1.987     raeburn  10370:     } else {
1.1071    raeburn  10371:         unless ($container =~ /^\Q$dir_root\E/) {
                   10372:             if (wantarray) {
                   10373:                 return ('',0,0);
                   10374:             } else {
                   10375:                 return;
                   10376:             }
                   10377:         } 
1.987     raeburn  10378:         if (open(my $fh,"<$container")) {
                   10379:             $content = join('', <$fh>);
                   10380:             close($fh);
                   10381:         } else {
1.1071    raeburn  10382:             if (wantarray) {
                   10383:                 return ('',0,0);
                   10384:             } else {
                   10385:                 return;
                   10386:             }
1.987     raeburn  10387:         }
                   10388:     }
                   10389:     my ($count,$codebasecount) = (0,0);
                   10390:     my $mm = new File::MMagic;
                   10391:     my $mime_type = $mm->checktype_contents($content);
                   10392:     if ($mime_type eq 'text/html') {
                   10393:         my $parse_result = 
                   10394:             &Apache::lonnet::extract_embedded_items($container,\%allfiles,
                   10395:                                                     \%codebase,\$content);
                   10396:         if ($parse_result eq 'ok') {
                   10397:             foreach my $i (@changes) {
                   10398:                 my $orig = &unescape($env{'form.embedded_orig_'.$i});
                   10399:                 my $ref = &unescape($env{'form.embedded_ref_'.$i});
                   10400:                 if ($allfiles{$ref}) {
                   10401:                     my $newname =  $orig;
                   10402:                     my ($attrib_regexp,$codebase);
1.1006    raeburn  10403:                     $attrib_regexp = &unescape($env{'form.embedded_attrib_'.$i});
1.987     raeburn  10404:                     if ($attrib_regexp =~ /:/) {
                   10405:                         $attrib_regexp =~ s/\:/|/g;
                   10406:                     }
                   10407:                     if ($content =~ m{($attrib_regexp\s*=\s*['"]?)\Q$ref\E(['"]?)}) {
                   10408:                         my $numchg = ($content =~ s{($attrib_regexp\s*=\s*['"]?)\Q$ref\E(['"]?)}{$1$newname$2}gi);
                   10409:                         $count += $numchg;
1.1075.2.35! raeburn  10410:                         $allfiles{$newname} = $allfiles{$ref};
1.987     raeburn  10411:                     }
                   10412:                     if ($env{'form.embedded_codebase_'.$i} ne '') {
1.1006    raeburn  10413:                         $codebase = &unescape($env{'form.embedded_codebase_'.$i});
1.987     raeburn  10414:                         my $numchg = ($content =~ s/(codebase\s*=\s*["']?)\Q$codebase\E(["']?)/$1.$2/i); #' stupid emacs
                   10415:                         $codebasecount ++;
                   10416:                     }
                   10417:                 }
                   10418:             }
1.1075.2.35! raeburn  10419:             my $skiprewrites;
1.987     raeburn  10420:             if ($count || $codebasecount) {
                   10421:                 my $saveresult;
1.1071    raeburn  10422:                 if (($context eq 'portfolio') || ($context eq 'coursedoc') || 
1.1075.2.35! raeburn  10423:                     ($context eq 'manage_dependencies') || ($context eq 'syllabus')) {
1.987     raeburn  10424:                     my $url = &Apache::lonnet::store_edited_file($container,$content,$udom,$uname,\$saveresult);
                   10425:                     if ($url eq $container) {
                   10426:                         my ($fname) = ($container =~ m{/([^/]+)$});
                   10427:                         $output = '<p>'.&mt('Updated [quant,_1,reference] in [_2].',
                   10428:                                             $count,'<span class="LC_filename">'.
1.1071    raeburn  10429:                                             $fname.'</span>').'</p>';
1.987     raeburn  10430:                     } else {
                   10431:                          $output = '<p class="LC_error">'.
                   10432:                                    &mt('Error: update failed for: [_1].',
                   10433:                                    '<span class="LC_filename">'.
                   10434:                                    $container.'</span>').'</p>';
                   10435:                     }
1.1075.2.35! raeburn  10436:                     if ($context eq 'syllabus') {
        !          10437:                         unless ($saveresult eq 'ok') {
        !          10438:                             $skiprewrites = 1;
        !          10439:                         }
        !          10440:                     }
1.987     raeburn  10441:                 } else {
                   10442:                     if (open(my $fh,">$container")) {
                   10443:                         print $fh $content;
                   10444:                         close($fh);
                   10445:                         $output = '<p>'.&mt('Updated [quant,_1,reference] in [_2].',
                   10446:                                   $count,'<span class="LC_filename">'.
                   10447:                                   $container.'</span>').'</p>';
1.661     raeburn  10448:                     } else {
1.987     raeburn  10449:                          $output = '<p class="LC_error">'.
                   10450:                                    &mt('Error: could not update [_1].',
                   10451:                                    '<span class="LC_filename">'.
                   10452:                                    $container.'</span>').'</p>';
1.661     raeburn  10453:                     }
                   10454:                 }
                   10455:             }
1.1075.2.35! raeburn  10456:             if (($context eq 'syllabus') && (!$skiprewrites)) {
        !          10457:                 my ($actionurl,$state);
        !          10458:                 $actionurl = "/public/$udom/$uname/syllabus";
        !          10459:                 my ($ignore,$num,$numpathchanges,$existing,$mapping) =
        !          10460:                     &ask_for_embedded_content($actionurl,$state,\%allfiles,
        !          10461:                                               \%codebase,
        !          10462:                                               {'context' => 'rewrites',
        !          10463:                                                'ignore_remote_references' => 1,});
        !          10464:                 if (ref($mapping) eq 'HASH') {
        !          10465:                     my $rewrites = 0;
        !          10466:                     foreach my $key (keys(%{$mapping})) {
        !          10467:                         next if ($key =~ m{^https?://});
        !          10468:                         my $ref = $mapping->{$key};
        !          10469:                         my $newname = "/uploaded/$udom/$uname/portfolio/syllabus/$key";
        !          10470:                         my $attrib;
        !          10471:                         if (ref($allfiles{$mapping->{$key}}) eq 'ARRAY') {
        !          10472:                             $attrib = join('|',@{$allfiles{$mapping->{$key}}});
        !          10473:                         }
        !          10474:                         if ($content =~ m{($attrib\s*=\s*['"]?)\Q$ref\E(['"]?)}) {
        !          10475:                             my $numchg = ($content =~ s{($attrib\s*=\s*['"]?)\Q$ref\E(['"]?)}{$1$newname$2}gi);
        !          10476:                             $rewrites += $numchg;
        !          10477:                         }
        !          10478:                     }
        !          10479:                     if ($rewrites) {
        !          10480:                         my $saveresult;
        !          10481:                         my $url = &Apache::lonnet::store_edited_file($container,$content,$udom,$uname,\$saveresult);
        !          10482:                         if ($url eq $container) {
        !          10483:                             my ($fname) = ($container =~ m{/([^/]+)$});
        !          10484:                             $output .= '<p>'.&mt('Rewrote [quant,_1,link] as [quant,_1,absolute link] in [_2].',
        !          10485:                                             $count,'<span class="LC_filename">'.
        !          10486:                                             $fname.'</span>').'</p>';
        !          10487:                         } else {
        !          10488:                             $output .= '<p class="LC_error">'.
        !          10489:                                        &mt('Error: could not update links in [_1].',
        !          10490:                                        '<span class="LC_filename">'.
        !          10491:                                        $container.'</span>').'</p>';
        !          10492: 
        !          10493:                         }
        !          10494:                     }
        !          10495:                 }
        !          10496:             }
1.987     raeburn  10497:         } else {
                   10498:             &logthis('Failed to parse '.$container.
                   10499:                      ' to modify references: '.$parse_result);
1.661     raeburn  10500:         }
                   10501:     }
1.1071    raeburn  10502:     if (wantarray) {
                   10503:         return ($output,$count,$codebasecount);
                   10504:     } else {
                   10505:         return $output;
                   10506:     }
1.661     raeburn  10507: }
                   10508: 
                   10509: sub check_for_existing {
                   10510:     my ($path,$fname,$element) = @_;
                   10511:     my ($state,$msg);
                   10512:     if (-d $path.'/'.$fname) {
                   10513:         $state = 'exists';
                   10514:         $msg = &mt('Unable to upload [_1]. A directory by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
                   10515:     } elsif (-e $path.'/'.$fname) {
                   10516:         $state = 'exists';
                   10517:         $msg = &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
                   10518:     }
                   10519:     if ($state eq 'exists') {
                   10520:         $msg = '<span class="LC_error">'.$msg.'</span><br />';
                   10521:     }
                   10522:     return ($state,$msg);
                   10523: }
                   10524: 
                   10525: sub check_for_upload {
                   10526:     my ($path,$fname,$group,$element,$portfolio_root,$port_path,
                   10527:         $disk_quota,$current_disk_usage,$uname,$udom) = @_;
1.985     raeburn  10528:     my $filesize = length($env{'form.'.$element});
                   10529:     if (!$filesize) {
                   10530:         my $msg = '<span class="LC_error">'.
                   10531:                   &mt('Unable to upload [_1]. (size = [_2] bytes)', 
                   10532:                       '<span class="LC_filename">'.$fname.'</span>',
                   10533:                       $filesize).'<br />'.
1.1007    raeburn  10534:                   &mt('Either the file you attempted to upload was empty, or your web browser was unable to read its contents.').'<br />'.
1.985     raeburn  10535:                   '</span>';
                   10536:         return ('zero_bytes',$msg);
                   10537:     }
                   10538:     $filesize =  $filesize/1000; #express in k (1024?)
1.661     raeburn  10539:     my $getpropath = 1;
1.1021    raeburn  10540:     my ($dirlistref,$listerror) =
                   10541:          &Apache::lonnet::dirlist($portfolio_root.$path,$udom,$uname,$getpropath);
1.661     raeburn  10542:     my $found_file = 0;
                   10543:     my $locked_file = 0;
1.991     raeburn  10544:     my @lockers;
                   10545:     my $navmap;
                   10546:     if ($env{'request.course.id'}) {
                   10547:         $navmap = Apache::lonnavmaps::navmap->new();
                   10548:     }
1.1021    raeburn  10549:     if (ref($dirlistref) eq 'ARRAY') {
                   10550:         foreach my $line (@{$dirlistref}) {
                   10551:             my ($file_name,$rest)=split(/\&/,$line,2);
                   10552:             if ($file_name eq $fname){
                   10553:                 $file_name = $path.$file_name;
                   10554:                 if ($group ne '') {
                   10555:                     $file_name = $group.$file_name;
                   10556:                 }
                   10557:                 $found_file = 1;
                   10558:                 if (&Apache::lonnet::is_locked($file_name,$udom,$uname,\@lockers) eq 'true') {
                   10559:                     foreach my $lock (@lockers) {
                   10560:                         if (ref($lock) eq 'ARRAY') {
                   10561:                             my ($symb,$crsid) = @{$lock};
                   10562:                             if ($crsid eq $env{'request.course.id'}) {
                   10563:                                 if (ref($navmap)) {
                   10564:                                     my $res = $navmap->getBySymb($symb);
                   10565:                                     foreach my $part (@{$res->parts()}) { 
                   10566:                                         my ($slot_status,$slot_time,$slot_name)=$res->check_for_slot($part);
                   10567:                                         unless (($slot_status == $res->RESERVED) ||
                   10568:                                                 ($slot_status == $res->RESERVED_LOCATION)) {
                   10569:                                             $locked_file = 1;
                   10570:                                         }
1.991     raeburn  10571:                                     }
1.1021    raeburn  10572:                                 } else {
                   10573:                                     $locked_file = 1;
1.991     raeburn  10574:                                 }
                   10575:                             } else {
                   10576:                                 $locked_file = 1;
                   10577:                             }
                   10578:                         }
1.1021    raeburn  10579:                    }
                   10580:                 } else {
                   10581:                     my @info = split(/\&/,$rest);
                   10582:                     my $currsize = $info[6]/1000;
                   10583:                     if ($currsize < $filesize) {
                   10584:                         my $extra = $filesize - $currsize;
                   10585:                         if (($current_disk_usage + $extra) > $disk_quota) {
                   10586:                             my $msg = '<span class="LC_error">'.
                   10587:                                       &mt('Unable to upload [_1]. (size = [_2] kilobytes). Disk quota will be exceeded if existing (smaller) file with same name (size = [_3] kilobytes) is replaced.',
                   10588:                                           '<span class="LC_filename">'.$fname.'</span>',$filesize,$currsize).'</span>'.
                   10589:                                       '<br />'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',
                   10590:                                                    $disk_quota,$current_disk_usage);
                   10591:                             return ('will_exceed_quota',$msg);
                   10592:                         }
1.984     raeburn  10593:                     }
                   10594:                 }
1.661     raeburn  10595:             }
                   10596:         }
                   10597:     }
                   10598:     if (($current_disk_usage + $filesize) > $disk_quota){
                   10599:         my $msg = '<span class="LC_error">'.
                   10600:                 &mt('Unable to upload [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.','<span class="LC_filename">'.$fname.'</span>',$filesize).'</span>'.
                   10601:                   '<br />'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',$disk_quota,$current_disk_usage);
                   10602:         return ('will_exceed_quota',$msg);
                   10603:     } elsif ($found_file) {
                   10604:         if ($locked_file) {
                   10605:             my $msg = '<span class="LC_error">';
                   10606:             $msg .= &mt('Unable to upload [_1]. A locked file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>','<span class="LC_filename">'.$port_path.$env{'form.currentpath'}.'</span>');
                   10607:             $msg .= '</span><br />';
                   10608:             $msg .= &mt('You will be able to rename or delete existing [_1] after a grade has been assigned.','<span class="LC_filename">'.$fname.'</span>');
                   10609:             return ('file_locked',$msg);
                   10610:         } else {
                   10611:             my $msg = '<span class="LC_error">';
1.984     raeburn  10612:             $msg .= &mt(' A file by that name: [_1] was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$port_path.$env{'form.currentpath'});
1.661     raeburn  10613:             $msg .= '</span>';
1.984     raeburn  10614:             return ('existingfile',$msg);
1.661     raeburn  10615:         }
                   10616:     }
                   10617: }
                   10618: 
1.987     raeburn  10619: sub check_for_traversal {
                   10620:     my ($path,$url,$toplevel) = @_;
                   10621:     my @parts=split(/\//,$path);
                   10622:     my $cleanpath;
                   10623:     my $fullpath = $url;
                   10624:     for (my $i=0;$i<@parts;$i++) {
                   10625:         next if ($parts[$i] eq '.');
                   10626:         if ($parts[$i] eq '..') {
                   10627:             $fullpath =~ s{([^/]+/)$}{};
                   10628:         } else {
                   10629:             $fullpath .= $parts[$i].'/';
                   10630:         }
                   10631:     }
                   10632:     if ($fullpath =~ /^\Q$url\E(.*)$/) {
                   10633:         $cleanpath = $1;
                   10634:     } elsif ($fullpath =~ /^\Q$toplevel\E(.*)$/) {
                   10635:         my $curr_toprel = $1;
                   10636:         my @parts = split(/\//,$curr_toprel);
                   10637:         my ($url_toprel) = ($url =~ /^\Q$toplevel\E(.*)$/);
                   10638:         my @urlparts = split(/\//,$url_toprel);
                   10639:         my $doubledots;
                   10640:         my $startdiff = -1;
                   10641:         for (my $i=0; $i<@urlparts; $i++) {
                   10642:             if ($startdiff == -1) {
                   10643:                 unless ($urlparts[$i] eq $parts[$i]) {
                   10644:                     $startdiff = $i;
                   10645:                     $doubledots .= '../';
                   10646:                 }
                   10647:             } else {
                   10648:                 $doubledots .= '../';
                   10649:             }
                   10650:         }
                   10651:         if ($startdiff > -1) {
                   10652:             $cleanpath = $doubledots;
                   10653:             for (my $i=$startdiff; $i<@parts; $i++) {
                   10654:                 $cleanpath .= $parts[$i].'/';
                   10655:             }
                   10656:         }
                   10657:     }
                   10658:     $cleanpath =~ s{(/)$}{};
                   10659:     return $cleanpath;
                   10660: }
1.31      albertel 10661: 
1.1053    raeburn  10662: sub is_archive_file {
                   10663:     my ($mimetype) = @_;
                   10664:     if (($mimetype eq 'application/octet-stream') ||
                   10665:         ($mimetype eq 'application/x-stuffit') ||
                   10666:         ($mimetype =~ m{^application/(x\-)?(compressed|tar|zip|tgz|gz|gtar|gzip|gunzip|bz|bz2|bzip2)})) {
                   10667:         return 1;
                   10668:     }
                   10669:     return;
                   10670: }
                   10671: 
                   10672: sub decompress_form {
1.1065    raeburn  10673:     my ($mimetype,$archiveurl,$action,$noextract,$hiddenelements,$dirlist) = @_;
1.1053    raeburn  10674:     my %lt = &Apache::lonlocal::texthash (
                   10675:         this => 'This file is an archive file.',
1.1067    raeburn  10676:         camt => 'This file is a Camtasia archive file.',
1.1065    raeburn  10677:         itsc => 'Its contents are as follows:',
1.1053    raeburn  10678:         youm => 'You may wish to extract its contents.',
                   10679:         extr => 'Extract contents',
1.1067    raeburn  10680:         auto => 'LON-CAPA can process the files automatically, or you can decide how each should be handled.',
                   10681:         proa => 'Process automatically?',
1.1053    raeburn  10682:         yes  => 'Yes',
                   10683:         no   => 'No',
1.1067    raeburn  10684:         fold => 'Title for folder containing movie',
                   10685:         movi => 'Title for page containing embedded movie', 
1.1053    raeburn  10686:     );
1.1065    raeburn  10687:     my $fileloc = &Apache::lonnet::filelocation(undef,$archiveurl);
1.1067    raeburn  10688:     my ($is_camtasia,$topdir,%toplevel,@paths);
1.1065    raeburn  10689:     my $info = &list_archive_contents($fileloc,\@paths);
                   10690:     if (@paths) {
                   10691:         foreach my $path (@paths) {
                   10692:             $path =~ s{^/}{};
1.1067    raeburn  10693:             if ($path =~ m{^([^/]+)/$}) {
                   10694:                 $topdir = $1;
                   10695:             }
1.1065    raeburn  10696:             if ($path =~ m{^([^/]+)/}) {
                   10697:                 $toplevel{$1} = $path;
                   10698:             } else {
                   10699:                 $toplevel{$path} = $path;
                   10700:             }
                   10701:         }
                   10702:     }
1.1067    raeburn  10703:     if ($mimetype =~ m{^application/(x\-)?(compressed|zip)}) {
                   10704:         my @camtasia = ("$topdir/","$topdir/index.html",
                   10705:                         "$topdir/media/",
                   10706:                         "$topdir/media/$topdir.mp4",
                   10707:                         "$topdir/media/FirstFrame.png",
                   10708:                         "$topdir/media/player.swf",
                   10709:                         "$topdir/media/swfobject.js",
                   10710:                         "$topdir/media/expressInstall.swf");
                   10711:         my @diffs = &compare_arrays(\@paths,\@camtasia);
                   10712:         if (@diffs == 0) {
                   10713:             $is_camtasia = 1;
                   10714:         }
                   10715:     }
                   10716:     my $output;
                   10717:     if ($is_camtasia) {
                   10718:         $output = <<"ENDCAM";
                   10719: <script type="text/javascript" language="Javascript">
                   10720: // <![CDATA[
                   10721: 
                   10722: function camtasiaToggle() {
                   10723:     for (var i=0; i<document.uploaded_decompress.autoextract_camtasia.length; i++) {
                   10724:         if (document.uploaded_decompress.autoextract_camtasia[i].checked) {
                   10725:             if (document.uploaded_decompress.autoextract_camtasia[i].value == 1) {
                   10726: 
                   10727:                 document.getElementById('camtasia_titles').style.display='block';
                   10728:             } else {
                   10729:                 document.getElementById('camtasia_titles').style.display='none';
                   10730:             }
                   10731:         }
                   10732:     }
                   10733:     return;
                   10734: }
                   10735: 
                   10736: // ]]>
                   10737: </script>
                   10738: <p>$lt{'camt'}</p>
                   10739: ENDCAM
1.1065    raeburn  10740:     } else {
1.1067    raeburn  10741:         $output = '<p>'.$lt{'this'};
                   10742:         if ($info eq '') {
                   10743:             $output .= ' '.$lt{'youm'}.'</p>'."\n";
                   10744:         } else {
                   10745:             $output .= ' '.$lt{'itsc'}.'</p>'."\n".
                   10746:                        '<div><pre>'.$info.'</pre></div>';
                   10747:         }
1.1065    raeburn  10748:     }
1.1067    raeburn  10749:     $output .= '<form name="uploaded_decompress" action="'.$action.'" method="post">'."\n";
1.1065    raeburn  10750:     my $duplicates;
                   10751:     my $num = 0;
                   10752:     if (ref($dirlist) eq 'ARRAY') {
                   10753:         foreach my $item (@{$dirlist}) {
                   10754:             if (ref($item) eq 'ARRAY') {
                   10755:                 if (exists($toplevel{$item->[0]})) {
                   10756:                     $duplicates .= 
                   10757:                         &start_data_table_row().
                   10758:                         '<td><label><input type="radio" name="archive_overwrite_'.$num.'" '.
                   10759:                         'value="0" checked="checked" />'.&mt('No').'</label>'.
                   10760:                         '&nbsp;<label><input type="radio" name="archive_overwrite_'.$num.'" '.
                   10761:                         'value="1" />'.&mt('Yes').'</label>'.
                   10762:                         '<input type="hidden" name="archive_overwrite_name_'.$num.'" value="'.$item->[0].'" /></td>'."\n".
                   10763:                         '<td>'.$item->[0].'</td>';
                   10764:                     if ($item->[2]) {
                   10765:                         $duplicates .= '<td>'.&mt('Directory').'</td>';
                   10766:                     } else {
                   10767:                         $duplicates .= '<td>'.&mt('File').'</td>';
                   10768:                     }
                   10769:                     $duplicates .= '<td>'.$item->[3].'</td>'.
                   10770:                                    '<td>'.
                   10771:                                    &Apache::lonlocal::locallocaltime($item->[4]).
                   10772:                                    '</td>'.
                   10773:                                    &end_data_table_row();
                   10774:                     $num ++;
                   10775:                 }
                   10776:             }
                   10777:         }
                   10778:     }
                   10779:     my $itemcount;
                   10780:     if (@paths > 0) {
                   10781:         $itemcount = scalar(@paths);
                   10782:     } else {
                   10783:         $itemcount = 1;
                   10784:     }
1.1067    raeburn  10785:     if ($is_camtasia) {
                   10786:         $output .= $lt{'auto'}.'<br />'.
                   10787:                    '<span class="LC_nobreak">'.$lt{'proa'}.'<label>'.
                   10788:                    '<input type="radio" name="autoextract_camtasia" value="1" onclick="javascript:camtasiaToggle();" checked="checked" />'.
                   10789:                    $lt{'yes'}.'</label>&nbsp;<label>'.
                   10790:                    '<input type="radio" name="autoextract_camtasia" value="0" onclick="javascript:camtasiaToggle();" />'.
                   10791:                    $lt{'no'}.'</label></span><br />'.
                   10792:                    '<div id="camtasia_titles" style="display:block">'.
                   10793:                    &Apache::lonhtmlcommon::start_pick_box().
                   10794:                    &Apache::lonhtmlcommon::row_title($lt{'fold'}).
                   10795:                    '<input type="textbox" name="camtasia_foldername" value="'.$env{'form.comment'}.'" />'."\n".
                   10796:                    &Apache::lonhtmlcommon::row_closure().
                   10797:                    &Apache::lonhtmlcommon::row_title($lt{'movi'}).
                   10798:                    '<input type="textbox" name="camtasia_moviename" value="" />'."\n".
                   10799:                    &Apache::lonhtmlcommon::row_closure(1).
                   10800:                    &Apache::lonhtmlcommon::end_pick_box().
                   10801:                    '</div>';
                   10802:     }
1.1065    raeburn  10803:     $output .= 
                   10804:         '<input type="hidden" name="archive_overwrite_total" value="'.$num.'" />'.
1.1067    raeburn  10805:         '<input type="hidden" name="archive_itemcount" value="'.$itemcount.'" />'.
                   10806:         "\n";
1.1065    raeburn  10807:     if ($duplicates ne '') {
                   10808:         $output .= '<p><span class="LC_warning">'.
                   10809:                    &mt('Warning: decompression of the archive will overwrite the following items which already exist:').'</span><br />'.  
                   10810:                    &start_data_table().
                   10811:                    &start_data_table_header_row().
                   10812:                    '<th>'.&mt('Overwrite?').'</th>'.
                   10813:                    '<th>'.&mt('Name').'</th>'.
                   10814:                    '<th>'.&mt('Type').'</th>'.
                   10815:                    '<th>'.&mt('Size').'</th>'.
                   10816:                    '<th>'.&mt('Last modified').'</th>'.
                   10817:                    &end_data_table_header_row().
                   10818:                    $duplicates.
                   10819:                    &end_data_table().
                   10820:                    '</p>';
                   10821:     }
1.1067    raeburn  10822:     $output .= '<input type="hidden" name="archiveurl" value="'.$archiveurl.'" />'."\n";
1.1053    raeburn  10823:     if (ref($hiddenelements) eq 'HASH') {
                   10824:         foreach my $hidden (sort(keys(%{$hiddenelements}))) {
                   10825:             $output .= '<input type="hidden" name="'.$hidden.'" value="'.$hiddenelements->{$hidden}.'" />'."\n";
                   10826:         }
                   10827:     }
                   10828:     $output .= <<"END";
1.1067    raeburn  10829: <br />
1.1053    raeburn  10830: <input type="submit" name="decompress" value="$lt{'extr'}" />
                   10831: </form>
                   10832: $noextract
                   10833: END
                   10834:     return $output;
                   10835: }
                   10836: 
1.1065    raeburn  10837: sub decompression_utility {
                   10838:     my ($program) = @_;
                   10839:     my @utilities = ('tar','gunzip','bunzip2','unzip'); 
                   10840:     my $location;
                   10841:     if (grep(/^\Q$program\E$/,@utilities)) { 
                   10842:         foreach my $dir ('/bin/','/usr/bin/','/usr/local/bin/','/sbin/',
                   10843:                          '/usr/sbin/') {
                   10844:             if (-x $dir.$program) {
                   10845:                 $location = $dir.$program;
                   10846:                 last;
                   10847:             }
                   10848:         }
                   10849:     }
                   10850:     return $location;
                   10851: }
                   10852: 
                   10853: sub list_archive_contents {
                   10854:     my ($file,$pathsref) = @_;
                   10855:     my (@cmd,$output);
                   10856:     my $needsregexp;
                   10857:     if ($file =~ /\.zip$/) {
                   10858:         @cmd = (&decompression_utility('unzip'),"-l");
                   10859:         $needsregexp = 1;
                   10860:     } elsif (($file =~ m/\.tar\.gz$/) ||
                   10861:              ($file =~ /\.tgz$/)) {
                   10862:         @cmd = (&decompression_utility('tar'),"-ztf");
                   10863:     } elsif ($file =~ /\.tar\.bz2$/) {
                   10864:         @cmd = (&decompression_utility('tar'),"-jtf");
                   10865:     } elsif ($file =~ m|\.tar$|) {
                   10866:         @cmd = (&decompression_utility('tar'),"-tf");
                   10867:     }
                   10868:     if (@cmd) {
                   10869:         undef($!);
                   10870:         undef($@);
                   10871:         if (open(my $fh,"-|", @cmd, $file)) {
                   10872:             while (my $line = <$fh>) {
                   10873:                 $output .= $line;
                   10874:                 chomp($line);
                   10875:                 my $item;
                   10876:                 if ($needsregexp) {
                   10877:                     ($item) = ($line =~ /^\s*\d+\s+[\d\-]+\s+[\d:]+\s*(.+)$/); 
                   10878:                 } else {
                   10879:                     $item = $line;
                   10880:                 }
                   10881:                 if ($item ne '') {
                   10882:                     unless (grep(/^\Q$item\E$/,@{$pathsref})) {
                   10883:                         push(@{$pathsref},$item);
                   10884:                     } 
                   10885:                 }
                   10886:             }
                   10887:             close($fh);
                   10888:         }
                   10889:     }
                   10890:     return $output;
                   10891: }
                   10892: 
1.1053    raeburn  10893: sub decompress_uploaded_file {
                   10894:     my ($file,$dir) = @_;
                   10895:     &Apache::lonnet::appenv({'cgi.file' => $file});
                   10896:     &Apache::lonnet::appenv({'cgi.dir' => $dir});
                   10897:     my $result = &Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
                   10898:     my ($handle) = ($env{'user.environment'} =~m{/([^/]+)\.id$});
                   10899:     my $lonidsdir = $Apache::lonnet::perlvar{'lonIDsDir'};
                   10900:     &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle,1);
                   10901:     my $decompressed = $env{'cgi.decompressed'};
                   10902:     &Apache::lonnet::delenv('cgi.file');
                   10903:     &Apache::lonnet::delenv('cgi.dir');
                   10904:     &Apache::lonnet::delenv('cgi.decompressed');
                   10905:     return ($decompressed,$result);
                   10906: }
                   10907: 
1.1055    raeburn  10908: sub process_decompression {
                   10909:     my ($docudom,$docuname,$file,$destination,$dir_root,$hiddenelem) = @_;
                   10910:     my ($dir,$error,$warning,$output);
                   10911:     if ($file !~ /\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
1.1075.2.34  raeburn  10912:         $error = &mt('Filename not a supported archive file type.').
                   10913:                  '<br />'.&mt('Filename should end with one of: [_1].',
1.1055    raeburn  10914:                               '.zip, .tar, .bz2, .gz, .tar.gz, .tar.bz2, .tgz');
                   10915:     } else {
                   10916:         my $docuhome = &Apache::lonnet::homeserver($docuname,$docudom);
                   10917:         if ($docuhome eq 'no_host') {
                   10918:             $error = &mt('Could not determine home server for course.');
                   10919:         } else {
                   10920:             my @ids=&Apache::lonnet::current_machine_ids();
                   10921:             my $currdir = "$dir_root/$destination";
                   10922:             if (grep(/^\Q$docuhome\E$/,@ids)) {
                   10923:                 $dir = &LONCAPA::propath($docudom,$docuname).
                   10924:                        "$dir_root/$destination";
                   10925:             } else {
                   10926:                 $dir = $Apache::lonnet::perlvar{'lonDocRoot'}.
                   10927:                        "$dir_root/$docudom/$docuname/$destination";
                   10928:                 unless (&Apache::lonnet::repcopy_userfile("$dir/$file") eq 'ok') {
                   10929:                     $error = &mt('Archive file not found.');
                   10930:                 }
                   10931:             }
1.1065    raeburn  10932:             my (@to_overwrite,@to_skip);
                   10933:             if ($env{'form.archive_overwrite_total'} > 0) {
                   10934:                 my $total = $env{'form.archive_overwrite_total'};
                   10935:                 for (my $i=0; $i<$total; $i++) {
                   10936:                     if ($env{'form.archive_overwrite_'.$i} == 1) {
                   10937:                         push(@to_overwrite,$env{'form.archive_overwrite_name_'.$i});
                   10938:                     } elsif ($env{'form.archive_overwrite_'.$i} == 0) {
                   10939:                         push(@to_skip,$env{'form.archive_overwrite_name_'.$i});
                   10940:                     }
                   10941:                 }
                   10942:             }
                   10943:             my $numskip = scalar(@to_skip);
                   10944:             if (($numskip > 0) && 
                   10945:                 ($numskip == $env{'form.archive_itemcount'})) {
                   10946:                 $warning = &mt('All items in the archive file already exist, and no overwriting of existing files has been requested.');         
                   10947:             } elsif ($dir eq '') {
1.1055    raeburn  10948:                 $error = &mt('Directory containing archive file unavailable.');
                   10949:             } elsif (!$error) {
1.1065    raeburn  10950:                 my ($decompressed,$display);
                   10951:                 if ($numskip > 0) {
                   10952:                     my $tempdir = time.'_'.$$.int(rand(10000));
                   10953:                     mkdir("$dir/$tempdir",0755);
                   10954:                     system("mv $dir/$file $dir/$tempdir/$file");
                   10955:                     ($decompressed,$display) = 
                   10956:                         &decompress_uploaded_file($file,"$dir/$tempdir");
                   10957:                     foreach my $item (@to_skip) {
                   10958:                         if (($item ne '') && ($item !~ /\.\./)) {
                   10959:                             if (-f "$dir/$tempdir/$item") { 
                   10960:                                 unlink("$dir/$tempdir/$item");
                   10961:                             } elsif (-d "$dir/$tempdir/$item") {
                   10962:                                 system("rm -rf $dir/$tempdir/$item");
                   10963:                             }
                   10964:                         }
                   10965:                     }
                   10966:                     system("mv $dir/$tempdir/* $dir");
                   10967:                     rmdir("$dir/$tempdir");   
                   10968:                 } else {
                   10969:                     ($decompressed,$display) = 
                   10970:                         &decompress_uploaded_file($file,$dir);
                   10971:                 }
1.1055    raeburn  10972:                 if ($decompressed eq 'ok') {
1.1065    raeburn  10973:                     $output = '<p class="LC_info">'.
                   10974:                               &mt('Files extracted successfully from archive.').
                   10975:                               '</p>'."\n";
1.1055    raeburn  10976:                     my ($warning,$result,@contents);
                   10977:                     my ($newdirlistref,$newlisterror) =
                   10978:                         &Apache::lonnet::dirlist($currdir,$docudom,
                   10979:                                                  $docuname,1);
                   10980:                     my (%is_dir,%changes,@newitems);
                   10981:                     my $dirptr = 16384;
1.1065    raeburn  10982:                     if (ref($newdirlistref) eq 'ARRAY') {
1.1055    raeburn  10983:                         foreach my $dir_line (@{$newdirlistref}) {
                   10984:                             my ($item,undef,undef,$testdir)=split(/\&/,$dir_line,5);
1.1065    raeburn  10985:                             unless (($item =~ /^\.+$/) || ($item eq $file) || 
                   10986:                                     ((@to_skip > 0) && (grep(/^\Q$item\E$/,@to_skip)))) {
1.1055    raeburn  10987:                                 push(@newitems,$item);
                   10988:                                 if ($dirptr&$testdir) {
                   10989:                                     $is_dir{$item} = 1;
                   10990:                                 }
                   10991:                                 $changes{$item} = 1;
                   10992:                             }
                   10993:                         }
                   10994:                     }
                   10995:                     if (keys(%changes) > 0) {
                   10996:                         foreach my $item (sort(@newitems)) {
                   10997:                             if ($changes{$item}) {
                   10998:                                 push(@contents,$item);
                   10999:                             }
                   11000:                         }
                   11001:                     }
                   11002:                     if (@contents > 0) {
1.1067    raeburn  11003:                         my $wantform;
                   11004:                         unless ($env{'form.autoextract_camtasia'}) {
                   11005:                             $wantform = 1;
                   11006:                         }
1.1056    raeburn  11007:                         my (%children,%parent,%dirorder,%titles);
1.1055    raeburn  11008:                         my ($count,$datatable) = &get_extracted($docudom,$docuname,
                   11009:                                                                 $currdir,\%is_dir,
                   11010:                                                                 \%children,\%parent,
1.1056    raeburn  11011:                                                                 \@contents,\%dirorder,
                   11012:                                                                 \%titles,$wantform);
1.1055    raeburn  11013:                         if ($datatable ne '') {
                   11014:                             $output .= &archive_options_form('decompressed',$datatable,
                   11015:                                                              $count,$hiddenelem);
1.1065    raeburn  11016:                             my $startcount = 6;
1.1055    raeburn  11017:                             $output .= &archive_javascript($startcount,$count,
1.1056    raeburn  11018:                                                            \%titles,\%children);
1.1055    raeburn  11019:                         }
1.1067    raeburn  11020:                         if ($env{'form.autoextract_camtasia'}) {
                   11021:                             my %displayed;
                   11022:                             my $total = 1;
                   11023:                             $env{'form.archive_directory'} = [];
                   11024:                             foreach my $i (sort { $a <=> $b } keys(%dirorder)) {
                   11025:                                 my $path = join('/',map { $titles{$_}; } @{$dirorder{$i}});
                   11026:                                 $path =~ s{/$}{};
                   11027:                                 my $item;
                   11028:                                 if ($path ne '') {
                   11029:                                     $item = "$path/$titles{$i}";
                   11030:                                 } else {
                   11031:                                     $item = $titles{$i};
                   11032:                                 }
                   11033:                                 $env{'form.archive_content_'.$i} = "$dir_root/$destination/$item";
                   11034:                                 if ($item eq $contents[0]) {
                   11035:                                     push(@{$env{'form.archive_directory'}},$i);
                   11036:                                     $env{'form.archive_'.$i} = 'display';
                   11037:                                     $env{'form.archive_title_'.$i} = $env{'form.camtasia_foldername'};
                   11038:                                     $displayed{'folder'} = $i;
                   11039:                                 } elsif ($item eq "$contents[0]/index.html") {
                   11040:                                     $env{'form.archive_'.$i} = 'display';
                   11041:                                     $env{'form.archive_title_'.$i} = $env{'form.camtasia_moviename'};
                   11042:                                     $displayed{'web'} = $i;
                   11043:                                 } else {
                   11044:                                     if ($item eq "$contents[0]/media") {
                   11045:                                         push(@{$env{'form.archive_directory'}},$i);
                   11046:                                     }
                   11047:                                     $env{'form.archive_'.$i} = 'dependency';
                   11048:                                 }
                   11049:                                 $total ++;
                   11050:                             }
                   11051:                             for (my $i=1; $i<$total; $i++) {
                   11052:                                 next if ($i == $displayed{'web'});
                   11053:                                 next if ($i == $displayed{'folder'});
                   11054:                                 $env{'form.archive_dependent_on_'.$i} = $displayed{'web'};
                   11055:                             }
                   11056:                             $env{'form.phase'} = 'decompress_cleanup';
                   11057:                             $env{'form.archivedelete'} = 1;
                   11058:                             $env{'form.archive_count'} = $total-1;
                   11059:                             $output .=
                   11060:                                 &process_extracted_files('coursedocs',$docudom,
                   11061:                                                          $docuname,$destination,
                   11062:                                                          $dir_root,$hiddenelem);
                   11063:                         }
1.1055    raeburn  11064:                     } else {
                   11065:                         $warning = &mt('No new items extracted from archive file.');
                   11066:                     }
                   11067:                 } else {
                   11068:                     $output = $display;
                   11069:                     $error = &mt('An error occurred during extraction from the archive file.');
                   11070:                 }
                   11071:             }
                   11072:         }
                   11073:     }
                   11074:     if ($error) {
                   11075:         $output .= '<p class="LC_error">'.&mt('Not extracted.').'<br />'.
                   11076:                    $error.'</p>'."\n";
                   11077:     }
                   11078:     if ($warning) {
                   11079:         $output .= '<p class="LC_warning">'.$warning.'</p>'."\n";
                   11080:     }
                   11081:     return $output;
                   11082: }
                   11083: 
                   11084: sub get_extracted {
1.1056    raeburn  11085:     my ($docudom,$docuname,$currdir,$is_dir,$children,$parent,$contents,$dirorder,
                   11086:         $titles,$wantform) = @_;
1.1055    raeburn  11087:     my $count = 0;
                   11088:     my $depth = 0;
                   11089:     my $datatable;
1.1056    raeburn  11090:     my @hierarchy;
1.1055    raeburn  11091:     return unless ((ref($is_dir) eq 'HASH') && (ref($children) eq 'HASH') &&
1.1056    raeburn  11092:                    (ref($parent) eq 'HASH') && (ref($contents) eq 'ARRAY') &&
                   11093:                    (ref($dirorder) eq 'HASH') && (ref($titles) eq 'HASH'));
1.1055    raeburn  11094:     foreach my $item (@{$contents}) {
                   11095:         $count ++;
1.1056    raeburn  11096:         @{$dirorder->{$count}} = @hierarchy;
                   11097:         $titles->{$count} = $item;
1.1055    raeburn  11098:         &archive_hierarchy($depth,$count,$parent,$children);
                   11099:         if ($wantform) {
                   11100:             $datatable .= &archive_row($is_dir->{$item},$item,
                   11101:                                        $currdir,$depth,$count);
                   11102:         }
                   11103:         if ($is_dir->{$item}) {
                   11104:             $depth ++;
1.1056    raeburn  11105:             push(@hierarchy,$count);
                   11106:             $parent->{$depth} = $count;
1.1055    raeburn  11107:             $datatable .=
                   11108:                 &recurse_extracted_archive("$currdir/$item",$docudom,$docuname,
1.1056    raeburn  11109:                                            \$depth,\$count,\@hierarchy,$dirorder,
                   11110:                                            $children,$parent,$titles,$wantform);
1.1055    raeburn  11111:             $depth --;
1.1056    raeburn  11112:             pop(@hierarchy);
1.1055    raeburn  11113:         }
                   11114:     }
                   11115:     return ($count,$datatable);
                   11116: }
                   11117: 
                   11118: sub recurse_extracted_archive {
1.1056    raeburn  11119:     my ($currdir,$docudom,$docuname,$depth,$count,$hierarchy,$dirorder,
                   11120:         $children,$parent,$titles,$wantform) = @_;
1.1055    raeburn  11121:     my $result='';
1.1056    raeburn  11122:     unless ((ref($depth)) && (ref($count)) && (ref($hierarchy) eq 'ARRAY') &&
                   11123:             (ref($children) eq 'HASH') && (ref($parent) eq 'HASH') &&
                   11124:             (ref($dirorder) eq 'HASH')) {
1.1055    raeburn  11125:         return $result;
                   11126:     }
                   11127:     my $dirptr = 16384;
                   11128:     my ($newdirlistref,$newlisterror) =
                   11129:         &Apache::lonnet::dirlist($currdir,$docudom,$docuname,1);
                   11130:     if (ref($newdirlistref) eq 'ARRAY') {
                   11131:         foreach my $dir_line (@{$newdirlistref}) {
                   11132:             my ($item,undef,undef,$testdir)=split(/\&/,$dir_line,5);
                   11133:             unless ($item =~ /^\.+$/) {
                   11134:                 $$count ++;
1.1056    raeburn  11135:                 @{$dirorder->{$$count}} = @{$hierarchy};
                   11136:                 $titles->{$$count} = $item;
1.1055    raeburn  11137:                 &archive_hierarchy($$depth,$$count,$parent,$children);
1.1056    raeburn  11138: 
1.1055    raeburn  11139:                 my $is_dir;
                   11140:                 if ($dirptr&$testdir) {
                   11141:                     $is_dir = 1;
                   11142:                 }
                   11143:                 if ($wantform) {
                   11144:                     $result .= &archive_row($is_dir,$item,$currdir,$$depth,$$count);
                   11145:                 }
                   11146:                 if ($is_dir) {
                   11147:                     $$depth ++;
1.1056    raeburn  11148:                     push(@{$hierarchy},$$count);
                   11149:                     $parent->{$$depth} = $$count;
1.1055    raeburn  11150:                     $result .=
                   11151:                         &recurse_extracted_archive("$currdir/$item",$docudom,
                   11152:                                                    $docuname,$depth,$count,
1.1056    raeburn  11153:                                                    $hierarchy,$dirorder,$children,
                   11154:                                                    $parent,$titles,$wantform);
1.1055    raeburn  11155:                     $$depth --;
1.1056    raeburn  11156:                     pop(@{$hierarchy});
1.1055    raeburn  11157:                 }
                   11158:             }
                   11159:         }
                   11160:     }
                   11161:     return $result;
                   11162: }
                   11163: 
                   11164: sub archive_hierarchy {
                   11165:     my ($depth,$count,$parent,$children) =@_;
                   11166:     if ((ref($parent) eq 'HASH') && (ref($children) eq 'HASH')) {
                   11167:         if (exists($parent->{$depth})) {
                   11168:              $children->{$parent->{$depth}} .= $count.':';
                   11169:         }
                   11170:     }
                   11171:     return;
                   11172: }
                   11173: 
                   11174: sub archive_row {
                   11175:     my ($is_dir,$item,$currdir,$depth,$count) = @_;
                   11176:     my ($name) = ($item =~ m{([^/]+)$});
                   11177:     my %choices = &Apache::lonlocal::texthash (
1.1059    raeburn  11178:                                        'display'    => 'Add as file',
1.1055    raeburn  11179:                                        'dependency' => 'Include as dependency',
                   11180:                                        'discard'    => 'Discard',
                   11181:                                       );
                   11182:     if ($is_dir) {
1.1059    raeburn  11183:         $choices{'display'} = &mt('Add as folder'); 
1.1055    raeburn  11184:     }
1.1056    raeburn  11185:     my $output = &start_data_table_row().'<td align="right">'.$count.'</td>'."\n";
                   11186:     my $offset = 0;
1.1055    raeburn  11187:     foreach my $action ('display','dependency','discard') {
1.1056    raeburn  11188:         $offset ++;
1.1065    raeburn  11189:         if ($action ne 'display') {
                   11190:             $offset ++;
                   11191:         }  
1.1055    raeburn  11192:         $output .= '<td><span class="LC_nobreak">'.
                   11193:                    '<label><input type="radio" name="archive_'.$count.
                   11194:                    '" id="archive_'.$action.'_'.$count.'" value="'.$action.'"';
                   11195:         my $text = $choices{$action};
                   11196:         if ($is_dir) {
                   11197:             $output .= ' onclick="javascript:propagateCheck(this.form,'."'$count'".');"';
                   11198:             if ($action eq 'display') {
1.1059    raeburn  11199:                 $text = &mt('Add as folder');
1.1055    raeburn  11200:             }
1.1056    raeburn  11201:         } else {
                   11202:             $output .= ' onclick="javascript:dependencyCheck(this.form,'."$count,$offset".');"';
                   11203: 
                   11204:         }
                   11205:         $output .= ' />&nbsp;'.$choices{$action}.'</label></span>';
                   11206:         if ($action eq 'dependency') {
                   11207:             $output .= '<div id="arc_depon_'.$count.'" style="display:none;">'."\n".
                   11208:                        &mt('Used by:').'&nbsp;<select name="archive_dependent_on_'.$count.'" '.
                   11209:                        'onchange="propagateSelect(this.form,'."$count,$offset".')">'."\n".
                   11210:                        '<option value=""></option>'."\n".
                   11211:                        '</select>'."\n".
                   11212:                        '</div>';
1.1059    raeburn  11213:         } elsif ($action eq 'display') {
                   11214:             $output .= '<div id="arc_title_'.$count.'" style="display:none;">'."\n".
                   11215:                        &mt('Title:').'&nbsp;<input type="text" name="archive_title_'.$count.'" id="archive_title_'.$count.'" />'."\n".
                   11216:                        '</div>';
1.1055    raeburn  11217:         }
1.1056    raeburn  11218:         $output .= '</td>';
1.1055    raeburn  11219:     }
                   11220:     $output .= '<td><input type="hidden" name="archive_content_'.$count.'" value="'.
                   11221:                &HTML::Entities::encode("$currdir/$item",'"<>&').'" />'.('&nbsp;' x 2);
                   11222:     for (my $i=0; $i<$depth; $i++) {
                   11223:         $output .= ('<img src="/adm/lonIcons/whitespace1.gif" class="LC_docs_spacer" alt="" />' x2)."\n";
                   11224:     }
                   11225:     if ($is_dir) {
                   11226:         $output .= '<img src="/adm/lonIcons/navmap.folder.open.gif" alt="" />&nbsp;'."\n".
                   11227:                    '<input type="hidden" name="archive_directory" value="'.$count.'" />'."\n";
                   11228:     } else {
                   11229:         $output .= '<input type="hidden" name="archive_file" value="'.$count.'" />'."\n";
                   11230:     }
                   11231:     $output .= '&nbsp;'.$name.'</td>'."\n".
                   11232:                &end_data_table_row();
                   11233:     return $output;
                   11234: }
                   11235: 
                   11236: sub archive_options_form {
1.1065    raeburn  11237:     my ($form,$display,$count,$hiddenelem) = @_;
                   11238:     my %lt = &Apache::lonlocal::texthash(
                   11239:                perm => 'Permanently remove archive file?',
                   11240:                hows => 'How should each extracted item be incorporated in the course?',
                   11241:                cont => 'Content actions for all',
                   11242:                addf => 'Add as folder/file',
                   11243:                incd => 'Include as dependency for a displayed file',
                   11244:                disc => 'Discard',
                   11245:                no   => 'No',
                   11246:                yes  => 'Yes',
                   11247:                save => 'Save',
                   11248:     );
                   11249:     my $output = <<"END";
                   11250: <form name="$form" method="post" action="">
                   11251: <p><span class="LC_nobreak">$lt{'perm'}&nbsp;
                   11252: <label>
                   11253:   <input type="radio" name="archivedelete" value="0" checked="checked" />$lt{'no'}
                   11254: </label>
                   11255: &nbsp;
                   11256: <label>
                   11257:   <input type="radio" name="archivedelete" value="1" />$lt{'yes'}</label>
                   11258: </span>
                   11259: </p>
                   11260: <input type="hidden" name="phase" value="decompress_cleanup" />
                   11261: <br />$lt{'hows'}
                   11262: <div class="LC_columnSection">
                   11263:   <fieldset>
                   11264:     <legend>$lt{'cont'}</legend>
                   11265:     <input type="button" value="$lt{'addf'}" onclick="javascript:checkAll(document.$form,'display');" /> 
                   11266:     &nbsp;&nbsp;<input type="button" value="$lt{'incd'}" onclick="javascript:checkAll(document.$form,'dependency');" />
                   11267:     &nbsp;&nbsp;<input type="button" value="$lt{'disc'}" onclick="javascript:checkAll(document.$form,'discard');" />
                   11268:   </fieldset>
                   11269: </div>
                   11270: END
                   11271:     return $output.
1.1055    raeburn  11272:            &start_data_table()."\n".
1.1065    raeburn  11273:            $display."\n".
1.1055    raeburn  11274:            &end_data_table()."\n".
                   11275:            '<input type="hidden" name="archive_count" value="'.$count.'" />'.
                   11276:            $hiddenelem.
1.1065    raeburn  11277:            '<br /><input type="submit" name="archive_submit" value="'.$lt{'save'}.'" />'.
1.1055    raeburn  11278:            '</form>';
                   11279: }
                   11280: 
                   11281: sub archive_javascript {
1.1056    raeburn  11282:     my ($startcount,$numitems,$titles,$children) = @_;
                   11283:     return unless ((ref($titles) eq 'HASH') && (ref($children) eq 'HASH'));
1.1059    raeburn  11284:     my $maintitle = $env{'form.comment'};
1.1055    raeburn  11285:     my $scripttag = <<START;
                   11286: <script type="text/javascript">
                   11287: // <![CDATA[
                   11288: 
                   11289: function checkAll(form,prefix) {
                   11290:     var idstr =  new RegExp("^archive_"+prefix+"_\\\\d+\$");
                   11291:     for (var i=0; i < form.elements.length; i++) {
                   11292:         var id = form.elements[i].id;
                   11293:         if ((id != '') && (id != undefined)) {
                   11294:             if (idstr.test(id)) {
                   11295:                 if (form.elements[i].type == 'radio') {
                   11296:                     form.elements[i].checked = true;
1.1056    raeburn  11297:                     var nostart = i-$startcount;
1.1059    raeburn  11298:                     var offset = nostart%7;
                   11299:                     var count = (nostart-offset)/7;    
1.1056    raeburn  11300:                     dependencyCheck(form,count,offset);
1.1055    raeburn  11301:                 }
                   11302:             }
                   11303:         }
                   11304:     }
                   11305: }
                   11306: 
                   11307: function propagateCheck(form,count) {
                   11308:     if (count > 0) {
1.1059    raeburn  11309:         var startelement = $startcount + ((count-1) * 7);
                   11310:         for (var j=1; j<6; j++) {
                   11311:             if ((j != 2) && (j != 4)) {
1.1056    raeburn  11312:                 var item = startelement + j; 
                   11313:                 if (form.elements[item].type == 'radio') {
                   11314:                     if (form.elements[item].checked) {
                   11315:                         containerCheck(form,count,j);
                   11316:                         break;
                   11317:                     }
1.1055    raeburn  11318:                 }
                   11319:             }
                   11320:         }
                   11321:     }
                   11322: }
                   11323: 
                   11324: numitems = $numitems
1.1056    raeburn  11325: var titles = new Array(numitems);
                   11326: var parents = new Array(numitems);
1.1055    raeburn  11327: for (var i=0; i<numitems; i++) {
1.1056    raeburn  11328:     parents[i] = new Array;
1.1055    raeburn  11329: }
1.1059    raeburn  11330: var maintitle = '$maintitle';
1.1055    raeburn  11331: 
                   11332: START
                   11333: 
1.1056    raeburn  11334:     foreach my $container (sort { $a <=> $b } (keys(%{$children}))) {
                   11335:         my @contents = split(/:/,$children->{$container});
1.1055    raeburn  11336:         for (my $i=0; $i<@contents; $i ++) {
                   11337:             $scripttag .= 'parents['.$container.']['.$i.'] = '.$contents[$i]."\n";
                   11338:         }
                   11339:     }
                   11340: 
1.1056    raeburn  11341:     foreach my $key (sort { $a <=> $b } (keys(%{$titles}))) {
                   11342:         $scripttag .= "titles[$key] = '".$titles->{$key}."';\n";
                   11343:     }
                   11344: 
1.1055    raeburn  11345:     $scripttag .= <<END;
                   11346: 
                   11347: function containerCheck(form,count,offset) {
                   11348:     if (count > 0) {
1.1056    raeburn  11349:         dependencyCheck(form,count,offset);
1.1059    raeburn  11350:         var item = (offset+$startcount)+7*(count-1);
1.1055    raeburn  11351:         form.elements[item].checked = true;
                   11352:         if(Object.prototype.toString.call(parents[count]) === '[object Array]') {
                   11353:             if (parents[count].length > 0) {
                   11354:                 for (var j=0; j<parents[count].length; j++) {
1.1056    raeburn  11355:                     containerCheck(form,parents[count][j],offset);
                   11356:                 }
                   11357:             }
                   11358:         }
                   11359:     }
                   11360: }
                   11361: 
                   11362: function dependencyCheck(form,count,offset) {
                   11363:     if (count > 0) {
1.1059    raeburn  11364:         var chosen = (offset+$startcount)+7*(count-1);
                   11365:         var depitem = $startcount + ((count-1) * 7) + 4;
1.1056    raeburn  11366:         var currtype = form.elements[depitem].type;
                   11367:         if (form.elements[chosen].value == 'dependency') {
                   11368:             document.getElementById('arc_depon_'+count).style.display='block'; 
                   11369:             form.elements[depitem].options.length = 0;
                   11370:             form.elements[depitem].options[0] = new Option('Select','',true,true);
1.1075.2.11  raeburn  11371:             for (var i=1; i<=numitems; i++) {
                   11372:                 if (i == count) {
                   11373:                     continue;
                   11374:                 }
1.1059    raeburn  11375:                 var startelement = $startcount + (i-1) * 7;
                   11376:                 for (var j=1; j<6; j++) {
                   11377:                     if ((j != 2) && (j!= 4)) {
1.1056    raeburn  11378:                         var item = startelement + j;
                   11379:                         if (form.elements[item].type == 'radio') {
                   11380:                             if (form.elements[item].checked) {
                   11381:                                 if (form.elements[item].value == 'display') {
                   11382:                                     var n = form.elements[depitem].options.length;
                   11383:                                     form.elements[depitem].options[n] = new Option(titles[i],i,false,false);
                   11384:                                 }
                   11385:                             }
                   11386:                         }
                   11387:                     }
                   11388:                 }
                   11389:             }
                   11390:         } else {
                   11391:             document.getElementById('arc_depon_'+count).style.display='none';
                   11392:             form.elements[depitem].options.length = 0;
                   11393:             form.elements[depitem].options[0] = new Option('Select','',true,true);
                   11394:         }
1.1059    raeburn  11395:         titleCheck(form,count,offset);
1.1056    raeburn  11396:     }
                   11397: }
                   11398: 
                   11399: function propagateSelect(form,count,offset) {
                   11400:     if (count > 0) {
1.1065    raeburn  11401:         var item = (1+offset+$startcount)+7*(count-1);
1.1056    raeburn  11402:         var picked = form.elements[item].options[form.elements[item].selectedIndex].value; 
                   11403:         if (Object.prototype.toString.call(parents[count]) === '[object Array]') {
                   11404:             if (parents[count].length > 0) {
                   11405:                 for (var j=0; j<parents[count].length; j++) {
                   11406:                     containerSelect(form,parents[count][j],offset,picked);
1.1055    raeburn  11407:                 }
                   11408:             }
                   11409:         }
                   11410:     }
                   11411: }
1.1056    raeburn  11412: 
                   11413: function containerSelect(form,count,offset,picked) {
                   11414:     if (count > 0) {
1.1065    raeburn  11415:         var item = (offset+$startcount)+7*(count-1);
1.1056    raeburn  11416:         if (form.elements[item].type == 'radio') {
                   11417:             if (form.elements[item].value == 'dependency') {
                   11418:                 if (form.elements[item+1].type == 'select-one') {
                   11419:                     for (var i=0; i<form.elements[item+1].options.length; i++) {
                   11420:                         if (form.elements[item+1].options[i].value == picked) {
                   11421:                             form.elements[item+1].selectedIndex = i;
                   11422:                             break;
                   11423:                         }
                   11424:                     }
                   11425:                 }
                   11426:                 if (Object.prototype.toString.call(parents[count]) === '[object Array]') {
                   11427:                     if (parents[count].length > 0) {
                   11428:                         for (var j=0; j<parents[count].length; j++) {
                   11429:                             containerSelect(form,parents[count][j],offset,picked);
                   11430:                         }
                   11431:                     }
                   11432:                 }
                   11433:             }
                   11434:         }
                   11435:     }
                   11436: }
                   11437: 
1.1059    raeburn  11438: function titleCheck(form,count,offset) {
                   11439:     if (count > 0) {
                   11440:         var chosen = (offset+$startcount)+7*(count-1);
                   11441:         var depitem = $startcount + ((count-1) * 7) + 2;
                   11442:         var currtype = form.elements[depitem].type;
                   11443:         if (form.elements[chosen].value == 'display') {
                   11444:             document.getElementById('arc_title_'+count).style.display='block';
                   11445:             if ((count==1) && ((parents[count].length > 0) || (numitems == 1))) {
                   11446:                 document.getElementById('archive_title_'+count).value=maintitle;
                   11447:             }
                   11448:         } else {
                   11449:             document.getElementById('arc_title_'+count).style.display='none';
                   11450:             if (currtype == 'text') { 
                   11451:                 document.getElementById('archive_title_'+count).value='';
                   11452:             }
                   11453:         }
                   11454:     }
                   11455:     return;
                   11456: }
                   11457: 
1.1055    raeburn  11458: // ]]>
                   11459: </script>
                   11460: END
                   11461:     return $scripttag;
                   11462: }
                   11463: 
                   11464: sub process_extracted_files {
1.1067    raeburn  11465:     my ($context,$docudom,$docuname,$destination,$dir_root,$hiddenelem) = @_;
1.1055    raeburn  11466:     my $numitems = $env{'form.archive_count'};
                   11467:     return unless ($numitems);
                   11468:     my @ids=&Apache::lonnet::current_machine_ids();
                   11469:     my ($prefix,$pathtocheck,$dir,$ishome,$error,$warning,%toplevelitems,%is_dir,
1.1067    raeburn  11470:         %folders,%containers,%mapinner,%prompttofetch);
1.1055    raeburn  11471:     my $docuhome = &Apache::lonnet::homeserver($docuname,$docudom);
                   11472:     if (grep(/^\Q$docuhome\E$/,@ids)) {
                   11473:         $prefix = &LONCAPA::propath($docudom,$docuname);
                   11474:         $pathtocheck = "$dir_root/$destination";
                   11475:         $dir = $dir_root;
                   11476:         $ishome = 1;
                   11477:     } else {
                   11478:         $prefix = $Apache::lonnet::perlvar{'lonDocRoot'};
                   11479:         $pathtocheck = "$dir_root/$docudom/$docuname/$destination";
                   11480:         $dir = "$dir_root/$docudom/$docuname";    
                   11481:     }
                   11482:     my $currdir = "$dir_root/$destination";
                   11483:     (my $docstype,$mapinner{'0'}) = ($destination =~ m{^(docs|supplemental)/(\w+)/});
                   11484:     if ($env{'form.folderpath'}) {
                   11485:         my @items = split('&',$env{'form.folderpath'});
                   11486:         $folders{'0'} = $items[-2];
1.1075.2.17  raeburn  11487:         if ($env{'form.folderpath'} =~ /\:1$/) {
                   11488:             $containers{'0'}='page';
                   11489:         } else {
                   11490:             $containers{'0'}='sequence';
                   11491:         }
1.1055    raeburn  11492:     }
                   11493:     my @archdirs = &get_env_multiple('form.archive_directory');
                   11494:     if ($numitems) {
                   11495:         for (my $i=1; $i<=$numitems; $i++) {
                   11496:             my $path = $env{'form.archive_content_'.$i};
                   11497:             if ($path =~ m{^\Q$pathtocheck\E/([^/]+)$}) {
                   11498:                 my $item = $1;
                   11499:                 $toplevelitems{$item} = $i;
                   11500:                 if (grep(/^\Q$i\E$/,@archdirs)) {
                   11501:                     $is_dir{$item} = 1;
                   11502:                 }
                   11503:             }
                   11504:         }
                   11505:     }
1.1067    raeburn  11506:     my ($output,%children,%parent,%titles,%dirorder,$result);
1.1055    raeburn  11507:     if (keys(%toplevelitems) > 0) {
                   11508:         my @contents = sort(keys(%toplevelitems));
1.1056    raeburn  11509:         (my $count,undef) = &get_extracted($docudom,$docuname,$currdir,\%is_dir,\%children,
                   11510:                                            \%parent,\@contents,\%dirorder,\%titles);
1.1055    raeburn  11511:     }
1.1066    raeburn  11512:     my (%referrer,%orphaned,%todelete,%todeletedir,%newdest,%newseqid);
1.1055    raeburn  11513:     if ($numitems) {
                   11514:         for (my $i=1; $i<=$numitems; $i++) {
1.1075.2.11  raeburn  11515:             next if ($env{'form.archive_'.$i} eq 'dependency');
1.1055    raeburn  11516:             my $path = $env{'form.archive_content_'.$i};
                   11517:             if ($path =~ /^\Q$pathtocheck\E/) {
                   11518:                 if ($env{'form.archive_'.$i} eq 'discard') {
                   11519:                     if ($prefix ne '' && $path ne '') {
                   11520:                         if (-e $prefix.$path) {
1.1066    raeburn  11521:                             if ((@archdirs > 0) && 
                   11522:                                 (grep(/^\Q$i\E$/,@archdirs))) {
                   11523:                                 $todeletedir{$prefix.$path} = 1;
                   11524:                             } else {
                   11525:                                 $todelete{$prefix.$path} = 1;
                   11526:                             }
1.1055    raeburn  11527:                         }
                   11528:                     }
                   11529:                 } elsif ($env{'form.archive_'.$i} eq 'display') {
1.1059    raeburn  11530:                     my ($docstitle,$title,$url,$outer);
1.1055    raeburn  11531:                     ($title) = ($path =~ m{/([^/]+)$});
1.1059    raeburn  11532:                     $docstitle = $env{'form.archive_title_'.$i};
                   11533:                     if ($docstitle eq '') {
                   11534:                         $docstitle = $title;
                   11535:                     }
1.1055    raeburn  11536:                     $outer = 0;
1.1056    raeburn  11537:                     if (ref($dirorder{$i}) eq 'ARRAY') {
                   11538:                         if (@{$dirorder{$i}} > 0) {
                   11539:                             foreach my $item (reverse(@{$dirorder{$i}})) {
1.1055    raeburn  11540:                                 if ($env{'form.archive_'.$item} eq 'display') {
                   11541:                                     $outer = $item;
                   11542:                                     last;
                   11543:                                 }
                   11544:                             }
                   11545:                         }
                   11546:                     }
                   11547:                     my ($errtext,$fatal) = 
                   11548:                         &LONCAPA::map::mapread('/uploaded/'.$docudom.'/'.$docuname.
                   11549:                                                '/'.$folders{$outer}.'.'.
                   11550:                                                $containers{$outer});
                   11551:                     next if ($fatal);
                   11552:                     if ((@archdirs > 0) && (grep(/^\Q$i\E$/,@archdirs))) {
                   11553:                         if ($context eq 'coursedocs') {
1.1056    raeburn  11554:                             $mapinner{$i} = time;
1.1055    raeburn  11555:                             $folders{$i} = 'default_'.$mapinner{$i};
                   11556:                             $containers{$i} = 'sequence';
                   11557:                             my $url = '/uploaded/'.$docudom.'/'.$docuname.'/'.
                   11558:                                       $folders{$i}.'.'.$containers{$i};
                   11559:                             my $newidx = &LONCAPA::map::getresidx();
                   11560:                             $LONCAPA::map::resources[$newidx]=
1.1059    raeburn  11561:                                 $docstitle.':'.$url.':false:normal:res';
1.1055    raeburn  11562:                             push(@LONCAPA::map::order,$newidx);
                   11563:                             my ($outtext,$errtext) =
                   11564:                                 &LONCAPA::map::storemap('/uploaded/'.$docudom.'/'.
                   11565:                                                         $docuname.'/'.$folders{$outer}.
1.1075.2.11  raeburn  11566:                                                         '.'.$containers{$outer},1,1);
1.1056    raeburn  11567:                             $newseqid{$i} = $newidx;
1.1067    raeburn  11568:                             unless ($errtext) {
                   11569:                                 $result .=  '<li>'.&mt('Folder: [_1] added to course',$docstitle).'</li>'."\n";
                   11570:                             }
1.1055    raeburn  11571:                         }
                   11572:                     } else {
                   11573:                         if ($context eq 'coursedocs') {
                   11574:                             my $newidx=&LONCAPA::map::getresidx();
                   11575:                             my $url = '/uploaded/'.$docudom.'/'.$docuname.'/'.
                   11576:                                       $docstype.'/'.$mapinner{$outer}.'/'.$newidx.'/'.
                   11577:                                       $title;
                   11578:                             if (!-e "$prefix$dir/$docstype/$mapinner{$outer}") {
                   11579:                                 mkdir("$prefix$dir/$docstype/$mapinner{$outer}",0755);
                   11580:                             }
                   11581:                             if (!-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx") {
                   11582:                                 mkdir("$prefix$dir/$docstype/$mapinner{$outer}/$newidx");
                   11583:                             }
                   11584:                             if (-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx") {
                   11585:                                 system("mv $prefix$path $prefix$dir/$docstype/$mapinner{$outer}/$newidx/$title");
1.1056    raeburn  11586:                                 $newdest{$i} = "$prefix$dir/$docstype/$mapinner{$outer}/$newidx";
1.1067    raeburn  11587:                                 unless ($ishome) {
                   11588:                                     my $fetch = "$newdest{$i}/$title";
                   11589:                                     $fetch =~ s/^\Q$prefix$dir\E//;
                   11590:                                     $prompttofetch{$fetch} = 1;
                   11591:                                 }
1.1055    raeburn  11592:                             }
                   11593:                             $LONCAPA::map::resources[$newidx]=
1.1059    raeburn  11594:                                 $docstitle.':'.$url.':false:normal:res';
1.1055    raeburn  11595:                             push(@LONCAPA::map::order, $newidx);
                   11596:                             my ($outtext,$errtext)=
                   11597:                                 &LONCAPA::map::storemap('/uploaded/'.$docudom.'/'.
                   11598:                                                         $docuname.'/'.$folders{$outer}.
1.1075.2.11  raeburn  11599:                                                         '.'.$containers{$outer},1,1);
1.1067    raeburn  11600:                             unless ($errtext) {
                   11601:                                 if (-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx/$title") {
                   11602:                                     $result .= '<li>'.&mt('File: [_1] added to course',$docstitle).'</li>'."\n";
                   11603:                                 }
                   11604:                             }
1.1055    raeburn  11605:                         }
                   11606:                     }
1.1075.2.11  raeburn  11607:                 }
                   11608:             } else {
                   11609:                 $warning .= &mt('Item extracted from archive: [_1] has unexpected path.',$path).'<br />';
                   11610:             }
                   11611:         }
                   11612:         for (my $i=1; $i<=$numitems; $i++) {
                   11613:             next unless ($env{'form.archive_'.$i} eq 'dependency');
                   11614:             my $path = $env{'form.archive_content_'.$i};
                   11615:             if ($path =~ /^\Q$pathtocheck\E/) {
                   11616:                 my ($title) = ($path =~ m{/([^/]+)$});
                   11617:                 $referrer{$i} = $env{'form.archive_dependent_on_'.$i};
                   11618:                 if ($env{'form.archive_'.$referrer{$i}} eq 'display') {
                   11619:                     if (ref($dirorder{$i}) eq 'ARRAY') {
                   11620:                         my ($itemidx,$fullpath,$relpath);
                   11621:                         if (ref($dirorder{$referrer{$i}}) eq 'ARRAY') {
                   11622:                             my $container = $dirorder{$referrer{$i}}->[-1];
1.1056    raeburn  11623:                             for (my $j=0; $j<@{$dirorder{$i}}; $j++) {
1.1075.2.11  raeburn  11624:                                 if ($dirorder{$i}->[$j] eq $container) {
                   11625:                                     $itemidx = $j;
1.1056    raeburn  11626:                                 }
                   11627:                             }
1.1075.2.11  raeburn  11628:                         }
                   11629:                         if ($itemidx eq '') {
                   11630:                             $itemidx =  0;
                   11631:                         }
                   11632:                         if (grep(/^\Q$referrer{$i}\E$/,@archdirs)) {
                   11633:                             if ($mapinner{$referrer{$i}}) {
                   11634:                                 $fullpath = "$prefix$dir/$docstype/$mapinner{$referrer{$i}}";
                   11635:                                 for (my $j=$itemidx; $j<@{$dirorder{$i}}; $j++) {
                   11636:                                     if (grep(/^\Q$dirorder{$i}->[$j]\E$/,@archdirs)) {
                   11637:                                         unless (defined($newseqid{$dirorder{$i}->[$j]})) {
                   11638:                                             $fullpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   11639:                                             $relpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   11640:                                             if (!-e $fullpath) {
                   11641:                                                 mkdir($fullpath,0755);
1.1056    raeburn  11642:                                             }
                   11643:                                         }
1.1075.2.11  raeburn  11644:                                     } else {
                   11645:                                         last;
1.1056    raeburn  11646:                                     }
1.1075.2.11  raeburn  11647:                                 }
                   11648:                             }
                   11649:                         } elsif ($newdest{$referrer{$i}}) {
                   11650:                             $fullpath = $newdest{$referrer{$i}};
                   11651:                             for (my $j=$itemidx; $j<@{$dirorder{$i}}; $j++) {
                   11652:                                 if ($env{'form.archive_'.$dirorder{$i}->[$j]} eq 'discard') {
                   11653:                                     $orphaned{$i} = $env{'form.archive_'.$dirorder{$i}->[$j]};
                   11654:                                     last;
                   11655:                                 } elsif (grep(/^\Q$dirorder{$i}->[$j]\E$/,@archdirs)) {
                   11656:                                     unless (defined($newseqid{$dirorder{$i}->[$j]})) {
                   11657:                                         $fullpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   11658:                                         $relpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   11659:                                         if (!-e $fullpath) {
                   11660:                                             mkdir($fullpath,0755);
1.1056    raeburn  11661:                                         }
                   11662:                                     }
1.1075.2.11  raeburn  11663:                                 } else {
                   11664:                                     last;
1.1056    raeburn  11665:                                 }
1.1075.2.11  raeburn  11666:                             }
                   11667:                         }
                   11668:                         if ($fullpath ne '') {
                   11669:                             if (-e "$prefix$path") {
                   11670:                                 system("mv $prefix$path $fullpath/$title");
                   11671:                             }
                   11672:                             if (-e "$fullpath/$title") {
                   11673:                                 my $showpath;
                   11674:                                 if ($relpath ne '') {
                   11675:                                     $showpath = "$relpath/$title";
                   11676:                                 } else {
                   11677:                                     $showpath = "/$title";
1.1056    raeburn  11678:                                 }
1.1075.2.11  raeburn  11679:                                 $result .= '<li>'.&mt('[_1] included as a dependency',$showpath).'</li>'."\n";
                   11680:                             }
                   11681:                             unless ($ishome) {
                   11682:                                 my $fetch = "$fullpath/$title";
                   11683:                                 $fetch =~ s/^\Q$prefix$dir\E//;
                   11684:                                 $prompttofetch{$fetch} = 1;
1.1055    raeburn  11685:                             }
                   11686:                         }
                   11687:                     }
1.1075.2.11  raeburn  11688:                 } elsif ($env{'form.archive_'.$referrer{$i}} eq 'discard') {
                   11689:                     $warning .= &mt('[_1] is a dependency of [_2], which was discarded.',
                   11690:                                     $path,$env{'form.archive_content_'.$referrer{$i}}).'<br />';
1.1055    raeburn  11691:                 }
                   11692:             } else {
1.1075.2.11  raeburn  11693:                 $warning .= &mt('Item extracted from archive: [_1] has unexpected path.',$path).'<br />';
1.1055    raeburn  11694:             }
                   11695:         }
                   11696:         if (keys(%todelete)) {
                   11697:             foreach my $key (keys(%todelete)) {
                   11698:                 unlink($key);
1.1066    raeburn  11699:             }
                   11700:         }
                   11701:         if (keys(%todeletedir)) {
                   11702:             foreach my $key (keys(%todeletedir)) {
                   11703:                 rmdir($key);
                   11704:             }
                   11705:         }
                   11706:         foreach my $dir (sort(keys(%is_dir))) {
                   11707:             if (($pathtocheck ne '') && ($dir ne ''))  {
                   11708:                 &cleanup_empty_dirs($prefix."$pathtocheck/$dir");
1.1055    raeburn  11709:             }
                   11710:         }
1.1067    raeburn  11711:         if ($result ne '') {
                   11712:             $output .= '<ul>'."\n".
                   11713:                        $result."\n".
                   11714:                        '</ul>';
                   11715:         }
                   11716:         unless ($ishome) {
                   11717:             my $replicationfail;
                   11718:             foreach my $item (keys(%prompttofetch)) {
                   11719:                 my $fetchresult= &Apache::lonnet::reply('fetchuserfile:'.$item,$docuhome);
                   11720:                 unless ($fetchresult eq 'ok') {
                   11721:                     $replicationfail .= '<li>'.$item.'</li>'."\n";
                   11722:                 }
                   11723:             }
                   11724:             if ($replicationfail) {
                   11725:                 $output .= '<p class="LC_error">'.
                   11726:                            &mt('Course home server failed to retrieve:').'<ul>'.
                   11727:                            $replicationfail.
                   11728:                            '</ul></p>';
                   11729:             }
                   11730:         }
1.1055    raeburn  11731:     } else {
                   11732:         $warning = &mt('No items found in archive.');
                   11733:     }
                   11734:     if ($error) {
                   11735:         $output .= '<p class="LC_error">'.&mt('Not extracted.').'<br />'.
                   11736:                    $error.'</p>'."\n";
                   11737:     }
                   11738:     if ($warning) {
                   11739:         $output .= '<p class="LC_warning">'.$warning.'</p>'."\n";
                   11740:     }
                   11741:     return $output;
                   11742: }
                   11743: 
1.1066    raeburn  11744: sub cleanup_empty_dirs {
                   11745:     my ($path) = @_;
                   11746:     if (($path ne '') && (-d $path)) {
                   11747:         if (opendir(my $dirh,$path)) {
                   11748:             my @dircontents = grep(!/^\./,readdir($dirh));
                   11749:             my $numitems = 0;
                   11750:             foreach my $item (@dircontents) {
                   11751:                 if (-d "$path/$item") {
1.1075.2.28  raeburn  11752:                     &cleanup_empty_dirs("$path/$item");
1.1066    raeburn  11753:                     if (-e "$path/$item") {
                   11754:                         $numitems ++;
                   11755:                     }
                   11756:                 } else {
                   11757:                     $numitems ++;
                   11758:                 }
                   11759:             }
                   11760:             if ($numitems == 0) {
                   11761:                 rmdir($path);
                   11762:             }
                   11763:             closedir($dirh);
                   11764:         }
                   11765:     }
                   11766:     return;
                   11767: }
                   11768: 
1.41      ng       11769: =pod
1.45      matthew  11770: 
1.1068    raeburn  11771: =item &get_folder_hierarchy()
                   11772: 
                   11773: Provides hierarchy of names of folders/sub-folders containing the current
                   11774: item,
                   11775: 
                   11776: Inputs: 3
                   11777:      - $navmap - navmaps object
                   11778: 
                   11779:      - $map - url for map (either the trigger itself, or map containing
                   11780:                            the resource, which is the trigger).
                   11781: 
                   11782:      - $showitem - 1 => show title for map itself; 0 => do not show.
                   11783: 
                   11784: Outputs: 1 @pathitems - array of folder/subfolder names.
                   11785: 
                   11786: =cut
                   11787: 
                   11788: sub get_folder_hierarchy {
                   11789:     my ($navmap,$map,$showitem) = @_;
                   11790:     my @pathitems;
                   11791:     if (ref($navmap)) {
                   11792:         my $mapres = $navmap->getResourceByUrl($map);
                   11793:         if (ref($mapres)) {
                   11794:             my $pcslist = $mapres->map_hierarchy();
                   11795:             if ($pcslist ne '') {
                   11796:                 my @pcs = split(/,/,$pcslist);
                   11797:                 foreach my $pc (@pcs) {
                   11798:                     if ($pc == 1) {
                   11799:                         push(@pathitems,&mt('Main Course Documents'));
                   11800:                     } else {
                   11801:                         my $res = $navmap->getByMapPc($pc);
                   11802:                         if (ref($res)) {
                   11803:                             my $title = $res->compTitle();
                   11804:                             $title =~ s/\W+/_/g;
                   11805:                             if ($title ne '') {
                   11806:                                 push(@pathitems,$title);
                   11807:                             }
                   11808:                         }
                   11809:                     }
                   11810:                 }
                   11811:             }
1.1071    raeburn  11812:             if ($showitem) {
                   11813:                 if ($mapres->{ID} eq '0.0') {
                   11814:                     push(@pathitems,&mt('Main Course Documents'));
                   11815:                 } else {
                   11816:                     my $maptitle = $mapres->compTitle();
                   11817:                     $maptitle =~ s/\W+/_/g;
                   11818:                     if ($maptitle ne '') {
                   11819:                         push(@pathitems,$maptitle);
                   11820:                     }
1.1068    raeburn  11821:                 }
                   11822:             }
                   11823:         }
                   11824:     }
                   11825:     return @pathitems;
                   11826: }
                   11827: 
                   11828: =pod
                   11829: 
1.1015    raeburn  11830: =item * &get_turnedin_filepath()
                   11831: 
                   11832: Determines path in a user's portfolio file for storage of files uploaded
                   11833: to a specific essayresponse or dropbox item.
                   11834: 
                   11835: Inputs: 3 required + 1 optional.
                   11836: $symb is symb for resource, $uname and $udom are for current user (required).
                   11837: $caller is optional (can be "submission", if routine is called when storing
                   11838: an upoaded file when "Submit Answer" button was pressed).
                   11839: 
                   11840: Returns array containing $path and $multiresp. 
                   11841: $path is path in portfolio.  $multiresp is 1 if this resource contains more
                   11842: than one file upload item.  Callers of routine should append partid as a 
                   11843: subdirectory to $path in cases where $multiresp is 1.
                   11844: 
                   11845: Called by: homework/essayresponse.pm and homework/structuretags.pm
                   11846: 
                   11847: =cut
                   11848: 
                   11849: sub get_turnedin_filepath {
                   11850:     my ($symb,$uname,$udom,$caller) = @_;
                   11851:     my ($map,$resid,$resurl)=&Apache::lonnet::decode_symb($symb);
                   11852:     my $turnindir;
                   11853:     my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'turnindir');
                   11854:     $turnindir = $userhash{'turnindir'};
                   11855:     my ($path,$multiresp);
                   11856:     if ($turnindir eq '') {
                   11857:         if ($caller eq 'submission') {
                   11858:             $turnindir = &mt('turned in');
                   11859:             $turnindir =~ s/\W+/_/g;
                   11860:             my %newhash = (
                   11861:                             'turnindir' => $turnindir,
                   11862:                           );
                   11863:             &Apache::lonnet::put('environment',\%newhash,$udom,$uname);
                   11864:         }
                   11865:     }
                   11866:     if ($turnindir ne '') {
                   11867:         $path = '/'.$turnindir.'/';
                   11868:         my ($multipart,$turnin,@pathitems);
                   11869:         my $navmap = Apache::lonnavmaps::navmap->new();
                   11870:         if (defined($navmap)) {
                   11871:             my $mapres = $navmap->getResourceByUrl($map);
                   11872:             if (ref($mapres)) {
                   11873:                 my $pcslist = $mapres->map_hierarchy();
                   11874:                 if ($pcslist ne '') {
                   11875:                     foreach my $pc (split(/,/,$pcslist)) {
                   11876:                         my $res = $navmap->getByMapPc($pc);
                   11877:                         if (ref($res)) {
                   11878:                             my $title = $res->compTitle();
                   11879:                             $title =~ s/\W+/_/g;
                   11880:                             if ($title ne '') {
                   11881:                                 push(@pathitems,$title);
                   11882:                             }
                   11883:                         }
                   11884:                     }
                   11885:                 }
                   11886:                 my $maptitle = $mapres->compTitle();
                   11887:                 $maptitle =~ s/\W+/_/g;
                   11888:                 if ($maptitle ne '') {
                   11889:                     push(@pathitems,$maptitle);
                   11890:                 }
                   11891:                 unless ($env{'request.state'} eq 'construct') {
                   11892:                     my $res = $navmap->getBySymb($symb);
                   11893:                     if (ref($res)) {
                   11894:                         my $partlist = $res->parts();
                   11895:                         my $totaluploads = 0;
                   11896:                         if (ref($partlist) eq 'ARRAY') {
                   11897:                             foreach my $part (@{$partlist}) {
                   11898:                                 my @types = $res->responseType($part);
                   11899:                                 my @ids = $res->responseIds($part);
                   11900:                                 for (my $i=0; $i < scalar(@ids); $i++) {
                   11901:                                     if ($types[$i] eq 'essay') {
                   11902:                                         my $partid = $part.'_'.$ids[$i];
                   11903:                                         if (&Apache::lonnet::EXT("resource.$partid.uploadedfiletypes") ne '') {
                   11904:                                             $totaluploads ++;
                   11905:                                         }
                   11906:                                     }
                   11907:                                 }
                   11908:                             }
                   11909:                             if ($totaluploads > 1) {
                   11910:                                 $multiresp = 1;
                   11911:                             }
                   11912:                         }
                   11913:                     }
                   11914:                 }
                   11915:             } else {
                   11916:                 return;
                   11917:             }
                   11918:         } else {
                   11919:             return;
                   11920:         }
                   11921:         my $restitle=&Apache::lonnet::gettitle($symb);
                   11922:         $restitle =~ s/\W+/_/g;
                   11923:         if ($restitle eq '') {
                   11924:             $restitle = ($resurl =~ m{/[^/]+$});
                   11925:             if ($restitle eq '') {
                   11926:                 $restitle = time;
                   11927:             }
                   11928:         }
                   11929:         push(@pathitems,$restitle);
                   11930:         $path .= join('/',@pathitems);
                   11931:     }
                   11932:     return ($path,$multiresp);
                   11933: }
                   11934: 
                   11935: =pod
                   11936: 
1.464     albertel 11937: =back
1.41      ng       11938: 
1.112     bowersj2 11939: =head1 CSV Upload/Handling functions
1.38      albertel 11940: 
1.41      ng       11941: =over 4
                   11942: 
1.648     raeburn  11943: =item * &upfile_store($r)
1.41      ng       11944: 
                   11945: Store uploaded file, $r should be the HTTP Request object,
1.258     albertel 11946: needs $env{'form.upfile'}
1.41      ng       11947: returns $datatoken to be put into hidden field
                   11948: 
                   11949: =cut
1.31      albertel 11950: 
                   11951: sub upfile_store {
                   11952:     my $r=shift;
1.258     albertel 11953:     $env{'form.upfile'}=~s/\r/\n/gs;
                   11954:     $env{'form.upfile'}=~s/\f/\n/gs;
                   11955:     $env{'form.upfile'}=~s/\n+/\n/gs;
                   11956:     $env{'form.upfile'}=~s/\n+$//gs;
1.31      albertel 11957: 
1.258     albertel 11958:     my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
                   11959: 	'_enroll_'.$env{'request.course.id'}.'_'.time.'_'.$$;
1.31      albertel 11960:     {
1.158     raeburn  11961:         my $datafile = $r->dir_config('lonDaemons').
                   11962:                            '/tmp/'.$datatoken.'.tmp';
                   11963:         if ( open(my $fh,">$datafile") ) {
1.258     albertel 11964:             print $fh $env{'form.upfile'};
1.158     raeburn  11965:             close($fh);
                   11966:         }
1.31      albertel 11967:     }
                   11968:     return $datatoken;
                   11969: }
                   11970: 
1.56      matthew  11971: =pod
                   11972: 
1.648     raeburn  11973: =item * &load_tmp_file($r)
1.41      ng       11974: 
                   11975: Load uploaded file from tmp, $r should be the HTTP Request object,
1.258     albertel 11976: needs $env{'form.datatoken'},
                   11977: sets $env{'form.upfile'} to the contents of the file
1.41      ng       11978: 
                   11979: =cut
1.31      albertel 11980: 
                   11981: sub load_tmp_file {
                   11982:     my $r=shift;
                   11983:     my @studentdata=();
                   11984:     {
1.158     raeburn  11985:         my $studentfile = $r->dir_config('lonDaemons').
1.258     albertel 11986:                               '/tmp/'.$env{'form.datatoken'}.'.tmp';
1.158     raeburn  11987:         if ( open(my $fh,"<$studentfile") ) {
                   11988:             @studentdata=<$fh>;
                   11989:             close($fh);
                   11990:         }
1.31      albertel 11991:     }
1.258     albertel 11992:     $env{'form.upfile'}=join('',@studentdata);
1.31      albertel 11993: }
                   11994: 
1.56      matthew  11995: =pod
                   11996: 
1.648     raeburn  11997: =item * &upfile_record_sep()
1.41      ng       11998: 
                   11999: Separate uploaded file into records
                   12000: returns array of records,
1.258     albertel 12001: needs $env{'form.upfile'} and $env{'form.upfiletype'}
1.41      ng       12002: 
                   12003: =cut
1.31      albertel 12004: 
                   12005: sub upfile_record_sep {
1.258     albertel 12006:     if ($env{'form.upfiletype'} eq 'xml') {
1.31      albertel 12007:     } else {
1.248     albertel 12008: 	my @records;
1.258     albertel 12009: 	foreach my $line (split(/\n/,$env{'form.upfile'})) {
1.248     albertel 12010: 	    if ($line=~/^\s*$/) { next; }
                   12011: 	    push(@records,$line);
                   12012: 	}
                   12013: 	return @records;
1.31      albertel 12014:     }
                   12015: }
                   12016: 
1.56      matthew  12017: =pod
                   12018: 
1.648     raeburn  12019: =item * &record_sep($record)
1.41      ng       12020: 
1.258     albertel 12021: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $env{'form.upfiletype'}
1.41      ng       12022: 
                   12023: =cut
                   12024: 
1.263     www      12025: sub takeleft {
                   12026:     my $index=shift;
                   12027:     return substr('0000'.$index,-4,4);
                   12028: }
                   12029: 
1.31      albertel 12030: sub record_sep {
                   12031:     my $record=shift;
                   12032:     my %components=();
1.258     albertel 12033:     if ($env{'form.upfiletype'} eq 'xml') {
                   12034:     } elsif ($env{'form.upfiletype'} eq 'space') {
1.31      albertel 12035:         my $i=0;
1.356     albertel 12036:         foreach my $field (split(/\s+/,$record)) {
1.31      albertel 12037:             $field=~s/^(\"|\')//;
                   12038:             $field=~s/(\"|\')$//;
1.263     www      12039:             $components{&takeleft($i)}=$field;
1.31      albertel 12040:             $i++;
                   12041:         }
1.258     albertel 12042:     } elsif ($env{'form.upfiletype'} eq 'tab') {
1.31      albertel 12043:         my $i=0;
1.356     albertel 12044:         foreach my $field (split(/\t/,$record)) {
1.31      albertel 12045:             $field=~s/^(\"|\')//;
                   12046:             $field=~s/(\"|\')$//;
1.263     www      12047:             $components{&takeleft($i)}=$field;
1.31      albertel 12048:             $i++;
                   12049:         }
                   12050:     } else {
1.561     www      12051:         my $separator=',';
1.480     banghart 12052:         if ($env{'form.upfiletype'} eq 'semisv') {
1.561     www      12053:             $separator=';';
1.480     banghart 12054:         }
1.31      albertel 12055:         my $i=0;
1.561     www      12056: # the character we are looking for to indicate the end of a quote or a record 
                   12057:         my $looking_for=$separator;
                   12058: # do not add the characters to the fields
                   12059:         my $ignore=0;
                   12060: # we just encountered a separator (or the beginning of the record)
                   12061:         my $just_found_separator=1;
                   12062: # store the field we are working on here
                   12063:         my $field='';
                   12064: # work our way through all characters in record
                   12065:         foreach my $character ($record=~/(.)/g) {
                   12066:             if ($character eq $looking_for) {
                   12067:                if ($character ne $separator) {
                   12068: # Found the end of a quote, again looking for separator
                   12069:                   $looking_for=$separator;
                   12070:                   $ignore=1;
                   12071:                } else {
                   12072: # Found a separator, store away what we got
                   12073:                   $components{&takeleft($i)}=$field;
                   12074: 	          $i++;
                   12075:                   $just_found_separator=1;
                   12076:                   $ignore=0;
                   12077:                   $field='';
                   12078:                }
                   12079:                next;
                   12080:             }
                   12081: # single or double quotation marks after a separator indicate beginning of a quote
                   12082: # we are now looking for the end of the quote and need to ignore separators
                   12083:             if ((($character eq '"') || ($character eq "'")) && ($just_found_separator))  {
                   12084:                $looking_for=$character;
                   12085:                next;
                   12086:             }
                   12087: # ignore would be true after we reached the end of a quote
                   12088:             if ($ignore) { next; }
                   12089:             if (($just_found_separator) && ($character=~/\s/)) { next; }
                   12090:             $field.=$character;
                   12091:             $just_found_separator=0; 
1.31      albertel 12092:         }
1.561     www      12093: # catch the very last entry, since we never encountered the separator
                   12094:         $components{&takeleft($i)}=$field;
1.31      albertel 12095:     }
                   12096:     return %components;
                   12097: }
                   12098: 
1.144     matthew  12099: ######################################################
                   12100: ######################################################
                   12101: 
1.56      matthew  12102: =pod
                   12103: 
1.648     raeburn  12104: =item * &upfile_select_html()
1.41      ng       12105: 
1.144     matthew  12106: Return HTML code to select a file from the users machine and specify 
                   12107: the file type.
1.41      ng       12108: 
                   12109: =cut
                   12110: 
1.144     matthew  12111: ######################################################
                   12112: ######################################################
1.31      albertel 12113: sub upfile_select_html {
1.144     matthew  12114:     my %Types = (
                   12115:                  csv   => &mt('CSV (comma separated values, spreadsheet)'),
1.480     banghart 12116:                  semisv => &mt('Semicolon separated values'),
1.144     matthew  12117:                  space => &mt('Space separated'),
                   12118:                  tab   => &mt('Tabulator separated'),
                   12119: #                 xml   => &mt('HTML/XML'),
                   12120:                  );
                   12121:     my $Str = '<input type="file" name="upfile" size="50" />'.
1.727     riegler  12122:         '<br />'.&mt('Type').': <select name="upfiletype">';
1.144     matthew  12123:     foreach my $type (sort(keys(%Types))) {
                   12124:         $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
                   12125:     }
                   12126:     $Str .= "</select>\n";
                   12127:     return $Str;
1.31      albertel 12128: }
                   12129: 
1.301     albertel 12130: sub get_samples {
                   12131:     my ($records,$toget) = @_;
                   12132:     my @samples=({});
                   12133:     my $got=0;
                   12134:     foreach my $rec (@$records) {
                   12135: 	my %temp = &record_sep($rec);
                   12136: 	if (! grep(/\S/, values(%temp))) { next; }
                   12137: 	if (%temp) {
                   12138: 	    $samples[$got]=\%temp;
                   12139: 	    $got++;
                   12140: 	    if ($got == $toget) { last; }
                   12141: 	}
                   12142:     }
                   12143:     return \@samples;
                   12144: }
                   12145: 
1.144     matthew  12146: ######################################################
                   12147: ######################################################
                   12148: 
1.56      matthew  12149: =pod
                   12150: 
1.648     raeburn  12151: =item * &csv_print_samples($r,$records)
1.41      ng       12152: 
                   12153: Prints a table of sample values from each column uploaded $r is an
                   12154: Apache Request ref, $records is an arrayref from
                   12155: &Apache::loncommon::upfile_record_sep
                   12156: 
                   12157: =cut
                   12158: 
1.144     matthew  12159: ######################################################
                   12160: ######################################################
1.31      albertel 12161: sub csv_print_samples {
                   12162:     my ($r,$records) = @_;
1.662     bisitz   12163:     my $samples = &get_samples($records,5);
1.301     albertel 12164: 
1.594     raeburn  12165:     $r->print(&mt('Samples').'<br />'.&start_data_table().
                   12166:               &start_data_table_header_row());
1.356     albertel 12167:     foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) { 
1.845     bisitz   12168:         $r->print('<th>'.&mt('Column [_1]',($sample+1)).'</th>'); }
1.594     raeburn  12169:     $r->print(&end_data_table_header_row());
1.301     albertel 12170:     foreach my $hash (@$samples) {
1.594     raeburn  12171: 	$r->print(&start_data_table_row());
1.356     albertel 12172: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
1.31      albertel 12173: 	    $r->print('<td>');
1.356     albertel 12174: 	    if (defined($$hash{$sample})) { $r->print($$hash{$sample}); }
1.31      albertel 12175: 	    $r->print('</td>');
                   12176: 	}
1.594     raeburn  12177: 	$r->print(&end_data_table_row());
1.31      albertel 12178:     }
1.594     raeburn  12179:     $r->print(&end_data_table().'<br />'."\n");
1.31      albertel 12180: }
                   12181: 
1.144     matthew  12182: ######################################################
                   12183: ######################################################
                   12184: 
1.56      matthew  12185: =pod
                   12186: 
1.648     raeburn  12187: =item * &csv_print_select_table($r,$records,$d)
1.41      ng       12188: 
                   12189: Prints a table to create associations between values and table columns.
1.144     matthew  12190: 
1.41      ng       12191: $r is an Apache Request ref,
                   12192: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
1.174     matthew  12193: $d is an array of 2 element arrays (internal name, displayed name,defaultcol)
1.41      ng       12194: 
                   12195: =cut
                   12196: 
1.144     matthew  12197: ######################################################
                   12198: ######################################################
1.31      albertel 12199: sub csv_print_select_table {
                   12200:     my ($r,$records,$d) = @_;
1.301     albertel 12201:     my $i=0;
                   12202:     my $samples = &get_samples($records,1);
1.144     matthew  12203:     $r->print(&mt('Associate columns with student attributes.')."\n".
1.594     raeburn  12204: 	      &start_data_table().&start_data_table_header_row().
1.144     matthew  12205:               '<th>'.&mt('Attribute').'</th>'.
1.594     raeburn  12206:               '<th>'.&mt('Column').'</th>'.
                   12207:               &end_data_table_header_row()."\n");
1.356     albertel 12208:     foreach my $array_ref (@$d) {
                   12209: 	my ($value,$display,$defaultcol)=@{ $array_ref };
1.729     raeburn  12210: 	$r->print(&start_data_table_row().'<td>'.$display.'</td>');
1.31      albertel 12211: 
1.875     bisitz   12212: 	$r->print('<td><select name="f'.$i.'"'.
1.32      matthew  12213: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.31      albertel 12214: 	$r->print('<option value="none"></option>');
1.356     albertel 12215: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
                   12216: 	    $r->print('<option value="'.$sample.'"'.
                   12217:                       ($sample eq $defaultcol ? ' selected="selected" ' : '').
1.662     bisitz   12218:                       '>'.&mt('Column [_1]',($sample+1)).'</option>');
1.31      albertel 12219: 	}
1.594     raeburn  12220: 	$r->print('</select></td>'.&end_data_table_row()."\n");
1.31      albertel 12221: 	$i++;
                   12222:     }
1.594     raeburn  12223:     $r->print(&end_data_table());
1.31      albertel 12224:     $i--;
                   12225:     return $i;
                   12226: }
1.56      matthew  12227: 
1.144     matthew  12228: ######################################################
                   12229: ######################################################
                   12230: 
1.56      matthew  12231: =pod
1.31      albertel 12232: 
1.648     raeburn  12233: =item * &csv_samples_select_table($r,$records,$d)
1.41      ng       12234: 
                   12235: Prints a table of sample values from the upload and can make associate samples to internal names.
                   12236: 
                   12237: $r is an Apache Request ref,
                   12238: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
                   12239: $d is an array of 2 element arrays (internal name, displayed name)
                   12240: 
                   12241: =cut
                   12242: 
1.144     matthew  12243: ######################################################
                   12244: ######################################################
1.31      albertel 12245: sub csv_samples_select_table {
                   12246:     my ($r,$records,$d) = @_;
                   12247:     my $i=0;
1.144     matthew  12248:     #
1.662     bisitz   12249:     my $max_samples = 5;
                   12250:     my $samples = &get_samples($records,$max_samples);
1.594     raeburn  12251:     $r->print(&start_data_table().
                   12252:               &start_data_table_header_row().'<th>'.
                   12253:               &mt('Field').'</th><th>'.&mt('Samples').'</th>'.
                   12254:               &end_data_table_header_row());
1.301     albertel 12255: 
                   12256:     foreach my $key (sort(keys(%{ $samples->[0] }))) {
1.594     raeburn  12257: 	$r->print(&start_data_table_row().'<td><select name="f'.$i.'"'.
1.32      matthew  12258: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.301     albertel 12259: 	foreach my $option (@$d) {
                   12260: 	    my ($value,$display,$defaultcol)=@{ $option };
1.174     matthew  12261: 	    $r->print('<option value="'.$value.'"'.
1.253     albertel 12262:                       ($i eq $defaultcol ? ' selected="selected" ':'').'>'.
1.174     matthew  12263:                       $display.'</option>');
1.31      albertel 12264: 	}
                   12265: 	$r->print('</select></td><td>');
1.662     bisitz   12266: 	foreach my $line (0..($max_samples-1)) {
1.301     albertel 12267: 	    if (defined($samples->[$line]{$key})) { 
                   12268: 		$r->print($samples->[$line]{$key}."<br />\n"); 
                   12269: 	    }
                   12270: 	}
1.594     raeburn  12271: 	$r->print('</td>'.&end_data_table_row());
1.31      albertel 12272: 	$i++;
                   12273:     }
1.594     raeburn  12274:     $r->print(&end_data_table());
1.31      albertel 12275:     $i--;
                   12276:     return($i);
1.115     matthew  12277: }
                   12278: 
1.144     matthew  12279: ######################################################
                   12280: ######################################################
                   12281: 
1.115     matthew  12282: =pod
                   12283: 
1.648     raeburn  12284: =item * &clean_excel_name($name)
1.115     matthew  12285: 
                   12286: Returns a replacement for $name which does not contain any illegal characters.
                   12287: 
                   12288: =cut
                   12289: 
1.144     matthew  12290: ######################################################
                   12291: ######################################################
1.115     matthew  12292: sub clean_excel_name {
                   12293:     my ($name) = @_;
                   12294:     $name =~ s/[:\*\?\/\\]//g;
                   12295:     if (length($name) > 31) {
                   12296:         $name = substr($name,0,31);
                   12297:     }
                   12298:     return $name;
1.25      albertel 12299: }
1.84      albertel 12300: 
1.85      albertel 12301: =pod
                   12302: 
1.648     raeburn  12303: =item * &check_if_partid_hidden($id,$symb,$udom,$uname)
1.85      albertel 12304: 
                   12305: Returns either 1 or undef
                   12306: 
                   12307: 1 if the part is to be hidden, undef if it is to be shown
                   12308: 
                   12309: Arguments are:
                   12310: 
                   12311: $id the id of the part to be checked
                   12312: $symb, optional the symb of the resource to check
                   12313: $udom, optional the domain of the user to check for
                   12314: $uname, optional the username of the user to check for
                   12315: 
                   12316: =cut
1.84      albertel 12317: 
                   12318: sub check_if_partid_hidden {
                   12319:     my ($id,$symb,$udom,$uname) = @_;
1.133     albertel 12320:     my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
1.84      albertel 12321: 					 $symb,$udom,$uname);
1.141     albertel 12322:     my $truth=1;
                   12323:     #if the string starts with !, then the list is the list to show not hide
                   12324:     if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
1.84      albertel 12325:     my @hiddenlist=split(/,/,$hiddenparts);
                   12326:     foreach my $checkid (@hiddenlist) {
1.141     albertel 12327: 	if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
1.84      albertel 12328:     }
1.141     albertel 12329:     return !$truth;
1.84      albertel 12330: }
1.127     matthew  12331: 
1.138     matthew  12332: 
                   12333: ############################################################
                   12334: ############################################################
                   12335: 
                   12336: =pod
                   12337: 
1.157     matthew  12338: =back 
                   12339: 
1.138     matthew  12340: =head1 cgi-bin script and graphing routines
                   12341: 
1.157     matthew  12342: =over 4
                   12343: 
1.648     raeburn  12344: =item * &get_cgi_id()
1.138     matthew  12345: 
                   12346: Inputs: none
                   12347: 
                   12348: Returns an id which can be used to pass environment variables
                   12349: to various cgi-bin scripts.  These environment variables will
                   12350: be removed from the users environment after a given time by
                   12351: the routine &Apache::lonnet::transfer_profile_to_env.
                   12352: 
                   12353: =cut
                   12354: 
                   12355: ############################################################
                   12356: ############################################################
1.152     albertel 12357: my $uniq=0;
1.136     matthew  12358: sub get_cgi_id {
1.154     albertel 12359:     $uniq=($uniq+1)%100000;
1.280     albertel 12360:     return (time.'_'.$$.'_'.$uniq);
1.136     matthew  12361: }
                   12362: 
1.127     matthew  12363: ############################################################
                   12364: ############################################################
                   12365: 
                   12366: =pod
                   12367: 
1.648     raeburn  12368: =item * &DrawBarGraph()
1.127     matthew  12369: 
1.138     matthew  12370: Facilitates the plotting of data in a (stacked) bar graph.
                   12371: Puts plot definition data into the users environment in order for 
                   12372: graph.png to plot it.  Returns an <img> tag for the plot.
                   12373: The bars on the plot are labeled '1','2',...,'n'.
                   12374: 
                   12375: Inputs:
                   12376: 
                   12377: =over 4
                   12378: 
                   12379: =item $Title: string, the title of the plot
                   12380: 
                   12381: =item $xlabel: string, text describing the X-axis of the plot
                   12382: 
                   12383: =item $ylabel: string, text describing the Y-axis of the plot
                   12384: 
                   12385: =item $Max: scalar, the maximum Y value to use in the plot
                   12386: If $Max is < any data point, the graph will not be rendered.
                   12387: 
1.140     matthew  12388: =item $colors: array ref holding the colors to be used for the data sets when
1.138     matthew  12389: they are plotted.  If undefined, default values will be used.
                   12390: 
1.178     matthew  12391: =item $labels: array ref holding the labels to use on the x-axis for the bars.
                   12392: 
1.138     matthew  12393: =item @Values: An array of array references.  Each array reference holds data
                   12394: to be plotted in a stacked bar chart.
                   12395: 
1.239     matthew  12396: =item If the final element of @Values is a hash reference the key/value
                   12397: pairs will be added to the graph definition.
                   12398: 
1.138     matthew  12399: =back
                   12400: 
                   12401: Returns:
                   12402: 
                   12403: An <img> tag which references graph.png and the appropriate identifying
                   12404: information for the plot.
                   12405: 
1.127     matthew  12406: =cut
                   12407: 
                   12408: ############################################################
                   12409: ############################################################
1.134     matthew  12410: sub DrawBarGraph {
1.178     matthew  12411:     my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
1.134     matthew  12412:     #
                   12413:     if (! defined($colors)) {
                   12414:         $colors = ['#33ff00', 
                   12415:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
                   12416:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
                   12417:                   ]; 
                   12418:     }
1.228     matthew  12419:     my $extra_settings = {};
                   12420:     if (ref($Values[-1]) eq 'HASH') {
                   12421:         $extra_settings = pop(@Values);
                   12422:     }
1.127     matthew  12423:     #
1.136     matthew  12424:     my $identifier = &get_cgi_id();
                   12425:     my $id = 'cgi.'.$identifier;        
1.129     matthew  12426:     if (! @Values || ref($Values[0]) ne 'ARRAY') {
1.127     matthew  12427:         return '';
                   12428:     }
1.225     matthew  12429:     #
                   12430:     my @Labels;
                   12431:     if (defined($labels)) {
                   12432:         @Labels = @$labels;
                   12433:     } else {
                   12434:         for (my $i=0;$i<@{$Values[0]};$i++) {
                   12435:             push (@Labels,$i+1);
                   12436:         }
                   12437:     }
                   12438:     #
1.129     matthew  12439:     my $NumBars = scalar(@{$Values[0]});
1.225     matthew  12440:     if ($NumBars < scalar(@Labels)) { $NumBars = scalar(@Labels); }
1.129     matthew  12441:     my %ValuesHash;
                   12442:     my $NumSets=1;
                   12443:     foreach my $array (@Values) {
                   12444:         next if (! ref($array));
1.136     matthew  12445:         $ValuesHash{$id.'.data.'.$NumSets++} = 
1.132     matthew  12446:             join(',',@$array);
1.129     matthew  12447:     }
1.127     matthew  12448:     #
1.136     matthew  12449:     my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
1.225     matthew  12450:     if ($NumBars < 3) {
                   12451:         $width = 120+$NumBars*32;
1.220     matthew  12452:         $xskip = 1;
1.225     matthew  12453:         $bar_width = 30;
                   12454:     } elsif ($NumBars < 5) {
                   12455:         $width = 120+$NumBars*20;
                   12456:         $xskip = 1;
                   12457:         $bar_width = 20;
1.220     matthew  12458:     } elsif ($NumBars < 10) {
1.136     matthew  12459:         $width = 120+$NumBars*15;
                   12460:         $xskip = 1;
                   12461:         $bar_width = 15;
                   12462:     } elsif ($NumBars <= 25) {
                   12463:         $width = 120+$NumBars*11;
                   12464:         $xskip = 5;
                   12465:         $bar_width = 8;
                   12466:     } elsif ($NumBars <= 50) {
                   12467:         $width = 120+$NumBars*8;
                   12468:         $xskip = 5;
                   12469:         $bar_width = 4;
                   12470:     } else {
                   12471:         $width = 120+$NumBars*8;
                   12472:         $xskip = 5;
                   12473:         $bar_width = 4;
                   12474:     }
                   12475:     #
1.137     matthew  12476:     $Max = 1 if ($Max < 1);
                   12477:     if ( int($Max) < $Max ) {
                   12478:         $Max++;
                   12479:         $Max = int($Max);
                   12480:     }
1.127     matthew  12481:     $Title  = '' if (! defined($Title));
                   12482:     $xlabel = '' if (! defined($xlabel));
                   12483:     $ylabel = '' if (! defined($ylabel));
1.369     www      12484:     $ValuesHash{$id.'.title'}    = &escape($Title);
                   12485:     $ValuesHash{$id.'.xlabel'}   = &escape($xlabel);
                   12486:     $ValuesHash{$id.'.ylabel'}   = &escape($ylabel);
1.137     matthew  12487:     $ValuesHash{$id.'.y_max_value'} = $Max;
1.136     matthew  12488:     $ValuesHash{$id.'.NumBars'}  = $NumBars;
                   12489:     $ValuesHash{$id.'.NumSets'}  = $NumSets;
                   12490:     $ValuesHash{$id.'.PlotType'} = 'bar';
                   12491:     $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   12492:     $ValuesHash{$id.'.height'}   = $height;
                   12493:     $ValuesHash{$id.'.width'}    = $width;
                   12494:     $ValuesHash{$id.'.xskip'}    = $xskip;
                   12495:     $ValuesHash{$id.'.bar_width'} = $bar_width;
                   12496:     $ValuesHash{$id.'.labels'} = join(',',@Labels);
1.127     matthew  12497:     #
1.228     matthew  12498:     # Deal with other parameters
                   12499:     while (my ($key,$value) = each(%$extra_settings)) {
                   12500:         $ValuesHash{$id.'.'.$key} = $value;
                   12501:     }
                   12502:     #
1.646     raeburn  12503:     &Apache::lonnet::appenv(\%ValuesHash);
1.137     matthew  12504:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   12505: }
                   12506: 
                   12507: ############################################################
                   12508: ############################################################
                   12509: 
                   12510: =pod
                   12511: 
1.648     raeburn  12512: =item * &DrawXYGraph()
1.137     matthew  12513: 
1.138     matthew  12514: Facilitates the plotting of data in an XY graph.
                   12515: Puts plot definition data into the users environment in order for 
                   12516: graph.png to plot it.  Returns an <img> tag for the plot.
                   12517: 
                   12518: Inputs:
                   12519: 
                   12520: =over 4
                   12521: 
                   12522: =item $Title: string, the title of the plot
                   12523: 
                   12524: =item $xlabel: string, text describing the X-axis of the plot
                   12525: 
                   12526: =item $ylabel: string, text describing the Y-axis of the plot
                   12527: 
                   12528: =item $Max: scalar, the maximum Y value to use in the plot
                   12529: If $Max is < any data point, the graph will not be rendered.
                   12530: 
                   12531: =item $colors: Array ref containing the hex color codes for the data to be 
                   12532: plotted in.  If undefined, default values will be used.
                   12533: 
                   12534: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   12535: 
                   12536: =item $Ydata: Array ref containing Array refs.  
1.185     www      12537: Each of the contained arrays will be plotted as a separate curve.
1.138     matthew  12538: 
                   12539: =item %Values: hash indicating or overriding any default values which are 
                   12540: passed to graph.png.  
                   12541: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   12542: 
                   12543: =back
                   12544: 
                   12545: Returns:
                   12546: 
                   12547: An <img> tag which references graph.png and the appropriate identifying
                   12548: information for the plot.
                   12549: 
1.137     matthew  12550: =cut
                   12551: 
                   12552: ############################################################
                   12553: ############################################################
                   12554: sub DrawXYGraph {
                   12555:     my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
                   12556:     #
                   12557:     # Create the identifier for the graph
                   12558:     my $identifier = &get_cgi_id();
                   12559:     my $id = 'cgi.'.$identifier;
                   12560:     #
                   12561:     $Title  = '' if (! defined($Title));
                   12562:     $xlabel = '' if (! defined($xlabel));
                   12563:     $ylabel = '' if (! defined($ylabel));
                   12564:     my %ValuesHash = 
                   12565:         (
1.369     www      12566:          $id.'.title'  => &escape($Title),
                   12567:          $id.'.xlabel' => &escape($xlabel),
                   12568:          $id.'.ylabel' => &escape($ylabel),
1.137     matthew  12569:          $id.'.y_max_value'=> $Max,
                   12570:          $id.'.labels'     => join(',',@$Xlabels),
                   12571:          $id.'.PlotType'   => 'XY',
                   12572:          );
                   12573:     #
                   12574:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   12575:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   12576:     }
                   12577:     #
                   12578:     if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
                   12579:         return '';
                   12580:     }
                   12581:     my $NumSets=1;
1.138     matthew  12582:     foreach my $array (@{$Ydata}){
1.137     matthew  12583:         next if (! ref($array));
                   12584:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
                   12585:     }
1.138     matthew  12586:     $ValuesHash{$id.'.NumSets'} = $NumSets-1;
1.137     matthew  12587:     #
                   12588:     # Deal with other parameters
                   12589:     while (my ($key,$value) = each(%Values)) {
                   12590:         $ValuesHash{$id.'.'.$key} = $value;
1.127     matthew  12591:     }
                   12592:     #
1.646     raeburn  12593:     &Apache::lonnet::appenv(\%ValuesHash);
1.136     matthew  12594:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   12595: }
                   12596: 
                   12597: ############################################################
                   12598: ############################################################
                   12599: 
                   12600: =pod
                   12601: 
1.648     raeburn  12602: =item * &DrawXYYGraph()
1.138     matthew  12603: 
                   12604: Facilitates the plotting of data in an XY graph with two Y axes.
                   12605: Puts plot definition data into the users environment in order for 
                   12606: graph.png to plot it.  Returns an <img> tag for the plot.
                   12607: 
                   12608: Inputs:
                   12609: 
                   12610: =over 4
                   12611: 
                   12612: =item $Title: string, the title of the plot
                   12613: 
                   12614: =item $xlabel: string, text describing the X-axis of the plot
                   12615: 
                   12616: =item $ylabel: string, text describing the Y-axis of the plot
                   12617: 
                   12618: =item $colors: Array ref containing the hex color codes for the data to be 
                   12619: plotted in.  If undefined, default values will be used.
                   12620: 
                   12621: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   12622: 
                   12623: =item $Ydata1: The first data set
                   12624: 
                   12625: =item $Min1: The minimum value of the left Y-axis
                   12626: 
                   12627: =item $Max1: The maximum value of the left Y-axis
                   12628: 
                   12629: =item $Ydata2: The second data set
                   12630: 
                   12631: =item $Min2: The minimum value of the right Y-axis
                   12632: 
                   12633: =item $Max2: The maximum value of the left Y-axis
                   12634: 
                   12635: =item %Values: hash indicating or overriding any default values which are 
                   12636: passed to graph.png.  
                   12637: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   12638: 
                   12639: =back
                   12640: 
                   12641: Returns:
                   12642: 
                   12643: An <img> tag which references graph.png and the appropriate identifying
                   12644: information for the plot.
1.136     matthew  12645: 
                   12646: =cut
                   12647: 
                   12648: ############################################################
                   12649: ############################################################
1.137     matthew  12650: sub DrawXYYGraph {
                   12651:     my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
                   12652:                                         $Ydata2,$Min2,$Max2,%Values)=@_;
1.136     matthew  12653:     #
                   12654:     # Create the identifier for the graph
                   12655:     my $identifier = &get_cgi_id();
                   12656:     my $id = 'cgi.'.$identifier;
                   12657:     #
                   12658:     $Title  = '' if (! defined($Title));
                   12659:     $xlabel = '' if (! defined($xlabel));
                   12660:     $ylabel = '' if (! defined($ylabel));
                   12661:     my %ValuesHash = 
                   12662:         (
1.369     www      12663:          $id.'.title'  => &escape($Title),
                   12664:          $id.'.xlabel' => &escape($xlabel),
                   12665:          $id.'.ylabel' => &escape($ylabel),
1.136     matthew  12666:          $id.'.labels' => join(',',@$Xlabels),
                   12667:          $id.'.PlotType' => 'XY',
                   12668:          $id.'.NumSets' => 2,
1.137     matthew  12669:          $id.'.two_axes' => 1,
                   12670:          $id.'.y1_max_value' => $Max1,
                   12671:          $id.'.y1_min_value' => $Min1,
                   12672:          $id.'.y2_max_value' => $Max2,
                   12673:          $id.'.y2_min_value' => $Min2,
1.136     matthew  12674:          );
                   12675:     #
1.137     matthew  12676:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   12677:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   12678:     }
                   12679:     #
                   12680:     if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
                   12681:         ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
1.136     matthew  12682:         return '';
                   12683:     }
                   12684:     my $NumSets=1;
1.137     matthew  12685:     foreach my $array ($Ydata1,$Ydata2){
1.136     matthew  12686:         next if (! ref($array));
                   12687:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
1.137     matthew  12688:     }
                   12689:     #
                   12690:     # Deal with other parameters
                   12691:     while (my ($key,$value) = each(%Values)) {
                   12692:         $ValuesHash{$id.'.'.$key} = $value;
1.136     matthew  12693:     }
                   12694:     #
1.646     raeburn  12695:     &Apache::lonnet::appenv(\%ValuesHash);
1.130     albertel 12696:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
1.139     matthew  12697: }
                   12698: 
                   12699: ############################################################
                   12700: ############################################################
                   12701: 
                   12702: =pod
                   12703: 
1.157     matthew  12704: =back 
                   12705: 
1.139     matthew  12706: =head1 Statistics helper routines?  
                   12707: 
                   12708: Bad place for them but what the hell.
                   12709: 
1.157     matthew  12710: =over 4
                   12711: 
1.648     raeburn  12712: =item * &chartlink()
1.139     matthew  12713: 
                   12714: Returns a link to the chart for a specific student.  
                   12715: 
                   12716: Inputs:
                   12717: 
                   12718: =over 4
                   12719: 
                   12720: =item $linktext: The text of the link
                   12721: 
                   12722: =item $sname: The students username
                   12723: 
                   12724: =item $sdomain: The students domain
                   12725: 
                   12726: =back
                   12727: 
1.157     matthew  12728: =back
                   12729: 
1.139     matthew  12730: =cut
                   12731: 
                   12732: ############################################################
                   12733: ############################################################
                   12734: sub chartlink {
                   12735:     my ($linktext, $sname, $sdomain) = @_;
                   12736:     my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
1.369     www      12737:         '&amp;SelectedStudent='.&escape($sname.':'.$sdomain).
1.219     albertel 12738:         '&amp;chartoutputmode='.HTML::Entities::encode('html, with all links').
1.139     matthew  12739:        '">'.$linktext.'</a>';
1.153     matthew  12740: }
                   12741: 
                   12742: #######################################################
                   12743: #######################################################
                   12744: 
                   12745: =pod
                   12746: 
                   12747: =head1 Course Environment Routines
1.157     matthew  12748: 
                   12749: =over 4
1.153     matthew  12750: 
1.648     raeburn  12751: =item * &restore_course_settings()
1.153     matthew  12752: 
1.648     raeburn  12753: =item * &store_course_settings()
1.153     matthew  12754: 
                   12755: Restores/Store indicated form parameters from the course environment.
                   12756: Will not overwrite existing values of the form parameters.
                   12757: 
                   12758: Inputs: 
                   12759: a scalar describing the data (e.g. 'chart', 'problem_analysis')
                   12760: 
                   12761: a hash ref describing the data to be stored.  For example:
                   12762:    
                   12763: %Save_Parameters = ('Status' => 'scalar',
                   12764:     'chartoutputmode' => 'scalar',
                   12765:     'chartoutputdata' => 'scalar',
                   12766:     'Section' => 'array',
1.373     raeburn  12767:     'Group' => 'array',
1.153     matthew  12768:     'StudentData' => 'array',
                   12769:     'Maps' => 'array');
                   12770: 
                   12771: Returns: both routines return nothing
                   12772: 
1.631     raeburn  12773: =back
                   12774: 
1.153     matthew  12775: =cut
                   12776: 
                   12777: #######################################################
                   12778: #######################################################
                   12779: sub store_course_settings {
1.496     albertel 12780:     return &store_settings($env{'request.course.id'},@_);
                   12781: }
                   12782: 
                   12783: sub store_settings {
1.153     matthew  12784:     # save to the environment
                   12785:     # appenv the same items, just to be safe
1.300     albertel 12786:     my $udom  = $env{'user.domain'};
                   12787:     my $uname = $env{'user.name'};
1.496     albertel 12788:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  12789:     my %SaveHash;
                   12790:     my %AppHash;
                   12791:     while (my ($setting,$type) = each(%$Settings)) {
1.496     albertel 12792:         my $basename = join('.','internal',$context,$prefix,$setting);
1.300     albertel 12793:         my $envname = 'environment.'.$basename;
1.258     albertel 12794:         if (exists($env{'form.'.$setting})) {
1.153     matthew  12795:             # Save this value away
                   12796:             if ($type eq 'scalar' &&
1.258     albertel 12797:                 (! exists($env{$envname}) || 
                   12798:                  $env{$envname} ne $env{'form.'.$setting})) {
                   12799:                 $SaveHash{$basename} = $env{'form.'.$setting};
                   12800:                 $AppHash{$envname}   = $env{'form.'.$setting};
1.153     matthew  12801:             } elsif ($type eq 'array') {
                   12802:                 my $stored_form;
1.258     albertel 12803:                 if (ref($env{'form.'.$setting})) {
1.153     matthew  12804:                     $stored_form = join(',',
                   12805:                                         map {
1.369     www      12806:                                             &escape($_);
1.258     albertel 12807:                                         } sort(@{$env{'form.'.$setting}}));
1.153     matthew  12808:                 } else {
                   12809:                     $stored_form = 
1.369     www      12810:                         &escape($env{'form.'.$setting});
1.153     matthew  12811:                 }
                   12812:                 # Determine if the array contents are the same.
1.258     albertel 12813:                 if ($stored_form ne $env{$envname}) {
1.153     matthew  12814:                     $SaveHash{$basename} = $stored_form;
                   12815:                     $AppHash{$envname}   = $stored_form;
                   12816:                 }
                   12817:             }
                   12818:         }
                   12819:     }
                   12820:     my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
1.300     albertel 12821:                                           $udom,$uname);
1.153     matthew  12822:     if ($put_result !~ /^(ok|delayed)/) {
                   12823:         &Apache::lonnet::logthis('unable to save form parameters, '.
                   12824:                                  'got error:'.$put_result);
                   12825:     }
                   12826:     # Make sure these settings stick around in this session, too
1.646     raeburn  12827:     &Apache::lonnet::appenv(\%AppHash);
1.153     matthew  12828:     return;
                   12829: }
                   12830: 
                   12831: sub restore_course_settings {
1.499     albertel 12832:     return &restore_settings($env{'request.course.id'},@_);
1.496     albertel 12833: }
                   12834: 
                   12835: sub restore_settings {
                   12836:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  12837:     while (my ($setting,$type) = each(%$Settings)) {
1.258     albertel 12838:         next if (exists($env{'form.'.$setting}));
1.496     albertel 12839:         my $envname = 'environment.internal.'.$context.'.'.$prefix.
1.153     matthew  12840:             '.'.$setting;
1.258     albertel 12841:         if (exists($env{$envname})) {
1.153     matthew  12842:             if ($type eq 'scalar') {
1.258     albertel 12843:                 $env{'form.'.$setting} = $env{$envname};
1.153     matthew  12844:             } elsif ($type eq 'array') {
1.258     albertel 12845:                 $env{'form.'.$setting} = [ 
1.153     matthew  12846:                                            map { 
1.369     www      12847:                                                &unescape($_); 
1.258     albertel 12848:                                            } split(',',$env{$envname})
1.153     matthew  12849:                                            ];
                   12850:             }
                   12851:         }
                   12852:     }
1.127     matthew  12853: }
                   12854: 
1.618     raeburn  12855: #######################################################
                   12856: #######################################################
                   12857: 
                   12858: =pod
                   12859: 
                   12860: =head1 Domain E-mail Routines  
                   12861: 
                   12862: =over 4
                   12863: 
1.648     raeburn  12864: =item * &build_recipient_list()
1.618     raeburn  12865: 
1.884     raeburn  12866: Build recipient lists for five types of e-mail:
1.766     raeburn  12867: (a) Error Reports, (b) Package Updates, (c) lonstatus warnings/errors
1.884     raeburn  12868: (d) Help requests, (e) Course requests needing approval,  generated by
                   12869: lonerrorhandler.pm, CHECKRPMS, loncron, lonsupportreq.pm and
                   12870: loncoursequeueadmin.pm respectively.
1.618     raeburn  12871: 
                   12872: Inputs:
1.619     raeburn  12873: defmail (scalar - email address of default recipient), 
1.618     raeburn  12874: mailing type (scalar - errormail, packagesmail, or helpdeskmail), 
1.619     raeburn  12875: defdom (domain for which to retrieve configuration settings),
                   12876: origmail (scalar - email address of recipient from loncapa.conf, 
                   12877: i.e., predates configuration by DC via domainprefs.pm 
1.618     raeburn  12878: 
1.655     raeburn  12879: Returns: comma separated list of addresses to which to send e-mail.
                   12880: 
                   12881: =back
1.618     raeburn  12882: 
                   12883: =cut
                   12884: 
                   12885: ############################################################
                   12886: ############################################################
                   12887: sub build_recipient_list {
1.619     raeburn  12888:     my ($defmail,$mailing,$defdom,$origmail) = @_;
1.618     raeburn  12889:     my @recipients;
                   12890:     my $otheremails;
                   12891:     my %domconfig =
                   12892:          &Apache::lonnet::get_dom('configuration',['contacts'],$defdom);
                   12893:     if (ref($domconfig{'contacts'}) eq 'HASH') {
1.766     raeburn  12894:         if (exists($domconfig{'contacts'}{$mailing})) {
                   12895:             if (ref($domconfig{'contacts'}{$mailing}) eq 'HASH') {
                   12896:                 my @contacts = ('adminemail','supportemail');
                   12897:                 foreach my $item (@contacts) {
                   12898:                     if ($domconfig{'contacts'}{$mailing}{$item}) {
                   12899:                         my $addr = $domconfig{'contacts'}{$item}; 
                   12900:                         if (!grep(/^\Q$addr\E$/,@recipients)) {
                   12901:                             push(@recipients,$addr);
                   12902:                         }
1.619     raeburn  12903:                     }
1.766     raeburn  12904:                     $otheremails = $domconfig{'contacts'}{$mailing}{'others'};
1.618     raeburn  12905:                 }
                   12906:             }
1.766     raeburn  12907:         } elsif ($origmail ne '') {
                   12908:             push(@recipients,$origmail);
1.618     raeburn  12909:         }
1.619     raeburn  12910:     } elsif ($origmail ne '') {
                   12911:         push(@recipients,$origmail);
1.618     raeburn  12912:     }
1.688     raeburn  12913:     if (defined($defmail)) {
                   12914:         if ($defmail ne '') {
                   12915:             push(@recipients,$defmail);
                   12916:         }
1.618     raeburn  12917:     }
                   12918:     if ($otheremails) {
1.619     raeburn  12919:         my @others;
                   12920:         if ($otheremails =~ /,/) {
                   12921:             @others = split(/,/,$otheremails);
1.618     raeburn  12922:         } else {
1.619     raeburn  12923:             push(@others,$otheremails);
                   12924:         }
                   12925:         foreach my $addr (@others) {
                   12926:             if (!grep(/^\Q$addr\E$/,@recipients)) {
                   12927:                 push(@recipients,$addr);
                   12928:             }
1.618     raeburn  12929:         }
                   12930:     }
1.619     raeburn  12931:     my $recipientlist = join(',',@recipients); 
1.618     raeburn  12932:     return $recipientlist;
                   12933: }
                   12934: 
1.127     matthew  12935: ############################################################
                   12936: ############################################################
1.154     albertel 12937: 
1.655     raeburn  12938: =pod
                   12939: 
                   12940: =head1 Course Catalog Routines
                   12941: 
                   12942: =over 4
                   12943: 
                   12944: =item * &gather_categories()
                   12945: 
                   12946: Converts category definitions - keys of categories hash stored in  
                   12947: coursecategories in configuration.db on the primary library server in a 
                   12948: domain - to an array.  Also generates javascript and idx hash used to 
                   12949: generate Domain Coordinator interface for editing Course Categories.
                   12950: 
                   12951: Inputs:
1.663     raeburn  12952: 
1.655     raeburn  12953: categories (reference to hash of category definitions).
1.663     raeburn  12954: 
1.655     raeburn  12955: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   12956:       categories and subcategories).
1.663     raeburn  12957: 
1.655     raeburn  12958: idx (reference to hash of counters used in Domain Coordinator interface for 
                   12959:       editing Course Categories).
1.663     raeburn  12960: 
1.655     raeburn  12961: jsarray (reference to array of categories used to create Javascript arrays for
                   12962:          Domain Coordinator interface for editing Course Categories).
                   12963: 
                   12964: Returns: nothing
                   12965: 
                   12966: Side effects: populates cats, idx and jsarray. 
                   12967: 
                   12968: =cut
                   12969: 
                   12970: sub gather_categories {
                   12971:     my ($categories,$cats,$idx,$jsarray) = @_;
                   12972:     my %counters;
                   12973:     my $num = 0;
                   12974:     foreach my $item (keys(%{$categories})) {
                   12975:         my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
                   12976:         if ($container eq '' && $depth == 0) {
                   12977:             $cats->[$depth][$categories->{$item}] = $cat;
                   12978:         } else {
                   12979:             $cats->[$depth]{$container}[$categories->{$item}] = $cat;
                   12980:         }
                   12981:         my ($escitem,$tail) = split(/:/,$item,2);
                   12982:         if ($counters{$tail} eq '') {
                   12983:             $counters{$tail} = $num;
                   12984:             $num ++;
                   12985:         }
                   12986:         if (ref($idx) eq 'HASH') {
                   12987:             $idx->{$item} = $counters{$tail};
                   12988:         }
                   12989:         if (ref($jsarray) eq 'ARRAY') {
                   12990:             push(@{$jsarray->[$counters{$tail}]},$item);
                   12991:         }
                   12992:     }
                   12993:     return;
                   12994: }
                   12995: 
                   12996: =pod
                   12997: 
                   12998: =item * &extract_categories()
                   12999: 
                   13000: Used to generate breadcrumb trails for course categories.
                   13001: 
                   13002: Inputs:
1.663     raeburn  13003: 
1.655     raeburn  13004: categories (reference to hash of category definitions).
1.663     raeburn  13005: 
1.655     raeburn  13006: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   13007:       categories and subcategories).
1.663     raeburn  13008: 
1.655     raeburn  13009: trails (reference to array of breacrumb trails for each category).
1.663     raeburn  13010: 
1.655     raeburn  13011: allitems (reference to hash - key is category key 
                   13012:          (format: escaped(name):escaped(parent category):depth in hierarchy).
1.663     raeburn  13013: 
1.655     raeburn  13014: idx (reference to hash of counters used in Domain Coordinator interface for
                   13015:       editing Course Categories).
1.663     raeburn  13016: 
1.655     raeburn  13017: jsarray (reference to array of categories used to create Javascript arrays for
                   13018:          Domain Coordinator interface for editing Course Categories).
                   13019: 
1.665     raeburn  13020: subcats (reference to hash of arrays containing all subcategories within each 
                   13021:          category, -recursive)
                   13022: 
1.655     raeburn  13023: Returns: nothing
                   13024: 
                   13025: Side effects: populates trails and allitems hash references.
                   13026: 
                   13027: =cut
                   13028: 
                   13029: sub extract_categories {
1.665     raeburn  13030:     my ($categories,$cats,$trails,$allitems,$idx,$jsarray,$subcats) = @_;
1.655     raeburn  13031:     if (ref($categories) eq 'HASH') {
                   13032:         &gather_categories($categories,$cats,$idx,$jsarray);
                   13033:         if (ref($cats->[0]) eq 'ARRAY') {
                   13034:             for (my $i=0; $i<@{$cats->[0]}; $i++) {
                   13035:                 my $name = $cats->[0][$i];
                   13036:                 my $item = &escape($name).'::0';
                   13037:                 my $trailstr;
                   13038:                 if ($name eq 'instcode') {
                   13039:                     $trailstr = &mt('Official courses (with institutional codes)');
1.919     raeburn  13040:                 } elsif ($name eq 'communities') {
                   13041:                     $trailstr = &mt('Communities');
1.655     raeburn  13042:                 } else {
                   13043:                     $trailstr = $name;
                   13044:                 }
                   13045:                 if ($allitems->{$item} eq '') {
                   13046:                     push(@{$trails},$trailstr);
                   13047:                     $allitems->{$item} = scalar(@{$trails})-1;
                   13048:                 }
                   13049:                 my @parents = ($name);
                   13050:                 if (ref($cats->[1]{$name}) eq 'ARRAY') {
                   13051:                     for (my $j=0; $j<@{$cats->[1]{$name}}; $j++) {
                   13052:                         my $category = $cats->[1]{$name}[$j];
1.665     raeburn  13053:                         if (ref($subcats) eq 'HASH') {
                   13054:                             push(@{$subcats->{$item}},&escape($category).':'.&escape($name).':1');
                   13055:                         }
                   13056:                         &recurse_categories($cats,2,$category,$trails,$allitems,\@parents,$subcats);
                   13057:                     }
                   13058:                 } else {
                   13059:                     if (ref($subcats) eq 'HASH') {
                   13060:                         $subcats->{$item} = [];
1.655     raeburn  13061:                     }
                   13062:                 }
                   13063:             }
                   13064:         }
                   13065:     }
                   13066:     return;
                   13067: }
                   13068: 
                   13069: =pod
                   13070: 
                   13071: =item *&recurse_categories()
                   13072: 
                   13073: Recursively used to generate breadcrumb trails for course categories.
                   13074: 
                   13075: Inputs:
1.663     raeburn  13076: 
1.655     raeburn  13077: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   13078:       categories and subcategories).
1.663     raeburn  13079: 
1.655     raeburn  13080: depth (current depth in hierarchy of categories and sub-categories - 0 indexed).
1.663     raeburn  13081: 
                   13082: category (current course category, for which breadcrumb trail is being generated).
                   13083: 
                   13084: trails (reference to array of breadcrumb trails for each category).
                   13085: 
1.655     raeburn  13086: allitems (reference to hash - key is category key
                   13087:          (format: escaped(name):escaped(parent category):depth in hierarchy).
1.663     raeburn  13088: 
1.655     raeburn  13089: parents (array containing containers directories for current category, 
                   13090:          back to top level). 
                   13091: 
                   13092: Returns: nothing
                   13093: 
                   13094: Side effects: populates trails and allitems hash references
                   13095: 
                   13096: =cut
                   13097: 
                   13098: sub recurse_categories {
1.665     raeburn  13099:     my ($cats,$depth,$category,$trails,$allitems,$parents,$subcats) = @_;
1.655     raeburn  13100:     my $shallower = $depth - 1;
                   13101:     if (ref($cats->[$depth]{$category}) eq 'ARRAY') {
                   13102:         for (my $k=0; $k<@{$cats->[$depth]{$category}}; $k++) {
                   13103:             my $name = $cats->[$depth]{$category}[$k];
                   13104:             my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
                   13105:             my $trailstr = join(' -&gt; ',(@{$parents},$category));
                   13106:             if ($allitems->{$item} eq '') {
                   13107:                 push(@{$trails},$trailstr);
                   13108:                 $allitems->{$item} = scalar(@{$trails})-1;
                   13109:             }
                   13110:             my $deeper = $depth+1;
                   13111:             push(@{$parents},$category);
1.665     raeburn  13112:             if (ref($subcats) eq 'HASH') {
                   13113:                 my $subcat = &escape($name).':'.$category.':'.$depth;
                   13114:                 for (my $j=@{$parents}; $j>=0; $j--) {
                   13115:                     my $higher;
                   13116:                     if ($j > 0) {
                   13117:                         $higher = &escape($parents->[$j]).':'.
                   13118:                                   &escape($parents->[$j-1]).':'.$j;
                   13119:                     } else {
                   13120:                         $higher = &escape($parents->[$j]).'::'.$j;
                   13121:                     }
                   13122:                     push(@{$subcats->{$higher}},$subcat);
                   13123:                 }
                   13124:             }
                   13125:             &recurse_categories($cats,$deeper,$name,$trails,$allitems,$parents,
                   13126:                                 $subcats);
1.655     raeburn  13127:             pop(@{$parents});
                   13128:         }
                   13129:     } else {
                   13130:         my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
                   13131:         my $trailstr = join(' -&gt; ',(@{$parents},$category));
                   13132:         if ($allitems->{$item} eq '') {
                   13133:             push(@{$trails},$trailstr);
                   13134:             $allitems->{$item} = scalar(@{$trails})-1;
                   13135:         }
                   13136:     }
                   13137:     return;
                   13138: }
                   13139: 
1.663     raeburn  13140: =pod
                   13141: 
                   13142: =item *&assign_categories_table()
                   13143: 
                   13144: Create a datatable for display of hierarchical categories in a domain,
                   13145: with checkboxes to allow a course to be categorized. 
                   13146: 
                   13147: Inputs:
                   13148: 
                   13149: cathash - reference to hash of categories defined for the domain (from
                   13150:           configuration.db)
                   13151: 
                   13152: currcat - scalar with an & separated list of categories assigned to a course. 
                   13153: 
1.919     raeburn  13154: type    - scalar contains course type (Course or Community).
                   13155: 
1.663     raeburn  13156: Returns: $output (markup to be displayed) 
                   13157: 
                   13158: =cut
                   13159: 
                   13160: sub assign_categories_table {
1.919     raeburn  13161:     my ($cathash,$currcat,$type) = @_;
1.663     raeburn  13162:     my $output;
                   13163:     if (ref($cathash) eq 'HASH') {
                   13164:         my (@cats,@trails,%allitems,%idx,@jsarray,@path,$maxdepth);
                   13165:         &extract_categories($cathash,\@cats,\@trails,\%allitems,\%idx,\@jsarray);
                   13166:         $maxdepth = scalar(@cats);
                   13167:         if (@cats > 0) {
                   13168:             my $itemcount = 0;
                   13169:             if (ref($cats[0]) eq 'ARRAY') {
                   13170:                 my @currcategories;
                   13171:                 if ($currcat ne '') {
                   13172:                     @currcategories = split('&',$currcat);
                   13173:                 }
1.919     raeburn  13174:                 my $table;
1.663     raeburn  13175:                 for (my $i=0; $i<@{$cats[0]}; $i++) {
                   13176:                     my $parent = $cats[0][$i];
1.919     raeburn  13177:                     next if ($parent eq 'instcode');
                   13178:                     if ($type eq 'Community') {
                   13179:                         next unless ($parent eq 'communities');
                   13180:                     } else {
                   13181:                         next if ($parent eq 'communities');
                   13182:                     }
1.663     raeburn  13183:                     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   13184:                     my $item = &escape($parent).'::0';
                   13185:                     my $checked = '';
                   13186:                     if (@currcategories > 0) {
                   13187:                         if (grep(/^\Q$item\E$/,@currcategories)) {
1.772     bisitz   13188:                             $checked = ' checked="checked"';
1.663     raeburn  13189:                         }
                   13190:                     }
1.919     raeburn  13191:                     my $parent_title = $parent;
                   13192:                     if ($parent eq 'communities') {
                   13193:                         $parent_title = &mt('Communities');
                   13194:                     }
                   13195:                     $table .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.
                   13196:                               '<input type="checkbox" name="usecategory" value="'.
                   13197:                               $item.'"'.$checked.' />'.$parent_title.'</span>'.
                   13198:                               '<input type="hidden" name="catname" value="'.$parent.'" /></td>';
1.663     raeburn  13199:                     my $depth = 1;
                   13200:                     push(@path,$parent);
1.919     raeburn  13201:                     $table .= &assign_category_rows($itemcount,\@cats,$depth,$parent,\@path,\@currcategories);
1.663     raeburn  13202:                     pop(@path);
1.919     raeburn  13203:                     $table .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
1.663     raeburn  13204:                     $itemcount ++;
                   13205:                 }
1.919     raeburn  13206:                 if ($itemcount) {
                   13207:                     $output = &Apache::loncommon::start_data_table().
                   13208:                               $table.
                   13209:                               &Apache::loncommon::end_data_table();
                   13210:                 }
1.663     raeburn  13211:             }
                   13212:         }
                   13213:     }
                   13214:     return $output;
                   13215: }
                   13216: 
                   13217: =pod
                   13218: 
                   13219: =item *&assign_category_rows()
                   13220: 
                   13221: Create a datatable row for display of nested categories in a domain,
                   13222: with checkboxes to allow a course to be categorized,called recursively.
                   13223: 
                   13224: Inputs:
                   13225: 
                   13226: itemcount - track row number for alternating colors
                   13227: 
                   13228: cats - reference to array of arrays/hashes which encapsulates hierarchy of
                   13229:       categories and subcategories.
                   13230: 
                   13231: depth - current depth in hierarchy of categories and sub-categories - 0 indexed.
                   13232: 
                   13233: parent - parent of current category item
                   13234: 
                   13235: path - Array containing all categories back up through the hierarchy from the
                   13236:        current category to the top level.
                   13237: 
                   13238: currcategories - reference to array of current categories assigned to the course
                   13239: 
                   13240: Returns: $output (markup to be displayed).
                   13241: 
                   13242: =cut
                   13243: 
                   13244: sub assign_category_rows {
                   13245:     my ($itemcount,$cats,$depth,$parent,$path,$currcategories) = @_;
                   13246:     my ($text,$name,$item,$chgstr);
                   13247:     if (ref($cats) eq 'ARRAY') {
                   13248:         my $maxdepth = scalar(@{$cats});
                   13249:         if (ref($cats->[$depth]) eq 'HASH') {
                   13250:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
                   13251:                 my $numchildren = @{$cats->[$depth]{$parent}};
                   13252:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   13253:                 $text .= '<td><table class="LC_datatable">';
                   13254:                 for (my $j=0; $j<$numchildren; $j++) {
                   13255:                     $name = $cats->[$depth]{$parent}[$j];
                   13256:                     $item = &escape($name).':'.&escape($parent).':'.$depth;
                   13257:                     my $deeper = $depth+1;
                   13258:                     my $checked = '';
                   13259:                     if (ref($currcategories) eq 'ARRAY') {
                   13260:                         if (@{$currcategories} > 0) {
                   13261:                             if (grep(/^\Q$item\E$/,@{$currcategories})) {
1.772     bisitz   13262:                                 $checked = ' checked="checked"';
1.663     raeburn  13263:                             }
                   13264:                         }
                   13265:                     }
1.664     raeburn  13266:                     $text .= '<tr><td><span class="LC_nobreak"><label>'.
                   13267:                              '<input type="checkbox" name="usecategory" value="'.
1.675     raeburn  13268:                              $item.'"'.$checked.' />'.$name.'</label></span>'.
                   13269:                              '<input type="hidden" name="catname" value="'.$name.'" />'.
                   13270:                              '</td><td>';
1.663     raeburn  13271:                     if (ref($path) eq 'ARRAY') {
                   13272:                         push(@{$path},$name);
                   13273:                         $text .= &assign_category_rows($itemcount,$cats,$deeper,$name,$path,$currcategories);
                   13274:                         pop(@{$path});
                   13275:                     }
                   13276:                     $text .= '</td></tr>';
                   13277:                 }
                   13278:                 $text .= '</table></td>';
                   13279:             }
                   13280:         }
                   13281:     }
                   13282:     return $text;
                   13283: }
                   13284: 
1.655     raeburn  13285: ############################################################
                   13286: ############################################################
                   13287: 
                   13288: 
1.443     albertel 13289: sub commit_customrole {
1.664     raeburn  13290:     my ($udom,$uname,$url,$three,$four,$five,$start,$end,$context) = @_;
1.630     raeburn  13291:     my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.':'.$three.' in '.$url.
1.443     albertel 13292:                          ($start?', '.&mt('starting').' '.localtime($start):'').
                   13293:                          ($end?', ending '.localtime($end):'').': <b>'.
                   13294:               &Apache::lonnet::assigncustomrole(
1.664     raeburn  13295:                  $udom,$uname,$url,$three,$four,$five,$end,$start,undef,undef,$context).
1.443     albertel 13296:                  '</b><br />';
                   13297:     return $output;
                   13298: }
                   13299: 
                   13300: sub commit_standardrole {
1.1075.2.31  raeburn  13301:     my ($udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context,$credits) = @_;
1.541     raeburn  13302:     my ($output,$logmsg,$linefeed);
                   13303:     if ($context eq 'auto') {
                   13304:         $linefeed = "\n";
                   13305:     } else {
                   13306:         $linefeed = "<br />\n";
                   13307:     }  
1.443     albertel 13308:     if ($three eq 'st') {
1.541     raeburn  13309:         my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$three,$start,$end,
1.1075.2.31  raeburn  13310:                                          $one,$two,$sec,$context,$credits);
1.541     raeburn  13311:         if (($result =~ /^error/) || ($result eq 'not_in_class') || 
1.626     raeburn  13312:             ($result eq 'unknown_course') || ($result eq 'refused')) {
                   13313:             $output = $logmsg.' '.&mt('Error: ').$result."\n"; 
1.443     albertel 13314:         } else {
1.541     raeburn  13315:             $output = $logmsg.$linefeed.&mt('Assigning').' '.$three.' in '.$url.
1.443     albertel 13316:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  13317:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
                   13318:             if ($context eq 'auto') {
                   13319:                 $output .= $result.$linefeed.&mt('Add to classlist').': ok';
                   13320:             } else {
                   13321:                $output .= '<b>'.$result.'</b>'.$linefeed.
                   13322:                &mt('Add to classlist').': <b>ok</b>';
                   13323:             }
                   13324:             $output .= $linefeed;
1.443     albertel 13325:         }
                   13326:     } else {
                   13327:         $output = &mt('Assigning').' '.$three.' in '.$url.
                   13328:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  13329:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
1.652     raeburn  13330:         my $result = &Apache::lonnet::assignrole($udom,$uname,$url,$three,$end,$start,'','',$context);
1.541     raeburn  13331:         if ($context eq 'auto') {
                   13332:             $output .= $result.$linefeed;
                   13333:         } else {
                   13334:             $output .= '<b>'.$result.'</b>'.$linefeed;
                   13335:         }
1.443     albertel 13336:     }
                   13337:     return $output;
                   13338: }
                   13339: 
                   13340: sub commit_studentrole {
1.1075.2.31  raeburn  13341:     my ($logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context,
                   13342:         $credits) = @_;
1.626     raeburn  13343:     my ($result,$linefeed,$oldsecurl,$newsecurl);
1.541     raeburn  13344:     if ($context eq 'auto') {
                   13345:         $linefeed = "\n";
                   13346:     } else {
                   13347:         $linefeed = '<br />'."\n";
                   13348:     }
1.443     albertel 13349:     if (defined($one) && defined($two)) {
                   13350:         my $cid=$one.'_'.$two;
                   13351:         my $oldsec=&Apache::lonnet::getsection($udom,$uname,$cid);
                   13352:         my $secchange = 0;
                   13353:         my $expire_role_result;
                   13354:         my $modify_section_result;
1.628     raeburn  13355:         if ($oldsec ne '-1') { 
                   13356:             if ($oldsec ne $sec) {
1.443     albertel 13357:                 $secchange = 1;
1.628     raeburn  13358:                 my $now = time;
1.443     albertel 13359:                 my $uurl='/'.$cid;
                   13360:                 $uurl=~s/\_/\//g;
                   13361:                 if ($oldsec) {
                   13362:                     $uurl.='/'.$oldsec;
                   13363:                 }
1.626     raeburn  13364:                 $oldsecurl = $uurl;
1.628     raeburn  13365:                 $expire_role_result = 
1.652     raeburn  13366:                     &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',$now,'','',$context);
1.628     raeburn  13367:                 if ($env{'request.course.sec'} ne '') { 
                   13368:                     if ($expire_role_result eq 'refused') {
                   13369:                         my @roles = ('st');
                   13370:                         my @statuses = ('previous');
                   13371:                         my @roledoms = ($one);
                   13372:                         my $withsec = 1;
                   13373:                         my %roleshash = 
                   13374:                             &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   13375:                                               \@statuses,\@roles,\@roledoms,$withsec);
                   13376:                         if (defined ($roleshash{$two.':'.$one.':st:'.$oldsec})) {
                   13377:                             my ($oldstart,$oldend) = 
                   13378:                                 split(':',$roleshash{$two.':'.$one.':st:'.$oldsec});
                   13379:                             if ($oldend > 0 && $oldend <= $now) {
                   13380:                                 $expire_role_result = 'ok';
                   13381:                             }
                   13382:                         }
                   13383:                     }
                   13384:                 }
1.443     albertel 13385:                 $result = $expire_role_result;
                   13386:             }
                   13387:         }
                   13388:         if (($expire_role_result eq 'ok') || ($secchange == 0)) {
1.1075.2.31  raeburn  13389:             $modify_section_result = 
                   13390:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,
                   13391:                                                            undef,undef,undef,$sec,
                   13392:                                                            $end,$start,'','',$cid,
                   13393:                                                            '',$context,$credits);
1.443     albertel 13394:             if ($modify_section_result =~ /^ok/) {
                   13395:                 if ($secchange == 1) {
1.628     raeburn  13396:                     if ($sec eq '') {
                   13397:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to student role without a section.',$uname,$oldsec).$linefeed;
                   13398:                     } else {
                   13399:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to new section: [_3].',$uname,$oldsec,$sec).$linefeed;
                   13400:                     }
1.443     albertel 13401:                 } elsif ($oldsec eq '-1') {
1.628     raeburn  13402:                     if ($sec eq '') {
                   13403:                         $$logmsg .= &mt('New student role without a section for [_1] in course [_2].',$uname,$cid).$linefeed;
                   13404:                     } else {
                   13405:                         $$logmsg .= &mt('New student role for [_1] in section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
                   13406:                     }
1.443     albertel 13407:                 } else {
1.628     raeburn  13408:                     if ($sec eq '') {
                   13409:                         $$logmsg .= &mt('Student [_1] assigned to course [_2] without a section.',$uname,$cid).$linefeed;
                   13410:                     } else {
                   13411:                         $$logmsg .= &mt('Student [_1] assigned to section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
                   13412:                     }
1.443     albertel 13413:                 }
                   13414:             } else {
1.628     raeburn  13415:                 if ($secchange) {       
                   13416:                     $$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;
                   13417:                 } else {
                   13418:                     $$logmsg .= &mt('Error when attempting to modify role for [_1] for section: "[_2]" in course [_3] -error:',$uname,$sec,$cid).' '.$modify_section_result.$linefeed;
                   13419:                 }
1.443     albertel 13420:             }
                   13421:             $result = $modify_section_result;
                   13422:         } elsif ($secchange == 1) {
1.628     raeburn  13423:             if ($oldsec eq '') {
1.1075.2.20  raeburn  13424:                 $$logmsg .= &mt('Error when attempting to expire existing role without a section for [_1] in course [_2] -error: ',$uname,$cid).' '.$expire_role_result.$linefeed;
1.628     raeburn  13425:             } else {
                   13426:                 $$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;
                   13427:             }
1.626     raeburn  13428:             if ($expire_role_result eq 'refused') {
                   13429:                 my $newsecurl = '/'.$cid;
                   13430:                 $newsecurl =~ s/\_/\//g;
                   13431:                 if ($sec ne '') {
                   13432:                     $newsecurl.='/'.$sec;
                   13433:                 }
                   13434:                 if (&Apache::lonnet::allowed('cst',$newsecurl) && !(&Apache::lonnet::allowed('cst',$oldsecurl))) {
                   13435:                     if ($sec eq '') {
                   13436:                         $$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;
                   13437:                     } else {
                   13438:                         $$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;
                   13439:                     }
                   13440:                 }
                   13441:             }
1.443     albertel 13442:         }
                   13443:     } else {
1.626     raeburn  13444:         $$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 13445:         $result = "error: incomplete course id\n";
                   13446:     }
                   13447:     return $result;
                   13448: }
                   13449: 
1.1075.2.25  raeburn  13450: sub show_role_extent {
                   13451:     my ($scope,$context,$role) = @_;
                   13452:     $scope =~ s{^/}{};
                   13453:     my @courseroles = &Apache::lonuserutils::roles_by_context('course',1);
                   13454:     push(@courseroles,'co');
                   13455:     my @authorroles = &Apache::lonuserutils::roles_by_context('author');
                   13456:     if (($context eq 'course') || (grep(/^\Q$role\E/,@courseroles))) {
                   13457:         $scope =~ s{/}{_};
                   13458:         return '<span class="LC_cusr_emph">'.$env{'course.'.$scope.'.description'}.'</span>';
                   13459:     } elsif (($context eq 'author') || (grep(/^\Q$role\E/,@authorroles))) {
                   13460:         my ($audom,$auname) = split(/\//,$scope);
                   13461:         return &mt('[_1] Author Space','<span class="LC_cusr_emph">'.
                   13462:                    &Apache::loncommon::plainname($auname,$audom).'</span>');
                   13463:     } else {
                   13464:         $scope =~ s{/$}{};
                   13465:         return &mt('Domain: [_1]','<span class="LC_cusr_emph">'.
                   13466:                    &Apache::lonnet::domain($scope,'description').'</span>');
                   13467:     }
                   13468: }
                   13469: 
1.443     albertel 13470: ############################################################
                   13471: ############################################################
                   13472: 
1.566     albertel 13473: sub check_clone {
1.578     raeburn  13474:     my ($args,$linefeed) = @_;
1.566     albertel 13475:     my $cloneid='/'.$args->{'clonedomain'}.'/'.$args->{'clonecourse'};
                   13476:     my ($clonecrsudom,$clonecrsunum)= &LONCAPA::split_courseid($cloneid);
                   13477:     my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
                   13478:     my $clonemsg;
                   13479:     my $can_clone = 0;
1.944     raeburn  13480:     my $lctype = lc($args->{'crstype'});
1.908     raeburn  13481:     if ($lctype ne 'community') {
                   13482:         $lctype = 'course';
                   13483:     }
1.566     albertel 13484:     if ($clonehome eq 'no_host') {
1.944     raeburn  13485:         if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  13486:             $clonemsg = &mt('No new community created.').$linefeed.&mt('A new community could not be cloned from the specified original - [_1] - because it is a non-existent community.',$args->{'clonecourse'}.':'.$args->{'clonedomain'});
                   13487:         } else {
                   13488:             $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'});
                   13489:         }     
1.566     albertel 13490:     } else {
                   13491: 	my %clonedesc = &Apache::lonnet::coursedescription($cloneid,{'one_time' => 1});
1.944     raeburn  13492:         if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  13493:             if ($clonedesc{'type'} ne 'Community') {
                   13494:                  $clonemsg = &mt('No new community created.').$linefeed.&mt('A new community could not be cloned from the specified original - [_1] - because it is a course not a community.',$args->{'clonecourse'}.':'.$args->{'clonedomain'});
                   13495:                 return ($can_clone, $clonemsg, $cloneid, $clonehome);
                   13496:             }
                   13497:         }
1.882     raeburn  13498: 	if (($env{'request.role.domain'} eq $args->{'clonedomain'}) && 
                   13499:             (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'}))) {
1.566     albertel 13500: 	    $can_clone = 1;
                   13501: 	} else {
                   13502: 	    my %clonehash = &Apache::lonnet::get('environment',['cloners'],
                   13503: 						 $args->{'clonedomain'},$args->{'clonecourse'});
                   13504: 	    my @cloners = split(/,/,$clonehash{'cloners'});
1.578     raeburn  13505:             if (grep(/^\*$/,@cloners)) {
                   13506:                 $can_clone = 1;
                   13507:             } elsif (grep(/^\*\:\Q$args->{'ccdomain'}\E$/,@cloners)) {
                   13508:                 $can_clone = 1;
                   13509:             } else {
1.908     raeburn  13510:                 my $ccrole = 'cc';
1.944     raeburn  13511:                 if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  13512:                     $ccrole = 'co';
                   13513:                 }
1.578     raeburn  13514: 	        my %roleshash =
                   13515: 		    &Apache::lonnet::get_my_roles($args->{'ccuname'},
                   13516: 					 $args->{'ccdomain'},
1.908     raeburn  13517:                                          'userroles',['active'],[$ccrole],
1.578     raeburn  13518: 					 [$args->{'clonedomain'}]);
1.908     raeburn  13519: 	        if (($roleshash{$args->{'clonecourse'}.':'.$args->{'clonedomain'}.':'.$ccrole}) || (grep(/^\Q$args->{'ccuname'}\E:\Q$args->{'ccdomain'}\E$/,@cloners))) {
1.942     raeburn  13520:                     $can_clone = 1;
                   13521:                 } elsif (&Apache::lonnet::is_course_owner($args->{'clonedomain'},$args->{'clonecourse'},$args->{'ccuname'},$args->{'ccdomain'})) {
                   13522:                     $can_clone = 1;
                   13523:                 } else {
1.944     raeburn  13524:                     if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  13525:                         $clonemsg = &mt('No new community created.').$linefeed.&mt('The new community could not be cloned from the existing community because the new community owner ([_1]) does not have cloning rights in the existing community ([_2]).',$args->{'ccuname'}.':'.$args->{'ccdomain'},$clonedesc{'description'});
                   13526:                     } else {
                   13527:                         $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'});
                   13528:                     }
1.578     raeburn  13529: 	        }
1.566     albertel 13530: 	    }
1.578     raeburn  13531:         }
1.566     albertel 13532:     }
                   13533:     return ($can_clone, $clonemsg, $cloneid, $clonehome);
                   13534: }
                   13535: 
1.444     albertel 13536: sub construct_course {
1.885     raeburn  13537:     my ($args,$logmsg,$courseid,$crsudom,$crsunum,$udom,$uname,$context,$cnum,$category) = @_;
1.444     albertel 13538:     my $outcome;
1.541     raeburn  13539:     my $linefeed =  '<br />'."\n";
                   13540:     if ($context eq 'auto') {
                   13541:         $linefeed = "\n";
                   13542:     }
1.566     albertel 13543: 
                   13544: #
                   13545: # Are we cloning?
                   13546: #
                   13547:     my ($can_clone, $clonemsg, $cloneid, $clonehome);
                   13548:     if (($args->{'clonecourse'}) && ($args->{'clonedomain'})) {
1.578     raeburn  13549: 	($can_clone, $clonemsg, $cloneid, $clonehome) = &check_clone($args,$linefeed);
1.566     albertel 13550: 	if ($context ne 'auto') {
1.578     raeburn  13551:             if ($clonemsg ne '') {
                   13552: 	        $clonemsg = '<span class="LC_error">'.$clonemsg.'</span>';
                   13553:             }
1.566     albertel 13554: 	}
                   13555: 	$outcome .= $clonemsg.$linefeed;
                   13556: 
                   13557:         if (!$can_clone) {
                   13558: 	    return (0,$outcome);
                   13559: 	}
                   13560:     }
                   13561: 
1.444     albertel 13562: #
                   13563: # Open course
                   13564: #
                   13565:     my $crstype = lc($args->{'crstype'});
                   13566:     my %cenv=();
                   13567:     $$courseid=&Apache::lonnet::createcourse($args->{'course_domain'},
                   13568:                                              $args->{'cdescr'},
                   13569:                                              $args->{'curl'},
                   13570:                                              $args->{'course_home'},
                   13571:                                              $args->{'nonstandard'},
                   13572:                                              $args->{'crscode'},
                   13573:                                              $args->{'ccuname'}.':'.
                   13574:                                              $args->{'ccdomain'},
1.882     raeburn  13575:                                              $args->{'crstype'},
1.885     raeburn  13576:                                              $cnum,$context,$category);
1.444     albertel 13577: 
                   13578:     # Note: The testing routines depend on this being output; see 
                   13579:     # Utils::Course. This needs to at least be output as a comment
                   13580:     # if anyone ever decides to not show this, and Utils::Course::new
                   13581:     # will need to be suitably modified.
1.541     raeburn  13582:     $outcome .= &mt('New LON-CAPA [_1] ID: [_2]',$crstype,$$courseid).$linefeed;
1.943     raeburn  13583:     if ($$courseid =~ /^error:/) {
                   13584:         return (0,$outcome);
                   13585:     }
                   13586: 
1.444     albertel 13587: #
                   13588: # Check if created correctly
                   13589: #
1.479     albertel 13590:     ($$crsudom,$$crsunum)= &LONCAPA::split_courseid($$courseid);
1.444     albertel 13591:     my $crsuhome=&Apache::lonnet::homeserver($$crsunum,$$crsudom);
1.943     raeburn  13592:     if ($crsuhome eq 'no_host') {
                   13593:         $outcome .= &mt('Course creation failed, unrecognized course home server.').$linefeed;
                   13594:         return (0,$outcome);
                   13595:     }
1.541     raeburn  13596:     $outcome .= &mt('Created on').': '.$crsuhome.$linefeed;
1.566     albertel 13597: 
1.444     albertel 13598: #
1.566     albertel 13599: # Do the cloning
                   13600: #   
                   13601:     if ($can_clone && $cloneid) {
                   13602: 	$clonemsg = &mt('Cloning [_1] from [_2]',$crstype,$clonehome);
                   13603: 	if ($context ne 'auto') {
                   13604: 	    $clonemsg = '<span class="LC_success">'.$clonemsg.'</span>';
                   13605: 	}
                   13606: 	$outcome .= $clonemsg.$linefeed;
                   13607: 	my %oldcenv=&Apache::lonnet::dump('environment',$$crsudom,$$crsunum);
1.444     albertel 13608: # Copy all files
1.637     www      13609: 	&Apache::lonclonecourse::copycoursefiles($cloneid,$$courseid,$args->{'datemode'},$args->{'dateshift'});
1.444     albertel 13610: # Restore URL
1.566     albertel 13611: 	$cenv{'url'}=$oldcenv{'url'};
1.444     albertel 13612: # Restore title
1.566     albertel 13613: 	$cenv{'description'}=$oldcenv{'description'};
1.955     raeburn  13614: # Restore creation date, creator and creation context.
                   13615:         $cenv{'internal.created'}=$oldcenv{'internal.created'};
                   13616:         $cenv{'internal.creator'}=$oldcenv{'internal.creator'};
                   13617:         $cenv{'internal.creationcontext'}=$oldcenv{'internal.creationcontext'};
1.444     albertel 13618: # Mark as cloned
1.566     albertel 13619: 	$cenv{'clonedfrom'}=$cloneid;
1.638     www      13620: # Need to clone grading mode
                   13621:         my %newenv=&Apache::lonnet::get('environment',['grading'],$$crsudom,$$crsunum);
                   13622:         $cenv{'grading'}=$newenv{'grading'};
                   13623: # Do not clone these environment entries
                   13624:         &Apache::lonnet::del('environment',
                   13625:                   ['default_enrollment_start_date',
                   13626:                    'default_enrollment_end_date',
                   13627:                    'question.email',
                   13628:                    'policy.email',
                   13629:                    'comment.email',
                   13630:                    'pch.users.denied',
1.725     raeburn  13631:                    'plc.users.denied',
                   13632:                    'hidefromcat',
                   13633:                    'categories'],
1.638     www      13634:                    $$crsudom,$$crsunum);
1.444     albertel 13635:     }
1.566     albertel 13636: 
1.444     albertel 13637: #
                   13638: # Set environment (will override cloned, if existing)
                   13639: #
                   13640:     my @sections = ();
                   13641:     my @xlists = ();
                   13642:     if ($args->{'crstype'}) {
                   13643:         $cenv{'type'}=$args->{'crstype'};
                   13644:     }
                   13645:     if ($args->{'crsid'}) {
                   13646:         $cenv{'courseid'}=$args->{'crsid'};
                   13647:     }
                   13648:     if ($args->{'crscode'}) {
                   13649:         $cenv{'internal.coursecode'}=$args->{'crscode'};
                   13650:     }
                   13651:     if ($args->{'crsquota'} ne '') {
                   13652:         $cenv{'internal.coursequota'}=$args->{'crsquota'};
                   13653:     } else {
                   13654:         $cenv{'internal.coursequota'}=$args->{'crsquota'} = 20;
                   13655:     }
                   13656:     if ($args->{'ccuname'}) {
                   13657:         $cenv{'internal.courseowner'} = $args->{'ccuname'}.
                   13658:                                         ':'.$args->{'ccdomain'};
                   13659:     } else {
                   13660:         $cenv{'internal.courseowner'} = $args->{'curruser'};
                   13661:     }
1.1075.2.31  raeburn  13662:     if ($args->{'defaultcredits'}) {
                   13663:         $cenv{'internal.defaultcredits'} = $args->{'defaultcredits'};
                   13664:     }
1.444     albertel 13665:     my @badclasses = (); # Used to accumulate sections/crosslistings that did not pass classlist access check for course owner.
                   13666:     if ($args->{'crssections'}) {
                   13667:         $cenv{'internal.sectionnums'} = '';
                   13668:         if ($args->{'crssections'} =~ m/,/) {
                   13669:             @sections = split/,/,$args->{'crssections'};
                   13670:         } else {
                   13671:             $sections[0] = $args->{'crssections'};
                   13672:         }
                   13673:         if (@sections > 0) {
                   13674:             foreach my $item (@sections) {
                   13675:                 my ($sec,$gp) = split/:/,$item;
                   13676:                 my $class = $args->{'crscode'}.$sec;
                   13677:                 my $addcheck = &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$class,$cenv{'internal.courseowner'});
                   13678:                 $cenv{'internal.sectionnums'} .= $item.',';
                   13679:                 unless ($addcheck eq 'ok') {
                   13680:                     push @badclasses, $class;
                   13681:                 }
                   13682:             }
                   13683:             $cenv{'internal.sectionnums'} =~ s/,$//;
                   13684:         }
                   13685:     }
                   13686: # do not hide course coordinator from staff listing, 
                   13687: # even if privileged
                   13688:     $cenv{'nothideprivileged'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   13689: # add crosslistings
                   13690:     if ($args->{'crsxlist'}) {
                   13691:         $cenv{'internal.crosslistings'}='';
                   13692:         if ($args->{'crsxlist'} =~ m/,/) {
                   13693:             @xlists = split/,/,$args->{'crsxlist'};
                   13694:         } else {
                   13695:             $xlists[0] = $args->{'crsxlist'};
                   13696:         }
                   13697:         if (@xlists > 0) {
                   13698:             foreach my $item (@xlists) {
                   13699:                 my ($xl,$gp) = split/:/,$item;
                   13700:                 my $addcheck =  &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$xl,$cenv{'internal.courseowner'});
                   13701:                 $cenv{'internal.crosslistings'} .= $item.',';
                   13702:                 unless ($addcheck eq 'ok') {
                   13703:                     push @badclasses, $xl;
                   13704:                 }
                   13705:             }
                   13706:             $cenv{'internal.crosslistings'} =~ s/,$//;
                   13707:         }
                   13708:     }
                   13709:     if ($args->{'autoadds'}) {
                   13710:         $cenv{'internal.autoadds'}=$args->{'autoadds'};
                   13711:     }
                   13712:     if ($args->{'autodrops'}) {
                   13713:         $cenv{'internal.autodrops'}=$args->{'autodrops'};
                   13714:     }
                   13715: # check for notification of enrollment changes
                   13716:     my @notified = ();
                   13717:     if ($args->{'notify_owner'}) {
                   13718:         if ($args->{'ccuname'} ne '') {
                   13719:             push(@notified,$args->{'ccuname'}.':'.$args->{'ccdomain'});
                   13720:         }
                   13721:     }
                   13722:     if ($args->{'notify_dc'}) {
                   13723:         if ($uname ne '') { 
1.630     raeburn  13724:             push(@notified,$uname.':'.$udom);
1.444     albertel 13725:         }
                   13726:     }
                   13727:     if (@notified > 0) {
                   13728:         my $notifylist;
                   13729:         if (@notified > 1) {
                   13730:             $notifylist = join(',',@notified);
                   13731:         } else {
                   13732:             $notifylist = $notified[0];
                   13733:         }
                   13734:         $cenv{'internal.notifylist'} = $notifylist;
                   13735:     }
                   13736:     if (@badclasses > 0) {
                   13737:         my %lt=&Apache::lonlocal::texthash(
                   13738:                 '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',
                   13739:                 'dnhr' => 'does not have rights to access enrollment in these classes',
                   13740:                 'adby' => 'as determined by the policies of your institution on access to official classlists'
                   13741:         );
1.541     raeburn  13742:         my $badclass_msg = $cenv{'internal.courseowner'}.') - '.$lt{'dnhr'}.
                   13743:                            ' ('.$lt{'adby'}.')';
                   13744:         if ($context eq 'auto') {
                   13745:             $outcome .= $badclass_msg.$linefeed;
1.566     albertel 13746:             $outcome .= '<div class="LC_warning">'.$badclass_msg.$linefeed.'<ul>'."\n";
1.541     raeburn  13747:             foreach my $item (@badclasses) {
                   13748:                 if ($context eq 'auto') {
                   13749:                     $outcome .= " - $item\n";
                   13750:                 } else {
                   13751:                     $outcome .= "<li>$item</li>\n";
                   13752:                 }
                   13753:             }
                   13754:             if ($context eq 'auto') {
                   13755:                 $outcome .= $linefeed;
                   13756:             } else {
1.566     albertel 13757:                 $outcome .= "</ul><br /><br /></div>\n";
1.541     raeburn  13758:             }
                   13759:         } 
1.444     albertel 13760:     }
                   13761:     if ($args->{'no_end_date'}) {
                   13762:         $args->{'endaccess'} = 0;
                   13763:     }
                   13764:     $cenv{'internal.autostart'}=$args->{'enrollstart'};
                   13765:     $cenv{'internal.autoend'}=$args->{'enrollend'};
                   13766:     $cenv{'default_enrollment_start_date'}=$args->{'startaccess'};
                   13767:     $cenv{'default_enrollment_end_date'}=$args->{'endaccess'};
                   13768:     if ($args->{'showphotos'}) {
                   13769:       $cenv{'internal.showphotos'}=$args->{'showphotos'};
                   13770:     }
                   13771:     $cenv{'internal.authtype'} = $args->{'authtype'};
                   13772:     $cenv{'internal.autharg'} = $args->{'autharg'}; 
                   13773:     if ( ($cenv{'internal.authtype'} =~ /^krb/) && ($cenv{'internal.autoadds'} == 1)) {
                   13774:         if (! defined($cenv{'internal.autharg'}) || $cenv{'internal.autharg'}  eq '') {
1.541     raeburn  13775:             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'); 
                   13776:             if ($context eq 'auto') {
                   13777:                 $outcome .= $krb_msg;
                   13778:             } else {
1.566     albertel 13779:                 $outcome .= '<span class="LC_error">'.$krb_msg.'</span>';
1.541     raeburn  13780:             }
                   13781:             $outcome .= $linefeed;
1.444     albertel 13782:         }
                   13783:     }
                   13784:     if (($args->{'ccdomain'}) && ($args->{'ccuname'})) {
                   13785:        if ($args->{'setpolicy'}) {
                   13786:            $cenv{'policy.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   13787:        }
                   13788:        if ($args->{'setcontent'}) {
                   13789:            $cenv{'question.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   13790:        }
                   13791:     }
                   13792:     if ($args->{'reshome'}) {
                   13793: 	$cenv{'reshome'}=$args->{'reshome'}.'/';
                   13794: 	$cenv{'reshome'}=~s/\/+$/\//;
                   13795:     }
                   13796: #
                   13797: # course has keyed access
                   13798: #
                   13799:     if ($args->{'setkeys'}) {
                   13800:        $cenv{'keyaccess'}='yes';
                   13801:     }
                   13802: # if specified, key authority is not course, but user
                   13803: # only active if keyaccess is yes
                   13804:     if ($args->{'keyauth'}) {
1.487     albertel 13805: 	my ($user,$domain) = split(':',$args->{'keyauth'});
                   13806: 	$user = &LONCAPA::clean_username($user);
                   13807: 	$domain = &LONCAPA::clean_username($domain);
1.488     foxr     13808: 	if ($user ne '' && $domain ne '') {
1.487     albertel 13809: 	    $cenv{'keyauth'}=$user.':'.$domain;
1.444     albertel 13810: 	}
                   13811:     }
                   13812: 
                   13813:     if ($args->{'disresdis'}) {
                   13814:         $cenv{'pch.roles.denied'}='st';
                   13815:     }
                   13816:     if ($args->{'disablechat'}) {
                   13817:         $cenv{'plc.roles.denied'}='st';
                   13818:     }
                   13819: 
                   13820:     # Record we've not yet viewed the Course Initialization Helper for this 
                   13821:     # course
                   13822:     $cenv{'course.helper.not.run'} = 1;
                   13823:     #
                   13824:     # Use new Randomseed
                   13825:     #
                   13826:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
                   13827:     $cenv{'receiptalg'}=&Apache::lonnet::latest_receipt_algorithm_id();;
                   13828:     #
                   13829:     # The encryption code and receipt prefix for this course
                   13830:     #
                   13831:     $cenv{'internal.encseed'}=$Apache::lonnet::perlvar{'lonReceipt'}.$$.time.int(rand(9999));
                   13832:     $cenv{'internal.encpref'}=100+int(9*rand(99));
                   13833:     #
                   13834:     # By default, use standard grading
                   13835:     if (!defined($cenv{'grading'})) { $cenv{'grading'} = 'standard'; }
                   13836: 
1.541     raeburn  13837:     $outcome .= $linefeed.&mt('Setting environment').': '.                 
                   13838:           &Apache::lonnet::put('environment',\%cenv,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 13839: #
                   13840: # Open all assignments
                   13841: #
                   13842:     if ($args->{'openall'}) {
                   13843:        my $storeunder=$$crsudom.'_'.$$crsunum.'.0.opendate';
                   13844:        my %storecontent = ($storeunder         => time,
                   13845:                            $storeunder.'.type' => 'date_start');
                   13846:        
                   13847:        $outcome .= &mt('Opening all assignments').': '.&Apache::lonnet::cput
1.541     raeburn  13848:                  ('resourcedata',\%storecontent,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 13849:    }
                   13850: #
                   13851: # Set first page
                   13852: #
                   13853:     unless (($args->{'nonstandard'}) || ($args->{'firstres'} eq 'blank')
                   13854: 	    || ($cloneid)) {
1.445     albertel 13855: 	use LONCAPA::map;
1.444     albertel 13856: 	$outcome .= &mt('Setting first resource').': ';
1.445     albertel 13857: 
                   13858: 	my $map = '/uploaded/'.$$crsudom.'/'.$$crsunum.'/default.sequence';
                   13859:         my ($errtext,$fatal)=&LONCAPA::map::mapread($map);
                   13860: 
1.444     albertel 13861:         $outcome .= ($fatal?$errtext:'read ok').' - ';
                   13862:         my $title; my $url;
                   13863:         if ($args->{'firstres'} eq 'syl') {
1.690     bisitz   13864: 	    $title=&mt('Syllabus');
1.444     albertel 13865:             $url='/public/'.$$crsudom.'/'.$$crsunum.'/syllabus';
                   13866:         } else {
1.963     raeburn  13867:             $title=&mt('Table of Contents');
1.444     albertel 13868:             $url='/adm/navmaps';
                   13869:         }
1.445     albertel 13870: 
                   13871:         $LONCAPA::map::resources[1]=$title.':'.$url.':false:start:res';
                   13872: 	(my $outtext,$errtext) = &LONCAPA::map::storemap($map,1);
                   13873: 
                   13874: 	if ($errtext) { $fatal=2; }
1.541     raeburn  13875:         $outcome .= ($fatal?$errtext:'write ok').$linefeed;
1.444     albertel 13876:     }
1.566     albertel 13877: 
                   13878:     return (1,$outcome);
1.444     albertel 13879: }
                   13880: 
                   13881: ############################################################
                   13882: ############################################################
                   13883: 
1.953     droeschl 13884: #SD
                   13885: # only Community and Course, or anything else?
1.378     raeburn  13886: sub course_type {
                   13887:     my ($cid) = @_;
                   13888:     if (!defined($cid)) {
                   13889:         $cid = $env{'request.course.id'};
                   13890:     }
1.404     albertel 13891:     if (defined($env{'course.'.$cid.'.type'})) {
                   13892:         return $env{'course.'.$cid.'.type'};
1.378     raeburn  13893:     } else {
                   13894:         return 'Course';
1.377     raeburn  13895:     }
                   13896: }
1.156     albertel 13897: 
1.406     raeburn  13898: sub group_term {
                   13899:     my $crstype = &course_type();
                   13900:     my %names = (
                   13901:                   'Course' => 'group',
1.865     raeburn  13902:                   'Community' => 'group',
1.406     raeburn  13903:                 );
                   13904:     return $names{$crstype};
                   13905: }
                   13906: 
1.902     raeburn  13907: sub course_types {
                   13908:     my @types = ('official','unofficial','community');
                   13909:     my %typename = (
                   13910:                          official   => 'Official course',
                   13911:                          unofficial => 'Unofficial course',
                   13912:                          community  => 'Community',
                   13913:                    );
                   13914:     return (\@types,\%typename);
                   13915: }
                   13916: 
1.156     albertel 13917: sub icon {
                   13918:     my ($file)=@_;
1.505     albertel 13919:     my $curfext = lc((split(/\./,$file))[-1]);
1.168     albertel 13920:     my $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/unknown.gif';
1.156     albertel 13921:     my $embstyle = &Apache::loncommon::fileembstyle($curfext);
1.168     albertel 13922:     if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) {
                   13923: 	if (-e  $Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
                   13924: 	          $Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   13925: 	            $curfext.".gif") {
                   13926: 	    $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   13927: 		$curfext.".gif";
                   13928: 	}
                   13929:     }
1.249     albertel 13930:     return &lonhttpdurl($iconname);
1.154     albertel 13931: } 
1.84      albertel 13932: 
1.575     albertel 13933: sub lonhttpdurl {
1.692     www      13934: #
                   13935: # Had been used for "small fry" static images on separate port 8080.
                   13936: # Modify here if lightweight http functionality desired again.
                   13937: # Currently eliminated due to increasing firewall issues.
                   13938: #
1.575     albertel 13939:     my ($url)=@_;
1.692     www      13940:     return $url;
1.215     albertel 13941: }
                   13942: 
1.213     albertel 13943: sub connection_aborted {
                   13944:     my ($r)=@_;
                   13945:     $r->print(" ");$r->rflush();
                   13946:     my $c = $r->connection;
                   13947:     return $c->aborted();
                   13948: }
                   13949: 
1.221     foxr     13950: #    Escapes strings that may have embedded 's that will be put into
1.222     foxr     13951: #    strings as 'strings'.
                   13952: sub escape_single {
1.221     foxr     13953:     my ($input) = @_;
1.223     albertel 13954:     $input =~ s/\\/\\\\/g;	# Escape the \'s..(must be first)>
1.221     foxr     13955:     $input =~ s/\'/\\\'/g;	# Esacpe the 's....
                   13956:     return $input;
                   13957: }
1.223     albertel 13958: 
1.222     foxr     13959: #  Same as escape_single, but escape's "'s  This 
                   13960: #  can be used for  "strings"
                   13961: sub escape_double {
                   13962:     my ($input) = @_;
                   13963:     $input =~ s/\\/\\\\/g;	# Escape the /'s..(must be first)>
                   13964:     $input =~ s/\"/\\\"/g;	# Esacpe the "s....
                   13965:     return $input;
                   13966: }
1.223     albertel 13967:  
1.222     foxr     13968: #   Escapes the last element of a full URL.
                   13969: sub escape_url {
                   13970:     my ($url)   = @_;
1.238     raeburn  13971:     my @urlslices = split(/\//, $url,-1);
1.369     www      13972:     my $lastitem = &escape(pop(@urlslices));
1.223     albertel 13973:     return join('/',@urlslices).'/'.$lastitem;
1.222     foxr     13974: }
1.462     albertel 13975: 
1.820     raeburn  13976: sub compare_arrays {
                   13977:     my ($arrayref1,$arrayref2) = @_;
                   13978:     my (@difference,%count);
                   13979:     @difference = ();
                   13980:     %count = ();
                   13981:     if ((ref($arrayref1) eq 'ARRAY') && (ref($arrayref2) eq 'ARRAY')) {
                   13982:         foreach my $element (@{$arrayref1}, @{$arrayref2}) { $count{$element}++; }
                   13983:         foreach my $element (keys(%count)) {
                   13984:             if ($count{$element} == 1) {
                   13985:                 push(@difference,$element);
                   13986:             }
                   13987:         }
                   13988:     }
                   13989:     return @difference;
                   13990: }
                   13991: 
1.817     bisitz   13992: # -------------------------------------------------------- Initialize user login
1.462     albertel 13993: sub init_user_environment {
1.463     albertel 13994:     my ($r, $username, $domain, $authhost, $form, $args) = @_;
1.462     albertel 13995:     my $lonids=$Apache::lonnet::perlvar{'lonIDsDir'};
                   13996: 
                   13997:     my $public=($username eq 'public' && $domain eq 'public');
                   13998: 
                   13999: # See if old ID present, if so, remove
                   14000: 
1.1062    raeburn  14001:     my ($filename,$cookie,$userroles,$firstaccenv,$timerintenv);
1.462     albertel 14002:     my $now=time;
                   14003: 
                   14004:     if ($public) {
                   14005: 	my $max_public=100;
                   14006: 	my $oldest;
                   14007: 	my $oldest_time=0;
                   14008: 	for(my $next=1;$next<=$max_public;$next++) {
                   14009: 	    if (-e $lonids."/publicuser_$next.id") {
                   14010: 		my $mtime=(stat($lonids."/publicuser_$next.id"))[9];
                   14011: 		if ($mtime<$oldest_time || !$oldest_time) {
                   14012: 		    $oldest_time=$mtime;
                   14013: 		    $oldest=$next;
                   14014: 		}
                   14015: 	    } else {
                   14016: 		$cookie="publicuser_$next";
                   14017: 		last;
                   14018: 	    }
                   14019: 	}
                   14020: 	if (!$cookie) { $cookie="publicuser_$oldest"; }
                   14021:     } else {
1.463     albertel 14022: 	# if this isn't a robot, kill any existing non-robot sessions
                   14023: 	if (!$args->{'robot'}) {
                   14024: 	    opendir(DIR,$lonids);
                   14025: 	    while ($filename=readdir(DIR)) {
                   14026: 		if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
                   14027: 		    unlink($lonids.'/'.$filename);
                   14028: 		}
1.462     albertel 14029: 	    }
1.463     albertel 14030: 	    closedir(DIR);
1.462     albertel 14031: 	}
                   14032: # Give them a new cookie
1.463     albertel 14033: 	my $id = ($args->{'robot'} ? 'robot'.$args->{'robot'}
1.684     www      14034: 		                   : $now.$$.int(rand(10000)));
1.463     albertel 14035: 	$cookie="$username\_$id\_$domain\_$authhost";
1.462     albertel 14036:     
                   14037: # Initialize roles
                   14038: 
1.1062    raeburn  14039: 	($userroles,$firstaccenv,$timerintenv) = 
                   14040:             &Apache::lonnet::rolesinit($domain,$username,$authhost);
1.462     albertel 14041:     }
                   14042: # ------------------------------------ Check browser type and MathML capability
                   14043: 
                   14044:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                   14045:         $clientunicode,$clientos) = &decode_user_agent($r);
                   14046: 
                   14047: # ------------------------------------------------------------- Get environment
                   14048: 
                   14049:     my %userenv = &Apache::lonnet::dump('environment',$domain,$username);
                   14050:     my ($tmp) = keys(%userenv);
                   14051:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   14052:     } else {
                   14053: 	undef(%userenv);
                   14054:     }
                   14055:     if (($userenv{'interface'}) && (!$form->{'interface'})) {
                   14056: 	$form->{'interface'}=$userenv{'interface'};
                   14057:     }
                   14058:     if ($userenv{'texengine'} eq 'ttm') { $clientmathml=1; }
                   14059: 
                   14060: # --------------- Do not trust query string to be put directly into environment
1.817     bisitz   14061:     foreach my $option ('interface','localpath','localres') {
                   14062:         $form->{$option}=~s/[\n\r\=]//gs;
1.462     albertel 14063:     }
                   14064: # --------------------------------------------------------- Write first profile
                   14065: 
                   14066:     {
                   14067: 	my %initial_env = 
                   14068: 	    ("user.name"          => $username,
                   14069: 	     "user.domain"        => $domain,
                   14070: 	     "user.home"          => $authhost,
                   14071: 	     "browser.type"       => $clientbrowser,
                   14072: 	     "browser.version"    => $clientversion,
                   14073: 	     "browser.mathml"     => $clientmathml,
                   14074: 	     "browser.unicode"    => $clientunicode,
                   14075: 	     "browser.os"         => $clientos,
                   14076: 	     "server.domain"      => $Apache::lonnet::perlvar{'lonDefDomain'},
                   14077: 	     "request.course.fn"  => '',
                   14078: 	     "request.course.uri" => '',
                   14079: 	     "request.course.sec" => '',
                   14080: 	     "request.role"       => 'cm',
                   14081: 	     "request.role.adv"   => $env{'user.adv'},
                   14082: 	     "request.host"       => $ENV{'REMOTE_ADDR'},);
                   14083: 
                   14084:         if ($form->{'localpath'}) {
                   14085: 	    $initial_env{"browser.localpath"}  = $form->{'localpath'};
                   14086: 	    $initial_env{"browser.localres"}   = $form->{'localres'};
                   14087:         }
                   14088: 	
                   14089: 	if ($form->{'interface'}) {
                   14090: 	    $form->{'interface'}=~s/\W//gs;
                   14091: 	    $initial_env{"browser.interface"} = $form->{'interface'};
                   14092: 	    $env{'browser.interface'}=$form->{'interface'};
                   14093: 	}
                   14094: 
1.981     raeburn  14095:         my %is_adv = ( is_adv => $env{'user.adv'} );
1.1016    raeburn  14096:         my %domdef;
                   14097:         unless ($domain eq 'public') {
                   14098:             %domdef = &Apache::lonnet::get_domain_defaults($domain);
                   14099:         }
1.980     raeburn  14100: 
1.1075.2.7  raeburn  14101:         foreach my $tool ('aboutme','blog','webdav','portfolio') {
1.724     raeburn  14102:             $userenv{'availabletools.'.$tool} = 
1.980     raeburn  14103:                 &Apache::lonnet::usertools_access($username,$domain,$tool,'reload',
                   14104:                                                   undef,\%userenv,\%domdef,\%is_adv);
1.724     raeburn  14105:         }
                   14106: 
1.864     raeburn  14107:         foreach my $crstype ('official','unofficial','community') {
1.765     raeburn  14108:             $userenv{'canrequest.'.$crstype} =
                   14109:                 &Apache::lonnet::usertools_access($username,$domain,$crstype,
1.980     raeburn  14110:                                                   'reload','requestcourses',
                   14111:                                                   \%userenv,\%domdef,\%is_adv);
1.765     raeburn  14112:         }
                   14113: 
1.1075.2.14  raeburn  14114:         $userenv{'canrequest.author'} =
                   14115:             &Apache::lonnet::usertools_access($username,$domain,'requestauthor',
                   14116:                                         'reload','requestauthor',
                   14117:                                         \%userenv,\%domdef,\%is_adv);
                   14118:         my %reqauthor = &Apache::lonnet::get('requestauthor',['author_status','author'],
                   14119:                                              $domain,$username);
                   14120:         my $reqstatus = $reqauthor{'author_status'};
                   14121:         if ($reqstatus eq 'approval' || $reqstatus eq 'approved') {
                   14122:             if (ref($reqauthor{'author'}) eq 'HASH') {
                   14123:                 $userenv{'requestauthorqueued'} = $reqstatus.':'.
                   14124:                                                   $reqauthor{'author'}{'timestamp'};
                   14125:             }
                   14126:         }
                   14127: 
1.462     albertel 14128: 	$env{'user.environment'} = "$lonids/$cookie.id";
1.1062    raeburn  14129: 
1.462     albertel 14130: 	if (tie(my %disk_env,'GDBM_File',"$lonids/$cookie.id",
                   14131: 		 &GDBM_WRCREAT(),0640)) {
                   14132: 	    &_add_to_env(\%disk_env,\%initial_env);
                   14133: 	    &_add_to_env(\%disk_env,\%userenv,'environment.');
                   14134: 	    &_add_to_env(\%disk_env,$userroles);
1.1062    raeburn  14135:             if (ref($firstaccenv) eq 'HASH') {
                   14136:                 &_add_to_env(\%disk_env,$firstaccenv);
                   14137:             }
                   14138:             if (ref($timerintenv) eq 'HASH') {
                   14139:                 &_add_to_env(\%disk_env,$timerintenv);
                   14140:             }
1.463     albertel 14141: 	    if (ref($args->{'extra_env'})) {
                   14142: 		&_add_to_env(\%disk_env,$args->{'extra_env'});
                   14143: 	    }
1.462     albertel 14144: 	    untie(%disk_env);
                   14145: 	} else {
1.705     tempelho 14146: 	    &Apache::lonnet::logthis("<span style=\"color:blue;\">WARNING: ".
                   14147: 			   'Could not create environment storage in lonauth: '.$!.'</span>');
1.462     albertel 14148: 	    return 'error: '.$!;
                   14149: 	}
                   14150:     }
                   14151:     $env{'request.role'}='cm';
                   14152:     $env{'request.role.adv'}=$env{'user.adv'};
                   14153:     $env{'browser.type'}=$clientbrowser;
                   14154: 
                   14155:     return $cookie;
                   14156: 
                   14157: }
                   14158: 
                   14159: sub _add_to_env {
                   14160:     my ($idf,$env_data,$prefix) = @_;
1.676     raeburn  14161:     if (ref($env_data) eq 'HASH') {
                   14162:         while (my ($key,$value) = each(%$env_data)) {
                   14163: 	    $idf->{$prefix.$key} = $value;
                   14164: 	    $env{$prefix.$key}   = $value;
                   14165:         }
1.462     albertel 14166:     }
                   14167: }
                   14168: 
1.685     tempelho 14169: # --- Get the symbolic name of a problem and the url
                   14170: sub get_symb {
                   14171:     my ($request,$silent) = @_;
1.726     raeburn  14172:     (my $url=$env{'form.url'}) =~ s-^https?\://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
1.685     tempelho 14173:     my $symb=($env{'form.symb'} ne '' ? $env{'form.symb'} : (&Apache::lonnet::symbread($url)));
                   14174:     if ($symb eq '') {
                   14175:         if (!$silent) {
1.1071    raeburn  14176:             if (ref($request)) { 
                   14177:                 $request->print("Unable to handle ambiguous references:$url:.");
                   14178:             }
1.685     tempelho 14179:             return ();
                   14180:         }
                   14181:     }
                   14182:     &Apache::lonenc::check_decrypt(\$symb);
                   14183:     return ($symb);
                   14184: }
                   14185: 
                   14186: # --------------------------------------------------------------Get annotation
                   14187: 
                   14188: sub get_annotation {
                   14189:     my ($symb,$enc) = @_;
                   14190: 
                   14191:     my $key = $symb;
                   14192:     if (!$enc) {
                   14193:         $key =
                   14194:             &Apache::lonnet::clutter((&Apache::lonnet::decode_symb($symb))[2]);
                   14195:     }
                   14196:     my %annotation=&Apache::lonnet::get('nohist_annotations',[$key]);
                   14197:     return $annotation{$key};
                   14198: }
                   14199: 
                   14200: sub clean_symb {
1.731     raeburn  14201:     my ($symb,$delete_enc) = @_;
1.685     tempelho 14202: 
                   14203:     &Apache::lonenc::check_decrypt(\$symb);
                   14204:     my $enc = $env{'request.enc'};
1.731     raeburn  14205:     if ($delete_enc) {
1.730     raeburn  14206:         delete($env{'request.enc'});
                   14207:     }
1.685     tempelho 14208: 
                   14209:     return ($symb,$enc);
                   14210: }
1.462     albertel 14211: 
1.990     raeburn  14212: sub build_release_hashes {
                   14213:     my ($checkparms,$checkresponsetypes,$checkcrstypes,$anonsurvey,$randomizetry) = @_;
                   14214:     return unless((ref($checkparms) eq 'HASH') && (ref($checkresponsetypes) eq 'HASH') &&
                   14215:                   (ref($checkcrstypes) eq 'HASH') && (ref($anonsurvey) eq 'HASH') &&
                   14216:                   (ref($randomizetry) eq 'HASH'));
                   14217:     foreach my $key (keys(%Apache::lonnet::needsrelease)) {
                   14218:         my ($item,$name,$value) = split(/:/,$key);
                   14219:         if ($item eq 'parameter') {
                   14220:             if (ref($checkparms->{$name}) eq 'ARRAY') {
                   14221:                 unless(grep(/^\Q$name\E$/,@{$checkparms->{$name}})) {
                   14222:                     push(@{$checkparms->{$name}},$value);
                   14223:                 }
                   14224:             } else {
                   14225:                 push(@{$checkparms->{$name}},$value);
                   14226:             }
                   14227:         } elsif ($item eq 'resourcetag') {
                   14228:             if ($name eq 'responsetype') {
                   14229:                 $checkresponsetypes->{$value} = $Apache::lonnet::needsrelease{$key}
                   14230:             }
                   14231:         } elsif ($item eq 'course') {
                   14232:             if ($name eq 'crstype') {
                   14233:                 $checkcrstypes->{$value} = $Apache::lonnet::needsrelease{$key};
                   14234:             }
                   14235:         }
                   14236:     }
                   14237:     ($anonsurvey->{major},$anonsurvey->{minor}) = split(/\./,$Apache::lonnet::needsrelease{'parameter:type:anonsurvey'});
                   14238:     ($randomizetry->{major},$randomizetry->{minor}) = split(/\./,$Apache::lonnet::needsrelease{'parameter:type:randomizetry'});
                   14239:     return;
                   14240: }
                   14241: 
1.1075.2.11  raeburn  14242: sub update_content_constraints {
                   14243:     my ($cdom,$cnum,$chome,$cid) = @_;
                   14244:     my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired');
                   14245:     my ($reqdmajor,$reqdminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
                   14246:     my %checkresponsetypes;
                   14247:     foreach my $key (keys(%Apache::lonnet::needsrelease)) {
                   14248:         my ($item,$name,$value) = split(/:/,$key);
                   14249:         if ($item eq 'resourcetag') {
                   14250:             if ($name eq 'responsetype') {
                   14251:                 $checkresponsetypes{$value} = $Apache::lonnet::needsrelease{$key}
                   14252:             }
                   14253:         }
                   14254:     }
                   14255:     my $navmap = Apache::lonnavmaps::navmap->new();
                   14256:     if (defined($navmap)) {
                   14257:         my %allresponses;
                   14258:         foreach my $res ($navmap->retrieveResources(undef,sub { $_[0]->is_problem() },1,0)) {
                   14259:             my %responses = $res->responseTypes();
                   14260:             foreach my $key (keys(%responses)) {
                   14261:                 next unless(exists($checkresponsetypes{$key}));
                   14262:                 $allresponses{$key} += $responses{$key};
                   14263:             }
                   14264:         }
                   14265:         foreach my $key (keys(%allresponses)) {
                   14266:             my ($major,$minor) = split(/\./,$checkresponsetypes{$key});
                   14267:             if (($major > $reqdmajor) || ($major == $reqdmajor && $minor > $reqdminor)) {
                   14268:                 ($reqdmajor,$reqdminor) = ($major,$minor);
                   14269:             }
                   14270:         }
                   14271:         undef($navmap);
                   14272:     }
                   14273:     unless (($reqdmajor eq '') && ($reqdminor eq '')) {
                   14274:         &Apache::lonnet::update_released_required($reqdmajor.'.'.$reqdminor,$cdom,$cnum,$chome,$cid);
                   14275:     }
                   14276:     return;
                   14277: }
                   14278: 
1.1075.2.27  raeburn  14279: sub allmaps_incourse {
                   14280:     my ($cdom,$cnum,$chome,$cid) = @_;
                   14281:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
                   14282:         $cid = $env{'request.course.id'};
                   14283:         $cdom = $env{'course.'.$cid.'.domain'};
                   14284:         $cnum = $env{'course.'.$cid.'.num'};
                   14285:         $chome = $env{'course.'.$cid.'.home'};
                   14286:     }
                   14287:     my %allmaps = ();
                   14288:     my $lastchange =
                   14289:         &Apache::lonnet::get_coursechange($cdom,$cnum);
                   14290:     if ($lastchange > $env{'request.course.tied'}) {
                   14291:         my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
                   14292:         unless ($ferr) {
                   14293:             &update_content_constraints($cdom,$cnum,$chome,$cid);
                   14294:         }
                   14295:     }
                   14296:     my $navmap = Apache::lonnavmaps::navmap->new();
                   14297:     if (defined($navmap)) {
                   14298:         foreach my $res ($navmap->retrieveResources(undef,sub { $_[0]->is_map() },1,0,1)) {
                   14299:             $allmaps{$res->src()} = 1;
                   14300:         }
                   14301:     }
                   14302:     return \%allmaps;
                   14303: }
                   14304: 
1.1075.2.11  raeburn  14305: sub parse_supplemental_title {
                   14306:     my ($title) = @_;
                   14307: 
                   14308:     my ($foldertitle,$renametitle);
                   14309:     if ($title =~ /&amp;&amp;&amp;/) {
                   14310:         $title = &HTML::Entites::decode($title);
                   14311:     }
                   14312:     if ($title =~ m/^(\d+)___&&&___($match_username)___&&&___($match_domain)___&&&___(.*)$/) {
                   14313:         $renametitle=$4;
                   14314:         my ($time,$uname,$udom) = ($1,$2,$3);
                   14315:         $foldertitle=&Apache::lontexconvert::msgtexconverted($4);
                   14316:         my $name =  &plainname($uname,$udom);
                   14317:         $name = &HTML::Entities::encode($name,'"<>&\'');
                   14318:         $renametitle = &HTML::Entities::encode($renametitle,'"<>&\'');
                   14319:         $title='<i>'.&Apache::lonlocal::locallocaltime($time).'</i> '.
                   14320:             $name.': <br />'.$foldertitle;
                   14321:     }
                   14322:     if (wantarray) {
                   14323:         return ($title,$foldertitle,$renametitle);
                   14324:     }
                   14325:     return $title;
                   14326: }
                   14327: 
1.1075.2.18  raeburn  14328: sub symb_to_docspath {
                   14329:     my ($symb) = @_;
                   14330:     return unless ($symb);
                   14331:     my ($mapurl,$id,$resurl) = &Apache::lonnet::decode_symb($symb);
                   14332:     if ($resurl=~/\.(sequence|page)$/) {
                   14333:         $mapurl=$resurl;
                   14334:     } elsif ($resurl eq 'adm/navmaps') {
                   14335:         $mapurl=$env{'course.'.$env{'request.course.id'}.'.url'};
                   14336:     }
                   14337:     my $mapresobj;
                   14338:     my $navmap = Apache::lonnavmaps::navmap->new();
                   14339:     if (ref($navmap)) {
                   14340:         $mapresobj = $navmap->getResourceByUrl($mapurl);
                   14341:     }
                   14342:     $mapurl=~s{^.*/([^/]+)\.(\w+)$}{$1};
                   14343:     my $type=$2;
                   14344:     my $path;
                   14345:     if (ref($mapresobj)) {
                   14346:         my $pcslist = $mapresobj->map_hierarchy();
                   14347:         if ($pcslist ne '') {
                   14348:             foreach my $pc (split(/,/,$pcslist)) {
                   14349:                 next if ($pc <= 1);
                   14350:                 my $res = $navmap->getByMapPc($pc);
                   14351:                 if (ref($res)) {
                   14352:                     my $thisurl = $res->src();
                   14353:                     $thisurl=~s{^.*/([^/]+)\.\w+$}{$1};
                   14354:                     my $thistitle = $res->title();
                   14355:                     $path .= '&'.
                   14356:                              &Apache::lonhtmlcommon::entity_encode($thisurl).'&'.
                   14357:                              &Apache::lonhtmlcommon::entity_encode($thistitle).
                   14358:                              ':'.$res->randompick().
                   14359:                              ':'.$res->randomout().
                   14360:                              ':'.$res->encrypted().
                   14361:                              ':'.$res->randomorder().
                   14362:                              ':'.$res->is_page();
                   14363:                 }
                   14364:             }
                   14365:         }
                   14366:         $path =~ s/^\&//;
                   14367:         my $maptitle = $mapresobj->title();
                   14368:         if ($mapurl eq 'default') {
                   14369:             $maptitle = 'Main Course Documents';
                   14370:         }
                   14371:         $path .= (($path ne '')? '&' : '').
                   14372:                  &Apache::lonhtmlcommon::entity_encode($mapurl).'&'.
                   14373:                  &Apache::lonhtmlcommon::entity_encode($maptitle).
                   14374:                  ':'.$mapresobj->randompick().
                   14375:                  ':'.$mapresobj->randomout().
                   14376:                  ':'.$mapresobj->encrypted().
                   14377:                  ':'.$mapresobj->randomorder().
                   14378:                  ':'.$mapresobj->is_page();
                   14379:     } else {
                   14380:         my $maptitle = &Apache::lonnet::gettitle($mapurl);
                   14381:         my $ispage = (($type eq 'page')? 1 : '');
                   14382:         if ($mapurl eq 'default') {
                   14383:             $maptitle = 'Main Course Documents';
                   14384:         }
                   14385:         $path = &Apache::lonhtmlcommon::entity_encode($mapurl).'&'.
                   14386:                 &Apache::lonhtmlcommon::entity_encode($maptitle).':::::'.$ispage;
                   14387:     }
                   14388:     unless ($mapurl eq 'default') {
                   14389:         $path = 'default&'.
                   14390:                 &Apache::lonhtmlcommon::entity_encode('Main Course Documents').
                   14391:                 ':::::&'.$path;
                   14392:     }
                   14393:     return $path;
                   14394: }
                   14395: 
1.1075.2.14  raeburn  14396: sub captcha_display {
                   14397:     my ($context,$lonhost) = @_;
                   14398:     my ($output,$error);
                   14399:     my ($captcha,$pubkey,$privkey) = &get_captcha_config($context,$lonhost);
                   14400:     if ($captcha eq 'original') {
                   14401:         $output = &create_captcha();
                   14402:         unless ($output) {
                   14403:             $error = 'captcha';
                   14404:         }
                   14405:     } elsif ($captcha eq 'recaptcha') {
                   14406:         $output = &create_recaptcha($pubkey);
                   14407:         unless ($output) {
                   14408:             $error = 'recaptcha';
                   14409:         }
                   14410:     }
                   14411:     return ($output,$error);
                   14412: }
                   14413: 
                   14414: sub captcha_response {
                   14415:     my ($context,$lonhost) = @_;
                   14416:     my ($captcha_chk,$captcha_error);
                   14417:     my ($captcha,$pubkey,$privkey) = &get_captcha_config($context,$lonhost);
                   14418:     if ($captcha eq 'original') {
                   14419:         ($captcha_chk,$captcha_error) = &check_captcha();
                   14420:     } elsif ($captcha eq 'recaptcha') {
                   14421:         $captcha_chk = &check_recaptcha($privkey);
                   14422:     } else {
                   14423:         $captcha_chk = 1;
                   14424:     }
                   14425:     return ($captcha_chk,$captcha_error);
                   14426: }
                   14427: 
                   14428: sub get_captcha_config {
                   14429:     my ($context,$lonhost) = @_;
                   14430:     my ($captcha,$pubkey,$privkey,$hashtocheck);
                   14431:     my $hostname = &Apache::lonnet::hostname($lonhost);
                   14432:     my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
                   14433:     my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
                   14434:     if ($context eq 'usercreation') {
                   14435:         my %domconfig = &Apache::lonnet::get_dom('configuration',[$context],$serverhomedom);
                   14436:         if (ref($domconfig{$context}) eq 'HASH') {
                   14437:             $hashtocheck = $domconfig{$context}{'cancreate'};
                   14438:             if (ref($hashtocheck) eq 'HASH') {
                   14439:                 if ($hashtocheck->{'captcha'} eq 'recaptcha') {
                   14440:                     if (ref($hashtocheck->{'recaptchakeys'}) eq 'HASH') {
                   14441:                         $pubkey = $hashtocheck->{'recaptchakeys'}{'public'};
                   14442:                         $privkey = $hashtocheck->{'recaptchakeys'}{'private'};
                   14443:                     }
                   14444:                     if ($privkey && $pubkey) {
                   14445:                         $captcha = 'recaptcha';
                   14446:                     } else {
                   14447:                         $captcha = 'original';
                   14448:                     }
                   14449:                 } elsif ($hashtocheck->{'captcha'} ne 'notused') {
                   14450:                     $captcha = 'original';
                   14451:                 }
                   14452:             }
                   14453:         } else {
                   14454:             $captcha = 'captcha';
                   14455:         }
                   14456:     } elsif ($context eq 'login') {
                   14457:         my %domconfhash = &Apache::loncommon::get_domainconf($serverhomedom);
                   14458:         if ($domconfhash{$serverhomedom.'.login.captcha'} eq 'recaptcha') {
                   14459:             $pubkey = $domconfhash{$serverhomedom.'.login.recaptchakeys_public'};
                   14460:             $privkey = $domconfhash{$serverhomedom.'.login.recaptchakeys_private'};
                   14461:             if ($privkey && $pubkey) {
                   14462:                 $captcha = 'recaptcha';
                   14463:             } else {
                   14464:                 $captcha = 'original';
                   14465:             }
                   14466:         } elsif ($domconfhash{$serverhomedom.'.login.captcha'} eq 'original') {
                   14467:             $captcha = 'original';
                   14468:         }
                   14469:     }
                   14470:     return ($captcha,$pubkey,$privkey);
                   14471: }
                   14472: 
                   14473: sub create_captcha {
                   14474:     my %captcha_params = &captcha_settings();
                   14475:     my ($output,$maxtries,$tries) = ('',10,0);
                   14476:     while ($tries < $maxtries) {
                   14477:         $tries ++;
                   14478:         my $captcha = Authen::Captcha->new (
                   14479:                                            output_folder => $captcha_params{'output_dir'},
                   14480:                                            data_folder   => $captcha_params{'db_dir'},
                   14481:                                           );
                   14482:         my $md5sum = $captcha->generate_code($captcha_params{'numchars'});
                   14483: 
                   14484:         if (-e $Apache::lonnet::perlvar{'lonCaptchaDir'}.'/'.$md5sum.'.png') {
                   14485:             $output = '<input type="hidden" name="crypt" value="'.$md5sum.'" />'."\n".
                   14486:                       &mt('Type in the letters/numbers shown below').'&nbsp;'.
                   14487:                      '<input type="text" size="5" name="code" value="" /><br />'.
                   14488:                      '<img src="'.$captcha_params{'www_output_dir'}.'/'.$md5sum.'.png" />';
                   14489:             last;
                   14490:         }
                   14491:     }
                   14492:     return $output;
                   14493: }
                   14494: 
                   14495: sub captcha_settings {
                   14496:     my %captcha_params = (
                   14497:                            output_dir     => $Apache::lonnet::perlvar{'lonCaptchaDir'},
                   14498:                            www_output_dir => "/captchaspool",
                   14499:                            db_dir         => $Apache::lonnet::perlvar{'lonCaptchaDb'},
                   14500:                            numchars       => '5',
                   14501:                          );
                   14502:     return %captcha_params;
                   14503: }
                   14504: 
                   14505: sub check_captcha {
                   14506:     my ($captcha_chk,$captcha_error);
                   14507:     my $code = $env{'form.code'};
                   14508:     my $md5sum = $env{'form.crypt'};
                   14509:     my %captcha_params = &captcha_settings();
                   14510:     my $captcha = Authen::Captcha->new(
                   14511:                       output_folder => $captcha_params{'output_dir'},
                   14512:                       data_folder   => $captcha_params{'db_dir'},
                   14513:                   );
1.1075.2.26  raeburn  14514:     $captcha_chk = $captcha->check_code($code,$md5sum);
1.1075.2.14  raeburn  14515:     my %captcha_hash = (
                   14516:                         0       => 'Code not checked (file error)',
                   14517:                        -1      => 'Failed: code expired',
                   14518:                        -2      => 'Failed: invalid code (not in database)',
                   14519:                        -3      => 'Failed: invalid code (code does not match crypt)',
                   14520:     );
                   14521:     if ($captcha_chk != 1) {
                   14522:         $captcha_error = $captcha_hash{$captcha_chk}
                   14523:     }
                   14524:     return ($captcha_chk,$captcha_error);
                   14525: }
                   14526: 
                   14527: sub create_recaptcha {
                   14528:     my ($pubkey) = @_;
                   14529:     my $captcha = Captcha::reCAPTCHA->new;
                   14530:     return $captcha->get_options_setter({theme => 'white'})."\n".
                   14531:            $captcha->get_html($pubkey).
                   14532:            &mt('If either word is hard to read, [_1] will replace them.',
                   14533:                '<image src="/res/adm/pages/refresh.gif" alt="reCAPTCHA refresh" />').
                   14534:            '<br /><br />';
                   14535: }
                   14536: 
                   14537: sub check_recaptcha {
                   14538:     my ($privkey) = @_;
                   14539:     my $captcha_chk;
                   14540:     my $captcha = Captcha::reCAPTCHA->new;
                   14541:     my $captcha_result =
                   14542:         $captcha->check_answer(
                   14543:                                 $privkey,
                   14544:                                 $ENV{'REMOTE_ADDR'},
                   14545:                                 $env{'form.recaptcha_challenge_field'},
                   14546:                                 $env{'form.recaptcha_response_field'},
                   14547:                               );
                   14548:     if ($captcha_result->{is_valid}) {
                   14549:         $captcha_chk = 1;
                   14550:     }
                   14551:     return $captcha_chk;
                   14552: }
                   14553: 
1.41      ng       14554: =pod
                   14555: 
                   14556: =back
                   14557: 
1.112     bowersj2 14558: =cut
1.41      ng       14559: 
1.112     bowersj2 14560: 1;
                   14561: __END__;
1.41      ng       14562: 

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