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

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.20! raeburn     4: # $Id: loncommon.pm,v 1.1075.2.19 2012/12/14 13:38:50 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.479     albertel   70: use LONCAPA qw(:DEFAULT :match);
1.657     raeburn    71: use DateTime::TimeZone;
1.687     raeburn    72: use DateTime::Locale::Catalog;
1.1075.2.14  raeburn    73: use Authen::Captcha;
                     74: use Captcha::reCAPTCHA;
1.117     www        75: 
1.517     raeburn    76: # ---------------------------------------------- Designs
                     77: use vars qw(%defaultdesign);
                     78: 
1.22      www        79: my $readit;
                     80: 
1.517     raeburn    81: 
1.157     matthew    82: ##
                     83: ## Global Variables
                     84: ##
1.46      matthew    85: 
1.643     foxr       86: 
                     87: # ----------------------------------------------- SSI with retries:
                     88: #
                     89: 
                     90: =pod
                     91: 
1.648     raeburn    92: =head1 Server Side include with retries:
1.643     foxr       93: 
                     94: =over 4
                     95: 
1.648     raeburn    96: =item * &ssi_with_retries(resource,retries form)
1.643     foxr       97: 
                     98: Performs an ssi with some number of retries.  Retries continue either
                     99: until the result is ok or until the retry count supplied by the
                    100: caller is exhausted.  
                    101: 
                    102: Inputs:
1.648     raeburn   103: 
                    104: =over 4
                    105: 
1.643     foxr      106: resource   - Identifies the resource to insert.
1.648     raeburn   107: 
1.643     foxr      108: retries    - Count of the number of retries allowed.
1.648     raeburn   109: 
1.643     foxr      110: form       - Hash that identifies the rendering options.
                    111: 
1.648     raeburn   112: =back
                    113: 
                    114: Returns:
                    115: 
                    116: =over 4
                    117: 
1.643     foxr      118: content    - The content of the response.  If retries were exhausted this is empty.
1.648     raeburn   119: 
1.643     foxr      120: response   - The response from the last attempt (which may or may not have been successful.
                    121: 
1.648     raeburn   122: =back
                    123: 
                    124: =back
                    125: 
1.643     foxr      126: =cut
                    127: 
                    128: sub ssi_with_retries {
                    129:     my ($resource, $retries, %form) = @_;
                    130: 
                    131: 
                    132:     my $ok = 0;			# True if we got a good response.
                    133:     my $content;
                    134:     my $response;
                    135: 
                    136:     # Try to get the ssi done. within the retries count:
                    137: 
                    138:     do {
                    139: 	($content, $response) = &Apache::lonnet::ssi($resource, %form);
                    140: 	$ok      = $response->is_success;
1.650     www       141:         if (!$ok) {
                    142:             &Apache::lonnet::logthis("Failed ssi_with_retries on $resource: ".$response->is_success.', '.$response->code.', '.$response->message);
                    143:         }
1.643     foxr      144: 	$retries--;
                    145:     } while (!$ok && ($retries > 0));
                    146: 
                    147:     if (!$ok) {
                    148: 	$content = '';		# On error return an empty content.
                    149:     }
                    150:     return ($content, $response);
                    151: 
                    152: }
                    153: 
                    154: 
                    155: 
1.20      www       156: # ----------------------------------------------- Filetypes/Languages/Copyright
1.12      harris41  157: my %language;
1.124     www       158: my %supported_language;
1.1048    foxr      159: my %latex_language;		# For choosing hyphenation in <transl..>
                    160: my %latex_language_bykey;	# for choosing hyphenation from metadata
1.12      harris41  161: my %cprtag;
1.192     taceyjo1  162: my %scprtag;
1.351     www       163: my %fe; my %fd; my %fm;
1.41      ng        164: my %category_extensions;
1.12      harris41  165: 
1.46      matthew   166: # ---------------------------------------------- Thesaurus variables
1.144     matthew   167: #
                    168: # %Keywords:
                    169: #      A hash used by &keyword to determine if a word is considered a keyword.
                    170: # $thesaurus_db_file 
                    171: #      Scalar containing the full path to the thesaurus database.
1.46      matthew   172: 
                    173: my %Keywords;
                    174: my $thesaurus_db_file;
                    175: 
1.144     matthew   176: #
                    177: # Initialize values from language.tab, copyright.tab, filetypes.tab,
                    178: # thesaurus.tab, and filecategories.tab.
                    179: #
1.18      www       180: BEGIN {
1.46      matthew   181:     # Variable initialization
                    182:     $thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
                    183:     #
1.22      www       184:     unless ($readit) {
1.12      harris41  185: # ------------------------------------------------------------------- languages
                    186:     {
1.158     raeburn   187:         my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    188:                                    '/language.tab';
                    189:         if ( open(my $fh,"<$langtabfile") ) {
1.356     albertel  190:             while (my $line = <$fh>) {
                    191:                 next if ($line=~/^\#/);
                    192:                 chomp($line);
1.1048    foxr      193:                 my ($key,$two,$country,$three,$enc,$val,$sup,$latex)=(split(/\t/,$line));
1.158     raeburn   194:                 $language{$key}=$val.' - '.$enc;
                    195:                 if ($sup) {
                    196:                     $supported_language{$key}=$sup;
                    197:                 }
1.1048    foxr      198: 		if ($latex) {
                    199: 		    $latex_language_bykey{$key} = $latex;
                    200: 		    $latex_language{$two} = $latex;
                    201: 		}
1.158     raeburn   202:             }
                    203:             close($fh);
                    204:         }
1.12      harris41  205:     }
                    206: # ------------------------------------------------------------------ copyrights
                    207:     {
1.158     raeburn   208:         my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    209:                                   '/copyright.tab';
                    210:         if ( open (my $fh,"<$copyrightfile") ) {
1.356     albertel  211:             while (my $line = <$fh>) {
                    212:                 next if ($line=~/^\#/);
                    213:                 chomp($line);
                    214:                 my ($key,$val)=(split(/\s+/,$line,2));
1.158     raeburn   215:                 $cprtag{$key}=$val;
                    216:             }
                    217:             close($fh);
                    218:         }
1.12      harris41  219:     }
1.351     www       220: # ----------------------------------------------------------- source copyrights
1.192     taceyjo1  221:     {
                    222:         my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    223:                                   '/source_copyright.tab';
                    224:         if ( open (my $fh,"<$sourcecopyrightfile") ) {
1.356     albertel  225:             while (my $line = <$fh>) {
                    226:                 next if ($line =~ /^\#/);
                    227:                 chomp($line);
                    228:                 my ($key,$val)=(split(/\s+/,$line,2));
1.192     taceyjo1  229:                 $scprtag{$key}=$val;
                    230:             }
                    231:             close($fh);
                    232:         }
                    233:     }
1.63      www       234: 
1.517     raeburn   235: # -------------------------------------------------------------- default domain designs
1.63      www       236:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
1.517     raeburn   237:     my $designfile = $designdir.'/default.tab';
                    238:     if ( open (my $fh,"<$designfile") ) {
                    239:         while (my $line = <$fh>) {
                    240:             next if ($line =~ /^\#/);
                    241:             chomp($line);
                    242:             my ($key,$val)=(split(/\=/,$line));
                    243:             if ($val) { $defaultdesign{$key}=$val; }
                    244:         }
                    245:         close($fh);
1.63      www       246:     }
                    247: 
1.15      harris41  248: # ------------------------------------------------------------- file categories
                    249:     {
1.158     raeburn   250:         my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    251:                                   '/filecategories.tab';
                    252:         if ( open (my $fh,"<$categoryfile") ) {
1.356     albertel  253: 	    while (my $line = <$fh>) {
                    254: 		next if ($line =~ /^\#/);
                    255: 		chomp($line);
                    256:                 my ($extension,$category)=(split(/\s+/,$line,2));
1.158     raeburn   257:                 push @{$category_extensions{lc($category)}},$extension;
                    258:             }
                    259:             close($fh);
                    260:         }
                    261: 
1.15      harris41  262:     }
1.12      harris41  263: # ------------------------------------------------------------------ file types
                    264:     {
1.158     raeburn   265:         my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    266:                '/filetypes.tab';
                    267:         if ( open (my $fh,"<$typesfile") ) {
1.356     albertel  268:             while (my $line = <$fh>) {
                    269: 		next if ($line =~ /^\#/);
                    270: 		chomp($line);
                    271:                 my ($ending,$emb,$mime,$descr)=split(/\s+/,$line,4);
1.158     raeburn   272:                 if ($descr ne '') {
                    273:                     $fe{$ending}=lc($emb);
                    274:                     $fd{$ending}=$descr;
1.351     www       275:                     if ($mime ne 'unk') { $fm{$ending}=$mime; }
1.158     raeburn   276:                 }
                    277:             }
                    278:             close($fh);
                    279:         }
1.12      harris41  280:     }
1.22      www       281:     &Apache::lonnet::logthis(
1.705     tempelho  282:              "<span style='color:yellow;'>INFO: Read file types</span>");
1.22      www       283:     $readit=1;
1.46      matthew   284:     }  # end of unless($readit) 
1.32      matthew   285:     
                    286: }
1.112     bowersj2  287: 
1.42      matthew   288: ###############################################################
                    289: ##           HTML and Javascript Helper Functions            ##
                    290: ###############################################################
                    291: 
                    292: =pod 
                    293: 
1.112     bowersj2  294: =head1 HTML and Javascript Functions
1.42      matthew   295: 
1.112     bowersj2  296: =over 4
                    297: 
1.648     raeburn   298: =item * &browser_and_searcher_javascript()
1.112     bowersj2  299: 
                    300: X<browsing, javascript>X<searching, javascript>Returns a string
                    301: containing javascript with two functions, C<openbrowser> and
                    302: C<opensearcher>. Returned string does not contain E<lt>scriptE<gt>
                    303: tags.
1.42      matthew   304: 
1.648     raeburn   305: =item * &openbrowser(formname,elementname,only,omit) [javascript]
1.42      matthew   306: 
                    307: inputs: formname, elementname, only, omit
                    308: 
                    309: formname and elementname indicate the name of the html form and name of
                    310: the element that the results of the browsing selection are to be placed in. 
                    311: 
                    312: Specifying 'only' will restrict the browser to displaying only files
1.185     www       313: with the given extension.  Can be a comma separated list.
1.42      matthew   314: 
                    315: Specifying 'omit' will restrict the browser to NOT displaying files
1.185     www       316: with the given extension.  Can be a comma separated list.
1.42      matthew   317: 
1.648     raeburn   318: =item * &opensearcher(formname,elementname) [javascript]
1.42      matthew   319: 
                    320: Inputs: formname, elementname
                    321: 
                    322: formname and elementname specify the name of the html form and the name
                    323: of the element the selection from the search results will be placed in.
1.542     raeburn   324: 
1.42      matthew   325: =cut
                    326: 
                    327: sub browser_and_searcher_javascript {
1.199     albertel  328:     my ($mode)=@_;
                    329:     if (!defined($mode)) { $mode='edit'; }
1.453     albertel  330:     my $resurl=&escape_single(&lastresurl());
1.42      matthew   331:     return <<END;
1.219     albertel  332: // <!-- BEGIN LON-CAPA Internal
1.50      matthew   333:     var editbrowser = null;
1.135     albertel  334:     function openbrowser(formname,elementname,only,omit,titleelement) {
1.170     www       335:         var url = '$resurl/?';
1.42      matthew   336:         if (editbrowser == null) {
                    337:             url += 'launch=1&';
                    338:         }
                    339:         url += 'catalogmode=interactive&';
1.199     albertel  340:         url += 'mode=$mode&';
1.611     albertel  341:         url += 'inhibitmenu=yes&';
1.42      matthew   342:         url += 'form=' + formname + '&';
                    343:         if (only != null) {
                    344:             url += 'only=' + only + '&';
1.217     albertel  345:         } else {
                    346:             url += 'only=&';
                    347: 	}
1.42      matthew   348:         if (omit != null) {
                    349:             url += 'omit=' + omit + '&';
1.217     albertel  350:         } else {
                    351:             url += 'omit=&';
                    352: 	}
1.135     albertel  353:         if (titleelement != null) {
                    354:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  355:         } else {
                    356: 	    url += 'titleelement=&';
                    357: 	}
1.42      matthew   358:         url += 'element=' + elementname + '';
                    359:         var title = 'Browser';
1.435     albertel  360:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
1.42      matthew   361:         options += ',width=700,height=600';
                    362:         editbrowser = open(url,title,options,'1');
                    363:         editbrowser.focus();
                    364:     }
                    365:     var editsearcher;
1.135     albertel  366:     function opensearcher(formname,elementname,titleelement) {
1.42      matthew   367:         var url = '/adm/searchcat?';
                    368:         if (editsearcher == null) {
                    369:             url += 'launch=1&';
                    370:         }
                    371:         url += 'catalogmode=interactive&';
1.199     albertel  372:         url += 'mode=$mode&';
1.42      matthew   373:         url += 'form=' + formname + '&';
1.135     albertel  374:         if (titleelement != null) {
                    375:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  376:         } else {
                    377: 	    url += 'titleelement=&';
                    378: 	}
1.42      matthew   379:         url += 'element=' + elementname + '';
                    380:         var title = 'Search';
1.435     albertel  381:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
1.42      matthew   382:         options += ',width=700,height=600';
                    383:         editsearcher = open(url,title,options,'1');
                    384:         editsearcher.focus();
                    385:     }
1.219     albertel  386: // END LON-CAPA Internal -->
1.42      matthew   387: END
1.170     www       388: }
                    389: 
                    390: sub lastresurl {
1.258     albertel  391:     if ($env{'environment.lastresurl'}) {
                    392: 	return $env{'environment.lastresurl'}
1.170     www       393:     } else {
                    394: 	return '/res';
                    395:     }
                    396: }
                    397: 
                    398: sub storeresurl {
                    399:     my $resurl=&Apache::lonnet::clutter(shift);
                    400:     unless ($resurl=~/^\/res/) { return 0; }
                    401:     $resurl=~s/\/$//;
                    402:     &Apache::lonnet::put('environment',{'lastresurl' => $resurl});
1.646     raeburn   403:     &Apache::lonnet::appenv({'environment.lastresurl' => $resurl});
1.170     www       404:     return 1;
1.42      matthew   405: }
                    406: 
1.74      www       407: sub studentbrowser_javascript {
1.111     www       408:    unless (
1.258     albertel  409:             (($env{'request.course.id'}) && 
1.302     albertel  410:              (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    411: 	      || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    412: 					  '/'.$env{'request.course.sec'})
                    413: 	      ))
1.258     albertel  414:          || ($env{'request.role'}=~/^(au|dc|su)/)
1.111     www       415:           ) { return ''; }  
1.74      www       416:    return (<<'ENDSTDBRW');
1.776     bisitz    417: <script type="text/javascript" language="Javascript">
1.824     bisitz    418: // <![CDATA[
1.74      www       419:     var stdeditbrowser;
1.999     www       420:     function openstdbrowser(formname,uname,udom,clicker,roleflag,ignorefilter,courseadvonly) {
1.74      www       421:         var url = '/adm/pickstudent?';
                    422:         var filter;
1.558     albertel  423: 	if (!ignorefilter) {
                    424: 	    eval('filter=document.'+formname+'.'+uname+'.value;');
                    425: 	}
1.74      www       426:         if (filter != null) {
                    427:            if (filter != '') {
                    428:                url += 'filter='+filter+'&';
                    429: 	   }
                    430:         }
                    431:         url += 'form=' + formname + '&unameelement='+uname+
1.999     www       432:                                     '&udomelement='+udom+
                    433:                                     '&clicker='+clicker;
1.111     www       434: 	if (roleflag) { url+="&roles=1"; }
1.793     raeburn   435:         if (courseadvonly) { url+="&courseadvonly=1"; }
1.102     www       436:         var title = 'Student_Browser';
1.74      www       437:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    438:         options += ',width=700,height=600';
                    439:         stdeditbrowser = open(url,title,options,'1');
                    440:         stdeditbrowser.focus();
                    441:     }
1.824     bisitz    442: // ]]>
1.74      www       443: </script>
                    444: ENDSTDBRW
                    445: }
1.42      matthew   446: 
1.1003    www       447: sub resourcebrowser_javascript {
                    448:    unless ($env{'request.course.id'}) { return ''; }
1.1004    www       449:    return (<<'ENDRESBRW');
1.1003    www       450: <script type="text/javascript" language="Javascript">
                    451: // <![CDATA[
                    452:     var reseditbrowser;
1.1004    www       453:     function openresbrowser(formname,reslink) {
1.1005    www       454:         var url = '/adm/pickresource?form='+formname+'&reslink='+reslink;
1.1003    www       455:         var title = 'Resource_Browser';
                    456:         var options = 'scrollbars=1,resizable=1,menubar=0';
1.1005    www       457:         options += ',width=700,height=500';
1.1004    www       458:         reseditbrowser = open(url,title,options,'1');
                    459:         reseditbrowser.focus();
1.1003    www       460:     }
                    461: // ]]>
                    462: </script>
1.1004    www       463: ENDRESBRW
1.1003    www       464: }
                    465: 
1.74      www       466: sub selectstudent_link {
1.999     www       467:    my ($form,$unameele,$udomele,$courseadvonly,$clickerid)=@_;
                    468:    my $callargs = "'".&Apache::lonhtmlcommon::entity_encode($form)."','".
                    469:                       &Apache::lonhtmlcommon::entity_encode($unameele)."','".
                    470:                       &Apache::lonhtmlcommon::entity_encode($udomele)."'";
1.258     albertel  471:    if ($env{'request.course.id'}) {  
1.302     albertel  472:        if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    473: 	   && !&Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    474: 					'/'.$env{'request.course.sec'})) {
1.111     www       475: 	   return '';
                    476:        }
1.999     www       477:        $callargs.=",'".&Apache::lonhtmlcommon::entity_encode($clickerid)."'";
1.793     raeburn   478:        if ($courseadvonly)  {
                    479:            $callargs .= ",'',1,1";
                    480:        }
                    481:        return '<span class="LC_nobreak">'.
                    482:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
                    483:               &mt('Select User').'</a></span>';
1.74      www       484:    }
1.258     albertel  485:    if ($env{'request.role'}=~/^(au|dc|su)/) {
1.1012    www       486:        $callargs .= ",'',1"; 
1.793     raeburn   487:        return '<span class="LC_nobreak">'.
                    488:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
                    489:               &mt('Select User').'</a></span>';
1.111     www       490:    }
                    491:    return '';
1.91      www       492: }
                    493: 
1.1004    www       494: sub selectresource_link {
                    495:    my ($form,$reslink,$arg)=@_;
                    496:    
                    497:    my $callargs = "'".&Apache::lonhtmlcommon::entity_encode($form)."','".
                    498:                       &Apache::lonhtmlcommon::entity_encode($reslink)."'";
                    499:    unless ($env{'request.course.id'}) { return $arg; }
                    500:    return '<span class="LC_nobreak">'.
                    501:               '<a href="javascript:openresbrowser('.$callargs.');">'.
                    502:               $arg.'</a></span>';
                    503: }
                    504: 
                    505: 
                    506: 
1.653     raeburn   507: sub authorbrowser_javascript {
                    508:     return <<"ENDAUTHORBRW";
1.776     bisitz    509: <script type="text/javascript" language="JavaScript">
1.824     bisitz    510: // <![CDATA[
1.653     raeburn   511: var stdeditbrowser;
                    512: 
                    513: function openauthorbrowser(formname,udom) {
                    514:     var url = '/adm/pickauthor?';
                    515:     url += 'form='+formname+'&roledom='+udom;
                    516:     var title = 'Author_Browser';
                    517:     var options = 'scrollbars=1,resizable=1,menubar=0';
                    518:     options += ',width=700,height=600';
                    519:     stdeditbrowser = open(url,title,options,'1');
                    520:     stdeditbrowser.focus();
                    521: }
                    522: 
1.824     bisitz    523: // ]]>
1.653     raeburn   524: </script>
                    525: ENDAUTHORBRW
                    526: }
                    527: 
1.91      www       528: sub coursebrowser_javascript {
1.909     raeburn   529:     my ($domainfilter,$sec_element,$formname,$role_element,$crstype) = @_;
1.932     raeburn   530:     my $wintitle = 'Course_Browser';
1.931     raeburn   531:     if ($crstype eq 'Community') {
1.932     raeburn   532:         $wintitle = 'Community_Browser';
1.909     raeburn   533:     }
1.876     raeburn   534:     my $id_functions = &javascript_index_functions();
                    535:     my $output = '
1.776     bisitz    536: <script type="text/javascript" language="JavaScript">
1.824     bisitz    537: // <![CDATA[
1.468     raeburn   538:     var stdeditbrowser;'."\n";
1.876     raeburn   539: 
                    540:     $output .= <<"ENDSTDBRW";
1.909     raeburn   541:     function opencrsbrowser(formname,uname,udom,desc,extra_element,multflag,type,type_elem) {
1.91      www       542:         var url = '/adm/pickcourse?';
1.895     raeburn   543:         var formid = getFormIdByName(formname);
1.876     raeburn   544:         var domainfilter = getDomainFromSelectbox(formname,udom);
1.128     albertel  545:         if (domainfilter != null) {
                    546:            if (domainfilter != '') {
                    547:                url += 'domainfilter='+domainfilter+'&';
                    548: 	   }
                    549:         }
1.91      www       550:         url += 'form=' + formname + '&cnumelement='+uname+
1.187     albertel  551: 	                            '&cdomelement='+udom+
                    552:                                     '&cnameelement='+desc;
1.468     raeburn   553:         if (extra_element !=null && extra_element != '') {
1.594     raeburn   554:             if (formname == 'rolechoice' || formname == 'studentform') {
1.468     raeburn   555:                 url += '&roleelement='+extra_element;
                    556:                 if (domainfilter == null || domainfilter == '') {
                    557:                     url += '&domainfilter='+extra_element;
                    558:                 }
1.234     raeburn   559:             }
1.468     raeburn   560:             else {
                    561:                 if (formname == 'portform') {
                    562:                     url += '&setroles='+extra_element;
1.800     raeburn   563:                 } else {
                    564:                     if (formname == 'rules') {
                    565:                         url += '&fixeddom='+extra_element; 
                    566:                     }
1.468     raeburn   567:                 }
                    568:             }     
1.230     raeburn   569:         }
1.909     raeburn   570:         if (type != null && type != '') {
                    571:             url += '&type='+type;
                    572:         }
                    573:         if (type_elem != null && type_elem != '') {
                    574:             url += '&typeelement='+type_elem;
                    575:         }
1.872     raeburn   576:         if (formname == 'ccrs') {
                    577:             var ownername = document.forms[formid].ccuname.value;
                    578:             var ownerdom =  document.forms[formid].ccdomain.options[document.forms[formid].ccdomain.selectedIndex].value;
                    579:             url += '&cloner='+ownername+':'+ownerdom;
                    580:         }
1.293     raeburn   581:         if (multflag !=null && multflag != '') {
                    582:             url += '&multiple='+multflag;
                    583:         }
1.909     raeburn   584:         var title = '$wintitle';
1.91      www       585:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    586:         options += ',width=700,height=600';
                    587:         stdeditbrowser = open(url,title,options,'1');
                    588:         stdeditbrowser.focus();
                    589:     }
1.876     raeburn   590: $id_functions
                    591: ENDSTDBRW
1.905     raeburn   592:     if (($sec_element ne '') || ($role_element ne '')) {
                    593:         $output .= &setsec_javascript($sec_element,$formname,$role_element);
1.876     raeburn   594:     }
                    595:     $output .= '
                    596: // ]]>
                    597: </script>';
                    598:     return $output;
                    599: }
                    600: 
                    601: sub javascript_index_functions {
                    602:     return <<"ENDJS";
                    603: 
                    604: function getFormIdByName(formname) {
                    605:     for (var i=0;i<document.forms.length;i++) {
                    606:         if (document.forms[i].name == formname) {
                    607:             return i;
                    608:         }
                    609:     }
                    610:     return -1;
                    611: }
                    612: 
                    613: function getIndexByName(formid,item) {
                    614:     for (var i=0;i<document.forms[formid].elements.length;i++) {
                    615:         if (document.forms[formid].elements[i].name == item) {
                    616:             return i;
                    617:         }
                    618:     }
                    619:     return -1;
                    620: }
1.468     raeburn   621: 
1.876     raeburn   622: function getDomainFromSelectbox(formname,udom) {
                    623:     var userdom;
                    624:     var formid = getFormIdByName(formname);
                    625:     if (formid > -1) {
                    626:         var domid = getIndexByName(formid,udom);
                    627:         if (domid > -1) {
                    628:             if (document.forms[formid].elements[domid].type == 'select-one') {
                    629:                 userdom=document.forms[formid].elements[domid].options[document.forms[formid].elements[domid].selectedIndex].value;
                    630:             }
                    631:             if (document.forms[formid].elements[domid].type == 'hidden') {
                    632:                 userdom=document.forms[formid].elements[domid].value;
1.468     raeburn   633:             }
                    634:         }
                    635:     }
1.876     raeburn   636:     return userdom;
                    637: }
                    638: 
                    639: ENDJS
1.468     raeburn   640: 
1.876     raeburn   641: }
                    642: 
1.1017    raeburn   643: sub javascript_array_indexof {
1.1018    raeburn   644:     return <<ENDJS;
1.1017    raeburn   645: <script type="text/javascript" language="JavaScript">
                    646: // <![CDATA[
                    647: 
                    648: if (!Array.prototype.indexOf) {
                    649:     Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
                    650:         "use strict";
                    651:         if (this === void 0 || this === null) {
                    652:             throw new TypeError();
                    653:         }
                    654:         var t = Object(this);
                    655:         var len = t.length >>> 0;
                    656:         if (len === 0) {
                    657:             return -1;
                    658:         }
                    659:         var n = 0;
                    660:         if (arguments.length > 0) {
                    661:             n = Number(arguments[1]);
                    662:             if (n !== n) { // shortcut for verifying if it's NaN
                    663:                 n = 0;
                    664:             } else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
                    665:                 n = (n > 0 || -1) * Math.floor(Math.abs(n));
                    666:             }
                    667:         }
                    668:         if (n >= len) {
                    669:             return -1;
                    670:         }
                    671:         var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
                    672:         for (; k < len; k++) {
                    673:             if (k in t && t[k] === searchElement) {
                    674:                 return k;
                    675:             }
                    676:         }
                    677:         return -1;
                    678:     }
                    679: }
                    680: 
                    681: // ]]>
                    682: </script>
                    683: 
                    684: ENDJS
                    685: 
                    686: }
                    687: 
1.876     raeburn   688: sub userbrowser_javascript {
                    689:     my $id_functions = &javascript_index_functions();
                    690:     return <<"ENDUSERBRW";
                    691: 
1.888     raeburn   692: function openuserbrowser(formname,uname,udom,ulast,ufirst,uemail,hideudom,crsdom,caller) {
1.876     raeburn   693:     var url = '/adm/pickuser?';
                    694:     var userdom = getDomainFromSelectbox(formname,udom);
                    695:     if (userdom != null) {
                    696:        if (userdom != '') {
                    697:            url += 'srchdom='+userdom+'&';
                    698:        }
                    699:     }
                    700:     url += 'form=' + formname + '&unameelement='+uname+
                    701:                                 '&udomelement='+udom+
                    702:                                 '&ulastelement='+ulast+
                    703:                                 '&ufirstelement='+ufirst+
                    704:                                 '&uemailelement='+uemail+
1.881     raeburn   705:                                 '&hideudomelement='+hideudom+
                    706:                                 '&coursedom='+crsdom;
1.888     raeburn   707:     if ((caller != null) && (caller != undefined)) {
                    708:         url += '&caller='+caller;
                    709:     }
1.876     raeburn   710:     var title = 'User_Browser';
                    711:     var options = 'scrollbars=1,resizable=1,menubar=0';
                    712:     options += ',width=700,height=600';
                    713:     var stdeditbrowser = open(url,title,options,'1');
                    714:     stdeditbrowser.focus();
                    715: }
                    716: 
1.888     raeburn   717: function fix_domain (formname,udom,origdom,uname) {
1.876     raeburn   718:     var formid = getFormIdByName(formname);
                    719:     if (formid > -1) {
1.888     raeburn   720:         var unameid = getIndexByName(formid,uname);
1.876     raeburn   721:         var domid = getIndexByName(formid,udom);
                    722:         var hidedomid = getIndexByName(formid,origdom);
                    723:         if (hidedomid > -1) {
                    724:             var fixeddom = document.forms[formid].elements[hidedomid].value;
1.888     raeburn   725:             var unameval = document.forms[formid].elements[unameid].value;
                    726:             if ((fixeddom != '') && (fixeddom != undefined) && (fixeddom != null) && (unameval != '') && (unameval != undefined) && (unameval != null)) {
                    727:                 if (domid > -1) {
                    728:                     var slct = document.forms[formid].elements[domid];
                    729:                     if (slct.type == 'select-one') {
                    730:                         var i;
                    731:                         for (i=0;i<slct.length;i++) {
                    732:                             if (slct.options[i].value==fixeddom) { slct.selectedIndex=i; }
                    733:                         }
                    734:                     }
                    735:                     if (slct.type == 'hidden') {
                    736:                         slct.value = fixeddom;
1.876     raeburn   737:                     }
                    738:                 }
1.468     raeburn   739:             }
                    740:         }
                    741:     }
1.876     raeburn   742:     return;
                    743: }
                    744: 
                    745: $id_functions
                    746: ENDUSERBRW
1.468     raeburn   747: }
                    748: 
                    749: sub setsec_javascript {
1.905     raeburn   750:     my ($sec_element,$formname,$role_element) = @_;
                    751:     my (@courserolenames,@communityrolenames,$rolestr,$courserolestr,
                    752:         $communityrolestr);
                    753:     if ($role_element ne '') {
                    754:         my @allroles = ('st','ta','ep','in','ad');
                    755:         foreach my $crstype ('Course','Community') {
                    756:             if ($crstype eq 'Community') {
                    757:                 foreach my $role (@allroles) {
                    758:                     push(@communityrolenames,&Apache::lonnet::plaintext($role,$crstype));
                    759:                 }
                    760:                 push(@communityrolenames,&Apache::lonnet::plaintext('co'));
                    761:             } else {
                    762:                 foreach my $role (@allroles) {
                    763:                     push(@courserolenames,&Apache::lonnet::plaintext($role,$crstype));
                    764:                 }
                    765:                 push(@courserolenames,&Apache::lonnet::plaintext('cc'));
                    766:             }
                    767:         }
                    768:         $rolestr = '"'.join('","',@allroles).'"';
                    769:         $courserolestr = '"'.join('","',@courserolenames).'"';
                    770:         $communityrolestr = '"'.join('","',@communityrolenames).'"';
                    771:     }
1.468     raeburn   772:     my $setsections = qq|
                    773: function setSect(sectionlist) {
1.629     raeburn   774:     var sectionsArray = new Array();
                    775:     if ((sectionlist != '') && (typeof sectionlist != "undefined")) {
                    776:         sectionsArray = sectionlist.split(",");
                    777:     }
1.468     raeburn   778:     var numSections = sectionsArray.length;
                    779:     document.$formname.$sec_element.length = 0;
                    780:     if (numSections == 0) {
                    781:         document.$formname.$sec_element.multiple=false;
                    782:         document.$formname.$sec_element.size=1;
                    783:         document.$formname.$sec_element.options[0] = new Option('No existing sections','',false,false)
                    784:     } else {
                    785:         if (numSections == 1) {
                    786:             document.$formname.$sec_element.multiple=false;
                    787:             document.$formname.$sec_element.size=1;
                    788:             document.$formname.$sec_element.options[0] = new Option('Select','',true,true);
                    789:             document.$formname.$sec_element.options[1] = new Option('No section','',false,false)
                    790:             document.$formname.$sec_element.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
                    791:         } else {
                    792:             for (var i=0; i<numSections; i++) {
                    793:                 document.$formname.$sec_element.options[i] = new Option(sectionsArray[i],sectionsArray[i],false,false)
                    794:             }
                    795:             document.$formname.$sec_element.multiple=true
                    796:             if (numSections < 3) {
                    797:                 document.$formname.$sec_element.size=numSections;
                    798:             } else {
                    799:                 document.$formname.$sec_element.size=3;
                    800:             }
                    801:             document.$formname.$sec_element.options[0].selected = false
                    802:         }
                    803:     }
1.91      www       804: }
1.905     raeburn   805: 
                    806: function setRole(crstype) {
1.468     raeburn   807: |;
1.905     raeburn   808:     if ($role_element eq '') {
                    809:         $setsections .= '    return;
                    810: }
                    811: ';
                    812:     } else {
                    813:         $setsections .= qq|
                    814:     var elementLength = document.$formname.$role_element.length;
                    815:     var allroles = Array($rolestr);
                    816:     var courserolenames = Array($courserolestr);
                    817:     var communityrolenames = Array($communityrolestr);
                    818:     if (elementLength != undefined) {
                    819:         if (document.$formname.$role_element.options[5].value == 'cc') {
                    820:             if (crstype == 'Course') {
                    821:                 return;
                    822:             } else {
                    823:                 allroles[5] = 'co';
                    824:                 for (var i=0; i<6; i++) {
                    825:                     document.$formname.$role_element.options[i].value = allroles[i];
                    826:                     document.$formname.$role_element.options[i].text = communityrolenames[i];
                    827:                 }
                    828:             }
                    829:         } else {
                    830:             if (crstype == 'Community') {
                    831:                 return;
                    832:             } else {
                    833:                 allroles[5] = 'cc';
                    834:                 for (var i=0; i<6; i++) {
                    835:                     document.$formname.$role_element.options[i].value = allroles[i];
                    836:                     document.$formname.$role_element.options[i].text = courserolenames[i];
                    837:                 }
                    838:             }
                    839:         }
                    840:     }
                    841:     return;
                    842: }
                    843: |;
                    844:     }
1.468     raeburn   845:     return $setsections;
                    846: }
                    847: 
1.91      www       848: sub selectcourse_link {
1.909     raeburn   849:    my ($form,$unameele,$udomele,$desc,$extra_element,$multflag,$selecttype,
                    850:        $typeelement) = @_;
                    851:    my $type = $selecttype;
1.871     raeburn   852:    my $linktext = &mt('Select Course');
                    853:    if ($selecttype eq 'Community') {
1.909     raeburn   854:        $linktext = &mt('Select Community');
1.906     raeburn   855:    } elsif ($selecttype eq 'Course/Community') {
                    856:        $linktext = &mt('Select Course/Community');
1.909     raeburn   857:        $type = '';
1.1019    raeburn   858:    } elsif ($selecttype eq 'Select') {
                    859:        $linktext = &mt('Select');
                    860:        $type = '';
1.871     raeburn   861:    }
1.787     bisitz    862:    return '<span class="LC_nobreak">'
                    863:          ."<a href='"
                    864:          .'javascript:opencrsbrowser("'.$form.'","'.$unameele
                    865:          .'","'.$udomele.'","'.$desc.'","'.$extra_element
1.909     raeburn   866:          .'","'.$multflag.'","'.$type.'","'.$typeelement.'");'
1.871     raeburn   867:          ."'>".$linktext.'</a>'
1.787     bisitz    868:          .'</span>';
1.74      www       869: }
1.42      matthew   870: 
1.653     raeburn   871: sub selectauthor_link {
                    872:    my ($form,$udom)=@_;
                    873:    return '<a href="javascript:openauthorbrowser('."'$form','$udom'".');">'.
                    874:           &mt('Select Author').'</a>';
                    875: }
                    876: 
1.876     raeburn   877: sub selectuser_link {
1.881     raeburn   878:     my ($form,$unameelem,$domelem,$lastelem,$firstelem,$emailelem,$hdomelem,
1.888     raeburn   879:         $coursedom,$linktext,$caller) = @_;
1.876     raeburn   880:     return '<a href="javascript:openuserbrowser('."'$form','$unameelem','$domelem',".
1.888     raeburn   881:            "'$lastelem','$firstelem','$emailelem','$hdomelem','$coursedom','$caller'".
1.881     raeburn   882:            ');">'.$linktext.'</a>';
1.876     raeburn   883: }
                    884: 
1.273     raeburn   885: sub check_uncheck_jscript {
                    886:     my $jscript = <<"ENDSCRT";
                    887: function checkAll(field) {
                    888:     if (field.length > 0) {
                    889:         for (i = 0; i < field.length; i++) {
1.1075.2.14  raeburn   890:             if (!field[i].disabled) {
                    891:                 field[i].checked = true;
                    892:             }
1.273     raeburn   893:         }
                    894:     } else {
1.1075.2.14  raeburn   895:         if (!field.disabled) {
                    896:             field.checked = true;
                    897:         }
1.273     raeburn   898:     }
                    899: }
                    900:  
                    901: function uncheckAll(field) {
                    902:     if (field.length > 0) {
                    903:         for (i = 0; i < field.length; i++) {
                    904:             field[i].checked = false ;
1.543     albertel  905:         }
                    906:     } else {
1.273     raeburn   907:         field.checked = false ;
                    908:     }
                    909: }
                    910: ENDSCRT
                    911:     return $jscript;
                    912: }
                    913: 
1.656     www       914: sub select_timezone {
1.659     raeburn   915:    my ($name,$selected,$onchange,$includeempty)=@_;
                    916:    my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
                    917:    if ($includeempty) {
                    918:        $output .= '<option value=""';
                    919:        if (($selected eq '') || ($selected eq 'local')) {
                    920:            $output .= ' selected="selected" ';
                    921:        }
                    922:        $output .= '> </option>';
                    923:    }
1.657     raeburn   924:    my @timezones = DateTime::TimeZone->all_names;
                    925:    foreach my $tzone (@timezones) {
                    926:        $output.= '<option value="'.$tzone.'"';
                    927:        if ($tzone eq $selected) {
                    928:            $output.=' selected="selected"';
                    929:        }
                    930:        $output.=">$tzone</option>\n";
1.656     www       931:    }
                    932:    $output.="</select>";
                    933:    return $output;
                    934: }
1.273     raeburn   935: 
1.687     raeburn   936: sub select_datelocale {
                    937:     my ($name,$selected,$onchange,$includeempty)=@_;
                    938:     my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
                    939:     if ($includeempty) {
                    940:         $output .= '<option value=""';
                    941:         if ($selected eq '') {
                    942:             $output .= ' selected="selected" ';
                    943:         }
                    944:         $output .= '> </option>';
                    945:     }
                    946:     my (@possibles,%locale_names);
                    947:     my @locales = DateTime::Locale::Catalog::Locales;
                    948:     foreach my $locale (@locales) {
                    949:         if (ref($locale) eq 'HASH') {
                    950:             my $id = $locale->{'id'};
                    951:             if ($id ne '') {
                    952:                 my $en_terr = $locale->{'en_territory'};
                    953:                 my $native_terr = $locale->{'native_territory'};
1.695     raeburn   954:                 my @languages = &Apache::lonlocal::preferred_languages();
1.687     raeburn   955:                 if (grep(/^en$/,@languages) || !@languages) {
                    956:                     if ($en_terr ne '') {
                    957:                         $locale_names{$id} = '('.$en_terr.')';
                    958:                     } elsif ($native_terr ne '') {
                    959:                         $locale_names{$id} = $native_terr;
                    960:                     }
                    961:                 } else {
                    962:                     if ($native_terr ne '') {
                    963:                         $locale_names{$id} = $native_terr.' ';
                    964:                     } elsif ($en_terr ne '') {
                    965:                         $locale_names{$id} = '('.$en_terr.')';
                    966:                     }
                    967:                 }
                    968:                 push (@possibles,$id);
                    969:             }
                    970:         }
                    971:     }
                    972:     foreach my $item (sort(@possibles)) {
                    973:         $output.= '<option value="'.$item.'"';
                    974:         if ($item eq $selected) {
                    975:             $output.=' selected="selected"';
                    976:         }
                    977:         $output.=">$item";
                    978:         if ($locale_names{$item} ne '') {
                    979:             $output.="  $locale_names{$item}</option>\n";
                    980:         }
                    981:         $output.="</option>\n";
                    982:     }
                    983:     $output.="</select>";
                    984:     return $output;
                    985: }
                    986: 
1.792     raeburn   987: sub select_language {
                    988:     my ($name,$selected,$includeempty) = @_;
                    989:     my %langchoices;
                    990:     if ($includeempty) {
                    991:         %langchoices = ('' => 'No language preference');
                    992:     }
                    993:     foreach my $id (&languageids()) {
                    994:         my $code = &supportedlanguagecode($id);
                    995:         if ($code) {
                    996:             $langchoices{$code} = &plainlanguagedescription($id);
                    997:         }
                    998:     }
1.970     raeburn   999:     return &select_form($selected,$name,\%langchoices);
1.792     raeburn  1000: }
                   1001: 
1.42      matthew  1002: =pod
1.36      matthew  1003: 
1.648     raeburn  1004: =item * &linked_select_forms(...)
1.36      matthew  1005: 
                   1006: linked_select_forms returns a string containing a <script></script> block
                   1007: and html for two <select> menus.  The select menus will be linked in that
                   1008: changing the value of the first menu will result in new values being placed
                   1009: in the second menu.  The values in the select menu will appear in alphabetical
1.609     raeburn  1010: order unless a defined order is provided.
1.36      matthew  1011: 
                   1012: linked_select_forms takes the following ordered inputs:
                   1013: 
                   1014: =over 4
                   1015: 
1.112     bowersj2 1016: =item * $formname, the name of the <form> tag
1.36      matthew  1017: 
1.112     bowersj2 1018: =item * $middletext, the text which appears between the <select> tags
1.36      matthew  1019: 
1.112     bowersj2 1020: =item * $firstdefault, the default value for the first menu
1.36      matthew  1021: 
1.112     bowersj2 1022: =item * $firstselectname, the name of the first <select> tag
1.36      matthew  1023: 
1.112     bowersj2 1024: =item * $secondselectname, the name of the second <select> tag
1.36      matthew  1025: 
1.112     bowersj2 1026: =item * $hashref, a reference to a hash containing the data for the menus.
1.36      matthew  1027: 
1.609     raeburn  1028: =item * $menuorder, the order of values in the first menu
                   1029: 
1.41      ng       1030: =back 
                   1031: 
1.36      matthew  1032: Below is an example of such a hash.  Only the 'text', 'default', and 
                   1033: 'select2' keys must appear as stated.  keys(%menu) are the possible 
                   1034: values for the first select menu.  The text that coincides with the 
1.41      ng       1035: first menu value is given in $menu{$choice1}->{'text'}.  The values 
1.36      matthew  1036: and text for the second menu are given in the hash pointed to by 
                   1037: $menu{$choice1}->{'select2'}.  
                   1038: 
1.112     bowersj2 1039:  my %menu = ( A1 => { text =>"Choice A1" ,
                   1040:                        default => "B3",
                   1041:                        select2 => { 
                   1042:                            B1 => "Choice B1",
                   1043:                            B2 => "Choice B2",
                   1044:                            B3 => "Choice B3",
                   1045:                            B4 => "Choice B4"
1.609     raeburn  1046:                            },
                   1047:                        order => ['B4','B3','B1','B2'],
1.112     bowersj2 1048:                    },
                   1049:                A2 => { text =>"Choice A2" ,
                   1050:                        default => "C2",
                   1051:                        select2 => { 
                   1052:                            C1 => "Choice C1",
                   1053:                            C2 => "Choice C2",
                   1054:                            C3 => "Choice C3"
1.609     raeburn  1055:                            },
                   1056:                        order => ['C2','C1','C3'],
1.112     bowersj2 1057:                    },
                   1058:                A3 => { text =>"Choice A3" ,
                   1059:                        default => "D6",
                   1060:                        select2 => { 
                   1061:                            D1 => "Choice D1",
                   1062:                            D2 => "Choice D2",
                   1063:                            D3 => "Choice D3",
                   1064:                            D4 => "Choice D4",
                   1065:                            D5 => "Choice D5",
                   1066:                            D6 => "Choice D6",
                   1067:                            D7 => "Choice D7"
1.609     raeburn  1068:                            },
                   1069:                        order => ['D4','D3','D2','D1','D7','D6','D5'],
1.112     bowersj2 1070:                    }
                   1071:                );
1.36      matthew  1072: 
                   1073: =cut
                   1074: 
                   1075: sub linked_select_forms {
                   1076:     my ($formname,
                   1077:         $middletext,
                   1078:         $firstdefault,
                   1079:         $firstselectname,
                   1080:         $secondselectname, 
1.609     raeburn  1081:         $hashref,
                   1082:         $menuorder,
1.36      matthew  1083:         ) = @_;
                   1084:     my $second = "document.$formname.$secondselectname";
                   1085:     my $first = "document.$formname.$firstselectname";
                   1086:     # output the javascript to do the changing
                   1087:     my $result = '';
1.776     bisitz   1088:     $result.='<script type="text/javascript" language="JavaScript">'."\n";
1.824     bisitz   1089:     $result.="// <![CDATA[\n";
1.36      matthew  1090:     $result.="var select2data = new Object();\n";
                   1091:     $" = '","';
                   1092:     my $debug = '';
                   1093:     foreach my $s1 (sort(keys(%$hashref))) {
                   1094:         $result.="select2data.d_$s1 = new Object();\n";        
                   1095:         $result.="select2data.d_$s1.def = new String('".
                   1096:             $hashref->{$s1}->{'default'}."');\n";
1.609     raeburn  1097:         $result.="select2data.d_$s1.values = new Array(";
1.36      matthew  1098:         my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
1.609     raeburn  1099:         if (ref($hashref->{$s1}->{'order'}) eq 'ARRAY') {
                   1100:             @s2values = @{$hashref->{$s1}->{'order'}};
                   1101:         }
1.36      matthew  1102:         $result.="\"@s2values\");\n";
                   1103:         $result.="select2data.d_$s1.texts = new Array(";        
                   1104:         my @s2texts;
                   1105:         foreach my $value (@s2values) {
                   1106:             push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
                   1107:         }
                   1108:         $result.="\"@s2texts\");\n";
                   1109:     }
                   1110:     $"=' ';
                   1111:     $result.= <<"END";
                   1112: 
                   1113: function select1_changed() {
                   1114:     // Determine new choice
                   1115:     var newvalue = "d_" + $first.value;
                   1116:     // update select2
                   1117:     var values     = select2data[newvalue].values;
                   1118:     var texts      = select2data[newvalue].texts;
                   1119:     var select2def = select2data[newvalue].def;
                   1120:     var i;
                   1121:     // out with the old
                   1122:     for (i = 0; i < $second.options.length; i++) {
                   1123:         $second.options[i] = null;
                   1124:     }
                   1125:     // in with the nuclear
                   1126:     for (i=0;i<values.length; i++) {
                   1127:         $second.options[i] = new Option(values[i]);
1.143     matthew  1128:         $second.options[i].value = values[i];
1.36      matthew  1129:         $second.options[i].text = texts[i];
                   1130:         if (values[i] == select2def) {
                   1131:             $second.options[i].selected = true;
                   1132:         }
                   1133:     }
                   1134: }
1.824     bisitz   1135: // ]]>
1.36      matthew  1136: </script>
                   1137: END
                   1138:     # output the initial values for the selection lists
                   1139:     $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed()\">\n";
1.609     raeburn  1140:     my @order = sort(keys(%{$hashref}));
                   1141:     if (ref($menuorder) eq 'ARRAY') {
                   1142:         @order = @{$menuorder};
                   1143:     }
                   1144:     foreach my $value (@order) {
1.36      matthew  1145:         $result.="    <option value=\"$value\" ";
1.253     albertel 1146:         $result.=" selected=\"selected\" " if ($value eq $firstdefault);
1.119     www      1147:         $result.=">".&mt($hashref->{$value}->{'text'})."</option>\n";
1.36      matthew  1148:     }
                   1149:     $result .= "</select>\n";
                   1150:     my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
                   1151:     $result .= $middletext;
                   1152:     $result .= "<select size=\"1\" name=\"$secondselectname\">\n";
                   1153:     my $seconddefault = $hashref->{$firstdefault}->{'default'};
1.609     raeburn  1154:     
                   1155:     my @secondorder = sort(keys(%select2));
                   1156:     if (ref($hashref->{$firstdefault}->{'order'}) eq 'ARRAY') {
                   1157:         @secondorder = @{$hashref->{$firstdefault}->{'order'}};
                   1158:     }
                   1159:     foreach my $value (@secondorder) {
1.36      matthew  1160:         $result.="    <option value=\"$value\" ";        
1.253     albertel 1161:         $result.=" selected=\"selected\" " if ($value eq $seconddefault);
1.119     www      1162:         $result.=">".&mt($select2{$value})."</option>\n";
1.36      matthew  1163:     }
                   1164:     $result .= "</select>\n";
                   1165:     #    return $debug;
                   1166:     return $result;
                   1167: }   #  end of sub linked_select_forms {
                   1168: 
1.45      matthew  1169: =pod
1.44      bowersj2 1170: 
1.973     raeburn  1171: =item * &help_open_topic($topic,$text,$stayOnPage,$width,$height,$imgid)
1.44      bowersj2 1172: 
1.112     bowersj2 1173: Returns a string corresponding to an HTML link to the given help
                   1174: $topic, where $topic corresponds to the name of a .tex file in
                   1175: /home/httpd/html/adm/help/tex, with underscores replaced by
                   1176: spaces. 
                   1177: 
                   1178: $text will optionally be linked to the same topic, allowing you to
                   1179: link text in addition to the graphic. If you do not want to link
                   1180: text, but wish to specify one of the later parameters, pass an
                   1181: empty string. 
                   1182: 
                   1183: $stayOnPage is a value that will be interpreted as a boolean. If true,
                   1184: the link will not open a new window. If false, the link will open
                   1185: a new window using Javascript. (Default is false.) 
                   1186: 
                   1187: $width and $height are optional numerical parameters that will
                   1188: override the width and height of the popped up window, which may
1.973     raeburn  1189: be useful for certain help topics with big pictures included.
                   1190: 
                   1191: $imgid is the id of the img tag used for the help icon. This may be
                   1192: used in a javascript call to switch the image src.  See 
                   1193: lonhtmlcommon::htmlareaselectactive() for an example.
1.44      bowersj2 1194: 
                   1195: =cut
                   1196: 
                   1197: sub help_open_topic {
1.973     raeburn  1198:     my ($topic, $text, $stayOnPage, $width, $height, $imgid) = @_;
1.48      bowersj2 1199:     $text = "" if (not defined $text);
1.44      bowersj2 1200:     $stayOnPage = 0 if (not defined $stayOnPage);
1.1033    www      1201:     $width = 500 if (not defined $width);
1.44      bowersj2 1202:     $height = 400 if (not defined $height);
                   1203:     my $filename = $topic;
                   1204:     $filename =~ s/ /_/g;
                   1205: 
1.48      bowersj2 1206:     my $template = "";
                   1207:     my $link;
1.572     banghart 1208:     
1.159     www      1209:     $topic=~s/\W/\_/g;
1.44      bowersj2 1210: 
1.572     banghart 1211:     if (!$stayOnPage) {
1.1033    www      1212: 	$link = "javascript:openMyModal('/adm/help/${filename}.hlp',$width,$height,'yes');";
1.1037    www      1213:     } elsif ($stayOnPage eq 'popup') {
                   1214:         $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 1215:     } else {
1.48      bowersj2 1216: 	$link = "/adm/help/${filename}.hlp";
                   1217:     }
                   1218: 
                   1219:     # Add the text
1.755     neumanie 1220:     if ($text ne "") {	
1.763     bisitz   1221: 	$template.='<span class="LC_help_open_topic">'
                   1222:                   .'<a target="_top" href="'.$link.'">'
                   1223:                   .$text.'</a>';
1.48      bowersj2 1224:     }
                   1225: 
1.763     bisitz   1226:     # (Always) Add the graphic
1.179     matthew  1227:     my $title = &mt('Online Help');
1.667     raeburn  1228:     my $helpicon=&lonhttpdurl("/adm/help/help.png");
1.973     raeburn  1229:     if ($imgid ne '') {
                   1230:         $imgid = ' id="'.$imgid.'"';
                   1231:     }
1.763     bisitz   1232:     $template.=' <a target="_top" href="'.$link.'" title="'.$title.'">'
                   1233:               .'<img src="'.$helpicon.'" border="0"'
                   1234:               .' alt="'.&mt('Help: [_1]',$topic).'"'
1.973     raeburn  1235:               .' title="'.$title.'" style="vertical-align:middle;"'.$imgid 
1.763     bisitz   1236:               .' /></a>';
                   1237:     if ($text ne "") {	
                   1238:         $template.='</span>';
                   1239:     }
1.44      bowersj2 1240:     return $template;
                   1241: 
1.106     bowersj2 1242: }
                   1243: 
                   1244: # This is a quicky function for Latex cheatsheet editing, since it 
                   1245: # appears in at least four places
                   1246: sub helpLatexCheatsheet {
1.1037    www      1247:     my ($topic,$text,$not_author,$stayOnPage) = @_;
1.732     raeburn  1248:     my $out;
1.106     bowersj2 1249:     my $addOther = '';
1.732     raeburn  1250:     if ($topic) {
1.1037    www      1251: 	$addOther = '<span>'.&help_open_topic($topic,&mt($text),$stayOnPage, undef, 600).'</span> ';
1.763     bisitz   1252:     }
                   1253:     $out = '<span>' # Start cheatsheet
                   1254: 	  .$addOther
                   1255:           .'<span>'
1.1037    www      1256: 	  .&help_open_topic('Greek_Symbols',&mt('Greek Symbols'),$stayOnPage,undef,600)
1.763     bisitz   1257: 	  .'</span> <span>'
1.1037    www      1258: 	  .&help_open_topic('Other_Symbols',&mt('Other Symbols'),$stayOnPage,undef,600)
1.763     bisitz   1259: 	  .'</span>';
1.732     raeburn  1260:     unless ($not_author) {
1.763     bisitz   1261:         $out .= ' <span>'
1.1037    www      1262: 	       .&help_open_topic('Authoring_Output_Tags',&mt('Output Tags'),$stayOnPage,undef,600)
1.763     bisitz   1263: 	       .'</span>';
1.732     raeburn  1264:     }
1.763     bisitz   1265:     $out .= '</span>'; # End cheatsheet
1.732     raeburn  1266:     return $out;
1.172     www      1267: }
                   1268: 
1.430     albertel 1269: sub general_help {
                   1270:     my $helptopic='Student_Intro';
                   1271:     if ($env{'request.role'}=~/^(ca|au)/) {
                   1272: 	$helptopic='Authoring_Intro';
1.907     raeburn  1273:     } elsif ($env{'request.role'}=~/^(cc|co)/) {
1.430     albertel 1274: 	$helptopic='Course_Coordination_Intro';
1.672     raeburn  1275:     } elsif ($env{'request.role'}=~/^dc/) {
                   1276:         $helptopic='Domain_Coordination_Intro';
1.430     albertel 1277:     }
                   1278:     return $helptopic;
                   1279: }
                   1280: 
                   1281: sub update_help_link {
                   1282:     my ($topic,$component_help,$faq,$bug,$stayOnPage) = @_;
                   1283:     my $origurl = $ENV{'REQUEST_URI'};
                   1284:     $origurl=~s|^/~|/priv/|;
                   1285:     my $timestamp = time;
                   1286:     foreach my $datum (\$topic,\$component_help,\$faq,\$bug,\$origurl) {
                   1287:         $$datum = &escape($$datum);
                   1288:     }
                   1289: 
                   1290:     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";
                   1291:     my $output .= <<"ENDOUTPUT";
                   1292: <script type="text/javascript">
1.824     bisitz   1293: // <![CDATA[
1.430     albertel 1294: banner_link = '$banner_link';
1.824     bisitz   1295: // ]]>
1.430     albertel 1296: </script>
                   1297: ENDOUTPUT
                   1298:     return $output;
                   1299: }
                   1300: 
                   1301: # now just updates the help link and generates a blue icon
1.193     raeburn  1302: sub help_open_menu {
1.430     albertel 1303:     my ($topic,$component_help,$faq,$bug,$stayOnPage,$width,$height,$text) 
1.552     banghart 1304: 	= @_;    
1.949     droeschl 1305:     $stayOnPage = 1;
1.430     albertel 1306:     my $output;
                   1307:     if ($component_help) {
                   1308: 	if (!$text) {
                   1309: 	    $output=&help_open_topic($component_help,undef,$stayOnPage,
                   1310: 				       $width,$height);
                   1311: 	} else {
                   1312: 	    my $help_text;
                   1313: 	    $help_text=&unescape($topic);
                   1314: 	    $output='<table><tr><td>'.
                   1315: 		&help_open_topic($component_help,$help_text,$stayOnPage,
                   1316: 				 $width,$height).'</td></tr></table>';
                   1317: 	}
                   1318:     }
                   1319:     my $banner_link = &update_help_link($topic,$component_help,$faq,$bug,$stayOnPage);
                   1320:     return $output.$banner_link;
                   1321: }
                   1322: 
                   1323: sub top_nav_help {
                   1324:     my ($text) = @_;
1.436     albertel 1325:     $text = &mt($text);
1.949     droeschl 1326:     my $stay_on_page = 1;
                   1327: 
1.572     banghart 1328:     my $link = ($stay_on_page) ? "javascript:helpMenu('display')"
1.436     albertel 1329: 	                     : "javascript:helpMenu('open')";
1.572     banghart 1330:     my $banner_link = &update_help_link(undef,undef,undef,undef,$stay_on_page);
1.436     albertel 1331: 
1.201     raeburn  1332:     my $title = &mt('Get help');
1.436     albertel 1333: 
                   1334:     return <<"END";
                   1335: $banner_link
                   1336:  <a href="$link" title="$title">$text</a>
                   1337: END
                   1338: }
                   1339: 
                   1340: sub help_menu_js {
                   1341:     my ($text) = @_;
1.949     droeschl 1342:     my $stayOnPage = 1;
1.436     albertel 1343:     my $width = 620;
                   1344:     my $height = 600;
1.430     albertel 1345:     my $helptopic=&general_help();
                   1346:     my $details_link = '/adm/help/'.$helptopic.'.hlp';
1.261     albertel 1347:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.331     albertel 1348:     my $start_page =
                   1349:         &Apache::loncommon::start_page('Help Menu', undef,
                   1350: 				       {'frameset'    => 1,
                   1351: 					'js_ready'    => 1,
                   1352: 					'add_entries' => {
                   1353: 					    'border' => '0',
1.579     raeburn  1354: 					    'rows'   => "110,*",},});
1.331     albertel 1355:     my $end_page =
                   1356:         &Apache::loncommon::end_page({'frameset' => 1,
                   1357: 				      'js_ready' => 1,});
                   1358: 
1.436     albertel 1359:     my $template .= <<"ENDTEMPLATE";
                   1360: <script type="text/javascript">
1.877     bisitz   1361: // <![CDATA[
1.253     albertel 1362: // <!-- BEGIN LON-CAPA Internal
1.430     albertel 1363: var banner_link = '';
1.243     raeburn  1364: function helpMenu(target) {
                   1365:     var caller = this;
                   1366:     if (target == 'open') {
                   1367:         var newWindow = null;
                   1368:         try {
1.262     albertel 1369:             newWindow =  window.open($nothing,"helpmenu","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" )
1.243     raeburn  1370:         }
                   1371:         catch(error) {
                   1372:             writeHelp(caller);
                   1373:             return;
                   1374:         }
                   1375:         if (newWindow) {
                   1376:             caller = newWindow;
                   1377:         }
1.193     raeburn  1378:     }
1.243     raeburn  1379:     writeHelp(caller);
                   1380:     return;
                   1381: }
                   1382: function writeHelp(caller) {
1.1072    raeburn  1383:     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  1384:     caller.document.close()
                   1385:     caller.focus()
1.193     raeburn  1386: }
1.877     bisitz   1387: // END LON-CAPA Internal -->
1.253     albertel 1388: // ]]>
1.436     albertel 1389: </script>
1.193     raeburn  1390: ENDTEMPLATE
                   1391:     return $template;
                   1392: }
                   1393: 
1.172     www      1394: sub help_open_bug {
                   1395:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel 1396:     unless ($env{'user.adv'}) { return ''; }
1.172     www      1397:     unless ($Apache::lonnet::perlvar{'BugzillaHost'}) { return ''; }
                   1398:     $text = "" if (not defined $text);
                   1399: 	$stayOnPage=1;
1.184     albertel 1400:     $width = 600 if (not defined $width);
                   1401:     $height = 600 if (not defined $height);
1.172     www      1402: 
                   1403:     $topic=~s/\W+/\+/g;
                   1404:     my $link='';
                   1405:     my $template='';
1.379     albertel 1406:     my $url=$Apache::lonnet::perlvar{'BugzillaHost'}.'enter_bug.cgi?product=LON-CAPA&amp;bug_file_loc='.
                   1407: 	&escape($ENV{'REQUEST_URI'}).'&amp;component='.$topic;
1.172     www      1408:     if (!$stayOnPage)
                   1409:     {
                   1410: 	$link = "javascript:void(open('$url', 'Bugzilla', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                   1411:     }
                   1412:     else
                   1413:     {
                   1414: 	$link = $url;
                   1415:     }
                   1416:     # Add the text
                   1417:     if ($text ne "")
                   1418:     {
                   1419: 	$template .= 
                   1420:   "<table bgcolor='#AA3333' cellspacing='1' cellpadding='1' border='0'><tr>".
1.705     tempelho 1421:   "<td bgcolor='#FF5555'><a target=\"_top\" href=\"$link\"><span style=\"color:#FFFFFF;font-size:10pt;\">$text</span></a>";
1.172     www      1422:     }
                   1423: 
                   1424:     # Add the graphic
1.179     matthew  1425:     my $title = &mt('Report a Bug');
1.215     albertel 1426:     my $bugicon=&lonhttpdurl("/adm/lonMisc/smallBug.gif");
1.172     www      1427:     $template .= <<"ENDTEMPLATE";
1.436     albertel 1428:  <a target="_top" href="$link" title="$title"><img src="$bugicon" border="0" alt="(Bug: $topic)" /></a>
1.172     www      1429: ENDTEMPLATE
                   1430:     if ($text ne '') { $template.='</td></tr></table>' };
                   1431:     return $template;
                   1432: 
                   1433: }
                   1434: 
                   1435: sub help_open_faq {
                   1436:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel 1437:     unless ($env{'user.adv'}) { return ''; }
1.172     www      1438:     unless ($Apache::lonnet::perlvar{'FAQHost'}) { return ''; }
                   1439:     $text = "" if (not defined $text);
                   1440: 	$stayOnPage=1;
                   1441:     $width = 350 if (not defined $width);
                   1442:     $height = 400 if (not defined $height);
                   1443: 
                   1444:     $topic=~s/\W+/\+/g;
                   1445:     my $link='';
                   1446:     my $template='';
                   1447:     my $url=$Apache::lonnet::perlvar{'FAQHost'}.'/fom/cache/'.$topic.'.html';
                   1448:     if (!$stayOnPage)
                   1449:     {
                   1450: 	$link = "javascript:void(open('$url', 'FAQ-O-Matic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                   1451:     }
                   1452:     else
                   1453:     {
                   1454: 	$link = $url;
                   1455:     }
                   1456: 
                   1457:     # Add the text
                   1458:     if ($text ne "")
                   1459:     {
                   1460: 	$template .= 
1.173     www      1461:   "<table bgcolor='#337733' cellspacing='1' cellpadding='1' border='0'><tr>".
1.705     tempelho 1462:   "<td bgcolor='#448844'><a target=\"_top\" href=\"$link\"><span style=\"color:#FFFFFF; font-size:10pt;\">$text</span></a>";
1.172     www      1463:     }
                   1464: 
                   1465:     # Add the graphic
1.179     matthew  1466:     my $title = &mt('View the FAQ');
1.215     albertel 1467:     my $faqicon=&lonhttpdurl("/adm/lonMisc/smallFAQ.gif");
1.172     www      1468:     $template .= <<"ENDTEMPLATE";
1.436     albertel 1469:  <a target="_top" href="$link" title="$title"><img src="$faqicon" border="0" alt="(FAQ: $topic)" /></a>
1.172     www      1470: ENDTEMPLATE
                   1471:     if ($text ne '') { $template.='</td></tr></table>' };
                   1472:     return $template;
                   1473: 
1.44      bowersj2 1474: }
1.37      matthew  1475: 
1.180     matthew  1476: ###############################################################
                   1477: ###############################################################
                   1478: 
1.45      matthew  1479: =pod
                   1480: 
1.648     raeburn  1481: =item * &change_content_javascript():
1.256     matthew  1482: 
                   1483: This and the next function allow you to create small sections of an
                   1484: otherwise static HTML page that you can update on the fly with
                   1485: Javascript, even in Netscape 4.
                   1486: 
                   1487: The Javascript fragment returned by this function (no E<lt>scriptE<gt> tag)
                   1488: must be written to the HTML page once. It will prove the Javascript
                   1489: function "change(name, content)". Calling the change function with the
                   1490: name of the section 
                   1491: you want to update, matching the name passed to C<changable_area>, and
                   1492: the new content you want to put in there, will put the content into
                   1493: that area.
                   1494: 
                   1495: B<Note>: Netscape 4 only reserves enough space for the changable area
                   1496: to contain room for the original contents. You need to "make space"
                   1497: for whatever changes you wish to make, and be B<sure> to check your
                   1498: code in Netscape 4. This feature in Netscape 4 is B<not> powerful;
                   1499: it's adequate for updating a one-line status display, but little more.
                   1500: This script will set the space to 100% width, so you only need to
                   1501: worry about height in Netscape 4.
                   1502: 
                   1503: Modern browsers are much less limiting, and if you can commit to the
                   1504: user not using Netscape 4, this feature may be used freely with
                   1505: pretty much any HTML.
                   1506: 
                   1507: =cut
                   1508: 
                   1509: sub change_content_javascript {
                   1510:     # If we're on Netscape 4, we need to use Layer-based code
1.258     albertel 1511:     if ($env{'browser.type'} eq 'netscape' &&
                   1512: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew  1513: 	return (<<NETSCAPE4);
                   1514: 	function change(name, content) {
                   1515: 	    doc = document.layers[name+"___escape"].layers[0].document;
                   1516: 	    doc.open();
                   1517: 	    doc.write(content);
                   1518: 	    doc.close();
                   1519: 	}
                   1520: NETSCAPE4
                   1521:     } else {
                   1522: 	# Otherwise, we need to use semi-standards-compliant code
                   1523: 	# (technically, "innerHTML" isn't standard but the equivalent
                   1524: 	# is really scary, and every useful browser supports it
                   1525: 	return (<<DOMBASED);
                   1526: 	function change(name, content) {
                   1527: 	    element = document.getElementById(name);
                   1528: 	    element.innerHTML = content;
                   1529: 	}
                   1530: DOMBASED
                   1531:     }
                   1532: }
                   1533: 
                   1534: =pod
                   1535: 
1.648     raeburn  1536: =item * &changable_area($name,$origContent):
1.256     matthew  1537: 
                   1538: This provides a "changable area" that can be modified on the fly via
                   1539: the Javascript code provided in C<change_content_javascript>. $name is
                   1540: the name you will use to reference the area later; do not repeat the
                   1541: same name on a given HTML page more then once. $origContent is what
                   1542: the area will originally contain, which can be left blank.
                   1543: 
                   1544: =cut
                   1545: 
                   1546: sub changable_area {
                   1547:     my ($name, $origContent) = @_;
                   1548: 
1.258     albertel 1549:     if ($env{'browser.type'} eq 'netscape' &&
                   1550: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew  1551: 	# If this is netscape 4, we need to use the Layer tag
                   1552: 	return "<ilayer width='100%' id='${name}___escape' overflow='none'><layer width='100%' id='$name' overflow='none'>$origContent</layer></ilayer>";
                   1553:     } else {
                   1554: 	return "<span id='$name'>$origContent</span>";
                   1555:     }
                   1556: }
                   1557: 
                   1558: =pod
                   1559: 
1.648     raeburn  1560: =item * &viewport_geometry_js 
1.590     raeburn  1561: 
                   1562: Provides javascript object (Geometry) which can provide information about the viewport geometry for the client browser.
                   1563: 
                   1564: =cut
                   1565: 
                   1566: 
                   1567: sub viewport_geometry_js { 
                   1568:     return <<"GEOMETRY";
                   1569: var Geometry = {};
                   1570: function init_geometry() {
                   1571:     if (Geometry.init) { return };
                   1572:     Geometry.init=1;
                   1573:     if (window.innerHeight) {
                   1574:         Geometry.getViewportHeight   = function() { return window.innerHeight; };
                   1575:         Geometry.getViewportWidth   = function() { return window.innerWidth; };
                   1576:         Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
                   1577:         Geometry.getVerticalScroll   = function() { return window.pageYOffset; };
                   1578:     }
                   1579:     else if (document.documentElement && document.documentElement.clientHeight) {
                   1580:         Geometry.getViewportHeight =
                   1581:             function() { return document.documentElement.clientHeight; };
                   1582:         Geometry.getViewportWidth =
                   1583:             function() { return document.documentElement.clientWidth; };
                   1584: 
                   1585:         Geometry.getHorizontalScroll =
                   1586:             function() { return document.documentElement.scrollLeft; };
                   1587:         Geometry.getVerticalScroll =
                   1588:             function() { return document.documentElement.scrollTop; };
                   1589:     }
                   1590:     else if (document.body.clientHeight) {
                   1591:         Geometry.getViewportHeight =
                   1592:             function() { return document.body.clientHeight; };
                   1593:         Geometry.getViewportWidth =
                   1594:             function() { return document.body.clientWidth; };
                   1595:         Geometry.getHorizontalScroll =
                   1596:             function() { return document.body.scrollLeft; };
                   1597:         Geometry.getVerticalScroll =
                   1598:             function() { return document.body.scrollTop; };
                   1599:     }
                   1600: }
                   1601: 
                   1602: GEOMETRY
                   1603: }
                   1604: 
                   1605: =pod
                   1606: 
1.648     raeburn  1607: =item * &viewport_size_js()
1.590     raeburn  1608: 
                   1609: 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. 
                   1610: 
                   1611: =cut
                   1612: 
                   1613: sub viewport_size_js {
                   1614:     my $geometry = &viewport_geometry_js();
                   1615:     return <<"DIMS";
                   1616: 
                   1617: $geometry
                   1618: 
                   1619: function getViewportDims(width,height) {
                   1620:     init_geometry();
                   1621:     width.value = Geometry.getViewportWidth();
                   1622:     height.value = Geometry.getViewportHeight();
                   1623:     return;
                   1624: }
                   1625: 
                   1626: DIMS
                   1627: }
                   1628: 
                   1629: =pod
                   1630: 
1.648     raeburn  1631: =item * &resize_textarea_js()
1.565     albertel 1632: 
                   1633: emits the needed javascript to resize a textarea to be as big as possible
                   1634: 
                   1635: creates a function resize_textrea that takes two IDs first should be
                   1636: the id of the element to resize, second should be the id of a div that
                   1637: surrounds everything that comes after the textarea, this routine needs
                   1638: to be attached to the <body> for the onload and onresize events.
                   1639: 
1.648     raeburn  1640: =back
1.565     albertel 1641: 
                   1642: =cut
                   1643: 
                   1644: sub resize_textarea_js {
1.590     raeburn  1645:     my $geometry = &viewport_geometry_js();
1.565     albertel 1646:     return <<"RESIZE";
                   1647:     <script type="text/javascript">
1.824     bisitz   1648: // <![CDATA[
1.590     raeburn  1649: $geometry
1.565     albertel 1650: 
1.588     albertel 1651: function getX(element) {
                   1652:     var x = 0;
                   1653:     while (element) {
                   1654: 	x += element.offsetLeft;
                   1655: 	element = element.offsetParent;
                   1656:     }
                   1657:     return x;
                   1658: }
                   1659: function getY(element) {
                   1660:     var y = 0;
                   1661:     while (element) {
                   1662: 	y += element.offsetTop;
                   1663: 	element = element.offsetParent;
                   1664:     }
                   1665:     return y;
                   1666: }
                   1667: 
                   1668: 
1.565     albertel 1669: function resize_textarea(textarea_id,bottom_id) {
                   1670:     init_geometry();
                   1671:     var textarea        = document.getElementById(textarea_id);
                   1672:     //alert(textarea);
                   1673: 
1.588     albertel 1674:     var textarea_top    = getY(textarea);
1.565     albertel 1675:     var textarea_height = textarea.offsetHeight;
                   1676:     var bottom          = document.getElementById(bottom_id);
1.588     albertel 1677:     var bottom_top      = getY(bottom);
1.565     albertel 1678:     var bottom_height   = bottom.offsetHeight;
                   1679:     var window_height   = Geometry.getViewportHeight();
1.588     albertel 1680:     var fudge           = 23;
1.565     albertel 1681:     var new_height      = window_height-fudge-textarea_top-bottom_height;
                   1682:     if (new_height < 300) {
                   1683: 	new_height = 300;
                   1684:     }
                   1685:     textarea.style.height=new_height+'px';
                   1686: }
1.824     bisitz   1687: // ]]>
1.565     albertel 1688: </script>
                   1689: RESIZE
                   1690: 
                   1691: }
                   1692: 
                   1693: =pod
                   1694: 
1.256     matthew  1695: =head1 Excel and CSV file utility routines
                   1696: 
                   1697: =over 4
                   1698: 
                   1699: =cut
                   1700: 
                   1701: ###############################################################
                   1702: ###############################################################
                   1703: 
                   1704: =pod
                   1705: 
1.648     raeburn  1706: =item * &csv_translate($text) 
1.37      matthew  1707: 
1.185     www      1708: Translate $text to allow it to be output as a 'comma separated values' 
1.37      matthew  1709: format.
                   1710: 
                   1711: =cut
                   1712: 
1.180     matthew  1713: ###############################################################
                   1714: ###############################################################
1.37      matthew  1715: sub csv_translate {
                   1716:     my $text = shift;
                   1717:     $text =~ s/\"/\"\"/g;
1.209     albertel 1718:     $text =~ s/\n/ /g;
1.37      matthew  1719:     return $text;
                   1720: }
1.180     matthew  1721: 
                   1722: ###############################################################
                   1723: ###############################################################
                   1724: 
                   1725: =pod
                   1726: 
1.648     raeburn  1727: =item * &define_excel_formats()
1.180     matthew  1728: 
                   1729: Define some commonly used Excel cell formats.
                   1730: 
                   1731: Currently supported formats:
                   1732: 
                   1733: =over 4
                   1734: 
                   1735: =item header
                   1736: 
                   1737: =item bold
                   1738: 
                   1739: =item h1
                   1740: 
                   1741: =item h2
                   1742: 
                   1743: =item h3
                   1744: 
1.256     matthew  1745: =item h4
                   1746: 
                   1747: =item i
                   1748: 
1.180     matthew  1749: =item date
                   1750: 
                   1751: =back
                   1752: 
                   1753: Inputs: $workbook
                   1754: 
                   1755: Returns: $format, a hash reference.
                   1756: 
1.1057    foxr     1757: 
1.180     matthew  1758: =cut
                   1759: 
                   1760: ###############################################################
                   1761: ###############################################################
                   1762: sub define_excel_formats {
                   1763:     my ($workbook) = @_;
                   1764:     my $format;
                   1765:     $format->{'header'} = $workbook->add_format(bold      => 1, 
                   1766:                                                 bottom    => 1,
                   1767:                                                 align     => 'center');
                   1768:     $format->{'bold'} = $workbook->add_format(bold=>1);
                   1769:     $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
                   1770:     $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
                   1771:     $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
1.255     matthew  1772:     $format->{'h4'}   = $workbook->add_format(bold=>1, size=>12);
1.246     matthew  1773:     $format->{'i'}    = $workbook->add_format(italic=>1);
1.180     matthew  1774:     $format->{'date'} = $workbook->add_format(num_format=>
1.207     matthew  1775:                                             'mm/dd/yyyy hh:mm:ss');
1.180     matthew  1776:     return $format;
                   1777: }
                   1778: 
                   1779: ###############################################################
                   1780: ###############################################################
1.113     bowersj2 1781: 
                   1782: =pod
                   1783: 
1.648     raeburn  1784: =item * &create_workbook()
1.255     matthew  1785: 
                   1786: Create an Excel worksheet.  If it fails, output message on the
                   1787: request object and return undefs.
                   1788: 
                   1789: Inputs: Apache request object
                   1790: 
                   1791: Returns (undef) on failure, 
                   1792:     Excel worksheet object, scalar with filename, and formats 
                   1793:     from &Apache::loncommon::define_excel_formats on success
                   1794: 
                   1795: =cut
                   1796: 
                   1797: ###############################################################
                   1798: ###############################################################
                   1799: sub create_workbook {
                   1800:     my ($r) = @_;
                   1801:         #
                   1802:     # Create the excel spreadsheet
                   1803:     my $filename = '/prtspool/'.
1.258     albertel 1804:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.255     matthew  1805:         time.'_'.rand(1000000000).'.xls';
                   1806:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
                   1807:     if (! defined($workbook)) {
                   1808:         $r->log_error("Error creating excel spreadsheet $filename: $!");
1.928     bisitz   1809:         $r->print(
                   1810:             '<p class="LC_error">'
                   1811:            .&mt('Problems occurred in creating the new Excel file.')
                   1812:            .' '.&mt('This error has been logged.')
                   1813:            .' '.&mt('Please alert your LON-CAPA administrator.')
                   1814:            .'</p>'
                   1815:         );
1.255     matthew  1816:         return (undef);
                   1817:     }
                   1818:     #
1.1014    foxr     1819:     $workbook->set_tempdir(LONCAPA::tempdir());
1.255     matthew  1820:     #
                   1821:     my $format = &Apache::loncommon::define_excel_formats($workbook);
                   1822:     return ($workbook,$filename,$format);
                   1823: }
                   1824: 
                   1825: ###############################################################
                   1826: ###############################################################
                   1827: 
                   1828: =pod
                   1829: 
1.648     raeburn  1830: =item * &create_text_file()
1.113     bowersj2 1831: 
1.542     raeburn  1832: Create a file to write to and eventually make available to the user.
1.256     matthew  1833: If file creation fails, outputs an error message on the request object and 
                   1834: return undefs.
1.113     bowersj2 1835: 
1.256     matthew  1836: Inputs: Apache request object, and file suffix
1.113     bowersj2 1837: 
1.256     matthew  1838: Returns (undef) on failure, 
                   1839:     Filehandle and filename on success.
1.113     bowersj2 1840: 
                   1841: =cut
                   1842: 
1.256     matthew  1843: ###############################################################
                   1844: ###############################################################
                   1845: sub create_text_file {
                   1846:     my ($r,$suffix) = @_;
                   1847:     if (! defined($suffix)) { $suffix = 'txt'; };
                   1848:     my $fh;
                   1849:     my $filename = '/prtspool/'.
1.258     albertel 1850:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.256     matthew  1851:         time.'_'.rand(1000000000).'.'.$suffix;
                   1852:     $fh = Apache::File->new('>/home/httpd'.$filename);
                   1853:     if (! defined($fh)) {
                   1854:         $r->log_error("Couldn't open $filename for output $!");
1.928     bisitz   1855:         $r->print(
                   1856:             '<p class="LC_error">'
                   1857:            .&mt('Problems occurred in creating the output file.')
                   1858:            .' '.&mt('This error has been logged.')
                   1859:            .' '.&mt('Please alert your LON-CAPA administrator.')
                   1860:            .'</p>'
                   1861:         );
1.113     bowersj2 1862:     }
1.256     matthew  1863:     return ($fh,$filename)
1.113     bowersj2 1864: }
                   1865: 
                   1866: 
1.256     matthew  1867: =pod 
1.113     bowersj2 1868: 
                   1869: =back
                   1870: 
                   1871: =cut
1.37      matthew  1872: 
                   1873: ###############################################################
1.33      matthew  1874: ##        Home server <option> list generating code          ##
                   1875: ###############################################################
1.35      matthew  1876: 
1.169     www      1877: # ------------------------------------------
                   1878: 
                   1879: sub domain_select {
                   1880:     my ($name,$value,$multiple)=@_;
                   1881:     my %domains=map { 
1.514     albertel 1882: 	$_ => $_.' '. &Apache::lonnet::domain($_,'description') 
1.512     albertel 1883:     } &Apache::lonnet::all_domains();
1.169     www      1884:     if ($multiple) {
                   1885: 	$domains{''}=&mt('Any domain');
1.550     albertel 1886: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
1.287     albertel 1887: 	return &multiple_select_form($name,$value,4,\%domains);
1.169     www      1888:     } else {
1.550     albertel 1889: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
1.970     raeburn  1890: 	return &select_form($name,$value,\%domains);
1.169     www      1891:     }
                   1892: }
                   1893: 
1.282     albertel 1894: #-------------------------------------------
                   1895: 
                   1896: =pod
                   1897: 
1.519     raeburn  1898: =head1 Routines for form select boxes
                   1899: 
                   1900: =over 4
                   1901: 
1.648     raeburn  1902: =item * &multiple_select_form($name,$value,$size,$hash,$order)
1.282     albertel 1903: 
                   1904: Returns a string containing a <select> element int multiple mode
                   1905: 
                   1906: 
                   1907: Args:
                   1908:   $name - name of the <select> element
1.506     raeburn  1909:   $value - scalar or array ref of values that should already be selected
1.282     albertel 1910:   $size - number of rows long the select element is
1.283     albertel 1911:   $hash - the elements should be 'option' => 'shown text'
1.282     albertel 1912:           (shown text should already have been &mt())
1.506     raeburn  1913:   $order - (optional) array ref of the order to show the elements in
1.283     albertel 1914: 
1.282     albertel 1915: =cut
                   1916: 
                   1917: #-------------------------------------------
1.169     www      1918: sub multiple_select_form {
1.284     albertel 1919:     my ($name,$value,$size,$hash,$order)=@_;
1.169     www      1920:     my %selected = map { $_ => 1 } ref($value)?@{$value}:($value);
                   1921:     my $output='';
1.191     matthew  1922:     if (! defined($size)) {
                   1923:         $size = 4;
1.283     albertel 1924:         if (scalar(keys(%$hash))<4) {
                   1925:             $size = scalar(keys(%$hash));
1.191     matthew  1926:         }
                   1927:     }
1.734     bisitz   1928:     $output.="\n".'<select name="'.$name.'" size="'.$size.'" multiple="multiple">';
1.501     banghart 1929:     my @order;
1.506     raeburn  1930:     if (ref($order) eq 'ARRAY')  {
                   1931:         @order = @{$order};
                   1932:     } else {
                   1933:         @order = sort(keys(%$hash));
1.501     banghart 1934:     }
                   1935:     if (exists($$hash{'select_form_order'})) {
                   1936:         @order = @{$$hash{'select_form_order'}};
                   1937:     }
                   1938:         
1.284     albertel 1939:     foreach my $key (@order) {
1.356     albertel 1940:         $output.='<option value="'.&HTML::Entities::encode($key,'"<>&').'" ';
1.284     albertel 1941:         $output.='selected="selected" ' if ($selected{$key});
                   1942:         $output.='>'.$hash->{$key}."</option>\n";
1.169     www      1943:     }
                   1944:     $output.="</select>\n";
                   1945:     return $output;
                   1946: }
                   1947: 
1.88      www      1948: #-------------------------------------------
                   1949: 
                   1950: =pod
                   1951: 
1.970     raeburn  1952: =item * &select_form($defdom,$name,$hashref,$onchange)
1.88      www      1953: 
                   1954: Returns a string containing a <select name='$name' size='1'> form to 
1.970     raeburn  1955: allow a user to select options from a ref to a hash containing:
                   1956: option_name => displayed text. An optional $onchange can include
                   1957: a javascript onchange item, e.g., onchange="this.form.submit();"  
                   1958: 
1.88      www      1959: See lonrights.pm for an example invocation and use.
                   1960: 
                   1961: =cut
                   1962: 
                   1963: #-------------------------------------------
                   1964: sub select_form {
1.970     raeburn  1965:     my ($def,$name,$hashref,$onchange) = @_;
                   1966:     return unless (ref($hashref) eq 'HASH');
                   1967:     if ($onchange) {
                   1968:         $onchange = ' onchange="'.$onchange.'"';
                   1969:     }
                   1970:     my $selectform = "<select name=\"$name\" size=\"1\"$onchange>\n";
1.128     albertel 1971:     my @keys;
1.970     raeburn  1972:     if (exists($hashref->{'select_form_order'})) {
                   1973: 	@keys=@{$hashref->{'select_form_order'}};
1.128     albertel 1974:     } else {
1.970     raeburn  1975: 	@keys=sort(keys(%{$hashref}));
1.128     albertel 1976:     }
1.356     albertel 1977:     foreach my $key (@keys) {
                   1978:         $selectform.=
                   1979: 	    '<option value="'.&HTML::Entities::encode($key,'"<>&').'" '.
                   1980:             ($key eq $def ? 'selected="selected" ' : '').
1.970     raeburn  1981:                 ">".$hashref->{$key}."</option>\n";
1.88      www      1982:     }
                   1983:     $selectform.="</select>";
                   1984:     return $selectform;
                   1985: }
                   1986: 
1.475     www      1987: # For display filters
                   1988: 
                   1989: sub display_filter {
1.1074    raeburn  1990:     my ($context) = @_;
1.475     www      1991:     if (!$env{'form.show'}) { $env{'form.show'}=10; }
1.477     www      1992:     if (!$env{'form.displayfilter'}) { $env{'form.displayfilter'}='currentfolder'; }
1.1074    raeburn  1993:     my $phraseinput = 'hidden';
                   1994:     my $includeinput = 'hidden';
                   1995:     my ($checked,$includetypestext);
                   1996:     if ($env{'form.displayfilter'} eq 'containing') {
                   1997:         $phraseinput = 'text'; 
                   1998:         if ($context eq 'parmslog') {
                   1999:             $includeinput = 'checkbox';
                   2000:             if ($env{'form.includetypes'}) {
                   2001:                 $checked = ' checked="checked"';
                   2002:             }
                   2003:             $includetypestext = &mt('Include parameter types');
                   2004:         }
                   2005:     } else {
                   2006:         $includetypestext = '&nbsp;';
                   2007:     }
                   2008:     my ($additional,$secondid,$thirdid);
                   2009:     if ($context eq 'parmslog') {
                   2010:         $additional = 
                   2011:             '<label><input type="'.$includeinput.'" name="includetypes"'. 
                   2012:             $checked.' name="includetypes" value="1" id="includetypes" />'.
                   2013:             '&nbsp;<span id="includetypestext">'.$includetypestext.'</span>'.
                   2014:             '</label>';
                   2015:         $secondid = 'includetypes';
                   2016:         $thirdid = 'includetypestext';
                   2017:     }
                   2018:     my $onchange = "javascript:toggleHistoryOptions(this,'containingphrase','$context',
                   2019:                                                     '$secondid','$thirdid')";
                   2020:     return '<span class="LC_nobreak"><label>'.&mt('Records: [_1]',
1.475     www      2021: 			       &Apache::lonmeta::selectbox('show',$env{'form.show'},undef,
                   2022: 							   (&mt('all'),10,20,50,100,1000,10000))).
1.714     bisitz   2023: 	   '</label></span> <span class="LC_nobreak">'.
1.1074    raeburn  2024:            &mt('Filter: [_1]',
1.477     www      2025: 	   &select_form($env{'form.displayfilter'},
                   2026: 			'displayfilter',
1.970     raeburn  2027: 			{'currentfolder' => 'Current folder/page',
1.477     www      2028: 			 'containing' => 'Containing phrase',
1.1074    raeburn  2029: 			 'none' => 'None'},$onchange)).'&nbsp;'.
                   2030: 			 '<input type="'.$phraseinput.'" name="containingphrase" id="containingphrase" size="30" value="'.
                   2031:                          &HTML::Entities::encode($env{'form.containingphrase'}).
                   2032:                          '" />'.$additional;
                   2033: }
                   2034: 
                   2035: sub display_filter_js {
                   2036:     my $includetext = &mt('Include parameter types');
                   2037:     return <<"ENDJS";
                   2038:   
                   2039: function toggleHistoryOptions(setter,firstid,context,secondid,thirdid) {
                   2040:     var firstType = 'hidden';
                   2041:     if (setter.options[setter.selectedIndex].value == 'containing') {
                   2042:         firstType = 'text';
                   2043:     }
                   2044:     firstObject = document.getElementById(firstid);
                   2045:     if (typeof(firstObject) == 'object') {
                   2046:         if (firstObject.type != firstType) {
                   2047:             changeInputType(firstObject,firstType);
                   2048:         }
                   2049:     }
                   2050:     if (context == 'parmslog') {
                   2051:         var secondType = 'hidden';
                   2052:         if (firstType == 'text') {
                   2053:             secondType = 'checkbox';
                   2054:         }
                   2055:         secondObject = document.getElementById(secondid);  
                   2056:         if (typeof(secondObject) == 'object') {
                   2057:             if (secondObject.type != secondType) {
                   2058:                 changeInputType(secondObject,secondType);
                   2059:             }
                   2060:         }
                   2061:         var textItem = document.getElementById(thirdid);
                   2062:         var currtext = textItem.innerHTML;
                   2063:         var newtext;
                   2064:         if (firstType == 'text') {
                   2065:             newtext = '$includetext';
                   2066:         } else {
                   2067:             newtext = '&nbsp;';
                   2068:         }
                   2069:         if (currtext != newtext) {
                   2070:             textItem.innerHTML = newtext;
                   2071:         }
                   2072:     }
                   2073:     return;
                   2074: }
                   2075: 
                   2076: function changeInputType(oldObject,newType) {
                   2077:     var newObject = document.createElement('input');
                   2078:     newObject.type = newType;
                   2079:     if (oldObject.size) {
                   2080:         newObject.size = oldObject.size;
                   2081:     }
                   2082:     if (oldObject.value) {
                   2083:         newObject.value = oldObject.value;
                   2084:     }
                   2085:     if (oldObject.name) {
                   2086:         newObject.name = oldObject.name;
                   2087:     }
                   2088:     if (oldObject.id) {
                   2089:         newObject.id = oldObject.id;
                   2090:     }
                   2091:     oldObject.parentNode.replaceChild(newObject,oldObject);
                   2092:     return;
                   2093: }
                   2094: 
                   2095: ENDJS
1.475     www      2096: }
                   2097: 
1.167     www      2098: sub gradeleveldescription {
                   2099:     my $gradelevel=shift;
                   2100:     my %gradelevels=(0 => 'Not specified',
                   2101: 		     1 => 'Grade 1',
                   2102: 		     2 => 'Grade 2',
                   2103: 		     3 => 'Grade 3',
                   2104: 		     4 => 'Grade 4',
                   2105: 		     5 => 'Grade 5',
                   2106: 		     6 => 'Grade 6',
                   2107: 		     7 => 'Grade 7',
                   2108: 		     8 => 'Grade 8',
                   2109: 		     9 => 'Grade 9',
                   2110: 		     10 => 'Grade 10',
                   2111: 		     11 => 'Grade 11',
                   2112: 		     12 => 'Grade 12',
                   2113: 		     13 => 'Grade 13',
                   2114: 		     14 => '100 Level',
                   2115: 		     15 => '200 Level',
                   2116: 		     16 => '300 Level',
                   2117: 		     17 => '400 Level',
                   2118: 		     18 => 'Graduate Level');
                   2119:     return &mt($gradelevels{$gradelevel});
                   2120: }
                   2121: 
1.163     www      2122: sub select_level_form {
                   2123:     my ($deflevel,$name)=@_;
                   2124:     unless ($deflevel) { $deflevel=0; }
1.167     www      2125:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
                   2126:     for (my $i=0; $i<=18; $i++) {
                   2127:         $selectform.="<option value=\"$i\" ".
1.253     albertel 2128:             ($i==$deflevel ? 'selected="selected" ' : '').
1.167     www      2129:                 ">".&gradeleveldescription($i)."</option>\n";
                   2130:     }
                   2131:     $selectform.="</select>";
                   2132:     return $selectform;
1.163     www      2133: }
1.167     www      2134: 
1.35      matthew  2135: #-------------------------------------------
                   2136: 
1.45      matthew  2137: =pod
                   2138: 
1.910     raeburn  2139: =item * &select_dom_form($defdom,$name,$includeempty,$showdomdesc,$onchange,$incdoms)
1.35      matthew  2140: 
                   2141: Returns a string containing a <select name='$name' size='1'> form to 
                   2142: allow a user to select the domain to preform an operation in.  
                   2143: See loncreateuser.pm for an example invocation and use.
                   2144: 
1.90      www      2145: If the $includeempty flag is set, it also includes an empty choice ("no domain
                   2146: selected");
                   2147: 
1.743     raeburn  2148: If the $showdomdesc flag is set, the domain name is followed by the domain description.
                   2149: 
1.910     raeburn  2150: 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.
                   2151: 
                   2152: The optional $incdoms is a reference to an array of domains which will be the only available options. 
1.563     raeburn  2153: 
1.35      matthew  2154: =cut
                   2155: 
                   2156: #-------------------------------------------
1.34      matthew  2157: sub select_dom_form {
1.910     raeburn  2158:     my ($defdom,$name,$includeempty,$showdomdesc,$onchange,$incdoms) = @_;
1.872     raeburn  2159:     if ($onchange) {
1.874     raeburn  2160:         $onchange = ' onchange="'.$onchange.'"';
1.743     raeburn  2161:     }
1.910     raeburn  2162:     my @domains;
                   2163:     if (ref($incdoms) eq 'ARRAY') {
                   2164:         @domains = sort {lc($a) cmp lc($b)} (@{$incdoms});
                   2165:     } else {
                   2166:         @domains = sort {lc($a) cmp lc($b)} (&Apache::lonnet::all_domains());
                   2167:     }
1.90      www      2168:     if ($includeempty) { @domains=('',@domains); }
1.743     raeburn  2169:     my $selectdomain = "<select name=\"$name\" size=\"1\"$onchange>\n";
1.356     albertel 2170:     foreach my $dom (@domains) {
                   2171:         $selectdomain.="<option value=\"$dom\" ".
1.563     raeburn  2172:             ($dom eq $defdom ? 'selected="selected" ' : '').'>'.$dom;
                   2173:         if ($showdomdesc) {
                   2174:             if ($dom ne '') {
                   2175:                 my $domdesc = &Apache::lonnet::domain($dom,'description');
                   2176:                 if ($domdesc ne '') {
                   2177:                     $selectdomain .= ' ('.$domdesc.')';
                   2178:                 }
                   2179:             } 
                   2180:         }
                   2181:         $selectdomain .= "</option>\n";
1.34      matthew  2182:     }
                   2183:     $selectdomain.="</select>";
                   2184:     return $selectdomain;
                   2185: }
                   2186: 
1.35      matthew  2187: #-------------------------------------------
                   2188: 
1.45      matthew  2189: =pod
                   2190: 
1.648     raeburn  2191: =item * &home_server_form_item($domain,$name,$defaultflag)
1.35      matthew  2192: 
1.586     raeburn  2193: input: 4 arguments (two required, two optional) - 
                   2194:     $domain - domain of new user
                   2195:     $name - name of form element
                   2196:     $default - Value of 'default' causes a default item to be first 
                   2197:                             option, and selected by default. 
                   2198:     $hide - Value of 'hide' causes hiding of the name of the server, 
                   2199:                             if 1 server found, or default, if 0 found.
1.594     raeburn  2200: output: returns 2 items: 
1.586     raeburn  2201: (a) form element which contains either:
                   2202:    (i) <select name="$name">
                   2203:         <option value="$hostid1">$hostid $servers{$hostid}</option>
                   2204:         <option value="$hostid2">$hostid $servers{$hostid}</option>       
                   2205:        </select>
                   2206:        form item if there are multiple library servers in $domain, or
                   2207:    (ii) an <input type="hidden" name="$name" value="$hostid" /> form item 
                   2208:        if there is only one library server in $domain.
                   2209: 
                   2210: (b) number of library servers found.
                   2211: 
                   2212: See loncreateuser.pm for example of use.
1.35      matthew  2213: 
                   2214: =cut
                   2215: 
                   2216: #-------------------------------------------
1.586     raeburn  2217: sub home_server_form_item {
                   2218:     my ($domain,$name,$default,$hide) = @_;
1.513     albertel 2219:     my %servers = &Apache::lonnet::get_servers($domain,'library');
1.586     raeburn  2220:     my $result;
                   2221:     my $numlib = keys(%servers);
                   2222:     if ($numlib > 1) {
                   2223:         $result .= '<select name="'.$name.'" />'."\n";
                   2224:         if ($default) {
1.804     bisitz   2225:             $result .= '<option value="default" selected="selected">'.&mt('default').
1.586     raeburn  2226:                        '</option>'."\n";
                   2227:         }
                   2228:         foreach my $hostid (sort(keys(%servers))) {
                   2229:             $result.= '<option value="'.$hostid.'">'.
                   2230: 	              $hostid.' '.$servers{$hostid}."</option>\n";
                   2231:         }
                   2232:         $result .= '</select>'."\n";
                   2233:     } elsif ($numlib == 1) {
                   2234:         my $hostid;
                   2235:         foreach my $item (keys(%servers)) {
                   2236:             $hostid = $item;
                   2237:         }
                   2238:         $result .= '<input type="hidden" name="'.$name.'" value="'.
                   2239:                    $hostid.'" />';
                   2240:                    if (!$hide) {
                   2241:                        $result .= $hostid.' '.$servers{$hostid};
                   2242:                    }
                   2243:                    $result .= "\n";
                   2244:     } elsif ($default) {
                   2245:         $result .= '<input type="hidden" name="'.$name.
                   2246:                    '" value="default" />';
                   2247:                    if (!$hide) {
                   2248:                        $result .= &mt('default');
                   2249:                    }
                   2250:                    $result .= "\n";
1.33      matthew  2251:     }
1.586     raeburn  2252:     return ($result,$numlib);
1.33      matthew  2253: }
1.112     bowersj2 2254: 
                   2255: =pod
                   2256: 
1.534     albertel 2257: =back 
                   2258: 
1.112     bowersj2 2259: =cut
1.87      matthew  2260: 
                   2261: ###############################################################
1.112     bowersj2 2262: ##                  Decoding User Agent                      ##
1.87      matthew  2263: ###############################################################
                   2264: 
                   2265: =pod
                   2266: 
1.112     bowersj2 2267: =head1 Decoding the User Agent
                   2268: 
                   2269: =over 4
                   2270: 
                   2271: =item * &decode_user_agent()
1.87      matthew  2272: 
                   2273: Inputs: $r
                   2274: 
                   2275: Outputs:
                   2276: 
                   2277: =over 4
                   2278: 
1.112     bowersj2 2279: =item * $httpbrowser
1.87      matthew  2280: 
1.112     bowersj2 2281: =item * $clientbrowser
1.87      matthew  2282: 
1.112     bowersj2 2283: =item * $clientversion
1.87      matthew  2284: 
1.112     bowersj2 2285: =item * $clientmathml
1.87      matthew  2286: 
1.112     bowersj2 2287: =item * $clientunicode
1.87      matthew  2288: 
1.112     bowersj2 2289: =item * $clientos
1.87      matthew  2290: 
                   2291: =back
                   2292: 
1.157     matthew  2293: =back 
                   2294: 
1.87      matthew  2295: =cut
                   2296: 
                   2297: ###############################################################
                   2298: ###############################################################
                   2299: sub decode_user_agent {
1.247     albertel 2300:     my ($r)=@_;
1.87      matthew  2301:     my @browsertype=split(/\&/,$Apache::lonnet::perlvar{"lonBrowsDet"});
                   2302:     my %mathcap=split(/\&/,$$Apache::lonnet::perlvar{"lonMathML"});
                   2303:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
1.247     albertel 2304:     if (!$httpbrowser && $r) { $httpbrowser=$r->header_in('User-Agent'); }
1.87      matthew  2305:     my $clientbrowser='unknown';
                   2306:     my $clientversion='0';
                   2307:     my $clientmathml='';
                   2308:     my $clientunicode='0';
                   2309:     for (my $i=0;$i<=$#browsertype;$i++) {
                   2310:         my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\:/,$browsertype[$i]);
                   2311: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
                   2312: 	    $clientbrowser=$bname;
                   2313:             $httpbrowser=~/$vreg/i;
                   2314: 	    $clientversion=$1;
                   2315:             $clientmathml=($clientversion>=$minv);
                   2316:             $clientunicode=($clientversion>=$univ);
                   2317: 	}
                   2318:     }
                   2319:     my $clientos='unknown';
                   2320:     if (($httpbrowser=~/linux/i) ||
                   2321:         ($httpbrowser=~/unix/i) ||
                   2322:         ($httpbrowser=~/ux/i) ||
                   2323:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
                   2324:     if (($httpbrowser=~/vax/i) ||
                   2325:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
                   2326:     if ($httpbrowser=~/next/i) { $clientos='next'; }
                   2327:     if (($httpbrowser=~/mac/i) ||
                   2328:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
                   2329:     if ($httpbrowser=~/win/i) { $clientos='win'; }
                   2330:     if ($httpbrowser=~/embed/i) { $clientos='pda'; }
                   2331:     return ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                   2332:             $clientunicode,$clientos,);
                   2333: }
                   2334: 
1.32      matthew  2335: ###############################################################
                   2336: ##    Authentication changing form generation subroutines    ##
                   2337: ###############################################################
                   2338: ##
                   2339: ## All of the authform_xxxxxxx subroutines take their inputs in a
                   2340: ## hash, and have reasonable default values.
                   2341: ##
                   2342: ##    formname = the name given in the <form> tag.
1.35      matthew  2343: #-------------------------------------------
                   2344: 
1.45      matthew  2345: =pod
                   2346: 
1.112     bowersj2 2347: =head1 Authentication Routines
                   2348: 
                   2349: =over 4
                   2350: 
1.648     raeburn  2351: =item * &authform_xxxxxx()
1.35      matthew  2352: 
                   2353: The authform_xxxxxx subroutines provide javascript and html forms which 
                   2354: handle some of the conveniences required for authentication forms.  
                   2355: This is not an optimal method, but it works.  
                   2356: 
                   2357: =over 4
                   2358: 
1.112     bowersj2 2359: =item * authform_header
1.35      matthew  2360: 
1.112     bowersj2 2361: =item * authform_authorwarning
1.35      matthew  2362: 
1.112     bowersj2 2363: =item * authform_nochange
1.35      matthew  2364: 
1.112     bowersj2 2365: =item * authform_kerberos
1.35      matthew  2366: 
1.112     bowersj2 2367: =item * authform_internal
1.35      matthew  2368: 
1.112     bowersj2 2369: =item * authform_filesystem
1.35      matthew  2370: 
                   2371: =back
                   2372: 
1.648     raeburn  2373: See loncreateuser.pm for invocation and use examples.
1.157     matthew  2374: 
1.35      matthew  2375: =cut
                   2376: 
                   2377: #-------------------------------------------
1.32      matthew  2378: sub authform_header{  
                   2379:     my %in = (
                   2380:         formname => 'cu',
1.80      albertel 2381:         kerb_def_dom => '',
1.32      matthew  2382:         @_,
                   2383:     );
                   2384:     $in{'formname'} = 'document.' . $in{'formname'};
                   2385:     my $result='';
1.80      albertel 2386: 
                   2387: #---------------------------------------------- Code for upper case translation
                   2388:     my $Javascript_toUpperCase;
                   2389:     unless ($in{kerb_def_dom}) {
                   2390:         $Javascript_toUpperCase =<<"END";
                   2391:         switch (choice) {
                   2392:            case 'krb': currentform.elements[choicearg].value =
                   2393:                currentform.elements[choicearg].value.toUpperCase();
                   2394:                break;
                   2395:            default:
                   2396:         }
                   2397: END
                   2398:     } else {
                   2399:         $Javascript_toUpperCase = "";
                   2400:     }
                   2401: 
1.165     raeburn  2402:     my $radioval = "'nochange'";
1.591     raeburn  2403:     if (defined($in{'curr_authtype'})) {
                   2404:         if ($in{'curr_authtype'} ne '') {
                   2405:             $radioval = "'".$in{'curr_authtype'}."arg'";
                   2406:         }
1.174     matthew  2407:     }
1.165     raeburn  2408:     my $argfield = 'null';
1.591     raeburn  2409:     if (defined($in{'mode'})) {
1.165     raeburn  2410:         if ($in{'mode'} eq 'modifycourse')  {
1.591     raeburn  2411:             if (defined($in{'curr_autharg'})) {
                   2412:                 if ($in{'curr_autharg'} ne '') {
1.165     raeburn  2413:                     $argfield = "'$in{'curr_autharg'}'";
                   2414:                 }
                   2415:             }
                   2416:         }
                   2417:     }
                   2418: 
1.32      matthew  2419:     $result.=<<"END";
                   2420: var current = new Object();
1.165     raeburn  2421: current.radiovalue = $radioval;
                   2422: current.argfield = $argfield;
1.32      matthew  2423: 
                   2424: function changed_radio(choice,currentform) {
                   2425:     var choicearg = choice + 'arg';
                   2426:     // If a radio button in changed, we need to change the argfield
                   2427:     if (current.radiovalue != choice) {
                   2428:         current.radiovalue = choice;
                   2429:         if (current.argfield != null) {
                   2430:             currentform.elements[current.argfield].value = '';
                   2431:         }
                   2432:         if (choice == 'nochange') {
                   2433:             current.argfield = null;
                   2434:         } else {
                   2435:             current.argfield = choicearg;
                   2436:             switch(choice) {
                   2437:                 case 'krb': 
                   2438:                     currentform.elements[current.argfield].value = 
                   2439:                         "$in{'kerb_def_dom'}";
                   2440:                 break;
                   2441:               default:
                   2442:                 break;
                   2443:             }
                   2444:         }
                   2445:     }
                   2446:     return;
                   2447: }
1.22      www      2448: 
1.32      matthew  2449: function changed_text(choice,currentform) {
                   2450:     var choicearg = choice + 'arg';
                   2451:     if (currentform.elements[choicearg].value !='') {
1.80      albertel 2452:         $Javascript_toUpperCase
1.32      matthew  2453:         // clear old field
                   2454:         if ((current.argfield != choicearg) && (current.argfield != null)) {
                   2455:             currentform.elements[current.argfield].value = '';
                   2456:         }
                   2457:         current.argfield = choicearg;
                   2458:     }
                   2459:     set_auth_radio_buttons(choice,currentform);
                   2460:     return;
1.20      www      2461: }
1.32      matthew  2462: 
                   2463: function set_auth_radio_buttons(newvalue,currentform) {
1.986     raeburn  2464:     var numauthchoices = currentform.login.length;
                   2465:     if (typeof numauthchoices  == "undefined") {
                   2466:         return;
                   2467:     } 
1.32      matthew  2468:     var i=0;
1.986     raeburn  2469:     while (i < numauthchoices) {
1.32      matthew  2470:         if (currentform.login[i].value == newvalue) { break; }
                   2471:         i++;
                   2472:     }
1.986     raeburn  2473:     if (i == numauthchoices) {
1.32      matthew  2474:         return;
                   2475:     }
                   2476:     current.radiovalue = newvalue;
                   2477:     currentform.login[i].checked = true;
                   2478:     return;
                   2479: }
                   2480: END
                   2481:     return $result;
                   2482: }
                   2483: 
1.1075.2.20! raeburn  2484: sub authform_authorwarning {
1.32      matthew  2485:     my $result='';
1.144     matthew  2486:     $result='<i>'.
                   2487:         &mt('As a general rule, only authors or co-authors should be '.
                   2488:             'filesystem authenticated '.
                   2489:             '(which allows access to the server filesystem).')."</i>\n";
1.32      matthew  2490:     return $result;
                   2491: }
                   2492: 
1.1075.2.20! raeburn  2493: sub authform_nochange {
1.32      matthew  2494:     my %in = (
                   2495:               formname => 'document.cu',
                   2496:               kerb_def_dom => 'MSU.EDU',
                   2497:               @_,
                   2498:           );
1.1075.2.20! raeburn  2499:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'}); 
1.586     raeburn  2500:     my $result;
1.1075.2.20! raeburn  2501:     if (!$authnum) {
        !          2502:         $result = &mt('Under your current role you are not permitted to change login settings for this user');
1.586     raeburn  2503:     } else {
                   2504:         $result = '<label>'.&mt('[_1] Do not change login data',
                   2505:                   '<input type="radio" name="login" value="nochange" '.
                   2506:                   'checked="checked" onclick="'.
1.281     albertel 2507:             "javascript:changed_radio('nochange',$in{'formname'});".'" />').
                   2508: 	    '</label>';
1.586     raeburn  2509:     }
1.32      matthew  2510:     return $result;
                   2511: }
                   2512: 
1.591     raeburn  2513: sub authform_kerberos {
1.32      matthew  2514:     my %in = (
                   2515:               formname => 'document.cu',
                   2516:               kerb_def_dom => 'MSU.EDU',
1.80      albertel 2517:               kerb_def_auth => 'krb4',
1.32      matthew  2518:               @_,
                   2519:               );
1.586     raeburn  2520:     my ($check4,$check5,$krbcheck,$krbarg,$krbver,$result,$authtype,
                   2521:         $autharg,$jscall);
1.1075.2.20! raeburn  2522:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.80      albertel 2523:     if ($in{'kerb_def_auth'} eq 'krb5') {
1.772     bisitz   2524:        $check5 = ' checked="checked"';
1.80      albertel 2525:     } else {
1.772     bisitz   2526:        $check4 = ' checked="checked"';
1.80      albertel 2527:     }
1.165     raeburn  2528:     $krbarg = $in{'kerb_def_dom'};
1.591     raeburn  2529:     if (defined($in{'curr_authtype'})) {
                   2530:         if ($in{'curr_authtype'} eq 'krb') {
1.772     bisitz   2531:             $krbcheck = ' checked="checked"';
1.623     raeburn  2532:             if (defined($in{'mode'})) {
                   2533:                 if ($in{'mode'} eq 'modifyuser') {
                   2534:                     $krbcheck = '';
                   2535:                 }
                   2536:             }
1.591     raeburn  2537:             if (defined($in{'curr_kerb_ver'})) {
                   2538:                 if ($in{'curr_krb_ver'} eq '5') {
1.772     bisitz   2539:                     $check5 = ' checked="checked"';
1.591     raeburn  2540:                     $check4 = '';
                   2541:                 } else {
1.772     bisitz   2542:                     $check4 = ' checked="checked"';
1.591     raeburn  2543:                     $check5 = '';
                   2544:                 }
1.586     raeburn  2545:             }
1.591     raeburn  2546:             if (defined($in{'curr_autharg'})) {
1.165     raeburn  2547:                 $krbarg = $in{'curr_autharg'};
                   2548:             }
1.586     raeburn  2549:             if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
1.591     raeburn  2550:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2551:                     $result = 
                   2552:     &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
                   2553:         $in{'curr_autharg'},$krbver);
                   2554:                 } else {
                   2555:                     $result =
                   2556:     &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
                   2557:                 }
                   2558:                 return $result; 
                   2559:             }
                   2560:         }
                   2561:     } else {
                   2562:         if ($authnum == 1) {
1.784     bisitz   2563:             $authtype = '<input type="hidden" name="login" value="krb" />';
1.165     raeburn  2564:         }
                   2565:     }
1.586     raeburn  2566:     if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
                   2567:         return;
1.587     raeburn  2568:     } elsif ($authtype eq '') {
1.591     raeburn  2569:         if (defined($in{'mode'})) {
1.587     raeburn  2570:             if ($in{'mode'} eq 'modifycourse') {
                   2571:                 if ($authnum == 1) {
1.1075.2.20! raeburn  2572:                     $authtype = '<input type="radio" name="login" value="krb" />';
1.587     raeburn  2573:                 }
                   2574:             }
                   2575:         }
1.586     raeburn  2576:     }
                   2577:     $jscall = "javascript:changed_radio('krb',$in{'formname'});";
                   2578:     if ($authtype eq '') {
                   2579:         $authtype = '<input type="radio" name="login" value="krb" '.
                   2580:                     'onclick="'.$jscall.'" onchange="'.$jscall.'"'.
                   2581:                     $krbcheck.' />';
                   2582:     }
                   2583:     if (($can_assign{'krb4'} && $can_assign{'krb5'}) ||
1.1075.2.20! raeburn  2584:         ($can_assign{'krb4'} && !$can_assign{'krb5'} &&
1.586     raeburn  2585:          $in{'curr_authtype'} eq 'krb5') ||
1.1075.2.20! raeburn  2586:         (!$can_assign{'krb4'} && $can_assign{'krb5'} &&
1.586     raeburn  2587:          $in{'curr_authtype'} eq 'krb4')) {
                   2588:         $result .= &mt
1.144     matthew  2589:         ('[_1] Kerberos authenticated with domain [_2] '.
1.281     albertel 2590:          '[_3] Version 4 [_4] Version 5 [_5]',
1.586     raeburn  2591:          '<label>'.$authtype,
1.281     albertel 2592:          '</label><input type="text" size="10" name="krbarg" '.
1.165     raeburn  2593:              'value="'.$krbarg.'" '.
1.144     matthew  2594:              'onchange="'.$jscall.'" />',
1.281     albertel 2595:          '<label><input type="radio" name="krbver" value="4" '.$check4.' />',
                   2596:          '</label><label><input type="radio" name="krbver" value="5" '.$check5.' />',
                   2597: 	 '</label>');
1.586     raeburn  2598:     } elsif ($can_assign{'krb4'}) {
                   2599:         $result .= &mt
                   2600:         ('[_1] Kerberos authenticated with domain [_2] '.
                   2601:          '[_3] Version 4 [_4]',
                   2602:          '<label>'.$authtype,
                   2603:          '</label><input type="text" size="10" name="krbarg" '.
                   2604:              'value="'.$krbarg.'" '.
                   2605:              'onchange="'.$jscall.'" />',
                   2606:          '<label><input type="hidden" name="krbver" value="4" />',
                   2607:          '</label>');
                   2608:     } elsif ($can_assign{'krb5'}) {
                   2609:         $result .= &mt
                   2610:         ('[_1] Kerberos authenticated with domain [_2] '.
                   2611:          '[_3] Version 5 [_4]',
                   2612:          '<label>'.$authtype,
                   2613:          '</label><input type="text" size="10" name="krbarg" '.
                   2614:              'value="'.$krbarg.'" '.
                   2615:              'onchange="'.$jscall.'" />',
                   2616:          '<label><input type="hidden" name="krbver" value="5" />',
                   2617:          '</label>');
                   2618:     }
1.32      matthew  2619:     return $result;
                   2620: }
                   2621: 
1.1075.2.20! raeburn  2622: sub authform_internal {
1.586     raeburn  2623:     my %in = (
1.32      matthew  2624:                 formname => 'document.cu',
                   2625:                 kerb_def_dom => 'MSU.EDU',
                   2626:                 @_,
                   2627:                 );
1.586     raeburn  2628:     my ($intcheck,$intarg,$result,$authtype,$autharg,$jscall);
1.1075.2.20! raeburn  2629:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.591     raeburn  2630:     if (defined($in{'curr_authtype'})) {
                   2631:         if ($in{'curr_authtype'} eq 'int') {
1.586     raeburn  2632:             if ($can_assign{'int'}) {
1.772     bisitz   2633:                 $intcheck = 'checked="checked" ';
1.623     raeburn  2634:                 if (defined($in{'mode'})) {
                   2635:                     if ($in{'mode'} eq 'modifyuser') {
                   2636:                         $intcheck = '';
                   2637:                     }
                   2638:                 }
1.591     raeburn  2639:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2640:                     $intarg = $in{'curr_autharg'};
                   2641:                 }
                   2642:             } else {
                   2643:                 $result = &mt('Currently internally authenticated.');
                   2644:                 return $result;
1.165     raeburn  2645:             }
                   2646:         }
1.586     raeburn  2647:     } else {
                   2648:         if ($authnum == 1) {
1.784     bisitz   2649:             $authtype = '<input type="hidden" name="login" value="int" />';
1.586     raeburn  2650:         }
                   2651:     }
                   2652:     if (!$can_assign{'int'}) {
                   2653:         return;
1.587     raeburn  2654:     } elsif ($authtype eq '') {
1.591     raeburn  2655:         if (defined($in{'mode'})) {
1.587     raeburn  2656:             if ($in{'mode'} eq 'modifycourse') {
                   2657:                 if ($authnum == 1) {
1.1075.2.20! raeburn  2658:                     $authtype = '<input type="radio" name="login" value="int" />';
1.587     raeburn  2659:                 }
                   2660:             }
                   2661:         }
1.165     raeburn  2662:     }
1.586     raeburn  2663:     $jscall = "javascript:changed_radio('int',$in{'formname'});";
                   2664:     if ($authtype eq '') {
                   2665:         $authtype = '<input type="radio" name="login" value="int" '.$intcheck.
                   2666:                     ' onchange="'.$jscall.'" onclick="'.$jscall.'" />';
                   2667:     }
1.605     bisitz   2668:     $autharg = '<input type="password" size="10" name="intarg" value="'.
1.586     raeburn  2669:                $intarg.'" onchange="'.$jscall.'" />';
                   2670:     $result = &mt
1.144     matthew  2671:         ('[_1] Internally authenticated (with initial password [_2])',
1.586     raeburn  2672:          '<label>'.$authtype,'</label>'.$autharg);
1.824     bisitz   2673:     $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  2674:     return $result;
                   2675: }
                   2676: 
1.1075.2.20! raeburn  2677: sub authform_local {
1.32      matthew  2678:     my %in = (
                   2679:               formname => 'document.cu',
                   2680:               kerb_def_dom => 'MSU.EDU',
                   2681:               @_,
                   2682:               );
1.586     raeburn  2683:     my ($loccheck,$locarg,$result,$authtype,$autharg,$jscall);
1.1075.2.20! raeburn  2684:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.591     raeburn  2685:     if (defined($in{'curr_authtype'})) {
                   2686:         if ($in{'curr_authtype'} eq 'loc') {
1.586     raeburn  2687:             if ($can_assign{'loc'}) {
1.772     bisitz   2688:                 $loccheck = 'checked="checked" ';
1.623     raeburn  2689:                 if (defined($in{'mode'})) {
                   2690:                     if ($in{'mode'} eq 'modifyuser') {
                   2691:                         $loccheck = '';
                   2692:                     }
                   2693:                 }
1.591     raeburn  2694:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2695:                     $locarg = $in{'curr_autharg'};
                   2696:                 }
                   2697:             } else {
                   2698:                 $result = &mt('Currently using local (institutional) authentication.');
                   2699:                 return $result;
1.165     raeburn  2700:             }
                   2701:         }
1.586     raeburn  2702:     } else {
                   2703:         if ($authnum == 1) {
1.784     bisitz   2704:             $authtype = '<input type="hidden" name="login" value="loc" />';
1.586     raeburn  2705:         }
                   2706:     }
                   2707:     if (!$can_assign{'loc'}) {
                   2708:         return;
1.587     raeburn  2709:     } elsif ($authtype eq '') {
1.591     raeburn  2710:         if (defined($in{'mode'})) {
1.587     raeburn  2711:             if ($in{'mode'} eq 'modifycourse') {
                   2712:                 if ($authnum == 1) {
1.1075.2.20! raeburn  2713:                     $authtype = '<input type="radio" name="login" value="loc" />';
1.587     raeburn  2714:                 }
                   2715:             }
                   2716:         }
1.165     raeburn  2717:     }
1.586     raeburn  2718:     $jscall = "javascript:changed_radio('loc',$in{'formname'});";
                   2719:     if ($authtype eq '') {
                   2720:         $authtype = '<input type="radio" name="login" value="loc" '.
                   2721:                     $loccheck.' onchange="'.$jscall.'" onclick="'.
                   2722:                     $jscall.'" />';
                   2723:     }
                   2724:     $autharg = '<input type="text" size="10" name="locarg" value="'.
                   2725:                $locarg.'" onchange="'.$jscall.'" />';
                   2726:     $result = &mt('[_1] Local Authentication with argument [_2]',
                   2727:                   '<label>'.$authtype,'</label>'.$autharg);
1.32      matthew  2728:     return $result;
                   2729: }
                   2730: 
1.1075.2.20! raeburn  2731: sub authform_filesystem {
1.32      matthew  2732:     my %in = (
                   2733:               formname => 'document.cu',
                   2734:               kerb_def_dom => 'MSU.EDU',
                   2735:               @_,
                   2736:               );
1.586     raeburn  2737:     my ($fsyscheck,$result,$authtype,$autharg,$jscall);
1.1075.2.20! raeburn  2738:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.591     raeburn  2739:     if (defined($in{'curr_authtype'})) {
                   2740:         if ($in{'curr_authtype'} eq 'fsys') {
1.586     raeburn  2741:             if ($can_assign{'fsys'}) {
1.772     bisitz   2742:                 $fsyscheck = 'checked="checked" ';
1.623     raeburn  2743:                 if (defined($in{'mode'})) {
                   2744:                     if ($in{'mode'} eq 'modifyuser') {
                   2745:                         $fsyscheck = '';
                   2746:                     }
                   2747:                 }
1.586     raeburn  2748:             } else {
                   2749:                 $result = &mt('Currently Filesystem Authenticated.');
                   2750:                 return $result;
                   2751:             }           
                   2752:         }
                   2753:     } else {
                   2754:         if ($authnum == 1) {
1.784     bisitz   2755:             $authtype = '<input type="hidden" name="login" value="fsys" />';
1.586     raeburn  2756:         }
                   2757:     }
                   2758:     if (!$can_assign{'fsys'}) {
                   2759:         return;
1.587     raeburn  2760:     } elsif ($authtype eq '') {
1.591     raeburn  2761:         if (defined($in{'mode'})) {
1.587     raeburn  2762:             if ($in{'mode'} eq 'modifycourse') {
                   2763:                 if ($authnum == 1) {
1.1075.2.20! raeburn  2764:                     $authtype = '<input type="radio" name="login" value="fsys" />';
1.587     raeburn  2765:                 }
                   2766:             }
                   2767:         }
1.586     raeburn  2768:     }
                   2769:     $jscall = "javascript:changed_radio('fsys',$in{'formname'});";
                   2770:     if ($authtype eq '') {
                   2771:         $authtype = '<input type="radio" name="login" value="fsys" '.
                   2772:                     $fsyscheck.' onchange="'.$jscall.'" onclick="'.
                   2773:                     $jscall.'" />';
                   2774:     }
                   2775:     $autharg = '<input type="text" size="10" name="fsysarg" value=""'.
                   2776:                ' onchange="'.$jscall.'" />';
                   2777:     $result = &mt
1.144     matthew  2778:         ('[_1] Filesystem Authenticated (with initial password [_2])',
1.281     albertel 2779:          '<label><input type="radio" name="login" value="fsys" '.
1.586     raeburn  2780:          $fsyscheck.'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1.605     bisitz   2781:          '</label><input type="password" size="10" name="fsysarg" value="" '.
1.144     matthew  2782:                   'onchange="'.$jscall.'" />');
1.32      matthew  2783:     return $result;
                   2784: }
                   2785: 
1.586     raeburn  2786: sub get_assignable_auth {
                   2787:     my ($dom) = @_;
                   2788:     if ($dom eq '') {
                   2789:         $dom = $env{'request.role.domain'};
                   2790:     }
                   2791:     my %can_assign = (
                   2792:                           krb4 => 1,
                   2793:                           krb5 => 1,
                   2794:                           int  => 1,
                   2795:                           loc  => 1,
                   2796:                      );
                   2797:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2798:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   2799:         if (ref($domconfig{'usercreation'}{'authtypes'}) eq 'HASH') {
                   2800:             my $authhash = $domconfig{'usercreation'}{'authtypes'};
                   2801:             my $context;
                   2802:             if ($env{'request.role'} =~ /^au/) {
                   2803:                 $context = 'author';
                   2804:             } elsif ($env{'request.role'} =~ /^dc/) {
                   2805:                 $context = 'domain';
                   2806:             } elsif ($env{'request.course.id'}) {
                   2807:                 $context = 'course';
                   2808:             }
                   2809:             if ($context) {
                   2810:                 if (ref($authhash->{$context}) eq 'HASH') {
                   2811:                    %can_assign = %{$authhash->{$context}}; 
                   2812:                 }
                   2813:             }
                   2814:         }
                   2815:     }
                   2816:     my $authnum = 0;
                   2817:     foreach my $key (keys(%can_assign)) {
                   2818:         if ($can_assign{$key}) {
                   2819:             $authnum ++;
                   2820:         }
                   2821:     }
                   2822:     if ($can_assign{'krb4'} && $can_assign{'krb5'}) {
                   2823:         $authnum --;
                   2824:     }
                   2825:     return ($authnum,%can_assign);
                   2826: }
                   2827: 
1.80      albertel 2828: ###############################################################
                   2829: ##    Get Kerberos Defaults for Domain                 ##
                   2830: ###############################################################
                   2831: ##
                   2832: ## Returns default kerberos version and an associated argument
                   2833: ## as listed in file domain.tab. If not listed, provides
                   2834: ## appropriate default domain and kerberos version.
                   2835: ##
                   2836: #-------------------------------------------
                   2837: 
                   2838: =pod
                   2839: 
1.648     raeburn  2840: =item * &get_kerberos_defaults()
1.80      albertel 2841: 
                   2842: get_kerberos_defaults($target_domain) returns the default kerberos
1.641     raeburn  2843: version and domain. If not found, it defaults to version 4 and the 
                   2844: domain of the server.
1.80      albertel 2845: 
1.648     raeburn  2846: =over 4
                   2847: 
1.80      albertel 2848: ($def_version, $def_krb_domain) = &get_kerberos_defaults($target_domain);
                   2849: 
1.648     raeburn  2850: =back
                   2851: 
                   2852: =back
                   2853: 
1.80      albertel 2854: =cut
                   2855: 
                   2856: #-------------------------------------------
                   2857: sub get_kerberos_defaults {
                   2858:     my $domain=shift;
1.641     raeburn  2859:     my ($krbdef,$krbdefdom);
                   2860:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                   2861:     if (($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) {
                   2862:         $krbdef = $domdefaults{'auth_def'};
                   2863:         $krbdefdom = $domdefaults{'auth_arg_def'};
                   2864:     } else {
1.80      albertel 2865:         $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
                   2866:         my $krbdefdom=$1;
                   2867:         $krbdefdom=~tr/a-z/A-Z/;
                   2868:         $krbdef = "krb4";
                   2869:     }
                   2870:     return ($krbdef,$krbdefdom);
                   2871: }
1.112     bowersj2 2872: 
1.32      matthew  2873: 
1.46      matthew  2874: ###############################################################
                   2875: ##                Thesaurus Functions                        ##
                   2876: ###############################################################
1.20      www      2877: 
1.46      matthew  2878: =pod
1.20      www      2879: 
1.112     bowersj2 2880: =head1 Thesaurus Functions
                   2881: 
                   2882: =over 4
                   2883: 
1.648     raeburn  2884: =item * &initialize_keywords()
1.46      matthew  2885: 
                   2886: Initializes the package variable %Keywords if it is empty.  Uses the
                   2887: package variable $thesaurus_db_file.
                   2888: 
                   2889: =cut
                   2890: 
                   2891: ###################################################
                   2892: 
                   2893: sub initialize_keywords {
                   2894:     return 1 if (scalar keys(%Keywords));
                   2895:     # If we are here, %Keywords is empty, so fill it up
                   2896:     #   Make sure the file we need exists...
                   2897:     if (! -e $thesaurus_db_file) {
                   2898:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file".
                   2899:                                  " failed because it does not exist");
                   2900:         return 0;
                   2901:     }
                   2902:     #   Set up the hash as a database
                   2903:     my %thesaurus_db;
                   2904:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 2905:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  2906:         &Apache::lonnet::logthis("Could not tie \%thesaurus_db to ".
                   2907:                                  $thesaurus_db_file);
                   2908:         return 0;
                   2909:     } 
                   2910:     #  Get the average number of appearances of a word.
                   2911:     my $avecount = $thesaurus_db{'average.count'};
                   2912:     #  Put keywords (those that appear > average) into %Keywords
                   2913:     while (my ($word,$data)=each (%thesaurus_db)) {
                   2914:         my ($count,undef) = split /:/,$data;
                   2915:         $Keywords{$word}++ if ($count > $avecount);
                   2916:     }
                   2917:     untie %thesaurus_db;
                   2918:     # Remove special values from %Keywords.
1.356     albertel 2919:     foreach my $value ('total.count','average.count') {
                   2920:         delete($Keywords{$value}) if (exists($Keywords{$value}));
1.586     raeburn  2921:   }
1.46      matthew  2922:     return 1;
                   2923: }
                   2924: 
                   2925: ###################################################
                   2926: 
                   2927: =pod
                   2928: 
1.648     raeburn  2929: =item * &keyword($word)
1.46      matthew  2930: 
                   2931: Returns true if $word is a keyword.  A keyword is a word that appears more 
                   2932: than the average number of times in the thesaurus database.  Calls 
                   2933: &initialize_keywords
                   2934: 
                   2935: =cut
                   2936: 
                   2937: ###################################################
1.20      www      2938: 
                   2939: sub keyword {
1.46      matthew  2940:     return if (!&initialize_keywords());
                   2941:     my $word=lc(shift());
                   2942:     $word=~s/\W//g;
                   2943:     return exists($Keywords{$word});
1.20      www      2944: }
1.46      matthew  2945: 
                   2946: ###############################################################
                   2947: 
                   2948: =pod 
1.20      www      2949: 
1.648     raeburn  2950: =item * &get_related_words()
1.46      matthew  2951: 
1.160     matthew  2952: Look up a word in the thesaurus.  Takes a scalar argument and returns
1.46      matthew  2953: an array of words.  If the keyword is not in the thesaurus, an empty array
                   2954: will be returned.  The order of the words returned is determined by the
                   2955: database which holds them.
                   2956: 
                   2957: Uses global $thesaurus_db_file.
                   2958: 
1.1057    foxr     2959: 
1.46      matthew  2960: =cut
                   2961: 
                   2962: ###############################################################
                   2963: sub get_related_words {
                   2964:     my $keyword = shift;
                   2965:     my %thesaurus_db;
                   2966:     if (! -e $thesaurus_db_file) {
                   2967:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file ".
                   2968:                                  "failed because the file does not exist");
                   2969:         return ();
                   2970:     }
                   2971:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 2972:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  2973:         return ();
                   2974:     } 
                   2975:     my @Words=();
1.429     www      2976:     my $count=0;
1.46      matthew  2977:     if (exists($thesaurus_db{$keyword})) {
1.356     albertel 2978: 	# The first element is the number of times
                   2979: 	# the word appears.  We do not need it now.
1.429     www      2980: 	my (undef,@RelatedWords) = (split(/:/,$thesaurus_db{$keyword}));
                   2981: 	my (undef,$mostfrequentcount)=split(/\,/,$RelatedWords[0]);
                   2982: 	my $threshold=$mostfrequentcount/10;
                   2983:         foreach my $possibleword (@RelatedWords) {
                   2984:             my ($word,$wordcount)=split(/\,/,$possibleword);
                   2985:             if ($wordcount>$threshold) {
                   2986: 		push(@Words,$word);
                   2987:                 $count++;
                   2988:                 if ($count>10) { last; }
                   2989: 	    }
1.20      www      2990:         }
                   2991:     }
1.46      matthew  2992:     untie %thesaurus_db;
                   2993:     return @Words;
1.14      harris41 2994: }
1.46      matthew  2995: 
1.112     bowersj2 2996: =pod
                   2997: 
                   2998: =back
                   2999: 
                   3000: =cut
1.61      www      3001: 
                   3002: # -------------------------------------------------------------- Plaintext name
1.81      albertel 3003: =pod
                   3004: 
1.112     bowersj2 3005: =head1 User Name Functions
                   3006: 
                   3007: =over 4
                   3008: 
1.648     raeburn  3009: =item * &plainname($uname,$udom,$first)
1.81      albertel 3010: 
1.112     bowersj2 3011: Takes a users logon name and returns it as a string in
1.226     albertel 3012: "first middle last generation" form 
                   3013: if $first is set to 'lastname' then it returns it as
                   3014: 'lastname generation, firstname middlename' if their is a lastname
1.81      albertel 3015: 
                   3016: =cut
1.61      www      3017: 
1.295     www      3018: 
1.81      albertel 3019: ###############################################################
1.61      www      3020: sub plainname {
1.226     albertel 3021:     my ($uname,$udom,$first)=@_;
1.537     albertel 3022:     return if (!defined($uname) || !defined($udom));
1.295     www      3023:     my %names=&getnames($uname,$udom);
1.226     albertel 3024:     my $name=&Apache::lonnet::format_name($names{'firstname'},
                   3025: 					  $names{'middlename'},
                   3026: 					  $names{'lastname'},
                   3027: 					  $names{'generation'},$first);
                   3028:     $name=~s/^\s+//;
1.62      www      3029:     $name=~s/\s+$//;
                   3030:     $name=~s/\s+/ /g;
1.353     albertel 3031:     if ($name !~ /\S/) { $name=$uname.':'.$udom; }
1.62      www      3032:     return $name;
1.61      www      3033: }
1.66      www      3034: 
                   3035: # -------------------------------------------------------------------- Nickname
1.81      albertel 3036: =pod
                   3037: 
1.648     raeburn  3038: =item * &nickname($uname,$udom)
1.81      albertel 3039: 
                   3040: Gets a users name and returns it as a string as
                   3041: 
                   3042: "&quot;nickname&quot;"
1.66      www      3043: 
1.81      albertel 3044: if the user has a nickname or
                   3045: 
                   3046: "first middle last generation"
                   3047: 
                   3048: if the user does not
                   3049: 
                   3050: =cut
1.66      www      3051: 
                   3052: sub nickname {
                   3053:     my ($uname,$udom)=@_;
1.537     albertel 3054:     return if (!defined($uname) || !defined($udom));
1.295     www      3055:     my %names=&getnames($uname,$udom);
1.68      albertel 3056:     my $name=$names{'nickname'};
1.66      www      3057:     if ($name) {
                   3058:        $name='&quot;'.$name.'&quot;'; 
                   3059:     } else {
                   3060:        $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
                   3061: 	     $names{'lastname'}.' '.$names{'generation'};
                   3062:        $name=~s/\s+$//;
                   3063:        $name=~s/\s+/ /g;
                   3064:     }
                   3065:     return $name;
                   3066: }
                   3067: 
1.295     www      3068: sub getnames {
                   3069:     my ($uname,$udom)=@_;
1.537     albertel 3070:     return if (!defined($uname) || !defined($udom));
1.433     albertel 3071:     if ($udom eq 'public' && $uname eq 'public') {
                   3072: 	return ('lastname' => &mt('Public'));
                   3073:     }
1.295     www      3074:     my $id=$uname.':'.$udom;
                   3075:     my ($names,$cached)=&Apache::lonnet::is_cached_new('namescache',$id);
                   3076:     if ($cached) {
                   3077: 	return %{$names};
                   3078:     } else {
                   3079: 	my %loadnames=&Apache::lonnet::get('environment',
                   3080:                     ['firstname','middlename','lastname','generation','nickname'],
                   3081: 					 $udom,$uname);
                   3082: 	&Apache::lonnet::do_cache_new('namescache',$id,\%loadnames);
                   3083: 	return %loadnames;
                   3084:     }
                   3085: }
1.61      www      3086: 
1.542     raeburn  3087: # -------------------------------------------------------------------- getemails
1.648     raeburn  3088: 
1.542     raeburn  3089: =pod
                   3090: 
1.648     raeburn  3091: =item * &getemails($uname,$udom)
1.542     raeburn  3092: 
                   3093: Gets a user's email information and returns it as a hash with keys:
                   3094: notification, critnotification, permanentemail
                   3095: 
                   3096: For notification and critnotification, values are comma-separated lists 
1.648     raeburn  3097: of e-mail addresses; for permanentemail, value is a single e-mail address.
1.542     raeburn  3098:  
1.648     raeburn  3099: 
1.542     raeburn  3100: =cut
                   3101: 
1.648     raeburn  3102: 
1.466     albertel 3103: sub getemails {
                   3104:     my ($uname,$udom)=@_;
                   3105:     if ($udom eq 'public' && $uname eq 'public') {
                   3106: 	return;
                   3107:     }
1.467     www      3108:     if (!$udom) { $udom=$env{'user.domain'}; }
                   3109:     if (!$uname) { $uname=$env{'user.name'}; }
1.466     albertel 3110:     my $id=$uname.':'.$udom;
                   3111:     my ($names,$cached)=&Apache::lonnet::is_cached_new('emailscache',$id);
                   3112:     if ($cached) {
                   3113: 	return %{$names};
                   3114:     } else {
                   3115: 	my %loadnames=&Apache::lonnet::get('environment',
                   3116:                     			   ['notification','critnotification',
                   3117: 					    'permanentemail'],
                   3118: 					   $udom,$uname);
                   3119: 	&Apache::lonnet::do_cache_new('emailscache',$id,\%loadnames);
                   3120: 	return %loadnames;
                   3121:     }
                   3122: }
                   3123: 
1.551     albertel 3124: sub flush_email_cache {
                   3125:     my ($uname,$udom)=@_;
                   3126:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   3127:     if (!$uname) { $uname=$env{'user.name'};   }
                   3128:     return if ($udom eq 'public' && $uname eq 'public');
                   3129:     my $id=$uname.':'.$udom;
                   3130:     &Apache::lonnet::devalidate_cache_new('emailscache',$id);
                   3131: }
                   3132: 
1.728     raeburn  3133: # -------------------------------------------------------------------- getlangs
                   3134: 
                   3135: =pod
                   3136: 
                   3137: =item * &getlangs($uname,$udom)
                   3138: 
                   3139: Gets a user's language preference and returns it as a hash with key:
                   3140: language.
                   3141: 
                   3142: =cut
                   3143: 
                   3144: 
                   3145: sub getlangs {
                   3146:     my ($uname,$udom) = @_;
                   3147:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   3148:     if (!$uname) { $uname=$env{'user.name'};   }
                   3149:     my $id=$uname.':'.$udom;
                   3150:     my ($langs,$cached)=&Apache::lonnet::is_cached_new('userlangs',$id);
                   3151:     if ($cached) {
                   3152:         return %{$langs};
                   3153:     } else {
                   3154:         my %loadlangs=&Apache::lonnet::get('environment',['languages'],
                   3155:                                            $udom,$uname);
                   3156:         &Apache::lonnet::do_cache_new('userlangs',$id,\%loadlangs);
                   3157:         return %loadlangs;
                   3158:     }
                   3159: }
                   3160: 
                   3161: sub flush_langs_cache {
                   3162:     my ($uname,$udom)=@_;
                   3163:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   3164:     if (!$uname) { $uname=$env{'user.name'};   }
                   3165:     return if ($udom eq 'public' && $uname eq 'public');
                   3166:     my $id=$uname.':'.$udom;
                   3167:     &Apache::lonnet::devalidate_cache_new('userlangs',$id);
                   3168: }
                   3169: 
1.61      www      3170: # ------------------------------------------------------------------ Screenname
1.81      albertel 3171: 
                   3172: =pod
                   3173: 
1.648     raeburn  3174: =item * &screenname($uname,$udom)
1.81      albertel 3175: 
                   3176: Gets a users screenname and returns it as a string
                   3177: 
                   3178: =cut
1.61      www      3179: 
                   3180: sub screenname {
                   3181:     my ($uname,$udom)=@_;
1.258     albertel 3182:     if ($uname eq $env{'user.name'} &&
                   3183: 	$udom eq $env{'user.domain'}) {return $env{'environment.screenname'};}
1.212     albertel 3184:     my %names=&Apache::lonnet::get('environment',['screenname'],$udom,$uname);
1.68      albertel 3185:     return $names{'screenname'};
1.62      www      3186: }
                   3187: 
1.212     albertel 3188: 
1.802     bisitz   3189: # ------------------------------------------------------------- Confirm Wrapper
                   3190: =pod
                   3191: 
                   3192: =item confirmwrapper
                   3193: 
                   3194: Wrap messages about completion of operation in box
                   3195: 
                   3196: =cut
                   3197: 
                   3198: sub confirmwrapper {
                   3199:     my ($message)=@_;
                   3200:     if ($message) {
                   3201:         return "\n".'<div class="LC_confirm_box">'."\n"
                   3202:                .$message."\n"
                   3203:                .'</div>'."\n";
                   3204:     } else {
                   3205:         return $message;
                   3206:     }
                   3207: }
                   3208: 
1.62      www      3209: # ------------------------------------------------------------- Message Wrapper
                   3210: 
                   3211: sub messagewrapper {
1.369     www      3212:     my ($link,$username,$domain,$subject,$text)=@_;
1.62      www      3213:     return 
1.441     albertel 3214:         '<a href="/adm/email?compose=individual&amp;'.
                   3215:         'recname='.$username.'&amp;recdom='.$domain.
                   3216: 	'&amp;subject='.&escape($subject).'&amp;text='.&escape($text).'" '.
1.200     matthew  3217:         'title="'.&mt('Send message').'">'.$link.'</a>';
1.74      www      3218: }
1.802     bisitz   3219: 
1.74      www      3220: # --------------------------------------------------------------- Notes Wrapper
                   3221: 
                   3222: sub noteswrapper {
                   3223:     my ($link,$un,$do)=@_;
                   3224:     return 
1.896     amueller 3225: "<a href='/adm/email?recordftf=retrieve&amp;recname=$un&amp;recdom=$do'>$link</a>";
1.62      www      3226: }
1.802     bisitz   3227: 
1.62      www      3228: # ------------------------------------------------------------- Aboutme Wrapper
                   3229: 
                   3230: sub aboutmewrapper {
1.1070    raeburn  3231:     my ($link,$username,$domain,$target,$class)=@_;
1.447     raeburn  3232:     if (!defined($username)  && !defined($domain)) {
                   3233:         return;
                   3234:     }
1.1075.2.15  raeburn  3235:     return '<a href="/adm/'.$domain.'/'.$username.'/aboutme"'.
1.1070    raeburn  3236: 	($target?' target="'.$target.'"':'').($class?' class="'.$class.'"':'').' title="'.&mt("View this user's personal information page").'">'.$link.'</a>';
1.62      www      3237: }
                   3238: 
                   3239: # ------------------------------------------------------------ Syllabus Wrapper
                   3240: 
                   3241: sub syllabuswrapper {
1.707     bisitz   3242:     my ($linktext,$coursedir,$domain)=@_;
1.208     matthew  3243:     return qq{<a href="/public/$domain/$coursedir/syllabus">$linktext</a>};
1.61      www      3244: }
1.14      harris41 3245: 
1.802     bisitz   3246: # -----------------------------------------------------------------------------
                   3247: 
1.208     matthew  3248: sub track_student_link {
1.887     raeburn  3249:     my ($linktext,$sname,$sdom,$target,$start,$only_body) = @_;
1.268     albertel 3250:     my $link ="/adm/trackstudent?";
1.208     matthew  3251:     my $title = 'View recent activity';
                   3252:     if (defined($sname) && $sname !~ /^\s*$/ &&
                   3253:         defined($sdom)  && $sdom  !~ /^\s*$/) {
1.268     albertel 3254:         $link .= "selected_student=$sname:$sdom";
1.208     matthew  3255:         $title .= ' of this student';
1.268     albertel 3256:     } 
1.208     matthew  3257:     if (defined($target) && $target !~ /^\s*$/) {
                   3258:         $target = qq{target="$target"};
                   3259:     } else {
                   3260:         $target = '';
                   3261:     }
1.268     albertel 3262:     if ($start) { $link.='&amp;start='.$start; }
1.887     raeburn  3263:     if ($only_body) { $link .= '&amp;only_body=1'; }
1.554     albertel 3264:     $title = &mt($title);
                   3265:     $linktext = &mt($linktext);
1.448     albertel 3266:     return qq{<a href="$link" title="$title" $target>$linktext</a>}.
                   3267: 	&help_open_topic('View_recent_activity');
1.208     matthew  3268: }
                   3269: 
1.781     raeburn  3270: sub slot_reservations_link {
                   3271:     my ($linktext,$sname,$sdom,$target) = @_;
                   3272:     my $link ="/adm/slotrequest?command=showresv&amp;origin=aboutme";
                   3273:     my $title = 'View slot reservation history';
                   3274:     if (defined($sname) && $sname !~ /^\s*$/ &&
                   3275:         defined($sdom)  && $sdom  !~ /^\s*$/) {
                   3276:         $link .= "&amp;uname=$sname&amp;udom=$sdom";
                   3277:         $title .= ' of this student';
                   3278:     }
                   3279:     if (defined($target) && $target !~ /^\s*$/) {
                   3280:         $target = qq{target="$target"};
                   3281:     } else {
                   3282:         $target = '';
                   3283:     }
                   3284:     $title = &mt($title);
                   3285:     $linktext = &mt($linktext);
                   3286:     return qq{<a href="$link" title="$title" $target>$linktext</a>};
                   3287: # FIXME uncomment when help item created: &help_open_topic('Slot_Reservation_History');
                   3288: 
                   3289: }
                   3290: 
1.508     www      3291: # ===================================================== Display a student photo
                   3292: 
                   3293: 
1.509     albertel 3294: sub student_image_tag {
1.508     www      3295:     my ($domain,$user)=@_;
                   3296:     my $imgsrc=&Apache::lonnet::studentphoto($domain,$user,'jpg');
                   3297:     if (($imgsrc) && ($imgsrc ne '/adm/lonKaputt/lonlogo_broken.gif')) {
                   3298: 	return '<img src="'.$imgsrc.'" align="right" />';
                   3299:     } else {
                   3300: 	return '';
                   3301:     }
                   3302: }
                   3303: 
1.112     bowersj2 3304: =pod
                   3305: 
                   3306: =back
                   3307: 
                   3308: =head1 Access .tab File Data
                   3309: 
                   3310: =over 4
                   3311: 
1.648     raeburn  3312: =item * &languageids() 
1.112     bowersj2 3313: 
                   3314: returns list of all language ids
                   3315: 
                   3316: =cut
                   3317: 
1.14      harris41 3318: sub languageids {
1.16      harris41 3319:     return sort(keys(%language));
1.14      harris41 3320: }
                   3321: 
1.112     bowersj2 3322: =pod
                   3323: 
1.648     raeburn  3324: =item * &languagedescription() 
1.112     bowersj2 3325: 
                   3326: returns description of a specified language id
                   3327: 
                   3328: =cut
                   3329: 
1.14      harris41 3330: sub languagedescription {
1.125     www      3331:     my $code=shift;
                   3332:     return  ($supported_language{$code}?'* ':'').
                   3333:             $language{$code}.
1.126     www      3334: 	    ($supported_language{$code}?' ('.&mt('interface available').')':'');
1.145     www      3335: }
                   3336: 
1.1048    foxr     3337: =pod
                   3338: 
                   3339: =item * &plainlanguagedescription
                   3340: 
                   3341: Returns both the plain language description (e.g. 'Creoles and Pidgins, English-based (Other)')
                   3342: and the language character encoding (e.g. ISO) separated by a ' - ' string.
                   3343: 
                   3344: =cut
                   3345: 
1.145     www      3346: sub plainlanguagedescription {
                   3347:     my $code=shift;
                   3348:     return $language{$code};
                   3349: }
                   3350: 
1.1048    foxr     3351: =pod
                   3352: 
                   3353: =item * &supportedlanguagecode
                   3354: 
                   3355: Returns the supported language code (e.g. sptutf maps to pt) given a language
                   3356: code.
                   3357: 
                   3358: =cut
                   3359: 
1.145     www      3360: sub supportedlanguagecode {
                   3361:     my $code=shift;
                   3362:     return $supported_language{$code};
1.97      www      3363: }
                   3364: 
1.112     bowersj2 3365: =pod
                   3366: 
1.1048    foxr     3367: =item * &latexlanguage()
                   3368: 
                   3369: Given a language key code returns the correspondnig language to use
                   3370: to select the correct hyphenation on LaTeX printouts.  This is undef if there
                   3371: is no supported hyphenation for the language code.
                   3372: 
                   3373: =cut
                   3374: 
                   3375: sub latexlanguage {
                   3376:     my $code = shift;
                   3377:     return $latex_language{$code};
                   3378: }
                   3379: 
                   3380: =pod
                   3381: 
                   3382: =item * &latexhyphenation()
                   3383: 
                   3384: Same as above but what's supplied is the language as it might be stored
                   3385: in the metadata.
                   3386: 
                   3387: =cut
                   3388: 
                   3389: sub latexhyphenation {
                   3390:     my $key = shift;
                   3391:     return $latex_language_bykey{$key};
                   3392: }
                   3393: 
                   3394: =pod
                   3395: 
1.648     raeburn  3396: =item * &copyrightids() 
1.112     bowersj2 3397: 
                   3398: returns list of all copyrights
                   3399: 
                   3400: =cut
                   3401: 
                   3402: sub copyrightids {
                   3403:     return sort(keys(%cprtag));
                   3404: }
                   3405: 
                   3406: =pod
                   3407: 
1.648     raeburn  3408: =item * &copyrightdescription() 
1.112     bowersj2 3409: 
                   3410: returns description of a specified copyright id
                   3411: 
                   3412: =cut
                   3413: 
                   3414: sub copyrightdescription {
1.166     www      3415:     return &mt($cprtag{shift(@_)});
1.112     bowersj2 3416: }
1.197     matthew  3417: 
                   3418: =pod
                   3419: 
1.648     raeburn  3420: =item * &source_copyrightids() 
1.192     taceyjo1 3421: 
                   3422: returns list of all source copyrights
                   3423: 
                   3424: =cut
                   3425: 
                   3426: sub source_copyrightids {
                   3427:     return sort(keys(%scprtag));
                   3428: }
                   3429: 
                   3430: =pod
                   3431: 
1.648     raeburn  3432: =item * &source_copyrightdescription() 
1.192     taceyjo1 3433: 
                   3434: returns description of a specified source copyright id
                   3435: 
                   3436: =cut
                   3437: 
                   3438: sub source_copyrightdescription {
                   3439:     return &mt($scprtag{shift(@_)});
                   3440: }
1.112     bowersj2 3441: 
                   3442: =pod
                   3443: 
1.648     raeburn  3444: =item * &filecategories() 
1.112     bowersj2 3445: 
                   3446: returns list of all file categories
                   3447: 
                   3448: =cut
                   3449: 
                   3450: sub filecategories {
                   3451:     return sort(keys(%category_extensions));
                   3452: }
                   3453: 
                   3454: =pod
                   3455: 
1.648     raeburn  3456: =item * &filecategorytypes() 
1.112     bowersj2 3457: 
                   3458: returns list of file types belonging to a given file
                   3459: category
                   3460: 
                   3461: =cut
                   3462: 
                   3463: sub filecategorytypes {
1.356     albertel 3464:     my ($cat) = @_;
                   3465:     return @{$category_extensions{lc($cat)}};
1.112     bowersj2 3466: }
                   3467: 
                   3468: =pod
                   3469: 
1.648     raeburn  3470: =item * &fileembstyle() 
1.112     bowersj2 3471: 
                   3472: returns embedding style for a specified file type
                   3473: 
                   3474: =cut
                   3475: 
                   3476: sub fileembstyle {
                   3477:     return $fe{lc(shift(@_))};
1.169     www      3478: }
                   3479: 
1.351     www      3480: sub filemimetype {
                   3481:     return $fm{lc(shift(@_))};
                   3482: }
                   3483: 
1.169     www      3484: 
                   3485: sub filecategoryselect {
                   3486:     my ($name,$value)=@_;
1.189     matthew  3487:     return &select_form($value,$name,
1.970     raeburn  3488:                         {'' => &mt('Any category'), map { $_,$_ } sort(keys(%category_extensions))});
1.112     bowersj2 3489: }
                   3490: 
                   3491: =pod
                   3492: 
1.648     raeburn  3493: =item * &filedescription() 
1.112     bowersj2 3494: 
                   3495: returns description for a specified file type
                   3496: 
                   3497: =cut
                   3498: 
                   3499: sub filedescription {
1.188     matthew  3500:     my $file_description = $fd{lc(shift())};
                   3501:     $file_description =~ s:([\[\]]):~$1:g;
                   3502:     return &mt($file_description);
1.112     bowersj2 3503: }
                   3504: 
                   3505: =pod
                   3506: 
1.648     raeburn  3507: =item * &filedescriptionex() 
1.112     bowersj2 3508: 
                   3509: returns description for a specified file type with
                   3510: extra formatting
                   3511: 
                   3512: =cut
                   3513: 
                   3514: sub filedescriptionex {
                   3515:     my $ex=shift;
1.188     matthew  3516:     my $file_description = $fd{lc($ex)};
                   3517:     $file_description =~ s:([\[\]]):~$1:g;
                   3518:     return '.'.$ex.' '.&mt($file_description);
1.112     bowersj2 3519: }
                   3520: 
                   3521: # End of .tab access
                   3522: =pod
                   3523: 
                   3524: =back
                   3525: 
                   3526: =cut
                   3527: 
                   3528: # ------------------------------------------------------------------ File Types
                   3529: sub fileextensions {
                   3530:     return sort(keys(%fe));
                   3531: }
                   3532: 
1.97      www      3533: # ----------------------------------------------------------- Display Languages
                   3534: # returns a hash with all desired display languages
                   3535: #
                   3536: 
                   3537: sub display_languages {
                   3538:     my %languages=();
1.695     raeburn  3539:     foreach my $lang (&Apache::lonlocal::preferred_languages()) {
1.356     albertel 3540: 	$languages{$lang}=1;
1.97      www      3541:     }
                   3542:     &get_unprocessed_cgi($ENV{'QUERY_STRING'},['displaylanguage']);
1.258     albertel 3543:     if ($env{'form.displaylanguage'}) {
1.356     albertel 3544: 	foreach my $lang (split(/\s*(\,|\;|\:)\s*/,$env{'form.displaylanguage'})) {
                   3545: 	    $languages{$lang}=1;
1.97      www      3546:         }
                   3547:     }
                   3548:     return %languages;
1.14      harris41 3549: }
                   3550: 
1.582     albertel 3551: sub languages {
                   3552:     my ($possible_langs) = @_;
1.695     raeburn  3553:     my @preferred_langs = &Apache::lonlocal::preferred_languages();
1.582     albertel 3554:     if (!ref($possible_langs)) {
                   3555: 	if( wantarray ) {
                   3556: 	    return @preferred_langs;
                   3557: 	} else {
                   3558: 	    return $preferred_langs[0];
                   3559: 	}
                   3560:     }
                   3561:     my %possibilities = map { $_ => 1 } (@$possible_langs);
                   3562:     my @preferred_possibilities;
                   3563:     foreach my $preferred_lang (@preferred_langs) {
                   3564: 	if (exists($possibilities{$preferred_lang})) {
                   3565: 	    push(@preferred_possibilities, $preferred_lang);
                   3566: 	}
                   3567:     }
                   3568:     if( wantarray ) {
                   3569: 	return @preferred_possibilities;
                   3570:     }
                   3571:     return $preferred_possibilities[0];
                   3572: }
                   3573: 
1.742     raeburn  3574: sub user_lang {
                   3575:     my ($touname,$toudom,$fromcid) = @_;
                   3576:     my @userlangs;
                   3577:     if (($fromcid ne '') && ($env{'course.'.$fromcid.'.languages'} ne '')) {
                   3578:         @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
                   3579:                     $env{'course.'.$fromcid.'.languages'}));
                   3580:     } else {
                   3581:         my %langhash = &getlangs($touname,$toudom);
                   3582:         if ($langhash{'languages'} ne '') {
                   3583:             @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
                   3584:         } else {
                   3585:             my %domdefs = &Apache::lonnet::get_domain_defaults($toudom);
                   3586:             if ($domdefs{'lang_def'} ne '') {
                   3587:                 @userlangs = ($domdefs{'lang_def'});
                   3588:             }
                   3589:         }
                   3590:     }
                   3591:     my @languages=&Apache::lonlocal::get_genlanguages(@userlangs);
                   3592:     my $user_lh = Apache::localize->get_handle(@languages);
                   3593:     return $user_lh;
                   3594: }
                   3595: 
                   3596: 
1.112     bowersj2 3597: ###############################################################
                   3598: ##               Student Answer Attempts                     ##
                   3599: ###############################################################
                   3600: 
                   3601: =pod
                   3602: 
                   3603: =head1 Alternate Problem Views
                   3604: 
                   3605: =over 4
                   3606: 
1.648     raeburn  3607: =item * &get_previous_attempt($symb, $username, $domain, $course,
1.112     bowersj2 3608:     $getattempt, $regexp, $gradesub)
                   3609: 
                   3610: Return string with previous attempt on problem. Arguments:
                   3611: 
                   3612: =over 4
                   3613: 
                   3614: =item * $symb: Problem, including path
                   3615: 
                   3616: =item * $username: username of the desired student
                   3617: 
                   3618: =item * $domain: domain of the desired student
1.14      harris41 3619: 
1.112     bowersj2 3620: =item * $course: Course ID
1.14      harris41 3621: 
1.112     bowersj2 3622: =item * $getattempt: Leave blank for all attempts, otherwise put
                   3623:     something
1.14      harris41 3624: 
1.112     bowersj2 3625: =item * $regexp: if string matches this regexp, the string will be
                   3626:     sent to $gradesub
1.14      harris41 3627: 
1.112     bowersj2 3628: =item * $gradesub: routine that processes the string if it matches $regexp
1.14      harris41 3629: 
1.112     bowersj2 3630: =back
1.14      harris41 3631: 
1.112     bowersj2 3632: The output string is a table containing all desired attempts, if any.
1.16      harris41 3633: 
1.112     bowersj2 3634: =cut
1.1       albertel 3635: 
                   3636: sub get_previous_attempt {
1.43      ng       3637:   my ($symb,$username,$domain,$course,$getattempt,$regexp,$gradesub)=@_;
1.1       albertel 3638:   my $prevattempts='';
1.43      ng       3639:   no strict 'refs';
1.1       albertel 3640:   if ($symb) {
1.3       albertel 3641:     my (%returnhash)=
                   3642:       &Apache::lonnet::restore($symb,$course,$domain,$username);
1.1       albertel 3643:     if ($returnhash{'version'}) {
                   3644:       my %lasthash=();
                   3645:       my $version;
                   3646:       for ($version=1;$version<=$returnhash{'version'};$version++) {
1.356     albertel 3647:         foreach my $key (sort(split(/\:/,$returnhash{$version.':keys'}))) {
                   3648: 	  $lasthash{$key}=$returnhash{$version.':'.$key};
1.19      harris41 3649:         }
1.1       albertel 3650:       }
1.596     albertel 3651:       $prevattempts=&start_data_table().&start_data_table_header_row();
                   3652:       $prevattempts.='<th>'.&mt('History').'</th>';
1.978     raeburn  3653:       my (%typeparts,%lasthidden);
1.945     raeburn  3654:       my $showsurv=&Apache::lonnet::allowed('vas',$env{'request.course.id'});
1.356     albertel 3655:       foreach my $key (sort(keys(%lasthash))) {
                   3656: 	my ($ign,@parts) = split(/\./,$key);
1.41      ng       3657: 	if ($#parts > 0) {
1.31      albertel 3658: 	  my $data=$parts[-1];
1.989     raeburn  3659:           next if ($data eq 'foilorder');
1.31      albertel 3660: 	  pop(@parts);
1.1010    www      3661:           $prevattempts.='<th>'.&mt('Part ').join('.',@parts).'<br />'.$data.'&nbsp;</th>';
1.945     raeburn  3662:           if ($data eq 'type') {
                   3663:               unless ($showsurv) {
                   3664:                   my $id = join(',',@parts);
                   3665:                   $typeparts{$ign.'.'.$id} = $lasthash{$key};
1.978     raeburn  3666:                   if (($lasthash{$key} eq 'anonsurvey') || ($lasthash{$key} eq 'anonsurveycred')) {
                   3667:                       $lasthidden{$ign.'.'.$id} = 1;
                   3668:                   }
1.945     raeburn  3669:               }
1.1010    www      3670:           } 
1.31      albertel 3671: 	} else {
1.41      ng       3672: 	  if ($#parts == 0) {
                   3673: 	    $prevattempts.='<th>'.$parts[0].'</th>';
                   3674: 	  } else {
                   3675: 	    $prevattempts.='<th>'.$ign.'</th>';
                   3676: 	  }
1.31      albertel 3677: 	}
1.16      harris41 3678:       }
1.596     albertel 3679:       $prevattempts.=&end_data_table_header_row();
1.40      ng       3680:       if ($getattempt eq '') {
                   3681: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
1.945     raeburn  3682:             my @hidden;
                   3683:             if (%typeparts) {
                   3684:                 foreach my $id (keys(%typeparts)) {
                   3685:                     if (($returnhash{$version.':'.$id.'.type'} eq 'anonsurvey') || ($returnhash{$version.':'.$id.'.type'} eq 'anonsurveycred')) {
                   3686:                         push(@hidden,$id);
                   3687:                     }
                   3688:                 }
                   3689:             }
                   3690:             $prevattempts.=&start_data_table_row().
                   3691:                            '<td>'.&mt('Transaction [_1]',$version).'</td>';
                   3692:             if (@hidden) {
                   3693:                 foreach my $key (sort(keys(%lasthash))) {
1.989     raeburn  3694:                     next if ($key =~ /\.foilorder$/);
1.945     raeburn  3695:                     my $hide;
                   3696:                     foreach my $id (@hidden) {
                   3697:                         if ($key =~ /^\Q$id\E/) {
                   3698:                             $hide = 1;
                   3699:                             last;
                   3700:                         }
                   3701:                     }
                   3702:                     if ($hide) {
                   3703:                         my ($id,$data) = ($key =~ /^(.+)\.([^.]+)$/);
                   3704:                         if (($data eq 'award') || ($data eq 'awarddetail')) {
                   3705:                             my $value = &format_previous_attempt_value($key,
                   3706:                                              $returnhash{$version.':'.$key});
                   3707:                             $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3708:                         } else {
                   3709:                             $prevattempts.='<td>&nbsp;</td>';
                   3710:                         }
                   3711:                     } else {
                   3712:                         if ($key =~ /\./) {
                   3713:                             my $value = &format_previous_attempt_value($key,
                   3714:                                               $returnhash{$version.':'.$key});
                   3715:                             $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3716:                         } else {
                   3717:                             $prevattempts.='<td>&nbsp;</td>';
                   3718:                         }
                   3719:                     }
                   3720:                 }
                   3721:             } else {
                   3722: 	        foreach my $key (sort(keys(%lasthash))) {
1.989     raeburn  3723:                     next if ($key =~ /\.foilorder$/);
1.945     raeburn  3724: 		    my $value = &format_previous_attempt_value($key,
                   3725: 			            $returnhash{$version.':'.$key});
                   3726: 		    $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3727: 	        }
                   3728:             }
                   3729: 	    $prevattempts.=&end_data_table_row();
1.40      ng       3730: 	 }
1.1       albertel 3731:       }
1.945     raeburn  3732:       my @currhidden = keys(%lasthidden);
1.596     albertel 3733:       $prevattempts.=&start_data_table_row().'<td>'.&mt('Current').'</td>';
1.356     albertel 3734:       foreach my $key (sort(keys(%lasthash))) {
1.989     raeburn  3735:           next if ($key =~ /\.foilorder$/);
1.945     raeburn  3736:           if (%typeparts) {
                   3737:               my $hidden;
                   3738:               foreach my $id (@currhidden) {
                   3739:                   if ($key =~ /^\Q$id\E/) {
                   3740:                       $hidden = 1;
                   3741:                       last;
                   3742:                   }
                   3743:               }
                   3744:               if ($hidden) {
                   3745:                   my ($id,$data) = ($key =~ /^(.+)\.([^.]+)$/);
                   3746:                   if (($data eq 'award') || ($data eq 'awarddetail')) {
                   3747:                       my $value = &format_previous_attempt_value($key,$lasthash{$key});
                   3748:                       if ($key =~/$regexp$/ && (defined &$gradesub)) {
                   3749:                           $value = &$gradesub($value);
                   3750:                       }
                   3751:                       $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3752:                   } else {
                   3753:                       $prevattempts.='<td>&nbsp;</td>';
                   3754:                   }
                   3755:               } else {
                   3756:                   my $value = &format_previous_attempt_value($key,$lasthash{$key});
                   3757:                   if ($key =~/$regexp$/ && (defined &$gradesub)) {
                   3758:                       $value = &$gradesub($value);
                   3759:                   }
                   3760:                   $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3761:               }
                   3762:           } else {
                   3763: 	      my $value = &format_previous_attempt_value($key,$lasthash{$key});
                   3764: 	      if ($key =~/$regexp$/ && (defined &$gradesub)) {
                   3765:                   $value = &$gradesub($value);
                   3766:               }
                   3767: 	      $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3768:           }
1.16      harris41 3769:       }
1.596     albertel 3770:       $prevattempts.= &end_data_table_row().&end_data_table();
1.1       albertel 3771:     } else {
1.596     albertel 3772:       $prevattempts=
                   3773: 	  &start_data_table().&start_data_table_row().
                   3774: 	  '<td>'.&mt('Nothing submitted - no attempts.').'</td>'.
                   3775: 	  &end_data_table_row().&end_data_table();
1.1       albertel 3776:     }
                   3777:   } else {
1.596     albertel 3778:     $prevattempts=
                   3779: 	  &start_data_table().&start_data_table_row().
                   3780: 	  '<td>'.&mt('No data.').'</td>'.
                   3781: 	  &end_data_table_row().&end_data_table();
1.1       albertel 3782:   }
1.10      albertel 3783: }
                   3784: 
1.581     albertel 3785: sub format_previous_attempt_value {
                   3786:     my ($key,$value) = @_;
1.1011    www      3787:     if (($key =~ /timestamp/) || ($key=~/duedate/)) {
1.581     albertel 3788: 	$value = &Apache::lonlocal::locallocaltime($value);
                   3789:     } elsif (ref($value) eq 'ARRAY') {
                   3790: 	$value = '('.join(', ', @{ $value }).')';
1.988     raeburn  3791:     } elsif ($key =~ /answerstring$/) {
                   3792:         my %answers = &Apache::lonnet::str2hash($value);
                   3793:         my @anskeys = sort(keys(%answers));
                   3794:         if (@anskeys == 1) {
                   3795:             my $answer = $answers{$anskeys[0]};
1.1001    raeburn  3796:             if ($answer =~ m{\0}) {
                   3797:                 $answer =~ s{\0}{,}g;
1.988     raeburn  3798:             }
                   3799:             my $tag_internal_answer_name = 'INTERNAL';
                   3800:             if ($anskeys[0] eq $tag_internal_answer_name) {
                   3801:                 $value = $answer; 
                   3802:             } else {
                   3803:                 $value = $anskeys[0].'='.$answer;
                   3804:             }
                   3805:         } else {
                   3806:             foreach my $ans (@anskeys) {
                   3807:                 my $answer = $answers{$ans};
1.1001    raeburn  3808:                 if ($answer =~ m{\0}) {
                   3809:                     $answer =~ s{\0}{,}g;
1.988     raeburn  3810:                 }
                   3811:                 $value .=  $ans.'='.$answer.'<br />';;
                   3812:             } 
                   3813:         }
1.581     albertel 3814:     } else {
                   3815: 	$value = &unescape($value);
                   3816:     }
                   3817:     return $value;
                   3818: }
                   3819: 
                   3820: 
1.107     albertel 3821: sub relative_to_absolute {
                   3822:     my ($url,$output)=@_;
                   3823:     my $parser=HTML::TokeParser->new(\$output);
                   3824:     my $token;
                   3825:     my $thisdir=$url;
                   3826:     my @rlinks=();
                   3827:     while ($token=$parser->get_token) {
                   3828: 	if ($token->[0] eq 'S') {
                   3829: 	    if ($token->[1] eq 'a') {
                   3830: 		if ($token->[2]->{'href'}) {
                   3831: 		    $rlinks[$#rlinks+1]=$token->[2]->{'href'};
                   3832: 		}
                   3833: 	    } elsif ($token->[1] eq 'img' || $token->[1] eq 'embed' ) {
                   3834: 		$rlinks[$#rlinks+1]=$token->[2]->{'src'};
                   3835: 	    } elsif ($token->[1] eq 'base') {
                   3836: 		$thisdir=$token->[2]->{'href'};
                   3837: 	    }
                   3838: 	}
                   3839:     }
                   3840:     $thisdir=~s-/[^/]*$--;
1.356     albertel 3841:     foreach my $link (@rlinks) {
1.726     raeburn  3842: 	unless (($link=~/^https?\:\/\//i) ||
1.356     albertel 3843: 		($link=~/^\//) ||
                   3844: 		($link=~/^javascript:/i) ||
                   3845: 		($link=~/^mailto:/i) ||
                   3846: 		($link=~/^\#/)) {
                   3847: 	    my $newlocation=&Apache::lonnet::hreflocation($thisdir,$link);
                   3848: 	    $output=~s/(\"|\'|\=\s*)\Q$link\E(\"|\'|\s|\>)/$1$newlocation$2/;
1.107     albertel 3849: 	}
                   3850:     }
                   3851: # -------------------------------------------------- Deal with Applet codebases
                   3852:     $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
                   3853:     return $output;
                   3854: }
                   3855: 
1.112     bowersj2 3856: =pod
                   3857: 
1.648     raeburn  3858: =item * &get_student_view()
1.112     bowersj2 3859: 
                   3860: show a snapshot of what student was looking at
                   3861: 
                   3862: =cut
                   3863: 
1.10      albertel 3864: sub get_student_view {
1.186     albertel 3865:   my ($symb,$username,$domain,$courseid,$target,$moreenv) = @_;
1.114     www      3866:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 3867:   my (%form);
1.10      albertel 3868:   my @elements=('symb','courseid','domain','username');
                   3869:   foreach my $element (@elements) {
1.186     albertel 3870:       $form{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 3871:   }
1.186     albertel 3872:   if (defined($moreenv)) {
                   3873:       %form=(%form,%{$moreenv});
                   3874:   }
1.236     albertel 3875:   if (defined($target)) { $form{'grade_target'} = $target; }
1.107     albertel 3876:   $feedurl=&Apache::lonnet::clutter($feedurl);
1.650     www      3877:   my ($userview,$response)=&Apache::lonnet::ssi_body($feedurl,%form);
1.11      albertel 3878:   $userview=~s/\<body[^\>]*\>//gi;
                   3879:   $userview=~s/\<\/body\>//gi;
                   3880:   $userview=~s/\<html\>//gi;
                   3881:   $userview=~s/\<\/html\>//gi;
                   3882:   $userview=~s/\<head\>//gi;
                   3883:   $userview=~s/\<\/head\>//gi;
                   3884:   $userview=~s/action\s*\=/would_be_action\=/gi;
1.107     albertel 3885:   $userview=&relative_to_absolute($feedurl,$userview);
1.650     www      3886:   if (wantarray) {
                   3887:      return ($userview,$response);
                   3888:   } else {
                   3889:      return $userview;
                   3890:   }
                   3891: }
                   3892: 
                   3893: sub get_student_view_with_retries {
                   3894:   my ($symb,$retries,$username,$domain,$courseid,$target,$moreenv) = @_;
                   3895: 
                   3896:     my $ok = 0;                 # True if we got a good response.
                   3897:     my $content;
                   3898:     my $response;
                   3899: 
                   3900:     # Try to get the student_view done. within the retries count:
                   3901:     
                   3902:     do {
                   3903:          ($content, $response) = &get_student_view($symb,$username,$domain,$courseid,$target,$moreenv);
                   3904:          $ok      = $response->is_success;
                   3905:          if (!$ok) {
                   3906:             &Apache::lonnet::logthis("Failed get_student_view_with_retries on $symb: ".$response->is_success.', '.$response->code.', '.$response->message);
                   3907:          }
                   3908:          $retries--;
                   3909:     } while (!$ok && ($retries > 0));
                   3910:     
                   3911:     if (!$ok) {
                   3912:        $content = '';          # On error return an empty content.
                   3913:     }
1.651     www      3914:     if (wantarray) {
                   3915:        return ($content, $response);
                   3916:     } else {
                   3917:        return $content;
                   3918:     }
1.11      albertel 3919: }
                   3920: 
1.112     bowersj2 3921: =pod
                   3922: 
1.648     raeburn  3923: =item * &get_student_answers() 
1.112     bowersj2 3924: 
                   3925: show a snapshot of how student was answering problem
                   3926: 
                   3927: =cut
                   3928: 
1.11      albertel 3929: sub get_student_answers {
1.100     sakharuk 3930:   my ($symb,$username,$domain,$courseid,%form) = @_;
1.114     www      3931:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 3932:   my (%moreenv);
1.11      albertel 3933:   my @elements=('symb','courseid','domain','username');
                   3934:   foreach my $element (@elements) {
1.186     albertel 3935:     $moreenv{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 3936:   }
1.186     albertel 3937:   $moreenv{'grade_target'}='answer';
                   3938:   %moreenv=(%form,%moreenv);
1.497     raeburn  3939:   $feedurl = &Apache::lonnet::clutter($feedurl);
                   3940:   my $userview=&Apache::lonnet::ssi($feedurl,%moreenv);
1.10      albertel 3941:   return $userview;
1.1       albertel 3942: }
1.116     albertel 3943: 
                   3944: =pod
                   3945: 
                   3946: =item * &submlink()
                   3947: 
1.242     albertel 3948: Inputs: $text $uname $udom $symb $target
1.116     albertel 3949: 
                   3950: Returns: A link to grades.pm such as to see the SUBM view of a student
                   3951: 
                   3952: =cut
                   3953: 
                   3954: ###############################################
                   3955: sub submlink {
1.242     albertel 3956:     my ($text,$uname,$udom,$symb,$target)=@_;
1.116     albertel 3957:     if (!($uname && $udom)) {
                   3958: 	(my $cursymb, my $courseid,$udom,$uname)=
1.463     albertel 3959: 	    &Apache::lonnet::whichuser($symb);
1.116     albertel 3960: 	if (!$symb) { $symb=$cursymb; }
                   3961:     }
1.254     matthew  3962:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      3963:     $symb=&escape($symb);
1.960     bisitz   3964:     if ($target) { $target=" target=\"$target\""; }
                   3965:     return
                   3966:         '<a href="/adm/grades?command=submission'.
                   3967:         '&amp;symb='.$symb.
                   3968:         '&amp;student='.$uname.
                   3969:         '&amp;userdom='.$udom.'"'.
                   3970:         $target.'>'.$text.'</a>';
1.242     albertel 3971: }
                   3972: ##############################################
                   3973: 
                   3974: =pod
                   3975: 
                   3976: =item * &pgrdlink()
                   3977: 
                   3978: Inputs: $text $uname $udom $symb $target
                   3979: 
                   3980: Returns: A link to grades.pm such as to see the PGRD view of a student
                   3981: 
                   3982: =cut
                   3983: 
                   3984: ###############################################
                   3985: sub pgrdlink {
                   3986:     my $link=&submlink(@_);
                   3987:     $link=~s/(&command=submission)/$1&showgrading=yes/;
                   3988:     return $link;
                   3989: }
                   3990: ##############################################
                   3991: 
                   3992: =pod
                   3993: 
                   3994: =item * &pprmlink()
                   3995: 
                   3996: Inputs: $text $uname $udom $symb $target
                   3997: 
                   3998: Returns: A link to parmset.pm such as to see the PPRM view of a
1.283     albertel 3999: student and a specific resource
1.242     albertel 4000: 
                   4001: =cut
                   4002: 
                   4003: ###############################################
                   4004: sub pprmlink {
                   4005:     my ($text,$uname,$udom,$symb,$target)=@_;
                   4006:     if (!($uname && $udom)) {
                   4007: 	(my $cursymb, my $courseid,$udom,$uname)=
1.463     albertel 4008: 	    &Apache::lonnet::whichuser($symb);
1.242     albertel 4009: 	if (!$symb) { $symb=$cursymb; }
                   4010:     }
1.254     matthew  4011:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      4012:     $symb=&escape($symb);
1.242     albertel 4013:     if ($target) { $target="target=\"$target\""; }
1.595     albertel 4014:     return '<a href="/adm/parmset?command=set&amp;'.
                   4015: 	'symb='.$symb.'&amp;uname='.$uname.
                   4016: 	'&amp;udom='.$udom.'" '.$target.'>'.$text.'</a>';
1.116     albertel 4017: }
                   4018: ##############################################
1.37      matthew  4019: 
1.112     bowersj2 4020: =pod
                   4021: 
                   4022: =back
                   4023: 
                   4024: =cut
                   4025: 
1.37      matthew  4026: ###############################################
1.51      www      4027: 
                   4028: 
                   4029: sub timehash {
1.687     raeburn  4030:     my ($thistime) = @_;
                   4031:     my $timezone = &Apache::lonlocal::gettimezone();
                   4032:     my $dt = DateTime->from_epoch(epoch => $thistime)
                   4033:                      ->set_time_zone($timezone);
                   4034:     my $wday = $dt->day_of_week();
                   4035:     if ($wday == 7) { $wday = 0; }
                   4036:     return ( 'second' => $dt->second(),
                   4037:              'minute' => $dt->minute(),
                   4038:              'hour'   => $dt->hour(),
                   4039:              'day'     => $dt->day_of_month(),
                   4040:              'month'   => $dt->month(),
                   4041:              'year'    => $dt->year(),
                   4042:              'weekday' => $wday,
                   4043:              'dayyear' => $dt->day_of_year(),
                   4044:              'dlsav'   => $dt->is_dst() );
1.51      www      4045: }
                   4046: 
1.370     www      4047: sub utc_string {
                   4048:     my ($date)=@_;
1.371     www      4049:     return strftime("%Y%m%dT%H%M%SZ",gmtime($date));
1.370     www      4050: }
                   4051: 
1.51      www      4052: sub maketime {
                   4053:     my %th=@_;
1.687     raeburn  4054:     my ($epoch_time,$timezone,$dt);
                   4055:     $timezone = &Apache::lonlocal::gettimezone();
                   4056:     eval {
                   4057:         $dt = DateTime->new( year   => $th{'year'},
                   4058:                              month  => $th{'month'},
                   4059:                              day    => $th{'day'},
                   4060:                              hour   => $th{'hour'},
                   4061:                              minute => $th{'minute'},
                   4062:                              second => $th{'second'},
                   4063:                              time_zone => $timezone,
                   4064:                          );
                   4065:     };
                   4066:     if (!$@) {
                   4067:         $epoch_time = $dt->epoch;
                   4068:         if ($epoch_time) {
                   4069:             return $epoch_time;
                   4070:         }
                   4071:     }
1.51      www      4072:     return POSIX::mktime(
                   4073:         ($th{'seconds'},$th{'minutes'},$th{'hours'},
1.210     www      4074:          $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,-1));
1.70      www      4075: }
                   4076: 
                   4077: #########################################
1.51      www      4078: 
                   4079: sub findallcourses {
1.482     raeburn  4080:     my ($roles,$uname,$udom) = @_;
1.355     albertel 4081:     my %roles;
                   4082:     if (ref($roles)) { %roles = map { $_ => 1 } @{$roles}; }
1.348     albertel 4083:     my %courses;
1.51      www      4084:     my $now=time;
1.482     raeburn  4085:     if (!defined($uname)) {
                   4086:         $uname = $env{'user.name'};
                   4087:     }
                   4088:     if (!defined($udom)) {
                   4089:         $udom = $env{'user.domain'};
                   4090:     }
                   4091:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.1073    raeburn  4092:         my %roleshash = &Apache::lonnet::dump('roles',$udom,$uname);
1.482     raeburn  4093:         if (!%roles) {
                   4094:             %roles = (
                   4095:                        cc => 1,
1.907     raeburn  4096:                        co => 1,
1.482     raeburn  4097:                        in => 1,
                   4098:                        ep => 1,
                   4099:                        ta => 1,
                   4100:                        cr => 1,
                   4101:                        st => 1,
                   4102:              );
                   4103:         }
                   4104:         foreach my $entry (keys(%roleshash)) {
                   4105:             my ($trole,$tend,$tstart) = split(/_/,$roleshash{$entry});
                   4106:             if ($trole =~ /^cr/) { 
                   4107:                 next if (!exists($roles{$trole}) && !exists($roles{'cr'}));
                   4108:             } else {
                   4109:                 next if (!exists($roles{$trole}));
                   4110:             }
                   4111:             if ($tend) {
                   4112:                 next if ($tend < $now);
                   4113:             }
                   4114:             if ($tstart) {
                   4115:                 next if ($tstart > $now);
                   4116:             }
1.1058    raeburn  4117:             my ($cdom,$cnum,$sec,$cnumpart,$secpart,$role);
1.482     raeburn  4118:             (undef,$cdom,$cnumpart,$secpart) = split(/\//,$entry);
1.1058    raeburn  4119:             my $value = $trole.'/'.$cdom.'/';
1.482     raeburn  4120:             if ($secpart eq '') {
                   4121:                 ($cnum,$role) = split(/_/,$cnumpart); 
                   4122:                 $sec = 'none';
1.1058    raeburn  4123:                 $value .= $cnum.'/';
1.482     raeburn  4124:             } else {
                   4125:                 $cnum = $cnumpart;
                   4126:                 ($sec,$role) = split(/_/,$secpart);
1.1058    raeburn  4127:                 $value .= $cnum.'/'.$sec;
                   4128:             }
                   4129:             if (ref($courses{$cdom.'_'.$cnum}{$sec}) eq 'ARRAY') {
                   4130:                 unless (grep(/^\Q$value\E$/,@{$courses{$cdom.'_'.$cnum}{$sec}})) {
                   4131:                     push(@{$courses{$cdom.'_'.$cnum}{$sec}},$value);
                   4132:                 }
                   4133:             } else {
                   4134:                 @{$courses{$cdom.'_'.$cnum}{$sec}} = ($value);
1.490     raeburn  4135:             }
1.482     raeburn  4136:         }
                   4137:     } else {
                   4138:         foreach my $key (keys(%env)) {
1.483     albertel 4139: 	    if ( $key=~m{^user\.role\.(\w+)\./($match_domain)/($match_courseid)/?(\w*)$} ||
                   4140:                  $key=~m{^user\.role\.(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_courseid)/?(\w*)$}) {
1.482     raeburn  4141: 	        my ($role,$cdom,$cnum,$sec) = ($1,$2,$3,$4);
                   4142: 	        next if ($role eq 'ca' || $role eq 'aa');
                   4143: 	        next if (%roles && !exists($roles{$role}));
                   4144: 	        my ($starttime,$endtime)=split(/\./,$env{$key});
                   4145:                 my $active=1;
                   4146:                 if ($starttime) {
                   4147: 		    if ($now<$starttime) { $active=0; }
                   4148:                 }
                   4149:                 if ($endtime) {
                   4150:                     if ($now>$endtime) { $active=0; }
                   4151:                 }
                   4152:                 if ($active) {
1.1058    raeburn  4153:                     my $value = $role.'/'.$cdom.'/'.$cnum.'/';
1.482     raeburn  4154:                     if ($sec eq '') {
                   4155:                         $sec = 'none';
1.1058    raeburn  4156:                     } else {
                   4157:                         $value .= $sec;
                   4158:                     }
                   4159:                     if (ref($courses{$cdom.'_'.$cnum}{$sec}) eq 'ARRAY') {
                   4160:                         unless (grep(/^\Q$value\E$/,@{$courses{$cdom.'_'.$cnum}{$sec}})) {
                   4161:                             push(@{$courses{$cdom.'_'.$cnum}{$sec}},$value);
                   4162:                         }
                   4163:                     } else {
                   4164:                         @{$courses{$cdom.'_'.$cnum}{$sec}} = ($value);
1.482     raeburn  4165:                     }
1.474     raeburn  4166:                 }
                   4167:             }
1.51      www      4168:         }
                   4169:     }
1.474     raeburn  4170:     return %courses;
1.51      www      4171: }
1.37      matthew  4172: 
1.54      www      4173: ###############################################
1.474     raeburn  4174: 
                   4175: sub blockcheck {
1.1062    raeburn  4176:     my ($setters,$activity,$uname,$udom,$url) = @_;
1.490     raeburn  4177: 
                   4178:     if (!defined($udom)) {
                   4179:         $udom = $env{'user.domain'};
                   4180:     }
                   4181:     if (!defined($uname)) {
                   4182:         $uname = $env{'user.name'};
                   4183:     }
                   4184: 
                   4185:     # If uname and udom are for a course, check for blocks in the course.
                   4186: 
                   4187:     if (&Apache::lonnet::is_course($udom,$uname)) {
1.1062    raeburn  4188:         my ($startblock,$endblock,$triggerblock) = 
                   4189:             &get_blocks($setters,$activity,$udom,$uname,$url);
                   4190:         return ($startblock,$endblock,$triggerblock);
1.490     raeburn  4191:     }
1.474     raeburn  4192: 
1.502     raeburn  4193:     my $startblock = 0;
                   4194:     my $endblock = 0;
1.1062    raeburn  4195:     my $triggerblock = '';
1.482     raeburn  4196:     my %live_courses = &findallcourses(undef,$uname,$udom);
1.474     raeburn  4197: 
1.490     raeburn  4198:     # If uname is for a user, and activity is course-specific, i.e.,
                   4199:     # boards, chat or groups, check for blocking in current course only.
1.474     raeburn  4200: 
1.490     raeburn  4201:     if (($activity eq 'boards' || $activity eq 'chat' ||
                   4202:          $activity eq 'groups') && ($env{'request.course.id'})) {
                   4203:         foreach my $key (keys(%live_courses)) {
                   4204:             if ($key ne $env{'request.course.id'}) {
                   4205:                 delete($live_courses{$key});
                   4206:             }
                   4207:         }
                   4208:     }
                   4209: 
                   4210:     my $otheruser = 0;
                   4211:     my %own_courses;
                   4212:     if ((($uname ne $env{'user.name'})) || ($udom ne $env{'user.domain'})) {
                   4213:         # Resource belongs to user other than current user.
                   4214:         $otheruser = 1;
                   4215:         # Gather courses for current user
                   4216:         %own_courses = 
                   4217:             &findallcourses(undef,$env{'user.name'},$env{'user.domain'});
                   4218:     }
                   4219: 
                   4220:     # Gather active course roles - course coordinator, instructor, 
                   4221:     # exam proctor, ta, student, or custom role.
1.474     raeburn  4222: 
                   4223:     foreach my $course (keys(%live_courses)) {
1.482     raeburn  4224:         my ($cdom,$cnum);
                   4225:         if ((defined($env{'course.'.$course.'.domain'})) && (defined($env{'course.'.$course.'.num'}))) {
                   4226:             $cdom = $env{'course.'.$course.'.domain'};
                   4227:             $cnum = $env{'course.'.$course.'.num'};
                   4228:         } else {
1.490     raeburn  4229:             ($cdom,$cnum) = split(/_/,$course); 
1.482     raeburn  4230:         }
                   4231:         my $no_ownblock = 0;
                   4232:         my $no_userblock = 0;
1.533     raeburn  4233:         if ($otheruser && $activity ne 'com') {
1.490     raeburn  4234:             # Check if current user has 'evb' priv for this
                   4235:             if (defined($own_courses{$course})) {
                   4236:                 foreach my $sec (keys(%{$own_courses{$course}})) {
                   4237:                     my $checkrole = 'cm./'.$cdom.'/'.$cnum;
                   4238:                     if ($sec ne 'none') {
                   4239:                         $checkrole .= '/'.$sec;
                   4240:                     }
                   4241:                     if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
                   4242:                         $no_ownblock = 1;
                   4243:                         last;
                   4244:                     }
                   4245:                 }
                   4246:             }
                   4247:             # if they have 'evb' priv and are currently not playing student
                   4248:             next if (($no_ownblock) &&
                   4249:                  ($env{'request.role'} !~ m{^st\./$cdom/$cnum}));
                   4250:         }
1.474     raeburn  4251:         foreach my $sec (keys(%{$live_courses{$course}})) {
1.482     raeburn  4252:             my $checkrole = 'cm./'.$cdom.'/'.$cnum;
1.474     raeburn  4253:             if ($sec ne 'none') {
1.482     raeburn  4254:                 $checkrole .= '/'.$sec;
1.474     raeburn  4255:             }
1.490     raeburn  4256:             if ($otheruser) {
                   4257:                 # Resource belongs to user other than current user.
                   4258:                 # Assemble privs for that user, and check for 'evb' priv.
1.1058    raeburn  4259:                 my (%allroles,%userroles);
                   4260:                 if (ref($live_courses{$course}{$sec}) eq 'ARRAY') {
                   4261:                     foreach my $entry (@{$live_courses{$course}{$sec}}) { 
                   4262:                         my ($trole,$tdom,$tnum,$tsec);
                   4263:                         if ($entry =~ /^cr/) {
                   4264:                             ($trole,$tdom,$tnum,$tsec) = 
                   4265:                                 ($entry =~ m|^(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_username)/?(\w*)$|);
                   4266:                         } else {
                   4267:                            ($trole,$tdom,$tnum,$tsec) = split(/\//,$entry);
                   4268:                         }
                   4269:                         my ($spec,$area,$trest);
                   4270:                         $area = '/'.$tdom.'/'.$tnum;
                   4271:                         $trest = $tnum;
                   4272:                         if ($tsec ne '') {
                   4273:                             $area .= '/'.$tsec;
                   4274:                             $trest .= '/'.$tsec;
                   4275:                         }
                   4276:                         $spec = $trole.'.'.$area;
                   4277:                         if ($trole =~ /^cr/) {
                   4278:                             &Apache::lonnet::custom_roleprivs(\%allroles,$trole,
                   4279:                                                               $tdom,$spec,$trest,$area);
                   4280:                         } else {
                   4281:                             &Apache::lonnet::standard_roleprivs(\%allroles,$trole,
                   4282:                                                                 $tdom,$spec,$trest,$area);
                   4283:                         }
                   4284:                     }
                   4285:                     my ($author,$adv) = &Apache::lonnet::set_userprivs(\%userroles,\%allroles);
                   4286:                     if ($userroles{'user.priv.'.$checkrole} =~ /evb\&([^\:]*)/) {
                   4287:                         if ($1) {
                   4288:                             $no_userblock = 1;
                   4289:                             last;
                   4290:                         }
1.486     raeburn  4291:                     }
                   4292:                 }
1.490     raeburn  4293:             } else {
                   4294:                 # Resource belongs to current user
                   4295:                 # Check for 'evb' priv via lonnet::allowed().
1.482     raeburn  4296:                 if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
                   4297:                     $no_ownblock = 1;
                   4298:                     last;
                   4299:                 }
1.474     raeburn  4300:             }
                   4301:         }
                   4302:         # if they have the evb priv and are currently not playing student
1.482     raeburn  4303:         next if (($no_ownblock) &&
1.491     albertel 4304:                  ($env{'request.role'} !~ m{^st\./\Q$cdom\E/\Q$cnum\E}));
1.482     raeburn  4305:         next if ($no_userblock);
1.474     raeburn  4306: 
1.866     kalberla 4307:         # Retrieve blocking times and identity of locker for course
1.490     raeburn  4308:         # of specified user, unless user has 'evb' privilege.
1.502     raeburn  4309:         
1.1062    raeburn  4310:         my ($start,$end,$trigger) = 
                   4311:             &get_blocks($setters,$activity,$cdom,$cnum,$url);
1.502     raeburn  4312:         if (($start != 0) && 
                   4313:             (($startblock == 0) || ($startblock > $start))) {
                   4314:             $startblock = $start;
1.1062    raeburn  4315:             if ($trigger ne '') {
                   4316:                 $triggerblock = $trigger;
                   4317:             }
1.502     raeburn  4318:         }
                   4319:         if (($end != 0)  &&
                   4320:             (($endblock == 0) || ($endblock < $end))) {
                   4321:             $endblock = $end;
1.1062    raeburn  4322:             if ($trigger ne '') {
                   4323:                 $triggerblock = $trigger;
                   4324:             }
1.502     raeburn  4325:         }
1.490     raeburn  4326:     }
1.1062    raeburn  4327:     return ($startblock,$endblock,$triggerblock);
1.490     raeburn  4328: }
                   4329: 
                   4330: sub get_blocks {
1.1062    raeburn  4331:     my ($setters,$activity,$cdom,$cnum,$url) = @_;
1.490     raeburn  4332:     my $startblock = 0;
                   4333:     my $endblock = 0;
1.1062    raeburn  4334:     my $triggerblock = '';
1.490     raeburn  4335:     my $course = $cdom.'_'.$cnum;
                   4336:     $setters->{$course} = {};
                   4337:     $setters->{$course}{'staff'} = [];
                   4338:     $setters->{$course}{'times'} = [];
1.1062    raeburn  4339:     $setters->{$course}{'triggers'} = [];
                   4340:     my (@blockers,%triggered);
                   4341:     my $now = time;
                   4342:     my %commblocks = &Apache::lonnet::get_comm_blocks($cdom,$cnum);
                   4343:     if ($activity eq 'docs') {
                   4344:         @blockers = &Apache::lonnet::has_comm_blocking('bre',undef,$url,\%commblocks);
                   4345:         foreach my $block (@blockers) {
                   4346:             if ($block =~ /^firstaccess____(.+)$/) {
                   4347:                 my $item = $1;
                   4348:                 my $type = 'map';
                   4349:                 my $timersymb = $item;
                   4350:                 if ($item eq 'course') {
                   4351:                     $type = 'course';
                   4352:                 } elsif ($item =~ /___\d+___/) {
                   4353:                     $type = 'resource';
                   4354:                 } else {
                   4355:                     $timersymb = &Apache::lonnet::symbread($item);
                   4356:                 }
                   4357:                 my $start = $env{'course.'.$cdom.'_'.$cnum.'.firstaccess.'.$timersymb};
                   4358:                 my $end = $start + $env{'course.'.$cdom.'_'.$cnum.'.timerinterval.'.$timersymb};
                   4359:                 $triggered{$block} = {
                   4360:                                        start => $start,
                   4361:                                        end   => $end,
                   4362:                                        type  => $type,
                   4363:                                      };
                   4364:             }
                   4365:         }
                   4366:     } else {
                   4367:         foreach my $block (keys(%commblocks)) {
                   4368:             if ($block =~ m/^(\d+)____(\d+)$/) { 
                   4369:                 my ($start,$end) = ($1,$2);
                   4370:                 if ($start <= time && $end >= time) {
                   4371:                     if (ref($commblocks{$block}) eq 'HASH') {
                   4372:                         if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   4373:                             if ($commblocks{$block}{'blocks'}{$activity} eq 'on') {
                   4374:                                 unless(grep(/^\Q$block\E$/,@blockers)) {
                   4375:                                     push(@blockers,$block);
                   4376:                                 }
                   4377:                             }
                   4378:                         }
                   4379:                     }
                   4380:                 }
                   4381:             } elsif ($block =~ /^firstaccess____(.+)$/) {
                   4382:                 my $item = $1;
                   4383:                 my $timersymb = $item; 
                   4384:                 my $type = 'map';
                   4385:                 if ($item eq 'course') {
                   4386:                     $type = 'course';
                   4387:                 } elsif ($item =~ /___\d+___/) {
                   4388:                     $type = 'resource';
                   4389:                 } else {
                   4390:                     $timersymb = &Apache::lonnet::symbread($item);
                   4391:                 }
                   4392:                 my $start = $env{'course.'.$cdom.'_'.$cnum.'.firstaccess.'.$timersymb};
                   4393:                 my $end = $start + $env{'course.'.$cdom.'_'.$cnum.'.timerinterval.'.$timersymb}; 
                   4394:                 if ($start && $end) {
                   4395:                     if (($start <= time) && ($end >= time)) {
                   4396:                         unless (grep(/^\Q$block\E$/,@blockers)) {
                   4397:                             push(@blockers,$block);
                   4398:                             $triggered{$block} = {
                   4399:                                                    start => $start,
                   4400:                                                    end   => $end,
                   4401:                                                    type  => $type,
                   4402:                                                  };
                   4403:                         }
                   4404:                     }
1.490     raeburn  4405:                 }
1.1062    raeburn  4406:             }
                   4407:         }
                   4408:     }
                   4409:     foreach my $blocker (@blockers) {
                   4410:         my ($staff_name,$staff_dom,$title,$blocks) =
                   4411:             &parse_block_record($commblocks{$blocker});
                   4412:         push(@{$$setters{$course}{'staff'}},[$staff_name,$staff_dom]);
                   4413:         my ($start,$end,$triggertype);
                   4414:         if ($blocker =~ m/^(\d+)____(\d+)$/) {
                   4415:             ($start,$end) = ($1,$2);
                   4416:         } elsif (ref($triggered{$blocker}) eq 'HASH') {
                   4417:             $start = $triggered{$blocker}{'start'};
                   4418:             $end = $triggered{$blocker}{'end'};
                   4419:             $triggertype = $triggered{$blocker}{'type'};
                   4420:         }
                   4421:         if ($start) {
                   4422:             push(@{$$setters{$course}{'times'}}, [$start,$end]);
                   4423:             if ($triggertype) {
                   4424:                 push(@{$$setters{$course}{'triggers'}},$triggertype);
                   4425:             } else {
                   4426:                 push(@{$$setters{$course}{'triggers'}},0);
                   4427:             }
                   4428:             if ( ($startblock == 0) || ($startblock > $start) ) {
                   4429:                 $startblock = $start;
                   4430:                 if ($triggertype) {
                   4431:                     $triggerblock = $blocker;
1.474     raeburn  4432:                 }
                   4433:             }
1.1062    raeburn  4434:             if ( ($endblock == 0) || ($endblock < $end) ) {
                   4435:                $endblock = $end;
                   4436:                if ($triggertype) {
                   4437:                    $triggerblock = $blocker;
                   4438:                }
                   4439:             }
1.474     raeburn  4440:         }
                   4441:     }
1.1062    raeburn  4442:     return ($startblock,$endblock,$triggerblock);
1.474     raeburn  4443: }
                   4444: 
                   4445: sub parse_block_record {
                   4446:     my ($record) = @_;
                   4447:     my ($setuname,$setudom,$title,$blocks);
                   4448:     if (ref($record) eq 'HASH') {
                   4449:         ($setuname,$setudom) = split(/:/,$record->{'setter'});
                   4450:         $title = &unescape($record->{'event'});
                   4451:         $blocks = $record->{'blocks'};
                   4452:     } else {
                   4453:         my @data = split(/:/,$record,3);
                   4454:         if (scalar(@data) eq 2) {
                   4455:             $title = $data[1];
                   4456:             ($setuname,$setudom) = split(/@/,$data[0]);
                   4457:         } else {
                   4458:             ($setuname,$setudom,$title) = @data;
                   4459:         }
                   4460:         $blocks = { 'com' => 'on' };
                   4461:     }
                   4462:     return ($setuname,$setudom,$title,$blocks);
                   4463: }
                   4464: 
1.854     kalberla 4465: sub blocking_status {
1.1062    raeburn  4466:     my ($activity,$uname,$udom,$url) = @_;
1.1061    raeburn  4467:     my %setters;
1.890     droeschl 4468: 
1.1061    raeburn  4469: # check for active blocking
1.1062    raeburn  4470:     my ($startblock,$endblock,$triggerblock) = 
                   4471:         &blockcheck(\%setters,$activity,$uname,$udom,$url);
                   4472:     my $blocked = 0;
                   4473:     if ($startblock && $endblock) {
                   4474:         $blocked = 1;
                   4475:     }
1.890     droeschl 4476: 
1.1061    raeburn  4477: # caller just wants to know whether a block is active
                   4478:     if (!wantarray) { return $blocked; }
                   4479: 
                   4480: # build a link to a popup window containing the details
                   4481:     my $querystring  = "?activity=$activity";
                   4482: # $uname and $udom decide whose portfolio the user is trying to look at
1.1062    raeburn  4483:     if ($activity eq 'port') {
                   4484:         $querystring .= "&amp;udom=$udom"      if $udom;
                   4485:         $querystring .= "&amp;uname=$uname"    if $uname;
                   4486:     } elsif ($activity eq 'docs') {
                   4487:         $querystring .= '&amp;url='.&HTML::Entities::encode($url,'&"');
                   4488:     }
1.1061    raeburn  4489: 
                   4490:     my $output .= <<'END_MYBLOCK';
                   4491: function openWindow(url, wdwName, w, h, toolbar,scrollbar) {
                   4492:     var options = "width=" + w + ",height=" + h + ",";
                   4493:     options += "resizable=yes,scrollbars="+scrollbar+",status=no,";
                   4494:     options += "menubar=no,toolbar="+toolbar+",location=no,directories=no";
                   4495:     var newWin = window.open(url, wdwName, options);
                   4496:     newWin.focus();
                   4497: }
1.890     droeschl 4498: END_MYBLOCK
1.854     kalberla 4499: 
1.1061    raeburn  4500:     $output = Apache::lonhtmlcommon::scripttag($output);
1.890     droeschl 4501:   
1.1061    raeburn  4502:     my $popupUrl = "/adm/blockingstatus/$querystring";
1.1062    raeburn  4503:     my $text = &mt('Communication Blocked');
                   4504:     if ($activity eq 'docs') {
                   4505:         $text = &mt('Content Access Blocked');
1.1063    raeburn  4506:     } elsif ($activity eq 'printout') {
                   4507:         $text = &mt('Printing Blocked');
1.1062    raeburn  4508:     }
1.1061    raeburn  4509:     $output .= <<"END_BLOCK";
1.867     kalberla 4510: <div class='LC_comblock'>
1.869     kalberla 4511:   <a onclick='openWindow("$popupUrl","Blocking Table",600,300,"no","no");return false;' href='/adm/blockingstatus/$querystring'
1.890     droeschl 4512:   title='$text'>
                   4513:   <img class='LC_noBorder LC_middle' title='$text' src='/res/adm/pages/comblock.png' alt='$text'/></a>
1.869     kalberla 4514:   <a onclick='openWindow("$popupUrl","Blocking Table",600,300,"no","no");return false;' href='/adm/blockingstatus/$querystring' 
1.890     droeschl 4515:   title='$text'>$text</a>
1.867     kalberla 4516: </div>
                   4517: 
                   4518: END_BLOCK
1.474     raeburn  4519: 
1.1061    raeburn  4520:     return ($blocked, $output);
1.854     kalberla 4521: }
1.490     raeburn  4522: 
1.60      matthew  4523: ###############################################
                   4524: 
1.682     raeburn  4525: sub check_ip_acc {
                   4526:     my ($acc)=@_;
                   4527:     &Apache::lonxml::debug("acc is $acc");
                   4528:     if (!defined($acc) || $acc =~ /^\s*$/ || $acc =~/^\s*no\s*$/i) {
                   4529:         return 1;
                   4530:     }
                   4531:     my $allowed=0;
                   4532:     my $ip=$env{'request.host'} || $ENV{'REMOTE_ADDR'};
                   4533: 
                   4534:     my $name;
                   4535:     foreach my $pattern (split(',',$acc)) {
                   4536:         $pattern =~ s/^\s*//;
                   4537:         $pattern =~ s/\s*$//;
                   4538:         if ($pattern =~ /\*$/) {
                   4539:             #35.8.*
                   4540:             $pattern=~s/\*//;
                   4541:             if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
                   4542:         } elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {
                   4543:             #35.8.3.[34-56]
                   4544:             my $low=$2;
                   4545:             my $high=$3;
                   4546:             $pattern=$1;
                   4547:             if ($ip =~ /^\Q$pattern\E/) {
                   4548:                 my $last=(split(/\./,$ip))[3];
                   4549:                 if ($last <=$high && $last >=$low) { $allowed=1; }
                   4550:             }
                   4551:         } elsif ($pattern =~ /^\*/) {
                   4552:             #*.msu.edu
                   4553:             $pattern=~s/\*//;
                   4554:             if (!defined($name)) {
                   4555:                 use Socket;
                   4556:                 my $netaddr=inet_aton($ip);
                   4557:                 ($name)=gethostbyaddr($netaddr,AF_INET);
                   4558:             }
                   4559:             if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
                   4560:         } elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
                   4561:             #127.0.0.1
                   4562:             if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
                   4563:         } else {
                   4564:             #some.name.com
                   4565:             if (!defined($name)) {
                   4566:                 use Socket;
                   4567:                 my $netaddr=inet_aton($ip);
                   4568:                 ($name)=gethostbyaddr($netaddr,AF_INET);
                   4569:             }
                   4570:             if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
                   4571:         }
                   4572:         if ($allowed) { last; }
                   4573:     }
                   4574:     return $allowed;
                   4575: }
                   4576: 
                   4577: ###############################################
                   4578: 
1.60      matthew  4579: =pod
                   4580: 
1.112     bowersj2 4581: =head1 Domain Template Functions
                   4582: 
                   4583: =over 4
                   4584: 
                   4585: =item * &determinedomain()
1.60      matthew  4586: 
                   4587: Inputs: $domain (usually will be undef)
                   4588: 
1.63      www      4589: Returns: Determines which domain should be used for designs
1.60      matthew  4590: 
                   4591: =cut
1.54      www      4592: 
1.60      matthew  4593: ###############################################
1.63      www      4594: sub determinedomain {
                   4595:     my $domain=shift;
1.531     albertel 4596:     if (! $domain) {
1.60      matthew  4597:         # Determine domain if we have not been given one
1.893     raeburn  4598:         $domain = &Apache::lonnet::default_login_domain();
1.258     albertel 4599:         if ($env{'user.domain'}) { $domain=$env{'user.domain'}; }
                   4600:         if ($env{'request.role.domain'}) { 
                   4601:             $domain=$env{'request.role.domain'}; 
1.60      matthew  4602:         }
                   4603:     }
1.63      www      4604:     return $domain;
                   4605: }
                   4606: ###############################################
1.517     raeburn  4607: 
1.518     albertel 4608: sub devalidate_domconfig_cache {
                   4609:     my ($udom)=@_;
                   4610:     &Apache::lonnet::devalidate_cache_new('domainconfig',$udom);
                   4611: }
                   4612: 
                   4613: # ---------------------- Get domain configuration for a domain
                   4614: sub get_domainconf {
                   4615:     my ($udom) = @_;
                   4616:     my $cachetime=1800;
                   4617:     my ($result,$cached)=&Apache::lonnet::is_cached_new('domainconfig',$udom);
                   4618:     if (defined($cached)) { return %{$result}; }
                   4619: 
                   4620:     my %domconfig = &Apache::lonnet::get_dom('configuration',
1.948     raeburn  4621: 					     ['login','rolecolors','autoenroll'],$udom);
1.632     raeburn  4622:     my (%designhash,%legacy);
1.518     albertel 4623:     if (keys(%domconfig) > 0) {
                   4624:         if (ref($domconfig{'login'}) eq 'HASH') {
1.632     raeburn  4625:             if (keys(%{$domconfig{'login'}})) {
                   4626:                 foreach my $key (keys(%{$domconfig{'login'}})) {
1.699     raeburn  4627:                     if (ref($domconfig{'login'}{$key}) eq 'HASH') {
1.946     raeburn  4628:                         if ($key eq 'loginvia') {
                   4629:                             if (ref($domconfig{'login'}{'loginvia'}) eq 'HASH') {
1.1013    raeburn  4630:                                 foreach my $hostname (keys(%{$domconfig{'login'}{'loginvia'}})) {
1.948     raeburn  4631:                                     if (ref($domconfig{'login'}{'loginvia'}{$hostname}) eq 'HASH') {
                   4632:                                         if ($domconfig{'login'}{'loginvia'}{$hostname}{'server'}) {
                   4633:                                             my $server = $domconfig{'login'}{'loginvia'}{$hostname}{'server'};
                   4634:                                             $designhash{$udom.'.login.loginvia'} = $server;
                   4635:                                             if ($domconfig{'login'}{'loginvia'}{$hostname}{'serverpath'} eq 'custom') {
                   4636: 
                   4637:                                                 $designhash{$udom.'.login.loginvia_'.$hostname} = $server.':'.$domconfig{'login'}{'loginvia'}{$hostname}{'custompath'};
                   4638:                                             } else {
1.1013    raeburn  4639:                                                 $designhash{$udom.'.login.loginvia_'.$hostname} = $server.':'.$domconfig{'login'}{'loginvia'}{$hostname}{'serverpath'};
1.948     raeburn  4640:                                             }
                   4641:                                             if ($domconfig{'login'}{'loginvia'}{$hostname}{'exempt'}) {
                   4642:                                                 $designhash{$udom.'.login.loginvia_exempt_'.$hostname} = $domconfig{'login'}{'loginvia'}{$hostname}{'exempt'};
                   4643:                                             }
1.946     raeburn  4644:                                         }
                   4645:                                     }
                   4646:                                 }
                   4647:                             }
                   4648:                         } else {
                   4649:                             foreach my $img (keys(%{$domconfig{'login'}{$key}})) {
                   4650:                                 $designhash{$udom.'.login.'.$key.'_'.$img} = 
                   4651:                                     $domconfig{'login'}{$key}{$img};
                   4652:                             }
1.699     raeburn  4653:                         }
                   4654:                     } else {
                   4655:                         $designhash{$udom.'.login.'.$key}=$domconfig{'login'}{$key};
                   4656:                     }
1.632     raeburn  4657:                 }
                   4658:             } else {
                   4659:                 $legacy{'login'} = 1;
1.518     albertel 4660:             }
1.632     raeburn  4661:         } else {
                   4662:             $legacy{'login'} = 1;
1.518     albertel 4663:         }
                   4664:         if (ref($domconfig{'rolecolors'}) eq 'HASH') {
1.632     raeburn  4665:             if (keys(%{$domconfig{'rolecolors'}})) {
                   4666:                 foreach my $role (keys(%{$domconfig{'rolecolors'}})) {
                   4667:                     if (ref($domconfig{'rolecolors'}{$role}) eq 'HASH') {
                   4668:                         foreach my $item (keys(%{$domconfig{'rolecolors'}{$role}})) {
                   4669:                             $designhash{$udom.'.'.$role.'.'.$item}=$domconfig{'rolecolors'}{$role}{$item};
                   4670:                         }
1.518     albertel 4671:                     }
                   4672:                 }
1.632     raeburn  4673:             } else {
                   4674:                 $legacy{'rolecolors'} = 1;
1.518     albertel 4675:             }
1.632     raeburn  4676:         } else {
                   4677:             $legacy{'rolecolors'} = 1;
1.518     albertel 4678:         }
1.948     raeburn  4679:         if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   4680:             if ($domconfig{'autoenroll'}{'co-owners'}) {
                   4681:                 $designhash{$udom.'.autoassign.co-owners'}=$domconfig{'autoenroll'}{'co-owners'};
                   4682:             }
                   4683:         }
1.632     raeburn  4684:         if (keys(%legacy) > 0) {
                   4685:             my %legacyhash = &get_legacy_domconf($udom);
                   4686:             foreach my $item (keys(%legacyhash)) {
                   4687:                 if ($item =~ /^\Q$udom\E\.login/) {
                   4688:                     if ($legacy{'login'}) { 
                   4689:                         $designhash{$item} = $legacyhash{$item};
                   4690:                     }
                   4691:                 } else {
                   4692:                     if ($legacy{'rolecolors'}) {
                   4693:                         $designhash{$item} = $legacyhash{$item};
                   4694:                     }
1.518     albertel 4695:                 }
                   4696:             }
                   4697:         }
1.632     raeburn  4698:     } else {
                   4699:         %designhash = &get_legacy_domconf($udom); 
1.518     albertel 4700:     }
                   4701:     &Apache::lonnet::do_cache_new('domainconfig',$udom,\%designhash,
                   4702: 				  $cachetime);
                   4703:     return %designhash;
                   4704: }
                   4705: 
1.632     raeburn  4706: sub get_legacy_domconf {
                   4707:     my ($udom) = @_;
                   4708:     my %legacyhash;
                   4709:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
                   4710:     my $designfile =  $designdir.'/'.$udom.'.tab';
                   4711:     if (-e $designfile) {
                   4712:         if ( open (my $fh,"<$designfile") ) {
                   4713:             while (my $line = <$fh>) {
                   4714:                 next if ($line =~ /^\#/);
                   4715:                 chomp($line);
                   4716:                 my ($key,$val)=(split(/\=/,$line));
                   4717:                 if ($val) { $legacyhash{$udom.'.'.$key}=$val; }
                   4718:             }
                   4719:             close($fh);
                   4720:         }
                   4721:     }
1.1026    raeburn  4722:     if (-e $Apache::lonnet::perlvar{'lonDocRoot'}.'/adm/lonDomLogos/'.$udom.'.gif') {
1.632     raeburn  4723:         $legacyhash{$udom.'.login.domlogo'} = "/adm/lonDomLogos/$udom.gif";
                   4724:     }
                   4725:     return %legacyhash;
                   4726: }
                   4727: 
1.63      www      4728: =pod
                   4729: 
1.112     bowersj2 4730: =item * &domainlogo()
1.63      www      4731: 
                   4732: Inputs: $domain (usually will be undef)
                   4733: 
                   4734: Returns: A link to a domain logo, if the domain logo exists.
                   4735: If the domain logo does not exist, a description of the domain.
                   4736: 
                   4737: =cut
1.112     bowersj2 4738: 
1.63      www      4739: ###############################################
                   4740: sub domainlogo {
1.517     raeburn  4741:     my $domain = &determinedomain(shift);
1.518     albertel 4742:     my %designhash = &get_domainconf($domain);    
1.517     raeburn  4743:     # See if there is a logo
                   4744:     if ($designhash{$domain.'.login.domlogo'} ne '') {
1.519     raeburn  4745:         my $imgsrc = $designhash{$domain.'.login.domlogo'};
1.538     albertel 4746:         if ($imgsrc =~ m{^/(adm|res)/}) {
                   4747: 	    if ($imgsrc =~ m{^/res/}) {
                   4748: 		my $local_name = &Apache::lonnet::filelocation('',$imgsrc);
                   4749: 		&Apache::lonnet::repcopy($local_name);
                   4750: 	    }
                   4751: 	   $imgsrc = &lonhttpdurl($imgsrc);
1.519     raeburn  4752:         } 
                   4753:         return '<img src="'.$imgsrc.'" alt="'.$domain.'" />';
1.514     albertel 4754:     } elsif (defined(&Apache::lonnet::domain($domain,'description'))) {
                   4755:         return &Apache::lonnet::domain($domain,'description');
1.59      www      4756:     } else {
1.60      matthew  4757:         return '';
1.59      www      4758:     }
                   4759: }
1.63      www      4760: ##############################################
                   4761: 
                   4762: =pod
                   4763: 
1.112     bowersj2 4764: =item * &designparm()
1.63      www      4765: 
                   4766: Inputs: $which parameter; $domain (usually will be undef)
                   4767: 
                   4768: Returns: value of designparamter $which
                   4769: 
                   4770: =cut
1.112     bowersj2 4771: 
1.397     albertel 4772: 
1.400     albertel 4773: ##############################################
1.397     albertel 4774: sub designparm {
                   4775:     my ($which,$domain)=@_;
                   4776:     if (exists($env{'environment.color.'.$which})) {
1.817     bisitz   4777:         return $env{'environment.color.'.$which};
1.96      www      4778:     }
1.63      www      4779:     $domain=&determinedomain($domain);
1.1016    raeburn  4780:     my %domdesign;
                   4781:     unless ($domain eq 'public') {
                   4782:         %domdesign = &get_domainconf($domain);
                   4783:     }
1.520     raeburn  4784:     my $output;
1.517     raeburn  4785:     if ($domdesign{$domain.'.'.$which} ne '') {
1.817     bisitz   4786:         $output = $domdesign{$domain.'.'.$which};
1.63      www      4787:     } else {
1.520     raeburn  4788:         $output = $defaultdesign{$which};
                   4789:     }
                   4790:     if (($which =~ /^(student|coordinator|author|admin)\.img$/) ||
1.635     raeburn  4791:         ($which =~ /login\.(img|logo|domlogo|login)/)) {
1.538     albertel 4792:         if ($output =~ m{^/(adm|res)/}) {
1.817     bisitz   4793:             if ($output =~ m{^/res/}) {
                   4794:                 my $local_name = &Apache::lonnet::filelocation('',$output);
                   4795:                 &Apache::lonnet::repcopy($local_name);
                   4796:             }
1.520     raeburn  4797:             $output = &lonhttpdurl($output);
                   4798:         }
1.63      www      4799:     }
1.520     raeburn  4800:     return $output;
1.63      www      4801: }
1.59      www      4802: 
1.822     bisitz   4803: ##############################################
                   4804: =pod
                   4805: 
1.832     bisitz   4806: =item * &authorspace()
                   4807: 
1.1028    raeburn  4808: Inputs: $url (usually will be undef).
1.832     bisitz   4809: 
1.1028    raeburn  4810: Returns: Path to Construction Space containing the resource or 
                   4811:          directory being viewed (or for which action is being taken). 
                   4812:          If $url is provided, and begins /priv/<domain>/<uname>
                   4813:          the path will be that portion of the $context argument.
                   4814:          Otherwise the path will be for the author space of the current
                   4815:          user when the current role is author, or for that of the 
                   4816:          co-author/assistant co-author space when the current role 
                   4817:          is co-author or assistant co-author.
1.832     bisitz   4818: 
                   4819: =cut
                   4820: 
                   4821: sub authorspace {
1.1028    raeburn  4822:     my ($url) = @_;
                   4823:     if ($url ne '') {
                   4824:         if ($url =~ m{^(/priv/$match_domain/$match_username/)}) {
                   4825:            return $1;
                   4826:         }
                   4827:     }
1.832     bisitz   4828:     my $caname = '';
1.1024    www      4829:     my $cadom = '';
1.1028    raeburn  4830:     if ($env{'request.role'} =~ /^(?:ca|aa)/) {
1.1024    www      4831:         ($cadom,$caname) =
1.832     bisitz   4832:             ($env{'request.role'}=~/($match_domain)\/($match_username)$/);
1.1028    raeburn  4833:     } elsif ($env{'request.role'} =~ m{^au\./($match_domain)/}) {
1.832     bisitz   4834:         $caname = $env{'user.name'};
1.1024    www      4835:         $cadom = $env{'user.domain'};
1.832     bisitz   4836:     }
1.1028    raeburn  4837:     if (($caname ne '') && ($cadom ne '')) {
                   4838:         return "/priv/$cadom/$caname/";
                   4839:     }
                   4840:     return;
1.832     bisitz   4841: }
                   4842: 
                   4843: ##############################################
                   4844: =pod
                   4845: 
1.822     bisitz   4846: =item * &head_subbox()
                   4847: 
                   4848: Inputs: $content (contains HTML code with page functions, etc.)
                   4849: 
                   4850: Returns: HTML div with $content
                   4851:          To be included in page header
                   4852: 
                   4853: =cut
                   4854: 
                   4855: sub head_subbox {
                   4856:     my ($content)=@_;
                   4857:     my $output =
1.993     raeburn  4858:         '<div class="LC_head_subbox">'
1.822     bisitz   4859:        .$content
                   4860:        .'</div>'
                   4861: }
                   4862: 
                   4863: ##############################################
                   4864: =pod
                   4865: 
                   4866: =item * &CSTR_pageheader()
                   4867: 
1.1026    raeburn  4868: Input: (optional) filename from which breadcrumb trail is built.
                   4869:        In most cases no input as needed, as $env{'request.filename'}
                   4870:        is appropriate for use in building the breadcrumb trail.
1.822     bisitz   4871: 
                   4872: Returns: HTML div with CSTR path and recent box
                   4873:          To be included on Construction Space pages
                   4874: 
                   4875: =cut
                   4876: 
                   4877: sub CSTR_pageheader {
1.1026    raeburn  4878:     my ($trailfile) = @_;
                   4879:     if ($trailfile eq '') {
                   4880:         $trailfile = $env{'request.filename'};
                   4881:     }
                   4882: 
                   4883: # this is for resources; directories have customtitle, and crumbs
                   4884: # and select recent are created in lonpubdir.pm
                   4885: 
                   4886:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
1.1022    www      4887:     my ($udom,$uname,$thisdisfn)=
1.1026    raeburn  4888:         ($trailfile =~ m{^\Q$londocroot\E/priv/([^/]+)/([^/]+)/(.*)$});
                   4889:     my $formaction = "/priv/$udom/$uname/$thisdisfn";
                   4890:     $formaction =~ s{/+}{/}g;
1.822     bisitz   4891: 
                   4892:     my $parentpath = '';
                   4893:     my $lastitem = '';
                   4894:     if ($thisdisfn =~ m-(.+/)([^/]*)$-) {
                   4895:         $parentpath = $1;
                   4896:         $lastitem = $2;
                   4897:     } else {
                   4898:         $lastitem = $thisdisfn;
                   4899:     }
1.921     bisitz   4900: 
                   4901:     my $output =
1.822     bisitz   4902:          '<div>'
                   4903:         .&Apache::loncommon::help_open_menu('','',3,'Authoring') #FIXME: Broken? Where is it?
                   4904:         .'<b>'.&mt('Construction Space:').'</b> '
                   4905:         .'<form name="dirs" method="post" action="'.$formaction
1.921     bisitz   4906:         .'" target="_top">' #FIXME lonpubdir: target="_parent"
1.1024    www      4907:         .&Apache::lonhtmlcommon::crumbs($uname.'/'.$parentpath,'_top','/priv/'.$udom,undef,undef);
1.921     bisitz   4908: 
                   4909:     if ($lastitem) {
                   4910:         $output .=
                   4911:              '<span class="LC_filename">'
                   4912:             .$lastitem
                   4913:             .'</span>';
                   4914:     }
                   4915:     $output .=
                   4916:          '<br />'
1.822     bisitz   4917:         #FIXME lonpubdir: &Apache::lonhtmlcommon::crumbs($uname.$thisdisfn.'/','_top','/priv','','+1',1)."</b></tt><br />"
                   4918:         .&Apache::lonhtmlcommon::select_recent('construct','recent','this.form.action=this.form.recent.value;this.form.submit()')
                   4919:         .'</form>'
                   4920:         .&Apache::lonmenu::constspaceform()
                   4921:         .'</div>';
1.921     bisitz   4922: 
                   4923:     return $output;
1.822     bisitz   4924: }
                   4925: 
1.60      matthew  4926: ###############################################
                   4927: ###############################################
                   4928: 
                   4929: =pod
                   4930: 
1.112     bowersj2 4931: =back
                   4932: 
1.549     albertel 4933: =head1 HTML Helpers
1.112     bowersj2 4934: 
                   4935: =over 4
                   4936: 
                   4937: =item * &bodytag()
1.60      matthew  4938: 
                   4939: Returns a uniform header for LON-CAPA web pages.
                   4940: 
                   4941: Inputs: 
                   4942: 
1.112     bowersj2 4943: =over 4
                   4944: 
                   4945: =item * $title, A title to be displayed on the page.
                   4946: 
                   4947: =item * $function, the current role (can be undef).
                   4948: 
                   4949: =item * $addentries, extra parameters for the <body> tag.
                   4950: 
                   4951: =item * $bodyonly, if defined, only return the <body> tag.
                   4952: 
                   4953: =item * $domain, if defined, force a given domain.
                   4954: 
                   4955: =item * $forcereg, if page should register as content page (relevant for 
1.86      www      4956:             text interface only)
1.60      matthew  4957: 
1.814     bisitz   4958: =item * $no_nav_bar, if true, keep the 'what is this' info but remove the
                   4959:                      navigational links
1.317     albertel 4960: 
1.338     albertel 4961: =item * $bgcolor, used to override the bgcolor on a webpage to a specific value
                   4962: 
1.1075.2.12  raeburn  4963: =item * $no_inline_link, if true and in remote mode, don't show the
                   4964:          'Switch To Inline Menu' link
                   4965: 
1.460     albertel 4966: =item * $args, optional argument valid values are
                   4967:             no_auto_mt_title -> prevents &mt()ing the title arg
1.562     albertel 4968:             inherit_jsmath -> when creating popup window in a page,
                   4969:                               should it have jsmath forced on by the
                   4970:                               current page
1.460     albertel 4971: 
1.1075.2.15  raeburn  4972: =item * $advtoolsref, optional argument, ref to an array containing
                   4973:             inlineremote items to be added in "Functions" menu below
                   4974:             breadcrumbs.
                   4975: 
1.112     bowersj2 4976: =back
                   4977: 
1.60      matthew  4978: Returns: A uniform header for LON-CAPA web pages.  
                   4979: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
                   4980: If $bodyonly is undef or zero, an html string containing a <body> tag and 
                   4981: other decorations will be returned.
                   4982: 
                   4983: =cut
                   4984: 
1.54      www      4985: sub bodytag {
1.831     bisitz   4986:     my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,
1.1075.2.15  raeburn  4987:         $no_nav_bar,$bgcolor,$no_inline_link,$args,$advtoolsref)=@_;
1.339     albertel 4988: 
1.954     raeburn  4989:     my $public;
                   4990:     if ((($env{'user.name'} eq 'public') && ($env{'user.domain'} eq 'public'))
                   4991:         || ($env{'user.name'} eq '') && ($env{'user.domain'} eq '')) {
                   4992:         $public = 1;
                   4993:     }
1.460     albertel 4994:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
1.339     albertel 4995: 
1.183     matthew  4996:     $function = &get_users_function() if (!$function);
1.339     albertel 4997:     my $img =    &designparm($function.'.img',$domain);
                   4998:     my $font =   &designparm($function.'.font',$domain);
                   4999:     my $pgbg   = $bgcolor || &designparm($function.'.pgbg',$domain);
                   5000: 
1.803     bisitz   5001:     my %design = ( 'style'   => 'margin-top: 0',
1.535     albertel 5002: 		   'bgcolor' => $pgbg,
1.339     albertel 5003: 		   'text'    => $font,
                   5004:                    'alink'   => &designparm($function.'.alink',$domain),
                   5005: 		   'vlink'   => &designparm($function.'.vlink',$domain),
                   5006: 		   'link'    => &designparm($function.'.link',$domain),);
1.438     albertel 5007:     @design{keys(%$addentries)} = @$addentries{keys(%$addentries)}; 
1.339     albertel 5008: 
1.63      www      5009:  # role and realm
1.378     raeburn  5010:     my ($role,$realm) = split(/\./,$env{'request.role'},2);
                   5011:     if ($role  eq 'ca') {
1.479     albertel 5012:         my ($rdom,$rname) = ($realm =~ m{^/($match_domain)/($match_username)$});
1.500     albertel 5013:         $realm = &plainname($rname,$rdom);
1.378     raeburn  5014:     } 
1.55      www      5015: # realm
1.258     albertel 5016:     if ($env{'request.course.id'}) {
1.378     raeburn  5017:         if ($env{'request.role'} !~ /^cr/) {
                   5018:             $role = &Apache::lonnet::plaintext($role,&course_type());
                   5019:         }
1.898     raeburn  5020:         if ($env{'request.course.sec'}) {
                   5021:             $role .= ('&nbsp;'x2).'-&nbsp;'.&mt('section:').'&nbsp;'.$env{'request.course.sec'};
                   5022:         }   
1.359     albertel 5023: 	$realm = $env{'course.'.$env{'request.course.id'}.'.description'};
1.378     raeburn  5024:     } else {
                   5025:         $role = &Apache::lonnet::plaintext($role);
1.54      www      5026:     }
1.433     albertel 5027: 
1.359     albertel 5028:     if (!$realm) { $realm='&nbsp;'; }
1.1075.2.12  raeburn  5029: # Set messages
                   5030:     my $messages=&domainlogo($domain);
1.330     albertel 5031: 
1.438     albertel 5032:     my $extra_body_attr = &make_attr_string($forcereg,\%design);
1.329     albertel 5033: 
1.101     www      5034: # construct main body tag
1.359     albertel 5035:     my $bodytag = "<body $extra_body_attr>".
1.562     albertel 5036: 	&Apache::lontexconvert::init_math_support($args->{'inherit_jsmath'});
1.252     albertel 5037: 
1.530     albertel 5038:     if ($bodyonly) {
1.60      matthew  5039:         return $bodytag;
1.798     tempelho 5040:     } 
1.359     albertel 5041: 
1.410     albertel 5042:     my $name = &plainname($env{'user.name'},$env{'user.domain'});
1.954     raeburn  5043:     if ($public) {
1.433     albertel 5044: 	undef($role);
1.434     albertel 5045:     } else {
1.1070    raeburn  5046: 	$name = &aboutmewrapper($name,$env{'user.name'},$env{'user.domain'},
                   5047:                                 undef,'LC_menubuttons_link');
1.433     albertel 5048:     }
1.359     albertel 5049:     
1.762     bisitz   5050:     my $titleinfo = '<h1>'.$title.'</h1>';
1.359     albertel 5051:     #
                   5052:     # Extra info if you are the DC
                   5053:     my $dc_info = '';
                   5054:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
                   5055:                         $env{'course.'.$env{'request.course.id'}.
                   5056:                                  '.domain'}.'/'})) {
                   5057:         my $cid = $env{'request.course.id'};
1.917     raeburn  5058:         $dc_info = $cid.' '.$env{'course.'.$cid.'.internal.coursecode'};
1.380     www      5059:         $dc_info =~ s/\s+$//;
1.359     albertel 5060:     }
                   5061: 
1.898     raeburn  5062:     $role = '<span class="LC_nobreak">('.$role.')</span>' if $role;
1.853     droeschl 5063:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
                   5064: 
1.1075.2.13  raeburn  5065:     if ($no_nav_bar || $env{'form.inhibitmenu'} eq 'yes') { 
                   5066:         return $bodytag; 
                   5067:     }
1.903     droeschl 5068: 
1.1075.2.13  raeburn  5069:     if ($env{'request.state'} eq 'construct') { $forcereg=1; }
                   5070: 
                   5071:     unless ($env{'environment.remote'} eq 'on') {
1.903     droeschl 5072: 
                   5073:         #    if ($env{'request.state'} eq 'construct') {
                   5074:         #        $titleinfo = &CSTR_pageheader(); #FIXME: Will be removed once all scripts have their own calls
                   5075:         #    }
                   5076: 
1.359     albertel 5077: 
1.1075.2.2  raeburn  5078: 
1.916     droeschl 5079:         if ($env{'request.noversionuri'} =~ m{^/res/adm/pages/}) {
1.1075.2.1  raeburn  5080:             unless ($env{'request.noversionuri'} =~ m{/res/adm/pages/bookmarkmenu/}) {
                   5081:                 if ($dc_info) {
                   5082:                      $dc_info = qq|<span class="LC_cusr_subheading">$dc_info</span>|;
                   5083:                 }
                   5084:                 $bodytag .= qq|<div id="LC_nav_bar">$name $role<br />
                   5085:                                <em>$realm</em> $dc_info</div>|;
                   5086:             }
1.903     droeschl 5087:             return $bodytag;
                   5088:         }
1.894     droeschl 5089: 
1.927     raeburn  5090:         unless ($env{'request.symb'} =~ m/\.page___\d+___/) {
                   5091:             $bodytag .= qq|<div id="LC_nav_bar">$name $role</div>|;
                   5092:         }
1.916     droeschl 5093: 
1.903     droeschl 5094:         $bodytag .= Apache::lonhtmlcommon::scripttag(
                   5095:             Apache::lonmenu::utilityfunctions(), 'start');
1.816     bisitz   5096: 
1.903     droeschl 5097:         $bodytag .= Apache::lonmenu::primary_menu();
1.852     droeschl 5098: 
1.917     raeburn  5099:         if ($dc_info) {
                   5100:             $dc_info = &dc_courseid_toggle($dc_info);
                   5101:         }
                   5102:         $bodytag .= qq|<div id="LC_realm">$realm $dc_info</div>|;
1.916     droeschl 5103: 
1.903     droeschl 5104:         #don't show menus for public users
1.954     raeburn  5105:         if (!$public){
1.903     droeschl 5106:             $bodytag .= Apache::lonmenu::secondary_menu();
                   5107:             $bodytag .= Apache::lonmenu::serverform();
1.920     raeburn  5108:             $bodytag .= Apache::lonhtmlcommon::scripttag('', 'end');
                   5109:             if ($env{'request.state'} eq 'construct') {
1.962     droeschl 5110:                 $bodytag .= &Apache::lonmenu::innerregister($forcereg,
1.920     raeburn  5111:                                 $args->{'bread_crumbs'});
                   5112:             } elsif ($forcereg) { 
                   5113:                 $bodytag .= &Apache::lonmenu::innerregister($forcereg);
1.1075.2.15  raeburn  5114:             } else {
                   5115:                 $bodytag .=
                   5116:                     &Apache::lonmenu::prepare_functions($env{'request.noversionuri'},
                   5117:                                                         $forcereg,$args->{'group'},
                   5118:                                                         $args->{'bread_crumbs'},
                   5119:                                                         $advtoolsref);
1.920     raeburn  5120:             }
1.903     droeschl 5121:         }else{
                   5122:             # this is to seperate menu from content when there's no secondary
                   5123:             # menu. Especially needed for public accessible ressources.
                   5124:             $bodytag .= '<hr style="clear:both" />';
                   5125:             $bodytag .= Apache::lonhtmlcommon::scripttag('', 'end'); 
1.235     raeburn  5126:         }
1.903     droeschl 5127: 
1.235     raeburn  5128:         return $bodytag;
1.1075.2.12  raeburn  5129:     }
                   5130: 
                   5131: #
                   5132: # Top frame rendering, Remote is up
                   5133: #
                   5134: 
                   5135:     my $imgsrc = $img;
                   5136:     if ($img =~ /^\/adm/) {
                   5137:         $imgsrc = &lonhttpdurl($img);
                   5138:     }
                   5139:     my $upperleft='<img src="'.$imgsrc.'" alt="'.$function.'" />';
                   5140: 
                   5141:     # Explicit link to get inline menu
                   5142:     my $menu= ($no_inline_link?''
                   5143:                :'<a href="/adm/remote?action=collapse" target="_top">'.&mt('Switch to Inline Menu Mode').'</a>');
                   5144: 
                   5145:     if ($dc_info) {
                   5146:         $dc_info = qq|<span class="LC_cusr_subheading">($dc_info)</span>|;
                   5147:     }
                   5148: 
                   5149:     unless ($env{'form.inhibitmenu'}) {
                   5150:         $bodytag .= qq|<div id="LC_nav_bar">$name $role</div>
                   5151:                        <ol class="LC_primary_menu LC_right">
                   5152:                        <li>$menu</li>
                   5153:                        </ol><div id="LC_realm"> $realm $dc_info</div>|;
                   5154:     }
1.1075.2.13  raeburn  5155:     my $funclist;
                   5156:     if ($env{'request.state'} eq 'construct') {
                   5157:         if (!$public){
                   5158:             if ($env{'request.state'} eq 'construct') {
                   5159:                 $funclist = &Apache::lonhtmlcommon::scripttag(
                   5160:                                 &Apache::lonmenu::utilityfunctions(), 'start').
                   5161:                             &Apache::lonhtmlcommon::scripttag('','end').
                   5162:                             &Apache::lonmenu::innerregister($forcereg,
                   5163:                                                             $args->{'bread_crumbs'});
                   5164:             }
                   5165:         }
                   5166:     }
1.1075.2.12  raeburn  5167:     return(<<ENDBODY);
                   5168: $bodytag
                   5169: <table id="LC_title_bar" class="LC_with_remote">
                   5170: <tr><td>$upperleft</td>
                   5171:     <td>$messages&nbsp;</td>
                   5172: </tr>
                   5173: <tr><td>$titleinfo $dc_info $menu</td>
                   5174: </tr>
                   5175: </table>
1.1075.2.13  raeburn  5176: $funclist
1.1075.2.12  raeburn  5177: ENDBODY
1.182     matthew  5178: }
                   5179: 
1.917     raeburn  5180: sub dc_courseid_toggle {
                   5181:     my ($dc_info) = @_;
1.980     raeburn  5182:     return ' <span id="dccidtext" class="LC_cusr_subheading LC_nobreak">'.
1.1069    raeburn  5183:            '<a href="javascript:showCourseID();" class="LC_menubuttons_link">'.
1.917     raeburn  5184:            &mt('(More ...)').'</a></span>'.
                   5185:            '<div id="dccid" class="LC_dccid">'.$dc_info.'</div>';
                   5186: }
                   5187: 
1.330     albertel 5188: sub make_attr_string {
                   5189:     my ($register,$attr_ref) = @_;
                   5190: 
                   5191:     if ($attr_ref && !ref($attr_ref)) {
                   5192: 	die("addentries Must be a hash ref ".
                   5193: 	    join(':',caller(1))." ".
                   5194: 	    join(':',caller(0))." ");
                   5195:     }
                   5196: 
                   5197:     if ($register) {
1.339     albertel 5198: 	my ($on_load,$on_unload);
                   5199: 	foreach my $key (keys(%{$attr_ref})) {
                   5200: 	    if      (lc($key) eq 'onload') {
                   5201: 		$on_load.=$attr_ref->{$key}.';';
                   5202: 		delete($attr_ref->{$key});
                   5203: 
                   5204: 	    } elsif (lc($key) eq 'onunload') {
                   5205: 		$on_unload.=$attr_ref->{$key}.';';
                   5206: 		delete($attr_ref->{$key});
                   5207: 	    }
                   5208: 	}
1.1075.2.12  raeburn  5209:         if ($env{'environment.remote'} eq 'on') {
                   5210:             $attr_ref->{'onload'}  =
                   5211:                 &Apache::lonmenu::loadevents().  $on_load;
                   5212:             $attr_ref->{'onunload'}=
                   5213:                 &Apache::lonmenu::unloadevents().$on_unload;
                   5214:         } else {  
                   5215: 	    $attr_ref->{'onload'}  = $on_load;
                   5216: 	    $attr_ref->{'onunload'}= $on_unload;
                   5217:         }
1.330     albertel 5218:     }
1.339     albertel 5219: 
1.330     albertel 5220:     my $attr_string;
                   5221:     foreach my $attr (keys(%$attr_ref)) {
                   5222: 	$attr_string .= " $attr=\"".$attr_ref->{$attr}.'" ';
                   5223:     }
                   5224:     return $attr_string;
                   5225: }
                   5226: 
                   5227: 
1.182     matthew  5228: ###############################################
1.251     albertel 5229: ###############################################
                   5230: 
                   5231: =pod
                   5232: 
                   5233: =item * &endbodytag()
                   5234: 
                   5235: Returns a uniform footer for LON-CAPA web pages.
                   5236: 
1.635     raeburn  5237: Inputs: 1 - optional reference to an args hash
                   5238: If in the hash, key for noredirectlink has a value which evaluates to true,
                   5239: a 'Continue' link is not displayed if the page contains an
                   5240: internal redirect in the <head></head> section,
                   5241: i.e., $env{'internal.head.redirect'} exists   
1.251     albertel 5242: 
                   5243: =cut
                   5244: 
                   5245: sub endbodytag {
1.635     raeburn  5246:     my ($args) = @_;
1.1075.2.6  raeburn  5247:     my $endbodytag;
                   5248:     unless ((ref($args) eq 'HASH') && ($args->{'notbody'})) {
                   5249:         $endbodytag='</body>';
                   5250:     }
1.269     albertel 5251:     $endbodytag=&Apache::lontexconvert::jsMath_process()."\n".$endbodytag;
1.315     albertel 5252:     if ( exists( $env{'internal.head.redirect'} ) ) {
1.635     raeburn  5253:         if (!(ref($args) eq 'HASH' && $args->{'noredirectlink'})) {
                   5254: 	    $endbodytag=
                   5255: 	        "<br /><a href=\"$env{'internal.head.redirect'}\">".
                   5256: 	        &mt('Continue').'</a>'.
                   5257: 	        $endbodytag;
                   5258:         }
1.315     albertel 5259:     }
1.251     albertel 5260:     return $endbodytag;
                   5261: }
                   5262: 
1.352     albertel 5263: =pod
                   5264: 
                   5265: =item * &standard_css()
                   5266: 
                   5267: Returns a style sheet
                   5268: 
                   5269: Inputs: (all optional)
                   5270:             domain         -> force to color decorate a page for a specific
                   5271:                                domain
                   5272:             function       -> force usage of a specific rolish color scheme
                   5273:             bgcolor        -> override the default page bgcolor
                   5274: 
                   5275: =cut
                   5276: 
1.343     albertel 5277: sub standard_css {
1.345     albertel 5278:     my ($function,$domain,$bgcolor) = @_;
1.352     albertel 5279:     $function  = &get_users_function() if (!$function);
                   5280:     my $img    = &designparm($function.'.img',   $domain);
                   5281:     my $tabbg  = &designparm($function.'.tabbg', $domain);
                   5282:     my $font   = &designparm($function.'.font',  $domain);
1.801     tempelho 5283:     my $fontmenu = &designparm($function.'.fontmenu', $domain);
1.791     tempelho 5284: #second colour for later usage
1.345     albertel 5285:     my $sidebg = &designparm($function.'.sidebg',$domain);
1.382     albertel 5286:     my $pgbg_or_bgcolor =
                   5287: 	         $bgcolor ||
1.352     albertel 5288: 	         &designparm($function.'.pgbg',  $domain);
1.382     albertel 5289:     my $pgbg   = &designparm($function.'.pgbg',  $domain);
1.352     albertel 5290:     my $alink  = &designparm($function.'.alink', $domain);
                   5291:     my $vlink  = &designparm($function.'.vlink', $domain);
                   5292:     my $link   = &designparm($function.'.link',  $domain);
                   5293: 
1.602     albertel 5294:     my $sans                 = 'Verdana,Arial,Helvetica,sans-serif';
1.395     albertel 5295:     my $mono                 = 'monospace';
1.850     bisitz   5296:     my $data_table_head      = $sidebg;
                   5297:     my $data_table_light     = '#FAFAFA';
1.1060    bisitz   5298:     my $data_table_dark      = '#E0E0E0';
1.470     banghart 5299:     my $data_table_darker    = '#CCCCCC';
1.349     albertel 5300:     my $data_table_highlight = '#FFFF00';
1.352     albertel 5301:     my $mail_new             = '#FFBB77';
                   5302:     my $mail_new_hover       = '#DD9955';
                   5303:     my $mail_read            = '#BBBB77';
                   5304:     my $mail_read_hover      = '#999944';
                   5305:     my $mail_replied         = '#AAAA88';
                   5306:     my $mail_replied_hover   = '#888855';
                   5307:     my $mail_other           = '#99BBBB';
                   5308:     my $mail_other_hover     = '#669999';
1.391     albertel 5309:     my $table_header         = '#DDDDDD';
1.489     raeburn  5310:     my $feedback_link_bg     = '#BBBBBB';
1.911     bisitz   5311:     my $lg_border_color      = '#C8C8C8';
1.952     onken    5312:     my $button_hover         = '#BF2317';
1.392     albertel 5313: 
1.608     albertel 5314:     my $border = ($env{'browser.type'} eq 'explorer' ||
1.911     bisitz   5315:       $env{'browser.type'} eq 'safari'     ) ? '0 2px 0 2px'
                   5316:                                              : '0 3px 0 4px';
1.448     albertel 5317: 
1.523     albertel 5318: 
1.343     albertel 5319:     return <<END;
1.947     droeschl 5320: 
                   5321: /* needed for iframe to allow 100% height in FF */
                   5322: body, html { 
                   5323:     margin: 0;
                   5324:     padding: 0 0.5%;
                   5325:     height: 99%; /* to avoid scrollbars */
                   5326: }
                   5327: 
1.795     www      5328: body {
1.911     bisitz   5329:   font-family: $sans;
                   5330:   line-height:130%;
                   5331:   font-size:0.83em;
                   5332:   color:$font;
1.795     www      5333: }
                   5334: 
1.959     onken    5335: a:focus,
                   5336: a:focus img {
1.795     www      5337:   color: red;
                   5338: }
1.698     harmsja  5339: 
1.911     bisitz   5340: form, .inline {
                   5341:   display: inline;
1.795     www      5342: }
1.721     harmsja  5343: 
1.795     www      5344: .LC_right {
1.911     bisitz   5345:   text-align:right;
1.795     www      5346: }
                   5347: 
                   5348: .LC_middle {
1.911     bisitz   5349:   vertical-align:middle;
1.795     www      5350: }
1.721     harmsja  5351: 
1.911     bisitz   5352: .LC_400Box {
                   5353:   width:400px;
                   5354: }
1.721     harmsja  5355: 
1.947     droeschl 5356: .LC_iframecontainer {
                   5357:     width: 98%;
                   5358:     margin: 0;
                   5359:     position: fixed;
                   5360:     top: 8.5em;
                   5361:     bottom: 0;
                   5362: }
                   5363: 
                   5364: .LC_iframecontainer iframe{
                   5365:     border: none;
                   5366:     width: 100%;
                   5367:     height: 100%;
                   5368: }
                   5369: 
1.778     bisitz   5370: .LC_filename {
                   5371:   font-family: $mono;
                   5372:   white-space:pre;
1.921     bisitz   5373:   font-size: 120%;
1.778     bisitz   5374: }
                   5375: 
                   5376: .LC_fileicon {
                   5377:   border: none;
                   5378:   height: 1.3em;
                   5379:   vertical-align: text-bottom;
                   5380:   margin-right: 0.3em;
                   5381:   text-decoration:none;
                   5382: }
                   5383: 
1.1008    www      5384: .LC_setting {
                   5385:   text-decoration:underline;
                   5386: }
                   5387: 
1.350     albertel 5388: .LC_error {
                   5389:   color: red;
                   5390: }
1.795     www      5391: 
1.1075.2.15  raeburn  5392: .LC_warning {
                   5393:   color: darkorange;
                   5394: }
                   5395: 
1.457     albertel 5396: .LC_diff_removed {
1.733     bisitz   5397:   color: red;
1.394     albertel 5398: }
1.532     albertel 5399: 
                   5400: .LC_info,
1.457     albertel 5401: .LC_success,
                   5402: .LC_diff_added {
1.350     albertel 5403:   color: green;
                   5404: }
1.795     www      5405: 
1.802     bisitz   5406: div.LC_confirm_box {
                   5407:   background-color: #FAFAFA;
                   5408:   border: 1px solid $lg_border_color;
                   5409:   margin-right: 0;
                   5410:   padding: 5px;
                   5411: }
                   5412: 
                   5413: div.LC_confirm_box .LC_error img,
                   5414: div.LC_confirm_box .LC_success img {
                   5415:   vertical-align: middle;
                   5416: }
                   5417: 
1.440     albertel 5418: .LC_icon {
1.771     droeschl 5419:   border: none;
1.790     droeschl 5420:   vertical-align: middle;
1.771     droeschl 5421: }
                   5422: 
1.543     albertel 5423: .LC_docs_spacer {
                   5424:   width: 25px;
                   5425:   height: 1px;
1.771     droeschl 5426:   border: none;
1.543     albertel 5427: }
1.346     albertel 5428: 
1.532     albertel 5429: .LC_internal_info {
1.735     bisitz   5430:   color: #999999;
1.532     albertel 5431: }
                   5432: 
1.794     www      5433: .LC_discussion {
1.1050    www      5434:   background: $data_table_dark;
1.911     bisitz   5435:   border: 1px solid black;
                   5436:   margin: 2px;
1.794     www      5437: }
                   5438: 
                   5439: .LC_disc_action_left {
1.1050    www      5440:   background: $sidebg;
1.911     bisitz   5441:   text-align: left;
1.1050    www      5442:   padding: 4px;
                   5443:   margin: 2px;
1.794     www      5444: }
                   5445: 
                   5446: .LC_disc_action_right {
1.1050    www      5447:   background: $sidebg;
1.911     bisitz   5448:   text-align: right;
1.1050    www      5449:   padding: 4px;
                   5450:   margin: 2px;
1.794     www      5451: }
                   5452: 
                   5453: .LC_disc_new_item {
1.911     bisitz   5454:   background: white;
                   5455:   border: 2px solid red;
1.1050    www      5456:   margin: 4px;
                   5457:   padding: 4px;
1.794     www      5458: }
                   5459: 
                   5460: .LC_disc_old_item {
1.911     bisitz   5461:   background: white;
1.1050    www      5462:   margin: 4px;
                   5463:   padding: 4px;
1.794     www      5464: }
                   5465: 
1.458     albertel 5466: table.LC_pastsubmission {
                   5467:   border: 1px solid black;
                   5468:   margin: 2px;
                   5469: }
                   5470: 
1.924     bisitz   5471: table#LC_menubuttons {
1.345     albertel 5472:   width: 100%;
                   5473:   background: $pgbg;
1.392     albertel 5474:   border: 2px;
1.402     albertel 5475:   border-collapse: separate;
1.803     bisitz   5476:   padding: 0;
1.345     albertel 5477: }
1.392     albertel 5478: 
1.801     tempelho 5479: table#LC_title_bar a {
                   5480:   color: $fontmenu;
                   5481: }
1.836     bisitz   5482: 
1.807     droeschl 5483: table#LC_title_bar {
1.819     tempelho 5484:   clear: both;
1.836     bisitz   5485:   display: none;
1.807     droeschl 5486: }
                   5487: 
1.795     www      5488: table#LC_title_bar,
1.933     droeschl 5489: table.LC_breadcrumbs, /* obsolete? */
1.393     albertel 5490: table#LC_title_bar.LC_with_remote {
1.359     albertel 5491:   width: 100%;
1.392     albertel 5492:   border-color: $pgbg;
                   5493:   border-style: solid;
                   5494:   border-width: $border;
1.379     albertel 5495:   background: $pgbg;
1.801     tempelho 5496:   color: $fontmenu;
1.392     albertel 5497:   border-collapse: collapse;
1.803     bisitz   5498:   padding: 0;
1.819     tempelho 5499:   margin: 0;
1.359     albertel 5500: }
1.795     www      5501: 
1.933     droeschl 5502: ul.LC_breadcrumb_tools_outerlist {
1.913     droeschl 5503:     margin: 0;
                   5504:     padding: 0;
1.933     droeschl 5505:     position: relative;
                   5506:     list-style: none;
1.913     droeschl 5507: }
1.933     droeschl 5508: ul.LC_breadcrumb_tools_outerlist li {
1.913     droeschl 5509:     display: inline;
                   5510: }
1.933     droeschl 5511: 
                   5512: .LC_breadcrumb_tools_navigation {
1.913     droeschl 5513:     padding: 0;
1.933     droeschl 5514:     margin: 0;
                   5515:     float: left;
1.913     droeschl 5516: }
1.933     droeschl 5517: .LC_breadcrumb_tools_tools {
                   5518:     padding: 0;
                   5519:     margin: 0;
1.913     droeschl 5520:     float: right;
                   5521: }
                   5522: 
1.359     albertel 5523: table#LC_title_bar td {
                   5524:   background: $tabbg;
                   5525: }
1.795     www      5526: 
1.911     bisitz   5527: table#LC_menubuttons img {
1.803     bisitz   5528:   border: none;
1.346     albertel 5529: }
1.795     www      5530: 
1.842     droeschl 5531: .LC_breadcrumbs_component {
1.911     bisitz   5532:   float: right;
                   5533:   margin: 0 1em;
1.357     albertel 5534: }
1.842     droeschl 5535: .LC_breadcrumbs_component img {
1.911     bisitz   5536:   vertical-align: middle;
1.777     tempelho 5537: }
1.795     www      5538: 
1.383     albertel 5539: td.LC_table_cell_checkbox {
                   5540:   text-align: center;
                   5541: }
1.795     www      5542: 
                   5543: .LC_fontsize_small {
1.911     bisitz   5544:   font-size: 70%;
1.705     tempelho 5545: }
                   5546: 
1.844     bisitz   5547: #LC_breadcrumbs {
1.911     bisitz   5548:   clear:both;
                   5549:   background: $sidebg;
                   5550:   border-bottom: 1px solid $lg_border_color;
                   5551:   line-height: 2.5em;
1.933     droeschl 5552:   overflow: hidden;
1.911     bisitz   5553:   margin: 0;
                   5554:   padding: 0;
1.995     raeburn  5555:   text-align: left;
1.819     tempelho 5556: }
1.862     bisitz   5557: 
1.1075.2.16  raeburn  5558: .LC_head_subbox, .LC_actionbox {
1.911     bisitz   5559:   clear:both;
                   5560:   background: #F8F8F8; /* $sidebg; */
1.915     droeschl 5561:   border: 1px solid $sidebg;
1.1075.2.16  raeburn  5562:   margin: 0 0 10px 0;
1.966     bisitz   5563:   padding: 3px;
1.995     raeburn  5564:   text-align: left;
1.822     bisitz   5565: }
                   5566: 
1.795     www      5567: .LC_fontsize_medium {
1.911     bisitz   5568:   font-size: 85%;
1.705     tempelho 5569: }
                   5570: 
1.795     www      5571: .LC_fontsize_large {
1.911     bisitz   5572:   font-size: 120%;
1.705     tempelho 5573: }
                   5574: 
1.346     albertel 5575: .LC_menubuttons_inline_text {
                   5576:   color: $font;
1.698     harmsja  5577:   font-size: 90%;
1.701     harmsja  5578:   padding-left:3px;
1.346     albertel 5579: }
                   5580: 
1.934     droeschl 5581: .LC_menubuttons_inline_text img{
                   5582:   vertical-align: middle;
                   5583: }
                   5584: 
1.1051    www      5585: li.LC_menubuttons_inline_text img {
1.951     onken    5586:   cursor:pointer;
1.1002    droeschl 5587:   text-decoration: none;
1.951     onken    5588: }
                   5589: 
1.526     www      5590: .LC_menubuttons_link {
                   5591:   text-decoration: none;
                   5592: }
1.795     www      5593: 
1.522     albertel 5594: .LC_menubuttons_category {
1.521     www      5595:   color: $font;
1.526     www      5596:   background: $pgbg;
1.521     www      5597:   font-size: larger;
                   5598:   font-weight: bold;
                   5599: }
                   5600: 
1.346     albertel 5601: td.LC_menubuttons_text {
1.911     bisitz   5602:   color: $font;
1.346     albertel 5603: }
1.706     harmsja  5604: 
1.346     albertel 5605: .LC_current_location {
                   5606:   background: $tabbg;
                   5607: }
1.795     www      5608: 
1.938     bisitz   5609: table.LC_data_table {
1.347     albertel 5610:   border: 1px solid #000000;
1.402     albertel 5611:   border-collapse: separate;
1.426     albertel 5612:   border-spacing: 1px;
1.610     albertel 5613:   background: $pgbg;
1.347     albertel 5614: }
1.795     www      5615: 
1.422     albertel 5616: .LC_data_table_dense {
                   5617:   font-size: small;
                   5618: }
1.795     www      5619: 
1.507     raeburn  5620: table.LC_nested_outer {
                   5621:   border: 1px solid #000000;
1.589     raeburn  5622:   border-collapse: collapse;
1.803     bisitz   5623:   border-spacing: 0;
1.507     raeburn  5624:   width: 100%;
                   5625: }
1.795     www      5626: 
1.879     raeburn  5627: table.LC_innerpickbox,
1.507     raeburn  5628: table.LC_nested {
1.803     bisitz   5629:   border: none;
1.589     raeburn  5630:   border-collapse: collapse;
1.803     bisitz   5631:   border-spacing: 0;
1.507     raeburn  5632:   width: 100%;
                   5633: }
1.795     www      5634: 
1.911     bisitz   5635: table.LC_data_table tr th,
                   5636: table.LC_calendar tr th,
1.879     raeburn  5637: table.LC_prior_tries tr th,
                   5638: table.LC_innerpickbox tr th {
1.349     albertel 5639:   font-weight: bold;
                   5640:   background-color: $data_table_head;
1.801     tempelho 5641:   color:$fontmenu;
1.701     harmsja  5642:   font-size:90%;
1.347     albertel 5643: }
1.795     www      5644: 
1.879     raeburn  5645: table.LC_innerpickbox tr th,
                   5646: table.LC_innerpickbox tr td {
                   5647:   vertical-align: top;
                   5648: }
                   5649: 
1.711     raeburn  5650: table.LC_data_table tr.LC_info_row > td {
1.735     bisitz   5651:   background-color: #CCCCCC;
1.711     raeburn  5652:   font-weight: bold;
                   5653:   text-align: left;
                   5654: }
1.795     www      5655: 
1.912     bisitz   5656: table.LC_data_table tr.LC_odd_row > td {
                   5657:   background-color: $data_table_light;
                   5658:   padding: 2px;
                   5659:   vertical-align: top;
                   5660: }
                   5661: 
1.809     bisitz   5662: table.LC_pick_box tr > td.LC_odd_row {
1.349     albertel 5663:   background-color: $data_table_light;
1.912     bisitz   5664:   vertical-align: top;
                   5665: }
                   5666: 
                   5667: table.LC_data_table tr.LC_even_row > td {
                   5668:   background-color: $data_table_dark;
1.425     albertel 5669:   padding: 2px;
1.900     bisitz   5670:   vertical-align: top;
1.347     albertel 5671: }
1.795     www      5672: 
1.809     bisitz   5673: table.LC_pick_box tr > td.LC_even_row {
1.349     albertel 5674:   background-color: $data_table_dark;
1.900     bisitz   5675:   vertical-align: top;
1.347     albertel 5676: }
1.795     www      5677: 
1.425     albertel 5678: table.LC_data_table tr.LC_data_table_highlight td {
                   5679:   background-color: $data_table_darker;
                   5680: }
1.795     www      5681: 
1.639     raeburn  5682: table.LC_data_table tr td.LC_leftcol_header {
                   5683:   background-color: $data_table_head;
                   5684:   font-weight: bold;
                   5685: }
1.795     www      5686: 
1.451     albertel 5687: table.LC_data_table tr.LC_empty_row td,
1.507     raeburn  5688: table.LC_nested tr.LC_empty_row td {
1.421     albertel 5689:   font-weight: bold;
                   5690:   font-style: italic;
                   5691:   text-align: center;
                   5692:   padding: 8px;
1.347     albertel 5693: }
1.795     www      5694: 
1.940     bisitz   5695: table.LC_data_table tr.LC_empty_row td {
                   5696:   background-color: $sidebg;
                   5697: }
                   5698: 
                   5699: table.LC_nested tr.LC_empty_row td {
                   5700:   background-color: #FFFFFF;
                   5701: }
                   5702: 
1.890     droeschl 5703: table.LC_caption {
                   5704: }
                   5705: 
1.507     raeburn  5706: table.LC_nested tr.LC_empty_row td {
1.465     albertel 5707:   padding: 4ex
                   5708: }
1.795     www      5709: 
1.507     raeburn  5710: table.LC_nested_outer tr th {
                   5711:   font-weight: bold;
1.801     tempelho 5712:   color:$fontmenu;
1.507     raeburn  5713:   background-color: $data_table_head;
1.701     harmsja  5714:   font-size: small;
1.507     raeburn  5715:   border-bottom: 1px solid #000000;
                   5716: }
1.795     www      5717: 
1.507     raeburn  5718: table.LC_nested_outer tr td.LC_subheader {
                   5719:   background-color: $data_table_head;
                   5720:   font-weight: bold;
                   5721:   font-size: small;
                   5722:   border-bottom: 1px solid #000000;
                   5723:   text-align: right;
1.451     albertel 5724: }
1.795     www      5725: 
1.507     raeburn  5726: table.LC_nested tr.LC_info_row td {
1.735     bisitz   5727:   background-color: #CCCCCC;
1.451     albertel 5728:   font-weight: bold;
                   5729:   font-size: small;
1.507     raeburn  5730:   text-align: center;
                   5731: }
1.795     www      5732: 
1.589     raeburn  5733: table.LC_nested tr.LC_info_row td.LC_left_item,
                   5734: table.LC_nested_outer tr th.LC_left_item {
1.507     raeburn  5735:   text-align: left;
1.451     albertel 5736: }
1.795     www      5737: 
1.507     raeburn  5738: table.LC_nested td {
1.735     bisitz   5739:   background-color: #FFFFFF;
1.451     albertel 5740:   font-size: small;
1.507     raeburn  5741: }
1.795     www      5742: 
1.507     raeburn  5743: table.LC_nested_outer tr th.LC_right_item,
                   5744: table.LC_nested tr.LC_info_row td.LC_right_item,
                   5745: table.LC_nested tr.LC_odd_row td.LC_right_item,
                   5746: table.LC_nested tr td.LC_right_item {
1.451     albertel 5747:   text-align: right;
                   5748: }
                   5749: 
1.507     raeburn  5750: table.LC_nested tr.LC_odd_row td {
1.735     bisitz   5751:   background-color: #EEEEEE;
1.451     albertel 5752: }
                   5753: 
1.473     raeburn  5754: table.LC_createuser {
                   5755: }
                   5756: 
                   5757: table.LC_createuser tr.LC_section_row td {
1.701     harmsja  5758:   font-size: small;
1.473     raeburn  5759: }
                   5760: 
                   5761: table.LC_createuser tr.LC_info_row td  {
1.735     bisitz   5762:   background-color: #CCCCCC;
1.473     raeburn  5763:   font-weight: bold;
                   5764:   text-align: center;
                   5765: }
                   5766: 
1.349     albertel 5767: table.LC_calendar {
                   5768:   border: 1px solid #000000;
                   5769:   border-collapse: collapse;
1.917     raeburn  5770:   width: 98%;
1.349     albertel 5771: }
1.795     www      5772: 
1.349     albertel 5773: table.LC_calendar_pickdate {
                   5774:   font-size: xx-small;
                   5775: }
1.795     www      5776: 
1.349     albertel 5777: table.LC_calendar tr td {
                   5778:   border: 1px solid #000000;
                   5779:   vertical-align: top;
1.917     raeburn  5780:   width: 14%;
1.349     albertel 5781: }
1.795     www      5782: 
1.349     albertel 5783: table.LC_calendar tr td.LC_calendar_day_empty {
                   5784:   background-color: $data_table_dark;
                   5785: }
1.795     www      5786: 
1.779     bisitz   5787: table.LC_calendar tr td.LC_calendar_day_current {
                   5788:   background-color: $data_table_highlight;
1.777     tempelho 5789: }
1.795     www      5790: 
1.938     bisitz   5791: table.LC_data_table tr td.LC_mail_new {
1.349     albertel 5792:   background-color: $mail_new;
                   5793: }
1.795     www      5794: 
1.938     bisitz   5795: table.LC_data_table tr.LC_mail_new:hover {
1.349     albertel 5796:   background-color: $mail_new_hover;
                   5797: }
1.795     www      5798: 
1.938     bisitz   5799: table.LC_data_table tr td.LC_mail_read {
1.349     albertel 5800:   background-color: $mail_read;
                   5801: }
1.795     www      5802: 
1.938     bisitz   5803: /*
                   5804: table.LC_data_table tr.LC_mail_read:hover {
1.349     albertel 5805:   background-color: $mail_read_hover;
                   5806: }
1.938     bisitz   5807: */
1.795     www      5808: 
1.938     bisitz   5809: table.LC_data_table tr td.LC_mail_replied {
1.349     albertel 5810:   background-color: $mail_replied;
                   5811: }
1.795     www      5812: 
1.938     bisitz   5813: /*
                   5814: table.LC_data_table tr.LC_mail_replied:hover {
1.349     albertel 5815:   background-color: $mail_replied_hover;
                   5816: }
1.938     bisitz   5817: */
1.795     www      5818: 
1.938     bisitz   5819: table.LC_data_table tr td.LC_mail_other {
1.349     albertel 5820:   background-color: $mail_other;
                   5821: }
1.795     www      5822: 
1.938     bisitz   5823: /*
                   5824: table.LC_data_table tr.LC_mail_other:hover {
1.349     albertel 5825:   background-color: $mail_other_hover;
                   5826: }
1.938     bisitz   5827: */
1.494     raeburn  5828: 
1.777     tempelho 5829: table.LC_data_table tr > td.LC_browser_file,
                   5830: table.LC_data_table tr > td.LC_browser_file_published {
1.899     bisitz   5831:   background: #AAEE77;
1.389     albertel 5832: }
1.795     www      5833: 
1.777     tempelho 5834: table.LC_data_table tr > td.LC_browser_file_locked,
                   5835: table.LC_data_table tr > td.LC_browser_file_unpublished {
1.389     albertel 5836:   background: #FFAA99;
1.387     albertel 5837: }
1.795     www      5838: 
1.777     tempelho 5839: table.LC_data_table tr > td.LC_browser_file_obsolete {
1.899     bisitz   5840:   background: #888888;
1.779     bisitz   5841: }
1.795     www      5842: 
1.777     tempelho 5843: table.LC_data_table tr > td.LC_browser_file_modified,
1.779     bisitz   5844: table.LC_data_table tr > td.LC_browser_file_metamodified {
1.899     bisitz   5845:   background: #F8F866;
1.777     tempelho 5846: }
1.795     www      5847: 
1.696     bisitz   5848: table.LC_data_table tr.LC_browser_folder > td {
1.899     bisitz   5849:   background: #E0E8FF;
1.387     albertel 5850: }
1.696     bisitz   5851: 
1.707     bisitz   5852: table.LC_data_table tr > td.LC_roles_is {
1.911     bisitz   5853:   /* background: #77FF77; */
1.707     bisitz   5854: }
1.795     www      5855: 
1.707     bisitz   5856: table.LC_data_table tr > td.LC_roles_future {
1.939     bisitz   5857:   border-right: 8px solid #FFFF77;
1.707     bisitz   5858: }
1.795     www      5859: 
1.707     bisitz   5860: table.LC_data_table tr > td.LC_roles_will {
1.939     bisitz   5861:   border-right: 8px solid #FFAA77;
1.707     bisitz   5862: }
1.795     www      5863: 
1.707     bisitz   5864: table.LC_data_table tr > td.LC_roles_expired {
1.939     bisitz   5865:   border-right: 8px solid #FF7777;
1.707     bisitz   5866: }
1.795     www      5867: 
1.707     bisitz   5868: table.LC_data_table tr > td.LC_roles_will_not {
1.939     bisitz   5869:   border-right: 8px solid #AAFF77;
1.707     bisitz   5870: }
1.795     www      5871: 
1.707     bisitz   5872: table.LC_data_table tr > td.LC_roles_selected {
1.939     bisitz   5873:   border-right: 8px solid #11CC55;
1.707     bisitz   5874: }
                   5875: 
1.388     albertel 5876: span.LC_current_location {
1.701     harmsja  5877:   font-size:larger;
1.388     albertel 5878:   background: $pgbg;
                   5879: }
1.387     albertel 5880: 
1.1029    www      5881: span.LC_current_nav_location {
                   5882:   font-weight:bold;
                   5883:   background: $sidebg;
                   5884: }
                   5885: 
1.395     albertel 5886: span.LC_parm_menu_item {
                   5887:   font-size: larger;
                   5888: }
1.795     www      5889: 
1.395     albertel 5890: span.LC_parm_scope_all {
                   5891:   color: red;
                   5892: }
1.795     www      5893: 
1.395     albertel 5894: span.LC_parm_scope_folder {
                   5895:   color: green;
                   5896: }
1.795     www      5897: 
1.395     albertel 5898: span.LC_parm_scope_resource {
                   5899:   color: orange;
                   5900: }
1.795     www      5901: 
1.395     albertel 5902: span.LC_parm_part {
                   5903:   color: blue;
                   5904: }
1.795     www      5905: 
1.911     bisitz   5906: span.LC_parm_folder,
                   5907: span.LC_parm_symb {
1.395     albertel 5908:   font-size: x-small;
                   5909:   font-family: $mono;
                   5910:   color: #AAAAAA;
                   5911: }
                   5912: 
1.977     bisitz   5913: ul.LC_parm_parmlist li {
                   5914:   display: inline-block;
                   5915:   padding: 0.3em 0.8em;
                   5916:   vertical-align: top;
                   5917:   width: 150px;
                   5918:   border-top:1px solid $lg_border_color;
                   5919: }
                   5920: 
1.795     www      5921: td.LC_parm_overview_level_menu,
                   5922: td.LC_parm_overview_map_menu,
                   5923: td.LC_parm_overview_parm_selectors,
                   5924: td.LC_parm_overview_restrictions  {
1.396     albertel 5925:   border: 1px solid black;
                   5926:   border-collapse: collapse;
                   5927: }
1.795     www      5928: 
1.396     albertel 5929: table.LC_parm_overview_restrictions td {
                   5930:   border-width: 1px 4px 1px 4px;
                   5931:   border-style: solid;
                   5932:   border-color: $pgbg;
                   5933:   text-align: center;
                   5934: }
1.795     www      5935: 
1.396     albertel 5936: table.LC_parm_overview_restrictions th {
                   5937:   background: $tabbg;
                   5938:   border-width: 1px 4px 1px 4px;
                   5939:   border-style: solid;
                   5940:   border-color: $pgbg;
                   5941: }
1.795     www      5942: 
1.398     albertel 5943: table#LC_helpmenu {
1.803     bisitz   5944:   border: none;
1.398     albertel 5945:   height: 55px;
1.803     bisitz   5946:   border-spacing: 0;
1.398     albertel 5947: }
                   5948: 
                   5949: table#LC_helpmenu fieldset legend {
                   5950:   font-size: larger;
                   5951: }
1.795     www      5952: 
1.397     albertel 5953: table#LC_helpmenu_links {
                   5954:   width: 100%;
                   5955:   border: 1px solid black;
                   5956:   background: $pgbg;
1.803     bisitz   5957:   padding: 0;
1.397     albertel 5958:   border-spacing: 1px;
                   5959: }
1.795     www      5960: 
1.397     albertel 5961: table#LC_helpmenu_links tr td {
                   5962:   padding: 1px;
                   5963:   background: $tabbg;
1.399     albertel 5964:   text-align: center;
                   5965:   font-weight: bold;
1.397     albertel 5966: }
1.396     albertel 5967: 
1.795     www      5968: table#LC_helpmenu_links a:link,
                   5969: table#LC_helpmenu_links a:visited,
1.397     albertel 5970: table#LC_helpmenu_links a:active {
                   5971:   text-decoration: none;
                   5972:   color: $font;
                   5973: }
1.795     www      5974: 
1.397     albertel 5975: table#LC_helpmenu_links a:hover {
                   5976:   text-decoration: underline;
                   5977:   color: $vlink;
                   5978: }
1.396     albertel 5979: 
1.417     albertel 5980: .LC_chrt_popup_exists {
                   5981:   border: 1px solid #339933;
                   5982:   margin: -1px;
                   5983: }
1.795     www      5984: 
1.417     albertel 5985: .LC_chrt_popup_up {
                   5986:   border: 1px solid yellow;
                   5987:   margin: -1px;
                   5988: }
1.795     www      5989: 
1.417     albertel 5990: .LC_chrt_popup {
                   5991:   border: 1px solid #8888FF;
                   5992:   background: #CCCCFF;
                   5993: }
1.795     www      5994: 
1.421     albertel 5995: table.LC_pick_box {
                   5996:   border-collapse: separate;
                   5997:   background: white;
                   5998:   border: 1px solid black;
                   5999:   border-spacing: 1px;
                   6000: }
1.795     www      6001: 
1.421     albertel 6002: table.LC_pick_box td.LC_pick_box_title {
1.850     bisitz   6003:   background: $sidebg;
1.421     albertel 6004:   font-weight: bold;
1.900     bisitz   6005:   text-align: left;
1.740     bisitz   6006:   vertical-align: top;
1.421     albertel 6007:   width: 184px;
                   6008:   padding: 8px;
                   6009: }
1.795     www      6010: 
1.579     raeburn  6011: table.LC_pick_box td.LC_pick_box_value {
                   6012:   text-align: left;
                   6013:   padding: 8px;
                   6014: }
1.795     www      6015: 
1.579     raeburn  6016: table.LC_pick_box td.LC_pick_box_select {
                   6017:   text-align: left;
                   6018:   padding: 8px;
                   6019: }
1.795     www      6020: 
1.424     albertel 6021: table.LC_pick_box td.LC_pick_box_separator {
1.803     bisitz   6022:   padding: 0;
1.421     albertel 6023:   height: 1px;
                   6024:   background: black;
                   6025: }
1.795     www      6026: 
1.421     albertel 6027: table.LC_pick_box td.LC_pick_box_submit {
                   6028:   text-align: right;
                   6029: }
1.795     www      6030: 
1.579     raeburn  6031: table.LC_pick_box td.LC_evenrow_value {
                   6032:   text-align: left;
                   6033:   padding: 8px;
                   6034:   background-color: $data_table_light;
                   6035: }
1.795     www      6036: 
1.579     raeburn  6037: table.LC_pick_box td.LC_oddrow_value {
                   6038:   text-align: left;
                   6039:   padding: 8px;
                   6040:   background-color: $data_table_light;
                   6041: }
1.795     www      6042: 
1.579     raeburn  6043: span.LC_helpform_receipt_cat {
                   6044:   font-weight: bold;
                   6045: }
1.795     www      6046: 
1.424     albertel 6047: table.LC_group_priv_box {
                   6048:   background: white;
                   6049:   border: 1px solid black;
                   6050:   border-spacing: 1px;
                   6051: }
1.795     www      6052: 
1.424     albertel 6053: table.LC_group_priv_box td.LC_pick_box_title {
                   6054:   background: $tabbg;
                   6055:   font-weight: bold;
                   6056:   text-align: right;
                   6057:   width: 184px;
                   6058: }
1.795     www      6059: 
1.424     albertel 6060: table.LC_group_priv_box td.LC_groups_fixed {
                   6061:   background: $data_table_light;
                   6062:   text-align: center;
                   6063: }
1.795     www      6064: 
1.424     albertel 6065: table.LC_group_priv_box td.LC_groups_optional {
                   6066:   background: $data_table_dark;
                   6067:   text-align: center;
                   6068: }
1.795     www      6069: 
1.424     albertel 6070: table.LC_group_priv_box td.LC_groups_functionality {
                   6071:   background: $data_table_darker;
                   6072:   text-align: center;
                   6073:   font-weight: bold;
                   6074: }
1.795     www      6075: 
1.424     albertel 6076: table.LC_group_priv td {
                   6077:   text-align: left;
1.803     bisitz   6078:   padding: 0;
1.424     albertel 6079: }
                   6080: 
                   6081: .LC_navbuttons {
                   6082:   margin: 2ex 0ex 2ex 0ex;
                   6083: }
1.795     www      6084: 
1.423     albertel 6085: .LC_topic_bar {
                   6086:   font-weight: bold;
                   6087:   background: $tabbg;
1.918     wenzelju 6088:   margin: 1em 0em 1em 2em;
1.805     bisitz   6089:   padding: 3px;
1.918     wenzelju 6090:   font-size: 1.2em;
1.423     albertel 6091: }
1.795     www      6092: 
1.423     albertel 6093: .LC_topic_bar span {
1.918     wenzelju 6094:   left: 0.5em;
                   6095:   position: absolute;
1.423     albertel 6096:   vertical-align: middle;
1.918     wenzelju 6097:   font-size: 1.2em;
1.423     albertel 6098: }
1.795     www      6099: 
1.423     albertel 6100: table.LC_course_group_status {
                   6101:   margin: 20px;
                   6102: }
1.795     www      6103: 
1.423     albertel 6104: table.LC_status_selector td {
                   6105:   vertical-align: top;
                   6106:   text-align: center;
1.424     albertel 6107:   padding: 4px;
                   6108: }
1.795     www      6109: 
1.599     albertel 6110: div.LC_feedback_link {
1.616     albertel 6111:   clear: both;
1.829     kalberla 6112:   background: $sidebg;
1.779     bisitz   6113:   width: 100%;
1.829     kalberla 6114:   padding-bottom: 10px;
                   6115:   border: 1px $tabbg solid;
1.833     kalberla 6116:   height: 22px;
                   6117:   line-height: 22px;
                   6118:   padding-top: 5px;
                   6119: }
                   6120: 
                   6121: div.LC_feedback_link img {
                   6122:   height: 22px;
1.867     kalberla 6123:   vertical-align:middle;
1.829     kalberla 6124: }
                   6125: 
1.911     bisitz   6126: div.LC_feedback_link a {
1.829     kalberla 6127:   text-decoration: none;
1.489     raeburn  6128: }
1.795     www      6129: 
1.867     kalberla 6130: div.LC_comblock {
1.911     bisitz   6131:   display:inline;
1.867     kalberla 6132:   color:$font;
                   6133:   font-size:90%;
                   6134: }
                   6135: 
                   6136: div.LC_feedback_link div.LC_comblock {
                   6137:   padding-left:5px;
                   6138: }
                   6139: 
                   6140: div.LC_feedback_link div.LC_comblock a {
                   6141:   color:$font;
                   6142: }
                   6143: 
1.489     raeburn  6144: span.LC_feedback_link {
1.858     bisitz   6145:   /* background: $feedback_link_bg; */
1.599     albertel 6146:   font-size: larger;
                   6147: }
1.795     www      6148: 
1.599     albertel 6149: span.LC_message_link {
1.858     bisitz   6150:   /* background: $feedback_link_bg; */
1.599     albertel 6151:   font-size: larger;
                   6152:   position: absolute;
                   6153:   right: 1em;
1.489     raeburn  6154: }
1.421     albertel 6155: 
1.515     albertel 6156: table.LC_prior_tries {
1.524     albertel 6157:   border: 1px solid #000000;
                   6158:   border-collapse: separate;
                   6159:   border-spacing: 1px;
1.515     albertel 6160: }
1.523     albertel 6161: 
1.515     albertel 6162: table.LC_prior_tries td {
1.524     albertel 6163:   padding: 2px;
1.515     albertel 6164: }
1.523     albertel 6165: 
                   6166: .LC_answer_correct {
1.795     www      6167:   background: lightgreen;
                   6168:   color: darkgreen;
                   6169:   padding: 6px;
1.523     albertel 6170: }
1.795     www      6171: 
1.523     albertel 6172: .LC_answer_charged_try {
1.797     www      6173:   background: #FFAAAA;
1.795     www      6174:   color: darkred;
                   6175:   padding: 6px;
1.523     albertel 6176: }
1.795     www      6177: 
1.779     bisitz   6178: .LC_answer_not_charged_try,
1.523     albertel 6179: .LC_answer_no_grade,
                   6180: .LC_answer_late {
1.795     www      6181:   background: lightyellow;
1.523     albertel 6182:   color: black;
1.795     www      6183:   padding: 6px;
1.523     albertel 6184: }
1.795     www      6185: 
1.523     albertel 6186: .LC_answer_previous {
1.795     www      6187:   background: lightblue;
                   6188:   color: darkblue;
                   6189:   padding: 6px;
1.523     albertel 6190: }
1.795     www      6191: 
1.779     bisitz   6192: .LC_answer_no_message {
1.777     tempelho 6193:   background: #FFFFFF;
                   6194:   color: black;
1.795     www      6195:   padding: 6px;
1.779     bisitz   6196: }
1.795     www      6197: 
1.779     bisitz   6198: .LC_answer_unknown {
                   6199:   background: orange;
                   6200:   color: black;
1.795     www      6201:   padding: 6px;
1.777     tempelho 6202: }
1.795     www      6203: 
1.529     albertel 6204: span.LC_prior_numerical,
                   6205: span.LC_prior_string,
                   6206: span.LC_prior_custom,
                   6207: span.LC_prior_reaction,
                   6208: span.LC_prior_math {
1.925     bisitz   6209:   font-family: $mono;
1.523     albertel 6210:   white-space: pre;
                   6211: }
                   6212: 
1.525     albertel 6213: span.LC_prior_string {
1.925     bisitz   6214:   font-family: $mono;
1.525     albertel 6215:   white-space: pre;
                   6216: }
                   6217: 
1.523     albertel 6218: table.LC_prior_option {
                   6219:   width: 100%;
                   6220:   border-collapse: collapse;
                   6221: }
1.795     www      6222: 
1.911     bisitz   6223: table.LC_prior_rank,
1.795     www      6224: table.LC_prior_match {
1.528     albertel 6225:   border-collapse: collapse;
                   6226: }
1.795     www      6227: 
1.528     albertel 6228: table.LC_prior_option tr td,
                   6229: table.LC_prior_rank tr td,
                   6230: table.LC_prior_match tr td {
1.524     albertel 6231:   border: 1px solid #000000;
1.515     albertel 6232: }
                   6233: 
1.855     bisitz   6234: .LC_nobreak {
1.544     albertel 6235:   white-space: nowrap;
1.519     raeburn  6236: }
                   6237: 
1.576     raeburn  6238: span.LC_cusr_emph {
                   6239:   font-style: italic;
                   6240: }
                   6241: 
1.633     raeburn  6242: span.LC_cusr_subheading {
                   6243:   font-weight: normal;
                   6244:   font-size: 85%;
                   6245: }
                   6246: 
1.861     bisitz   6247: div.LC_docs_entry_move {
1.859     bisitz   6248:   border: 1px solid #BBBBBB;
1.545     albertel 6249:   background: #DDDDDD;
1.861     bisitz   6250:   width: 22px;
1.859     bisitz   6251:   padding: 1px;
                   6252:   margin: 0;
1.545     albertel 6253: }
                   6254: 
1.861     bisitz   6255: table.LC_data_table tr > td.LC_docs_entry_commands,
                   6256: table.LC_data_table tr > td.LC_docs_entry_parameter {
1.545     albertel 6257:   font-size: x-small;
                   6258: }
1.795     www      6259: 
1.861     bisitz   6260: .LC_docs_entry_parameter {
                   6261:   white-space: nowrap;
                   6262: }
                   6263: 
1.544     albertel 6264: .LC_docs_copy {
1.545     albertel 6265:   color: #000099;
1.544     albertel 6266: }
1.795     www      6267: 
1.544     albertel 6268: .LC_docs_cut {
1.545     albertel 6269:   color: #550044;
1.544     albertel 6270: }
1.795     www      6271: 
1.544     albertel 6272: .LC_docs_rename {
1.545     albertel 6273:   color: #009900;
1.544     albertel 6274: }
1.795     www      6275: 
1.544     albertel 6276: .LC_docs_remove {
1.545     albertel 6277:   color: #990000;
                   6278: }
                   6279: 
1.547     albertel 6280: .LC_docs_reinit_warn,
                   6281: .LC_docs_ext_edit {
                   6282:   font-size: x-small;
                   6283: }
                   6284: 
1.545     albertel 6285: table.LC_docs_adddocs td,
                   6286: table.LC_docs_adddocs th {
                   6287:   border: 1px solid #BBBBBB;
                   6288:   padding: 4px;
                   6289:   background: #DDDDDD;
1.543     albertel 6290: }
                   6291: 
1.584     albertel 6292: table.LC_sty_begin {
                   6293:   background: #BBFFBB;
                   6294: }
1.795     www      6295: 
1.584     albertel 6296: table.LC_sty_end {
                   6297:   background: #FFBBBB;
                   6298: }
                   6299: 
1.589     raeburn  6300: table.LC_double_column {
1.803     bisitz   6301:   border-width: 0;
1.589     raeburn  6302:   border-collapse: collapse;
                   6303:   width: 100%;
                   6304:   padding: 2px;
                   6305: }
                   6306: 
                   6307: table.LC_double_column tr td.LC_left_col {
1.590     raeburn  6308:   top: 2px;
1.589     raeburn  6309:   left: 2px;
                   6310:   width: 47%;
                   6311:   vertical-align: top;
                   6312: }
                   6313: 
                   6314: table.LC_double_column tr td.LC_right_col {
                   6315:   top: 2px;
1.779     bisitz   6316:   right: 2px;
1.589     raeburn  6317:   width: 47%;
                   6318:   vertical-align: top;
                   6319: }
                   6320: 
1.591     raeburn  6321: div.LC_left_float {
                   6322:   float: left;
                   6323:   padding-right: 5%;
1.597     albertel 6324:   padding-bottom: 4px;
1.591     raeburn  6325: }
                   6326: 
                   6327: div.LC_clear_float_header {
1.597     albertel 6328:   padding-bottom: 2px;
1.591     raeburn  6329: }
                   6330: 
                   6331: div.LC_clear_float_footer {
1.597     albertel 6332:   padding-top: 10px;
1.591     raeburn  6333:   clear: both;
                   6334: }
                   6335: 
1.597     albertel 6336: div.LC_grade_show_user {
1.941     bisitz   6337: /*  border-left: 5px solid $sidebg; */
                   6338:   border-top: 5px solid #000000;
                   6339:   margin: 50px 0 0 0;
1.936     bisitz   6340:   padding: 15px 0 5px 10px;
1.597     albertel 6341: }
1.795     www      6342: 
1.936     bisitz   6343: div.LC_grade_show_user_odd_row {
1.941     bisitz   6344: /*  border-left: 5px solid #000000; */
                   6345: }
                   6346: 
                   6347: div.LC_grade_show_user div.LC_Box {
                   6348:   margin-right: 50px;
1.597     albertel 6349: }
                   6350: 
                   6351: div.LC_grade_submissions,
                   6352: div.LC_grade_message_center,
1.936     bisitz   6353: div.LC_grade_info_links {
1.597     albertel 6354:   margin: 5px;
                   6355:   width: 99%;
                   6356:   background: #FFFFFF;
                   6357: }
1.795     www      6358: 
1.597     albertel 6359: div.LC_grade_submissions_header,
1.936     bisitz   6360: div.LC_grade_message_center_header {
1.705     tempelho 6361:   font-weight: bold;
                   6362:   font-size: large;
1.597     albertel 6363: }
1.795     www      6364: 
1.597     albertel 6365: div.LC_grade_submissions_body,
1.936     bisitz   6366: div.LC_grade_message_center_body {
1.597     albertel 6367:   border: 1px solid black;
                   6368:   width: 99%;
                   6369:   background: #FFFFFF;
                   6370: }
1.795     www      6371: 
1.613     albertel 6372: table.LC_scantron_action {
                   6373:   width: 100%;
                   6374: }
1.795     www      6375: 
1.613     albertel 6376: table.LC_scantron_action tr th {
1.698     harmsja  6377:   font-weight:bold;
                   6378:   font-style:normal;
1.613     albertel 6379: }
1.795     www      6380: 
1.779     bisitz   6381: .LC_edit_problem_header,
1.614     albertel 6382: div.LC_edit_problem_footer {
1.705     tempelho 6383:   font-weight: normal;
                   6384:   font-size:  medium;
1.602     albertel 6385:   margin: 2px;
1.1060    bisitz   6386:   background-color: $sidebg;
1.600     albertel 6387: }
1.795     www      6388: 
1.600     albertel 6389: div.LC_edit_problem_header,
1.602     albertel 6390: div.LC_edit_problem_header div,
1.614     albertel 6391: div.LC_edit_problem_footer,
                   6392: div.LC_edit_problem_footer div,
1.602     albertel 6393: div.LC_edit_problem_editxml_header,
                   6394: div.LC_edit_problem_editxml_header div {
1.600     albertel 6395:   margin-top: 5px;
                   6396: }
1.795     www      6397: 
1.600     albertel 6398: div.LC_edit_problem_header_title {
1.705     tempelho 6399:   font-weight: bold;
                   6400:   font-size: larger;
1.602     albertel 6401:   background: $tabbg;
                   6402:   padding: 3px;
1.1060    bisitz   6403:   margin: 0 0 5px 0;
1.602     albertel 6404: }
1.795     www      6405: 
1.602     albertel 6406: table.LC_edit_problem_header_title {
                   6407:   width: 100%;
1.600     albertel 6408:   background: $tabbg;
1.602     albertel 6409: }
                   6410: 
                   6411: div.LC_edit_problem_discards {
                   6412:   float: left;
                   6413:   padding-bottom: 5px;
                   6414: }
1.795     www      6415: 
1.602     albertel 6416: div.LC_edit_problem_saves {
                   6417:   float: right;
                   6418:   padding-bottom: 5px;
1.600     albertel 6419: }
1.795     www      6420: 
1.911     bisitz   6421: img.stift {
1.803     bisitz   6422:   border-width: 0;
                   6423:   vertical-align: middle;
1.677     riegler  6424: }
1.680     riegler  6425: 
1.923     bisitz   6426: table td.LC_mainmenu_col_fieldset {
1.680     riegler  6427:   vertical-align: top;
1.777     tempelho 6428: }
1.795     www      6429: 
1.716     raeburn  6430: div.LC_createcourse {
1.911     bisitz   6431:   margin: 10px 10px 10px 10px;
1.716     raeburn  6432: }
                   6433: 
1.917     raeburn  6434: .LC_dccid {
                   6435:   margin: 0.2em 0 0 0;
                   6436:   padding: 0;
                   6437:   font-size: 90%;
                   6438:   display:none;
                   6439: }
                   6440: 
1.897     wenzelju 6441: ol.LC_primary_menu a:hover,
1.721     harmsja  6442: ol#LC_MenuBreadcrumbs a:hover,
                   6443: ol#LC_PathBreadcrumbs a:hover,
1.897     wenzelju 6444: ul#LC_secondary_menu a:hover,
1.721     harmsja  6445: .LC_FormSectionClearButton input:hover
1.795     www      6446: ul.LC_TabContent   li:hover a {
1.952     onken    6447:   color:$button_hover;
1.911     bisitz   6448:   text-decoration:none;
1.693     droeschl 6449: }
                   6450: 
1.779     bisitz   6451: h1 {
1.911     bisitz   6452:   padding: 0;
                   6453:   line-height:130%;
1.693     droeschl 6454: }
1.698     harmsja  6455: 
1.911     bisitz   6456: h2,
                   6457: h3,
                   6458: h4,
                   6459: h5,
                   6460: h6 {
                   6461:   margin: 5px 0 5px 0;
                   6462:   padding: 0;
                   6463:   line-height:130%;
1.693     droeschl 6464: }
1.795     www      6465: 
                   6466: .LC_hcell {
1.911     bisitz   6467:   padding:3px 15px 3px 15px;
                   6468:   margin: 0;
                   6469:   background-color:$tabbg;
                   6470:   color:$fontmenu;
                   6471:   border-bottom:solid 1px $lg_border_color;
1.693     droeschl 6472: }
1.795     www      6473: 
1.840     bisitz   6474: .LC_Box > .LC_hcell {
1.911     bisitz   6475:   margin: 0 -10px 10px -10px;
1.835     bisitz   6476: }
                   6477: 
1.721     harmsja  6478: .LC_noBorder {
1.911     bisitz   6479:   border: 0;
1.698     harmsja  6480: }
1.693     droeschl 6481: 
1.721     harmsja  6482: .LC_FormSectionClearButton input {
1.911     bisitz   6483:   background-color:transparent;
                   6484:   border: none;
                   6485:   cursor:pointer;
                   6486:   text-decoration:underline;
1.693     droeschl 6487: }
1.763     bisitz   6488: 
                   6489: .LC_help_open_topic {
1.911     bisitz   6490:   color: #FFFFFF;
                   6491:   background-color: #EEEEFF;
                   6492:   margin: 1px;
                   6493:   padding: 4px;
                   6494:   border: 1px solid #000033;
                   6495:   white-space: nowrap;
                   6496:   /* vertical-align: middle; */
1.759     neumanie 6497: }
1.693     droeschl 6498: 
1.911     bisitz   6499: dl,
                   6500: ul,
                   6501: div,
                   6502: fieldset {
                   6503:   margin: 10px 10px 10px 0;
                   6504:   /* overflow: hidden; */
1.693     droeschl 6505: }
1.795     www      6506: 
1.838     bisitz   6507: fieldset > legend {
1.911     bisitz   6508:   font-weight: bold;
                   6509:   padding: 0 5px 0 5px;
1.838     bisitz   6510: }
                   6511: 
1.813     bisitz   6512: #LC_nav_bar {
1.911     bisitz   6513:   float: left;
1.995     raeburn  6514:   background-color: $pgbg_or_bgcolor;
1.966     bisitz   6515:   margin: 0 0 2px 0;
1.807     droeschl 6516: }
                   6517: 
1.916     droeschl 6518: #LC_realm {
                   6519:   margin: 0.2em 0 0 0;
                   6520:   padding: 0;
                   6521:   font-weight: bold;
                   6522:   text-align: center;
1.995     raeburn  6523:   background-color: $pgbg_or_bgcolor;
1.916     droeschl 6524: }
                   6525: 
1.911     bisitz   6526: #LC_nav_bar em {
                   6527:   font-weight: bold;
                   6528:   font-style: normal;
1.807     droeschl 6529: }
                   6530: 
1.897     wenzelju 6531: ol.LC_primary_menu {
1.911     bisitz   6532:   float: right;
1.934     droeschl 6533:   margin: 0;
1.1075.2.2  raeburn  6534:   padding: 0;
1.995     raeburn  6535:   background-color: $pgbg_or_bgcolor;
1.807     droeschl 6536: }
                   6537: 
1.852     droeschl 6538: ol#LC_PathBreadcrumbs {
1.911     bisitz   6539:   margin: 0;
1.693     droeschl 6540: }
                   6541: 
1.897     wenzelju 6542: ol.LC_primary_menu li {
1.1075.2.2  raeburn  6543:   color: RGB(80, 80, 80);
                   6544:   vertical-align: middle;
                   6545:   text-align: left;
                   6546:   list-style: none;
                   6547:   float: left;
                   6548: }
                   6549: 
                   6550: ol.LC_primary_menu li a {
                   6551:   display: block;
                   6552:   margin: 0;
                   6553:   padding: 0 5px 0 10px;
                   6554:   text-decoration: none;
                   6555: }
                   6556: 
                   6557: ol.LC_primary_menu li ul {
                   6558:   display: none;
                   6559:   width: 10em;
                   6560:   background-color: $data_table_light;
                   6561: }
                   6562: 
                   6563: ol.LC_primary_menu li:hover ul, ol.LC_primary_menu li.hover ul {
                   6564:   display: block;
                   6565:   position: absolute;
                   6566:   margin: 0;
                   6567:   padding: 0;
1.1075.2.5  raeburn  6568:   z-index: 2;
1.1075.2.2  raeburn  6569: }
                   6570: 
                   6571: ol.LC_primary_menu li:hover li, ol.LC_primary_menu li.hover li {
                   6572:   font-size: 90%;
1.911     bisitz   6573:   vertical-align: top;
1.1075.2.2  raeburn  6574:   float: none;
1.1075.2.5  raeburn  6575:   border-left: 1px solid black;
                   6576:   border-right: 1px solid black;
1.1075.2.2  raeburn  6577: }
                   6578: 
                   6579: ol.LC_primary_menu li:hover li a, ol.LC_primary_menu li.hover li a {
1.1075.2.5  raeburn  6580:   background-color:$data_table_light;
1.1075.2.2  raeburn  6581: }
                   6582: 
                   6583: ol.LC_primary_menu li li a:hover {
                   6584:    color:$button_hover;
                   6585:    background-color:$data_table_dark;
1.693     droeschl 6586: }
                   6587: 
1.897     wenzelju 6588: ol.LC_primary_menu li img {
1.911     bisitz   6589:   vertical-align: bottom;
1.934     droeschl 6590:   height: 1.1em;
1.1075.2.3  raeburn  6591:   margin: 0.2em 0 0 0;
1.693     droeschl 6592: }
                   6593: 
1.897     wenzelju 6594: ol.LC_primary_menu a {
1.911     bisitz   6595:   color: RGB(80, 80, 80);
                   6596:   text-decoration: none;
1.693     droeschl 6597: }
1.795     www      6598: 
1.949     droeschl 6599: ol.LC_primary_menu a.LC_new_message {
                   6600:   font-weight:bold;
                   6601:   color: darkred;
                   6602: }
                   6603: 
1.975     raeburn  6604: ol.LC_docs_parameters {
                   6605:   margin-left: 0;
                   6606:   padding: 0;
                   6607:   list-style: none;
                   6608: }
                   6609: 
                   6610: ol.LC_docs_parameters li {
                   6611:   margin: 0;
                   6612:   padding-right: 20px;
                   6613:   display: inline;
                   6614: }
                   6615: 
1.976     raeburn  6616: ol.LC_docs_parameters li:before {
                   6617:   content: "\\002022 \\0020";
                   6618: }
                   6619: 
                   6620: li.LC_docs_parameters_title {
                   6621:   font-weight: bold;
                   6622: }
                   6623: 
                   6624: ol.LC_docs_parameters li.LC_docs_parameters_title:before {
                   6625:   content: "";
                   6626: }
                   6627: 
1.897     wenzelju 6628: ul#LC_secondary_menu {
1.911     bisitz   6629:   clear: both;
                   6630:   color: $fontmenu;
                   6631:   background: $tabbg;
                   6632:   list-style: none;
                   6633:   padding: 0;
                   6634:   margin: 0;
                   6635:   width: 100%;
1.995     raeburn  6636:   text-align: left;
1.1075.2.4  raeburn  6637:   float: left;
1.808     droeschl 6638: }
                   6639: 
1.897     wenzelju 6640: ul#LC_secondary_menu li {
1.911     bisitz   6641:   font-weight: bold;
                   6642:   line-height: 1.8em;
                   6643:   border-right: 1px solid black;
                   6644:   vertical-align: middle;
1.1075.2.4  raeburn  6645:   float: left;
                   6646: }
                   6647: 
                   6648: ul#LC_secondary_menu li.LC_hoverable:hover, ul#LC_secondary_menu li.hover {
                   6649:   background-color: $data_table_light;
                   6650: }
                   6651: 
                   6652: ul#LC_secondary_menu li a {
                   6653:   padding: 0 0.8em;
                   6654: }
                   6655: 
                   6656: ul#LC_secondary_menu li ul {
                   6657:   display: none;
                   6658: }
                   6659: 
                   6660: ul#LC_secondary_menu li:hover ul, ul#LC_secondary_menu li.hover ul {
                   6661:   display: block;
                   6662:   position: absolute;
                   6663:   margin: 0;
                   6664:   padding: 0;
                   6665:   list-style:none;
                   6666:   float: none;
                   6667:   background-color: $data_table_light;
1.1075.2.5  raeburn  6668:   z-index: 2;
1.1075.2.10  raeburn  6669:   margin-left: -1px;
1.1075.2.4  raeburn  6670: }
                   6671: 
                   6672: ul#LC_secondary_menu li ul li {
                   6673:   font-size: 90%;
                   6674:   vertical-align: top;
                   6675:   border-left: 1px solid black;
                   6676:   border-right: 1px solid black;
                   6677:   background-color: $data_table_light
                   6678:   list-style:none;
                   6679:   float: none;
                   6680: }
                   6681: 
                   6682: ul#LC_secondary_menu li ul li:hover, ul#LC_secondary_menu li ul li.hover {
                   6683:   background-color: $data_table_dark;
1.807     droeschl 6684: }
                   6685: 
1.847     tempelho 6686: ul.LC_TabContent {
1.911     bisitz   6687:   display:block;
                   6688:   background: $sidebg;
                   6689:   border-bottom: solid 1px $lg_border_color;
                   6690:   list-style:none;
1.1020    raeburn  6691:   margin: -1px -10px 0 -10px;
1.911     bisitz   6692:   padding: 0;
1.693     droeschl 6693: }
                   6694: 
1.795     www      6695: ul.LC_TabContent li,
                   6696: ul.LC_TabContentBigger li {
1.911     bisitz   6697:   float:left;
1.741     harmsja  6698: }
1.795     www      6699: 
1.897     wenzelju 6700: ul#LC_secondary_menu li a {
1.911     bisitz   6701:   color: $fontmenu;
                   6702:   text-decoration: none;
1.693     droeschl 6703: }
1.795     www      6704: 
1.721     harmsja  6705: ul.LC_TabContent {
1.952     onken    6706:   min-height:20px;
1.721     harmsja  6707: }
1.795     www      6708: 
                   6709: ul.LC_TabContent li {
1.911     bisitz   6710:   vertical-align:middle;
1.959     onken    6711:   padding: 0 16px 0 10px;
1.911     bisitz   6712:   background-color:$tabbg;
                   6713:   border-bottom:solid 1px $lg_border_color;
1.1020    raeburn  6714:   border-left: solid 1px $font;
1.721     harmsja  6715: }
1.795     www      6716: 
1.847     tempelho 6717: ul.LC_TabContent .right {
1.911     bisitz   6718:   float:right;
1.847     tempelho 6719: }
                   6720: 
1.911     bisitz   6721: ul.LC_TabContent li a,
                   6722: ul.LC_TabContent li {
                   6723:   color:rgb(47,47,47);
                   6724:   text-decoration:none;
                   6725:   font-size:95%;
                   6726:   font-weight:bold;
1.952     onken    6727:   min-height:20px;
                   6728: }
                   6729: 
1.959     onken    6730: ul.LC_TabContent li a:hover,
                   6731: ul.LC_TabContent li a:focus {
1.952     onken    6732:   color: $button_hover;
1.959     onken    6733:   background:none;
                   6734:   outline:none;
1.952     onken    6735: }
                   6736: 
                   6737: ul.LC_TabContent li:hover {
                   6738:   color: $button_hover;
                   6739:   cursor:pointer;
1.721     harmsja  6740: }
1.795     www      6741: 
1.911     bisitz   6742: ul.LC_TabContent li.active {
1.952     onken    6743:   color: $font;
1.911     bisitz   6744:   background:#FFFFFF url(/adm/lonIcons/open.gif) no-repeat scroll right center;
1.952     onken    6745:   border-bottom:solid 1px #FFFFFF;
                   6746:   cursor: default;
1.744     ehlerst  6747: }
1.795     www      6748: 
1.959     onken    6749: ul.LC_TabContent li.active a {
                   6750:   color:$font;
                   6751:   background:#FFFFFF;
                   6752:   outline: none;
                   6753: }
1.1047    raeburn  6754: 
                   6755: ul.LC_TabContent li.goback {
                   6756:   float: left;
                   6757:   border-left: none;
                   6758: }
                   6759: 
1.870     tempelho 6760: #maincoursedoc {
1.911     bisitz   6761:   clear:both;
1.870     tempelho 6762: }
                   6763: 
                   6764: ul.LC_TabContentBigger {
1.911     bisitz   6765:   display:block;
                   6766:   list-style:none;
                   6767:   padding: 0;
1.870     tempelho 6768: }
                   6769: 
1.795     www      6770: ul.LC_TabContentBigger li {
1.911     bisitz   6771:   vertical-align:bottom;
                   6772:   height: 30px;
                   6773:   font-size:110%;
                   6774:   font-weight:bold;
                   6775:   color: #737373;
1.841     tempelho 6776: }
                   6777: 
1.957     onken    6778: ul.LC_TabContentBigger li.active {
                   6779:   position: relative;
                   6780:   top: 1px;
                   6781: }
                   6782: 
1.870     tempelho 6783: ul.LC_TabContentBigger li a {
1.911     bisitz   6784:   background:url('/adm/lonIcons/tabbgleft.gif') left bottom no-repeat;
                   6785:   height: 30px;
                   6786:   line-height: 30px;
                   6787:   text-align: center;
                   6788:   display: block;
                   6789:   text-decoration: none;
1.958     onken    6790:   outline: none;  
1.741     harmsja  6791: }
1.795     www      6792: 
1.870     tempelho 6793: ul.LC_TabContentBigger li.active a {
1.911     bisitz   6794:   background:url('/adm/lonIcons/tabbgleft.gif') left top no-repeat;
                   6795:   color:$font;
1.744     ehlerst  6796: }
1.795     www      6797: 
1.870     tempelho 6798: ul.LC_TabContentBigger li b {
1.911     bisitz   6799:   background: url('/adm/lonIcons/tabbgright.gif') no-repeat right bottom;
                   6800:   display: block;
                   6801:   float: left;
                   6802:   padding: 0 30px;
1.957     onken    6803:   border-bottom: 1px solid $lg_border_color;
1.870     tempelho 6804: }
                   6805: 
1.956     onken    6806: ul.LC_TabContentBigger li:hover b {
                   6807:   color:$button_hover;
                   6808: }
                   6809: 
1.870     tempelho 6810: ul.LC_TabContentBigger li.active b {
1.911     bisitz   6811:   background:url('/adm/lonIcons/tabbgright.gif') right top no-repeat;
                   6812:   color:$font;
1.957     onken    6813:   border: 0;
1.741     harmsja  6814: }
1.693     droeschl 6815: 
1.870     tempelho 6816: 
1.862     bisitz   6817: ul.LC_CourseBreadcrumbs {
                   6818:   background: $sidebg;
1.1020    raeburn  6819:   height: 2em;
1.862     bisitz   6820:   padding-left: 10px;
1.1020    raeburn  6821:   margin: 0;
1.862     bisitz   6822:   list-style-position: inside;
                   6823: }
                   6824: 
1.911     bisitz   6825: ol#LC_MenuBreadcrumbs,
1.862     bisitz   6826: ol#LC_PathBreadcrumbs {
1.911     bisitz   6827:   padding-left: 10px;
                   6828:   margin: 0;
1.933     droeschl 6829:   height: 2.5em;  /* equal to #LC_breadcrumbs line-height */
1.693     droeschl 6830: }
                   6831: 
1.911     bisitz   6832: ol#LC_MenuBreadcrumbs li,
                   6833: ol#LC_PathBreadcrumbs li,
1.862     bisitz   6834: ul.LC_CourseBreadcrumbs li {
1.911     bisitz   6835:   display: inline;
1.933     droeschl 6836:   white-space: normal;  
1.693     droeschl 6837: }
                   6838: 
1.823     bisitz   6839: ol#LC_MenuBreadcrumbs li a,
1.862     bisitz   6840: ul.LC_CourseBreadcrumbs li a {
1.911     bisitz   6841:   text-decoration: none;
                   6842:   font-size:90%;
1.693     droeschl 6843: }
1.795     www      6844: 
1.969     droeschl 6845: ol#LC_MenuBreadcrumbs h1 {
                   6846:   display: inline;
                   6847:   font-size: 90%;
                   6848:   line-height: 2.5em;
                   6849:   margin: 0;
                   6850:   padding: 0;
                   6851: }
                   6852: 
1.795     www      6853: ol#LC_PathBreadcrumbs li a {
1.911     bisitz   6854:   text-decoration:none;
                   6855:   font-size:100%;
                   6856:   font-weight:bold;
1.693     droeschl 6857: }
1.795     www      6858: 
1.840     bisitz   6859: .LC_Box {
1.911     bisitz   6860:   border: solid 1px $lg_border_color;
                   6861:   padding: 0 10px 10px 10px;
1.746     neumanie 6862: }
1.795     www      6863: 
1.1020    raeburn  6864: .LC_DocsBox {
                   6865:   border: solid 1px $lg_border_color;
                   6866:   padding: 0 0 10px 10px;
                   6867: }
                   6868: 
1.795     www      6869: .LC_AboutMe_Image {
1.911     bisitz   6870:   float:left;
                   6871:   margin-right:10px;
1.747     neumanie 6872: }
1.795     www      6873: 
                   6874: .LC_Clear_AboutMe_Image {
1.911     bisitz   6875:   clear:left;
1.747     neumanie 6876: }
1.795     www      6877: 
1.721     harmsja  6878: dl.LC_ListStyleClean dt {
1.911     bisitz   6879:   padding-right: 5px;
                   6880:   display: table-header-group;
1.693     droeschl 6881: }
                   6882: 
1.721     harmsja  6883: dl.LC_ListStyleClean dd {
1.911     bisitz   6884:   display: table-row;
1.693     droeschl 6885: }
                   6886: 
1.721     harmsja  6887: .LC_ListStyleClean,
                   6888: .LC_ListStyleSimple,
                   6889: .LC_ListStyleNormal,
1.795     www      6890: .LC_ListStyleSpecial {
1.911     bisitz   6891:   /* display:block; */
                   6892:   list-style-position: inside;
                   6893:   list-style-type: none;
                   6894:   overflow: hidden;
                   6895:   padding: 0;
1.693     droeschl 6896: }
                   6897: 
1.721     harmsja  6898: .LC_ListStyleSimple li,
                   6899: .LC_ListStyleSimple dd,
                   6900: .LC_ListStyleNormal li,
                   6901: .LC_ListStyleNormal dd,
                   6902: .LC_ListStyleSpecial li,
1.795     www      6903: .LC_ListStyleSpecial dd {
1.911     bisitz   6904:   margin: 0;
                   6905:   padding: 5px 5px 5px 10px;
                   6906:   clear: both;
1.693     droeschl 6907: }
                   6908: 
1.721     harmsja  6909: .LC_ListStyleClean li,
                   6910: .LC_ListStyleClean dd {
1.911     bisitz   6911:   padding-top: 0;
                   6912:   padding-bottom: 0;
1.693     droeschl 6913: }
                   6914: 
1.721     harmsja  6915: .LC_ListStyleSimple dd,
1.795     www      6916: .LC_ListStyleSimple li {
1.911     bisitz   6917:   border-bottom: solid 1px $lg_border_color;
1.693     droeschl 6918: }
                   6919: 
1.721     harmsja  6920: .LC_ListStyleSpecial li,
                   6921: .LC_ListStyleSpecial dd {
1.911     bisitz   6922:   list-style-type: none;
                   6923:   background-color: RGB(220, 220, 220);
                   6924:   margin-bottom: 4px;
1.693     droeschl 6925: }
                   6926: 
1.721     harmsja  6927: table.LC_SimpleTable {
1.911     bisitz   6928:   margin:5px;
                   6929:   border:solid 1px $lg_border_color;
1.795     www      6930: }
1.693     droeschl 6931: 
1.721     harmsja  6932: table.LC_SimpleTable tr {
1.911     bisitz   6933:   padding: 0;
                   6934:   border:solid 1px $lg_border_color;
1.693     droeschl 6935: }
1.795     www      6936: 
                   6937: table.LC_SimpleTable thead {
1.911     bisitz   6938:   background:rgb(220,220,220);
1.693     droeschl 6939: }
                   6940: 
1.721     harmsja  6941: div.LC_columnSection {
1.911     bisitz   6942:   display: block;
                   6943:   clear: both;
                   6944:   overflow: hidden;
                   6945:   margin: 0;
1.693     droeschl 6946: }
                   6947: 
1.721     harmsja  6948: div.LC_columnSection>* {
1.911     bisitz   6949:   float: left;
                   6950:   margin: 10px 20px 10px 0;
                   6951:   overflow:hidden;
1.693     droeschl 6952: }
1.721     harmsja  6953: 
1.795     www      6954: table em {
1.911     bisitz   6955:   font-weight: bold;
                   6956:   font-style: normal;
1.748     schulted 6957: }
1.795     www      6958: 
1.779     bisitz   6959: table.LC_tableBrowseRes,
1.795     www      6960: table.LC_tableOfContent {
1.911     bisitz   6961:   border:none;
                   6962:   border-spacing: 1px;
                   6963:   padding: 3px;
                   6964:   background-color: #FFFFFF;
                   6965:   font-size: 90%;
1.753     droeschl 6966: }
1.789     droeschl 6967: 
1.911     bisitz   6968: table.LC_tableOfContent {
                   6969:   border-collapse: collapse;
1.789     droeschl 6970: }
                   6971: 
1.771     droeschl 6972: table.LC_tableBrowseRes a,
1.768     schulted 6973: table.LC_tableOfContent a {
1.911     bisitz   6974:   background-color: transparent;
                   6975:   text-decoration: none;
1.753     droeschl 6976: }
                   6977: 
1.795     www      6978: table.LC_tableOfContent img {
1.911     bisitz   6979:   border: none;
                   6980:   height: 1.3em;
                   6981:   vertical-align: text-bottom;
                   6982:   margin-right: 0.3em;
1.753     droeschl 6983: }
1.757     schulted 6984: 
1.795     www      6985: a#LC_content_toolbar_firsthomework {
1.911     bisitz   6986:   background-image:url(/res/adm/pages/open-first-problem.gif);
1.774     ehlerst  6987: }
                   6988: 
1.795     www      6989: a#LC_content_toolbar_everything {
1.911     bisitz   6990:   background-image:url(/res/adm/pages/show-all.gif);
1.774     ehlerst  6991: }
                   6992: 
1.795     www      6993: a#LC_content_toolbar_uncompleted {
1.911     bisitz   6994:   background-image:url(/res/adm/pages/show-incomplete-problems.gif);
1.774     ehlerst  6995: }
                   6996: 
1.795     www      6997: #LC_content_toolbar_clearbubbles {
1.911     bisitz   6998:   background-image:url(/res/adm/pages/mark-discussionentries-read.gif);
1.774     ehlerst  6999: }
                   7000: 
1.795     www      7001: a#LC_content_toolbar_changefolder {
1.911     bisitz   7002:   background : url(/res/adm/pages/close-all-folders.gif) top center ;
1.757     schulted 7003: }
                   7004: 
1.795     www      7005: a#LC_content_toolbar_changefolder_toggled {
1.911     bisitz   7006:   background-image:url(/res/adm/pages/open-all-folders.gif);
1.757     schulted 7007: }
                   7008: 
1.1043    raeburn  7009: a#LC_content_toolbar_edittoplevel {
                   7010:   background-image:url(/res/adm/pages/edittoplevel.gif);
                   7011: }
                   7012: 
1.795     www      7013: ul#LC_toolbar li a:hover {
1.911     bisitz   7014:   background-position: bottom center;
1.757     schulted 7015: }
                   7016: 
1.795     www      7017: ul#LC_toolbar {
1.911     bisitz   7018:   padding: 0;
                   7019:   margin: 2px;
                   7020:   list-style:none;
                   7021:   position:relative;
                   7022:   background-color:white;
1.1075.2.9  raeburn  7023:   overflow: auto;
1.757     schulted 7024: }
                   7025: 
1.795     www      7026: ul#LC_toolbar li {
1.911     bisitz   7027:   border:1px solid white;
                   7028:   padding: 0;
                   7029:   margin: 0;
                   7030:   float: left;
                   7031:   display:inline;
                   7032:   vertical-align:middle;
1.1075.2.9  raeburn  7033:   white-space: nowrap;
1.911     bisitz   7034: }
1.757     schulted 7035: 
1.783     amueller 7036: 
1.795     www      7037: a.LC_toolbarItem {
1.911     bisitz   7038:   display:block;
                   7039:   padding: 0;
                   7040:   margin: 0;
                   7041:   height: 32px;
                   7042:   width: 32px;
                   7043:   color:white;
                   7044:   border: none;
                   7045:   background-repeat:no-repeat;
                   7046:   background-color:transparent;
1.757     schulted 7047: }
                   7048: 
1.915     droeschl 7049: ul.LC_funclist {
                   7050:     margin: 0;
                   7051:     padding: 0.5em 1em 0.5em 0;
                   7052: }
                   7053: 
1.933     droeschl 7054: ul.LC_funclist > li:first-child {
                   7055:     font-weight:bold; 
                   7056:     margin-left:0.8em;
                   7057: }
                   7058: 
1.915     droeschl 7059: ul.LC_funclist + ul.LC_funclist {
                   7060:     /* 
                   7061:        left border as a seperator if we have more than
                   7062:        one list 
                   7063:     */
                   7064:     border-left: 1px solid $sidebg;
                   7065:     /* 
                   7066:        this hides the left border behind the border of the 
                   7067:        outer box if element is wrapped to the next 'line' 
                   7068:     */
                   7069:     margin-left: -1px;
                   7070: }
                   7071: 
1.843     bisitz   7072: ul.LC_funclist li {
1.915     droeschl 7073:   display: inline;
1.782     bisitz   7074:   white-space: nowrap;
1.915     droeschl 7075:   margin: 0 0 0 25px;
                   7076:   line-height: 150%;
1.782     bisitz   7077: }
                   7078: 
1.974     wenzelju 7079: .LC_hidden {
                   7080:   display: none;
                   7081: }
                   7082: 
1.1030    www      7083: .LCmodal-overlay {
                   7084: 		position:fixed;
                   7085: 		top:0;
                   7086: 		right:0;
                   7087: 		bottom:0;
                   7088: 		left:0;
                   7089: 		height:100%;
                   7090: 		width:100%;
                   7091: 		margin:0;
                   7092: 		padding:0;
                   7093: 		background:#999;
                   7094: 		opacity:.75;
                   7095: 		filter: alpha(opacity=75);
                   7096: 		-moz-opacity: 0.75;
                   7097: 		z-index:101;
                   7098: }
                   7099: 
                   7100: * html .LCmodal-overlay {   
                   7101: 		position: absolute;
                   7102: 		height: expression(document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + 'px');
                   7103: }
                   7104: 
                   7105: .LCmodal-window {
                   7106: 		position:fixed;
                   7107: 		top:50%;
                   7108: 		left:50%;
                   7109: 		margin:0;
                   7110: 		padding:0;
                   7111: 		z-index:102;
                   7112: 	}
                   7113: 
                   7114: * html .LCmodal-window {
                   7115: 		position:absolute;
                   7116: }
                   7117: 
                   7118: .LCclose-window {
                   7119: 		position:absolute;
                   7120: 		width:32px;
                   7121: 		height:32px;
                   7122: 		right:8px;
                   7123: 		top:8px;
                   7124: 		background:transparent url('/res/adm/pages/process-stop.png') no-repeat scroll right top;
                   7125: 		text-indent:-99999px;
                   7126: 		overflow:hidden;
                   7127: 		cursor:pointer;
                   7128: }
                   7129: 
1.1075.2.17  raeburn  7130: /*
                   7131:   styles used by TTH when "Default set of options to pass to tth/m
                   7132:   when converting TeX" in course settings has been set
                   7133: 
                   7134:   option passed: -t
                   7135: 
                   7136: */
                   7137: 
                   7138: td div.comp { margin-top: -0.6ex; margin-bottom: -1ex;}
                   7139: td div.comb { margin-top: -0.6ex; margin-bottom: -.6ex;}
                   7140: td div.hrcomp { line-height: 0.9; margin-top: -0.8ex; margin-bottom: -1ex;}
                   7141: td div.norm {line-height:normal;}
                   7142: 
                   7143: /*
                   7144:   option passed -y3
                   7145: */
                   7146: 
                   7147: span.roman {font-family: serif; font-style: normal; font-weight: normal;}
                   7148: span.overacc2 {position: relative;  left: .8em; top: -1.2ex;}
                   7149: span.overacc1 {position: relative;  left: .6em; top: -1.2ex;}
                   7150: 
1.343     albertel 7151: END
                   7152: }
                   7153: 
1.306     albertel 7154: =pod
                   7155: 
                   7156: =item * &headtag()
                   7157: 
                   7158: Returns a uniform footer for LON-CAPA web pages.
                   7159: 
1.307     albertel 7160: Inputs: $title - optional title for the head
                   7161:         $head_extra - optional extra HTML to put inside the <head>
1.315     albertel 7162:         $args - optional arguments
1.319     albertel 7163:             force_register - if is true call registerurl so the remote is 
                   7164:                              informed
1.415     albertel 7165:             redirect       -> array ref of
                   7166:                                    1- seconds before redirect occurs
                   7167:                                    2- url to redirect to
                   7168:                                    3- whether the side effect should occur
1.315     albertel 7169:                            (side effect of setting 
                   7170:                                $env{'internal.head.redirect'} to the url 
                   7171:                                redirected too)
1.352     albertel 7172:             domain         -> force to color decorate a page for a specific
                   7173:                                domain
                   7174:             function       -> force usage of a specific rolish color scheme
                   7175:             bgcolor        -> override the default page bgcolor
1.460     albertel 7176:             no_auto_mt_title
                   7177:                            -> prevent &mt()ing the title arg
1.464     albertel 7178: 
1.306     albertel 7179: =cut
                   7180: 
                   7181: sub headtag {
1.313     albertel 7182:     my ($title,$head_extra,$args) = @_;
1.306     albertel 7183:     
1.363     albertel 7184:     my $function = $args->{'function'} || &get_users_function();
                   7185:     my $domain   = $args->{'domain'}   || &determinedomain();
                   7186:     my $bgcolor  = $args->{'bgcolor'}  || &designparm($function.'.pgbg',$domain);
1.418     albertel 7187:     my $url = join(':',$env{'user.name'},$env{'user.domain'},
1.458     albertel 7188: 		   $Apache::lonnet::perlvar{'lonVersion'},
1.531     albertel 7189: 		   #time(),
1.418     albertel 7190: 		   $env{'environment.color.timestamp'},
1.363     albertel 7191: 		   $function,$domain,$bgcolor);
                   7192: 
1.369     www      7193:     $url = '/adm/css/'.&escape($url).'.css';
1.363     albertel 7194: 
1.308     albertel 7195:     my $result =
                   7196: 	'<head>'.
1.461     albertel 7197: 	&font_settings();
1.319     albertel 7198: 
1.1064    raeburn  7199:     my $inhibitprint = &print_suppression();
                   7200: 
1.461     albertel 7201:     if (!$args->{'frameset'}) {
                   7202: 	$result .= &Apache::lonhtmlcommon::htmlareaheaders();
                   7203:     }
1.1075.2.12  raeburn  7204:     if ($args->{'force_register'}) {
                   7205:         $result .= &Apache::lonmenu::registerurl(1);
1.319     albertel 7206:     }
1.436     albertel 7207:     if (!$args->{'no_nav_bar'} 
                   7208: 	&& !$args->{'only_body'}
                   7209: 	&& !$args->{'frameset'}) {
                   7210: 	$result .= &help_menu_js();
1.1032    www      7211:         $result.=&modal_window();
1.1038    www      7212:         $result.=&togglebox_script();
1.1034    www      7213:         $result.=&wishlist_window();
1.1041    www      7214:         $result.=&LCprogressbarUpdate_script();
1.1034    www      7215:     } else {
                   7216:         if ($args->{'add_modal'}) {
                   7217:            $result.=&modal_window();
                   7218:         }
                   7219:         if ($args->{'add_wishlist'}) {
                   7220:            $result.=&wishlist_window();
                   7221:         }
1.1038    www      7222:         if ($args->{'add_togglebox'}) {
                   7223:            $result.=&togglebox_script();
                   7224:         }
1.1041    www      7225:         if ($args->{'add_progressbar'}) {
                   7226:            $result.=&LCprogressbarUpdate_script();
                   7227:         }
1.436     albertel 7228:     }
1.314     albertel 7229:     if (ref($args->{'redirect'})) {
1.414     albertel 7230: 	my ($time,$url,$inhibit_continue) = @{$args->{'redirect'}};
1.315     albertel 7231: 	$url = &Apache::lonenc::check_encrypt($url);
1.414     albertel 7232: 	if (!$inhibit_continue) {
                   7233: 	    $env{'internal.head.redirect'} = $url;
                   7234: 	}
1.313     albertel 7235: 	$result.=<<ADDMETA
                   7236: <meta http-equiv="pragma" content="no-cache" />
1.344     albertel 7237: <meta http-equiv="Refresh" content="$time; url=$url" />
1.313     albertel 7238: ADDMETA
                   7239:     }
1.306     albertel 7240:     if (!defined($title)) {
                   7241: 	$title = 'The LearningOnline Network with CAPA';
                   7242:     }
1.460     albertel 7243:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
                   7244:     $result .= '<title> LON-CAPA '.$title.'</title>'
1.414     albertel 7245: 	.'<link rel="stylesheet" type="text/css" href="'.$url.'" />'
1.1064    raeburn  7246:         .$inhibitprint
1.414     albertel 7247: 	.$head_extra;
1.962     droeschl 7248:     return $result.'</head>';
1.306     albertel 7249: }
                   7250: 
                   7251: =pod
                   7252: 
1.340     albertel 7253: =item * &font_settings()
                   7254: 
                   7255: Returns neccessary <meta> to set the proper encoding
                   7256: 
                   7257: Inputs: none
                   7258: 
                   7259: =cut
                   7260: 
                   7261: sub font_settings {
                   7262:     my $headerstring='';
1.647     www      7263:     if (!$env{'browser.mathml'} && $env{'browser.unicode'}) {
1.340     albertel 7264: 	$headerstring.=
                   7265: 	    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
                   7266:     }
                   7267:     return $headerstring;
                   7268: }
                   7269: 
1.341     albertel 7270: =pod
                   7271: 
1.1064    raeburn  7272: =item * &print_suppression()
                   7273: 
                   7274: In course context returns css which causes the body to be blank when media="print",
                   7275: if printout generation is unavailable for the current resource.
                   7276: 
                   7277: This could be because:
                   7278: 
                   7279: (a) printstartdate is in the future
                   7280: 
                   7281: (b) printenddate is in the past
                   7282: 
                   7283: (c) there is an active exam block with "printout"
                   7284: functionality blocked
                   7285: 
                   7286: Users with pav, pfo or evb privileges are exempt.
                   7287: 
                   7288: Inputs: none
                   7289: 
                   7290: =cut
                   7291: 
                   7292: 
                   7293: sub print_suppression {
                   7294:     my $noprint;
                   7295:     if ($env{'request.course.id'}) {
                   7296:         my $scope = $env{'request.course.id'};
                   7297:         if ((&Apache::lonnet::allowed('pav',$scope)) ||
                   7298:             (&Apache::lonnet::allowed('pfo',$scope))) {
                   7299:             return;
                   7300:         }
                   7301:         if ($env{'request.course.sec'} ne '') {
                   7302:             $scope .= "/$env{'request.course.sec'}";
                   7303:             if ((&Apache::lonnet::allowed('pav',$scope)) ||
                   7304:                 (&Apache::lonnet::allowed('pfo',$scope))) {
1.1065    raeburn  7305:                 return;
1.1064    raeburn  7306:             }
                   7307:         }
                   7308:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7309:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.1065    raeburn  7310:         my $blocked = &blocking_status('printout',$cnum,$cdom);
1.1064    raeburn  7311:         if ($blocked) {
                   7312:             my $checkrole = "cm./$cdom/$cnum";
                   7313:             if ($env{'request.course.sec'} ne '') {
                   7314:                 $checkrole .= "/$env{'request.course.sec'}";
                   7315:             }
                   7316:             unless ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
                   7317:                     ($env{'request.role'} !~ m{^st\./$cdom/$cnum})) {
                   7318:                 $noprint = 1;
                   7319:             }
                   7320:         }
                   7321:         unless ($noprint) {
                   7322:             my $symb = &Apache::lonnet::symbread();
                   7323:             if ($symb ne '') {
                   7324:                 my $navmap = Apache::lonnavmaps::navmap->new();
                   7325:                 if (ref($navmap)) {
                   7326:                     my $res = $navmap->getBySymb($symb);
                   7327:                     if (ref($res)) {
                   7328:                         if (!$res->resprintable()) {
                   7329:                             $noprint = 1;
                   7330:                         }
                   7331:                     }
                   7332:                 }
                   7333:             }
                   7334:         }
                   7335:         if ($noprint) {
                   7336:             return <<"ENDSTYLE";
                   7337: <style type="text/css" media="print">
                   7338:     body { display:none }
                   7339: </style>
                   7340: ENDSTYLE
                   7341:         }
                   7342:     }
                   7343:     return;
                   7344: }
                   7345: 
                   7346: =pod
                   7347: 
1.341     albertel 7348: =item * &xml_begin()
                   7349: 
                   7350: Returns the needed doctype and <html>
                   7351: 
                   7352: Inputs: none
                   7353: 
                   7354: =cut
                   7355: 
                   7356: sub xml_begin {
                   7357:     my $output='';
                   7358: 
                   7359:     if ($env{'browser.mathml'}) {
                   7360: 	$output='<?xml version="1.0"?>'
                   7361:             #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
                   7362: #            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                   7363:             
                   7364: #	    .'<!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">] >'
                   7365: 	    .'<!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">'
                   7366:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                   7367: 	    .'xmlns="http://www.w3.org/1999/xhtml">';
                   7368:     } else {
1.849     bisitz   7369: 	$output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
                   7370:            .'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
1.341     albertel 7371:     }
                   7372:     return $output;
                   7373: }
1.340     albertel 7374: 
                   7375: =pod
                   7376: 
1.306     albertel 7377: =item * &start_page()
                   7378: 
                   7379: Returns a complete <html> .. <body> section for LON-CAPA web pages.
                   7380: 
1.648     raeburn  7381: Inputs:
                   7382: 
                   7383: =over 4
                   7384: 
                   7385: $title - optional title for the page
                   7386: 
                   7387: $head_extra - optional extra HTML to incude inside the <head>
                   7388: 
                   7389: $args - additional optional args supported are:
                   7390: 
                   7391: =over 8
                   7392: 
                   7393:              only_body      -> is true will set &bodytag() onlybodytag
1.317     albertel 7394:                                     arg on
1.814     bisitz   7395:              no_nav_bar     -> is true will set &bodytag() no_nav_bar arg on
1.648     raeburn  7396:              add_entries    -> additional attributes to add to the  <body>
                   7397:              domain         -> force to color decorate a page for a 
1.317     albertel 7398:                                     specific domain
1.648     raeburn  7399:              function       -> force usage of a specific rolish color
1.317     albertel 7400:                                     scheme
1.648     raeburn  7401:              redirect       -> see &headtag()
                   7402:              bgcolor        -> override the default page bg color
                   7403:              js_ready       -> return a string ready for being used in 
1.317     albertel 7404:                                     a javascript writeln
1.648     raeburn  7405:              html_encode    -> return a string ready for being used in 
1.320     albertel 7406:                                     a html attribute
1.648     raeburn  7407:              force_register -> if is true will turn on the &bodytag()
1.317     albertel 7408:                                     $forcereg arg
1.648     raeburn  7409:              frameset       -> if true will start with a <frameset>
1.330     albertel 7410:                                     rather than <body>
1.648     raeburn  7411:              skip_phases    -> hash ref of 
1.338     albertel 7412:                                     head -> skip the <html><head> generation
                   7413:                                     body -> skip all <body> generation
1.1075.2.12  raeburn  7414:              no_inline_link -> if true and in remote mode, don't show the
                   7415:                                     'Switch To Inline Menu' link
1.648     raeburn  7416:              no_auto_mt_title -> prevent &mt()ing the title arg
                   7417:              inherit_jsmath -> when creating popup window in a page,
                   7418:                                     should it have jsmath forced on by the
                   7419:                                     current page
1.867     kalberla 7420:              bread_crumbs ->             Array containing breadcrumbs
1.983     raeburn  7421:              bread_crumbs_component ->  if exists show it as headline else show only the breadcrumbs
1.1075.2.15  raeburn  7422:              group          -> includes the current group, if page is for a
                   7423:                                specific group
1.361     albertel 7424: 
1.648     raeburn  7425: =back
1.460     albertel 7426: 
1.648     raeburn  7427: =back
1.562     albertel 7428: 
1.306     albertel 7429: =cut
                   7430: 
                   7431: sub start_page {
1.309     albertel 7432:     my ($title,$head_extra,$args) = @_;
1.318     albertel 7433:     #&Apache::lonnet::logthis("start_page ".join(':',caller(0)));
1.319     albertel 7434: 
1.315     albertel 7435:     $env{'internal.start_page'}++;
1.1075.2.15  raeburn  7436:     my ($result,@advtools);
1.964     droeschl 7437: 
1.338     albertel 7438:     if (! exists($args->{'skip_phases'}{'head'}) ) {
1.1030    www      7439:         $result .= &xml_begin() . &headtag($title, $head_extra, $args);
1.338     albertel 7440:     }
                   7441:     
                   7442:     if (! exists($args->{'skip_phases'}{'body'}) ) {
                   7443: 	if ($args->{'frameset'}) {
                   7444: 	    my $attr_string = &make_attr_string($args->{'force_register'},
                   7445: 						$args->{'add_entries'});
                   7446: 	    $result .= "\n<frameset $attr_string>\n";
1.831     bisitz   7447:         } else {
                   7448:             $result .=
                   7449:                 &bodytag($title, 
                   7450:                          $args->{'function'},       $args->{'add_entries'},
                   7451:                          $args->{'only_body'},      $args->{'domain'},
                   7452:                          $args->{'force_register'}, $args->{'no_nav_bar'},
1.1075.2.12  raeburn  7453:                          $args->{'bgcolor'},        $args->{'no_inline_link'},
1.1075.2.15  raeburn  7454:                          $args,                     \@advtools);
1.831     bisitz   7455:         }
1.330     albertel 7456:     }
1.338     albertel 7457: 
1.315     albertel 7458:     if ($args->{'js_ready'}) {
1.713     kaisler  7459: 		$result = &js_ready($result);
1.315     albertel 7460:     }
1.320     albertel 7461:     if ($args->{'html_encode'}) {
1.713     kaisler  7462: 		$result = &html_encode($result);
                   7463:     }
                   7464: 
1.813     bisitz   7465:     # Preparation for new and consistent functionlist at top of screen
                   7466:     # if ($args->{'functionlist'}) {
                   7467:     #            $result .= &build_functionlist();
                   7468:     #}
                   7469: 
1.964     droeschl 7470:     # Don't add anything more if only_body wanted or in const space
                   7471:     return $result if    $args->{'only_body'} 
                   7472:                       || $env{'request.state'} eq 'construct';
1.813     bisitz   7473: 
                   7474:     #Breadcrumbs
1.758     kaisler  7475:     if (exists($args->{'bread_crumbs'}) or exists($args->{'bread_crumbs_component'})) {
                   7476: 		&Apache::lonhtmlcommon::clear_breadcrumbs();
                   7477: 		#if any br links exists, add them to the breadcrumbs
                   7478: 		if (exists($args->{'bread_crumbs'}) and ref($args->{'bread_crumbs'}) eq 'ARRAY') {         
                   7479: 			foreach my $crumb (@{$args->{'bread_crumbs'}}){
                   7480: 				&Apache::lonhtmlcommon::add_breadcrumb($crumb);
                   7481: 			}
                   7482: 		}
1.1075.2.19  raeburn  7483:                 # if @advtools array contains items add then to the breadcrumbs
                   7484:                 if (@advtools > 0) {
                   7485:                     &Apache::lonmenu::advtools_crumbs(@advtools);
                   7486:                 }
1.758     kaisler  7487: 
                   7488: 		#if bread_crumbs_component exists show it as headline else show only the breadcrumbs
                   7489: 		if(exists($args->{'bread_crumbs_component'})){
                   7490: 			$result .= &Apache::lonhtmlcommon::breadcrumbs($args->{'bread_crumbs_component'});
                   7491: 		}else{
                   7492: 			$result .= &Apache::lonhtmlcommon::breadcrumbs();
                   7493: 		}
1.320     albertel 7494:     }
1.315     albertel 7495:     return $result;
1.306     albertel 7496: }
                   7497: 
                   7498: sub end_page {
1.315     albertel 7499:     my ($args) = @_;
                   7500:     $env{'internal.end_page'}++;
1.330     albertel 7501:     my $result;
1.335     albertel 7502:     if ($args->{'discussion'}) {
                   7503: 	my ($target,$parser);
                   7504: 	if (ref($args->{'discussion'})) {
                   7505: 	    ($target,$parser) =($args->{'discussion'}{'target'},
                   7506: 				$args->{'discussion'}{'parser'});
                   7507: 	}
                   7508: 	$result .= &Apache::lonxml::xmlend($target,$parser);
                   7509:     }
1.330     albertel 7510:     if ($args->{'frameset'}) {
                   7511: 	$result .= '</frameset>';
                   7512:     } else {
1.635     raeburn  7513: 	$result .= &endbodytag($args);
1.330     albertel 7514:     }
1.1075.2.6  raeburn  7515:     unless ($args->{'notbody'}) {
                   7516:         $result .= "\n</html>";
                   7517:     }
1.330     albertel 7518: 
1.315     albertel 7519:     if ($args->{'js_ready'}) {
1.317     albertel 7520: 	$result = &js_ready($result);
1.315     albertel 7521:     }
1.335     albertel 7522: 
1.320     albertel 7523:     if ($args->{'html_encode'}) {
                   7524: 	$result = &html_encode($result);
                   7525:     }
1.335     albertel 7526: 
1.315     albertel 7527:     return $result;
                   7528: }
                   7529: 
1.1034    www      7530: sub wishlist_window {
                   7531:     return(<<'ENDWISHLIST');
1.1046    raeburn  7532: <script type="text/javascript">
1.1034    www      7533: // <![CDATA[
                   7534: // <!-- BEGIN LON-CAPA Internal
                   7535: function set_wishlistlink(title, path) {
                   7536:     if (!title) {
                   7537:         title = document.title;
                   7538:         title = title.replace(/^LON-CAPA /,'');
                   7539:     }
                   7540:     if (!path) {
                   7541:         path = location.pathname;
                   7542:     }
                   7543:     Win = window.open('/adm/wishlist?mode=newLink&setTitle='+title+'&setPath='+path,
                   7544:                       'wishlistNewLink','width=560,height=350,scrollbars=0');
                   7545: }
                   7546: // END LON-CAPA Internal -->
                   7547: // ]]>
                   7548: </script>
                   7549: ENDWISHLIST
                   7550: }
                   7551: 
1.1030    www      7552: sub modal_window {
                   7553:     return(<<'ENDMODAL');
1.1046    raeburn  7554: <script type="text/javascript">
1.1030    www      7555: // <![CDATA[
                   7556: // <!-- BEGIN LON-CAPA Internal
                   7557: var modalWindow = {
                   7558: 	parent:"body",
                   7559: 	windowId:null,
                   7560: 	content:null,
                   7561: 	width:null,
                   7562: 	height:null,
                   7563: 	close:function()
                   7564: 	{
                   7565: 	        $(".LCmodal-window").remove();
                   7566: 	        $(".LCmodal-overlay").remove();
                   7567: 	},
                   7568: 	open:function()
                   7569: 	{
                   7570: 		var modal = "";
                   7571: 		modal += "<div class=\"LCmodal-overlay\"></div>";
                   7572: 		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;\">";
                   7573: 		modal += this.content;
                   7574: 		modal += "</div>";	
                   7575: 
                   7576: 		$(this.parent).append(modal);
                   7577: 
                   7578: 		$(".LCmodal-window").append("<a class=\"LCclose-window\"></a>");
                   7579: 		$(".LCclose-window").click(function(){modalWindow.close();});
                   7580: 		$(".LCmodal-overlay").click(function(){modalWindow.close();});
                   7581: 	}
                   7582: };
1.1031    www      7583: 	var openMyModal = function(source,width,height,scrolling)
1.1030    www      7584: 	{
                   7585: 		modalWindow.windowId = "myModal";
                   7586: 		modalWindow.width = width;
                   7587: 		modalWindow.height = height;
1.1031    www      7588: 		modalWindow.content = "<iframe width='"+width+"' height='"+height+"' frameborder='0' scrolling='"+scrolling+"' allowtransparency='true' src='" + source + "'>&lt/iframe>";
1.1030    www      7589: 		modalWindow.open();
                   7590: 	};	
                   7591: // END LON-CAPA Internal -->
                   7592: // ]]>
                   7593: </script>
                   7594: ENDMODAL
                   7595: }
                   7596: 
                   7597: sub modal_link {
1.1052    www      7598:     my ($link,$linktext,$width,$height,$target,$scrolling,$title)=@_;
1.1030    www      7599:     unless ($width) { $width=480; }
                   7600:     unless ($height) { $height=400; }
1.1031    www      7601:     unless ($scrolling) { $scrolling='yes'; }
1.1074    raeburn  7602:     my $target_attr;
                   7603:     if (defined($target)) {
                   7604:         $target_attr = 'target="'.$target.'"';
                   7605:     }
                   7606:     return <<"ENDLINK";
                   7607: <a href="$link" $target_attr title="$title" onclick="javascript:openMyModal('$link',$width,$height,'$scrolling'); return false;">
                   7608:            $linktext</a>
                   7609: ENDLINK
1.1030    www      7610: }
                   7611: 
1.1032    www      7612: sub modal_adhoc_script {
                   7613:     my ($funcname,$width,$height,$content)=@_;
                   7614:     return (<<ENDADHOC);
1.1046    raeburn  7615: <script type="text/javascript">
1.1032    www      7616: // <![CDATA[
                   7617:         var $funcname = function()
                   7618:         {
                   7619:                 modalWindow.windowId = "myModal";
                   7620:                 modalWindow.width = $width;
                   7621:                 modalWindow.height = $height;
                   7622:                 modalWindow.content = '$content';
                   7623:                 modalWindow.open();
                   7624:         };  
                   7625: // ]]>
                   7626: </script>
                   7627: ENDADHOC
                   7628: }
                   7629: 
1.1041    www      7630: sub modal_adhoc_inner {
                   7631:     my ($funcname,$width,$height,$content)=@_;
                   7632:     my $innerwidth=$width-20;
                   7633:     $content=&js_ready(
1.1042    www      7634:                &start_page('Dialog',undef,{'only_body'=>1,'bgcolor'=>'#FFFFFF'}).
1.1041    www      7635:                  &start_scrollbox($width.'px',$innerwidth.'px',$height.'px').
                   7636:                     $content.
                   7637:                  &end_scrollbox().
                   7638:                &end_page()
                   7639:              );
                   7640:     return &modal_adhoc_script($funcname,$width,$height,$content);
                   7641: }
                   7642: 
                   7643: sub modal_adhoc_window {
                   7644:     my ($funcname,$width,$height,$content,$linktext)=@_;
                   7645:     return &modal_adhoc_inner($funcname,$width,$height,$content).
                   7646:            "<a href=\"javascript:$funcname();void(0);\">".$linktext."</a>";
                   7647: }
                   7648: 
                   7649: sub modal_adhoc_launch {
                   7650:     my ($funcname,$width,$height,$content)=@_;
                   7651:     return &modal_adhoc_inner($funcname,$width,$height,$content).(<<ENDLAUNCH);
                   7652: <script type="text/javascript">
                   7653: // <![CDATA[
                   7654: $funcname();
                   7655: // ]]>
                   7656: </script>
                   7657: ENDLAUNCH
                   7658: }
                   7659: 
                   7660: sub modal_adhoc_close {
                   7661:     return (<<ENDCLOSE);
                   7662: <script type="text/javascript">
                   7663: // <![CDATA[
                   7664: modalWindow.close();
                   7665: // ]]>
                   7666: </script>
                   7667: ENDCLOSE
                   7668: }
                   7669: 
1.1038    www      7670: sub togglebox_script {
                   7671:    return(<<ENDTOGGLE);
                   7672: <script type="text/javascript"> 
                   7673: // <![CDATA[
                   7674: function LCtoggleDisplay(id,hidetext,showtext) {
                   7675:    link = document.getElementById(id + "link").childNodes[0];
                   7676:    with (document.getElementById(id).style) {
                   7677:       if (display == "none" ) {
                   7678:           display = "inline";
                   7679:           link.nodeValue = hidetext;
                   7680:         } else {
                   7681:           display = "none";
                   7682:           link.nodeValue = showtext;
                   7683:        }
                   7684:    }
                   7685: }
                   7686: // ]]>
                   7687: </script>
                   7688: ENDTOGGLE
                   7689: }
                   7690: 
1.1039    www      7691: sub start_togglebox {
                   7692:     my ($id,$heading,$headerbg,$hidetext,$showtext)=@_;
                   7693:     unless ($heading) { $heading=''; } else { $heading.=' '; }
                   7694:     unless ($showtext) { $showtext=&mt('show'); }
                   7695:     unless ($hidetext) { $hidetext=&mt('hide'); }
                   7696:     unless ($headerbg) { $headerbg='#FFFFFF'; }
                   7697:     return &start_data_table().
                   7698:            &start_data_table_header_row().
                   7699:            '<td bgcolor="'.$headerbg.'">'.$heading.
                   7700:            '[<a id="'.$id.'link" href="javascript:LCtoggleDisplay(\''.$id.'\',\''.$hidetext.'\',\''.
                   7701:            $showtext.'\')">'.$showtext.'</a>]</td>'.
                   7702:            &end_data_table_header_row().
                   7703:            '<tr id="'.$id.'" style="display:none""><td>';
                   7704: }
                   7705: 
                   7706: sub end_togglebox {
                   7707:     return '</td></tr>'.&end_data_table();
                   7708: }
                   7709: 
1.1041    www      7710: sub LCprogressbar_script {
1.1045    www      7711:    my ($id)=@_;
1.1041    www      7712:    return(<<ENDPROGRESS);
                   7713: <script type="text/javascript">
                   7714: // <![CDATA[
1.1045    www      7715: \$('#progressbar$id').progressbar({
1.1041    www      7716:   value: 0,
                   7717:   change: function(event, ui) {
                   7718:     var newVal = \$(this).progressbar('option', 'value');
                   7719:     \$('.pblabel', this).text(LCprogressTxt);
                   7720:   }
                   7721: });
                   7722: // ]]>
                   7723: </script>
                   7724: ENDPROGRESS
                   7725: }
                   7726: 
                   7727: sub LCprogressbarUpdate_script {
                   7728:    return(<<ENDPROGRESSUPDATE);
                   7729: <style type="text/css">
                   7730: .ui-progressbar { position:relative; }
                   7731: .pblabel { position: absolute; width: 100%; text-align: center; line-height: 1.9em; }
                   7732: </style>
                   7733: <script type="text/javascript">
                   7734: // <![CDATA[
1.1045    www      7735: var LCprogressTxt='---';
                   7736: 
                   7737: function LCupdateProgress(percent,progresstext,id) {
1.1041    www      7738:    LCprogressTxt=progresstext;
1.1045    www      7739:    \$('#progressbar'+id).progressbar('value',percent);
1.1041    www      7740: }
                   7741: // ]]>
                   7742: </script>
                   7743: ENDPROGRESSUPDATE
                   7744: }
                   7745: 
1.1042    www      7746: my $LClastpercent;
1.1045    www      7747: my $LCidcnt;
                   7748: my $LCcurrentid;
1.1042    www      7749: 
1.1041    www      7750: sub LCprogressbar {
1.1042    www      7751:     my ($r)=(@_);
                   7752:     $LClastpercent=0;
1.1045    www      7753:     $LCidcnt++;
                   7754:     $LCcurrentid=$$.'_'.$LCidcnt;
1.1041    www      7755:     my $starting=&mt('Starting');
                   7756:     my $content=(<<ENDPROGBAR);
                   7757: <p>
1.1045    www      7758:   <div id="progressbar$LCcurrentid">
1.1041    www      7759:     <span class="pblabel">$starting</span>
                   7760:   </div>
                   7761: </p>
                   7762: ENDPROGBAR
1.1045    www      7763:     &r_print($r,$content.&LCprogressbar_script($LCcurrentid));
1.1041    www      7764: }
                   7765: 
                   7766: sub LCprogressbarUpdate {
1.1042    www      7767:     my ($r,$val,$text)=@_;
                   7768:     unless ($val) { 
                   7769:        if ($LClastpercent) {
                   7770:            $val=$LClastpercent;
                   7771:        } else {
                   7772:            $val=0;
                   7773:        }
                   7774:     }
1.1041    www      7775:     if ($val<0) { $val=0; }
                   7776:     if ($val>100) { $val=0; }
1.1042    www      7777:     $LClastpercent=$val;
1.1041    www      7778:     unless ($text) { $text=$val.'%'; }
                   7779:     $text=&js_ready($text);
1.1044    www      7780:     &r_print($r,<<ENDUPDATE);
1.1041    www      7781: <script type="text/javascript">
                   7782: // <![CDATA[
1.1045    www      7783: LCupdateProgress($val,'$text','$LCcurrentid');
1.1041    www      7784: // ]]>
                   7785: </script>
                   7786: ENDUPDATE
1.1035    www      7787: }
                   7788: 
1.1042    www      7789: sub LCprogressbarClose {
                   7790:     my ($r)=@_;
                   7791:     $LClastpercent=0;
1.1044    www      7792:     &r_print($r,<<ENDCLOSE);
1.1042    www      7793: <script type="text/javascript">
                   7794: // <![CDATA[
1.1045    www      7795: \$("#progressbar$LCcurrentid").hide('slow'); 
1.1042    www      7796: // ]]>
                   7797: </script>
                   7798: ENDCLOSE
1.1044    www      7799: }
                   7800: 
                   7801: sub r_print {
                   7802:     my ($r,$to_print)=@_;
                   7803:     if ($r) {
                   7804:       $r->print($to_print);
                   7805:       $r->rflush();
                   7806:     } else {
                   7807:       print($to_print);
                   7808:     }
1.1042    www      7809: }
                   7810: 
1.320     albertel 7811: sub html_encode {
                   7812:     my ($result) = @_;
                   7813: 
1.322     albertel 7814:     $result = &HTML::Entities::encode($result,'<>&"');
1.320     albertel 7815:     
                   7816:     return $result;
                   7817: }
1.1044    www      7818: 
1.317     albertel 7819: sub js_ready {
                   7820:     my ($result) = @_;
                   7821: 
1.323     albertel 7822:     $result =~ s/[\n\r]/ /xmsg;
                   7823:     $result =~ s/\\/\\\\/xmsg;
                   7824:     $result =~ s/'/\\'/xmsg;
1.372     albertel 7825:     $result =~ s{</}{<\\/}xmsg;
1.317     albertel 7826:     
                   7827:     return $result;
                   7828: }
                   7829: 
1.315     albertel 7830: sub validate_page {
                   7831:     if (  exists($env{'internal.start_page'})
1.316     albertel 7832: 	  &&     $env{'internal.start_page'} > 1) {
                   7833: 	&Apache::lonnet::logthis('start_page called multiple times '.
1.318     albertel 7834: 				 $env{'internal.start_page'}.' '.
1.316     albertel 7835: 				 $ENV{'request.filename'});
1.315     albertel 7836:     }
                   7837:     if (  exists($env{'internal.end_page'})
1.316     albertel 7838: 	  &&     $env{'internal.end_page'} > 1) {
                   7839: 	&Apache::lonnet::logthis('end_page called multiple times '.
1.318     albertel 7840: 				 $env{'internal.end_page'}.' '.
1.316     albertel 7841: 				 $env{'request.filename'});
1.315     albertel 7842:     }
                   7843:     if (     exists($env{'internal.start_page'})
                   7844: 	&& ! exists($env{'internal.end_page'})) {
1.316     albertel 7845: 	&Apache::lonnet::logthis('start_page called without end_page '.
                   7846: 				 $env{'request.filename'});
1.315     albertel 7847:     }
                   7848:     if (   ! exists($env{'internal.start_page'})
                   7849: 	&&   exists($env{'internal.end_page'})) {
1.316     albertel 7850: 	&Apache::lonnet::logthis('end_page called without start_page'.
                   7851: 				 $env{'request.filename'});
1.315     albertel 7852:     }
1.306     albertel 7853: }
1.315     albertel 7854: 
1.996     www      7855: 
                   7856: sub start_scrollbox {
1.1075    raeburn  7857:     my ($outerwidth,$width,$height,$id,$bgcolor)=@_;
1.998     raeburn  7858:     unless ($outerwidth) { $outerwidth='520px'; }
                   7859:     unless ($width) { $width='500px'; }
                   7860:     unless ($height) { $height='200px'; }
1.1075    raeburn  7861:     my ($table_id,$div_id,$tdcol);
1.1018    raeburn  7862:     if ($id ne '') {
1.1020    raeburn  7863:         $table_id = " id='table_$id'";
                   7864:         $div_id = " id='div_$id'";
1.1018    raeburn  7865:     }
1.1075    raeburn  7866:     if ($bgcolor ne '') {
                   7867:         $tdcol = "background-color: $bgcolor;";
                   7868:     }
                   7869:     return <<"END";
                   7870: <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>
                   7871: END
1.996     www      7872: }
                   7873: 
                   7874: sub end_scrollbox {
1.1036    www      7875:     return '</div></td></tr></table>';
1.996     www      7876: }
                   7877: 
1.318     albertel 7878: sub simple_error_page {
                   7879:     my ($r,$title,$msg) = @_;
                   7880:     my $page =
                   7881: 	&Apache::loncommon::start_page($title).
1.1075.2.15  raeburn  7882: 	'<p class="LC_error">'.&mt($msg).'</p>'.
1.318     albertel 7883: 	&Apache::loncommon::end_page();
                   7884:     if (ref($r)) {
                   7885: 	$r->print($page);
1.327     albertel 7886: 	return;
1.318     albertel 7887:     }
                   7888:     return $page;
                   7889: }
1.347     albertel 7890: 
                   7891: {
1.610     albertel 7892:     my @row_count;
1.961     onken    7893: 
                   7894:     sub start_data_table_count {
                   7895:         unshift(@row_count, 0);
                   7896:         return;
                   7897:     }
                   7898: 
                   7899:     sub end_data_table_count {
                   7900:         shift(@row_count);
                   7901:         return;
                   7902:     }
                   7903: 
1.347     albertel 7904:     sub start_data_table {
1.1018    raeburn  7905: 	my ($add_class,$id) = @_;
1.422     albertel 7906: 	my $css_class = (join(' ','LC_data_table',$add_class));
1.1018    raeburn  7907:         my $table_id;
                   7908:         if (defined($id)) {
                   7909:             $table_id = ' id="'.$id.'"';
                   7910:         }
1.961     onken    7911: 	&start_data_table_count();
1.1018    raeburn  7912: 	return '<table class="'.$css_class.'"'.$table_id.'>'."\n";
1.347     albertel 7913:     }
                   7914: 
                   7915:     sub end_data_table {
1.961     onken    7916: 	&end_data_table_count();
1.389     albertel 7917: 	return '</table>'."\n";;
1.347     albertel 7918:     }
                   7919: 
                   7920:     sub start_data_table_row {
1.974     wenzelju 7921: 	my ($add_class, $id) = @_;
1.610     albertel 7922: 	$row_count[0]++;
                   7923: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.900     bisitz   7924: 	$css_class = (join(' ',$css_class,$add_class)) unless ($add_class eq '');
1.974     wenzelju 7925:         $id = (' id="'.$id.'"') unless ($id eq '');
                   7926:         return  '<tr class="'.$css_class.'"'.$id.'>'."\n";
1.347     albertel 7927:     }
1.471     banghart 7928:     
                   7929:     sub continue_data_table_row {
1.974     wenzelju 7930: 	my ($add_class, $id) = @_;
1.610     albertel 7931: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.974     wenzelju 7932: 	$css_class = (join(' ',$css_class,$add_class)) unless ($add_class eq '');
                   7933:         $id = (' id="'.$id.'"') unless ($id eq '');
                   7934:         return  '<tr class="'.$css_class.'"'.$id.'>'."\n";
1.471     banghart 7935:     }
1.347     albertel 7936: 
                   7937:     sub end_data_table_row {
1.389     albertel 7938: 	return '</tr>'."\n";;
1.347     albertel 7939:     }
1.367     www      7940: 
1.421     albertel 7941:     sub start_data_table_empty_row {
1.707     bisitz   7942: #	$row_count[0]++;
1.421     albertel 7943: 	return  '<tr class="LC_empty_row" >'."\n";;
                   7944:     }
                   7945: 
                   7946:     sub end_data_table_empty_row {
                   7947: 	return '</tr>'."\n";;
                   7948:     }
                   7949: 
1.367     www      7950:     sub start_data_table_header_row {
1.389     albertel 7951: 	return  '<tr class="LC_header_row">'."\n";;
1.367     www      7952:     }
                   7953: 
                   7954:     sub end_data_table_header_row {
1.389     albertel 7955: 	return '</tr>'."\n";;
1.367     www      7956:     }
1.890     droeschl 7957: 
                   7958:     sub data_table_caption {
                   7959:         my $caption = shift;
                   7960:         return "<caption class=\"LC_caption\">$caption</caption>";
                   7961:     }
1.347     albertel 7962: }
                   7963: 
1.548     albertel 7964: =pod
                   7965: 
                   7966: =item * &inhibit_menu_check($arg)
                   7967: 
                   7968: Checks for a inhibitmenu state and generates output to preserve it
                   7969: 
                   7970: Inputs:         $arg - can be any of
                   7971:                      - undef - in which case the return value is a string 
                   7972:                                to add  into arguments list of a uri
                   7973:                      - 'input' - in which case the return value is a HTML
                   7974:                                  <form> <input> field of type hidden to
                   7975:                                  preserve the value
                   7976:                      - a url - in which case the return value is the url with
                   7977:                                the neccesary cgi args added to preserve the
                   7978:                                inhibitmenu state
                   7979:                      - a ref to a url - no return value, but the string is
                   7980:                                         updated to include the neccessary cgi
                   7981:                                         args to preserve the inhibitmenu state
                   7982: 
                   7983: =cut
                   7984: 
                   7985: sub inhibit_menu_check {
                   7986:     my ($arg) = @_;
                   7987:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
                   7988:     if ($arg eq 'input') {
                   7989: 	if ($env{'form.inhibitmenu'}) {
                   7990: 	    return '<input type="hidden" name="inhibitmenu" value="'.$env{'form.inhibitmenu'}.'" />';
                   7991: 	} else {
                   7992: 	    return
                   7993: 	}
                   7994:     }
                   7995:     if ($env{'form.inhibitmenu'}) {
                   7996: 	if (ref($arg)) {
                   7997: 	    $$arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   7998: 	} elsif ($arg eq '') {
                   7999: 	    $arg .= 'inhibitmenu='.$env{'form.inhibitmenu'};
                   8000: 	} else {
                   8001: 	    $arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   8002: 	}
                   8003:     }
                   8004:     if (!ref($arg)) {
                   8005: 	return $arg;
                   8006:     }
                   8007: }
                   8008: 
1.251     albertel 8009: ###############################################
1.182     matthew  8010: 
                   8011: =pod
                   8012: 
1.549     albertel 8013: =back
                   8014: 
                   8015: =head1 User Information Routines
                   8016: 
                   8017: =over 4
                   8018: 
1.405     albertel 8019: =item * &get_users_function()
1.182     matthew  8020: 
                   8021: Used by &bodytag to determine the current users primary role.
                   8022: Returns either 'student','coordinator','admin', or 'author'.
                   8023: 
                   8024: =cut
                   8025: 
                   8026: ###############################################
                   8027: sub get_users_function {
1.815     tempelho 8028:     my $function = 'norole';
1.818     tempelho 8029:     if ($env{'request.role'}=~/^(st)/) {
                   8030:         $function='student';
                   8031:     }
1.907     raeburn  8032:     if ($env{'request.role'}=~/^(cc|co|in|ta|ep)/) {
1.182     matthew  8033:         $function='coordinator';
                   8034:     }
1.258     albertel 8035:     if ($env{'request.role'}=~/^(su|dc|ad|li)/) {
1.182     matthew  8036:         $function='admin';
                   8037:     }
1.826     bisitz   8038:     if (($env{'request.role'}=~/^(au|ca|aa)/) ||
1.1025    raeburn  8039:         ($ENV{'REQUEST_URI'}=~ m{/^(/priv)})) {
1.182     matthew  8040:         $function='author';
                   8041:     }
                   8042:     return $function;
1.54      www      8043: }
1.99      www      8044: 
                   8045: ###############################################
                   8046: 
1.233     raeburn  8047: =pod
                   8048: 
1.821     raeburn  8049: =item * &show_course()
                   8050: 
                   8051: Used by lonmenu.pm and lonroles.pm to determine whether to use the word
                   8052: 'Courses' or 'Roles' in inline navigation and on screen displaying user's roles.
                   8053: 
                   8054: Inputs:
                   8055: None
                   8056: 
                   8057: Outputs:
                   8058: Scalar: 1 if 'Course' to be used, 0 otherwise.
                   8059: 
                   8060: =cut
                   8061: 
                   8062: ###############################################
                   8063: sub show_course {
                   8064:     my $course = !$env{'user.adv'};
                   8065:     if (!$env{'user.adv'}) {
                   8066:         foreach my $env (keys(%env)) {
                   8067:             next if ($env !~ m/^user\.priv\./);
                   8068:             if ($env !~ m/^user\.priv\.(?:st|cm)/) {
                   8069:                 $course = 0;
                   8070:                 last;
                   8071:             }
                   8072:         }
                   8073:     }
                   8074:     return $course;
                   8075: }
                   8076: 
                   8077: ###############################################
                   8078: 
                   8079: =pod
                   8080: 
1.542     raeburn  8081: =item * &check_user_status()
1.274     raeburn  8082: 
                   8083: Determines current status of supplied role for a
                   8084: specific user. Roles can be active, previous or future.
                   8085: 
                   8086: Inputs: 
                   8087: user's domain, user's username, course's domain,
1.375     raeburn  8088: course's number, optional section ID.
1.274     raeburn  8089: 
                   8090: Outputs:
                   8091: role status: active, previous or future. 
                   8092: 
                   8093: =cut
                   8094: 
                   8095: sub check_user_status {
1.412     raeburn  8096:     my ($udom,$uname,$cdom,$crs,$role,$sec) = @_;
1.1073    raeburn  8097:     my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
1.274     raeburn  8098:     my @uroles = keys %userinfo;
                   8099:     my $srchstr;
                   8100:     my $active_chk = 'none';
1.412     raeburn  8101:     my $now = time;
1.274     raeburn  8102:     if (@uroles > 0) {
1.908     raeburn  8103:         if (($role eq 'cc') || ($role eq 'co') || ($sec eq '') || (!defined($sec))) {
1.274     raeburn  8104:             $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
                   8105:         } else {
1.412     raeburn  8106:             $srchstr = '/'.$cdom.'/'.$crs.'/'.$sec.'_'.$role;
                   8107:         }
                   8108:         if (grep/^\Q$srchstr\E$/,@uroles) {
1.274     raeburn  8109:             my $role_end = 0;
                   8110:             my $role_start = 0;
                   8111:             $active_chk = 'active';
1.412     raeburn  8112:             if ($userinfo{$srchstr} =~ m/^\Q$role\E_(\d+)/) {
                   8113:                 $role_end = $1;
                   8114:                 if ($userinfo{$srchstr} =~ m/^\Q$role\E_\Q$role_end\E_(\d+)$/) {
                   8115:                     $role_start = $1;
1.274     raeburn  8116:                 }
                   8117:             }
                   8118:             if ($role_start > 0) {
1.412     raeburn  8119:                 if ($now < $role_start) {
1.274     raeburn  8120:                     $active_chk = 'future';
                   8121:                 }
                   8122:             }
                   8123:             if ($role_end > 0) {
1.412     raeburn  8124:                 if ($now > $role_end) {
1.274     raeburn  8125:                     $active_chk = 'previous';
                   8126:                 }
                   8127:             }
                   8128:         }
                   8129:     }
                   8130:     return $active_chk;
                   8131: }
                   8132: 
                   8133: ###############################################
                   8134: 
                   8135: =pod
                   8136: 
1.405     albertel 8137: =item * &get_sections()
1.233     raeburn  8138: 
                   8139: Determines all the sections for a course including
                   8140: sections with students and sections containing other roles.
1.419     raeburn  8141: Incoming parameters: 
                   8142: 
                   8143: 1. domain
                   8144: 2. course number 
                   8145: 3. reference to array containing roles for which sections should 
                   8146: be gathered (optional).
                   8147: 4. reference to array containing status types for which sections 
                   8148: should be gathered (optional).
                   8149: 
                   8150: If the third argument is undefined, sections are gathered for any role. 
                   8151: If the fourth argument is undefined, sections are gathered for any status.
                   8152: Permissible values are 'active' or 'future' or 'previous'.
1.233     raeburn  8153:  
1.374     raeburn  8154: Returns section hash (keys are section IDs, values are
                   8155: number of users in each section), subject to the
1.419     raeburn  8156: optional roles filter, optional status filter 
1.233     raeburn  8157: 
                   8158: =cut
                   8159: 
                   8160: ###############################################
                   8161: sub get_sections {
1.419     raeburn  8162:     my ($cdom,$cnum,$possible_roles,$possible_status) = @_;
1.366     albertel 8163:     if (!defined($cdom) || !defined($cnum)) {
                   8164:         my $cid =  $env{'request.course.id'};
                   8165: 
                   8166: 	return if (!defined($cid));
                   8167: 
                   8168:         $cdom = $env{'course.'.$cid.'.domain'};
                   8169:         $cnum = $env{'course.'.$cid.'.num'};
                   8170:     }
                   8171: 
                   8172:     my %sectioncount;
1.419     raeburn  8173:     my $now = time;
1.240     albertel 8174: 
1.366     albertel 8175:     if (!defined($possible_roles) || (grep(/^st$/,@$possible_roles))) {
1.276     albertel 8176: 	my ($classlist) = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1.240     albertel 8177: 	my $sec_index = &Apache::loncoursedata::CL_SECTION();
                   8178: 	my $status_index = &Apache::loncoursedata::CL_STATUS();
1.419     raeburn  8179:         my $start_index = &Apache::loncoursedata::CL_START();
                   8180:         my $end_index = &Apache::loncoursedata::CL_END();
                   8181:         my $status;
1.366     albertel 8182: 	while (my ($student,$data) = each(%$classlist)) {
1.419     raeburn  8183: 	    my ($section,$stu_status,$start,$end) = ($data->[$sec_index],
                   8184: 				                     $data->[$status_index],
                   8185:                                                      $data->[$start_index],
                   8186:                                                      $data->[$end_index]);
                   8187:             if ($stu_status eq 'Active') {
                   8188:                 $status = 'active';
                   8189:             } elsif ($end < $now) {
                   8190:                 $status = 'previous';
                   8191:             } elsif ($start > $now) {
                   8192:                 $status = 'future';
                   8193:             } 
                   8194: 	    if ($section ne '-1' && $section !~ /^\s*$/) {
                   8195:                 if ((!defined($possible_status)) || (($status ne '') && 
                   8196:                     (grep/^\Q$status\E$/,@{$possible_status}))) { 
                   8197: 		    $sectioncount{$section}++;
                   8198:                 }
1.240     albertel 8199: 	    }
                   8200: 	}
                   8201:     }
                   8202:     my %courseroles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   8203:     foreach my $user (sort(keys(%courseroles))) {
                   8204: 	if ($user !~ /^(\w{2})/) { next; }
                   8205: 	my ($role) = ($user =~ /^(\w{2})/);
                   8206: 	if ($possible_roles && !(grep(/^$role$/,@$possible_roles))) { next; }
1.419     raeburn  8207: 	my ($section,$status);
1.240     albertel 8208: 	if ($role eq 'cr' &&
                   8209: 	    $user =~ m-^$role/[^/]*/[^/]*/[^/]*:[^:]*:[^:]*:(\w+)-) {
                   8210: 	    $section=$1;
                   8211: 	}
                   8212: 	if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
                   8213: 	if (!defined($section) || $section eq '-1') { next; }
1.419     raeburn  8214:         my ($end,$start) = ($courseroles{$user} =~ /^([^:]*):([^:]*)$/);
                   8215:         if ($end == -1 && $start == -1) {
                   8216:             next; #deleted role
                   8217:         }
                   8218:         if (!defined($possible_status)) { 
                   8219:             $sectioncount{$section}++;
                   8220:         } else {
                   8221:             if ((!$end || $end >= $now) && (!$start || $start <= $now)) {
                   8222:                 $status = 'active';
                   8223:             } elsif ($end < $now) {
                   8224:                 $status = 'future';
                   8225:             } elsif ($start > $now) {
                   8226:                 $status = 'previous';
                   8227:             }
                   8228:             if (($status ne '') && (grep/^\Q$status\E$/,@{$possible_status})) {
                   8229:                 $sectioncount{$section}++;
                   8230:             }
                   8231:         }
1.233     raeburn  8232:     }
1.366     albertel 8233:     return %sectioncount;
1.233     raeburn  8234: }
                   8235: 
1.274     raeburn  8236: ###############################################
1.294     raeburn  8237: 
                   8238: =pod
1.405     albertel 8239: 
                   8240: =item * &get_course_users()
                   8241: 
1.275     raeburn  8242: Retrieves usernames:domains for users in the specified course
                   8243: with specific role(s), and access status. 
                   8244: 
                   8245: Incoming parameters:
1.277     albertel 8246: 1. course domain
                   8247: 2. course number
                   8248: 3. access status: users must have - either active, 
1.275     raeburn  8249: previous, future, or all.
1.277     albertel 8250: 4. reference to array of permissible roles
1.288     raeburn  8251: 5. reference to array of section restrictions (optional)
                   8252: 6. reference to results object (hash of hashes).
                   8253: 7. reference to optional userdata hash
1.609     raeburn  8254: 8. reference to optional statushash
1.630     raeburn  8255: 9. flag if privileged users (except those set to unhide in
                   8256:    course settings) should be excluded    
1.609     raeburn  8257: Keys of top level results hash are roles.
1.275     raeburn  8258: Keys of inner hashes are username:domain, with 
                   8259: values set to access type.
1.288     raeburn  8260: Optional userdata hash returns an array with arguments in the 
                   8261: same order as loncoursedata::get_classlist() for student data.
                   8262: 
1.609     raeburn  8263: Optional statushash returns
                   8264: 
1.288     raeburn  8265: Entries for end, start, section and status are blank because
                   8266: of the possibility of multiple values for non-student roles.
                   8267: 
1.275     raeburn  8268: =cut
1.405     albertel 8269: 
1.275     raeburn  8270: ###############################################
1.405     albertel 8271: 
1.275     raeburn  8272: sub get_course_users {
1.630     raeburn  8273:     my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata,$statushash,$hidepriv) = @_;
1.288     raeburn  8274:     my %idx = ();
1.419     raeburn  8275:     my %seclists;
1.288     raeburn  8276: 
                   8277:     $idx{udom} = &Apache::loncoursedata::CL_SDOM();
                   8278:     $idx{uname} =  &Apache::loncoursedata::CL_SNAME();
                   8279:     $idx{end} = &Apache::loncoursedata::CL_END();
                   8280:     $idx{start} = &Apache::loncoursedata::CL_START();
                   8281:     $idx{id} = &Apache::loncoursedata::CL_ID();
                   8282:     $idx{section} = &Apache::loncoursedata::CL_SECTION();
                   8283:     $idx{fullname} = &Apache::loncoursedata::CL_FULLNAME();
                   8284:     $idx{status} = &Apache::loncoursedata::CL_STATUS();
                   8285: 
1.290     albertel 8286:     if (grep(/^st$/,@{$roles})) {
1.276     albertel 8287:         my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist($cdom,$cnum);
1.278     raeburn  8288:         my $now = time;
1.277     albertel 8289:         foreach my $student (keys(%{$classlist})) {
1.288     raeburn  8290:             my $match = 0;
1.412     raeburn  8291:             my $secmatch = 0;
1.419     raeburn  8292:             my $section = $$classlist{$student}[$idx{section}];
1.609     raeburn  8293:             my $status = $$classlist{$student}[$idx{status}];
1.419     raeburn  8294:             if ($section eq '') {
                   8295:                 $section = 'none';
                   8296:             }
1.291     albertel 8297:             if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 8298:                 if (grep(/^all$/,@{$sections})) {
1.412     raeburn  8299:                     $secmatch = 1;
                   8300:                 } elsif ($$classlist{$student}[$idx{section}] eq '') {
1.420     albertel 8301:                     if (grep(/^none$/,@{$sections})) {
1.412     raeburn  8302:                         $secmatch = 1;
                   8303:                     }
                   8304:                 } else {  
1.419     raeburn  8305: 		    if (grep(/^\Q$section\E$/,@{$sections})) {
1.412     raeburn  8306: 		        $secmatch = 1;
                   8307:                     }
1.290     albertel 8308: 		}
1.412     raeburn  8309:                 if (!$secmatch) {
                   8310:                     next;
                   8311:                 }
1.419     raeburn  8312:             }
1.275     raeburn  8313:             if (defined($$types{'active'})) {
1.288     raeburn  8314:                 if ($$classlist{$student}[$idx{status}] eq 'Active') {
1.275     raeburn  8315:                     push(@{$$users{st}{$student}},'active');
1.288     raeburn  8316:                     $match = 1;
1.275     raeburn  8317:                 }
                   8318:             }
                   8319:             if (defined($$types{'previous'})) {
1.609     raeburn  8320:                 if ($$classlist{$student}[$idx{status}] eq 'Expired') {
1.275     raeburn  8321:                     push(@{$$users{st}{$student}},'previous');
1.288     raeburn  8322:                     $match = 1;
1.275     raeburn  8323:                 }
                   8324:             }
                   8325:             if (defined($$types{'future'})) {
1.609     raeburn  8326:                 if ($$classlist{$student}[$idx{status}] eq 'Future') {
1.275     raeburn  8327:                     push(@{$$users{st}{$student}},'future');
1.288     raeburn  8328:                     $match = 1;
1.275     raeburn  8329:                 }
                   8330:             }
1.609     raeburn  8331:             if ($match) {
                   8332:                 push(@{$seclists{$student}},$section);
                   8333:                 if (ref($userdata) eq 'HASH') {
                   8334:                     $$userdata{$student} = $$classlist{$student};
                   8335:                 }
                   8336:                 if (ref($statushash) eq 'HASH') {
                   8337:                     $statushash->{$student}{'st'}{$section} = $status;
                   8338:                 }
1.288     raeburn  8339:             }
1.275     raeburn  8340:         }
                   8341:     }
1.412     raeburn  8342:     if ((@{$roles} > 1) || ((@{$roles} == 1) && ($$roles[0] ne "st"))) {
1.439     raeburn  8343:         my %coursepersonnel = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   8344:         my $now = time;
1.609     raeburn  8345:         my %displaystatus = ( previous => 'Expired',
                   8346:                               active   => 'Active',
                   8347:                               future   => 'Future',
                   8348:                             );
1.630     raeburn  8349:         my %nothide;
                   8350:         if ($hidepriv) {
                   8351:             my %coursehash=&Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                   8352:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   8353:                 if ($user !~ /:/) {
                   8354:                     $nothide{join(':',split(/[\@]/,$user))}=1;
                   8355:                 } else {
                   8356:                     $nothide{$user} = 1;
                   8357:                 }
                   8358:             }
                   8359:         }
1.439     raeburn  8360:         foreach my $person (sort(keys(%coursepersonnel))) {
1.288     raeburn  8361:             my $match = 0;
1.412     raeburn  8362:             my $secmatch = 0;
1.439     raeburn  8363:             my $status;
1.412     raeburn  8364:             my ($role,$user,$usec) = ($person =~ /^([^:]*):([^:]+:[^:]+):([^:]*)/);
1.275     raeburn  8365:             $user =~ s/:$//;
1.439     raeburn  8366:             my ($end,$start) = split(/:/,$coursepersonnel{$person});
                   8367:             if ($end == -1 || $start == -1) {
                   8368:                 next;
                   8369:             }
                   8370:             if (($role) && ((grep(/^\Q$role\E$/,@{$roles})) ||
                   8371:                 (grep(/^cr$/,@{$roles}) && $role =~ /^cr\//))) {
1.412     raeburn  8372:                 my ($uname,$udom) = split(/:/,$user);
                   8373:                 if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 8374:                     if (grep(/^all$/,@{$sections})) {
1.412     raeburn  8375:                         $secmatch = 1;
                   8376:                     } elsif ($usec eq '') {
1.420     albertel 8377:                         if (grep(/^none$/,@{$sections})) {
1.412     raeburn  8378:                             $secmatch = 1;
                   8379:                         }
                   8380:                     } else {
                   8381:                         if (grep(/^\Q$usec\E$/,@{$sections})) {
                   8382:                             $secmatch = 1;
                   8383:                         }
                   8384:                     }
                   8385:                     if (!$secmatch) {
                   8386:                         next;
                   8387:                     }
1.288     raeburn  8388:                 }
1.419     raeburn  8389:                 if ($usec eq '') {
                   8390:                     $usec = 'none';
                   8391:                 }
1.275     raeburn  8392:                 if ($uname ne '' && $udom ne '') {
1.630     raeburn  8393:                     if ($hidepriv) {
                   8394:                         if ((&Apache::lonnet::privileged($uname,$udom)) &&
                   8395:                             (!$nothide{$uname.':'.$udom})) {
                   8396:                             next;
                   8397:                         }
                   8398:                     }
1.503     raeburn  8399:                     if ($end > 0 && $end < $now) {
1.439     raeburn  8400:                         $status = 'previous';
                   8401:                     } elsif ($start > $now) {
                   8402:                         $status = 'future';
                   8403:                     } else {
                   8404:                         $status = 'active';
                   8405:                     }
1.277     albertel 8406:                     foreach my $type (keys(%{$types})) { 
1.275     raeburn  8407:                         if ($status eq $type) {
1.420     albertel 8408:                             if (!grep(/^\Q$type\E$/,@{$$users{$role}{$user}})) {
1.419     raeburn  8409:                                 push(@{$$users{$role}{$user}},$type);
                   8410:                             }
1.288     raeburn  8411:                             $match = 1;
                   8412:                         }
                   8413:                     }
1.419     raeburn  8414:                     if (($match) && (ref($userdata) eq 'HASH')) {
                   8415:                         if (!exists($$userdata{$uname.':'.$udom})) {
                   8416: 			    &get_user_info($udom,$uname,\%idx,$userdata);
                   8417:                         }
1.420     albertel 8418:                         if (!grep(/^\Q$usec\E$/,@{$seclists{$uname.':'.$udom}})) {
1.419     raeburn  8419:                             push(@{$seclists{$uname.':'.$udom}},$usec);
                   8420:                         }
1.609     raeburn  8421:                         if (ref($statushash) eq 'HASH') {
                   8422:                             $statushash->{$uname.':'.$udom}{$role}{$usec} = $displaystatus{$status};
                   8423:                         }
1.275     raeburn  8424:                     }
                   8425:                 }
                   8426:             }
                   8427:         }
1.290     albertel 8428:         if (grep(/^ow$/,@{$roles})) {
1.279     raeburn  8429:             if ((defined($cdom)) && (defined($cnum))) {
                   8430:                 my %csettings = &Apache::lonnet::get('environment',['internal.courseowner'],$cdom,$cnum);
                   8431:                 if ( defined($csettings{'internal.courseowner'}) ) {
                   8432:                     my $owner = $csettings{'internal.courseowner'};
1.609     raeburn  8433:                     next if ($owner eq '');
                   8434:                     my ($ownername,$ownerdom);
                   8435:                     if ($owner =~ /^([^:]+):([^:]+)$/) {
                   8436:                         $ownername = $1;
                   8437:                         $ownerdom = $2;
                   8438:                     } else {
                   8439:                         $ownername = $owner;
                   8440:                         $ownerdom = $cdom;
                   8441:                         $owner = $ownername.':'.$ownerdom;
1.439     raeburn  8442:                     }
                   8443:                     @{$$users{'ow'}{$owner}} = 'any';
1.290     albertel 8444:                     if (defined($userdata) && 
1.609     raeburn  8445: 			!exists($$userdata{$owner})) {
                   8446: 			&get_user_info($ownerdom,$ownername,\%idx,$userdata);
                   8447:                         if (!grep(/^none$/,@{$seclists{$owner}})) {
                   8448:                             push(@{$seclists{$owner}},'none');
                   8449:                         }
                   8450:                         if (ref($statushash) eq 'HASH') {
                   8451:                             $statushash->{$owner}{'ow'}{'none'} = 'Any';
1.419     raeburn  8452:                         }
1.290     albertel 8453: 		    }
1.279     raeburn  8454:                 }
                   8455:             }
                   8456:         }
1.419     raeburn  8457:         foreach my $user (keys(%seclists)) {
                   8458:             @{$seclists{$user}} = (sort {$a <=> $b} @{$seclists{$user}});
                   8459:             $$userdata{$user}[$idx{section}] = join(',',@{$seclists{$user}});
                   8460:         }
1.275     raeburn  8461:     }
                   8462:     return;
                   8463: }
                   8464: 
1.288     raeburn  8465: sub get_user_info {
                   8466:     my ($udom,$uname,$idx,$userdata) = @_;
1.289     albertel 8467:     $$userdata{$uname.':'.$udom}[$$idx{fullname}] = 
                   8468: 	&plainname($uname,$udom,'lastname');
1.291     albertel 8469:     $$userdata{$uname.':'.$udom}[$$idx{uname}] = $uname;
1.297     raeburn  8470:     $$userdata{$uname.':'.$udom}[$$idx{udom}] = $udom;
1.609     raeburn  8471:     my %idhash =  &Apache::lonnet::idrget($udom,($uname));
                   8472:     $$userdata{$uname.':'.$udom}[$$idx{id}] = $idhash{$uname}; 
1.288     raeburn  8473:     return;
                   8474: }
1.275     raeburn  8475: 
1.472     raeburn  8476: ###############################################
                   8477: 
                   8478: =pod
                   8479: 
                   8480: =item * &get_user_quota()
                   8481: 
                   8482: Retrieves quota assigned for storage of portfolio files for a user  
                   8483: 
                   8484: Incoming parameters:
                   8485: 1. user's username
                   8486: 2. user's domain
                   8487: 
                   8488: Returns:
1.536     raeburn  8489: 1. Disk quota (in Mb) assigned to student.
                   8490: 2. (Optional) Type of setting: custom or default
                   8491:    (individually assigned or default for user's 
                   8492:    institutional status).
                   8493: 3. (Optional) - User's institutional status (e.g., faculty, staff
                   8494:    or student - types as defined in localenroll::inst_usertypes 
                   8495:    for user's domain, which determines default quota for user.
                   8496: 4. (Optional) - Default quota which would apply to the user.
1.472     raeburn  8497: 
                   8498: If a value has been stored in the user's environment, 
1.536     raeburn  8499: it will return that, otherwise it returns the maximal default
                   8500: defined for the user's instituional status(es) in the domain.
1.472     raeburn  8501: 
                   8502: =cut
                   8503: 
                   8504: ###############################################
                   8505: 
                   8506: 
                   8507: sub get_user_quota {
                   8508:     my ($uname,$udom) = @_;
1.536     raeburn  8509:     my ($quota,$quotatype,$settingstatus,$defquota);
1.472     raeburn  8510:     if (!defined($udom)) {
                   8511:         $udom = $env{'user.domain'};
                   8512:     }
                   8513:     if (!defined($uname)) {
                   8514:         $uname = $env{'user.name'};
                   8515:     }
                   8516:     if (($udom eq '' || $uname eq '') ||
                   8517:         ($udom eq 'public') && ($uname eq 'public')) {
                   8518:         $quota = 0;
1.536     raeburn  8519:         $quotatype = 'default';
                   8520:         $defquota = 0; 
1.472     raeburn  8521:     } else {
1.536     raeburn  8522:         my $inststatus;
1.472     raeburn  8523:         if ($udom eq $env{'user.domain'} && $uname eq $env{'user.name'}) {
                   8524:             $quota = $env{'environment.portfolioquota'};
1.536     raeburn  8525:             $inststatus = $env{'environment.inststatus'};
1.472     raeburn  8526:         } else {
1.536     raeburn  8527:             my %userenv = 
                   8528:                 &Apache::lonnet::get('environment',['portfolioquota',
                   8529:                                      'inststatus'],$udom,$uname);
1.472     raeburn  8530:             my ($tmp) = keys(%userenv);
                   8531:             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   8532:                 $quota = $userenv{'portfolioquota'};
1.536     raeburn  8533:                 $inststatus = $userenv{'inststatus'};
1.472     raeburn  8534:             } else {
                   8535:                 undef(%userenv);
                   8536:             }
                   8537:         }
1.536     raeburn  8538:         ($defquota,$settingstatus) = &default_quota($udom,$inststatus);
1.472     raeburn  8539:         if ($quota eq '') {
1.536     raeburn  8540:             $quota = $defquota;
                   8541:             $quotatype = 'default';
                   8542:         } else {
                   8543:             $quotatype = 'custom';
1.472     raeburn  8544:         }
                   8545:     }
1.536     raeburn  8546:     if (wantarray) {
                   8547:         return ($quota,$quotatype,$settingstatus,$defquota);
                   8548:     } else {
                   8549:         return $quota;
                   8550:     }
1.472     raeburn  8551: }
                   8552: 
                   8553: ###############################################
                   8554: 
                   8555: =pod
                   8556: 
                   8557: =item * &default_quota()
                   8558: 
1.536     raeburn  8559: Retrieves default quota assigned for storage of user portfolio files,
                   8560: given an (optional) user's institutional status.
1.472     raeburn  8561: 
                   8562: Incoming parameters:
                   8563: 1. domain
1.536     raeburn  8564: 2. (Optional) institutional status(es).  This is a : separated list of 
                   8565:    status types (e.g., faculty, staff, student etc.)
                   8566:    which apply to the user for whom the default is being retrieved.
                   8567:    If the institutional status string in undefined, the domain
                   8568:    default quota will be returned. 
1.472     raeburn  8569: 
                   8570: Returns:
                   8571: 1. Default disk quota (in Mb) for user portfolios in the domain.
1.536     raeburn  8572: 2. (Optional) institutional type which determined the value of the
                   8573:    default quota.
1.472     raeburn  8574: 
                   8575: If a value has been stored in the domain's configuration db,
                   8576: it will return that, otherwise it returns 20 (for backwards 
                   8577: compatibility with domains which have not set up a configuration
                   8578: db file; the original statically defined portfolio quota was 20 Mb). 
                   8579: 
1.536     raeburn  8580: If the user's status includes multiple types (e.g., staff and student),
                   8581: the largest default quota which applies to the user determines the
                   8582: default quota returned.
                   8583: 
1.780     raeburn  8584: =back
                   8585: 
1.472     raeburn  8586: =cut
                   8587: 
                   8588: ###############################################
                   8589: 
                   8590: 
                   8591: sub default_quota {
1.536     raeburn  8592:     my ($udom,$inststatus) = @_;
                   8593:     my ($defquota,$settingstatus);
                   8594:     my %quotahash = &Apache::lonnet::get_dom('configuration',
1.622     raeburn  8595:                                             ['quotas'],$udom);
                   8596:     if (ref($quotahash{'quotas'}) eq 'HASH') {
1.536     raeburn  8597:         if ($inststatus ne '') {
1.765     raeburn  8598:             my @statuses = map { &unescape($_); } split(/:/,$inststatus);
1.536     raeburn  8599:             foreach my $item (@statuses) {
1.711     raeburn  8600:                 if (ref($quotahash{'quotas'}{'defaultquota'}) eq 'HASH') {
                   8601:                     if ($quotahash{'quotas'}{'defaultquota'}{$item} ne '') {
                   8602:                         if ($defquota eq '') {
                   8603:                             $defquota = $quotahash{'quotas'}{'defaultquota'}{$item};
                   8604:                             $settingstatus = $item;
                   8605:                         } elsif ($quotahash{'quotas'}{'defaultquota'}{$item} > $defquota) {
                   8606:                             $defquota = $quotahash{'quotas'}{'defaultquota'}{$item};
                   8607:                             $settingstatus = $item;
                   8608:                         }
                   8609:                     }
                   8610:                 } else {
                   8611:                     if ($quotahash{'quotas'}{$item} ne '') {
                   8612:                         if ($defquota eq '') {
                   8613:                             $defquota = $quotahash{'quotas'}{$item};
                   8614:                             $settingstatus = $item;
                   8615:                         } elsif ($quotahash{'quotas'}{$item} > $defquota) {
                   8616:                             $defquota = $quotahash{'quotas'}{$item};
                   8617:                             $settingstatus = $item;
                   8618:                         }
1.536     raeburn  8619:                     }
                   8620:                 }
                   8621:             }
                   8622:         }
                   8623:         if ($defquota eq '') {
1.711     raeburn  8624:             if (ref($quotahash{'quotas'}{'defaultquota'}) eq 'HASH') {
                   8625:                 $defquota = $quotahash{'quotas'}{'defaultquota'}{'default'};
                   8626:             } else {
                   8627:                 $defquota = $quotahash{'quotas'}{'default'};
                   8628:             }
1.536     raeburn  8629:             $settingstatus = 'default';
                   8630:         }
                   8631:     } else {
                   8632:         $settingstatus = 'default';
                   8633:         $defquota = 20;
                   8634:     }
                   8635:     if (wantarray) {
                   8636:         return ($defquota,$settingstatus);
1.472     raeburn  8637:     } else {
1.536     raeburn  8638:         return $defquota;
1.472     raeburn  8639:     }
                   8640: }
                   8641: 
1.384     raeburn  8642: sub get_secgrprole_info {
                   8643:     my ($cdom,$cnum,$needroles,$type)  = @_;
                   8644:     my %sections_count = &get_sections($cdom,$cnum);
                   8645:     my @sections =  (sort {$a <=> $b} keys(%sections_count));
                   8646:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
                   8647:     my @groups = sort(keys(%curr_groups));
                   8648:     my $allroles = [];
                   8649:     my $rolehash;
                   8650:     my $accesshash = {
                   8651:                      active => 'Currently has access',
                   8652:                      future => 'Will have future access',
                   8653:                      previous => 'Previously had access',
                   8654:                   };
                   8655:     if ($needroles) {
                   8656:         $rolehash = {'all' => 'all'};
1.385     albertel 8657:         my %user_roles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   8658: 	if (&Apache::lonnet::error(%user_roles)) {
                   8659: 	    undef(%user_roles);
                   8660: 	}
                   8661:         foreach my $item (keys(%user_roles)) {
1.384     raeburn  8662:             my ($role)=split(/\:/,$item,2);
                   8663:             if ($role eq 'cr') { next; }
                   8664:             if ($role =~ /^cr/) {
                   8665:                 $$rolehash{$role} = (split('/',$role))[3];
                   8666:             } else {
                   8667:                 $$rolehash{$role} = &Apache::lonnet::plaintext($role,$type);
                   8668:             }
                   8669:         }
                   8670:         foreach my $key (sort(keys(%{$rolehash}))) {
                   8671:             push(@{$allroles},$key);
                   8672:         }
                   8673:         push (@{$allroles},'st');
                   8674:         $$rolehash{'st'} = &Apache::lonnet::plaintext('st',$type);
                   8675:     }
                   8676:     return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
                   8677: }
                   8678: 
1.555     raeburn  8679: sub user_picker {
1.994     raeburn  8680:     my ($dom,$srch,$forcenewuser,$caller,$cancreate,$usertype,$context) = @_;
1.555     raeburn  8681:     my $currdom = $dom;
                   8682:     my %curr_selected = (
                   8683:                         srchin => 'dom',
1.580     raeburn  8684:                         srchby => 'lastname',
1.555     raeburn  8685:                       );
                   8686:     my $srchterm;
1.625     raeburn  8687:     if ((ref($srch) eq 'HASH') && ($env{'form.origform'} ne 'crtusername')) {
1.555     raeburn  8688:         if ($srch->{'srchby'} ne '') {
                   8689:             $curr_selected{'srchby'} = $srch->{'srchby'};
                   8690:         }
                   8691:         if ($srch->{'srchin'} ne '') {
                   8692:             $curr_selected{'srchin'} = $srch->{'srchin'};
                   8693:         }
                   8694:         if ($srch->{'srchtype'} ne '') {
                   8695:             $curr_selected{'srchtype'} = $srch->{'srchtype'};
                   8696:         }
                   8697:         if ($srch->{'srchdomain'} ne '') {
                   8698:             $currdom = $srch->{'srchdomain'};
                   8699:         }
                   8700:         $srchterm = $srch->{'srchterm'};
                   8701:     }
                   8702:     my %lt=&Apache::lonlocal::texthash(
1.573     raeburn  8703:                     'usr'       => 'Search criteria',
1.563     raeburn  8704:                     'doma'      => 'Domain/institution to search',
1.558     albertel 8705:                     'uname'     => 'username',
                   8706:                     'lastname'  => 'last name',
1.555     raeburn  8707:                     'lastfirst' => 'last name, first name',
1.558     albertel 8708:                     'crs'       => 'in this course',
1.576     raeburn  8709:                     'dom'       => 'in selected LON-CAPA domain', 
1.558     albertel 8710:                     'alc'       => 'all LON-CAPA',
1.573     raeburn  8711:                     'instd'     => 'in institutional directory for selected domain',
1.558     albertel 8712:                     'exact'     => 'is',
                   8713:                     'contains'  => 'contains',
1.569     raeburn  8714:                     'begins'    => 'begins with',
1.571     raeburn  8715:                     'youm'      => "You must include some text to search for.",
                   8716:                     'thte'      => "The text you are searching for must contain at least two characters when using a 'begins' type search.",
                   8717:                     'thet'      => "The text you are searching for must contain at least three characters when using a 'contains' type search.",
                   8718:                     'yomc'      => "You must choose a domain when using an institutional directory search.",
                   8719:                     'ymcd'      => "You must choose a domain when using a domain search.",
                   8720:                     'whus'      => "When using searching by last,first you must include a comma as separator between last name and first name.",
                   8721:                     'whse'      => "When searching by last,first you must include at least one character in the first name.",
                   8722:                      'thfo'     => "The following need to be corrected before the search can be run:",
1.555     raeburn  8723:                                        );
1.563     raeburn  8724:     my $domform = &select_dom_form($currdom,'srchdomain',1,1);
                   8725:     my $srchinsel = ' <select name="srchin">';
1.555     raeburn  8726: 
                   8727:     my @srchins = ('crs','dom','alc','instd');
                   8728: 
                   8729:     foreach my $option (@srchins) {
                   8730:         # FIXME 'alc' option unavailable until 
                   8731:         #       loncreateuser::print_user_query_page()
                   8732:         #       has been completed.
                   8733:         next if ($option eq 'alc');
1.880     raeburn  8734:         next if (($option eq 'crs') && ($env{'form.form'} eq 'requestcrs'));  
1.555     raeburn  8735:         next if ($option eq 'crs' && !$env{'request.course.id'});
1.563     raeburn  8736:         if ($curr_selected{'srchin'} eq $option) {
                   8737:             $srchinsel .= ' 
                   8738:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   8739:         } else {
                   8740:             $srchinsel .= '
                   8741:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   8742:         }
1.555     raeburn  8743:     }
1.563     raeburn  8744:     $srchinsel .= "\n  </select>\n";
1.555     raeburn  8745: 
                   8746:     my $srchbysel =  ' <select name="srchby">';
1.580     raeburn  8747:     foreach my $option ('lastname','lastfirst','uname') {
1.555     raeburn  8748:         if ($curr_selected{'srchby'} eq $option) {
                   8749:             $srchbysel .= '
                   8750:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   8751:         } else {
                   8752:             $srchbysel .= '
                   8753:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   8754:          }
                   8755:     }
                   8756:     $srchbysel .= "\n  </select>\n";
                   8757: 
                   8758:     my $srchtypesel = ' <select name="srchtype">';
1.580     raeburn  8759:     foreach my $option ('begins','contains','exact') {
1.555     raeburn  8760:         if ($curr_selected{'srchtype'} eq $option) {
                   8761:             $srchtypesel .= '
                   8762:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   8763:         } else {
                   8764:             $srchtypesel .= '
                   8765:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   8766:         }
                   8767:     }
                   8768:     $srchtypesel .= "\n  </select>\n";
                   8769: 
1.558     albertel 8770:     my ($newuserscript,$new_user_create);
1.994     raeburn  8771:     my $context_dom = $env{'request.role.domain'};
                   8772:     if ($context eq 'requestcrs') {
                   8773:         if ($env{'form.coursedom'} ne '') { 
                   8774:             $context_dom = $env{'form.coursedom'};
                   8775:         }
                   8776:     }
1.556     raeburn  8777:     if ($forcenewuser) {
1.576     raeburn  8778:         if (ref($srch) eq 'HASH') {
1.994     raeburn  8779:             if ($srch->{'srchby'} eq 'uname' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchin'} eq 'dom' && $srch->{'srchdomain'} eq $context_dom) {
1.627     raeburn  8780:                 if ($cancreate) {
                   8781:                     $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>';
                   8782:                 } else {
1.799     bisitz   8783:                     my $helplink = 'javascript:helpMenu('."'display'".')';
1.627     raeburn  8784:                     my %usertypetext = (
                   8785:                         official   => 'institutional',
                   8786:                         unofficial => 'non-institutional',
                   8787:                     );
1.799     bisitz   8788:                     $new_user_create = '<p class="LC_warning">'
                   8789:                                       .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
                   8790:                                       .' '
                   8791:                                       .&mt('Please contact the [_1]helpdesk[_2] for assistance.'
                   8792:                                           ,'<a href="'.$helplink.'">','</a>')
                   8793:                                       .'</p><br />';
1.627     raeburn  8794:                 }
1.576     raeburn  8795:             }
                   8796:         }
                   8797: 
1.556     raeburn  8798:         $newuserscript = <<"ENDSCRIPT";
                   8799: 
1.570     raeburn  8800: function setSearch(createnew,callingForm) {
1.556     raeburn  8801:     if (createnew == 1) {
1.570     raeburn  8802:         for (var i=0; i<callingForm.srchby.length; i++) {
                   8803:             if (callingForm.srchby.options[i].value == 'uname') {
                   8804:                 callingForm.srchby.selectedIndex = i;
1.556     raeburn  8805:             }
                   8806:         }
1.570     raeburn  8807:         for (var i=0; i<callingForm.srchin.length; i++) {
                   8808:             if ( callingForm.srchin.options[i].value == 'dom') {
                   8809: 		callingForm.srchin.selectedIndex = i;
1.556     raeburn  8810:             }
                   8811:         }
1.570     raeburn  8812:         for (var i=0; i<callingForm.srchtype.length; i++) {
                   8813:             if (callingForm.srchtype.options[i].value == 'exact') {
                   8814:                 callingForm.srchtype.selectedIndex = i;
1.556     raeburn  8815:             }
                   8816:         }
1.570     raeburn  8817:         for (var i=0; i<callingForm.srchdomain.length; i++) {
1.994     raeburn  8818:             if (callingForm.srchdomain.options[i].value == '$context_dom') {
1.570     raeburn  8819:                 callingForm.srchdomain.selectedIndex = i;
1.556     raeburn  8820:             }
                   8821:         }
                   8822:     }
                   8823: }
                   8824: ENDSCRIPT
1.558     albertel 8825: 
1.556     raeburn  8826:     }
                   8827: 
1.555     raeburn  8828:     my $output = <<"END_BLOCK";
1.556     raeburn  8829: <script type="text/javascript">
1.824     bisitz   8830: // <![CDATA[
1.570     raeburn  8831: function validateEntry(callingForm) {
1.558     albertel 8832: 
1.556     raeburn  8833:     var checkok = 1;
1.558     albertel 8834:     var srchin;
1.570     raeburn  8835:     for (var i=0; i<callingForm.srchin.length; i++) {
                   8836: 	if ( callingForm.srchin[i].checked ) {
                   8837: 	    srchin = callingForm.srchin[i].value;
1.558     albertel 8838: 	}
                   8839:     }
                   8840: 
1.570     raeburn  8841:     var srchtype = callingForm.srchtype.options[callingForm.srchtype.selectedIndex].value;
                   8842:     var srchby = callingForm.srchby.options[callingForm.srchby.selectedIndex].value;
                   8843:     var srchdomain = callingForm.srchdomain.options[callingForm.srchdomain.selectedIndex].value;
                   8844:     var srchterm =  callingForm.srchterm.value;
                   8845:     var srchin = callingForm.srchin.options[callingForm.srchin.selectedIndex].value;
1.556     raeburn  8846:     var msg = "";
                   8847: 
                   8848:     if (srchterm == "") {
                   8849:         checkok = 0;
1.571     raeburn  8850:         msg += "$lt{'youm'}\\n";
1.556     raeburn  8851:     }
                   8852: 
1.569     raeburn  8853:     if (srchtype== 'begins') {
                   8854:         if (srchterm.length < 2) {
                   8855:             checkok = 0;
1.571     raeburn  8856:             msg += "$lt{'thte'}\\n";
1.569     raeburn  8857:         }
                   8858:     }
                   8859: 
1.556     raeburn  8860:     if (srchtype== 'contains') {
                   8861:         if (srchterm.length < 3) {
                   8862:             checkok = 0;
1.571     raeburn  8863:             msg += "$lt{'thet'}\\n";
1.556     raeburn  8864:         }
                   8865:     }
                   8866:     if (srchin == 'instd') {
                   8867:         if (srchdomain == '') {
                   8868:             checkok = 0;
1.571     raeburn  8869:             msg += "$lt{'yomc'}\\n";
1.556     raeburn  8870:         }
                   8871:     }
                   8872:     if (srchin == 'dom') {
                   8873:         if (srchdomain == '') {
                   8874:             checkok = 0;
1.571     raeburn  8875:             msg += "$lt{'ymcd'}\\n";
1.556     raeburn  8876:         }
                   8877:     }
                   8878:     if (srchby == 'lastfirst') {
                   8879:         if (srchterm.indexOf(",") == -1) {
                   8880:             checkok = 0;
1.571     raeburn  8881:             msg += "$lt{'whus'}\\n";
1.556     raeburn  8882:         }
                   8883:         if (srchterm.indexOf(",") == srchterm.length -1) {
                   8884:             checkok = 0;
1.571     raeburn  8885:             msg += "$lt{'whse'}\\n";
1.556     raeburn  8886:         }
                   8887:     }
                   8888:     if (checkok == 0) {
1.571     raeburn  8889:         alert("$lt{'thfo'}\\n"+msg);
1.556     raeburn  8890:         return;
                   8891:     }
                   8892:     if (checkok == 1) {
1.570     raeburn  8893:         callingForm.submit();
1.556     raeburn  8894:     }
                   8895: }
                   8896: 
                   8897: $newuserscript
                   8898: 
1.824     bisitz   8899: // ]]>
1.556     raeburn  8900: </script>
1.558     albertel 8901: 
                   8902: $new_user_create
                   8903: 
1.555     raeburn  8904: END_BLOCK
1.558     albertel 8905: 
1.876     raeburn  8906:     $output .= &Apache::lonhtmlcommon::start_pick_box().
                   8907:                &Apache::lonhtmlcommon::row_title($lt{'doma'}).
                   8908:                $domform.
                   8909:                &Apache::lonhtmlcommon::row_closure().
                   8910:                &Apache::lonhtmlcommon::row_title($lt{'usr'}).
                   8911:                $srchbysel.
                   8912:                $srchtypesel. 
                   8913:                '<input type="text" size="15" name="srchterm" value="'.$srchterm.'" />'.
                   8914:                $srchinsel.
                   8915:                &Apache::lonhtmlcommon::row_closure(1). 
                   8916:                &Apache::lonhtmlcommon::end_pick_box().
                   8917:                '<br />';
1.555     raeburn  8918:     return $output;
                   8919: }
                   8920: 
1.612     raeburn  8921: sub user_rule_check {
1.615     raeburn  8922:     my ($usershash,$checks,$alerts,$rulematch,$inst_results,$curr_rules,$got_rules) = @_;
1.612     raeburn  8923:     my $response;
                   8924:     if (ref($usershash) eq 'HASH') {
                   8925:         foreach my $user (keys(%{$usershash})) {
                   8926:             my ($uname,$udom) = split(/:/,$user);
                   8927:             next if ($udom eq '' || $uname eq '');
1.615     raeburn  8928:             my ($id,$newuser);
1.612     raeburn  8929:             if (ref($usershash->{$user}) eq 'HASH') {
1.615     raeburn  8930:                 $newuser = $usershash->{$user}->{'newuser'};
1.612     raeburn  8931:                 $id = $usershash->{$user}->{'id'};
                   8932:             }
                   8933:             my $inst_response;
                   8934:             if (ref($checks) eq 'HASH') {
                   8935:                 if (defined($checks->{'username'})) {
1.615     raeburn  8936:                     ($inst_response,%{$inst_results->{$user}}) = 
1.612     raeburn  8937:                         &Apache::lonnet::get_instuser($udom,$uname);
                   8938:                 } elsif (defined($checks->{'id'})) {
1.615     raeburn  8939:                     ($inst_response,%{$inst_results->{$user}}) =
1.612     raeburn  8940:                         &Apache::lonnet::get_instuser($udom,undef,$id);
                   8941:                 }
1.615     raeburn  8942:             } else {
                   8943:                 ($inst_response,%{$inst_results->{$user}}) =
                   8944:                     &Apache::lonnet::get_instuser($udom,$uname);
                   8945:                 return;
1.612     raeburn  8946:             }
1.615     raeburn  8947:             if (!$got_rules->{$udom}) {
1.612     raeburn  8948:                 my %domconfig = &Apache::lonnet::get_dom('configuration',
                   8949:                                                   ['usercreation'],$udom);
                   8950:                 if (ref($domconfig{'usercreation'}) eq 'HASH') {
1.615     raeburn  8951:                     foreach my $item ('username','id') {
1.612     raeburn  8952:                         if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
                   8953:                             $$curr_rules{$udom}{$item} = 
                   8954:                                 $domconfig{'usercreation'}{$item.'_rule'};
1.585     raeburn  8955:                         }
                   8956:                     }
                   8957:                 }
1.615     raeburn  8958:                 $got_rules->{$udom} = 1;  
1.585     raeburn  8959:             }
1.612     raeburn  8960:             foreach my $item (keys(%{$checks})) {
                   8961:                 if (ref($$curr_rules{$udom}) eq 'HASH') {
                   8962:                     if (ref($$curr_rules{$udom}{$item}) eq 'ARRAY') {
                   8963:                         if (@{$$curr_rules{$udom}{$item}} > 0) {
                   8964:                             my %rule_check = &Apache::lonnet::inst_rulecheck($udom,$uname,$id,$item,$$curr_rules{$udom}{$item});
                   8965:                             foreach my $rule (@{$$curr_rules{$udom}{$item}}) {
                   8966:                                 if ($rule_check{$rule}) {
                   8967:                                     $$rulematch{$user}{$item} = $rule;
                   8968:                                     if ($inst_response eq 'ok') {
1.615     raeburn  8969:                                         if (ref($inst_results) eq 'HASH') {
                   8970:                                             if (ref($inst_results->{$user}) eq 'HASH') {
                   8971:                                                 if (keys(%{$inst_results->{$user}}) == 0) {
                   8972:                                                     $$alerts{$item}{$udom}{$uname} = 1;
                   8973:                                                 }
1.612     raeburn  8974:                                             }
                   8975:                                         }
1.615     raeburn  8976:                                     }
                   8977:                                     last;
1.585     raeburn  8978:                                 }
                   8979:                             }
                   8980:                         }
                   8981:                     }
                   8982:                 }
                   8983:             }
                   8984:         }
                   8985:     }
1.612     raeburn  8986:     return;
                   8987: }
                   8988: 
                   8989: sub user_rule_formats {
                   8990:     my ($domain,$domdesc,$curr_rules,$check) = @_;
                   8991:     my %text = ( 
                   8992:                  'username' => 'Usernames',
                   8993:                  'id'       => 'IDs',
                   8994:                );
                   8995:     my $output;
                   8996:     my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($domain,$check);
                   8997:     if ((ref($rules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
                   8998:         if (@{$ruleorder} > 0) {
1.1075.2.20! raeburn  8999:             $output = '<br />'.
        !          9000:                       &mt($text{$check}.' with the following format(s) may [_1]only[_2] be used for verified users at [_3]:',
        !          9001:                           '<span class="LC_cusr_emph">','</span>',$domdesc).
        !          9002:                       ' <ul>';
1.612     raeburn  9003:             foreach my $rule (@{$ruleorder}) {
                   9004:                 if (ref($curr_rules) eq 'ARRAY') {
                   9005:                     if (grep(/^\Q$rule\E$/,@{$curr_rules})) {
                   9006:                         if (ref($rules->{$rule}) eq 'HASH') {
                   9007:                             $output .= '<li>'.$rules->{$rule}{'name'}.': '.
                   9008:                                         $rules->{$rule}{'desc'}.'</li>';
                   9009:                         }
                   9010:                     }
                   9011:                 }
                   9012:             }
                   9013:             $output .= '</ul>';
                   9014:         }
                   9015:     }
                   9016:     return $output;
                   9017: }
                   9018: 
                   9019: sub instrule_disallow_msg {
1.615     raeburn  9020:     my ($checkitem,$domdesc,$count,$mode) = @_;
1.612     raeburn  9021:     my $response;
                   9022:     my %text = (
                   9023:                   item   => 'username',
                   9024:                   items  => 'usernames',
                   9025:                   match  => 'matches',
                   9026:                   do     => 'does',
                   9027:                   action => 'a username',
                   9028:                   one    => 'one',
                   9029:                );
                   9030:     if ($count > 1) {
                   9031:         $text{'item'} = 'usernames';
                   9032:         $text{'match'} ='match';
                   9033:         $text{'do'} = 'do';
                   9034:         $text{'action'} = 'usernames',
                   9035:         $text{'one'} = 'ones';
                   9036:     }
                   9037:     if ($checkitem eq 'id') {
                   9038:         $text{'items'} = 'IDs';
                   9039:         $text{'item'} = 'ID';
                   9040:         $text{'action'} = 'an ID';
1.615     raeburn  9041:         if ($count > 1) {
                   9042:             $text{'item'} = 'IDs';
                   9043:             $text{'action'} = 'IDs';
                   9044:         }
1.612     raeburn  9045:     }
1.674     bisitz   9046:     $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  9047:     if ($mode eq 'upload') {
                   9048:         if ($checkitem eq 'username') {
                   9049:             $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'}.");
                   9050:         } elsif ($checkitem eq 'id') {
1.674     bisitz   9051:             $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  9052:         }
1.669     raeburn  9053:     } elsif ($mode eq 'selfcreate') {
                   9054:         if ($checkitem eq 'id') {
                   9055:             $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.");
                   9056:         }
1.615     raeburn  9057:     } else {
                   9058:         if ($checkitem eq 'username') {
                   9059:             $response .= &mt("You must choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}.");
                   9060:         } elsif ($checkitem eq 'id') {
                   9061:             $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.");
                   9062:         }
1.612     raeburn  9063:     }
                   9064:     return $response;
1.585     raeburn  9065: }
                   9066: 
1.624     raeburn  9067: sub personal_data_fieldtitles {
                   9068:     my %fieldtitles = &Apache::lonlocal::texthash (
                   9069:                         id => 'Student/Employee ID',
                   9070:                         permanentemail => 'E-mail address',
                   9071:                         lastname => 'Last Name',
                   9072:                         firstname => 'First Name',
                   9073:                         middlename => 'Middle Name',
                   9074:                         generation => 'Generation',
                   9075:                         gen => 'Generation',
1.765     raeburn  9076:                         inststatus => 'Affiliation',
1.624     raeburn  9077:                    );
                   9078:     return %fieldtitles;
                   9079: }
                   9080: 
1.642     raeburn  9081: sub sorted_inst_types {
                   9082:     my ($dom) = @_;
                   9083:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($dom);
                   9084:     my $othertitle = &mt('All users');
                   9085:     if ($env{'request.course.id'}) {
1.668     raeburn  9086:         $othertitle  = &mt('Any users');
1.642     raeburn  9087:     }
                   9088:     my @types;
                   9089:     if (ref($order) eq 'ARRAY') {
                   9090:         @types = @{$order};
                   9091:     }
                   9092:     if (@types == 0) {
                   9093:         if (ref($usertypes) eq 'HASH') {
                   9094:             @types = sort(keys(%{$usertypes}));
                   9095:         }
                   9096:     }
                   9097:     if (keys(%{$usertypes}) > 0) {
                   9098:         $othertitle = &mt('Other users');
                   9099:     }
                   9100:     return ($othertitle,$usertypes,\@types);
                   9101: }
                   9102: 
1.645     raeburn  9103: sub get_institutional_codes {
                   9104:     my ($settings,$allcourses,$LC_code) = @_;
                   9105: # Get complete list of course sections to update
                   9106:     my @currsections = ();
                   9107:     my @currxlists = ();
                   9108:     my $coursecode = $$settings{'internal.coursecode'};
                   9109: 
                   9110:     if ($$settings{'internal.sectionnums'} ne '') {
                   9111:         @currsections = split(/,/,$$settings{'internal.sectionnums'});
                   9112:     }
                   9113: 
                   9114:     if ($$settings{'internal.crosslistings'} ne '') {
                   9115:         @currxlists = split(/,/,$$settings{'internal.crosslistings'});
                   9116:     }
                   9117: 
                   9118:     if (@currxlists > 0) {
                   9119:         foreach (@currxlists) {
                   9120:             if (m/^([^:]+):(\w*)$/) {
                   9121:                 unless (grep/^$1$/,@{$allcourses}) {
                   9122:                     push @{$allcourses},$1;
                   9123:                     $$LC_code{$1} = $2;
                   9124:                 }
                   9125:             }
                   9126:         }
                   9127:     }
                   9128:  
                   9129:     if (@currsections > 0) {
                   9130:         foreach (@currsections) {
                   9131:             if (m/^(\w+):(\w*)$/) {
                   9132:                 my $sec = $coursecode.$1;
                   9133:                 my $lc_sec = $2;
                   9134:                 unless (grep/^$sec$/,@{$allcourses}) {
                   9135:                     push @{$allcourses},$sec;
                   9136:                     $$LC_code{$sec} = $lc_sec;
                   9137:                 }
                   9138:             }
                   9139:         }
                   9140:     }
                   9141:     return;
                   9142: }
                   9143: 
1.971     raeburn  9144: sub get_standard_codeitems {
                   9145:     return ('Year','Semester','Department','Number','Section');
                   9146: }
                   9147: 
1.112     bowersj2 9148: =pod
                   9149: 
1.780     raeburn  9150: =head1 Slot Helpers
                   9151: 
                   9152: =over 4
                   9153: 
                   9154: =item * sorted_slots()
                   9155: 
1.1040    raeburn  9156: Sorts an array of slot names in order of an optional sort key,
                   9157: default sort is by slot start time (earliest first). 
1.780     raeburn  9158: 
                   9159: Inputs:
                   9160: 
                   9161: =over 4
                   9162: 
                   9163: slotsarr  - Reference to array of unsorted slot names.
                   9164: 
                   9165: slots     - Reference to hash of hash, where outer hash keys are slot names.
                   9166: 
1.1040    raeburn  9167: sortkey   - Name of key in inner hash to be sorted on (e.g., starttime).
                   9168: 
1.549     albertel 9169: =back
                   9170: 
1.780     raeburn  9171: Returns:
                   9172: 
                   9173: =over 4
                   9174: 
1.1040    raeburn  9175: sorted   - An array of slot names sorted by a specified sort key 
                   9176:            (default sort key is start time of the slot).
1.780     raeburn  9177: 
                   9178: =back
                   9179: 
                   9180: =cut
                   9181: 
                   9182: 
                   9183: sub sorted_slots {
1.1040    raeburn  9184:     my ($slotsarr,$slots,$sortkey) = @_;
                   9185:     if ($sortkey eq '') {
                   9186:         $sortkey = 'starttime';
                   9187:     }
1.780     raeburn  9188:     my @sorted;
                   9189:     if ((ref($slotsarr) eq 'ARRAY') && (ref($slots) eq 'HASH')) {
                   9190:         @sorted =
                   9191:             sort {
                   9192:                      if (ref($slots->{$a}) && ref($slots->{$b})) {
1.1040    raeburn  9193:                          return $slots->{$a}{$sortkey} <=> $slots->{$b}{$sortkey}
1.780     raeburn  9194:                      }
                   9195:                      if (ref($slots->{$a})) { return -1;}
                   9196:                      if (ref($slots->{$b})) { return 1;}
                   9197:                      return 0;
                   9198:                  } @{$slotsarr};
                   9199:     }
                   9200:     return @sorted;
                   9201: }
                   9202: 
1.1040    raeburn  9203: =pod
                   9204: 
                   9205: =item * get_future_slots()
                   9206: 
                   9207: Inputs:
                   9208: 
                   9209: =over 4
                   9210: 
                   9211: cnum - course number
                   9212: 
                   9213: cdom - course domain
                   9214: 
                   9215: now - current UNIX time
                   9216: 
                   9217: symb - optional symb
                   9218: 
                   9219: =back
                   9220: 
                   9221: Returns:
                   9222: 
                   9223: =over 4
                   9224: 
                   9225: sorted_reservable - ref to array of student_schedulable slots currently 
                   9226:                     reservable, ordered by end date of reservation period.
                   9227: 
                   9228: reservable_now - ref to hash of student_schedulable slots currently
                   9229:                  reservable.
                   9230: 
                   9231:     Keys in inner hash are:
                   9232:     (a) symb: either blank or symb to which slot use is restricted.
                   9233:     (b) endreserve: end date of reservation period. 
                   9234: 
                   9235: sorted_future - ref to array of student_schedulable slots reservable in
                   9236:                 the future, ordered by start date of reservation period.
                   9237: 
                   9238: future_reservable - ref to hash of student_schedulable slots reservable
                   9239:                     in the future.
                   9240: 
                   9241:     Keys in inner hash are:
                   9242:     (a) symb: either blank or symb to which slot use is restricted.
                   9243:     (b) startreserve:  start date of reservation period.
                   9244: 
                   9245: =back
                   9246: 
                   9247: =cut
                   9248: 
                   9249: sub get_future_slots {
                   9250:     my ($cnum,$cdom,$now,$symb) = @_;
                   9251:     my (%reservable_now,%future_reservable,@sorted_reservable,@sorted_future);
                   9252:     my %slots = &Apache::lonnet::get_course_slots($cnum,$cdom);
                   9253:     foreach my $slot (keys(%slots)) {
                   9254:         next unless($slots{$slot}->{'type'} eq 'schedulable_student');
                   9255:         if ($symb) {
                   9256:             next if (($slots{$slot}->{'symb'} ne '') && 
                   9257:                      ($slots{$slot}->{'symb'} ne $symb));
                   9258:         }
                   9259:         if (($slots{$slot}->{'starttime'} > $now) &&
                   9260:             ($slots{$slot}->{'endtime'} > $now)) {
                   9261:             if (($slots{$slot}->{'allowedsections'}) || ($slots{$slot}->{'allowedusers'})) {
                   9262:                 my $userallowed = 0;
                   9263:                 if ($slots{$slot}->{'allowedsections'}) {
                   9264:                     my @allowed_sec = split(',',$slots{$slot}->{'allowedsections'});
                   9265:                     if (!defined($env{'request.role.sec'})
                   9266:                         && grep(/^No section assigned$/,@allowed_sec)) {
                   9267:                         $userallowed=1;
                   9268:                     } else {
                   9269:                         if (grep(/^\Q$env{'request.role.sec'}\E$/,@allowed_sec)) {
                   9270:                             $userallowed=1;
                   9271:                         }
                   9272:                     }
                   9273:                     unless ($userallowed) {
                   9274:                         if (defined($env{'request.course.groups'})) {
                   9275:                             my @groups = split(/:/,$env{'request.course.groups'});
                   9276:                             foreach my $group (@groups) {
                   9277:                                 if (grep(/^\Q$group\E$/,@allowed_sec)) {
                   9278:                                     $userallowed=1;
                   9279:                                     last;
                   9280:                                 }
                   9281:                             }
                   9282:                         }
                   9283:                     }
                   9284:                 }
                   9285:                 if ($slots{$slot}->{'allowedusers'}) {
                   9286:                     my @allowed_users = split(',',$slots{$slot}->{'allowedusers'});
                   9287:                     my $user = $env{'user.name'}.':'.$env{'user.domain'};
                   9288:                     if (grep(/^\Q$user\E$/,@allowed_users)) {
                   9289:                         $userallowed = 1;
                   9290:                     }
                   9291:                 }
                   9292:                 next unless($userallowed);
                   9293:             }
                   9294:             my $startreserve = $slots{$slot}->{'startreserve'};
                   9295:             my $endreserve = $slots{$slot}->{'endreserve'};
                   9296:             my $symb = $slots{$slot}->{'symb'};
                   9297:             if (($startreserve < $now) &&
                   9298:                 (!$endreserve || $endreserve > $now)) {
                   9299:                 my $lastres = $endreserve;
                   9300:                 if (!$lastres) {
                   9301:                     $lastres = $slots{$slot}->{'starttime'};
                   9302:                 }
                   9303:                 $reservable_now{$slot} = {
                   9304:                                            symb       => $symb,
                   9305:                                            endreserve => $lastres
                   9306:                                          };
                   9307:             } elsif (($startreserve > $now) &&
                   9308:                      (!$endreserve || $endreserve > $startreserve)) {
                   9309:                 $future_reservable{$slot} = {
                   9310:                                               symb         => $symb,
                   9311:                                               startreserve => $startreserve
                   9312:                                             };
                   9313:             }
                   9314:         }
                   9315:     }
                   9316:     my @unsorted_reservable = keys(%reservable_now);
                   9317:     if (@unsorted_reservable > 0) {
                   9318:         @sorted_reservable = 
                   9319:             &sorted_slots(\@unsorted_reservable,\%reservable_now,'endreserve');
                   9320:     }
                   9321:     my @unsorted_future = keys(%future_reservable);
                   9322:     if (@unsorted_future > 0) {
                   9323:         @sorted_future =
                   9324:             &sorted_slots(\@unsorted_future,\%future_reservable,'startreserve');
                   9325:     }
                   9326:     return (\@sorted_reservable,\%reservable_now,\@sorted_future,\%future_reservable);
                   9327: }
1.780     raeburn  9328: 
                   9329: =pod
                   9330: 
1.1057    foxr     9331: =back
                   9332: 
1.549     albertel 9333: =head1 HTTP Helpers
                   9334: 
                   9335: =over 4
                   9336: 
1.648     raeburn  9337: =item * &get_unprocessed_cgi($query,$possible_names)
1.112     bowersj2 9338: 
1.258     albertel 9339: Modify the %env hash to contain unprocessed CGI form parameters held in
1.112     bowersj2 9340: $query.  The parameters listed in $possible_names (an array reference),
1.258     albertel 9341: will be set in $env{'form.name'} if they do not already exist.
1.112     bowersj2 9342: 
                   9343: Typically called with $ENV{'QUERY_STRING'} as the first parameter.  
                   9344: $possible_names is an ref to an array of form element names.  As an example:
                   9345: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
1.258     albertel 9346: will result in $env{'form.uname'} and $env{'form.udom'} being set.
1.112     bowersj2 9347: 
                   9348: =cut
1.1       albertel 9349: 
1.6       albertel 9350: sub get_unprocessed_cgi {
1.25      albertel 9351:   my ($query,$possible_names)= @_;
1.26      matthew  9352:   # $Apache::lonxml::debug=1;
1.356     albertel 9353:   foreach my $pair (split(/&/,$query)) {
                   9354:     my ($name, $value) = split(/=/,$pair);
1.369     www      9355:     $name = &unescape($name);
1.25      albertel 9356:     if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
                   9357:       $value =~ tr/+/ /;
                   9358:       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.258     albertel 9359:       unless (defined($env{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
1.25      albertel 9360:     }
1.16      harris41 9361:   }
1.6       albertel 9362: }
                   9363: 
1.112     bowersj2 9364: =pod
                   9365: 
1.648     raeburn  9366: =item * &cacheheader() 
1.112     bowersj2 9367: 
                   9368: returns cache-controlling header code
                   9369: 
                   9370: =cut
                   9371: 
1.7       albertel 9372: sub cacheheader {
1.258     albertel 9373:     unless ($env{'request.method'} eq 'GET') { return ''; }
1.216     albertel 9374:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
                   9375:     my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
1.7       albertel 9376:                 <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
                   9377:                 <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
1.216     albertel 9378:     return $output;
1.7       albertel 9379: }
                   9380: 
1.112     bowersj2 9381: =pod
                   9382: 
1.648     raeburn  9383: =item * &no_cache($r) 
1.112     bowersj2 9384: 
                   9385: specifies header code to not have cache
                   9386: 
                   9387: =cut
                   9388: 
1.9       albertel 9389: sub no_cache {
1.216     albertel 9390:     my ($r) = @_;
                   9391:     if ($ENV{'REQUEST_METHOD'} ne 'GET' &&
1.258     albertel 9392: 	$env{'request.method'} ne 'GET') { return ''; }
1.216     albertel 9393:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time));
                   9394:     $r->no_cache(1);
                   9395:     $r->header_out("Expires" => $date);
                   9396:     $r->header_out("Pragma" => "no-cache");
1.123     www      9397: }
                   9398: 
                   9399: sub content_type {
1.181     albertel 9400:     my ($r,$type,$charset) = @_;
1.299     foxr     9401:     if ($r) {
                   9402: 	#  Note that printout.pl calls this with undef for $r.
                   9403: 	&no_cache($r);
                   9404:     }
1.258     albertel 9405:     if ($env{'browser.mathml'} && $type eq 'text/html') { $type='text/xml'; }
1.181     albertel 9406:     unless ($charset) {
                   9407: 	$charset=&Apache::lonlocal::current_encoding;
                   9408:     }
                   9409:     if ($charset) { $type.='; charset='.$charset; }
                   9410:     if ($r) {
                   9411: 	$r->content_type($type);
                   9412:     } else {
                   9413: 	print("Content-type: $type\n\n");
                   9414:     }
1.9       albertel 9415: }
1.25      albertel 9416: 
1.112     bowersj2 9417: =pod
                   9418: 
1.648     raeburn  9419: =item * &add_to_env($name,$value) 
1.112     bowersj2 9420: 
1.258     albertel 9421: adds $name to the %env hash with value
1.112     bowersj2 9422: $value, if $name already exists, the entry is converted to an array
                   9423: reference and $value is added to the array.
                   9424: 
                   9425: =cut
                   9426: 
1.25      albertel 9427: sub add_to_env {
                   9428:   my ($name,$value)=@_;
1.258     albertel 9429:   if (defined($env{$name})) {
                   9430:     if (ref($env{$name})) {
1.25      albertel 9431:       #already have multiple values
1.258     albertel 9432:       push(@{ $env{$name} },$value);
1.25      albertel 9433:     } else {
                   9434:       #first time seeing multiple values, convert hash entry to an arrayref
1.258     albertel 9435:       my $first=$env{$name};
                   9436:       undef($env{$name});
                   9437:       push(@{ $env{$name} },$first,$value);
1.25      albertel 9438:     }
                   9439:   } else {
1.258     albertel 9440:     $env{$name}=$value;
1.25      albertel 9441:   }
1.31      albertel 9442: }
1.149     albertel 9443: 
                   9444: =pod
                   9445: 
1.648     raeburn  9446: =item * &get_env_multiple($name) 
1.149     albertel 9447: 
1.258     albertel 9448: gets $name from the %env hash, it seemlessly handles the cases where multiple
1.149     albertel 9449: values may be defined and end up as an array ref.
                   9450: 
                   9451: returns an array of values
                   9452: 
                   9453: =cut
                   9454: 
                   9455: sub get_env_multiple {
                   9456:     my ($name) = @_;
                   9457:     my @values;
1.258     albertel 9458:     if (defined($env{$name})) {
1.149     albertel 9459:         # exists is it an array
1.258     albertel 9460:         if (ref($env{$name})) {
                   9461:             @values=@{ $env{$name} };
1.149     albertel 9462:         } else {
1.258     albertel 9463:             $values[0]=$env{$name};
1.149     albertel 9464:         }
                   9465:     }
                   9466:     return(@values);
                   9467: }
                   9468: 
1.660     raeburn  9469: sub ask_for_embedded_content {
                   9470:     my ($actionurl,$state,$allfiles,$codebase,$args)=@_;
1.1071    raeburn  9471:     my (%subdependencies,%dependencies,%mapping,%existing,%newfiles,%pathchanges,
1.1075.2.11  raeburn  9472:         %currsubfile,%unused,$rem);
1.1071    raeburn  9473:     my $counter = 0;
                   9474:     my $numnew = 0;
1.987     raeburn  9475:     my $numremref = 0;
                   9476:     my $numinvalid = 0;
                   9477:     my $numpathchg = 0;
                   9478:     my $numexisting = 0;
1.1071    raeburn  9479:     my $numunused = 0;
                   9480:     my ($output,$upload_output,$toplevel,$url,$udom,$uname,$getpropath,$cdom,$cnum,
                   9481:         $fileloc,$filename,$delete_output,$modify_output,$title,$symb,$path);
                   9482:     my $heading = &mt('Upload embedded files');
                   9483:     my $buttontext = &mt('Upload');
                   9484: 
1.1075.2.11  raeburn  9485:     my $navmap;
                   9486:     if ($env{'request.course.id'}) {
                   9487:         $navmap = Apache::lonnavmaps::navmap->new();
                   9488:     }
1.984     raeburn  9489:     if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
                   9490:         my $current_path='/';
                   9491:         if ($env{'form.currentpath'}) {
                   9492:             $current_path = $env{'form.currentpath'};
                   9493:         }
                   9494:         if ($actionurl eq '/adm/coursegrp_portfolio') {
                   9495:             $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   9496:             $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
                   9497:             $url = '/userfiles/groups/'.$env{'form.group'}.'/portfolio';
                   9498:         } else {
                   9499:             $udom = $env{'user.domain'};
                   9500:             $uname = $env{'user.name'};
                   9501:             $url = '/userfiles/portfolio';
                   9502:         }
1.987     raeburn  9503:         $toplevel = $url.'/';
1.984     raeburn  9504:         $url .= $current_path;
                   9505:         $getpropath = 1;
1.987     raeburn  9506:     } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank') ||
                   9507:              ($actionurl eq '/adm/imsimport')) { 
1.1022    www      9508:         my ($udom,$uname,$rest) = ($args->{'current_path'} =~ m{/priv/($match_domain)/($match_username)/?(.*)$});
1.1026    raeburn  9509:         $url = $Apache::lonnet::perlvar{'lonDocRoot'}."/priv/$udom/$uname/";
1.987     raeburn  9510:         $toplevel = $url;
1.984     raeburn  9511:         if ($rest ne '') {
1.987     raeburn  9512:             $url .= $rest;
                   9513:         }
                   9514:     } elsif ($actionurl eq '/adm/coursedocs') {
                   9515:         if (ref($args) eq 'HASH') {
1.1071    raeburn  9516:             $url = $args->{'docs_url'};
                   9517:             $toplevel = $url;
1.1075.2.11  raeburn  9518:             if ($args->{'context'} eq 'paste') {
                   9519:                 ($cdom,$cnum) = ($url =~ m{^\Q/uploaded/\E($match_domain)/($match_courseid)/});
                   9520:                 ($path) =
                   9521:                     ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/\E(?:docs|supplemental)/(?:default|\d+)/\d+)/});
                   9522:                 $fileloc = &Apache::lonnet::filelocation('',$toplevel);
                   9523:                 $fileloc =~ s{^/}{};
                   9524:             }
1.1071    raeburn  9525:         }
                   9526:     } elsif ($actionurl eq '/adm/dependencies') {
                   9527:         if ($env{'request.course.id'} ne '') {
                   9528:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   9529:             $cnum =  $env{'course.'.$env{'request.course.id'}.'.num'};
                   9530:             if (ref($args) eq 'HASH') {
                   9531:                 $url = $args->{'docs_url'};
                   9532:                 $title = $args->{'docs_title'};
                   9533:                 $toplevel = "/$url";
1.1075.2.11  raeburn  9534:                 ($rem) = ($toplevel =~ m{^(.+/)[^/]+$});
1.1071    raeburn  9535:                 ($path) =  
                   9536:                     ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/\E(?:docs|supplemental)/(?:default|\d+)/\d+)/});
                   9537:                 $fileloc = &Apache::lonnet::filelocation('',$toplevel);
                   9538:                 $fileloc =~ s{^/}{};
                   9539:                 ($filename) = ($fileloc =~ m{.+/([^/]+)$});
                   9540:                 $heading = &mt('Status of dependencies in [_1]',"$title ($filename)");
                   9541:             }
1.987     raeburn  9542:         }
                   9543:     }
                   9544:     my $now = time();
                   9545:     foreach my $embed_file (keys(%{$allfiles})) {
                   9546:         my $absolutepath;
                   9547:         if ($embed_file =~ m{^\w+://}) {
                   9548:             $newfiles{$embed_file} = 1;
                   9549:             $mapping{$embed_file} = $embed_file;
                   9550:         } else {
                   9551:             if ($embed_file =~ m{^/}) {
                   9552:                 $absolutepath = $embed_file;
                   9553:                 $embed_file =~ s{^(/+)}{};
                   9554:             }
                   9555:             if ($embed_file =~ m{/}) {
                   9556:                 my ($path,$fname) = ($embed_file =~ m{^(.+)/([^/]*)$});
                   9557:                 $path = &check_for_traversal($path,$url,$toplevel);
                   9558:                 my $item = $fname;
                   9559:                 if ($path ne '') {
                   9560:                     $item = $path.'/'.$fname;
                   9561:                     $subdependencies{$path}{$fname} = 1;
                   9562:                 } else {
                   9563:                     $dependencies{$item} = 1;
                   9564:                 }
                   9565:                 if ($absolutepath) {
                   9566:                     $mapping{$item} = $absolutepath;
                   9567:                 } else {
                   9568:                     $mapping{$item} = $embed_file;
                   9569:                 }
                   9570:             } else {
                   9571:                 $dependencies{$embed_file} = 1;
                   9572:                 if ($absolutepath) {
                   9573:                     $mapping{$embed_file} = $absolutepath;
                   9574:                 } else {
                   9575:                     $mapping{$embed_file} = $embed_file;
                   9576:                 }
                   9577:             }
1.984     raeburn  9578:         }
                   9579:     }
1.1071    raeburn  9580:     my $dirptr = 16384;
1.984     raeburn  9581:     foreach my $path (keys(%subdependencies)) {
1.1071    raeburn  9582:         $currsubfile{$path} = {};
1.984     raeburn  9583:         if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) { 
1.1021    raeburn  9584:             my ($sublistref,$listerror) =
                   9585:                 &Apache::lonnet::dirlist($url.$path,$udom,$uname,$getpropath);
                   9586:             if (ref($sublistref) eq 'ARRAY') {
                   9587:                 foreach my $line (@{$sublistref}) {
                   9588:                     my ($file_name,$rest) = split(/\&/,$line,2);
1.1071    raeburn  9589:                     $currsubfile{$path}{$file_name} = 1;
1.1021    raeburn  9590:                 }
1.984     raeburn  9591:             }
1.987     raeburn  9592:         } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
1.984     raeburn  9593:             if (opendir(my $dir,$url.'/'.$path)) {
                   9594:                 my @subdir_list = grep(!/^\./,readdir($dir));
1.1071    raeburn  9595:                 map {$currsubfile{$path}{$_} = 1;} @subdir_list;
                   9596:             }
1.1075.2.11  raeburn  9597:         } elsif (($actionurl eq '/adm/dependencies') ||
                   9598:                  (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
                   9599:                   ($args->{'context'} eq 'paste'))) {
1.1071    raeburn  9600:             if ($env{'request.course.id'} ne '') {
                   9601:                 my ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
                   9602:                 if ($dir ne '') {
                   9603:                     my ($sublistref,$listerror) =
                   9604:                         &Apache::lonnet::dirlist($dir.$path,$cdom,$cnum,$getpropath,undef,'/');
                   9605:                     if (ref($sublistref) eq 'ARRAY') {
                   9606:                         foreach my $line (@{$sublistref}) {
                   9607:                             my ($file_name,$dom,undef,$testdir,undef,undef,undef,undef,$size,
                   9608:                                 undef,$mtime)=split(/\&/,$line,12);
                   9609:                             unless (($testdir&$dirptr) ||
                   9610:                                     ($file_name =~ /^\.\.?$/)) {
                   9611:                                 $currsubfile{$path}{$file_name} = [$size,$mtime];
                   9612:                             }
                   9613:                         }
                   9614:                     }
                   9615:                 }
1.984     raeburn  9616:             }
                   9617:         }
                   9618:         foreach my $file (keys(%{$subdependencies{$path}})) {
1.1071    raeburn  9619:             if (exists($currsubfile{$path}{$file})) {
1.987     raeburn  9620:                 my $item = $path.'/'.$file;
                   9621:                 unless ($mapping{$item} eq $item) {
                   9622:                     $pathchanges{$item} = 1;
                   9623:                 }
                   9624:                 $existing{$item} = 1;
                   9625:                 $numexisting ++;
                   9626:             } else {
                   9627:                 $newfiles{$path.'/'.$file} = 1;
1.984     raeburn  9628:             }
                   9629:         }
1.1071    raeburn  9630:         if ($actionurl eq '/adm/dependencies') {
                   9631:             foreach my $path (keys(%currsubfile)) {
                   9632:                 if (ref($currsubfile{$path}) eq 'HASH') {
                   9633:                     foreach my $file (keys(%{$currsubfile{$path}})) {
                   9634:                          unless ($subdependencies{$path}{$file}) {
1.1075.2.11  raeburn  9635:                              next if (($rem ne '') &&
                   9636:                                       (($env{"httpref.$rem"."$path/$file"} ne '') ||
                   9637:                                        (ref($navmap) &&
                   9638:                                        (($navmap->getResourceByUrl($rem."$path/$file") ne '') ||
                   9639:                                         (($file =~ /^(.*\.s?html?)\.bak$/i) &&
                   9640:                                          ($navmap->getResourceByUrl($rem."$path/$1")))))));
1.1071    raeburn  9641:                              $unused{$path.'/'.$file} = 1; 
                   9642:                          }
                   9643:                     }
                   9644:                 }
                   9645:             }
                   9646:         }
1.984     raeburn  9647:     }
1.987     raeburn  9648:     my %currfile;
1.984     raeburn  9649:     if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
1.1021    raeburn  9650:         my ($dirlistref,$listerror) =
                   9651:             &Apache::lonnet::dirlist($url,$udom,$uname,$getpropath);
                   9652:         if (ref($dirlistref) eq 'ARRAY') {
                   9653:             foreach my $line (@{$dirlistref}) {
                   9654:                 my ($file_name,$rest) = split(/\&/,$line,2);
                   9655:                 $currfile{$file_name} = 1;
                   9656:             }
1.984     raeburn  9657:         }
1.987     raeburn  9658:     } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
1.984     raeburn  9659:         if (opendir(my $dir,$url)) {
1.987     raeburn  9660:             my @dir_list = grep(!/^\./,readdir($dir));
1.984     raeburn  9661:             map {$currfile{$_} = 1;} @dir_list;
                   9662:         }
1.1075.2.11  raeburn  9663:     } elsif (($actionurl eq '/adm/dependencies') ||
                   9664:              (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
                   9665:               ($args->{'context'} eq 'paste'))) {
1.1071    raeburn  9666:         if ($env{'request.course.id'} ne '') {
                   9667:             my ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
                   9668:             if ($dir ne '') {
                   9669:                 my ($dirlistref,$listerror) =
                   9670:                     &Apache::lonnet::dirlist($dir,$cdom,$cnum,$getpropath,undef,'/');
                   9671:                 if (ref($dirlistref) eq 'ARRAY') {
                   9672:                     foreach my $line (@{$dirlistref}) {
                   9673:                         my ($file_name,$dom,undef,$testdir,undef,undef,undef,undef,
                   9674:                             $size,undef,$mtime)=split(/\&/,$line,12);
                   9675:                         unless (($testdir&$dirptr) ||
                   9676:                                 ($file_name =~ /^\.\.?$/)) {
                   9677:                             $currfile{$file_name} = [$size,$mtime];
                   9678:                         }
                   9679:                     }
                   9680:                 }
                   9681:             }
                   9682:         }
1.984     raeburn  9683:     }
                   9684:     foreach my $file (keys(%dependencies)) {
1.1071    raeburn  9685:         if (exists($currfile{$file})) {
1.987     raeburn  9686:             unless ($mapping{$file} eq $file) {
                   9687:                 $pathchanges{$file} = 1;
                   9688:             }
                   9689:             $existing{$file} = 1;
                   9690:             $numexisting ++;
                   9691:         } else {
1.984     raeburn  9692:             $newfiles{$file} = 1;
                   9693:         }
                   9694:     }
1.1071    raeburn  9695:     foreach my $file (keys(%currfile)) {
                   9696:         unless (($file eq $filename) ||
                   9697:                 ($file eq $filename.'.bak') ||
                   9698:                 ($dependencies{$file})) {
1.1075.2.11  raeburn  9699:             if ($actionurl eq '/adm/dependencies') {
                   9700:                 next if (($rem ne '') &&
                   9701:                          (($env{"httpref.$rem".$file} ne '') ||
                   9702:                           (ref($navmap) &&
                   9703:                           (($navmap->getResourceByUrl($rem.$file) ne '') ||
                   9704:                            (($file =~ /^(.*\.s?html?)\.bak$/i) &&
                   9705:                             ($navmap->getResourceByUrl($rem.$1)))))));
                   9706:             }
1.1071    raeburn  9707:             $unused{$file} = 1;
                   9708:         }
                   9709:     }
1.1075.2.11  raeburn  9710:     if (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
                   9711:         ($args->{'context'} eq 'paste')) {
                   9712:         $counter = scalar(keys(%existing));
                   9713:         $numpathchg = scalar(keys(%pathchanges));
                   9714:         return ($output,$counter,$numpathchg,\%existing);
                   9715:     }
1.984     raeburn  9716:     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%newfiles)) {
1.1071    raeburn  9717:         if ($actionurl eq '/adm/dependencies') {
                   9718:             next if ($embed_file =~ m{^\w+://});
                   9719:         }
1.660     raeburn  9720:         $upload_output .= &start_data_table_row().
1.1071    raeburn  9721:                           '<td><img src="'.&icon($embed_file).'" />&nbsp;'.
                   9722:                           '<span class="LC_filename">'.$embed_file.'</span>';
1.987     raeburn  9723:         unless ($mapping{$embed_file} eq $embed_file) {
                   9724:             $upload_output .= '<br /><span class="LC_info" style="font-size:smaller;">'.&mt('changed from: [_1]',$mapping{$embed_file}).'</span>';
                   9725:         }
                   9726:         $upload_output .= '</td><td>';
1.1071    raeburn  9727:         if ($args->{'ignore_remote_references'} && $embed_file =~ m{^\w+://}) { 
1.660     raeburn  9728:             $upload_output.='<span class="LC_warning">'.&mt("URL points to other server.").'</span>';
1.987     raeburn  9729:             $numremref++;
1.660     raeburn  9730:         } elsif ($args->{'error_on_invalid_names'}
                   9731:             && $embed_file ne &Apache::lonnet::clean_filename($embed_file,{'keep_path' => 1,})) {
1.987     raeburn  9732:             $upload_output.='<span class="LC_warning">'.&mt('Invalid characters').'</span>';
                   9733:             $numinvalid++;
1.660     raeburn  9734:         } else {
1.1071    raeburn  9735:             $upload_output .= &embedded_file_element('upload_embedded',$counter,
1.987     raeburn  9736:                                                      $embed_file,\%mapping,
1.1071    raeburn  9737:                                                      $allfiles,$codebase,'upload');
                   9738:             $counter ++;
                   9739:             $numnew ++;
1.987     raeburn  9740:         }
                   9741:         $upload_output .= '</td>'.&Apache::loncommon::end_data_table_row()."\n";
                   9742:     }
                   9743:     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%existing)) {
1.1071    raeburn  9744:         if ($actionurl eq '/adm/dependencies') {
                   9745:             my ($size,$mtime) = &get_dependency_details(\%currfile,\%currsubfile,$embed_file);
                   9746:             $modify_output .= &start_data_table_row().
                   9747:                               '<td><a href="'.$path.'/'.$embed_file.'" style="text-decoration:none;">'.
                   9748:                               '<img src="'.&icon($embed_file).'" border="0" />'.
                   9749:                               '&nbsp;<span class="LC_filename">'.$embed_file.'</span></a></td>'.
                   9750:                               '<td>'.$size.'</td>'.
                   9751:                               '<td>'.$mtime.'</td>'.
                   9752:                               '<td><label><input type="checkbox" name="mod_upload_dep" '.
                   9753:                               'onclick="toggleBrowse('."'$counter'".')" id="mod_upload_dep_'.
                   9754:                               $counter.'" value="'.$counter.'" />'.&mt('Yes').'</label>'.
                   9755:                               '<div id="moduploaddep_'.$counter.'" style="display:none;">'.
                   9756:                               &embedded_file_element('upload_embedded',$counter,
                   9757:                                                      $embed_file,\%mapping,
                   9758:                                                      $allfiles,$codebase,'modify').
                   9759:                               '</div></td>'.
                   9760:                               &end_data_table_row()."\n";
                   9761:             $counter ++;
                   9762:         } else {
                   9763:             $upload_output .= &start_data_table_row().
                   9764:                               '<td><span class="LC_filename">'.$embed_file.'</span></td>';
                   9765:                               '<td><span class="LC_warning">'.&mt('Already exists').'</span></td>'.
                   9766:                               &Apache::loncommon::end_data_table_row()."\n";
                   9767:         }
                   9768:     }
                   9769:     my $delidx = $counter;
                   9770:     foreach my $oldfile (sort {lc($a) cmp lc($b)} keys(%unused)) {
                   9771:         my ($size,$mtime) = &get_dependency_details(\%currfile,\%currsubfile,$oldfile);
                   9772:         $delete_output .= &start_data_table_row().
                   9773:                           '<td><img src="'.&icon($oldfile).'" />'.
                   9774:                           '&nbsp;<span class="LC_filename">'.$oldfile.'</span></td>'.
                   9775:                           '<td>'.$size.'</td>'.
                   9776:                           '<td>'.$mtime.'</td>'.
                   9777:                           '<td><label><input type="checkbox" name="del_upload_dep" '.
                   9778:                           ' value="'.$delidx.'" />'.&mt('Yes').'</label>'.
                   9779:                           &embedded_file_element('upload_embedded',$delidx,
                   9780:                                                  $oldfile,\%mapping,$allfiles,
                   9781:                                                  $codebase,'delete').'</td>'.
                   9782:                           &end_data_table_row()."\n"; 
                   9783:         $numunused ++;
                   9784:         $delidx ++;
1.987     raeburn  9785:     }
                   9786:     if ($upload_output) {
                   9787:         $upload_output = &start_data_table().
                   9788:                          $upload_output.
                   9789:                          &end_data_table()."\n";
                   9790:     }
1.1071    raeburn  9791:     if ($modify_output) {
                   9792:         $modify_output = &start_data_table().
                   9793:                          &start_data_table_header_row().
                   9794:                          '<th>'.&mt('File').'</th>'.
                   9795:                          '<th>'.&mt('Size (KB)').'</th>'.
                   9796:                          '<th>'.&mt('Modified').'</th>'.
                   9797:                          '<th>'.&mt('Upload replacement?').'</th>'.
                   9798:                          &end_data_table_header_row().
                   9799:                          $modify_output.
                   9800:                          &end_data_table()."\n";
                   9801:     }
                   9802:     if ($delete_output) {
                   9803:         $delete_output = &start_data_table().
                   9804:                          &start_data_table_header_row().
                   9805:                          '<th>'.&mt('File').'</th>'.
                   9806:                          '<th>'.&mt('Size (KB)').'</th>'.
                   9807:                          '<th>'.&mt('Modified').'</th>'.
                   9808:                          '<th>'.&mt('Delete?').'</th>'.
                   9809:                          &end_data_table_header_row().
                   9810:                          $delete_output.
                   9811:                          &end_data_table()."\n";
                   9812:     }
1.987     raeburn  9813:     my $applies = 0;
                   9814:     if ($numremref) {
                   9815:         $applies ++;
                   9816:     }
                   9817:     if ($numinvalid) {
                   9818:         $applies ++;
                   9819:     }
                   9820:     if ($numexisting) {
                   9821:         $applies ++;
                   9822:     }
1.1071    raeburn  9823:     if ($counter || $numunused) {
1.987     raeburn  9824:         $output = '<form name="upload_embedded" action="'.$actionurl.'"'.
                   9825:                   ' method="post" enctype="multipart/form-data">'."\n".
1.1071    raeburn  9826:                   $state.'<h3>'.$heading.'</h3>'; 
                   9827:         if ($actionurl eq '/adm/dependencies') {
                   9828:             if ($numnew) {
                   9829:                 $output .= '<h4>'.&mt('Missing dependencies').'</h4>'.
                   9830:                            '<p>'.&mt('The following files need to be uploaded.').'</p>'."\n".
                   9831:                            $upload_output.'<br />'."\n";
                   9832:             }
                   9833:             if ($numexisting) {
                   9834:                 $output .= '<h4>'.&mt('Uploaded dependencies (in use)').'</h4>'.
                   9835:                            '<p>'.&mt('Upload a new file to replace the one currently in use.').'</p>'."\n".
                   9836:                            $modify_output.'<br />'."\n";
                   9837:                            $buttontext = &mt('Save changes');
                   9838:             }
                   9839:             if ($numunused) {
                   9840:                 $output .= '<h4>'.&mt('Unused files').'</h4>'.
                   9841:                            '<p>'.&mt('The following uploaded files are no longer used.').'</p>'."\n".
                   9842:                            $delete_output.'<br />'."\n";
                   9843:                            $buttontext = &mt('Save changes');
                   9844:             }
                   9845:         } else {
                   9846:             $output .= $upload_output.'<br />'."\n";
                   9847:         }
                   9848:         $output .= '<input type ="hidden" name="number_embedded_items" value="'.
                   9849:                    $counter.'" />'."\n";
                   9850:         if ($actionurl eq '/adm/dependencies') { 
                   9851:             $output .= '<input type ="hidden" name="number_newemb_items" value="'.
                   9852:                        $numnew.'" />'."\n";
                   9853:         } elsif ($actionurl eq '') {
1.987     raeburn  9854:             $output .=  '<input type="hidden" name="phase" value="three" />';
                   9855:         }
                   9856:     } elsif ($applies) {
                   9857:         $output = '<b>'.&mt('Referenced files').'</b>:<br />';
                   9858:         if ($applies > 1) {
                   9859:             $output .=  
                   9860:                 &mt('No files need to be uploaded, as one of the following applies to each reference:').'<ul>';
                   9861:             if ($numremref) {
                   9862:                 $output .= '<li>'.&mt('reference is to a URL which points to another server').'</li>'."\n";
                   9863:             }
                   9864:             if ($numinvalid) {
                   9865:                 $output .= '<li>'.&mt('reference is to file with a name containing invalid characters').'</li>'."\n";
                   9866:             }
                   9867:             if ($numexisting) {
                   9868:                 $output .= '<li>'.&mt('reference is to an existing file at the specified location').'</li>'."\n";
                   9869:             }
                   9870:             $output .= '</ul><br />';
                   9871:         } elsif ($numremref) {
                   9872:             $output .= '<p>'.&mt('None to upload, as all references are to URLs pointing to another server.').'</p>';
                   9873:         } elsif ($numinvalid) {
                   9874:             $output .= '<p>'.&mt('None to upload, as all references are to files with names containing invalid characters.').'</p>';
                   9875:         } elsif ($numexisting) {
                   9876:             $output .= '<p>'.&mt('None to upload, as all references are to existing files.').'</p>';
                   9877:         }
                   9878:         $output .= $upload_output.'<br />';
                   9879:     }
                   9880:     my ($pathchange_output,$chgcount);
1.1071    raeburn  9881:     $chgcount = $counter;
1.987     raeburn  9882:     if (keys(%pathchanges) > 0) {
                   9883:         foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%pathchanges)) {
1.1071    raeburn  9884:             if ($counter) {
1.987     raeburn  9885:                 $output .= &embedded_file_element('pathchange',$chgcount,
                   9886:                                                   $embed_file,\%mapping,
1.1071    raeburn  9887:                                                   $allfiles,$codebase,'change');
1.987     raeburn  9888:             } else {
                   9889:                 $pathchange_output .= 
                   9890:                     &start_data_table_row().
                   9891:                     '<td><input type ="checkbox" name="namechange" value="'.
                   9892:                     $chgcount.'" checked="checked" /></td>'.
                   9893:                     '<td>'.$mapping{$embed_file}.'</td>'.
                   9894:                     '<td>'.$embed_file.
                   9895:                     &embedded_file_element('pathchange',$numpathchg,$embed_file,
1.1071    raeburn  9896:                                            \%mapping,$allfiles,$codebase,'change').
1.987     raeburn  9897:                     '</td>'.&end_data_table_row();
1.660     raeburn  9898:             }
1.987     raeburn  9899:             $numpathchg ++;
                   9900:             $chgcount ++;
1.660     raeburn  9901:         }
                   9902:     }
1.1071    raeburn  9903:     if ($counter) {
1.987     raeburn  9904:         if ($numpathchg) {
                   9905:             $output .= '<input type ="hidden" name="number_pathchange_items" value="'.
                   9906:                        $numpathchg.'" />'."\n";
                   9907:         }
                   9908:         if (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank') || 
                   9909:             ($actionurl eq '/adm/imsimport')) {
                   9910:             $output .= '<input type="hidden" name="phase" value="three" />'."\n";
                   9911:         } elsif ($actionurl eq '/adm/portfolio' || $actionurl eq '/adm/coursegrp_portfolio') {
                   9912:             $output .= '<input type="hidden" name="action" value="upload_embedded" />';
1.1071    raeburn  9913:         } elsif ($actionurl eq '/adm/dependencies') {
                   9914:             $output .= '<input type="hidden" name="action" value="process_changes" />';
1.987     raeburn  9915:         }
1.1071    raeburn  9916:         $output .=  '<input type ="submit" value="'.$buttontext.'" />'."\n".'</form>'."\n";
1.987     raeburn  9917:     } elsif ($numpathchg) {
                   9918:         my %pathchange = ();
                   9919:         $output .= &modify_html_form('pathchange',$actionurl,$state,\%pathchange,$pathchange_output);
                   9920:         if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
                   9921:             $output .= '<p>'.&mt('or').'</p>'; 
                   9922:         } 
                   9923:     }
1.1071    raeburn  9924:     return ($output,$counter,$numpathchg);
1.987     raeburn  9925: }
                   9926: 
                   9927: sub embedded_file_element {
1.1071    raeburn  9928:     my ($context,$num,$embed_file,$mapping,$allfiles,$codebase,$type) = @_;
1.987     raeburn  9929:     return unless ((ref($mapping) eq 'HASH') && (ref($allfiles) eq 'HASH') &&
                   9930:                    (ref($codebase) eq 'HASH'));
                   9931:     my $output;
1.1071    raeburn  9932:     if (($context eq 'upload_embedded') && ($type ne 'delete')) {
1.987     raeburn  9933:        $output = '<input name="embedded_item_'.$num.'" type="file" value="" />'."\n";
                   9934:     }
                   9935:     $output .= '<input name="embedded_orig_'.$num.'" type="hidden" value="'.
                   9936:                &escape($embed_file).'" />';
                   9937:     unless (($context eq 'upload_embedded') && 
                   9938:             ($mapping->{$embed_file} eq $embed_file)) {
                   9939:         $output .='
                   9940:         <input name="embedded_ref_'.$num.'" type="hidden" value="'.&escape($mapping->{$embed_file}).'" />';
                   9941:     }
                   9942:     my $attrib;
                   9943:     if (ref($allfiles->{$mapping->{$embed_file}}) eq 'ARRAY') {
                   9944:         $attrib = &escape(join(':',@{$allfiles->{$mapping->{$embed_file}}}));
                   9945:     }
                   9946:     $output .=
                   9947:         "\n\t\t".
                   9948:         '<input name="embedded_attrib_'.$num.'" type="hidden" value="'.
                   9949:         $attrib.'" />';
                   9950:     if (exists($codebase->{$mapping->{$embed_file}})) {
                   9951:         $output .=
                   9952:             "\n\t\t".
                   9953:             '<input name="codebase_'.$num.'" type="hidden" value="'.
                   9954:             &escape($codebase->{$mapping->{$embed_file}}).'" />';
1.984     raeburn  9955:     }
1.987     raeburn  9956:     return $output;
1.660     raeburn  9957: }
                   9958: 
1.1071    raeburn  9959: sub get_dependency_details {
                   9960:     my ($currfile,$currsubfile,$embed_file) = @_;
                   9961:     my ($size,$mtime,$showsize,$showmtime);
                   9962:     if ((ref($currfile) eq 'HASH') && (ref($currsubfile))) {
                   9963:         if ($embed_file =~ m{/}) {
                   9964:             my ($path,$fname) = split(/\//,$embed_file);
                   9965:             if (ref($currsubfile->{$path}{$fname}) eq 'ARRAY') {
                   9966:                 ($size,$mtime) = @{$currsubfile->{$path}{$fname}};
                   9967:             }
                   9968:         } else {
                   9969:             if (ref($currfile->{$embed_file}) eq 'ARRAY') {
                   9970:                 ($size,$mtime) = @{$currfile->{$embed_file}};
                   9971:             }
                   9972:         }
                   9973:         $showsize = $size/1024.0;
                   9974:         $showsize = sprintf("%.1f",$showsize);
                   9975:         if ($mtime > 0) {
                   9976:             $showmtime = &Apache::lonlocal::locallocaltime($mtime);
                   9977:         }
                   9978:     }
                   9979:     return ($showsize,$showmtime);
                   9980: }
                   9981: 
                   9982: sub ask_embedded_js {
                   9983:     return <<"END";
                   9984: <script type="text/javascript"">
                   9985: // <![CDATA[
                   9986: function toggleBrowse(counter) {
                   9987:     var chkboxid = document.getElementById('mod_upload_dep_'+counter);
                   9988:     var fileid = document.getElementById('embedded_item_'+counter);
                   9989:     var uploaddivid = document.getElementById('moduploaddep_'+counter);
                   9990:     if (chkboxid.checked == true) {
                   9991:         uploaddivid.style.display='block';
                   9992:     } else {
                   9993:         uploaddivid.style.display='none';
                   9994:         fileid.value = '';
                   9995:     }
                   9996: }
                   9997: // ]]>
                   9998: </script>
                   9999: 
                   10000: END
                   10001: }
                   10002: 
1.661     raeburn  10003: sub upload_embedded {
                   10004:     my ($context,$dirpath,$uname,$udom,$dir_root,$url_root,$group,$disk_quota,
1.987     raeburn  10005:         $current_disk_usage,$hiddenstate,$actionurl) = @_;
                   10006:     my (%pathchange,$output,$modifyform,$footer,$returnflag);
1.661     raeburn  10007:     for (my $i=0; $i<$env{'form.number_embedded_items'}; $i++) {
                   10008:         next if (!exists($env{'form.embedded_item_'.$i.'.filename'}));
                   10009:         my $orig_uploaded_filename =
                   10010:             $env{'form.embedded_item_'.$i.'.filename'};
1.987     raeburn  10011:         foreach my $type ('orig','ref','attrib','codebase') {
                   10012:             if ($env{'form.embedded_'.$type.'_'.$i} ne '') {
                   10013:                 $env{'form.embedded_'.$type.'_'.$i} =
                   10014:                     &unescape($env{'form.embedded_'.$type.'_'.$i});
                   10015:             }
                   10016:         }
1.661     raeburn  10017:         my ($path,$fname) =
                   10018:             ($env{'form.embedded_orig_'.$i} =~ m{(.*/)([^/]*)});
                   10019:         # no path, whole string is fname
                   10020:         if (!$fname) { $fname = $env{'form.embedded_orig_'.$i} };
                   10021:         $fname = &Apache::lonnet::clean_filename($fname);
                   10022:         # See if there is anything left
                   10023:         next if ($fname eq '');
                   10024: 
                   10025:         # Check if file already exists as a file or directory.
                   10026:         my ($state,$msg);
                   10027:         if ($context eq 'portfolio') {
                   10028:             my $port_path = $dirpath;
                   10029:             if ($group ne '') {
                   10030:                 $port_path = "groups/$group/$port_path";
                   10031:             }
1.987     raeburn  10032:             ($state,$msg) = &check_for_upload($env{'form.currentpath'}.$path,
                   10033:                                               $fname,$group,'embedded_item_'.$i,
1.661     raeburn  10034:                                               $dir_root,$port_path,$disk_quota,
                   10035:                                               $current_disk_usage,$uname,$udom);
                   10036:             if ($state eq 'will_exceed_quota'
1.984     raeburn  10037:                 || $state eq 'file_locked') {
1.661     raeburn  10038:                 $output .= $msg;
                   10039:                 next;
                   10040:             }
                   10041:         } elsif (($context eq 'author') || ($context eq 'testbank')) {
                   10042:             ($state,$msg) = &check_for_existing($path,$fname,'embedded_item_'.$i);
                   10043:             if ($state eq 'exists') {
                   10044:                 $output .= $msg;
                   10045:                 next;
                   10046:             }
                   10047:         }
                   10048:         # Check if extension is valid
                   10049:         if (($fname =~ /\.(\w+)$/) &&
                   10050:             (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.987     raeburn  10051:             $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  10052:             next;
                   10053:         } elsif (($fname =~ /\.(\w+)$/) &&
                   10054:                  (!defined(&Apache::loncommon::fileembstyle($1)))) {
1.987     raeburn  10055:             $output .= &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1).'<br />';
1.661     raeburn  10056:             next;
                   10057:         } elsif ($fname=~/\.(\d+)\.(\w+)$/) {
1.987     raeburn  10058:             $output .= &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2).'<br />';
1.661     raeburn  10059:             next;
                   10060:         }
                   10061:         $env{'form.embedded_item_'.$i.'.filename'}=$fname;
                   10062:         if ($context eq 'portfolio') {
1.984     raeburn  10063:             my $result;
                   10064:             if ($state eq 'existingfile') {
                   10065:                 $result=
                   10066:                     &Apache::lonnet::userfileupload('embedded_item_'.$i,'existingfile',
1.987     raeburn  10067:                                                     $dirpath.$env{'form.currentpath'}.$path);
1.661     raeburn  10068:             } else {
1.984     raeburn  10069:                 $result=
                   10070:                     &Apache::lonnet::userfileupload('embedded_item_'.$i,'',
1.987     raeburn  10071:                                                     $dirpath.
                   10072:                                                     $env{'form.currentpath'}.$path);
1.984     raeburn  10073:                 if ($result !~ m|^/uploaded/|) {
                   10074:                     $output .= '<span class="LC_error">'
                   10075:                                .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
                   10076:                                ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
                   10077:                                .'</span><br />';
                   10078:                     next;
                   10079:                 } else {
1.987     raeburn  10080:                     $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                   10081:                                $path.$fname.'</span>').'<br />';     
1.984     raeburn  10082:                 }
1.661     raeburn  10083:             }
1.987     raeburn  10084:         } elsif ($context eq 'coursedoc') {
                   10085:             my $result =
                   10086:                 &Apache::lonnet::userfileupload('embedded_item_'.$i,'coursedoc',
                   10087:                                                 $dirpath.'/'.$path);
                   10088:             if ($result !~ m|^/uploaded/|) {
                   10089:                 $output .= '<span class="LC_error">'
                   10090:                            .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
                   10091:                            ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
                   10092:                            .'</span><br />';
                   10093:                     next;
                   10094:             } else {
                   10095:                 $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                   10096:                            $path.$fname.'</span>').'<br />';
                   10097:             }
1.661     raeburn  10098:         } else {
                   10099: # Save the file
                   10100:             my $target = $env{'form.embedded_item_'.$i};
                   10101:             my $fullpath = $dir_root.$dirpath.'/'.$path;
                   10102:             my $dest = $fullpath.$fname;
                   10103:             my $url = $url_root.$dirpath.'/'.$path.$fname;
1.1027    raeburn  10104:             my @parts=split(/\//,"$dirpath/$path");
1.661     raeburn  10105:             my $count;
                   10106:             my $filepath = $dir_root;
1.1027    raeburn  10107:             foreach my $subdir (@parts) {
                   10108:                 $filepath .= "/$subdir";
                   10109:                 if (!-e $filepath) {
1.661     raeburn  10110:                     mkdir($filepath,0770);
                   10111:                 }
                   10112:             }
                   10113:             my $fh;
                   10114:             if (!open($fh,'>'.$dest)) {
                   10115:                 &Apache::lonnet::logthis('Failed to create '.$dest);
                   10116:                 $output .= '<span class="LC_error">'.
1.1071    raeburn  10117:                            &mt('An error occurred while trying to upload [_1] for embedded element [_2].',
                   10118:                                $orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
1.661     raeburn  10119:                            '</span><br />';
                   10120:             } else {
                   10121:                 if (!print $fh $env{'form.embedded_item_'.$i}) {
                   10122:                     &Apache::lonnet::logthis('Failed to write to '.$dest);
                   10123:                     $output .= '<span class="LC_error">'.
1.1071    raeburn  10124:                               &mt('An error occurred while writing the file [_1] for embedded element [_2].',
                   10125:                                   $orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
1.661     raeburn  10126:                               '</span><br />';
                   10127:                 } else {
1.987     raeburn  10128:                     $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                   10129:                                $url.'</span>').'<br />';
                   10130:                     unless ($context eq 'testbank') {
                   10131:                         $footer .= &mt('View embedded file: [_1]',
                   10132:                                        '<a href="'.$url.'">'.$fname.'</a>').'<br />';
                   10133:                     }
                   10134:                 }
                   10135:                 close($fh);
                   10136:             }
                   10137:         }
                   10138:         if ($env{'form.embedded_ref_'.$i}) {
                   10139:             $pathchange{$i} = 1;
                   10140:         }
                   10141:     }
                   10142:     if ($output) {
                   10143:         $output = '<p>'.$output.'</p>';
                   10144:     }
                   10145:     $output .= &modify_html_form('upload_embedded',$actionurl,$hiddenstate,\%pathchange);
                   10146:     $returnflag = 'ok';
1.1071    raeburn  10147:     my $numpathchgs = scalar(keys(%pathchange));
                   10148:     if ($numpathchgs > 0) {
1.987     raeburn  10149:         if ($context eq 'portfolio') {
                   10150:             $output .= '<p>'.&mt('or').'</p>';
                   10151:         } elsif ($context eq 'testbank') {
1.1071    raeburn  10152:             $output .=  '<p>'.&mt('Or [_1]continue[_2] the testbank import without modifying the reference(s).',
                   10153:                                   '<a href="javascript:document.testbankForm.submit();">','</a>').'</p>';
1.987     raeburn  10154:             $returnflag = 'modify_orightml';
                   10155:         }
                   10156:     }
1.1071    raeburn  10157:     return ($output.$footer,$returnflag,$numpathchgs);
1.987     raeburn  10158: }
                   10159: 
                   10160: sub modify_html_form {
                   10161:     my ($context,$actionurl,$hiddenstate,$pathchange,$pathchgtable) = @_;
                   10162:     my $end = 0;
                   10163:     my $modifyform;
                   10164:     if ($context eq 'upload_embedded') {
                   10165:         return unless (ref($pathchange) eq 'HASH');
                   10166:         if ($env{'form.number_embedded_items'}) {
                   10167:             $end += $env{'form.number_embedded_items'};
                   10168:         }
                   10169:         if ($env{'form.number_pathchange_items'}) {
                   10170:             $end += $env{'form.number_pathchange_items'};
                   10171:         }
                   10172:         if ($end) {
                   10173:             for (my $i=0; $i<$end; $i++) {
                   10174:                 if ($i < $env{'form.number_embedded_items'}) {
                   10175:                     next unless($pathchange->{$i});
                   10176:                 }
                   10177:                 $modifyform .=
                   10178:                     &start_data_table_row().
                   10179:                     '<td><input type ="checkbox" name="namechange" value="'.$i.'" '.
                   10180:                     'checked="checked" /></td>'.
                   10181:                     '<td>'.$env{'form.embedded_ref_'.$i}.
                   10182:                     '<input type="hidden" name="embedded_ref_'.$i.'" value="'.
                   10183:                     &escape($env{'form.embedded_ref_'.$i}).'" />'.
                   10184:                     '<input type="hidden" name="embedded_codebase_'.$i.'" value="'.
                   10185:                     &escape($env{'form.embedded_codebase_'.$i}).'" />'.
                   10186:                     '<input type="hidden" name="embedded_attrib_'.$i.'" value="'.
                   10187:                     &escape($env{'form.embedded_attrib_'.$i}).'" /></td>'.
                   10188:                     '<td>'.$env{'form.embedded_orig_'.$i}.
                   10189:                     '<input type="hidden" name="embedded_orig_'.$i.'" value="'.
                   10190:                     &escape($env{'form.embedded_orig_'.$i}).'" /></td>'.
                   10191:                     &end_data_table_row();
1.1071    raeburn  10192:             }
1.987     raeburn  10193:         }
                   10194:     } else {
                   10195:         $modifyform = $pathchgtable;
                   10196:         if (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
                   10197:             $hiddenstate .= '<input type="hidden" name="phase" value="four" />';
                   10198:         } elsif (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
                   10199:             $hiddenstate .= '<input type="hidden" name="action" value="modify_orightml" />';
                   10200:         }
                   10201:     }
                   10202:     if ($modifyform) {
1.1071    raeburn  10203:         if ($actionurl eq '/adm/dependencies') {
                   10204:             $hiddenstate .= '<input type="hidden" name="action" value="modifyhrefs" />';
                   10205:         }
1.987     raeburn  10206:         return '<h3>'.&mt('Changes in content of HTML file required').'</h3>'."\n".
                   10207:                '<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".
                   10208:                '<li>'.&mt('For consistency between the reference(s) and the location of the corresponding stored file within LON-CAPA.').'</li>'."\n".
                   10209:                '<li>'.&mt('To change absolute paths to relative paths, or replace directory traversal via "../" within the original reference.').'</li>'."\n".
                   10210:                '</ol></p>'."\n".'<p>'.
                   10211:                &mt('LON-CAPA can make the required changes to your HTML file.').'</p>'."\n".
                   10212:                '<form method="post" name="refchanger" action="'.$actionurl.'">'.
                   10213:                &start_data_table()."\n".
                   10214:                &start_data_table_header_row().
                   10215:                '<th>'.&mt('Change?').'</th>'.
                   10216:                '<th>'.&mt('Current reference').'</th>'.
                   10217:                '<th>'.&mt('Required reference').'</th>'.
                   10218:                &end_data_table_header_row()."\n".
                   10219:                $modifyform.
                   10220:                &end_data_table().'<br />'."\n".$hiddenstate.
                   10221:                '<input type="submit" name="pathchanges" value="'.&mt('Modify HTML file').'" />'.
                   10222:                '</form>'."\n";
                   10223:     }
                   10224:     return;
                   10225: }
                   10226: 
                   10227: sub modify_html_refs {
                   10228:     my ($context,$dirpath,$uname,$udom,$dir_root) = @_;
                   10229:     my $container;
                   10230:     if ($context eq 'portfolio') {
                   10231:         $container = $env{'form.container'};
                   10232:     } elsif ($context eq 'coursedoc') {
                   10233:         $container = $env{'form.primaryurl'};
1.1071    raeburn  10234:     } elsif ($context eq 'manage_dependencies') {
                   10235:         (undef,undef,$container) = &Apache::lonnet::decode_symb($env{'form.symb'});
                   10236:         $container = "/$container";
1.987     raeburn  10237:     } else {
1.1027    raeburn  10238:         $container = $Apache::lonnet::perlvar{'lonDocRoot'}.$env{'form.filename'};
1.987     raeburn  10239:     }
                   10240:     my (%allfiles,%codebase,$output,$content);
                   10241:     my @changes = &get_env_multiple('form.namechange');
1.1071    raeburn  10242:     unless (@changes > 0) {
                   10243:         if (wantarray) {
                   10244:             return ('',0,0); 
                   10245:         } else {
                   10246:             return;
                   10247:         }
                   10248:     }
                   10249:     if (($context eq 'portfolio') || ($context eq 'coursedoc') || 
                   10250:         ($context eq 'manage_dependencies')) {
                   10251:         unless ($container =~ m{^/uploaded/\Q$udom\E/\Q$uname\E/}) {
                   10252:             if (wantarray) {
                   10253:                 return ('',0,0);
                   10254:             } else {
                   10255:                 return;
                   10256:             }
                   10257:         } 
1.987     raeburn  10258:         $content = &Apache::lonnet::getfile($container);
1.1071    raeburn  10259:         if ($content eq '-1') {
                   10260:             if (wantarray) {
                   10261:                 return ('',0,0);
                   10262:             } else {
                   10263:                 return;
                   10264:             }
                   10265:         }
1.987     raeburn  10266:     } else {
1.1071    raeburn  10267:         unless ($container =~ /^\Q$dir_root\E/) {
                   10268:             if (wantarray) {
                   10269:                 return ('',0,0);
                   10270:             } else {
                   10271:                 return;
                   10272:             }
                   10273:         } 
1.987     raeburn  10274:         if (open(my $fh,"<$container")) {
                   10275:             $content = join('', <$fh>);
                   10276:             close($fh);
                   10277:         } else {
1.1071    raeburn  10278:             if (wantarray) {
                   10279:                 return ('',0,0);
                   10280:             } else {
                   10281:                 return;
                   10282:             }
1.987     raeburn  10283:         }
                   10284:     }
                   10285:     my ($count,$codebasecount) = (0,0);
                   10286:     my $mm = new File::MMagic;
                   10287:     my $mime_type = $mm->checktype_contents($content);
                   10288:     if ($mime_type eq 'text/html') {
                   10289:         my $parse_result = 
                   10290:             &Apache::lonnet::extract_embedded_items($container,\%allfiles,
                   10291:                                                     \%codebase,\$content);
                   10292:         if ($parse_result eq 'ok') {
                   10293:             foreach my $i (@changes) {
                   10294:                 my $orig = &unescape($env{'form.embedded_orig_'.$i});
                   10295:                 my $ref = &unescape($env{'form.embedded_ref_'.$i});
                   10296:                 if ($allfiles{$ref}) {
                   10297:                     my $newname =  $orig;
                   10298:                     my ($attrib_regexp,$codebase);
1.1006    raeburn  10299:                     $attrib_regexp = &unescape($env{'form.embedded_attrib_'.$i});
1.987     raeburn  10300:                     if ($attrib_regexp =~ /:/) {
                   10301:                         $attrib_regexp =~ s/\:/|/g;
                   10302:                     }
                   10303:                     if ($content =~ m{($attrib_regexp\s*=\s*['"]?)\Q$ref\E(['"]?)}) {
                   10304:                         my $numchg = ($content =~ s{($attrib_regexp\s*=\s*['"]?)\Q$ref\E(['"]?)}{$1$newname$2}gi);
                   10305:                         $count += $numchg;
                   10306:                     }
                   10307:                     if ($env{'form.embedded_codebase_'.$i} ne '') {
1.1006    raeburn  10308:                         $codebase = &unescape($env{'form.embedded_codebase_'.$i});
1.987     raeburn  10309:                         my $numchg = ($content =~ s/(codebase\s*=\s*["']?)\Q$codebase\E(["']?)/$1.$2/i); #' stupid emacs
                   10310:                         $codebasecount ++;
                   10311:                     }
                   10312:                 }
                   10313:             }
                   10314:             if ($count || $codebasecount) {
                   10315:                 my $saveresult;
1.1071    raeburn  10316:                 if (($context eq 'portfolio') || ($context eq 'coursedoc') || 
                   10317:                     ($context eq 'manage_dependencies')) {
1.987     raeburn  10318:                     my $url = &Apache::lonnet::store_edited_file($container,$content,$udom,$uname,\$saveresult);
                   10319:                     if ($url eq $container) {
                   10320:                         my ($fname) = ($container =~ m{/([^/]+)$});
                   10321:                         $output = '<p>'.&mt('Updated [quant,_1,reference] in [_2].',
                   10322:                                             $count,'<span class="LC_filename">'.
1.1071    raeburn  10323:                                             $fname.'</span>').'</p>';
1.987     raeburn  10324:                     } else {
                   10325:                          $output = '<p class="LC_error">'.
                   10326:                                    &mt('Error: update failed for: [_1].',
                   10327:                                    '<span class="LC_filename">'.
                   10328:                                    $container.'</span>').'</p>';
                   10329:                     }
                   10330:                 } else {
                   10331:                     if (open(my $fh,">$container")) {
                   10332:                         print $fh $content;
                   10333:                         close($fh);
                   10334:                         $output = '<p>'.&mt('Updated [quant,_1,reference] in [_2].',
                   10335:                                   $count,'<span class="LC_filename">'.
                   10336:                                   $container.'</span>').'</p>';
1.661     raeburn  10337:                     } else {
1.987     raeburn  10338:                          $output = '<p class="LC_error">'.
                   10339:                                    &mt('Error: could not update [_1].',
                   10340:                                    '<span class="LC_filename">'.
                   10341:                                    $container.'</span>').'</p>';
1.661     raeburn  10342:                     }
                   10343:                 }
                   10344:             }
1.987     raeburn  10345:         } else {
                   10346:             &logthis('Failed to parse '.$container.
                   10347:                      ' to modify references: '.$parse_result);
1.661     raeburn  10348:         }
                   10349:     }
1.1071    raeburn  10350:     if (wantarray) {
                   10351:         return ($output,$count,$codebasecount);
                   10352:     } else {
                   10353:         return $output;
                   10354:     }
1.661     raeburn  10355: }
                   10356: 
                   10357: sub check_for_existing {
                   10358:     my ($path,$fname,$element) = @_;
                   10359:     my ($state,$msg);
                   10360:     if (-d $path.'/'.$fname) {
                   10361:         $state = 'exists';
                   10362:         $msg = &mt('Unable to upload [_1]. A directory by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
                   10363:     } elsif (-e $path.'/'.$fname) {
                   10364:         $state = 'exists';
                   10365:         $msg = &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
                   10366:     }
                   10367:     if ($state eq 'exists') {
                   10368:         $msg = '<span class="LC_error">'.$msg.'</span><br />';
                   10369:     }
                   10370:     return ($state,$msg);
                   10371: }
                   10372: 
                   10373: sub check_for_upload {
                   10374:     my ($path,$fname,$group,$element,$portfolio_root,$port_path,
                   10375:         $disk_quota,$current_disk_usage,$uname,$udom) = @_;
1.985     raeburn  10376:     my $filesize = length($env{'form.'.$element});
                   10377:     if (!$filesize) {
                   10378:         my $msg = '<span class="LC_error">'.
                   10379:                   &mt('Unable to upload [_1]. (size = [_2] bytes)', 
                   10380:                       '<span class="LC_filename">'.$fname.'</span>',
                   10381:                       $filesize).'<br />'.
1.1007    raeburn  10382:                   &mt('Either the file you attempted to upload was empty, or your web browser was unable to read its contents.').'<br />'.
1.985     raeburn  10383:                   '</span>';
                   10384:         return ('zero_bytes',$msg);
                   10385:     }
                   10386:     $filesize =  $filesize/1000; #express in k (1024?)
1.661     raeburn  10387:     my $getpropath = 1;
1.1021    raeburn  10388:     my ($dirlistref,$listerror) =
                   10389:          &Apache::lonnet::dirlist($portfolio_root.$path,$udom,$uname,$getpropath);
1.661     raeburn  10390:     my $found_file = 0;
                   10391:     my $locked_file = 0;
1.991     raeburn  10392:     my @lockers;
                   10393:     my $navmap;
                   10394:     if ($env{'request.course.id'}) {
                   10395:         $navmap = Apache::lonnavmaps::navmap->new();
                   10396:     }
1.1021    raeburn  10397:     if (ref($dirlistref) eq 'ARRAY') {
                   10398:         foreach my $line (@{$dirlistref}) {
                   10399:             my ($file_name,$rest)=split(/\&/,$line,2);
                   10400:             if ($file_name eq $fname){
                   10401:                 $file_name = $path.$file_name;
                   10402:                 if ($group ne '') {
                   10403:                     $file_name = $group.$file_name;
                   10404:                 }
                   10405:                 $found_file = 1;
                   10406:                 if (&Apache::lonnet::is_locked($file_name,$udom,$uname,\@lockers) eq 'true') {
                   10407:                     foreach my $lock (@lockers) {
                   10408:                         if (ref($lock) eq 'ARRAY') {
                   10409:                             my ($symb,$crsid) = @{$lock};
                   10410:                             if ($crsid eq $env{'request.course.id'}) {
                   10411:                                 if (ref($navmap)) {
                   10412:                                     my $res = $navmap->getBySymb($symb);
                   10413:                                     foreach my $part (@{$res->parts()}) { 
                   10414:                                         my ($slot_status,$slot_time,$slot_name)=$res->check_for_slot($part);
                   10415:                                         unless (($slot_status == $res->RESERVED) ||
                   10416:                                                 ($slot_status == $res->RESERVED_LOCATION)) {
                   10417:                                             $locked_file = 1;
                   10418:                                         }
1.991     raeburn  10419:                                     }
1.1021    raeburn  10420:                                 } else {
                   10421:                                     $locked_file = 1;
1.991     raeburn  10422:                                 }
                   10423:                             } else {
                   10424:                                 $locked_file = 1;
                   10425:                             }
                   10426:                         }
1.1021    raeburn  10427:                    }
                   10428:                 } else {
                   10429:                     my @info = split(/\&/,$rest);
                   10430:                     my $currsize = $info[6]/1000;
                   10431:                     if ($currsize < $filesize) {
                   10432:                         my $extra = $filesize - $currsize;
                   10433:                         if (($current_disk_usage + $extra) > $disk_quota) {
                   10434:                             my $msg = '<span class="LC_error">'.
                   10435:                                       &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.',
                   10436:                                           '<span class="LC_filename">'.$fname.'</span>',$filesize,$currsize).'</span>'.
                   10437:                                       '<br />'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',
                   10438:                                                    $disk_quota,$current_disk_usage);
                   10439:                             return ('will_exceed_quota',$msg);
                   10440:                         }
1.984     raeburn  10441:                     }
                   10442:                 }
1.661     raeburn  10443:             }
                   10444:         }
                   10445:     }
                   10446:     if (($current_disk_usage + $filesize) > $disk_quota){
                   10447:         my $msg = '<span class="LC_error">'.
                   10448:                 &mt('Unable to upload [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.','<span class="LC_filename">'.$fname.'</span>',$filesize).'</span>'.
                   10449:                   '<br />'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',$disk_quota,$current_disk_usage);
                   10450:         return ('will_exceed_quota',$msg);
                   10451:     } elsif ($found_file) {
                   10452:         if ($locked_file) {
                   10453:             my $msg = '<span class="LC_error">';
                   10454:             $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>');
                   10455:             $msg .= '</span><br />';
                   10456:             $msg .= &mt('You will be able to rename or delete existing [_1] after a grade has been assigned.','<span class="LC_filename">'.$fname.'</span>');
                   10457:             return ('file_locked',$msg);
                   10458:         } else {
                   10459:             my $msg = '<span class="LC_error">';
1.984     raeburn  10460:             $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  10461:             $msg .= '</span>';
1.984     raeburn  10462:             return ('existingfile',$msg);
1.661     raeburn  10463:         }
                   10464:     }
                   10465: }
                   10466: 
1.987     raeburn  10467: sub check_for_traversal {
                   10468:     my ($path,$url,$toplevel) = @_;
                   10469:     my @parts=split(/\//,$path);
                   10470:     my $cleanpath;
                   10471:     my $fullpath = $url;
                   10472:     for (my $i=0;$i<@parts;$i++) {
                   10473:         next if ($parts[$i] eq '.');
                   10474:         if ($parts[$i] eq '..') {
                   10475:             $fullpath =~ s{([^/]+/)$}{};
                   10476:         } else {
                   10477:             $fullpath .= $parts[$i].'/';
                   10478:         }
                   10479:     }
                   10480:     if ($fullpath =~ /^\Q$url\E(.*)$/) {
                   10481:         $cleanpath = $1;
                   10482:     } elsif ($fullpath =~ /^\Q$toplevel\E(.*)$/) {
                   10483:         my $curr_toprel = $1;
                   10484:         my @parts = split(/\//,$curr_toprel);
                   10485:         my ($url_toprel) = ($url =~ /^\Q$toplevel\E(.*)$/);
                   10486:         my @urlparts = split(/\//,$url_toprel);
                   10487:         my $doubledots;
                   10488:         my $startdiff = -1;
                   10489:         for (my $i=0; $i<@urlparts; $i++) {
                   10490:             if ($startdiff == -1) {
                   10491:                 unless ($urlparts[$i] eq $parts[$i]) {
                   10492:                     $startdiff = $i;
                   10493:                     $doubledots .= '../';
                   10494:                 }
                   10495:             } else {
                   10496:                 $doubledots .= '../';
                   10497:             }
                   10498:         }
                   10499:         if ($startdiff > -1) {
                   10500:             $cleanpath = $doubledots;
                   10501:             for (my $i=$startdiff; $i<@parts; $i++) {
                   10502:                 $cleanpath .= $parts[$i].'/';
                   10503:             }
                   10504:         }
                   10505:     }
                   10506:     $cleanpath =~ s{(/)$}{};
                   10507:     return $cleanpath;
                   10508: }
1.31      albertel 10509: 
1.1053    raeburn  10510: sub is_archive_file {
                   10511:     my ($mimetype) = @_;
                   10512:     if (($mimetype eq 'application/octet-stream') ||
                   10513:         ($mimetype eq 'application/x-stuffit') ||
                   10514:         ($mimetype =~ m{^application/(x\-)?(compressed|tar|zip|tgz|gz|gtar|gzip|gunzip|bz|bz2|bzip2)})) {
                   10515:         return 1;
                   10516:     }
                   10517:     return;
                   10518: }
                   10519: 
                   10520: sub decompress_form {
1.1065    raeburn  10521:     my ($mimetype,$archiveurl,$action,$noextract,$hiddenelements,$dirlist) = @_;
1.1053    raeburn  10522:     my %lt = &Apache::lonlocal::texthash (
                   10523:         this => 'This file is an archive file.',
1.1067    raeburn  10524:         camt => 'This file is a Camtasia archive file.',
1.1065    raeburn  10525:         itsc => 'Its contents are as follows:',
1.1053    raeburn  10526:         youm => 'You may wish to extract its contents.',
                   10527:         extr => 'Extract contents',
1.1067    raeburn  10528:         auto => 'LON-CAPA can process the files automatically, or you can decide how each should be handled.',
                   10529:         proa => 'Process automatically?',
1.1053    raeburn  10530:         yes  => 'Yes',
                   10531:         no   => 'No',
1.1067    raeburn  10532:         fold => 'Title for folder containing movie',
                   10533:         movi => 'Title for page containing embedded movie', 
1.1053    raeburn  10534:     );
1.1065    raeburn  10535:     my $fileloc = &Apache::lonnet::filelocation(undef,$archiveurl);
1.1067    raeburn  10536:     my ($is_camtasia,$topdir,%toplevel,@paths);
1.1065    raeburn  10537:     my $info = &list_archive_contents($fileloc,\@paths);
                   10538:     if (@paths) {
                   10539:         foreach my $path (@paths) {
                   10540:             $path =~ s{^/}{};
1.1067    raeburn  10541:             if ($path =~ m{^([^/]+)/$}) {
                   10542:                 $topdir = $1;
                   10543:             }
1.1065    raeburn  10544:             if ($path =~ m{^([^/]+)/}) {
                   10545:                 $toplevel{$1} = $path;
                   10546:             } else {
                   10547:                 $toplevel{$path} = $path;
                   10548:             }
                   10549:         }
                   10550:     }
1.1067    raeburn  10551:     if ($mimetype =~ m{^application/(x\-)?(compressed|zip)}) {
                   10552:         my @camtasia = ("$topdir/","$topdir/index.html",
                   10553:                         "$topdir/media/",
                   10554:                         "$topdir/media/$topdir.mp4",
                   10555:                         "$topdir/media/FirstFrame.png",
                   10556:                         "$topdir/media/player.swf",
                   10557:                         "$topdir/media/swfobject.js",
                   10558:                         "$topdir/media/expressInstall.swf");
                   10559:         my @diffs = &compare_arrays(\@paths,\@camtasia);
                   10560:         if (@diffs == 0) {
                   10561:             $is_camtasia = 1;
                   10562:         }
                   10563:     }
                   10564:     my $output;
                   10565:     if ($is_camtasia) {
                   10566:         $output = <<"ENDCAM";
                   10567: <script type="text/javascript" language="Javascript">
                   10568: // <![CDATA[
                   10569: 
                   10570: function camtasiaToggle() {
                   10571:     for (var i=0; i<document.uploaded_decompress.autoextract_camtasia.length; i++) {
                   10572:         if (document.uploaded_decompress.autoextract_camtasia[i].checked) {
                   10573:             if (document.uploaded_decompress.autoextract_camtasia[i].value == 1) {
                   10574: 
                   10575:                 document.getElementById('camtasia_titles').style.display='block';
                   10576:             } else {
                   10577:                 document.getElementById('camtasia_titles').style.display='none';
                   10578:             }
                   10579:         }
                   10580:     }
                   10581:     return;
                   10582: }
                   10583: 
                   10584: // ]]>
                   10585: </script>
                   10586: <p>$lt{'camt'}</p>
                   10587: ENDCAM
1.1065    raeburn  10588:     } else {
1.1067    raeburn  10589:         $output = '<p>'.$lt{'this'};
                   10590:         if ($info eq '') {
                   10591:             $output .= ' '.$lt{'youm'}.'</p>'."\n";
                   10592:         } else {
                   10593:             $output .= ' '.$lt{'itsc'}.'</p>'."\n".
                   10594:                        '<div><pre>'.$info.'</pre></div>';
                   10595:         }
1.1065    raeburn  10596:     }
1.1067    raeburn  10597:     $output .= '<form name="uploaded_decompress" action="'.$action.'" method="post">'."\n";
1.1065    raeburn  10598:     my $duplicates;
                   10599:     my $num = 0;
                   10600:     if (ref($dirlist) eq 'ARRAY') {
                   10601:         foreach my $item (@{$dirlist}) {
                   10602:             if (ref($item) eq 'ARRAY') {
                   10603:                 if (exists($toplevel{$item->[0]})) {
                   10604:                     $duplicates .= 
                   10605:                         &start_data_table_row().
                   10606:                         '<td><label><input type="radio" name="archive_overwrite_'.$num.'" '.
                   10607:                         'value="0" checked="checked" />'.&mt('No').'</label>'.
                   10608:                         '&nbsp;<label><input type="radio" name="archive_overwrite_'.$num.'" '.
                   10609:                         'value="1" />'.&mt('Yes').'</label>'.
                   10610:                         '<input type="hidden" name="archive_overwrite_name_'.$num.'" value="'.$item->[0].'" /></td>'."\n".
                   10611:                         '<td>'.$item->[0].'</td>';
                   10612:                     if ($item->[2]) {
                   10613:                         $duplicates .= '<td>'.&mt('Directory').'</td>';
                   10614:                     } else {
                   10615:                         $duplicates .= '<td>'.&mt('File').'</td>';
                   10616:                     }
                   10617:                     $duplicates .= '<td>'.$item->[3].'</td>'.
                   10618:                                    '<td>'.
                   10619:                                    &Apache::lonlocal::locallocaltime($item->[4]).
                   10620:                                    '</td>'.
                   10621:                                    &end_data_table_row();
                   10622:                     $num ++;
                   10623:                 }
                   10624:             }
                   10625:         }
                   10626:     }
                   10627:     my $itemcount;
                   10628:     if (@paths > 0) {
                   10629:         $itemcount = scalar(@paths);
                   10630:     } else {
                   10631:         $itemcount = 1;
                   10632:     }
1.1067    raeburn  10633:     if ($is_camtasia) {
                   10634:         $output .= $lt{'auto'}.'<br />'.
                   10635:                    '<span class="LC_nobreak">'.$lt{'proa'}.'<label>'.
                   10636:                    '<input type="radio" name="autoextract_camtasia" value="1" onclick="javascript:camtasiaToggle();" checked="checked" />'.
                   10637:                    $lt{'yes'}.'</label>&nbsp;<label>'.
                   10638:                    '<input type="radio" name="autoextract_camtasia" value="0" onclick="javascript:camtasiaToggle();" />'.
                   10639:                    $lt{'no'}.'</label></span><br />'.
                   10640:                    '<div id="camtasia_titles" style="display:block">'.
                   10641:                    &Apache::lonhtmlcommon::start_pick_box().
                   10642:                    &Apache::lonhtmlcommon::row_title($lt{'fold'}).
                   10643:                    '<input type="textbox" name="camtasia_foldername" value="'.$env{'form.comment'}.'" />'."\n".
                   10644:                    &Apache::lonhtmlcommon::row_closure().
                   10645:                    &Apache::lonhtmlcommon::row_title($lt{'movi'}).
                   10646:                    '<input type="textbox" name="camtasia_moviename" value="" />'."\n".
                   10647:                    &Apache::lonhtmlcommon::row_closure(1).
                   10648:                    &Apache::lonhtmlcommon::end_pick_box().
                   10649:                    '</div>';
                   10650:     }
1.1065    raeburn  10651:     $output .= 
                   10652:         '<input type="hidden" name="archive_overwrite_total" value="'.$num.'" />'.
1.1067    raeburn  10653:         '<input type="hidden" name="archive_itemcount" value="'.$itemcount.'" />'.
                   10654:         "\n";
1.1065    raeburn  10655:     if ($duplicates ne '') {
                   10656:         $output .= '<p><span class="LC_warning">'.
                   10657:                    &mt('Warning: decompression of the archive will overwrite the following items which already exist:').'</span><br />'.  
                   10658:                    &start_data_table().
                   10659:                    &start_data_table_header_row().
                   10660:                    '<th>'.&mt('Overwrite?').'</th>'.
                   10661:                    '<th>'.&mt('Name').'</th>'.
                   10662:                    '<th>'.&mt('Type').'</th>'.
                   10663:                    '<th>'.&mt('Size').'</th>'.
                   10664:                    '<th>'.&mt('Last modified').'</th>'.
                   10665:                    &end_data_table_header_row().
                   10666:                    $duplicates.
                   10667:                    &end_data_table().
                   10668:                    '</p>';
                   10669:     }
1.1067    raeburn  10670:     $output .= '<input type="hidden" name="archiveurl" value="'.$archiveurl.'" />'."\n";
1.1053    raeburn  10671:     if (ref($hiddenelements) eq 'HASH') {
                   10672:         foreach my $hidden (sort(keys(%{$hiddenelements}))) {
                   10673:             $output .= '<input type="hidden" name="'.$hidden.'" value="'.$hiddenelements->{$hidden}.'" />'."\n";
                   10674:         }
                   10675:     }
                   10676:     $output .= <<"END";
1.1067    raeburn  10677: <br />
1.1053    raeburn  10678: <input type="submit" name="decompress" value="$lt{'extr'}" />
                   10679: </form>
                   10680: $noextract
                   10681: END
                   10682:     return $output;
                   10683: }
                   10684: 
1.1065    raeburn  10685: sub decompression_utility {
                   10686:     my ($program) = @_;
                   10687:     my @utilities = ('tar','gunzip','bunzip2','unzip'); 
                   10688:     my $location;
                   10689:     if (grep(/^\Q$program\E$/,@utilities)) { 
                   10690:         foreach my $dir ('/bin/','/usr/bin/','/usr/local/bin/','/sbin/',
                   10691:                          '/usr/sbin/') {
                   10692:             if (-x $dir.$program) {
                   10693:                 $location = $dir.$program;
                   10694:                 last;
                   10695:             }
                   10696:         }
                   10697:     }
                   10698:     return $location;
                   10699: }
                   10700: 
                   10701: sub list_archive_contents {
                   10702:     my ($file,$pathsref) = @_;
                   10703:     my (@cmd,$output);
                   10704:     my $needsregexp;
                   10705:     if ($file =~ /\.zip$/) {
                   10706:         @cmd = (&decompression_utility('unzip'),"-l");
                   10707:         $needsregexp = 1;
                   10708:     } elsif (($file =~ m/\.tar\.gz$/) ||
                   10709:              ($file =~ /\.tgz$/)) {
                   10710:         @cmd = (&decompression_utility('tar'),"-ztf");
                   10711:     } elsif ($file =~ /\.tar\.bz2$/) {
                   10712:         @cmd = (&decompression_utility('tar'),"-jtf");
                   10713:     } elsif ($file =~ m|\.tar$|) {
                   10714:         @cmd = (&decompression_utility('tar'),"-tf");
                   10715:     }
                   10716:     if (@cmd) {
                   10717:         undef($!);
                   10718:         undef($@);
                   10719:         if (open(my $fh,"-|", @cmd, $file)) {
                   10720:             while (my $line = <$fh>) {
                   10721:                 $output .= $line;
                   10722:                 chomp($line);
                   10723:                 my $item;
                   10724:                 if ($needsregexp) {
                   10725:                     ($item) = ($line =~ /^\s*\d+\s+[\d\-]+\s+[\d:]+\s*(.+)$/); 
                   10726:                 } else {
                   10727:                     $item = $line;
                   10728:                 }
                   10729:                 if ($item ne '') {
                   10730:                     unless (grep(/^\Q$item\E$/,@{$pathsref})) {
                   10731:                         push(@{$pathsref},$item);
                   10732:                     } 
                   10733:                 }
                   10734:             }
                   10735:             close($fh);
                   10736:         }
                   10737:     }
                   10738:     return $output;
                   10739: }
                   10740: 
1.1053    raeburn  10741: sub decompress_uploaded_file {
                   10742:     my ($file,$dir) = @_;
                   10743:     &Apache::lonnet::appenv({'cgi.file' => $file});
                   10744:     &Apache::lonnet::appenv({'cgi.dir' => $dir});
                   10745:     my $result = &Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
                   10746:     my ($handle) = ($env{'user.environment'} =~m{/([^/]+)\.id$});
                   10747:     my $lonidsdir = $Apache::lonnet::perlvar{'lonIDsDir'};
                   10748:     &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle,1);
                   10749:     my $decompressed = $env{'cgi.decompressed'};
                   10750:     &Apache::lonnet::delenv('cgi.file');
                   10751:     &Apache::lonnet::delenv('cgi.dir');
                   10752:     &Apache::lonnet::delenv('cgi.decompressed');
                   10753:     return ($decompressed,$result);
                   10754: }
                   10755: 
1.1055    raeburn  10756: sub process_decompression {
                   10757:     my ($docudom,$docuname,$file,$destination,$dir_root,$hiddenelem) = @_;
                   10758:     my ($dir,$error,$warning,$output);
                   10759:     if ($file !~ /\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
                   10760:         $error = &mt('File name not a supported archive file type.').
                   10761:                  '<br />'.&mt('File name should end with one of: [_1].',
                   10762:                               '.zip, .tar, .bz2, .gz, .tar.gz, .tar.bz2, .tgz');
                   10763:     } else {
                   10764:         my $docuhome = &Apache::lonnet::homeserver($docuname,$docudom);
                   10765:         if ($docuhome eq 'no_host') {
                   10766:             $error = &mt('Could not determine home server for course.');
                   10767:         } else {
                   10768:             my @ids=&Apache::lonnet::current_machine_ids();
                   10769:             my $currdir = "$dir_root/$destination";
                   10770:             if (grep(/^\Q$docuhome\E$/,@ids)) {
                   10771:                 $dir = &LONCAPA::propath($docudom,$docuname).
                   10772:                        "$dir_root/$destination";
                   10773:             } else {
                   10774:                 $dir = $Apache::lonnet::perlvar{'lonDocRoot'}.
                   10775:                        "$dir_root/$docudom/$docuname/$destination";
                   10776:                 unless (&Apache::lonnet::repcopy_userfile("$dir/$file") eq 'ok') {
                   10777:                     $error = &mt('Archive file not found.');
                   10778:                 }
                   10779:             }
1.1065    raeburn  10780:             my (@to_overwrite,@to_skip);
                   10781:             if ($env{'form.archive_overwrite_total'} > 0) {
                   10782:                 my $total = $env{'form.archive_overwrite_total'};
                   10783:                 for (my $i=0; $i<$total; $i++) {
                   10784:                     if ($env{'form.archive_overwrite_'.$i} == 1) {
                   10785:                         push(@to_overwrite,$env{'form.archive_overwrite_name_'.$i});
                   10786:                     } elsif ($env{'form.archive_overwrite_'.$i} == 0) {
                   10787:                         push(@to_skip,$env{'form.archive_overwrite_name_'.$i});
                   10788:                     }
                   10789:                 }
                   10790:             }
                   10791:             my $numskip = scalar(@to_skip);
                   10792:             if (($numskip > 0) && 
                   10793:                 ($numskip == $env{'form.archive_itemcount'})) {
                   10794:                 $warning = &mt('All items in the archive file already exist, and no overwriting of existing files has been requested.');         
                   10795:             } elsif ($dir eq '') {
1.1055    raeburn  10796:                 $error = &mt('Directory containing archive file unavailable.');
                   10797:             } elsif (!$error) {
1.1065    raeburn  10798:                 my ($decompressed,$display);
                   10799:                 if ($numskip > 0) {
                   10800:                     my $tempdir = time.'_'.$$.int(rand(10000));
                   10801:                     mkdir("$dir/$tempdir",0755);
                   10802:                     system("mv $dir/$file $dir/$tempdir/$file");
                   10803:                     ($decompressed,$display) = 
                   10804:                         &decompress_uploaded_file($file,"$dir/$tempdir");
                   10805:                     foreach my $item (@to_skip) {
                   10806:                         if (($item ne '') && ($item !~ /\.\./)) {
                   10807:                             if (-f "$dir/$tempdir/$item") { 
                   10808:                                 unlink("$dir/$tempdir/$item");
                   10809:                             } elsif (-d "$dir/$tempdir/$item") {
                   10810:                                 system("rm -rf $dir/$tempdir/$item");
                   10811:                             }
                   10812:                         }
                   10813:                     }
                   10814:                     system("mv $dir/$tempdir/* $dir");
                   10815:                     rmdir("$dir/$tempdir");   
                   10816:                 } else {
                   10817:                     ($decompressed,$display) = 
                   10818:                         &decompress_uploaded_file($file,$dir);
                   10819:                 }
1.1055    raeburn  10820:                 if ($decompressed eq 'ok') {
1.1065    raeburn  10821:                     $output = '<p class="LC_info">'.
                   10822:                               &mt('Files extracted successfully from archive.').
                   10823:                               '</p>'."\n";
1.1055    raeburn  10824:                     my ($warning,$result,@contents);
                   10825:                     my ($newdirlistref,$newlisterror) =
                   10826:                         &Apache::lonnet::dirlist($currdir,$docudom,
                   10827:                                                  $docuname,1);
                   10828:                     my (%is_dir,%changes,@newitems);
                   10829:                     my $dirptr = 16384;
1.1065    raeburn  10830:                     if (ref($newdirlistref) eq 'ARRAY') {
1.1055    raeburn  10831:                         foreach my $dir_line (@{$newdirlistref}) {
                   10832:                             my ($item,undef,undef,$testdir)=split(/\&/,$dir_line,5);
1.1065    raeburn  10833:                             unless (($item =~ /^\.+$/) || ($item eq $file) || 
                   10834:                                     ((@to_skip > 0) && (grep(/^\Q$item\E$/,@to_skip)))) {
1.1055    raeburn  10835:                                 push(@newitems,$item);
                   10836:                                 if ($dirptr&$testdir) {
                   10837:                                     $is_dir{$item} = 1;
                   10838:                                 }
                   10839:                                 $changes{$item} = 1;
                   10840:                             }
                   10841:                         }
                   10842:                     }
                   10843:                     if (keys(%changes) > 0) {
                   10844:                         foreach my $item (sort(@newitems)) {
                   10845:                             if ($changes{$item}) {
                   10846:                                 push(@contents,$item);
                   10847:                             }
                   10848:                         }
                   10849:                     }
                   10850:                     if (@contents > 0) {
1.1067    raeburn  10851:                         my $wantform;
                   10852:                         unless ($env{'form.autoextract_camtasia'}) {
                   10853:                             $wantform = 1;
                   10854:                         }
1.1056    raeburn  10855:                         my (%children,%parent,%dirorder,%titles);
1.1055    raeburn  10856:                         my ($count,$datatable) = &get_extracted($docudom,$docuname,
                   10857:                                                                 $currdir,\%is_dir,
                   10858:                                                                 \%children,\%parent,
1.1056    raeburn  10859:                                                                 \@contents,\%dirorder,
                   10860:                                                                 \%titles,$wantform);
1.1055    raeburn  10861:                         if ($datatable ne '') {
                   10862:                             $output .= &archive_options_form('decompressed',$datatable,
                   10863:                                                              $count,$hiddenelem);
1.1065    raeburn  10864:                             my $startcount = 6;
1.1055    raeburn  10865:                             $output .= &archive_javascript($startcount,$count,
1.1056    raeburn  10866:                                                            \%titles,\%children);
1.1055    raeburn  10867:                         }
1.1067    raeburn  10868:                         if ($env{'form.autoextract_camtasia'}) {
                   10869:                             my %displayed;
                   10870:                             my $total = 1;
                   10871:                             $env{'form.archive_directory'} = [];
                   10872:                             foreach my $i (sort { $a <=> $b } keys(%dirorder)) {
                   10873:                                 my $path = join('/',map { $titles{$_}; } @{$dirorder{$i}});
                   10874:                                 $path =~ s{/$}{};
                   10875:                                 my $item;
                   10876:                                 if ($path ne '') {
                   10877:                                     $item = "$path/$titles{$i}";
                   10878:                                 } else {
                   10879:                                     $item = $titles{$i};
                   10880:                                 }
                   10881:                                 $env{'form.archive_content_'.$i} = "$dir_root/$destination/$item";
                   10882:                                 if ($item eq $contents[0]) {
                   10883:                                     push(@{$env{'form.archive_directory'}},$i);
                   10884:                                     $env{'form.archive_'.$i} = 'display';
                   10885:                                     $env{'form.archive_title_'.$i} = $env{'form.camtasia_foldername'};
                   10886:                                     $displayed{'folder'} = $i;
                   10887:                                 } elsif ($item eq "$contents[0]/index.html") {
                   10888:                                     $env{'form.archive_'.$i} = 'display';
                   10889:                                     $env{'form.archive_title_'.$i} = $env{'form.camtasia_moviename'};
                   10890:                                     $displayed{'web'} = $i;
                   10891:                                 } else {
                   10892:                                     if ($item eq "$contents[0]/media") {
                   10893:                                         push(@{$env{'form.archive_directory'}},$i);
                   10894:                                     }
                   10895:                                     $env{'form.archive_'.$i} = 'dependency';
                   10896:                                 }
                   10897:                                 $total ++;
                   10898:                             }
                   10899:                             for (my $i=1; $i<$total; $i++) {
                   10900:                                 next if ($i == $displayed{'web'});
                   10901:                                 next if ($i == $displayed{'folder'});
                   10902:                                 $env{'form.archive_dependent_on_'.$i} = $displayed{'web'};
                   10903:                             }
                   10904:                             $env{'form.phase'} = 'decompress_cleanup';
                   10905:                             $env{'form.archivedelete'} = 1;
                   10906:                             $env{'form.archive_count'} = $total-1;
                   10907:                             $output .=
                   10908:                                 &process_extracted_files('coursedocs',$docudom,
                   10909:                                                          $docuname,$destination,
                   10910:                                                          $dir_root,$hiddenelem);
                   10911:                         }
1.1055    raeburn  10912:                     } else {
                   10913:                         $warning = &mt('No new items extracted from archive file.');
                   10914:                     }
                   10915:                 } else {
                   10916:                     $output = $display;
                   10917:                     $error = &mt('An error occurred during extraction from the archive file.');
                   10918:                 }
                   10919:             }
                   10920:         }
                   10921:     }
                   10922:     if ($error) {
                   10923:         $output .= '<p class="LC_error">'.&mt('Not extracted.').'<br />'.
                   10924:                    $error.'</p>'."\n";
                   10925:     }
                   10926:     if ($warning) {
                   10927:         $output .= '<p class="LC_warning">'.$warning.'</p>'."\n";
                   10928:     }
                   10929:     return $output;
                   10930: }
                   10931: 
                   10932: sub get_extracted {
1.1056    raeburn  10933:     my ($docudom,$docuname,$currdir,$is_dir,$children,$parent,$contents,$dirorder,
                   10934:         $titles,$wantform) = @_;
1.1055    raeburn  10935:     my $count = 0;
                   10936:     my $depth = 0;
                   10937:     my $datatable;
1.1056    raeburn  10938:     my @hierarchy;
1.1055    raeburn  10939:     return unless ((ref($is_dir) eq 'HASH') && (ref($children) eq 'HASH') &&
1.1056    raeburn  10940:                    (ref($parent) eq 'HASH') && (ref($contents) eq 'ARRAY') &&
                   10941:                    (ref($dirorder) eq 'HASH') && (ref($titles) eq 'HASH'));
1.1055    raeburn  10942:     foreach my $item (@{$contents}) {
                   10943:         $count ++;
1.1056    raeburn  10944:         @{$dirorder->{$count}} = @hierarchy;
                   10945:         $titles->{$count} = $item;
1.1055    raeburn  10946:         &archive_hierarchy($depth,$count,$parent,$children);
                   10947:         if ($wantform) {
                   10948:             $datatable .= &archive_row($is_dir->{$item},$item,
                   10949:                                        $currdir,$depth,$count);
                   10950:         }
                   10951:         if ($is_dir->{$item}) {
                   10952:             $depth ++;
1.1056    raeburn  10953:             push(@hierarchy,$count);
                   10954:             $parent->{$depth} = $count;
1.1055    raeburn  10955:             $datatable .=
                   10956:                 &recurse_extracted_archive("$currdir/$item",$docudom,$docuname,
1.1056    raeburn  10957:                                            \$depth,\$count,\@hierarchy,$dirorder,
                   10958:                                            $children,$parent,$titles,$wantform);
1.1055    raeburn  10959:             $depth --;
1.1056    raeburn  10960:             pop(@hierarchy);
1.1055    raeburn  10961:         }
                   10962:     }
                   10963:     return ($count,$datatable);
                   10964: }
                   10965: 
                   10966: sub recurse_extracted_archive {
1.1056    raeburn  10967:     my ($currdir,$docudom,$docuname,$depth,$count,$hierarchy,$dirorder,
                   10968:         $children,$parent,$titles,$wantform) = @_;
1.1055    raeburn  10969:     my $result='';
1.1056    raeburn  10970:     unless ((ref($depth)) && (ref($count)) && (ref($hierarchy) eq 'ARRAY') &&
                   10971:             (ref($children) eq 'HASH') && (ref($parent) eq 'HASH') &&
                   10972:             (ref($dirorder) eq 'HASH')) {
1.1055    raeburn  10973:         return $result;
                   10974:     }
                   10975:     my $dirptr = 16384;
                   10976:     my ($newdirlistref,$newlisterror) =
                   10977:         &Apache::lonnet::dirlist($currdir,$docudom,$docuname,1);
                   10978:     if (ref($newdirlistref) eq 'ARRAY') {
                   10979:         foreach my $dir_line (@{$newdirlistref}) {
                   10980:             my ($item,undef,undef,$testdir)=split(/\&/,$dir_line,5);
                   10981:             unless ($item =~ /^\.+$/) {
                   10982:                 $$count ++;
1.1056    raeburn  10983:                 @{$dirorder->{$$count}} = @{$hierarchy};
                   10984:                 $titles->{$$count} = $item;
1.1055    raeburn  10985:                 &archive_hierarchy($$depth,$$count,$parent,$children);
1.1056    raeburn  10986: 
1.1055    raeburn  10987:                 my $is_dir;
                   10988:                 if ($dirptr&$testdir) {
                   10989:                     $is_dir = 1;
                   10990:                 }
                   10991:                 if ($wantform) {
                   10992:                     $result .= &archive_row($is_dir,$item,$currdir,$$depth,$$count);
                   10993:                 }
                   10994:                 if ($is_dir) {
                   10995:                     $$depth ++;
1.1056    raeburn  10996:                     push(@{$hierarchy},$$count);
                   10997:                     $parent->{$$depth} = $$count;
1.1055    raeburn  10998:                     $result .=
                   10999:                         &recurse_extracted_archive("$currdir/$item",$docudom,
                   11000:                                                    $docuname,$depth,$count,
1.1056    raeburn  11001:                                                    $hierarchy,$dirorder,$children,
                   11002:                                                    $parent,$titles,$wantform);
1.1055    raeburn  11003:                     $$depth --;
1.1056    raeburn  11004:                     pop(@{$hierarchy});
1.1055    raeburn  11005:                 }
                   11006:             }
                   11007:         }
                   11008:     }
                   11009:     return $result;
                   11010: }
                   11011: 
                   11012: sub archive_hierarchy {
                   11013:     my ($depth,$count,$parent,$children) =@_;
                   11014:     if ((ref($parent) eq 'HASH') && (ref($children) eq 'HASH')) {
                   11015:         if (exists($parent->{$depth})) {
                   11016:              $children->{$parent->{$depth}} .= $count.':';
                   11017:         }
                   11018:     }
                   11019:     return;
                   11020: }
                   11021: 
                   11022: sub archive_row {
                   11023:     my ($is_dir,$item,$currdir,$depth,$count) = @_;
                   11024:     my ($name) = ($item =~ m{([^/]+)$});
                   11025:     my %choices = &Apache::lonlocal::texthash (
1.1059    raeburn  11026:                                        'display'    => 'Add as file',
1.1055    raeburn  11027:                                        'dependency' => 'Include as dependency',
                   11028:                                        'discard'    => 'Discard',
                   11029:                                       );
                   11030:     if ($is_dir) {
1.1059    raeburn  11031:         $choices{'display'} = &mt('Add as folder'); 
1.1055    raeburn  11032:     }
1.1056    raeburn  11033:     my $output = &start_data_table_row().'<td align="right">'.$count.'</td>'."\n";
                   11034:     my $offset = 0;
1.1055    raeburn  11035:     foreach my $action ('display','dependency','discard') {
1.1056    raeburn  11036:         $offset ++;
1.1065    raeburn  11037:         if ($action ne 'display') {
                   11038:             $offset ++;
                   11039:         }  
1.1055    raeburn  11040:         $output .= '<td><span class="LC_nobreak">'.
                   11041:                    '<label><input type="radio" name="archive_'.$count.
                   11042:                    '" id="archive_'.$action.'_'.$count.'" value="'.$action.'"';
                   11043:         my $text = $choices{$action};
                   11044:         if ($is_dir) {
                   11045:             $output .= ' onclick="javascript:propagateCheck(this.form,'."'$count'".');"';
                   11046:             if ($action eq 'display') {
1.1059    raeburn  11047:                 $text = &mt('Add as folder');
1.1055    raeburn  11048:             }
1.1056    raeburn  11049:         } else {
                   11050:             $output .= ' onclick="javascript:dependencyCheck(this.form,'."$count,$offset".');"';
                   11051: 
                   11052:         }
                   11053:         $output .= ' />&nbsp;'.$choices{$action}.'</label></span>';
                   11054:         if ($action eq 'dependency') {
                   11055:             $output .= '<div id="arc_depon_'.$count.'" style="display:none;">'."\n".
                   11056:                        &mt('Used by:').'&nbsp;<select name="archive_dependent_on_'.$count.'" '.
                   11057:                        'onchange="propagateSelect(this.form,'."$count,$offset".')">'."\n".
                   11058:                        '<option value=""></option>'."\n".
                   11059:                        '</select>'."\n".
                   11060:                        '</div>';
1.1059    raeburn  11061:         } elsif ($action eq 'display') {
                   11062:             $output .= '<div id="arc_title_'.$count.'" style="display:none;">'."\n".
                   11063:                        &mt('Title:').'&nbsp;<input type="text" name="archive_title_'.$count.'" id="archive_title_'.$count.'" />'."\n".
                   11064:                        '</div>';
1.1055    raeburn  11065:         }
1.1056    raeburn  11066:         $output .= '</td>';
1.1055    raeburn  11067:     }
                   11068:     $output .= '<td><input type="hidden" name="archive_content_'.$count.'" value="'.
                   11069:                &HTML::Entities::encode("$currdir/$item",'"<>&').'" />'.('&nbsp;' x 2);
                   11070:     for (my $i=0; $i<$depth; $i++) {
                   11071:         $output .= ('<img src="/adm/lonIcons/whitespace1.gif" class="LC_docs_spacer" alt="" />' x2)."\n";
                   11072:     }
                   11073:     if ($is_dir) {
                   11074:         $output .= '<img src="/adm/lonIcons/navmap.folder.open.gif" alt="" />&nbsp;'."\n".
                   11075:                    '<input type="hidden" name="archive_directory" value="'.$count.'" />'."\n";
                   11076:     } else {
                   11077:         $output .= '<input type="hidden" name="archive_file" value="'.$count.'" />'."\n";
                   11078:     }
                   11079:     $output .= '&nbsp;'.$name.'</td>'."\n".
                   11080:                &end_data_table_row();
                   11081:     return $output;
                   11082: }
                   11083: 
                   11084: sub archive_options_form {
1.1065    raeburn  11085:     my ($form,$display,$count,$hiddenelem) = @_;
                   11086:     my %lt = &Apache::lonlocal::texthash(
                   11087:                perm => 'Permanently remove archive file?',
                   11088:                hows => 'How should each extracted item be incorporated in the course?',
                   11089:                cont => 'Content actions for all',
                   11090:                addf => 'Add as folder/file',
                   11091:                incd => 'Include as dependency for a displayed file',
                   11092:                disc => 'Discard',
                   11093:                no   => 'No',
                   11094:                yes  => 'Yes',
                   11095:                save => 'Save',
                   11096:     );
                   11097:     my $output = <<"END";
                   11098: <form name="$form" method="post" action="">
                   11099: <p><span class="LC_nobreak">$lt{'perm'}&nbsp;
                   11100: <label>
                   11101:   <input type="radio" name="archivedelete" value="0" checked="checked" />$lt{'no'}
                   11102: </label>
                   11103: &nbsp;
                   11104: <label>
                   11105:   <input type="radio" name="archivedelete" value="1" />$lt{'yes'}</label>
                   11106: </span>
                   11107: </p>
                   11108: <input type="hidden" name="phase" value="decompress_cleanup" />
                   11109: <br />$lt{'hows'}
                   11110: <div class="LC_columnSection">
                   11111:   <fieldset>
                   11112:     <legend>$lt{'cont'}</legend>
                   11113:     <input type="button" value="$lt{'addf'}" onclick="javascript:checkAll(document.$form,'display');" /> 
                   11114:     &nbsp;&nbsp;<input type="button" value="$lt{'incd'}" onclick="javascript:checkAll(document.$form,'dependency');" />
                   11115:     &nbsp;&nbsp;<input type="button" value="$lt{'disc'}" onclick="javascript:checkAll(document.$form,'discard');" />
                   11116:   </fieldset>
                   11117: </div>
                   11118: END
                   11119:     return $output.
1.1055    raeburn  11120:            &start_data_table()."\n".
1.1065    raeburn  11121:            $display."\n".
1.1055    raeburn  11122:            &end_data_table()."\n".
                   11123:            '<input type="hidden" name="archive_count" value="'.$count.'" />'.
                   11124:            $hiddenelem.
1.1065    raeburn  11125:            '<br /><input type="submit" name="archive_submit" value="'.$lt{'save'}.'" />'.
1.1055    raeburn  11126:            '</form>';
                   11127: }
                   11128: 
                   11129: sub archive_javascript {
1.1056    raeburn  11130:     my ($startcount,$numitems,$titles,$children) = @_;
                   11131:     return unless ((ref($titles) eq 'HASH') && (ref($children) eq 'HASH'));
1.1059    raeburn  11132:     my $maintitle = $env{'form.comment'};
1.1055    raeburn  11133:     my $scripttag = <<START;
                   11134: <script type="text/javascript">
                   11135: // <![CDATA[
                   11136: 
                   11137: function checkAll(form,prefix) {
                   11138:     var idstr =  new RegExp("^archive_"+prefix+"_\\\\d+\$");
                   11139:     for (var i=0; i < form.elements.length; i++) {
                   11140:         var id = form.elements[i].id;
                   11141:         if ((id != '') && (id != undefined)) {
                   11142:             if (idstr.test(id)) {
                   11143:                 if (form.elements[i].type == 'radio') {
                   11144:                     form.elements[i].checked = true;
1.1056    raeburn  11145:                     var nostart = i-$startcount;
1.1059    raeburn  11146:                     var offset = nostart%7;
                   11147:                     var count = (nostart-offset)/7;    
1.1056    raeburn  11148:                     dependencyCheck(form,count,offset);
1.1055    raeburn  11149:                 }
                   11150:             }
                   11151:         }
                   11152:     }
                   11153: }
                   11154: 
                   11155: function propagateCheck(form,count) {
                   11156:     if (count > 0) {
1.1059    raeburn  11157:         var startelement = $startcount + ((count-1) * 7);
                   11158:         for (var j=1; j<6; j++) {
                   11159:             if ((j != 2) && (j != 4)) {
1.1056    raeburn  11160:                 var item = startelement + j; 
                   11161:                 if (form.elements[item].type == 'radio') {
                   11162:                     if (form.elements[item].checked) {
                   11163:                         containerCheck(form,count,j);
                   11164:                         break;
                   11165:                     }
1.1055    raeburn  11166:                 }
                   11167:             }
                   11168:         }
                   11169:     }
                   11170: }
                   11171: 
                   11172: numitems = $numitems
1.1056    raeburn  11173: var titles = new Array(numitems);
                   11174: var parents = new Array(numitems);
1.1055    raeburn  11175: for (var i=0; i<numitems; i++) {
1.1056    raeburn  11176:     parents[i] = new Array;
1.1055    raeburn  11177: }
1.1059    raeburn  11178: var maintitle = '$maintitle';
1.1055    raeburn  11179: 
                   11180: START
                   11181: 
1.1056    raeburn  11182:     foreach my $container (sort { $a <=> $b } (keys(%{$children}))) {
                   11183:         my @contents = split(/:/,$children->{$container});
1.1055    raeburn  11184:         for (my $i=0; $i<@contents; $i ++) {
                   11185:             $scripttag .= 'parents['.$container.']['.$i.'] = '.$contents[$i]."\n";
                   11186:         }
                   11187:     }
                   11188: 
1.1056    raeburn  11189:     foreach my $key (sort { $a <=> $b } (keys(%{$titles}))) {
                   11190:         $scripttag .= "titles[$key] = '".$titles->{$key}."';\n";
                   11191:     }
                   11192: 
1.1055    raeburn  11193:     $scripttag .= <<END;
                   11194: 
                   11195: function containerCheck(form,count,offset) {
                   11196:     if (count > 0) {
1.1056    raeburn  11197:         dependencyCheck(form,count,offset);
1.1059    raeburn  11198:         var item = (offset+$startcount)+7*(count-1);
1.1055    raeburn  11199:         form.elements[item].checked = true;
                   11200:         if(Object.prototype.toString.call(parents[count]) === '[object Array]') {
                   11201:             if (parents[count].length > 0) {
                   11202:                 for (var j=0; j<parents[count].length; j++) {
1.1056    raeburn  11203:                     containerCheck(form,parents[count][j],offset);
                   11204:                 }
                   11205:             }
                   11206:         }
                   11207:     }
                   11208: }
                   11209: 
                   11210: function dependencyCheck(form,count,offset) {
                   11211:     if (count > 0) {
1.1059    raeburn  11212:         var chosen = (offset+$startcount)+7*(count-1);
                   11213:         var depitem = $startcount + ((count-1) * 7) + 4;
1.1056    raeburn  11214:         var currtype = form.elements[depitem].type;
                   11215:         if (form.elements[chosen].value == 'dependency') {
                   11216:             document.getElementById('arc_depon_'+count).style.display='block'; 
                   11217:             form.elements[depitem].options.length = 0;
                   11218:             form.elements[depitem].options[0] = new Option('Select','',true,true);
1.1075.2.11  raeburn  11219:             for (var i=1; i<=numitems; i++) {
                   11220:                 if (i == count) {
                   11221:                     continue;
                   11222:                 }
1.1059    raeburn  11223:                 var startelement = $startcount + (i-1) * 7;
                   11224:                 for (var j=1; j<6; j++) {
                   11225:                     if ((j != 2) && (j!= 4)) {
1.1056    raeburn  11226:                         var item = startelement + j;
                   11227:                         if (form.elements[item].type == 'radio') {
                   11228:                             if (form.elements[item].checked) {
                   11229:                                 if (form.elements[item].value == 'display') {
                   11230:                                     var n = form.elements[depitem].options.length;
                   11231:                                     form.elements[depitem].options[n] = new Option(titles[i],i,false,false);
                   11232:                                 }
                   11233:                             }
                   11234:                         }
                   11235:                     }
                   11236:                 }
                   11237:             }
                   11238:         } else {
                   11239:             document.getElementById('arc_depon_'+count).style.display='none';
                   11240:             form.elements[depitem].options.length = 0;
                   11241:             form.elements[depitem].options[0] = new Option('Select','',true,true);
                   11242:         }
1.1059    raeburn  11243:         titleCheck(form,count,offset);
1.1056    raeburn  11244:     }
                   11245: }
                   11246: 
                   11247: function propagateSelect(form,count,offset) {
                   11248:     if (count > 0) {
1.1065    raeburn  11249:         var item = (1+offset+$startcount)+7*(count-1);
1.1056    raeburn  11250:         var picked = form.elements[item].options[form.elements[item].selectedIndex].value; 
                   11251:         if (Object.prototype.toString.call(parents[count]) === '[object Array]') {
                   11252:             if (parents[count].length > 0) {
                   11253:                 for (var j=0; j<parents[count].length; j++) {
                   11254:                     containerSelect(form,parents[count][j],offset,picked);
1.1055    raeburn  11255:                 }
                   11256:             }
                   11257:         }
                   11258:     }
                   11259: }
1.1056    raeburn  11260: 
                   11261: function containerSelect(form,count,offset,picked) {
                   11262:     if (count > 0) {
1.1065    raeburn  11263:         var item = (offset+$startcount)+7*(count-1);
1.1056    raeburn  11264:         if (form.elements[item].type == 'radio') {
                   11265:             if (form.elements[item].value == 'dependency') {
                   11266:                 if (form.elements[item+1].type == 'select-one') {
                   11267:                     for (var i=0; i<form.elements[item+1].options.length; i++) {
                   11268:                         if (form.elements[item+1].options[i].value == picked) {
                   11269:                             form.elements[item+1].selectedIndex = i;
                   11270:                             break;
                   11271:                         }
                   11272:                     }
                   11273:                 }
                   11274:                 if (Object.prototype.toString.call(parents[count]) === '[object Array]') {
                   11275:                     if (parents[count].length > 0) {
                   11276:                         for (var j=0; j<parents[count].length; j++) {
                   11277:                             containerSelect(form,parents[count][j],offset,picked);
                   11278:                         }
                   11279:                     }
                   11280:                 }
                   11281:             }
                   11282:         }
                   11283:     }
                   11284: }
                   11285: 
1.1059    raeburn  11286: function titleCheck(form,count,offset) {
                   11287:     if (count > 0) {
                   11288:         var chosen = (offset+$startcount)+7*(count-1);
                   11289:         var depitem = $startcount + ((count-1) * 7) + 2;
                   11290:         var currtype = form.elements[depitem].type;
                   11291:         if (form.elements[chosen].value == 'display') {
                   11292:             document.getElementById('arc_title_'+count).style.display='block';
                   11293:             if ((count==1) && ((parents[count].length > 0) || (numitems == 1))) {
                   11294:                 document.getElementById('archive_title_'+count).value=maintitle;
                   11295:             }
                   11296:         } else {
                   11297:             document.getElementById('arc_title_'+count).style.display='none';
                   11298:             if (currtype == 'text') { 
                   11299:                 document.getElementById('archive_title_'+count).value='';
                   11300:             }
                   11301:         }
                   11302:     }
                   11303:     return;
                   11304: }
                   11305: 
1.1055    raeburn  11306: // ]]>
                   11307: </script>
                   11308: END
                   11309:     return $scripttag;
                   11310: }
                   11311: 
                   11312: sub process_extracted_files {
1.1067    raeburn  11313:     my ($context,$docudom,$docuname,$destination,$dir_root,$hiddenelem) = @_;
1.1055    raeburn  11314:     my $numitems = $env{'form.archive_count'};
                   11315:     return unless ($numitems);
                   11316:     my @ids=&Apache::lonnet::current_machine_ids();
                   11317:     my ($prefix,$pathtocheck,$dir,$ishome,$error,$warning,%toplevelitems,%is_dir,
1.1067    raeburn  11318:         %folders,%containers,%mapinner,%prompttofetch);
1.1055    raeburn  11319:     my $docuhome = &Apache::lonnet::homeserver($docuname,$docudom);
                   11320:     if (grep(/^\Q$docuhome\E$/,@ids)) {
                   11321:         $prefix = &LONCAPA::propath($docudom,$docuname);
                   11322:         $pathtocheck = "$dir_root/$destination";
                   11323:         $dir = $dir_root;
                   11324:         $ishome = 1;
                   11325:     } else {
                   11326:         $prefix = $Apache::lonnet::perlvar{'lonDocRoot'};
                   11327:         $pathtocheck = "$dir_root/$docudom/$docuname/$destination";
                   11328:         $dir = "$dir_root/$docudom/$docuname";    
                   11329:     }
                   11330:     my $currdir = "$dir_root/$destination";
                   11331:     (my $docstype,$mapinner{'0'}) = ($destination =~ m{^(docs|supplemental)/(\w+)/});
                   11332:     if ($env{'form.folderpath'}) {
                   11333:         my @items = split('&',$env{'form.folderpath'});
                   11334:         $folders{'0'} = $items[-2];
1.1075.2.17  raeburn  11335:         if ($env{'form.folderpath'} =~ /\:1$/) {
                   11336:             $containers{'0'}='page';
                   11337:         } else {
                   11338:             $containers{'0'}='sequence';
                   11339:         }
1.1055    raeburn  11340:     }
                   11341:     my @archdirs = &get_env_multiple('form.archive_directory');
                   11342:     if ($numitems) {
                   11343:         for (my $i=1; $i<=$numitems; $i++) {
                   11344:             my $path = $env{'form.archive_content_'.$i};
                   11345:             if ($path =~ m{^\Q$pathtocheck\E/([^/]+)$}) {
                   11346:                 my $item = $1;
                   11347:                 $toplevelitems{$item} = $i;
                   11348:                 if (grep(/^\Q$i\E$/,@archdirs)) {
                   11349:                     $is_dir{$item} = 1;
                   11350:                 }
                   11351:             }
                   11352:         }
                   11353:     }
1.1067    raeburn  11354:     my ($output,%children,%parent,%titles,%dirorder,$result);
1.1055    raeburn  11355:     if (keys(%toplevelitems) > 0) {
                   11356:         my @contents = sort(keys(%toplevelitems));
1.1056    raeburn  11357:         (my $count,undef) = &get_extracted($docudom,$docuname,$currdir,\%is_dir,\%children,
                   11358:                                            \%parent,\@contents,\%dirorder,\%titles);
1.1055    raeburn  11359:     }
1.1066    raeburn  11360:     my (%referrer,%orphaned,%todelete,%todeletedir,%newdest,%newseqid);
1.1055    raeburn  11361:     if ($numitems) {
                   11362:         for (my $i=1; $i<=$numitems; $i++) {
1.1075.2.11  raeburn  11363:             next if ($env{'form.archive_'.$i} eq 'dependency');
1.1055    raeburn  11364:             my $path = $env{'form.archive_content_'.$i};
                   11365:             if ($path =~ /^\Q$pathtocheck\E/) {
                   11366:                 if ($env{'form.archive_'.$i} eq 'discard') {
                   11367:                     if ($prefix ne '' && $path ne '') {
                   11368:                         if (-e $prefix.$path) {
1.1066    raeburn  11369:                             if ((@archdirs > 0) && 
                   11370:                                 (grep(/^\Q$i\E$/,@archdirs))) {
                   11371:                                 $todeletedir{$prefix.$path} = 1;
                   11372:                             } else {
                   11373:                                 $todelete{$prefix.$path} = 1;
                   11374:                             }
1.1055    raeburn  11375:                         }
                   11376:                     }
                   11377:                 } elsif ($env{'form.archive_'.$i} eq 'display') {
1.1059    raeburn  11378:                     my ($docstitle,$title,$url,$outer);
1.1055    raeburn  11379:                     ($title) = ($path =~ m{/([^/]+)$});
1.1059    raeburn  11380:                     $docstitle = $env{'form.archive_title_'.$i};
                   11381:                     if ($docstitle eq '') {
                   11382:                         $docstitle = $title;
                   11383:                     }
1.1055    raeburn  11384:                     $outer = 0;
1.1056    raeburn  11385:                     if (ref($dirorder{$i}) eq 'ARRAY') {
                   11386:                         if (@{$dirorder{$i}} > 0) {
                   11387:                             foreach my $item (reverse(@{$dirorder{$i}})) {
1.1055    raeburn  11388:                                 if ($env{'form.archive_'.$item} eq 'display') {
                   11389:                                     $outer = $item;
                   11390:                                     last;
                   11391:                                 }
                   11392:                             }
                   11393:                         }
                   11394:                     }
                   11395:                     my ($errtext,$fatal) = 
                   11396:                         &LONCAPA::map::mapread('/uploaded/'.$docudom.'/'.$docuname.
                   11397:                                                '/'.$folders{$outer}.'.'.
                   11398:                                                $containers{$outer});
                   11399:                     next if ($fatal);
                   11400:                     if ((@archdirs > 0) && (grep(/^\Q$i\E$/,@archdirs))) {
                   11401:                         if ($context eq 'coursedocs') {
1.1056    raeburn  11402:                             $mapinner{$i} = time;
1.1055    raeburn  11403:                             $folders{$i} = 'default_'.$mapinner{$i};
                   11404:                             $containers{$i} = 'sequence';
                   11405:                             my $url = '/uploaded/'.$docudom.'/'.$docuname.'/'.
                   11406:                                       $folders{$i}.'.'.$containers{$i};
                   11407:                             my $newidx = &LONCAPA::map::getresidx();
                   11408:                             $LONCAPA::map::resources[$newidx]=
1.1059    raeburn  11409:                                 $docstitle.':'.$url.':false:normal:res';
1.1055    raeburn  11410:                             push(@LONCAPA::map::order,$newidx);
                   11411:                             my ($outtext,$errtext) =
                   11412:                                 &LONCAPA::map::storemap('/uploaded/'.$docudom.'/'.
                   11413:                                                         $docuname.'/'.$folders{$outer}.
1.1075.2.11  raeburn  11414:                                                         '.'.$containers{$outer},1,1);
1.1056    raeburn  11415:                             $newseqid{$i} = $newidx;
1.1067    raeburn  11416:                             unless ($errtext) {
                   11417:                                 $result .=  '<li>'.&mt('Folder: [_1] added to course',$docstitle).'</li>'."\n";
                   11418:                             }
1.1055    raeburn  11419:                         }
                   11420:                     } else {
                   11421:                         if ($context eq 'coursedocs') {
                   11422:                             my $newidx=&LONCAPA::map::getresidx();
                   11423:                             my $url = '/uploaded/'.$docudom.'/'.$docuname.'/'.
                   11424:                                       $docstype.'/'.$mapinner{$outer}.'/'.$newidx.'/'.
                   11425:                                       $title;
                   11426:                             if (!-e "$prefix$dir/$docstype/$mapinner{$outer}") {
                   11427:                                 mkdir("$prefix$dir/$docstype/$mapinner{$outer}",0755);
                   11428:                             }
                   11429:                             if (!-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx") {
                   11430:                                 mkdir("$prefix$dir/$docstype/$mapinner{$outer}/$newidx");
                   11431:                             }
                   11432:                             if (-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx") {
                   11433:                                 system("mv $prefix$path $prefix$dir/$docstype/$mapinner{$outer}/$newidx/$title");
1.1056    raeburn  11434:                                 $newdest{$i} = "$prefix$dir/$docstype/$mapinner{$outer}/$newidx";
1.1067    raeburn  11435:                                 unless ($ishome) {
                   11436:                                     my $fetch = "$newdest{$i}/$title";
                   11437:                                     $fetch =~ s/^\Q$prefix$dir\E//;
                   11438:                                     $prompttofetch{$fetch} = 1;
                   11439:                                 }
1.1055    raeburn  11440:                             }
                   11441:                             $LONCAPA::map::resources[$newidx]=
1.1059    raeburn  11442:                                 $docstitle.':'.$url.':false:normal:res';
1.1055    raeburn  11443:                             push(@LONCAPA::map::order, $newidx);
                   11444:                             my ($outtext,$errtext)=
                   11445:                                 &LONCAPA::map::storemap('/uploaded/'.$docudom.'/'.
                   11446:                                                         $docuname.'/'.$folders{$outer}.
1.1075.2.11  raeburn  11447:                                                         '.'.$containers{$outer},1,1);
1.1067    raeburn  11448:                             unless ($errtext) {
                   11449:                                 if (-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx/$title") {
                   11450:                                     $result .= '<li>'.&mt('File: [_1] added to course',$docstitle).'</li>'."\n";
                   11451:                                 }
                   11452:                             }
1.1055    raeburn  11453:                         }
                   11454:                     }
1.1075.2.11  raeburn  11455:                 }
                   11456:             } else {
                   11457:                 $warning .= &mt('Item extracted from archive: [_1] has unexpected path.',$path).'<br />';
                   11458:             }
                   11459:         }
                   11460:         for (my $i=1; $i<=$numitems; $i++) {
                   11461:             next unless ($env{'form.archive_'.$i} eq 'dependency');
                   11462:             my $path = $env{'form.archive_content_'.$i};
                   11463:             if ($path =~ /^\Q$pathtocheck\E/) {
                   11464:                 my ($title) = ($path =~ m{/([^/]+)$});
                   11465:                 $referrer{$i} = $env{'form.archive_dependent_on_'.$i};
                   11466:                 if ($env{'form.archive_'.$referrer{$i}} eq 'display') {
                   11467:                     if (ref($dirorder{$i}) eq 'ARRAY') {
                   11468:                         my ($itemidx,$fullpath,$relpath);
                   11469:                         if (ref($dirorder{$referrer{$i}}) eq 'ARRAY') {
                   11470:                             my $container = $dirorder{$referrer{$i}}->[-1];
1.1056    raeburn  11471:                             for (my $j=0; $j<@{$dirorder{$i}}; $j++) {
1.1075.2.11  raeburn  11472:                                 if ($dirorder{$i}->[$j] eq $container) {
                   11473:                                     $itemidx = $j;
1.1056    raeburn  11474:                                 }
                   11475:                             }
1.1075.2.11  raeburn  11476:                         }
                   11477:                         if ($itemidx eq '') {
                   11478:                             $itemidx =  0;
                   11479:                         }
                   11480:                         if (grep(/^\Q$referrer{$i}\E$/,@archdirs)) {
                   11481:                             if ($mapinner{$referrer{$i}}) {
                   11482:                                 $fullpath = "$prefix$dir/$docstype/$mapinner{$referrer{$i}}";
                   11483:                                 for (my $j=$itemidx; $j<@{$dirorder{$i}}; $j++) {
                   11484:                                     if (grep(/^\Q$dirorder{$i}->[$j]\E$/,@archdirs)) {
                   11485:                                         unless (defined($newseqid{$dirorder{$i}->[$j]})) {
                   11486:                                             $fullpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   11487:                                             $relpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   11488:                                             if (!-e $fullpath) {
                   11489:                                                 mkdir($fullpath,0755);
1.1056    raeburn  11490:                                             }
                   11491:                                         }
1.1075.2.11  raeburn  11492:                                     } else {
                   11493:                                         last;
1.1056    raeburn  11494:                                     }
1.1075.2.11  raeburn  11495:                                 }
                   11496:                             }
                   11497:                         } elsif ($newdest{$referrer{$i}}) {
                   11498:                             $fullpath = $newdest{$referrer{$i}};
                   11499:                             for (my $j=$itemidx; $j<@{$dirorder{$i}}; $j++) {
                   11500:                                 if ($env{'form.archive_'.$dirorder{$i}->[$j]} eq 'discard') {
                   11501:                                     $orphaned{$i} = $env{'form.archive_'.$dirorder{$i}->[$j]};
                   11502:                                     last;
                   11503:                                 } elsif (grep(/^\Q$dirorder{$i}->[$j]\E$/,@archdirs)) {
                   11504:                                     unless (defined($newseqid{$dirorder{$i}->[$j]})) {
                   11505:                                         $fullpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   11506:                                         $relpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   11507:                                         if (!-e $fullpath) {
                   11508:                                             mkdir($fullpath,0755);
1.1056    raeburn  11509:                                         }
                   11510:                                     }
1.1075.2.11  raeburn  11511:                                 } else {
                   11512:                                     last;
1.1056    raeburn  11513:                                 }
1.1075.2.11  raeburn  11514:                             }
                   11515:                         }
                   11516:                         if ($fullpath ne '') {
                   11517:                             if (-e "$prefix$path") {
                   11518:                                 system("mv $prefix$path $fullpath/$title");
                   11519:                             }
                   11520:                             if (-e "$fullpath/$title") {
                   11521:                                 my $showpath;
                   11522:                                 if ($relpath ne '') {
                   11523:                                     $showpath = "$relpath/$title";
                   11524:                                 } else {
                   11525:                                     $showpath = "/$title";
1.1056    raeburn  11526:                                 }
1.1075.2.11  raeburn  11527:                                 $result .= '<li>'.&mt('[_1] included as a dependency',$showpath).'</li>'."\n";
                   11528:                             }
                   11529:                             unless ($ishome) {
                   11530:                                 my $fetch = "$fullpath/$title";
                   11531:                                 $fetch =~ s/^\Q$prefix$dir\E//;
                   11532:                                 $prompttofetch{$fetch} = 1;
1.1055    raeburn  11533:                             }
                   11534:                         }
                   11535:                     }
1.1075.2.11  raeburn  11536:                 } elsif ($env{'form.archive_'.$referrer{$i}} eq 'discard') {
                   11537:                     $warning .= &mt('[_1] is a dependency of [_2], which was discarded.',
                   11538:                                     $path,$env{'form.archive_content_'.$referrer{$i}}).'<br />';
1.1055    raeburn  11539:                 }
                   11540:             } else {
1.1075.2.11  raeburn  11541:                 $warning .= &mt('Item extracted from archive: [_1] has unexpected path.',$path).'<br />';
1.1055    raeburn  11542:             }
                   11543:         }
                   11544:         if (keys(%todelete)) {
                   11545:             foreach my $key (keys(%todelete)) {
                   11546:                 unlink($key);
1.1066    raeburn  11547:             }
                   11548:         }
                   11549:         if (keys(%todeletedir)) {
                   11550:             foreach my $key (keys(%todeletedir)) {
                   11551:                 rmdir($key);
                   11552:             }
                   11553:         }
                   11554:         foreach my $dir (sort(keys(%is_dir))) {
                   11555:             if (($pathtocheck ne '') && ($dir ne ''))  {
                   11556:                 &cleanup_empty_dirs($prefix."$pathtocheck/$dir");
1.1055    raeburn  11557:             }
                   11558:         }
1.1067    raeburn  11559:         if ($result ne '') {
                   11560:             $output .= '<ul>'."\n".
                   11561:                        $result."\n".
                   11562:                        '</ul>';
                   11563:         }
                   11564:         unless ($ishome) {
                   11565:             my $replicationfail;
                   11566:             foreach my $item (keys(%prompttofetch)) {
                   11567:                 my $fetchresult= &Apache::lonnet::reply('fetchuserfile:'.$item,$docuhome);
                   11568:                 unless ($fetchresult eq 'ok') {
                   11569:                     $replicationfail .= '<li>'.$item.'</li>'."\n";
                   11570:                 }
                   11571:             }
                   11572:             if ($replicationfail) {
                   11573:                 $output .= '<p class="LC_error">'.
                   11574:                            &mt('Course home server failed to retrieve:').'<ul>'.
                   11575:                            $replicationfail.
                   11576:                            '</ul></p>';
                   11577:             }
                   11578:         }
1.1055    raeburn  11579:     } else {
                   11580:         $warning = &mt('No items found in archive.');
                   11581:     }
                   11582:     if ($error) {
                   11583:         $output .= '<p class="LC_error">'.&mt('Not extracted.').'<br />'.
                   11584:                    $error.'</p>'."\n";
                   11585:     }
                   11586:     if ($warning) {
                   11587:         $output .= '<p class="LC_warning">'.$warning.'</p>'."\n";
                   11588:     }
                   11589:     return $output;
                   11590: }
                   11591: 
1.1066    raeburn  11592: sub cleanup_empty_dirs {
                   11593:     my ($path) = @_;
                   11594:     if (($path ne '') && (-d $path)) {
                   11595:         if (opendir(my $dirh,$path)) {
                   11596:             my @dircontents = grep(!/^\./,readdir($dirh));
                   11597:             my $numitems = 0;
                   11598:             foreach my $item (@dircontents) {
                   11599:                 if (-d "$path/$item") {
                   11600:                     &recurse_dirs("$path/$item");
                   11601:                     if (-e "$path/$item") {
                   11602:                         $numitems ++;
                   11603:                     }
                   11604:                 } else {
                   11605:                     $numitems ++;
                   11606:                 }
                   11607:             }
                   11608:             if ($numitems == 0) {
                   11609:                 rmdir($path);
                   11610:             }
                   11611:             closedir($dirh);
                   11612:         }
                   11613:     }
                   11614:     return;
                   11615: }
                   11616: 
1.41      ng       11617: =pod
1.45      matthew  11618: 
1.1068    raeburn  11619: =item &get_folder_hierarchy()
                   11620: 
                   11621: Provides hierarchy of names of folders/sub-folders containing the current
                   11622: item,
                   11623: 
                   11624: Inputs: 3
                   11625:      - $navmap - navmaps object
                   11626: 
                   11627:      - $map - url for map (either the trigger itself, or map containing
                   11628:                            the resource, which is the trigger).
                   11629: 
                   11630:      - $showitem - 1 => show title for map itself; 0 => do not show.
                   11631: 
                   11632: Outputs: 1 @pathitems - array of folder/subfolder names.
                   11633: 
                   11634: =cut
                   11635: 
                   11636: sub get_folder_hierarchy {
                   11637:     my ($navmap,$map,$showitem) = @_;
                   11638:     my @pathitems;
                   11639:     if (ref($navmap)) {
                   11640:         my $mapres = $navmap->getResourceByUrl($map);
                   11641:         if (ref($mapres)) {
                   11642:             my $pcslist = $mapres->map_hierarchy();
                   11643:             if ($pcslist ne '') {
                   11644:                 my @pcs = split(/,/,$pcslist);
                   11645:                 foreach my $pc (@pcs) {
                   11646:                     if ($pc == 1) {
                   11647:                         push(@pathitems,&mt('Main Course Documents'));
                   11648:                     } else {
                   11649:                         my $res = $navmap->getByMapPc($pc);
                   11650:                         if (ref($res)) {
                   11651:                             my $title = $res->compTitle();
                   11652:                             $title =~ s/\W+/_/g;
                   11653:                             if ($title ne '') {
                   11654:                                 push(@pathitems,$title);
                   11655:                             }
                   11656:                         }
                   11657:                     }
                   11658:                 }
                   11659:             }
1.1071    raeburn  11660:             if ($showitem) {
                   11661:                 if ($mapres->{ID} eq '0.0') {
                   11662:                     push(@pathitems,&mt('Main Course Documents'));
                   11663:                 } else {
                   11664:                     my $maptitle = $mapres->compTitle();
                   11665:                     $maptitle =~ s/\W+/_/g;
                   11666:                     if ($maptitle ne '') {
                   11667:                         push(@pathitems,$maptitle);
                   11668:                     }
1.1068    raeburn  11669:                 }
                   11670:             }
                   11671:         }
                   11672:     }
                   11673:     return @pathitems;
                   11674: }
                   11675: 
                   11676: =pod
                   11677: 
1.1015    raeburn  11678: =item * &get_turnedin_filepath()
                   11679: 
                   11680: Determines path in a user's portfolio file for storage of files uploaded
                   11681: to a specific essayresponse or dropbox item.
                   11682: 
                   11683: Inputs: 3 required + 1 optional.
                   11684: $symb is symb for resource, $uname and $udom are for current user (required).
                   11685: $caller is optional (can be "submission", if routine is called when storing
                   11686: an upoaded file when "Submit Answer" button was pressed).
                   11687: 
                   11688: Returns array containing $path and $multiresp. 
                   11689: $path is path in portfolio.  $multiresp is 1 if this resource contains more
                   11690: than one file upload item.  Callers of routine should append partid as a 
                   11691: subdirectory to $path in cases where $multiresp is 1.
                   11692: 
                   11693: Called by: homework/essayresponse.pm and homework/structuretags.pm
                   11694: 
                   11695: =cut
                   11696: 
                   11697: sub get_turnedin_filepath {
                   11698:     my ($symb,$uname,$udom,$caller) = @_;
                   11699:     my ($map,$resid,$resurl)=&Apache::lonnet::decode_symb($symb);
                   11700:     my $turnindir;
                   11701:     my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'turnindir');
                   11702:     $turnindir = $userhash{'turnindir'};
                   11703:     my ($path,$multiresp);
                   11704:     if ($turnindir eq '') {
                   11705:         if ($caller eq 'submission') {
                   11706:             $turnindir = &mt('turned in');
                   11707:             $turnindir =~ s/\W+/_/g;
                   11708:             my %newhash = (
                   11709:                             'turnindir' => $turnindir,
                   11710:                           );
                   11711:             &Apache::lonnet::put('environment',\%newhash,$udom,$uname);
                   11712:         }
                   11713:     }
                   11714:     if ($turnindir ne '') {
                   11715:         $path = '/'.$turnindir.'/';
                   11716:         my ($multipart,$turnin,@pathitems);
                   11717:         my $navmap = Apache::lonnavmaps::navmap->new();
                   11718:         if (defined($navmap)) {
                   11719:             my $mapres = $navmap->getResourceByUrl($map);
                   11720:             if (ref($mapres)) {
                   11721:                 my $pcslist = $mapres->map_hierarchy();
                   11722:                 if ($pcslist ne '') {
                   11723:                     foreach my $pc (split(/,/,$pcslist)) {
                   11724:                         my $res = $navmap->getByMapPc($pc);
                   11725:                         if (ref($res)) {
                   11726:                             my $title = $res->compTitle();
                   11727:                             $title =~ s/\W+/_/g;
                   11728:                             if ($title ne '') {
                   11729:                                 push(@pathitems,$title);
                   11730:                             }
                   11731:                         }
                   11732:                     }
                   11733:                 }
                   11734:                 my $maptitle = $mapres->compTitle();
                   11735:                 $maptitle =~ s/\W+/_/g;
                   11736:                 if ($maptitle ne '') {
                   11737:                     push(@pathitems,$maptitle);
                   11738:                 }
                   11739:                 unless ($env{'request.state'} eq 'construct') {
                   11740:                     my $res = $navmap->getBySymb($symb);
                   11741:                     if (ref($res)) {
                   11742:                         my $partlist = $res->parts();
                   11743:                         my $totaluploads = 0;
                   11744:                         if (ref($partlist) eq 'ARRAY') {
                   11745:                             foreach my $part (@{$partlist}) {
                   11746:                                 my @types = $res->responseType($part);
                   11747:                                 my @ids = $res->responseIds($part);
                   11748:                                 for (my $i=0; $i < scalar(@ids); $i++) {
                   11749:                                     if ($types[$i] eq 'essay') {
                   11750:                                         my $partid = $part.'_'.$ids[$i];
                   11751:                                         if (&Apache::lonnet::EXT("resource.$partid.uploadedfiletypes") ne '') {
                   11752:                                             $totaluploads ++;
                   11753:                                         }
                   11754:                                     }
                   11755:                                 }
                   11756:                             }
                   11757:                             if ($totaluploads > 1) {
                   11758:                                 $multiresp = 1;
                   11759:                             }
                   11760:                         }
                   11761:                     }
                   11762:                 }
                   11763:             } else {
                   11764:                 return;
                   11765:             }
                   11766:         } else {
                   11767:             return;
                   11768:         }
                   11769:         my $restitle=&Apache::lonnet::gettitle($symb);
                   11770:         $restitle =~ s/\W+/_/g;
                   11771:         if ($restitle eq '') {
                   11772:             $restitle = ($resurl =~ m{/[^/]+$});
                   11773:             if ($restitle eq '') {
                   11774:                 $restitle = time;
                   11775:             }
                   11776:         }
                   11777:         push(@pathitems,$restitle);
                   11778:         $path .= join('/',@pathitems);
                   11779:     }
                   11780:     return ($path,$multiresp);
                   11781: }
                   11782: 
                   11783: =pod
                   11784: 
1.464     albertel 11785: =back
1.41      ng       11786: 
1.112     bowersj2 11787: =head1 CSV Upload/Handling functions
1.38      albertel 11788: 
1.41      ng       11789: =over 4
                   11790: 
1.648     raeburn  11791: =item * &upfile_store($r)
1.41      ng       11792: 
                   11793: Store uploaded file, $r should be the HTTP Request object,
1.258     albertel 11794: needs $env{'form.upfile'}
1.41      ng       11795: returns $datatoken to be put into hidden field
                   11796: 
                   11797: =cut
1.31      albertel 11798: 
                   11799: sub upfile_store {
                   11800:     my $r=shift;
1.258     albertel 11801:     $env{'form.upfile'}=~s/\r/\n/gs;
                   11802:     $env{'form.upfile'}=~s/\f/\n/gs;
                   11803:     $env{'form.upfile'}=~s/\n+/\n/gs;
                   11804:     $env{'form.upfile'}=~s/\n+$//gs;
1.31      albertel 11805: 
1.258     albertel 11806:     my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
                   11807: 	'_enroll_'.$env{'request.course.id'}.'_'.time.'_'.$$;
1.31      albertel 11808:     {
1.158     raeburn  11809:         my $datafile = $r->dir_config('lonDaemons').
                   11810:                            '/tmp/'.$datatoken.'.tmp';
                   11811:         if ( open(my $fh,">$datafile") ) {
1.258     albertel 11812:             print $fh $env{'form.upfile'};
1.158     raeburn  11813:             close($fh);
                   11814:         }
1.31      albertel 11815:     }
                   11816:     return $datatoken;
                   11817: }
                   11818: 
1.56      matthew  11819: =pod
                   11820: 
1.648     raeburn  11821: =item * &load_tmp_file($r)
1.41      ng       11822: 
                   11823: Load uploaded file from tmp, $r should be the HTTP Request object,
1.258     albertel 11824: needs $env{'form.datatoken'},
                   11825: sets $env{'form.upfile'} to the contents of the file
1.41      ng       11826: 
                   11827: =cut
1.31      albertel 11828: 
                   11829: sub load_tmp_file {
                   11830:     my $r=shift;
                   11831:     my @studentdata=();
                   11832:     {
1.158     raeburn  11833:         my $studentfile = $r->dir_config('lonDaemons').
1.258     albertel 11834:                               '/tmp/'.$env{'form.datatoken'}.'.tmp';
1.158     raeburn  11835:         if ( open(my $fh,"<$studentfile") ) {
                   11836:             @studentdata=<$fh>;
                   11837:             close($fh);
                   11838:         }
1.31      albertel 11839:     }
1.258     albertel 11840:     $env{'form.upfile'}=join('',@studentdata);
1.31      albertel 11841: }
                   11842: 
1.56      matthew  11843: =pod
                   11844: 
1.648     raeburn  11845: =item * &upfile_record_sep()
1.41      ng       11846: 
                   11847: Separate uploaded file into records
                   11848: returns array of records,
1.258     albertel 11849: needs $env{'form.upfile'} and $env{'form.upfiletype'}
1.41      ng       11850: 
                   11851: =cut
1.31      albertel 11852: 
                   11853: sub upfile_record_sep {
1.258     albertel 11854:     if ($env{'form.upfiletype'} eq 'xml') {
1.31      albertel 11855:     } else {
1.248     albertel 11856: 	my @records;
1.258     albertel 11857: 	foreach my $line (split(/\n/,$env{'form.upfile'})) {
1.248     albertel 11858: 	    if ($line=~/^\s*$/) { next; }
                   11859: 	    push(@records,$line);
                   11860: 	}
                   11861: 	return @records;
1.31      albertel 11862:     }
                   11863: }
                   11864: 
1.56      matthew  11865: =pod
                   11866: 
1.648     raeburn  11867: =item * &record_sep($record)
1.41      ng       11868: 
1.258     albertel 11869: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $env{'form.upfiletype'}
1.41      ng       11870: 
                   11871: =cut
                   11872: 
1.263     www      11873: sub takeleft {
                   11874:     my $index=shift;
                   11875:     return substr('0000'.$index,-4,4);
                   11876: }
                   11877: 
1.31      albertel 11878: sub record_sep {
                   11879:     my $record=shift;
                   11880:     my %components=();
1.258     albertel 11881:     if ($env{'form.upfiletype'} eq 'xml') {
                   11882:     } elsif ($env{'form.upfiletype'} eq 'space') {
1.31      albertel 11883:         my $i=0;
1.356     albertel 11884:         foreach my $field (split(/\s+/,$record)) {
1.31      albertel 11885:             $field=~s/^(\"|\')//;
                   11886:             $field=~s/(\"|\')$//;
1.263     www      11887:             $components{&takeleft($i)}=$field;
1.31      albertel 11888:             $i++;
                   11889:         }
1.258     albertel 11890:     } elsif ($env{'form.upfiletype'} eq 'tab') {
1.31      albertel 11891:         my $i=0;
1.356     albertel 11892:         foreach my $field (split(/\t/,$record)) {
1.31      albertel 11893:             $field=~s/^(\"|\')//;
                   11894:             $field=~s/(\"|\')$//;
1.263     www      11895:             $components{&takeleft($i)}=$field;
1.31      albertel 11896:             $i++;
                   11897:         }
                   11898:     } else {
1.561     www      11899:         my $separator=',';
1.480     banghart 11900:         if ($env{'form.upfiletype'} eq 'semisv') {
1.561     www      11901:             $separator=';';
1.480     banghart 11902:         }
1.31      albertel 11903:         my $i=0;
1.561     www      11904: # the character we are looking for to indicate the end of a quote or a record 
                   11905:         my $looking_for=$separator;
                   11906: # do not add the characters to the fields
                   11907:         my $ignore=0;
                   11908: # we just encountered a separator (or the beginning of the record)
                   11909:         my $just_found_separator=1;
                   11910: # store the field we are working on here
                   11911:         my $field='';
                   11912: # work our way through all characters in record
                   11913:         foreach my $character ($record=~/(.)/g) {
                   11914:             if ($character eq $looking_for) {
                   11915:                if ($character ne $separator) {
                   11916: # Found the end of a quote, again looking for separator
                   11917:                   $looking_for=$separator;
                   11918:                   $ignore=1;
                   11919:                } else {
                   11920: # Found a separator, store away what we got
                   11921:                   $components{&takeleft($i)}=$field;
                   11922: 	          $i++;
                   11923:                   $just_found_separator=1;
                   11924:                   $ignore=0;
                   11925:                   $field='';
                   11926:                }
                   11927:                next;
                   11928:             }
                   11929: # single or double quotation marks after a separator indicate beginning of a quote
                   11930: # we are now looking for the end of the quote and need to ignore separators
                   11931:             if ((($character eq '"') || ($character eq "'")) && ($just_found_separator))  {
                   11932:                $looking_for=$character;
                   11933:                next;
                   11934:             }
                   11935: # ignore would be true after we reached the end of a quote
                   11936:             if ($ignore) { next; }
                   11937:             if (($just_found_separator) && ($character=~/\s/)) { next; }
                   11938:             $field.=$character;
                   11939:             $just_found_separator=0; 
1.31      albertel 11940:         }
1.561     www      11941: # catch the very last entry, since we never encountered the separator
                   11942:         $components{&takeleft($i)}=$field;
1.31      albertel 11943:     }
                   11944:     return %components;
                   11945: }
                   11946: 
1.144     matthew  11947: ######################################################
                   11948: ######################################################
                   11949: 
1.56      matthew  11950: =pod
                   11951: 
1.648     raeburn  11952: =item * &upfile_select_html()
1.41      ng       11953: 
1.144     matthew  11954: Return HTML code to select a file from the users machine and specify 
                   11955: the file type.
1.41      ng       11956: 
                   11957: =cut
                   11958: 
1.144     matthew  11959: ######################################################
                   11960: ######################################################
1.31      albertel 11961: sub upfile_select_html {
1.144     matthew  11962:     my %Types = (
                   11963:                  csv   => &mt('CSV (comma separated values, spreadsheet)'),
1.480     banghart 11964:                  semisv => &mt('Semicolon separated values'),
1.144     matthew  11965:                  space => &mt('Space separated'),
                   11966:                  tab   => &mt('Tabulator separated'),
                   11967: #                 xml   => &mt('HTML/XML'),
                   11968:                  );
                   11969:     my $Str = '<input type="file" name="upfile" size="50" />'.
1.727     riegler  11970:         '<br />'.&mt('Type').': <select name="upfiletype">';
1.144     matthew  11971:     foreach my $type (sort(keys(%Types))) {
                   11972:         $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
                   11973:     }
                   11974:     $Str .= "</select>\n";
                   11975:     return $Str;
1.31      albertel 11976: }
                   11977: 
1.301     albertel 11978: sub get_samples {
                   11979:     my ($records,$toget) = @_;
                   11980:     my @samples=({});
                   11981:     my $got=0;
                   11982:     foreach my $rec (@$records) {
                   11983: 	my %temp = &record_sep($rec);
                   11984: 	if (! grep(/\S/, values(%temp))) { next; }
                   11985: 	if (%temp) {
                   11986: 	    $samples[$got]=\%temp;
                   11987: 	    $got++;
                   11988: 	    if ($got == $toget) { last; }
                   11989: 	}
                   11990:     }
                   11991:     return \@samples;
                   11992: }
                   11993: 
1.144     matthew  11994: ######################################################
                   11995: ######################################################
                   11996: 
1.56      matthew  11997: =pod
                   11998: 
1.648     raeburn  11999: =item * &csv_print_samples($r,$records)
1.41      ng       12000: 
                   12001: Prints a table of sample values from each column uploaded $r is an
                   12002: Apache Request ref, $records is an arrayref from
                   12003: &Apache::loncommon::upfile_record_sep
                   12004: 
                   12005: =cut
                   12006: 
1.144     matthew  12007: ######################################################
                   12008: ######################################################
1.31      albertel 12009: sub csv_print_samples {
                   12010:     my ($r,$records) = @_;
1.662     bisitz   12011:     my $samples = &get_samples($records,5);
1.301     albertel 12012: 
1.594     raeburn  12013:     $r->print(&mt('Samples').'<br />'.&start_data_table().
                   12014:               &start_data_table_header_row());
1.356     albertel 12015:     foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) { 
1.845     bisitz   12016:         $r->print('<th>'.&mt('Column [_1]',($sample+1)).'</th>'); }
1.594     raeburn  12017:     $r->print(&end_data_table_header_row());
1.301     albertel 12018:     foreach my $hash (@$samples) {
1.594     raeburn  12019: 	$r->print(&start_data_table_row());
1.356     albertel 12020: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
1.31      albertel 12021: 	    $r->print('<td>');
1.356     albertel 12022: 	    if (defined($$hash{$sample})) { $r->print($$hash{$sample}); }
1.31      albertel 12023: 	    $r->print('</td>');
                   12024: 	}
1.594     raeburn  12025: 	$r->print(&end_data_table_row());
1.31      albertel 12026:     }
1.594     raeburn  12027:     $r->print(&end_data_table().'<br />'."\n");
1.31      albertel 12028: }
                   12029: 
1.144     matthew  12030: ######################################################
                   12031: ######################################################
                   12032: 
1.56      matthew  12033: =pod
                   12034: 
1.648     raeburn  12035: =item * &csv_print_select_table($r,$records,$d)
1.41      ng       12036: 
                   12037: Prints a table to create associations between values and table columns.
1.144     matthew  12038: 
1.41      ng       12039: $r is an Apache Request ref,
                   12040: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
1.174     matthew  12041: $d is an array of 2 element arrays (internal name, displayed name,defaultcol)
1.41      ng       12042: 
                   12043: =cut
                   12044: 
1.144     matthew  12045: ######################################################
                   12046: ######################################################
1.31      albertel 12047: sub csv_print_select_table {
                   12048:     my ($r,$records,$d) = @_;
1.301     albertel 12049:     my $i=0;
                   12050:     my $samples = &get_samples($records,1);
1.144     matthew  12051:     $r->print(&mt('Associate columns with student attributes.')."\n".
1.594     raeburn  12052: 	      &start_data_table().&start_data_table_header_row().
1.144     matthew  12053:               '<th>'.&mt('Attribute').'</th>'.
1.594     raeburn  12054:               '<th>'.&mt('Column').'</th>'.
                   12055:               &end_data_table_header_row()."\n");
1.356     albertel 12056:     foreach my $array_ref (@$d) {
                   12057: 	my ($value,$display,$defaultcol)=@{ $array_ref };
1.729     raeburn  12058: 	$r->print(&start_data_table_row().'<td>'.$display.'</td>');
1.31      albertel 12059: 
1.875     bisitz   12060: 	$r->print('<td><select name="f'.$i.'"'.
1.32      matthew  12061: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.31      albertel 12062: 	$r->print('<option value="none"></option>');
1.356     albertel 12063: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
                   12064: 	    $r->print('<option value="'.$sample.'"'.
                   12065:                       ($sample eq $defaultcol ? ' selected="selected" ' : '').
1.662     bisitz   12066:                       '>'.&mt('Column [_1]',($sample+1)).'</option>');
1.31      albertel 12067: 	}
1.594     raeburn  12068: 	$r->print('</select></td>'.&end_data_table_row()."\n");
1.31      albertel 12069: 	$i++;
                   12070:     }
1.594     raeburn  12071:     $r->print(&end_data_table());
1.31      albertel 12072:     $i--;
                   12073:     return $i;
                   12074: }
1.56      matthew  12075: 
1.144     matthew  12076: ######################################################
                   12077: ######################################################
                   12078: 
1.56      matthew  12079: =pod
1.31      albertel 12080: 
1.648     raeburn  12081: =item * &csv_samples_select_table($r,$records,$d)
1.41      ng       12082: 
                   12083: Prints a table of sample values from the upload and can make associate samples to internal names.
                   12084: 
                   12085: $r is an Apache Request ref,
                   12086: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
                   12087: $d is an array of 2 element arrays (internal name, displayed name)
                   12088: 
                   12089: =cut
                   12090: 
1.144     matthew  12091: ######################################################
                   12092: ######################################################
1.31      albertel 12093: sub csv_samples_select_table {
                   12094:     my ($r,$records,$d) = @_;
                   12095:     my $i=0;
1.144     matthew  12096:     #
1.662     bisitz   12097:     my $max_samples = 5;
                   12098:     my $samples = &get_samples($records,$max_samples);
1.594     raeburn  12099:     $r->print(&start_data_table().
                   12100:               &start_data_table_header_row().'<th>'.
                   12101:               &mt('Field').'</th><th>'.&mt('Samples').'</th>'.
                   12102:               &end_data_table_header_row());
1.301     albertel 12103: 
                   12104:     foreach my $key (sort(keys(%{ $samples->[0] }))) {
1.594     raeburn  12105: 	$r->print(&start_data_table_row().'<td><select name="f'.$i.'"'.
1.32      matthew  12106: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.301     albertel 12107: 	foreach my $option (@$d) {
                   12108: 	    my ($value,$display,$defaultcol)=@{ $option };
1.174     matthew  12109: 	    $r->print('<option value="'.$value.'"'.
1.253     albertel 12110:                       ($i eq $defaultcol ? ' selected="selected" ':'').'>'.
1.174     matthew  12111:                       $display.'</option>');
1.31      albertel 12112: 	}
                   12113: 	$r->print('</select></td><td>');
1.662     bisitz   12114: 	foreach my $line (0..($max_samples-1)) {
1.301     albertel 12115: 	    if (defined($samples->[$line]{$key})) { 
                   12116: 		$r->print($samples->[$line]{$key}."<br />\n"); 
                   12117: 	    }
                   12118: 	}
1.594     raeburn  12119: 	$r->print('</td>'.&end_data_table_row());
1.31      albertel 12120: 	$i++;
                   12121:     }
1.594     raeburn  12122:     $r->print(&end_data_table());
1.31      albertel 12123:     $i--;
                   12124:     return($i);
1.115     matthew  12125: }
                   12126: 
1.144     matthew  12127: ######################################################
                   12128: ######################################################
                   12129: 
1.115     matthew  12130: =pod
                   12131: 
1.648     raeburn  12132: =item * &clean_excel_name($name)
1.115     matthew  12133: 
                   12134: Returns a replacement for $name which does not contain any illegal characters.
                   12135: 
                   12136: =cut
                   12137: 
1.144     matthew  12138: ######################################################
                   12139: ######################################################
1.115     matthew  12140: sub clean_excel_name {
                   12141:     my ($name) = @_;
                   12142:     $name =~ s/[:\*\?\/\\]//g;
                   12143:     if (length($name) > 31) {
                   12144:         $name = substr($name,0,31);
                   12145:     }
                   12146:     return $name;
1.25      albertel 12147: }
1.84      albertel 12148: 
1.85      albertel 12149: =pod
                   12150: 
1.648     raeburn  12151: =item * &check_if_partid_hidden($id,$symb,$udom,$uname)
1.85      albertel 12152: 
                   12153: Returns either 1 or undef
                   12154: 
                   12155: 1 if the part is to be hidden, undef if it is to be shown
                   12156: 
                   12157: Arguments are:
                   12158: 
                   12159: $id the id of the part to be checked
                   12160: $symb, optional the symb of the resource to check
                   12161: $udom, optional the domain of the user to check for
                   12162: $uname, optional the username of the user to check for
                   12163: 
                   12164: =cut
1.84      albertel 12165: 
                   12166: sub check_if_partid_hidden {
                   12167:     my ($id,$symb,$udom,$uname) = @_;
1.133     albertel 12168:     my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
1.84      albertel 12169: 					 $symb,$udom,$uname);
1.141     albertel 12170:     my $truth=1;
                   12171:     #if the string starts with !, then the list is the list to show not hide
                   12172:     if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
1.84      albertel 12173:     my @hiddenlist=split(/,/,$hiddenparts);
                   12174:     foreach my $checkid (@hiddenlist) {
1.141     albertel 12175: 	if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
1.84      albertel 12176:     }
1.141     albertel 12177:     return !$truth;
1.84      albertel 12178: }
1.127     matthew  12179: 
1.138     matthew  12180: 
                   12181: ############################################################
                   12182: ############################################################
                   12183: 
                   12184: =pod
                   12185: 
1.157     matthew  12186: =back 
                   12187: 
1.138     matthew  12188: =head1 cgi-bin script and graphing routines
                   12189: 
1.157     matthew  12190: =over 4
                   12191: 
1.648     raeburn  12192: =item * &get_cgi_id()
1.138     matthew  12193: 
                   12194: Inputs: none
                   12195: 
                   12196: Returns an id which can be used to pass environment variables
                   12197: to various cgi-bin scripts.  These environment variables will
                   12198: be removed from the users environment after a given time by
                   12199: the routine &Apache::lonnet::transfer_profile_to_env.
                   12200: 
                   12201: =cut
                   12202: 
                   12203: ############################################################
                   12204: ############################################################
1.152     albertel 12205: my $uniq=0;
1.136     matthew  12206: sub get_cgi_id {
1.154     albertel 12207:     $uniq=($uniq+1)%100000;
1.280     albertel 12208:     return (time.'_'.$$.'_'.$uniq);
1.136     matthew  12209: }
                   12210: 
1.127     matthew  12211: ############################################################
                   12212: ############################################################
                   12213: 
                   12214: =pod
                   12215: 
1.648     raeburn  12216: =item * &DrawBarGraph()
1.127     matthew  12217: 
1.138     matthew  12218: Facilitates the plotting of data in a (stacked) bar graph.
                   12219: Puts plot definition data into the users environment in order for 
                   12220: graph.png to plot it.  Returns an <img> tag for the plot.
                   12221: The bars on the plot are labeled '1','2',...,'n'.
                   12222: 
                   12223: Inputs:
                   12224: 
                   12225: =over 4
                   12226: 
                   12227: =item $Title: string, the title of the plot
                   12228: 
                   12229: =item $xlabel: string, text describing the X-axis of the plot
                   12230: 
                   12231: =item $ylabel: string, text describing the Y-axis of the plot
                   12232: 
                   12233: =item $Max: scalar, the maximum Y value to use in the plot
                   12234: If $Max is < any data point, the graph will not be rendered.
                   12235: 
1.140     matthew  12236: =item $colors: array ref holding the colors to be used for the data sets when
1.138     matthew  12237: they are plotted.  If undefined, default values will be used.
                   12238: 
1.178     matthew  12239: =item $labels: array ref holding the labels to use on the x-axis for the bars.
                   12240: 
1.138     matthew  12241: =item @Values: An array of array references.  Each array reference holds data
                   12242: to be plotted in a stacked bar chart.
                   12243: 
1.239     matthew  12244: =item If the final element of @Values is a hash reference the key/value
                   12245: pairs will be added to the graph definition.
                   12246: 
1.138     matthew  12247: =back
                   12248: 
                   12249: Returns:
                   12250: 
                   12251: An <img> tag which references graph.png and the appropriate identifying
                   12252: information for the plot.
                   12253: 
1.127     matthew  12254: =cut
                   12255: 
                   12256: ############################################################
                   12257: ############################################################
1.134     matthew  12258: sub DrawBarGraph {
1.178     matthew  12259:     my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
1.134     matthew  12260:     #
                   12261:     if (! defined($colors)) {
                   12262:         $colors = ['#33ff00', 
                   12263:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
                   12264:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
                   12265:                   ]; 
                   12266:     }
1.228     matthew  12267:     my $extra_settings = {};
                   12268:     if (ref($Values[-1]) eq 'HASH') {
                   12269:         $extra_settings = pop(@Values);
                   12270:     }
1.127     matthew  12271:     #
1.136     matthew  12272:     my $identifier = &get_cgi_id();
                   12273:     my $id = 'cgi.'.$identifier;        
1.129     matthew  12274:     if (! @Values || ref($Values[0]) ne 'ARRAY') {
1.127     matthew  12275:         return '';
                   12276:     }
1.225     matthew  12277:     #
                   12278:     my @Labels;
                   12279:     if (defined($labels)) {
                   12280:         @Labels = @$labels;
                   12281:     } else {
                   12282:         for (my $i=0;$i<@{$Values[0]};$i++) {
                   12283:             push (@Labels,$i+1);
                   12284:         }
                   12285:     }
                   12286:     #
1.129     matthew  12287:     my $NumBars = scalar(@{$Values[0]});
1.225     matthew  12288:     if ($NumBars < scalar(@Labels)) { $NumBars = scalar(@Labels); }
1.129     matthew  12289:     my %ValuesHash;
                   12290:     my $NumSets=1;
                   12291:     foreach my $array (@Values) {
                   12292:         next if (! ref($array));
1.136     matthew  12293:         $ValuesHash{$id.'.data.'.$NumSets++} = 
1.132     matthew  12294:             join(',',@$array);
1.129     matthew  12295:     }
1.127     matthew  12296:     #
1.136     matthew  12297:     my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
1.225     matthew  12298:     if ($NumBars < 3) {
                   12299:         $width = 120+$NumBars*32;
1.220     matthew  12300:         $xskip = 1;
1.225     matthew  12301:         $bar_width = 30;
                   12302:     } elsif ($NumBars < 5) {
                   12303:         $width = 120+$NumBars*20;
                   12304:         $xskip = 1;
                   12305:         $bar_width = 20;
1.220     matthew  12306:     } elsif ($NumBars < 10) {
1.136     matthew  12307:         $width = 120+$NumBars*15;
                   12308:         $xskip = 1;
                   12309:         $bar_width = 15;
                   12310:     } elsif ($NumBars <= 25) {
                   12311:         $width = 120+$NumBars*11;
                   12312:         $xskip = 5;
                   12313:         $bar_width = 8;
                   12314:     } elsif ($NumBars <= 50) {
                   12315:         $width = 120+$NumBars*8;
                   12316:         $xskip = 5;
                   12317:         $bar_width = 4;
                   12318:     } else {
                   12319:         $width = 120+$NumBars*8;
                   12320:         $xskip = 5;
                   12321:         $bar_width = 4;
                   12322:     }
                   12323:     #
1.137     matthew  12324:     $Max = 1 if ($Max < 1);
                   12325:     if ( int($Max) < $Max ) {
                   12326:         $Max++;
                   12327:         $Max = int($Max);
                   12328:     }
1.127     matthew  12329:     $Title  = '' if (! defined($Title));
                   12330:     $xlabel = '' if (! defined($xlabel));
                   12331:     $ylabel = '' if (! defined($ylabel));
1.369     www      12332:     $ValuesHash{$id.'.title'}    = &escape($Title);
                   12333:     $ValuesHash{$id.'.xlabel'}   = &escape($xlabel);
                   12334:     $ValuesHash{$id.'.ylabel'}   = &escape($ylabel);
1.137     matthew  12335:     $ValuesHash{$id.'.y_max_value'} = $Max;
1.136     matthew  12336:     $ValuesHash{$id.'.NumBars'}  = $NumBars;
                   12337:     $ValuesHash{$id.'.NumSets'}  = $NumSets;
                   12338:     $ValuesHash{$id.'.PlotType'} = 'bar';
                   12339:     $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   12340:     $ValuesHash{$id.'.height'}   = $height;
                   12341:     $ValuesHash{$id.'.width'}    = $width;
                   12342:     $ValuesHash{$id.'.xskip'}    = $xskip;
                   12343:     $ValuesHash{$id.'.bar_width'} = $bar_width;
                   12344:     $ValuesHash{$id.'.labels'} = join(',',@Labels);
1.127     matthew  12345:     #
1.228     matthew  12346:     # Deal with other parameters
                   12347:     while (my ($key,$value) = each(%$extra_settings)) {
                   12348:         $ValuesHash{$id.'.'.$key} = $value;
                   12349:     }
                   12350:     #
1.646     raeburn  12351:     &Apache::lonnet::appenv(\%ValuesHash);
1.137     matthew  12352:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   12353: }
                   12354: 
                   12355: ############################################################
                   12356: ############################################################
                   12357: 
                   12358: =pod
                   12359: 
1.648     raeburn  12360: =item * &DrawXYGraph()
1.137     matthew  12361: 
1.138     matthew  12362: Facilitates the plotting of data in an XY graph.
                   12363: Puts plot definition data into the users environment in order for 
                   12364: graph.png to plot it.  Returns an <img> tag for the plot.
                   12365: 
                   12366: Inputs:
                   12367: 
                   12368: =over 4
                   12369: 
                   12370: =item $Title: string, the title of the plot
                   12371: 
                   12372: =item $xlabel: string, text describing the X-axis of the plot
                   12373: 
                   12374: =item $ylabel: string, text describing the Y-axis of the plot
                   12375: 
                   12376: =item $Max: scalar, the maximum Y value to use in the plot
                   12377: If $Max is < any data point, the graph will not be rendered.
                   12378: 
                   12379: =item $colors: Array ref containing the hex color codes for the data to be 
                   12380: plotted in.  If undefined, default values will be used.
                   12381: 
                   12382: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   12383: 
                   12384: =item $Ydata: Array ref containing Array refs.  
1.185     www      12385: Each of the contained arrays will be plotted as a separate curve.
1.138     matthew  12386: 
                   12387: =item %Values: hash indicating or overriding any default values which are 
                   12388: passed to graph.png.  
                   12389: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   12390: 
                   12391: =back
                   12392: 
                   12393: Returns:
                   12394: 
                   12395: An <img> tag which references graph.png and the appropriate identifying
                   12396: information for the plot.
                   12397: 
1.137     matthew  12398: =cut
                   12399: 
                   12400: ############################################################
                   12401: ############################################################
                   12402: sub DrawXYGraph {
                   12403:     my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
                   12404:     #
                   12405:     # Create the identifier for the graph
                   12406:     my $identifier = &get_cgi_id();
                   12407:     my $id = 'cgi.'.$identifier;
                   12408:     #
                   12409:     $Title  = '' if (! defined($Title));
                   12410:     $xlabel = '' if (! defined($xlabel));
                   12411:     $ylabel = '' if (! defined($ylabel));
                   12412:     my %ValuesHash = 
                   12413:         (
1.369     www      12414:          $id.'.title'  => &escape($Title),
                   12415:          $id.'.xlabel' => &escape($xlabel),
                   12416:          $id.'.ylabel' => &escape($ylabel),
1.137     matthew  12417:          $id.'.y_max_value'=> $Max,
                   12418:          $id.'.labels'     => join(',',@$Xlabels),
                   12419:          $id.'.PlotType'   => 'XY',
                   12420:          );
                   12421:     #
                   12422:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   12423:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   12424:     }
                   12425:     #
                   12426:     if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
                   12427:         return '';
                   12428:     }
                   12429:     my $NumSets=1;
1.138     matthew  12430:     foreach my $array (@{$Ydata}){
1.137     matthew  12431:         next if (! ref($array));
                   12432:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
                   12433:     }
1.138     matthew  12434:     $ValuesHash{$id.'.NumSets'} = $NumSets-1;
1.137     matthew  12435:     #
                   12436:     # Deal with other parameters
                   12437:     while (my ($key,$value) = each(%Values)) {
                   12438:         $ValuesHash{$id.'.'.$key} = $value;
1.127     matthew  12439:     }
                   12440:     #
1.646     raeburn  12441:     &Apache::lonnet::appenv(\%ValuesHash);
1.136     matthew  12442:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   12443: }
                   12444: 
                   12445: ############################################################
                   12446: ############################################################
                   12447: 
                   12448: =pod
                   12449: 
1.648     raeburn  12450: =item * &DrawXYYGraph()
1.138     matthew  12451: 
                   12452: Facilitates the plotting of data in an XY graph with two Y axes.
                   12453: Puts plot definition data into the users environment in order for 
                   12454: graph.png to plot it.  Returns an <img> tag for the plot.
                   12455: 
                   12456: Inputs:
                   12457: 
                   12458: =over 4
                   12459: 
                   12460: =item $Title: string, the title of the plot
                   12461: 
                   12462: =item $xlabel: string, text describing the X-axis of the plot
                   12463: 
                   12464: =item $ylabel: string, text describing the Y-axis of the plot
                   12465: 
                   12466: =item $colors: Array ref containing the hex color codes for the data to be 
                   12467: plotted in.  If undefined, default values will be used.
                   12468: 
                   12469: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   12470: 
                   12471: =item $Ydata1: The first data set
                   12472: 
                   12473: =item $Min1: The minimum value of the left Y-axis
                   12474: 
                   12475: =item $Max1: The maximum value of the left Y-axis
                   12476: 
                   12477: =item $Ydata2: The second data set
                   12478: 
                   12479: =item $Min2: The minimum value of the right Y-axis
                   12480: 
                   12481: =item $Max2: The maximum value of the left Y-axis
                   12482: 
                   12483: =item %Values: hash indicating or overriding any default values which are 
                   12484: passed to graph.png.  
                   12485: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   12486: 
                   12487: =back
                   12488: 
                   12489: Returns:
                   12490: 
                   12491: An <img> tag which references graph.png and the appropriate identifying
                   12492: information for the plot.
1.136     matthew  12493: 
                   12494: =cut
                   12495: 
                   12496: ############################################################
                   12497: ############################################################
1.137     matthew  12498: sub DrawXYYGraph {
                   12499:     my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
                   12500:                                         $Ydata2,$Min2,$Max2,%Values)=@_;
1.136     matthew  12501:     #
                   12502:     # Create the identifier for the graph
                   12503:     my $identifier = &get_cgi_id();
                   12504:     my $id = 'cgi.'.$identifier;
                   12505:     #
                   12506:     $Title  = '' if (! defined($Title));
                   12507:     $xlabel = '' if (! defined($xlabel));
                   12508:     $ylabel = '' if (! defined($ylabel));
                   12509:     my %ValuesHash = 
                   12510:         (
1.369     www      12511:          $id.'.title'  => &escape($Title),
                   12512:          $id.'.xlabel' => &escape($xlabel),
                   12513:          $id.'.ylabel' => &escape($ylabel),
1.136     matthew  12514:          $id.'.labels' => join(',',@$Xlabels),
                   12515:          $id.'.PlotType' => 'XY',
                   12516:          $id.'.NumSets' => 2,
1.137     matthew  12517:          $id.'.two_axes' => 1,
                   12518:          $id.'.y1_max_value' => $Max1,
                   12519:          $id.'.y1_min_value' => $Min1,
                   12520:          $id.'.y2_max_value' => $Max2,
                   12521:          $id.'.y2_min_value' => $Min2,
1.136     matthew  12522:          );
                   12523:     #
1.137     matthew  12524:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   12525:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   12526:     }
                   12527:     #
                   12528:     if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
                   12529:         ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
1.136     matthew  12530:         return '';
                   12531:     }
                   12532:     my $NumSets=1;
1.137     matthew  12533:     foreach my $array ($Ydata1,$Ydata2){
1.136     matthew  12534:         next if (! ref($array));
                   12535:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
1.137     matthew  12536:     }
                   12537:     #
                   12538:     # Deal with other parameters
                   12539:     while (my ($key,$value) = each(%Values)) {
                   12540:         $ValuesHash{$id.'.'.$key} = $value;
1.136     matthew  12541:     }
                   12542:     #
1.646     raeburn  12543:     &Apache::lonnet::appenv(\%ValuesHash);
1.130     albertel 12544:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
1.139     matthew  12545: }
                   12546: 
                   12547: ############################################################
                   12548: ############################################################
                   12549: 
                   12550: =pod
                   12551: 
1.157     matthew  12552: =back 
                   12553: 
1.139     matthew  12554: =head1 Statistics helper routines?  
                   12555: 
                   12556: Bad place for them but what the hell.
                   12557: 
1.157     matthew  12558: =over 4
                   12559: 
1.648     raeburn  12560: =item * &chartlink()
1.139     matthew  12561: 
                   12562: Returns a link to the chart for a specific student.  
                   12563: 
                   12564: Inputs:
                   12565: 
                   12566: =over 4
                   12567: 
                   12568: =item $linktext: The text of the link
                   12569: 
                   12570: =item $sname: The students username
                   12571: 
                   12572: =item $sdomain: The students domain
                   12573: 
                   12574: =back
                   12575: 
1.157     matthew  12576: =back
                   12577: 
1.139     matthew  12578: =cut
                   12579: 
                   12580: ############################################################
                   12581: ############################################################
                   12582: sub chartlink {
                   12583:     my ($linktext, $sname, $sdomain) = @_;
                   12584:     my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
1.369     www      12585:         '&amp;SelectedStudent='.&escape($sname.':'.$sdomain).
1.219     albertel 12586:         '&amp;chartoutputmode='.HTML::Entities::encode('html, with all links').
1.139     matthew  12587:        '">'.$linktext.'</a>';
1.153     matthew  12588: }
                   12589: 
                   12590: #######################################################
                   12591: #######################################################
                   12592: 
                   12593: =pod
                   12594: 
                   12595: =head1 Course Environment Routines
1.157     matthew  12596: 
                   12597: =over 4
1.153     matthew  12598: 
1.648     raeburn  12599: =item * &restore_course_settings()
1.153     matthew  12600: 
1.648     raeburn  12601: =item * &store_course_settings()
1.153     matthew  12602: 
                   12603: Restores/Store indicated form parameters from the course environment.
                   12604: Will not overwrite existing values of the form parameters.
                   12605: 
                   12606: Inputs: 
                   12607: a scalar describing the data (e.g. 'chart', 'problem_analysis')
                   12608: 
                   12609: a hash ref describing the data to be stored.  For example:
                   12610:    
                   12611: %Save_Parameters = ('Status' => 'scalar',
                   12612:     'chartoutputmode' => 'scalar',
                   12613:     'chartoutputdata' => 'scalar',
                   12614:     'Section' => 'array',
1.373     raeburn  12615:     'Group' => 'array',
1.153     matthew  12616:     'StudentData' => 'array',
                   12617:     'Maps' => 'array');
                   12618: 
                   12619: Returns: both routines return nothing
                   12620: 
1.631     raeburn  12621: =back
                   12622: 
1.153     matthew  12623: =cut
                   12624: 
                   12625: #######################################################
                   12626: #######################################################
                   12627: sub store_course_settings {
1.496     albertel 12628:     return &store_settings($env{'request.course.id'},@_);
                   12629: }
                   12630: 
                   12631: sub store_settings {
1.153     matthew  12632:     # save to the environment
                   12633:     # appenv the same items, just to be safe
1.300     albertel 12634:     my $udom  = $env{'user.domain'};
                   12635:     my $uname = $env{'user.name'};
1.496     albertel 12636:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  12637:     my %SaveHash;
                   12638:     my %AppHash;
                   12639:     while (my ($setting,$type) = each(%$Settings)) {
1.496     albertel 12640:         my $basename = join('.','internal',$context,$prefix,$setting);
1.300     albertel 12641:         my $envname = 'environment.'.$basename;
1.258     albertel 12642:         if (exists($env{'form.'.$setting})) {
1.153     matthew  12643:             # Save this value away
                   12644:             if ($type eq 'scalar' &&
1.258     albertel 12645:                 (! exists($env{$envname}) || 
                   12646:                  $env{$envname} ne $env{'form.'.$setting})) {
                   12647:                 $SaveHash{$basename} = $env{'form.'.$setting};
                   12648:                 $AppHash{$envname}   = $env{'form.'.$setting};
1.153     matthew  12649:             } elsif ($type eq 'array') {
                   12650:                 my $stored_form;
1.258     albertel 12651:                 if (ref($env{'form.'.$setting})) {
1.153     matthew  12652:                     $stored_form = join(',',
                   12653:                                         map {
1.369     www      12654:                                             &escape($_);
1.258     albertel 12655:                                         } sort(@{$env{'form.'.$setting}}));
1.153     matthew  12656:                 } else {
                   12657:                     $stored_form = 
1.369     www      12658:                         &escape($env{'form.'.$setting});
1.153     matthew  12659:                 }
                   12660:                 # Determine if the array contents are the same.
1.258     albertel 12661:                 if ($stored_form ne $env{$envname}) {
1.153     matthew  12662:                     $SaveHash{$basename} = $stored_form;
                   12663:                     $AppHash{$envname}   = $stored_form;
                   12664:                 }
                   12665:             }
                   12666:         }
                   12667:     }
                   12668:     my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
1.300     albertel 12669:                                           $udom,$uname);
1.153     matthew  12670:     if ($put_result !~ /^(ok|delayed)/) {
                   12671:         &Apache::lonnet::logthis('unable to save form parameters, '.
                   12672:                                  'got error:'.$put_result);
                   12673:     }
                   12674:     # Make sure these settings stick around in this session, too
1.646     raeburn  12675:     &Apache::lonnet::appenv(\%AppHash);
1.153     matthew  12676:     return;
                   12677: }
                   12678: 
                   12679: sub restore_course_settings {
1.499     albertel 12680:     return &restore_settings($env{'request.course.id'},@_);
1.496     albertel 12681: }
                   12682: 
                   12683: sub restore_settings {
                   12684:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  12685:     while (my ($setting,$type) = each(%$Settings)) {
1.258     albertel 12686:         next if (exists($env{'form.'.$setting}));
1.496     albertel 12687:         my $envname = 'environment.internal.'.$context.'.'.$prefix.
1.153     matthew  12688:             '.'.$setting;
1.258     albertel 12689:         if (exists($env{$envname})) {
1.153     matthew  12690:             if ($type eq 'scalar') {
1.258     albertel 12691:                 $env{'form.'.$setting} = $env{$envname};
1.153     matthew  12692:             } elsif ($type eq 'array') {
1.258     albertel 12693:                 $env{'form.'.$setting} = [ 
1.153     matthew  12694:                                            map { 
1.369     www      12695:                                                &unescape($_); 
1.258     albertel 12696:                                            } split(',',$env{$envname})
1.153     matthew  12697:                                            ];
                   12698:             }
                   12699:         }
                   12700:     }
1.127     matthew  12701: }
                   12702: 
1.618     raeburn  12703: #######################################################
                   12704: #######################################################
                   12705: 
                   12706: =pod
                   12707: 
                   12708: =head1 Domain E-mail Routines  
                   12709: 
                   12710: =over 4
                   12711: 
1.648     raeburn  12712: =item * &build_recipient_list()
1.618     raeburn  12713: 
1.884     raeburn  12714: Build recipient lists for five types of e-mail:
1.766     raeburn  12715: (a) Error Reports, (b) Package Updates, (c) lonstatus warnings/errors
1.884     raeburn  12716: (d) Help requests, (e) Course requests needing approval,  generated by
                   12717: lonerrorhandler.pm, CHECKRPMS, loncron, lonsupportreq.pm and
                   12718: loncoursequeueadmin.pm respectively.
1.618     raeburn  12719: 
                   12720: Inputs:
1.619     raeburn  12721: defmail (scalar - email address of default recipient), 
1.618     raeburn  12722: mailing type (scalar - errormail, packagesmail, or helpdeskmail), 
1.619     raeburn  12723: defdom (domain for which to retrieve configuration settings),
                   12724: origmail (scalar - email address of recipient from loncapa.conf, 
                   12725: i.e., predates configuration by DC via domainprefs.pm 
1.618     raeburn  12726: 
1.655     raeburn  12727: Returns: comma separated list of addresses to which to send e-mail.
                   12728: 
                   12729: =back
1.618     raeburn  12730: 
                   12731: =cut
                   12732: 
                   12733: ############################################################
                   12734: ############################################################
                   12735: sub build_recipient_list {
1.619     raeburn  12736:     my ($defmail,$mailing,$defdom,$origmail) = @_;
1.618     raeburn  12737:     my @recipients;
                   12738:     my $otheremails;
                   12739:     my %domconfig =
                   12740:          &Apache::lonnet::get_dom('configuration',['contacts'],$defdom);
                   12741:     if (ref($domconfig{'contacts'}) eq 'HASH') {
1.766     raeburn  12742:         if (exists($domconfig{'contacts'}{$mailing})) {
                   12743:             if (ref($domconfig{'contacts'}{$mailing}) eq 'HASH') {
                   12744:                 my @contacts = ('adminemail','supportemail');
                   12745:                 foreach my $item (@contacts) {
                   12746:                     if ($domconfig{'contacts'}{$mailing}{$item}) {
                   12747:                         my $addr = $domconfig{'contacts'}{$item}; 
                   12748:                         if (!grep(/^\Q$addr\E$/,@recipients)) {
                   12749:                             push(@recipients,$addr);
                   12750:                         }
1.619     raeburn  12751:                     }
1.766     raeburn  12752:                     $otheremails = $domconfig{'contacts'}{$mailing}{'others'};
1.618     raeburn  12753:                 }
                   12754:             }
1.766     raeburn  12755:         } elsif ($origmail ne '') {
                   12756:             push(@recipients,$origmail);
1.618     raeburn  12757:         }
1.619     raeburn  12758:     } elsif ($origmail ne '') {
                   12759:         push(@recipients,$origmail);
1.618     raeburn  12760:     }
1.688     raeburn  12761:     if (defined($defmail)) {
                   12762:         if ($defmail ne '') {
                   12763:             push(@recipients,$defmail);
                   12764:         }
1.618     raeburn  12765:     }
                   12766:     if ($otheremails) {
1.619     raeburn  12767:         my @others;
                   12768:         if ($otheremails =~ /,/) {
                   12769:             @others = split(/,/,$otheremails);
1.618     raeburn  12770:         } else {
1.619     raeburn  12771:             push(@others,$otheremails);
                   12772:         }
                   12773:         foreach my $addr (@others) {
                   12774:             if (!grep(/^\Q$addr\E$/,@recipients)) {
                   12775:                 push(@recipients,$addr);
                   12776:             }
1.618     raeburn  12777:         }
                   12778:     }
1.619     raeburn  12779:     my $recipientlist = join(',',@recipients); 
1.618     raeburn  12780:     return $recipientlist;
                   12781: }
                   12782: 
1.127     matthew  12783: ############################################################
                   12784: ############################################################
1.154     albertel 12785: 
1.655     raeburn  12786: =pod
                   12787: 
                   12788: =head1 Course Catalog Routines
                   12789: 
                   12790: =over 4
                   12791: 
                   12792: =item * &gather_categories()
                   12793: 
                   12794: Converts category definitions - keys of categories hash stored in  
                   12795: coursecategories in configuration.db on the primary library server in a 
                   12796: domain - to an array.  Also generates javascript and idx hash used to 
                   12797: generate Domain Coordinator interface for editing Course Categories.
                   12798: 
                   12799: Inputs:
1.663     raeburn  12800: 
1.655     raeburn  12801: categories (reference to hash of category definitions).
1.663     raeburn  12802: 
1.655     raeburn  12803: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   12804:       categories and subcategories).
1.663     raeburn  12805: 
1.655     raeburn  12806: idx (reference to hash of counters used in Domain Coordinator interface for 
                   12807:       editing Course Categories).
1.663     raeburn  12808: 
1.655     raeburn  12809: jsarray (reference to array of categories used to create Javascript arrays for
                   12810:          Domain Coordinator interface for editing Course Categories).
                   12811: 
                   12812: Returns: nothing
                   12813: 
                   12814: Side effects: populates cats, idx and jsarray. 
                   12815: 
                   12816: =cut
                   12817: 
                   12818: sub gather_categories {
                   12819:     my ($categories,$cats,$idx,$jsarray) = @_;
                   12820:     my %counters;
                   12821:     my $num = 0;
                   12822:     foreach my $item (keys(%{$categories})) {
                   12823:         my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
                   12824:         if ($container eq '' && $depth == 0) {
                   12825:             $cats->[$depth][$categories->{$item}] = $cat;
                   12826:         } else {
                   12827:             $cats->[$depth]{$container}[$categories->{$item}] = $cat;
                   12828:         }
                   12829:         my ($escitem,$tail) = split(/:/,$item,2);
                   12830:         if ($counters{$tail} eq '') {
                   12831:             $counters{$tail} = $num;
                   12832:             $num ++;
                   12833:         }
                   12834:         if (ref($idx) eq 'HASH') {
                   12835:             $idx->{$item} = $counters{$tail};
                   12836:         }
                   12837:         if (ref($jsarray) eq 'ARRAY') {
                   12838:             push(@{$jsarray->[$counters{$tail}]},$item);
                   12839:         }
                   12840:     }
                   12841:     return;
                   12842: }
                   12843: 
                   12844: =pod
                   12845: 
                   12846: =item * &extract_categories()
                   12847: 
                   12848: Used to generate breadcrumb trails for course categories.
                   12849: 
                   12850: Inputs:
1.663     raeburn  12851: 
1.655     raeburn  12852: categories (reference to hash of category definitions).
1.663     raeburn  12853: 
1.655     raeburn  12854: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   12855:       categories and subcategories).
1.663     raeburn  12856: 
1.655     raeburn  12857: trails (reference to array of breacrumb trails for each category).
1.663     raeburn  12858: 
1.655     raeburn  12859: allitems (reference to hash - key is category key 
                   12860:          (format: escaped(name):escaped(parent category):depth in hierarchy).
1.663     raeburn  12861: 
1.655     raeburn  12862: idx (reference to hash of counters used in Domain Coordinator interface for
                   12863:       editing Course Categories).
1.663     raeburn  12864: 
1.655     raeburn  12865: jsarray (reference to array of categories used to create Javascript arrays for
                   12866:          Domain Coordinator interface for editing Course Categories).
                   12867: 
1.665     raeburn  12868: subcats (reference to hash of arrays containing all subcategories within each 
                   12869:          category, -recursive)
                   12870: 
1.655     raeburn  12871: Returns: nothing
                   12872: 
                   12873: Side effects: populates trails and allitems hash references.
                   12874: 
                   12875: =cut
                   12876: 
                   12877: sub extract_categories {
1.665     raeburn  12878:     my ($categories,$cats,$trails,$allitems,$idx,$jsarray,$subcats) = @_;
1.655     raeburn  12879:     if (ref($categories) eq 'HASH') {
                   12880:         &gather_categories($categories,$cats,$idx,$jsarray);
                   12881:         if (ref($cats->[0]) eq 'ARRAY') {
                   12882:             for (my $i=0; $i<@{$cats->[0]}; $i++) {
                   12883:                 my $name = $cats->[0][$i];
                   12884:                 my $item = &escape($name).'::0';
                   12885:                 my $trailstr;
                   12886:                 if ($name eq 'instcode') {
                   12887:                     $trailstr = &mt('Official courses (with institutional codes)');
1.919     raeburn  12888:                 } elsif ($name eq 'communities') {
                   12889:                     $trailstr = &mt('Communities');
1.655     raeburn  12890:                 } else {
                   12891:                     $trailstr = $name;
                   12892:                 }
                   12893:                 if ($allitems->{$item} eq '') {
                   12894:                     push(@{$trails},$trailstr);
                   12895:                     $allitems->{$item} = scalar(@{$trails})-1;
                   12896:                 }
                   12897:                 my @parents = ($name);
                   12898:                 if (ref($cats->[1]{$name}) eq 'ARRAY') {
                   12899:                     for (my $j=0; $j<@{$cats->[1]{$name}}; $j++) {
                   12900:                         my $category = $cats->[1]{$name}[$j];
1.665     raeburn  12901:                         if (ref($subcats) eq 'HASH') {
                   12902:                             push(@{$subcats->{$item}},&escape($category).':'.&escape($name).':1');
                   12903:                         }
                   12904:                         &recurse_categories($cats,2,$category,$trails,$allitems,\@parents,$subcats);
                   12905:                     }
                   12906:                 } else {
                   12907:                     if (ref($subcats) eq 'HASH') {
                   12908:                         $subcats->{$item} = [];
1.655     raeburn  12909:                     }
                   12910:                 }
                   12911:             }
                   12912:         }
                   12913:     }
                   12914:     return;
                   12915: }
                   12916: 
                   12917: =pod
                   12918: 
                   12919: =item *&recurse_categories()
                   12920: 
                   12921: Recursively used to generate breadcrumb trails for course categories.
                   12922: 
                   12923: Inputs:
1.663     raeburn  12924: 
1.655     raeburn  12925: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   12926:       categories and subcategories).
1.663     raeburn  12927: 
1.655     raeburn  12928: depth (current depth in hierarchy of categories and sub-categories - 0 indexed).
1.663     raeburn  12929: 
                   12930: category (current course category, for which breadcrumb trail is being generated).
                   12931: 
                   12932: trails (reference to array of breadcrumb trails for each category).
                   12933: 
1.655     raeburn  12934: allitems (reference to hash - key is category key
                   12935:          (format: escaped(name):escaped(parent category):depth in hierarchy).
1.663     raeburn  12936: 
1.655     raeburn  12937: parents (array containing containers directories for current category, 
                   12938:          back to top level). 
                   12939: 
                   12940: Returns: nothing
                   12941: 
                   12942: Side effects: populates trails and allitems hash references
                   12943: 
                   12944: =cut
                   12945: 
                   12946: sub recurse_categories {
1.665     raeburn  12947:     my ($cats,$depth,$category,$trails,$allitems,$parents,$subcats) = @_;
1.655     raeburn  12948:     my $shallower = $depth - 1;
                   12949:     if (ref($cats->[$depth]{$category}) eq 'ARRAY') {
                   12950:         for (my $k=0; $k<@{$cats->[$depth]{$category}}; $k++) {
                   12951:             my $name = $cats->[$depth]{$category}[$k];
                   12952:             my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
                   12953:             my $trailstr = join(' -&gt; ',(@{$parents},$category));
                   12954:             if ($allitems->{$item} eq '') {
                   12955:                 push(@{$trails},$trailstr);
                   12956:                 $allitems->{$item} = scalar(@{$trails})-1;
                   12957:             }
                   12958:             my $deeper = $depth+1;
                   12959:             push(@{$parents},$category);
1.665     raeburn  12960:             if (ref($subcats) eq 'HASH') {
                   12961:                 my $subcat = &escape($name).':'.$category.':'.$depth;
                   12962:                 for (my $j=@{$parents}; $j>=0; $j--) {
                   12963:                     my $higher;
                   12964:                     if ($j > 0) {
                   12965:                         $higher = &escape($parents->[$j]).':'.
                   12966:                                   &escape($parents->[$j-1]).':'.$j;
                   12967:                     } else {
                   12968:                         $higher = &escape($parents->[$j]).'::'.$j;
                   12969:                     }
                   12970:                     push(@{$subcats->{$higher}},$subcat);
                   12971:                 }
                   12972:             }
                   12973:             &recurse_categories($cats,$deeper,$name,$trails,$allitems,$parents,
                   12974:                                 $subcats);
1.655     raeburn  12975:             pop(@{$parents});
                   12976:         }
                   12977:     } else {
                   12978:         my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
                   12979:         my $trailstr = join(' -&gt; ',(@{$parents},$category));
                   12980:         if ($allitems->{$item} eq '') {
                   12981:             push(@{$trails},$trailstr);
                   12982:             $allitems->{$item} = scalar(@{$trails})-1;
                   12983:         }
                   12984:     }
                   12985:     return;
                   12986: }
                   12987: 
1.663     raeburn  12988: =pod
                   12989: 
                   12990: =item *&assign_categories_table()
                   12991: 
                   12992: Create a datatable for display of hierarchical categories in a domain,
                   12993: with checkboxes to allow a course to be categorized. 
                   12994: 
                   12995: Inputs:
                   12996: 
                   12997: cathash - reference to hash of categories defined for the domain (from
                   12998:           configuration.db)
                   12999: 
                   13000: currcat - scalar with an & separated list of categories assigned to a course. 
                   13001: 
1.919     raeburn  13002: type    - scalar contains course type (Course or Community).
                   13003: 
1.663     raeburn  13004: Returns: $output (markup to be displayed) 
                   13005: 
                   13006: =cut
                   13007: 
                   13008: sub assign_categories_table {
1.919     raeburn  13009:     my ($cathash,$currcat,$type) = @_;
1.663     raeburn  13010:     my $output;
                   13011:     if (ref($cathash) eq 'HASH') {
                   13012:         my (@cats,@trails,%allitems,%idx,@jsarray,@path,$maxdepth);
                   13013:         &extract_categories($cathash,\@cats,\@trails,\%allitems,\%idx,\@jsarray);
                   13014:         $maxdepth = scalar(@cats);
                   13015:         if (@cats > 0) {
                   13016:             my $itemcount = 0;
                   13017:             if (ref($cats[0]) eq 'ARRAY') {
                   13018:                 my @currcategories;
                   13019:                 if ($currcat ne '') {
                   13020:                     @currcategories = split('&',$currcat);
                   13021:                 }
1.919     raeburn  13022:                 my $table;
1.663     raeburn  13023:                 for (my $i=0; $i<@{$cats[0]}; $i++) {
                   13024:                     my $parent = $cats[0][$i];
1.919     raeburn  13025:                     next if ($parent eq 'instcode');
                   13026:                     if ($type eq 'Community') {
                   13027:                         next unless ($parent eq 'communities');
                   13028:                     } else {
                   13029:                         next if ($parent eq 'communities');
                   13030:                     }
1.663     raeburn  13031:                     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   13032:                     my $item = &escape($parent).'::0';
                   13033:                     my $checked = '';
                   13034:                     if (@currcategories > 0) {
                   13035:                         if (grep(/^\Q$item\E$/,@currcategories)) {
1.772     bisitz   13036:                             $checked = ' checked="checked"';
1.663     raeburn  13037:                         }
                   13038:                     }
1.919     raeburn  13039:                     my $parent_title = $parent;
                   13040:                     if ($parent eq 'communities') {
                   13041:                         $parent_title = &mt('Communities');
                   13042:                     }
                   13043:                     $table .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.
                   13044:                               '<input type="checkbox" name="usecategory" value="'.
                   13045:                               $item.'"'.$checked.' />'.$parent_title.'</span>'.
                   13046:                               '<input type="hidden" name="catname" value="'.$parent.'" /></td>';
1.663     raeburn  13047:                     my $depth = 1;
                   13048:                     push(@path,$parent);
1.919     raeburn  13049:                     $table .= &assign_category_rows($itemcount,\@cats,$depth,$parent,\@path,\@currcategories);
1.663     raeburn  13050:                     pop(@path);
1.919     raeburn  13051:                     $table .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
1.663     raeburn  13052:                     $itemcount ++;
                   13053:                 }
1.919     raeburn  13054:                 if ($itemcount) {
                   13055:                     $output = &Apache::loncommon::start_data_table().
                   13056:                               $table.
                   13057:                               &Apache::loncommon::end_data_table();
                   13058:                 }
1.663     raeburn  13059:             }
                   13060:         }
                   13061:     }
                   13062:     return $output;
                   13063: }
                   13064: 
                   13065: =pod
                   13066: 
                   13067: =item *&assign_category_rows()
                   13068: 
                   13069: Create a datatable row for display of nested categories in a domain,
                   13070: with checkboxes to allow a course to be categorized,called recursively.
                   13071: 
                   13072: Inputs:
                   13073: 
                   13074: itemcount - track row number for alternating colors
                   13075: 
                   13076: cats - reference to array of arrays/hashes which encapsulates hierarchy of
                   13077:       categories and subcategories.
                   13078: 
                   13079: depth - current depth in hierarchy of categories and sub-categories - 0 indexed.
                   13080: 
                   13081: parent - parent of current category item
                   13082: 
                   13083: path - Array containing all categories back up through the hierarchy from the
                   13084:        current category to the top level.
                   13085: 
                   13086: currcategories - reference to array of current categories assigned to the course
                   13087: 
                   13088: Returns: $output (markup to be displayed).
                   13089: 
                   13090: =cut
                   13091: 
                   13092: sub assign_category_rows {
                   13093:     my ($itemcount,$cats,$depth,$parent,$path,$currcategories) = @_;
                   13094:     my ($text,$name,$item,$chgstr);
                   13095:     if (ref($cats) eq 'ARRAY') {
                   13096:         my $maxdepth = scalar(@{$cats});
                   13097:         if (ref($cats->[$depth]) eq 'HASH') {
                   13098:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
                   13099:                 my $numchildren = @{$cats->[$depth]{$parent}};
                   13100:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   13101:                 $text .= '<td><table class="LC_datatable">';
                   13102:                 for (my $j=0; $j<$numchildren; $j++) {
                   13103:                     $name = $cats->[$depth]{$parent}[$j];
                   13104:                     $item = &escape($name).':'.&escape($parent).':'.$depth;
                   13105:                     my $deeper = $depth+1;
                   13106:                     my $checked = '';
                   13107:                     if (ref($currcategories) eq 'ARRAY') {
                   13108:                         if (@{$currcategories} > 0) {
                   13109:                             if (grep(/^\Q$item\E$/,@{$currcategories})) {
1.772     bisitz   13110:                                 $checked = ' checked="checked"';
1.663     raeburn  13111:                             }
                   13112:                         }
                   13113:                     }
1.664     raeburn  13114:                     $text .= '<tr><td><span class="LC_nobreak"><label>'.
                   13115:                              '<input type="checkbox" name="usecategory" value="'.
1.675     raeburn  13116:                              $item.'"'.$checked.' />'.$name.'</label></span>'.
                   13117:                              '<input type="hidden" name="catname" value="'.$name.'" />'.
                   13118:                              '</td><td>';
1.663     raeburn  13119:                     if (ref($path) eq 'ARRAY') {
                   13120:                         push(@{$path},$name);
                   13121:                         $text .= &assign_category_rows($itemcount,$cats,$deeper,$name,$path,$currcategories);
                   13122:                         pop(@{$path});
                   13123:                     }
                   13124:                     $text .= '</td></tr>';
                   13125:                 }
                   13126:                 $text .= '</table></td>';
                   13127:             }
                   13128:         }
                   13129:     }
                   13130:     return $text;
                   13131: }
                   13132: 
1.655     raeburn  13133: ############################################################
                   13134: ############################################################
                   13135: 
                   13136: 
1.443     albertel 13137: sub commit_customrole {
1.664     raeburn  13138:     my ($udom,$uname,$url,$three,$four,$five,$start,$end,$context) = @_;
1.630     raeburn  13139:     my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.':'.$three.' in '.$url.
1.443     albertel 13140:                          ($start?', '.&mt('starting').' '.localtime($start):'').
                   13141:                          ($end?', ending '.localtime($end):'').': <b>'.
                   13142:               &Apache::lonnet::assigncustomrole(
1.664     raeburn  13143:                  $udom,$uname,$url,$three,$four,$five,$end,$start,undef,undef,$context).
1.443     albertel 13144:                  '</b><br />';
                   13145:     return $output;
                   13146: }
                   13147: 
                   13148: sub commit_standardrole {
1.541     raeburn  13149:     my ($udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
                   13150:     my ($output,$logmsg,$linefeed);
                   13151:     if ($context eq 'auto') {
                   13152:         $linefeed = "\n";
                   13153:     } else {
                   13154:         $linefeed = "<br />\n";
                   13155:     }  
1.443     albertel 13156:     if ($three eq 'st') {
1.541     raeburn  13157:         my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$three,$start,$end,
                   13158:                                          $one,$two,$sec,$context);
                   13159:         if (($result =~ /^error/) || ($result eq 'not_in_class') || 
1.626     raeburn  13160:             ($result eq 'unknown_course') || ($result eq 'refused')) {
                   13161:             $output = $logmsg.' '.&mt('Error: ').$result."\n"; 
1.443     albertel 13162:         } else {
1.541     raeburn  13163:             $output = $logmsg.$linefeed.&mt('Assigning').' '.$three.' in '.$url.
1.443     albertel 13164:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  13165:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
                   13166:             if ($context eq 'auto') {
                   13167:                 $output .= $result.$linefeed.&mt('Add to classlist').': ok';
                   13168:             } else {
                   13169:                $output .= '<b>'.$result.'</b>'.$linefeed.
                   13170:                &mt('Add to classlist').': <b>ok</b>';
                   13171:             }
                   13172:             $output .= $linefeed;
1.443     albertel 13173:         }
                   13174:     } else {
                   13175:         $output = &mt('Assigning').' '.$three.' in '.$url.
                   13176:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  13177:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
1.652     raeburn  13178:         my $result = &Apache::lonnet::assignrole($udom,$uname,$url,$three,$end,$start,'','',$context);
1.541     raeburn  13179:         if ($context eq 'auto') {
                   13180:             $output .= $result.$linefeed;
                   13181:         } else {
                   13182:             $output .= '<b>'.$result.'</b>'.$linefeed;
                   13183:         }
1.443     albertel 13184:     }
                   13185:     return $output;
                   13186: }
                   13187: 
                   13188: sub commit_studentrole {
1.541     raeburn  13189:     my ($logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
1.626     raeburn  13190:     my ($result,$linefeed,$oldsecurl,$newsecurl);
1.541     raeburn  13191:     if ($context eq 'auto') {
                   13192:         $linefeed = "\n";
                   13193:     } else {
                   13194:         $linefeed = '<br />'."\n";
                   13195:     }
1.443     albertel 13196:     if (defined($one) && defined($two)) {
                   13197:         my $cid=$one.'_'.$two;
                   13198:         my $oldsec=&Apache::lonnet::getsection($udom,$uname,$cid);
                   13199:         my $secchange = 0;
                   13200:         my $expire_role_result;
                   13201:         my $modify_section_result;
1.628     raeburn  13202:         if ($oldsec ne '-1') { 
                   13203:             if ($oldsec ne $sec) {
1.443     albertel 13204:                 $secchange = 1;
1.628     raeburn  13205:                 my $now = time;
1.443     albertel 13206:                 my $uurl='/'.$cid;
                   13207:                 $uurl=~s/\_/\//g;
                   13208:                 if ($oldsec) {
                   13209:                     $uurl.='/'.$oldsec;
                   13210:                 }
1.626     raeburn  13211:                 $oldsecurl = $uurl;
1.628     raeburn  13212:                 $expire_role_result = 
1.652     raeburn  13213:                     &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',$now,'','',$context);
1.628     raeburn  13214:                 if ($env{'request.course.sec'} ne '') { 
                   13215:                     if ($expire_role_result eq 'refused') {
                   13216:                         my @roles = ('st');
                   13217:                         my @statuses = ('previous');
                   13218:                         my @roledoms = ($one);
                   13219:                         my $withsec = 1;
                   13220:                         my %roleshash = 
                   13221:                             &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   13222:                                               \@statuses,\@roles,\@roledoms,$withsec);
                   13223:                         if (defined ($roleshash{$two.':'.$one.':st:'.$oldsec})) {
                   13224:                             my ($oldstart,$oldend) = 
                   13225:                                 split(':',$roleshash{$two.':'.$one.':st:'.$oldsec});
                   13226:                             if ($oldend > 0 && $oldend <= $now) {
                   13227:                                 $expire_role_result = 'ok';
                   13228:                             }
                   13229:                         }
                   13230:                     }
                   13231:                 }
1.443     albertel 13232:                 $result = $expire_role_result;
                   13233:             }
                   13234:         }
                   13235:         if (($expire_role_result eq 'ok') || ($secchange == 0)) {
1.652     raeburn  13236:             $modify_section_result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,'','',$cid,'',$context);
1.443     albertel 13237:             if ($modify_section_result =~ /^ok/) {
                   13238:                 if ($secchange == 1) {
1.628     raeburn  13239:                     if ($sec eq '') {
                   13240:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to student role without a section.',$uname,$oldsec).$linefeed;
                   13241:                     } else {
                   13242:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to new section: [_3].',$uname,$oldsec,$sec).$linefeed;
                   13243:                     }
1.443     albertel 13244:                 } elsif ($oldsec eq '-1') {
1.628     raeburn  13245:                     if ($sec eq '') {
                   13246:                         $$logmsg .= &mt('New student role without a section for [_1] in course [_2].',$uname,$cid).$linefeed;
                   13247:                     } else {
                   13248:                         $$logmsg .= &mt('New student role for [_1] in section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
                   13249:                     }
1.443     albertel 13250:                 } else {
1.628     raeburn  13251:                     if ($sec eq '') {
                   13252:                         $$logmsg .= &mt('Student [_1] assigned to course [_2] without a section.',$uname,$cid).$linefeed;
                   13253:                     } else {
                   13254:                         $$logmsg .= &mt('Student [_1] assigned to section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
                   13255:                     }
1.443     albertel 13256:                 }
                   13257:             } else {
1.628     raeburn  13258:                 if ($secchange) {       
                   13259:                     $$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;
                   13260:                 } else {
                   13261:                     $$logmsg .= &mt('Error when attempting to modify role for [_1] for section: "[_2]" in course [_3] -error:',$uname,$sec,$cid).' '.$modify_section_result.$linefeed;
                   13262:                 }
1.443     albertel 13263:             }
                   13264:             $result = $modify_section_result;
                   13265:         } elsif ($secchange == 1) {
1.628     raeburn  13266:             if ($oldsec eq '') {
1.1075.2.20! raeburn  13267:                 $$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  13268:             } else {
                   13269:                 $$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;
                   13270:             }
1.626     raeburn  13271:             if ($expire_role_result eq 'refused') {
                   13272:                 my $newsecurl = '/'.$cid;
                   13273:                 $newsecurl =~ s/\_/\//g;
                   13274:                 if ($sec ne '') {
                   13275:                     $newsecurl.='/'.$sec;
                   13276:                 }
                   13277:                 if (&Apache::lonnet::allowed('cst',$newsecurl) && !(&Apache::lonnet::allowed('cst',$oldsecurl))) {
                   13278:                     if ($sec eq '') {
                   13279:                         $$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;
                   13280:                     } else {
                   13281:                         $$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;
                   13282:                     }
                   13283:                 }
                   13284:             }
1.443     albertel 13285:         }
                   13286:     } else {
1.626     raeburn  13287:         $$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 13288:         $result = "error: incomplete course id\n";
                   13289:     }
                   13290:     return $result;
                   13291: }
                   13292: 
                   13293: ############################################################
                   13294: ############################################################
                   13295: 
1.566     albertel 13296: sub check_clone {
1.578     raeburn  13297:     my ($args,$linefeed) = @_;
1.566     albertel 13298:     my $cloneid='/'.$args->{'clonedomain'}.'/'.$args->{'clonecourse'};
                   13299:     my ($clonecrsudom,$clonecrsunum)= &LONCAPA::split_courseid($cloneid);
                   13300:     my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
                   13301:     my $clonemsg;
                   13302:     my $can_clone = 0;
1.944     raeburn  13303:     my $lctype = lc($args->{'crstype'});
1.908     raeburn  13304:     if ($lctype ne 'community') {
                   13305:         $lctype = 'course';
                   13306:     }
1.566     albertel 13307:     if ($clonehome eq 'no_host') {
1.944     raeburn  13308:         if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  13309:             $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'});
                   13310:         } else {
                   13311:             $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'});
                   13312:         }     
1.566     albertel 13313:     } else {
                   13314: 	my %clonedesc = &Apache::lonnet::coursedescription($cloneid,{'one_time' => 1});
1.944     raeburn  13315:         if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  13316:             if ($clonedesc{'type'} ne 'Community') {
                   13317:                  $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'});
                   13318:                 return ($can_clone, $clonemsg, $cloneid, $clonehome);
                   13319:             }
                   13320:         }
1.882     raeburn  13321: 	if (($env{'request.role.domain'} eq $args->{'clonedomain'}) && 
                   13322:             (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'}))) {
1.566     albertel 13323: 	    $can_clone = 1;
                   13324: 	} else {
                   13325: 	    my %clonehash = &Apache::lonnet::get('environment',['cloners'],
                   13326: 						 $args->{'clonedomain'},$args->{'clonecourse'});
                   13327: 	    my @cloners = split(/,/,$clonehash{'cloners'});
1.578     raeburn  13328:             if (grep(/^\*$/,@cloners)) {
                   13329:                 $can_clone = 1;
                   13330:             } elsif (grep(/^\*\:\Q$args->{'ccdomain'}\E$/,@cloners)) {
                   13331:                 $can_clone = 1;
                   13332:             } else {
1.908     raeburn  13333:                 my $ccrole = 'cc';
1.944     raeburn  13334:                 if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  13335:                     $ccrole = 'co';
                   13336:                 }
1.578     raeburn  13337: 	        my %roleshash =
                   13338: 		    &Apache::lonnet::get_my_roles($args->{'ccuname'},
                   13339: 					 $args->{'ccdomain'},
1.908     raeburn  13340:                                          'userroles',['active'],[$ccrole],
1.578     raeburn  13341: 					 [$args->{'clonedomain'}]);
1.908     raeburn  13342: 	        if (($roleshash{$args->{'clonecourse'}.':'.$args->{'clonedomain'}.':'.$ccrole}) || (grep(/^\Q$args->{'ccuname'}\E:\Q$args->{'ccdomain'}\E$/,@cloners))) {
1.942     raeburn  13343:                     $can_clone = 1;
                   13344:                 } elsif (&Apache::lonnet::is_course_owner($args->{'clonedomain'},$args->{'clonecourse'},$args->{'ccuname'},$args->{'ccdomain'})) {
                   13345:                     $can_clone = 1;
                   13346:                 } else {
1.944     raeburn  13347:                     if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  13348:                         $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'});
                   13349:                     } else {
                   13350:                         $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'});
                   13351:                     }
1.578     raeburn  13352: 	        }
1.566     albertel 13353: 	    }
1.578     raeburn  13354:         }
1.566     albertel 13355:     }
                   13356:     return ($can_clone, $clonemsg, $cloneid, $clonehome);
                   13357: }
                   13358: 
1.444     albertel 13359: sub construct_course {
1.885     raeburn  13360:     my ($args,$logmsg,$courseid,$crsudom,$crsunum,$udom,$uname,$context,$cnum,$category) = @_;
1.444     albertel 13361:     my $outcome;
1.541     raeburn  13362:     my $linefeed =  '<br />'."\n";
                   13363:     if ($context eq 'auto') {
                   13364:         $linefeed = "\n";
                   13365:     }
1.566     albertel 13366: 
                   13367: #
                   13368: # Are we cloning?
                   13369: #
                   13370:     my ($can_clone, $clonemsg, $cloneid, $clonehome);
                   13371:     if (($args->{'clonecourse'}) && ($args->{'clonedomain'})) {
1.578     raeburn  13372: 	($can_clone, $clonemsg, $cloneid, $clonehome) = &check_clone($args,$linefeed);
1.566     albertel 13373: 	if ($context ne 'auto') {
1.578     raeburn  13374:             if ($clonemsg ne '') {
                   13375: 	        $clonemsg = '<span class="LC_error">'.$clonemsg.'</span>';
                   13376:             }
1.566     albertel 13377: 	}
                   13378: 	$outcome .= $clonemsg.$linefeed;
                   13379: 
                   13380:         if (!$can_clone) {
                   13381: 	    return (0,$outcome);
                   13382: 	}
                   13383:     }
                   13384: 
1.444     albertel 13385: #
                   13386: # Open course
                   13387: #
                   13388:     my $crstype = lc($args->{'crstype'});
                   13389:     my %cenv=();
                   13390:     $$courseid=&Apache::lonnet::createcourse($args->{'course_domain'},
                   13391:                                              $args->{'cdescr'},
                   13392:                                              $args->{'curl'},
                   13393:                                              $args->{'course_home'},
                   13394:                                              $args->{'nonstandard'},
                   13395:                                              $args->{'crscode'},
                   13396:                                              $args->{'ccuname'}.':'.
                   13397:                                              $args->{'ccdomain'},
1.882     raeburn  13398:                                              $args->{'crstype'},
1.885     raeburn  13399:                                              $cnum,$context,$category);
1.444     albertel 13400: 
                   13401:     # Note: The testing routines depend on this being output; see 
                   13402:     # Utils::Course. This needs to at least be output as a comment
                   13403:     # if anyone ever decides to not show this, and Utils::Course::new
                   13404:     # will need to be suitably modified.
1.541     raeburn  13405:     $outcome .= &mt('New LON-CAPA [_1] ID: [_2]',$crstype,$$courseid).$linefeed;
1.943     raeburn  13406:     if ($$courseid =~ /^error:/) {
                   13407:         return (0,$outcome);
                   13408:     }
                   13409: 
1.444     albertel 13410: #
                   13411: # Check if created correctly
                   13412: #
1.479     albertel 13413:     ($$crsudom,$$crsunum)= &LONCAPA::split_courseid($$courseid);
1.444     albertel 13414:     my $crsuhome=&Apache::lonnet::homeserver($$crsunum,$$crsudom);
1.943     raeburn  13415:     if ($crsuhome eq 'no_host') {
                   13416:         $outcome .= &mt('Course creation failed, unrecognized course home server.').$linefeed;
                   13417:         return (0,$outcome);
                   13418:     }
1.541     raeburn  13419:     $outcome .= &mt('Created on').': '.$crsuhome.$linefeed;
1.566     albertel 13420: 
1.444     albertel 13421: #
1.566     albertel 13422: # Do the cloning
                   13423: #   
                   13424:     if ($can_clone && $cloneid) {
                   13425: 	$clonemsg = &mt('Cloning [_1] from [_2]',$crstype,$clonehome);
                   13426: 	if ($context ne 'auto') {
                   13427: 	    $clonemsg = '<span class="LC_success">'.$clonemsg.'</span>';
                   13428: 	}
                   13429: 	$outcome .= $clonemsg.$linefeed;
                   13430: 	my %oldcenv=&Apache::lonnet::dump('environment',$$crsudom,$$crsunum);
1.444     albertel 13431: # Copy all files
1.637     www      13432: 	&Apache::lonclonecourse::copycoursefiles($cloneid,$$courseid,$args->{'datemode'},$args->{'dateshift'});
1.444     albertel 13433: # Restore URL
1.566     albertel 13434: 	$cenv{'url'}=$oldcenv{'url'};
1.444     albertel 13435: # Restore title
1.566     albertel 13436: 	$cenv{'description'}=$oldcenv{'description'};
1.955     raeburn  13437: # Restore creation date, creator and creation context.
                   13438:         $cenv{'internal.created'}=$oldcenv{'internal.created'};
                   13439:         $cenv{'internal.creator'}=$oldcenv{'internal.creator'};
                   13440:         $cenv{'internal.creationcontext'}=$oldcenv{'internal.creationcontext'};
1.444     albertel 13441: # Mark as cloned
1.566     albertel 13442: 	$cenv{'clonedfrom'}=$cloneid;
1.638     www      13443: # Need to clone grading mode
                   13444:         my %newenv=&Apache::lonnet::get('environment',['grading'],$$crsudom,$$crsunum);
                   13445:         $cenv{'grading'}=$newenv{'grading'};
                   13446: # Do not clone these environment entries
                   13447:         &Apache::lonnet::del('environment',
                   13448:                   ['default_enrollment_start_date',
                   13449:                    'default_enrollment_end_date',
                   13450:                    'question.email',
                   13451:                    'policy.email',
                   13452:                    'comment.email',
                   13453:                    'pch.users.denied',
1.725     raeburn  13454:                    'plc.users.denied',
                   13455:                    'hidefromcat',
                   13456:                    'categories'],
1.638     www      13457:                    $$crsudom,$$crsunum);
1.444     albertel 13458:     }
1.566     albertel 13459: 
1.444     albertel 13460: #
                   13461: # Set environment (will override cloned, if existing)
                   13462: #
                   13463:     my @sections = ();
                   13464:     my @xlists = ();
                   13465:     if ($args->{'crstype'}) {
                   13466:         $cenv{'type'}=$args->{'crstype'};
                   13467:     }
                   13468:     if ($args->{'crsid'}) {
                   13469:         $cenv{'courseid'}=$args->{'crsid'};
                   13470:     }
                   13471:     if ($args->{'crscode'}) {
                   13472:         $cenv{'internal.coursecode'}=$args->{'crscode'};
                   13473:     }
                   13474:     if ($args->{'crsquota'} ne '') {
                   13475:         $cenv{'internal.coursequota'}=$args->{'crsquota'};
                   13476:     } else {
                   13477:         $cenv{'internal.coursequota'}=$args->{'crsquota'} = 20;
                   13478:     }
                   13479:     if ($args->{'ccuname'}) {
                   13480:         $cenv{'internal.courseowner'} = $args->{'ccuname'}.
                   13481:                                         ':'.$args->{'ccdomain'};
                   13482:     } else {
                   13483:         $cenv{'internal.courseowner'} = $args->{'curruser'};
                   13484:     }
                   13485:     my @badclasses = (); # Used to accumulate sections/crosslistings that did not pass classlist access check for course owner.
                   13486:     if ($args->{'crssections'}) {
                   13487:         $cenv{'internal.sectionnums'} = '';
                   13488:         if ($args->{'crssections'} =~ m/,/) {
                   13489:             @sections = split/,/,$args->{'crssections'};
                   13490:         } else {
                   13491:             $sections[0] = $args->{'crssections'};
                   13492:         }
                   13493:         if (@sections > 0) {
                   13494:             foreach my $item (@sections) {
                   13495:                 my ($sec,$gp) = split/:/,$item;
                   13496:                 my $class = $args->{'crscode'}.$sec;
                   13497:                 my $addcheck = &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$class,$cenv{'internal.courseowner'});
                   13498:                 $cenv{'internal.sectionnums'} .= $item.',';
                   13499:                 unless ($addcheck eq 'ok') {
                   13500:                     push @badclasses, $class;
                   13501:                 }
                   13502:             }
                   13503:             $cenv{'internal.sectionnums'} =~ s/,$//;
                   13504:         }
                   13505:     }
                   13506: # do not hide course coordinator from staff listing, 
                   13507: # even if privileged
                   13508:     $cenv{'nothideprivileged'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   13509: # add crosslistings
                   13510:     if ($args->{'crsxlist'}) {
                   13511:         $cenv{'internal.crosslistings'}='';
                   13512:         if ($args->{'crsxlist'} =~ m/,/) {
                   13513:             @xlists = split/,/,$args->{'crsxlist'};
                   13514:         } else {
                   13515:             $xlists[0] = $args->{'crsxlist'};
                   13516:         }
                   13517:         if (@xlists > 0) {
                   13518:             foreach my $item (@xlists) {
                   13519:                 my ($xl,$gp) = split/:/,$item;
                   13520:                 my $addcheck =  &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$xl,$cenv{'internal.courseowner'});
                   13521:                 $cenv{'internal.crosslistings'} .= $item.',';
                   13522:                 unless ($addcheck eq 'ok') {
                   13523:                     push @badclasses, $xl;
                   13524:                 }
                   13525:             }
                   13526:             $cenv{'internal.crosslistings'} =~ s/,$//;
                   13527:         }
                   13528:     }
                   13529:     if ($args->{'autoadds'}) {
                   13530:         $cenv{'internal.autoadds'}=$args->{'autoadds'};
                   13531:     }
                   13532:     if ($args->{'autodrops'}) {
                   13533:         $cenv{'internal.autodrops'}=$args->{'autodrops'};
                   13534:     }
                   13535: # check for notification of enrollment changes
                   13536:     my @notified = ();
                   13537:     if ($args->{'notify_owner'}) {
                   13538:         if ($args->{'ccuname'} ne '') {
                   13539:             push(@notified,$args->{'ccuname'}.':'.$args->{'ccdomain'});
                   13540:         }
                   13541:     }
                   13542:     if ($args->{'notify_dc'}) {
                   13543:         if ($uname ne '') { 
1.630     raeburn  13544:             push(@notified,$uname.':'.$udom);
1.444     albertel 13545:         }
                   13546:     }
                   13547:     if (@notified > 0) {
                   13548:         my $notifylist;
                   13549:         if (@notified > 1) {
                   13550:             $notifylist = join(',',@notified);
                   13551:         } else {
                   13552:             $notifylist = $notified[0];
                   13553:         }
                   13554:         $cenv{'internal.notifylist'} = $notifylist;
                   13555:     }
                   13556:     if (@badclasses > 0) {
                   13557:         my %lt=&Apache::lonlocal::texthash(
                   13558:                 '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',
                   13559:                 'dnhr' => 'does not have rights to access enrollment in these classes',
                   13560:                 'adby' => 'as determined by the policies of your institution on access to official classlists'
                   13561:         );
1.541     raeburn  13562:         my $badclass_msg = $cenv{'internal.courseowner'}.') - '.$lt{'dnhr'}.
                   13563:                            ' ('.$lt{'adby'}.')';
                   13564:         if ($context eq 'auto') {
                   13565:             $outcome .= $badclass_msg.$linefeed;
1.566     albertel 13566:             $outcome .= '<div class="LC_warning">'.$badclass_msg.$linefeed.'<ul>'."\n";
1.541     raeburn  13567:             foreach my $item (@badclasses) {
                   13568:                 if ($context eq 'auto') {
                   13569:                     $outcome .= " - $item\n";
                   13570:                 } else {
                   13571:                     $outcome .= "<li>$item</li>\n";
                   13572:                 }
                   13573:             }
                   13574:             if ($context eq 'auto') {
                   13575:                 $outcome .= $linefeed;
                   13576:             } else {
1.566     albertel 13577:                 $outcome .= "</ul><br /><br /></div>\n";
1.541     raeburn  13578:             }
                   13579:         } 
1.444     albertel 13580:     }
                   13581:     if ($args->{'no_end_date'}) {
                   13582:         $args->{'endaccess'} = 0;
                   13583:     }
                   13584:     $cenv{'internal.autostart'}=$args->{'enrollstart'};
                   13585:     $cenv{'internal.autoend'}=$args->{'enrollend'};
                   13586:     $cenv{'default_enrollment_start_date'}=$args->{'startaccess'};
                   13587:     $cenv{'default_enrollment_end_date'}=$args->{'endaccess'};
                   13588:     if ($args->{'showphotos'}) {
                   13589:       $cenv{'internal.showphotos'}=$args->{'showphotos'};
                   13590:     }
                   13591:     $cenv{'internal.authtype'} = $args->{'authtype'};
                   13592:     $cenv{'internal.autharg'} = $args->{'autharg'}; 
                   13593:     if ( ($cenv{'internal.authtype'} =~ /^krb/) && ($cenv{'internal.autoadds'} == 1)) {
                   13594:         if (! defined($cenv{'internal.autharg'}) || $cenv{'internal.autharg'}  eq '') {
1.541     raeburn  13595:             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'); 
                   13596:             if ($context eq 'auto') {
                   13597:                 $outcome .= $krb_msg;
                   13598:             } else {
1.566     albertel 13599:                 $outcome .= '<span class="LC_error">'.$krb_msg.'</span>';
1.541     raeburn  13600:             }
                   13601:             $outcome .= $linefeed;
1.444     albertel 13602:         }
                   13603:     }
                   13604:     if (($args->{'ccdomain'}) && ($args->{'ccuname'})) {
                   13605:        if ($args->{'setpolicy'}) {
                   13606:            $cenv{'policy.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   13607:        }
                   13608:        if ($args->{'setcontent'}) {
                   13609:            $cenv{'question.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   13610:        }
                   13611:     }
                   13612:     if ($args->{'reshome'}) {
                   13613: 	$cenv{'reshome'}=$args->{'reshome'}.'/';
                   13614: 	$cenv{'reshome'}=~s/\/+$/\//;
                   13615:     }
                   13616: #
                   13617: # course has keyed access
                   13618: #
                   13619:     if ($args->{'setkeys'}) {
                   13620:        $cenv{'keyaccess'}='yes';
                   13621:     }
                   13622: # if specified, key authority is not course, but user
                   13623: # only active if keyaccess is yes
                   13624:     if ($args->{'keyauth'}) {
1.487     albertel 13625: 	my ($user,$domain) = split(':',$args->{'keyauth'});
                   13626: 	$user = &LONCAPA::clean_username($user);
                   13627: 	$domain = &LONCAPA::clean_username($domain);
1.488     foxr     13628: 	if ($user ne '' && $domain ne '') {
1.487     albertel 13629: 	    $cenv{'keyauth'}=$user.':'.$domain;
1.444     albertel 13630: 	}
                   13631:     }
                   13632: 
                   13633:     if ($args->{'disresdis'}) {
                   13634:         $cenv{'pch.roles.denied'}='st';
                   13635:     }
                   13636:     if ($args->{'disablechat'}) {
                   13637:         $cenv{'plc.roles.denied'}='st';
                   13638:     }
                   13639: 
                   13640:     # Record we've not yet viewed the Course Initialization Helper for this 
                   13641:     # course
                   13642:     $cenv{'course.helper.not.run'} = 1;
                   13643:     #
                   13644:     # Use new Randomseed
                   13645:     #
                   13646:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
                   13647:     $cenv{'receiptalg'}=&Apache::lonnet::latest_receipt_algorithm_id();;
                   13648:     #
                   13649:     # The encryption code and receipt prefix for this course
                   13650:     #
                   13651:     $cenv{'internal.encseed'}=$Apache::lonnet::perlvar{'lonReceipt'}.$$.time.int(rand(9999));
                   13652:     $cenv{'internal.encpref'}=100+int(9*rand(99));
                   13653:     #
                   13654:     # By default, use standard grading
                   13655:     if (!defined($cenv{'grading'})) { $cenv{'grading'} = 'standard'; }
                   13656: 
1.541     raeburn  13657:     $outcome .= $linefeed.&mt('Setting environment').': '.                 
                   13658:           &Apache::lonnet::put('environment',\%cenv,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 13659: #
                   13660: # Open all assignments
                   13661: #
                   13662:     if ($args->{'openall'}) {
                   13663:        my $storeunder=$$crsudom.'_'.$$crsunum.'.0.opendate';
                   13664:        my %storecontent = ($storeunder         => time,
                   13665:                            $storeunder.'.type' => 'date_start');
                   13666:        
                   13667:        $outcome .= &mt('Opening all assignments').': '.&Apache::lonnet::cput
1.541     raeburn  13668:                  ('resourcedata',\%storecontent,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 13669:    }
                   13670: #
                   13671: # Set first page
                   13672: #
                   13673:     unless (($args->{'nonstandard'}) || ($args->{'firstres'} eq 'blank')
                   13674: 	    || ($cloneid)) {
1.445     albertel 13675: 	use LONCAPA::map;
1.444     albertel 13676: 	$outcome .= &mt('Setting first resource').': ';
1.445     albertel 13677: 
                   13678: 	my $map = '/uploaded/'.$$crsudom.'/'.$$crsunum.'/default.sequence';
                   13679:         my ($errtext,$fatal)=&LONCAPA::map::mapread($map);
                   13680: 
1.444     albertel 13681:         $outcome .= ($fatal?$errtext:'read ok').' - ';
                   13682:         my $title; my $url;
                   13683:         if ($args->{'firstres'} eq 'syl') {
1.690     bisitz   13684: 	    $title=&mt('Syllabus');
1.444     albertel 13685:             $url='/public/'.$$crsudom.'/'.$$crsunum.'/syllabus';
                   13686:         } else {
1.963     raeburn  13687:             $title=&mt('Table of Contents');
1.444     albertel 13688:             $url='/adm/navmaps';
                   13689:         }
1.445     albertel 13690: 
                   13691:         $LONCAPA::map::resources[1]=$title.':'.$url.':false:start:res';
                   13692: 	(my $outtext,$errtext) = &LONCAPA::map::storemap($map,1);
                   13693: 
                   13694: 	if ($errtext) { $fatal=2; }
1.541     raeburn  13695:         $outcome .= ($fatal?$errtext:'write ok').$linefeed;
1.444     albertel 13696:     }
1.566     albertel 13697: 
                   13698:     return (1,$outcome);
1.444     albertel 13699: }
                   13700: 
                   13701: ############################################################
                   13702: ############################################################
                   13703: 
1.953     droeschl 13704: #SD
                   13705: # only Community and Course, or anything else?
1.378     raeburn  13706: sub course_type {
                   13707:     my ($cid) = @_;
                   13708:     if (!defined($cid)) {
                   13709:         $cid = $env{'request.course.id'};
                   13710:     }
1.404     albertel 13711:     if (defined($env{'course.'.$cid.'.type'})) {
                   13712:         return $env{'course.'.$cid.'.type'};
1.378     raeburn  13713:     } else {
                   13714:         return 'Course';
1.377     raeburn  13715:     }
                   13716: }
1.156     albertel 13717: 
1.406     raeburn  13718: sub group_term {
                   13719:     my $crstype = &course_type();
                   13720:     my %names = (
                   13721:                   'Course' => 'group',
1.865     raeburn  13722:                   'Community' => 'group',
1.406     raeburn  13723:                 );
                   13724:     return $names{$crstype};
                   13725: }
                   13726: 
1.902     raeburn  13727: sub course_types {
                   13728:     my @types = ('official','unofficial','community');
                   13729:     my %typename = (
                   13730:                          official   => 'Official course',
                   13731:                          unofficial => 'Unofficial course',
                   13732:                          community  => 'Community',
                   13733:                    );
                   13734:     return (\@types,\%typename);
                   13735: }
                   13736: 
1.156     albertel 13737: sub icon {
                   13738:     my ($file)=@_;
1.505     albertel 13739:     my $curfext = lc((split(/\./,$file))[-1]);
1.168     albertel 13740:     my $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/unknown.gif';
1.156     albertel 13741:     my $embstyle = &Apache::loncommon::fileembstyle($curfext);
1.168     albertel 13742:     if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) {
                   13743: 	if (-e  $Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
                   13744: 	          $Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   13745: 	            $curfext.".gif") {
                   13746: 	    $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   13747: 		$curfext.".gif";
                   13748: 	}
                   13749:     }
1.249     albertel 13750:     return &lonhttpdurl($iconname);
1.154     albertel 13751: } 
1.84      albertel 13752: 
1.575     albertel 13753: sub lonhttpdurl {
1.692     www      13754: #
                   13755: # Had been used for "small fry" static images on separate port 8080.
                   13756: # Modify here if lightweight http functionality desired again.
                   13757: # Currently eliminated due to increasing firewall issues.
                   13758: #
1.575     albertel 13759:     my ($url)=@_;
1.692     www      13760:     return $url;
1.215     albertel 13761: }
                   13762: 
1.213     albertel 13763: sub connection_aborted {
                   13764:     my ($r)=@_;
                   13765:     $r->print(" ");$r->rflush();
                   13766:     my $c = $r->connection;
                   13767:     return $c->aborted();
                   13768: }
                   13769: 
1.221     foxr     13770: #    Escapes strings that may have embedded 's that will be put into
1.222     foxr     13771: #    strings as 'strings'.
                   13772: sub escape_single {
1.221     foxr     13773:     my ($input) = @_;
1.223     albertel 13774:     $input =~ s/\\/\\\\/g;	# Escape the \'s..(must be first)>
1.221     foxr     13775:     $input =~ s/\'/\\\'/g;	# Esacpe the 's....
                   13776:     return $input;
                   13777: }
1.223     albertel 13778: 
1.222     foxr     13779: #  Same as escape_single, but escape's "'s  This 
                   13780: #  can be used for  "strings"
                   13781: sub escape_double {
                   13782:     my ($input) = @_;
                   13783:     $input =~ s/\\/\\\\/g;	# Escape the /'s..(must be first)>
                   13784:     $input =~ s/\"/\\\"/g;	# Esacpe the "s....
                   13785:     return $input;
                   13786: }
1.223     albertel 13787:  
1.222     foxr     13788: #   Escapes the last element of a full URL.
                   13789: sub escape_url {
                   13790:     my ($url)   = @_;
1.238     raeburn  13791:     my @urlslices = split(/\//, $url,-1);
1.369     www      13792:     my $lastitem = &escape(pop(@urlslices));
1.223     albertel 13793:     return join('/',@urlslices).'/'.$lastitem;
1.222     foxr     13794: }
1.462     albertel 13795: 
1.820     raeburn  13796: sub compare_arrays {
                   13797:     my ($arrayref1,$arrayref2) = @_;
                   13798:     my (@difference,%count);
                   13799:     @difference = ();
                   13800:     %count = ();
                   13801:     if ((ref($arrayref1) eq 'ARRAY') && (ref($arrayref2) eq 'ARRAY')) {
                   13802:         foreach my $element (@{$arrayref1}, @{$arrayref2}) { $count{$element}++; }
                   13803:         foreach my $element (keys(%count)) {
                   13804:             if ($count{$element} == 1) {
                   13805:                 push(@difference,$element);
                   13806:             }
                   13807:         }
                   13808:     }
                   13809:     return @difference;
                   13810: }
                   13811: 
1.817     bisitz   13812: # -------------------------------------------------------- Initialize user login
1.462     albertel 13813: sub init_user_environment {
1.463     albertel 13814:     my ($r, $username, $domain, $authhost, $form, $args) = @_;
1.462     albertel 13815:     my $lonids=$Apache::lonnet::perlvar{'lonIDsDir'};
                   13816: 
                   13817:     my $public=($username eq 'public' && $domain eq 'public');
                   13818: 
                   13819: # See if old ID present, if so, remove
                   13820: 
1.1062    raeburn  13821:     my ($filename,$cookie,$userroles,$firstaccenv,$timerintenv);
1.462     albertel 13822:     my $now=time;
                   13823: 
                   13824:     if ($public) {
                   13825: 	my $max_public=100;
                   13826: 	my $oldest;
                   13827: 	my $oldest_time=0;
                   13828: 	for(my $next=1;$next<=$max_public;$next++) {
                   13829: 	    if (-e $lonids."/publicuser_$next.id") {
                   13830: 		my $mtime=(stat($lonids."/publicuser_$next.id"))[9];
                   13831: 		if ($mtime<$oldest_time || !$oldest_time) {
                   13832: 		    $oldest_time=$mtime;
                   13833: 		    $oldest=$next;
                   13834: 		}
                   13835: 	    } else {
                   13836: 		$cookie="publicuser_$next";
                   13837: 		last;
                   13838: 	    }
                   13839: 	}
                   13840: 	if (!$cookie) { $cookie="publicuser_$oldest"; }
                   13841:     } else {
1.463     albertel 13842: 	# if this isn't a robot, kill any existing non-robot sessions
                   13843: 	if (!$args->{'robot'}) {
                   13844: 	    opendir(DIR,$lonids);
                   13845: 	    while ($filename=readdir(DIR)) {
                   13846: 		if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
                   13847: 		    unlink($lonids.'/'.$filename);
                   13848: 		}
1.462     albertel 13849: 	    }
1.463     albertel 13850: 	    closedir(DIR);
1.462     albertel 13851: 	}
                   13852: # Give them a new cookie
1.463     albertel 13853: 	my $id = ($args->{'robot'} ? 'robot'.$args->{'robot'}
1.684     www      13854: 		                   : $now.$$.int(rand(10000)));
1.463     albertel 13855: 	$cookie="$username\_$id\_$domain\_$authhost";
1.462     albertel 13856:     
                   13857: # Initialize roles
                   13858: 
1.1062    raeburn  13859: 	($userroles,$firstaccenv,$timerintenv) = 
                   13860:             &Apache::lonnet::rolesinit($domain,$username,$authhost);
1.462     albertel 13861:     }
                   13862: # ------------------------------------ Check browser type and MathML capability
                   13863: 
                   13864:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                   13865:         $clientunicode,$clientos) = &decode_user_agent($r);
                   13866: 
                   13867: # ------------------------------------------------------------- Get environment
                   13868: 
                   13869:     my %userenv = &Apache::lonnet::dump('environment',$domain,$username);
                   13870:     my ($tmp) = keys(%userenv);
                   13871:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   13872:     } else {
                   13873: 	undef(%userenv);
                   13874:     }
                   13875:     if (($userenv{'interface'}) && (!$form->{'interface'})) {
                   13876: 	$form->{'interface'}=$userenv{'interface'};
                   13877:     }
                   13878:     if ($userenv{'texengine'} eq 'ttm') { $clientmathml=1; }
                   13879: 
                   13880: # --------------- Do not trust query string to be put directly into environment
1.817     bisitz   13881:     foreach my $option ('interface','localpath','localres') {
                   13882:         $form->{$option}=~s/[\n\r\=]//gs;
1.462     albertel 13883:     }
                   13884: # --------------------------------------------------------- Write first profile
                   13885: 
                   13886:     {
                   13887: 	my %initial_env = 
                   13888: 	    ("user.name"          => $username,
                   13889: 	     "user.domain"        => $domain,
                   13890: 	     "user.home"          => $authhost,
                   13891: 	     "browser.type"       => $clientbrowser,
                   13892: 	     "browser.version"    => $clientversion,
                   13893: 	     "browser.mathml"     => $clientmathml,
                   13894: 	     "browser.unicode"    => $clientunicode,
                   13895: 	     "browser.os"         => $clientos,
                   13896: 	     "server.domain"      => $Apache::lonnet::perlvar{'lonDefDomain'},
                   13897: 	     "request.course.fn"  => '',
                   13898: 	     "request.course.uri" => '',
                   13899: 	     "request.course.sec" => '',
                   13900: 	     "request.role"       => 'cm',
                   13901: 	     "request.role.adv"   => $env{'user.adv'},
                   13902: 	     "request.host"       => $ENV{'REMOTE_ADDR'},);
                   13903: 
                   13904:         if ($form->{'localpath'}) {
                   13905: 	    $initial_env{"browser.localpath"}  = $form->{'localpath'};
                   13906: 	    $initial_env{"browser.localres"}   = $form->{'localres'};
                   13907:         }
                   13908: 	
                   13909: 	if ($form->{'interface'}) {
                   13910: 	    $form->{'interface'}=~s/\W//gs;
                   13911: 	    $initial_env{"browser.interface"} = $form->{'interface'};
                   13912: 	    $env{'browser.interface'}=$form->{'interface'};
                   13913: 	}
                   13914: 
1.981     raeburn  13915:         my %is_adv = ( is_adv => $env{'user.adv'} );
1.1016    raeburn  13916:         my %domdef;
                   13917:         unless ($domain eq 'public') {
                   13918:             %domdef = &Apache::lonnet::get_domain_defaults($domain);
                   13919:         }
1.980     raeburn  13920: 
1.1075.2.7  raeburn  13921:         foreach my $tool ('aboutme','blog','webdav','portfolio') {
1.724     raeburn  13922:             $userenv{'availabletools.'.$tool} = 
1.980     raeburn  13923:                 &Apache::lonnet::usertools_access($username,$domain,$tool,'reload',
                   13924:                                                   undef,\%userenv,\%domdef,\%is_adv);
1.724     raeburn  13925:         }
                   13926: 
1.864     raeburn  13927:         foreach my $crstype ('official','unofficial','community') {
1.765     raeburn  13928:             $userenv{'canrequest.'.$crstype} =
                   13929:                 &Apache::lonnet::usertools_access($username,$domain,$crstype,
1.980     raeburn  13930:                                                   'reload','requestcourses',
                   13931:                                                   \%userenv,\%domdef,\%is_adv);
1.765     raeburn  13932:         }
                   13933: 
1.1075.2.14  raeburn  13934:         $userenv{'canrequest.author'} =
                   13935:             &Apache::lonnet::usertools_access($username,$domain,'requestauthor',
                   13936:                                         'reload','requestauthor',
                   13937:                                         \%userenv,\%domdef,\%is_adv);
                   13938:         my %reqauthor = &Apache::lonnet::get('requestauthor',['author_status','author'],
                   13939:                                              $domain,$username);
                   13940:         my $reqstatus = $reqauthor{'author_status'};
                   13941:         if ($reqstatus eq 'approval' || $reqstatus eq 'approved') {
                   13942:             if (ref($reqauthor{'author'}) eq 'HASH') {
                   13943:                 $userenv{'requestauthorqueued'} = $reqstatus.':'.
                   13944:                                                   $reqauthor{'author'}{'timestamp'};
                   13945:             }
                   13946:         }
                   13947: 
1.462     albertel 13948: 	$env{'user.environment'} = "$lonids/$cookie.id";
1.1062    raeburn  13949: 
1.462     albertel 13950: 	if (tie(my %disk_env,'GDBM_File',"$lonids/$cookie.id",
                   13951: 		 &GDBM_WRCREAT(),0640)) {
                   13952: 	    &_add_to_env(\%disk_env,\%initial_env);
                   13953: 	    &_add_to_env(\%disk_env,\%userenv,'environment.');
                   13954: 	    &_add_to_env(\%disk_env,$userroles);
1.1062    raeburn  13955:             if (ref($firstaccenv) eq 'HASH') {
                   13956:                 &_add_to_env(\%disk_env,$firstaccenv);
                   13957:             }
                   13958:             if (ref($timerintenv) eq 'HASH') {
                   13959:                 &_add_to_env(\%disk_env,$timerintenv);
                   13960:             }
1.463     albertel 13961: 	    if (ref($args->{'extra_env'})) {
                   13962: 		&_add_to_env(\%disk_env,$args->{'extra_env'});
                   13963: 	    }
1.462     albertel 13964: 	    untie(%disk_env);
                   13965: 	} else {
1.705     tempelho 13966: 	    &Apache::lonnet::logthis("<span style=\"color:blue;\">WARNING: ".
                   13967: 			   'Could not create environment storage in lonauth: '.$!.'</span>');
1.462     albertel 13968: 	    return 'error: '.$!;
                   13969: 	}
                   13970:     }
                   13971:     $env{'request.role'}='cm';
                   13972:     $env{'request.role.adv'}=$env{'user.adv'};
                   13973:     $env{'browser.type'}=$clientbrowser;
                   13974: 
                   13975:     return $cookie;
                   13976: 
                   13977: }
                   13978: 
                   13979: sub _add_to_env {
                   13980:     my ($idf,$env_data,$prefix) = @_;
1.676     raeburn  13981:     if (ref($env_data) eq 'HASH') {
                   13982:         while (my ($key,$value) = each(%$env_data)) {
                   13983: 	    $idf->{$prefix.$key} = $value;
                   13984: 	    $env{$prefix.$key}   = $value;
                   13985:         }
1.462     albertel 13986:     }
                   13987: }
                   13988: 
1.685     tempelho 13989: # --- Get the symbolic name of a problem and the url
                   13990: sub get_symb {
                   13991:     my ($request,$silent) = @_;
1.726     raeburn  13992:     (my $url=$env{'form.url'}) =~ s-^https?\://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
1.685     tempelho 13993:     my $symb=($env{'form.symb'} ne '' ? $env{'form.symb'} : (&Apache::lonnet::symbread($url)));
                   13994:     if ($symb eq '') {
                   13995:         if (!$silent) {
1.1071    raeburn  13996:             if (ref($request)) { 
                   13997:                 $request->print("Unable to handle ambiguous references:$url:.");
                   13998:             }
1.685     tempelho 13999:             return ();
                   14000:         }
                   14001:     }
                   14002:     &Apache::lonenc::check_decrypt(\$symb);
                   14003:     return ($symb);
                   14004: }
                   14005: 
                   14006: # --------------------------------------------------------------Get annotation
                   14007: 
                   14008: sub get_annotation {
                   14009:     my ($symb,$enc) = @_;
                   14010: 
                   14011:     my $key = $symb;
                   14012:     if (!$enc) {
                   14013:         $key =
                   14014:             &Apache::lonnet::clutter((&Apache::lonnet::decode_symb($symb))[2]);
                   14015:     }
                   14016:     my %annotation=&Apache::lonnet::get('nohist_annotations',[$key]);
                   14017:     return $annotation{$key};
                   14018: }
                   14019: 
                   14020: sub clean_symb {
1.731     raeburn  14021:     my ($symb,$delete_enc) = @_;
1.685     tempelho 14022: 
                   14023:     &Apache::lonenc::check_decrypt(\$symb);
                   14024:     my $enc = $env{'request.enc'};
1.731     raeburn  14025:     if ($delete_enc) {
1.730     raeburn  14026:         delete($env{'request.enc'});
                   14027:     }
1.685     tempelho 14028: 
                   14029:     return ($symb,$enc);
                   14030: }
1.462     albertel 14031: 
1.990     raeburn  14032: sub build_release_hashes {
                   14033:     my ($checkparms,$checkresponsetypes,$checkcrstypes,$anonsurvey,$randomizetry) = @_;
                   14034:     return unless((ref($checkparms) eq 'HASH') && (ref($checkresponsetypes) eq 'HASH') &&
                   14035:                   (ref($checkcrstypes) eq 'HASH') && (ref($anonsurvey) eq 'HASH') &&
                   14036:                   (ref($randomizetry) eq 'HASH'));
                   14037:     foreach my $key (keys(%Apache::lonnet::needsrelease)) {
                   14038:         my ($item,$name,$value) = split(/:/,$key);
                   14039:         if ($item eq 'parameter') {
                   14040:             if (ref($checkparms->{$name}) eq 'ARRAY') {
                   14041:                 unless(grep(/^\Q$name\E$/,@{$checkparms->{$name}})) {
                   14042:                     push(@{$checkparms->{$name}},$value);
                   14043:                 }
                   14044:             } else {
                   14045:                 push(@{$checkparms->{$name}},$value);
                   14046:             }
                   14047:         } elsif ($item eq 'resourcetag') {
                   14048:             if ($name eq 'responsetype') {
                   14049:                 $checkresponsetypes->{$value} = $Apache::lonnet::needsrelease{$key}
                   14050:             }
                   14051:         } elsif ($item eq 'course') {
                   14052:             if ($name eq 'crstype') {
                   14053:                 $checkcrstypes->{$value} = $Apache::lonnet::needsrelease{$key};
                   14054:             }
                   14055:         }
                   14056:     }
                   14057:     ($anonsurvey->{major},$anonsurvey->{minor}) = split(/\./,$Apache::lonnet::needsrelease{'parameter:type:anonsurvey'});
                   14058:     ($randomizetry->{major},$randomizetry->{minor}) = split(/\./,$Apache::lonnet::needsrelease{'parameter:type:randomizetry'});
                   14059:     return;
                   14060: }
                   14061: 
1.1075.2.11  raeburn  14062: sub update_content_constraints {
                   14063:     my ($cdom,$cnum,$chome,$cid) = @_;
                   14064:     my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired');
                   14065:     my ($reqdmajor,$reqdminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
                   14066:     my %checkresponsetypes;
                   14067:     foreach my $key (keys(%Apache::lonnet::needsrelease)) {
                   14068:         my ($item,$name,$value) = split(/:/,$key);
                   14069:         if ($item eq 'resourcetag') {
                   14070:             if ($name eq 'responsetype') {
                   14071:                 $checkresponsetypes{$value} = $Apache::lonnet::needsrelease{$key}
                   14072:             }
                   14073:         }
                   14074:     }
                   14075:     my $navmap = Apache::lonnavmaps::navmap->new();
                   14076:     if (defined($navmap)) {
                   14077:         my %allresponses;
                   14078:         foreach my $res ($navmap->retrieveResources(undef,sub { $_[0]->is_problem() },1,0)) {
                   14079:             my %responses = $res->responseTypes();
                   14080:             foreach my $key (keys(%responses)) {
                   14081:                 next unless(exists($checkresponsetypes{$key}));
                   14082:                 $allresponses{$key} += $responses{$key};
                   14083:             }
                   14084:         }
                   14085:         foreach my $key (keys(%allresponses)) {
                   14086:             my ($major,$minor) = split(/\./,$checkresponsetypes{$key});
                   14087:             if (($major > $reqdmajor) || ($major == $reqdmajor && $minor > $reqdminor)) {
                   14088:                 ($reqdmajor,$reqdminor) = ($major,$minor);
                   14089:             }
                   14090:         }
                   14091:         undef($navmap);
                   14092:     }
                   14093:     unless (($reqdmajor eq '') && ($reqdminor eq '')) {
                   14094:         &Apache::lonnet::update_released_required($reqdmajor.'.'.$reqdminor,$cdom,$cnum,$chome,$cid);
                   14095:     }
                   14096:     return;
                   14097: }
                   14098: 
                   14099: sub parse_supplemental_title {
                   14100:     my ($title) = @_;
                   14101: 
                   14102:     my ($foldertitle,$renametitle);
                   14103:     if ($title =~ /&amp;&amp;&amp;/) {
                   14104:         $title = &HTML::Entites::decode($title);
                   14105:     }
                   14106:     if ($title =~ m/^(\d+)___&&&___($match_username)___&&&___($match_domain)___&&&___(.*)$/) {
                   14107:         $renametitle=$4;
                   14108:         my ($time,$uname,$udom) = ($1,$2,$3);
                   14109:         $foldertitle=&Apache::lontexconvert::msgtexconverted($4);
                   14110:         my $name =  &plainname($uname,$udom);
                   14111:         $name = &HTML::Entities::encode($name,'"<>&\'');
                   14112:         $renametitle = &HTML::Entities::encode($renametitle,'"<>&\'');
                   14113:         $title='<i>'.&Apache::lonlocal::locallocaltime($time).'</i> '.
                   14114:             $name.': <br />'.$foldertitle;
                   14115:     }
                   14116:     if (wantarray) {
                   14117:         return ($title,$foldertitle,$renametitle);
                   14118:     }
                   14119:     return $title;
                   14120: }
                   14121: 
1.1075.2.18  raeburn  14122: sub symb_to_docspath {
                   14123:     my ($symb) = @_;
                   14124:     return unless ($symb);
                   14125:     my ($mapurl,$id,$resurl) = &Apache::lonnet::decode_symb($symb);
                   14126:     if ($resurl=~/\.(sequence|page)$/) {
                   14127:         $mapurl=$resurl;
                   14128:     } elsif ($resurl eq 'adm/navmaps') {
                   14129:         $mapurl=$env{'course.'.$env{'request.course.id'}.'.url'};
                   14130:     }
                   14131:     my $mapresobj;
                   14132:     my $navmap = Apache::lonnavmaps::navmap->new();
                   14133:     if (ref($navmap)) {
                   14134:         $mapresobj = $navmap->getResourceByUrl($mapurl);
                   14135:     }
                   14136:     $mapurl=~s{^.*/([^/]+)\.(\w+)$}{$1};
                   14137:     my $type=$2;
                   14138:     my $path;
                   14139:     if (ref($mapresobj)) {
                   14140:         my $pcslist = $mapresobj->map_hierarchy();
                   14141:         if ($pcslist ne '') {
                   14142:             foreach my $pc (split(/,/,$pcslist)) {
                   14143:                 next if ($pc <= 1);
                   14144:                 my $res = $navmap->getByMapPc($pc);
                   14145:                 if (ref($res)) {
                   14146:                     my $thisurl = $res->src();
                   14147:                     $thisurl=~s{^.*/([^/]+)\.\w+$}{$1};
                   14148:                     my $thistitle = $res->title();
                   14149:                     $path .= '&'.
                   14150:                              &Apache::lonhtmlcommon::entity_encode($thisurl).'&'.
                   14151:                              &Apache::lonhtmlcommon::entity_encode($thistitle).
                   14152:                              ':'.$res->randompick().
                   14153:                              ':'.$res->randomout().
                   14154:                              ':'.$res->encrypted().
                   14155:                              ':'.$res->randomorder().
                   14156:                              ':'.$res->is_page();
                   14157:                 }
                   14158:             }
                   14159:         }
                   14160:         $path =~ s/^\&//;
                   14161:         my $maptitle = $mapresobj->title();
                   14162:         if ($mapurl eq 'default') {
                   14163:             $maptitle = 'Main Course Documents';
                   14164:         }
                   14165:         $path .= (($path ne '')? '&' : '').
                   14166:                  &Apache::lonhtmlcommon::entity_encode($mapurl).'&'.
                   14167:                  &Apache::lonhtmlcommon::entity_encode($maptitle).
                   14168:                  ':'.$mapresobj->randompick().
                   14169:                  ':'.$mapresobj->randomout().
                   14170:                  ':'.$mapresobj->encrypted().
                   14171:                  ':'.$mapresobj->randomorder().
                   14172:                  ':'.$mapresobj->is_page();
                   14173:     } else {
                   14174:         my $maptitle = &Apache::lonnet::gettitle($mapurl);
                   14175:         my $ispage = (($type eq 'page')? 1 : '');
                   14176:         if ($mapurl eq 'default') {
                   14177:             $maptitle = 'Main Course Documents';
                   14178:         }
                   14179:         $path = &Apache::lonhtmlcommon::entity_encode($mapurl).'&'.
                   14180:                 &Apache::lonhtmlcommon::entity_encode($maptitle).':::::'.$ispage;
                   14181:     }
                   14182:     unless ($mapurl eq 'default') {
                   14183:         $path = 'default&'.
                   14184:                 &Apache::lonhtmlcommon::entity_encode('Main Course Documents').
                   14185:                 ':::::&'.$path;
                   14186:     }
                   14187:     return $path;
                   14188: }
                   14189: 
1.1075.2.14  raeburn  14190: sub captcha_display {
                   14191:     my ($context,$lonhost) = @_;
                   14192:     my ($output,$error);
                   14193:     my ($captcha,$pubkey,$privkey) = &get_captcha_config($context,$lonhost);
                   14194:     if ($captcha eq 'original') {
                   14195:         $output = &create_captcha();
                   14196:         unless ($output) {
                   14197:             $error = 'captcha';
                   14198:         }
                   14199:     } elsif ($captcha eq 'recaptcha') {
                   14200:         $output = &create_recaptcha($pubkey);
                   14201:         unless ($output) {
                   14202:             $error = 'recaptcha';
                   14203:         }
                   14204:     }
                   14205:     return ($output,$error);
                   14206: }
                   14207: 
                   14208: sub captcha_response {
                   14209:     my ($context,$lonhost) = @_;
                   14210:     my ($captcha_chk,$captcha_error);
                   14211:     my ($captcha,$pubkey,$privkey) = &get_captcha_config($context,$lonhost);
                   14212:     if ($captcha eq 'original') {
                   14213:         ($captcha_chk,$captcha_error) = &check_captcha();
                   14214:     } elsif ($captcha eq 'recaptcha') {
                   14215:         $captcha_chk = &check_recaptcha($privkey);
                   14216:     } else {
                   14217:         $captcha_chk = 1;
                   14218:     }
                   14219:     return ($captcha_chk,$captcha_error);
                   14220: }
                   14221: 
                   14222: sub get_captcha_config {
                   14223:     my ($context,$lonhost) = @_;
                   14224:     my ($captcha,$pubkey,$privkey,$hashtocheck);
                   14225:     my $hostname = &Apache::lonnet::hostname($lonhost);
                   14226:     my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
                   14227:     my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
                   14228:     if ($context eq 'usercreation') {
                   14229:         my %domconfig = &Apache::lonnet::get_dom('configuration',[$context],$serverhomedom);
                   14230:         if (ref($domconfig{$context}) eq 'HASH') {
                   14231:             $hashtocheck = $domconfig{$context}{'cancreate'};
                   14232:             if (ref($hashtocheck) eq 'HASH') {
                   14233:                 if ($hashtocheck->{'captcha'} eq 'recaptcha') {
                   14234:                     if (ref($hashtocheck->{'recaptchakeys'}) eq 'HASH') {
                   14235:                         $pubkey = $hashtocheck->{'recaptchakeys'}{'public'};
                   14236:                         $privkey = $hashtocheck->{'recaptchakeys'}{'private'};
                   14237:                     }
                   14238:                     if ($privkey && $pubkey) {
                   14239:                         $captcha = 'recaptcha';
                   14240:                     } else {
                   14241:                         $captcha = 'original';
                   14242:                     }
                   14243:                 } elsif ($hashtocheck->{'captcha'} ne 'notused') {
                   14244:                     $captcha = 'original';
                   14245:                 }
                   14246:             }
                   14247:         } else {
                   14248:             $captcha = 'captcha';
                   14249:         }
                   14250:     } elsif ($context eq 'login') {
                   14251:         my %domconfhash = &Apache::loncommon::get_domainconf($serverhomedom);
                   14252:         if ($domconfhash{$serverhomedom.'.login.captcha'} eq 'recaptcha') {
                   14253:             $pubkey = $domconfhash{$serverhomedom.'.login.recaptchakeys_public'};
                   14254:             $privkey = $domconfhash{$serverhomedom.'.login.recaptchakeys_private'};
                   14255:             if ($privkey && $pubkey) {
                   14256:                 $captcha = 'recaptcha';
                   14257:             } else {
                   14258:                 $captcha = 'original';
                   14259:             }
                   14260:         } elsif ($domconfhash{$serverhomedom.'.login.captcha'} eq 'original') {
                   14261:             $captcha = 'original';
                   14262:         }
                   14263:     }
                   14264:     return ($captcha,$pubkey,$privkey);
                   14265: }
                   14266: 
                   14267: sub create_captcha {
                   14268:     my %captcha_params = &captcha_settings();
                   14269:     my ($output,$maxtries,$tries) = ('',10,0);
                   14270:     while ($tries < $maxtries) {
                   14271:         $tries ++;
                   14272:         my $captcha = Authen::Captcha->new (
                   14273:                                            output_folder => $captcha_params{'output_dir'},
                   14274:                                            data_folder   => $captcha_params{'db_dir'},
                   14275:                                           );
                   14276:         my $md5sum = $captcha->generate_code($captcha_params{'numchars'});
                   14277: 
                   14278:         if (-e $Apache::lonnet::perlvar{'lonCaptchaDir'}.'/'.$md5sum.'.png') {
                   14279:             $output = '<input type="hidden" name="crypt" value="'.$md5sum.'" />'."\n".
                   14280:                       &mt('Type in the letters/numbers shown below').'&nbsp;'.
                   14281:                      '<input type="text" size="5" name="code" value="" /><br />'.
                   14282:                      '<img src="'.$captcha_params{'www_output_dir'}.'/'.$md5sum.'.png" />';
                   14283:             last;
                   14284:         }
                   14285:     }
                   14286:     return $output;
                   14287: }
                   14288: 
                   14289: sub captcha_settings {
                   14290:     my %captcha_params = (
                   14291:                            output_dir     => $Apache::lonnet::perlvar{'lonCaptchaDir'},
                   14292:                            www_output_dir => "/captchaspool",
                   14293:                            db_dir         => $Apache::lonnet::perlvar{'lonCaptchaDb'},
                   14294:                            numchars       => '5',
                   14295:                          );
                   14296:     return %captcha_params;
                   14297: }
                   14298: 
                   14299: sub check_captcha {
                   14300:     my ($captcha_chk,$captcha_error);
                   14301:     my $code = $env{'form.code'};
                   14302:     my $md5sum = $env{'form.crypt'};
                   14303:     my %captcha_params = &captcha_settings();
                   14304:     my $captcha = Authen::Captcha->new(
                   14305:                       output_folder => $captcha_params{'output_dir'},
                   14306:                       data_folder   => $captcha_params{'db_dir'},
                   14307:                   );
                   14308:     my $captcha_chk = $captcha->check_code($code,$md5sum);
                   14309:     my %captcha_hash = (
                   14310:                         0       => 'Code not checked (file error)',
                   14311:                        -1      => 'Failed: code expired',
                   14312:                        -2      => 'Failed: invalid code (not in database)',
                   14313:                        -3      => 'Failed: invalid code (code does not match crypt)',
                   14314:     );
                   14315:     if ($captcha_chk != 1) {
                   14316:         $captcha_error = $captcha_hash{$captcha_chk}
                   14317:     }
                   14318:     return ($captcha_chk,$captcha_error);
                   14319: }
                   14320: 
                   14321: sub create_recaptcha {
                   14322:     my ($pubkey) = @_;
                   14323:     my $captcha = Captcha::reCAPTCHA->new;
                   14324:     return $captcha->get_options_setter({theme => 'white'})."\n".
                   14325:            $captcha->get_html($pubkey).
                   14326:            &mt('If either word is hard to read, [_1] will replace them.',
                   14327:                '<image src="/res/adm/pages/refresh.gif" alt="reCAPTCHA refresh" />').
                   14328:            '<br /><br />';
                   14329: }
                   14330: 
                   14331: sub check_recaptcha {
                   14332:     my ($privkey) = @_;
                   14333:     my $captcha_chk;
                   14334:     my $captcha = Captcha::reCAPTCHA->new;
                   14335:     my $captcha_result =
                   14336:         $captcha->check_answer(
                   14337:                                 $privkey,
                   14338:                                 $ENV{'REMOTE_ADDR'},
                   14339:                                 $env{'form.recaptcha_challenge_field'},
                   14340:                                 $env{'form.recaptcha_response_field'},
                   14341:                               );
                   14342:     if ($captcha_result->{is_valid}) {
                   14343:         $captcha_chk = 1;
                   14344:     }
                   14345:     return $captcha_chk;
                   14346: }
                   14347: 
1.41      ng       14348: =pod
                   14349: 
                   14350: =back
                   14351: 
1.112     bowersj2 14352: =cut
1.41      ng       14353: 
1.112     bowersj2 14354: 1;
                   14355: __END__;
1.41      ng       14356: 

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