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

1.10      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # a pile of common routines
1.10      albertel    3: #
1.1060  ! bisitz      4: # $Id: loncommon.pm,v 1.1059 2012/03/17 20:11:26 raeburn Exp $
1.10      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.1       albertel   28: 
                     29: # Makes a table out of the previous attempts
1.2       albertel   30: # Inputs result_from_symbread, user, domain, course_id
1.16      harris41   31: # Reads in non-network-related .tab files
1.1       albertel   32: 
1.35      matthew    33: # POD header:
                     34: 
1.45      matthew    35: =pod
                     36: 
1.35      matthew    37: =head1 NAME
                     38: 
                     39: Apache::loncommon - pile of common routines
                     40: 
                     41: =head1 SYNOPSIS
                     42: 
1.112     bowersj2   43: Common routines for manipulating connections, student answers,
                     44:     domains, common Javascript fragments, etc.
1.35      matthew    45: 
1.112     bowersj2   46: =head1 OVERVIEW
1.35      matthew    47: 
1.112     bowersj2   48: A collection of commonly used subroutines that don't have a natural
                     49: home anywhere else. This collection helps remove
1.35      matthew    50: redundancy from other modules and increase efficiency of memory usage.
                     51: 
                     52: =cut 
                     53: 
                     54: # End of POD header
1.1       albertel   55: package Apache::loncommon;
                     56: 
                     57: use strict;
1.258     albertel   58: use Apache::lonnet;
1.46      matthew    59: use GDBM_File;
1.51      www        60: use POSIX qw(strftime mktime);
1.82      www        61: use Apache::lonmenu();
1.498     albertel   62: use Apache::lonenc();
1.117     www        63: use Apache::lonlocal;
1.685     tempelho   64: use Apache::lonnet();
1.139     matthew    65: use HTML::Entities;
1.334     albertel   66: use Apache::lonhtmlcommon();
                     67: use Apache::loncoursedata();
1.344     albertel   68: use Apache::lontexconvert();
1.444     albertel   69: use Apache::lonclonecourse();
1.479     albertel   70: use LONCAPA qw(:DEFAULT :match);
1.657     raeburn    71: use DateTime::TimeZone;
1.687     raeburn    72: use DateTime::Locale::Catalog;
1.117     www        73: 
1.517     raeburn    74: # ---------------------------------------------- Designs
                     75: use vars qw(%defaultdesign);
                     76: 
1.22      www        77: my $readit;
                     78: 
1.517     raeburn    79: 
1.157     matthew    80: ##
                     81: ## Global Variables
                     82: ##
1.46      matthew    83: 
1.643     foxr       84: 
                     85: # ----------------------------------------------- SSI with retries:
                     86: #
                     87: 
                     88: =pod
                     89: 
1.648     raeburn    90: =head1 Server Side include with retries:
1.643     foxr       91: 
                     92: =over 4
                     93: 
1.648     raeburn    94: =item * &ssi_with_retries(resource,retries form)
1.643     foxr       95: 
                     96: Performs an ssi with some number of retries.  Retries continue either
                     97: until the result is ok or until the retry count supplied by the
                     98: caller is exhausted.  
                     99: 
                    100: Inputs:
1.648     raeburn   101: 
                    102: =over 4
                    103: 
1.643     foxr      104: resource   - Identifies the resource to insert.
1.648     raeburn   105: 
1.643     foxr      106: retries    - Count of the number of retries allowed.
1.648     raeburn   107: 
1.643     foxr      108: form       - Hash that identifies the rendering options.
                    109: 
1.648     raeburn   110: =back
                    111: 
                    112: Returns:
                    113: 
                    114: =over 4
                    115: 
1.643     foxr      116: content    - The content of the response.  If retries were exhausted this is empty.
1.648     raeburn   117: 
1.643     foxr      118: response   - The response from the last attempt (which may or may not have been successful.
                    119: 
1.648     raeburn   120: =back
                    121: 
                    122: =back
                    123: 
1.643     foxr      124: =cut
                    125: 
                    126: sub ssi_with_retries {
                    127:     my ($resource, $retries, %form) = @_;
                    128: 
                    129: 
                    130:     my $ok = 0;			# True if we got a good response.
                    131:     my $content;
                    132:     my $response;
                    133: 
                    134:     # Try to get the ssi done. within the retries count:
                    135: 
                    136:     do {
                    137: 	($content, $response) = &Apache::lonnet::ssi($resource, %form);
                    138: 	$ok      = $response->is_success;
1.650     www       139:         if (!$ok) {
                    140:             &Apache::lonnet::logthis("Failed ssi_with_retries on $resource: ".$response->is_success.', '.$response->code.', '.$response->message);
                    141:         }
1.643     foxr      142: 	$retries--;
                    143:     } while (!$ok && ($retries > 0));
                    144: 
                    145:     if (!$ok) {
                    146: 	$content = '';		# On error return an empty content.
                    147:     }
                    148:     return ($content, $response);
                    149: 
                    150: }
                    151: 
                    152: 
                    153: 
1.20      www       154: # ----------------------------------------------- Filetypes/Languages/Copyright
1.12      harris41  155: my %language;
1.124     www       156: my %supported_language;
1.1048    foxr      157: my %latex_language;		# For choosing hyphenation in <transl..>
                    158: my %latex_language_bykey;	# for choosing hyphenation from metadata
1.12      harris41  159: my %cprtag;
1.192     taceyjo1  160: my %scprtag;
1.351     www       161: my %fe; my %fd; my %fm;
1.41      ng        162: my %category_extensions;
1.12      harris41  163: 
1.46      matthew   164: # ---------------------------------------------- Thesaurus variables
1.144     matthew   165: #
                    166: # %Keywords:
                    167: #      A hash used by &keyword to determine if a word is considered a keyword.
                    168: # $thesaurus_db_file 
                    169: #      Scalar containing the full path to the thesaurus database.
1.46      matthew   170: 
                    171: my %Keywords;
                    172: my $thesaurus_db_file;
                    173: 
1.144     matthew   174: #
                    175: # Initialize values from language.tab, copyright.tab, filetypes.tab,
                    176: # thesaurus.tab, and filecategories.tab.
                    177: #
1.18      www       178: BEGIN {
1.46      matthew   179:     # Variable initialization
                    180:     $thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
                    181:     #
1.22      www       182:     unless ($readit) {
1.12      harris41  183: # ------------------------------------------------------------------- languages
                    184:     {
1.158     raeburn   185:         my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    186:                                    '/language.tab';
                    187:         if ( open(my $fh,"<$langtabfile") ) {
1.356     albertel  188:             while (my $line = <$fh>) {
                    189:                 next if ($line=~/^\#/);
                    190:                 chomp($line);
1.1048    foxr      191:                 my ($key,$two,$country,$three,$enc,$val,$sup,$latex)=(split(/\t/,$line));
1.158     raeburn   192:                 $language{$key}=$val.' - '.$enc;
                    193:                 if ($sup) {
                    194:                     $supported_language{$key}=$sup;
                    195:                 }
1.1048    foxr      196: 		if ($latex) {
                    197: 		    $latex_language_bykey{$key} = $latex;
                    198: 		    $latex_language{$two} = $latex;
                    199: 		}
1.158     raeburn   200:             }
                    201:             close($fh);
                    202:         }
1.12      harris41  203:     }
                    204: # ------------------------------------------------------------------ copyrights
                    205:     {
1.158     raeburn   206:         my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    207:                                   '/copyright.tab';
                    208:         if ( open (my $fh,"<$copyrightfile") ) {
1.356     albertel  209:             while (my $line = <$fh>) {
                    210:                 next if ($line=~/^\#/);
                    211:                 chomp($line);
                    212:                 my ($key,$val)=(split(/\s+/,$line,2));
1.158     raeburn   213:                 $cprtag{$key}=$val;
                    214:             }
                    215:             close($fh);
                    216:         }
1.12      harris41  217:     }
1.351     www       218: # ----------------------------------------------------------- source copyrights
1.192     taceyjo1  219:     {
                    220:         my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    221:                                   '/source_copyright.tab';
                    222:         if ( open (my $fh,"<$sourcecopyrightfile") ) {
1.356     albertel  223:             while (my $line = <$fh>) {
                    224:                 next if ($line =~ /^\#/);
                    225:                 chomp($line);
                    226:                 my ($key,$val)=(split(/\s+/,$line,2));
1.192     taceyjo1  227:                 $scprtag{$key}=$val;
                    228:             }
                    229:             close($fh);
                    230:         }
                    231:     }
1.63      www       232: 
1.517     raeburn   233: # -------------------------------------------------------------- default domain designs
1.63      www       234:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
1.517     raeburn   235:     my $designfile = $designdir.'/default.tab';
                    236:     if ( open (my $fh,"<$designfile") ) {
                    237:         while (my $line = <$fh>) {
                    238:             next if ($line =~ /^\#/);
                    239:             chomp($line);
                    240:             my ($key,$val)=(split(/\=/,$line));
                    241:             if ($val) { $defaultdesign{$key}=$val; }
                    242:         }
                    243:         close($fh);
1.63      www       244:     }
                    245: 
1.15      harris41  246: # ------------------------------------------------------------- file categories
                    247:     {
1.158     raeburn   248:         my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    249:                                   '/filecategories.tab';
                    250:         if ( open (my $fh,"<$categoryfile") ) {
1.356     albertel  251: 	    while (my $line = <$fh>) {
                    252: 		next if ($line =~ /^\#/);
                    253: 		chomp($line);
                    254:                 my ($extension,$category)=(split(/\s+/,$line,2));
1.158     raeburn   255:                 push @{$category_extensions{lc($category)}},$extension;
                    256:             }
                    257:             close($fh);
                    258:         }
                    259: 
1.15      harris41  260:     }
1.12      harris41  261: # ------------------------------------------------------------------ file types
                    262:     {
1.158     raeburn   263:         my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    264:                '/filetypes.tab';
                    265:         if ( open (my $fh,"<$typesfile") ) {
1.356     albertel  266:             while (my $line = <$fh>) {
                    267: 		next if ($line =~ /^\#/);
                    268: 		chomp($line);
                    269:                 my ($ending,$emb,$mime,$descr)=split(/\s+/,$line,4);
1.158     raeburn   270:                 if ($descr ne '') {
                    271:                     $fe{$ending}=lc($emb);
                    272:                     $fd{$ending}=$descr;
1.351     www       273:                     if ($mime ne 'unk') { $fm{$ending}=$mime; }
1.158     raeburn   274:                 }
                    275:             }
                    276:             close($fh);
                    277:         }
1.12      harris41  278:     }
1.22      www       279:     &Apache::lonnet::logthis(
1.705     tempelho  280:              "<span style='color:yellow;'>INFO: Read file types</span>");
1.22      www       281:     $readit=1;
1.46      matthew   282:     }  # end of unless($readit) 
1.32      matthew   283:     
                    284: }
1.112     bowersj2  285: 
1.42      matthew   286: ###############################################################
                    287: ##           HTML and Javascript Helper Functions            ##
                    288: ###############################################################
                    289: 
                    290: =pod 
                    291: 
1.112     bowersj2  292: =head1 HTML and Javascript Functions
1.42      matthew   293: 
1.112     bowersj2  294: =over 4
                    295: 
1.648     raeburn   296: =item * &browser_and_searcher_javascript()
1.112     bowersj2  297: 
                    298: X<browsing, javascript>X<searching, javascript>Returns a string
                    299: containing javascript with two functions, C<openbrowser> and
                    300: C<opensearcher>. Returned string does not contain E<lt>scriptE<gt>
                    301: tags.
1.42      matthew   302: 
1.648     raeburn   303: =item * &openbrowser(formname,elementname,only,omit) [javascript]
1.42      matthew   304: 
                    305: inputs: formname, elementname, only, omit
                    306: 
                    307: formname and elementname indicate the name of the html form and name of
                    308: the element that the results of the browsing selection are to be placed in. 
                    309: 
                    310: Specifying 'only' will restrict the browser to displaying only files
1.185     www       311: with the given extension.  Can be a comma separated list.
1.42      matthew   312: 
                    313: Specifying 'omit' will restrict the browser to NOT displaying files
1.185     www       314: with the given extension.  Can be a comma separated list.
1.42      matthew   315: 
1.648     raeburn   316: =item * &opensearcher(formname,elementname) [javascript]
1.42      matthew   317: 
                    318: Inputs: formname, elementname
                    319: 
                    320: formname and elementname specify the name of the html form and the name
                    321: of the element the selection from the search results will be placed in.
1.542     raeburn   322: 
1.42      matthew   323: =cut
                    324: 
                    325: sub browser_and_searcher_javascript {
1.199     albertel  326:     my ($mode)=@_;
                    327:     if (!defined($mode)) { $mode='edit'; }
1.453     albertel  328:     my $resurl=&escape_single(&lastresurl());
1.42      matthew   329:     return <<END;
1.219     albertel  330: // <!-- BEGIN LON-CAPA Internal
1.50      matthew   331:     var editbrowser = null;
1.135     albertel  332:     function openbrowser(formname,elementname,only,omit,titleelement) {
1.170     www       333:         var url = '$resurl/?';
1.42      matthew   334:         if (editbrowser == null) {
                    335:             url += 'launch=1&';
                    336:         }
                    337:         url += 'catalogmode=interactive&';
1.199     albertel  338:         url += 'mode=$mode&';
1.611     albertel  339:         url += 'inhibitmenu=yes&';
1.42      matthew   340:         url += 'form=' + formname + '&';
                    341:         if (only != null) {
                    342:             url += 'only=' + only + '&';
1.217     albertel  343:         } else {
                    344:             url += 'only=&';
                    345: 	}
1.42      matthew   346:         if (omit != null) {
                    347:             url += 'omit=' + omit + '&';
1.217     albertel  348:         } else {
                    349:             url += 'omit=&';
                    350: 	}
1.135     albertel  351:         if (titleelement != null) {
                    352:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  353:         } else {
                    354: 	    url += 'titleelement=&';
                    355: 	}
1.42      matthew   356:         url += 'element=' + elementname + '';
                    357:         var title = 'Browser';
1.435     albertel  358:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
1.42      matthew   359:         options += ',width=700,height=600';
                    360:         editbrowser = open(url,title,options,'1');
                    361:         editbrowser.focus();
                    362:     }
                    363:     var editsearcher;
1.135     albertel  364:     function opensearcher(formname,elementname,titleelement) {
1.42      matthew   365:         var url = '/adm/searchcat?';
                    366:         if (editsearcher == null) {
                    367:             url += 'launch=1&';
                    368:         }
                    369:         url += 'catalogmode=interactive&';
1.199     albertel  370:         url += 'mode=$mode&';
1.42      matthew   371:         url += 'form=' + formname + '&';
1.135     albertel  372:         if (titleelement != null) {
                    373:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  374:         } else {
                    375: 	    url += 'titleelement=&';
                    376: 	}
1.42      matthew   377:         url += 'element=' + elementname + '';
                    378:         var title = 'Search';
1.435     albertel  379:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
1.42      matthew   380:         options += ',width=700,height=600';
                    381:         editsearcher = open(url,title,options,'1');
                    382:         editsearcher.focus();
                    383:     }
1.219     albertel  384: // END LON-CAPA Internal -->
1.42      matthew   385: END
1.170     www       386: }
                    387: 
                    388: sub lastresurl {
1.258     albertel  389:     if ($env{'environment.lastresurl'}) {
                    390: 	return $env{'environment.lastresurl'}
1.170     www       391:     } else {
                    392: 	return '/res';
                    393:     }
                    394: }
                    395: 
                    396: sub storeresurl {
                    397:     my $resurl=&Apache::lonnet::clutter(shift);
                    398:     unless ($resurl=~/^\/res/) { return 0; }
                    399:     $resurl=~s/\/$//;
                    400:     &Apache::lonnet::put('environment',{'lastresurl' => $resurl});
1.646     raeburn   401:     &Apache::lonnet::appenv({'environment.lastresurl' => $resurl});
1.170     www       402:     return 1;
1.42      matthew   403: }
                    404: 
1.74      www       405: sub studentbrowser_javascript {
1.111     www       406:    unless (
1.258     albertel  407:             (($env{'request.course.id'}) && 
1.302     albertel  408:              (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    409: 	      || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    410: 					  '/'.$env{'request.course.sec'})
                    411: 	      ))
1.258     albertel  412:          || ($env{'request.role'}=~/^(au|dc|su)/)
1.111     www       413:           ) { return ''; }  
1.74      www       414:    return (<<'ENDSTDBRW');
1.776     bisitz    415: <script type="text/javascript" language="Javascript">
1.824     bisitz    416: // <![CDATA[
1.74      www       417:     var stdeditbrowser;
1.999     www       418:     function openstdbrowser(formname,uname,udom,clicker,roleflag,ignorefilter,courseadvonly) {
1.74      www       419:         var url = '/adm/pickstudent?';
                    420:         var filter;
1.558     albertel  421: 	if (!ignorefilter) {
                    422: 	    eval('filter=document.'+formname+'.'+uname+'.value;');
                    423: 	}
1.74      www       424:         if (filter != null) {
                    425:            if (filter != '') {
                    426:                url += 'filter='+filter+'&';
                    427: 	   }
                    428:         }
                    429:         url += 'form=' + formname + '&unameelement='+uname+
1.999     www       430:                                     '&udomelement='+udom+
                    431:                                     '&clicker='+clicker;
1.111     www       432: 	if (roleflag) { url+="&roles=1"; }
1.793     raeburn   433:         if (courseadvonly) { url+="&courseadvonly=1"; }
1.102     www       434:         var title = 'Student_Browser';
1.74      www       435:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    436:         options += ',width=700,height=600';
                    437:         stdeditbrowser = open(url,title,options,'1');
                    438:         stdeditbrowser.focus();
                    439:     }
1.824     bisitz    440: // ]]>
1.74      www       441: </script>
                    442: ENDSTDBRW
                    443: }
1.42      matthew   444: 
1.1003    www       445: sub resourcebrowser_javascript {
                    446:    unless ($env{'request.course.id'}) { return ''; }
1.1004    www       447:    return (<<'ENDRESBRW');
1.1003    www       448: <script type="text/javascript" language="Javascript">
                    449: // <![CDATA[
                    450:     var reseditbrowser;
1.1004    www       451:     function openresbrowser(formname,reslink) {
1.1005    www       452:         var url = '/adm/pickresource?form='+formname+'&reslink='+reslink;
1.1003    www       453:         var title = 'Resource_Browser';
                    454:         var options = 'scrollbars=1,resizable=1,menubar=0';
1.1005    www       455:         options += ',width=700,height=500';
1.1004    www       456:         reseditbrowser = open(url,title,options,'1');
                    457:         reseditbrowser.focus();
1.1003    www       458:     }
                    459: // ]]>
                    460: </script>
1.1004    www       461: ENDRESBRW
1.1003    www       462: }
                    463: 
1.74      www       464: sub selectstudent_link {
1.999     www       465:    my ($form,$unameele,$udomele,$courseadvonly,$clickerid)=@_;
                    466:    my $callargs = "'".&Apache::lonhtmlcommon::entity_encode($form)."','".
                    467:                       &Apache::lonhtmlcommon::entity_encode($unameele)."','".
                    468:                       &Apache::lonhtmlcommon::entity_encode($udomele)."'";
1.258     albertel  469:    if ($env{'request.course.id'}) {  
1.302     albertel  470:        if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    471: 	   && !&Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    472: 					'/'.$env{'request.course.sec'})) {
1.111     www       473: 	   return '';
                    474:        }
1.999     www       475:        $callargs.=",'".&Apache::lonhtmlcommon::entity_encode($clickerid)."'";
1.793     raeburn   476:        if ($courseadvonly)  {
                    477:            $callargs .= ",'',1,1";
                    478:        }
                    479:        return '<span class="LC_nobreak">'.
                    480:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
                    481:               &mt('Select User').'</a></span>';
1.74      www       482:    }
1.258     albertel  483:    if ($env{'request.role'}=~/^(au|dc|su)/) {
1.1012    www       484:        $callargs .= ",'',1"; 
1.793     raeburn   485:        return '<span class="LC_nobreak">'.
                    486:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
                    487:               &mt('Select User').'</a></span>';
1.111     www       488:    }
                    489:    return '';
1.91      www       490: }
                    491: 
1.1004    www       492: sub selectresource_link {
                    493:    my ($form,$reslink,$arg)=@_;
                    494:    
                    495:    my $callargs = "'".&Apache::lonhtmlcommon::entity_encode($form)."','".
                    496:                       &Apache::lonhtmlcommon::entity_encode($reslink)."'";
                    497:    unless ($env{'request.course.id'}) { return $arg; }
                    498:    return '<span class="LC_nobreak">'.
                    499:               '<a href="javascript:openresbrowser('.$callargs.');">'.
                    500:               $arg.'</a></span>';
                    501: }
                    502: 
                    503: 
                    504: 
1.653     raeburn   505: sub authorbrowser_javascript {
                    506:     return <<"ENDAUTHORBRW";
1.776     bisitz    507: <script type="text/javascript" language="JavaScript">
1.824     bisitz    508: // <![CDATA[
1.653     raeburn   509: var stdeditbrowser;
                    510: 
                    511: function openauthorbrowser(formname,udom) {
                    512:     var url = '/adm/pickauthor?';
                    513:     url += 'form='+formname+'&roledom='+udom;
                    514:     var title = 'Author_Browser';
                    515:     var options = 'scrollbars=1,resizable=1,menubar=0';
                    516:     options += ',width=700,height=600';
                    517:     stdeditbrowser = open(url,title,options,'1');
                    518:     stdeditbrowser.focus();
                    519: }
                    520: 
1.824     bisitz    521: // ]]>
1.653     raeburn   522: </script>
                    523: ENDAUTHORBRW
                    524: }
                    525: 
1.91      www       526: sub coursebrowser_javascript {
1.909     raeburn   527:     my ($domainfilter,$sec_element,$formname,$role_element,$crstype) = @_;
1.932     raeburn   528:     my $wintitle = 'Course_Browser';
1.931     raeburn   529:     if ($crstype eq 'Community') {
1.932     raeburn   530:         $wintitle = 'Community_Browser';
1.909     raeburn   531:     }
1.876     raeburn   532:     my $id_functions = &javascript_index_functions();
                    533:     my $output = '
1.776     bisitz    534: <script type="text/javascript" language="JavaScript">
1.824     bisitz    535: // <![CDATA[
1.468     raeburn   536:     var stdeditbrowser;'."\n";
1.876     raeburn   537: 
                    538:     $output .= <<"ENDSTDBRW";
1.909     raeburn   539:     function opencrsbrowser(formname,uname,udom,desc,extra_element,multflag,type,type_elem) {
1.91      www       540:         var url = '/adm/pickcourse?';
1.895     raeburn   541:         var formid = getFormIdByName(formname);
1.876     raeburn   542:         var domainfilter = getDomainFromSelectbox(formname,udom);
1.128     albertel  543:         if (domainfilter != null) {
                    544:            if (domainfilter != '') {
                    545:                url += 'domainfilter='+domainfilter+'&';
                    546: 	   }
                    547:         }
1.91      www       548:         url += 'form=' + formname + '&cnumelement='+uname+
1.187     albertel  549: 	                            '&cdomelement='+udom+
                    550:                                     '&cnameelement='+desc;
1.468     raeburn   551:         if (extra_element !=null && extra_element != '') {
1.594     raeburn   552:             if (formname == 'rolechoice' || formname == 'studentform') {
1.468     raeburn   553:                 url += '&roleelement='+extra_element;
                    554:                 if (domainfilter == null || domainfilter == '') {
                    555:                     url += '&domainfilter='+extra_element;
                    556:                 }
1.234     raeburn   557:             }
1.468     raeburn   558:             else {
                    559:                 if (formname == 'portform') {
                    560:                     url += '&setroles='+extra_element;
1.800     raeburn   561:                 } else {
                    562:                     if (formname == 'rules') {
                    563:                         url += '&fixeddom='+extra_element; 
                    564:                     }
1.468     raeburn   565:                 }
                    566:             }     
1.230     raeburn   567:         }
1.909     raeburn   568:         if (type != null && type != '') {
                    569:             url += '&type='+type;
                    570:         }
                    571:         if (type_elem != null && type_elem != '') {
                    572:             url += '&typeelement='+type_elem;
                    573:         }
1.872     raeburn   574:         if (formname == 'ccrs') {
                    575:             var ownername = document.forms[formid].ccuname.value;
                    576:             var ownerdom =  document.forms[formid].ccdomain.options[document.forms[formid].ccdomain.selectedIndex].value;
                    577:             url += '&cloner='+ownername+':'+ownerdom;
                    578:         }
1.293     raeburn   579:         if (multflag !=null && multflag != '') {
                    580:             url += '&multiple='+multflag;
                    581:         }
1.909     raeburn   582:         var title = '$wintitle';
1.91      www       583:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    584:         options += ',width=700,height=600';
                    585:         stdeditbrowser = open(url,title,options,'1');
                    586:         stdeditbrowser.focus();
                    587:     }
1.876     raeburn   588: $id_functions
                    589: ENDSTDBRW
1.905     raeburn   590:     if (($sec_element ne '') || ($role_element ne '')) {
                    591:         $output .= &setsec_javascript($sec_element,$formname,$role_element);
1.876     raeburn   592:     }
                    593:     $output .= '
                    594: // ]]>
                    595: </script>';
                    596:     return $output;
                    597: }
                    598: 
                    599: sub javascript_index_functions {
                    600:     return <<"ENDJS";
                    601: 
                    602: function getFormIdByName(formname) {
                    603:     for (var i=0;i<document.forms.length;i++) {
                    604:         if (document.forms[i].name == formname) {
                    605:             return i;
                    606:         }
                    607:     }
                    608:     return -1;
                    609: }
                    610: 
                    611: function getIndexByName(formid,item) {
                    612:     for (var i=0;i<document.forms[formid].elements.length;i++) {
                    613:         if (document.forms[formid].elements[i].name == item) {
                    614:             return i;
                    615:         }
                    616:     }
                    617:     return -1;
                    618: }
1.468     raeburn   619: 
1.876     raeburn   620: function getDomainFromSelectbox(formname,udom) {
                    621:     var userdom;
                    622:     var formid = getFormIdByName(formname);
                    623:     if (formid > -1) {
                    624:         var domid = getIndexByName(formid,udom);
                    625:         if (domid > -1) {
                    626:             if (document.forms[formid].elements[domid].type == 'select-one') {
                    627:                 userdom=document.forms[formid].elements[domid].options[document.forms[formid].elements[domid].selectedIndex].value;
                    628:             }
                    629:             if (document.forms[formid].elements[domid].type == 'hidden') {
                    630:                 userdom=document.forms[formid].elements[domid].value;
1.468     raeburn   631:             }
                    632:         }
                    633:     }
1.876     raeburn   634:     return userdom;
                    635: }
                    636: 
                    637: ENDJS
1.468     raeburn   638: 
1.876     raeburn   639: }
                    640: 
1.1017    raeburn   641: sub javascript_array_indexof {
1.1018    raeburn   642:     return <<ENDJS;
1.1017    raeburn   643: <script type="text/javascript" language="JavaScript">
                    644: // <![CDATA[
                    645: 
                    646: if (!Array.prototype.indexOf) {
                    647:     Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
                    648:         "use strict";
                    649:         if (this === void 0 || this === null) {
                    650:             throw new TypeError();
                    651:         }
                    652:         var t = Object(this);
                    653:         var len = t.length >>> 0;
                    654:         if (len === 0) {
                    655:             return -1;
                    656:         }
                    657:         var n = 0;
                    658:         if (arguments.length > 0) {
                    659:             n = Number(arguments[1]);
                    660:             if (n !== n) { // shortcut for verifying if it's NaN
                    661:                 n = 0;
                    662:             } else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
                    663:                 n = (n > 0 || -1) * Math.floor(Math.abs(n));
                    664:             }
                    665:         }
                    666:         if (n >= len) {
                    667:             return -1;
                    668:         }
                    669:         var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
                    670:         for (; k < len; k++) {
                    671:             if (k in t && t[k] === searchElement) {
                    672:                 return k;
                    673:             }
                    674:         }
                    675:         return -1;
                    676:     }
                    677: }
                    678: 
                    679: // ]]>
                    680: </script>
                    681: 
                    682: ENDJS
                    683: 
                    684: }
                    685: 
1.876     raeburn   686: sub userbrowser_javascript {
                    687:     my $id_functions = &javascript_index_functions();
                    688:     return <<"ENDUSERBRW";
                    689: 
1.888     raeburn   690: function openuserbrowser(formname,uname,udom,ulast,ufirst,uemail,hideudom,crsdom,caller) {
1.876     raeburn   691:     var url = '/adm/pickuser?';
                    692:     var userdom = getDomainFromSelectbox(formname,udom);
                    693:     if (userdom != null) {
                    694:        if (userdom != '') {
                    695:            url += 'srchdom='+userdom+'&';
                    696:        }
                    697:     }
                    698:     url += 'form=' + formname + '&unameelement='+uname+
                    699:                                 '&udomelement='+udom+
                    700:                                 '&ulastelement='+ulast+
                    701:                                 '&ufirstelement='+ufirst+
                    702:                                 '&uemailelement='+uemail+
1.881     raeburn   703:                                 '&hideudomelement='+hideudom+
                    704:                                 '&coursedom='+crsdom;
1.888     raeburn   705:     if ((caller != null) && (caller != undefined)) {
                    706:         url += '&caller='+caller;
                    707:     }
1.876     raeburn   708:     var title = 'User_Browser';
                    709:     var options = 'scrollbars=1,resizable=1,menubar=0';
                    710:     options += ',width=700,height=600';
                    711:     var stdeditbrowser = open(url,title,options,'1');
                    712:     stdeditbrowser.focus();
                    713: }
                    714: 
1.888     raeburn   715: function fix_domain (formname,udom,origdom,uname) {
1.876     raeburn   716:     var formid = getFormIdByName(formname);
                    717:     if (formid > -1) {
1.888     raeburn   718:         var unameid = getIndexByName(formid,uname);
1.876     raeburn   719:         var domid = getIndexByName(formid,udom);
                    720:         var hidedomid = getIndexByName(formid,origdom);
                    721:         if (hidedomid > -1) {
                    722:             var fixeddom = document.forms[formid].elements[hidedomid].value;
1.888     raeburn   723:             var unameval = document.forms[formid].elements[unameid].value;
                    724:             if ((fixeddom != '') && (fixeddom != undefined) && (fixeddom != null) && (unameval != '') && (unameval != undefined) && (unameval != null)) {
                    725:                 if (domid > -1) {
                    726:                     var slct = document.forms[formid].elements[domid];
                    727:                     if (slct.type == 'select-one') {
                    728:                         var i;
                    729:                         for (i=0;i<slct.length;i++) {
                    730:                             if (slct.options[i].value==fixeddom) { slct.selectedIndex=i; }
                    731:                         }
                    732:                     }
                    733:                     if (slct.type == 'hidden') {
                    734:                         slct.value = fixeddom;
1.876     raeburn   735:                     }
                    736:                 }
1.468     raeburn   737:             }
                    738:         }
                    739:     }
1.876     raeburn   740:     return;
                    741: }
                    742: 
                    743: $id_functions
                    744: ENDUSERBRW
1.468     raeburn   745: }
                    746: 
                    747: sub setsec_javascript {
1.905     raeburn   748:     my ($sec_element,$formname,$role_element) = @_;
                    749:     my (@courserolenames,@communityrolenames,$rolestr,$courserolestr,
                    750:         $communityrolestr);
                    751:     if ($role_element ne '') {
                    752:         my @allroles = ('st','ta','ep','in','ad');
                    753:         foreach my $crstype ('Course','Community') {
                    754:             if ($crstype eq 'Community') {
                    755:                 foreach my $role (@allroles) {
                    756:                     push(@communityrolenames,&Apache::lonnet::plaintext($role,$crstype));
                    757:                 }
                    758:                 push(@communityrolenames,&Apache::lonnet::plaintext('co'));
                    759:             } else {
                    760:                 foreach my $role (@allroles) {
                    761:                     push(@courserolenames,&Apache::lonnet::plaintext($role,$crstype));
                    762:                 }
                    763:                 push(@courserolenames,&Apache::lonnet::plaintext('cc'));
                    764:             }
                    765:         }
                    766:         $rolestr = '"'.join('","',@allroles).'"';
                    767:         $courserolestr = '"'.join('","',@courserolenames).'"';
                    768:         $communityrolestr = '"'.join('","',@communityrolenames).'"';
                    769:     }
1.468     raeburn   770:     my $setsections = qq|
                    771: function setSect(sectionlist) {
1.629     raeburn   772:     var sectionsArray = new Array();
                    773:     if ((sectionlist != '') && (typeof sectionlist != "undefined")) {
                    774:         sectionsArray = sectionlist.split(",");
                    775:     }
1.468     raeburn   776:     var numSections = sectionsArray.length;
                    777:     document.$formname.$sec_element.length = 0;
                    778:     if (numSections == 0) {
                    779:         document.$formname.$sec_element.multiple=false;
                    780:         document.$formname.$sec_element.size=1;
                    781:         document.$formname.$sec_element.options[0] = new Option('No existing sections','',false,false)
                    782:     } else {
                    783:         if (numSections == 1) {
                    784:             document.$formname.$sec_element.multiple=false;
                    785:             document.$formname.$sec_element.size=1;
                    786:             document.$formname.$sec_element.options[0] = new Option('Select','',true,true);
                    787:             document.$formname.$sec_element.options[1] = new Option('No section','',false,false)
                    788:             document.$formname.$sec_element.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
                    789:         } else {
                    790:             for (var i=0; i<numSections; i++) {
                    791:                 document.$formname.$sec_element.options[i] = new Option(sectionsArray[i],sectionsArray[i],false,false)
                    792:             }
                    793:             document.$formname.$sec_element.multiple=true
                    794:             if (numSections < 3) {
                    795:                 document.$formname.$sec_element.size=numSections;
                    796:             } else {
                    797:                 document.$formname.$sec_element.size=3;
                    798:             }
                    799:             document.$formname.$sec_element.options[0].selected = false
                    800:         }
                    801:     }
1.91      www       802: }
1.905     raeburn   803: 
                    804: function setRole(crstype) {
1.468     raeburn   805: |;
1.905     raeburn   806:     if ($role_element eq '') {
                    807:         $setsections .= '    return;
                    808: }
                    809: ';
                    810:     } else {
                    811:         $setsections .= qq|
                    812:     var elementLength = document.$formname.$role_element.length;
                    813:     var allroles = Array($rolestr);
                    814:     var courserolenames = Array($courserolestr);
                    815:     var communityrolenames = Array($communityrolestr);
                    816:     if (elementLength != undefined) {
                    817:         if (document.$formname.$role_element.options[5].value == 'cc') {
                    818:             if (crstype == 'Course') {
                    819:                 return;
                    820:             } else {
                    821:                 allroles[5] = 'co';
                    822:                 for (var i=0; i<6; i++) {
                    823:                     document.$formname.$role_element.options[i].value = allroles[i];
                    824:                     document.$formname.$role_element.options[i].text = communityrolenames[i];
                    825:                 }
                    826:             }
                    827:         } else {
                    828:             if (crstype == 'Community') {
                    829:                 return;
                    830:             } else {
                    831:                 allroles[5] = 'cc';
                    832:                 for (var i=0; i<6; i++) {
                    833:                     document.$formname.$role_element.options[i].value = allroles[i];
                    834:                     document.$formname.$role_element.options[i].text = courserolenames[i];
                    835:                 }
                    836:             }
                    837:         }
                    838:     }
                    839:     return;
                    840: }
                    841: |;
                    842:     }
1.468     raeburn   843:     return $setsections;
                    844: }
                    845: 
1.91      www       846: sub selectcourse_link {
1.909     raeburn   847:    my ($form,$unameele,$udomele,$desc,$extra_element,$multflag,$selecttype,
                    848:        $typeelement) = @_;
                    849:    my $type = $selecttype;
1.871     raeburn   850:    my $linktext = &mt('Select Course');
                    851:    if ($selecttype eq 'Community') {
1.909     raeburn   852:        $linktext = &mt('Select Community');
1.906     raeburn   853:    } elsif ($selecttype eq 'Course/Community') {
                    854:        $linktext = &mt('Select Course/Community');
1.909     raeburn   855:        $type = '';
1.1019    raeburn   856:    } elsif ($selecttype eq 'Select') {
                    857:        $linktext = &mt('Select');
                    858:        $type = '';
1.871     raeburn   859:    }
1.787     bisitz    860:    return '<span class="LC_nobreak">'
                    861:          ."<a href='"
                    862:          .'javascript:opencrsbrowser("'.$form.'","'.$unameele
                    863:          .'","'.$udomele.'","'.$desc.'","'.$extra_element
1.909     raeburn   864:          .'","'.$multflag.'","'.$type.'","'.$typeelement.'");'
1.871     raeburn   865:          ."'>".$linktext.'</a>'
1.787     bisitz    866:          .'</span>';
1.74      www       867: }
1.42      matthew   868: 
1.653     raeburn   869: sub selectauthor_link {
                    870:    my ($form,$udom)=@_;
                    871:    return '<a href="javascript:openauthorbrowser('."'$form','$udom'".');">'.
                    872:           &mt('Select Author').'</a>';
                    873: }
                    874: 
1.876     raeburn   875: sub selectuser_link {
1.881     raeburn   876:     my ($form,$unameelem,$domelem,$lastelem,$firstelem,$emailelem,$hdomelem,
1.888     raeburn   877:         $coursedom,$linktext,$caller) = @_;
1.876     raeburn   878:     return '<a href="javascript:openuserbrowser('."'$form','$unameelem','$domelem',".
1.888     raeburn   879:            "'$lastelem','$firstelem','$emailelem','$hdomelem','$coursedom','$caller'".
1.881     raeburn   880:            ');">'.$linktext.'</a>';
1.876     raeburn   881: }
                    882: 
1.273     raeburn   883: sub check_uncheck_jscript {
                    884:     my $jscript = <<"ENDSCRT";
                    885: function checkAll(field) {
                    886:     if (field.length > 0) {
                    887:         for (i = 0; i < field.length; i++) {
                    888:             field[i].checked = true ;
                    889:         }
                    890:     } else {
                    891:         field.checked = true
                    892:     }
                    893: }
                    894:  
                    895: function uncheckAll(field) {
                    896:     if (field.length > 0) {
                    897:         for (i = 0; i < field.length; i++) {
                    898:             field[i].checked = false ;
1.543     albertel  899:         }
                    900:     } else {
1.273     raeburn   901:         field.checked = false ;
                    902:     }
                    903: }
                    904: ENDSCRT
                    905:     return $jscript;
                    906: }
                    907: 
1.656     www       908: sub select_timezone {
1.659     raeburn   909:    my ($name,$selected,$onchange,$includeempty)=@_;
                    910:    my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
                    911:    if ($includeempty) {
                    912:        $output .= '<option value=""';
                    913:        if (($selected eq '') || ($selected eq 'local')) {
                    914:            $output .= ' selected="selected" ';
                    915:        }
                    916:        $output .= '> </option>';
                    917:    }
1.657     raeburn   918:    my @timezones = DateTime::TimeZone->all_names;
                    919:    foreach my $tzone (@timezones) {
                    920:        $output.= '<option value="'.$tzone.'"';
                    921:        if ($tzone eq $selected) {
                    922:            $output.=' selected="selected"';
                    923:        }
                    924:        $output.=">$tzone</option>\n";
1.656     www       925:    }
                    926:    $output.="</select>";
                    927:    return $output;
                    928: }
1.273     raeburn   929: 
1.687     raeburn   930: sub select_datelocale {
                    931:     my ($name,$selected,$onchange,$includeempty)=@_;
                    932:     my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
                    933:     if ($includeempty) {
                    934:         $output .= '<option value=""';
                    935:         if ($selected eq '') {
                    936:             $output .= ' selected="selected" ';
                    937:         }
                    938:         $output .= '> </option>';
                    939:     }
                    940:     my (@possibles,%locale_names);
                    941:     my @locales = DateTime::Locale::Catalog::Locales;
                    942:     foreach my $locale (@locales) {
                    943:         if (ref($locale) eq 'HASH') {
                    944:             my $id = $locale->{'id'};
                    945:             if ($id ne '') {
                    946:                 my $en_terr = $locale->{'en_territory'};
                    947:                 my $native_terr = $locale->{'native_territory'};
1.695     raeburn   948:                 my @languages = &Apache::lonlocal::preferred_languages();
1.687     raeburn   949:                 if (grep(/^en$/,@languages) || !@languages) {
                    950:                     if ($en_terr ne '') {
                    951:                         $locale_names{$id} = '('.$en_terr.')';
                    952:                     } elsif ($native_terr ne '') {
                    953:                         $locale_names{$id} = $native_terr;
                    954:                     }
                    955:                 } else {
                    956:                     if ($native_terr ne '') {
                    957:                         $locale_names{$id} = $native_terr.' ';
                    958:                     } elsif ($en_terr ne '') {
                    959:                         $locale_names{$id} = '('.$en_terr.')';
                    960:                     }
                    961:                 }
                    962:                 push (@possibles,$id);
                    963:             }
                    964:         }
                    965:     }
                    966:     foreach my $item (sort(@possibles)) {
                    967:         $output.= '<option value="'.$item.'"';
                    968:         if ($item eq $selected) {
                    969:             $output.=' selected="selected"';
                    970:         }
                    971:         $output.=">$item";
                    972:         if ($locale_names{$item} ne '') {
                    973:             $output.="  $locale_names{$item}</option>\n";
                    974:         }
                    975:         $output.="</option>\n";
                    976:     }
                    977:     $output.="</select>";
                    978:     return $output;
                    979: }
                    980: 
1.792     raeburn   981: sub select_language {
                    982:     my ($name,$selected,$includeempty) = @_;
                    983:     my %langchoices;
                    984:     if ($includeempty) {
                    985:         %langchoices = ('' => 'No language preference');
                    986:     }
                    987:     foreach my $id (&languageids()) {
                    988:         my $code = &supportedlanguagecode($id);
                    989:         if ($code) {
                    990:             $langchoices{$code} = &plainlanguagedescription($id);
                    991:         }
                    992:     }
1.970     raeburn   993:     return &select_form($selected,$name,\%langchoices);
1.792     raeburn   994: }
                    995: 
1.42      matthew   996: =pod
1.36      matthew   997: 
1.648     raeburn   998: =item * &linked_select_forms(...)
1.36      matthew   999: 
                   1000: linked_select_forms returns a string containing a <script></script> block
                   1001: and html for two <select> menus.  The select menus will be linked in that
                   1002: changing the value of the first menu will result in new values being placed
                   1003: in the second menu.  The values in the select menu will appear in alphabetical
1.609     raeburn  1004: order unless a defined order is provided.
1.36      matthew  1005: 
                   1006: linked_select_forms takes the following ordered inputs:
                   1007: 
                   1008: =over 4
                   1009: 
1.112     bowersj2 1010: =item * $formname, the name of the <form> tag
1.36      matthew  1011: 
1.112     bowersj2 1012: =item * $middletext, the text which appears between the <select> tags
1.36      matthew  1013: 
1.112     bowersj2 1014: =item * $firstdefault, the default value for the first menu
1.36      matthew  1015: 
1.112     bowersj2 1016: =item * $firstselectname, the name of the first <select> tag
1.36      matthew  1017: 
1.112     bowersj2 1018: =item * $secondselectname, the name of the second <select> tag
1.36      matthew  1019: 
1.112     bowersj2 1020: =item * $hashref, a reference to a hash containing the data for the menus.
1.36      matthew  1021: 
1.609     raeburn  1022: =item * $menuorder, the order of values in the first menu
                   1023: 
1.41      ng       1024: =back 
                   1025: 
1.36      matthew  1026: Below is an example of such a hash.  Only the 'text', 'default', and 
                   1027: 'select2' keys must appear as stated.  keys(%menu) are the possible 
                   1028: values for the first select menu.  The text that coincides with the 
1.41      ng       1029: first menu value is given in $menu{$choice1}->{'text'}.  The values 
1.36      matthew  1030: and text for the second menu are given in the hash pointed to by 
                   1031: $menu{$choice1}->{'select2'}.  
                   1032: 
1.112     bowersj2 1033:  my %menu = ( A1 => { text =>"Choice A1" ,
                   1034:                        default => "B3",
                   1035:                        select2 => { 
                   1036:                            B1 => "Choice B1",
                   1037:                            B2 => "Choice B2",
                   1038:                            B3 => "Choice B3",
                   1039:                            B4 => "Choice B4"
1.609     raeburn  1040:                            },
                   1041:                        order => ['B4','B3','B1','B2'],
1.112     bowersj2 1042:                    },
                   1043:                A2 => { text =>"Choice A2" ,
                   1044:                        default => "C2",
                   1045:                        select2 => { 
                   1046:                            C1 => "Choice C1",
                   1047:                            C2 => "Choice C2",
                   1048:                            C3 => "Choice C3"
1.609     raeburn  1049:                            },
                   1050:                        order => ['C2','C1','C3'],
1.112     bowersj2 1051:                    },
                   1052:                A3 => { text =>"Choice A3" ,
                   1053:                        default => "D6",
                   1054:                        select2 => { 
                   1055:                            D1 => "Choice D1",
                   1056:                            D2 => "Choice D2",
                   1057:                            D3 => "Choice D3",
                   1058:                            D4 => "Choice D4",
                   1059:                            D5 => "Choice D5",
                   1060:                            D6 => "Choice D6",
                   1061:                            D7 => "Choice D7"
1.609     raeburn  1062:                            },
                   1063:                        order => ['D4','D3','D2','D1','D7','D6','D5'],
1.112     bowersj2 1064:                    }
                   1065:                );
1.36      matthew  1066: 
                   1067: =cut
                   1068: 
                   1069: sub linked_select_forms {
                   1070:     my ($formname,
                   1071:         $middletext,
                   1072:         $firstdefault,
                   1073:         $firstselectname,
                   1074:         $secondselectname, 
1.609     raeburn  1075:         $hashref,
                   1076:         $menuorder,
1.36      matthew  1077:         ) = @_;
                   1078:     my $second = "document.$formname.$secondselectname";
                   1079:     my $first = "document.$formname.$firstselectname";
                   1080:     # output the javascript to do the changing
                   1081:     my $result = '';
1.776     bisitz   1082:     $result.='<script type="text/javascript" language="JavaScript">'."\n";
1.824     bisitz   1083:     $result.="// <![CDATA[\n";
1.36      matthew  1084:     $result.="var select2data = new Object();\n";
                   1085:     $" = '","';
                   1086:     my $debug = '';
                   1087:     foreach my $s1 (sort(keys(%$hashref))) {
                   1088:         $result.="select2data.d_$s1 = new Object();\n";        
                   1089:         $result.="select2data.d_$s1.def = new String('".
                   1090:             $hashref->{$s1}->{'default'}."');\n";
1.609     raeburn  1091:         $result.="select2data.d_$s1.values = new Array(";
1.36      matthew  1092:         my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
1.609     raeburn  1093:         if (ref($hashref->{$s1}->{'order'}) eq 'ARRAY') {
                   1094:             @s2values = @{$hashref->{$s1}->{'order'}};
                   1095:         }
1.36      matthew  1096:         $result.="\"@s2values\");\n";
                   1097:         $result.="select2data.d_$s1.texts = new Array(";        
                   1098:         my @s2texts;
                   1099:         foreach my $value (@s2values) {
                   1100:             push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
                   1101:         }
                   1102:         $result.="\"@s2texts\");\n";
                   1103:     }
                   1104:     $"=' ';
                   1105:     $result.= <<"END";
                   1106: 
                   1107: function select1_changed() {
                   1108:     // Determine new choice
                   1109:     var newvalue = "d_" + $first.value;
                   1110:     // update select2
                   1111:     var values     = select2data[newvalue].values;
                   1112:     var texts      = select2data[newvalue].texts;
                   1113:     var select2def = select2data[newvalue].def;
                   1114:     var i;
                   1115:     // out with the old
                   1116:     for (i = 0; i < $second.options.length; i++) {
                   1117:         $second.options[i] = null;
                   1118:     }
                   1119:     // in with the nuclear
                   1120:     for (i=0;i<values.length; i++) {
                   1121:         $second.options[i] = new Option(values[i]);
1.143     matthew  1122:         $second.options[i].value = values[i];
1.36      matthew  1123:         $second.options[i].text = texts[i];
                   1124:         if (values[i] == select2def) {
                   1125:             $second.options[i].selected = true;
                   1126:         }
                   1127:     }
                   1128: }
1.824     bisitz   1129: // ]]>
1.36      matthew  1130: </script>
                   1131: END
                   1132:     # output the initial values for the selection lists
                   1133:     $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed()\">\n";
1.609     raeburn  1134:     my @order = sort(keys(%{$hashref}));
                   1135:     if (ref($menuorder) eq 'ARRAY') {
                   1136:         @order = @{$menuorder};
                   1137:     }
                   1138:     foreach my $value (@order) {
1.36      matthew  1139:         $result.="    <option value=\"$value\" ";
1.253     albertel 1140:         $result.=" selected=\"selected\" " if ($value eq $firstdefault);
1.119     www      1141:         $result.=">".&mt($hashref->{$value}->{'text'})."</option>\n";
1.36      matthew  1142:     }
                   1143:     $result .= "</select>\n";
                   1144:     my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
                   1145:     $result .= $middletext;
                   1146:     $result .= "<select size=\"1\" name=\"$secondselectname\">\n";
                   1147:     my $seconddefault = $hashref->{$firstdefault}->{'default'};
1.609     raeburn  1148:     
                   1149:     my @secondorder = sort(keys(%select2));
                   1150:     if (ref($hashref->{$firstdefault}->{'order'}) eq 'ARRAY') {
                   1151:         @secondorder = @{$hashref->{$firstdefault}->{'order'}};
                   1152:     }
                   1153:     foreach my $value (@secondorder) {
1.36      matthew  1154:         $result.="    <option value=\"$value\" ";        
1.253     albertel 1155:         $result.=" selected=\"selected\" " if ($value eq $seconddefault);
1.119     www      1156:         $result.=">".&mt($select2{$value})."</option>\n";
1.36      matthew  1157:     }
                   1158:     $result .= "</select>\n";
                   1159:     #    return $debug;
                   1160:     return $result;
                   1161: }   #  end of sub linked_select_forms {
                   1162: 
1.45      matthew  1163: =pod
1.44      bowersj2 1164: 
1.973     raeburn  1165: =item * &help_open_topic($topic,$text,$stayOnPage,$width,$height,$imgid)
1.44      bowersj2 1166: 
1.112     bowersj2 1167: Returns a string corresponding to an HTML link to the given help
                   1168: $topic, where $topic corresponds to the name of a .tex file in
                   1169: /home/httpd/html/adm/help/tex, with underscores replaced by
                   1170: spaces. 
                   1171: 
                   1172: $text will optionally be linked to the same topic, allowing you to
                   1173: link text in addition to the graphic. If you do not want to link
                   1174: text, but wish to specify one of the later parameters, pass an
                   1175: empty string. 
                   1176: 
                   1177: $stayOnPage is a value that will be interpreted as a boolean. If true,
                   1178: the link will not open a new window. If false, the link will open
                   1179: a new window using Javascript. (Default is false.) 
                   1180: 
                   1181: $width and $height are optional numerical parameters that will
                   1182: override the width and height of the popped up window, which may
1.973     raeburn  1183: be useful for certain help topics with big pictures included.
                   1184: 
                   1185: $imgid is the id of the img tag used for the help icon. This may be
                   1186: used in a javascript call to switch the image src.  See 
                   1187: lonhtmlcommon::htmlareaselectactive() for an example.
1.44      bowersj2 1188: 
                   1189: =cut
                   1190: 
                   1191: sub help_open_topic {
1.973     raeburn  1192:     my ($topic, $text, $stayOnPage, $width, $height, $imgid) = @_;
1.48      bowersj2 1193:     $text = "" if (not defined $text);
1.44      bowersj2 1194:     $stayOnPage = 0 if (not defined $stayOnPage);
1.1033    www      1195:     $width = 500 if (not defined $width);
1.44      bowersj2 1196:     $height = 400 if (not defined $height);
                   1197:     my $filename = $topic;
                   1198:     $filename =~ s/ /_/g;
                   1199: 
1.48      bowersj2 1200:     my $template = "";
                   1201:     my $link;
1.572     banghart 1202:     
1.159     www      1203:     $topic=~s/\W/\_/g;
1.44      bowersj2 1204: 
1.572     banghart 1205:     if (!$stayOnPage) {
1.1033    www      1206: 	$link = "javascript:openMyModal('/adm/help/${filename}.hlp',$width,$height,'yes');";
1.1037    www      1207:     } elsif ($stayOnPage eq 'popup') {
                   1208:         $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 1209:     } else {
1.48      bowersj2 1210: 	$link = "/adm/help/${filename}.hlp";
                   1211:     }
                   1212: 
                   1213:     # Add the text
1.755     neumanie 1214:     if ($text ne "") {	
1.763     bisitz   1215: 	$template.='<span class="LC_help_open_topic">'
                   1216:                   .'<a target="_top" href="'.$link.'">'
                   1217:                   .$text.'</a>';
1.48      bowersj2 1218:     }
                   1219: 
1.763     bisitz   1220:     # (Always) Add the graphic
1.179     matthew  1221:     my $title = &mt('Online Help');
1.667     raeburn  1222:     my $helpicon=&lonhttpdurl("/adm/help/help.png");
1.973     raeburn  1223:     if ($imgid ne '') {
                   1224:         $imgid = ' id="'.$imgid.'"';
                   1225:     }
1.763     bisitz   1226:     $template.=' <a target="_top" href="'.$link.'" title="'.$title.'">'
                   1227:               .'<img src="'.$helpicon.'" border="0"'
                   1228:               .' alt="'.&mt('Help: [_1]',$topic).'"'
1.973     raeburn  1229:               .' title="'.$title.'" style="vertical-align:middle;"'.$imgid 
1.763     bisitz   1230:               .' /></a>';
                   1231:     if ($text ne "") {	
                   1232:         $template.='</span>';
                   1233:     }
1.44      bowersj2 1234:     return $template;
                   1235: 
1.106     bowersj2 1236: }
                   1237: 
                   1238: # This is a quicky function for Latex cheatsheet editing, since it 
                   1239: # appears in at least four places
                   1240: sub helpLatexCheatsheet {
1.1037    www      1241:     my ($topic,$text,$not_author,$stayOnPage) = @_;
1.732     raeburn  1242:     my $out;
1.106     bowersj2 1243:     my $addOther = '';
1.732     raeburn  1244:     if ($topic) {
1.1037    www      1245: 	$addOther = '<span>'.&help_open_topic($topic,&mt($text),$stayOnPage, undef, 600).'</span> ';
1.763     bisitz   1246:     }
                   1247:     $out = '<span>' # Start cheatsheet
                   1248: 	  .$addOther
                   1249:           .'<span>'
1.1037    www      1250: 	  .&help_open_topic('Greek_Symbols',&mt('Greek Symbols'),$stayOnPage,undef,600)
1.763     bisitz   1251: 	  .'</span> <span>'
1.1037    www      1252: 	  .&help_open_topic('Other_Symbols',&mt('Other Symbols'),$stayOnPage,undef,600)
1.763     bisitz   1253: 	  .'</span>';
1.732     raeburn  1254:     unless ($not_author) {
1.763     bisitz   1255:         $out .= ' <span>'
1.1037    www      1256: 	       .&help_open_topic('Authoring_Output_Tags',&mt('Output Tags'),$stayOnPage,undef,600)
1.763     bisitz   1257: 	       .'</span>';
1.732     raeburn  1258:     }
1.763     bisitz   1259:     $out .= '</span>'; # End cheatsheet
1.732     raeburn  1260:     return $out;
1.172     www      1261: }
                   1262: 
1.430     albertel 1263: sub general_help {
                   1264:     my $helptopic='Student_Intro';
                   1265:     if ($env{'request.role'}=~/^(ca|au)/) {
                   1266: 	$helptopic='Authoring_Intro';
1.907     raeburn  1267:     } elsif ($env{'request.role'}=~/^(cc|co)/) {
1.430     albertel 1268: 	$helptopic='Course_Coordination_Intro';
1.672     raeburn  1269:     } elsif ($env{'request.role'}=~/^dc/) {
                   1270:         $helptopic='Domain_Coordination_Intro';
1.430     albertel 1271:     }
                   1272:     return $helptopic;
                   1273: }
                   1274: 
                   1275: sub update_help_link {
                   1276:     my ($topic,$component_help,$faq,$bug,$stayOnPage) = @_;
                   1277:     my $origurl = $ENV{'REQUEST_URI'};
                   1278:     $origurl=~s|^/~|/priv/|;
                   1279:     my $timestamp = time;
                   1280:     foreach my $datum (\$topic,\$component_help,\$faq,\$bug,\$origurl) {
                   1281:         $$datum = &escape($$datum);
                   1282:     }
                   1283: 
                   1284:     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";
                   1285:     my $output .= <<"ENDOUTPUT";
                   1286: <script type="text/javascript">
1.824     bisitz   1287: // <![CDATA[
1.430     albertel 1288: banner_link = '$banner_link';
1.824     bisitz   1289: // ]]>
1.430     albertel 1290: </script>
                   1291: ENDOUTPUT
                   1292:     return $output;
                   1293: }
                   1294: 
                   1295: # now just updates the help link and generates a blue icon
1.193     raeburn  1296: sub help_open_menu {
1.430     albertel 1297:     my ($topic,$component_help,$faq,$bug,$stayOnPage,$width,$height,$text) 
1.552     banghart 1298: 	= @_;    
1.949     droeschl 1299:     $stayOnPage = 1;
1.430     albertel 1300:     my $output;
                   1301:     if ($component_help) {
                   1302: 	if (!$text) {
                   1303: 	    $output=&help_open_topic($component_help,undef,$stayOnPage,
                   1304: 				       $width,$height);
                   1305: 	} else {
                   1306: 	    my $help_text;
                   1307: 	    $help_text=&unescape($topic);
                   1308: 	    $output='<table><tr><td>'.
                   1309: 		&help_open_topic($component_help,$help_text,$stayOnPage,
                   1310: 				 $width,$height).'</td></tr></table>';
                   1311: 	}
                   1312:     }
                   1313:     my $banner_link = &update_help_link($topic,$component_help,$faq,$bug,$stayOnPage);
                   1314:     return $output.$banner_link;
                   1315: }
                   1316: 
                   1317: sub top_nav_help {
                   1318:     my ($text) = @_;
1.436     albertel 1319:     $text = &mt($text);
1.949     droeschl 1320:     my $stay_on_page = 1;
                   1321: 
1.572     banghart 1322:     my $link = ($stay_on_page) ? "javascript:helpMenu('display')"
1.436     albertel 1323: 	                     : "javascript:helpMenu('open')";
1.572     banghart 1324:     my $banner_link = &update_help_link(undef,undef,undef,undef,$stay_on_page);
1.436     albertel 1325: 
1.201     raeburn  1326:     my $title = &mt('Get help');
1.436     albertel 1327: 
                   1328:     return <<"END";
                   1329: $banner_link
                   1330:  <a href="$link" title="$title">$text</a>
                   1331: END
                   1332: }
                   1333: 
                   1334: sub help_menu_js {
                   1335:     my ($text) = @_;
1.949     droeschl 1336:     my $stayOnPage = 1;
1.436     albertel 1337:     my $width = 620;
                   1338:     my $height = 600;
1.430     albertel 1339:     my $helptopic=&general_help();
                   1340:     my $details_link = '/adm/help/'.$helptopic.'.hlp';
1.261     albertel 1341:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.331     albertel 1342:     my $start_page =
                   1343:         &Apache::loncommon::start_page('Help Menu', undef,
                   1344: 				       {'frameset'    => 1,
                   1345: 					'js_ready'    => 1,
                   1346: 					'add_entries' => {
                   1347: 					    'border' => '0',
1.579     raeburn  1348: 					    'rows'   => "110,*",},});
1.331     albertel 1349:     my $end_page =
                   1350:         &Apache::loncommon::end_page({'frameset' => 1,
                   1351: 				      'js_ready' => 1,});
                   1352: 
1.436     albertel 1353:     my $template .= <<"ENDTEMPLATE";
                   1354: <script type="text/javascript">
1.877     bisitz   1355: // <![CDATA[
1.253     albertel 1356: // <!-- BEGIN LON-CAPA Internal
1.430     albertel 1357: var banner_link = '';
1.243     raeburn  1358: function helpMenu(target) {
                   1359:     var caller = this;
                   1360:     if (target == 'open') {
                   1361:         var newWindow = null;
                   1362:         try {
1.262     albertel 1363:             newWindow =  window.open($nothing,"helpmenu","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" )
1.243     raeburn  1364:         }
                   1365:         catch(error) {
                   1366:             writeHelp(caller);
                   1367:             return;
                   1368:         }
                   1369:         if (newWindow) {
                   1370:             caller = newWindow;
                   1371:         }
1.193     raeburn  1372:     }
1.243     raeburn  1373:     writeHelp(caller);
                   1374:     return;
                   1375: }
                   1376: function writeHelp(caller) {
1.430     albertel 1377:     caller.document.writeln('$start_page<frame name="bannerframe"  src="'+banner_link+'" /><frame name="bodyframe" src="$details_link" /> $end_page')
1.243     raeburn  1378:     caller.document.close()
                   1379:     caller.focus()
1.193     raeburn  1380: }
1.877     bisitz   1381: // END LON-CAPA Internal -->
1.253     albertel 1382: // ]]>
1.436     albertel 1383: </script>
1.193     raeburn  1384: ENDTEMPLATE
                   1385:     return $template;
                   1386: }
                   1387: 
1.172     www      1388: sub help_open_bug {
                   1389:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel 1390:     unless ($env{'user.adv'}) { return ''; }
1.172     www      1391:     unless ($Apache::lonnet::perlvar{'BugzillaHost'}) { return ''; }
                   1392:     $text = "" if (not defined $text);
                   1393: 	$stayOnPage=1;
1.184     albertel 1394:     $width = 600 if (not defined $width);
                   1395:     $height = 600 if (not defined $height);
1.172     www      1396: 
                   1397:     $topic=~s/\W+/\+/g;
                   1398:     my $link='';
                   1399:     my $template='';
1.379     albertel 1400:     my $url=$Apache::lonnet::perlvar{'BugzillaHost'}.'enter_bug.cgi?product=LON-CAPA&amp;bug_file_loc='.
                   1401: 	&escape($ENV{'REQUEST_URI'}).'&amp;component='.$topic;
1.172     www      1402:     if (!$stayOnPage)
                   1403:     {
                   1404: 	$link = "javascript:void(open('$url', 'Bugzilla', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                   1405:     }
                   1406:     else
                   1407:     {
                   1408: 	$link = $url;
                   1409:     }
                   1410:     # Add the text
                   1411:     if ($text ne "")
                   1412:     {
                   1413: 	$template .= 
                   1414:   "<table bgcolor='#AA3333' cellspacing='1' cellpadding='1' border='0'><tr>".
1.705     tempelho 1415:   "<td bgcolor='#FF5555'><a target=\"_top\" href=\"$link\"><span style=\"color:#FFFFFF;font-size:10pt;\">$text</span></a>";
1.172     www      1416:     }
                   1417: 
                   1418:     # Add the graphic
1.179     matthew  1419:     my $title = &mt('Report a Bug');
1.215     albertel 1420:     my $bugicon=&lonhttpdurl("/adm/lonMisc/smallBug.gif");
1.172     www      1421:     $template .= <<"ENDTEMPLATE";
1.436     albertel 1422:  <a target="_top" href="$link" title="$title"><img src="$bugicon" border="0" alt="(Bug: $topic)" /></a>
1.172     www      1423: ENDTEMPLATE
                   1424:     if ($text ne '') { $template.='</td></tr></table>' };
                   1425:     return $template;
                   1426: 
                   1427: }
                   1428: 
                   1429: sub help_open_faq {
                   1430:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel 1431:     unless ($env{'user.adv'}) { return ''; }
1.172     www      1432:     unless ($Apache::lonnet::perlvar{'FAQHost'}) { return ''; }
                   1433:     $text = "" if (not defined $text);
                   1434: 	$stayOnPage=1;
                   1435:     $width = 350 if (not defined $width);
                   1436:     $height = 400 if (not defined $height);
                   1437: 
                   1438:     $topic=~s/\W+/\+/g;
                   1439:     my $link='';
                   1440:     my $template='';
                   1441:     my $url=$Apache::lonnet::perlvar{'FAQHost'}.'/fom/cache/'.$topic.'.html';
                   1442:     if (!$stayOnPage)
                   1443:     {
                   1444: 	$link = "javascript:void(open('$url', 'FAQ-O-Matic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                   1445:     }
                   1446:     else
                   1447:     {
                   1448: 	$link = $url;
                   1449:     }
                   1450: 
                   1451:     # Add the text
                   1452:     if ($text ne "")
                   1453:     {
                   1454: 	$template .= 
1.173     www      1455:   "<table bgcolor='#337733' cellspacing='1' cellpadding='1' border='0'><tr>".
1.705     tempelho 1456:   "<td bgcolor='#448844'><a target=\"_top\" href=\"$link\"><span style=\"color:#FFFFFF; font-size:10pt;\">$text</span></a>";
1.172     www      1457:     }
                   1458: 
                   1459:     # Add the graphic
1.179     matthew  1460:     my $title = &mt('View the FAQ');
1.215     albertel 1461:     my $faqicon=&lonhttpdurl("/adm/lonMisc/smallFAQ.gif");
1.172     www      1462:     $template .= <<"ENDTEMPLATE";
1.436     albertel 1463:  <a target="_top" href="$link" title="$title"><img src="$faqicon" border="0" alt="(FAQ: $topic)" /></a>
1.172     www      1464: ENDTEMPLATE
                   1465:     if ($text ne '') { $template.='</td></tr></table>' };
                   1466:     return $template;
                   1467: 
1.44      bowersj2 1468: }
1.37      matthew  1469: 
1.180     matthew  1470: ###############################################################
                   1471: ###############################################################
                   1472: 
1.45      matthew  1473: =pod
                   1474: 
1.648     raeburn  1475: =item * &change_content_javascript():
1.256     matthew  1476: 
                   1477: This and the next function allow you to create small sections of an
                   1478: otherwise static HTML page that you can update on the fly with
                   1479: Javascript, even in Netscape 4.
                   1480: 
                   1481: The Javascript fragment returned by this function (no E<lt>scriptE<gt> tag)
                   1482: must be written to the HTML page once. It will prove the Javascript
                   1483: function "change(name, content)". Calling the change function with the
                   1484: name of the section 
                   1485: you want to update, matching the name passed to C<changable_area>, and
                   1486: the new content you want to put in there, will put the content into
                   1487: that area.
                   1488: 
                   1489: B<Note>: Netscape 4 only reserves enough space for the changable area
                   1490: to contain room for the original contents. You need to "make space"
                   1491: for whatever changes you wish to make, and be B<sure> to check your
                   1492: code in Netscape 4. This feature in Netscape 4 is B<not> powerful;
                   1493: it's adequate for updating a one-line status display, but little more.
                   1494: This script will set the space to 100% width, so you only need to
                   1495: worry about height in Netscape 4.
                   1496: 
                   1497: Modern browsers are much less limiting, and if you can commit to the
                   1498: user not using Netscape 4, this feature may be used freely with
                   1499: pretty much any HTML.
                   1500: 
                   1501: =cut
                   1502: 
                   1503: sub change_content_javascript {
                   1504:     # If we're on Netscape 4, we need to use Layer-based code
1.258     albertel 1505:     if ($env{'browser.type'} eq 'netscape' &&
                   1506: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew  1507: 	return (<<NETSCAPE4);
                   1508: 	function change(name, content) {
                   1509: 	    doc = document.layers[name+"___escape"].layers[0].document;
                   1510: 	    doc.open();
                   1511: 	    doc.write(content);
                   1512: 	    doc.close();
                   1513: 	}
                   1514: NETSCAPE4
                   1515:     } else {
                   1516: 	# Otherwise, we need to use semi-standards-compliant code
                   1517: 	# (technically, "innerHTML" isn't standard but the equivalent
                   1518: 	# is really scary, and every useful browser supports it
                   1519: 	return (<<DOMBASED);
                   1520: 	function change(name, content) {
                   1521: 	    element = document.getElementById(name);
                   1522: 	    element.innerHTML = content;
                   1523: 	}
                   1524: DOMBASED
                   1525:     }
                   1526: }
                   1527: 
                   1528: =pod
                   1529: 
1.648     raeburn  1530: =item * &changable_area($name,$origContent):
1.256     matthew  1531: 
                   1532: This provides a "changable area" that can be modified on the fly via
                   1533: the Javascript code provided in C<change_content_javascript>. $name is
                   1534: the name you will use to reference the area later; do not repeat the
                   1535: same name on a given HTML page more then once. $origContent is what
                   1536: the area will originally contain, which can be left blank.
                   1537: 
                   1538: =cut
                   1539: 
                   1540: sub changable_area {
                   1541:     my ($name, $origContent) = @_;
                   1542: 
1.258     albertel 1543:     if ($env{'browser.type'} eq 'netscape' &&
                   1544: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew  1545: 	# If this is netscape 4, we need to use the Layer tag
                   1546: 	return "<ilayer width='100%' id='${name}___escape' overflow='none'><layer width='100%' id='$name' overflow='none'>$origContent</layer></ilayer>";
                   1547:     } else {
                   1548: 	return "<span id='$name'>$origContent</span>";
                   1549:     }
                   1550: }
                   1551: 
                   1552: =pod
                   1553: 
1.648     raeburn  1554: =item * &viewport_geometry_js 
1.590     raeburn  1555: 
                   1556: Provides javascript object (Geometry) which can provide information about the viewport geometry for the client browser.
                   1557: 
                   1558: =cut
                   1559: 
                   1560: 
                   1561: sub viewport_geometry_js { 
                   1562:     return <<"GEOMETRY";
                   1563: var Geometry = {};
                   1564: function init_geometry() {
                   1565:     if (Geometry.init) { return };
                   1566:     Geometry.init=1;
                   1567:     if (window.innerHeight) {
                   1568:         Geometry.getViewportHeight   = function() { return window.innerHeight; };
                   1569:         Geometry.getViewportWidth   = function() { return window.innerWidth; };
                   1570:         Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
                   1571:         Geometry.getVerticalScroll   = function() { return window.pageYOffset; };
                   1572:     }
                   1573:     else if (document.documentElement && document.documentElement.clientHeight) {
                   1574:         Geometry.getViewportHeight =
                   1575:             function() { return document.documentElement.clientHeight; };
                   1576:         Geometry.getViewportWidth =
                   1577:             function() { return document.documentElement.clientWidth; };
                   1578: 
                   1579:         Geometry.getHorizontalScroll =
                   1580:             function() { return document.documentElement.scrollLeft; };
                   1581:         Geometry.getVerticalScroll =
                   1582:             function() { return document.documentElement.scrollTop; };
                   1583:     }
                   1584:     else if (document.body.clientHeight) {
                   1585:         Geometry.getViewportHeight =
                   1586:             function() { return document.body.clientHeight; };
                   1587:         Geometry.getViewportWidth =
                   1588:             function() { return document.body.clientWidth; };
                   1589:         Geometry.getHorizontalScroll =
                   1590:             function() { return document.body.scrollLeft; };
                   1591:         Geometry.getVerticalScroll =
                   1592:             function() { return document.body.scrollTop; };
                   1593:     }
                   1594: }
                   1595: 
                   1596: GEOMETRY
                   1597: }
                   1598: 
                   1599: =pod
                   1600: 
1.648     raeburn  1601: =item * &viewport_size_js()
1.590     raeburn  1602: 
                   1603: 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. 
                   1604: 
                   1605: =cut
                   1606: 
                   1607: sub viewport_size_js {
                   1608:     my $geometry = &viewport_geometry_js();
                   1609:     return <<"DIMS";
                   1610: 
                   1611: $geometry
                   1612: 
                   1613: function getViewportDims(width,height) {
                   1614:     init_geometry();
                   1615:     width.value = Geometry.getViewportWidth();
                   1616:     height.value = Geometry.getViewportHeight();
                   1617:     return;
                   1618: }
                   1619: 
                   1620: DIMS
                   1621: }
                   1622: 
                   1623: =pod
                   1624: 
1.648     raeburn  1625: =item * &resize_textarea_js()
1.565     albertel 1626: 
                   1627: emits the needed javascript to resize a textarea to be as big as possible
                   1628: 
                   1629: creates a function resize_textrea that takes two IDs first should be
                   1630: the id of the element to resize, second should be the id of a div that
                   1631: surrounds everything that comes after the textarea, this routine needs
                   1632: to be attached to the <body> for the onload and onresize events.
                   1633: 
1.648     raeburn  1634: =back
1.565     albertel 1635: 
                   1636: =cut
                   1637: 
                   1638: sub resize_textarea_js {
1.590     raeburn  1639:     my $geometry = &viewport_geometry_js();
1.565     albertel 1640:     return <<"RESIZE";
                   1641:     <script type="text/javascript">
1.824     bisitz   1642: // <![CDATA[
1.590     raeburn  1643: $geometry
1.565     albertel 1644: 
1.588     albertel 1645: function getX(element) {
                   1646:     var x = 0;
                   1647:     while (element) {
                   1648: 	x += element.offsetLeft;
                   1649: 	element = element.offsetParent;
                   1650:     }
                   1651:     return x;
                   1652: }
                   1653: function getY(element) {
                   1654:     var y = 0;
                   1655:     while (element) {
                   1656: 	y += element.offsetTop;
                   1657: 	element = element.offsetParent;
                   1658:     }
                   1659:     return y;
                   1660: }
                   1661: 
                   1662: 
1.565     albertel 1663: function resize_textarea(textarea_id,bottom_id) {
                   1664:     init_geometry();
                   1665:     var textarea        = document.getElementById(textarea_id);
                   1666:     //alert(textarea);
                   1667: 
1.588     albertel 1668:     var textarea_top    = getY(textarea);
1.565     albertel 1669:     var textarea_height = textarea.offsetHeight;
                   1670:     var bottom          = document.getElementById(bottom_id);
1.588     albertel 1671:     var bottom_top      = getY(bottom);
1.565     albertel 1672:     var bottom_height   = bottom.offsetHeight;
                   1673:     var window_height   = Geometry.getViewportHeight();
1.588     albertel 1674:     var fudge           = 23;
1.565     albertel 1675:     var new_height      = window_height-fudge-textarea_top-bottom_height;
                   1676:     if (new_height < 300) {
                   1677: 	new_height = 300;
                   1678:     }
                   1679:     textarea.style.height=new_height+'px';
                   1680: }
1.824     bisitz   1681: // ]]>
1.565     albertel 1682: </script>
                   1683: RESIZE
                   1684: 
                   1685: }
                   1686: 
                   1687: =pod
                   1688: 
1.256     matthew  1689: =head1 Excel and CSV file utility routines
                   1690: 
                   1691: =over 4
                   1692: 
                   1693: =cut
                   1694: 
                   1695: ###############################################################
                   1696: ###############################################################
                   1697: 
                   1698: =pod
                   1699: 
1.648     raeburn  1700: =item * &csv_translate($text) 
1.37      matthew  1701: 
1.185     www      1702: Translate $text to allow it to be output as a 'comma separated values' 
1.37      matthew  1703: format.
                   1704: 
                   1705: =cut
                   1706: 
1.180     matthew  1707: ###############################################################
                   1708: ###############################################################
1.37      matthew  1709: sub csv_translate {
                   1710:     my $text = shift;
                   1711:     $text =~ s/\"/\"\"/g;
1.209     albertel 1712:     $text =~ s/\n/ /g;
1.37      matthew  1713:     return $text;
                   1714: }
1.180     matthew  1715: 
                   1716: ###############################################################
                   1717: ###############################################################
                   1718: 
                   1719: =pod
                   1720: 
1.648     raeburn  1721: =item * &define_excel_formats()
1.180     matthew  1722: 
                   1723: Define some commonly used Excel cell formats.
                   1724: 
                   1725: Currently supported formats:
                   1726: 
                   1727: =over 4
                   1728: 
                   1729: =item header
                   1730: 
                   1731: =item bold
                   1732: 
                   1733: =item h1
                   1734: 
                   1735: =item h2
                   1736: 
                   1737: =item h3
                   1738: 
1.256     matthew  1739: =item h4
                   1740: 
                   1741: =item i
                   1742: 
1.180     matthew  1743: =item date
                   1744: 
                   1745: =back
                   1746: 
                   1747: Inputs: $workbook
                   1748: 
                   1749: Returns: $format, a hash reference.
                   1750: 
1.1057    foxr     1751: 
1.180     matthew  1752: =cut
                   1753: 
                   1754: ###############################################################
                   1755: ###############################################################
                   1756: sub define_excel_formats {
                   1757:     my ($workbook) = @_;
                   1758:     my $format;
                   1759:     $format->{'header'} = $workbook->add_format(bold      => 1, 
                   1760:                                                 bottom    => 1,
                   1761:                                                 align     => 'center');
                   1762:     $format->{'bold'} = $workbook->add_format(bold=>1);
                   1763:     $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
                   1764:     $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
                   1765:     $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
1.255     matthew  1766:     $format->{'h4'}   = $workbook->add_format(bold=>1, size=>12);
1.246     matthew  1767:     $format->{'i'}    = $workbook->add_format(italic=>1);
1.180     matthew  1768:     $format->{'date'} = $workbook->add_format(num_format=>
1.207     matthew  1769:                                             'mm/dd/yyyy hh:mm:ss');
1.180     matthew  1770:     return $format;
                   1771: }
                   1772: 
                   1773: ###############################################################
                   1774: ###############################################################
1.113     bowersj2 1775: 
                   1776: =pod
                   1777: 
1.648     raeburn  1778: =item * &create_workbook()
1.255     matthew  1779: 
                   1780: Create an Excel worksheet.  If it fails, output message on the
                   1781: request object and return undefs.
                   1782: 
                   1783: Inputs: Apache request object
                   1784: 
                   1785: Returns (undef) on failure, 
                   1786:     Excel worksheet object, scalar with filename, and formats 
                   1787:     from &Apache::loncommon::define_excel_formats on success
                   1788: 
                   1789: =cut
                   1790: 
                   1791: ###############################################################
                   1792: ###############################################################
                   1793: sub create_workbook {
                   1794:     my ($r) = @_;
                   1795:         #
                   1796:     # Create the excel spreadsheet
                   1797:     my $filename = '/prtspool/'.
1.258     albertel 1798:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.255     matthew  1799:         time.'_'.rand(1000000000).'.xls';
                   1800:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
                   1801:     if (! defined($workbook)) {
                   1802:         $r->log_error("Error creating excel spreadsheet $filename: $!");
1.928     bisitz   1803:         $r->print(
                   1804:             '<p class="LC_error">'
                   1805:            .&mt('Problems occurred in creating the new Excel file.')
                   1806:            .' '.&mt('This error has been logged.')
                   1807:            .' '.&mt('Please alert your LON-CAPA administrator.')
                   1808:            .'</p>'
                   1809:         );
1.255     matthew  1810:         return (undef);
                   1811:     }
                   1812:     #
1.1014    foxr     1813:     $workbook->set_tempdir(LONCAPA::tempdir());
1.255     matthew  1814:     #
                   1815:     my $format = &Apache::loncommon::define_excel_formats($workbook);
                   1816:     return ($workbook,$filename,$format);
                   1817: }
                   1818: 
                   1819: ###############################################################
                   1820: ###############################################################
                   1821: 
                   1822: =pod
                   1823: 
1.648     raeburn  1824: =item * &create_text_file()
1.113     bowersj2 1825: 
1.542     raeburn  1826: Create a file to write to and eventually make available to the user.
1.256     matthew  1827: If file creation fails, outputs an error message on the request object and 
                   1828: return undefs.
1.113     bowersj2 1829: 
1.256     matthew  1830: Inputs: Apache request object, and file suffix
1.113     bowersj2 1831: 
1.256     matthew  1832: Returns (undef) on failure, 
                   1833:     Filehandle and filename on success.
1.113     bowersj2 1834: 
                   1835: =cut
                   1836: 
1.256     matthew  1837: ###############################################################
                   1838: ###############################################################
                   1839: sub create_text_file {
                   1840:     my ($r,$suffix) = @_;
                   1841:     if (! defined($suffix)) { $suffix = 'txt'; };
                   1842:     my $fh;
                   1843:     my $filename = '/prtspool/'.
1.258     albertel 1844:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.256     matthew  1845:         time.'_'.rand(1000000000).'.'.$suffix;
                   1846:     $fh = Apache::File->new('>/home/httpd'.$filename);
                   1847:     if (! defined($fh)) {
                   1848:         $r->log_error("Couldn't open $filename for output $!");
1.928     bisitz   1849:         $r->print(
                   1850:             '<p class="LC_error">'
                   1851:            .&mt('Problems occurred in creating the output file.')
                   1852:            .' '.&mt('This error has been logged.')
                   1853:            .' '.&mt('Please alert your LON-CAPA administrator.')
                   1854:            .'</p>'
                   1855:         );
1.113     bowersj2 1856:     }
1.256     matthew  1857:     return ($fh,$filename)
1.113     bowersj2 1858: }
                   1859: 
                   1860: 
1.256     matthew  1861: =pod 
1.113     bowersj2 1862: 
                   1863: =back
                   1864: 
                   1865: =cut
1.37      matthew  1866: 
                   1867: ###############################################################
1.33      matthew  1868: ##        Home server <option> list generating code          ##
                   1869: ###############################################################
1.35      matthew  1870: 
1.169     www      1871: # ------------------------------------------
                   1872: 
                   1873: sub domain_select {
                   1874:     my ($name,$value,$multiple)=@_;
                   1875:     my %domains=map { 
1.514     albertel 1876: 	$_ => $_.' '. &Apache::lonnet::domain($_,'description') 
1.512     albertel 1877:     } &Apache::lonnet::all_domains();
1.169     www      1878:     if ($multiple) {
                   1879: 	$domains{''}=&mt('Any domain');
1.550     albertel 1880: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
1.287     albertel 1881: 	return &multiple_select_form($name,$value,4,\%domains);
1.169     www      1882:     } else {
1.550     albertel 1883: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
1.970     raeburn  1884: 	return &select_form($name,$value,\%domains);
1.169     www      1885:     }
                   1886: }
                   1887: 
1.282     albertel 1888: #-------------------------------------------
                   1889: 
                   1890: =pod
                   1891: 
1.519     raeburn  1892: =head1 Routines for form select boxes
                   1893: 
                   1894: =over 4
                   1895: 
1.648     raeburn  1896: =item * &multiple_select_form($name,$value,$size,$hash,$order)
1.282     albertel 1897: 
                   1898: Returns a string containing a <select> element int multiple mode
                   1899: 
                   1900: 
                   1901: Args:
                   1902:   $name - name of the <select> element
1.506     raeburn  1903:   $value - scalar or array ref of values that should already be selected
1.282     albertel 1904:   $size - number of rows long the select element is
1.283     albertel 1905:   $hash - the elements should be 'option' => 'shown text'
1.282     albertel 1906:           (shown text should already have been &mt())
1.506     raeburn  1907:   $order - (optional) array ref of the order to show the elements in
1.283     albertel 1908: 
1.282     albertel 1909: =cut
                   1910: 
                   1911: #-------------------------------------------
1.169     www      1912: sub multiple_select_form {
1.284     albertel 1913:     my ($name,$value,$size,$hash,$order)=@_;
1.169     www      1914:     my %selected = map { $_ => 1 } ref($value)?@{$value}:($value);
                   1915:     my $output='';
1.191     matthew  1916:     if (! defined($size)) {
                   1917:         $size = 4;
1.283     albertel 1918:         if (scalar(keys(%$hash))<4) {
                   1919:             $size = scalar(keys(%$hash));
1.191     matthew  1920:         }
                   1921:     }
1.734     bisitz   1922:     $output.="\n".'<select name="'.$name.'" size="'.$size.'" multiple="multiple">';
1.501     banghart 1923:     my @order;
1.506     raeburn  1924:     if (ref($order) eq 'ARRAY')  {
                   1925:         @order = @{$order};
                   1926:     } else {
                   1927:         @order = sort(keys(%$hash));
1.501     banghart 1928:     }
                   1929:     if (exists($$hash{'select_form_order'})) {
                   1930:         @order = @{$$hash{'select_form_order'}};
                   1931:     }
                   1932:         
1.284     albertel 1933:     foreach my $key (@order) {
1.356     albertel 1934:         $output.='<option value="'.&HTML::Entities::encode($key,'"<>&').'" ';
1.284     albertel 1935:         $output.='selected="selected" ' if ($selected{$key});
                   1936:         $output.='>'.$hash->{$key}."</option>\n";
1.169     www      1937:     }
                   1938:     $output.="</select>\n";
                   1939:     return $output;
                   1940: }
                   1941: 
1.88      www      1942: #-------------------------------------------
                   1943: 
                   1944: =pod
                   1945: 
1.970     raeburn  1946: =item * &select_form($defdom,$name,$hashref,$onchange)
1.88      www      1947: 
                   1948: Returns a string containing a <select name='$name' size='1'> form to 
1.970     raeburn  1949: allow a user to select options from a ref to a hash containing:
                   1950: option_name => displayed text. An optional $onchange can include
                   1951: a javascript onchange item, e.g., onchange="this.form.submit();"  
                   1952: 
1.88      www      1953: See lonrights.pm for an example invocation and use.
                   1954: 
                   1955: =cut
                   1956: 
                   1957: #-------------------------------------------
                   1958: sub select_form {
1.970     raeburn  1959:     my ($def,$name,$hashref,$onchange) = @_;
                   1960:     return unless (ref($hashref) eq 'HASH');
                   1961:     if ($onchange) {
                   1962:         $onchange = ' onchange="'.$onchange.'"';
                   1963:     }
                   1964:     my $selectform = "<select name=\"$name\" size=\"1\"$onchange>\n";
1.128     albertel 1965:     my @keys;
1.970     raeburn  1966:     if (exists($hashref->{'select_form_order'})) {
                   1967: 	@keys=@{$hashref->{'select_form_order'}};
1.128     albertel 1968:     } else {
1.970     raeburn  1969: 	@keys=sort(keys(%{$hashref}));
1.128     albertel 1970:     }
1.356     albertel 1971:     foreach my $key (@keys) {
                   1972:         $selectform.=
                   1973: 	    '<option value="'.&HTML::Entities::encode($key,'"<>&').'" '.
                   1974:             ($key eq $def ? 'selected="selected" ' : '').
1.970     raeburn  1975:                 ">".$hashref->{$key}."</option>\n";
1.88      www      1976:     }
                   1977:     $selectform.="</select>";
                   1978:     return $selectform;
                   1979: }
                   1980: 
1.475     www      1981: # For display filters
                   1982: 
                   1983: sub display_filter {
                   1984:     if (!$env{'form.show'}) { $env{'form.show'}=10; }
1.477     www      1985:     if (!$env{'form.displayfilter'}) { $env{'form.displayfilter'}='currentfolder'; }
1.714     bisitz   1986:     return '<span class="LC_nobreak"><label>'.&mt('Records [_1]',
1.475     www      1987: 			       &Apache::lonmeta::selectbox('show',$env{'form.show'},undef,
                   1988: 							   (&mt('all'),10,20,50,100,1000,10000))).
1.714     bisitz   1989: 	   '</label></span> <span class="LC_nobreak">'.
1.475     www      1990:            &mt('Filter [_1]',
1.477     www      1991: 	   &select_form($env{'form.displayfilter'},
                   1992: 			'displayfilter',
1.970     raeburn  1993: 			{'currentfolder' => 'Current folder/page',
1.477     www      1994: 			 'containing' => 'Containing phrase',
1.970     raeburn  1995: 			 'none' => 'None'})).
1.714     bisitz   1996: 			 '<input type="text" name="containingphrase" size="30" value="'.&HTML::Entities::encode($env{'form.containingphrase'}).'" /></span>';
1.475     www      1997: }
                   1998: 
1.167     www      1999: sub gradeleveldescription {
                   2000:     my $gradelevel=shift;
                   2001:     my %gradelevels=(0 => 'Not specified',
                   2002: 		     1 => 'Grade 1',
                   2003: 		     2 => 'Grade 2',
                   2004: 		     3 => 'Grade 3',
                   2005: 		     4 => 'Grade 4',
                   2006: 		     5 => 'Grade 5',
                   2007: 		     6 => 'Grade 6',
                   2008: 		     7 => 'Grade 7',
                   2009: 		     8 => 'Grade 8',
                   2010: 		     9 => 'Grade 9',
                   2011: 		     10 => 'Grade 10',
                   2012: 		     11 => 'Grade 11',
                   2013: 		     12 => 'Grade 12',
                   2014: 		     13 => 'Grade 13',
                   2015: 		     14 => '100 Level',
                   2016: 		     15 => '200 Level',
                   2017: 		     16 => '300 Level',
                   2018: 		     17 => '400 Level',
                   2019: 		     18 => 'Graduate Level');
                   2020:     return &mt($gradelevels{$gradelevel});
                   2021: }
                   2022: 
1.163     www      2023: sub select_level_form {
                   2024:     my ($deflevel,$name)=@_;
                   2025:     unless ($deflevel) { $deflevel=0; }
1.167     www      2026:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
                   2027:     for (my $i=0; $i<=18; $i++) {
                   2028:         $selectform.="<option value=\"$i\" ".
1.253     albertel 2029:             ($i==$deflevel ? 'selected="selected" ' : '').
1.167     www      2030:                 ">".&gradeleveldescription($i)."</option>\n";
                   2031:     }
                   2032:     $selectform.="</select>";
                   2033:     return $selectform;
1.163     www      2034: }
1.167     www      2035: 
1.35      matthew  2036: #-------------------------------------------
                   2037: 
1.45      matthew  2038: =pod
                   2039: 
1.910     raeburn  2040: =item * &select_dom_form($defdom,$name,$includeempty,$showdomdesc,$onchange,$incdoms)
1.35      matthew  2041: 
                   2042: Returns a string containing a <select name='$name' size='1'> form to 
                   2043: allow a user to select the domain to preform an operation in.  
                   2044: See loncreateuser.pm for an example invocation and use.
                   2045: 
1.90      www      2046: If the $includeempty flag is set, it also includes an empty choice ("no domain
                   2047: selected");
                   2048: 
1.743     raeburn  2049: If the $showdomdesc flag is set, the domain name is followed by the domain description.
                   2050: 
1.910     raeburn  2051: 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.
                   2052: 
                   2053: The optional $incdoms is a reference to an array of domains which will be the only available options. 
1.563     raeburn  2054: 
1.35      matthew  2055: =cut
                   2056: 
                   2057: #-------------------------------------------
1.34      matthew  2058: sub select_dom_form {
1.910     raeburn  2059:     my ($defdom,$name,$includeempty,$showdomdesc,$onchange,$incdoms) = @_;
1.872     raeburn  2060:     if ($onchange) {
1.874     raeburn  2061:         $onchange = ' onchange="'.$onchange.'"';
1.743     raeburn  2062:     }
1.910     raeburn  2063:     my @domains;
                   2064:     if (ref($incdoms) eq 'ARRAY') {
                   2065:         @domains = sort {lc($a) cmp lc($b)} (@{$incdoms});
                   2066:     } else {
                   2067:         @domains = sort {lc($a) cmp lc($b)} (&Apache::lonnet::all_domains());
                   2068:     }
1.90      www      2069:     if ($includeempty) { @domains=('',@domains); }
1.743     raeburn  2070:     my $selectdomain = "<select name=\"$name\" size=\"1\"$onchange>\n";
1.356     albertel 2071:     foreach my $dom (@domains) {
                   2072:         $selectdomain.="<option value=\"$dom\" ".
1.563     raeburn  2073:             ($dom eq $defdom ? 'selected="selected" ' : '').'>'.$dom;
                   2074:         if ($showdomdesc) {
                   2075:             if ($dom ne '') {
                   2076:                 my $domdesc = &Apache::lonnet::domain($dom,'description');
                   2077:                 if ($domdesc ne '') {
                   2078:                     $selectdomain .= ' ('.$domdesc.')';
                   2079:                 }
                   2080:             } 
                   2081:         }
                   2082:         $selectdomain .= "</option>\n";
1.34      matthew  2083:     }
                   2084:     $selectdomain.="</select>";
                   2085:     return $selectdomain;
                   2086: }
                   2087: 
1.35      matthew  2088: #-------------------------------------------
                   2089: 
1.45      matthew  2090: =pod
                   2091: 
1.648     raeburn  2092: =item * &home_server_form_item($domain,$name,$defaultflag)
1.35      matthew  2093: 
1.586     raeburn  2094: input: 4 arguments (two required, two optional) - 
                   2095:     $domain - domain of new user
                   2096:     $name - name of form element
                   2097:     $default - Value of 'default' causes a default item to be first 
                   2098:                             option, and selected by default. 
                   2099:     $hide - Value of 'hide' causes hiding of the name of the server, 
                   2100:                             if 1 server found, or default, if 0 found.
1.594     raeburn  2101: output: returns 2 items: 
1.586     raeburn  2102: (a) form element which contains either:
                   2103:    (i) <select name="$name">
                   2104:         <option value="$hostid1">$hostid $servers{$hostid}</option>
                   2105:         <option value="$hostid2">$hostid $servers{$hostid}</option>       
                   2106:        </select>
                   2107:        form item if there are multiple library servers in $domain, or
                   2108:    (ii) an <input type="hidden" name="$name" value="$hostid" /> form item 
                   2109:        if there is only one library server in $domain.
                   2110: 
                   2111: (b) number of library servers found.
                   2112: 
                   2113: See loncreateuser.pm for example of use.
1.35      matthew  2114: 
                   2115: =cut
                   2116: 
                   2117: #-------------------------------------------
1.586     raeburn  2118: sub home_server_form_item {
                   2119:     my ($domain,$name,$default,$hide) = @_;
1.513     albertel 2120:     my %servers = &Apache::lonnet::get_servers($domain,'library');
1.586     raeburn  2121:     my $result;
                   2122:     my $numlib = keys(%servers);
                   2123:     if ($numlib > 1) {
                   2124:         $result .= '<select name="'.$name.'" />'."\n";
                   2125:         if ($default) {
1.804     bisitz   2126:             $result .= '<option value="default" selected="selected">'.&mt('default').
1.586     raeburn  2127:                        '</option>'."\n";
                   2128:         }
                   2129:         foreach my $hostid (sort(keys(%servers))) {
                   2130:             $result.= '<option value="'.$hostid.'">'.
                   2131: 	              $hostid.' '.$servers{$hostid}."</option>\n";
                   2132:         }
                   2133:         $result .= '</select>'."\n";
                   2134:     } elsif ($numlib == 1) {
                   2135:         my $hostid;
                   2136:         foreach my $item (keys(%servers)) {
                   2137:             $hostid = $item;
                   2138:         }
                   2139:         $result .= '<input type="hidden" name="'.$name.'" value="'.
                   2140:                    $hostid.'" />';
                   2141:                    if (!$hide) {
                   2142:                        $result .= $hostid.' '.$servers{$hostid};
                   2143:                    }
                   2144:                    $result .= "\n";
                   2145:     } elsif ($default) {
                   2146:         $result .= '<input type="hidden" name="'.$name.
                   2147:                    '" value="default" />';
                   2148:                    if (!$hide) {
                   2149:                        $result .= &mt('default');
                   2150:                    }
                   2151:                    $result .= "\n";
1.33      matthew  2152:     }
1.586     raeburn  2153:     return ($result,$numlib);
1.33      matthew  2154: }
1.112     bowersj2 2155: 
                   2156: =pod
                   2157: 
1.534     albertel 2158: =back 
                   2159: 
1.112     bowersj2 2160: =cut
1.87      matthew  2161: 
                   2162: ###############################################################
1.112     bowersj2 2163: ##                  Decoding User Agent                      ##
1.87      matthew  2164: ###############################################################
                   2165: 
                   2166: =pod
                   2167: 
1.112     bowersj2 2168: =head1 Decoding the User Agent
                   2169: 
                   2170: =over 4
                   2171: 
                   2172: =item * &decode_user_agent()
1.87      matthew  2173: 
                   2174: Inputs: $r
                   2175: 
                   2176: Outputs:
                   2177: 
                   2178: =over 4
                   2179: 
1.112     bowersj2 2180: =item * $httpbrowser
1.87      matthew  2181: 
1.112     bowersj2 2182: =item * $clientbrowser
1.87      matthew  2183: 
1.112     bowersj2 2184: =item * $clientversion
1.87      matthew  2185: 
1.112     bowersj2 2186: =item * $clientmathml
1.87      matthew  2187: 
1.112     bowersj2 2188: =item * $clientunicode
1.87      matthew  2189: 
1.112     bowersj2 2190: =item * $clientos
1.87      matthew  2191: 
                   2192: =back
                   2193: 
1.157     matthew  2194: =back 
                   2195: 
1.87      matthew  2196: =cut
                   2197: 
                   2198: ###############################################################
                   2199: ###############################################################
                   2200: sub decode_user_agent {
1.247     albertel 2201:     my ($r)=@_;
1.87      matthew  2202:     my @browsertype=split(/\&/,$Apache::lonnet::perlvar{"lonBrowsDet"});
                   2203:     my %mathcap=split(/\&/,$$Apache::lonnet::perlvar{"lonMathML"});
                   2204:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
1.247     albertel 2205:     if (!$httpbrowser && $r) { $httpbrowser=$r->header_in('User-Agent'); }
1.87      matthew  2206:     my $clientbrowser='unknown';
                   2207:     my $clientversion='0';
                   2208:     my $clientmathml='';
                   2209:     my $clientunicode='0';
                   2210:     for (my $i=0;$i<=$#browsertype;$i++) {
                   2211:         my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\:/,$browsertype[$i]);
                   2212: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
                   2213: 	    $clientbrowser=$bname;
                   2214:             $httpbrowser=~/$vreg/i;
                   2215: 	    $clientversion=$1;
                   2216:             $clientmathml=($clientversion>=$minv);
                   2217:             $clientunicode=($clientversion>=$univ);
                   2218: 	}
                   2219:     }
                   2220:     my $clientos='unknown';
                   2221:     if (($httpbrowser=~/linux/i) ||
                   2222:         ($httpbrowser=~/unix/i) ||
                   2223:         ($httpbrowser=~/ux/i) ||
                   2224:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
                   2225:     if (($httpbrowser=~/vax/i) ||
                   2226:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
                   2227:     if ($httpbrowser=~/next/i) { $clientos='next'; }
                   2228:     if (($httpbrowser=~/mac/i) ||
                   2229:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
                   2230:     if ($httpbrowser=~/win/i) { $clientos='win'; }
                   2231:     if ($httpbrowser=~/embed/i) { $clientos='pda'; }
                   2232:     return ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                   2233:             $clientunicode,$clientos,);
                   2234: }
                   2235: 
1.32      matthew  2236: ###############################################################
                   2237: ##    Authentication changing form generation subroutines    ##
                   2238: ###############################################################
                   2239: ##
                   2240: ## All of the authform_xxxxxxx subroutines take their inputs in a
                   2241: ## hash, and have reasonable default values.
                   2242: ##
                   2243: ##    formname = the name given in the <form> tag.
1.35      matthew  2244: #-------------------------------------------
                   2245: 
1.45      matthew  2246: =pod
                   2247: 
1.112     bowersj2 2248: =head1 Authentication Routines
                   2249: 
                   2250: =over 4
                   2251: 
1.648     raeburn  2252: =item * &authform_xxxxxx()
1.35      matthew  2253: 
                   2254: The authform_xxxxxx subroutines provide javascript and html forms which 
                   2255: handle some of the conveniences required for authentication forms.  
                   2256: This is not an optimal method, but it works.  
                   2257: 
                   2258: =over 4
                   2259: 
1.112     bowersj2 2260: =item * authform_header
1.35      matthew  2261: 
1.112     bowersj2 2262: =item * authform_authorwarning
1.35      matthew  2263: 
1.112     bowersj2 2264: =item * authform_nochange
1.35      matthew  2265: 
1.112     bowersj2 2266: =item * authform_kerberos
1.35      matthew  2267: 
1.112     bowersj2 2268: =item * authform_internal
1.35      matthew  2269: 
1.112     bowersj2 2270: =item * authform_filesystem
1.35      matthew  2271: 
                   2272: =back
                   2273: 
1.648     raeburn  2274: See loncreateuser.pm for invocation and use examples.
1.157     matthew  2275: 
1.35      matthew  2276: =cut
                   2277: 
                   2278: #-------------------------------------------
1.32      matthew  2279: sub authform_header{  
                   2280:     my %in = (
                   2281:         formname => 'cu',
1.80      albertel 2282:         kerb_def_dom => '',
1.32      matthew  2283:         @_,
                   2284:     );
                   2285:     $in{'formname'} = 'document.' . $in{'formname'};
                   2286:     my $result='';
1.80      albertel 2287: 
                   2288: #---------------------------------------------- Code for upper case translation
                   2289:     my $Javascript_toUpperCase;
                   2290:     unless ($in{kerb_def_dom}) {
                   2291:         $Javascript_toUpperCase =<<"END";
                   2292:         switch (choice) {
                   2293:            case 'krb': currentform.elements[choicearg].value =
                   2294:                currentform.elements[choicearg].value.toUpperCase();
                   2295:                break;
                   2296:            default:
                   2297:         }
                   2298: END
                   2299:     } else {
                   2300:         $Javascript_toUpperCase = "";
                   2301:     }
                   2302: 
1.165     raeburn  2303:     my $radioval = "'nochange'";
1.591     raeburn  2304:     if (defined($in{'curr_authtype'})) {
                   2305:         if ($in{'curr_authtype'} ne '') {
                   2306:             $radioval = "'".$in{'curr_authtype'}."arg'";
                   2307:         }
1.174     matthew  2308:     }
1.165     raeburn  2309:     my $argfield = 'null';
1.591     raeburn  2310:     if (defined($in{'mode'})) {
1.165     raeburn  2311:         if ($in{'mode'} eq 'modifycourse')  {
1.591     raeburn  2312:             if (defined($in{'curr_autharg'})) {
                   2313:                 if ($in{'curr_autharg'} ne '') {
1.165     raeburn  2314:                     $argfield = "'$in{'curr_autharg'}'";
                   2315:                 }
                   2316:             }
                   2317:         }
                   2318:     }
                   2319: 
1.32      matthew  2320:     $result.=<<"END";
                   2321: var current = new Object();
1.165     raeburn  2322: current.radiovalue = $radioval;
                   2323: current.argfield = $argfield;
1.32      matthew  2324: 
                   2325: function changed_radio(choice,currentform) {
                   2326:     var choicearg = choice + 'arg';
                   2327:     // If a radio button in changed, we need to change the argfield
                   2328:     if (current.radiovalue != choice) {
                   2329:         current.radiovalue = choice;
                   2330:         if (current.argfield != null) {
                   2331:             currentform.elements[current.argfield].value = '';
                   2332:         }
                   2333:         if (choice == 'nochange') {
                   2334:             current.argfield = null;
                   2335:         } else {
                   2336:             current.argfield = choicearg;
                   2337:             switch(choice) {
                   2338:                 case 'krb': 
                   2339:                     currentform.elements[current.argfield].value = 
                   2340:                         "$in{'kerb_def_dom'}";
                   2341:                 break;
                   2342:               default:
                   2343:                 break;
                   2344:             }
                   2345:         }
                   2346:     }
                   2347:     return;
                   2348: }
1.22      www      2349: 
1.32      matthew  2350: function changed_text(choice,currentform) {
                   2351:     var choicearg = choice + 'arg';
                   2352:     if (currentform.elements[choicearg].value !='') {
1.80      albertel 2353:         $Javascript_toUpperCase
1.32      matthew  2354:         // clear old field
                   2355:         if ((current.argfield != choicearg) && (current.argfield != null)) {
                   2356:             currentform.elements[current.argfield].value = '';
                   2357:         }
                   2358:         current.argfield = choicearg;
                   2359:     }
                   2360:     set_auth_radio_buttons(choice,currentform);
                   2361:     return;
1.20      www      2362: }
1.32      matthew  2363: 
                   2364: function set_auth_radio_buttons(newvalue,currentform) {
1.986     raeburn  2365:     var numauthchoices = currentform.login.length;
                   2366:     if (typeof numauthchoices  == "undefined") {
                   2367:         return;
                   2368:     } 
1.32      matthew  2369:     var i=0;
1.986     raeburn  2370:     while (i < numauthchoices) {
1.32      matthew  2371:         if (currentform.login[i].value == newvalue) { break; }
                   2372:         i++;
                   2373:     }
1.986     raeburn  2374:     if (i == numauthchoices) {
1.32      matthew  2375:         return;
                   2376:     }
                   2377:     current.radiovalue = newvalue;
                   2378:     currentform.login[i].checked = true;
                   2379:     return;
                   2380: }
                   2381: END
                   2382:     return $result;
                   2383: }
                   2384: 
                   2385: sub authform_authorwarning{
                   2386:     my $result='';
1.144     matthew  2387:     $result='<i>'.
                   2388:         &mt('As a general rule, only authors or co-authors should be '.
                   2389:             'filesystem authenticated '.
                   2390:             '(which allows access to the server filesystem).')."</i>\n";
1.32      matthew  2391:     return $result;
                   2392: }
                   2393: 
                   2394: sub authform_nochange{  
                   2395:     my %in = (
                   2396:               formname => 'document.cu',
                   2397:               kerb_def_dom => 'MSU.EDU',
                   2398:               @_,
                   2399:           );
1.586     raeburn  2400:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'}); 
                   2401:     my $result;
                   2402:     if (keys(%can_assign) == 0) {
                   2403:         $result = &mt('Under you current role you are not permitted to change login settings for this user');  
                   2404:     } else {
                   2405:         $result = '<label>'.&mt('[_1] Do not change login data',
                   2406:                   '<input type="radio" name="login" value="nochange" '.
                   2407:                   'checked="checked" onclick="'.
1.281     albertel 2408:             "javascript:changed_radio('nochange',$in{'formname'});".'" />').
                   2409: 	    '</label>';
1.586     raeburn  2410:     }
1.32      matthew  2411:     return $result;
                   2412: }
                   2413: 
1.591     raeburn  2414: sub authform_kerberos {
1.32      matthew  2415:     my %in = (
                   2416:               formname => 'document.cu',
                   2417:               kerb_def_dom => 'MSU.EDU',
1.80      albertel 2418:               kerb_def_auth => 'krb4',
1.32      matthew  2419:               @_,
                   2420:               );
1.586     raeburn  2421:     my ($check4,$check5,$krbcheck,$krbarg,$krbver,$result,$authtype,
                   2422:         $autharg,$jscall);
                   2423:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
1.80      albertel 2424:     if ($in{'kerb_def_auth'} eq 'krb5') {
1.772     bisitz   2425:        $check5 = ' checked="checked"';
1.80      albertel 2426:     } else {
1.772     bisitz   2427:        $check4 = ' checked="checked"';
1.80      albertel 2428:     }
1.165     raeburn  2429:     $krbarg = $in{'kerb_def_dom'};
1.591     raeburn  2430:     if (defined($in{'curr_authtype'})) {
                   2431:         if ($in{'curr_authtype'} eq 'krb') {
1.772     bisitz   2432:             $krbcheck = ' checked="checked"';
1.623     raeburn  2433:             if (defined($in{'mode'})) {
                   2434:                 if ($in{'mode'} eq 'modifyuser') {
                   2435:                     $krbcheck = '';
                   2436:                 }
                   2437:             }
1.591     raeburn  2438:             if (defined($in{'curr_kerb_ver'})) {
                   2439:                 if ($in{'curr_krb_ver'} eq '5') {
1.772     bisitz   2440:                     $check5 = ' checked="checked"';
1.591     raeburn  2441:                     $check4 = '';
                   2442:                 } else {
1.772     bisitz   2443:                     $check4 = ' checked="checked"';
1.591     raeburn  2444:                     $check5 = '';
                   2445:                 }
1.586     raeburn  2446:             }
1.591     raeburn  2447:             if (defined($in{'curr_autharg'})) {
1.165     raeburn  2448:                 $krbarg = $in{'curr_autharg'};
                   2449:             }
1.586     raeburn  2450:             if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
1.591     raeburn  2451:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2452:                     $result = 
                   2453:     &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
                   2454:         $in{'curr_autharg'},$krbver);
                   2455:                 } else {
                   2456:                     $result =
                   2457:     &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
                   2458:                 }
                   2459:                 return $result; 
                   2460:             }
                   2461:         }
                   2462:     } else {
                   2463:         if ($authnum == 1) {
1.784     bisitz   2464:             $authtype = '<input type="hidden" name="login" value="krb" />';
1.165     raeburn  2465:         }
                   2466:     }
1.586     raeburn  2467:     if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
                   2468:         return;
1.587     raeburn  2469:     } elsif ($authtype eq '') {
1.591     raeburn  2470:         if (defined($in{'mode'})) {
1.587     raeburn  2471:             if ($in{'mode'} eq 'modifycourse') {
                   2472:                 if ($authnum == 1) {
1.784     bisitz   2473:                     $authtype = '<input type="hidden" name="login" value="krb" />';
1.587     raeburn  2474:                 }
                   2475:             }
                   2476:         }
1.586     raeburn  2477:     }
                   2478:     $jscall = "javascript:changed_radio('krb',$in{'formname'});";
                   2479:     if ($authtype eq '') {
                   2480:         $authtype = '<input type="radio" name="login" value="krb" '.
                   2481:                     'onclick="'.$jscall.'" onchange="'.$jscall.'"'.
                   2482:                     $krbcheck.' />';
                   2483:     }
                   2484:     if (($can_assign{'krb4'} && $can_assign{'krb5'}) ||
                   2485:         ($can_assign{'krb4'} && !$can_assign{'krb5'} && 
                   2486:          $in{'curr_authtype'} eq 'krb5') ||
                   2487:         (!$can_assign{'krb4'} && $can_assign{'krb5'} && 
                   2488:          $in{'curr_authtype'} eq 'krb4')) {
                   2489:         $result .= &mt
1.144     matthew  2490:         ('[_1] Kerberos authenticated with domain [_2] '.
1.281     albertel 2491:          '[_3] Version 4 [_4] Version 5 [_5]',
1.586     raeburn  2492:          '<label>'.$authtype,
1.281     albertel 2493:          '</label><input type="text" size="10" name="krbarg" '.
1.165     raeburn  2494:              'value="'.$krbarg.'" '.
1.144     matthew  2495:              'onchange="'.$jscall.'" />',
1.281     albertel 2496:          '<label><input type="radio" name="krbver" value="4" '.$check4.' />',
                   2497:          '</label><label><input type="radio" name="krbver" value="5" '.$check5.' />',
                   2498: 	 '</label>');
1.586     raeburn  2499:     } elsif ($can_assign{'krb4'}) {
                   2500:         $result .= &mt
                   2501:         ('[_1] Kerberos authenticated with domain [_2] '.
                   2502:          '[_3] Version 4 [_4]',
                   2503:          '<label>'.$authtype,
                   2504:          '</label><input type="text" size="10" name="krbarg" '.
                   2505:              'value="'.$krbarg.'" '.
                   2506:              'onchange="'.$jscall.'" />',
                   2507:          '<label><input type="hidden" name="krbver" value="4" />',
                   2508:          '</label>');
                   2509:     } elsif ($can_assign{'krb5'}) {
                   2510:         $result .= &mt
                   2511:         ('[_1] Kerberos authenticated with domain [_2] '.
                   2512:          '[_3] Version 5 [_4]',
                   2513:          '<label>'.$authtype,
                   2514:          '</label><input type="text" size="10" name="krbarg" '.
                   2515:              'value="'.$krbarg.'" '.
                   2516:              'onchange="'.$jscall.'" />',
                   2517:          '<label><input type="hidden" name="krbver" value="5" />',
                   2518:          '</label>');
                   2519:     }
1.32      matthew  2520:     return $result;
                   2521: }
                   2522: 
                   2523: sub authform_internal{  
1.586     raeburn  2524:     my %in = (
1.32      matthew  2525:                 formname => 'document.cu',
                   2526:                 kerb_def_dom => 'MSU.EDU',
                   2527:                 @_,
                   2528:                 );
1.586     raeburn  2529:     my ($intcheck,$intarg,$result,$authtype,$autharg,$jscall);
                   2530:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
1.591     raeburn  2531:     if (defined($in{'curr_authtype'})) {
                   2532:         if ($in{'curr_authtype'} eq 'int') {
1.586     raeburn  2533:             if ($can_assign{'int'}) {
1.772     bisitz   2534:                 $intcheck = 'checked="checked" ';
1.623     raeburn  2535:                 if (defined($in{'mode'})) {
                   2536:                     if ($in{'mode'} eq 'modifyuser') {
                   2537:                         $intcheck = '';
                   2538:                     }
                   2539:                 }
1.591     raeburn  2540:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2541:                     $intarg = $in{'curr_autharg'};
                   2542:                 }
                   2543:             } else {
                   2544:                 $result = &mt('Currently internally authenticated.');
                   2545:                 return $result;
1.165     raeburn  2546:             }
                   2547:         }
1.586     raeburn  2548:     } else {
                   2549:         if ($authnum == 1) {
1.784     bisitz   2550:             $authtype = '<input type="hidden" name="login" value="int" />';
1.586     raeburn  2551:         }
                   2552:     }
                   2553:     if (!$can_assign{'int'}) {
                   2554:         return;
1.587     raeburn  2555:     } elsif ($authtype eq '') {
1.591     raeburn  2556:         if (defined($in{'mode'})) {
1.587     raeburn  2557:             if ($in{'mode'} eq 'modifycourse') {
                   2558:                 if ($authnum == 1) {
1.784     bisitz   2559:                     $authtype = '<input type="hidden" name="login" value="int" />';
1.587     raeburn  2560:                 }
                   2561:             }
                   2562:         }
1.165     raeburn  2563:     }
1.586     raeburn  2564:     $jscall = "javascript:changed_radio('int',$in{'formname'});";
                   2565:     if ($authtype eq '') {
                   2566:         $authtype = '<input type="radio" name="login" value="int" '.$intcheck.
                   2567:                     ' onchange="'.$jscall.'" onclick="'.$jscall.'" />';
                   2568:     }
1.605     bisitz   2569:     $autharg = '<input type="password" size="10" name="intarg" value="'.
1.586     raeburn  2570:                $intarg.'" onchange="'.$jscall.'" />';
                   2571:     $result = &mt
1.144     matthew  2572:         ('[_1] Internally authenticated (with initial password [_2])',
1.586     raeburn  2573:          '<label>'.$authtype,'</label>'.$autharg);
1.824     bisitz   2574:     $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  2575:     return $result;
                   2576: }
                   2577: 
                   2578: sub authform_local{  
                   2579:     my %in = (
                   2580:               formname => 'document.cu',
                   2581:               kerb_def_dom => 'MSU.EDU',
                   2582:               @_,
                   2583:               );
1.586     raeburn  2584:     my ($loccheck,$locarg,$result,$authtype,$autharg,$jscall);
                   2585:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
1.591     raeburn  2586:     if (defined($in{'curr_authtype'})) {
                   2587:         if ($in{'curr_authtype'} eq 'loc') {
1.586     raeburn  2588:             if ($can_assign{'loc'}) {
1.772     bisitz   2589:                 $loccheck = 'checked="checked" ';
1.623     raeburn  2590:                 if (defined($in{'mode'})) {
                   2591:                     if ($in{'mode'} eq 'modifyuser') {
                   2592:                         $loccheck = '';
                   2593:                     }
                   2594:                 }
1.591     raeburn  2595:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2596:                     $locarg = $in{'curr_autharg'};
                   2597:                 }
                   2598:             } else {
                   2599:                 $result = &mt('Currently using local (institutional) authentication.');
                   2600:                 return $result;
1.165     raeburn  2601:             }
                   2602:         }
1.586     raeburn  2603:     } else {
                   2604:         if ($authnum == 1) {
1.784     bisitz   2605:             $authtype = '<input type="hidden" name="login" value="loc" />';
1.586     raeburn  2606:         }
                   2607:     }
                   2608:     if (!$can_assign{'loc'}) {
                   2609:         return;
1.587     raeburn  2610:     } elsif ($authtype eq '') {
1.591     raeburn  2611:         if (defined($in{'mode'})) {
1.587     raeburn  2612:             if ($in{'mode'} eq 'modifycourse') {
                   2613:                 if ($authnum == 1) {
1.784     bisitz   2614:                     $authtype = '<input type="hidden" name="login" value="loc" />';
1.587     raeburn  2615:                 }
                   2616:             }
                   2617:         }
1.165     raeburn  2618:     }
1.586     raeburn  2619:     $jscall = "javascript:changed_radio('loc',$in{'formname'});";
                   2620:     if ($authtype eq '') {
                   2621:         $authtype = '<input type="radio" name="login" value="loc" '.
                   2622:                     $loccheck.' onchange="'.$jscall.'" onclick="'.
                   2623:                     $jscall.'" />';
                   2624:     }
                   2625:     $autharg = '<input type="text" size="10" name="locarg" value="'.
                   2626:                $locarg.'" onchange="'.$jscall.'" />';
                   2627:     $result = &mt('[_1] Local Authentication with argument [_2]',
                   2628:                   '<label>'.$authtype,'</label>'.$autharg);
1.32      matthew  2629:     return $result;
                   2630: }
                   2631: 
                   2632: sub authform_filesystem{  
                   2633:     my %in = (
                   2634:               formname => 'document.cu',
                   2635:               kerb_def_dom => 'MSU.EDU',
                   2636:               @_,
                   2637:               );
1.586     raeburn  2638:     my ($fsyscheck,$result,$authtype,$autharg,$jscall);
                   2639:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
1.591     raeburn  2640:     if (defined($in{'curr_authtype'})) {
                   2641:         if ($in{'curr_authtype'} eq 'fsys') {
1.586     raeburn  2642:             if ($can_assign{'fsys'}) {
1.772     bisitz   2643:                 $fsyscheck = 'checked="checked" ';
1.623     raeburn  2644:                 if (defined($in{'mode'})) {
                   2645:                     if ($in{'mode'} eq 'modifyuser') {
                   2646:                         $fsyscheck = '';
                   2647:                     }
                   2648:                 }
1.586     raeburn  2649:             } else {
                   2650:                 $result = &mt('Currently Filesystem Authenticated.');
                   2651:                 return $result;
                   2652:             }           
                   2653:         }
                   2654:     } else {
                   2655:         if ($authnum == 1) {
1.784     bisitz   2656:             $authtype = '<input type="hidden" name="login" value="fsys" />';
1.586     raeburn  2657:         }
                   2658:     }
                   2659:     if (!$can_assign{'fsys'}) {
                   2660:         return;
1.587     raeburn  2661:     } elsif ($authtype eq '') {
1.591     raeburn  2662:         if (defined($in{'mode'})) {
1.587     raeburn  2663:             if ($in{'mode'} eq 'modifycourse') {
                   2664:                 if ($authnum == 1) {
1.784     bisitz   2665:                     $authtype = '<input type="hidden" name="login" value="fsys" />';
1.587     raeburn  2666:                 }
                   2667:             }
                   2668:         }
1.586     raeburn  2669:     }
                   2670:     $jscall = "javascript:changed_radio('fsys',$in{'formname'});";
                   2671:     if ($authtype eq '') {
                   2672:         $authtype = '<input type="radio" name="login" value="fsys" '.
                   2673:                     $fsyscheck.' onchange="'.$jscall.'" onclick="'.
                   2674:                     $jscall.'" />';
                   2675:     }
                   2676:     $autharg = '<input type="text" size="10" name="fsysarg" value=""'.
                   2677:                ' onchange="'.$jscall.'" />';
                   2678:     $result = &mt
1.144     matthew  2679:         ('[_1] Filesystem Authenticated (with initial password [_2])',
1.281     albertel 2680:          '<label><input type="radio" name="login" value="fsys" '.
1.586     raeburn  2681:          $fsyscheck.'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1.605     bisitz   2682:          '</label><input type="password" size="10" name="fsysarg" value="" '.
1.144     matthew  2683:                   'onchange="'.$jscall.'" />');
1.32      matthew  2684:     return $result;
                   2685: }
                   2686: 
1.586     raeburn  2687: sub get_assignable_auth {
                   2688:     my ($dom) = @_;
                   2689:     if ($dom eq '') {
                   2690:         $dom = $env{'request.role.domain'};
                   2691:     }
                   2692:     my %can_assign = (
                   2693:                           krb4 => 1,
                   2694:                           krb5 => 1,
                   2695:                           int  => 1,
                   2696:                           loc  => 1,
                   2697:                      );
                   2698:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2699:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   2700:         if (ref($domconfig{'usercreation'}{'authtypes'}) eq 'HASH') {
                   2701:             my $authhash = $domconfig{'usercreation'}{'authtypes'};
                   2702:             my $context;
                   2703:             if ($env{'request.role'} =~ /^au/) {
                   2704:                 $context = 'author';
                   2705:             } elsif ($env{'request.role'} =~ /^dc/) {
                   2706:                 $context = 'domain';
                   2707:             } elsif ($env{'request.course.id'}) {
                   2708:                 $context = 'course';
                   2709:             }
                   2710:             if ($context) {
                   2711:                 if (ref($authhash->{$context}) eq 'HASH') {
                   2712:                    %can_assign = %{$authhash->{$context}}; 
                   2713:                 }
                   2714:             }
                   2715:         }
                   2716:     }
                   2717:     my $authnum = 0;
                   2718:     foreach my $key (keys(%can_assign)) {
                   2719:         if ($can_assign{$key}) {
                   2720:             $authnum ++;
                   2721:         }
                   2722:     }
                   2723:     if ($can_assign{'krb4'} && $can_assign{'krb5'}) {
                   2724:         $authnum --;
                   2725:     }
                   2726:     return ($authnum,%can_assign);
                   2727: }
                   2728: 
1.80      albertel 2729: ###############################################################
                   2730: ##    Get Kerberos Defaults for Domain                 ##
                   2731: ###############################################################
                   2732: ##
                   2733: ## Returns default kerberos version and an associated argument
                   2734: ## as listed in file domain.tab. If not listed, provides
                   2735: ## appropriate default domain and kerberos version.
                   2736: ##
                   2737: #-------------------------------------------
                   2738: 
                   2739: =pod
                   2740: 
1.648     raeburn  2741: =item * &get_kerberos_defaults()
1.80      albertel 2742: 
                   2743: get_kerberos_defaults($target_domain) returns the default kerberos
1.641     raeburn  2744: version and domain. If not found, it defaults to version 4 and the 
                   2745: domain of the server.
1.80      albertel 2746: 
1.648     raeburn  2747: =over 4
                   2748: 
1.80      albertel 2749: ($def_version, $def_krb_domain) = &get_kerberos_defaults($target_domain);
                   2750: 
1.648     raeburn  2751: =back
                   2752: 
                   2753: =back
                   2754: 
1.80      albertel 2755: =cut
                   2756: 
                   2757: #-------------------------------------------
                   2758: sub get_kerberos_defaults {
                   2759:     my $domain=shift;
1.641     raeburn  2760:     my ($krbdef,$krbdefdom);
                   2761:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                   2762:     if (($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) {
                   2763:         $krbdef = $domdefaults{'auth_def'};
                   2764:         $krbdefdom = $domdefaults{'auth_arg_def'};
                   2765:     } else {
1.80      albertel 2766:         $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
                   2767:         my $krbdefdom=$1;
                   2768:         $krbdefdom=~tr/a-z/A-Z/;
                   2769:         $krbdef = "krb4";
                   2770:     }
                   2771:     return ($krbdef,$krbdefdom);
                   2772: }
1.112     bowersj2 2773: 
1.32      matthew  2774: 
1.46      matthew  2775: ###############################################################
                   2776: ##                Thesaurus Functions                        ##
                   2777: ###############################################################
1.20      www      2778: 
1.46      matthew  2779: =pod
1.20      www      2780: 
1.112     bowersj2 2781: =head1 Thesaurus Functions
                   2782: 
                   2783: =over 4
                   2784: 
1.648     raeburn  2785: =item * &initialize_keywords()
1.46      matthew  2786: 
                   2787: Initializes the package variable %Keywords if it is empty.  Uses the
                   2788: package variable $thesaurus_db_file.
                   2789: 
                   2790: =cut
                   2791: 
                   2792: ###################################################
                   2793: 
                   2794: sub initialize_keywords {
                   2795:     return 1 if (scalar keys(%Keywords));
                   2796:     # If we are here, %Keywords is empty, so fill it up
                   2797:     #   Make sure the file we need exists...
                   2798:     if (! -e $thesaurus_db_file) {
                   2799:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file".
                   2800:                                  " failed because it does not exist");
                   2801:         return 0;
                   2802:     }
                   2803:     #   Set up the hash as a database
                   2804:     my %thesaurus_db;
                   2805:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 2806:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  2807:         &Apache::lonnet::logthis("Could not tie \%thesaurus_db to ".
                   2808:                                  $thesaurus_db_file);
                   2809:         return 0;
                   2810:     } 
                   2811:     #  Get the average number of appearances of a word.
                   2812:     my $avecount = $thesaurus_db{'average.count'};
                   2813:     #  Put keywords (those that appear > average) into %Keywords
                   2814:     while (my ($word,$data)=each (%thesaurus_db)) {
                   2815:         my ($count,undef) = split /:/,$data;
                   2816:         $Keywords{$word}++ if ($count > $avecount);
                   2817:     }
                   2818:     untie %thesaurus_db;
                   2819:     # Remove special values from %Keywords.
1.356     albertel 2820:     foreach my $value ('total.count','average.count') {
                   2821:         delete($Keywords{$value}) if (exists($Keywords{$value}));
1.586     raeburn  2822:   }
1.46      matthew  2823:     return 1;
                   2824: }
                   2825: 
                   2826: ###################################################
                   2827: 
                   2828: =pod
                   2829: 
1.648     raeburn  2830: =item * &keyword($word)
1.46      matthew  2831: 
                   2832: Returns true if $word is a keyword.  A keyword is a word that appears more 
                   2833: than the average number of times in the thesaurus database.  Calls 
                   2834: &initialize_keywords
                   2835: 
                   2836: =cut
                   2837: 
                   2838: ###################################################
1.20      www      2839: 
                   2840: sub keyword {
1.46      matthew  2841:     return if (!&initialize_keywords());
                   2842:     my $word=lc(shift());
                   2843:     $word=~s/\W//g;
                   2844:     return exists($Keywords{$word});
1.20      www      2845: }
1.46      matthew  2846: 
                   2847: ###############################################################
                   2848: 
                   2849: =pod 
1.20      www      2850: 
1.648     raeburn  2851: =item * &get_related_words()
1.46      matthew  2852: 
1.160     matthew  2853: Look up a word in the thesaurus.  Takes a scalar argument and returns
1.46      matthew  2854: an array of words.  If the keyword is not in the thesaurus, an empty array
                   2855: will be returned.  The order of the words returned is determined by the
                   2856: database which holds them.
                   2857: 
                   2858: Uses global $thesaurus_db_file.
                   2859: 
1.1057    foxr     2860: 
1.46      matthew  2861: =cut
                   2862: 
                   2863: ###############################################################
                   2864: sub get_related_words {
                   2865:     my $keyword = shift;
                   2866:     my %thesaurus_db;
                   2867:     if (! -e $thesaurus_db_file) {
                   2868:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file ".
                   2869:                                  "failed because the file does not exist");
                   2870:         return ();
                   2871:     }
                   2872:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 2873:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  2874:         return ();
                   2875:     } 
                   2876:     my @Words=();
1.429     www      2877:     my $count=0;
1.46      matthew  2878:     if (exists($thesaurus_db{$keyword})) {
1.356     albertel 2879: 	# The first element is the number of times
                   2880: 	# the word appears.  We do not need it now.
1.429     www      2881: 	my (undef,@RelatedWords) = (split(/:/,$thesaurus_db{$keyword}));
                   2882: 	my (undef,$mostfrequentcount)=split(/\,/,$RelatedWords[0]);
                   2883: 	my $threshold=$mostfrequentcount/10;
                   2884:         foreach my $possibleword (@RelatedWords) {
                   2885:             my ($word,$wordcount)=split(/\,/,$possibleword);
                   2886:             if ($wordcount>$threshold) {
                   2887: 		push(@Words,$word);
                   2888:                 $count++;
                   2889:                 if ($count>10) { last; }
                   2890: 	    }
1.20      www      2891:         }
                   2892:     }
1.46      matthew  2893:     untie %thesaurus_db;
                   2894:     return @Words;
1.14      harris41 2895: }
1.46      matthew  2896: 
1.112     bowersj2 2897: =pod
                   2898: 
                   2899: =back
                   2900: 
                   2901: =cut
1.61      www      2902: 
                   2903: # -------------------------------------------------------------- Plaintext name
1.81      albertel 2904: =pod
                   2905: 
1.112     bowersj2 2906: =head1 User Name Functions
                   2907: 
                   2908: =over 4
                   2909: 
1.648     raeburn  2910: =item * &plainname($uname,$udom,$first)
1.81      albertel 2911: 
1.112     bowersj2 2912: Takes a users logon name and returns it as a string in
1.226     albertel 2913: "first middle last generation" form 
                   2914: if $first is set to 'lastname' then it returns it as
                   2915: 'lastname generation, firstname middlename' if their is a lastname
1.81      albertel 2916: 
                   2917: =cut
1.61      www      2918: 
1.295     www      2919: 
1.81      albertel 2920: ###############################################################
1.61      www      2921: sub plainname {
1.226     albertel 2922:     my ($uname,$udom,$first)=@_;
1.537     albertel 2923:     return if (!defined($uname) || !defined($udom));
1.295     www      2924:     my %names=&getnames($uname,$udom);
1.226     albertel 2925:     my $name=&Apache::lonnet::format_name($names{'firstname'},
                   2926: 					  $names{'middlename'},
                   2927: 					  $names{'lastname'},
                   2928: 					  $names{'generation'},$first);
                   2929:     $name=~s/^\s+//;
1.62      www      2930:     $name=~s/\s+$//;
                   2931:     $name=~s/\s+/ /g;
1.353     albertel 2932:     if ($name !~ /\S/) { $name=$uname.':'.$udom; }
1.62      www      2933:     return $name;
1.61      www      2934: }
1.66      www      2935: 
                   2936: # -------------------------------------------------------------------- Nickname
1.81      albertel 2937: =pod
                   2938: 
1.648     raeburn  2939: =item * &nickname($uname,$udom)
1.81      albertel 2940: 
                   2941: Gets a users name and returns it as a string as
                   2942: 
                   2943: "&quot;nickname&quot;"
1.66      www      2944: 
1.81      albertel 2945: if the user has a nickname or
                   2946: 
                   2947: "first middle last generation"
                   2948: 
                   2949: if the user does not
                   2950: 
                   2951: =cut
1.66      www      2952: 
                   2953: sub nickname {
                   2954:     my ($uname,$udom)=@_;
1.537     albertel 2955:     return if (!defined($uname) || !defined($udom));
1.295     www      2956:     my %names=&getnames($uname,$udom);
1.68      albertel 2957:     my $name=$names{'nickname'};
1.66      www      2958:     if ($name) {
                   2959:        $name='&quot;'.$name.'&quot;'; 
                   2960:     } else {
                   2961:        $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
                   2962: 	     $names{'lastname'}.' '.$names{'generation'};
                   2963:        $name=~s/\s+$//;
                   2964:        $name=~s/\s+/ /g;
                   2965:     }
                   2966:     return $name;
                   2967: }
                   2968: 
1.295     www      2969: sub getnames {
                   2970:     my ($uname,$udom)=@_;
1.537     albertel 2971:     return if (!defined($uname) || !defined($udom));
1.433     albertel 2972:     if ($udom eq 'public' && $uname eq 'public') {
                   2973: 	return ('lastname' => &mt('Public'));
                   2974:     }
1.295     www      2975:     my $id=$uname.':'.$udom;
                   2976:     my ($names,$cached)=&Apache::lonnet::is_cached_new('namescache',$id);
                   2977:     if ($cached) {
                   2978: 	return %{$names};
                   2979:     } else {
                   2980: 	my %loadnames=&Apache::lonnet::get('environment',
                   2981:                     ['firstname','middlename','lastname','generation','nickname'],
                   2982: 					 $udom,$uname);
                   2983: 	&Apache::lonnet::do_cache_new('namescache',$id,\%loadnames);
                   2984: 	return %loadnames;
                   2985:     }
                   2986: }
1.61      www      2987: 
1.542     raeburn  2988: # -------------------------------------------------------------------- getemails
1.648     raeburn  2989: 
1.542     raeburn  2990: =pod
                   2991: 
1.648     raeburn  2992: =item * &getemails($uname,$udom)
1.542     raeburn  2993: 
                   2994: Gets a user's email information and returns it as a hash with keys:
                   2995: notification, critnotification, permanentemail
                   2996: 
                   2997: For notification and critnotification, values are comma-separated lists 
1.648     raeburn  2998: of e-mail addresses; for permanentemail, value is a single e-mail address.
1.542     raeburn  2999:  
1.648     raeburn  3000: 
1.542     raeburn  3001: =cut
                   3002: 
1.648     raeburn  3003: 
1.466     albertel 3004: sub getemails {
                   3005:     my ($uname,$udom)=@_;
                   3006:     if ($udom eq 'public' && $uname eq 'public') {
                   3007: 	return;
                   3008:     }
1.467     www      3009:     if (!$udom) { $udom=$env{'user.domain'}; }
                   3010:     if (!$uname) { $uname=$env{'user.name'}; }
1.466     albertel 3011:     my $id=$uname.':'.$udom;
                   3012:     my ($names,$cached)=&Apache::lonnet::is_cached_new('emailscache',$id);
                   3013:     if ($cached) {
                   3014: 	return %{$names};
                   3015:     } else {
                   3016: 	my %loadnames=&Apache::lonnet::get('environment',
                   3017:                     			   ['notification','critnotification',
                   3018: 					    'permanentemail'],
                   3019: 					   $udom,$uname);
                   3020: 	&Apache::lonnet::do_cache_new('emailscache',$id,\%loadnames);
                   3021: 	return %loadnames;
                   3022:     }
                   3023: }
                   3024: 
1.551     albertel 3025: sub flush_email_cache {
                   3026:     my ($uname,$udom)=@_;
                   3027:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   3028:     if (!$uname) { $uname=$env{'user.name'};   }
                   3029:     return if ($udom eq 'public' && $uname eq 'public');
                   3030:     my $id=$uname.':'.$udom;
                   3031:     &Apache::lonnet::devalidate_cache_new('emailscache',$id);
                   3032: }
                   3033: 
1.728     raeburn  3034: # -------------------------------------------------------------------- getlangs
                   3035: 
                   3036: =pod
                   3037: 
                   3038: =item * &getlangs($uname,$udom)
                   3039: 
                   3040: Gets a user's language preference and returns it as a hash with key:
                   3041: language.
                   3042: 
                   3043: =cut
                   3044: 
                   3045: 
                   3046: sub getlangs {
                   3047:     my ($uname,$udom) = @_;
                   3048:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   3049:     if (!$uname) { $uname=$env{'user.name'};   }
                   3050:     my $id=$uname.':'.$udom;
                   3051:     my ($langs,$cached)=&Apache::lonnet::is_cached_new('userlangs',$id);
                   3052:     if ($cached) {
                   3053:         return %{$langs};
                   3054:     } else {
                   3055:         my %loadlangs=&Apache::lonnet::get('environment',['languages'],
                   3056:                                            $udom,$uname);
                   3057:         &Apache::lonnet::do_cache_new('userlangs',$id,\%loadlangs);
                   3058:         return %loadlangs;
                   3059:     }
                   3060: }
                   3061: 
                   3062: sub flush_langs_cache {
                   3063:     my ($uname,$udom)=@_;
                   3064:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   3065:     if (!$uname) { $uname=$env{'user.name'};   }
                   3066:     return if ($udom eq 'public' && $uname eq 'public');
                   3067:     my $id=$uname.':'.$udom;
                   3068:     &Apache::lonnet::devalidate_cache_new('userlangs',$id);
                   3069: }
                   3070: 
1.61      www      3071: # ------------------------------------------------------------------ Screenname
1.81      albertel 3072: 
                   3073: =pod
                   3074: 
1.648     raeburn  3075: =item * &screenname($uname,$udom)
1.81      albertel 3076: 
                   3077: Gets a users screenname and returns it as a string
                   3078: 
                   3079: =cut
1.61      www      3080: 
                   3081: sub screenname {
                   3082:     my ($uname,$udom)=@_;
1.258     albertel 3083:     if ($uname eq $env{'user.name'} &&
                   3084: 	$udom eq $env{'user.domain'}) {return $env{'environment.screenname'};}
1.212     albertel 3085:     my %names=&Apache::lonnet::get('environment',['screenname'],$udom,$uname);
1.68      albertel 3086:     return $names{'screenname'};
1.62      www      3087: }
                   3088: 
1.212     albertel 3089: 
1.802     bisitz   3090: # ------------------------------------------------------------- Confirm Wrapper
                   3091: =pod
                   3092: 
                   3093: =item confirmwrapper
                   3094: 
                   3095: Wrap messages about completion of operation in box
                   3096: 
                   3097: =cut
                   3098: 
                   3099: sub confirmwrapper {
                   3100:     my ($message)=@_;
                   3101:     if ($message) {
                   3102:         return "\n".'<div class="LC_confirm_box">'."\n"
                   3103:                .$message."\n"
                   3104:                .'</div>'."\n";
                   3105:     } else {
                   3106:         return $message;
                   3107:     }
                   3108: }
                   3109: 
1.62      www      3110: # ------------------------------------------------------------- Message Wrapper
                   3111: 
                   3112: sub messagewrapper {
1.369     www      3113:     my ($link,$username,$domain,$subject,$text)=@_;
1.62      www      3114:     return 
1.441     albertel 3115:         '<a href="/adm/email?compose=individual&amp;'.
                   3116:         'recname='.$username.'&amp;recdom='.$domain.
                   3117: 	'&amp;subject='.&escape($subject).'&amp;text='.&escape($text).'" '.
1.200     matthew  3118:         'title="'.&mt('Send message').'">'.$link.'</a>';
1.74      www      3119: }
1.802     bisitz   3120: 
1.74      www      3121: # --------------------------------------------------------------- Notes Wrapper
                   3122: 
                   3123: sub noteswrapper {
                   3124:     my ($link,$un,$do)=@_;
                   3125:     return 
1.896     amueller 3126: "<a href='/adm/email?recordftf=retrieve&amp;recname=$un&amp;recdom=$do'>$link</a>";
1.62      www      3127: }
1.802     bisitz   3128: 
1.62      www      3129: # ------------------------------------------------------------- Aboutme Wrapper
                   3130: 
                   3131: sub aboutmewrapper {
1.166     www      3132:     my ($link,$username,$domain,$target)=@_;
1.447     raeburn  3133:     if (!defined($username)  && !defined($domain)) {
                   3134:         return;
                   3135:     }
1.892     amueller 3136:     return '<a href="/adm/'.$domain.'/'.$username.'/aboutme?forcestudent=1"'.
1.756     weissno  3137: 	($target?' target="$target"':'').' title="'.&mt("View this user's personal information page").'">'.$link.'</a>';
1.62      www      3138: }
                   3139: 
                   3140: # ------------------------------------------------------------ Syllabus Wrapper
                   3141: 
                   3142: sub syllabuswrapper {
1.707     bisitz   3143:     my ($linktext,$coursedir,$domain)=@_;
1.208     matthew  3144:     return qq{<a href="/public/$domain/$coursedir/syllabus">$linktext</a>};
1.61      www      3145: }
1.14      harris41 3146: 
1.802     bisitz   3147: # -----------------------------------------------------------------------------
                   3148: 
1.208     matthew  3149: sub track_student_link {
1.887     raeburn  3150:     my ($linktext,$sname,$sdom,$target,$start,$only_body) = @_;
1.268     albertel 3151:     my $link ="/adm/trackstudent?";
1.208     matthew  3152:     my $title = 'View recent activity';
                   3153:     if (defined($sname) && $sname !~ /^\s*$/ &&
                   3154:         defined($sdom)  && $sdom  !~ /^\s*$/) {
1.268     albertel 3155:         $link .= "selected_student=$sname:$sdom";
1.208     matthew  3156:         $title .= ' of this student';
1.268     albertel 3157:     } 
1.208     matthew  3158:     if (defined($target) && $target !~ /^\s*$/) {
                   3159:         $target = qq{target="$target"};
                   3160:     } else {
                   3161:         $target = '';
                   3162:     }
1.268     albertel 3163:     if ($start) { $link.='&amp;start='.$start; }
1.887     raeburn  3164:     if ($only_body) { $link .= '&amp;only_body=1'; }
1.554     albertel 3165:     $title = &mt($title);
                   3166:     $linktext = &mt($linktext);
1.448     albertel 3167:     return qq{<a href="$link" title="$title" $target>$linktext</a>}.
                   3168: 	&help_open_topic('View_recent_activity');
1.208     matthew  3169: }
                   3170: 
1.781     raeburn  3171: sub slot_reservations_link {
                   3172:     my ($linktext,$sname,$sdom,$target) = @_;
                   3173:     my $link ="/adm/slotrequest?command=showresv&amp;origin=aboutme";
                   3174:     my $title = 'View slot reservation history';
                   3175:     if (defined($sname) && $sname !~ /^\s*$/ &&
                   3176:         defined($sdom)  && $sdom  !~ /^\s*$/) {
                   3177:         $link .= "&amp;uname=$sname&amp;udom=$sdom";
                   3178:         $title .= ' of this student';
                   3179:     }
                   3180:     if (defined($target) && $target !~ /^\s*$/) {
                   3181:         $target = qq{target="$target"};
                   3182:     } else {
                   3183:         $target = '';
                   3184:     }
                   3185:     $title = &mt($title);
                   3186:     $linktext = &mt($linktext);
                   3187:     return qq{<a href="$link" title="$title" $target>$linktext</a>};
                   3188: # FIXME uncomment when help item created: &help_open_topic('Slot_Reservation_History');
                   3189: 
                   3190: }
                   3191: 
1.508     www      3192: # ===================================================== Display a student photo
                   3193: 
                   3194: 
1.509     albertel 3195: sub student_image_tag {
1.508     www      3196:     my ($domain,$user)=@_;
                   3197:     my $imgsrc=&Apache::lonnet::studentphoto($domain,$user,'jpg');
                   3198:     if (($imgsrc) && ($imgsrc ne '/adm/lonKaputt/lonlogo_broken.gif')) {
                   3199: 	return '<img src="'.$imgsrc.'" align="right" />';
                   3200:     } else {
                   3201: 	return '';
                   3202:     }
                   3203: }
                   3204: 
1.112     bowersj2 3205: =pod
                   3206: 
                   3207: =back
                   3208: 
                   3209: =head1 Access .tab File Data
                   3210: 
                   3211: =over 4
                   3212: 
1.648     raeburn  3213: =item * &languageids() 
1.112     bowersj2 3214: 
                   3215: returns list of all language ids
                   3216: 
                   3217: =cut
                   3218: 
1.14      harris41 3219: sub languageids {
1.16      harris41 3220:     return sort(keys(%language));
1.14      harris41 3221: }
                   3222: 
1.112     bowersj2 3223: =pod
                   3224: 
1.648     raeburn  3225: =item * &languagedescription() 
1.112     bowersj2 3226: 
                   3227: returns description of a specified language id
                   3228: 
                   3229: =cut
                   3230: 
1.14      harris41 3231: sub languagedescription {
1.125     www      3232:     my $code=shift;
                   3233:     return  ($supported_language{$code}?'* ':'').
                   3234:             $language{$code}.
1.126     www      3235: 	    ($supported_language{$code}?' ('.&mt('interface available').')':'');
1.145     www      3236: }
                   3237: 
1.1048    foxr     3238: =pod
                   3239: 
                   3240: =item * &plainlanguagedescription
                   3241: 
                   3242: Returns both the plain language description (e.g. 'Creoles and Pidgins, English-based (Other)')
                   3243: and the language character encoding (e.g. ISO) separated by a ' - ' string.
                   3244: 
                   3245: =cut
                   3246: 
1.145     www      3247: sub plainlanguagedescription {
                   3248:     my $code=shift;
                   3249:     return $language{$code};
                   3250: }
                   3251: 
1.1048    foxr     3252: =pod
                   3253: 
                   3254: =item * &supportedlanguagecode
                   3255: 
                   3256: Returns the supported language code (e.g. sptutf maps to pt) given a language
                   3257: code.
                   3258: 
                   3259: =cut
                   3260: 
1.145     www      3261: sub supportedlanguagecode {
                   3262:     my $code=shift;
                   3263:     return $supported_language{$code};
1.97      www      3264: }
                   3265: 
1.112     bowersj2 3266: =pod
                   3267: 
1.1048    foxr     3268: =item * &latexlanguage()
                   3269: 
                   3270: Given a language key code returns the correspondnig language to use
                   3271: to select the correct hyphenation on LaTeX printouts.  This is undef if there
                   3272: is no supported hyphenation for the language code.
                   3273: 
                   3274: =cut
                   3275: 
                   3276: sub latexlanguage {
                   3277:     my $code = shift;
                   3278:     return $latex_language{$code};
                   3279: }
                   3280: 
                   3281: =pod
                   3282: 
                   3283: =item * &latexhyphenation()
                   3284: 
                   3285: Same as above but what's supplied is the language as it might be stored
                   3286: in the metadata.
                   3287: 
                   3288: =cut
                   3289: 
                   3290: sub latexhyphenation {
                   3291:     my $key = shift;
                   3292:     return $latex_language_bykey{$key};
                   3293: }
                   3294: 
                   3295: =pod
                   3296: 
1.648     raeburn  3297: =item * &copyrightids() 
1.112     bowersj2 3298: 
                   3299: returns list of all copyrights
                   3300: 
                   3301: =cut
                   3302: 
                   3303: sub copyrightids {
                   3304:     return sort(keys(%cprtag));
                   3305: }
                   3306: 
                   3307: =pod
                   3308: 
1.648     raeburn  3309: =item * &copyrightdescription() 
1.112     bowersj2 3310: 
                   3311: returns description of a specified copyright id
                   3312: 
                   3313: =cut
                   3314: 
                   3315: sub copyrightdescription {
1.166     www      3316:     return &mt($cprtag{shift(@_)});
1.112     bowersj2 3317: }
1.197     matthew  3318: 
                   3319: =pod
                   3320: 
1.648     raeburn  3321: =item * &source_copyrightids() 
1.192     taceyjo1 3322: 
                   3323: returns list of all source copyrights
                   3324: 
                   3325: =cut
                   3326: 
                   3327: sub source_copyrightids {
                   3328:     return sort(keys(%scprtag));
                   3329: }
                   3330: 
                   3331: =pod
                   3332: 
1.648     raeburn  3333: =item * &source_copyrightdescription() 
1.192     taceyjo1 3334: 
                   3335: returns description of a specified source copyright id
                   3336: 
                   3337: =cut
                   3338: 
                   3339: sub source_copyrightdescription {
                   3340:     return &mt($scprtag{shift(@_)});
                   3341: }
1.112     bowersj2 3342: 
                   3343: =pod
                   3344: 
1.648     raeburn  3345: =item * &filecategories() 
1.112     bowersj2 3346: 
                   3347: returns list of all file categories
                   3348: 
                   3349: =cut
                   3350: 
                   3351: sub filecategories {
                   3352:     return sort(keys(%category_extensions));
                   3353: }
                   3354: 
                   3355: =pod
                   3356: 
1.648     raeburn  3357: =item * &filecategorytypes() 
1.112     bowersj2 3358: 
                   3359: returns list of file types belonging to a given file
                   3360: category
                   3361: 
                   3362: =cut
                   3363: 
                   3364: sub filecategorytypes {
1.356     albertel 3365:     my ($cat) = @_;
                   3366:     return @{$category_extensions{lc($cat)}};
1.112     bowersj2 3367: }
                   3368: 
                   3369: =pod
                   3370: 
1.648     raeburn  3371: =item * &fileembstyle() 
1.112     bowersj2 3372: 
                   3373: returns embedding style for a specified file type
                   3374: 
                   3375: =cut
                   3376: 
                   3377: sub fileembstyle {
                   3378:     return $fe{lc(shift(@_))};
1.169     www      3379: }
                   3380: 
1.351     www      3381: sub filemimetype {
                   3382:     return $fm{lc(shift(@_))};
                   3383: }
                   3384: 
1.169     www      3385: 
                   3386: sub filecategoryselect {
                   3387:     my ($name,$value)=@_;
1.189     matthew  3388:     return &select_form($value,$name,
1.970     raeburn  3389:                         {'' => &mt('Any category'), map { $_,$_ } sort(keys(%category_extensions))});
1.112     bowersj2 3390: }
                   3391: 
                   3392: =pod
                   3393: 
1.648     raeburn  3394: =item * &filedescription() 
1.112     bowersj2 3395: 
                   3396: returns description for a specified file type
                   3397: 
                   3398: =cut
                   3399: 
                   3400: sub filedescription {
1.188     matthew  3401:     my $file_description = $fd{lc(shift())};
                   3402:     $file_description =~ s:([\[\]]):~$1:g;
                   3403:     return &mt($file_description);
1.112     bowersj2 3404: }
                   3405: 
                   3406: =pod
                   3407: 
1.648     raeburn  3408: =item * &filedescriptionex() 
1.112     bowersj2 3409: 
                   3410: returns description for a specified file type with
                   3411: extra formatting
                   3412: 
                   3413: =cut
                   3414: 
                   3415: sub filedescriptionex {
                   3416:     my $ex=shift;
1.188     matthew  3417:     my $file_description = $fd{lc($ex)};
                   3418:     $file_description =~ s:([\[\]]):~$1:g;
                   3419:     return '.'.$ex.' '.&mt($file_description);
1.112     bowersj2 3420: }
                   3421: 
                   3422: # End of .tab access
                   3423: =pod
                   3424: 
                   3425: =back
                   3426: 
                   3427: =cut
                   3428: 
                   3429: # ------------------------------------------------------------------ File Types
                   3430: sub fileextensions {
                   3431:     return sort(keys(%fe));
                   3432: }
                   3433: 
1.97      www      3434: # ----------------------------------------------------------- Display Languages
                   3435: # returns a hash with all desired display languages
                   3436: #
                   3437: 
                   3438: sub display_languages {
                   3439:     my %languages=();
1.695     raeburn  3440:     foreach my $lang (&Apache::lonlocal::preferred_languages()) {
1.356     albertel 3441: 	$languages{$lang}=1;
1.97      www      3442:     }
                   3443:     &get_unprocessed_cgi($ENV{'QUERY_STRING'},['displaylanguage']);
1.258     albertel 3444:     if ($env{'form.displaylanguage'}) {
1.356     albertel 3445: 	foreach my $lang (split(/\s*(\,|\;|\:)\s*/,$env{'form.displaylanguage'})) {
                   3446: 	    $languages{$lang}=1;
1.97      www      3447:         }
                   3448:     }
                   3449:     return %languages;
1.14      harris41 3450: }
                   3451: 
1.582     albertel 3452: sub languages {
                   3453:     my ($possible_langs) = @_;
1.695     raeburn  3454:     my @preferred_langs = &Apache::lonlocal::preferred_languages();
1.582     albertel 3455:     if (!ref($possible_langs)) {
                   3456: 	if( wantarray ) {
                   3457: 	    return @preferred_langs;
                   3458: 	} else {
                   3459: 	    return $preferred_langs[0];
                   3460: 	}
                   3461:     }
                   3462:     my %possibilities = map { $_ => 1 } (@$possible_langs);
                   3463:     my @preferred_possibilities;
                   3464:     foreach my $preferred_lang (@preferred_langs) {
                   3465: 	if (exists($possibilities{$preferred_lang})) {
                   3466: 	    push(@preferred_possibilities, $preferred_lang);
                   3467: 	}
                   3468:     }
                   3469:     if( wantarray ) {
                   3470: 	return @preferred_possibilities;
                   3471:     }
                   3472:     return $preferred_possibilities[0];
                   3473: }
                   3474: 
1.742     raeburn  3475: sub user_lang {
                   3476:     my ($touname,$toudom,$fromcid) = @_;
                   3477:     my @userlangs;
                   3478:     if (($fromcid ne '') && ($env{'course.'.$fromcid.'.languages'} ne '')) {
                   3479:         @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
                   3480:                     $env{'course.'.$fromcid.'.languages'}));
                   3481:     } else {
                   3482:         my %langhash = &getlangs($touname,$toudom);
                   3483:         if ($langhash{'languages'} ne '') {
                   3484:             @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
                   3485:         } else {
                   3486:             my %domdefs = &Apache::lonnet::get_domain_defaults($toudom);
                   3487:             if ($domdefs{'lang_def'} ne '') {
                   3488:                 @userlangs = ($domdefs{'lang_def'});
                   3489:             }
                   3490:         }
                   3491:     }
                   3492:     my @languages=&Apache::lonlocal::get_genlanguages(@userlangs);
                   3493:     my $user_lh = Apache::localize->get_handle(@languages);
                   3494:     return $user_lh;
                   3495: }
                   3496: 
                   3497: 
1.112     bowersj2 3498: ###############################################################
                   3499: ##               Student Answer Attempts                     ##
                   3500: ###############################################################
                   3501: 
                   3502: =pod
                   3503: 
                   3504: =head1 Alternate Problem Views
                   3505: 
                   3506: =over 4
                   3507: 
1.648     raeburn  3508: =item * &get_previous_attempt($symb, $username, $domain, $course,
1.112     bowersj2 3509:     $getattempt, $regexp, $gradesub)
                   3510: 
                   3511: Return string with previous attempt on problem. Arguments:
                   3512: 
                   3513: =over 4
                   3514: 
                   3515: =item * $symb: Problem, including path
                   3516: 
                   3517: =item * $username: username of the desired student
                   3518: 
                   3519: =item * $domain: domain of the desired student
1.14      harris41 3520: 
1.112     bowersj2 3521: =item * $course: Course ID
1.14      harris41 3522: 
1.112     bowersj2 3523: =item * $getattempt: Leave blank for all attempts, otherwise put
                   3524:     something
1.14      harris41 3525: 
1.112     bowersj2 3526: =item * $regexp: if string matches this regexp, the string will be
                   3527:     sent to $gradesub
1.14      harris41 3528: 
1.112     bowersj2 3529: =item * $gradesub: routine that processes the string if it matches $regexp
1.14      harris41 3530: 
1.112     bowersj2 3531: =back
1.14      harris41 3532: 
1.112     bowersj2 3533: The output string is a table containing all desired attempts, if any.
1.16      harris41 3534: 
1.112     bowersj2 3535: =cut
1.1       albertel 3536: 
                   3537: sub get_previous_attempt {
1.43      ng       3538:   my ($symb,$username,$domain,$course,$getattempt,$regexp,$gradesub)=@_;
1.1       albertel 3539:   my $prevattempts='';
1.43      ng       3540:   no strict 'refs';
1.1       albertel 3541:   if ($symb) {
1.3       albertel 3542:     my (%returnhash)=
                   3543:       &Apache::lonnet::restore($symb,$course,$domain,$username);
1.1       albertel 3544:     if ($returnhash{'version'}) {
                   3545:       my %lasthash=();
                   3546:       my $version;
                   3547:       for ($version=1;$version<=$returnhash{'version'};$version++) {
1.356     albertel 3548:         foreach my $key (sort(split(/\:/,$returnhash{$version.':keys'}))) {
                   3549: 	  $lasthash{$key}=$returnhash{$version.':'.$key};
1.19      harris41 3550:         }
1.1       albertel 3551:       }
1.596     albertel 3552:       $prevattempts=&start_data_table().&start_data_table_header_row();
                   3553:       $prevattempts.='<th>'.&mt('History').'</th>';
1.978     raeburn  3554:       my (%typeparts,%lasthidden);
1.945     raeburn  3555:       my $showsurv=&Apache::lonnet::allowed('vas',$env{'request.course.id'});
1.356     albertel 3556:       foreach my $key (sort(keys(%lasthash))) {
                   3557: 	my ($ign,@parts) = split(/\./,$key);
1.41      ng       3558: 	if ($#parts > 0) {
1.31      albertel 3559: 	  my $data=$parts[-1];
1.989     raeburn  3560:           next if ($data eq 'foilorder');
1.31      albertel 3561: 	  pop(@parts);
1.1010    www      3562:           $prevattempts.='<th>'.&mt('Part ').join('.',@parts).'<br />'.$data.'&nbsp;</th>';
1.945     raeburn  3563:           if ($data eq 'type') {
                   3564:               unless ($showsurv) {
                   3565:                   my $id = join(',',@parts);
                   3566:                   $typeparts{$ign.'.'.$id} = $lasthash{$key};
1.978     raeburn  3567:                   if (($lasthash{$key} eq 'anonsurvey') || ($lasthash{$key} eq 'anonsurveycred')) {
                   3568:                       $lasthidden{$ign.'.'.$id} = 1;
                   3569:                   }
1.945     raeburn  3570:               }
1.1010    www      3571:           } 
1.31      albertel 3572: 	} else {
1.41      ng       3573: 	  if ($#parts == 0) {
                   3574: 	    $prevattempts.='<th>'.$parts[0].'</th>';
                   3575: 	  } else {
                   3576: 	    $prevattempts.='<th>'.$ign.'</th>';
                   3577: 	  }
1.31      albertel 3578: 	}
1.16      harris41 3579:       }
1.596     albertel 3580:       $prevattempts.=&end_data_table_header_row();
1.40      ng       3581:       if ($getattempt eq '') {
                   3582: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
1.945     raeburn  3583:             my @hidden;
                   3584:             if (%typeparts) {
                   3585:                 foreach my $id (keys(%typeparts)) {
                   3586:                     if (($returnhash{$version.':'.$id.'.type'} eq 'anonsurvey') || ($returnhash{$version.':'.$id.'.type'} eq 'anonsurveycred')) {
                   3587:                         push(@hidden,$id);
                   3588:                     }
                   3589:                 }
                   3590:             }
                   3591:             $prevattempts.=&start_data_table_row().
                   3592:                            '<td>'.&mt('Transaction [_1]',$version).'</td>';
                   3593:             if (@hidden) {
                   3594:                 foreach my $key (sort(keys(%lasthash))) {
1.989     raeburn  3595:                     next if ($key =~ /\.foilorder$/);
1.945     raeburn  3596:                     my $hide;
                   3597:                     foreach my $id (@hidden) {
                   3598:                         if ($key =~ /^\Q$id\E/) {
                   3599:                             $hide = 1;
                   3600:                             last;
                   3601:                         }
                   3602:                     }
                   3603:                     if ($hide) {
                   3604:                         my ($id,$data) = ($key =~ /^(.+)\.([^.]+)$/);
                   3605:                         if (($data eq 'award') || ($data eq 'awarddetail')) {
                   3606:                             my $value = &format_previous_attempt_value($key,
                   3607:                                              $returnhash{$version.':'.$key});
                   3608:                             $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3609:                         } else {
                   3610:                             $prevattempts.='<td>&nbsp;</td>';
                   3611:                         }
                   3612:                     } else {
                   3613:                         if ($key =~ /\./) {
                   3614:                             my $value = &format_previous_attempt_value($key,
                   3615:                                               $returnhash{$version.':'.$key});
                   3616:                             $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3617:                         } else {
                   3618:                             $prevattempts.='<td>&nbsp;</td>';
                   3619:                         }
                   3620:                     }
                   3621:                 }
                   3622:             } else {
                   3623: 	        foreach my $key (sort(keys(%lasthash))) {
1.989     raeburn  3624:                     next if ($key =~ /\.foilorder$/);
1.945     raeburn  3625: 		    my $value = &format_previous_attempt_value($key,
                   3626: 			            $returnhash{$version.':'.$key});
                   3627: 		    $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3628: 	        }
                   3629:             }
                   3630: 	    $prevattempts.=&end_data_table_row();
1.40      ng       3631: 	 }
1.1       albertel 3632:       }
1.945     raeburn  3633:       my @currhidden = keys(%lasthidden);
1.596     albertel 3634:       $prevattempts.=&start_data_table_row().'<td>'.&mt('Current').'</td>';
1.356     albertel 3635:       foreach my $key (sort(keys(%lasthash))) {
1.989     raeburn  3636:           next if ($key =~ /\.foilorder$/);
1.945     raeburn  3637:           if (%typeparts) {
                   3638:               my $hidden;
                   3639:               foreach my $id (@currhidden) {
                   3640:                   if ($key =~ /^\Q$id\E/) {
                   3641:                       $hidden = 1;
                   3642:                       last;
                   3643:                   }
                   3644:               }
                   3645:               if ($hidden) {
                   3646:                   my ($id,$data) = ($key =~ /^(.+)\.([^.]+)$/);
                   3647:                   if (($data eq 'award') || ($data eq 'awarddetail')) {
                   3648:                       my $value = &format_previous_attempt_value($key,$lasthash{$key});
                   3649:                       if ($key =~/$regexp$/ && (defined &$gradesub)) {
                   3650:                           $value = &$gradesub($value);
                   3651:                       }
                   3652:                       $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3653:                   } else {
                   3654:                       $prevattempts.='<td>&nbsp;</td>';
                   3655:                   }
                   3656:               } else {
                   3657:                   my $value = &format_previous_attempt_value($key,$lasthash{$key});
                   3658:                   if ($key =~/$regexp$/ && (defined &$gradesub)) {
                   3659:                       $value = &$gradesub($value);
                   3660:                   }
                   3661:                   $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3662:               }
                   3663:           } else {
                   3664: 	      my $value = &format_previous_attempt_value($key,$lasthash{$key});
                   3665: 	      if ($key =~/$regexp$/ && (defined &$gradesub)) {
                   3666:                   $value = &$gradesub($value);
                   3667:               }
                   3668: 	      $prevattempts.='<td>'.$value.'&nbsp;</td>';
                   3669:           }
1.16      harris41 3670:       }
1.596     albertel 3671:       $prevattempts.= &end_data_table_row().&end_data_table();
1.1       albertel 3672:     } else {
1.596     albertel 3673:       $prevattempts=
                   3674: 	  &start_data_table().&start_data_table_row().
                   3675: 	  '<td>'.&mt('Nothing submitted - no attempts.').'</td>'.
                   3676: 	  &end_data_table_row().&end_data_table();
1.1       albertel 3677:     }
                   3678:   } else {
1.596     albertel 3679:     $prevattempts=
                   3680: 	  &start_data_table().&start_data_table_row().
                   3681: 	  '<td>'.&mt('No data.').'</td>'.
                   3682: 	  &end_data_table_row().&end_data_table();
1.1       albertel 3683:   }
1.10      albertel 3684: }
                   3685: 
1.581     albertel 3686: sub format_previous_attempt_value {
                   3687:     my ($key,$value) = @_;
1.1011    www      3688:     if (($key =~ /timestamp/) || ($key=~/duedate/)) {
1.581     albertel 3689: 	$value = &Apache::lonlocal::locallocaltime($value);
                   3690:     } elsif (ref($value) eq 'ARRAY') {
                   3691: 	$value = '('.join(', ', @{ $value }).')';
1.988     raeburn  3692:     } elsif ($key =~ /answerstring$/) {
                   3693:         my %answers = &Apache::lonnet::str2hash($value);
                   3694:         my @anskeys = sort(keys(%answers));
                   3695:         if (@anskeys == 1) {
                   3696:             my $answer = $answers{$anskeys[0]};
1.1001    raeburn  3697:             if ($answer =~ m{\0}) {
                   3698:                 $answer =~ s{\0}{,}g;
1.988     raeburn  3699:             }
                   3700:             my $tag_internal_answer_name = 'INTERNAL';
                   3701:             if ($anskeys[0] eq $tag_internal_answer_name) {
                   3702:                 $value = $answer; 
                   3703:             } else {
                   3704:                 $value = $anskeys[0].'='.$answer;
                   3705:             }
                   3706:         } else {
                   3707:             foreach my $ans (@anskeys) {
                   3708:                 my $answer = $answers{$ans};
1.1001    raeburn  3709:                 if ($answer =~ m{\0}) {
                   3710:                     $answer =~ s{\0}{,}g;
1.988     raeburn  3711:                 }
                   3712:                 $value .=  $ans.'='.$answer.'<br />';;
                   3713:             } 
                   3714:         }
1.581     albertel 3715:     } else {
                   3716: 	$value = &unescape($value);
                   3717:     }
                   3718:     return $value;
                   3719: }
                   3720: 
                   3721: 
1.107     albertel 3722: sub relative_to_absolute {
                   3723:     my ($url,$output)=@_;
                   3724:     my $parser=HTML::TokeParser->new(\$output);
                   3725:     my $token;
                   3726:     my $thisdir=$url;
                   3727:     my @rlinks=();
                   3728:     while ($token=$parser->get_token) {
                   3729: 	if ($token->[0] eq 'S') {
                   3730: 	    if ($token->[1] eq 'a') {
                   3731: 		if ($token->[2]->{'href'}) {
                   3732: 		    $rlinks[$#rlinks+1]=$token->[2]->{'href'};
                   3733: 		}
                   3734: 	    } elsif ($token->[1] eq 'img' || $token->[1] eq 'embed' ) {
                   3735: 		$rlinks[$#rlinks+1]=$token->[2]->{'src'};
                   3736: 	    } elsif ($token->[1] eq 'base') {
                   3737: 		$thisdir=$token->[2]->{'href'};
                   3738: 	    }
                   3739: 	}
                   3740:     }
                   3741:     $thisdir=~s-/[^/]*$--;
1.356     albertel 3742:     foreach my $link (@rlinks) {
1.726     raeburn  3743: 	unless (($link=~/^https?\:\/\//i) ||
1.356     albertel 3744: 		($link=~/^\//) ||
                   3745: 		($link=~/^javascript:/i) ||
                   3746: 		($link=~/^mailto:/i) ||
                   3747: 		($link=~/^\#/)) {
                   3748: 	    my $newlocation=&Apache::lonnet::hreflocation($thisdir,$link);
                   3749: 	    $output=~s/(\"|\'|\=\s*)\Q$link\E(\"|\'|\s|\>)/$1$newlocation$2/;
1.107     albertel 3750: 	}
                   3751:     }
                   3752: # -------------------------------------------------- Deal with Applet codebases
                   3753:     $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
                   3754:     return $output;
                   3755: }
                   3756: 
1.112     bowersj2 3757: =pod
                   3758: 
1.648     raeburn  3759: =item * &get_student_view()
1.112     bowersj2 3760: 
                   3761: show a snapshot of what student was looking at
                   3762: 
                   3763: =cut
                   3764: 
1.10      albertel 3765: sub get_student_view {
1.186     albertel 3766:   my ($symb,$username,$domain,$courseid,$target,$moreenv) = @_;
1.114     www      3767:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 3768:   my (%form);
1.10      albertel 3769:   my @elements=('symb','courseid','domain','username');
                   3770:   foreach my $element (@elements) {
1.186     albertel 3771:       $form{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 3772:   }
1.186     albertel 3773:   if (defined($moreenv)) {
                   3774:       %form=(%form,%{$moreenv});
                   3775:   }
1.236     albertel 3776:   if (defined($target)) { $form{'grade_target'} = $target; }
1.107     albertel 3777:   $feedurl=&Apache::lonnet::clutter($feedurl);
1.650     www      3778:   my ($userview,$response)=&Apache::lonnet::ssi_body($feedurl,%form);
1.11      albertel 3779:   $userview=~s/\<body[^\>]*\>//gi;
                   3780:   $userview=~s/\<\/body\>//gi;
                   3781:   $userview=~s/\<html\>//gi;
                   3782:   $userview=~s/\<\/html\>//gi;
                   3783:   $userview=~s/\<head\>//gi;
                   3784:   $userview=~s/\<\/head\>//gi;
                   3785:   $userview=~s/action\s*\=/would_be_action\=/gi;
1.107     albertel 3786:   $userview=&relative_to_absolute($feedurl,$userview);
1.650     www      3787:   if (wantarray) {
                   3788:      return ($userview,$response);
                   3789:   } else {
                   3790:      return $userview;
                   3791:   }
                   3792: }
                   3793: 
                   3794: sub get_student_view_with_retries {
                   3795:   my ($symb,$retries,$username,$domain,$courseid,$target,$moreenv) = @_;
                   3796: 
                   3797:     my $ok = 0;                 # True if we got a good response.
                   3798:     my $content;
                   3799:     my $response;
                   3800: 
                   3801:     # Try to get the student_view done. within the retries count:
                   3802:     
                   3803:     do {
                   3804:          ($content, $response) = &get_student_view($symb,$username,$domain,$courseid,$target,$moreenv);
                   3805:          $ok      = $response->is_success;
                   3806:          if (!$ok) {
                   3807:             &Apache::lonnet::logthis("Failed get_student_view_with_retries on $symb: ".$response->is_success.', '.$response->code.', '.$response->message);
                   3808:          }
                   3809:          $retries--;
                   3810:     } while (!$ok && ($retries > 0));
                   3811:     
                   3812:     if (!$ok) {
                   3813:        $content = '';          # On error return an empty content.
                   3814:     }
1.651     www      3815:     if (wantarray) {
                   3816:        return ($content, $response);
                   3817:     } else {
                   3818:        return $content;
                   3819:     }
1.11      albertel 3820: }
                   3821: 
1.112     bowersj2 3822: =pod
                   3823: 
1.648     raeburn  3824: =item * &get_student_answers() 
1.112     bowersj2 3825: 
                   3826: show a snapshot of how student was answering problem
                   3827: 
                   3828: =cut
                   3829: 
1.11      albertel 3830: sub get_student_answers {
1.100     sakharuk 3831:   my ($symb,$username,$domain,$courseid,%form) = @_;
1.114     www      3832:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 3833:   my (%moreenv);
1.11      albertel 3834:   my @elements=('symb','courseid','domain','username');
                   3835:   foreach my $element (@elements) {
1.186     albertel 3836:     $moreenv{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 3837:   }
1.186     albertel 3838:   $moreenv{'grade_target'}='answer';
                   3839:   %moreenv=(%form,%moreenv);
1.497     raeburn  3840:   $feedurl = &Apache::lonnet::clutter($feedurl);
                   3841:   my $userview=&Apache::lonnet::ssi($feedurl,%moreenv);
1.10      albertel 3842:   return $userview;
1.1       albertel 3843: }
1.116     albertel 3844: 
                   3845: =pod
                   3846: 
                   3847: =item * &submlink()
                   3848: 
1.242     albertel 3849: Inputs: $text $uname $udom $symb $target
1.116     albertel 3850: 
                   3851: Returns: A link to grades.pm such as to see the SUBM view of a student
                   3852: 
                   3853: =cut
                   3854: 
                   3855: ###############################################
                   3856: sub submlink {
1.242     albertel 3857:     my ($text,$uname,$udom,$symb,$target)=@_;
1.116     albertel 3858:     if (!($uname && $udom)) {
                   3859: 	(my $cursymb, my $courseid,$udom,$uname)=
1.463     albertel 3860: 	    &Apache::lonnet::whichuser($symb);
1.116     albertel 3861: 	if (!$symb) { $symb=$cursymb; }
                   3862:     }
1.254     matthew  3863:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      3864:     $symb=&escape($symb);
1.960     bisitz   3865:     if ($target) { $target=" target=\"$target\""; }
                   3866:     return
                   3867:         '<a href="/adm/grades?command=submission'.
                   3868:         '&amp;symb='.$symb.
                   3869:         '&amp;student='.$uname.
                   3870:         '&amp;userdom='.$udom.'"'.
                   3871:         $target.'>'.$text.'</a>';
1.242     albertel 3872: }
                   3873: ##############################################
                   3874: 
                   3875: =pod
                   3876: 
                   3877: =item * &pgrdlink()
                   3878: 
                   3879: Inputs: $text $uname $udom $symb $target
                   3880: 
                   3881: Returns: A link to grades.pm such as to see the PGRD view of a student
                   3882: 
                   3883: =cut
                   3884: 
                   3885: ###############################################
                   3886: sub pgrdlink {
                   3887:     my $link=&submlink(@_);
                   3888:     $link=~s/(&command=submission)/$1&showgrading=yes/;
                   3889:     return $link;
                   3890: }
                   3891: ##############################################
                   3892: 
                   3893: =pod
                   3894: 
                   3895: =item * &pprmlink()
                   3896: 
                   3897: Inputs: $text $uname $udom $symb $target
                   3898: 
                   3899: Returns: A link to parmset.pm such as to see the PPRM view of a
1.283     albertel 3900: student and a specific resource
1.242     albertel 3901: 
                   3902: =cut
                   3903: 
                   3904: ###############################################
                   3905: sub pprmlink {
                   3906:     my ($text,$uname,$udom,$symb,$target)=@_;
                   3907:     if (!($uname && $udom)) {
                   3908: 	(my $cursymb, my $courseid,$udom,$uname)=
1.463     albertel 3909: 	    &Apache::lonnet::whichuser($symb);
1.242     albertel 3910: 	if (!$symb) { $symb=$cursymb; }
                   3911:     }
1.254     matthew  3912:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      3913:     $symb=&escape($symb);
1.242     albertel 3914:     if ($target) { $target="target=\"$target\""; }
1.595     albertel 3915:     return '<a href="/adm/parmset?command=set&amp;'.
                   3916: 	'symb='.$symb.'&amp;uname='.$uname.
                   3917: 	'&amp;udom='.$udom.'" '.$target.'>'.$text.'</a>';
1.116     albertel 3918: }
                   3919: ##############################################
1.37      matthew  3920: 
1.112     bowersj2 3921: =pod
                   3922: 
                   3923: =back
                   3924: 
                   3925: =cut
                   3926: 
1.37      matthew  3927: ###############################################
1.51      www      3928: 
                   3929: 
                   3930: sub timehash {
1.687     raeburn  3931:     my ($thistime) = @_;
                   3932:     my $timezone = &Apache::lonlocal::gettimezone();
                   3933:     my $dt = DateTime->from_epoch(epoch => $thistime)
                   3934:                      ->set_time_zone($timezone);
                   3935:     my $wday = $dt->day_of_week();
                   3936:     if ($wday == 7) { $wday = 0; }
                   3937:     return ( 'second' => $dt->second(),
                   3938:              'minute' => $dt->minute(),
                   3939:              'hour'   => $dt->hour(),
                   3940:              'day'     => $dt->day_of_month(),
                   3941:              'month'   => $dt->month(),
                   3942:              'year'    => $dt->year(),
                   3943:              'weekday' => $wday,
                   3944:              'dayyear' => $dt->day_of_year(),
                   3945:              'dlsav'   => $dt->is_dst() );
1.51      www      3946: }
                   3947: 
1.370     www      3948: sub utc_string {
                   3949:     my ($date)=@_;
1.371     www      3950:     return strftime("%Y%m%dT%H%M%SZ",gmtime($date));
1.370     www      3951: }
                   3952: 
1.51      www      3953: sub maketime {
                   3954:     my %th=@_;
1.687     raeburn  3955:     my ($epoch_time,$timezone,$dt);
                   3956:     $timezone = &Apache::lonlocal::gettimezone();
                   3957:     eval {
                   3958:         $dt = DateTime->new( year   => $th{'year'},
                   3959:                              month  => $th{'month'},
                   3960:                              day    => $th{'day'},
                   3961:                              hour   => $th{'hour'},
                   3962:                              minute => $th{'minute'},
                   3963:                              second => $th{'second'},
                   3964:                              time_zone => $timezone,
                   3965:                          );
                   3966:     };
                   3967:     if (!$@) {
                   3968:         $epoch_time = $dt->epoch;
                   3969:         if ($epoch_time) {
                   3970:             return $epoch_time;
                   3971:         }
                   3972:     }
1.51      www      3973:     return POSIX::mktime(
                   3974:         ($th{'seconds'},$th{'minutes'},$th{'hours'},
1.210     www      3975:          $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,-1));
1.70      www      3976: }
                   3977: 
                   3978: #########################################
1.51      www      3979: 
                   3980: sub findallcourses {
1.482     raeburn  3981:     my ($roles,$uname,$udom) = @_;
1.355     albertel 3982:     my %roles;
                   3983:     if (ref($roles)) { %roles = map { $_ => 1 } @{$roles}; }
1.348     albertel 3984:     my %courses;
1.51      www      3985:     my $now=time;
1.482     raeburn  3986:     if (!defined($uname)) {
                   3987:         $uname = $env{'user.name'};
                   3988:     }
                   3989:     if (!defined($udom)) {
                   3990:         $udom = $env{'user.domain'};
                   3991:     }
                   3992:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.982     raeburn  3993:         my $extra = &Apache::lonnet::freeze_escape({'skipcheck' => 1});
                   3994:         my %roleshash = &Apache::lonnet::dump('roles',$udom,$uname,'.',undef,
                   3995:                                               $extra);
1.482     raeburn  3996:         if (!%roles) {
                   3997:             %roles = (
                   3998:                        cc => 1,
1.907     raeburn  3999:                        co => 1,
1.482     raeburn  4000:                        in => 1,
                   4001:                        ep => 1,
                   4002:                        ta => 1,
                   4003:                        cr => 1,
                   4004:                        st => 1,
                   4005:              );
                   4006:         }
                   4007:         foreach my $entry (keys(%roleshash)) {
                   4008:             my ($trole,$tend,$tstart) = split(/_/,$roleshash{$entry});
                   4009:             if ($trole =~ /^cr/) { 
                   4010:                 next if (!exists($roles{$trole}) && !exists($roles{'cr'}));
                   4011:             } else {
                   4012:                 next if (!exists($roles{$trole}));
                   4013:             }
                   4014:             if ($tend) {
                   4015:                 next if ($tend < $now);
                   4016:             }
                   4017:             if ($tstart) {
                   4018:                 next if ($tstart > $now);
                   4019:             }
1.1058    raeburn  4020:             my ($cdom,$cnum,$sec,$cnumpart,$secpart,$role);
1.482     raeburn  4021:             (undef,$cdom,$cnumpart,$secpart) = split(/\//,$entry);
1.1058    raeburn  4022:             my $value = $trole.'/'.$cdom.'/';
1.482     raeburn  4023:             if ($secpart eq '') {
                   4024:                 ($cnum,$role) = split(/_/,$cnumpart); 
                   4025:                 $sec = 'none';
1.1058    raeburn  4026:                 $value .= $cnum.'/';
1.482     raeburn  4027:             } else {
                   4028:                 $cnum = $cnumpart;
                   4029:                 ($sec,$role) = split(/_/,$secpart);
1.1058    raeburn  4030:                 $value .= $cnum.'/'.$sec;
                   4031:             }
                   4032:             if (ref($courses{$cdom.'_'.$cnum}{$sec}) eq 'ARRAY') {
                   4033:                 unless (grep(/^\Q$value\E$/,@{$courses{$cdom.'_'.$cnum}{$sec}})) {
                   4034:                     push(@{$courses{$cdom.'_'.$cnum}{$sec}},$value);
                   4035:                 }
                   4036:             } else {
                   4037:                 @{$courses{$cdom.'_'.$cnum}{$sec}} = ($value);
1.490     raeburn  4038:             }
1.482     raeburn  4039:         }
                   4040:     } else {
                   4041:         foreach my $key (keys(%env)) {
1.483     albertel 4042: 	    if ( $key=~m{^user\.role\.(\w+)\./($match_domain)/($match_courseid)/?(\w*)$} ||
                   4043:                  $key=~m{^user\.role\.(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_courseid)/?(\w*)$}) {
1.482     raeburn  4044: 	        my ($role,$cdom,$cnum,$sec) = ($1,$2,$3,$4);
                   4045: 	        next if ($role eq 'ca' || $role eq 'aa');
                   4046: 	        next if (%roles && !exists($roles{$role}));
                   4047: 	        my ($starttime,$endtime)=split(/\./,$env{$key});
                   4048:                 my $active=1;
                   4049:                 if ($starttime) {
                   4050: 		    if ($now<$starttime) { $active=0; }
                   4051:                 }
                   4052:                 if ($endtime) {
                   4053:                     if ($now>$endtime) { $active=0; }
                   4054:                 }
                   4055:                 if ($active) {
1.1058    raeburn  4056:                     my $value = $role.'/'.$cdom.'/'.$cnum.'/';
1.482     raeburn  4057:                     if ($sec eq '') {
                   4058:                         $sec = 'none';
1.1058    raeburn  4059:                     } else {
                   4060:                         $value .= $sec;
                   4061:                     }
                   4062:                     if (ref($courses{$cdom.'_'.$cnum}{$sec}) eq 'ARRAY') {
                   4063:                         unless (grep(/^\Q$value\E$/,@{$courses{$cdom.'_'.$cnum}{$sec}})) {
                   4064:                             push(@{$courses{$cdom.'_'.$cnum}{$sec}},$value);
                   4065:                         }
                   4066:                     } else {
                   4067:                         @{$courses{$cdom.'_'.$cnum}{$sec}} = ($value);
1.482     raeburn  4068:                     }
1.474     raeburn  4069:                 }
                   4070:             }
1.51      www      4071:         }
                   4072:     }
1.474     raeburn  4073:     return %courses;
1.51      www      4074: }
1.37      matthew  4075: 
1.54      www      4076: ###############################################
1.474     raeburn  4077: 
                   4078: sub blockcheck {
1.482     raeburn  4079:     my ($setters,$activity,$uname,$udom) = @_;
1.490     raeburn  4080: 
                   4081:     if (!defined($udom)) {
                   4082:         $udom = $env{'user.domain'};
                   4083:     }
                   4084:     if (!defined($uname)) {
                   4085:         $uname = $env{'user.name'};
                   4086:     }
                   4087: 
                   4088:     # If uname and udom are for a course, check for blocks in the course.
                   4089: 
                   4090:     if (&Apache::lonnet::is_course($udom,$uname)) {
                   4091:         my %records = &Apache::lonnet::dump('comm_block',$udom,$uname);
1.502     raeburn  4092:         my ($startblock,$endblock)=&get_blocks($setters,$activity,$udom,$uname);
1.490     raeburn  4093:         return ($startblock,$endblock);
                   4094:     }
1.474     raeburn  4095: 
1.502     raeburn  4096:     my $startblock = 0;
                   4097:     my $endblock = 0;
1.482     raeburn  4098:     my %live_courses = &findallcourses(undef,$uname,$udom);
1.474     raeburn  4099: 
1.490     raeburn  4100:     # If uname is for a user, and activity is course-specific, i.e.,
                   4101:     # boards, chat or groups, check for blocking in current course only.
1.474     raeburn  4102: 
1.490     raeburn  4103:     if (($activity eq 'boards' || $activity eq 'chat' ||
                   4104:          $activity eq 'groups') && ($env{'request.course.id'})) {
                   4105:         foreach my $key (keys(%live_courses)) {
                   4106:             if ($key ne $env{'request.course.id'}) {
                   4107:                 delete($live_courses{$key});
                   4108:             }
                   4109:         }
                   4110:     }
                   4111: 
                   4112:     my $otheruser = 0;
                   4113:     my %own_courses;
                   4114:     if ((($uname ne $env{'user.name'})) || ($udom ne $env{'user.domain'})) {
                   4115:         # Resource belongs to user other than current user.
                   4116:         $otheruser = 1;
                   4117:         # Gather courses for current user
                   4118:         %own_courses = 
                   4119:             &findallcourses(undef,$env{'user.name'},$env{'user.domain'});
                   4120:     }
                   4121: 
                   4122:     # Gather active course roles - course coordinator, instructor, 
                   4123:     # exam proctor, ta, student, or custom role.
1.474     raeburn  4124: 
                   4125:     foreach my $course (keys(%live_courses)) {
1.482     raeburn  4126:         my ($cdom,$cnum);
                   4127:         if ((defined($env{'course.'.$course.'.domain'})) && (defined($env{'course.'.$course.'.num'}))) {
                   4128:             $cdom = $env{'course.'.$course.'.domain'};
                   4129:             $cnum = $env{'course.'.$course.'.num'};
                   4130:         } else {
1.490     raeburn  4131:             ($cdom,$cnum) = split(/_/,$course); 
1.482     raeburn  4132:         }
                   4133:         my $no_ownblock = 0;
                   4134:         my $no_userblock = 0;
1.533     raeburn  4135:         if ($otheruser && $activity ne 'com') {
1.490     raeburn  4136:             # Check if current user has 'evb' priv for this
                   4137:             if (defined($own_courses{$course})) {
                   4138:                 foreach my $sec (keys(%{$own_courses{$course}})) {
                   4139:                     my $checkrole = 'cm./'.$cdom.'/'.$cnum;
                   4140:                     if ($sec ne 'none') {
                   4141:                         $checkrole .= '/'.$sec;
                   4142:                     }
                   4143:                     if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
                   4144:                         $no_ownblock = 1;
                   4145:                         last;
                   4146:                     }
                   4147:                 }
                   4148:             }
                   4149:             # if they have 'evb' priv and are currently not playing student
                   4150:             next if (($no_ownblock) &&
                   4151:                  ($env{'request.role'} !~ m{^st\./$cdom/$cnum}));
                   4152:         }
1.474     raeburn  4153:         foreach my $sec (keys(%{$live_courses{$course}})) {
1.482     raeburn  4154:             my $checkrole = 'cm./'.$cdom.'/'.$cnum;
1.474     raeburn  4155:             if ($sec ne 'none') {
1.482     raeburn  4156:                 $checkrole .= '/'.$sec;
1.474     raeburn  4157:             }
1.490     raeburn  4158:             if ($otheruser) {
                   4159:                 # Resource belongs to user other than current user.
                   4160:                 # Assemble privs for that user, and check for 'evb' priv.
1.1058    raeburn  4161:                 my (%allroles,%userroles);
                   4162:                 if (ref($live_courses{$course}{$sec}) eq 'ARRAY') {
                   4163:                     foreach my $entry (@{$live_courses{$course}{$sec}}) { 
                   4164:                         my ($trole,$tdom,$tnum,$tsec);
                   4165:                         if ($entry =~ /^cr/) {
                   4166:                             ($trole,$tdom,$tnum,$tsec) = 
                   4167:                                 ($entry =~ m|^(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_username)/?(\w*)$|);
                   4168:                         } else {
                   4169:                            ($trole,$tdom,$tnum,$tsec) = split(/\//,$entry);
                   4170:                         }
                   4171:                         my ($spec,$area,$trest);
                   4172:                         $area = '/'.$tdom.'/'.$tnum;
                   4173:                         $trest = $tnum;
                   4174:                         if ($tsec ne '') {
                   4175:                             $area .= '/'.$tsec;
                   4176:                             $trest .= '/'.$tsec;
                   4177:                         }
                   4178:                         $spec = $trole.'.'.$area;
                   4179:                         if ($trole =~ /^cr/) {
                   4180:                             &Apache::lonnet::custom_roleprivs(\%allroles,$trole,
                   4181:                                                               $tdom,$spec,$trest,$area);
                   4182:                         } else {
                   4183:                             &Apache::lonnet::standard_roleprivs(\%allroles,$trole,
                   4184:                                                                 $tdom,$spec,$trest,$area);
                   4185:                         }
                   4186:                     }
                   4187:                     my ($author,$adv) = &Apache::lonnet::set_userprivs(\%userroles,\%allroles);
                   4188:                     if ($userroles{'user.priv.'.$checkrole} =~ /evb\&([^\:]*)/) {
                   4189:                         if ($1) {
                   4190:                             $no_userblock = 1;
                   4191:                             last;
                   4192:                         }
1.486     raeburn  4193:                     }
                   4194:                 }
1.490     raeburn  4195:             } else {
                   4196:                 # Resource belongs to current user
                   4197:                 # Check for 'evb' priv via lonnet::allowed().
1.482     raeburn  4198:                 if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
                   4199:                     $no_ownblock = 1;
                   4200:                     last;
                   4201:                 }
1.474     raeburn  4202:             }
                   4203:         }
                   4204:         # if they have the evb priv and are currently not playing student
1.482     raeburn  4205:         next if (($no_ownblock) &&
1.491     albertel 4206:                  ($env{'request.role'} !~ m{^st\./\Q$cdom\E/\Q$cnum\E}));
1.482     raeburn  4207:         next if ($no_userblock);
1.474     raeburn  4208: 
1.866     kalberla 4209:         # Retrieve blocking times and identity of locker for course
1.490     raeburn  4210:         # of specified user, unless user has 'evb' privilege.
1.502     raeburn  4211:         
                   4212:         my ($start,$end)=&get_blocks($setters,$activity,$cdom,$cnum);
                   4213:         if (($start != 0) && 
                   4214:             (($startblock == 0) || ($startblock > $start))) {
                   4215:             $startblock = $start;
                   4216:         }
                   4217:         if (($end != 0)  &&
                   4218:             (($endblock == 0) || ($endblock < $end))) {
                   4219:             $endblock = $end;
                   4220:         }
1.490     raeburn  4221:     }
                   4222:     return ($startblock,$endblock);
                   4223: }
                   4224: 
                   4225: sub get_blocks {
                   4226:     my ($setters,$activity,$cdom,$cnum) = @_;
                   4227:     my $startblock = 0;
                   4228:     my $endblock = 0;
                   4229:     my $course = $cdom.'_'.$cnum;
                   4230:     $setters->{$course} = {};
                   4231:     $setters->{$course}{'staff'} = [];
                   4232:     $setters->{$course}{'times'} = [];
                   4233:     my %records = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
                   4234:     foreach my $record (keys(%records)) {
                   4235:         my ($start,$end) = ($record =~ m/^(\d+)____(\d+)$/);
                   4236:         if ($start <= time && $end >= time) {
                   4237:             my ($staff_name,$staff_dom,$title,$blocks) =
                   4238:                 &parse_block_record($records{$record});
                   4239:             if ($blocks->{$activity} eq 'on') {
                   4240:                 push(@{$$setters{$course}{'staff'}},[$staff_name,$staff_dom]);
                   4241:                 push(@{$$setters{$course}{'times'}}, [$start,$end]);
1.491     albertel 4242:                 if ( ($startblock == 0) || ($startblock > $start) ) {
                   4243:                     $startblock = $start;
1.490     raeburn  4244:                 }
1.491     albertel 4245:                 if ( ($endblock == 0) || ($endblock < $end) ) {
                   4246:                     $endblock = $end;
1.474     raeburn  4247:                 }
                   4248:             }
                   4249:         }
                   4250:     }
                   4251:     return ($startblock,$endblock);
                   4252: }
                   4253: 
                   4254: sub parse_block_record {
                   4255:     my ($record) = @_;
                   4256:     my ($setuname,$setudom,$title,$blocks);
                   4257:     if (ref($record) eq 'HASH') {
                   4258:         ($setuname,$setudom) = split(/:/,$record->{'setter'});
                   4259:         $title = &unescape($record->{'event'});
                   4260:         $blocks = $record->{'blocks'};
                   4261:     } else {
                   4262:         my @data = split(/:/,$record,3);
                   4263:         if (scalar(@data) eq 2) {
                   4264:             $title = $data[1];
                   4265:             ($setuname,$setudom) = split(/@/,$data[0]);
                   4266:         } else {
                   4267:             ($setuname,$setudom,$title) = @data;
                   4268:         }
                   4269:         $blocks = { 'com' => 'on' };
                   4270:     }
                   4271:     return ($setuname,$setudom,$title,$blocks);
                   4272: }
                   4273: 
1.854     kalberla 4274: sub blocking_status {
                   4275:   my ($activity,$uname,$udom) = @_;
1.867     kalberla 4276:   my %setters;
1.890     droeschl 4277: 
                   4278:   # check for active blocking
1.867     kalberla 4279:   my ($startblock,$endblock)=&blockcheck(\%setters,$activity,$uname,$udom);
1.854     kalberla 4280: 
1.890     droeschl 4281:   my $blocked = $startblock && $endblock ? 1 : 0;
                   4282: 
                   4283:   # caller just wants to know whether a block is active
                   4284:   if (!wantarray) { return $blocked; }
                   4285: 
                   4286:   # build a link to a popup window containing the details
                   4287:   my $querystring  = "?activity=$activity";
                   4288:   # $uname and $udom decide whose portfolio the user is trying to look at
                   4289:      $querystring .= "&amp;udom=$udom"      if $udom;
                   4290:      $querystring .= "&amp;uname=$uname"    if $uname;
                   4291: 
                   4292:   my $output .= <<'END_MYBLOCK';
1.854     kalberla 4293:     function openWindow(url, wdwName, w, h, toolbar,scrollbar) {
                   4294:         var options = "width=" + w + ",height=" + h + ",";
                   4295:         options += "resizable=yes,scrollbars="+scrollbar+",status=no,";
                   4296:         options += "menubar=no,toolbar="+toolbar+",location=no,directories=no";
                   4297:         var newWin = window.open(url, wdwName, options);
                   4298:         newWin.focus();
                   4299:     }
1.890     droeschl 4300: END_MYBLOCK
1.854     kalberla 4301: 
1.890     droeschl 4302:   $output = Apache::lonhtmlcommon::scripttag($output);
                   4303:   
1.854     kalberla 4304:   my $popupUrl = "/adm/blockingstatus/$querystring";
1.890     droeschl 4305:   my $text = mt('Communication Blocked');
                   4306: 
1.867     kalberla 4307:   $output .= <<"END_BLOCK";
                   4308: <div class='LC_comblock'>
1.869     kalberla 4309:   <a onclick='openWindow("$popupUrl","Blocking Table",600,300,"no","no");return false;' href='/adm/blockingstatus/$querystring'
1.890     droeschl 4310:   title='$text'>
                   4311:   <img class='LC_noBorder LC_middle' title='$text' src='/res/adm/pages/comblock.png' alt='$text'/></a>
1.869     kalberla 4312:   <a onclick='openWindow("$popupUrl","Blocking Table",600,300,"no","no");return false;' href='/adm/blockingstatus/$querystring' 
1.890     droeschl 4313:   title='$text'>$text</a>
1.867     kalberla 4314: </div>
                   4315: 
                   4316: END_BLOCK
1.474     raeburn  4317: 
1.854     kalberla 4318:   return ($blocked, $output);
                   4319: }
1.490     raeburn  4320: 
1.60      matthew  4321: ###############################################
                   4322: 
1.682     raeburn  4323: sub check_ip_acc {
                   4324:     my ($acc)=@_;
                   4325:     &Apache::lonxml::debug("acc is $acc");
                   4326:     if (!defined($acc) || $acc =~ /^\s*$/ || $acc =~/^\s*no\s*$/i) {
                   4327:         return 1;
                   4328:     }
                   4329:     my $allowed=0;
                   4330:     my $ip=$env{'request.host'} || $ENV{'REMOTE_ADDR'};
                   4331: 
                   4332:     my $name;
                   4333:     foreach my $pattern (split(',',$acc)) {
                   4334:         $pattern =~ s/^\s*//;
                   4335:         $pattern =~ s/\s*$//;
                   4336:         if ($pattern =~ /\*$/) {
                   4337:             #35.8.*
                   4338:             $pattern=~s/\*//;
                   4339:             if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
                   4340:         } elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {
                   4341:             #35.8.3.[34-56]
                   4342:             my $low=$2;
                   4343:             my $high=$3;
                   4344:             $pattern=$1;
                   4345:             if ($ip =~ /^\Q$pattern\E/) {
                   4346:                 my $last=(split(/\./,$ip))[3];
                   4347:                 if ($last <=$high && $last >=$low) { $allowed=1; }
                   4348:             }
                   4349:         } elsif ($pattern =~ /^\*/) {
                   4350:             #*.msu.edu
                   4351:             $pattern=~s/\*//;
                   4352:             if (!defined($name)) {
                   4353:                 use Socket;
                   4354:                 my $netaddr=inet_aton($ip);
                   4355:                 ($name)=gethostbyaddr($netaddr,AF_INET);
                   4356:             }
                   4357:             if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
                   4358:         } elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
                   4359:             #127.0.0.1
                   4360:             if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
                   4361:         } else {
                   4362:             #some.name.com
                   4363:             if (!defined($name)) {
                   4364:                 use Socket;
                   4365:                 my $netaddr=inet_aton($ip);
                   4366:                 ($name)=gethostbyaddr($netaddr,AF_INET);
                   4367:             }
                   4368:             if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
                   4369:         }
                   4370:         if ($allowed) { last; }
                   4371:     }
                   4372:     return $allowed;
                   4373: }
                   4374: 
                   4375: ###############################################
                   4376: 
1.60      matthew  4377: =pod
                   4378: 
1.112     bowersj2 4379: =head1 Domain Template Functions
                   4380: 
                   4381: =over 4
                   4382: 
                   4383: =item * &determinedomain()
1.60      matthew  4384: 
                   4385: Inputs: $domain (usually will be undef)
                   4386: 
1.63      www      4387: Returns: Determines which domain should be used for designs
1.60      matthew  4388: 
                   4389: =cut
1.54      www      4390: 
1.60      matthew  4391: ###############################################
1.63      www      4392: sub determinedomain {
                   4393:     my $domain=shift;
1.531     albertel 4394:     if (! $domain) {
1.60      matthew  4395:         # Determine domain if we have not been given one
1.893     raeburn  4396:         $domain = &Apache::lonnet::default_login_domain();
1.258     albertel 4397:         if ($env{'user.domain'}) { $domain=$env{'user.domain'}; }
                   4398:         if ($env{'request.role.domain'}) { 
                   4399:             $domain=$env{'request.role.domain'}; 
1.60      matthew  4400:         }
                   4401:     }
1.63      www      4402:     return $domain;
                   4403: }
                   4404: ###############################################
1.517     raeburn  4405: 
1.518     albertel 4406: sub devalidate_domconfig_cache {
                   4407:     my ($udom)=@_;
                   4408:     &Apache::lonnet::devalidate_cache_new('domainconfig',$udom);
                   4409: }
                   4410: 
                   4411: # ---------------------- Get domain configuration for a domain
                   4412: sub get_domainconf {
                   4413:     my ($udom) = @_;
                   4414:     my $cachetime=1800;
                   4415:     my ($result,$cached)=&Apache::lonnet::is_cached_new('domainconfig',$udom);
                   4416:     if (defined($cached)) { return %{$result}; }
                   4417: 
                   4418:     my %domconfig = &Apache::lonnet::get_dom('configuration',
1.948     raeburn  4419: 					     ['login','rolecolors','autoenroll'],$udom);
1.632     raeburn  4420:     my (%designhash,%legacy);
1.518     albertel 4421:     if (keys(%domconfig) > 0) {
                   4422:         if (ref($domconfig{'login'}) eq 'HASH') {
1.632     raeburn  4423:             if (keys(%{$domconfig{'login'}})) {
                   4424:                 foreach my $key (keys(%{$domconfig{'login'}})) {
1.699     raeburn  4425:                     if (ref($domconfig{'login'}{$key}) eq 'HASH') {
1.946     raeburn  4426:                         if ($key eq 'loginvia') {
                   4427:                             if (ref($domconfig{'login'}{'loginvia'}) eq 'HASH') {
1.1013    raeburn  4428:                                 foreach my $hostname (keys(%{$domconfig{'login'}{'loginvia'}})) {
1.948     raeburn  4429:                                     if (ref($domconfig{'login'}{'loginvia'}{$hostname}) eq 'HASH') {
                   4430:                                         if ($domconfig{'login'}{'loginvia'}{$hostname}{'server'}) {
                   4431:                                             my $server = $domconfig{'login'}{'loginvia'}{$hostname}{'server'};
                   4432:                                             $designhash{$udom.'.login.loginvia'} = $server;
                   4433:                                             if ($domconfig{'login'}{'loginvia'}{$hostname}{'serverpath'} eq 'custom') {
                   4434: 
                   4435:                                                 $designhash{$udom.'.login.loginvia_'.$hostname} = $server.':'.$domconfig{'login'}{'loginvia'}{$hostname}{'custompath'};
                   4436:                                             } else {
1.1013    raeburn  4437:                                                 $designhash{$udom.'.login.loginvia_'.$hostname} = $server.':'.$domconfig{'login'}{'loginvia'}{$hostname}{'serverpath'};
1.948     raeburn  4438:                                             }
                   4439:                                             if ($domconfig{'login'}{'loginvia'}{$hostname}{'exempt'}) {
                   4440:                                                 $designhash{$udom.'.login.loginvia_exempt_'.$hostname} = $domconfig{'login'}{'loginvia'}{$hostname}{'exempt'};
                   4441:                                             }
1.946     raeburn  4442:                                         }
                   4443:                                     }
                   4444:                                 }
                   4445:                             }
                   4446:                         } else {
                   4447:                             foreach my $img (keys(%{$domconfig{'login'}{$key}})) {
                   4448:                                 $designhash{$udom.'.login.'.$key.'_'.$img} = 
                   4449:                                     $domconfig{'login'}{$key}{$img};
                   4450:                             }
1.699     raeburn  4451:                         }
                   4452:                     } else {
                   4453:                         $designhash{$udom.'.login.'.$key}=$domconfig{'login'}{$key};
                   4454:                     }
1.632     raeburn  4455:                 }
                   4456:             } else {
                   4457:                 $legacy{'login'} = 1;
1.518     albertel 4458:             }
1.632     raeburn  4459:         } else {
                   4460:             $legacy{'login'} = 1;
1.518     albertel 4461:         }
                   4462:         if (ref($domconfig{'rolecolors'}) eq 'HASH') {
1.632     raeburn  4463:             if (keys(%{$domconfig{'rolecolors'}})) {
                   4464:                 foreach my $role (keys(%{$domconfig{'rolecolors'}})) {
                   4465:                     if (ref($domconfig{'rolecolors'}{$role}) eq 'HASH') {
                   4466:                         foreach my $item (keys(%{$domconfig{'rolecolors'}{$role}})) {
                   4467:                             $designhash{$udom.'.'.$role.'.'.$item}=$domconfig{'rolecolors'}{$role}{$item};
                   4468:                         }
1.518     albertel 4469:                     }
                   4470:                 }
1.632     raeburn  4471:             } else {
                   4472:                 $legacy{'rolecolors'} = 1;
1.518     albertel 4473:             }
1.632     raeburn  4474:         } else {
                   4475:             $legacy{'rolecolors'} = 1;
1.518     albertel 4476:         }
1.948     raeburn  4477:         if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   4478:             if ($domconfig{'autoenroll'}{'co-owners'}) {
                   4479:                 $designhash{$udom.'.autoassign.co-owners'}=$domconfig{'autoenroll'}{'co-owners'};
                   4480:             }
                   4481:         }
1.632     raeburn  4482:         if (keys(%legacy) > 0) {
                   4483:             my %legacyhash = &get_legacy_domconf($udom);
                   4484:             foreach my $item (keys(%legacyhash)) {
                   4485:                 if ($item =~ /^\Q$udom\E\.login/) {
                   4486:                     if ($legacy{'login'}) { 
                   4487:                         $designhash{$item} = $legacyhash{$item};
                   4488:                     }
                   4489:                 } else {
                   4490:                     if ($legacy{'rolecolors'}) {
                   4491:                         $designhash{$item} = $legacyhash{$item};
                   4492:                     }
1.518     albertel 4493:                 }
                   4494:             }
                   4495:         }
1.632     raeburn  4496:     } else {
                   4497:         %designhash = &get_legacy_domconf($udom); 
1.518     albertel 4498:     }
                   4499:     &Apache::lonnet::do_cache_new('domainconfig',$udom,\%designhash,
                   4500: 				  $cachetime);
                   4501:     return %designhash;
                   4502: }
                   4503: 
1.632     raeburn  4504: sub get_legacy_domconf {
                   4505:     my ($udom) = @_;
                   4506:     my %legacyhash;
                   4507:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
                   4508:     my $designfile =  $designdir.'/'.$udom.'.tab';
                   4509:     if (-e $designfile) {
                   4510:         if ( open (my $fh,"<$designfile") ) {
                   4511:             while (my $line = <$fh>) {
                   4512:                 next if ($line =~ /^\#/);
                   4513:                 chomp($line);
                   4514:                 my ($key,$val)=(split(/\=/,$line));
                   4515:                 if ($val) { $legacyhash{$udom.'.'.$key}=$val; }
                   4516:             }
                   4517:             close($fh);
                   4518:         }
                   4519:     }
1.1026    raeburn  4520:     if (-e $Apache::lonnet::perlvar{'lonDocRoot'}.'/adm/lonDomLogos/'.$udom.'.gif') {
1.632     raeburn  4521:         $legacyhash{$udom.'.login.domlogo'} = "/adm/lonDomLogos/$udom.gif";
                   4522:     }
                   4523:     return %legacyhash;
                   4524: }
                   4525: 
1.63      www      4526: =pod
                   4527: 
1.112     bowersj2 4528: =item * &domainlogo()
1.63      www      4529: 
                   4530: Inputs: $domain (usually will be undef)
                   4531: 
                   4532: Returns: A link to a domain logo, if the domain logo exists.
                   4533: If the domain logo does not exist, a description of the domain.
                   4534: 
                   4535: =cut
1.112     bowersj2 4536: 
1.63      www      4537: ###############################################
                   4538: sub domainlogo {
1.517     raeburn  4539:     my $domain = &determinedomain(shift);
1.518     albertel 4540:     my %designhash = &get_domainconf($domain);    
1.517     raeburn  4541:     # See if there is a logo
                   4542:     if ($designhash{$domain.'.login.domlogo'} ne '') {
1.519     raeburn  4543:         my $imgsrc = $designhash{$domain.'.login.domlogo'};
1.538     albertel 4544:         if ($imgsrc =~ m{^/(adm|res)/}) {
                   4545: 	    if ($imgsrc =~ m{^/res/}) {
                   4546: 		my $local_name = &Apache::lonnet::filelocation('',$imgsrc);
                   4547: 		&Apache::lonnet::repcopy($local_name);
                   4548: 	    }
                   4549: 	   $imgsrc = &lonhttpdurl($imgsrc);
1.519     raeburn  4550:         } 
                   4551:         return '<img src="'.$imgsrc.'" alt="'.$domain.'" />';
1.514     albertel 4552:     } elsif (defined(&Apache::lonnet::domain($domain,'description'))) {
                   4553:         return &Apache::lonnet::domain($domain,'description');
1.59      www      4554:     } else {
1.60      matthew  4555:         return '';
1.59      www      4556:     }
                   4557: }
1.63      www      4558: ##############################################
                   4559: 
                   4560: =pod
                   4561: 
1.112     bowersj2 4562: =item * &designparm()
1.63      www      4563: 
                   4564: Inputs: $which parameter; $domain (usually will be undef)
                   4565: 
                   4566: Returns: value of designparamter $which
                   4567: 
                   4568: =cut
1.112     bowersj2 4569: 
1.397     albertel 4570: 
1.400     albertel 4571: ##############################################
1.397     albertel 4572: sub designparm {
                   4573:     my ($which,$domain)=@_;
                   4574:     if (exists($env{'environment.color.'.$which})) {
1.817     bisitz   4575:         return $env{'environment.color.'.$which};
1.96      www      4576:     }
1.63      www      4577:     $domain=&determinedomain($domain);
1.1016    raeburn  4578:     my %domdesign;
                   4579:     unless ($domain eq 'public') {
                   4580:         %domdesign = &get_domainconf($domain);
                   4581:     }
1.520     raeburn  4582:     my $output;
1.517     raeburn  4583:     if ($domdesign{$domain.'.'.$which} ne '') {
1.817     bisitz   4584:         $output = $domdesign{$domain.'.'.$which};
1.63      www      4585:     } else {
1.520     raeburn  4586:         $output = $defaultdesign{$which};
                   4587:     }
                   4588:     if (($which =~ /^(student|coordinator|author|admin)\.img$/) ||
1.635     raeburn  4589:         ($which =~ /login\.(img|logo|domlogo|login)/)) {
1.538     albertel 4590:         if ($output =~ m{^/(adm|res)/}) {
1.817     bisitz   4591:             if ($output =~ m{^/res/}) {
                   4592:                 my $local_name = &Apache::lonnet::filelocation('',$output);
                   4593:                 &Apache::lonnet::repcopy($local_name);
                   4594:             }
1.520     raeburn  4595:             $output = &lonhttpdurl($output);
                   4596:         }
1.63      www      4597:     }
1.520     raeburn  4598:     return $output;
1.63      www      4599: }
1.59      www      4600: 
1.822     bisitz   4601: ##############################################
                   4602: =pod
                   4603: 
1.832     bisitz   4604: =item * &authorspace()
                   4605: 
1.1028    raeburn  4606: Inputs: $url (usually will be undef).
1.832     bisitz   4607: 
1.1028    raeburn  4608: Returns: Path to Construction Space containing the resource or 
                   4609:          directory being viewed (or for which action is being taken). 
                   4610:          If $url is provided, and begins /priv/<domain>/<uname>
                   4611:          the path will be that portion of the $context argument.
                   4612:          Otherwise the path will be for the author space of the current
                   4613:          user when the current role is author, or for that of the 
                   4614:          co-author/assistant co-author space when the current role 
                   4615:          is co-author or assistant co-author.
1.832     bisitz   4616: 
                   4617: =cut
                   4618: 
                   4619: sub authorspace {
1.1028    raeburn  4620:     my ($url) = @_;
                   4621:     if ($url ne '') {
                   4622:         if ($url =~ m{^(/priv/$match_domain/$match_username/)}) {
                   4623:            return $1;
                   4624:         }
                   4625:     }
1.832     bisitz   4626:     my $caname = '';
1.1024    www      4627:     my $cadom = '';
1.1028    raeburn  4628:     if ($env{'request.role'} =~ /^(?:ca|aa)/) {
1.1024    www      4629:         ($cadom,$caname) =
1.832     bisitz   4630:             ($env{'request.role'}=~/($match_domain)\/($match_username)$/);
1.1028    raeburn  4631:     } elsif ($env{'request.role'} =~ m{^au\./($match_domain)/}) {
1.832     bisitz   4632:         $caname = $env{'user.name'};
1.1024    www      4633:         $cadom = $env{'user.domain'};
1.832     bisitz   4634:     }
1.1028    raeburn  4635:     if (($caname ne '') && ($cadom ne '')) {
                   4636:         return "/priv/$cadom/$caname/";
                   4637:     }
                   4638:     return;
1.832     bisitz   4639: }
                   4640: 
                   4641: ##############################################
                   4642: =pod
                   4643: 
1.822     bisitz   4644: =item * &head_subbox()
                   4645: 
                   4646: Inputs: $content (contains HTML code with page functions, etc.)
                   4647: 
                   4648: Returns: HTML div with $content
                   4649:          To be included in page header
                   4650: 
                   4651: =cut
                   4652: 
                   4653: sub head_subbox {
                   4654:     my ($content)=@_;
                   4655:     my $output =
1.993     raeburn  4656:         '<div class="LC_head_subbox">'
1.822     bisitz   4657:        .$content
                   4658:        .'</div>'
                   4659: }
                   4660: 
                   4661: ##############################################
                   4662: =pod
                   4663: 
                   4664: =item * &CSTR_pageheader()
                   4665: 
1.1026    raeburn  4666: Input: (optional) filename from which breadcrumb trail is built.
                   4667:        In most cases no input as needed, as $env{'request.filename'}
                   4668:        is appropriate for use in building the breadcrumb trail.
1.822     bisitz   4669: 
                   4670: Returns: HTML div with CSTR path and recent box
                   4671:          To be included on Construction Space pages
                   4672: 
                   4673: =cut
                   4674: 
                   4675: sub CSTR_pageheader {
1.1026    raeburn  4676:     my ($trailfile) = @_;
                   4677:     if ($trailfile eq '') {
                   4678:         $trailfile = $env{'request.filename'};
                   4679:     }
                   4680: 
                   4681: # this is for resources; directories have customtitle, and crumbs
                   4682: # and select recent are created in lonpubdir.pm
                   4683: 
                   4684:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
1.1022    www      4685:     my ($udom,$uname,$thisdisfn)=
1.1026    raeburn  4686:         ($trailfile =~ m{^\Q$londocroot\E/priv/([^/]+)/([^/]+)/(.*)$});
                   4687:     my $formaction = "/priv/$udom/$uname/$thisdisfn";
                   4688:     $formaction =~ s{/+}{/}g;
1.822     bisitz   4689: 
                   4690:     my $parentpath = '';
                   4691:     my $lastitem = '';
                   4692:     if ($thisdisfn =~ m-(.+/)([^/]*)$-) {
                   4693:         $parentpath = $1;
                   4694:         $lastitem = $2;
                   4695:     } else {
                   4696:         $lastitem = $thisdisfn;
                   4697:     }
1.921     bisitz   4698: 
                   4699:     my $output =
1.822     bisitz   4700:          '<div>'
                   4701:         .&Apache::loncommon::help_open_menu('','',3,'Authoring') #FIXME: Broken? Where is it?
                   4702:         .'<b>'.&mt('Construction Space:').'</b> '
                   4703:         .'<form name="dirs" method="post" action="'.$formaction
1.921     bisitz   4704:         .'" target="_top">' #FIXME lonpubdir: target="_parent"
1.1024    www      4705:         .&Apache::lonhtmlcommon::crumbs($uname.'/'.$parentpath,'_top','/priv/'.$udom,undef,undef);
1.921     bisitz   4706: 
                   4707:     if ($lastitem) {
                   4708:         $output .=
                   4709:              '<span class="LC_filename">'
                   4710:             .$lastitem
                   4711:             .'</span>';
                   4712:     }
                   4713:     $output .=
                   4714:          '<br />'
1.822     bisitz   4715:         #FIXME lonpubdir: &Apache::lonhtmlcommon::crumbs($uname.$thisdisfn.'/','_top','/priv','','+1',1)."</b></tt><br />"
                   4716:         .&Apache::lonhtmlcommon::select_recent('construct','recent','this.form.action=this.form.recent.value;this.form.submit()')
                   4717:         .'</form>'
                   4718:         .&Apache::lonmenu::constspaceform()
                   4719:         .'</div>';
1.921     bisitz   4720: 
                   4721:     return $output;
1.822     bisitz   4722: }
                   4723: 
1.60      matthew  4724: ###############################################
                   4725: ###############################################
                   4726: 
                   4727: =pod
                   4728: 
1.112     bowersj2 4729: =back
                   4730: 
1.549     albertel 4731: =head1 HTML Helpers
1.112     bowersj2 4732: 
                   4733: =over 4
                   4734: 
                   4735: =item * &bodytag()
1.60      matthew  4736: 
                   4737: Returns a uniform header for LON-CAPA web pages.
                   4738: 
                   4739: Inputs: 
                   4740: 
1.112     bowersj2 4741: =over 4
                   4742: 
                   4743: =item * $title, A title to be displayed on the page.
                   4744: 
                   4745: =item * $function, the current role (can be undef).
                   4746: 
                   4747: =item * $addentries, extra parameters for the <body> tag.
                   4748: 
                   4749: =item * $bodyonly, if defined, only return the <body> tag.
                   4750: 
                   4751: =item * $domain, if defined, force a given domain.
                   4752: 
                   4753: =item * $forcereg, if page should register as content page (relevant for 
1.86      www      4754:             text interface only)
1.60      matthew  4755: 
1.814     bisitz   4756: =item * $no_nav_bar, if true, keep the 'what is this' info but remove the
                   4757:                      navigational links
1.317     albertel 4758: 
1.338     albertel 4759: =item * $bgcolor, used to override the bgcolor on a webpage to a specific value
                   4760: 
1.460     albertel 4761: =item * $args, optional argument valid values are
                   4762:             no_auto_mt_title -> prevents &mt()ing the title arg
1.562     albertel 4763:             inherit_jsmath -> when creating popup window in a page,
                   4764:                               should it have jsmath forced on by the
                   4765:                               current page
1.460     albertel 4766: 
1.112     bowersj2 4767: =back
                   4768: 
1.60      matthew  4769: Returns: A uniform header for LON-CAPA web pages.  
                   4770: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
                   4771: If $bodyonly is undef or zero, an html string containing a <body> tag and 
                   4772: other decorations will be returned.
                   4773: 
                   4774: =cut
                   4775: 
1.54      www      4776: sub bodytag {
1.831     bisitz   4777:     my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,
1.962     droeschl 4778:         $no_nav_bar,$bgcolor,$args)=@_;
1.339     albertel 4779: 
1.954     raeburn  4780:     my $public;
                   4781:     if ((($env{'user.name'} eq 'public') && ($env{'user.domain'} eq 'public'))
                   4782:         || ($env{'user.name'} eq '') && ($env{'user.domain'} eq '')) {
                   4783:         $public = 1;
                   4784:     }
1.460     albertel 4785:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
1.339     albertel 4786: 
1.183     matthew  4787:     $function = &get_users_function() if (!$function);
1.339     albertel 4788:     my $img =    &designparm($function.'.img',$domain);
                   4789:     my $font =   &designparm($function.'.font',$domain);
                   4790:     my $pgbg   = $bgcolor || &designparm($function.'.pgbg',$domain);
                   4791: 
1.803     bisitz   4792:     my %design = ( 'style'   => 'margin-top: 0',
1.535     albertel 4793: 		   'bgcolor' => $pgbg,
1.339     albertel 4794: 		   'text'    => $font,
                   4795:                    'alink'   => &designparm($function.'.alink',$domain),
                   4796: 		   'vlink'   => &designparm($function.'.vlink',$domain),
                   4797: 		   'link'    => &designparm($function.'.link',$domain),);
1.438     albertel 4798:     @design{keys(%$addentries)} = @$addentries{keys(%$addentries)}; 
1.339     albertel 4799: 
1.63      www      4800:  # role and realm
1.378     raeburn  4801:     my ($role,$realm) = split(/\./,$env{'request.role'},2);
                   4802:     if ($role  eq 'ca') {
1.479     albertel 4803:         my ($rdom,$rname) = ($realm =~ m{^/($match_domain)/($match_username)$});
1.500     albertel 4804:         $realm = &plainname($rname,$rdom);
1.378     raeburn  4805:     } 
1.55      www      4806: # realm
1.258     albertel 4807:     if ($env{'request.course.id'}) {
1.378     raeburn  4808:         if ($env{'request.role'} !~ /^cr/) {
                   4809:             $role = &Apache::lonnet::plaintext($role,&course_type());
                   4810:         }
1.898     raeburn  4811:         if ($env{'request.course.sec'}) {
                   4812:             $role .= ('&nbsp;'x2).'-&nbsp;'.&mt('section:').'&nbsp;'.$env{'request.course.sec'};
                   4813:         }   
1.359     albertel 4814: 	$realm = $env{'course.'.$env{'request.course.id'}.'.description'};
1.378     raeburn  4815:     } else {
                   4816:         $role = &Apache::lonnet::plaintext($role);
1.54      www      4817:     }
1.433     albertel 4818: 
1.359     albertel 4819:     if (!$realm) { $realm='&nbsp;'; }
1.330     albertel 4820: 
1.438     albertel 4821:     my $extra_body_attr = &make_attr_string($forcereg,\%design);
1.329     albertel 4822: 
1.101     www      4823: # construct main body tag
1.359     albertel 4824:     my $bodytag = "<body $extra_body_attr>".
1.562     albertel 4825: 	&Apache::lontexconvert::init_math_support($args->{'inherit_jsmath'});
1.252     albertel 4826: 
1.530     albertel 4827:     if ($bodyonly) {
1.60      matthew  4828:         return $bodytag;
1.798     tempelho 4829:     } 
1.359     albertel 4830: 
1.410     albertel 4831:     my $name = &plainname($env{'user.name'},$env{'user.domain'});
1.954     raeburn  4832:     if ($public) {
1.433     albertel 4833: 	undef($role);
1.434     albertel 4834:     } else {
                   4835: 	$name = &aboutmewrapper($name,$env{'user.name'},$env{'user.domain'});
1.433     albertel 4836:     }
1.359     albertel 4837:     
1.762     bisitz   4838:     my $titleinfo = '<h1>'.$title.'</h1>';
1.359     albertel 4839:     #
                   4840:     # Extra info if you are the DC
                   4841:     my $dc_info = '';
                   4842:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
                   4843:                         $env{'course.'.$env{'request.course.id'}.
                   4844:                                  '.domain'}.'/'})) {
                   4845:         my $cid = $env{'request.course.id'};
1.917     raeburn  4846:         $dc_info = $cid.' '.$env{'course.'.$cid.'.internal.coursecode'};
1.380     www      4847:         $dc_info =~ s/\s+$//;
1.359     albertel 4848:     }
                   4849: 
1.898     raeburn  4850:     $role = '<span class="LC_nobreak">('.$role.')</span>' if $role;
1.853     droeschl 4851:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
                   4852: 
1.916     droeschl 4853:         if ($no_nav_bar || $env{'form.inhibitmenu'} eq 'yes') { 
                   4854:             return $bodytag; 
                   4855:         } 
1.903     droeschl 4856: 
                   4857:         if ($env{'request.state'} eq 'construct') { $forcereg=1; }
                   4858: 
                   4859:         #    if ($env{'request.state'} eq 'construct') {
                   4860:         #        $titleinfo = &CSTR_pageheader(); #FIXME: Will be removed once all scripts have their own calls
                   4861:         #    }
                   4862: 
1.359     albertel 4863: 
                   4864: 
1.916     droeschl 4865:         if ($env{'request.noversionuri'} =~ m{^/res/adm/pages/}) {
1.917     raeburn  4866:              if ($dc_info) {
                   4867:                  $dc_info = qq|<span class="LC_cusr_subheading">$dc_info</span>|;
                   4868:              }
1.916     droeschl 4869:              $bodytag .= qq|<div id="LC_nav_bar">$name $role<br />
                   4870:                 <em>$realm</em> $dc_info</div>|;
1.903     droeschl 4871:             return $bodytag;
                   4872:         }
1.894     droeschl 4873: 
1.927     raeburn  4874:         unless ($env{'request.symb'} =~ m/\.page___\d+___/) {
                   4875:             $bodytag .= qq|<div id="LC_nav_bar">$name $role</div>|;
                   4876:         }
1.916     droeschl 4877: 
1.903     droeschl 4878:         $bodytag .= Apache::lonhtmlcommon::scripttag(
                   4879:             Apache::lonmenu::utilityfunctions(), 'start');
1.816     bisitz   4880: 
1.903     droeschl 4881:         $bodytag .= Apache::lonmenu::primary_menu();
1.852     droeschl 4882: 
1.917     raeburn  4883:         if ($dc_info) {
                   4884:             $dc_info = &dc_courseid_toggle($dc_info);
                   4885:         }
                   4886:         $bodytag .= qq|<div id="LC_realm">$realm $dc_info</div>|;
1.916     droeschl 4887: 
1.903     droeschl 4888:         #don't show menus for public users
1.954     raeburn  4889:         if (!$public){
1.903     droeschl 4890:             $bodytag .= Apache::lonmenu::secondary_menu();
                   4891:             $bodytag .= Apache::lonmenu::serverform();
1.920     raeburn  4892:             $bodytag .= Apache::lonhtmlcommon::scripttag('', 'end');
                   4893:             if ($env{'request.state'} eq 'construct') {
1.962     droeschl 4894:                 $bodytag .= &Apache::lonmenu::innerregister($forcereg,
1.920     raeburn  4895:                                 $args->{'bread_crumbs'});
                   4896:             } elsif ($forcereg) { 
                   4897:                 $bodytag .= &Apache::lonmenu::innerregister($forcereg);
                   4898:             }
1.903     droeschl 4899:         }else{
                   4900:             # this is to seperate menu from content when there's no secondary
                   4901:             # menu. Especially needed for public accessible ressources.
                   4902:             $bodytag .= '<hr style="clear:both" />';
                   4903:             $bodytag .= Apache::lonhtmlcommon::scripttag('', 'end'); 
1.235     raeburn  4904:         }
1.903     droeschl 4905: 
1.235     raeburn  4906:         return $bodytag;
1.182     matthew  4907: }
                   4908: 
1.917     raeburn  4909: sub dc_courseid_toggle {
                   4910:     my ($dc_info) = @_;
1.980     raeburn  4911:     return ' <span id="dccidtext" class="LC_cusr_subheading LC_nobreak">'.
1.917     raeburn  4912:            '<a href="javascript:showCourseID();">'.
                   4913:            &mt('(More ...)').'</a></span>'.
                   4914:            '<div id="dccid" class="LC_dccid">'.$dc_info.'</div>';
                   4915: }
                   4916: 
1.330     albertel 4917: sub make_attr_string {
                   4918:     my ($register,$attr_ref) = @_;
                   4919: 
                   4920:     if ($attr_ref && !ref($attr_ref)) {
                   4921: 	die("addentries Must be a hash ref ".
                   4922: 	    join(':',caller(1))." ".
                   4923: 	    join(':',caller(0))." ");
                   4924:     }
                   4925: 
                   4926:     if ($register) {
1.339     albertel 4927: 	my ($on_load,$on_unload);
                   4928: 	foreach my $key (keys(%{$attr_ref})) {
                   4929: 	    if      (lc($key) eq 'onload') {
                   4930: 		$on_load.=$attr_ref->{$key}.';';
                   4931: 		delete($attr_ref->{$key});
                   4932: 
                   4933: 	    } elsif (lc($key) eq 'onunload') {
                   4934: 		$on_unload.=$attr_ref->{$key}.';';
                   4935: 		delete($attr_ref->{$key});
                   4936: 	    }
                   4937: 	}
1.953     droeschl 4938: 	$attr_ref->{'onload'}  = $on_load;
                   4939: 	$attr_ref->{'onunload'}= $on_unload;
1.330     albertel 4940:     }
1.339     albertel 4941: 
1.330     albertel 4942:     my $attr_string;
                   4943:     foreach my $attr (keys(%$attr_ref)) {
                   4944: 	$attr_string .= " $attr=\"".$attr_ref->{$attr}.'" ';
                   4945:     }
                   4946:     return $attr_string;
                   4947: }
                   4948: 
                   4949: 
1.182     matthew  4950: ###############################################
1.251     albertel 4951: ###############################################
                   4952: 
                   4953: =pod
                   4954: 
                   4955: =item * &endbodytag()
                   4956: 
                   4957: Returns a uniform footer for LON-CAPA web pages.
                   4958: 
1.635     raeburn  4959: Inputs: 1 - optional reference to an args hash
                   4960: If in the hash, key for noredirectlink has a value which evaluates to true,
                   4961: a 'Continue' link is not displayed if the page contains an
                   4962: internal redirect in the <head></head> section,
                   4963: i.e., $env{'internal.head.redirect'} exists   
1.251     albertel 4964: 
                   4965: =cut
                   4966: 
                   4967: sub endbodytag {
1.635     raeburn  4968:     my ($args) = @_;
1.251     albertel 4969:     my $endbodytag='</body>';
1.269     albertel 4970:     $endbodytag=&Apache::lontexconvert::jsMath_process()."\n".$endbodytag;
1.315     albertel 4971:     if ( exists( $env{'internal.head.redirect'} ) ) {
1.635     raeburn  4972:         if (!(ref($args) eq 'HASH' && $args->{'noredirectlink'})) {
                   4973: 	    $endbodytag=
                   4974: 	        "<br /><a href=\"$env{'internal.head.redirect'}\">".
                   4975: 	        &mt('Continue').'</a>'.
                   4976: 	        $endbodytag;
                   4977:         }
1.315     albertel 4978:     }
1.251     albertel 4979:     return $endbodytag;
                   4980: }
                   4981: 
1.352     albertel 4982: =pod
                   4983: 
                   4984: =item * &standard_css()
                   4985: 
                   4986: Returns a style sheet
                   4987: 
                   4988: Inputs: (all optional)
                   4989:             domain         -> force to color decorate a page for a specific
                   4990:                                domain
                   4991:             function       -> force usage of a specific rolish color scheme
                   4992:             bgcolor        -> override the default page bgcolor
                   4993: 
                   4994: =cut
                   4995: 
1.343     albertel 4996: sub standard_css {
1.345     albertel 4997:     my ($function,$domain,$bgcolor) = @_;
1.352     albertel 4998:     $function  = &get_users_function() if (!$function);
                   4999:     my $img    = &designparm($function.'.img',   $domain);
                   5000:     my $tabbg  = &designparm($function.'.tabbg', $domain);
                   5001:     my $font   = &designparm($function.'.font',  $domain);
1.801     tempelho 5002:     my $fontmenu = &designparm($function.'.fontmenu', $domain);
1.791     tempelho 5003: #second colour for later usage
1.345     albertel 5004:     my $sidebg = &designparm($function.'.sidebg',$domain);
1.382     albertel 5005:     my $pgbg_or_bgcolor =
                   5006: 	         $bgcolor ||
1.352     albertel 5007: 	         &designparm($function.'.pgbg',  $domain);
1.382     albertel 5008:     my $pgbg   = &designparm($function.'.pgbg',  $domain);
1.352     albertel 5009:     my $alink  = &designparm($function.'.alink', $domain);
                   5010:     my $vlink  = &designparm($function.'.vlink', $domain);
                   5011:     my $link   = &designparm($function.'.link',  $domain);
                   5012: 
1.602     albertel 5013:     my $sans                 = 'Verdana,Arial,Helvetica,sans-serif';
1.395     albertel 5014:     my $mono                 = 'monospace';
1.850     bisitz   5015:     my $data_table_head      = $sidebg;
                   5016:     my $data_table_light     = '#FAFAFA';
1.1060  ! bisitz   5017:     my $data_table_dark      = '#E0E0E0';
1.470     banghart 5018:     my $data_table_darker    = '#CCCCCC';
1.349     albertel 5019:     my $data_table_highlight = '#FFFF00';
1.352     albertel 5020:     my $mail_new             = '#FFBB77';
                   5021:     my $mail_new_hover       = '#DD9955';
                   5022:     my $mail_read            = '#BBBB77';
                   5023:     my $mail_read_hover      = '#999944';
                   5024:     my $mail_replied         = '#AAAA88';
                   5025:     my $mail_replied_hover   = '#888855';
                   5026:     my $mail_other           = '#99BBBB';
                   5027:     my $mail_other_hover     = '#669999';
1.391     albertel 5028:     my $table_header         = '#DDDDDD';
1.489     raeburn  5029:     my $feedback_link_bg     = '#BBBBBB';
1.911     bisitz   5030:     my $lg_border_color      = '#C8C8C8';
1.952     onken    5031:     my $button_hover         = '#BF2317';
1.392     albertel 5032: 
1.608     albertel 5033:     my $border = ($env{'browser.type'} eq 'explorer' ||
1.911     bisitz   5034:       $env{'browser.type'} eq 'safari'     ) ? '0 2px 0 2px'
                   5035:                                              : '0 3px 0 4px';
1.448     albertel 5036: 
1.523     albertel 5037: 
1.343     albertel 5038:     return <<END;
1.947     droeschl 5039: 
                   5040: /* needed for iframe to allow 100% height in FF */
                   5041: body, html { 
                   5042:     margin: 0;
                   5043:     padding: 0 0.5%;
                   5044:     height: 99%; /* to avoid scrollbars */
                   5045: }
                   5046: 
1.795     www      5047: body {
1.911     bisitz   5048:   font-family: $sans;
                   5049:   line-height:130%;
                   5050:   font-size:0.83em;
                   5051:   color:$font;
1.795     www      5052: }
                   5053: 
1.959     onken    5054: a:focus,
                   5055: a:focus img {
1.795     www      5056:   color: red;
                   5057: }
1.698     harmsja  5058: 
1.911     bisitz   5059: form, .inline {
                   5060:   display: inline;
1.795     www      5061: }
1.721     harmsja  5062: 
1.795     www      5063: .LC_right {
1.911     bisitz   5064:   text-align:right;
1.795     www      5065: }
                   5066: 
                   5067: .LC_middle {
1.911     bisitz   5068:   vertical-align:middle;
1.795     www      5069: }
1.721     harmsja  5070: 
1.911     bisitz   5071: .LC_400Box {
                   5072:   width:400px;
                   5073: }
1.721     harmsja  5074: 
1.947     droeschl 5075: .LC_iframecontainer {
                   5076:     width: 98%;
                   5077:     margin: 0;
                   5078:     position: fixed;
                   5079:     top: 8.5em;
                   5080:     bottom: 0;
                   5081: }
                   5082: 
                   5083: .LC_iframecontainer iframe{
                   5084:     border: none;
                   5085:     width: 100%;
                   5086:     height: 100%;
                   5087: }
                   5088: 
1.778     bisitz   5089: .LC_filename {
                   5090:   font-family: $mono;
                   5091:   white-space:pre;
1.921     bisitz   5092:   font-size: 120%;
1.778     bisitz   5093: }
                   5094: 
                   5095: .LC_fileicon {
                   5096:   border: none;
                   5097:   height: 1.3em;
                   5098:   vertical-align: text-bottom;
                   5099:   margin-right: 0.3em;
                   5100:   text-decoration:none;
                   5101: }
                   5102: 
1.1008    www      5103: .LC_setting {
                   5104:   text-decoration:underline;
                   5105: }
                   5106: 
1.350     albertel 5107: .LC_error {
                   5108:   color: red;
                   5109:   font-size: larger;
                   5110: }
1.795     www      5111: 
1.457     albertel 5112: .LC_warning,
                   5113: .LC_diff_removed {
1.733     bisitz   5114:   color: red;
1.394     albertel 5115: }
1.532     albertel 5116: 
                   5117: .LC_info,
1.457     albertel 5118: .LC_success,
                   5119: .LC_diff_added {
1.350     albertel 5120:   color: green;
                   5121: }
1.795     www      5122: 
1.802     bisitz   5123: div.LC_confirm_box {
                   5124:   background-color: #FAFAFA;
                   5125:   border: 1px solid $lg_border_color;
                   5126:   margin-right: 0;
                   5127:   padding: 5px;
                   5128: }
                   5129: 
                   5130: div.LC_confirm_box .LC_error img,
                   5131: div.LC_confirm_box .LC_success img {
                   5132:   vertical-align: middle;
                   5133: }
                   5134: 
1.440     albertel 5135: .LC_icon {
1.771     droeschl 5136:   border: none;
1.790     droeschl 5137:   vertical-align: middle;
1.771     droeschl 5138: }
                   5139: 
1.543     albertel 5140: .LC_docs_spacer {
                   5141:   width: 25px;
                   5142:   height: 1px;
1.771     droeschl 5143:   border: none;
1.543     albertel 5144: }
1.346     albertel 5145: 
1.532     albertel 5146: .LC_internal_info {
1.735     bisitz   5147:   color: #999999;
1.532     albertel 5148: }
                   5149: 
1.794     www      5150: .LC_discussion {
1.1050    www      5151:   background: $data_table_dark;
1.911     bisitz   5152:   border: 1px solid black;
                   5153:   margin: 2px;
1.794     www      5154: }
                   5155: 
                   5156: .LC_disc_action_left {
1.1050    www      5157:   background: $sidebg;
1.911     bisitz   5158:   text-align: left;
1.1050    www      5159:   padding: 4px;
                   5160:   margin: 2px;
1.794     www      5161: }
                   5162: 
                   5163: .LC_disc_action_right {
1.1050    www      5164:   background: $sidebg;
1.911     bisitz   5165:   text-align: right;
1.1050    www      5166:   padding: 4px;
                   5167:   margin: 2px;
1.794     www      5168: }
                   5169: 
                   5170: .LC_disc_new_item {
1.911     bisitz   5171:   background: white;
                   5172:   border: 2px solid red;
1.1050    www      5173:   margin: 4px;
                   5174:   padding: 4px;
1.794     www      5175: }
                   5176: 
                   5177: .LC_disc_old_item {
1.911     bisitz   5178:   background: white;
1.1050    www      5179:   margin: 4px;
                   5180:   padding: 4px;
1.794     www      5181: }
                   5182: 
1.458     albertel 5183: table.LC_pastsubmission {
                   5184:   border: 1px solid black;
                   5185:   margin: 2px;
                   5186: }
                   5187: 
1.924     bisitz   5188: table#LC_menubuttons {
1.345     albertel 5189:   width: 100%;
                   5190:   background: $pgbg;
1.392     albertel 5191:   border: 2px;
1.402     albertel 5192:   border-collapse: separate;
1.803     bisitz   5193:   padding: 0;
1.345     albertel 5194: }
1.392     albertel 5195: 
1.801     tempelho 5196: table#LC_title_bar a {
                   5197:   color: $fontmenu;
                   5198: }
1.836     bisitz   5199: 
1.807     droeschl 5200: table#LC_title_bar {
1.819     tempelho 5201:   clear: both;
1.836     bisitz   5202:   display: none;
1.807     droeschl 5203: }
                   5204: 
1.795     www      5205: table#LC_title_bar,
1.933     droeschl 5206: table.LC_breadcrumbs, /* obsolete? */
1.393     albertel 5207: table#LC_title_bar.LC_with_remote {
1.359     albertel 5208:   width: 100%;
1.392     albertel 5209:   border-color: $pgbg;
                   5210:   border-style: solid;
                   5211:   border-width: $border;
1.379     albertel 5212:   background: $pgbg;
1.801     tempelho 5213:   color: $fontmenu;
1.392     albertel 5214:   border-collapse: collapse;
1.803     bisitz   5215:   padding: 0;
1.819     tempelho 5216:   margin: 0;
1.359     albertel 5217: }
1.795     www      5218: 
1.933     droeschl 5219: ul.LC_breadcrumb_tools_outerlist {
1.913     droeschl 5220:     margin: 0;
                   5221:     padding: 0;
1.933     droeschl 5222:     position: relative;
                   5223:     list-style: none;
1.913     droeschl 5224: }
1.933     droeschl 5225: ul.LC_breadcrumb_tools_outerlist li {
1.913     droeschl 5226:     display: inline;
                   5227: }
1.933     droeschl 5228: 
                   5229: .LC_breadcrumb_tools_navigation {
1.913     droeschl 5230:     padding: 0;
1.933     droeschl 5231:     margin: 0;
                   5232:     float: left;
1.913     droeschl 5233: }
1.933     droeschl 5234: .LC_breadcrumb_tools_tools {
                   5235:     padding: 0;
                   5236:     margin: 0;
1.913     droeschl 5237:     float: right;
                   5238: }
                   5239: 
1.359     albertel 5240: table#LC_title_bar td {
                   5241:   background: $tabbg;
                   5242: }
1.795     www      5243: 
1.911     bisitz   5244: table#LC_menubuttons img {
1.803     bisitz   5245:   border: none;
1.346     albertel 5246: }
1.795     www      5247: 
1.842     droeschl 5248: .LC_breadcrumbs_component {
1.911     bisitz   5249:   float: right;
                   5250:   margin: 0 1em;
1.357     albertel 5251: }
1.842     droeschl 5252: .LC_breadcrumbs_component img {
1.911     bisitz   5253:   vertical-align: middle;
1.777     tempelho 5254: }
1.795     www      5255: 
1.383     albertel 5256: td.LC_table_cell_checkbox {
                   5257:   text-align: center;
                   5258: }
1.795     www      5259: 
                   5260: .LC_fontsize_small {
1.911     bisitz   5261:   font-size: 70%;
1.705     tempelho 5262: }
                   5263: 
1.844     bisitz   5264: #LC_breadcrumbs {
1.911     bisitz   5265:   clear:both;
                   5266:   background: $sidebg;
                   5267:   border-bottom: 1px solid $lg_border_color;
                   5268:   line-height: 2.5em;
1.933     droeschl 5269:   overflow: hidden;
1.911     bisitz   5270:   margin: 0;
                   5271:   padding: 0;
1.995     raeburn  5272:   text-align: left;
1.819     tempelho 5273: }
1.862     bisitz   5274: 
1.993     raeburn  5275: .LC_head_subbox {
1.911     bisitz   5276:   clear:both;
                   5277:   background: #F8F8F8; /* $sidebg; */
1.915     droeschl 5278:   border: 1px solid $sidebg;
                   5279:   margin: 0 0 10px 0;      
1.966     bisitz   5280:   padding: 3px;
1.995     raeburn  5281:   text-align: left;
1.822     bisitz   5282: }
                   5283: 
1.795     www      5284: .LC_fontsize_medium {
1.911     bisitz   5285:   font-size: 85%;
1.705     tempelho 5286: }
                   5287: 
1.795     www      5288: .LC_fontsize_large {
1.911     bisitz   5289:   font-size: 120%;
1.705     tempelho 5290: }
                   5291: 
1.346     albertel 5292: .LC_menubuttons_inline_text {
                   5293:   color: $font;
1.698     harmsja  5294:   font-size: 90%;
1.701     harmsja  5295:   padding-left:3px;
1.346     albertel 5296: }
                   5297: 
1.934     droeschl 5298: .LC_menubuttons_inline_text img{
                   5299:   vertical-align: middle;
                   5300: }
                   5301: 
1.1051    www      5302: li.LC_menubuttons_inline_text img {
1.951     onken    5303:   cursor:pointer;
1.1002    droeschl 5304:   text-decoration: none;
1.951     onken    5305: }
                   5306: 
1.526     www      5307: .LC_menubuttons_link {
                   5308:   text-decoration: none;
                   5309: }
1.795     www      5310: 
1.522     albertel 5311: .LC_menubuttons_category {
1.521     www      5312:   color: $font;
1.526     www      5313:   background: $pgbg;
1.521     www      5314:   font-size: larger;
                   5315:   font-weight: bold;
                   5316: }
                   5317: 
1.346     albertel 5318: td.LC_menubuttons_text {
1.911     bisitz   5319:   color: $font;
1.346     albertel 5320: }
1.706     harmsja  5321: 
1.346     albertel 5322: .LC_current_location {
                   5323:   background: $tabbg;
                   5324: }
1.795     www      5325: 
1.938     bisitz   5326: table.LC_data_table {
1.347     albertel 5327:   border: 1px solid #000000;
1.402     albertel 5328:   border-collapse: separate;
1.426     albertel 5329:   border-spacing: 1px;
1.610     albertel 5330:   background: $pgbg;
1.347     albertel 5331: }
1.795     www      5332: 
1.422     albertel 5333: .LC_data_table_dense {
                   5334:   font-size: small;
                   5335: }
1.795     www      5336: 
1.507     raeburn  5337: table.LC_nested_outer {
                   5338:   border: 1px solid #000000;
1.589     raeburn  5339:   border-collapse: collapse;
1.803     bisitz   5340:   border-spacing: 0;
1.507     raeburn  5341:   width: 100%;
                   5342: }
1.795     www      5343: 
1.879     raeburn  5344: table.LC_innerpickbox,
1.507     raeburn  5345: table.LC_nested {
1.803     bisitz   5346:   border: none;
1.589     raeburn  5347:   border-collapse: collapse;
1.803     bisitz   5348:   border-spacing: 0;
1.507     raeburn  5349:   width: 100%;
                   5350: }
1.795     www      5351: 
1.911     bisitz   5352: table.LC_data_table tr th,
                   5353: table.LC_calendar tr th,
1.879     raeburn  5354: table.LC_prior_tries tr th,
                   5355: table.LC_innerpickbox tr th {
1.349     albertel 5356:   font-weight: bold;
                   5357:   background-color: $data_table_head;
1.801     tempelho 5358:   color:$fontmenu;
1.701     harmsja  5359:   font-size:90%;
1.347     albertel 5360: }
1.795     www      5361: 
1.879     raeburn  5362: table.LC_innerpickbox tr th,
                   5363: table.LC_innerpickbox tr td {
                   5364:   vertical-align: top;
                   5365: }
                   5366: 
1.711     raeburn  5367: table.LC_data_table tr.LC_info_row > td {
1.735     bisitz   5368:   background-color: #CCCCCC;
1.711     raeburn  5369:   font-weight: bold;
                   5370:   text-align: left;
                   5371: }
1.795     www      5372: 
1.912     bisitz   5373: table.LC_data_table tr.LC_odd_row > td {
                   5374:   background-color: $data_table_light;
                   5375:   padding: 2px;
                   5376:   vertical-align: top;
                   5377: }
                   5378: 
1.809     bisitz   5379: table.LC_pick_box tr > td.LC_odd_row {
1.349     albertel 5380:   background-color: $data_table_light;
1.912     bisitz   5381:   vertical-align: top;
                   5382: }
                   5383: 
                   5384: table.LC_data_table tr.LC_even_row > td {
                   5385:   background-color: $data_table_dark;
1.425     albertel 5386:   padding: 2px;
1.900     bisitz   5387:   vertical-align: top;
1.347     albertel 5388: }
1.795     www      5389: 
1.809     bisitz   5390: table.LC_pick_box tr > td.LC_even_row {
1.349     albertel 5391:   background-color: $data_table_dark;
1.900     bisitz   5392:   vertical-align: top;
1.347     albertel 5393: }
1.795     www      5394: 
1.425     albertel 5395: table.LC_data_table tr.LC_data_table_highlight td {
                   5396:   background-color: $data_table_darker;
                   5397: }
1.795     www      5398: 
1.639     raeburn  5399: table.LC_data_table tr td.LC_leftcol_header {
                   5400:   background-color: $data_table_head;
                   5401:   font-weight: bold;
                   5402: }
1.795     www      5403: 
1.451     albertel 5404: table.LC_data_table tr.LC_empty_row td,
1.507     raeburn  5405: table.LC_nested tr.LC_empty_row td {
1.421     albertel 5406:   font-weight: bold;
                   5407:   font-style: italic;
                   5408:   text-align: center;
                   5409:   padding: 8px;
1.347     albertel 5410: }
1.795     www      5411: 
1.940     bisitz   5412: table.LC_data_table tr.LC_empty_row td {
                   5413:   background-color: $sidebg;
                   5414: }
                   5415: 
                   5416: table.LC_nested tr.LC_empty_row td {
                   5417:   background-color: #FFFFFF;
                   5418: }
                   5419: 
1.890     droeschl 5420: table.LC_caption {
                   5421: }
                   5422: 
1.507     raeburn  5423: table.LC_nested tr.LC_empty_row td {
1.465     albertel 5424:   padding: 4ex
                   5425: }
1.795     www      5426: 
1.507     raeburn  5427: table.LC_nested_outer tr th {
                   5428:   font-weight: bold;
1.801     tempelho 5429:   color:$fontmenu;
1.507     raeburn  5430:   background-color: $data_table_head;
1.701     harmsja  5431:   font-size: small;
1.507     raeburn  5432:   border-bottom: 1px solid #000000;
                   5433: }
1.795     www      5434: 
1.507     raeburn  5435: table.LC_nested_outer tr td.LC_subheader {
                   5436:   background-color: $data_table_head;
                   5437:   font-weight: bold;
                   5438:   font-size: small;
                   5439:   border-bottom: 1px solid #000000;
                   5440:   text-align: right;
1.451     albertel 5441: }
1.795     www      5442: 
1.507     raeburn  5443: table.LC_nested tr.LC_info_row td {
1.735     bisitz   5444:   background-color: #CCCCCC;
1.451     albertel 5445:   font-weight: bold;
                   5446:   font-size: small;
1.507     raeburn  5447:   text-align: center;
                   5448: }
1.795     www      5449: 
1.589     raeburn  5450: table.LC_nested tr.LC_info_row td.LC_left_item,
                   5451: table.LC_nested_outer tr th.LC_left_item {
1.507     raeburn  5452:   text-align: left;
1.451     albertel 5453: }
1.795     www      5454: 
1.507     raeburn  5455: table.LC_nested td {
1.735     bisitz   5456:   background-color: #FFFFFF;
1.451     albertel 5457:   font-size: small;
1.507     raeburn  5458: }
1.795     www      5459: 
1.507     raeburn  5460: table.LC_nested_outer tr th.LC_right_item,
                   5461: table.LC_nested tr.LC_info_row td.LC_right_item,
                   5462: table.LC_nested tr.LC_odd_row td.LC_right_item,
                   5463: table.LC_nested tr td.LC_right_item {
1.451     albertel 5464:   text-align: right;
                   5465: }
                   5466: 
1.507     raeburn  5467: table.LC_nested tr.LC_odd_row td {
1.735     bisitz   5468:   background-color: #EEEEEE;
1.451     albertel 5469: }
                   5470: 
1.473     raeburn  5471: table.LC_createuser {
                   5472: }
                   5473: 
                   5474: table.LC_createuser tr.LC_section_row td {
1.701     harmsja  5475:   font-size: small;
1.473     raeburn  5476: }
                   5477: 
                   5478: table.LC_createuser tr.LC_info_row td  {
1.735     bisitz   5479:   background-color: #CCCCCC;
1.473     raeburn  5480:   font-weight: bold;
                   5481:   text-align: center;
                   5482: }
                   5483: 
1.349     albertel 5484: table.LC_calendar {
                   5485:   border: 1px solid #000000;
                   5486:   border-collapse: collapse;
1.917     raeburn  5487:   width: 98%;
1.349     albertel 5488: }
1.795     www      5489: 
1.349     albertel 5490: table.LC_calendar_pickdate {
                   5491:   font-size: xx-small;
                   5492: }
1.795     www      5493: 
1.349     albertel 5494: table.LC_calendar tr td {
                   5495:   border: 1px solid #000000;
                   5496:   vertical-align: top;
1.917     raeburn  5497:   width: 14%;
1.349     albertel 5498: }
1.795     www      5499: 
1.349     albertel 5500: table.LC_calendar tr td.LC_calendar_day_empty {
                   5501:   background-color: $data_table_dark;
                   5502: }
1.795     www      5503: 
1.779     bisitz   5504: table.LC_calendar tr td.LC_calendar_day_current {
                   5505:   background-color: $data_table_highlight;
1.777     tempelho 5506: }
1.795     www      5507: 
1.938     bisitz   5508: table.LC_data_table tr td.LC_mail_new {
1.349     albertel 5509:   background-color: $mail_new;
                   5510: }
1.795     www      5511: 
1.938     bisitz   5512: table.LC_data_table tr.LC_mail_new:hover {
1.349     albertel 5513:   background-color: $mail_new_hover;
                   5514: }
1.795     www      5515: 
1.938     bisitz   5516: table.LC_data_table tr td.LC_mail_read {
1.349     albertel 5517:   background-color: $mail_read;
                   5518: }
1.795     www      5519: 
1.938     bisitz   5520: /*
                   5521: table.LC_data_table tr.LC_mail_read:hover {
1.349     albertel 5522:   background-color: $mail_read_hover;
                   5523: }
1.938     bisitz   5524: */
1.795     www      5525: 
1.938     bisitz   5526: table.LC_data_table tr td.LC_mail_replied {
1.349     albertel 5527:   background-color: $mail_replied;
                   5528: }
1.795     www      5529: 
1.938     bisitz   5530: /*
                   5531: table.LC_data_table tr.LC_mail_replied:hover {
1.349     albertel 5532:   background-color: $mail_replied_hover;
                   5533: }
1.938     bisitz   5534: */
1.795     www      5535: 
1.938     bisitz   5536: table.LC_data_table tr td.LC_mail_other {
1.349     albertel 5537:   background-color: $mail_other;
                   5538: }
1.795     www      5539: 
1.938     bisitz   5540: /*
                   5541: table.LC_data_table tr.LC_mail_other:hover {
1.349     albertel 5542:   background-color: $mail_other_hover;
                   5543: }
1.938     bisitz   5544: */
1.494     raeburn  5545: 
1.777     tempelho 5546: table.LC_data_table tr > td.LC_browser_file,
                   5547: table.LC_data_table tr > td.LC_browser_file_published {
1.899     bisitz   5548:   background: #AAEE77;
1.389     albertel 5549: }
1.795     www      5550: 
1.777     tempelho 5551: table.LC_data_table tr > td.LC_browser_file_locked,
                   5552: table.LC_data_table tr > td.LC_browser_file_unpublished {
1.389     albertel 5553:   background: #FFAA99;
1.387     albertel 5554: }
1.795     www      5555: 
1.777     tempelho 5556: table.LC_data_table tr > td.LC_browser_file_obsolete {
1.899     bisitz   5557:   background: #888888;
1.779     bisitz   5558: }
1.795     www      5559: 
1.777     tempelho 5560: table.LC_data_table tr > td.LC_browser_file_modified,
1.779     bisitz   5561: table.LC_data_table tr > td.LC_browser_file_metamodified {
1.899     bisitz   5562:   background: #F8F866;
1.777     tempelho 5563: }
1.795     www      5564: 
1.696     bisitz   5565: table.LC_data_table tr.LC_browser_folder > td {
1.899     bisitz   5566:   background: #E0E8FF;
1.387     albertel 5567: }
1.696     bisitz   5568: 
1.707     bisitz   5569: table.LC_data_table tr > td.LC_roles_is {
1.911     bisitz   5570:   /* background: #77FF77; */
1.707     bisitz   5571: }
1.795     www      5572: 
1.707     bisitz   5573: table.LC_data_table tr > td.LC_roles_future {
1.939     bisitz   5574:   border-right: 8px solid #FFFF77;
1.707     bisitz   5575: }
1.795     www      5576: 
1.707     bisitz   5577: table.LC_data_table tr > td.LC_roles_will {
1.939     bisitz   5578:   border-right: 8px solid #FFAA77;
1.707     bisitz   5579: }
1.795     www      5580: 
1.707     bisitz   5581: table.LC_data_table tr > td.LC_roles_expired {
1.939     bisitz   5582:   border-right: 8px solid #FF7777;
1.707     bisitz   5583: }
1.795     www      5584: 
1.707     bisitz   5585: table.LC_data_table tr > td.LC_roles_will_not {
1.939     bisitz   5586:   border-right: 8px solid #AAFF77;
1.707     bisitz   5587: }
1.795     www      5588: 
1.707     bisitz   5589: table.LC_data_table tr > td.LC_roles_selected {
1.939     bisitz   5590:   border-right: 8px solid #11CC55;
1.707     bisitz   5591: }
                   5592: 
1.388     albertel 5593: span.LC_current_location {
1.701     harmsja  5594:   font-size:larger;
1.388     albertel 5595:   background: $pgbg;
                   5596: }
1.387     albertel 5597: 
1.1029    www      5598: span.LC_current_nav_location {
                   5599:   font-weight:bold;
                   5600:   background: $sidebg;
                   5601: }
                   5602: 
1.395     albertel 5603: span.LC_parm_menu_item {
                   5604:   font-size: larger;
                   5605: }
1.795     www      5606: 
1.395     albertel 5607: span.LC_parm_scope_all {
                   5608:   color: red;
                   5609: }
1.795     www      5610: 
1.395     albertel 5611: span.LC_parm_scope_folder {
                   5612:   color: green;
                   5613: }
1.795     www      5614: 
1.395     albertel 5615: span.LC_parm_scope_resource {
                   5616:   color: orange;
                   5617: }
1.795     www      5618: 
1.395     albertel 5619: span.LC_parm_part {
                   5620:   color: blue;
                   5621: }
1.795     www      5622: 
1.911     bisitz   5623: span.LC_parm_folder,
                   5624: span.LC_parm_symb {
1.395     albertel 5625:   font-size: x-small;
                   5626:   font-family: $mono;
                   5627:   color: #AAAAAA;
                   5628: }
                   5629: 
1.977     bisitz   5630: ul.LC_parm_parmlist li {
                   5631:   display: inline-block;
                   5632:   padding: 0.3em 0.8em;
                   5633:   vertical-align: top;
                   5634:   width: 150px;
                   5635:   border-top:1px solid $lg_border_color;
                   5636: }
                   5637: 
1.795     www      5638: td.LC_parm_overview_level_menu,
                   5639: td.LC_parm_overview_map_menu,
                   5640: td.LC_parm_overview_parm_selectors,
                   5641: td.LC_parm_overview_restrictions  {
1.396     albertel 5642:   border: 1px solid black;
                   5643:   border-collapse: collapse;
                   5644: }
1.795     www      5645: 
1.396     albertel 5646: table.LC_parm_overview_restrictions td {
                   5647:   border-width: 1px 4px 1px 4px;
                   5648:   border-style: solid;
                   5649:   border-color: $pgbg;
                   5650:   text-align: center;
                   5651: }
1.795     www      5652: 
1.396     albertel 5653: table.LC_parm_overview_restrictions th {
                   5654:   background: $tabbg;
                   5655:   border-width: 1px 4px 1px 4px;
                   5656:   border-style: solid;
                   5657:   border-color: $pgbg;
                   5658: }
1.795     www      5659: 
1.398     albertel 5660: table#LC_helpmenu {
1.803     bisitz   5661:   border: none;
1.398     albertel 5662:   height: 55px;
1.803     bisitz   5663:   border-spacing: 0;
1.398     albertel 5664: }
                   5665: 
                   5666: table#LC_helpmenu fieldset legend {
                   5667:   font-size: larger;
                   5668: }
1.795     www      5669: 
1.397     albertel 5670: table#LC_helpmenu_links {
                   5671:   width: 100%;
                   5672:   border: 1px solid black;
                   5673:   background: $pgbg;
1.803     bisitz   5674:   padding: 0;
1.397     albertel 5675:   border-spacing: 1px;
                   5676: }
1.795     www      5677: 
1.397     albertel 5678: table#LC_helpmenu_links tr td {
                   5679:   padding: 1px;
                   5680:   background: $tabbg;
1.399     albertel 5681:   text-align: center;
                   5682:   font-weight: bold;
1.397     albertel 5683: }
1.396     albertel 5684: 
1.795     www      5685: table#LC_helpmenu_links a:link,
                   5686: table#LC_helpmenu_links a:visited,
1.397     albertel 5687: table#LC_helpmenu_links a:active {
                   5688:   text-decoration: none;
                   5689:   color: $font;
                   5690: }
1.795     www      5691: 
1.397     albertel 5692: table#LC_helpmenu_links a:hover {
                   5693:   text-decoration: underline;
                   5694:   color: $vlink;
                   5695: }
1.396     albertel 5696: 
1.417     albertel 5697: .LC_chrt_popup_exists {
                   5698:   border: 1px solid #339933;
                   5699:   margin: -1px;
                   5700: }
1.795     www      5701: 
1.417     albertel 5702: .LC_chrt_popup_up {
                   5703:   border: 1px solid yellow;
                   5704:   margin: -1px;
                   5705: }
1.795     www      5706: 
1.417     albertel 5707: .LC_chrt_popup {
                   5708:   border: 1px solid #8888FF;
                   5709:   background: #CCCCFF;
                   5710: }
1.795     www      5711: 
1.421     albertel 5712: table.LC_pick_box {
                   5713:   border-collapse: separate;
                   5714:   background: white;
                   5715:   border: 1px solid black;
                   5716:   border-spacing: 1px;
                   5717: }
1.795     www      5718: 
1.421     albertel 5719: table.LC_pick_box td.LC_pick_box_title {
1.850     bisitz   5720:   background: $sidebg;
1.421     albertel 5721:   font-weight: bold;
1.900     bisitz   5722:   text-align: left;
1.740     bisitz   5723:   vertical-align: top;
1.421     albertel 5724:   width: 184px;
                   5725:   padding: 8px;
                   5726: }
1.795     www      5727: 
1.579     raeburn  5728: table.LC_pick_box td.LC_pick_box_value {
                   5729:   text-align: left;
                   5730:   padding: 8px;
                   5731: }
1.795     www      5732: 
1.579     raeburn  5733: table.LC_pick_box td.LC_pick_box_select {
                   5734:   text-align: left;
                   5735:   padding: 8px;
                   5736: }
1.795     www      5737: 
1.424     albertel 5738: table.LC_pick_box td.LC_pick_box_separator {
1.803     bisitz   5739:   padding: 0;
1.421     albertel 5740:   height: 1px;
                   5741:   background: black;
                   5742: }
1.795     www      5743: 
1.421     albertel 5744: table.LC_pick_box td.LC_pick_box_submit {
                   5745:   text-align: right;
                   5746: }
1.795     www      5747: 
1.579     raeburn  5748: table.LC_pick_box td.LC_evenrow_value {
                   5749:   text-align: left;
                   5750:   padding: 8px;
                   5751:   background-color: $data_table_light;
                   5752: }
1.795     www      5753: 
1.579     raeburn  5754: table.LC_pick_box td.LC_oddrow_value {
                   5755:   text-align: left;
                   5756:   padding: 8px;
                   5757:   background-color: $data_table_light;
                   5758: }
1.795     www      5759: 
1.579     raeburn  5760: span.LC_helpform_receipt_cat {
                   5761:   font-weight: bold;
                   5762: }
1.795     www      5763: 
1.424     albertel 5764: table.LC_group_priv_box {
                   5765:   background: white;
                   5766:   border: 1px solid black;
                   5767:   border-spacing: 1px;
                   5768: }
1.795     www      5769: 
1.424     albertel 5770: table.LC_group_priv_box td.LC_pick_box_title {
                   5771:   background: $tabbg;
                   5772:   font-weight: bold;
                   5773:   text-align: right;
                   5774:   width: 184px;
                   5775: }
1.795     www      5776: 
1.424     albertel 5777: table.LC_group_priv_box td.LC_groups_fixed {
                   5778:   background: $data_table_light;
                   5779:   text-align: center;
                   5780: }
1.795     www      5781: 
1.424     albertel 5782: table.LC_group_priv_box td.LC_groups_optional {
                   5783:   background: $data_table_dark;
                   5784:   text-align: center;
                   5785: }
1.795     www      5786: 
1.424     albertel 5787: table.LC_group_priv_box td.LC_groups_functionality {
                   5788:   background: $data_table_darker;
                   5789:   text-align: center;
                   5790:   font-weight: bold;
                   5791: }
1.795     www      5792: 
1.424     albertel 5793: table.LC_group_priv td {
                   5794:   text-align: left;
1.803     bisitz   5795:   padding: 0;
1.424     albertel 5796: }
                   5797: 
                   5798: .LC_navbuttons {
                   5799:   margin: 2ex 0ex 2ex 0ex;
                   5800: }
1.795     www      5801: 
1.423     albertel 5802: .LC_topic_bar {
                   5803:   font-weight: bold;
                   5804:   background: $tabbg;
1.918     wenzelju 5805:   margin: 1em 0em 1em 2em;
1.805     bisitz   5806:   padding: 3px;
1.918     wenzelju 5807:   font-size: 1.2em;
1.423     albertel 5808: }
1.795     www      5809: 
1.423     albertel 5810: .LC_topic_bar span {
1.918     wenzelju 5811:   left: 0.5em;
                   5812:   position: absolute;
1.423     albertel 5813:   vertical-align: middle;
1.918     wenzelju 5814:   font-size: 1.2em;
1.423     albertel 5815: }
1.795     www      5816: 
1.423     albertel 5817: table.LC_course_group_status {
                   5818:   margin: 20px;
                   5819: }
1.795     www      5820: 
1.423     albertel 5821: table.LC_status_selector td {
                   5822:   vertical-align: top;
                   5823:   text-align: center;
1.424     albertel 5824:   padding: 4px;
                   5825: }
1.795     www      5826: 
1.599     albertel 5827: div.LC_feedback_link {
1.616     albertel 5828:   clear: both;
1.829     kalberla 5829:   background: $sidebg;
1.779     bisitz   5830:   width: 100%;
1.829     kalberla 5831:   padding-bottom: 10px;
                   5832:   border: 1px $tabbg solid;
1.833     kalberla 5833:   height: 22px;
                   5834:   line-height: 22px;
                   5835:   padding-top: 5px;
                   5836: }
                   5837: 
                   5838: div.LC_feedback_link img {
                   5839:   height: 22px;
1.867     kalberla 5840:   vertical-align:middle;
1.829     kalberla 5841: }
                   5842: 
1.911     bisitz   5843: div.LC_feedback_link a {
1.829     kalberla 5844:   text-decoration: none;
1.489     raeburn  5845: }
1.795     www      5846: 
1.867     kalberla 5847: div.LC_comblock {
1.911     bisitz   5848:   display:inline;
1.867     kalberla 5849:   color:$font;
                   5850:   font-size:90%;
                   5851: }
                   5852: 
                   5853: div.LC_feedback_link div.LC_comblock {
                   5854:   padding-left:5px;
                   5855: }
                   5856: 
                   5857: div.LC_feedback_link div.LC_comblock a {
                   5858:   color:$font;
                   5859: }
                   5860: 
1.489     raeburn  5861: span.LC_feedback_link {
1.858     bisitz   5862:   /* background: $feedback_link_bg; */
1.599     albertel 5863:   font-size: larger;
                   5864: }
1.795     www      5865: 
1.599     albertel 5866: span.LC_message_link {
1.858     bisitz   5867:   /* background: $feedback_link_bg; */
1.599     albertel 5868:   font-size: larger;
                   5869:   position: absolute;
                   5870:   right: 1em;
1.489     raeburn  5871: }
1.421     albertel 5872: 
1.515     albertel 5873: table.LC_prior_tries {
1.524     albertel 5874:   border: 1px solid #000000;
                   5875:   border-collapse: separate;
                   5876:   border-spacing: 1px;
1.515     albertel 5877: }
1.523     albertel 5878: 
1.515     albertel 5879: table.LC_prior_tries td {
1.524     albertel 5880:   padding: 2px;
1.515     albertel 5881: }
1.523     albertel 5882: 
                   5883: .LC_answer_correct {
1.795     www      5884:   background: lightgreen;
                   5885:   color: darkgreen;
                   5886:   padding: 6px;
1.523     albertel 5887: }
1.795     www      5888: 
1.523     albertel 5889: .LC_answer_charged_try {
1.797     www      5890:   background: #FFAAAA;
1.795     www      5891:   color: darkred;
                   5892:   padding: 6px;
1.523     albertel 5893: }
1.795     www      5894: 
1.779     bisitz   5895: .LC_answer_not_charged_try,
1.523     albertel 5896: .LC_answer_no_grade,
                   5897: .LC_answer_late {
1.795     www      5898:   background: lightyellow;
1.523     albertel 5899:   color: black;
1.795     www      5900:   padding: 6px;
1.523     albertel 5901: }
1.795     www      5902: 
1.523     albertel 5903: .LC_answer_previous {
1.795     www      5904:   background: lightblue;
                   5905:   color: darkblue;
                   5906:   padding: 6px;
1.523     albertel 5907: }
1.795     www      5908: 
1.779     bisitz   5909: .LC_answer_no_message {
1.777     tempelho 5910:   background: #FFFFFF;
                   5911:   color: black;
1.795     www      5912:   padding: 6px;
1.779     bisitz   5913: }
1.795     www      5914: 
1.779     bisitz   5915: .LC_answer_unknown {
                   5916:   background: orange;
                   5917:   color: black;
1.795     www      5918:   padding: 6px;
1.777     tempelho 5919: }
1.795     www      5920: 
1.529     albertel 5921: span.LC_prior_numerical,
                   5922: span.LC_prior_string,
                   5923: span.LC_prior_custom,
                   5924: span.LC_prior_reaction,
                   5925: span.LC_prior_math {
1.925     bisitz   5926:   font-family: $mono;
1.523     albertel 5927:   white-space: pre;
                   5928: }
                   5929: 
1.525     albertel 5930: span.LC_prior_string {
1.925     bisitz   5931:   font-family: $mono;
1.525     albertel 5932:   white-space: pre;
                   5933: }
                   5934: 
1.523     albertel 5935: table.LC_prior_option {
                   5936:   width: 100%;
                   5937:   border-collapse: collapse;
                   5938: }
1.795     www      5939: 
1.911     bisitz   5940: table.LC_prior_rank,
1.795     www      5941: table.LC_prior_match {
1.528     albertel 5942:   border-collapse: collapse;
                   5943: }
1.795     www      5944: 
1.528     albertel 5945: table.LC_prior_option tr td,
                   5946: table.LC_prior_rank tr td,
                   5947: table.LC_prior_match tr td {
1.524     albertel 5948:   border: 1px solid #000000;
1.515     albertel 5949: }
                   5950: 
1.855     bisitz   5951: .LC_nobreak {
1.544     albertel 5952:   white-space: nowrap;
1.519     raeburn  5953: }
                   5954: 
1.576     raeburn  5955: span.LC_cusr_emph {
                   5956:   font-style: italic;
                   5957: }
                   5958: 
1.633     raeburn  5959: span.LC_cusr_subheading {
                   5960:   font-weight: normal;
                   5961:   font-size: 85%;
                   5962: }
                   5963: 
1.861     bisitz   5964: div.LC_docs_entry_move {
1.859     bisitz   5965:   border: 1px solid #BBBBBB;
1.545     albertel 5966:   background: #DDDDDD;
1.861     bisitz   5967:   width: 22px;
1.859     bisitz   5968:   padding: 1px;
                   5969:   margin: 0;
1.545     albertel 5970: }
                   5971: 
1.861     bisitz   5972: table.LC_data_table tr > td.LC_docs_entry_commands,
                   5973: table.LC_data_table tr > td.LC_docs_entry_parameter {
1.545     albertel 5974:   background: #DDDDDD;
                   5975:   font-size: x-small;
                   5976: }
1.795     www      5977: 
1.861     bisitz   5978: .LC_docs_entry_parameter {
                   5979:   white-space: nowrap;
                   5980: }
                   5981: 
1.544     albertel 5982: .LC_docs_copy {
1.545     albertel 5983:   color: #000099;
1.544     albertel 5984: }
1.795     www      5985: 
1.544     albertel 5986: .LC_docs_cut {
1.545     albertel 5987:   color: #550044;
1.544     albertel 5988: }
1.795     www      5989: 
1.544     albertel 5990: .LC_docs_rename {
1.545     albertel 5991:   color: #009900;
1.544     albertel 5992: }
1.795     www      5993: 
1.544     albertel 5994: .LC_docs_remove {
1.545     albertel 5995:   color: #990000;
                   5996: }
                   5997: 
1.547     albertel 5998: .LC_docs_reinit_warn,
                   5999: .LC_docs_ext_edit {
                   6000:   font-size: x-small;
                   6001: }
                   6002: 
1.545     albertel 6003: table.LC_docs_adddocs td,
                   6004: table.LC_docs_adddocs th {
                   6005:   border: 1px solid #BBBBBB;
                   6006:   padding: 4px;
                   6007:   background: #DDDDDD;
1.543     albertel 6008: }
                   6009: 
1.584     albertel 6010: table.LC_sty_begin {
                   6011:   background: #BBFFBB;
                   6012: }
1.795     www      6013: 
1.584     albertel 6014: table.LC_sty_end {
                   6015:   background: #FFBBBB;
                   6016: }
                   6017: 
1.589     raeburn  6018: table.LC_double_column {
1.803     bisitz   6019:   border-width: 0;
1.589     raeburn  6020:   border-collapse: collapse;
                   6021:   width: 100%;
                   6022:   padding: 2px;
                   6023: }
                   6024: 
                   6025: table.LC_double_column tr td.LC_left_col {
1.590     raeburn  6026:   top: 2px;
1.589     raeburn  6027:   left: 2px;
                   6028:   width: 47%;
                   6029:   vertical-align: top;
                   6030: }
                   6031: 
                   6032: table.LC_double_column tr td.LC_right_col {
                   6033:   top: 2px;
1.779     bisitz   6034:   right: 2px;
1.589     raeburn  6035:   width: 47%;
                   6036:   vertical-align: top;
                   6037: }
                   6038: 
1.591     raeburn  6039: div.LC_left_float {
                   6040:   float: left;
                   6041:   padding-right: 5%;
1.597     albertel 6042:   padding-bottom: 4px;
1.591     raeburn  6043: }
                   6044: 
                   6045: div.LC_clear_float_header {
1.597     albertel 6046:   padding-bottom: 2px;
1.591     raeburn  6047: }
                   6048: 
                   6049: div.LC_clear_float_footer {
1.597     albertel 6050:   padding-top: 10px;
1.591     raeburn  6051:   clear: both;
                   6052: }
                   6053: 
1.597     albertel 6054: div.LC_grade_show_user {
1.941     bisitz   6055: /*  border-left: 5px solid $sidebg; */
                   6056:   border-top: 5px solid #000000;
                   6057:   margin: 50px 0 0 0;
1.936     bisitz   6058:   padding: 15px 0 5px 10px;
1.597     albertel 6059: }
1.795     www      6060: 
1.936     bisitz   6061: div.LC_grade_show_user_odd_row {
1.941     bisitz   6062: /*  border-left: 5px solid #000000; */
                   6063: }
                   6064: 
                   6065: div.LC_grade_show_user div.LC_Box {
                   6066:   margin-right: 50px;
1.597     albertel 6067: }
                   6068: 
                   6069: div.LC_grade_submissions,
                   6070: div.LC_grade_message_center,
1.936     bisitz   6071: div.LC_grade_info_links {
1.597     albertel 6072:   margin: 5px;
                   6073:   width: 99%;
                   6074:   background: #FFFFFF;
                   6075: }
1.795     www      6076: 
1.597     albertel 6077: div.LC_grade_submissions_header,
1.936     bisitz   6078: div.LC_grade_message_center_header {
1.705     tempelho 6079:   font-weight: bold;
                   6080:   font-size: large;
1.597     albertel 6081: }
1.795     www      6082: 
1.597     albertel 6083: div.LC_grade_submissions_body,
1.936     bisitz   6084: div.LC_grade_message_center_body {
1.597     albertel 6085:   border: 1px solid black;
                   6086:   width: 99%;
                   6087:   background: #FFFFFF;
                   6088: }
1.795     www      6089: 
1.613     albertel 6090: table.LC_scantron_action {
                   6091:   width: 100%;
                   6092: }
1.795     www      6093: 
1.613     albertel 6094: table.LC_scantron_action tr th {
1.698     harmsja  6095:   font-weight:bold;
                   6096:   font-style:normal;
1.613     albertel 6097: }
1.795     www      6098: 
1.779     bisitz   6099: .LC_edit_problem_header,
1.614     albertel 6100: div.LC_edit_problem_footer {
1.705     tempelho 6101:   font-weight: normal;
                   6102:   font-size:  medium;
1.602     albertel 6103:   margin: 2px;
1.1060  ! bisitz   6104:   background-color: $sidebg;
1.600     albertel 6105: }
1.795     www      6106: 
1.600     albertel 6107: div.LC_edit_problem_header,
1.602     albertel 6108: div.LC_edit_problem_header div,
1.614     albertel 6109: div.LC_edit_problem_footer,
                   6110: div.LC_edit_problem_footer div,
1.602     albertel 6111: div.LC_edit_problem_editxml_header,
                   6112: div.LC_edit_problem_editxml_header div {
1.600     albertel 6113:   margin-top: 5px;
                   6114: }
1.795     www      6115: 
1.600     albertel 6116: div.LC_edit_problem_header_title {
1.705     tempelho 6117:   font-weight: bold;
                   6118:   font-size: larger;
1.602     albertel 6119:   background: $tabbg;
                   6120:   padding: 3px;
1.1060  ! bisitz   6121:   margin: 0 0 5px 0;
1.602     albertel 6122: }
1.795     www      6123: 
1.602     albertel 6124: table.LC_edit_problem_header_title {
                   6125:   width: 100%;
1.600     albertel 6126:   background: $tabbg;
1.602     albertel 6127: }
                   6128: 
                   6129: div.LC_edit_problem_discards {
                   6130:   float: left;
                   6131:   padding-bottom: 5px;
                   6132: }
1.795     www      6133: 
1.602     albertel 6134: div.LC_edit_problem_saves {
                   6135:   float: right;
                   6136:   padding-bottom: 5px;
1.600     albertel 6137: }
1.795     www      6138: 
1.911     bisitz   6139: img.stift {
1.803     bisitz   6140:   border-width: 0;
                   6141:   vertical-align: middle;
1.677     riegler  6142: }
1.680     riegler  6143: 
1.923     bisitz   6144: table td.LC_mainmenu_col_fieldset {
1.680     riegler  6145:   vertical-align: top;
1.777     tempelho 6146: }
1.795     www      6147: 
1.716     raeburn  6148: div.LC_createcourse {
1.911     bisitz   6149:   margin: 10px 10px 10px 10px;
1.716     raeburn  6150: }
                   6151: 
1.917     raeburn  6152: .LC_dccid {
                   6153:   margin: 0.2em 0 0 0;
                   6154:   padding: 0;
                   6155:   font-size: 90%;
                   6156:   display:none;
                   6157: }
                   6158: 
1.897     wenzelju 6159: ol.LC_primary_menu a:hover,
1.721     harmsja  6160: ol#LC_MenuBreadcrumbs a:hover,
                   6161: ol#LC_PathBreadcrumbs a:hover,
1.897     wenzelju 6162: ul#LC_secondary_menu a:hover,
1.721     harmsja  6163: .LC_FormSectionClearButton input:hover
1.795     www      6164: ul.LC_TabContent   li:hover a {
1.952     onken    6165:   color:$button_hover;
1.911     bisitz   6166:   text-decoration:none;
1.693     droeschl 6167: }
                   6168: 
1.779     bisitz   6169: h1 {
1.911     bisitz   6170:   padding: 0;
                   6171:   line-height:130%;
1.693     droeschl 6172: }
1.698     harmsja  6173: 
1.911     bisitz   6174: h2,
                   6175: h3,
                   6176: h4,
                   6177: h5,
                   6178: h6 {
                   6179:   margin: 5px 0 5px 0;
                   6180:   padding: 0;
                   6181:   line-height:130%;
1.693     droeschl 6182: }
1.795     www      6183: 
                   6184: .LC_hcell {
1.911     bisitz   6185:   padding:3px 15px 3px 15px;
                   6186:   margin: 0;
                   6187:   background-color:$tabbg;
                   6188:   color:$fontmenu;
                   6189:   border-bottom:solid 1px $lg_border_color;
1.693     droeschl 6190: }
1.795     www      6191: 
1.840     bisitz   6192: .LC_Box > .LC_hcell {
1.911     bisitz   6193:   margin: 0 -10px 10px -10px;
1.835     bisitz   6194: }
                   6195: 
1.721     harmsja  6196: .LC_noBorder {
1.911     bisitz   6197:   border: 0;
1.698     harmsja  6198: }
1.693     droeschl 6199: 
1.721     harmsja  6200: .LC_FormSectionClearButton input {
1.911     bisitz   6201:   background-color:transparent;
                   6202:   border: none;
                   6203:   cursor:pointer;
                   6204:   text-decoration:underline;
1.693     droeschl 6205: }
1.763     bisitz   6206: 
                   6207: .LC_help_open_topic {
1.911     bisitz   6208:   color: #FFFFFF;
                   6209:   background-color: #EEEEFF;
                   6210:   margin: 1px;
                   6211:   padding: 4px;
                   6212:   border: 1px solid #000033;
                   6213:   white-space: nowrap;
                   6214:   /* vertical-align: middle; */
1.759     neumanie 6215: }
1.693     droeschl 6216: 
1.911     bisitz   6217: dl,
                   6218: ul,
                   6219: div,
                   6220: fieldset {
                   6221:   margin: 10px 10px 10px 0;
                   6222:   /* overflow: hidden; */
1.693     droeschl 6223: }
1.795     www      6224: 
1.838     bisitz   6225: fieldset > legend {
1.911     bisitz   6226:   font-weight: bold;
                   6227:   padding: 0 5px 0 5px;
1.838     bisitz   6228: }
                   6229: 
1.813     bisitz   6230: #LC_nav_bar {
1.911     bisitz   6231:   float: left;
1.995     raeburn  6232:   background-color: $pgbg_or_bgcolor;
1.966     bisitz   6233:   margin: 0 0 2px 0;
1.807     droeschl 6234: }
                   6235: 
1.916     droeschl 6236: #LC_realm {
                   6237:   margin: 0.2em 0 0 0;
                   6238:   padding: 0;
                   6239:   font-weight: bold;
                   6240:   text-align: center;
1.995     raeburn  6241:   background-color: $pgbg_or_bgcolor;
1.916     droeschl 6242: }
                   6243: 
1.911     bisitz   6244: #LC_nav_bar em {
                   6245:   font-weight: bold;
                   6246:   font-style: normal;
1.807     droeschl 6247: }
                   6248: 
1.897     wenzelju 6249: ol.LC_primary_menu {
1.911     bisitz   6250:   float: right;
1.934     droeschl 6251:   margin: 0;
1.995     raeburn  6252:   background-color: $pgbg_or_bgcolor;
1.807     droeschl 6253: }
                   6254: 
1.852     droeschl 6255: ol#LC_PathBreadcrumbs {
1.911     bisitz   6256:   margin: 0;
1.693     droeschl 6257: }
                   6258: 
1.897     wenzelju 6259: ol.LC_primary_menu li {
1.911     bisitz   6260:   display: inline;
                   6261:   padding: 5px 5px 0 10px;
                   6262:   vertical-align: top;
1.693     droeschl 6263: }
                   6264: 
1.897     wenzelju 6265: ol.LC_primary_menu li img {
1.911     bisitz   6266:   vertical-align: bottom;
1.934     droeschl 6267:   height: 1.1em;
1.693     droeschl 6268: }
                   6269: 
1.897     wenzelju 6270: ol.LC_primary_menu a {
1.911     bisitz   6271:   color: RGB(80, 80, 80);
                   6272:   text-decoration: none;
1.693     droeschl 6273: }
1.795     www      6274: 
1.949     droeschl 6275: ol.LC_primary_menu a.LC_new_message {
                   6276:   font-weight:bold;
                   6277:   color: darkred;
                   6278: }
                   6279: 
1.975     raeburn  6280: ol.LC_docs_parameters {
                   6281:   margin-left: 0;
                   6282:   padding: 0;
                   6283:   list-style: none;
                   6284: }
                   6285: 
                   6286: ol.LC_docs_parameters li {
                   6287:   margin: 0;
                   6288:   padding-right: 20px;
                   6289:   display: inline;
                   6290: }
                   6291: 
1.976     raeburn  6292: ol.LC_docs_parameters li:before {
                   6293:   content: "\\002022 \\0020";
                   6294: }
                   6295: 
                   6296: li.LC_docs_parameters_title {
                   6297:   font-weight: bold;
                   6298: }
                   6299: 
                   6300: ol.LC_docs_parameters li.LC_docs_parameters_title:before {
                   6301:   content: "";
                   6302: }
                   6303: 
1.897     wenzelju 6304: ul#LC_secondary_menu {
1.911     bisitz   6305:   clear: both;
                   6306:   color: $fontmenu;
                   6307:   background: $tabbg;
                   6308:   list-style: none;
                   6309:   padding: 0;
                   6310:   margin: 0;
                   6311:   width: 100%;
1.995     raeburn  6312:   text-align: left;
1.808     droeschl 6313: }
                   6314: 
1.897     wenzelju 6315: ul#LC_secondary_menu li {
1.911     bisitz   6316:   font-weight: bold;
                   6317:   line-height: 1.8em;
                   6318:   padding: 0 0.8em;
                   6319:   border-right: 1px solid black;
                   6320:   display: inline;
                   6321:   vertical-align: middle;
1.807     droeschl 6322: }
                   6323: 
1.847     tempelho 6324: ul.LC_TabContent {
1.911     bisitz   6325:   display:block;
                   6326:   background: $sidebg;
                   6327:   border-bottom: solid 1px $lg_border_color;
                   6328:   list-style:none;
1.1020    raeburn  6329:   margin: -1px -10px 0 -10px;
1.911     bisitz   6330:   padding: 0;
1.693     droeschl 6331: }
                   6332: 
1.795     www      6333: ul.LC_TabContent li,
                   6334: ul.LC_TabContentBigger li {
1.911     bisitz   6335:   float:left;
1.741     harmsja  6336: }
1.795     www      6337: 
1.897     wenzelju 6338: ul#LC_secondary_menu li a {
1.911     bisitz   6339:   color: $fontmenu;
                   6340:   text-decoration: none;
1.693     droeschl 6341: }
1.795     www      6342: 
1.721     harmsja  6343: ul.LC_TabContent {
1.952     onken    6344:   min-height:20px;
1.721     harmsja  6345: }
1.795     www      6346: 
                   6347: ul.LC_TabContent li {
1.911     bisitz   6348:   vertical-align:middle;
1.959     onken    6349:   padding: 0 16px 0 10px;
1.911     bisitz   6350:   background-color:$tabbg;
                   6351:   border-bottom:solid 1px $lg_border_color;
1.1020    raeburn  6352:   border-left: solid 1px $font;
1.721     harmsja  6353: }
1.795     www      6354: 
1.847     tempelho 6355: ul.LC_TabContent .right {
1.911     bisitz   6356:   float:right;
1.847     tempelho 6357: }
                   6358: 
1.911     bisitz   6359: ul.LC_TabContent li a,
                   6360: ul.LC_TabContent li {
                   6361:   color:rgb(47,47,47);
                   6362:   text-decoration:none;
                   6363:   font-size:95%;
                   6364:   font-weight:bold;
1.952     onken    6365:   min-height:20px;
                   6366: }
                   6367: 
1.959     onken    6368: ul.LC_TabContent li a:hover,
                   6369: ul.LC_TabContent li a:focus {
1.952     onken    6370:   color: $button_hover;
1.959     onken    6371:   background:none;
                   6372:   outline:none;
1.952     onken    6373: }
                   6374: 
                   6375: ul.LC_TabContent li:hover {
                   6376:   color: $button_hover;
                   6377:   cursor:pointer;
1.721     harmsja  6378: }
1.795     www      6379: 
1.911     bisitz   6380: ul.LC_TabContent li.active {
1.952     onken    6381:   color: $font;
1.911     bisitz   6382:   background:#FFFFFF url(/adm/lonIcons/open.gif) no-repeat scroll right center;
1.952     onken    6383:   border-bottom:solid 1px #FFFFFF;
                   6384:   cursor: default;
1.744     ehlerst  6385: }
1.795     www      6386: 
1.959     onken    6387: ul.LC_TabContent li.active a {
                   6388:   color:$font;
                   6389:   background:#FFFFFF;
                   6390:   outline: none;
                   6391: }
1.1047    raeburn  6392: 
                   6393: ul.LC_TabContent li.goback {
                   6394:   float: left;
                   6395:   border-left: none;
                   6396: }
                   6397: 
1.870     tempelho 6398: #maincoursedoc {
1.911     bisitz   6399:   clear:both;
1.870     tempelho 6400: }
                   6401: 
                   6402: ul.LC_TabContentBigger {
1.911     bisitz   6403:   display:block;
                   6404:   list-style:none;
                   6405:   padding: 0;
1.870     tempelho 6406: }
                   6407: 
1.795     www      6408: ul.LC_TabContentBigger li {
1.911     bisitz   6409:   vertical-align:bottom;
                   6410:   height: 30px;
                   6411:   font-size:110%;
                   6412:   font-weight:bold;
                   6413:   color: #737373;
1.841     tempelho 6414: }
                   6415: 
1.957     onken    6416: ul.LC_TabContentBigger li.active {
                   6417:   position: relative;
                   6418:   top: 1px;
                   6419: }
                   6420: 
1.870     tempelho 6421: ul.LC_TabContentBigger li a {
1.911     bisitz   6422:   background:url('/adm/lonIcons/tabbgleft.gif') left bottom no-repeat;
                   6423:   height: 30px;
                   6424:   line-height: 30px;
                   6425:   text-align: center;
                   6426:   display: block;
                   6427:   text-decoration: none;
1.958     onken    6428:   outline: none;  
1.741     harmsja  6429: }
1.795     www      6430: 
1.870     tempelho 6431: ul.LC_TabContentBigger li.active a {
1.911     bisitz   6432:   background:url('/adm/lonIcons/tabbgleft.gif') left top no-repeat;
                   6433:   color:$font;
1.744     ehlerst  6434: }
1.795     www      6435: 
1.870     tempelho 6436: ul.LC_TabContentBigger li b {
1.911     bisitz   6437:   background: url('/adm/lonIcons/tabbgright.gif') no-repeat right bottom;
                   6438:   display: block;
                   6439:   float: left;
                   6440:   padding: 0 30px;
1.957     onken    6441:   border-bottom: 1px solid $lg_border_color;
1.870     tempelho 6442: }
                   6443: 
1.956     onken    6444: ul.LC_TabContentBigger li:hover b {
                   6445:   color:$button_hover;
                   6446: }
                   6447: 
1.870     tempelho 6448: ul.LC_TabContentBigger li.active b {
1.911     bisitz   6449:   background:url('/adm/lonIcons/tabbgright.gif') right top no-repeat;
                   6450:   color:$font;
1.957     onken    6451:   border: 0;
1.741     harmsja  6452: }
1.693     droeschl 6453: 
1.870     tempelho 6454: 
1.862     bisitz   6455: ul.LC_CourseBreadcrumbs {
                   6456:   background: $sidebg;
1.1020    raeburn  6457:   height: 2em;
1.862     bisitz   6458:   padding-left: 10px;
1.1020    raeburn  6459:   margin: 0;
1.862     bisitz   6460:   list-style-position: inside;
                   6461: }
                   6462: 
1.911     bisitz   6463: ol#LC_MenuBreadcrumbs,
1.862     bisitz   6464: ol#LC_PathBreadcrumbs {
1.911     bisitz   6465:   padding-left: 10px;
                   6466:   margin: 0;
1.933     droeschl 6467:   height: 2.5em;  /* equal to #LC_breadcrumbs line-height */
1.693     droeschl 6468: }
                   6469: 
1.911     bisitz   6470: ol#LC_MenuBreadcrumbs li,
                   6471: ol#LC_PathBreadcrumbs li,
1.862     bisitz   6472: ul.LC_CourseBreadcrumbs li {
1.911     bisitz   6473:   display: inline;
1.933     droeschl 6474:   white-space: normal;  
1.693     droeschl 6475: }
                   6476: 
1.823     bisitz   6477: ol#LC_MenuBreadcrumbs li a,
1.862     bisitz   6478: ul.LC_CourseBreadcrumbs li a {
1.911     bisitz   6479:   text-decoration: none;
                   6480:   font-size:90%;
1.693     droeschl 6481: }
1.795     www      6482: 
1.969     droeschl 6483: ol#LC_MenuBreadcrumbs h1 {
                   6484:   display: inline;
                   6485:   font-size: 90%;
                   6486:   line-height: 2.5em;
                   6487:   margin: 0;
                   6488:   padding: 0;
                   6489: }
                   6490: 
1.795     www      6491: ol#LC_PathBreadcrumbs li a {
1.911     bisitz   6492:   text-decoration:none;
                   6493:   font-size:100%;
                   6494:   font-weight:bold;
1.693     droeschl 6495: }
1.795     www      6496: 
1.840     bisitz   6497: .LC_Box {
1.911     bisitz   6498:   border: solid 1px $lg_border_color;
                   6499:   padding: 0 10px 10px 10px;
1.746     neumanie 6500: }
1.795     www      6501: 
1.1020    raeburn  6502: .LC_DocsBox {
                   6503:   border: solid 1px $lg_border_color;
                   6504:   padding: 0 0 10px 10px;
                   6505: }
                   6506: 
1.795     www      6507: .LC_AboutMe_Image {
1.911     bisitz   6508:   float:left;
                   6509:   margin-right:10px;
1.747     neumanie 6510: }
1.795     www      6511: 
                   6512: .LC_Clear_AboutMe_Image {
1.911     bisitz   6513:   clear:left;
1.747     neumanie 6514: }
1.795     www      6515: 
1.721     harmsja  6516: dl.LC_ListStyleClean dt {
1.911     bisitz   6517:   padding-right: 5px;
                   6518:   display: table-header-group;
1.693     droeschl 6519: }
                   6520: 
1.721     harmsja  6521: dl.LC_ListStyleClean dd {
1.911     bisitz   6522:   display: table-row;
1.693     droeschl 6523: }
                   6524: 
1.721     harmsja  6525: .LC_ListStyleClean,
                   6526: .LC_ListStyleSimple,
                   6527: .LC_ListStyleNormal,
1.795     www      6528: .LC_ListStyleSpecial {
1.911     bisitz   6529:   /* display:block; */
                   6530:   list-style-position: inside;
                   6531:   list-style-type: none;
                   6532:   overflow: hidden;
                   6533:   padding: 0;
1.693     droeschl 6534: }
                   6535: 
1.721     harmsja  6536: .LC_ListStyleSimple li,
                   6537: .LC_ListStyleSimple dd,
                   6538: .LC_ListStyleNormal li,
                   6539: .LC_ListStyleNormal dd,
                   6540: .LC_ListStyleSpecial li,
1.795     www      6541: .LC_ListStyleSpecial dd {
1.911     bisitz   6542:   margin: 0;
                   6543:   padding: 5px 5px 5px 10px;
                   6544:   clear: both;
1.693     droeschl 6545: }
                   6546: 
1.721     harmsja  6547: .LC_ListStyleClean li,
                   6548: .LC_ListStyleClean dd {
1.911     bisitz   6549:   padding-top: 0;
                   6550:   padding-bottom: 0;
1.693     droeschl 6551: }
                   6552: 
1.721     harmsja  6553: .LC_ListStyleSimple dd,
1.795     www      6554: .LC_ListStyleSimple li {
1.911     bisitz   6555:   border-bottom: solid 1px $lg_border_color;
1.693     droeschl 6556: }
                   6557: 
1.721     harmsja  6558: .LC_ListStyleSpecial li,
                   6559: .LC_ListStyleSpecial dd {
1.911     bisitz   6560:   list-style-type: none;
                   6561:   background-color: RGB(220, 220, 220);
                   6562:   margin-bottom: 4px;
1.693     droeschl 6563: }
                   6564: 
1.721     harmsja  6565: table.LC_SimpleTable {
1.911     bisitz   6566:   margin:5px;
                   6567:   border:solid 1px $lg_border_color;
1.795     www      6568: }
1.693     droeschl 6569: 
1.721     harmsja  6570: table.LC_SimpleTable tr {
1.911     bisitz   6571:   padding: 0;
                   6572:   border:solid 1px $lg_border_color;
1.693     droeschl 6573: }
1.795     www      6574: 
                   6575: table.LC_SimpleTable thead {
1.911     bisitz   6576:   background:rgb(220,220,220);
1.693     droeschl 6577: }
                   6578: 
1.721     harmsja  6579: div.LC_columnSection {
1.911     bisitz   6580:   display: block;
                   6581:   clear: both;
                   6582:   overflow: hidden;
                   6583:   margin: 0;
1.693     droeschl 6584: }
                   6585: 
1.721     harmsja  6586: div.LC_columnSection>* {
1.911     bisitz   6587:   float: left;
                   6588:   margin: 10px 20px 10px 0;
                   6589:   overflow:hidden;
1.693     droeschl 6590: }
1.721     harmsja  6591: 
1.795     www      6592: table em {
1.911     bisitz   6593:   font-weight: bold;
                   6594:   font-style: normal;
1.748     schulted 6595: }
1.795     www      6596: 
1.779     bisitz   6597: table.LC_tableBrowseRes,
1.795     www      6598: table.LC_tableOfContent {
1.911     bisitz   6599:   border:none;
                   6600:   border-spacing: 1px;
                   6601:   padding: 3px;
                   6602:   background-color: #FFFFFF;
                   6603:   font-size: 90%;
1.753     droeschl 6604: }
1.789     droeschl 6605: 
1.911     bisitz   6606: table.LC_tableOfContent {
                   6607:   border-collapse: collapse;
1.789     droeschl 6608: }
                   6609: 
1.771     droeschl 6610: table.LC_tableBrowseRes a,
1.768     schulted 6611: table.LC_tableOfContent a {
1.911     bisitz   6612:   background-color: transparent;
                   6613:   text-decoration: none;
1.753     droeschl 6614: }
                   6615: 
1.795     www      6616: table.LC_tableOfContent img {
1.911     bisitz   6617:   border: none;
                   6618:   height: 1.3em;
                   6619:   vertical-align: text-bottom;
                   6620:   margin-right: 0.3em;
1.753     droeschl 6621: }
1.757     schulted 6622: 
1.795     www      6623: a#LC_content_toolbar_firsthomework {
1.911     bisitz   6624:   background-image:url(/res/adm/pages/open-first-problem.gif);
1.774     ehlerst  6625: }
                   6626: 
1.795     www      6627: a#LC_content_toolbar_everything {
1.911     bisitz   6628:   background-image:url(/res/adm/pages/show-all.gif);
1.774     ehlerst  6629: }
                   6630: 
1.795     www      6631: a#LC_content_toolbar_uncompleted {
1.911     bisitz   6632:   background-image:url(/res/adm/pages/show-incomplete-problems.gif);
1.774     ehlerst  6633: }
                   6634: 
1.795     www      6635: #LC_content_toolbar_clearbubbles {
1.911     bisitz   6636:   background-image:url(/res/adm/pages/mark-discussionentries-read.gif);
1.774     ehlerst  6637: }
                   6638: 
1.795     www      6639: a#LC_content_toolbar_changefolder {
1.911     bisitz   6640:   background : url(/res/adm/pages/close-all-folders.gif) top center ;
1.757     schulted 6641: }
                   6642: 
1.795     www      6643: a#LC_content_toolbar_changefolder_toggled {
1.911     bisitz   6644:   background-image:url(/res/adm/pages/open-all-folders.gif);
1.757     schulted 6645: }
                   6646: 
1.1043    raeburn  6647: a#LC_content_toolbar_edittoplevel {
                   6648:   background-image:url(/res/adm/pages/edittoplevel.gif);
                   6649: }
                   6650: 
1.795     www      6651: ul#LC_toolbar li a:hover {
1.911     bisitz   6652:   background-position: bottom center;
1.757     schulted 6653: }
                   6654: 
1.795     www      6655: ul#LC_toolbar {
1.911     bisitz   6656:   padding: 0;
                   6657:   margin: 2px;
                   6658:   list-style:none;
                   6659:   position:relative;
                   6660:   background-color:white;
1.757     schulted 6661: }
                   6662: 
1.795     www      6663: ul#LC_toolbar li {
1.911     bisitz   6664:   border:1px solid white;
                   6665:   padding: 0;
                   6666:   margin: 0;
                   6667:   float: left;
                   6668:   display:inline;
                   6669:   vertical-align:middle;
                   6670: }
1.757     schulted 6671: 
1.783     amueller 6672: 
1.795     www      6673: a.LC_toolbarItem {
1.911     bisitz   6674:   display:block;
                   6675:   padding: 0;
                   6676:   margin: 0;
                   6677:   height: 32px;
                   6678:   width: 32px;
                   6679:   color:white;
                   6680:   border: none;
                   6681:   background-repeat:no-repeat;
                   6682:   background-color:transparent;
1.757     schulted 6683: }
                   6684: 
1.915     droeschl 6685: ul.LC_funclist {
                   6686:     margin: 0;
                   6687:     padding: 0.5em 1em 0.5em 0;
                   6688: }
                   6689: 
1.933     droeschl 6690: ul.LC_funclist > li:first-child {
                   6691:     font-weight:bold; 
                   6692:     margin-left:0.8em;
                   6693: }
                   6694: 
1.915     droeschl 6695: ul.LC_funclist + ul.LC_funclist {
                   6696:     /* 
                   6697:        left border as a seperator if we have more than
                   6698:        one list 
                   6699:     */
                   6700:     border-left: 1px solid $sidebg;
                   6701:     /* 
                   6702:        this hides the left border behind the border of the 
                   6703:        outer box if element is wrapped to the next 'line' 
                   6704:     */
                   6705:     margin-left: -1px;
                   6706: }
                   6707: 
1.843     bisitz   6708: ul.LC_funclist li {
1.915     droeschl 6709:   display: inline;
1.782     bisitz   6710:   white-space: nowrap;
1.915     droeschl 6711:   margin: 0 0 0 25px;
                   6712:   line-height: 150%;
1.782     bisitz   6713: }
                   6714: 
1.974     wenzelju 6715: .LC_hidden {
                   6716:   display: none;
                   6717: }
                   6718: 
1.1030    www      6719: .LCmodal-overlay {
                   6720: 		position:fixed;
                   6721: 		top:0;
                   6722: 		right:0;
                   6723: 		bottom:0;
                   6724: 		left:0;
                   6725: 		height:100%;
                   6726: 		width:100%;
                   6727: 		margin:0;
                   6728: 		padding:0;
                   6729: 		background:#999;
                   6730: 		opacity:.75;
                   6731: 		filter: alpha(opacity=75);
                   6732: 		-moz-opacity: 0.75;
                   6733: 		z-index:101;
                   6734: }
                   6735: 
                   6736: * html .LCmodal-overlay {   
                   6737: 		position: absolute;
                   6738: 		height: expression(document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + 'px');
                   6739: }
                   6740: 
                   6741: .LCmodal-window {
                   6742: 		position:fixed;
                   6743: 		top:50%;
                   6744: 		left:50%;
                   6745: 		margin:0;
                   6746: 		padding:0;
                   6747: 		z-index:102;
                   6748: 	}
                   6749: 
                   6750: * html .LCmodal-window {
                   6751: 		position:absolute;
                   6752: }
                   6753: 
                   6754: .LCclose-window {
                   6755: 		position:absolute;
                   6756: 		width:32px;
                   6757: 		height:32px;
                   6758: 		right:8px;
                   6759: 		top:8px;
                   6760: 		background:transparent url('/res/adm/pages/process-stop.png') no-repeat scroll right top;
                   6761: 		text-indent:-99999px;
                   6762: 		overflow:hidden;
                   6763: 		cursor:pointer;
                   6764: }
                   6765: 
1.343     albertel 6766: END
                   6767: }
                   6768: 
1.306     albertel 6769: =pod
                   6770: 
                   6771: =item * &headtag()
                   6772: 
                   6773: Returns a uniform footer for LON-CAPA web pages.
                   6774: 
1.307     albertel 6775: Inputs: $title - optional title for the head
                   6776:         $head_extra - optional extra HTML to put inside the <head>
1.315     albertel 6777:         $args - optional arguments
1.319     albertel 6778:             force_register - if is true call registerurl so the remote is 
                   6779:                              informed
1.415     albertel 6780:             redirect       -> array ref of
                   6781:                                    1- seconds before redirect occurs
                   6782:                                    2- url to redirect to
                   6783:                                    3- whether the side effect should occur
1.315     albertel 6784:                            (side effect of setting 
                   6785:                                $env{'internal.head.redirect'} to the url 
                   6786:                                redirected too)
1.352     albertel 6787:             domain         -> force to color decorate a page for a specific
                   6788:                                domain
                   6789:             function       -> force usage of a specific rolish color scheme
                   6790:             bgcolor        -> override the default page bgcolor
1.460     albertel 6791:             no_auto_mt_title
                   6792:                            -> prevent &mt()ing the title arg
1.464     albertel 6793: 
1.306     albertel 6794: =cut
                   6795: 
                   6796: sub headtag {
1.313     albertel 6797:     my ($title,$head_extra,$args) = @_;
1.306     albertel 6798:     
1.363     albertel 6799:     my $function = $args->{'function'} || &get_users_function();
                   6800:     my $domain   = $args->{'domain'}   || &determinedomain();
                   6801:     my $bgcolor  = $args->{'bgcolor'}  || &designparm($function.'.pgbg',$domain);
1.418     albertel 6802:     my $url = join(':',$env{'user.name'},$env{'user.domain'},
1.458     albertel 6803: 		   $Apache::lonnet::perlvar{'lonVersion'},
1.531     albertel 6804: 		   #time(),
1.418     albertel 6805: 		   $env{'environment.color.timestamp'},
1.363     albertel 6806: 		   $function,$domain,$bgcolor);
                   6807: 
1.369     www      6808:     $url = '/adm/css/'.&escape($url).'.css';
1.363     albertel 6809: 
1.308     albertel 6810:     my $result =
                   6811: 	'<head>'.
1.461     albertel 6812: 	&font_settings();
1.319     albertel 6813: 
1.461     albertel 6814:     if (!$args->{'frameset'}) {
                   6815: 	$result .= &Apache::lonhtmlcommon::htmlareaheaders();
                   6816:     }
1.962     droeschl 6817:     if ($args->{'force_register'} && $env{'request.noversionuri'} !~ m{^/res/adm/pages/}) {
                   6818:         $result .= Apache::lonxml::display_title();
1.319     albertel 6819:     }
1.436     albertel 6820:     if (!$args->{'no_nav_bar'} 
                   6821: 	&& !$args->{'only_body'}
                   6822: 	&& !$args->{'frameset'}) {
                   6823: 	$result .= &help_menu_js();
1.1032    www      6824:         $result.=&modal_window();
1.1038    www      6825:         $result.=&togglebox_script();
1.1034    www      6826:         $result.=&wishlist_window();
1.1041    www      6827:         $result.=&LCprogressbarUpdate_script();
1.1034    www      6828:     } else {
                   6829:         if ($args->{'add_modal'}) {
                   6830:            $result.=&modal_window();
                   6831:         }
                   6832:         if ($args->{'add_wishlist'}) {
                   6833:            $result.=&wishlist_window();
                   6834:         }
1.1038    www      6835:         if ($args->{'add_togglebox'}) {
                   6836:            $result.=&togglebox_script();
                   6837:         }
1.1041    www      6838:         if ($args->{'add_progressbar'}) {
                   6839:            $result.=&LCprogressbarUpdate_script();
                   6840:         }
1.436     albertel 6841:     }
1.314     albertel 6842:     if (ref($args->{'redirect'})) {
1.414     albertel 6843: 	my ($time,$url,$inhibit_continue) = @{$args->{'redirect'}};
1.315     albertel 6844: 	$url = &Apache::lonenc::check_encrypt($url);
1.414     albertel 6845: 	if (!$inhibit_continue) {
                   6846: 	    $env{'internal.head.redirect'} = $url;
                   6847: 	}
1.313     albertel 6848: 	$result.=<<ADDMETA
                   6849: <meta http-equiv="pragma" content="no-cache" />
1.344     albertel 6850: <meta http-equiv="Refresh" content="$time; url=$url" />
1.313     albertel 6851: ADDMETA
                   6852:     }
1.306     albertel 6853:     if (!defined($title)) {
                   6854: 	$title = 'The LearningOnline Network with CAPA';
                   6855:     }
1.460     albertel 6856:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
                   6857:     $result .= '<title> LON-CAPA '.$title.'</title>'
1.414     albertel 6858: 	.'<link rel="stylesheet" type="text/css" href="'.$url.'" />'
                   6859: 	.$head_extra;
1.962     droeschl 6860:     return $result.'</head>';
1.306     albertel 6861: }
                   6862: 
                   6863: =pod
                   6864: 
1.340     albertel 6865: =item * &font_settings()
                   6866: 
                   6867: Returns neccessary <meta> to set the proper encoding
                   6868: 
                   6869: Inputs: none
                   6870: 
                   6871: =cut
                   6872: 
                   6873: sub font_settings {
                   6874:     my $headerstring='';
1.647     www      6875:     if (!$env{'browser.mathml'} && $env{'browser.unicode'}) {
1.340     albertel 6876: 	$headerstring.=
                   6877: 	    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
                   6878:     }
                   6879:     return $headerstring;
                   6880: }
                   6881: 
1.341     albertel 6882: =pod
                   6883: 
                   6884: =item * &xml_begin()
                   6885: 
                   6886: Returns the needed doctype and <html>
                   6887: 
                   6888: Inputs: none
                   6889: 
                   6890: =cut
                   6891: 
                   6892: sub xml_begin {
                   6893:     my $output='';
                   6894: 
                   6895:     if ($env{'browser.mathml'}) {
                   6896: 	$output='<?xml version="1.0"?>'
                   6897:             #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
                   6898: #            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                   6899:             
                   6900: #	    .'<!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">] >'
                   6901: 	    .'<!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">'
                   6902:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                   6903: 	    .'xmlns="http://www.w3.org/1999/xhtml">';
                   6904:     } else {
1.849     bisitz   6905: 	$output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
                   6906:            .'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
1.341     albertel 6907:     }
                   6908:     return $output;
                   6909: }
1.340     albertel 6910: 
                   6911: =pod
                   6912: 
1.306     albertel 6913: =item * &start_page()
                   6914: 
                   6915: Returns a complete <html> .. <body> section for LON-CAPA web pages.
                   6916: 
1.648     raeburn  6917: Inputs:
                   6918: 
                   6919: =over 4
                   6920: 
                   6921: $title - optional title for the page
                   6922: 
                   6923: $head_extra - optional extra HTML to incude inside the <head>
                   6924: 
                   6925: $args - additional optional args supported are:
                   6926: 
                   6927: =over 8
                   6928: 
                   6929:              only_body      -> is true will set &bodytag() onlybodytag
1.317     albertel 6930:                                     arg on
1.814     bisitz   6931:              no_nav_bar     -> is true will set &bodytag() no_nav_bar arg on
1.648     raeburn  6932:              add_entries    -> additional attributes to add to the  <body>
                   6933:              domain         -> force to color decorate a page for a 
1.317     albertel 6934:                                     specific domain
1.648     raeburn  6935:              function       -> force usage of a specific rolish color
1.317     albertel 6936:                                     scheme
1.648     raeburn  6937:              redirect       -> see &headtag()
                   6938:              bgcolor        -> override the default page bg color
                   6939:              js_ready       -> return a string ready for being used in 
1.317     albertel 6940:                                     a javascript writeln
1.648     raeburn  6941:              html_encode    -> return a string ready for being used in 
1.320     albertel 6942:                                     a html attribute
1.648     raeburn  6943:              force_register -> if is true will turn on the &bodytag()
1.317     albertel 6944:                                     $forcereg arg
1.648     raeburn  6945:              frameset       -> if true will start with a <frameset>
1.330     albertel 6946:                                     rather than <body>
1.648     raeburn  6947:              skip_phases    -> hash ref of 
1.338     albertel 6948:                                     head -> skip the <html><head> generation
                   6949:                                     body -> skip all <body> generation
1.648     raeburn  6950:              no_auto_mt_title -> prevent &mt()ing the title arg
                   6951:              inherit_jsmath -> when creating popup window in a page,
                   6952:                                     should it have jsmath forced on by the
                   6953:                                     current page
1.867     kalberla 6954:              bread_crumbs ->             Array containing breadcrumbs
1.983     raeburn  6955:              bread_crumbs_component ->  if exists show it as headline else show only the breadcrumbs
1.361     albertel 6956: 
1.648     raeburn  6957: =back
1.460     albertel 6958: 
1.648     raeburn  6959: =back
1.562     albertel 6960: 
1.306     albertel 6961: =cut
                   6962: 
                   6963: sub start_page {
1.309     albertel 6964:     my ($title,$head_extra,$args) = @_;
1.318     albertel 6965:     #&Apache::lonnet::logthis("start_page ".join(':',caller(0)));
1.319     albertel 6966: 
1.315     albertel 6967:     $env{'internal.start_page'}++;
1.338     albertel 6968:     my $result;
1.964     droeschl 6969: 
1.338     albertel 6970:     if (! exists($args->{'skip_phases'}{'head'}) ) {
1.1030    www      6971:         $result .= &xml_begin() . &headtag($title, $head_extra, $args);
1.338     albertel 6972:     }
                   6973:     
                   6974:     if (! exists($args->{'skip_phases'}{'body'}) ) {
                   6975: 	if ($args->{'frameset'}) {
                   6976: 	    my $attr_string = &make_attr_string($args->{'force_register'},
                   6977: 						$args->{'add_entries'});
                   6978: 	    $result .= "\n<frameset $attr_string>\n";
1.831     bisitz   6979:         } else {
                   6980:             $result .=
                   6981:                 &bodytag($title, 
                   6982:                          $args->{'function'},       $args->{'add_entries'},
                   6983:                          $args->{'only_body'},      $args->{'domain'},
                   6984:                          $args->{'force_register'}, $args->{'no_nav_bar'},
1.962     droeschl 6985:                          $args->{'bgcolor'},        $args);
1.831     bisitz   6986:         }
1.330     albertel 6987:     }
1.338     albertel 6988: 
1.315     albertel 6989:     if ($args->{'js_ready'}) {
1.713     kaisler  6990: 		$result = &js_ready($result);
1.315     albertel 6991:     }
1.320     albertel 6992:     if ($args->{'html_encode'}) {
1.713     kaisler  6993: 		$result = &html_encode($result);
                   6994:     }
                   6995: 
1.813     bisitz   6996:     # Preparation for new and consistent functionlist at top of screen
                   6997:     # if ($args->{'functionlist'}) {
                   6998:     #            $result .= &build_functionlist();
                   6999:     #}
                   7000: 
1.964     droeschl 7001:     # Don't add anything more if only_body wanted or in const space
                   7002:     return $result if    $args->{'only_body'} 
                   7003:                       || $env{'request.state'} eq 'construct';
1.813     bisitz   7004: 
                   7005:     #Breadcrumbs
1.758     kaisler  7006:     if (exists($args->{'bread_crumbs'}) or exists($args->{'bread_crumbs_component'})) {
                   7007: 		&Apache::lonhtmlcommon::clear_breadcrumbs();
                   7008: 		#if any br links exists, add them to the breadcrumbs
                   7009: 		if (exists($args->{'bread_crumbs'}) and ref($args->{'bread_crumbs'}) eq 'ARRAY') {         
                   7010: 			foreach my $crumb (@{$args->{'bread_crumbs'}}){
                   7011: 				&Apache::lonhtmlcommon::add_breadcrumb($crumb);
                   7012: 			}
                   7013: 		}
                   7014: 
                   7015: 		#if bread_crumbs_component exists show it as headline else show only the breadcrumbs
                   7016: 		if(exists($args->{'bread_crumbs_component'})){
                   7017: 			$result .= &Apache::lonhtmlcommon::breadcrumbs($args->{'bread_crumbs_component'});
                   7018: 		}else{
                   7019: 			$result .= &Apache::lonhtmlcommon::breadcrumbs();
                   7020: 		}
1.320     albertel 7021:     }
1.315     albertel 7022:     return $result;
1.306     albertel 7023: }
                   7024: 
                   7025: sub end_page {
1.315     albertel 7026:     my ($args) = @_;
                   7027:     $env{'internal.end_page'}++;
1.330     albertel 7028:     my $result;
1.335     albertel 7029:     if ($args->{'discussion'}) {
                   7030: 	my ($target,$parser);
                   7031: 	if (ref($args->{'discussion'})) {
                   7032: 	    ($target,$parser) =($args->{'discussion'}{'target'},
                   7033: 				$args->{'discussion'}{'parser'});
                   7034: 	}
                   7035: 	$result .= &Apache::lonxml::xmlend($target,$parser);
                   7036:     }
1.330     albertel 7037:     if ($args->{'frameset'}) {
                   7038: 	$result .= '</frameset>';
                   7039:     } else {
1.635     raeburn  7040: 	$result .= &endbodytag($args);
1.330     albertel 7041:     }
                   7042:     $result .= "\n</html>";
                   7043: 
1.315     albertel 7044:     if ($args->{'js_ready'}) {
1.317     albertel 7045: 	$result = &js_ready($result);
1.315     albertel 7046:     }
1.335     albertel 7047: 
1.320     albertel 7048:     if ($args->{'html_encode'}) {
                   7049: 	$result = &html_encode($result);
                   7050:     }
1.335     albertel 7051: 
1.315     albertel 7052:     return $result;
                   7053: }
                   7054: 
1.1034    www      7055: sub wishlist_window {
                   7056:     return(<<'ENDWISHLIST');
1.1046    raeburn  7057: <script type="text/javascript">
1.1034    www      7058: // <![CDATA[
                   7059: // <!-- BEGIN LON-CAPA Internal
                   7060: function set_wishlistlink(title, path) {
                   7061:     if (!title) {
                   7062:         title = document.title;
                   7063:         title = title.replace(/^LON-CAPA /,'');
                   7064:     }
                   7065:     if (!path) {
                   7066:         path = location.pathname;
                   7067:     }
                   7068:     Win = window.open('/adm/wishlist?mode=newLink&setTitle='+title+'&setPath='+path,
                   7069:                       'wishlistNewLink','width=560,height=350,scrollbars=0');
                   7070: }
                   7071: // END LON-CAPA Internal -->
                   7072: // ]]>
                   7073: </script>
                   7074: ENDWISHLIST
                   7075: }
                   7076: 
1.1030    www      7077: sub modal_window {
                   7078:     return(<<'ENDMODAL');
1.1046    raeburn  7079: <script type="text/javascript">
1.1030    www      7080: // <![CDATA[
                   7081: // <!-- BEGIN LON-CAPA Internal
                   7082: var modalWindow = {
                   7083: 	parent:"body",
                   7084: 	windowId:null,
                   7085: 	content:null,
                   7086: 	width:null,
                   7087: 	height:null,
                   7088: 	close:function()
                   7089: 	{
                   7090: 	        $(".LCmodal-window").remove();
                   7091: 	        $(".LCmodal-overlay").remove();
                   7092: 	},
                   7093: 	open:function()
                   7094: 	{
                   7095: 		var modal = "";
                   7096: 		modal += "<div class=\"LCmodal-overlay\"></div>";
                   7097: 		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;\">";
                   7098: 		modal += this.content;
                   7099: 		modal += "</div>";	
                   7100: 
                   7101: 		$(this.parent).append(modal);
                   7102: 
                   7103: 		$(".LCmodal-window").append("<a class=\"LCclose-window\"></a>");
                   7104: 		$(".LCclose-window").click(function(){modalWindow.close();});
                   7105: 		$(".LCmodal-overlay").click(function(){modalWindow.close();});
                   7106: 	}
                   7107: };
1.1031    www      7108: 	var openMyModal = function(source,width,height,scrolling)
1.1030    www      7109: 	{
                   7110: 		modalWindow.windowId = "myModal";
                   7111: 		modalWindow.width = width;
                   7112: 		modalWindow.height = height;
1.1031    www      7113: 		modalWindow.content = "<iframe width='"+width+"' height='"+height+"' frameborder='0' scrolling='"+scrolling+"' allowtransparency='true' src='" + source + "'>&lt/iframe>";
1.1030    www      7114: 		modalWindow.open();
                   7115: 	};	
                   7116: // END LON-CAPA Internal -->
                   7117: // ]]>
                   7118: </script>
                   7119: ENDMODAL
                   7120: }
                   7121: 
                   7122: sub modal_link {
1.1052    www      7123:     my ($link,$linktext,$width,$height,$target,$scrolling,$title)=@_;
1.1030    www      7124:     unless ($width) { $width=480; }
                   7125:     unless ($height) { $height=400; }
1.1031    www      7126:     unless ($scrolling) { $scrolling='yes'; }
1.1052    www      7127:     return '<a href="'.$link.'" target="'.$target.'" title="'.$title.'" onclick="openMyModal(\''.$link.'\','.$width.','.$height.',\''.$scrolling.'\'); return false;">'.
1.1031    www      7128:            $linktext.'</a>';
1.1030    www      7129: }
                   7130: 
1.1032    www      7131: sub modal_adhoc_script {
                   7132:     my ($funcname,$width,$height,$content)=@_;
                   7133:     return (<<ENDADHOC);
1.1046    raeburn  7134: <script type="text/javascript">
1.1032    www      7135: // <![CDATA[
                   7136:         var $funcname = function()
                   7137:         {
                   7138:                 modalWindow.windowId = "myModal";
                   7139:                 modalWindow.width = $width;
                   7140:                 modalWindow.height = $height;
                   7141:                 modalWindow.content = '$content';
                   7142:                 modalWindow.open();
                   7143:         };  
                   7144: // ]]>
                   7145: </script>
                   7146: ENDADHOC
                   7147: }
                   7148: 
1.1041    www      7149: sub modal_adhoc_inner {
                   7150:     my ($funcname,$width,$height,$content)=@_;
                   7151:     my $innerwidth=$width-20;
                   7152:     $content=&js_ready(
1.1042    www      7153:                &start_page('Dialog',undef,{'only_body'=>1,'bgcolor'=>'#FFFFFF'}).
1.1041    www      7154:                  &start_scrollbox($width.'px',$innerwidth.'px',$height.'px').
                   7155:                     $content.
                   7156:                  &end_scrollbox().
                   7157:                &end_page()
                   7158:              );
                   7159:     return &modal_adhoc_script($funcname,$width,$height,$content);
                   7160: }
                   7161: 
                   7162: sub modal_adhoc_window {
                   7163:     my ($funcname,$width,$height,$content,$linktext)=@_;
                   7164:     return &modal_adhoc_inner($funcname,$width,$height,$content).
                   7165:            "<a href=\"javascript:$funcname();void(0);\">".$linktext."</a>";
                   7166: }
                   7167: 
                   7168: sub modal_adhoc_launch {
                   7169:     my ($funcname,$width,$height,$content)=@_;
                   7170:     return &modal_adhoc_inner($funcname,$width,$height,$content).(<<ENDLAUNCH);
                   7171: <script type="text/javascript">
                   7172: // <![CDATA[
                   7173: $funcname();
                   7174: // ]]>
                   7175: </script>
                   7176: ENDLAUNCH
                   7177: }
                   7178: 
                   7179: sub modal_adhoc_close {
                   7180:     return (<<ENDCLOSE);
                   7181: <script type="text/javascript">
                   7182: // <![CDATA[
                   7183: modalWindow.close();
                   7184: // ]]>
                   7185: </script>
                   7186: ENDCLOSE
                   7187: }
                   7188: 
1.1038    www      7189: sub togglebox_script {
                   7190:    return(<<ENDTOGGLE);
                   7191: <script type="text/javascript"> 
                   7192: // <![CDATA[
                   7193: function LCtoggleDisplay(id,hidetext,showtext) {
                   7194:    link = document.getElementById(id + "link").childNodes[0];
                   7195:    with (document.getElementById(id).style) {
                   7196:       if (display == "none" ) {
                   7197:           display = "inline";
                   7198:           link.nodeValue = hidetext;
                   7199:         } else {
                   7200:           display = "none";
                   7201:           link.nodeValue = showtext;
                   7202:        }
                   7203:    }
                   7204: }
                   7205: // ]]>
                   7206: </script>
                   7207: ENDTOGGLE
                   7208: }
                   7209: 
1.1039    www      7210: sub start_togglebox {
                   7211:     my ($id,$heading,$headerbg,$hidetext,$showtext)=@_;
                   7212:     unless ($heading) { $heading=''; } else { $heading.=' '; }
                   7213:     unless ($showtext) { $showtext=&mt('show'); }
                   7214:     unless ($hidetext) { $hidetext=&mt('hide'); }
                   7215:     unless ($headerbg) { $headerbg='#FFFFFF'; }
                   7216:     return &start_data_table().
                   7217:            &start_data_table_header_row().
                   7218:            '<td bgcolor="'.$headerbg.'">'.$heading.
                   7219:            '[<a id="'.$id.'link" href="javascript:LCtoggleDisplay(\''.$id.'\',\''.$hidetext.'\',\''.
                   7220:            $showtext.'\')">'.$showtext.'</a>]</td>'.
                   7221:            &end_data_table_header_row().
                   7222:            '<tr id="'.$id.'" style="display:none""><td>';
                   7223: }
                   7224: 
                   7225: sub end_togglebox {
                   7226:     return '</td></tr>'.&end_data_table();
                   7227: }
                   7228: 
1.1041    www      7229: sub LCprogressbar_script {
1.1045    www      7230:    my ($id)=@_;
1.1041    www      7231:    return(<<ENDPROGRESS);
                   7232: <script type="text/javascript">
                   7233: // <![CDATA[
1.1045    www      7234: \$('#progressbar$id').progressbar({
1.1041    www      7235:   value: 0,
                   7236:   change: function(event, ui) {
                   7237:     var newVal = \$(this).progressbar('option', 'value');
                   7238:     \$('.pblabel', this).text(LCprogressTxt);
                   7239:   }
                   7240: });
                   7241: // ]]>
                   7242: </script>
                   7243: ENDPROGRESS
                   7244: }
                   7245: 
                   7246: sub LCprogressbarUpdate_script {
                   7247:    return(<<ENDPROGRESSUPDATE);
                   7248: <style type="text/css">
                   7249: .ui-progressbar { position:relative; }
                   7250: .pblabel { position: absolute; width: 100%; text-align: center; line-height: 1.9em; }
                   7251: </style>
                   7252: <script type="text/javascript">
                   7253: // <![CDATA[
1.1045    www      7254: var LCprogressTxt='---';
                   7255: 
                   7256: function LCupdateProgress(percent,progresstext,id) {
1.1041    www      7257:    LCprogressTxt=progresstext;
1.1045    www      7258:    \$('#progressbar'+id).progressbar('value',percent);
1.1041    www      7259: }
                   7260: // ]]>
                   7261: </script>
                   7262: ENDPROGRESSUPDATE
                   7263: }
                   7264: 
1.1042    www      7265: my $LClastpercent;
1.1045    www      7266: my $LCidcnt;
                   7267: my $LCcurrentid;
1.1042    www      7268: 
1.1041    www      7269: sub LCprogressbar {
1.1042    www      7270:     my ($r)=(@_);
                   7271:     $LClastpercent=0;
1.1045    www      7272:     $LCidcnt++;
                   7273:     $LCcurrentid=$$.'_'.$LCidcnt;
1.1041    www      7274:     my $starting=&mt('Starting');
                   7275:     my $content=(<<ENDPROGBAR);
                   7276: <p>
1.1045    www      7277:   <div id="progressbar$LCcurrentid">
1.1041    www      7278:     <span class="pblabel">$starting</span>
                   7279:   </div>
                   7280: </p>
                   7281: ENDPROGBAR
1.1045    www      7282:     &r_print($r,$content.&LCprogressbar_script($LCcurrentid));
1.1041    www      7283: }
                   7284: 
                   7285: sub LCprogressbarUpdate {
1.1042    www      7286:     my ($r,$val,$text)=@_;
                   7287:     unless ($val) { 
                   7288:        if ($LClastpercent) {
                   7289:            $val=$LClastpercent;
                   7290:        } else {
                   7291:            $val=0;
                   7292:        }
                   7293:     }
1.1041    www      7294:     if ($val<0) { $val=0; }
                   7295:     if ($val>100) { $val=0; }
1.1042    www      7296:     $LClastpercent=$val;
1.1041    www      7297:     unless ($text) { $text=$val.'%'; }
                   7298:     $text=&js_ready($text);
1.1044    www      7299:     &r_print($r,<<ENDUPDATE);
1.1041    www      7300: <script type="text/javascript">
                   7301: // <![CDATA[
1.1045    www      7302: LCupdateProgress($val,'$text','$LCcurrentid');
1.1041    www      7303: // ]]>
                   7304: </script>
                   7305: ENDUPDATE
1.1035    www      7306: }
                   7307: 
1.1042    www      7308: sub LCprogressbarClose {
                   7309:     my ($r)=@_;
                   7310:     $LClastpercent=0;
1.1044    www      7311:     &r_print($r,<<ENDCLOSE);
1.1042    www      7312: <script type="text/javascript">
                   7313: // <![CDATA[
1.1045    www      7314: \$("#progressbar$LCcurrentid").hide('slow'); 
1.1042    www      7315: // ]]>
                   7316: </script>
                   7317: ENDCLOSE
1.1044    www      7318: }
                   7319: 
                   7320: sub r_print {
                   7321:     my ($r,$to_print)=@_;
                   7322:     if ($r) {
                   7323:       $r->print($to_print);
                   7324:       $r->rflush();
                   7325:     } else {
                   7326:       print($to_print);
                   7327:     }
1.1042    www      7328: }
                   7329: 
1.320     albertel 7330: sub html_encode {
                   7331:     my ($result) = @_;
                   7332: 
1.322     albertel 7333:     $result = &HTML::Entities::encode($result,'<>&"');
1.320     albertel 7334:     
                   7335:     return $result;
                   7336: }
1.1044    www      7337: 
1.317     albertel 7338: sub js_ready {
                   7339:     my ($result) = @_;
                   7340: 
1.323     albertel 7341:     $result =~ s/[\n\r]/ /xmsg;
                   7342:     $result =~ s/\\/\\\\/xmsg;
                   7343:     $result =~ s/'/\\'/xmsg;
1.372     albertel 7344:     $result =~ s{</}{<\\/}xmsg;
1.317     albertel 7345:     
                   7346:     return $result;
                   7347: }
                   7348: 
1.315     albertel 7349: sub validate_page {
                   7350:     if (  exists($env{'internal.start_page'})
1.316     albertel 7351: 	  &&     $env{'internal.start_page'} > 1) {
                   7352: 	&Apache::lonnet::logthis('start_page called multiple times '.
1.318     albertel 7353: 				 $env{'internal.start_page'}.' '.
1.316     albertel 7354: 				 $ENV{'request.filename'});
1.315     albertel 7355:     }
                   7356:     if (  exists($env{'internal.end_page'})
1.316     albertel 7357: 	  &&     $env{'internal.end_page'} > 1) {
                   7358: 	&Apache::lonnet::logthis('end_page called multiple times '.
1.318     albertel 7359: 				 $env{'internal.end_page'}.' '.
1.316     albertel 7360: 				 $env{'request.filename'});
1.315     albertel 7361:     }
                   7362:     if (     exists($env{'internal.start_page'})
                   7363: 	&& ! exists($env{'internal.end_page'})) {
1.316     albertel 7364: 	&Apache::lonnet::logthis('start_page called without end_page '.
                   7365: 				 $env{'request.filename'});
1.315     albertel 7366:     }
                   7367:     if (   ! exists($env{'internal.start_page'})
                   7368: 	&&   exists($env{'internal.end_page'})) {
1.316     albertel 7369: 	&Apache::lonnet::logthis('end_page called without start_page'.
                   7370: 				 $env{'request.filename'});
1.315     albertel 7371:     }
1.306     albertel 7372: }
1.315     albertel 7373: 
1.996     www      7374: 
                   7375: sub start_scrollbox {
1.1018    raeburn  7376:     my ($outerwidth,$width,$height,$id)=@_;
1.998     raeburn  7377:     unless ($outerwidth) { $outerwidth='520px'; }
                   7378:     unless ($width) { $width='500px'; }
                   7379:     unless ($height) { $height='200px'; }
1.1020    raeburn  7380:     my ($table_id,$div_id);
1.1018    raeburn  7381:     if ($id ne '') {
1.1020    raeburn  7382:         $table_id = " id='table_$id'";
                   7383:         $div_id = " id='div_$id'";
1.1018    raeburn  7384:     }
1.1020    raeburn  7385:     return "<table style='width: $outerwidth; border: 1px solid none;'$table_id><tr><td style='width: $width;' bgcolor='#FFFFFF'><div style='overflow:auto; width:$width; height: $height;'$div_id>";
1.996     www      7386: }
                   7387: 
                   7388: sub end_scrollbox {
1.1036    www      7389:     return '</div></td></tr></table>';
1.996     www      7390: }
                   7391: 
1.318     albertel 7392: sub simple_error_page {
                   7393:     my ($r,$title,$msg) = @_;
                   7394:     my $page =
                   7395: 	&Apache::loncommon::start_page($title).
                   7396: 	&mt($msg).
                   7397: 	&Apache::loncommon::end_page();
                   7398:     if (ref($r)) {
                   7399: 	$r->print($page);
1.327     albertel 7400: 	return;
1.318     albertel 7401:     }
                   7402:     return $page;
                   7403: }
1.347     albertel 7404: 
                   7405: {
1.610     albertel 7406:     my @row_count;
1.961     onken    7407: 
                   7408:     sub start_data_table_count {
                   7409:         unshift(@row_count, 0);
                   7410:         return;
                   7411:     }
                   7412: 
                   7413:     sub end_data_table_count {
                   7414:         shift(@row_count);
                   7415:         return;
                   7416:     }
                   7417: 
1.347     albertel 7418:     sub start_data_table {
1.1018    raeburn  7419: 	my ($add_class,$id) = @_;
1.422     albertel 7420: 	my $css_class = (join(' ','LC_data_table',$add_class));
1.1018    raeburn  7421:         my $table_id;
                   7422:         if (defined($id)) {
                   7423:             $table_id = ' id="'.$id.'"';
                   7424:         }
1.961     onken    7425: 	&start_data_table_count();
1.1018    raeburn  7426: 	return '<table class="'.$css_class.'"'.$table_id.'>'."\n";
1.347     albertel 7427:     }
                   7428: 
                   7429:     sub end_data_table {
1.961     onken    7430: 	&end_data_table_count();
1.389     albertel 7431: 	return '</table>'."\n";;
1.347     albertel 7432:     }
                   7433: 
                   7434:     sub start_data_table_row {
1.974     wenzelju 7435: 	my ($add_class, $id) = @_;
1.610     albertel 7436: 	$row_count[0]++;
                   7437: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.900     bisitz   7438: 	$css_class = (join(' ',$css_class,$add_class)) unless ($add_class eq '');
1.974     wenzelju 7439:         $id = (' id="'.$id.'"') unless ($id eq '');
                   7440:         return  '<tr class="'.$css_class.'"'.$id.'>'."\n";
1.347     albertel 7441:     }
1.471     banghart 7442:     
                   7443:     sub continue_data_table_row {
1.974     wenzelju 7444: 	my ($add_class, $id) = @_;
1.610     albertel 7445: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.974     wenzelju 7446: 	$css_class = (join(' ',$css_class,$add_class)) unless ($add_class eq '');
                   7447:         $id = (' id="'.$id.'"') unless ($id eq '');
                   7448:         return  '<tr class="'.$css_class.'"'.$id.'>'."\n";
1.471     banghart 7449:     }
1.347     albertel 7450: 
                   7451:     sub end_data_table_row {
1.389     albertel 7452: 	return '</tr>'."\n";;
1.347     albertel 7453:     }
1.367     www      7454: 
1.421     albertel 7455:     sub start_data_table_empty_row {
1.707     bisitz   7456: #	$row_count[0]++;
1.421     albertel 7457: 	return  '<tr class="LC_empty_row" >'."\n";;
                   7458:     }
                   7459: 
                   7460:     sub end_data_table_empty_row {
                   7461: 	return '</tr>'."\n";;
                   7462:     }
                   7463: 
1.367     www      7464:     sub start_data_table_header_row {
1.389     albertel 7465: 	return  '<tr class="LC_header_row">'."\n";;
1.367     www      7466:     }
                   7467: 
                   7468:     sub end_data_table_header_row {
1.389     albertel 7469: 	return '</tr>'."\n";;
1.367     www      7470:     }
1.890     droeschl 7471: 
                   7472:     sub data_table_caption {
                   7473:         my $caption = shift;
                   7474:         return "<caption class=\"LC_caption\">$caption</caption>";
                   7475:     }
1.347     albertel 7476: }
                   7477: 
1.548     albertel 7478: =pod
                   7479: 
                   7480: =item * &inhibit_menu_check($arg)
                   7481: 
                   7482: Checks for a inhibitmenu state and generates output to preserve it
                   7483: 
                   7484: Inputs:         $arg - can be any of
                   7485:                      - undef - in which case the return value is a string 
                   7486:                                to add  into arguments list of a uri
                   7487:                      - 'input' - in which case the return value is a HTML
                   7488:                                  <form> <input> field of type hidden to
                   7489:                                  preserve the value
                   7490:                      - a url - in which case the return value is the url with
                   7491:                                the neccesary cgi args added to preserve the
                   7492:                                inhibitmenu state
                   7493:                      - a ref to a url - no return value, but the string is
                   7494:                                         updated to include the neccessary cgi
                   7495:                                         args to preserve the inhibitmenu state
                   7496: 
                   7497: =cut
                   7498: 
                   7499: sub inhibit_menu_check {
                   7500:     my ($arg) = @_;
                   7501:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
                   7502:     if ($arg eq 'input') {
                   7503: 	if ($env{'form.inhibitmenu'}) {
                   7504: 	    return '<input type="hidden" name="inhibitmenu" value="'.$env{'form.inhibitmenu'}.'" />';
                   7505: 	} else {
                   7506: 	    return
                   7507: 	}
                   7508:     }
                   7509:     if ($env{'form.inhibitmenu'}) {
                   7510: 	if (ref($arg)) {
                   7511: 	    $$arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   7512: 	} elsif ($arg eq '') {
                   7513: 	    $arg .= 'inhibitmenu='.$env{'form.inhibitmenu'};
                   7514: 	} else {
                   7515: 	    $arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   7516: 	}
                   7517:     }
                   7518:     if (!ref($arg)) {
                   7519: 	return $arg;
                   7520:     }
                   7521: }
                   7522: 
1.251     albertel 7523: ###############################################
1.182     matthew  7524: 
                   7525: =pod
                   7526: 
1.549     albertel 7527: =back
                   7528: 
                   7529: =head1 User Information Routines
                   7530: 
                   7531: =over 4
                   7532: 
1.405     albertel 7533: =item * &get_users_function()
1.182     matthew  7534: 
                   7535: Used by &bodytag to determine the current users primary role.
                   7536: Returns either 'student','coordinator','admin', or 'author'.
                   7537: 
                   7538: =cut
                   7539: 
                   7540: ###############################################
                   7541: sub get_users_function {
1.815     tempelho 7542:     my $function = 'norole';
1.818     tempelho 7543:     if ($env{'request.role'}=~/^(st)/) {
                   7544:         $function='student';
                   7545:     }
1.907     raeburn  7546:     if ($env{'request.role'}=~/^(cc|co|in|ta|ep)/) {
1.182     matthew  7547:         $function='coordinator';
                   7548:     }
1.258     albertel 7549:     if ($env{'request.role'}=~/^(su|dc|ad|li)/) {
1.182     matthew  7550:         $function='admin';
                   7551:     }
1.826     bisitz   7552:     if (($env{'request.role'}=~/^(au|ca|aa)/) ||
1.1025    raeburn  7553:         ($ENV{'REQUEST_URI'}=~ m{/^(/priv)})) {
1.182     matthew  7554:         $function='author';
                   7555:     }
                   7556:     return $function;
1.54      www      7557: }
1.99      www      7558: 
                   7559: ###############################################
                   7560: 
1.233     raeburn  7561: =pod
                   7562: 
1.821     raeburn  7563: =item * &show_course()
                   7564: 
                   7565: Used by lonmenu.pm and lonroles.pm to determine whether to use the word
                   7566: 'Courses' or 'Roles' in inline navigation and on screen displaying user's roles.
                   7567: 
                   7568: Inputs:
                   7569: None
                   7570: 
                   7571: Outputs:
                   7572: Scalar: 1 if 'Course' to be used, 0 otherwise.
                   7573: 
                   7574: =cut
                   7575: 
                   7576: ###############################################
                   7577: sub show_course {
                   7578:     my $course = !$env{'user.adv'};
                   7579:     if (!$env{'user.adv'}) {
                   7580:         foreach my $env (keys(%env)) {
                   7581:             next if ($env !~ m/^user\.priv\./);
                   7582:             if ($env !~ m/^user\.priv\.(?:st|cm)/) {
                   7583:                 $course = 0;
                   7584:                 last;
                   7585:             }
                   7586:         }
                   7587:     }
                   7588:     return $course;
                   7589: }
                   7590: 
                   7591: ###############################################
                   7592: 
                   7593: =pod
                   7594: 
1.542     raeburn  7595: =item * &check_user_status()
1.274     raeburn  7596: 
                   7597: Determines current status of supplied role for a
                   7598: specific user. Roles can be active, previous or future.
                   7599: 
                   7600: Inputs: 
                   7601: user's domain, user's username, course's domain,
1.375     raeburn  7602: course's number, optional section ID.
1.274     raeburn  7603: 
                   7604: Outputs:
                   7605: role status: active, previous or future. 
                   7606: 
                   7607: =cut
                   7608: 
                   7609: sub check_user_status {
1.412     raeburn  7610:     my ($udom,$uname,$cdom,$crs,$role,$sec) = @_;
1.982     raeburn  7611:     my $extra = &Apache::lonnet::freeze_escape({'skipcheck' => 1});
                   7612:     my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname,'.',undef,$extra);
1.274     raeburn  7613:     my @uroles = keys %userinfo;
                   7614:     my $srchstr;
                   7615:     my $active_chk = 'none';
1.412     raeburn  7616:     my $now = time;
1.274     raeburn  7617:     if (@uroles > 0) {
1.908     raeburn  7618:         if (($role eq 'cc') || ($role eq 'co') || ($sec eq '') || (!defined($sec))) {
1.274     raeburn  7619:             $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
                   7620:         } else {
1.412     raeburn  7621:             $srchstr = '/'.$cdom.'/'.$crs.'/'.$sec.'_'.$role;
                   7622:         }
                   7623:         if (grep/^\Q$srchstr\E$/,@uroles) {
1.274     raeburn  7624:             my $role_end = 0;
                   7625:             my $role_start = 0;
                   7626:             $active_chk = 'active';
1.412     raeburn  7627:             if ($userinfo{$srchstr} =~ m/^\Q$role\E_(\d+)/) {
                   7628:                 $role_end = $1;
                   7629:                 if ($userinfo{$srchstr} =~ m/^\Q$role\E_\Q$role_end\E_(\d+)$/) {
                   7630:                     $role_start = $1;
1.274     raeburn  7631:                 }
                   7632:             }
                   7633:             if ($role_start > 0) {
1.412     raeburn  7634:                 if ($now < $role_start) {
1.274     raeburn  7635:                     $active_chk = 'future';
                   7636:                 }
                   7637:             }
                   7638:             if ($role_end > 0) {
1.412     raeburn  7639:                 if ($now > $role_end) {
1.274     raeburn  7640:                     $active_chk = 'previous';
                   7641:                 }
                   7642:             }
                   7643:         }
                   7644:     }
                   7645:     return $active_chk;
                   7646: }
                   7647: 
                   7648: ###############################################
                   7649: 
                   7650: =pod
                   7651: 
1.405     albertel 7652: =item * &get_sections()
1.233     raeburn  7653: 
                   7654: Determines all the sections for a course including
                   7655: sections with students and sections containing other roles.
1.419     raeburn  7656: Incoming parameters: 
                   7657: 
                   7658: 1. domain
                   7659: 2. course number 
                   7660: 3. reference to array containing roles for which sections should 
                   7661: be gathered (optional).
                   7662: 4. reference to array containing status types for which sections 
                   7663: should be gathered (optional).
                   7664: 
                   7665: If the third argument is undefined, sections are gathered for any role. 
                   7666: If the fourth argument is undefined, sections are gathered for any status.
                   7667: Permissible values are 'active' or 'future' or 'previous'.
1.233     raeburn  7668:  
1.374     raeburn  7669: Returns section hash (keys are section IDs, values are
                   7670: number of users in each section), subject to the
1.419     raeburn  7671: optional roles filter, optional status filter 
1.233     raeburn  7672: 
                   7673: =cut
                   7674: 
                   7675: ###############################################
                   7676: sub get_sections {
1.419     raeburn  7677:     my ($cdom,$cnum,$possible_roles,$possible_status) = @_;
1.366     albertel 7678:     if (!defined($cdom) || !defined($cnum)) {
                   7679:         my $cid =  $env{'request.course.id'};
                   7680: 
                   7681: 	return if (!defined($cid));
                   7682: 
                   7683:         $cdom = $env{'course.'.$cid.'.domain'};
                   7684:         $cnum = $env{'course.'.$cid.'.num'};
                   7685:     }
                   7686: 
                   7687:     my %sectioncount;
1.419     raeburn  7688:     my $now = time;
1.240     albertel 7689: 
1.366     albertel 7690:     if (!defined($possible_roles) || (grep(/^st$/,@$possible_roles))) {
1.276     albertel 7691: 	my ($classlist) = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1.240     albertel 7692: 	my $sec_index = &Apache::loncoursedata::CL_SECTION();
                   7693: 	my $status_index = &Apache::loncoursedata::CL_STATUS();
1.419     raeburn  7694:         my $start_index = &Apache::loncoursedata::CL_START();
                   7695:         my $end_index = &Apache::loncoursedata::CL_END();
                   7696:         my $status;
1.366     albertel 7697: 	while (my ($student,$data) = each(%$classlist)) {
1.419     raeburn  7698: 	    my ($section,$stu_status,$start,$end) = ($data->[$sec_index],
                   7699: 				                     $data->[$status_index],
                   7700:                                                      $data->[$start_index],
                   7701:                                                      $data->[$end_index]);
                   7702:             if ($stu_status eq 'Active') {
                   7703:                 $status = 'active';
                   7704:             } elsif ($end < $now) {
                   7705:                 $status = 'previous';
                   7706:             } elsif ($start > $now) {
                   7707:                 $status = 'future';
                   7708:             } 
                   7709: 	    if ($section ne '-1' && $section !~ /^\s*$/) {
                   7710:                 if ((!defined($possible_status)) || (($status ne '') && 
                   7711:                     (grep/^\Q$status\E$/,@{$possible_status}))) { 
                   7712: 		    $sectioncount{$section}++;
                   7713:                 }
1.240     albertel 7714: 	    }
                   7715: 	}
                   7716:     }
                   7717:     my %courseroles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   7718:     foreach my $user (sort(keys(%courseroles))) {
                   7719: 	if ($user !~ /^(\w{2})/) { next; }
                   7720: 	my ($role) = ($user =~ /^(\w{2})/);
                   7721: 	if ($possible_roles && !(grep(/^$role$/,@$possible_roles))) { next; }
1.419     raeburn  7722: 	my ($section,$status);
1.240     albertel 7723: 	if ($role eq 'cr' &&
                   7724: 	    $user =~ m-^$role/[^/]*/[^/]*/[^/]*:[^:]*:[^:]*:(\w+)-) {
                   7725: 	    $section=$1;
                   7726: 	}
                   7727: 	if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
                   7728: 	if (!defined($section) || $section eq '-1') { next; }
1.419     raeburn  7729:         my ($end,$start) = ($courseroles{$user} =~ /^([^:]*):([^:]*)$/);
                   7730:         if ($end == -1 && $start == -1) {
                   7731:             next; #deleted role
                   7732:         }
                   7733:         if (!defined($possible_status)) { 
                   7734:             $sectioncount{$section}++;
                   7735:         } else {
                   7736:             if ((!$end || $end >= $now) && (!$start || $start <= $now)) {
                   7737:                 $status = 'active';
                   7738:             } elsif ($end < $now) {
                   7739:                 $status = 'future';
                   7740:             } elsif ($start > $now) {
                   7741:                 $status = 'previous';
                   7742:             }
                   7743:             if (($status ne '') && (grep/^\Q$status\E$/,@{$possible_status})) {
                   7744:                 $sectioncount{$section}++;
                   7745:             }
                   7746:         }
1.233     raeburn  7747:     }
1.366     albertel 7748:     return %sectioncount;
1.233     raeburn  7749: }
                   7750: 
1.274     raeburn  7751: ###############################################
1.294     raeburn  7752: 
                   7753: =pod
1.405     albertel 7754: 
                   7755: =item * &get_course_users()
                   7756: 
1.275     raeburn  7757: Retrieves usernames:domains for users in the specified course
                   7758: with specific role(s), and access status. 
                   7759: 
                   7760: Incoming parameters:
1.277     albertel 7761: 1. course domain
                   7762: 2. course number
                   7763: 3. access status: users must have - either active, 
1.275     raeburn  7764: previous, future, or all.
1.277     albertel 7765: 4. reference to array of permissible roles
1.288     raeburn  7766: 5. reference to array of section restrictions (optional)
                   7767: 6. reference to results object (hash of hashes).
                   7768: 7. reference to optional userdata hash
1.609     raeburn  7769: 8. reference to optional statushash
1.630     raeburn  7770: 9. flag if privileged users (except those set to unhide in
                   7771:    course settings) should be excluded    
1.609     raeburn  7772: Keys of top level results hash are roles.
1.275     raeburn  7773: Keys of inner hashes are username:domain, with 
                   7774: values set to access type.
1.288     raeburn  7775: Optional userdata hash returns an array with arguments in the 
                   7776: same order as loncoursedata::get_classlist() for student data.
                   7777: 
1.609     raeburn  7778: Optional statushash returns
                   7779: 
1.288     raeburn  7780: Entries for end, start, section and status are blank because
                   7781: of the possibility of multiple values for non-student roles.
                   7782: 
1.275     raeburn  7783: =cut
1.405     albertel 7784: 
1.275     raeburn  7785: ###############################################
1.405     albertel 7786: 
1.275     raeburn  7787: sub get_course_users {
1.630     raeburn  7788:     my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata,$statushash,$hidepriv) = @_;
1.288     raeburn  7789:     my %idx = ();
1.419     raeburn  7790:     my %seclists;
1.288     raeburn  7791: 
                   7792:     $idx{udom} = &Apache::loncoursedata::CL_SDOM();
                   7793:     $idx{uname} =  &Apache::loncoursedata::CL_SNAME();
                   7794:     $idx{end} = &Apache::loncoursedata::CL_END();
                   7795:     $idx{start} = &Apache::loncoursedata::CL_START();
                   7796:     $idx{id} = &Apache::loncoursedata::CL_ID();
                   7797:     $idx{section} = &Apache::loncoursedata::CL_SECTION();
                   7798:     $idx{fullname} = &Apache::loncoursedata::CL_FULLNAME();
                   7799:     $idx{status} = &Apache::loncoursedata::CL_STATUS();
                   7800: 
1.290     albertel 7801:     if (grep(/^st$/,@{$roles})) {
1.276     albertel 7802:         my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist($cdom,$cnum);
1.278     raeburn  7803:         my $now = time;
1.277     albertel 7804:         foreach my $student (keys(%{$classlist})) {
1.288     raeburn  7805:             my $match = 0;
1.412     raeburn  7806:             my $secmatch = 0;
1.419     raeburn  7807:             my $section = $$classlist{$student}[$idx{section}];
1.609     raeburn  7808:             my $status = $$classlist{$student}[$idx{status}];
1.419     raeburn  7809:             if ($section eq '') {
                   7810:                 $section = 'none';
                   7811:             }
1.291     albertel 7812:             if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 7813:                 if (grep(/^all$/,@{$sections})) {
1.412     raeburn  7814:                     $secmatch = 1;
                   7815:                 } elsif ($$classlist{$student}[$idx{section}] eq '') {
1.420     albertel 7816:                     if (grep(/^none$/,@{$sections})) {
1.412     raeburn  7817:                         $secmatch = 1;
                   7818:                     }
                   7819:                 } else {  
1.419     raeburn  7820: 		    if (grep(/^\Q$section\E$/,@{$sections})) {
1.412     raeburn  7821: 		        $secmatch = 1;
                   7822:                     }
1.290     albertel 7823: 		}
1.412     raeburn  7824:                 if (!$secmatch) {
                   7825:                     next;
                   7826:                 }
1.419     raeburn  7827:             }
1.275     raeburn  7828:             if (defined($$types{'active'})) {
1.288     raeburn  7829:                 if ($$classlist{$student}[$idx{status}] eq 'Active') {
1.275     raeburn  7830:                     push(@{$$users{st}{$student}},'active');
1.288     raeburn  7831:                     $match = 1;
1.275     raeburn  7832:                 }
                   7833:             }
                   7834:             if (defined($$types{'previous'})) {
1.609     raeburn  7835:                 if ($$classlist{$student}[$idx{status}] eq 'Expired') {
1.275     raeburn  7836:                     push(@{$$users{st}{$student}},'previous');
1.288     raeburn  7837:                     $match = 1;
1.275     raeburn  7838:                 }
                   7839:             }
                   7840:             if (defined($$types{'future'})) {
1.609     raeburn  7841:                 if ($$classlist{$student}[$idx{status}] eq 'Future') {
1.275     raeburn  7842:                     push(@{$$users{st}{$student}},'future');
1.288     raeburn  7843:                     $match = 1;
1.275     raeburn  7844:                 }
                   7845:             }
1.609     raeburn  7846:             if ($match) {
                   7847:                 push(@{$seclists{$student}},$section);
                   7848:                 if (ref($userdata) eq 'HASH') {
                   7849:                     $$userdata{$student} = $$classlist{$student};
                   7850:                 }
                   7851:                 if (ref($statushash) eq 'HASH') {
                   7852:                     $statushash->{$student}{'st'}{$section} = $status;
                   7853:                 }
1.288     raeburn  7854:             }
1.275     raeburn  7855:         }
                   7856:     }
1.412     raeburn  7857:     if ((@{$roles} > 1) || ((@{$roles} == 1) && ($$roles[0] ne "st"))) {
1.439     raeburn  7858:         my %coursepersonnel = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   7859:         my $now = time;
1.609     raeburn  7860:         my %displaystatus = ( previous => 'Expired',
                   7861:                               active   => 'Active',
                   7862:                               future   => 'Future',
                   7863:                             );
1.630     raeburn  7864:         my %nothide;
                   7865:         if ($hidepriv) {
                   7866:             my %coursehash=&Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                   7867:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   7868:                 if ($user !~ /:/) {
                   7869:                     $nothide{join(':',split(/[\@]/,$user))}=1;
                   7870:                 } else {
                   7871:                     $nothide{$user} = 1;
                   7872:                 }
                   7873:             }
                   7874:         }
1.439     raeburn  7875:         foreach my $person (sort(keys(%coursepersonnel))) {
1.288     raeburn  7876:             my $match = 0;
1.412     raeburn  7877:             my $secmatch = 0;
1.439     raeburn  7878:             my $status;
1.412     raeburn  7879:             my ($role,$user,$usec) = ($person =~ /^([^:]*):([^:]+:[^:]+):([^:]*)/);
1.275     raeburn  7880:             $user =~ s/:$//;
1.439     raeburn  7881:             my ($end,$start) = split(/:/,$coursepersonnel{$person});
                   7882:             if ($end == -1 || $start == -1) {
                   7883:                 next;
                   7884:             }
                   7885:             if (($role) && ((grep(/^\Q$role\E$/,@{$roles})) ||
                   7886:                 (grep(/^cr$/,@{$roles}) && $role =~ /^cr\//))) {
1.412     raeburn  7887:                 my ($uname,$udom) = split(/:/,$user);
                   7888:                 if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 7889:                     if (grep(/^all$/,@{$sections})) {
1.412     raeburn  7890:                         $secmatch = 1;
                   7891:                     } elsif ($usec eq '') {
1.420     albertel 7892:                         if (grep(/^none$/,@{$sections})) {
1.412     raeburn  7893:                             $secmatch = 1;
                   7894:                         }
                   7895:                     } else {
                   7896:                         if (grep(/^\Q$usec\E$/,@{$sections})) {
                   7897:                             $secmatch = 1;
                   7898:                         }
                   7899:                     }
                   7900:                     if (!$secmatch) {
                   7901:                         next;
                   7902:                     }
1.288     raeburn  7903:                 }
1.419     raeburn  7904:                 if ($usec eq '') {
                   7905:                     $usec = 'none';
                   7906:                 }
1.275     raeburn  7907:                 if ($uname ne '' && $udom ne '') {
1.630     raeburn  7908:                     if ($hidepriv) {
                   7909:                         if ((&Apache::lonnet::privileged($uname,$udom)) &&
                   7910:                             (!$nothide{$uname.':'.$udom})) {
                   7911:                             next;
                   7912:                         }
                   7913:                     }
1.503     raeburn  7914:                     if ($end > 0 && $end < $now) {
1.439     raeburn  7915:                         $status = 'previous';
                   7916:                     } elsif ($start > $now) {
                   7917:                         $status = 'future';
                   7918:                     } else {
                   7919:                         $status = 'active';
                   7920:                     }
1.277     albertel 7921:                     foreach my $type (keys(%{$types})) { 
1.275     raeburn  7922:                         if ($status eq $type) {
1.420     albertel 7923:                             if (!grep(/^\Q$type\E$/,@{$$users{$role}{$user}})) {
1.419     raeburn  7924:                                 push(@{$$users{$role}{$user}},$type);
                   7925:                             }
1.288     raeburn  7926:                             $match = 1;
                   7927:                         }
                   7928:                     }
1.419     raeburn  7929:                     if (($match) && (ref($userdata) eq 'HASH')) {
                   7930:                         if (!exists($$userdata{$uname.':'.$udom})) {
                   7931: 			    &get_user_info($udom,$uname,\%idx,$userdata);
                   7932:                         }
1.420     albertel 7933:                         if (!grep(/^\Q$usec\E$/,@{$seclists{$uname.':'.$udom}})) {
1.419     raeburn  7934:                             push(@{$seclists{$uname.':'.$udom}},$usec);
                   7935:                         }
1.609     raeburn  7936:                         if (ref($statushash) eq 'HASH') {
                   7937:                             $statushash->{$uname.':'.$udom}{$role}{$usec} = $displaystatus{$status};
                   7938:                         }
1.275     raeburn  7939:                     }
                   7940:                 }
                   7941:             }
                   7942:         }
1.290     albertel 7943:         if (grep(/^ow$/,@{$roles})) {
1.279     raeburn  7944:             if ((defined($cdom)) && (defined($cnum))) {
                   7945:                 my %csettings = &Apache::lonnet::get('environment',['internal.courseowner'],$cdom,$cnum);
                   7946:                 if ( defined($csettings{'internal.courseowner'}) ) {
                   7947:                     my $owner = $csettings{'internal.courseowner'};
1.609     raeburn  7948:                     next if ($owner eq '');
                   7949:                     my ($ownername,$ownerdom);
                   7950:                     if ($owner =~ /^([^:]+):([^:]+)$/) {
                   7951:                         $ownername = $1;
                   7952:                         $ownerdom = $2;
                   7953:                     } else {
                   7954:                         $ownername = $owner;
                   7955:                         $ownerdom = $cdom;
                   7956:                         $owner = $ownername.':'.$ownerdom;
1.439     raeburn  7957:                     }
                   7958:                     @{$$users{'ow'}{$owner}} = 'any';
1.290     albertel 7959:                     if (defined($userdata) && 
1.609     raeburn  7960: 			!exists($$userdata{$owner})) {
                   7961: 			&get_user_info($ownerdom,$ownername,\%idx,$userdata);
                   7962:                         if (!grep(/^none$/,@{$seclists{$owner}})) {
                   7963:                             push(@{$seclists{$owner}},'none');
                   7964:                         }
                   7965:                         if (ref($statushash) eq 'HASH') {
                   7966:                             $statushash->{$owner}{'ow'}{'none'} = 'Any';
1.419     raeburn  7967:                         }
1.290     albertel 7968: 		    }
1.279     raeburn  7969:                 }
                   7970:             }
                   7971:         }
1.419     raeburn  7972:         foreach my $user (keys(%seclists)) {
                   7973:             @{$seclists{$user}} = (sort {$a <=> $b} @{$seclists{$user}});
                   7974:             $$userdata{$user}[$idx{section}] = join(',',@{$seclists{$user}});
                   7975:         }
1.275     raeburn  7976:     }
                   7977:     return;
                   7978: }
                   7979: 
1.288     raeburn  7980: sub get_user_info {
                   7981:     my ($udom,$uname,$idx,$userdata) = @_;
1.289     albertel 7982:     $$userdata{$uname.':'.$udom}[$$idx{fullname}] = 
                   7983: 	&plainname($uname,$udom,'lastname');
1.291     albertel 7984:     $$userdata{$uname.':'.$udom}[$$idx{uname}] = $uname;
1.297     raeburn  7985:     $$userdata{$uname.':'.$udom}[$$idx{udom}] = $udom;
1.609     raeburn  7986:     my %idhash =  &Apache::lonnet::idrget($udom,($uname));
                   7987:     $$userdata{$uname.':'.$udom}[$$idx{id}] = $idhash{$uname}; 
1.288     raeburn  7988:     return;
                   7989: }
1.275     raeburn  7990: 
1.472     raeburn  7991: ###############################################
                   7992: 
                   7993: =pod
                   7994: 
                   7995: =item * &get_user_quota()
                   7996: 
                   7997: Retrieves quota assigned for storage of portfolio files for a user  
                   7998: 
                   7999: Incoming parameters:
                   8000: 1. user's username
                   8001: 2. user's domain
                   8002: 
                   8003: Returns:
1.536     raeburn  8004: 1. Disk quota (in Mb) assigned to student.
                   8005: 2. (Optional) Type of setting: custom or default
                   8006:    (individually assigned or default for user's 
                   8007:    institutional status).
                   8008: 3. (Optional) - User's institutional status (e.g., faculty, staff
                   8009:    or student - types as defined in localenroll::inst_usertypes 
                   8010:    for user's domain, which determines default quota for user.
                   8011: 4. (Optional) - Default quota which would apply to the user.
1.472     raeburn  8012: 
                   8013: If a value has been stored in the user's environment, 
1.536     raeburn  8014: it will return that, otherwise it returns the maximal default
                   8015: defined for the user's instituional status(es) in the domain.
1.472     raeburn  8016: 
                   8017: =cut
                   8018: 
                   8019: ###############################################
                   8020: 
                   8021: 
                   8022: sub get_user_quota {
                   8023:     my ($uname,$udom) = @_;
1.536     raeburn  8024:     my ($quota,$quotatype,$settingstatus,$defquota);
1.472     raeburn  8025:     if (!defined($udom)) {
                   8026:         $udom = $env{'user.domain'};
                   8027:     }
                   8028:     if (!defined($uname)) {
                   8029:         $uname = $env{'user.name'};
                   8030:     }
                   8031:     if (($udom eq '' || $uname eq '') ||
                   8032:         ($udom eq 'public') && ($uname eq 'public')) {
                   8033:         $quota = 0;
1.536     raeburn  8034:         $quotatype = 'default';
                   8035:         $defquota = 0; 
1.472     raeburn  8036:     } else {
1.536     raeburn  8037:         my $inststatus;
1.472     raeburn  8038:         if ($udom eq $env{'user.domain'} && $uname eq $env{'user.name'}) {
                   8039:             $quota = $env{'environment.portfolioquota'};
1.536     raeburn  8040:             $inststatus = $env{'environment.inststatus'};
1.472     raeburn  8041:         } else {
1.536     raeburn  8042:             my %userenv = 
                   8043:                 &Apache::lonnet::get('environment',['portfolioquota',
                   8044:                                      'inststatus'],$udom,$uname);
1.472     raeburn  8045:             my ($tmp) = keys(%userenv);
                   8046:             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   8047:                 $quota = $userenv{'portfolioquota'};
1.536     raeburn  8048:                 $inststatus = $userenv{'inststatus'};
1.472     raeburn  8049:             } else {
                   8050:                 undef(%userenv);
                   8051:             }
                   8052:         }
1.536     raeburn  8053:         ($defquota,$settingstatus) = &default_quota($udom,$inststatus);
1.472     raeburn  8054:         if ($quota eq '') {
1.536     raeburn  8055:             $quota = $defquota;
                   8056:             $quotatype = 'default';
                   8057:         } else {
                   8058:             $quotatype = 'custom';
1.472     raeburn  8059:         }
                   8060:     }
1.536     raeburn  8061:     if (wantarray) {
                   8062:         return ($quota,$quotatype,$settingstatus,$defquota);
                   8063:     } else {
                   8064:         return $quota;
                   8065:     }
1.472     raeburn  8066: }
                   8067: 
                   8068: ###############################################
                   8069: 
                   8070: =pod
                   8071: 
                   8072: =item * &default_quota()
                   8073: 
1.536     raeburn  8074: Retrieves default quota assigned for storage of user portfolio files,
                   8075: given an (optional) user's institutional status.
1.472     raeburn  8076: 
                   8077: Incoming parameters:
                   8078: 1. domain
1.536     raeburn  8079: 2. (Optional) institutional status(es).  This is a : separated list of 
                   8080:    status types (e.g., faculty, staff, student etc.)
                   8081:    which apply to the user for whom the default is being retrieved.
                   8082:    If the institutional status string in undefined, the domain
                   8083:    default quota will be returned. 
1.472     raeburn  8084: 
                   8085: Returns:
                   8086: 1. Default disk quota (in Mb) for user portfolios in the domain.
1.536     raeburn  8087: 2. (Optional) institutional type which determined the value of the
                   8088:    default quota.
1.472     raeburn  8089: 
                   8090: If a value has been stored in the domain's configuration db,
                   8091: it will return that, otherwise it returns 20 (for backwards 
                   8092: compatibility with domains which have not set up a configuration
                   8093: db file; the original statically defined portfolio quota was 20 Mb). 
                   8094: 
1.536     raeburn  8095: If the user's status includes multiple types (e.g., staff and student),
                   8096: the largest default quota which applies to the user determines the
                   8097: default quota returned.
                   8098: 
1.780     raeburn  8099: =back
                   8100: 
1.472     raeburn  8101: =cut
                   8102: 
                   8103: ###############################################
                   8104: 
                   8105: 
                   8106: sub default_quota {
1.536     raeburn  8107:     my ($udom,$inststatus) = @_;
                   8108:     my ($defquota,$settingstatus);
                   8109:     my %quotahash = &Apache::lonnet::get_dom('configuration',
1.622     raeburn  8110:                                             ['quotas'],$udom);
                   8111:     if (ref($quotahash{'quotas'}) eq 'HASH') {
1.536     raeburn  8112:         if ($inststatus ne '') {
1.765     raeburn  8113:             my @statuses = map { &unescape($_); } split(/:/,$inststatus);
1.536     raeburn  8114:             foreach my $item (@statuses) {
1.711     raeburn  8115:                 if (ref($quotahash{'quotas'}{'defaultquota'}) eq 'HASH') {
                   8116:                     if ($quotahash{'quotas'}{'defaultquota'}{$item} ne '') {
                   8117:                         if ($defquota eq '') {
                   8118:                             $defquota = $quotahash{'quotas'}{'defaultquota'}{$item};
                   8119:                             $settingstatus = $item;
                   8120:                         } elsif ($quotahash{'quotas'}{'defaultquota'}{$item} > $defquota) {
                   8121:                             $defquota = $quotahash{'quotas'}{'defaultquota'}{$item};
                   8122:                             $settingstatus = $item;
                   8123:                         }
                   8124:                     }
                   8125:                 } else {
                   8126:                     if ($quotahash{'quotas'}{$item} ne '') {
                   8127:                         if ($defquota eq '') {
                   8128:                             $defquota = $quotahash{'quotas'}{$item};
                   8129:                             $settingstatus = $item;
                   8130:                         } elsif ($quotahash{'quotas'}{$item} > $defquota) {
                   8131:                             $defquota = $quotahash{'quotas'}{$item};
                   8132:                             $settingstatus = $item;
                   8133:                         }
1.536     raeburn  8134:                     }
                   8135:                 }
                   8136:             }
                   8137:         }
                   8138:         if ($defquota eq '') {
1.711     raeburn  8139:             if (ref($quotahash{'quotas'}{'defaultquota'}) eq 'HASH') {
                   8140:                 $defquota = $quotahash{'quotas'}{'defaultquota'}{'default'};
                   8141:             } else {
                   8142:                 $defquota = $quotahash{'quotas'}{'default'};
                   8143:             }
1.536     raeburn  8144:             $settingstatus = 'default';
                   8145:         }
                   8146:     } else {
                   8147:         $settingstatus = 'default';
                   8148:         $defquota = 20;
                   8149:     }
                   8150:     if (wantarray) {
                   8151:         return ($defquota,$settingstatus);
1.472     raeburn  8152:     } else {
1.536     raeburn  8153:         return $defquota;
1.472     raeburn  8154:     }
                   8155: }
                   8156: 
1.384     raeburn  8157: sub get_secgrprole_info {
                   8158:     my ($cdom,$cnum,$needroles,$type)  = @_;
                   8159:     my %sections_count = &get_sections($cdom,$cnum);
                   8160:     my @sections =  (sort {$a <=> $b} keys(%sections_count));
                   8161:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
                   8162:     my @groups = sort(keys(%curr_groups));
                   8163:     my $allroles = [];
                   8164:     my $rolehash;
                   8165:     my $accesshash = {
                   8166:                      active => 'Currently has access',
                   8167:                      future => 'Will have future access',
                   8168:                      previous => 'Previously had access',
                   8169:                   };
                   8170:     if ($needroles) {
                   8171:         $rolehash = {'all' => 'all'};
1.385     albertel 8172:         my %user_roles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   8173: 	if (&Apache::lonnet::error(%user_roles)) {
                   8174: 	    undef(%user_roles);
                   8175: 	}
                   8176:         foreach my $item (keys(%user_roles)) {
1.384     raeburn  8177:             my ($role)=split(/\:/,$item,2);
                   8178:             if ($role eq 'cr') { next; }
                   8179:             if ($role =~ /^cr/) {
                   8180:                 $$rolehash{$role} = (split('/',$role))[3];
                   8181:             } else {
                   8182:                 $$rolehash{$role} = &Apache::lonnet::plaintext($role,$type);
                   8183:             }
                   8184:         }
                   8185:         foreach my $key (sort(keys(%{$rolehash}))) {
                   8186:             push(@{$allroles},$key);
                   8187:         }
                   8188:         push (@{$allroles},'st');
                   8189:         $$rolehash{'st'} = &Apache::lonnet::plaintext('st',$type);
                   8190:     }
                   8191:     return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
                   8192: }
                   8193: 
1.555     raeburn  8194: sub user_picker {
1.994     raeburn  8195:     my ($dom,$srch,$forcenewuser,$caller,$cancreate,$usertype,$context) = @_;
1.555     raeburn  8196:     my $currdom = $dom;
                   8197:     my %curr_selected = (
                   8198:                         srchin => 'dom',
1.580     raeburn  8199:                         srchby => 'lastname',
1.555     raeburn  8200:                       );
                   8201:     my $srchterm;
1.625     raeburn  8202:     if ((ref($srch) eq 'HASH') && ($env{'form.origform'} ne 'crtusername')) {
1.555     raeburn  8203:         if ($srch->{'srchby'} ne '') {
                   8204:             $curr_selected{'srchby'} = $srch->{'srchby'};
                   8205:         }
                   8206:         if ($srch->{'srchin'} ne '') {
                   8207:             $curr_selected{'srchin'} = $srch->{'srchin'};
                   8208:         }
                   8209:         if ($srch->{'srchtype'} ne '') {
                   8210:             $curr_selected{'srchtype'} = $srch->{'srchtype'};
                   8211:         }
                   8212:         if ($srch->{'srchdomain'} ne '') {
                   8213:             $currdom = $srch->{'srchdomain'};
                   8214:         }
                   8215:         $srchterm = $srch->{'srchterm'};
                   8216:     }
                   8217:     my %lt=&Apache::lonlocal::texthash(
1.573     raeburn  8218:                     'usr'       => 'Search criteria',
1.563     raeburn  8219:                     'doma'      => 'Domain/institution to search',
1.558     albertel 8220:                     'uname'     => 'username',
                   8221:                     'lastname'  => 'last name',
1.555     raeburn  8222:                     'lastfirst' => 'last name, first name',
1.558     albertel 8223:                     'crs'       => 'in this course',
1.576     raeburn  8224:                     'dom'       => 'in selected LON-CAPA domain', 
1.558     albertel 8225:                     'alc'       => 'all LON-CAPA',
1.573     raeburn  8226:                     'instd'     => 'in institutional directory for selected domain',
1.558     albertel 8227:                     'exact'     => 'is',
                   8228:                     'contains'  => 'contains',
1.569     raeburn  8229:                     'begins'    => 'begins with',
1.571     raeburn  8230:                     'youm'      => "You must include some text to search for.",
                   8231:                     'thte'      => "The text you are searching for must contain at least two characters when using a 'begins' type search.",
                   8232:                     'thet'      => "The text you are searching for must contain at least three characters when using a 'contains' type search.",
                   8233:                     'yomc'      => "You must choose a domain when using an institutional directory search.",
                   8234:                     'ymcd'      => "You must choose a domain when using a domain search.",
                   8235:                     'whus'      => "When using searching by last,first you must include a comma as separator between last name and first name.",
                   8236:                     'whse'      => "When searching by last,first you must include at least one character in the first name.",
                   8237:                      'thfo'     => "The following need to be corrected before the search can be run:",
1.555     raeburn  8238:                                        );
1.563     raeburn  8239:     my $domform = &select_dom_form($currdom,'srchdomain',1,1);
                   8240:     my $srchinsel = ' <select name="srchin">';
1.555     raeburn  8241: 
                   8242:     my @srchins = ('crs','dom','alc','instd');
                   8243: 
                   8244:     foreach my $option (@srchins) {
                   8245:         # FIXME 'alc' option unavailable until 
                   8246:         #       loncreateuser::print_user_query_page()
                   8247:         #       has been completed.
                   8248:         next if ($option eq 'alc');
1.880     raeburn  8249:         next if (($option eq 'crs') && ($env{'form.form'} eq 'requestcrs'));  
1.555     raeburn  8250:         next if ($option eq 'crs' && !$env{'request.course.id'});
1.563     raeburn  8251:         if ($curr_selected{'srchin'} eq $option) {
                   8252:             $srchinsel .= ' 
                   8253:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   8254:         } else {
                   8255:             $srchinsel .= '
                   8256:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   8257:         }
1.555     raeburn  8258:     }
1.563     raeburn  8259:     $srchinsel .= "\n  </select>\n";
1.555     raeburn  8260: 
                   8261:     my $srchbysel =  ' <select name="srchby">';
1.580     raeburn  8262:     foreach my $option ('lastname','lastfirst','uname') {
1.555     raeburn  8263:         if ($curr_selected{'srchby'} eq $option) {
                   8264:             $srchbysel .= '
                   8265:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   8266:         } else {
                   8267:             $srchbysel .= '
                   8268:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   8269:          }
                   8270:     }
                   8271:     $srchbysel .= "\n  </select>\n";
                   8272: 
                   8273:     my $srchtypesel = ' <select name="srchtype">';
1.580     raeburn  8274:     foreach my $option ('begins','contains','exact') {
1.555     raeburn  8275:         if ($curr_selected{'srchtype'} eq $option) {
                   8276:             $srchtypesel .= '
                   8277:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   8278:         } else {
                   8279:             $srchtypesel .= '
                   8280:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   8281:         }
                   8282:     }
                   8283:     $srchtypesel .= "\n  </select>\n";
                   8284: 
1.558     albertel 8285:     my ($newuserscript,$new_user_create);
1.994     raeburn  8286:     my $context_dom = $env{'request.role.domain'};
                   8287:     if ($context eq 'requestcrs') {
                   8288:         if ($env{'form.coursedom'} ne '') { 
                   8289:             $context_dom = $env{'form.coursedom'};
                   8290:         }
                   8291:     }
1.556     raeburn  8292:     if ($forcenewuser) {
1.576     raeburn  8293:         if (ref($srch) eq 'HASH') {
1.994     raeburn  8294:             if ($srch->{'srchby'} eq 'uname' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchin'} eq 'dom' && $srch->{'srchdomain'} eq $context_dom) {
1.627     raeburn  8295:                 if ($cancreate) {
                   8296:                     $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>';
                   8297:                 } else {
1.799     bisitz   8298:                     my $helplink = 'javascript:helpMenu('."'display'".')';
1.627     raeburn  8299:                     my %usertypetext = (
                   8300:                         official   => 'institutional',
                   8301:                         unofficial => 'non-institutional',
                   8302:                     );
1.799     bisitz   8303:                     $new_user_create = '<p class="LC_warning">'
                   8304:                                       .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
                   8305:                                       .' '
                   8306:                                       .&mt('Please contact the [_1]helpdesk[_2] for assistance.'
                   8307:                                           ,'<a href="'.$helplink.'">','</a>')
                   8308:                                       .'</p><br />';
1.627     raeburn  8309:                 }
1.576     raeburn  8310:             }
                   8311:         }
                   8312: 
1.556     raeburn  8313:         $newuserscript = <<"ENDSCRIPT";
                   8314: 
1.570     raeburn  8315: function setSearch(createnew,callingForm) {
1.556     raeburn  8316:     if (createnew == 1) {
1.570     raeburn  8317:         for (var i=0; i<callingForm.srchby.length; i++) {
                   8318:             if (callingForm.srchby.options[i].value == 'uname') {
                   8319:                 callingForm.srchby.selectedIndex = i;
1.556     raeburn  8320:             }
                   8321:         }
1.570     raeburn  8322:         for (var i=0; i<callingForm.srchin.length; i++) {
                   8323:             if ( callingForm.srchin.options[i].value == 'dom') {
                   8324: 		callingForm.srchin.selectedIndex = i;
1.556     raeburn  8325:             }
                   8326:         }
1.570     raeburn  8327:         for (var i=0; i<callingForm.srchtype.length; i++) {
                   8328:             if (callingForm.srchtype.options[i].value == 'exact') {
                   8329:                 callingForm.srchtype.selectedIndex = i;
1.556     raeburn  8330:             }
                   8331:         }
1.570     raeburn  8332:         for (var i=0; i<callingForm.srchdomain.length; i++) {
1.994     raeburn  8333:             if (callingForm.srchdomain.options[i].value == '$context_dom') {
1.570     raeburn  8334:                 callingForm.srchdomain.selectedIndex = i;
1.556     raeburn  8335:             }
                   8336:         }
                   8337:     }
                   8338: }
                   8339: ENDSCRIPT
1.558     albertel 8340: 
1.556     raeburn  8341:     }
                   8342: 
1.555     raeburn  8343:     my $output = <<"END_BLOCK";
1.556     raeburn  8344: <script type="text/javascript">
1.824     bisitz   8345: // <![CDATA[
1.570     raeburn  8346: function validateEntry(callingForm) {
1.558     albertel 8347: 
1.556     raeburn  8348:     var checkok = 1;
1.558     albertel 8349:     var srchin;
1.570     raeburn  8350:     for (var i=0; i<callingForm.srchin.length; i++) {
                   8351: 	if ( callingForm.srchin[i].checked ) {
                   8352: 	    srchin = callingForm.srchin[i].value;
1.558     albertel 8353: 	}
                   8354:     }
                   8355: 
1.570     raeburn  8356:     var srchtype = callingForm.srchtype.options[callingForm.srchtype.selectedIndex].value;
                   8357:     var srchby = callingForm.srchby.options[callingForm.srchby.selectedIndex].value;
                   8358:     var srchdomain = callingForm.srchdomain.options[callingForm.srchdomain.selectedIndex].value;
                   8359:     var srchterm =  callingForm.srchterm.value;
                   8360:     var srchin = callingForm.srchin.options[callingForm.srchin.selectedIndex].value;
1.556     raeburn  8361:     var msg = "";
                   8362: 
                   8363:     if (srchterm == "") {
                   8364:         checkok = 0;
1.571     raeburn  8365:         msg += "$lt{'youm'}\\n";
1.556     raeburn  8366:     }
                   8367: 
1.569     raeburn  8368:     if (srchtype== 'begins') {
                   8369:         if (srchterm.length < 2) {
                   8370:             checkok = 0;
1.571     raeburn  8371:             msg += "$lt{'thte'}\\n";
1.569     raeburn  8372:         }
                   8373:     }
                   8374: 
1.556     raeburn  8375:     if (srchtype== 'contains') {
                   8376:         if (srchterm.length < 3) {
                   8377:             checkok = 0;
1.571     raeburn  8378:             msg += "$lt{'thet'}\\n";
1.556     raeburn  8379:         }
                   8380:     }
                   8381:     if (srchin == 'instd') {
                   8382:         if (srchdomain == '') {
                   8383:             checkok = 0;
1.571     raeburn  8384:             msg += "$lt{'yomc'}\\n";
1.556     raeburn  8385:         }
                   8386:     }
                   8387:     if (srchin == 'dom') {
                   8388:         if (srchdomain == '') {
                   8389:             checkok = 0;
1.571     raeburn  8390:             msg += "$lt{'ymcd'}\\n";
1.556     raeburn  8391:         }
                   8392:     }
                   8393:     if (srchby == 'lastfirst') {
                   8394:         if (srchterm.indexOf(",") == -1) {
                   8395:             checkok = 0;
1.571     raeburn  8396:             msg += "$lt{'whus'}\\n";
1.556     raeburn  8397:         }
                   8398:         if (srchterm.indexOf(",") == srchterm.length -1) {
                   8399:             checkok = 0;
1.571     raeburn  8400:             msg += "$lt{'whse'}\\n";
1.556     raeburn  8401:         }
                   8402:     }
                   8403:     if (checkok == 0) {
1.571     raeburn  8404:         alert("$lt{'thfo'}\\n"+msg);
1.556     raeburn  8405:         return;
                   8406:     }
                   8407:     if (checkok == 1) {
1.570     raeburn  8408:         callingForm.submit();
1.556     raeburn  8409:     }
                   8410: }
                   8411: 
                   8412: $newuserscript
                   8413: 
1.824     bisitz   8414: // ]]>
1.556     raeburn  8415: </script>
1.558     albertel 8416: 
                   8417: $new_user_create
                   8418: 
1.555     raeburn  8419: END_BLOCK
1.558     albertel 8420: 
1.876     raeburn  8421:     $output .= &Apache::lonhtmlcommon::start_pick_box().
                   8422:                &Apache::lonhtmlcommon::row_title($lt{'doma'}).
                   8423:                $domform.
                   8424:                &Apache::lonhtmlcommon::row_closure().
                   8425:                &Apache::lonhtmlcommon::row_title($lt{'usr'}).
                   8426:                $srchbysel.
                   8427:                $srchtypesel. 
                   8428:                '<input type="text" size="15" name="srchterm" value="'.$srchterm.'" />'.
                   8429:                $srchinsel.
                   8430:                &Apache::lonhtmlcommon::row_closure(1). 
                   8431:                &Apache::lonhtmlcommon::end_pick_box().
                   8432:                '<br />';
1.555     raeburn  8433:     return $output;
                   8434: }
                   8435: 
1.612     raeburn  8436: sub user_rule_check {
1.615     raeburn  8437:     my ($usershash,$checks,$alerts,$rulematch,$inst_results,$curr_rules,$got_rules) = @_;
1.612     raeburn  8438:     my $response;
                   8439:     if (ref($usershash) eq 'HASH') {
                   8440:         foreach my $user (keys(%{$usershash})) {
                   8441:             my ($uname,$udom) = split(/:/,$user);
                   8442:             next if ($udom eq '' || $uname eq '');
1.615     raeburn  8443:             my ($id,$newuser);
1.612     raeburn  8444:             if (ref($usershash->{$user}) eq 'HASH') {
1.615     raeburn  8445:                 $newuser = $usershash->{$user}->{'newuser'};
1.612     raeburn  8446:                 $id = $usershash->{$user}->{'id'};
                   8447:             }
                   8448:             my $inst_response;
                   8449:             if (ref($checks) eq 'HASH') {
                   8450:                 if (defined($checks->{'username'})) {
1.615     raeburn  8451:                     ($inst_response,%{$inst_results->{$user}}) = 
1.612     raeburn  8452:                         &Apache::lonnet::get_instuser($udom,$uname);
                   8453:                 } elsif (defined($checks->{'id'})) {
1.615     raeburn  8454:                     ($inst_response,%{$inst_results->{$user}}) =
1.612     raeburn  8455:                         &Apache::lonnet::get_instuser($udom,undef,$id);
                   8456:                 }
1.615     raeburn  8457:             } else {
                   8458:                 ($inst_response,%{$inst_results->{$user}}) =
                   8459:                     &Apache::lonnet::get_instuser($udom,$uname);
                   8460:                 return;
1.612     raeburn  8461:             }
1.615     raeburn  8462:             if (!$got_rules->{$udom}) {
1.612     raeburn  8463:                 my %domconfig = &Apache::lonnet::get_dom('configuration',
                   8464:                                                   ['usercreation'],$udom);
                   8465:                 if (ref($domconfig{'usercreation'}) eq 'HASH') {
1.615     raeburn  8466:                     foreach my $item ('username','id') {
1.612     raeburn  8467:                         if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
                   8468:                             $$curr_rules{$udom}{$item} = 
                   8469:                                 $domconfig{'usercreation'}{$item.'_rule'};
1.585     raeburn  8470:                         }
                   8471:                     }
                   8472:                 }
1.615     raeburn  8473:                 $got_rules->{$udom} = 1;  
1.585     raeburn  8474:             }
1.612     raeburn  8475:             foreach my $item (keys(%{$checks})) {
                   8476:                 if (ref($$curr_rules{$udom}) eq 'HASH') {
                   8477:                     if (ref($$curr_rules{$udom}{$item}) eq 'ARRAY') {
                   8478:                         if (@{$$curr_rules{$udom}{$item}} > 0) {
                   8479:                             my %rule_check = &Apache::lonnet::inst_rulecheck($udom,$uname,$id,$item,$$curr_rules{$udom}{$item});
                   8480:                             foreach my $rule (@{$$curr_rules{$udom}{$item}}) {
                   8481:                                 if ($rule_check{$rule}) {
                   8482:                                     $$rulematch{$user}{$item} = $rule;
                   8483:                                     if ($inst_response eq 'ok') {
1.615     raeburn  8484:                                         if (ref($inst_results) eq 'HASH') {
                   8485:                                             if (ref($inst_results->{$user}) eq 'HASH') {
                   8486:                                                 if (keys(%{$inst_results->{$user}}) == 0) {
                   8487:                                                     $$alerts{$item}{$udom}{$uname} = 1;
                   8488:                                                 }
1.612     raeburn  8489:                                             }
                   8490:                                         }
1.615     raeburn  8491:                                     }
                   8492:                                     last;
1.585     raeburn  8493:                                 }
                   8494:                             }
                   8495:                         }
                   8496:                     }
                   8497:                 }
                   8498:             }
                   8499:         }
                   8500:     }
1.612     raeburn  8501:     return;
                   8502: }
                   8503: 
                   8504: sub user_rule_formats {
                   8505:     my ($domain,$domdesc,$curr_rules,$check) = @_;
                   8506:     my %text = ( 
                   8507:                  'username' => 'Usernames',
                   8508:                  'id'       => 'IDs',
                   8509:                );
                   8510:     my $output;
                   8511:     my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($domain,$check);
                   8512:     if ((ref($rules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
                   8513:         if (@{$ruleorder} > 0) {
                   8514:             $output = '<br />'.&mt("$text{$check} with the following format(s) may <span class=\"LC_cusr_emph\">only</span> be used for verified users at [_1]:",$domdesc).' <ul>';
                   8515:             foreach my $rule (@{$ruleorder}) {
                   8516:                 if (ref($curr_rules) eq 'ARRAY') {
                   8517:                     if (grep(/^\Q$rule\E$/,@{$curr_rules})) {
                   8518:                         if (ref($rules->{$rule}) eq 'HASH') {
                   8519:                             $output .= '<li>'.$rules->{$rule}{'name'}.': '.
                   8520:                                         $rules->{$rule}{'desc'}.'</li>';
                   8521:                         }
                   8522:                     }
                   8523:                 }
                   8524:             }
                   8525:             $output .= '</ul>';
                   8526:         }
                   8527:     }
                   8528:     return $output;
                   8529: }
                   8530: 
                   8531: sub instrule_disallow_msg {
1.615     raeburn  8532:     my ($checkitem,$domdesc,$count,$mode) = @_;
1.612     raeburn  8533:     my $response;
                   8534:     my %text = (
                   8535:                   item   => 'username',
                   8536:                   items  => 'usernames',
                   8537:                   match  => 'matches',
                   8538:                   do     => 'does',
                   8539:                   action => 'a username',
                   8540:                   one    => 'one',
                   8541:                );
                   8542:     if ($count > 1) {
                   8543:         $text{'item'} = 'usernames';
                   8544:         $text{'match'} ='match';
                   8545:         $text{'do'} = 'do';
                   8546:         $text{'action'} = 'usernames',
                   8547:         $text{'one'} = 'ones';
                   8548:     }
                   8549:     if ($checkitem eq 'id') {
                   8550:         $text{'items'} = 'IDs';
                   8551:         $text{'item'} = 'ID';
                   8552:         $text{'action'} = 'an ID';
1.615     raeburn  8553:         if ($count > 1) {
                   8554:             $text{'item'} = 'IDs';
                   8555:             $text{'action'} = 'IDs';
                   8556:         }
1.612     raeburn  8557:     }
1.674     bisitz   8558:     $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  8559:     if ($mode eq 'upload') {
                   8560:         if ($checkitem eq 'username') {
                   8561:             $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'}.");
                   8562:         } elsif ($checkitem eq 'id') {
1.674     bisitz   8563:             $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  8564:         }
1.669     raeburn  8565:     } elsif ($mode eq 'selfcreate') {
                   8566:         if ($checkitem eq 'id') {
                   8567:             $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.");
                   8568:         }
1.615     raeburn  8569:     } else {
                   8570:         if ($checkitem eq 'username') {
                   8571:             $response .= &mt("You must choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}.");
                   8572:         } elsif ($checkitem eq 'id') {
                   8573:             $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.");
                   8574:         }
1.612     raeburn  8575:     }
                   8576:     return $response;
1.585     raeburn  8577: }
                   8578: 
1.624     raeburn  8579: sub personal_data_fieldtitles {
                   8580:     my %fieldtitles = &Apache::lonlocal::texthash (
                   8581:                         id => 'Student/Employee ID',
                   8582:                         permanentemail => 'E-mail address',
                   8583:                         lastname => 'Last Name',
                   8584:                         firstname => 'First Name',
                   8585:                         middlename => 'Middle Name',
                   8586:                         generation => 'Generation',
                   8587:                         gen => 'Generation',
1.765     raeburn  8588:                         inststatus => 'Affiliation',
1.624     raeburn  8589:                    );
                   8590:     return %fieldtitles;
                   8591: }
                   8592: 
1.642     raeburn  8593: sub sorted_inst_types {
                   8594:     my ($dom) = @_;
                   8595:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($dom);
                   8596:     my $othertitle = &mt('All users');
                   8597:     if ($env{'request.course.id'}) {
1.668     raeburn  8598:         $othertitle  = &mt('Any users');
1.642     raeburn  8599:     }
                   8600:     my @types;
                   8601:     if (ref($order) eq 'ARRAY') {
                   8602:         @types = @{$order};
                   8603:     }
                   8604:     if (@types == 0) {
                   8605:         if (ref($usertypes) eq 'HASH') {
                   8606:             @types = sort(keys(%{$usertypes}));
                   8607:         }
                   8608:     }
                   8609:     if (keys(%{$usertypes}) > 0) {
                   8610:         $othertitle = &mt('Other users');
                   8611:     }
                   8612:     return ($othertitle,$usertypes,\@types);
                   8613: }
                   8614: 
1.645     raeburn  8615: sub get_institutional_codes {
                   8616:     my ($settings,$allcourses,$LC_code) = @_;
                   8617: # Get complete list of course sections to update
                   8618:     my @currsections = ();
                   8619:     my @currxlists = ();
                   8620:     my $coursecode = $$settings{'internal.coursecode'};
                   8621: 
                   8622:     if ($$settings{'internal.sectionnums'} ne '') {
                   8623:         @currsections = split(/,/,$$settings{'internal.sectionnums'});
                   8624:     }
                   8625: 
                   8626:     if ($$settings{'internal.crosslistings'} ne '') {
                   8627:         @currxlists = split(/,/,$$settings{'internal.crosslistings'});
                   8628:     }
                   8629: 
                   8630:     if (@currxlists > 0) {
                   8631:         foreach (@currxlists) {
                   8632:             if (m/^([^:]+):(\w*)$/) {
                   8633:                 unless (grep/^$1$/,@{$allcourses}) {
                   8634:                     push @{$allcourses},$1;
                   8635:                     $$LC_code{$1} = $2;
                   8636:                 }
                   8637:             }
                   8638:         }
                   8639:     }
                   8640:  
                   8641:     if (@currsections > 0) {
                   8642:         foreach (@currsections) {
                   8643:             if (m/^(\w+):(\w*)$/) {
                   8644:                 my $sec = $coursecode.$1;
                   8645:                 my $lc_sec = $2;
                   8646:                 unless (grep/^$sec$/,@{$allcourses}) {
                   8647:                     push @{$allcourses},$sec;
                   8648:                     $$LC_code{$sec} = $lc_sec;
                   8649:                 }
                   8650:             }
                   8651:         }
                   8652:     }
                   8653:     return;
                   8654: }
                   8655: 
1.971     raeburn  8656: sub get_standard_codeitems {
                   8657:     return ('Year','Semester','Department','Number','Section');
                   8658: }
                   8659: 
1.112     bowersj2 8660: =pod
                   8661: 
1.780     raeburn  8662: =head1 Slot Helpers
                   8663: 
                   8664: =over 4
                   8665: 
                   8666: =item * sorted_slots()
                   8667: 
1.1040    raeburn  8668: Sorts an array of slot names in order of an optional sort key,
                   8669: default sort is by slot start time (earliest first). 
1.780     raeburn  8670: 
                   8671: Inputs:
                   8672: 
                   8673: =over 4
                   8674: 
                   8675: slotsarr  - Reference to array of unsorted slot names.
                   8676: 
                   8677: slots     - Reference to hash of hash, where outer hash keys are slot names.
                   8678: 
1.1040    raeburn  8679: sortkey   - Name of key in inner hash to be sorted on (e.g., starttime).
                   8680: 
1.549     albertel 8681: =back
                   8682: 
1.780     raeburn  8683: Returns:
                   8684: 
                   8685: =over 4
                   8686: 
1.1040    raeburn  8687: sorted   - An array of slot names sorted by a specified sort key 
                   8688:            (default sort key is start time of the slot).
1.780     raeburn  8689: 
                   8690: =back
                   8691: 
                   8692: =cut
                   8693: 
                   8694: 
                   8695: sub sorted_slots {
1.1040    raeburn  8696:     my ($slotsarr,$slots,$sortkey) = @_;
                   8697:     if ($sortkey eq '') {
                   8698:         $sortkey = 'starttime';
                   8699:     }
1.780     raeburn  8700:     my @sorted;
                   8701:     if ((ref($slotsarr) eq 'ARRAY') && (ref($slots) eq 'HASH')) {
                   8702:         @sorted =
                   8703:             sort {
                   8704:                      if (ref($slots->{$a}) && ref($slots->{$b})) {
1.1040    raeburn  8705:                          return $slots->{$a}{$sortkey} <=> $slots->{$b}{$sortkey}
1.780     raeburn  8706:                      }
                   8707:                      if (ref($slots->{$a})) { return -1;}
                   8708:                      if (ref($slots->{$b})) { return 1;}
                   8709:                      return 0;
                   8710:                  } @{$slotsarr};
                   8711:     }
                   8712:     return @sorted;
                   8713: }
                   8714: 
1.1040    raeburn  8715: =pod
                   8716: 
                   8717: =item * get_future_slots()
                   8718: 
                   8719: Inputs:
                   8720: 
                   8721: =over 4
                   8722: 
                   8723: cnum - course number
                   8724: 
                   8725: cdom - course domain
                   8726: 
                   8727: now - current UNIX time
                   8728: 
                   8729: symb - optional symb
                   8730: 
                   8731: =back
                   8732: 
                   8733: Returns:
                   8734: 
                   8735: =over 4
                   8736: 
                   8737: sorted_reservable - ref to array of student_schedulable slots currently 
                   8738:                     reservable, ordered by end date of reservation period.
                   8739: 
                   8740: reservable_now - ref to hash of student_schedulable slots currently
                   8741:                  reservable.
                   8742: 
                   8743:     Keys in inner hash are:
                   8744:     (a) symb: either blank or symb to which slot use is restricted.
                   8745:     (b) endreserve: end date of reservation period. 
                   8746: 
                   8747: sorted_future - ref to array of student_schedulable slots reservable in
                   8748:                 the future, ordered by start date of reservation period.
                   8749: 
                   8750: future_reservable - ref to hash of student_schedulable slots reservable
                   8751:                     in the future.
                   8752: 
                   8753:     Keys in inner hash are:
                   8754:     (a) symb: either blank or symb to which slot use is restricted.
                   8755:     (b) startreserve:  start date of reservation period.
                   8756: 
                   8757: =back
                   8758: 
                   8759: =cut
                   8760: 
                   8761: sub get_future_slots {
                   8762:     my ($cnum,$cdom,$now,$symb) = @_;
                   8763:     my (%reservable_now,%future_reservable,@sorted_reservable,@sorted_future);
                   8764:     my %slots = &Apache::lonnet::get_course_slots($cnum,$cdom);
                   8765:     foreach my $slot (keys(%slots)) {
                   8766:         next unless($slots{$slot}->{'type'} eq 'schedulable_student');
                   8767:         if ($symb) {
                   8768:             next if (($slots{$slot}->{'symb'} ne '') && 
                   8769:                      ($slots{$slot}->{'symb'} ne $symb));
                   8770:         }
                   8771:         if (($slots{$slot}->{'starttime'} > $now) &&
                   8772:             ($slots{$slot}->{'endtime'} > $now)) {
                   8773:             if (($slots{$slot}->{'allowedsections'}) || ($slots{$slot}->{'allowedusers'})) {
                   8774:                 my $userallowed = 0;
                   8775:                 if ($slots{$slot}->{'allowedsections'}) {
                   8776:                     my @allowed_sec = split(',',$slots{$slot}->{'allowedsections'});
                   8777:                     if (!defined($env{'request.role.sec'})
                   8778:                         && grep(/^No section assigned$/,@allowed_sec)) {
                   8779:                         $userallowed=1;
                   8780:                     } else {
                   8781:                         if (grep(/^\Q$env{'request.role.sec'}\E$/,@allowed_sec)) {
                   8782:                             $userallowed=1;
                   8783:                         }
                   8784:                     }
                   8785:                     unless ($userallowed) {
                   8786:                         if (defined($env{'request.course.groups'})) {
                   8787:                             my @groups = split(/:/,$env{'request.course.groups'});
                   8788:                             foreach my $group (@groups) {
                   8789:                                 if (grep(/^\Q$group\E$/,@allowed_sec)) {
                   8790:                                     $userallowed=1;
                   8791:                                     last;
                   8792:                                 }
                   8793:                             }
                   8794:                         }
                   8795:                     }
                   8796:                 }
                   8797:                 if ($slots{$slot}->{'allowedusers'}) {
                   8798:                     my @allowed_users = split(',',$slots{$slot}->{'allowedusers'});
                   8799:                     my $user = $env{'user.name'}.':'.$env{'user.domain'};
                   8800:                     if (grep(/^\Q$user\E$/,@allowed_users)) {
                   8801:                         $userallowed = 1;
                   8802:                     }
                   8803:                 }
                   8804:                 next unless($userallowed);
                   8805:             }
                   8806:             my $startreserve = $slots{$slot}->{'startreserve'};
                   8807:             my $endreserve = $slots{$slot}->{'endreserve'};
                   8808:             my $symb = $slots{$slot}->{'symb'};
                   8809:             if (($startreserve < $now) &&
                   8810:                 (!$endreserve || $endreserve > $now)) {
                   8811:                 my $lastres = $endreserve;
                   8812:                 if (!$lastres) {
                   8813:                     $lastres = $slots{$slot}->{'starttime'};
                   8814:                 }
                   8815:                 $reservable_now{$slot} = {
                   8816:                                            symb       => $symb,
                   8817:                                            endreserve => $lastres
                   8818:                                          };
                   8819:             } elsif (($startreserve > $now) &&
                   8820:                      (!$endreserve || $endreserve > $startreserve)) {
                   8821:                 $future_reservable{$slot} = {
                   8822:                                               symb         => $symb,
                   8823:                                               startreserve => $startreserve
                   8824:                                             };
                   8825:             }
                   8826:         }
                   8827:     }
                   8828:     my @unsorted_reservable = keys(%reservable_now);
                   8829:     if (@unsorted_reservable > 0) {
                   8830:         @sorted_reservable = 
                   8831:             &sorted_slots(\@unsorted_reservable,\%reservable_now,'endreserve');
                   8832:     }
                   8833:     my @unsorted_future = keys(%future_reservable);
                   8834:     if (@unsorted_future > 0) {
                   8835:         @sorted_future =
                   8836:             &sorted_slots(\@unsorted_future,\%future_reservable,'startreserve');
                   8837:     }
                   8838:     return (\@sorted_reservable,\%reservable_now,\@sorted_future,\%future_reservable);
                   8839: }
1.780     raeburn  8840: 
                   8841: =pod
                   8842: 
1.1057    foxr     8843: =back
                   8844: 
1.549     albertel 8845: =head1 HTTP Helpers
                   8846: 
                   8847: =over 4
                   8848: 
1.648     raeburn  8849: =item * &get_unprocessed_cgi($query,$possible_names)
1.112     bowersj2 8850: 
1.258     albertel 8851: Modify the %env hash to contain unprocessed CGI form parameters held in
1.112     bowersj2 8852: $query.  The parameters listed in $possible_names (an array reference),
1.258     albertel 8853: will be set in $env{'form.name'} if they do not already exist.
1.112     bowersj2 8854: 
                   8855: Typically called with $ENV{'QUERY_STRING'} as the first parameter.  
                   8856: $possible_names is an ref to an array of form element names.  As an example:
                   8857: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
1.258     albertel 8858: will result in $env{'form.uname'} and $env{'form.udom'} being set.
1.112     bowersj2 8859: 
                   8860: =cut
1.1       albertel 8861: 
1.6       albertel 8862: sub get_unprocessed_cgi {
1.25      albertel 8863:   my ($query,$possible_names)= @_;
1.26      matthew  8864:   # $Apache::lonxml::debug=1;
1.356     albertel 8865:   foreach my $pair (split(/&/,$query)) {
                   8866:     my ($name, $value) = split(/=/,$pair);
1.369     www      8867:     $name = &unescape($name);
1.25      albertel 8868:     if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
                   8869:       $value =~ tr/+/ /;
                   8870:       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.258     albertel 8871:       unless (defined($env{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
1.25      albertel 8872:     }
1.16      harris41 8873:   }
1.6       albertel 8874: }
                   8875: 
1.112     bowersj2 8876: =pod
                   8877: 
1.648     raeburn  8878: =item * &cacheheader() 
1.112     bowersj2 8879: 
                   8880: returns cache-controlling header code
                   8881: 
                   8882: =cut
                   8883: 
1.7       albertel 8884: sub cacheheader {
1.258     albertel 8885:     unless ($env{'request.method'} eq 'GET') { return ''; }
1.216     albertel 8886:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
                   8887:     my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
1.7       albertel 8888:                 <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
                   8889:                 <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
1.216     albertel 8890:     return $output;
1.7       albertel 8891: }
                   8892: 
1.112     bowersj2 8893: =pod
                   8894: 
1.648     raeburn  8895: =item * &no_cache($r) 
1.112     bowersj2 8896: 
                   8897: specifies header code to not have cache
                   8898: 
                   8899: =cut
                   8900: 
1.9       albertel 8901: sub no_cache {
1.216     albertel 8902:     my ($r) = @_;
                   8903:     if ($ENV{'REQUEST_METHOD'} ne 'GET' &&
1.258     albertel 8904: 	$env{'request.method'} ne 'GET') { return ''; }
1.216     albertel 8905:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time));
                   8906:     $r->no_cache(1);
                   8907:     $r->header_out("Expires" => $date);
                   8908:     $r->header_out("Pragma" => "no-cache");
1.123     www      8909: }
                   8910: 
                   8911: sub content_type {
1.181     albertel 8912:     my ($r,$type,$charset) = @_;
1.299     foxr     8913:     if ($r) {
                   8914: 	#  Note that printout.pl calls this with undef for $r.
                   8915: 	&no_cache($r);
                   8916:     }
1.258     albertel 8917:     if ($env{'browser.mathml'} && $type eq 'text/html') { $type='text/xml'; }
1.181     albertel 8918:     unless ($charset) {
                   8919: 	$charset=&Apache::lonlocal::current_encoding;
                   8920:     }
                   8921:     if ($charset) { $type.='; charset='.$charset; }
                   8922:     if ($r) {
                   8923: 	$r->content_type($type);
                   8924:     } else {
                   8925: 	print("Content-type: $type\n\n");
                   8926:     }
1.9       albertel 8927: }
1.25      albertel 8928: 
1.112     bowersj2 8929: =pod
                   8930: 
1.648     raeburn  8931: =item * &add_to_env($name,$value) 
1.112     bowersj2 8932: 
1.258     albertel 8933: adds $name to the %env hash with value
1.112     bowersj2 8934: $value, if $name already exists, the entry is converted to an array
                   8935: reference and $value is added to the array.
                   8936: 
                   8937: =cut
                   8938: 
1.25      albertel 8939: sub add_to_env {
                   8940:   my ($name,$value)=@_;
1.258     albertel 8941:   if (defined($env{$name})) {
                   8942:     if (ref($env{$name})) {
1.25      albertel 8943:       #already have multiple values
1.258     albertel 8944:       push(@{ $env{$name} },$value);
1.25      albertel 8945:     } else {
                   8946:       #first time seeing multiple values, convert hash entry to an arrayref
1.258     albertel 8947:       my $first=$env{$name};
                   8948:       undef($env{$name});
                   8949:       push(@{ $env{$name} },$first,$value);
1.25      albertel 8950:     }
                   8951:   } else {
1.258     albertel 8952:     $env{$name}=$value;
1.25      albertel 8953:   }
1.31      albertel 8954: }
1.149     albertel 8955: 
                   8956: =pod
                   8957: 
1.648     raeburn  8958: =item * &get_env_multiple($name) 
1.149     albertel 8959: 
1.258     albertel 8960: gets $name from the %env hash, it seemlessly handles the cases where multiple
1.149     albertel 8961: values may be defined and end up as an array ref.
                   8962: 
                   8963: returns an array of values
                   8964: 
                   8965: =cut
                   8966: 
                   8967: sub get_env_multiple {
                   8968:     my ($name) = @_;
                   8969:     my @values;
1.258     albertel 8970:     if (defined($env{$name})) {
1.149     albertel 8971:         # exists is it an array
1.258     albertel 8972:         if (ref($env{$name})) {
                   8973:             @values=@{ $env{$name} };
1.149     albertel 8974:         } else {
1.258     albertel 8975:             $values[0]=$env{$name};
1.149     albertel 8976:         }
                   8977:     }
                   8978:     return(@values);
                   8979: }
                   8980: 
1.660     raeburn  8981: sub ask_for_embedded_content {
                   8982:     my ($actionurl,$state,$allfiles,$codebase,$args)=@_;
1.987     raeburn  8983:     my (%subdependencies,%dependencies,%mapping,%existing,%newfiles,%pathchanges);
1.660     raeburn  8984:     my $num = 0;
1.987     raeburn  8985:     my $numremref = 0;
                   8986:     my $numinvalid = 0;
                   8987:     my $numpathchg = 0;
                   8988:     my $numexisting = 0;
                   8989:     my ($output,$upload_output,$toplevel,$url,$udom,$uname,$getpropath);
1.984     raeburn  8990:     if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
                   8991:         my $current_path='/';
                   8992:         if ($env{'form.currentpath'}) {
                   8993:             $current_path = $env{'form.currentpath'};
                   8994:         }
                   8995:         if ($actionurl eq '/adm/coursegrp_portfolio') {
                   8996:             $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8997:             $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
                   8998:             $url = '/userfiles/groups/'.$env{'form.group'}.'/portfolio';
                   8999:         } else {
                   9000:             $udom = $env{'user.domain'};
                   9001:             $uname = $env{'user.name'};
                   9002:             $url = '/userfiles/portfolio';
                   9003:         }
1.987     raeburn  9004:         $toplevel = $url.'/';
1.984     raeburn  9005:         $url .= $current_path;
                   9006:         $getpropath = 1;
1.987     raeburn  9007:     } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank') ||
                   9008:              ($actionurl eq '/adm/imsimport')) { 
1.1022    www      9009:         my ($udom,$uname,$rest) = ($args->{'current_path'} =~ m{/priv/($match_domain)/($match_username)/?(.*)$});
1.1026    raeburn  9010:         $url = $Apache::lonnet::perlvar{'lonDocRoot'}."/priv/$udom/$uname/";
1.987     raeburn  9011:         $toplevel = $url;
1.984     raeburn  9012:         if ($rest ne '') {
1.987     raeburn  9013:             $url .= $rest;
                   9014:         }
                   9015:     } elsif ($actionurl eq '/adm/coursedocs') {
                   9016:         if (ref($args) eq 'HASH') {
                   9017:            $url = $args->{'docs_url'};
                   9018:            $toplevel = $url;
                   9019:         }
                   9020:     }
                   9021:     my $now = time();
                   9022:     foreach my $embed_file (keys(%{$allfiles})) {
                   9023:         my $absolutepath;
                   9024:         if ($embed_file =~ m{^\w+://}) {
                   9025:             $newfiles{$embed_file} = 1;
                   9026:             $mapping{$embed_file} = $embed_file;
                   9027:         } else {
                   9028:             if ($embed_file =~ m{^/}) {
                   9029:                 $absolutepath = $embed_file;
                   9030:                 $embed_file =~ s{^(/+)}{};
                   9031:             }
                   9032:             if ($embed_file =~ m{/}) {
                   9033:                 my ($path,$fname) = ($embed_file =~ m{^(.+)/([^/]*)$});
                   9034:                 $path = &check_for_traversal($path,$url,$toplevel);
                   9035:                 my $item = $fname;
                   9036:                 if ($path ne '') {
                   9037:                     $item = $path.'/'.$fname;
                   9038:                     $subdependencies{$path}{$fname} = 1;
                   9039:                 } else {
                   9040:                     $dependencies{$item} = 1;
                   9041:                 }
                   9042:                 if ($absolutepath) {
                   9043:                     $mapping{$item} = $absolutepath;
                   9044:                 } else {
                   9045:                     $mapping{$item} = $embed_file;
                   9046:                 }
                   9047:             } else {
                   9048:                 $dependencies{$embed_file} = 1;
                   9049:                 if ($absolutepath) {
                   9050:                     $mapping{$embed_file} = $absolutepath;
                   9051:                 } else {
                   9052:                     $mapping{$embed_file} = $embed_file;
                   9053:                 }
                   9054:             }
1.984     raeburn  9055:         }
                   9056:     }
                   9057:     foreach my $path (keys(%subdependencies)) {
                   9058:         my %currsubfile;
                   9059:         if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) { 
1.1021    raeburn  9060:             my ($sublistref,$listerror) =
                   9061:                 &Apache::lonnet::dirlist($url.$path,$udom,$uname,$getpropath);
                   9062:             if (ref($sublistref) eq 'ARRAY') {
                   9063:                 foreach my $line (@{$sublistref}) {
                   9064:                     my ($file_name,$rest) = split(/\&/,$line,2);
                   9065:                     $currsubfile{$file_name} = 1;
                   9066:                 }
1.984     raeburn  9067:             }
1.987     raeburn  9068:         } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
1.984     raeburn  9069:             if (opendir(my $dir,$url.'/'.$path)) {
                   9070:                 my @subdir_list = grep(!/^\./,readdir($dir));
                   9071:                 map {$currsubfile{$_} = 1;} @subdir_list;
                   9072:             }
                   9073:         }
                   9074:         foreach my $file (keys(%{$subdependencies{$path}})) {
1.987     raeburn  9075:             if ($currsubfile{$file}) {
                   9076:                 my $item = $path.'/'.$file;
                   9077:                 unless ($mapping{$item} eq $item) {
                   9078:                     $pathchanges{$item} = 1;
                   9079:                 }
                   9080:                 $existing{$item} = 1;
                   9081:                 $numexisting ++;
                   9082:             } else {
                   9083:                 $newfiles{$path.'/'.$file} = 1;
1.984     raeburn  9084:             }
                   9085:         }
                   9086:     }
1.987     raeburn  9087:     my %currfile;
1.984     raeburn  9088:     if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
1.1021    raeburn  9089:         my ($dirlistref,$listerror) =
                   9090:             &Apache::lonnet::dirlist($url,$udom,$uname,$getpropath);
                   9091:         if (ref($dirlistref) eq 'ARRAY') {
                   9092:             foreach my $line (@{$dirlistref}) {
                   9093:                 my ($file_name,$rest) = split(/\&/,$line,2);
                   9094:                 $currfile{$file_name} = 1;
                   9095:             }
1.984     raeburn  9096:         }
1.987     raeburn  9097:     } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
1.984     raeburn  9098:         if (opendir(my $dir,$url)) {
1.987     raeburn  9099:             my @dir_list = grep(!/^\./,readdir($dir));
1.984     raeburn  9100:             map {$currfile{$_} = 1;} @dir_list;
                   9101:         }
                   9102:     }
                   9103:     foreach my $file (keys(%dependencies)) {
1.987     raeburn  9104:         if ($currfile{$file}) {
                   9105:             unless ($mapping{$file} eq $file) {
                   9106:                 $pathchanges{$file} = 1;
                   9107:             }
                   9108:             $existing{$file} = 1;
                   9109:             $numexisting ++;
                   9110:         } else {
1.984     raeburn  9111:             $newfiles{$file} = 1;
                   9112:         }
                   9113:     }
                   9114:     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%newfiles)) {
1.660     raeburn  9115:         $upload_output .= &start_data_table_row().
1.987     raeburn  9116:                           '<td><span class="LC_filename">'.$embed_file.'</span>';
                   9117:         unless ($mapping{$embed_file} eq $embed_file) {
                   9118:             $upload_output .= '<br /><span class="LC_info" style="font-size:smaller;">'.&mt('changed from: [_1]',$mapping{$embed_file}).'</span>';
                   9119:         }
                   9120:         $upload_output .= '</td><td>';
1.660     raeburn  9121:         if ($args->{'ignore_remote_references'}
                   9122:             && $embed_file =~ m{^\w+://}) {
                   9123:             $upload_output.='<span class="LC_warning">'.&mt("URL points to other server.").'</span>';
1.987     raeburn  9124:             $numremref++;
1.660     raeburn  9125:         } elsif ($args->{'error_on_invalid_names'}
                   9126:             && $embed_file ne &Apache::lonnet::clean_filename($embed_file,{'keep_path' => 1,})) {
                   9127: 
1.987     raeburn  9128:             $upload_output.='<span class="LC_warning">'.&mt('Invalid characters').'</span>';
                   9129:             $numinvalid++;
1.660     raeburn  9130:         } else {
1.987     raeburn  9131:             $upload_output .= &embedded_file_element('upload_embedded',$num,
                   9132:                                                      $embed_file,\%mapping,
                   9133:                                                      $allfiles,$codebase);
                   9134:             $num++;
                   9135:         }
                   9136:         $upload_output .= '</td>'.&Apache::loncommon::end_data_table_row()."\n";
                   9137:     }
                   9138:     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%existing)) {
                   9139:         $upload_output .= &start_data_table_row().
                   9140:                           '<td><span class="LC_filename">'.$embed_file.'</span></td>'.
                   9141:                           '<td><span class="LC_warning">'.&mt('Already exists').'</span></td>'.
                   9142:                           &Apache::loncommon::end_data_table_row()."\n";
                   9143:     }
                   9144:     if ($upload_output) {
                   9145:         $upload_output = &start_data_table().
                   9146:                          $upload_output.
                   9147:                          &end_data_table()."\n";
                   9148:     }
                   9149:     my $applies = 0;
                   9150:     if ($numremref) {
                   9151:         $applies ++;
                   9152:     }
                   9153:     if ($numinvalid) {
                   9154:         $applies ++;
                   9155:     }
                   9156:     if ($numexisting) {
                   9157:         $applies ++;
                   9158:     }
                   9159:     if ($num) {
                   9160:         $output = '<form name="upload_embedded" action="'.$actionurl.'"'.
                   9161:                   ' method="post" enctype="multipart/form-data">'."\n".
                   9162:                   $state.
                   9163:                   '<h3>'.&mt('Upload embedded files').
                   9164:                   ':</h3>'.$upload_output.'<br />'."\n".
                   9165:                   '<input type ="hidden" name="number_embedded_items" value="'.
                   9166:                   $num.'" />'."\n";
                   9167:         if ($actionurl eq '') {
                   9168:             $output .=  '<input type="hidden" name="phase" value="three" />';
                   9169:         }
                   9170:     } elsif ($applies) {
                   9171:         $output = '<b>'.&mt('Referenced files').'</b>:<br />';
                   9172:         if ($applies > 1) {
                   9173:             $output .=  
                   9174:                 &mt('No files need to be uploaded, as one of the following applies to each reference:').'<ul>';
                   9175:             if ($numremref) {
                   9176:                 $output .= '<li>'.&mt('reference is to a URL which points to another server').'</li>'."\n";
                   9177:             }
                   9178:             if ($numinvalid) {
                   9179:                 $output .= '<li>'.&mt('reference is to file with a name containing invalid characters').'</li>'."\n";
                   9180:             }
                   9181:             if ($numexisting) {
                   9182:                 $output .= '<li>'.&mt('reference is to an existing file at the specified location').'</li>'."\n";
                   9183:             }
                   9184:             $output .= '</ul><br />';
                   9185:         } elsif ($numremref) {
                   9186:             $output .= '<p>'.&mt('None to upload, as all references are to URLs pointing to another server.').'</p>';
                   9187:         } elsif ($numinvalid) {
                   9188:             $output .= '<p>'.&mt('None to upload, as all references are to files with names containing invalid characters.').'</p>';
                   9189:         } elsif ($numexisting) {
                   9190:             $output .= '<p>'.&mt('None to upload, as all references are to existing files.').'</p>';
                   9191:         }
                   9192:         $output .= $upload_output.'<br />';
                   9193:     }
                   9194:     my ($pathchange_output,$chgcount);
                   9195:     $chgcount = $num;
                   9196:     if (keys(%pathchanges) > 0) {
                   9197:         foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%pathchanges)) {
                   9198:             if ($num) {
                   9199:                 $output .= &embedded_file_element('pathchange',$chgcount,
                   9200:                                                   $embed_file,\%mapping,
                   9201:                                                   $allfiles,$codebase);
                   9202:             } else {
                   9203:                 $pathchange_output .= 
                   9204:                     &start_data_table_row().
                   9205:                     '<td><input type ="checkbox" name="namechange" value="'.
                   9206:                     $chgcount.'" checked="checked" /></td>'.
                   9207:                     '<td>'.$mapping{$embed_file}.'</td>'.
                   9208:                     '<td>'.$embed_file.
                   9209:                     &embedded_file_element('pathchange',$numpathchg,$embed_file,
                   9210:                                            \%mapping,$allfiles,$codebase).
                   9211:                     '</td>'.&end_data_table_row();
1.660     raeburn  9212:             }
1.987     raeburn  9213:             $numpathchg ++;
                   9214:             $chgcount ++;
1.660     raeburn  9215:         }
                   9216:     }
1.984     raeburn  9217:     if ($num) {
1.987     raeburn  9218:         if ($numpathchg) {
                   9219:             $output .= '<input type ="hidden" name="number_pathchange_items" value="'.
                   9220:                        $numpathchg.'" />'."\n";
                   9221:         }
                   9222:         if (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank') || 
                   9223:             ($actionurl eq '/adm/imsimport')) {
                   9224:             $output .= '<input type="hidden" name="phase" value="three" />'."\n";
                   9225:         } elsif ($actionurl eq '/adm/portfolio' || $actionurl eq '/adm/coursegrp_portfolio') {
                   9226:             $output .= '<input type="hidden" name="action" value="upload_embedded" />';
                   9227:         }
                   9228:         $output .=  '<input type ="submit" value="'.&mt('Upload Listed Files').'" />'."\n".
                   9229:                     &mt('(only files for which a location has been provided will be uploaded)').'</form>'."\n";
                   9230:     } elsif ($numpathchg) {
                   9231:         my %pathchange = ();
                   9232:         $output .= &modify_html_form('pathchange',$actionurl,$state,\%pathchange,$pathchange_output);
                   9233:         if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
                   9234:             $output .= '<p>'.&mt('or').'</p>'; 
                   9235:         } 
                   9236:     }
                   9237:     return ($output,$num,$numpathchg);
                   9238: }
                   9239: 
                   9240: sub embedded_file_element {
                   9241:     my ($context,$num,$embed_file,$mapping,$allfiles,$codebase) = @_;
                   9242:     return unless ((ref($mapping) eq 'HASH') && (ref($allfiles) eq 'HASH') &&
                   9243:                    (ref($codebase) eq 'HASH'));
                   9244:     my $output;
                   9245:     if ($context eq 'upload_embedded') {
                   9246:        $output = '<input name="embedded_item_'.$num.'" type="file" value="" />'."\n";
                   9247:     }
                   9248:     $output .= '<input name="embedded_orig_'.$num.'" type="hidden" value="'.
                   9249:                &escape($embed_file).'" />';
                   9250:     unless (($context eq 'upload_embedded') && 
                   9251:             ($mapping->{$embed_file} eq $embed_file)) {
                   9252:         $output .='
                   9253:         <input name="embedded_ref_'.$num.'" type="hidden" value="'.&escape($mapping->{$embed_file}).'" />';
                   9254:     }
                   9255:     my $attrib;
                   9256:     if (ref($allfiles->{$mapping->{$embed_file}}) eq 'ARRAY') {
                   9257:         $attrib = &escape(join(':',@{$allfiles->{$mapping->{$embed_file}}}));
                   9258:     }
                   9259:     $output .=
                   9260:         "\n\t\t".
                   9261:         '<input name="embedded_attrib_'.$num.'" type="hidden" value="'.
                   9262:         $attrib.'" />';
                   9263:     if (exists($codebase->{$mapping->{$embed_file}})) {
                   9264:         $output .=
                   9265:             "\n\t\t".
                   9266:             '<input name="codebase_'.$num.'" type="hidden" value="'.
                   9267:             &escape($codebase->{$mapping->{$embed_file}}).'" />';
1.984     raeburn  9268:     }
1.987     raeburn  9269:     return $output;
1.660     raeburn  9270: }
                   9271: 
1.661     raeburn  9272: sub upload_embedded {
                   9273:     my ($context,$dirpath,$uname,$udom,$dir_root,$url_root,$group,$disk_quota,
1.987     raeburn  9274:         $current_disk_usage,$hiddenstate,$actionurl) = @_;
                   9275:     my (%pathchange,$output,$modifyform,$footer,$returnflag);
1.661     raeburn  9276:     for (my $i=0; $i<$env{'form.number_embedded_items'}; $i++) {
                   9277:         next if (!exists($env{'form.embedded_item_'.$i.'.filename'}));
                   9278:         my $orig_uploaded_filename =
                   9279:             $env{'form.embedded_item_'.$i.'.filename'};
1.987     raeburn  9280:         foreach my $type ('orig','ref','attrib','codebase') {
                   9281:             if ($env{'form.embedded_'.$type.'_'.$i} ne '') {
                   9282:                 $env{'form.embedded_'.$type.'_'.$i} =
                   9283:                     &unescape($env{'form.embedded_'.$type.'_'.$i});
                   9284:             }
                   9285:         }
1.661     raeburn  9286:         my ($path,$fname) =
                   9287:             ($env{'form.embedded_orig_'.$i} =~ m{(.*/)([^/]*)});
                   9288:         # no path, whole string is fname
                   9289:         if (!$fname) { $fname = $env{'form.embedded_orig_'.$i} };
                   9290:         $fname = &Apache::lonnet::clean_filename($fname);
                   9291:         # See if there is anything left
                   9292:         next if ($fname eq '');
                   9293: 
                   9294:         # Check if file already exists as a file or directory.
                   9295:         my ($state,$msg);
                   9296:         if ($context eq 'portfolio') {
                   9297:             my $port_path = $dirpath;
                   9298:             if ($group ne '') {
                   9299:                 $port_path = "groups/$group/$port_path";
                   9300:             }
1.987     raeburn  9301:             ($state,$msg) = &check_for_upload($env{'form.currentpath'}.$path,
                   9302:                                               $fname,$group,'embedded_item_'.$i,
1.661     raeburn  9303:                                               $dir_root,$port_path,$disk_quota,
                   9304:                                               $current_disk_usage,$uname,$udom);
                   9305:             if ($state eq 'will_exceed_quota'
1.984     raeburn  9306:                 || $state eq 'file_locked') {
1.661     raeburn  9307:                 $output .= $msg;
                   9308:                 next;
                   9309:             }
                   9310:         } elsif (($context eq 'author') || ($context eq 'testbank')) {
                   9311:             ($state,$msg) = &check_for_existing($path,$fname,'embedded_item_'.$i);
                   9312:             if ($state eq 'exists') {
                   9313:                 $output .= $msg;
                   9314:                 next;
                   9315:             }
                   9316:         }
                   9317:         # Check if extension is valid
                   9318:         if (($fname =~ /\.(\w+)$/) &&
                   9319:             (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.987     raeburn  9320:             $output .= &mt('Invalid file extension ([_1]) - reserved for LONCAPA use - rename the file with a different extension and re-upload. ',$1).'<br />';
1.661     raeburn  9321:             next;
                   9322:         } elsif (($fname =~ /\.(\w+)$/) &&
                   9323:                  (!defined(&Apache::loncommon::fileembstyle($1)))) {
1.987     raeburn  9324:             $output .= &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1).'<br />';
1.661     raeburn  9325:             next;
                   9326:         } elsif ($fname=~/\.(\d+)\.(\w+)$/) {
1.987     raeburn  9327:             $output .= &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2).'<br />';
1.661     raeburn  9328:             next;
                   9329:         }
                   9330: 
                   9331:         $env{'form.embedded_item_'.$i.'.filename'}=$fname;
                   9332:         if ($context eq 'portfolio') {
1.984     raeburn  9333:             my $result;
                   9334:             if ($state eq 'existingfile') {
                   9335:                 $result=
                   9336:                     &Apache::lonnet::userfileupload('embedded_item_'.$i,'existingfile',
1.987     raeburn  9337:                                                     $dirpath.$env{'form.currentpath'}.$path);
1.661     raeburn  9338:             } else {
1.984     raeburn  9339:                 $result=
                   9340:                     &Apache::lonnet::userfileupload('embedded_item_'.$i,'',
1.987     raeburn  9341:                                                     $dirpath.
                   9342:                                                     $env{'form.currentpath'}.$path);
1.984     raeburn  9343:                 if ($result !~ m|^/uploaded/|) {
                   9344:                     $output .= '<span class="LC_error">'
                   9345:                                .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
                   9346:                                ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
                   9347:                                .'</span><br />';
                   9348:                     next;
                   9349:                 } else {
1.987     raeburn  9350:                     $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                   9351:                                $path.$fname.'</span>').'<br />';     
1.984     raeburn  9352:                 }
1.661     raeburn  9353:             }
1.987     raeburn  9354:         } elsif ($context eq 'coursedoc') {
                   9355:             my $result =
                   9356:                 &Apache::lonnet::userfileupload('embedded_item_'.$i,'coursedoc',
                   9357:                                                 $dirpath.'/'.$path);
                   9358:             if ($result !~ m|^/uploaded/|) {
                   9359:                 $output .= '<span class="LC_error">'
                   9360:                            .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
                   9361:                            ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
                   9362:                            .'</span><br />';
                   9363:                     next;
                   9364:             } else {
                   9365:                 $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                   9366:                            $path.$fname.'</span>').'<br />';
                   9367:             }
1.661     raeburn  9368:         } else {
                   9369: # Save the file
                   9370:             my $target = $env{'form.embedded_item_'.$i};
                   9371:             my $fullpath = $dir_root.$dirpath.'/'.$path;
                   9372:             my $dest = $fullpath.$fname;
                   9373:             my $url = $url_root.$dirpath.'/'.$path.$fname;
1.1027    raeburn  9374:             my @parts=split(/\//,"$dirpath/$path");
1.661     raeburn  9375:             my $count;
                   9376:             my $filepath = $dir_root;
1.1027    raeburn  9377:             foreach my $subdir (@parts) {
                   9378:                 $filepath .= "/$subdir";
                   9379:                 if (!-e $filepath) {
1.661     raeburn  9380:                     mkdir($filepath,0770);
                   9381:                 }
                   9382:             }
                   9383:             my $fh;
                   9384:             if (!open($fh,'>'.$dest)) {
                   9385:                 &Apache::lonnet::logthis('Failed to create '.$dest);
                   9386:                 $output .= '<span class="LC_error">'.
                   9387:                            &mt('An error occurred while trying to upload [_1] for embedded element [_2].',$orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
                   9388:                            '</span><br />';
                   9389:             } else {
                   9390:                 if (!print $fh $env{'form.embedded_item_'.$i}) {
                   9391:                     &Apache::lonnet::logthis('Failed to write to '.$dest);
                   9392:                     $output .= '<span class="LC_error">'.
                   9393:                               &mt('An error occurred while writing the file [_1] for embedded element [_2].',$orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
                   9394:                               '</span><br />';
                   9395:                 } else {
1.987     raeburn  9396:                     $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                   9397:                                $url.'</span>').'<br />';
                   9398:                     unless ($context eq 'testbank') {
                   9399:                         $footer .= &mt('View embedded file: [_1]',
                   9400:                                        '<a href="'.$url.'">'.$fname.'</a>').'<br />';
                   9401:                     }
                   9402:                 }
                   9403:                 close($fh);
                   9404:             }
                   9405:         }
                   9406:         if ($env{'form.embedded_ref_'.$i}) {
                   9407:             $pathchange{$i} = 1;
                   9408:         }
                   9409:     }
                   9410:     if ($output) {
                   9411:         $output = '<p>'.$output.'</p>';
                   9412:     }
                   9413:     $output .= &modify_html_form('upload_embedded',$actionurl,$hiddenstate,\%pathchange);
                   9414:     $returnflag = 'ok';
                   9415:     if (keys(%pathchange) > 0) {
                   9416:         if ($context eq 'portfolio') {
                   9417:             $output .= '<p>'.&mt('or').'</p>';
                   9418:         } elsif ($context eq 'testbank') {
1.988     raeburn  9419:             $output .=  '<p>'.&mt('Or [_1]continue[_2] the testbank import without modifying the reference(s).','<a href="javascript:document.testbankForm.submit();">','</a>').'</p>';
1.987     raeburn  9420:             $returnflag = 'modify_orightml';
                   9421:         }
                   9422:     }
                   9423:     return ($output.$footer,$returnflag);
                   9424: }
                   9425: 
                   9426: sub modify_html_form {
                   9427:     my ($context,$actionurl,$hiddenstate,$pathchange,$pathchgtable) = @_;
                   9428:     my $end = 0;
                   9429:     my $modifyform;
                   9430:     if ($context eq 'upload_embedded') {
                   9431:         return unless (ref($pathchange) eq 'HASH');
                   9432:         if ($env{'form.number_embedded_items'}) {
                   9433:             $end += $env{'form.number_embedded_items'};
                   9434:         }
                   9435:         if ($env{'form.number_pathchange_items'}) {
                   9436:             $end += $env{'form.number_pathchange_items'};
                   9437:         }
                   9438:         if ($end) {
                   9439:             for (my $i=0; $i<$end; $i++) {
                   9440:                 if ($i < $env{'form.number_embedded_items'}) {
                   9441:                     next unless($pathchange->{$i});
                   9442:                 }
                   9443:                 $modifyform .=
                   9444:                     &start_data_table_row().
                   9445:                     '<td><input type ="checkbox" name="namechange" value="'.$i.'" '.
                   9446:                     'checked="checked" /></td>'.
                   9447:                     '<td>'.$env{'form.embedded_ref_'.$i}.
                   9448:                     '<input type="hidden" name="embedded_ref_'.$i.'" value="'.
                   9449:                     &escape($env{'form.embedded_ref_'.$i}).'" />'.
                   9450:                     '<input type="hidden" name="embedded_codebase_'.$i.'" value="'.
                   9451:                     &escape($env{'form.embedded_codebase_'.$i}).'" />'.
                   9452:                     '<input type="hidden" name="embedded_attrib_'.$i.'" value="'.
                   9453:                     &escape($env{'form.embedded_attrib_'.$i}).'" /></td>'.
                   9454:                     '<td>'.$env{'form.embedded_orig_'.$i}.
                   9455:                     '<input type="hidden" name="embedded_orig_'.$i.'" value="'.
                   9456:                     &escape($env{'form.embedded_orig_'.$i}).'" /></td>'.
                   9457:                     &end_data_table_row();
                   9458:             } 
                   9459:         }
                   9460:     } else {
                   9461:         $modifyform = $pathchgtable;
                   9462:         if (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
                   9463:             $hiddenstate .= '<input type="hidden" name="phase" value="four" />';
                   9464:         } elsif (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
                   9465:             $hiddenstate .= '<input type="hidden" name="action" value="modify_orightml" />';
                   9466:         }
                   9467:     }
                   9468:     if ($modifyform) {
                   9469:         return '<h3>'.&mt('Changes in content of HTML file required').'</h3>'."\n".
                   9470:                '<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".
                   9471:                '<li>'.&mt('For consistency between the reference(s) and the location of the corresponding stored file within LON-CAPA.').'</li>'."\n".
                   9472:                '<li>'.&mt('To change absolute paths to relative paths, or replace directory traversal via "../" within the original reference.').'</li>'."\n".
                   9473:                '</ol></p>'."\n".'<p>'.
                   9474:                &mt('LON-CAPA can make the required changes to your HTML file.').'</p>'."\n".
                   9475:                '<form method="post" name="refchanger" action="'.$actionurl.'">'.
                   9476:                &start_data_table()."\n".
                   9477:                &start_data_table_header_row().
                   9478:                '<th>'.&mt('Change?').'</th>'.
                   9479:                '<th>'.&mt('Current reference').'</th>'.
                   9480:                '<th>'.&mt('Required reference').'</th>'.
                   9481:                &end_data_table_header_row()."\n".
                   9482:                $modifyform.
                   9483:                &end_data_table().'<br />'."\n".$hiddenstate.
                   9484:                '<input type="submit" name="pathchanges" value="'.&mt('Modify HTML file').'" />'.
                   9485:                '</form>'."\n";
                   9486:     }
                   9487:     return;
                   9488: }
                   9489: 
                   9490: sub modify_html_refs {
                   9491:     my ($context,$dirpath,$uname,$udom,$dir_root) = @_;
                   9492:     my $container;
                   9493:     if ($context eq 'portfolio') {
                   9494:         $container = $env{'form.container'};
                   9495:     } elsif ($context eq 'coursedoc') {
                   9496:         $container = $env{'form.primaryurl'};
                   9497:     } else {
1.1027    raeburn  9498:         $container = $Apache::lonnet::perlvar{'lonDocRoot'}.$env{'form.filename'};
1.987     raeburn  9499:     }
                   9500:     my (%allfiles,%codebase,$output,$content);
                   9501:     my @changes = &get_env_multiple('form.namechange');
                   9502:     return unless (@changes > 0);
                   9503:     if (($context eq 'portfolio') || ($context eq 'coursedoc')) {
                   9504:         return unless ($container =~ m{^/uploaded/\Q$udom\E/\Q$uname\E/});
                   9505:         $content = &Apache::lonnet::getfile($container);
                   9506:         return if ($content eq '-1');
                   9507:     } else {
                   9508:         return unless ($container =~ /^\Q$dir_root\E/); 
                   9509:         if (open(my $fh,"<$container")) {
                   9510:             $content = join('', <$fh>);
                   9511:             close($fh);
                   9512:         } else {
                   9513:             return;
                   9514:         }
                   9515:     }
                   9516:     my ($count,$codebasecount) = (0,0);
                   9517:     my $mm = new File::MMagic;
                   9518:     my $mime_type = $mm->checktype_contents($content);
                   9519:     if ($mime_type eq 'text/html') {
                   9520:         my $parse_result = 
                   9521:             &Apache::lonnet::extract_embedded_items($container,\%allfiles,
                   9522:                                                     \%codebase,\$content);
                   9523:         if ($parse_result eq 'ok') {
                   9524:             foreach my $i (@changes) {
                   9525:                 my $orig = &unescape($env{'form.embedded_orig_'.$i});
                   9526:                 my $ref = &unescape($env{'form.embedded_ref_'.$i});
                   9527:                 if ($allfiles{$ref}) {
                   9528:                     my $newname =  $orig;
                   9529:                     my ($attrib_regexp,$codebase);
1.1006    raeburn  9530:                     $attrib_regexp = &unescape($env{'form.embedded_attrib_'.$i});
1.987     raeburn  9531:                     if ($attrib_regexp =~ /:/) {
                   9532:                         $attrib_regexp =~ s/\:/|/g;
                   9533:                     }
                   9534:                     if ($content =~ m{($attrib_regexp\s*=\s*['"]?)\Q$ref\E(['"]?)}) {
                   9535:                         my $numchg = ($content =~ s{($attrib_regexp\s*=\s*['"]?)\Q$ref\E(['"]?)}{$1$newname$2}gi);
                   9536:                         $count += $numchg;
                   9537:                     }
                   9538:                     if ($env{'form.embedded_codebase_'.$i} ne '') {
1.1006    raeburn  9539:                         $codebase = &unescape($env{'form.embedded_codebase_'.$i});
1.987     raeburn  9540:                         my $numchg = ($content =~ s/(codebase\s*=\s*["']?)\Q$codebase\E(["']?)/$1.$2/i); #' stupid emacs
                   9541:                         $codebasecount ++;
                   9542:                     }
                   9543:                 }
                   9544:             }
                   9545:             if ($count || $codebasecount) {
                   9546:                 my $saveresult;
                   9547:                 if ($context eq 'portfolio' || $context eq 'coursedoc') {
                   9548:                     my $url = &Apache::lonnet::store_edited_file($container,$content,$udom,$uname,\$saveresult);
                   9549:                     if ($url eq $container) {
                   9550:                         my ($fname) = ($container =~ m{/([^/]+)$});
                   9551:                         $output = '<p>'.&mt('Updated [quant,_1,reference] in [_2].',
                   9552:                                             $count,'<span class="LC_filename">'.
                   9553:                                             $fname.'</span>').'</p>'; 
                   9554:                     } else {
                   9555:                          $output = '<p class="LC_error">'.
                   9556:                                    &mt('Error: update failed for: [_1].',
                   9557:                                    '<span class="LC_filename">'.
                   9558:                                    $container.'</span>').'</p>';
                   9559:                     }
                   9560:                 } else {
                   9561:                     if (open(my $fh,">$container")) {
                   9562:                         print $fh $content;
                   9563:                         close($fh);
                   9564:                         $output = '<p>'.&mt('Updated [quant,_1,reference] in [_2].',
                   9565:                                   $count,'<span class="LC_filename">'.
                   9566:                                   $container.'</span>').'</p>';
1.661     raeburn  9567:                     } else {
1.987     raeburn  9568:                          $output = '<p class="LC_error">'.
                   9569:                                    &mt('Error: could not update [_1].',
                   9570:                                    '<span class="LC_filename">'.
                   9571:                                    $container.'</span>').'</p>';
1.661     raeburn  9572:                     }
                   9573:                 }
                   9574:             }
1.987     raeburn  9575:         } else {
                   9576:             &logthis('Failed to parse '.$container.
                   9577:                      ' to modify references: '.$parse_result);
1.661     raeburn  9578:         }
                   9579:     }
                   9580:     return $output;
                   9581: }
                   9582: 
                   9583: sub check_for_existing {
                   9584:     my ($path,$fname,$element) = @_;
                   9585:     my ($state,$msg);
                   9586:     if (-d $path.'/'.$fname) {
                   9587:         $state = 'exists';
                   9588:         $msg = &mt('Unable to upload [_1]. A directory by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
                   9589:     } elsif (-e $path.'/'.$fname) {
                   9590:         $state = 'exists';
                   9591:         $msg = &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
                   9592:     }
                   9593:     if ($state eq 'exists') {
                   9594:         $msg = '<span class="LC_error">'.$msg.'</span><br />';
                   9595:     }
                   9596:     return ($state,$msg);
                   9597: }
                   9598: 
                   9599: sub check_for_upload {
                   9600:     my ($path,$fname,$group,$element,$portfolio_root,$port_path,
                   9601:         $disk_quota,$current_disk_usage,$uname,$udom) = @_;
1.985     raeburn  9602:     my $filesize = length($env{'form.'.$element});
                   9603:     if (!$filesize) {
                   9604:         my $msg = '<span class="LC_error">'.
                   9605:                   &mt('Unable to upload [_1]. (size = [_2] bytes)', 
                   9606:                       '<span class="LC_filename">'.$fname.'</span>',
                   9607:                       $filesize).'<br />'.
1.1007    raeburn  9608:                   &mt('Either the file you attempted to upload was empty, or your web browser was unable to read its contents.').'<br />'.
1.985     raeburn  9609:                   '</span>';
                   9610:         return ('zero_bytes',$msg);
                   9611:     }
                   9612:     $filesize =  $filesize/1000; #express in k (1024?)
1.661     raeburn  9613:     my $getpropath = 1;
1.1021    raeburn  9614:     my ($dirlistref,$listerror) =
                   9615:          &Apache::lonnet::dirlist($portfolio_root.$path,$udom,$uname,$getpropath);
1.661     raeburn  9616:     my $found_file = 0;
                   9617:     my $locked_file = 0;
1.991     raeburn  9618:     my @lockers;
                   9619:     my $navmap;
                   9620:     if ($env{'request.course.id'}) {
                   9621:         $navmap = Apache::lonnavmaps::navmap->new();
                   9622:     }
1.1021    raeburn  9623:     if (ref($dirlistref) eq 'ARRAY') {
                   9624:         foreach my $line (@{$dirlistref}) {
                   9625:             my ($file_name,$rest)=split(/\&/,$line,2);
                   9626:             if ($file_name eq $fname){
                   9627:                 $file_name = $path.$file_name;
                   9628:                 if ($group ne '') {
                   9629:                     $file_name = $group.$file_name;
                   9630:                 }
                   9631:                 $found_file = 1;
                   9632:                 if (&Apache::lonnet::is_locked($file_name,$udom,$uname,\@lockers) eq 'true') {
                   9633:                     foreach my $lock (@lockers) {
                   9634:                         if (ref($lock) eq 'ARRAY') {
                   9635:                             my ($symb,$crsid) = @{$lock};
                   9636:                             if ($crsid eq $env{'request.course.id'}) {
                   9637:                                 if (ref($navmap)) {
                   9638:                                     my $res = $navmap->getBySymb($symb);
                   9639:                                     foreach my $part (@{$res->parts()}) { 
                   9640:                                         my ($slot_status,$slot_time,$slot_name)=$res->check_for_slot($part);
                   9641:                                         unless (($slot_status == $res->RESERVED) ||
                   9642:                                                 ($slot_status == $res->RESERVED_LOCATION)) {
                   9643:                                             $locked_file = 1;
                   9644:                                         }
1.991     raeburn  9645:                                     }
1.1021    raeburn  9646:                                 } else {
                   9647:                                     $locked_file = 1;
1.991     raeburn  9648:                                 }
                   9649:                             } else {
                   9650:                                 $locked_file = 1;
                   9651:                             }
                   9652:                         }
1.1021    raeburn  9653:                    }
                   9654:                 } else {
                   9655:                     my @info = split(/\&/,$rest);
                   9656:                     my $currsize = $info[6]/1000;
                   9657:                     if ($currsize < $filesize) {
                   9658:                         my $extra = $filesize - $currsize;
                   9659:                         if (($current_disk_usage + $extra) > $disk_quota) {
                   9660:                             my $msg = '<span class="LC_error">'.
                   9661:                                       &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.',
                   9662:                                           '<span class="LC_filename">'.$fname.'</span>',$filesize,$currsize).'</span>'.
                   9663:                                       '<br />'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',
                   9664:                                                    $disk_quota,$current_disk_usage);
                   9665:                             return ('will_exceed_quota',$msg);
                   9666:                         }
1.984     raeburn  9667:                     }
                   9668:                 }
1.661     raeburn  9669:             }
                   9670:         }
                   9671:     }
                   9672:     if (($current_disk_usage + $filesize) > $disk_quota){
                   9673:         my $msg = '<span class="LC_error">'.
                   9674:                 &mt('Unable to upload [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.','<span class="LC_filename">'.$fname.'</span>',$filesize).'</span>'.
                   9675:                   '<br />'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',$disk_quota,$current_disk_usage);
                   9676:         return ('will_exceed_quota',$msg);
                   9677:     } elsif ($found_file) {
                   9678:         if ($locked_file) {
                   9679:             my $msg = '<span class="LC_error">';
                   9680:             $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>');
                   9681:             $msg .= '</span><br />';
                   9682:             $msg .= &mt('You will be able to rename or delete existing [_1] after a grade has been assigned.','<span class="LC_filename">'.$fname.'</span>');
                   9683:             return ('file_locked',$msg);
                   9684:         } else {
                   9685:             my $msg = '<span class="LC_error">';
1.984     raeburn  9686:             $msg .= &mt(' A file by that name: [_1] was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$port_path.$env{'form.currentpath'});
1.661     raeburn  9687:             $msg .= '</span>';
1.984     raeburn  9688:             return ('existingfile',$msg);
1.661     raeburn  9689:         }
                   9690:     }
                   9691: }
                   9692: 
1.987     raeburn  9693: sub check_for_traversal {
                   9694:     my ($path,$url,$toplevel) = @_;
                   9695:     my @parts=split(/\//,$path);
                   9696:     my $cleanpath;
                   9697:     my $fullpath = $url;
                   9698:     for (my $i=0;$i<@parts;$i++) {
                   9699:         next if ($parts[$i] eq '.');
                   9700:         if ($parts[$i] eq '..') {
                   9701:             $fullpath =~ s{([^/]+/)$}{};
                   9702:         } else {
                   9703:             $fullpath .= $parts[$i].'/';
                   9704:         }
                   9705:     }
                   9706:     if ($fullpath =~ /^\Q$url\E(.*)$/) {
                   9707:         $cleanpath = $1;
                   9708:     } elsif ($fullpath =~ /^\Q$toplevel\E(.*)$/) {
                   9709:         my $curr_toprel = $1;
                   9710:         my @parts = split(/\//,$curr_toprel);
                   9711:         my ($url_toprel) = ($url =~ /^\Q$toplevel\E(.*)$/);
                   9712:         my @urlparts = split(/\//,$url_toprel);
                   9713:         my $doubledots;
                   9714:         my $startdiff = -1;
                   9715:         for (my $i=0; $i<@urlparts; $i++) {
                   9716:             if ($startdiff == -1) {
                   9717:                 unless ($urlparts[$i] eq $parts[$i]) {
                   9718:                     $startdiff = $i;
                   9719:                     $doubledots .= '../';
                   9720:                 }
                   9721:             } else {
                   9722:                 $doubledots .= '../';
                   9723:             }
                   9724:         }
                   9725:         if ($startdiff > -1) {
                   9726:             $cleanpath = $doubledots;
                   9727:             for (my $i=$startdiff; $i<@parts; $i++) {
                   9728:                 $cleanpath .= $parts[$i].'/';
                   9729:             }
                   9730:         }
                   9731:     }
                   9732:     $cleanpath =~ s{(/)$}{};
                   9733:     return $cleanpath;
                   9734: }
1.31      albertel 9735: 
1.1053    raeburn  9736: sub is_archive_file {
                   9737:     my ($mimetype) = @_;
                   9738:     if (($mimetype eq 'application/octet-stream') ||
                   9739:         ($mimetype eq 'application/x-stuffit') ||
                   9740:         ($mimetype =~ m{^application/(x\-)?(compressed|tar|zip|tgz|gz|gtar|gzip|gunzip|bz|bz2|bzip2)})) {
                   9741:         return 1;
                   9742:     }
                   9743:     return;
                   9744: }
                   9745: 
                   9746: sub decompress_form {
                   9747:     my ($mimetype,$archiveurl,$action,$noextract,$hiddenelements) = @_;
                   9748:     my %lt = &Apache::lonlocal::texthash (
                   9749:         this => 'This file is an archive file.',
                   9750:         youm => 'You may wish to extract its contents.',
                   9751:         camt => 'Extraction of contents is recommended for Camtasia zip files.',
                   9752:         perm => 'Permanently remove archive file after extraction of contents?',
                   9753:         extr => 'Extract contents',
                   9754:         yes  => 'Yes',
                   9755:         no   => 'No',
                   9756:     );
                   9757:     my $output = '<p>'.$lt{'this'}.' '.$lt{'youm'}.'<br />';
                   9758:     if ($mimetype =~ m{^application/(x\-)?(compressed|zip)}) {
                   9759:         $output .= $lt{'camt'};
                   9760:     }
                   9761:     $output .= '</p>';
                   9762:     $output .= <<"START";
                   9763: <div id="uploadfileresult">
                   9764:   <form name="uploaded_decompress" action="$action" method="post">
                   9765:   <input type="hidden" name="archiveurl" value="$archiveurl" />
                   9766: START
                   9767:     if (ref($hiddenelements) eq 'HASH') {
                   9768:         foreach my $hidden (sort(keys(%{$hiddenelements}))) {
                   9769:             $output .= '<input type="hidden" name="'.$hidden.'" value="'.$hiddenelements->{$hidden}.'" />'."\n";
                   9770:         }
                   9771:     }
                   9772:     $output .= <<"END";
                   9773: <span class="LC_nobreak">$lt{'perm'}&nbsp;
                   9774: <label><input type="radio" name="archivedelete" value="0" checked="checked" />$lt{'no'}</label>&nbsp;&nbsp;
                   9775: <label><input type="radio" name="archivedelete" value="1" />$lt{'yes'}</label></span><br />
                   9776: <input type="submit" name="decompress" value="$lt{'extr'}" />
                   9777: </form>
                   9778: $noextract
                   9779: </div>
                   9780: END
                   9781:     return $output;
                   9782: }
                   9783: 
                   9784: sub decompress_uploaded_file {
                   9785:     my ($file,$dir) = @_;
                   9786:     &Apache::lonnet::appenv({'cgi.file' => $file});
                   9787:     &Apache::lonnet::appenv({'cgi.dir' => $dir});
                   9788:     my $result = &Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
                   9789:     my ($handle) = ($env{'user.environment'} =~m{/([^/]+)\.id$});
                   9790:     my $lonidsdir = $Apache::lonnet::perlvar{'lonIDsDir'};
                   9791:     &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle,1);
                   9792:     my $decompressed = $env{'cgi.decompressed'};
                   9793:     &Apache::lonnet::delenv('cgi.file');
                   9794:     &Apache::lonnet::delenv('cgi.dir');
                   9795:     &Apache::lonnet::delenv('cgi.decompressed');
                   9796:     return ($decompressed,$result);
                   9797: }
                   9798: 
1.1055    raeburn  9799: sub process_decompression {
                   9800:     my ($docudom,$docuname,$file,$destination,$dir_root,$hiddenelem) = @_;
                   9801:     my ($dir,$error,$warning,$output);
                   9802:     if ($file !~ /\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
                   9803:         $error = &mt('File name not a supported archive file type.').
                   9804:                  '<br />'.&mt('File name should end with one of: [_1].',
                   9805:                               '.zip, .tar, .bz2, .gz, .tar.gz, .tar.bz2, .tgz');
                   9806:     } else {
                   9807:         my $docuhome = &Apache::lonnet::homeserver($docuname,$docudom);
                   9808:         if ($docuhome eq 'no_host') {
                   9809:             $error = &mt('Could not determine home server for course.');
                   9810:         } else {
                   9811:             my @ids=&Apache::lonnet::current_machine_ids();
                   9812:             my $currdir = "$dir_root/$destination";
                   9813:             my ($currdirlistref,$currlisterror) =
                   9814:                 &Apache::lonnet::dirlist($currdir,$docudom,$docuname,1);
                   9815:             if (grep(/^\Q$docuhome\E$/,@ids)) {
                   9816:                 $dir = &LONCAPA::propath($docudom,$docuname).
                   9817:                        "$dir_root/$destination";
                   9818:             } else {
                   9819:                 $dir = $Apache::lonnet::perlvar{'lonDocRoot'}.
                   9820:                        "$dir_root/$docudom/$docuname/$destination";
                   9821:                 unless (&Apache::lonnet::repcopy_userfile("$dir/$file") eq 'ok') {
                   9822:                     $error = &mt('Archive file not found.');
                   9823:                 }
                   9824:             }
                   9825:             if ($dir eq '') {
                   9826:                 $error = &mt('Directory containing archive file unavailable.');
                   9827:             } elsif (!$error) {
                   9828:                 my ($decompressed,$display) = &decompress_uploaded_file($file,$dir);
                   9829:                 if ($decompressed eq 'ok') {
                   9830:                     $output = &mt('Files extracted successfully from archive.').'<br />';
                   9831:                     my ($warning,$result,@contents);
                   9832:                     my ($newdirlistref,$newlisterror) =
                   9833:                         &Apache::lonnet::dirlist($currdir,$docudom,
                   9834:                                                  $docuname,1);
                   9835:                     my (%is_dir,%changes,@newitems);
                   9836:                     my $dirptr = 16384;
                   9837:                     if (ref($currdirlistref) eq 'ARRAY') {
                   9838:                         my @curritems;
                   9839:                         foreach my $dir_line (@{$currdirlistref}) {
                   9840:                             my ($item,$rest)=split(/\&/,$dir_line,2);
                   9841:                             unless ($item =~ /\.+$/) {
                   9842:                                 push(@curritems,$item);
                   9843:                             }
                   9844:                         }
                   9845:                         if (ref($newdirlistref) eq 'ARRAY') {
                   9846:                             foreach my $dir_line (@{$newdirlistref}) {
                   9847:                                 my ($item,undef,undef,$testdir)=split(/\&/,$dir_line,4);
                   9848:                                 unless ($item =~ /^\.+$/) {
                   9849:                                     if ($dirptr&$testdir) {
                   9850:                                         $is_dir{$item} = 1;
                   9851:                                     }
                   9852:                                     push(@newitems,$item);
                   9853:                                 }
                   9854:                             }
                   9855:                             my @diffs = &compare_arrays(\@curritems,\@newitems);
                   9856:                             if (@diffs > 0) {
                   9857:                                foreach my $item (@diffs) {
                   9858:                                    $changes{$item} = 1;
                   9859:                                }
                   9860:                             }
                   9861:                         }
                   9862:                     } elsif (ref($newdirlistref) eq 'ARRAY') {
                   9863:                         foreach my $dir_line (@{$newdirlistref}) {
                   9864:                             my ($item,undef,undef,$testdir)=split(/\&/,$dir_line,5);
                   9865:                             unless ($item =~ /\.+$/) {
                   9866:                                 push(@newitems,$item);
                   9867:                                 if ($dirptr&$testdir) {
                   9868:                                     $is_dir{$item} = 1;
                   9869:                                 }
                   9870:                                 $changes{$item} = 1;
                   9871:                             }
                   9872:                         }
                   9873:                     }
                   9874:                     if (keys(%changes) > 0) {
                   9875:                         foreach my $item (sort(@newitems)) {
                   9876:                             if ($changes{$item}) {
                   9877:                                 push(@contents,$item);
                   9878:                             }
                   9879:                         }
                   9880:                     }
                   9881:                     if (@contents > 0) {
1.1056    raeburn  9882:                         my (%children,%parent,%dirorder,%titles);
1.1055    raeburn  9883:                         my $wantform = 1;
                   9884:                         my ($count,$datatable) = &get_extracted($docudom,$docuname,
                   9885:                                                                 $currdir,\%is_dir,
                   9886:                                                                 \%children,\%parent,
1.1056    raeburn  9887:                                                                 \@contents,\%dirorder,
                   9888:                                                                 \%titles,$wantform);
1.1055    raeburn  9889:                         if ($datatable ne '') {
                   9890:                             $output .= &archive_options_form('decompressed',$datatable,
                   9891:                                                              $count,$hiddenelem);
1.1056    raeburn  9892:                             my $startcount = 4;
1.1055    raeburn  9893:                             $output .= &archive_javascript($startcount,$count,
1.1056    raeburn  9894:                                                            \%titles,\%children);
1.1055    raeburn  9895:                         }
                   9896:                     } else {
                   9897:                         $warning = &mt('No new items extracted from archive file.');
                   9898:                     }
                   9899:                 } else {
                   9900:                     $output = $display;
                   9901:                     $error = &mt('An error occurred during extraction from the archive file.');
                   9902:                 }
                   9903:             }
                   9904:         }
                   9905:     }
                   9906:     if ($error) {
                   9907:         $output .= '<p class="LC_error">'.&mt('Not extracted.').'<br />'.
                   9908:                    $error.'</p>'."\n";
                   9909:     }
                   9910:     if ($warning) {
                   9911:         $output .= '<p class="LC_warning">'.$warning.'</p>'."\n";
                   9912:     }
                   9913:     return $output;
                   9914: }
                   9915: 
                   9916: sub get_extracted {
1.1056    raeburn  9917:     my ($docudom,$docuname,$currdir,$is_dir,$children,$parent,$contents,$dirorder,
                   9918:         $titles,$wantform) = @_;
1.1055    raeburn  9919:     my $count = 0;
                   9920:     my $depth = 0;
                   9921:     my $datatable;
1.1056    raeburn  9922:     my @hierarchy;
1.1055    raeburn  9923:     return unless ((ref($is_dir) eq 'HASH') && (ref($children) eq 'HASH') &&
1.1056    raeburn  9924:                    (ref($parent) eq 'HASH') && (ref($contents) eq 'ARRAY') &&
                   9925:                    (ref($dirorder) eq 'HASH') && (ref($titles) eq 'HASH'));
1.1055    raeburn  9926:     foreach my $item (@{$contents}) {
                   9927:         $count ++;
1.1056    raeburn  9928:         @{$dirorder->{$count}} = @hierarchy;
                   9929:         $titles->{$count} = $item;
1.1055    raeburn  9930:         &archive_hierarchy($depth,$count,$parent,$children);
                   9931:         if ($wantform) {
                   9932:             $datatable .= &archive_row($is_dir->{$item},$item,
                   9933:                                        $currdir,$depth,$count);
                   9934:         }
                   9935:         if ($is_dir->{$item}) {
                   9936:             $depth ++;
1.1056    raeburn  9937:             push(@hierarchy,$count);
                   9938:             $parent->{$depth} = $count;
1.1055    raeburn  9939:             $datatable .=
                   9940:                 &recurse_extracted_archive("$currdir/$item",$docudom,$docuname,
1.1056    raeburn  9941:                                            \$depth,\$count,\@hierarchy,$dirorder,
                   9942:                                            $children,$parent,$titles,$wantform);
1.1055    raeburn  9943:             $depth --;
1.1056    raeburn  9944:             pop(@hierarchy);
1.1055    raeburn  9945:         }
                   9946:     }
                   9947:     return ($count,$datatable);
                   9948: }
                   9949: 
                   9950: sub recurse_extracted_archive {
1.1056    raeburn  9951:     my ($currdir,$docudom,$docuname,$depth,$count,$hierarchy,$dirorder,
                   9952:         $children,$parent,$titles,$wantform) = @_;
1.1055    raeburn  9953:     my $result='';
1.1056    raeburn  9954:     unless ((ref($depth)) && (ref($count)) && (ref($hierarchy) eq 'ARRAY') &&
                   9955:             (ref($children) eq 'HASH') && (ref($parent) eq 'HASH') &&
                   9956:             (ref($dirorder) eq 'HASH')) {
1.1055    raeburn  9957:         return $result;
                   9958:     }
                   9959:     my $dirptr = 16384;
                   9960:     my ($newdirlistref,$newlisterror) =
                   9961:         &Apache::lonnet::dirlist($currdir,$docudom,$docuname,1);
                   9962:     if (ref($newdirlistref) eq 'ARRAY') {
                   9963:         foreach my $dir_line (@{$newdirlistref}) {
                   9964:             my ($item,undef,undef,$testdir)=split(/\&/,$dir_line,5);
                   9965:             unless ($item =~ /^\.+$/) {
                   9966:                 $$count ++;
1.1056    raeburn  9967:                 @{$dirorder->{$$count}} = @{$hierarchy};
                   9968:                 $titles->{$$count} = $item;
1.1055    raeburn  9969:                 &archive_hierarchy($$depth,$$count,$parent,$children);
1.1056    raeburn  9970: 
1.1055    raeburn  9971:                 my $is_dir;
                   9972:                 if ($dirptr&$testdir) {
                   9973:                     $is_dir = 1;
                   9974:                 }
                   9975:                 if ($wantform) {
                   9976:                     $result .= &archive_row($is_dir,$item,$currdir,$$depth,$$count);
                   9977:                 }
                   9978:                 if ($is_dir) {
                   9979:                     $$depth ++;
1.1056    raeburn  9980:                     push(@{$hierarchy},$$count);
                   9981:                     $parent->{$$depth} = $$count;
1.1055    raeburn  9982:                     $result .=
                   9983:                         &recurse_extracted_archive("$currdir/$item",$docudom,
                   9984:                                                    $docuname,$depth,$count,
1.1056    raeburn  9985:                                                    $hierarchy,$dirorder,$children,
                   9986:                                                    $parent,$titles,$wantform);
1.1055    raeburn  9987:                     $$depth --;
1.1056    raeburn  9988:                     pop(@{$hierarchy});
1.1055    raeburn  9989:                 }
                   9990:             }
                   9991:         }
                   9992:     }
                   9993:     return $result;
                   9994: }
                   9995: 
                   9996: sub archive_hierarchy {
                   9997:     my ($depth,$count,$parent,$children) =@_;
                   9998:     if ((ref($parent) eq 'HASH') && (ref($children) eq 'HASH')) {
                   9999:         if (exists($parent->{$depth})) {
                   10000:              $children->{$parent->{$depth}} .= $count.':';
                   10001:         }
                   10002:     }
                   10003:     return;
                   10004: }
                   10005: 
                   10006: sub archive_row {
                   10007:     my ($is_dir,$item,$currdir,$depth,$count) = @_;
                   10008:     my ($name) = ($item =~ m{([^/]+)$});
                   10009:     my %choices = &Apache::lonlocal::texthash (
1.1059    raeburn  10010:                                        'display'    => 'Add as file',
1.1055    raeburn  10011:                                        'dependency' => 'Include as dependency',
                   10012:                                        'discard'    => 'Discard',
                   10013:                                       );
                   10014:     if ($is_dir) {
1.1059    raeburn  10015:         $choices{'display'} = &mt('Add as folder'); 
1.1055    raeburn  10016:     }
1.1056    raeburn  10017:     my $output = &start_data_table_row().'<td align="right">'.$count.'</td>'."\n";
                   10018:     my $offset = 0;
1.1055    raeburn  10019:     foreach my $action ('display','dependency','discard') {
1.1056    raeburn  10020:         $offset ++;
1.1055    raeburn  10021:         $output .= '<td><span class="LC_nobreak">'.
                   10022:                    '<label><input type="radio" name="archive_'.$count.
                   10023:                    '" id="archive_'.$action.'_'.$count.'" value="'.$action.'"';
                   10024:         my $text = $choices{$action};
                   10025:         if ($is_dir) {
                   10026:             $output .= ' onclick="javascript:propagateCheck(this.form,'."'$count'".');"';
                   10027:             if ($action eq 'display') {
1.1059    raeburn  10028:                 $text = &mt('Add as folder');
1.1055    raeburn  10029:             }
1.1056    raeburn  10030:         } else {
                   10031:             $output .= ' onclick="javascript:dependencyCheck(this.form,'."$count,$offset".');"';
                   10032: 
                   10033:         }
                   10034:         $output .= ' />&nbsp;'.$choices{$action}.'</label></span>';
                   10035:         if ($action eq 'dependency') {
                   10036:             $output .= '<div id="arc_depon_'.$count.'" style="display:none;">'."\n".
                   10037:                        &mt('Used by:').'&nbsp;<select name="archive_dependent_on_'.$count.'" '.
                   10038:                        'onchange="propagateSelect(this.form,'."$count,$offset".')">'."\n".
                   10039:                        '<option value=""></option>'."\n".
                   10040:                        '</select>'."\n".
                   10041:                        '</div>';
1.1059    raeburn  10042:         } elsif ($action eq 'display') {
                   10043:             $output .= '<div id="arc_title_'.$count.'" style="display:none;">'."\n".
                   10044:                        &mt('Title:').'&nbsp;<input type="text" name="archive_title_'.$count.'" id="archive_title_'.$count.'" />'."\n".
                   10045:                        '</div>';
1.1055    raeburn  10046:         }
1.1056    raeburn  10047:         $output .= '</td>';
1.1055    raeburn  10048:     }
                   10049:     $output .= '<td><input type="hidden" name="archive_content_'.$count.'" value="'.
                   10050:                &HTML::Entities::encode("$currdir/$item",'"<>&').'" />'.('&nbsp;' x 2);
                   10051:     for (my $i=0; $i<$depth; $i++) {
                   10052:         $output .= ('<img src="/adm/lonIcons/whitespace1.gif" class="LC_docs_spacer" alt="" />' x2)."\n";
                   10053:     }
                   10054:     if ($is_dir) {
                   10055:         $output .= '<img src="/adm/lonIcons/navmap.folder.open.gif" alt="" />&nbsp;'."\n".
                   10056:                    '<input type="hidden" name="archive_directory" value="'.$count.'" />'."\n";
                   10057:     } else {
                   10058:         $output .= '<input type="hidden" name="archive_file" value="'.$count.'" />'."\n";
                   10059:     }
                   10060:     $output .= '&nbsp;'.$name.'</td>'."\n".
                   10061:                &end_data_table_row();
                   10062:     return $output;
                   10063: }
                   10064: 
                   10065: sub archive_options_form {
                   10066:     my ($form,$output,$count,$hiddenelem) = @_;
                   10067:     return '<form name="'.$form.'" method="post" action="">'."\n".
                   10068:            '<input type="hidden" name="phase" value="decompress_cleanup" />'."\n".
                   10069:                     '<p>'.
                   10070:                     &mt('How should each item be incorporated in the course?').
                   10071:                     '</p>'.
                   10072:                     '<div class="LC_columnSection"><fieldset>'.
                   10073:                     '<legend>'.&mt('Content actions for all').'</legend>'.
1.1059    raeburn  10074:                     '<input type="button" value="'.&mt('Add as folder/file').'" '.
1.1055    raeburn  10075:                     'onclick="javascript:checkAll(document.'.$form.",'display'".')" />'.
1.1059    raeburn  10076:                     '&nbsp;&nbsp;<input type="button" value="'.&mt('Include as dependency for a displayed file').'"'.
1.1055    raeburn  10077:                     ' onclick="javascript:checkAll(document.'.$form.",'dependency'".')" />'.
                   10078:                     '&nbsp;&nbsp;<input type="button" value="'.&mt('Discard').'"'.
                   10079:                     ' onclick="javascript:checkAll(document.'.$form.",'discard'".')" />'.
                   10080:                      '</fieldset></div>'.
                   10081:            &start_data_table()."\n".
                   10082:            $output."\n".
                   10083:            &end_data_table()."\n".
                   10084:            '<input type="hidden" name="archive_count" value="'.$count.'" />'.
                   10085:            $hiddenelem.
                   10086:            '<br /><input type="submit" name="archive_submit" value="'.&mt('Save').'" />'.
                   10087:            '</form>';
                   10088: }
                   10089: 
                   10090: sub archive_javascript {
1.1056    raeburn  10091:     my ($startcount,$numitems,$titles,$children) = @_;
                   10092:     return unless ((ref($titles) eq 'HASH') && (ref($children) eq 'HASH'));
1.1059    raeburn  10093:     my $maintitle = $env{'form.comment'};
1.1055    raeburn  10094:     my $scripttag = <<START;
                   10095: <script type="text/javascript">
                   10096: // <![CDATA[
                   10097: 
                   10098: function checkAll(form,prefix) {
                   10099:     var idstr =  new RegExp("^archive_"+prefix+"_\\\\d+\$");
                   10100:     for (var i=0; i < form.elements.length; i++) {
                   10101:         var id = form.elements[i].id;
                   10102:         if ((id != '') && (id != undefined)) {
                   10103:             if (idstr.test(id)) {
                   10104:                 if (form.elements[i].type == 'radio') {
                   10105:                     form.elements[i].checked = true;
1.1056    raeburn  10106:                     var nostart = i-$startcount;
1.1059    raeburn  10107:                     var offset = nostart%7;
                   10108:                     var count = (nostart-offset)/7;    
1.1056    raeburn  10109:                     dependencyCheck(form,count,offset);
1.1055    raeburn  10110:                 }
                   10111:             }
                   10112:         }
                   10113:     }
                   10114: }
                   10115: 
                   10116: function propagateCheck(form,count) {
                   10117:     if (count > 0) {
1.1059    raeburn  10118:         var startelement = $startcount + ((count-1) * 7);
                   10119:         for (var j=1; j<6; j++) {
                   10120:             if ((j != 2) && (j != 4)) {
1.1056    raeburn  10121:                 var item = startelement + j; 
                   10122:                 if (form.elements[item].type == 'radio') {
                   10123:                     if (form.elements[item].checked) {
                   10124:                         containerCheck(form,count,j);
                   10125:                         break;
                   10126:                     }
1.1055    raeburn  10127:                 }
                   10128:             }
                   10129:         }
                   10130:     }
                   10131: }
                   10132: 
                   10133: numitems = $numitems
1.1056    raeburn  10134: var titles = new Array(numitems);
                   10135: var parents = new Array(numitems);
1.1055    raeburn  10136: for (var i=0; i<numitems; i++) {
1.1056    raeburn  10137:     parents[i] = new Array;
1.1055    raeburn  10138: }
1.1059    raeburn  10139: var maintitle = '$maintitle';
1.1055    raeburn  10140: 
                   10141: START
                   10142: 
1.1056    raeburn  10143:     foreach my $container (sort { $a <=> $b } (keys(%{$children}))) {
                   10144:         my @contents = split(/:/,$children->{$container});
1.1055    raeburn  10145:         for (my $i=0; $i<@contents; $i ++) {
                   10146:             $scripttag .= 'parents['.$container.']['.$i.'] = '.$contents[$i]."\n";
                   10147:         }
                   10148:     }
                   10149: 
1.1056    raeburn  10150:     foreach my $key (sort { $a <=> $b } (keys(%{$titles}))) {
                   10151:         $scripttag .= "titles[$key] = '".$titles->{$key}."';\n";
                   10152:     }
                   10153: 
1.1055    raeburn  10154:     $scripttag .= <<END;
                   10155: 
                   10156: function containerCheck(form,count,offset) {
                   10157:     if (count > 0) {
1.1056    raeburn  10158:         dependencyCheck(form,count,offset);
1.1059    raeburn  10159:         var item = (offset+$startcount)+7*(count-1);
1.1055    raeburn  10160:         form.elements[item].checked = true;
                   10161:         if(Object.prototype.toString.call(parents[count]) === '[object Array]') {
                   10162:             if (parents[count].length > 0) {
                   10163:                 for (var j=0; j<parents[count].length; j++) {
1.1056    raeburn  10164:                     containerCheck(form,parents[count][j],offset);
                   10165:                 }
                   10166:             }
                   10167:         }
                   10168:     }
                   10169: }
                   10170: 
                   10171: function dependencyCheck(form,count,offset) {
                   10172:     if (count > 0) {
1.1059    raeburn  10173:         var chosen = (offset+$startcount)+7*(count-1);
                   10174:         var depitem = $startcount + ((count-1) * 7) + 4;
1.1056    raeburn  10175:         var currtype = form.elements[depitem].type;
                   10176:         if (form.elements[chosen].value == 'dependency') {
                   10177:             document.getElementById('arc_depon_'+count).style.display='block'; 
                   10178:             form.elements[depitem].options.length = 0;
                   10179:             form.elements[depitem].options[0] = new Option('Select','',true,true);
                   10180:             for (var i=1; i<count; i++) {
1.1059    raeburn  10181:                 var startelement = $startcount + (i-1) * 7;
                   10182:                 for (var j=1; j<6; j++) {
                   10183:                     if ((j != 2) && (j!= 4)) {
1.1056    raeburn  10184:                         var item = startelement + j;
                   10185:                         if (form.elements[item].type == 'radio') {
                   10186:                             if (form.elements[item].checked) {
                   10187:                                 if (form.elements[item].value == 'display') {
                   10188:                                     var n = form.elements[depitem].options.length;
                   10189:                                     form.elements[depitem].options[n] = new Option(titles[i],i,false,false);
                   10190:                                 }
                   10191:                             }
                   10192:                         }
                   10193:                     }
                   10194:                 }
                   10195:             }
                   10196:         } else {
                   10197:             document.getElementById('arc_depon_'+count).style.display='none';
                   10198:             form.elements[depitem].options.length = 0;
                   10199:             form.elements[depitem].options[0] = new Option('Select','',true,true);
                   10200:         }
1.1059    raeburn  10201:         titleCheck(form,count,offset);
1.1056    raeburn  10202:     }
                   10203: }
                   10204: 
                   10205: function propagateSelect(form,count,offset) {
                   10206:     if (count > 0) {
1.1059    raeburn  10207:         var item = (2+offset+$startcount)+7*(count-1);
1.1056    raeburn  10208:         var picked = form.elements[item].options[form.elements[item].selectedIndex].value; 
                   10209:         if (Object.prototype.toString.call(parents[count]) === '[object Array]') {
                   10210:             if (parents[count].length > 0) {
                   10211:                 for (var j=0; j<parents[count].length; j++) {
                   10212:                     containerSelect(form,parents[count][j],offset,picked);
1.1055    raeburn  10213:                 }
                   10214:             }
                   10215:         }
                   10216:     }
                   10217: }
1.1056    raeburn  10218: 
                   10219: function containerSelect(form,count,offset,picked) {
                   10220:     if (count > 0) {
1.1059    raeburn  10221:         var item = (1+offset+$startcount)+7*(count-1);
1.1056    raeburn  10222:         if (form.elements[item].type == 'radio') {
                   10223:             if (form.elements[item].value == 'dependency') {
                   10224:                 if (form.elements[item+1].type == 'select-one') {
                   10225:                     for (var i=0; i<form.elements[item+1].options.length; i++) {
                   10226:                         if (form.elements[item+1].options[i].value == picked) {
                   10227:                             form.elements[item+1].selectedIndex = i;
                   10228:                             break;
                   10229:                         }
                   10230:                     }
                   10231:                 }
                   10232:                 if (Object.prototype.toString.call(parents[count]) === '[object Array]') {
                   10233:                     if (parents[count].length > 0) {
                   10234:                         for (var j=0; j<parents[count].length; j++) {
                   10235:                             containerSelect(form,parents[count][j],offset,picked);
                   10236:                         }
                   10237:                     }
                   10238:                 }
                   10239:             }
                   10240:         }
                   10241:     }
                   10242: }
                   10243: 
1.1059    raeburn  10244: function titleCheck(form,count,offset) {
                   10245:     if (count > 0) {
                   10246:         var chosen = (offset+$startcount)+7*(count-1);
                   10247:         var depitem = $startcount + ((count-1) * 7) + 2;
                   10248:         var currtype = form.elements[depitem].type;
                   10249:         if (form.elements[chosen].value == 'display') {
                   10250:             document.getElementById('arc_title_'+count).style.display='block';
                   10251:             if ((count==1) && ((parents[count].length > 0) || (numitems == 1))) {
                   10252:                 document.getElementById('archive_title_'+count).value=maintitle;
                   10253:             }
                   10254:         } else {
                   10255:             document.getElementById('arc_title_'+count).style.display='none';
                   10256:             if (currtype == 'text') { 
                   10257:                 document.getElementById('archive_title_'+count).value='';
                   10258:             }
                   10259:         }
                   10260:     }
                   10261:     return;
                   10262: }
                   10263: 
1.1055    raeburn  10264: // ]]>
                   10265: </script>
                   10266: END
                   10267:     return $scripttag;
                   10268: }
                   10269: 
                   10270: sub process_extracted_files {
                   10271:     my ($context,$docudom,$docuname,$destination,$dir_root,$hiddenelem) = @_;
                   10272:     my $numitems = $env{'form.archive_count'};
                   10273:     return unless ($numitems);
                   10274:     my @ids=&Apache::lonnet::current_machine_ids();
                   10275:     my ($prefix,$pathtocheck,$dir,$ishome,$error,$warning,%toplevelitems,%is_dir,
                   10276:         %folders,%containers,%mapinner);
                   10277:     my $docuhome = &Apache::lonnet::homeserver($docuname,$docudom);
                   10278:     if (grep(/^\Q$docuhome\E$/,@ids)) {
                   10279:         $prefix = &LONCAPA::propath($docudom,$docuname);
                   10280:         $pathtocheck = "$dir_root/$destination";
                   10281:         $dir = $dir_root;
                   10282:         $ishome = 1;
                   10283:     } else {
                   10284:         $prefix = $Apache::lonnet::perlvar{'lonDocRoot'};
                   10285:         $pathtocheck = "$dir_root/$docudom/$docuname/$destination";
                   10286:         $dir = "$dir_root/$docudom/$docuname";    
                   10287:     }
                   10288:     my $currdir = "$dir_root/$destination";
                   10289:     (my $docstype,$mapinner{'0'}) = ($destination =~ m{^(docs|supplemental)/(\w+)/});
                   10290:     if ($env{'form.folderpath'}) {
                   10291:         my @items = split('&',$env{'form.folderpath'});
                   10292:         $folders{'0'} = $items[-2];
                   10293:         $containers{'0'}='sequence';
                   10294:     } elsif ($env{'form.pagepath'}) {
                   10295:         my @items = split('&',$env{'form.pagepath'});
                   10296:         $folders{'0'} = $items[-2];
                   10297:         $containers{'0'}='page';
                   10298:     }
                   10299:     my @archdirs = &get_env_multiple('form.archive_directory');
                   10300:     if ($numitems) {
                   10301:         for (my $i=1; $i<=$numitems; $i++) {
                   10302:             my $path = $env{'form.archive_content_'.$i};
                   10303:             if ($path =~ m{^\Q$pathtocheck\E/([^/]+)$}) {
                   10304:                 my $item = $1;
                   10305:                 $toplevelitems{$item} = $i;
                   10306:                 if (grep(/^\Q$i\E$/,@archdirs)) {
                   10307:                     $is_dir{$item} = 1;
                   10308:                 }
                   10309:             }
                   10310:         }
                   10311:     }
1.1056    raeburn  10312:     my ($output,%children,%parent,%titles,%dirorder);
1.1055    raeburn  10313:     if (keys(%toplevelitems) > 0) {
                   10314:         my @contents = sort(keys(%toplevelitems));
1.1056    raeburn  10315:         (my $count,undef) = &get_extracted($docudom,$docuname,$currdir,\%is_dir,\%children,
                   10316:                                            \%parent,\@contents,\%dirorder,\%titles);
1.1055    raeburn  10317:     }
1.1056    raeburn  10318:     my (%referrer,%orphaned,%todelete,%newdest,%newseqid);
1.1055    raeburn  10319:     if ($numitems) {
                   10320:         for (my $i=1; $i<=$numitems; $i++) {
                   10321:             my $path = $env{'form.archive_content_'.$i};
                   10322:             if ($path =~ /^\Q$pathtocheck\E/) {
                   10323:                 if ($env{'form.archive_'.$i} eq 'discard') {
                   10324:                     if ($prefix ne '' && $path ne '') {
                   10325:                         if (-e $prefix.$path) {
                   10326:                             $todelete{$prefix.$path} = 1;
                   10327:                         }
                   10328:                     }
                   10329:                 } elsif ($env{'form.archive_'.$i} eq 'display') {
1.1059    raeburn  10330:                     my ($docstitle,$title,$url,$outer);
1.1055    raeburn  10331:                     ($title) = ($path =~ m{/([^/]+)$});
1.1059    raeburn  10332:                     $docstitle = $env{'form.archive_title_'.$i};
                   10333:                     if ($docstitle eq '') {
                   10334:                         $docstitle = $title;
                   10335:                     }
1.1055    raeburn  10336:                     $outer = 0;
1.1056    raeburn  10337:                     if (ref($dirorder{$i}) eq 'ARRAY') {
                   10338:                         if (@{$dirorder{$i}} > 0) {
                   10339:                             foreach my $item (reverse(@{$dirorder{$i}})) {
1.1055    raeburn  10340:                                 if ($env{'form.archive_'.$item} eq 'display') {
                   10341:                                     $outer = $item;
                   10342:                                     last;
                   10343:                                 }
                   10344:                             }
                   10345:                         }
                   10346:                     }
                   10347:                     my ($errtext,$fatal) = 
                   10348:                         &LONCAPA::map::mapread('/uploaded/'.$docudom.'/'.$docuname.
                   10349:                                                '/'.$folders{$outer}.'.'.
                   10350:                                                $containers{$outer});
                   10351:                     next if ($fatal);
                   10352:                     if ((@archdirs > 0) && (grep(/^\Q$i\E$/,@archdirs))) {
                   10353:                         if ($context eq 'coursedocs') {
1.1056    raeburn  10354:                             $mapinner{$i} = time;
1.1055    raeburn  10355:                             $folders{$i} = 'default_'.$mapinner{$i};
                   10356:                             $containers{$i} = 'sequence';
                   10357:                             my $url = '/uploaded/'.$docudom.'/'.$docuname.'/'.
                   10358:                                       $folders{$i}.'.'.$containers{$i};
                   10359:                             my $newidx = &LONCAPA::map::getresidx();
                   10360:                             $LONCAPA::map::resources[$newidx]=
1.1059    raeburn  10361:                                 $docstitle.':'.$url.':false:normal:res';
1.1055    raeburn  10362:                             push(@LONCAPA::map::order,$newidx);
                   10363:                             my ($outtext,$errtext) =
                   10364:                                 &LONCAPA::map::storemap('/uploaded/'.$docudom.'/'.
                   10365:                                                         $docuname.'/'.$folders{$outer}.
                   10366:                                                         '.'.$containers{$outer},1);
1.1056    raeburn  10367:                             $newseqid{$i} = $newidx;
1.1055    raeburn  10368:                         }
                   10369:                     } else {
                   10370:                         if ($context eq 'coursedocs') {
                   10371:                             my $newidx=&LONCAPA::map::getresidx();
                   10372:                             my $url = '/uploaded/'.$docudom.'/'.$docuname.'/'.
                   10373:                                       $docstype.'/'.$mapinner{$outer}.'/'.$newidx.'/'.
                   10374:                                       $title;
                   10375:                             if (!-e "$prefix$dir/$docstype/$mapinner{$outer}") {
                   10376:                                 mkdir("$prefix$dir/$docstype/$mapinner{$outer}",0755);
                   10377:                             }
                   10378:                             if (!-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx") {
                   10379:                                 mkdir("$prefix$dir/$docstype/$mapinner{$outer}/$newidx");
                   10380:                             }
                   10381:                             if (-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx") {
                   10382:                                 system("mv $prefix$path $prefix$dir/$docstype/$mapinner{$outer}/$newidx/$title");
1.1056    raeburn  10383:                                 $newdest{$i} = "$prefix$dir/$docstype/$mapinner{$outer}/$newidx";
1.1055    raeburn  10384:                             }
                   10385:                             $LONCAPA::map::resources[$newidx]=
1.1059    raeburn  10386:                                 $docstitle.':'.$url.':false:normal:res';
1.1055    raeburn  10387:                             push(@LONCAPA::map::order, $newidx);
                   10388:                             my ($outtext,$errtext)=
                   10389:                                 &LONCAPA::map::storemap('/uploaded/'.$docudom.'/'.
                   10390:                                                         $docuname.'/'.$folders{$outer}.
                   10391:                                                         '.'.$containers{$outer},1);
                   10392:                         }
                   10393:                     }
                   10394:                 } elsif ($env{'form.archive_'.$i} eq 'dependency') {
1.1056    raeburn  10395:                     my ($title) = ($path =~ m{/([^/]+)$});
                   10396:                     $referrer{$i} = $env{'form.archive_dependent_on_'.$i};
                   10397:                     if ($env{'form.archive_'.$referrer{$i}} eq 'display') {
                   10398:                         if (ref($dirorder{$i}) eq 'ARRAY') {
                   10399:                             my ($itemidx,$fullpath);
                   10400:                             for (my $j=0; $j<@{$dirorder{$i}}; $j++) {
                   10401:                                 if (ref($dirorder{$referrer{$i}}) eq 'ARRAY') {
                   10402:                                     my $container = $dirorder{$referrer{$i}}->[-1];
                   10403:                                     for (my $j=0; $j<@{$dirorder{$i}}; $j++) {
                   10404:                                         if ($dirorder{$i}->[$j] eq $container) {
                   10405:                                             $itemidx = $j;
                   10406:                                         }
                   10407:                                     }
                   10408:                                 }
                   10409:                             }
                   10410:                             if ($itemidx ne '') {
                   10411:                                 if (grep(/^\Q$referrer{$i}\E$/,@archdirs)) {
                   10412:                                     if ($mapinner{$referrer{$i}}) {
                   10413:                                         $fullpath = "$prefix$dir/$docstype/$mapinner{$referrer{$i}}";
                   10414:                                         for (my $j=$itemidx; $j<@{$dirorder{$i}}; $j++) {
                   10415:                                             if (grep(/^\Q$dirorder{$i}->[$j]\E$/,@archdirs)) {
                   10416:                                                 unless (defined($newseqid{$dirorder{$i}->[$j]})) {
                   10417:                                                     $fullpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   10418:                                                     if (!-e $fullpath) {
                   10419:                                                         mkdir($fullpath,0755);
                   10420:                                                     }
                   10421:                                                 }
                   10422:                                             } else {
                   10423:                                                 last;
                   10424:                                             }
                   10425:                                         }
                   10426:                                     }
                   10427:                                 } elsif ($newdest{$referrer{$i}}) {
                   10428:                                     $fullpath = $newdest{$referrer{$i}};
                   10429:                                     for (my $j=$itemidx; $j<@{$dirorder{$i}}; $j++) {
                   10430:                                         if ($env{'form.archive_'.$dirorder{$i}->[$j]} eq 'discard') {
                   10431:                                             $orphaned{$i} = $env{'form.archive_'.$dirorder{$i}->[$j]};
                   10432:                                             last;
                   10433:                                         } elsif (grep(/^\Q$dirorder{$i}->[$j]\E$/,@archdirs)) {
                   10434:                                             unless (defined($newseqid{$dirorder{$i}->[$j]})) {
                   10435:                                                 $fullpath .= '/'.$titles{$dirorder{$i}->[$j]};
                   10436:                                                 if (!-e $fullpath) {
                   10437:                                                     mkdir($fullpath,0755);
                   10438:                                                 }
                   10439:                                             }
                   10440:                                         } else {
                   10441:                                             last;
                   10442:                                         }
                   10443:                                     }
                   10444:                                 }
                   10445:                                 if ($fullpath ne '') {
                   10446:                                     system("mv $prefix$path $fullpath/$title");
                   10447:                                 }
1.1055    raeburn  10448:                             }
                   10449:                         }
1.1056    raeburn  10450:                     } elsif ($env{'form.archive_'.$referrer{$i}} eq 'discard') {
                   10451:                         $warning .= &mt('[_1] is a dependency of [_2], which was discarded.',
                   10452:                                         $path,$env{'form.archive_content_'.$referrer{$i}}).'<br />';
1.1055    raeburn  10453:                     }
                   10454:                 }
                   10455:             } else {
                   10456:                 $warning .= &mt('Item extracted from archive: [_1] has unexpected path.',$path).'<br />'; 
                   10457:             }
                   10458:         }
                   10459:         if (keys(%todelete)) {
                   10460:             foreach my $key (keys(%todelete)) {
                   10461:                 unlink($key);
                   10462:                 unless ($ishome) {
                   10463:                     #FIXME Need to notify homeserver to delete files.
                   10464:                 }
                   10465:             }
                   10466:         }
                   10467:     } else {
                   10468:         $warning = &mt('No items found in archive.');
                   10469:     }
                   10470:     if ($error) {
                   10471:         $output .= '<p class="LC_error">'.&mt('Not extracted.').'<br />'.
                   10472:                    $error.'</p>'."\n";
                   10473:     }
                   10474:     if ($warning) {
                   10475:         $output .= '<p class="LC_warning">'.$warning.'</p>'."\n";
                   10476:     }
                   10477:     return $output;
                   10478: }
                   10479: 
1.41      ng       10480: =pod
1.45      matthew  10481: 
1.1015    raeburn  10482: =item * &get_turnedin_filepath()
                   10483: 
                   10484: Determines path in a user's portfolio file for storage of files uploaded
                   10485: to a specific essayresponse or dropbox item.
                   10486: 
                   10487: Inputs: 3 required + 1 optional.
                   10488: $symb is symb for resource, $uname and $udom are for current user (required).
                   10489: $caller is optional (can be "submission", if routine is called when storing
                   10490: an upoaded file when "Submit Answer" button was pressed).
                   10491: 
                   10492: Returns array containing $path and $multiresp. 
                   10493: $path is path in portfolio.  $multiresp is 1 if this resource contains more
                   10494: than one file upload item.  Callers of routine should append partid as a 
                   10495: subdirectory to $path in cases where $multiresp is 1.
                   10496: 
                   10497: Called by: homework/essayresponse.pm and homework/structuretags.pm
                   10498: 
                   10499: =cut
                   10500: 
                   10501: sub get_turnedin_filepath {
                   10502:     my ($symb,$uname,$udom,$caller) = @_;
                   10503:     my ($map,$resid,$resurl)=&Apache::lonnet::decode_symb($symb);
                   10504:     my $turnindir;
                   10505:     my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'turnindir');
                   10506:     $turnindir = $userhash{'turnindir'};
                   10507:     my ($path,$multiresp);
                   10508:     if ($turnindir eq '') {
                   10509:         if ($caller eq 'submission') {
                   10510:             $turnindir = &mt('turned in');
                   10511:             $turnindir =~ s/\W+/_/g;
                   10512:             my %newhash = (
                   10513:                             'turnindir' => $turnindir,
                   10514:                           );
                   10515:             &Apache::lonnet::put('environment',\%newhash,$udom,$uname);
                   10516:         }
                   10517:     }
                   10518:     if ($turnindir ne '') {
                   10519:         $path = '/'.$turnindir.'/';
                   10520:         my ($multipart,$turnin,@pathitems);
                   10521:         my $navmap = Apache::lonnavmaps::navmap->new();
                   10522:         if (defined($navmap)) {
                   10523:             my $mapres = $navmap->getResourceByUrl($map);
                   10524:             if (ref($mapres)) {
                   10525:                 my $pcslist = $mapres->map_hierarchy();
                   10526:                 if ($pcslist ne '') {
                   10527:                     foreach my $pc (split(/,/,$pcslist)) {
                   10528:                         my $res = $navmap->getByMapPc($pc);
                   10529:                         if (ref($res)) {
                   10530:                             my $title = $res->compTitle();
                   10531:                             $title =~ s/\W+/_/g;
                   10532:                             if ($title ne '') {
                   10533:                                 push(@pathitems,$title);
                   10534:                             }
                   10535:                         }
                   10536:                     }
                   10537:                 }
                   10538:                 my $maptitle = $mapres->compTitle();
                   10539:                 $maptitle =~ s/\W+/_/g;
                   10540:                 if ($maptitle ne '') {
                   10541:                     push(@pathitems,$maptitle);
                   10542:                 }
                   10543:                 unless ($env{'request.state'} eq 'construct') {
                   10544:                     my $res = $navmap->getBySymb($symb);
                   10545:                     if (ref($res)) {
                   10546:                         my $partlist = $res->parts();
                   10547:                         my $totaluploads = 0;
                   10548:                         if (ref($partlist) eq 'ARRAY') {
                   10549:                             foreach my $part (@{$partlist}) {
                   10550:                                 my @types = $res->responseType($part);
                   10551:                                 my @ids = $res->responseIds($part);
                   10552:                                 for (my $i=0; $i < scalar(@ids); $i++) {
                   10553:                                     if ($types[$i] eq 'essay') {
                   10554:                                         my $partid = $part.'_'.$ids[$i];
                   10555:                                         if (&Apache::lonnet::EXT("resource.$partid.uploadedfiletypes") ne '') {
                   10556:                                             $totaluploads ++;
                   10557:                                         }
                   10558:                                     }
                   10559:                                 }
                   10560:                             }
                   10561:                             if ($totaluploads > 1) {
                   10562:                                 $multiresp = 1;
                   10563:                             }
                   10564:                         }
                   10565:                     }
                   10566:                 }
                   10567:             } else {
                   10568:                 return;
                   10569:             }
                   10570:         } else {
                   10571:             return;
                   10572:         }
                   10573:         my $restitle=&Apache::lonnet::gettitle($symb);
                   10574:         $restitle =~ s/\W+/_/g;
                   10575:         if ($restitle eq '') {
                   10576:             $restitle = ($resurl =~ m{/[^/]+$});
                   10577:             if ($restitle eq '') {
                   10578:                 $restitle = time;
                   10579:             }
                   10580:         }
                   10581:         push(@pathitems,$restitle);
                   10582:         $path .= join('/',@pathitems);
                   10583:     }
                   10584:     return ($path,$multiresp);
                   10585: }
                   10586: 
                   10587: =pod
                   10588: 
1.464     albertel 10589: =back
1.41      ng       10590: 
1.112     bowersj2 10591: =head1 CSV Upload/Handling functions
1.38      albertel 10592: 
1.41      ng       10593: =over 4
                   10594: 
1.648     raeburn  10595: =item * &upfile_store($r)
1.41      ng       10596: 
                   10597: Store uploaded file, $r should be the HTTP Request object,
1.258     albertel 10598: needs $env{'form.upfile'}
1.41      ng       10599: returns $datatoken to be put into hidden field
                   10600: 
                   10601: =cut
1.31      albertel 10602: 
                   10603: sub upfile_store {
                   10604:     my $r=shift;
1.258     albertel 10605:     $env{'form.upfile'}=~s/\r/\n/gs;
                   10606:     $env{'form.upfile'}=~s/\f/\n/gs;
                   10607:     $env{'form.upfile'}=~s/\n+/\n/gs;
                   10608:     $env{'form.upfile'}=~s/\n+$//gs;
1.31      albertel 10609: 
1.258     albertel 10610:     my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
                   10611: 	'_enroll_'.$env{'request.course.id'}.'_'.time.'_'.$$;
1.31      albertel 10612:     {
1.158     raeburn  10613:         my $datafile = $r->dir_config('lonDaemons').
                   10614:                            '/tmp/'.$datatoken.'.tmp';
                   10615:         if ( open(my $fh,">$datafile") ) {
1.258     albertel 10616:             print $fh $env{'form.upfile'};
1.158     raeburn  10617:             close($fh);
                   10618:         }
1.31      albertel 10619:     }
                   10620:     return $datatoken;
                   10621: }
                   10622: 
1.56      matthew  10623: =pod
                   10624: 
1.648     raeburn  10625: =item * &load_tmp_file($r)
1.41      ng       10626: 
                   10627: Load uploaded file from tmp, $r should be the HTTP Request object,
1.258     albertel 10628: needs $env{'form.datatoken'},
                   10629: sets $env{'form.upfile'} to the contents of the file
1.41      ng       10630: 
                   10631: =cut
1.31      albertel 10632: 
                   10633: sub load_tmp_file {
                   10634:     my $r=shift;
                   10635:     my @studentdata=();
                   10636:     {
1.158     raeburn  10637:         my $studentfile = $r->dir_config('lonDaemons').
1.258     albertel 10638:                               '/tmp/'.$env{'form.datatoken'}.'.tmp';
1.158     raeburn  10639:         if ( open(my $fh,"<$studentfile") ) {
                   10640:             @studentdata=<$fh>;
                   10641:             close($fh);
                   10642:         }
1.31      albertel 10643:     }
1.258     albertel 10644:     $env{'form.upfile'}=join('',@studentdata);
1.31      albertel 10645: }
                   10646: 
1.56      matthew  10647: =pod
                   10648: 
1.648     raeburn  10649: =item * &upfile_record_sep()
1.41      ng       10650: 
                   10651: Separate uploaded file into records
                   10652: returns array of records,
1.258     albertel 10653: needs $env{'form.upfile'} and $env{'form.upfiletype'}
1.41      ng       10654: 
                   10655: =cut
1.31      albertel 10656: 
                   10657: sub upfile_record_sep {
1.258     albertel 10658:     if ($env{'form.upfiletype'} eq 'xml') {
1.31      albertel 10659:     } else {
1.248     albertel 10660: 	my @records;
1.258     albertel 10661: 	foreach my $line (split(/\n/,$env{'form.upfile'})) {
1.248     albertel 10662: 	    if ($line=~/^\s*$/) { next; }
                   10663: 	    push(@records,$line);
                   10664: 	}
                   10665: 	return @records;
1.31      albertel 10666:     }
                   10667: }
                   10668: 
1.56      matthew  10669: =pod
                   10670: 
1.648     raeburn  10671: =item * &record_sep($record)
1.41      ng       10672: 
1.258     albertel 10673: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $env{'form.upfiletype'}
1.41      ng       10674: 
                   10675: =cut
                   10676: 
1.263     www      10677: sub takeleft {
                   10678:     my $index=shift;
                   10679:     return substr('0000'.$index,-4,4);
                   10680: }
                   10681: 
1.31      albertel 10682: sub record_sep {
                   10683:     my $record=shift;
                   10684:     my %components=();
1.258     albertel 10685:     if ($env{'form.upfiletype'} eq 'xml') {
                   10686:     } elsif ($env{'form.upfiletype'} eq 'space') {
1.31      albertel 10687:         my $i=0;
1.356     albertel 10688:         foreach my $field (split(/\s+/,$record)) {
1.31      albertel 10689:             $field=~s/^(\"|\')//;
                   10690:             $field=~s/(\"|\')$//;
1.263     www      10691:             $components{&takeleft($i)}=$field;
1.31      albertel 10692:             $i++;
                   10693:         }
1.258     albertel 10694:     } elsif ($env{'form.upfiletype'} eq 'tab') {
1.31      albertel 10695:         my $i=0;
1.356     albertel 10696:         foreach my $field (split(/\t/,$record)) {
1.31      albertel 10697:             $field=~s/^(\"|\')//;
                   10698:             $field=~s/(\"|\')$//;
1.263     www      10699:             $components{&takeleft($i)}=$field;
1.31      albertel 10700:             $i++;
                   10701:         }
                   10702:     } else {
1.561     www      10703:         my $separator=',';
1.480     banghart 10704:         if ($env{'form.upfiletype'} eq 'semisv') {
1.561     www      10705:             $separator=';';
1.480     banghart 10706:         }
1.31      albertel 10707:         my $i=0;
1.561     www      10708: # the character we are looking for to indicate the end of a quote or a record 
                   10709:         my $looking_for=$separator;
                   10710: # do not add the characters to the fields
                   10711:         my $ignore=0;
                   10712: # we just encountered a separator (or the beginning of the record)
                   10713:         my $just_found_separator=1;
                   10714: # store the field we are working on here
                   10715:         my $field='';
                   10716: # work our way through all characters in record
                   10717:         foreach my $character ($record=~/(.)/g) {
                   10718:             if ($character eq $looking_for) {
                   10719:                if ($character ne $separator) {
                   10720: # Found the end of a quote, again looking for separator
                   10721:                   $looking_for=$separator;
                   10722:                   $ignore=1;
                   10723:                } else {
                   10724: # Found a separator, store away what we got
                   10725:                   $components{&takeleft($i)}=$field;
                   10726: 	          $i++;
                   10727:                   $just_found_separator=1;
                   10728:                   $ignore=0;
                   10729:                   $field='';
                   10730:                }
                   10731:                next;
                   10732:             }
                   10733: # single or double quotation marks after a separator indicate beginning of a quote
                   10734: # we are now looking for the end of the quote and need to ignore separators
                   10735:             if ((($character eq '"') || ($character eq "'")) && ($just_found_separator))  {
                   10736:                $looking_for=$character;
                   10737:                next;
                   10738:             }
                   10739: # ignore would be true after we reached the end of a quote
                   10740:             if ($ignore) { next; }
                   10741:             if (($just_found_separator) && ($character=~/\s/)) { next; }
                   10742:             $field.=$character;
                   10743:             $just_found_separator=0; 
1.31      albertel 10744:         }
1.561     www      10745: # catch the very last entry, since we never encountered the separator
                   10746:         $components{&takeleft($i)}=$field;
1.31      albertel 10747:     }
                   10748:     return %components;
                   10749: }
                   10750: 
1.144     matthew  10751: ######################################################
                   10752: ######################################################
                   10753: 
1.56      matthew  10754: =pod
                   10755: 
1.648     raeburn  10756: =item * &upfile_select_html()
1.41      ng       10757: 
1.144     matthew  10758: Return HTML code to select a file from the users machine and specify 
                   10759: the file type.
1.41      ng       10760: 
                   10761: =cut
                   10762: 
1.144     matthew  10763: ######################################################
                   10764: ######################################################
1.31      albertel 10765: sub upfile_select_html {
1.144     matthew  10766:     my %Types = (
                   10767:                  csv   => &mt('CSV (comma separated values, spreadsheet)'),
1.480     banghart 10768:                  semisv => &mt('Semicolon separated values'),
1.144     matthew  10769:                  space => &mt('Space separated'),
                   10770:                  tab   => &mt('Tabulator separated'),
                   10771: #                 xml   => &mt('HTML/XML'),
                   10772:                  );
                   10773:     my $Str = '<input type="file" name="upfile" size="50" />'.
1.727     riegler  10774:         '<br />'.&mt('Type').': <select name="upfiletype">';
1.144     matthew  10775:     foreach my $type (sort(keys(%Types))) {
                   10776:         $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
                   10777:     }
                   10778:     $Str .= "</select>\n";
                   10779:     return $Str;
1.31      albertel 10780: }
                   10781: 
1.301     albertel 10782: sub get_samples {
                   10783:     my ($records,$toget) = @_;
                   10784:     my @samples=({});
                   10785:     my $got=0;
                   10786:     foreach my $rec (@$records) {
                   10787: 	my %temp = &record_sep($rec);
                   10788: 	if (! grep(/\S/, values(%temp))) { next; }
                   10789: 	if (%temp) {
                   10790: 	    $samples[$got]=\%temp;
                   10791: 	    $got++;
                   10792: 	    if ($got == $toget) { last; }
                   10793: 	}
                   10794:     }
                   10795:     return \@samples;
                   10796: }
                   10797: 
1.144     matthew  10798: ######################################################
                   10799: ######################################################
                   10800: 
1.56      matthew  10801: =pod
                   10802: 
1.648     raeburn  10803: =item * &csv_print_samples($r,$records)
1.41      ng       10804: 
                   10805: Prints a table of sample values from each column uploaded $r is an
                   10806: Apache Request ref, $records is an arrayref from
                   10807: &Apache::loncommon::upfile_record_sep
                   10808: 
                   10809: =cut
                   10810: 
1.144     matthew  10811: ######################################################
                   10812: ######################################################
1.31      albertel 10813: sub csv_print_samples {
                   10814:     my ($r,$records) = @_;
1.662     bisitz   10815:     my $samples = &get_samples($records,5);
1.301     albertel 10816: 
1.594     raeburn  10817:     $r->print(&mt('Samples').'<br />'.&start_data_table().
                   10818:               &start_data_table_header_row());
1.356     albertel 10819:     foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) { 
1.845     bisitz   10820:         $r->print('<th>'.&mt('Column [_1]',($sample+1)).'</th>'); }
1.594     raeburn  10821:     $r->print(&end_data_table_header_row());
1.301     albertel 10822:     foreach my $hash (@$samples) {
1.594     raeburn  10823: 	$r->print(&start_data_table_row());
1.356     albertel 10824: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
1.31      albertel 10825: 	    $r->print('<td>');
1.356     albertel 10826: 	    if (defined($$hash{$sample})) { $r->print($$hash{$sample}); }
1.31      albertel 10827: 	    $r->print('</td>');
                   10828: 	}
1.594     raeburn  10829: 	$r->print(&end_data_table_row());
1.31      albertel 10830:     }
1.594     raeburn  10831:     $r->print(&end_data_table().'<br />'."\n");
1.31      albertel 10832: }
                   10833: 
1.144     matthew  10834: ######################################################
                   10835: ######################################################
                   10836: 
1.56      matthew  10837: =pod
                   10838: 
1.648     raeburn  10839: =item * &csv_print_select_table($r,$records,$d)
1.41      ng       10840: 
                   10841: Prints a table to create associations between values and table columns.
1.144     matthew  10842: 
1.41      ng       10843: $r is an Apache Request ref,
                   10844: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
1.174     matthew  10845: $d is an array of 2 element arrays (internal name, displayed name,defaultcol)
1.41      ng       10846: 
                   10847: =cut
                   10848: 
1.144     matthew  10849: ######################################################
                   10850: ######################################################
1.31      albertel 10851: sub csv_print_select_table {
                   10852:     my ($r,$records,$d) = @_;
1.301     albertel 10853:     my $i=0;
                   10854:     my $samples = &get_samples($records,1);
1.144     matthew  10855:     $r->print(&mt('Associate columns with student attributes.')."\n".
1.594     raeburn  10856: 	      &start_data_table().&start_data_table_header_row().
1.144     matthew  10857:               '<th>'.&mt('Attribute').'</th>'.
1.594     raeburn  10858:               '<th>'.&mt('Column').'</th>'.
                   10859:               &end_data_table_header_row()."\n");
1.356     albertel 10860:     foreach my $array_ref (@$d) {
                   10861: 	my ($value,$display,$defaultcol)=@{ $array_ref };
1.729     raeburn  10862: 	$r->print(&start_data_table_row().'<td>'.$display.'</td>');
1.31      albertel 10863: 
1.875     bisitz   10864: 	$r->print('<td><select name="f'.$i.'"'.
1.32      matthew  10865: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.31      albertel 10866: 	$r->print('<option value="none"></option>');
1.356     albertel 10867: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
                   10868: 	    $r->print('<option value="'.$sample.'"'.
                   10869:                       ($sample eq $defaultcol ? ' selected="selected" ' : '').
1.662     bisitz   10870:                       '>'.&mt('Column [_1]',($sample+1)).'</option>');
1.31      albertel 10871: 	}
1.594     raeburn  10872: 	$r->print('</select></td>'.&end_data_table_row()."\n");
1.31      albertel 10873: 	$i++;
                   10874:     }
1.594     raeburn  10875:     $r->print(&end_data_table());
1.31      albertel 10876:     $i--;
                   10877:     return $i;
                   10878: }
1.56      matthew  10879: 
1.144     matthew  10880: ######################################################
                   10881: ######################################################
                   10882: 
1.56      matthew  10883: =pod
1.31      albertel 10884: 
1.648     raeburn  10885: =item * &csv_samples_select_table($r,$records,$d)
1.41      ng       10886: 
                   10887: Prints a table of sample values from the upload and can make associate samples to internal names.
                   10888: 
                   10889: $r is an Apache Request ref,
                   10890: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
                   10891: $d is an array of 2 element arrays (internal name, displayed name)
                   10892: 
                   10893: =cut
                   10894: 
1.144     matthew  10895: ######################################################
                   10896: ######################################################
1.31      albertel 10897: sub csv_samples_select_table {
                   10898:     my ($r,$records,$d) = @_;
                   10899:     my $i=0;
1.144     matthew  10900:     #
1.662     bisitz   10901:     my $max_samples = 5;
                   10902:     my $samples = &get_samples($records,$max_samples);
1.594     raeburn  10903:     $r->print(&start_data_table().
                   10904:               &start_data_table_header_row().'<th>'.
                   10905:               &mt('Field').'</th><th>'.&mt('Samples').'</th>'.
                   10906:               &end_data_table_header_row());
1.301     albertel 10907: 
                   10908:     foreach my $key (sort(keys(%{ $samples->[0] }))) {
1.594     raeburn  10909: 	$r->print(&start_data_table_row().'<td><select name="f'.$i.'"'.
1.32      matthew  10910: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.301     albertel 10911: 	foreach my $option (@$d) {
                   10912: 	    my ($value,$display,$defaultcol)=@{ $option };
1.174     matthew  10913: 	    $r->print('<option value="'.$value.'"'.
1.253     albertel 10914:                       ($i eq $defaultcol ? ' selected="selected" ':'').'>'.
1.174     matthew  10915:                       $display.'</option>');
1.31      albertel 10916: 	}
                   10917: 	$r->print('</select></td><td>');
1.662     bisitz   10918: 	foreach my $line (0..($max_samples-1)) {
1.301     albertel 10919: 	    if (defined($samples->[$line]{$key})) { 
                   10920: 		$r->print($samples->[$line]{$key}."<br />\n"); 
                   10921: 	    }
                   10922: 	}
1.594     raeburn  10923: 	$r->print('</td>'.&end_data_table_row());
1.31      albertel 10924: 	$i++;
                   10925:     }
1.594     raeburn  10926:     $r->print(&end_data_table());
1.31      albertel 10927:     $i--;
                   10928:     return($i);
1.115     matthew  10929: }
                   10930: 
1.144     matthew  10931: ######################################################
                   10932: ######################################################
                   10933: 
1.115     matthew  10934: =pod
                   10935: 
1.648     raeburn  10936: =item * &clean_excel_name($name)
1.115     matthew  10937: 
                   10938: Returns a replacement for $name which does not contain any illegal characters.
                   10939: 
                   10940: =cut
                   10941: 
1.144     matthew  10942: ######################################################
                   10943: ######################################################
1.115     matthew  10944: sub clean_excel_name {
                   10945:     my ($name) = @_;
                   10946:     $name =~ s/[:\*\?\/\\]//g;
                   10947:     if (length($name) > 31) {
                   10948:         $name = substr($name,0,31);
                   10949:     }
                   10950:     return $name;
1.25      albertel 10951: }
1.84      albertel 10952: 
1.85      albertel 10953: =pod
                   10954: 
1.648     raeburn  10955: =item * &check_if_partid_hidden($id,$symb,$udom,$uname)
1.85      albertel 10956: 
                   10957: Returns either 1 or undef
                   10958: 
                   10959: 1 if the part is to be hidden, undef if it is to be shown
                   10960: 
                   10961: Arguments are:
                   10962: 
                   10963: $id the id of the part to be checked
                   10964: $symb, optional the symb of the resource to check
                   10965: $udom, optional the domain of the user to check for
                   10966: $uname, optional the username of the user to check for
                   10967: 
                   10968: =cut
1.84      albertel 10969: 
                   10970: sub check_if_partid_hidden {
                   10971:     my ($id,$symb,$udom,$uname) = @_;
1.133     albertel 10972:     my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
1.84      albertel 10973: 					 $symb,$udom,$uname);
1.141     albertel 10974:     my $truth=1;
                   10975:     #if the string starts with !, then the list is the list to show not hide
                   10976:     if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
1.84      albertel 10977:     my @hiddenlist=split(/,/,$hiddenparts);
                   10978:     foreach my $checkid (@hiddenlist) {
1.141     albertel 10979: 	if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
1.84      albertel 10980:     }
1.141     albertel 10981:     return !$truth;
1.84      albertel 10982: }
1.127     matthew  10983: 
1.138     matthew  10984: 
                   10985: ############################################################
                   10986: ############################################################
                   10987: 
                   10988: =pod
                   10989: 
1.157     matthew  10990: =back 
                   10991: 
1.138     matthew  10992: =head1 cgi-bin script and graphing routines
                   10993: 
1.157     matthew  10994: =over 4
                   10995: 
1.648     raeburn  10996: =item * &get_cgi_id()
1.138     matthew  10997: 
                   10998: Inputs: none
                   10999: 
                   11000: Returns an id which can be used to pass environment variables
                   11001: to various cgi-bin scripts.  These environment variables will
                   11002: be removed from the users environment after a given time by
                   11003: the routine &Apache::lonnet::transfer_profile_to_env.
                   11004: 
                   11005: =cut
                   11006: 
                   11007: ############################################################
                   11008: ############################################################
1.152     albertel 11009: my $uniq=0;
1.136     matthew  11010: sub get_cgi_id {
1.154     albertel 11011:     $uniq=($uniq+1)%100000;
1.280     albertel 11012:     return (time.'_'.$$.'_'.$uniq);
1.136     matthew  11013: }
                   11014: 
1.127     matthew  11015: ############################################################
                   11016: ############################################################
                   11017: 
                   11018: =pod
                   11019: 
1.648     raeburn  11020: =item * &DrawBarGraph()
1.127     matthew  11021: 
1.138     matthew  11022: Facilitates the plotting of data in a (stacked) bar graph.
                   11023: Puts plot definition data into the users environment in order for 
                   11024: graph.png to plot it.  Returns an <img> tag for the plot.
                   11025: The bars on the plot are labeled '1','2',...,'n'.
                   11026: 
                   11027: Inputs:
                   11028: 
                   11029: =over 4
                   11030: 
                   11031: =item $Title: string, the title of the plot
                   11032: 
                   11033: =item $xlabel: string, text describing the X-axis of the plot
                   11034: 
                   11035: =item $ylabel: string, text describing the Y-axis of the plot
                   11036: 
                   11037: =item $Max: scalar, the maximum Y value to use in the plot
                   11038: If $Max is < any data point, the graph will not be rendered.
                   11039: 
1.140     matthew  11040: =item $colors: array ref holding the colors to be used for the data sets when
1.138     matthew  11041: they are plotted.  If undefined, default values will be used.
                   11042: 
1.178     matthew  11043: =item $labels: array ref holding the labels to use on the x-axis for the bars.
                   11044: 
1.138     matthew  11045: =item @Values: An array of array references.  Each array reference holds data
                   11046: to be plotted in a stacked bar chart.
                   11047: 
1.239     matthew  11048: =item If the final element of @Values is a hash reference the key/value
                   11049: pairs will be added to the graph definition.
                   11050: 
1.138     matthew  11051: =back
                   11052: 
                   11053: Returns:
                   11054: 
                   11055: An <img> tag which references graph.png and the appropriate identifying
                   11056: information for the plot.
                   11057: 
1.127     matthew  11058: =cut
                   11059: 
                   11060: ############################################################
                   11061: ############################################################
1.134     matthew  11062: sub DrawBarGraph {
1.178     matthew  11063:     my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
1.134     matthew  11064:     #
                   11065:     if (! defined($colors)) {
                   11066:         $colors = ['#33ff00', 
                   11067:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
                   11068:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
                   11069:                   ]; 
                   11070:     }
1.228     matthew  11071:     my $extra_settings = {};
                   11072:     if (ref($Values[-1]) eq 'HASH') {
                   11073:         $extra_settings = pop(@Values);
                   11074:     }
1.127     matthew  11075:     #
1.136     matthew  11076:     my $identifier = &get_cgi_id();
                   11077:     my $id = 'cgi.'.$identifier;        
1.129     matthew  11078:     if (! @Values || ref($Values[0]) ne 'ARRAY') {
1.127     matthew  11079:         return '';
                   11080:     }
1.225     matthew  11081:     #
                   11082:     my @Labels;
                   11083:     if (defined($labels)) {
                   11084:         @Labels = @$labels;
                   11085:     } else {
                   11086:         for (my $i=0;$i<@{$Values[0]};$i++) {
                   11087:             push (@Labels,$i+1);
                   11088:         }
                   11089:     }
                   11090:     #
1.129     matthew  11091:     my $NumBars = scalar(@{$Values[0]});
1.225     matthew  11092:     if ($NumBars < scalar(@Labels)) { $NumBars = scalar(@Labels); }
1.129     matthew  11093:     my %ValuesHash;
                   11094:     my $NumSets=1;
                   11095:     foreach my $array (@Values) {
                   11096:         next if (! ref($array));
1.136     matthew  11097:         $ValuesHash{$id.'.data.'.$NumSets++} = 
1.132     matthew  11098:             join(',',@$array);
1.129     matthew  11099:     }
1.127     matthew  11100:     #
1.136     matthew  11101:     my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
1.225     matthew  11102:     if ($NumBars < 3) {
                   11103:         $width = 120+$NumBars*32;
1.220     matthew  11104:         $xskip = 1;
1.225     matthew  11105:         $bar_width = 30;
                   11106:     } elsif ($NumBars < 5) {
                   11107:         $width = 120+$NumBars*20;
                   11108:         $xskip = 1;
                   11109:         $bar_width = 20;
1.220     matthew  11110:     } elsif ($NumBars < 10) {
1.136     matthew  11111:         $width = 120+$NumBars*15;
                   11112:         $xskip = 1;
                   11113:         $bar_width = 15;
                   11114:     } elsif ($NumBars <= 25) {
                   11115:         $width = 120+$NumBars*11;
                   11116:         $xskip = 5;
                   11117:         $bar_width = 8;
                   11118:     } elsif ($NumBars <= 50) {
                   11119:         $width = 120+$NumBars*8;
                   11120:         $xskip = 5;
                   11121:         $bar_width = 4;
                   11122:     } else {
                   11123:         $width = 120+$NumBars*8;
                   11124:         $xskip = 5;
                   11125:         $bar_width = 4;
                   11126:     }
                   11127:     #
1.137     matthew  11128:     $Max = 1 if ($Max < 1);
                   11129:     if ( int($Max) < $Max ) {
                   11130:         $Max++;
                   11131:         $Max = int($Max);
                   11132:     }
1.127     matthew  11133:     $Title  = '' if (! defined($Title));
                   11134:     $xlabel = '' if (! defined($xlabel));
                   11135:     $ylabel = '' if (! defined($ylabel));
1.369     www      11136:     $ValuesHash{$id.'.title'}    = &escape($Title);
                   11137:     $ValuesHash{$id.'.xlabel'}   = &escape($xlabel);
                   11138:     $ValuesHash{$id.'.ylabel'}   = &escape($ylabel);
1.137     matthew  11139:     $ValuesHash{$id.'.y_max_value'} = $Max;
1.136     matthew  11140:     $ValuesHash{$id.'.NumBars'}  = $NumBars;
                   11141:     $ValuesHash{$id.'.NumSets'}  = $NumSets;
                   11142:     $ValuesHash{$id.'.PlotType'} = 'bar';
                   11143:     $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   11144:     $ValuesHash{$id.'.height'}   = $height;
                   11145:     $ValuesHash{$id.'.width'}    = $width;
                   11146:     $ValuesHash{$id.'.xskip'}    = $xskip;
                   11147:     $ValuesHash{$id.'.bar_width'} = $bar_width;
                   11148:     $ValuesHash{$id.'.labels'} = join(',',@Labels);
1.127     matthew  11149:     #
1.228     matthew  11150:     # Deal with other parameters
                   11151:     while (my ($key,$value) = each(%$extra_settings)) {
                   11152:         $ValuesHash{$id.'.'.$key} = $value;
                   11153:     }
                   11154:     #
1.646     raeburn  11155:     &Apache::lonnet::appenv(\%ValuesHash);
1.137     matthew  11156:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   11157: }
                   11158: 
                   11159: ############################################################
                   11160: ############################################################
                   11161: 
                   11162: =pod
                   11163: 
1.648     raeburn  11164: =item * &DrawXYGraph()
1.137     matthew  11165: 
1.138     matthew  11166: Facilitates the plotting of data in an XY graph.
                   11167: Puts plot definition data into the users environment in order for 
                   11168: graph.png to plot it.  Returns an <img> tag for the plot.
                   11169: 
                   11170: Inputs:
                   11171: 
                   11172: =over 4
                   11173: 
                   11174: =item $Title: string, the title of the plot
                   11175: 
                   11176: =item $xlabel: string, text describing the X-axis of the plot
                   11177: 
                   11178: =item $ylabel: string, text describing the Y-axis of the plot
                   11179: 
                   11180: =item $Max: scalar, the maximum Y value to use in the plot
                   11181: If $Max is < any data point, the graph will not be rendered.
                   11182: 
                   11183: =item $colors: Array ref containing the hex color codes for the data to be 
                   11184: plotted in.  If undefined, default values will be used.
                   11185: 
                   11186: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   11187: 
                   11188: =item $Ydata: Array ref containing Array refs.  
1.185     www      11189: Each of the contained arrays will be plotted as a separate curve.
1.138     matthew  11190: 
                   11191: =item %Values: hash indicating or overriding any default values which are 
                   11192: passed to graph.png.  
                   11193: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   11194: 
                   11195: =back
                   11196: 
                   11197: Returns:
                   11198: 
                   11199: An <img> tag which references graph.png and the appropriate identifying
                   11200: information for the plot.
                   11201: 
1.137     matthew  11202: =cut
                   11203: 
                   11204: ############################################################
                   11205: ############################################################
                   11206: sub DrawXYGraph {
                   11207:     my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
                   11208:     #
                   11209:     # Create the identifier for the graph
                   11210:     my $identifier = &get_cgi_id();
                   11211:     my $id = 'cgi.'.$identifier;
                   11212:     #
                   11213:     $Title  = '' if (! defined($Title));
                   11214:     $xlabel = '' if (! defined($xlabel));
                   11215:     $ylabel = '' if (! defined($ylabel));
                   11216:     my %ValuesHash = 
                   11217:         (
1.369     www      11218:          $id.'.title'  => &escape($Title),
                   11219:          $id.'.xlabel' => &escape($xlabel),
                   11220:          $id.'.ylabel' => &escape($ylabel),
1.137     matthew  11221:          $id.'.y_max_value'=> $Max,
                   11222:          $id.'.labels'     => join(',',@$Xlabels),
                   11223:          $id.'.PlotType'   => 'XY',
                   11224:          );
                   11225:     #
                   11226:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   11227:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   11228:     }
                   11229:     #
                   11230:     if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
                   11231:         return '';
                   11232:     }
                   11233:     my $NumSets=1;
1.138     matthew  11234:     foreach my $array (@{$Ydata}){
1.137     matthew  11235:         next if (! ref($array));
                   11236:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
                   11237:     }
1.138     matthew  11238:     $ValuesHash{$id.'.NumSets'} = $NumSets-1;
1.137     matthew  11239:     #
                   11240:     # Deal with other parameters
                   11241:     while (my ($key,$value) = each(%Values)) {
                   11242:         $ValuesHash{$id.'.'.$key} = $value;
1.127     matthew  11243:     }
                   11244:     #
1.646     raeburn  11245:     &Apache::lonnet::appenv(\%ValuesHash);
1.136     matthew  11246:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   11247: }
                   11248: 
                   11249: ############################################################
                   11250: ############################################################
                   11251: 
                   11252: =pod
                   11253: 
1.648     raeburn  11254: =item * &DrawXYYGraph()
1.138     matthew  11255: 
                   11256: Facilitates the plotting of data in an XY graph with two Y axes.
                   11257: Puts plot definition data into the users environment in order for 
                   11258: graph.png to plot it.  Returns an <img> tag for the plot.
                   11259: 
                   11260: Inputs:
                   11261: 
                   11262: =over 4
                   11263: 
                   11264: =item $Title: string, the title of the plot
                   11265: 
                   11266: =item $xlabel: string, text describing the X-axis of the plot
                   11267: 
                   11268: =item $ylabel: string, text describing the Y-axis of the plot
                   11269: 
                   11270: =item $colors: Array ref containing the hex color codes for the data to be 
                   11271: plotted in.  If undefined, default values will be used.
                   11272: 
                   11273: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   11274: 
                   11275: =item $Ydata1: The first data set
                   11276: 
                   11277: =item $Min1: The minimum value of the left Y-axis
                   11278: 
                   11279: =item $Max1: The maximum value of the left Y-axis
                   11280: 
                   11281: =item $Ydata2: The second data set
                   11282: 
                   11283: =item $Min2: The minimum value of the right Y-axis
                   11284: 
                   11285: =item $Max2: The maximum value of the left Y-axis
                   11286: 
                   11287: =item %Values: hash indicating or overriding any default values which are 
                   11288: passed to graph.png.  
                   11289: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   11290: 
                   11291: =back
                   11292: 
                   11293: Returns:
                   11294: 
                   11295: An <img> tag which references graph.png and the appropriate identifying
                   11296: information for the plot.
1.136     matthew  11297: 
                   11298: =cut
                   11299: 
                   11300: ############################################################
                   11301: ############################################################
1.137     matthew  11302: sub DrawXYYGraph {
                   11303:     my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
                   11304:                                         $Ydata2,$Min2,$Max2,%Values)=@_;
1.136     matthew  11305:     #
                   11306:     # Create the identifier for the graph
                   11307:     my $identifier = &get_cgi_id();
                   11308:     my $id = 'cgi.'.$identifier;
                   11309:     #
                   11310:     $Title  = '' if (! defined($Title));
                   11311:     $xlabel = '' if (! defined($xlabel));
                   11312:     $ylabel = '' if (! defined($ylabel));
                   11313:     my %ValuesHash = 
                   11314:         (
1.369     www      11315:          $id.'.title'  => &escape($Title),
                   11316:          $id.'.xlabel' => &escape($xlabel),
                   11317:          $id.'.ylabel' => &escape($ylabel),
1.136     matthew  11318:          $id.'.labels' => join(',',@$Xlabels),
                   11319:          $id.'.PlotType' => 'XY',
                   11320:          $id.'.NumSets' => 2,
1.137     matthew  11321:          $id.'.two_axes' => 1,
                   11322:          $id.'.y1_max_value' => $Max1,
                   11323:          $id.'.y1_min_value' => $Min1,
                   11324:          $id.'.y2_max_value' => $Max2,
                   11325:          $id.'.y2_min_value' => $Min2,
1.136     matthew  11326:          );
                   11327:     #
1.137     matthew  11328:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   11329:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   11330:     }
                   11331:     #
                   11332:     if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
                   11333:         ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
1.136     matthew  11334:         return '';
                   11335:     }
                   11336:     my $NumSets=1;
1.137     matthew  11337:     foreach my $array ($Ydata1,$Ydata2){
1.136     matthew  11338:         next if (! ref($array));
                   11339:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
1.137     matthew  11340:     }
                   11341:     #
                   11342:     # Deal with other parameters
                   11343:     while (my ($key,$value) = each(%Values)) {
                   11344:         $ValuesHash{$id.'.'.$key} = $value;
1.136     matthew  11345:     }
                   11346:     #
1.646     raeburn  11347:     &Apache::lonnet::appenv(\%ValuesHash);
1.130     albertel 11348:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
1.139     matthew  11349: }
                   11350: 
                   11351: ############################################################
                   11352: ############################################################
                   11353: 
                   11354: =pod
                   11355: 
1.157     matthew  11356: =back 
                   11357: 
1.139     matthew  11358: =head1 Statistics helper routines?  
                   11359: 
                   11360: Bad place for them but what the hell.
                   11361: 
1.157     matthew  11362: =over 4
                   11363: 
1.648     raeburn  11364: =item * &chartlink()
1.139     matthew  11365: 
                   11366: Returns a link to the chart for a specific student.  
                   11367: 
                   11368: Inputs:
                   11369: 
                   11370: =over 4
                   11371: 
                   11372: =item $linktext: The text of the link
                   11373: 
                   11374: =item $sname: The students username
                   11375: 
                   11376: =item $sdomain: The students domain
                   11377: 
                   11378: =back
                   11379: 
1.157     matthew  11380: =back
                   11381: 
1.139     matthew  11382: =cut
                   11383: 
                   11384: ############################################################
                   11385: ############################################################
                   11386: sub chartlink {
                   11387:     my ($linktext, $sname, $sdomain) = @_;
                   11388:     my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
1.369     www      11389:         '&amp;SelectedStudent='.&escape($sname.':'.$sdomain).
1.219     albertel 11390:         '&amp;chartoutputmode='.HTML::Entities::encode('html, with all links').
1.139     matthew  11391:        '">'.$linktext.'</a>';
1.153     matthew  11392: }
                   11393: 
                   11394: #######################################################
                   11395: #######################################################
                   11396: 
                   11397: =pod
                   11398: 
                   11399: =head1 Course Environment Routines
1.157     matthew  11400: 
                   11401: =over 4
1.153     matthew  11402: 
1.648     raeburn  11403: =item * &restore_course_settings()
1.153     matthew  11404: 
1.648     raeburn  11405: =item * &store_course_settings()
1.153     matthew  11406: 
                   11407: Restores/Store indicated form parameters from the course environment.
                   11408: Will not overwrite existing values of the form parameters.
                   11409: 
                   11410: Inputs: 
                   11411: a scalar describing the data (e.g. 'chart', 'problem_analysis')
                   11412: 
                   11413: a hash ref describing the data to be stored.  For example:
                   11414:    
                   11415: %Save_Parameters = ('Status' => 'scalar',
                   11416:     'chartoutputmode' => 'scalar',
                   11417:     'chartoutputdata' => 'scalar',
                   11418:     'Section' => 'array',
1.373     raeburn  11419:     'Group' => 'array',
1.153     matthew  11420:     'StudentData' => 'array',
                   11421:     'Maps' => 'array');
                   11422: 
                   11423: Returns: both routines return nothing
                   11424: 
1.631     raeburn  11425: =back
                   11426: 
1.153     matthew  11427: =cut
                   11428: 
                   11429: #######################################################
                   11430: #######################################################
                   11431: sub store_course_settings {
1.496     albertel 11432:     return &store_settings($env{'request.course.id'},@_);
                   11433: }
                   11434: 
                   11435: sub store_settings {
1.153     matthew  11436:     # save to the environment
                   11437:     # appenv the same items, just to be safe
1.300     albertel 11438:     my $udom  = $env{'user.domain'};
                   11439:     my $uname = $env{'user.name'};
1.496     albertel 11440:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  11441:     my %SaveHash;
                   11442:     my %AppHash;
                   11443:     while (my ($setting,$type) = each(%$Settings)) {
1.496     albertel 11444:         my $basename = join('.','internal',$context,$prefix,$setting);
1.300     albertel 11445:         my $envname = 'environment.'.$basename;
1.258     albertel 11446:         if (exists($env{'form.'.$setting})) {
1.153     matthew  11447:             # Save this value away
                   11448:             if ($type eq 'scalar' &&
1.258     albertel 11449:                 (! exists($env{$envname}) || 
                   11450:                  $env{$envname} ne $env{'form.'.$setting})) {
                   11451:                 $SaveHash{$basename} = $env{'form.'.$setting};
                   11452:                 $AppHash{$envname}   = $env{'form.'.$setting};
1.153     matthew  11453:             } elsif ($type eq 'array') {
                   11454:                 my $stored_form;
1.258     albertel 11455:                 if (ref($env{'form.'.$setting})) {
1.153     matthew  11456:                     $stored_form = join(',',
                   11457:                                         map {
1.369     www      11458:                                             &escape($_);
1.258     albertel 11459:                                         } sort(@{$env{'form.'.$setting}}));
1.153     matthew  11460:                 } else {
                   11461:                     $stored_form = 
1.369     www      11462:                         &escape($env{'form.'.$setting});
1.153     matthew  11463:                 }
                   11464:                 # Determine if the array contents are the same.
1.258     albertel 11465:                 if ($stored_form ne $env{$envname}) {
1.153     matthew  11466:                     $SaveHash{$basename} = $stored_form;
                   11467:                     $AppHash{$envname}   = $stored_form;
                   11468:                 }
                   11469:             }
                   11470:         }
                   11471:     }
                   11472:     my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
1.300     albertel 11473:                                           $udom,$uname);
1.153     matthew  11474:     if ($put_result !~ /^(ok|delayed)/) {
                   11475:         &Apache::lonnet::logthis('unable to save form parameters, '.
                   11476:                                  'got error:'.$put_result);
                   11477:     }
                   11478:     # Make sure these settings stick around in this session, too
1.646     raeburn  11479:     &Apache::lonnet::appenv(\%AppHash);
1.153     matthew  11480:     return;
                   11481: }
                   11482: 
                   11483: sub restore_course_settings {
1.499     albertel 11484:     return &restore_settings($env{'request.course.id'},@_);
1.496     albertel 11485: }
                   11486: 
                   11487: sub restore_settings {
                   11488:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  11489:     while (my ($setting,$type) = each(%$Settings)) {
1.258     albertel 11490:         next if (exists($env{'form.'.$setting}));
1.496     albertel 11491:         my $envname = 'environment.internal.'.$context.'.'.$prefix.
1.153     matthew  11492:             '.'.$setting;
1.258     albertel 11493:         if (exists($env{$envname})) {
1.153     matthew  11494:             if ($type eq 'scalar') {
1.258     albertel 11495:                 $env{'form.'.$setting} = $env{$envname};
1.153     matthew  11496:             } elsif ($type eq 'array') {
1.258     albertel 11497:                 $env{'form.'.$setting} = [ 
1.153     matthew  11498:                                            map { 
1.369     www      11499:                                                &unescape($_); 
1.258     albertel 11500:                                            } split(',',$env{$envname})
1.153     matthew  11501:                                            ];
                   11502:             }
                   11503:         }
                   11504:     }
1.127     matthew  11505: }
                   11506: 
1.618     raeburn  11507: #######################################################
                   11508: #######################################################
                   11509: 
                   11510: =pod
                   11511: 
                   11512: =head1 Domain E-mail Routines  
                   11513: 
                   11514: =over 4
                   11515: 
1.648     raeburn  11516: =item * &build_recipient_list()
1.618     raeburn  11517: 
1.884     raeburn  11518: Build recipient lists for five types of e-mail:
1.766     raeburn  11519: (a) Error Reports, (b) Package Updates, (c) lonstatus warnings/errors
1.884     raeburn  11520: (d) Help requests, (e) Course requests needing approval,  generated by
                   11521: lonerrorhandler.pm, CHECKRPMS, loncron, lonsupportreq.pm and
                   11522: loncoursequeueadmin.pm respectively.
1.618     raeburn  11523: 
                   11524: Inputs:
1.619     raeburn  11525: defmail (scalar - email address of default recipient), 
1.618     raeburn  11526: mailing type (scalar - errormail, packagesmail, or helpdeskmail), 
1.619     raeburn  11527: defdom (domain for which to retrieve configuration settings),
                   11528: origmail (scalar - email address of recipient from loncapa.conf, 
                   11529: i.e., predates configuration by DC via domainprefs.pm 
1.618     raeburn  11530: 
1.655     raeburn  11531: Returns: comma separated list of addresses to which to send e-mail.
                   11532: 
                   11533: =back
1.618     raeburn  11534: 
                   11535: =cut
                   11536: 
                   11537: ############################################################
                   11538: ############################################################
                   11539: sub build_recipient_list {
1.619     raeburn  11540:     my ($defmail,$mailing,$defdom,$origmail) = @_;
1.618     raeburn  11541:     my @recipients;
                   11542:     my $otheremails;
                   11543:     my %domconfig =
                   11544:          &Apache::lonnet::get_dom('configuration',['contacts'],$defdom);
                   11545:     if (ref($domconfig{'contacts'}) eq 'HASH') {
1.766     raeburn  11546:         if (exists($domconfig{'contacts'}{$mailing})) {
                   11547:             if (ref($domconfig{'contacts'}{$mailing}) eq 'HASH') {
                   11548:                 my @contacts = ('adminemail','supportemail');
                   11549:                 foreach my $item (@contacts) {
                   11550:                     if ($domconfig{'contacts'}{$mailing}{$item}) {
                   11551:                         my $addr = $domconfig{'contacts'}{$item}; 
                   11552:                         if (!grep(/^\Q$addr\E$/,@recipients)) {
                   11553:                             push(@recipients,$addr);
                   11554:                         }
1.619     raeburn  11555:                     }
1.766     raeburn  11556:                     $otheremails = $domconfig{'contacts'}{$mailing}{'others'};
1.618     raeburn  11557:                 }
                   11558:             }
1.766     raeburn  11559:         } elsif ($origmail ne '') {
                   11560:             push(@recipients,$origmail);
1.618     raeburn  11561:         }
1.619     raeburn  11562:     } elsif ($origmail ne '') {
                   11563:         push(@recipients,$origmail);
1.618     raeburn  11564:     }
1.688     raeburn  11565:     if (defined($defmail)) {
                   11566:         if ($defmail ne '') {
                   11567:             push(@recipients,$defmail);
                   11568:         }
1.618     raeburn  11569:     }
                   11570:     if ($otheremails) {
1.619     raeburn  11571:         my @others;
                   11572:         if ($otheremails =~ /,/) {
                   11573:             @others = split(/,/,$otheremails);
1.618     raeburn  11574:         } else {
1.619     raeburn  11575:             push(@others,$otheremails);
                   11576:         }
                   11577:         foreach my $addr (@others) {
                   11578:             if (!grep(/^\Q$addr\E$/,@recipients)) {
                   11579:                 push(@recipients,$addr);
                   11580:             }
1.618     raeburn  11581:         }
                   11582:     }
1.619     raeburn  11583:     my $recipientlist = join(',',@recipients); 
1.618     raeburn  11584:     return $recipientlist;
                   11585: }
                   11586: 
1.127     matthew  11587: ############################################################
                   11588: ############################################################
1.154     albertel 11589: 
1.655     raeburn  11590: =pod
                   11591: 
                   11592: =head1 Course Catalog Routines
                   11593: 
                   11594: =over 4
                   11595: 
                   11596: =item * &gather_categories()
                   11597: 
                   11598: Converts category definitions - keys of categories hash stored in  
                   11599: coursecategories in configuration.db on the primary library server in a 
                   11600: domain - to an array.  Also generates javascript and idx hash used to 
                   11601: generate Domain Coordinator interface for editing Course Categories.
                   11602: 
                   11603: Inputs:
1.663     raeburn  11604: 
1.655     raeburn  11605: categories (reference to hash of category definitions).
1.663     raeburn  11606: 
1.655     raeburn  11607: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   11608:       categories and subcategories).
1.663     raeburn  11609: 
1.655     raeburn  11610: idx (reference to hash of counters used in Domain Coordinator interface for 
                   11611:       editing Course Categories).
1.663     raeburn  11612: 
1.655     raeburn  11613: jsarray (reference to array of categories used to create Javascript arrays for
                   11614:          Domain Coordinator interface for editing Course Categories).
                   11615: 
                   11616: Returns: nothing
                   11617: 
                   11618: Side effects: populates cats, idx and jsarray. 
                   11619: 
                   11620: =cut
                   11621: 
                   11622: sub gather_categories {
                   11623:     my ($categories,$cats,$idx,$jsarray) = @_;
                   11624:     my %counters;
                   11625:     my $num = 0;
                   11626:     foreach my $item (keys(%{$categories})) {
                   11627:         my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
                   11628:         if ($container eq '' && $depth == 0) {
                   11629:             $cats->[$depth][$categories->{$item}] = $cat;
                   11630:         } else {
                   11631:             $cats->[$depth]{$container}[$categories->{$item}] = $cat;
                   11632:         }
                   11633:         my ($escitem,$tail) = split(/:/,$item,2);
                   11634:         if ($counters{$tail} eq '') {
                   11635:             $counters{$tail} = $num;
                   11636:             $num ++;
                   11637:         }
                   11638:         if (ref($idx) eq 'HASH') {
                   11639:             $idx->{$item} = $counters{$tail};
                   11640:         }
                   11641:         if (ref($jsarray) eq 'ARRAY') {
                   11642:             push(@{$jsarray->[$counters{$tail}]},$item);
                   11643:         }
                   11644:     }
                   11645:     return;
                   11646: }
                   11647: 
                   11648: =pod
                   11649: 
                   11650: =item * &extract_categories()
                   11651: 
                   11652: Used to generate breadcrumb trails for course categories.
                   11653: 
                   11654: Inputs:
1.663     raeburn  11655: 
1.655     raeburn  11656: categories (reference to hash of category definitions).
1.663     raeburn  11657: 
1.655     raeburn  11658: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   11659:       categories and subcategories).
1.663     raeburn  11660: 
1.655     raeburn  11661: trails (reference to array of breacrumb trails for each category).
1.663     raeburn  11662: 
1.655     raeburn  11663: allitems (reference to hash - key is category key 
                   11664:          (format: escaped(name):escaped(parent category):depth in hierarchy).
1.663     raeburn  11665: 
1.655     raeburn  11666: idx (reference to hash of counters used in Domain Coordinator interface for
                   11667:       editing Course Categories).
1.663     raeburn  11668: 
1.655     raeburn  11669: jsarray (reference to array of categories used to create Javascript arrays for
                   11670:          Domain Coordinator interface for editing Course Categories).
                   11671: 
1.665     raeburn  11672: subcats (reference to hash of arrays containing all subcategories within each 
                   11673:          category, -recursive)
                   11674: 
1.655     raeburn  11675: Returns: nothing
                   11676: 
                   11677: Side effects: populates trails and allitems hash references.
                   11678: 
                   11679: =cut
                   11680: 
                   11681: sub extract_categories {
1.665     raeburn  11682:     my ($categories,$cats,$trails,$allitems,$idx,$jsarray,$subcats) = @_;
1.655     raeburn  11683:     if (ref($categories) eq 'HASH') {
                   11684:         &gather_categories($categories,$cats,$idx,$jsarray);
                   11685:         if (ref($cats->[0]) eq 'ARRAY') {
                   11686:             for (my $i=0; $i<@{$cats->[0]}; $i++) {
                   11687:                 my $name = $cats->[0][$i];
                   11688:                 my $item = &escape($name).'::0';
                   11689:                 my $trailstr;
                   11690:                 if ($name eq 'instcode') {
                   11691:                     $trailstr = &mt('Official courses (with institutional codes)');
1.919     raeburn  11692:                 } elsif ($name eq 'communities') {
                   11693:                     $trailstr = &mt('Communities');
1.655     raeburn  11694:                 } else {
                   11695:                     $trailstr = $name;
                   11696:                 }
                   11697:                 if ($allitems->{$item} eq '') {
                   11698:                     push(@{$trails},$trailstr);
                   11699:                     $allitems->{$item} = scalar(@{$trails})-1;
                   11700:                 }
                   11701:                 my @parents = ($name);
                   11702:                 if (ref($cats->[1]{$name}) eq 'ARRAY') {
                   11703:                     for (my $j=0; $j<@{$cats->[1]{$name}}; $j++) {
                   11704:                         my $category = $cats->[1]{$name}[$j];
1.665     raeburn  11705:                         if (ref($subcats) eq 'HASH') {
                   11706:                             push(@{$subcats->{$item}},&escape($category).':'.&escape($name).':1');
                   11707:                         }
                   11708:                         &recurse_categories($cats,2,$category,$trails,$allitems,\@parents,$subcats);
                   11709:                     }
                   11710:                 } else {
                   11711:                     if (ref($subcats) eq 'HASH') {
                   11712:                         $subcats->{$item} = [];
1.655     raeburn  11713:                     }
                   11714:                 }
                   11715:             }
                   11716:         }
                   11717:     }
                   11718:     return;
                   11719: }
                   11720: 
                   11721: =pod
                   11722: 
                   11723: =item *&recurse_categories()
                   11724: 
                   11725: Recursively used to generate breadcrumb trails for course categories.
                   11726: 
                   11727: Inputs:
1.663     raeburn  11728: 
1.655     raeburn  11729: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   11730:       categories and subcategories).
1.663     raeburn  11731: 
1.655     raeburn  11732: depth (current depth in hierarchy of categories and sub-categories - 0 indexed).
1.663     raeburn  11733: 
                   11734: category (current course category, for which breadcrumb trail is being generated).
                   11735: 
                   11736: trails (reference to array of breadcrumb trails for each category).
                   11737: 
1.655     raeburn  11738: allitems (reference to hash - key is category key
                   11739:          (format: escaped(name):escaped(parent category):depth in hierarchy).
1.663     raeburn  11740: 
1.655     raeburn  11741: parents (array containing containers directories for current category, 
                   11742:          back to top level). 
                   11743: 
                   11744: Returns: nothing
                   11745: 
                   11746: Side effects: populates trails and allitems hash references
                   11747: 
                   11748: =cut
                   11749: 
                   11750: sub recurse_categories {
1.665     raeburn  11751:     my ($cats,$depth,$category,$trails,$allitems,$parents,$subcats) = @_;
1.655     raeburn  11752:     my $shallower = $depth - 1;
                   11753:     if (ref($cats->[$depth]{$category}) eq 'ARRAY') {
                   11754:         for (my $k=0; $k<@{$cats->[$depth]{$category}}; $k++) {
                   11755:             my $name = $cats->[$depth]{$category}[$k];
                   11756:             my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
                   11757:             my $trailstr = join(' -&gt; ',(@{$parents},$category));
                   11758:             if ($allitems->{$item} eq '') {
                   11759:                 push(@{$trails},$trailstr);
                   11760:                 $allitems->{$item} = scalar(@{$trails})-1;
                   11761:             }
                   11762:             my $deeper = $depth+1;
                   11763:             push(@{$parents},$category);
1.665     raeburn  11764:             if (ref($subcats) eq 'HASH') {
                   11765:                 my $subcat = &escape($name).':'.$category.':'.$depth;
                   11766:                 for (my $j=@{$parents}; $j>=0; $j--) {
                   11767:                     my $higher;
                   11768:                     if ($j > 0) {
                   11769:                         $higher = &escape($parents->[$j]).':'.
                   11770:                                   &escape($parents->[$j-1]).':'.$j;
                   11771:                     } else {
                   11772:                         $higher = &escape($parents->[$j]).'::'.$j;
                   11773:                     }
                   11774:                     push(@{$subcats->{$higher}},$subcat);
                   11775:                 }
                   11776:             }
                   11777:             &recurse_categories($cats,$deeper,$name,$trails,$allitems,$parents,
                   11778:                                 $subcats);
1.655     raeburn  11779:             pop(@{$parents});
                   11780:         }
                   11781:     } else {
                   11782:         my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
                   11783:         my $trailstr = join(' -&gt; ',(@{$parents},$category));
                   11784:         if ($allitems->{$item} eq '') {
                   11785:             push(@{$trails},$trailstr);
                   11786:             $allitems->{$item} = scalar(@{$trails})-1;
                   11787:         }
                   11788:     }
                   11789:     return;
                   11790: }
                   11791: 
1.663     raeburn  11792: =pod
                   11793: 
                   11794: =item *&assign_categories_table()
                   11795: 
                   11796: Create a datatable for display of hierarchical categories in a domain,
                   11797: with checkboxes to allow a course to be categorized. 
                   11798: 
                   11799: Inputs:
                   11800: 
                   11801: cathash - reference to hash of categories defined for the domain (from
                   11802:           configuration.db)
                   11803: 
                   11804: currcat - scalar with an & separated list of categories assigned to a course. 
                   11805: 
1.919     raeburn  11806: type    - scalar contains course type (Course or Community).
                   11807: 
1.663     raeburn  11808: Returns: $output (markup to be displayed) 
                   11809: 
                   11810: =cut
                   11811: 
                   11812: sub assign_categories_table {
1.919     raeburn  11813:     my ($cathash,$currcat,$type) = @_;
1.663     raeburn  11814:     my $output;
                   11815:     if (ref($cathash) eq 'HASH') {
                   11816:         my (@cats,@trails,%allitems,%idx,@jsarray,@path,$maxdepth);
                   11817:         &extract_categories($cathash,\@cats,\@trails,\%allitems,\%idx,\@jsarray);
                   11818:         $maxdepth = scalar(@cats);
                   11819:         if (@cats > 0) {
                   11820:             my $itemcount = 0;
                   11821:             if (ref($cats[0]) eq 'ARRAY') {
                   11822:                 my @currcategories;
                   11823:                 if ($currcat ne '') {
                   11824:                     @currcategories = split('&',$currcat);
                   11825:                 }
1.919     raeburn  11826:                 my $table;
1.663     raeburn  11827:                 for (my $i=0; $i<@{$cats[0]}; $i++) {
                   11828:                     my $parent = $cats[0][$i];
1.919     raeburn  11829:                     next if ($parent eq 'instcode');
                   11830:                     if ($type eq 'Community') {
                   11831:                         next unless ($parent eq 'communities');
                   11832:                     } else {
                   11833:                         next if ($parent eq 'communities');
                   11834:                     }
1.663     raeburn  11835:                     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   11836:                     my $item = &escape($parent).'::0';
                   11837:                     my $checked = '';
                   11838:                     if (@currcategories > 0) {
                   11839:                         if (grep(/^\Q$item\E$/,@currcategories)) {
1.772     bisitz   11840:                             $checked = ' checked="checked"';
1.663     raeburn  11841:                         }
                   11842:                     }
1.919     raeburn  11843:                     my $parent_title = $parent;
                   11844:                     if ($parent eq 'communities') {
                   11845:                         $parent_title = &mt('Communities');
                   11846:                     }
                   11847:                     $table .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.
                   11848:                               '<input type="checkbox" name="usecategory" value="'.
                   11849:                               $item.'"'.$checked.' />'.$parent_title.'</span>'.
                   11850:                               '<input type="hidden" name="catname" value="'.$parent.'" /></td>';
1.663     raeburn  11851:                     my $depth = 1;
                   11852:                     push(@path,$parent);
1.919     raeburn  11853:                     $table .= &assign_category_rows($itemcount,\@cats,$depth,$parent,\@path,\@currcategories);
1.663     raeburn  11854:                     pop(@path);
1.919     raeburn  11855:                     $table .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
1.663     raeburn  11856:                     $itemcount ++;
                   11857:                 }
1.919     raeburn  11858:                 if ($itemcount) {
                   11859:                     $output = &Apache::loncommon::start_data_table().
                   11860:                               $table.
                   11861:                               &Apache::loncommon::end_data_table();
                   11862:                 }
1.663     raeburn  11863:             }
                   11864:         }
                   11865:     }
                   11866:     return $output;
                   11867: }
                   11868: 
                   11869: =pod
                   11870: 
                   11871: =item *&assign_category_rows()
                   11872: 
                   11873: Create a datatable row for display of nested categories in a domain,
                   11874: with checkboxes to allow a course to be categorized,called recursively.
                   11875: 
                   11876: Inputs:
                   11877: 
                   11878: itemcount - track row number for alternating colors
                   11879: 
                   11880: cats - reference to array of arrays/hashes which encapsulates hierarchy of
                   11881:       categories and subcategories.
                   11882: 
                   11883: depth - current depth in hierarchy of categories and sub-categories - 0 indexed.
                   11884: 
                   11885: parent - parent of current category item
                   11886: 
                   11887: path - Array containing all categories back up through the hierarchy from the
                   11888:        current category to the top level.
                   11889: 
                   11890: currcategories - reference to array of current categories assigned to the course
                   11891: 
                   11892: Returns: $output (markup to be displayed).
                   11893: 
                   11894: =cut
                   11895: 
                   11896: sub assign_category_rows {
                   11897:     my ($itemcount,$cats,$depth,$parent,$path,$currcategories) = @_;
                   11898:     my ($text,$name,$item,$chgstr);
                   11899:     if (ref($cats) eq 'ARRAY') {
                   11900:         my $maxdepth = scalar(@{$cats});
                   11901:         if (ref($cats->[$depth]) eq 'HASH') {
                   11902:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
                   11903:                 my $numchildren = @{$cats->[$depth]{$parent}};
                   11904:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   11905:                 $text .= '<td><table class="LC_datatable">';
                   11906:                 for (my $j=0; $j<$numchildren; $j++) {
                   11907:                     $name = $cats->[$depth]{$parent}[$j];
                   11908:                     $item = &escape($name).':'.&escape($parent).':'.$depth;
                   11909:                     my $deeper = $depth+1;
                   11910:                     my $checked = '';
                   11911:                     if (ref($currcategories) eq 'ARRAY') {
                   11912:                         if (@{$currcategories} > 0) {
                   11913:                             if (grep(/^\Q$item\E$/,@{$currcategories})) {
1.772     bisitz   11914:                                 $checked = ' checked="checked"';
1.663     raeburn  11915:                             }
                   11916:                         }
                   11917:                     }
1.664     raeburn  11918:                     $text .= '<tr><td><span class="LC_nobreak"><label>'.
                   11919:                              '<input type="checkbox" name="usecategory" value="'.
1.675     raeburn  11920:                              $item.'"'.$checked.' />'.$name.'</label></span>'.
                   11921:                              '<input type="hidden" name="catname" value="'.$name.'" />'.
                   11922:                              '</td><td>';
1.663     raeburn  11923:                     if (ref($path) eq 'ARRAY') {
                   11924:                         push(@{$path},$name);
                   11925:                         $text .= &assign_category_rows($itemcount,$cats,$deeper,$name,$path,$currcategories);
                   11926:                         pop(@{$path});
                   11927:                     }
                   11928:                     $text .= '</td></tr>';
                   11929:                 }
                   11930:                 $text .= '</table></td>';
                   11931:             }
                   11932:         }
                   11933:     }
                   11934:     return $text;
                   11935: }
                   11936: 
1.655     raeburn  11937: ############################################################
                   11938: ############################################################
                   11939: 
                   11940: 
1.443     albertel 11941: sub commit_customrole {
1.664     raeburn  11942:     my ($udom,$uname,$url,$three,$four,$five,$start,$end,$context) = @_;
1.630     raeburn  11943:     my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.':'.$three.' in '.$url.
1.443     albertel 11944:                          ($start?', '.&mt('starting').' '.localtime($start):'').
                   11945:                          ($end?', ending '.localtime($end):'').': <b>'.
                   11946:               &Apache::lonnet::assigncustomrole(
1.664     raeburn  11947:                  $udom,$uname,$url,$three,$four,$five,$end,$start,undef,undef,$context).
1.443     albertel 11948:                  '</b><br />';
                   11949:     return $output;
                   11950: }
                   11951: 
                   11952: sub commit_standardrole {
1.541     raeburn  11953:     my ($udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
                   11954:     my ($output,$logmsg,$linefeed);
                   11955:     if ($context eq 'auto') {
                   11956:         $linefeed = "\n";
                   11957:     } else {
                   11958:         $linefeed = "<br />\n";
                   11959:     }  
1.443     albertel 11960:     if ($three eq 'st') {
1.541     raeburn  11961:         my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$three,$start,$end,
                   11962:                                          $one,$two,$sec,$context);
                   11963:         if (($result =~ /^error/) || ($result eq 'not_in_class') || 
1.626     raeburn  11964:             ($result eq 'unknown_course') || ($result eq 'refused')) {
                   11965:             $output = $logmsg.' '.&mt('Error: ').$result."\n"; 
1.443     albertel 11966:         } else {
1.541     raeburn  11967:             $output = $logmsg.$linefeed.&mt('Assigning').' '.$three.' in '.$url.
1.443     albertel 11968:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  11969:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
                   11970:             if ($context eq 'auto') {
                   11971:                 $output .= $result.$linefeed.&mt('Add to classlist').': ok';
                   11972:             } else {
                   11973:                $output .= '<b>'.$result.'</b>'.$linefeed.
                   11974:                &mt('Add to classlist').': <b>ok</b>';
                   11975:             }
                   11976:             $output .= $linefeed;
1.443     albertel 11977:         }
                   11978:     } else {
                   11979:         $output = &mt('Assigning').' '.$three.' in '.$url.
                   11980:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  11981:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
1.652     raeburn  11982:         my $result = &Apache::lonnet::assignrole($udom,$uname,$url,$three,$end,$start,'','',$context);
1.541     raeburn  11983:         if ($context eq 'auto') {
                   11984:             $output .= $result.$linefeed;
                   11985:         } else {
                   11986:             $output .= '<b>'.$result.'</b>'.$linefeed;
                   11987:         }
1.443     albertel 11988:     }
                   11989:     return $output;
                   11990: }
                   11991: 
                   11992: sub commit_studentrole {
1.541     raeburn  11993:     my ($logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
1.626     raeburn  11994:     my ($result,$linefeed,$oldsecurl,$newsecurl);
1.541     raeburn  11995:     if ($context eq 'auto') {
                   11996:         $linefeed = "\n";
                   11997:     } else {
                   11998:         $linefeed = '<br />'."\n";
                   11999:     }
1.443     albertel 12000:     if (defined($one) && defined($two)) {
                   12001:         my $cid=$one.'_'.$two;
                   12002:         my $oldsec=&Apache::lonnet::getsection($udom,$uname,$cid);
                   12003:         my $secchange = 0;
                   12004:         my $expire_role_result;
                   12005:         my $modify_section_result;
1.628     raeburn  12006:         if ($oldsec ne '-1') { 
                   12007:             if ($oldsec ne $sec) {
1.443     albertel 12008:                 $secchange = 1;
1.628     raeburn  12009:                 my $now = time;
1.443     albertel 12010:                 my $uurl='/'.$cid;
                   12011:                 $uurl=~s/\_/\//g;
                   12012:                 if ($oldsec) {
                   12013:                     $uurl.='/'.$oldsec;
                   12014:                 }
1.626     raeburn  12015:                 $oldsecurl = $uurl;
1.628     raeburn  12016:                 $expire_role_result = 
1.652     raeburn  12017:                     &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',$now,'','',$context);
1.628     raeburn  12018:                 if ($env{'request.course.sec'} ne '') { 
                   12019:                     if ($expire_role_result eq 'refused') {
                   12020:                         my @roles = ('st');
                   12021:                         my @statuses = ('previous');
                   12022:                         my @roledoms = ($one);
                   12023:                         my $withsec = 1;
                   12024:                         my %roleshash = 
                   12025:                             &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   12026:                                               \@statuses,\@roles,\@roledoms,$withsec);
                   12027:                         if (defined ($roleshash{$two.':'.$one.':st:'.$oldsec})) {
                   12028:                             my ($oldstart,$oldend) = 
                   12029:                                 split(':',$roleshash{$two.':'.$one.':st:'.$oldsec});
                   12030:                             if ($oldend > 0 && $oldend <= $now) {
                   12031:                                 $expire_role_result = 'ok';
                   12032:                             }
                   12033:                         }
                   12034:                     }
                   12035:                 }
1.443     albertel 12036:                 $result = $expire_role_result;
                   12037:             }
                   12038:         }
                   12039:         if (($expire_role_result eq 'ok') || ($secchange == 0)) {
1.652     raeburn  12040:             $modify_section_result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,'','',$cid,'',$context);
1.443     albertel 12041:             if ($modify_section_result =~ /^ok/) {
                   12042:                 if ($secchange == 1) {
1.628     raeburn  12043:                     if ($sec eq '') {
                   12044:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to student role without a section.',$uname,$oldsec).$linefeed;
                   12045:                     } else {
                   12046:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to new section: [_3].',$uname,$oldsec,$sec).$linefeed;
                   12047:                     }
1.443     albertel 12048:                 } elsif ($oldsec eq '-1') {
1.628     raeburn  12049:                     if ($sec eq '') {
                   12050:                         $$logmsg .= &mt('New student role without a section for [_1] in course [_2].',$uname,$cid).$linefeed;
                   12051:                     } else {
                   12052:                         $$logmsg .= &mt('New student role for [_1] in section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
                   12053:                     }
1.443     albertel 12054:                 } else {
1.628     raeburn  12055:                     if ($sec eq '') {
                   12056:                         $$logmsg .= &mt('Student [_1] assigned to course [_2] without a section.',$uname,$cid).$linefeed;
                   12057:                     } else {
                   12058:                         $$logmsg .= &mt('Student [_1] assigned to section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
                   12059:                     }
1.443     albertel 12060:                 }
                   12061:             } else {
1.628     raeburn  12062:                 if ($secchange) {       
                   12063:                     $$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;
                   12064:                 } else {
                   12065:                     $$logmsg .= &mt('Error when attempting to modify role for [_1] for section: "[_2]" in course [_3] -error:',$uname,$sec,$cid).' '.$modify_section_result.$linefeed;
                   12066:                 }
1.443     albertel 12067:             }
                   12068:             $result = $modify_section_result;
                   12069:         } elsif ($secchange == 1) {
1.628     raeburn  12070:             if ($oldsec eq '') {
                   12071:                 $$logmsg .= &mt('Error when attempting to expire existing role without a section for [_1] in course [_3] -error: ',$uname,$cid).' '.$expire_role_result.$linefeed;
                   12072:             } else {
                   12073:                 $$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;
                   12074:             }
1.626     raeburn  12075:             if ($expire_role_result eq 'refused') {
                   12076:                 my $newsecurl = '/'.$cid;
                   12077:                 $newsecurl =~ s/\_/\//g;
                   12078:                 if ($sec ne '') {
                   12079:                     $newsecurl.='/'.$sec;
                   12080:                 }
                   12081:                 if (&Apache::lonnet::allowed('cst',$newsecurl) && !(&Apache::lonnet::allowed('cst',$oldsecurl))) {
                   12082:                     if ($sec eq '') {
                   12083:                         $$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;
                   12084:                     } else {
                   12085:                         $$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;
                   12086:                     }
                   12087:                 }
                   12088:             }
1.443     albertel 12089:         }
                   12090:     } else {
1.626     raeburn  12091:         $$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 12092:         $result = "error: incomplete course id\n";
                   12093:     }
                   12094:     return $result;
                   12095: }
                   12096: 
                   12097: ############################################################
                   12098: ############################################################
                   12099: 
1.566     albertel 12100: sub check_clone {
1.578     raeburn  12101:     my ($args,$linefeed) = @_;
1.566     albertel 12102:     my $cloneid='/'.$args->{'clonedomain'}.'/'.$args->{'clonecourse'};
                   12103:     my ($clonecrsudom,$clonecrsunum)= &LONCAPA::split_courseid($cloneid);
                   12104:     my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
                   12105:     my $clonemsg;
                   12106:     my $can_clone = 0;
1.944     raeburn  12107:     my $lctype = lc($args->{'crstype'});
1.908     raeburn  12108:     if ($lctype ne 'community') {
                   12109:         $lctype = 'course';
                   12110:     }
1.566     albertel 12111:     if ($clonehome eq 'no_host') {
1.944     raeburn  12112:         if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  12113:             $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'});
                   12114:         } else {
                   12115:             $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'});
                   12116:         }     
1.566     albertel 12117:     } else {
                   12118: 	my %clonedesc = &Apache::lonnet::coursedescription($cloneid,{'one_time' => 1});
1.944     raeburn  12119:         if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  12120:             if ($clonedesc{'type'} ne 'Community') {
                   12121:                  $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'});
                   12122:                 return ($can_clone, $clonemsg, $cloneid, $clonehome);
                   12123:             }
                   12124:         }
1.882     raeburn  12125: 	if (($env{'request.role.domain'} eq $args->{'clonedomain'}) && 
                   12126:             (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'}))) {
1.566     albertel 12127: 	    $can_clone = 1;
                   12128: 	} else {
                   12129: 	    my %clonehash = &Apache::lonnet::get('environment',['cloners'],
                   12130: 						 $args->{'clonedomain'},$args->{'clonecourse'});
                   12131: 	    my @cloners = split(/,/,$clonehash{'cloners'});
1.578     raeburn  12132:             if (grep(/^\*$/,@cloners)) {
                   12133:                 $can_clone = 1;
                   12134:             } elsif (grep(/^\*\:\Q$args->{'ccdomain'}\E$/,@cloners)) {
                   12135:                 $can_clone = 1;
                   12136:             } else {
1.908     raeburn  12137:                 my $ccrole = 'cc';
1.944     raeburn  12138:                 if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  12139:                     $ccrole = 'co';
                   12140:                 }
1.578     raeburn  12141: 	        my %roleshash =
                   12142: 		    &Apache::lonnet::get_my_roles($args->{'ccuname'},
                   12143: 					 $args->{'ccdomain'},
1.908     raeburn  12144:                                          'userroles',['active'],[$ccrole],
1.578     raeburn  12145: 					 [$args->{'clonedomain'}]);
1.908     raeburn  12146: 	        if (($roleshash{$args->{'clonecourse'}.':'.$args->{'clonedomain'}.':'.$ccrole}) || (grep(/^\Q$args->{'ccuname'}\E:\Q$args->{'ccdomain'}\E$/,@cloners))) {
1.942     raeburn  12147:                     $can_clone = 1;
                   12148:                 } elsif (&Apache::lonnet::is_course_owner($args->{'clonedomain'},$args->{'clonecourse'},$args->{'ccuname'},$args->{'ccdomain'})) {
                   12149:                     $can_clone = 1;
                   12150:                 } else {
1.944     raeburn  12151:                     if ($args->{'crstype'} eq 'Community') {
1.908     raeburn  12152:                         $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'});
                   12153:                     } else {
                   12154:                         $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'});
                   12155:                     }
1.578     raeburn  12156: 	        }
1.566     albertel 12157: 	    }
1.578     raeburn  12158:         }
1.566     albertel 12159:     }
                   12160:     return ($can_clone, $clonemsg, $cloneid, $clonehome);
                   12161: }
                   12162: 
1.444     albertel 12163: sub construct_course {
1.885     raeburn  12164:     my ($args,$logmsg,$courseid,$crsudom,$crsunum,$udom,$uname,$context,$cnum,$category) = @_;
1.444     albertel 12165:     my $outcome;
1.541     raeburn  12166:     my $linefeed =  '<br />'."\n";
                   12167:     if ($context eq 'auto') {
                   12168:         $linefeed = "\n";
                   12169:     }
1.566     albertel 12170: 
                   12171: #
                   12172: # Are we cloning?
                   12173: #
                   12174:     my ($can_clone, $clonemsg, $cloneid, $clonehome);
                   12175:     if (($args->{'clonecourse'}) && ($args->{'clonedomain'})) {
1.578     raeburn  12176: 	($can_clone, $clonemsg, $cloneid, $clonehome) = &check_clone($args,$linefeed);
1.566     albertel 12177: 	if ($context ne 'auto') {
1.578     raeburn  12178:             if ($clonemsg ne '') {
                   12179: 	        $clonemsg = '<span class="LC_error">'.$clonemsg.'</span>';
                   12180:             }
1.566     albertel 12181: 	}
                   12182: 	$outcome .= $clonemsg.$linefeed;
                   12183: 
                   12184:         if (!$can_clone) {
                   12185: 	    return (0,$outcome);
                   12186: 	}
                   12187:     }
                   12188: 
1.444     albertel 12189: #
                   12190: # Open course
                   12191: #
                   12192:     my $crstype = lc($args->{'crstype'});
                   12193:     my %cenv=();
                   12194:     $$courseid=&Apache::lonnet::createcourse($args->{'course_domain'},
                   12195:                                              $args->{'cdescr'},
                   12196:                                              $args->{'curl'},
                   12197:                                              $args->{'course_home'},
                   12198:                                              $args->{'nonstandard'},
                   12199:                                              $args->{'crscode'},
                   12200:                                              $args->{'ccuname'}.':'.
                   12201:                                              $args->{'ccdomain'},
1.882     raeburn  12202:                                              $args->{'crstype'},
1.885     raeburn  12203:                                              $cnum,$context,$category);
1.444     albertel 12204: 
                   12205:     # Note: The testing routines depend on this being output; see 
                   12206:     # Utils::Course. This needs to at least be output as a comment
                   12207:     # if anyone ever decides to not show this, and Utils::Course::new
                   12208:     # will need to be suitably modified.
1.541     raeburn  12209:     $outcome .= &mt('New LON-CAPA [_1] ID: [_2]',$crstype,$$courseid).$linefeed;
1.943     raeburn  12210:     if ($$courseid =~ /^error:/) {
                   12211:         return (0,$outcome);
                   12212:     }
                   12213: 
1.444     albertel 12214: #
                   12215: # Check if created correctly
                   12216: #
1.479     albertel 12217:     ($$crsudom,$$crsunum)= &LONCAPA::split_courseid($$courseid);
1.444     albertel 12218:     my $crsuhome=&Apache::lonnet::homeserver($$crsunum,$$crsudom);
1.943     raeburn  12219:     if ($crsuhome eq 'no_host') {
                   12220:         $outcome .= &mt('Course creation failed, unrecognized course home server.').$linefeed;
                   12221:         return (0,$outcome);
                   12222:     }
1.541     raeburn  12223:     $outcome .= &mt('Created on').': '.$crsuhome.$linefeed;
1.566     albertel 12224: 
1.444     albertel 12225: #
1.566     albertel 12226: # Do the cloning
                   12227: #   
                   12228:     if ($can_clone && $cloneid) {
                   12229: 	$clonemsg = &mt('Cloning [_1] from [_2]',$crstype,$clonehome);
                   12230: 	if ($context ne 'auto') {
                   12231: 	    $clonemsg = '<span class="LC_success">'.$clonemsg.'</span>';
                   12232: 	}
                   12233: 	$outcome .= $clonemsg.$linefeed;
                   12234: 	my %oldcenv=&Apache::lonnet::dump('environment',$$crsudom,$$crsunum);
1.444     albertel 12235: # Copy all files
1.637     www      12236: 	&Apache::lonclonecourse::copycoursefiles($cloneid,$$courseid,$args->{'datemode'},$args->{'dateshift'});
1.444     albertel 12237: # Restore URL
1.566     albertel 12238: 	$cenv{'url'}=$oldcenv{'url'};
1.444     albertel 12239: # Restore title
1.566     albertel 12240: 	$cenv{'description'}=$oldcenv{'description'};
1.955     raeburn  12241: # Restore creation date, creator and creation context.
                   12242:         $cenv{'internal.created'}=$oldcenv{'internal.created'};
                   12243:         $cenv{'internal.creator'}=$oldcenv{'internal.creator'};
                   12244:         $cenv{'internal.creationcontext'}=$oldcenv{'internal.creationcontext'};
1.444     albertel 12245: # Mark as cloned
1.566     albertel 12246: 	$cenv{'clonedfrom'}=$cloneid;
1.638     www      12247: # Need to clone grading mode
                   12248:         my %newenv=&Apache::lonnet::get('environment',['grading'],$$crsudom,$$crsunum);
                   12249:         $cenv{'grading'}=$newenv{'grading'};
                   12250: # Do not clone these environment entries
                   12251:         &Apache::lonnet::del('environment',
                   12252:                   ['default_enrollment_start_date',
                   12253:                    'default_enrollment_end_date',
                   12254:                    'question.email',
                   12255:                    'policy.email',
                   12256:                    'comment.email',
                   12257:                    'pch.users.denied',
1.725     raeburn  12258:                    'plc.users.denied',
                   12259:                    'hidefromcat',
                   12260:                    'categories'],
1.638     www      12261:                    $$crsudom,$$crsunum);
1.444     albertel 12262:     }
1.566     albertel 12263: 
1.444     albertel 12264: #
                   12265: # Set environment (will override cloned, if existing)
                   12266: #
                   12267:     my @sections = ();
                   12268:     my @xlists = ();
                   12269:     if ($args->{'crstype'}) {
                   12270:         $cenv{'type'}=$args->{'crstype'};
                   12271:     }
                   12272:     if ($args->{'crsid'}) {
                   12273:         $cenv{'courseid'}=$args->{'crsid'};
                   12274:     }
                   12275:     if ($args->{'crscode'}) {
                   12276:         $cenv{'internal.coursecode'}=$args->{'crscode'};
                   12277:     }
                   12278:     if ($args->{'crsquota'} ne '') {
                   12279:         $cenv{'internal.coursequota'}=$args->{'crsquota'};
                   12280:     } else {
                   12281:         $cenv{'internal.coursequota'}=$args->{'crsquota'} = 20;
                   12282:     }
                   12283:     if ($args->{'ccuname'}) {
                   12284:         $cenv{'internal.courseowner'} = $args->{'ccuname'}.
                   12285:                                         ':'.$args->{'ccdomain'};
                   12286:     } else {
                   12287:         $cenv{'internal.courseowner'} = $args->{'curruser'};
                   12288:     }
                   12289:     my @badclasses = (); # Used to accumulate sections/crosslistings that did not pass classlist access check for course owner.
                   12290:     if ($args->{'crssections'}) {
                   12291:         $cenv{'internal.sectionnums'} = '';
                   12292:         if ($args->{'crssections'} =~ m/,/) {
                   12293:             @sections = split/,/,$args->{'crssections'};
                   12294:         } else {
                   12295:             $sections[0] = $args->{'crssections'};
                   12296:         }
                   12297:         if (@sections > 0) {
                   12298:             foreach my $item (@sections) {
                   12299:                 my ($sec,$gp) = split/:/,$item;
                   12300:                 my $class = $args->{'crscode'}.$sec;
                   12301:                 my $addcheck = &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$class,$cenv{'internal.courseowner'});
                   12302:                 $cenv{'internal.sectionnums'} .= $item.',';
                   12303:                 unless ($addcheck eq 'ok') {
                   12304:                     push @badclasses, $class;
                   12305:                 }
                   12306:             }
                   12307:             $cenv{'internal.sectionnums'} =~ s/,$//;
                   12308:         }
                   12309:     }
                   12310: # do not hide course coordinator from staff listing, 
                   12311: # even if privileged
                   12312:     $cenv{'nothideprivileged'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   12313: # add crosslistings
                   12314:     if ($args->{'crsxlist'}) {
                   12315:         $cenv{'internal.crosslistings'}='';
                   12316:         if ($args->{'crsxlist'} =~ m/,/) {
                   12317:             @xlists = split/,/,$args->{'crsxlist'};
                   12318:         } else {
                   12319:             $xlists[0] = $args->{'crsxlist'};
                   12320:         }
                   12321:         if (@xlists > 0) {
                   12322:             foreach my $item (@xlists) {
                   12323:                 my ($xl,$gp) = split/:/,$item;
                   12324:                 my $addcheck =  &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$xl,$cenv{'internal.courseowner'});
                   12325:                 $cenv{'internal.crosslistings'} .= $item.',';
                   12326:                 unless ($addcheck eq 'ok') {
                   12327:                     push @badclasses, $xl;
                   12328:                 }
                   12329:             }
                   12330:             $cenv{'internal.crosslistings'} =~ s/,$//;
                   12331:         }
                   12332:     }
                   12333:     if ($args->{'autoadds'}) {
                   12334:         $cenv{'internal.autoadds'}=$args->{'autoadds'};
                   12335:     }
                   12336:     if ($args->{'autodrops'}) {
                   12337:         $cenv{'internal.autodrops'}=$args->{'autodrops'};
                   12338:     }
                   12339: # check for notification of enrollment changes
                   12340:     my @notified = ();
                   12341:     if ($args->{'notify_owner'}) {
                   12342:         if ($args->{'ccuname'} ne '') {
                   12343:             push(@notified,$args->{'ccuname'}.':'.$args->{'ccdomain'});
                   12344:         }
                   12345:     }
                   12346:     if ($args->{'notify_dc'}) {
                   12347:         if ($uname ne '') { 
1.630     raeburn  12348:             push(@notified,$uname.':'.$udom);
1.444     albertel 12349:         }
                   12350:     }
                   12351:     if (@notified > 0) {
                   12352:         my $notifylist;
                   12353:         if (@notified > 1) {
                   12354:             $notifylist = join(',',@notified);
                   12355:         } else {
                   12356:             $notifylist = $notified[0];
                   12357:         }
                   12358:         $cenv{'internal.notifylist'} = $notifylist;
                   12359:     }
                   12360:     if (@badclasses > 0) {
                   12361:         my %lt=&Apache::lonlocal::texthash(
                   12362:                 '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',
                   12363:                 'dnhr' => 'does not have rights to access enrollment in these classes',
                   12364:                 'adby' => 'as determined by the policies of your institution on access to official classlists'
                   12365:         );
1.541     raeburn  12366:         my $badclass_msg = $cenv{'internal.courseowner'}.') - '.$lt{'dnhr'}.
                   12367:                            ' ('.$lt{'adby'}.')';
                   12368:         if ($context eq 'auto') {
                   12369:             $outcome .= $badclass_msg.$linefeed;
1.566     albertel 12370:             $outcome .= '<div class="LC_warning">'.$badclass_msg.$linefeed.'<ul>'."\n";
1.541     raeburn  12371:             foreach my $item (@badclasses) {
                   12372:                 if ($context eq 'auto') {
                   12373:                     $outcome .= " - $item\n";
                   12374:                 } else {
                   12375:                     $outcome .= "<li>$item</li>\n";
                   12376:                 }
                   12377:             }
                   12378:             if ($context eq 'auto') {
                   12379:                 $outcome .= $linefeed;
                   12380:             } else {
1.566     albertel 12381:                 $outcome .= "</ul><br /><br /></div>\n";
1.541     raeburn  12382:             }
                   12383:         } 
1.444     albertel 12384:     }
                   12385:     if ($args->{'no_end_date'}) {
                   12386:         $args->{'endaccess'} = 0;
                   12387:     }
                   12388:     $cenv{'internal.autostart'}=$args->{'enrollstart'};
                   12389:     $cenv{'internal.autoend'}=$args->{'enrollend'};
                   12390:     $cenv{'default_enrollment_start_date'}=$args->{'startaccess'};
                   12391:     $cenv{'default_enrollment_end_date'}=$args->{'endaccess'};
                   12392:     if ($args->{'showphotos'}) {
                   12393:       $cenv{'internal.showphotos'}=$args->{'showphotos'};
                   12394:     }
                   12395:     $cenv{'internal.authtype'} = $args->{'authtype'};
                   12396:     $cenv{'internal.autharg'} = $args->{'autharg'}; 
                   12397:     if ( ($cenv{'internal.authtype'} =~ /^krb/) && ($cenv{'internal.autoadds'} == 1)) {
                   12398:         if (! defined($cenv{'internal.autharg'}) || $cenv{'internal.autharg'}  eq '') {
1.541     raeburn  12399:             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'); 
                   12400:             if ($context eq 'auto') {
                   12401:                 $outcome .= $krb_msg;
                   12402:             } else {
1.566     albertel 12403:                 $outcome .= '<span class="LC_error">'.$krb_msg.'</span>';
1.541     raeburn  12404:             }
                   12405:             $outcome .= $linefeed;
1.444     albertel 12406:         }
                   12407:     }
                   12408:     if (($args->{'ccdomain'}) && ($args->{'ccuname'})) {
                   12409:        if ($args->{'setpolicy'}) {
                   12410:            $cenv{'policy.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   12411:        }
                   12412:        if ($args->{'setcontent'}) {
                   12413:            $cenv{'question.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   12414:        }
                   12415:     }
                   12416:     if ($args->{'reshome'}) {
                   12417: 	$cenv{'reshome'}=$args->{'reshome'}.'/';
                   12418: 	$cenv{'reshome'}=~s/\/+$/\//;
                   12419:     }
                   12420: #
                   12421: # course has keyed access
                   12422: #
                   12423:     if ($args->{'setkeys'}) {
                   12424:        $cenv{'keyaccess'}='yes';
                   12425:     }
                   12426: # if specified, key authority is not course, but user
                   12427: # only active if keyaccess is yes
                   12428:     if ($args->{'keyauth'}) {
1.487     albertel 12429: 	my ($user,$domain) = split(':',$args->{'keyauth'});
                   12430: 	$user = &LONCAPA::clean_username($user);
                   12431: 	$domain = &LONCAPA::clean_username($domain);
1.488     foxr     12432: 	if ($user ne '' && $domain ne '') {
1.487     albertel 12433: 	    $cenv{'keyauth'}=$user.':'.$domain;
1.444     albertel 12434: 	}
                   12435:     }
                   12436: 
                   12437:     if ($args->{'disresdis'}) {
                   12438:         $cenv{'pch.roles.denied'}='st';
                   12439:     }
                   12440:     if ($args->{'disablechat'}) {
                   12441:         $cenv{'plc.roles.denied'}='st';
                   12442:     }
                   12443: 
                   12444:     # Record we've not yet viewed the Course Initialization Helper for this 
                   12445:     # course
                   12446:     $cenv{'course.helper.not.run'} = 1;
                   12447:     #
                   12448:     # Use new Randomseed
                   12449:     #
                   12450:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
                   12451:     $cenv{'receiptalg'}=&Apache::lonnet::latest_receipt_algorithm_id();;
                   12452:     #
                   12453:     # The encryption code and receipt prefix for this course
                   12454:     #
                   12455:     $cenv{'internal.encseed'}=$Apache::lonnet::perlvar{'lonReceipt'}.$$.time.int(rand(9999));
                   12456:     $cenv{'internal.encpref'}=100+int(9*rand(99));
                   12457:     #
                   12458:     # By default, use standard grading
                   12459:     if (!defined($cenv{'grading'})) { $cenv{'grading'} = 'standard'; }
                   12460: 
1.541     raeburn  12461:     $outcome .= $linefeed.&mt('Setting environment').': '.                 
                   12462:           &Apache::lonnet::put('environment',\%cenv,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 12463: #
                   12464: # Open all assignments
                   12465: #
                   12466:     if ($args->{'openall'}) {
                   12467:        my $storeunder=$$crsudom.'_'.$$crsunum.'.0.opendate';
                   12468:        my %storecontent = ($storeunder         => time,
                   12469:                            $storeunder.'.type' => 'date_start');
                   12470:        
                   12471:        $outcome .= &mt('Opening all assignments').': '.&Apache::lonnet::cput
1.541     raeburn  12472:                  ('resourcedata',\%storecontent,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 12473:    }
                   12474: #
                   12475: # Set first page
                   12476: #
                   12477:     unless (($args->{'nonstandard'}) || ($args->{'firstres'} eq 'blank')
                   12478: 	    || ($cloneid)) {
1.445     albertel 12479: 	use LONCAPA::map;
1.444     albertel 12480: 	$outcome .= &mt('Setting first resource').': ';
1.445     albertel 12481: 
                   12482: 	my $map = '/uploaded/'.$$crsudom.'/'.$$crsunum.'/default.sequence';
                   12483:         my ($errtext,$fatal)=&LONCAPA::map::mapread($map);
                   12484: 
1.444     albertel 12485:         $outcome .= ($fatal?$errtext:'read ok').' - ';
                   12486:         my $title; my $url;
                   12487:         if ($args->{'firstres'} eq 'syl') {
1.690     bisitz   12488: 	    $title=&mt('Syllabus');
1.444     albertel 12489:             $url='/public/'.$$crsudom.'/'.$$crsunum.'/syllabus';
                   12490:         } else {
1.963     raeburn  12491:             $title=&mt('Table of Contents');
1.444     albertel 12492:             $url='/adm/navmaps';
                   12493:         }
1.445     albertel 12494: 
                   12495:         $LONCAPA::map::resources[1]=$title.':'.$url.':false:start:res';
                   12496: 	(my $outtext,$errtext) = &LONCAPA::map::storemap($map,1);
                   12497: 
                   12498: 	if ($errtext) { $fatal=2; }
1.541     raeburn  12499:         $outcome .= ($fatal?$errtext:'write ok').$linefeed;
1.444     albertel 12500:     }
1.566     albertel 12501: 
                   12502:     return (1,$outcome);
1.444     albertel 12503: }
                   12504: 
                   12505: ############################################################
                   12506: ############################################################
                   12507: 
1.953     droeschl 12508: #SD
                   12509: # only Community and Course, or anything else?
1.378     raeburn  12510: sub course_type {
                   12511:     my ($cid) = @_;
                   12512:     if (!defined($cid)) {
                   12513:         $cid = $env{'request.course.id'};
                   12514:     }
1.404     albertel 12515:     if (defined($env{'course.'.$cid.'.type'})) {
                   12516:         return $env{'course.'.$cid.'.type'};
1.378     raeburn  12517:     } else {
                   12518:         return 'Course';
1.377     raeburn  12519:     }
                   12520: }
1.156     albertel 12521: 
1.406     raeburn  12522: sub group_term {
                   12523:     my $crstype = &course_type();
                   12524:     my %names = (
                   12525:                   'Course' => 'group',
1.865     raeburn  12526:                   'Community' => 'group',
1.406     raeburn  12527:                 );
                   12528:     return $names{$crstype};
                   12529: }
                   12530: 
1.902     raeburn  12531: sub course_types {
                   12532:     my @types = ('official','unofficial','community');
                   12533:     my %typename = (
                   12534:                          official   => 'Official course',
                   12535:                          unofficial => 'Unofficial course',
                   12536:                          community  => 'Community',
                   12537:                    );
                   12538:     return (\@types,\%typename);
                   12539: }
                   12540: 
1.156     albertel 12541: sub icon {
                   12542:     my ($file)=@_;
1.505     albertel 12543:     my $curfext = lc((split(/\./,$file))[-1]);
1.168     albertel 12544:     my $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/unknown.gif';
1.156     albertel 12545:     my $embstyle = &Apache::loncommon::fileembstyle($curfext);
1.168     albertel 12546:     if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) {
                   12547: 	if (-e  $Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
                   12548: 	          $Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   12549: 	            $curfext.".gif") {
                   12550: 	    $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   12551: 		$curfext.".gif";
                   12552: 	}
                   12553:     }
1.249     albertel 12554:     return &lonhttpdurl($iconname);
1.154     albertel 12555: } 
1.84      albertel 12556: 
1.575     albertel 12557: sub lonhttpdurl {
1.692     www      12558: #
                   12559: # Had been used for "small fry" static images on separate port 8080.
                   12560: # Modify here if lightweight http functionality desired again.
                   12561: # Currently eliminated due to increasing firewall issues.
                   12562: #
1.575     albertel 12563:     my ($url)=@_;
1.692     www      12564:     return $url;
1.215     albertel 12565: }
                   12566: 
1.213     albertel 12567: sub connection_aborted {
                   12568:     my ($r)=@_;
                   12569:     $r->print(" ");$r->rflush();
                   12570:     my $c = $r->connection;
                   12571:     return $c->aborted();
                   12572: }
                   12573: 
1.221     foxr     12574: #    Escapes strings that may have embedded 's that will be put into
1.222     foxr     12575: #    strings as 'strings'.
                   12576: sub escape_single {
1.221     foxr     12577:     my ($input) = @_;
1.223     albertel 12578:     $input =~ s/\\/\\\\/g;	# Escape the \'s..(must be first)>
1.221     foxr     12579:     $input =~ s/\'/\\\'/g;	# Esacpe the 's....
                   12580:     return $input;
                   12581: }
1.223     albertel 12582: 
1.222     foxr     12583: #  Same as escape_single, but escape's "'s  This 
                   12584: #  can be used for  "strings"
                   12585: sub escape_double {
                   12586:     my ($input) = @_;
                   12587:     $input =~ s/\\/\\\\/g;	# Escape the /'s..(must be first)>
                   12588:     $input =~ s/\"/\\\"/g;	# Esacpe the "s....
                   12589:     return $input;
                   12590: }
1.223     albertel 12591:  
1.222     foxr     12592: #   Escapes the last element of a full URL.
                   12593: sub escape_url {
                   12594:     my ($url)   = @_;
1.238     raeburn  12595:     my @urlslices = split(/\//, $url,-1);
1.369     www      12596:     my $lastitem = &escape(pop(@urlslices));
1.223     albertel 12597:     return join('/',@urlslices).'/'.$lastitem;
1.222     foxr     12598: }
1.462     albertel 12599: 
1.820     raeburn  12600: sub compare_arrays {
                   12601:     my ($arrayref1,$arrayref2) = @_;
                   12602:     my (@difference,%count);
                   12603:     @difference = ();
                   12604:     %count = ();
                   12605:     if ((ref($arrayref1) eq 'ARRAY') && (ref($arrayref2) eq 'ARRAY')) {
                   12606:         foreach my $element (@{$arrayref1}, @{$arrayref2}) { $count{$element}++; }
                   12607:         foreach my $element (keys(%count)) {
                   12608:             if ($count{$element} == 1) {
                   12609:                 push(@difference,$element);
                   12610:             }
                   12611:         }
                   12612:     }
                   12613:     return @difference;
                   12614: }
                   12615: 
1.817     bisitz   12616: # -------------------------------------------------------- Initialize user login
1.462     albertel 12617: sub init_user_environment {
1.463     albertel 12618:     my ($r, $username, $domain, $authhost, $form, $args) = @_;
1.462     albertel 12619:     my $lonids=$Apache::lonnet::perlvar{'lonIDsDir'};
                   12620: 
                   12621:     my $public=($username eq 'public' && $domain eq 'public');
                   12622: 
                   12623: # See if old ID present, if so, remove
                   12624: 
                   12625:     my ($filename,$cookie,$userroles);
                   12626:     my $now=time;
                   12627: 
                   12628:     if ($public) {
                   12629: 	my $max_public=100;
                   12630: 	my $oldest;
                   12631: 	my $oldest_time=0;
                   12632: 	for(my $next=1;$next<=$max_public;$next++) {
                   12633: 	    if (-e $lonids."/publicuser_$next.id") {
                   12634: 		my $mtime=(stat($lonids."/publicuser_$next.id"))[9];
                   12635: 		if ($mtime<$oldest_time || !$oldest_time) {
                   12636: 		    $oldest_time=$mtime;
                   12637: 		    $oldest=$next;
                   12638: 		}
                   12639: 	    } else {
                   12640: 		$cookie="publicuser_$next";
                   12641: 		last;
                   12642: 	    }
                   12643: 	}
                   12644: 	if (!$cookie) { $cookie="publicuser_$oldest"; }
                   12645:     } else {
1.463     albertel 12646: 	# if this isn't a robot, kill any existing non-robot sessions
                   12647: 	if (!$args->{'robot'}) {
                   12648: 	    opendir(DIR,$lonids);
                   12649: 	    while ($filename=readdir(DIR)) {
                   12650: 		if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
                   12651: 		    unlink($lonids.'/'.$filename);
                   12652: 		}
1.462     albertel 12653: 	    }
1.463     albertel 12654: 	    closedir(DIR);
1.462     albertel 12655: 	}
                   12656: # Give them a new cookie
1.463     albertel 12657: 	my $id = ($args->{'robot'} ? 'robot'.$args->{'robot'}
1.684     www      12658: 		                   : $now.$$.int(rand(10000)));
1.463     albertel 12659: 	$cookie="$username\_$id\_$domain\_$authhost";
1.462     albertel 12660:     
                   12661: # Initialize roles
                   12662: 
                   12663: 	$userroles=&Apache::lonnet::rolesinit($domain,$username,$authhost);
                   12664:     }
                   12665: # ------------------------------------ Check browser type and MathML capability
                   12666: 
                   12667:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                   12668:         $clientunicode,$clientos) = &decode_user_agent($r);
                   12669: 
                   12670: # ------------------------------------------------------------- Get environment
                   12671: 
                   12672:     my %userenv = &Apache::lonnet::dump('environment',$domain,$username);
                   12673:     my ($tmp) = keys(%userenv);
                   12674:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   12675:     } else {
                   12676: 	undef(%userenv);
                   12677:     }
                   12678:     if (($userenv{'interface'}) && (!$form->{'interface'})) {
                   12679: 	$form->{'interface'}=$userenv{'interface'};
                   12680:     }
                   12681:     if ($userenv{'texengine'} eq 'ttm') { $clientmathml=1; }
                   12682: 
                   12683: # --------------- Do not trust query string to be put directly into environment
1.817     bisitz   12684:     foreach my $option ('interface','localpath','localres') {
                   12685:         $form->{$option}=~s/[\n\r\=]//gs;
1.462     albertel 12686:     }
                   12687: # --------------------------------------------------------- Write first profile
                   12688: 
                   12689:     {
                   12690: 	my %initial_env = 
                   12691: 	    ("user.name"          => $username,
                   12692: 	     "user.domain"        => $domain,
                   12693: 	     "user.home"          => $authhost,
                   12694: 	     "browser.type"       => $clientbrowser,
                   12695: 	     "browser.version"    => $clientversion,
                   12696: 	     "browser.mathml"     => $clientmathml,
                   12697: 	     "browser.unicode"    => $clientunicode,
                   12698: 	     "browser.os"         => $clientos,
                   12699: 	     "server.domain"      => $Apache::lonnet::perlvar{'lonDefDomain'},
                   12700: 	     "request.course.fn"  => '',
                   12701: 	     "request.course.uri" => '',
                   12702: 	     "request.course.sec" => '',
                   12703: 	     "request.role"       => 'cm',
                   12704: 	     "request.role.adv"   => $env{'user.adv'},
                   12705: 	     "request.host"       => $ENV{'REMOTE_ADDR'},);
                   12706: 
                   12707:         if ($form->{'localpath'}) {
                   12708: 	    $initial_env{"browser.localpath"}  = $form->{'localpath'};
                   12709: 	    $initial_env{"browser.localres"}   = $form->{'localres'};
                   12710:         }
                   12711: 	
                   12712: 	if ($form->{'interface'}) {
                   12713: 	    $form->{'interface'}=~s/\W//gs;
                   12714: 	    $initial_env{"browser.interface"} = $form->{'interface'};
                   12715: 	    $env{'browser.interface'}=$form->{'interface'};
                   12716: 	}
                   12717: 
1.981     raeburn  12718:         my %is_adv = ( is_adv => $env{'user.adv'} );
1.1016    raeburn  12719:         my %domdef;
                   12720:         unless ($domain eq 'public') {
                   12721:             %domdef = &Apache::lonnet::get_domain_defaults($domain);
                   12722:         }
1.980     raeburn  12723: 
1.724     raeburn  12724:         foreach my $tool ('aboutme','blog','portfolio') {
                   12725:             $userenv{'availabletools.'.$tool} = 
1.980     raeburn  12726:                 &Apache::lonnet::usertools_access($username,$domain,$tool,'reload',
                   12727:                                                   undef,\%userenv,\%domdef,\%is_adv);
1.724     raeburn  12728:         }
                   12729: 
1.864     raeburn  12730:         foreach my $crstype ('official','unofficial','community') {
1.765     raeburn  12731:             $userenv{'canrequest.'.$crstype} =
                   12732:                 &Apache::lonnet::usertools_access($username,$domain,$crstype,
1.980     raeburn  12733:                                                   'reload','requestcourses',
                   12734:                                                   \%userenv,\%domdef,\%is_adv);
1.765     raeburn  12735:         }
                   12736: 
1.462     albertel 12737: 	$env{'user.environment'} = "$lonids/$cookie.id";
                   12738: 	
                   12739: 	if (tie(my %disk_env,'GDBM_File',"$lonids/$cookie.id",
                   12740: 		 &GDBM_WRCREAT(),0640)) {
                   12741: 	    &_add_to_env(\%disk_env,\%initial_env);
                   12742: 	    &_add_to_env(\%disk_env,\%userenv,'environment.');
                   12743: 	    &_add_to_env(\%disk_env,$userroles);
1.463     albertel 12744: 	    if (ref($args->{'extra_env'})) {
                   12745: 		&_add_to_env(\%disk_env,$args->{'extra_env'});
                   12746: 	    }
1.462     albertel 12747: 	    untie(%disk_env);
                   12748: 	} else {
1.705     tempelho 12749: 	    &Apache::lonnet::logthis("<span style=\"color:blue;\">WARNING: ".
                   12750: 			   'Could not create environment storage in lonauth: '.$!.'</span>');
1.462     albertel 12751: 	    return 'error: '.$!;
                   12752: 	}
                   12753:     }
                   12754:     $env{'request.role'}='cm';
                   12755:     $env{'request.role.adv'}=$env{'user.adv'};
                   12756:     $env{'browser.type'}=$clientbrowser;
                   12757: 
                   12758:     return $cookie;
                   12759: 
                   12760: }
                   12761: 
                   12762: sub _add_to_env {
                   12763:     my ($idf,$env_data,$prefix) = @_;
1.676     raeburn  12764:     if (ref($env_data) eq 'HASH') {
                   12765:         while (my ($key,$value) = each(%$env_data)) {
                   12766: 	    $idf->{$prefix.$key} = $value;
                   12767: 	    $env{$prefix.$key}   = $value;
                   12768:         }
1.462     albertel 12769:     }
                   12770: }
                   12771: 
1.685     tempelho 12772: # --- Get the symbolic name of a problem and the url
                   12773: sub get_symb {
                   12774:     my ($request,$silent) = @_;
1.726     raeburn  12775:     (my $url=$env{'form.url'}) =~ s-^https?\://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
1.685     tempelho 12776:     my $symb=($env{'form.symb'} ne '' ? $env{'form.symb'} : (&Apache::lonnet::symbread($url)));
                   12777:     if ($symb eq '') {
                   12778:         if (!$silent) {
                   12779:             $request->print("Unable to handle ambiguous references:$url:.");
                   12780:             return ();
                   12781:         }
                   12782:     }
                   12783:     &Apache::lonenc::check_decrypt(\$symb);
                   12784:     return ($symb);
                   12785: }
                   12786: 
                   12787: # --------------------------------------------------------------Get annotation
                   12788: 
                   12789: sub get_annotation {
                   12790:     my ($symb,$enc) = @_;
                   12791: 
                   12792:     my $key = $symb;
                   12793:     if (!$enc) {
                   12794:         $key =
                   12795:             &Apache::lonnet::clutter((&Apache::lonnet::decode_symb($symb))[2]);
                   12796:     }
                   12797:     my %annotation=&Apache::lonnet::get('nohist_annotations',[$key]);
                   12798:     return $annotation{$key};
                   12799: }
                   12800: 
                   12801: sub clean_symb {
1.731     raeburn  12802:     my ($symb,$delete_enc) = @_;
1.685     tempelho 12803: 
                   12804:     &Apache::lonenc::check_decrypt(\$symb);
                   12805:     my $enc = $env{'request.enc'};
1.731     raeburn  12806:     if ($delete_enc) {
1.730     raeburn  12807:         delete($env{'request.enc'});
                   12808:     }
1.685     tempelho 12809: 
                   12810:     return ($symb,$enc);
                   12811: }
1.462     albertel 12812: 
1.990     raeburn  12813: sub build_release_hashes {
                   12814:     my ($checkparms,$checkresponsetypes,$checkcrstypes,$anonsurvey,$randomizetry) = @_;
                   12815:     return unless((ref($checkparms) eq 'HASH') && (ref($checkresponsetypes) eq 'HASH') &&
                   12816:                   (ref($checkcrstypes) eq 'HASH') && (ref($anonsurvey) eq 'HASH') &&
                   12817:                   (ref($randomizetry) eq 'HASH'));
                   12818:     foreach my $key (keys(%Apache::lonnet::needsrelease)) {
                   12819:         my ($item,$name,$value) = split(/:/,$key);
                   12820:         if ($item eq 'parameter') {
                   12821:             if (ref($checkparms->{$name}) eq 'ARRAY') {
                   12822:                 unless(grep(/^\Q$name\E$/,@{$checkparms->{$name}})) {
                   12823:                     push(@{$checkparms->{$name}},$value);
                   12824:                 }
                   12825:             } else {
                   12826:                 push(@{$checkparms->{$name}},$value);
                   12827:             }
                   12828:         } elsif ($item eq 'resourcetag') {
                   12829:             if ($name eq 'responsetype') {
                   12830:                 $checkresponsetypes->{$value} = $Apache::lonnet::needsrelease{$key}
                   12831:             }
                   12832:         } elsif ($item eq 'course') {
                   12833:             if ($name eq 'crstype') {
                   12834:                 $checkcrstypes->{$value} = $Apache::lonnet::needsrelease{$key};
                   12835:             }
                   12836:         }
                   12837:     }
                   12838:     ($anonsurvey->{major},$anonsurvey->{minor}) = split(/\./,$Apache::lonnet::needsrelease{'parameter:type:anonsurvey'});
                   12839:     ($randomizetry->{major},$randomizetry->{minor}) = split(/\./,$Apache::lonnet::needsrelease{'parameter:type:randomizetry'});
                   12840:     return;
                   12841: }
                   12842: 
1.41      ng       12843: =pod
                   12844: 
                   12845: =back
                   12846: 
1.112     bowersj2 12847: =cut
1.41      ng       12848: 
1.112     bowersj2 12849: 1;
                   12850: __END__;
1.41      ng       12851: 

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