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

1.10      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # a pile of common routines
1.10      albertel    3: #
1.1222  ! damieng     4: # $Id: loncommon.pm,v 1.1221 2015/05/21 23:40:09 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.1108    raeburn    70: use Apache::lonuserutils();
1.1110    raeburn    71: use Apache::lonuserstate();
1.1182    raeburn    72: use Apache::courseclassifier();
1.479     albertel   73: use LONCAPA qw(:DEFAULT :match);
1.657     raeburn    74: use DateTime::TimeZone;
1.687     raeburn    75: use DateTime::Locale::Catalog;
1.1220    raeburn    76: use Encode();
1.1091    foxr       77: use Text::Aspell;
1.1094    raeburn    78: use Authen::Captcha;
                     79: use Captcha::reCAPTCHA;
1.1174    raeburn    80: use Crypt::DES;
                     81: use DynaLoader; # for Crypt::DES version
1.117     www        82: 
1.517     raeburn    83: # ---------------------------------------------- Designs
                     84: use vars qw(%defaultdesign);
                     85: 
1.22      www        86: my $readit;
                     87: 
1.517     raeburn    88: 
1.157     matthew    89: ##
                     90: ## Global Variables
                     91: ##
1.46      matthew    92: 
1.643     foxr       93: 
                     94: # ----------------------------------------------- SSI with retries:
                     95: #
                     96: 
                     97: =pod
                     98: 
1.648     raeburn    99: =head1 Server Side include with retries:
1.643     foxr      100: 
                    101: =over 4
                    102: 
1.648     raeburn   103: =item * &ssi_with_retries(resource,retries form)
1.643     foxr      104: 
                    105: Performs an ssi with some number of retries.  Retries continue either
                    106: until the result is ok or until the retry count supplied by the
                    107: caller is exhausted.  
                    108: 
                    109: Inputs:
1.648     raeburn   110: 
                    111: =over 4
                    112: 
1.643     foxr      113: resource   - Identifies the resource to insert.
1.648     raeburn   114: 
1.643     foxr      115: retries    - Count of the number of retries allowed.
1.648     raeburn   116: 
1.643     foxr      117: form       - Hash that identifies the rendering options.
                    118: 
1.648     raeburn   119: =back
                    120: 
                    121: Returns:
                    122: 
                    123: =over 4
                    124: 
1.643     foxr      125: content    - The content of the response.  If retries were exhausted this is empty.
1.648     raeburn   126: 
1.643     foxr      127: response   - The response from the last attempt (which may or may not have been successful.
                    128: 
1.648     raeburn   129: =back
                    130: 
                    131: =back
                    132: 
1.643     foxr      133: =cut
                    134: 
                    135: sub ssi_with_retries {
                    136:     my ($resource, $retries, %form) = @_;
                    137: 
                    138: 
                    139:     my $ok = 0;			# True if we got a good response.
                    140:     my $content;
                    141:     my $response;
                    142: 
                    143:     # Try to get the ssi done. within the retries count:
                    144: 
                    145:     do {
                    146: 	($content, $response) = &Apache::lonnet::ssi($resource, %form);
                    147: 	$ok      = $response->is_success;
1.650     www       148:         if (!$ok) {
                    149:             &Apache::lonnet::logthis("Failed ssi_with_retries on $resource: ".$response->is_success.', '.$response->code.', '.$response->message);
                    150:         }
1.643     foxr      151: 	$retries--;
                    152:     } while (!$ok && ($retries > 0));
                    153: 
                    154:     if (!$ok) {
                    155: 	$content = '';		# On error return an empty content.
                    156:     }
                    157:     return ($content, $response);
                    158: 
                    159: }
                    160: 
                    161: 
                    162: 
1.20      www       163: # ----------------------------------------------- Filetypes/Languages/Copyright
1.12      harris41  164: my %language;
1.124     www       165: my %supported_language;
1.1088    foxr      166: my %supported_codes;
1.1048    foxr      167: my %latex_language;		# For choosing hyphenation in <transl..>
                    168: my %latex_language_bykey;	# for choosing hyphenation from metadata
1.12      harris41  169: my %cprtag;
1.192     taceyjo1  170: my %scprtag;
1.351     www       171: my %fe; my %fd; my %fm;
1.41      ng        172: my %category_extensions;
1.12      harris41  173: 
1.46      matthew   174: # ---------------------------------------------- Thesaurus variables
1.144     matthew   175: #
                    176: # %Keywords:
                    177: #      A hash used by &keyword to determine if a word is considered a keyword.
                    178: # $thesaurus_db_file 
                    179: #      Scalar containing the full path to the thesaurus database.
1.46      matthew   180: 
                    181: my %Keywords;
                    182: my $thesaurus_db_file;
                    183: 
1.144     matthew   184: #
                    185: # Initialize values from language.tab, copyright.tab, filetypes.tab,
                    186: # thesaurus.tab, and filecategories.tab.
                    187: #
1.18      www       188: BEGIN {
1.46      matthew   189:     # Variable initialization
                    190:     $thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
                    191:     #
1.22      www       192:     unless ($readit) {
1.12      harris41  193: # ------------------------------------------------------------------- languages
                    194:     {
1.158     raeburn   195:         my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    196:                                    '/language.tab';
                    197:         if ( open(my $fh,"<$langtabfile") ) {
1.356     albertel  198:             while (my $line = <$fh>) {
                    199:                 next if ($line=~/^\#/);
                    200:                 chomp($line);
1.1088    foxr      201:                 my ($key,$code,$country,$three,$enc,$val,$sup,$latex)=(split(/\t/,$line));
1.158     raeburn   202:                 $language{$key}=$val.' - '.$enc;
                    203:                 if ($sup) {
                    204:                     $supported_language{$key}=$sup;
1.1088    foxr      205: 		    $supported_codes{$key}   = $code;
1.158     raeburn   206:                 }
1.1048    foxr      207: 		if ($latex) {
                    208: 		    $latex_language_bykey{$key} = $latex;
1.1088    foxr      209: 		    $latex_language{$code} = $latex;
1.1048    foxr      210: 		}
1.158     raeburn   211:             }
                    212:             close($fh);
                    213:         }
1.12      harris41  214:     }
                    215: # ------------------------------------------------------------------ copyrights
                    216:     {
1.158     raeburn   217:         my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    218:                                   '/copyright.tab';
                    219:         if ( open (my $fh,"<$copyrightfile") ) {
1.356     albertel  220:             while (my $line = <$fh>) {
                    221:                 next if ($line=~/^\#/);
                    222:                 chomp($line);
                    223:                 my ($key,$val)=(split(/\s+/,$line,2));
1.158     raeburn   224:                 $cprtag{$key}=$val;
                    225:             }
                    226:             close($fh);
                    227:         }
1.12      harris41  228:     }
1.351     www       229: # ----------------------------------------------------------- source copyrights
1.192     taceyjo1  230:     {
                    231:         my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    232:                                   '/source_copyright.tab';
                    233:         if ( open (my $fh,"<$sourcecopyrightfile") ) {
1.356     albertel  234:             while (my $line = <$fh>) {
                    235:                 next if ($line =~ /^\#/);
                    236:                 chomp($line);
                    237:                 my ($key,$val)=(split(/\s+/,$line,2));
1.192     taceyjo1  238:                 $scprtag{$key}=$val;
                    239:             }
                    240:             close($fh);
                    241:         }
                    242:     }
1.63      www       243: 
1.517     raeburn   244: # -------------------------------------------------------------- default domain designs
1.63      www       245:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
1.517     raeburn   246:     my $designfile = $designdir.'/default.tab';
                    247:     if ( open (my $fh,"<$designfile") ) {
                    248:         while (my $line = <$fh>) {
                    249:             next if ($line =~ /^\#/);
                    250:             chomp($line);
                    251:             my ($key,$val)=(split(/\=/,$line));
                    252:             if ($val) { $defaultdesign{$key}=$val; }
                    253:         }
                    254:         close($fh);
1.63      www       255:     }
                    256: 
1.15      harris41  257: # ------------------------------------------------------------- file categories
                    258:     {
1.158     raeburn   259:         my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    260:                                   '/filecategories.tab';
                    261:         if ( open (my $fh,"<$categoryfile") ) {
1.356     albertel  262: 	    while (my $line = <$fh>) {
                    263: 		next if ($line =~ /^\#/);
                    264: 		chomp($line);
                    265:                 my ($extension,$category)=(split(/\s+/,$line,2));
1.158     raeburn   266:                 push @{$category_extensions{lc($category)}},$extension;
                    267:             }
                    268:             close($fh);
                    269:         }
                    270: 
1.15      harris41  271:     }
1.12      harris41  272: # ------------------------------------------------------------------ file types
                    273:     {
1.158     raeburn   274:         my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    275:                '/filetypes.tab';
                    276:         if ( open (my $fh,"<$typesfile") ) {
1.356     albertel  277:             while (my $line = <$fh>) {
                    278: 		next if ($line =~ /^\#/);
                    279: 		chomp($line);
                    280:                 my ($ending,$emb,$mime,$descr)=split(/\s+/,$line,4);
1.158     raeburn   281:                 if ($descr ne '') {
                    282:                     $fe{$ending}=lc($emb);
                    283:                     $fd{$ending}=$descr;
1.351     www       284:                     if ($mime ne 'unk') { $fm{$ending}=$mime; }
1.158     raeburn   285:                 }
                    286:             }
                    287:             close($fh);
                    288:         }
1.12      harris41  289:     }
1.22      www       290:     &Apache::lonnet::logthis(
1.705     tempelho  291:              "<span style='color:yellow;'>INFO: Read file types</span>");
1.22      www       292:     $readit=1;
1.46      matthew   293:     }  # end of unless($readit) 
1.32      matthew   294:     
                    295: }
1.112     bowersj2  296: 
1.42      matthew   297: ###############################################################
                    298: ##           HTML and Javascript Helper Functions            ##
                    299: ###############################################################
                    300: 
                    301: =pod 
                    302: 
1.112     bowersj2  303: =head1 HTML and Javascript Functions
1.42      matthew   304: 
1.112     bowersj2  305: =over 4
                    306: 
1.648     raeburn   307: =item * &browser_and_searcher_javascript()
1.112     bowersj2  308: 
                    309: X<browsing, javascript>X<searching, javascript>Returns a string
                    310: containing javascript with two functions, C<openbrowser> and
                    311: C<opensearcher>. Returned string does not contain E<lt>scriptE<gt>
                    312: tags.
1.42      matthew   313: 
1.648     raeburn   314: =item * &openbrowser(formname,elementname,only,omit) [javascript]
1.42      matthew   315: 
                    316: inputs: formname, elementname, only, omit
                    317: 
                    318: formname and elementname indicate the name of the html form and name of
                    319: the element that the results of the browsing selection are to be placed in. 
                    320: 
                    321: Specifying 'only' will restrict the browser to displaying only files
1.185     www       322: with the given extension.  Can be a comma separated list.
1.42      matthew   323: 
                    324: Specifying 'omit' will restrict the browser to NOT displaying files
1.185     www       325: with the given extension.  Can be a comma separated list.
1.42      matthew   326: 
1.648     raeburn   327: =item * &opensearcher(formname,elementname) [javascript]
1.42      matthew   328: 
                    329: Inputs: formname, elementname
                    330: 
                    331: formname and elementname specify the name of the html form and the name
                    332: of the element the selection from the search results will be placed in.
1.542     raeburn   333: 
1.42      matthew   334: =cut
                    335: 
                    336: sub browser_and_searcher_javascript {
1.199     albertel  337:     my ($mode)=@_;
                    338:     if (!defined($mode)) { $mode='edit'; }
1.453     albertel  339:     my $resurl=&escape_single(&lastresurl());
1.42      matthew   340:     return <<END;
1.219     albertel  341: // <!-- BEGIN LON-CAPA Internal
1.50      matthew   342:     var editbrowser = null;
1.135     albertel  343:     function openbrowser(formname,elementname,only,omit,titleelement) {
1.170     www       344:         var url = '$resurl/?';
1.42      matthew   345:         if (editbrowser == null) {
                    346:             url += 'launch=1&';
                    347:         }
                    348:         url += 'catalogmode=interactive&';
1.199     albertel  349:         url += 'mode=$mode&';
1.611     albertel  350:         url += 'inhibitmenu=yes&';
1.42      matthew   351:         url += 'form=' + formname + '&';
                    352:         if (only != null) {
                    353:             url += 'only=' + only + '&';
1.217     albertel  354:         } else {
                    355:             url += 'only=&';
                    356: 	}
1.42      matthew   357:         if (omit != null) {
                    358:             url += 'omit=' + omit + '&';
1.217     albertel  359:         } else {
                    360:             url += 'omit=&';
                    361: 	}
1.135     albertel  362:         if (titleelement != null) {
                    363:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  364:         } else {
                    365: 	    url += 'titleelement=&';
                    366: 	}
1.42      matthew   367:         url += 'element=' + elementname + '';
                    368:         var title = 'Browser';
1.435     albertel  369:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
1.42      matthew   370:         options += ',width=700,height=600';
                    371:         editbrowser = open(url,title,options,'1');
                    372:         editbrowser.focus();
                    373:     }
                    374:     var editsearcher;
1.135     albertel  375:     function opensearcher(formname,elementname,titleelement) {
1.42      matthew   376:         var url = '/adm/searchcat?';
                    377:         if (editsearcher == null) {
                    378:             url += 'launch=1&';
                    379:         }
                    380:         url += 'catalogmode=interactive&';
1.199     albertel  381:         url += 'mode=$mode&';
1.42      matthew   382:         url += 'form=' + formname + '&';
1.135     albertel  383:         if (titleelement != null) {
                    384:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  385:         } else {
                    386: 	    url += 'titleelement=&';
                    387: 	}
1.42      matthew   388:         url += 'element=' + elementname + '';
                    389:         var title = 'Search';
1.435     albertel  390:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
1.42      matthew   391:         options += ',width=700,height=600';
                    392:         editsearcher = open(url,title,options,'1');
                    393:         editsearcher.focus();
                    394:     }
1.219     albertel  395: // END LON-CAPA Internal -->
1.42      matthew   396: END
1.170     www       397: }
                    398: 
                    399: sub lastresurl {
1.258     albertel  400:     if ($env{'environment.lastresurl'}) {
                    401: 	return $env{'environment.lastresurl'}
1.170     www       402:     } else {
                    403: 	return '/res';
                    404:     }
                    405: }
                    406: 
                    407: sub storeresurl {
                    408:     my $resurl=&Apache::lonnet::clutter(shift);
                    409:     unless ($resurl=~/^\/res/) { return 0; }
                    410:     $resurl=~s/\/$//;
                    411:     &Apache::lonnet::put('environment',{'lastresurl' => $resurl});
1.646     raeburn   412:     &Apache::lonnet::appenv({'environment.lastresurl' => $resurl});
1.170     www       413:     return 1;
1.42      matthew   414: }
                    415: 
1.74      www       416: sub studentbrowser_javascript {
1.111     www       417:    unless (
1.258     albertel  418:             (($env{'request.course.id'}) && 
1.302     albertel  419:              (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    420: 	      || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    421: 					  '/'.$env{'request.course.sec'})
                    422: 	      ))
1.258     albertel  423:          || ($env{'request.role'}=~/^(au|dc|su)/)
1.111     www       424:           ) { return ''; }  
1.74      www       425:    return (<<'ENDSTDBRW');
1.776     bisitz    426: <script type="text/javascript" language="Javascript">
1.824     bisitz    427: // <![CDATA[
1.74      www       428:     var stdeditbrowser;
1.999     www       429:     function openstdbrowser(formname,uname,udom,clicker,roleflag,ignorefilter,courseadvonly) {
1.74      www       430:         var url = '/adm/pickstudent?';
                    431:         var filter;
1.558     albertel  432: 	if (!ignorefilter) {
                    433: 	    eval('filter=document.'+formname+'.'+uname+'.value;');
                    434: 	}
1.74      www       435:         if (filter != null) {
                    436:            if (filter != '') {
                    437:                url += 'filter='+filter+'&';
                    438: 	   }
                    439:         }
                    440:         url += 'form=' + formname + '&unameelement='+uname+
1.999     www       441:                                     '&udomelement='+udom+
                    442:                                     '&clicker='+clicker;
1.111     www       443: 	if (roleflag) { url+="&roles=1"; }
1.793     raeburn   444:         if (courseadvonly) { url+="&courseadvonly=1"; }
1.102     www       445:         var title = 'Student_Browser';
1.74      www       446:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    447:         options += ',width=700,height=600';
                    448:         stdeditbrowser = open(url,title,options,'1');
                    449:         stdeditbrowser.focus();
                    450:     }
1.824     bisitz    451: // ]]>
1.74      www       452: </script>
                    453: ENDSTDBRW
                    454: }
1.42      matthew   455: 
1.1003    www       456: sub resourcebrowser_javascript {
                    457:    unless ($env{'request.course.id'}) { return ''; }
1.1004    www       458:    return (<<'ENDRESBRW');
1.1003    www       459: <script type="text/javascript" language="Javascript">
                    460: // <![CDATA[
                    461:     var reseditbrowser;
1.1004    www       462:     function openresbrowser(formname,reslink) {
1.1005    www       463:         var url = '/adm/pickresource?form='+formname+'&reslink='+reslink;
1.1003    www       464:         var title = 'Resource_Browser';
                    465:         var options = 'scrollbars=1,resizable=1,menubar=0';
1.1005    www       466:         options += ',width=700,height=500';
1.1004    www       467:         reseditbrowser = open(url,title,options,'1');
                    468:         reseditbrowser.focus();
1.1003    www       469:     }
                    470: // ]]>
                    471: </script>
1.1004    www       472: ENDRESBRW
1.1003    www       473: }
                    474: 
1.74      www       475: sub selectstudent_link {
1.999     www       476:    my ($form,$unameele,$udomele,$courseadvonly,$clickerid)=@_;
                    477:    my $callargs = "'".&Apache::lonhtmlcommon::entity_encode($form)."','".
                    478:                       &Apache::lonhtmlcommon::entity_encode($unameele)."','".
                    479:                       &Apache::lonhtmlcommon::entity_encode($udomele)."'";
1.258     albertel  480:    if ($env{'request.course.id'}) {  
1.302     albertel  481:        if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    482: 	   && !&Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    483: 					'/'.$env{'request.course.sec'})) {
1.111     www       484: 	   return '';
                    485:        }
1.999     www       486:        $callargs.=",'".&Apache::lonhtmlcommon::entity_encode($clickerid)."'";
1.793     raeburn   487:        if ($courseadvonly)  {
                    488:            $callargs .= ",'',1,1";
                    489:        }
                    490:        return '<span class="LC_nobreak">'.
                    491:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
                    492:               &mt('Select User').'</a></span>';
1.74      www       493:    }
1.258     albertel  494:    if ($env{'request.role'}=~/^(au|dc|su)/) {
1.1012    www       495:        $callargs .= ",'',1"; 
1.793     raeburn   496:        return '<span class="LC_nobreak">'.
                    497:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
                    498:               &mt('Select User').'</a></span>';
1.111     www       499:    }
                    500:    return '';
1.91      www       501: }
                    502: 
1.1004    www       503: sub selectresource_link {
                    504:    my ($form,$reslink,$arg)=@_;
                    505:    
                    506:    my $callargs = "'".&Apache::lonhtmlcommon::entity_encode($form)."','".
                    507:                       &Apache::lonhtmlcommon::entity_encode($reslink)."'";
                    508:    unless ($env{'request.course.id'}) { return $arg; }
                    509:    return '<span class="LC_nobreak">'.
                    510:               '<a href="javascript:openresbrowser('.$callargs.');">'.
                    511:               $arg.'</a></span>';
                    512: }
                    513: 
                    514: 
                    515: 
1.653     raeburn   516: sub authorbrowser_javascript {
                    517:     return <<"ENDAUTHORBRW";
1.776     bisitz    518: <script type="text/javascript" language="JavaScript">
1.824     bisitz    519: // <![CDATA[
1.653     raeburn   520: var stdeditbrowser;
                    521: 
                    522: function openauthorbrowser(formname,udom) {
                    523:     var url = '/adm/pickauthor?';
                    524:     url += 'form='+formname+'&roledom='+udom;
                    525:     var title = 'Author_Browser';
                    526:     var options = 'scrollbars=1,resizable=1,menubar=0';
                    527:     options += ',width=700,height=600';
                    528:     stdeditbrowser = open(url,title,options,'1');
                    529:     stdeditbrowser.focus();
                    530: }
                    531: 
1.824     bisitz    532: // ]]>
1.653     raeburn   533: </script>
                    534: ENDAUTHORBRW
                    535: }
                    536: 
1.91      www       537: sub coursebrowser_javascript {
1.1116    raeburn   538:     my ($domainfilter,$sec_element,$formname,$role_element,$crstype,
1.1221    raeburn   539:         $credits_element,$instcode) = @_;
1.932     raeburn   540:     my $wintitle = 'Course_Browser';
1.931     raeburn   541:     if ($crstype eq 'Community') {
1.932     raeburn   542:         $wintitle = 'Community_Browser';
1.909     raeburn   543:     }
1.876     raeburn   544:     my $id_functions = &javascript_index_functions();
                    545:     my $output = '
1.776     bisitz    546: <script type="text/javascript" language="JavaScript">
1.824     bisitz    547: // <![CDATA[
1.468     raeburn   548:     var stdeditbrowser;'."\n";
1.876     raeburn   549: 
                    550:     $output .= <<"ENDSTDBRW";
1.909     raeburn   551:     function opencrsbrowser(formname,uname,udom,desc,extra_element,multflag,type,type_elem) {
1.91      www       552:         var url = '/adm/pickcourse?';
1.895     raeburn   553:         var formid = getFormIdByName(formname);
1.876     raeburn   554:         var domainfilter = getDomainFromSelectbox(formname,udom);
1.128     albertel  555:         if (domainfilter != null) {
                    556:            if (domainfilter != '') {
                    557:                url += 'domainfilter='+domainfilter+'&';
                    558: 	   }
                    559:         }
1.91      www       560:         url += 'form=' + formname + '&cnumelement='+uname+
1.187     albertel  561: 	                            '&cdomelement='+udom+
                    562:                                     '&cnameelement='+desc;
1.468     raeburn   563:         if (extra_element !=null && extra_element != '') {
1.594     raeburn   564:             if (formname == 'rolechoice' || formname == 'studentform') {
1.468     raeburn   565:                 url += '&roleelement='+extra_element;
                    566:                 if (domainfilter == null || domainfilter == '') {
                    567:                     url += '&domainfilter='+extra_element;
                    568:                 }
1.234     raeburn   569:             }
1.468     raeburn   570:             else {
                    571:                 if (formname == 'portform') {
                    572:                     url += '&setroles='+extra_element;
1.800     raeburn   573:                 } else {
                    574:                     if (formname == 'rules') {
                    575:                         url += '&fixeddom='+extra_element; 
                    576:                     }
1.468     raeburn   577:                 }
                    578:             }     
1.230     raeburn   579:         }
1.909     raeburn   580:         if (type != null && type != '') {
                    581:             url += '&type='+type;
                    582:         }
                    583:         if (type_elem != null && type_elem != '') {
                    584:             url += '&typeelement='+type_elem;
                    585:         }
1.872     raeburn   586:         if (formname == 'ccrs') {
                    587:             var ownername = document.forms[formid].ccuname.value;
                    588:             var ownerdom =  document.forms[formid].ccdomain.options[document.forms[formid].ccdomain.selectedIndex].value;
1.1221    raeburn   589:             url += '&cloner='+ownername+':'+ownerdom+'&crscode='+document.forms[formid].crscode.value;
                    590:         }
                    591:         if (formname == 'requestcrs') {
                    592:             url += '&crsdom=$domainfilter&crscode=$instcode';
1.872     raeburn   593:         }
1.293     raeburn   594:         if (multflag !=null && multflag != '') {
                    595:             url += '&multiple='+multflag;
                    596:         }
1.909     raeburn   597:         var title = '$wintitle';
1.91      www       598:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    599:         options += ',width=700,height=600';
                    600:         stdeditbrowser = open(url,title,options,'1');
                    601:         stdeditbrowser.focus();
                    602:     }
1.876     raeburn   603: $id_functions
                    604: ENDSTDBRW
1.1116    raeburn   605:     if (($sec_element ne '') || ($role_element ne '') || ($credits_element ne '')) {
                    606:         $output .= &setsec_javascript($sec_element,$formname,$role_element,
                    607:                                       $credits_element);
1.876     raeburn   608:     }
                    609:     $output .= '
                    610: // ]]>
                    611: </script>';
                    612:     return $output;
                    613: }
                    614: 
                    615: sub javascript_index_functions {
                    616:     return <<"ENDJS";
                    617: 
                    618: function getFormIdByName(formname) {
                    619:     for (var i=0;i<document.forms.length;i++) {
                    620:         if (document.forms[i].name == formname) {
                    621:             return i;
                    622:         }
                    623:     }
                    624:     return -1;
                    625: }
                    626: 
                    627: function getIndexByName(formid,item) {
                    628:     for (var i=0;i<document.forms[formid].elements.length;i++) {
                    629:         if (document.forms[formid].elements[i].name == item) {
                    630:             return i;
                    631:         }
                    632:     }
                    633:     return -1;
                    634: }
1.468     raeburn   635: 
1.876     raeburn   636: function getDomainFromSelectbox(formname,udom) {
                    637:     var userdom;
                    638:     var formid = getFormIdByName(formname);
                    639:     if (formid > -1) {
                    640:         var domid = getIndexByName(formid,udom);
                    641:         if (domid > -1) {
                    642:             if (document.forms[formid].elements[domid].type == 'select-one') {
                    643:                 userdom=document.forms[formid].elements[domid].options[document.forms[formid].elements[domid].selectedIndex].value;
                    644:             }
                    645:             if (document.forms[formid].elements[domid].type == 'hidden') {
                    646:                 userdom=document.forms[formid].elements[domid].value;
1.468     raeburn   647:             }
                    648:         }
                    649:     }
1.876     raeburn   650:     return userdom;
                    651: }
                    652: 
                    653: ENDJS
1.468     raeburn   654: 
1.876     raeburn   655: }
                    656: 
1.1017    raeburn   657: sub javascript_array_indexof {
1.1018    raeburn   658:     return <<ENDJS;
1.1017    raeburn   659: <script type="text/javascript" language="JavaScript">
                    660: // <![CDATA[
                    661: 
                    662: if (!Array.prototype.indexOf) {
                    663:     Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
                    664:         "use strict";
                    665:         if (this === void 0 || this === null) {
                    666:             throw new TypeError();
                    667:         }
                    668:         var t = Object(this);
                    669:         var len = t.length >>> 0;
                    670:         if (len === 0) {
                    671:             return -1;
                    672:         }
                    673:         var n = 0;
                    674:         if (arguments.length > 0) {
                    675:             n = Number(arguments[1]);
1.1088    foxr      676:             if (n !== n) { // shortcut for verifying if it is NaN
1.1017    raeburn   677:                 n = 0;
                    678:             } else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
                    679:                 n = (n > 0 || -1) * Math.floor(Math.abs(n));
                    680:             }
                    681:         }
                    682:         if (n >= len) {
                    683:             return -1;
                    684:         }
                    685:         var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
                    686:         for (; k < len; k++) {
                    687:             if (k in t && t[k] === searchElement) {
                    688:                 return k;
                    689:             }
                    690:         }
                    691:         return -1;
                    692:     }
                    693: }
                    694: 
                    695: // ]]>
                    696: </script>
                    697: 
                    698: ENDJS
                    699: 
                    700: }
                    701: 
1.876     raeburn   702: sub userbrowser_javascript {
                    703:     my $id_functions = &javascript_index_functions();
                    704:     return <<"ENDUSERBRW";
                    705: 
1.888     raeburn   706: function openuserbrowser(formname,uname,udom,ulast,ufirst,uemail,hideudom,crsdom,caller) {
1.876     raeburn   707:     var url = '/adm/pickuser?';
                    708:     var userdom = getDomainFromSelectbox(formname,udom);
                    709:     if (userdom != null) {
                    710:        if (userdom != '') {
                    711:            url += 'srchdom='+userdom+'&';
                    712:        }
                    713:     }
                    714:     url += 'form=' + formname + '&unameelement='+uname+
                    715:                                 '&udomelement='+udom+
                    716:                                 '&ulastelement='+ulast+
                    717:                                 '&ufirstelement='+ufirst+
                    718:                                 '&uemailelement='+uemail+
1.881     raeburn   719:                                 '&hideudomelement='+hideudom+
                    720:                                 '&coursedom='+crsdom;
1.888     raeburn   721:     if ((caller != null) && (caller != undefined)) {
                    722:         url += '&caller='+caller;
                    723:     }
1.876     raeburn   724:     var title = 'User_Browser';
                    725:     var options = 'scrollbars=1,resizable=1,menubar=0';
                    726:     options += ',width=700,height=600';
                    727:     var stdeditbrowser = open(url,title,options,'1');
                    728:     stdeditbrowser.focus();
                    729: }
                    730: 
1.888     raeburn   731: function fix_domain (formname,udom,origdom,uname) {
1.876     raeburn   732:     var formid = getFormIdByName(formname);
                    733:     if (formid > -1) {
1.888     raeburn   734:         var unameid = getIndexByName(formid,uname);
1.876     raeburn   735:         var domid = getIndexByName(formid,udom);
                    736:         var hidedomid = getIndexByName(formid,origdom);
                    737:         if (hidedomid > -1) {
                    738:             var fixeddom = document.forms[formid].elements[hidedomid].value;
1.888     raeburn   739:             var unameval = document.forms[formid].elements[unameid].value;
                    740:             if ((fixeddom != '') && (fixeddom != undefined) && (fixeddom != null) && (unameval != '') && (unameval != undefined) && (unameval != null)) {
                    741:                 if (domid > -1) {
                    742:                     var slct = document.forms[formid].elements[domid];
                    743:                     if (slct.type == 'select-one') {
                    744:                         var i;
                    745:                         for (i=0;i<slct.length;i++) {
                    746:                             if (slct.options[i].value==fixeddom) { slct.selectedIndex=i; }
                    747:                         }
                    748:                     }
                    749:                     if (slct.type == 'hidden') {
                    750:                         slct.value = fixeddom;
1.876     raeburn   751:                     }
                    752:                 }
1.468     raeburn   753:             }
                    754:         }
                    755:     }
1.876     raeburn   756:     return;
                    757: }
                    758: 
                    759: $id_functions
                    760: ENDUSERBRW
1.468     raeburn   761: }
                    762: 
                    763: sub setsec_javascript {
1.1116    raeburn   764:     my ($sec_element,$formname,$role_element,$credits_element) = @_;
1.905     raeburn   765:     my (@courserolenames,@communityrolenames,$rolestr,$courserolestr,
                    766:         $communityrolestr);
                    767:     if ($role_element ne '') {
                    768:         my @allroles = ('st','ta','ep','in','ad');
                    769:         foreach my $crstype ('Course','Community') {
                    770:             if ($crstype eq 'Community') {
                    771:                 foreach my $role (@allroles) {
                    772:                     push(@communityrolenames,&Apache::lonnet::plaintext($role,$crstype));
                    773:                 }
                    774:                 push(@communityrolenames,&Apache::lonnet::plaintext('co'));
                    775:             } else {
                    776:                 foreach my $role (@allroles) {
                    777:                     push(@courserolenames,&Apache::lonnet::plaintext($role,$crstype));
                    778:                 }
                    779:                 push(@courserolenames,&Apache::lonnet::plaintext('cc'));
                    780:             }
                    781:         }
                    782:         $rolestr = '"'.join('","',@allroles).'"';
                    783:         $courserolestr = '"'.join('","',@courserolenames).'"';
                    784:         $communityrolestr = '"'.join('","',@communityrolenames).'"';
                    785:     }
1.468     raeburn   786:     my $setsections = qq|
                    787: function setSect(sectionlist) {
1.629     raeburn   788:     var sectionsArray = new Array();
                    789:     if ((sectionlist != '') && (typeof sectionlist != "undefined")) {
                    790:         sectionsArray = sectionlist.split(",");
                    791:     }
1.468     raeburn   792:     var numSections = sectionsArray.length;
                    793:     document.$formname.$sec_element.length = 0;
                    794:     if (numSections == 0) {
                    795:         document.$formname.$sec_element.multiple=false;
                    796:         document.$formname.$sec_element.size=1;
                    797:         document.$formname.$sec_element.options[0] = new Option('No existing sections','',false,false)
                    798:     } else {
                    799:         if (numSections == 1) {
                    800:             document.$formname.$sec_element.multiple=false;
                    801:             document.$formname.$sec_element.size=1;
                    802:             document.$formname.$sec_element.options[0] = new Option('Select','',true,true);
                    803:             document.$formname.$sec_element.options[1] = new Option('No section','',false,false)
                    804:             document.$formname.$sec_element.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
                    805:         } else {
                    806:             for (var i=0; i<numSections; i++) {
                    807:                 document.$formname.$sec_element.options[i] = new Option(sectionsArray[i],sectionsArray[i],false,false)
                    808:             }
                    809:             document.$formname.$sec_element.multiple=true
                    810:             if (numSections < 3) {
                    811:                 document.$formname.$sec_element.size=numSections;
                    812:             } else {
                    813:                 document.$formname.$sec_element.size=3;
                    814:             }
                    815:             document.$formname.$sec_element.options[0].selected = false
                    816:         }
                    817:     }
1.91      www       818: }
1.905     raeburn   819: 
                    820: function setRole(crstype) {
1.468     raeburn   821: |;
1.905     raeburn   822:     if ($role_element eq '') {
                    823:         $setsections .= '    return;
                    824: }
                    825: ';
                    826:     } else {
                    827:         $setsections .= qq|
                    828:     var elementLength = document.$formname.$role_element.length;
                    829:     var allroles = Array($rolestr);
                    830:     var courserolenames = Array($courserolestr);
                    831:     var communityrolenames = Array($communityrolestr);
                    832:     if (elementLength != undefined) {
                    833:         if (document.$formname.$role_element.options[5].value == 'cc') {
                    834:             if (crstype == 'Course') {
                    835:                 return;
                    836:             } else {
                    837:                 allroles[5] = 'co';
                    838:                 for (var i=0; i<6; i++) {
                    839:                     document.$formname.$role_element.options[i].value = allroles[i];
                    840:                     document.$formname.$role_element.options[i].text = communityrolenames[i];
                    841:                 }
                    842:             }
                    843:         } else {
                    844:             if (crstype == 'Community') {
                    845:                 return;
                    846:             } else {
                    847:                 allroles[5] = 'cc';
                    848:                 for (var i=0; i<6; i++) {
                    849:                     document.$formname.$role_element.options[i].value = allroles[i];
                    850:                     document.$formname.$role_element.options[i].text = courserolenames[i];
                    851:                 }
                    852:             }
                    853:         }
                    854:     }
                    855:     return;
                    856: }
                    857: |;
                    858:     }
1.1116    raeburn   859:     if ($credits_element) {
                    860:         $setsections .= qq|
                    861: function setCredits(defaultcredits) {
                    862:     document.$formname.$credits_element.value = defaultcredits;
                    863:     return;
                    864: }
                    865: |;
                    866:     }
1.468     raeburn   867:     return $setsections;
                    868: }
                    869: 
1.91      www       870: sub selectcourse_link {
1.909     raeburn   871:    my ($form,$unameele,$udomele,$desc,$extra_element,$multflag,$selecttype,
                    872:        $typeelement) = @_;
                    873:    my $type = $selecttype;
1.871     raeburn   874:    my $linktext = &mt('Select Course');
                    875:    if ($selecttype eq 'Community') {
1.909     raeburn   876:        $linktext = &mt('Select Community');
1.906     raeburn   877:    } elsif ($selecttype eq 'Course/Community') {
                    878:        $linktext = &mt('Select Course/Community');
1.909     raeburn   879:        $type = '';
1.1019    raeburn   880:    } elsif ($selecttype eq 'Select') {
                    881:        $linktext = &mt('Select');
                    882:        $type = '';
1.871     raeburn   883:    }
1.787     bisitz    884:    return '<span class="LC_nobreak">'
                    885:          ."<a href='"
                    886:          .'javascript:opencrsbrowser("'.$form.'","'.$unameele
                    887:          .'","'.$udomele.'","'.$desc.'","'.$extra_element
1.909     raeburn   888:          .'","'.$multflag.'","'.$type.'","'.$typeelement.'");'
1.871     raeburn   889:          ."'>".$linktext.'</a>'
1.787     bisitz    890:          .'</span>';
1.74      www       891: }
1.42      matthew   892: 
1.653     raeburn   893: sub selectauthor_link {
                    894:    my ($form,$udom)=@_;
                    895:    return '<a href="javascript:openauthorbrowser('."'$form','$udom'".');">'.
                    896:           &mt('Select Author').'</a>';
                    897: }
                    898: 
1.876     raeburn   899: sub selectuser_link {
1.881     raeburn   900:     my ($form,$unameelem,$domelem,$lastelem,$firstelem,$emailelem,$hdomelem,
1.888     raeburn   901:         $coursedom,$linktext,$caller) = @_;
1.876     raeburn   902:     return '<a href="javascript:openuserbrowser('."'$form','$unameelem','$domelem',".
1.888     raeburn   903:            "'$lastelem','$firstelem','$emailelem','$hdomelem','$coursedom','$caller'".
1.881     raeburn   904:            ');">'.$linktext.'</a>';
1.876     raeburn   905: }
                    906: 
1.273     raeburn   907: sub check_uncheck_jscript {
                    908:     my $jscript = <<"ENDSCRT";
                    909: function checkAll(field) {
                    910:     if (field.length > 0) {
                    911:         for (i = 0; i < field.length; i++) {
1.1093    raeburn   912:             if (!field[i].disabled) { 
                    913:                 field[i].checked = true;
                    914:             }
1.273     raeburn   915:         }
                    916:     } else {
1.1093    raeburn   917:         if (!field.disabled) { 
                    918:             field.checked = true;
                    919:         }
1.273     raeburn   920:     }
                    921: }
                    922:  
                    923: function uncheckAll(field) {
                    924:     if (field.length > 0) {
                    925:         for (i = 0; i < field.length; i++) {
                    926:             field[i].checked = false ;
1.543     albertel  927:         }
                    928:     } else {
1.273     raeburn   929:         field.checked = false ;
                    930:     }
                    931: }
                    932: ENDSCRT
                    933:     return $jscript;
                    934: }
                    935: 
1.656     www       936: sub select_timezone {
1.659     raeburn   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 '') || ($selected eq 'local')) {
                    942:            $output .= ' selected="selected" ';
                    943:        }
                    944:        $output .= '> </option>';
                    945:    }
1.657     raeburn   946:    my @timezones = DateTime::TimeZone->all_names;
                    947:    foreach my $tzone (@timezones) {
                    948:        $output.= '<option value="'.$tzone.'"';
                    949:        if ($tzone eq $selected) {
                    950:            $output.=' selected="selected"';
                    951:        }
                    952:        $output.=">$tzone</option>\n";
1.656     www       953:    }
                    954:    $output.="</select>";
                    955:    return $output;
                    956: }
1.273     raeburn   957: 
1.687     raeburn   958: sub select_datelocale {
                    959:     my ($name,$selected,$onchange,$includeempty)=@_;
                    960:     my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
                    961:     if ($includeempty) {
                    962:         $output .= '<option value=""';
                    963:         if ($selected eq '') {
                    964:             $output .= ' selected="selected" ';
                    965:         }
                    966:         $output .= '> </option>';
                    967:     }
                    968:     my (@possibles,%locale_names);
                    969:     my @locales = DateTime::Locale::Catalog::Locales;
                    970:     foreach my $locale (@locales) {
                    971:         if (ref($locale) eq 'HASH') {
                    972:             my $id = $locale->{'id'};
                    973:             if ($id ne '') {
                    974:                 my $en_terr = $locale->{'en_territory'};
                    975:                 my $native_terr = $locale->{'native_territory'};
1.695     raeburn   976:                 my @languages = &Apache::lonlocal::preferred_languages();
1.687     raeburn   977:                 if (grep(/^en$/,@languages) || !@languages) {
                    978:                     if ($en_terr ne '') {
                    979:                         $locale_names{$id} = '('.$en_terr.')';
                    980:                     } elsif ($native_terr ne '') {
                    981:                         $locale_names{$id} = $native_terr;
                    982:                     }
                    983:                 } else {
                    984:                     if ($native_terr ne '') {
                    985:                         $locale_names{$id} = $native_terr.' ';
                    986:                     } elsif ($en_terr ne '') {
                    987:                         $locale_names{$id} = '('.$en_terr.')';
                    988:                     }
                    989:                 }
1.1220    raeburn   990:                 $locale_names{$id} = Encode::encode('UTF-8',$locale_names{$id});
1.687     raeburn   991:                 push (@possibles,$id);
                    992:             }
                    993:         }
                    994:     }
                    995:     foreach my $item (sort(@possibles)) {
                    996:         $output.= '<option value="'.$item.'"';
                    997:         if ($item eq $selected) {
                    998:             $output.=' selected="selected"';
                    999:         }
                   1000:         $output.=">$item";
                   1001:         if ($locale_names{$item} ne '') {
1.1220    raeburn  1002:             $output.='  '.$locale_names{$item};
1.687     raeburn  1003:         }
                   1004:         $output.="</option>\n";
                   1005:     }
                   1006:     $output.="</select>";
                   1007:     return $output;
                   1008: }
                   1009: 
1.792     raeburn  1010: sub select_language {
                   1011:     my ($name,$selected,$includeempty) = @_;
                   1012:     my %langchoices;
                   1013:     if ($includeempty) {
1.1117    raeburn  1014:         %langchoices = ('' => 'No language preference');
1.792     raeburn  1015:     }
                   1016:     foreach my $id (&languageids()) {
                   1017:         my $code = &supportedlanguagecode($id);
                   1018:         if ($code) {
                   1019:             $langchoices{$code} = &plainlanguagedescription($id);
                   1020:         }
                   1021:     }
1.1117    raeburn  1022:     %langchoices = &Apache::lonlocal::texthash(%langchoices);
1.970     raeburn  1023:     return &select_form($selected,$name,\%langchoices);
1.792     raeburn  1024: }
                   1025: 
1.42      matthew  1026: =pod
1.36      matthew  1027: 
1.1088    foxr     1028: 
                   1029: =item * &list_languages()
                   1030: 
                   1031: Returns an array reference that is suitable for use in language prompters.
                   1032: Each array element is itself a two element array.  The first element
                   1033: is the language code.  The second element a descsriptiuon of the 
                   1034: language itself.  This is suitable for use in e.g.
                   1035: &Apache::edit::select_arg (once dereferenced that is).
                   1036: 
                   1037: =cut 
                   1038: 
                   1039: sub list_languages {
                   1040:     my @lang_choices;
                   1041: 
                   1042:     foreach my $id (&languageids()) {
                   1043: 	my $code = &supportedlanguagecode($id);
                   1044: 	if ($code) {
                   1045: 	    my $selector    = $supported_codes{$id};
                   1046: 	    my $description = &plainlanguagedescription($id);
                   1047: 	    push (@lang_choices, [$selector, $description]);
                   1048: 	}
                   1049:     }
                   1050:     return \@lang_choices;
                   1051: }
                   1052: 
                   1053: =pod
                   1054: 
1.648     raeburn  1055: =item * &linked_select_forms(...)
1.36      matthew  1056: 
                   1057: linked_select_forms returns a string containing a <script></script> block
                   1058: and html for two <select> menus.  The select menus will be linked in that
                   1059: changing the value of the first menu will result in new values being placed
                   1060: in the second menu.  The values in the select menu will appear in alphabetical
1.609     raeburn  1061: order unless a defined order is provided.
1.36      matthew  1062: 
                   1063: linked_select_forms takes the following ordered inputs:
                   1064: 
                   1065: =over 4
                   1066: 
1.112     bowersj2 1067: =item * $formname, the name of the <form> tag
1.36      matthew  1068: 
1.112     bowersj2 1069: =item * $middletext, the text which appears between the <select> tags
1.36      matthew  1070: 
1.112     bowersj2 1071: =item * $firstdefault, the default value for the first menu
1.36      matthew  1072: 
1.112     bowersj2 1073: =item * $firstselectname, the name of the first <select> tag
1.36      matthew  1074: 
1.112     bowersj2 1075: =item * $secondselectname, the name of the second <select> tag
1.36      matthew  1076: 
1.112     bowersj2 1077: =item * $hashref, a reference to a hash containing the data for the menus.
1.36      matthew  1078: 
1.609     raeburn  1079: =item * $menuorder, the order of values in the first menu
                   1080: 
1.1115    raeburn  1081: =item * $onchangefirst, additional javascript call to execute for an onchange
                   1082:         event for the first <select> tag
                   1083: 
                   1084: =item * $onchangesecond, additional javascript call to execute for an onchange
                   1085:         event for the second <select> tag
                   1086: 
1.41      ng       1087: =back 
                   1088: 
1.36      matthew  1089: Below is an example of such a hash.  Only the 'text', 'default', and 
                   1090: 'select2' keys must appear as stated.  keys(%menu) are the possible 
                   1091: values for the first select menu.  The text that coincides with the 
1.41      ng       1092: first menu value is given in $menu{$choice1}->{'text'}.  The values 
1.36      matthew  1093: and text for the second menu are given in the hash pointed to by 
                   1094: $menu{$choice1}->{'select2'}.  
                   1095: 
1.112     bowersj2 1096:  my %menu = ( A1 => { text =>"Choice A1" ,
                   1097:                        default => "B3",
                   1098:                        select2 => { 
                   1099:                            B1 => "Choice B1",
                   1100:                            B2 => "Choice B2",
                   1101:                            B3 => "Choice B3",
                   1102:                            B4 => "Choice B4"
1.609     raeburn  1103:                            },
                   1104:                        order => ['B4','B3','B1','B2'],
1.112     bowersj2 1105:                    },
                   1106:                A2 => { text =>"Choice A2" ,
                   1107:                        default => "C2",
                   1108:                        select2 => { 
                   1109:                            C1 => "Choice C1",
                   1110:                            C2 => "Choice C2",
                   1111:                            C3 => "Choice C3"
1.609     raeburn  1112:                            },
                   1113:                        order => ['C2','C1','C3'],
1.112     bowersj2 1114:                    },
                   1115:                A3 => { text =>"Choice A3" ,
                   1116:                        default => "D6",
                   1117:                        select2 => { 
                   1118:                            D1 => "Choice D1",
                   1119:                            D2 => "Choice D2",
                   1120:                            D3 => "Choice D3",
                   1121:                            D4 => "Choice D4",
                   1122:                            D5 => "Choice D5",
                   1123:                            D6 => "Choice D6",
                   1124:                            D7 => "Choice D7"
1.609     raeburn  1125:                            },
                   1126:                        order => ['D4','D3','D2','D1','D7','D6','D5'],
1.112     bowersj2 1127:                    }
                   1128:                );
1.36      matthew  1129: 
                   1130: =cut
                   1131: 
                   1132: sub linked_select_forms {
                   1133:     my ($formname,
                   1134:         $middletext,
                   1135:         $firstdefault,
                   1136:         $firstselectname,
                   1137:         $secondselectname, 
1.609     raeburn  1138:         $hashref,
                   1139:         $menuorder,
1.1115    raeburn  1140:         $onchangefirst,
                   1141:         $onchangesecond
1.36      matthew  1142:         ) = @_;
                   1143:     my $second = "document.$formname.$secondselectname";
                   1144:     my $first = "document.$formname.$firstselectname";
                   1145:     # output the javascript to do the changing
                   1146:     my $result = '';
1.776     bisitz   1147:     $result.='<script type="text/javascript" language="JavaScript">'."\n";
1.824     bisitz   1148:     $result.="// <![CDATA[\n";
1.36      matthew  1149:     $result.="var select2data = new Object();\n";
                   1150:     $" = '","';
                   1151:     my $debug = '';
                   1152:     foreach my $s1 (sort(keys(%$hashref))) {
                   1153:         $result.="select2data.d_$s1 = new Object();\n";        
                   1154:         $result.="select2data.d_$s1.def = new String('".
                   1155:             $hashref->{$s1}->{'default'}."');\n";
1.609     raeburn  1156:         $result.="select2data.d_$s1.values = new Array(";
1.36      matthew  1157:         my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
1.609     raeburn  1158:         if (ref($hashref->{$s1}->{'order'}) eq 'ARRAY') {
                   1159:             @s2values = @{$hashref->{$s1}->{'order'}};
                   1160:         }
1.36      matthew  1161:         $result.="\"@s2values\");\n";
                   1162:         $result.="select2data.d_$s1.texts = new Array(";        
                   1163:         my @s2texts;
                   1164:         foreach my $value (@s2values) {
                   1165:             push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
                   1166:         }
                   1167:         $result.="\"@s2texts\");\n";
                   1168:     }
                   1169:     $"=' ';
                   1170:     $result.= <<"END";
                   1171: 
                   1172: function select1_changed() {
                   1173:     // Determine new choice
                   1174:     var newvalue = "d_" + $first.value;
                   1175:     // update select2
                   1176:     var values     = select2data[newvalue].values;
                   1177:     var texts      = select2data[newvalue].texts;
                   1178:     var select2def = select2data[newvalue].def;
                   1179:     var i;
                   1180:     // out with the old
                   1181:     for (i = 0; i < $second.options.length; i++) {
                   1182:         $second.options[i] = null;
                   1183:     }
                   1184:     // in with the nuclear
                   1185:     for (i=0;i<values.length; i++) {
                   1186:         $second.options[i] = new Option(values[i]);
1.143     matthew  1187:         $second.options[i].value = values[i];
1.36      matthew  1188:         $second.options[i].text = texts[i];
                   1189:         if (values[i] == select2def) {
                   1190:             $second.options[i].selected = true;
                   1191:         }
                   1192:     }
                   1193: }
1.824     bisitz   1194: // ]]>
1.36      matthew  1195: </script>
                   1196: END
                   1197:     # output the initial values for the selection lists
1.1115    raeburn  1198:     $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed();$onchangefirst\">\n";
1.609     raeburn  1199:     my @order = sort(keys(%{$hashref}));
                   1200:     if (ref($menuorder) eq 'ARRAY') {
                   1201:         @order = @{$menuorder};
                   1202:     }
                   1203:     foreach my $value (@order) {
1.36      matthew  1204:         $result.="    <option value=\"$value\" ";
1.253     albertel 1205:         $result.=" selected=\"selected\" " if ($value eq $firstdefault);
1.119     www      1206:         $result.=">".&mt($hashref->{$value}->{'text'})."</option>\n";
1.36      matthew  1207:     }
                   1208:     $result .= "</select>\n";
                   1209:     my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
                   1210:     $result .= $middletext;
1.1115    raeburn  1211:     $result .= "<select size=\"1\" name=\"$secondselectname\"";
                   1212:     if ($onchangesecond) {
                   1213:         $result .= ' onchange="'.$onchangesecond.'"';
                   1214:     }
                   1215:     $result .= ">\n";
1.36      matthew  1216:     my $seconddefault = $hashref->{$firstdefault}->{'default'};
1.609     raeburn  1217:     
                   1218:     my @secondorder = sort(keys(%select2));
                   1219:     if (ref($hashref->{$firstdefault}->{'order'}) eq 'ARRAY') {
                   1220:         @secondorder = @{$hashref->{$firstdefault}->{'order'}};
                   1221:     }
                   1222:     foreach my $value (@secondorder) {
1.36      matthew  1223:         $result.="    <option value=\"$value\" ";        
1.253     albertel 1224:         $result.=" selected=\"selected\" " if ($value eq $seconddefault);
1.119     www      1225:         $result.=">".&mt($select2{$value})."</option>\n";
1.36      matthew  1226:     }
                   1227:     $result .= "</select>\n";
                   1228:     #    return $debug;
                   1229:     return $result;
                   1230: }   #  end of sub linked_select_forms {
                   1231: 
1.45      matthew  1232: =pod
1.44      bowersj2 1233: 
1.973     raeburn  1234: =item * &help_open_topic($topic,$text,$stayOnPage,$width,$height,$imgid)
1.44      bowersj2 1235: 
1.112     bowersj2 1236: Returns a string corresponding to an HTML link to the given help
                   1237: $topic, where $topic corresponds to the name of a .tex file in
                   1238: /home/httpd/html/adm/help/tex, with underscores replaced by
                   1239: spaces. 
                   1240: 
                   1241: $text will optionally be linked to the same topic, allowing you to
                   1242: link text in addition to the graphic. If you do not want to link
                   1243: text, but wish to specify one of the later parameters, pass an
                   1244: empty string. 
                   1245: 
                   1246: $stayOnPage is a value that will be interpreted as a boolean. If true,
                   1247: the link will not open a new window. If false, the link will open
                   1248: a new window using Javascript. (Default is false.) 
                   1249: 
                   1250: $width and $height are optional numerical parameters that will
                   1251: override the width and height of the popped up window, which may
1.973     raeburn  1252: be useful for certain help topics with big pictures included.
                   1253: 
                   1254: $imgid is the id of the img tag used for the help icon. This may be
                   1255: used in a javascript call to switch the image src.  See 
                   1256: lonhtmlcommon::htmlareaselectactive() for an example.
1.44      bowersj2 1257: 
                   1258: =cut
                   1259: 
                   1260: sub help_open_topic {
1.973     raeburn  1261:     my ($topic, $text, $stayOnPage, $width, $height, $imgid) = @_;
1.48      bowersj2 1262:     $text = "" if (not defined $text);
1.44      bowersj2 1263:     $stayOnPage = 0 if (not defined $stayOnPage);
1.1033    www      1264:     $width = 500 if (not defined $width);
1.44      bowersj2 1265:     $height = 400 if (not defined $height);
                   1266:     my $filename = $topic;
                   1267:     $filename =~ s/ /_/g;
                   1268: 
1.48      bowersj2 1269:     my $template = "";
                   1270:     my $link;
1.572     banghart 1271:     
1.159     www      1272:     $topic=~s/\W/\_/g;
1.44      bowersj2 1273: 
1.572     banghart 1274:     if (!$stayOnPage) {
1.1033    www      1275: 	$link = "javascript:openMyModal('/adm/help/${filename}.hlp',$width,$height,'yes');";
1.1037    www      1276:     } elsif ($stayOnPage eq 'popup') {
                   1277:         $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 1278:     } else {
1.48      bowersj2 1279: 	$link = "/adm/help/${filename}.hlp";
                   1280:     }
                   1281: 
                   1282:     # Add the text
1.755     neumanie 1283:     if ($text ne "") {	
1.763     bisitz   1284: 	$template.='<span class="LC_help_open_topic">'
                   1285:                   .'<a target="_top" href="'.$link.'">'
                   1286:                   .$text.'</a>';
1.48      bowersj2 1287:     }
                   1288: 
1.763     bisitz   1289:     # (Always) Add the graphic
1.179     matthew  1290:     my $title = &mt('Online Help');
1.667     raeburn  1291:     my $helpicon=&lonhttpdurl("/adm/help/help.png");
1.973     raeburn  1292:     if ($imgid ne '') {
                   1293:         $imgid = ' id="'.$imgid.'"';
                   1294:     }
1.763     bisitz   1295:     $template.=' <a target="_top" href="'.$link.'" title="'.$title.'">'
                   1296:               .'<img src="'.$helpicon.'" border="0"'
                   1297:               .' alt="'.&mt('Help: [_1]',$topic).'"'
1.973     raeburn  1298:               .' title="'.$title.'" style="vertical-align:middle;"'.$imgid 
1.763     bisitz   1299:               .' /></a>';
                   1300:     if ($text ne "") {	
                   1301:         $template.='</span>';
                   1302:     }
1.44      bowersj2 1303:     return $template;
                   1304: 
1.106     bowersj2 1305: }
                   1306: 
                   1307: # This is a quicky function for Latex cheatsheet editing, since it 
                   1308: # appears in at least four places
                   1309: sub helpLatexCheatsheet {
1.1037    www      1310:     my ($topic,$text,$not_author,$stayOnPage) = @_;
1.732     raeburn  1311:     my $out;
1.106     bowersj2 1312:     my $addOther = '';
1.732     raeburn  1313:     if ($topic) {
1.1037    www      1314: 	$addOther = '<span>'.&help_open_topic($topic,&mt($text),$stayOnPage, undef, 600).'</span> ';
1.763     bisitz   1315:     }
                   1316:     $out = '<span>' # Start cheatsheet
                   1317: 	  .$addOther
                   1318:           .'<span>'
1.1037    www      1319: 	  .&help_open_topic('Greek_Symbols',&mt('Greek Symbols'),$stayOnPage,undef,600)
1.763     bisitz   1320: 	  .'</span> <span>'
1.1037    www      1321: 	  .&help_open_topic('Other_Symbols',&mt('Other Symbols'),$stayOnPage,undef,600)
1.763     bisitz   1322: 	  .'</span>';
1.732     raeburn  1323:     unless ($not_author) {
1.1186    kruse    1324:         $out .= '<span>'
                   1325:                .&help_open_topic('Authoring_Output_Tags',&mt('Output Tags'),$stayOnPage,undef,600)
                   1326:                .'</span> <span>'
                   1327:                .&help_open_topic('Authoring_Multilingual_Problems',&mt('How to create problems in different languages'),$stayOnPage,undef,600)
1.763     bisitz   1328: 	       .'</span>';
1.732     raeburn  1329:     }
1.763     bisitz   1330:     $out .= '</span>'; # End cheatsheet
1.732     raeburn  1331:     return $out;
1.172     www      1332: }
                   1333: 
1.430     albertel 1334: sub general_help {
                   1335:     my $helptopic='Student_Intro';
                   1336:     if ($env{'request.role'}=~/^(ca|au)/) {
                   1337: 	$helptopic='Authoring_Intro';
1.907     raeburn  1338:     } elsif ($env{'request.role'}=~/^(cc|co)/) {
1.430     albertel 1339: 	$helptopic='Course_Coordination_Intro';
1.672     raeburn  1340:     } elsif ($env{'request.role'}=~/^dc/) {
                   1341:         $helptopic='Domain_Coordination_Intro';
1.430     albertel 1342:     }
                   1343:     return $helptopic;
                   1344: }
                   1345: 
                   1346: sub update_help_link {
                   1347:     my ($topic,$component_help,$faq,$bug,$stayOnPage) = @_;
                   1348:     my $origurl = $ENV{'REQUEST_URI'};
                   1349:     $origurl=~s|^/~|/priv/|;
                   1350:     my $timestamp = time;
                   1351:     foreach my $datum (\$topic,\$component_help,\$faq,\$bug,\$origurl) {
                   1352:         $$datum = &escape($$datum);
                   1353:     }
                   1354: 
                   1355:     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";
                   1356:     my $output .= <<"ENDOUTPUT";
                   1357: <script type="text/javascript">
1.824     bisitz   1358: // <![CDATA[
1.430     albertel 1359: banner_link = '$banner_link';
1.824     bisitz   1360: // ]]>
1.430     albertel 1361: </script>
                   1362: ENDOUTPUT
                   1363:     return $output;
                   1364: }
                   1365: 
                   1366: # now just updates the help link and generates a blue icon
1.193     raeburn  1367: sub help_open_menu {
1.430     albertel 1368:     my ($topic,$component_help,$faq,$bug,$stayOnPage,$width,$height,$text) 
1.552     banghart 1369: 	= @_;    
1.949     droeschl 1370:     $stayOnPage = 1;
1.430     albertel 1371:     my $output;
                   1372:     if ($component_help) {
                   1373: 	if (!$text) {
                   1374: 	    $output=&help_open_topic($component_help,undef,$stayOnPage,
                   1375: 				       $width,$height);
                   1376: 	} else {
                   1377: 	    my $help_text;
                   1378: 	    $help_text=&unescape($topic);
                   1379: 	    $output='<table><tr><td>'.
                   1380: 		&help_open_topic($component_help,$help_text,$stayOnPage,
                   1381: 				 $width,$height).'</td></tr></table>';
                   1382: 	}
                   1383:     }
                   1384:     my $banner_link = &update_help_link($topic,$component_help,$faq,$bug,$stayOnPage);
                   1385:     return $output.$banner_link;
                   1386: }
                   1387: 
                   1388: sub top_nav_help {
                   1389:     my ($text) = @_;
1.436     albertel 1390:     $text = &mt($text);
1.949     droeschl 1391:     my $stay_on_page = 1;
                   1392: 
1.1168    raeburn  1393:     my ($link,$banner_link);
                   1394:     unless ($env{'request.noversionuri'} =~ m{^/adm/helpmenu}) {
                   1395:         $link = ($stay_on_page) ? "javascript:helpMenu('display')"
                   1396: 	                         : "javascript:helpMenu('open')";
                   1397:         $banner_link = &update_help_link(undef,undef,undef,undef,$stay_on_page);
                   1398:     }
1.201     raeburn  1399:     my $title = &mt('Get help');
1.1168    raeburn  1400:     if ($link) {
                   1401:         return <<"END";
1.436     albertel 1402: $banner_link
1.1159    raeburn  1403: <a href="$link" title="$title">$text</a>
1.436     albertel 1404: END
1.1168    raeburn  1405:     } else {
                   1406:         return '&nbsp;'.$text.'&nbsp;';
                   1407:     }
1.436     albertel 1408: }
                   1409: 
                   1410: sub help_menu_js {
1.1154    raeburn  1411:     my ($httphost) = @_;
1.949     droeschl 1412:     my $stayOnPage = 1;
1.436     albertel 1413:     my $width = 620;
                   1414:     my $height = 600;
1.430     albertel 1415:     my $helptopic=&general_help();
1.1154    raeburn  1416:     my $details_link = $httphost.'/adm/help/'.$helptopic.'.hlp';
1.261     albertel 1417:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.331     albertel 1418:     my $start_page =
                   1419:         &Apache::loncommon::start_page('Help Menu', undef,
                   1420: 				       {'frameset'    => 1,
                   1421: 					'js_ready'    => 1,
1.1154    raeburn  1422:                                         'use_absolute' => $httphost,
1.331     albertel 1423: 					'add_entries' => {
1.1168    raeburn  1424: 					    'border' => '0', 
1.579     raeburn  1425: 					    'rows'   => "110,*",},});
1.331     albertel 1426:     my $end_page =
                   1427:         &Apache::loncommon::end_page({'frameset' => 1,
                   1428: 				      'js_ready' => 1,});
                   1429: 
1.436     albertel 1430:     my $template .= <<"ENDTEMPLATE";
                   1431: <script type="text/javascript">
1.877     bisitz   1432: // <![CDATA[
1.253     albertel 1433: // <!-- BEGIN LON-CAPA Internal
1.430     albertel 1434: var banner_link = '';
1.243     raeburn  1435: function helpMenu(target) {
                   1436:     var caller = this;
                   1437:     if (target == 'open') {
                   1438:         var newWindow = null;
                   1439:         try {
1.262     albertel 1440:             newWindow =  window.open($nothing,"helpmenu","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" )
1.243     raeburn  1441:         }
                   1442:         catch(error) {
                   1443:             writeHelp(caller);
                   1444:             return;
                   1445:         }
                   1446:         if (newWindow) {
                   1447:             caller = newWindow;
                   1448:         }
1.193     raeburn  1449:     }
1.243     raeburn  1450:     writeHelp(caller);
                   1451:     return;
                   1452: }
                   1453: function writeHelp(caller) {
1.1168    raeburn  1454:     caller.document.writeln('$start_page\\n<frame name="bannerframe" src="'+banner_link+'" marginwidth="0" marginheight="0" frameborder="0">\\n');
                   1455:     caller.document.writeln('<frame name="bodyframe" src="$details_link" marginwidth="0" marginheight="0" frameborder="0">\\n$end_page');
                   1456:     caller.document.close();
                   1457:     caller.focus();
1.193     raeburn  1458: }
1.877     bisitz   1459: // END LON-CAPA Internal -->
1.253     albertel 1460: // ]]>
1.436     albertel 1461: </script>
1.193     raeburn  1462: ENDTEMPLATE
                   1463:     return $template;
                   1464: }
                   1465: 
1.172     www      1466: sub help_open_bug {
                   1467:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel 1468:     unless ($env{'user.adv'}) { return ''; }
1.172     www      1469:     unless ($Apache::lonnet::perlvar{'BugzillaHost'}) { return ''; }
                   1470:     $text = "" if (not defined $text);
                   1471: 	$stayOnPage=1;
1.184     albertel 1472:     $width = 600 if (not defined $width);
                   1473:     $height = 600 if (not defined $height);
1.172     www      1474: 
                   1475:     $topic=~s/\W+/\+/g;
                   1476:     my $link='';
                   1477:     my $template='';
1.379     albertel 1478:     my $url=$Apache::lonnet::perlvar{'BugzillaHost'}.'enter_bug.cgi?product=LON-CAPA&amp;bug_file_loc='.
                   1479: 	&escape($ENV{'REQUEST_URI'}).'&amp;component='.$topic;
1.172     www      1480:     if (!$stayOnPage)
                   1481:     {
                   1482: 	$link = "javascript:void(open('$url', 'Bugzilla', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                   1483:     }
                   1484:     else
                   1485:     {
                   1486: 	$link = $url;
                   1487:     }
                   1488:     # Add the text
                   1489:     if ($text ne "")
                   1490:     {
                   1491: 	$template .= 
                   1492:   "<table bgcolor='#AA3333' cellspacing='1' cellpadding='1' border='0'><tr>".
1.705     tempelho 1493:   "<td bgcolor='#FF5555'><a target=\"_top\" href=\"$link\"><span style=\"color:#FFFFFF;font-size:10pt;\">$text</span></a>";
1.172     www      1494:     }
                   1495: 
                   1496:     # Add the graphic
1.179     matthew  1497:     my $title = &mt('Report a Bug');
1.215     albertel 1498:     my $bugicon=&lonhttpdurl("/adm/lonMisc/smallBug.gif");
1.172     www      1499:     $template .= <<"ENDTEMPLATE";
1.436     albertel 1500:  <a target="_top" href="$link" title="$title"><img src="$bugicon" border="0" alt="(Bug: $topic)" /></a>
1.172     www      1501: ENDTEMPLATE
                   1502:     if ($text ne '') { $template.='</td></tr></table>' };
                   1503:     return $template;
                   1504: 
                   1505: }
                   1506: 
                   1507: sub help_open_faq {
                   1508:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel 1509:     unless ($env{'user.adv'}) { return ''; }
1.172     www      1510:     unless ($Apache::lonnet::perlvar{'FAQHost'}) { return ''; }
                   1511:     $text = "" if (not defined $text);
                   1512: 	$stayOnPage=1;
                   1513:     $width = 350 if (not defined $width);
                   1514:     $height = 400 if (not defined $height);
                   1515: 
                   1516:     $topic=~s/\W+/\+/g;
                   1517:     my $link='';
                   1518:     my $template='';
                   1519:     my $url=$Apache::lonnet::perlvar{'FAQHost'}.'/fom/cache/'.$topic.'.html';
                   1520:     if (!$stayOnPage)
                   1521:     {
                   1522: 	$link = "javascript:void(open('$url', 'FAQ-O-Matic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                   1523:     }
                   1524:     else
                   1525:     {
                   1526: 	$link = $url;
                   1527:     }
                   1528: 
                   1529:     # Add the text
                   1530:     if ($text ne "")
                   1531:     {
                   1532: 	$template .= 
1.173     www      1533:   "<table bgcolor='#337733' cellspacing='1' cellpadding='1' border='0'><tr>".
1.705     tempelho 1534:   "<td bgcolor='#448844'><a target=\"_top\" href=\"$link\"><span style=\"color:#FFFFFF; font-size:10pt;\">$text</span></a>";
1.172     www      1535:     }
                   1536: 
                   1537:     # Add the graphic
1.179     matthew  1538:     my $title = &mt('View the FAQ');
1.215     albertel 1539:     my $faqicon=&lonhttpdurl("/adm/lonMisc/smallFAQ.gif");
1.172     www      1540:     $template .= <<"ENDTEMPLATE";
1.436     albertel 1541:  <a target="_top" href="$link" title="$title"><img src="$faqicon" border="0" alt="(FAQ: $topic)" /></a>
1.172     www      1542: ENDTEMPLATE
                   1543:     if ($text ne '') { $template.='</td></tr></table>' };
                   1544:     return $template;
                   1545: 
1.44      bowersj2 1546: }
1.37      matthew  1547: 
1.180     matthew  1548: ###############################################################
                   1549: ###############################################################
                   1550: 
1.45      matthew  1551: =pod
                   1552: 
1.648     raeburn  1553: =item * &change_content_javascript():
1.256     matthew  1554: 
                   1555: This and the next function allow you to create small sections of an
                   1556: otherwise static HTML page that you can update on the fly with
                   1557: Javascript, even in Netscape 4.
                   1558: 
                   1559: The Javascript fragment returned by this function (no E<lt>scriptE<gt> tag)
                   1560: must be written to the HTML page once. It will prove the Javascript
                   1561: function "change(name, content)". Calling the change function with the
                   1562: name of the section 
                   1563: you want to update, matching the name passed to C<changable_area>, and
                   1564: the new content you want to put in there, will put the content into
                   1565: that area.
                   1566: 
                   1567: B<Note>: Netscape 4 only reserves enough space for the changable area
                   1568: to contain room for the original contents. You need to "make space"
                   1569: for whatever changes you wish to make, and be B<sure> to check your
                   1570: code in Netscape 4. This feature in Netscape 4 is B<not> powerful;
                   1571: it's adequate for updating a one-line status display, but little more.
                   1572: This script will set the space to 100% width, so you only need to
                   1573: worry about height in Netscape 4.
                   1574: 
                   1575: Modern browsers are much less limiting, and if you can commit to the
                   1576: user not using Netscape 4, this feature may be used freely with
                   1577: pretty much any HTML.
                   1578: 
                   1579: =cut
                   1580: 
                   1581: sub change_content_javascript {
                   1582:     # If we're on Netscape 4, we need to use Layer-based code
1.258     albertel 1583:     if ($env{'browser.type'} eq 'netscape' &&
                   1584: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew  1585: 	return (<<NETSCAPE4);
                   1586: 	function change(name, content) {
                   1587: 	    doc = document.layers[name+"___escape"].layers[0].document;
                   1588: 	    doc.open();
                   1589: 	    doc.write(content);
                   1590: 	    doc.close();
                   1591: 	}
                   1592: NETSCAPE4
                   1593:     } else {
                   1594: 	# Otherwise, we need to use semi-standards-compliant code
                   1595: 	# (technically, "innerHTML" isn't standard but the equivalent
                   1596: 	# is really scary, and every useful browser supports it
                   1597: 	return (<<DOMBASED);
                   1598: 	function change(name, content) {
                   1599: 	    element = document.getElementById(name);
                   1600: 	    element.innerHTML = content;
                   1601: 	}
                   1602: DOMBASED
                   1603:     }
                   1604: }
                   1605: 
                   1606: =pod
                   1607: 
1.648     raeburn  1608: =item * &changable_area($name,$origContent):
1.256     matthew  1609: 
                   1610: This provides a "changable area" that can be modified on the fly via
                   1611: the Javascript code provided in C<change_content_javascript>. $name is
                   1612: the name you will use to reference the area later; do not repeat the
                   1613: same name on a given HTML page more then once. $origContent is what
                   1614: the area will originally contain, which can be left blank.
                   1615: 
                   1616: =cut
                   1617: 
                   1618: sub changable_area {
                   1619:     my ($name, $origContent) = @_;
                   1620: 
1.258     albertel 1621:     if ($env{'browser.type'} eq 'netscape' &&
                   1622: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew  1623: 	# If this is netscape 4, we need to use the Layer tag
                   1624: 	return "<ilayer width='100%' id='${name}___escape' overflow='none'><layer width='100%' id='$name' overflow='none'>$origContent</layer></ilayer>";
                   1625:     } else {
                   1626: 	return "<span id='$name'>$origContent</span>";
                   1627:     }
                   1628: }
                   1629: 
                   1630: =pod
                   1631: 
1.648     raeburn  1632: =item * &viewport_geometry_js 
1.590     raeburn  1633: 
                   1634: Provides javascript object (Geometry) which can provide information about the viewport geometry for the client browser.
                   1635: 
                   1636: =cut
                   1637: 
                   1638: 
                   1639: sub viewport_geometry_js { 
                   1640:     return <<"GEOMETRY";
                   1641: var Geometry = {};
                   1642: function init_geometry() {
                   1643:     if (Geometry.init) { return };
                   1644:     Geometry.init=1;
                   1645:     if (window.innerHeight) {
                   1646:         Geometry.getViewportHeight   = function() { return window.innerHeight; };
                   1647:         Geometry.getViewportWidth   = function() { return window.innerWidth; };
                   1648:         Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
                   1649:         Geometry.getVerticalScroll   = function() { return window.pageYOffset; };
                   1650:     }
                   1651:     else if (document.documentElement && document.documentElement.clientHeight) {
                   1652:         Geometry.getViewportHeight =
                   1653:             function() { return document.documentElement.clientHeight; };
                   1654:         Geometry.getViewportWidth =
                   1655:             function() { return document.documentElement.clientWidth; };
                   1656: 
                   1657:         Geometry.getHorizontalScroll =
                   1658:             function() { return document.documentElement.scrollLeft; };
                   1659:         Geometry.getVerticalScroll =
                   1660:             function() { return document.documentElement.scrollTop; };
                   1661:     }
                   1662:     else if (document.body.clientHeight) {
                   1663:         Geometry.getViewportHeight =
                   1664:             function() { return document.body.clientHeight; };
                   1665:         Geometry.getViewportWidth =
                   1666:             function() { return document.body.clientWidth; };
                   1667:         Geometry.getHorizontalScroll =
                   1668:             function() { return document.body.scrollLeft; };
                   1669:         Geometry.getVerticalScroll =
                   1670:             function() { return document.body.scrollTop; };
                   1671:     }
                   1672: }
                   1673: 
                   1674: GEOMETRY
                   1675: }
                   1676: 
                   1677: =pod
                   1678: 
1.648     raeburn  1679: =item * &viewport_size_js()
1.590     raeburn  1680: 
                   1681: 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. 
                   1682: 
                   1683: =cut
                   1684: 
                   1685: sub viewport_size_js {
                   1686:     my $geometry = &viewport_geometry_js();
                   1687:     return <<"DIMS";
                   1688: 
                   1689: $geometry
                   1690: 
                   1691: function getViewportDims(width,height) {
                   1692:     init_geometry();
                   1693:     width.value = Geometry.getViewportWidth();
                   1694:     height.value = Geometry.getViewportHeight();
                   1695:     return;
                   1696: }
                   1697: 
                   1698: DIMS
                   1699: }
                   1700: 
                   1701: =pod
                   1702: 
1.648     raeburn  1703: =item * &resize_textarea_js()
1.565     albertel 1704: 
                   1705: emits the needed javascript to resize a textarea to be as big as possible
                   1706: 
                   1707: creates a function resize_textrea that takes two IDs first should be
                   1708: the id of the element to resize, second should be the id of a div that
                   1709: surrounds everything that comes after the textarea, this routine needs
                   1710: to be attached to the <body> for the onload and onresize events.
                   1711: 
1.648     raeburn  1712: =back
1.565     albertel 1713: 
                   1714: =cut
                   1715: 
                   1716: sub resize_textarea_js {
1.590     raeburn  1717:     my $geometry = &viewport_geometry_js();
1.565     albertel 1718:     return <<"RESIZE";
                   1719:     <script type="text/javascript">
1.824     bisitz   1720: // <![CDATA[
1.590     raeburn  1721: $geometry
1.565     albertel 1722: 
1.588     albertel 1723: function getX(element) {
                   1724:     var x = 0;
                   1725:     while (element) {
                   1726: 	x += element.offsetLeft;
                   1727: 	element = element.offsetParent;
                   1728:     }
                   1729:     return x;
                   1730: }
                   1731: function getY(element) {
                   1732:     var y = 0;
                   1733:     while (element) {
                   1734: 	y += element.offsetTop;
                   1735: 	element = element.offsetParent;
                   1736:     }
                   1737:     return y;
                   1738: }
                   1739: 
                   1740: 
1.565     albertel 1741: function resize_textarea(textarea_id,bottom_id) {
                   1742:     init_geometry();
                   1743:     var textarea        = document.getElementById(textarea_id);
                   1744:     //alert(textarea);
                   1745: 
1.588     albertel 1746:     var textarea_top    = getY(textarea);
1.565     albertel 1747:     var textarea_height = textarea.offsetHeight;
                   1748:     var bottom          = document.getElementById(bottom_id);
1.588     albertel 1749:     var bottom_top      = getY(bottom);
1.565     albertel 1750:     var bottom_height   = bottom.offsetHeight;
                   1751:     var window_height   = Geometry.getViewportHeight();
1.588     albertel 1752:     var fudge           = 23;
1.565     albertel 1753:     var new_height      = window_height-fudge-textarea_top-bottom_height;
                   1754:     if (new_height < 300) {
                   1755: 	new_height = 300;
                   1756:     }
                   1757:     textarea.style.height=new_height+'px';
                   1758: }
1.824     bisitz   1759: // ]]>
1.565     albertel 1760: </script>
                   1761: RESIZE
                   1762: 
                   1763: }
                   1764: 
1.1205    golterma 1765: sub colorfuleditor_js {
                   1766:     return <<"COLORFULEDIT"
                   1767: <script type="text/javascript">
                   1768: // <![CDATA[>
                   1769:     function fold_box(curDepth, lastresource){
                   1770: 
                   1771:     // we need a list because there can be several blocks you need to fold in one tag
                   1772:         var block = document.getElementsByName('foldblock_'+curDepth);
                   1773:     // but there is only one folding button per tag
                   1774:         var foldbutton = document.getElementById('folding_btn_'+curDepth);
                   1775: 
                   1776:         if(block.item(0).style.display == 'none'){
                   1777: 
                   1778:             foldbutton.value = '@{[&mt("Hide")]}';
                   1779:             for (i = 0; i < block.length; i++){
                   1780:                 block.item(i).style.display = '';
                   1781:             }
                   1782:         }else{
                   1783: 
                   1784:             foldbutton.value = '@{[&mt("Show")]}';
                   1785:             for (i = 0; i < block.length; i++){
                   1786:                 // block.item(i).style.visibility = 'collapse';
                   1787:                 block.item(i).style.display = 'none';
                   1788:             }
                   1789:         };
                   1790:         saveState(lastresource);
                   1791:     }
                   1792: 
                   1793:     function saveState (lastresource) {
                   1794: 
                   1795:         var tag_list = getTagList();
                   1796:         if(tag_list != null){
                   1797:             var timestamp = new Date().getTime();
                   1798:             var key = lastresource;
                   1799: 
                   1800:             // the value pattern is: 'time;key1,value1;key2,value2; ... '
                   1801:             // starting with timestamp
                   1802:             var value = timestamp+';';
                   1803: 
                   1804:             // building the list of key-value pairs
                   1805:             for(var i = 0; i < tag_list.length; i++){
                   1806:                 value += tag_list[i]+',';
                   1807:                 value += document.getElementsByName(tag_list[i])[0].style.display+';';
                   1808:             }
                   1809: 
                   1810:             // only iterate whole storage if nothing to override
                   1811:             if(localStorage.getItem(key) == null){        
                   1812: 
                   1813:                 // prevent storage from growing large
                   1814:                 if(localStorage.length > 50){
                   1815:                     var regex_getTimestamp = /^(?:\d)+;/;
                   1816:                     var oldest_timestamp = regex_getTimestamp.exec(localStorage.key(0));
                   1817:                     var oldest_key;
                   1818:                     
                   1819:                     for(var i = 1; i < localStorage.length; i++){
                   1820:                         if (regex_getTimestamp.exec(localStorage.key(i)) < oldest_timestamp) {
                   1821:                             oldest_key = localStorage.key(i);
                   1822:                             oldest_timestamp = regex_getTimestamp.exec(oldest_key);
                   1823:                         }
                   1824:                     }
                   1825:                     localStorage.removeItem(oldest_key);
                   1826:                 }
                   1827:             }
                   1828:             localStorage.setItem(key,value);
                   1829:         }
                   1830:     }
                   1831: 
                   1832:     // restore folding status of blocks (on page load)
                   1833:     function restoreState (lastresource) {
                   1834:         if(localStorage.getItem(lastresource) != null){
                   1835:             var key = lastresource;
                   1836:             var value = localStorage.getItem(key);
                   1837:             var regex_delTimestamp = /^\d+;/;
                   1838: 
                   1839:             value.replace(regex_delTimestamp, '');
                   1840: 
                   1841:             var valueArr = value.split(';');
                   1842:             var pairs;
                   1843:             var elements;
                   1844:             for (var i = 0; i < valueArr.length; i++){
                   1845:                 pairs = valueArr[i].split(',');
                   1846:                 elements = document.getElementsByName(pairs[0]);
                   1847: 
                   1848:                 for (var j = 0; j < elements.length; j++){  
                   1849:                     elements[j].style.display = pairs[1];
                   1850:                     if (pairs[1] == "none"){
                   1851:                         var regex_id = /([_\\d]+)\$/;
                   1852:                         regex_id.exec(pairs[0]);
                   1853:                         document.getElementById("folding_btn"+RegExp.\$1).value = "Show";
                   1854:                     }
                   1855:                 }
                   1856:             }
                   1857:         }
                   1858:     }
                   1859: 
                   1860:     function getTagList () {
                   1861:         
                   1862:         var stringToSearch = document.lonhomework.innerHTML;
                   1863: 
                   1864:         var ret = new Array();
                   1865:         var regex_findBlock = /(foldblock_.*?)"/g;
                   1866:         var tag_list = stringToSearch.match(regex_findBlock);
                   1867: 
                   1868:         if(tag_list != null){
                   1869:             for(var i = 0; i < tag_list.length; i++){            
                   1870:                 ret.push(tag_list[i].replace(/"/, ''));
                   1871:             }
                   1872:         }
                   1873:         return ret;
                   1874:     }
                   1875: 
                   1876:     function saveScrollPosition (resource) {
                   1877:         var tag_list = getTagList();
                   1878: 
                   1879:         // we dont always want to jump to the first block
                   1880:         // 170 is roughly above the "Problem Editing" header. we just want to save if the user scrolled down further than this
                   1881:         if(\$(window).scrollTop() > 170){
                   1882:             if(tag_list != null){
                   1883:                 var result;
                   1884:                 for(var i = 0; i < tag_list.length; i++){
                   1885:                     if(isElementInViewport(tag_list[i])){
                   1886:                         result += tag_list[i]+';';
                   1887:                     }
                   1888:                 }
                   1889:                 sessionStorage.setItem('anchor_'+resource, result);
                   1890:             }
                   1891:         } else {
                   1892:             // we dont need to save zero, just delete the item to leave everything tidy
                   1893:             sessionStorage.removeItem('anchor_'+resource);
                   1894:         }
                   1895:     }
                   1896: 
                   1897:     function restoreScrollPosition(resource){
                   1898: 
                   1899:         var elem = sessionStorage.getItem('anchor_'+resource);
                   1900:         if(elem != null){
                   1901:             var tag_list = elem.split(';');
                   1902:             var elem_list;
                   1903: 
                   1904:             for(var i = 0; i < tag_list.length; i++){
                   1905:                 elem_list = document.getElementsByName(tag_list[i]);
                   1906:                 
                   1907:                 if(elem_list.length > 0){
                   1908:                     elem = elem_list[0];
                   1909:                     break;
                   1910:                 }
                   1911:             }
                   1912:             elem.scrollIntoView();
                   1913:         }
                   1914:     }
                   1915: 
                   1916:     function isElementInViewport(el) {
                   1917: 
                   1918:         // change to last element instead of first
                   1919:         var elem = document.getElementsByName(el);
                   1920:         var rect = elem[0].getBoundingClientRect();
                   1921: 
                   1922:         return (
                   1923:             rect.top >= 0 &&
                   1924:             rect.left >= 0 &&
                   1925:             rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
                   1926:             rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
                   1927:         );
                   1928:     }
                   1929:     
                   1930:     function autosize(depth){
                   1931:         var cmInst = window['cm'+depth];
                   1932:         var fitsizeButton = document.getElementById('fitsize'+depth);
                   1933: 
                   1934:         // is fixed size, switching to dynamic
                   1935:         if (sessionStorage.getItem("autosized_"+depth) == null) {
                   1936:             cmInst.setSize("","auto");
                   1937:             fitsizeButton.value = "@{[&mt('Fixed size')]}";
                   1938:             sessionStorage.setItem("autosized_"+depth, "yes");
                   1939: 
                   1940:         // is dynamic size, switching to fixed
                   1941:         } else {
                   1942:             cmInst.setSize("","300px");
                   1943:             fitsizeButton.value = "@{[&mt('Dynamic size')]}";
                   1944:             sessionStorage.removeItem("autosized_"+depth);
                   1945:         }
                   1946:     }
                   1947: 
                   1948: 
                   1949: 
                   1950: // ]]>
                   1951: </script>
                   1952: COLORFULEDIT
                   1953: }
                   1954: 
                   1955: sub xmleditor_js {
                   1956:     return <<XMLEDIT
                   1957: <script type="text/javascript" src="/adm/jQuery/addons/jquery-scrolltofixed.js"></script>
                   1958: <script type="text/javascript">
                   1959: // <![CDATA[>
                   1960: 
                   1961:     function saveScrollPosition (resource) {
                   1962: 
                   1963:         var scrollPos = \$(window).scrollTop();
                   1964:         sessionStorage.setItem(resource,scrollPos);
                   1965:     }
                   1966: 
                   1967:     function restoreScrollPosition(resource){
                   1968: 
                   1969:         var scrollPos = sessionStorage.getItem(resource);
                   1970:         \$(window).scrollTop(scrollPos);
                   1971:     }
                   1972: 
                   1973:     // unless internet explorer
                   1974:     if (!(window.navigator.appName == "Microsoft Internet Explorer" && (document.documentMode || document.compatMode))){
                   1975: 
                   1976:         \$(document).ready(function() {
                   1977:              \$(".LC_edit_actionbar").scrollToFixed(\{zIndex: 100\});
                   1978:         });
                   1979:     }
                   1980: 
                   1981:     // inserts text at cursor position into codemirror (xml editor only)
                   1982:     function insertText(text){
                   1983:         cm.focus();
                   1984:         var curPos = cm.getCursor();
                   1985:         cm.replaceRange(text.replace(/ESCAPEDSCRIPT/g,'script'), {line: curPos.line,ch: curPos.ch});
                   1986:     }
                   1987: // ]]>
                   1988: </script>
                   1989: XMLEDIT
                   1990: }
                   1991: 
                   1992: sub insert_folding_button {
                   1993:     my $curDepth = $Apache::lonxml::curdepth;
                   1994:     my $lastresource = $env{'request.ambiguous'};
                   1995: 
                   1996:     return "<input type=\"button\" id=\"folding_btn_$curDepth\" 
                   1997:             value=\"".&mt('Hide')."\" onclick=\"fold_box('$curDepth','$lastresource')\">";
                   1998: }
                   1999: 
1.565     albertel 2000: =pod
                   2001: 
1.256     matthew  2002: =head1 Excel and CSV file utility routines
                   2003: 
                   2004: =cut
                   2005: 
                   2006: ###############################################################
                   2007: ###############################################################
                   2008: 
                   2009: =pod
                   2010: 
1.1162    raeburn  2011: =over 4
                   2012: 
1.648     raeburn  2013: =item * &csv_translate($text) 
1.37      matthew  2014: 
1.185     www      2015: Translate $text to allow it to be output as a 'comma separated values' 
1.37      matthew  2016: format.
                   2017: 
                   2018: =cut
                   2019: 
1.180     matthew  2020: ###############################################################
                   2021: ###############################################################
1.37      matthew  2022: sub csv_translate {
                   2023:     my $text = shift;
                   2024:     $text =~ s/\"/\"\"/g;
1.209     albertel 2025:     $text =~ s/\n/ /g;
1.37      matthew  2026:     return $text;
                   2027: }
1.180     matthew  2028: 
                   2029: ###############################################################
                   2030: ###############################################################
                   2031: 
                   2032: =pod
                   2033: 
1.648     raeburn  2034: =item * &define_excel_formats()
1.180     matthew  2035: 
                   2036: Define some commonly used Excel cell formats.
                   2037: 
                   2038: Currently supported formats:
                   2039: 
                   2040: =over 4
                   2041: 
                   2042: =item header
                   2043: 
                   2044: =item bold
                   2045: 
                   2046: =item h1
                   2047: 
                   2048: =item h2
                   2049: 
                   2050: =item h3
                   2051: 
1.256     matthew  2052: =item h4
                   2053: 
                   2054: =item i
                   2055: 
1.180     matthew  2056: =item date
                   2057: 
                   2058: =back
                   2059: 
                   2060: Inputs: $workbook
                   2061: 
                   2062: Returns: $format, a hash reference.
                   2063: 
1.1057    foxr     2064: 
1.180     matthew  2065: =cut
                   2066: 
                   2067: ###############################################################
                   2068: ###############################################################
                   2069: sub define_excel_formats {
                   2070:     my ($workbook) = @_;
                   2071:     my $format;
                   2072:     $format->{'header'} = $workbook->add_format(bold      => 1, 
                   2073:                                                 bottom    => 1,
                   2074:                                                 align     => 'center');
                   2075:     $format->{'bold'} = $workbook->add_format(bold=>1);
                   2076:     $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
                   2077:     $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
                   2078:     $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
1.255     matthew  2079:     $format->{'h4'}   = $workbook->add_format(bold=>1, size=>12);
1.246     matthew  2080:     $format->{'i'}    = $workbook->add_format(italic=>1);
1.180     matthew  2081:     $format->{'date'} = $workbook->add_format(num_format=>
1.207     matthew  2082:                                             'mm/dd/yyyy hh:mm:ss');
1.180     matthew  2083:     return $format;
                   2084: }
                   2085: 
                   2086: ###############################################################
                   2087: ###############################################################
1.113     bowersj2 2088: 
                   2089: =pod
                   2090: 
1.648     raeburn  2091: =item * &create_workbook()
1.255     matthew  2092: 
                   2093: Create an Excel worksheet.  If it fails, output message on the
                   2094: request object and return undefs.
                   2095: 
                   2096: Inputs: Apache request object
                   2097: 
                   2098: Returns (undef) on failure, 
                   2099:     Excel worksheet object, scalar with filename, and formats 
                   2100:     from &Apache::loncommon::define_excel_formats on success
                   2101: 
                   2102: =cut
                   2103: 
                   2104: ###############################################################
                   2105: ###############################################################
                   2106: sub create_workbook {
                   2107:     my ($r) = @_;
                   2108:         #
                   2109:     # Create the excel spreadsheet
                   2110:     my $filename = '/prtspool/'.
1.258     albertel 2111:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.255     matthew  2112:         time.'_'.rand(1000000000).'.xls';
                   2113:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
                   2114:     if (! defined($workbook)) {
                   2115:         $r->log_error("Error creating excel spreadsheet $filename: $!");
1.928     bisitz   2116:         $r->print(
                   2117:             '<p class="LC_error">'
                   2118:            .&mt('Problems occurred in creating the new Excel file.')
                   2119:            .' '.&mt('This error has been logged.')
                   2120:            .' '.&mt('Please alert your LON-CAPA administrator.')
                   2121:            .'</p>'
                   2122:         );
1.255     matthew  2123:         return (undef);
                   2124:     }
                   2125:     #
1.1014    foxr     2126:     $workbook->set_tempdir(LONCAPA::tempdir());
1.255     matthew  2127:     #
                   2128:     my $format = &Apache::loncommon::define_excel_formats($workbook);
                   2129:     return ($workbook,$filename,$format);
                   2130: }
                   2131: 
                   2132: ###############################################################
                   2133: ###############################################################
                   2134: 
                   2135: =pod
                   2136: 
1.648     raeburn  2137: =item * &create_text_file()
1.113     bowersj2 2138: 
1.542     raeburn  2139: Create a file to write to and eventually make available to the user.
1.256     matthew  2140: If file creation fails, outputs an error message on the request object and 
                   2141: return undefs.
1.113     bowersj2 2142: 
1.256     matthew  2143: Inputs: Apache request object, and file suffix
1.113     bowersj2 2144: 
1.256     matthew  2145: Returns (undef) on failure, 
                   2146:     Filehandle and filename on success.
1.113     bowersj2 2147: 
                   2148: =cut
                   2149: 
1.256     matthew  2150: ###############################################################
                   2151: ###############################################################
                   2152: sub create_text_file {
                   2153:     my ($r,$suffix) = @_;
                   2154:     if (! defined($suffix)) { $suffix = 'txt'; };
                   2155:     my $fh;
                   2156:     my $filename = '/prtspool/'.
1.258     albertel 2157:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.256     matthew  2158:         time.'_'.rand(1000000000).'.'.$suffix;
                   2159:     $fh = Apache::File->new('>/home/httpd'.$filename);
                   2160:     if (! defined($fh)) {
                   2161:         $r->log_error("Couldn't open $filename for output $!");
1.928     bisitz   2162:         $r->print(
                   2163:             '<p class="LC_error">'
                   2164:            .&mt('Problems occurred in creating the output file.')
                   2165:            .' '.&mt('This error has been logged.')
                   2166:            .' '.&mt('Please alert your LON-CAPA administrator.')
                   2167:            .'</p>'
                   2168:         );
1.113     bowersj2 2169:     }
1.256     matthew  2170:     return ($fh,$filename)
1.113     bowersj2 2171: }
                   2172: 
                   2173: 
1.256     matthew  2174: =pod 
1.113     bowersj2 2175: 
                   2176: =back
                   2177: 
                   2178: =cut
1.37      matthew  2179: 
                   2180: ###############################################################
1.33      matthew  2181: ##        Home server <option> list generating code          ##
                   2182: ###############################################################
1.35      matthew  2183: 
1.169     www      2184: # ------------------------------------------
                   2185: 
                   2186: sub domain_select {
                   2187:     my ($name,$value,$multiple)=@_;
                   2188:     my %domains=map { 
1.514     albertel 2189: 	$_ => $_.' '. &Apache::lonnet::domain($_,'description') 
1.512     albertel 2190:     } &Apache::lonnet::all_domains();
1.169     www      2191:     if ($multiple) {
                   2192: 	$domains{''}=&mt('Any domain');
1.550     albertel 2193: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
1.287     albertel 2194: 	return &multiple_select_form($name,$value,4,\%domains);
1.169     www      2195:     } else {
1.550     albertel 2196: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
1.970     raeburn  2197: 	return &select_form($name,$value,\%domains);
1.169     www      2198:     }
                   2199: }
                   2200: 
1.282     albertel 2201: #-------------------------------------------
                   2202: 
                   2203: =pod
                   2204: 
1.519     raeburn  2205: =head1 Routines for form select boxes
                   2206: 
                   2207: =over 4
                   2208: 
1.648     raeburn  2209: =item * &multiple_select_form($name,$value,$size,$hash,$order)
1.282     albertel 2210: 
                   2211: Returns a string containing a <select> element int multiple mode
                   2212: 
                   2213: 
                   2214: Args:
                   2215:   $name - name of the <select> element
1.506     raeburn  2216:   $value - scalar or array ref of values that should already be selected
1.282     albertel 2217:   $size - number of rows long the select element is
1.283     albertel 2218:   $hash - the elements should be 'option' => 'shown text'
1.282     albertel 2219:           (shown text should already have been &mt())
1.506     raeburn  2220:   $order - (optional) array ref of the order to show the elements in
1.283     albertel 2221: 
1.282     albertel 2222: =cut
                   2223: 
                   2224: #-------------------------------------------
1.169     www      2225: sub multiple_select_form {
1.284     albertel 2226:     my ($name,$value,$size,$hash,$order)=@_;
1.169     www      2227:     my %selected = map { $_ => 1 } ref($value)?@{$value}:($value);
                   2228:     my $output='';
1.191     matthew  2229:     if (! defined($size)) {
                   2230:         $size = 4;
1.283     albertel 2231:         if (scalar(keys(%$hash))<4) {
                   2232:             $size = scalar(keys(%$hash));
1.191     matthew  2233:         }
                   2234:     }
1.734     bisitz   2235:     $output.="\n".'<select name="'.$name.'" size="'.$size.'" multiple="multiple">';
1.501     banghart 2236:     my @order;
1.506     raeburn  2237:     if (ref($order) eq 'ARRAY')  {
                   2238:         @order = @{$order};
                   2239:     } else {
                   2240:         @order = sort(keys(%$hash));
1.501     banghart 2241:     }
                   2242:     if (exists($$hash{'select_form_order'})) {
                   2243:         @order = @{$$hash{'select_form_order'}};
                   2244:     }
                   2245:         
1.284     albertel 2246:     foreach my $key (@order) {
1.356     albertel 2247:         $output.='<option value="'.&HTML::Entities::encode($key,'"<>&').'" ';
1.284     albertel 2248:         $output.='selected="selected" ' if ($selected{$key});
                   2249:         $output.='>'.$hash->{$key}."</option>\n";
1.169     www      2250:     }
                   2251:     $output.="</select>\n";
                   2252:     return $output;
                   2253: }
                   2254: 
1.88      www      2255: #-------------------------------------------
                   2256: 
                   2257: =pod
                   2258: 
1.970     raeburn  2259: =item * &select_form($defdom,$name,$hashref,$onchange)
1.88      www      2260: 
                   2261: Returns a string containing a <select name='$name' size='1'> form to 
1.970     raeburn  2262: allow a user to select options from a ref to a hash containing:
                   2263: option_name => displayed text. An optional $onchange can include
                   2264: a javascript onchange item, e.g., onchange="this.form.submit();"  
                   2265: 
1.88      www      2266: See lonrights.pm for an example invocation and use.
                   2267: 
                   2268: =cut
                   2269: 
                   2270: #-------------------------------------------
                   2271: sub select_form {
1.970     raeburn  2272:     my ($def,$name,$hashref,$onchange) = @_;
                   2273:     return unless (ref($hashref) eq 'HASH');
                   2274:     if ($onchange) {
                   2275:         $onchange = ' onchange="'.$onchange.'"';
                   2276:     }
                   2277:     my $selectform = "<select name=\"$name\" size=\"1\"$onchange>\n";
1.128     albertel 2278:     my @keys;
1.970     raeburn  2279:     if (exists($hashref->{'select_form_order'})) {
                   2280: 	@keys=@{$hashref->{'select_form_order'}};
1.128     albertel 2281:     } else {
1.970     raeburn  2282: 	@keys=sort(keys(%{$hashref}));
1.128     albertel 2283:     }
1.356     albertel 2284:     foreach my $key (@keys) {
                   2285:         $selectform.=
                   2286: 	    '<option value="'.&HTML::Entities::encode($key,'"<>&').'" '.
                   2287:             ($key eq $def ? 'selected="selected" ' : '').
1.970     raeburn  2288:                 ">".$hashref->{$key}."</option>\n";
1.88      www      2289:     }
                   2290:     $selectform.="</select>";
                   2291:     return $selectform;
                   2292: }
                   2293: 
1.475     www      2294: # For display filters
                   2295: 
                   2296: sub display_filter {
1.1074    raeburn  2297:     my ($context) = @_;
1.475     www      2298:     if (!$env{'form.show'}) { $env{'form.show'}=10; }
1.477     www      2299:     if (!$env{'form.displayfilter'}) { $env{'form.displayfilter'}='currentfolder'; }
1.1074    raeburn  2300:     my $phraseinput = 'hidden';
                   2301:     my $includeinput = 'hidden';
                   2302:     my ($checked,$includetypestext);
                   2303:     if ($env{'form.displayfilter'} eq 'containing') {
                   2304:         $phraseinput = 'text'; 
                   2305:         if ($context eq 'parmslog') {
                   2306:             $includeinput = 'checkbox';
                   2307:             if ($env{'form.includetypes'}) {
                   2308:                 $checked = ' checked="checked"';
                   2309:             }
                   2310:             $includetypestext = &mt('Include parameter types');
                   2311:         }
                   2312:     } else {
                   2313:         $includetypestext = '&nbsp;';
                   2314:     }
                   2315:     my ($additional,$secondid,$thirdid);
                   2316:     if ($context eq 'parmslog') {
                   2317:         $additional = 
                   2318:             '<label><input type="'.$includeinput.'" name="includetypes"'. 
                   2319:             $checked.' name="includetypes" value="1" id="includetypes" />'.
                   2320:             '&nbsp;<span id="includetypestext">'.$includetypestext.'</span>'.
                   2321:             '</label>';
                   2322:         $secondid = 'includetypes';
                   2323:         $thirdid = 'includetypestext';
                   2324:     }
                   2325:     my $onchange = "javascript:toggleHistoryOptions(this,'containingphrase','$context',
                   2326:                                                     '$secondid','$thirdid')";
                   2327:     return '<span class="LC_nobreak"><label>'.&mt('Records: [_1]',
1.475     www      2328: 			       &Apache::lonmeta::selectbox('show',$env{'form.show'},undef,
                   2329: 							   (&mt('all'),10,20,50,100,1000,10000))).
1.714     bisitz   2330: 	   '</label></span> <span class="LC_nobreak">'.
1.1074    raeburn  2331:            &mt('Filter: [_1]',
1.477     www      2332: 	   &select_form($env{'form.displayfilter'},
                   2333: 			'displayfilter',
1.970     raeburn  2334: 			{'currentfolder' => 'Current folder/page',
1.477     www      2335: 			 'containing' => 'Containing phrase',
1.1074    raeburn  2336: 			 'none' => 'None'},$onchange)).'&nbsp;'.
                   2337: 			 '<input type="'.$phraseinput.'" name="containingphrase" id="containingphrase" size="30" value="'.
                   2338:                          &HTML::Entities::encode($env{'form.containingphrase'}).
                   2339:                          '" />'.$additional;
                   2340: }
                   2341: 
                   2342: sub display_filter_js {
                   2343:     my $includetext = &mt('Include parameter types');
                   2344:     return <<"ENDJS";
                   2345:   
                   2346: function toggleHistoryOptions(setter,firstid,context,secondid,thirdid) {
                   2347:     var firstType = 'hidden';
                   2348:     if (setter.options[setter.selectedIndex].value == 'containing') {
                   2349:         firstType = 'text';
                   2350:     }
                   2351:     firstObject = document.getElementById(firstid);
                   2352:     if (typeof(firstObject) == 'object') {
                   2353:         if (firstObject.type != firstType) {
                   2354:             changeInputType(firstObject,firstType);
                   2355:         }
                   2356:     }
                   2357:     if (context == 'parmslog') {
                   2358:         var secondType = 'hidden';
                   2359:         if (firstType == 'text') {
                   2360:             secondType = 'checkbox';
                   2361:         }
                   2362:         secondObject = document.getElementById(secondid);  
                   2363:         if (typeof(secondObject) == 'object') {
                   2364:             if (secondObject.type != secondType) {
                   2365:                 changeInputType(secondObject,secondType);
                   2366:             }
                   2367:         }
                   2368:         var textItem = document.getElementById(thirdid);
                   2369:         var currtext = textItem.innerHTML;
                   2370:         var newtext;
                   2371:         if (firstType == 'text') {
                   2372:             newtext = '$includetext';
                   2373:         } else {
                   2374:             newtext = '&nbsp;';
                   2375:         }
                   2376:         if (currtext != newtext) {
                   2377:             textItem.innerHTML = newtext;
                   2378:         }
                   2379:     }
                   2380:     return;
                   2381: }
                   2382: 
                   2383: function changeInputType(oldObject,newType) {
                   2384:     var newObject = document.createElement('input');
                   2385:     newObject.type = newType;
                   2386:     if (oldObject.size) {
                   2387:         newObject.size = oldObject.size;
                   2388:     }
                   2389:     if (oldObject.value) {
                   2390:         newObject.value = oldObject.value;
                   2391:     }
                   2392:     if (oldObject.name) {
                   2393:         newObject.name = oldObject.name;
                   2394:     }
                   2395:     if (oldObject.id) {
                   2396:         newObject.id = oldObject.id;
                   2397:     }
                   2398:     oldObject.parentNode.replaceChild(newObject,oldObject);
                   2399:     return;
                   2400: }
                   2401: 
                   2402: ENDJS
1.475     www      2403: }
                   2404: 
1.167     www      2405: sub gradeleveldescription {
                   2406:     my $gradelevel=shift;
                   2407:     my %gradelevels=(0 => 'Not specified',
                   2408: 		     1 => 'Grade 1',
                   2409: 		     2 => 'Grade 2',
                   2410: 		     3 => 'Grade 3',
                   2411: 		     4 => 'Grade 4',
                   2412: 		     5 => 'Grade 5',
                   2413: 		     6 => 'Grade 6',
                   2414: 		     7 => 'Grade 7',
                   2415: 		     8 => 'Grade 8',
                   2416: 		     9 => 'Grade 9',
                   2417: 		     10 => 'Grade 10',
                   2418: 		     11 => 'Grade 11',
                   2419: 		     12 => 'Grade 12',
                   2420: 		     13 => 'Grade 13',
                   2421: 		     14 => '100 Level',
                   2422: 		     15 => '200 Level',
                   2423: 		     16 => '300 Level',
                   2424: 		     17 => '400 Level',
                   2425: 		     18 => 'Graduate Level');
                   2426:     return &mt($gradelevels{$gradelevel});
                   2427: }
                   2428: 
1.163     www      2429: sub select_level_form {
                   2430:     my ($deflevel,$name)=@_;
                   2431:     unless ($deflevel) { $deflevel=0; }
1.167     www      2432:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
                   2433:     for (my $i=0; $i<=18; $i++) {
                   2434:         $selectform.="<option value=\"$i\" ".
1.253     albertel 2435:             ($i==$deflevel ? 'selected="selected" ' : '').
1.167     www      2436:                 ">".&gradeleveldescription($i)."</option>\n";
                   2437:     }
                   2438:     $selectform.="</select>";
                   2439:     return $selectform;
1.163     www      2440: }
1.167     www      2441: 
1.35      matthew  2442: #-------------------------------------------
                   2443: 
1.45      matthew  2444: =pod
                   2445: 
1.1121    raeburn  2446: =item * &select_dom_form($defdom,$name,$includeempty,$showdomdesc,$onchange,$incdoms,$excdoms)
1.35      matthew  2447: 
                   2448: Returns a string containing a <select name='$name' size='1'> form to 
                   2449: allow a user to select the domain to preform an operation in.  
                   2450: See loncreateuser.pm for an example invocation and use.
                   2451: 
1.90      www      2452: If the $includeempty flag is set, it also includes an empty choice ("no domain
                   2453: selected");
                   2454: 
1.743     raeburn  2455: If the $showdomdesc flag is set, the domain name is followed by the domain description.
                   2456: 
1.910     raeburn  2457: 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.
                   2458: 
1.1121    raeburn  2459: The optional $incdoms is a reference to an array of domains which will be the only available options.
                   2460: 
                   2461: The optional $excdoms is a reference to an array of domains which will be excluded from the available options.
1.563     raeburn  2462: 
1.35      matthew  2463: =cut
                   2464: 
                   2465: #-------------------------------------------
1.34      matthew  2466: sub select_dom_form {
1.1121    raeburn  2467:     my ($defdom,$name,$includeempty,$showdomdesc,$onchange,$incdoms,$excdoms) = @_;
1.872     raeburn  2468:     if ($onchange) {
1.874     raeburn  2469:         $onchange = ' onchange="'.$onchange.'"';
1.743     raeburn  2470:     }
1.1121    raeburn  2471:     my (@domains,%exclude);
1.910     raeburn  2472:     if (ref($incdoms) eq 'ARRAY') {
                   2473:         @domains = sort {lc($a) cmp lc($b)} (@{$incdoms});
                   2474:     } else {
                   2475:         @domains = sort {lc($a) cmp lc($b)} (&Apache::lonnet::all_domains());
                   2476:     }
1.90      www      2477:     if ($includeempty) { @domains=('',@domains); }
1.1121    raeburn  2478:     if (ref($excdoms) eq 'ARRAY') {
                   2479:         map { $exclude{$_} = 1; } @{$excdoms}; 
                   2480:     }
1.743     raeburn  2481:     my $selectdomain = "<select name=\"$name\" size=\"1\"$onchange>\n";
1.356     albertel 2482:     foreach my $dom (@domains) {
1.1121    raeburn  2483:         next if ($exclude{$dom});
1.356     albertel 2484:         $selectdomain.="<option value=\"$dom\" ".
1.563     raeburn  2485:             ($dom eq $defdom ? 'selected="selected" ' : '').'>'.$dom;
                   2486:         if ($showdomdesc) {
                   2487:             if ($dom ne '') {
                   2488:                 my $domdesc = &Apache::lonnet::domain($dom,'description');
                   2489:                 if ($domdesc ne '') {
                   2490:                     $selectdomain .= ' ('.$domdesc.')';
                   2491:                 }
                   2492:             } 
                   2493:         }
                   2494:         $selectdomain .= "</option>\n";
1.34      matthew  2495:     }
                   2496:     $selectdomain.="</select>";
                   2497:     return $selectdomain;
                   2498: }
                   2499: 
1.35      matthew  2500: #-------------------------------------------
                   2501: 
1.45      matthew  2502: =pod
                   2503: 
1.648     raeburn  2504: =item * &home_server_form_item($domain,$name,$defaultflag)
1.35      matthew  2505: 
1.586     raeburn  2506: input: 4 arguments (two required, two optional) - 
                   2507:     $domain - domain of new user
                   2508:     $name - name of form element
                   2509:     $default - Value of 'default' causes a default item to be first 
                   2510:                             option, and selected by default. 
                   2511:     $hide - Value of 'hide' causes hiding of the name of the server, 
                   2512:                             if 1 server found, or default, if 0 found.
1.594     raeburn  2513: output: returns 2 items: 
1.586     raeburn  2514: (a) form element which contains either:
                   2515:    (i) <select name="$name">
                   2516:         <option value="$hostid1">$hostid $servers{$hostid}</option>
                   2517:         <option value="$hostid2">$hostid $servers{$hostid}</option>       
                   2518:        </select>
                   2519:        form item if there are multiple library servers in $domain, or
                   2520:    (ii) an <input type="hidden" name="$name" value="$hostid" /> form item 
                   2521:        if there is only one library server in $domain.
                   2522: 
                   2523: (b) number of library servers found.
                   2524: 
                   2525: See loncreateuser.pm for example of use.
1.35      matthew  2526: 
                   2527: =cut
                   2528: 
                   2529: #-------------------------------------------
1.586     raeburn  2530: sub home_server_form_item {
                   2531:     my ($domain,$name,$default,$hide) = @_;
1.513     albertel 2532:     my %servers = &Apache::lonnet::get_servers($domain,'library');
1.586     raeburn  2533:     my $result;
                   2534:     my $numlib = keys(%servers);
                   2535:     if ($numlib > 1) {
                   2536:         $result .= '<select name="'.$name.'" />'."\n";
                   2537:         if ($default) {
1.804     bisitz   2538:             $result .= '<option value="default" selected="selected">'.&mt('default').
1.586     raeburn  2539:                        '</option>'."\n";
                   2540:         }
                   2541:         foreach my $hostid (sort(keys(%servers))) {
                   2542:             $result.= '<option value="'.$hostid.'">'.
                   2543: 	              $hostid.' '.$servers{$hostid}."</option>\n";
                   2544:         }
                   2545:         $result .= '</select>'."\n";
                   2546:     } elsif ($numlib == 1) {
                   2547:         my $hostid;
                   2548:         foreach my $item (keys(%servers)) {
                   2549:             $hostid = $item;
                   2550:         }
                   2551:         $result .= '<input type="hidden" name="'.$name.'" value="'.
                   2552:                    $hostid.'" />';
                   2553:                    if (!$hide) {
                   2554:                        $result .= $hostid.' '.$servers{$hostid};
                   2555:                    }
                   2556:                    $result .= "\n";
                   2557:     } elsif ($default) {
                   2558:         $result .= '<input type="hidden" name="'.$name.
                   2559:                    '" value="default" />';
                   2560:                    if (!$hide) {
                   2561:                        $result .= &mt('default');
                   2562:                    }
                   2563:                    $result .= "\n";
1.33      matthew  2564:     }
1.586     raeburn  2565:     return ($result,$numlib);
1.33      matthew  2566: }
1.112     bowersj2 2567: 
                   2568: =pod
                   2569: 
1.534     albertel 2570: =back 
                   2571: 
1.112     bowersj2 2572: =cut
1.87      matthew  2573: 
                   2574: ###############################################################
1.112     bowersj2 2575: ##                  Decoding User Agent                      ##
1.87      matthew  2576: ###############################################################
                   2577: 
                   2578: =pod
                   2579: 
1.112     bowersj2 2580: =head1 Decoding the User Agent
                   2581: 
                   2582: =over 4
                   2583: 
                   2584: =item * &decode_user_agent()
1.87      matthew  2585: 
                   2586: Inputs: $r
                   2587: 
                   2588: Outputs:
                   2589: 
                   2590: =over 4
                   2591: 
1.112     bowersj2 2592: =item * $httpbrowser
1.87      matthew  2593: 
1.112     bowersj2 2594: =item * $clientbrowser
1.87      matthew  2595: 
1.112     bowersj2 2596: =item * $clientversion
1.87      matthew  2597: 
1.112     bowersj2 2598: =item * $clientmathml
1.87      matthew  2599: 
1.112     bowersj2 2600: =item * $clientunicode
1.87      matthew  2601: 
1.112     bowersj2 2602: =item * $clientos
1.87      matthew  2603: 
1.1137    raeburn  2604: =item * $clientmobile
                   2605: 
1.1141    raeburn  2606: =item * $clientinfo
                   2607: 
1.1194    raeburn  2608: =item * $clientosversion
                   2609: 
1.87      matthew  2610: =back
                   2611: 
1.157     matthew  2612: =back 
                   2613: 
1.87      matthew  2614: =cut
                   2615: 
                   2616: ###############################################################
                   2617: ###############################################################
                   2618: sub decode_user_agent {
1.247     albertel 2619:     my ($r)=@_;
1.87      matthew  2620:     my @browsertype=split(/\&/,$Apache::lonnet::perlvar{"lonBrowsDet"});
                   2621:     my %mathcap=split(/\&/,$$Apache::lonnet::perlvar{"lonMathML"});
                   2622:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
1.247     albertel 2623:     if (!$httpbrowser && $r) { $httpbrowser=$r->header_in('User-Agent'); }
1.87      matthew  2624:     my $clientbrowser='unknown';
                   2625:     my $clientversion='0';
                   2626:     my $clientmathml='';
                   2627:     my $clientunicode='0';
1.1137    raeburn  2628:     my $clientmobile=0;
1.1194    raeburn  2629:     my $clientosversion='';
1.87      matthew  2630:     for (my $i=0;$i<=$#browsertype;$i++) {
1.1193    raeburn  2631:         my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\%/,$browsertype[$i]);
1.87      matthew  2632: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
                   2633: 	    $clientbrowser=$bname;
                   2634:             $httpbrowser=~/$vreg/i;
                   2635: 	    $clientversion=$1;
                   2636:             $clientmathml=($clientversion>=$minv);
                   2637:             $clientunicode=($clientversion>=$univ);
                   2638: 	}
                   2639:     }
                   2640:     my $clientos='unknown';
1.1141    raeburn  2641:     my $clientinfo;
1.87      matthew  2642:     if (($httpbrowser=~/linux/i) ||
                   2643:         ($httpbrowser=~/unix/i) ||
                   2644:         ($httpbrowser=~/ux/i) ||
                   2645:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
                   2646:     if (($httpbrowser=~/vax/i) ||
                   2647:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
                   2648:     if ($httpbrowser=~/next/i) { $clientos='next'; }
                   2649:     if (($httpbrowser=~/mac/i) ||
                   2650:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
1.1194    raeburn  2651:     if ($httpbrowser=~/win/i) {
                   2652:         $clientos='win';
                   2653:         if ($httpbrowser =~/Windows\s+NT\s+(\d+\.\d+)/i) {
                   2654:             $clientosversion = $1;
                   2655:         }
                   2656:     }
1.87      matthew  2657:     if ($httpbrowser=~/embed/i) { $clientos='pda'; }
1.1137    raeburn  2658:     if ($httpbrowser=~/(Android|iPod|iPad|iPhone|webOS|Blackberry|Windows Phone|Opera m(?:ob|in)|Fennec)/i) {
                   2659:         $clientmobile=lc($1);
                   2660:     }
1.1141    raeburn  2661:     if ($httpbrowser=~ m{Firefox/(\d+\.\d+)}) {
                   2662:         $clientinfo = 'firefox-'.$1;
                   2663:     } elsif ($httpbrowser=~ m{chromeframe/(\d+\.\d+)\.}) {
                   2664:         $clientinfo = 'chromeframe-'.$1;
                   2665:     }
1.87      matthew  2666:     return ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
1.1194    raeburn  2667:             $clientunicode,$clientos,$clientmobile,$clientinfo,
                   2668:             $clientosversion);
1.87      matthew  2669: }
                   2670: 
1.32      matthew  2671: ###############################################################
                   2672: ##    Authentication changing form generation subroutines    ##
                   2673: ###############################################################
                   2674: ##
                   2675: ## All of the authform_xxxxxxx subroutines take their inputs in a
                   2676: ## hash, and have reasonable default values.
                   2677: ##
                   2678: ##    formname = the name given in the <form> tag.
1.35      matthew  2679: #-------------------------------------------
                   2680: 
1.45      matthew  2681: =pod
                   2682: 
1.112     bowersj2 2683: =head1 Authentication Routines
                   2684: 
                   2685: =over 4
                   2686: 
1.648     raeburn  2687: =item * &authform_xxxxxx()
1.35      matthew  2688: 
                   2689: The authform_xxxxxx subroutines provide javascript and html forms which 
                   2690: handle some of the conveniences required for authentication forms.  
                   2691: This is not an optimal method, but it works.  
                   2692: 
                   2693: =over 4
                   2694: 
1.112     bowersj2 2695: =item * authform_header
1.35      matthew  2696: 
1.112     bowersj2 2697: =item * authform_authorwarning
1.35      matthew  2698: 
1.112     bowersj2 2699: =item * authform_nochange
1.35      matthew  2700: 
1.112     bowersj2 2701: =item * authform_kerberos
1.35      matthew  2702: 
1.112     bowersj2 2703: =item * authform_internal
1.35      matthew  2704: 
1.112     bowersj2 2705: =item * authform_filesystem
1.35      matthew  2706: 
                   2707: =back
                   2708: 
1.648     raeburn  2709: See loncreateuser.pm for invocation and use examples.
1.157     matthew  2710: 
1.35      matthew  2711: =cut
                   2712: 
                   2713: #-------------------------------------------
1.32      matthew  2714: sub authform_header{  
                   2715:     my %in = (
                   2716:         formname => 'cu',
1.80      albertel 2717:         kerb_def_dom => '',
1.32      matthew  2718:         @_,
                   2719:     );
                   2720:     $in{'formname'} = 'document.' . $in{'formname'};
                   2721:     my $result='';
1.80      albertel 2722: 
                   2723: #---------------------------------------------- Code for upper case translation
                   2724:     my $Javascript_toUpperCase;
                   2725:     unless ($in{kerb_def_dom}) {
                   2726:         $Javascript_toUpperCase =<<"END";
                   2727:         switch (choice) {
                   2728:            case 'krb': currentform.elements[choicearg].value =
                   2729:                currentform.elements[choicearg].value.toUpperCase();
                   2730:                break;
                   2731:            default:
                   2732:         }
                   2733: END
                   2734:     } else {
                   2735:         $Javascript_toUpperCase = "";
                   2736:     }
                   2737: 
1.165     raeburn  2738:     my $radioval = "'nochange'";
1.591     raeburn  2739:     if (defined($in{'curr_authtype'})) {
                   2740:         if ($in{'curr_authtype'} ne '') {
                   2741:             $radioval = "'".$in{'curr_authtype'}."arg'";
                   2742:         }
1.174     matthew  2743:     }
1.165     raeburn  2744:     my $argfield = 'null';
1.591     raeburn  2745:     if (defined($in{'mode'})) {
1.165     raeburn  2746:         if ($in{'mode'} eq 'modifycourse')  {
1.591     raeburn  2747:             if (defined($in{'curr_autharg'})) {
                   2748:                 if ($in{'curr_autharg'} ne '') {
1.165     raeburn  2749:                     $argfield = "'$in{'curr_autharg'}'";
                   2750:                 }
                   2751:             }
                   2752:         }
                   2753:     }
                   2754: 
1.32      matthew  2755:     $result.=<<"END";
                   2756: var current = new Object();
1.165     raeburn  2757: current.radiovalue = $radioval;
                   2758: current.argfield = $argfield;
1.32      matthew  2759: 
                   2760: function changed_radio(choice,currentform) {
                   2761:     var choicearg = choice + 'arg';
                   2762:     // If a radio button in changed, we need to change the argfield
                   2763:     if (current.radiovalue != choice) {
                   2764:         current.radiovalue = choice;
                   2765:         if (current.argfield != null) {
                   2766:             currentform.elements[current.argfield].value = '';
                   2767:         }
                   2768:         if (choice == 'nochange') {
                   2769:             current.argfield = null;
                   2770:         } else {
                   2771:             current.argfield = choicearg;
                   2772:             switch(choice) {
                   2773:                 case 'krb': 
                   2774:                     currentform.elements[current.argfield].value = 
                   2775:                         "$in{'kerb_def_dom'}";
                   2776:                 break;
                   2777:               default:
                   2778:                 break;
                   2779:             }
                   2780:         }
                   2781:     }
                   2782:     return;
                   2783: }
1.22      www      2784: 
1.32      matthew  2785: function changed_text(choice,currentform) {
                   2786:     var choicearg = choice + 'arg';
                   2787:     if (currentform.elements[choicearg].value !='') {
1.80      albertel 2788:         $Javascript_toUpperCase
1.32      matthew  2789:         // clear old field
                   2790:         if ((current.argfield != choicearg) && (current.argfield != null)) {
                   2791:             currentform.elements[current.argfield].value = '';
                   2792:         }
                   2793:         current.argfield = choicearg;
                   2794:     }
                   2795:     set_auth_radio_buttons(choice,currentform);
                   2796:     return;
1.20      www      2797: }
1.32      matthew  2798: 
                   2799: function set_auth_radio_buttons(newvalue,currentform) {
1.986     raeburn  2800:     var numauthchoices = currentform.login.length;
                   2801:     if (typeof numauthchoices  == "undefined") {
                   2802:         return;
                   2803:     } 
1.32      matthew  2804:     var i=0;
1.986     raeburn  2805:     while (i < numauthchoices) {
1.32      matthew  2806:         if (currentform.login[i].value == newvalue) { break; }
                   2807:         i++;
                   2808:     }
1.986     raeburn  2809:     if (i == numauthchoices) {
1.32      matthew  2810:         return;
                   2811:     }
                   2812:     current.radiovalue = newvalue;
                   2813:     currentform.login[i].checked = true;
                   2814:     return;
                   2815: }
                   2816: END
                   2817:     return $result;
                   2818: }
                   2819: 
1.1106    raeburn  2820: sub authform_authorwarning {
1.32      matthew  2821:     my $result='';
1.144     matthew  2822:     $result='<i>'.
                   2823:         &mt('As a general rule, only authors or co-authors should be '.
                   2824:             'filesystem authenticated '.
                   2825:             '(which allows access to the server filesystem).')."</i>\n";
1.32      matthew  2826:     return $result;
                   2827: }
                   2828: 
1.1106    raeburn  2829: sub authform_nochange {
1.32      matthew  2830:     my %in = (
                   2831:               formname => 'document.cu',
                   2832:               kerb_def_dom => 'MSU.EDU',
                   2833:               @_,
                   2834:           );
1.1106    raeburn  2835:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.586     raeburn  2836:     my $result;
1.1104    raeburn  2837:     if (!$authnum) {
1.1105    raeburn  2838:         $result = &mt('Under your current role you are not permitted to change login settings for this user');
1.586     raeburn  2839:     } else {
                   2840:         $result = '<label>'.&mt('[_1] Do not change login data',
                   2841:                   '<input type="radio" name="login" value="nochange" '.
                   2842:                   'checked="checked" onclick="'.
1.281     albertel 2843:             "javascript:changed_radio('nochange',$in{'formname'});".'" />').
                   2844: 	    '</label>';
1.586     raeburn  2845:     }
1.32      matthew  2846:     return $result;
                   2847: }
                   2848: 
1.591     raeburn  2849: sub authform_kerberos {
1.32      matthew  2850:     my %in = (
                   2851:               formname => 'document.cu',
                   2852:               kerb_def_dom => 'MSU.EDU',
1.80      albertel 2853:               kerb_def_auth => 'krb4',
1.32      matthew  2854:               @_,
                   2855:               );
1.586     raeburn  2856:     my ($check4,$check5,$krbcheck,$krbarg,$krbver,$result,$authtype,
                   2857:         $autharg,$jscall);
1.1106    raeburn  2858:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.80      albertel 2859:     if ($in{'kerb_def_auth'} eq 'krb5') {
1.772     bisitz   2860:        $check5 = ' checked="checked"';
1.80      albertel 2861:     } else {
1.772     bisitz   2862:        $check4 = ' checked="checked"';
1.80      albertel 2863:     }
1.165     raeburn  2864:     $krbarg = $in{'kerb_def_dom'};
1.591     raeburn  2865:     if (defined($in{'curr_authtype'})) {
                   2866:         if ($in{'curr_authtype'} eq 'krb') {
1.772     bisitz   2867:             $krbcheck = ' checked="checked"';
1.623     raeburn  2868:             if (defined($in{'mode'})) {
                   2869:                 if ($in{'mode'} eq 'modifyuser') {
                   2870:                     $krbcheck = '';
                   2871:                 }
                   2872:             }
1.591     raeburn  2873:             if (defined($in{'curr_kerb_ver'})) {
                   2874:                 if ($in{'curr_krb_ver'} eq '5') {
1.772     bisitz   2875:                     $check5 = ' checked="checked"';
1.591     raeburn  2876:                     $check4 = '';
                   2877:                 } else {
1.772     bisitz   2878:                     $check4 = ' checked="checked"';
1.591     raeburn  2879:                     $check5 = '';
                   2880:                 }
1.586     raeburn  2881:             }
1.591     raeburn  2882:             if (defined($in{'curr_autharg'})) {
1.165     raeburn  2883:                 $krbarg = $in{'curr_autharg'};
                   2884:             }
1.586     raeburn  2885:             if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
1.591     raeburn  2886:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2887:                     $result = 
                   2888:     &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
                   2889:         $in{'curr_autharg'},$krbver);
                   2890:                 } else {
                   2891:                     $result =
                   2892:     &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
                   2893:                 }
                   2894:                 return $result; 
                   2895:             }
                   2896:         }
                   2897:     } else {
                   2898:         if ($authnum == 1) {
1.784     bisitz   2899:             $authtype = '<input type="hidden" name="login" value="krb" />';
1.165     raeburn  2900:         }
                   2901:     }
1.586     raeburn  2902:     if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
                   2903:         return;
1.587     raeburn  2904:     } elsif ($authtype eq '') {
1.591     raeburn  2905:         if (defined($in{'mode'})) {
1.587     raeburn  2906:             if ($in{'mode'} eq 'modifycourse') {
                   2907:                 if ($authnum == 1) {
1.1104    raeburn  2908:                     $authtype = '<input type="radio" name="login" value="krb" />';
1.587     raeburn  2909:                 }
                   2910:             }
                   2911:         }
1.586     raeburn  2912:     }
                   2913:     $jscall = "javascript:changed_radio('krb',$in{'formname'});";
                   2914:     if ($authtype eq '') {
                   2915:         $authtype = '<input type="radio" name="login" value="krb" '.
                   2916:                     'onclick="'.$jscall.'" onchange="'.$jscall.'"'.
                   2917:                     $krbcheck.' />';
                   2918:     }
                   2919:     if (($can_assign{'krb4'} && $can_assign{'krb5'}) ||
1.1106    raeburn  2920:         ($can_assign{'krb4'} && !$can_assign{'krb5'} &&
1.586     raeburn  2921:          $in{'curr_authtype'} eq 'krb5') ||
1.1106    raeburn  2922:         (!$can_assign{'krb4'} && $can_assign{'krb5'} &&
1.586     raeburn  2923:          $in{'curr_authtype'} eq 'krb4')) {
                   2924:         $result .= &mt
1.144     matthew  2925:         ('[_1] Kerberos authenticated with domain [_2] '.
1.281     albertel 2926:          '[_3] Version 4 [_4] Version 5 [_5]',
1.586     raeburn  2927:          '<label>'.$authtype,
1.281     albertel 2928:          '</label><input type="text" size="10" name="krbarg" '.
1.165     raeburn  2929:              'value="'.$krbarg.'" '.
1.144     matthew  2930:              'onchange="'.$jscall.'" />',
1.281     albertel 2931:          '<label><input type="radio" name="krbver" value="4" '.$check4.' />',
                   2932:          '</label><label><input type="radio" name="krbver" value="5" '.$check5.' />',
                   2933: 	 '</label>');
1.586     raeburn  2934:     } elsif ($can_assign{'krb4'}) {
                   2935:         $result .= &mt
                   2936:         ('[_1] Kerberos authenticated with domain [_2] '.
                   2937:          '[_3] Version 4 [_4]',
                   2938:          '<label>'.$authtype,
                   2939:          '</label><input type="text" size="10" name="krbarg" '.
                   2940:              'value="'.$krbarg.'" '.
                   2941:              'onchange="'.$jscall.'" />',
                   2942:          '<label><input type="hidden" name="krbver" value="4" />',
                   2943:          '</label>');
                   2944:     } elsif ($can_assign{'krb5'}) {
                   2945:         $result .= &mt
                   2946:         ('[_1] Kerberos authenticated with domain [_2] '.
                   2947:          '[_3] Version 5 [_4]',
                   2948:          '<label>'.$authtype,
                   2949:          '</label><input type="text" size="10" name="krbarg" '.
                   2950:              'value="'.$krbarg.'" '.
                   2951:              'onchange="'.$jscall.'" />',
                   2952:          '<label><input type="hidden" name="krbver" value="5" />',
                   2953:          '</label>');
                   2954:     }
1.32      matthew  2955:     return $result;
                   2956: }
                   2957: 
1.1106    raeburn  2958: sub authform_internal {
1.586     raeburn  2959:     my %in = (
1.32      matthew  2960:                 formname => 'document.cu',
                   2961:                 kerb_def_dom => 'MSU.EDU',
                   2962:                 @_,
                   2963:                 );
1.586     raeburn  2964:     my ($intcheck,$intarg,$result,$authtype,$autharg,$jscall);
1.1106    raeburn  2965:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.591     raeburn  2966:     if (defined($in{'curr_authtype'})) {
                   2967:         if ($in{'curr_authtype'} eq 'int') {
1.586     raeburn  2968:             if ($can_assign{'int'}) {
1.772     bisitz   2969:                 $intcheck = 'checked="checked" ';
1.623     raeburn  2970:                 if (defined($in{'mode'})) {
                   2971:                     if ($in{'mode'} eq 'modifyuser') {
                   2972:                         $intcheck = '';
                   2973:                     }
                   2974:                 }
1.591     raeburn  2975:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2976:                     $intarg = $in{'curr_autharg'};
                   2977:                 }
                   2978:             } else {
                   2979:                 $result = &mt('Currently internally authenticated.');
                   2980:                 return $result;
1.165     raeburn  2981:             }
                   2982:         }
1.586     raeburn  2983:     } else {
                   2984:         if ($authnum == 1) {
1.784     bisitz   2985:             $authtype = '<input type="hidden" name="login" value="int" />';
1.586     raeburn  2986:         }
                   2987:     }
                   2988:     if (!$can_assign{'int'}) {
                   2989:         return;
1.587     raeburn  2990:     } elsif ($authtype eq '') {
1.591     raeburn  2991:         if (defined($in{'mode'})) {
1.587     raeburn  2992:             if ($in{'mode'} eq 'modifycourse') {
                   2993:                 if ($authnum == 1) {
1.1104    raeburn  2994:                     $authtype = '<input type="radio" name="login" value="int" />';
1.587     raeburn  2995:                 }
                   2996:             }
                   2997:         }
1.165     raeburn  2998:     }
1.586     raeburn  2999:     $jscall = "javascript:changed_radio('int',$in{'formname'});";
                   3000:     if ($authtype eq '') {
                   3001:         $authtype = '<input type="radio" name="login" value="int" '.$intcheck.
                   3002:                     ' onchange="'.$jscall.'" onclick="'.$jscall.'" />';
                   3003:     }
1.605     bisitz   3004:     $autharg = '<input type="password" size="10" name="intarg" value="'.
1.586     raeburn  3005:                $intarg.'" onchange="'.$jscall.'" />';
                   3006:     $result = &mt
1.144     matthew  3007:         ('[_1] Internally authenticated (with initial password [_2])',
1.586     raeburn  3008:          '<label>'.$authtype,'</label>'.$autharg);
1.824     bisitz   3009:     $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  3010:     return $result;
                   3011: }
                   3012: 
1.1104    raeburn  3013: sub authform_local {
1.32      matthew  3014:     my %in = (
                   3015:               formname => 'document.cu',
                   3016:               kerb_def_dom => 'MSU.EDU',
                   3017:               @_,
                   3018:               );
1.586     raeburn  3019:     my ($loccheck,$locarg,$result,$authtype,$autharg,$jscall);
1.1106    raeburn  3020:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.591     raeburn  3021:     if (defined($in{'curr_authtype'})) {
                   3022:         if ($in{'curr_authtype'} eq 'loc') {
1.586     raeburn  3023:             if ($can_assign{'loc'}) {
1.772     bisitz   3024:                 $loccheck = 'checked="checked" ';
1.623     raeburn  3025:                 if (defined($in{'mode'})) {
                   3026:                     if ($in{'mode'} eq 'modifyuser') {
                   3027:                         $loccheck = '';
                   3028:                     }
                   3029:                 }
1.591     raeburn  3030:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  3031:                     $locarg = $in{'curr_autharg'};
                   3032:                 }
                   3033:             } else {
                   3034:                 $result = &mt('Currently using local (institutional) authentication.');
                   3035:                 return $result;
1.165     raeburn  3036:             }
                   3037:         }
1.586     raeburn  3038:     } else {
                   3039:         if ($authnum == 1) {
1.784     bisitz   3040:             $authtype = '<input type="hidden" name="login" value="loc" />';
1.586     raeburn  3041:         }
                   3042:     }
                   3043:     if (!$can_assign{'loc'}) {
                   3044:         return;
1.587     raeburn  3045:     } elsif ($authtype eq '') {
1.591     raeburn  3046:         if (defined($in{'mode'})) {
1.587     raeburn  3047:             if ($in{'mode'} eq 'modifycourse') {
                   3048:                 if ($authnum == 1) {
1.1104    raeburn  3049:                     $authtype = '<input type="radio" name="login" value="loc" />';
1.587     raeburn  3050:                 }
                   3051:             }
                   3052:         }
1.165     raeburn  3053:     }
1.586     raeburn  3054:     $jscall = "javascript:changed_radio('loc',$in{'formname'});";
                   3055:     if ($authtype eq '') {
                   3056:         $authtype = '<input type="radio" name="login" value="loc" '.
                   3057:                     $loccheck.' onchange="'.$jscall.'" onclick="'.
                   3058:                     $jscall.'" />';
                   3059:     }
                   3060:     $autharg = '<input type="text" size="10" name="locarg" value="'.
                   3061:                $locarg.'" onchange="'.$jscall.'" />';
                   3062:     $result = &mt('[_1] Local Authentication with argument [_2]',
                   3063:                   '<label>'.$authtype,'</label>'.$autharg);
1.32      matthew  3064:     return $result;
                   3065: }
                   3066: 
1.1106    raeburn  3067: sub authform_filesystem {
1.32      matthew  3068:     my %in = (
                   3069:               formname => 'document.cu',
                   3070:               kerb_def_dom => 'MSU.EDU',
                   3071:               @_,
                   3072:               );
1.586     raeburn  3073:     my ($fsyscheck,$result,$authtype,$autharg,$jscall);
1.1106    raeburn  3074:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
1.591     raeburn  3075:     if (defined($in{'curr_authtype'})) {
                   3076:         if ($in{'curr_authtype'} eq 'fsys') {
1.586     raeburn  3077:             if ($can_assign{'fsys'}) {
1.772     bisitz   3078:                 $fsyscheck = 'checked="checked" ';
1.623     raeburn  3079:                 if (defined($in{'mode'})) {
                   3080:                     if ($in{'mode'} eq 'modifyuser') {
                   3081:                         $fsyscheck = '';
                   3082:                     }
                   3083:                 }
1.586     raeburn  3084:             } else {
                   3085:                 $result = &mt('Currently Filesystem Authenticated.');
                   3086:                 return $result;
                   3087:             }           
                   3088:         }
                   3089:     } else {
                   3090:         if ($authnum == 1) {
1.784     bisitz   3091:             $authtype = '<input type="hidden" name="login" value="fsys" />';
1.586     raeburn  3092:         }
                   3093:     }
                   3094:     if (!$can_assign{'fsys'}) {
                   3095:         return;
1.587     raeburn  3096:     } elsif ($authtype eq '') {
1.591     raeburn  3097:         if (defined($in{'mode'})) {
1.587     raeburn  3098:             if ($in{'mode'} eq 'modifycourse') {
                   3099:                 if ($authnum == 1) {
1.1104    raeburn  3100:                     $authtype = '<input type="radio" name="login" value="fsys" />';
1.587     raeburn  3101:                 }
                   3102:             }
                   3103:         }
1.586     raeburn  3104:     }
                   3105:     $jscall = "javascript:changed_radio('fsys',$in{'formname'});";
                   3106:     if ($authtype eq '') {
                   3107:         $authtype = '<input type="radio" name="login" value="fsys" '.
                   3108:                     $fsyscheck.' onchange="'.$jscall.'" onclick="'.
                   3109:                     $jscall.'" />';
                   3110:     }
                   3111:     $autharg = '<input type="text" size="10" name="fsysarg" value=""'.
                   3112:                ' onchange="'.$jscall.'" />';
                   3113:     $result = &mt
1.144     matthew  3114:         ('[_1] Filesystem Authenticated (with initial password [_2])',
1.281     albertel 3115:          '<label><input type="radio" name="login" value="fsys" '.
1.586     raeburn  3116:          $fsyscheck.'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1.605     bisitz   3117:          '</label><input type="password" size="10" name="fsysarg" value="" '.
1.144     matthew  3118:                   'onchange="'.$jscall.'" />');
1.32      matthew  3119:     return $result;
                   3120: }
                   3121: 
1.586     raeburn  3122: sub get_assignable_auth {
                   3123:     my ($dom) = @_;
                   3124:     if ($dom eq '') {
                   3125:         $dom = $env{'request.role.domain'};
                   3126:     }
                   3127:     my %can_assign = (
                   3128:                           krb4 => 1,
                   3129:                           krb5 => 1,
                   3130:                           int  => 1,
                   3131:                           loc  => 1,
                   3132:                      );
                   3133:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   3134:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   3135:         if (ref($domconfig{'usercreation'}{'authtypes'}) eq 'HASH') {
                   3136:             my $authhash = $domconfig{'usercreation'}{'authtypes'};
                   3137:             my $context;
                   3138:             if ($env{'request.role'} =~ /^au/) {
                   3139:                 $context = 'author';
                   3140:             } elsif ($env{'request.role'} =~ /^dc/) {
                   3141:                 $context = 'domain';
                   3142:             } elsif ($env{'request.course.id'}) {
                   3143:                 $context = 'course';
                   3144:             }
                   3145:             if ($context) {
                   3146:                 if (ref($authhash->{$context}) eq 'HASH') {
                   3147:                    %can_assign = %{$authhash->{$context}}; 
                   3148:                 }
                   3149:             }
                   3150:         }
                   3151:     }
                   3152:     my $authnum = 0;
                   3153:     foreach my $key (keys(%can_assign)) {
                   3154:         if ($can_assign{$key}) {
                   3155:             $authnum ++;
                   3156:         }
                   3157:     }
                   3158:     if ($can_assign{'krb4'} && $can_assign{'krb5'}) {
                   3159:         $authnum --;
                   3160:     }
                   3161:     return ($authnum,%can_assign);
                   3162: }
                   3163: 
1.80      albertel 3164: ###############################################################
                   3165: ##    Get Kerberos Defaults for Domain                 ##
                   3166: ###############################################################
                   3167: ##
                   3168: ## Returns default kerberos version and an associated argument
                   3169: ## as listed in file domain.tab. If not listed, provides
                   3170: ## appropriate default domain and kerberos version.
                   3171: ##
                   3172: #-------------------------------------------
                   3173: 
                   3174: =pod
                   3175: 
1.648     raeburn  3176: =item * &get_kerberos_defaults()
1.80      albertel 3177: 
                   3178: get_kerberos_defaults($target_domain) returns the default kerberos
1.641     raeburn  3179: version and domain. If not found, it defaults to version 4 and the 
                   3180: domain of the server.
1.80      albertel 3181: 
1.648     raeburn  3182: =over 4
                   3183: 
1.80      albertel 3184: ($def_version, $def_krb_domain) = &get_kerberos_defaults($target_domain);
                   3185: 
1.648     raeburn  3186: =back
                   3187: 
                   3188: =back
                   3189: 
1.80      albertel 3190: =cut
                   3191: 
                   3192: #-------------------------------------------
                   3193: sub get_kerberos_defaults {
                   3194:     my $domain=shift;
1.641     raeburn  3195:     my ($krbdef,$krbdefdom);
                   3196:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                   3197:     if (($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) {
                   3198:         $krbdef = $domdefaults{'auth_def'};
                   3199:         $krbdefdom = $domdefaults{'auth_arg_def'};
                   3200:     } else {
1.80      albertel 3201:         $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
                   3202:         my $krbdefdom=$1;
                   3203:         $krbdefdom=~tr/a-z/A-Z/;
                   3204:         $krbdef = "krb4";
                   3205:     }
                   3206:     return ($krbdef,$krbdefdom);
                   3207: }
1.112     bowersj2 3208: 
1.32      matthew  3209: 
1.46      matthew  3210: ###############################################################
                   3211: ##                Thesaurus Functions                        ##
                   3212: ###############################################################
1.20      www      3213: 
1.46      matthew  3214: =pod
1.20      www      3215: 
1.112     bowersj2 3216: =head1 Thesaurus Functions
                   3217: 
                   3218: =over 4
                   3219: 
1.648     raeburn  3220: =item * &initialize_keywords()
1.46      matthew  3221: 
                   3222: Initializes the package variable %Keywords if it is empty.  Uses the
                   3223: package variable $thesaurus_db_file.
                   3224: 
                   3225: =cut
                   3226: 
                   3227: ###################################################
                   3228: 
                   3229: sub initialize_keywords {
                   3230:     return 1 if (scalar keys(%Keywords));
                   3231:     # If we are here, %Keywords is empty, so fill it up
                   3232:     #   Make sure the file we need exists...
                   3233:     if (! -e $thesaurus_db_file) {
                   3234:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file".
                   3235:                                  " failed because it does not exist");
                   3236:         return 0;
                   3237:     }
                   3238:     #   Set up the hash as a database
                   3239:     my %thesaurus_db;
                   3240:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 3241:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  3242:         &Apache::lonnet::logthis("Could not tie \%thesaurus_db to ".
                   3243:                                  $thesaurus_db_file);
                   3244:         return 0;
                   3245:     } 
                   3246:     #  Get the average number of appearances of a word.
                   3247:     my $avecount = $thesaurus_db{'average.count'};
                   3248:     #  Put keywords (those that appear > average) into %Keywords
                   3249:     while (my ($word,$data)=each (%thesaurus_db)) {
                   3250:         my ($count,undef) = split /:/,$data;
                   3251:         $Keywords{$word}++ if ($count > $avecount);
                   3252:     }
                   3253:     untie %thesaurus_db;
                   3254:     # Remove special values from %Keywords.
1.356     albertel 3255:     foreach my $value ('total.count','average.count') {
                   3256:         delete($Keywords{$value}) if (exists($Keywords{$value}));
1.586     raeburn  3257:   }
1.46      matthew  3258:     return 1;
                   3259: }
                   3260: 
                   3261: ###################################################
                   3262: 
                   3263: =pod
                   3264: 
1.648     raeburn  3265: =item * &keyword($word)
1.46      matthew  3266: 
                   3267: Returns true if $word is a keyword.  A keyword is a word that appears more 
                   3268: than the average number of times in the thesaurus database.  Calls 
                   3269: &initialize_keywords
                   3270: 
                   3271: =cut
                   3272: 
                   3273: ###################################################
1.20      www      3274: 
                   3275: sub keyword {
1.46      matthew  3276:     return if (!&initialize_keywords());
                   3277:     my $word=lc(shift());
                   3278:     $word=~s/\W//g;
                   3279:     return exists($Keywords{$word});
1.20      www      3280: }
1.46      matthew  3281: 
                   3282: ###############################################################
                   3283: 
                   3284: =pod 
1.20      www      3285: 
1.648     raeburn  3286: =item * &get_related_words()
1.46      matthew  3287: 
1.160     matthew  3288: Look up a word in the thesaurus.  Takes a scalar argument and returns
1.46      matthew  3289: an array of words.  If the keyword is not in the thesaurus, an empty array
                   3290: will be returned.  The order of the words returned is determined by the
                   3291: database which holds them.
                   3292: 
                   3293: Uses global $thesaurus_db_file.
                   3294: 
1.1057    foxr     3295: 
1.46      matthew  3296: =cut
                   3297: 
                   3298: ###############################################################
                   3299: sub get_related_words {
                   3300:     my $keyword = shift;
                   3301:     my %thesaurus_db;
                   3302:     if (! -e $thesaurus_db_file) {
                   3303:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file ".
                   3304:                                  "failed because the file does not exist");
                   3305:         return ();
                   3306:     }
                   3307:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 3308:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  3309:         return ();
                   3310:     } 
                   3311:     my @Words=();
1.429     www      3312:     my $count=0;
1.46      matthew  3313:     if (exists($thesaurus_db{$keyword})) {
1.356     albertel 3314: 	# The first element is the number of times
                   3315: 	# the word appears.  We do not need it now.
1.429     www      3316: 	my (undef,@RelatedWords) = (split(/:/,$thesaurus_db{$keyword}));
                   3317: 	my (undef,$mostfrequentcount)=split(/\,/,$RelatedWords[0]);
                   3318: 	my $threshold=$mostfrequentcount/10;
                   3319:         foreach my $possibleword (@RelatedWords) {
                   3320:             my ($word,$wordcount)=split(/\,/,$possibleword);
                   3321:             if ($wordcount>$threshold) {
                   3322: 		push(@Words,$word);
                   3323:                 $count++;
                   3324:                 if ($count>10) { last; }
                   3325: 	    }
1.20      www      3326:         }
                   3327:     }
1.46      matthew  3328:     untie %thesaurus_db;
                   3329:     return @Words;
1.14      harris41 3330: }
1.1090    foxr     3331: ###############################################################
                   3332: #
                   3333: #  Spell checking
                   3334: #
                   3335: 
                   3336: =pod
                   3337: 
1.1142    raeburn  3338: =back
                   3339: 
1.1090    foxr     3340: =head1 Spell checking
                   3341: 
                   3342: =over 4
                   3343: 
                   3344: =item * &check_spelling($wordlist $language)
                   3345: 
                   3346: Takes a string containing words and feeds it to an external
                   3347: spellcheck program via a pipeline. Returns a string containing
                   3348: them mis-spelled words.
                   3349: 
                   3350: Parameters:
                   3351: 
                   3352: =over 4
                   3353: 
                   3354: =item - $wordlist
                   3355: 
                   3356: String that will be fed into the spellcheck program.
                   3357: 
                   3358: =item - $language
                   3359: 
                   3360: Language string that specifies the language for which the spell
                   3361: check will be performed.
                   3362: 
                   3363: =back
                   3364: 
                   3365: =back
                   3366: 
                   3367: Note: This sub assumes that aspell is installed.
                   3368: 
                   3369: 
                   3370: =cut
                   3371: 
1.46      matthew  3372: 
1.1090    foxr     3373: sub check_spelling {
                   3374:     my ($wordlist, $language) = @_;
1.1091    foxr     3375:     my @misspellings;
                   3376:     
                   3377:     # Generate the speller and set the langauge.
                   3378:     # if explicitly selected:
1.1090    foxr     3379: 
1.1091    foxr     3380:     my $speller = Text::Aspell->new;
1.1090    foxr     3381:     if ($language) {
1.1091    foxr     3382: 	$speller->set_option('lang', $language);
1.1090    foxr     3383:     }
                   3384: 
1.1091    foxr     3385:     # Turn the word list into an array of words by splittingon whitespace
1.1090    foxr     3386: 
1.1091    foxr     3387:     my @words = split(/\s+/, $wordlist);
1.1090    foxr     3388: 
1.1091    foxr     3389:     foreach my $word (@words) {
                   3390: 	if(! $speller->check($word)) {
                   3391: 	    push(@misspellings, $word);
1.1090    foxr     3392: 	}
                   3393:     }
1.1091    foxr     3394:     return join(' ', @misspellings);
                   3395:     
1.1090    foxr     3396: }
                   3397: 
1.61      www      3398: # -------------------------------------------------------------- Plaintext name
1.81      albertel 3399: =pod
                   3400: 
1.112     bowersj2 3401: =head1 User Name Functions
                   3402: 
                   3403: =over 4
                   3404: 
1.648     raeburn  3405: =item * &plainname($uname,$udom,$first)
1.81      albertel 3406: 
1.112     bowersj2 3407: Takes a users logon name and returns it as a string in
1.226     albertel 3408: "first middle last generation" form 
                   3409: if $first is set to 'lastname' then it returns it as
                   3410: 'lastname generation, firstname middlename' if their is a lastname
1.81      albertel 3411: 
                   3412: =cut
1.61      www      3413: 
1.295     www      3414: 
1.81      albertel 3415: ###############################################################
1.61      www      3416: sub plainname {
1.226     albertel 3417:     my ($uname,$udom,$first)=@_;
1.537     albertel 3418:     return if (!defined($uname) || !defined($udom));
1.295     www      3419:     my %names=&getnames($uname,$udom);
1.226     albertel 3420:     my $name=&Apache::lonnet::format_name($names{'firstname'},
                   3421: 					  $names{'middlename'},
                   3422: 					  $names{'lastname'},
                   3423: 					  $names{'generation'},$first);
                   3424:     $name=~s/^\s+//;
1.62      www      3425:     $name=~s/\s+$//;
                   3426:     $name=~s/\s+/ /g;
1.353     albertel 3427:     if ($name !~ /\S/) { $name=$uname.':'.$udom; }
1.62      www      3428:     return $name;
1.61      www      3429: }
1.66      www      3430: 
                   3431: # -------------------------------------------------------------------- Nickname
1.81      albertel 3432: =pod
                   3433: 
1.648     raeburn  3434: =item * &nickname($uname,$udom)
1.81      albertel 3435: 
                   3436: Gets a users name and returns it as a string as
                   3437: 
                   3438: "&quot;nickname&quot;"
1.66      www      3439: 
1.81      albertel 3440: if the user has a nickname or
                   3441: 
                   3442: "first middle last generation"
                   3443: 
                   3444: if the user does not
                   3445: 
                   3446: =cut
1.66      www      3447: 
                   3448: sub nickname {
                   3449:     my ($uname,$udom)=@_;
1.537     albertel 3450:     return if (!defined($uname) || !defined($udom));
1.295     www      3451:     my %names=&getnames($uname,$udom);
1.68      albertel 3452:     my $name=$names{'nickname'};
1.66      www      3453:     if ($name) {
                   3454:        $name='&quot;'.$name.'&quot;'; 
                   3455:     } else {
                   3456:        $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
                   3457: 	     $names{'lastname'}.' '.$names{'generation'};
                   3458:        $name=~s/\s+$//;
                   3459:        $name=~s/\s+/ /g;
                   3460:     }
                   3461:     return $name;
                   3462: }
                   3463: 
1.295     www      3464: sub getnames {
                   3465:     my ($uname,$udom)=@_;
1.537     albertel 3466:     return if (!defined($uname) || !defined($udom));
1.433     albertel 3467:     if ($udom eq 'public' && $uname eq 'public') {
                   3468: 	return ('lastname' => &mt('Public'));
                   3469:     }
1.295     www      3470:     my $id=$uname.':'.$udom;
                   3471:     my ($names,$cached)=&Apache::lonnet::is_cached_new('namescache',$id);
                   3472:     if ($cached) {
                   3473: 	return %{$names};
                   3474:     } else {
                   3475: 	my %loadnames=&Apache::lonnet::get('environment',
                   3476:                     ['firstname','middlename','lastname','generation','nickname'],
                   3477: 					 $udom,$uname);
                   3478: 	&Apache::lonnet::do_cache_new('namescache',$id,\%loadnames);
                   3479: 	return %loadnames;
                   3480:     }
                   3481: }
1.61      www      3482: 
1.542     raeburn  3483: # -------------------------------------------------------------------- getemails
1.648     raeburn  3484: 
1.542     raeburn  3485: =pod
                   3486: 
1.648     raeburn  3487: =item * &getemails($uname,$udom)
1.542     raeburn  3488: 
                   3489: Gets a user's email information and returns it as a hash with keys:
                   3490: notification, critnotification, permanentemail
                   3491: 
                   3492: For notification and critnotification, values are comma-separated lists 
1.648     raeburn  3493: of e-mail addresses; for permanentemail, value is a single e-mail address.
1.542     raeburn  3494:  
1.648     raeburn  3495: 
1.542     raeburn  3496: =cut
                   3497: 
1.648     raeburn  3498: 
1.466     albertel 3499: sub getemails {
                   3500:     my ($uname,$udom)=@_;
                   3501:     if ($udom eq 'public' && $uname eq 'public') {
                   3502: 	return;
                   3503:     }
1.467     www      3504:     if (!$udom) { $udom=$env{'user.domain'}; }
                   3505:     if (!$uname) { $uname=$env{'user.name'}; }
1.466     albertel 3506:     my $id=$uname.':'.$udom;
                   3507:     my ($names,$cached)=&Apache::lonnet::is_cached_new('emailscache',$id);
                   3508:     if ($cached) {
                   3509: 	return %{$names};
                   3510:     } else {
                   3511: 	my %loadnames=&Apache::lonnet::get('environment',
                   3512:                     			   ['notification','critnotification',
                   3513: 					    'permanentemail'],
                   3514: 					   $udom,$uname);
                   3515: 	&Apache::lonnet::do_cache_new('emailscache',$id,\%loadnames);
                   3516: 	return %loadnames;
                   3517:     }
                   3518: }
                   3519: 
1.551     albertel 3520: sub flush_email_cache {
                   3521:     my ($uname,$udom)=@_;
                   3522:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   3523:     if (!$uname) { $uname=$env{'user.name'};   }
                   3524:     return if ($udom eq 'public' && $uname eq 'public');
                   3525:     my $id=$uname.':'.$udom;
                   3526:     &Apache::lonnet::devalidate_cache_new('emailscache',$id);
                   3527: }
                   3528: 
1.728     raeburn  3529: # -------------------------------------------------------------------- getlangs
                   3530: 
                   3531: =pod
                   3532: 
                   3533: =item * &getlangs($uname,$udom)
                   3534: 
                   3535: Gets a user's language preference and returns it as a hash with key:
                   3536: language.
                   3537: 
                   3538: =cut
                   3539: 
                   3540: 
                   3541: sub getlangs {
                   3542:     my ($uname,$udom) = @_;
                   3543:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   3544:     if (!$uname) { $uname=$env{'user.name'};   }
                   3545:     my $id=$uname.':'.$udom;
                   3546:     my ($langs,$cached)=&Apache::lonnet::is_cached_new('userlangs',$id);
                   3547:     if ($cached) {
                   3548:         return %{$langs};
                   3549:     } else {
                   3550:         my %loadlangs=&Apache::lonnet::get('environment',['languages'],
                   3551:                                            $udom,$uname);
                   3552:         &Apache::lonnet::do_cache_new('userlangs',$id,\%loadlangs);
                   3553:         return %loadlangs;
                   3554:     }
                   3555: }
                   3556: 
                   3557: sub flush_langs_cache {
                   3558:     my ($uname,$udom)=@_;
                   3559:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   3560:     if (!$uname) { $uname=$env{'user.name'};   }
                   3561:     return if ($udom eq 'public' && $uname eq 'public');
                   3562:     my $id=$uname.':'.$udom;
                   3563:     &Apache::lonnet::devalidate_cache_new('userlangs',$id);
                   3564: }
                   3565: 
1.61      www      3566: # ------------------------------------------------------------------ Screenname
1.81      albertel 3567: 
                   3568: =pod
                   3569: 
1.648     raeburn  3570: =item * &screenname($uname,$udom)
1.81      albertel 3571: 
                   3572: Gets a users screenname and returns it as a string
                   3573: 
                   3574: =cut
1.61      www      3575: 
                   3576: sub screenname {
                   3577:     my ($uname,$udom)=@_;
1.258     albertel 3578:     if ($uname eq $env{'user.name'} &&
                   3579: 	$udom eq $env{'user.domain'}) {return $env{'environment.screenname'};}
1.212     albertel 3580:     my %names=&Apache::lonnet::get('environment',['screenname'],$udom,$uname);
1.68      albertel 3581:     return $names{'screenname'};
1.62      www      3582: }
                   3583: 
1.212     albertel 3584: 
1.802     bisitz   3585: # ------------------------------------------------------------- Confirm Wrapper
                   3586: =pod
                   3587: 
1.1142    raeburn  3588: =item * &confirmwrapper($message)
1.802     bisitz   3589: 
                   3590: Wrap messages about completion of operation in box
                   3591: 
                   3592: =cut
                   3593: 
                   3594: sub confirmwrapper {
                   3595:     my ($message)=@_;
                   3596:     if ($message) {
                   3597:         return "\n".'<div class="LC_confirm_box">'."\n"
                   3598:                .$message."\n"
                   3599:                .'</div>'."\n";
                   3600:     } else {
                   3601:         return $message;
                   3602:     }
                   3603: }
                   3604: 
1.62      www      3605: # ------------------------------------------------------------- Message Wrapper
                   3606: 
                   3607: sub messagewrapper {
1.369     www      3608:     my ($link,$username,$domain,$subject,$text)=@_;
1.62      www      3609:     return 
1.441     albertel 3610:         '<a href="/adm/email?compose=individual&amp;'.
                   3611:         'recname='.$username.'&amp;recdom='.$domain.
                   3612: 	'&amp;subject='.&escape($subject).'&amp;text='.&escape($text).'" '.
1.200     matthew  3613:         'title="'.&mt('Send message').'">'.$link.'</a>';
1.74      www      3614: }
1.802     bisitz   3615: 
1.74      www      3616: # --------------------------------------------------------------- Notes Wrapper
                   3617: 
                   3618: sub noteswrapper {
                   3619:     my ($link,$un,$do)=@_;
                   3620:     return 
1.896     amueller 3621: "<a href='/adm/email?recordftf=retrieve&amp;recname=$un&amp;recdom=$do'>$link</a>";
1.62      www      3622: }
1.802     bisitz   3623: 
1.62      www      3624: # ------------------------------------------------------------- Aboutme Wrapper
                   3625: 
                   3626: sub aboutmewrapper {
1.1070    raeburn  3627:     my ($link,$username,$domain,$target,$class)=@_;
1.447     raeburn  3628:     if (!defined($username)  && !defined($domain)) {
                   3629:         return;
                   3630:     }
1.1096    raeburn  3631:     return '<a href="/adm/'.$domain.'/'.$username.'/aboutme"'.
1.1070    raeburn  3632: 	($target?' target="'.$target.'"':'').($class?' class="'.$class.'"':'').' title="'.&mt("View this user's personal information page").'">'.$link.'</a>';
1.62      www      3633: }
                   3634: 
                   3635: # ------------------------------------------------------------ Syllabus Wrapper
                   3636: 
                   3637: sub syllabuswrapper {
1.707     bisitz   3638:     my ($linktext,$coursedir,$domain)=@_;
1.208     matthew  3639:     return qq{<a href="/public/$domain/$coursedir/syllabus">$linktext</a>};
1.61      www      3640: }
1.14      harris41 3641: 
1.802     bisitz   3642: # -----------------------------------------------------------------------------
                   3643: 
1.208     matthew  3644: sub track_student_link {
1.887     raeburn  3645:     my ($linktext,$sname,$sdom,$target,$start,$only_body) = @_;
1.268     albertel 3646:     my $link ="/adm/trackstudent?";
1.208     matthew  3647:     my $title = 'View recent activity';
                   3648:     if (defined($sname) && $sname !~ /^\s*$/ &&
                   3649:         defined($sdom)  && $sdom  !~ /^\s*$/) {
1.268     albertel 3650:         $link .= "selected_student=$sname:$sdom";
1.208     matthew  3651:         $title .= ' of this student';
1.268     albertel 3652:     } 
1.208     matthew  3653:     if (defined($target) && $target !~ /^\s*$/) {
                   3654:         $target = qq{target="$target"};
                   3655:     } else {
                   3656:         $target = '';
                   3657:     }
1.268     albertel 3658:     if ($start) { $link.='&amp;start='.$start; }
1.887     raeburn  3659:     if ($only_body) { $link .= '&amp;only_body=1'; }
1.554     albertel 3660:     $title = &mt($title);
                   3661:     $linktext = &mt($linktext);
1.448     albertel 3662:     return qq{<a href="$link" title="$title" $target>$linktext</a>}.
                   3663: 	&help_open_topic('View_recent_activity');
1.208     matthew  3664: }
                   3665: 
1.781     raeburn  3666: sub slot_reservations_link {
                   3667:     my ($linktext,$sname,$sdom,$target) = @_;
                   3668:     my $link ="/adm/slotrequest?command=showresv&amp;origin=aboutme";
                   3669:     my $title = 'View slot reservation history';
                   3670:     if (defined($sname) && $sname !~ /^\s*$/ &&
                   3671:         defined($sdom)  && $sdom  !~ /^\s*$/) {
                   3672:         $link .= "&amp;uname=$sname&amp;udom=$sdom";
                   3673:         $title .= ' of this student';
                   3674:     }
                   3675:     if (defined($target) && $target !~ /^\s*$/) {
                   3676:         $target = qq{target="$target"};
                   3677:     } else {
                   3678:         $target = '';
                   3679:     }
                   3680:     $title = &mt($title);
                   3681:     $linktext = &mt($linktext);
                   3682:     return qq{<a href="$link" title="$title" $target>$linktext</a>};
                   3683: # FIXME uncomment when help item created: &help_open_topic('Slot_Reservation_History');
                   3684: 
                   3685: }
                   3686: 
1.508     www      3687: # ===================================================== Display a student photo
                   3688: 
                   3689: 
1.509     albertel 3690: sub student_image_tag {
1.508     www      3691:     my ($domain,$user)=@_;
                   3692:     my $imgsrc=&Apache::lonnet::studentphoto($domain,$user,'jpg');
                   3693:     if (($imgsrc) && ($imgsrc ne '/adm/lonKaputt/lonlogo_broken.gif')) {
                   3694: 	return '<img src="'.$imgsrc.'" align="right" />';
                   3695:     } else {
                   3696: 	return '';
                   3697:     }
                   3698: }
                   3699: 
1.112     bowersj2 3700: =pod
                   3701: 
                   3702: =back
                   3703: 
                   3704: =head1 Access .tab File Data
                   3705: 
                   3706: =over 4
                   3707: 
1.648     raeburn  3708: =item * &languageids() 
1.112     bowersj2 3709: 
                   3710: returns list of all language ids
                   3711: 
                   3712: =cut
                   3713: 
1.14      harris41 3714: sub languageids {
1.16      harris41 3715:     return sort(keys(%language));
1.14      harris41 3716: }
                   3717: 
1.112     bowersj2 3718: =pod
                   3719: 
1.648     raeburn  3720: =item * &languagedescription() 
1.112     bowersj2 3721: 
                   3722: returns description of a specified language id
                   3723: 
                   3724: =cut
                   3725: 
1.14      harris41 3726: sub languagedescription {
1.125     www      3727:     my $code=shift;
                   3728:     return  ($supported_language{$code}?'* ':'').
                   3729:             $language{$code}.
1.126     www      3730: 	    ($supported_language{$code}?' ('.&mt('interface available').')':'');
1.145     www      3731: }
                   3732: 
1.1048    foxr     3733: =pod
                   3734: 
                   3735: =item * &plainlanguagedescription
                   3736: 
                   3737: Returns both the plain language description (e.g. 'Creoles and Pidgins, English-based (Other)')
                   3738: and the language character encoding (e.g. ISO) separated by a ' - ' string.
                   3739: 
                   3740: =cut
                   3741: 
1.145     www      3742: sub plainlanguagedescription {
                   3743:     my $code=shift;
                   3744:     return $language{$code};
                   3745: }
                   3746: 
1.1048    foxr     3747: =pod
                   3748: 
                   3749: =item * &supportedlanguagecode
                   3750: 
                   3751: Returns the supported language code (e.g. sptutf maps to pt) given a language
                   3752: code.
                   3753: 
                   3754: =cut
                   3755: 
1.145     www      3756: sub supportedlanguagecode {
                   3757:     my $code=shift;
                   3758:     return $supported_language{$code};
1.97      www      3759: }
                   3760: 
1.112     bowersj2 3761: =pod
                   3762: 
1.1048    foxr     3763: =item * &latexlanguage()
                   3764: 
                   3765: Given a language key code returns the correspondnig language to use
                   3766: to select the correct hyphenation on LaTeX printouts.  This is undef if there
                   3767: is no supported hyphenation for the language code.
                   3768: 
                   3769: =cut
                   3770: 
                   3771: sub latexlanguage {
                   3772:     my $code = shift;
                   3773:     return $latex_language{$code};
                   3774: }
                   3775: 
                   3776: =pod
                   3777: 
                   3778: =item * &latexhyphenation()
                   3779: 
                   3780: Same as above but what's supplied is the language as it might be stored
                   3781: in the metadata.
                   3782: 
                   3783: =cut
                   3784: 
                   3785: sub latexhyphenation {
                   3786:     my $key = shift;
                   3787:     return $latex_language_bykey{$key};
                   3788: }
                   3789: 
                   3790: =pod
                   3791: 
1.648     raeburn  3792: =item * &copyrightids() 
1.112     bowersj2 3793: 
                   3794: returns list of all copyrights
                   3795: 
                   3796: =cut
                   3797: 
                   3798: sub copyrightids {
                   3799:     return sort(keys(%cprtag));
                   3800: }
                   3801: 
                   3802: =pod
                   3803: 
1.648     raeburn  3804: =item * &copyrightdescription() 
1.112     bowersj2 3805: 
                   3806: returns description of a specified copyright id
                   3807: 
                   3808: =cut
                   3809: 
                   3810: sub copyrightdescription {
1.166     www      3811:     return &mt($cprtag{shift(@_)});
1.112     bowersj2 3812: }
1.197     matthew  3813: 
                   3814: =pod
                   3815: 
1.648     raeburn  3816: =item * &source_copyrightids() 
1.192     taceyjo1 3817: 
                   3818: returns list of all source copyrights
                   3819: 
                   3820: =cut
                   3821: 
                   3822: sub source_copyrightids {
                   3823:     return sort(keys(%scprtag));
                   3824: }
                   3825: 
                   3826: =pod
                   3827: 
1.648     raeburn  3828: =item * &source_copyrightdescription() 
1.192     taceyjo1 3829: 
                   3830: returns description of a specified source copyright id
                   3831: 
                   3832: =cut
                   3833: 
                   3834: sub source_copyrightdescription {
                   3835:     return &mt($scprtag{shift(@_)});
                   3836: }
1.112     bowersj2 3837: 
                   3838: =pod
                   3839: 
1.648     raeburn  3840: =item * &filecategories() 
1.112     bowersj2 3841: 
                   3842: returns list of all file categories
                   3843: 
                   3844: =cut
                   3845: 
                   3846: sub filecategories {
                   3847:     return sort(keys(%category_extensions));
                   3848: }
                   3849: 
                   3850: =pod
                   3851: 
1.648     raeburn  3852: =item * &filecategorytypes() 
1.112     bowersj2 3853: 
                   3854: returns list of file types belonging to a given file
                   3855: category
                   3856: 
                   3857: =cut
                   3858: 
                   3859: sub filecategorytypes {
1.356     albertel 3860:     my ($cat) = @_;
                   3861:     return @{$category_extensions{lc($cat)}};
1.112     bowersj2 3862: }
                   3863: 
                   3864: =pod
                   3865: 
1.648     raeburn  3866: =item * &fileembstyle() 
1.112     bowersj2 3867: 
                   3868: returns embedding style for a specified file type
                   3869: 
                   3870: =cut
                   3871: 
                   3872: sub fileembstyle {
                   3873:     return $fe{lc(shift(@_))};
1.169     www      3874: }
                   3875: 
1.351     www      3876: sub filemimetype {
                   3877:     return $fm{lc(shift(@_))};
                   3878: }
                   3879: 
1.169     www      3880: 
                   3881: sub filecategoryselect {
                   3882:     my ($name,$value)=@_;
1.189     matthew  3883:     return &select_form($value,$name,
1.970     raeburn  3884:                         {'' => &mt('Any category'), map { $_,$_ } sort(keys(%category_extensions))});
1.112     bowersj2 3885: }
                   3886: 
                   3887: =pod
                   3888: 
1.648     raeburn  3889: =item * &filedescription() 
1.112     bowersj2 3890: 
                   3891: returns description for a specified file type
                   3892: 
                   3893: =cut
                   3894: 
                   3895: sub filedescription {
1.188     matthew  3896:     my $file_description = $fd{lc(shift())};
                   3897:     $file_description =~ s:([\[\]]):~$1:g;
                   3898:     return &mt($file_description);
1.112     bowersj2 3899: }
                   3900: 
                   3901: =pod
                   3902: 
1.648     raeburn  3903: =item * &filedescriptionex() 
1.112     bowersj2 3904: 
                   3905: returns description for a specified file type with
                   3906: extra formatting
                   3907: 
                   3908: =cut
                   3909: 
                   3910: sub filedescriptionex {
                   3911:     my $ex=shift;
1.188     matthew  3912:     my $file_description = $fd{lc($ex)};
                   3913:     $file_description =~ s:([\[\]]):~$1:g;
                   3914:     return '.'.$ex.' '.&mt($file_description);
1.112     bowersj2 3915: }
                   3916: 
                   3917: # End of .tab access
                   3918: =pod
                   3919: 
                   3920: =back
                   3921: 
                   3922: =cut
                   3923: 
                   3924: # ------------------------------------------------------------------ File Types
                   3925: sub fileextensions {
                   3926:     return sort(keys(%fe));
                   3927: }
                   3928: 
1.97      www      3929: # ----------------------------------------------------------- Display Languages
                   3930: # returns a hash with all desired display languages
                   3931: #
                   3932: 
                   3933: sub display_languages {
                   3934:     my %languages=();
1.695     raeburn  3935:     foreach my $lang (&Apache::lonlocal::preferred_languages()) {
1.356     albertel 3936: 	$languages{$lang}=1;
1.97      www      3937:     }
                   3938:     &get_unprocessed_cgi($ENV{'QUERY_STRING'},['displaylanguage']);
1.258     albertel 3939:     if ($env{'form.displaylanguage'}) {
1.356     albertel 3940: 	foreach my $lang (split(/\s*(\,|\;|\:)\s*/,$env{'form.displaylanguage'})) {
                   3941: 	    $languages{$lang}=1;
1.97      www      3942:         }
                   3943:     }
                   3944:     return %languages;
1.14      harris41 3945: }
                   3946: 
1.582     albertel 3947: sub languages {
                   3948:     my ($possible_langs) = @_;
1.695     raeburn  3949:     my @preferred_langs = &Apache::lonlocal::preferred_languages();
1.582     albertel 3950:     if (!ref($possible_langs)) {
                   3951: 	if( wantarray ) {
                   3952: 	    return @preferred_langs;
                   3953: 	} else {
                   3954: 	    return $preferred_langs[0];
                   3955: 	}
                   3956:     }
                   3957:     my %possibilities = map { $_ => 1 } (@$possible_langs);
                   3958:     my @preferred_possibilities;
                   3959:     foreach my $preferred_lang (@preferred_langs) {
                   3960: 	if (exists($possibilities{$preferred_lang})) {
                   3961: 	    push(@preferred_possibilities, $preferred_lang);
                   3962: 	}
                   3963:     }
                   3964:     if( wantarray ) {
                   3965: 	return @preferred_possibilities;
                   3966:     }
                   3967:     return $preferred_possibilities[0];
                   3968: }
                   3969: 
1.742     raeburn  3970: sub user_lang {
                   3971:     my ($touname,$toudom,$fromcid) = @_;
                   3972:     my @userlangs;
                   3973:     if (($fromcid ne '') && ($env{'course.'.$fromcid.'.languages'} ne '')) {
                   3974:         @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
                   3975:                     $env{'course.'.$fromcid.'.languages'}));
                   3976:     } else {
                   3977:         my %langhash = &getlangs($touname,$toudom);
                   3978:         if ($langhash{'languages'} ne '') {
                   3979:             @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
                   3980:         } else {
                   3981:             my %domdefs = &Apache::lonnet::get_domain_defaults($toudom);
                   3982:             if ($domdefs{'lang_def'} ne '') {
                   3983:                 @userlangs = ($domdefs{'lang_def'});
                   3984:             }
                   3985:         }
                   3986:     }
                   3987:     my @languages=&Apache::lonlocal::get_genlanguages(@userlangs);
                   3988:     my $user_lh = Apache::localize->get_handle(@languages);
                   3989:     return $user_lh;
                   3990: }
                   3991: 
                   3992: 
1.112     bowersj2 3993: ###############################################################
                   3994: ##               Student Answer Attempts                     ##
                   3995: ###############################################################
                   3996: 
                   3997: =pod
                   3998: 
                   3999: =head1 Alternate Problem Views
                   4000: 
                   4001: =over 4
                   4002: 
1.648     raeburn  4003: =item * &get_previous_attempt($symb, $username, $domain, $course,
1.1199    raeburn  4004:     $getattempt, $regexp, $gradesub, $usec, $identifier)
1.112     bowersj2 4005: 
                   4006: Return string with previous attempt on problem. Arguments:
                   4007: 
                   4008: =over 4
                   4009: 
                   4010: =item * $symb: Problem, including path
                   4011: 
                   4012: =item * $username: username of the desired student
                   4013: 
                   4014: =item * $domain: domain of the desired student
1.14      harris41 4015: 
1.112     bowersj2 4016: =item * $course: Course ID
1.14      harris41 4017: 
1.112     bowersj2 4018: =item * $getattempt: Leave blank for all attempts, otherwise put
                   4019:     something
1.14      harris41 4020: 
1.112     bowersj2 4021: =item * $regexp: if string matches this regexp, the string will be
                   4022:     sent to $gradesub
1.14      harris41 4023: 
1.112     bowersj2 4024: =item * $gradesub: routine that processes the string if it matches $regexp
1.14      harris41 4025: 
1.1199    raeburn  4026: =item * $usec: section of the desired student
                   4027: 
                   4028: =item * $identifier: counter for student (multiple students one problem) or 
                   4029:     problem (one student; whole sequence).
                   4030: 
1.112     bowersj2 4031: =back
1.14      harris41 4032: 
1.112     bowersj2 4033: The output string is a table containing all desired attempts, if any.
1.16      harris41 4034: 
1.112     bowersj2 4035: =cut
1.1       albertel 4036: 
                   4037: sub get_previous_attempt {
1.1199    raeburn  4038:   my ($symb,$username,$domain,$course,$getattempt,$regexp,$gradesub,$usec,$identifier)=@_;
1.1       albertel 4039:   my $prevattempts='';
1.43      ng       4040:   no strict 'refs';
1.1       albertel 4041:   if ($symb) {
1.3       albertel 4042:     my (%returnhash)=
                   4043:       &Apache::lonnet::restore($symb,$course,$domain,$username);
1.1       albertel 4044:     if ($returnhash{'version'}) {
                   4045:       my %lasthash=();
                   4046:       my $version;
                   4047:       for ($version=1;$version<=$returnhash{'version'};$version++) {
1.1212    raeburn  4048:         foreach my $key (reverse(sort(split(/\:/,$returnhash{$version.':keys'})))) {
                   4049:             if ($key =~ /\.rawrndseed$/) {
                   4050:                 my ($id) = ($key =~ /^(.+)\.rawrndseed$/);
                   4051:                 $lasthash{$id.'.rndseed'} = $returnhash{$version.':'.$key};
                   4052:             } else {
                   4053:                 $lasthash{$key}=$returnhash{$version.':'.$key};
                   4054:             }
1.19      harris41 4055:         }
1.1       albertel 4056:       }
1.596     albertel 4057:       $prevattempts=&start_data_table().&start_data_table_header_row();
                   4058:       $prevattempts.='<th>'.&mt('History').'</th>';
1.1199    raeburn  4059:       my (%typeparts,%lasthidden,%regraded,%hidestatus);
1.945     raeburn  4060:       my $showsurv=&Apache::lonnet::allowed('vas',$env{'request.course.id'});
1.356     albertel 4061:       foreach my $key (sort(keys(%lasthash))) {
                   4062: 	my ($ign,@parts) = split(/\./,$key);
1.41      ng       4063: 	if ($#parts > 0) {
1.31      albertel 4064: 	  my $data=$parts[-1];
1.989     raeburn  4065:           next if ($data eq 'foilorder');
1.31      albertel 4066: 	  pop(@parts);
1.1010    www      4067:           $prevattempts.='<th>'.&mt('Part ').join('.',@parts).'<br />'.$data.'&nbsp;</th>';
1.945     raeburn  4068:           if ($data eq 'type') {
                   4069:               unless ($showsurv) {
                   4070:                   my $id = join(',',@parts);
                   4071:                   $typeparts{$ign.'.'.$id} = $lasthash{$key};
1.978     raeburn  4072:                   if (($lasthash{$key} eq 'anonsurvey') || ($lasthash{$key} eq 'anonsurveycred')) {
                   4073:                       $lasthidden{$ign.'.'.$id} = 1;
                   4074:                   }
1.945     raeburn  4075:               }
1.1199    raeburn  4076:               if ($identifier ne '') {
                   4077:                   my $id = join(',',@parts);
                   4078:                   if (&Apache::lonnet::EXT("resource.$id.problemstatus",$symb,
                   4079:                                                $domain,$username,$usec,undef,$course) =~ /^no/) {
                   4080:                       $hidestatus{$ign.'.'.$id} = 1;
                   4081:                   }
                   4082:               }
                   4083:           } elsif ($data eq 'regrader') {
                   4084:               if (($identifier ne '') && (@parts)) {
1.1200    raeburn  4085:                   my $id = join(',',@parts);
                   4086:                   $regraded{$ign.'.'.$id} = 1;
1.1199    raeburn  4087:               }
1.1010    www      4088:           } 
1.31      albertel 4089: 	} else {
1.41      ng       4090: 	  if ($#parts == 0) {
                   4091: 	    $prevattempts.='<th>'.$parts[0].'</th>';
                   4092: 	  } else {
                   4093: 	    $prevattempts.='<th>'.$ign.'</th>';
                   4094: 	  }
1.31      albertel 4095: 	}
1.16      harris41 4096:       }
1.596     albertel 4097:       $prevattempts.=&end_data_table_header_row();
1.40      ng       4098:       if ($getattempt eq '') {
1.1199    raeburn  4099:         my (%solved,%resets,%probstatus);
1.1200    raeburn  4100:         if (($identifier ne '') && (keys(%regraded) > 0)) {
                   4101:             for ($version=1;$version<=$returnhash{'version'};$version++) {
                   4102:                 foreach my $id (keys(%regraded)) {
                   4103:                     if (($returnhash{$version.':'.$id.'.regrader'}) &&
                   4104:                         ($returnhash{$version.':'.$id.'.tries'} eq '') &&
                   4105:                         ($returnhash{$version.':'.$id.'.award'} eq '')) {
                   4106:                         push(@{$resets{$id}},$version);
1.1199    raeburn  4107:                     }
                   4108:                 }
                   4109:             }
1.1200    raeburn  4110:         }
                   4111: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
1.1199    raeburn  4112:             my (@hidden,@unsolved);
1.945     raeburn  4113:             if (%typeparts) {
                   4114:                 foreach my $id (keys(%typeparts)) {
1.1199    raeburn  4115:                     if (($returnhash{$version.':'.$id.'.type'} eq 'anonsurvey') || 
                   4116:                         ($returnhash{$version.':'.$id.'.type'} eq 'anonsurveycred')) {
1.945     raeburn  4117:                         push(@hidden,$id);
1.1199    raeburn  4118:                     } elsif ($identifier ne '') {
                   4119:                         unless (($returnhash{$version.':'.$id.'.type'} eq 'survey') ||
                   4120:                                 ($returnhash{$version.':'.$id.'.type'} eq 'surveycred') ||
                   4121:                                 ($hidestatus{$id})) {
1.1200    raeburn  4122:                             next if ((ref($resets{$id}) eq 'ARRAY') && grep(/^\Q$version\E$/,@{$resets{$id}}));
1.1199    raeburn  4123:                             if ($returnhash{$version.':'.$id.'.solved'} eq 'correct_by_student') {
                   4124:                                 push(@{$solved{$id}},$version);
                   4125:                             } elsif (($returnhash{$version.':'.$id.'.solved'} ne '') &&
                   4126:                                      (ref($solved{$id}) eq 'ARRAY')) {
                   4127:                                 my $skip;
                   4128:                                 if (ref($resets{$id}) eq 'ARRAY') {
                   4129:                                     foreach my $reset (@{$resets{$id}}) {
                   4130:                                         if ($reset > $solved{$id}[-1]) {
                   4131:                                             $skip=1;
                   4132:                                             last;
                   4133:                                         }
                   4134:                                     }
                   4135:                                 }
                   4136:                                 unless ($skip) {
                   4137:                                     my ($ign,$partslist) = split(/\./,$id,2);
                   4138:                                     push(@unsolved,$partslist);
                   4139:                                 }
                   4140:                             }
                   4141:                         }
1.945     raeburn  4142:                     }
                   4143:                 }
                   4144:             }
                   4145:             $prevattempts.=&start_data_table_row().
1.1199    raeburn  4146:                            '<td>'.&mt('Transaction [_1]',$version);
                   4147:             if (@unsolved) {
                   4148:                 $prevattempts .= '<span class="LC_nobreak"><label>'.
                   4149:                                  '<input type="checkbox" name="HIDE'.$identifier.'" value="'.$version.':'.join('_',@unsolved).'" />'.
                   4150:                                  &mt('Hide').'</label></span>';
                   4151:             }
                   4152:             $prevattempts .= '</td>';
1.945     raeburn  4153:             if (@hidden) {
                   4154:                 foreach my $key (sort(keys(%lasthash))) {
1.989     raeburn  4155:                     next if ($key =~ /\.foilorder$/);
1.945     raeburn  4156:                     my $hide;
                   4157:                     foreach my $id (@hidden) {
                   4158:                         if ($key =~ /^\Q$id\E/) {
                   4159:                             $hide = 1;
                   4160:                             last;
                   4161:                         }
                   4162:                     }
                   4163:                     if ($hide) {
                   4164:                         my ($id,$data) = ($key =~ /^(.+)\.([^.]+)$/);
                   4165:                         if (($data eq 'award') || ($data eq 'awarddetail')) {
                   4166:                             my $value = &format_previous_attempt_value($key,
                   4167:                                              $returnhash{$version.':'.$key});
1.1173    kruse    4168:                             $prevattempts.='<td>'.$value.'&nbsp;</td>';
1.945     raeburn  4169:                         } else {
                   4170:                             $prevattempts.='<td>&nbsp;</td>';
                   4171:                         }
                   4172:                     } else {
                   4173:                         if ($key =~ /\./) {
1.1212    raeburn  4174:                             my $value = $returnhash{$version.':'.$key};
                   4175:                             if ($key =~ /\.rndseed$/) {
                   4176:                                 my ($id) = ($key =~ /^(.+)\.[^.]+$/);
                   4177:                                 if (exists($returnhash{$version.':'.$id.'.rawrndseed'})) {
                   4178:                                     $value = $returnhash{$version.':'.$id.'.rawrndseed'};
                   4179:                                 }
                   4180:                             }
                   4181:                             $prevattempts.='<td>'.&format_previous_attempt_value($key,$value).
                   4182:                                            '&nbsp;</td>';
1.945     raeburn  4183:                         } else {
                   4184:                             $prevattempts.='<td>&nbsp;</td>';
                   4185:                         }
                   4186:                     }
                   4187:                 }
                   4188:             } else {
                   4189: 	        foreach my $key (sort(keys(%lasthash))) {
1.989     raeburn  4190:                     next if ($key =~ /\.foilorder$/);
1.1212    raeburn  4191:                     my $value = $returnhash{$version.':'.$key};
                   4192:                     if ($key =~ /\.rndseed$/) {
                   4193:                         my ($id) = ($key =~ /^(.+)\.[^.]+$/);
                   4194:                         if (exists($returnhash{$version.':'.$id.'.rawrndseed'})) {
                   4195:                             $value = $returnhash{$version.':'.$id.'.rawrndseed'};
                   4196:                         }
                   4197:                     }
                   4198:                     $prevattempts.='<td>'.&format_previous_attempt_value($key,$value).
                   4199:                                    '&nbsp;</td>';
1.945     raeburn  4200: 	        }
                   4201:             }
                   4202: 	    $prevattempts.=&end_data_table_row();
1.40      ng       4203: 	 }
1.1       albertel 4204:       }
1.945     raeburn  4205:       my @currhidden = keys(%lasthidden);
1.596     albertel 4206:       $prevattempts.=&start_data_table_row().'<td>'.&mt('Current').'</td>';
1.356     albertel 4207:       foreach my $key (sort(keys(%lasthash))) {
1.989     raeburn  4208:           next if ($key =~ /\.foilorder$/);
1.945     raeburn  4209:           if (%typeparts) {
                   4210:               my $hidden;
                   4211:               foreach my $id (@currhidden) {
                   4212:                   if ($key =~ /^\Q$id\E/) {
                   4213:                       $hidden = 1;
                   4214:                       last;
                   4215:                   }
                   4216:               }
                   4217:               if ($hidden) {
                   4218:                   my ($id,$data) = ($key =~ /^(.+)\.([^.]+)$/);
                   4219:                   if (($data eq 'award') || ($data eq 'awarddetail')) {
                   4220:                       my $value = &format_previous_attempt_value($key,$lasthash{$key});
                   4221:                       if ($key =~/$regexp$/ && (defined &$gradesub)) {
                   4222:                           $value = &$gradesub($value);
                   4223:                       }
1.1173    kruse    4224:                       $prevattempts.='<td>'. $value.'&nbsp;</td>';
1.945     raeburn  4225:                   } else {
                   4226:                       $prevattempts.='<td>&nbsp;</td>';
                   4227:                   }
                   4228:               } else {
                   4229:                   my $value = &format_previous_attempt_value($key,$lasthash{$key});
                   4230:                   if ($key =~/$regexp$/ && (defined &$gradesub)) {
                   4231:                       $value = &$gradesub($value);
                   4232:                   }
1.1173    kruse    4233:                   $prevattempts.='<td>'.$value.'&nbsp;</td>';
1.945     raeburn  4234:               }
                   4235:           } else {
                   4236: 	      my $value = &format_previous_attempt_value($key,$lasthash{$key});
                   4237: 	      if ($key =~/$regexp$/ && (defined &$gradesub)) {
                   4238:                   $value = &$gradesub($value);
                   4239:               }
1.1173    kruse    4240: 	     $prevattempts.='<td>'.$value.'&nbsp;</td>';
1.945     raeburn  4241:           }
1.16      harris41 4242:       }
1.596     albertel 4243:       $prevattempts.= &end_data_table_row().&end_data_table();
1.1       albertel 4244:     } else {
1.596     albertel 4245:       $prevattempts=
                   4246: 	  &start_data_table().&start_data_table_row().
                   4247: 	  '<td>'.&mt('Nothing submitted - no attempts.').'</td>'.
                   4248: 	  &end_data_table_row().&end_data_table();
1.1       albertel 4249:     }
                   4250:   } else {
1.596     albertel 4251:     $prevattempts=
                   4252: 	  &start_data_table().&start_data_table_row().
                   4253: 	  '<td>'.&mt('No data.').'</td>'.
                   4254: 	  &end_data_table_row().&end_data_table();
1.1       albertel 4255:   }
1.10      albertel 4256: }
                   4257: 
1.581     albertel 4258: sub format_previous_attempt_value {
                   4259:     my ($key,$value) = @_;
1.1011    www      4260:     if (($key =~ /timestamp/) || ($key=~/duedate/)) {
1.1173    kruse    4261:         $value = &Apache::lonlocal::locallocaltime($value);
1.581     albertel 4262:     } elsif (ref($value) eq 'ARRAY') {
1.1173    kruse    4263:         $value = &HTML::Entities::encode('('.join(', ', @{ $value }).')','"<>&');
1.988     raeburn  4264:     } elsif ($key =~ /answerstring$/) {
                   4265:         my %answers = &Apache::lonnet::str2hash($value);
1.1173    kruse    4266:         my @answer = %answers;
                   4267:         %answers = map {&HTML::Entities::encode($_, '"<>&')} @answer;
1.988     raeburn  4268:         my @anskeys = sort(keys(%answers));
                   4269:         if (@anskeys == 1) {
                   4270:             my $answer = $answers{$anskeys[0]};
1.1001    raeburn  4271:             if ($answer =~ m{\0}) {
                   4272:                 $answer =~ s{\0}{,}g;
1.988     raeburn  4273:             }
                   4274:             my $tag_internal_answer_name = 'INTERNAL';
                   4275:             if ($anskeys[0] eq $tag_internal_answer_name) {
                   4276:                 $value = $answer; 
                   4277:             } else {
                   4278:                 $value = $anskeys[0].'='.$answer;
                   4279:             }
                   4280:         } else {
                   4281:             foreach my $ans (@anskeys) {
                   4282:                 my $answer = $answers{$ans};
1.1001    raeburn  4283:                 if ($answer =~ m{\0}) {
                   4284:                     $answer =~ s{\0}{,}g;
1.988     raeburn  4285:                 }
                   4286:                 $value .=  $ans.'='.$answer.'<br />';;
                   4287:             } 
                   4288:         }
1.581     albertel 4289:     } else {
1.1173    kruse    4290:         $value = &HTML::Entities::encode(&unescape($value), '"<>&');
1.581     albertel 4291:     }
                   4292:     return $value;
                   4293: }
                   4294: 
                   4295: 
1.107     albertel 4296: sub relative_to_absolute {
                   4297:     my ($url,$output)=@_;
                   4298:     my $parser=HTML::TokeParser->new(\$output);
                   4299:     my $token;
                   4300:     my $thisdir=$url;
                   4301:     my @rlinks=();
                   4302:     while ($token=$parser->get_token) {
                   4303: 	if ($token->[0] eq 'S') {
                   4304: 	    if ($token->[1] eq 'a') {
                   4305: 		if ($token->[2]->{'href'}) {
                   4306: 		    $rlinks[$#rlinks+1]=$token->[2]->{'href'};
                   4307: 		}
                   4308: 	    } elsif ($token->[1] eq 'img' || $token->[1] eq 'embed' ) {
                   4309: 		$rlinks[$#rlinks+1]=$token->[2]->{'src'};
                   4310: 	    } elsif ($token->[1] eq 'base') {
                   4311: 		$thisdir=$token->[2]->{'href'};
                   4312: 	    }
                   4313: 	}
                   4314:     }
                   4315:     $thisdir=~s-/[^/]*$--;
1.356     albertel 4316:     foreach my $link (@rlinks) {
1.726     raeburn  4317: 	unless (($link=~/^https?\:\/\//i) ||
1.356     albertel 4318: 		($link=~/^\//) ||
                   4319: 		($link=~/^javascript:/i) ||
                   4320: 		($link=~/^mailto:/i) ||
                   4321: 		($link=~/^\#/)) {
                   4322: 	    my $newlocation=&Apache::lonnet::hreflocation($thisdir,$link);
                   4323: 	    $output=~s/(\"|\'|\=\s*)\Q$link\E(\"|\'|\s|\>)/$1$newlocation$2/;
1.107     albertel 4324: 	}
                   4325:     }
                   4326: # -------------------------------------------------- Deal with Applet codebases
                   4327:     $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
                   4328:     return $output;
                   4329: }
                   4330: 
1.112     bowersj2 4331: =pod
                   4332: 
1.648     raeburn  4333: =item * &get_student_view()
1.112     bowersj2 4334: 
                   4335: show a snapshot of what student was looking at
                   4336: 
                   4337: =cut
                   4338: 
1.10      albertel 4339: sub get_student_view {
1.186     albertel 4340:   my ($symb,$username,$domain,$courseid,$target,$moreenv) = @_;
1.114     www      4341:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 4342:   my (%form);
1.10      albertel 4343:   my @elements=('symb','courseid','domain','username');
                   4344:   foreach my $element (@elements) {
1.186     albertel 4345:       $form{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 4346:   }
1.186     albertel 4347:   if (defined($moreenv)) {
                   4348:       %form=(%form,%{$moreenv});
                   4349:   }
1.236     albertel 4350:   if (defined($target)) { $form{'grade_target'} = $target; }
1.107     albertel 4351:   $feedurl=&Apache::lonnet::clutter($feedurl);
1.650     www      4352:   my ($userview,$response)=&Apache::lonnet::ssi_body($feedurl,%form);
1.11      albertel 4353:   $userview=~s/\<body[^\>]*\>//gi;
                   4354:   $userview=~s/\<\/body\>//gi;
                   4355:   $userview=~s/\<html\>//gi;
                   4356:   $userview=~s/\<\/html\>//gi;
                   4357:   $userview=~s/\<head\>//gi;
                   4358:   $userview=~s/\<\/head\>//gi;
                   4359:   $userview=~s/action\s*\=/would_be_action\=/gi;
1.107     albertel 4360:   $userview=&relative_to_absolute($feedurl,$userview);
1.650     www      4361:   if (wantarray) {
                   4362:      return ($userview,$response);
                   4363:   } else {
                   4364:      return $userview;
                   4365:   }
                   4366: }
                   4367: 
                   4368: sub get_student_view_with_retries {
                   4369:   my ($symb,$retries,$username,$domain,$courseid,$target,$moreenv) = @_;
                   4370: 
                   4371:     my $ok = 0;                 # True if we got a good response.
                   4372:     my $content;
                   4373:     my $response;
                   4374: 
                   4375:     # Try to get the student_view done. within the retries count:
                   4376:     
                   4377:     do {
                   4378:          ($content, $response) = &get_student_view($symb,$username,$domain,$courseid,$target,$moreenv);
                   4379:          $ok      = $response->is_success;
                   4380:          if (!$ok) {
                   4381:             &Apache::lonnet::logthis("Failed get_student_view_with_retries on $symb: ".$response->is_success.', '.$response->code.', '.$response->message);
                   4382:          }
                   4383:          $retries--;
                   4384:     } while (!$ok && ($retries > 0));
                   4385:     
                   4386:     if (!$ok) {
                   4387:        $content = '';          # On error return an empty content.
                   4388:     }
1.651     www      4389:     if (wantarray) {
                   4390:        return ($content, $response);
                   4391:     } else {
                   4392:        return $content;
                   4393:     }
1.11      albertel 4394: }
                   4395: 
1.112     bowersj2 4396: =pod
                   4397: 
1.648     raeburn  4398: =item * &get_student_answers() 
1.112     bowersj2 4399: 
                   4400: show a snapshot of how student was answering problem
                   4401: 
                   4402: =cut
                   4403: 
1.11      albertel 4404: sub get_student_answers {
1.100     sakharuk 4405:   my ($symb,$username,$domain,$courseid,%form) = @_;
1.114     www      4406:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 4407:   my (%moreenv);
1.11      albertel 4408:   my @elements=('symb','courseid','domain','username');
                   4409:   foreach my $element (@elements) {
1.186     albertel 4410:     $moreenv{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 4411:   }
1.186     albertel 4412:   $moreenv{'grade_target'}='answer';
                   4413:   %moreenv=(%form,%moreenv);
1.497     raeburn  4414:   $feedurl = &Apache::lonnet::clutter($feedurl);
                   4415:   my $userview=&Apache::lonnet::ssi($feedurl,%moreenv);
1.10      albertel 4416:   return $userview;
1.1       albertel 4417: }
1.116     albertel 4418: 
                   4419: =pod
                   4420: 
                   4421: =item * &submlink()
                   4422: 
1.242     albertel 4423: Inputs: $text $uname $udom $symb $target
1.116     albertel 4424: 
                   4425: Returns: A link to grades.pm such as to see the SUBM view of a student
                   4426: 
                   4427: =cut
                   4428: 
                   4429: ###############################################
                   4430: sub submlink {
1.242     albertel 4431:     my ($text,$uname,$udom,$symb,$target)=@_;
1.116     albertel 4432:     if (!($uname && $udom)) {
                   4433: 	(my $cursymb, my $courseid,$udom,$uname)=
1.463     albertel 4434: 	    &Apache::lonnet::whichuser($symb);
1.116     albertel 4435: 	if (!$symb) { $symb=$cursymb; }
                   4436:     }
1.254     matthew  4437:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      4438:     $symb=&escape($symb);
1.960     bisitz   4439:     if ($target) { $target=" target=\"$target\""; }
                   4440:     return
                   4441:         '<a href="/adm/grades?command=submission'.
                   4442:         '&amp;symb='.$symb.
                   4443:         '&amp;student='.$uname.
                   4444:         '&amp;userdom='.$udom.'"'.
                   4445:         $target.'>'.$text.'</a>';
1.242     albertel 4446: }
                   4447: ##############################################
                   4448: 
                   4449: =pod
                   4450: 
                   4451: =item * &pgrdlink()
                   4452: 
                   4453: Inputs: $text $uname $udom $symb $target
                   4454: 
                   4455: Returns: A link to grades.pm such as to see the PGRD view of a student
                   4456: 
                   4457: =cut
                   4458: 
                   4459: ###############################################
                   4460: sub pgrdlink {
                   4461:     my $link=&submlink(@_);
                   4462:     $link=~s/(&command=submission)/$1&showgrading=yes/;
                   4463:     return $link;
                   4464: }
                   4465: ##############################################
                   4466: 
                   4467: =pod
                   4468: 
                   4469: =item * &pprmlink()
                   4470: 
                   4471: Inputs: $text $uname $udom $symb $target
                   4472: 
                   4473: Returns: A link to parmset.pm such as to see the PPRM view of a
1.283     albertel 4474: student and a specific resource
1.242     albertel 4475: 
                   4476: =cut
                   4477: 
                   4478: ###############################################
                   4479: sub pprmlink {
                   4480:     my ($text,$uname,$udom,$symb,$target)=@_;
                   4481:     if (!($uname && $udom)) {
                   4482: 	(my $cursymb, my $courseid,$udom,$uname)=
1.463     albertel 4483: 	    &Apache::lonnet::whichuser($symb);
1.242     albertel 4484: 	if (!$symb) { $symb=$cursymb; }
                   4485:     }
1.254     matthew  4486:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      4487:     $symb=&escape($symb);
1.242     albertel 4488:     if ($target) { $target="target=\"$target\""; }
1.595     albertel 4489:     return '<a href="/adm/parmset?command=set&amp;'.
                   4490: 	'symb='.$symb.'&amp;uname='.$uname.
                   4491: 	'&amp;udom='.$udom.'" '.$target.'>'.$text.'</a>';
1.116     albertel 4492: }
                   4493: ##############################################
1.37      matthew  4494: 
1.112     bowersj2 4495: =pod
                   4496: 
                   4497: =back
                   4498: 
                   4499: =cut
                   4500: 
1.37      matthew  4501: ###############################################
1.51      www      4502: 
                   4503: 
                   4504: sub timehash {
1.687     raeburn  4505:     my ($thistime) = @_;
                   4506:     my $timezone = &Apache::lonlocal::gettimezone();
                   4507:     my $dt = DateTime->from_epoch(epoch => $thistime)
                   4508:                      ->set_time_zone($timezone);
                   4509:     my $wday = $dt->day_of_week();
                   4510:     if ($wday == 7) { $wday = 0; }
                   4511:     return ( 'second' => $dt->second(),
                   4512:              'minute' => $dt->minute(),
                   4513:              'hour'   => $dt->hour(),
                   4514:              'day'     => $dt->day_of_month(),
                   4515:              'month'   => $dt->month(),
                   4516:              'year'    => $dt->year(),
                   4517:              'weekday' => $wday,
                   4518:              'dayyear' => $dt->day_of_year(),
                   4519:              'dlsav'   => $dt->is_dst() );
1.51      www      4520: }
                   4521: 
1.370     www      4522: sub utc_string {
                   4523:     my ($date)=@_;
1.371     www      4524:     return strftime("%Y%m%dT%H%M%SZ",gmtime($date));
1.370     www      4525: }
                   4526: 
1.51      www      4527: sub maketime {
                   4528:     my %th=@_;
1.687     raeburn  4529:     my ($epoch_time,$timezone,$dt);
                   4530:     $timezone = &Apache::lonlocal::gettimezone();
                   4531:     eval {
                   4532:         $dt = DateTime->new( year   => $th{'year'},
                   4533:                              month  => $th{'month'},
                   4534:                              day    => $th{'day'},
                   4535:                              hour   => $th{'hour'},
                   4536:                              minute => $th{'minute'},
                   4537:                              second => $th{'second'},
                   4538:                              time_zone => $timezone,
                   4539:                          );
                   4540:     };
                   4541:     if (!$@) {
                   4542:         $epoch_time = $dt->epoch;
                   4543:         if ($epoch_time) {
                   4544:             return $epoch_time;
                   4545:         }
                   4546:     }
1.51      www      4547:     return POSIX::mktime(
                   4548:         ($th{'seconds'},$th{'minutes'},$th{'hours'},
1.210     www      4549:          $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,-1));
1.70      www      4550: }
                   4551: 
                   4552: #########################################
1.51      www      4553: 
                   4554: sub findallcourses {
1.482     raeburn  4555:     my ($roles,$uname,$udom) = @_;
1.355     albertel 4556:     my %roles;
                   4557:     if (ref($roles)) { %roles = map { $_ => 1 } @{$roles}; }
1.348     albertel 4558:     my %courses;
1.51      www      4559:     my $now=time;
1.482     raeburn  4560:     if (!defined($uname)) {
                   4561:         $uname = $env{'user.name'};
                   4562:     }
                   4563:     if (!defined($udom)) {
                   4564:         $udom = $env{'user.domain'};
                   4565:     }
                   4566:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.1073    raeburn  4567:         my %roleshash = &Apache::lonnet::dump('roles',$udom,$uname);
1.482     raeburn  4568:         if (!%roles) {
                   4569:             %roles = (
                   4570:                        cc => 1,
1.907     raeburn  4571:                        co => 1,
1.482     raeburn  4572:                        in => 1,
                   4573:                        ep => 1,
                   4574:                        ta => 1,
                   4575:                        cr => 1,
                   4576:                        st => 1,
                   4577:              );
                   4578:         }
                   4579:         foreach my $entry (keys(%roleshash)) {
                   4580:             my ($trole,$tend,$tstart) = split(/_/,$roleshash{$entry});
                   4581:             if ($trole =~ /^cr/) { 
                   4582:                 next if (!exists($roles{$trole}) && !exists($roles{'cr'}));
                   4583:             } else {
                   4584:                 next if (!exists($roles{$trole}));
                   4585:             }
                   4586:             if ($tend) {
                   4587:                 next if ($tend < $now);
                   4588:             }
                   4589:             if ($tstart) {
                   4590:                 next if ($tstart > $now);
                   4591:             }
1.1058    raeburn  4592:             my ($cdom,$cnum,$sec,$cnumpart,$secpart,$role);
1.482     raeburn  4593:             (undef,$cdom,$cnumpart,$secpart) = split(/\//,$entry);
1.1058    raeburn  4594:             my $value = $trole.'/'.$cdom.'/';
1.482     raeburn  4595:             if ($secpart eq '') {
                   4596:                 ($cnum,$role) = split(/_/,$cnumpart); 
                   4597:                 $sec = 'none';
1.1058    raeburn  4598:                 $value .= $cnum.'/';
1.482     raeburn  4599:             } else {
                   4600:                 $cnum = $cnumpart;
                   4601:                 ($sec,$role) = split(/_/,$secpart);
1.1058    raeburn  4602:                 $value .= $cnum.'/'.$sec;
                   4603:             }
                   4604:             if (ref($courses{$cdom.'_'.$cnum}{$sec}) eq 'ARRAY') {
                   4605:                 unless (grep(/^\Q$value\E$/,@{$courses{$cdom.'_'.$cnum}{$sec}})) {
                   4606:                     push(@{$courses{$cdom.'_'.$cnum}{$sec}},$value);
                   4607:                 }
                   4608:             } else {
                   4609:                 @{$courses{$cdom.'_'.$cnum}{$sec}} = ($value);
1.490     raeburn  4610:             }
1.482     raeburn  4611:         }
                   4612:     } else {
                   4613:         foreach my $key (keys(%env)) {
1.483     albertel 4614: 	    if ( $key=~m{^user\.role\.(\w+)\./($match_domain)/($match_courseid)/?(\w*)$} ||
                   4615:                  $key=~m{^user\.role\.(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_courseid)/?(\w*)$}) {
1.482     raeburn  4616: 	        my ($role,$cdom,$cnum,$sec) = ($1,$2,$3,$4);
                   4617: 	        next if ($role eq 'ca' || $role eq 'aa');
                   4618: 	        next if (%roles && !exists($roles{$role}));
                   4619: 	        my ($starttime,$endtime)=split(/\./,$env{$key});
                   4620:                 my $active=1;
                   4621:                 if ($starttime) {
                   4622: 		    if ($now<$starttime) { $active=0; }
                   4623:                 }
                   4624:                 if ($endtime) {
                   4625:                     if ($now>$endtime) { $active=0; }
                   4626:                 }
                   4627:                 if ($active) {
1.1058    raeburn  4628:                     my $value = $role.'/'.$cdom.'/'.$cnum.'/';
1.482     raeburn  4629:                     if ($sec eq '') {
                   4630:                         $sec = 'none';
1.1058    raeburn  4631:                     } else {
                   4632:                         $value .= $sec;
                   4633:                     }
                   4634:                     if (ref($courses{$cdom.'_'.$cnum}{$sec}) eq 'ARRAY') {
                   4635:                         unless (grep(/^\Q$value\E$/,@{$courses{$cdom.'_'.$cnum}{$sec}})) {
                   4636:                             push(@{$courses{$cdom.'_'.$cnum}{$sec}},$value);
                   4637:                         }
                   4638:                     } else {
                   4639:                         @{$courses{$cdom.'_'.$cnum}{$sec}} = ($value);
1.482     raeburn  4640:                     }
1.474     raeburn  4641:                 }
                   4642:             }
1.51      www      4643:         }
                   4644:     }
1.474     raeburn  4645:     return %courses;
1.51      www      4646: }
1.37      matthew  4647: 
1.54      www      4648: ###############################################
1.474     raeburn  4649: 
                   4650: sub blockcheck {
1.1189    raeburn  4651:     my ($setters,$activity,$uname,$udom,$url,$is_course) = @_;
1.490     raeburn  4652: 
1.1189    raeburn  4653:     if (defined($udom) && defined($uname)) {
                   4654:         # If uname and udom are for a course, check for blocks in the course.
                   4655:         if (($is_course) || (&Apache::lonnet::is_course($udom,$uname))) {
                   4656:             my ($startblock,$endblock,$triggerblock) =
                   4657:                 &get_blocks($setters,$activity,$udom,$uname,$url);
                   4658:             return ($startblock,$endblock,$triggerblock);
                   4659:         }
                   4660:     } else {
1.490     raeburn  4661:         $udom = $env{'user.domain'};
                   4662:         $uname = $env{'user.name'};
                   4663:     }
                   4664: 
1.502     raeburn  4665:     my $startblock = 0;
                   4666:     my $endblock = 0;
1.1062    raeburn  4667:     my $triggerblock = '';
1.482     raeburn  4668:     my %live_courses = &findallcourses(undef,$uname,$udom);
1.474     raeburn  4669: 
1.490     raeburn  4670:     # If uname is for a user, and activity is course-specific, i.e.,
                   4671:     # boards, chat or groups, check for blocking in current course only.
1.474     raeburn  4672: 
1.490     raeburn  4673:     if (($activity eq 'boards' || $activity eq 'chat' ||
1.1189    raeburn  4674:          $activity eq 'groups' || $activity eq 'printout') &&
                   4675:         ($env{'request.course.id'})) {
1.490     raeburn  4676:         foreach my $key (keys(%live_courses)) {
                   4677:             if ($key ne $env{'request.course.id'}) {
                   4678:                 delete($live_courses{$key});
                   4679:             }
                   4680:         }
                   4681:     }
                   4682: 
                   4683:     my $otheruser = 0;
                   4684:     my %own_courses;
                   4685:     if ((($uname ne $env{'user.name'})) || ($udom ne $env{'user.domain'})) {
                   4686:         # Resource belongs to user other than current user.
                   4687:         $otheruser = 1;
                   4688:         # Gather courses for current user
                   4689:         %own_courses = 
                   4690:             &findallcourses(undef,$env{'user.name'},$env{'user.domain'});
                   4691:     }
                   4692: 
                   4693:     # Gather active course roles - course coordinator, instructor, 
                   4694:     # exam proctor, ta, student, or custom role.
1.474     raeburn  4695: 
                   4696:     foreach my $course (keys(%live_courses)) {
1.482     raeburn  4697:         my ($cdom,$cnum);
                   4698:         if ((defined($env{'course.'.$course.'.domain'})) && (defined($env{'course.'.$course.'.num'}))) {
                   4699:             $cdom = $env{'course.'.$course.'.domain'};
                   4700:             $cnum = $env{'course.'.$course.'.num'};
                   4701:         } else {
1.490     raeburn  4702:             ($cdom,$cnum) = split(/_/,$course); 
1.482     raeburn  4703:         }
                   4704:         my $no_ownblock = 0;
                   4705:         my $no_userblock = 0;
1.533     raeburn  4706:         if ($otheruser && $activity ne 'com') {
1.490     raeburn  4707:             # Check if current user has 'evb' priv for this
                   4708:             if (defined($own_courses{$course})) {
                   4709:                 foreach my $sec (keys(%{$own_courses{$course}})) {
                   4710:                     my $checkrole = 'cm./'.$cdom.'/'.$cnum;
                   4711:                     if ($sec ne 'none') {
                   4712:                         $checkrole .= '/'.$sec;
                   4713:                     }
                   4714:                     if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
                   4715:                         $no_ownblock = 1;
                   4716:                         last;
                   4717:                     }
                   4718:                 }
                   4719:             }
                   4720:             # if they have 'evb' priv and are currently not playing student
                   4721:             next if (($no_ownblock) &&
                   4722:                  ($env{'request.role'} !~ m{^st\./$cdom/$cnum}));
                   4723:         }
1.474     raeburn  4724:         foreach my $sec (keys(%{$live_courses{$course}})) {
1.482     raeburn  4725:             my $checkrole = 'cm./'.$cdom.'/'.$cnum;
1.474     raeburn  4726:             if ($sec ne 'none') {
1.482     raeburn  4727:                 $checkrole .= '/'.$sec;
1.474     raeburn  4728:             }
1.490     raeburn  4729:             if ($otheruser) {
                   4730:                 # Resource belongs to user other than current user.
                   4731:                 # Assemble privs for that user, and check for 'evb' priv.
1.1058    raeburn  4732:                 my (%allroles,%userroles);
                   4733:                 if (ref($live_courses{$course}{$sec}) eq 'ARRAY') {
                   4734:                     foreach my $entry (@{$live_courses{$course}{$sec}}) { 
                   4735:                         my ($trole,$tdom,$tnum,$tsec);
                   4736:                         if ($entry =~ /^cr/) {
                   4737:                             ($trole,$tdom,$tnum,$tsec) = 
                   4738:                                 ($entry =~ m|^(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_username)/?(\w*)$|);
                   4739:                         } else {
                   4740:                            ($trole,$tdom,$tnum,$tsec) = split(/\//,$entry);
                   4741:                         }
                   4742:                         my ($spec,$area,$trest);
                   4743:                         $area = '/'.$tdom.'/'.$tnum;
                   4744:                         $trest = $tnum;
                   4745:                         if ($tsec ne '') {
                   4746:                             $area .= '/'.$tsec;
                   4747:                             $trest .= '/'.$tsec;
                   4748:                         }
                   4749:                         $spec = $trole.'.'.$area;
                   4750:                         if ($trole =~ /^cr/) {
                   4751:                             &Apache::lonnet::custom_roleprivs(\%allroles,$trole,
                   4752:                                                               $tdom,$spec,$trest,$area);
                   4753:                         } else {
                   4754:                             &Apache::lonnet::standard_roleprivs(\%allroles,$trole,
                   4755:                                                                 $tdom,$spec,$trest,$area);
                   4756:                         }
                   4757:                     }
                   4758:                     my ($author,$adv) = &Apache::lonnet::set_userprivs(\%userroles,\%allroles);
                   4759:                     if ($userroles{'user.priv.'.$checkrole} =~ /evb\&([^\:]*)/) {
                   4760:                         if ($1) {
                   4761:                             $no_userblock = 1;
                   4762:                             last;
                   4763:                         }
1.486     raeburn  4764:                     }
                   4765:                 }
1.490     raeburn  4766:             } else {
                   4767:                 # Resource belongs to current user
                   4768:                 # Check for 'evb' priv via lonnet::allowed().
1.482     raeburn  4769:                 if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
                   4770:                     $no_ownblock = 1;
                   4771:                     last;
                   4772:                 }
1.474     raeburn  4773:             }
                   4774:         }
                   4775:         # if they have the evb priv and are currently not playing student
1.482     raeburn  4776:         next if (($no_ownblock) &&
1.491     albertel 4777:                  ($env{'request.role'} !~ m{^st\./\Q$cdom\E/\Q$cnum\E}));
1.482     raeburn  4778:         next if ($no_userblock);
1.474     raeburn  4779: 
1.866     kalberla 4780:         # Retrieve blocking times and identity of locker for course
1.490     raeburn  4781:         # of specified user, unless user has 'evb' privilege.
1.502     raeburn  4782:         
1.1062    raeburn  4783:         my ($start,$end,$trigger) = 
                   4784:             &get_blocks($setters,$activity,$cdom,$cnum,$url);
1.502     raeburn  4785:         if (($start != 0) && 
                   4786:             (($startblock == 0) || ($startblock > $start))) {
                   4787:             $startblock = $start;
1.1062    raeburn  4788:             if ($trigger ne '') {
                   4789:                 $triggerblock = $trigger;
                   4790:             }
1.502     raeburn  4791:         }
                   4792:         if (($end != 0)  &&
                   4793:             (($endblock == 0) || ($endblock < $end))) {
                   4794:             $endblock = $end;
1.1062    raeburn  4795:             if ($trigger ne '') {
                   4796:                 $triggerblock = $trigger;
                   4797:             }
1.502     raeburn  4798:         }
1.490     raeburn  4799:     }
1.1062    raeburn  4800:     return ($startblock,$endblock,$triggerblock);
1.490     raeburn  4801: }
                   4802: 
                   4803: sub get_blocks {
1.1062    raeburn  4804:     my ($setters,$activity,$cdom,$cnum,$url) = @_;
1.490     raeburn  4805:     my $startblock = 0;
                   4806:     my $endblock = 0;
1.1062    raeburn  4807:     my $triggerblock = '';
1.490     raeburn  4808:     my $course = $cdom.'_'.$cnum;
                   4809:     $setters->{$course} = {};
                   4810:     $setters->{$course}{'staff'} = [];
                   4811:     $setters->{$course}{'times'} = [];
1.1062    raeburn  4812:     $setters->{$course}{'triggers'} = [];
                   4813:     my (@blockers,%triggered);
                   4814:     my $now = time;
                   4815:     my %commblocks = &Apache::lonnet::get_comm_blocks($cdom,$cnum);
                   4816:     if ($activity eq 'docs') {
                   4817:         @blockers = &Apache::lonnet::has_comm_blocking('bre',undef,$url,\%commblocks);
                   4818:         foreach my $block (@blockers) {
                   4819:             if ($block =~ /^firstaccess____(.+)$/) {
                   4820:                 my $item = $1;
                   4821:                 my $type = 'map';
                   4822:                 my $timersymb = $item;
                   4823:                 if ($item eq 'course') {
                   4824:                     $type = 'course';
                   4825:                 } elsif ($item =~ /___\d+___/) {
                   4826:                     $type = 'resource';
                   4827:                 } else {
                   4828:                     $timersymb = &Apache::lonnet::symbread($item);
                   4829:                 }
                   4830:                 my $start = $env{'course.'.$cdom.'_'.$cnum.'.firstaccess.'.$timersymb};
                   4831:                 my $end = $start + $env{'course.'.$cdom.'_'.$cnum.'.timerinterval.'.$timersymb};
                   4832:                 $triggered{$block} = {
                   4833:                                        start => $start,
                   4834:                                        end   => $end,
                   4835:                                        type  => $type,
                   4836:                                      };
                   4837:             }
                   4838:         }
                   4839:     } else {
                   4840:         foreach my $block (keys(%commblocks)) {
                   4841:             if ($block =~ m/^(\d+)____(\d+)$/) { 
                   4842:                 my ($start,$end) = ($1,$2);
                   4843:                 if ($start <= time && $end >= time) {
                   4844:                     if (ref($commblocks{$block}) eq 'HASH') {
                   4845:                         if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   4846:                             if ($commblocks{$block}{'blocks'}{$activity} eq 'on') {
                   4847:                                 unless(grep(/^\Q$block\E$/,@blockers)) {
                   4848:                                     push(@blockers,$block);
                   4849:                                 }
                   4850:                             }
                   4851:                         }
                   4852:                     }
                   4853:                 }
                   4854:             } elsif ($block =~ /^firstaccess____(.+)$/) {
                   4855:                 my $item = $1;
                   4856:                 my $timersymb = $item; 
                   4857:                 my $type = 'map';
                   4858:                 if ($item eq 'course') {
                   4859:                     $type = 'course';
                   4860:                 } elsif ($item =~ /___\d+___/) {
                   4861:                     $type = 'resource';
                   4862:                 } else {
                   4863:                     $timersymb = &Apache::lonnet::symbread($item);
                   4864:                 }
                   4865:                 my $start = $env{'course.'.$cdom.'_'.$cnum.'.firstaccess.'.$timersymb};
                   4866:                 my $end = $start + $env{'course.'.$cdom.'_'.$cnum.'.timerinterval.'.$timersymb}; 
                   4867:                 if ($start && $end) {
                   4868:                     if (($start <= time) && ($end >= time)) {
                   4869:                         unless (grep(/^\Q$block\E$/,@blockers)) {
                   4870:                             push(@blockers,$block);
                   4871:                             $triggered{$block} = {
                   4872:                                                    start => $start,
                   4873:                                                    end   => $end,
                   4874:                                                    type  => $type,
                   4875:                                                  };
                   4876:                         }
                   4877:                     }
1.490     raeburn  4878:                 }
1.1062    raeburn  4879:             }
                   4880:         }
                   4881:     }
                   4882:     foreach my $blocker (@blockers) {
                   4883:         my ($staff_name,$staff_dom,$title,$blocks) =
                   4884:             &parse_block_record($commblocks{$blocker});
                   4885:         push(@{$$setters{$course}{'staff'}},[$staff_name,$staff_dom]);
                   4886:         my ($start,$end,$triggertype);
                   4887:         if ($blocker =~ m/^(\d+)____(\d+)$/) {
                   4888:             ($start,$end) = ($1,$2);
                   4889:         } elsif (ref($triggered{$blocker}) eq 'HASH') {
                   4890:             $start = $triggered{$blocker}{'start'};
                   4891:             $end = $triggered{$blocker}{'end'};
                   4892:             $triggertype = $triggered{$blocker}{'type'};
                   4893:         }
                   4894:         if ($start) {
                   4895:             push(@{$$setters{$course}{'times'}}, [$start,$end]);
                   4896:             if ($triggertype) {
                   4897:                 push(@{$$setters{$course}{'triggers'}},$triggertype);
                   4898:             } else {
                   4899:                 push(@{$$setters{$course}{'triggers'}},0);
                   4900:             }
                   4901:             if ( ($startblock == 0) || ($startblock > $start) ) {
                   4902:                 $startblock = $start;
                   4903:                 if ($triggertype) {
                   4904:                     $triggerblock = $blocker;
1.474     raeburn  4905:                 }
                   4906:             }
1.1062    raeburn  4907:             if ( ($endblock == 0) || ($endblock < $end) ) {
                   4908:                $endblock = $end;
                   4909:                if ($triggertype) {
                   4910:                    $triggerblock = $blocker;
                   4911:                }
                   4912:             }
1.474     raeburn  4913:         }
                   4914:     }
1.1062    raeburn  4915:     return ($startblock,$endblock,$triggerblock);
1.474     raeburn  4916: }
                   4917: 
                   4918: sub parse_block_record {
                   4919:     my ($record) = @_;
                   4920:     my ($setuname,$setudom,$title,$blocks);
                   4921:     if (ref($record) eq 'HASH') {
                   4922:         ($setuname,$setudom) = split(/:/,$record->{'setter'});
                   4923:         $title = &unescape($record->{'event'});
                   4924:         $blocks = $record->{'blocks'};
                   4925:     } else {
                   4926:         my @data = split(/:/,$record,3);
                   4927:         if (scalar(@data) eq 2) {
                   4928:             $title = $data[1];
                   4929:             ($setuname,$setudom) = split(/@/,$data[0]);
                   4930:         } else {
                   4931:             ($setuname,$setudom,$title) = @data;
                   4932:         }
                   4933:         $blocks = { 'com' => 'on' };
                   4934:     }
                   4935:     return ($setuname,$setudom,$title,$blocks);
                   4936: }
                   4937: 
1.854     kalberla 4938: sub blocking_status {
1.1189    raeburn  4939:     my ($activity,$uname,$udom,$url,$is_course) = @_;
1.1061    raeburn  4940:     my %setters;
1.890     droeschl 4941: 
1.1061    raeburn  4942: # check for active blocking
1.1062    raeburn  4943:     my ($startblock,$endblock,$triggerblock) = 
1.1189    raeburn  4944:         &blockcheck(\%setters,$activity,$uname,$udom,$url,$is_course);
1.1062    raeburn  4945:     my $blocked = 0;
                   4946:     if ($startblock && $endblock) {
                   4947:         $blocked = 1;
                   4948:     }
1.890     droeschl 4949: 
1.1061    raeburn  4950: # caller just wants to know whether a block is active
                   4951:     if (!wantarray) { return $blocked; }
                   4952: 
                   4953: # build a link to a popup window containing the details
                   4954:     my $querystring  = "?activity=$activity";
                   4955: # $uname and $udom decide whose portfolio the user is trying to look at
1.1062    raeburn  4956:     if ($activity eq 'port') {
                   4957:         $querystring .= "&amp;udom=$udom"      if $udom;
                   4958:         $querystring .= "&amp;uname=$uname"    if $uname;
                   4959:     } elsif ($activity eq 'docs') {
                   4960:         $querystring .= '&amp;url='.&HTML::Entities::encode($url,'&"');
                   4961:     }
1.1061    raeburn  4962: 
                   4963:     my $output .= <<'END_MYBLOCK';
                   4964: function openWindow(url, wdwName, w, h, toolbar,scrollbar) {
                   4965:     var options = "width=" + w + ",height=" + h + ",";
                   4966:     options += "resizable=yes,scrollbars="+scrollbar+",status=no,";
                   4967:     options += "menubar=no,toolbar="+toolbar+",location=no,directories=no";
                   4968:     var newWin = window.open(url, wdwName, options);
                   4969:     newWin.focus();
                   4970: }
1.890     droeschl 4971: END_MYBLOCK
1.854     kalberla 4972: 
1.1061    raeburn  4973:     $output = Apache::lonhtmlcommon::scripttag($output);
1.890     droeschl 4974:   
1.1061    raeburn  4975:     my $popupUrl = "/adm/blockingstatus/$querystring";
1.1062    raeburn  4976:     my $text = &mt('Communication Blocked');
1.1217    raeburn  4977:     my $class = 'LC_comblock';
1.1062    raeburn  4978:     if ($activity eq 'docs') {
                   4979:         $text = &mt('Content Access Blocked');
1.1217    raeburn  4980:         $class = '';
1.1063    raeburn  4981:     } elsif ($activity eq 'printout') {
                   4982:         $text = &mt('Printing Blocked');
1.1062    raeburn  4983:     }
1.1061    raeburn  4984:     $output .= <<"END_BLOCK";
1.1217    raeburn  4985: <div class='$class'>
1.869     kalberla 4986:   <a onclick='openWindow("$popupUrl","Blocking Table",600,300,"no","no");return false;' href='/adm/blockingstatus/$querystring'
1.890     droeschl 4987:   title='$text'>
                   4988:   <img class='LC_noBorder LC_middle' title='$text' src='/res/adm/pages/comblock.png' alt='$text'/></a>
1.869     kalberla 4989:   <a onclick='openWindow("$popupUrl","Blocking Table",600,300,"no","no");return false;' href='/adm/blockingstatus/$querystring' 
1.890     droeschl 4990:   title='$text'>$text</a>
1.867     kalberla 4991: </div>
                   4992: 
                   4993: END_BLOCK
1.474     raeburn  4994: 
1.1061    raeburn  4995:     return ($blocked, $output);
1.854     kalberla 4996: }
1.490     raeburn  4997: 
1.60      matthew  4998: ###############################################
                   4999: 
1.682     raeburn  5000: sub check_ip_acc {
1.1201    raeburn  5001:     my ($acc,$clientip)=@_;
1.682     raeburn  5002:     &Apache::lonxml::debug("acc is $acc");
                   5003:     if (!defined($acc) || $acc =~ /^\s*$/ || $acc =~/^\s*no\s*$/i) {
                   5004:         return 1;
                   5005:     }
1.1219    raeburn  5006:     my $allowed;
1.1201    raeburn  5007:     my $ip=$env{'request.host'} || $ENV{'REMOTE_ADDR'} || $clientip;
1.682     raeburn  5008: 
                   5009:     my $name;
1.1219    raeburn  5010:     my %access = (
                   5011:                      allowfrom => 1,
                   5012:                      denyfrom  => 0,
                   5013:                  );
                   5014:     my @allows;
                   5015:     my @denies;
                   5016:     foreach my $item (split(',',$acc)) {
                   5017:         $item =~ s/^\s*//;
                   5018:         $item =~ s/\s*$//;
                   5019:         my $pattern;
                   5020:         if ($item =~ /^\!(.+)$/) {
                   5021:             push(@denies,$1);
                   5022:         } else {
                   5023:             push(@allows,$item);
                   5024:         }
                   5025:    }
                   5026:    my $numdenies = scalar(@denies);
                   5027:    my $numallows = scalar(@allows);
                   5028:    my $count = 0;
                   5029:    foreach my $pattern (@denies,@allows) {
                   5030:         $count ++; 
                   5031:         my $acctype = 'allowfrom';
                   5032:         if ($count <= $numdenies) {
                   5033:             $acctype = 'denyfrom';
                   5034:         }
1.682     raeburn  5035:         if ($pattern =~ /\*$/) {
                   5036:             #35.8.*
                   5037:             $pattern=~s/\*//;
1.1219    raeburn  5038:             if ($ip =~ /^\Q$pattern\E/) { $allowed=$access{$acctype}; }
1.682     raeburn  5039:         } elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {
                   5040:             #35.8.3.[34-56]
                   5041:             my $low=$2;
                   5042:             my $high=$3;
                   5043:             $pattern=$1;
                   5044:             if ($ip =~ /^\Q$pattern\E/) {
                   5045:                 my $last=(split(/\./,$ip))[3];
1.1219    raeburn  5046:                 if ($last <=$high && $last >=$low) { $allowed=$access{$acctype}; }
1.682     raeburn  5047:             }
                   5048:         } elsif ($pattern =~ /^\*/) {
                   5049:             #*.msu.edu
                   5050:             $pattern=~s/\*//;
                   5051:             if (!defined($name)) {
                   5052:                 use Socket;
                   5053:                 my $netaddr=inet_aton($ip);
                   5054:                 ($name)=gethostbyaddr($netaddr,AF_INET);
                   5055:             }
1.1219    raeburn  5056:             if ($name =~ /\Q$pattern\E$/i) { $allowed=$access{$acctype}; }
1.682     raeburn  5057:         } elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
                   5058:             #127.0.0.1
1.1219    raeburn  5059:             if ($ip =~ /^\Q$pattern\E/) { $allowed=$access{$acctype}; }
1.682     raeburn  5060:         } else {
                   5061:             #some.name.com
                   5062:             if (!defined($name)) {
                   5063:                 use Socket;
                   5064:                 my $netaddr=inet_aton($ip);
                   5065:                 ($name)=gethostbyaddr($netaddr,AF_INET);
                   5066:             }
1.1219    raeburn  5067:             if ($name =~ /\Q$pattern\E$/i) { $allowed=$access{$acctype}; }
                   5068:         }
                   5069:         if ($allowed =~ /^(0|1)$/) { last; }
                   5070:     }
                   5071:     if ($allowed eq '') {
                   5072:         if ($numdenies && !$numallows) {
                   5073:             $allowed = 1;
                   5074:         } else {
                   5075:             $allowed = 0;
1.682     raeburn  5076:         }
                   5077:     }
                   5078:     return $allowed;
                   5079: }
                   5080: 
                   5081: ###############################################
                   5082: 
1.60      matthew  5083: =pod
                   5084: 
1.112     bowersj2 5085: =head1 Domain Template Functions
                   5086: 
                   5087: =over 4
                   5088: 
                   5089: =item * &determinedomain()
1.60      matthew  5090: 
                   5091: Inputs: $domain (usually will be undef)
                   5092: 
1.63      www      5093: Returns: Determines which domain should be used for designs
1.60      matthew  5094: 
                   5095: =cut
1.54      www      5096: 
1.60      matthew  5097: ###############################################
1.63      www      5098: sub determinedomain {
                   5099:     my $domain=shift;
1.531     albertel 5100:     if (! $domain) {
1.60      matthew  5101:         # Determine domain if we have not been given one
1.893     raeburn  5102:         $domain = &Apache::lonnet::default_login_domain();
1.258     albertel 5103:         if ($env{'user.domain'}) { $domain=$env{'user.domain'}; }
                   5104:         if ($env{'request.role.domain'}) { 
                   5105:             $domain=$env{'request.role.domain'}; 
1.60      matthew  5106:         }
                   5107:     }
1.63      www      5108:     return $domain;
                   5109: }
                   5110: ###############################################
1.517     raeburn  5111: 
1.518     albertel 5112: sub devalidate_domconfig_cache {
                   5113:     my ($udom)=@_;
                   5114:     &Apache::lonnet::devalidate_cache_new('domainconfig',$udom);
                   5115: }
                   5116: 
                   5117: # ---------------------- Get domain configuration for a domain
                   5118: sub get_domainconf {
                   5119:     my ($udom) = @_;
                   5120:     my $cachetime=1800;
                   5121:     my ($result,$cached)=&Apache::lonnet::is_cached_new('domainconfig',$udom);
                   5122:     if (defined($cached)) { return %{$result}; }
                   5123: 
                   5124:     my %domconfig = &Apache::lonnet::get_dom('configuration',
1.948     raeburn  5125: 					     ['login','rolecolors','autoenroll'],$udom);
1.632     raeburn  5126:     my (%designhash,%legacy);
1.518     albertel 5127:     if (keys(%domconfig) > 0) {
                   5128:         if (ref($domconfig{'login'}) eq 'HASH') {
1.632     raeburn  5129:             if (keys(%{$domconfig{'login'}})) {
                   5130:                 foreach my $key (keys(%{$domconfig{'login'}})) {
1.699     raeburn  5131:                     if (ref($domconfig{'login'}{$key}) eq 'HASH') {
1.1208    raeburn  5132:                         if (($key eq 'loginvia') || ($key eq 'headtag')) {
                   5133:                             if (ref($domconfig{'login'}{$key}) eq 'HASH') {
                   5134:                                 foreach my $hostname (keys(%{$domconfig{'login'}{$key}})) {
                   5135:                                     if (ref($domconfig{'login'}{$key}{$hostname}) eq 'HASH') {
                   5136:                                         if ($key eq 'loginvia') {
                   5137:                                             if ($domconfig{'login'}{'loginvia'}{$hostname}{'server'}) {
                   5138:                                                 my $server = $domconfig{'login'}{'loginvia'}{$hostname}{'server'};
                   5139:                                                 $designhash{$udom.'.login.loginvia'} = $server;
                   5140:                                                 if ($domconfig{'login'}{'loginvia'}{$hostname}{'serverpath'} eq 'custom') {
                   5141: 
                   5142:                                                     $designhash{$udom.'.login.loginvia_'.$hostname} = $server.':'.$domconfig{'login'}{'loginvia'}{$hostname}{'custompath'};
                   5143:                                                 } else {
                   5144:                                                     $designhash{$udom.'.login.loginvia_'.$hostname} = $server.':'.$domconfig{'login'}{'loginvia'}{$hostname}{'serverpath'};
                   5145:                                                 }
1.948     raeburn  5146:                                             }
1.1208    raeburn  5147:                                         } elsif ($key eq 'headtag') {
                   5148:                                             if ($domconfig{'login'}{'headtag'}{$hostname}{'url'}) {
                   5149:                                                 $designhash{$udom.'.login.headtag_'.$hostname} = $domconfig{'login'}{'headtag'}{$hostname}{'url'};
1.948     raeburn  5150:                                             }
1.946     raeburn  5151:                                         }
1.1208    raeburn  5152:                                         if ($domconfig{'login'}{$key}{$hostname}{'exempt'}) {
                   5153:                                             $designhash{$udom.'.login.'.$key.'_exempt_'.$hostname} = $domconfig{'login'}{$key}{$hostname}{'exempt'};
                   5154:                                         }
1.946     raeburn  5155:                                     }
                   5156:                                 }
                   5157:                             }
                   5158:                         } else {
                   5159:                             foreach my $img (keys(%{$domconfig{'login'}{$key}})) {
                   5160:                                 $designhash{$udom.'.login.'.$key.'_'.$img} = 
                   5161:                                     $domconfig{'login'}{$key}{$img};
                   5162:                             }
1.699     raeburn  5163:                         }
                   5164:                     } else {
                   5165:                         $designhash{$udom.'.login.'.$key}=$domconfig{'login'}{$key};
                   5166:                     }
1.632     raeburn  5167:                 }
                   5168:             } else {
                   5169:                 $legacy{'login'} = 1;
1.518     albertel 5170:             }
1.632     raeburn  5171:         } else {
                   5172:             $legacy{'login'} = 1;
1.518     albertel 5173:         }
                   5174:         if (ref($domconfig{'rolecolors'}) eq 'HASH') {
1.632     raeburn  5175:             if (keys(%{$domconfig{'rolecolors'}})) {
                   5176:                 foreach my $role (keys(%{$domconfig{'rolecolors'}})) {
                   5177:                     if (ref($domconfig{'rolecolors'}{$role}) eq 'HASH') {
                   5178:                         foreach my $item (keys(%{$domconfig{'rolecolors'}{$role}})) {
                   5179:                             $designhash{$udom.'.'.$role.'.'.$item}=$domconfig{'rolecolors'}{$role}{$item};
                   5180:                         }
1.518     albertel 5181:                     }
                   5182:                 }
1.632     raeburn  5183:             } else {
                   5184:                 $legacy{'rolecolors'} = 1;
1.518     albertel 5185:             }
1.632     raeburn  5186:         } else {
                   5187:             $legacy{'rolecolors'} = 1;
1.518     albertel 5188:         }
1.948     raeburn  5189:         if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   5190:             if ($domconfig{'autoenroll'}{'co-owners'}) {
                   5191:                 $designhash{$udom.'.autoassign.co-owners'}=$domconfig{'autoenroll'}{'co-owners'};
                   5192:             }
                   5193:         }
1.632     raeburn  5194:         if (keys(%legacy) > 0) {
                   5195:             my %legacyhash = &get_legacy_domconf($udom);
                   5196:             foreach my $item (keys(%legacyhash)) {
                   5197:                 if ($item =~ /^\Q$udom\E\.login/) {
                   5198:                     if ($legacy{'login'}) { 
                   5199:                         $designhash{$item} = $legacyhash{$item};
                   5200:                     }
                   5201:                 } else {
                   5202:                     if ($legacy{'rolecolors'}) {
                   5203:                         $designhash{$item} = $legacyhash{$item};
                   5204:                     }
1.518     albertel 5205:                 }
                   5206:             }
                   5207:         }
1.632     raeburn  5208:     } else {
                   5209:         %designhash = &get_legacy_domconf($udom); 
1.518     albertel 5210:     }
                   5211:     &Apache::lonnet::do_cache_new('domainconfig',$udom,\%designhash,
                   5212: 				  $cachetime);
                   5213:     return %designhash;
                   5214: }
                   5215: 
1.632     raeburn  5216: sub get_legacy_domconf {
                   5217:     my ($udom) = @_;
                   5218:     my %legacyhash;
                   5219:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
                   5220:     my $designfile =  $designdir.'/'.$udom.'.tab';
                   5221:     if (-e $designfile) {
                   5222:         if ( open (my $fh,"<$designfile") ) {
                   5223:             while (my $line = <$fh>) {
                   5224:                 next if ($line =~ /^\#/);
                   5225:                 chomp($line);
                   5226:                 my ($key,$val)=(split(/\=/,$line));
                   5227:                 if ($val) { $legacyhash{$udom.'.'.$key}=$val; }
                   5228:             }
                   5229:             close($fh);
                   5230:         }
                   5231:     }
1.1026    raeburn  5232:     if (-e $Apache::lonnet::perlvar{'lonDocRoot'}.'/adm/lonDomLogos/'.$udom.'.gif') {
1.632     raeburn  5233:         $legacyhash{$udom.'.login.domlogo'} = "/adm/lonDomLogos/$udom.gif";
                   5234:     }
                   5235:     return %legacyhash;
                   5236: }
                   5237: 
1.63      www      5238: =pod
                   5239: 
1.112     bowersj2 5240: =item * &domainlogo()
1.63      www      5241: 
                   5242: Inputs: $domain (usually will be undef)
                   5243: 
                   5244: Returns: A link to a domain logo, if the domain logo exists.
                   5245: If the domain logo does not exist, a description of the domain.
                   5246: 
                   5247: =cut
1.112     bowersj2 5248: 
1.63      www      5249: ###############################################
                   5250: sub domainlogo {
1.517     raeburn  5251:     my $domain = &determinedomain(shift);
1.518     albertel 5252:     my %designhash = &get_domainconf($domain);    
1.517     raeburn  5253:     # See if there is a logo
                   5254:     if ($designhash{$domain.'.login.domlogo'} ne '') {
1.519     raeburn  5255:         my $imgsrc = $designhash{$domain.'.login.domlogo'};
1.538     albertel 5256:         if ($imgsrc =~ m{^/(adm|res)/}) {
                   5257: 	    if ($imgsrc =~ m{^/res/}) {
                   5258: 		my $local_name = &Apache::lonnet::filelocation('',$imgsrc);
                   5259: 		&Apache::lonnet::repcopy($local_name);
                   5260: 	    }
                   5261: 	   $imgsrc = &lonhttpdurl($imgsrc);
1.519     raeburn  5262:         } 
                   5263:         return '<img src="'.$imgsrc.'" alt="'.$domain.'" />';
1.514     albertel 5264:     } elsif (defined(&Apache::lonnet::domain($domain,'description'))) {
                   5265:         return &Apache::lonnet::domain($domain,'description');
1.59      www      5266:     } else {
1.60      matthew  5267:         return '';
1.59      www      5268:     }
                   5269: }
1.63      www      5270: ##############################################
                   5271: 
                   5272: =pod
                   5273: 
1.112     bowersj2 5274: =item * &designparm()
1.63      www      5275: 
                   5276: Inputs: $which parameter; $domain (usually will be undef)
                   5277: 
                   5278: Returns: value of designparamter $which
                   5279: 
                   5280: =cut
1.112     bowersj2 5281: 
1.397     albertel 5282: 
1.400     albertel 5283: ##############################################
1.397     albertel 5284: sub designparm {
                   5285:     my ($which,$domain)=@_;
                   5286:     if (exists($env{'environment.color.'.$which})) {
1.817     bisitz   5287:         return $env{'environment.color.'.$which};
1.96      www      5288:     }
1.63      www      5289:     $domain=&determinedomain($domain);
1.1016    raeburn  5290:     my %domdesign;
                   5291:     unless ($domain eq 'public') {
                   5292:         %domdesign = &get_domainconf($domain);
                   5293:     }
1.520     raeburn  5294:     my $output;
1.517     raeburn  5295:     if ($domdesign{$domain.'.'.$which} ne '') {
1.817     bisitz   5296:         $output = $domdesign{$domain.'.'.$which};
1.63      www      5297:     } else {
1.520     raeburn  5298:         $output = $defaultdesign{$which};
                   5299:     }
                   5300:     if (($which =~ /^(student|coordinator|author|admin)\.img$/) ||
1.635     raeburn  5301:         ($which =~ /login\.(img|logo|domlogo|login)/)) {
1.538     albertel 5302:         if ($output =~ m{^/(adm|res)/}) {
1.817     bisitz   5303:             if ($output =~ m{^/res/}) {
                   5304:                 my $local_name = &Apache::lonnet::filelocation('',$output);
                   5305:                 &Apache::lonnet::repcopy($local_name);
                   5306:             }
1.520     raeburn  5307:             $output = &lonhttpdurl($output);
                   5308:         }
1.63      www      5309:     }
1.520     raeburn  5310:     return $output;
1.63      www      5311: }
1.59      www      5312: 
1.822     bisitz   5313: ##############################################
                   5314: =pod
                   5315: 
1.832     bisitz   5316: =item * &authorspace()
                   5317: 
1.1028    raeburn  5318: Inputs: $url (usually will be undef).
1.832     bisitz   5319: 
1.1132    raeburn  5320: Returns: Path to Authoring Space containing the resource or 
1.1028    raeburn  5321:          directory being viewed (or for which action is being taken). 
                   5322:          If $url is provided, and begins /priv/<domain>/<uname>
                   5323:          the path will be that portion of the $context argument.
                   5324:          Otherwise the path will be for the author space of the current
                   5325:          user when the current role is author, or for that of the 
                   5326:          co-author/assistant co-author space when the current role 
                   5327:          is co-author or assistant co-author.
1.832     bisitz   5328: 
                   5329: =cut
                   5330: 
                   5331: sub authorspace {
1.1028    raeburn  5332:     my ($url) = @_;
                   5333:     if ($url ne '') {
                   5334:         if ($url =~ m{^(/priv/$match_domain/$match_username/)}) {
                   5335:            return $1;
                   5336:         }
                   5337:     }
1.832     bisitz   5338:     my $caname = '';
1.1024    www      5339:     my $cadom = '';
1.1028    raeburn  5340:     if ($env{'request.role'} =~ /^(?:ca|aa)/) {
1.1024    www      5341:         ($cadom,$caname) =
1.832     bisitz   5342:             ($env{'request.role'}=~/($match_domain)\/($match_username)$/);
1.1028    raeburn  5343:     } elsif ($env{'request.role'} =~ m{^au\./($match_domain)/}) {
1.832     bisitz   5344:         $caname = $env{'user.name'};
1.1024    www      5345:         $cadom = $env{'user.domain'};
1.832     bisitz   5346:     }
1.1028    raeburn  5347:     if (($caname ne '') && ($cadom ne '')) {
                   5348:         return "/priv/$cadom/$caname/";
                   5349:     }
                   5350:     return;
1.832     bisitz   5351: }
                   5352: 
                   5353: ##############################################
                   5354: =pod
                   5355: 
1.822     bisitz   5356: =item * &head_subbox()
                   5357: 
                   5358: Inputs: $content (contains HTML code with page functions, etc.)
                   5359: 
                   5360: Returns: HTML div with $content
                   5361:          To be included in page header
                   5362: 
                   5363: =cut
                   5364: 
                   5365: sub head_subbox {
                   5366:     my ($content)=@_;
                   5367:     my $output =
1.993     raeburn  5368:         '<div class="LC_head_subbox">'
1.822     bisitz   5369:        .$content
                   5370:        .'</div>'
                   5371: }
                   5372: 
                   5373: ##############################################
                   5374: =pod
                   5375: 
                   5376: =item * &CSTR_pageheader()
                   5377: 
1.1026    raeburn  5378: Input: (optional) filename from which breadcrumb trail is built.
                   5379:        In most cases no input as needed, as $env{'request.filename'}
                   5380:        is appropriate for use in building the breadcrumb trail.
1.822     bisitz   5381: 
                   5382: Returns: HTML div with CSTR path and recent box
1.1132    raeburn  5383:          To be included on Authoring Space pages
1.822     bisitz   5384: 
                   5385: =cut
                   5386: 
                   5387: sub CSTR_pageheader {
1.1026    raeburn  5388:     my ($trailfile) = @_;
                   5389:     if ($trailfile eq '') {
                   5390:         $trailfile = $env{'request.filename'};
                   5391:     }
                   5392: 
                   5393: # this is for resources; directories have customtitle, and crumbs
                   5394: # and select recent are created in lonpubdir.pm
                   5395: 
                   5396:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
1.1022    www      5397:     my ($udom,$uname,$thisdisfn)=
1.1113    raeburn  5398:         ($trailfile =~ m{^\Q$londocroot\E/priv/([^/]+)/([^/]+)(?:|/(.*))$});
1.1026    raeburn  5399:     my $formaction = "/priv/$udom/$uname/$thisdisfn";
                   5400:     $formaction =~ s{/+}{/}g;
1.822     bisitz   5401: 
                   5402:     my $parentpath = '';
                   5403:     my $lastitem = '';
                   5404:     if ($thisdisfn =~ m-(.+/)([^/]*)$-) {
                   5405:         $parentpath = $1;
                   5406:         $lastitem = $2;
                   5407:     } else {
                   5408:         $lastitem = $thisdisfn;
                   5409:     }
1.921     bisitz   5410: 
                   5411:     my $output =
1.822     bisitz   5412:          '<div>'
                   5413:         .&Apache::loncommon::help_open_menu('','',3,'Authoring') #FIXME: Broken? Where is it?
1.1132    raeburn  5414:         .'<b>'.&mt('Authoring Space:').'</b> '
1.822     bisitz   5415:         .'<form name="dirs" method="post" action="'.$formaction
1.921     bisitz   5416:         .'" target="_top">' #FIXME lonpubdir: target="_parent"
1.1024    www      5417:         .&Apache::lonhtmlcommon::crumbs($uname.'/'.$parentpath,'_top','/priv/'.$udom,undef,undef);
1.921     bisitz   5418: 
                   5419:     if ($lastitem) {
                   5420:         $output .=
                   5421:              '<span class="LC_filename">'
                   5422:             .$lastitem
                   5423:             .'</span>';
                   5424:     }
                   5425:     $output .=
                   5426:          '<br />'
1.822     bisitz   5427:         #FIXME lonpubdir: &Apache::lonhtmlcommon::crumbs($uname.$thisdisfn.'/','_top','/priv','','+1',1)."</b></tt><br />"
                   5428:         .&Apache::lonhtmlcommon::select_recent('construct','recent','this.form.action=this.form.recent.value;this.form.submit()')
                   5429:         .'</form>'
                   5430:         .&Apache::lonmenu::constspaceform()
                   5431:         .'</div>';
1.921     bisitz   5432: 
                   5433:     return $output;
1.822     bisitz   5434: }
                   5435: 
1.60      matthew  5436: ###############################################
                   5437: ###############################################
                   5438: 
                   5439: =pod
                   5440: 
1.112     bowersj2 5441: =back
                   5442: 
1.549     albertel 5443: =head1 HTML Helpers
1.112     bowersj2 5444: 
                   5445: =over 4
                   5446: 
                   5447: =item * &bodytag()
1.60      matthew  5448: 
                   5449: Returns a uniform header for LON-CAPA web pages.
                   5450: 
                   5451: Inputs: 
                   5452: 
1.112     bowersj2 5453: =over 4
                   5454: 
                   5455: =item * $title, A title to be displayed on the page.
                   5456: 
                   5457: =item * $function, the current role (can be undef).
                   5458: 
                   5459: =item * $addentries, extra parameters for the <body> tag.
                   5460: 
                   5461: =item * $bodyonly, if defined, only return the <body> tag.
                   5462: 
                   5463: =item * $domain, if defined, force a given domain.
                   5464: 
                   5465: =item * $forcereg, if page should register as content page (relevant for 
1.86      www      5466:             text interface only)
1.60      matthew  5467: 
1.814     bisitz   5468: =item * $no_nav_bar, if true, keep the 'what is this' info but remove the
                   5469:                      navigational links
1.317     albertel 5470: 
1.338     albertel 5471: =item * $bgcolor, used to override the bgcolor on a webpage to a specific value
                   5472: 
1.460     albertel 5473: =item * $args, optional argument valid values are
                   5474:             no_auto_mt_title -> prevents &mt()ing the title arg
1.562     albertel 5475:             inherit_jsmath -> when creating popup window in a page,
                   5476:                               should it have jsmath forced on by the
                   5477:                               current page
1.460     albertel 5478: 
1.1096    raeburn  5479: =item * $advtoolsref, optional argument, ref to an array containing
                   5480:             inlineremote items to be added in "Functions" menu below
                   5481:             breadcrumbs.
                   5482: 
1.112     bowersj2 5483: =back
                   5484: 
1.60      matthew  5485: Returns: A uniform header for LON-CAPA web pages.  
                   5486: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
                   5487: If $bodyonly is undef or zero, an html string containing a <body> tag and 
                   5488: other decorations will be returned.
                   5489: 
                   5490: =cut
                   5491: 
1.54      www      5492: sub bodytag {
1.831     bisitz   5493:     my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,
1.1096    raeburn  5494:         $no_nav_bar,$bgcolor,$args,$advtoolsref)=@_;
1.339     albertel 5495: 
1.954     raeburn  5496:     my $public;
                   5497:     if ((($env{'user.name'} eq 'public') && ($env{'user.domain'} eq 'public'))
                   5498:         || ($env{'user.name'} eq '') && ($env{'user.domain'} eq '')) {
                   5499:         $public = 1;
                   5500:     }
1.460     albertel 5501:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
1.1154    raeburn  5502:     my $httphost = $args->{'use_absolute'};
1.339     albertel 5503: 
1.183     matthew  5504:     $function = &get_users_function() if (!$function);
1.339     albertel 5505:     my $img =    &designparm($function.'.img',$domain);
                   5506:     my $font =   &designparm($function.'.font',$domain);
                   5507:     my $pgbg   = $bgcolor || &designparm($function.'.pgbg',$domain);
                   5508: 
1.803     bisitz   5509:     my %design = ( 'style'   => 'margin-top: 0',
1.535     albertel 5510: 		   'bgcolor' => $pgbg,
1.339     albertel 5511: 		   'text'    => $font,
                   5512:                    'alink'   => &designparm($function.'.alink',$domain),
                   5513: 		   'vlink'   => &designparm($function.'.vlink',$domain),
                   5514: 		   'link'    => &designparm($function.'.link',$domain),);
1.438     albertel 5515:     @design{keys(%$addentries)} = @$addentries{keys(%$addentries)}; 
1.339     albertel 5516: 
1.63      www      5517:  # role and realm
1.1178    raeburn  5518:     my ($role,$realm) = split(m{\./},$env{'request.role'},2);
                   5519:     if ($realm) {
                   5520:         $realm = '/'.$realm;
                   5521:     }
1.378     raeburn  5522:     if ($role  eq 'ca') {
1.479     albertel 5523:         my ($rdom,$rname) = ($realm =~ m{^/($match_domain)/($match_username)$});
1.500     albertel 5524:         $realm = &plainname($rname,$rdom);
1.378     raeburn  5525:     } 
1.55      www      5526: # realm
1.258     albertel 5527:     if ($env{'request.course.id'}) {
1.378     raeburn  5528:         if ($env{'request.role'} !~ /^cr/) {
                   5529:             $role = &Apache::lonnet::plaintext($role,&course_type());
                   5530:         }
1.898     raeburn  5531:         if ($env{'request.course.sec'}) {
                   5532:             $role .= ('&nbsp;'x2).'-&nbsp;'.&mt('section:').'&nbsp;'.$env{'request.course.sec'};
                   5533:         }   
1.359     albertel 5534: 	$realm = $env{'course.'.$env{'request.course.id'}.'.description'};
1.378     raeburn  5535:     } else {
                   5536:         $role = &Apache::lonnet::plaintext($role);
1.54      www      5537:     }
1.433     albertel 5538: 
1.359     albertel 5539:     if (!$realm) { $realm='&nbsp;'; }
1.330     albertel 5540: 
1.438     albertel 5541:     my $extra_body_attr = &make_attr_string($forcereg,\%design);
1.329     albertel 5542: 
1.101     www      5543: # construct main body tag
1.359     albertel 5544:     my $bodytag = "<body $extra_body_attr>".
1.562     albertel 5545: 	&Apache::lontexconvert::init_math_support($args->{'inherit_jsmath'});
1.252     albertel 5546: 
1.1131    raeburn  5547:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
                   5548: 
1.1130    raeburn  5549:     if (($bodyonly) || ($no_nav_bar) || ($env{'form.inhibitmenu'} eq 'yes')) {
1.60      matthew  5550:         return $bodytag;
1.1130    raeburn  5551:     }
1.359     albertel 5552: 
1.954     raeburn  5553:     if ($public) {
1.433     albertel 5554: 	undef($role);
                   5555:     }
1.359     albertel 5556:     
1.762     bisitz   5557:     my $titleinfo = '<h1>'.$title.'</h1>';
1.359     albertel 5558:     #
                   5559:     # Extra info if you are the DC
                   5560:     my $dc_info = '';
                   5561:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
                   5562:                         $env{'course.'.$env{'request.course.id'}.
                   5563:                                  '.domain'}.'/'})) {
                   5564:         my $cid = $env{'request.course.id'};
1.917     raeburn  5565:         $dc_info = $cid.' '.$env{'course.'.$cid.'.internal.coursecode'};
1.380     www      5566:         $dc_info =~ s/\s+$//;
1.359     albertel 5567:     }
                   5568: 
1.898     raeburn  5569:     $role = '<span class="LC_nobreak">('.$role.')</span>' if $role;
1.853     droeschl 5570: 
1.903     droeschl 5571:         if ($env{'request.state'} eq 'construct') { $forcereg=1; }
                   5572: 
                   5573:         #    if ($env{'request.state'} eq 'construct') {
                   5574:         #        $titleinfo = &CSTR_pageheader(); #FIXME: Will be removed once all scripts have their own calls
                   5575:         #    }
                   5576: 
1.1130    raeburn  5577:         $bodytag .= Apache::lonhtmlcommon::scripttag(
1.1154    raeburn  5578:             Apache::lonmenu::utilityfunctions($httphost), 'start');
1.359     albertel 5579: 
1.1130    raeburn  5580:         my ($left,$right) = Apache::lonmenu::primary_menu();
1.359     albertel 5581: 
1.916     droeschl 5582:         if ($env{'request.noversionuri'} =~ m{^/res/adm/pages/}) {
1.917     raeburn  5583:              if ($dc_info) {
                   5584:                  $dc_info = qq|<span class="LC_cusr_subheading">$dc_info</span>|;
                   5585:              }
1.1130    raeburn  5586:              $bodytag .= qq|<div id="LC_nav_bar">$left $role<br />
1.916     droeschl 5587:                 <em>$realm</em> $dc_info</div>|;
1.903     droeschl 5588:             return $bodytag;
                   5589:         }
1.894     droeschl 5590: 
1.927     raeburn  5591:         unless ($env{'request.symb'} =~ m/\.page___\d+___/) {
1.1130    raeburn  5592:             $bodytag .= qq|<div id="LC_nav_bar">$left $role</div>|;
1.927     raeburn  5593:         }
1.916     droeschl 5594: 
1.1130    raeburn  5595:         $bodytag .= $right;
1.852     droeschl 5596: 
1.917     raeburn  5597:         if ($dc_info) {
                   5598:             $dc_info = &dc_courseid_toggle($dc_info);
                   5599:         }
                   5600:         $bodytag .= qq|<div id="LC_realm">$realm $dc_info</div>|;
1.916     droeschl 5601: 
1.1169    raeburn  5602:         #if directed to not display the secondary menu, don't.  
1.1168    raeburn  5603:         if ($args->{'no_secondary_menu'}) {
                   5604:             return $bodytag;
                   5605:         }
1.1169    raeburn  5606:         #don't show menus for public users
1.954     raeburn  5607:         if (!$public){
1.1154    raeburn  5608:             $bodytag .= Apache::lonmenu::secondary_menu($httphost);
1.903     droeschl 5609:             $bodytag .= Apache::lonmenu::serverform();
1.920     raeburn  5610:             $bodytag .= Apache::lonhtmlcommon::scripttag('', 'end');
                   5611:             if ($env{'request.state'} eq 'construct') {
1.962     droeschl 5612:                 $bodytag .= &Apache::lonmenu::innerregister($forcereg,
1.920     raeburn  5613:                                 $args->{'bread_crumbs'});
1.1096    raeburn  5614:             } elsif ($forcereg) {
                   5615:                 $bodytag .= &Apache::lonmenu::innerregister($forcereg,undef,
                   5616:                                                             $args->{'group'});
                   5617:             } else {
                   5618:                 $bodytag .= 
                   5619:                     &Apache::lonmenu::prepare_functions($env{'request.noversionuri'},
                   5620:                                                         $forcereg,$args->{'group'},
                   5621:                                                         $args->{'bread_crumbs'},
                   5622:                                                         $advtoolsref);
1.920     raeburn  5623:             }
1.903     droeschl 5624:         }else{
                   5625:             # this is to seperate menu from content when there's no secondary
                   5626:             # menu. Especially needed for public accessible ressources.
                   5627:             $bodytag .= '<hr style="clear:both" />';
                   5628:             $bodytag .= Apache::lonhtmlcommon::scripttag('', 'end'); 
1.235     raeburn  5629:         }
1.903     droeschl 5630: 
1.235     raeburn  5631:         return $bodytag;
1.182     matthew  5632: }
                   5633: 
1.917     raeburn  5634: sub dc_courseid_toggle {
                   5635:     my ($dc_info) = @_;
1.980     raeburn  5636:     return ' <span id="dccidtext" class="LC_cusr_subheading LC_nobreak">'.
1.1069    raeburn  5637:            '<a href="javascript:showCourseID();" class="LC_menubuttons_link">'.
1.917     raeburn  5638:            &mt('(More ...)').'</a></span>'.
                   5639:            '<div id="dccid" class="LC_dccid">'.$dc_info.'</div>';
                   5640: }
                   5641: 
1.330     albertel 5642: sub make_attr_string {
                   5643:     my ($register,$attr_ref) = @_;
                   5644: 
                   5645:     if ($attr_ref && !ref($attr_ref)) {
                   5646: 	die("addentries Must be a hash ref ".
                   5647: 	    join(':',caller(1))." ".
                   5648: 	    join(':',caller(0))." ");
                   5649:     }
                   5650: 
                   5651:     if ($register) {
1.339     albertel 5652: 	my ($on_load,$on_unload);
                   5653: 	foreach my $key (keys(%{$attr_ref})) {
                   5654: 	    if      (lc($key) eq 'onload') {
                   5655: 		$on_load.=$attr_ref->{$key}.';';
                   5656: 		delete($attr_ref->{$key});
                   5657: 
                   5658: 	    } elsif (lc($key) eq 'onunload') {
                   5659: 		$on_unload.=$attr_ref->{$key}.';';
                   5660: 		delete($attr_ref->{$key});
                   5661: 	    }
                   5662: 	}
1.953     droeschl 5663: 	$attr_ref->{'onload'}  = $on_load;
                   5664: 	$attr_ref->{'onunload'}= $on_unload;
1.330     albertel 5665:     }
1.339     albertel 5666: 
1.330     albertel 5667:     my $attr_string;
1.1159    raeburn  5668:     foreach my $attr (sort(keys(%$attr_ref))) {
1.330     albertel 5669: 	$attr_string .= " $attr=\"".$attr_ref->{$attr}.'" ';
                   5670:     }
                   5671:     return $attr_string;
                   5672: }
                   5673: 
                   5674: 
1.182     matthew  5675: ###############################################
1.251     albertel 5676: ###############################################
                   5677: 
                   5678: =pod
                   5679: 
                   5680: =item * &endbodytag()
                   5681: 
                   5682: Returns a uniform footer for LON-CAPA web pages.
                   5683: 
1.635     raeburn  5684: Inputs: 1 - optional reference to an args hash
                   5685: If in the hash, key for noredirectlink has a value which evaluates to true,
                   5686: a 'Continue' link is not displayed if the page contains an
                   5687: internal redirect in the <head></head> section,
                   5688: i.e., $env{'internal.head.redirect'} exists   
1.251     albertel 5689: 
                   5690: =cut
                   5691: 
                   5692: sub endbodytag {
1.635     raeburn  5693:     my ($args) = @_;
1.1080    raeburn  5694:     my $endbodytag;
                   5695:     unless ((ref($args) eq 'HASH') && ($args->{'notbody'})) {
                   5696:         $endbodytag='</body>';
                   5697:     }
1.269     albertel 5698:     $endbodytag=&Apache::lontexconvert::jsMath_process()."\n".$endbodytag;
1.315     albertel 5699:     if ( exists( $env{'internal.head.redirect'} ) ) {
1.635     raeburn  5700:         if (!(ref($args) eq 'HASH' && $args->{'noredirectlink'})) {
                   5701: 	    $endbodytag=
                   5702: 	        "<br /><a href=\"$env{'internal.head.redirect'}\">".
                   5703: 	        &mt('Continue').'</a>'.
                   5704: 	        $endbodytag;
                   5705:         }
1.315     albertel 5706:     }
1.251     albertel 5707:     return $endbodytag;
                   5708: }
                   5709: 
1.352     albertel 5710: =pod
                   5711: 
                   5712: =item * &standard_css()
                   5713: 
                   5714: Returns a style sheet
                   5715: 
                   5716: Inputs: (all optional)
                   5717:             domain         -> force to color decorate a page for a specific
                   5718:                                domain
                   5719:             function       -> force usage of a specific rolish color scheme
                   5720:             bgcolor        -> override the default page bgcolor
                   5721: 
                   5722: =cut
                   5723: 
1.343     albertel 5724: sub standard_css {
1.345     albertel 5725:     my ($function,$domain,$bgcolor) = @_;
1.352     albertel 5726:     $function  = &get_users_function() if (!$function);
                   5727:     my $img    = &designparm($function.'.img',   $domain);
                   5728:     my $tabbg  = &designparm($function.'.tabbg', $domain);
                   5729:     my $font   = &designparm($function.'.font',  $domain);
1.801     tempelho 5730:     my $fontmenu = &designparm($function.'.fontmenu', $domain);
1.791     tempelho 5731: #second colour for later usage
1.345     albertel 5732:     my $sidebg = &designparm($function.'.sidebg',$domain);
1.382     albertel 5733:     my $pgbg_or_bgcolor =
                   5734: 	         $bgcolor ||
1.352     albertel 5735: 	         &designparm($function.'.pgbg',  $domain);
1.382     albertel 5736:     my $pgbg   = &designparm($function.'.pgbg',  $domain);
1.352     albertel 5737:     my $alink  = &designparm($function.'.alink', $domain);
                   5738:     my $vlink  = &designparm($function.'.vlink', $domain);
                   5739:     my $link   = &designparm($function.'.link',  $domain);
                   5740: 
1.602     albertel 5741:     my $sans                 = 'Verdana,Arial,Helvetica,sans-serif';
1.395     albertel 5742:     my $mono                 = 'monospace';
1.850     bisitz   5743:     my $data_table_head      = $sidebg;
                   5744:     my $data_table_light     = '#FAFAFA';
1.1060    bisitz   5745:     my $data_table_dark      = '#E0E0E0';
1.470     banghart 5746:     my $data_table_darker    = '#CCCCCC';
1.349     albertel 5747:     my $data_table_highlight = '#FFFF00';
1.352     albertel 5748:     my $mail_new             = '#FFBB77';
                   5749:     my $mail_new_hover       = '#DD9955';
                   5750:     my $mail_read            = '#BBBB77';
                   5751:     my $mail_read_hover      = '#999944';
                   5752:     my $mail_replied         = '#AAAA88';
                   5753:     my $mail_replied_hover   = '#888855';
                   5754:     my $mail_other           = '#99BBBB';
                   5755:     my $mail_other_hover     = '#669999';
1.391     albertel 5756:     my $table_header         = '#DDDDDD';
1.489     raeburn  5757:     my $feedback_link_bg     = '#BBBBBB';
1.911     bisitz   5758:     my $lg_border_color      = '#C8C8C8';
1.952     onken    5759:     my $button_hover         = '#BF2317';
1.392     albertel 5760: 
1.608     albertel 5761:     my $border = ($env{'browser.type'} eq 'explorer' ||
1.911     bisitz   5762:       $env{'browser.type'} eq 'safari'     ) ? '0 2px 0 2px'
                   5763:                                              : '0 3px 0 4px';
1.448     albertel 5764: 
1.523     albertel 5765: 
1.343     albertel 5766:     return <<END;
1.947     droeschl 5767: 
                   5768: /* needed for iframe to allow 100% height in FF */
                   5769: body, html { 
                   5770:     margin: 0;
                   5771:     padding: 0 0.5%;
                   5772:     height: 99%; /* to avoid scrollbars */
                   5773: }
                   5774: 
1.795     www      5775: body {
1.911     bisitz   5776:   font-family: $sans;
                   5777:   line-height:130%;
                   5778:   font-size:0.83em;
                   5779:   color:$font;
1.795     www      5780: }
                   5781: 
1.959     onken    5782: a:focus,
                   5783: a:focus img {
1.795     www      5784:   color: red;
                   5785: }
1.698     harmsja  5786: 
1.911     bisitz   5787: form, .inline {
                   5788:   display: inline;
1.795     www      5789: }
1.721     harmsja  5790: 
1.795     www      5791: .LC_right {
1.911     bisitz   5792:   text-align:right;
1.795     www      5793: }
                   5794: 
                   5795: .LC_middle {
1.911     bisitz   5796:   vertical-align:middle;
1.795     www      5797: }
1.721     harmsja  5798: 
1.1130    raeburn  5799: .LC_floatleft {
                   5800:   float: left;
                   5801: }
                   5802: 
                   5803: .LC_floatright {
                   5804:   float: right;
                   5805: }
                   5806: 
1.911     bisitz   5807: .LC_400Box {
                   5808:   width:400px;
                   5809: }
1.721     harmsja  5810: 
1.947     droeschl 5811: .LC_iframecontainer {
                   5812:     width: 98%;
                   5813:     margin: 0;
                   5814:     position: fixed;
                   5815:     top: 8.5em;
                   5816:     bottom: 0;
                   5817: }
                   5818: 
                   5819: .LC_iframecontainer iframe{
                   5820:     border: none;
                   5821:     width: 100%;
                   5822:     height: 100%;
                   5823: }
                   5824: 
1.778     bisitz   5825: .LC_filename {
                   5826:   font-family: $mono;
                   5827:   white-space:pre;
1.921     bisitz   5828:   font-size: 120%;
1.778     bisitz   5829: }
                   5830: 
                   5831: .LC_fileicon {
                   5832:   border: none;
                   5833:   height: 1.3em;
                   5834:   vertical-align: text-bottom;
                   5835:   margin-right: 0.3em;
                   5836:   text-decoration:none;
                   5837: }
                   5838: 
1.1008    www      5839: .LC_setting {
                   5840:   text-decoration:underline;
                   5841: }
                   5842: 
1.350     albertel 5843: .LC_error {
                   5844:   color: red;
                   5845: }
1.795     www      5846: 
1.1097    bisitz   5847: .LC_warning {
                   5848:   color: darkorange;
                   5849: }
                   5850: 
1.457     albertel 5851: .LC_diff_removed {
1.733     bisitz   5852:   color: red;
1.394     albertel 5853: }
1.532     albertel 5854: 
                   5855: .LC_info,
1.457     albertel 5856: .LC_success,
                   5857: .LC_diff_added {
1.350     albertel 5858:   color: green;
                   5859: }
1.795     www      5860: 
1.802     bisitz   5861: div.LC_confirm_box {
                   5862:   background-color: #FAFAFA;
                   5863:   border: 1px solid $lg_border_color;
                   5864:   margin-right: 0;
                   5865:   padding: 5px;
                   5866: }
                   5867: 
                   5868: div.LC_confirm_box .LC_error img,
                   5869: div.LC_confirm_box .LC_success img {
                   5870:   vertical-align: middle;
                   5871: }
                   5872: 
1.440     albertel 5873: .LC_icon {
1.771     droeschl 5874:   border: none;
1.790     droeschl 5875:   vertical-align: middle;
1.771     droeschl 5876: }
                   5877: 
1.543     albertel 5878: .LC_docs_spacer {
                   5879:   width: 25px;
                   5880:   height: 1px;
1.771     droeschl 5881:   border: none;
1.543     albertel 5882: }
1.346     albertel 5883: 
1.532     albertel 5884: .LC_internal_info {
1.735     bisitz   5885:   color: #999999;
1.532     albertel 5886: }
                   5887: 
1.794     www      5888: .LC_discussion {
1.1050    www      5889:   background: $data_table_dark;
1.911     bisitz   5890:   border: 1px solid black;
                   5891:   margin: 2px;
1.794     www      5892: }
                   5893: 
                   5894: .LC_disc_action_left {
1.1050    www      5895:   background: $sidebg;
1.911     bisitz   5896:   text-align: left;
1.1050    www      5897:   padding: 4px;
                   5898:   margin: 2px;
1.794     www      5899: }
                   5900: 
                   5901: .LC_disc_action_right {
1.1050    www      5902:   background: $sidebg;
1.911     bisitz   5903:   text-align: right;
1.1050    www      5904:   padding: 4px;
                   5905:   margin: 2px;
1.794     www      5906: }
                   5907: 
                   5908: .LC_disc_new_item {
1.911     bisitz   5909:   background: white;
                   5910:   border: 2px solid red;
1.1050    www      5911:   margin: 4px;
                   5912:   padding: 4px;
1.794     www      5913: }
                   5914: 
                   5915: .LC_disc_old_item {
1.911     bisitz   5916:   background: white;
1.1050    www      5917:   margin: 4px;
                   5918:   padding: 4px;
1.794     www      5919: }
                   5920: 
1.458     albertel 5921: table.LC_pastsubmission {
                   5922:   border: 1px solid black;
                   5923:   margin: 2px;
                   5924: }
                   5925: 
1.924     bisitz   5926: table#LC_menubuttons {
1.345     albertel 5927:   width: 100%;
                   5928:   background: $pgbg;
1.392     albertel 5929:   border: 2px;
1.402     albertel 5930:   border-collapse: separate;
1.803     bisitz   5931:   padding: 0;
1.345     albertel 5932: }
1.392     albertel 5933: 
1.801     tempelho 5934: table#LC_title_bar a {
                   5935:   color: $fontmenu;
                   5936: }
1.836     bisitz   5937: 
1.807     droeschl 5938: table#LC_title_bar {
1.819     tempelho 5939:   clear: both;
1.836     bisitz   5940:   display: none;
1.807     droeschl 5941: }
                   5942: 
1.795     www      5943: table#LC_title_bar,
1.933     droeschl 5944: table.LC_breadcrumbs, /* obsolete? */
1.393     albertel 5945: table#LC_title_bar.LC_with_remote {
1.359     albertel 5946:   width: 100%;
1.392     albertel 5947:   border-color: $pgbg;
                   5948:   border-style: solid;
                   5949:   border-width: $border;
1.379     albertel 5950:   background: $pgbg;
1.801     tempelho 5951:   color: $fontmenu;
1.392     albertel 5952:   border-collapse: collapse;
1.803     bisitz   5953:   padding: 0;
1.819     tempelho 5954:   margin: 0;
1.359     albertel 5955: }
1.795     www      5956: 
1.933     droeschl 5957: ul.LC_breadcrumb_tools_outerlist {
1.913     droeschl 5958:     margin: 0;
                   5959:     padding: 0;
1.933     droeschl 5960:     position: relative;
                   5961:     list-style: none;
1.913     droeschl 5962: }
1.933     droeschl 5963: ul.LC_breadcrumb_tools_outerlist li {
1.913     droeschl 5964:     display: inline;
                   5965: }
1.933     droeschl 5966: 
                   5967: .LC_breadcrumb_tools_navigation {
1.913     droeschl 5968:     padding: 0;
1.933     droeschl 5969:     margin: 0;
                   5970:     float: left;
1.913     droeschl 5971: }
1.933     droeschl 5972: .LC_breadcrumb_tools_tools {
                   5973:     padding: 0;
                   5974:     margin: 0;
1.913     droeschl 5975:     float: right;
                   5976: }
                   5977: 
1.359     albertel 5978: table#LC_title_bar td {
                   5979:   background: $tabbg;
                   5980: }
1.795     www      5981: 
1.911     bisitz   5982: table#LC_menubuttons img {
1.803     bisitz   5983:   border: none;
1.346     albertel 5984: }
1.795     www      5985: 
1.842     droeschl 5986: .LC_breadcrumbs_component {
1.911     bisitz   5987:   float: right;
                   5988:   margin: 0 1em;
1.357     albertel 5989: }
1.842     droeschl 5990: .LC_breadcrumbs_component img {
1.911     bisitz   5991:   vertical-align: middle;
1.777     tempelho 5992: }
1.795     www      5993: 
1.383     albertel 5994: td.LC_table_cell_checkbox {
                   5995:   text-align: center;
                   5996: }
1.795     www      5997: 
                   5998: .LC_fontsize_small {
1.911     bisitz   5999:   font-size: 70%;
1.705     tempelho 6000: }
                   6001: 
1.844     bisitz   6002: #LC_breadcrumbs {
1.911     bisitz   6003:   clear:both;
                   6004:   background: $sidebg;
                   6005:   border-bottom: 1px solid $lg_border_color;
                   6006:   line-height: 2.5em;
1.933     droeschl 6007:   overflow: hidden;
1.911     bisitz   6008:   margin: 0;
                   6009:   padding: 0;
1.995     raeburn  6010:   text-align: left;
1.819     tempelho 6011: }
1.862     bisitz   6012: 
1.1098    bisitz   6013: .LC_head_subbox, .LC_actionbox {
1.911     bisitz   6014:   clear:both;
                   6015:   background: #F8F8F8; /* $sidebg; */
1.915     droeschl 6016:   border: 1px solid $sidebg;
1.1098    bisitz   6017:   margin: 0 0 10px 0;
1.966     bisitz   6018:   padding: 3px;
1.995     raeburn  6019:   text-align: left;
1.822     bisitz   6020: }
                   6021: 
1.795     www      6022: .LC_fontsize_medium {
1.911     bisitz   6023:   font-size: 85%;
1.705     tempelho 6024: }
                   6025: 
1.795     www      6026: .LC_fontsize_large {
1.911     bisitz   6027:   font-size: 120%;
1.705     tempelho 6028: }
                   6029: 
1.346     albertel 6030: .LC_menubuttons_inline_text {
                   6031:   color: $font;
1.698     harmsja  6032:   font-size: 90%;
1.701     harmsja  6033:   padding-left:3px;
1.346     albertel 6034: }
                   6035: 
1.934     droeschl 6036: .LC_menubuttons_inline_text img{
                   6037:   vertical-align: middle;
                   6038: }
                   6039: 
1.1051    www      6040: li.LC_menubuttons_inline_text img {
1.951     onken    6041:   cursor:pointer;
1.1002    droeschl 6042:   text-decoration: none;
1.951     onken    6043: }
                   6044: 
1.526     www      6045: .LC_menubuttons_link {
                   6046:   text-decoration: none;
                   6047: }
1.795     www      6048: 
1.522     albertel 6049: .LC_menubuttons_category {
1.521     www      6050:   color: $font;
1.526     www      6051:   background: $pgbg;
1.521     www      6052:   font-size: larger;
                   6053:   font-weight: bold;
                   6054: }
                   6055: 
1.346     albertel 6056: td.LC_menubuttons_text {
1.911     bisitz   6057:   color: $font;
1.346     albertel 6058: }
1.706     harmsja  6059: 
1.346     albertel 6060: .LC_current_location {
                   6061:   background: $tabbg;
                   6062: }
1.795     www      6063: 
1.938     bisitz   6064: table.LC_data_table {
1.347     albertel 6065:   border: 1px solid #000000;
1.402     albertel 6066:   border-collapse: separate;
1.426     albertel 6067:   border-spacing: 1px;
1.610     albertel 6068:   background: $pgbg;
1.347     albertel 6069: }
1.795     www      6070: 
1.422     albertel 6071: .LC_data_table_dense {
                   6072:   font-size: small;
                   6073: }
1.795     www      6074: 
1.507     raeburn  6075: table.LC_nested_outer {
                   6076:   border: 1px solid #000000;
1.589     raeburn  6077:   border-collapse: collapse;
1.803     bisitz   6078:   border-spacing: 0;
1.507     raeburn  6079:   width: 100%;
                   6080: }
1.795     www      6081: 
1.879     raeburn  6082: table.LC_innerpickbox,
1.507     raeburn  6083: table.LC_nested {
1.803     bisitz   6084:   border: none;
1.589     raeburn  6085:   border-collapse: collapse;
1.803     bisitz   6086:   border-spacing: 0;
1.507     raeburn  6087:   width: 100%;
                   6088: }
1.795     www      6089: 
1.911     bisitz   6090: table.LC_data_table tr th,
                   6091: table.LC_calendar tr th,
1.879     raeburn  6092: table.LC_prior_tries tr th,
                   6093: table.LC_innerpickbox tr th {
1.349     albertel 6094:   font-weight: bold;
                   6095:   background-color: $data_table_head;
1.801     tempelho 6096:   color:$fontmenu;
1.701     harmsja  6097:   font-size:90%;
1.347     albertel 6098: }
1.795     www      6099: 
1.879     raeburn  6100: table.LC_innerpickbox tr th,
                   6101: table.LC_innerpickbox tr td {
                   6102:   vertical-align: top;
                   6103: }
                   6104: 
1.711     raeburn  6105: table.LC_data_table tr.LC_info_row > td {
1.735     bisitz   6106:   background-color: #CCCCCC;
1.711     raeburn  6107:   font-weight: bold;
                   6108:   text-align: left;
                   6109: }
1.795     www      6110: 
1.912     bisitz   6111: table.LC_data_table tr.LC_odd_row > td {
                   6112:   background-color: $data_table_light;
                   6113:   padding: 2px;
                   6114:   vertical-align: top;
                   6115: }
                   6116: 
1.809     bisitz   6117: table.LC_pick_box tr > td.LC_odd_row {
1.349     albertel 6118:   background-color: $data_table_light;
1.912     bisitz   6119:   vertical-align: top;
                   6120: }
                   6121: 
                   6122: table.LC_data_table tr.LC_even_row > td {
                   6123:   background-color: $data_table_dark;
1.425     albertel 6124:   padding: 2px;
1.900     bisitz   6125:   vertical-align: top;
1.347     albertel 6126: }
1.795     www      6127: 
1.809     bisitz   6128: table.LC_pick_box tr > td.LC_even_row {
1.349     albertel 6129:   background-color: $data_table_dark;
1.900     bisitz   6130:   vertical-align: top;
1.347     albertel 6131: }
1.795     www      6132: 
1.425     albertel 6133: table.LC_data_table tr.LC_data_table_highlight td {
                   6134:   background-color: $data_table_darker;
                   6135: }
1.795     www      6136: 
1.639     raeburn  6137: table.LC_data_table tr td.LC_leftcol_header {
                   6138:   background-color: $data_table_head;
                   6139:   font-weight: bold;
                   6140: }
1.795     www      6141: 
1.451     albertel 6142: table.LC_data_table tr.LC_empty_row td,
1.507     raeburn  6143: table.LC_nested tr.LC_empty_row td {
1.421     albertel 6144:   font-weight: bold;
                   6145:   font-style: italic;
                   6146:   text-align: center;
                   6147:   padding: 8px;
1.347     albertel 6148: }
1.795     www      6149: 
1.1114    raeburn  6150: table.LC_data_table tr.LC_empty_row td,
                   6151: table.LC_data_table tr.LC_footer_row td {
1.940     bisitz   6152:   background-color: $sidebg;
                   6153: }
                   6154: 
                   6155: table.LC_nested tr.LC_empty_row td {
                   6156:   background-color: #FFFFFF;
                   6157: }
                   6158: 
1.890     droeschl 6159: table.LC_caption {
                   6160: }
                   6161: 
1.507     raeburn  6162: table.LC_nested tr.LC_empty_row td {
1.465     albertel 6163:   padding: 4ex
                   6164: }
1.795     www      6165: 
1.507     raeburn  6166: table.LC_nested_outer tr th {
                   6167:   font-weight: bold;
1.801     tempelho 6168:   color:$fontmenu;
1.507     raeburn  6169:   background-color: $data_table_head;
1.701     harmsja  6170:   font-size: small;
1.507     raeburn  6171:   border-bottom: 1px solid #000000;
                   6172: }
1.795     www      6173: 
1.507     raeburn  6174: table.LC_nested_outer tr td.LC_subheader {
                   6175:   background-color: $data_table_head;
                   6176:   font-weight: bold;
                   6177:   font-size: small;
                   6178:   border-bottom: 1px solid #000000;
                   6179:   text-align: right;
1.451     albertel 6180: }
1.795     www      6181: 
1.507     raeburn  6182: table.LC_nested tr.LC_info_row td {
1.735     bisitz   6183:   background-color: #CCCCCC;
1.451     albertel 6184:   font-weight: bold;
                   6185:   font-size: small;
1.507     raeburn  6186:   text-align: center;
                   6187: }
1.795     www      6188: 
1.589     raeburn  6189: table.LC_nested tr.LC_info_row td.LC_left_item,
                   6190: table.LC_nested_outer tr th.LC_left_item {
1.507     raeburn  6191:   text-align: left;
1.451     albertel 6192: }
1.795     www      6193: 
1.507     raeburn  6194: table.LC_nested td {
1.735     bisitz   6195:   background-color: #FFFFFF;
1.451     albertel 6196:   font-size: small;
1.507     raeburn  6197: }
1.795     www      6198: 
1.507     raeburn  6199: table.LC_nested_outer tr th.LC_right_item,
                   6200: table.LC_nested tr.LC_info_row td.LC_right_item,
                   6201: table.LC_nested tr.LC_odd_row td.LC_right_item,
                   6202: table.LC_nested tr td.LC_right_item {
1.451     albertel 6203:   text-align: right;
                   6204: }
                   6205: 
1.507     raeburn  6206: table.LC_nested tr.LC_odd_row td {
1.735     bisitz   6207:   background-color: #EEEEEE;
1.451     albertel 6208: }
                   6209: 
1.473     raeburn  6210: table.LC_createuser {
                   6211: }
                   6212: 
                   6213: table.LC_createuser tr.LC_section_row td {
1.701     harmsja  6214:   font-size: small;
1.473     raeburn  6215: }
                   6216: 
                   6217: table.LC_createuser tr.LC_info_row td  {
1.735     bisitz   6218:   background-color: #CCCCCC;
1.473     raeburn  6219:   font-weight: bold;
                   6220:   text-align: center;
                   6221: }
                   6222: 
1.349     albertel 6223: table.LC_calendar {
                   6224:   border: 1px solid #000000;
                   6225:   border-collapse: collapse;
1.917     raeburn  6226:   width: 98%;
1.349     albertel 6227: }
1.795     www      6228: 
1.349     albertel 6229: table.LC_calendar_pickdate {
                   6230:   font-size: xx-small;
                   6231: }
1.795     www      6232: 
1.349     albertel 6233: table.LC_calendar tr td {
                   6234:   border: 1px solid #000000;
                   6235:   vertical-align: top;
1.917     raeburn  6236:   width: 14%;
1.349     albertel 6237: }
1.795     www      6238: 
1.349     albertel 6239: table.LC_calendar tr td.LC_calendar_day_empty {
                   6240:   background-color: $data_table_dark;
                   6241: }
1.795     www      6242: 
1.779     bisitz   6243: table.LC_calendar tr td.LC_calendar_day_current {
                   6244:   background-color: $data_table_highlight;
1.777     tempelho 6245: }
1.795     www      6246: 
1.938     bisitz   6247: table.LC_data_table tr td.LC_mail_new {
1.349     albertel 6248:   background-color: $mail_new;
                   6249: }
1.795     www      6250: 
1.938     bisitz   6251: table.LC_data_table tr.LC_mail_new:hover {
1.349     albertel 6252:   background-color: $mail_new_hover;
                   6253: }
1.795     www      6254: 
1.938     bisitz   6255: table.LC_data_table tr td.LC_mail_read {
1.349     albertel 6256:   background-color: $mail_read;
                   6257: }
1.795     www      6258: 
1.938     bisitz   6259: /*
                   6260: table.LC_data_table tr.LC_mail_read:hover {
1.349     albertel 6261:   background-color: $mail_read_hover;
                   6262: }
1.938     bisitz   6263: */
1.795     www      6264: 
1.938     bisitz   6265: table.LC_data_table tr td.LC_mail_replied {
1.349     albertel 6266:   background-color: $mail_replied;
                   6267: }
1.795     www      6268: 
1.938     bisitz   6269: /*
                   6270: table.LC_data_table tr.LC_mail_replied:hover {
1.349     albertel 6271:   background-color: $mail_replied_hover;
                   6272: }
1.938     bisitz   6273: */
1.795     www      6274: 
1.938     bisitz   6275: table.LC_data_table tr td.LC_mail_other {
1.349     albertel 6276:   background-color: $mail_other;
                   6277: }
1.795     www      6278: 
1.938     bisitz   6279: /*
                   6280: table.LC_data_table tr.LC_mail_other:hover {
1.349     albertel 6281:   background-color: $mail_other_hover;
                   6282: }
1.938     bisitz   6283: */
1.494     raeburn  6284: 
1.777     tempelho 6285: table.LC_data_table tr > td.LC_browser_file,
                   6286: table.LC_data_table tr > td.LC_browser_file_published {
1.899     bisitz   6287:   background: #AAEE77;
1.389     albertel 6288: }
1.795     www      6289: 
1.777     tempelho 6290: table.LC_data_table tr > td.LC_browser_file_locked,
                   6291: table.LC_data_table tr > td.LC_browser_file_unpublished {
1.389     albertel 6292:   background: #FFAA99;
1.387     albertel 6293: }
1.795     www      6294: 
1.777     tempelho 6295: table.LC_data_table tr > td.LC_browser_file_obsolete {
1.899     bisitz   6296:   background: #888888;
1.779     bisitz   6297: }
1.795     www      6298: 
1.777     tempelho 6299: table.LC_data_table tr > td.LC_browser_file_modified,
1.779     bisitz   6300: table.LC_data_table tr > td.LC_browser_file_metamodified {
1.899     bisitz   6301:   background: #F8F866;
1.777     tempelho 6302: }
1.795     www      6303: 
1.696     bisitz   6304: table.LC_data_table tr.LC_browser_folder > td {
1.899     bisitz   6305:   background: #E0E8FF;
1.387     albertel 6306: }
1.696     bisitz   6307: 
1.707     bisitz   6308: table.LC_data_table tr > td.LC_roles_is {
1.911     bisitz   6309:   /* background: #77FF77; */
1.707     bisitz   6310: }
1.795     www      6311: 
1.707     bisitz   6312: table.LC_data_table tr > td.LC_roles_future {
1.939     bisitz   6313:   border-right: 8px solid #FFFF77;
1.707     bisitz   6314: }
1.795     www      6315: 
1.707     bisitz   6316: table.LC_data_table tr > td.LC_roles_will {
1.939     bisitz   6317:   border-right: 8px solid #FFAA77;
1.707     bisitz   6318: }
1.795     www      6319: 
1.707     bisitz   6320: table.LC_data_table tr > td.LC_roles_expired {
1.939     bisitz   6321:   border-right: 8px solid #FF7777;
1.707     bisitz   6322: }
1.795     www      6323: 
1.707     bisitz   6324: table.LC_data_table tr > td.LC_roles_will_not {
1.939     bisitz   6325:   border-right: 8px solid #AAFF77;
1.707     bisitz   6326: }
1.795     www      6327: 
1.707     bisitz   6328: table.LC_data_table tr > td.LC_roles_selected {
1.939     bisitz   6329:   border-right: 8px solid #11CC55;
1.707     bisitz   6330: }
                   6331: 
1.388     albertel 6332: span.LC_current_location {
1.701     harmsja  6333:   font-size:larger;
1.388     albertel 6334:   background: $pgbg;
                   6335: }
1.387     albertel 6336: 
1.1029    www      6337: span.LC_current_nav_location {
                   6338:   font-weight:bold;
                   6339:   background: $sidebg;
                   6340: }
                   6341: 
1.395     albertel 6342: span.LC_parm_menu_item {
                   6343:   font-size: larger;
                   6344: }
1.795     www      6345: 
1.395     albertel 6346: span.LC_parm_scope_all {
                   6347:   color: red;
                   6348: }
1.795     www      6349: 
1.395     albertel 6350: span.LC_parm_scope_folder {
                   6351:   color: green;
                   6352: }
1.795     www      6353: 
1.395     albertel 6354: span.LC_parm_scope_resource {
                   6355:   color: orange;
                   6356: }
1.795     www      6357: 
1.395     albertel 6358: span.LC_parm_part {
                   6359:   color: blue;
                   6360: }
1.795     www      6361: 
1.911     bisitz   6362: span.LC_parm_folder,
                   6363: span.LC_parm_symb {
1.395     albertel 6364:   font-size: x-small;
                   6365:   font-family: $mono;
                   6366:   color: #AAAAAA;
                   6367: }
                   6368: 
1.977     bisitz   6369: ul.LC_parm_parmlist li {
                   6370:   display: inline-block;
                   6371:   padding: 0.3em 0.8em;
                   6372:   vertical-align: top;
                   6373:   width: 150px;
                   6374:   border-top:1px solid $lg_border_color;
                   6375: }
                   6376: 
1.795     www      6377: td.LC_parm_overview_level_menu,
                   6378: td.LC_parm_overview_map_menu,
                   6379: td.LC_parm_overview_parm_selectors,
                   6380: td.LC_parm_overview_restrictions  {
1.396     albertel 6381:   border: 1px solid black;
                   6382:   border-collapse: collapse;
                   6383: }
1.795     www      6384: 
1.396     albertel 6385: table.LC_parm_overview_restrictions td {
                   6386:   border-width: 1px 4px 1px 4px;
                   6387:   border-style: solid;
                   6388:   border-color: $pgbg;
                   6389:   text-align: center;
                   6390: }
1.795     www      6391: 
1.396     albertel 6392: table.LC_parm_overview_restrictions th {
                   6393:   background: $tabbg;
                   6394:   border-width: 1px 4px 1px 4px;
                   6395:   border-style: solid;
                   6396:   border-color: $pgbg;
                   6397: }
1.795     www      6398: 
1.398     albertel 6399: table#LC_helpmenu {
1.803     bisitz   6400:   border: none;
1.398     albertel 6401:   height: 55px;
1.803     bisitz   6402:   border-spacing: 0;
1.398     albertel 6403: }
                   6404: 
                   6405: table#LC_helpmenu fieldset legend {
                   6406:   font-size: larger;
                   6407: }
1.795     www      6408: 
1.397     albertel 6409: table#LC_helpmenu_links {
                   6410:   width: 100%;
                   6411:   border: 1px solid black;
                   6412:   background: $pgbg;
1.803     bisitz   6413:   padding: 0;
1.397     albertel 6414:   border-spacing: 1px;
                   6415: }
1.795     www      6416: 
1.397     albertel 6417: table#LC_helpmenu_links tr td {
                   6418:   padding: 1px;
                   6419:   background: $tabbg;
1.399     albertel 6420:   text-align: center;
                   6421:   font-weight: bold;
1.397     albertel 6422: }
1.396     albertel 6423: 
1.795     www      6424: table#LC_helpmenu_links a:link,
                   6425: table#LC_helpmenu_links a:visited,
1.397     albertel 6426: table#LC_helpmenu_links a:active {
                   6427:   text-decoration: none;
                   6428:   color: $font;
                   6429: }
1.795     www      6430: 
1.397     albertel 6431: table#LC_helpmenu_links a:hover {
                   6432:   text-decoration: underline;
                   6433:   color: $vlink;
                   6434: }
1.396     albertel 6435: 
1.417     albertel 6436: .LC_chrt_popup_exists {
                   6437:   border: 1px solid #339933;
                   6438:   margin: -1px;
                   6439: }
1.795     www      6440: 
1.417     albertel 6441: .LC_chrt_popup_up {
                   6442:   border: 1px solid yellow;
                   6443:   margin: -1px;
                   6444: }
1.795     www      6445: 
1.417     albertel 6446: .LC_chrt_popup {
                   6447:   border: 1px solid #8888FF;
                   6448:   background: #CCCCFF;
                   6449: }
1.795     www      6450: 
1.421     albertel 6451: table.LC_pick_box {
                   6452:   border-collapse: separate;
                   6453:   background: white;
                   6454:   border: 1px solid black;
                   6455:   border-spacing: 1px;
                   6456: }
1.795     www      6457: 
1.421     albertel 6458: table.LC_pick_box td.LC_pick_box_title {
1.850     bisitz   6459:   background: $sidebg;
1.421     albertel 6460:   font-weight: bold;
1.900     bisitz   6461:   text-align: left;
1.740     bisitz   6462:   vertical-align: top;
1.421     albertel 6463:   width: 184px;
                   6464:   padding: 8px;
                   6465: }
1.795     www      6466: 
1.579     raeburn  6467: table.LC_pick_box td.LC_pick_box_value {
                   6468:   text-align: left;
                   6469:   padding: 8px;
                   6470: }
1.795     www      6471: 
1.579     raeburn  6472: table.LC_pick_box td.LC_pick_box_select {
                   6473:   text-align: left;
                   6474:   padding: 8px;
                   6475: }
1.795     www      6476: 
1.424     albertel 6477: table.LC_pick_box td.LC_pick_box_separator {
1.803     bisitz   6478:   padding: 0;
1.421     albertel 6479:   height: 1px;
                   6480:   background: black;
                   6481: }
1.795     www      6482: 
1.421     albertel 6483: table.LC_pick_box td.LC_pick_box_submit {
                   6484:   text-align: right;
                   6485: }
1.795     www      6486: 
1.579     raeburn  6487: table.LC_pick_box td.LC_evenrow_value {
                   6488:   text-align: left;
                   6489:   padding: 8px;
                   6490:   background-color: $data_table_light;
                   6491: }
1.795     www      6492: 
1.579     raeburn  6493: table.LC_pick_box td.LC_oddrow_value {
                   6494:   text-align: left;
                   6495:   padding: 8px;
                   6496:   background-color: $data_table_light;
                   6497: }
1.795     www      6498: 
1.579     raeburn  6499: span.LC_helpform_receipt_cat {
                   6500:   font-weight: bold;
                   6501: }
1.795     www      6502: 
1.424     albertel 6503: table.LC_group_priv_box {
                   6504:   background: white;
                   6505:   border: 1px solid black;
                   6506:   border-spacing: 1px;
                   6507: }
1.795     www      6508: 
1.424     albertel 6509: table.LC_group_priv_box td.LC_pick_box_title {
                   6510:   background: $tabbg;
                   6511:   font-weight: bold;
                   6512:   text-align: right;
                   6513:   width: 184px;
                   6514: }
1.795     www      6515: 
1.424     albertel 6516: table.LC_group_priv_box td.LC_groups_fixed {
                   6517:   background: $data_table_light;
                   6518:   text-align: center;
                   6519: }
1.795     www      6520: 
1.424     albertel 6521: table.LC_group_priv_box td.LC_groups_optional {
                   6522:   background: $data_table_dark;
                   6523:   text-align: center;
                   6524: }
1.795     www      6525: 
1.424     albertel 6526: table.LC_group_priv_box td.LC_groups_functionality {
                   6527:   background: $data_table_darker;
                   6528:   text-align: center;
                   6529:   font-weight: bold;
                   6530: }
1.795     www      6531: 
1.424     albertel 6532: table.LC_group_priv td {
                   6533:   text-align: left;
1.803     bisitz   6534:   padding: 0;
1.424     albertel 6535: }
                   6536: 
                   6537: .LC_navbuttons {
                   6538:   margin: 2ex 0ex 2ex 0ex;
                   6539: }
1.795     www      6540: 
1.423     albertel 6541: .LC_topic_bar {
                   6542:   font-weight: bold;
                   6543:   background: $tabbg;
1.918     wenzelju 6544:   margin: 1em 0em 1em 2em;
1.805     bisitz   6545:   padding: 3px;
1.918     wenzelju 6546:   font-size: 1.2em;
1.423     albertel 6547: }
1.795     www      6548: 
1.423     albertel 6549: .LC_topic_bar span {
1.918     wenzelju 6550:   left: 0.5em;
                   6551:   position: absolute;
1.423     albertel 6552:   vertical-align: middle;
1.918     wenzelju 6553:   font-size: 1.2em;
1.423     albertel 6554: }
1.795     www      6555: 
1.423     albertel 6556: table.LC_course_group_status {
                   6557:   margin: 20px;
                   6558: }
1.795     www      6559: 
1.423     albertel 6560: table.LC_status_selector td {
                   6561:   vertical-align: top;
                   6562:   text-align: center;
1.424     albertel 6563:   padding: 4px;
                   6564: }
1.795     www      6565: 
1.599     albertel 6566: div.LC_feedback_link {
1.616     albertel 6567:   clear: both;
1.829     kalberla 6568:   background: $sidebg;
1.779     bisitz   6569:   width: 100%;
1.829     kalberla 6570:   padding-bottom: 10px;
                   6571:   border: 1px $tabbg solid;
1.833     kalberla 6572:   height: 22px;
                   6573:   line-height: 22px;
                   6574:   padding-top: 5px;
                   6575: }
                   6576: 
                   6577: div.LC_feedback_link img {
                   6578:   height: 22px;
1.867     kalberla 6579:   vertical-align:middle;
1.829     kalberla 6580: }
                   6581: 
1.911     bisitz   6582: div.LC_feedback_link a {
1.829     kalberla 6583:   text-decoration: none;
1.489     raeburn  6584: }
1.795     www      6585: 
1.867     kalberla 6586: div.LC_comblock {
1.911     bisitz   6587:   display:inline;
1.867     kalberla 6588:   color:$font;
                   6589:   font-size:90%;
                   6590: }
                   6591: 
                   6592: div.LC_feedback_link div.LC_comblock {
                   6593:   padding-left:5px;
                   6594: }
                   6595: 
                   6596: div.LC_feedback_link div.LC_comblock a {
                   6597:   color:$font;
                   6598: }
                   6599: 
1.489     raeburn  6600: span.LC_feedback_link {
1.858     bisitz   6601:   /* background: $feedback_link_bg; */
1.599     albertel 6602:   font-size: larger;
                   6603: }
1.795     www      6604: 
1.599     albertel 6605: span.LC_message_link {
1.858     bisitz   6606:   /* background: $feedback_link_bg; */
1.599     albertel 6607:   font-size: larger;
                   6608:   position: absolute;
                   6609:   right: 1em;
1.489     raeburn  6610: }
1.421     albertel 6611: 
1.515     albertel 6612: table.LC_prior_tries {
1.524     albertel 6613:   border: 1px solid #000000;
                   6614:   border-collapse: separate;
                   6615:   border-spacing: 1px;
1.515     albertel 6616: }
1.523     albertel 6617: 
1.515     albertel 6618: table.LC_prior_tries td {
1.524     albertel 6619:   padding: 2px;
1.515     albertel 6620: }
1.523     albertel 6621: 
                   6622: .LC_answer_correct {
1.795     www      6623:   background: lightgreen;
                   6624:   color: darkgreen;
                   6625:   padding: 6px;
1.523     albertel 6626: }
1.795     www      6627: 
1.523     albertel 6628: .LC_answer_charged_try {
1.797     www      6629:   background: #FFAAAA;
1.795     www      6630:   color: darkred;
                   6631:   padding: 6px;
1.523     albertel 6632: }
1.795     www      6633: 
1.779     bisitz   6634: .LC_answer_not_charged_try,
1.523     albertel 6635: .LC_answer_no_grade,
                   6636: .LC_answer_late {
1.795     www      6637:   background: lightyellow;
1.523     albertel 6638:   color: black;
1.795     www      6639:   padding: 6px;
1.523     albertel 6640: }
1.795     www      6641: 
1.523     albertel 6642: .LC_answer_previous {
1.795     www      6643:   background: lightblue;
                   6644:   color: darkblue;
                   6645:   padding: 6px;
1.523     albertel 6646: }
1.795     www      6647: 
1.779     bisitz   6648: .LC_answer_no_message {
1.777     tempelho 6649:   background: #FFFFFF;
                   6650:   color: black;
1.795     www      6651:   padding: 6px;
1.779     bisitz   6652: }
1.795     www      6653: 
1.779     bisitz   6654: .LC_answer_unknown {
                   6655:   background: orange;
                   6656:   color: black;
1.795     www      6657:   padding: 6px;
1.777     tempelho 6658: }
1.795     www      6659: 
1.529     albertel 6660: span.LC_prior_numerical,
                   6661: span.LC_prior_string,
                   6662: span.LC_prior_custom,
                   6663: span.LC_prior_reaction,
                   6664: span.LC_prior_math {
1.925     bisitz   6665:   font-family: $mono;
1.523     albertel 6666:   white-space: pre;
                   6667: }
                   6668: 
1.525     albertel 6669: span.LC_prior_string {
1.925     bisitz   6670:   font-family: $mono;
1.525     albertel 6671:   white-space: pre;
                   6672: }
                   6673: 
1.523     albertel 6674: table.LC_prior_option {
                   6675:   width: 100%;
                   6676:   border-collapse: collapse;
                   6677: }
1.795     www      6678: 
1.911     bisitz   6679: table.LC_prior_rank,
1.795     www      6680: table.LC_prior_match {
1.528     albertel 6681:   border-collapse: collapse;
                   6682: }
1.795     www      6683: 
1.528     albertel 6684: table.LC_prior_option tr td,
                   6685: table.LC_prior_rank tr td,
                   6686: table.LC_prior_match tr td {
1.524     albertel 6687:   border: 1px solid #000000;
1.515     albertel 6688: }
                   6689: 
1.855     bisitz   6690: .LC_nobreak {
1.544     albertel 6691:   white-space: nowrap;
1.519     raeburn  6692: }
                   6693: 
1.576     raeburn  6694: span.LC_cusr_emph {
                   6695:   font-style: italic;
                   6696: }
                   6697: 
1.633     raeburn  6698: span.LC_cusr_subheading {
                   6699:   font-weight: normal;
                   6700:   font-size: 85%;
                   6701: }
                   6702: 
1.861     bisitz   6703: div.LC_docs_entry_move {
1.859     bisitz   6704:   border: 1px solid #BBBBBB;
1.545     albertel 6705:   background: #DDDDDD;
1.861     bisitz   6706:   width: 22px;
1.859     bisitz   6707:   padding: 1px;
                   6708:   margin: 0;
1.545     albertel 6709: }
                   6710: 
1.861     bisitz   6711: table.LC_data_table tr > td.LC_docs_entry_commands,
                   6712: table.LC_data_table tr > td.LC_docs_entry_parameter {
1.545     albertel 6713:   font-size: x-small;
                   6714: }
1.795     www      6715: 
1.861     bisitz   6716: .LC_docs_entry_parameter {
                   6717:   white-space: nowrap;
                   6718: }
                   6719: 
1.544     albertel 6720: .LC_docs_copy {
1.545     albertel 6721:   color: #000099;
1.544     albertel 6722: }
1.795     www      6723: 
1.544     albertel 6724: .LC_docs_cut {
1.545     albertel 6725:   color: #550044;
1.544     albertel 6726: }
1.795     www      6727: 
1.544     albertel 6728: .LC_docs_rename {
1.545     albertel 6729:   color: #009900;
1.544     albertel 6730: }
1.795     www      6731: 
1.544     albertel 6732: .LC_docs_remove {
1.545     albertel 6733:   color: #990000;
                   6734: }
                   6735: 
1.547     albertel 6736: .LC_docs_reinit_warn,
                   6737: .LC_docs_ext_edit {
                   6738:   font-size: x-small;
                   6739: }
                   6740: 
1.545     albertel 6741: table.LC_docs_adddocs td,
                   6742: table.LC_docs_adddocs th {
                   6743:   border: 1px solid #BBBBBB;
                   6744:   padding: 4px;
                   6745:   background: #DDDDDD;
1.543     albertel 6746: }
                   6747: 
1.584     albertel 6748: table.LC_sty_begin {
                   6749:   background: #BBFFBB;
                   6750: }
1.795     www      6751: 
1.584     albertel 6752: table.LC_sty_end {
                   6753:   background: #FFBBBB;
                   6754: }
                   6755: 
1.589     raeburn  6756: table.LC_double_column {
1.803     bisitz   6757:   border-width: 0;
1.589     raeburn  6758:   border-collapse: collapse;
                   6759:   width: 100%;
                   6760:   padding: 2px;
                   6761: }
                   6762: 
                   6763: table.LC_double_column tr td.LC_left_col {
1.590     raeburn  6764:   top: 2px;
1.589     raeburn  6765:   left: 2px;
                   6766:   width: 47%;
                   6767:   vertical-align: top;
                   6768: }
                   6769: 
                   6770: table.LC_double_column tr td.LC_right_col {
                   6771:   top: 2px;
1.779     bisitz   6772:   right: 2px;
1.589     raeburn  6773:   width: 47%;
                   6774:   vertical-align: top;
                   6775: }
                   6776: 
1.591     raeburn  6777: div.LC_left_float {
                   6778:   float: left;
                   6779:   padding-right: 5%;
1.597     albertel 6780:   padding-bottom: 4px;
1.591     raeburn  6781: }
                   6782: 
                   6783: div.LC_clear_float_header {
1.597     albertel 6784:   padding-bottom: 2px;
1.591     raeburn  6785: }
                   6786: 
                   6787: div.LC_clear_float_footer {
1.597     albertel 6788:   padding-top: 10px;
1.591     raeburn  6789:   clear: both;
                   6790: }
                   6791: 
1.597     albertel 6792: div.LC_grade_show_user {
1.941     bisitz   6793: /*  border-left: 5px solid $sidebg; */
                   6794:   border-top: 5px solid #000000;
                   6795:   margin: 50px 0 0 0;
1.936     bisitz   6796:   padding: 15px 0 5px 10px;
1.597     albertel 6797: }
1.795     www      6798: 
1.936     bisitz   6799: div.LC_grade_show_user_odd_row {
1.941     bisitz   6800: /*  border-left: 5px solid #000000; */
                   6801: }
                   6802: 
                   6803: div.LC_grade_show_user div.LC_Box {
                   6804:   margin-right: 50px;
1.597     albertel 6805: }
                   6806: 
                   6807: div.LC_grade_submissions,
                   6808: div.LC_grade_message_center,
1.936     bisitz   6809: div.LC_grade_info_links {
1.597     albertel 6810:   margin: 5px;
                   6811:   width: 99%;
                   6812:   background: #FFFFFF;
                   6813: }
1.795     www      6814: 
1.597     albertel 6815: div.LC_grade_submissions_header,
1.936     bisitz   6816: div.LC_grade_message_center_header {
1.705     tempelho 6817:   font-weight: bold;
                   6818:   font-size: large;
1.597     albertel 6819: }
1.795     www      6820: 
1.597     albertel 6821: div.LC_grade_submissions_body,
1.936     bisitz   6822: div.LC_grade_message_center_body {
1.597     albertel 6823:   border: 1px solid black;
                   6824:   width: 99%;
                   6825:   background: #FFFFFF;
                   6826: }
1.795     www      6827: 
1.613     albertel 6828: table.LC_scantron_action {
                   6829:   width: 100%;
                   6830: }
1.795     www      6831: 
1.613     albertel 6832: table.LC_scantron_action tr th {
1.698     harmsja  6833:   font-weight:bold;
                   6834:   font-style:normal;
1.613     albertel 6835: }
1.795     www      6836: 
1.779     bisitz   6837: .LC_edit_problem_header,
1.614     albertel 6838: div.LC_edit_problem_footer {
1.705     tempelho 6839:   font-weight: normal;
                   6840:   font-size:  medium;
1.602     albertel 6841:   margin: 2px;
1.1060    bisitz   6842:   background-color: $sidebg;
1.600     albertel 6843: }
1.795     www      6844: 
1.600     albertel 6845: div.LC_edit_problem_header,
1.602     albertel 6846: div.LC_edit_problem_header div,
1.614     albertel 6847: div.LC_edit_problem_footer,
                   6848: div.LC_edit_problem_footer div,
1.602     albertel 6849: div.LC_edit_problem_editxml_header,
                   6850: div.LC_edit_problem_editxml_header div {
1.1205    golterma 6851:   z-index: 100;
1.600     albertel 6852: }
1.795     www      6853: 
1.600     albertel 6854: div.LC_edit_problem_header_title {
1.705     tempelho 6855:   font-weight: bold;
                   6856:   font-size: larger;
1.602     albertel 6857:   background: $tabbg;
                   6858:   padding: 3px;
1.1060    bisitz   6859:   margin: 0 0 5px 0;
1.602     albertel 6860: }
1.795     www      6861: 
1.602     albertel 6862: table.LC_edit_problem_header_title {
                   6863:   width: 100%;
1.600     albertel 6864:   background: $tabbg;
1.602     albertel 6865: }
                   6866: 
1.1205    golterma 6867: div.LC_edit_actionbar {
                   6868:     background-color: $sidebg;
1.1218    droeschl 6869:     margin: 0;
                   6870:     padding: 0;
                   6871:     line-height: 200%;
1.602     albertel 6872: }
1.795     www      6873: 
1.1218    droeschl 6874: div.LC_edit_actionbar div{
                   6875:     padding: 0;
                   6876:     margin: 0;
                   6877:     display: inline-block;
1.600     albertel 6878: }
1.795     www      6879: 
1.1124    bisitz   6880: .LC_edit_opt {
                   6881:   padding-left: 1em;
                   6882:   white-space: nowrap;
                   6883: }
                   6884: 
1.1152    golterma 6885: .LC_edit_problem_latexhelper{
                   6886:     text-align: right;
                   6887: }
                   6888: 
                   6889: #LC_edit_problem_colorful div{
                   6890:     margin-left: 40px;
                   6891: }
                   6892: 
1.1205    golterma 6893: #LC_edit_problem_codemirror div{
                   6894:     margin-left: 0px;
                   6895: }
                   6896: 
1.911     bisitz   6897: img.stift {
1.803     bisitz   6898:   border-width: 0;
                   6899:   vertical-align: middle;
1.677     riegler  6900: }
1.680     riegler  6901: 
1.923     bisitz   6902: table td.LC_mainmenu_col_fieldset {
1.680     riegler  6903:   vertical-align: top;
1.777     tempelho 6904: }
1.795     www      6905: 
1.716     raeburn  6906: div.LC_createcourse {
1.911     bisitz   6907:   margin: 10px 10px 10px 10px;
1.716     raeburn  6908: }
                   6909: 
1.917     raeburn  6910: .LC_dccid {
1.1130    raeburn  6911:   float: right;
1.917     raeburn  6912:   margin: 0.2em 0 0 0;
                   6913:   padding: 0;
                   6914:   font-size: 90%;
                   6915:   display:none;
                   6916: }
                   6917: 
1.897     wenzelju 6918: ol.LC_primary_menu a:hover,
1.721     harmsja  6919: ol#LC_MenuBreadcrumbs a:hover,
                   6920: ol#LC_PathBreadcrumbs a:hover,
1.897     wenzelju 6921: ul#LC_secondary_menu a:hover,
1.721     harmsja  6922: .LC_FormSectionClearButton input:hover
1.795     www      6923: ul.LC_TabContent   li:hover a {
1.952     onken    6924:   color:$button_hover;
1.911     bisitz   6925:   text-decoration:none;
1.693     droeschl 6926: }
                   6927: 
1.779     bisitz   6928: h1 {
1.911     bisitz   6929:   padding: 0;
                   6930:   line-height:130%;
1.693     droeschl 6931: }
1.698     harmsja  6932: 
1.911     bisitz   6933: h2,
                   6934: h3,
                   6935: h4,
                   6936: h5,
                   6937: h6 {
                   6938:   margin: 5px 0 5px 0;
                   6939:   padding: 0;
                   6940:   line-height:130%;
1.693     droeschl 6941: }
1.795     www      6942: 
                   6943: .LC_hcell {
1.911     bisitz   6944:   padding:3px 15px 3px 15px;
                   6945:   margin: 0;
                   6946:   background-color:$tabbg;
                   6947:   color:$fontmenu;
                   6948:   border-bottom:solid 1px $lg_border_color;
1.693     droeschl 6949: }
1.795     www      6950: 
1.840     bisitz   6951: .LC_Box > .LC_hcell {
1.911     bisitz   6952:   margin: 0 -10px 10px -10px;
1.835     bisitz   6953: }
                   6954: 
1.721     harmsja  6955: .LC_noBorder {
1.911     bisitz   6956:   border: 0;
1.698     harmsja  6957: }
1.693     droeschl 6958: 
1.721     harmsja  6959: .LC_FormSectionClearButton input {
1.911     bisitz   6960:   background-color:transparent;
                   6961:   border: none;
                   6962:   cursor:pointer;
                   6963:   text-decoration:underline;
1.693     droeschl 6964: }
1.763     bisitz   6965: 
                   6966: .LC_help_open_topic {
1.911     bisitz   6967:   color: #FFFFFF;
                   6968:   background-color: #EEEEFF;
                   6969:   margin: 1px;
                   6970:   padding: 4px;
                   6971:   border: 1px solid #000033;
                   6972:   white-space: nowrap;
                   6973:   /* vertical-align: middle; */
1.759     neumanie 6974: }
1.693     droeschl 6975: 
1.911     bisitz   6976: dl,
                   6977: ul,
                   6978: div,
                   6979: fieldset {
                   6980:   margin: 10px 10px 10px 0;
                   6981:   /* overflow: hidden; */
1.693     droeschl 6982: }
1.795     www      6983: 
1.1211    raeburn  6984: article.geogebraweb div {
                   6985:     margin: 0;
                   6986: }
                   6987: 
1.838     bisitz   6988: fieldset > legend {
1.911     bisitz   6989:   font-weight: bold;
                   6990:   padding: 0 5px 0 5px;
1.838     bisitz   6991: }
                   6992: 
1.813     bisitz   6993: #LC_nav_bar {
1.911     bisitz   6994:   float: left;
1.995     raeburn  6995:   background-color: $pgbg_or_bgcolor;
1.966     bisitz   6996:   margin: 0 0 2px 0;
1.807     droeschl 6997: }
                   6998: 
1.916     droeschl 6999: #LC_realm {
                   7000:   margin: 0.2em 0 0 0;
                   7001:   padding: 0;
                   7002:   font-weight: bold;
                   7003:   text-align: center;
1.995     raeburn  7004:   background-color: $pgbg_or_bgcolor;
1.916     droeschl 7005: }
                   7006: 
1.911     bisitz   7007: #LC_nav_bar em {
                   7008:   font-weight: bold;
                   7009:   font-style: normal;
1.807     droeschl 7010: }
                   7011: 
1.897     wenzelju 7012: ol.LC_primary_menu {
1.934     droeschl 7013:   margin: 0;
1.1076    raeburn  7014:   padding: 0;
1.807     droeschl 7015: }
                   7016: 
1.852     droeschl 7017: ol#LC_PathBreadcrumbs {
1.911     bisitz   7018:   margin: 0;
1.693     droeschl 7019: }
                   7020: 
1.897     wenzelju 7021: ol.LC_primary_menu li {
1.1076    raeburn  7022:   color: RGB(80, 80, 80);
                   7023:   vertical-align: middle;
                   7024:   text-align: left;
                   7025:   list-style: none;
1.1205    golterma 7026:   position: relative;
1.1076    raeburn  7027:   float: left;
1.1205    golterma 7028:   z-index: 100; /* will be displayed above codemirror and underneath the help-layer */
                   7029:   line-height: 1.5em;
1.1076    raeburn  7030: }
                   7031: 
1.1205    golterma 7032: ol.LC_primary_menu li a,
                   7033: ol.LC_primary_menu li p {
1.1076    raeburn  7034:   display: block;
                   7035:   margin: 0;
                   7036:   padding: 0 5px 0 10px;
                   7037:   text-decoration: none;
                   7038: }
                   7039: 
1.1205    golterma 7040: ol.LC_primary_menu li p span.LC_primary_menu_innertitle {
                   7041:   display: inline-block;
                   7042:   width: 95%;
                   7043:   text-align: left;
                   7044: }
                   7045: 
                   7046: ol.LC_primary_menu li p span.LC_primary_menu_innerarrow {
                   7047:   display: inline-block;	
                   7048:   width: 5%;
                   7049:   float: right;
                   7050:   text-align: right;
                   7051:   font-size: 70%;
                   7052: }
                   7053: 
                   7054: ol.LC_primary_menu ul {
1.1076    raeburn  7055:   display: none;
1.1205    golterma 7056:   width: 15em;
1.1076    raeburn  7057:   background-color: $data_table_light;
1.1205    golterma 7058:   position: absolute;
                   7059:   top: 100%;
1.1076    raeburn  7060: }
                   7061: 
1.1205    golterma 7062: ol.LC_primary_menu ul ul {
                   7063:   left: 100%;
                   7064:   top: 0;
                   7065: }
                   7066: 
                   7067: ol.LC_primary_menu li:hover > ul, ol.LC_primary_menu li.hover > ul {
1.1076    raeburn  7068:   display: block;
                   7069:   position: absolute;
                   7070:   margin: 0;
                   7071:   padding: 0;
1.1078    raeburn  7072:   z-index: 2;
1.1076    raeburn  7073: }
                   7074: 
                   7075: ol.LC_primary_menu li:hover li, ol.LC_primary_menu li.hover li {
1.1205    golterma 7076: /* First Submenu -> size should be smaller than the menu title of the whole menu */
1.1076    raeburn  7077:   font-size: 90%;
1.911     bisitz   7078:   vertical-align: top;
1.1076    raeburn  7079:   float: none;
1.1079    raeburn  7080:   border-left: 1px solid black;
                   7081:   border-right: 1px solid black;
1.1205    golterma 7082: /* A dark bottom border to visualize different menu options; 
                   7083: overwritten in the create_submenu routine for the last border-bottom of the menu */
                   7084:   border-bottom: 1px solid $data_table_dark; 
1.1076    raeburn  7085: }
                   7086: 
1.1205    golterma 7087: ol.LC_primary_menu li li p:hover {
                   7088:   color:$button_hover;
                   7089:   text-decoration:none;
                   7090:   background-color:$data_table_dark;
1.1076    raeburn  7091: }
                   7092: 
                   7093: ol.LC_primary_menu li li a:hover {
                   7094:    color:$button_hover;
                   7095:    background-color:$data_table_dark;
1.693     droeschl 7096: }
                   7097: 
1.1205    golterma 7098: /* Font-size equal to the size of the predecessors*/
                   7099: ol.LC_primary_menu li:hover li li {
                   7100:   font-size: 100%;
                   7101: }
                   7102: 
1.897     wenzelju 7103: ol.LC_primary_menu li img {
1.911     bisitz   7104:   vertical-align: bottom;
1.934     droeschl 7105:   height: 1.1em;
1.1077    raeburn  7106:   margin: 0.2em 0 0 0;
1.693     droeschl 7107: }
                   7108: 
1.897     wenzelju 7109: ol.LC_primary_menu a {
1.911     bisitz   7110:   color: RGB(80, 80, 80);
                   7111:   text-decoration: none;
1.693     droeschl 7112: }
1.795     www      7113: 
1.949     droeschl 7114: ol.LC_primary_menu a.LC_new_message {
                   7115:   font-weight:bold;
                   7116:   color: darkred;
                   7117: }
                   7118: 
1.975     raeburn  7119: ol.LC_docs_parameters {
                   7120:   margin-left: 0;
                   7121:   padding: 0;
                   7122:   list-style: none;
                   7123: }
                   7124: 
                   7125: ol.LC_docs_parameters li {
                   7126:   margin: 0;
                   7127:   padding-right: 20px;
                   7128:   display: inline;
                   7129: }
                   7130: 
1.976     raeburn  7131: ol.LC_docs_parameters li:before {
                   7132:   content: "\\002022 \\0020";
                   7133: }
                   7134: 
                   7135: li.LC_docs_parameters_title {
                   7136:   font-weight: bold;
                   7137: }
                   7138: 
                   7139: ol.LC_docs_parameters li.LC_docs_parameters_title:before {
                   7140:   content: "";
                   7141: }
                   7142: 
1.897     wenzelju 7143: ul#LC_secondary_menu {
1.1107    raeburn  7144:   clear: right;
1.911     bisitz   7145:   color: $fontmenu;
                   7146:   background: $tabbg;
                   7147:   list-style: none;
                   7148:   padding: 0;
                   7149:   margin: 0;
                   7150:   width: 100%;
1.995     raeburn  7151:   text-align: left;
1.1107    raeburn  7152:   float: left;
1.808     droeschl 7153: }
                   7154: 
1.897     wenzelju 7155: ul#LC_secondary_menu li {
1.911     bisitz   7156:   font-weight: bold;
                   7157:   line-height: 1.8em;
1.1107    raeburn  7158:   border-right: 1px solid black;
                   7159:   float: left;
                   7160: }
                   7161: 
                   7162: ul#LC_secondary_menu li.LC_hoverable:hover, ul#LC_secondary_menu li.hover {
                   7163:   background-color: $data_table_light;
                   7164: }
                   7165: 
                   7166: ul#LC_secondary_menu li a {
1.911     bisitz   7167:   padding: 0 0.8em;
1.1107    raeburn  7168: }
                   7169: 
                   7170: ul#LC_secondary_menu li ul {
                   7171:   display: none;
                   7172: }
                   7173: 
                   7174: ul#LC_secondary_menu li:hover ul, ul#LC_secondary_menu li.hover ul {
                   7175:   display: block;
                   7176:   position: absolute;
                   7177:   margin: 0;
                   7178:   padding: 0;
                   7179:   list-style:none;
                   7180:   float: none;
                   7181:   background-color: $data_table_light;
                   7182:   z-index: 2;
                   7183:   margin-left: -1px;
                   7184: }
                   7185: 
                   7186: ul#LC_secondary_menu li ul li {
                   7187:   font-size: 90%;
                   7188:   vertical-align: top;
                   7189:   border-left: 1px solid black;
1.911     bisitz   7190:   border-right: 1px solid black;
1.1119    raeburn  7191:   background-color: $data_table_light;
1.1107    raeburn  7192:   list-style:none;
                   7193:   float: none;
                   7194: }
                   7195: 
                   7196: ul#LC_secondary_menu li ul li:hover, ul#LC_secondary_menu li ul li.hover {
                   7197:   background-color: $data_table_dark;
1.807     droeschl 7198: }
                   7199: 
1.847     tempelho 7200: ul.LC_TabContent {
1.911     bisitz   7201:   display:block;
                   7202:   background: $sidebg;
                   7203:   border-bottom: solid 1px $lg_border_color;
                   7204:   list-style:none;
1.1020    raeburn  7205:   margin: -1px -10px 0 -10px;
1.911     bisitz   7206:   padding: 0;
1.693     droeschl 7207: }
                   7208: 
1.795     www      7209: ul.LC_TabContent li,
                   7210: ul.LC_TabContentBigger li {
1.911     bisitz   7211:   float:left;
1.741     harmsja  7212: }
1.795     www      7213: 
1.897     wenzelju 7214: ul#LC_secondary_menu li a {
1.911     bisitz   7215:   color: $fontmenu;
                   7216:   text-decoration: none;
1.693     droeschl 7217: }
1.795     www      7218: 
1.721     harmsja  7219: ul.LC_TabContent {
1.952     onken    7220:   min-height:20px;
1.721     harmsja  7221: }
1.795     www      7222: 
                   7223: ul.LC_TabContent li {
1.911     bisitz   7224:   vertical-align:middle;
1.959     onken    7225:   padding: 0 16px 0 10px;
1.911     bisitz   7226:   background-color:$tabbg;
                   7227:   border-bottom:solid 1px $lg_border_color;
1.1020    raeburn  7228:   border-left: solid 1px $font;
1.721     harmsja  7229: }
1.795     www      7230: 
1.847     tempelho 7231: ul.LC_TabContent .right {
1.911     bisitz   7232:   float:right;
1.847     tempelho 7233: }
                   7234: 
1.911     bisitz   7235: ul.LC_TabContent li a,
                   7236: ul.LC_TabContent li {
                   7237:   color:rgb(47,47,47);
                   7238:   text-decoration:none;
                   7239:   font-size:95%;
                   7240:   font-weight:bold;
1.952     onken    7241:   min-height:20px;
                   7242: }
                   7243: 
1.959     onken    7244: ul.LC_TabContent li a:hover,
                   7245: ul.LC_TabContent li a:focus {
1.952     onken    7246:   color: $button_hover;
1.959     onken    7247:   background:none;
                   7248:   outline:none;
1.952     onken    7249: }
                   7250: 
                   7251: ul.LC_TabContent li:hover {
                   7252:   color: $button_hover;
                   7253:   cursor:pointer;
1.721     harmsja  7254: }
1.795     www      7255: 
1.911     bisitz   7256: ul.LC_TabContent li.active {
1.952     onken    7257:   color: $font;
1.911     bisitz   7258:   background:#FFFFFF url(/adm/lonIcons/open.gif) no-repeat scroll right center;
1.952     onken    7259:   border-bottom:solid 1px #FFFFFF;
                   7260:   cursor: default;
1.744     ehlerst  7261: }
1.795     www      7262: 
1.959     onken    7263: ul.LC_TabContent li.active a {
                   7264:   color:$font;
                   7265:   background:#FFFFFF;
                   7266:   outline: none;
                   7267: }
1.1047    raeburn  7268: 
                   7269: ul.LC_TabContent li.goback {
                   7270:   float: left;
                   7271:   border-left: none;
                   7272: }
                   7273: 
1.870     tempelho 7274: #maincoursedoc {
1.911     bisitz   7275:   clear:both;
1.870     tempelho 7276: }
                   7277: 
                   7278: ul.LC_TabContentBigger {
1.911     bisitz   7279:   display:block;
                   7280:   list-style:none;
                   7281:   padding: 0;
1.870     tempelho 7282: }
                   7283: 
1.795     www      7284: ul.LC_TabContentBigger li {
1.911     bisitz   7285:   vertical-align:bottom;
                   7286:   height: 30px;
                   7287:   font-size:110%;
                   7288:   font-weight:bold;
                   7289:   color: #737373;
1.841     tempelho 7290: }
                   7291: 
1.957     onken    7292: ul.LC_TabContentBigger li.active {
                   7293:   position: relative;
                   7294:   top: 1px;
                   7295: }
                   7296: 
1.870     tempelho 7297: ul.LC_TabContentBigger li a {
1.911     bisitz   7298:   background:url('/adm/lonIcons/tabbgleft.gif') left bottom no-repeat;
                   7299:   height: 30px;
                   7300:   line-height: 30px;
                   7301:   text-align: center;
                   7302:   display: block;
                   7303:   text-decoration: none;
1.958     onken    7304:   outline: none;  
1.741     harmsja  7305: }
1.795     www      7306: 
1.870     tempelho 7307: ul.LC_TabContentBigger li.active a {
1.911     bisitz   7308:   background:url('/adm/lonIcons/tabbgleft.gif') left top no-repeat;
                   7309:   color:$font;
1.744     ehlerst  7310: }
1.795     www      7311: 
1.870     tempelho 7312: ul.LC_TabContentBigger li b {
1.911     bisitz   7313:   background: url('/adm/lonIcons/tabbgright.gif') no-repeat right bottom;
                   7314:   display: block;
                   7315:   float: left;
                   7316:   padding: 0 30px;
1.957     onken    7317:   border-bottom: 1px solid $lg_border_color;
1.870     tempelho 7318: }
                   7319: 
1.956     onken    7320: ul.LC_TabContentBigger li:hover b {
                   7321:   color:$button_hover;
                   7322: }
                   7323: 
1.870     tempelho 7324: ul.LC_TabContentBigger li.active b {
1.911     bisitz   7325:   background:url('/adm/lonIcons/tabbgright.gif') right top no-repeat;
                   7326:   color:$font;
1.957     onken    7327:   border: 0;
1.741     harmsja  7328: }
1.693     droeschl 7329: 
1.870     tempelho 7330: 
1.862     bisitz   7331: ul.LC_CourseBreadcrumbs {
                   7332:   background: $sidebg;
1.1020    raeburn  7333:   height: 2em;
1.862     bisitz   7334:   padding-left: 10px;
1.1020    raeburn  7335:   margin: 0;
1.862     bisitz   7336:   list-style-position: inside;
                   7337: }
                   7338: 
1.911     bisitz   7339: ol#LC_MenuBreadcrumbs,
1.862     bisitz   7340: ol#LC_PathBreadcrumbs {
1.911     bisitz   7341:   padding-left: 10px;
                   7342:   margin: 0;
1.933     droeschl 7343:   height: 2.5em;  /* equal to #LC_breadcrumbs line-height */
1.693     droeschl 7344: }
                   7345: 
1.911     bisitz   7346: ol#LC_MenuBreadcrumbs li,
                   7347: ol#LC_PathBreadcrumbs li,
1.862     bisitz   7348: ul.LC_CourseBreadcrumbs li {
1.911     bisitz   7349:   display: inline;
1.933     droeschl 7350:   white-space: normal;  
1.693     droeschl 7351: }
                   7352: 
1.823     bisitz   7353: ol#LC_MenuBreadcrumbs li a,
1.862     bisitz   7354: ul.LC_CourseBreadcrumbs li a {
1.911     bisitz   7355:   text-decoration: none;
                   7356:   font-size:90%;
1.693     droeschl 7357: }
1.795     www      7358: 
1.969     droeschl 7359: ol#LC_MenuBreadcrumbs h1 {
                   7360:   display: inline;
                   7361:   font-size: 90%;
                   7362:   line-height: 2.5em;
                   7363:   margin: 0;
                   7364:   padding: 0;
                   7365: }
                   7366: 
1.795     www      7367: ol#LC_PathBreadcrumbs li a {
1.911     bisitz   7368:   text-decoration:none;
                   7369:   font-size:100%;
                   7370:   font-weight:bold;
1.693     droeschl 7371: }
1.795     www      7372: 
1.840     bisitz   7373: .LC_Box {
1.911     bisitz   7374:   border: solid 1px $lg_border_color;
                   7375:   padding: 0 10px 10px 10px;
1.746     neumanie 7376: }
1.795     www      7377: 
1.1020    raeburn  7378: .LC_DocsBox {
                   7379:   border: solid 1px $lg_border_color;
                   7380:   padding: 0 0 10px 10px;
                   7381: }
                   7382: 
1.795     www      7383: .LC_AboutMe_Image {
1.911     bisitz   7384:   float:left;
                   7385:   margin-right:10px;
1.747     neumanie 7386: }
1.795     www      7387: 
                   7388: .LC_Clear_AboutMe_Image {
1.911     bisitz   7389:   clear:left;
1.747     neumanie 7390: }
1.795     www      7391: 
1.721     harmsja  7392: dl.LC_ListStyleClean dt {
1.911     bisitz   7393:   padding-right: 5px;
                   7394:   display: table-header-group;
1.693     droeschl 7395: }
                   7396: 
1.721     harmsja  7397: dl.LC_ListStyleClean dd {
1.911     bisitz   7398:   display: table-row;
1.693     droeschl 7399: }
                   7400: 
1.721     harmsja  7401: .LC_ListStyleClean,
                   7402: .LC_ListStyleSimple,
                   7403: .LC_ListStyleNormal,
1.795     www      7404: .LC_ListStyleSpecial {
1.911     bisitz   7405:   /* display:block; */
                   7406:   list-style-position: inside;
                   7407:   list-style-type: none;
                   7408:   overflow: hidden;
                   7409:   padding: 0;
1.693     droeschl 7410: }
                   7411: 
1.721     harmsja  7412: .LC_ListStyleSimple li,
                   7413: .LC_ListStyleSimple dd,
                   7414: .LC_ListStyleNormal li,
                   7415: .LC_ListStyleNormal dd,
                   7416: .LC_ListStyleSpecial li,
1.795     www      7417: .LC_ListStyleSpecial dd {
1.911     bisitz   7418:   margin: 0;
                   7419:   padding: 5px 5px 5px 10px;
                   7420:   clear: both;
1.693     droeschl 7421: }
                   7422: 
1.721     harmsja  7423: .LC_ListStyleClean li,
                   7424: .LC_ListStyleClean dd {
1.911     bisitz   7425:   padding-top: 0;
                   7426:   padding-bottom: 0;
1.693     droeschl 7427: }
                   7428: 
1.721     harmsja  7429: .LC_ListStyleSimple dd,
1.795     www      7430: .LC_ListStyleSimple li {
1.911     bisitz   7431:   border-bottom: solid 1px $lg_border_color;
1.693     droeschl 7432: }
                   7433: 
1.721     harmsja  7434: .LC_ListStyleSpecial li,
                   7435: .LC_ListStyleSpecial dd {
1.911     bisitz   7436:   list-style-type: none;
                   7437:   background-color: RGB(220, 220, 220);
                   7438:   margin-bottom: 4px;
1.693     droeschl 7439: }
                   7440: 
1.721     harmsja  7441: table.LC_SimpleTable {
1.911     bisitz   7442:   margin:5px;
                   7443:   border:solid 1px $lg_border_color;
1.795     www      7444: }
1.693     droeschl 7445: 
1.721     harmsja  7446: table.LC_SimpleTable tr {
1.911     bisitz   7447:   padding: 0;
                   7448:   border:solid 1px $lg_border_color;
1.693     droeschl 7449: }
1.795     www      7450: 
                   7451: table.LC_SimpleTable thead {
1.911     bisitz   7452:   background:rgb(220,220,220);
1.693     droeschl 7453: }
                   7454: 
1.721     harmsja  7455: div.LC_columnSection {
1.911     bisitz   7456:   display: block;
                   7457:   clear: both;
                   7458:   overflow: hidden;
                   7459:   margin: 0;
1.693     droeschl 7460: }
                   7461: 
1.721     harmsja  7462: div.LC_columnSection>* {
1.911     bisitz   7463:   float: left;
                   7464:   margin: 10px 20px 10px 0;
                   7465:   overflow:hidden;
1.693     droeschl 7466: }
1.721     harmsja  7467: 
1.795     www      7468: table em {
1.911     bisitz   7469:   font-weight: bold;
                   7470:   font-style: normal;
1.748     schulted 7471: }
1.795     www      7472: 
1.779     bisitz   7473: table.LC_tableBrowseRes,
1.795     www      7474: table.LC_tableOfContent {
1.911     bisitz   7475:   border:none;
                   7476:   border-spacing: 1px;
                   7477:   padding: 3px;
                   7478:   background-color: #FFFFFF;
                   7479:   font-size: 90%;
1.753     droeschl 7480: }
1.789     droeschl 7481: 
1.911     bisitz   7482: table.LC_tableOfContent {
                   7483:   border-collapse: collapse;
1.789     droeschl 7484: }
                   7485: 
1.771     droeschl 7486: table.LC_tableBrowseRes a,
1.768     schulted 7487: table.LC_tableOfContent a {
1.911     bisitz   7488:   background-color: transparent;
                   7489:   text-decoration: none;
1.753     droeschl 7490: }
                   7491: 
1.795     www      7492: table.LC_tableOfContent img {
1.911     bisitz   7493:   border: none;
                   7494:   height: 1.3em;
                   7495:   vertical-align: text-bottom;
                   7496:   margin-right: 0.3em;
1.753     droeschl 7497: }
1.757     schulted 7498: 
1.795     www      7499: a#LC_content_toolbar_firsthomework {
1.911     bisitz   7500:   background-image:url(/res/adm/pages/open-first-problem.gif);
1.774     ehlerst  7501: }
                   7502: 
1.795     www      7503: a#LC_content_toolbar_everything {
1.911     bisitz   7504:   background-image:url(/res/adm/pages/show-all.gif);
1.774     ehlerst  7505: }
                   7506: 
1.795     www      7507: a#LC_content_toolbar_uncompleted {
1.911     bisitz   7508:   background-image:url(/res/adm/pages/show-incomplete-problems.gif);
1.774     ehlerst  7509: }
                   7510: 
1.795     www      7511: #LC_content_toolbar_clearbubbles {
1.911     bisitz   7512:   background-image:url(/res/adm/pages/mark-discussionentries-read.gif);
1.774     ehlerst  7513: }
                   7514: 
1.795     www      7515: a#LC_content_toolbar_changefolder {
1.911     bisitz   7516:   background : url(/res/adm/pages/close-all-folders.gif) top center ;
1.757     schulted 7517: }
                   7518: 
1.795     www      7519: a#LC_content_toolbar_changefolder_toggled {
1.911     bisitz   7520:   background-image:url(/res/adm/pages/open-all-folders.gif);
1.757     schulted 7521: }
                   7522: 
1.1043    raeburn  7523: a#LC_content_toolbar_edittoplevel {
                   7524:   background-image:url(/res/adm/pages/edittoplevel.gif);
                   7525: }
                   7526: 
1.795     www      7527: ul#LC_toolbar li a:hover {
1.911     bisitz   7528:   background-position: bottom center;
1.757     schulted 7529: }
                   7530: 
1.795     www      7531: ul#LC_toolbar {
1.911     bisitz   7532:   padding: 0;
                   7533:   margin: 2px;
                   7534:   list-style:none;
                   7535:   position:relative;
                   7536:   background-color:white;
1.1082    raeburn  7537:   overflow: auto;
1.757     schulted 7538: }
                   7539: 
1.795     www      7540: ul#LC_toolbar li {
1.911     bisitz   7541:   border:1px solid white;
                   7542:   padding: 0;
                   7543:   margin: 0;
                   7544:   float: left;
                   7545:   display:inline;
                   7546:   vertical-align:middle;
1.1082    raeburn  7547:   white-space: nowrap;
1.911     bisitz   7548: }
1.757     schulted 7549: 
1.783     amueller 7550: 
1.795     www      7551: a.LC_toolbarItem {
1.911     bisitz   7552:   display:block;
                   7553:   padding: 0;
                   7554:   margin: 0;
                   7555:   height: 32px;
                   7556:   width: 32px;
                   7557:   color:white;
                   7558:   border: none;
                   7559:   background-repeat:no-repeat;
                   7560:   background-color:transparent;
1.757     schulted 7561: }
                   7562: 
1.915     droeschl 7563: ul.LC_funclist {
                   7564:     margin: 0;
                   7565:     padding: 0.5em 1em 0.5em 0;
                   7566: }
                   7567: 
1.933     droeschl 7568: ul.LC_funclist > li:first-child {
                   7569:     font-weight:bold; 
                   7570:     margin-left:0.8em;
                   7571: }
                   7572: 
1.915     droeschl 7573: ul.LC_funclist + ul.LC_funclist {
                   7574:     /* 
                   7575:        left border as a seperator if we have more than
                   7576:        one list 
                   7577:     */
                   7578:     border-left: 1px solid $sidebg;
                   7579:     /* 
                   7580:        this hides the left border behind the border of the 
                   7581:        outer box if element is wrapped to the next 'line' 
                   7582:     */
                   7583:     margin-left: -1px;
                   7584: }
                   7585: 
1.843     bisitz   7586: ul.LC_funclist li {
1.915     droeschl 7587:   display: inline;
1.782     bisitz   7588:   white-space: nowrap;
1.915     droeschl 7589:   margin: 0 0 0 25px;
                   7590:   line-height: 150%;
1.782     bisitz   7591: }
                   7592: 
1.974     wenzelju 7593: .LC_hidden {
                   7594:   display: none;
                   7595: }
                   7596: 
1.1030    www      7597: .LCmodal-overlay {
                   7598: 		position:fixed;
                   7599: 		top:0;
                   7600: 		right:0;
                   7601: 		bottom:0;
                   7602: 		left:0;
                   7603: 		height:100%;
                   7604: 		width:100%;
                   7605: 		margin:0;
                   7606: 		padding:0;
                   7607: 		background:#999;
                   7608: 		opacity:.75;
                   7609: 		filter: alpha(opacity=75);
                   7610: 		-moz-opacity: 0.75;
                   7611: 		z-index:101;
                   7612: }
                   7613: 
                   7614: * html .LCmodal-overlay {   
                   7615: 		position: absolute;
                   7616: 		height: expression(document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + 'px');
                   7617: }
                   7618: 
                   7619: .LCmodal-window {
                   7620: 		position:fixed;
                   7621: 		top:50%;
                   7622: 		left:50%;
                   7623: 		margin:0;
                   7624: 		padding:0;
                   7625: 		z-index:102;
                   7626: 	}
                   7627: 
                   7628: * html .LCmodal-window {
                   7629: 		position:absolute;
                   7630: }
                   7631: 
                   7632: .LCclose-window {
                   7633: 		position:absolute;
                   7634: 		width:32px;
                   7635: 		height:32px;
                   7636: 		right:8px;
                   7637: 		top:8px;
                   7638: 		background:transparent url('/res/adm/pages/process-stop.png') no-repeat scroll right top;
                   7639: 		text-indent:-99999px;
                   7640: 		overflow:hidden;
                   7641: 		cursor:pointer;
                   7642: }
                   7643: 
1.1100    raeburn  7644: /*
                   7645:   styles used by TTH when "Default set of options to pass to tth/m
                   7646:   when converting TeX" in course settings has been set
                   7647: 
                   7648:   option passed: -t
                   7649: 
                   7650: */
                   7651: 
                   7652: td div.comp { margin-top: -0.6ex; margin-bottom: -1ex;}
                   7653: td div.comb { margin-top: -0.6ex; margin-bottom: -.6ex;}
                   7654: td div.hrcomp { line-height: 0.9; margin-top: -0.8ex; margin-bottom: -1ex;}
                   7655: td div.norm {line-height:normal;}
                   7656: 
                   7657: /*
                   7658:   option passed -y3
                   7659: */
                   7660: 
                   7661: span.roman {font-family: serif; font-style: normal; font-weight: normal;}
                   7662: span.overacc2 {position: relative;  left: .8em; top: -1.2ex;}
                   7663: span.overacc1 {position: relative;  left: .6em; top: -1.2ex;}
                   7664: 
1.343     albertel 7665: END
                   7666: }
                   7667: 
1.306     albertel 7668: =pod
                   7669: 
                   7670: =item * &headtag()
                   7671: 
                   7672: Returns a uniform footer for LON-CAPA web pages.
                   7673: 
1.307     albertel 7674: Inputs: $title - optional title for the head
                   7675:         $head_extra - optional extra HTML to put inside the <head>
1.315     albertel 7676:         $args - optional arguments
1.319     albertel 7677:             force_register - if is true call registerurl so the remote is 
                   7678:                              informed
1.415     albertel 7679:             redirect       -> array ref of
                   7680:                                    1- seconds before redirect occurs
                   7681:                                    2- url to redirect to
                   7682:                                    3- whether the side effect should occur
1.315     albertel 7683:                            (side effect of setting 
                   7684:                                $env{'internal.head.redirect'} to the url 
                   7685:                                redirected too)
1.352     albertel 7686:             domain         -> force to color decorate a page for a specific
                   7687:                                domain
                   7688:             function       -> force usage of a specific rolish color scheme
                   7689:             bgcolor        -> override the default page bgcolor
1.460     albertel 7690:             no_auto_mt_title
                   7691:                            -> prevent &mt()ing the title arg
1.464     albertel 7692: 
1.306     albertel 7693: =cut
                   7694: 
                   7695: sub headtag {
1.313     albertel 7696:     my ($title,$head_extra,$args) = @_;
1.306     albertel 7697:     
1.363     albertel 7698:     my $function = $args->{'function'} || &get_users_function();
                   7699:     my $domain   = $args->{'domain'}   || &determinedomain();
                   7700:     my $bgcolor  = $args->{'bgcolor'}  || &designparm($function.'.pgbg',$domain);
1.1154    raeburn  7701:     my $httphost = $args->{'use_absolute'};
1.418     albertel 7702:     my $url = join(':',$env{'user.name'},$env{'user.domain'},
1.458     albertel 7703: 		   $Apache::lonnet::perlvar{'lonVersion'},
1.531     albertel 7704: 		   #time(),
1.418     albertel 7705: 		   $env{'environment.color.timestamp'},
1.363     albertel 7706: 		   $function,$domain,$bgcolor);
                   7707: 
1.369     www      7708:     $url = '/adm/css/'.&escape($url).'.css';
1.363     albertel 7709: 
1.308     albertel 7710:     my $result =
                   7711: 	'<head>'.
1.1160    raeburn  7712: 	&font_settings($args);
1.319     albertel 7713: 
1.1188    raeburn  7714:     my $inhibitprint;
                   7715:     if ($args->{'print_suppress'}) {
                   7716:         $inhibitprint = &print_suppression();
                   7717:     }
1.1064    raeburn  7718: 
1.461     albertel 7719:     if (!$args->{'frameset'}) {
                   7720: 	$result .= &Apache::lonhtmlcommon::htmlareaheaders();
                   7721:     }
1.962     droeschl 7722:     if ($args->{'force_register'} && $env{'request.noversionuri'} !~ m{^/res/adm/pages/}) {
                   7723:         $result .= Apache::lonxml::display_title();
1.319     albertel 7724:     }
1.436     albertel 7725:     if (!$args->{'no_nav_bar'} 
                   7726: 	&& !$args->{'only_body'}
                   7727: 	&& !$args->{'frameset'}) {
1.1154    raeburn  7728: 	$result .= &help_menu_js($httphost);
1.1032    www      7729:         $result.=&modal_window();
1.1038    www      7730:         $result.=&togglebox_script();
1.1034    www      7731:         $result.=&wishlist_window();
1.1041    www      7732:         $result.=&LCprogressbarUpdate_script();
1.1034    www      7733:     } else {
                   7734:         if ($args->{'add_modal'}) {
                   7735:            $result.=&modal_window();
                   7736:         }
                   7737:         if ($args->{'add_wishlist'}) {
                   7738:            $result.=&wishlist_window();
                   7739:         }
1.1038    www      7740:         if ($args->{'add_togglebox'}) {
                   7741:            $result.=&togglebox_script();
                   7742:         }
1.1041    www      7743:         if ($args->{'add_progressbar'}) {
                   7744:            $result.=&LCprogressbarUpdate_script();
                   7745:         }
1.436     albertel 7746:     }
1.314     albertel 7747:     if (ref($args->{'redirect'})) {
1.414     albertel 7748: 	my ($time,$url,$inhibit_continue) = @{$args->{'redirect'}};
1.315     albertel 7749: 	$url = &Apache::lonenc::check_encrypt($url);
1.414     albertel 7750: 	if (!$inhibit_continue) {
                   7751: 	    $env{'internal.head.redirect'} = $url;
                   7752: 	}
1.313     albertel 7753: 	$result.=<<ADDMETA
                   7754: <meta http-equiv="pragma" content="no-cache" />
1.344     albertel 7755: <meta http-equiv="Refresh" content="$time; url=$url" />
1.313     albertel 7756: ADDMETA
1.1210    raeburn  7757:     } else {
                   7758:         unless (($args->{'frameset'}) || ($args->{'js_ready'}) || ($args->{'only_body'}) || ($args->{'no_nav_bar'})) {
                   7759:             my $requrl = $env{'request.uri'};
                   7760:             if ($requrl eq '') {
                   7761:                 $requrl = $ENV{'REQUEST_URI'};
                   7762:                 $requrl =~ s/\?.+$//;
                   7763:             }
                   7764:             unless (($requrl =~ m{^/adm/(?:switchserver|login|authenticate|logout|groupsort|cleanup|helper|slotrequest|grades)(\?|$)}) ||
                   7765:                     (($requrl =~ m{^/res/}) && (($env{'form.submitted'} eq 'scantron') ||
                   7766:                      ($env{'form.grade_symb'}) || ($Apache::lonhomework::scantronmode)))) {
                   7767:                 my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
                   7768:                 unless (&Apache::lonnet::allowed('mau',$dom_in_use)) {
                   7769:                     my %domdefs = &Apache::lonnet::get_domain_defaults($dom_in_use);
                   7770:                     if (ref($domdefs{'offloadnow'}) eq 'HASH') {
                   7771:                         my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
                   7772:                         if ($domdefs{'offloadnow'}{$lonhost}) {
                   7773:                             my $newserver = &Apache::lonnet::spareserver(30000,undef,1,$dom_in_use);
                   7774:                             if (($newserver) && ($newserver ne $lonhost)) {
                   7775:                                 my $numsec = 5;
                   7776:                                 my $timeout = $numsec * 1000;
                   7777:                                 my ($newurl,$locknum,%locks,$msg);
                   7778:                                 if ($env{'request.role.adv'}) {
                   7779:                                     ($locknum,%locks) = &Apache::lonnet::get_locks();
                   7780:                                 }
                   7781:                                 my $disable_submit = 0;
                   7782:                                 if ($requrl =~ /$LONCAPA::assess_re/) {
                   7783:                                     $disable_submit = 1;
                   7784:                                 }
                   7785:                                 if ($locknum) {
                   7786:                                     my @lockinfo = sort(values(%locks));
                   7787:                                     $msg = &mt('Once the following tasks are complete: ')."\\n".
                   7788:                                            join(", ",sort(values(%locks)))."\\n".
                   7789:                                            &mt('your session will be transferred to a different server, after you click "Roles".');
                   7790:                                 } else {
                   7791:                                     if (($requrl =~ m{^/res/}) && ($env{'form.submitted'} =~ /^part_/)) {
                   7792:                                         $msg = &mt('Your LON-CAPA submission has been recorded')."\\n";
                   7793:                                     }
                   7794:                                     $msg .= &mt('Your current LON-CAPA session will be transferred to a different server in [quant,_1,second].',$numsec);
                   7795:                                     $newurl = '/adm/switchserver?otherserver='.$newserver;
                   7796:                                     if (($env{'request.role'}) && ($env{'request.role'} ne 'cm')) {
                   7797:                                         $newurl .= '&role='.$env{'request.role'};
                   7798:                                     }
                   7799:                                     if ($env{'request.symb'}) {
                   7800:                                         $newurl .= '&symb='.$env{'request.symb'};
                   7801:                                     } else {
                   7802:                                         $newurl .= '&origurl='.$requrl;
                   7803:                                     }
                   7804:                                 }
1.1222  ! damieng  7805:                                 &js_escape(\$msg);
1.1210    raeburn  7806:                                 $result.=<<OFFLOAD
                   7807: <meta http-equiv="pragma" content="no-cache" />
                   7808: <script type="text/javascript">
1.1215    raeburn  7809: // <![CDATA[
1.1210    raeburn  7810: function LC_Offload_Now() {
                   7811:     var dest = "$newurl";
                   7812:     if (dest != '') {
                   7813:         window.location.href="$newurl";
                   7814:     }
                   7815: }
1.1214    raeburn  7816: \$(document).ready(function () {
                   7817:     window.alert('$msg');
                   7818:     if ($disable_submit) {
1.1210    raeburn  7819:         \$(".LC_hwk_submit").prop("disabled", true);
                   7820:         \$( ".LC_textline" ).prop( "readonly", "readonly");
1.1214    raeburn  7821:     }
                   7822:     setTimeout('LC_Offload_Now()', $timeout);
                   7823: });
1.1215    raeburn  7824: // ]]>
1.1210    raeburn  7825: </script>
                   7826: OFFLOAD
                   7827:                             }
                   7828:                         }
                   7829:                     }
                   7830:                 }
                   7831:             }
                   7832:         }
1.313     albertel 7833:     }
1.306     albertel 7834:     if (!defined($title)) {
                   7835: 	$title = 'The LearningOnline Network with CAPA';
                   7836:     }
1.460     albertel 7837:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
                   7838:     $result .= '<title> LON-CAPA '.$title.'</title>'
1.1168    raeburn  7839: 	.'<link rel="stylesheet" type="text/css" href="'.$url.'"';
                   7840:     if (!$args->{'frameset'}) {
                   7841:         $result .= ' /';
                   7842:     }
                   7843:     $result .= '>' 
1.1064    raeburn  7844:         .$inhibitprint
1.414     albertel 7845: 	.$head_extra;
1.1137    raeburn  7846:     if ($env{'browser.mobile'}) {
                   7847:         $result .= '
                   7848: <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
                   7849: <meta name="apple-mobile-web-app-capable" content="yes" />';
                   7850:     }
1.962     droeschl 7851:     return $result.'</head>';
1.306     albertel 7852: }
                   7853: 
                   7854: =pod
                   7855: 
1.340     albertel 7856: =item * &font_settings()
                   7857: 
                   7858: Returns neccessary <meta> to set the proper encoding
                   7859: 
1.1160    raeburn  7860: Inputs: optional reference to HASH -- $args passed to &headtag()
1.340     albertel 7861: 
                   7862: =cut
                   7863: 
                   7864: sub font_settings {
1.1160    raeburn  7865:     my ($args) = @_;
1.340     albertel 7866:     my $headerstring='';
1.1160    raeburn  7867:     if ((!$env{'browser.mathml'} && $env{'browser.unicode'}) ||
                   7868:         ((ref($args) eq 'HASH') && ($args->{'browser.unicode'}))) {
1.1168    raeburn  7869:         $headerstring.=
                   7870:             '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"';
                   7871:         if (!$args->{'frameset'}) {
                   7872: 	    $headerstring.= ' /';
                   7873:         }
                   7874: 	$headerstring .= '>'."\n";
1.340     albertel 7875:     }
                   7876:     return $headerstring;
                   7877: }
                   7878: 
1.341     albertel 7879: =pod
                   7880: 
1.1064    raeburn  7881: =item * &print_suppression()
                   7882: 
                   7883: In course context returns css which causes the body to be blank when media="print",
                   7884: if printout generation is unavailable for the current resource.
                   7885: 
                   7886: This could be because:
                   7887: 
                   7888: (a) printstartdate is in the future
                   7889: 
                   7890: (b) printenddate is in the past
                   7891: 
                   7892: (c) there is an active exam block with "printout"
                   7893: functionality blocked
                   7894: 
                   7895: Users with pav, pfo or evb privileges are exempt.
                   7896: 
                   7897: Inputs: none
                   7898: 
                   7899: =cut
                   7900: 
                   7901: 
                   7902: sub print_suppression {
                   7903:     my $noprint;
                   7904:     if ($env{'request.course.id'}) {
                   7905:         my $scope = $env{'request.course.id'};
                   7906:         if ((&Apache::lonnet::allowed('pav',$scope)) ||
                   7907:             (&Apache::lonnet::allowed('pfo',$scope))) {
                   7908:             return;
                   7909:         }
                   7910:         if ($env{'request.course.sec'} ne '') {
                   7911:             $scope .= "/$env{'request.course.sec'}";
                   7912:             if ((&Apache::lonnet::allowed('pav',$scope)) ||
                   7913:                 (&Apache::lonnet::allowed('pfo',$scope))) {
1.1065    raeburn  7914:                 return;
1.1064    raeburn  7915:             }
                   7916:         }
                   7917:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7918:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.1189    raeburn  7919:         my $blocked = &blocking_status('printout',$cnum,$cdom,undef,1);
1.1064    raeburn  7920:         if ($blocked) {
                   7921:             my $checkrole = "cm./$cdom/$cnum";
                   7922:             if ($env{'request.course.sec'} ne '') {
                   7923:                 $checkrole .= "/$env{'request.course.sec'}";
                   7924:             }
                   7925:             unless ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
                   7926:                     ($env{'request.role'} !~ m{^st\./$cdom/$cnum})) {
                   7927:                 $noprint = 1;
                   7928:             }
                   7929:         }
                   7930:         unless ($noprint) {
                   7931:             my $symb = &Apache::lonnet::symbread();
                   7932:             if ($symb ne '') {
                   7933:                 my $navmap = Apache::lonnavmaps::navmap->new();
                   7934:                 if (ref($navmap)) {
                   7935:                     my $res = $navmap->getBySymb($symb);
                   7936:                     if (ref($res)) {
                   7937:                         if (!$res->resprintable()) {
                   7938:                             $noprint = 1;
                   7939:                         }
                   7940:                     }
                   7941:                 }
                   7942:             }
                   7943:         }
                   7944:         if ($noprint) {
                   7945:             return <<"ENDSTYLE";
                   7946: <style type="text/css" media="print">
                   7947:     body { display:none }
                   7948: </style>
                   7949: ENDSTYLE
                   7950:         }
                   7951:     }
                   7952:     return;
                   7953: }
                   7954: 
                   7955: =pod
                   7956: 
1.341     albertel 7957: =item * &xml_begin()
                   7958: 
                   7959: Returns the needed doctype and <html>
                   7960: 
                   7961: Inputs: none
                   7962: 
                   7963: =cut
                   7964: 
                   7965: sub xml_begin {
1.1168    raeburn  7966:     my ($is_frameset) = @_;
1.341     albertel 7967:     my $output='';
                   7968: 
                   7969:     if ($env{'browser.mathml'}) {
                   7970: 	$output='<?xml version="1.0"?>'
                   7971:             #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
                   7972: #            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                   7973:             
                   7974: #	    .'<!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">] >'
                   7975: 	    .'<!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">'
                   7976:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                   7977: 	    .'xmlns="http://www.w3.org/1999/xhtml">';
1.1168    raeburn  7978:     } elsif ($is_frameset) {
                   7979:         $output='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'."\n".
                   7980:                 '<html>'."\n";
1.341     albertel 7981:     } else {
1.1168    raeburn  7982: 	$output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n".
                   7983:                 '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'."\n";
1.341     albertel 7984:     }
                   7985:     return $output;
                   7986: }
1.340     albertel 7987: 
                   7988: =pod
                   7989: 
1.306     albertel 7990: =item * &start_page()
                   7991: 
                   7992: Returns a complete <html> .. <body> section for LON-CAPA web pages.
                   7993: 
1.648     raeburn  7994: Inputs:
                   7995: 
                   7996: =over 4
                   7997: 
                   7998: $title - optional title for the page
                   7999: 
                   8000: $head_extra - optional extra HTML to incude inside the <head>
                   8001: 
                   8002: $args - additional optional args supported are:
                   8003: 
                   8004: =over 8
                   8005: 
                   8006:              only_body      -> is true will set &bodytag() onlybodytag
1.317     albertel 8007:                                     arg on
1.814     bisitz   8008:              no_nav_bar     -> is true will set &bodytag() no_nav_bar arg on
1.648     raeburn  8009:              add_entries    -> additional attributes to add to the  <body>
                   8010:              domain         -> force to color decorate a page for a 
1.317     albertel 8011:                                     specific domain
1.648     raeburn  8012:              function       -> force usage of a specific rolish color
1.317     albertel 8013:                                     scheme
1.648     raeburn  8014:              redirect       -> see &headtag()
                   8015:              bgcolor        -> override the default page bg color
                   8016:              js_ready       -> return a string ready for being used in 
1.317     albertel 8017:                                     a javascript writeln
1.648     raeburn  8018:              html_encode    -> return a string ready for being used in 
1.320     albertel 8019:                                     a html attribute
1.648     raeburn  8020:              force_register -> if is true will turn on the &bodytag()
1.317     albertel 8021:                                     $forcereg arg
1.648     raeburn  8022:              frameset       -> if true will start with a <frameset>
1.330     albertel 8023:                                     rather than <body>
1.648     raeburn  8024:              skip_phases    -> hash ref of 
1.338     albertel 8025:                                     head -> skip the <html><head> generation
                   8026:                                     body -> skip all <body> generation
1.648     raeburn  8027:              no_auto_mt_title -> prevent &mt()ing the title arg
                   8028:              inherit_jsmath -> when creating popup window in a page,
                   8029:                                     should it have jsmath forced on by the
                   8030:                                     current page
1.867     kalberla 8031:              bread_crumbs ->             Array containing breadcrumbs
1.983     raeburn  8032:              bread_crumbs_component ->  if exists show it as headline else show only the breadcrumbs
1.1096    raeburn  8033:              group          -> includes the current group, if page is for a 
                   8034:                                specific group  
1.361     albertel 8035: 
1.648     raeburn  8036: =back
1.460     albertel 8037: 
1.648     raeburn  8038: =back
1.562     albertel 8039: 
1.306     albertel 8040: =cut
                   8041: 
                   8042: sub start_page {
1.309     albertel 8043:     my ($title,$head_extra,$args) = @_;
1.318     albertel 8044:     #&Apache::lonnet::logthis("start_page ".join(':',caller(0)));
1.319     albertel 8045: 
1.315     albertel 8046:     $env{'internal.start_page'}++;
1.1096    raeburn  8047:     my ($result,@advtools);
1.964     droeschl 8048: 
1.338     albertel 8049:     if (! exists($args->{'skip_phases'}{'head'}) ) {
1.1168    raeburn  8050:         $result .= &xml_begin($args->{'frameset'}) . &headtag($title, $head_extra, $args);
1.338     albertel 8051:     }
                   8052:     
                   8053:     if (! exists($args->{'skip_phases'}{'body'}) ) {
                   8054: 	if ($args->{'frameset'}) {
                   8055: 	    my $attr_string = &make_attr_string($args->{'force_register'},
                   8056: 						$args->{'add_entries'});
                   8057: 	    $result .= "\n<frameset $attr_string>\n";
1.831     bisitz   8058:         } else {
                   8059:             $result .=
                   8060:                 &bodytag($title, 
                   8061:                          $args->{'function'},       $args->{'add_entries'},
                   8062:                          $args->{'only_body'},      $args->{'domain'},
                   8063:                          $args->{'force_register'}, $args->{'no_nav_bar'},
1.1096    raeburn  8064:                          $args->{'bgcolor'},        $args,
                   8065:                          \@advtools);
1.831     bisitz   8066:         }
1.330     albertel 8067:     }
1.338     albertel 8068: 
1.315     albertel 8069:     if ($args->{'js_ready'}) {
1.713     kaisler  8070: 		$result = &js_ready($result);
1.315     albertel 8071:     }
1.320     albertel 8072:     if ($args->{'html_encode'}) {
1.713     kaisler  8073: 		$result = &html_encode($result);
                   8074:     }
                   8075: 
1.813     bisitz   8076:     # Preparation for new and consistent functionlist at top of screen
                   8077:     # if ($args->{'functionlist'}) {
                   8078:     #            $result .= &build_functionlist();
                   8079:     #}
                   8080: 
1.964     droeschl 8081:     # Don't add anything more if only_body wanted or in const space
                   8082:     return $result if    $args->{'only_body'} 
                   8083:                       || $env{'request.state'} eq 'construct';
1.813     bisitz   8084: 
                   8085:     #Breadcrumbs
1.758     kaisler  8086:     if (exists($args->{'bread_crumbs'}) or exists($args->{'bread_crumbs_component'})) {
                   8087: 		&Apache::lonhtmlcommon::clear_breadcrumbs();
                   8088: 		#if any br links exists, add them to the breadcrumbs
                   8089: 		if (exists($args->{'bread_crumbs'}) and ref($args->{'bread_crumbs'}) eq 'ARRAY') {         
                   8090: 			foreach my $crumb (@{$args->{'bread_crumbs'}}){
                   8091: 				&Apache::lonhtmlcommon::add_breadcrumb($crumb);
                   8092: 			}
                   8093: 		}
1.1096    raeburn  8094:                 # if @advtools array contains items add then to the breadcrumbs
                   8095:                 if (@advtools > 0) {
                   8096:                     &Apache::lonmenu::advtools_crumbs(@advtools);
                   8097:                 }
1.758     kaisler  8098: 
                   8099: 		#if bread_crumbs_component exists show it as headline else show only the breadcrumbs
                   8100: 		if(exists($args->{'bread_crumbs_component'})){
                   8101: 			$result .= &Apache::lonhtmlcommon::breadcrumbs($args->{'bread_crumbs_component'});
                   8102: 		}else{
                   8103: 			$result .= &Apache::lonhtmlcommon::breadcrumbs();
                   8104: 		}
1.320     albertel 8105:     }
1.315     albertel 8106:     return $result;
1.306     albertel 8107: }
                   8108: 
                   8109: sub end_page {
1.315     albertel 8110:     my ($args) = @_;
                   8111:     $env{'internal.end_page'}++;
1.330     albertel 8112:     my $result;
1.335     albertel 8113:     if ($args->{'discussion'}) {
                   8114: 	my ($target,$parser);
                   8115: 	if (ref($args->{'discussion'})) {
                   8116: 	    ($target,$parser) =($args->{'discussion'}{'target'},
                   8117: 				$args->{'discussion'}{'parser'});
                   8118: 	}
                   8119: 	$result .= &Apache::lonxml::xmlend($target,$parser);
                   8120:     }
1.330     albertel 8121:     if ($args->{'frameset'}) {
                   8122: 	$result .= '</frameset>';
                   8123:     } else {
1.635     raeburn  8124: 	$result .= &endbodytag($args);
1.330     albertel 8125:     }
1.1080    raeburn  8126:     unless ($args->{'notbody'}) {
                   8127:         $result .= "\n</html>";
                   8128:     }
1.330     albertel 8129: 
1.315     albertel 8130:     if ($args->{'js_ready'}) {
1.317     albertel 8131: 	$result = &js_ready($result);
1.315     albertel 8132:     }
1.335     albertel 8133: 
1.320     albertel 8134:     if ($args->{'html_encode'}) {
                   8135: 	$result = &html_encode($result);
                   8136:     }
1.335     albertel 8137: 
1.315     albertel 8138:     return $result;
                   8139: }
                   8140: 
1.1034    www      8141: sub wishlist_window {
                   8142:     return(<<'ENDWISHLIST');
1.1046    raeburn  8143: <script type="text/javascript">
1.1034    www      8144: // <![CDATA[
                   8145: // <!-- BEGIN LON-CAPA Internal
                   8146: function set_wishlistlink(title, path) {
                   8147:     if (!title) {
                   8148:         title = document.title;
                   8149:         title = title.replace(/^LON-CAPA /,'');
                   8150:     }
1.1175    raeburn  8151:     title = encodeURIComponent(title);
1.1203    raeburn  8152:     title = title.replace("'","\\\'");
1.1034    www      8153:     if (!path) {
                   8154:         path = location.pathname;
                   8155:     }
1.1175    raeburn  8156:     path = encodeURIComponent(path);
1.1203    raeburn  8157:     path = path.replace("'","\\\'");
1.1034    www      8158:     Win = window.open('/adm/wishlist?mode=newLink&setTitle='+title+'&setPath='+path,
                   8159:                       'wishlistNewLink','width=560,height=350,scrollbars=0');
                   8160: }
                   8161: // END LON-CAPA Internal -->
                   8162: // ]]>
                   8163: </script>
                   8164: ENDWISHLIST
                   8165: }
                   8166: 
1.1030    www      8167: sub modal_window {
                   8168:     return(<<'ENDMODAL');
1.1046    raeburn  8169: <script type="text/javascript">
1.1030    www      8170: // <![CDATA[
                   8171: // <!-- BEGIN LON-CAPA Internal
                   8172: var modalWindow = {
                   8173: 	parent:"body",
                   8174: 	windowId:null,
                   8175: 	content:null,
                   8176: 	width:null,
                   8177: 	height:null,
                   8178: 	close:function()
                   8179: 	{
                   8180: 	        $(".LCmodal-window").remove();
                   8181: 	        $(".LCmodal-overlay").remove();
                   8182: 	},
                   8183: 	open:function()
                   8184: 	{
                   8185: 		var modal = "";
                   8186: 		modal += "<div class=\"LCmodal-overlay\"></div>";
                   8187: 		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;\">";
                   8188: 		modal += this.content;
                   8189: 		modal += "</div>";	
                   8190: 
                   8191: 		$(this.parent).append(modal);
                   8192: 
                   8193: 		$(".LCmodal-window").append("<a class=\"LCclose-window\"></a>");
                   8194: 		$(".LCclose-window").click(function(){modalWindow.close();});
                   8195: 		$(".LCmodal-overlay").click(function(){modalWindow.close();});
                   8196: 	}
                   8197: };
1.1140    raeburn  8198: 	var openMyModal = function(source,width,height,scrolling,transparency,style)
1.1030    www      8199: 	{
1.1203    raeburn  8200:                 source = source.replace("'","&#39;");
1.1030    www      8201: 		modalWindow.windowId = "myModal";
                   8202: 		modalWindow.width = width;
                   8203: 		modalWindow.height = height;
1.1196    raeburn  8204: 		modalWindow.content = "<iframe width='"+width+"' height='"+height+"' frameborder='0' scrolling='"+scrolling+"' allowtransparency='"+transparency+"' src='" + source + "' style='"+style+"'></iframe>";
1.1030    www      8205: 		modalWindow.open();
1.1208    raeburn  8206: 	};
1.1030    www      8207: // END LON-CAPA Internal -->
                   8208: // ]]>
                   8209: </script>
                   8210: ENDMODAL
                   8211: }
                   8212: 
                   8213: sub modal_link {
1.1140    raeburn  8214:     my ($link,$linktext,$width,$height,$target,$scrolling,$title,$transparency,$style)=@_;
1.1030    www      8215:     unless ($width) { $width=480; }
                   8216:     unless ($height) { $height=400; }
1.1031    www      8217:     unless ($scrolling) { $scrolling='yes'; }
1.1140    raeburn  8218:     unless ($transparency) { $transparency='true'; }
                   8219: 
1.1074    raeburn  8220:     my $target_attr;
                   8221:     if (defined($target)) {
                   8222:         $target_attr = 'target="'.$target.'"';
                   8223:     }
                   8224:     return <<"ENDLINK";
1.1140    raeburn  8225: <a href="$link" $target_attr title="$title" onclick="javascript:openMyModal('$link',$width,$height,'$scrolling','$transparency','$style'); return false;">
1.1074    raeburn  8226:            $linktext</a>
                   8227: ENDLINK
1.1030    www      8228: }
                   8229: 
1.1032    www      8230: sub modal_adhoc_script {
                   8231:     my ($funcname,$width,$height,$content)=@_;
                   8232:     return (<<ENDADHOC);
1.1046    raeburn  8233: <script type="text/javascript">
1.1032    www      8234: // <![CDATA[
                   8235:         var $funcname = function()
                   8236:         {
                   8237:                 modalWindow.windowId = "myModal";
                   8238:                 modalWindow.width = $width;
                   8239:                 modalWindow.height = $height;
                   8240:                 modalWindow.content = '$content';
                   8241:                 modalWindow.open();
                   8242:         };  
                   8243: // ]]>
                   8244: </script>
                   8245: ENDADHOC
                   8246: }
                   8247: 
1.1041    www      8248: sub modal_adhoc_inner {
                   8249:     my ($funcname,$width,$height,$content)=@_;
                   8250:     my $innerwidth=$width-20;
                   8251:     $content=&js_ready(
1.1140    raeburn  8252:                  &start_page('Dialog',undef,{'only_body'=>1,'bgcolor'=>'#FFFFFF'}).
                   8253:                  &start_scrollbox($width.'px',$innerwidth.'px',$height.'px','myModal','#FFFFFF',undef,1).
                   8254:                  $content.
1.1041    www      8255:                  &end_scrollbox().
1.1140    raeburn  8256:                  &end_page()
1.1041    www      8257:              );
                   8258:     return &modal_adhoc_script($funcname,$width,$height,$content);
                   8259: }
                   8260: 
                   8261: sub modal_adhoc_window {
                   8262:     my ($funcname,$width,$height,$content,$linktext)=@_;
                   8263:     return &modal_adhoc_inner($funcname,$width,$height,$content).
                   8264:            "<a href=\"javascript:$funcname();void(0);\">".$linktext."</a>";
                   8265: }
                   8266: 
                   8267: sub modal_adhoc_launch {
                   8268:     my ($funcname,$width,$height,$content)=@_;
                   8269:     return &modal_adhoc_inner($funcname,$width,$height,$content).(<<ENDLAUNCH);
                   8270: <script type="text/javascript">
                   8271: // <![CDATA[
                   8272: $funcname();
                   8273: // ]]>
                   8274: </script>
                   8275: ENDLAUNCH
                   8276: }
                   8277: 
                   8278: sub modal_adhoc_close {
                   8279:     return (<<ENDCLOSE);
                   8280: <script type="text/javascript">
                   8281: // <![CDATA[
                   8282: modalWindow.close();
                   8283: // ]]>
                   8284: </script>
                   8285: ENDCLOSE
                   8286: }
                   8287: 
1.1038    www      8288: sub togglebox_script {
                   8289:    return(<<ENDTOGGLE);
                   8290: <script type="text/javascript"> 
                   8291: // <![CDATA[
                   8292: function LCtoggleDisplay(id,hidetext,showtext) {
                   8293:    link = document.getElementById(id + "link").childNodes[0];
                   8294:    with (document.getElementById(id).style) {
                   8295:       if (display == "none" ) {
                   8296:           display = "inline";
                   8297:           link.nodeValue = hidetext;
                   8298:         } else {
                   8299:           display = "none";
                   8300:           link.nodeValue = showtext;
                   8301:        }
                   8302:    }
                   8303: }
                   8304: // ]]>
                   8305: </script>
                   8306: ENDTOGGLE
                   8307: }
                   8308: 
1.1039    www      8309: sub start_togglebox {
                   8310:     my ($id,$heading,$headerbg,$hidetext,$showtext)=@_;
                   8311:     unless ($heading) { $heading=''; } else { $heading.=' '; }
                   8312:     unless ($showtext) { $showtext=&mt('show'); }
                   8313:     unless ($hidetext) { $hidetext=&mt('hide'); }
                   8314:     unless ($headerbg) { $headerbg='#FFFFFF'; }
                   8315:     return &start_data_table().
                   8316:            &start_data_table_header_row().
                   8317:            '<td bgcolor="'.$headerbg.'">'.$heading.
                   8318:            '[<a id="'.$id.'link" href="javascript:LCtoggleDisplay(\''.$id.'\',\''.$hidetext.'\',\''.
                   8319:            $showtext.'\')">'.$showtext.'</a>]</td>'.
                   8320:            &end_data_table_header_row().
                   8321:            '<tr id="'.$id.'" style="display:none""><td>';
                   8322: }
                   8323: 
                   8324: sub end_togglebox {
                   8325:     return '</td></tr>'.&end_data_table();
                   8326: }
                   8327: 
1.1041    www      8328: sub LCprogressbar_script {
1.1045    www      8329:    my ($id)=@_;
1.1041    www      8330:    return(<<ENDPROGRESS);
                   8331: <script type="text/javascript">
                   8332: // <![CDATA[
1.1045    www      8333: \$('#progressbar$id').progressbar({
1.1041    www      8334:   value: 0,
                   8335:   change: function(event, ui) {
                   8336:     var newVal = \$(this).progressbar('option', 'value');
                   8337:     \$('.pblabel', this).text(LCprogressTxt);
                   8338:   }
                   8339: });
                   8340: // ]]>
                   8341: </script>
                   8342: ENDPROGRESS
                   8343: }
                   8344: 
                   8345: sub LCprogressbarUpdate_script {
                   8346:    return(<<ENDPROGRESSUPDATE);
                   8347: <style type="text/css">
                   8348: .ui-progressbar { position:relative; }
                   8349: .pblabel { position: absolute; width: 100%; text-align: center; line-height: 1.9em; }
                   8350: </style>
                   8351: <script type="text/javascript">
                   8352: // <![CDATA[
1.1045    www      8353: var LCprogressTxt='---';
                   8354: 
                   8355: function LCupdateProgress(percent,progresstext,id) {
1.1041    www      8356:    LCprogressTxt=progresstext;
1.1045    www      8357:    \$('#progressbar'+id).progressbar('value',percent);
1.1041    www      8358: }
                   8359: // ]]>
                   8360: </script>
                   8361: ENDPROGRESSUPDATE
                   8362: }
                   8363: 
1.1042    www      8364: my $LClastpercent;
1.1045    www      8365: my $LCidcnt;
                   8366: my $LCcurrentid;
1.1042    www      8367: 
1.1041    www      8368: sub LCprogressbar {
1.1042    www      8369:     my ($r)=(@_);
                   8370:     $LClastpercent=0;
1.1045    www      8371:     $LCidcnt++;
                   8372:     $LCcurrentid=$$.'_'.$LCidcnt;
1.1041    www      8373:     my $starting=&mt('Starting');
                   8374:     my $content=(<<ENDPROGBAR);
1.1045    www      8375:   <div id="progressbar$LCcurrentid">
1.1041    www      8376:     <span class="pblabel">$starting</span>
                   8377:   </div>
                   8378: ENDPROGBAR
1.1045    www      8379:     &r_print($r,$content.&LCprogressbar_script($LCcurrentid));
1.1041    www      8380: }
                   8381: 
                   8382: sub LCprogressbarUpdate {
1.1042    www      8383:     my ($r,$val,$text)=@_;
                   8384:     unless ($val) { 
                   8385:        if ($LClastpercent) {
                   8386:            $val=$LClastpercent;
                   8387:        } else {
                   8388:            $val=0;
                   8389:        }
                   8390:     }
1.1041    www      8391:     if ($val<0) { $val=0; }
                   8392:     if ($val>100) { $val=0; }
1.1042    www      8393:     $LClastpercent=$val;
1.1041    www      8394:     unless ($text) { $text=$val.'%'; }
                   8395:     $text=&js_ready($text);
1.1044    www      8396:     &r_print($r,<<ENDUPDATE);
1.1041    www      8397: <script type="text/javascript">
                   8398: // <![CDATA[
1.1045    www      8399: LCupdateProgress($val,'$text','$LCcurrentid');
1.1041    www      8400: // ]]>
                   8401: </script>
                   8402: ENDUPDATE
1.1035    www      8403: }
                   8404: 
1.1042    www      8405: sub LCprogressbarClose {
                   8406:     my ($r)=@_;
                   8407:     $LClastpercent=0;
1.1044    www      8408:     &r_print($r,<<ENDCLOSE);
1.1042    www      8409: <script type="text/javascript">
                   8410: // <![CDATA[
1.1045    www      8411: \$("#progressbar$LCcurrentid").hide('slow'); 
1.1042    www      8412: // ]]>
                   8413: </script>
                   8414: ENDCLOSE
1.1044    www      8415: }
                   8416: 
                   8417: sub r_print {
                   8418:     my ($r,$to_print)=@_;
                   8419:     if ($r) {
                   8420:       $r->print($to_print);
                   8421:       $r->rflush();
                   8422:     } else {
                   8423:       print($to_print);
                   8424:     }
1.1042    www      8425: }
                   8426: 
1.320     albertel 8427: sub html_encode {
                   8428:     my ($result) = @_;
                   8429: 
1.322     albertel 8430:     $result = &HTML::Entities::encode($result,'<>&"');
1.320     albertel 8431:     
                   8432:     return $result;
                   8433: }
1.1044    www      8434: 
1.317     albertel 8435: sub js_ready {
                   8436:     my ($result) = @_;
                   8437: 
1.323     albertel 8438:     $result =~ s/[\n\r]/ /xmsg;
                   8439:     $result =~ s/\\/\\\\/xmsg;
                   8440:     $result =~ s/'/\\'/xmsg;
1.372     albertel 8441:     $result =~ s{</}{<\\/}xmsg;
1.317     albertel 8442:     
                   8443:     return $result;
                   8444: }
                   8445: 
1.315     albertel 8446: sub validate_page {
                   8447:     if (  exists($env{'internal.start_page'})
1.316     albertel 8448: 	  &&     $env{'internal.start_page'} > 1) {
                   8449: 	&Apache::lonnet::logthis('start_page called multiple times '.
1.318     albertel 8450: 				 $env{'internal.start_page'}.' '.
1.316     albertel 8451: 				 $ENV{'request.filename'});
1.315     albertel 8452:     }
                   8453:     if (  exists($env{'internal.end_page'})
1.316     albertel 8454: 	  &&     $env{'internal.end_page'} > 1) {
                   8455: 	&Apache::lonnet::logthis('end_page called multiple times '.
1.318     albertel 8456: 				 $env{'internal.end_page'}.' '.
1.316     albertel 8457: 				 $env{'request.filename'});
1.315     albertel 8458:     }
                   8459:     if (     exists($env{'internal.start_page'})
                   8460: 	&& ! exists($env{'internal.end_page'})) {
1.316     albertel 8461: 	&Apache::lonnet::logthis('start_page called without end_page '.
                   8462: 				 $env{'request.filename'});
1.315     albertel 8463:     }
                   8464:     if (   ! exists($env{'internal.start_page'})
                   8465: 	&&   exists($env{'internal.end_page'})) {
1.316     albertel 8466: 	&Apache::lonnet::logthis('end_page called without start_page'.
                   8467: 				 $env{'request.filename'});
1.315     albertel 8468:     }
1.306     albertel 8469: }
1.315     albertel 8470: 
1.996     www      8471: 
                   8472: sub start_scrollbox {
1.1140    raeburn  8473:     my ($outerwidth,$width,$height,$id,$bgcolor,$cursor,$needjsready) = @_;
1.998     raeburn  8474:     unless ($outerwidth) { $outerwidth='520px'; }
                   8475:     unless ($width) { $width='500px'; }
                   8476:     unless ($height) { $height='200px'; }
1.1075    raeburn  8477:     my ($table_id,$div_id,$tdcol);
1.1018    raeburn  8478:     if ($id ne '') {
1.1140    raeburn  8479:         $table_id = ' id="table_'.$id.'"';
1.1137    raeburn  8480:         $div_id = ' id="div_'.$id.'"';
1.1018    raeburn  8481:     }
1.1075    raeburn  8482:     if ($bgcolor ne '') {
                   8483:         $tdcol = "background-color: $bgcolor;";
                   8484:     }
1.1137    raeburn  8485:     my $nicescroll_js;
                   8486:     if ($env{'browser.mobile'}) {
1.1140    raeburn  8487:         $nicescroll_js = &nicescroll_javascript('div_'.$id,$cursor,$needjsready);
                   8488:     }
                   8489:     return <<"END";
                   8490: $nicescroll_js
                   8491: 
                   8492: <table style="width: $outerwidth; border: 1px solid none;"$table_id><tr><td style="width: $width;$tdcol">
                   8493: <div style="overflow:auto; width:$width; height:$height;"$div_id>
                   8494: END
                   8495: }
                   8496: 
                   8497: sub end_scrollbox {
                   8498:     return '</div></td></tr></table>';
                   8499: }
                   8500: 
                   8501: sub nicescroll_javascript {
                   8502:     my ($id,$cursor,$needjsready,$framecheck,$location) = @_;
                   8503:     my %options;
                   8504:     if (ref($cursor) eq 'HASH') {
                   8505:         %options = %{$cursor};
                   8506:     }
                   8507:     unless ($options{'railalign'} =~ /^left|right$/) {
                   8508:         $options{'railalign'} = 'left';
                   8509:     }
                   8510:     unless ($options{'cursorcolor'} =~ /^\#\w+$/) {
                   8511:         my $function  = &get_users_function();
                   8512:         $options{'cursorcolor'} = &designparm($function.'.sidebg',$env{'request.role.domain'});
1.1138    raeburn  8513:         unless ($options{'cursorcolor'} =~ /^\#\w+$/) {
1.1140    raeburn  8514:             $options{'cursorcolor'} = '#00F';
1.1138    raeburn  8515:         }
1.1140    raeburn  8516:     }
                   8517:     if ($options{'cursoropacity'} =~ /^[\d.]+$/) {
                   8518:         unless ($options{'cursoropacity'} >= 0.0 && $options{'cursoropacity'} <=1.0) {
1.1138    raeburn  8519:             $options{'cursoropacity'}='1.0';
                   8520:         }
1.1140    raeburn  8521:     } else {
                   8522:         $options{'cursoropacity'}='1.0';
                   8523:     }
                   8524:     if ($options{'cursorfixedheight'} eq 'none') {
                   8525:         delete($options{'cursorfixedheight'});
                   8526:     } else {
                   8527:         unless ($options{'cursorfixedheight'} =~ /^\d+$/) { $options{'cursorfixedheight'}='50'; }
                   8528:     }
                   8529:     unless ($options{'railoffset'} =~ /^{[\w\:\d\-,]+}$/) {
                   8530:         delete($options{'railoffset'});
                   8531:     }
                   8532:     my @niceoptions;
                   8533:     while (my($key,$value) = each(%options)) {
                   8534:         if ($value =~ /^\{.+\}$/) {
                   8535:             push(@niceoptions,$key.':'.$value);
1.1138    raeburn  8536:         } else {
1.1140    raeburn  8537:             push(@niceoptions,$key.':"'.$value.'"');
1.1138    raeburn  8538:         }
1.1140    raeburn  8539:     }
                   8540:     my $nicescroll_js = '
1.1137    raeburn  8541: $(document).ready(
1.1140    raeburn  8542:       function() {
                   8543:           $("#'.$id.'").niceScroll({'.join(',',@niceoptions).'});
                   8544:       }
1.1137    raeburn  8545: );
                   8546: ';
1.1140    raeburn  8547:     if ($framecheck) {
                   8548:         $nicescroll_js .= '
                   8549: function expand_div(caller) {
                   8550:     if (top === self) {
                   8551:         document.getElementById("'.$id.'").style.width = "auto";
                   8552:         document.getElementById("'.$id.'").style.height = "auto";
                   8553:     } else {
                   8554:         try {
                   8555:             if (parent.frames) {
                   8556:                 if (parent.frames.length > 1) {
                   8557:                     var framesrc = parent.frames[1].location.href;
                   8558:                     var currsrc = framesrc.replace(/\#.*$/,"");
                   8559:                     if ((caller == "search") || (currsrc == "'.$location.'")) {
                   8560:                         document.getElementById("'.$id.'").style.width = "auto";
                   8561:                         document.getElementById("'.$id.'").style.height = "auto";
                   8562:                     }
                   8563:                 }
                   8564:             }
                   8565:         } catch (e) {
                   8566:             return;
                   8567:         }
1.1137    raeburn  8568:     }
1.1140    raeburn  8569:     return;
1.996     www      8570: }
1.1140    raeburn  8571: ';
                   8572:     }
                   8573:     if ($needjsready) {
                   8574:         $nicescroll_js = '
                   8575: <script type="text/javascript">'."\n".$nicescroll_js."\n</script>\n";
                   8576:     } else {
                   8577:         $nicescroll_js = &Apache::lonhtmlcommon::scripttag($nicescroll_js);
                   8578:     }
                   8579:     return $nicescroll_js;
1.996     www      8580: }
                   8581: 
1.318     albertel 8582: sub simple_error_page {
1.1150    bisitz   8583:     my ($r,$title,$msg,$args) = @_;
1.1151    raeburn  8584:     if (ref($args) eq 'HASH') {
                   8585:         if (!$args->{'no_auto_mt_msg'}) { $msg = &mt($msg); }
                   8586:     } else {
                   8587:         $msg = &mt($msg);
                   8588:     }
1.1150    bisitz   8589: 
1.318     albertel 8590:     my $page =
                   8591: 	&Apache::loncommon::start_page($title).
1.1150    bisitz   8592: 	'<p class="LC_error">'.$msg.'</p>'.
1.318     albertel 8593: 	&Apache::loncommon::end_page();
                   8594:     if (ref($r)) {
                   8595: 	$r->print($page);
1.327     albertel 8596: 	return;
1.318     albertel 8597:     }
                   8598:     return $page;
                   8599: }
1.347     albertel 8600: 
                   8601: {
1.610     albertel 8602:     my @row_count;
1.961     onken    8603: 
                   8604:     sub start_data_table_count {
                   8605:         unshift(@row_count, 0);
                   8606:         return;
                   8607:     }
                   8608: 
                   8609:     sub end_data_table_count {
                   8610:         shift(@row_count);
                   8611:         return;
                   8612:     }
                   8613: 
1.347     albertel 8614:     sub start_data_table {
1.1018    raeburn  8615: 	my ($add_class,$id) = @_;
1.422     albertel 8616: 	my $css_class = (join(' ','LC_data_table',$add_class));
1.1018    raeburn  8617:         my $table_id;
                   8618:         if (defined($id)) {
                   8619:             $table_id = ' id="'.$id.'"';
                   8620:         }
1.961     onken    8621: 	&start_data_table_count();
1.1018    raeburn  8622: 	return '<table class="'.$css_class.'"'.$table_id.'>'."\n";
1.347     albertel 8623:     }
                   8624: 
                   8625:     sub end_data_table {
1.961     onken    8626: 	&end_data_table_count();
1.389     albertel 8627: 	return '</table>'."\n";;
1.347     albertel 8628:     }
                   8629: 
                   8630:     sub start_data_table_row {
1.974     wenzelju 8631: 	my ($add_class, $id) = @_;
1.610     albertel 8632: 	$row_count[0]++;
                   8633: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.900     bisitz   8634: 	$css_class = (join(' ',$css_class,$add_class)) unless ($add_class eq '');
1.974     wenzelju 8635:         $id = (' id="'.$id.'"') unless ($id eq '');
                   8636:         return  '<tr class="'.$css_class.'"'.$id.'>'."\n";
1.347     albertel 8637:     }
1.471     banghart 8638:     
                   8639:     sub continue_data_table_row {
1.974     wenzelju 8640: 	my ($add_class, $id) = @_;
1.610     albertel 8641: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.974     wenzelju 8642: 	$css_class = (join(' ',$css_class,$add_class)) unless ($add_class eq '');
                   8643:         $id = (' id="'.$id.'"') unless ($id eq '');
                   8644:         return  '<tr class="'.$css_class.'"'.$id.'>'."\n";
1.471     banghart 8645:     }
1.347     albertel 8646: 
                   8647:     sub end_data_table_row {
1.389     albertel 8648: 	return '</tr>'."\n";;
1.347     albertel 8649:     }
1.367     www      8650: 
1.421     albertel 8651:     sub start_data_table_empty_row {
1.707     bisitz   8652: #	$row_count[0]++;
1.421     albertel 8653: 	return  '<tr class="LC_empty_row" >'."\n";;
                   8654:     }
                   8655: 
                   8656:     sub end_data_table_empty_row {
                   8657: 	return '</tr>'."\n";;
                   8658:     }
                   8659: 
1.367     www      8660:     sub start_data_table_header_row {
1.389     albertel 8661: 	return  '<tr class="LC_header_row">'."\n";;
1.367     www      8662:     }
                   8663: 
                   8664:     sub end_data_table_header_row {
1.389     albertel 8665: 	return '</tr>'."\n";;
1.367     www      8666:     }
1.890     droeschl 8667: 
                   8668:     sub data_table_caption {
                   8669:         my $caption = shift;
                   8670:         return "<caption class=\"LC_caption\">$caption</caption>";
                   8671:     }
1.347     albertel 8672: }
                   8673: 
1.548     albertel 8674: =pod
                   8675: 
                   8676: =item * &inhibit_menu_check($arg)
                   8677: 
                   8678: Checks for a inhibitmenu state and generates output to preserve it
                   8679: 
                   8680: Inputs:         $arg - can be any of
                   8681:                      - undef - in which case the return value is a string 
                   8682:                                to add  into arguments list of a uri
                   8683:                      - 'input' - in which case the return value is a HTML
                   8684:                                  <form> <input> field of type hidden to
                   8685:                                  preserve the value
                   8686:                      - a url - in which case the return value is the url with
                   8687:                                the neccesary cgi args added to preserve the
                   8688:                                inhibitmenu state
                   8689:                      - a ref to a url - no return value, but the string is
                   8690:                                         updated to include the neccessary cgi
                   8691:                                         args to preserve the inhibitmenu state
                   8692: 
                   8693: =cut
                   8694: 
                   8695: sub inhibit_menu_check {
                   8696:     my ($arg) = @_;
                   8697:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
                   8698:     if ($arg eq 'input') {
                   8699: 	if ($env{'form.inhibitmenu'}) {
                   8700: 	    return '<input type="hidden" name="inhibitmenu" value="'.$env{'form.inhibitmenu'}.'" />';
                   8701: 	} else {
                   8702: 	    return
                   8703: 	}
                   8704:     }
                   8705:     if ($env{'form.inhibitmenu'}) {
                   8706: 	if (ref($arg)) {
                   8707: 	    $$arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   8708: 	} elsif ($arg eq '') {
                   8709: 	    $arg .= 'inhibitmenu='.$env{'form.inhibitmenu'};
                   8710: 	} else {
                   8711: 	    $arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   8712: 	}
                   8713:     }
                   8714:     if (!ref($arg)) {
                   8715: 	return $arg;
                   8716:     }
                   8717: }
                   8718: 
1.251     albertel 8719: ###############################################
1.182     matthew  8720: 
                   8721: =pod
                   8722: 
1.549     albertel 8723: =back
                   8724: 
                   8725: =head1 User Information Routines
                   8726: 
                   8727: =over 4
                   8728: 
1.405     albertel 8729: =item * &get_users_function()
1.182     matthew  8730: 
                   8731: Used by &bodytag to determine the current users primary role.
                   8732: Returns either 'student','coordinator','admin', or 'author'.
                   8733: 
                   8734: =cut
                   8735: 
                   8736: ###############################################
                   8737: sub get_users_function {
1.815     tempelho 8738:     my $function = 'norole';
1.818     tempelho 8739:     if ($env{'request.role'}=~/^(st)/) {
                   8740:         $function='student';
                   8741:     }
1.907     raeburn  8742:     if ($env{'request.role'}=~/^(cc|co|in|ta|ep)/) {
1.182     matthew  8743:         $function='coordinator';
                   8744:     }
1.258     albertel 8745:     if ($env{'request.role'}=~/^(su|dc|ad|li)/) {
1.182     matthew  8746:         $function='admin';
                   8747:     }
1.826     bisitz   8748:     if (($env{'request.role'}=~/^(au|ca|aa)/) ||
1.1025    raeburn  8749:         ($ENV{'REQUEST_URI'}=~ m{/^(/priv)})) {
1.182     matthew  8750:         $function='author';
                   8751:     }
                   8752:     return $function;
1.54      www      8753: }
1.99      www      8754: 
                   8755: ###############################################
                   8756: 
1.233     raeburn  8757: =pod
                   8758: 
1.821     raeburn  8759: =item * &show_course()
                   8760: 
                   8761: Used by lonmenu.pm and lonroles.pm to determine whether to use the word
                   8762: 'Courses' or 'Roles' in inline navigation and on screen displaying user's roles.
                   8763: 
                   8764: Inputs:
                   8765: None
                   8766: 
                   8767: Outputs:
                   8768: Scalar: 1 if 'Course' to be used, 0 otherwise.
                   8769: 
                   8770: =cut
                   8771: 
                   8772: ###############################################
                   8773: sub show_course {
                   8774:     my $course = !$env{'user.adv'};
                   8775:     if (!$env{'user.adv'}) {
                   8776:         foreach my $env (keys(%env)) {
                   8777:             next if ($env !~ m/^user\.priv\./);
                   8778:             if ($env !~ m/^user\.priv\.(?:st|cm)/) {
                   8779:                 $course = 0;
                   8780:                 last;
                   8781:             }
                   8782:         }
                   8783:     }
                   8784:     return $course;
                   8785: }
                   8786: 
                   8787: ###############################################
                   8788: 
                   8789: =pod
                   8790: 
1.542     raeburn  8791: =item * &check_user_status()
1.274     raeburn  8792: 
                   8793: Determines current status of supplied role for a
                   8794: specific user. Roles can be active, previous or future.
                   8795: 
                   8796: Inputs: 
                   8797: user's domain, user's username, course's domain,
1.375     raeburn  8798: course's number, optional section ID.
1.274     raeburn  8799: 
                   8800: Outputs:
                   8801: role status: active, previous or future. 
                   8802: 
                   8803: =cut
                   8804: 
                   8805: sub check_user_status {
1.412     raeburn  8806:     my ($udom,$uname,$cdom,$crs,$role,$sec) = @_;
1.1073    raeburn  8807:     my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
1.1202    raeburn  8808:     my @uroles = keys(%userinfo);
1.274     raeburn  8809:     my $srchstr;
                   8810:     my $active_chk = 'none';
1.412     raeburn  8811:     my $now = time;
1.274     raeburn  8812:     if (@uroles > 0) {
1.908     raeburn  8813:         if (($role eq 'cc') || ($role eq 'co') || ($sec eq '') || (!defined($sec))) {
1.274     raeburn  8814:             $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
                   8815:         } else {
1.412     raeburn  8816:             $srchstr = '/'.$cdom.'/'.$crs.'/'.$sec.'_'.$role;
                   8817:         }
                   8818:         if (grep/^\Q$srchstr\E$/,@uroles) {
1.274     raeburn  8819:             my $role_end = 0;
                   8820:             my $role_start = 0;
                   8821:             $active_chk = 'active';
1.412     raeburn  8822:             if ($userinfo{$srchstr} =~ m/^\Q$role\E_(\d+)/) {
                   8823:                 $role_end = $1;
                   8824:                 if ($userinfo{$srchstr} =~ m/^\Q$role\E_\Q$role_end\E_(\d+)$/) {
                   8825:                     $role_start = $1;
1.274     raeburn  8826:                 }
                   8827:             }
                   8828:             if ($role_start > 0) {
1.412     raeburn  8829:                 if ($now < $role_start) {
1.274     raeburn  8830:                     $active_chk = 'future';
                   8831:                 }
                   8832:             }
                   8833:             if ($role_end > 0) {
1.412     raeburn  8834:                 if ($now > $role_end) {
1.274     raeburn  8835:                     $active_chk = 'previous';
                   8836:                 }
                   8837:             }
                   8838:         }
                   8839:     }
                   8840:     return $active_chk;
                   8841: }
                   8842: 
                   8843: ###############################################
                   8844: 
                   8845: =pod
                   8846: 
1.405     albertel 8847: =item * &get_sections()
1.233     raeburn  8848: 
                   8849: Determines all the sections for a course including
                   8850: sections with students and sections containing other roles.
1.419     raeburn  8851: Incoming parameters: 
                   8852: 
                   8853: 1. domain
                   8854: 2. course number 
                   8855: 3. reference to array containing roles for which sections should 
                   8856: be gathered (optional).
                   8857: 4. reference to array containing status types for which sections 
                   8858: should be gathered (optional).
                   8859: 
                   8860: If the third argument is undefined, sections are gathered for any role. 
                   8861: If the fourth argument is undefined, sections are gathered for any status.
                   8862: Permissible values are 'active' or 'future' or 'previous'.
1.233     raeburn  8863:  
1.374     raeburn  8864: Returns section hash (keys are section IDs, values are
                   8865: number of users in each section), subject to the
1.419     raeburn  8866: optional roles filter, optional status filter 
1.233     raeburn  8867: 
                   8868: =cut
                   8869: 
                   8870: ###############################################
                   8871: sub get_sections {
1.419     raeburn  8872:     my ($cdom,$cnum,$possible_roles,$possible_status) = @_;
1.366     albertel 8873:     if (!defined($cdom) || !defined($cnum)) {
                   8874:         my $cid =  $env{'request.course.id'};
                   8875: 
                   8876: 	return if (!defined($cid));
                   8877: 
                   8878:         $cdom = $env{'course.'.$cid.'.domain'};
                   8879:         $cnum = $env{'course.'.$cid.'.num'};
                   8880:     }
                   8881: 
                   8882:     my %sectioncount;
1.419     raeburn  8883:     my $now = time;
1.240     albertel 8884: 
1.1118    raeburn  8885:     my $check_students = 1;
                   8886:     my $only_students = 0;
                   8887:     if (ref($possible_roles) eq 'ARRAY') {
                   8888:         if (grep(/^st$/,@{$possible_roles})) {
                   8889:             if (@{$possible_roles} == 1) {
                   8890:                 $only_students = 1;
                   8891:             }
                   8892:         } else {
                   8893:             $check_students = 0;
                   8894:         }
                   8895:     }
                   8896: 
                   8897:     if ($check_students) { 
1.276     albertel 8898: 	my ($classlist) = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1.240     albertel 8899: 	my $sec_index = &Apache::loncoursedata::CL_SECTION();
                   8900: 	my $status_index = &Apache::loncoursedata::CL_STATUS();
1.419     raeburn  8901:         my $start_index = &Apache::loncoursedata::CL_START();
                   8902:         my $end_index = &Apache::loncoursedata::CL_END();
                   8903:         my $status;
1.366     albertel 8904: 	while (my ($student,$data) = each(%$classlist)) {
1.419     raeburn  8905: 	    my ($section,$stu_status,$start,$end) = ($data->[$sec_index],
                   8906: 				                     $data->[$status_index],
                   8907:                                                      $data->[$start_index],
                   8908:                                                      $data->[$end_index]);
                   8909:             if ($stu_status eq 'Active') {
                   8910:                 $status = 'active';
                   8911:             } elsif ($end < $now) {
                   8912:                 $status = 'previous';
                   8913:             } elsif ($start > $now) {
                   8914:                 $status = 'future';
                   8915:             } 
                   8916: 	    if ($section ne '-1' && $section !~ /^\s*$/) {
                   8917:                 if ((!defined($possible_status)) || (($status ne '') && 
                   8918:                     (grep/^\Q$status\E$/,@{$possible_status}))) { 
                   8919: 		    $sectioncount{$section}++;
                   8920:                 }
1.240     albertel 8921: 	    }
                   8922: 	}
                   8923:     }
1.1118    raeburn  8924:     if ($only_students) {
                   8925:         return %sectioncount;
                   8926:     }
1.240     albertel 8927:     my %courseroles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   8928:     foreach my $user (sort(keys(%courseroles))) {
                   8929: 	if ($user !~ /^(\w{2})/) { next; }
                   8930: 	my ($role) = ($user =~ /^(\w{2})/);
                   8931: 	if ($possible_roles && !(grep(/^$role$/,@$possible_roles))) { next; }
1.419     raeburn  8932: 	my ($section,$status);
1.240     albertel 8933: 	if ($role eq 'cr' &&
                   8934: 	    $user =~ m-^$role/[^/]*/[^/]*/[^/]*:[^:]*:[^:]*:(\w+)-) {
                   8935: 	    $section=$1;
                   8936: 	}
                   8937: 	if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
                   8938: 	if (!defined($section) || $section eq '-1') { next; }
1.419     raeburn  8939:         my ($end,$start) = ($courseroles{$user} =~ /^([^:]*):([^:]*)$/);
                   8940:         if ($end == -1 && $start == -1) {
                   8941:             next; #deleted role
                   8942:         }
                   8943:         if (!defined($possible_status)) { 
                   8944:             $sectioncount{$section}++;
                   8945:         } else {
                   8946:             if ((!$end || $end >= $now) && (!$start || $start <= $now)) {
                   8947:                 $status = 'active';
                   8948:             } elsif ($end < $now) {
                   8949:                 $status = 'future';
                   8950:             } elsif ($start > $now) {
                   8951:                 $status = 'previous';
                   8952:             }
                   8953:             if (($status ne '') && (grep/^\Q$status\E$/,@{$possible_status})) {
                   8954:                 $sectioncount{$section}++;
                   8955:             }
                   8956:         }
1.233     raeburn  8957:     }
1.366     albertel 8958:     return %sectioncount;
1.233     raeburn  8959: }
                   8960: 
1.274     raeburn  8961: ###############################################
1.294     raeburn  8962: 
                   8963: =pod
1.405     albertel 8964: 
                   8965: =item * &get_course_users()
                   8966: 
1.275     raeburn  8967: Retrieves usernames:domains for users in the specified course
                   8968: with specific role(s), and access status. 
                   8969: 
                   8970: Incoming parameters:
1.277     albertel 8971: 1. course domain
                   8972: 2. course number
                   8973: 3. access status: users must have - either active, 
1.275     raeburn  8974: previous, future, or all.
1.277     albertel 8975: 4. reference to array of permissible roles
1.288     raeburn  8976: 5. reference to array of section restrictions (optional)
                   8977: 6. reference to results object (hash of hashes).
                   8978: 7. reference to optional userdata hash
1.609     raeburn  8979: 8. reference to optional statushash
1.630     raeburn  8980: 9. flag if privileged users (except those set to unhide in
                   8981:    course settings) should be excluded    
1.609     raeburn  8982: Keys of top level results hash are roles.
1.275     raeburn  8983: Keys of inner hashes are username:domain, with 
                   8984: values set to access type.
1.288     raeburn  8985: Optional userdata hash returns an array with arguments in the 
                   8986: same order as loncoursedata::get_classlist() for student data.
                   8987: 
1.609     raeburn  8988: Optional statushash returns
                   8989: 
1.288     raeburn  8990: Entries for end, start, section and status are blank because
                   8991: of the possibility of multiple values for non-student roles.
                   8992: 
1.275     raeburn  8993: =cut
1.405     albertel 8994: 
1.275     raeburn  8995: ###############################################
1.405     albertel 8996: 
1.275     raeburn  8997: sub get_course_users {
1.630     raeburn  8998:     my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata,$statushash,$hidepriv) = @_;
1.288     raeburn  8999:     my %idx = ();
1.419     raeburn  9000:     my %seclists;
1.288     raeburn  9001: 
                   9002:     $idx{udom} = &Apache::loncoursedata::CL_SDOM();
                   9003:     $idx{uname} =  &Apache::loncoursedata::CL_SNAME();
                   9004:     $idx{end} = &Apache::loncoursedata::CL_END();
                   9005:     $idx{start} = &Apache::loncoursedata::CL_START();
                   9006:     $idx{id} = &Apache::loncoursedata::CL_ID();
                   9007:     $idx{section} = &Apache::loncoursedata::CL_SECTION();
                   9008:     $idx{fullname} = &Apache::loncoursedata::CL_FULLNAME();
                   9009:     $idx{status} = &Apache::loncoursedata::CL_STATUS();
                   9010: 
1.290     albertel 9011:     if (grep(/^st$/,@{$roles})) {
1.276     albertel 9012:         my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist($cdom,$cnum);
1.278     raeburn  9013:         my $now = time;
1.277     albertel 9014:         foreach my $student (keys(%{$classlist})) {
1.288     raeburn  9015:             my $match = 0;
1.412     raeburn  9016:             my $secmatch = 0;
1.419     raeburn  9017:             my $section = $$classlist{$student}[$idx{section}];
1.609     raeburn  9018:             my $status = $$classlist{$student}[$idx{status}];
1.419     raeburn  9019:             if ($section eq '') {
                   9020:                 $section = 'none';
                   9021:             }
1.291     albertel 9022:             if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 9023:                 if (grep(/^all$/,@{$sections})) {
1.412     raeburn  9024:                     $secmatch = 1;
                   9025:                 } elsif ($$classlist{$student}[$idx{section}] eq '') {
1.420     albertel 9026:                     if (grep(/^none$/,@{$sections})) {
1.412     raeburn  9027:                         $secmatch = 1;
                   9028:                     }
                   9029:                 } else {  
1.419     raeburn  9030: 		    if (grep(/^\Q$section\E$/,@{$sections})) {
1.412     raeburn  9031: 		        $secmatch = 1;
                   9032:                     }
1.290     albertel 9033: 		}
1.412     raeburn  9034:                 if (!$secmatch) {
                   9035:                     next;
                   9036:                 }
1.419     raeburn  9037:             }
1.275     raeburn  9038:             if (defined($$types{'active'})) {
1.288     raeburn  9039:                 if ($$classlist{$student}[$idx{status}] eq 'Active') {
1.275     raeburn  9040:                     push(@{$$users{st}{$student}},'active');
1.288     raeburn  9041:                     $match = 1;
1.275     raeburn  9042:                 }
                   9043:             }
                   9044:             if (defined($$types{'previous'})) {
1.609     raeburn  9045:                 if ($$classlist{$student}[$idx{status}] eq 'Expired') {
1.275     raeburn  9046:                     push(@{$$users{st}{$student}},'previous');
1.288     raeburn  9047:                     $match = 1;
1.275     raeburn  9048:                 }
                   9049:             }
                   9050:             if (defined($$types{'future'})) {
1.609     raeburn  9051:                 if ($$classlist{$student}[$idx{status}] eq 'Future') {
1.275     raeburn  9052:                     push(@{$$users{st}{$student}},'future');
1.288     raeburn  9053:                     $match = 1;
1.275     raeburn  9054:                 }
                   9055:             }
1.609     raeburn  9056:             if ($match) {
                   9057:                 push(@{$seclists{$student}},$section);
                   9058:                 if (ref($userdata) eq 'HASH') {
                   9059:                     $$userdata{$student} = $$classlist{$student};
                   9060:                 }
                   9061:                 if (ref($statushash) eq 'HASH') {
                   9062:                     $statushash->{$student}{'st'}{$section} = $status;
                   9063:                 }
1.288     raeburn  9064:             }
1.275     raeburn  9065:         }
                   9066:     }
1.412     raeburn  9067:     if ((@{$roles} > 1) || ((@{$roles} == 1) && ($$roles[0] ne "st"))) {
1.439     raeburn  9068:         my %coursepersonnel = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   9069:         my $now = time;
1.609     raeburn  9070:         my %displaystatus = ( previous => 'Expired',
                   9071:                               active   => 'Active',
                   9072:                               future   => 'Future',
                   9073:                             );
1.1121    raeburn  9074:         my (%nothide,@possdoms);
1.630     raeburn  9075:         if ($hidepriv) {
                   9076:             my %coursehash=&Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                   9077:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   9078:                 if ($user !~ /:/) {
                   9079:                     $nothide{join(':',split(/[\@]/,$user))}=1;
                   9080:                 } else {
                   9081:                     $nothide{$user} = 1;
                   9082:                 }
                   9083:             }
1.1121    raeburn  9084:             my @possdoms = ($cdom);
                   9085:             if ($coursehash{'checkforpriv'}) {
                   9086:                 push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
                   9087:             }
1.630     raeburn  9088:         }
1.439     raeburn  9089:         foreach my $person (sort(keys(%coursepersonnel))) {
1.288     raeburn  9090:             my $match = 0;
1.412     raeburn  9091:             my $secmatch = 0;
1.439     raeburn  9092:             my $status;
1.412     raeburn  9093:             my ($role,$user,$usec) = ($person =~ /^([^:]*):([^:]+:[^:]+):([^:]*)/);
1.275     raeburn  9094:             $user =~ s/:$//;
1.439     raeburn  9095:             my ($end,$start) = split(/:/,$coursepersonnel{$person});
                   9096:             if ($end == -1 || $start == -1) {
                   9097:                 next;
                   9098:             }
                   9099:             if (($role) && ((grep(/^\Q$role\E$/,@{$roles})) ||
                   9100:                 (grep(/^cr$/,@{$roles}) && $role =~ /^cr\//))) {
1.412     raeburn  9101:                 my ($uname,$udom) = split(/:/,$user);
                   9102:                 if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 9103:                     if (grep(/^all$/,@{$sections})) {
1.412     raeburn  9104:                         $secmatch = 1;
                   9105:                     } elsif ($usec eq '') {
1.420     albertel 9106:                         if (grep(/^none$/,@{$sections})) {
1.412     raeburn  9107:                             $secmatch = 1;
                   9108:                         }
                   9109:                     } else {
                   9110:                         if (grep(/^\Q$usec\E$/,@{$sections})) {
                   9111:                             $secmatch = 1;
                   9112:                         }
                   9113:                     }
                   9114:                     if (!$secmatch) {
                   9115:                         next;
                   9116:                     }
1.288     raeburn  9117:                 }
1.419     raeburn  9118:                 if ($usec eq '') {
                   9119:                     $usec = 'none';
                   9120:                 }
1.275     raeburn  9121:                 if ($uname ne '' && $udom ne '') {
1.630     raeburn  9122:                     if ($hidepriv) {
1.1121    raeburn  9123:                         if ((&Apache::lonnet::privileged($uname,$udom,\@possdoms)) &&
1.630     raeburn  9124:                             (!$nothide{$uname.':'.$udom})) {
                   9125:                             next;
                   9126:                         }
                   9127:                     }
1.503     raeburn  9128:                     if ($end > 0 && $end < $now) {
1.439     raeburn  9129:                         $status = 'previous';
                   9130:                     } elsif ($start > $now) {
                   9131:                         $status = 'future';
                   9132:                     } else {
                   9133:                         $status = 'active';
                   9134:                     }
1.277     albertel 9135:                     foreach my $type (keys(%{$types})) { 
1.275     raeburn  9136:                         if ($status eq $type) {
1.420     albertel 9137:                             if (!grep(/^\Q$type\E$/,@{$$users{$role}{$user}})) {
1.419     raeburn  9138:                                 push(@{$$users{$role}{$user}},$type);
                   9139:                             }
1.288     raeburn  9140:                             $match = 1;
                   9141:                         }
                   9142:                     }
1.419     raeburn  9143:                     if (($match) && (ref($userdata) eq 'HASH')) {
                   9144:                         if (!exists($$userdata{$uname.':'.$udom})) {
                   9145: 			    &get_user_info($udom,$uname,\%idx,$userdata);
                   9146:                         }
1.420     albertel 9147:                         if (!grep(/^\Q$usec\E$/,@{$seclists{$uname.':'.$udom}})) {
1.419     raeburn  9148:                             push(@{$seclists{$uname.':'.$udom}},$usec);
                   9149:                         }
1.609     raeburn  9150:                         if (ref($statushash) eq 'HASH') {
                   9151:                             $statushash->{$uname.':'.$udom}{$role}{$usec} = $displaystatus{$status};
                   9152:                         }
1.275     raeburn  9153:                     }
                   9154:                 }
                   9155:             }
                   9156:         }
1.290     albertel 9157:         if (grep(/^ow$/,@{$roles})) {
1.279     raeburn  9158:             if ((defined($cdom)) && (defined($cnum))) {
                   9159:                 my %csettings = &Apache::lonnet::get('environment',['internal.courseowner'],$cdom,$cnum);
                   9160:                 if ( defined($csettings{'internal.courseowner'}) ) {
                   9161:                     my $owner = $csettings{'internal.courseowner'};
1.609     raeburn  9162:                     next if ($owner eq '');
                   9163:                     my ($ownername,$ownerdom);
                   9164:                     if ($owner =~ /^([^:]+):([^:]+)$/) {
                   9165:                         $ownername = $1;
                   9166:                         $ownerdom = $2;
                   9167:                     } else {
                   9168:                         $ownername = $owner;
                   9169:                         $ownerdom = $cdom;
                   9170:                         $owner = $ownername.':'.$ownerdom;
1.439     raeburn  9171:                     }
                   9172:                     @{$$users{'ow'}{$owner}} = 'any';
1.290     albertel 9173:                     if (defined($userdata) && 
1.609     raeburn  9174: 			!exists($$userdata{$owner})) {
                   9175: 			&get_user_info($ownerdom,$ownername,\%idx,$userdata);
                   9176:                         if (!grep(/^none$/,@{$seclists{$owner}})) {
                   9177:                             push(@{$seclists{$owner}},'none');
                   9178:                         }
                   9179:                         if (ref($statushash) eq 'HASH') {
                   9180:                             $statushash->{$owner}{'ow'}{'none'} = 'Any';
1.419     raeburn  9181:                         }
1.290     albertel 9182: 		    }
1.279     raeburn  9183:                 }
                   9184:             }
                   9185:         }
1.419     raeburn  9186:         foreach my $user (keys(%seclists)) {
                   9187:             @{$seclists{$user}} = (sort {$a <=> $b} @{$seclists{$user}});
                   9188:             $$userdata{$user}[$idx{section}] = join(',',@{$seclists{$user}});
                   9189:         }
1.275     raeburn  9190:     }
                   9191:     return;
                   9192: }
                   9193: 
1.288     raeburn  9194: sub get_user_info {
                   9195:     my ($udom,$uname,$idx,$userdata) = @_;
1.289     albertel 9196:     $$userdata{$uname.':'.$udom}[$$idx{fullname}] = 
                   9197: 	&plainname($uname,$udom,'lastname');
1.291     albertel 9198:     $$userdata{$uname.':'.$udom}[$$idx{uname}] = $uname;
1.297     raeburn  9199:     $$userdata{$uname.':'.$udom}[$$idx{udom}] = $udom;
1.609     raeburn  9200:     my %idhash =  &Apache::lonnet::idrget($udom,($uname));
                   9201:     $$userdata{$uname.':'.$udom}[$$idx{id}] = $idhash{$uname}; 
1.288     raeburn  9202:     return;
                   9203: }
1.275     raeburn  9204: 
1.472     raeburn  9205: ###############################################
                   9206: 
                   9207: =pod
                   9208: 
                   9209: =item * &get_user_quota()
                   9210: 
1.1134    raeburn  9211: Retrieves quota assigned for storage of user files.
                   9212: Default is to report quota for portfolio files.
1.472     raeburn  9213: 
                   9214: Incoming parameters:
                   9215: 1. user's username
                   9216: 2. user's domain
1.1134    raeburn  9217: 3. quota name - portfolio, author, or course
1.1136    raeburn  9218:    (if no quota name provided, defaults to portfolio).
1.1165    raeburn  9219: 4. crstype - official, unofficial, textbook or community, if quota name is
1.1136    raeburn  9220:    course
1.472     raeburn  9221: 
                   9222: Returns:
1.1163    raeburn  9223: 1. Disk quota (in MB) assigned to student.
1.536     raeburn  9224: 2. (Optional) Type of setting: custom or default
                   9225:    (individually assigned or default for user's 
                   9226:    institutional status).
                   9227: 3. (Optional) - User's institutional status (e.g., faculty, staff
                   9228:    or student - types as defined in localenroll::inst_usertypes 
                   9229:    for user's domain, which determines default quota for user.
                   9230: 4. (Optional) - Default quota which would apply to the user.
1.472     raeburn  9231: 
                   9232: If a value has been stored in the user's environment, 
1.536     raeburn  9233: it will return that, otherwise it returns the maximal default
1.1134    raeburn  9234: defined for the user's institutional status(es) in the domain.
1.472     raeburn  9235: 
                   9236: =cut
                   9237: 
                   9238: ###############################################
                   9239: 
                   9240: 
                   9241: sub get_user_quota {
1.1136    raeburn  9242:     my ($uname,$udom,$quotaname,$crstype) = @_;
1.536     raeburn  9243:     my ($quota,$quotatype,$settingstatus,$defquota);
1.472     raeburn  9244:     if (!defined($udom)) {
                   9245:         $udom = $env{'user.domain'};
                   9246:     }
                   9247:     if (!defined($uname)) {
                   9248:         $uname = $env{'user.name'};
                   9249:     }
                   9250:     if (($udom eq '' || $uname eq '') ||
                   9251:         ($udom eq 'public') && ($uname eq 'public')) {
                   9252:         $quota = 0;
1.536     raeburn  9253:         $quotatype = 'default';
                   9254:         $defquota = 0; 
1.472     raeburn  9255:     } else {
1.536     raeburn  9256:         my $inststatus;
1.1134    raeburn  9257:         if ($quotaname eq 'course') {
                   9258:             if (($env{'course.'.$udom.'_'.$uname.'.num'} eq $uname) &&
                   9259:                 ($env{'course.'.$udom.'_'.$uname.'.domain'} eq $udom)) {
                   9260:                 $quota = $env{'course.'.$udom.'_'.$uname.'.internal.uploadquota'};
                   9261:             } else {
                   9262:                 my %cenv = &Apache::lonnet::coursedescription("$udom/$uname");
                   9263:                 $quota = $cenv{'internal.uploadquota'};
                   9264:             }
1.536     raeburn  9265:         } else {
1.1134    raeburn  9266:             if ($udom eq $env{'user.domain'} && $uname eq $env{'user.name'}) {
                   9267:                 if ($quotaname eq 'author') {
                   9268:                     $quota = $env{'environment.authorquota'};
                   9269:                 } else {
                   9270:                     $quota = $env{'environment.portfolioquota'};
                   9271:                 }
                   9272:                 $inststatus = $env{'environment.inststatus'};
                   9273:             } else {
                   9274:                 my %userenv = 
                   9275:                     &Apache::lonnet::get('environment',['portfolioquota',
                   9276:                                          'authorquota','inststatus'],$udom,$uname);
                   9277:                 my ($tmp) = keys(%userenv);
                   9278:                 if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   9279:                     if ($quotaname eq 'author') {
                   9280:                         $quota = $userenv{'authorquota'};
                   9281:                     } else {
                   9282:                         $quota = $userenv{'portfolioquota'};
                   9283:                     }
                   9284:                     $inststatus = $userenv{'inststatus'};
                   9285:                 } else {
                   9286:                     undef(%userenv);
                   9287:                 }
                   9288:             }
                   9289:         }
                   9290:         if ($quota eq '' || wantarray) {
                   9291:             if ($quotaname eq 'course') {
                   9292:                 my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
1.1165    raeburn  9293:                 if (($crstype eq 'official') || ($crstype eq 'unofficial') || 
                   9294:                     ($crstype eq 'community') || ($crstype eq 'textbook')) { 
1.1136    raeburn  9295:                     $defquota = $domdefs{$crstype.'quota'};
                   9296:                 }
                   9297:                 if ($defquota eq '') {
                   9298:                     $defquota = 500;
                   9299:                 }
1.1134    raeburn  9300:             } else {
                   9301:                 ($defquota,$settingstatus) = &default_quota($udom,$inststatus,$quotaname);
                   9302:             }
                   9303:             if ($quota eq '') {
                   9304:                 $quota = $defquota;
                   9305:                 $quotatype = 'default';
                   9306:             } else {
                   9307:                 $quotatype = 'custom';
                   9308:             }
1.472     raeburn  9309:         }
                   9310:     }
1.536     raeburn  9311:     if (wantarray) {
                   9312:         return ($quota,$quotatype,$settingstatus,$defquota);
                   9313:     } else {
                   9314:         return $quota;
                   9315:     }
1.472     raeburn  9316: }
                   9317: 
                   9318: ###############################################
                   9319: 
                   9320: =pod
                   9321: 
                   9322: =item * &default_quota()
                   9323: 
1.536     raeburn  9324: Retrieves default quota assigned for storage of user portfolio files,
                   9325: given an (optional) user's institutional status.
1.472     raeburn  9326: 
                   9327: Incoming parameters:
1.1142    raeburn  9328: 
1.472     raeburn  9329: 1. domain
1.536     raeburn  9330: 2. (Optional) institutional status(es).  This is a : separated list of 
                   9331:    status types (e.g., faculty, staff, student etc.)
                   9332:    which apply to the user for whom the default is being retrieved.
                   9333:    If the institutional status string in undefined, the domain
1.1134    raeburn  9334:    default quota will be returned.
                   9335: 3.  quota name - portfolio, author, or course
                   9336:    (if no quota name provided, defaults to portfolio).
1.472     raeburn  9337: 
                   9338: Returns:
1.1142    raeburn  9339: 
1.1163    raeburn  9340: 1. Default disk quota (in MB) for user portfolios in the domain.
1.536     raeburn  9341: 2. (Optional) institutional type which determined the value of the
                   9342:    default quota.
1.472     raeburn  9343: 
                   9344: If a value has been stored in the domain's configuration db,
                   9345: it will return that, otherwise it returns 20 (for backwards 
                   9346: compatibility with domains which have not set up a configuration
1.1163    raeburn  9347: db file; the original statically defined portfolio quota was 20 MB). 
1.472     raeburn  9348: 
1.536     raeburn  9349: If the user's status includes multiple types (e.g., staff and student),
                   9350: the largest default quota which applies to the user determines the
                   9351: default quota returned.
                   9352: 
1.472     raeburn  9353: =cut
                   9354: 
                   9355: ###############################################
                   9356: 
                   9357: 
                   9358: sub default_quota {
1.1134    raeburn  9359:     my ($udom,$inststatus,$quotaname) = @_;
1.536     raeburn  9360:     my ($defquota,$settingstatus);
                   9361:     my %quotahash = &Apache::lonnet::get_dom('configuration',
1.622     raeburn  9362:                                             ['quotas'],$udom);
1.1134    raeburn  9363:     my $key = 'defaultquota';
                   9364:     if ($quotaname eq 'author') {
                   9365:         $key = 'authorquota';
                   9366:     }
1.622     raeburn  9367:     if (ref($quotahash{'quotas'}) eq 'HASH') {
1.536     raeburn  9368:         if ($inststatus ne '') {
1.765     raeburn  9369:             my @statuses = map { &unescape($_); } split(/:/,$inststatus);
1.536     raeburn  9370:             foreach my $item (@statuses) {
1.1134    raeburn  9371:                 if (ref($quotahash{'quotas'}{$key}) eq 'HASH') {
                   9372:                     if ($quotahash{'quotas'}{$key}{$item} ne '') {
1.711     raeburn  9373:                         if ($defquota eq '') {
1.1134    raeburn  9374:                             $defquota = $quotahash{'quotas'}{$key}{$item};
1.711     raeburn  9375:                             $settingstatus = $item;
1.1134    raeburn  9376:                         } elsif ($quotahash{'quotas'}{$key}{$item} > $defquota) {
                   9377:                             $defquota = $quotahash{'quotas'}{$key}{$item};
1.711     raeburn  9378:                             $settingstatus = $item;
                   9379:                         }
                   9380:                     }
1.1134    raeburn  9381:                 } elsif ($key eq 'defaultquota') {
1.711     raeburn  9382:                     if ($quotahash{'quotas'}{$item} ne '') {
                   9383:                         if ($defquota eq '') {
                   9384:                             $defquota = $quotahash{'quotas'}{$item};
                   9385:                             $settingstatus = $item;
                   9386:                         } elsif ($quotahash{'quotas'}{$item} > $defquota) {
                   9387:                             $defquota = $quotahash{'quotas'}{$item};
                   9388:                             $settingstatus = $item;
                   9389:                         }
1.536     raeburn  9390:                     }
                   9391:                 }
                   9392:             }
                   9393:         }
                   9394:         if ($defquota eq '') {
1.1134    raeburn  9395:             if (ref($quotahash{'quotas'}{$key}) eq 'HASH') {
                   9396:                 $defquota = $quotahash{'quotas'}{$key}{'default'};
                   9397:             } elsif ($key eq 'defaultquota') {
1.711     raeburn  9398:                 $defquota = $quotahash{'quotas'}{'default'};
                   9399:             }
1.536     raeburn  9400:             $settingstatus = 'default';
1.1139    raeburn  9401:             if ($defquota eq '') {
                   9402:                 if ($quotaname eq 'author') {
                   9403:                     $defquota = 500;
                   9404:                 }
                   9405:             }
1.536     raeburn  9406:         }
                   9407:     } else {
                   9408:         $settingstatus = 'default';
1.1134    raeburn  9409:         if ($quotaname eq 'author') {
                   9410:             $defquota = 500;
                   9411:         } else {
                   9412:             $defquota = 20;
                   9413:         }
1.536     raeburn  9414:     }
                   9415:     if (wantarray) {
                   9416:         return ($defquota,$settingstatus);
1.472     raeburn  9417:     } else {
1.536     raeburn  9418:         return $defquota;
1.472     raeburn  9419:     }
                   9420: }
                   9421: 
1.1135    raeburn  9422: ###############################################
                   9423: 
                   9424: =pod
                   9425: 
1.1136    raeburn  9426: =item * &excess_filesize_warning()
1.1135    raeburn  9427: 
                   9428: Returns warning message if upload of file to authoring space, or copying
1.1136    raeburn  9429: of existing file within authoring space will cause quota for the authoring
1.1146    raeburn  9430: space to be exceeded.
1.1136    raeburn  9431: 
                   9432: Same, if upload of a file directly to a course/community via Course Editor
1.1137    raeburn  9433: will cause quota for uploaded content for the course to be exceeded.
1.1135    raeburn  9434: 
1.1165    raeburn  9435: Inputs: 7 
1.1136    raeburn  9436: 1. username or coursenum
1.1135    raeburn  9437: 2. domain
1.1136    raeburn  9438: 3. context ('author' or 'course')
1.1135    raeburn  9439: 4. filename of file for which action is being requested
                   9440: 5. filesize (kB) of file
                   9441: 6. action being taken: copy or upload.
1.1165    raeburn  9442: 7. quotatype (in course context -- official, unofficial, community or textbook).
1.1135    raeburn  9443: 
                   9444: Returns: 1 scalar: HTML to display containing warning if quota would be exceeded,
1.1142    raeburn  9445:          otherwise return null.
                   9446: 
                   9447: =back
1.1135    raeburn  9448: 
                   9449: =cut
                   9450: 
1.1136    raeburn  9451: sub excess_filesize_warning {
1.1165    raeburn  9452:     my ($uname,$udom,$context,$filename,$filesize,$action,$quotatype) = @_;
1.1136    raeburn  9453:     my $current_disk_usage = 0;
1.1165    raeburn  9454:     my $disk_quota = &get_user_quota($uname,$udom,$context,$quotatype); #expressed in MB
1.1136    raeburn  9455:     if ($context eq 'author') {
                   9456:         my $authorspace = $Apache::lonnet::perlvar{'lonDocRoot'}."/priv/$udom/$uname";
                   9457:         $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$authorspace);
                   9458:     } else {
                   9459:         foreach my $subdir ('docs','supplemental') {
                   9460:             $current_disk_usage += &Apache::lonnet::diskusage($udom,$uname,"userfiles/$subdir",1);
                   9461:         }
                   9462:     }
1.1135    raeburn  9463:     $disk_quota = int($disk_quota * 1000);
                   9464:     if (($current_disk_usage + $filesize) > $disk_quota) {
1.1179    bisitz   9465:         return '<p class="LC_warning">'.
1.1135    raeburn  9466:                 &mt("Unable to $action [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.",
1.1179    bisitz   9467:                     '<span class="LC_filename">'.$filename.'</span>',$filesize).'</p>'.
                   9468:                '<p>'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',
1.1135    raeburn  9469:                             $disk_quota,$current_disk_usage).
                   9470:                '</p>';
                   9471:     }
                   9472:     return;
                   9473: }
                   9474: 
                   9475: ###############################################
                   9476: 
                   9477: 
1.1136    raeburn  9478: 
                   9479: 
1.384     raeburn  9480: sub get_secgrprole_info {
                   9481:     my ($cdom,$cnum,$needroles,$type)  = @_;
                   9482:     my %sections_count = &get_sections($cdom,$cnum);
                   9483:     my @sections =  (sort {$a <=> $b} keys(%sections_count));
                   9484:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
                   9485:     my @groups = sort(keys(%curr_groups));
                   9486:     my $allroles = [];
                   9487:     my $rolehash;
                   9488:     my $accesshash = {
                   9489:                      active => 'Currently has access',
                   9490:                      future => 'Will have future access',
                   9491:                      previous => 'Previously had access',
                   9492:                   };
                   9493:     if ($needroles) {
                   9494:         $rolehash = {'all' => 'all'};
1.385     albertel 9495:         my %user_roles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   9496: 	if (&Apache::lonnet::error(%user_roles)) {
                   9497: 	    undef(%user_roles);
                   9498: 	}
                   9499:         foreach my $item (keys(%user_roles)) {
1.384     raeburn  9500:             my ($role)=split(/\:/,$item,2);
                   9501:             if ($role eq 'cr') { next; }
                   9502:             if ($role =~ /^cr/) {
                   9503:                 $$rolehash{$role} = (split('/',$role))[3];
                   9504:             } else {
                   9505:                 $$rolehash{$role} = &Apache::lonnet::plaintext($role,$type);
                   9506:             }
                   9507:         }
                   9508:         foreach my $key (sort(keys(%{$rolehash}))) {
                   9509:             push(@{$allroles},$key);
                   9510:         }
                   9511:         push (@{$allroles},'st');
                   9512:         $$rolehash{'st'} = &Apache::lonnet::plaintext('st',$type);
                   9513:     }
                   9514:     return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
                   9515: }
                   9516: 
1.555     raeburn  9517: sub user_picker {
1.994     raeburn  9518:     my ($dom,$srch,$forcenewuser,$caller,$cancreate,$usertype,$context) = @_;
1.555     raeburn  9519:     my $currdom = $dom;
                   9520:     my %curr_selected = (
                   9521:                         srchin => 'dom',
1.580     raeburn  9522:                         srchby => 'lastname',
1.555     raeburn  9523:                       );
                   9524:     my $srchterm;
1.625     raeburn  9525:     if ((ref($srch) eq 'HASH') && ($env{'form.origform'} ne 'crtusername')) {
1.555     raeburn  9526:         if ($srch->{'srchby'} ne '') {
                   9527:             $curr_selected{'srchby'} = $srch->{'srchby'};
                   9528:         }
                   9529:         if ($srch->{'srchin'} ne '') {
                   9530:             $curr_selected{'srchin'} = $srch->{'srchin'};
                   9531:         }
                   9532:         if ($srch->{'srchtype'} ne '') {
                   9533:             $curr_selected{'srchtype'} = $srch->{'srchtype'};
                   9534:         }
                   9535:         if ($srch->{'srchdomain'} ne '') {
                   9536:             $currdom = $srch->{'srchdomain'};
                   9537:         }
                   9538:         $srchterm = $srch->{'srchterm'};
                   9539:     }
1.1222  ! damieng  9540:     my %html_lt=&Apache::lonlocal::texthash(
1.573     raeburn  9541:                     'usr'       => 'Search criteria',
1.563     raeburn  9542:                     'doma'      => 'Domain/institution to search',
1.558     albertel 9543:                     'uname'     => 'username',
                   9544:                     'lastname'  => 'last name',
1.555     raeburn  9545:                     'lastfirst' => 'last name, first name',
1.558     albertel 9546:                     'crs'       => 'in this course',
1.576     raeburn  9547:                     'dom'       => 'in selected LON-CAPA domain', 
1.558     albertel 9548:                     'alc'       => 'all LON-CAPA',
1.573     raeburn  9549:                     'instd'     => 'in institutional directory for selected domain',
1.558     albertel 9550:                     'exact'     => 'is',
                   9551:                     'contains'  => 'contains',
1.569     raeburn  9552:                     'begins'    => 'begins with',
1.1222  ! damieng  9553:                                        );
        !          9554:     my %js_lt=&Apache::lonlocal::texthash(
1.571     raeburn  9555:                     'youm'      => "You must include some text to search for.",
                   9556:                     'thte'      => "The text you are searching for must contain at least two characters when using a 'begins' type search.",
                   9557:                     'thet'      => "The text you are searching for must contain at least three characters when using a 'contains' type search.",
                   9558:                     'yomc'      => "You must choose a domain when using an institutional directory search.",
                   9559:                     'ymcd'      => "You must choose a domain when using a domain search.",
                   9560:                     'whus'      => "When using searching by last,first you must include a comma as separator between last name and first name.",
                   9561:                     'whse'      => "When searching by last,first you must include at least one character in the first name.",
                   9562:                      'thfo'     => "The following need to be corrected before the search can be run:",
1.555     raeburn  9563:                                        );
1.1222  ! damieng  9564:     &html_escape(\%html_lt);
        !          9565:     &js_escape(\%js_lt);
1.563     raeburn  9566:     my $domform = &select_dom_form($currdom,'srchdomain',1,1);
                   9567:     my $srchinsel = ' <select name="srchin">';
1.555     raeburn  9568: 
                   9569:     my @srchins = ('crs','dom','alc','instd');
                   9570: 
                   9571:     foreach my $option (@srchins) {
                   9572:         # FIXME 'alc' option unavailable until 
                   9573:         #       loncreateuser::print_user_query_page()
                   9574:         #       has been completed.
                   9575:         next if ($option eq 'alc');
1.880     raeburn  9576:         next if (($option eq 'crs') && ($env{'form.form'} eq 'requestcrs'));  
1.555     raeburn  9577:         next if ($option eq 'crs' && !$env{'request.course.id'});
1.563     raeburn  9578:         if ($curr_selected{'srchin'} eq $option) {
                   9579:             $srchinsel .= ' 
1.1222  ! damieng  9580:    <option value="'.$option.'" selected="selected">'.$html_lt{$option}.'</option>';
1.563     raeburn  9581:         } else {
                   9582:             $srchinsel .= '
1.1222  ! damieng  9583:    <option value="'.$option.'">'.$html_lt{$option}.'</option>';
1.563     raeburn  9584:         }
1.555     raeburn  9585:     }
1.563     raeburn  9586:     $srchinsel .= "\n  </select>\n";
1.555     raeburn  9587: 
                   9588:     my $srchbysel =  ' <select name="srchby">';
1.580     raeburn  9589:     foreach my $option ('lastname','lastfirst','uname') {
1.555     raeburn  9590:         if ($curr_selected{'srchby'} eq $option) {
                   9591:             $srchbysel .= '
1.1222  ! damieng  9592:    <option value="'.$option.'" selected="selected">'.$html_lt{$option}.'</option>';
1.555     raeburn  9593:         } else {
                   9594:             $srchbysel .= '
1.1222  ! damieng  9595:    <option value="'.$option.'">'.$html_lt{$option}.'</option>';
1.555     raeburn  9596:          }
                   9597:     }
                   9598:     $srchbysel .= "\n  </select>\n";
                   9599: 
                   9600:     my $srchtypesel = ' <select name="srchtype">';
1.580     raeburn  9601:     foreach my $option ('begins','contains','exact') {
1.555     raeburn  9602:         if ($curr_selected{'srchtype'} eq $option) {
                   9603:             $srchtypesel .= '
1.1222  ! damieng  9604:    <option value="'.$option.'" selected="selected">'.$html_lt{$option}.'</option>';
1.555     raeburn  9605:         } else {
                   9606:             $srchtypesel .= '
1.1222  ! damieng  9607:    <option value="'.$option.'">'.$html_lt{$option}.'</option>';
1.555     raeburn  9608:         }
                   9609:     }
                   9610:     $srchtypesel .= "\n  </select>\n";
                   9611: 
1.558     albertel 9612:     my ($newuserscript,$new_user_create);
1.994     raeburn  9613:     my $context_dom = $env{'request.role.domain'};
                   9614:     if ($context eq 'requestcrs') {
                   9615:         if ($env{'form.coursedom'} ne '') { 
                   9616:             $context_dom = $env{'form.coursedom'};
                   9617:         }
                   9618:     }
1.556     raeburn  9619:     if ($forcenewuser) {
1.576     raeburn  9620:         if (ref($srch) eq 'HASH') {
1.994     raeburn  9621:             if ($srch->{'srchby'} eq 'uname' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchin'} eq 'dom' && $srch->{'srchdomain'} eq $context_dom) {
1.627     raeburn  9622:                 if ($cancreate) {
                   9623:                     $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>';
                   9624:                 } else {
1.799     bisitz   9625:                     my $helplink = 'javascript:helpMenu('."'display'".')';
1.627     raeburn  9626:                     my %usertypetext = (
                   9627:                         official   => 'institutional',
                   9628:                         unofficial => 'non-institutional',
                   9629:                     );
1.799     bisitz   9630:                     $new_user_create = '<p class="LC_warning">'
                   9631:                                       .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
                   9632:                                       .' '
                   9633:                                       .&mt('Please contact the [_1]helpdesk[_2] for assistance.'
                   9634:                                           ,'<a href="'.$helplink.'">','</a>')
                   9635:                                       .'</p><br />';
1.627     raeburn  9636:                 }
1.576     raeburn  9637:             }
                   9638:         }
                   9639: 
1.556     raeburn  9640:         $newuserscript = <<"ENDSCRIPT";
                   9641: 
1.570     raeburn  9642: function setSearch(createnew,callingForm) {
1.556     raeburn  9643:     if (createnew == 1) {
1.570     raeburn  9644:         for (var i=0; i<callingForm.srchby.length; i++) {
                   9645:             if (callingForm.srchby.options[i].value == 'uname') {
                   9646:                 callingForm.srchby.selectedIndex = i;
1.556     raeburn  9647:             }
                   9648:         }
1.570     raeburn  9649:         for (var i=0; i<callingForm.srchin.length; i++) {
                   9650:             if ( callingForm.srchin.options[i].value == 'dom') {
                   9651: 		callingForm.srchin.selectedIndex = i;
1.556     raeburn  9652:             }
                   9653:         }
1.570     raeburn  9654:         for (var i=0; i<callingForm.srchtype.length; i++) {
                   9655:             if (callingForm.srchtype.options[i].value == 'exact') {
                   9656:                 callingForm.srchtype.selectedIndex = i;
1.556     raeburn  9657:             }
                   9658:         }
1.570     raeburn  9659:         for (var i=0; i<callingForm.srchdomain.length; i++) {
1.994     raeburn  9660:             if (callingForm.srchdomain.options[i].value == '$context_dom') {
1.570     raeburn  9661:                 callingForm.srchdomain.selectedIndex = i;
1.556     raeburn  9662:             }
                   9663:         }
                   9664:     }
                   9665: }
                   9666: ENDSCRIPT
1.558     albertel 9667: 
1.556     raeburn  9668:     }
                   9669: 
1.555     raeburn  9670:     my $output = <<"END_BLOCK";
1.556     raeburn  9671: <script type="text/javascript">
1.824     bisitz   9672: // <![CDATA[
1.570     raeburn  9673: function validateEntry(callingForm) {
1.558     albertel 9674: 
1.556     raeburn  9675:     var checkok = 1;
1.558     albertel 9676:     var srchin;
1.570     raeburn  9677:     for (var i=0; i<callingForm.srchin.length; i++) {
                   9678: 	if ( callingForm.srchin[i].checked ) {
                   9679: 	    srchin = callingForm.srchin[i].value;
1.558     albertel 9680: 	}
                   9681:     }
                   9682: 
1.570     raeburn  9683:     var srchtype = callingForm.srchtype.options[callingForm.srchtype.selectedIndex].value;
                   9684:     var srchby = callingForm.srchby.options[callingForm.srchby.selectedIndex].value;
                   9685:     var srchdomain = callingForm.srchdomain.options[callingForm.srchdomain.selectedIndex].value;
                   9686:     var srchterm =  callingForm.srchterm.value;
                   9687:     var srchin = callingForm.srchin.options[callingForm.srchin.selectedIndex].value;
1.556     raeburn  9688:     var msg = "";
                   9689: 
                   9690:     if (srchterm == "") {
                   9691:         checkok = 0;
1.1222  ! damieng  9692:         msg += "$js_lt{'youm'}\\n";
1.556     raeburn  9693:     }
                   9694: 
1.569     raeburn  9695:     if (srchtype== 'begins') {
                   9696:         if (srchterm.length < 2) {
                   9697:             checkok = 0;
1.1222  ! damieng  9698:             msg += "$js_lt{'thte'}\\n";
1.569     raeburn  9699:         }
                   9700:     }
                   9701: 
1.556     raeburn  9702:     if (srchtype== 'contains') {
                   9703:         if (srchterm.length < 3) {
                   9704:             checkok = 0;
1.1222  ! damieng  9705:             msg += "$js_lt{'thet'}\\n";
1.556     raeburn  9706:         }
                   9707:     }
                   9708:     if (srchin == 'instd') {
                   9709:         if (srchdomain == '') {
                   9710:             checkok = 0;
1.1222  ! damieng  9711:             msg += "$js_lt{'yomc'}\\n";
1.556     raeburn  9712:         }
                   9713:     }
                   9714:     if (srchin == 'dom') {
                   9715:         if (srchdomain == '') {
                   9716:             checkok = 0;
1.1222  ! damieng  9717:             msg += "$js_lt{'ymcd'}\\n";
1.556     raeburn  9718:         }
                   9719:     }
                   9720:     if (srchby == 'lastfirst') {
                   9721:         if (srchterm.indexOf(",") == -1) {
                   9722:             checkok = 0;
1.1222  ! damieng  9723:             msg += "$js_lt{'whus'}\\n";
1.556     raeburn  9724:         }
                   9725:         if (srchterm.indexOf(",") == srchterm.length -1) {
                   9726:             checkok = 0;
1.1222  ! damieng  9727:             msg += "$js_lt{'whse'}\\n";
1.556     raeburn  9728:         }
                   9729:     }
                   9730:     if (checkok == 0) {
1.1222  ! damieng  9731:         alert("$js_lt{'thfo'}\\n"+msg);
1.556     raeburn  9732:         return;
                   9733:     }
                   9734:     if (checkok == 1) {
1.570     raeburn  9735:         callingForm.submit();
1.556     raeburn  9736:     }
                   9737: }
                   9738: 
                   9739: $newuserscript
                   9740: 
1.824     bisitz   9741: // ]]>
1.556     raeburn  9742: </script>
1.558     albertel 9743: 
                   9744: $new_user_create
                   9745: 
1.555     raeburn  9746: END_BLOCK
1.558     albertel 9747: 
1.876     raeburn  9748:     $output .= &Apache::lonhtmlcommon::start_pick_box().
1.1222  ! damieng  9749:                &Apache::lonhtmlcommon::row_title($html_lt{'doma'}).
1.876     raeburn  9750:                $domform.
                   9751:                &Apache::lonhtmlcommon::row_closure().
1.1222  ! damieng  9752:                &Apache::lonhtmlcommon::row_title($html_lt{'usr'}).
1.876     raeburn  9753:                $srchbysel.
                   9754:                $srchtypesel. 
                   9755:                '<input type="text" size="15" name="srchterm" value="'.$srchterm.'" />'.
                   9756:                $srchinsel.
                   9757:                &Apache::lonhtmlcommon::row_closure(1). 
                   9758:                &Apache::lonhtmlcommon::end_pick_box().
                   9759:                '<br />';
1.555     raeburn  9760:     return $output;
                   9761: }
                   9762: 
1.612     raeburn  9763: sub user_rule_check {
1.615     raeburn  9764:     my ($usershash,$checks,$alerts,$rulematch,$inst_results,$curr_rules,$got_rules) = @_;
1.612     raeburn  9765:     my $response;
                   9766:     if (ref($usershash) eq 'HASH') {
                   9767:         foreach my $user (keys(%{$usershash})) {
                   9768:             my ($uname,$udom) = split(/:/,$user);
                   9769:             next if ($udom eq '' || $uname eq '');
1.615     raeburn  9770:             my ($id,$newuser);
1.612     raeburn  9771:             if (ref($usershash->{$user}) eq 'HASH') {
1.615     raeburn  9772:                 $newuser = $usershash->{$user}->{'newuser'};
1.612     raeburn  9773:                 $id = $usershash->{$user}->{'id'};
                   9774:             }
                   9775:             my $inst_response;
                   9776:             if (ref($checks) eq 'HASH') {
                   9777:                 if (defined($checks->{'username'})) {
1.615     raeburn  9778:                     ($inst_response,%{$inst_results->{$user}}) = 
1.612     raeburn  9779:                         &Apache::lonnet::get_instuser($udom,$uname);
                   9780:                 } elsif (defined($checks->{'id'})) {
1.615     raeburn  9781:                     ($inst_response,%{$inst_results->{$user}}) =
1.612     raeburn  9782:                         &Apache::lonnet::get_instuser($udom,undef,$id);
                   9783:                 }
1.615     raeburn  9784:             } else {
                   9785:                 ($inst_response,%{$inst_results->{$user}}) =
                   9786:                     &Apache::lonnet::get_instuser($udom,$uname);
                   9787:                 return;
1.612     raeburn  9788:             }
1.615     raeburn  9789:             if (!$got_rules->{$udom}) {
1.612     raeburn  9790:                 my %domconfig = &Apache::lonnet::get_dom('configuration',
                   9791:                                                   ['usercreation'],$udom);
                   9792:                 if (ref($domconfig{'usercreation'}) eq 'HASH') {
1.615     raeburn  9793:                     foreach my $item ('username','id') {
1.612     raeburn  9794:                         if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
                   9795:                             $$curr_rules{$udom}{$item} = 
                   9796:                                 $domconfig{'usercreation'}{$item.'_rule'};
1.585     raeburn  9797:                         }
                   9798:                     }
                   9799:                 }
1.615     raeburn  9800:                 $got_rules->{$udom} = 1;  
1.585     raeburn  9801:             }
1.612     raeburn  9802:             foreach my $item (keys(%{$checks})) {
                   9803:                 if (ref($$curr_rules{$udom}) eq 'HASH') {
                   9804:                     if (ref($$curr_rules{$udom}{$item}) eq 'ARRAY') {
                   9805:                         if (@{$$curr_rules{$udom}{$item}} > 0) {
                   9806:                             my %rule_check = &Apache::lonnet::inst_rulecheck($udom,$uname,$id,$item,$$curr_rules{$udom}{$item});
                   9807:                             foreach my $rule (@{$$curr_rules{$udom}{$item}}) {
                   9808:                                 if ($rule_check{$rule}) {
                   9809:                                     $$rulematch{$user}{$item} = $rule;
                   9810:                                     if ($inst_response eq 'ok') {
1.615     raeburn  9811:                                         if (ref($inst_results) eq 'HASH') {
                   9812:                                             if (ref($inst_results->{$user}) eq 'HASH') {
                   9813:                                                 if (keys(%{$inst_results->{$user}}) == 0) {
                   9814:                                                     $$alerts{$item}{$udom}{$uname} = 1;
                   9815:                                                 }
1.612     raeburn  9816:                                             }
                   9817:                                         }
1.615     raeburn  9818:                                     }
                   9819:                                     last;
1.585     raeburn  9820:                                 }
                   9821:                             }
                   9822:                         }
                   9823:                     }
                   9824:                 }
                   9825:             }
                   9826:         }
                   9827:     }
1.612     raeburn  9828:     return;
                   9829: }
                   9830: 
                   9831: sub user_rule_formats {
                   9832:     my ($domain,$domdesc,$curr_rules,$check) = @_;
                   9833:     my %text = ( 
                   9834:                  'username' => 'Usernames',
                   9835:                  'id'       => 'IDs',
                   9836:                );
                   9837:     my $output;
                   9838:     my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($domain,$check);
                   9839:     if ((ref($rules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
                   9840:         if (@{$ruleorder} > 0) {
1.1102    raeburn  9841:             $output = '<br />'.
                   9842:                       &mt($text{$check}.' with the following format(s) may [_1]only[_2] be used for verified users at [_3]:',
                   9843:                           '<span class="LC_cusr_emph">','</span>',$domdesc).
                   9844:                       ' <ul>';
1.612     raeburn  9845:             foreach my $rule (@{$ruleorder}) {
                   9846:                 if (ref($curr_rules) eq 'ARRAY') {
                   9847:                     if (grep(/^\Q$rule\E$/,@{$curr_rules})) {
                   9848:                         if (ref($rules->{$rule}) eq 'HASH') {
                   9849:                             $output .= '<li>'.$rules->{$rule}{'name'}.': '.
                   9850:                                         $rules->{$rule}{'desc'}.'</li>';
                   9851:                         }
                   9852:                     }
                   9853:                 }
                   9854:             }
                   9855:             $output .= '</ul>';
                   9856:         }
                   9857:     }
                   9858:     return $output;
                   9859: }
                   9860: 
                   9861: sub instrule_disallow_msg {
1.615     raeburn  9862:     my ($checkitem,$domdesc,$count,$mode) = @_;
1.612     raeburn  9863:     my $response;
                   9864:     my %text = (
                   9865:                   item   => 'username',
                   9866:                   items  => 'usernames',
                   9867:                   match  => 'matches',
                   9868:                   do     => 'does',
                   9869:                   action => 'a username',
                   9870:                   one    => 'one',
                   9871:                );
                   9872:     if ($count > 1) {
                   9873:         $text{'item'} = 'usernames';
                   9874:         $text{'match'} ='match';
                   9875:         $text{'do'} = 'do';
                   9876:         $text{'action'} = 'usernames',
                   9877:         $text{'one'} = 'ones';
                   9878:     }
                   9879:     if ($checkitem eq 'id') {
                   9880:         $text{'items'} = 'IDs';
                   9881:         $text{'item'} = 'ID';
                   9882:         $text{'action'} = 'an ID';
1.615     raeburn  9883:         if ($count > 1) {
                   9884:             $text{'item'} = 'IDs';
                   9885:             $text{'action'} = 'IDs';
                   9886:         }
1.612     raeburn  9887:     }
1.674     bisitz   9888:     $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  9889:     if ($mode eq 'upload') {
                   9890:         if ($checkitem eq 'username') {
                   9891:             $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'}.");
                   9892:         } elsif ($checkitem eq 'id') {
1.674     bisitz   9893:             $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  9894:         }
1.669     raeburn  9895:     } elsif ($mode eq 'selfcreate') {
                   9896:         if ($checkitem eq 'id') {
                   9897:             $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.");
                   9898:         }
1.615     raeburn  9899:     } else {
                   9900:         if ($checkitem eq 'username') {
                   9901:             $response .= &mt("You must choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}.");
                   9902:         } elsif ($checkitem eq 'id') {
                   9903:             $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.");
                   9904:         }
1.612     raeburn  9905:     }
                   9906:     return $response;
1.585     raeburn  9907: }
                   9908: 
1.624     raeburn  9909: sub personal_data_fieldtitles {
                   9910:     my %fieldtitles = &Apache::lonlocal::texthash (
                   9911:                         id => 'Student/Employee ID',
                   9912:                         permanentemail => 'E-mail address',
                   9913:                         lastname => 'Last Name',
                   9914:                         firstname => 'First Name',
                   9915:                         middlename => 'Middle Name',
                   9916:                         generation => 'Generation',
                   9917:                         gen => 'Generation',
1.765     raeburn  9918:                         inststatus => 'Affiliation',
1.624     raeburn  9919:                    );
                   9920:     return %fieldtitles;
                   9921: }
                   9922: 
1.642     raeburn  9923: sub sorted_inst_types {
                   9924:     my ($dom) = @_;
1.1185    raeburn  9925:     my ($usertypes,$order);
                   9926:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
                   9927:     if (ref($domdefaults{'inststatus'}) eq 'HASH') {
                   9928:         $usertypes = $domdefaults{'inststatus'}{'inststatustypes'};
                   9929:         $order = $domdefaults{'inststatus'}{'inststatusorder'};
                   9930:     } else {
                   9931:         ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($dom);
                   9932:     }
1.642     raeburn  9933:     my $othertitle = &mt('All users');
                   9934:     if ($env{'request.course.id'}) {
1.668     raeburn  9935:         $othertitle  = &mt('Any users');
1.642     raeburn  9936:     }
                   9937:     my @types;
                   9938:     if (ref($order) eq 'ARRAY') {
                   9939:         @types = @{$order};
                   9940:     }
                   9941:     if (@types == 0) {
                   9942:         if (ref($usertypes) eq 'HASH') {
                   9943:             @types = sort(keys(%{$usertypes}));
                   9944:         }
                   9945:     }
                   9946:     if (keys(%{$usertypes}) > 0) {
                   9947:         $othertitle = &mt('Other users');
                   9948:     }
                   9949:     return ($othertitle,$usertypes,\@types);
                   9950: }
                   9951: 
1.645     raeburn  9952: sub get_institutional_codes {
                   9953:     my ($settings,$allcourses,$LC_code) = @_;
                   9954: # Get complete list of course sections to update
                   9955:     my @currsections = ();
                   9956:     my @currxlists = ();
                   9957:     my $coursecode = $$settings{'internal.coursecode'};
                   9958: 
                   9959:     if ($$settings{'internal.sectionnums'} ne '') {
                   9960:         @currsections = split(/,/,$$settings{'internal.sectionnums'});
                   9961:     }
                   9962: 
                   9963:     if ($$settings{'internal.crosslistings'} ne '') {
                   9964:         @currxlists = split(/,/,$$settings{'internal.crosslistings'});
                   9965:     }
                   9966: 
                   9967:     if (@currxlists > 0) {
                   9968:         foreach (@currxlists) {
                   9969:             if (m/^([^:]+):(\w*)$/) {
                   9970:                 unless (grep/^$1$/,@{$allcourses}) {
                   9971:                     push @{$allcourses},$1;
                   9972:                     $$LC_code{$1} = $2;
                   9973:                 }
                   9974:             }
                   9975:         }
                   9976:     }
                   9977:  
                   9978:     if (@currsections > 0) {
                   9979:         foreach (@currsections) {
                   9980:             if (m/^(\w+):(\w*)$/) {
                   9981:                 my $sec = $coursecode.$1;
                   9982:                 my $lc_sec = $2;
                   9983:                 unless (grep/^$sec$/,@{$allcourses}) {
                   9984:                     push @{$allcourses},$sec;
                   9985:                     $$LC_code{$sec} = $lc_sec;
                   9986:                 }
                   9987:             }
                   9988:         }
                   9989:     }
                   9990:     return;
                   9991: }
                   9992: 
1.971     raeburn  9993: sub get_standard_codeitems {
                   9994:     return ('Year','Semester','Department','Number','Section');
                   9995: }
                   9996: 
1.112     bowersj2 9997: =pod
                   9998: 
1.780     raeburn  9999: =head1 Slot Helpers
                   10000: 
                   10001: =over 4
                   10002: 
                   10003: =item * sorted_slots()
                   10004: 
1.1040    raeburn  10005: Sorts an array of slot names in order of an optional sort key,
                   10006: default sort is by slot start time (earliest first). 
1.780     raeburn  10007: 
                   10008: Inputs:
                   10009: 
                   10010: =over 4
                   10011: 
                   10012: slotsarr  - Reference to array of unsorted slot names.
                   10013: 
                   10014: slots     - Reference to hash of hash, where outer hash keys are slot names.
                   10015: 
1.1040    raeburn  10016: sortkey   - Name of key in inner hash to be sorted on (e.g., starttime).
                   10017: 
1.549     albertel 10018: =back
                   10019: 
1.780     raeburn  10020: Returns:
                   10021: 
                   10022: =over 4
                   10023: 
1.1040    raeburn  10024: sorted   - An array of slot names sorted by a specified sort key 
                   10025:            (default sort key is start time of the slot).
1.780     raeburn  10026: 
                   10027: =back
                   10028: 
                   10029: =cut
                   10030: 
                   10031: 
                   10032: sub sorted_slots {
1.1040    raeburn  10033:     my ($slotsarr,$slots,$sortkey) = @_;
                   10034:     if ($sortkey eq '') {
                   10035:         $sortkey = 'starttime';
                   10036:     }
1.780     raeburn  10037:     my @sorted;
                   10038:     if ((ref($slotsarr) eq 'ARRAY') && (ref($slots) eq 'HASH')) {
                   10039:         @sorted =
                   10040:             sort {
                   10041:                      if (ref($slots->{$a}) && ref($slots->{$b})) {
1.1040    raeburn  10042:                          return $slots->{$a}{$sortkey} <=> $slots->{$b}{$sortkey}
1.780     raeburn  10043:                      }
                   10044:                      if (ref($slots->{$a})) { return -1;}
                   10045:                      if (ref($slots->{$b})) { return 1;}
                   10046:                      return 0;
                   10047:                  } @{$slotsarr};
                   10048:     }
                   10049:     return @sorted;
                   10050: }
                   10051: 
1.1040    raeburn  10052: =pod
                   10053: 
                   10054: =item * get_future_slots()
                   10055: 
                   10056: Inputs:
                   10057: 
                   10058: =over 4
                   10059: 
                   10060: cnum - course number
                   10061: 
                   10062: cdom - course domain
                   10063: 
                   10064: now - current UNIX time
                   10065: 
                   10066: symb - optional symb
                   10067: 
                   10068: =back
                   10069: 
                   10070: Returns:
                   10071: 
                   10072: =over 4
                   10073: 
                   10074: sorted_reservable - ref to array of student_schedulable slots currently 
                   10075:                     reservable, ordered by end date of reservation period.
                   10076: 
                   10077: reservable_now - ref to hash of student_schedulable slots currently
                   10078:                  reservable.
                   10079: 
                   10080:     Keys in inner hash are:
                   10081:     (a) symb: either blank or symb to which slot use is restricted.
                   10082:     (b) endreserve: end date of reservation period. 
                   10083: 
                   10084: sorted_future - ref to array of student_schedulable slots reservable in
                   10085:                 the future, ordered by start date of reservation period.
                   10086: 
                   10087: future_reservable - ref to hash of student_schedulable slots reservable
                   10088:                     in the future.
                   10089: 
                   10090:     Keys in inner hash are:
                   10091:     (a) symb: either blank or symb to which slot use is restricted.
                   10092:     (b) startreserve:  start date of reservation period.
                   10093: 
                   10094: =back
                   10095: 
                   10096: =cut
                   10097: 
                   10098: sub get_future_slots {
                   10099:     my ($cnum,$cdom,$now,$symb) = @_;
                   10100:     my (%reservable_now,%future_reservable,@sorted_reservable,@sorted_future);
                   10101:     my %slots = &Apache::lonnet::get_course_slots($cnum,$cdom);
                   10102:     foreach my $slot (keys(%slots)) {
                   10103:         next unless($slots{$slot}->{'type'} eq 'schedulable_student');
                   10104:         if ($symb) {
                   10105:             next if (($slots{$slot}->{'symb'} ne '') && 
                   10106:                      ($slots{$slot}->{'symb'} ne $symb));
                   10107:         }
                   10108:         if (($slots{$slot}->{'starttime'} > $now) &&
                   10109:             ($slots{$slot}->{'endtime'} > $now)) {
                   10110:             if (($slots{$slot}->{'allowedsections'}) || ($slots{$slot}->{'allowedusers'})) {
                   10111:                 my $userallowed = 0;
                   10112:                 if ($slots{$slot}->{'allowedsections'}) {
                   10113:                     my @allowed_sec = split(',',$slots{$slot}->{'allowedsections'});
                   10114:                     if (!defined($env{'request.role.sec'})
                   10115:                         && grep(/^No section assigned$/,@allowed_sec)) {
                   10116:                         $userallowed=1;
                   10117:                     } else {
                   10118:                         if (grep(/^\Q$env{'request.role.sec'}\E$/,@allowed_sec)) {
                   10119:                             $userallowed=1;
                   10120:                         }
                   10121:                     }
                   10122:                     unless ($userallowed) {
                   10123:                         if (defined($env{'request.course.groups'})) {
                   10124:                             my @groups = split(/:/,$env{'request.course.groups'});
                   10125:                             foreach my $group (@groups) {
                   10126:                                 if (grep(/^\Q$group\E$/,@allowed_sec)) {
                   10127:                                     $userallowed=1;
                   10128:                                     last;
                   10129:                                 }
                   10130:                             }
                   10131:                         }
                   10132:                     }
                   10133:                 }
                   10134:                 if ($slots{$slot}->{'allowedusers'}) {
                   10135:                     my @allowed_users = split(',',$slots{$slot}->{'allowedusers'});
                   10136:                     my $user = $env{'user.name'}.':'.$env{'user.domain'};
                   10137:                     if (grep(/^\Q$user\E$/,@allowed_users)) {
                   10138:                         $userallowed = 1;
                   10139:                     }
                   10140:                 }
                   10141:                 next unless($userallowed);
                   10142:             }
                   10143:             my $startreserve = $slots{$slot}->{'startreserve'};
                   10144:             my $endreserve = $slots{$slot}->{'endreserve'};
                   10145:             my $symb = $slots{$slot}->{'symb'};
                   10146:             if (($startreserve < $now) &&
                   10147:                 (!$endreserve || $endreserve > $now)) {
                   10148:                 my $lastres = $endreserve;
                   10149:                 if (!$lastres) {
                   10150:                     $lastres = $slots{$slot}->{'starttime'};
                   10151:                 }
                   10152:                 $reservable_now{$slot} = {
                   10153:                                            symb       => $symb,
                   10154:                                            endreserve => $lastres
                   10155:                                          };
                   10156:             } elsif (($startreserve > $now) &&
                   10157:                      (!$endreserve || $endreserve > $startreserve)) {
                   10158:                 $future_reservable{$slot} = {
                   10159:                                               symb         => $symb,
                   10160:                                               startreserve => $startreserve
                   10161:                                             };
                   10162:             }
                   10163:         }
                   10164:     }
                   10165:     my @unsorted_reservable = keys(%reservable_now);
                   10166:     if (@unsorted_reservable > 0) {
                   10167:         @sorted_reservable = 
                   10168:             &sorted_slots(\@unsorted_reservable,\%reservable_now,'endreserve');
                   10169:     }
                   10170:     my @unsorted_future = keys(%future_reservable);
                   10171:     if (@unsorted_future > 0) {
                   10172:         @sorted_future =
                   10173:             &sorted_slots(\@unsorted_future,\%future_reservable,'startreserve');
                   10174:     }
                   10175:     return (\@sorted_reservable,\%reservable_now,\@sorted_future,\%future_reservable);
                   10176: }
1.780     raeburn  10177: 
                   10178: =pod
                   10179: 
1.1057    foxr     10180: =back
                   10181: 
1.549     albertel 10182: =head1 HTTP Helpers
                   10183: 
                   10184: =over 4
                   10185: 
1.648     raeburn  10186: =item * &get_unprocessed_cgi($query,$possible_names)
1.112     bowersj2 10187: 
1.258     albertel 10188: Modify the %env hash to contain unprocessed CGI form parameters held in
1.112     bowersj2 10189: $query.  The parameters listed in $possible_names (an array reference),
1.258     albertel 10190: will be set in $env{'form.name'} if they do not already exist.
1.112     bowersj2 10191: 
                   10192: Typically called with $ENV{'QUERY_STRING'} as the first parameter.  
                   10193: $possible_names is an ref to an array of form element names.  As an example:
                   10194: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
1.258     albertel 10195: will result in $env{'form.uname'} and $env{'form.udom'} being set.
1.112     bowersj2 10196: 
                   10197: =cut
1.1       albertel 10198: 
1.6       albertel 10199: sub get_unprocessed_cgi {
1.25      albertel 10200:   my ($query,$possible_names)= @_;
1.26      matthew  10201:   # $Apache::lonxml::debug=1;
1.356     albertel 10202:   foreach my $pair (split(/&/,$query)) {
                   10203:     my ($name, $value) = split(/=/,$pair);
1.369     www      10204:     $name = &unescape($name);
1.25      albertel 10205:     if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
                   10206:       $value =~ tr/+/ /;
                   10207:       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.258     albertel 10208:       unless (defined($env{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
1.25      albertel 10209:     }
1.16      harris41 10210:   }
1.6       albertel 10211: }
                   10212: 
1.112     bowersj2 10213: =pod
                   10214: 
1.648     raeburn  10215: =item * &cacheheader() 
1.112     bowersj2 10216: 
                   10217: returns cache-controlling header code
                   10218: 
                   10219: =cut
                   10220: 
1.7       albertel 10221: sub cacheheader {
1.258     albertel 10222:     unless ($env{'request.method'} eq 'GET') { return ''; }
1.216     albertel 10223:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
                   10224:     my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
1.7       albertel 10225:                 <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
                   10226:                 <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
1.216     albertel 10227:     return $output;
1.7       albertel 10228: }
                   10229: 
1.112     bowersj2 10230: =pod
                   10231: 
1.648     raeburn  10232: =item * &no_cache($r) 
1.112     bowersj2 10233: 
                   10234: specifies header code to not have cache
                   10235: 
                   10236: =cut
                   10237: 
1.9       albertel 10238: sub no_cache {
1.216     albertel 10239:     my ($r) = @_;
                   10240:     if ($ENV{'REQUEST_METHOD'} ne 'GET' &&
1.258     albertel 10241: 	$env{'request.method'} ne 'GET') { return ''; }
1.216     albertel 10242:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time));
                   10243:     $r->no_cache(1);
                   10244:     $r->header_out("Expires" => $date);
                   10245:     $r->header_out("Pragma" => "no-cache");
1.123     www      10246: }
                   10247: 
                   10248: sub content_type {
1.181     albertel 10249:     my ($r,$type,$charset) = @_;
1.299     foxr     10250:     if ($r) {
                   10251: 	#  Note that printout.pl calls this with undef for $r.
                   10252: 	&no_cache($r);
                   10253:     }
1.258     albertel 10254:     if ($env{'browser.mathml'} && $type eq 'text/html') { $type='text/xml'; }
1.181     albertel 10255:     unless ($charset) {
                   10256: 	$charset=&Apache::lonlocal::current_encoding;
                   10257:     }
                   10258:     if ($charset) { $type.='; charset='.$charset; }
                   10259:     if ($r) {
                   10260: 	$r->content_type($type);
                   10261:     } else {
                   10262: 	print("Content-type: $type\n\n");
                   10263:     }
1.9       albertel 10264: }
1.25      albertel 10265: 
1.112     bowersj2 10266: =pod
                   10267: 
1.648     raeburn  10268: =item * &add_to_env($name,$value) 
1.112     bowersj2 10269: 
1.258     albertel 10270: adds $name to the %env hash with value
1.112     bowersj2 10271: $value, if $name already exists, the entry is converted to an array
                   10272: reference and $value is added to the array.
                   10273: 
                   10274: =cut
                   10275: 
1.25      albertel 10276: sub add_to_env {
                   10277:   my ($name,$value)=@_;
1.258     albertel 10278:   if (defined($env{$name})) {
                   10279:     if (ref($env{$name})) {
1.25      albertel 10280:       #already have multiple values
1.258     albertel 10281:       push(@{ $env{$name} },$value);
1.25      albertel 10282:     } else {
                   10283:       #first time seeing multiple values, convert hash entry to an arrayref
1.258     albertel 10284:       my $first=$env{$name};
                   10285:       undef($env{$name});
                   10286:       push(@{ $env{$name} },$first,$value);
1.25      albertel 10287:     }
                   10288:   } else {
1.258     albertel 10289:     $env{$name}=$value;
1.25      albertel 10290:   }
1.31      albertel 10291: }
1.149     albertel 10292: 
                   10293: =pod
                   10294: 
1.648     raeburn  10295: =item * &get_env_multiple($name) 
1.149     albertel 10296: 
1.258     albertel 10297: gets $name from the %env hash, it seemlessly handles the cases where multiple
1.149     albertel 10298: values may be defined and end up as an array ref.
                   10299: 
                   10300: returns an array of values
                   10301: 
                   10302: =cut
                   10303: 
                   10304: sub get_env_multiple {
                   10305:     my ($name) = @_;
                   10306:     my @values;
1.258     albertel 10307:     if (defined($env{$name})) {
1.149     albertel 10308:         # exists is it an array
1.258     albertel 10309:         if (ref($env{$name})) {
                   10310:             @values=@{ $env{$name} };
1.149     albertel 10311:         } else {
1.258     albertel 10312:             $values[0]=$env{$name};
1.149     albertel 10313:         }
                   10314:     }
                   10315:     return(@values);
                   10316: }
                   10317: 
1.660     raeburn  10318: sub ask_for_embedded_content {
                   10319:     my ($actionurl,$state,$allfiles,$codebase,$args)=@_;
1.1071    raeburn  10320:     my (%subdependencies,%dependencies,%mapping,%existing,%newfiles,%pathchanges,
1.1085    raeburn  10321:         %currsubfile,%unused,$rem);
1.1071    raeburn  10322:     my $counter = 0;
                   10323:     my $numnew = 0;
1.987     raeburn  10324:     my $numremref = 0;
                   10325:     my $numinvalid = 0;
                   10326:     my $numpathchg = 0;
                   10327:     my $numexisting = 0;
1.1071    raeburn  10328:     my $numunused = 0;
                   10329:     my ($output,$upload_output,$toplevel,$url,$udom,$uname,$getpropath,$cdom,$cnum,
1.1156    raeburn  10330:         $fileloc,$filename,$delete_output,$modify_output,$title,$symb,$path,$navmap);
1.1071    raeburn  10331:     my $heading = &mt('Upload embedded files');
                   10332:     my $buttontext = &mt('Upload');
                   10333: 
1.1085    raeburn  10334:     if ($env{'request.course.id'}) {
1.1123    raeburn  10335:         if ($actionurl eq '/adm/dependencies') {
                   10336:             $navmap = Apache::lonnavmaps::navmap->new();
                   10337:         }
                   10338:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   10339:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.1085    raeburn  10340:     }
1.1123    raeburn  10341:     if (($actionurl eq '/adm/portfolio') || 
                   10342:         ($actionurl eq '/adm/coursegrp_portfolio')) {
1.984     raeburn  10343:         my $current_path='/';
                   10344:         if ($env{'form.currentpath'}) {
                   10345:             $current_path = $env{'form.currentpath'};
                   10346:         }
                   10347:         if ($actionurl eq '/adm/coursegrp_portfolio') {
1.1123    raeburn  10348:             $udom = $cdom;
                   10349:             $uname = $cnum;
1.984     raeburn  10350:             $url = '/userfiles/groups/'.$env{'form.group'}.'/portfolio';
                   10351:         } else {
                   10352:             $udom = $env{'user.domain'};
                   10353:             $uname = $env{'user.name'};
                   10354:             $url = '/userfiles/portfolio';
                   10355:         }
1.987     raeburn  10356:         $toplevel = $url.'/';
1.984     raeburn  10357:         $url .= $current_path;
                   10358:         $getpropath = 1;
1.987     raeburn  10359:     } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank') ||
                   10360:              ($actionurl eq '/adm/imsimport')) { 
1.1022    www      10361:         my ($udom,$uname,$rest) = ($args->{'current_path'} =~ m{/priv/($match_domain)/($match_username)/?(.*)$});
1.1026    raeburn  10362:         $url = $Apache::lonnet::perlvar{'lonDocRoot'}."/priv/$udom/$uname/";
1.987     raeburn  10363:         $toplevel = $url;
1.984     raeburn  10364:         if ($rest ne '') {
1.987     raeburn  10365:             $url .= $rest;
                   10366:         }
                   10367:     } elsif ($actionurl eq '/adm/coursedocs') {
                   10368:         if (ref($args) eq 'HASH') {
1.1071    raeburn  10369:             $url = $args->{'docs_url'};
                   10370:             $toplevel = $url;
1.1084    raeburn  10371:             if ($args->{'context'} eq 'paste') {
                   10372:                 ($cdom,$cnum) = ($url =~ m{^\Q/uploaded/\E($match_domain)/($match_courseid)/});
                   10373:                 ($path) = 
                   10374:                     ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/\E(?:docs|supplemental)/(?:default|\d+)/\d+)/});
                   10375:                 $fileloc = &Apache::lonnet::filelocation('',$toplevel);
                   10376:                 $fileloc =~ s{^/}{};
                   10377:             }
1.1071    raeburn  10378:         }
1.1084    raeburn  10379:     } elsif ($actionurl eq '/adm/dependencies')  {
1.1071    raeburn  10380:         if ($env{'request.course.id'} ne '') {
                   10381:             if (ref($args) eq 'HASH') {
                   10382:                 $url = $args->{'docs_url'};
                   10383:                 $title = $args->{'docs_title'};
1.1126    raeburn  10384:                 $toplevel = $url; 
                   10385:                 unless ($toplevel =~ m{^/}) {
                   10386:                     $toplevel = "/$url";
                   10387:                 }
1.1085    raeburn  10388:                 ($rem) = ($toplevel =~ m{^(.+/)[^/]+$});
1.1126    raeburn  10389:                 if ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/portfolio/syllabus\E)}) {
                   10390:                     $path = $1;
                   10391:                 } else {
                   10392:                     ($path) =
                   10393:                         ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/\E(?:docs|supplemental)/(?:default|\d+)/\d+)/});
                   10394:                 }
1.1195    raeburn  10395:                 if ($toplevel=~/^\/*(uploaded|editupload)/) {
                   10396:                     $fileloc = $toplevel;
                   10397:                     $fileloc=~ s/^\s*(\S+)\s*$/$1/;
                   10398:                     my ($udom,$uname,$fname) =
                   10399:                         ($fileloc=~ m{^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$});
                   10400:                     $fileloc = propath($udom,$uname).'/userfiles/'.$fname;
                   10401:                 } else {
                   10402:                     $fileloc = &Apache::lonnet::filelocation('',$toplevel);
                   10403:                 }
1.1071    raeburn  10404:                 $fileloc =~ s{^/}{};
                   10405:                 ($filename) = ($fileloc =~ m{.+/([^/]+)$});
                   10406:                 $heading = &mt('Status of dependencies in [_1]',"$title ($filename)");
                   10407:             }
1.987     raeburn  10408:         }
1.1123    raeburn  10409:     } elsif ($actionurl eq "/public/$cdom/$cnum/syllabus") {
                   10410:         $udom = $cdom;
                   10411:         $uname = $cnum;
                   10412:         $url = "/uploaded/$cdom/$cnum/portfolio/syllabus";
                   10413:         $toplevel = $url;
                   10414:         $path = $url;
                   10415:         $fileloc = &Apache::lonnet::filelocation('',$toplevel).'/';
                   10416:         $fileloc =~ s{^/}{};
1.987     raeburn  10417:     }
1.1126    raeburn  10418:     foreach my $file (keys(%{$allfiles})) {
                   10419:         my $embed_file;
                   10420:         if (($path eq "/uploaded/$cdom/$cnum/portfolio/syllabus") && ($file =~ m{^\Q$path/\E(.+)$})) {
                   10421:             $embed_file = $1;
                   10422:         } else {
                   10423:             $embed_file = $file;
                   10424:         }
1.1158    raeburn  10425:         my ($absolutepath,$cleaned_file);
                   10426:         if ($embed_file =~ m{^\w+://}) {
                   10427:             $cleaned_file = $embed_file;
1.1147    raeburn  10428:             $newfiles{$cleaned_file} = 1;
                   10429:             $mapping{$cleaned_file} = $embed_file;
1.987     raeburn  10430:         } else {
1.1158    raeburn  10431:             $cleaned_file = &clean_path($embed_file);
1.987     raeburn  10432:             if ($embed_file =~ m{^/}) {
                   10433:                 $absolutepath = $embed_file;
                   10434:             }
1.1147    raeburn  10435:             if ($cleaned_file =~ m{/}) {
                   10436:                 my ($path,$fname) = ($cleaned_file =~ m{^(.+)/([^/]*)$});
1.987     raeburn  10437:                 $path = &check_for_traversal($path,$url,$toplevel);
                   10438:                 my $item = $fname;
                   10439:                 if ($path ne '') {
                   10440:                     $item = $path.'/'.$fname;
                   10441:                     $subdependencies{$path}{$fname} = 1;
                   10442:                 } else {
                   10443:                     $dependencies{$item} = 1;
                   10444:                 }
                   10445:                 if ($absolutepath) {
                   10446:                     $mapping{$item} = $absolutepath;
                   10447:                 } else {
                   10448:                     $mapping{$item} = $embed_file;
                   10449:                 }
                   10450:             } else {
                   10451:                 $dependencies{$embed_file} = 1;
                   10452:                 if ($absolutepath) {
1.1147    raeburn  10453:                     $mapping{$cleaned_file} = $absolutepath;
1.987     raeburn  10454:                 } else {
1.1147    raeburn  10455:                     $mapping{$cleaned_file} = $embed_file;
1.987     raeburn  10456:                 }
                   10457:             }
1.984     raeburn  10458:         }
                   10459:     }
1.1071    raeburn  10460:     my $dirptr = 16384;
1.984     raeburn  10461:     foreach my $path (keys(%subdependencies)) {
1.1071    raeburn  10462:         $currsubfile{$path} = {};
1.1123    raeburn  10463:         if (($actionurl eq '/adm/portfolio') || 
                   10464:             ($actionurl eq '/adm/coursegrp_portfolio')) {
1.1021    raeburn  10465:             my ($sublistref,$listerror) =
                   10466:                 &Apache::lonnet::dirlist($url.$path,$udom,$uname,$getpropath);
                   10467:             if (ref($sublistref) eq 'ARRAY') {
                   10468:                 foreach my $line (@{$sublistref}) {
                   10469:                     my ($file_name,$rest) = split(/\&/,$line,2);
1.1071    raeburn  10470:                     $currsubfile{$path}{$file_name} = 1;
1.1021    raeburn  10471:                 }
1.984     raeburn  10472:             }
1.987     raeburn  10473:         } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
1.984     raeburn  10474:             if (opendir(my $dir,$url.'/'.$path)) {
                   10475:                 my @subdir_list = grep(!/^\./,readdir($dir));
1.1071    raeburn  10476:                 map {$currsubfile{$path}{$_} = 1;} @subdir_list;
                   10477:             }
1.1084    raeburn  10478:         } elsif (($actionurl eq '/adm/dependencies') ||
                   10479:                  (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
1.1123    raeburn  10480:                   ($args->{'context'} eq 'paste')) ||
                   10481:                  ($actionurl eq "/public/$cdom/$cnum/syllabus")) {
1.1071    raeburn  10482:             if ($env{'request.course.id'} ne '') {
1.1123    raeburn  10483:                 my $dir;
                   10484:                 if ($actionurl eq "/public/$cdom/$cnum/syllabus") {
                   10485:                     $dir = $fileloc;
                   10486:                 } else {
                   10487:                     ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
                   10488:                 }
1.1071    raeburn  10489:                 if ($dir ne '') {
                   10490:                     my ($sublistref,$listerror) =
                   10491:                         &Apache::lonnet::dirlist($dir.$path,$cdom,$cnum,$getpropath,undef,'/');
                   10492:                     if (ref($sublistref) eq 'ARRAY') {
                   10493:                         foreach my $line (@{$sublistref}) {
                   10494:                             my ($file_name,$dom,undef,$testdir,undef,undef,undef,undef,$size,
                   10495:                                 undef,$mtime)=split(/\&/,$line,12);
                   10496:                             unless (($testdir&$dirptr) ||
                   10497:                                     ($file_name =~ /^\.\.?$/)) {
                   10498:                                 $currsubfile{$path}{$file_name} = [$size,$mtime];
                   10499:                             }
                   10500:                         }
                   10501:                     }
                   10502:                 }
1.984     raeburn  10503:             }
                   10504:         }
                   10505:         foreach my $file (keys(%{$subdependencies{$path}})) {
1.1071    raeburn  10506:             if (exists($currsubfile{$path}{$file})) {
1.987     raeburn  10507:                 my $item = $path.'/'.$file;
                   10508:                 unless ($mapping{$item} eq $item) {
                   10509:                     $pathchanges{$item} = 1;
                   10510:                 }
                   10511:                 $existing{$item} = 1;
                   10512:                 $numexisting ++;
                   10513:             } else {
                   10514:                 $newfiles{$path.'/'.$file} = 1;
1.984     raeburn  10515:             }
                   10516:         }
1.1071    raeburn  10517:         if ($actionurl eq '/adm/dependencies') {
                   10518:             foreach my $path (keys(%currsubfile)) {
                   10519:                 if (ref($currsubfile{$path}) eq 'HASH') {
                   10520:                     foreach my $file (keys(%{$currsubfile{$path}})) {
                   10521:                          unless ($subdependencies{$path}{$file}) {
1.1085    raeburn  10522:                              next if (($rem ne '') &&
                   10523:                                       (($env{"httpref.$rem"."$path/$file"} ne '') ||
                   10524:                                        (ref($navmap) &&
                   10525:                                        (($navmap->getResourceByUrl($rem."$path/$file") ne '') ||
                   10526:                                         (($file =~ /^(.*\.s?html?)\.bak$/i) &&
                   10527:                                          ($navmap->getResourceByUrl($rem."$path/$1")))))));
1.1071    raeburn  10528:                              $unused{$path.'/'.$file} = 1; 
                   10529:                          }
                   10530:                     }
                   10531:                 }
                   10532:             }
                   10533:         }
1.984     raeburn  10534:     }
1.987     raeburn  10535:     my %currfile;
1.1123    raeburn  10536:     if (($actionurl eq '/adm/portfolio') ||
                   10537:         ($actionurl eq '/adm/coursegrp_portfolio')) {
1.1021    raeburn  10538:         my ($dirlistref,$listerror) =
                   10539:             &Apache::lonnet::dirlist($url,$udom,$uname,$getpropath);
                   10540:         if (ref($dirlistref) eq 'ARRAY') {
                   10541:             foreach my $line (@{$dirlistref}) {
                   10542:                 my ($file_name,$rest) = split(/\&/,$line,2);
                   10543:                 $currfile{$file_name} = 1;
                   10544:             }
1.984     raeburn  10545:         }
1.987     raeburn  10546:     } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
1.984     raeburn  10547:         if (opendir(my $dir,$url)) {
1.987     raeburn  10548:             my @dir_list = grep(!/^\./,readdir($dir));
1.984     raeburn  10549:             map {$currfile{$_} = 1;} @dir_list;
                   10550:         }
1.1084    raeburn  10551:     } elsif (($actionurl eq '/adm/dependencies') ||
                   10552:              (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
1.1123    raeburn  10553:               ($args->{'context'} eq 'paste')) ||
                   10554:              ($actionurl eq "/public/$cdom/$cnum/syllabus")) {
1.1071    raeburn  10555:         if ($env{'request.course.id'} ne '') {
                   10556:             my ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
                   10557:             if ($dir ne '') {
                   10558:                 my ($dirlistref,$listerror) =
                   10559:                     &Apache::lonnet::dirlist($dir,$cdom,$cnum,$getpropath,undef,'/');
                   10560:                 if (ref($dirlistref) eq 'ARRAY') {
                   10561:                     foreach my $line (@{$dirlistref}) {
                   10562:                         my ($file_name,$dom,undef,$testdir,undef,undef,undef,undef,
                   10563:                             $size,undef,$mtime)=split(/\&/,$line,12);
                   10564:                         unless (($testdir&$dirptr) ||
                   10565:                                 ($file_name =~ /^\.\.?$/)) {
                   10566:                             $currfile{$file_name} = [$size,$mtime];
                   10567:                         }
                   10568:                     }
                   10569:                 }
                   10570:             }
                   10571:         }
1.984     raeburn  10572:     }
                   10573:     foreach my $file (keys(%dependencies)) {
1.1071    raeburn  10574:         if (exists($currfile{$file})) {
1.987     raeburn  10575:             unless ($mapping{$file} eq $file) {
                   10576:                 $pathchanges{$file} = 1;
                   10577:             }
                   10578:             $existing{$file} = 1;
                   10579:             $numexisting ++;
                   10580:         } else {
1.984     raeburn  10581:             $newfiles{$file} = 1;
                   10582:         }
                   10583:     }
1.1071    raeburn  10584:     foreach my $file (keys(%currfile)) {
                   10585:         unless (($file eq $filename) ||
                   10586:                 ($file eq $filename.'.bak') ||
                   10587:                 ($dependencies{$file})) {
1.1085    raeburn  10588:             if ($actionurl eq '/adm/dependencies') {
1.1126    raeburn  10589:                 unless ($toplevel =~ m{^\Q/uploaded/$cdom/$cnum/portfolio/syllabus\E}) {
                   10590:                     next if (($rem ne '') &&
                   10591:                              (($env{"httpref.$rem".$file} ne '') ||
                   10592:                               (ref($navmap) &&
                   10593:                               (($navmap->getResourceByUrl($rem.$file) ne '') ||
                   10594:                                (($file =~ /^(.*\.s?html?)\.bak$/i) &&
                   10595:                                 ($navmap->getResourceByUrl($rem.$1)))))));
                   10596:                 }
1.1085    raeburn  10597:             }
1.1071    raeburn  10598:             $unused{$file} = 1;
                   10599:         }
                   10600:     }
1.1084    raeburn  10601:     if (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
                   10602:         ($args->{'context'} eq 'paste')) {
                   10603:         $counter = scalar(keys(%existing));
                   10604:         $numpathchg = scalar(keys(%pathchanges));
1.1123    raeburn  10605:         return ($output,$counter,$numpathchg,\%existing);
                   10606:     } elsif (($actionurl eq "/public/$cdom/$cnum/syllabus") && 
                   10607:              (ref($args) eq 'HASH') && ($args->{'context'} eq 'rewrites')) {
                   10608:         $counter = scalar(keys(%existing));
                   10609:         $numpathchg = scalar(keys(%pathchanges));
                   10610:         return ($output,$counter,$numpathchg,\%existing,\%mapping);
1.1084    raeburn  10611:     }
1.984     raeburn  10612:     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%newfiles)) {
1.1071    raeburn  10613:         if ($actionurl eq '/adm/dependencies') {
                   10614:             next if ($embed_file =~ m{^\w+://});
                   10615:         }
1.660     raeburn  10616:         $upload_output .= &start_data_table_row().
1.1123    raeburn  10617:                           '<td valign="top"><img src="'.&icon($embed_file).'" />&nbsp;'.
1.1071    raeburn  10618:                           '<span class="LC_filename">'.$embed_file.'</span>';
1.987     raeburn  10619:         unless ($mapping{$embed_file} eq $embed_file) {
1.1123    raeburn  10620:             $upload_output .= '<br /><span class="LC_info" style="font-size:smaller;">'.
                   10621:                               &mt('changed from: [_1]',$mapping{$embed_file}).'</span>';
1.987     raeburn  10622:         }
1.1123    raeburn  10623:         $upload_output .= '</td>';
1.1071    raeburn  10624:         if ($args->{'ignore_remote_references'} && $embed_file =~ m{^\w+://}) { 
1.1123    raeburn  10625:             $upload_output.='<td align="right">'.
                   10626:                             '<span class="LC_info LC_fontsize_medium">'.
                   10627:                             &mt("URL points to web address").'</span>';
1.987     raeburn  10628:             $numremref++;
1.660     raeburn  10629:         } elsif ($args->{'error_on_invalid_names'}
                   10630:             && $embed_file ne &Apache::lonnet::clean_filename($embed_file,{'keep_path' => 1,})) {
1.1123    raeburn  10631:             $upload_output.='<td align="right"><span class="LC_warning">'.
                   10632:                             &mt('Invalid characters').'</span>';
1.987     raeburn  10633:             $numinvalid++;
1.660     raeburn  10634:         } else {
1.1123    raeburn  10635:             $upload_output .= '<td>'.
                   10636:                               &embedded_file_element('upload_embedded',$counter,
1.987     raeburn  10637:                                                      $embed_file,\%mapping,
1.1071    raeburn  10638:                                                      $allfiles,$codebase,'upload');
                   10639:             $counter ++;
                   10640:             $numnew ++;
1.987     raeburn  10641:         }
                   10642:         $upload_output .= '</td>'.&Apache::loncommon::end_data_table_row()."\n";
                   10643:     }
                   10644:     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%existing)) {
1.1071    raeburn  10645:         if ($actionurl eq '/adm/dependencies') {
                   10646:             my ($size,$mtime) = &get_dependency_details(\%currfile,\%currsubfile,$embed_file);
                   10647:             $modify_output .= &start_data_table_row().
                   10648:                               '<td><a href="'.$path.'/'.$embed_file.'" style="text-decoration:none;">'.
                   10649:                               '<img src="'.&icon($embed_file).'" border="0" />'.
                   10650:                               '&nbsp;<span class="LC_filename">'.$embed_file.'</span></a></td>'.
                   10651:                               '<td>'.$size.'</td>'.
                   10652:                               '<td>'.$mtime.'</td>'.
                   10653:                               '<td><label><input type="checkbox" name="mod_upload_dep" '.
                   10654:                               'onclick="toggleBrowse('."'$counter'".')" id="mod_upload_dep_'.
                   10655:                               $counter.'" value="'.$counter.'" />'.&mt('Yes').'</label>'.
                   10656:                               '<div id="moduploaddep_'.$counter.'" style="display:none;">'.
                   10657:                               &embedded_file_element('upload_embedded',$counter,
                   10658:                                                      $embed_file,\%mapping,
                   10659:                                                      $allfiles,$codebase,'modify').
                   10660:                               '</div></td>'.
                   10661:                               &end_data_table_row()."\n";
                   10662:             $counter ++;
                   10663:         } else {
                   10664:             $upload_output .= &start_data_table_row().
1.1123    raeburn  10665:                               '<td valign="top"><img src="'.&icon($embed_file).'" />&nbsp;'.
                   10666:                               '<span class="LC_filename">'.$embed_file.'</span></td>'.
                   10667:                               '<td align="right"><span class="LC_info LC_fontsize_medium">'.&mt('Already exists').'</span></td>'.
1.1071    raeburn  10668:                               &Apache::loncommon::end_data_table_row()."\n";
                   10669:         }
                   10670:     }
                   10671:     my $delidx = $counter;
                   10672:     foreach my $oldfile (sort {lc($a) cmp lc($b)} keys(%unused)) {
                   10673:         my ($size,$mtime) = &get_dependency_details(\%currfile,\%currsubfile,$oldfile);
                   10674:         $delete_output .= &start_data_table_row().
                   10675:                           '<td><img src="'.&icon($oldfile).'" />'.
                   10676:                           '&nbsp;<span class="LC_filename">'.$oldfile.'</span></td>'.
                   10677:                           '<td>'.$size.'</td>'.
                   10678:                           '<td>'.$mtime.'</td>'.
                   10679:                           '<td><label><input type="checkbox" name="del_upload_dep" '.
                   10680:                           ' value="'.$delidx.'" />'.&mt('Yes').'</label>'.
                   10681:                           &embedded_file_element('upload_embedded',$delidx,
                   10682:                                                  $oldfile,\%mapping,$allfiles,
                   10683:                                                  $codebase,'delete').'</td>'.
                   10684:                           &end_data_table_row()."\n"; 
                   10685:         $numunused ++;
                   10686:         $delidx ++;
1.987     raeburn  10687:     }
                   10688:     if ($upload_output) {
                   10689:         $upload_output = &start_data_table().
                   10690:                          $upload_output.
                   10691:                          &end_data_table()."\n";
                   10692:     }
1.1071    raeburn  10693:     if ($modify_output) {
                   10694:         $modify_output = &start_data_table().
                   10695:                          &start_data_table_header_row().
                   10696:                          '<th>'.&mt('File').'</th>'.
                   10697:                          '<th>'.&mt('Size (KB)').'</th>'.
                   10698:                          '<th>'.&mt('Modified').'</th>'.
                   10699:                          '<th>'.&mt('Upload replacement?').'</th>'.
                   10700:                          &end_data_table_header_row().
                   10701:                          $modify_output.
                   10702:                          &end_data_table()."\n";
                   10703:     }
                   10704:     if ($delete_output) {
                   10705:         $delete_output = &start_data_table().
                   10706:                          &start_data_table_header_row().
                   10707:                          '<th>'.&mt('File').'</th>'.
                   10708:                          '<th>'.&mt('Size (KB)').'</th>'.
                   10709:                          '<th>'.&mt('Modified').'</th>'.
                   10710:                          '<th>'.&mt('Delete?').'</th>'.
                   10711:                          &end_data_table_header_row().
                   10712:                          $delete_output.
                   10713:                          &end_data_table()."\n";
                   10714:     }
1.987     raeburn  10715:     my $applies = 0;
                   10716:     if ($numremref) {
                   10717:         $applies ++;
                   10718:     }
                   10719:     if ($numinvalid) {
                   10720:         $applies ++;
                   10721:     }
                   10722:     if ($numexisting) {
                   10723:         $applies ++;
                   10724:     }
1.1071    raeburn  10725:     if ($counter || $numunused) {
1.987     raeburn  10726:         $output = '<form name="upload_embedded" action="'.$actionurl.'"'.
                   10727:                   ' method="post" enctype="multipart/form-data">'."\n".
1.1071    raeburn  10728:                   $state.'<h3>'.$heading.'</h3>'; 
                   10729:         if ($actionurl eq '/adm/dependencies') {
                   10730:             if ($numnew) {
                   10731:                 $output .= '<h4>'.&mt('Missing dependencies').'</h4>'.
                   10732:                            '<p>'.&mt('The following files need to be uploaded.').'</p>'."\n".
                   10733:                            $upload_output.'<br />'."\n";
                   10734:             }
                   10735:             if ($numexisting) {
                   10736:                 $output .= '<h4>'.&mt('Uploaded dependencies (in use)').'</h4>'.
                   10737:                            '<p>'.&mt('Upload a new file to replace the one currently in use.').'</p>'."\n".
                   10738:                            $modify_output.'<br />'."\n";
                   10739:                            $buttontext = &mt('Save changes');
                   10740:             }
                   10741:             if ($numunused) {
                   10742:                 $output .= '<h4>'.&mt('Unused files').'</h4>'.
                   10743:                            '<p>'.&mt('The following uploaded files are no longer used.').'</p>'."\n".
                   10744:                            $delete_output.'<br />'."\n";
                   10745:                            $buttontext = &mt('Save changes');
                   10746:             }
                   10747:         } else {
                   10748:             $output .= $upload_output.'<br />'."\n";
                   10749:         }
                   10750:         $output .= '<input type ="hidden" name="number_embedded_items" value="'.
                   10751:                    $counter.'" />'."\n";
                   10752:         if ($actionurl eq '/adm/dependencies') { 
                   10753:             $output .= '<input type ="hidden" name="number_newemb_items" value="'.
                   10754:                        $numnew.'" />'."\n";
                   10755:         } elsif ($actionurl eq '') {
1.987     raeburn  10756:             $output .=  '<input type="hidden" name="phase" value="three" />';
                   10757:         }
                   10758:     } elsif ($applies) {
                   10759:         $output = '<b>'.&mt('Referenced files').'</b>:<br />';
                   10760:         if ($applies > 1) {
                   10761:             $output .=  
1.1123    raeburn  10762:                 &mt('No dependencies need to be uploaded, as one of the following applies to each reference:').'<ul>';
1.987     raeburn  10763:             if ($numremref) {
                   10764:                 $output .= '<li>'.&mt('reference is to a URL which points to another server').'</li>'."\n";
                   10765:             }
                   10766:             if ($numinvalid) {
                   10767:                 $output .= '<li>'.&mt('reference is to file with a name containing invalid characters').'</li>'."\n";
                   10768:             }
                   10769:             if ($numexisting) {
                   10770:                 $output .= '<li>'.&mt('reference is to an existing file at the specified location').'</li>'."\n";
                   10771:             }
                   10772:             $output .= '</ul><br />';
                   10773:         } elsif ($numremref) {
                   10774:             $output .= '<p>'.&mt('None to upload, as all references are to URLs pointing to another server.').'</p>';
                   10775:         } elsif ($numinvalid) {
                   10776:             $output .= '<p>'.&mt('None to upload, as all references are to files with names containing invalid characters.').'</p>';
                   10777:         } elsif ($numexisting) {
                   10778:             $output .= '<p>'.&mt('None to upload, as all references are to existing files.').'</p>';
                   10779:         }
                   10780:         $output .= $upload_output.'<br />';
                   10781:     }
                   10782:     my ($pathchange_output,$chgcount);
1.1071    raeburn  10783:     $chgcount = $counter;
1.987     raeburn  10784:     if (keys(%pathchanges) > 0) {
                   10785:         foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%pathchanges)) {
1.1071    raeburn  10786:             if ($counter) {
1.987     raeburn  10787:                 $output .= &embedded_file_element('pathchange',$chgcount,
                   10788:                                                   $embed_file,\%mapping,
1.1071    raeburn  10789:                                                   $allfiles,$codebase,'change');
1.987     raeburn  10790:             } else {
                   10791:                 $pathchange_output .= 
                   10792:                     &start_data_table_row().
                   10793:                     '<td><input type ="checkbox" name="namechange" value="'.
                   10794:                     $chgcount.'" checked="checked" /></td>'.
                   10795:                     '<td>'.$mapping{$embed_file}.'</td>'.
                   10796:                     '<td>'.$embed_file.
                   10797:                     &embedded_file_element('pathchange',$numpathchg,$embed_file,
1.1071    raeburn  10798:                                            \%mapping,$allfiles,$codebase,'change').
1.987     raeburn  10799:                     '</td>'.&end_data_table_row();
1.660     raeburn  10800:             }
1.987     raeburn  10801:             $numpathchg ++;
                   10802:             $chgcount ++;
1.660     raeburn  10803:         }
                   10804:     }
1.1127    raeburn  10805:     if (($counter) || ($numunused)) {
1.987     raeburn  10806:         if ($numpathchg) {
                   10807:             $output .= '<input type ="hidden" name="number_pathchange_items" value="'.
                   10808:                        $numpathchg.'" />'."\n";
                   10809:         }
                   10810:         if (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank') || 
                   10811:             ($actionurl eq '/adm/imsimport')) {
                   10812:             $output .= '<input type="hidden" name="phase" value="three" />'."\n";
                   10813:         } elsif ($actionurl eq '/adm/portfolio' || $actionurl eq '/adm/coursegrp_portfolio') {
                   10814:             $output .= '<input type="hidden" name="action" value="upload_embedded" />';
1.1071    raeburn  10815:         } elsif ($actionurl eq '/adm/dependencies') {
                   10816:             $output .= '<input type="hidden" name="action" value="process_changes" />';
1.987     raeburn  10817:         }
1.1123    raeburn  10818:         $output .= '<input type ="submit" value="'.$buttontext.'" />'."\n".'</form>'."\n";
1.987     raeburn  10819:     } elsif ($numpathchg) {
                   10820:         my %pathchange = ();
                   10821:         $output .= &modify_html_form('pathchange',$actionurl,$state,\%pathchange,$pathchange_output);
                   10822:         if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
                   10823:             $output .= '<p>'.&mt('or').'</p>'; 
1.1123    raeburn  10824:         }
1.987     raeburn  10825:     }
1.1071    raeburn  10826:     return ($output,$counter,$numpathchg);
1.987     raeburn  10827: }
                   10828: 
1.1147    raeburn  10829: =pod
                   10830: 
                   10831: =item * clean_path($name)
                   10832: 
                   10833: Performs clean-up of directories, subdirectories and filename in an
                   10834: embedded object, referenced in an HTML file which is being uploaded
                   10835: to a course or portfolio, where 
                   10836: "Upload embedded images/multimedia files if HTML file" checkbox was
                   10837: checked.
                   10838: 
                   10839: Clean-up is similar to replacements in lonnet::clean_filename()
                   10840: except each / between sub-directory and next level is preserved.
                   10841: 
                   10842: =cut
                   10843: 
                   10844: sub clean_path {
                   10845:     my ($embed_file) = @_;
                   10846:     $embed_file =~s{^/+}{};
                   10847:     my @contents;
                   10848:     if ($embed_file =~ m{/}) {
                   10849:         @contents = split(/\//,$embed_file);
                   10850:     } else {
                   10851:         @contents = ($embed_file);
                   10852:     }
                   10853:     my $lastidx = scalar(@contents)-1;
                   10854:     for (my $i=0; $i<=$lastidx; $i++) { 
                   10855:         $contents[$i]=~s{\\}{/}g;
                   10856:         $contents[$i]=~s/\s+/\_/g;
                   10857:         $contents[$i]=~s{[^/\w\.\-]}{}g;
                   10858:         if ($i == $lastidx) {
                   10859:             $contents[$i]=~s/\.(\d+)(?=\.)/_$1/g;
                   10860:         }
                   10861:     }
                   10862:     if ($lastidx > 0) {
                   10863:         return join('/',@contents);
                   10864:     } else {
                   10865:         return $contents[0];
                   10866:     }
                   10867: }
                   10868: 
1.987     raeburn  10869: sub embedded_file_element {
1.1071    raeburn  10870:     my ($context,$num,$embed_file,$mapping,$allfiles,$codebase,$type) = @_;
1.987     raeburn  10871:     return unless ((ref($mapping) eq 'HASH') && (ref($allfiles) eq 'HASH') &&
                   10872:                    (ref($codebase) eq 'HASH'));
                   10873:     my $output;
1.1071    raeburn  10874:     if (($context eq 'upload_embedded') && ($type ne 'delete')) {
1.987     raeburn  10875:        $output = '<input name="embedded_item_'.$num.'" type="file" value="" />'."\n";
                   10876:     }
                   10877:     $output .= '<input name="embedded_orig_'.$num.'" type="hidden" value="'.
                   10878:                &escape($embed_file).'" />';
                   10879:     unless (($context eq 'upload_embedded') && 
                   10880:             ($mapping->{$embed_file} eq $embed_file)) {
                   10881:         $output .='
                   10882:         <input name="embedded_ref_'.$num.'" type="hidden" value="'.&escape($mapping->{$embed_file}).'" />';
                   10883:     }
                   10884:     my $attrib;
                   10885:     if (ref($allfiles->{$mapping->{$embed_file}}) eq 'ARRAY') {
                   10886:         $attrib = &escape(join(':',@{$allfiles->{$mapping->{$embed_file}}}));
                   10887:     }
                   10888:     $output .=
                   10889:         "\n\t\t".
                   10890:         '<input name="embedded_attrib_'.$num.'" type="hidden" value="'.
                   10891:         $attrib.'" />';
                   10892:     if (exists($codebase->{$mapping->{$embed_file}})) {
                   10893:         $output .=
                   10894:             "\n\t\t".
                   10895:             '<input name="codebase_'.$num.'" type="hidden" value="'.
                   10896:             &escape($codebase->{$mapping->{$embed_file}}).'" />';
1.984     raeburn  10897:     }
1.987     raeburn  10898:     return $output;
1.660     raeburn  10899: }
                   10900: 
1.1071    raeburn  10901: sub get_dependency_details {
                   10902:     my ($currfile,$currsubfile,$embed_file) = @_;
                   10903:     my ($size,$mtime,$showsize,$showmtime);
                   10904:     if ((ref($currfile) eq 'HASH') && (ref($currsubfile))) {
                   10905:         if ($embed_file =~ m{/}) {
                   10906:             my ($path,$fname) = split(/\//,$embed_file);
                   10907:             if (ref($currsubfile->{$path}{$fname}) eq 'ARRAY') {
                   10908:                 ($size,$mtime) = @{$currsubfile->{$path}{$fname}};
                   10909:             }
                   10910:         } else {
                   10911:             if (ref($currfile->{$embed_file}) eq 'ARRAY') {
                   10912:                 ($size,$mtime) = @{$currfile->{$embed_file}};
                   10913:             }
                   10914:         }
                   10915:         $showsize = $size/1024.0;
                   10916:         $showsize = sprintf("%.1f",$showsize);
                   10917:         if ($mtime > 0) {
                   10918:             $showmtime = &Apache::lonlocal::locallocaltime($mtime);
                   10919:         }
                   10920:     }
                   10921:     return ($showsize,$showmtime);
                   10922: }
                   10923: 
                   10924: sub ask_embedded_js {
                   10925:     return <<"END";
                   10926: <script type="text/javascript"">
                   10927: // <![CDATA[
                   10928: function toggleBrowse(counter) {
                   10929:     var chkboxid = document.getElementById('mod_upload_dep_'+counter);
                   10930:     var fileid = document.getElementById('embedded_item_'+counter);
                   10931:     var uploaddivid = document.getElementById('moduploaddep_'+counter);
                   10932:     if (chkboxid.checked == true) {
                   10933:         uploaddivid.style.display='block';
                   10934:     } else {
                   10935:         uploaddivid.style.display='none';
                   10936:         fileid.value = '';
                   10937:     }
                   10938: }
                   10939: // ]]>
                   10940: </script>
                   10941: 
                   10942: END
                   10943: }
                   10944: 
1.661     raeburn  10945: sub upload_embedded {
                   10946:     my ($context,$dirpath,$uname,$udom,$dir_root,$url_root,$group,$disk_quota,
1.987     raeburn  10947:         $current_disk_usage,$hiddenstate,$actionurl) = @_;
                   10948:     my (%pathchange,$output,$modifyform,$footer,$returnflag);
1.661     raeburn  10949:     for (my $i=0; $i<$env{'form.number_embedded_items'}; $i++) {
                   10950:         next if (!exists($env{'form.embedded_item_'.$i.'.filename'}));
                   10951:         my $orig_uploaded_filename =
                   10952:             $env{'form.embedded_item_'.$i.'.filename'};
1.987     raeburn  10953:         foreach my $type ('orig','ref','attrib','codebase') {
                   10954:             if ($env{'form.embedded_'.$type.'_'.$i} ne '') {
                   10955:                 $env{'form.embedded_'.$type.'_'.$i} =
                   10956:                     &unescape($env{'form.embedded_'.$type.'_'.$i});
                   10957:             }
                   10958:         }
1.661     raeburn  10959:         my ($path,$fname) =
                   10960:             ($env{'form.embedded_orig_'.$i} =~ m{(.*/)([^/]*)});
                   10961:         # no path, whole string is fname
                   10962:         if (!$fname) { $fname = $env{'form.embedded_orig_'.$i} };
                   10963:         $fname = &Apache::lonnet::clean_filename($fname);
                   10964:         # See if there is anything left
                   10965:         next if ($fname eq '');
                   10966: 
                   10967:         # Check if file already exists as a file or directory.
                   10968:         my ($state,$msg);
                   10969:         if ($context eq 'portfolio') {
                   10970:             my $port_path = $dirpath;
                   10971:             if ($group ne '') {
                   10972:                 $port_path = "groups/$group/$port_path";
                   10973:             }
1.987     raeburn  10974:             ($state,$msg) = &check_for_upload($env{'form.currentpath'}.$path,
                   10975:                                               $fname,$group,'embedded_item_'.$i,
1.661     raeburn  10976:                                               $dir_root,$port_path,$disk_quota,
                   10977:                                               $current_disk_usage,$uname,$udom);
                   10978:             if ($state eq 'will_exceed_quota'
1.984     raeburn  10979:                 || $state eq 'file_locked') {
1.661     raeburn  10980:                 $output .= $msg;
                   10981:                 next;
                   10982:             }
                   10983:         } elsif (($context eq 'author') || ($context eq 'testbank')) {
                   10984:             ($state,$msg) = &check_for_existing($path,$fname,'embedded_item_'.$i);
                   10985:             if ($state eq 'exists') {
                   10986:                 $output .= $msg;
                   10987:                 next;
                   10988:             }
                   10989:         }
                   10990:         # Check if extension is valid
                   10991:         if (($fname =~ /\.(\w+)$/) &&
                   10992:             (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.1155    bisitz   10993:             $output .= &mt('Invalid file extension ([_1]) - reserved for internal use.',$1)
                   10994:                       .' '.&mt('Rename the file with a different extension and re-upload.').'<br />';
1.661     raeburn  10995:             next;
                   10996:         } elsif (($fname =~ /\.(\w+)$/) &&
                   10997:                  (!defined(&Apache::loncommon::fileembstyle($1)))) {
1.987     raeburn  10998:             $output .= &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1).'<br />';
1.661     raeburn  10999:             next;
                   11000:         } elsif ($fname=~/\.(\d+)\.(\w+)$/) {
1.1120    bisitz   11001:             $output .= &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2).'<br />';
1.661     raeburn  11002:             next;
                   11003:         }
                   11004:         $env{'form.embedded_item_'.$i.'.filename'}=$fname;
1.1123    raeburn  11005:         my $subdir = $path;
                   11006:         $subdir =~ s{/+$}{};
1.661     raeburn  11007:         if ($context eq 'portfolio') {
1.984     raeburn  11008:             my $result;
                   11009:             if ($state eq 'existingfile') {
                   11010:                 $result=
                   11011:                     &Apache::lonnet::userfileupload('embedded_item_'.$i,'existingfile',
1.1123    raeburn  11012:                                                     $dirpath.$env{'form.currentpath'}.$subdir);
1.661     raeburn  11013:             } else {
1.984     raeburn  11014:                 $result=
                   11015:                     &Apache::lonnet::userfileupload('embedded_item_'.$i,'',
1.987     raeburn  11016:                                                     $dirpath.
1.1123    raeburn  11017:                                                     $env{'form.currentpath'}.$subdir);
1.984     raeburn  11018:                 if ($result !~ m|^/uploaded/|) {
                   11019:                     $output .= '<span class="LC_error">'
                   11020:                                .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
                   11021:                                ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
                   11022:                                .'</span><br />';
                   11023:                     next;
                   11024:                 } else {
1.987     raeburn  11025:                     $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                   11026:                                $path.$fname.'</span>').'<br />';     
1.984     raeburn  11027:                 }
1.661     raeburn  11028:             }
1.1123    raeburn  11029:         } elsif (($context eq 'coursedoc') || ($context eq 'syllabus')) {
1.1126    raeburn  11030:             my $extendedsubdir = $dirpath.'/'.$subdir;
                   11031:             $extendedsubdir =~ s{/+$}{};
1.987     raeburn  11032:             my $result =
1.1126    raeburn  11033:                 &Apache::lonnet::userfileupload('embedded_item_'.$i,$context,$extendedsubdir);
1.987     raeburn  11034:             if ($result !~ m|^/uploaded/|) {
                   11035:                 $output .= '<span class="LC_error">'
                   11036:                            .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
                   11037:                            ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
                   11038:                            .'</span><br />';
                   11039:                     next;
                   11040:             } else {
                   11041:                 $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                   11042:                            $path.$fname.'</span>').'<br />';
1.1125    raeburn  11043:                 if ($context eq 'syllabus') {
                   11044:                     &Apache::lonnet::make_public_indefinitely($result);
                   11045:                 }
1.987     raeburn  11046:             }
1.661     raeburn  11047:         } else {
                   11048: # Save the file
                   11049:             my $target = $env{'form.embedded_item_'.$i};
                   11050:             my $fullpath = $dir_root.$dirpath.'/'.$path;
                   11051:             my $dest = $fullpath.$fname;
                   11052:             my $url = $url_root.$dirpath.'/'.$path.$fname;
1.1027    raeburn  11053:             my @parts=split(/\//,"$dirpath/$path");
1.661     raeburn  11054:             my $count;
                   11055:             my $filepath = $dir_root;
1.1027    raeburn  11056:             foreach my $subdir (@parts) {
                   11057:                 $filepath .= "/$subdir";
                   11058:                 if (!-e $filepath) {
1.661     raeburn  11059:                     mkdir($filepath,0770);
                   11060:                 }
                   11061:             }
                   11062:             my $fh;
                   11063:             if (!open($fh,'>'.$dest)) {
                   11064:                 &Apache::lonnet::logthis('Failed to create '.$dest);
                   11065:                 $output .= '<span class="LC_error">'.
1.1071    raeburn  11066:                            &mt('An error occurred while trying to upload [_1] for embedded element [_2].',
                   11067:                                $orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
1.661     raeburn  11068:                            '</span><br />';
                   11069:             } else {
                   11070:                 if (!print $fh $env{'form.embedded_item_'.$i}) {
                   11071:                     &Apache::lonnet::logthis('Failed to write to '.$dest);
                   11072:                     $output .= '<span class="LC_error">'.
1.1071    raeburn  11073:                               &mt('An error occurred while writing the file [_1] for embedded element [_2].',
                   11074:                                   $orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
1.661     raeburn  11075:                               '</span><br />';
                   11076:                 } else {
1.987     raeburn  11077:                     $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                   11078:                                $url.'</span>').'<br />';
                   11079:                     unless ($context eq 'testbank') {
                   11080:                         $footer .= &mt('View embedded file: [_1]',
                   11081:                                        '<a href="'.$url.'">'.$fname.'</a>').'<br />';
                   11082:                     }
                   11083:                 }
                   11084:                 close($fh);
                   11085:             }
                   11086:         }
                   11087:         if ($env{'form.embedded_ref_'.$i}) {
                   11088:             $pathchange{$i} = 1;
                   11089:         }
                   11090:     }
                   11091:     if ($output) {
                   11092:         $output = '<p>'.$output.'</p>';
                   11093:     }
                   11094:     $output .= &modify_html_form('upload_embedded',$actionurl,$hiddenstate,\%pathchange);
                   11095:     $returnflag = 'ok';
1.1071    raeburn  11096:     my $numpathchgs = scalar(keys(%pathchange));
                   11097:     if ($numpathchgs > 0) {
1.987     raeburn  11098:         if ($context eq 'portfolio') {
                   11099:             $output .= '<p>'.&mt('or').'</p>';
                   11100:         } elsif ($context eq 'testbank') {
1.1071    raeburn  11101:             $output .=  '<p>'.&mt('Or [_1]continue[_2] the testbank import without modifying the reference(s).',
                   11102:                                   '<a href="javascript:document.testbankForm.submit();">','</a>').'</p>';
1.987     raeburn  11103:             $returnflag = 'modify_orightml';
                   11104:         }
                   11105:     }
1.1071    raeburn  11106:     return ($output.$footer,$returnflag,$numpathchgs);
1.987     raeburn  11107: }
                   11108: 
                   11109: sub modify_html_form {
                   11110:     my ($context,$actionurl,$hiddenstate,$pathchange,$pathchgtable) = @_;
                   11111:     my $end = 0;
                   11112:     my $modifyform;
                   11113:     if ($context eq 'upload_embedded') {
                   11114:         return unless (ref($pathchange) eq 'HASH');
                   11115:         if ($env{'form.number_embedded_items'}) {
                   11116:             $end += $env{'form.number_embedded_items'};
                   11117:         }
                   11118:         if ($env{'form.number_pathchange_items'}) {
                   11119:             $end += $env{'form.number_pathchange_items'};
                   11120:         }
                   11121:         if ($end) {
                   11122:             for (my $i=0; $i<$end; $i++) {
                   11123:                 if ($i < $env{'form.number_embedded_items'}) {
                   11124:                     next unless($pathchange->{$i});
                   11125:                 }
                   11126:                 $modifyform .=
                   11127:                     &start_data_table_row().
                   11128:                     '<td><input type ="checkbox" name="namechange" value="'.$i.'" '.
                   11129:                     'checked="checked" /></td>'.
                   11130:                     '<td>'.$env{'form.embedded_ref_'.$i}.
                   11131:                     '<input type="hidden" name="embedded_ref_'.$i.'" value="'.
                   11132:                     &escape($env{'form.embedded_ref_'.$i}).'" />'.
                   11133:                     '<input type="hidden" name="embedded_codebase_'.$i.'" value="'.
                   11134:                     &escape($env{'form.embedded_codebase_'.$i}).'" />'.
                   11135:                     '<input type="hidden" name="embedded_attrib_'.$i.'" value="'.
                   11136:                     &escape($env{'form.embedded_attrib_'.$i}).'" /></td>'.
                   11137:                     '<td>'.$env{'form.embedded_orig_'.$i}.
                   11138:                     '<input type="hidden" name="embedded_orig_'.$i.'" value="'.
                   11139:                     &escape($env{'form.embedded_orig_'.$i}).'" /></td>'.
                   11140:                     &end_data_table_row();
1.1071    raeburn  11141:             }
1.987     raeburn  11142:         }
                   11143:     } else {
                   11144:         $modifyform = $pathchgtable;
                   11145:         if (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
                   11146:             $hiddenstate .= '<input type="hidden" name="phase" value="four" />';
                   11147:         } elsif (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
                   11148:             $hiddenstate .= '<input type="hidden" name="action" value="modify_orightml" />';
                   11149:         }
                   11150:     }
                   11151:     if ($modifyform) {
1.1071    raeburn  11152:         if ($actionurl eq '/adm/dependencies') {
                   11153:             $hiddenstate .= '<input type="hidden" name="action" value="modifyhrefs" />';
                   11154:         }
1.987     raeburn  11155:         return '<h3>'.&mt('Changes in content of HTML file required').'</h3>'."\n".
                   11156:                '<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".
                   11157:                '<li>'.&mt('For consistency between the reference(s) and the location of the corresponding stored file within LON-CAPA.').'</li>'."\n".
                   11158:                '<li>'.&mt('To change absolute paths to relative paths, or replace directory traversal via "../" within the original reference.').'</li>'."\n".
                   11159:                '</ol></p>'."\n".'<p>'.
                   11160:                &mt('LON-CAPA can make the required changes to your HTML file.').'</p>'."\n".
                   11161:                '<form method="post" name="refchanger" action="'.$actionurl.'">'.
                   11162:                &start_data_table()."\n".
                   11163:                &start_data_table_header_row().
                   11164:                '<th>'.&mt('Change?').'</th>'.
                   11165:                '<th>'.&mt('Current reference').'</th>'.
                   11166:                '<th>'.&mt('Required reference').'</th>'.
                   11167:                &end_data_table_header_row()."\n".
                   11168:                $modifyform.
                   11169:                &end_data_table().'<br />'."\n".$hiddenstate.
                   11170:                '<input type="submit" name="pathchanges" value="'.&mt('Modify HTML file').'" />'.
                   11171:                '</form>'."\n";
                   11172:     }
                   11173:     return;
                   11174: }
                   11175: 
                   11176: sub modify_html_refs {
1.1123    raeburn  11177:     my ($context,$dirpath,$uname,$udom,$dir_root,$url) = @_;
1.987     raeburn  11178:     my $container;
                   11179:     if ($context eq 'portfolio') {
                   11180:         $container = $env{'form.container'};
                   11181:     } elsif ($context eq 'coursedoc') {
                   11182:         $container = $env{'form.primaryurl'};
1.1071    raeburn  11183:     } elsif ($context eq 'manage_dependencies') {
                   11184:         (undef,undef,$container) = &Apache::lonnet::decode_symb($env{'form.symb'});
                   11185:         $container = "/$container";
1.1123    raeburn  11186:     } elsif ($context eq 'syllabus') {
                   11187:         $container = $url;
1.987     raeburn  11188:     } else {
1.1027    raeburn  11189:         $container = $Apache::lonnet::perlvar{'lonDocRoot'}.$env{'form.filename'};
1.987     raeburn  11190:     }
                   11191:     my (%allfiles,%codebase,$output,$content);
                   11192:     my @changes = &get_env_multiple('form.namechange');
1.1126    raeburn  11193:     unless ((@changes > 0) || ($context eq 'syllabus')) {
1.1071    raeburn  11194:         if (wantarray) {
                   11195:             return ('',0,0); 
                   11196:         } else {
                   11197:             return;
                   11198:         }
                   11199:     }
                   11200:     if (($context eq 'portfolio') || ($context eq 'coursedoc') || 
1.1123    raeburn  11201:         ($context eq 'manage_dependencies') || ($context eq 'syllabus')) {
1.1071    raeburn  11202:         unless ($container =~ m{^/uploaded/\Q$udom\E/\Q$uname\E/}) {
                   11203:             if (wantarray) {
                   11204:                 return ('',0,0);
                   11205:             } else {
                   11206:                 return;
                   11207:             }
                   11208:         } 
1.987     raeburn  11209:         $content = &Apache::lonnet::getfile($container);
1.1071    raeburn  11210:         if ($content eq '-1') {
                   11211:             if (wantarray) {
                   11212:                 return ('',0,0);
                   11213:             } else {
                   11214:                 return;
                   11215:             }
                   11216:         }
1.987     raeburn  11217:     } else {
1.1071    raeburn  11218:         unless ($container =~ /^\Q$dir_root\E/) {
                   11219:             if (wantarray) {
                   11220:                 return ('',0,0);
                   11221:             } else {
                   11222:                 return;
                   11223:             }
                   11224:         } 
1.987     raeburn  11225:         if (open(my $fh,"<$container")) {
                   11226:             $content = join('', <$fh>);
                   11227:             close($fh);
                   11228:         } else {
1.1071    raeburn  11229:             if (wantarray) {
                   11230:                 return ('',0,0);
                   11231:             } else {
                   11232:                 return;
                   11233:             }
1.987     raeburn  11234:         }
                   11235:     }
                   11236:     my ($count,$codebasecount) = (0,0);
                   11237:     my $mm = new File::MMagic;
                   11238:     my $mime_type = $mm->checktype_contents($content);
                   11239:     if ($mime_type eq 'text/html') {
                   11240:         my $parse_result = 
                   11241:             &Apache::lonnet::extract_embedded_items($container,\%allfiles,
                   11242:                                                     \%codebase,\$content);
                   11243:         if ($parse_result eq 'ok') {
                   11244:             foreach my $i (@changes) {
                   11245:                 my $orig = &unescape($env{'form.embedded_orig_'.$i});
                   11246:                 my $ref = &unescape($env{'form.embedded_ref_'.$i});
                   11247:                 if ($allfiles{$ref}) {
                   11248:                     my $newname =  $orig;
                   11249:                     my ($attrib_regexp,$codebase);
1.1006    raeburn  11250:                     $attrib_regexp = &unescape($env{'form.embedded_attrib_'.$i});
1.987     raeburn  11251:                     if ($attrib_regexp =~ /:/) {
                   11252:                         $attrib_regexp =~ s/\:/|/g;
                   11253:                     }
                   11254:                     if ($content =~ m{($attrib_regexp\s*=\s*['"]?)\Q$ref\E(['"]?)}) {
                   11255:                         my $numchg = ($content =~ s{($attrib_regexp\s*=\s*['"]?)\Q$ref\E(['"]?)}{$1$newname$2}gi);
                   11256:                         $count += $numchg;
1.1123    raeburn  11257:                         $allfiles{$newname} = $allfiles{$ref};
1.1148    raeburn  11258:                         delete($allfiles{$ref});
1.987     raeburn  11259:                     }
                   11260:                     if ($env{'form.embedded_codebase_'.$i} ne '') {
1.1006    raeburn  11261:                         $codebase = &unescape($env{'form.embedded_codebase_'.$i});
1.987     raeburn  11262:                         my $numchg = ($content =~ s/(codebase\s*=\s*["']?)\Q$codebase\E(["']?)/$1.$2/i); #' stupid emacs
                   11263:                         $codebasecount ++;
                   11264:                     }
                   11265:                 }
                   11266:             }
1.1123    raeburn  11267:             my $skiprewrites;
1.987     raeburn  11268:             if ($count || $codebasecount) {
                   11269:                 my $saveresult;
1.1071    raeburn  11270:                 if (($context eq 'portfolio') || ($context eq 'coursedoc') || 
1.1123    raeburn  11271:                     ($context eq 'manage_dependencies') || ($context eq 'syllabus')) {
1.987     raeburn  11272:                     my $url = &Apache::lonnet::store_edited_file($container,$content,$udom,$uname,\$saveresult);
                   11273:                     if ($url eq $container) {
                   11274:                         my ($fname) = ($container =~ m{/([^/]+)$});
                   11275:                         $output = '<p>'.&mt('Updated [quant,_1,reference] in [_2].',
                   11276:                                             $count,'<span class="LC_filename">'.
1.1071    raeburn  11277:                                             $fname.'</span>').'</p>';
1.987     raeburn  11278:                     } else {
                   11279:                          $output = '<p class="LC_error">'.
                   11280:                                    &mt('Error: update failed for: [_1].',
                   11281:                                    '<span class="LC_filename">'.
                   11282:                                    $container.'</span>').'</p>';
                   11283:                     }
1.1123    raeburn  11284:                     if ($context eq 'syllabus') {
                   11285:                         unless ($saveresult eq 'ok') {
                   11286:                             $skiprewrites = 1;
                   11287:                         }
                   11288:                     }
1.987     raeburn  11289:                 } else {
                   11290:                     if (open(my $fh,">$container")) {
                   11291:                         print $fh $content;
                   11292:                         close($fh);
                   11293:                         $output = '<p>'.&mt('Updated [quant,_1,reference] in [_2].',
                   11294:                                   $count,'<span class="LC_filename">'.
                   11295:                                   $container.'</span>').'</p>';
1.661     raeburn  11296:                     } else {
1.987     raeburn  11297:                          $output = '<p class="LC_error">'.
                   11298:                                    &mt('Error: could not update [_1].',
                   11299:                                    '<span class="LC_filename">'.
                   11300:                                    $container.'</span>').'</p>';
1.661     raeburn  11301:                     }
                   11302:                 }
                   11303:             }
1.1123    raeburn  11304:             if (($context eq 'syllabus') && (!$skiprewrites)) {
                   11305:                 my ($actionurl,$state);
                   11306:                 $actionurl = "/public/$udom/$uname/syllabus";
                   11307:                 my ($ignore,$num,$numpathchanges,$existing,$mapping) =
                   11308:                     &ask_for_embedded_content($actionurl,$state,\%allfiles,
                   11309:                                               \%codebase,
                   11310:                                               {'context' => 'rewrites',
                   11311:                                                'ignore_remote_references' => 1,});
                   11312:                 if (ref($mapping) eq 'HASH') {
                   11313:                     my $rewrites = 0;
                   11314:                     foreach my $key (keys(%{$mapping})) {
                   11315:                         next if ($key =~ m{^https?://});
                   11316:                         my $ref = $mapping->{$key};
                   11317:                         my $newname = "/uploaded/$udom/$uname/portfolio/syllabus/$key";
                   11318:                         my $attrib;
                   11319:                         if (ref($allfiles{$mapping->{$key}}) eq 'ARRAY') {
                   11320:                             $attrib = join('|',@{$allfiles{$mapping->{$key}}});
                   11321:                         }
                   11322:                         if ($content =~ m{($attrib\s*=\s*['"]?)\Q$ref\E(['"]?)}) {
                   11323:                             my $numchg = ($content =~ s{($attrib\s*=\s*['"]?)\Q$ref\E(['"]?)}{$1$newname$2}gi);
                   11324:                             $rewrites += $numchg;
                   11325:                         }
                   11326:                     }
                   11327:                     if ($rewrites) {
                   11328:                         my $saveresult; 
                   11329:                         my $url = &Apache::lonnet::store_edited_file($container,$content,$udom,$uname,\$saveresult);
                   11330:                         if ($url eq $container) {
                   11331:                             my ($fname) = ($container =~ m{/([^/]+)$});
                   11332:                             $output .= '<p>'.&mt('Rewrote [quant,_1,link] as [quant,_1,absolute link] in [_2].',
                   11333:                                             $count,'<span class="LC_filename">'.
                   11334:                                             $fname.'</span>').'</p>';
                   11335:                         } else {
                   11336:                             $output .= '<p class="LC_error">'.
                   11337:                                        &mt('Error: could not update links in [_1].',
                   11338:                                        '<span class="LC_filename">'.
                   11339:                                        $container.'</span>').'</p>';
                   11340: 
                   11341:                         }
                   11342:                     }
                   11343:                 }
                   11344:             }
1.987     raeburn  11345:         } else {
                   11346:             &logthis('Failed to parse '.$container.
                   11347:                      ' to modify references: '.$parse_result);
1.661     raeburn  11348:         }
                   11349:     }
1.1071    raeburn  11350:     if (wantarray) {
                   11351:         return ($output,$count,$codebasecount);
                   11352:     } else {
                   11353:         return $output;
                   11354:     }
1.661     raeburn  11355: }
                   11356: 
                   11357: sub check_for_existing {
                   11358:     my ($path,$fname,$element) = @_;
                   11359:     my ($state,$msg);
                   11360:     if (-d $path.'/'.$fname) {
                   11361:         $state = 'exists';
                   11362:         $msg = &mt('Unable to upload [_1]. A directory by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
                   11363:     } elsif (-e $path.'/'.$fname) {
                   11364:         $state = 'exists';
                   11365:         $msg = &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
                   11366:     }
                   11367:     if ($state eq 'exists') {
                   11368:         $msg = '<span class="LC_error">'.$msg.'</span><br />';
                   11369:     }
                   11370:     return ($state,$msg);
                   11371: }
                   11372: 
                   11373: sub check_for_upload {
                   11374:     my ($path,$fname,$group,$element,$portfolio_root,$port_path,
                   11375:         $disk_quota,$current_disk_usage,$uname,$udom) = @_;
1.985     raeburn  11376:     my $filesize = length($env{'form.'.$element});
                   11377:     if (!$filesize) {
                   11378:         my $msg = '<span class="LC_error">'.
                   11379:                   &mt('Unable to upload [_1]. (size = [_2] bytes)', 
                   11380:                       '<span class="LC_filename">'.$fname.'</span>',
                   11381:                       $filesize).'<br />'.
1.1007    raeburn  11382:                   &mt('Either the file you attempted to upload was empty, or your web browser was unable to read its contents.').'<br />'.
1.985     raeburn  11383:                   '</span>';
                   11384:         return ('zero_bytes',$msg);
                   11385:     }
                   11386:     $filesize =  $filesize/1000; #express in k (1024?)
1.661     raeburn  11387:     my $getpropath = 1;
1.1021    raeburn  11388:     my ($dirlistref,$listerror) =
                   11389:          &Apache::lonnet::dirlist($portfolio_root.$path,$udom,$uname,$getpropath);
1.661     raeburn  11390:     my $found_file = 0;
                   11391:     my $locked_file = 0;
1.991     raeburn  11392:     my @lockers;
                   11393:     my $navmap;
                   11394:     if ($env{'request.course.id'}) {
                   11395:         $navmap = Apache::lonnavmaps::navmap->new();
                   11396:     }
1.1021    raeburn  11397:     if (ref($dirlistref) eq 'ARRAY') {
                   11398:         foreach my $line (@{$dirlistref}) {
                   11399:             my ($file_name,$rest)=split(/\&/,$line,2);
                   11400:             if ($file_name eq $fname){
                   11401:                 $file_name = $path.$file_name;
                   11402:                 if ($group ne '') {
                   11403:                     $file_name = $group.$file_name;
                   11404:                 }
                   11405:                 $found_file = 1;
                   11406:                 if (&Apache::lonnet::is_locked($file_name,$udom,$uname,\@lockers) eq 'true') {
                   11407:                     foreach my $lock (@lockers) {
                   11408:                         if (ref($lock) eq 'ARRAY') {
                   11409:                             my ($symb,$crsid) = @{$lock};
                   11410:                             if ($crsid eq $env{'request.course.id'}) {
                   11411:                                 if (ref($navmap)) {
                   11412:                                     my $res = $navmap->getBySymb($symb);
                   11413:                                     foreach my $part (@{$res->parts()}) { 
                   11414:                                         my ($slot_status,$slot_time,$slot_name)=$res->check_for_slot($part);
                   11415:                                         unless (($slot_status == $res->RESERVED) ||
                   11416:                                                 ($slot_status == $res->RESERVED_LOCATION)) {
                   11417:                                             $locked_file = 1;
                   11418:                                         }
1.991     raeburn  11419:                                     }
1.1021    raeburn  11420:                                 } else {
                   11421:                                     $locked_file = 1;
1.991     raeburn  11422:                                 }
                   11423:                             } else {
                   11424:                                 $locked_file = 1;
                   11425:                             }
                   11426:                         }
1.1021    raeburn  11427:                    }
                   11428:                 } else {
                   11429:                     my @info = split(/\&/,$rest);
                   11430:                     my $currsize = $info[6]/1000;
                   11431:                     if ($currsize < $filesize) {
                   11432:                         my $extra = $filesize - $currsize;
                   11433:                         if (($current_disk_usage + $extra) > $disk_quota) {
1.1179    bisitz   11434:                             my $msg = '<p class="LC_warning">'.
1.1021    raeburn  11435:                                       &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.',
1.1179    bisitz   11436:                                           '<span class="LC_filename">'.$fname.'</span>',$filesize,$currsize).'</p>'.
                   11437:                                       '<p>'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',
                   11438:                                                    $disk_quota,$current_disk_usage).'</p>';
1.1021    raeburn  11439:                             return ('will_exceed_quota',$msg);
                   11440:                         }
1.984     raeburn  11441:                     }
                   11442:                 }
1.661     raeburn  11443:             }
                   11444:         }
                   11445:     }
                   11446:     if (($current_disk_usage + $filesize) > $disk_quota){
1.1179    bisitz   11447:         my $msg = '<p class="LC_warning">'.
                   11448:                 &mt('Unable to upload [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.','<span class="LC_filename">'.$fname.'</span>',$filesize).'</p>'.
1.1184    raeburn  11449:                   '<p>'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',$disk_quota,$current_disk_usage).'</p>';
1.661     raeburn  11450:         return ('will_exceed_quota',$msg);
                   11451:     } elsif ($found_file) {
                   11452:         if ($locked_file) {
1.1179    bisitz   11453:             my $msg = '<p class="LC_warning">';
1.661     raeburn  11454:             $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>');
1.1179    bisitz   11455:             $msg .= '</p>';
1.661     raeburn  11456:             $msg .= &mt('You will be able to rename or delete existing [_1] after a grade has been assigned.','<span class="LC_filename">'.$fname.'</span>');
                   11457:             return ('file_locked',$msg);
                   11458:         } else {
1.1179    bisitz   11459:             my $msg = '<p class="LC_error">';
1.984     raeburn  11460:             $msg .= &mt(' A file by that name: [_1] was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$port_path.$env{'form.currentpath'});
1.1179    bisitz   11461:             $msg .= '</p>';
1.984     raeburn  11462:             return ('existingfile',$msg);
1.661     raeburn  11463:         }
                   11464:     }
                   11465: }
                   11466: 
1.987     raeburn  11467: sub check_for_traversal {
                   11468:     my ($path,$url,$toplevel) = @_;
                   11469:     my @parts=split(/\//,$path);
                   11470:     my $cleanpath;
                   11471:     my $fullpath = $url;
                   11472:     for (my $i=0;$i<@parts;$i++) {
                   11473:         next if ($parts[$i] eq '.');
                   11474:         if ($parts[$i] eq '..') {
                   11475:             $fullpath =~ s{([^/]+/)$}{};
                   11476:         } else {
                   11477:             $fullpath .= $parts[$i].'/';
                   11478:         }
                   11479:     }
                   11480:     if ($fullpath =~ /^\Q$url\E(.*)$/) {
                   11481:         $cleanpath = $1;
                   11482:     } elsif ($fullpath =~ /^\Q$toplevel\E(.*)$/) {
                   11483:         my $curr_toprel = $1;
                   11484:         my @parts = split(/\//,$curr_toprel);
                   11485:         my ($url_toprel) = ($url =~ /^\Q$toplevel\E(.*)$/);
                   11486:         my @urlparts = split(/\//,$url_toprel);
                   11487:         my $doubledots;
                   11488:         my $startdiff = -1;
                   11489:         for (my $i=0; $i<@urlparts; $i++) {
                   11490:             if ($startdiff == -1) {
                   11491:                 unless ($urlparts[$i] eq $parts[$i]) {
                   11492:                     $startdiff = $i;
                   11493:                     $doubledots .= '../';
                   11494:                 }
                   11495:             } else {
                   11496:                 $doubledots .= '../';
                   11497:             }
                   11498:         }
                   11499:         if ($startdiff > -1) {
                   11500:             $cleanpath = $doubledots;
                   11501:             for (my $i=$startdiff; $i<@parts; $i++) {
                   11502:                 $cleanpath .= $parts[$i].'/';
                   11503:             }
                   11504:         }
                   11505:     }
                   11506:     $cleanpath =~ s{(/)$}{};
                   11507:     return $cleanpath;
                   11508: }
1.31      albertel 11509: 
1.1053    raeburn  11510: sub is_archive_file {
                   11511:     my ($mimetype) = @_;
                   11512:     if (($mimetype eq 'application/octet-stream') ||
                   11513:         ($mimetype eq 'application/x-stuffit') ||
                   11514:         ($mimetype =~ m{^application/(x\-)?(compressed|tar|zip|tgz|gz|gtar|gzip|gunzip|bz|bz2|bzip2)})) {
                   11515:         return 1;
                   11516:     }
                   11517:     return;
                   11518: }
                   11519: 
                   11520: sub decompress_form {
1.1065    raeburn  11521:     my ($mimetype,$archiveurl,$action,$noextract,$hiddenelements,$dirlist) = @_;
1.1053    raeburn  11522:     my %lt = &Apache::lonlocal::texthash (
                   11523:         this => 'This file is an archive file.',
1.1067    raeburn  11524:         camt => 'This file is a Camtasia archive file.',
1.1065    raeburn  11525:         itsc => 'Its contents are as follows:',
1.1053    raeburn  11526:         youm => 'You may wish to extract its contents.',
                   11527:         extr => 'Extract contents',
1.1067    raeburn  11528:         auto => 'LON-CAPA can process the files automatically, or you can decide how each should be handled.',
                   11529:         proa => 'Process automatically?',
1.1053    raeburn  11530:         yes  => 'Yes',
                   11531:         no   => 'No',
1.1067    raeburn  11532:         fold => 'Title for folder containing movie',
                   11533:         movi => 'Title for page containing embedded movie', 
1.1053    raeburn  11534:     );
1.1065    raeburn  11535:     my $fileloc = &Apache::lonnet::filelocation(undef,$archiveurl);
1.1067    raeburn  11536:     my ($is_camtasia,$topdir,%toplevel,@paths);
1.1065    raeburn  11537:     my $info = &list_archive_contents($fileloc,\@paths);
                   11538:     if (@paths) {
                   11539:         foreach my $path (@paths) {
                   11540:             $path =~ s{^/}{};
1.1067    raeburn  11541:             if ($path =~ m{^([^/]+)/$}) {
                   11542:                 $topdir = $1;
                   11543:             }
1.1065    raeburn  11544:             if ($path =~ m{^([^/]+)/}) {
                   11545:                 $toplevel{$1} = $path;
                   11546:             } else {
                   11547:                 $toplevel{$path} = $path;
                   11548:             }
                   11549:         }
                   11550:     }
1.1067    raeburn  11551:     if ($mimetype =~ m{^application/(x\-)?(compressed|zip)}) {
1.1164    raeburn  11552:         my @camtasia6 = ("$topdir/","$topdir/index.html",
1.1067    raeburn  11553:                         "$topdir/media/",
                   11554:                         "$topdir/media/$topdir.mp4",
                   11555:                         "$topdir/media/FirstFrame.png",
                   11556:                         "$topdir/media/player.swf",
                   11557:                         "$topdir/media/swfobject.js",
                   11558:                         "$topdir/media/expressInstall.swf");
1.1197    raeburn  11559:         my @camtasia8_1 = ("$topdir/","$topdir/$topdir.html",
1.1164    raeburn  11560:                          "$topdir/$topdir.mp4",
                   11561:                          "$topdir/$topdir\_config.xml",
                   11562:                          "$topdir/$topdir\_controller.swf",
                   11563:                          "$topdir/$topdir\_embed.css",
                   11564:                          "$topdir/$topdir\_First_Frame.png",
                   11565:                          "$topdir/$topdir\_player.html",
                   11566:                          "$topdir/$topdir\_Thumbnails.png",
                   11567:                          "$topdir/playerProductInstall.swf",
                   11568:                          "$topdir/scripts/",
                   11569:                          "$topdir/scripts/config_xml.js",
                   11570:                          "$topdir/scripts/handlebars.js",
                   11571:                          "$topdir/scripts/jquery-1.7.1.min.js",
                   11572:                          "$topdir/scripts/jquery-ui-1.8.15.custom.min.js",
                   11573:                          "$topdir/scripts/modernizr.js",
                   11574:                          "$topdir/scripts/player-min.js",
                   11575:                          "$topdir/scripts/swfobject.js",
                   11576:                          "$topdir/skins/",
                   11577:                          "$topdir/skins/configuration_express.xml",
                   11578:                          "$topdir/skins/express_show/",
                   11579:                          "$topdir/skins/express_show/player-min.css",
                   11580:                          "$topdir/skins/express_show/spritesheet.png");
1.1197    raeburn  11581:         my @camtasia8_4 = ("$topdir/","$topdir/$topdir.html",
                   11582:                          "$topdir/$topdir.mp4",
                   11583:                          "$topdir/$topdir\_config.xml",
                   11584:                          "$topdir/$topdir\_controller.swf",
                   11585:                          "$topdir/$topdir\_embed.css",
                   11586:                          "$topdir/$topdir\_First_Frame.png",
                   11587:                          "$topdir/$topdir\_player.html",
                   11588:                          "$topdir/$topdir\_Thumbnails.png",
                   11589:                          "$topdir/playerProductInstall.swf",
                   11590:                          "$topdir/scripts/",
                   11591:                          "$topdir/scripts/config_xml.js",
                   11592:                          "$topdir/scripts/techsmith-smart-player.min.js",
                   11593:                          "$topdir/skins/",
                   11594:                          "$topdir/skins/configuration_express.xml",
                   11595:                          "$topdir/skins/express_show/",
                   11596:                          "$topdir/skins/express_show/spritesheet.min.css",
                   11597:                          "$topdir/skins/express_show/spritesheet.png",
                   11598:                          "$topdir/skins/express_show/techsmith-smart-player.min.css");
1.1164    raeburn  11599:         my @diffs = &compare_arrays(\@paths,\@camtasia6);
1.1067    raeburn  11600:         if (@diffs == 0) {
1.1164    raeburn  11601:             $is_camtasia = 6;
                   11602:         } else {
1.1197    raeburn  11603:             @diffs = &compare_arrays(\@paths,\@camtasia8_1);
1.1164    raeburn  11604:             if (@diffs == 0) {
                   11605:                 $is_camtasia = 8;
1.1197    raeburn  11606:             } else {
                   11607:                 @diffs = &compare_arrays(\@paths,\@camtasia8_4);
                   11608:                 if (@diffs == 0) {
                   11609:                     $is_camtasia = 8;
                   11610:                 }
1.1164    raeburn  11611:             }
1.1067    raeburn  11612:         }
                   11613:     }
                   11614:     my $output;
                   11615:     if ($is_camtasia) {
                   11616:         $output = <<"ENDCAM";
                   11617: <script type="text/javascript" language="Javascript">
                   11618: // <![CDATA[
                   11619: 
                   11620: function camtasiaToggle() {
                   11621:     for (var i=0; i<document.uploaded_decompress.autoextract_camtasia.length; i++) {
                   11622:         if (document.uploaded_decompress.autoextract_camtasia[i].checked) {
1.1164    raeburn  11623:             if (document.uploaded_decompress.autoextract_camtasia[i].value == $is_camtasia) {
1.1067    raeburn  11624:                 document.getElementById('camtasia_titles').style.display='block';
                   11625:             } else {
                   11626:                 document.getElementById('camtasia_titles').style.display='none';
                   11627:             }
                   11628:         }
                   11629:     }
                   11630:     return;
                   11631: }
                   11632: 
                   11633: // ]]>
                   11634: </script>
                   11635: <p>$lt{'camt'}</p>
                   11636: ENDCAM
1.1065    raeburn  11637:     } else {
1.1067    raeburn  11638:         $output = '<p>'.$lt{'this'};
                   11639:         if ($info eq '') {
                   11640:             $output .= ' '.$lt{'youm'}.'</p>'."\n";
                   11641:         } else {
                   11642:             $output .= ' '.$lt{'itsc'}.'</p>'."\n".
                   11643:                        '<div><pre>'.$info.'</pre></div>';
                   11644:         }
1.1065    raeburn  11645:     }
1.1067    raeburn  11646:     $output .= '<form name="uploaded_decompress" action="'.$action.'" method="post">'."\n";
1.1065    raeburn  11647:     my $duplicates;
                   11648:     my $num = 0;
                   11649:     if (ref($dirlist) eq 'ARRAY') {
                   11650:         foreach my $item (@{$dirlist}) {
                   11651:             if (ref($item) eq 'ARRAY') {
                   11652:                 if (exists($toplevel{$item->[0]})) {
                   11653:                     $duplicates .= 
                   11654:                         &start_data_table_row().
                   11655:                         '<td><label><input type="radio" name="archive_overwrite_'.$num.'" '.
                   11656:                         'value="0" checked="checked" />'.&mt('No').'</label>'.
                   11657:                         '&nbsp;<label><input type="radio" name="archive_overwrite_'.$num.'" '.
                   11658:                         'value="1" />'.&mt('Yes').'</label>'.
                   11659:                         '<input type="hidden" name="archive_overwrite_name_'.$num.'" value="'.$item->[0].'" /></td>'."\n".
                   11660:                         '<td>'.$item->[0].'</td>';
                   11661:                     if ($item->[2]) {
                   11662:                         $duplicates .= '<td>'.&mt('Directory').'</td>';
                   11663:                     } else {
                   11664:                         $duplicates .= '<td>'.&mt('File').'</td>';
                   11665:                     }
                   11666:                     $duplicates .= '<td>'.$item->[3].'</td>'.
                   11667:                                    '<td>'.
                   11668:                                    &Apache::lonlocal::locallocaltime($item->[4]).
                   11669:                                    '</td>'.
                   11670:                                    &end_data_table_row();
                   11671:                     $num ++;
                   11672:                 }
                   11673:             }
                   11674:         }
                   11675:     }
                   11676:     my $itemcount;
                   11677:     if (@paths > 0) {
                   11678:         $itemcount = scalar(@paths);
                   11679:     } else {
                   11680:         $itemcount = 1;
                   11681:     }
1.1067    raeburn  11682:     if ($is_camtasia) {
                   11683:         $output .= $lt{'auto'}.'<br />'.
                   11684:                    '<span class="LC_nobreak">'.$lt{'proa'}.'<label>'.
1.1164    raeburn  11685:                    '<input type="radio" name="autoextract_camtasia" value="'.$is_camtasia.'" onclick="javascript:camtasiaToggle();" checked="checked" />'.
1.1067    raeburn  11686:                    $lt{'yes'}.'</label>&nbsp;<label>'.
                   11687:                    '<input type="radio" name="autoextract_camtasia" value="0" onclick="javascript:camtasiaToggle();" />'.
                   11688:                    $lt{'no'}.'</label></span><br />'.
                   11689:                    '<div id="camtasia_titles" style="display:block">'.
                   11690:                    &Apache::lonhtmlcommon::start_pick_box().
                   11691:                    &Apache::lonhtmlcommon::row_title($lt{'fold'}).
                   11692:                    '<input type="textbox" name="camtasia_foldername" value="'.$env{'form.comment'}.'" />'."\n".
                   11693:                    &Apache::lonhtmlcommon::row_closure().
                   11694:                    &Apache::lonhtmlcommon::row_title($lt{'movi'}).
                   11695:                    '<input type="textbox" name="camtasia_moviename" value="" />'."\n".
                   11696:                    &Apache::lonhtmlcommon::row_closure(1).
                   11697:                    &Apache::lonhtmlcommon::end_pick_box().
                   11698:                    '</div>';
                   11699:     }
1.1065    raeburn  11700:     $output .= 
                   11701:         '<input type="hidden" name="archive_overwrite_total" value="'.$num.'" />'.
1.1067    raeburn  11702:         '<input type="hidden" name="archive_itemcount" value="'.$itemcount.'" />'.
                   11703:         "\n";
1.1065    raeburn  11704:     if ($duplicates ne '') {
                   11705:         $output .= '<p><span class="LC_warning">'.
                   11706:                    &mt('Warning: decompression of the archive will overwrite the following items which already exist:').'</span><br />'.  
                   11707:                    &start_data_table().
                   11708:                    &start_data_table_header_row().
                   11709:                    '<th>'.&mt('Overwrite?').'</th>'.
                   11710:                    '<th>'.&mt('Name').'</th>'.
                   11711:                    '<th>'.&mt('Type').'</th>'.
                   11712:                    '<th>'.&mt('Size').'</th>'.
                   11713:                    '<th>'.&mt('Last modified').'</th>'.
                   11714:                    &end_data_table_header_row().
                   11715:                    $duplicates.
                   11716:                    &end_data_table().
                   11717:                    '</p>';
                   11718:     }
1.1067    raeburn  11719:     $output .= '<input type="hidden" name="archiveurl" value="'.$archiveurl.'" />'."\n";
1.1053    raeburn  11720:     if (ref($hiddenelements) eq 'HASH') {
                   11721:         foreach my $hidden (sort(keys(%{$hiddenelements}))) {
                   11722:             $output .= '<input type="hidden" name="'.$hidden.'" value="'.$hiddenelements->{$hidden}.'" />'."\n";
                   11723:         }
                   11724:     }
                   11725:     $output .= <<"END";
1.1067    raeburn  11726: <br />
1.1053    raeburn  11727: <input type="submit" name="decompress" value="$lt{'extr'}" />
                   11728: </form>
                   11729: $noextract
                   11730: END
                   11731:     return $output;
                   11732: }
                   11733: 
1.1065    raeburn  11734: sub decompression_utility {
                   11735:     my ($program) = @_;
                   11736:     my @utilities = ('tar','gunzip','bunzip2','unzip'); 
                   11737:     my $location;
                   11738:     if (grep(/^\Q$program\E$/,@utilities)) { 
                   11739:         foreach my $dir ('/bin/','/usr/bin/','/usr/local/bin/','/sbin/',
                   11740:                          '/usr/sbin/') {
                   11741:             if (-x $dir.$program) {
                   11742:                 $location = $dir.$program;
                   11743:                 last;
                   11744:             }
                   11745:         }
                   11746:     }
                   11747:     return $location;
                   11748: }
                   11749: 
                   11750: sub list_archive_contents {
                   11751:     my ($file,$pathsref) = @_;
                   11752:     my (@cmd,$output);
                   11753:     my $needsregexp;
                   11754:     if ($file =~ /\.zip$/) {
                   11755:         @cmd = (&decompression_utility('unzip'),"-l");
                   11756:         $needsregexp = 1;
                   11757:     } elsif (($file =~ m/\.tar\.gz$/) ||
                   11758:              ($file =~ /\.tgz$/)) {
                   11759:         @cmd = (&decompression_utility('tar'),"-ztf");
                   11760:     } elsif ($file =~ /\.tar\.bz2$/) {
                   11761:         @cmd = (&decompression_utility('tar'),"-jtf");
                   11762:     } elsif ($file =~ m|\.tar$|) {
                   11763:         @cmd = (&decompression_utility('tar'),"-tf");
                   11764:     }
                   11765:     if (@cmd) {
                   11766:         undef($!);
                   11767:         undef($@);
                   11768:         if (open(my $fh,"-|", @cmd, $file)) {
                   11769:             while (my $line = <$fh>) {
                   11770:                 $output .= $line;
                   11771:                 chomp($line);
                   11772:                 my $item;
                   11773:                 if ($needsregexp) {
                   11774:                     ($item) = ($line =~ /^\s*\d+\s+[\d\-]+\s+[\d:]+\s*(.+)$/); 
                   11775:                 } else {
                   11776:                     $item = $line;
                   11777:                 }
                   11778:                 if ($item ne '') {
                   11779:                     unless (grep(/^\Q$item\E$/,@{$pathsref})) {
                   11780:                         push(@{$pathsref},$item);
                   11781:                     } 
                   11782:                 }
                   11783:             }
                   11784:             close($fh);
                   11785:         }
                   11786:     }
                   11787:     return $output;
                   11788: }
                   11789: 
1.1053    raeburn  11790: sub decompress_uploaded_file {
                   11791:     my ($file,$dir) = @_;
                   11792:     &Apache::lonnet::appenv({'cgi.file' => $file});
                   11793:     &Apache::lonnet::appenv({'cgi.dir' => $dir});
                   11794:     my $result = &Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
                   11795:     my ($handle) = ($env{'user.environment'} =~m{/([^/]+)\.id$});
                   11796:     my $lonidsdir = $Apache::lonnet::perlvar{'lonIDsDir'};
                   11797:     &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle,1);
                   11798:     my $decompressed = $env{'cgi.decompressed'};
                   11799:     &Apache::lonnet::delenv('cgi.file');
                   11800:     &Apache::lonnet::delenv('cgi.dir');
                   11801:     &Apache::lonnet::delenv('cgi.decompressed');
                   11802:     return ($decompressed,$result);
                   11803: }
                   11804: 
1.1055    raeburn  11805: sub process_decompression {
                   11806:     my ($docudom,$docuname,$file,$destination,$dir_root,$hiddenelem) = @_;
                   11807:     my ($dir,$error,$warning,$output);
1.1180    raeburn  11808:     if ($file !~ /\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/i) {
1.1120    bisitz   11809:         $error = &mt('Filename not a supported archive file type.').
                   11810:                  '<br />'.&mt('Filename should end with one of: [_1].',
1.1055    raeburn  11811:                               '.zip, .tar, .bz2, .gz, .tar.gz, .tar.bz2, .tgz');
                   11812:     } else {
                   11813:         my $docuhome = &Apache::lonnet::homeserver($docuname,$docudom);
                   11814:         if ($docuhome eq 'no_host') {
                   11815:             $error = &mt('Could not determine home server for course.');
                   11816:         } else {
                   11817:             my @ids=&Apache::lonnet::current_machine_ids();
                   11818:             my $currdir = "$dir_root/$destination";
                   11819:             if (grep(/^\Q$docuhome\E$/,@ids)) {
                   11820:                 $dir = &LONCAPA::propath($docudom,$docuname).
                   11821:                        "$dir_root/$destination";
                   11822:             } else {
                   11823:                 $dir = $Apache::lonnet::perlvar{'lonDocRoot'}.
                   11824:                        "$dir_root/$docudom/$docuname/$destination";
                   11825:                 unless (&Apache::lonnet::repcopy_userfile("$dir/$file") eq 'ok') {
                   11826:                     $error = &mt('Archive file not found.');
                   11827:                 }
                   11828:             }
1.1065    raeburn  11829:             my (@to_overwrite,@to_skip);
                   11830:             if ($env{'form.archive_overwrite_total'} > 0) {
                   11831:                 my $total = $env{'form.archive_overwrite_total'};
                   11832:                 for (my $i=0; $i<$total; $i++) {
                   11833:                     if ($env{'form.archive_overwrite_'.$i} == 1) {
                   11834:                         push(@to_overwrite,$env{'form.archive_overwrite_name_'.$i});
                   11835:                     } elsif ($env{'form.archive_overwrite_'.$i} == 0) {
                   11836:                         push(@to_skip,$env{'form.archive_overwrite_name_'.$i});
                   11837:                     }
                   11838:                 }
                   11839:             }
                   11840:             my $numskip = scalar(@to_skip);
                   11841:             if (($numskip > 0) && 
                   11842:                 ($numskip == $env{'form.archive_itemcount'})) {
                   11843:                 $warning = &mt('All items in the archive file already exist, and no overwriting of existing files has been requested.');         
                   11844:             } elsif ($dir eq '') {
1.1055    raeburn  11845:                 $error = &mt('Directory containing archive file unavailable.');
                   11846:             } elsif (!$error) {
1.1065    raeburn  11847:                 my ($decompressed,$display);
                   11848:                 if ($numskip > 0) {
                   11849:                     my $tempdir = time.'_'.$$.int(rand(10000));
                   11850:                     mkdir("$dir/$tempdir",0755);
                   11851:                     system("mv $dir/$file $dir/$tempdir/$file");
                   11852:                     ($decompressed,$display) = 
                   11853:                         &decompress_uploaded_file($file,"$dir/$tempdir");
                   11854:                     foreach my $item (@to_skip) {
                   11855:                         if (($item ne '') && ($item !~ /\.\./)) {
                   11856:                             if (-f "$dir/$tempdir/$item") { 
                   11857:                                 unlink("$dir/$tempdir/$item");
                   11858:                             } elsif (-d "$dir/$tempdir/$item") {
                   11859:                                 system("rm -rf $dir/$tempdir/$item");
                   11860:                             }
                   11861:                         }
                   11862:                     }
                   11863:                     system("mv $dir/$tempdir/* $dir");
                   11864:                     rmdir("$dir/$tempdir");   
                   11865:                 } else {
                   11866:                     ($decompressed,$display) = 
                   11867:                         &decompress_uploaded_file($file,$dir);
                   11868:                 }
1.1055    raeburn  11869:                 if ($decompressed eq 'ok') {
1.1065    raeburn  11870:                     $output = '<p class="LC_info">'.
                   11871:                               &mt('Files extracted successfully from archive.').
                   11872:                               '</p>'."\n";
1.1055    raeburn  11873:                     my ($warning,$result,@contents);
                   11874:                     my ($newdirlistref,$newlisterror) =
                   11875:                         &Apache::lonnet::dirlist($currdir,$docudom,
                   11876:                                                  $docuname,1);
                   11877:                     my (%is_dir,%changes,@newitems);
                   11878:                     my $dirptr = 16384;
1.1065    raeburn  11879:                     if (ref($newdirlistref) eq 'ARRAY') {
1.1055    raeburn  11880:                         foreach my $dir_line (@{$newdirlistref}) {
                   11881:                             my ($item,undef,undef,$testdir)=split(/\&/,$dir_line,5);
1.1065    raeburn  11882:                             unless (($item =~ /^\.+$/) || ($item eq $file) || 
                   11883:                                     ((@to_skip > 0) && (grep(/^\Q$item\E$/,@to_skip)))) {
1.1055    raeburn  11884:                                 push(@newitems,$item);
                   11885:                                 if ($dirptr&$testdir) {
                   11886:                                     $is_dir{$item} = 1;
                   11887:                                 }
                   11888:                                 $changes{$item} = 1;
                   11889:                             }
                   11890:                         }
                   11891:                     }
                   11892:                     if (keys(%changes) > 0) {
                   11893:                         foreach my $item (sort(@newitems)) {
                   11894:                             if ($changes{$item}) {
                   11895:                                 push(@contents,$item);
                   11896:                             }
                   11897:                         }
                   11898:                     }
                   11899:                     if (@contents > 0) {
1.1067    raeburn  11900:                         my $wantform;
                   11901:                         unless ($env{'form.autoextract_camtasia'}) {
                   11902:                             $wantform = 1;
                   11903:                         }
1.1056    raeburn  11904:                         my (%children,%parent,%dirorder,%titles);
1.1055    raeburn  11905:                         my ($count,$datatable) = &get_extracted($docudom,$docuname,
                   11906:                                                                 $currdir,\%is_dir,
                   11907:                                                                 \%children,\%parent,
1.1056    raeburn  11908:                                                                 \@contents,\%dirorder,
                   11909:                                                                 \%titles,$wantform);
1.1055    raeburn  11910:                         if ($datatable ne '') {
                   11911:                             $output .= &archive_options_form('decompressed',$datatable,
                   11912:                                                              $count,$hiddenelem);
1.1065    raeburn  11913:                             my $startcount = 6;
1.1055    raeburn  11914:                             $output .= &archive_javascript($startcount,$count,
1.1056    raeburn  11915:                                                            \%titles,\%children);
1.1055    raeburn  11916:                         }
1.1067    raeburn  11917:                         if ($env{'form.autoextract_camtasia'}) {
1.1164    raeburn  11918:                             my $version = $env{'form.autoextract_camtasia'};
1.1067    raeburn  11919:                             my %displayed;
                   11920:                             my $total = 1;
                   11921:                             $env{'form.archive_directory'} = [];
                   11922:                             foreach my $i (sort { $a <=> $b } keys(%dirorder)) {
                   11923:                                 my $path = join('/',map { $titles{$_}; } @{$dirorder{$i}});
                   11924:                                 $path =~ s{/$}{};
                   11925:                                 my $item;
                   11926:                                 if ($path ne '') {
                   11927:                                     $item = "$path/$titles{$i}";
                   11928:                                 } else {
                   11929:                                     $item = $titles{$i};
                   11930:                                 }
                   11931:                                 $env{'form.archive_content_'.$i} = "$dir_root/$destination/$item";
                   11932:                                 if ($item eq $contents[0]) {
                   11933:                                     push(@{$env{'form.archive_directory'}},$i);
                   11934:                                     $env{'form.archive_'.$i} = 'display';
                   11935:                                     $env{'form.archive_title_'.$i} = $env{'form.camtasia_foldername'};
                   11936:                                     $displayed{'folder'} = $i;
1.1164    raeburn  11937:                                 } elsif ((($item eq "$contents[0]/index.html") && ($version == 6)) ||
                   11938:                                          (($item eq "$contents[0]/$contents[0]".'.html') && ($version == 8))) { 
1.1067    raeburn  11939:                                     $env{'form.archive_'.$i} = 'display';
                   11940:                                     $env{'form.archive_title_'.$i} = $env{'form.camtasia_moviename'};
                   11941:                                     $displayed{'web'} = $i;
                   11942:                                 } else {
1.1164    raeburn  11943:                                     if ((($item eq "$contents[0]/media") && ($version == 6)) ||
                   11944:                                         ((($item eq "$contents[0]/scripts") || ($item eq "$contents[0]/skins") ||
                   11945:                                              ($item eq "$contents[0]/skins/express_show")) && ($version == 8))) {
1.1067    raeburn  11946:                                         push(@{$env{'form.archive_directory'}},$i);
                   11947:                                     }
                   11948:                                     $env{'form.archive_'.$i} = 'dependency';
                   11949:                                 }
                   11950:                                 $total ++;
                   11951:                             }
                   11952:                             for (my $i=1; $i<$total; $i++) {
                   11953:                                 next if ($i == $displayed{'web'});
                   11954:                                 next if ($i == $displayed{'folder'});
                   11955:                                 $env{'form.archive_dependent_on_'.$i} = $displayed{'web'};
                   11956:                             }
                   11957:                             $env{'form.phase'} = 'decompress_cleanup';
                   11958:                             $env{'form.archivedelete'} = 1;
                   11959:                             $env{'form.archive_count'} = $total-1;
                   11960:                             $output .=
                   11961:                                 &process_extracted_files('coursedocs',$docudom,
                   11962:                                                          $docuname,$destination,
                   11963:                                                          $dir_root,$hiddenelem);
                   11964:                         }
1.1055    raeburn  11965:                     } else {
                   11966:                         $warning = &mt('No new items extracted from archive file.');
                   11967:                     }
                   11968:                 } else {
                   11969:                     $output = $display;
                   11970:                     $error = &mt('An error occurred during extraction from the archive file.');
                   11971:                 }
                   11972:             }
                   11973:         }
                   11974:     }
                   11975:     if ($error) {
                   11976:         $output .= '<p class="LC_error">'.&mt('Not extracted.').'<br />'.
                   11977:                    $error.'</p>'."\n";
                   11978:     }
                   11979:     if ($warning) {
                   11980:         $output .= '<p class="LC_warning">'.$warning.'</p>'."\n";
                   11981:     }
                   11982:     return $output;
                   11983: }
                   11984: 
                   11985: sub get_extracted {
1.1056    raeburn  11986:     my ($docudom,$docuname,$currdir,$is_dir,$children,$parent,$contents,$dirorder,
                   11987:         $titles,$wantform) = @_;
1.1055    raeburn  11988:     my $count = 0;
                   11989:     my $depth = 0;
                   11990:     my $datatable;
1.1056    raeburn  11991:     my @hierarchy;
1.1055    raeburn  11992:     return unless ((ref($is_dir) eq 'HASH') && (ref($children) eq 'HASH') &&
1.1056    raeburn  11993:                    (ref($parent) eq 'HASH') && (ref($contents) eq 'ARRAY') &&
                   11994:                    (ref($dirorder) eq 'HASH') && (ref($titles) eq 'HASH'));
1.1055    raeburn  11995:     foreach my $item (@{$contents}) {
                   11996:         $count ++;
1.1056    raeburn  11997:         @{$dirorder->{$count}} = @hierarchy;
                   11998:         $titles->{$count} = $item;
1.1055    raeburn  11999:         &archive_hierarchy($depth,$count,$parent,$children);
                   12000:         if ($wantform) {
                   12001:             $datatable .= &archive_row($is_dir->{$item},$item,
                   12002:                                        $currdir,$depth,$count);
                   12003:         }
                   12004:         if ($is_dir->{$item}) {
                   12005:             $depth ++;
1.1056    raeburn  12006:             push(@hierarchy,$count);
                   12007:             $parent->{$depth} = $count;
1.1055    raeburn  12008:             $datatable .=
                   12009:                 &recurse_extracted_archive("$currdir/$item",$docudom,$docuname,
1.1056    raeburn  12010:                                            \$depth,\$count,\@hierarchy,$dirorder,
                   12011:                                            $children,$parent,$titles,$wantform);
1.1055    raeburn  12012:             $depth --;
1.1056    raeburn  12013:             pop(@hierarchy);
1.1055    raeburn  12014:         }
                   12015:     }
                   12016:     return ($count,$datatable);
                   12017: }
                   12018: 
                   12019: sub recurse_extracted_archive {
1.1056    raeburn  12020:     my ($currdir,$docudom,$docuname,$depth,$count,$hierarchy,$dirorder,
                   12021:         $children,$parent,$titles,$wantform) = @_;
1.1055    raeburn  12022:     my $result='';
1.1056    raeburn  12023:     unless ((ref($depth)) && (ref($count)) && (ref($hierarchy) eq 'ARRAY') &&
                   12024:             (ref($children) eq 'HASH') && (ref($parent) eq 'HASH') &&
                   12025:             (ref($dirorder) eq 'HASH')) {
1.1055    raeburn  12026:         return $result;
                   12027:     }
                   12028:     my $dirptr = 16384;
                   12029:     my ($newdirlistref,$newlisterror) =
                   12030:         &Apache::lonnet::dirlist($currdir,$docudom,$docuname,1);
                   12031:     if (ref($newdirlistref) eq 'ARRAY') {
                   12032:         foreach my $dir_line (@{$newdirlistref}) {
                   12033:             my ($item,undef,undef,$testdir)=split(/\&/,$dir_line,5);
                   12034:             unless ($item =~ /^\.+$/) {
                   12035:                 $$count ++;
1.1056    raeburn  12036:                 @{$dirorder->{$$count}} = @{$hierarchy};
                   12037:                 $titles->{$$count} = $item;
1.1055    raeburn  12038:                 &archive_hierarchy($$depth,$$count,$parent,$children);
1.1056    raeburn  12039: 
1.1055    raeburn  12040:                 my $is_dir;
                   12041:                 if ($dirptr&$testdir) {
                   12042:                     $is_dir = 1;
                   12043:                 }
                   12044:                 if ($wantform) {
                   12045:                     $result .= &archive_row($is_dir,$item,$currdir,$$depth,$$count);
                   12046:                 }
                   12047:                 if ($is_dir) {
                   12048:                     $$depth ++;
1.1056    raeburn  12049:                     push(@{$hierarchy},$$count);
                   12050:                     $parent->{$$depth} = $$count;
1.1055    raeburn  12051:                     $result .=
                   12052:                         &recurse_extracted_archive("$currdir/$item",$docudom,
                   12053:                                                    $docuname,$depth,$count,
1.1056    raeburn  12054:                                                    $hierarchy,$dirorder,$children,
                   12055:                                                    $parent,$titles,$wantform);
1.1055    raeburn  12056:                     $$depth --;
1.1056    raeburn  12057:                     pop(@{$hierarchy});
1.1055    raeburn  12058:                 }
                   12059:             }
                   12060:         }
                   12061:     }
                   12062:     return $result;
                   12063: }
                   12064: 
                   12065: sub archive_hierarchy {
                   12066:     my ($depth,$count,$parent,$children) =@_;
                   12067:     if ((ref($parent) eq 'HASH') && (ref($children) eq 'HASH')) {
                   12068:         if (exists($parent->{$depth})) {
                   12069:              $children->{$parent->{$depth}} .= $count.':';
                   12070:         }
                   12071:     }
                   12072:     return;
                   12073: }
                   12074: 
                   12075: sub archive_row {
                   12076:     my ($is_dir,$item,$currdir,$depth,$count) = @_;
                   12077:     my ($name) = ($item =~ m{([^/]+)$});
                   12078:     my %choices = &Apache::lonlocal::texthash (
1.1059    raeburn  12079:                                        'display'    => 'Add as file',
1.1055    raeburn  12080:                                        'dependency' => 'Include as dependency',
                   12081:                                        'discard'    => 'Discard',
                   12082:                                       );
                   12083:     if ($is_dir) {
1.1059    raeburn  12084:         $choices{'display'} = &mt('Add as folder'); 
1.1055    raeburn  12085:     }
1.1056    raeburn  12086:     my $output = &start_data_table_row().'<td align="right">'.$count.'</td>'."\n";
                   12087:     my $offset = 0;
1.1055    raeburn  12088:     foreach my $action ('display','dependency','discard') {
1.1056    raeburn  12089:         $offset ++;
1.1065    raeburn  12090:         if ($action ne 'display') {
                   12091:             $offset ++;
                   12092:         }  
1.1055    raeburn  12093:         $output .= '<td><span class="LC_nobreak">'.
                   12094:                    '<label><input type="radio" name="archive_'.$count.
                   12095:                    '" id="archive_'.$action.'_'.$count.'" value="'.$action.'"';
                   12096:         my $text = $choices{$action};
                   12097:         if ($is_dir) {
                   12098:             $output .= ' onclick="javascript:propagateCheck(this.form,'."'$count'".');"';
                   12099:             if ($action eq 'display') {
1.1059    raeburn  12100:                 $text = &mt('Add as folder');
1.1055    raeburn  12101:             }
1.1056    raeburn  12102:         } else {
                   12103:             $output .= ' onclick="javascript:dependencyCheck(this.form,'."$count,$offset".');"';
                   12104: 
                   12105:         }
                   12106:         $output .= ' />&nbsp;'.$choices{$action}.'</label></span>';
                   12107:         if ($action eq 'dependency') {
                   12108:             $output .= '<div id="arc_depon_'.$count.'" style="display:none;">'."\n".
                   12109:                        &mt('Used by:').'&nbsp;<select name="archive_dependent_on_'.$count.'" '.
                   12110:                        'onchange="propagateSelect(this.form,'."$count,$offset".')">'."\n".
                   12111:                        '<option value=""></option>'."\n".
                   12112:                        '</select>'."\n".
                   12113:                        '</div>';
1.1059    raeburn  12114:         } elsif ($action eq 'display') {
                   12115:             $output .= '<div id="arc_title_'.$count.'" style="display:none;">'."\n".
                   12116:                        &mt('Title:').'&nbsp;<input type="text" name="archive_title_'.$count.'" id="archive_title_'.$count.'" />'."\n".
                   12117:                        '</div>';
1.1055    raeburn  12118:         }
1.1056    raeburn  12119:         $output .= '</td>';
1.1055    raeburn  12120:     }
                   12121:     $output .= '<td><input type="hidden" name="archive_content_'.$count.'" value="'.
                   12122:                &HTML::Entities::encode("$currdir/$item",'"<>&').'" />'.('&nbsp;' x 2);
                   12123:     for (my $i=0; $i<$depth; $i++) {
                   12124:         $output .= ('<img src="/adm/lonIcons/whitespace1.gif" class="LC_docs_spacer" alt="" />' x2)."\n";
                   12125:     }
                   12126:     if ($is_dir) {
                   12127:         $output .= '<img src="/adm/lonIcons/navmap.folder.open.gif" alt="" />&nbsp;'."\n".
                   12128:                    '<input type="hidden" name="archive_directory" value="'.$count.'" />'."\n";
                   12129:     } else {
                   12130:         $output .= '<input type="hidden" name="archive_file" value="'.$count.'" />'."\n";
                   12131:     }
                   12132:     $output .= '&nbsp;'.$name.'</td>'."\n".
                   12133:                &end_data_table_row();
                   12134:     return $output;
                   12135: }
                   12136: 
                   12137: sub archive_options_form {
1.1065    raeburn  12138:     my ($form,$display,$count,$hiddenelem) = @_;
                   12139:     my %lt = &Apache::lonlocal::texthash(
                   12140:                perm => 'Permanently remove archive file?',
                   12141:                hows => 'How should each extracted item be incorporated in the course?',
                   12142:                cont => 'Content actions for all',
                   12143:                addf => 'Add as folder/file',
                   12144:                incd => 'Include as dependency for a displayed file',
                   12145:                disc => 'Discard',
                   12146:                no   => 'No',
                   12147:                yes  => 'Yes',
                   12148:                save => 'Save',
                   12149:     );
                   12150:     my $output = <<"END";
                   12151: <form name="$form" method="post" action="">
                   12152: <p><span class="LC_nobreak">$lt{'perm'}&nbsp;
                   12153: <label>
                   12154:   <input type="radio" name="archivedelete" value="0" checked="checked" />$lt{'no'}
                   12155: </label>
                   12156: &nbsp;
                   12157: <label>
                   12158:   <input type="radio" name="archivedelete" value="1" />$lt{'yes'}</label>
                   12159: </span>
                   12160: </p>
                   12161: <input type="hidden" name="phase" value="decompress_cleanup" />
                   12162: <br />$lt{'hows'}
                   12163: <div class="LC_columnSection">
                   12164:   <fieldset>
                   12165:     <legend>$lt{'cont'}</legend>
                   12166:     <input type="button" value="$lt{'addf'}" onclick="javascript:checkAll(document.$form,'display');" /> 
                   12167:     &nbsp;&nbsp;<input type="button" value="$lt{'incd'}" onclick="javascript:checkAll(document.$form,'dependency');" />
                   12168:     &nbsp;&nbsp;<input type="button" value="$lt{'disc'}" onclick="javascript:checkAll(document.$form,'discard');" />
                   12169:   </fieldset>
                   12170: </div>
                   12171: END
                   12172:     return $output.
1.1055    raeburn  12173:            &start_data_table()."\n".
1.1065    raeburn  12174:            $display."\n".
1.1055    raeburn  12175:            &end_data_table()."\n".
                   12176:            '<input type="hidden" name="archive_count" value="'.$count.'" />'.
                   12177:            $hiddenelem.
1.1065    raeburn  12178:            '<br /><input type="submit" name="archive_submit" value="'.$lt{'save'}.'" />'.
1.1055    raeburn  12179:            '</form>';
                   12180: }
                   12181: 
                   12182: sub archive_javascript {
1.1056    raeburn  12183:     my ($startcount,$numitems,$titles,$children) = @_;
                   12184:     return unless ((ref($titles) eq 'HASH') && (ref($children) eq 'HASH'));
1.1059    raeburn  12185:     my $maintitle = $env{'form.comment'};
1.1055    raeburn  12186:     my $scripttag = <<START;
                   12187: <script type="text/javascript">
                   12188: // <![CDATA[
                   12189: 
                   12190: function checkAll(form,prefix) {
                   12191:     var idstr =  new RegExp("^archive_"+prefix+"_\\\\d+\$");
                   12192:     for (var i=0; i < form.elements.length; i++) {
                   12193:         var id = form.elements[i].id;
                   12194:         if ((id != '') && (id != undefined)) {
                   12195:             if (idstr.test(id)) {
                   12196:                 if (form.elements[i].type == 'radio') {
                   12197:                     form.elements[i].checked = true;
1.1056    raeburn  12198:                     var nostart = i-$startcount;
1.1059    raeburn  12199:                     var offset = nostart%7;
                   12200:                     var count = (nostart-offset)/7;    
1.1056    raeburn  12201:                     dependencyCheck(form,count,offset);
1.1055    raeburn  12202:                 }
                   12203:             }
                   12204:         }
                   12205:     }
                   12206: }
                   12207: 
                   12208: function propagateCheck(form,count) {
                   12209:     if (count > 0) {
1.1059    raeburn  12210:         var startelement = $startcount + ((count-1) * 7);
                   12211:         for (var j=1; j<6; j++) {
                   12212:             if ((j != 2) && (j != 4)) {
1.1056    raeburn  12213:                 var item = startelement + j; 
                   12214:                 if (form.elements[item].type == 'radio') {
                   12215:                     if (form.elements[item].checked) {
                   12216:                         containerCheck(form,count,j);
                   12217:                         break;
                   12218:                     }
1.1055    raeburn  12219:                 }
                   12220:             }
                   12221:         }
                   12222:     }
                   12223: }
                   12224: 
                   12225: numitems = $numitems
1.1056    raeburn  12226: var titles = new Array(numitems);
                   12227: var parents = new Array(numitems);
1.1055    raeburn  12228: for (var i=0; i<numitems; i++) {
1.1056    raeburn  12229:     parents[i] = new Array;
1.1055    raeburn  12230: }
1.1059    raeburn  12231: var maintitle = '$maintitle';
1.1055    raeburn  12232: 
                   12233: START
                   12234: 
1.1056    raeburn  12235:     foreach my $container (sort { $a <=> $b } (keys(%{$children}))) {
                   12236:         my @contents = split(/:/,$children->{$container});
1.1055    raeburn  12237:         for (my $i=0; $i<@contents; $i ++) {
                   12238:             $scripttag .= 'parents['.$container.']['.$i.'] = '.$contents[$i]."\n";
                   12239:         }
                   12240:     }
                   12241: 
1.1056    raeburn  12242:     foreach my $key (sort { $a <=> $b } (keys(%{$titles}))) {
                   12243:         $scripttag .= "titles[$key] = '".$titles->{$key}."';\n";
                   12244:     }
                   12245: 
1.1055    raeburn  12246:     $scripttag .= <<END;
                   12247: 
                   12248: function containerCheck(form,count,offset) {
                   12249:     if (count > 0) {
1.1056    raeburn  12250:         dependencyCheck(form,count,offset);
1.1059    raeburn  12251:         var item = (offset+$startcount)+7*(count-1);
1.1055    raeburn  12252:         form.elements[item].checked = true;
                   12253:         if(Object.prototype.toString.call(parents[count]) === '[object Array]') {
                   12254:             if (parents[count].length > 0) {
                   12255:                 for (var j=0; j<parents[count].length; j++) {
1.1056    raeburn  12256:                     containerCheck(form,parents[count][j],offset);
                   12257:                 }
                   12258:             }
                   12259:         }
                   12260:     }
                   12261: }
                   12262: 
                   12263: function dependencyCheck(form,count,offset) {
                   12264:     if (count > 0) {
1.1059    raeburn  12265:         var chosen = (offset+$startcount)+7*(count-1);
                   12266:         var depitem = $startcount + ((count-1) * 7) + 4;
1.1056    raeburn  12267:         var currtype = form.elements[depitem].type;
                   12268:         if (form.elements[chosen].value == 'dependency') {
                   12269:             document.getElementById('arc_depon_'+count).style.display='block'; 
                   12270:             form.elements[depitem].options.length = 0;
                   12271:             form.elements[depitem].options[0] = new Option('Select','',true,true);
1.1085    raeburn  12272:             for (var i=1; i<=numitems; i++) {
                   12273:                 if (i == count) {
                   12274:                     continue;
                   12275:                 }
1.1059    raeburn  12276:                 var startelement = $startcount + (i-1) * 7;
                   12277:                 for (var j=1; j<6; j++) {
                   12278:                     if ((j != 2) && (j!= 4)) {
1.1056    raeburn  12279:                         var item = startelement + j;
                   12280:                         if (form.elements[item].type == 'radio') {
                   12281:                             if (form.elements[item].checked) {
                   12282:                                 if (form.elements[item].value == 'display') {
                   12283:                                     var n = form.elements[depitem].options.length;
                   12284:                                     form.elements[depitem].options[n] = new Option(titles[i],i,false,false);
                   12285:                                 }
                   12286:                             }
                   12287:                         }
                   12288:                     }
                   12289:                 }
                   12290:             }
                   12291:         } else {
                   12292:             document.getElementById('arc_depon_'+count).style.display='none';
                   12293:             form.elements[depitem].options.length = 0;
                   12294:             form.elements[depitem].options[0] = new Option('Select','',true,true);
                   12295:         }
1.1059    raeburn  12296:         titleCheck(form,count,offset);
1.1056    raeburn  12297:     }
                   12298: }
                   12299: 
                   12300: function propagateSelect(form,count,offset) {
                   12301:     if (count > 0) {
1.1065    raeburn  12302:         var item = (1+offset+$startcount)+7*(count-1);
1.1056    raeburn  12303:         var picked = form.elements[item].options[form.elements[item].selectedIndex].value; 
                   12304:         if (Object.prototype.toString.call(parents[count]) === '[object Array]') {
                   12305:             if (parents[count].length > 0) {
                   12306:                 for (var j=0; j<parents[count].length; j++) {
                   12307:                     containerSelect(form,parents[count][j],offset,picked);
1.1055    raeburn  12308:                 }
                   12309:             }
                   12310:         }
                   12311:     }
                   12312: }
1.1056    raeburn  12313: 
                   12314: function containerSelect(form,count,offset,picked) {
                   12315:     if (count > 0) {
1.1065    raeburn  12316:         var item = (offset+$startcount)+7*(count-1);
1.1056    raeburn  12317:         if (form.elements[item].type == 'radio') {
                   12318:             if (form.elements[item].value == 'dependency') {
                   12319:                 if (form.elements[item+1].type == 'select-one') {
                   12320:                     for (var i=0; i<form.elements[item+1].options.length; i++) {
                   12321:                         if (form.elements[item+1].options[i].value == picked) {
                   12322:                             form.elements[item+1].selectedIndex = i;
                   12323:                             break;
                   12324:                         }
                   12325:                     }
                   12326:                 }
                   12327:                 if (Object.prototype.toString.call(parents[count]) === '[object Array]') {
                   12328:                     if (parents[count].length > 0) {
                   12329:                         for (var j=0; j<parents[count].length; j++) {
                   12330:                             containerSelect(form,parents[count][j],offset,picked);
                   12331:                         }
                   12332:                     }
                   12333:                 }
                   12334:             }
                   12335:         }
                   12336:     }
                   12337: }
                   12338: 
1.1059    raeburn  12339: function titleCheck(form,count,offset) {
                   12340:     if (count > 0) {
                   12341:         var chosen = (offset+$startcount)+7*(count-1);
                   12342:         var depitem = $startcount + ((count-1) * 7) + 2;
                   12343:         var currtype = form.elements[depitem].type;
                   12344:         if (form.elements[chosen].value == 'display') {
                   12345:             document.getElementById('arc_title_'+count).style.display='block';
                   12346:             if ((count==1) && ((parents[count].length > 0) || (numitems == 1))) {
                   12347:                 document.getElementById('archive_title_'+count).value=maintitle;
                   12348:             }
                   12349:         } else {
                   12350:             document.getElementById('arc_title_'+count).style.display='none';
                   12351:             if (currtype == 'text') { 
                   12352:                 document.getElementById('archive_title_'+count).value='';
                   12353:             }
                   12354:         }
                   12355:     }
                   12356:     return;
                   12357: }
                   12358: 
1.1055    raeburn  12359: // ]]>
                   12360: </script>
                   12361: END
                   12362:     return $scripttag;
                   12363: }
                   12364: 
                   12365: sub process_extracted_files {
1.1067    raeburn  12366:     my ($context,$docudom,$docuname,$destination,$dir_root,$hiddenelem) = @_;
1.1055    raeburn  12367:     my $numitems = $env{'form.archive_count'};
                   12368:     return unless ($numitems);
                   12369:     my @ids=&Apache::lonnet::current_machine_ids();
                   12370:     my ($prefix,$pathtocheck,$dir,$ishome,$error,$warning,%toplevelitems,%is_dir,
1.1067    raeburn  12371:         %folders,%containers,%mapinner,%prompttofetch);
1.1055    raeburn  12372:     my $docuhome = &Apache::lonnet::homeserver($docuname,$docudom);
                   12373:     if (grep(/^\Q$docuhome\E$/,@ids)) {
                   12374:         $prefix = &LONCAPA::propath($docudom,$docuname);
                   12375:         $pathtocheck = "$dir_root/$destination";
                   12376:         $dir = $dir_root;
                   12377:         $ishome = 1;
                   12378:     } else {
                   12379:         $prefix = $Apache::lonnet::perlvar{'lonDocRoot'};
                   12380:         $pathtocheck = "$dir_root/$docudom/$docuname/$destination";
                   12381:         $dir = "$dir_root/$docudom/$docuname";    
                   12382:     }
                   12383:     my $currdir = "$dir_root/$destination";
                   12384:     (my $docstype,$mapinner{'0'}) = ($destination =~ m{^(docs|supplemental)/(\w+)/});
                   12385:     if ($env{'form.folderpath'}) {
                   12386:         my @items = split('&',$env{'form.folderpath'});
                   12387:         $folders{'0'} = $items[-2];
1.1099    raeburn  12388:         if ($env{'form.folderpath'} =~ /\:1$/) {
                   12389:             $containers{'0'}='page';
                   12390:         } else {  
                   12391:             $containers{'0'}='sequence';
                   12392:         }
1.1055    raeburn  12393:     }
                   12394:     my @archdirs = &get_env_multiple('form.archive_directory');
                   12395:     if ($numitems) {
                   12396:         for (my $i=1; $i<=$numitems; $i++) {
                   12397:             my $path = $env{'form.archive_content_'.$i};
                   12398:             if ($path =~ m{^\Q$pathtocheck\E/([^/]+)$}) {
                   12399:                 my $item = $1;
                   12400:                 $toplevelitems{$item} = $i;
                   12401:                 if (grep(/^\Q$i\E$/,@archdirs)) {
                   12402:                     $is_dir{$item} = 1;
                   12403:                 }
                   12404:             }
                   12405:         }
                   12406:     }
1.1067    raeburn  12407:     my ($output,%children,%parent,%titles,%dirorder,$result);
1.1055    raeburn  12408:     if (keys(%toplevelitems) > 0) {
                   12409:         my @contents = sort(keys(%toplevelitems));
1.1056    raeburn  12410:         (my $count,undef) = &get_extracted($docudom,$docuname,$currdir,\%is_dir,\%children,
                   12411:                                            \%parent,\@contents,\%dirorder,\%titles);
1.1055    raeburn  12412:     }
1.1066    raeburn  12413:     my (%referrer,%orphaned,%todelete,%todeletedir,%newdest,%newseqid);
1.1055    raeburn  12414:     if ($numitems) {
                   12415:         for (my $i=1; $i<=$numitems; $i++) {
1.1086    raeburn  12416:             next if ($env{'form.archive_'.$i} eq 'dependency');
1.1055    raeburn  12417:             my $path = $env{'form.archive_content_'.$i};
                   12418:             if ($path =~ /^\Q$pathtocheck\E/) {
                   12419:                 if ($env{'form.archive_'.$i} eq 'discard') {
                   12420:                     if ($prefix ne '' && $path ne '') {
                   12421:                         if (-e $prefix.$path) {
1.1066    raeburn  12422:                             if ((@archdirs > 0) && 
                   12423:                                 (grep(/^\Q$i\E$/,@archdirs))) {
                   12424:                                 $todeletedir{$prefix.$path} = 1;
                   12425:                             } else {
                   12426:                                 $todelete{$prefix.$path} = 1;
                   12427:                             }
1.1055    raeburn  12428:                         }
                   12429:                     }
                   12430:                 } elsif ($env{'form.archive_'.$i} eq 'display') {
1.1059    raeburn  12431:                     my ($docstitle,$title,$url,$outer);
1.1055    raeburn  12432:                     ($title) = ($path =~ m{/([^/]+)$});
1.1059    raeburn  12433:                     $docstitle = $env{'form.archive_title_'.$i};
                   12434:                     if ($docstitle eq '') {
                   12435:                         $docstitle = $title;
                   12436:                     }
1.1055    raeburn  12437:                     $outer = 0;
1.1056    raeburn  12438:                     if (ref($dirorder{$i}) eq 'ARRAY') {
                   12439:                         if (@{$dirorder{$i}} > 0) {
                   12440:                             foreach my $item (reverse(@{$dirorder{$i}})) {
1.1055    raeburn  12441:                                 if ($env{'form.archive_'.$item} eq 'display') {
                   12442:                                     $outer = $item;
                   12443:                                     last;
                   12444:                                 }
                   12445:                             }
                   12446:                         }
                   12447:                     }
                   12448:                     my ($errtext,$fatal) = 
                   12449:                         &LONCAPA::map::mapread('/uploaded/'.$docudom.'/'.$docuname.
                   12450:                                                '/'.$folders{$outer}.'.'.
                   12451:                                                $containers{$outer});
                   12452:                     next if ($fatal);
                   12453:                     if ((@archdirs > 0) && (grep(/^\Q$i\E$/,@archdirs))) {
                   12454:                         if ($context eq 'coursedocs') {
1.1056    raeburn  12455:                             $mapinner{$i} = time;
1.1055    raeburn  12456:                             $folders{$i} = 'default_'.$mapinner{$i};
                   12457:                             $containers{$i} = 'sequence';
                   12458:                             my $url = '/uploaded/'.$docudom.'/'.$docuname.'/'.
                   12459:                                       $folders{$i}.'.'.$containers{$i};
                   12460:                             my $newidx = &LONCAPA::map::getresidx();
                   12461:                             $LONCAPA::map::resources[$newidx]=
1.1059    raeburn  12462:                                 $docstitle.':'.$url.':false:normal:res';
1.1055    raeburn  12463:                             push(@LONCAPA::map::order,$newidx);
                   12464:                             my ($outtext,$errtext) =
                   12465:                                 &LONCAPA::map::storemap('/uploaded/'.$docudom.'/'.
                   12466:                                                         $docuname.'/'.$folders{$outer}.
1.1087    raeburn  12467:                                                         '.'.$containers{$outer},1,1);
1.1056    raeburn  12468:                             $newseqid{$i} = $newidx;
1.1067    raeburn  12469:                             unless ($errtext) {
                   12470:                                 $result .=  '<li>'.&mt('Folder: [_1] added to course',$docstitle).'</li>'."\n";
                   12471:                             }
1.1055    raeburn  12472:                         }
                   12473:                     } else {
                   12474:                         if ($context eq 'coursedocs') {
                   12475:                             my $newidx=&LONCAPA::map::getresidx();
                   12476:                             my $url = '/uploaded/'.$docudom.'/'.$docuname.'/'.
                   12477:                                       $docstype.'/'.$mapinner{$outer}.'/'.$newidx.'/'.
                   12478:                                       $title;
                   12479:                             if (!-e "$prefix$dir/$docstype/$mapinner{$outer}") {
                   12480:                                 mkdir("$prefix$dir/$docstype/$mapinner{$outer}",0755);
                   12481:                             }
                   12482:                             if (!-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx") {
                   12483:                                 mkdir("$prefix$dir/$docstype/$mapinner{$outer}/$newidx");
                   12484:                             }
                   12485:                             if (-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx") {
                   12486:                                 system("mv $prefix$path $prefix$dir/$docstype/$mapinner{$outer}/$newidx/$title");
1.1056    raeburn  12487:                                 $newdest{$i} = "$prefix$dir/$docstype/$mapinner{$outer}/$newidx";
1.1067    raeburn  12488:                                 unless ($ishome) {
                   12489:                                     my $fetch = "$newdest{$i}/$title";
                   12490:                                     $fetch =~ s/^\Q$prefix$dir\E//;
                   12491:                                     $prompttofetch{$fetch} = 1;
                   12492:                                 }
1.1055    raeburn  12493:                             }
                   12494:                             $LONCAPA::map::resources[$newidx]=
1.1059    raeburn  12495:                                 $docstitle.':'.$url.':false:normal:res';
1.1055    raeburn  12496:                             push(@LONCAPA::map::order, $newidx);
                   12497:                             my ($outtext,$errtext)=
                   12498:                                 &LONCAPA::map::storemap('/uploaded/'.$docudom.'/'.
                   12499:                                                         $docuname.'/'.$folders{$outer}.
1.1087    raeburn  12500:                                                         '.'.$containers{$outer},1,1);
1.1067    raeburn  12501:                             unless ($errtext) {
                   12502:                                 if (-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx/$title") {
                   12503:                                     $result .= '<li>'.&mt('File: [_1] added to course',$docstitle).'</li>'."\n";
                   12504:                                 }
                   12505:                             }
1.1055    raeburn  12506:                         }
                   12507:                     }
1.1086    raeburn  12508:                 }
                   12509:             } else {
                   12510:                 $warning .= &mt('Item extracted from archive: [_1] has unexpected path.',$path).'<br />'; 
                   12511:             }
                   12512:         }
                   12513:         for (my $i=1; $i<=$numitems; $i++) {
                   12514:             next unless ($env{'form.archive_'.$i} eq 'dependency');
                   12515:             my $path = $env{'form.archive_content_'.$i};
                   12516:             if ($path =~ /^\Q$pathtocheck\E/) {
                   12517:                 my ($title) = ($path =~ m{/([^/]+)$});
                   12518:                 $referrer{$i} = $env{'form.archive_dependent_on_'.$i};
                   12519:                 if ($env{'form.archive_'.$referrer{$i}} eq 'display') {
                   12520:                     if (ref($dirorder{$i}) eq 'ARRAY') {
                   12521:                         my ($itemidx,$fullpath,$relpath);
                   12522:                         if (ref($dirorder{$referrer{$i}}) eq 'ARRAY') {
                   12523:                             my $container = $dirorder{$referrer{$i}}->[-1];
1.1056    raeburn  12524:                             for (my $j=0; $j<@{$dirorder{$i}}; $j++) {
1.1086    raeburn  12525:                                 if ($dirorder{$i}->[$j] eq $container) {
                   12526:                                     $itemidx = $j;
1.1056    raeburn  12527:                                 }
                   12528:                             }
1.1086    raeburn  12529:                         }
                   12530:                         if ($itemidx eq '') {
                   12531:                             $itemidx =  0;
                   12532:                         } 
                   12533:                         if (grep(/^\Q$referrer{$i}\E$/,@archdirs)) {
                   12534:                             if ($mapinner{$referrer{$i}}) {
                   12535:                                 $fullpath = "$prefix$dir/$docstype/$mapinner{$referrer{$i}}";
                   12536:                                 for (my $j=$itemidx; $j<@{$dirorder{$i}}; $j++) {
                   12537:                                     if (grep(/^\Q$dirorder{$i}->[$j]\E$/,@archdirs)) {
                   12538:                                         unless (defined($newseqid{$dirorder{$i}->[$j]})) {
                   12539:                                             $fullpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   12540:                                             $relpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   12541:                                             if (!-e $fullpath) {
                   12542:                                                 mkdir($fullpath,0755);
1.1056    raeburn  12543:                                             }
                   12544:                                         }
1.1086    raeburn  12545:                                     } else {
                   12546:                                         last;
1.1056    raeburn  12547:                                     }
1.1086    raeburn  12548:                                 }
                   12549:                             }
                   12550:                         } elsif ($newdest{$referrer{$i}}) {
                   12551:                             $fullpath = $newdest{$referrer{$i}};
                   12552:                             for (my $j=$itemidx; $j<@{$dirorder{$i}}; $j++) {
                   12553:                                 if ($env{'form.archive_'.$dirorder{$i}->[$j]} eq 'discard') {
                   12554:                                     $orphaned{$i} = $env{'form.archive_'.$dirorder{$i}->[$j]};
                   12555:                                     last;
                   12556:                                 } elsif (grep(/^\Q$dirorder{$i}->[$j]\E$/,@archdirs)) {
                   12557:                                     unless (defined($newseqid{$dirorder{$i}->[$j]})) {
                   12558:                                         $fullpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   12559:                                         $relpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   12560:                                         if (!-e $fullpath) {
                   12561:                                             mkdir($fullpath,0755);
1.1056    raeburn  12562:                                         }
                   12563:                                     }
1.1086    raeburn  12564:                                 } else {
                   12565:                                     last;
1.1056    raeburn  12566:                                 }
1.1055    raeburn  12567:                             }
                   12568:                         }
1.1086    raeburn  12569:                         if ($fullpath ne '') {
                   12570:                             if (-e "$prefix$path") {
                   12571:                                 system("mv $prefix$path $fullpath/$title");
                   12572:                             }
                   12573:                             if (-e "$fullpath/$title") {
                   12574:                                 my $showpath;
                   12575:                                 if ($relpath ne '') {
                   12576:                                     $showpath = "$relpath/$title";
                   12577:                                 } else {
                   12578:                                     $showpath = "/$title";
                   12579:                                 } 
                   12580:                                 $result .= '<li>'.&mt('[_1] included as a dependency',$showpath).'</li>'."\n";
                   12581:                             } 
                   12582:                             unless ($ishome) {
                   12583:                                 my $fetch = "$fullpath/$title";
                   12584:                                 $fetch =~ s/^\Q$prefix$dir\E//; 
                   12585:                                 $prompttofetch{$fetch} = 1;
                   12586:                             }
                   12587:                         }
1.1055    raeburn  12588:                     }
1.1086    raeburn  12589:                 } elsif ($env{'form.archive_'.$referrer{$i}} eq 'discard') {
                   12590:                     $warning .= &mt('[_1] is a dependency of [_2], which was discarded.',
                   12591:                                     $path,$env{'form.archive_content_'.$referrer{$i}}).'<br />';
1.1055    raeburn  12592:                 }
                   12593:             } else {
                   12594:                 $warning .= &mt('Item extracted from archive: [_1] has unexpected path.',$path).'<br />'; 
                   12595:             }
                   12596:         }
                   12597:         if (keys(%todelete)) {
                   12598:             foreach my $key (keys(%todelete)) {
                   12599:                 unlink($key);
1.1066    raeburn  12600:             }
                   12601:         }
                   12602:         if (keys(%todeletedir)) {
                   12603:             foreach my $key (keys(%todeletedir)) {
                   12604:                 rmdir($key);
                   12605:             }
                   12606:         }
                   12607:         foreach my $dir (sort(keys(%is_dir))) {
                   12608:             if (($pathtocheck ne '') && ($dir ne ''))  {
                   12609:                 &cleanup_empty_dirs($prefix."$pathtocheck/$dir");
1.1055    raeburn  12610:             }
                   12611:         }
1.1067    raeburn  12612:         if ($result ne '') {
                   12613:             $output .= '<ul>'."\n".
                   12614:                        $result."\n".
                   12615:                        '</ul>';
                   12616:         }
                   12617:         unless ($ishome) {
                   12618:             my $replicationfail;
                   12619:             foreach my $item (keys(%prompttofetch)) {
                   12620:                 my $fetchresult= &Apache::lonnet::reply('fetchuserfile:'.$item,$docuhome);
                   12621:                 unless ($fetchresult eq 'ok') {
                   12622:                     $replicationfail .= '<li>'.$item.'</li>'."\n";
                   12623:                 }
                   12624:             }
                   12625:             if ($replicationfail) {
                   12626:                 $output .= '<p class="LC_error">'.
                   12627:                            &mt('Course home server failed to retrieve:').'<ul>'.
                   12628:                            $replicationfail.
                   12629:                            '</ul></p>';
                   12630:             }
                   12631:         }
1.1055    raeburn  12632:     } else {
                   12633:         $warning = &mt('No items found in archive.');
                   12634:     }
                   12635:     if ($error) {
                   12636:         $output .= '<p class="LC_error">'.&mt('Not extracted.').'<br />'.
                   12637:                    $error.'</p>'."\n";
                   12638:     }
                   12639:     if ($warning) {
                   12640:         $output .= '<p class="LC_warning">'.$warning.'</p>'."\n";
                   12641:     }
                   12642:     return $output;
                   12643: }
                   12644: 
1.1066    raeburn  12645: sub cleanup_empty_dirs {
                   12646:     my ($path) = @_;
                   12647:     if (($path ne '') && (-d $path)) {
                   12648:         if (opendir(my $dirh,$path)) {
                   12649:             my @dircontents = grep(!/^\./,readdir($dirh));
                   12650:             my $numitems = 0;
                   12651:             foreach my $item (@dircontents) {
                   12652:                 if (-d "$path/$item") {
1.1111    raeburn  12653:                     &cleanup_empty_dirs("$path/$item");
1.1066    raeburn  12654:                     if (-e "$path/$item") {
                   12655:                         $numitems ++;
                   12656:                     }
                   12657:                 } else {
                   12658:                     $numitems ++;
                   12659:                 }
                   12660:             }
                   12661:             if ($numitems == 0) {
                   12662:                 rmdir($path);
                   12663:             }
                   12664:             closedir($dirh);
                   12665:         }
                   12666:     }
                   12667:     return;
                   12668: }
                   12669: 
1.41      ng       12670: =pod
1.45      matthew  12671: 
1.1162    raeburn  12672: =item * &get_folder_hierarchy()
1.1068    raeburn  12673: 
                   12674: Provides hierarchy of names of folders/sub-folders containing the current
                   12675: item,
                   12676: 
                   12677: Inputs: 3
                   12678:      - $navmap - navmaps object
                   12679: 
                   12680:      - $map - url for map (either the trigger itself, or map containing
                   12681:                            the resource, which is the trigger).
                   12682: 
                   12683:      - $showitem - 1 => show title for map itself; 0 => do not show.
                   12684: 
                   12685: Outputs: 1 @pathitems - array of folder/subfolder names.
                   12686: 
                   12687: =cut
                   12688: 
                   12689: sub get_folder_hierarchy {
                   12690:     my ($navmap,$map,$showitem) = @_;
                   12691:     my @pathitems;
                   12692:     if (ref($navmap)) {
                   12693:         my $mapres = $navmap->getResourceByUrl($map);
                   12694:         if (ref($mapres)) {
                   12695:             my $pcslist = $mapres->map_hierarchy();
                   12696:             if ($pcslist ne '') {
                   12697:                 my @pcs = split(/,/,$pcslist);
                   12698:                 foreach my $pc (@pcs) {
                   12699:                     if ($pc == 1) {
1.1129    raeburn  12700:                         push(@pathitems,&mt('Main Content'));
1.1068    raeburn  12701:                     } else {
                   12702:                         my $res = $navmap->getByMapPc($pc);
                   12703:                         if (ref($res)) {
                   12704:                             my $title = $res->compTitle();
                   12705:                             $title =~ s/\W+/_/g;
                   12706:                             if ($title ne '') {
                   12707:                                 push(@pathitems,$title);
                   12708:                             }
                   12709:                         }
                   12710:                     }
                   12711:                 }
                   12712:             }
1.1071    raeburn  12713:             if ($showitem) {
                   12714:                 if ($mapres->{ID} eq '0.0') {
1.1129    raeburn  12715:                     push(@pathitems,&mt('Main Content'));
1.1071    raeburn  12716:                 } else {
                   12717:                     my $maptitle = $mapres->compTitle();
                   12718:                     $maptitle =~ s/\W+/_/g;
                   12719:                     if ($maptitle ne '') {
                   12720:                         push(@pathitems,$maptitle);
                   12721:                     }
1.1068    raeburn  12722:                 }
                   12723:             }
                   12724:         }
                   12725:     }
                   12726:     return @pathitems;
                   12727: }
                   12728: 
                   12729: =pod
                   12730: 
1.1015    raeburn  12731: =item * &get_turnedin_filepath()
                   12732: 
                   12733: Determines path in a user's portfolio file for storage of files uploaded
                   12734: to a specific essayresponse or dropbox item.
                   12735: 
                   12736: Inputs: 3 required + 1 optional.
                   12737: $symb is symb for resource, $uname and $udom are for current user (required).
                   12738: $caller is optional (can be "submission", if routine is called when storing
                   12739: an upoaded file when "Submit Answer" button was pressed).
                   12740: 
                   12741: Returns array containing $path and $multiresp. 
                   12742: $path is path in portfolio.  $multiresp is 1 if this resource contains more
                   12743: than one file upload item.  Callers of routine should append partid as a 
                   12744: subdirectory to $path in cases where $multiresp is 1.
                   12745: 
                   12746: Called by: homework/essayresponse.pm and homework/structuretags.pm
                   12747: 
                   12748: =cut
                   12749: 
                   12750: sub get_turnedin_filepath {
                   12751:     my ($symb,$uname,$udom,$caller) = @_;
                   12752:     my ($map,$resid,$resurl)=&Apache::lonnet::decode_symb($symb);
                   12753:     my $turnindir;
                   12754:     my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'turnindir');
                   12755:     $turnindir = $userhash{'turnindir'};
                   12756:     my ($path,$multiresp);
                   12757:     if ($turnindir eq '') {
                   12758:         if ($caller eq 'submission') {
                   12759:             $turnindir = &mt('turned in');
                   12760:             $turnindir =~ s/\W+/_/g;
                   12761:             my %newhash = (
                   12762:                             'turnindir' => $turnindir,
                   12763:                           );
                   12764:             &Apache::lonnet::put('environment',\%newhash,$udom,$uname);
                   12765:         }
                   12766:     }
                   12767:     if ($turnindir ne '') {
                   12768:         $path = '/'.$turnindir.'/';
                   12769:         my ($multipart,$turnin,@pathitems);
                   12770:         my $navmap = Apache::lonnavmaps::navmap->new();
                   12771:         if (defined($navmap)) {
                   12772:             my $mapres = $navmap->getResourceByUrl($map);
                   12773:             if (ref($mapres)) {
                   12774:                 my $pcslist = $mapres->map_hierarchy();
                   12775:                 if ($pcslist ne '') {
                   12776:                     foreach my $pc (split(/,/,$pcslist)) {
                   12777:                         my $res = $navmap->getByMapPc($pc);
                   12778:                         if (ref($res)) {
                   12779:                             my $title = $res->compTitle();
                   12780:                             $title =~ s/\W+/_/g;
                   12781:                             if ($title ne '') {
1.1149    raeburn  12782:                                 if (($pc > 1) && (length($title) > 12)) {
                   12783:                                     $title = substr($title,0,12);
                   12784:                                 }
1.1015    raeburn  12785:                                 push(@pathitems,$title);
                   12786:                             }
                   12787:                         }
                   12788:                     }
                   12789:                 }
                   12790:                 my $maptitle = $mapres->compTitle();
                   12791:                 $maptitle =~ s/\W+/_/g;
                   12792:                 if ($maptitle ne '') {
1.1149    raeburn  12793:                     if (length($maptitle) > 12) {
                   12794:                         $maptitle = substr($maptitle,0,12);
                   12795:                     }
1.1015    raeburn  12796:                     push(@pathitems,$maptitle);
                   12797:                 }
                   12798:                 unless ($env{'request.state'} eq 'construct') {
                   12799:                     my $res = $navmap->getBySymb($symb);
                   12800:                     if (ref($res)) {
                   12801:                         my $partlist = $res->parts();
                   12802:                         my $totaluploads = 0;
                   12803:                         if (ref($partlist) eq 'ARRAY') {
                   12804:                             foreach my $part (@{$partlist}) {
                   12805:                                 my @types = $res->responseType($part);
                   12806:                                 my @ids = $res->responseIds($part);
                   12807:                                 for (my $i=0; $i < scalar(@ids); $i++) {
                   12808:                                     if ($types[$i] eq 'essay') {
                   12809:                                         my $partid = $part.'_'.$ids[$i];
                   12810:                                         if (&Apache::lonnet::EXT("resource.$partid.uploadedfiletypes") ne '') {
                   12811:                                             $totaluploads ++;
                   12812:                                         }
                   12813:                                     }
                   12814:                                 }
                   12815:                             }
                   12816:                             if ($totaluploads > 1) {
                   12817:                                 $multiresp = 1;
                   12818:                             }
                   12819:                         }
                   12820:                     }
                   12821:                 }
                   12822:             } else {
                   12823:                 return;
                   12824:             }
                   12825:         } else {
                   12826:             return;
                   12827:         }
                   12828:         my $restitle=&Apache::lonnet::gettitle($symb);
                   12829:         $restitle =~ s/\W+/_/g;
                   12830:         if ($restitle eq '') {
                   12831:             $restitle = ($resurl =~ m{/[^/]+$});
                   12832:             if ($restitle eq '') {
                   12833:                 $restitle = time;
                   12834:             }
                   12835:         }
1.1149    raeburn  12836:         if (length($restitle) > 12) {
                   12837:             $restitle = substr($restitle,0,12);
                   12838:         }
1.1015    raeburn  12839:         push(@pathitems,$restitle);
                   12840:         $path .= join('/',@pathitems);
                   12841:     }
                   12842:     return ($path,$multiresp);
                   12843: }
                   12844: 
                   12845: =pod
                   12846: 
1.464     albertel 12847: =back
1.41      ng       12848: 
1.112     bowersj2 12849: =head1 CSV Upload/Handling functions
1.38      albertel 12850: 
1.41      ng       12851: =over 4
                   12852: 
1.648     raeburn  12853: =item * &upfile_store($r)
1.41      ng       12854: 
                   12855: Store uploaded file, $r should be the HTTP Request object,
1.258     albertel 12856: needs $env{'form.upfile'}
1.41      ng       12857: returns $datatoken to be put into hidden field
                   12858: 
                   12859: =cut
1.31      albertel 12860: 
                   12861: sub upfile_store {
                   12862:     my $r=shift;
1.258     albertel 12863:     $env{'form.upfile'}=~s/\r/\n/gs;
                   12864:     $env{'form.upfile'}=~s/\f/\n/gs;
                   12865:     $env{'form.upfile'}=~s/\n+/\n/gs;
                   12866:     $env{'form.upfile'}=~s/\n+$//gs;
1.31      albertel 12867: 
1.258     albertel 12868:     my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
                   12869: 	'_enroll_'.$env{'request.course.id'}.'_'.time.'_'.$$;
1.31      albertel 12870:     {
1.158     raeburn  12871:         my $datafile = $r->dir_config('lonDaemons').
                   12872:                            '/tmp/'.$datatoken.'.tmp';
                   12873:         if ( open(my $fh,">$datafile") ) {
1.258     albertel 12874:             print $fh $env{'form.upfile'};
1.158     raeburn  12875:             close($fh);
                   12876:         }
1.31      albertel 12877:     }
                   12878:     return $datatoken;
                   12879: }
                   12880: 
1.56      matthew  12881: =pod
                   12882: 
1.648     raeburn  12883: =item * &load_tmp_file($r)
1.41      ng       12884: 
                   12885: Load uploaded file from tmp, $r should be the HTTP Request object,
1.258     albertel 12886: needs $env{'form.datatoken'},
                   12887: sets $env{'form.upfile'} to the contents of the file
1.41      ng       12888: 
                   12889: =cut
1.31      albertel 12890: 
                   12891: sub load_tmp_file {
                   12892:     my $r=shift;
                   12893:     my @studentdata=();
                   12894:     {
1.158     raeburn  12895:         my $studentfile = $r->dir_config('lonDaemons').
1.258     albertel 12896:                               '/tmp/'.$env{'form.datatoken'}.'.tmp';
1.158     raeburn  12897:         if ( open(my $fh,"<$studentfile") ) {
                   12898:             @studentdata=<$fh>;
                   12899:             close($fh);
                   12900:         }
1.31      albertel 12901:     }
1.258     albertel 12902:     $env{'form.upfile'}=join('',@studentdata);
1.31      albertel 12903: }
                   12904: 
1.56      matthew  12905: =pod
                   12906: 
1.648     raeburn  12907: =item * &upfile_record_sep()
1.41      ng       12908: 
                   12909: Separate uploaded file into records
                   12910: returns array of records,
1.258     albertel 12911: needs $env{'form.upfile'} and $env{'form.upfiletype'}
1.41      ng       12912: 
                   12913: =cut
1.31      albertel 12914: 
                   12915: sub upfile_record_sep {
1.258     albertel 12916:     if ($env{'form.upfiletype'} eq 'xml') {
1.31      albertel 12917:     } else {
1.248     albertel 12918: 	my @records;
1.258     albertel 12919: 	foreach my $line (split(/\n/,$env{'form.upfile'})) {
1.248     albertel 12920: 	    if ($line=~/^\s*$/) { next; }
                   12921: 	    push(@records,$line);
                   12922: 	}
                   12923: 	return @records;
1.31      albertel 12924:     }
                   12925: }
                   12926: 
1.56      matthew  12927: =pod
                   12928: 
1.648     raeburn  12929: =item * &record_sep($record)
1.41      ng       12930: 
1.258     albertel 12931: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $env{'form.upfiletype'}
1.41      ng       12932: 
                   12933: =cut
                   12934: 
1.263     www      12935: sub takeleft {
                   12936:     my $index=shift;
                   12937:     return substr('0000'.$index,-4,4);
                   12938: }
                   12939: 
1.31      albertel 12940: sub record_sep {
                   12941:     my $record=shift;
                   12942:     my %components=();
1.258     albertel 12943:     if ($env{'form.upfiletype'} eq 'xml') {
                   12944:     } elsif ($env{'form.upfiletype'} eq 'space') {
1.31      albertel 12945:         my $i=0;
1.356     albertel 12946:         foreach my $field (split(/\s+/,$record)) {
1.31      albertel 12947:             $field=~s/^(\"|\')//;
                   12948:             $field=~s/(\"|\')$//;
1.263     www      12949:             $components{&takeleft($i)}=$field;
1.31      albertel 12950:             $i++;
                   12951:         }
1.258     albertel 12952:     } elsif ($env{'form.upfiletype'} eq 'tab') {
1.31      albertel 12953:         my $i=0;
1.356     albertel 12954:         foreach my $field (split(/\t/,$record)) {
1.31      albertel 12955:             $field=~s/^(\"|\')//;
                   12956:             $field=~s/(\"|\')$//;
1.263     www      12957:             $components{&takeleft($i)}=$field;
1.31      albertel 12958:             $i++;
                   12959:         }
                   12960:     } else {
1.561     www      12961:         my $separator=',';
1.480     banghart 12962:         if ($env{'form.upfiletype'} eq 'semisv') {
1.561     www      12963:             $separator=';';
1.480     banghart 12964:         }
1.31      albertel 12965:         my $i=0;
1.561     www      12966: # the character we are looking for to indicate the end of a quote or a record 
                   12967:         my $looking_for=$separator;
                   12968: # do not add the characters to the fields
                   12969:         my $ignore=0;
                   12970: # we just encountered a separator (or the beginning of the record)
                   12971:         my $just_found_separator=1;
                   12972: # store the field we are working on here
                   12973:         my $field='';
                   12974: # work our way through all characters in record
                   12975:         foreach my $character ($record=~/(.)/g) {
                   12976:             if ($character eq $looking_for) {
                   12977:                if ($character ne $separator) {
                   12978: # Found the end of a quote, again looking for separator
                   12979:                   $looking_for=$separator;
                   12980:                   $ignore=1;
                   12981:                } else {
                   12982: # Found a separator, store away what we got
                   12983:                   $components{&takeleft($i)}=$field;
                   12984: 	          $i++;
                   12985:                   $just_found_separator=1;
                   12986:                   $ignore=0;
                   12987:                   $field='';
                   12988:                }
                   12989:                next;
                   12990:             }
                   12991: # single or double quotation marks after a separator indicate beginning of a quote
                   12992: # we are now looking for the end of the quote and need to ignore separators
                   12993:             if ((($character eq '"') || ($character eq "'")) && ($just_found_separator))  {
                   12994:                $looking_for=$character;
                   12995:                next;
                   12996:             }
                   12997: # ignore would be true after we reached the end of a quote
                   12998:             if ($ignore) { next; }
                   12999:             if (($just_found_separator) && ($character=~/\s/)) { next; }
                   13000:             $field.=$character;
                   13001:             $just_found_separator=0; 
1.31      albertel 13002:         }
1.561     www      13003: # catch the very last entry, since we never encountered the separator
                   13004:         $components{&takeleft($i)}=$field;
1.31      albertel 13005:     }
                   13006:     return %components;
                   13007: }
                   13008: 
1.144     matthew  13009: ######################################################
                   13010: ######################################################
                   13011: 
1.56      matthew  13012: =pod
                   13013: 
1.648     raeburn  13014: =item * &upfile_select_html()
1.41      ng       13015: 
1.144     matthew  13016: Return HTML code to select a file from the users machine and specify 
                   13017: the file type.
1.41      ng       13018: 
                   13019: =cut
                   13020: 
1.144     matthew  13021: ######################################################
                   13022: ######################################################
1.31      albertel 13023: sub upfile_select_html {
1.144     matthew  13024:     my %Types = (
                   13025:                  csv   => &mt('CSV (comma separated values, spreadsheet)'),
1.480     banghart 13026:                  semisv => &mt('Semicolon separated values'),
1.144     matthew  13027:                  space => &mt('Space separated'),
                   13028:                  tab   => &mt('Tabulator separated'),
                   13029: #                 xml   => &mt('HTML/XML'),
                   13030:                  );
                   13031:     my $Str = '<input type="file" name="upfile" size="50" />'.
1.727     riegler  13032:         '<br />'.&mt('Type').': <select name="upfiletype">';
1.144     matthew  13033:     foreach my $type (sort(keys(%Types))) {
                   13034:         $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
                   13035:     }
                   13036:     $Str .= "</select>\n";
                   13037:     return $Str;
1.31      albertel 13038: }
                   13039: 
1.301     albertel 13040: sub get_samples {
                   13041:     my ($records,$toget) = @_;
                   13042:     my @samples=({});
                   13043:     my $got=0;
                   13044:     foreach my $rec (@$records) {
                   13045: 	my %temp = &record_sep($rec);
                   13046: 	if (! grep(/\S/, values(%temp))) { next; }
                   13047: 	if (%temp) {
                   13048: 	    $samples[$got]=\%temp;
                   13049: 	    $got++;
                   13050: 	    if ($got == $toget) { last; }
                   13051: 	}
                   13052:     }
                   13053:     return \@samples;
                   13054: }
                   13055: 
1.144     matthew  13056: ######################################################
                   13057: ######################################################
                   13058: 
1.56      matthew  13059: =pod
                   13060: 
1.648     raeburn  13061: =item * &csv_print_samples($r,$records)
1.41      ng       13062: 
                   13063: Prints a table of sample values from each column uploaded $r is an
                   13064: Apache Request ref, $records is an arrayref from
                   13065: &Apache::loncommon::upfile_record_sep
                   13066: 
                   13067: =cut
                   13068: 
1.144     matthew  13069: ######################################################
                   13070: ######################################################
1.31      albertel 13071: sub csv_print_samples {
                   13072:     my ($r,$records) = @_;
1.662     bisitz   13073:     my $samples = &get_samples($records,5);
1.301     albertel 13074: 
1.594     raeburn  13075:     $r->print(&mt('Samples').'<br />'.&start_data_table().
                   13076:               &start_data_table_header_row());
1.356     albertel 13077:     foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) { 
1.845     bisitz   13078:         $r->print('<th>'.&mt('Column [_1]',($sample+1)).'</th>'); }
1.594     raeburn  13079:     $r->print(&end_data_table_header_row());
1.301     albertel 13080:     foreach my $hash (@$samples) {
1.594     raeburn  13081: 	$r->print(&start_data_table_row());
1.356     albertel 13082: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
1.31      albertel 13083: 	    $r->print('<td>');
1.356     albertel 13084: 	    if (defined($$hash{$sample})) { $r->print($$hash{$sample}); }
1.31      albertel 13085: 	    $r->print('</td>');
                   13086: 	}
1.594     raeburn  13087: 	$r->print(&end_data_table_row());
1.31      albertel 13088:     }
1.594     raeburn  13089:     $r->print(&end_data_table().'<br />'."\n");
1.31      albertel 13090: }
                   13091: 
1.144     matthew  13092: ######################################################
                   13093: ######################################################
                   13094: 
1.56      matthew  13095: =pod
                   13096: 
1.648     raeburn  13097: =item * &csv_print_select_table($r,$records,$d)
1.41      ng       13098: 
                   13099: Prints a table to create associations between values and table columns.
1.144     matthew  13100: 
1.41      ng       13101: $r is an Apache Request ref,
                   13102: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
1.174     matthew  13103: $d is an array of 2 element arrays (internal name, displayed name,defaultcol)
1.41      ng       13104: 
                   13105: =cut
                   13106: 
1.144     matthew  13107: ######################################################
                   13108: ######################################################
1.31      albertel 13109: sub csv_print_select_table {
                   13110:     my ($r,$records,$d) = @_;
1.301     albertel 13111:     my $i=0;
                   13112:     my $samples = &get_samples($records,1);
1.144     matthew  13113:     $r->print(&mt('Associate columns with student attributes.')."\n".
1.594     raeburn  13114: 	      &start_data_table().&start_data_table_header_row().
1.144     matthew  13115:               '<th>'.&mt('Attribute').'</th>'.
1.594     raeburn  13116:               '<th>'.&mt('Column').'</th>'.
                   13117:               &end_data_table_header_row()."\n");
1.356     albertel 13118:     foreach my $array_ref (@$d) {
                   13119: 	my ($value,$display,$defaultcol)=@{ $array_ref };
1.729     raeburn  13120: 	$r->print(&start_data_table_row().'<td>'.$display.'</td>');
1.31      albertel 13121: 
1.875     bisitz   13122: 	$r->print('<td><select name="f'.$i.'"'.
1.32      matthew  13123: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.31      albertel 13124: 	$r->print('<option value="none"></option>');
1.356     albertel 13125: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
                   13126: 	    $r->print('<option value="'.$sample.'"'.
                   13127:                       ($sample eq $defaultcol ? ' selected="selected" ' : '').
1.662     bisitz   13128:                       '>'.&mt('Column [_1]',($sample+1)).'</option>');
1.31      albertel 13129: 	}
1.594     raeburn  13130: 	$r->print('</select></td>'.&end_data_table_row()."\n");
1.31      albertel 13131: 	$i++;
                   13132:     }
1.594     raeburn  13133:     $r->print(&end_data_table());
1.31      albertel 13134:     $i--;
                   13135:     return $i;
                   13136: }
1.56      matthew  13137: 
1.144     matthew  13138: ######################################################
                   13139: ######################################################
                   13140: 
1.56      matthew  13141: =pod
1.31      albertel 13142: 
1.648     raeburn  13143: =item * &csv_samples_select_table($r,$records,$d)
1.41      ng       13144: 
                   13145: Prints a table of sample values from the upload and can make associate samples to internal names.
                   13146: 
                   13147: $r is an Apache Request ref,
                   13148: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
                   13149: $d is an array of 2 element arrays (internal name, displayed name)
                   13150: 
                   13151: =cut
                   13152: 
1.144     matthew  13153: ######################################################
                   13154: ######################################################
1.31      albertel 13155: sub csv_samples_select_table {
                   13156:     my ($r,$records,$d) = @_;
                   13157:     my $i=0;
1.144     matthew  13158:     #
1.662     bisitz   13159:     my $max_samples = 5;
                   13160:     my $samples = &get_samples($records,$max_samples);
1.594     raeburn  13161:     $r->print(&start_data_table().
                   13162:               &start_data_table_header_row().'<th>'.
                   13163:               &mt('Field').'</th><th>'.&mt('Samples').'</th>'.
                   13164:               &end_data_table_header_row());
1.301     albertel 13165: 
                   13166:     foreach my $key (sort(keys(%{ $samples->[0] }))) {
1.594     raeburn  13167: 	$r->print(&start_data_table_row().'<td><select name="f'.$i.'"'.
1.32      matthew  13168: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.301     albertel 13169: 	foreach my $option (@$d) {
                   13170: 	    my ($value,$display,$defaultcol)=@{ $option };
1.174     matthew  13171: 	    $r->print('<option value="'.$value.'"'.
1.253     albertel 13172:                       ($i eq $defaultcol ? ' selected="selected" ':'').'>'.
1.174     matthew  13173:                       $display.'</option>');
1.31      albertel 13174: 	}
                   13175: 	$r->print('</select></td><td>');
1.662     bisitz   13176: 	foreach my $line (0..($max_samples-1)) {
1.301     albertel 13177: 	    if (defined($samples->[$line]{$key})) { 
                   13178: 		$r->print($samples->[$line]{$key}."<br />\n"); 
                   13179: 	    }
                   13180: 	}
1.594     raeburn  13181: 	$r->print('</td>'.&end_data_table_row());
1.31      albertel 13182: 	$i++;
                   13183:     }
1.594     raeburn  13184:     $r->print(&end_data_table());
1.31      albertel 13185:     $i--;
                   13186:     return($i);
1.115     matthew  13187: }
                   13188: 
1.144     matthew  13189: ######################################################
                   13190: ######################################################
                   13191: 
1.115     matthew  13192: =pod
                   13193: 
1.648     raeburn  13194: =item * &clean_excel_name($name)
1.115     matthew  13195: 
                   13196: Returns a replacement for $name which does not contain any illegal characters.
                   13197: 
                   13198: =cut
                   13199: 
1.144     matthew  13200: ######################################################
                   13201: ######################################################
1.115     matthew  13202: sub clean_excel_name {
                   13203:     my ($name) = @_;
                   13204:     $name =~ s/[:\*\?\/\\]//g;
                   13205:     if (length($name) > 31) {
                   13206:         $name = substr($name,0,31);
                   13207:     }
                   13208:     return $name;
1.25      albertel 13209: }
1.84      albertel 13210: 
1.85      albertel 13211: =pod
                   13212: 
1.648     raeburn  13213: =item * &check_if_partid_hidden($id,$symb,$udom,$uname)
1.85      albertel 13214: 
                   13215: Returns either 1 or undef
                   13216: 
                   13217: 1 if the part is to be hidden, undef if it is to be shown
                   13218: 
                   13219: Arguments are:
                   13220: 
                   13221: $id the id of the part to be checked
                   13222: $symb, optional the symb of the resource to check
                   13223: $udom, optional the domain of the user to check for
                   13224: $uname, optional the username of the user to check for
                   13225: 
                   13226: =cut
1.84      albertel 13227: 
                   13228: sub check_if_partid_hidden {
                   13229:     my ($id,$symb,$udom,$uname) = @_;
1.133     albertel 13230:     my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
1.84      albertel 13231: 					 $symb,$udom,$uname);
1.141     albertel 13232:     my $truth=1;
                   13233:     #if the string starts with !, then the list is the list to show not hide
                   13234:     if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
1.84      albertel 13235:     my @hiddenlist=split(/,/,$hiddenparts);
                   13236:     foreach my $checkid (@hiddenlist) {
1.141     albertel 13237: 	if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
1.84      albertel 13238:     }
1.141     albertel 13239:     return !$truth;
1.84      albertel 13240: }
1.127     matthew  13241: 
1.138     matthew  13242: 
                   13243: ############################################################
                   13244: ############################################################
                   13245: 
                   13246: =pod
                   13247: 
1.157     matthew  13248: =back 
                   13249: 
1.138     matthew  13250: =head1 cgi-bin script and graphing routines
                   13251: 
1.157     matthew  13252: =over 4
                   13253: 
1.648     raeburn  13254: =item * &get_cgi_id()
1.138     matthew  13255: 
                   13256: Inputs: none
                   13257: 
                   13258: Returns an id which can be used to pass environment variables
                   13259: to various cgi-bin scripts.  These environment variables will
                   13260: be removed from the users environment after a given time by
                   13261: the routine &Apache::lonnet::transfer_profile_to_env.
                   13262: 
                   13263: =cut
                   13264: 
                   13265: ############################################################
                   13266: ############################################################
1.152     albertel 13267: my $uniq=0;
1.136     matthew  13268: sub get_cgi_id {
1.154     albertel 13269:     $uniq=($uniq+1)%100000;
1.280     albertel 13270:     return (time.'_'.$$.'_'.$uniq);
1.136     matthew  13271: }
                   13272: 
1.127     matthew  13273: ############################################################
                   13274: ############################################################
                   13275: 
                   13276: =pod
                   13277: 
1.648     raeburn  13278: =item * &DrawBarGraph()
1.127     matthew  13279: 
1.138     matthew  13280: Facilitates the plotting of data in a (stacked) bar graph.
                   13281: Puts plot definition data into the users environment in order for 
                   13282: graph.png to plot it.  Returns an <img> tag for the plot.
                   13283: The bars on the plot are labeled '1','2',...,'n'.
                   13284: 
                   13285: Inputs:
                   13286: 
                   13287: =over 4
                   13288: 
                   13289: =item $Title: string, the title of the plot
                   13290: 
                   13291: =item $xlabel: string, text describing the X-axis of the plot
                   13292: 
                   13293: =item $ylabel: string, text describing the Y-axis of the plot
                   13294: 
                   13295: =item $Max: scalar, the maximum Y value to use in the plot
                   13296: If $Max is < any data point, the graph will not be rendered.
                   13297: 
1.140     matthew  13298: =item $colors: array ref holding the colors to be used for the data sets when
1.138     matthew  13299: they are plotted.  If undefined, default values will be used.
                   13300: 
1.178     matthew  13301: =item $labels: array ref holding the labels to use on the x-axis for the bars.
                   13302: 
1.138     matthew  13303: =item @Values: An array of array references.  Each array reference holds data
                   13304: to be plotted in a stacked bar chart.
                   13305: 
1.239     matthew  13306: =item If the final element of @Values is a hash reference the key/value
                   13307: pairs will be added to the graph definition.
                   13308: 
1.138     matthew  13309: =back
                   13310: 
                   13311: Returns:
                   13312: 
                   13313: An <img> tag which references graph.png and the appropriate identifying
                   13314: information for the plot.
                   13315: 
1.127     matthew  13316: =cut
                   13317: 
                   13318: ############################################################
                   13319: ############################################################
1.134     matthew  13320: sub DrawBarGraph {
1.178     matthew  13321:     my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
1.134     matthew  13322:     #
                   13323:     if (! defined($colors)) {
                   13324:         $colors = ['#33ff00', 
                   13325:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
                   13326:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
                   13327:                   ]; 
                   13328:     }
1.228     matthew  13329:     my $extra_settings = {};
                   13330:     if (ref($Values[-1]) eq 'HASH') {
                   13331:         $extra_settings = pop(@Values);
                   13332:     }
1.127     matthew  13333:     #
1.136     matthew  13334:     my $identifier = &get_cgi_id();
                   13335:     my $id = 'cgi.'.$identifier;        
1.129     matthew  13336:     if (! @Values || ref($Values[0]) ne 'ARRAY') {
1.127     matthew  13337:         return '';
                   13338:     }
1.225     matthew  13339:     #
                   13340:     my @Labels;
                   13341:     if (defined($labels)) {
                   13342:         @Labels = @$labels;
                   13343:     } else {
                   13344:         for (my $i=0;$i<@{$Values[0]};$i++) {
                   13345:             push (@Labels,$i+1);
                   13346:         }
                   13347:     }
                   13348:     #
1.129     matthew  13349:     my $NumBars = scalar(@{$Values[0]});
1.225     matthew  13350:     if ($NumBars < scalar(@Labels)) { $NumBars = scalar(@Labels); }
1.129     matthew  13351:     my %ValuesHash;
                   13352:     my $NumSets=1;
                   13353:     foreach my $array (@Values) {
                   13354:         next if (! ref($array));
1.136     matthew  13355:         $ValuesHash{$id.'.data.'.$NumSets++} = 
1.132     matthew  13356:             join(',',@$array);
1.129     matthew  13357:     }
1.127     matthew  13358:     #
1.136     matthew  13359:     my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
1.225     matthew  13360:     if ($NumBars < 3) {
                   13361:         $width = 120+$NumBars*32;
1.220     matthew  13362:         $xskip = 1;
1.225     matthew  13363:         $bar_width = 30;
                   13364:     } elsif ($NumBars < 5) {
                   13365:         $width = 120+$NumBars*20;
                   13366:         $xskip = 1;
                   13367:         $bar_width = 20;
1.220     matthew  13368:     } elsif ($NumBars < 10) {
1.136     matthew  13369:         $width = 120+$NumBars*15;
                   13370:         $xskip = 1;
                   13371:         $bar_width = 15;
                   13372:     } elsif ($NumBars <= 25) {
                   13373:         $width = 120+$NumBars*11;
                   13374:         $xskip = 5;
                   13375:         $bar_width = 8;
                   13376:     } elsif ($NumBars <= 50) {
                   13377:         $width = 120+$NumBars*8;
                   13378:         $xskip = 5;
                   13379:         $bar_width = 4;
                   13380:     } else {
                   13381:         $width = 120+$NumBars*8;
                   13382:         $xskip = 5;
                   13383:         $bar_width = 4;
                   13384:     }
                   13385:     #
1.137     matthew  13386:     $Max = 1 if ($Max < 1);
                   13387:     if ( int($Max) < $Max ) {
                   13388:         $Max++;
                   13389:         $Max = int($Max);
                   13390:     }
1.127     matthew  13391:     $Title  = '' if (! defined($Title));
                   13392:     $xlabel = '' if (! defined($xlabel));
                   13393:     $ylabel = '' if (! defined($ylabel));
1.369     www      13394:     $ValuesHash{$id.'.title'}    = &escape($Title);
                   13395:     $ValuesHash{$id.'.xlabel'}   = &escape($xlabel);
                   13396:     $ValuesHash{$id.'.ylabel'}   = &escape($ylabel);
1.137     matthew  13397:     $ValuesHash{$id.'.y_max_value'} = $Max;
1.136     matthew  13398:     $ValuesHash{$id.'.NumBars'}  = $NumBars;
                   13399:     $ValuesHash{$id.'.NumSets'}  = $NumSets;
                   13400:     $ValuesHash{$id.'.PlotType'} = 'bar';
                   13401:     $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   13402:     $ValuesHash{$id.'.height'}   = $height;
                   13403:     $ValuesHash{$id.'.width'}    = $width;
                   13404:     $ValuesHash{$id.'.xskip'}    = $xskip;
                   13405:     $ValuesHash{$id.'.bar_width'} = $bar_width;
                   13406:     $ValuesHash{$id.'.labels'} = join(',',@Labels);
1.127     matthew  13407:     #
1.228     matthew  13408:     # Deal with other parameters
                   13409:     while (my ($key,$value) = each(%$extra_settings)) {
                   13410:         $ValuesHash{$id.'.'.$key} = $value;
                   13411:     }
                   13412:     #
1.646     raeburn  13413:     &Apache::lonnet::appenv(\%ValuesHash);
1.137     matthew  13414:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   13415: }
                   13416: 
                   13417: ############################################################
                   13418: ############################################################
                   13419: 
                   13420: =pod
                   13421: 
1.648     raeburn  13422: =item * &DrawXYGraph()
1.137     matthew  13423: 
1.138     matthew  13424: Facilitates the plotting of data in an XY graph.
                   13425: Puts plot definition data into the users environment in order for 
                   13426: graph.png to plot it.  Returns an <img> tag for the plot.
                   13427: 
                   13428: Inputs:
                   13429: 
                   13430: =over 4
                   13431: 
                   13432: =item $Title: string, the title of the plot
                   13433: 
                   13434: =item $xlabel: string, text describing the X-axis of the plot
                   13435: 
                   13436: =item $ylabel: string, text describing the Y-axis of the plot
                   13437: 
                   13438: =item $Max: scalar, the maximum Y value to use in the plot
                   13439: If $Max is < any data point, the graph will not be rendered.
                   13440: 
                   13441: =item $colors: Array ref containing the hex color codes for the data to be 
                   13442: plotted in.  If undefined, default values will be used.
                   13443: 
                   13444: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   13445: 
                   13446: =item $Ydata: Array ref containing Array refs.  
1.185     www      13447: Each of the contained arrays will be plotted as a separate curve.
1.138     matthew  13448: 
                   13449: =item %Values: hash indicating or overriding any default values which are 
                   13450: passed to graph.png.  
                   13451: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   13452: 
                   13453: =back
                   13454: 
                   13455: Returns:
                   13456: 
                   13457: An <img> tag which references graph.png and the appropriate identifying
                   13458: information for the plot.
                   13459: 
1.137     matthew  13460: =cut
                   13461: 
                   13462: ############################################################
                   13463: ############################################################
                   13464: sub DrawXYGraph {
                   13465:     my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
                   13466:     #
                   13467:     # Create the identifier for the graph
                   13468:     my $identifier = &get_cgi_id();
                   13469:     my $id = 'cgi.'.$identifier;
                   13470:     #
                   13471:     $Title  = '' if (! defined($Title));
                   13472:     $xlabel = '' if (! defined($xlabel));
                   13473:     $ylabel = '' if (! defined($ylabel));
                   13474:     my %ValuesHash = 
                   13475:         (
1.369     www      13476:          $id.'.title'  => &escape($Title),
                   13477:          $id.'.xlabel' => &escape($xlabel),
                   13478:          $id.'.ylabel' => &escape($ylabel),
1.137     matthew  13479:          $id.'.y_max_value'=> $Max,
                   13480:          $id.'.labels'     => join(',',@$Xlabels),
                   13481:          $id.'.PlotType'   => 'XY',
                   13482:          );
                   13483:     #
                   13484:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   13485:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   13486:     }
                   13487:     #
                   13488:     if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
                   13489:         return '';
                   13490:     }
                   13491:     my $NumSets=1;
1.138     matthew  13492:     foreach my $array (@{$Ydata}){
1.137     matthew  13493:         next if (! ref($array));
                   13494:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
                   13495:     }
1.138     matthew  13496:     $ValuesHash{$id.'.NumSets'} = $NumSets-1;
1.137     matthew  13497:     #
                   13498:     # Deal with other parameters
                   13499:     while (my ($key,$value) = each(%Values)) {
                   13500:         $ValuesHash{$id.'.'.$key} = $value;
1.127     matthew  13501:     }
                   13502:     #
1.646     raeburn  13503:     &Apache::lonnet::appenv(\%ValuesHash);
1.136     matthew  13504:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   13505: }
                   13506: 
                   13507: ############################################################
                   13508: ############################################################
                   13509: 
                   13510: =pod
                   13511: 
1.648     raeburn  13512: =item * &DrawXYYGraph()
1.138     matthew  13513: 
                   13514: Facilitates the plotting of data in an XY graph with two Y axes.
                   13515: Puts plot definition data into the users environment in order for 
                   13516: graph.png to plot it.  Returns an <img> tag for the plot.
                   13517: 
                   13518: Inputs:
                   13519: 
                   13520: =over 4
                   13521: 
                   13522: =item $Title: string, the title of the plot
                   13523: 
                   13524: =item $xlabel: string, text describing the X-axis of the plot
                   13525: 
                   13526: =item $ylabel: string, text describing the Y-axis of the plot
                   13527: 
                   13528: =item $colors: Array ref containing the hex color codes for the data to be 
                   13529: plotted in.  If undefined, default values will be used.
                   13530: 
                   13531: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   13532: 
                   13533: =item $Ydata1: The first data set
                   13534: 
                   13535: =item $Min1: The minimum value of the left Y-axis
                   13536: 
                   13537: =item $Max1: The maximum value of the left Y-axis
                   13538: 
                   13539: =item $Ydata2: The second data set
                   13540: 
                   13541: =item $Min2: The minimum value of the right Y-axis
                   13542: 
                   13543: =item $Max2: The maximum value of the left Y-axis
                   13544: 
                   13545: =item %Values: hash indicating or overriding any default values which are 
                   13546: passed to graph.png.  
                   13547: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   13548: 
                   13549: =back
                   13550: 
                   13551: Returns:
                   13552: 
                   13553: An <img> tag which references graph.png and the appropriate identifying
                   13554: information for the plot.
1.136     matthew  13555: 
                   13556: =cut
                   13557: 
                   13558: ############################################################
                   13559: ############################################################
1.137     matthew  13560: sub DrawXYYGraph {
                   13561:     my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
                   13562:                                         $Ydata2,$Min2,$Max2,%Values)=@_;
1.136     matthew  13563:     #
                   13564:     # Create the identifier for the graph
                   13565:     my $identifier = &get_cgi_id();
                   13566:     my $id = 'cgi.'.$identifier;
                   13567:     #
                   13568:     $Title  = '' if (! defined($Title));
                   13569:     $xlabel = '' if (! defined($xlabel));
                   13570:     $ylabel = '' if (! defined($ylabel));
                   13571:     my %ValuesHash = 
                   13572:         (
1.369     www      13573:          $id.'.title'  => &escape($Title),
                   13574:          $id.'.xlabel' => &escape($xlabel),
                   13575:          $id.'.ylabel' => &escape($ylabel),
1.136     matthew  13576:          $id.'.labels' => join(',',@$Xlabels),
                   13577:          $id.'.PlotType' => 'XY',
                   13578:          $id.'.NumSets' => 2,
1.137     matthew  13579:          $id.'.two_axes' => 1,
                   13580:          $id.'.y1_max_value' => $Max1,
                   13581:          $id.'.y1_min_value' => $Min1,
                   13582:          $id.'.y2_max_value' => $Max2,
                   13583:          $id.'.y2_min_value' => $Min2,
1.136     matthew  13584:          );
                   13585:     #
1.137     matthew  13586:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   13587:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   13588:     }
                   13589:     #
                   13590:     if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
                   13591:         ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
1.136     matthew  13592:         return '';
                   13593:     }
                   13594:     my $NumSets=1;
1.137     matthew  13595:     foreach my $array ($Ydata1,$Ydata2){
1.136     matthew  13596:         next if (! ref($array));
                   13597:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
1.137     matthew  13598:     }
                   13599:     #
                   13600:     # Deal with other parameters
                   13601:     while (my ($key,$value) = each(%Values)) {
                   13602:         $ValuesHash{$id.'.'.$key} = $value;
1.136     matthew  13603:     }
                   13604:     #
1.646     raeburn  13605:     &Apache::lonnet::appenv(\%ValuesHash);
1.130     albertel 13606:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
1.139     matthew  13607: }
                   13608: 
                   13609: ############################################################
                   13610: ############################################################
                   13611: 
                   13612: =pod
                   13613: 
1.157     matthew  13614: =back 
                   13615: 
1.139     matthew  13616: =head1 Statistics helper routines?  
                   13617: 
                   13618: Bad place for them but what the hell.
                   13619: 
1.157     matthew  13620: =over 4
                   13621: 
1.648     raeburn  13622: =item * &chartlink()
1.139     matthew  13623: 
                   13624: Returns a link to the chart for a specific student.  
                   13625: 
                   13626: Inputs:
                   13627: 
                   13628: =over 4
                   13629: 
                   13630: =item $linktext: The text of the link
                   13631: 
                   13632: =item $sname: The students username
                   13633: 
                   13634: =item $sdomain: The students domain
                   13635: 
                   13636: =back
                   13637: 
1.157     matthew  13638: =back
                   13639: 
1.139     matthew  13640: =cut
                   13641: 
                   13642: ############################################################
                   13643: ############################################################
                   13644: sub chartlink {
                   13645:     my ($linktext, $sname, $sdomain) = @_;
                   13646:     my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
1.369     www      13647:         '&amp;SelectedStudent='.&escape($sname.':'.$sdomain).
1.219     albertel 13648:         '&amp;chartoutputmode='.HTML::Entities::encode('html, with all links').
1.139     matthew  13649:        '">'.$linktext.'</a>';
1.153     matthew  13650: }
                   13651: 
                   13652: #######################################################
                   13653: #######################################################
                   13654: 
                   13655: =pod
                   13656: 
                   13657: =head1 Course Environment Routines
1.157     matthew  13658: 
                   13659: =over 4
1.153     matthew  13660: 
1.648     raeburn  13661: =item * &restore_course_settings()
1.153     matthew  13662: 
1.648     raeburn  13663: =item * &store_course_settings()
1.153     matthew  13664: 
                   13665: Restores/Store indicated form parameters from the course environment.
                   13666: Will not overwrite existing values of the form parameters.
                   13667: 
                   13668: Inputs: 
                   13669: a scalar describing the data (e.g. 'chart', 'problem_analysis')
                   13670: 
                   13671: a hash ref describing the data to be stored.  For example:
                   13672:    
                   13673: %Save_Parameters = ('Status' => 'scalar',
                   13674:     'chartoutputmode' => 'scalar',
                   13675:     'chartoutputdata' => 'scalar',
                   13676:     'Section' => 'array',
1.373     raeburn  13677:     'Group' => 'array',
1.153     matthew  13678:     'StudentData' => 'array',
                   13679:     'Maps' => 'array');
                   13680: 
                   13681: Returns: both routines return nothing
                   13682: 
1.631     raeburn  13683: =back
                   13684: 
1.153     matthew  13685: =cut
                   13686: 
                   13687: #######################################################
                   13688: #######################################################
                   13689: sub store_course_settings {
1.496     albertel 13690:     return &store_settings($env{'request.course.id'},@_);
                   13691: }
                   13692: 
                   13693: sub store_settings {
1.153     matthew  13694:     # save to the environment
                   13695:     # appenv the same items, just to be safe
1.300     albertel 13696:     my $udom  = $env{'user.domain'};
                   13697:     my $uname = $env{'user.name'};
1.496     albertel 13698:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  13699:     my %SaveHash;
                   13700:     my %AppHash;
                   13701:     while (my ($setting,$type) = each(%$Settings)) {
1.496     albertel 13702:         my $basename = join('.','internal',$context,$prefix,$setting);
1.300     albertel 13703:         my $envname = 'environment.'.$basename;
1.258     albertel 13704:         if (exists($env{'form.'.$setting})) {
1.153     matthew  13705:             # Save this value away
                   13706:             if ($type eq 'scalar' &&
1.258     albertel 13707:                 (! exists($env{$envname}) || 
                   13708:                  $env{$envname} ne $env{'form.'.$setting})) {
                   13709:                 $SaveHash{$basename} = $env{'form.'.$setting};
                   13710:                 $AppHash{$envname}   = $env{'form.'.$setting};
1.153     matthew  13711:             } elsif ($type eq 'array') {
                   13712:                 my $stored_form;
1.258     albertel 13713:                 if (ref($env{'form.'.$setting})) {
1.153     matthew  13714:                     $stored_form = join(',',
                   13715:                                         map {
1.369     www      13716:                                             &escape($_);
1.258     albertel 13717:                                         } sort(@{$env{'form.'.$setting}}));
1.153     matthew  13718:                 } else {
                   13719:                     $stored_form = 
1.369     www      13720:                         &escape($env{'form.'.$setting});
1.153     matthew  13721:                 }
                   13722:                 # Determine if the array contents are the same.
1.258     albertel 13723:                 if ($stored_form ne $env{$envname}) {
1.153     matthew  13724:                     $SaveHash{$basename} = $stored_form;
                   13725:                     $AppHash{$envname}   = $stored_form;
                   13726:                 }
                   13727:             }
                   13728:         }
                   13729:     }
                   13730:     my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
1.300     albertel 13731:                                           $udom,$uname);
1.153     matthew  13732:     if ($put_result !~ /^(ok|delayed)/) {
                   13733:         &Apache::lonnet::logthis('unable to save form parameters, '.
                   13734:                                  'got error:'.$put_result);
                   13735:     }
                   13736:     # Make sure these settings stick around in this session, too
1.646     raeburn  13737:     &Apache::lonnet::appenv(\%AppHash);
1.153     matthew  13738:     return;
                   13739: }
                   13740: 
                   13741: sub restore_course_settings {
1.499     albertel 13742:     return &restore_settings($env{'request.course.id'},@_);
1.496     albertel 13743: }
                   13744: 
                   13745: sub restore_settings {
                   13746:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  13747:     while (my ($setting,$type) = each(%$Settings)) {
1.258     albertel 13748:         next if (exists($env{'form.'.$setting}));
1.496     albertel 13749:         my $envname = 'environment.internal.'.$context.'.'.$prefix.
1.153     matthew  13750:             '.'.$setting;
1.258     albertel 13751:         if (exists($env{$envname})) {
1.153     matthew  13752:             if ($type eq 'scalar') {
1.258     albertel 13753:                 $env{'form.'.$setting} = $env{$envname};
1.153     matthew  13754:             } elsif ($type eq 'array') {
1.258     albertel 13755:                 $env{'form.'.$setting} = [ 
1.153     matthew  13756:                                            map { 
1.369     www      13757:                                                &unescape($_); 
1.258     albertel 13758:                                            } split(',',$env{$envname})
1.153     matthew  13759:                                            ];
                   13760:             }
                   13761:         }
                   13762:     }
1.127     matthew  13763: }
                   13764: 
1.618     raeburn  13765: #######################################################
                   13766: #######################################################
                   13767: 
                   13768: =pod
                   13769: 
                   13770: =head1 Domain E-mail Routines  
                   13771: 
                   13772: =over 4
                   13773: 
1.648     raeburn  13774: =item * &build_recipient_list()
1.618     raeburn  13775: 
1.1144    raeburn  13776: Build recipient lists for following types of e-mail:
1.766     raeburn  13777: (a) Error Reports, (b) Package Updates, (c) lonstatus warnings/errors
1.1144    raeburn  13778: (d) Help requests, (e) Course requests needing approval, (f) loncapa
                   13779: module change checking, student/employee ID conflict checks, as
                   13780: generated by lonerrorhandler.pm, CHECKRPMS, loncron,
                   13781: lonsupportreq.pm, loncoursequeueadmin.pm, searchcat.pl respectively.
1.618     raeburn  13782: 
                   13783: Inputs:
1.619     raeburn  13784: defmail (scalar - email address of default recipient), 
1.1144    raeburn  13785: mailing type (scalar: errormail, packagesmail, helpdeskmail,
                   13786: requestsmail, updatesmail, or idconflictsmail).
                   13787: 
1.619     raeburn  13788: defdom (domain for which to retrieve configuration settings),
1.1144    raeburn  13789: 
1.619     raeburn  13790: origmail (scalar - email address of recipient from loncapa.conf, 
                   13791: i.e., predates configuration by DC via domainprefs.pm 
1.618     raeburn  13792: 
1.655     raeburn  13793: Returns: comma separated list of addresses to which to send e-mail.
                   13794: 
                   13795: =back
1.618     raeburn  13796: 
                   13797: =cut
                   13798: 
                   13799: ############################################################
                   13800: ############################################################
                   13801: sub build_recipient_list {
1.619     raeburn  13802:     my ($defmail,$mailing,$defdom,$origmail) = @_;
1.618     raeburn  13803:     my @recipients;
                   13804:     my $otheremails;
                   13805:     my %domconfig =
                   13806:          &Apache::lonnet::get_dom('configuration',['contacts'],$defdom);
                   13807:     if (ref($domconfig{'contacts'}) eq 'HASH') {
1.766     raeburn  13808:         if (exists($domconfig{'contacts'}{$mailing})) {
                   13809:             if (ref($domconfig{'contacts'}{$mailing}) eq 'HASH') {
                   13810:                 my @contacts = ('adminemail','supportemail');
                   13811:                 foreach my $item (@contacts) {
                   13812:                     if ($domconfig{'contacts'}{$mailing}{$item}) {
                   13813:                         my $addr = $domconfig{'contacts'}{$item}; 
                   13814:                         if (!grep(/^\Q$addr\E$/,@recipients)) {
                   13815:                             push(@recipients,$addr);
                   13816:                         }
1.619     raeburn  13817:                     }
1.766     raeburn  13818:                     $otheremails = $domconfig{'contacts'}{$mailing}{'others'};
1.618     raeburn  13819:                 }
                   13820:             }
1.766     raeburn  13821:         } elsif ($origmail ne '') {
                   13822:             push(@recipients,$origmail);
1.618     raeburn  13823:         }
1.619     raeburn  13824:     } elsif ($origmail ne '') {
                   13825:         push(@recipients,$origmail);
1.618     raeburn  13826:     }
1.688     raeburn  13827:     if (defined($defmail)) {
                   13828:         if ($defmail ne '') {
                   13829:             push(@recipients,$defmail);
                   13830:         }
1.618     raeburn  13831:     }
                   13832:     if ($otheremails) {
1.619     raeburn  13833:         my @others;
                   13834:         if ($otheremails =~ /,/) {
                   13835:             @others = split(/,/,$otheremails);
1.618     raeburn  13836:         } else {
1.619     raeburn  13837:             push(@others,$otheremails);
                   13838:         }
                   13839:         foreach my $addr (@others) {
                   13840:             if (!grep(/^\Q$addr\E$/,@recipients)) {
                   13841:                 push(@recipients,$addr);
                   13842:             }
1.618     raeburn  13843:         }
                   13844:     }
1.619     raeburn  13845:     my $recipientlist = join(',',@recipients); 
1.618     raeburn  13846:     return $recipientlist;
                   13847: }
                   13848: 
1.127     matthew  13849: ############################################################
                   13850: ############################################################
1.154     albertel 13851: 
1.655     raeburn  13852: =pod
                   13853: 
                   13854: =head1 Course Catalog Routines
                   13855: 
                   13856: =over 4
                   13857: 
                   13858: =item * &gather_categories()
                   13859: 
                   13860: Converts category definitions - keys of categories hash stored in  
                   13861: coursecategories in configuration.db on the primary library server in a 
                   13862: domain - to an array.  Also generates javascript and idx hash used to 
                   13863: generate Domain Coordinator interface for editing Course Categories.
                   13864: 
                   13865: Inputs:
1.663     raeburn  13866: 
1.655     raeburn  13867: categories (reference to hash of category definitions).
1.663     raeburn  13868: 
1.655     raeburn  13869: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   13870:       categories and subcategories).
1.663     raeburn  13871: 
1.655     raeburn  13872: idx (reference to hash of counters used in Domain Coordinator interface for 
                   13873:       editing Course Categories).
1.663     raeburn  13874: 
1.655     raeburn  13875: jsarray (reference to array of categories used to create Javascript arrays for
                   13876:          Domain Coordinator interface for editing Course Categories).
                   13877: 
                   13878: Returns: nothing
                   13879: 
                   13880: Side effects: populates cats, idx and jsarray. 
                   13881: 
                   13882: =cut
                   13883: 
                   13884: sub gather_categories {
                   13885:     my ($categories,$cats,$idx,$jsarray) = @_;
                   13886:     my %counters;
                   13887:     my $num = 0;
                   13888:     foreach my $item (keys(%{$categories})) {
                   13889:         my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
                   13890:         if ($container eq '' && $depth == 0) {
                   13891:             $cats->[$depth][$categories->{$item}] = $cat;
                   13892:         } else {
                   13893:             $cats->[$depth]{$container}[$categories->{$item}] = $cat;
                   13894:         }
                   13895:         my ($escitem,$tail) = split(/:/,$item,2);
                   13896:         if ($counters{$tail} eq '') {
                   13897:             $counters{$tail} = $num;
                   13898:             $num ++;
                   13899:         }
                   13900:         if (ref($idx) eq 'HASH') {
                   13901:             $idx->{$item} = $counters{$tail};
                   13902:         }
                   13903:         if (ref($jsarray) eq 'ARRAY') {
                   13904:             push(@{$jsarray->[$counters{$tail}]},$item);
                   13905:         }
                   13906:     }
                   13907:     return;
                   13908: }
                   13909: 
                   13910: =pod
                   13911: 
                   13912: =item * &extract_categories()
                   13913: 
                   13914: Used to generate breadcrumb trails for course categories.
                   13915: 
                   13916: Inputs:
1.663     raeburn  13917: 
1.655     raeburn  13918: categories (reference to hash of category definitions).
1.663     raeburn  13919: 
1.655     raeburn  13920: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   13921:       categories and subcategories).
1.663     raeburn  13922: 
1.655     raeburn  13923: trails (reference to array of breacrumb trails for each category).
1.663     raeburn  13924: 
1.655     raeburn  13925: allitems (reference to hash - key is category key 
                   13926:          (format: escaped(name):escaped(parent category):depth in hierarchy).
1.663     raeburn  13927: 
1.655     raeburn  13928: idx (reference to hash of counters used in Domain Coordinator interface for
                   13929:       editing Course Categories).
1.663     raeburn  13930: 
1.655     raeburn  13931: jsarray (reference to array of categories used to create Javascript arrays for
                   13932:          Domain Coordinator interface for editing Course Categories).
                   13933: 
1.665     raeburn  13934: subcats (reference to hash of arrays containing all subcategories within each 
                   13935:          category, -recursive)
                   13936: 
1.655     raeburn  13937: Returns: nothing
                   13938: 
                   13939: Side effects: populates trails and allitems hash references.
                   13940: 
                   13941: =cut
                   13942: 
                   13943: sub extract_categories {
1.665     raeburn  13944:     my ($categories,$cats,$trails,$allitems,$idx,$jsarray,$subcats) = @_;
1.655     raeburn  13945:     if (ref($categories) eq 'HASH') {
                   13946:         &gather_categories($categories,$cats,$idx,$jsarray);
                   13947:         if (ref($cats->[0]) eq 'ARRAY') {
                   13948:             for (my $i=0; $i<@{$cats->[0]}; $i++) {
                   13949:                 my $name = $cats->[0][$i];
                   13950:                 my $item = &escape($name).'::0';
                   13951:                 my $trailstr;
                   13952:                 if ($name eq 'instcode') {
                   13953:                     $trailstr = &mt('Official courses (with institutional codes)');
1.919     raeburn  13954:                 } elsif ($name eq 'communities') {
                   13955:                     $trailstr = &mt('Communities');
1.655     raeburn  13956:                 } else {
                   13957:                     $trailstr = $name;
                   13958:                 }
                   13959:                 if ($allitems->{$item} eq '') {
                   13960:                     push(@{$trails},$trailstr);
                   13961:                     $allitems->{$item} = scalar(@{$trails})-1;
                   13962:                 }
                   13963:                 my @parents = ($name);
                   13964:                 if (ref($cats->[1]{$name}) eq 'ARRAY') {
                   13965:                     for (my $j=0; $j<@{$cats->[1]{$name}}; $j++) {
                   13966:                         my $category = $cats->[1]{$name}[$j];
1.665     raeburn  13967:                         if (ref($subcats) eq 'HASH') {
                   13968:                             push(@{$subcats->{$item}},&escape($category).':'.&escape($name).':1');
                   13969:                         }
                   13970:                         &recurse_categories($cats,2,$category,$trails,$allitems,\@parents,$subcats);
                   13971:                     }
                   13972:                 } else {
                   13973:                     if (ref($subcats) eq 'HASH') {
                   13974:                         $subcats->{$item} = [];
1.655     raeburn  13975:                     }
                   13976:                 }
                   13977:             }
                   13978:         }
                   13979:     }
                   13980:     return;
                   13981: }
                   13982: 
                   13983: =pod
                   13984: 
1.1162    raeburn  13985: =item * &recurse_categories()
1.655     raeburn  13986: 
                   13987: Recursively used to generate breadcrumb trails for course categories.
                   13988: 
                   13989: Inputs:
1.663     raeburn  13990: 
1.655     raeburn  13991: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   13992:       categories and subcategories).
1.663     raeburn  13993: 
1.655     raeburn  13994: depth (current depth in hierarchy of categories and sub-categories - 0 indexed).
1.663     raeburn  13995: 
                   13996: category (current course category, for which breadcrumb trail is being generated).
                   13997: 
                   13998: trails (reference to array of breadcrumb trails for each category).
                   13999: 
1.655     raeburn  14000: allitems (reference to hash - key is category key
                   14001:          (format: escaped(name):escaped(parent category):depth in hierarchy).
1.663     raeburn  14002: 
1.655     raeburn  14003: parents (array containing containers directories for current category, 
                   14004:          back to top level). 
                   14005: 
                   14006: Returns: nothing
                   14007: 
                   14008: Side effects: populates trails and allitems hash references
                   14009: 
                   14010: =cut
                   14011: 
                   14012: sub recurse_categories {
1.665     raeburn  14013:     my ($cats,$depth,$category,$trails,$allitems,$parents,$subcats) = @_;
1.655     raeburn  14014:     my $shallower = $depth - 1;
                   14015:     if (ref($cats->[$depth]{$category}) eq 'ARRAY') {
                   14016:         for (my $k=0; $k<@{$cats->[$depth]{$category}}; $k++) {
                   14017:             my $name = $cats->[$depth]{$category}[$k];
                   14018:             my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
                   14019:             my $trailstr = join(' -&gt; ',(@{$parents},$category));
                   14020:             if ($allitems->{$item} eq '') {
                   14021:                 push(@{$trails},$trailstr);
                   14022:                 $allitems->{$item} = scalar(@{$trails})-1;
                   14023:             }
                   14024:             my $deeper = $depth+1;
                   14025:             push(@{$parents},$category);
1.665     raeburn  14026:             if (ref($subcats) eq 'HASH') {
                   14027:                 my $subcat = &escape($name).':'.$category.':'.$depth;
                   14028:                 for (my $j=@{$parents}; $j>=0; $j--) {
                   14029:                     my $higher;
                   14030:                     if ($j > 0) {
                   14031:                         $higher = &escape($parents->[$j]).':'.
                   14032:                                   &escape($parents->[$j-1]).':'.$j;
                   14033:                     } else {
                   14034:                         $higher = &escape($parents->[$j]).'::'.$j;
                   14035:                     }
                   14036:                     push(@{$subcats->{$higher}},$subcat);
                   14037:                 }
                   14038:             }
                   14039:             &recurse_categories($cats,$deeper,$name,$trails,$allitems,$parents,
                   14040:                                 $subcats);
1.655     raeburn  14041:             pop(@{$parents});
                   14042:         }
                   14043:     } else {
                   14044:         my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
                   14045:         my $trailstr = join(' -&gt; ',(@{$parents},$category));
                   14046:         if ($allitems->{$item} eq '') {
                   14047:             push(@{$trails},$trailstr);
                   14048:             $allitems->{$item} = scalar(@{$trails})-1;
                   14049:         }
                   14050:     }
                   14051:     return;
                   14052: }
                   14053: 
1.663     raeburn  14054: =pod
                   14055: 
1.1162    raeburn  14056: =item * &assign_categories_table()
1.663     raeburn  14057: 
                   14058: Create a datatable for display of hierarchical categories in a domain,
                   14059: with checkboxes to allow a course to be categorized. 
                   14060: 
                   14061: Inputs:
                   14062: 
                   14063: cathash - reference to hash of categories defined for the domain (from
                   14064:           configuration.db)
                   14065: 
                   14066: currcat - scalar with an & separated list of categories assigned to a course. 
                   14067: 
1.919     raeburn  14068: type    - scalar contains course type (Course or Community).
                   14069: 
1.663     raeburn  14070: Returns: $output (markup to be displayed) 
                   14071: 
                   14072: =cut
                   14073: 
                   14074: sub assign_categories_table {
1.919     raeburn  14075:     my ($cathash,$currcat,$type) = @_;
1.663     raeburn  14076:     my $output;
                   14077:     if (ref($cathash) eq 'HASH') {
                   14078:         my (@cats,@trails,%allitems,%idx,@jsarray,@path,$maxdepth);
                   14079:         &extract_categories($cathash,\@cats,\@trails,\%allitems,\%idx,\@jsarray);
                   14080:         $maxdepth = scalar(@cats);
                   14081:         if (@cats > 0) {
                   14082:             my $itemcount = 0;
                   14083:             if (ref($cats[0]) eq 'ARRAY') {
                   14084:                 my @currcategories;
                   14085:                 if ($currcat ne '') {
                   14086:                     @currcategories = split('&',$currcat);
                   14087:                 }
1.919     raeburn  14088:                 my $table;
1.663     raeburn  14089:                 for (my $i=0; $i<@{$cats[0]}; $i++) {
                   14090:                     my $parent = $cats[0][$i];
1.919     raeburn  14091:                     next if ($parent eq 'instcode');
                   14092:                     if ($type eq 'Community') {
                   14093:                         next unless ($parent eq 'communities');
                   14094:                     } else {
                   14095:                         next if ($parent eq 'communities');
                   14096:                     }
1.663     raeburn  14097:                     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   14098:                     my $item = &escape($parent).'::0';
                   14099:                     my $checked = '';
                   14100:                     if (@currcategories > 0) {
                   14101:                         if (grep(/^\Q$item\E$/,@currcategories)) {
1.772     bisitz   14102:                             $checked = ' checked="checked"';
1.663     raeburn  14103:                         }
                   14104:                     }
1.919     raeburn  14105:                     my $parent_title = $parent;
                   14106:                     if ($parent eq 'communities') {
                   14107:                         $parent_title = &mt('Communities');
                   14108:                     }
                   14109:                     $table .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.
                   14110:                               '<input type="checkbox" name="usecategory" value="'.
                   14111:                               $item.'"'.$checked.' />'.$parent_title.'</span>'.
                   14112:                               '<input type="hidden" name="catname" value="'.$parent.'" /></td>';
1.663     raeburn  14113:                     my $depth = 1;
                   14114:                     push(@path,$parent);
1.919     raeburn  14115:                     $table .= &assign_category_rows($itemcount,\@cats,$depth,$parent,\@path,\@currcategories);
1.663     raeburn  14116:                     pop(@path);
1.919     raeburn  14117:                     $table .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
1.663     raeburn  14118:                     $itemcount ++;
                   14119:                 }
1.919     raeburn  14120:                 if ($itemcount) {
                   14121:                     $output = &Apache::loncommon::start_data_table().
                   14122:                               $table.
                   14123:                               &Apache::loncommon::end_data_table();
                   14124:                 }
1.663     raeburn  14125:             }
                   14126:         }
                   14127:     }
                   14128:     return $output;
                   14129: }
                   14130: 
                   14131: =pod
                   14132: 
1.1162    raeburn  14133: =item * &assign_category_rows()
1.663     raeburn  14134: 
                   14135: Create a datatable row for display of nested categories in a domain,
                   14136: with checkboxes to allow a course to be categorized,called recursively.
                   14137: 
                   14138: Inputs:
                   14139: 
                   14140: itemcount - track row number for alternating colors
                   14141: 
                   14142: cats - reference to array of arrays/hashes which encapsulates hierarchy of
                   14143:       categories and subcategories.
                   14144: 
                   14145: depth - current depth in hierarchy of categories and sub-categories - 0 indexed.
                   14146: 
                   14147: parent - parent of current category item
                   14148: 
                   14149: path - Array containing all categories back up through the hierarchy from the
                   14150:        current category to the top level.
                   14151: 
                   14152: currcategories - reference to array of current categories assigned to the course
                   14153: 
                   14154: Returns: $output (markup to be displayed).
                   14155: 
                   14156: =cut
                   14157: 
                   14158: sub assign_category_rows {
                   14159:     my ($itemcount,$cats,$depth,$parent,$path,$currcategories) = @_;
                   14160:     my ($text,$name,$item,$chgstr);
                   14161:     if (ref($cats) eq 'ARRAY') {
                   14162:         my $maxdepth = scalar(@{$cats});
                   14163:         if (ref($cats->[$depth]) eq 'HASH') {
                   14164:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
                   14165:                 my $numchildren = @{$cats->[$depth]{$parent}};
                   14166:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
1.1145    raeburn  14167:                 $text .= '<td><table class="LC_data_table">';
1.663     raeburn  14168:                 for (my $j=0; $j<$numchildren; $j++) {
                   14169:                     $name = $cats->[$depth]{$parent}[$j];
                   14170:                     $item = &escape($name).':'.&escape($parent).':'.$depth;
                   14171:                     my $deeper = $depth+1;
                   14172:                     my $checked = '';
                   14173:                     if (ref($currcategories) eq 'ARRAY') {
                   14174:                         if (@{$currcategories} > 0) {
                   14175:                             if (grep(/^\Q$item\E$/,@{$currcategories})) {
1.772     bisitz   14176:                                 $checked = ' checked="checked"';
1.663     raeburn  14177:                             }
                   14178:                         }
                   14179:                     }
1.664     raeburn  14180:                     $text .= '<tr><td><span class="LC_nobreak"><label>'.
                   14181:                              '<input type="checkbox" name="usecategory" value="'.
1.675     raeburn  14182:                              $item.'"'.$checked.' />'.$name.'</label></span>'.
                   14183:                              '<input type="hidden" name="catname" value="'.$name.'" />'.
                   14184:                              '</td><td>';
1.663     raeburn  14185:                     if (ref($path) eq 'ARRAY') {
                   14186:                         push(@{$path},$name);
                   14187:                         $text .= &assign_category_rows($itemcount,$cats,$deeper,$name,$path,$currcategories);
                   14188:                         pop(@{$path});
                   14189:                     }
                   14190:                     $text .= '</td></tr>';
                   14191:                 }
                   14192:                 $text .= '</table></td>';
                   14193:             }
                   14194:         }
                   14195:     }
                   14196:     return $text;
                   14197: }
                   14198: 
1.1181    raeburn  14199: =pod
                   14200: 
                   14201: =back
                   14202: 
                   14203: =cut
                   14204: 
1.655     raeburn  14205: ############################################################
                   14206: ############################################################
                   14207: 
                   14208: 
1.443     albertel 14209: sub commit_customrole {
1.664     raeburn  14210:     my ($udom,$uname,$url,$three,$four,$five,$start,$end,$context) = @_;
1.630     raeburn  14211:     my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.':'.$three.' in '.$url.
1.443     albertel 14212:                          ($start?', '.&mt('starting').' '.localtime($start):'').
                   14213:                          ($end?', ending '.localtime($end):'').': <b>'.
                   14214:               &Apache::lonnet::assigncustomrole(
1.664     raeburn  14215:                  $udom,$uname,$url,$three,$four,$five,$end,$start,undef,undef,$context).
1.443     albertel 14216:                  '</b><br />';
                   14217:     return $output;
                   14218: }
                   14219: 
                   14220: sub commit_standardrole {
1.1116    raeburn  14221:     my ($udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context,$credits) = @_;
1.541     raeburn  14222:     my ($output,$logmsg,$linefeed);
                   14223:     if ($context eq 'auto') {
                   14224:         $linefeed = "\n";
                   14225:     } else {
                   14226:         $linefeed = "<br />\n";
                   14227:     }  
1.443     albertel 14228:     if ($three eq 'st') {
1.541     raeburn  14229:         my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$three,$start,$end,
1.1116    raeburn  14230:                                          $one,$two,$sec,$context,$credits);
1.541     raeburn  14231:         if (($result =~ /^error/) || ($result eq 'not_in_class') || 
1.626     raeburn  14232:             ($result eq 'unknown_course') || ($result eq 'refused')) {
                   14233:             $output = $logmsg.' '.&mt('Error: ').$result."\n"; 
1.443     albertel 14234:         } else {
1.541     raeburn  14235:             $output = $logmsg.$linefeed.&mt('Assigning').' '.$three.' in '.$url.
1.443     albertel 14236:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  14237:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
                   14238:             if ($context eq 'auto') {
                   14239:                 $output .= $result.$linefeed.&mt('Add to classlist').': ok';
                   14240:             } else {
                   14241:                $output .= '<b>'.$result.'</b>'.$linefeed.
                   14242:                &mt('Add to classlist').': <b>ok</b>';
                   14243:             }
                   14244:             $output .= $linefeed;
1.443     albertel 14245:         }
                   14246:     } else {
                   14247:         $output = &mt('Assigning').' '.$three.' in '.$url.
                   14248:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  14249:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
1.652     raeburn  14250:         my $result = &Apache::lonnet::assignrole($udom,$uname,$url,$three,$end,$start,'','',$context);
1.541     raeburn  14251:         if ($context eq 'auto') {
                   14252:             $output .= $result.$linefeed;
                   14253:         } else {
                   14254:             $output .= '<b>'.$result.'</b>'.$linefeed;
                   14255:         }
1.443     albertel 14256:     }
                   14257:     return $output;
                   14258: }
                   14259: 
                   14260: sub commit_studentrole {
1.1116    raeburn  14261:     my ($logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context,
                   14262:         $credits) = @_;
1.626     raeburn  14263:     my ($result,$linefeed,$oldsecurl,$newsecurl);
1.541     raeburn  14264:     if ($context eq 'auto') {
                   14265:         $linefeed = "\n";
                   14266:     } else {
                   14267:         $linefeed = '<br />'."\n";
                   14268:     }
1.443     albertel 14269:     if (defined($one) && defined($two)) {
                   14270:         my $cid=$one.'_'.$two;
                   14271:         my $oldsec=&Apache::lonnet::getsection($udom,$uname,$cid);
                   14272:         my $secchange = 0;
                   14273:         my $expire_role_result;
                   14274:         my $modify_section_result;
1.628     raeburn  14275:         if ($oldsec ne '-1') { 
                   14276:             if ($oldsec ne $sec) {
1.443     albertel 14277:                 $secchange = 1;
1.628     raeburn  14278:                 my $now = time;
1.443     albertel 14279:                 my $uurl='/'.$cid;
                   14280:                 $uurl=~s/\_/\//g;
                   14281:                 if ($oldsec) {
                   14282:                     $uurl.='/'.$oldsec;
                   14283:                 }
1.626     raeburn  14284:                 $oldsecurl = $uurl;
1.628     raeburn  14285:                 $expire_role_result = 
1.652     raeburn  14286:                     &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',$now,'','',$context);
1.628     raeburn  14287:                 if ($env{'request.course.sec'} ne '') { 
                   14288:                     if ($expire_role_result eq 'refused') {
                   14289:                         my @roles = ('st');
                   14290:                         my @statuses = ('previous');
                   14291:                         my @roledoms = ($one);
                   14292:                         my $withsec = 1;
                   14293:                         my %roleshash = 
                   14294:                             &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   14295:                                               \@statuses,\@roles,\@roledoms,$withsec);
                   14296:                         if (defined ($roleshash{$two.':'.$one.':st:'.$oldsec})) {
                   14297:                             my ($oldstart,$oldend) = 
                   14298:                                 split(':',$roleshash{$two.':'.$one.':st:'.$oldsec});
                   14299:                             if ($oldend > 0 && $oldend <= $now) {
                   14300:                                 $expire_role_result = 'ok';
                   14301:                             }
                   14302:                         }
                   14303:                     }
                   14304:                 }
1.443     albertel 14305:                 $result = $expire_role_result;
                   14306:             }
                   14307:         }
                   14308:         if (($expire_role_result eq 'ok') || ($secchange == 0)) {
1.1116    raeburn  14309:             $modify_section_result = 
                   14310:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,
                   14311:                                                            undef,undef,undef,$sec,
                   14312:                                                            $end,$start,'','',$cid,
                   14313:                                                            '',$context,$credits);
1.443     albertel 14314:             if ($modify_section_result =~ /^ok/) {
                   14315:                 if ($secchange == 1) {
1.628     raeburn  14316:                     if ($sec eq '') {
                   14317:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to student role without a section.',$uname,$oldsec).$linefeed;
                   14318:                     } else {
                   14319:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to new section: [_3].',$uname,$oldsec,$sec).$linefeed;
                   14320:                     }
1.443     albertel 14321:                 } elsif ($oldsec eq '-1') {
1.628     raeburn  14322:                     if ($sec eq '') {
                   14323:                         $$logmsg .= &mt('New student role without a section for [_1] in course [_2].',$uname,$cid).$linefeed;
                   14324:                     } else {
                   14325:                         $$logmsg .= &mt('New student role for [_1] in section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
                   14326:                     }
1.443     albertel 14327:                 } else {
1.628     raeburn  14328:                     if ($sec eq '') {
                   14329:                         $$logmsg .= &mt('Student [_1] assigned to course [_2] without a section.',$uname,$cid).$linefeed;
                   14330:                     } else {
                   14331:                         $$logmsg .= &mt('Student [_1] assigned to section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
                   14332:                     }
1.443     albertel 14333:                 }
                   14334:             } else {
1.1115    raeburn  14335:                 if ($secchange) { 
1.628     raeburn  14336:                     $$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;
                   14337:                 } else {
                   14338:                     $$logmsg .= &mt('Error when attempting to modify role for [_1] for section: "[_2]" in course [_3] -error:',$uname,$sec,$cid).' '.$modify_section_result.$linefeed;
                   14339:                 }
1.443     albertel 14340:             }
                   14341:             $result = $modify_section_result;
                   14342:         } elsif ($secchange == 1) {
1.628     raeburn  14343:             if ($oldsec eq '') {
1.1103    raeburn  14344:                 $$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  14345:             } else {
                   14346:                 $$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;
                   14347:             }
1.626     raeburn  14348:             if ($expire_role_result eq 'refused') {
                   14349:                 my $newsecurl = '/'.$cid;
                   14350:                 $newsecurl =~ s/\_/\//g;
                   14351:                 if ($sec ne '') {
                   14352:                     $newsecurl.='/'.$sec;
                   14353:                 }
                   14354:                 if (&Apache::lonnet::allowed('cst',$newsecurl) && !(&Apache::lonnet::allowed('cst',$oldsecurl))) {
                   14355:                     if ($sec eq '') {
                   14356:                         $$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;
                   14357:                     } else {
                   14358:                         $$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;
                   14359:                     }
                   14360:                 }
                   14361:             }
1.443     albertel 14362:         }
                   14363:     } else {
1.626     raeburn  14364:         $$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 14365:         $result = "error: incomplete course id\n";
                   14366:     }
                   14367:     return $result;
                   14368: }
                   14369: 
1.1108    raeburn  14370: sub show_role_extent {
                   14371:     my ($scope,$context,$role) = @_;
                   14372:     $scope =~ s{^/}{};
                   14373:     my @courseroles = &Apache::lonuserutils::roles_by_context('course',1);
                   14374:     push(@courseroles,'co');
                   14375:     my @authorroles = &Apache::lonuserutils::roles_by_context('author');
                   14376:     if (($context eq 'course') || (grep(/^\Q$role\E/,@courseroles))) {
                   14377:         $scope =~ s{/}{_};
                   14378:         return '<span class="LC_cusr_emph">'.$env{'course.'.$scope.'.description'}.'</span>';
                   14379:     } elsif (($context eq 'author') || (grep(/^\Q$role\E/,@authorroles))) {
                   14380:         my ($audom,$auname) = split(/\//,$scope);
                   14381:         return &mt('[_1] Author Space','<span class="LC_cusr_emph">'.
                   14382:                    &Apache::loncommon::plainname($auname,$audom).'</span>');
                   14383:     } else {
                   14384:         $scope =~ s{/$}{};
                   14385:         return &mt('Domain: [_1]','<span class="LC_cusr_emph">'.
                   14386:                    &Apache::lonnet::domain($scope,'description').'</span>');
                   14387:     }
                   14388: }
                   14389: 
1.443     albertel 14390: ############################################################
                   14391: ############################################################
                   14392: 
1.566     albertel 14393: sub check_clone {
1.578     raeburn  14394:     my ($args,$linefeed) = @_;
1.566     albertel 14395:     my $cloneid='/'.$args->{'clonedomain'}.'/'.$args->{'clonecourse'};
                   14396:     my ($clonecrsudom,$clonecrsunum)= &LONCAPA::split_courseid($cloneid);
                   14397:     my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
                   14398:     my $clonemsg;
                   14399:     my $can_clone = 0;
1.944     raeburn  14400:     my $lctype = lc($args->{'crstype'});
1.908     raeburn  14401:     if ($lctype ne 'community') {
                   14402:         $lctype = 'course';
                   14403:     }
1.566     albertel 14404:     if ($clonehome eq 'no_host') {
1.944     raeburn  14405:         if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  14406:             $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'});
                   14407:         } else {
                   14408:             $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'});
                   14409:         }     
1.566     albertel 14410:     } else {
                   14411: 	my %clonedesc = &Apache::lonnet::coursedescription($cloneid,{'one_time' => 1});
1.944     raeburn  14412:         if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  14413:             if ($clonedesc{'type'} ne 'Community') {
                   14414:                  $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'});
                   14415:                 return ($can_clone, $clonemsg, $cloneid, $clonehome);
                   14416:             }
                   14417:         }
1.882     raeburn  14418: 	if (($env{'request.role.domain'} eq $args->{'clonedomain'}) && 
                   14419:             (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'}))) {
1.566     albertel 14420: 	    $can_clone = 1;
                   14421: 	} else {
1.1221    raeburn  14422: 	    my %clonehash = &Apache::lonnet::get('environment',['cloners','internal.coursecode'],
1.566     albertel 14423: 						 $args->{'clonedomain'},$args->{'clonecourse'});
1.1221    raeburn  14424:             if ($clonehash{'cloners'} eq '') {
                   14425:                 my %domdefs = &Apache::lonnet::get_domain_defaults($args->{'course_domain'});
                   14426:                 if ($domdefs{'canclone'}) {
                   14427:                     unless ($domdefs{'canclone'} eq 'none') {
                   14428:                         if ($domdefs{'canclone'} eq 'domain') {
                   14429:                             if ($args->{'ccdomain'} eq $args->{'clonedomain'}) {
                   14430:                                 $can_clone = 1;
                   14431:                             }
                   14432:                         } elsif (($clonehash{'internal.coursecode'}) && ($args->{'crscode'}) && 
                   14433:                                  ($args->{'clonedomain'} eq  $args->{'course_domain'})) {
                   14434:                             if (&Apache::lonnet::default_instcode_cloning($args->{'clonedomain'},$domdefs{'canclone'},
                   14435:                                                                           $clonehash{'internal.coursecode'},$args->{'crscode'})) {
                   14436:                                 $can_clone = 1;
                   14437:                             }
                   14438:                         }
                   14439:                     }
                   14440:                 }
1.578     raeburn  14441:             } else {
1.1221    raeburn  14442: 	        my @cloners = split(/,/,$clonehash{'cloners'});
                   14443:                 if (grep(/^\*$/,@cloners)) {
1.942     raeburn  14444:                     $can_clone = 1;
1.1221    raeburn  14445:                 } elsif (grep(/^\*\:\Q$args->{'ccdomain'}\E$/,@cloners)) {
1.942     raeburn  14446:                     $can_clone = 1;
1.1221    raeburn  14447:                 }
                   14448:                 unless ($can_clone) {
                   14449:                     if (($clonehash{'internal.coursecode'}) && ($args->{'crscode'}) && ($args->{'clonedomain'} eq  $args->{'course_domain'})) {
                   14450:                         my (%gotdomdefaults,%gotcodedefaults);
                   14451:                         foreach my $cloner (@cloners) {
                   14452:                             if (($cloner ne '*') && ($cloner !~ /^\*\:$match_domain$/) &&
                   14453:                                 ($cloner !~ /^$match_username\:$match_domain$/) && ($cloner ne '')) {
                   14454:                                 my (%codedefaults,@code_order);
                   14455:                                 if (ref($gotcodedefaults{$args->{'clonedomain'}}) eq 'HASH') {
                   14456:                                     if (ref($gotcodedefaults{$args->{'clonedomain'}}{'defaults'}) eq 'HASH') {
                   14457:                                         %codedefaults = %{$gotcodedefaults{$args->{'clonedomain'}}{'defaults'}};
                   14458:                                     }
                   14459:                                     if (ref($gotcodedefaults{$args->{'clonedomain'}}{'order'}) eq 'ARRAY') {
                   14460:                                         @code_order = @{$gotcodedefaults{$args->{'clonedomain'}}{'order'}};
                   14461:                                     }
                   14462:                                 } else {
                   14463:                                     &Apache::lonnet::auto_instcode_defaults($args->{'clonedomain'},
                   14464:                                                                             \%codedefaults,
                   14465:                                                                             \@code_order);
                   14466:                                     $gotcodedefaults{$args->{'clonedomain'}}{'defaults'} = \%codedefaults;
                   14467:                                     $gotcodedefaults{$args->{'clonedomain'}}{'order'} = \@code_order;
                   14468:                                 }
                   14469:                                 if (@code_order > 0) {
                   14470:                                     if (&Apache::lonnet::check_instcode_cloning(\%codedefaults,\@code_order,
                   14471:                                                                                 $cloner,$clonehash{'internal.coursecode'},
                   14472:                                                                                 $args->{'crscode'})) {
                   14473:                                         $can_clone = 1;
                   14474:                                         last;
                   14475:                                     }
                   14476:                                 }
                   14477:                             }
                   14478:                         }
                   14479:                     }
                   14480:                     unless ($can_clone) {
                   14481:                         my $ccrole = 'cc';
                   14482:                         if ($args->{'crstype'} eq 'Community') {
                   14483:                             $ccrole = 'co';
                   14484:                         }
                   14485: 	                my %roleshash =
                   14486: 		            &Apache::lonnet::get_my_roles($args->{'ccuname'},
                   14487: 					                  $args->{'ccdomain'},
                   14488:                                                           'userroles',['active'],[$ccrole],
                   14489: 					                  [$args->{'clonedomain'}]);
                   14490: 	                if (($roleshash{$args->{'clonecourse'}.':'.$args->{'clonedomain'}.':'.$ccrole}) || 
                   14491:                             (grep(/^\Q$args->{'ccuname'}\E:\Q$args->{'ccdomain'}\E$/,@cloners))) {
                   14492:                             $can_clone = 1;
                   14493:                         } elsif (&Apache::lonnet::is_course_owner($args->{'clonedomain'},$args->{'clonecourse'},
                   14494:                                                                   $args->{'ccuname'},$args->{'ccdomain'})) {
                   14495:                             $can_clone = 1;
                   14496:                         }
                   14497:                     }
                   14498:                 }
                   14499:             }
                   14500:             unless ($can_clone) {
                   14501:                 if ($args->{'crstype'} eq 'Community') {
                   14502:                     $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'});
1.942     raeburn  14503:                 } else {
1.1221    raeburn  14504:                     $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'});
                   14505:                 }
1.566     albertel 14506: 	    }
1.578     raeburn  14507:         }
1.566     albertel 14508:     }
                   14509:     return ($can_clone, $clonemsg, $cloneid, $clonehome);
                   14510: }
                   14511: 
1.444     albertel 14512: sub construct_course {
1.1166    raeburn  14513:     my ($args,$logmsg,$courseid,$crsudom,$crsunum,$udom,$uname,$context,$cnum,$category,$coderef) = @_;
1.444     albertel 14514:     my $outcome;
1.541     raeburn  14515:     my $linefeed =  '<br />'."\n";
                   14516:     if ($context eq 'auto') {
                   14517:         $linefeed = "\n";
                   14518:     }
1.566     albertel 14519: 
                   14520: #
                   14521: # Are we cloning?
                   14522: #
                   14523:     my ($can_clone, $clonemsg, $cloneid, $clonehome);
                   14524:     if (($args->{'clonecourse'}) && ($args->{'clonedomain'})) {
1.578     raeburn  14525: 	($can_clone, $clonemsg, $cloneid, $clonehome) = &check_clone($args,$linefeed);
1.566     albertel 14526: 	if ($context ne 'auto') {
1.578     raeburn  14527:             if ($clonemsg ne '') {
                   14528: 	        $clonemsg = '<span class="LC_error">'.$clonemsg.'</span>';
                   14529:             }
1.566     albertel 14530: 	}
                   14531: 	$outcome .= $clonemsg.$linefeed;
                   14532: 
                   14533:         if (!$can_clone) {
                   14534: 	    return (0,$outcome);
                   14535: 	}
                   14536:     }
                   14537: 
1.444     albertel 14538: #
                   14539: # Open course
                   14540: #
                   14541:     my $crstype = lc($args->{'crstype'});
                   14542:     my %cenv=();
                   14543:     $$courseid=&Apache::lonnet::createcourse($args->{'course_domain'},
                   14544:                                              $args->{'cdescr'},
                   14545:                                              $args->{'curl'},
                   14546:                                              $args->{'course_home'},
                   14547:                                              $args->{'nonstandard'},
                   14548:                                              $args->{'crscode'},
                   14549:                                              $args->{'ccuname'}.':'.
                   14550:                                              $args->{'ccdomain'},
1.882     raeburn  14551:                                              $args->{'crstype'},
1.885     raeburn  14552:                                              $cnum,$context,$category);
1.444     albertel 14553: 
                   14554:     # Note: The testing routines depend on this being output; see 
                   14555:     # Utils::Course. This needs to at least be output as a comment
                   14556:     # if anyone ever decides to not show this, and Utils::Course::new
                   14557:     # will need to be suitably modified.
1.541     raeburn  14558:     $outcome .= &mt('New LON-CAPA [_1] ID: [_2]',$crstype,$$courseid).$linefeed;
1.943     raeburn  14559:     if ($$courseid =~ /^error:/) {
                   14560:         return (0,$outcome);
                   14561:     }
                   14562: 
1.444     albertel 14563: #
                   14564: # Check if created correctly
                   14565: #
1.479     albertel 14566:     ($$crsudom,$$crsunum)= &LONCAPA::split_courseid($$courseid);
1.444     albertel 14567:     my $crsuhome=&Apache::lonnet::homeserver($$crsunum,$$crsudom);
1.943     raeburn  14568:     if ($crsuhome eq 'no_host') {
                   14569:         $outcome .= &mt('Course creation failed, unrecognized course home server.').$linefeed;
                   14570:         return (0,$outcome);
                   14571:     }
1.541     raeburn  14572:     $outcome .= &mt('Created on').': '.$crsuhome.$linefeed;
1.566     albertel 14573: 
1.444     albertel 14574: #
1.566     albertel 14575: # Do the cloning
                   14576: #   
                   14577:     if ($can_clone && $cloneid) {
                   14578: 	$clonemsg = &mt('Cloning [_1] from [_2]',$crstype,$clonehome);
                   14579: 	if ($context ne 'auto') {
                   14580: 	    $clonemsg = '<span class="LC_success">'.$clonemsg.'</span>';
                   14581: 	}
                   14582: 	$outcome .= $clonemsg.$linefeed;
                   14583: 	my %oldcenv=&Apache::lonnet::dump('environment',$$crsudom,$$crsunum);
1.444     albertel 14584: # Copy all files
1.637     www      14585: 	&Apache::lonclonecourse::copycoursefiles($cloneid,$$courseid,$args->{'datemode'},$args->{'dateshift'});
1.444     albertel 14586: # Restore URL
1.566     albertel 14587: 	$cenv{'url'}=$oldcenv{'url'};
1.444     albertel 14588: # Restore title
1.566     albertel 14589: 	$cenv{'description'}=$oldcenv{'description'};
1.955     raeburn  14590: # Restore creation date, creator and creation context.
                   14591:         $cenv{'internal.created'}=$oldcenv{'internal.created'};
                   14592:         $cenv{'internal.creator'}=$oldcenv{'internal.creator'};
                   14593:         $cenv{'internal.creationcontext'}=$oldcenv{'internal.creationcontext'};
1.444     albertel 14594: # Mark as cloned
1.566     albertel 14595: 	$cenv{'clonedfrom'}=$cloneid;
1.638     www      14596: # Need to clone grading mode
                   14597:         my %newenv=&Apache::lonnet::get('environment',['grading'],$$crsudom,$$crsunum);
                   14598:         $cenv{'grading'}=$newenv{'grading'};
                   14599: # Do not clone these environment entries
                   14600:         &Apache::lonnet::del('environment',
                   14601:                   ['default_enrollment_start_date',
                   14602:                    'default_enrollment_end_date',
                   14603:                    'question.email',
                   14604:                    'policy.email',
                   14605:                    'comment.email',
                   14606:                    'pch.users.denied',
1.725     raeburn  14607:                    'plc.users.denied',
                   14608:                    'hidefromcat',
1.1121    raeburn  14609:                    'checkforpriv',
1.1166    raeburn  14610:                    'categories',
                   14611:                    'internal.uniquecode'],
1.638     www      14612:                    $$crsudom,$$crsunum);
1.1170    raeburn  14613:         if ($args->{'textbook'}) {
                   14614:             $cenv{'internal.textbook'} = $args->{'textbook'};
                   14615:         }
1.444     albertel 14616:     }
1.566     albertel 14617: 
1.444     albertel 14618: #
                   14619: # Set environment (will override cloned, if existing)
                   14620: #
                   14621:     my @sections = ();
                   14622:     my @xlists = ();
                   14623:     if ($args->{'crstype'}) {
                   14624:         $cenv{'type'}=$args->{'crstype'};
                   14625:     }
                   14626:     if ($args->{'crsid'}) {
                   14627:         $cenv{'courseid'}=$args->{'crsid'};
                   14628:     }
                   14629:     if ($args->{'crscode'}) {
                   14630:         $cenv{'internal.coursecode'}=$args->{'crscode'};
                   14631:     }
                   14632:     if ($args->{'crsquota'} ne '') {
                   14633:         $cenv{'internal.coursequota'}=$args->{'crsquota'};
                   14634:     } else {
                   14635:         $cenv{'internal.coursequota'}=$args->{'crsquota'} = 20;
                   14636:     }
                   14637:     if ($args->{'ccuname'}) {
                   14638:         $cenv{'internal.courseowner'} = $args->{'ccuname'}.
                   14639:                                         ':'.$args->{'ccdomain'};
                   14640:     } else {
                   14641:         $cenv{'internal.courseowner'} = $args->{'curruser'};
                   14642:     }
1.1116    raeburn  14643:     if ($args->{'defaultcredits'}) {
                   14644:         $cenv{'internal.defaultcredits'} = $args->{'defaultcredits'};
                   14645:     }
1.444     albertel 14646:     my @badclasses = (); # Used to accumulate sections/crosslistings that did not pass classlist access check for course owner.
                   14647:     if ($args->{'crssections'}) {
                   14648:         $cenv{'internal.sectionnums'} = '';
                   14649:         if ($args->{'crssections'} =~ m/,/) {
                   14650:             @sections = split/,/,$args->{'crssections'};
                   14651:         } else {
                   14652:             $sections[0] = $args->{'crssections'};
                   14653:         }
                   14654:         if (@sections > 0) {
                   14655:             foreach my $item (@sections) {
                   14656:                 my ($sec,$gp) = split/:/,$item;
                   14657:                 my $class = $args->{'crscode'}.$sec;
                   14658:                 my $addcheck = &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$class,$cenv{'internal.courseowner'});
                   14659:                 $cenv{'internal.sectionnums'} .= $item.',';
                   14660:                 unless ($addcheck eq 'ok') {
                   14661:                     push @badclasses, $class;
                   14662:                 }
                   14663:             }
                   14664:             $cenv{'internal.sectionnums'} =~ s/,$//;
                   14665:         }
                   14666:     }
                   14667: # do not hide course coordinator from staff listing, 
                   14668: # even if privileged
                   14669:     $cenv{'nothideprivileged'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
1.1121    raeburn  14670: # add course coordinator's domain to domains to check for privileged users
                   14671: # if different to course domain
                   14672:     if ($$crsudom ne $args->{'ccdomain'}) {
                   14673:         $cenv{'checkforpriv'} = $args->{'ccdomain'};
                   14674:     }
1.444     albertel 14675: # add crosslistings
                   14676:     if ($args->{'crsxlist'}) {
                   14677:         $cenv{'internal.crosslistings'}='';
                   14678:         if ($args->{'crsxlist'} =~ m/,/) {
                   14679:             @xlists = split/,/,$args->{'crsxlist'};
                   14680:         } else {
                   14681:             $xlists[0] = $args->{'crsxlist'};
                   14682:         }
                   14683:         if (@xlists > 0) {
                   14684:             foreach my $item (@xlists) {
                   14685:                 my ($xl,$gp) = split/:/,$item;
                   14686:                 my $addcheck =  &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$xl,$cenv{'internal.courseowner'});
                   14687:                 $cenv{'internal.crosslistings'} .= $item.',';
                   14688:                 unless ($addcheck eq 'ok') {
                   14689:                     push @badclasses, $xl;
                   14690:                 }
                   14691:             }
                   14692:             $cenv{'internal.crosslistings'} =~ s/,$//;
                   14693:         }
                   14694:     }
                   14695:     if ($args->{'autoadds'}) {
                   14696:         $cenv{'internal.autoadds'}=$args->{'autoadds'};
                   14697:     }
                   14698:     if ($args->{'autodrops'}) {
                   14699:         $cenv{'internal.autodrops'}=$args->{'autodrops'};
                   14700:     }
                   14701: # check for notification of enrollment changes
                   14702:     my @notified = ();
                   14703:     if ($args->{'notify_owner'}) {
                   14704:         if ($args->{'ccuname'} ne '') {
                   14705:             push(@notified,$args->{'ccuname'}.':'.$args->{'ccdomain'});
                   14706:         }
                   14707:     }
                   14708:     if ($args->{'notify_dc'}) {
                   14709:         if ($uname ne '') { 
1.630     raeburn  14710:             push(@notified,$uname.':'.$udom);
1.444     albertel 14711:         }
                   14712:     }
                   14713:     if (@notified > 0) {
                   14714:         my $notifylist;
                   14715:         if (@notified > 1) {
                   14716:             $notifylist = join(',',@notified);
                   14717:         } else {
                   14718:             $notifylist = $notified[0];
                   14719:         }
                   14720:         $cenv{'internal.notifylist'} = $notifylist;
                   14721:     }
                   14722:     if (@badclasses > 0) {
                   14723:         my %lt=&Apache::lonlocal::texthash(
                   14724:                 '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',
                   14725:                 'dnhr' => 'does not have rights to access enrollment in these classes',
                   14726:                 'adby' => 'as determined by the policies of your institution on access to official classlists'
                   14727:         );
1.541     raeburn  14728:         my $badclass_msg = $cenv{'internal.courseowner'}.') - '.$lt{'dnhr'}.
                   14729:                            ' ('.$lt{'adby'}.')';
                   14730:         if ($context eq 'auto') {
                   14731:             $outcome .= $badclass_msg.$linefeed;
1.566     albertel 14732:             $outcome .= '<div class="LC_warning">'.$badclass_msg.$linefeed.'<ul>'."\n";
1.541     raeburn  14733:             foreach my $item (@badclasses) {
                   14734:                 if ($context eq 'auto') {
                   14735:                     $outcome .= " - $item\n";
                   14736:                 } else {
                   14737:                     $outcome .= "<li>$item</li>\n";
                   14738:                 }
                   14739:             }
                   14740:             if ($context eq 'auto') {
                   14741:                 $outcome .= $linefeed;
                   14742:             } else {
1.566     albertel 14743:                 $outcome .= "</ul><br /><br /></div>\n";
1.541     raeburn  14744:             }
                   14745:         } 
1.444     albertel 14746:     }
                   14747:     if ($args->{'no_end_date'}) {
                   14748:         $args->{'endaccess'} = 0;
                   14749:     }
                   14750:     $cenv{'internal.autostart'}=$args->{'enrollstart'};
                   14751:     $cenv{'internal.autoend'}=$args->{'enrollend'};
                   14752:     $cenv{'default_enrollment_start_date'}=$args->{'startaccess'};
                   14753:     $cenv{'default_enrollment_end_date'}=$args->{'endaccess'};
                   14754:     if ($args->{'showphotos'}) {
                   14755:       $cenv{'internal.showphotos'}=$args->{'showphotos'};
                   14756:     }
                   14757:     $cenv{'internal.authtype'} = $args->{'authtype'};
                   14758:     $cenv{'internal.autharg'} = $args->{'autharg'}; 
                   14759:     if ( ($cenv{'internal.authtype'} =~ /^krb/) && ($cenv{'internal.autoadds'} == 1)) {
                   14760:         if (! defined($cenv{'internal.autharg'}) || $cenv{'internal.autharg'}  eq '') {
1.541     raeburn  14761:             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'); 
                   14762:             if ($context eq 'auto') {
                   14763:                 $outcome .= $krb_msg;
                   14764:             } else {
1.566     albertel 14765:                 $outcome .= '<span class="LC_error">'.$krb_msg.'</span>';
1.541     raeburn  14766:             }
                   14767:             $outcome .= $linefeed;
1.444     albertel 14768:         }
                   14769:     }
                   14770:     if (($args->{'ccdomain'}) && ($args->{'ccuname'})) {
                   14771:        if ($args->{'setpolicy'}) {
                   14772:            $cenv{'policy.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   14773:        }
                   14774:        if ($args->{'setcontent'}) {
                   14775:            $cenv{'question.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   14776:        }
                   14777:     }
                   14778:     if ($args->{'reshome'}) {
                   14779: 	$cenv{'reshome'}=$args->{'reshome'}.'/';
                   14780: 	$cenv{'reshome'}=~s/\/+$/\//;
                   14781:     }
                   14782: #
                   14783: # course has keyed access
                   14784: #
                   14785:     if ($args->{'setkeys'}) {
                   14786:        $cenv{'keyaccess'}='yes';
                   14787:     }
                   14788: # if specified, key authority is not course, but user
                   14789: # only active if keyaccess is yes
                   14790:     if ($args->{'keyauth'}) {
1.487     albertel 14791: 	my ($user,$domain) = split(':',$args->{'keyauth'});
                   14792: 	$user = &LONCAPA::clean_username($user);
                   14793: 	$domain = &LONCAPA::clean_username($domain);
1.488     foxr     14794: 	if ($user ne '' && $domain ne '') {
1.487     albertel 14795: 	    $cenv{'keyauth'}=$user.':'.$domain;
1.444     albertel 14796: 	}
                   14797:     }
                   14798: 
1.1166    raeburn  14799: #
1.1167    raeburn  14800: #  generate and store uniquecode (available to course requester), if course should have one.
1.1166    raeburn  14801: #
                   14802:     if ($args->{'uniquecode'}) {
                   14803:         my ($code,$error) = &make_unique_code($$crsudom,$$crsunum);
                   14804:         if ($code) {
                   14805:             $cenv{'internal.uniquecode'} = $code;
1.1167    raeburn  14806:             my %crsinfo =
                   14807:                 &Apache::lonnet::courseiddump($$crsudom,'.',1,'.','.',$$crsunum,undef,undef,'.');
                   14808:             if (ref($crsinfo{$$crsudom.'_'.$$crsunum}) eq 'HASH') {
                   14809:                 $crsinfo{$$crsudom.'_'.$$crsunum}{'uniquecode'} = $code;
                   14810:                 my $putres = &Apache::lonnet::courseidput($$crsudom,\%crsinfo,$crsuhome,'notime');
                   14811:             } 
1.1166    raeburn  14812:             if (ref($coderef)) {
                   14813:                 $$coderef = $code;
                   14814:             }
                   14815:         }
                   14816:     }
                   14817: 
1.444     albertel 14818:     if ($args->{'disresdis'}) {
                   14819:         $cenv{'pch.roles.denied'}='st';
                   14820:     }
                   14821:     if ($args->{'disablechat'}) {
                   14822:         $cenv{'plc.roles.denied'}='st';
                   14823:     }
                   14824: 
                   14825:     # Record we've not yet viewed the Course Initialization Helper for this 
                   14826:     # course
                   14827:     $cenv{'course.helper.not.run'} = 1;
                   14828:     #
                   14829:     # Use new Randomseed
                   14830:     #
                   14831:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
                   14832:     $cenv{'receiptalg'}=&Apache::lonnet::latest_receipt_algorithm_id();;
                   14833:     #
                   14834:     # The encryption code and receipt prefix for this course
                   14835:     #
                   14836:     $cenv{'internal.encseed'}=$Apache::lonnet::perlvar{'lonReceipt'}.$$.time.int(rand(9999));
                   14837:     $cenv{'internal.encpref'}=100+int(9*rand(99));
                   14838:     #
                   14839:     # By default, use standard grading
                   14840:     if (!defined($cenv{'grading'})) { $cenv{'grading'} = 'standard'; }
                   14841: 
1.541     raeburn  14842:     $outcome .= $linefeed.&mt('Setting environment').': '.                 
                   14843:           &Apache::lonnet::put('environment',\%cenv,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 14844: #
                   14845: # Open all assignments
                   14846: #
                   14847:     if ($args->{'openall'}) {
                   14848:        my $storeunder=$$crsudom.'_'.$$crsunum.'.0.opendate';
                   14849:        my %storecontent = ($storeunder         => time,
                   14850:                            $storeunder.'.type' => 'date_start');
                   14851:        
                   14852:        $outcome .= &mt('Opening all assignments').': '.&Apache::lonnet::cput
1.541     raeburn  14853:                  ('resourcedata',\%storecontent,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 14854:    }
                   14855: #
                   14856: # Set first page
                   14857: #
                   14858:     unless (($args->{'nonstandard'}) || ($args->{'firstres'} eq 'blank')
                   14859: 	    || ($cloneid)) {
1.445     albertel 14860: 	use LONCAPA::map;
1.444     albertel 14861: 	$outcome .= &mt('Setting first resource').': ';
1.445     albertel 14862: 
                   14863: 	my $map = '/uploaded/'.$$crsudom.'/'.$$crsunum.'/default.sequence';
                   14864:         my ($errtext,$fatal)=&LONCAPA::map::mapread($map);
                   14865: 
1.444     albertel 14866:         $outcome .= ($fatal?$errtext:'read ok').' - ';
                   14867:         my $title; my $url;
                   14868:         if ($args->{'firstres'} eq 'syl') {
1.690     bisitz   14869: 	    $title=&mt('Syllabus');
1.444     albertel 14870:             $url='/public/'.$$crsudom.'/'.$$crsunum.'/syllabus';
                   14871:         } else {
1.963     raeburn  14872:             $title=&mt('Table of Contents');
1.444     albertel 14873:             $url='/adm/navmaps';
                   14874:         }
1.445     albertel 14875: 
                   14876:         $LONCAPA::map::resources[1]=$title.':'.$url.':false:start:res';
                   14877: 	(my $outtext,$errtext) = &LONCAPA::map::storemap($map,1);
                   14878: 
                   14879: 	if ($errtext) { $fatal=2; }
1.541     raeburn  14880:         $outcome .= ($fatal?$errtext:'write ok').$linefeed;
1.444     albertel 14881:     }
1.566     albertel 14882: 
                   14883:     return (1,$outcome);
1.444     albertel 14884: }
                   14885: 
1.1166    raeburn  14886: sub make_unique_code {
                   14887:     my ($cdom,$cnum) = @_;
                   14888:     # get lock on uniquecodes db
                   14889:     my $lockhash = {
                   14890:                       $cnum."\0".'uniquecodes' => $env{'user.name'}.
                   14891:                                                   ':'.$env{'user.domain'},
                   14892:                    };
                   14893:     my $tries = 0;
                   14894:     my $gotlock = &Apache::lonnet::newput_dom('uniquecodes',$lockhash,$cdom);
                   14895:     my ($code,$error);
                   14896:   
                   14897:     while (($gotlock ne 'ok') && ($tries<3)) {
                   14898:         $tries ++;
                   14899:         sleep 1;
                   14900:         $gotlock = &Apache::lonnet::newput_dom('uniquecodes',$lockhash,$cdom);
                   14901:     }
                   14902:     if ($gotlock eq 'ok') {
                   14903:         my %currcodes = &Apache::lonnet::dump_dom('uniquecodes',$cdom);
                   14904:         my $gotcode;
                   14905:         my $attempts = 0;
                   14906:         while ((!$gotcode) && ($attempts < 100)) {
                   14907:             $code = &generate_code();
                   14908:             if (!exists($currcodes{$code})) {
                   14909:                 $gotcode = 1;
                   14910:                 unless (&Apache::lonnet::newput_dom('uniquecodes',{ $code => $cnum },$cdom) eq 'ok') {
                   14911:                     $error = 'nostore';
                   14912:                 }
                   14913:             }
                   14914:             $attempts ++;
                   14915:         }
                   14916:         my @del_lock = ($cnum."\0".'uniquecodes');
                   14917:         my $dellockoutcome = &Apache::lonnet::del_dom('uniquecodes',\@del_lock,$cdom);
                   14918:     } else {
                   14919:         $error = 'nolock';
                   14920:     }
                   14921:     return ($code,$error);
                   14922: }
                   14923: 
                   14924: sub generate_code {
                   14925:     my $code;
                   14926:     my @letts = qw(B C D G H J K M N P Q R S T V W X Z);
                   14927:     for (my $i=0; $i<6; $i++) {
                   14928:         my $lettnum = int (rand 2);
                   14929:         my $item = '';
                   14930:         if ($lettnum) {
                   14931:             $item = $letts[int( rand(18) )];
                   14932:         } else {
                   14933:             $item = 1+int( rand(8) );
                   14934:         }
                   14935:         $code .= $item;
                   14936:     }
                   14937:     return $code;
                   14938: }
                   14939: 
1.444     albertel 14940: ############################################################
                   14941: ############################################################
                   14942: 
1.953     droeschl 14943: #SD
                   14944: # only Community and Course, or anything else?
1.378     raeburn  14945: sub course_type {
                   14946:     my ($cid) = @_;
                   14947:     if (!defined($cid)) {
                   14948:         $cid = $env{'request.course.id'};
                   14949:     }
1.404     albertel 14950:     if (defined($env{'course.'.$cid.'.type'})) {
                   14951:         return $env{'course.'.$cid.'.type'};
1.378     raeburn  14952:     } else {
                   14953:         return 'Course';
1.377     raeburn  14954:     }
                   14955: }
1.156     albertel 14956: 
1.406     raeburn  14957: sub group_term {
                   14958:     my $crstype = &course_type();
                   14959:     my %names = (
                   14960:                   'Course' => 'group',
1.865     raeburn  14961:                   'Community' => 'group',
1.406     raeburn  14962:                 );
                   14963:     return $names{$crstype};
                   14964: }
                   14965: 
1.902     raeburn  14966: sub course_types {
1.1165    raeburn  14967:     my @types = ('official','unofficial','community','textbook');
1.902     raeburn  14968:     my %typename = (
                   14969:                          official   => 'Official course',
                   14970:                          unofficial => 'Unofficial course',
                   14971:                          community  => 'Community',
1.1165    raeburn  14972:                          textbook   => 'Textbook course',
1.902     raeburn  14973:                    );
                   14974:     return (\@types,\%typename);
                   14975: }
                   14976: 
1.156     albertel 14977: sub icon {
                   14978:     my ($file)=@_;
1.505     albertel 14979:     my $curfext = lc((split(/\./,$file))[-1]);
1.168     albertel 14980:     my $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/unknown.gif';
1.156     albertel 14981:     my $embstyle = &Apache::loncommon::fileembstyle($curfext);
1.168     albertel 14982:     if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) {
                   14983: 	if (-e  $Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
                   14984: 	          $Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   14985: 	            $curfext.".gif") {
                   14986: 	    $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   14987: 		$curfext.".gif";
                   14988: 	}
                   14989:     }
1.249     albertel 14990:     return &lonhttpdurl($iconname);
1.154     albertel 14991: } 
1.84      albertel 14992: 
1.575     albertel 14993: sub lonhttpdurl {
1.692     www      14994: #
                   14995: # Had been used for "small fry" static images on separate port 8080.
                   14996: # Modify here if lightweight http functionality desired again.
                   14997: # Currently eliminated due to increasing firewall issues.
                   14998: #
1.575     albertel 14999:     my ($url)=@_;
1.692     www      15000:     return $url;
1.215     albertel 15001: }
                   15002: 
1.213     albertel 15003: sub connection_aborted {
                   15004:     my ($r)=@_;
                   15005:     $r->print(" ");$r->rflush();
                   15006:     my $c = $r->connection;
                   15007:     return $c->aborted();
                   15008: }
                   15009: 
1.221     foxr     15010: #    Escapes strings that may have embedded 's that will be put into
1.222     foxr     15011: #    strings as 'strings'.
                   15012: sub escape_single {
1.221     foxr     15013:     my ($input) = @_;
1.223     albertel 15014:     $input =~ s/\\/\\\\/g;	# Escape the \'s..(must be first)>
1.221     foxr     15015:     $input =~ s/\'/\\\'/g;	# Esacpe the 's....
                   15016:     return $input;
                   15017: }
1.223     albertel 15018: 
1.222     foxr     15019: #  Same as escape_single, but escape's "'s  This 
                   15020: #  can be used for  "strings"
                   15021: sub escape_double {
                   15022:     my ($input) = @_;
                   15023:     $input =~ s/\\/\\\\/g;	# Escape the /'s..(must be first)>
                   15024:     $input =~ s/\"/\\\"/g;	# Esacpe the "s....
                   15025:     return $input;
                   15026: }
1.223     albertel 15027:  
1.222     foxr     15028: #   Escapes the last element of a full URL.
                   15029: sub escape_url {
                   15030:     my ($url)   = @_;
1.238     raeburn  15031:     my @urlslices = split(/\//, $url,-1);
1.369     www      15032:     my $lastitem = &escape(pop(@urlslices));
1.1203    raeburn  15033:     return &HTML::Entities::encode(join('/',@urlslices),"'").'/'.$lastitem;
1.222     foxr     15034: }
1.462     albertel 15035: 
1.820     raeburn  15036: sub compare_arrays {
                   15037:     my ($arrayref1,$arrayref2) = @_;
                   15038:     my (@difference,%count);
                   15039:     @difference = ();
                   15040:     %count = ();
                   15041:     if ((ref($arrayref1) eq 'ARRAY') && (ref($arrayref2) eq 'ARRAY')) {
                   15042:         foreach my $element (@{$arrayref1}, @{$arrayref2}) { $count{$element}++; }
                   15043:         foreach my $element (keys(%count)) {
                   15044:             if ($count{$element} == 1) {
                   15045:                 push(@difference,$element);
                   15046:             }
                   15047:         }
                   15048:     }
                   15049:     return @difference;
                   15050: }
                   15051: 
1.817     bisitz   15052: # -------------------------------------------------------- Initialize user login
1.462     albertel 15053: sub init_user_environment {
1.463     albertel 15054:     my ($r, $username, $domain, $authhost, $form, $args) = @_;
1.462     albertel 15055:     my $lonids=$Apache::lonnet::perlvar{'lonIDsDir'};
                   15056: 
                   15057:     my $public=($username eq 'public' && $domain eq 'public');
                   15058: 
                   15059: # See if old ID present, if so, remove
                   15060: 
1.1062    raeburn  15061:     my ($filename,$cookie,$userroles,$firstaccenv,$timerintenv);
1.462     albertel 15062:     my $now=time;
                   15063: 
                   15064:     if ($public) {
                   15065: 	my $max_public=100;
                   15066: 	my $oldest;
                   15067: 	my $oldest_time=0;
                   15068: 	for(my $next=1;$next<=$max_public;$next++) {
                   15069: 	    if (-e $lonids."/publicuser_$next.id") {
                   15070: 		my $mtime=(stat($lonids."/publicuser_$next.id"))[9];
                   15071: 		if ($mtime<$oldest_time || !$oldest_time) {
                   15072: 		    $oldest_time=$mtime;
                   15073: 		    $oldest=$next;
                   15074: 		}
                   15075: 	    } else {
                   15076: 		$cookie="publicuser_$next";
                   15077: 		last;
                   15078: 	    }
                   15079: 	}
                   15080: 	if (!$cookie) { $cookie="publicuser_$oldest"; }
                   15081:     } else {
1.463     albertel 15082: 	# if this isn't a robot, kill any existing non-robot sessions
                   15083: 	if (!$args->{'robot'}) {
                   15084: 	    opendir(DIR,$lonids);
                   15085: 	    while ($filename=readdir(DIR)) {
                   15086: 		if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
                   15087: 		    unlink($lonids.'/'.$filename);
                   15088: 		}
1.462     albertel 15089: 	    }
1.463     albertel 15090: 	    closedir(DIR);
1.1204    raeburn  15091: # If there is a undeleted lockfile for the user's paste buffer remove it.
                   15092:             my $namespace = 'nohist_courseeditor';
                   15093:             my $lockingkey = 'paste'."\0".'locked_num';
                   15094:             my %lockhash = &Apache::lonnet::get($namespace,[$lockingkey],
                   15095:                                                 $domain,$username);
                   15096:             if (exists($lockhash{$lockingkey})) {
                   15097:                 my $delresult = &Apache::lonnet::del($namespace,[$lockingkey],$domain,$username);
                   15098:                 unless ($delresult eq 'ok') {
                   15099:                     &Apache::lonnet::logthis("Failed to delete paste buffer locking key in $namespace for ".$username.":".$domain." Result was: $delresult");
                   15100:                 }
                   15101:             }
1.462     albertel 15102: 	}
                   15103: # Give them a new cookie
1.463     albertel 15104: 	my $id = ($args->{'robot'} ? 'robot'.$args->{'robot'}
1.684     www      15105: 		                   : $now.$$.int(rand(10000)));
1.463     albertel 15106: 	$cookie="$username\_$id\_$domain\_$authhost";
1.462     albertel 15107:     
                   15108: # Initialize roles
                   15109: 
1.1062    raeburn  15110: 	($userroles,$firstaccenv,$timerintenv) = 
                   15111:             &Apache::lonnet::rolesinit($domain,$username,$authhost);
1.462     albertel 15112:     }
                   15113: # ------------------------------------ Check browser type and MathML capability
                   15114: 
1.1194    raeburn  15115:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,$clientunicode,
                   15116:         $clientos,$clientmobile,$clientinfo,$clientosversion) = &decode_user_agent($r);
1.462     albertel 15117: 
                   15118: # ------------------------------------------------------------- Get environment
                   15119: 
                   15120:     my %userenv = &Apache::lonnet::dump('environment',$domain,$username);
                   15121:     my ($tmp) = keys(%userenv);
                   15122:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   15123:     } else {
                   15124: 	undef(%userenv);
                   15125:     }
                   15126:     if (($userenv{'interface'}) && (!$form->{'interface'})) {
                   15127: 	$form->{'interface'}=$userenv{'interface'};
                   15128:     }
                   15129:     if ($userenv{'texengine'} eq 'ttm') { $clientmathml=1; }
                   15130: 
                   15131: # --------------- Do not trust query string to be put directly into environment
1.817     bisitz   15132:     foreach my $option ('interface','localpath','localres') {
                   15133:         $form->{$option}=~s/[\n\r\=]//gs;
1.462     albertel 15134:     }
                   15135: # --------------------------------------------------------- Write first profile
                   15136: 
                   15137:     {
                   15138: 	my %initial_env = 
                   15139: 	    ("user.name"          => $username,
                   15140: 	     "user.domain"        => $domain,
                   15141: 	     "user.home"          => $authhost,
                   15142: 	     "browser.type"       => $clientbrowser,
                   15143: 	     "browser.version"    => $clientversion,
                   15144: 	     "browser.mathml"     => $clientmathml,
                   15145: 	     "browser.unicode"    => $clientunicode,
                   15146: 	     "browser.os"         => $clientos,
1.1137    raeburn  15147:              "browser.mobile"     => $clientmobile,
1.1141    raeburn  15148:              "browser.info"       => $clientinfo,
1.1194    raeburn  15149:              "browser.osversion"  => $clientosversion,
1.462     albertel 15150: 	     "server.domain"      => $Apache::lonnet::perlvar{'lonDefDomain'},
                   15151: 	     "request.course.fn"  => '',
                   15152: 	     "request.course.uri" => '',
                   15153: 	     "request.course.sec" => '',
                   15154: 	     "request.role"       => 'cm',
                   15155: 	     "request.role.adv"   => $env{'user.adv'},
                   15156: 	     "request.host"       => $ENV{'REMOTE_ADDR'},);
                   15157: 
                   15158:         if ($form->{'localpath'}) {
                   15159: 	    $initial_env{"browser.localpath"}  = $form->{'localpath'};
                   15160: 	    $initial_env{"browser.localres"}   = $form->{'localres'};
                   15161:         }
                   15162: 	
                   15163: 	if ($form->{'interface'}) {
                   15164: 	    $form->{'interface'}=~s/\W//gs;
                   15165: 	    $initial_env{"browser.interface"} = $form->{'interface'};
                   15166: 	    $env{'browser.interface'}=$form->{'interface'};
                   15167: 	}
                   15168: 
1.1157    raeburn  15169:         if ($form->{'iptoken'}) {
                   15170:             my $lonhost = $r->dir_config('lonHostID');
                   15171:             $initial_env{"user.noloadbalance"} = $lonhost;
                   15172:             $env{'user.noloadbalance'} = $lonhost;
                   15173:         }
                   15174: 
1.981     raeburn  15175:         my %is_adv = ( is_adv => $env{'user.adv'} );
1.1016    raeburn  15176:         my %domdef;
                   15177:         unless ($domain eq 'public') {
                   15178:             %domdef = &Apache::lonnet::get_domain_defaults($domain);
                   15179:         }
1.980     raeburn  15180: 
1.1081    raeburn  15181:         foreach my $tool ('aboutme','blog','webdav','portfolio') {
1.724     raeburn  15182:             $userenv{'availabletools.'.$tool} = 
1.980     raeburn  15183:                 &Apache::lonnet::usertools_access($username,$domain,$tool,'reload',
                   15184:                                                   undef,\%userenv,\%domdef,\%is_adv);
1.724     raeburn  15185:         }
                   15186: 
1.1165    raeburn  15187:         foreach my $crstype ('official','unofficial','community','textbook') {
1.765     raeburn  15188:             $userenv{'canrequest.'.$crstype} =
                   15189:                 &Apache::lonnet::usertools_access($username,$domain,$crstype,
1.980     raeburn  15190:                                                   'reload','requestcourses',
                   15191:                                                   \%userenv,\%domdef,\%is_adv);
1.765     raeburn  15192:         }
                   15193: 
1.1092    raeburn  15194:         $userenv{'canrequest.author'} =
                   15195:             &Apache::lonnet::usertools_access($username,$domain,'requestauthor',
                   15196:                                         'reload','requestauthor',
                   15197:                                         \%userenv,\%domdef,\%is_adv);
                   15198:         my %reqauthor = &Apache::lonnet::get('requestauthor',['author_status','author'],
                   15199:                                              $domain,$username);
                   15200:         my $reqstatus = $reqauthor{'author_status'};
                   15201:         if ($reqstatus eq 'approval' || $reqstatus eq 'approved') { 
                   15202:             if (ref($reqauthor{'author'}) eq 'HASH') {
                   15203:                 $userenv{'requestauthorqueued'} = $reqstatus.':'.
                   15204:                                                   $reqauthor{'author'}{'timestamp'};
                   15205:             }
                   15206:         }
                   15207: 
1.462     albertel 15208: 	$env{'user.environment'} = "$lonids/$cookie.id";
1.1062    raeburn  15209: 
1.462     albertel 15210: 	if (tie(my %disk_env,'GDBM_File',"$lonids/$cookie.id",
                   15211: 		 &GDBM_WRCREAT(),0640)) {
                   15212: 	    &_add_to_env(\%disk_env,\%initial_env);
                   15213: 	    &_add_to_env(\%disk_env,\%userenv,'environment.');
                   15214: 	    &_add_to_env(\%disk_env,$userroles);
1.1062    raeburn  15215:             if (ref($firstaccenv) eq 'HASH') {
                   15216:                 &_add_to_env(\%disk_env,$firstaccenv);
                   15217:             }
                   15218:             if (ref($timerintenv) eq 'HASH') {
                   15219:                 &_add_to_env(\%disk_env,$timerintenv);
                   15220:             }
1.463     albertel 15221: 	    if (ref($args->{'extra_env'})) {
                   15222: 		&_add_to_env(\%disk_env,$args->{'extra_env'});
                   15223: 	    }
1.462     albertel 15224: 	    untie(%disk_env);
                   15225: 	} else {
1.705     tempelho 15226: 	    &Apache::lonnet::logthis("<span style=\"color:blue;\">WARNING: ".
                   15227: 			   'Could not create environment storage in lonauth: '.$!.'</span>');
1.462     albertel 15228: 	    return 'error: '.$!;
                   15229: 	}
                   15230:     }
                   15231:     $env{'request.role'}='cm';
                   15232:     $env{'request.role.adv'}=$env{'user.adv'};
                   15233:     $env{'browser.type'}=$clientbrowser;
                   15234: 
                   15235:     return $cookie;
                   15236: 
                   15237: }
                   15238: 
                   15239: sub _add_to_env {
                   15240:     my ($idf,$env_data,$prefix) = @_;
1.676     raeburn  15241:     if (ref($env_data) eq 'HASH') {
                   15242:         while (my ($key,$value) = each(%$env_data)) {
                   15243: 	    $idf->{$prefix.$key} = $value;
                   15244: 	    $env{$prefix.$key}   = $value;
                   15245:         }
1.462     albertel 15246:     }
                   15247: }
                   15248: 
1.685     tempelho 15249: # --- Get the symbolic name of a problem and the url
                   15250: sub get_symb {
                   15251:     my ($request,$silent) = @_;
1.726     raeburn  15252:     (my $url=$env{'form.url'}) =~ s-^https?\://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
1.685     tempelho 15253:     my $symb=($env{'form.symb'} ne '' ? $env{'form.symb'} : (&Apache::lonnet::symbread($url)));
                   15254:     if ($symb eq '') {
                   15255:         if (!$silent) {
1.1071    raeburn  15256:             if (ref($request)) { 
                   15257:                 $request->print("Unable to handle ambiguous references:$url:.");
                   15258:             }
1.685     tempelho 15259:             return ();
                   15260:         }
                   15261:     }
                   15262:     &Apache::lonenc::check_decrypt(\$symb);
                   15263:     return ($symb);
                   15264: }
                   15265: 
                   15266: # --------------------------------------------------------------Get annotation
                   15267: 
                   15268: sub get_annotation {
                   15269:     my ($symb,$enc) = @_;
                   15270: 
                   15271:     my $key = $symb;
                   15272:     if (!$enc) {
                   15273:         $key =
                   15274:             &Apache::lonnet::clutter((&Apache::lonnet::decode_symb($symb))[2]);
                   15275:     }
                   15276:     my %annotation=&Apache::lonnet::get('nohist_annotations',[$key]);
                   15277:     return $annotation{$key};
                   15278: }
                   15279: 
                   15280: sub clean_symb {
1.731     raeburn  15281:     my ($symb,$delete_enc) = @_;
1.685     tempelho 15282: 
                   15283:     &Apache::lonenc::check_decrypt(\$symb);
                   15284:     my $enc = $env{'request.enc'};
1.731     raeburn  15285:     if ($delete_enc) {
1.730     raeburn  15286:         delete($env{'request.enc'});
                   15287:     }
1.685     tempelho 15288: 
                   15289:     return ($symb,$enc);
                   15290: }
1.462     albertel 15291: 
1.1181    raeburn  15292: ############################################################
                   15293: ############################################################
                   15294: 
                   15295: =pod
                   15296: 
                   15297: =head1 Routines for building display used to search for courses
                   15298: 
                   15299: 
                   15300: =over 4
                   15301: 
                   15302: =item * &build_filters()
                   15303: 
                   15304: Create markup for a table used to set filters to use when selecting
1.1182    raeburn  15305: courses in a domain.  Used by lonpickcourse.pm, lonmodifycourse.pm
                   15306: and quotacheck.pl
                   15307: 
1.1181    raeburn  15308: 
                   15309: Inputs:
                   15310: 
                   15311: filterlist - anonymous array of fields to include as potential filters 
                   15312: 
                   15313: crstype - course type
                   15314: 
                   15315: roleelement - fifth arg in selectcourse_link() populates fifth arg in javascript: opencrsbrowser() function, used
                   15316:               to pop-open a course selector (will contain "extra element"). 
                   15317: 
                   15318: multelement - if multiple course selections will be allowed, this will be a hidden form element: name: multiple; value: 1
                   15319: 
                   15320: filter - anonymous hash of criteria and their values
                   15321: 
                   15322: action - form action
                   15323: 
                   15324: numfiltersref - ref to scalar (count of number of elements in institutional codes -- e.g., 4 for year, semester, department, and number)
                   15325: 
1.1182    raeburn  15326: caller - caller context (e.g., set to 'modifycourse' when routine is called from lonmodifycourse.pm)
1.1181    raeburn  15327: 
                   15328: cloneruname - username of owner of new course who wants to clone
                   15329: 
                   15330: clonerudom - domain of owner of new course who wants to clone
                   15331: 
                   15332: typeelem - text to use for left column in row containing course type (i.e., Course, Community or Course/Community) 
                   15333: 
                   15334: codetitlesref - reference to array of titles of components in institutional codes (official courses)
                   15335: 
                   15336: codedom - domain
                   15337: 
                   15338: formname - value of form element named "form". 
                   15339: 
                   15340: fixeddom - domain, if fixed.
                   15341: 
                   15342: prevphase - value to assign to form element named "phase" when going back to the previous screen  
                   15343: 
                   15344: cnameelement - name of form element in form on opener page which will receive title of selected course 
                   15345: 
                   15346: cnumelement - name of form element in form on opener page which will receive courseID  of selected course
                   15347: 
                   15348: cdomelement - name of form element in form on opener page which will receive domain of selected course
                   15349: 
                   15350: setroles - includes access constraint identifier when setting a roles-based condition for acces to a portfolio file
                   15351: 
                   15352: clonetext - hidden form elements containing list of courses cloneable by intended course owner when DC creates a course
                   15353: 
                   15354: clonewarning - warning message about missing information for intended course owner when DC creates a course
                   15355: 
1.1182    raeburn  15356: 
1.1181    raeburn  15357: Returns: $output - HTML for display of search criteria, and hidden form elements.
                   15358: 
1.1182    raeburn  15359: 
1.1181    raeburn  15360: Side Effects: None
                   15361: 
                   15362: =cut
                   15363: 
                   15364: # ---------------------------------------------- search for courses based on last activity etc.
                   15365: 
                   15366: sub build_filters {
                   15367:     my ($filterlist,$crstype,$roleelement,$multelement,$filter,$action,
                   15368:         $numtitlesref,$caller,$cloneruname,$clonerudom,$typeelement,
                   15369:         $codetitlesref,$codedom,$formname,$fixeddom,$prevphase,
                   15370:         $cnameelement,$cnumelement,$cdomelement,$setroles,
                   15371:         $clonetext,$clonewarning) = @_;
1.1182    raeburn  15372:     my ($list,$jscript);
1.1181    raeburn  15373:     my $onchange = 'javascript:updateFilters(this)';
                   15374:     my ($domainselectform,$sincefilterform,$createdfilterform,
                   15375:         $ownerdomselectform,$persondomselectform,$instcodeform,
                   15376:         $typeselectform,$instcodetitle);
                   15377:     if ($formname eq '') {
                   15378:         $formname = $caller;
                   15379:     }
                   15380:     foreach my $item (@{$filterlist}) {
                   15381:         unless (($item eq 'descriptfilter') || ($item eq 'instcodefilter') ||
                   15382:                 ($item eq 'sincefilter') || ($item eq 'createdfilter')) {
                   15383:             if ($item eq 'domainfilter') {
                   15384:                 $filter->{$item} = &LONCAPA::clean_domain($filter->{$item});
                   15385:             } elsif ($item eq 'coursefilter') {
                   15386:                 $filter->{$item} = &LONCAPA::clean_courseid($filter->{$item});
                   15387:             } elsif ($item eq 'ownerfilter') {
                   15388:                 $filter->{$item} = &LONCAPA::clean_username($filter->{$item});
                   15389:             } elsif ($item eq 'ownerdomfilter') {
                   15390:                 $filter->{'ownerdomfilter'} =
                   15391:                     &LONCAPA::clean_domain($filter->{$item});
                   15392:                 $ownerdomselectform = &select_dom_form($filter->{'ownerdomfilter'},
                   15393:                                                        'ownerdomfilter',1);
                   15394:             } elsif ($item eq 'personfilter') {
                   15395:                 $filter->{$item} = &LONCAPA::clean_username($filter->{$item});
                   15396:             } elsif ($item eq 'persondomfilter') {
                   15397:                 $persondomselectform = &select_dom_form($filter->{'persondomfilter'},
                   15398:                                                         'persondomfilter',1);
                   15399:             } else {
                   15400:                 $filter->{$item} =~ s/\W//g;
                   15401:             }
                   15402:             if (!$filter->{$item}) {
                   15403:                 $filter->{$item} = '';
                   15404:             }
                   15405:         }
                   15406:         if ($item eq 'domainfilter') {
                   15407:             my $allow_blank = 1;
                   15408:             if ($formname eq 'portform') {
                   15409:                 $allow_blank=0;
                   15410:             } elsif ($formname eq 'studentform') {
                   15411:                 $allow_blank=0;
                   15412:             }
                   15413:             if ($fixeddom) {
                   15414:                 $domainselectform = '<input type="hidden" name="domainfilter"'.
                   15415:                                     ' value="'.$codedom.'" />'.
                   15416:                                     &Apache::lonnet::domain($codedom,'description');
                   15417:             } else {
                   15418:                 $domainselectform = &select_dom_form($filter->{$item},
                   15419:                                                      'domainfilter',
                   15420:                                                       $allow_blank,'',$onchange);
                   15421:             }
                   15422:         } else {
                   15423:             $list->{$item} = &HTML::Entities::encode($filter->{$item},'<>&"');
                   15424:         }
                   15425:     }
                   15426: 
                   15427:     # last course activity filter and selection
                   15428:     $sincefilterform = &timebased_select_form('sincefilter',$filter);
                   15429: 
                   15430:     # course created filter and selection
                   15431:     if (exists($filter->{'createdfilter'})) {
                   15432:         $createdfilterform = &timebased_select_form('createdfilter',$filter);
                   15433:     }
                   15434: 
                   15435:     my %lt = &Apache::lonlocal::texthash(
                   15436:                 'cac' => "$crstype Activity",
                   15437:                 'ccr' => "$crstype Created",
                   15438:                 'cde' => "$crstype Title",
                   15439:                 'cdo' => "$crstype Domain",
                   15440:                 'ins' => 'Institutional Code',
                   15441:                 'inc' => 'Institutional Categorization',
                   15442:                 'cow' => "$crstype Owner/Co-owner",
                   15443:                 'cop' => "$crstype Personnel Includes",
                   15444:                 'cog' => 'Type',
                   15445:              );
                   15446: 
                   15447:     if (($formname eq 'ccrs') || ($formname eq 'requestcrs')) {
                   15448:         my $typeval = 'Course';
                   15449:         if ($crstype eq 'Community') {
                   15450:             $typeval = 'Community';
                   15451:         }
                   15452:         $typeselectform = '<input type="hidden" name="type" value="'.$typeval.'" />';
                   15453:     } else {
                   15454:         $typeselectform =  '<select name="type" size="1"';
                   15455:         if ($onchange) {
                   15456:             $typeselectform .= ' onchange="'.$onchange.'"';
                   15457:         }
                   15458:         $typeselectform .= '>'."\n";
                   15459:         foreach my $posstype ('Course','Community') {
                   15460:             $typeselectform.='<option value="'.$posstype.'"'.
                   15461:                 ($posstype eq $crstype ? ' selected="selected" ' : ''). ">".&mt($posstype)."</option>\n";
                   15462:         }
                   15463:         $typeselectform.="</select>";
                   15464:     }
                   15465: 
                   15466:     my ($cloneableonlyform,$cloneabletitle);
                   15467:     if (exists($filter->{'cloneableonly'})) {
                   15468:         my $cloneableon = '';
                   15469:         my $cloneableoff = ' checked="checked"';
                   15470:         if ($filter->{'cloneableonly'}) {
                   15471:             $cloneableon = $cloneableoff;
                   15472:             $cloneableoff = '';
                   15473:         }
                   15474:         $cloneableonlyform = '<span class="LC_nobreak"><label><input type="radio" name="cloneableonly" value="1" '.$cloneableon.'/>&nbsp;'.&mt('Required').'</label>'.('&nbsp;'x3).'<label><input type="radio" name="cloneableonly" value="" '.$cloneableoff.' />&nbsp;'.&mt('No restriction').'</label></span>';
                   15475:         if ($formname eq 'ccrs') {
1.1187    bisitz   15476:             $cloneabletitle = &mt('Cloneable for [_1]',$cloneruname.':'.$clonerudom);
1.1181    raeburn  15477:         } else {
                   15478:             $cloneabletitle = &mt('Cloneable by you');
                   15479:         }
                   15480:     }
                   15481:     my $officialjs;
                   15482:     if ($crstype eq 'Course') {
                   15483:         if (exists($filter->{'instcodefilter'})) {
1.1182    raeburn  15484: #            if (($fixeddom) || ($formname eq 'requestcrs') ||
                   15485: #                ($formname eq 'modifycourse') || ($formname eq 'filterpicker')) {
                   15486:             if ($codedom) { 
1.1181    raeburn  15487:                 $officialjs = 1;
                   15488:                 ($instcodeform,$jscript,$$numtitlesref) =
                   15489:                     &Apache::courseclassifier::instcode_selectors($codedom,'filterpicker',
                   15490:                                                                   $officialjs,$codetitlesref);
                   15491:                 if ($jscript) {
1.1182    raeburn  15492:                     $jscript = '<script type="text/javascript">'."\n".
                   15493:                                '// <![CDATA['."\n".
                   15494:                                $jscript."\n".
                   15495:                                '// ]]>'."\n".
                   15496:                                '</script>'."\n";
1.1181    raeburn  15497:                 }
                   15498:             }
                   15499:             if ($instcodeform eq '') {
                   15500:                 $instcodeform =
                   15501:                     '<input type="text" name="instcodefilter" size="10" value="'.
                   15502:                     $list->{'instcodefilter'}.'" />';
                   15503:                 $instcodetitle = $lt{'ins'};
                   15504:             } else {
                   15505:                 $instcodetitle = $lt{'inc'};
                   15506:             }
                   15507:             if ($fixeddom) {
                   15508:                 $instcodetitle .= '<br />('.$codedom.')';
                   15509:             }
                   15510:         }
                   15511:     }
                   15512:     my $output = qq|
                   15513: <form method="post" name="filterpicker" action="$action">
                   15514: <input type="hidden" name="form" value="$formname" />
                   15515: |;
                   15516:     if ($formname eq 'modifycourse') {
                   15517:         $output .= '<input type="hidden" name="phase" value="courselist" />'."\n".
                   15518:                    '<input type="hidden" name="prevphase" value="'.
                   15519:                    $prevphase.'" />'."\n";
1.1198    musolffc 15520:     } elsif ($formname eq 'quotacheck') {
                   15521:         $output .= qq|
                   15522: <input type="hidden" name="sortby" value="" />
                   15523: <input type="hidden" name="sortorder" value="" />
                   15524: |;
                   15525:     } else {
1.1181    raeburn  15526:         my $name_input;
                   15527:         if ($cnameelement ne '') {
                   15528:             $name_input = '<input type="hidden" name="cnameelement" value="'.
                   15529:                           $cnameelement.'" />';
                   15530:         }
                   15531:         $output .= qq|
1.1182    raeburn  15532: <input type="hidden" name="cnumelement" value="$cnumelement" />
                   15533: <input type="hidden" name="cdomelement" value="$cdomelement" />
1.1181    raeburn  15534: $name_input
                   15535: $roleelement
                   15536: $multelement
                   15537: $typeelement
                   15538: |;
                   15539:         if ($formname eq 'portform') {
                   15540:             $output .= '<input type="hidden" name="setroles" value="'.$setroles.'" />'."\n";
                   15541:         }
                   15542:     }
                   15543:     if ($fixeddom) {
                   15544:         $output .= '<input type="hidden" name="fixeddom" value="'.$fixeddom.'" />'."\n";
                   15545:     }
                   15546:     $output .= "<br />\n".&Apache::lonhtmlcommon::start_pick_box();
                   15547:     if ($sincefilterform) {
                   15548:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cac'})
                   15549:                   .$sincefilterform
                   15550:                   .&Apache::lonhtmlcommon::row_closure();
                   15551:     }
                   15552:     if ($createdfilterform) {
                   15553:         $output .= &Apache::lonhtmlcommon::row_title($lt{'ccr'})
                   15554:                   .$createdfilterform
                   15555:                   .&Apache::lonhtmlcommon::row_closure();
                   15556:     }
                   15557:     if ($domainselectform) {
                   15558:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cdo'})
                   15559:                   .$domainselectform
                   15560:                   .&Apache::lonhtmlcommon::row_closure();
                   15561:     }
                   15562:     if ($typeselectform) {
                   15563:         if (($formname eq 'ccrs') || ($formname eq 'requestcrs')) {
                   15564:             $output .= $typeselectform;
                   15565:         } else {
                   15566:             $output .= &Apache::lonhtmlcommon::row_title($lt{'cog'})
                   15567:                       .$typeselectform
                   15568:                       .&Apache::lonhtmlcommon::row_closure();
                   15569:         }
                   15570:     }
                   15571:     if ($instcodeform) {
                   15572:         $output .= &Apache::lonhtmlcommon::row_title($instcodetitle)
                   15573:                   .$instcodeform
                   15574:                   .&Apache::lonhtmlcommon::row_closure();
                   15575:     }
                   15576:     if (exists($filter->{'ownerfilter'})) {
                   15577:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cow'}).
                   15578:                    '<table><tr><td>'.&mt('Username').'<br />'.
                   15579:                    '<input type="text" name="ownerfilter" size="20" value="'.
                   15580:                    $list->{'ownerfilter'}.'" /></td><td>'.&mt('Domain').'<br />'.
                   15581:                    $ownerdomselectform.'</td></tr></table>'.
                   15582:                    &Apache::lonhtmlcommon::row_closure();
                   15583:     }
                   15584:     if (exists($filter->{'personfilter'})) {
                   15585:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cop'}).
                   15586:                    '<table><tr><td>'.&mt('Username').'<br />'.
                   15587:                    '<input type="text" name="personfilter" size="20" value="'.
                   15588:                    $list->{'personfilter'}.'" /></td><td>'.&mt('Domain').'<br />'.
                   15589:                    $persondomselectform.'</td></tr></table>'.
                   15590:                    &Apache::lonhtmlcommon::row_closure();
                   15591:     }
                   15592:     if (exists($filter->{'coursefilter'})) {
                   15593:         $output .= &Apache::lonhtmlcommon::row_title(&mt('LON-CAPA course ID'))
                   15594:                   .'<input type="text" name="coursefilter" size="25" value="'
                   15595:                   .$list->{'coursefilter'}.'" />'
                   15596:                   .&Apache::lonhtmlcommon::row_closure();
                   15597:     }
                   15598:     if ($cloneableonlyform) {
                   15599:         $output .= &Apache::lonhtmlcommon::row_title($cloneabletitle).
                   15600:                    $cloneableonlyform.&Apache::lonhtmlcommon::row_closure();
                   15601:     }
                   15602:     if (exists($filter->{'descriptfilter'})) {
                   15603:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cde'})
                   15604:                   .'<input type="text" name="descriptfilter" size="40" value="'
                   15605:                   .$list->{'descriptfilter'}.'" />'
                   15606:                   .&Apache::lonhtmlcommon::row_closure(1);
                   15607:     }
                   15608:     $output .= &Apache::lonhtmlcommon::end_pick_box().'<p>'.$clonetext."\n".
                   15609:                '<input type="hidden" name="updater" value="" />'."\n".
                   15610:                '<input type="submit" name="gosearch" value="'.
                   15611:                &mt('Search').'" /></p>'."\n".'</form>'."\n".'<hr />'."\n";
                   15612:     return $jscript.$clonewarning.$output;
                   15613: }
                   15614: 
                   15615: =pod 
                   15616: 
                   15617: =item * &timebased_select_form()
                   15618: 
1.1182    raeburn  15619: Create markup for a dropdown list used to select a time-based
1.1181    raeburn  15620: filter e.g., Course Activity, Course Created, when searching for courses
                   15621: or communities
                   15622: 
                   15623: Inputs:
                   15624: 
                   15625: item - name of form element (sincefilter or createdfilter)
                   15626: 
                   15627: filter - anonymous hash of criteria and their values
                   15628: 
                   15629: Returns: HTML for a select box contained a blank, then six time selections,
                   15630:          with value set in incoming form variables currently selected. 
                   15631: 
                   15632: Side Effects: None
                   15633: 
                   15634: =cut
                   15635: 
                   15636: sub timebased_select_form {
                   15637:     my ($item,$filter) = @_;
                   15638:     if (ref($filter) eq 'HASH') {
                   15639:         $filter->{$item} =~ s/[^\d-]//g;
                   15640:         if (!$filter->{$item}) { $filter->{$item}=-1; }
                   15641:         return &select_form(
                   15642:                             $filter->{$item},
                   15643:                             $item,
                   15644:                             {      '-1' => '',
                   15645:                                 '86400' => &mt('today'),
                   15646:                                '604800' => &mt('last week'),
                   15647:                               '2592000' => &mt('last month'),
                   15648:                               '7776000' => &mt('last three months'),
                   15649:                              '15552000' => &mt('last six months'),
                   15650:                              '31104000' => &mt('last year'),
                   15651:                     'select_form_order' =>
                   15652:                            ['-1','86400','604800','2592000','7776000',
                   15653:                             '15552000','31104000']});
                   15654:     }
                   15655: }
                   15656: 
                   15657: =pod
                   15658: 
                   15659: =item * &js_changer()
                   15660: 
                   15661: Create script tag containing Javascript used to submit course search form
1.1183    raeburn  15662: when course type or domain is changed, and also to hide 'Searching ...' on
                   15663: page load completion for page showing search result.
1.1181    raeburn  15664: 
                   15665: Inputs: None
                   15666: 
1.1183    raeburn  15667: Returns: markup containing updateFilters() and hideSearching() javascript functions. 
1.1181    raeburn  15668: 
                   15669: Side Effects: None
                   15670: 
                   15671: =cut
                   15672: 
                   15673: sub js_changer {
                   15674:     return <<ENDJS;
                   15675: <script type="text/javascript">
                   15676: // <![CDATA[
                   15677: function updateFilters(caller) {
                   15678:     if (typeof(caller) != "undefined") {
                   15679:         document.filterpicker.updater.value = caller.name;
                   15680:     }
                   15681:     document.filterpicker.submit();
                   15682: }
1.1183    raeburn  15683: 
                   15684: function hideSearching() {
                   15685:     if (document.getElementById('searching')) {
                   15686:         document.getElementById('searching').style.display = 'none';
                   15687:     }
                   15688:     return;
                   15689: }
                   15690: 
1.1181    raeburn  15691: // ]]>
                   15692: </script>
                   15693: 
                   15694: ENDJS
                   15695: }
                   15696: 
                   15697: =pod
                   15698: 
1.1182    raeburn  15699: =item * &search_courses()
                   15700: 
                   15701: Process selected filters form course search form and pass to lonnet::courseiddump
                   15702: to retrieve a hash for which keys are courseIDs which match the selected filters.
                   15703: 
                   15704: Inputs:
                   15705: 
                   15706: dom - domain being searched 
                   15707: 
                   15708: type - course type ('Course' or 'Community' or '.' if any).
                   15709: 
                   15710: filter - anonymous hash of criteria and their values
                   15711: 
                   15712: numtitles - for institutional codes - number of categories
                   15713: 
                   15714: cloneruname - optional username of new course owner
                   15715: 
                   15716: clonerudom - optional domain of new course owner
                   15717: 
1.1221    raeburn  15718: domcloner - optional "domcloner" flag; has value=1 if user has ccc priv in domain being filtered by, 
1.1182    raeburn  15719:             (used when DC is using course creation form)
                   15720: 
                   15721: codetitles - reference to array of titles of components in institutional codes (official courses).
                   15722: 
1.1221    raeburn  15723: cc_clone - escaped comma separated list of courses for which course cloner has active CC role
                   15724:            (and so can clone automatically)
                   15725: 
                   15726: reqcrsdom - domain of new course, where search_courses is used to identify potential courses to clone
                   15727: 
                   15728: reqinstcode - institutional code of new course, where search_courses is used to identify potential 
                   15729:               courses to clone 
1.1182    raeburn  15730: 
                   15731: Returns: %courses - hash of courses satisfying search criteria, keys = course IDs, values are corresponding colon-separated escaped description, institutional code, owner and type.
                   15732: 
                   15733: 
                   15734: Side Effects: None
                   15735: 
                   15736: =cut
                   15737: 
                   15738: 
                   15739: sub search_courses {
1.1221    raeburn  15740:     my ($dom,$type,$filter,$numtitles,$cloneruname,$clonerudom,$domcloner,$codetitles,
                   15741:         $cc_clone,$reqcrsdom,$reqinstcode) = @_;
1.1182    raeburn  15742:     my (%courses,%showcourses,$cloner);
                   15743:     if (($filter->{'ownerfilter'} ne '') ||
                   15744:         ($filter->{'ownerdomfilter'} ne '')) {
                   15745:         $filter->{'combownerfilter'} = $filter->{'ownerfilter'}.':'.
                   15746:                                        $filter->{'ownerdomfilter'};
                   15747:     }
                   15748:     foreach my $item ('descriptfilter','coursefilter','combownerfilter') {
                   15749:         if (!$filter->{$item}) {
                   15750:             $filter->{$item}='.';
                   15751:         }
                   15752:     }
                   15753:     my $now = time;
                   15754:     my $timefilter =
                   15755:        ($filter->{'sincefilter'}==-1?1:$now-$filter->{'sincefilter'});
                   15756:     my ($createdbefore,$createdafter);
                   15757:     if (($filter->{'createdfilter'} ne '') && ($filter->{'createdfilter'} !=-1)) {
                   15758:         $createdbefore = $now;
                   15759:         $createdafter = $now-$filter->{'createdfilter'};
                   15760:     }
                   15761:     my ($instcodefilter,$regexpok);
                   15762:     if ($numtitles) {
                   15763:         if ($env{'form.official'} eq 'on') {
                   15764:             $instcodefilter =
                   15765:                 &Apache::courseclassifier::instcode_search_str($dom,$numtitles,$codetitles);
                   15766:             $regexpok = 1;
                   15767:         } elsif ($env{'form.official'} eq 'off') {
                   15768:             $instcodefilter = &Apache::courseclassifier::instcode_search_str($dom,$numtitles,$codetitles);
                   15769:             unless ($instcodefilter eq '') {
                   15770:                 $regexpok = -1;
                   15771:             }
                   15772:         }
                   15773:     } else {
                   15774:         $instcodefilter = $filter->{'instcodefilter'};
                   15775:     }
                   15776:     if ($instcodefilter eq '') { $instcodefilter = '.'; }
                   15777:     if ($type eq '') { $type = '.'; }
                   15778: 
                   15779:     if (($clonerudom ne '') && ($cloneruname ne '')) {
                   15780:         $cloner = $cloneruname.':'.$clonerudom;
                   15781:     }
                   15782:     %courses = &Apache::lonnet::courseiddump($dom,
                   15783:                                              $filter->{'descriptfilter'},
                   15784:                                              $timefilter,
                   15785:                                              $instcodefilter,
                   15786:                                              $filter->{'combownerfilter'},
                   15787:                                              $filter->{'coursefilter'},
                   15788:                                              undef,undef,$type,$regexpok,undef,undef,
1.1221    raeburn  15789:                                              undef,undef,$cloner,$cc_clone,
1.1182    raeburn  15790:                                              $filter->{'cloneableonly'},
                   15791:                                              $createdbefore,$createdafter,undef,
1.1221    raeburn  15792:                                              $domcloner,undef,$reqcrsdom,$reqinstcode);
1.1182    raeburn  15793:     if (($filter->{'personfilter'} ne '') && ($filter->{'persondomfilter'} ne '')) {
                   15794:         my $ccrole;
                   15795:         if ($type eq 'Community') {
                   15796:             $ccrole = 'co';
                   15797:         } else {
                   15798:             $ccrole = 'cc';
                   15799:         }
                   15800:         my %rolehash = &Apache::lonnet::get_my_roles($filter->{'personfilter'},
                   15801:                                                      $filter->{'persondomfilter'},
                   15802:                                                      'userroles',undef,
                   15803:                                                      [$ccrole,'in','ad','ep','ta','cr'],
                   15804:                                                      $dom);
                   15805:         foreach my $role (keys(%rolehash)) {
                   15806:             my ($cnum,$cdom,$courserole) = split(':',$role);
                   15807:             my $cid = $cdom.'_'.$cnum;
                   15808:             if (exists($courses{$cid})) {
                   15809:                 if (ref($courses{$cid}) eq 'HASH') {
                   15810:                     if (ref($courses{$cid}{roles}) eq 'ARRAY') {
                   15811:                         if (!grep(/^\Q$courserole\E$/,@{$courses{$cid}{roles}})) {
                   15812:                             push (@{$courses{$cid}{roles}},$courserole);
                   15813:                         }
                   15814:                     } else {
                   15815:                         $courses{$cid}{roles} = [$courserole];
                   15816:                     }
                   15817:                     $showcourses{$cid} = $courses{$cid};
                   15818:                 }
                   15819:             }
                   15820:         }
                   15821:         %courses = %showcourses;
                   15822:     }
                   15823:     return %courses;
                   15824: }
                   15825: 
                   15826: =pod
                   15827: 
1.1181    raeburn  15828: =back
                   15829: 
1.1207    raeburn  15830: =head1 Routines for version requirements for current course.
                   15831: 
                   15832: =over 4
                   15833: 
                   15834: =item * &check_release_required()
                   15835: 
                   15836: Compares required LON-CAPA version with version on server, and
                   15837: if required version is newer looks for a server with the required version.
                   15838: 
                   15839: Looks first at servers in user's owen domain; if none suitable, looks at
                   15840: servers in course's domain are permitted to host sessions for user's domain.
                   15841: 
                   15842: Inputs:
                   15843: 
                   15844: $loncaparev - Version on current server (format: Major.Minor.Subrelease-datestamp)
                   15845: 
                   15846: $courseid - Course ID of current course
                   15847: 
                   15848: $rolecode - User's current role in course (for switchserver query string).
                   15849: 
                   15850: $required - LON-CAPA version needed by course (format: Major.Minor).
                   15851: 
                   15852: 
                   15853: Returns:
                   15854: 
                   15855: $switchserver - query string tp append to /adm/switchserver call (if 
                   15856:                 current server's LON-CAPA version is too old. 
                   15857: 
                   15858: $warning - Message is displayed if no suitable server could be found.
                   15859: 
                   15860: =cut
                   15861: 
                   15862: sub check_release_required {
                   15863:     my ($loncaparev,$courseid,$rolecode,$required) = @_;
                   15864:     my ($switchserver,$warning);
                   15865:     if ($required ne '') {
                   15866:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
                   15867:         my ($major,$minor) = ($loncaparev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   15868:         if ($reqdmajor ne '' && $reqdminor ne '') {
                   15869:             my $otherserver;
                   15870:             if (($major eq '' && $minor eq '') ||
                   15871:                 (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
                   15872:                 my ($userdomserver) = &Apache::lonnet::choose_server($env{'user.domain'},undef,$required,1);
                   15873:                 my $switchlcrev =
                   15874:                     &Apache::lonnet::get_server_loncaparev($env{'user.domain'},
                   15875:                                                            $userdomserver);
                   15876:                 my ($swmajor,$swminor) = ($switchlcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                   15877:                 if (($swmajor eq '' && $swminor eq '') || ($reqdmajor > $swmajor) ||
                   15878:                     (($reqdmajor == $swmajor) && ($reqdminor > $swminor))) {
                   15879:                     my $cdom = $env{'course.'.$courseid.'.domain'};
                   15880:                     if ($cdom ne $env{'user.domain'}) {
                   15881:                         my ($coursedomserver,$coursehostname) = &Apache::lonnet::choose_server($cdom,undef,$required,1);
                   15882:                         my $serverhomeID = &Apache::lonnet::get_server_homeID($coursehostname);
                   15883:                         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
                   15884:                         my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
                   15885:                         my %udomdefaults = &Apache::lonnet::get_domain_defaults($env{'user.domain'});
                   15886:                         my $remoterev = &Apache::lonnet::get_server_loncaparev($serverhomedom,$coursedomserver);
                   15887:                         my $canhost =
                   15888:                             &Apache::lonnet::can_host_session($env{'user.domain'},
                   15889:                                                               $coursedomserver,
                   15890:                                                               $remoterev,
                   15891:                                                               $udomdefaults{'remotesessions'},
                   15892:                                                               $defdomdefaults{'hostedsessions'});
                   15893: 
                   15894:                         if ($canhost) {
                   15895:                             $otherserver = $coursedomserver;
                   15896:                         } else {
                   15897:                             $warning = &mt('Requires LON-CAPA version [_1].',$env{'course.'.$courseid.'.internal.releaserequired'}).'<br />'. &mt("No suitable server could be found amongst servers in either your own domain or in the course's domain.");
                   15898:                         }
                   15899:                     } else {
                   15900:                         $warning = &mt('Requires LON-CAPA version [_1].',$env{'course.'.$courseid.'.internal.releaserequired'}).'<br />'.&mt("No suitable server could be found amongst servers in your own domain (which is also the course's domain).");
                   15901:                     }
                   15902:                 } else {
                   15903:                     $otherserver = $userdomserver;
                   15904:                 }
                   15905:             }
                   15906:             if ($otherserver ne '') {
                   15907:                 $switchserver = 'otherserver='.$otherserver.'&amp;role='.$rolecode;
                   15908:             }
                   15909:         }
                   15910:     }
                   15911:     return ($switchserver,$warning);
                   15912: }
                   15913: 
                   15914: =pod
                   15915: 
                   15916: =item * &check_release_result()
                   15917: 
                   15918: Inputs:
                   15919: 
                   15920: $switchwarning - Warning message if no suitable server found to host session.
                   15921: 
                   15922: $switchserver - query string to append to /adm/switchserver containing lonHostID
                   15923:                 and current role.
                   15924: 
                   15925: Returns: HTML to display with information about requirement to switch server.
                   15926:          Either displaying warning with link to Roles/Courses screen or
                   15927:          display link to switchserver.
                   15928: 
1.1181    raeburn  15929: =cut
                   15930: 
1.1207    raeburn  15931: sub check_release_result {
                   15932:     my ($switchwarning,$switchserver) = @_;
                   15933:     my $output = &start_page('Selected course unavailable on this server').
                   15934:                  '<p class="LC_warning">';
                   15935:     if ($switchwarning) {
                   15936:         $output .= $switchwarning.'<br /><a href="/adm/roles">';
                   15937:         if (&show_course()) {
                   15938:             $output .= &mt('Display courses');
                   15939:         } else {
                   15940:             $output .= &mt('Display roles');
                   15941:         }
                   15942:         $output .= '</a>';
                   15943:     } elsif ($switchserver) {
                   15944:         $output .= &mt('This course requires a newer version of LON-CAPA than is installed on this server.').
                   15945:                    '<br />'.
                   15946:                    '<a href="/adm/switchserver?'.$switchserver.'">'.
                   15947:                    &mt('Switch Server').
                   15948:                    '</a>';
                   15949:     }
                   15950:     $output .= '</p>'.&end_page();
                   15951:     return $output;
                   15952: }
                   15953: 
                   15954: =pod
                   15955: 
                   15956: =item * &needs_coursereinit()
                   15957: 
                   15958: Determine if course contents stored for user's session needs to be
                   15959: refreshed, because content has changed since "Big Hash" last tied.
                   15960: 
                   15961: Check for change is made if time last checked is more than 10 minutes ago
                   15962: (by default).
                   15963: 
                   15964: Inputs:
                   15965: 
                   15966: $loncaparev - Version on current server (format: Major.Minor.Subrelease-datestamp)
                   15967: 
                   15968: $interval (optional) - Time which may elapse (in s) between last check for content
                   15969:                        change in current course. (default: 600 s).  
                   15970: 
                   15971: Returns: an array; first element is:
                   15972: 
                   15973: =over 4
                   15974: 
                   15975: 'switch' - if content updates mean user's session
                   15976:            needs to be switched to a server running a newer LON-CAPA version
                   15977:  
                   15978: 'update' - if course session needs to be refreshed (i.e., Big Hash needs to be reloaded)
                   15979:            on current server hosting user's session                
                   15980: 
                   15981: ''       - if no action required.
                   15982: 
                   15983: =back
                   15984: 
                   15985: If first item element is 'switch':
                   15986: 
                   15987: second item is $switchwarning - Warning message if no suitable server found to host session. 
                   15988: 
                   15989: third item is $switchserver - query string to append to /adm/switchserver containing lonHostID
                   15990:                               and current role. 
                   15991: 
                   15992: otherwise: no other elements returned.
                   15993: 
                   15994: =back
                   15995: 
                   15996: =cut
                   15997: 
                   15998: sub needs_coursereinit {
                   15999:     my ($loncaparev,$interval) = @_;
                   16000:     return() unless ($env{'request.course.id'} && $env{'request.course.tied'});
                   16001:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   16002:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   16003:     my $now = time;
                   16004:     if ($interval eq '') {
                   16005:         $interval = 600;
                   16006:     }
                   16007:     if (($now-$env{'request.course.timechecked'})>$interval) {
                   16008:         my $lastchange = &Apache::lonnet::get_coursechange($cdom,$cnum);
                   16009:         &Apache::lonnet::appenv({'request.course.timechecked'=>$now});
                   16010:         if ($lastchange > $env{'request.course.tied'}) {
                   16011:             my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired');
                   16012:             if ($curr_reqd_hash{'internal.releaserequired'} ne '') {
                   16013:                 my $required = $env{'course.'.$cdom.'_'.$cnum.'.internal.releaserequired'};
                   16014:                 if ($curr_reqd_hash{'internal.releaserequired'} ne $required) {
                   16015:                     &Apache::lonnet::appenv({'course.'.$cdom.'_'.$cnum.'.internal.releaserequired' =>
                   16016:                                              $curr_reqd_hash{'internal.releaserequired'}});
                   16017:                     my ($switchserver,$switchwarning) =
                   16018:                         &check_release_required($loncaparev,$cdom.'_'.$cnum,$env{'request.role'},
                   16019:                                                 $curr_reqd_hash{'internal.releaserequired'});
                   16020:                     if ($switchwarning ne '' || $switchserver ne '') {
                   16021:                         return ('switch',$switchwarning,$switchserver);
                   16022:                     }
                   16023:                 }
                   16024:             }
                   16025:             return ('update');
                   16026:         }
                   16027:     }
                   16028:     return ();
                   16029: }
1.1181    raeburn  16030: 
1.1083    raeburn  16031: sub update_content_constraints {
                   16032:     my ($cdom,$cnum,$chome,$cid) = @_;
                   16033:     my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired');
                   16034:     my ($reqdmajor,$reqdminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
                   16035:     my %checkresponsetypes;
                   16036:     foreach my $key (keys(%Apache::lonnet::needsrelease)) {
1.1219    raeburn  16037:         my ($item,$name,$value,$valmatch) = split(/:/,$key);
1.1083    raeburn  16038:         if ($item eq 'resourcetag') {
                   16039:             if ($name eq 'responsetype') {
                   16040:                 $checkresponsetypes{$value} = $Apache::lonnet::needsrelease{$key}
                   16041:             }
                   16042:         }
                   16043:     }
                   16044:     my $navmap = Apache::lonnavmaps::navmap->new();
                   16045:     if (defined($navmap)) {
                   16046:         my %allresponses;
                   16047:         foreach my $res ($navmap->retrieveResources(undef,sub { $_[0]->is_problem() },1,0)) {
                   16048:             my %responses = $res->responseTypes();
                   16049:             foreach my $key (keys(%responses)) {
                   16050:                 next unless(exists($checkresponsetypes{$key}));
                   16051:                 $allresponses{$key} += $responses{$key};
                   16052:             }
                   16053:         }
                   16054:         foreach my $key (keys(%allresponses)) {
                   16055:             my ($major,$minor) = split(/\./,$checkresponsetypes{$key});
                   16056:             if (($major > $reqdmajor) || ($major == $reqdmajor && $minor > $reqdminor)) {
                   16057:                 ($reqdmajor,$reqdminor) = ($major,$minor);
                   16058:             }
                   16059:         }
                   16060:         undef($navmap);
                   16061:     }
                   16062:     unless (($reqdmajor eq '') && ($reqdminor eq '')) {
                   16063:         &Apache::lonnet::update_released_required($reqdmajor.'.'.$reqdminor,$cdom,$cnum,$chome,$cid);
                   16064:     }
                   16065:     return;
                   16066: }
                   16067: 
1.1110    raeburn  16068: sub allmaps_incourse {
                   16069:     my ($cdom,$cnum,$chome,$cid) = @_;
                   16070:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
                   16071:         $cid = $env{'request.course.id'};
                   16072:         $cdom = $env{'course.'.$cid.'.domain'};
                   16073:         $cnum = $env{'course.'.$cid.'.num'};
                   16074:         $chome = $env{'course.'.$cid.'.home'};
                   16075:     }
                   16076:     my %allmaps = ();
                   16077:     my $lastchange =
                   16078:         &Apache::lonnet::get_coursechange($cdom,$cnum);
                   16079:     if ($lastchange > $env{'request.course.tied'}) {
                   16080:         my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
                   16081:         unless ($ferr) {
                   16082:             &update_content_constraints($cdom,$cnum,$chome,$cid);
                   16083:         }
                   16084:     }
                   16085:     my $navmap = Apache::lonnavmaps::navmap->new();
                   16086:     if (defined($navmap)) {
                   16087:         foreach my $res ($navmap->retrieveResources(undef,sub { $_[0]->is_map() },1,0,1)) {
                   16088:             $allmaps{$res->src()} = 1;
                   16089:         }
                   16090:     }
                   16091:     return \%allmaps;
                   16092: }
                   16093: 
1.1083    raeburn  16094: sub parse_supplemental_title {
                   16095:     my ($title) = @_;
                   16096: 
                   16097:     my ($foldertitle,$renametitle);
                   16098:     if ($title =~ /&amp;&amp;&amp;/) {
                   16099:         $title = &HTML::Entites::decode($title);
                   16100:     }
                   16101:     if ($title =~ m/^(\d+)___&&&___($match_username)___&&&___($match_domain)___&&&___(.*)$/) {
                   16102:         $renametitle=$4;
                   16103:         my ($time,$uname,$udom) = ($1,$2,$3);
                   16104:         $foldertitle=&Apache::lontexconvert::msgtexconverted($4);
                   16105:         my $name =  &plainname($uname,$udom);
                   16106:         $name = &HTML::Entities::encode($name,'"<>&\'');
                   16107:         $renametitle = &HTML::Entities::encode($renametitle,'"<>&\'');
                   16108:         $title='<i>'.&Apache::lonlocal::locallocaltime($time).'</i> '.
                   16109:             $name.': <br />'.$foldertitle;
                   16110:     }
                   16111:     if (wantarray) {
                   16112:         return ($title,$foldertitle,$renametitle);
                   16113:     }
                   16114:     return $title;
                   16115: }
                   16116: 
1.1143    raeburn  16117: sub recurse_supplemental {
                   16118:     my ($cnum,$cdom,$suppmap,$numfiles,$errors) = @_;
                   16119:     if ($suppmap) {
                   16120:         my ($errtext,$fatal) = &LONCAPA::map::mapread('/uploaded/'.$cdom.'/'.$cnum.'/'.$suppmap);
                   16121:         if ($fatal) {
                   16122:             $errors ++;
                   16123:         } else {
                   16124:             if ($#LONCAPA::map::resources > 0) {
                   16125:                 foreach my $res (@LONCAPA::map::resources) {
                   16126:                     my ($title,$src,$ext,$type,$status)=split(/\:/,$res);
                   16127:                     if (($src ne '') && ($status eq 'res')) {
1.1146    raeburn  16128:                         if ($src =~ m{^\Q/uploaded/$cdom/$cnum/\E(supplemental_\d+\.sequence)$}) {
                   16129:                             ($numfiles,$errors) = &recurse_supplemental($cnum,$cdom,$1,$numfiles,$errors);
1.1143    raeburn  16130:                         } else {
                   16131:                             $numfiles ++;
                   16132:                         }
                   16133:                     }
                   16134:                 }
                   16135:             }
                   16136:         }
                   16137:     }
                   16138:     return ($numfiles,$errors);
                   16139: }
                   16140: 
1.1101    raeburn  16141: sub symb_to_docspath {
                   16142:     my ($symb) = @_;
                   16143:     return unless ($symb);
                   16144:     my ($mapurl,$id,$resurl) = &Apache::lonnet::decode_symb($symb);
                   16145:     if ($resurl=~/\.(sequence|page)$/) {
                   16146:         $mapurl=$resurl;
                   16147:     } elsif ($resurl eq 'adm/navmaps') {
                   16148:         $mapurl=$env{'course.'.$env{'request.course.id'}.'.url'};
                   16149:     }
                   16150:     my $mapresobj;
                   16151:     my $navmap = Apache::lonnavmaps::navmap->new();
                   16152:     if (ref($navmap)) {
                   16153:         $mapresobj = $navmap->getResourceByUrl($mapurl);
                   16154:     }
                   16155:     $mapurl=~s{^.*/([^/]+)\.(\w+)$}{$1};
                   16156:     my $type=$2;
                   16157:     my $path;
                   16158:     if (ref($mapresobj)) {
                   16159:         my $pcslist = $mapresobj->map_hierarchy();
                   16160:         if ($pcslist ne '') {
                   16161:             foreach my $pc (split(/,/,$pcslist)) {
                   16162:                 next if ($pc <= 1);
                   16163:                 my $res = $navmap->getByMapPc($pc);
                   16164:                 if (ref($res)) {
                   16165:                     my $thisurl = $res->src();
                   16166:                     $thisurl=~s{^.*/([^/]+)\.\w+$}{$1};
                   16167:                     my $thistitle = $res->title();
                   16168:                     $path .= '&'.
                   16169:                              &Apache::lonhtmlcommon::entity_encode($thisurl).'&'.
1.1146    raeburn  16170:                              &escape($thistitle).
1.1101    raeburn  16171:                              ':'.$res->randompick().
                   16172:                              ':'.$res->randomout().
                   16173:                              ':'.$res->encrypted().
                   16174:                              ':'.$res->randomorder().
                   16175:                              ':'.$res->is_page();
                   16176:                 }
                   16177:             }
                   16178:         }
                   16179:         $path =~ s/^\&//;
                   16180:         my $maptitle = $mapresobj->title();
                   16181:         if ($mapurl eq 'default') {
1.1129    raeburn  16182:             $maptitle = 'Main Content';
1.1101    raeburn  16183:         }
                   16184:         $path .= (($path ne '')? '&' : '').
                   16185:                  &Apache::lonhtmlcommon::entity_encode($mapurl).'&'.
1.1146    raeburn  16186:                  &escape($maptitle).
1.1101    raeburn  16187:                  ':'.$mapresobj->randompick().
                   16188:                  ':'.$mapresobj->randomout().
                   16189:                  ':'.$mapresobj->encrypted().
                   16190:                  ':'.$mapresobj->randomorder().
                   16191:                  ':'.$mapresobj->is_page();
                   16192:     } else {
                   16193:         my $maptitle = &Apache::lonnet::gettitle($mapurl);
                   16194:         my $ispage = (($type eq 'page')? 1 : '');
                   16195:         if ($mapurl eq 'default') {
1.1129    raeburn  16196:             $maptitle = 'Main Content';
1.1101    raeburn  16197:         }
                   16198:         $path = &Apache::lonhtmlcommon::entity_encode($mapurl).'&'.
1.1146    raeburn  16199:                 &escape($maptitle).':::::'.$ispage;
1.1101    raeburn  16200:     }
                   16201:     unless ($mapurl eq 'default') {
                   16202:         $path = 'default&'.
1.1146    raeburn  16203:                 &escape('Main Content').
1.1101    raeburn  16204:                 ':::::&'.$path;
                   16205:     }
                   16206:     return $path;
                   16207: }
                   16208: 
1.1094    raeburn  16209: sub captcha_display {
                   16210:     my ($context,$lonhost) = @_;
                   16211:     my ($output,$error);
                   16212:     my ($captcha,$pubkey,$privkey) = &get_captcha_config($context,$lonhost);
1.1095    raeburn  16213:     if ($captcha eq 'original') {
1.1094    raeburn  16214:         $output = &create_captcha();
                   16215:         unless ($output) {
1.1172    raeburn  16216:             $error = 'captcha';
1.1094    raeburn  16217:         }
                   16218:     } elsif ($captcha eq 'recaptcha') {
                   16219:         $output = &create_recaptcha($pubkey);
                   16220:         unless ($output) {
1.1172    raeburn  16221:             $error = 'recaptcha';
1.1094    raeburn  16222:         }
                   16223:     }
1.1176    raeburn  16224:     return ($output,$error,$captcha);
1.1094    raeburn  16225: }
                   16226: 
                   16227: sub captcha_response {
                   16228:     my ($context,$lonhost) = @_;
                   16229:     my ($captcha_chk,$captcha_error);
                   16230:     my ($captcha,$pubkey,$privkey) = &get_captcha_config($context,$lonhost);
1.1095    raeburn  16231:     if ($captcha eq 'original') {
1.1094    raeburn  16232:         ($captcha_chk,$captcha_error) = &check_captcha();
                   16233:     } elsif ($captcha eq 'recaptcha') {
                   16234:         $captcha_chk = &check_recaptcha($privkey);
                   16235:     } else {
                   16236:         $captcha_chk = 1;
                   16237:     }
                   16238:     return ($captcha_chk,$captcha_error);
                   16239: }
                   16240: 
                   16241: sub get_captcha_config {
                   16242:     my ($context,$lonhost) = @_;
1.1095    raeburn  16243:     my ($captcha,$pubkey,$privkey,$hashtocheck);
1.1094    raeburn  16244:     my $hostname = &Apache::lonnet::hostname($lonhost);
                   16245:     my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
                   16246:     my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
1.1095    raeburn  16247:     if ($context eq 'usercreation') {
                   16248:         my %domconfig = &Apache::lonnet::get_dom('configuration',[$context],$serverhomedom);
                   16249:         if (ref($domconfig{$context}) eq 'HASH') {
                   16250:             $hashtocheck = $domconfig{$context}{'cancreate'};
                   16251:             if (ref($hashtocheck) eq 'HASH') {
                   16252:                 if ($hashtocheck->{'captcha'} eq 'recaptcha') {
                   16253:                     if (ref($hashtocheck->{'recaptchakeys'}) eq 'HASH') {
                   16254:                         $pubkey = $hashtocheck->{'recaptchakeys'}{'public'};
                   16255:                         $privkey = $hashtocheck->{'recaptchakeys'}{'private'};
                   16256:                     }
                   16257:                     if ($privkey && $pubkey) {
                   16258:                         $captcha = 'recaptcha';
                   16259:                     } else {
                   16260:                         $captcha = 'original';
                   16261:                     }
                   16262:                 } elsif ($hashtocheck->{'captcha'} ne 'notused') {
                   16263:                     $captcha = 'original';
                   16264:                 }
1.1094    raeburn  16265:             }
1.1095    raeburn  16266:         } else {
                   16267:             $captcha = 'captcha';
                   16268:         }
                   16269:     } elsif ($context eq 'login') {
                   16270:         my %domconfhash = &Apache::loncommon::get_domainconf($serverhomedom);
                   16271:         if ($domconfhash{$serverhomedom.'.login.captcha'} eq 'recaptcha') {
                   16272:             $pubkey = $domconfhash{$serverhomedom.'.login.recaptchakeys_public'};
                   16273:             $privkey = $domconfhash{$serverhomedom.'.login.recaptchakeys_private'};
1.1094    raeburn  16274:             if ($privkey && $pubkey) {
                   16275:                 $captcha = 'recaptcha';
1.1095    raeburn  16276:             } else {
                   16277:                 $captcha = 'original';
1.1094    raeburn  16278:             }
1.1095    raeburn  16279:         } elsif ($domconfhash{$serverhomedom.'.login.captcha'} eq 'original') {
                   16280:             $captcha = 'original';
1.1094    raeburn  16281:         }
                   16282:     }
                   16283:     return ($captcha,$pubkey,$privkey);
                   16284: }
                   16285: 
                   16286: sub create_captcha {
                   16287:     my %captcha_params = &captcha_settings();
                   16288:     my ($output,$maxtries,$tries) = ('',10,0);
                   16289:     while ($tries < $maxtries) {
                   16290:         $tries ++;
                   16291:         my $captcha = Authen::Captcha->new (
                   16292:                                            output_folder => $captcha_params{'output_dir'},
                   16293:                                            data_folder   => $captcha_params{'db_dir'},
                   16294:                                           );
                   16295:         my $md5sum = $captcha->generate_code($captcha_params{'numchars'});
                   16296: 
                   16297:         if (-e $Apache::lonnet::perlvar{'lonCaptchaDir'}.'/'.$md5sum.'.png') {
                   16298:             $output = '<input type="hidden" name="crypt" value="'.$md5sum.'" />'."\n".
                   16299:                       &mt('Type in the letters/numbers shown below').'&nbsp;'.
1.1176    raeburn  16300:                       '<input type="text" size="5" name="code" value="" autocomplete="off" />'.
                   16301:                       '<br />'.
                   16302:                       '<img src="'.$captcha_params{'www_output_dir'}.'/'.$md5sum.'.png" alt="captcha" />';
1.1094    raeburn  16303:             last;
                   16304:         }
                   16305:     }
                   16306:     return $output;
                   16307: }
                   16308: 
                   16309: sub captcha_settings {
                   16310:     my %captcha_params = (
                   16311:                            output_dir     => $Apache::lonnet::perlvar{'lonCaptchaDir'},
                   16312:                            www_output_dir => "/captchaspool",
                   16313:                            db_dir         => $Apache::lonnet::perlvar{'lonCaptchaDb'},
                   16314:                            numchars       => '5',
                   16315:                          );
                   16316:     return %captcha_params;
                   16317: }
                   16318: 
                   16319: sub check_captcha {
                   16320:     my ($captcha_chk,$captcha_error);
                   16321:     my $code = $env{'form.code'};
                   16322:     my $md5sum = $env{'form.crypt'};
                   16323:     my %captcha_params = &captcha_settings();
                   16324:     my $captcha = Authen::Captcha->new(
                   16325:                       output_folder => $captcha_params{'output_dir'},
                   16326:                       data_folder   => $captcha_params{'db_dir'},
                   16327:                   );
1.1109    raeburn  16328:     $captcha_chk = $captcha->check_code($code,$md5sum);
1.1094    raeburn  16329:     my %captcha_hash = (
                   16330:                         0       => 'Code not checked (file error)',
                   16331:                        -1      => 'Failed: code expired',
                   16332:                        -2      => 'Failed: invalid code (not in database)',
                   16333:                        -3      => 'Failed: invalid code (code does not match crypt)',
                   16334:     );
                   16335:     if ($captcha_chk != 1) {
                   16336:         $captcha_error = $captcha_hash{$captcha_chk}
                   16337:     }
                   16338:     return ($captcha_chk,$captcha_error);
                   16339: }
                   16340: 
                   16341: sub create_recaptcha {
                   16342:     my ($pubkey) = @_;
1.1153    raeburn  16343:     my $use_ssl;
                   16344:     if ($ENV{'SERVER_PORT'} == 443) {
                   16345:         $use_ssl = 1;
                   16346:     }
1.1094    raeburn  16347:     my $captcha = Captcha::reCAPTCHA->new;
                   16348:     return $captcha->get_options_setter({theme => 'white'})."\n".
1.1153    raeburn  16349:            $captcha->get_html($pubkey,undef,$use_ssl).
1.1213    raeburn  16350:            &mt('If the text is hard to read, [_1] will replace them.',
1.1133    raeburn  16351:                '<img src="/res/adm/pages/refresh.gif" alt="reCAPTCHA refresh" />').
1.1094    raeburn  16352:            '<br /><br />';
                   16353: }
                   16354: 
                   16355: sub check_recaptcha {
                   16356:     my ($privkey) = @_;
                   16357:     my $captcha_chk;
                   16358:     my $captcha = Captcha::reCAPTCHA->new;
                   16359:     my $captcha_result =
                   16360:         $captcha->check_answer(
                   16361:                                 $privkey,
                   16362:                                 $ENV{'REMOTE_ADDR'},
                   16363:                                 $env{'form.recaptcha_challenge_field'},
                   16364:                                 $env{'form.recaptcha_response_field'},
                   16365:                               );
                   16366:     if ($captcha_result->{is_valid}) {
                   16367:         $captcha_chk = 1;
                   16368:     }
                   16369:     return $captcha_chk;
                   16370: }
                   16371: 
1.1174    raeburn  16372: sub emailusername_info {
1.1177    raeburn  16373:     my @fields = ('firstname','lastname','institution','web','location','officialemail');
1.1174    raeburn  16374:     my %titles = &Apache::lonlocal::texthash (
                   16375:                      lastname      => 'Last Name',
                   16376:                      firstname     => 'First Name',
                   16377:                      institution   => 'School/college/university',
                   16378:                      location      => "School's city, state/province, country",
                   16379:                      web           => "School's web address",
                   16380:                      officialemail => 'E-mail address at institution (if different)',
                   16381:                  );
                   16382:     return (\@fields,\%titles);
                   16383: }
                   16384: 
1.1161    raeburn  16385: sub cleanup_html {
                   16386:     my ($incoming) = @_;
                   16387:     my $outgoing;
                   16388:     if ($incoming ne '') {
                   16389:         $outgoing = $incoming;
                   16390:         $outgoing =~ s/;/&#059;/g;
                   16391:         $outgoing =~ s/\#/&#035;/g;
                   16392:         $outgoing =~ s/\&/&#038;/g;
                   16393:         $outgoing =~ s/</&#060;/g;
                   16394:         $outgoing =~ s/>/&#062;/g;
                   16395:         $outgoing =~ s/\(/&#040/g;
                   16396:         $outgoing =~ s/\)/&#041;/g;
                   16397:         $outgoing =~ s/"/&#034;/g;
                   16398:         $outgoing =~ s/'/&#039;/g;
                   16399:         $outgoing =~ s/\$/&#036;/g;
                   16400:         $outgoing =~ s{/}{&#047;}g;
                   16401:         $outgoing =~ s/=/&#061;/g;
                   16402:         $outgoing =~ s/\\/&#092;/g
                   16403:     }
                   16404:     return $outgoing;
                   16405: }
                   16406: 
1.1190    musolffc 16407: # Checks for critical messages and returns a redirect url if one exists.
                   16408: # $interval indicates how often to check for messages.
                   16409: sub critical_redirect {
                   16410:     my ($interval) = @_;
                   16411:     if ((time-$env{'user.criticalcheck.time'})>$interval) {
                   16412:         my @what=&Apache::lonnet::dump('critical', $env{'user.domain'}, 
                   16413:                                         $env{'user.name'});
                   16414:         &Apache::lonnet::appenv({'user.criticalcheck.time'=>time});
1.1191    raeburn  16415:         my $redirecturl;
1.1190    musolffc 16416:         if ($what[0]) {
                   16417: 	    if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
                   16418: 	        $redirecturl='/adm/email?critical=display';
1.1191    raeburn  16419: 	        my $url=&Apache::lonnet::absolute_url().$redirecturl;
                   16420:                 return (1, $url);
1.1190    musolffc 16421:             }
1.1191    raeburn  16422:         }
                   16423:     } 
                   16424:     return ();
1.1190    musolffc 16425: }
                   16426: 
1.1174    raeburn  16427: # Use:
                   16428: #   my $answer=reply("encrypt:passwd:$udom:$uname:$upass",$tryserver);
                   16429: #
                   16430: ##################################################
                   16431: #          password associated functions         #
                   16432: ##################################################
                   16433: sub des_keys {
                   16434:     # Make a new key for DES encryption.
                   16435:     # Each key has two parts which are returned separately.
                   16436:     # Please note:  Each key must be passed through the &hex function
                   16437:     # before it is output to the web browser.  The hex versions cannot
                   16438:     # be used to decrypt.
                   16439:     my @hexstr=('0','1','2','3','4','5','6','7',
                   16440:                 '8','9','a','b','c','d','e','f');
                   16441:     my $lkey='';
                   16442:     for (0..7) {
                   16443:         $lkey.=$hexstr[rand(15)];
                   16444:     }
                   16445:     my $ukey='';
                   16446:     for (0..7) {
                   16447:         $ukey.=$hexstr[rand(15)];
                   16448:     }
                   16449:     return ($lkey,$ukey);
                   16450: }
                   16451: 
                   16452: sub des_decrypt {
                   16453:     my ($key,$cyphertext) = @_;
                   16454:     my $keybin=pack("H16",$key);
                   16455:     my $cypher;
                   16456:     if ($Crypt::DES::VERSION>=2.03) {
                   16457:         $cypher=new Crypt::DES $keybin;
                   16458:     } else {
                   16459:         $cypher=new DES $keybin;
                   16460:     }
                   16461:     my $plaintext=
                   16462:         $cypher->decrypt(unpack("a8",pack("H16",substr($cyphertext,0,16))));
                   16463:     $plaintext.=
                   16464:         $cypher->decrypt(unpack("a8",pack("H16",substr($cyphertext,16,16))));
                   16465:     $plaintext=substr($plaintext,1,ord(substr($plaintext,0,1)) );
                   16466:     return $plaintext;
                   16467: }
                   16468: 
1.112     bowersj2 16469: 1;
                   16470: __END__;
1.41      ng       16471: 

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