File:  [LON-CAPA] / loncom / interface / loncommon.pm
Revision 1.1221: download - view: text, annotated - select for diffs
Thu May 21 23:40:09 2015 UTC (9 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Support checking of course cloning rights based on (a) domain defaults
  (where no course-specific rights set), or (b) course specific rights
  for official courses, based on institutional code category/ies set in
  courseprefs, e.g., department=phy etc.

    1: # The LearningOnline Network with CAPA
    2: # a pile of common routines
    3: #
    4: # $Id: loncommon.pm,v 1.1221 2015/05/21 23:40:09 raeburn Exp $
    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: #
   28: 
   29: # Makes a table out of the previous attempts
   30: # Inputs result_from_symbread, user, domain, course_id
   31: # Reads in non-network-related .tab files
   32: 
   33: # POD header:
   34: 
   35: =pod
   36: 
   37: =head1 NAME
   38: 
   39: Apache::loncommon - pile of common routines
   40: 
   41: =head1 SYNOPSIS
   42: 
   43: Common routines for manipulating connections, student answers,
   44:     domains, common Javascript fragments, etc.
   45: 
   46: =head1 OVERVIEW
   47: 
   48: A collection of commonly used subroutines that don't have a natural
   49: home anywhere else. This collection helps remove
   50: redundancy from other modules and increase efficiency of memory usage.
   51: 
   52: =cut 
   53: 
   54: # End of POD header
   55: package Apache::loncommon;
   56: 
   57: use strict;
   58: use Apache::lonnet;
   59: use GDBM_File;
   60: use POSIX qw(strftime mktime);
   61: use Apache::lonmenu();
   62: use Apache::lonenc();
   63: use Apache::lonlocal;
   64: use Apache::lonnet();
   65: use HTML::Entities;
   66: use Apache::lonhtmlcommon();
   67: use Apache::loncoursedata();
   68: use Apache::lontexconvert();
   69: use Apache::lonclonecourse();
   70: use Apache::lonuserutils();
   71: use Apache::lonuserstate();
   72: use Apache::courseclassifier();
   73: use LONCAPA qw(:DEFAULT :match);
   74: use DateTime::TimeZone;
   75: use DateTime::Locale::Catalog;
   76: use Encode();
   77: use Text::Aspell;
   78: use Authen::Captcha;
   79: use Captcha::reCAPTCHA;
   80: use Crypt::DES;
   81: use DynaLoader; # for Crypt::DES version
   82: 
   83: # ---------------------------------------------- Designs
   84: use vars qw(%defaultdesign);
   85: 
   86: my $readit;
   87: 
   88: 
   89: ##
   90: ## Global Variables
   91: ##
   92: 
   93: 
   94: # ----------------------------------------------- SSI with retries:
   95: #
   96: 
   97: =pod
   98: 
   99: =head1 Server Side include with retries:
  100: 
  101: =over 4
  102: 
  103: =item * &ssi_with_retries(resource,retries form)
  104: 
  105: Performs an ssi with some number of retries.  Retries continue either
  106: until the result is ok or until the retry count supplied by the
  107: caller is exhausted.  
  108: 
  109: Inputs:
  110: 
  111: =over 4
  112: 
  113: resource   - Identifies the resource to insert.
  114: 
  115: retries    - Count of the number of retries allowed.
  116: 
  117: form       - Hash that identifies the rendering options.
  118: 
  119: =back
  120: 
  121: Returns:
  122: 
  123: =over 4
  124: 
  125: content    - The content of the response.  If retries were exhausted this is empty.
  126: 
  127: response   - The response from the last attempt (which may or may not have been successful.
  128: 
  129: =back
  130: 
  131: =back
  132: 
  133: =cut
  134: 
  135: sub ssi_with_retries {
  136:     my ($resource, $retries, %form) = @_;
  137: 
  138: 
  139:     my $ok = 0;			# True if we got a good response.
  140:     my $content;
  141:     my $response;
  142: 
  143:     # Try to get the ssi done. within the retries count:
  144: 
  145:     do {
  146: 	($content, $response) = &Apache::lonnet::ssi($resource, %form);
  147: 	$ok      = $response->is_success;
  148:         if (!$ok) {
  149:             &Apache::lonnet::logthis("Failed ssi_with_retries on $resource: ".$response->is_success.', '.$response->code.', '.$response->message);
  150:         }
  151: 	$retries--;
  152:     } while (!$ok && ($retries > 0));
  153: 
  154:     if (!$ok) {
  155: 	$content = '';		# On error return an empty content.
  156:     }
  157:     return ($content, $response);
  158: 
  159: }
  160: 
  161: 
  162: 
  163: # ----------------------------------------------- Filetypes/Languages/Copyright
  164: my %language;
  165: my %supported_language;
  166: my %supported_codes;
  167: my %latex_language;		# For choosing hyphenation in <transl..>
  168: my %latex_language_bykey;	# for choosing hyphenation from metadata
  169: my %cprtag;
  170: my %scprtag;
  171: my %fe; my %fd; my %fm;
  172: my %category_extensions;
  173: 
  174: # ---------------------------------------------- Thesaurus variables
  175: #
  176: # %Keywords:
  177: #      A hash used by &keyword to determine if a word is considered a keyword.
  178: # $thesaurus_db_file 
  179: #      Scalar containing the full path to the thesaurus database.
  180: 
  181: my %Keywords;
  182: my $thesaurus_db_file;
  183: 
  184: #
  185: # Initialize values from language.tab, copyright.tab, filetypes.tab,
  186: # thesaurus.tab, and filecategories.tab.
  187: #
  188: BEGIN {
  189:     # Variable initialization
  190:     $thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
  191:     #
  192:     unless ($readit) {
  193: # ------------------------------------------------------------------- languages
  194:     {
  195:         my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
  196:                                    '/language.tab';
  197:         if ( open(my $fh,"<$langtabfile") ) {
  198:             while (my $line = <$fh>) {
  199:                 next if ($line=~/^\#/);
  200:                 chomp($line);
  201:                 my ($key,$code,$country,$three,$enc,$val,$sup,$latex)=(split(/\t/,$line));
  202:                 $language{$key}=$val.' - '.$enc;
  203:                 if ($sup) {
  204:                     $supported_language{$key}=$sup;
  205: 		    $supported_codes{$key}   = $code;
  206:                 }
  207: 		if ($latex) {
  208: 		    $latex_language_bykey{$key} = $latex;
  209: 		    $latex_language{$code} = $latex;
  210: 		}
  211:             }
  212:             close($fh);
  213:         }
  214:     }
  215: # ------------------------------------------------------------------ copyrights
  216:     {
  217:         my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
  218:                                   '/copyright.tab';
  219:         if ( open (my $fh,"<$copyrightfile") ) {
  220:             while (my $line = <$fh>) {
  221:                 next if ($line=~/^\#/);
  222:                 chomp($line);
  223:                 my ($key,$val)=(split(/\s+/,$line,2));
  224:                 $cprtag{$key}=$val;
  225:             }
  226:             close($fh);
  227:         }
  228:     }
  229: # ----------------------------------------------------------- source copyrights
  230:     {
  231:         my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
  232:                                   '/source_copyright.tab';
  233:         if ( open (my $fh,"<$sourcecopyrightfile") ) {
  234:             while (my $line = <$fh>) {
  235:                 next if ($line =~ /^\#/);
  236:                 chomp($line);
  237:                 my ($key,$val)=(split(/\s+/,$line,2));
  238:                 $scprtag{$key}=$val;
  239:             }
  240:             close($fh);
  241:         }
  242:     }
  243: 
  244: # -------------------------------------------------------------- default domain designs
  245:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
  246:     my $designfile = $designdir.'/default.tab';
  247:     if ( open (my $fh,"<$designfile") ) {
  248:         while (my $line = <$fh>) {
  249:             next if ($line =~ /^\#/);
  250:             chomp($line);
  251:             my ($key,$val)=(split(/\=/,$line));
  252:             if ($val) { $defaultdesign{$key}=$val; }
  253:         }
  254:         close($fh);
  255:     }
  256: 
  257: # ------------------------------------------------------------- file categories
  258:     {
  259:         my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
  260:                                   '/filecategories.tab';
  261:         if ( open (my $fh,"<$categoryfile") ) {
  262: 	    while (my $line = <$fh>) {
  263: 		next if ($line =~ /^\#/);
  264: 		chomp($line);
  265:                 my ($extension,$category)=(split(/\s+/,$line,2));
  266:                 push @{$category_extensions{lc($category)}},$extension;
  267:             }
  268:             close($fh);
  269:         }
  270: 
  271:     }
  272: # ------------------------------------------------------------------ file types
  273:     {
  274:         my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
  275:                '/filetypes.tab';
  276:         if ( open (my $fh,"<$typesfile") ) {
  277:             while (my $line = <$fh>) {
  278: 		next if ($line =~ /^\#/);
  279: 		chomp($line);
  280:                 my ($ending,$emb,$mime,$descr)=split(/\s+/,$line,4);
  281:                 if ($descr ne '') {
  282:                     $fe{$ending}=lc($emb);
  283:                     $fd{$ending}=$descr;
  284:                     if ($mime ne 'unk') { $fm{$ending}=$mime; }
  285:                 }
  286:             }
  287:             close($fh);
  288:         }
  289:     }
  290:     &Apache::lonnet::logthis(
  291:              "<span style='color:yellow;'>INFO: Read file types</span>");
  292:     $readit=1;
  293:     }  # end of unless($readit) 
  294:     
  295: }
  296: 
  297: ###############################################################
  298: ##           HTML and Javascript Helper Functions            ##
  299: ###############################################################
  300: 
  301: =pod 
  302: 
  303: =head1 HTML and Javascript Functions
  304: 
  305: =over 4
  306: 
  307: =item * &browser_and_searcher_javascript()
  308: 
  309: X<browsing, javascript>X<searching, javascript>Returns a string
  310: containing javascript with two functions, C<openbrowser> and
  311: C<opensearcher>. Returned string does not contain E<lt>scriptE<gt>
  312: tags.
  313: 
  314: =item * &openbrowser(formname,elementname,only,omit) [javascript]
  315: 
  316: inputs: formname, elementname, only, omit
  317: 
  318: formname and elementname indicate the name of the html form and name of
  319: the element that the results of the browsing selection are to be placed in. 
  320: 
  321: Specifying 'only' will restrict the browser to displaying only files
  322: with the given extension.  Can be a comma separated list.
  323: 
  324: Specifying 'omit' will restrict the browser to NOT displaying files
  325: with the given extension.  Can be a comma separated list.
  326: 
  327: =item * &opensearcher(formname,elementname) [javascript]
  328: 
  329: Inputs: formname, elementname
  330: 
  331: formname and elementname specify the name of the html form and the name
  332: of the element the selection from the search results will be placed in.
  333: 
  334: =cut
  335: 
  336: sub browser_and_searcher_javascript {
  337:     my ($mode)=@_;
  338:     if (!defined($mode)) { $mode='edit'; }
  339:     my $resurl=&escape_single(&lastresurl());
  340:     return <<END;
  341: // <!-- BEGIN LON-CAPA Internal
  342:     var editbrowser = null;
  343:     function openbrowser(formname,elementname,only,omit,titleelement) {
  344:         var url = '$resurl/?';
  345:         if (editbrowser == null) {
  346:             url += 'launch=1&';
  347:         }
  348:         url += 'catalogmode=interactive&';
  349:         url += 'mode=$mode&';
  350:         url += 'inhibitmenu=yes&';
  351:         url += 'form=' + formname + '&';
  352:         if (only != null) {
  353:             url += 'only=' + only + '&';
  354:         } else {
  355:             url += 'only=&';
  356: 	}
  357:         if (omit != null) {
  358:             url += 'omit=' + omit + '&';
  359:         } else {
  360:             url += 'omit=&';
  361: 	}
  362:         if (titleelement != null) {
  363:             url += 'titleelement=' + titleelement + '&';
  364:         } else {
  365: 	    url += 'titleelement=&';
  366: 	}
  367:         url += 'element=' + elementname + '';
  368:         var title = 'Browser';
  369:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
  370:         options += ',width=700,height=600';
  371:         editbrowser = open(url,title,options,'1');
  372:         editbrowser.focus();
  373:     }
  374:     var editsearcher;
  375:     function opensearcher(formname,elementname,titleelement) {
  376:         var url = '/adm/searchcat?';
  377:         if (editsearcher == null) {
  378:             url += 'launch=1&';
  379:         }
  380:         url += 'catalogmode=interactive&';
  381:         url += 'mode=$mode&';
  382:         url += 'form=' + formname + '&';
  383:         if (titleelement != null) {
  384:             url += 'titleelement=' + titleelement + '&';
  385:         } else {
  386: 	    url += 'titleelement=&';
  387: 	}
  388:         url += 'element=' + elementname + '';
  389:         var title = 'Search';
  390:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
  391:         options += ',width=700,height=600';
  392:         editsearcher = open(url,title,options,'1');
  393:         editsearcher.focus();
  394:     }
  395: // END LON-CAPA Internal -->
  396: END
  397: }
  398: 
  399: sub lastresurl {
  400:     if ($env{'environment.lastresurl'}) {
  401: 	return $env{'environment.lastresurl'}
  402:     } else {
  403: 	return '/res';
  404:     }
  405: }
  406: 
  407: sub storeresurl {
  408:     my $resurl=&Apache::lonnet::clutter(shift);
  409:     unless ($resurl=~/^\/res/) { return 0; }
  410:     $resurl=~s/\/$//;
  411:     &Apache::lonnet::put('environment',{'lastresurl' => $resurl});
  412:     &Apache::lonnet::appenv({'environment.lastresurl' => $resurl});
  413:     return 1;
  414: }
  415: 
  416: sub studentbrowser_javascript {
  417:    unless (
  418:             (($env{'request.course.id'}) && 
  419:              (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
  420: 	      || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
  421: 					  '/'.$env{'request.course.sec'})
  422: 	      ))
  423:          || ($env{'request.role'}=~/^(au|dc|su)/)
  424:           ) { return ''; }  
  425:    return (<<'ENDSTDBRW');
  426: <script type="text/javascript" language="Javascript">
  427: // <![CDATA[
  428:     var stdeditbrowser;
  429:     function openstdbrowser(formname,uname,udom,clicker,roleflag,ignorefilter,courseadvonly) {
  430:         var url = '/adm/pickstudent?';
  431:         var filter;
  432: 	if (!ignorefilter) {
  433: 	    eval('filter=document.'+formname+'.'+uname+'.value;');
  434: 	}
  435:         if (filter != null) {
  436:            if (filter != '') {
  437:                url += 'filter='+filter+'&';
  438: 	   }
  439:         }
  440:         url += 'form=' + formname + '&unameelement='+uname+
  441:                                     '&udomelement='+udom+
  442:                                     '&clicker='+clicker;
  443: 	if (roleflag) { url+="&roles=1"; }
  444:         if (courseadvonly) { url+="&courseadvonly=1"; }
  445:         var title = 'Student_Browser';
  446:         var options = 'scrollbars=1,resizable=1,menubar=0';
  447:         options += ',width=700,height=600';
  448:         stdeditbrowser = open(url,title,options,'1');
  449:         stdeditbrowser.focus();
  450:     }
  451: // ]]>
  452: </script>
  453: ENDSTDBRW
  454: }
  455: 
  456: sub resourcebrowser_javascript {
  457:    unless ($env{'request.course.id'}) { return ''; }
  458:    return (<<'ENDRESBRW');
  459: <script type="text/javascript" language="Javascript">
  460: // <![CDATA[
  461:     var reseditbrowser;
  462:     function openresbrowser(formname,reslink) {
  463:         var url = '/adm/pickresource?form='+formname+'&reslink='+reslink;
  464:         var title = 'Resource_Browser';
  465:         var options = 'scrollbars=1,resizable=1,menubar=0';
  466:         options += ',width=700,height=500';
  467:         reseditbrowser = open(url,title,options,'1');
  468:         reseditbrowser.focus();
  469:     }
  470: // ]]>
  471: </script>
  472: ENDRESBRW
  473: }
  474: 
  475: sub selectstudent_link {
  476:    my ($form,$unameele,$udomele,$courseadvonly,$clickerid)=@_;
  477:    my $callargs = "'".&Apache::lonhtmlcommon::entity_encode($form)."','".
  478:                       &Apache::lonhtmlcommon::entity_encode($unameele)."','".
  479:                       &Apache::lonhtmlcommon::entity_encode($udomele)."'";
  480:    if ($env{'request.course.id'}) {  
  481:        if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
  482: 	   && !&Apache::lonnet::allowed('srm',$env{'request.course.id'}.
  483: 					'/'.$env{'request.course.sec'})) {
  484: 	   return '';
  485:        }
  486:        $callargs.=",'".&Apache::lonhtmlcommon::entity_encode($clickerid)."'";
  487:        if ($courseadvonly)  {
  488:            $callargs .= ",'',1,1";
  489:        }
  490:        return '<span class="LC_nobreak">'.
  491:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
  492:               &mt('Select User').'</a></span>';
  493:    }
  494:    if ($env{'request.role'}=~/^(au|dc|su)/) {
  495:        $callargs .= ",'',1"; 
  496:        return '<span class="LC_nobreak">'.
  497:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
  498:               &mt('Select User').'</a></span>';
  499:    }
  500:    return '';
  501: }
  502: 
  503: sub selectresource_link {
  504:    my ($form,$reslink,$arg)=@_;
  505:    
  506:    my $callargs = "'".&Apache::lonhtmlcommon::entity_encode($form)."','".
  507:                       &Apache::lonhtmlcommon::entity_encode($reslink)."'";
  508:    unless ($env{'request.course.id'}) { return $arg; }
  509:    return '<span class="LC_nobreak">'.
  510:               '<a href="javascript:openresbrowser('.$callargs.');">'.
  511:               $arg.'</a></span>';
  512: }
  513: 
  514: 
  515: 
  516: sub authorbrowser_javascript {
  517:     return <<"ENDAUTHORBRW";
  518: <script type="text/javascript" language="JavaScript">
  519: // <![CDATA[
  520: var stdeditbrowser;
  521: 
  522: function openauthorbrowser(formname,udom) {
  523:     var url = '/adm/pickauthor?';
  524:     url += 'form='+formname+'&roledom='+udom;
  525:     var title = 'Author_Browser';
  526:     var options = 'scrollbars=1,resizable=1,menubar=0';
  527:     options += ',width=700,height=600';
  528:     stdeditbrowser = open(url,title,options,'1');
  529:     stdeditbrowser.focus();
  530: }
  531: 
  532: // ]]>
  533: </script>
  534: ENDAUTHORBRW
  535: }
  536: 
  537: sub coursebrowser_javascript {
  538:     my ($domainfilter,$sec_element,$formname,$role_element,$crstype,
  539:         $credits_element,$instcode) = @_;
  540:     my $wintitle = 'Course_Browser';
  541:     if ($crstype eq 'Community') {
  542:         $wintitle = 'Community_Browser';
  543:     }
  544:     my $id_functions = &javascript_index_functions();
  545:     my $output = '
  546: <script type="text/javascript" language="JavaScript">
  547: // <![CDATA[
  548:     var stdeditbrowser;'."\n";
  549: 
  550:     $output .= <<"ENDSTDBRW";
  551:     function opencrsbrowser(formname,uname,udom,desc,extra_element,multflag,type,type_elem) {
  552:         var url = '/adm/pickcourse?';
  553:         var formid = getFormIdByName(formname);
  554:         var domainfilter = getDomainFromSelectbox(formname,udom);
  555:         if (domainfilter != null) {
  556:            if (domainfilter != '') {
  557:                url += 'domainfilter='+domainfilter+'&';
  558: 	   }
  559:         }
  560:         url += 'form=' + formname + '&cnumelement='+uname+
  561: 	                            '&cdomelement='+udom+
  562:                                     '&cnameelement='+desc;
  563:         if (extra_element !=null && extra_element != '') {
  564:             if (formname == 'rolechoice' || formname == 'studentform') {
  565:                 url += '&roleelement='+extra_element;
  566:                 if (domainfilter == null || domainfilter == '') {
  567:                     url += '&domainfilter='+extra_element;
  568:                 }
  569:             }
  570:             else {
  571:                 if (formname == 'portform') {
  572:                     url += '&setroles='+extra_element;
  573:                 } else {
  574:                     if (formname == 'rules') {
  575:                         url += '&fixeddom='+extra_element; 
  576:                     }
  577:                 }
  578:             }     
  579:         }
  580:         if (type != null && type != '') {
  581:             url += '&type='+type;
  582:         }
  583:         if (type_elem != null && type_elem != '') {
  584:             url += '&typeelement='+type_elem;
  585:         }
  586:         if (formname == 'ccrs') {
  587:             var ownername = document.forms[formid].ccuname.value;
  588:             var ownerdom =  document.forms[formid].ccdomain.options[document.forms[formid].ccdomain.selectedIndex].value;
  589:             url += '&cloner='+ownername+':'+ownerdom+'&crscode='+document.forms[formid].crscode.value;
  590:         }
  591:         if (formname == 'requestcrs') {
  592:             url += '&crsdom=$domainfilter&crscode=$instcode';
  593:         }
  594:         if (multflag !=null && multflag != '') {
  595:             url += '&multiple='+multflag;
  596:         }
  597:         var title = '$wintitle';
  598:         var options = 'scrollbars=1,resizable=1,menubar=0';
  599:         options += ',width=700,height=600';
  600:         stdeditbrowser = open(url,title,options,'1');
  601:         stdeditbrowser.focus();
  602:     }
  603: $id_functions
  604: ENDSTDBRW
  605:     if (($sec_element ne '') || ($role_element ne '') || ($credits_element ne '')) {
  606:         $output .= &setsec_javascript($sec_element,$formname,$role_element,
  607:                                       $credits_element);
  608:     }
  609:     $output .= '
  610: // ]]>
  611: </script>';
  612:     return $output;
  613: }
  614: 
  615: sub javascript_index_functions {
  616:     return <<"ENDJS";
  617: 
  618: function getFormIdByName(formname) {
  619:     for (var i=0;i<document.forms.length;i++) {
  620:         if (document.forms[i].name == formname) {
  621:             return i;
  622:         }
  623:     }
  624:     return -1;
  625: }
  626: 
  627: function getIndexByName(formid,item) {
  628:     for (var i=0;i<document.forms[formid].elements.length;i++) {
  629:         if (document.forms[formid].elements[i].name == item) {
  630:             return i;
  631:         }
  632:     }
  633:     return -1;
  634: }
  635: 
  636: function getDomainFromSelectbox(formname,udom) {
  637:     var userdom;
  638:     var formid = getFormIdByName(formname);
  639:     if (formid > -1) {
  640:         var domid = getIndexByName(formid,udom);
  641:         if (domid > -1) {
  642:             if (document.forms[formid].elements[domid].type == 'select-one') {
  643:                 userdom=document.forms[formid].elements[domid].options[document.forms[formid].elements[domid].selectedIndex].value;
  644:             }
  645:             if (document.forms[formid].elements[domid].type == 'hidden') {
  646:                 userdom=document.forms[formid].elements[domid].value;
  647:             }
  648:         }
  649:     }
  650:     return userdom;
  651: }
  652: 
  653: ENDJS
  654: 
  655: }
  656: 
  657: sub javascript_array_indexof {
  658:     return <<ENDJS;
  659: <script type="text/javascript" language="JavaScript">
  660: // <![CDATA[
  661: 
  662: if (!Array.prototype.indexOf) {
  663:     Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
  664:         "use strict";
  665:         if (this === void 0 || this === null) {
  666:             throw new TypeError();
  667:         }
  668:         var t = Object(this);
  669:         var len = t.length >>> 0;
  670:         if (len === 0) {
  671:             return -1;
  672:         }
  673:         var n = 0;
  674:         if (arguments.length > 0) {
  675:             n = Number(arguments[1]);
  676:             if (n !== n) { // shortcut for verifying if it is NaN
  677:                 n = 0;
  678:             } else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
  679:                 n = (n > 0 || -1) * Math.floor(Math.abs(n));
  680:             }
  681:         }
  682:         if (n >= len) {
  683:             return -1;
  684:         }
  685:         var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
  686:         for (; k < len; k++) {
  687:             if (k in t && t[k] === searchElement) {
  688:                 return k;
  689:             }
  690:         }
  691:         return -1;
  692:     }
  693: }
  694: 
  695: // ]]>
  696: </script>
  697: 
  698: ENDJS
  699: 
  700: }
  701: 
  702: sub userbrowser_javascript {
  703:     my $id_functions = &javascript_index_functions();
  704:     return <<"ENDUSERBRW";
  705: 
  706: function openuserbrowser(formname,uname,udom,ulast,ufirst,uemail,hideudom,crsdom,caller) {
  707:     var url = '/adm/pickuser?';
  708:     var userdom = getDomainFromSelectbox(formname,udom);
  709:     if (userdom != null) {
  710:        if (userdom != '') {
  711:            url += 'srchdom='+userdom+'&';
  712:        }
  713:     }
  714:     url += 'form=' + formname + '&unameelement='+uname+
  715:                                 '&udomelement='+udom+
  716:                                 '&ulastelement='+ulast+
  717:                                 '&ufirstelement='+ufirst+
  718:                                 '&uemailelement='+uemail+
  719:                                 '&hideudomelement='+hideudom+
  720:                                 '&coursedom='+crsdom;
  721:     if ((caller != null) && (caller != undefined)) {
  722:         url += '&caller='+caller;
  723:     }
  724:     var title = 'User_Browser';
  725:     var options = 'scrollbars=1,resizable=1,menubar=0';
  726:     options += ',width=700,height=600';
  727:     var stdeditbrowser = open(url,title,options,'1');
  728:     stdeditbrowser.focus();
  729: }
  730: 
  731: function fix_domain (formname,udom,origdom,uname) {
  732:     var formid = getFormIdByName(formname);
  733:     if (formid > -1) {
  734:         var unameid = getIndexByName(formid,uname);
  735:         var domid = getIndexByName(formid,udom);
  736:         var hidedomid = getIndexByName(formid,origdom);
  737:         if (hidedomid > -1) {
  738:             var fixeddom = document.forms[formid].elements[hidedomid].value;
  739:             var unameval = document.forms[formid].elements[unameid].value;
  740:             if ((fixeddom != '') && (fixeddom != undefined) && (fixeddom != null) && (unameval != '') && (unameval != undefined) && (unameval != null)) {
  741:                 if (domid > -1) {
  742:                     var slct = document.forms[formid].elements[domid];
  743:                     if (slct.type == 'select-one') {
  744:                         var i;
  745:                         for (i=0;i<slct.length;i++) {
  746:                             if (slct.options[i].value==fixeddom) { slct.selectedIndex=i; }
  747:                         }
  748:                     }
  749:                     if (slct.type == 'hidden') {
  750:                         slct.value = fixeddom;
  751:                     }
  752:                 }
  753:             }
  754:         }
  755:     }
  756:     return;
  757: }
  758: 
  759: $id_functions
  760: ENDUSERBRW
  761: }
  762: 
  763: sub setsec_javascript {
  764:     my ($sec_element,$formname,$role_element,$credits_element) = @_;
  765:     my (@courserolenames,@communityrolenames,$rolestr,$courserolestr,
  766:         $communityrolestr);
  767:     if ($role_element ne '') {
  768:         my @allroles = ('st','ta','ep','in','ad');
  769:         foreach my $crstype ('Course','Community') {
  770:             if ($crstype eq 'Community') {
  771:                 foreach my $role (@allroles) {
  772:                     push(@communityrolenames,&Apache::lonnet::plaintext($role,$crstype));
  773:                 }
  774:                 push(@communityrolenames,&Apache::lonnet::plaintext('co'));
  775:             } else {
  776:                 foreach my $role (@allroles) {
  777:                     push(@courserolenames,&Apache::lonnet::plaintext($role,$crstype));
  778:                 }
  779:                 push(@courserolenames,&Apache::lonnet::plaintext('cc'));
  780:             }
  781:         }
  782:         $rolestr = '"'.join('","',@allroles).'"';
  783:         $courserolestr = '"'.join('","',@courserolenames).'"';
  784:         $communityrolestr = '"'.join('","',@communityrolenames).'"';
  785:     }
  786:     my $setsections = qq|
  787: function setSect(sectionlist) {
  788:     var sectionsArray = new Array();
  789:     if ((sectionlist != '') && (typeof sectionlist != "undefined")) {
  790:         sectionsArray = sectionlist.split(",");
  791:     }
  792:     var numSections = sectionsArray.length;
  793:     document.$formname.$sec_element.length = 0;
  794:     if (numSections == 0) {
  795:         document.$formname.$sec_element.multiple=false;
  796:         document.$formname.$sec_element.size=1;
  797:         document.$formname.$sec_element.options[0] = new Option('No existing sections','',false,false)
  798:     } else {
  799:         if (numSections == 1) {
  800:             document.$formname.$sec_element.multiple=false;
  801:             document.$formname.$sec_element.size=1;
  802:             document.$formname.$sec_element.options[0] = new Option('Select','',true,true);
  803:             document.$formname.$sec_element.options[1] = new Option('No section','',false,false)
  804:             document.$formname.$sec_element.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
  805:         } else {
  806:             for (var i=0; i<numSections; i++) {
  807:                 document.$formname.$sec_element.options[i] = new Option(sectionsArray[i],sectionsArray[i],false,false)
  808:             }
  809:             document.$formname.$sec_element.multiple=true
  810:             if (numSections < 3) {
  811:                 document.$formname.$sec_element.size=numSections;
  812:             } else {
  813:                 document.$formname.$sec_element.size=3;
  814:             }
  815:             document.$formname.$sec_element.options[0].selected = false
  816:         }
  817:     }
  818: }
  819: 
  820: function setRole(crstype) {
  821: |;
  822:     if ($role_element eq '') {
  823:         $setsections .= '    return;
  824: }
  825: ';
  826:     } else {
  827:         $setsections .= qq|
  828:     var elementLength = document.$formname.$role_element.length;
  829:     var allroles = Array($rolestr);
  830:     var courserolenames = Array($courserolestr);
  831:     var communityrolenames = Array($communityrolestr);
  832:     if (elementLength != undefined) {
  833:         if (document.$formname.$role_element.options[5].value == 'cc') {
  834:             if (crstype == 'Course') {
  835:                 return;
  836:             } else {
  837:                 allroles[5] = 'co';
  838:                 for (var i=0; i<6; i++) {
  839:                     document.$formname.$role_element.options[i].value = allroles[i];
  840:                     document.$formname.$role_element.options[i].text = communityrolenames[i];
  841:                 }
  842:             }
  843:         } else {
  844:             if (crstype == 'Community') {
  845:                 return;
  846:             } else {
  847:                 allroles[5] = 'cc';
  848:                 for (var i=0; i<6; i++) {
  849:                     document.$formname.$role_element.options[i].value = allroles[i];
  850:                     document.$formname.$role_element.options[i].text = courserolenames[i];
  851:                 }
  852:             }
  853:         }
  854:     }
  855:     return;
  856: }
  857: |;
  858:     }
  859:     if ($credits_element) {
  860:         $setsections .= qq|
  861: function setCredits(defaultcredits) {
  862:     document.$formname.$credits_element.value = defaultcredits;
  863:     return;
  864: }
  865: |;
  866:     }
  867:     return $setsections;
  868: }
  869: 
  870: sub selectcourse_link {
  871:    my ($form,$unameele,$udomele,$desc,$extra_element,$multflag,$selecttype,
  872:        $typeelement) = @_;
  873:    my $type = $selecttype;
  874:    my $linktext = &mt('Select Course');
  875:    if ($selecttype eq 'Community') {
  876:        $linktext = &mt('Select Community');
  877:    } elsif ($selecttype eq 'Course/Community') {
  878:        $linktext = &mt('Select Course/Community');
  879:        $type = '';
  880:    } elsif ($selecttype eq 'Select') {
  881:        $linktext = &mt('Select');
  882:        $type = '';
  883:    }
  884:    return '<span class="LC_nobreak">'
  885:          ."<a href='"
  886:          .'javascript:opencrsbrowser("'.$form.'","'.$unameele
  887:          .'","'.$udomele.'","'.$desc.'","'.$extra_element
  888:          .'","'.$multflag.'","'.$type.'","'.$typeelement.'");'
  889:          ."'>".$linktext.'</a>'
  890:          .'</span>';
  891: }
  892: 
  893: sub selectauthor_link {
  894:    my ($form,$udom)=@_;
  895:    return '<a href="javascript:openauthorbrowser('."'$form','$udom'".');">'.
  896:           &mt('Select Author').'</a>';
  897: }
  898: 
  899: sub selectuser_link {
  900:     my ($form,$unameelem,$domelem,$lastelem,$firstelem,$emailelem,$hdomelem,
  901:         $coursedom,$linktext,$caller) = @_;
  902:     return '<a href="javascript:openuserbrowser('."'$form','$unameelem','$domelem',".
  903:            "'$lastelem','$firstelem','$emailelem','$hdomelem','$coursedom','$caller'".
  904:            ');">'.$linktext.'</a>';
  905: }
  906: 
  907: sub check_uncheck_jscript {
  908:     my $jscript = <<"ENDSCRT";
  909: function checkAll(field) {
  910:     if (field.length > 0) {
  911:         for (i = 0; i < field.length; i++) {
  912:             if (!field[i].disabled) { 
  913:                 field[i].checked = true;
  914:             }
  915:         }
  916:     } else {
  917:         if (!field.disabled) { 
  918:             field.checked = true;
  919:         }
  920:     }
  921: }
  922:  
  923: function uncheckAll(field) {
  924:     if (field.length > 0) {
  925:         for (i = 0; i < field.length; i++) {
  926:             field[i].checked = false ;
  927:         }
  928:     } else {
  929:         field.checked = false ;
  930:     }
  931: }
  932: ENDSCRT
  933:     return $jscript;
  934: }
  935: 
  936: sub select_timezone {
  937:    my ($name,$selected,$onchange,$includeempty)=@_;
  938:    my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
  939:    if ($includeempty) {
  940:        $output .= '<option value=""';
  941:        if (($selected eq '') || ($selected eq 'local')) {
  942:            $output .= ' selected="selected" ';
  943:        }
  944:        $output .= '> </option>';
  945:    }
  946:    my @timezones = DateTime::TimeZone->all_names;
  947:    foreach my $tzone (@timezones) {
  948:        $output.= '<option value="'.$tzone.'"';
  949:        if ($tzone eq $selected) {
  950:            $output.=' selected="selected"';
  951:        }
  952:        $output.=">$tzone</option>\n";
  953:    }
  954:    $output.="</select>";
  955:    return $output;
  956: }
  957: 
  958: sub select_datelocale {
  959:     my ($name,$selected,$onchange,$includeempty)=@_;
  960:     my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
  961:     if ($includeempty) {
  962:         $output .= '<option value=""';
  963:         if ($selected eq '') {
  964:             $output .= ' selected="selected" ';
  965:         }
  966:         $output .= '> </option>';
  967:     }
  968:     my (@possibles,%locale_names);
  969:     my @locales = DateTime::Locale::Catalog::Locales;
  970:     foreach my $locale (@locales) {
  971:         if (ref($locale) eq 'HASH') {
  972:             my $id = $locale->{'id'};
  973:             if ($id ne '') {
  974:                 my $en_terr = $locale->{'en_territory'};
  975:                 my $native_terr = $locale->{'native_territory'};
  976:                 my @languages = &Apache::lonlocal::preferred_languages();
  977:                 if (grep(/^en$/,@languages) || !@languages) {
  978:                     if ($en_terr ne '') {
  979:                         $locale_names{$id} = '('.$en_terr.')';
  980:                     } elsif ($native_terr ne '') {
  981:                         $locale_names{$id} = $native_terr;
  982:                     }
  983:                 } else {
  984:                     if ($native_terr ne '') {
  985:                         $locale_names{$id} = $native_terr.' ';
  986:                     } elsif ($en_terr ne '') {
  987:                         $locale_names{$id} = '('.$en_terr.')';
  988:                     }
  989:                 }
  990:                 $locale_names{$id} = Encode::encode('UTF-8',$locale_names{$id});
  991:                 push (@possibles,$id);
  992:             }
  993:         }
  994:     }
  995:     foreach my $item (sort(@possibles)) {
  996:         $output.= '<option value="'.$item.'"';
  997:         if ($item eq $selected) {
  998:             $output.=' selected="selected"';
  999:         }
 1000:         $output.=">$item";
 1001:         if ($locale_names{$item} ne '') {
 1002:             $output.='  '.$locale_names{$item};
 1003:         }
 1004:         $output.="</option>\n";
 1005:     }
 1006:     $output.="</select>";
 1007:     return $output;
 1008: }
 1009: 
 1010: sub select_language {
 1011:     my ($name,$selected,$includeempty) = @_;
 1012:     my %langchoices;
 1013:     if ($includeempty) {
 1014:         %langchoices = ('' => 'No language preference');
 1015:     }
 1016:     foreach my $id (&languageids()) {
 1017:         my $code = &supportedlanguagecode($id);
 1018:         if ($code) {
 1019:             $langchoices{$code} = &plainlanguagedescription($id);
 1020:         }
 1021:     }
 1022:     %langchoices = &Apache::lonlocal::texthash(%langchoices);
 1023:     return &select_form($selected,$name,\%langchoices);
 1024: }
 1025: 
 1026: =pod
 1027: 
 1028: 
 1029: =item * &list_languages()
 1030: 
 1031: Returns an array reference that is suitable for use in language prompters.
 1032: Each array element is itself a two element array.  The first element
 1033: is the language code.  The second element a descsriptiuon of the 
 1034: language itself.  This is suitable for use in e.g.
 1035: &Apache::edit::select_arg (once dereferenced that is).
 1036: 
 1037: =cut 
 1038: 
 1039: sub list_languages {
 1040:     my @lang_choices;
 1041: 
 1042:     foreach my $id (&languageids()) {
 1043: 	my $code = &supportedlanguagecode($id);
 1044: 	if ($code) {
 1045: 	    my $selector    = $supported_codes{$id};
 1046: 	    my $description = &plainlanguagedescription($id);
 1047: 	    push (@lang_choices, [$selector, $description]);
 1048: 	}
 1049:     }
 1050:     return \@lang_choices;
 1051: }
 1052: 
 1053: =pod
 1054: 
 1055: =item * &linked_select_forms(...)
 1056: 
 1057: linked_select_forms returns a string containing a <script></script> block
 1058: and html for two <select> menus.  The select menus will be linked in that
 1059: changing the value of the first menu will result in new values being placed
 1060: in the second menu.  The values in the select menu will appear in alphabetical
 1061: order unless a defined order is provided.
 1062: 
 1063: linked_select_forms takes the following ordered inputs:
 1064: 
 1065: =over 4
 1066: 
 1067: =item * $formname, the name of the <form> tag
 1068: 
 1069: =item * $middletext, the text which appears between the <select> tags
 1070: 
 1071: =item * $firstdefault, the default value for the first menu
 1072: 
 1073: =item * $firstselectname, the name of the first <select> tag
 1074: 
 1075: =item * $secondselectname, the name of the second <select> tag
 1076: 
 1077: =item * $hashref, a reference to a hash containing the data for the menus.
 1078: 
 1079: =item * $menuorder, the order of values in the first menu
 1080: 
 1081: =item * $onchangefirst, additional javascript call to execute for an onchange
 1082:         event for the first <select> tag
 1083: 
 1084: =item * $onchangesecond, additional javascript call to execute for an onchange
 1085:         event for the second <select> tag
 1086: 
 1087: =back 
 1088: 
 1089: Below is an example of such a hash.  Only the 'text', 'default', and 
 1090: 'select2' keys must appear as stated.  keys(%menu) are the possible 
 1091: values for the first select menu.  The text that coincides with the 
 1092: first menu value is given in $menu{$choice1}->{'text'}.  The values 
 1093: and text for the second menu are given in the hash pointed to by 
 1094: $menu{$choice1}->{'select2'}.  
 1095: 
 1096:  my %menu = ( A1 => { text =>"Choice A1" ,
 1097:                        default => "B3",
 1098:                        select2 => { 
 1099:                            B1 => "Choice B1",
 1100:                            B2 => "Choice B2",
 1101:                            B3 => "Choice B3",
 1102:                            B4 => "Choice B4"
 1103:                            },
 1104:                        order => ['B4','B3','B1','B2'],
 1105:                    },
 1106:                A2 => { text =>"Choice A2" ,
 1107:                        default => "C2",
 1108:                        select2 => { 
 1109:                            C1 => "Choice C1",
 1110:                            C2 => "Choice C2",
 1111:                            C3 => "Choice C3"
 1112:                            },
 1113:                        order => ['C2','C1','C3'],
 1114:                    },
 1115:                A3 => { text =>"Choice A3" ,
 1116:                        default => "D6",
 1117:                        select2 => { 
 1118:                            D1 => "Choice D1",
 1119:                            D2 => "Choice D2",
 1120:                            D3 => "Choice D3",
 1121:                            D4 => "Choice D4",
 1122:                            D5 => "Choice D5",
 1123:                            D6 => "Choice D6",
 1124:                            D7 => "Choice D7"
 1125:                            },
 1126:                        order => ['D4','D3','D2','D1','D7','D6','D5'],
 1127:                    }
 1128:                );
 1129: 
 1130: =cut
 1131: 
 1132: sub linked_select_forms {
 1133:     my ($formname,
 1134:         $middletext,
 1135:         $firstdefault,
 1136:         $firstselectname,
 1137:         $secondselectname, 
 1138:         $hashref,
 1139:         $menuorder,
 1140:         $onchangefirst,
 1141:         $onchangesecond
 1142:         ) = @_;
 1143:     my $second = "document.$formname.$secondselectname";
 1144:     my $first = "document.$formname.$firstselectname";
 1145:     # output the javascript to do the changing
 1146:     my $result = '';
 1147:     $result.='<script type="text/javascript" language="JavaScript">'."\n";
 1148:     $result.="// <![CDATA[\n";
 1149:     $result.="var select2data = new Object();\n";
 1150:     $" = '","';
 1151:     my $debug = '';
 1152:     foreach my $s1 (sort(keys(%$hashref))) {
 1153:         $result.="select2data.d_$s1 = new Object();\n";        
 1154:         $result.="select2data.d_$s1.def = new String('".
 1155:             $hashref->{$s1}->{'default'}."');\n";
 1156:         $result.="select2data.d_$s1.values = new Array(";
 1157:         my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
 1158:         if (ref($hashref->{$s1}->{'order'}) eq 'ARRAY') {
 1159:             @s2values = @{$hashref->{$s1}->{'order'}};
 1160:         }
 1161:         $result.="\"@s2values\");\n";
 1162:         $result.="select2data.d_$s1.texts = new Array(";        
 1163:         my @s2texts;
 1164:         foreach my $value (@s2values) {
 1165:             push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
 1166:         }
 1167:         $result.="\"@s2texts\");\n";
 1168:     }
 1169:     $"=' ';
 1170:     $result.= <<"END";
 1171: 
 1172: function select1_changed() {
 1173:     // Determine new choice
 1174:     var newvalue = "d_" + $first.value;
 1175:     // update select2
 1176:     var values     = select2data[newvalue].values;
 1177:     var texts      = select2data[newvalue].texts;
 1178:     var select2def = select2data[newvalue].def;
 1179:     var i;
 1180:     // out with the old
 1181:     for (i = 0; i < $second.options.length; i++) {
 1182:         $second.options[i] = null;
 1183:     }
 1184:     // in with the nuclear
 1185:     for (i=0;i<values.length; i++) {
 1186:         $second.options[i] = new Option(values[i]);
 1187:         $second.options[i].value = values[i];
 1188:         $second.options[i].text = texts[i];
 1189:         if (values[i] == select2def) {
 1190:             $second.options[i].selected = true;
 1191:         }
 1192:     }
 1193: }
 1194: // ]]>
 1195: </script>
 1196: END
 1197:     # output the initial values for the selection lists
 1198:     $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed();$onchangefirst\">\n";
 1199:     my @order = sort(keys(%{$hashref}));
 1200:     if (ref($menuorder) eq 'ARRAY') {
 1201:         @order = @{$menuorder};
 1202:     }
 1203:     foreach my $value (@order) {
 1204:         $result.="    <option value=\"$value\" ";
 1205:         $result.=" selected=\"selected\" " if ($value eq $firstdefault);
 1206:         $result.=">".&mt($hashref->{$value}->{'text'})."</option>\n";
 1207:     }
 1208:     $result .= "</select>\n";
 1209:     my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
 1210:     $result .= $middletext;
 1211:     $result .= "<select size=\"1\" name=\"$secondselectname\"";
 1212:     if ($onchangesecond) {
 1213:         $result .= ' onchange="'.$onchangesecond.'"';
 1214:     }
 1215:     $result .= ">\n";
 1216:     my $seconddefault = $hashref->{$firstdefault}->{'default'};
 1217:     
 1218:     my @secondorder = sort(keys(%select2));
 1219:     if (ref($hashref->{$firstdefault}->{'order'}) eq 'ARRAY') {
 1220:         @secondorder = @{$hashref->{$firstdefault}->{'order'}};
 1221:     }
 1222:     foreach my $value (@secondorder) {
 1223:         $result.="    <option value=\"$value\" ";        
 1224:         $result.=" selected=\"selected\" " if ($value eq $seconddefault);
 1225:         $result.=">".&mt($select2{$value})."</option>\n";
 1226:     }
 1227:     $result .= "</select>\n";
 1228:     #    return $debug;
 1229:     return $result;
 1230: }   #  end of sub linked_select_forms {
 1231: 
 1232: =pod
 1233: 
 1234: =item * &help_open_topic($topic,$text,$stayOnPage,$width,$height,$imgid)
 1235: 
 1236: Returns a string corresponding to an HTML link to the given help
 1237: $topic, where $topic corresponds to the name of a .tex file in
 1238: /home/httpd/html/adm/help/tex, with underscores replaced by
 1239: spaces. 
 1240: 
 1241: $text will optionally be linked to the same topic, allowing you to
 1242: link text in addition to the graphic. If you do not want to link
 1243: text, but wish to specify one of the later parameters, pass an
 1244: empty string. 
 1245: 
 1246: $stayOnPage is a value that will be interpreted as a boolean. If true,
 1247: the link will not open a new window. If false, the link will open
 1248: a new window using Javascript. (Default is false.) 
 1249: 
 1250: $width and $height are optional numerical parameters that will
 1251: override the width and height of the popped up window, which may
 1252: be useful for certain help topics with big pictures included.
 1253: 
 1254: $imgid is the id of the img tag used for the help icon. This may be
 1255: used in a javascript call to switch the image src.  See 
 1256: lonhtmlcommon::htmlareaselectactive() for an example.
 1257: 
 1258: =cut
 1259: 
 1260: sub help_open_topic {
 1261:     my ($topic, $text, $stayOnPage, $width, $height, $imgid) = @_;
 1262:     $text = "" if (not defined $text);
 1263:     $stayOnPage = 0 if (not defined $stayOnPage);
 1264:     $width = 500 if (not defined $width);
 1265:     $height = 400 if (not defined $height);
 1266:     my $filename = $topic;
 1267:     $filename =~ s/ /_/g;
 1268: 
 1269:     my $template = "";
 1270:     my $link;
 1271:     
 1272:     $topic=~s/\W/\_/g;
 1273: 
 1274:     if (!$stayOnPage) {
 1275: 	$link = "javascript:openMyModal('/adm/help/${filename}.hlp',$width,$height,'yes');";
 1276:     } elsif ($stayOnPage eq 'popup') {
 1277:         $link = "javascript:void(open('/adm/help/${filename}.hlp', 'Help_for_$topic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
 1278:     } else {
 1279: 	$link = "/adm/help/${filename}.hlp";
 1280:     }
 1281: 
 1282:     # Add the text
 1283:     if ($text ne "") {	
 1284: 	$template.='<span class="LC_help_open_topic">'
 1285:                   .'<a target="_top" href="'.$link.'">'
 1286:                   .$text.'</a>';
 1287:     }
 1288: 
 1289:     # (Always) Add the graphic
 1290:     my $title = &mt('Online Help');
 1291:     my $helpicon=&lonhttpdurl("/adm/help/help.png");
 1292:     if ($imgid ne '') {
 1293:         $imgid = ' id="'.$imgid.'"';
 1294:     }
 1295:     $template.=' <a target="_top" href="'.$link.'" title="'.$title.'">'
 1296:               .'<img src="'.$helpicon.'" border="0"'
 1297:               .' alt="'.&mt('Help: [_1]',$topic).'"'
 1298:               .' title="'.$title.'" style="vertical-align:middle;"'.$imgid 
 1299:               .' /></a>';
 1300:     if ($text ne "") {	
 1301:         $template.='</span>';
 1302:     }
 1303:     return $template;
 1304: 
 1305: }
 1306: 
 1307: # This is a quicky function for Latex cheatsheet editing, since it 
 1308: # appears in at least four places
 1309: sub helpLatexCheatsheet {
 1310:     my ($topic,$text,$not_author,$stayOnPage) = @_;
 1311:     my $out;
 1312:     my $addOther = '';
 1313:     if ($topic) {
 1314: 	$addOther = '<span>'.&help_open_topic($topic,&mt($text),$stayOnPage, undef, 600).'</span> ';
 1315:     }
 1316:     $out = '<span>' # Start cheatsheet
 1317: 	  .$addOther
 1318:           .'<span>'
 1319: 	  .&help_open_topic('Greek_Symbols',&mt('Greek Symbols'),$stayOnPage,undef,600)
 1320: 	  .'</span> <span>'
 1321: 	  .&help_open_topic('Other_Symbols',&mt('Other Symbols'),$stayOnPage,undef,600)
 1322: 	  .'</span>';
 1323:     unless ($not_author) {
 1324:         $out .= '<span>'
 1325:                .&help_open_topic('Authoring_Output_Tags',&mt('Output Tags'),$stayOnPage,undef,600)
 1326:                .'</span> <span>'
 1327:                .&help_open_topic('Authoring_Multilingual_Problems',&mt('How to create problems in different languages'),$stayOnPage,undef,600)
 1328: 	       .'</span>';
 1329:     }
 1330:     $out .= '</span>'; # End cheatsheet
 1331:     return $out;
 1332: }
 1333: 
 1334: sub general_help {
 1335:     my $helptopic='Student_Intro';
 1336:     if ($env{'request.role'}=~/^(ca|au)/) {
 1337: 	$helptopic='Authoring_Intro';
 1338:     } elsif ($env{'request.role'}=~/^(cc|co)/) {
 1339: 	$helptopic='Course_Coordination_Intro';
 1340:     } elsif ($env{'request.role'}=~/^dc/) {
 1341:         $helptopic='Domain_Coordination_Intro';
 1342:     }
 1343:     return $helptopic;
 1344: }
 1345: 
 1346: sub update_help_link {
 1347:     my ($topic,$component_help,$faq,$bug,$stayOnPage) = @_;
 1348:     my $origurl = $ENV{'REQUEST_URI'};
 1349:     $origurl=~s|^/~|/priv/|;
 1350:     my $timestamp = time;
 1351:     foreach my $datum (\$topic,\$component_help,\$faq,\$bug,\$origurl) {
 1352:         $$datum = &escape($$datum);
 1353:     }
 1354: 
 1355:     my $banner_link = "/adm/helpmenu?page=banner&amp;topic=$topic&amp;component_help=$component_help&amp;faq=$faq&amp;bug=$bug&amp;origurl=$origurl&amp;stamp=$timestamp&amp;stayonpage=$stayOnPage";
 1356:     my $output .= <<"ENDOUTPUT";
 1357: <script type="text/javascript">
 1358: // <![CDATA[
 1359: banner_link = '$banner_link';
 1360: // ]]>
 1361: </script>
 1362: ENDOUTPUT
 1363:     return $output;
 1364: }
 1365: 
 1366: # now just updates the help link and generates a blue icon
 1367: sub help_open_menu {
 1368:     my ($topic,$component_help,$faq,$bug,$stayOnPage,$width,$height,$text) 
 1369: 	= @_;    
 1370:     $stayOnPage = 1;
 1371:     my $output;
 1372:     if ($component_help) {
 1373: 	if (!$text) {
 1374: 	    $output=&help_open_topic($component_help,undef,$stayOnPage,
 1375: 				       $width,$height);
 1376: 	} else {
 1377: 	    my $help_text;
 1378: 	    $help_text=&unescape($topic);
 1379: 	    $output='<table><tr><td>'.
 1380: 		&help_open_topic($component_help,$help_text,$stayOnPage,
 1381: 				 $width,$height).'</td></tr></table>';
 1382: 	}
 1383:     }
 1384:     my $banner_link = &update_help_link($topic,$component_help,$faq,$bug,$stayOnPage);
 1385:     return $output.$banner_link;
 1386: }
 1387: 
 1388: sub top_nav_help {
 1389:     my ($text) = @_;
 1390:     $text = &mt($text);
 1391:     my $stay_on_page = 1;
 1392: 
 1393:     my ($link,$banner_link);
 1394:     unless ($env{'request.noversionuri'} =~ m{^/adm/helpmenu}) {
 1395:         $link = ($stay_on_page) ? "javascript:helpMenu('display')"
 1396: 	                         : "javascript:helpMenu('open')";
 1397:         $banner_link = &update_help_link(undef,undef,undef,undef,$stay_on_page);
 1398:     }
 1399:     my $title = &mt('Get help');
 1400:     if ($link) {
 1401:         return <<"END";
 1402: $banner_link
 1403: <a href="$link" title="$title">$text</a>
 1404: END
 1405:     } else {
 1406:         return '&nbsp;'.$text.'&nbsp;';
 1407:     }
 1408: }
 1409: 
 1410: sub help_menu_js {
 1411:     my ($httphost) = @_;
 1412:     my $stayOnPage = 1;
 1413:     my $width = 620;
 1414:     my $height = 600;
 1415:     my $helptopic=&general_help();
 1416:     my $details_link = $httphost.'/adm/help/'.$helptopic.'.hlp';
 1417:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
 1418:     my $start_page =
 1419:         &Apache::loncommon::start_page('Help Menu', undef,
 1420: 				       {'frameset'    => 1,
 1421: 					'js_ready'    => 1,
 1422:                                         'use_absolute' => $httphost,
 1423: 					'add_entries' => {
 1424: 					    'border' => '0', 
 1425: 					    'rows'   => "110,*",},});
 1426:     my $end_page =
 1427:         &Apache::loncommon::end_page({'frameset' => 1,
 1428: 				      'js_ready' => 1,});
 1429: 
 1430:     my $template .= <<"ENDTEMPLATE";
 1431: <script type="text/javascript">
 1432: // <![CDATA[
 1433: // <!-- BEGIN LON-CAPA Internal
 1434: var banner_link = '';
 1435: function helpMenu(target) {
 1436:     var caller = this;
 1437:     if (target == 'open') {
 1438:         var newWindow = null;
 1439:         try {
 1440:             newWindow =  window.open($nothing,"helpmenu","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" )
 1441:         }
 1442:         catch(error) {
 1443:             writeHelp(caller);
 1444:             return;
 1445:         }
 1446:         if (newWindow) {
 1447:             caller = newWindow;
 1448:         }
 1449:     }
 1450:     writeHelp(caller);
 1451:     return;
 1452: }
 1453: function writeHelp(caller) {
 1454:     caller.document.writeln('$start_page\\n<frame name="bannerframe" src="'+banner_link+'" marginwidth="0" marginheight="0" frameborder="0">\\n');
 1455:     caller.document.writeln('<frame name="bodyframe" src="$details_link" marginwidth="0" marginheight="0" frameborder="0">\\n$end_page');
 1456:     caller.document.close();
 1457:     caller.focus();
 1458: }
 1459: // END LON-CAPA Internal -->
 1460: // ]]>
 1461: </script>
 1462: ENDTEMPLATE
 1463:     return $template;
 1464: }
 1465: 
 1466: sub help_open_bug {
 1467:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
 1468:     unless ($env{'user.adv'}) { return ''; }
 1469:     unless ($Apache::lonnet::perlvar{'BugzillaHost'}) { return ''; }
 1470:     $text = "" if (not defined $text);
 1471: 	$stayOnPage=1;
 1472:     $width = 600 if (not defined $width);
 1473:     $height = 600 if (not defined $height);
 1474: 
 1475:     $topic=~s/\W+/\+/g;
 1476:     my $link='';
 1477:     my $template='';
 1478:     my $url=$Apache::lonnet::perlvar{'BugzillaHost'}.'enter_bug.cgi?product=LON-CAPA&amp;bug_file_loc='.
 1479: 	&escape($ENV{'REQUEST_URI'}).'&amp;component='.$topic;
 1480:     if (!$stayOnPage)
 1481:     {
 1482: 	$link = "javascript:void(open('$url', 'Bugzilla', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
 1483:     }
 1484:     else
 1485:     {
 1486: 	$link = $url;
 1487:     }
 1488:     # Add the text
 1489:     if ($text ne "")
 1490:     {
 1491: 	$template .= 
 1492:   "<table bgcolor='#AA3333' cellspacing='1' cellpadding='1' border='0'><tr>".
 1493:   "<td bgcolor='#FF5555'><a target=\"_top\" href=\"$link\"><span style=\"color:#FFFFFF;font-size:10pt;\">$text</span></a>";
 1494:     }
 1495: 
 1496:     # Add the graphic
 1497:     my $title = &mt('Report a Bug');
 1498:     my $bugicon=&lonhttpdurl("/adm/lonMisc/smallBug.gif");
 1499:     $template .= <<"ENDTEMPLATE";
 1500:  <a target="_top" href="$link" title="$title"><img src="$bugicon" border="0" alt="(Bug: $topic)" /></a>
 1501: ENDTEMPLATE
 1502:     if ($text ne '') { $template.='</td></tr></table>' };
 1503:     return $template;
 1504: 
 1505: }
 1506: 
 1507: sub help_open_faq {
 1508:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
 1509:     unless ($env{'user.adv'}) { return ''; }
 1510:     unless ($Apache::lonnet::perlvar{'FAQHost'}) { return ''; }
 1511:     $text = "" if (not defined $text);
 1512: 	$stayOnPage=1;
 1513:     $width = 350 if (not defined $width);
 1514:     $height = 400 if (not defined $height);
 1515: 
 1516:     $topic=~s/\W+/\+/g;
 1517:     my $link='';
 1518:     my $template='';
 1519:     my $url=$Apache::lonnet::perlvar{'FAQHost'}.'/fom/cache/'.$topic.'.html';
 1520:     if (!$stayOnPage)
 1521:     {
 1522: 	$link = "javascript:void(open('$url', 'FAQ-O-Matic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
 1523:     }
 1524:     else
 1525:     {
 1526: 	$link = $url;
 1527:     }
 1528: 
 1529:     # Add the text
 1530:     if ($text ne "")
 1531:     {
 1532: 	$template .= 
 1533:   "<table bgcolor='#337733' cellspacing='1' cellpadding='1' border='0'><tr>".
 1534:   "<td bgcolor='#448844'><a target=\"_top\" href=\"$link\"><span style=\"color:#FFFFFF; font-size:10pt;\">$text</span></a>";
 1535:     }
 1536: 
 1537:     # Add the graphic
 1538:     my $title = &mt('View the FAQ');
 1539:     my $faqicon=&lonhttpdurl("/adm/lonMisc/smallFAQ.gif");
 1540:     $template .= <<"ENDTEMPLATE";
 1541:  <a target="_top" href="$link" title="$title"><img src="$faqicon" border="0" alt="(FAQ: $topic)" /></a>
 1542: ENDTEMPLATE
 1543:     if ($text ne '') { $template.='</td></tr></table>' };
 1544:     return $template;
 1545: 
 1546: }
 1547: 
 1548: ###############################################################
 1549: ###############################################################
 1550: 
 1551: =pod
 1552: 
 1553: =item * &change_content_javascript():
 1554: 
 1555: This and the next function allow you to create small sections of an
 1556: otherwise static HTML page that you can update on the fly with
 1557: Javascript, even in Netscape 4.
 1558: 
 1559: The Javascript fragment returned by this function (no E<lt>scriptE<gt> tag)
 1560: must be written to the HTML page once. It will prove the Javascript
 1561: function "change(name, content)". Calling the change function with the
 1562: name of the section 
 1563: you want to update, matching the name passed to C<changable_area>, and
 1564: the new content you want to put in there, will put the content into
 1565: that area.
 1566: 
 1567: B<Note>: Netscape 4 only reserves enough space for the changable area
 1568: to contain room for the original contents. You need to "make space"
 1569: for whatever changes you wish to make, and be B<sure> to check your
 1570: code in Netscape 4. This feature in Netscape 4 is B<not> powerful;
 1571: it's adequate for updating a one-line status display, but little more.
 1572: This script will set the space to 100% width, so you only need to
 1573: worry about height in Netscape 4.
 1574: 
 1575: Modern browsers are much less limiting, and if you can commit to the
 1576: user not using Netscape 4, this feature may be used freely with
 1577: pretty much any HTML.
 1578: 
 1579: =cut
 1580: 
 1581: sub change_content_javascript {
 1582:     # If we're on Netscape 4, we need to use Layer-based code
 1583:     if ($env{'browser.type'} eq 'netscape' &&
 1584: 	$env{'browser.version'} =~ /^4\./) {
 1585: 	return (<<NETSCAPE4);
 1586: 	function change(name, content) {
 1587: 	    doc = document.layers[name+"___escape"].layers[0].document;
 1588: 	    doc.open();
 1589: 	    doc.write(content);
 1590: 	    doc.close();
 1591: 	}
 1592: NETSCAPE4
 1593:     } else {
 1594: 	# Otherwise, we need to use semi-standards-compliant code
 1595: 	# (technically, "innerHTML" isn't standard but the equivalent
 1596: 	# is really scary, and every useful browser supports it
 1597: 	return (<<DOMBASED);
 1598: 	function change(name, content) {
 1599: 	    element = document.getElementById(name);
 1600: 	    element.innerHTML = content;
 1601: 	}
 1602: DOMBASED
 1603:     }
 1604: }
 1605: 
 1606: =pod
 1607: 
 1608: =item * &changable_area($name,$origContent):
 1609: 
 1610: This provides a "changable area" that can be modified on the fly via
 1611: the Javascript code provided in C<change_content_javascript>. $name is
 1612: the name you will use to reference the area later; do not repeat the
 1613: same name on a given HTML page more then once. $origContent is what
 1614: the area will originally contain, which can be left blank.
 1615: 
 1616: =cut
 1617: 
 1618: sub changable_area {
 1619:     my ($name, $origContent) = @_;
 1620: 
 1621:     if ($env{'browser.type'} eq 'netscape' &&
 1622: 	$env{'browser.version'} =~ /^4\./) {
 1623: 	# If this is netscape 4, we need to use the Layer tag
 1624: 	return "<ilayer width='100%' id='${name}___escape' overflow='none'><layer width='100%' id='$name' overflow='none'>$origContent</layer></ilayer>";
 1625:     } else {
 1626: 	return "<span id='$name'>$origContent</span>";
 1627:     }
 1628: }
 1629: 
 1630: =pod
 1631: 
 1632: =item * &viewport_geometry_js 
 1633: 
 1634: Provides javascript object (Geometry) which can provide information about the viewport geometry for the client browser.
 1635: 
 1636: =cut
 1637: 
 1638: 
 1639: sub viewport_geometry_js { 
 1640:     return <<"GEOMETRY";
 1641: var Geometry = {};
 1642: function init_geometry() {
 1643:     if (Geometry.init) { return };
 1644:     Geometry.init=1;
 1645:     if (window.innerHeight) {
 1646:         Geometry.getViewportHeight   = function() { return window.innerHeight; };
 1647:         Geometry.getViewportWidth   = function() { return window.innerWidth; };
 1648:         Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
 1649:         Geometry.getVerticalScroll   = function() { return window.pageYOffset; };
 1650:     }
 1651:     else if (document.documentElement && document.documentElement.clientHeight) {
 1652:         Geometry.getViewportHeight =
 1653:             function() { return document.documentElement.clientHeight; };
 1654:         Geometry.getViewportWidth =
 1655:             function() { return document.documentElement.clientWidth; };
 1656: 
 1657:         Geometry.getHorizontalScroll =
 1658:             function() { return document.documentElement.scrollLeft; };
 1659:         Geometry.getVerticalScroll =
 1660:             function() { return document.documentElement.scrollTop; };
 1661:     }
 1662:     else if (document.body.clientHeight) {
 1663:         Geometry.getViewportHeight =
 1664:             function() { return document.body.clientHeight; };
 1665:         Geometry.getViewportWidth =
 1666:             function() { return document.body.clientWidth; };
 1667:         Geometry.getHorizontalScroll =
 1668:             function() { return document.body.scrollLeft; };
 1669:         Geometry.getVerticalScroll =
 1670:             function() { return document.body.scrollTop; };
 1671:     }
 1672: }
 1673: 
 1674: GEOMETRY
 1675: }
 1676: 
 1677: =pod
 1678: 
 1679: =item * &viewport_size_js()
 1680: 
 1681: Provides a javascript function to set values of two form elements - width and height (elements are passed in as arguments to the javascript function) to the dimensions of the user's browser window. 
 1682: 
 1683: =cut
 1684: 
 1685: sub viewport_size_js {
 1686:     my $geometry = &viewport_geometry_js();
 1687:     return <<"DIMS";
 1688: 
 1689: $geometry
 1690: 
 1691: function getViewportDims(width,height) {
 1692:     init_geometry();
 1693:     width.value = Geometry.getViewportWidth();
 1694:     height.value = Geometry.getViewportHeight();
 1695:     return;
 1696: }
 1697: 
 1698: DIMS
 1699: }
 1700: 
 1701: =pod
 1702: 
 1703: =item * &resize_textarea_js()
 1704: 
 1705: emits the needed javascript to resize a textarea to be as big as possible
 1706: 
 1707: creates a function resize_textrea that takes two IDs first should be
 1708: the id of the element to resize, second should be the id of a div that
 1709: surrounds everything that comes after the textarea, this routine needs
 1710: to be attached to the <body> for the onload and onresize events.
 1711: 
 1712: =back
 1713: 
 1714: =cut
 1715: 
 1716: sub resize_textarea_js {
 1717:     my $geometry = &viewport_geometry_js();
 1718:     return <<"RESIZE";
 1719:     <script type="text/javascript">
 1720: // <![CDATA[
 1721: $geometry
 1722: 
 1723: function getX(element) {
 1724:     var x = 0;
 1725:     while (element) {
 1726: 	x += element.offsetLeft;
 1727: 	element = element.offsetParent;
 1728:     }
 1729:     return x;
 1730: }
 1731: function getY(element) {
 1732:     var y = 0;
 1733:     while (element) {
 1734: 	y += element.offsetTop;
 1735: 	element = element.offsetParent;
 1736:     }
 1737:     return y;
 1738: }
 1739: 
 1740: 
 1741: function resize_textarea(textarea_id,bottom_id) {
 1742:     init_geometry();
 1743:     var textarea        = document.getElementById(textarea_id);
 1744:     //alert(textarea);
 1745: 
 1746:     var textarea_top    = getY(textarea);
 1747:     var textarea_height = textarea.offsetHeight;
 1748:     var bottom          = document.getElementById(bottom_id);
 1749:     var bottom_top      = getY(bottom);
 1750:     var bottom_height   = bottom.offsetHeight;
 1751:     var window_height   = Geometry.getViewportHeight();
 1752:     var fudge           = 23;
 1753:     var new_height      = window_height-fudge-textarea_top-bottom_height;
 1754:     if (new_height < 300) {
 1755: 	new_height = 300;
 1756:     }
 1757:     textarea.style.height=new_height+'px';
 1758: }
 1759: // ]]>
 1760: </script>
 1761: RESIZE
 1762: 
 1763: }
 1764: 
 1765: sub colorfuleditor_js {
 1766:     return <<"COLORFULEDIT"
 1767: <script type="text/javascript">
 1768: // <![CDATA[>
 1769:     function fold_box(curDepth, lastresource){
 1770: 
 1771:     // we need a list because there can be several blocks you need to fold in one tag
 1772:         var block = document.getElementsByName('foldblock_'+curDepth);
 1773:     // but there is only one folding button per tag
 1774:         var foldbutton = document.getElementById('folding_btn_'+curDepth);
 1775: 
 1776:         if(block.item(0).style.display == 'none'){
 1777: 
 1778:             foldbutton.value = '@{[&mt("Hide")]}';
 1779:             for (i = 0; i < block.length; i++){
 1780:                 block.item(i).style.display = '';
 1781:             }
 1782:         }else{
 1783: 
 1784:             foldbutton.value = '@{[&mt("Show")]}';
 1785:             for (i = 0; i < block.length; i++){
 1786:                 // block.item(i).style.visibility = 'collapse';
 1787:                 block.item(i).style.display = 'none';
 1788:             }
 1789:         };
 1790:         saveState(lastresource);
 1791:     }
 1792: 
 1793:     function saveState (lastresource) {
 1794: 
 1795:         var tag_list = getTagList();
 1796:         if(tag_list != null){
 1797:             var timestamp = new Date().getTime();
 1798:             var key = lastresource;
 1799: 
 1800:             // the value pattern is: 'time;key1,value1;key2,value2; ... '
 1801:             // starting with timestamp
 1802:             var value = timestamp+';';
 1803: 
 1804:             // building the list of key-value pairs
 1805:             for(var i = 0; i < tag_list.length; i++){
 1806:                 value += tag_list[i]+',';
 1807:                 value += document.getElementsByName(tag_list[i])[0].style.display+';';
 1808:             }
 1809: 
 1810:             // only iterate whole storage if nothing to override
 1811:             if(localStorage.getItem(key) == null){        
 1812: 
 1813:                 // prevent storage from growing large
 1814:                 if(localStorage.length > 50){
 1815:                     var regex_getTimestamp = /^(?:\d)+;/;
 1816:                     var oldest_timestamp = regex_getTimestamp.exec(localStorage.key(0));
 1817:                     var oldest_key;
 1818:                     
 1819:                     for(var i = 1; i < localStorage.length; i++){
 1820:                         if (regex_getTimestamp.exec(localStorage.key(i)) < oldest_timestamp) {
 1821:                             oldest_key = localStorage.key(i);
 1822:                             oldest_timestamp = regex_getTimestamp.exec(oldest_key);
 1823:                         }
 1824:                     }
 1825:                     localStorage.removeItem(oldest_key);
 1826:                 }
 1827:             }
 1828:             localStorage.setItem(key,value);
 1829:         }
 1830:     }
 1831: 
 1832:     // restore folding status of blocks (on page load)
 1833:     function restoreState (lastresource) {
 1834:         if(localStorage.getItem(lastresource) != null){
 1835:             var key = lastresource;
 1836:             var value = localStorage.getItem(key);
 1837:             var regex_delTimestamp = /^\d+;/;
 1838: 
 1839:             value.replace(regex_delTimestamp, '');
 1840: 
 1841:             var valueArr = value.split(';');
 1842:             var pairs;
 1843:             var elements;
 1844:             for (var i = 0; i < valueArr.length; i++){
 1845:                 pairs = valueArr[i].split(',');
 1846:                 elements = document.getElementsByName(pairs[0]);
 1847: 
 1848:                 for (var j = 0; j < elements.length; j++){  
 1849:                     elements[j].style.display = pairs[1];
 1850:                     if (pairs[1] == "none"){
 1851:                         var regex_id = /([_\\d]+)\$/;
 1852:                         regex_id.exec(pairs[0]);
 1853:                         document.getElementById("folding_btn"+RegExp.\$1).value = "Show";
 1854:                     }
 1855:                 }
 1856:             }
 1857:         }
 1858:     }
 1859: 
 1860:     function getTagList () {
 1861:         
 1862:         var stringToSearch = document.lonhomework.innerHTML;
 1863: 
 1864:         var ret = new Array();
 1865:         var regex_findBlock = /(foldblock_.*?)"/g;
 1866:         var tag_list = stringToSearch.match(regex_findBlock);
 1867: 
 1868:         if(tag_list != null){
 1869:             for(var i = 0; i < tag_list.length; i++){            
 1870:                 ret.push(tag_list[i].replace(/"/, ''));
 1871:             }
 1872:         }
 1873:         return ret;
 1874:     }
 1875: 
 1876:     function saveScrollPosition (resource) {
 1877:         var tag_list = getTagList();
 1878: 
 1879:         // we dont always want to jump to the first block
 1880:         // 170 is roughly above the "Problem Editing" header. we just want to save if the user scrolled down further than this
 1881:         if(\$(window).scrollTop() > 170){
 1882:             if(tag_list != null){
 1883:                 var result;
 1884:                 for(var i = 0; i < tag_list.length; i++){
 1885:                     if(isElementInViewport(tag_list[i])){
 1886:                         result += tag_list[i]+';';
 1887:                     }
 1888:                 }
 1889:                 sessionStorage.setItem('anchor_'+resource, result);
 1890:             }
 1891:         } else {
 1892:             // we dont need to save zero, just delete the item to leave everything tidy
 1893:             sessionStorage.removeItem('anchor_'+resource);
 1894:         }
 1895:     }
 1896: 
 1897:     function restoreScrollPosition(resource){
 1898: 
 1899:         var elem = sessionStorage.getItem('anchor_'+resource);
 1900:         if(elem != null){
 1901:             var tag_list = elem.split(';');
 1902:             var elem_list;
 1903: 
 1904:             for(var i = 0; i < tag_list.length; i++){
 1905:                 elem_list = document.getElementsByName(tag_list[i]);
 1906:                 
 1907:                 if(elem_list.length > 0){
 1908:                     elem = elem_list[0];
 1909:                     break;
 1910:                 }
 1911:             }
 1912:             elem.scrollIntoView();
 1913:         }
 1914:     }
 1915: 
 1916:     function isElementInViewport(el) {
 1917: 
 1918:         // change to last element instead of first
 1919:         var elem = document.getElementsByName(el);
 1920:         var rect = elem[0].getBoundingClientRect();
 1921: 
 1922:         return (
 1923:             rect.top >= 0 &&
 1924:             rect.left >= 0 &&
 1925:             rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
 1926:             rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
 1927:         );
 1928:     }
 1929:     
 1930:     function autosize(depth){
 1931:         var cmInst = window['cm'+depth];
 1932:         var fitsizeButton = document.getElementById('fitsize'+depth);
 1933: 
 1934:         // is fixed size, switching to dynamic
 1935:         if (sessionStorage.getItem("autosized_"+depth) == null) {
 1936:             cmInst.setSize("","auto");
 1937:             fitsizeButton.value = "@{[&mt('Fixed size')]}";
 1938:             sessionStorage.setItem("autosized_"+depth, "yes");
 1939: 
 1940:         // is dynamic size, switching to fixed
 1941:         } else {
 1942:             cmInst.setSize("","300px");
 1943:             fitsizeButton.value = "@{[&mt('Dynamic size')]}";
 1944:             sessionStorage.removeItem("autosized_"+depth);
 1945:         }
 1946:     }
 1947: 
 1948: 
 1949: 
 1950: // ]]>
 1951: </script>
 1952: COLORFULEDIT
 1953: }
 1954: 
 1955: sub xmleditor_js {
 1956:     return <<XMLEDIT
 1957: <script type="text/javascript" src="/adm/jQuery/addons/jquery-scrolltofixed.js"></script>
 1958: <script type="text/javascript">
 1959: // <![CDATA[>
 1960: 
 1961:     function saveScrollPosition (resource) {
 1962: 
 1963:         var scrollPos = \$(window).scrollTop();
 1964:         sessionStorage.setItem(resource,scrollPos);
 1965:     }
 1966: 
 1967:     function restoreScrollPosition(resource){
 1968: 
 1969:         var scrollPos = sessionStorage.getItem(resource);
 1970:         \$(window).scrollTop(scrollPos);
 1971:     }
 1972: 
 1973:     // unless internet explorer
 1974:     if (!(window.navigator.appName == "Microsoft Internet Explorer" && (document.documentMode || document.compatMode))){
 1975: 
 1976:         \$(document).ready(function() {
 1977:              \$(".LC_edit_actionbar").scrollToFixed(\{zIndex: 100\});
 1978:         });
 1979:     }
 1980: 
 1981:     // inserts text at cursor position into codemirror (xml editor only)
 1982:     function insertText(text){
 1983:         cm.focus();
 1984:         var curPos = cm.getCursor();
 1985:         cm.replaceRange(text.replace(/ESCAPEDSCRIPT/g,'script'), {line: curPos.line,ch: curPos.ch});
 1986:     }
 1987: // ]]>
 1988: </script>
 1989: XMLEDIT
 1990: }
 1991: 
 1992: sub insert_folding_button {
 1993:     my $curDepth = $Apache::lonxml::curdepth;
 1994:     my $lastresource = $env{'request.ambiguous'};
 1995: 
 1996:     return "<input type=\"button\" id=\"folding_btn_$curDepth\" 
 1997:             value=\"".&mt('Hide')."\" onclick=\"fold_box('$curDepth','$lastresource')\">";
 1998: }
 1999: 
 2000: =pod
 2001: 
 2002: =head1 Excel and CSV file utility routines
 2003: 
 2004: =cut
 2005: 
 2006: ###############################################################
 2007: ###############################################################
 2008: 
 2009: =pod
 2010: 
 2011: =over 4
 2012: 
 2013: =item * &csv_translate($text) 
 2014: 
 2015: Translate $text to allow it to be output as a 'comma separated values' 
 2016: format.
 2017: 
 2018: =cut
 2019: 
 2020: ###############################################################
 2021: ###############################################################
 2022: sub csv_translate {
 2023:     my $text = shift;
 2024:     $text =~ s/\"/\"\"/g;
 2025:     $text =~ s/\n/ /g;
 2026:     return $text;
 2027: }
 2028: 
 2029: ###############################################################
 2030: ###############################################################
 2031: 
 2032: =pod
 2033: 
 2034: =item * &define_excel_formats()
 2035: 
 2036: Define some commonly used Excel cell formats.
 2037: 
 2038: Currently supported formats:
 2039: 
 2040: =over 4
 2041: 
 2042: =item header
 2043: 
 2044: =item bold
 2045: 
 2046: =item h1
 2047: 
 2048: =item h2
 2049: 
 2050: =item h3
 2051: 
 2052: =item h4
 2053: 
 2054: =item i
 2055: 
 2056: =item date
 2057: 
 2058: =back
 2059: 
 2060: Inputs: $workbook
 2061: 
 2062: Returns: $format, a hash reference.
 2063: 
 2064: 
 2065: =cut
 2066: 
 2067: ###############################################################
 2068: ###############################################################
 2069: sub define_excel_formats {
 2070:     my ($workbook) = @_;
 2071:     my $format;
 2072:     $format->{'header'} = $workbook->add_format(bold      => 1, 
 2073:                                                 bottom    => 1,
 2074:                                                 align     => 'center');
 2075:     $format->{'bold'} = $workbook->add_format(bold=>1);
 2076:     $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
 2077:     $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
 2078:     $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
 2079:     $format->{'h4'}   = $workbook->add_format(bold=>1, size=>12);
 2080:     $format->{'i'}    = $workbook->add_format(italic=>1);
 2081:     $format->{'date'} = $workbook->add_format(num_format=>
 2082:                                             'mm/dd/yyyy hh:mm:ss');
 2083:     return $format;
 2084: }
 2085: 
 2086: ###############################################################
 2087: ###############################################################
 2088: 
 2089: =pod
 2090: 
 2091: =item * &create_workbook()
 2092: 
 2093: Create an Excel worksheet.  If it fails, output message on the
 2094: request object and return undefs.
 2095: 
 2096: Inputs: Apache request object
 2097: 
 2098: Returns (undef) on failure, 
 2099:     Excel worksheet object, scalar with filename, and formats 
 2100:     from &Apache::loncommon::define_excel_formats on success
 2101: 
 2102: =cut
 2103: 
 2104: ###############################################################
 2105: ###############################################################
 2106: sub create_workbook {
 2107:     my ($r) = @_;
 2108:         #
 2109:     # Create the excel spreadsheet
 2110:     my $filename = '/prtspool/'.
 2111:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
 2112:         time.'_'.rand(1000000000).'.xls';
 2113:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
 2114:     if (! defined($workbook)) {
 2115:         $r->log_error("Error creating excel spreadsheet $filename: $!");
 2116:         $r->print(
 2117:             '<p class="LC_error">'
 2118:            .&mt('Problems occurred in creating the new Excel file.')
 2119:            .' '.&mt('This error has been logged.')
 2120:            .' '.&mt('Please alert your LON-CAPA administrator.')
 2121:            .'</p>'
 2122:         );
 2123:         return (undef);
 2124:     }
 2125:     #
 2126:     $workbook->set_tempdir(LONCAPA::tempdir());
 2127:     #
 2128:     my $format = &Apache::loncommon::define_excel_formats($workbook);
 2129:     return ($workbook,$filename,$format);
 2130: }
 2131: 
 2132: ###############################################################
 2133: ###############################################################
 2134: 
 2135: =pod
 2136: 
 2137: =item * &create_text_file()
 2138: 
 2139: Create a file to write to and eventually make available to the user.
 2140: If file creation fails, outputs an error message on the request object and 
 2141: return undefs.
 2142: 
 2143: Inputs: Apache request object, and file suffix
 2144: 
 2145: Returns (undef) on failure, 
 2146:     Filehandle and filename on success.
 2147: 
 2148: =cut
 2149: 
 2150: ###############################################################
 2151: ###############################################################
 2152: sub create_text_file {
 2153:     my ($r,$suffix) = @_;
 2154:     if (! defined($suffix)) { $suffix = 'txt'; };
 2155:     my $fh;
 2156:     my $filename = '/prtspool/'.
 2157:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
 2158:         time.'_'.rand(1000000000).'.'.$suffix;
 2159:     $fh = Apache::File->new('>/home/httpd'.$filename);
 2160:     if (! defined($fh)) {
 2161:         $r->log_error("Couldn't open $filename for output $!");
 2162:         $r->print(
 2163:             '<p class="LC_error">'
 2164:            .&mt('Problems occurred in creating the output file.')
 2165:            .' '.&mt('This error has been logged.')
 2166:            .' '.&mt('Please alert your LON-CAPA administrator.')
 2167:            .'</p>'
 2168:         );
 2169:     }
 2170:     return ($fh,$filename)
 2171: }
 2172: 
 2173: 
 2174: =pod 
 2175: 
 2176: =back
 2177: 
 2178: =cut
 2179: 
 2180: ###############################################################
 2181: ##        Home server <option> list generating code          ##
 2182: ###############################################################
 2183: 
 2184: # ------------------------------------------
 2185: 
 2186: sub domain_select {
 2187:     my ($name,$value,$multiple)=@_;
 2188:     my %domains=map { 
 2189: 	$_ => $_.' '. &Apache::lonnet::domain($_,'description') 
 2190:     } &Apache::lonnet::all_domains();
 2191:     if ($multiple) {
 2192: 	$domains{''}=&mt('Any domain');
 2193: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
 2194: 	return &multiple_select_form($name,$value,4,\%domains);
 2195:     } else {
 2196: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
 2197: 	return &select_form($name,$value,\%domains);
 2198:     }
 2199: }
 2200: 
 2201: #-------------------------------------------
 2202: 
 2203: =pod
 2204: 
 2205: =head1 Routines for form select boxes
 2206: 
 2207: =over 4
 2208: 
 2209: =item * &multiple_select_form($name,$value,$size,$hash,$order)
 2210: 
 2211: Returns a string containing a <select> element int multiple mode
 2212: 
 2213: 
 2214: Args:
 2215:   $name - name of the <select> element
 2216:   $value - scalar or array ref of values that should already be selected
 2217:   $size - number of rows long the select element is
 2218:   $hash - the elements should be 'option' => 'shown text'
 2219:           (shown text should already have been &mt())
 2220:   $order - (optional) array ref of the order to show the elements in
 2221: 
 2222: =cut
 2223: 
 2224: #-------------------------------------------
 2225: sub multiple_select_form {
 2226:     my ($name,$value,$size,$hash,$order)=@_;
 2227:     my %selected = map { $_ => 1 } ref($value)?@{$value}:($value);
 2228:     my $output='';
 2229:     if (! defined($size)) {
 2230:         $size = 4;
 2231:         if (scalar(keys(%$hash))<4) {
 2232:             $size = scalar(keys(%$hash));
 2233:         }
 2234:     }
 2235:     $output.="\n".'<select name="'.$name.'" size="'.$size.'" multiple="multiple">';
 2236:     my @order;
 2237:     if (ref($order) eq 'ARRAY')  {
 2238:         @order = @{$order};
 2239:     } else {
 2240:         @order = sort(keys(%$hash));
 2241:     }
 2242:     if (exists($$hash{'select_form_order'})) {
 2243:         @order = @{$$hash{'select_form_order'}};
 2244:     }
 2245:         
 2246:     foreach my $key (@order) {
 2247:         $output.='<option value="'.&HTML::Entities::encode($key,'"<>&').'" ';
 2248:         $output.='selected="selected" ' if ($selected{$key});
 2249:         $output.='>'.$hash->{$key}."</option>\n";
 2250:     }
 2251:     $output.="</select>\n";
 2252:     return $output;
 2253: }
 2254: 
 2255: #-------------------------------------------
 2256: 
 2257: =pod
 2258: 
 2259: =item * &select_form($defdom,$name,$hashref,$onchange)
 2260: 
 2261: Returns a string containing a <select name='$name' size='1'> form to 
 2262: allow a user to select options from a ref to a hash containing:
 2263: option_name => displayed text. An optional $onchange can include
 2264: a javascript onchange item, e.g., onchange="this.form.submit();"  
 2265: 
 2266: See lonrights.pm for an example invocation and use.
 2267: 
 2268: =cut
 2269: 
 2270: #-------------------------------------------
 2271: sub select_form {
 2272:     my ($def,$name,$hashref,$onchange) = @_;
 2273:     return unless (ref($hashref) eq 'HASH');
 2274:     if ($onchange) {
 2275:         $onchange = ' onchange="'.$onchange.'"';
 2276:     }
 2277:     my $selectform = "<select name=\"$name\" size=\"1\"$onchange>\n";
 2278:     my @keys;
 2279:     if (exists($hashref->{'select_form_order'})) {
 2280: 	@keys=@{$hashref->{'select_form_order'}};
 2281:     } else {
 2282: 	@keys=sort(keys(%{$hashref}));
 2283:     }
 2284:     foreach my $key (@keys) {
 2285:         $selectform.=
 2286: 	    '<option value="'.&HTML::Entities::encode($key,'"<>&').'" '.
 2287:             ($key eq $def ? 'selected="selected" ' : '').
 2288:                 ">".$hashref->{$key}."</option>\n";
 2289:     }
 2290:     $selectform.="</select>";
 2291:     return $selectform;
 2292: }
 2293: 
 2294: # For display filters
 2295: 
 2296: sub display_filter {
 2297:     my ($context) = @_;
 2298:     if (!$env{'form.show'}) { $env{'form.show'}=10; }
 2299:     if (!$env{'form.displayfilter'}) { $env{'form.displayfilter'}='currentfolder'; }
 2300:     my $phraseinput = 'hidden';
 2301:     my $includeinput = 'hidden';
 2302:     my ($checked,$includetypestext);
 2303:     if ($env{'form.displayfilter'} eq 'containing') {
 2304:         $phraseinput = 'text'; 
 2305:         if ($context eq 'parmslog') {
 2306:             $includeinput = 'checkbox';
 2307:             if ($env{'form.includetypes'}) {
 2308:                 $checked = ' checked="checked"';
 2309:             }
 2310:             $includetypestext = &mt('Include parameter types');
 2311:         }
 2312:     } else {
 2313:         $includetypestext = '&nbsp;';
 2314:     }
 2315:     my ($additional,$secondid,$thirdid);
 2316:     if ($context eq 'parmslog') {
 2317:         $additional = 
 2318:             '<label><input type="'.$includeinput.'" name="includetypes"'. 
 2319:             $checked.' name="includetypes" value="1" id="includetypes" />'.
 2320:             '&nbsp;<span id="includetypestext">'.$includetypestext.'</span>'.
 2321:             '</label>';
 2322:         $secondid = 'includetypes';
 2323:         $thirdid = 'includetypestext';
 2324:     }
 2325:     my $onchange = "javascript:toggleHistoryOptions(this,'containingphrase','$context',
 2326:                                                     '$secondid','$thirdid')";
 2327:     return '<span class="LC_nobreak"><label>'.&mt('Records: [_1]',
 2328: 			       &Apache::lonmeta::selectbox('show',$env{'form.show'},undef,
 2329: 							   (&mt('all'),10,20,50,100,1000,10000))).
 2330: 	   '</label></span> <span class="LC_nobreak">'.
 2331:            &mt('Filter: [_1]',
 2332: 	   &select_form($env{'form.displayfilter'},
 2333: 			'displayfilter',
 2334: 			{'currentfolder' => 'Current folder/page',
 2335: 			 'containing' => 'Containing phrase',
 2336: 			 'none' => 'None'},$onchange)).'&nbsp;'.
 2337: 			 '<input type="'.$phraseinput.'" name="containingphrase" id="containingphrase" size="30" value="'.
 2338:                          &HTML::Entities::encode($env{'form.containingphrase'}).
 2339:                          '" />'.$additional;
 2340: }
 2341: 
 2342: sub display_filter_js {
 2343:     my $includetext = &mt('Include parameter types');
 2344:     return <<"ENDJS";
 2345:   
 2346: function toggleHistoryOptions(setter,firstid,context,secondid,thirdid) {
 2347:     var firstType = 'hidden';
 2348:     if (setter.options[setter.selectedIndex].value == 'containing') {
 2349:         firstType = 'text';
 2350:     }
 2351:     firstObject = document.getElementById(firstid);
 2352:     if (typeof(firstObject) == 'object') {
 2353:         if (firstObject.type != firstType) {
 2354:             changeInputType(firstObject,firstType);
 2355:         }
 2356:     }
 2357:     if (context == 'parmslog') {
 2358:         var secondType = 'hidden';
 2359:         if (firstType == 'text') {
 2360:             secondType = 'checkbox';
 2361:         }
 2362:         secondObject = document.getElementById(secondid);  
 2363:         if (typeof(secondObject) == 'object') {
 2364:             if (secondObject.type != secondType) {
 2365:                 changeInputType(secondObject,secondType);
 2366:             }
 2367:         }
 2368:         var textItem = document.getElementById(thirdid);
 2369:         var currtext = textItem.innerHTML;
 2370:         var newtext;
 2371:         if (firstType == 'text') {
 2372:             newtext = '$includetext';
 2373:         } else {
 2374:             newtext = '&nbsp;';
 2375:         }
 2376:         if (currtext != newtext) {
 2377:             textItem.innerHTML = newtext;
 2378:         }
 2379:     }
 2380:     return;
 2381: }
 2382: 
 2383: function changeInputType(oldObject,newType) {
 2384:     var newObject = document.createElement('input');
 2385:     newObject.type = newType;
 2386:     if (oldObject.size) {
 2387:         newObject.size = oldObject.size;
 2388:     }
 2389:     if (oldObject.value) {
 2390:         newObject.value = oldObject.value;
 2391:     }
 2392:     if (oldObject.name) {
 2393:         newObject.name = oldObject.name;
 2394:     }
 2395:     if (oldObject.id) {
 2396:         newObject.id = oldObject.id;
 2397:     }
 2398:     oldObject.parentNode.replaceChild(newObject,oldObject);
 2399:     return;
 2400: }
 2401: 
 2402: ENDJS
 2403: }
 2404: 
 2405: sub gradeleveldescription {
 2406:     my $gradelevel=shift;
 2407:     my %gradelevels=(0 => 'Not specified',
 2408: 		     1 => 'Grade 1',
 2409: 		     2 => 'Grade 2',
 2410: 		     3 => 'Grade 3',
 2411: 		     4 => 'Grade 4',
 2412: 		     5 => 'Grade 5',
 2413: 		     6 => 'Grade 6',
 2414: 		     7 => 'Grade 7',
 2415: 		     8 => 'Grade 8',
 2416: 		     9 => 'Grade 9',
 2417: 		     10 => 'Grade 10',
 2418: 		     11 => 'Grade 11',
 2419: 		     12 => 'Grade 12',
 2420: 		     13 => 'Grade 13',
 2421: 		     14 => '100 Level',
 2422: 		     15 => '200 Level',
 2423: 		     16 => '300 Level',
 2424: 		     17 => '400 Level',
 2425: 		     18 => 'Graduate Level');
 2426:     return &mt($gradelevels{$gradelevel});
 2427: }
 2428: 
 2429: sub select_level_form {
 2430:     my ($deflevel,$name)=@_;
 2431:     unless ($deflevel) { $deflevel=0; }
 2432:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
 2433:     for (my $i=0; $i<=18; $i++) {
 2434:         $selectform.="<option value=\"$i\" ".
 2435:             ($i==$deflevel ? 'selected="selected" ' : '').
 2436:                 ">".&gradeleveldescription($i)."</option>\n";
 2437:     }
 2438:     $selectform.="</select>";
 2439:     return $selectform;
 2440: }
 2441: 
 2442: #-------------------------------------------
 2443: 
 2444: =pod
 2445: 
 2446: =item * &select_dom_form($defdom,$name,$includeempty,$showdomdesc,$onchange,$incdoms,$excdoms)
 2447: 
 2448: Returns a string containing a <select name='$name' size='1'> form to 
 2449: allow a user to select the domain to preform an operation in.  
 2450: See loncreateuser.pm for an example invocation and use.
 2451: 
 2452: If the $includeempty flag is set, it also includes an empty choice ("no domain
 2453: selected");
 2454: 
 2455: If the $showdomdesc flag is set, the domain name is followed by the domain description.
 2456: 
 2457: The optional $onchange argument specifies what should occur if the domain selector is changed, e.g., 'this.form.submit()' if the form is to be automatically submitted.
 2458: 
 2459: The optional $incdoms is a reference to an array of domains which will be the only available options.
 2460: 
 2461: The optional $excdoms is a reference to an array of domains which will be excluded from the available options.
 2462: 
 2463: =cut
 2464: 
 2465: #-------------------------------------------
 2466: sub select_dom_form {
 2467:     my ($defdom,$name,$includeempty,$showdomdesc,$onchange,$incdoms,$excdoms) = @_;
 2468:     if ($onchange) {
 2469:         $onchange = ' onchange="'.$onchange.'"';
 2470:     }
 2471:     my (@domains,%exclude);
 2472:     if (ref($incdoms) eq 'ARRAY') {
 2473:         @domains = sort {lc($a) cmp lc($b)} (@{$incdoms});
 2474:     } else {
 2475:         @domains = sort {lc($a) cmp lc($b)} (&Apache::lonnet::all_domains());
 2476:     }
 2477:     if ($includeempty) { @domains=('',@domains); }
 2478:     if (ref($excdoms) eq 'ARRAY') {
 2479:         map { $exclude{$_} = 1; } @{$excdoms}; 
 2480:     }
 2481:     my $selectdomain = "<select name=\"$name\" size=\"1\"$onchange>\n";
 2482:     foreach my $dom (@domains) {
 2483:         next if ($exclude{$dom});
 2484:         $selectdomain.="<option value=\"$dom\" ".
 2485:             ($dom eq $defdom ? 'selected="selected" ' : '').'>'.$dom;
 2486:         if ($showdomdesc) {
 2487:             if ($dom ne '') {
 2488:                 my $domdesc = &Apache::lonnet::domain($dom,'description');
 2489:                 if ($domdesc ne '') {
 2490:                     $selectdomain .= ' ('.$domdesc.')';
 2491:                 }
 2492:             } 
 2493:         }
 2494:         $selectdomain .= "</option>\n";
 2495:     }
 2496:     $selectdomain.="</select>";
 2497:     return $selectdomain;
 2498: }
 2499: 
 2500: #-------------------------------------------
 2501: 
 2502: =pod
 2503: 
 2504: =item * &home_server_form_item($domain,$name,$defaultflag)
 2505: 
 2506: input: 4 arguments (two required, two optional) - 
 2507:     $domain - domain of new user
 2508:     $name - name of form element
 2509:     $default - Value of 'default' causes a default item to be first 
 2510:                             option, and selected by default. 
 2511:     $hide - Value of 'hide' causes hiding of the name of the server, 
 2512:                             if 1 server found, or default, if 0 found.
 2513: output: returns 2 items: 
 2514: (a) form element which contains either:
 2515:    (i) <select name="$name">
 2516:         <option value="$hostid1">$hostid $servers{$hostid}</option>
 2517:         <option value="$hostid2">$hostid $servers{$hostid}</option>       
 2518:        </select>
 2519:        form item if there are multiple library servers in $domain, or
 2520:    (ii) an <input type="hidden" name="$name" value="$hostid" /> form item 
 2521:        if there is only one library server in $domain.
 2522: 
 2523: (b) number of library servers found.
 2524: 
 2525: See loncreateuser.pm for example of use.
 2526: 
 2527: =cut
 2528: 
 2529: #-------------------------------------------
 2530: sub home_server_form_item {
 2531:     my ($domain,$name,$default,$hide) = @_;
 2532:     my %servers = &Apache::lonnet::get_servers($domain,'library');
 2533:     my $result;
 2534:     my $numlib = keys(%servers);
 2535:     if ($numlib > 1) {
 2536:         $result .= '<select name="'.$name.'" />'."\n";
 2537:         if ($default) {
 2538:             $result .= '<option value="default" selected="selected">'.&mt('default').
 2539:                        '</option>'."\n";
 2540:         }
 2541:         foreach my $hostid (sort(keys(%servers))) {
 2542:             $result.= '<option value="'.$hostid.'">'.
 2543: 	              $hostid.' '.$servers{$hostid}."</option>\n";
 2544:         }
 2545:         $result .= '</select>'."\n";
 2546:     } elsif ($numlib == 1) {
 2547:         my $hostid;
 2548:         foreach my $item (keys(%servers)) {
 2549:             $hostid = $item;
 2550:         }
 2551:         $result .= '<input type="hidden" name="'.$name.'" value="'.
 2552:                    $hostid.'" />';
 2553:                    if (!$hide) {
 2554:                        $result .= $hostid.' '.$servers{$hostid};
 2555:                    }
 2556:                    $result .= "\n";
 2557:     } elsif ($default) {
 2558:         $result .= '<input type="hidden" name="'.$name.
 2559:                    '" value="default" />';
 2560:                    if (!$hide) {
 2561:                        $result .= &mt('default');
 2562:                    }
 2563:                    $result .= "\n";
 2564:     }
 2565:     return ($result,$numlib);
 2566: }
 2567: 
 2568: =pod
 2569: 
 2570: =back 
 2571: 
 2572: =cut
 2573: 
 2574: ###############################################################
 2575: ##                  Decoding User Agent                      ##
 2576: ###############################################################
 2577: 
 2578: =pod
 2579: 
 2580: =head1 Decoding the User Agent
 2581: 
 2582: =over 4
 2583: 
 2584: =item * &decode_user_agent()
 2585: 
 2586: Inputs: $r
 2587: 
 2588: Outputs:
 2589: 
 2590: =over 4
 2591: 
 2592: =item * $httpbrowser
 2593: 
 2594: =item * $clientbrowser
 2595: 
 2596: =item * $clientversion
 2597: 
 2598: =item * $clientmathml
 2599: 
 2600: =item * $clientunicode
 2601: 
 2602: =item * $clientos
 2603: 
 2604: =item * $clientmobile
 2605: 
 2606: =item * $clientinfo
 2607: 
 2608: =item * $clientosversion
 2609: 
 2610: =back
 2611: 
 2612: =back 
 2613: 
 2614: =cut
 2615: 
 2616: ###############################################################
 2617: ###############################################################
 2618: sub decode_user_agent {
 2619:     my ($r)=@_;
 2620:     my @browsertype=split(/\&/,$Apache::lonnet::perlvar{"lonBrowsDet"});
 2621:     my %mathcap=split(/\&/,$$Apache::lonnet::perlvar{"lonMathML"});
 2622:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
 2623:     if (!$httpbrowser && $r) { $httpbrowser=$r->header_in('User-Agent'); }
 2624:     my $clientbrowser='unknown';
 2625:     my $clientversion='0';
 2626:     my $clientmathml='';
 2627:     my $clientunicode='0';
 2628:     my $clientmobile=0;
 2629:     my $clientosversion='';
 2630:     for (my $i=0;$i<=$#browsertype;$i++) {
 2631:         my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\%/,$browsertype[$i]);
 2632: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
 2633: 	    $clientbrowser=$bname;
 2634:             $httpbrowser=~/$vreg/i;
 2635: 	    $clientversion=$1;
 2636:             $clientmathml=($clientversion>=$minv);
 2637:             $clientunicode=($clientversion>=$univ);
 2638: 	}
 2639:     }
 2640:     my $clientos='unknown';
 2641:     my $clientinfo;
 2642:     if (($httpbrowser=~/linux/i) ||
 2643:         ($httpbrowser=~/unix/i) ||
 2644:         ($httpbrowser=~/ux/i) ||
 2645:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
 2646:     if (($httpbrowser=~/vax/i) ||
 2647:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
 2648:     if ($httpbrowser=~/next/i) { $clientos='next'; }
 2649:     if (($httpbrowser=~/mac/i) ||
 2650:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
 2651:     if ($httpbrowser=~/win/i) {
 2652:         $clientos='win';
 2653:         if ($httpbrowser =~/Windows\s+NT\s+(\d+\.\d+)/i) {
 2654:             $clientosversion = $1;
 2655:         }
 2656:     }
 2657:     if ($httpbrowser=~/embed/i) { $clientos='pda'; }
 2658:     if ($httpbrowser=~/(Android|iPod|iPad|iPhone|webOS|Blackberry|Windows Phone|Opera m(?:ob|in)|Fennec)/i) {
 2659:         $clientmobile=lc($1);
 2660:     }
 2661:     if ($httpbrowser=~ m{Firefox/(\d+\.\d+)}) {
 2662:         $clientinfo = 'firefox-'.$1;
 2663:     } elsif ($httpbrowser=~ m{chromeframe/(\d+\.\d+)\.}) {
 2664:         $clientinfo = 'chromeframe-'.$1;
 2665:     }
 2666:     return ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
 2667:             $clientunicode,$clientos,$clientmobile,$clientinfo,
 2668:             $clientosversion);
 2669: }
 2670: 
 2671: ###############################################################
 2672: ##    Authentication changing form generation subroutines    ##
 2673: ###############################################################
 2674: ##
 2675: ## All of the authform_xxxxxxx subroutines take their inputs in a
 2676: ## hash, and have reasonable default values.
 2677: ##
 2678: ##    formname = the name given in the <form> tag.
 2679: #-------------------------------------------
 2680: 
 2681: =pod
 2682: 
 2683: =head1 Authentication Routines
 2684: 
 2685: =over 4
 2686: 
 2687: =item * &authform_xxxxxx()
 2688: 
 2689: The authform_xxxxxx subroutines provide javascript and html forms which 
 2690: handle some of the conveniences required for authentication forms.  
 2691: This is not an optimal method, but it works.  
 2692: 
 2693: =over 4
 2694: 
 2695: =item * authform_header
 2696: 
 2697: =item * authform_authorwarning
 2698: 
 2699: =item * authform_nochange
 2700: 
 2701: =item * authform_kerberos
 2702: 
 2703: =item * authform_internal
 2704: 
 2705: =item * authform_filesystem
 2706: 
 2707: =back
 2708: 
 2709: See loncreateuser.pm for invocation and use examples.
 2710: 
 2711: =cut
 2712: 
 2713: #-------------------------------------------
 2714: sub authform_header{  
 2715:     my %in = (
 2716:         formname => 'cu',
 2717:         kerb_def_dom => '',
 2718:         @_,
 2719:     );
 2720:     $in{'formname'} = 'document.' . $in{'formname'};
 2721:     my $result='';
 2722: 
 2723: #---------------------------------------------- Code for upper case translation
 2724:     my $Javascript_toUpperCase;
 2725:     unless ($in{kerb_def_dom}) {
 2726:         $Javascript_toUpperCase =<<"END";
 2727:         switch (choice) {
 2728:            case 'krb': currentform.elements[choicearg].value =
 2729:                currentform.elements[choicearg].value.toUpperCase();
 2730:                break;
 2731:            default:
 2732:         }
 2733: END
 2734:     } else {
 2735:         $Javascript_toUpperCase = "";
 2736:     }
 2737: 
 2738:     my $radioval = "'nochange'";
 2739:     if (defined($in{'curr_authtype'})) {
 2740:         if ($in{'curr_authtype'} ne '') {
 2741:             $radioval = "'".$in{'curr_authtype'}."arg'";
 2742:         }
 2743:     }
 2744:     my $argfield = 'null';
 2745:     if (defined($in{'mode'})) {
 2746:         if ($in{'mode'} eq 'modifycourse')  {
 2747:             if (defined($in{'curr_autharg'})) {
 2748:                 if ($in{'curr_autharg'} ne '') {
 2749:                     $argfield = "'$in{'curr_autharg'}'";
 2750:                 }
 2751:             }
 2752:         }
 2753:     }
 2754: 
 2755:     $result.=<<"END";
 2756: var current = new Object();
 2757: current.radiovalue = $radioval;
 2758: current.argfield = $argfield;
 2759: 
 2760: function changed_radio(choice,currentform) {
 2761:     var choicearg = choice + 'arg';
 2762:     // If a radio button in changed, we need to change the argfield
 2763:     if (current.radiovalue != choice) {
 2764:         current.radiovalue = choice;
 2765:         if (current.argfield != null) {
 2766:             currentform.elements[current.argfield].value = '';
 2767:         }
 2768:         if (choice == 'nochange') {
 2769:             current.argfield = null;
 2770:         } else {
 2771:             current.argfield = choicearg;
 2772:             switch(choice) {
 2773:                 case 'krb': 
 2774:                     currentform.elements[current.argfield].value = 
 2775:                         "$in{'kerb_def_dom'}";
 2776:                 break;
 2777:               default:
 2778:                 break;
 2779:             }
 2780:         }
 2781:     }
 2782:     return;
 2783: }
 2784: 
 2785: function changed_text(choice,currentform) {
 2786:     var choicearg = choice + 'arg';
 2787:     if (currentform.elements[choicearg].value !='') {
 2788:         $Javascript_toUpperCase
 2789:         // clear old field
 2790:         if ((current.argfield != choicearg) && (current.argfield != null)) {
 2791:             currentform.elements[current.argfield].value = '';
 2792:         }
 2793:         current.argfield = choicearg;
 2794:     }
 2795:     set_auth_radio_buttons(choice,currentform);
 2796:     return;
 2797: }
 2798: 
 2799: function set_auth_radio_buttons(newvalue,currentform) {
 2800:     var numauthchoices = currentform.login.length;
 2801:     if (typeof numauthchoices  == "undefined") {
 2802:         return;
 2803:     } 
 2804:     var i=0;
 2805:     while (i < numauthchoices) {
 2806:         if (currentform.login[i].value == newvalue) { break; }
 2807:         i++;
 2808:     }
 2809:     if (i == numauthchoices) {
 2810:         return;
 2811:     }
 2812:     current.radiovalue = newvalue;
 2813:     currentform.login[i].checked = true;
 2814:     return;
 2815: }
 2816: END
 2817:     return $result;
 2818: }
 2819: 
 2820: sub authform_authorwarning {
 2821:     my $result='';
 2822:     $result='<i>'.
 2823:         &mt('As a general rule, only authors or co-authors should be '.
 2824:             'filesystem authenticated '.
 2825:             '(which allows access to the server filesystem).')."</i>\n";
 2826:     return $result;
 2827: }
 2828: 
 2829: sub authform_nochange {
 2830:     my %in = (
 2831:               formname => 'document.cu',
 2832:               kerb_def_dom => 'MSU.EDU',
 2833:               @_,
 2834:           );
 2835:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
 2836:     my $result;
 2837:     if (!$authnum) {
 2838:         $result = &mt('Under your current role you are not permitted to change login settings for this user');
 2839:     } else {
 2840:         $result = '<label>'.&mt('[_1] Do not change login data',
 2841:                   '<input type="radio" name="login" value="nochange" '.
 2842:                   'checked="checked" onclick="'.
 2843:             "javascript:changed_radio('nochange',$in{'formname'});".'" />').
 2844: 	    '</label>';
 2845:     }
 2846:     return $result;
 2847: }
 2848: 
 2849: sub authform_kerberos {
 2850:     my %in = (
 2851:               formname => 'document.cu',
 2852:               kerb_def_dom => 'MSU.EDU',
 2853:               kerb_def_auth => 'krb4',
 2854:               @_,
 2855:               );
 2856:     my ($check4,$check5,$krbcheck,$krbarg,$krbver,$result,$authtype,
 2857:         $autharg,$jscall);
 2858:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
 2859:     if ($in{'kerb_def_auth'} eq 'krb5') {
 2860:        $check5 = ' checked="checked"';
 2861:     } else {
 2862:        $check4 = ' checked="checked"';
 2863:     }
 2864:     $krbarg = $in{'kerb_def_dom'};
 2865:     if (defined($in{'curr_authtype'})) {
 2866:         if ($in{'curr_authtype'} eq 'krb') {
 2867:             $krbcheck = ' checked="checked"';
 2868:             if (defined($in{'mode'})) {
 2869:                 if ($in{'mode'} eq 'modifyuser') {
 2870:                     $krbcheck = '';
 2871:                 }
 2872:             }
 2873:             if (defined($in{'curr_kerb_ver'})) {
 2874:                 if ($in{'curr_krb_ver'} eq '5') {
 2875:                     $check5 = ' checked="checked"';
 2876:                     $check4 = '';
 2877:                 } else {
 2878:                     $check4 = ' checked="checked"';
 2879:                     $check5 = '';
 2880:                 }
 2881:             }
 2882:             if (defined($in{'curr_autharg'})) {
 2883:                 $krbarg = $in{'curr_autharg'};
 2884:             }
 2885:             if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
 2886:                 if (defined($in{'curr_autharg'})) {
 2887:                     $result = 
 2888:     &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
 2889:         $in{'curr_autharg'},$krbver);
 2890:                 } else {
 2891:                     $result =
 2892:     &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
 2893:                 }
 2894:                 return $result; 
 2895:             }
 2896:         }
 2897:     } else {
 2898:         if ($authnum == 1) {
 2899:             $authtype = '<input type="hidden" name="login" value="krb" />';
 2900:         }
 2901:     }
 2902:     if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
 2903:         return;
 2904:     } elsif ($authtype eq '') {
 2905:         if (defined($in{'mode'})) {
 2906:             if ($in{'mode'} eq 'modifycourse') {
 2907:                 if ($authnum == 1) {
 2908:                     $authtype = '<input type="radio" name="login" value="krb" />';
 2909:                 }
 2910:             }
 2911:         }
 2912:     }
 2913:     $jscall = "javascript:changed_radio('krb',$in{'formname'});";
 2914:     if ($authtype eq '') {
 2915:         $authtype = '<input type="radio" name="login" value="krb" '.
 2916:                     'onclick="'.$jscall.'" onchange="'.$jscall.'"'.
 2917:                     $krbcheck.' />';
 2918:     }
 2919:     if (($can_assign{'krb4'} && $can_assign{'krb5'}) ||
 2920:         ($can_assign{'krb4'} && !$can_assign{'krb5'} &&
 2921:          $in{'curr_authtype'} eq 'krb5') ||
 2922:         (!$can_assign{'krb4'} && $can_assign{'krb5'} &&
 2923:          $in{'curr_authtype'} eq 'krb4')) {
 2924:         $result .= &mt
 2925:         ('[_1] Kerberos authenticated with domain [_2] '.
 2926:          '[_3] Version 4 [_4] Version 5 [_5]',
 2927:          '<label>'.$authtype,
 2928:          '</label><input type="text" size="10" name="krbarg" '.
 2929:              'value="'.$krbarg.'" '.
 2930:              'onchange="'.$jscall.'" />',
 2931:          '<label><input type="radio" name="krbver" value="4" '.$check4.' />',
 2932:          '</label><label><input type="radio" name="krbver" value="5" '.$check5.' />',
 2933: 	 '</label>');
 2934:     } elsif ($can_assign{'krb4'}) {
 2935:         $result .= &mt
 2936:         ('[_1] Kerberos authenticated with domain [_2] '.
 2937:          '[_3] Version 4 [_4]',
 2938:          '<label>'.$authtype,
 2939:          '</label><input type="text" size="10" name="krbarg" '.
 2940:              'value="'.$krbarg.'" '.
 2941:              'onchange="'.$jscall.'" />',
 2942:          '<label><input type="hidden" name="krbver" value="4" />',
 2943:          '</label>');
 2944:     } elsif ($can_assign{'krb5'}) {
 2945:         $result .= &mt
 2946:         ('[_1] Kerberos authenticated with domain [_2] '.
 2947:          '[_3] Version 5 [_4]',
 2948:          '<label>'.$authtype,
 2949:          '</label><input type="text" size="10" name="krbarg" '.
 2950:              'value="'.$krbarg.'" '.
 2951:              'onchange="'.$jscall.'" />',
 2952:          '<label><input type="hidden" name="krbver" value="5" />',
 2953:          '</label>');
 2954:     }
 2955:     return $result;
 2956: }
 2957: 
 2958: sub authform_internal {
 2959:     my %in = (
 2960:                 formname => 'document.cu',
 2961:                 kerb_def_dom => 'MSU.EDU',
 2962:                 @_,
 2963:                 );
 2964:     my ($intcheck,$intarg,$result,$authtype,$autharg,$jscall);
 2965:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
 2966:     if (defined($in{'curr_authtype'})) {
 2967:         if ($in{'curr_authtype'} eq 'int') {
 2968:             if ($can_assign{'int'}) {
 2969:                 $intcheck = 'checked="checked" ';
 2970:                 if (defined($in{'mode'})) {
 2971:                     if ($in{'mode'} eq 'modifyuser') {
 2972:                         $intcheck = '';
 2973:                     }
 2974:                 }
 2975:                 if (defined($in{'curr_autharg'})) {
 2976:                     $intarg = $in{'curr_autharg'};
 2977:                 }
 2978:             } else {
 2979:                 $result = &mt('Currently internally authenticated.');
 2980:                 return $result;
 2981:             }
 2982:         }
 2983:     } else {
 2984:         if ($authnum == 1) {
 2985:             $authtype = '<input type="hidden" name="login" value="int" />';
 2986:         }
 2987:     }
 2988:     if (!$can_assign{'int'}) {
 2989:         return;
 2990:     } elsif ($authtype eq '') {
 2991:         if (defined($in{'mode'})) {
 2992:             if ($in{'mode'} eq 'modifycourse') {
 2993:                 if ($authnum == 1) {
 2994:                     $authtype = '<input type="radio" name="login" value="int" />';
 2995:                 }
 2996:             }
 2997:         }
 2998:     }
 2999:     $jscall = "javascript:changed_radio('int',$in{'formname'});";
 3000:     if ($authtype eq '') {
 3001:         $authtype = '<input type="radio" name="login" value="int" '.$intcheck.
 3002:                     ' onchange="'.$jscall.'" onclick="'.$jscall.'" />';
 3003:     }
 3004:     $autharg = '<input type="password" size="10" name="intarg" value="'.
 3005:                $intarg.'" onchange="'.$jscall.'" />';
 3006:     $result = &mt
 3007:         ('[_1] Internally authenticated (with initial password [_2])',
 3008:          '<label>'.$authtype,'</label>'.$autharg);
 3009:     $result.="<label><input type=\"checkbox\" name=\"visible\" onclick='if (this.checked) { this.form.intarg.type=\"text\" } else { this.form.intarg.type=\"password\" }' />".&mt('Visible input').'</label>';
 3010:     return $result;
 3011: }
 3012: 
 3013: sub authform_local {
 3014:     my %in = (
 3015:               formname => 'document.cu',
 3016:               kerb_def_dom => 'MSU.EDU',
 3017:               @_,
 3018:               );
 3019:     my ($loccheck,$locarg,$result,$authtype,$autharg,$jscall);
 3020:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
 3021:     if (defined($in{'curr_authtype'})) {
 3022:         if ($in{'curr_authtype'} eq 'loc') {
 3023:             if ($can_assign{'loc'}) {
 3024:                 $loccheck = 'checked="checked" ';
 3025:                 if (defined($in{'mode'})) {
 3026:                     if ($in{'mode'} eq 'modifyuser') {
 3027:                         $loccheck = '';
 3028:                     }
 3029:                 }
 3030:                 if (defined($in{'curr_autharg'})) {
 3031:                     $locarg = $in{'curr_autharg'};
 3032:                 }
 3033:             } else {
 3034:                 $result = &mt('Currently using local (institutional) authentication.');
 3035:                 return $result;
 3036:             }
 3037:         }
 3038:     } else {
 3039:         if ($authnum == 1) {
 3040:             $authtype = '<input type="hidden" name="login" value="loc" />';
 3041:         }
 3042:     }
 3043:     if (!$can_assign{'loc'}) {
 3044:         return;
 3045:     } elsif ($authtype eq '') {
 3046:         if (defined($in{'mode'})) {
 3047:             if ($in{'mode'} eq 'modifycourse') {
 3048:                 if ($authnum == 1) {
 3049:                     $authtype = '<input type="radio" name="login" value="loc" />';
 3050:                 }
 3051:             }
 3052:         }
 3053:     }
 3054:     $jscall = "javascript:changed_radio('loc',$in{'formname'});";
 3055:     if ($authtype eq '') {
 3056:         $authtype = '<input type="radio" name="login" value="loc" '.
 3057:                     $loccheck.' onchange="'.$jscall.'" onclick="'.
 3058:                     $jscall.'" />';
 3059:     }
 3060:     $autharg = '<input type="text" size="10" name="locarg" value="'.
 3061:                $locarg.'" onchange="'.$jscall.'" />';
 3062:     $result = &mt('[_1] Local Authentication with argument [_2]',
 3063:                   '<label>'.$authtype,'</label>'.$autharg);
 3064:     return $result;
 3065: }
 3066: 
 3067: sub authform_filesystem {
 3068:     my %in = (
 3069:               formname => 'document.cu',
 3070:               kerb_def_dom => 'MSU.EDU',
 3071:               @_,
 3072:               );
 3073:     my ($fsyscheck,$result,$authtype,$autharg,$jscall);
 3074:     my ($authnum,%can_assign) = &get_assignable_auth($in{'domain'});
 3075:     if (defined($in{'curr_authtype'})) {
 3076:         if ($in{'curr_authtype'} eq 'fsys') {
 3077:             if ($can_assign{'fsys'}) {
 3078:                 $fsyscheck = 'checked="checked" ';
 3079:                 if (defined($in{'mode'})) {
 3080:                     if ($in{'mode'} eq 'modifyuser') {
 3081:                         $fsyscheck = '';
 3082:                     }
 3083:                 }
 3084:             } else {
 3085:                 $result = &mt('Currently Filesystem Authenticated.');
 3086:                 return $result;
 3087:             }           
 3088:         }
 3089:     } else {
 3090:         if ($authnum == 1) {
 3091:             $authtype = '<input type="hidden" name="login" value="fsys" />';
 3092:         }
 3093:     }
 3094:     if (!$can_assign{'fsys'}) {
 3095:         return;
 3096:     } elsif ($authtype eq '') {
 3097:         if (defined($in{'mode'})) {
 3098:             if ($in{'mode'} eq 'modifycourse') {
 3099:                 if ($authnum == 1) {
 3100:                     $authtype = '<input type="radio" name="login" value="fsys" />';
 3101:                 }
 3102:             }
 3103:         }
 3104:     }
 3105:     $jscall = "javascript:changed_radio('fsys',$in{'formname'});";
 3106:     if ($authtype eq '') {
 3107:         $authtype = '<input type="radio" name="login" value="fsys" '.
 3108:                     $fsyscheck.' onchange="'.$jscall.'" onclick="'.
 3109:                     $jscall.'" />';
 3110:     }
 3111:     $autharg = '<input type="text" size="10" name="fsysarg" value=""'.
 3112:                ' onchange="'.$jscall.'" />';
 3113:     $result = &mt
 3114:         ('[_1] Filesystem Authenticated (with initial password [_2])',
 3115:          '<label><input type="radio" name="login" value="fsys" '.
 3116:          $fsyscheck.'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
 3117:          '</label><input type="password" size="10" name="fsysarg" value="" '.
 3118:                   'onchange="'.$jscall.'" />');
 3119:     return $result;
 3120: }
 3121: 
 3122: sub get_assignable_auth {
 3123:     my ($dom) = @_;
 3124:     if ($dom eq '') {
 3125:         $dom = $env{'request.role.domain'};
 3126:     }
 3127:     my %can_assign = (
 3128:                           krb4 => 1,
 3129:                           krb5 => 1,
 3130:                           int  => 1,
 3131:                           loc  => 1,
 3132:                      );
 3133:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
 3134:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 3135:         if (ref($domconfig{'usercreation'}{'authtypes'}) eq 'HASH') {
 3136:             my $authhash = $domconfig{'usercreation'}{'authtypes'};
 3137:             my $context;
 3138:             if ($env{'request.role'} =~ /^au/) {
 3139:                 $context = 'author';
 3140:             } elsif ($env{'request.role'} =~ /^dc/) {
 3141:                 $context = 'domain';
 3142:             } elsif ($env{'request.course.id'}) {
 3143:                 $context = 'course';
 3144:             }
 3145:             if ($context) {
 3146:                 if (ref($authhash->{$context}) eq 'HASH') {
 3147:                    %can_assign = %{$authhash->{$context}}; 
 3148:                 }
 3149:             }
 3150:         }
 3151:     }
 3152:     my $authnum = 0;
 3153:     foreach my $key (keys(%can_assign)) {
 3154:         if ($can_assign{$key}) {
 3155:             $authnum ++;
 3156:         }
 3157:     }
 3158:     if ($can_assign{'krb4'} && $can_assign{'krb5'}) {
 3159:         $authnum --;
 3160:     }
 3161:     return ($authnum,%can_assign);
 3162: }
 3163: 
 3164: ###############################################################
 3165: ##    Get Kerberos Defaults for Domain                 ##
 3166: ###############################################################
 3167: ##
 3168: ## Returns default kerberos version and an associated argument
 3169: ## as listed in file domain.tab. If not listed, provides
 3170: ## appropriate default domain and kerberos version.
 3171: ##
 3172: #-------------------------------------------
 3173: 
 3174: =pod
 3175: 
 3176: =item * &get_kerberos_defaults()
 3177: 
 3178: get_kerberos_defaults($target_domain) returns the default kerberos
 3179: version and domain. If not found, it defaults to version 4 and the 
 3180: domain of the server.
 3181: 
 3182: =over 4
 3183: 
 3184: ($def_version, $def_krb_domain) = &get_kerberos_defaults($target_domain);
 3185: 
 3186: =back
 3187: 
 3188: =back
 3189: 
 3190: =cut
 3191: 
 3192: #-------------------------------------------
 3193: sub get_kerberos_defaults {
 3194:     my $domain=shift;
 3195:     my ($krbdef,$krbdefdom);
 3196:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
 3197:     if (($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) {
 3198:         $krbdef = $domdefaults{'auth_def'};
 3199:         $krbdefdom = $domdefaults{'auth_arg_def'};
 3200:     } else {
 3201:         $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
 3202:         my $krbdefdom=$1;
 3203:         $krbdefdom=~tr/a-z/A-Z/;
 3204:         $krbdef = "krb4";
 3205:     }
 3206:     return ($krbdef,$krbdefdom);
 3207: }
 3208: 
 3209: 
 3210: ###############################################################
 3211: ##                Thesaurus Functions                        ##
 3212: ###############################################################
 3213: 
 3214: =pod
 3215: 
 3216: =head1 Thesaurus Functions
 3217: 
 3218: =over 4
 3219: 
 3220: =item * &initialize_keywords()
 3221: 
 3222: Initializes the package variable %Keywords if it is empty.  Uses the
 3223: package variable $thesaurus_db_file.
 3224: 
 3225: =cut
 3226: 
 3227: ###################################################
 3228: 
 3229: sub initialize_keywords {
 3230:     return 1 if (scalar keys(%Keywords));
 3231:     # If we are here, %Keywords is empty, so fill it up
 3232:     #   Make sure the file we need exists...
 3233:     if (! -e $thesaurus_db_file) {
 3234:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file".
 3235:                                  " failed because it does not exist");
 3236:         return 0;
 3237:     }
 3238:     #   Set up the hash as a database
 3239:     my %thesaurus_db;
 3240:     if (! tie(%thesaurus_db,'GDBM_File',
 3241:               $thesaurus_db_file,&GDBM_READER(),0640)){
 3242:         &Apache::lonnet::logthis("Could not tie \%thesaurus_db to ".
 3243:                                  $thesaurus_db_file);
 3244:         return 0;
 3245:     } 
 3246:     #  Get the average number of appearances of a word.
 3247:     my $avecount = $thesaurus_db{'average.count'};
 3248:     #  Put keywords (those that appear > average) into %Keywords
 3249:     while (my ($word,$data)=each (%thesaurus_db)) {
 3250:         my ($count,undef) = split /:/,$data;
 3251:         $Keywords{$word}++ if ($count > $avecount);
 3252:     }
 3253:     untie %thesaurus_db;
 3254:     # Remove special values from %Keywords.
 3255:     foreach my $value ('total.count','average.count') {
 3256:         delete($Keywords{$value}) if (exists($Keywords{$value}));
 3257:   }
 3258:     return 1;
 3259: }
 3260: 
 3261: ###################################################
 3262: 
 3263: =pod
 3264: 
 3265: =item * &keyword($word)
 3266: 
 3267: Returns true if $word is a keyword.  A keyword is a word that appears more 
 3268: than the average number of times in the thesaurus database.  Calls 
 3269: &initialize_keywords
 3270: 
 3271: =cut
 3272: 
 3273: ###################################################
 3274: 
 3275: sub keyword {
 3276:     return if (!&initialize_keywords());
 3277:     my $word=lc(shift());
 3278:     $word=~s/\W//g;
 3279:     return exists($Keywords{$word});
 3280: }
 3281: 
 3282: ###############################################################
 3283: 
 3284: =pod 
 3285: 
 3286: =item * &get_related_words()
 3287: 
 3288: Look up a word in the thesaurus.  Takes a scalar argument and returns
 3289: an array of words.  If the keyword is not in the thesaurus, an empty array
 3290: will be returned.  The order of the words returned is determined by the
 3291: database which holds them.
 3292: 
 3293: Uses global $thesaurus_db_file.
 3294: 
 3295: 
 3296: =cut
 3297: 
 3298: ###############################################################
 3299: sub get_related_words {
 3300:     my $keyword = shift;
 3301:     my %thesaurus_db;
 3302:     if (! -e $thesaurus_db_file) {
 3303:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file ".
 3304:                                  "failed because the file does not exist");
 3305:         return ();
 3306:     }
 3307:     if (! tie(%thesaurus_db,'GDBM_File',
 3308:               $thesaurus_db_file,&GDBM_READER(),0640)){
 3309:         return ();
 3310:     } 
 3311:     my @Words=();
 3312:     my $count=0;
 3313:     if (exists($thesaurus_db{$keyword})) {
 3314: 	# The first element is the number of times
 3315: 	# the word appears.  We do not need it now.
 3316: 	my (undef,@RelatedWords) = (split(/:/,$thesaurus_db{$keyword}));
 3317: 	my (undef,$mostfrequentcount)=split(/\,/,$RelatedWords[0]);
 3318: 	my $threshold=$mostfrequentcount/10;
 3319:         foreach my $possibleword (@RelatedWords) {
 3320:             my ($word,$wordcount)=split(/\,/,$possibleword);
 3321:             if ($wordcount>$threshold) {
 3322: 		push(@Words,$word);
 3323:                 $count++;
 3324:                 if ($count>10) { last; }
 3325: 	    }
 3326:         }
 3327:     }
 3328:     untie %thesaurus_db;
 3329:     return @Words;
 3330: }
 3331: ###############################################################
 3332: #
 3333: #  Spell checking
 3334: #
 3335: 
 3336: =pod
 3337: 
 3338: =back
 3339: 
 3340: =head1 Spell checking
 3341: 
 3342: =over 4
 3343: 
 3344: =item * &check_spelling($wordlist $language)
 3345: 
 3346: Takes a string containing words and feeds it to an external
 3347: spellcheck program via a pipeline. Returns a string containing
 3348: them mis-spelled words.
 3349: 
 3350: Parameters:
 3351: 
 3352: =over 4
 3353: 
 3354: =item - $wordlist
 3355: 
 3356: String that will be fed into the spellcheck program.
 3357: 
 3358: =item - $language
 3359: 
 3360: Language string that specifies the language for which the spell
 3361: check will be performed.
 3362: 
 3363: =back
 3364: 
 3365: =back
 3366: 
 3367: Note: This sub assumes that aspell is installed.
 3368: 
 3369: 
 3370: =cut
 3371: 
 3372: 
 3373: sub check_spelling {
 3374:     my ($wordlist, $language) = @_;
 3375:     my @misspellings;
 3376:     
 3377:     # Generate the speller and set the langauge.
 3378:     # if explicitly selected:
 3379: 
 3380:     my $speller = Text::Aspell->new;
 3381:     if ($language) {
 3382: 	$speller->set_option('lang', $language);
 3383:     }
 3384: 
 3385:     # Turn the word list into an array of words by splittingon whitespace
 3386: 
 3387:     my @words = split(/\s+/, $wordlist);
 3388: 
 3389:     foreach my $word (@words) {
 3390: 	if(! $speller->check($word)) {
 3391: 	    push(@misspellings, $word);
 3392: 	}
 3393:     }
 3394:     return join(' ', @misspellings);
 3395:     
 3396: }
 3397: 
 3398: # -------------------------------------------------------------- Plaintext name
 3399: =pod
 3400: 
 3401: =head1 User Name Functions
 3402: 
 3403: =over 4
 3404: 
 3405: =item * &plainname($uname,$udom,$first)
 3406: 
 3407: Takes a users logon name and returns it as a string in
 3408: "first middle last generation" form 
 3409: if $first is set to 'lastname' then it returns it as
 3410: 'lastname generation, firstname middlename' if their is a lastname
 3411: 
 3412: =cut
 3413: 
 3414: 
 3415: ###############################################################
 3416: sub plainname {
 3417:     my ($uname,$udom,$first)=@_;
 3418:     return if (!defined($uname) || !defined($udom));
 3419:     my %names=&getnames($uname,$udom);
 3420:     my $name=&Apache::lonnet::format_name($names{'firstname'},
 3421: 					  $names{'middlename'},
 3422: 					  $names{'lastname'},
 3423: 					  $names{'generation'},$first);
 3424:     $name=~s/^\s+//;
 3425:     $name=~s/\s+$//;
 3426:     $name=~s/\s+/ /g;
 3427:     if ($name !~ /\S/) { $name=$uname.':'.$udom; }
 3428:     return $name;
 3429: }
 3430: 
 3431: # -------------------------------------------------------------------- Nickname
 3432: =pod
 3433: 
 3434: =item * &nickname($uname,$udom)
 3435: 
 3436: Gets a users name and returns it as a string as
 3437: 
 3438: "&quot;nickname&quot;"
 3439: 
 3440: if the user has a nickname or
 3441: 
 3442: "first middle last generation"
 3443: 
 3444: if the user does not
 3445: 
 3446: =cut
 3447: 
 3448: sub nickname {
 3449:     my ($uname,$udom)=@_;
 3450:     return if (!defined($uname) || !defined($udom));
 3451:     my %names=&getnames($uname,$udom);
 3452:     my $name=$names{'nickname'};
 3453:     if ($name) {
 3454:        $name='&quot;'.$name.'&quot;'; 
 3455:     } else {
 3456:        $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
 3457: 	     $names{'lastname'}.' '.$names{'generation'};
 3458:        $name=~s/\s+$//;
 3459:        $name=~s/\s+/ /g;
 3460:     }
 3461:     return $name;
 3462: }
 3463: 
 3464: sub getnames {
 3465:     my ($uname,$udom)=@_;
 3466:     return if (!defined($uname) || !defined($udom));
 3467:     if ($udom eq 'public' && $uname eq 'public') {
 3468: 	return ('lastname' => &mt('Public'));
 3469:     }
 3470:     my $id=$uname.':'.$udom;
 3471:     my ($names,$cached)=&Apache::lonnet::is_cached_new('namescache',$id);
 3472:     if ($cached) {
 3473: 	return %{$names};
 3474:     } else {
 3475: 	my %loadnames=&Apache::lonnet::get('environment',
 3476:                     ['firstname','middlename','lastname','generation','nickname'],
 3477: 					 $udom,$uname);
 3478: 	&Apache::lonnet::do_cache_new('namescache',$id,\%loadnames);
 3479: 	return %loadnames;
 3480:     }
 3481: }
 3482: 
 3483: # -------------------------------------------------------------------- getemails
 3484: 
 3485: =pod
 3486: 
 3487: =item * &getemails($uname,$udom)
 3488: 
 3489: Gets a user's email information and returns it as a hash with keys:
 3490: notification, critnotification, permanentemail
 3491: 
 3492: For notification and critnotification, values are comma-separated lists 
 3493: of e-mail addresses; for permanentemail, value is a single e-mail address.
 3494:  
 3495: 
 3496: =cut
 3497: 
 3498: 
 3499: sub getemails {
 3500:     my ($uname,$udom)=@_;
 3501:     if ($udom eq 'public' && $uname eq 'public') {
 3502: 	return;
 3503:     }
 3504:     if (!$udom) { $udom=$env{'user.domain'}; }
 3505:     if (!$uname) { $uname=$env{'user.name'}; }
 3506:     my $id=$uname.':'.$udom;
 3507:     my ($names,$cached)=&Apache::lonnet::is_cached_new('emailscache',$id);
 3508:     if ($cached) {
 3509: 	return %{$names};
 3510:     } else {
 3511: 	my %loadnames=&Apache::lonnet::get('environment',
 3512:                     			   ['notification','critnotification',
 3513: 					    'permanentemail'],
 3514: 					   $udom,$uname);
 3515: 	&Apache::lonnet::do_cache_new('emailscache',$id,\%loadnames);
 3516: 	return %loadnames;
 3517:     }
 3518: }
 3519: 
 3520: sub flush_email_cache {
 3521:     my ($uname,$udom)=@_;
 3522:     if (!$udom)  { $udom =$env{'user.domain'}; }
 3523:     if (!$uname) { $uname=$env{'user.name'};   }
 3524:     return if ($udom eq 'public' && $uname eq 'public');
 3525:     my $id=$uname.':'.$udom;
 3526:     &Apache::lonnet::devalidate_cache_new('emailscache',$id);
 3527: }
 3528: 
 3529: # -------------------------------------------------------------------- getlangs
 3530: 
 3531: =pod
 3532: 
 3533: =item * &getlangs($uname,$udom)
 3534: 
 3535: Gets a user's language preference and returns it as a hash with key:
 3536: language.
 3537: 
 3538: =cut
 3539: 
 3540: 
 3541: sub getlangs {
 3542:     my ($uname,$udom) = @_;
 3543:     if (!$udom)  { $udom =$env{'user.domain'}; }
 3544:     if (!$uname) { $uname=$env{'user.name'};   }
 3545:     my $id=$uname.':'.$udom;
 3546:     my ($langs,$cached)=&Apache::lonnet::is_cached_new('userlangs',$id);
 3547:     if ($cached) {
 3548:         return %{$langs};
 3549:     } else {
 3550:         my %loadlangs=&Apache::lonnet::get('environment',['languages'],
 3551:                                            $udom,$uname);
 3552:         &Apache::lonnet::do_cache_new('userlangs',$id,\%loadlangs);
 3553:         return %loadlangs;
 3554:     }
 3555: }
 3556: 
 3557: sub flush_langs_cache {
 3558:     my ($uname,$udom)=@_;
 3559:     if (!$udom)  { $udom =$env{'user.domain'}; }
 3560:     if (!$uname) { $uname=$env{'user.name'};   }
 3561:     return if ($udom eq 'public' && $uname eq 'public');
 3562:     my $id=$uname.':'.$udom;
 3563:     &Apache::lonnet::devalidate_cache_new('userlangs',$id);
 3564: }
 3565: 
 3566: # ------------------------------------------------------------------ Screenname
 3567: 
 3568: =pod
 3569: 
 3570: =item * &screenname($uname,$udom)
 3571: 
 3572: Gets a users screenname and returns it as a string
 3573: 
 3574: =cut
 3575: 
 3576: sub screenname {
 3577:     my ($uname,$udom)=@_;
 3578:     if ($uname eq $env{'user.name'} &&
 3579: 	$udom eq $env{'user.domain'}) {return $env{'environment.screenname'};}
 3580:     my %names=&Apache::lonnet::get('environment',['screenname'],$udom,$uname);
 3581:     return $names{'screenname'};
 3582: }
 3583: 
 3584: 
 3585: # ------------------------------------------------------------- Confirm Wrapper
 3586: =pod
 3587: 
 3588: =item * &confirmwrapper($message)
 3589: 
 3590: Wrap messages about completion of operation in box
 3591: 
 3592: =cut
 3593: 
 3594: sub confirmwrapper {
 3595:     my ($message)=@_;
 3596:     if ($message) {
 3597:         return "\n".'<div class="LC_confirm_box">'."\n"
 3598:                .$message."\n"
 3599:                .'</div>'."\n";
 3600:     } else {
 3601:         return $message;
 3602:     }
 3603: }
 3604: 
 3605: # ------------------------------------------------------------- Message Wrapper
 3606: 
 3607: sub messagewrapper {
 3608:     my ($link,$username,$domain,$subject,$text)=@_;
 3609:     return 
 3610:         '<a href="/adm/email?compose=individual&amp;'.
 3611:         'recname='.$username.'&amp;recdom='.$domain.
 3612: 	'&amp;subject='.&escape($subject).'&amp;text='.&escape($text).'" '.
 3613:         'title="'.&mt('Send message').'">'.$link.'</a>';
 3614: }
 3615: 
 3616: # --------------------------------------------------------------- Notes Wrapper
 3617: 
 3618: sub noteswrapper {
 3619:     my ($link,$un,$do)=@_;
 3620:     return 
 3621: "<a href='/adm/email?recordftf=retrieve&amp;recname=$un&amp;recdom=$do'>$link</a>";
 3622: }
 3623: 
 3624: # ------------------------------------------------------------- Aboutme Wrapper
 3625: 
 3626: sub aboutmewrapper {
 3627:     my ($link,$username,$domain,$target,$class)=@_;
 3628:     if (!defined($username)  && !defined($domain)) {
 3629:         return;
 3630:     }
 3631:     return '<a href="/adm/'.$domain.'/'.$username.'/aboutme"'.
 3632: 	($target?' target="'.$target.'"':'').($class?' class="'.$class.'"':'').' title="'.&mt("View this user's personal information page").'">'.$link.'</a>';
 3633: }
 3634: 
 3635: # ------------------------------------------------------------ Syllabus Wrapper
 3636: 
 3637: sub syllabuswrapper {
 3638:     my ($linktext,$coursedir,$domain)=@_;
 3639:     return qq{<a href="/public/$domain/$coursedir/syllabus">$linktext</a>};
 3640: }
 3641: 
 3642: # -----------------------------------------------------------------------------
 3643: 
 3644: sub track_student_link {
 3645:     my ($linktext,$sname,$sdom,$target,$start,$only_body) = @_;
 3646:     my $link ="/adm/trackstudent?";
 3647:     my $title = 'View recent activity';
 3648:     if (defined($sname) && $sname !~ /^\s*$/ &&
 3649:         defined($sdom)  && $sdom  !~ /^\s*$/) {
 3650:         $link .= "selected_student=$sname:$sdom";
 3651:         $title .= ' of this student';
 3652:     } 
 3653:     if (defined($target) && $target !~ /^\s*$/) {
 3654:         $target = qq{target="$target"};
 3655:     } else {
 3656:         $target = '';
 3657:     }
 3658:     if ($start) { $link.='&amp;start='.$start; }
 3659:     if ($only_body) { $link .= '&amp;only_body=1'; }
 3660:     $title = &mt($title);
 3661:     $linktext = &mt($linktext);
 3662:     return qq{<a href="$link" title="$title" $target>$linktext</a>}.
 3663: 	&help_open_topic('View_recent_activity');
 3664: }
 3665: 
 3666: sub slot_reservations_link {
 3667:     my ($linktext,$sname,$sdom,$target) = @_;
 3668:     my $link ="/adm/slotrequest?command=showresv&amp;origin=aboutme";
 3669:     my $title = 'View slot reservation history';
 3670:     if (defined($sname) && $sname !~ /^\s*$/ &&
 3671:         defined($sdom)  && $sdom  !~ /^\s*$/) {
 3672:         $link .= "&amp;uname=$sname&amp;udom=$sdom";
 3673:         $title .= ' of this student';
 3674:     }
 3675:     if (defined($target) && $target !~ /^\s*$/) {
 3676:         $target = qq{target="$target"};
 3677:     } else {
 3678:         $target = '';
 3679:     }
 3680:     $title = &mt($title);
 3681:     $linktext = &mt($linktext);
 3682:     return qq{<a href="$link" title="$title" $target>$linktext</a>};
 3683: # FIXME uncomment when help item created: &help_open_topic('Slot_Reservation_History');
 3684: 
 3685: }
 3686: 
 3687: # ===================================================== Display a student photo
 3688: 
 3689: 
 3690: sub student_image_tag {
 3691:     my ($domain,$user)=@_;
 3692:     my $imgsrc=&Apache::lonnet::studentphoto($domain,$user,'jpg');
 3693:     if (($imgsrc) && ($imgsrc ne '/adm/lonKaputt/lonlogo_broken.gif')) {
 3694: 	return '<img src="'.$imgsrc.'" align="right" />';
 3695:     } else {
 3696: 	return '';
 3697:     }
 3698: }
 3699: 
 3700: =pod
 3701: 
 3702: =back
 3703: 
 3704: =head1 Access .tab File Data
 3705: 
 3706: =over 4
 3707: 
 3708: =item * &languageids() 
 3709: 
 3710: returns list of all language ids
 3711: 
 3712: =cut
 3713: 
 3714: sub languageids {
 3715:     return sort(keys(%language));
 3716: }
 3717: 
 3718: =pod
 3719: 
 3720: =item * &languagedescription() 
 3721: 
 3722: returns description of a specified language id
 3723: 
 3724: =cut
 3725: 
 3726: sub languagedescription {
 3727:     my $code=shift;
 3728:     return  ($supported_language{$code}?'* ':'').
 3729:             $language{$code}.
 3730: 	    ($supported_language{$code}?' ('.&mt('interface available').')':'');
 3731: }
 3732: 
 3733: =pod
 3734: 
 3735: =item * &plainlanguagedescription
 3736: 
 3737: Returns both the plain language description (e.g. 'Creoles and Pidgins, English-based (Other)')
 3738: and the language character encoding (e.g. ISO) separated by a ' - ' string.
 3739: 
 3740: =cut
 3741: 
 3742: sub plainlanguagedescription {
 3743:     my $code=shift;
 3744:     return $language{$code};
 3745: }
 3746: 
 3747: =pod
 3748: 
 3749: =item * &supportedlanguagecode
 3750: 
 3751: Returns the supported language code (e.g. sptutf maps to pt) given a language
 3752: code.
 3753: 
 3754: =cut
 3755: 
 3756: sub supportedlanguagecode {
 3757:     my $code=shift;
 3758:     return $supported_language{$code};
 3759: }
 3760: 
 3761: =pod
 3762: 
 3763: =item * &latexlanguage()
 3764: 
 3765: Given a language key code returns the correspondnig language to use
 3766: to select the correct hyphenation on LaTeX printouts.  This is undef if there
 3767: is no supported hyphenation for the language code.
 3768: 
 3769: =cut
 3770: 
 3771: sub latexlanguage {
 3772:     my $code = shift;
 3773:     return $latex_language{$code};
 3774: }
 3775: 
 3776: =pod
 3777: 
 3778: =item * &latexhyphenation()
 3779: 
 3780: Same as above but what's supplied is the language as it might be stored
 3781: in the metadata.
 3782: 
 3783: =cut
 3784: 
 3785: sub latexhyphenation {
 3786:     my $key = shift;
 3787:     return $latex_language_bykey{$key};
 3788: }
 3789: 
 3790: =pod
 3791: 
 3792: =item * &copyrightids() 
 3793: 
 3794: returns list of all copyrights
 3795: 
 3796: =cut
 3797: 
 3798: sub copyrightids {
 3799:     return sort(keys(%cprtag));
 3800: }
 3801: 
 3802: =pod
 3803: 
 3804: =item * &copyrightdescription() 
 3805: 
 3806: returns description of a specified copyright id
 3807: 
 3808: =cut
 3809: 
 3810: sub copyrightdescription {
 3811:     return &mt($cprtag{shift(@_)});
 3812: }
 3813: 
 3814: =pod
 3815: 
 3816: =item * &source_copyrightids() 
 3817: 
 3818: returns list of all source copyrights
 3819: 
 3820: =cut
 3821: 
 3822: sub source_copyrightids {
 3823:     return sort(keys(%scprtag));
 3824: }
 3825: 
 3826: =pod
 3827: 
 3828: =item * &source_copyrightdescription() 
 3829: 
 3830: returns description of a specified source copyright id
 3831: 
 3832: =cut
 3833: 
 3834: sub source_copyrightdescription {
 3835:     return &mt($scprtag{shift(@_)});
 3836: }
 3837: 
 3838: =pod
 3839: 
 3840: =item * &filecategories() 
 3841: 
 3842: returns list of all file categories
 3843: 
 3844: =cut
 3845: 
 3846: sub filecategories {
 3847:     return sort(keys(%category_extensions));
 3848: }
 3849: 
 3850: =pod
 3851: 
 3852: =item * &filecategorytypes() 
 3853: 
 3854: returns list of file types belonging to a given file
 3855: category
 3856: 
 3857: =cut
 3858: 
 3859: sub filecategorytypes {
 3860:     my ($cat) = @_;
 3861:     return @{$category_extensions{lc($cat)}};
 3862: }
 3863: 
 3864: =pod
 3865: 
 3866: =item * &fileembstyle() 
 3867: 
 3868: returns embedding style for a specified file type
 3869: 
 3870: =cut
 3871: 
 3872: sub fileembstyle {
 3873:     return $fe{lc(shift(@_))};
 3874: }
 3875: 
 3876: sub filemimetype {
 3877:     return $fm{lc(shift(@_))};
 3878: }
 3879: 
 3880: 
 3881: sub filecategoryselect {
 3882:     my ($name,$value)=@_;
 3883:     return &select_form($value,$name,
 3884:                         {'' => &mt('Any category'), map { $_,$_ } sort(keys(%category_extensions))});
 3885: }
 3886: 
 3887: =pod
 3888: 
 3889: =item * &filedescription() 
 3890: 
 3891: returns description for a specified file type
 3892: 
 3893: =cut
 3894: 
 3895: sub filedescription {
 3896:     my $file_description = $fd{lc(shift())};
 3897:     $file_description =~ s:([\[\]]):~$1:g;
 3898:     return &mt($file_description);
 3899: }
 3900: 
 3901: =pod
 3902: 
 3903: =item * &filedescriptionex() 
 3904: 
 3905: returns description for a specified file type with
 3906: extra formatting
 3907: 
 3908: =cut
 3909: 
 3910: sub filedescriptionex {
 3911:     my $ex=shift;
 3912:     my $file_description = $fd{lc($ex)};
 3913:     $file_description =~ s:([\[\]]):~$1:g;
 3914:     return '.'.$ex.' '.&mt($file_description);
 3915: }
 3916: 
 3917: # End of .tab access
 3918: =pod
 3919: 
 3920: =back
 3921: 
 3922: =cut
 3923: 
 3924: # ------------------------------------------------------------------ File Types
 3925: sub fileextensions {
 3926:     return sort(keys(%fe));
 3927: }
 3928: 
 3929: # ----------------------------------------------------------- Display Languages
 3930: # returns a hash with all desired display languages
 3931: #
 3932: 
 3933: sub display_languages {
 3934:     my %languages=();
 3935:     foreach my $lang (&Apache::lonlocal::preferred_languages()) {
 3936: 	$languages{$lang}=1;
 3937:     }
 3938:     &get_unprocessed_cgi($ENV{'QUERY_STRING'},['displaylanguage']);
 3939:     if ($env{'form.displaylanguage'}) {
 3940: 	foreach my $lang (split(/\s*(\,|\;|\:)\s*/,$env{'form.displaylanguage'})) {
 3941: 	    $languages{$lang}=1;
 3942:         }
 3943:     }
 3944:     return %languages;
 3945: }
 3946: 
 3947: sub languages {
 3948:     my ($possible_langs) = @_;
 3949:     my @preferred_langs = &Apache::lonlocal::preferred_languages();
 3950:     if (!ref($possible_langs)) {
 3951: 	if( wantarray ) {
 3952: 	    return @preferred_langs;
 3953: 	} else {
 3954: 	    return $preferred_langs[0];
 3955: 	}
 3956:     }
 3957:     my %possibilities = map { $_ => 1 } (@$possible_langs);
 3958:     my @preferred_possibilities;
 3959:     foreach my $preferred_lang (@preferred_langs) {
 3960: 	if (exists($possibilities{$preferred_lang})) {
 3961: 	    push(@preferred_possibilities, $preferred_lang);
 3962: 	}
 3963:     }
 3964:     if( wantarray ) {
 3965: 	return @preferred_possibilities;
 3966:     }
 3967:     return $preferred_possibilities[0];
 3968: }
 3969: 
 3970: sub user_lang {
 3971:     my ($touname,$toudom,$fromcid) = @_;
 3972:     my @userlangs;
 3973:     if (($fromcid ne '') && ($env{'course.'.$fromcid.'.languages'} ne '')) {
 3974:         @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
 3975:                     $env{'course.'.$fromcid.'.languages'}));
 3976:     } else {
 3977:         my %langhash = &getlangs($touname,$toudom);
 3978:         if ($langhash{'languages'} ne '') {
 3979:             @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
 3980:         } else {
 3981:             my %domdefs = &Apache::lonnet::get_domain_defaults($toudom);
 3982:             if ($domdefs{'lang_def'} ne '') {
 3983:                 @userlangs = ($domdefs{'lang_def'});
 3984:             }
 3985:         }
 3986:     }
 3987:     my @languages=&Apache::lonlocal::get_genlanguages(@userlangs);
 3988:     my $user_lh = Apache::localize->get_handle(@languages);
 3989:     return $user_lh;
 3990: }
 3991: 
 3992: 
 3993: ###############################################################
 3994: ##               Student Answer Attempts                     ##
 3995: ###############################################################
 3996: 
 3997: =pod
 3998: 
 3999: =head1 Alternate Problem Views
 4000: 
 4001: =over 4
 4002: 
 4003: =item * &get_previous_attempt($symb, $username, $domain, $course,
 4004:     $getattempt, $regexp, $gradesub, $usec, $identifier)
 4005: 
 4006: Return string with previous attempt on problem. Arguments:
 4007: 
 4008: =over 4
 4009: 
 4010: =item * $symb: Problem, including path
 4011: 
 4012: =item * $username: username of the desired student
 4013: 
 4014: =item * $domain: domain of the desired student
 4015: 
 4016: =item * $course: Course ID
 4017: 
 4018: =item * $getattempt: Leave blank for all attempts, otherwise put
 4019:     something
 4020: 
 4021: =item * $regexp: if string matches this regexp, the string will be
 4022:     sent to $gradesub
 4023: 
 4024: =item * $gradesub: routine that processes the string if it matches $regexp
 4025: 
 4026: =item * $usec: section of the desired student
 4027: 
 4028: =item * $identifier: counter for student (multiple students one problem) or 
 4029:     problem (one student; whole sequence).
 4030: 
 4031: =back
 4032: 
 4033: The output string is a table containing all desired attempts, if any.
 4034: 
 4035: =cut
 4036: 
 4037: sub get_previous_attempt {
 4038:   my ($symb,$username,$domain,$course,$getattempt,$regexp,$gradesub,$usec,$identifier)=@_;
 4039:   my $prevattempts='';
 4040:   no strict 'refs';
 4041:   if ($symb) {
 4042:     my (%returnhash)=
 4043:       &Apache::lonnet::restore($symb,$course,$domain,$username);
 4044:     if ($returnhash{'version'}) {
 4045:       my %lasthash=();
 4046:       my $version;
 4047:       for ($version=1;$version<=$returnhash{'version'};$version++) {
 4048:         foreach my $key (reverse(sort(split(/\:/,$returnhash{$version.':keys'})))) {
 4049:             if ($key =~ /\.rawrndseed$/) {
 4050:                 my ($id) = ($key =~ /^(.+)\.rawrndseed$/);
 4051:                 $lasthash{$id.'.rndseed'} = $returnhash{$version.':'.$key};
 4052:             } else {
 4053:                 $lasthash{$key}=$returnhash{$version.':'.$key};
 4054:             }
 4055:         }
 4056:       }
 4057:       $prevattempts=&start_data_table().&start_data_table_header_row();
 4058:       $prevattempts.='<th>'.&mt('History').'</th>';
 4059:       my (%typeparts,%lasthidden,%regraded,%hidestatus);
 4060:       my $showsurv=&Apache::lonnet::allowed('vas',$env{'request.course.id'});
 4061:       foreach my $key (sort(keys(%lasthash))) {
 4062: 	my ($ign,@parts) = split(/\./,$key);
 4063: 	if ($#parts > 0) {
 4064: 	  my $data=$parts[-1];
 4065:           next if ($data eq 'foilorder');
 4066: 	  pop(@parts);
 4067:           $prevattempts.='<th>'.&mt('Part ').join('.',@parts).'<br />'.$data.'&nbsp;</th>';
 4068:           if ($data eq 'type') {
 4069:               unless ($showsurv) {
 4070:                   my $id = join(',',@parts);
 4071:                   $typeparts{$ign.'.'.$id} = $lasthash{$key};
 4072:                   if (($lasthash{$key} eq 'anonsurvey') || ($lasthash{$key} eq 'anonsurveycred')) {
 4073:                       $lasthidden{$ign.'.'.$id} = 1;
 4074:                   }
 4075:               }
 4076:               if ($identifier ne '') {
 4077:                   my $id = join(',',@parts);
 4078:                   if (&Apache::lonnet::EXT("resource.$id.problemstatus",$symb,
 4079:                                                $domain,$username,$usec,undef,$course) =~ /^no/) {
 4080:                       $hidestatus{$ign.'.'.$id} = 1;
 4081:                   }
 4082:               }
 4083:           } elsif ($data eq 'regrader') {
 4084:               if (($identifier ne '') && (@parts)) {
 4085:                   my $id = join(',',@parts);
 4086:                   $regraded{$ign.'.'.$id} = 1;
 4087:               }
 4088:           } 
 4089: 	} else {
 4090: 	  if ($#parts == 0) {
 4091: 	    $prevattempts.='<th>'.$parts[0].'</th>';
 4092: 	  } else {
 4093: 	    $prevattempts.='<th>'.$ign.'</th>';
 4094: 	  }
 4095: 	}
 4096:       }
 4097:       $prevattempts.=&end_data_table_header_row();
 4098:       if ($getattempt eq '') {
 4099:         my (%solved,%resets,%probstatus);
 4100:         if (($identifier ne '') && (keys(%regraded) > 0)) {
 4101:             for ($version=1;$version<=$returnhash{'version'};$version++) {
 4102:                 foreach my $id (keys(%regraded)) {
 4103:                     if (($returnhash{$version.':'.$id.'.regrader'}) &&
 4104:                         ($returnhash{$version.':'.$id.'.tries'} eq '') &&
 4105:                         ($returnhash{$version.':'.$id.'.award'} eq '')) {
 4106:                         push(@{$resets{$id}},$version);
 4107:                     }
 4108:                 }
 4109:             }
 4110:         }
 4111: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
 4112:             my (@hidden,@unsolved);
 4113:             if (%typeparts) {
 4114:                 foreach my $id (keys(%typeparts)) {
 4115:                     if (($returnhash{$version.':'.$id.'.type'} eq 'anonsurvey') || 
 4116:                         ($returnhash{$version.':'.$id.'.type'} eq 'anonsurveycred')) {
 4117:                         push(@hidden,$id);
 4118:                     } elsif ($identifier ne '') {
 4119:                         unless (($returnhash{$version.':'.$id.'.type'} eq 'survey') ||
 4120:                                 ($returnhash{$version.':'.$id.'.type'} eq 'surveycred') ||
 4121:                                 ($hidestatus{$id})) {
 4122:                             next if ((ref($resets{$id}) eq 'ARRAY') && grep(/^\Q$version\E$/,@{$resets{$id}}));
 4123:                             if ($returnhash{$version.':'.$id.'.solved'} eq 'correct_by_student') {
 4124:                                 push(@{$solved{$id}},$version);
 4125:                             } elsif (($returnhash{$version.':'.$id.'.solved'} ne '') &&
 4126:                                      (ref($solved{$id}) eq 'ARRAY')) {
 4127:                                 my $skip;
 4128:                                 if (ref($resets{$id}) eq 'ARRAY') {
 4129:                                     foreach my $reset (@{$resets{$id}}) {
 4130:                                         if ($reset > $solved{$id}[-1]) {
 4131:                                             $skip=1;
 4132:                                             last;
 4133:                                         }
 4134:                                     }
 4135:                                 }
 4136:                                 unless ($skip) {
 4137:                                     my ($ign,$partslist) = split(/\./,$id,2);
 4138:                                     push(@unsolved,$partslist);
 4139:                                 }
 4140:                             }
 4141:                         }
 4142:                     }
 4143:                 }
 4144:             }
 4145:             $prevattempts.=&start_data_table_row().
 4146:                            '<td>'.&mt('Transaction [_1]',$version);
 4147:             if (@unsolved) {
 4148:                 $prevattempts .= '<span class="LC_nobreak"><label>'.
 4149:                                  '<input type="checkbox" name="HIDE'.$identifier.'" value="'.$version.':'.join('_',@unsolved).'" />'.
 4150:                                  &mt('Hide').'</label></span>';
 4151:             }
 4152:             $prevattempts .= '</td>';
 4153:             if (@hidden) {
 4154:                 foreach my $key (sort(keys(%lasthash))) {
 4155:                     next if ($key =~ /\.foilorder$/);
 4156:                     my $hide;
 4157:                     foreach my $id (@hidden) {
 4158:                         if ($key =~ /^\Q$id\E/) {
 4159:                             $hide = 1;
 4160:                             last;
 4161:                         }
 4162:                     }
 4163:                     if ($hide) {
 4164:                         my ($id,$data) = ($key =~ /^(.+)\.([^.]+)$/);
 4165:                         if (($data eq 'award') || ($data eq 'awarddetail')) {
 4166:                             my $value = &format_previous_attempt_value($key,
 4167:                                              $returnhash{$version.':'.$key});
 4168:                             $prevattempts.='<td>'.$value.'&nbsp;</td>';
 4169:                         } else {
 4170:                             $prevattempts.='<td>&nbsp;</td>';
 4171:                         }
 4172:                     } else {
 4173:                         if ($key =~ /\./) {
 4174:                             my $value = $returnhash{$version.':'.$key};
 4175:                             if ($key =~ /\.rndseed$/) {
 4176:                                 my ($id) = ($key =~ /^(.+)\.[^.]+$/);
 4177:                                 if (exists($returnhash{$version.':'.$id.'.rawrndseed'})) {
 4178:                                     $value = $returnhash{$version.':'.$id.'.rawrndseed'};
 4179:                                 }
 4180:                             }
 4181:                             $prevattempts.='<td>'.&format_previous_attempt_value($key,$value).
 4182:                                            '&nbsp;</td>';
 4183:                         } else {
 4184:                             $prevattempts.='<td>&nbsp;</td>';
 4185:                         }
 4186:                     }
 4187:                 }
 4188:             } else {
 4189: 	        foreach my $key (sort(keys(%lasthash))) {
 4190:                     next if ($key =~ /\.foilorder$/);
 4191:                     my $value = $returnhash{$version.':'.$key};
 4192:                     if ($key =~ /\.rndseed$/) {
 4193:                         my ($id) = ($key =~ /^(.+)\.[^.]+$/);
 4194:                         if (exists($returnhash{$version.':'.$id.'.rawrndseed'})) {
 4195:                             $value = $returnhash{$version.':'.$id.'.rawrndseed'};
 4196:                         }
 4197:                     }
 4198:                     $prevattempts.='<td>'.&format_previous_attempt_value($key,$value).
 4199:                                    '&nbsp;</td>';
 4200: 	        }
 4201:             }
 4202: 	    $prevattempts.=&end_data_table_row();
 4203: 	 }
 4204:       }
 4205:       my @currhidden = keys(%lasthidden);
 4206:       $prevattempts.=&start_data_table_row().'<td>'.&mt('Current').'</td>';
 4207:       foreach my $key (sort(keys(%lasthash))) {
 4208:           next if ($key =~ /\.foilorder$/);
 4209:           if (%typeparts) {
 4210:               my $hidden;
 4211:               foreach my $id (@currhidden) {
 4212:                   if ($key =~ /^\Q$id\E/) {
 4213:                       $hidden = 1;
 4214:                       last;
 4215:                   }
 4216:               }
 4217:               if ($hidden) {
 4218:                   my ($id,$data) = ($key =~ /^(.+)\.([^.]+)$/);
 4219:                   if (($data eq 'award') || ($data eq 'awarddetail')) {
 4220:                       my $value = &format_previous_attempt_value($key,$lasthash{$key});
 4221:                       if ($key =~/$regexp$/ && (defined &$gradesub)) {
 4222:                           $value = &$gradesub($value);
 4223:                       }
 4224:                       $prevattempts.='<td>'. $value.'&nbsp;</td>';
 4225:                   } else {
 4226:                       $prevattempts.='<td>&nbsp;</td>';
 4227:                   }
 4228:               } else {
 4229:                   my $value = &format_previous_attempt_value($key,$lasthash{$key});
 4230:                   if ($key =~/$regexp$/ && (defined &$gradesub)) {
 4231:                       $value = &$gradesub($value);
 4232:                   }
 4233:                   $prevattempts.='<td>'.$value.'&nbsp;</td>';
 4234:               }
 4235:           } else {
 4236: 	      my $value = &format_previous_attempt_value($key,$lasthash{$key});
 4237: 	      if ($key =~/$regexp$/ && (defined &$gradesub)) {
 4238:                   $value = &$gradesub($value);
 4239:               }
 4240: 	     $prevattempts.='<td>'.$value.'&nbsp;</td>';
 4241:           }
 4242:       }
 4243:       $prevattempts.= &end_data_table_row().&end_data_table();
 4244:     } else {
 4245:       $prevattempts=
 4246: 	  &start_data_table().&start_data_table_row().
 4247: 	  '<td>'.&mt('Nothing submitted - no attempts.').'</td>'.
 4248: 	  &end_data_table_row().&end_data_table();
 4249:     }
 4250:   } else {
 4251:     $prevattempts=
 4252: 	  &start_data_table().&start_data_table_row().
 4253: 	  '<td>'.&mt('No data.').'</td>'.
 4254: 	  &end_data_table_row().&end_data_table();
 4255:   }
 4256: }
 4257: 
 4258: sub format_previous_attempt_value {
 4259:     my ($key,$value) = @_;
 4260:     if (($key =~ /timestamp/) || ($key=~/duedate/)) {
 4261:         $value = &Apache::lonlocal::locallocaltime($value);
 4262:     } elsif (ref($value) eq 'ARRAY') {
 4263:         $value = &HTML::Entities::encode('('.join(', ', @{ $value }).')','"<>&');
 4264:     } elsif ($key =~ /answerstring$/) {
 4265:         my %answers = &Apache::lonnet::str2hash($value);
 4266:         my @answer = %answers;
 4267:         %answers = map {&HTML::Entities::encode($_, '"<>&')} @answer;
 4268:         my @anskeys = sort(keys(%answers));
 4269:         if (@anskeys == 1) {
 4270:             my $answer = $answers{$anskeys[0]};
 4271:             if ($answer =~ m{\0}) {
 4272:                 $answer =~ s{\0}{,}g;
 4273:             }
 4274:             my $tag_internal_answer_name = 'INTERNAL';
 4275:             if ($anskeys[0] eq $tag_internal_answer_name) {
 4276:                 $value = $answer; 
 4277:             } else {
 4278:                 $value = $anskeys[0].'='.$answer;
 4279:             }
 4280:         } else {
 4281:             foreach my $ans (@anskeys) {
 4282:                 my $answer = $answers{$ans};
 4283:                 if ($answer =~ m{\0}) {
 4284:                     $answer =~ s{\0}{,}g;
 4285:                 }
 4286:                 $value .=  $ans.'='.$answer.'<br />';;
 4287:             } 
 4288:         }
 4289:     } else {
 4290:         $value = &HTML::Entities::encode(&unescape($value), '"<>&');
 4291:     }
 4292:     return $value;
 4293: }
 4294: 
 4295: 
 4296: sub relative_to_absolute {
 4297:     my ($url,$output)=@_;
 4298:     my $parser=HTML::TokeParser->new(\$output);
 4299:     my $token;
 4300:     my $thisdir=$url;
 4301:     my @rlinks=();
 4302:     while ($token=$parser->get_token) {
 4303: 	if ($token->[0] eq 'S') {
 4304: 	    if ($token->[1] eq 'a') {
 4305: 		if ($token->[2]->{'href'}) {
 4306: 		    $rlinks[$#rlinks+1]=$token->[2]->{'href'};
 4307: 		}
 4308: 	    } elsif ($token->[1] eq 'img' || $token->[1] eq 'embed' ) {
 4309: 		$rlinks[$#rlinks+1]=$token->[2]->{'src'};
 4310: 	    } elsif ($token->[1] eq 'base') {
 4311: 		$thisdir=$token->[2]->{'href'};
 4312: 	    }
 4313: 	}
 4314:     }
 4315:     $thisdir=~s-/[^/]*$--;
 4316:     foreach my $link (@rlinks) {
 4317: 	unless (($link=~/^https?\:\/\//i) ||
 4318: 		($link=~/^\//) ||
 4319: 		($link=~/^javascript:/i) ||
 4320: 		($link=~/^mailto:/i) ||
 4321: 		($link=~/^\#/)) {
 4322: 	    my $newlocation=&Apache::lonnet::hreflocation($thisdir,$link);
 4323: 	    $output=~s/(\"|\'|\=\s*)\Q$link\E(\"|\'|\s|\>)/$1$newlocation$2/;
 4324: 	}
 4325:     }
 4326: # -------------------------------------------------- Deal with Applet codebases
 4327:     $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
 4328:     return $output;
 4329: }
 4330: 
 4331: =pod
 4332: 
 4333: =item * &get_student_view()
 4334: 
 4335: show a snapshot of what student was looking at
 4336: 
 4337: =cut
 4338: 
 4339: sub get_student_view {
 4340:   my ($symb,$username,$domain,$courseid,$target,$moreenv) = @_;
 4341:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
 4342:   my (%form);
 4343:   my @elements=('symb','courseid','domain','username');
 4344:   foreach my $element (@elements) {
 4345:       $form{'grade_'.$element}=eval '$'.$element #'
 4346:   }
 4347:   if (defined($moreenv)) {
 4348:       %form=(%form,%{$moreenv});
 4349:   }
 4350:   if (defined($target)) { $form{'grade_target'} = $target; }
 4351:   $feedurl=&Apache::lonnet::clutter($feedurl);
 4352:   my ($userview,$response)=&Apache::lonnet::ssi_body($feedurl,%form);
 4353:   $userview=~s/\<body[^\>]*\>//gi;
 4354:   $userview=~s/\<\/body\>//gi;
 4355:   $userview=~s/\<html\>//gi;
 4356:   $userview=~s/\<\/html\>//gi;
 4357:   $userview=~s/\<head\>//gi;
 4358:   $userview=~s/\<\/head\>//gi;
 4359:   $userview=~s/action\s*\=/would_be_action\=/gi;
 4360:   $userview=&relative_to_absolute($feedurl,$userview);
 4361:   if (wantarray) {
 4362:      return ($userview,$response);
 4363:   } else {
 4364:      return $userview;
 4365:   }
 4366: }
 4367: 
 4368: sub get_student_view_with_retries {
 4369:   my ($symb,$retries,$username,$domain,$courseid,$target,$moreenv) = @_;
 4370: 
 4371:     my $ok = 0;                 # True if we got a good response.
 4372:     my $content;
 4373:     my $response;
 4374: 
 4375:     # Try to get the student_view done. within the retries count:
 4376:     
 4377:     do {
 4378:          ($content, $response) = &get_student_view($symb,$username,$domain,$courseid,$target,$moreenv);
 4379:          $ok      = $response->is_success;
 4380:          if (!$ok) {
 4381:             &Apache::lonnet::logthis("Failed get_student_view_with_retries on $symb: ".$response->is_success.', '.$response->code.', '.$response->message);
 4382:          }
 4383:          $retries--;
 4384:     } while (!$ok && ($retries > 0));
 4385:     
 4386:     if (!$ok) {
 4387:        $content = '';          # On error return an empty content.
 4388:     }
 4389:     if (wantarray) {
 4390:        return ($content, $response);
 4391:     } else {
 4392:        return $content;
 4393:     }
 4394: }
 4395: 
 4396: =pod
 4397: 
 4398: =item * &get_student_answers() 
 4399: 
 4400: show a snapshot of how student was answering problem
 4401: 
 4402: =cut
 4403: 
 4404: sub get_student_answers {
 4405:   my ($symb,$username,$domain,$courseid,%form) = @_;
 4406:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
 4407:   my (%moreenv);
 4408:   my @elements=('symb','courseid','domain','username');
 4409:   foreach my $element (@elements) {
 4410:     $moreenv{'grade_'.$element}=eval '$'.$element #'
 4411:   }
 4412:   $moreenv{'grade_target'}='answer';
 4413:   %moreenv=(%form,%moreenv);
 4414:   $feedurl = &Apache::lonnet::clutter($feedurl);
 4415:   my $userview=&Apache::lonnet::ssi($feedurl,%moreenv);
 4416:   return $userview;
 4417: }
 4418: 
 4419: =pod
 4420: 
 4421: =item * &submlink()
 4422: 
 4423: Inputs: $text $uname $udom $symb $target
 4424: 
 4425: Returns: A link to grades.pm such as to see the SUBM view of a student
 4426: 
 4427: =cut
 4428: 
 4429: ###############################################
 4430: sub submlink {
 4431:     my ($text,$uname,$udom,$symb,$target)=@_;
 4432:     if (!($uname && $udom)) {
 4433: 	(my $cursymb, my $courseid,$udom,$uname)=
 4434: 	    &Apache::lonnet::whichuser($symb);
 4435: 	if (!$symb) { $symb=$cursymb; }
 4436:     }
 4437:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
 4438:     $symb=&escape($symb);
 4439:     if ($target) { $target=" target=\"$target\""; }
 4440:     return
 4441:         '<a href="/adm/grades?command=submission'.
 4442:         '&amp;symb='.$symb.
 4443:         '&amp;student='.$uname.
 4444:         '&amp;userdom='.$udom.'"'.
 4445:         $target.'>'.$text.'</a>';
 4446: }
 4447: ##############################################
 4448: 
 4449: =pod
 4450: 
 4451: =item * &pgrdlink()
 4452: 
 4453: Inputs: $text $uname $udom $symb $target
 4454: 
 4455: Returns: A link to grades.pm such as to see the PGRD view of a student
 4456: 
 4457: =cut
 4458: 
 4459: ###############################################
 4460: sub pgrdlink {
 4461:     my $link=&submlink(@_);
 4462:     $link=~s/(&command=submission)/$1&showgrading=yes/;
 4463:     return $link;
 4464: }
 4465: ##############################################
 4466: 
 4467: =pod
 4468: 
 4469: =item * &pprmlink()
 4470: 
 4471: Inputs: $text $uname $udom $symb $target
 4472: 
 4473: Returns: A link to parmset.pm such as to see the PPRM view of a
 4474: student and a specific resource
 4475: 
 4476: =cut
 4477: 
 4478: ###############################################
 4479: sub pprmlink {
 4480:     my ($text,$uname,$udom,$symb,$target)=@_;
 4481:     if (!($uname && $udom)) {
 4482: 	(my $cursymb, my $courseid,$udom,$uname)=
 4483: 	    &Apache::lonnet::whichuser($symb);
 4484: 	if (!$symb) { $symb=$cursymb; }
 4485:     }
 4486:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
 4487:     $symb=&escape($symb);
 4488:     if ($target) { $target="target=\"$target\""; }
 4489:     return '<a href="/adm/parmset?command=set&amp;'.
 4490: 	'symb='.$symb.'&amp;uname='.$uname.
 4491: 	'&amp;udom='.$udom.'" '.$target.'>'.$text.'</a>';
 4492: }
 4493: ##############################################
 4494: 
 4495: =pod
 4496: 
 4497: =back
 4498: 
 4499: =cut
 4500: 
 4501: ###############################################
 4502: 
 4503: 
 4504: sub timehash {
 4505:     my ($thistime) = @_;
 4506:     my $timezone = &Apache::lonlocal::gettimezone();
 4507:     my $dt = DateTime->from_epoch(epoch => $thistime)
 4508:                      ->set_time_zone($timezone);
 4509:     my $wday = $dt->day_of_week();
 4510:     if ($wday == 7) { $wday = 0; }
 4511:     return ( 'second' => $dt->second(),
 4512:              'minute' => $dt->minute(),
 4513:              'hour'   => $dt->hour(),
 4514:              'day'     => $dt->day_of_month(),
 4515:              'month'   => $dt->month(),
 4516:              'year'    => $dt->year(),
 4517:              'weekday' => $wday,
 4518:              'dayyear' => $dt->day_of_year(),
 4519:              'dlsav'   => $dt->is_dst() );
 4520: }
 4521: 
 4522: sub utc_string {
 4523:     my ($date)=@_;
 4524:     return strftime("%Y%m%dT%H%M%SZ",gmtime($date));
 4525: }
 4526: 
 4527: sub maketime {
 4528:     my %th=@_;
 4529:     my ($epoch_time,$timezone,$dt);
 4530:     $timezone = &Apache::lonlocal::gettimezone();
 4531:     eval {
 4532:         $dt = DateTime->new( year   => $th{'year'},
 4533:                              month  => $th{'month'},
 4534:                              day    => $th{'day'},
 4535:                              hour   => $th{'hour'},
 4536:                              minute => $th{'minute'},
 4537:                              second => $th{'second'},
 4538:                              time_zone => $timezone,
 4539:                          );
 4540:     };
 4541:     if (!$@) {
 4542:         $epoch_time = $dt->epoch;
 4543:         if ($epoch_time) {
 4544:             return $epoch_time;
 4545:         }
 4546:     }
 4547:     return POSIX::mktime(
 4548:         ($th{'seconds'},$th{'minutes'},$th{'hours'},
 4549:          $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,-1));
 4550: }
 4551: 
 4552: #########################################
 4553: 
 4554: sub findallcourses {
 4555:     my ($roles,$uname,$udom) = @_;
 4556:     my %roles;
 4557:     if (ref($roles)) { %roles = map { $_ => 1 } @{$roles}; }
 4558:     my %courses;
 4559:     my $now=time;
 4560:     if (!defined($uname)) {
 4561:         $uname = $env{'user.name'};
 4562:     }
 4563:     if (!defined($udom)) {
 4564:         $udom = $env{'user.domain'};
 4565:     }
 4566:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
 4567:         my %roleshash = &Apache::lonnet::dump('roles',$udom,$uname);
 4568:         if (!%roles) {
 4569:             %roles = (
 4570:                        cc => 1,
 4571:                        co => 1,
 4572:                        in => 1,
 4573:                        ep => 1,
 4574:                        ta => 1,
 4575:                        cr => 1,
 4576:                        st => 1,
 4577:              );
 4578:         }
 4579:         foreach my $entry (keys(%roleshash)) {
 4580:             my ($trole,$tend,$tstart) = split(/_/,$roleshash{$entry});
 4581:             if ($trole =~ /^cr/) { 
 4582:                 next if (!exists($roles{$trole}) && !exists($roles{'cr'}));
 4583:             } else {
 4584:                 next if (!exists($roles{$trole}));
 4585:             }
 4586:             if ($tend) {
 4587:                 next if ($tend < $now);
 4588:             }
 4589:             if ($tstart) {
 4590:                 next if ($tstart > $now);
 4591:             }
 4592:             my ($cdom,$cnum,$sec,$cnumpart,$secpart,$role);
 4593:             (undef,$cdom,$cnumpart,$secpart) = split(/\//,$entry);
 4594:             my $value = $trole.'/'.$cdom.'/';
 4595:             if ($secpart eq '') {
 4596:                 ($cnum,$role) = split(/_/,$cnumpart); 
 4597:                 $sec = 'none';
 4598:                 $value .= $cnum.'/';
 4599:             } else {
 4600:                 $cnum = $cnumpart;
 4601:                 ($sec,$role) = split(/_/,$secpart);
 4602:                 $value .= $cnum.'/'.$sec;
 4603:             }
 4604:             if (ref($courses{$cdom.'_'.$cnum}{$sec}) eq 'ARRAY') {
 4605:                 unless (grep(/^\Q$value\E$/,@{$courses{$cdom.'_'.$cnum}{$sec}})) {
 4606:                     push(@{$courses{$cdom.'_'.$cnum}{$sec}},$value);
 4607:                 }
 4608:             } else {
 4609:                 @{$courses{$cdom.'_'.$cnum}{$sec}} = ($value);
 4610:             }
 4611:         }
 4612:     } else {
 4613:         foreach my $key (keys(%env)) {
 4614: 	    if ( $key=~m{^user\.role\.(\w+)\./($match_domain)/($match_courseid)/?(\w*)$} ||
 4615:                  $key=~m{^user\.role\.(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_courseid)/?(\w*)$}) {
 4616: 	        my ($role,$cdom,$cnum,$sec) = ($1,$2,$3,$4);
 4617: 	        next if ($role eq 'ca' || $role eq 'aa');
 4618: 	        next if (%roles && !exists($roles{$role}));
 4619: 	        my ($starttime,$endtime)=split(/\./,$env{$key});
 4620:                 my $active=1;
 4621:                 if ($starttime) {
 4622: 		    if ($now<$starttime) { $active=0; }
 4623:                 }
 4624:                 if ($endtime) {
 4625:                     if ($now>$endtime) { $active=0; }
 4626:                 }
 4627:                 if ($active) {
 4628:                     my $value = $role.'/'.$cdom.'/'.$cnum.'/';
 4629:                     if ($sec eq '') {
 4630:                         $sec = 'none';
 4631:                     } else {
 4632:                         $value .= $sec;
 4633:                     }
 4634:                     if (ref($courses{$cdom.'_'.$cnum}{$sec}) eq 'ARRAY') {
 4635:                         unless (grep(/^\Q$value\E$/,@{$courses{$cdom.'_'.$cnum}{$sec}})) {
 4636:                             push(@{$courses{$cdom.'_'.$cnum}{$sec}},$value);
 4637:                         }
 4638:                     } else {
 4639:                         @{$courses{$cdom.'_'.$cnum}{$sec}} = ($value);
 4640:                     }
 4641:                 }
 4642:             }
 4643:         }
 4644:     }
 4645:     return %courses;
 4646: }
 4647: 
 4648: ###############################################
 4649: 
 4650: sub blockcheck {
 4651:     my ($setters,$activity,$uname,$udom,$url,$is_course) = @_;
 4652: 
 4653:     if (defined($udom) && defined($uname)) {
 4654:         # If uname and udom are for a course, check for blocks in the course.
 4655:         if (($is_course) || (&Apache::lonnet::is_course($udom,$uname))) {
 4656:             my ($startblock,$endblock,$triggerblock) =
 4657:                 &get_blocks($setters,$activity,$udom,$uname,$url);
 4658:             return ($startblock,$endblock,$triggerblock);
 4659:         }
 4660:     } else {
 4661:         $udom = $env{'user.domain'};
 4662:         $uname = $env{'user.name'};
 4663:     }
 4664: 
 4665:     my $startblock = 0;
 4666:     my $endblock = 0;
 4667:     my $triggerblock = '';
 4668:     my %live_courses = &findallcourses(undef,$uname,$udom);
 4669: 
 4670:     # If uname is for a user, and activity is course-specific, i.e.,
 4671:     # boards, chat or groups, check for blocking in current course only.
 4672: 
 4673:     if (($activity eq 'boards' || $activity eq 'chat' ||
 4674:          $activity eq 'groups' || $activity eq 'printout') &&
 4675:         ($env{'request.course.id'})) {
 4676:         foreach my $key (keys(%live_courses)) {
 4677:             if ($key ne $env{'request.course.id'}) {
 4678:                 delete($live_courses{$key});
 4679:             }
 4680:         }
 4681:     }
 4682: 
 4683:     my $otheruser = 0;
 4684:     my %own_courses;
 4685:     if ((($uname ne $env{'user.name'})) || ($udom ne $env{'user.domain'})) {
 4686:         # Resource belongs to user other than current user.
 4687:         $otheruser = 1;
 4688:         # Gather courses for current user
 4689:         %own_courses = 
 4690:             &findallcourses(undef,$env{'user.name'},$env{'user.domain'});
 4691:     }
 4692: 
 4693:     # Gather active course roles - course coordinator, instructor, 
 4694:     # exam proctor, ta, student, or custom role.
 4695: 
 4696:     foreach my $course (keys(%live_courses)) {
 4697:         my ($cdom,$cnum);
 4698:         if ((defined($env{'course.'.$course.'.domain'})) && (defined($env{'course.'.$course.'.num'}))) {
 4699:             $cdom = $env{'course.'.$course.'.domain'};
 4700:             $cnum = $env{'course.'.$course.'.num'};
 4701:         } else {
 4702:             ($cdom,$cnum) = split(/_/,$course); 
 4703:         }
 4704:         my $no_ownblock = 0;
 4705:         my $no_userblock = 0;
 4706:         if ($otheruser && $activity ne 'com') {
 4707:             # Check if current user has 'evb' priv for this
 4708:             if (defined($own_courses{$course})) {
 4709:                 foreach my $sec (keys(%{$own_courses{$course}})) {
 4710:                     my $checkrole = 'cm./'.$cdom.'/'.$cnum;
 4711:                     if ($sec ne 'none') {
 4712:                         $checkrole .= '/'.$sec;
 4713:                     }
 4714:                     if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
 4715:                         $no_ownblock = 1;
 4716:                         last;
 4717:                     }
 4718:                 }
 4719:             }
 4720:             # if they have 'evb' priv and are currently not playing student
 4721:             next if (($no_ownblock) &&
 4722:                  ($env{'request.role'} !~ m{^st\./$cdom/$cnum}));
 4723:         }
 4724:         foreach my $sec (keys(%{$live_courses{$course}})) {
 4725:             my $checkrole = 'cm./'.$cdom.'/'.$cnum;
 4726:             if ($sec ne 'none') {
 4727:                 $checkrole .= '/'.$sec;
 4728:             }
 4729:             if ($otheruser) {
 4730:                 # Resource belongs to user other than current user.
 4731:                 # Assemble privs for that user, and check for 'evb' priv.
 4732:                 my (%allroles,%userroles);
 4733:                 if (ref($live_courses{$course}{$sec}) eq 'ARRAY') {
 4734:                     foreach my $entry (@{$live_courses{$course}{$sec}}) { 
 4735:                         my ($trole,$tdom,$tnum,$tsec);
 4736:                         if ($entry =~ /^cr/) {
 4737:                             ($trole,$tdom,$tnum,$tsec) = 
 4738:                                 ($entry =~ m|^(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_username)/?(\w*)$|);
 4739:                         } else {
 4740:                            ($trole,$tdom,$tnum,$tsec) = split(/\//,$entry);
 4741:                         }
 4742:                         my ($spec,$area,$trest);
 4743:                         $area = '/'.$tdom.'/'.$tnum;
 4744:                         $trest = $tnum;
 4745:                         if ($tsec ne '') {
 4746:                             $area .= '/'.$tsec;
 4747:                             $trest .= '/'.$tsec;
 4748:                         }
 4749:                         $spec = $trole.'.'.$area;
 4750:                         if ($trole =~ /^cr/) {
 4751:                             &Apache::lonnet::custom_roleprivs(\%allroles,$trole,
 4752:                                                               $tdom,$spec,$trest,$area);
 4753:                         } else {
 4754:                             &Apache::lonnet::standard_roleprivs(\%allroles,$trole,
 4755:                                                                 $tdom,$spec,$trest,$area);
 4756:                         }
 4757:                     }
 4758:                     my ($author,$adv) = &Apache::lonnet::set_userprivs(\%userroles,\%allroles);
 4759:                     if ($userroles{'user.priv.'.$checkrole} =~ /evb\&([^\:]*)/) {
 4760:                         if ($1) {
 4761:                             $no_userblock = 1;
 4762:                             last;
 4763:                         }
 4764:                     }
 4765:                 }
 4766:             } else {
 4767:                 # Resource belongs to current user
 4768:                 # Check for 'evb' priv via lonnet::allowed().
 4769:                 if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
 4770:                     $no_ownblock = 1;
 4771:                     last;
 4772:                 }
 4773:             }
 4774:         }
 4775:         # if they have the evb priv and are currently not playing student
 4776:         next if (($no_ownblock) &&
 4777:                  ($env{'request.role'} !~ m{^st\./\Q$cdom\E/\Q$cnum\E}));
 4778:         next if ($no_userblock);
 4779: 
 4780:         # Retrieve blocking times and identity of locker for course
 4781:         # of specified user, unless user has 'evb' privilege.
 4782:         
 4783:         my ($start,$end,$trigger) = 
 4784:             &get_blocks($setters,$activity,$cdom,$cnum,$url);
 4785:         if (($start != 0) && 
 4786:             (($startblock == 0) || ($startblock > $start))) {
 4787:             $startblock = $start;
 4788:             if ($trigger ne '') {
 4789:                 $triggerblock = $trigger;
 4790:             }
 4791:         }
 4792:         if (($end != 0)  &&
 4793:             (($endblock == 0) || ($endblock < $end))) {
 4794:             $endblock = $end;
 4795:             if ($trigger ne '') {
 4796:                 $triggerblock = $trigger;
 4797:             }
 4798:         }
 4799:     }
 4800:     return ($startblock,$endblock,$triggerblock);
 4801: }
 4802: 
 4803: sub get_blocks {
 4804:     my ($setters,$activity,$cdom,$cnum,$url) = @_;
 4805:     my $startblock = 0;
 4806:     my $endblock = 0;
 4807:     my $triggerblock = '';
 4808:     my $course = $cdom.'_'.$cnum;
 4809:     $setters->{$course} = {};
 4810:     $setters->{$course}{'staff'} = [];
 4811:     $setters->{$course}{'times'} = [];
 4812:     $setters->{$course}{'triggers'} = [];
 4813:     my (@blockers,%triggered);
 4814:     my $now = time;
 4815:     my %commblocks = &Apache::lonnet::get_comm_blocks($cdom,$cnum);
 4816:     if ($activity eq 'docs') {
 4817:         @blockers = &Apache::lonnet::has_comm_blocking('bre',undef,$url,\%commblocks);
 4818:         foreach my $block (@blockers) {
 4819:             if ($block =~ /^firstaccess____(.+)$/) {
 4820:                 my $item = $1;
 4821:                 my $type = 'map';
 4822:                 my $timersymb = $item;
 4823:                 if ($item eq 'course') {
 4824:                     $type = 'course';
 4825:                 } elsif ($item =~ /___\d+___/) {
 4826:                     $type = 'resource';
 4827:                 } else {
 4828:                     $timersymb = &Apache::lonnet::symbread($item);
 4829:                 }
 4830:                 my $start = $env{'course.'.$cdom.'_'.$cnum.'.firstaccess.'.$timersymb};
 4831:                 my $end = $start + $env{'course.'.$cdom.'_'.$cnum.'.timerinterval.'.$timersymb};
 4832:                 $triggered{$block} = {
 4833:                                        start => $start,
 4834:                                        end   => $end,
 4835:                                        type  => $type,
 4836:                                      };
 4837:             }
 4838:         }
 4839:     } else {
 4840:         foreach my $block (keys(%commblocks)) {
 4841:             if ($block =~ m/^(\d+)____(\d+)$/) { 
 4842:                 my ($start,$end) = ($1,$2);
 4843:                 if ($start <= time && $end >= time) {
 4844:                     if (ref($commblocks{$block}) eq 'HASH') {
 4845:                         if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 4846:                             if ($commblocks{$block}{'blocks'}{$activity} eq 'on') {
 4847:                                 unless(grep(/^\Q$block\E$/,@blockers)) {
 4848:                                     push(@blockers,$block);
 4849:                                 }
 4850:                             }
 4851:                         }
 4852:                     }
 4853:                 }
 4854:             } elsif ($block =~ /^firstaccess____(.+)$/) {
 4855:                 my $item = $1;
 4856:                 my $timersymb = $item; 
 4857:                 my $type = 'map';
 4858:                 if ($item eq 'course') {
 4859:                     $type = 'course';
 4860:                 } elsif ($item =~ /___\d+___/) {
 4861:                     $type = 'resource';
 4862:                 } else {
 4863:                     $timersymb = &Apache::lonnet::symbread($item);
 4864:                 }
 4865:                 my $start = $env{'course.'.$cdom.'_'.$cnum.'.firstaccess.'.$timersymb};
 4866:                 my $end = $start + $env{'course.'.$cdom.'_'.$cnum.'.timerinterval.'.$timersymb}; 
 4867:                 if ($start && $end) {
 4868:                     if (($start <= time) && ($end >= time)) {
 4869:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 4870:                             push(@blockers,$block);
 4871:                             $triggered{$block} = {
 4872:                                                    start => $start,
 4873:                                                    end   => $end,
 4874:                                                    type  => $type,
 4875:                                                  };
 4876:                         }
 4877:                     }
 4878:                 }
 4879:             }
 4880:         }
 4881:     }
 4882:     foreach my $blocker (@blockers) {
 4883:         my ($staff_name,$staff_dom,$title,$blocks) =
 4884:             &parse_block_record($commblocks{$blocker});
 4885:         push(@{$$setters{$course}{'staff'}},[$staff_name,$staff_dom]);
 4886:         my ($start,$end,$triggertype);
 4887:         if ($blocker =~ m/^(\d+)____(\d+)$/) {
 4888:             ($start,$end) = ($1,$2);
 4889:         } elsif (ref($triggered{$blocker}) eq 'HASH') {
 4890:             $start = $triggered{$blocker}{'start'};
 4891:             $end = $triggered{$blocker}{'end'};
 4892:             $triggertype = $triggered{$blocker}{'type'};
 4893:         }
 4894:         if ($start) {
 4895:             push(@{$$setters{$course}{'times'}}, [$start,$end]);
 4896:             if ($triggertype) {
 4897:                 push(@{$$setters{$course}{'triggers'}},$triggertype);
 4898:             } else {
 4899:                 push(@{$$setters{$course}{'triggers'}},0);
 4900:             }
 4901:             if ( ($startblock == 0) || ($startblock > $start) ) {
 4902:                 $startblock = $start;
 4903:                 if ($triggertype) {
 4904:                     $triggerblock = $blocker;
 4905:                 }
 4906:             }
 4907:             if ( ($endblock == 0) || ($endblock < $end) ) {
 4908:                $endblock = $end;
 4909:                if ($triggertype) {
 4910:                    $triggerblock = $blocker;
 4911:                }
 4912:             }
 4913:         }
 4914:     }
 4915:     return ($startblock,$endblock,$triggerblock);
 4916: }
 4917: 
 4918: sub parse_block_record {
 4919:     my ($record) = @_;
 4920:     my ($setuname,$setudom,$title,$blocks);
 4921:     if (ref($record) eq 'HASH') {
 4922:         ($setuname,$setudom) = split(/:/,$record->{'setter'});
 4923:         $title = &unescape($record->{'event'});
 4924:         $blocks = $record->{'blocks'};
 4925:     } else {
 4926:         my @data = split(/:/,$record,3);
 4927:         if (scalar(@data) eq 2) {
 4928:             $title = $data[1];
 4929:             ($setuname,$setudom) = split(/@/,$data[0]);
 4930:         } else {
 4931:             ($setuname,$setudom,$title) = @data;
 4932:         }
 4933:         $blocks = { 'com' => 'on' };
 4934:     }
 4935:     return ($setuname,$setudom,$title,$blocks);
 4936: }
 4937: 
 4938: sub blocking_status {
 4939:     my ($activity,$uname,$udom,$url,$is_course) = @_;
 4940:     my %setters;
 4941: 
 4942: # check for active blocking
 4943:     my ($startblock,$endblock,$triggerblock) = 
 4944:         &blockcheck(\%setters,$activity,$uname,$udom,$url,$is_course);
 4945:     my $blocked = 0;
 4946:     if ($startblock && $endblock) {
 4947:         $blocked = 1;
 4948:     }
 4949: 
 4950: # caller just wants to know whether a block is active
 4951:     if (!wantarray) { return $blocked; }
 4952: 
 4953: # build a link to a popup window containing the details
 4954:     my $querystring  = "?activity=$activity";
 4955: # $uname and $udom decide whose portfolio the user is trying to look at
 4956:     if ($activity eq 'port') {
 4957:         $querystring .= "&amp;udom=$udom"      if $udom;
 4958:         $querystring .= "&amp;uname=$uname"    if $uname;
 4959:     } elsif ($activity eq 'docs') {
 4960:         $querystring .= '&amp;url='.&HTML::Entities::encode($url,'&"');
 4961:     }
 4962: 
 4963:     my $output .= <<'END_MYBLOCK';
 4964: function openWindow(url, wdwName, w, h, toolbar,scrollbar) {
 4965:     var options = "width=" + w + ",height=" + h + ",";
 4966:     options += "resizable=yes,scrollbars="+scrollbar+",status=no,";
 4967:     options += "menubar=no,toolbar="+toolbar+",location=no,directories=no";
 4968:     var newWin = window.open(url, wdwName, options);
 4969:     newWin.focus();
 4970: }
 4971: END_MYBLOCK
 4972: 
 4973:     $output = Apache::lonhtmlcommon::scripttag($output);
 4974:   
 4975:     my $popupUrl = "/adm/blockingstatus/$querystring";
 4976:     my $text = &mt('Communication Blocked');
 4977:     my $class = 'LC_comblock';
 4978:     if ($activity eq 'docs') {
 4979:         $text = &mt('Content Access Blocked');
 4980:         $class = '';
 4981:     } elsif ($activity eq 'printout') {
 4982:         $text = &mt('Printing Blocked');
 4983:     }
 4984:     $output .= <<"END_BLOCK";
 4985: <div class='$class'>
 4986:   <a onclick='openWindow("$popupUrl","Blocking Table",600,300,"no","no");return false;' href='/adm/blockingstatus/$querystring'
 4987:   title='$text'>
 4988:   <img class='LC_noBorder LC_middle' title='$text' src='/res/adm/pages/comblock.png' alt='$text'/></a>
 4989:   <a onclick='openWindow("$popupUrl","Blocking Table",600,300,"no","no");return false;' href='/adm/blockingstatus/$querystring' 
 4990:   title='$text'>$text</a>
 4991: </div>
 4992: 
 4993: END_BLOCK
 4994: 
 4995:     return ($blocked, $output);
 4996: }
 4997: 
 4998: ###############################################
 4999: 
 5000: sub check_ip_acc {
 5001:     my ($acc,$clientip)=@_;
 5002:     &Apache::lonxml::debug("acc is $acc");
 5003:     if (!defined($acc) || $acc =~ /^\s*$/ || $acc =~/^\s*no\s*$/i) {
 5004:         return 1;
 5005:     }
 5006:     my $allowed;
 5007:     my $ip=$env{'request.host'} || $ENV{'REMOTE_ADDR'} || $clientip;
 5008: 
 5009:     my $name;
 5010:     my %access = (
 5011:                      allowfrom => 1,
 5012:                      denyfrom  => 0,
 5013:                  );
 5014:     my @allows;
 5015:     my @denies;
 5016:     foreach my $item (split(',',$acc)) {
 5017:         $item =~ s/^\s*//;
 5018:         $item =~ s/\s*$//;
 5019:         my $pattern;
 5020:         if ($item =~ /^\!(.+)$/) {
 5021:             push(@denies,$1);
 5022:         } else {
 5023:             push(@allows,$item);
 5024:         }
 5025:    }
 5026:    my $numdenies = scalar(@denies);
 5027:    my $numallows = scalar(@allows);
 5028:    my $count = 0;
 5029:    foreach my $pattern (@denies,@allows) {
 5030:         $count ++; 
 5031:         my $acctype = 'allowfrom';
 5032:         if ($count <= $numdenies) {
 5033:             $acctype = 'denyfrom';
 5034:         }
 5035:         if ($pattern =~ /\*$/) {
 5036:             #35.8.*
 5037:             $pattern=~s/\*//;
 5038:             if ($ip =~ /^\Q$pattern\E/) { $allowed=$access{$acctype}; }
 5039:         } elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {
 5040:             #35.8.3.[34-56]
 5041:             my $low=$2;
 5042:             my $high=$3;
 5043:             $pattern=$1;
 5044:             if ($ip =~ /^\Q$pattern\E/) {
 5045:                 my $last=(split(/\./,$ip))[3];
 5046:                 if ($last <=$high && $last >=$low) { $allowed=$access{$acctype}; }
 5047:             }
 5048:         } elsif ($pattern =~ /^\*/) {
 5049:             #*.msu.edu
 5050:             $pattern=~s/\*//;
 5051:             if (!defined($name)) {
 5052:                 use Socket;
 5053:                 my $netaddr=inet_aton($ip);
 5054:                 ($name)=gethostbyaddr($netaddr,AF_INET);
 5055:             }
 5056:             if ($name =~ /\Q$pattern\E$/i) { $allowed=$access{$acctype}; }
 5057:         } elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
 5058:             #127.0.0.1
 5059:             if ($ip =~ /^\Q$pattern\E/) { $allowed=$access{$acctype}; }
 5060:         } else {
 5061:             #some.name.com
 5062:             if (!defined($name)) {
 5063:                 use Socket;
 5064:                 my $netaddr=inet_aton($ip);
 5065:                 ($name)=gethostbyaddr($netaddr,AF_INET);
 5066:             }
 5067:             if ($name =~ /\Q$pattern\E$/i) { $allowed=$access{$acctype}; }
 5068:         }
 5069:         if ($allowed =~ /^(0|1)$/) { last; }
 5070:     }
 5071:     if ($allowed eq '') {
 5072:         if ($numdenies && !$numallows) {
 5073:             $allowed = 1;
 5074:         } else {
 5075:             $allowed = 0;
 5076:         }
 5077:     }
 5078:     return $allowed;
 5079: }
 5080: 
 5081: ###############################################
 5082: 
 5083: =pod
 5084: 
 5085: =head1 Domain Template Functions
 5086: 
 5087: =over 4
 5088: 
 5089: =item * &determinedomain()
 5090: 
 5091: Inputs: $domain (usually will be undef)
 5092: 
 5093: Returns: Determines which domain should be used for designs
 5094: 
 5095: =cut
 5096: 
 5097: ###############################################
 5098: sub determinedomain {
 5099:     my $domain=shift;
 5100:     if (! $domain) {
 5101:         # Determine domain if we have not been given one
 5102:         $domain = &Apache::lonnet::default_login_domain();
 5103:         if ($env{'user.domain'}) { $domain=$env{'user.domain'}; }
 5104:         if ($env{'request.role.domain'}) { 
 5105:             $domain=$env{'request.role.domain'}; 
 5106:         }
 5107:     }
 5108:     return $domain;
 5109: }
 5110: ###############################################
 5111: 
 5112: sub devalidate_domconfig_cache {
 5113:     my ($udom)=@_;
 5114:     &Apache::lonnet::devalidate_cache_new('domainconfig',$udom);
 5115: }
 5116: 
 5117: # ---------------------- Get domain configuration for a domain
 5118: sub get_domainconf {
 5119:     my ($udom) = @_;
 5120:     my $cachetime=1800;
 5121:     my ($result,$cached)=&Apache::lonnet::is_cached_new('domainconfig',$udom);
 5122:     if (defined($cached)) { return %{$result}; }
 5123: 
 5124:     my %domconfig = &Apache::lonnet::get_dom('configuration',
 5125: 					     ['login','rolecolors','autoenroll'],$udom);
 5126:     my (%designhash,%legacy);
 5127:     if (keys(%domconfig) > 0) {
 5128:         if (ref($domconfig{'login'}) eq 'HASH') {
 5129:             if (keys(%{$domconfig{'login'}})) {
 5130:                 foreach my $key (keys(%{$domconfig{'login'}})) {
 5131:                     if (ref($domconfig{'login'}{$key}) eq 'HASH') {
 5132:                         if (($key eq 'loginvia') || ($key eq 'headtag')) {
 5133:                             if (ref($domconfig{'login'}{$key}) eq 'HASH') {
 5134:                                 foreach my $hostname (keys(%{$domconfig{'login'}{$key}})) {
 5135:                                     if (ref($domconfig{'login'}{$key}{$hostname}) eq 'HASH') {
 5136:                                         if ($key eq 'loginvia') {
 5137:                                             if ($domconfig{'login'}{'loginvia'}{$hostname}{'server'}) {
 5138:                                                 my $server = $domconfig{'login'}{'loginvia'}{$hostname}{'server'};
 5139:                                                 $designhash{$udom.'.login.loginvia'} = $server;
 5140:                                                 if ($domconfig{'login'}{'loginvia'}{$hostname}{'serverpath'} eq 'custom') {
 5141: 
 5142:                                                     $designhash{$udom.'.login.loginvia_'.$hostname} = $server.':'.$domconfig{'login'}{'loginvia'}{$hostname}{'custompath'};
 5143:                                                 } else {
 5144:                                                     $designhash{$udom.'.login.loginvia_'.$hostname} = $server.':'.$domconfig{'login'}{'loginvia'}{$hostname}{'serverpath'};
 5145:                                                 }
 5146:                                             }
 5147:                                         } elsif ($key eq 'headtag') {
 5148:                                             if ($domconfig{'login'}{'headtag'}{$hostname}{'url'}) {
 5149:                                                 $designhash{$udom.'.login.headtag_'.$hostname} = $domconfig{'login'}{'headtag'}{$hostname}{'url'};
 5150:                                             }
 5151:                                         }
 5152:                                         if ($domconfig{'login'}{$key}{$hostname}{'exempt'}) {
 5153:                                             $designhash{$udom.'.login.'.$key.'_exempt_'.$hostname} = $domconfig{'login'}{$key}{$hostname}{'exempt'};
 5154:                                         }
 5155:                                     }
 5156:                                 }
 5157:                             }
 5158:                         } else {
 5159:                             foreach my $img (keys(%{$domconfig{'login'}{$key}})) {
 5160:                                 $designhash{$udom.'.login.'.$key.'_'.$img} = 
 5161:                                     $domconfig{'login'}{$key}{$img};
 5162:                             }
 5163:                         }
 5164:                     } else {
 5165:                         $designhash{$udom.'.login.'.$key}=$domconfig{'login'}{$key};
 5166:                     }
 5167:                 }
 5168:             } else {
 5169:                 $legacy{'login'} = 1;
 5170:             }
 5171:         } else {
 5172:             $legacy{'login'} = 1;
 5173:         }
 5174:         if (ref($domconfig{'rolecolors'}) eq 'HASH') {
 5175:             if (keys(%{$domconfig{'rolecolors'}})) {
 5176:                 foreach my $role (keys(%{$domconfig{'rolecolors'}})) {
 5177:                     if (ref($domconfig{'rolecolors'}{$role}) eq 'HASH') {
 5178:                         foreach my $item (keys(%{$domconfig{'rolecolors'}{$role}})) {
 5179:                             $designhash{$udom.'.'.$role.'.'.$item}=$domconfig{'rolecolors'}{$role}{$item};
 5180:                         }
 5181:                     }
 5182:                 }
 5183:             } else {
 5184:                 $legacy{'rolecolors'} = 1;
 5185:             }
 5186:         } else {
 5187:             $legacy{'rolecolors'} = 1;
 5188:         }
 5189:         if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 5190:             if ($domconfig{'autoenroll'}{'co-owners'}) {
 5191:                 $designhash{$udom.'.autoassign.co-owners'}=$domconfig{'autoenroll'}{'co-owners'};
 5192:             }
 5193:         }
 5194:         if (keys(%legacy) > 0) {
 5195:             my %legacyhash = &get_legacy_domconf($udom);
 5196:             foreach my $item (keys(%legacyhash)) {
 5197:                 if ($item =~ /^\Q$udom\E\.login/) {
 5198:                     if ($legacy{'login'}) { 
 5199:                         $designhash{$item} = $legacyhash{$item};
 5200:                     }
 5201:                 } else {
 5202:                     if ($legacy{'rolecolors'}) {
 5203:                         $designhash{$item} = $legacyhash{$item};
 5204:                     }
 5205:                 }
 5206:             }
 5207:         }
 5208:     } else {
 5209:         %designhash = &get_legacy_domconf($udom); 
 5210:     }
 5211:     &Apache::lonnet::do_cache_new('domainconfig',$udom,\%designhash,
 5212: 				  $cachetime);
 5213:     return %designhash;
 5214: }
 5215: 
 5216: sub get_legacy_domconf {
 5217:     my ($udom) = @_;
 5218:     my %legacyhash;
 5219:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
 5220:     my $designfile =  $designdir.'/'.$udom.'.tab';
 5221:     if (-e $designfile) {
 5222:         if ( open (my $fh,"<$designfile") ) {
 5223:             while (my $line = <$fh>) {
 5224:                 next if ($line =~ /^\#/);
 5225:                 chomp($line);
 5226:                 my ($key,$val)=(split(/\=/,$line));
 5227:                 if ($val) { $legacyhash{$udom.'.'.$key}=$val; }
 5228:             }
 5229:             close($fh);
 5230:         }
 5231:     }
 5232:     if (-e $Apache::lonnet::perlvar{'lonDocRoot'}.'/adm/lonDomLogos/'.$udom.'.gif') {
 5233:         $legacyhash{$udom.'.login.domlogo'} = "/adm/lonDomLogos/$udom.gif";
 5234:     }
 5235:     return %legacyhash;
 5236: }
 5237: 
 5238: =pod
 5239: 
 5240: =item * &domainlogo()
 5241: 
 5242: Inputs: $domain (usually will be undef)
 5243: 
 5244: Returns: A link to a domain logo, if the domain logo exists.
 5245: If the domain logo does not exist, a description of the domain.
 5246: 
 5247: =cut
 5248: 
 5249: ###############################################
 5250: sub domainlogo {
 5251:     my $domain = &determinedomain(shift);
 5252:     my %designhash = &get_domainconf($domain);    
 5253:     # See if there is a logo
 5254:     if ($designhash{$domain.'.login.domlogo'} ne '') {
 5255:         my $imgsrc = $designhash{$domain.'.login.domlogo'};
 5256:         if ($imgsrc =~ m{^/(adm|res)/}) {
 5257: 	    if ($imgsrc =~ m{^/res/}) {
 5258: 		my $local_name = &Apache::lonnet::filelocation('',$imgsrc);
 5259: 		&Apache::lonnet::repcopy($local_name);
 5260: 	    }
 5261: 	   $imgsrc = &lonhttpdurl($imgsrc);
 5262:         } 
 5263:         return '<img src="'.$imgsrc.'" alt="'.$domain.'" />';
 5264:     } elsif (defined(&Apache::lonnet::domain($domain,'description'))) {
 5265:         return &Apache::lonnet::domain($domain,'description');
 5266:     } else {
 5267:         return '';
 5268:     }
 5269: }
 5270: ##############################################
 5271: 
 5272: =pod
 5273: 
 5274: =item * &designparm()
 5275: 
 5276: Inputs: $which parameter; $domain (usually will be undef)
 5277: 
 5278: Returns: value of designparamter $which
 5279: 
 5280: =cut
 5281: 
 5282: 
 5283: ##############################################
 5284: sub designparm {
 5285:     my ($which,$domain)=@_;
 5286:     if (exists($env{'environment.color.'.$which})) {
 5287:         return $env{'environment.color.'.$which};
 5288:     }
 5289:     $domain=&determinedomain($domain);
 5290:     my %domdesign;
 5291:     unless ($domain eq 'public') {
 5292:         %domdesign = &get_domainconf($domain);
 5293:     }
 5294:     my $output;
 5295:     if ($domdesign{$domain.'.'.$which} ne '') {
 5296:         $output = $domdesign{$domain.'.'.$which};
 5297:     } else {
 5298:         $output = $defaultdesign{$which};
 5299:     }
 5300:     if (($which =~ /^(student|coordinator|author|admin)\.img$/) ||
 5301:         ($which =~ /login\.(img|logo|domlogo|login)/)) {
 5302:         if ($output =~ m{^/(adm|res)/}) {
 5303:             if ($output =~ m{^/res/}) {
 5304:                 my $local_name = &Apache::lonnet::filelocation('',$output);
 5305:                 &Apache::lonnet::repcopy($local_name);
 5306:             }
 5307:             $output = &lonhttpdurl($output);
 5308:         }
 5309:     }
 5310:     return $output;
 5311: }
 5312: 
 5313: ##############################################
 5314: =pod
 5315: 
 5316: =item * &authorspace()
 5317: 
 5318: Inputs: $url (usually will be undef).
 5319: 
 5320: Returns: Path to Authoring Space containing the resource or 
 5321:          directory being viewed (or for which action is being taken). 
 5322:          If $url is provided, and begins /priv/<domain>/<uname>
 5323:          the path will be that portion of the $context argument.
 5324:          Otherwise the path will be for the author space of the current
 5325:          user when the current role is author, or for that of the 
 5326:          co-author/assistant co-author space when the current role 
 5327:          is co-author or assistant co-author.
 5328: 
 5329: =cut
 5330: 
 5331: sub authorspace {
 5332:     my ($url) = @_;
 5333:     if ($url ne '') {
 5334:         if ($url =~ m{^(/priv/$match_domain/$match_username/)}) {
 5335:            return $1;
 5336:         }
 5337:     }
 5338:     my $caname = '';
 5339:     my $cadom = '';
 5340:     if ($env{'request.role'} =~ /^(?:ca|aa)/) {
 5341:         ($cadom,$caname) =
 5342:             ($env{'request.role'}=~/($match_domain)\/($match_username)$/);
 5343:     } elsif ($env{'request.role'} =~ m{^au\./($match_domain)/}) {
 5344:         $caname = $env{'user.name'};
 5345:         $cadom = $env{'user.domain'};
 5346:     }
 5347:     if (($caname ne '') && ($cadom ne '')) {
 5348:         return "/priv/$cadom/$caname/";
 5349:     }
 5350:     return;
 5351: }
 5352: 
 5353: ##############################################
 5354: =pod
 5355: 
 5356: =item * &head_subbox()
 5357: 
 5358: Inputs: $content (contains HTML code with page functions, etc.)
 5359: 
 5360: Returns: HTML div with $content
 5361:          To be included in page header
 5362: 
 5363: =cut
 5364: 
 5365: sub head_subbox {
 5366:     my ($content)=@_;
 5367:     my $output =
 5368:         '<div class="LC_head_subbox">'
 5369:        .$content
 5370:        .'</div>'
 5371: }
 5372: 
 5373: ##############################################
 5374: =pod
 5375: 
 5376: =item * &CSTR_pageheader()
 5377: 
 5378: Input: (optional) filename from which breadcrumb trail is built.
 5379:        In most cases no input as needed, as $env{'request.filename'}
 5380:        is appropriate for use in building the breadcrumb trail.
 5381: 
 5382: Returns: HTML div with CSTR path and recent box
 5383:          To be included on Authoring Space pages
 5384: 
 5385: =cut
 5386: 
 5387: sub CSTR_pageheader {
 5388:     my ($trailfile) = @_;
 5389:     if ($trailfile eq '') {
 5390:         $trailfile = $env{'request.filename'};
 5391:     }
 5392: 
 5393: # this is for resources; directories have customtitle, and crumbs
 5394: # and select recent are created in lonpubdir.pm
 5395: 
 5396:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 5397:     my ($udom,$uname,$thisdisfn)=
 5398:         ($trailfile =~ m{^\Q$londocroot\E/priv/([^/]+)/([^/]+)(?:|/(.*))$});
 5399:     my $formaction = "/priv/$udom/$uname/$thisdisfn";
 5400:     $formaction =~ s{/+}{/}g;
 5401: 
 5402:     my $parentpath = '';
 5403:     my $lastitem = '';
 5404:     if ($thisdisfn =~ m-(.+/)([^/]*)$-) {
 5405:         $parentpath = $1;
 5406:         $lastitem = $2;
 5407:     } else {
 5408:         $lastitem = $thisdisfn;
 5409:     }
 5410: 
 5411:     my $output =
 5412:          '<div>'
 5413:         .&Apache::loncommon::help_open_menu('','',3,'Authoring') #FIXME: Broken? Where is it?
 5414:         .'<b>'.&mt('Authoring Space:').'</b> '
 5415:         .'<form name="dirs" method="post" action="'.$formaction
 5416:         .'" target="_top">' #FIXME lonpubdir: target="_parent"
 5417:         .&Apache::lonhtmlcommon::crumbs($uname.'/'.$parentpath,'_top','/priv/'.$udom,undef,undef);
 5418: 
 5419:     if ($lastitem) {
 5420:         $output .=
 5421:              '<span class="LC_filename">'
 5422:             .$lastitem
 5423:             .'</span>';
 5424:     }
 5425:     $output .=
 5426:          '<br />'
 5427:         #FIXME lonpubdir: &Apache::lonhtmlcommon::crumbs($uname.$thisdisfn.'/','_top','/priv','','+1',1)."</b></tt><br />"
 5428:         .&Apache::lonhtmlcommon::select_recent('construct','recent','this.form.action=this.form.recent.value;this.form.submit()')
 5429:         .'</form>'
 5430:         .&Apache::lonmenu::constspaceform()
 5431:         .'</div>';
 5432: 
 5433:     return $output;
 5434: }
 5435: 
 5436: ###############################################
 5437: ###############################################
 5438: 
 5439: =pod
 5440: 
 5441: =back
 5442: 
 5443: =head1 HTML Helpers
 5444: 
 5445: =over 4
 5446: 
 5447: =item * &bodytag()
 5448: 
 5449: Returns a uniform header for LON-CAPA web pages.
 5450: 
 5451: Inputs: 
 5452: 
 5453: =over 4
 5454: 
 5455: =item * $title, A title to be displayed on the page.
 5456: 
 5457: =item * $function, the current role (can be undef).
 5458: 
 5459: =item * $addentries, extra parameters for the <body> tag.
 5460: 
 5461: =item * $bodyonly, if defined, only return the <body> tag.
 5462: 
 5463: =item * $domain, if defined, force a given domain.
 5464: 
 5465: =item * $forcereg, if page should register as content page (relevant for 
 5466:             text interface only)
 5467: 
 5468: =item * $no_nav_bar, if true, keep the 'what is this' info but remove the
 5469:                      navigational links
 5470: 
 5471: =item * $bgcolor, used to override the bgcolor on a webpage to a specific value
 5472: 
 5473: =item * $args, optional argument valid values are
 5474:             no_auto_mt_title -> prevents &mt()ing the title arg
 5475:             inherit_jsmath -> when creating popup window in a page,
 5476:                               should it have jsmath forced on by the
 5477:                               current page
 5478: 
 5479: =item * $advtoolsref, optional argument, ref to an array containing
 5480:             inlineremote items to be added in "Functions" menu below
 5481:             breadcrumbs.
 5482: 
 5483: =back
 5484: 
 5485: Returns: A uniform header for LON-CAPA web pages.  
 5486: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
 5487: If $bodyonly is undef or zero, an html string containing a <body> tag and 
 5488: other decorations will be returned.
 5489: 
 5490: =cut
 5491: 
 5492: sub bodytag {
 5493:     my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,
 5494:         $no_nav_bar,$bgcolor,$args,$advtoolsref)=@_;
 5495: 
 5496:     my $public;
 5497:     if ((($env{'user.name'} eq 'public') && ($env{'user.domain'} eq 'public'))
 5498:         || ($env{'user.name'} eq '') && ($env{'user.domain'} eq '')) {
 5499:         $public = 1;
 5500:     }
 5501:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
 5502:     my $httphost = $args->{'use_absolute'};
 5503: 
 5504:     $function = &get_users_function() if (!$function);
 5505:     my $img =    &designparm($function.'.img',$domain);
 5506:     my $font =   &designparm($function.'.font',$domain);
 5507:     my $pgbg   = $bgcolor || &designparm($function.'.pgbg',$domain);
 5508: 
 5509:     my %design = ( 'style'   => 'margin-top: 0',
 5510: 		   'bgcolor' => $pgbg,
 5511: 		   'text'    => $font,
 5512:                    'alink'   => &designparm($function.'.alink',$domain),
 5513: 		   'vlink'   => &designparm($function.'.vlink',$domain),
 5514: 		   'link'    => &designparm($function.'.link',$domain),);
 5515:     @design{keys(%$addentries)} = @$addentries{keys(%$addentries)}; 
 5516: 
 5517:  # role and realm
 5518:     my ($role,$realm) = split(m{\./},$env{'request.role'},2);
 5519:     if ($realm) {
 5520:         $realm = '/'.$realm;
 5521:     }
 5522:     if ($role  eq 'ca') {
 5523:         my ($rdom,$rname) = ($realm =~ m{^/($match_domain)/($match_username)$});
 5524:         $realm = &plainname($rname,$rdom);
 5525:     } 
 5526: # realm
 5527:     if ($env{'request.course.id'}) {
 5528:         if ($env{'request.role'} !~ /^cr/) {
 5529:             $role = &Apache::lonnet::plaintext($role,&course_type());
 5530:         }
 5531:         if ($env{'request.course.sec'}) {
 5532:             $role .= ('&nbsp;'x2).'-&nbsp;'.&mt('section:').'&nbsp;'.$env{'request.course.sec'};
 5533:         }   
 5534: 	$realm = $env{'course.'.$env{'request.course.id'}.'.description'};
 5535:     } else {
 5536:         $role = &Apache::lonnet::plaintext($role);
 5537:     }
 5538: 
 5539:     if (!$realm) { $realm='&nbsp;'; }
 5540: 
 5541:     my $extra_body_attr = &make_attr_string($forcereg,\%design);
 5542: 
 5543: # construct main body tag
 5544:     my $bodytag = "<body $extra_body_attr>".
 5545: 	&Apache::lontexconvert::init_math_support($args->{'inherit_jsmath'});
 5546: 
 5547:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
 5548: 
 5549:     if (($bodyonly) || ($no_nav_bar) || ($env{'form.inhibitmenu'} eq 'yes')) {
 5550:         return $bodytag;
 5551:     }
 5552: 
 5553:     if ($public) {
 5554: 	undef($role);
 5555:     }
 5556:     
 5557:     my $titleinfo = '<h1>'.$title.'</h1>';
 5558:     #
 5559:     # Extra info if you are the DC
 5560:     my $dc_info = '';
 5561:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
 5562:                         $env{'course.'.$env{'request.course.id'}.
 5563:                                  '.domain'}.'/'})) {
 5564:         my $cid = $env{'request.course.id'};
 5565:         $dc_info = $cid.' '.$env{'course.'.$cid.'.internal.coursecode'};
 5566:         $dc_info =~ s/\s+$//;
 5567:     }
 5568: 
 5569:     $role = '<span class="LC_nobreak">('.$role.')</span>' if $role;
 5570: 
 5571:         if ($env{'request.state'} eq 'construct') { $forcereg=1; }
 5572: 
 5573:         #    if ($env{'request.state'} eq 'construct') {
 5574:         #        $titleinfo = &CSTR_pageheader(); #FIXME: Will be removed once all scripts have their own calls
 5575:         #    }
 5576: 
 5577:         $bodytag .= Apache::lonhtmlcommon::scripttag(
 5578:             Apache::lonmenu::utilityfunctions($httphost), 'start');
 5579: 
 5580:         my ($left,$right) = Apache::lonmenu::primary_menu();
 5581: 
 5582:         if ($env{'request.noversionuri'} =~ m{^/res/adm/pages/}) {
 5583:              if ($dc_info) {
 5584:                  $dc_info = qq|<span class="LC_cusr_subheading">$dc_info</span>|;
 5585:              }
 5586:              $bodytag .= qq|<div id="LC_nav_bar">$left $role<br />
 5587:                 <em>$realm</em> $dc_info</div>|;
 5588:             return $bodytag;
 5589:         }
 5590: 
 5591:         unless ($env{'request.symb'} =~ m/\.page___\d+___/) {
 5592:             $bodytag .= qq|<div id="LC_nav_bar">$left $role</div>|;
 5593:         }
 5594: 
 5595:         $bodytag .= $right;
 5596: 
 5597:         if ($dc_info) {
 5598:             $dc_info = &dc_courseid_toggle($dc_info);
 5599:         }
 5600:         $bodytag .= qq|<div id="LC_realm">$realm $dc_info</div>|;
 5601: 
 5602:         #if directed to not display the secondary menu, don't.  
 5603:         if ($args->{'no_secondary_menu'}) {
 5604:             return $bodytag;
 5605:         }
 5606:         #don't show menus for public users
 5607:         if (!$public){
 5608:             $bodytag .= Apache::lonmenu::secondary_menu($httphost);
 5609:             $bodytag .= Apache::lonmenu::serverform();
 5610:             $bodytag .= Apache::lonhtmlcommon::scripttag('', 'end');
 5611:             if ($env{'request.state'} eq 'construct') {
 5612:                 $bodytag .= &Apache::lonmenu::innerregister($forcereg,
 5613:                                 $args->{'bread_crumbs'});
 5614:             } elsif ($forcereg) {
 5615:                 $bodytag .= &Apache::lonmenu::innerregister($forcereg,undef,
 5616:                                                             $args->{'group'});
 5617:             } else {
 5618:                 $bodytag .= 
 5619:                     &Apache::lonmenu::prepare_functions($env{'request.noversionuri'},
 5620:                                                         $forcereg,$args->{'group'},
 5621:                                                         $args->{'bread_crumbs'},
 5622:                                                         $advtoolsref);
 5623:             }
 5624:         }else{
 5625:             # this is to seperate menu from content when there's no secondary
 5626:             # menu. Especially needed for public accessible ressources.
 5627:             $bodytag .= '<hr style="clear:both" />';
 5628:             $bodytag .= Apache::lonhtmlcommon::scripttag('', 'end'); 
 5629:         }
 5630: 
 5631:         return $bodytag;
 5632: }
 5633: 
 5634: sub dc_courseid_toggle {
 5635:     my ($dc_info) = @_;
 5636:     return ' <span id="dccidtext" class="LC_cusr_subheading LC_nobreak">'.
 5637:            '<a href="javascript:showCourseID();" class="LC_menubuttons_link">'.
 5638:            &mt('(More ...)').'</a></span>'.
 5639:            '<div id="dccid" class="LC_dccid">'.$dc_info.'</div>';
 5640: }
 5641: 
 5642: sub make_attr_string {
 5643:     my ($register,$attr_ref) = @_;
 5644: 
 5645:     if ($attr_ref && !ref($attr_ref)) {
 5646: 	die("addentries Must be a hash ref ".
 5647: 	    join(':',caller(1))." ".
 5648: 	    join(':',caller(0))." ");
 5649:     }
 5650: 
 5651:     if ($register) {
 5652: 	my ($on_load,$on_unload);
 5653: 	foreach my $key (keys(%{$attr_ref})) {
 5654: 	    if      (lc($key) eq 'onload') {
 5655: 		$on_load.=$attr_ref->{$key}.';';
 5656: 		delete($attr_ref->{$key});
 5657: 
 5658: 	    } elsif (lc($key) eq 'onunload') {
 5659: 		$on_unload.=$attr_ref->{$key}.';';
 5660: 		delete($attr_ref->{$key});
 5661: 	    }
 5662: 	}
 5663: 	$attr_ref->{'onload'}  = $on_load;
 5664: 	$attr_ref->{'onunload'}= $on_unload;
 5665:     }
 5666: 
 5667:     my $attr_string;
 5668:     foreach my $attr (sort(keys(%$attr_ref))) {
 5669: 	$attr_string .= " $attr=\"".$attr_ref->{$attr}.'" ';
 5670:     }
 5671:     return $attr_string;
 5672: }
 5673: 
 5674: 
 5675: ###############################################
 5676: ###############################################
 5677: 
 5678: =pod
 5679: 
 5680: =item * &endbodytag()
 5681: 
 5682: Returns a uniform footer for LON-CAPA web pages.
 5683: 
 5684: Inputs: 1 - optional reference to an args hash
 5685: If in the hash, key for noredirectlink has a value which evaluates to true,
 5686: a 'Continue' link is not displayed if the page contains an
 5687: internal redirect in the <head></head> section,
 5688: i.e., $env{'internal.head.redirect'} exists   
 5689: 
 5690: =cut
 5691: 
 5692: sub endbodytag {
 5693:     my ($args) = @_;
 5694:     my $endbodytag;
 5695:     unless ((ref($args) eq 'HASH') && ($args->{'notbody'})) {
 5696:         $endbodytag='</body>';
 5697:     }
 5698:     $endbodytag=&Apache::lontexconvert::jsMath_process()."\n".$endbodytag;
 5699:     if ( exists( $env{'internal.head.redirect'} ) ) {
 5700:         if (!(ref($args) eq 'HASH' && $args->{'noredirectlink'})) {
 5701: 	    $endbodytag=
 5702: 	        "<br /><a href=\"$env{'internal.head.redirect'}\">".
 5703: 	        &mt('Continue').'</a>'.
 5704: 	        $endbodytag;
 5705:         }
 5706:     }
 5707:     return $endbodytag;
 5708: }
 5709: 
 5710: =pod
 5711: 
 5712: =item * &standard_css()
 5713: 
 5714: Returns a style sheet
 5715: 
 5716: Inputs: (all optional)
 5717:             domain         -> force to color decorate a page for a specific
 5718:                                domain
 5719:             function       -> force usage of a specific rolish color scheme
 5720:             bgcolor        -> override the default page bgcolor
 5721: 
 5722: =cut
 5723: 
 5724: sub standard_css {
 5725:     my ($function,$domain,$bgcolor) = @_;
 5726:     $function  = &get_users_function() if (!$function);
 5727:     my $img    = &designparm($function.'.img',   $domain);
 5728:     my $tabbg  = &designparm($function.'.tabbg', $domain);
 5729:     my $font   = &designparm($function.'.font',  $domain);
 5730:     my $fontmenu = &designparm($function.'.fontmenu', $domain);
 5731: #second colour for later usage
 5732:     my $sidebg = &designparm($function.'.sidebg',$domain);
 5733:     my $pgbg_or_bgcolor =
 5734: 	         $bgcolor ||
 5735: 	         &designparm($function.'.pgbg',  $domain);
 5736:     my $pgbg   = &designparm($function.'.pgbg',  $domain);
 5737:     my $alink  = &designparm($function.'.alink', $domain);
 5738:     my $vlink  = &designparm($function.'.vlink', $domain);
 5739:     my $link   = &designparm($function.'.link',  $domain);
 5740: 
 5741:     my $sans                 = 'Verdana,Arial,Helvetica,sans-serif';
 5742:     my $mono                 = 'monospace';
 5743:     my $data_table_head      = $sidebg;
 5744:     my $data_table_light     = '#FAFAFA';
 5745:     my $data_table_dark      = '#E0E0E0';
 5746:     my $data_table_darker    = '#CCCCCC';
 5747:     my $data_table_highlight = '#FFFF00';
 5748:     my $mail_new             = '#FFBB77';
 5749:     my $mail_new_hover       = '#DD9955';
 5750:     my $mail_read            = '#BBBB77';
 5751:     my $mail_read_hover      = '#999944';
 5752:     my $mail_replied         = '#AAAA88';
 5753:     my $mail_replied_hover   = '#888855';
 5754:     my $mail_other           = '#99BBBB';
 5755:     my $mail_other_hover     = '#669999';
 5756:     my $table_header         = '#DDDDDD';
 5757:     my $feedback_link_bg     = '#BBBBBB';
 5758:     my $lg_border_color      = '#C8C8C8';
 5759:     my $button_hover         = '#BF2317';
 5760: 
 5761:     my $border = ($env{'browser.type'} eq 'explorer' ||
 5762:       $env{'browser.type'} eq 'safari'     ) ? '0 2px 0 2px'
 5763:                                              : '0 3px 0 4px';
 5764: 
 5765: 
 5766:     return <<END;
 5767: 
 5768: /* needed for iframe to allow 100% height in FF */
 5769: body, html { 
 5770:     margin: 0;
 5771:     padding: 0 0.5%;
 5772:     height: 99%; /* to avoid scrollbars */
 5773: }
 5774: 
 5775: body {
 5776:   font-family: $sans;
 5777:   line-height:130%;
 5778:   font-size:0.83em;
 5779:   color:$font;
 5780: }
 5781: 
 5782: a:focus,
 5783: a:focus img {
 5784:   color: red;
 5785: }
 5786: 
 5787: form, .inline {
 5788:   display: inline;
 5789: }
 5790: 
 5791: .LC_right {
 5792:   text-align:right;
 5793: }
 5794: 
 5795: .LC_middle {
 5796:   vertical-align:middle;
 5797: }
 5798: 
 5799: .LC_floatleft {
 5800:   float: left;
 5801: }
 5802: 
 5803: .LC_floatright {
 5804:   float: right;
 5805: }
 5806: 
 5807: .LC_400Box {
 5808:   width:400px;
 5809: }
 5810: 
 5811: .LC_iframecontainer {
 5812:     width: 98%;
 5813:     margin: 0;
 5814:     position: fixed;
 5815:     top: 8.5em;
 5816:     bottom: 0;
 5817: }
 5818: 
 5819: .LC_iframecontainer iframe{
 5820:     border: none;
 5821:     width: 100%;
 5822:     height: 100%;
 5823: }
 5824: 
 5825: .LC_filename {
 5826:   font-family: $mono;
 5827:   white-space:pre;
 5828:   font-size: 120%;
 5829: }
 5830: 
 5831: .LC_fileicon {
 5832:   border: none;
 5833:   height: 1.3em;
 5834:   vertical-align: text-bottom;
 5835:   margin-right: 0.3em;
 5836:   text-decoration:none;
 5837: }
 5838: 
 5839: .LC_setting {
 5840:   text-decoration:underline;
 5841: }
 5842: 
 5843: .LC_error {
 5844:   color: red;
 5845: }
 5846: 
 5847: .LC_warning {
 5848:   color: darkorange;
 5849: }
 5850: 
 5851: .LC_diff_removed {
 5852:   color: red;
 5853: }
 5854: 
 5855: .LC_info,
 5856: .LC_success,
 5857: .LC_diff_added {
 5858:   color: green;
 5859: }
 5860: 
 5861: div.LC_confirm_box {
 5862:   background-color: #FAFAFA;
 5863:   border: 1px solid $lg_border_color;
 5864:   margin-right: 0;
 5865:   padding: 5px;
 5866: }
 5867: 
 5868: div.LC_confirm_box .LC_error img,
 5869: div.LC_confirm_box .LC_success img {
 5870:   vertical-align: middle;
 5871: }
 5872: 
 5873: .LC_icon {
 5874:   border: none;
 5875:   vertical-align: middle;
 5876: }
 5877: 
 5878: .LC_docs_spacer {
 5879:   width: 25px;
 5880:   height: 1px;
 5881:   border: none;
 5882: }
 5883: 
 5884: .LC_internal_info {
 5885:   color: #999999;
 5886: }
 5887: 
 5888: .LC_discussion {
 5889:   background: $data_table_dark;
 5890:   border: 1px solid black;
 5891:   margin: 2px;
 5892: }
 5893: 
 5894: .LC_disc_action_left {
 5895:   background: $sidebg;
 5896:   text-align: left;
 5897:   padding: 4px;
 5898:   margin: 2px;
 5899: }
 5900: 
 5901: .LC_disc_action_right {
 5902:   background: $sidebg;
 5903:   text-align: right;
 5904:   padding: 4px;
 5905:   margin: 2px;
 5906: }
 5907: 
 5908: .LC_disc_new_item {
 5909:   background: white;
 5910:   border: 2px solid red;
 5911:   margin: 4px;
 5912:   padding: 4px;
 5913: }
 5914: 
 5915: .LC_disc_old_item {
 5916:   background: white;
 5917:   margin: 4px;
 5918:   padding: 4px;
 5919: }
 5920: 
 5921: table.LC_pastsubmission {
 5922:   border: 1px solid black;
 5923:   margin: 2px;
 5924: }
 5925: 
 5926: table#LC_menubuttons {
 5927:   width: 100%;
 5928:   background: $pgbg;
 5929:   border: 2px;
 5930:   border-collapse: separate;
 5931:   padding: 0;
 5932: }
 5933: 
 5934: table#LC_title_bar a {
 5935:   color: $fontmenu;
 5936: }
 5937: 
 5938: table#LC_title_bar {
 5939:   clear: both;
 5940:   display: none;
 5941: }
 5942: 
 5943: table#LC_title_bar,
 5944: table.LC_breadcrumbs, /* obsolete? */
 5945: table#LC_title_bar.LC_with_remote {
 5946:   width: 100%;
 5947:   border-color: $pgbg;
 5948:   border-style: solid;
 5949:   border-width: $border;
 5950:   background: $pgbg;
 5951:   color: $fontmenu;
 5952:   border-collapse: collapse;
 5953:   padding: 0;
 5954:   margin: 0;
 5955: }
 5956: 
 5957: ul.LC_breadcrumb_tools_outerlist {
 5958:     margin: 0;
 5959:     padding: 0;
 5960:     position: relative;
 5961:     list-style: none;
 5962: }
 5963: ul.LC_breadcrumb_tools_outerlist li {
 5964:     display: inline;
 5965: }
 5966: 
 5967: .LC_breadcrumb_tools_navigation {
 5968:     padding: 0;
 5969:     margin: 0;
 5970:     float: left;
 5971: }
 5972: .LC_breadcrumb_tools_tools {
 5973:     padding: 0;
 5974:     margin: 0;
 5975:     float: right;
 5976: }
 5977: 
 5978: table#LC_title_bar td {
 5979:   background: $tabbg;
 5980: }
 5981: 
 5982: table#LC_menubuttons img {
 5983:   border: none;
 5984: }
 5985: 
 5986: .LC_breadcrumbs_component {
 5987:   float: right;
 5988:   margin: 0 1em;
 5989: }
 5990: .LC_breadcrumbs_component img {
 5991:   vertical-align: middle;
 5992: }
 5993: 
 5994: td.LC_table_cell_checkbox {
 5995:   text-align: center;
 5996: }
 5997: 
 5998: .LC_fontsize_small {
 5999:   font-size: 70%;
 6000: }
 6001: 
 6002: #LC_breadcrumbs {
 6003:   clear:both;
 6004:   background: $sidebg;
 6005:   border-bottom: 1px solid $lg_border_color;
 6006:   line-height: 2.5em;
 6007:   overflow: hidden;
 6008:   margin: 0;
 6009:   padding: 0;
 6010:   text-align: left;
 6011: }
 6012: 
 6013: .LC_head_subbox, .LC_actionbox {
 6014:   clear:both;
 6015:   background: #F8F8F8; /* $sidebg; */
 6016:   border: 1px solid $sidebg;
 6017:   margin: 0 0 10px 0;
 6018:   padding: 3px;
 6019:   text-align: left;
 6020: }
 6021: 
 6022: .LC_fontsize_medium {
 6023:   font-size: 85%;
 6024: }
 6025: 
 6026: .LC_fontsize_large {
 6027:   font-size: 120%;
 6028: }
 6029: 
 6030: .LC_menubuttons_inline_text {
 6031:   color: $font;
 6032:   font-size: 90%;
 6033:   padding-left:3px;
 6034: }
 6035: 
 6036: .LC_menubuttons_inline_text img{
 6037:   vertical-align: middle;
 6038: }
 6039: 
 6040: li.LC_menubuttons_inline_text img {
 6041:   cursor:pointer;
 6042:   text-decoration: none;
 6043: }
 6044: 
 6045: .LC_menubuttons_link {
 6046:   text-decoration: none;
 6047: }
 6048: 
 6049: .LC_menubuttons_category {
 6050:   color: $font;
 6051:   background: $pgbg;
 6052:   font-size: larger;
 6053:   font-weight: bold;
 6054: }
 6055: 
 6056: td.LC_menubuttons_text {
 6057:   color: $font;
 6058: }
 6059: 
 6060: .LC_current_location {
 6061:   background: $tabbg;
 6062: }
 6063: 
 6064: table.LC_data_table {
 6065:   border: 1px solid #000000;
 6066:   border-collapse: separate;
 6067:   border-spacing: 1px;
 6068:   background: $pgbg;
 6069: }
 6070: 
 6071: .LC_data_table_dense {
 6072:   font-size: small;
 6073: }
 6074: 
 6075: table.LC_nested_outer {
 6076:   border: 1px solid #000000;
 6077:   border-collapse: collapse;
 6078:   border-spacing: 0;
 6079:   width: 100%;
 6080: }
 6081: 
 6082: table.LC_innerpickbox,
 6083: table.LC_nested {
 6084:   border: none;
 6085:   border-collapse: collapse;
 6086:   border-spacing: 0;
 6087:   width: 100%;
 6088: }
 6089: 
 6090: table.LC_data_table tr th,
 6091: table.LC_calendar tr th,
 6092: table.LC_prior_tries tr th,
 6093: table.LC_innerpickbox tr th {
 6094:   font-weight: bold;
 6095:   background-color: $data_table_head;
 6096:   color:$fontmenu;
 6097:   font-size:90%;
 6098: }
 6099: 
 6100: table.LC_innerpickbox tr th,
 6101: table.LC_innerpickbox tr td {
 6102:   vertical-align: top;
 6103: }
 6104: 
 6105: table.LC_data_table tr.LC_info_row > td {
 6106:   background-color: #CCCCCC;
 6107:   font-weight: bold;
 6108:   text-align: left;
 6109: }
 6110: 
 6111: table.LC_data_table tr.LC_odd_row > td {
 6112:   background-color: $data_table_light;
 6113:   padding: 2px;
 6114:   vertical-align: top;
 6115: }
 6116: 
 6117: table.LC_pick_box tr > td.LC_odd_row {
 6118:   background-color: $data_table_light;
 6119:   vertical-align: top;
 6120: }
 6121: 
 6122: table.LC_data_table tr.LC_even_row > td {
 6123:   background-color: $data_table_dark;
 6124:   padding: 2px;
 6125:   vertical-align: top;
 6126: }
 6127: 
 6128: table.LC_pick_box tr > td.LC_even_row {
 6129:   background-color: $data_table_dark;
 6130:   vertical-align: top;
 6131: }
 6132: 
 6133: table.LC_data_table tr.LC_data_table_highlight td {
 6134:   background-color: $data_table_darker;
 6135: }
 6136: 
 6137: table.LC_data_table tr td.LC_leftcol_header {
 6138:   background-color: $data_table_head;
 6139:   font-weight: bold;
 6140: }
 6141: 
 6142: table.LC_data_table tr.LC_empty_row td,
 6143: table.LC_nested tr.LC_empty_row td {
 6144:   font-weight: bold;
 6145:   font-style: italic;
 6146:   text-align: center;
 6147:   padding: 8px;
 6148: }
 6149: 
 6150: table.LC_data_table tr.LC_empty_row td,
 6151: table.LC_data_table tr.LC_footer_row td {
 6152:   background-color: $sidebg;
 6153: }
 6154: 
 6155: table.LC_nested tr.LC_empty_row td {
 6156:   background-color: #FFFFFF;
 6157: }
 6158: 
 6159: table.LC_caption {
 6160: }
 6161: 
 6162: table.LC_nested tr.LC_empty_row td {
 6163:   padding: 4ex
 6164: }
 6165: 
 6166: table.LC_nested_outer tr th {
 6167:   font-weight: bold;
 6168:   color:$fontmenu;
 6169:   background-color: $data_table_head;
 6170:   font-size: small;
 6171:   border-bottom: 1px solid #000000;
 6172: }
 6173: 
 6174: table.LC_nested_outer tr td.LC_subheader {
 6175:   background-color: $data_table_head;
 6176:   font-weight: bold;
 6177:   font-size: small;
 6178:   border-bottom: 1px solid #000000;
 6179:   text-align: right;
 6180: }
 6181: 
 6182: table.LC_nested tr.LC_info_row td {
 6183:   background-color: #CCCCCC;
 6184:   font-weight: bold;
 6185:   font-size: small;
 6186:   text-align: center;
 6187: }
 6188: 
 6189: table.LC_nested tr.LC_info_row td.LC_left_item,
 6190: table.LC_nested_outer tr th.LC_left_item {
 6191:   text-align: left;
 6192: }
 6193: 
 6194: table.LC_nested td {
 6195:   background-color: #FFFFFF;
 6196:   font-size: small;
 6197: }
 6198: 
 6199: table.LC_nested_outer tr th.LC_right_item,
 6200: table.LC_nested tr.LC_info_row td.LC_right_item,
 6201: table.LC_nested tr.LC_odd_row td.LC_right_item,
 6202: table.LC_nested tr td.LC_right_item {
 6203:   text-align: right;
 6204: }
 6205: 
 6206: table.LC_nested tr.LC_odd_row td {
 6207:   background-color: #EEEEEE;
 6208: }
 6209: 
 6210: table.LC_createuser {
 6211: }
 6212: 
 6213: table.LC_createuser tr.LC_section_row td {
 6214:   font-size: small;
 6215: }
 6216: 
 6217: table.LC_createuser tr.LC_info_row td  {
 6218:   background-color: #CCCCCC;
 6219:   font-weight: bold;
 6220:   text-align: center;
 6221: }
 6222: 
 6223: table.LC_calendar {
 6224:   border: 1px solid #000000;
 6225:   border-collapse: collapse;
 6226:   width: 98%;
 6227: }
 6228: 
 6229: table.LC_calendar_pickdate {
 6230:   font-size: xx-small;
 6231: }
 6232: 
 6233: table.LC_calendar tr td {
 6234:   border: 1px solid #000000;
 6235:   vertical-align: top;
 6236:   width: 14%;
 6237: }
 6238: 
 6239: table.LC_calendar tr td.LC_calendar_day_empty {
 6240:   background-color: $data_table_dark;
 6241: }
 6242: 
 6243: table.LC_calendar tr td.LC_calendar_day_current {
 6244:   background-color: $data_table_highlight;
 6245: }
 6246: 
 6247: table.LC_data_table tr td.LC_mail_new {
 6248:   background-color: $mail_new;
 6249: }
 6250: 
 6251: table.LC_data_table tr.LC_mail_new:hover {
 6252:   background-color: $mail_new_hover;
 6253: }
 6254: 
 6255: table.LC_data_table tr td.LC_mail_read {
 6256:   background-color: $mail_read;
 6257: }
 6258: 
 6259: /*
 6260: table.LC_data_table tr.LC_mail_read:hover {
 6261:   background-color: $mail_read_hover;
 6262: }
 6263: */
 6264: 
 6265: table.LC_data_table tr td.LC_mail_replied {
 6266:   background-color: $mail_replied;
 6267: }
 6268: 
 6269: /*
 6270: table.LC_data_table tr.LC_mail_replied:hover {
 6271:   background-color: $mail_replied_hover;
 6272: }
 6273: */
 6274: 
 6275: table.LC_data_table tr td.LC_mail_other {
 6276:   background-color: $mail_other;
 6277: }
 6278: 
 6279: /*
 6280: table.LC_data_table tr.LC_mail_other:hover {
 6281:   background-color: $mail_other_hover;
 6282: }
 6283: */
 6284: 
 6285: table.LC_data_table tr > td.LC_browser_file,
 6286: table.LC_data_table tr > td.LC_browser_file_published {
 6287:   background: #AAEE77;
 6288: }
 6289: 
 6290: table.LC_data_table tr > td.LC_browser_file_locked,
 6291: table.LC_data_table tr > td.LC_browser_file_unpublished {
 6292:   background: #FFAA99;
 6293: }
 6294: 
 6295: table.LC_data_table tr > td.LC_browser_file_obsolete {
 6296:   background: #888888;
 6297: }
 6298: 
 6299: table.LC_data_table tr > td.LC_browser_file_modified,
 6300: table.LC_data_table tr > td.LC_browser_file_metamodified {
 6301:   background: #F8F866;
 6302: }
 6303: 
 6304: table.LC_data_table tr.LC_browser_folder > td {
 6305:   background: #E0E8FF;
 6306: }
 6307: 
 6308: table.LC_data_table tr > td.LC_roles_is {
 6309:   /* background: #77FF77; */
 6310: }
 6311: 
 6312: table.LC_data_table tr > td.LC_roles_future {
 6313:   border-right: 8px solid #FFFF77;
 6314: }
 6315: 
 6316: table.LC_data_table tr > td.LC_roles_will {
 6317:   border-right: 8px solid #FFAA77;
 6318: }
 6319: 
 6320: table.LC_data_table tr > td.LC_roles_expired {
 6321:   border-right: 8px solid #FF7777;
 6322: }
 6323: 
 6324: table.LC_data_table tr > td.LC_roles_will_not {
 6325:   border-right: 8px solid #AAFF77;
 6326: }
 6327: 
 6328: table.LC_data_table tr > td.LC_roles_selected {
 6329:   border-right: 8px solid #11CC55;
 6330: }
 6331: 
 6332: span.LC_current_location {
 6333:   font-size:larger;
 6334:   background: $pgbg;
 6335: }
 6336: 
 6337: span.LC_current_nav_location {
 6338:   font-weight:bold;
 6339:   background: $sidebg;
 6340: }
 6341: 
 6342: span.LC_parm_menu_item {
 6343:   font-size: larger;
 6344: }
 6345: 
 6346: span.LC_parm_scope_all {
 6347:   color: red;
 6348: }
 6349: 
 6350: span.LC_parm_scope_folder {
 6351:   color: green;
 6352: }
 6353: 
 6354: span.LC_parm_scope_resource {
 6355:   color: orange;
 6356: }
 6357: 
 6358: span.LC_parm_part {
 6359:   color: blue;
 6360: }
 6361: 
 6362: span.LC_parm_folder,
 6363: span.LC_parm_symb {
 6364:   font-size: x-small;
 6365:   font-family: $mono;
 6366:   color: #AAAAAA;
 6367: }
 6368: 
 6369: ul.LC_parm_parmlist li {
 6370:   display: inline-block;
 6371:   padding: 0.3em 0.8em;
 6372:   vertical-align: top;
 6373:   width: 150px;
 6374:   border-top:1px solid $lg_border_color;
 6375: }
 6376: 
 6377: td.LC_parm_overview_level_menu,
 6378: td.LC_parm_overview_map_menu,
 6379: td.LC_parm_overview_parm_selectors,
 6380: td.LC_parm_overview_restrictions  {
 6381:   border: 1px solid black;
 6382:   border-collapse: collapse;
 6383: }
 6384: 
 6385: table.LC_parm_overview_restrictions td {
 6386:   border-width: 1px 4px 1px 4px;
 6387:   border-style: solid;
 6388:   border-color: $pgbg;
 6389:   text-align: center;
 6390: }
 6391: 
 6392: table.LC_parm_overview_restrictions th {
 6393:   background: $tabbg;
 6394:   border-width: 1px 4px 1px 4px;
 6395:   border-style: solid;
 6396:   border-color: $pgbg;
 6397: }
 6398: 
 6399: table#LC_helpmenu {
 6400:   border: none;
 6401:   height: 55px;
 6402:   border-spacing: 0;
 6403: }
 6404: 
 6405: table#LC_helpmenu fieldset legend {
 6406:   font-size: larger;
 6407: }
 6408: 
 6409: table#LC_helpmenu_links {
 6410:   width: 100%;
 6411:   border: 1px solid black;
 6412:   background: $pgbg;
 6413:   padding: 0;
 6414:   border-spacing: 1px;
 6415: }
 6416: 
 6417: table#LC_helpmenu_links tr td {
 6418:   padding: 1px;
 6419:   background: $tabbg;
 6420:   text-align: center;
 6421:   font-weight: bold;
 6422: }
 6423: 
 6424: table#LC_helpmenu_links a:link,
 6425: table#LC_helpmenu_links a:visited,
 6426: table#LC_helpmenu_links a:active {
 6427:   text-decoration: none;
 6428:   color: $font;
 6429: }
 6430: 
 6431: table#LC_helpmenu_links a:hover {
 6432:   text-decoration: underline;
 6433:   color: $vlink;
 6434: }
 6435: 
 6436: .LC_chrt_popup_exists {
 6437:   border: 1px solid #339933;
 6438:   margin: -1px;
 6439: }
 6440: 
 6441: .LC_chrt_popup_up {
 6442:   border: 1px solid yellow;
 6443:   margin: -1px;
 6444: }
 6445: 
 6446: .LC_chrt_popup {
 6447:   border: 1px solid #8888FF;
 6448:   background: #CCCCFF;
 6449: }
 6450: 
 6451: table.LC_pick_box {
 6452:   border-collapse: separate;
 6453:   background: white;
 6454:   border: 1px solid black;
 6455:   border-spacing: 1px;
 6456: }
 6457: 
 6458: table.LC_pick_box td.LC_pick_box_title {
 6459:   background: $sidebg;
 6460:   font-weight: bold;
 6461:   text-align: left;
 6462:   vertical-align: top;
 6463:   width: 184px;
 6464:   padding: 8px;
 6465: }
 6466: 
 6467: table.LC_pick_box td.LC_pick_box_value {
 6468:   text-align: left;
 6469:   padding: 8px;
 6470: }
 6471: 
 6472: table.LC_pick_box td.LC_pick_box_select {
 6473:   text-align: left;
 6474:   padding: 8px;
 6475: }
 6476: 
 6477: table.LC_pick_box td.LC_pick_box_separator {
 6478:   padding: 0;
 6479:   height: 1px;
 6480:   background: black;
 6481: }
 6482: 
 6483: table.LC_pick_box td.LC_pick_box_submit {
 6484:   text-align: right;
 6485: }
 6486: 
 6487: table.LC_pick_box td.LC_evenrow_value {
 6488:   text-align: left;
 6489:   padding: 8px;
 6490:   background-color: $data_table_light;
 6491: }
 6492: 
 6493: table.LC_pick_box td.LC_oddrow_value {
 6494:   text-align: left;
 6495:   padding: 8px;
 6496:   background-color: $data_table_light;
 6497: }
 6498: 
 6499: span.LC_helpform_receipt_cat {
 6500:   font-weight: bold;
 6501: }
 6502: 
 6503: table.LC_group_priv_box {
 6504:   background: white;
 6505:   border: 1px solid black;
 6506:   border-spacing: 1px;
 6507: }
 6508: 
 6509: table.LC_group_priv_box td.LC_pick_box_title {
 6510:   background: $tabbg;
 6511:   font-weight: bold;
 6512:   text-align: right;
 6513:   width: 184px;
 6514: }
 6515: 
 6516: table.LC_group_priv_box td.LC_groups_fixed {
 6517:   background: $data_table_light;
 6518:   text-align: center;
 6519: }
 6520: 
 6521: table.LC_group_priv_box td.LC_groups_optional {
 6522:   background: $data_table_dark;
 6523:   text-align: center;
 6524: }
 6525: 
 6526: table.LC_group_priv_box td.LC_groups_functionality {
 6527:   background: $data_table_darker;
 6528:   text-align: center;
 6529:   font-weight: bold;
 6530: }
 6531: 
 6532: table.LC_group_priv td {
 6533:   text-align: left;
 6534:   padding: 0;
 6535: }
 6536: 
 6537: .LC_navbuttons {
 6538:   margin: 2ex 0ex 2ex 0ex;
 6539: }
 6540: 
 6541: .LC_topic_bar {
 6542:   font-weight: bold;
 6543:   background: $tabbg;
 6544:   margin: 1em 0em 1em 2em;
 6545:   padding: 3px;
 6546:   font-size: 1.2em;
 6547: }
 6548: 
 6549: .LC_topic_bar span {
 6550:   left: 0.5em;
 6551:   position: absolute;
 6552:   vertical-align: middle;
 6553:   font-size: 1.2em;
 6554: }
 6555: 
 6556: table.LC_course_group_status {
 6557:   margin: 20px;
 6558: }
 6559: 
 6560: table.LC_status_selector td {
 6561:   vertical-align: top;
 6562:   text-align: center;
 6563:   padding: 4px;
 6564: }
 6565: 
 6566: div.LC_feedback_link {
 6567:   clear: both;
 6568:   background: $sidebg;
 6569:   width: 100%;
 6570:   padding-bottom: 10px;
 6571:   border: 1px $tabbg solid;
 6572:   height: 22px;
 6573:   line-height: 22px;
 6574:   padding-top: 5px;
 6575: }
 6576: 
 6577: div.LC_feedback_link img {
 6578:   height: 22px;
 6579:   vertical-align:middle;
 6580: }
 6581: 
 6582: div.LC_feedback_link a {
 6583:   text-decoration: none;
 6584: }
 6585: 
 6586: div.LC_comblock {
 6587:   display:inline;
 6588:   color:$font;
 6589:   font-size:90%;
 6590: }
 6591: 
 6592: div.LC_feedback_link div.LC_comblock {
 6593:   padding-left:5px;
 6594: }
 6595: 
 6596: div.LC_feedback_link div.LC_comblock a {
 6597:   color:$font;
 6598: }
 6599: 
 6600: span.LC_feedback_link {
 6601:   /* background: $feedback_link_bg; */
 6602:   font-size: larger;
 6603: }
 6604: 
 6605: span.LC_message_link {
 6606:   /* background: $feedback_link_bg; */
 6607:   font-size: larger;
 6608:   position: absolute;
 6609:   right: 1em;
 6610: }
 6611: 
 6612: table.LC_prior_tries {
 6613:   border: 1px solid #000000;
 6614:   border-collapse: separate;
 6615:   border-spacing: 1px;
 6616: }
 6617: 
 6618: table.LC_prior_tries td {
 6619:   padding: 2px;
 6620: }
 6621: 
 6622: .LC_answer_correct {
 6623:   background: lightgreen;
 6624:   color: darkgreen;
 6625:   padding: 6px;
 6626: }
 6627: 
 6628: .LC_answer_charged_try {
 6629:   background: #FFAAAA;
 6630:   color: darkred;
 6631:   padding: 6px;
 6632: }
 6633: 
 6634: .LC_answer_not_charged_try,
 6635: .LC_answer_no_grade,
 6636: .LC_answer_late {
 6637:   background: lightyellow;
 6638:   color: black;
 6639:   padding: 6px;
 6640: }
 6641: 
 6642: .LC_answer_previous {
 6643:   background: lightblue;
 6644:   color: darkblue;
 6645:   padding: 6px;
 6646: }
 6647: 
 6648: .LC_answer_no_message {
 6649:   background: #FFFFFF;
 6650:   color: black;
 6651:   padding: 6px;
 6652: }
 6653: 
 6654: .LC_answer_unknown {
 6655:   background: orange;
 6656:   color: black;
 6657:   padding: 6px;
 6658: }
 6659: 
 6660: span.LC_prior_numerical,
 6661: span.LC_prior_string,
 6662: span.LC_prior_custom,
 6663: span.LC_prior_reaction,
 6664: span.LC_prior_math {
 6665:   font-family: $mono;
 6666:   white-space: pre;
 6667: }
 6668: 
 6669: span.LC_prior_string {
 6670:   font-family: $mono;
 6671:   white-space: pre;
 6672: }
 6673: 
 6674: table.LC_prior_option {
 6675:   width: 100%;
 6676:   border-collapse: collapse;
 6677: }
 6678: 
 6679: table.LC_prior_rank,
 6680: table.LC_prior_match {
 6681:   border-collapse: collapse;
 6682: }
 6683: 
 6684: table.LC_prior_option tr td,
 6685: table.LC_prior_rank tr td,
 6686: table.LC_prior_match tr td {
 6687:   border: 1px solid #000000;
 6688: }
 6689: 
 6690: .LC_nobreak {
 6691:   white-space: nowrap;
 6692: }
 6693: 
 6694: span.LC_cusr_emph {
 6695:   font-style: italic;
 6696: }
 6697: 
 6698: span.LC_cusr_subheading {
 6699:   font-weight: normal;
 6700:   font-size: 85%;
 6701: }
 6702: 
 6703: div.LC_docs_entry_move {
 6704:   border: 1px solid #BBBBBB;
 6705:   background: #DDDDDD;
 6706:   width: 22px;
 6707:   padding: 1px;
 6708:   margin: 0;
 6709: }
 6710: 
 6711: table.LC_data_table tr > td.LC_docs_entry_commands,
 6712: table.LC_data_table tr > td.LC_docs_entry_parameter {
 6713:   font-size: x-small;
 6714: }
 6715: 
 6716: .LC_docs_entry_parameter {
 6717:   white-space: nowrap;
 6718: }
 6719: 
 6720: .LC_docs_copy {
 6721:   color: #000099;
 6722: }
 6723: 
 6724: .LC_docs_cut {
 6725:   color: #550044;
 6726: }
 6727: 
 6728: .LC_docs_rename {
 6729:   color: #009900;
 6730: }
 6731: 
 6732: .LC_docs_remove {
 6733:   color: #990000;
 6734: }
 6735: 
 6736: .LC_docs_reinit_warn,
 6737: .LC_docs_ext_edit {
 6738:   font-size: x-small;
 6739: }
 6740: 
 6741: table.LC_docs_adddocs td,
 6742: table.LC_docs_adddocs th {
 6743:   border: 1px solid #BBBBBB;
 6744:   padding: 4px;
 6745:   background: #DDDDDD;
 6746: }
 6747: 
 6748: table.LC_sty_begin {
 6749:   background: #BBFFBB;
 6750: }
 6751: 
 6752: table.LC_sty_end {
 6753:   background: #FFBBBB;
 6754: }
 6755: 
 6756: table.LC_double_column {
 6757:   border-width: 0;
 6758:   border-collapse: collapse;
 6759:   width: 100%;
 6760:   padding: 2px;
 6761: }
 6762: 
 6763: table.LC_double_column tr td.LC_left_col {
 6764:   top: 2px;
 6765:   left: 2px;
 6766:   width: 47%;
 6767:   vertical-align: top;
 6768: }
 6769: 
 6770: table.LC_double_column tr td.LC_right_col {
 6771:   top: 2px;
 6772:   right: 2px;
 6773:   width: 47%;
 6774:   vertical-align: top;
 6775: }
 6776: 
 6777: div.LC_left_float {
 6778:   float: left;
 6779:   padding-right: 5%;
 6780:   padding-bottom: 4px;
 6781: }
 6782: 
 6783: div.LC_clear_float_header {
 6784:   padding-bottom: 2px;
 6785: }
 6786: 
 6787: div.LC_clear_float_footer {
 6788:   padding-top: 10px;
 6789:   clear: both;
 6790: }
 6791: 
 6792: div.LC_grade_show_user {
 6793: /*  border-left: 5px solid $sidebg; */
 6794:   border-top: 5px solid #000000;
 6795:   margin: 50px 0 0 0;
 6796:   padding: 15px 0 5px 10px;
 6797: }
 6798: 
 6799: div.LC_grade_show_user_odd_row {
 6800: /*  border-left: 5px solid #000000; */
 6801: }
 6802: 
 6803: div.LC_grade_show_user div.LC_Box {
 6804:   margin-right: 50px;
 6805: }
 6806: 
 6807: div.LC_grade_submissions,
 6808: div.LC_grade_message_center,
 6809: div.LC_grade_info_links {
 6810:   margin: 5px;
 6811:   width: 99%;
 6812:   background: #FFFFFF;
 6813: }
 6814: 
 6815: div.LC_grade_submissions_header,
 6816: div.LC_grade_message_center_header {
 6817:   font-weight: bold;
 6818:   font-size: large;
 6819: }
 6820: 
 6821: div.LC_grade_submissions_body,
 6822: div.LC_grade_message_center_body {
 6823:   border: 1px solid black;
 6824:   width: 99%;
 6825:   background: #FFFFFF;
 6826: }
 6827: 
 6828: table.LC_scantron_action {
 6829:   width: 100%;
 6830: }
 6831: 
 6832: table.LC_scantron_action tr th {
 6833:   font-weight:bold;
 6834:   font-style:normal;
 6835: }
 6836: 
 6837: .LC_edit_problem_header,
 6838: div.LC_edit_problem_footer {
 6839:   font-weight: normal;
 6840:   font-size:  medium;
 6841:   margin: 2px;
 6842:   background-color: $sidebg;
 6843: }
 6844: 
 6845: div.LC_edit_problem_header,
 6846: div.LC_edit_problem_header div,
 6847: div.LC_edit_problem_footer,
 6848: div.LC_edit_problem_footer div,
 6849: div.LC_edit_problem_editxml_header,
 6850: div.LC_edit_problem_editxml_header div {
 6851:   z-index: 100;
 6852: }
 6853: 
 6854: div.LC_edit_problem_header_title {
 6855:   font-weight: bold;
 6856:   font-size: larger;
 6857:   background: $tabbg;
 6858:   padding: 3px;
 6859:   margin: 0 0 5px 0;
 6860: }
 6861: 
 6862: table.LC_edit_problem_header_title {
 6863:   width: 100%;
 6864:   background: $tabbg;
 6865: }
 6866: 
 6867: div.LC_edit_actionbar {
 6868:     background-color: $sidebg;
 6869:     margin: 0;
 6870:     padding: 0;
 6871:     line-height: 200%;
 6872: }
 6873: 
 6874: div.LC_edit_actionbar div{
 6875:     padding: 0;
 6876:     margin: 0;
 6877:     display: inline-block;
 6878: }
 6879: 
 6880: .LC_edit_opt {
 6881:   padding-left: 1em;
 6882:   white-space: nowrap;
 6883: }
 6884: 
 6885: .LC_edit_problem_latexhelper{
 6886:     text-align: right;
 6887: }
 6888: 
 6889: #LC_edit_problem_colorful div{
 6890:     margin-left: 40px;
 6891: }
 6892: 
 6893: #LC_edit_problem_codemirror div{
 6894:     margin-left: 0px;
 6895: }
 6896: 
 6897: img.stift {
 6898:   border-width: 0;
 6899:   vertical-align: middle;
 6900: }
 6901: 
 6902: table td.LC_mainmenu_col_fieldset {
 6903:   vertical-align: top;
 6904: }
 6905: 
 6906: div.LC_createcourse {
 6907:   margin: 10px 10px 10px 10px;
 6908: }
 6909: 
 6910: .LC_dccid {
 6911:   float: right;
 6912:   margin: 0.2em 0 0 0;
 6913:   padding: 0;
 6914:   font-size: 90%;
 6915:   display:none;
 6916: }
 6917: 
 6918: ol.LC_primary_menu a:hover,
 6919: ol#LC_MenuBreadcrumbs a:hover,
 6920: ol#LC_PathBreadcrumbs a:hover,
 6921: ul#LC_secondary_menu a:hover,
 6922: .LC_FormSectionClearButton input:hover
 6923: ul.LC_TabContent   li:hover a {
 6924:   color:$button_hover;
 6925:   text-decoration:none;
 6926: }
 6927: 
 6928: h1 {
 6929:   padding: 0;
 6930:   line-height:130%;
 6931: }
 6932: 
 6933: h2,
 6934: h3,
 6935: h4,
 6936: h5,
 6937: h6 {
 6938:   margin: 5px 0 5px 0;
 6939:   padding: 0;
 6940:   line-height:130%;
 6941: }
 6942: 
 6943: .LC_hcell {
 6944:   padding:3px 15px 3px 15px;
 6945:   margin: 0;
 6946:   background-color:$tabbg;
 6947:   color:$fontmenu;
 6948:   border-bottom:solid 1px $lg_border_color;
 6949: }
 6950: 
 6951: .LC_Box > .LC_hcell {
 6952:   margin: 0 -10px 10px -10px;
 6953: }
 6954: 
 6955: .LC_noBorder {
 6956:   border: 0;
 6957: }
 6958: 
 6959: .LC_FormSectionClearButton input {
 6960:   background-color:transparent;
 6961:   border: none;
 6962:   cursor:pointer;
 6963:   text-decoration:underline;
 6964: }
 6965: 
 6966: .LC_help_open_topic {
 6967:   color: #FFFFFF;
 6968:   background-color: #EEEEFF;
 6969:   margin: 1px;
 6970:   padding: 4px;
 6971:   border: 1px solid #000033;
 6972:   white-space: nowrap;
 6973:   /* vertical-align: middle; */
 6974: }
 6975: 
 6976: dl,
 6977: ul,
 6978: div,
 6979: fieldset {
 6980:   margin: 10px 10px 10px 0;
 6981:   /* overflow: hidden; */
 6982: }
 6983: 
 6984: article.geogebraweb div {
 6985:     margin: 0;
 6986: }
 6987: 
 6988: fieldset > legend {
 6989:   font-weight: bold;
 6990:   padding: 0 5px 0 5px;
 6991: }
 6992: 
 6993: #LC_nav_bar {
 6994:   float: left;
 6995:   background-color: $pgbg_or_bgcolor;
 6996:   margin: 0 0 2px 0;
 6997: }
 6998: 
 6999: #LC_realm {
 7000:   margin: 0.2em 0 0 0;
 7001:   padding: 0;
 7002:   font-weight: bold;
 7003:   text-align: center;
 7004:   background-color: $pgbg_or_bgcolor;
 7005: }
 7006: 
 7007: #LC_nav_bar em {
 7008:   font-weight: bold;
 7009:   font-style: normal;
 7010: }
 7011: 
 7012: ol.LC_primary_menu {
 7013:   margin: 0;
 7014:   padding: 0;
 7015: }
 7016: 
 7017: ol#LC_PathBreadcrumbs {
 7018:   margin: 0;
 7019: }
 7020: 
 7021: ol.LC_primary_menu li {
 7022:   color: RGB(80, 80, 80);
 7023:   vertical-align: middle;
 7024:   text-align: left;
 7025:   list-style: none;
 7026:   position: relative;
 7027:   float: left;
 7028:   z-index: 100; /* will be displayed above codemirror and underneath the help-layer */
 7029:   line-height: 1.5em;
 7030: }
 7031: 
 7032: ol.LC_primary_menu li a,
 7033: ol.LC_primary_menu li p {
 7034:   display: block;
 7035:   margin: 0;
 7036:   padding: 0 5px 0 10px;
 7037:   text-decoration: none;
 7038: }
 7039: 
 7040: ol.LC_primary_menu li p span.LC_primary_menu_innertitle {
 7041:   display: inline-block;
 7042:   width: 95%;
 7043:   text-align: left;
 7044: }
 7045: 
 7046: ol.LC_primary_menu li p span.LC_primary_menu_innerarrow {
 7047:   display: inline-block;	
 7048:   width: 5%;
 7049:   float: right;
 7050:   text-align: right;
 7051:   font-size: 70%;
 7052: }
 7053: 
 7054: ol.LC_primary_menu ul {
 7055:   display: none;
 7056:   width: 15em;
 7057:   background-color: $data_table_light;
 7058:   position: absolute;
 7059:   top: 100%;
 7060: }
 7061: 
 7062: ol.LC_primary_menu ul ul {
 7063:   left: 100%;
 7064:   top: 0;
 7065: }
 7066: 
 7067: ol.LC_primary_menu li:hover > ul, ol.LC_primary_menu li.hover > ul {
 7068:   display: block;
 7069:   position: absolute;
 7070:   margin: 0;
 7071:   padding: 0;
 7072:   z-index: 2;
 7073: }
 7074: 
 7075: ol.LC_primary_menu li:hover li, ol.LC_primary_menu li.hover li {
 7076: /* First Submenu -> size should be smaller than the menu title of the whole menu */
 7077:   font-size: 90%;
 7078:   vertical-align: top;
 7079:   float: none;
 7080:   border-left: 1px solid black;
 7081:   border-right: 1px solid black;
 7082: /* A dark bottom border to visualize different menu options; 
 7083: overwritten in the create_submenu routine for the last border-bottom of the menu */
 7084:   border-bottom: 1px solid $data_table_dark; 
 7085: }
 7086: 
 7087: ol.LC_primary_menu li li p:hover {
 7088:   color:$button_hover;
 7089:   text-decoration:none;
 7090:   background-color:$data_table_dark;
 7091: }
 7092: 
 7093: ol.LC_primary_menu li li a:hover {
 7094:    color:$button_hover;
 7095:    background-color:$data_table_dark;
 7096: }
 7097: 
 7098: /* Font-size equal to the size of the predecessors*/
 7099: ol.LC_primary_menu li:hover li li {
 7100:   font-size: 100%;
 7101: }
 7102: 
 7103: ol.LC_primary_menu li img {
 7104:   vertical-align: bottom;
 7105:   height: 1.1em;
 7106:   margin: 0.2em 0 0 0;
 7107: }
 7108: 
 7109: ol.LC_primary_menu a {
 7110:   color: RGB(80, 80, 80);
 7111:   text-decoration: none;
 7112: }
 7113: 
 7114: ol.LC_primary_menu a.LC_new_message {
 7115:   font-weight:bold;
 7116:   color: darkred;
 7117: }
 7118: 
 7119: ol.LC_docs_parameters {
 7120:   margin-left: 0;
 7121:   padding: 0;
 7122:   list-style: none;
 7123: }
 7124: 
 7125: ol.LC_docs_parameters li {
 7126:   margin: 0;
 7127:   padding-right: 20px;
 7128:   display: inline;
 7129: }
 7130: 
 7131: ol.LC_docs_parameters li:before {
 7132:   content: "\\002022 \\0020";
 7133: }
 7134: 
 7135: li.LC_docs_parameters_title {
 7136:   font-weight: bold;
 7137: }
 7138: 
 7139: ol.LC_docs_parameters li.LC_docs_parameters_title:before {
 7140:   content: "";
 7141: }
 7142: 
 7143: ul#LC_secondary_menu {
 7144:   clear: right;
 7145:   color: $fontmenu;
 7146:   background: $tabbg;
 7147:   list-style: none;
 7148:   padding: 0;
 7149:   margin: 0;
 7150:   width: 100%;
 7151:   text-align: left;
 7152:   float: left;
 7153: }
 7154: 
 7155: ul#LC_secondary_menu li {
 7156:   font-weight: bold;
 7157:   line-height: 1.8em;
 7158:   border-right: 1px solid black;
 7159:   float: left;
 7160: }
 7161: 
 7162: ul#LC_secondary_menu li.LC_hoverable:hover, ul#LC_secondary_menu li.hover {
 7163:   background-color: $data_table_light;
 7164: }
 7165: 
 7166: ul#LC_secondary_menu li a {
 7167:   padding: 0 0.8em;
 7168: }
 7169: 
 7170: ul#LC_secondary_menu li ul {
 7171:   display: none;
 7172: }
 7173: 
 7174: ul#LC_secondary_menu li:hover ul, ul#LC_secondary_menu li.hover ul {
 7175:   display: block;
 7176:   position: absolute;
 7177:   margin: 0;
 7178:   padding: 0;
 7179:   list-style:none;
 7180:   float: none;
 7181:   background-color: $data_table_light;
 7182:   z-index: 2;
 7183:   margin-left: -1px;
 7184: }
 7185: 
 7186: ul#LC_secondary_menu li ul li {
 7187:   font-size: 90%;
 7188:   vertical-align: top;
 7189:   border-left: 1px solid black;
 7190:   border-right: 1px solid black;
 7191:   background-color: $data_table_light;
 7192:   list-style:none;
 7193:   float: none;
 7194: }
 7195: 
 7196: ul#LC_secondary_menu li ul li:hover, ul#LC_secondary_menu li ul li.hover {
 7197:   background-color: $data_table_dark;
 7198: }
 7199: 
 7200: ul.LC_TabContent {
 7201:   display:block;
 7202:   background: $sidebg;
 7203:   border-bottom: solid 1px $lg_border_color;
 7204:   list-style:none;
 7205:   margin: -1px -10px 0 -10px;
 7206:   padding: 0;
 7207: }
 7208: 
 7209: ul.LC_TabContent li,
 7210: ul.LC_TabContentBigger li {
 7211:   float:left;
 7212: }
 7213: 
 7214: ul#LC_secondary_menu li a {
 7215:   color: $fontmenu;
 7216:   text-decoration: none;
 7217: }
 7218: 
 7219: ul.LC_TabContent {
 7220:   min-height:20px;
 7221: }
 7222: 
 7223: ul.LC_TabContent li {
 7224:   vertical-align:middle;
 7225:   padding: 0 16px 0 10px;
 7226:   background-color:$tabbg;
 7227:   border-bottom:solid 1px $lg_border_color;
 7228:   border-left: solid 1px $font;
 7229: }
 7230: 
 7231: ul.LC_TabContent .right {
 7232:   float:right;
 7233: }
 7234: 
 7235: ul.LC_TabContent li a,
 7236: ul.LC_TabContent li {
 7237:   color:rgb(47,47,47);
 7238:   text-decoration:none;
 7239:   font-size:95%;
 7240:   font-weight:bold;
 7241:   min-height:20px;
 7242: }
 7243: 
 7244: ul.LC_TabContent li a:hover,
 7245: ul.LC_TabContent li a:focus {
 7246:   color: $button_hover;
 7247:   background:none;
 7248:   outline:none;
 7249: }
 7250: 
 7251: ul.LC_TabContent li:hover {
 7252:   color: $button_hover;
 7253:   cursor:pointer;
 7254: }
 7255: 
 7256: ul.LC_TabContent li.active {
 7257:   color: $font;
 7258:   background:#FFFFFF url(/adm/lonIcons/open.gif) no-repeat scroll right center;
 7259:   border-bottom:solid 1px #FFFFFF;
 7260:   cursor: default;
 7261: }
 7262: 
 7263: ul.LC_TabContent li.active a {
 7264:   color:$font;
 7265:   background:#FFFFFF;
 7266:   outline: none;
 7267: }
 7268: 
 7269: ul.LC_TabContent li.goback {
 7270:   float: left;
 7271:   border-left: none;
 7272: }
 7273: 
 7274: #maincoursedoc {
 7275:   clear:both;
 7276: }
 7277: 
 7278: ul.LC_TabContentBigger {
 7279:   display:block;
 7280:   list-style:none;
 7281:   padding: 0;
 7282: }
 7283: 
 7284: ul.LC_TabContentBigger li {
 7285:   vertical-align:bottom;
 7286:   height: 30px;
 7287:   font-size:110%;
 7288:   font-weight:bold;
 7289:   color: #737373;
 7290: }
 7291: 
 7292: ul.LC_TabContentBigger li.active {
 7293:   position: relative;
 7294:   top: 1px;
 7295: }
 7296: 
 7297: ul.LC_TabContentBigger li a {
 7298:   background:url('/adm/lonIcons/tabbgleft.gif') left bottom no-repeat;
 7299:   height: 30px;
 7300:   line-height: 30px;
 7301:   text-align: center;
 7302:   display: block;
 7303:   text-decoration: none;
 7304:   outline: none;  
 7305: }
 7306: 
 7307: ul.LC_TabContentBigger li.active a {
 7308:   background:url('/adm/lonIcons/tabbgleft.gif') left top no-repeat;
 7309:   color:$font;
 7310: }
 7311: 
 7312: ul.LC_TabContentBigger li b {
 7313:   background: url('/adm/lonIcons/tabbgright.gif') no-repeat right bottom;
 7314:   display: block;
 7315:   float: left;
 7316:   padding: 0 30px;
 7317:   border-bottom: 1px solid $lg_border_color;
 7318: }
 7319: 
 7320: ul.LC_TabContentBigger li:hover b {
 7321:   color:$button_hover;
 7322: }
 7323: 
 7324: ul.LC_TabContentBigger li.active b {
 7325:   background:url('/adm/lonIcons/tabbgright.gif') right top no-repeat;
 7326:   color:$font;
 7327:   border: 0;
 7328: }
 7329: 
 7330: 
 7331: ul.LC_CourseBreadcrumbs {
 7332:   background: $sidebg;
 7333:   height: 2em;
 7334:   padding-left: 10px;
 7335:   margin: 0;
 7336:   list-style-position: inside;
 7337: }
 7338: 
 7339: ol#LC_MenuBreadcrumbs,
 7340: ol#LC_PathBreadcrumbs {
 7341:   padding-left: 10px;
 7342:   margin: 0;
 7343:   height: 2.5em;  /* equal to #LC_breadcrumbs line-height */
 7344: }
 7345: 
 7346: ol#LC_MenuBreadcrumbs li,
 7347: ol#LC_PathBreadcrumbs li,
 7348: ul.LC_CourseBreadcrumbs li {
 7349:   display: inline;
 7350:   white-space: normal;  
 7351: }
 7352: 
 7353: ol#LC_MenuBreadcrumbs li a,
 7354: ul.LC_CourseBreadcrumbs li a {
 7355:   text-decoration: none;
 7356:   font-size:90%;
 7357: }
 7358: 
 7359: ol#LC_MenuBreadcrumbs h1 {
 7360:   display: inline;
 7361:   font-size: 90%;
 7362:   line-height: 2.5em;
 7363:   margin: 0;
 7364:   padding: 0;
 7365: }
 7366: 
 7367: ol#LC_PathBreadcrumbs li a {
 7368:   text-decoration:none;
 7369:   font-size:100%;
 7370:   font-weight:bold;
 7371: }
 7372: 
 7373: .LC_Box {
 7374:   border: solid 1px $lg_border_color;
 7375:   padding: 0 10px 10px 10px;
 7376: }
 7377: 
 7378: .LC_DocsBox {
 7379:   border: solid 1px $lg_border_color;
 7380:   padding: 0 0 10px 10px;
 7381: }
 7382: 
 7383: .LC_AboutMe_Image {
 7384:   float:left;
 7385:   margin-right:10px;
 7386: }
 7387: 
 7388: .LC_Clear_AboutMe_Image {
 7389:   clear:left;
 7390: }
 7391: 
 7392: dl.LC_ListStyleClean dt {
 7393:   padding-right: 5px;
 7394:   display: table-header-group;
 7395: }
 7396: 
 7397: dl.LC_ListStyleClean dd {
 7398:   display: table-row;
 7399: }
 7400: 
 7401: .LC_ListStyleClean,
 7402: .LC_ListStyleSimple,
 7403: .LC_ListStyleNormal,
 7404: .LC_ListStyleSpecial {
 7405:   /* display:block; */
 7406:   list-style-position: inside;
 7407:   list-style-type: none;
 7408:   overflow: hidden;
 7409:   padding: 0;
 7410: }
 7411: 
 7412: .LC_ListStyleSimple li,
 7413: .LC_ListStyleSimple dd,
 7414: .LC_ListStyleNormal li,
 7415: .LC_ListStyleNormal dd,
 7416: .LC_ListStyleSpecial li,
 7417: .LC_ListStyleSpecial dd {
 7418:   margin: 0;
 7419:   padding: 5px 5px 5px 10px;
 7420:   clear: both;
 7421: }
 7422: 
 7423: .LC_ListStyleClean li,
 7424: .LC_ListStyleClean dd {
 7425:   padding-top: 0;
 7426:   padding-bottom: 0;
 7427: }
 7428: 
 7429: .LC_ListStyleSimple dd,
 7430: .LC_ListStyleSimple li {
 7431:   border-bottom: solid 1px $lg_border_color;
 7432: }
 7433: 
 7434: .LC_ListStyleSpecial li,
 7435: .LC_ListStyleSpecial dd {
 7436:   list-style-type: none;
 7437:   background-color: RGB(220, 220, 220);
 7438:   margin-bottom: 4px;
 7439: }
 7440: 
 7441: table.LC_SimpleTable {
 7442:   margin:5px;
 7443:   border:solid 1px $lg_border_color;
 7444: }
 7445: 
 7446: table.LC_SimpleTable tr {
 7447:   padding: 0;
 7448:   border:solid 1px $lg_border_color;
 7449: }
 7450: 
 7451: table.LC_SimpleTable thead {
 7452:   background:rgb(220,220,220);
 7453: }
 7454: 
 7455: div.LC_columnSection {
 7456:   display: block;
 7457:   clear: both;
 7458:   overflow: hidden;
 7459:   margin: 0;
 7460: }
 7461: 
 7462: div.LC_columnSection>* {
 7463:   float: left;
 7464:   margin: 10px 20px 10px 0;
 7465:   overflow:hidden;
 7466: }
 7467: 
 7468: table em {
 7469:   font-weight: bold;
 7470:   font-style: normal;
 7471: }
 7472: 
 7473: table.LC_tableBrowseRes,
 7474: table.LC_tableOfContent {
 7475:   border:none;
 7476:   border-spacing: 1px;
 7477:   padding: 3px;
 7478:   background-color: #FFFFFF;
 7479:   font-size: 90%;
 7480: }
 7481: 
 7482: table.LC_tableOfContent {
 7483:   border-collapse: collapse;
 7484: }
 7485: 
 7486: table.LC_tableBrowseRes a,
 7487: table.LC_tableOfContent a {
 7488:   background-color: transparent;
 7489:   text-decoration: none;
 7490: }
 7491: 
 7492: table.LC_tableOfContent img {
 7493:   border: none;
 7494:   height: 1.3em;
 7495:   vertical-align: text-bottom;
 7496:   margin-right: 0.3em;
 7497: }
 7498: 
 7499: a#LC_content_toolbar_firsthomework {
 7500:   background-image:url(/res/adm/pages/open-first-problem.gif);
 7501: }
 7502: 
 7503: a#LC_content_toolbar_everything {
 7504:   background-image:url(/res/adm/pages/show-all.gif);
 7505: }
 7506: 
 7507: a#LC_content_toolbar_uncompleted {
 7508:   background-image:url(/res/adm/pages/show-incomplete-problems.gif);
 7509: }
 7510: 
 7511: #LC_content_toolbar_clearbubbles {
 7512:   background-image:url(/res/adm/pages/mark-discussionentries-read.gif);
 7513: }
 7514: 
 7515: a#LC_content_toolbar_changefolder {
 7516:   background : url(/res/adm/pages/close-all-folders.gif) top center ;
 7517: }
 7518: 
 7519: a#LC_content_toolbar_changefolder_toggled {
 7520:   background-image:url(/res/adm/pages/open-all-folders.gif);
 7521: }
 7522: 
 7523: a#LC_content_toolbar_edittoplevel {
 7524:   background-image:url(/res/adm/pages/edittoplevel.gif);
 7525: }
 7526: 
 7527: ul#LC_toolbar li a:hover {
 7528:   background-position: bottom center;
 7529: }
 7530: 
 7531: ul#LC_toolbar {
 7532:   padding: 0;
 7533:   margin: 2px;
 7534:   list-style:none;
 7535:   position:relative;
 7536:   background-color:white;
 7537:   overflow: auto;
 7538: }
 7539: 
 7540: ul#LC_toolbar li {
 7541:   border:1px solid white;
 7542:   padding: 0;
 7543:   margin: 0;
 7544:   float: left;
 7545:   display:inline;
 7546:   vertical-align:middle;
 7547:   white-space: nowrap;
 7548: }
 7549: 
 7550: 
 7551: a.LC_toolbarItem {
 7552:   display:block;
 7553:   padding: 0;
 7554:   margin: 0;
 7555:   height: 32px;
 7556:   width: 32px;
 7557:   color:white;
 7558:   border: none;
 7559:   background-repeat:no-repeat;
 7560:   background-color:transparent;
 7561: }
 7562: 
 7563: ul.LC_funclist {
 7564:     margin: 0;
 7565:     padding: 0.5em 1em 0.5em 0;
 7566: }
 7567: 
 7568: ul.LC_funclist > li:first-child {
 7569:     font-weight:bold; 
 7570:     margin-left:0.8em;
 7571: }
 7572: 
 7573: ul.LC_funclist + ul.LC_funclist {
 7574:     /* 
 7575:        left border as a seperator if we have more than
 7576:        one list 
 7577:     */
 7578:     border-left: 1px solid $sidebg;
 7579:     /* 
 7580:        this hides the left border behind the border of the 
 7581:        outer box if element is wrapped to the next 'line' 
 7582:     */
 7583:     margin-left: -1px;
 7584: }
 7585: 
 7586: ul.LC_funclist li {
 7587:   display: inline;
 7588:   white-space: nowrap;
 7589:   margin: 0 0 0 25px;
 7590:   line-height: 150%;
 7591: }
 7592: 
 7593: .LC_hidden {
 7594:   display: none;
 7595: }
 7596: 
 7597: .LCmodal-overlay {
 7598: 		position:fixed;
 7599: 		top:0;
 7600: 		right:0;
 7601: 		bottom:0;
 7602: 		left:0;
 7603: 		height:100%;
 7604: 		width:100%;
 7605: 		margin:0;
 7606: 		padding:0;
 7607: 		background:#999;
 7608: 		opacity:.75;
 7609: 		filter: alpha(opacity=75);
 7610: 		-moz-opacity: 0.75;
 7611: 		z-index:101;
 7612: }
 7613: 
 7614: * html .LCmodal-overlay {   
 7615: 		position: absolute;
 7616: 		height: expression(document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + 'px');
 7617: }
 7618: 
 7619: .LCmodal-window {
 7620: 		position:fixed;
 7621: 		top:50%;
 7622: 		left:50%;
 7623: 		margin:0;
 7624: 		padding:0;
 7625: 		z-index:102;
 7626: 	}
 7627: 
 7628: * html .LCmodal-window {
 7629: 		position:absolute;
 7630: }
 7631: 
 7632: .LCclose-window {
 7633: 		position:absolute;
 7634: 		width:32px;
 7635: 		height:32px;
 7636: 		right:8px;
 7637: 		top:8px;
 7638: 		background:transparent url('/res/adm/pages/process-stop.png') no-repeat scroll right top;
 7639: 		text-indent:-99999px;
 7640: 		overflow:hidden;
 7641: 		cursor:pointer;
 7642: }
 7643: 
 7644: /*
 7645:   styles used by TTH when "Default set of options to pass to tth/m
 7646:   when converting TeX" in course settings has been set
 7647: 
 7648:   option passed: -t
 7649: 
 7650: */
 7651: 
 7652: td div.comp { margin-top: -0.6ex; margin-bottom: -1ex;}
 7653: td div.comb { margin-top: -0.6ex; margin-bottom: -.6ex;}
 7654: td div.hrcomp { line-height: 0.9; margin-top: -0.8ex; margin-bottom: -1ex;}
 7655: td div.norm {line-height:normal;}
 7656: 
 7657: /*
 7658:   option passed -y3
 7659: */
 7660: 
 7661: span.roman {font-family: serif; font-style: normal; font-weight: normal;}
 7662: span.overacc2 {position: relative;  left: .8em; top: -1.2ex;}
 7663: span.overacc1 {position: relative;  left: .6em; top: -1.2ex;}
 7664: 
 7665: END
 7666: }
 7667: 
 7668: =pod
 7669: 
 7670: =item * &headtag()
 7671: 
 7672: Returns a uniform footer for LON-CAPA web pages.
 7673: 
 7674: Inputs: $title - optional title for the head
 7675:         $head_extra - optional extra HTML to put inside the <head>
 7676:         $args - optional arguments
 7677:             force_register - if is true call registerurl so the remote is 
 7678:                              informed
 7679:             redirect       -> array ref of
 7680:                                    1- seconds before redirect occurs
 7681:                                    2- url to redirect to
 7682:                                    3- whether the side effect should occur
 7683:                            (side effect of setting 
 7684:                                $env{'internal.head.redirect'} to the url 
 7685:                                redirected too)
 7686:             domain         -> force to color decorate a page for a specific
 7687:                                domain
 7688:             function       -> force usage of a specific rolish color scheme
 7689:             bgcolor        -> override the default page bgcolor
 7690:             no_auto_mt_title
 7691:                            -> prevent &mt()ing the title arg
 7692: 
 7693: =cut
 7694: 
 7695: sub headtag {
 7696:     my ($title,$head_extra,$args) = @_;
 7697:     
 7698:     my $function = $args->{'function'} || &get_users_function();
 7699:     my $domain   = $args->{'domain'}   || &determinedomain();
 7700:     my $bgcolor  = $args->{'bgcolor'}  || &designparm($function.'.pgbg',$domain);
 7701:     my $httphost = $args->{'use_absolute'};
 7702:     my $url = join(':',$env{'user.name'},$env{'user.domain'},
 7703: 		   $Apache::lonnet::perlvar{'lonVersion'},
 7704: 		   #time(),
 7705: 		   $env{'environment.color.timestamp'},
 7706: 		   $function,$domain,$bgcolor);
 7707: 
 7708:     $url = '/adm/css/'.&escape($url).'.css';
 7709: 
 7710:     my $result =
 7711: 	'<head>'.
 7712: 	&font_settings($args);
 7713: 
 7714:     my $inhibitprint;
 7715:     if ($args->{'print_suppress'}) {
 7716:         $inhibitprint = &print_suppression();
 7717:     }
 7718: 
 7719:     if (!$args->{'frameset'}) {
 7720: 	$result .= &Apache::lonhtmlcommon::htmlareaheaders();
 7721:     }
 7722:     if ($args->{'force_register'} && $env{'request.noversionuri'} !~ m{^/res/adm/pages/}) {
 7723:         $result .= Apache::lonxml::display_title();
 7724:     }
 7725:     if (!$args->{'no_nav_bar'} 
 7726: 	&& !$args->{'only_body'}
 7727: 	&& !$args->{'frameset'}) {
 7728: 	$result .= &help_menu_js($httphost);
 7729:         $result.=&modal_window();
 7730:         $result.=&togglebox_script();
 7731:         $result.=&wishlist_window();
 7732:         $result.=&LCprogressbarUpdate_script();
 7733:     } else {
 7734:         if ($args->{'add_modal'}) {
 7735:            $result.=&modal_window();
 7736:         }
 7737:         if ($args->{'add_wishlist'}) {
 7738:            $result.=&wishlist_window();
 7739:         }
 7740:         if ($args->{'add_togglebox'}) {
 7741:            $result.=&togglebox_script();
 7742:         }
 7743:         if ($args->{'add_progressbar'}) {
 7744:            $result.=&LCprogressbarUpdate_script();
 7745:         }
 7746:     }
 7747:     if (ref($args->{'redirect'})) {
 7748: 	my ($time,$url,$inhibit_continue) = @{$args->{'redirect'}};
 7749: 	$url = &Apache::lonenc::check_encrypt($url);
 7750: 	if (!$inhibit_continue) {
 7751: 	    $env{'internal.head.redirect'} = $url;
 7752: 	}
 7753: 	$result.=<<ADDMETA
 7754: <meta http-equiv="pragma" content="no-cache" />
 7755: <meta http-equiv="Refresh" content="$time; url=$url" />
 7756: ADDMETA
 7757:     } else {
 7758:         unless (($args->{'frameset'}) || ($args->{'js_ready'}) || ($args->{'only_body'}) || ($args->{'no_nav_bar'})) {
 7759:             my $requrl = $env{'request.uri'};
 7760:             if ($requrl eq '') {
 7761:                 $requrl = $ENV{'REQUEST_URI'};
 7762:                 $requrl =~ s/\?.+$//;
 7763:             }
 7764:             unless (($requrl =~ m{^/adm/(?:switchserver|login|authenticate|logout|groupsort|cleanup|helper|slotrequest|grades)(\?|$)}) ||
 7765:                     (($requrl =~ m{^/res/}) && (($env{'form.submitted'} eq 'scantron') ||
 7766:                      ($env{'form.grade_symb'}) || ($Apache::lonhomework::scantronmode)))) {
 7767:                 my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
 7768:                 unless (&Apache::lonnet::allowed('mau',$dom_in_use)) {
 7769:                     my %domdefs = &Apache::lonnet::get_domain_defaults($dom_in_use);
 7770:                     if (ref($domdefs{'offloadnow'}) eq 'HASH') {
 7771:                         my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
 7772:                         if ($domdefs{'offloadnow'}{$lonhost}) {
 7773:                             my $newserver = &Apache::lonnet::spareserver(30000,undef,1,$dom_in_use);
 7774:                             if (($newserver) && ($newserver ne $lonhost)) {
 7775:                                 my $numsec = 5;
 7776:                                 my $timeout = $numsec * 1000;
 7777:                                 my ($newurl,$locknum,%locks,$msg);
 7778:                                 if ($env{'request.role.adv'}) {
 7779:                                     ($locknum,%locks) = &Apache::lonnet::get_locks();
 7780:                                 }
 7781:                                 my $disable_submit = 0;
 7782:                                 if ($requrl =~ /$LONCAPA::assess_re/) {
 7783:                                     $disable_submit = 1;
 7784:                                 }
 7785:                                 if ($locknum) {
 7786:                                     my @lockinfo = sort(values(%locks));
 7787:                                     $msg = &mt('Once the following tasks are complete: ')."\\n".
 7788:                                            join(", ",sort(values(%locks)))."\\n".
 7789:                                            &mt('your session will be transferred to a different server, after you click "Roles".');
 7790:                                 } else {
 7791:                                     if (($requrl =~ m{^/res/}) && ($env{'form.submitted'} =~ /^part_/)) {
 7792:                                         $msg = &mt('Your LON-CAPA submission has been recorded')."\\n";
 7793:                                     }
 7794:                                     $msg .= &mt('Your current LON-CAPA session will be transferred to a different server in [quant,_1,second].',$numsec);
 7795:                                     $newurl = '/adm/switchserver?otherserver='.$newserver;
 7796:                                     if (($env{'request.role'}) && ($env{'request.role'} ne 'cm')) {
 7797:                                         $newurl .= '&role='.$env{'request.role'};
 7798:                                     }
 7799:                                     if ($env{'request.symb'}) {
 7800:                                         $newurl .= '&symb='.$env{'request.symb'};
 7801:                                     } else {
 7802:                                         $newurl .= '&origurl='.$requrl;
 7803:                                     }
 7804:                                 }
 7805:                                 $result.=<<OFFLOAD
 7806: <meta http-equiv="pragma" content="no-cache" />
 7807: <script type="text/javascript">
 7808: // <![CDATA[
 7809: function LC_Offload_Now() {
 7810:     var dest = "$newurl";
 7811:     if (dest != '') {
 7812:         window.location.href="$newurl";
 7813:     }
 7814: }
 7815: \$(document).ready(function () {
 7816:     window.alert('$msg');
 7817:     if ($disable_submit) {
 7818:         \$(".LC_hwk_submit").prop("disabled", true);
 7819:         \$( ".LC_textline" ).prop( "readonly", "readonly");
 7820:     }
 7821:     setTimeout('LC_Offload_Now()', $timeout);
 7822: });
 7823: // ]]>
 7824: </script>
 7825: OFFLOAD
 7826:                             }
 7827:                         }
 7828:                     }
 7829:                 }
 7830:             }
 7831:         }
 7832:     }
 7833:     if (!defined($title)) {
 7834: 	$title = 'The LearningOnline Network with CAPA';
 7835:     }
 7836:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
 7837:     $result .= '<title> LON-CAPA '.$title.'</title>'
 7838: 	.'<link rel="stylesheet" type="text/css" href="'.$url.'"';
 7839:     if (!$args->{'frameset'}) {
 7840:         $result .= ' /';
 7841:     }
 7842:     $result .= '>' 
 7843:         .$inhibitprint
 7844: 	.$head_extra;
 7845:     if ($env{'browser.mobile'}) {
 7846:         $result .= '
 7847: <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
 7848: <meta name="apple-mobile-web-app-capable" content="yes" />';
 7849:     }
 7850:     return $result.'</head>';
 7851: }
 7852: 
 7853: =pod
 7854: 
 7855: =item * &font_settings()
 7856: 
 7857: Returns neccessary <meta> to set the proper encoding
 7858: 
 7859: Inputs: optional reference to HASH -- $args passed to &headtag()
 7860: 
 7861: =cut
 7862: 
 7863: sub font_settings {
 7864:     my ($args) = @_;
 7865:     my $headerstring='';
 7866:     if ((!$env{'browser.mathml'} && $env{'browser.unicode'}) ||
 7867:         ((ref($args) eq 'HASH') && ($args->{'browser.unicode'}))) {
 7868:         $headerstring.=
 7869:             '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"';
 7870:         if (!$args->{'frameset'}) {
 7871: 	    $headerstring.= ' /';
 7872:         }
 7873: 	$headerstring .= '>'."\n";
 7874:     }
 7875:     return $headerstring;
 7876: }
 7877: 
 7878: =pod
 7879: 
 7880: =item * &print_suppression()
 7881: 
 7882: In course context returns css which causes the body to be blank when media="print",
 7883: if printout generation is unavailable for the current resource.
 7884: 
 7885: This could be because:
 7886: 
 7887: (a) printstartdate is in the future
 7888: 
 7889: (b) printenddate is in the past
 7890: 
 7891: (c) there is an active exam block with "printout"
 7892: functionality blocked
 7893: 
 7894: Users with pav, pfo or evb privileges are exempt.
 7895: 
 7896: Inputs: none
 7897: 
 7898: =cut
 7899: 
 7900: 
 7901: sub print_suppression {
 7902:     my $noprint;
 7903:     if ($env{'request.course.id'}) {
 7904:         my $scope = $env{'request.course.id'};
 7905:         if ((&Apache::lonnet::allowed('pav',$scope)) ||
 7906:             (&Apache::lonnet::allowed('pfo',$scope))) {
 7907:             return;
 7908:         }
 7909:         if ($env{'request.course.sec'} ne '') {
 7910:             $scope .= "/$env{'request.course.sec'}";
 7911:             if ((&Apache::lonnet::allowed('pav',$scope)) ||
 7912:                 (&Apache::lonnet::allowed('pfo',$scope))) {
 7913:                 return;
 7914:             }
 7915:         }
 7916:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7917:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7918:         my $blocked = &blocking_status('printout',$cnum,$cdom,undef,1);
 7919:         if ($blocked) {
 7920:             my $checkrole = "cm./$cdom/$cnum";
 7921:             if ($env{'request.course.sec'} ne '') {
 7922:                 $checkrole .= "/$env{'request.course.sec'}";
 7923:             }
 7924:             unless ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
 7925:                     ($env{'request.role'} !~ m{^st\./$cdom/$cnum})) {
 7926:                 $noprint = 1;
 7927:             }
 7928:         }
 7929:         unless ($noprint) {
 7930:             my $symb = &Apache::lonnet::symbread();
 7931:             if ($symb ne '') {
 7932:                 my $navmap = Apache::lonnavmaps::navmap->new();
 7933:                 if (ref($navmap)) {
 7934:                     my $res = $navmap->getBySymb($symb);
 7935:                     if (ref($res)) {
 7936:                         if (!$res->resprintable()) {
 7937:                             $noprint = 1;
 7938:                         }
 7939:                     }
 7940:                 }
 7941:             }
 7942:         }
 7943:         if ($noprint) {
 7944:             return <<"ENDSTYLE";
 7945: <style type="text/css" media="print">
 7946:     body { display:none }
 7947: </style>
 7948: ENDSTYLE
 7949:         }
 7950:     }
 7951:     return;
 7952: }
 7953: 
 7954: =pod
 7955: 
 7956: =item * &xml_begin()
 7957: 
 7958: Returns the needed doctype and <html>
 7959: 
 7960: Inputs: none
 7961: 
 7962: =cut
 7963: 
 7964: sub xml_begin {
 7965:     my ($is_frameset) = @_;
 7966:     my $output='';
 7967: 
 7968:     if ($env{'browser.mathml'}) {
 7969: 	$output='<?xml version="1.0"?>'
 7970:             #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
 7971: #            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
 7972:             
 7973: #	    .'<!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">] >'
 7974: 	    .'<!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">'
 7975:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
 7976: 	    .'xmlns="http://www.w3.org/1999/xhtml">';
 7977:     } elsif ($is_frameset) {
 7978:         $output='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'."\n".
 7979:                 '<html>'."\n";
 7980:     } else {
 7981: 	$output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n".
 7982:                 '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'."\n";
 7983:     }
 7984:     return $output;
 7985: }
 7986: 
 7987: =pod
 7988: 
 7989: =item * &start_page()
 7990: 
 7991: Returns a complete <html> .. <body> section for LON-CAPA web pages.
 7992: 
 7993: Inputs:
 7994: 
 7995: =over 4
 7996: 
 7997: $title - optional title for the page
 7998: 
 7999: $head_extra - optional extra HTML to incude inside the <head>
 8000: 
 8001: $args - additional optional args supported are:
 8002: 
 8003: =over 8
 8004: 
 8005:              only_body      -> is true will set &bodytag() onlybodytag
 8006:                                     arg on
 8007:              no_nav_bar     -> is true will set &bodytag() no_nav_bar arg on
 8008:              add_entries    -> additional attributes to add to the  <body>
 8009:              domain         -> force to color decorate a page for a 
 8010:                                     specific domain
 8011:              function       -> force usage of a specific rolish color
 8012:                                     scheme
 8013:              redirect       -> see &headtag()
 8014:              bgcolor        -> override the default page bg color
 8015:              js_ready       -> return a string ready for being used in 
 8016:                                     a javascript writeln
 8017:              html_encode    -> return a string ready for being used in 
 8018:                                     a html attribute
 8019:              force_register -> if is true will turn on the &bodytag()
 8020:                                     $forcereg arg
 8021:              frameset       -> if true will start with a <frameset>
 8022:                                     rather than <body>
 8023:              skip_phases    -> hash ref of 
 8024:                                     head -> skip the <html><head> generation
 8025:                                     body -> skip all <body> generation
 8026:              no_auto_mt_title -> prevent &mt()ing the title arg
 8027:              inherit_jsmath -> when creating popup window in a page,
 8028:                                     should it have jsmath forced on by the
 8029:                                     current page
 8030:              bread_crumbs ->             Array containing breadcrumbs
 8031:              bread_crumbs_component ->  if exists show it as headline else show only the breadcrumbs
 8032:              group          -> includes the current group, if page is for a 
 8033:                                specific group  
 8034: 
 8035: =back
 8036: 
 8037: =back
 8038: 
 8039: =cut
 8040: 
 8041: sub start_page {
 8042:     my ($title,$head_extra,$args) = @_;
 8043:     #&Apache::lonnet::logthis("start_page ".join(':',caller(0)));
 8044: 
 8045:     $env{'internal.start_page'}++;
 8046:     my ($result,@advtools);
 8047: 
 8048:     if (! exists($args->{'skip_phases'}{'head'}) ) {
 8049:         $result .= &xml_begin($args->{'frameset'}) . &headtag($title, $head_extra, $args);
 8050:     }
 8051:     
 8052:     if (! exists($args->{'skip_phases'}{'body'}) ) {
 8053: 	if ($args->{'frameset'}) {
 8054: 	    my $attr_string = &make_attr_string($args->{'force_register'},
 8055: 						$args->{'add_entries'});
 8056: 	    $result .= "\n<frameset $attr_string>\n";
 8057:         } else {
 8058:             $result .=
 8059:                 &bodytag($title, 
 8060:                          $args->{'function'},       $args->{'add_entries'},
 8061:                          $args->{'only_body'},      $args->{'domain'},
 8062:                          $args->{'force_register'}, $args->{'no_nav_bar'},
 8063:                          $args->{'bgcolor'},        $args,
 8064:                          \@advtools);
 8065:         }
 8066:     }
 8067: 
 8068:     if ($args->{'js_ready'}) {
 8069: 		$result = &js_ready($result);
 8070:     }
 8071:     if ($args->{'html_encode'}) {
 8072: 		$result = &html_encode($result);
 8073:     }
 8074: 
 8075:     # Preparation for new and consistent functionlist at top of screen
 8076:     # if ($args->{'functionlist'}) {
 8077:     #            $result .= &build_functionlist();
 8078:     #}
 8079: 
 8080:     # Don't add anything more if only_body wanted or in const space
 8081:     return $result if    $args->{'only_body'} 
 8082:                       || $env{'request.state'} eq 'construct';
 8083: 
 8084:     #Breadcrumbs
 8085:     if (exists($args->{'bread_crumbs'}) or exists($args->{'bread_crumbs_component'})) {
 8086: 		&Apache::lonhtmlcommon::clear_breadcrumbs();
 8087: 		#if any br links exists, add them to the breadcrumbs
 8088: 		if (exists($args->{'bread_crumbs'}) and ref($args->{'bread_crumbs'}) eq 'ARRAY') {         
 8089: 			foreach my $crumb (@{$args->{'bread_crumbs'}}){
 8090: 				&Apache::lonhtmlcommon::add_breadcrumb($crumb);
 8091: 			}
 8092: 		}
 8093:                 # if @advtools array contains items add then to the breadcrumbs
 8094:                 if (@advtools > 0) {
 8095:                     &Apache::lonmenu::advtools_crumbs(@advtools);
 8096:                 }
 8097: 
 8098: 		#if bread_crumbs_component exists show it as headline else show only the breadcrumbs
 8099: 		if(exists($args->{'bread_crumbs_component'})){
 8100: 			$result .= &Apache::lonhtmlcommon::breadcrumbs($args->{'bread_crumbs_component'});
 8101: 		}else{
 8102: 			$result .= &Apache::lonhtmlcommon::breadcrumbs();
 8103: 		}
 8104:     }
 8105:     return $result;
 8106: }
 8107: 
 8108: sub end_page {
 8109:     my ($args) = @_;
 8110:     $env{'internal.end_page'}++;
 8111:     my $result;
 8112:     if ($args->{'discussion'}) {
 8113: 	my ($target,$parser);
 8114: 	if (ref($args->{'discussion'})) {
 8115: 	    ($target,$parser) =($args->{'discussion'}{'target'},
 8116: 				$args->{'discussion'}{'parser'});
 8117: 	}
 8118: 	$result .= &Apache::lonxml::xmlend($target,$parser);
 8119:     }
 8120:     if ($args->{'frameset'}) {
 8121: 	$result .= '</frameset>';
 8122:     } else {
 8123: 	$result .= &endbodytag($args);
 8124:     }
 8125:     unless ($args->{'notbody'}) {
 8126:         $result .= "\n</html>";
 8127:     }
 8128: 
 8129:     if ($args->{'js_ready'}) {
 8130: 	$result = &js_ready($result);
 8131:     }
 8132: 
 8133:     if ($args->{'html_encode'}) {
 8134: 	$result = &html_encode($result);
 8135:     }
 8136: 
 8137:     return $result;
 8138: }
 8139: 
 8140: sub wishlist_window {
 8141:     return(<<'ENDWISHLIST');
 8142: <script type="text/javascript">
 8143: // <![CDATA[
 8144: // <!-- BEGIN LON-CAPA Internal
 8145: function set_wishlistlink(title, path) {
 8146:     if (!title) {
 8147:         title = document.title;
 8148:         title = title.replace(/^LON-CAPA /,'');
 8149:     }
 8150:     title = encodeURIComponent(title);
 8151:     title = title.replace("'","\\\'");
 8152:     if (!path) {
 8153:         path = location.pathname;
 8154:     }
 8155:     path = encodeURIComponent(path);
 8156:     path = path.replace("'","\\\'");
 8157:     Win = window.open('/adm/wishlist?mode=newLink&setTitle='+title+'&setPath='+path,
 8158:                       'wishlistNewLink','width=560,height=350,scrollbars=0');
 8159: }
 8160: // END LON-CAPA Internal -->
 8161: // ]]>
 8162: </script>
 8163: ENDWISHLIST
 8164: }
 8165: 
 8166: sub modal_window {
 8167:     return(<<'ENDMODAL');
 8168: <script type="text/javascript">
 8169: // <![CDATA[
 8170: // <!-- BEGIN LON-CAPA Internal
 8171: var modalWindow = {
 8172: 	parent:"body",
 8173: 	windowId:null,
 8174: 	content:null,
 8175: 	width:null,
 8176: 	height:null,
 8177: 	close:function()
 8178: 	{
 8179: 	        $(".LCmodal-window").remove();
 8180: 	        $(".LCmodal-overlay").remove();
 8181: 	},
 8182: 	open:function()
 8183: 	{
 8184: 		var modal = "";
 8185: 		modal += "<div class=\"LCmodal-overlay\"></div>";
 8186: 		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;\">";
 8187: 		modal += this.content;
 8188: 		modal += "</div>";	
 8189: 
 8190: 		$(this.parent).append(modal);
 8191: 
 8192: 		$(".LCmodal-window").append("<a class=\"LCclose-window\"></a>");
 8193: 		$(".LCclose-window").click(function(){modalWindow.close();});
 8194: 		$(".LCmodal-overlay").click(function(){modalWindow.close();});
 8195: 	}
 8196: };
 8197: 	var openMyModal = function(source,width,height,scrolling,transparency,style)
 8198: 	{
 8199:                 source = source.replace("'","&#39;");
 8200: 		modalWindow.windowId = "myModal";
 8201: 		modalWindow.width = width;
 8202: 		modalWindow.height = height;
 8203: 		modalWindow.content = "<iframe width='"+width+"' height='"+height+"' frameborder='0' scrolling='"+scrolling+"' allowtransparency='"+transparency+"' src='" + source + "' style='"+style+"'></iframe>";
 8204: 		modalWindow.open();
 8205: 	};
 8206: // END LON-CAPA Internal -->
 8207: // ]]>
 8208: </script>
 8209: ENDMODAL
 8210: }
 8211: 
 8212: sub modal_link {
 8213:     my ($link,$linktext,$width,$height,$target,$scrolling,$title,$transparency,$style)=@_;
 8214:     unless ($width) { $width=480; }
 8215:     unless ($height) { $height=400; }
 8216:     unless ($scrolling) { $scrolling='yes'; }
 8217:     unless ($transparency) { $transparency='true'; }
 8218: 
 8219:     my $target_attr;
 8220:     if (defined($target)) {
 8221:         $target_attr = 'target="'.$target.'"';
 8222:     }
 8223:     return <<"ENDLINK";
 8224: <a href="$link" $target_attr title="$title" onclick="javascript:openMyModal('$link',$width,$height,'$scrolling','$transparency','$style'); return false;">
 8225:            $linktext</a>
 8226: ENDLINK
 8227: }
 8228: 
 8229: sub modal_adhoc_script {
 8230:     my ($funcname,$width,$height,$content)=@_;
 8231:     return (<<ENDADHOC);
 8232: <script type="text/javascript">
 8233: // <![CDATA[
 8234:         var $funcname = function()
 8235:         {
 8236:                 modalWindow.windowId = "myModal";
 8237:                 modalWindow.width = $width;
 8238:                 modalWindow.height = $height;
 8239:                 modalWindow.content = '$content';
 8240:                 modalWindow.open();
 8241:         };  
 8242: // ]]>
 8243: </script>
 8244: ENDADHOC
 8245: }
 8246: 
 8247: sub modal_adhoc_inner {
 8248:     my ($funcname,$width,$height,$content)=@_;
 8249:     my $innerwidth=$width-20;
 8250:     $content=&js_ready(
 8251:                  &start_page('Dialog',undef,{'only_body'=>1,'bgcolor'=>'#FFFFFF'}).
 8252:                  &start_scrollbox($width.'px',$innerwidth.'px',$height.'px','myModal','#FFFFFF',undef,1).
 8253:                  $content.
 8254:                  &end_scrollbox().
 8255:                  &end_page()
 8256:              );
 8257:     return &modal_adhoc_script($funcname,$width,$height,$content);
 8258: }
 8259: 
 8260: sub modal_adhoc_window {
 8261:     my ($funcname,$width,$height,$content,$linktext)=@_;
 8262:     return &modal_adhoc_inner($funcname,$width,$height,$content).
 8263:            "<a href=\"javascript:$funcname();void(0);\">".$linktext."</a>";
 8264: }
 8265: 
 8266: sub modal_adhoc_launch {
 8267:     my ($funcname,$width,$height,$content)=@_;
 8268:     return &modal_adhoc_inner($funcname,$width,$height,$content).(<<ENDLAUNCH);
 8269: <script type="text/javascript">
 8270: // <![CDATA[
 8271: $funcname();
 8272: // ]]>
 8273: </script>
 8274: ENDLAUNCH
 8275: }
 8276: 
 8277: sub modal_adhoc_close {
 8278:     return (<<ENDCLOSE);
 8279: <script type="text/javascript">
 8280: // <![CDATA[
 8281: modalWindow.close();
 8282: // ]]>
 8283: </script>
 8284: ENDCLOSE
 8285: }
 8286: 
 8287: sub togglebox_script {
 8288:    return(<<ENDTOGGLE);
 8289: <script type="text/javascript"> 
 8290: // <![CDATA[
 8291: function LCtoggleDisplay(id,hidetext,showtext) {
 8292:    link = document.getElementById(id + "link").childNodes[0];
 8293:    with (document.getElementById(id).style) {
 8294:       if (display == "none" ) {
 8295:           display = "inline";
 8296:           link.nodeValue = hidetext;
 8297:         } else {
 8298:           display = "none";
 8299:           link.nodeValue = showtext;
 8300:        }
 8301:    }
 8302: }
 8303: // ]]>
 8304: </script>
 8305: ENDTOGGLE
 8306: }
 8307: 
 8308: sub start_togglebox {
 8309:     my ($id,$heading,$headerbg,$hidetext,$showtext)=@_;
 8310:     unless ($heading) { $heading=''; } else { $heading.=' '; }
 8311:     unless ($showtext) { $showtext=&mt('show'); }
 8312:     unless ($hidetext) { $hidetext=&mt('hide'); }
 8313:     unless ($headerbg) { $headerbg='#FFFFFF'; }
 8314:     return &start_data_table().
 8315:            &start_data_table_header_row().
 8316:            '<td bgcolor="'.$headerbg.'">'.$heading.
 8317:            '[<a id="'.$id.'link" href="javascript:LCtoggleDisplay(\''.$id.'\',\''.$hidetext.'\',\''.
 8318:            $showtext.'\')">'.$showtext.'</a>]</td>'.
 8319:            &end_data_table_header_row().
 8320:            '<tr id="'.$id.'" style="display:none""><td>';
 8321: }
 8322: 
 8323: sub end_togglebox {
 8324:     return '</td></tr>'.&end_data_table();
 8325: }
 8326: 
 8327: sub LCprogressbar_script {
 8328:    my ($id)=@_;
 8329:    return(<<ENDPROGRESS);
 8330: <script type="text/javascript">
 8331: // <![CDATA[
 8332: \$('#progressbar$id').progressbar({
 8333:   value: 0,
 8334:   change: function(event, ui) {
 8335:     var newVal = \$(this).progressbar('option', 'value');
 8336:     \$('.pblabel', this).text(LCprogressTxt);
 8337:   }
 8338: });
 8339: // ]]>
 8340: </script>
 8341: ENDPROGRESS
 8342: }
 8343: 
 8344: sub LCprogressbarUpdate_script {
 8345:    return(<<ENDPROGRESSUPDATE);
 8346: <style type="text/css">
 8347: .ui-progressbar { position:relative; }
 8348: .pblabel { position: absolute; width: 100%; text-align: center; line-height: 1.9em; }
 8349: </style>
 8350: <script type="text/javascript">
 8351: // <![CDATA[
 8352: var LCprogressTxt='---';
 8353: 
 8354: function LCupdateProgress(percent,progresstext,id) {
 8355:    LCprogressTxt=progresstext;
 8356:    \$('#progressbar'+id).progressbar('value',percent);
 8357: }
 8358: // ]]>
 8359: </script>
 8360: ENDPROGRESSUPDATE
 8361: }
 8362: 
 8363: my $LClastpercent;
 8364: my $LCidcnt;
 8365: my $LCcurrentid;
 8366: 
 8367: sub LCprogressbar {
 8368:     my ($r)=(@_);
 8369:     $LClastpercent=0;
 8370:     $LCidcnt++;
 8371:     $LCcurrentid=$$.'_'.$LCidcnt;
 8372:     my $starting=&mt('Starting');
 8373:     my $content=(<<ENDPROGBAR);
 8374:   <div id="progressbar$LCcurrentid">
 8375:     <span class="pblabel">$starting</span>
 8376:   </div>
 8377: ENDPROGBAR
 8378:     &r_print($r,$content.&LCprogressbar_script($LCcurrentid));
 8379: }
 8380: 
 8381: sub LCprogressbarUpdate {
 8382:     my ($r,$val,$text)=@_;
 8383:     unless ($val) { 
 8384:        if ($LClastpercent) {
 8385:            $val=$LClastpercent;
 8386:        } else {
 8387:            $val=0;
 8388:        }
 8389:     }
 8390:     if ($val<0) { $val=0; }
 8391:     if ($val>100) { $val=0; }
 8392:     $LClastpercent=$val;
 8393:     unless ($text) { $text=$val.'%'; }
 8394:     $text=&js_ready($text);
 8395:     &r_print($r,<<ENDUPDATE);
 8396: <script type="text/javascript">
 8397: // <![CDATA[
 8398: LCupdateProgress($val,'$text','$LCcurrentid');
 8399: // ]]>
 8400: </script>
 8401: ENDUPDATE
 8402: }
 8403: 
 8404: sub LCprogressbarClose {
 8405:     my ($r)=@_;
 8406:     $LClastpercent=0;
 8407:     &r_print($r,<<ENDCLOSE);
 8408: <script type="text/javascript">
 8409: // <![CDATA[
 8410: \$("#progressbar$LCcurrentid").hide('slow'); 
 8411: // ]]>
 8412: </script>
 8413: ENDCLOSE
 8414: }
 8415: 
 8416: sub r_print {
 8417:     my ($r,$to_print)=@_;
 8418:     if ($r) {
 8419:       $r->print($to_print);
 8420:       $r->rflush();
 8421:     } else {
 8422:       print($to_print);
 8423:     }
 8424: }
 8425: 
 8426: sub html_encode {
 8427:     my ($result) = @_;
 8428: 
 8429:     $result = &HTML::Entities::encode($result,'<>&"');
 8430:     
 8431:     return $result;
 8432: }
 8433: 
 8434: sub js_ready {
 8435:     my ($result) = @_;
 8436: 
 8437:     $result =~ s/[\n\r]/ /xmsg;
 8438:     $result =~ s/\\/\\\\/xmsg;
 8439:     $result =~ s/'/\\'/xmsg;
 8440:     $result =~ s{</}{<\\/}xmsg;
 8441:     
 8442:     return $result;
 8443: }
 8444: 
 8445: sub validate_page {
 8446:     if (  exists($env{'internal.start_page'})
 8447: 	  &&     $env{'internal.start_page'} > 1) {
 8448: 	&Apache::lonnet::logthis('start_page called multiple times '.
 8449: 				 $env{'internal.start_page'}.' '.
 8450: 				 $ENV{'request.filename'});
 8451:     }
 8452:     if (  exists($env{'internal.end_page'})
 8453: 	  &&     $env{'internal.end_page'} > 1) {
 8454: 	&Apache::lonnet::logthis('end_page called multiple times '.
 8455: 				 $env{'internal.end_page'}.' '.
 8456: 				 $env{'request.filename'});
 8457:     }
 8458:     if (     exists($env{'internal.start_page'})
 8459: 	&& ! exists($env{'internal.end_page'})) {
 8460: 	&Apache::lonnet::logthis('start_page called without end_page '.
 8461: 				 $env{'request.filename'});
 8462:     }
 8463:     if (   ! exists($env{'internal.start_page'})
 8464: 	&&   exists($env{'internal.end_page'})) {
 8465: 	&Apache::lonnet::logthis('end_page called without start_page'.
 8466: 				 $env{'request.filename'});
 8467:     }
 8468: }
 8469: 
 8470: 
 8471: sub start_scrollbox {
 8472:     my ($outerwidth,$width,$height,$id,$bgcolor,$cursor,$needjsready) = @_;
 8473:     unless ($outerwidth) { $outerwidth='520px'; }
 8474:     unless ($width) { $width='500px'; }
 8475:     unless ($height) { $height='200px'; }
 8476:     my ($table_id,$div_id,$tdcol);
 8477:     if ($id ne '') {
 8478:         $table_id = ' id="table_'.$id.'"';
 8479:         $div_id = ' id="div_'.$id.'"';
 8480:     }
 8481:     if ($bgcolor ne '') {
 8482:         $tdcol = "background-color: $bgcolor;";
 8483:     }
 8484:     my $nicescroll_js;
 8485:     if ($env{'browser.mobile'}) {
 8486:         $nicescroll_js = &nicescroll_javascript('div_'.$id,$cursor,$needjsready);
 8487:     }
 8488:     return <<"END";
 8489: $nicescroll_js
 8490: 
 8491: <table style="width: $outerwidth; border: 1px solid none;"$table_id><tr><td style="width: $width;$tdcol">
 8492: <div style="overflow:auto; width:$width; height:$height;"$div_id>
 8493: END
 8494: }
 8495: 
 8496: sub end_scrollbox {
 8497:     return '</div></td></tr></table>';
 8498: }
 8499: 
 8500: sub nicescroll_javascript {
 8501:     my ($id,$cursor,$needjsready,$framecheck,$location) = @_;
 8502:     my %options;
 8503:     if (ref($cursor) eq 'HASH') {
 8504:         %options = %{$cursor};
 8505:     }
 8506:     unless ($options{'railalign'} =~ /^left|right$/) {
 8507:         $options{'railalign'} = 'left';
 8508:     }
 8509:     unless ($options{'cursorcolor'} =~ /^\#\w+$/) {
 8510:         my $function  = &get_users_function();
 8511:         $options{'cursorcolor'} = &designparm($function.'.sidebg',$env{'request.role.domain'});
 8512:         unless ($options{'cursorcolor'} =~ /^\#\w+$/) {
 8513:             $options{'cursorcolor'} = '#00F';
 8514:         }
 8515:     }
 8516:     if ($options{'cursoropacity'} =~ /^[\d.]+$/) {
 8517:         unless ($options{'cursoropacity'} >= 0.0 && $options{'cursoropacity'} <=1.0) {
 8518:             $options{'cursoropacity'}='1.0';
 8519:         }
 8520:     } else {
 8521:         $options{'cursoropacity'}='1.0';
 8522:     }
 8523:     if ($options{'cursorfixedheight'} eq 'none') {
 8524:         delete($options{'cursorfixedheight'});
 8525:     } else {
 8526:         unless ($options{'cursorfixedheight'} =~ /^\d+$/) { $options{'cursorfixedheight'}='50'; }
 8527:     }
 8528:     unless ($options{'railoffset'} =~ /^{[\w\:\d\-,]+}$/) {
 8529:         delete($options{'railoffset'});
 8530:     }
 8531:     my @niceoptions;
 8532:     while (my($key,$value) = each(%options)) {
 8533:         if ($value =~ /^\{.+\}$/) {
 8534:             push(@niceoptions,$key.':'.$value);
 8535:         } else {
 8536:             push(@niceoptions,$key.':"'.$value.'"');
 8537:         }
 8538:     }
 8539:     my $nicescroll_js = '
 8540: $(document).ready(
 8541:       function() {
 8542:           $("#'.$id.'").niceScroll({'.join(',',@niceoptions).'});
 8543:       }
 8544: );
 8545: ';
 8546:     if ($framecheck) {
 8547:         $nicescroll_js .= '
 8548: function expand_div(caller) {
 8549:     if (top === self) {
 8550:         document.getElementById("'.$id.'").style.width = "auto";
 8551:         document.getElementById("'.$id.'").style.height = "auto";
 8552:     } else {
 8553:         try {
 8554:             if (parent.frames) {
 8555:                 if (parent.frames.length > 1) {
 8556:                     var framesrc = parent.frames[1].location.href;
 8557:                     var currsrc = framesrc.replace(/\#.*$/,"");
 8558:                     if ((caller == "search") || (currsrc == "'.$location.'")) {
 8559:                         document.getElementById("'.$id.'").style.width = "auto";
 8560:                         document.getElementById("'.$id.'").style.height = "auto";
 8561:                     }
 8562:                 }
 8563:             }
 8564:         } catch (e) {
 8565:             return;
 8566:         }
 8567:     }
 8568:     return;
 8569: }
 8570: ';
 8571:     }
 8572:     if ($needjsready) {
 8573:         $nicescroll_js = '
 8574: <script type="text/javascript">'."\n".$nicescroll_js."\n</script>\n";
 8575:     } else {
 8576:         $nicescroll_js = &Apache::lonhtmlcommon::scripttag($nicescroll_js);
 8577:     }
 8578:     return $nicescroll_js;
 8579: }
 8580: 
 8581: sub simple_error_page {
 8582:     my ($r,$title,$msg,$args) = @_;
 8583:     if (ref($args) eq 'HASH') {
 8584:         if (!$args->{'no_auto_mt_msg'}) { $msg = &mt($msg); }
 8585:     } else {
 8586:         $msg = &mt($msg);
 8587:     }
 8588: 
 8589:     my $page =
 8590: 	&Apache::loncommon::start_page($title).
 8591: 	'<p class="LC_error">'.$msg.'</p>'.
 8592: 	&Apache::loncommon::end_page();
 8593:     if (ref($r)) {
 8594: 	$r->print($page);
 8595: 	return;
 8596:     }
 8597:     return $page;
 8598: }
 8599: 
 8600: {
 8601:     my @row_count;
 8602: 
 8603:     sub start_data_table_count {
 8604:         unshift(@row_count, 0);
 8605:         return;
 8606:     }
 8607: 
 8608:     sub end_data_table_count {
 8609:         shift(@row_count);
 8610:         return;
 8611:     }
 8612: 
 8613:     sub start_data_table {
 8614: 	my ($add_class,$id) = @_;
 8615: 	my $css_class = (join(' ','LC_data_table',$add_class));
 8616:         my $table_id;
 8617:         if (defined($id)) {
 8618:             $table_id = ' id="'.$id.'"';
 8619:         }
 8620: 	&start_data_table_count();
 8621: 	return '<table class="'.$css_class.'"'.$table_id.'>'."\n";
 8622:     }
 8623: 
 8624:     sub end_data_table {
 8625: 	&end_data_table_count();
 8626: 	return '</table>'."\n";;
 8627:     }
 8628: 
 8629:     sub start_data_table_row {
 8630: 	my ($add_class, $id) = @_;
 8631: 	$row_count[0]++;
 8632: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
 8633: 	$css_class = (join(' ',$css_class,$add_class)) unless ($add_class eq '');
 8634:         $id = (' id="'.$id.'"') unless ($id eq '');
 8635:         return  '<tr class="'.$css_class.'"'.$id.'>'."\n";
 8636:     }
 8637:     
 8638:     sub continue_data_table_row {
 8639: 	my ($add_class, $id) = @_;
 8640: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
 8641: 	$css_class = (join(' ',$css_class,$add_class)) unless ($add_class eq '');
 8642:         $id = (' id="'.$id.'"') unless ($id eq '');
 8643:         return  '<tr class="'.$css_class.'"'.$id.'>'."\n";
 8644:     }
 8645: 
 8646:     sub end_data_table_row {
 8647: 	return '</tr>'."\n";;
 8648:     }
 8649: 
 8650:     sub start_data_table_empty_row {
 8651: #	$row_count[0]++;
 8652: 	return  '<tr class="LC_empty_row" >'."\n";;
 8653:     }
 8654: 
 8655:     sub end_data_table_empty_row {
 8656: 	return '</tr>'."\n";;
 8657:     }
 8658: 
 8659:     sub start_data_table_header_row {
 8660: 	return  '<tr class="LC_header_row">'."\n";;
 8661:     }
 8662: 
 8663:     sub end_data_table_header_row {
 8664: 	return '</tr>'."\n";;
 8665:     }
 8666: 
 8667:     sub data_table_caption {
 8668:         my $caption = shift;
 8669:         return "<caption class=\"LC_caption\">$caption</caption>";
 8670:     }
 8671: }
 8672: 
 8673: =pod
 8674: 
 8675: =item * &inhibit_menu_check($arg)
 8676: 
 8677: Checks for a inhibitmenu state and generates output to preserve it
 8678: 
 8679: Inputs:         $arg - can be any of
 8680:                      - undef - in which case the return value is a string 
 8681:                                to add  into arguments list of a uri
 8682:                      - 'input' - in which case the return value is a HTML
 8683:                                  <form> <input> field of type hidden to
 8684:                                  preserve the value
 8685:                      - a url - in which case the return value is the url with
 8686:                                the neccesary cgi args added to preserve the
 8687:                                inhibitmenu state
 8688:                      - a ref to a url - no return value, but the string is
 8689:                                         updated to include the neccessary cgi
 8690:                                         args to preserve the inhibitmenu state
 8691: 
 8692: =cut
 8693: 
 8694: sub inhibit_menu_check {
 8695:     my ($arg) = @_;
 8696:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
 8697:     if ($arg eq 'input') {
 8698: 	if ($env{'form.inhibitmenu'}) {
 8699: 	    return '<input type="hidden" name="inhibitmenu" value="'.$env{'form.inhibitmenu'}.'" />';
 8700: 	} else {
 8701: 	    return
 8702: 	}
 8703:     }
 8704:     if ($env{'form.inhibitmenu'}) {
 8705: 	if (ref($arg)) {
 8706: 	    $$arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
 8707: 	} elsif ($arg eq '') {
 8708: 	    $arg .= 'inhibitmenu='.$env{'form.inhibitmenu'};
 8709: 	} else {
 8710: 	    $arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
 8711: 	}
 8712:     }
 8713:     if (!ref($arg)) {
 8714: 	return $arg;
 8715:     }
 8716: }
 8717: 
 8718: ###############################################
 8719: 
 8720: =pod
 8721: 
 8722: =back
 8723: 
 8724: =head1 User Information Routines
 8725: 
 8726: =over 4
 8727: 
 8728: =item * &get_users_function()
 8729: 
 8730: Used by &bodytag to determine the current users primary role.
 8731: Returns either 'student','coordinator','admin', or 'author'.
 8732: 
 8733: =cut
 8734: 
 8735: ###############################################
 8736: sub get_users_function {
 8737:     my $function = 'norole';
 8738:     if ($env{'request.role'}=~/^(st)/) {
 8739:         $function='student';
 8740:     }
 8741:     if ($env{'request.role'}=~/^(cc|co|in|ta|ep)/) {
 8742:         $function='coordinator';
 8743:     }
 8744:     if ($env{'request.role'}=~/^(su|dc|ad|li)/) {
 8745:         $function='admin';
 8746:     }
 8747:     if (($env{'request.role'}=~/^(au|ca|aa)/) ||
 8748:         ($ENV{'REQUEST_URI'}=~ m{/^(/priv)})) {
 8749:         $function='author';
 8750:     }
 8751:     return $function;
 8752: }
 8753: 
 8754: ###############################################
 8755: 
 8756: =pod
 8757: 
 8758: =item * &show_course()
 8759: 
 8760: Used by lonmenu.pm and lonroles.pm to determine whether to use the word
 8761: 'Courses' or 'Roles' in inline navigation and on screen displaying user's roles.
 8762: 
 8763: Inputs:
 8764: None
 8765: 
 8766: Outputs:
 8767: Scalar: 1 if 'Course' to be used, 0 otherwise.
 8768: 
 8769: =cut
 8770: 
 8771: ###############################################
 8772: sub show_course {
 8773:     my $course = !$env{'user.adv'};
 8774:     if (!$env{'user.adv'}) {
 8775:         foreach my $env (keys(%env)) {
 8776:             next if ($env !~ m/^user\.priv\./);
 8777:             if ($env !~ m/^user\.priv\.(?:st|cm)/) {
 8778:                 $course = 0;
 8779:                 last;
 8780:             }
 8781:         }
 8782:     }
 8783:     return $course;
 8784: }
 8785: 
 8786: ###############################################
 8787: 
 8788: =pod
 8789: 
 8790: =item * &check_user_status()
 8791: 
 8792: Determines current status of supplied role for a
 8793: specific user. Roles can be active, previous or future.
 8794: 
 8795: Inputs: 
 8796: user's domain, user's username, course's domain,
 8797: course's number, optional section ID.
 8798: 
 8799: Outputs:
 8800: role status: active, previous or future. 
 8801: 
 8802: =cut
 8803: 
 8804: sub check_user_status {
 8805:     my ($udom,$uname,$cdom,$crs,$role,$sec) = @_;
 8806:     my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
 8807:     my @uroles = keys(%userinfo);
 8808:     my $srchstr;
 8809:     my $active_chk = 'none';
 8810:     my $now = time;
 8811:     if (@uroles > 0) {
 8812:         if (($role eq 'cc') || ($role eq 'co') || ($sec eq '') || (!defined($sec))) {
 8813:             $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
 8814:         } else {
 8815:             $srchstr = '/'.$cdom.'/'.$crs.'/'.$sec.'_'.$role;
 8816:         }
 8817:         if (grep/^\Q$srchstr\E$/,@uroles) {
 8818:             my $role_end = 0;
 8819:             my $role_start = 0;
 8820:             $active_chk = 'active';
 8821:             if ($userinfo{$srchstr} =~ m/^\Q$role\E_(\d+)/) {
 8822:                 $role_end = $1;
 8823:                 if ($userinfo{$srchstr} =~ m/^\Q$role\E_\Q$role_end\E_(\d+)$/) {
 8824:                     $role_start = $1;
 8825:                 }
 8826:             }
 8827:             if ($role_start > 0) {
 8828:                 if ($now < $role_start) {
 8829:                     $active_chk = 'future';
 8830:                 }
 8831:             }
 8832:             if ($role_end > 0) {
 8833:                 if ($now > $role_end) {
 8834:                     $active_chk = 'previous';
 8835:                 }
 8836:             }
 8837:         }
 8838:     }
 8839:     return $active_chk;
 8840: }
 8841: 
 8842: ###############################################
 8843: 
 8844: =pod
 8845: 
 8846: =item * &get_sections()
 8847: 
 8848: Determines all the sections for a course including
 8849: sections with students and sections containing other roles.
 8850: Incoming parameters: 
 8851: 
 8852: 1. domain
 8853: 2. course number 
 8854: 3. reference to array containing roles for which sections should 
 8855: be gathered (optional).
 8856: 4. reference to array containing status types for which sections 
 8857: should be gathered (optional).
 8858: 
 8859: If the third argument is undefined, sections are gathered for any role. 
 8860: If the fourth argument is undefined, sections are gathered for any status.
 8861: Permissible values are 'active' or 'future' or 'previous'.
 8862:  
 8863: Returns section hash (keys are section IDs, values are
 8864: number of users in each section), subject to the
 8865: optional roles filter, optional status filter 
 8866: 
 8867: =cut
 8868: 
 8869: ###############################################
 8870: sub get_sections {
 8871:     my ($cdom,$cnum,$possible_roles,$possible_status) = @_;
 8872:     if (!defined($cdom) || !defined($cnum)) {
 8873:         my $cid =  $env{'request.course.id'};
 8874: 
 8875: 	return if (!defined($cid));
 8876: 
 8877:         $cdom = $env{'course.'.$cid.'.domain'};
 8878:         $cnum = $env{'course.'.$cid.'.num'};
 8879:     }
 8880: 
 8881:     my %sectioncount;
 8882:     my $now = time;
 8883: 
 8884:     my $check_students = 1;
 8885:     my $only_students = 0;
 8886:     if (ref($possible_roles) eq 'ARRAY') {
 8887:         if (grep(/^st$/,@{$possible_roles})) {
 8888:             if (@{$possible_roles} == 1) {
 8889:                 $only_students = 1;
 8890:             }
 8891:         } else {
 8892:             $check_students = 0;
 8893:         }
 8894:     }
 8895: 
 8896:     if ($check_students) { 
 8897: 	my ($classlist) = &Apache::loncoursedata::get_classlist($cdom,$cnum);
 8898: 	my $sec_index = &Apache::loncoursedata::CL_SECTION();
 8899: 	my $status_index = &Apache::loncoursedata::CL_STATUS();
 8900:         my $start_index = &Apache::loncoursedata::CL_START();
 8901:         my $end_index = &Apache::loncoursedata::CL_END();
 8902:         my $status;
 8903: 	while (my ($student,$data) = each(%$classlist)) {
 8904: 	    my ($section,$stu_status,$start,$end) = ($data->[$sec_index],
 8905: 				                     $data->[$status_index],
 8906:                                                      $data->[$start_index],
 8907:                                                      $data->[$end_index]);
 8908:             if ($stu_status eq 'Active') {
 8909:                 $status = 'active';
 8910:             } elsif ($end < $now) {
 8911:                 $status = 'previous';
 8912:             } elsif ($start > $now) {
 8913:                 $status = 'future';
 8914:             } 
 8915: 	    if ($section ne '-1' && $section !~ /^\s*$/) {
 8916:                 if ((!defined($possible_status)) || (($status ne '') && 
 8917:                     (grep/^\Q$status\E$/,@{$possible_status}))) { 
 8918: 		    $sectioncount{$section}++;
 8919:                 }
 8920: 	    }
 8921: 	}
 8922:     }
 8923:     if ($only_students) {
 8924:         return %sectioncount;
 8925:     }
 8926:     my %courseroles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
 8927:     foreach my $user (sort(keys(%courseroles))) {
 8928: 	if ($user !~ /^(\w{2})/) { next; }
 8929: 	my ($role) = ($user =~ /^(\w{2})/);
 8930: 	if ($possible_roles && !(grep(/^$role$/,@$possible_roles))) { next; }
 8931: 	my ($section,$status);
 8932: 	if ($role eq 'cr' &&
 8933: 	    $user =~ m-^$role/[^/]*/[^/]*/[^/]*:[^:]*:[^:]*:(\w+)-) {
 8934: 	    $section=$1;
 8935: 	}
 8936: 	if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
 8937: 	if (!defined($section) || $section eq '-1') { next; }
 8938:         my ($end,$start) = ($courseroles{$user} =~ /^([^:]*):([^:]*)$/);
 8939:         if ($end == -1 && $start == -1) {
 8940:             next; #deleted role
 8941:         }
 8942:         if (!defined($possible_status)) { 
 8943:             $sectioncount{$section}++;
 8944:         } else {
 8945:             if ((!$end || $end >= $now) && (!$start || $start <= $now)) {
 8946:                 $status = 'active';
 8947:             } elsif ($end < $now) {
 8948:                 $status = 'future';
 8949:             } elsif ($start > $now) {
 8950:                 $status = 'previous';
 8951:             }
 8952:             if (($status ne '') && (grep/^\Q$status\E$/,@{$possible_status})) {
 8953:                 $sectioncount{$section}++;
 8954:             }
 8955:         }
 8956:     }
 8957:     return %sectioncount;
 8958: }
 8959: 
 8960: ###############################################
 8961: 
 8962: =pod
 8963: 
 8964: =item * &get_course_users()
 8965: 
 8966: Retrieves usernames:domains for users in the specified course
 8967: with specific role(s), and access status. 
 8968: 
 8969: Incoming parameters:
 8970: 1. course domain
 8971: 2. course number
 8972: 3. access status: users must have - either active, 
 8973: previous, future, or all.
 8974: 4. reference to array of permissible roles
 8975: 5. reference to array of section restrictions (optional)
 8976: 6. reference to results object (hash of hashes).
 8977: 7. reference to optional userdata hash
 8978: 8. reference to optional statushash
 8979: 9. flag if privileged users (except those set to unhide in
 8980:    course settings) should be excluded    
 8981: Keys of top level results hash are roles.
 8982: Keys of inner hashes are username:domain, with 
 8983: values set to access type.
 8984: Optional userdata hash returns an array with arguments in the 
 8985: same order as loncoursedata::get_classlist() for student data.
 8986: 
 8987: Optional statushash returns
 8988: 
 8989: Entries for end, start, section and status are blank because
 8990: of the possibility of multiple values for non-student roles.
 8991: 
 8992: =cut
 8993: 
 8994: ###############################################
 8995: 
 8996: sub get_course_users {
 8997:     my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata,$statushash,$hidepriv) = @_;
 8998:     my %idx = ();
 8999:     my %seclists;
 9000: 
 9001:     $idx{udom} = &Apache::loncoursedata::CL_SDOM();
 9002:     $idx{uname} =  &Apache::loncoursedata::CL_SNAME();
 9003:     $idx{end} = &Apache::loncoursedata::CL_END();
 9004:     $idx{start} = &Apache::loncoursedata::CL_START();
 9005:     $idx{id} = &Apache::loncoursedata::CL_ID();
 9006:     $idx{section} = &Apache::loncoursedata::CL_SECTION();
 9007:     $idx{fullname} = &Apache::loncoursedata::CL_FULLNAME();
 9008:     $idx{status} = &Apache::loncoursedata::CL_STATUS();
 9009: 
 9010:     if (grep(/^st$/,@{$roles})) {
 9011:         my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist($cdom,$cnum);
 9012:         my $now = time;
 9013:         foreach my $student (keys(%{$classlist})) {
 9014:             my $match = 0;
 9015:             my $secmatch = 0;
 9016:             my $section = $$classlist{$student}[$idx{section}];
 9017:             my $status = $$classlist{$student}[$idx{status}];
 9018:             if ($section eq '') {
 9019:                 $section = 'none';
 9020:             }
 9021:             if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
 9022:                 if (grep(/^all$/,@{$sections})) {
 9023:                     $secmatch = 1;
 9024:                 } elsif ($$classlist{$student}[$idx{section}] eq '') {
 9025:                     if (grep(/^none$/,@{$sections})) {
 9026:                         $secmatch = 1;
 9027:                     }
 9028:                 } else {  
 9029: 		    if (grep(/^\Q$section\E$/,@{$sections})) {
 9030: 		        $secmatch = 1;
 9031:                     }
 9032: 		}
 9033:                 if (!$secmatch) {
 9034:                     next;
 9035:                 }
 9036:             }
 9037:             if (defined($$types{'active'})) {
 9038:                 if ($$classlist{$student}[$idx{status}] eq 'Active') {
 9039:                     push(@{$$users{st}{$student}},'active');
 9040:                     $match = 1;
 9041:                 }
 9042:             }
 9043:             if (defined($$types{'previous'})) {
 9044:                 if ($$classlist{$student}[$idx{status}] eq 'Expired') {
 9045:                     push(@{$$users{st}{$student}},'previous');
 9046:                     $match = 1;
 9047:                 }
 9048:             }
 9049:             if (defined($$types{'future'})) {
 9050:                 if ($$classlist{$student}[$idx{status}] eq 'Future') {
 9051:                     push(@{$$users{st}{$student}},'future');
 9052:                     $match = 1;
 9053:                 }
 9054:             }
 9055:             if ($match) {
 9056:                 push(@{$seclists{$student}},$section);
 9057:                 if (ref($userdata) eq 'HASH') {
 9058:                     $$userdata{$student} = $$classlist{$student};
 9059:                 }
 9060:                 if (ref($statushash) eq 'HASH') {
 9061:                     $statushash->{$student}{'st'}{$section} = $status;
 9062:                 }
 9063:             }
 9064:         }
 9065:     }
 9066:     if ((@{$roles} > 1) || ((@{$roles} == 1) && ($$roles[0] ne "st"))) {
 9067:         my %coursepersonnel = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
 9068:         my $now = time;
 9069:         my %displaystatus = ( previous => 'Expired',
 9070:                               active   => 'Active',
 9071:                               future   => 'Future',
 9072:                             );
 9073:         my (%nothide,@possdoms);
 9074:         if ($hidepriv) {
 9075:             my %coursehash=&Apache::lonnet::coursedescription($cdom.'_'.$cnum);
 9076:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 9077:                 if ($user !~ /:/) {
 9078:                     $nothide{join(':',split(/[\@]/,$user))}=1;
 9079:                 } else {
 9080:                     $nothide{$user} = 1;
 9081:                 }
 9082:             }
 9083:             my @possdoms = ($cdom);
 9084:             if ($coursehash{'checkforpriv'}) {
 9085:                 push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
 9086:             }
 9087:         }
 9088:         foreach my $person (sort(keys(%coursepersonnel))) {
 9089:             my $match = 0;
 9090:             my $secmatch = 0;
 9091:             my $status;
 9092:             my ($role,$user,$usec) = ($person =~ /^([^:]*):([^:]+:[^:]+):([^:]*)/);
 9093:             $user =~ s/:$//;
 9094:             my ($end,$start) = split(/:/,$coursepersonnel{$person});
 9095:             if ($end == -1 || $start == -1) {
 9096:                 next;
 9097:             }
 9098:             if (($role) && ((grep(/^\Q$role\E$/,@{$roles})) ||
 9099:                 (grep(/^cr$/,@{$roles}) && $role =~ /^cr\//))) {
 9100:                 my ($uname,$udom) = split(/:/,$user);
 9101:                 if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
 9102:                     if (grep(/^all$/,@{$sections})) {
 9103:                         $secmatch = 1;
 9104:                     } elsif ($usec eq '') {
 9105:                         if (grep(/^none$/,@{$sections})) {
 9106:                             $secmatch = 1;
 9107:                         }
 9108:                     } else {
 9109:                         if (grep(/^\Q$usec\E$/,@{$sections})) {
 9110:                             $secmatch = 1;
 9111:                         }
 9112:                     }
 9113:                     if (!$secmatch) {
 9114:                         next;
 9115:                     }
 9116:                 }
 9117:                 if ($usec eq '') {
 9118:                     $usec = 'none';
 9119:                 }
 9120:                 if ($uname ne '' && $udom ne '') {
 9121:                     if ($hidepriv) {
 9122:                         if ((&Apache::lonnet::privileged($uname,$udom,\@possdoms)) &&
 9123:                             (!$nothide{$uname.':'.$udom})) {
 9124:                             next;
 9125:                         }
 9126:                     }
 9127:                     if ($end > 0 && $end < $now) {
 9128:                         $status = 'previous';
 9129:                     } elsif ($start > $now) {
 9130:                         $status = 'future';
 9131:                     } else {
 9132:                         $status = 'active';
 9133:                     }
 9134:                     foreach my $type (keys(%{$types})) { 
 9135:                         if ($status eq $type) {
 9136:                             if (!grep(/^\Q$type\E$/,@{$$users{$role}{$user}})) {
 9137:                                 push(@{$$users{$role}{$user}},$type);
 9138:                             }
 9139:                             $match = 1;
 9140:                         }
 9141:                     }
 9142:                     if (($match) && (ref($userdata) eq 'HASH')) {
 9143:                         if (!exists($$userdata{$uname.':'.$udom})) {
 9144: 			    &get_user_info($udom,$uname,\%idx,$userdata);
 9145:                         }
 9146:                         if (!grep(/^\Q$usec\E$/,@{$seclists{$uname.':'.$udom}})) {
 9147:                             push(@{$seclists{$uname.':'.$udom}},$usec);
 9148:                         }
 9149:                         if (ref($statushash) eq 'HASH') {
 9150:                             $statushash->{$uname.':'.$udom}{$role}{$usec} = $displaystatus{$status};
 9151:                         }
 9152:                     }
 9153:                 }
 9154:             }
 9155:         }
 9156:         if (grep(/^ow$/,@{$roles})) {
 9157:             if ((defined($cdom)) && (defined($cnum))) {
 9158:                 my %csettings = &Apache::lonnet::get('environment',['internal.courseowner'],$cdom,$cnum);
 9159:                 if ( defined($csettings{'internal.courseowner'}) ) {
 9160:                     my $owner = $csettings{'internal.courseowner'};
 9161:                     next if ($owner eq '');
 9162:                     my ($ownername,$ownerdom);
 9163:                     if ($owner =~ /^([^:]+):([^:]+)$/) {
 9164:                         $ownername = $1;
 9165:                         $ownerdom = $2;
 9166:                     } else {
 9167:                         $ownername = $owner;
 9168:                         $ownerdom = $cdom;
 9169:                         $owner = $ownername.':'.$ownerdom;
 9170:                     }
 9171:                     @{$$users{'ow'}{$owner}} = 'any';
 9172:                     if (defined($userdata) && 
 9173: 			!exists($$userdata{$owner})) {
 9174: 			&get_user_info($ownerdom,$ownername,\%idx,$userdata);
 9175:                         if (!grep(/^none$/,@{$seclists{$owner}})) {
 9176:                             push(@{$seclists{$owner}},'none');
 9177:                         }
 9178:                         if (ref($statushash) eq 'HASH') {
 9179:                             $statushash->{$owner}{'ow'}{'none'} = 'Any';
 9180:                         }
 9181: 		    }
 9182:                 }
 9183:             }
 9184:         }
 9185:         foreach my $user (keys(%seclists)) {
 9186:             @{$seclists{$user}} = (sort {$a <=> $b} @{$seclists{$user}});
 9187:             $$userdata{$user}[$idx{section}] = join(',',@{$seclists{$user}});
 9188:         }
 9189:     }
 9190:     return;
 9191: }
 9192: 
 9193: sub get_user_info {
 9194:     my ($udom,$uname,$idx,$userdata) = @_;
 9195:     $$userdata{$uname.':'.$udom}[$$idx{fullname}] = 
 9196: 	&plainname($uname,$udom,'lastname');
 9197:     $$userdata{$uname.':'.$udom}[$$idx{uname}] = $uname;
 9198:     $$userdata{$uname.':'.$udom}[$$idx{udom}] = $udom;
 9199:     my %idhash =  &Apache::lonnet::idrget($udom,($uname));
 9200:     $$userdata{$uname.':'.$udom}[$$idx{id}] = $idhash{$uname}; 
 9201:     return;
 9202: }
 9203: 
 9204: ###############################################
 9205: 
 9206: =pod
 9207: 
 9208: =item * &get_user_quota()
 9209: 
 9210: Retrieves quota assigned for storage of user files.
 9211: Default is to report quota for portfolio files.
 9212: 
 9213: Incoming parameters:
 9214: 1. user's username
 9215: 2. user's domain
 9216: 3. quota name - portfolio, author, or course
 9217:    (if no quota name provided, defaults to portfolio).
 9218: 4. crstype - official, unofficial, textbook or community, if quota name is
 9219:    course
 9220: 
 9221: Returns:
 9222: 1. Disk quota (in MB) assigned to student.
 9223: 2. (Optional) Type of setting: custom or default
 9224:    (individually assigned or default for user's 
 9225:    institutional status).
 9226: 3. (Optional) - User's institutional status (e.g., faculty, staff
 9227:    or student - types as defined in localenroll::inst_usertypes 
 9228:    for user's domain, which determines default quota for user.
 9229: 4. (Optional) - Default quota which would apply to the user.
 9230: 
 9231: If a value has been stored in the user's environment, 
 9232: it will return that, otherwise it returns the maximal default
 9233: defined for the user's institutional status(es) in the domain.
 9234: 
 9235: =cut
 9236: 
 9237: ###############################################
 9238: 
 9239: 
 9240: sub get_user_quota {
 9241:     my ($uname,$udom,$quotaname,$crstype) = @_;
 9242:     my ($quota,$quotatype,$settingstatus,$defquota);
 9243:     if (!defined($udom)) {
 9244:         $udom = $env{'user.domain'};
 9245:     }
 9246:     if (!defined($uname)) {
 9247:         $uname = $env{'user.name'};
 9248:     }
 9249:     if (($udom eq '' || $uname eq '') ||
 9250:         ($udom eq 'public') && ($uname eq 'public')) {
 9251:         $quota = 0;
 9252:         $quotatype = 'default';
 9253:         $defquota = 0; 
 9254:     } else {
 9255:         my $inststatus;
 9256:         if ($quotaname eq 'course') {
 9257:             if (($env{'course.'.$udom.'_'.$uname.'.num'} eq $uname) &&
 9258:                 ($env{'course.'.$udom.'_'.$uname.'.domain'} eq $udom)) {
 9259:                 $quota = $env{'course.'.$udom.'_'.$uname.'.internal.uploadquota'};
 9260:             } else {
 9261:                 my %cenv = &Apache::lonnet::coursedescription("$udom/$uname");
 9262:                 $quota = $cenv{'internal.uploadquota'};
 9263:             }
 9264:         } else {
 9265:             if ($udom eq $env{'user.domain'} && $uname eq $env{'user.name'}) {
 9266:                 if ($quotaname eq 'author') {
 9267:                     $quota = $env{'environment.authorquota'};
 9268:                 } else {
 9269:                     $quota = $env{'environment.portfolioquota'};
 9270:                 }
 9271:                 $inststatus = $env{'environment.inststatus'};
 9272:             } else {
 9273:                 my %userenv = 
 9274:                     &Apache::lonnet::get('environment',['portfolioquota',
 9275:                                          'authorquota','inststatus'],$udom,$uname);
 9276:                 my ($tmp) = keys(%userenv);
 9277:                 if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
 9278:                     if ($quotaname eq 'author') {
 9279:                         $quota = $userenv{'authorquota'};
 9280:                     } else {
 9281:                         $quota = $userenv{'portfolioquota'};
 9282:                     }
 9283:                     $inststatus = $userenv{'inststatus'};
 9284:                 } else {
 9285:                     undef(%userenv);
 9286:                 }
 9287:             }
 9288:         }
 9289:         if ($quota eq '' || wantarray) {
 9290:             if ($quotaname eq 'course') {
 9291:                 my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
 9292:                 if (($crstype eq 'official') || ($crstype eq 'unofficial') || 
 9293:                     ($crstype eq 'community') || ($crstype eq 'textbook')) { 
 9294:                     $defquota = $domdefs{$crstype.'quota'};
 9295:                 }
 9296:                 if ($defquota eq '') {
 9297:                     $defquota = 500;
 9298:                 }
 9299:             } else {
 9300:                 ($defquota,$settingstatus) = &default_quota($udom,$inststatus,$quotaname);
 9301:             }
 9302:             if ($quota eq '') {
 9303:                 $quota = $defquota;
 9304:                 $quotatype = 'default';
 9305:             } else {
 9306:                 $quotatype = 'custom';
 9307:             }
 9308:         }
 9309:     }
 9310:     if (wantarray) {
 9311:         return ($quota,$quotatype,$settingstatus,$defquota);
 9312:     } else {
 9313:         return $quota;
 9314:     }
 9315: }
 9316: 
 9317: ###############################################
 9318: 
 9319: =pod
 9320: 
 9321: =item * &default_quota()
 9322: 
 9323: Retrieves default quota assigned for storage of user portfolio files,
 9324: given an (optional) user's institutional status.
 9325: 
 9326: Incoming parameters:
 9327: 
 9328: 1. domain
 9329: 2. (Optional) institutional status(es).  This is a : separated list of 
 9330:    status types (e.g., faculty, staff, student etc.)
 9331:    which apply to the user for whom the default is being retrieved.
 9332:    If the institutional status string in undefined, the domain
 9333:    default quota will be returned.
 9334: 3.  quota name - portfolio, author, or course
 9335:    (if no quota name provided, defaults to portfolio).
 9336: 
 9337: Returns:
 9338: 
 9339: 1. Default disk quota (in MB) for user portfolios in the domain.
 9340: 2. (Optional) institutional type which determined the value of the
 9341:    default quota.
 9342: 
 9343: If a value has been stored in the domain's configuration db,
 9344: it will return that, otherwise it returns 20 (for backwards 
 9345: compatibility with domains which have not set up a configuration
 9346: db file; the original statically defined portfolio quota was 20 MB). 
 9347: 
 9348: If the user's status includes multiple types (e.g., staff and student),
 9349: the largest default quota which applies to the user determines the
 9350: default quota returned.
 9351: 
 9352: =cut
 9353: 
 9354: ###############################################
 9355: 
 9356: 
 9357: sub default_quota {
 9358:     my ($udom,$inststatus,$quotaname) = @_;
 9359:     my ($defquota,$settingstatus);
 9360:     my %quotahash = &Apache::lonnet::get_dom('configuration',
 9361:                                             ['quotas'],$udom);
 9362:     my $key = 'defaultquota';
 9363:     if ($quotaname eq 'author') {
 9364:         $key = 'authorquota';
 9365:     }
 9366:     if (ref($quotahash{'quotas'}) eq 'HASH') {
 9367:         if ($inststatus ne '') {
 9368:             my @statuses = map { &unescape($_); } split(/:/,$inststatus);
 9369:             foreach my $item (@statuses) {
 9370:                 if (ref($quotahash{'quotas'}{$key}) eq 'HASH') {
 9371:                     if ($quotahash{'quotas'}{$key}{$item} ne '') {
 9372:                         if ($defquota eq '') {
 9373:                             $defquota = $quotahash{'quotas'}{$key}{$item};
 9374:                             $settingstatus = $item;
 9375:                         } elsif ($quotahash{'quotas'}{$key}{$item} > $defquota) {
 9376:                             $defquota = $quotahash{'quotas'}{$key}{$item};
 9377:                             $settingstatus = $item;
 9378:                         }
 9379:                     }
 9380:                 } elsif ($key eq 'defaultquota') {
 9381:                     if ($quotahash{'quotas'}{$item} ne '') {
 9382:                         if ($defquota eq '') {
 9383:                             $defquota = $quotahash{'quotas'}{$item};
 9384:                             $settingstatus = $item;
 9385:                         } elsif ($quotahash{'quotas'}{$item} > $defquota) {
 9386:                             $defquota = $quotahash{'quotas'}{$item};
 9387:                             $settingstatus = $item;
 9388:                         }
 9389:                     }
 9390:                 }
 9391:             }
 9392:         }
 9393:         if ($defquota eq '') {
 9394:             if (ref($quotahash{'quotas'}{$key}) eq 'HASH') {
 9395:                 $defquota = $quotahash{'quotas'}{$key}{'default'};
 9396:             } elsif ($key eq 'defaultquota') {
 9397:                 $defquota = $quotahash{'quotas'}{'default'};
 9398:             }
 9399:             $settingstatus = 'default';
 9400:             if ($defquota eq '') {
 9401:                 if ($quotaname eq 'author') {
 9402:                     $defquota = 500;
 9403:                 }
 9404:             }
 9405:         }
 9406:     } else {
 9407:         $settingstatus = 'default';
 9408:         if ($quotaname eq 'author') {
 9409:             $defquota = 500;
 9410:         } else {
 9411:             $defquota = 20;
 9412:         }
 9413:     }
 9414:     if (wantarray) {
 9415:         return ($defquota,$settingstatus);
 9416:     } else {
 9417:         return $defquota;
 9418:     }
 9419: }
 9420: 
 9421: ###############################################
 9422: 
 9423: =pod
 9424: 
 9425: =item * &excess_filesize_warning()
 9426: 
 9427: Returns warning message if upload of file to authoring space, or copying
 9428: of existing file within authoring space will cause quota for the authoring
 9429: space to be exceeded.
 9430: 
 9431: Same, if upload of a file directly to a course/community via Course Editor
 9432: will cause quota for uploaded content for the course to be exceeded.
 9433: 
 9434: Inputs: 7 
 9435: 1. username or coursenum
 9436: 2. domain
 9437: 3. context ('author' or 'course')
 9438: 4. filename of file for which action is being requested
 9439: 5. filesize (kB) of file
 9440: 6. action being taken: copy or upload.
 9441: 7. quotatype (in course context -- official, unofficial, community or textbook).
 9442: 
 9443: Returns: 1 scalar: HTML to display containing warning if quota would be exceeded,
 9444:          otherwise return null.
 9445: 
 9446: =back
 9447: 
 9448: =cut
 9449: 
 9450: sub excess_filesize_warning {
 9451:     my ($uname,$udom,$context,$filename,$filesize,$action,$quotatype) = @_;
 9452:     my $current_disk_usage = 0;
 9453:     my $disk_quota = &get_user_quota($uname,$udom,$context,$quotatype); #expressed in MB
 9454:     if ($context eq 'author') {
 9455:         my $authorspace = $Apache::lonnet::perlvar{'lonDocRoot'}."/priv/$udom/$uname";
 9456:         $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$authorspace);
 9457:     } else {
 9458:         foreach my $subdir ('docs','supplemental') {
 9459:             $current_disk_usage += &Apache::lonnet::diskusage($udom,$uname,"userfiles/$subdir",1);
 9460:         }
 9461:     }
 9462:     $disk_quota = int($disk_quota * 1000);
 9463:     if (($current_disk_usage + $filesize) > $disk_quota) {
 9464:         return '<p class="LC_warning">'.
 9465:                 &mt("Unable to $action [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.",
 9466:                     '<span class="LC_filename">'.$filename.'</span>',$filesize).'</p>'.
 9467:                '<p>'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',
 9468:                             $disk_quota,$current_disk_usage).
 9469:                '</p>';
 9470:     }
 9471:     return;
 9472: }
 9473: 
 9474: ###############################################
 9475: 
 9476: 
 9477: 
 9478: 
 9479: sub get_secgrprole_info {
 9480:     my ($cdom,$cnum,$needroles,$type)  = @_;
 9481:     my %sections_count = &get_sections($cdom,$cnum);
 9482:     my @sections =  (sort {$a <=> $b} keys(%sections_count));
 9483:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
 9484:     my @groups = sort(keys(%curr_groups));
 9485:     my $allroles = [];
 9486:     my $rolehash;
 9487:     my $accesshash = {
 9488:                      active => 'Currently has access',
 9489:                      future => 'Will have future access',
 9490:                      previous => 'Previously had access',
 9491:                   };
 9492:     if ($needroles) {
 9493:         $rolehash = {'all' => 'all'};
 9494:         my %user_roles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
 9495: 	if (&Apache::lonnet::error(%user_roles)) {
 9496: 	    undef(%user_roles);
 9497: 	}
 9498:         foreach my $item (keys(%user_roles)) {
 9499:             my ($role)=split(/\:/,$item,2);
 9500:             if ($role eq 'cr') { next; }
 9501:             if ($role =~ /^cr/) {
 9502:                 $$rolehash{$role} = (split('/',$role))[3];
 9503:             } else {
 9504:                 $$rolehash{$role} = &Apache::lonnet::plaintext($role,$type);
 9505:             }
 9506:         }
 9507:         foreach my $key (sort(keys(%{$rolehash}))) {
 9508:             push(@{$allroles},$key);
 9509:         }
 9510:         push (@{$allroles},'st');
 9511:         $$rolehash{'st'} = &Apache::lonnet::plaintext('st',$type);
 9512:     }
 9513:     return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
 9514: }
 9515: 
 9516: sub user_picker {
 9517:     my ($dom,$srch,$forcenewuser,$caller,$cancreate,$usertype,$context) = @_;
 9518:     my $currdom = $dom;
 9519:     my %curr_selected = (
 9520:                         srchin => 'dom',
 9521:                         srchby => 'lastname',
 9522:                       );
 9523:     my $srchterm;
 9524:     if ((ref($srch) eq 'HASH') && ($env{'form.origform'} ne 'crtusername')) {
 9525:         if ($srch->{'srchby'} ne '') {
 9526:             $curr_selected{'srchby'} = $srch->{'srchby'};
 9527:         }
 9528:         if ($srch->{'srchin'} ne '') {
 9529:             $curr_selected{'srchin'} = $srch->{'srchin'};
 9530:         }
 9531:         if ($srch->{'srchtype'} ne '') {
 9532:             $curr_selected{'srchtype'} = $srch->{'srchtype'};
 9533:         }
 9534:         if ($srch->{'srchdomain'} ne '') {
 9535:             $currdom = $srch->{'srchdomain'};
 9536:         }
 9537:         $srchterm = $srch->{'srchterm'};
 9538:     }
 9539:     my %lt=&Apache::lonlocal::texthash(
 9540:                     'usr'       => 'Search criteria',
 9541:                     'doma'      => 'Domain/institution to search',
 9542:                     'uname'     => 'username',
 9543:                     'lastname'  => 'last name',
 9544:                     'lastfirst' => 'last name, first name',
 9545:                     'crs'       => 'in this course',
 9546:                     'dom'       => 'in selected LON-CAPA domain', 
 9547:                     'alc'       => 'all LON-CAPA',
 9548:                     'instd'     => 'in institutional directory for selected domain',
 9549:                     'exact'     => 'is',
 9550:                     'contains'  => 'contains',
 9551:                     'begins'    => 'begins with',
 9552:                     'youm'      => "You must include some text to search for.",
 9553:                     'thte'      => "The text you are searching for must contain at least two characters when using a 'begins' type search.",
 9554:                     'thet'      => "The text you are searching for must contain at least three characters when using a 'contains' type search.",
 9555:                     'yomc'      => "You must choose a domain when using an institutional directory search.",
 9556:                     'ymcd'      => "You must choose a domain when using a domain search.",
 9557:                     'whus'      => "When using searching by last,first you must include a comma as separator between last name and first name.",
 9558:                     'whse'      => "When searching by last,first you must include at least one character in the first name.",
 9559:                      'thfo'     => "The following need to be corrected before the search can be run:",
 9560:                                        );
 9561:     my $domform = &select_dom_form($currdom,'srchdomain',1,1);
 9562:     my $srchinsel = ' <select name="srchin">';
 9563: 
 9564:     my @srchins = ('crs','dom','alc','instd');
 9565: 
 9566:     foreach my $option (@srchins) {
 9567:         # FIXME 'alc' option unavailable until 
 9568:         #       loncreateuser::print_user_query_page()
 9569:         #       has been completed.
 9570:         next if ($option eq 'alc');
 9571:         next if (($option eq 'crs') && ($env{'form.form'} eq 'requestcrs'));  
 9572:         next if ($option eq 'crs' && !$env{'request.course.id'});
 9573:         if ($curr_selected{'srchin'} eq $option) {
 9574:             $srchinsel .= ' 
 9575:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
 9576:         } else {
 9577:             $srchinsel .= '
 9578:    <option value="'.$option.'">'.$lt{$option}.'</option>';
 9579:         }
 9580:     }
 9581:     $srchinsel .= "\n  </select>\n";
 9582: 
 9583:     my $srchbysel =  ' <select name="srchby">';
 9584:     foreach my $option ('lastname','lastfirst','uname') {
 9585:         if ($curr_selected{'srchby'} eq $option) {
 9586:             $srchbysel .= '
 9587:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
 9588:         } else {
 9589:             $srchbysel .= '
 9590:    <option value="'.$option.'">'.$lt{$option}.'</option>';
 9591:          }
 9592:     }
 9593:     $srchbysel .= "\n  </select>\n";
 9594: 
 9595:     my $srchtypesel = ' <select name="srchtype">';
 9596:     foreach my $option ('begins','contains','exact') {
 9597:         if ($curr_selected{'srchtype'} eq $option) {
 9598:             $srchtypesel .= '
 9599:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
 9600:         } else {
 9601:             $srchtypesel .= '
 9602:    <option value="'.$option.'">'.$lt{$option}.'</option>';
 9603:         }
 9604:     }
 9605:     $srchtypesel .= "\n  </select>\n";
 9606: 
 9607:     my ($newuserscript,$new_user_create);
 9608:     my $context_dom = $env{'request.role.domain'};
 9609:     if ($context eq 'requestcrs') {
 9610:         if ($env{'form.coursedom'} ne '') { 
 9611:             $context_dom = $env{'form.coursedom'};
 9612:         }
 9613:     }
 9614:     if ($forcenewuser) {
 9615:         if (ref($srch) eq 'HASH') {
 9616:             if ($srch->{'srchby'} eq 'uname' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchin'} eq 'dom' && $srch->{'srchdomain'} eq $context_dom) {
 9617:                 if ($cancreate) {
 9618:                     $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>';
 9619:                 } else {
 9620:                     my $helplink = 'javascript:helpMenu('."'display'".')';
 9621:                     my %usertypetext = (
 9622:                         official   => 'institutional',
 9623:                         unofficial => 'non-institutional',
 9624:                     );
 9625:                     $new_user_create = '<p class="LC_warning">'
 9626:                                       .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
 9627:                                       .' '
 9628:                                       .&mt('Please contact the [_1]helpdesk[_2] for assistance.'
 9629:                                           ,'<a href="'.$helplink.'">','</a>')
 9630:                                       .'</p><br />';
 9631:                 }
 9632:             }
 9633:         }
 9634: 
 9635:         $newuserscript = <<"ENDSCRIPT";
 9636: 
 9637: function setSearch(createnew,callingForm) {
 9638:     if (createnew == 1) {
 9639:         for (var i=0; i<callingForm.srchby.length; i++) {
 9640:             if (callingForm.srchby.options[i].value == 'uname') {
 9641:                 callingForm.srchby.selectedIndex = i;
 9642:             }
 9643:         }
 9644:         for (var i=0; i<callingForm.srchin.length; i++) {
 9645:             if ( callingForm.srchin.options[i].value == 'dom') {
 9646: 		callingForm.srchin.selectedIndex = i;
 9647:             }
 9648:         }
 9649:         for (var i=0; i<callingForm.srchtype.length; i++) {
 9650:             if (callingForm.srchtype.options[i].value == 'exact') {
 9651:                 callingForm.srchtype.selectedIndex = i;
 9652:             }
 9653:         }
 9654:         for (var i=0; i<callingForm.srchdomain.length; i++) {
 9655:             if (callingForm.srchdomain.options[i].value == '$context_dom') {
 9656:                 callingForm.srchdomain.selectedIndex = i;
 9657:             }
 9658:         }
 9659:     }
 9660: }
 9661: ENDSCRIPT
 9662: 
 9663:     }
 9664: 
 9665:     my $output = <<"END_BLOCK";
 9666: <script type="text/javascript">
 9667: // <![CDATA[
 9668: function validateEntry(callingForm) {
 9669: 
 9670:     var checkok = 1;
 9671:     var srchin;
 9672:     for (var i=0; i<callingForm.srchin.length; i++) {
 9673: 	if ( callingForm.srchin[i].checked ) {
 9674: 	    srchin = callingForm.srchin[i].value;
 9675: 	}
 9676:     }
 9677: 
 9678:     var srchtype = callingForm.srchtype.options[callingForm.srchtype.selectedIndex].value;
 9679:     var srchby = callingForm.srchby.options[callingForm.srchby.selectedIndex].value;
 9680:     var srchdomain = callingForm.srchdomain.options[callingForm.srchdomain.selectedIndex].value;
 9681:     var srchterm =  callingForm.srchterm.value;
 9682:     var srchin = callingForm.srchin.options[callingForm.srchin.selectedIndex].value;
 9683:     var msg = "";
 9684: 
 9685:     if (srchterm == "") {
 9686:         checkok = 0;
 9687:         msg += "$lt{'youm'}\\n";
 9688:     }
 9689: 
 9690:     if (srchtype== 'begins') {
 9691:         if (srchterm.length < 2) {
 9692:             checkok = 0;
 9693:             msg += "$lt{'thte'}\\n";
 9694:         }
 9695:     }
 9696: 
 9697:     if (srchtype== 'contains') {
 9698:         if (srchterm.length < 3) {
 9699:             checkok = 0;
 9700:             msg += "$lt{'thet'}\\n";
 9701:         }
 9702:     }
 9703:     if (srchin == 'instd') {
 9704:         if (srchdomain == '') {
 9705:             checkok = 0;
 9706:             msg += "$lt{'yomc'}\\n";
 9707:         }
 9708:     }
 9709:     if (srchin == 'dom') {
 9710:         if (srchdomain == '') {
 9711:             checkok = 0;
 9712:             msg += "$lt{'ymcd'}\\n";
 9713:         }
 9714:     }
 9715:     if (srchby == 'lastfirst') {
 9716:         if (srchterm.indexOf(",") == -1) {
 9717:             checkok = 0;
 9718:             msg += "$lt{'whus'}\\n";
 9719:         }
 9720:         if (srchterm.indexOf(",") == srchterm.length -1) {
 9721:             checkok = 0;
 9722:             msg += "$lt{'whse'}\\n";
 9723:         }
 9724:     }
 9725:     if (checkok == 0) {
 9726:         alert("$lt{'thfo'}\\n"+msg);
 9727:         return;
 9728:     }
 9729:     if (checkok == 1) {
 9730:         callingForm.submit();
 9731:     }
 9732: }
 9733: 
 9734: $newuserscript
 9735: 
 9736: // ]]>
 9737: </script>
 9738: 
 9739: $new_user_create
 9740: 
 9741: END_BLOCK
 9742: 
 9743:     $output .= &Apache::lonhtmlcommon::start_pick_box().
 9744:                &Apache::lonhtmlcommon::row_title($lt{'doma'}).
 9745:                $domform.
 9746:                &Apache::lonhtmlcommon::row_closure().
 9747:                &Apache::lonhtmlcommon::row_title($lt{'usr'}).
 9748:                $srchbysel.
 9749:                $srchtypesel. 
 9750:                '<input type="text" size="15" name="srchterm" value="'.$srchterm.'" />'.
 9751:                $srchinsel.
 9752:                &Apache::lonhtmlcommon::row_closure(1). 
 9753:                &Apache::lonhtmlcommon::end_pick_box().
 9754:                '<br />';
 9755:     return $output;
 9756: }
 9757: 
 9758: sub user_rule_check {
 9759:     my ($usershash,$checks,$alerts,$rulematch,$inst_results,$curr_rules,$got_rules) = @_;
 9760:     my $response;
 9761:     if (ref($usershash) eq 'HASH') {
 9762:         foreach my $user (keys(%{$usershash})) {
 9763:             my ($uname,$udom) = split(/:/,$user);
 9764:             next if ($udom eq '' || $uname eq '');
 9765:             my ($id,$newuser);
 9766:             if (ref($usershash->{$user}) eq 'HASH') {
 9767:                 $newuser = $usershash->{$user}->{'newuser'};
 9768:                 $id = $usershash->{$user}->{'id'};
 9769:             }
 9770:             my $inst_response;
 9771:             if (ref($checks) eq 'HASH') {
 9772:                 if (defined($checks->{'username'})) {
 9773:                     ($inst_response,%{$inst_results->{$user}}) = 
 9774:                         &Apache::lonnet::get_instuser($udom,$uname);
 9775:                 } elsif (defined($checks->{'id'})) {
 9776:                     ($inst_response,%{$inst_results->{$user}}) =
 9777:                         &Apache::lonnet::get_instuser($udom,undef,$id);
 9778:                 }
 9779:             } else {
 9780:                 ($inst_response,%{$inst_results->{$user}}) =
 9781:                     &Apache::lonnet::get_instuser($udom,$uname);
 9782:                 return;
 9783:             }
 9784:             if (!$got_rules->{$udom}) {
 9785:                 my %domconfig = &Apache::lonnet::get_dom('configuration',
 9786:                                                   ['usercreation'],$udom);
 9787:                 if (ref($domconfig{'usercreation'}) eq 'HASH') {
 9788:                     foreach my $item ('username','id') {
 9789:                         if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
 9790:                             $$curr_rules{$udom}{$item} = 
 9791:                                 $domconfig{'usercreation'}{$item.'_rule'};
 9792:                         }
 9793:                     }
 9794:                 }
 9795:                 $got_rules->{$udom} = 1;  
 9796:             }
 9797:             foreach my $item (keys(%{$checks})) {
 9798:                 if (ref($$curr_rules{$udom}) eq 'HASH') {
 9799:                     if (ref($$curr_rules{$udom}{$item}) eq 'ARRAY') {
 9800:                         if (@{$$curr_rules{$udom}{$item}} > 0) {
 9801:                             my %rule_check = &Apache::lonnet::inst_rulecheck($udom,$uname,$id,$item,$$curr_rules{$udom}{$item});
 9802:                             foreach my $rule (@{$$curr_rules{$udom}{$item}}) {
 9803:                                 if ($rule_check{$rule}) {
 9804:                                     $$rulematch{$user}{$item} = $rule;
 9805:                                     if ($inst_response eq 'ok') {
 9806:                                         if (ref($inst_results) eq 'HASH') {
 9807:                                             if (ref($inst_results->{$user}) eq 'HASH') {
 9808:                                                 if (keys(%{$inst_results->{$user}}) == 0) {
 9809:                                                     $$alerts{$item}{$udom}{$uname} = 1;
 9810:                                                 }
 9811:                                             }
 9812:                                         }
 9813:                                     }
 9814:                                     last;
 9815:                                 }
 9816:                             }
 9817:                         }
 9818:                     }
 9819:                 }
 9820:             }
 9821:         }
 9822:     }
 9823:     return;
 9824: }
 9825: 
 9826: sub user_rule_formats {
 9827:     my ($domain,$domdesc,$curr_rules,$check) = @_;
 9828:     my %text = ( 
 9829:                  'username' => 'Usernames',
 9830:                  'id'       => 'IDs',
 9831:                );
 9832:     my $output;
 9833:     my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($domain,$check);
 9834:     if ((ref($rules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
 9835:         if (@{$ruleorder} > 0) {
 9836:             $output = '<br />'.
 9837:                       &mt($text{$check}.' with the following format(s) may [_1]only[_2] be used for verified users at [_3]:',
 9838:                           '<span class="LC_cusr_emph">','</span>',$domdesc).
 9839:                       ' <ul>';
 9840:             foreach my $rule (@{$ruleorder}) {
 9841:                 if (ref($curr_rules) eq 'ARRAY') {
 9842:                     if (grep(/^\Q$rule\E$/,@{$curr_rules})) {
 9843:                         if (ref($rules->{$rule}) eq 'HASH') {
 9844:                             $output .= '<li>'.$rules->{$rule}{'name'}.': '.
 9845:                                         $rules->{$rule}{'desc'}.'</li>';
 9846:                         }
 9847:                     }
 9848:                 }
 9849:             }
 9850:             $output .= '</ul>';
 9851:         }
 9852:     }
 9853:     return $output;
 9854: }
 9855: 
 9856: sub instrule_disallow_msg {
 9857:     my ($checkitem,$domdesc,$count,$mode) = @_;
 9858:     my $response;
 9859:     my %text = (
 9860:                   item   => 'username',
 9861:                   items  => 'usernames',
 9862:                   match  => 'matches',
 9863:                   do     => 'does',
 9864:                   action => 'a username',
 9865:                   one    => 'one',
 9866:                );
 9867:     if ($count > 1) {
 9868:         $text{'item'} = 'usernames';
 9869:         $text{'match'} ='match';
 9870:         $text{'do'} = 'do';
 9871:         $text{'action'} = 'usernames',
 9872:         $text{'one'} = 'ones';
 9873:     }
 9874:     if ($checkitem eq 'id') {
 9875:         $text{'items'} = 'IDs';
 9876:         $text{'item'} = 'ID';
 9877:         $text{'action'} = 'an ID';
 9878:         if ($count > 1) {
 9879:             $text{'item'} = 'IDs';
 9880:             $text{'action'} = 'IDs';
 9881:         }
 9882:     }
 9883:     $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 />';
 9884:     if ($mode eq 'upload') {
 9885:         if ($checkitem eq 'username') {
 9886:             $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'}.");
 9887:         } elsif ($checkitem eq 'id') {
 9888:             $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.");
 9889:         }
 9890:     } elsif ($mode eq 'selfcreate') {
 9891:         if ($checkitem eq 'id') {
 9892:             $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.");
 9893:         }
 9894:     } else {
 9895:         if ($checkitem eq 'username') {
 9896:             $response .= &mt("You must choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}.");
 9897:         } elsif ($checkitem eq 'id') {
 9898:             $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.");
 9899:         }
 9900:     }
 9901:     return $response;
 9902: }
 9903: 
 9904: sub personal_data_fieldtitles {
 9905:     my %fieldtitles = &Apache::lonlocal::texthash (
 9906:                         id => 'Student/Employee ID',
 9907:                         permanentemail => 'E-mail address',
 9908:                         lastname => 'Last Name',
 9909:                         firstname => 'First Name',
 9910:                         middlename => 'Middle Name',
 9911:                         generation => 'Generation',
 9912:                         gen => 'Generation',
 9913:                         inststatus => 'Affiliation',
 9914:                    );
 9915:     return %fieldtitles;
 9916: }
 9917: 
 9918: sub sorted_inst_types {
 9919:     my ($dom) = @_;
 9920:     my ($usertypes,$order);
 9921:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9922:     if (ref($domdefaults{'inststatus'}) eq 'HASH') {
 9923:         $usertypes = $domdefaults{'inststatus'}{'inststatustypes'};
 9924:         $order = $domdefaults{'inststatus'}{'inststatusorder'};
 9925:     } else {
 9926:         ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($dom);
 9927:     }
 9928:     my $othertitle = &mt('All users');
 9929:     if ($env{'request.course.id'}) {
 9930:         $othertitle  = &mt('Any users');
 9931:     }
 9932:     my @types;
 9933:     if (ref($order) eq 'ARRAY') {
 9934:         @types = @{$order};
 9935:     }
 9936:     if (@types == 0) {
 9937:         if (ref($usertypes) eq 'HASH') {
 9938:             @types = sort(keys(%{$usertypes}));
 9939:         }
 9940:     }
 9941:     if (keys(%{$usertypes}) > 0) {
 9942:         $othertitle = &mt('Other users');
 9943:     }
 9944:     return ($othertitle,$usertypes,\@types);
 9945: }
 9946: 
 9947: sub get_institutional_codes {
 9948:     my ($settings,$allcourses,$LC_code) = @_;
 9949: # Get complete list of course sections to update
 9950:     my @currsections = ();
 9951:     my @currxlists = ();
 9952:     my $coursecode = $$settings{'internal.coursecode'};
 9953: 
 9954:     if ($$settings{'internal.sectionnums'} ne '') {
 9955:         @currsections = split(/,/,$$settings{'internal.sectionnums'});
 9956:     }
 9957: 
 9958:     if ($$settings{'internal.crosslistings'} ne '') {
 9959:         @currxlists = split(/,/,$$settings{'internal.crosslistings'});
 9960:     }
 9961: 
 9962:     if (@currxlists > 0) {
 9963:         foreach (@currxlists) {
 9964:             if (m/^([^:]+):(\w*)$/) {
 9965:                 unless (grep/^$1$/,@{$allcourses}) {
 9966:                     push @{$allcourses},$1;
 9967:                     $$LC_code{$1} = $2;
 9968:                 }
 9969:             }
 9970:         }
 9971:     }
 9972:  
 9973:     if (@currsections > 0) {
 9974:         foreach (@currsections) {
 9975:             if (m/^(\w+):(\w*)$/) {
 9976:                 my $sec = $coursecode.$1;
 9977:                 my $lc_sec = $2;
 9978:                 unless (grep/^$sec$/,@{$allcourses}) {
 9979:                     push @{$allcourses},$sec;
 9980:                     $$LC_code{$sec} = $lc_sec;
 9981:                 }
 9982:             }
 9983:         }
 9984:     }
 9985:     return;
 9986: }
 9987: 
 9988: sub get_standard_codeitems {
 9989:     return ('Year','Semester','Department','Number','Section');
 9990: }
 9991: 
 9992: =pod
 9993: 
 9994: =head1 Slot Helpers
 9995: 
 9996: =over 4
 9997: 
 9998: =item * sorted_slots()
 9999: 
10000: Sorts an array of slot names in order of an optional sort key,
10001: default sort is by slot start time (earliest first). 
10002: 
10003: Inputs:
10004: 
10005: =over 4
10006: 
10007: slotsarr  - Reference to array of unsorted slot names.
10008: 
10009: slots     - Reference to hash of hash, where outer hash keys are slot names.
10010: 
10011: sortkey   - Name of key in inner hash to be sorted on (e.g., starttime).
10012: 
10013: =back
10014: 
10015: Returns:
10016: 
10017: =over 4
10018: 
10019: sorted   - An array of slot names sorted by a specified sort key 
10020:            (default sort key is start time of the slot).
10021: 
10022: =back
10023: 
10024: =cut
10025: 
10026: 
10027: sub sorted_slots {
10028:     my ($slotsarr,$slots,$sortkey) = @_;
10029:     if ($sortkey eq '') {
10030:         $sortkey = 'starttime';
10031:     }
10032:     my @sorted;
10033:     if ((ref($slotsarr) eq 'ARRAY') && (ref($slots) eq 'HASH')) {
10034:         @sorted =
10035:             sort {
10036:                      if (ref($slots->{$a}) && ref($slots->{$b})) {
10037:                          return $slots->{$a}{$sortkey} <=> $slots->{$b}{$sortkey}
10038:                      }
10039:                      if (ref($slots->{$a})) { return -1;}
10040:                      if (ref($slots->{$b})) { return 1;}
10041:                      return 0;
10042:                  } @{$slotsarr};
10043:     }
10044:     return @sorted;
10045: }
10046: 
10047: =pod
10048: 
10049: =item * get_future_slots()
10050: 
10051: Inputs:
10052: 
10053: =over 4
10054: 
10055: cnum - course number
10056: 
10057: cdom - course domain
10058: 
10059: now - current UNIX time
10060: 
10061: symb - optional symb
10062: 
10063: =back
10064: 
10065: Returns:
10066: 
10067: =over 4
10068: 
10069: sorted_reservable - ref to array of student_schedulable slots currently 
10070:                     reservable, ordered by end date of reservation period.
10071: 
10072: reservable_now - ref to hash of student_schedulable slots currently
10073:                  reservable.
10074: 
10075:     Keys in inner hash are:
10076:     (a) symb: either blank or symb to which slot use is restricted.
10077:     (b) endreserve: end date of reservation period. 
10078: 
10079: sorted_future - ref to array of student_schedulable slots reservable in
10080:                 the future, ordered by start date of reservation period.
10081: 
10082: future_reservable - ref to hash of student_schedulable slots reservable
10083:                     in the future.
10084: 
10085:     Keys in inner hash are:
10086:     (a) symb: either blank or symb to which slot use is restricted.
10087:     (b) startreserve:  start date of reservation period.
10088: 
10089: =back
10090: 
10091: =cut
10092: 
10093: sub get_future_slots {
10094:     my ($cnum,$cdom,$now,$symb) = @_;
10095:     my (%reservable_now,%future_reservable,@sorted_reservable,@sorted_future);
10096:     my %slots = &Apache::lonnet::get_course_slots($cnum,$cdom);
10097:     foreach my $slot (keys(%slots)) {
10098:         next unless($slots{$slot}->{'type'} eq 'schedulable_student');
10099:         if ($symb) {
10100:             next if (($slots{$slot}->{'symb'} ne '') && 
10101:                      ($slots{$slot}->{'symb'} ne $symb));
10102:         }
10103:         if (($slots{$slot}->{'starttime'} > $now) &&
10104:             ($slots{$slot}->{'endtime'} > $now)) {
10105:             if (($slots{$slot}->{'allowedsections'}) || ($slots{$slot}->{'allowedusers'})) {
10106:                 my $userallowed = 0;
10107:                 if ($slots{$slot}->{'allowedsections'}) {
10108:                     my @allowed_sec = split(',',$slots{$slot}->{'allowedsections'});
10109:                     if (!defined($env{'request.role.sec'})
10110:                         && grep(/^No section assigned$/,@allowed_sec)) {
10111:                         $userallowed=1;
10112:                     } else {
10113:                         if (grep(/^\Q$env{'request.role.sec'}\E$/,@allowed_sec)) {
10114:                             $userallowed=1;
10115:                         }
10116:                     }
10117:                     unless ($userallowed) {
10118:                         if (defined($env{'request.course.groups'})) {
10119:                             my @groups = split(/:/,$env{'request.course.groups'});
10120:                             foreach my $group (@groups) {
10121:                                 if (grep(/^\Q$group\E$/,@allowed_sec)) {
10122:                                     $userallowed=1;
10123:                                     last;
10124:                                 }
10125:                             }
10126:                         }
10127:                     }
10128:                 }
10129:                 if ($slots{$slot}->{'allowedusers'}) {
10130:                     my @allowed_users = split(',',$slots{$slot}->{'allowedusers'});
10131:                     my $user = $env{'user.name'}.':'.$env{'user.domain'};
10132:                     if (grep(/^\Q$user\E$/,@allowed_users)) {
10133:                         $userallowed = 1;
10134:                     }
10135:                 }
10136:                 next unless($userallowed);
10137:             }
10138:             my $startreserve = $slots{$slot}->{'startreserve'};
10139:             my $endreserve = $slots{$slot}->{'endreserve'};
10140:             my $symb = $slots{$slot}->{'symb'};
10141:             if (($startreserve < $now) &&
10142:                 (!$endreserve || $endreserve > $now)) {
10143:                 my $lastres = $endreserve;
10144:                 if (!$lastres) {
10145:                     $lastres = $slots{$slot}->{'starttime'};
10146:                 }
10147:                 $reservable_now{$slot} = {
10148:                                            symb       => $symb,
10149:                                            endreserve => $lastres
10150:                                          };
10151:             } elsif (($startreserve > $now) &&
10152:                      (!$endreserve || $endreserve > $startreserve)) {
10153:                 $future_reservable{$slot} = {
10154:                                               symb         => $symb,
10155:                                               startreserve => $startreserve
10156:                                             };
10157:             }
10158:         }
10159:     }
10160:     my @unsorted_reservable = keys(%reservable_now);
10161:     if (@unsorted_reservable > 0) {
10162:         @sorted_reservable = 
10163:             &sorted_slots(\@unsorted_reservable,\%reservable_now,'endreserve');
10164:     }
10165:     my @unsorted_future = keys(%future_reservable);
10166:     if (@unsorted_future > 0) {
10167:         @sorted_future =
10168:             &sorted_slots(\@unsorted_future,\%future_reservable,'startreserve');
10169:     }
10170:     return (\@sorted_reservable,\%reservable_now,\@sorted_future,\%future_reservable);
10171: }
10172: 
10173: =pod
10174: 
10175: =back
10176: 
10177: =head1 HTTP Helpers
10178: 
10179: =over 4
10180: 
10181: =item * &get_unprocessed_cgi($query,$possible_names)
10182: 
10183: Modify the %env hash to contain unprocessed CGI form parameters held in
10184: $query.  The parameters listed in $possible_names (an array reference),
10185: will be set in $env{'form.name'} if they do not already exist.
10186: 
10187: Typically called with $ENV{'QUERY_STRING'} as the first parameter.  
10188: $possible_names is an ref to an array of form element names.  As an example:
10189: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
10190: will result in $env{'form.uname'} and $env{'form.udom'} being set.
10191: 
10192: =cut
10193: 
10194: sub get_unprocessed_cgi {
10195:   my ($query,$possible_names)= @_;
10196:   # $Apache::lonxml::debug=1;
10197:   foreach my $pair (split(/&/,$query)) {
10198:     my ($name, $value) = split(/=/,$pair);
10199:     $name = &unescape($name);
10200:     if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
10201:       $value =~ tr/+/ /;
10202:       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
10203:       unless (defined($env{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
10204:     }
10205:   }
10206: }
10207: 
10208: =pod
10209: 
10210: =item * &cacheheader() 
10211: 
10212: returns cache-controlling header code
10213: 
10214: =cut
10215: 
10216: sub cacheheader {
10217:     unless ($env{'request.method'} eq 'GET') { return ''; }
10218:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
10219:     my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
10220:                 <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
10221:                 <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
10222:     return $output;
10223: }
10224: 
10225: =pod
10226: 
10227: =item * &no_cache($r) 
10228: 
10229: specifies header code to not have cache
10230: 
10231: =cut
10232: 
10233: sub no_cache {
10234:     my ($r) = @_;
10235:     if ($ENV{'REQUEST_METHOD'} ne 'GET' &&
10236: 	$env{'request.method'} ne 'GET') { return ''; }
10237:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time));
10238:     $r->no_cache(1);
10239:     $r->header_out("Expires" => $date);
10240:     $r->header_out("Pragma" => "no-cache");
10241: }
10242: 
10243: sub content_type {
10244:     my ($r,$type,$charset) = @_;
10245:     if ($r) {
10246: 	#  Note that printout.pl calls this with undef for $r.
10247: 	&no_cache($r);
10248:     }
10249:     if ($env{'browser.mathml'} && $type eq 'text/html') { $type='text/xml'; }
10250:     unless ($charset) {
10251: 	$charset=&Apache::lonlocal::current_encoding;
10252:     }
10253:     if ($charset) { $type.='; charset='.$charset; }
10254:     if ($r) {
10255: 	$r->content_type($type);
10256:     } else {
10257: 	print("Content-type: $type\n\n");
10258:     }
10259: }
10260: 
10261: =pod
10262: 
10263: =item * &add_to_env($name,$value) 
10264: 
10265: adds $name to the %env hash with value
10266: $value, if $name already exists, the entry is converted to an array
10267: reference and $value is added to the array.
10268: 
10269: =cut
10270: 
10271: sub add_to_env {
10272:   my ($name,$value)=@_;
10273:   if (defined($env{$name})) {
10274:     if (ref($env{$name})) {
10275:       #already have multiple values
10276:       push(@{ $env{$name} },$value);
10277:     } else {
10278:       #first time seeing multiple values, convert hash entry to an arrayref
10279:       my $first=$env{$name};
10280:       undef($env{$name});
10281:       push(@{ $env{$name} },$first,$value);
10282:     }
10283:   } else {
10284:     $env{$name}=$value;
10285:   }
10286: }
10287: 
10288: =pod
10289: 
10290: =item * &get_env_multiple($name) 
10291: 
10292: gets $name from the %env hash, it seemlessly handles the cases where multiple
10293: values may be defined and end up as an array ref.
10294: 
10295: returns an array of values
10296: 
10297: =cut
10298: 
10299: sub get_env_multiple {
10300:     my ($name) = @_;
10301:     my @values;
10302:     if (defined($env{$name})) {
10303:         # exists is it an array
10304:         if (ref($env{$name})) {
10305:             @values=@{ $env{$name} };
10306:         } else {
10307:             $values[0]=$env{$name};
10308:         }
10309:     }
10310:     return(@values);
10311: }
10312: 
10313: sub ask_for_embedded_content {
10314:     my ($actionurl,$state,$allfiles,$codebase,$args)=@_;
10315:     my (%subdependencies,%dependencies,%mapping,%existing,%newfiles,%pathchanges,
10316:         %currsubfile,%unused,$rem);
10317:     my $counter = 0;
10318:     my $numnew = 0;
10319:     my $numremref = 0;
10320:     my $numinvalid = 0;
10321:     my $numpathchg = 0;
10322:     my $numexisting = 0;
10323:     my $numunused = 0;
10324:     my ($output,$upload_output,$toplevel,$url,$udom,$uname,$getpropath,$cdom,$cnum,
10325:         $fileloc,$filename,$delete_output,$modify_output,$title,$symb,$path,$navmap);
10326:     my $heading = &mt('Upload embedded files');
10327:     my $buttontext = &mt('Upload');
10328: 
10329:     if ($env{'request.course.id'}) {
10330:         if ($actionurl eq '/adm/dependencies') {
10331:             $navmap = Apache::lonnavmaps::navmap->new();
10332:         }
10333:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10334:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10335:     }
10336:     if (($actionurl eq '/adm/portfolio') || 
10337:         ($actionurl eq '/adm/coursegrp_portfolio')) {
10338:         my $current_path='/';
10339:         if ($env{'form.currentpath'}) {
10340:             $current_path = $env{'form.currentpath'};
10341:         }
10342:         if ($actionurl eq '/adm/coursegrp_portfolio') {
10343:             $udom = $cdom;
10344:             $uname = $cnum;
10345:             $url = '/userfiles/groups/'.$env{'form.group'}.'/portfolio';
10346:         } else {
10347:             $udom = $env{'user.domain'};
10348:             $uname = $env{'user.name'};
10349:             $url = '/userfiles/portfolio';
10350:         }
10351:         $toplevel = $url.'/';
10352:         $url .= $current_path;
10353:         $getpropath = 1;
10354:     } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank') ||
10355:              ($actionurl eq '/adm/imsimport')) { 
10356:         my ($udom,$uname,$rest) = ($args->{'current_path'} =~ m{/priv/($match_domain)/($match_username)/?(.*)$});
10357:         $url = $Apache::lonnet::perlvar{'lonDocRoot'}."/priv/$udom/$uname/";
10358:         $toplevel = $url;
10359:         if ($rest ne '') {
10360:             $url .= $rest;
10361:         }
10362:     } elsif ($actionurl eq '/adm/coursedocs') {
10363:         if (ref($args) eq 'HASH') {
10364:             $url = $args->{'docs_url'};
10365:             $toplevel = $url;
10366:             if ($args->{'context'} eq 'paste') {
10367:                 ($cdom,$cnum) = ($url =~ m{^\Q/uploaded/\E($match_domain)/($match_courseid)/});
10368:                 ($path) = 
10369:                     ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/\E(?:docs|supplemental)/(?:default|\d+)/\d+)/});
10370:                 $fileloc = &Apache::lonnet::filelocation('',$toplevel);
10371:                 $fileloc =~ s{^/}{};
10372:             }
10373:         }
10374:     } elsif ($actionurl eq '/adm/dependencies')  {
10375:         if ($env{'request.course.id'} ne '') {
10376:             if (ref($args) eq 'HASH') {
10377:                 $url = $args->{'docs_url'};
10378:                 $title = $args->{'docs_title'};
10379:                 $toplevel = $url; 
10380:                 unless ($toplevel =~ m{^/}) {
10381:                     $toplevel = "/$url";
10382:                 }
10383:                 ($rem) = ($toplevel =~ m{^(.+/)[^/]+$});
10384:                 if ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/portfolio/syllabus\E)}) {
10385:                     $path = $1;
10386:                 } else {
10387:                     ($path) =
10388:                         ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/\E(?:docs|supplemental)/(?:default|\d+)/\d+)/});
10389:                 }
10390:                 if ($toplevel=~/^\/*(uploaded|editupload)/) {
10391:                     $fileloc = $toplevel;
10392:                     $fileloc=~ s/^\s*(\S+)\s*$/$1/;
10393:                     my ($udom,$uname,$fname) =
10394:                         ($fileloc=~ m{^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$});
10395:                     $fileloc = propath($udom,$uname).'/userfiles/'.$fname;
10396:                 } else {
10397:                     $fileloc = &Apache::lonnet::filelocation('',$toplevel);
10398:                 }
10399:                 $fileloc =~ s{^/}{};
10400:                 ($filename) = ($fileloc =~ m{.+/([^/]+)$});
10401:                 $heading = &mt('Status of dependencies in [_1]',"$title ($filename)");
10402:             }
10403:         }
10404:     } elsif ($actionurl eq "/public/$cdom/$cnum/syllabus") {
10405:         $udom = $cdom;
10406:         $uname = $cnum;
10407:         $url = "/uploaded/$cdom/$cnum/portfolio/syllabus";
10408:         $toplevel = $url;
10409:         $path = $url;
10410:         $fileloc = &Apache::lonnet::filelocation('',$toplevel).'/';
10411:         $fileloc =~ s{^/}{};
10412:     }
10413:     foreach my $file (keys(%{$allfiles})) {
10414:         my $embed_file;
10415:         if (($path eq "/uploaded/$cdom/$cnum/portfolio/syllabus") && ($file =~ m{^\Q$path/\E(.+)$})) {
10416:             $embed_file = $1;
10417:         } else {
10418:             $embed_file = $file;
10419:         }
10420:         my ($absolutepath,$cleaned_file);
10421:         if ($embed_file =~ m{^\w+://}) {
10422:             $cleaned_file = $embed_file;
10423:             $newfiles{$cleaned_file} = 1;
10424:             $mapping{$cleaned_file} = $embed_file;
10425:         } else {
10426:             $cleaned_file = &clean_path($embed_file);
10427:             if ($embed_file =~ m{^/}) {
10428:                 $absolutepath = $embed_file;
10429:             }
10430:             if ($cleaned_file =~ m{/}) {
10431:                 my ($path,$fname) = ($cleaned_file =~ m{^(.+)/([^/]*)$});
10432:                 $path = &check_for_traversal($path,$url,$toplevel);
10433:                 my $item = $fname;
10434:                 if ($path ne '') {
10435:                     $item = $path.'/'.$fname;
10436:                     $subdependencies{$path}{$fname} = 1;
10437:                 } else {
10438:                     $dependencies{$item} = 1;
10439:                 }
10440:                 if ($absolutepath) {
10441:                     $mapping{$item} = $absolutepath;
10442:                 } else {
10443:                     $mapping{$item} = $embed_file;
10444:                 }
10445:             } else {
10446:                 $dependencies{$embed_file} = 1;
10447:                 if ($absolutepath) {
10448:                     $mapping{$cleaned_file} = $absolutepath;
10449:                 } else {
10450:                     $mapping{$cleaned_file} = $embed_file;
10451:                 }
10452:             }
10453:         }
10454:     }
10455:     my $dirptr = 16384;
10456:     foreach my $path (keys(%subdependencies)) {
10457:         $currsubfile{$path} = {};
10458:         if (($actionurl eq '/adm/portfolio') || 
10459:             ($actionurl eq '/adm/coursegrp_portfolio')) {
10460:             my ($sublistref,$listerror) =
10461:                 &Apache::lonnet::dirlist($url.$path,$udom,$uname,$getpropath);
10462:             if (ref($sublistref) eq 'ARRAY') {
10463:                 foreach my $line (@{$sublistref}) {
10464:                     my ($file_name,$rest) = split(/\&/,$line,2);
10465:                     $currsubfile{$path}{$file_name} = 1;
10466:                 }
10467:             }
10468:         } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
10469:             if (opendir(my $dir,$url.'/'.$path)) {
10470:                 my @subdir_list = grep(!/^\./,readdir($dir));
10471:                 map {$currsubfile{$path}{$_} = 1;} @subdir_list;
10472:             }
10473:         } elsif (($actionurl eq '/adm/dependencies') ||
10474:                  (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
10475:                   ($args->{'context'} eq 'paste')) ||
10476:                  ($actionurl eq "/public/$cdom/$cnum/syllabus")) {
10477:             if ($env{'request.course.id'} ne '') {
10478:                 my $dir;
10479:                 if ($actionurl eq "/public/$cdom/$cnum/syllabus") {
10480:                     $dir = $fileloc;
10481:                 } else {
10482:                     ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
10483:                 }
10484:                 if ($dir ne '') {
10485:                     my ($sublistref,$listerror) =
10486:                         &Apache::lonnet::dirlist($dir.$path,$cdom,$cnum,$getpropath,undef,'/');
10487:                     if (ref($sublistref) eq 'ARRAY') {
10488:                         foreach my $line (@{$sublistref}) {
10489:                             my ($file_name,$dom,undef,$testdir,undef,undef,undef,undef,$size,
10490:                                 undef,$mtime)=split(/\&/,$line,12);
10491:                             unless (($testdir&$dirptr) ||
10492:                                     ($file_name =~ /^\.\.?$/)) {
10493:                                 $currsubfile{$path}{$file_name} = [$size,$mtime];
10494:                             }
10495:                         }
10496:                     }
10497:                 }
10498:             }
10499:         }
10500:         foreach my $file (keys(%{$subdependencies{$path}})) {
10501:             if (exists($currsubfile{$path}{$file})) {
10502:                 my $item = $path.'/'.$file;
10503:                 unless ($mapping{$item} eq $item) {
10504:                     $pathchanges{$item} = 1;
10505:                 }
10506:                 $existing{$item} = 1;
10507:                 $numexisting ++;
10508:             } else {
10509:                 $newfiles{$path.'/'.$file} = 1;
10510:             }
10511:         }
10512:         if ($actionurl eq '/adm/dependencies') {
10513:             foreach my $path (keys(%currsubfile)) {
10514:                 if (ref($currsubfile{$path}) eq 'HASH') {
10515:                     foreach my $file (keys(%{$currsubfile{$path}})) {
10516:                          unless ($subdependencies{$path}{$file}) {
10517:                              next if (($rem ne '') &&
10518:                                       (($env{"httpref.$rem"."$path/$file"} ne '') ||
10519:                                        (ref($navmap) &&
10520:                                        (($navmap->getResourceByUrl($rem."$path/$file") ne '') ||
10521:                                         (($file =~ /^(.*\.s?html?)\.bak$/i) &&
10522:                                          ($navmap->getResourceByUrl($rem."$path/$1")))))));
10523:                              $unused{$path.'/'.$file} = 1; 
10524:                          }
10525:                     }
10526:                 }
10527:             }
10528:         }
10529:     }
10530:     my %currfile;
10531:     if (($actionurl eq '/adm/portfolio') ||
10532:         ($actionurl eq '/adm/coursegrp_portfolio')) {
10533:         my ($dirlistref,$listerror) =
10534:             &Apache::lonnet::dirlist($url,$udom,$uname,$getpropath);
10535:         if (ref($dirlistref) eq 'ARRAY') {
10536:             foreach my $line (@{$dirlistref}) {
10537:                 my ($file_name,$rest) = split(/\&/,$line,2);
10538:                 $currfile{$file_name} = 1;
10539:             }
10540:         }
10541:     } elsif (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
10542:         if (opendir(my $dir,$url)) {
10543:             my @dir_list = grep(!/^\./,readdir($dir));
10544:             map {$currfile{$_} = 1;} @dir_list;
10545:         }
10546:     } elsif (($actionurl eq '/adm/dependencies') ||
10547:              (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
10548:               ($args->{'context'} eq 'paste')) ||
10549:              ($actionurl eq "/public/$cdom/$cnum/syllabus")) {
10550:         if ($env{'request.course.id'} ne '') {
10551:             my ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
10552:             if ($dir ne '') {
10553:                 my ($dirlistref,$listerror) =
10554:                     &Apache::lonnet::dirlist($dir,$cdom,$cnum,$getpropath,undef,'/');
10555:                 if (ref($dirlistref) eq 'ARRAY') {
10556:                     foreach my $line (@{$dirlistref}) {
10557:                         my ($file_name,$dom,undef,$testdir,undef,undef,undef,undef,
10558:                             $size,undef,$mtime)=split(/\&/,$line,12);
10559:                         unless (($testdir&$dirptr) ||
10560:                                 ($file_name =~ /^\.\.?$/)) {
10561:                             $currfile{$file_name} = [$size,$mtime];
10562:                         }
10563:                     }
10564:                 }
10565:             }
10566:         }
10567:     }
10568:     foreach my $file (keys(%dependencies)) {
10569:         if (exists($currfile{$file})) {
10570:             unless ($mapping{$file} eq $file) {
10571:                 $pathchanges{$file} = 1;
10572:             }
10573:             $existing{$file} = 1;
10574:             $numexisting ++;
10575:         } else {
10576:             $newfiles{$file} = 1;
10577:         }
10578:     }
10579:     foreach my $file (keys(%currfile)) {
10580:         unless (($file eq $filename) ||
10581:                 ($file eq $filename.'.bak') ||
10582:                 ($dependencies{$file})) {
10583:             if ($actionurl eq '/adm/dependencies') {
10584:                 unless ($toplevel =~ m{^\Q/uploaded/$cdom/$cnum/portfolio/syllabus\E}) {
10585:                     next if (($rem ne '') &&
10586:                              (($env{"httpref.$rem".$file} ne '') ||
10587:                               (ref($navmap) &&
10588:                               (($navmap->getResourceByUrl($rem.$file) ne '') ||
10589:                                (($file =~ /^(.*\.s?html?)\.bak$/i) &&
10590:                                 ($navmap->getResourceByUrl($rem.$1)))))));
10591:                 }
10592:             }
10593:             $unused{$file} = 1;
10594:         }
10595:     }
10596:     if (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
10597:         ($args->{'context'} eq 'paste')) {
10598:         $counter = scalar(keys(%existing));
10599:         $numpathchg = scalar(keys(%pathchanges));
10600:         return ($output,$counter,$numpathchg,\%existing);
10601:     } elsif (($actionurl eq "/public/$cdom/$cnum/syllabus") && 
10602:              (ref($args) eq 'HASH') && ($args->{'context'} eq 'rewrites')) {
10603:         $counter = scalar(keys(%existing));
10604:         $numpathchg = scalar(keys(%pathchanges));
10605:         return ($output,$counter,$numpathchg,\%existing,\%mapping);
10606:     }
10607:     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%newfiles)) {
10608:         if ($actionurl eq '/adm/dependencies') {
10609:             next if ($embed_file =~ m{^\w+://});
10610:         }
10611:         $upload_output .= &start_data_table_row().
10612:                           '<td valign="top"><img src="'.&icon($embed_file).'" />&nbsp;'.
10613:                           '<span class="LC_filename">'.$embed_file.'</span>';
10614:         unless ($mapping{$embed_file} eq $embed_file) {
10615:             $upload_output .= '<br /><span class="LC_info" style="font-size:smaller;">'.
10616:                               &mt('changed from: [_1]',$mapping{$embed_file}).'</span>';
10617:         }
10618:         $upload_output .= '</td>';
10619:         if ($args->{'ignore_remote_references'} && $embed_file =~ m{^\w+://}) { 
10620:             $upload_output.='<td align="right">'.
10621:                             '<span class="LC_info LC_fontsize_medium">'.
10622:                             &mt("URL points to web address").'</span>';
10623:             $numremref++;
10624:         } elsif ($args->{'error_on_invalid_names'}
10625:             && $embed_file ne &Apache::lonnet::clean_filename($embed_file,{'keep_path' => 1,})) {
10626:             $upload_output.='<td align="right"><span class="LC_warning">'.
10627:                             &mt('Invalid characters').'</span>';
10628:             $numinvalid++;
10629:         } else {
10630:             $upload_output .= '<td>'.
10631:                               &embedded_file_element('upload_embedded',$counter,
10632:                                                      $embed_file,\%mapping,
10633:                                                      $allfiles,$codebase,'upload');
10634:             $counter ++;
10635:             $numnew ++;
10636:         }
10637:         $upload_output .= '</td>'.&Apache::loncommon::end_data_table_row()."\n";
10638:     }
10639:     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%existing)) {
10640:         if ($actionurl eq '/adm/dependencies') {
10641:             my ($size,$mtime) = &get_dependency_details(\%currfile,\%currsubfile,$embed_file);
10642:             $modify_output .= &start_data_table_row().
10643:                               '<td><a href="'.$path.'/'.$embed_file.'" style="text-decoration:none;">'.
10644:                               '<img src="'.&icon($embed_file).'" border="0" />'.
10645:                               '&nbsp;<span class="LC_filename">'.$embed_file.'</span></a></td>'.
10646:                               '<td>'.$size.'</td>'.
10647:                               '<td>'.$mtime.'</td>'.
10648:                               '<td><label><input type="checkbox" name="mod_upload_dep" '.
10649:                               'onclick="toggleBrowse('."'$counter'".')" id="mod_upload_dep_'.
10650:                               $counter.'" value="'.$counter.'" />'.&mt('Yes').'</label>'.
10651:                               '<div id="moduploaddep_'.$counter.'" style="display:none;">'.
10652:                               &embedded_file_element('upload_embedded',$counter,
10653:                                                      $embed_file,\%mapping,
10654:                                                      $allfiles,$codebase,'modify').
10655:                               '</div></td>'.
10656:                               &end_data_table_row()."\n";
10657:             $counter ++;
10658:         } else {
10659:             $upload_output .= &start_data_table_row().
10660:                               '<td valign="top"><img src="'.&icon($embed_file).'" />&nbsp;'.
10661:                               '<span class="LC_filename">'.$embed_file.'</span></td>'.
10662:                               '<td align="right"><span class="LC_info LC_fontsize_medium">'.&mt('Already exists').'</span></td>'.
10663:                               &Apache::loncommon::end_data_table_row()."\n";
10664:         }
10665:     }
10666:     my $delidx = $counter;
10667:     foreach my $oldfile (sort {lc($a) cmp lc($b)} keys(%unused)) {
10668:         my ($size,$mtime) = &get_dependency_details(\%currfile,\%currsubfile,$oldfile);
10669:         $delete_output .= &start_data_table_row().
10670:                           '<td><img src="'.&icon($oldfile).'" />'.
10671:                           '&nbsp;<span class="LC_filename">'.$oldfile.'</span></td>'.
10672:                           '<td>'.$size.'</td>'.
10673:                           '<td>'.$mtime.'</td>'.
10674:                           '<td><label><input type="checkbox" name="del_upload_dep" '.
10675:                           ' value="'.$delidx.'" />'.&mt('Yes').'</label>'.
10676:                           &embedded_file_element('upload_embedded',$delidx,
10677:                                                  $oldfile,\%mapping,$allfiles,
10678:                                                  $codebase,'delete').'</td>'.
10679:                           &end_data_table_row()."\n"; 
10680:         $numunused ++;
10681:         $delidx ++;
10682:     }
10683:     if ($upload_output) {
10684:         $upload_output = &start_data_table().
10685:                          $upload_output.
10686:                          &end_data_table()."\n";
10687:     }
10688:     if ($modify_output) {
10689:         $modify_output = &start_data_table().
10690:                          &start_data_table_header_row().
10691:                          '<th>'.&mt('File').'</th>'.
10692:                          '<th>'.&mt('Size (KB)').'</th>'.
10693:                          '<th>'.&mt('Modified').'</th>'.
10694:                          '<th>'.&mt('Upload replacement?').'</th>'.
10695:                          &end_data_table_header_row().
10696:                          $modify_output.
10697:                          &end_data_table()."\n";
10698:     }
10699:     if ($delete_output) {
10700:         $delete_output = &start_data_table().
10701:                          &start_data_table_header_row().
10702:                          '<th>'.&mt('File').'</th>'.
10703:                          '<th>'.&mt('Size (KB)').'</th>'.
10704:                          '<th>'.&mt('Modified').'</th>'.
10705:                          '<th>'.&mt('Delete?').'</th>'.
10706:                          &end_data_table_header_row().
10707:                          $delete_output.
10708:                          &end_data_table()."\n";
10709:     }
10710:     my $applies = 0;
10711:     if ($numremref) {
10712:         $applies ++;
10713:     }
10714:     if ($numinvalid) {
10715:         $applies ++;
10716:     }
10717:     if ($numexisting) {
10718:         $applies ++;
10719:     }
10720:     if ($counter || $numunused) {
10721:         $output = '<form name="upload_embedded" action="'.$actionurl.'"'.
10722:                   ' method="post" enctype="multipart/form-data">'."\n".
10723:                   $state.'<h3>'.$heading.'</h3>'; 
10724:         if ($actionurl eq '/adm/dependencies') {
10725:             if ($numnew) {
10726:                 $output .= '<h4>'.&mt('Missing dependencies').'</h4>'.
10727:                            '<p>'.&mt('The following files need to be uploaded.').'</p>'."\n".
10728:                            $upload_output.'<br />'."\n";
10729:             }
10730:             if ($numexisting) {
10731:                 $output .= '<h4>'.&mt('Uploaded dependencies (in use)').'</h4>'.
10732:                            '<p>'.&mt('Upload a new file to replace the one currently in use.').'</p>'."\n".
10733:                            $modify_output.'<br />'."\n";
10734:                            $buttontext = &mt('Save changes');
10735:             }
10736:             if ($numunused) {
10737:                 $output .= '<h4>'.&mt('Unused files').'</h4>'.
10738:                            '<p>'.&mt('The following uploaded files are no longer used.').'</p>'."\n".
10739:                            $delete_output.'<br />'."\n";
10740:                            $buttontext = &mt('Save changes');
10741:             }
10742:         } else {
10743:             $output .= $upload_output.'<br />'."\n";
10744:         }
10745:         $output .= '<input type ="hidden" name="number_embedded_items" value="'.
10746:                    $counter.'" />'."\n";
10747:         if ($actionurl eq '/adm/dependencies') { 
10748:             $output .= '<input type ="hidden" name="number_newemb_items" value="'.
10749:                        $numnew.'" />'."\n";
10750:         } elsif ($actionurl eq '') {
10751:             $output .=  '<input type="hidden" name="phase" value="three" />';
10752:         }
10753:     } elsif ($applies) {
10754:         $output = '<b>'.&mt('Referenced files').'</b>:<br />';
10755:         if ($applies > 1) {
10756:             $output .=  
10757:                 &mt('No dependencies need to be uploaded, as one of the following applies to each reference:').'<ul>';
10758:             if ($numremref) {
10759:                 $output .= '<li>'.&mt('reference is to a URL which points to another server').'</li>'."\n";
10760:             }
10761:             if ($numinvalid) {
10762:                 $output .= '<li>'.&mt('reference is to file with a name containing invalid characters').'</li>'."\n";
10763:             }
10764:             if ($numexisting) {
10765:                 $output .= '<li>'.&mt('reference is to an existing file at the specified location').'</li>'."\n";
10766:             }
10767:             $output .= '</ul><br />';
10768:         } elsif ($numremref) {
10769:             $output .= '<p>'.&mt('None to upload, as all references are to URLs pointing to another server.').'</p>';
10770:         } elsif ($numinvalid) {
10771:             $output .= '<p>'.&mt('None to upload, as all references are to files with names containing invalid characters.').'</p>';
10772:         } elsif ($numexisting) {
10773:             $output .= '<p>'.&mt('None to upload, as all references are to existing files.').'</p>';
10774:         }
10775:         $output .= $upload_output.'<br />';
10776:     }
10777:     my ($pathchange_output,$chgcount);
10778:     $chgcount = $counter;
10779:     if (keys(%pathchanges) > 0) {
10780:         foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%pathchanges)) {
10781:             if ($counter) {
10782:                 $output .= &embedded_file_element('pathchange',$chgcount,
10783:                                                   $embed_file,\%mapping,
10784:                                                   $allfiles,$codebase,'change');
10785:             } else {
10786:                 $pathchange_output .= 
10787:                     &start_data_table_row().
10788:                     '<td><input type ="checkbox" name="namechange" value="'.
10789:                     $chgcount.'" checked="checked" /></td>'.
10790:                     '<td>'.$mapping{$embed_file}.'</td>'.
10791:                     '<td>'.$embed_file.
10792:                     &embedded_file_element('pathchange',$numpathchg,$embed_file,
10793:                                            \%mapping,$allfiles,$codebase,'change').
10794:                     '</td>'.&end_data_table_row();
10795:             }
10796:             $numpathchg ++;
10797:             $chgcount ++;
10798:         }
10799:     }
10800:     if (($counter) || ($numunused)) {
10801:         if ($numpathchg) {
10802:             $output .= '<input type ="hidden" name="number_pathchange_items" value="'.
10803:                        $numpathchg.'" />'."\n";
10804:         }
10805:         if (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank') || 
10806:             ($actionurl eq '/adm/imsimport')) {
10807:             $output .= '<input type="hidden" name="phase" value="three" />'."\n";
10808:         } elsif ($actionurl eq '/adm/portfolio' || $actionurl eq '/adm/coursegrp_portfolio') {
10809:             $output .= '<input type="hidden" name="action" value="upload_embedded" />';
10810:         } elsif ($actionurl eq '/adm/dependencies') {
10811:             $output .= '<input type="hidden" name="action" value="process_changes" />';
10812:         }
10813:         $output .= '<input type ="submit" value="'.$buttontext.'" />'."\n".'</form>'."\n";
10814:     } elsif ($numpathchg) {
10815:         my %pathchange = ();
10816:         $output .= &modify_html_form('pathchange',$actionurl,$state,\%pathchange,$pathchange_output);
10817:         if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
10818:             $output .= '<p>'.&mt('or').'</p>'; 
10819:         }
10820:     }
10821:     return ($output,$counter,$numpathchg);
10822: }
10823: 
10824: =pod
10825: 
10826: =item * clean_path($name)
10827: 
10828: Performs clean-up of directories, subdirectories and filename in an
10829: embedded object, referenced in an HTML file which is being uploaded
10830: to a course or portfolio, where 
10831: "Upload embedded images/multimedia files if HTML file" checkbox was
10832: checked.
10833: 
10834: Clean-up is similar to replacements in lonnet::clean_filename()
10835: except each / between sub-directory and next level is preserved.
10836: 
10837: =cut
10838: 
10839: sub clean_path {
10840:     my ($embed_file) = @_;
10841:     $embed_file =~s{^/+}{};
10842:     my @contents;
10843:     if ($embed_file =~ m{/}) {
10844:         @contents = split(/\//,$embed_file);
10845:     } else {
10846:         @contents = ($embed_file);
10847:     }
10848:     my $lastidx = scalar(@contents)-1;
10849:     for (my $i=0; $i<=$lastidx; $i++) { 
10850:         $contents[$i]=~s{\\}{/}g;
10851:         $contents[$i]=~s/\s+/\_/g;
10852:         $contents[$i]=~s{[^/\w\.\-]}{}g;
10853:         if ($i == $lastidx) {
10854:             $contents[$i]=~s/\.(\d+)(?=\.)/_$1/g;
10855:         }
10856:     }
10857:     if ($lastidx > 0) {
10858:         return join('/',@contents);
10859:     } else {
10860:         return $contents[0];
10861:     }
10862: }
10863: 
10864: sub embedded_file_element {
10865:     my ($context,$num,$embed_file,$mapping,$allfiles,$codebase,$type) = @_;
10866:     return unless ((ref($mapping) eq 'HASH') && (ref($allfiles) eq 'HASH') &&
10867:                    (ref($codebase) eq 'HASH'));
10868:     my $output;
10869:     if (($context eq 'upload_embedded') && ($type ne 'delete')) {
10870:        $output = '<input name="embedded_item_'.$num.'" type="file" value="" />'."\n";
10871:     }
10872:     $output .= '<input name="embedded_orig_'.$num.'" type="hidden" value="'.
10873:                &escape($embed_file).'" />';
10874:     unless (($context eq 'upload_embedded') && 
10875:             ($mapping->{$embed_file} eq $embed_file)) {
10876:         $output .='
10877:         <input name="embedded_ref_'.$num.'" type="hidden" value="'.&escape($mapping->{$embed_file}).'" />';
10878:     }
10879:     my $attrib;
10880:     if (ref($allfiles->{$mapping->{$embed_file}}) eq 'ARRAY') {
10881:         $attrib = &escape(join(':',@{$allfiles->{$mapping->{$embed_file}}}));
10882:     }
10883:     $output .=
10884:         "\n\t\t".
10885:         '<input name="embedded_attrib_'.$num.'" type="hidden" value="'.
10886:         $attrib.'" />';
10887:     if (exists($codebase->{$mapping->{$embed_file}})) {
10888:         $output .=
10889:             "\n\t\t".
10890:             '<input name="codebase_'.$num.'" type="hidden" value="'.
10891:             &escape($codebase->{$mapping->{$embed_file}}).'" />';
10892:     }
10893:     return $output;
10894: }
10895: 
10896: sub get_dependency_details {
10897:     my ($currfile,$currsubfile,$embed_file) = @_;
10898:     my ($size,$mtime,$showsize,$showmtime);
10899:     if ((ref($currfile) eq 'HASH') && (ref($currsubfile))) {
10900:         if ($embed_file =~ m{/}) {
10901:             my ($path,$fname) = split(/\//,$embed_file);
10902:             if (ref($currsubfile->{$path}{$fname}) eq 'ARRAY') {
10903:                 ($size,$mtime) = @{$currsubfile->{$path}{$fname}};
10904:             }
10905:         } else {
10906:             if (ref($currfile->{$embed_file}) eq 'ARRAY') {
10907:                 ($size,$mtime) = @{$currfile->{$embed_file}};
10908:             }
10909:         }
10910:         $showsize = $size/1024.0;
10911:         $showsize = sprintf("%.1f",$showsize);
10912:         if ($mtime > 0) {
10913:             $showmtime = &Apache::lonlocal::locallocaltime($mtime);
10914:         }
10915:     }
10916:     return ($showsize,$showmtime);
10917: }
10918: 
10919: sub ask_embedded_js {
10920:     return <<"END";
10921: <script type="text/javascript"">
10922: // <![CDATA[
10923: function toggleBrowse(counter) {
10924:     var chkboxid = document.getElementById('mod_upload_dep_'+counter);
10925:     var fileid = document.getElementById('embedded_item_'+counter);
10926:     var uploaddivid = document.getElementById('moduploaddep_'+counter);
10927:     if (chkboxid.checked == true) {
10928:         uploaddivid.style.display='block';
10929:     } else {
10930:         uploaddivid.style.display='none';
10931:         fileid.value = '';
10932:     }
10933: }
10934: // ]]>
10935: </script>
10936: 
10937: END
10938: }
10939: 
10940: sub upload_embedded {
10941:     my ($context,$dirpath,$uname,$udom,$dir_root,$url_root,$group,$disk_quota,
10942:         $current_disk_usage,$hiddenstate,$actionurl) = @_;
10943:     my (%pathchange,$output,$modifyform,$footer,$returnflag);
10944:     for (my $i=0; $i<$env{'form.number_embedded_items'}; $i++) {
10945:         next if (!exists($env{'form.embedded_item_'.$i.'.filename'}));
10946:         my $orig_uploaded_filename =
10947:             $env{'form.embedded_item_'.$i.'.filename'};
10948:         foreach my $type ('orig','ref','attrib','codebase') {
10949:             if ($env{'form.embedded_'.$type.'_'.$i} ne '') {
10950:                 $env{'form.embedded_'.$type.'_'.$i} =
10951:                     &unescape($env{'form.embedded_'.$type.'_'.$i});
10952:             }
10953:         }
10954:         my ($path,$fname) =
10955:             ($env{'form.embedded_orig_'.$i} =~ m{(.*/)([^/]*)});
10956:         # no path, whole string is fname
10957:         if (!$fname) { $fname = $env{'form.embedded_orig_'.$i} };
10958:         $fname = &Apache::lonnet::clean_filename($fname);
10959:         # See if there is anything left
10960:         next if ($fname eq '');
10961: 
10962:         # Check if file already exists as a file or directory.
10963:         my ($state,$msg);
10964:         if ($context eq 'portfolio') {
10965:             my $port_path = $dirpath;
10966:             if ($group ne '') {
10967:                 $port_path = "groups/$group/$port_path";
10968:             }
10969:             ($state,$msg) = &check_for_upload($env{'form.currentpath'}.$path,
10970:                                               $fname,$group,'embedded_item_'.$i,
10971:                                               $dir_root,$port_path,$disk_quota,
10972:                                               $current_disk_usage,$uname,$udom);
10973:             if ($state eq 'will_exceed_quota'
10974:                 || $state eq 'file_locked') {
10975:                 $output .= $msg;
10976:                 next;
10977:             }
10978:         } elsif (($context eq 'author') || ($context eq 'testbank')) {
10979:             ($state,$msg) = &check_for_existing($path,$fname,'embedded_item_'.$i);
10980:             if ($state eq 'exists') {
10981:                 $output .= $msg;
10982:                 next;
10983:             }
10984:         }
10985:         # Check if extension is valid
10986:         if (($fname =~ /\.(\w+)$/) &&
10987:             (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
10988:             $output .= &mt('Invalid file extension ([_1]) - reserved for internal use.',$1)
10989:                       .' '.&mt('Rename the file with a different extension and re-upload.').'<br />';
10990:             next;
10991:         } elsif (($fname =~ /\.(\w+)$/) &&
10992:                  (!defined(&Apache::loncommon::fileembstyle($1)))) {
10993:             $output .= &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1).'<br />';
10994:             next;
10995:         } elsif ($fname=~/\.(\d+)\.(\w+)$/) {
10996:             $output .= &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2).'<br />';
10997:             next;
10998:         }
10999:         $env{'form.embedded_item_'.$i.'.filename'}=$fname;
11000:         my $subdir = $path;
11001:         $subdir =~ s{/+$}{};
11002:         if ($context eq 'portfolio') {
11003:             my $result;
11004:             if ($state eq 'existingfile') {
11005:                 $result=
11006:                     &Apache::lonnet::userfileupload('embedded_item_'.$i,'existingfile',
11007:                                                     $dirpath.$env{'form.currentpath'}.$subdir);
11008:             } else {
11009:                 $result=
11010:                     &Apache::lonnet::userfileupload('embedded_item_'.$i,'',
11011:                                                     $dirpath.
11012:                                                     $env{'form.currentpath'}.$subdir);
11013:                 if ($result !~ m|^/uploaded/|) {
11014:                     $output .= '<span class="LC_error">'
11015:                                .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
11016:                                ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
11017:                                .'</span><br />';
11018:                     next;
11019:                 } else {
11020:                     $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
11021:                                $path.$fname.'</span>').'<br />';     
11022:                 }
11023:             }
11024:         } elsif (($context eq 'coursedoc') || ($context eq 'syllabus')) {
11025:             my $extendedsubdir = $dirpath.'/'.$subdir;
11026:             $extendedsubdir =~ s{/+$}{};
11027:             my $result =
11028:                 &Apache::lonnet::userfileupload('embedded_item_'.$i,$context,$extendedsubdir);
11029:             if ($result !~ m|^/uploaded/|) {
11030:                 $output .= '<span class="LC_error">'
11031:                            .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
11032:                            ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
11033:                            .'</span><br />';
11034:                     next;
11035:             } else {
11036:                 $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
11037:                            $path.$fname.'</span>').'<br />';
11038:                 if ($context eq 'syllabus') {
11039:                     &Apache::lonnet::make_public_indefinitely($result);
11040:                 }
11041:             }
11042:         } else {
11043: # Save the file
11044:             my $target = $env{'form.embedded_item_'.$i};
11045:             my $fullpath = $dir_root.$dirpath.'/'.$path;
11046:             my $dest = $fullpath.$fname;
11047:             my $url = $url_root.$dirpath.'/'.$path.$fname;
11048:             my @parts=split(/\//,"$dirpath/$path");
11049:             my $count;
11050:             my $filepath = $dir_root;
11051:             foreach my $subdir (@parts) {
11052:                 $filepath .= "/$subdir";
11053:                 if (!-e $filepath) {
11054:                     mkdir($filepath,0770);
11055:                 }
11056:             }
11057:             my $fh;
11058:             if (!open($fh,'>'.$dest)) {
11059:                 &Apache::lonnet::logthis('Failed to create '.$dest);
11060:                 $output .= '<span class="LC_error">'.
11061:                            &mt('An error occurred while trying to upload [_1] for embedded element [_2].',
11062:                                $orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
11063:                            '</span><br />';
11064:             } else {
11065:                 if (!print $fh $env{'form.embedded_item_'.$i}) {
11066:                     &Apache::lonnet::logthis('Failed to write to '.$dest);
11067:                     $output .= '<span class="LC_error">'.
11068:                               &mt('An error occurred while writing the file [_1] for embedded element [_2].',
11069:                                   $orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
11070:                               '</span><br />';
11071:                 } else {
11072:                     $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
11073:                                $url.'</span>').'<br />';
11074:                     unless ($context eq 'testbank') {
11075:                         $footer .= &mt('View embedded file: [_1]',
11076:                                        '<a href="'.$url.'">'.$fname.'</a>').'<br />';
11077:                     }
11078:                 }
11079:                 close($fh);
11080:             }
11081:         }
11082:         if ($env{'form.embedded_ref_'.$i}) {
11083:             $pathchange{$i} = 1;
11084:         }
11085:     }
11086:     if ($output) {
11087:         $output = '<p>'.$output.'</p>';
11088:     }
11089:     $output .= &modify_html_form('upload_embedded',$actionurl,$hiddenstate,\%pathchange);
11090:     $returnflag = 'ok';
11091:     my $numpathchgs = scalar(keys(%pathchange));
11092:     if ($numpathchgs > 0) {
11093:         if ($context eq 'portfolio') {
11094:             $output .= '<p>'.&mt('or').'</p>';
11095:         } elsif ($context eq 'testbank') {
11096:             $output .=  '<p>'.&mt('Or [_1]continue[_2] the testbank import without modifying the reference(s).',
11097:                                   '<a href="javascript:document.testbankForm.submit();">','</a>').'</p>';
11098:             $returnflag = 'modify_orightml';
11099:         }
11100:     }
11101:     return ($output.$footer,$returnflag,$numpathchgs);
11102: }
11103: 
11104: sub modify_html_form {
11105:     my ($context,$actionurl,$hiddenstate,$pathchange,$pathchgtable) = @_;
11106:     my $end = 0;
11107:     my $modifyform;
11108:     if ($context eq 'upload_embedded') {
11109:         return unless (ref($pathchange) eq 'HASH');
11110:         if ($env{'form.number_embedded_items'}) {
11111:             $end += $env{'form.number_embedded_items'};
11112:         }
11113:         if ($env{'form.number_pathchange_items'}) {
11114:             $end += $env{'form.number_pathchange_items'};
11115:         }
11116:         if ($end) {
11117:             for (my $i=0; $i<$end; $i++) {
11118:                 if ($i < $env{'form.number_embedded_items'}) {
11119:                     next unless($pathchange->{$i});
11120:                 }
11121:                 $modifyform .=
11122:                     &start_data_table_row().
11123:                     '<td><input type ="checkbox" name="namechange" value="'.$i.'" '.
11124:                     'checked="checked" /></td>'.
11125:                     '<td>'.$env{'form.embedded_ref_'.$i}.
11126:                     '<input type="hidden" name="embedded_ref_'.$i.'" value="'.
11127:                     &escape($env{'form.embedded_ref_'.$i}).'" />'.
11128:                     '<input type="hidden" name="embedded_codebase_'.$i.'" value="'.
11129:                     &escape($env{'form.embedded_codebase_'.$i}).'" />'.
11130:                     '<input type="hidden" name="embedded_attrib_'.$i.'" value="'.
11131:                     &escape($env{'form.embedded_attrib_'.$i}).'" /></td>'.
11132:                     '<td>'.$env{'form.embedded_orig_'.$i}.
11133:                     '<input type="hidden" name="embedded_orig_'.$i.'" value="'.
11134:                     &escape($env{'form.embedded_orig_'.$i}).'" /></td>'.
11135:                     &end_data_table_row();
11136:             }
11137:         }
11138:     } else {
11139:         $modifyform = $pathchgtable;
11140:         if (($actionurl eq '/adm/upload') || ($actionurl eq '/adm/testbank')) {
11141:             $hiddenstate .= '<input type="hidden" name="phase" value="four" />';
11142:         } elsif (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
11143:             $hiddenstate .= '<input type="hidden" name="action" value="modify_orightml" />';
11144:         }
11145:     }
11146:     if ($modifyform) {
11147:         if ($actionurl eq '/adm/dependencies') {
11148:             $hiddenstate .= '<input type="hidden" name="action" value="modifyhrefs" />';
11149:         }
11150:         return '<h3>'.&mt('Changes in content of HTML file required').'</h3>'."\n".
11151:                '<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".
11152:                '<li>'.&mt('For consistency between the reference(s) and the location of the corresponding stored file within LON-CAPA.').'</li>'."\n".
11153:                '<li>'.&mt('To change absolute paths to relative paths, or replace directory traversal via "../" within the original reference.').'</li>'."\n".
11154:                '</ol></p>'."\n".'<p>'.
11155:                &mt('LON-CAPA can make the required changes to your HTML file.').'</p>'."\n".
11156:                '<form method="post" name="refchanger" action="'.$actionurl.'">'.
11157:                &start_data_table()."\n".
11158:                &start_data_table_header_row().
11159:                '<th>'.&mt('Change?').'</th>'.
11160:                '<th>'.&mt('Current reference').'</th>'.
11161:                '<th>'.&mt('Required reference').'</th>'.
11162:                &end_data_table_header_row()."\n".
11163:                $modifyform.
11164:                &end_data_table().'<br />'."\n".$hiddenstate.
11165:                '<input type="submit" name="pathchanges" value="'.&mt('Modify HTML file').'" />'.
11166:                '</form>'."\n";
11167:     }
11168:     return;
11169: }
11170: 
11171: sub modify_html_refs {
11172:     my ($context,$dirpath,$uname,$udom,$dir_root,$url) = @_;
11173:     my $container;
11174:     if ($context eq 'portfolio') {
11175:         $container = $env{'form.container'};
11176:     } elsif ($context eq 'coursedoc') {
11177:         $container = $env{'form.primaryurl'};
11178:     } elsif ($context eq 'manage_dependencies') {
11179:         (undef,undef,$container) = &Apache::lonnet::decode_symb($env{'form.symb'});
11180:         $container = "/$container";
11181:     } elsif ($context eq 'syllabus') {
11182:         $container = $url;
11183:     } else {
11184:         $container = $Apache::lonnet::perlvar{'lonDocRoot'}.$env{'form.filename'};
11185:     }
11186:     my (%allfiles,%codebase,$output,$content);
11187:     my @changes = &get_env_multiple('form.namechange');
11188:     unless ((@changes > 0) || ($context eq 'syllabus')) {
11189:         if (wantarray) {
11190:             return ('',0,0); 
11191:         } else {
11192:             return;
11193:         }
11194:     }
11195:     if (($context eq 'portfolio') || ($context eq 'coursedoc') || 
11196:         ($context eq 'manage_dependencies') || ($context eq 'syllabus')) {
11197:         unless ($container =~ m{^/uploaded/\Q$udom\E/\Q$uname\E/}) {
11198:             if (wantarray) {
11199:                 return ('',0,0);
11200:             } else {
11201:                 return;
11202:             }
11203:         } 
11204:         $content = &Apache::lonnet::getfile($container);
11205:         if ($content eq '-1') {
11206:             if (wantarray) {
11207:                 return ('',0,0);
11208:             } else {
11209:                 return;
11210:             }
11211:         }
11212:     } else {
11213:         unless ($container =~ /^\Q$dir_root\E/) {
11214:             if (wantarray) {
11215:                 return ('',0,0);
11216:             } else {
11217:                 return;
11218:             }
11219:         } 
11220:         if (open(my $fh,"<$container")) {
11221:             $content = join('', <$fh>);
11222:             close($fh);
11223:         } else {
11224:             if (wantarray) {
11225:                 return ('',0,0);
11226:             } else {
11227:                 return;
11228:             }
11229:         }
11230:     }
11231:     my ($count,$codebasecount) = (0,0);
11232:     my $mm = new File::MMagic;
11233:     my $mime_type = $mm->checktype_contents($content);
11234:     if ($mime_type eq 'text/html') {
11235:         my $parse_result = 
11236:             &Apache::lonnet::extract_embedded_items($container,\%allfiles,
11237:                                                     \%codebase,\$content);
11238:         if ($parse_result eq 'ok') {
11239:             foreach my $i (@changes) {
11240:                 my $orig = &unescape($env{'form.embedded_orig_'.$i});
11241:                 my $ref = &unescape($env{'form.embedded_ref_'.$i});
11242:                 if ($allfiles{$ref}) {
11243:                     my $newname =  $orig;
11244:                     my ($attrib_regexp,$codebase);
11245:                     $attrib_regexp = &unescape($env{'form.embedded_attrib_'.$i});
11246:                     if ($attrib_regexp =~ /:/) {
11247:                         $attrib_regexp =~ s/\:/|/g;
11248:                     }
11249:                     if ($content =~ m{($attrib_regexp\s*=\s*['"]?)\Q$ref\E(['"]?)}) {
11250:                         my $numchg = ($content =~ s{($attrib_regexp\s*=\s*['"]?)\Q$ref\E(['"]?)}{$1$newname$2}gi);
11251:                         $count += $numchg;
11252:                         $allfiles{$newname} = $allfiles{$ref};
11253:                         delete($allfiles{$ref});
11254:                     }
11255:                     if ($env{'form.embedded_codebase_'.$i} ne '') {
11256:                         $codebase = &unescape($env{'form.embedded_codebase_'.$i});
11257:                         my $numchg = ($content =~ s/(codebase\s*=\s*["']?)\Q$codebase\E(["']?)/$1.$2/i); #' stupid emacs
11258:                         $codebasecount ++;
11259:                     }
11260:                 }
11261:             }
11262:             my $skiprewrites;
11263:             if ($count || $codebasecount) {
11264:                 my $saveresult;
11265:                 if (($context eq 'portfolio') || ($context eq 'coursedoc') || 
11266:                     ($context eq 'manage_dependencies') || ($context eq 'syllabus')) {
11267:                     my $url = &Apache::lonnet::store_edited_file($container,$content,$udom,$uname,\$saveresult);
11268:                     if ($url eq $container) {
11269:                         my ($fname) = ($container =~ m{/([^/]+)$});
11270:                         $output = '<p>'.&mt('Updated [quant,_1,reference] in [_2].',
11271:                                             $count,'<span class="LC_filename">'.
11272:                                             $fname.'</span>').'</p>';
11273:                     } else {
11274:                          $output = '<p class="LC_error">'.
11275:                                    &mt('Error: update failed for: [_1].',
11276:                                    '<span class="LC_filename">'.
11277:                                    $container.'</span>').'</p>';
11278:                     }
11279:                     if ($context eq 'syllabus') {
11280:                         unless ($saveresult eq 'ok') {
11281:                             $skiprewrites = 1;
11282:                         }
11283:                     }
11284:                 } else {
11285:                     if (open(my $fh,">$container")) {
11286:                         print $fh $content;
11287:                         close($fh);
11288:                         $output = '<p>'.&mt('Updated [quant,_1,reference] in [_2].',
11289:                                   $count,'<span class="LC_filename">'.
11290:                                   $container.'</span>').'</p>';
11291:                     } else {
11292:                          $output = '<p class="LC_error">'.
11293:                                    &mt('Error: could not update [_1].',
11294:                                    '<span class="LC_filename">'.
11295:                                    $container.'</span>').'</p>';
11296:                     }
11297:                 }
11298:             }
11299:             if (($context eq 'syllabus') && (!$skiprewrites)) {
11300:                 my ($actionurl,$state);
11301:                 $actionurl = "/public/$udom/$uname/syllabus";
11302:                 my ($ignore,$num,$numpathchanges,$existing,$mapping) =
11303:                     &ask_for_embedded_content($actionurl,$state,\%allfiles,
11304:                                               \%codebase,
11305:                                               {'context' => 'rewrites',
11306:                                                'ignore_remote_references' => 1,});
11307:                 if (ref($mapping) eq 'HASH') {
11308:                     my $rewrites = 0;
11309:                     foreach my $key (keys(%{$mapping})) {
11310:                         next if ($key =~ m{^https?://});
11311:                         my $ref = $mapping->{$key};
11312:                         my $newname = "/uploaded/$udom/$uname/portfolio/syllabus/$key";
11313:                         my $attrib;
11314:                         if (ref($allfiles{$mapping->{$key}}) eq 'ARRAY') {
11315:                             $attrib = join('|',@{$allfiles{$mapping->{$key}}});
11316:                         }
11317:                         if ($content =~ m{($attrib\s*=\s*['"]?)\Q$ref\E(['"]?)}) {
11318:                             my $numchg = ($content =~ s{($attrib\s*=\s*['"]?)\Q$ref\E(['"]?)}{$1$newname$2}gi);
11319:                             $rewrites += $numchg;
11320:                         }
11321:                     }
11322:                     if ($rewrites) {
11323:                         my $saveresult; 
11324:                         my $url = &Apache::lonnet::store_edited_file($container,$content,$udom,$uname,\$saveresult);
11325:                         if ($url eq $container) {
11326:                             my ($fname) = ($container =~ m{/([^/]+)$});
11327:                             $output .= '<p>'.&mt('Rewrote [quant,_1,link] as [quant,_1,absolute link] in [_2].',
11328:                                             $count,'<span class="LC_filename">'.
11329:                                             $fname.'</span>').'</p>';
11330:                         } else {
11331:                             $output .= '<p class="LC_error">'.
11332:                                        &mt('Error: could not update links in [_1].',
11333:                                        '<span class="LC_filename">'.
11334:                                        $container.'</span>').'</p>';
11335: 
11336:                         }
11337:                     }
11338:                 }
11339:             }
11340:         } else {
11341:             &logthis('Failed to parse '.$container.
11342:                      ' to modify references: '.$parse_result);
11343:         }
11344:     }
11345:     if (wantarray) {
11346:         return ($output,$count,$codebasecount);
11347:     } else {
11348:         return $output;
11349:     }
11350: }
11351: 
11352: sub check_for_existing {
11353:     my ($path,$fname,$element) = @_;
11354:     my ($state,$msg);
11355:     if (-d $path.'/'.$fname) {
11356:         $state = 'exists';
11357:         $msg = &mt('Unable to upload [_1]. A directory by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
11358:     } elsif (-e $path.'/'.$fname) {
11359:         $state = 'exists';
11360:         $msg = &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
11361:     }
11362:     if ($state eq 'exists') {
11363:         $msg = '<span class="LC_error">'.$msg.'</span><br />';
11364:     }
11365:     return ($state,$msg);
11366: }
11367: 
11368: sub check_for_upload {
11369:     my ($path,$fname,$group,$element,$portfolio_root,$port_path,
11370:         $disk_quota,$current_disk_usage,$uname,$udom) = @_;
11371:     my $filesize = length($env{'form.'.$element});
11372:     if (!$filesize) {
11373:         my $msg = '<span class="LC_error">'.
11374:                   &mt('Unable to upload [_1]. (size = [_2] bytes)', 
11375:                       '<span class="LC_filename">'.$fname.'</span>',
11376:                       $filesize).'<br />'.
11377:                   &mt('Either the file you attempted to upload was empty, or your web browser was unable to read its contents.').'<br />'.
11378:                   '</span>';
11379:         return ('zero_bytes',$msg);
11380:     }
11381:     $filesize =  $filesize/1000; #express in k (1024?)
11382:     my $getpropath = 1;
11383:     my ($dirlistref,$listerror) =
11384:          &Apache::lonnet::dirlist($portfolio_root.$path,$udom,$uname,$getpropath);
11385:     my $found_file = 0;
11386:     my $locked_file = 0;
11387:     my @lockers;
11388:     my $navmap;
11389:     if ($env{'request.course.id'}) {
11390:         $navmap = Apache::lonnavmaps::navmap->new();
11391:     }
11392:     if (ref($dirlistref) eq 'ARRAY') {
11393:         foreach my $line (@{$dirlistref}) {
11394:             my ($file_name,$rest)=split(/\&/,$line,2);
11395:             if ($file_name eq $fname){
11396:                 $file_name = $path.$file_name;
11397:                 if ($group ne '') {
11398:                     $file_name = $group.$file_name;
11399:                 }
11400:                 $found_file = 1;
11401:                 if (&Apache::lonnet::is_locked($file_name,$udom,$uname,\@lockers) eq 'true') {
11402:                     foreach my $lock (@lockers) {
11403:                         if (ref($lock) eq 'ARRAY') {
11404:                             my ($symb,$crsid) = @{$lock};
11405:                             if ($crsid eq $env{'request.course.id'}) {
11406:                                 if (ref($navmap)) {
11407:                                     my $res = $navmap->getBySymb($symb);
11408:                                     foreach my $part (@{$res->parts()}) { 
11409:                                         my ($slot_status,$slot_time,$slot_name)=$res->check_for_slot($part);
11410:                                         unless (($slot_status == $res->RESERVED) ||
11411:                                                 ($slot_status == $res->RESERVED_LOCATION)) {
11412:                                             $locked_file = 1;
11413:                                         }
11414:                                     }
11415:                                 } else {
11416:                                     $locked_file = 1;
11417:                                 }
11418:                             } else {
11419:                                 $locked_file = 1;
11420:                             }
11421:                         }
11422:                    }
11423:                 } else {
11424:                     my @info = split(/\&/,$rest);
11425:                     my $currsize = $info[6]/1000;
11426:                     if ($currsize < $filesize) {
11427:                         my $extra = $filesize - $currsize;
11428:                         if (($current_disk_usage + $extra) > $disk_quota) {
11429:                             my $msg = '<p class="LC_warning">'.
11430:                                       &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.',
11431:                                           '<span class="LC_filename">'.$fname.'</span>',$filesize,$currsize).'</p>'.
11432:                                       '<p>'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',
11433:                                                    $disk_quota,$current_disk_usage).'</p>';
11434:                             return ('will_exceed_quota',$msg);
11435:                         }
11436:                     }
11437:                 }
11438:             }
11439:         }
11440:     }
11441:     if (($current_disk_usage + $filesize) > $disk_quota){
11442:         my $msg = '<p class="LC_warning">'.
11443:                 &mt('Unable to upload [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.','<span class="LC_filename">'.$fname.'</span>',$filesize).'</p>'.
11444:                   '<p>'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',$disk_quota,$current_disk_usage).'</p>';
11445:         return ('will_exceed_quota',$msg);
11446:     } elsif ($found_file) {
11447:         if ($locked_file) {
11448:             my $msg = '<p class="LC_warning">';
11449:             $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>');
11450:             $msg .= '</p>';
11451:             $msg .= &mt('You will be able to rename or delete existing [_1] after a grade has been assigned.','<span class="LC_filename">'.$fname.'</span>');
11452:             return ('file_locked',$msg);
11453:         } else {
11454:             my $msg = '<p class="LC_error">';
11455:             $msg .= &mt(' A file by that name: [_1] was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$port_path.$env{'form.currentpath'});
11456:             $msg .= '</p>';
11457:             return ('existingfile',$msg);
11458:         }
11459:     }
11460: }
11461: 
11462: sub check_for_traversal {
11463:     my ($path,$url,$toplevel) = @_;
11464:     my @parts=split(/\//,$path);
11465:     my $cleanpath;
11466:     my $fullpath = $url;
11467:     for (my $i=0;$i<@parts;$i++) {
11468:         next if ($parts[$i] eq '.');
11469:         if ($parts[$i] eq '..') {
11470:             $fullpath =~ s{([^/]+/)$}{};
11471:         } else {
11472:             $fullpath .= $parts[$i].'/';
11473:         }
11474:     }
11475:     if ($fullpath =~ /^\Q$url\E(.*)$/) {
11476:         $cleanpath = $1;
11477:     } elsif ($fullpath =~ /^\Q$toplevel\E(.*)$/) {
11478:         my $curr_toprel = $1;
11479:         my @parts = split(/\//,$curr_toprel);
11480:         my ($url_toprel) = ($url =~ /^\Q$toplevel\E(.*)$/);
11481:         my @urlparts = split(/\//,$url_toprel);
11482:         my $doubledots;
11483:         my $startdiff = -1;
11484:         for (my $i=0; $i<@urlparts; $i++) {
11485:             if ($startdiff == -1) {
11486:                 unless ($urlparts[$i] eq $parts[$i]) {
11487:                     $startdiff = $i;
11488:                     $doubledots .= '../';
11489:                 }
11490:             } else {
11491:                 $doubledots .= '../';
11492:             }
11493:         }
11494:         if ($startdiff > -1) {
11495:             $cleanpath = $doubledots;
11496:             for (my $i=$startdiff; $i<@parts; $i++) {
11497:                 $cleanpath .= $parts[$i].'/';
11498:             }
11499:         }
11500:     }
11501:     $cleanpath =~ s{(/)$}{};
11502:     return $cleanpath;
11503: }
11504: 
11505: sub is_archive_file {
11506:     my ($mimetype) = @_;
11507:     if (($mimetype eq 'application/octet-stream') ||
11508:         ($mimetype eq 'application/x-stuffit') ||
11509:         ($mimetype =~ m{^application/(x\-)?(compressed|tar|zip|tgz|gz|gtar|gzip|gunzip|bz|bz2|bzip2)})) {
11510:         return 1;
11511:     }
11512:     return;
11513: }
11514: 
11515: sub decompress_form {
11516:     my ($mimetype,$archiveurl,$action,$noextract,$hiddenelements,$dirlist) = @_;
11517:     my %lt = &Apache::lonlocal::texthash (
11518:         this => 'This file is an archive file.',
11519:         camt => 'This file is a Camtasia archive file.',
11520:         itsc => 'Its contents are as follows:',
11521:         youm => 'You may wish to extract its contents.',
11522:         extr => 'Extract contents',
11523:         auto => 'LON-CAPA can process the files automatically, or you can decide how each should be handled.',
11524:         proa => 'Process automatically?',
11525:         yes  => 'Yes',
11526:         no   => 'No',
11527:         fold => 'Title for folder containing movie',
11528:         movi => 'Title for page containing embedded movie', 
11529:     );
11530:     my $fileloc = &Apache::lonnet::filelocation(undef,$archiveurl);
11531:     my ($is_camtasia,$topdir,%toplevel,@paths);
11532:     my $info = &list_archive_contents($fileloc,\@paths);
11533:     if (@paths) {
11534:         foreach my $path (@paths) {
11535:             $path =~ s{^/}{};
11536:             if ($path =~ m{^([^/]+)/$}) {
11537:                 $topdir = $1;
11538:             }
11539:             if ($path =~ m{^([^/]+)/}) {
11540:                 $toplevel{$1} = $path;
11541:             } else {
11542:                 $toplevel{$path} = $path;
11543:             }
11544:         }
11545:     }
11546:     if ($mimetype =~ m{^application/(x\-)?(compressed|zip)}) {
11547:         my @camtasia6 = ("$topdir/","$topdir/index.html",
11548:                         "$topdir/media/",
11549:                         "$topdir/media/$topdir.mp4",
11550:                         "$topdir/media/FirstFrame.png",
11551:                         "$topdir/media/player.swf",
11552:                         "$topdir/media/swfobject.js",
11553:                         "$topdir/media/expressInstall.swf");
11554:         my @camtasia8_1 = ("$topdir/","$topdir/$topdir.html",
11555:                          "$topdir/$topdir.mp4",
11556:                          "$topdir/$topdir\_config.xml",
11557:                          "$topdir/$topdir\_controller.swf",
11558:                          "$topdir/$topdir\_embed.css",
11559:                          "$topdir/$topdir\_First_Frame.png",
11560:                          "$topdir/$topdir\_player.html",
11561:                          "$topdir/$topdir\_Thumbnails.png",
11562:                          "$topdir/playerProductInstall.swf",
11563:                          "$topdir/scripts/",
11564:                          "$topdir/scripts/config_xml.js",
11565:                          "$topdir/scripts/handlebars.js",
11566:                          "$topdir/scripts/jquery-1.7.1.min.js",
11567:                          "$topdir/scripts/jquery-ui-1.8.15.custom.min.js",
11568:                          "$topdir/scripts/modernizr.js",
11569:                          "$topdir/scripts/player-min.js",
11570:                          "$topdir/scripts/swfobject.js",
11571:                          "$topdir/skins/",
11572:                          "$topdir/skins/configuration_express.xml",
11573:                          "$topdir/skins/express_show/",
11574:                          "$topdir/skins/express_show/player-min.css",
11575:                          "$topdir/skins/express_show/spritesheet.png");
11576:         my @camtasia8_4 = ("$topdir/","$topdir/$topdir.html",
11577:                          "$topdir/$topdir.mp4",
11578:                          "$topdir/$topdir\_config.xml",
11579:                          "$topdir/$topdir\_controller.swf",
11580:                          "$topdir/$topdir\_embed.css",
11581:                          "$topdir/$topdir\_First_Frame.png",
11582:                          "$topdir/$topdir\_player.html",
11583:                          "$topdir/$topdir\_Thumbnails.png",
11584:                          "$topdir/playerProductInstall.swf",
11585:                          "$topdir/scripts/",
11586:                          "$topdir/scripts/config_xml.js",
11587:                          "$topdir/scripts/techsmith-smart-player.min.js",
11588:                          "$topdir/skins/",
11589:                          "$topdir/skins/configuration_express.xml",
11590:                          "$topdir/skins/express_show/",
11591:                          "$topdir/skins/express_show/spritesheet.min.css",
11592:                          "$topdir/skins/express_show/spritesheet.png",
11593:                          "$topdir/skins/express_show/techsmith-smart-player.min.css");
11594:         my @diffs = &compare_arrays(\@paths,\@camtasia6);
11595:         if (@diffs == 0) {
11596:             $is_camtasia = 6;
11597:         } else {
11598:             @diffs = &compare_arrays(\@paths,\@camtasia8_1);
11599:             if (@diffs == 0) {
11600:                 $is_camtasia = 8;
11601:             } else {
11602:                 @diffs = &compare_arrays(\@paths,\@camtasia8_4);
11603:                 if (@diffs == 0) {
11604:                     $is_camtasia = 8;
11605:                 }
11606:             }
11607:         }
11608:     }
11609:     my $output;
11610:     if ($is_camtasia) {
11611:         $output = <<"ENDCAM";
11612: <script type="text/javascript" language="Javascript">
11613: // <![CDATA[
11614: 
11615: function camtasiaToggle() {
11616:     for (var i=0; i<document.uploaded_decompress.autoextract_camtasia.length; i++) {
11617:         if (document.uploaded_decompress.autoextract_camtasia[i].checked) {
11618:             if (document.uploaded_decompress.autoextract_camtasia[i].value == $is_camtasia) {
11619:                 document.getElementById('camtasia_titles').style.display='block';
11620:             } else {
11621:                 document.getElementById('camtasia_titles').style.display='none';
11622:             }
11623:         }
11624:     }
11625:     return;
11626: }
11627: 
11628: // ]]>
11629: </script>
11630: <p>$lt{'camt'}</p>
11631: ENDCAM
11632:     } else {
11633:         $output = '<p>'.$lt{'this'};
11634:         if ($info eq '') {
11635:             $output .= ' '.$lt{'youm'}.'</p>'."\n";
11636:         } else {
11637:             $output .= ' '.$lt{'itsc'}.'</p>'."\n".
11638:                        '<div><pre>'.$info.'</pre></div>';
11639:         }
11640:     }
11641:     $output .= '<form name="uploaded_decompress" action="'.$action.'" method="post">'."\n";
11642:     my $duplicates;
11643:     my $num = 0;
11644:     if (ref($dirlist) eq 'ARRAY') {
11645:         foreach my $item (@{$dirlist}) {
11646:             if (ref($item) eq 'ARRAY') {
11647:                 if (exists($toplevel{$item->[0]})) {
11648:                     $duplicates .= 
11649:                         &start_data_table_row().
11650:                         '<td><label><input type="radio" name="archive_overwrite_'.$num.'" '.
11651:                         'value="0" checked="checked" />'.&mt('No').'</label>'.
11652:                         '&nbsp;<label><input type="radio" name="archive_overwrite_'.$num.'" '.
11653:                         'value="1" />'.&mt('Yes').'</label>'.
11654:                         '<input type="hidden" name="archive_overwrite_name_'.$num.'" value="'.$item->[0].'" /></td>'."\n".
11655:                         '<td>'.$item->[0].'</td>';
11656:                     if ($item->[2]) {
11657:                         $duplicates .= '<td>'.&mt('Directory').'</td>';
11658:                     } else {
11659:                         $duplicates .= '<td>'.&mt('File').'</td>';
11660:                     }
11661:                     $duplicates .= '<td>'.$item->[3].'</td>'.
11662:                                    '<td>'.
11663:                                    &Apache::lonlocal::locallocaltime($item->[4]).
11664:                                    '</td>'.
11665:                                    &end_data_table_row();
11666:                     $num ++;
11667:                 }
11668:             }
11669:         }
11670:     }
11671:     my $itemcount;
11672:     if (@paths > 0) {
11673:         $itemcount = scalar(@paths);
11674:     } else {
11675:         $itemcount = 1;
11676:     }
11677:     if ($is_camtasia) {
11678:         $output .= $lt{'auto'}.'<br />'.
11679:                    '<span class="LC_nobreak">'.$lt{'proa'}.'<label>'.
11680:                    '<input type="radio" name="autoextract_camtasia" value="'.$is_camtasia.'" onclick="javascript:camtasiaToggle();" checked="checked" />'.
11681:                    $lt{'yes'}.'</label>&nbsp;<label>'.
11682:                    '<input type="radio" name="autoextract_camtasia" value="0" onclick="javascript:camtasiaToggle();" />'.
11683:                    $lt{'no'}.'</label></span><br />'.
11684:                    '<div id="camtasia_titles" style="display:block">'.
11685:                    &Apache::lonhtmlcommon::start_pick_box().
11686:                    &Apache::lonhtmlcommon::row_title($lt{'fold'}).
11687:                    '<input type="textbox" name="camtasia_foldername" value="'.$env{'form.comment'}.'" />'."\n".
11688:                    &Apache::lonhtmlcommon::row_closure().
11689:                    &Apache::lonhtmlcommon::row_title($lt{'movi'}).
11690:                    '<input type="textbox" name="camtasia_moviename" value="" />'."\n".
11691:                    &Apache::lonhtmlcommon::row_closure(1).
11692:                    &Apache::lonhtmlcommon::end_pick_box().
11693:                    '</div>';
11694:     }
11695:     $output .= 
11696:         '<input type="hidden" name="archive_overwrite_total" value="'.$num.'" />'.
11697:         '<input type="hidden" name="archive_itemcount" value="'.$itemcount.'" />'.
11698:         "\n";
11699:     if ($duplicates ne '') {
11700:         $output .= '<p><span class="LC_warning">'.
11701:                    &mt('Warning: decompression of the archive will overwrite the following items which already exist:').'</span><br />'.  
11702:                    &start_data_table().
11703:                    &start_data_table_header_row().
11704:                    '<th>'.&mt('Overwrite?').'</th>'.
11705:                    '<th>'.&mt('Name').'</th>'.
11706:                    '<th>'.&mt('Type').'</th>'.
11707:                    '<th>'.&mt('Size').'</th>'.
11708:                    '<th>'.&mt('Last modified').'</th>'.
11709:                    &end_data_table_header_row().
11710:                    $duplicates.
11711:                    &end_data_table().
11712:                    '</p>';
11713:     }
11714:     $output .= '<input type="hidden" name="archiveurl" value="'.$archiveurl.'" />'."\n";
11715:     if (ref($hiddenelements) eq 'HASH') {
11716:         foreach my $hidden (sort(keys(%{$hiddenelements}))) {
11717:             $output .= '<input type="hidden" name="'.$hidden.'" value="'.$hiddenelements->{$hidden}.'" />'."\n";
11718:         }
11719:     }
11720:     $output .= <<"END";
11721: <br />
11722: <input type="submit" name="decompress" value="$lt{'extr'}" />
11723: </form>
11724: $noextract
11725: END
11726:     return $output;
11727: }
11728: 
11729: sub decompression_utility {
11730:     my ($program) = @_;
11731:     my @utilities = ('tar','gunzip','bunzip2','unzip'); 
11732:     my $location;
11733:     if (grep(/^\Q$program\E$/,@utilities)) { 
11734:         foreach my $dir ('/bin/','/usr/bin/','/usr/local/bin/','/sbin/',
11735:                          '/usr/sbin/') {
11736:             if (-x $dir.$program) {
11737:                 $location = $dir.$program;
11738:                 last;
11739:             }
11740:         }
11741:     }
11742:     return $location;
11743: }
11744: 
11745: sub list_archive_contents {
11746:     my ($file,$pathsref) = @_;
11747:     my (@cmd,$output);
11748:     my $needsregexp;
11749:     if ($file =~ /\.zip$/) {
11750:         @cmd = (&decompression_utility('unzip'),"-l");
11751:         $needsregexp = 1;
11752:     } elsif (($file =~ m/\.tar\.gz$/) ||
11753:              ($file =~ /\.tgz$/)) {
11754:         @cmd = (&decompression_utility('tar'),"-ztf");
11755:     } elsif ($file =~ /\.tar\.bz2$/) {
11756:         @cmd = (&decompression_utility('tar'),"-jtf");
11757:     } elsif ($file =~ m|\.tar$|) {
11758:         @cmd = (&decompression_utility('tar'),"-tf");
11759:     }
11760:     if (@cmd) {
11761:         undef($!);
11762:         undef($@);
11763:         if (open(my $fh,"-|", @cmd, $file)) {
11764:             while (my $line = <$fh>) {
11765:                 $output .= $line;
11766:                 chomp($line);
11767:                 my $item;
11768:                 if ($needsregexp) {
11769:                     ($item) = ($line =~ /^\s*\d+\s+[\d\-]+\s+[\d:]+\s*(.+)$/); 
11770:                 } else {
11771:                     $item = $line;
11772:                 }
11773:                 if ($item ne '') {
11774:                     unless (grep(/^\Q$item\E$/,@{$pathsref})) {
11775:                         push(@{$pathsref},$item);
11776:                     } 
11777:                 }
11778:             }
11779:             close($fh);
11780:         }
11781:     }
11782:     return $output;
11783: }
11784: 
11785: sub decompress_uploaded_file {
11786:     my ($file,$dir) = @_;
11787:     &Apache::lonnet::appenv({'cgi.file' => $file});
11788:     &Apache::lonnet::appenv({'cgi.dir' => $dir});
11789:     my $result = &Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
11790:     my ($handle) = ($env{'user.environment'} =~m{/([^/]+)\.id$});
11791:     my $lonidsdir = $Apache::lonnet::perlvar{'lonIDsDir'};
11792:     &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle,1);
11793:     my $decompressed = $env{'cgi.decompressed'};
11794:     &Apache::lonnet::delenv('cgi.file');
11795:     &Apache::lonnet::delenv('cgi.dir');
11796:     &Apache::lonnet::delenv('cgi.decompressed');
11797:     return ($decompressed,$result);
11798: }
11799: 
11800: sub process_decompression {
11801:     my ($docudom,$docuname,$file,$destination,$dir_root,$hiddenelem) = @_;
11802:     my ($dir,$error,$warning,$output);
11803:     if ($file !~ /\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/i) {
11804:         $error = &mt('Filename not a supported archive file type.').
11805:                  '<br />'.&mt('Filename should end with one of: [_1].',
11806:                               '.zip, .tar, .bz2, .gz, .tar.gz, .tar.bz2, .tgz');
11807:     } else {
11808:         my $docuhome = &Apache::lonnet::homeserver($docuname,$docudom);
11809:         if ($docuhome eq 'no_host') {
11810:             $error = &mt('Could not determine home server for course.');
11811:         } else {
11812:             my @ids=&Apache::lonnet::current_machine_ids();
11813:             my $currdir = "$dir_root/$destination";
11814:             if (grep(/^\Q$docuhome\E$/,@ids)) {
11815:                 $dir = &LONCAPA::propath($docudom,$docuname).
11816:                        "$dir_root/$destination";
11817:             } else {
11818:                 $dir = $Apache::lonnet::perlvar{'lonDocRoot'}.
11819:                        "$dir_root/$docudom/$docuname/$destination";
11820:                 unless (&Apache::lonnet::repcopy_userfile("$dir/$file") eq 'ok') {
11821:                     $error = &mt('Archive file not found.');
11822:                 }
11823:             }
11824:             my (@to_overwrite,@to_skip);
11825:             if ($env{'form.archive_overwrite_total'} > 0) {
11826:                 my $total = $env{'form.archive_overwrite_total'};
11827:                 for (my $i=0; $i<$total; $i++) {
11828:                     if ($env{'form.archive_overwrite_'.$i} == 1) {
11829:                         push(@to_overwrite,$env{'form.archive_overwrite_name_'.$i});
11830:                     } elsif ($env{'form.archive_overwrite_'.$i} == 0) {
11831:                         push(@to_skip,$env{'form.archive_overwrite_name_'.$i});
11832:                     }
11833:                 }
11834:             }
11835:             my $numskip = scalar(@to_skip);
11836:             if (($numskip > 0) && 
11837:                 ($numskip == $env{'form.archive_itemcount'})) {
11838:                 $warning = &mt('All items in the archive file already exist, and no overwriting of existing files has been requested.');         
11839:             } elsif ($dir eq '') {
11840:                 $error = &mt('Directory containing archive file unavailable.');
11841:             } elsif (!$error) {
11842:                 my ($decompressed,$display);
11843:                 if ($numskip > 0) {
11844:                     my $tempdir = time.'_'.$$.int(rand(10000));
11845:                     mkdir("$dir/$tempdir",0755);
11846:                     system("mv $dir/$file $dir/$tempdir/$file");
11847:                     ($decompressed,$display) = 
11848:                         &decompress_uploaded_file($file,"$dir/$tempdir");
11849:                     foreach my $item (@to_skip) {
11850:                         if (($item ne '') && ($item !~ /\.\./)) {
11851:                             if (-f "$dir/$tempdir/$item") { 
11852:                                 unlink("$dir/$tempdir/$item");
11853:                             } elsif (-d "$dir/$tempdir/$item") {
11854:                                 system("rm -rf $dir/$tempdir/$item");
11855:                             }
11856:                         }
11857:                     }
11858:                     system("mv $dir/$tempdir/* $dir");
11859:                     rmdir("$dir/$tempdir");   
11860:                 } else {
11861:                     ($decompressed,$display) = 
11862:                         &decompress_uploaded_file($file,$dir);
11863:                 }
11864:                 if ($decompressed eq 'ok') {
11865:                     $output = '<p class="LC_info">'.
11866:                               &mt('Files extracted successfully from archive.').
11867:                               '</p>'."\n";
11868:                     my ($warning,$result,@contents);
11869:                     my ($newdirlistref,$newlisterror) =
11870:                         &Apache::lonnet::dirlist($currdir,$docudom,
11871:                                                  $docuname,1);
11872:                     my (%is_dir,%changes,@newitems);
11873:                     my $dirptr = 16384;
11874:                     if (ref($newdirlistref) eq 'ARRAY') {
11875:                         foreach my $dir_line (@{$newdirlistref}) {
11876:                             my ($item,undef,undef,$testdir)=split(/\&/,$dir_line,5);
11877:                             unless (($item =~ /^\.+$/) || ($item eq $file) || 
11878:                                     ((@to_skip > 0) && (grep(/^\Q$item\E$/,@to_skip)))) {
11879:                                 push(@newitems,$item);
11880:                                 if ($dirptr&$testdir) {
11881:                                     $is_dir{$item} = 1;
11882:                                 }
11883:                                 $changes{$item} = 1;
11884:                             }
11885:                         }
11886:                     }
11887:                     if (keys(%changes) > 0) {
11888:                         foreach my $item (sort(@newitems)) {
11889:                             if ($changes{$item}) {
11890:                                 push(@contents,$item);
11891:                             }
11892:                         }
11893:                     }
11894:                     if (@contents > 0) {
11895:                         my $wantform;
11896:                         unless ($env{'form.autoextract_camtasia'}) {
11897:                             $wantform = 1;
11898:                         }
11899:                         my (%children,%parent,%dirorder,%titles);
11900:                         my ($count,$datatable) = &get_extracted($docudom,$docuname,
11901:                                                                 $currdir,\%is_dir,
11902:                                                                 \%children,\%parent,
11903:                                                                 \@contents,\%dirorder,
11904:                                                                 \%titles,$wantform);
11905:                         if ($datatable ne '') {
11906:                             $output .= &archive_options_form('decompressed',$datatable,
11907:                                                              $count,$hiddenelem);
11908:                             my $startcount = 6;
11909:                             $output .= &archive_javascript($startcount,$count,
11910:                                                            \%titles,\%children);
11911:                         }
11912:                         if ($env{'form.autoextract_camtasia'}) {
11913:                             my $version = $env{'form.autoextract_camtasia'};
11914:                             my %displayed;
11915:                             my $total = 1;
11916:                             $env{'form.archive_directory'} = [];
11917:                             foreach my $i (sort { $a <=> $b } keys(%dirorder)) {
11918:                                 my $path = join('/',map { $titles{$_}; } @{$dirorder{$i}});
11919:                                 $path =~ s{/$}{};
11920:                                 my $item;
11921:                                 if ($path ne '') {
11922:                                     $item = "$path/$titles{$i}";
11923:                                 } else {
11924:                                     $item = $titles{$i};
11925:                                 }
11926:                                 $env{'form.archive_content_'.$i} = "$dir_root/$destination/$item";
11927:                                 if ($item eq $contents[0]) {
11928:                                     push(@{$env{'form.archive_directory'}},$i);
11929:                                     $env{'form.archive_'.$i} = 'display';
11930:                                     $env{'form.archive_title_'.$i} = $env{'form.camtasia_foldername'};
11931:                                     $displayed{'folder'} = $i;
11932:                                 } elsif ((($item eq "$contents[0]/index.html") && ($version == 6)) ||
11933:                                          (($item eq "$contents[0]/$contents[0]".'.html') && ($version == 8))) { 
11934:                                     $env{'form.archive_'.$i} = 'display';
11935:                                     $env{'form.archive_title_'.$i} = $env{'form.camtasia_moviename'};
11936:                                     $displayed{'web'} = $i;
11937:                                 } else {
11938:                                     if ((($item eq "$contents[0]/media") && ($version == 6)) ||
11939:                                         ((($item eq "$contents[0]/scripts") || ($item eq "$contents[0]/skins") ||
11940:                                              ($item eq "$contents[0]/skins/express_show")) && ($version == 8))) {
11941:                                         push(@{$env{'form.archive_directory'}},$i);
11942:                                     }
11943:                                     $env{'form.archive_'.$i} = 'dependency';
11944:                                 }
11945:                                 $total ++;
11946:                             }
11947:                             for (my $i=1; $i<$total; $i++) {
11948:                                 next if ($i == $displayed{'web'});
11949:                                 next if ($i == $displayed{'folder'});
11950:                                 $env{'form.archive_dependent_on_'.$i} = $displayed{'web'};
11951:                             }
11952:                             $env{'form.phase'} = 'decompress_cleanup';
11953:                             $env{'form.archivedelete'} = 1;
11954:                             $env{'form.archive_count'} = $total-1;
11955:                             $output .=
11956:                                 &process_extracted_files('coursedocs',$docudom,
11957:                                                          $docuname,$destination,
11958:                                                          $dir_root,$hiddenelem);
11959:                         }
11960:                     } else {
11961:                         $warning = &mt('No new items extracted from archive file.');
11962:                     }
11963:                 } else {
11964:                     $output = $display;
11965:                     $error = &mt('An error occurred during extraction from the archive file.');
11966:                 }
11967:             }
11968:         }
11969:     }
11970:     if ($error) {
11971:         $output .= '<p class="LC_error">'.&mt('Not extracted.').'<br />'.
11972:                    $error.'</p>'."\n";
11973:     }
11974:     if ($warning) {
11975:         $output .= '<p class="LC_warning">'.$warning.'</p>'."\n";
11976:     }
11977:     return $output;
11978: }
11979: 
11980: sub get_extracted {
11981:     my ($docudom,$docuname,$currdir,$is_dir,$children,$parent,$contents,$dirorder,
11982:         $titles,$wantform) = @_;
11983:     my $count = 0;
11984:     my $depth = 0;
11985:     my $datatable;
11986:     my @hierarchy;
11987:     return unless ((ref($is_dir) eq 'HASH') && (ref($children) eq 'HASH') &&
11988:                    (ref($parent) eq 'HASH') && (ref($contents) eq 'ARRAY') &&
11989:                    (ref($dirorder) eq 'HASH') && (ref($titles) eq 'HASH'));
11990:     foreach my $item (@{$contents}) {
11991:         $count ++;
11992:         @{$dirorder->{$count}} = @hierarchy;
11993:         $titles->{$count} = $item;
11994:         &archive_hierarchy($depth,$count,$parent,$children);
11995:         if ($wantform) {
11996:             $datatable .= &archive_row($is_dir->{$item},$item,
11997:                                        $currdir,$depth,$count);
11998:         }
11999:         if ($is_dir->{$item}) {
12000:             $depth ++;
12001:             push(@hierarchy,$count);
12002:             $parent->{$depth} = $count;
12003:             $datatable .=
12004:                 &recurse_extracted_archive("$currdir/$item",$docudom,$docuname,
12005:                                            \$depth,\$count,\@hierarchy,$dirorder,
12006:                                            $children,$parent,$titles,$wantform);
12007:             $depth --;
12008:             pop(@hierarchy);
12009:         }
12010:     }
12011:     return ($count,$datatable);
12012: }
12013: 
12014: sub recurse_extracted_archive {
12015:     my ($currdir,$docudom,$docuname,$depth,$count,$hierarchy,$dirorder,
12016:         $children,$parent,$titles,$wantform) = @_;
12017:     my $result='';
12018:     unless ((ref($depth)) && (ref($count)) && (ref($hierarchy) eq 'ARRAY') &&
12019:             (ref($children) eq 'HASH') && (ref($parent) eq 'HASH') &&
12020:             (ref($dirorder) eq 'HASH')) {
12021:         return $result;
12022:     }
12023:     my $dirptr = 16384;
12024:     my ($newdirlistref,$newlisterror) =
12025:         &Apache::lonnet::dirlist($currdir,$docudom,$docuname,1);
12026:     if (ref($newdirlistref) eq 'ARRAY') {
12027:         foreach my $dir_line (@{$newdirlistref}) {
12028:             my ($item,undef,undef,$testdir)=split(/\&/,$dir_line,5);
12029:             unless ($item =~ /^\.+$/) {
12030:                 $$count ++;
12031:                 @{$dirorder->{$$count}} = @{$hierarchy};
12032:                 $titles->{$$count} = $item;
12033:                 &archive_hierarchy($$depth,$$count,$parent,$children);
12034: 
12035:                 my $is_dir;
12036:                 if ($dirptr&$testdir) {
12037:                     $is_dir = 1;
12038:                 }
12039:                 if ($wantform) {
12040:                     $result .= &archive_row($is_dir,$item,$currdir,$$depth,$$count);
12041:                 }
12042:                 if ($is_dir) {
12043:                     $$depth ++;
12044:                     push(@{$hierarchy},$$count);
12045:                     $parent->{$$depth} = $$count;
12046:                     $result .=
12047:                         &recurse_extracted_archive("$currdir/$item",$docudom,
12048:                                                    $docuname,$depth,$count,
12049:                                                    $hierarchy,$dirorder,$children,
12050:                                                    $parent,$titles,$wantform);
12051:                     $$depth --;
12052:                     pop(@{$hierarchy});
12053:                 }
12054:             }
12055:         }
12056:     }
12057:     return $result;
12058: }
12059: 
12060: sub archive_hierarchy {
12061:     my ($depth,$count,$parent,$children) =@_;
12062:     if ((ref($parent) eq 'HASH') && (ref($children) eq 'HASH')) {
12063:         if (exists($parent->{$depth})) {
12064:              $children->{$parent->{$depth}} .= $count.':';
12065:         }
12066:     }
12067:     return;
12068: }
12069: 
12070: sub archive_row {
12071:     my ($is_dir,$item,$currdir,$depth,$count) = @_;
12072:     my ($name) = ($item =~ m{([^/]+)$});
12073:     my %choices = &Apache::lonlocal::texthash (
12074:                                        'display'    => 'Add as file',
12075:                                        'dependency' => 'Include as dependency',
12076:                                        'discard'    => 'Discard',
12077:                                       );
12078:     if ($is_dir) {
12079:         $choices{'display'} = &mt('Add as folder'); 
12080:     }
12081:     my $output = &start_data_table_row().'<td align="right">'.$count.'</td>'."\n";
12082:     my $offset = 0;
12083:     foreach my $action ('display','dependency','discard') {
12084:         $offset ++;
12085:         if ($action ne 'display') {
12086:             $offset ++;
12087:         }  
12088:         $output .= '<td><span class="LC_nobreak">'.
12089:                    '<label><input type="radio" name="archive_'.$count.
12090:                    '" id="archive_'.$action.'_'.$count.'" value="'.$action.'"';
12091:         my $text = $choices{$action};
12092:         if ($is_dir) {
12093:             $output .= ' onclick="javascript:propagateCheck(this.form,'."'$count'".');"';
12094:             if ($action eq 'display') {
12095:                 $text = &mt('Add as folder');
12096:             }
12097:         } else {
12098:             $output .= ' onclick="javascript:dependencyCheck(this.form,'."$count,$offset".');"';
12099: 
12100:         }
12101:         $output .= ' />&nbsp;'.$choices{$action}.'</label></span>';
12102:         if ($action eq 'dependency') {
12103:             $output .= '<div id="arc_depon_'.$count.'" style="display:none;">'."\n".
12104:                        &mt('Used by:').'&nbsp;<select name="archive_dependent_on_'.$count.'" '.
12105:                        'onchange="propagateSelect(this.form,'."$count,$offset".')">'."\n".
12106:                        '<option value=""></option>'."\n".
12107:                        '</select>'."\n".
12108:                        '</div>';
12109:         } elsif ($action eq 'display') {
12110:             $output .= '<div id="arc_title_'.$count.'" style="display:none;">'."\n".
12111:                        &mt('Title:').'&nbsp;<input type="text" name="archive_title_'.$count.'" id="archive_title_'.$count.'" />'."\n".
12112:                        '</div>';
12113:         }
12114:         $output .= '</td>';
12115:     }
12116:     $output .= '<td><input type="hidden" name="archive_content_'.$count.'" value="'.
12117:                &HTML::Entities::encode("$currdir/$item",'"<>&').'" />'.('&nbsp;' x 2);
12118:     for (my $i=0; $i<$depth; $i++) {
12119:         $output .= ('<img src="/adm/lonIcons/whitespace1.gif" class="LC_docs_spacer" alt="" />' x2)."\n";
12120:     }
12121:     if ($is_dir) {
12122:         $output .= '<img src="/adm/lonIcons/navmap.folder.open.gif" alt="" />&nbsp;'."\n".
12123:                    '<input type="hidden" name="archive_directory" value="'.$count.'" />'."\n";
12124:     } else {
12125:         $output .= '<input type="hidden" name="archive_file" value="'.$count.'" />'."\n";
12126:     }
12127:     $output .= '&nbsp;'.$name.'</td>'."\n".
12128:                &end_data_table_row();
12129:     return $output;
12130: }
12131: 
12132: sub archive_options_form {
12133:     my ($form,$display,$count,$hiddenelem) = @_;
12134:     my %lt = &Apache::lonlocal::texthash(
12135:                perm => 'Permanently remove archive file?',
12136:                hows => 'How should each extracted item be incorporated in the course?',
12137:                cont => 'Content actions for all',
12138:                addf => 'Add as folder/file',
12139:                incd => 'Include as dependency for a displayed file',
12140:                disc => 'Discard',
12141:                no   => 'No',
12142:                yes  => 'Yes',
12143:                save => 'Save',
12144:     );
12145:     my $output = <<"END";
12146: <form name="$form" method="post" action="">
12147: <p><span class="LC_nobreak">$lt{'perm'}&nbsp;
12148: <label>
12149:   <input type="radio" name="archivedelete" value="0" checked="checked" />$lt{'no'}
12150: </label>
12151: &nbsp;
12152: <label>
12153:   <input type="radio" name="archivedelete" value="1" />$lt{'yes'}</label>
12154: </span>
12155: </p>
12156: <input type="hidden" name="phase" value="decompress_cleanup" />
12157: <br />$lt{'hows'}
12158: <div class="LC_columnSection">
12159:   <fieldset>
12160:     <legend>$lt{'cont'}</legend>
12161:     <input type="button" value="$lt{'addf'}" onclick="javascript:checkAll(document.$form,'display');" /> 
12162:     &nbsp;&nbsp;<input type="button" value="$lt{'incd'}" onclick="javascript:checkAll(document.$form,'dependency');" />
12163:     &nbsp;&nbsp;<input type="button" value="$lt{'disc'}" onclick="javascript:checkAll(document.$form,'discard');" />
12164:   </fieldset>
12165: </div>
12166: END
12167:     return $output.
12168:            &start_data_table()."\n".
12169:            $display."\n".
12170:            &end_data_table()."\n".
12171:            '<input type="hidden" name="archive_count" value="'.$count.'" />'.
12172:            $hiddenelem.
12173:            '<br /><input type="submit" name="archive_submit" value="'.$lt{'save'}.'" />'.
12174:            '</form>';
12175: }
12176: 
12177: sub archive_javascript {
12178:     my ($startcount,$numitems,$titles,$children) = @_;
12179:     return unless ((ref($titles) eq 'HASH') && (ref($children) eq 'HASH'));
12180:     my $maintitle = $env{'form.comment'};
12181:     my $scripttag = <<START;
12182: <script type="text/javascript">
12183: // <![CDATA[
12184: 
12185: function checkAll(form,prefix) {
12186:     var idstr =  new RegExp("^archive_"+prefix+"_\\\\d+\$");
12187:     for (var i=0; i < form.elements.length; i++) {
12188:         var id = form.elements[i].id;
12189:         if ((id != '') && (id != undefined)) {
12190:             if (idstr.test(id)) {
12191:                 if (form.elements[i].type == 'radio') {
12192:                     form.elements[i].checked = true;
12193:                     var nostart = i-$startcount;
12194:                     var offset = nostart%7;
12195:                     var count = (nostart-offset)/7;    
12196:                     dependencyCheck(form,count,offset);
12197:                 }
12198:             }
12199:         }
12200:     }
12201: }
12202: 
12203: function propagateCheck(form,count) {
12204:     if (count > 0) {
12205:         var startelement = $startcount + ((count-1) * 7);
12206:         for (var j=1; j<6; j++) {
12207:             if ((j != 2) && (j != 4)) {
12208:                 var item = startelement + j; 
12209:                 if (form.elements[item].type == 'radio') {
12210:                     if (form.elements[item].checked) {
12211:                         containerCheck(form,count,j);
12212:                         break;
12213:                     }
12214:                 }
12215:             }
12216:         }
12217:     }
12218: }
12219: 
12220: numitems = $numitems
12221: var titles = new Array(numitems);
12222: var parents = new Array(numitems);
12223: for (var i=0; i<numitems; i++) {
12224:     parents[i] = new Array;
12225: }
12226: var maintitle = '$maintitle';
12227: 
12228: START
12229: 
12230:     foreach my $container (sort { $a <=> $b } (keys(%{$children}))) {
12231:         my @contents = split(/:/,$children->{$container});
12232:         for (my $i=0; $i<@contents; $i ++) {
12233:             $scripttag .= 'parents['.$container.']['.$i.'] = '.$contents[$i]."\n";
12234:         }
12235:     }
12236: 
12237:     foreach my $key (sort { $a <=> $b } (keys(%{$titles}))) {
12238:         $scripttag .= "titles[$key] = '".$titles->{$key}."';\n";
12239:     }
12240: 
12241:     $scripttag .= <<END;
12242: 
12243: function containerCheck(form,count,offset) {
12244:     if (count > 0) {
12245:         dependencyCheck(form,count,offset);
12246:         var item = (offset+$startcount)+7*(count-1);
12247:         form.elements[item].checked = true;
12248:         if(Object.prototype.toString.call(parents[count]) === '[object Array]') {
12249:             if (parents[count].length > 0) {
12250:                 for (var j=0; j<parents[count].length; j++) {
12251:                     containerCheck(form,parents[count][j],offset);
12252:                 }
12253:             }
12254:         }
12255:     }
12256: }
12257: 
12258: function dependencyCheck(form,count,offset) {
12259:     if (count > 0) {
12260:         var chosen = (offset+$startcount)+7*(count-1);
12261:         var depitem = $startcount + ((count-1) * 7) + 4;
12262:         var currtype = form.elements[depitem].type;
12263:         if (form.elements[chosen].value == 'dependency') {
12264:             document.getElementById('arc_depon_'+count).style.display='block'; 
12265:             form.elements[depitem].options.length = 0;
12266:             form.elements[depitem].options[0] = new Option('Select','',true,true);
12267:             for (var i=1; i<=numitems; i++) {
12268:                 if (i == count) {
12269:                     continue;
12270:                 }
12271:                 var startelement = $startcount + (i-1) * 7;
12272:                 for (var j=1; j<6; j++) {
12273:                     if ((j != 2) && (j!= 4)) {
12274:                         var item = startelement + j;
12275:                         if (form.elements[item].type == 'radio') {
12276:                             if (form.elements[item].checked) {
12277:                                 if (form.elements[item].value == 'display') {
12278:                                     var n = form.elements[depitem].options.length;
12279:                                     form.elements[depitem].options[n] = new Option(titles[i],i,false,false);
12280:                                 }
12281:                             }
12282:                         }
12283:                     }
12284:                 }
12285:             }
12286:         } else {
12287:             document.getElementById('arc_depon_'+count).style.display='none';
12288:             form.elements[depitem].options.length = 0;
12289:             form.elements[depitem].options[0] = new Option('Select','',true,true);
12290:         }
12291:         titleCheck(form,count,offset);
12292:     }
12293: }
12294: 
12295: function propagateSelect(form,count,offset) {
12296:     if (count > 0) {
12297:         var item = (1+offset+$startcount)+7*(count-1);
12298:         var picked = form.elements[item].options[form.elements[item].selectedIndex].value; 
12299:         if (Object.prototype.toString.call(parents[count]) === '[object Array]') {
12300:             if (parents[count].length > 0) {
12301:                 for (var j=0; j<parents[count].length; j++) {
12302:                     containerSelect(form,parents[count][j],offset,picked);
12303:                 }
12304:             }
12305:         }
12306:     }
12307: }
12308: 
12309: function containerSelect(form,count,offset,picked) {
12310:     if (count > 0) {
12311:         var item = (offset+$startcount)+7*(count-1);
12312:         if (form.elements[item].type == 'radio') {
12313:             if (form.elements[item].value == 'dependency') {
12314:                 if (form.elements[item+1].type == 'select-one') {
12315:                     for (var i=0; i<form.elements[item+1].options.length; i++) {
12316:                         if (form.elements[item+1].options[i].value == picked) {
12317:                             form.elements[item+1].selectedIndex = i;
12318:                             break;
12319:                         }
12320:                     }
12321:                 }
12322:                 if (Object.prototype.toString.call(parents[count]) === '[object Array]') {
12323:                     if (parents[count].length > 0) {
12324:                         for (var j=0; j<parents[count].length; j++) {
12325:                             containerSelect(form,parents[count][j],offset,picked);
12326:                         }
12327:                     }
12328:                 }
12329:             }
12330:         }
12331:     }
12332: }
12333: 
12334: function titleCheck(form,count,offset) {
12335:     if (count > 0) {
12336:         var chosen = (offset+$startcount)+7*(count-1);
12337:         var depitem = $startcount + ((count-1) * 7) + 2;
12338:         var currtype = form.elements[depitem].type;
12339:         if (form.elements[chosen].value == 'display') {
12340:             document.getElementById('arc_title_'+count).style.display='block';
12341:             if ((count==1) && ((parents[count].length > 0) || (numitems == 1))) {
12342:                 document.getElementById('archive_title_'+count).value=maintitle;
12343:             }
12344:         } else {
12345:             document.getElementById('arc_title_'+count).style.display='none';
12346:             if (currtype == 'text') { 
12347:                 document.getElementById('archive_title_'+count).value='';
12348:             }
12349:         }
12350:     }
12351:     return;
12352: }
12353: 
12354: // ]]>
12355: </script>
12356: END
12357:     return $scripttag;
12358: }
12359: 
12360: sub process_extracted_files {
12361:     my ($context,$docudom,$docuname,$destination,$dir_root,$hiddenelem) = @_;
12362:     my $numitems = $env{'form.archive_count'};
12363:     return unless ($numitems);
12364:     my @ids=&Apache::lonnet::current_machine_ids();
12365:     my ($prefix,$pathtocheck,$dir,$ishome,$error,$warning,%toplevelitems,%is_dir,
12366:         %folders,%containers,%mapinner,%prompttofetch);
12367:     my $docuhome = &Apache::lonnet::homeserver($docuname,$docudom);
12368:     if (grep(/^\Q$docuhome\E$/,@ids)) {
12369:         $prefix = &LONCAPA::propath($docudom,$docuname);
12370:         $pathtocheck = "$dir_root/$destination";
12371:         $dir = $dir_root;
12372:         $ishome = 1;
12373:     } else {
12374:         $prefix = $Apache::lonnet::perlvar{'lonDocRoot'};
12375:         $pathtocheck = "$dir_root/$docudom/$docuname/$destination";
12376:         $dir = "$dir_root/$docudom/$docuname";    
12377:     }
12378:     my $currdir = "$dir_root/$destination";
12379:     (my $docstype,$mapinner{'0'}) = ($destination =~ m{^(docs|supplemental)/(\w+)/});
12380:     if ($env{'form.folderpath'}) {
12381:         my @items = split('&',$env{'form.folderpath'});
12382:         $folders{'0'} = $items[-2];
12383:         if ($env{'form.folderpath'} =~ /\:1$/) {
12384:             $containers{'0'}='page';
12385:         } else {  
12386:             $containers{'0'}='sequence';
12387:         }
12388:     }
12389:     my @archdirs = &get_env_multiple('form.archive_directory');
12390:     if ($numitems) {
12391:         for (my $i=1; $i<=$numitems; $i++) {
12392:             my $path = $env{'form.archive_content_'.$i};
12393:             if ($path =~ m{^\Q$pathtocheck\E/([^/]+)$}) {
12394:                 my $item = $1;
12395:                 $toplevelitems{$item} = $i;
12396:                 if (grep(/^\Q$i\E$/,@archdirs)) {
12397:                     $is_dir{$item} = 1;
12398:                 }
12399:             }
12400:         }
12401:     }
12402:     my ($output,%children,%parent,%titles,%dirorder,$result);
12403:     if (keys(%toplevelitems) > 0) {
12404:         my @contents = sort(keys(%toplevelitems));
12405:         (my $count,undef) = &get_extracted($docudom,$docuname,$currdir,\%is_dir,\%children,
12406:                                            \%parent,\@contents,\%dirorder,\%titles);
12407:     }
12408:     my (%referrer,%orphaned,%todelete,%todeletedir,%newdest,%newseqid);
12409:     if ($numitems) {
12410:         for (my $i=1; $i<=$numitems; $i++) {
12411:             next if ($env{'form.archive_'.$i} eq 'dependency');
12412:             my $path = $env{'form.archive_content_'.$i};
12413:             if ($path =~ /^\Q$pathtocheck\E/) {
12414:                 if ($env{'form.archive_'.$i} eq 'discard') {
12415:                     if ($prefix ne '' && $path ne '') {
12416:                         if (-e $prefix.$path) {
12417:                             if ((@archdirs > 0) && 
12418:                                 (grep(/^\Q$i\E$/,@archdirs))) {
12419:                                 $todeletedir{$prefix.$path} = 1;
12420:                             } else {
12421:                                 $todelete{$prefix.$path} = 1;
12422:                             }
12423:                         }
12424:                     }
12425:                 } elsif ($env{'form.archive_'.$i} eq 'display') {
12426:                     my ($docstitle,$title,$url,$outer);
12427:                     ($title) = ($path =~ m{/([^/]+)$});
12428:                     $docstitle = $env{'form.archive_title_'.$i};
12429:                     if ($docstitle eq '') {
12430:                         $docstitle = $title;
12431:                     }
12432:                     $outer = 0;
12433:                     if (ref($dirorder{$i}) eq 'ARRAY') {
12434:                         if (@{$dirorder{$i}} > 0) {
12435:                             foreach my $item (reverse(@{$dirorder{$i}})) {
12436:                                 if ($env{'form.archive_'.$item} eq 'display') {
12437:                                     $outer = $item;
12438:                                     last;
12439:                                 }
12440:                             }
12441:                         }
12442:                     }
12443:                     my ($errtext,$fatal) = 
12444:                         &LONCAPA::map::mapread('/uploaded/'.$docudom.'/'.$docuname.
12445:                                                '/'.$folders{$outer}.'.'.
12446:                                                $containers{$outer});
12447:                     next if ($fatal);
12448:                     if ((@archdirs > 0) && (grep(/^\Q$i\E$/,@archdirs))) {
12449:                         if ($context eq 'coursedocs') {
12450:                             $mapinner{$i} = time;
12451:                             $folders{$i} = 'default_'.$mapinner{$i};
12452:                             $containers{$i} = 'sequence';
12453:                             my $url = '/uploaded/'.$docudom.'/'.$docuname.'/'.
12454:                                       $folders{$i}.'.'.$containers{$i};
12455:                             my $newidx = &LONCAPA::map::getresidx();
12456:                             $LONCAPA::map::resources[$newidx]=
12457:                                 $docstitle.':'.$url.':false:normal:res';
12458:                             push(@LONCAPA::map::order,$newidx);
12459:                             my ($outtext,$errtext) =
12460:                                 &LONCAPA::map::storemap('/uploaded/'.$docudom.'/'.
12461:                                                         $docuname.'/'.$folders{$outer}.
12462:                                                         '.'.$containers{$outer},1,1);
12463:                             $newseqid{$i} = $newidx;
12464:                             unless ($errtext) {
12465:                                 $result .=  '<li>'.&mt('Folder: [_1] added to course',$docstitle).'</li>'."\n";
12466:                             }
12467:                         }
12468:                     } else {
12469:                         if ($context eq 'coursedocs') {
12470:                             my $newidx=&LONCAPA::map::getresidx();
12471:                             my $url = '/uploaded/'.$docudom.'/'.$docuname.'/'.
12472:                                       $docstype.'/'.$mapinner{$outer}.'/'.$newidx.'/'.
12473:                                       $title;
12474:                             if (!-e "$prefix$dir/$docstype/$mapinner{$outer}") {
12475:                                 mkdir("$prefix$dir/$docstype/$mapinner{$outer}",0755);
12476:                             }
12477:                             if (!-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx") {
12478:                                 mkdir("$prefix$dir/$docstype/$mapinner{$outer}/$newidx");
12479:                             }
12480:                             if (-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx") {
12481:                                 system("mv $prefix$path $prefix$dir/$docstype/$mapinner{$outer}/$newidx/$title");
12482:                                 $newdest{$i} = "$prefix$dir/$docstype/$mapinner{$outer}/$newidx";
12483:                                 unless ($ishome) {
12484:                                     my $fetch = "$newdest{$i}/$title";
12485:                                     $fetch =~ s/^\Q$prefix$dir\E//;
12486:                                     $prompttofetch{$fetch} = 1;
12487:                                 }
12488:                             }
12489:                             $LONCAPA::map::resources[$newidx]=
12490:                                 $docstitle.':'.$url.':false:normal:res';
12491:                             push(@LONCAPA::map::order, $newidx);
12492:                             my ($outtext,$errtext)=
12493:                                 &LONCAPA::map::storemap('/uploaded/'.$docudom.'/'.
12494:                                                         $docuname.'/'.$folders{$outer}.
12495:                                                         '.'.$containers{$outer},1,1);
12496:                             unless ($errtext) {
12497:                                 if (-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx/$title") {
12498:                                     $result .= '<li>'.&mt('File: [_1] added to course',$docstitle).'</li>'."\n";
12499:                                 }
12500:                             }
12501:                         }
12502:                     }
12503:                 }
12504:             } else {
12505:                 $warning .= &mt('Item extracted from archive: [_1] has unexpected path.',$path).'<br />'; 
12506:             }
12507:         }
12508:         for (my $i=1; $i<=$numitems; $i++) {
12509:             next unless ($env{'form.archive_'.$i} eq 'dependency');
12510:             my $path = $env{'form.archive_content_'.$i};
12511:             if ($path =~ /^\Q$pathtocheck\E/) {
12512:                 my ($title) = ($path =~ m{/([^/]+)$});
12513:                 $referrer{$i} = $env{'form.archive_dependent_on_'.$i};
12514:                 if ($env{'form.archive_'.$referrer{$i}} eq 'display') {
12515:                     if (ref($dirorder{$i}) eq 'ARRAY') {
12516:                         my ($itemidx,$fullpath,$relpath);
12517:                         if (ref($dirorder{$referrer{$i}}) eq 'ARRAY') {
12518:                             my $container = $dirorder{$referrer{$i}}->[-1];
12519:                             for (my $j=0; $j<@{$dirorder{$i}}; $j++) {
12520:                                 if ($dirorder{$i}->[$j] eq $container) {
12521:                                     $itemidx = $j;
12522:                                 }
12523:                             }
12524:                         }
12525:                         if ($itemidx eq '') {
12526:                             $itemidx =  0;
12527:                         } 
12528:                         if (grep(/^\Q$referrer{$i}\E$/,@archdirs)) {
12529:                             if ($mapinner{$referrer{$i}}) {
12530:                                 $fullpath = "$prefix$dir/$docstype/$mapinner{$referrer{$i}}";
12531:                                 for (my $j=$itemidx; $j<@{$dirorder{$i}}; $j++) {
12532:                                     if (grep(/^\Q$dirorder{$i}->[$j]\E$/,@archdirs)) {
12533:                                         unless (defined($newseqid{$dirorder{$i}->[$j]})) {
12534:                                             $fullpath .= '/'.$titles{$dirorder{$i}->[$j]};
12535:                                             $relpath .= '/'.$titles{$dirorder{$i}->[$j]};
12536:                                             if (!-e $fullpath) {
12537:                                                 mkdir($fullpath,0755);
12538:                                             }
12539:                                         }
12540:                                     } else {
12541:                                         last;
12542:                                     }
12543:                                 }
12544:                             }
12545:                         } elsif ($newdest{$referrer{$i}}) {
12546:                             $fullpath = $newdest{$referrer{$i}};
12547:                             for (my $j=$itemidx; $j<@{$dirorder{$i}}; $j++) {
12548:                                 if ($env{'form.archive_'.$dirorder{$i}->[$j]} eq 'discard') {
12549:                                     $orphaned{$i} = $env{'form.archive_'.$dirorder{$i}->[$j]};
12550:                                     last;
12551:                                 } elsif (grep(/^\Q$dirorder{$i}->[$j]\E$/,@archdirs)) {
12552:                                     unless (defined($newseqid{$dirorder{$i}->[$j]})) {
12553:                                         $fullpath .= '/'.$titles{$dirorder{$i}->[$j]};
12554:                                         $relpath .= '/'.$titles{$dirorder{$i}->[$j]};
12555:                                         if (!-e $fullpath) {
12556:                                             mkdir($fullpath,0755);
12557:                                         }
12558:                                     }
12559:                                 } else {
12560:                                     last;
12561:                                 }
12562:                             }
12563:                         }
12564:                         if ($fullpath ne '') {
12565:                             if (-e "$prefix$path") {
12566:                                 system("mv $prefix$path $fullpath/$title");
12567:                             }
12568:                             if (-e "$fullpath/$title") {
12569:                                 my $showpath;
12570:                                 if ($relpath ne '') {
12571:                                     $showpath = "$relpath/$title";
12572:                                 } else {
12573:                                     $showpath = "/$title";
12574:                                 } 
12575:                                 $result .= '<li>'.&mt('[_1] included as a dependency',$showpath).'</li>'."\n";
12576:                             } 
12577:                             unless ($ishome) {
12578:                                 my $fetch = "$fullpath/$title";
12579:                                 $fetch =~ s/^\Q$prefix$dir\E//; 
12580:                                 $prompttofetch{$fetch} = 1;
12581:                             }
12582:                         }
12583:                     }
12584:                 } elsif ($env{'form.archive_'.$referrer{$i}} eq 'discard') {
12585:                     $warning .= &mt('[_1] is a dependency of [_2], which was discarded.',
12586:                                     $path,$env{'form.archive_content_'.$referrer{$i}}).'<br />';
12587:                 }
12588:             } else {
12589:                 $warning .= &mt('Item extracted from archive: [_1] has unexpected path.',$path).'<br />'; 
12590:             }
12591:         }
12592:         if (keys(%todelete)) {
12593:             foreach my $key (keys(%todelete)) {
12594:                 unlink($key);
12595:             }
12596:         }
12597:         if (keys(%todeletedir)) {
12598:             foreach my $key (keys(%todeletedir)) {
12599:                 rmdir($key);
12600:             }
12601:         }
12602:         foreach my $dir (sort(keys(%is_dir))) {
12603:             if (($pathtocheck ne '') && ($dir ne ''))  {
12604:                 &cleanup_empty_dirs($prefix."$pathtocheck/$dir");
12605:             }
12606:         }
12607:         if ($result ne '') {
12608:             $output .= '<ul>'."\n".
12609:                        $result."\n".
12610:                        '</ul>';
12611:         }
12612:         unless ($ishome) {
12613:             my $replicationfail;
12614:             foreach my $item (keys(%prompttofetch)) {
12615:                 my $fetchresult= &Apache::lonnet::reply('fetchuserfile:'.$item,$docuhome);
12616:                 unless ($fetchresult eq 'ok') {
12617:                     $replicationfail .= '<li>'.$item.'</li>'."\n";
12618:                 }
12619:             }
12620:             if ($replicationfail) {
12621:                 $output .= '<p class="LC_error">'.
12622:                            &mt('Course home server failed to retrieve:').'<ul>'.
12623:                            $replicationfail.
12624:                            '</ul></p>';
12625:             }
12626:         }
12627:     } else {
12628:         $warning = &mt('No items found in archive.');
12629:     }
12630:     if ($error) {
12631:         $output .= '<p class="LC_error">'.&mt('Not extracted.').'<br />'.
12632:                    $error.'</p>'."\n";
12633:     }
12634:     if ($warning) {
12635:         $output .= '<p class="LC_warning">'.$warning.'</p>'."\n";
12636:     }
12637:     return $output;
12638: }
12639: 
12640: sub cleanup_empty_dirs {
12641:     my ($path) = @_;
12642:     if (($path ne '') && (-d $path)) {
12643:         if (opendir(my $dirh,$path)) {
12644:             my @dircontents = grep(!/^\./,readdir($dirh));
12645:             my $numitems = 0;
12646:             foreach my $item (@dircontents) {
12647:                 if (-d "$path/$item") {
12648:                     &cleanup_empty_dirs("$path/$item");
12649:                     if (-e "$path/$item") {
12650:                         $numitems ++;
12651:                     }
12652:                 } else {
12653:                     $numitems ++;
12654:                 }
12655:             }
12656:             if ($numitems == 0) {
12657:                 rmdir($path);
12658:             }
12659:             closedir($dirh);
12660:         }
12661:     }
12662:     return;
12663: }
12664: 
12665: =pod
12666: 
12667: =item * &get_folder_hierarchy()
12668: 
12669: Provides hierarchy of names of folders/sub-folders containing the current
12670: item,
12671: 
12672: Inputs: 3
12673:      - $navmap - navmaps object
12674: 
12675:      - $map - url for map (either the trigger itself, or map containing
12676:                            the resource, which is the trigger).
12677: 
12678:      - $showitem - 1 => show title for map itself; 0 => do not show.
12679: 
12680: Outputs: 1 @pathitems - array of folder/subfolder names.
12681: 
12682: =cut
12683: 
12684: sub get_folder_hierarchy {
12685:     my ($navmap,$map,$showitem) = @_;
12686:     my @pathitems;
12687:     if (ref($navmap)) {
12688:         my $mapres = $navmap->getResourceByUrl($map);
12689:         if (ref($mapres)) {
12690:             my $pcslist = $mapres->map_hierarchy();
12691:             if ($pcslist ne '') {
12692:                 my @pcs = split(/,/,$pcslist);
12693:                 foreach my $pc (@pcs) {
12694:                     if ($pc == 1) {
12695:                         push(@pathitems,&mt('Main Content'));
12696:                     } else {
12697:                         my $res = $navmap->getByMapPc($pc);
12698:                         if (ref($res)) {
12699:                             my $title = $res->compTitle();
12700:                             $title =~ s/\W+/_/g;
12701:                             if ($title ne '') {
12702:                                 push(@pathitems,$title);
12703:                             }
12704:                         }
12705:                     }
12706:                 }
12707:             }
12708:             if ($showitem) {
12709:                 if ($mapres->{ID} eq '0.0') {
12710:                     push(@pathitems,&mt('Main Content'));
12711:                 } else {
12712:                     my $maptitle = $mapres->compTitle();
12713:                     $maptitle =~ s/\W+/_/g;
12714:                     if ($maptitle ne '') {
12715:                         push(@pathitems,$maptitle);
12716:                     }
12717:                 }
12718:             }
12719:         }
12720:     }
12721:     return @pathitems;
12722: }
12723: 
12724: =pod
12725: 
12726: =item * &get_turnedin_filepath()
12727: 
12728: Determines path in a user's portfolio file for storage of files uploaded
12729: to a specific essayresponse or dropbox item.
12730: 
12731: Inputs: 3 required + 1 optional.
12732: $symb is symb for resource, $uname and $udom are for current user (required).
12733: $caller is optional (can be "submission", if routine is called when storing
12734: an upoaded file when "Submit Answer" button was pressed).
12735: 
12736: Returns array containing $path and $multiresp. 
12737: $path is path in portfolio.  $multiresp is 1 if this resource contains more
12738: than one file upload item.  Callers of routine should append partid as a 
12739: subdirectory to $path in cases where $multiresp is 1.
12740: 
12741: Called by: homework/essayresponse.pm and homework/structuretags.pm
12742: 
12743: =cut
12744: 
12745: sub get_turnedin_filepath {
12746:     my ($symb,$uname,$udom,$caller) = @_;
12747:     my ($map,$resid,$resurl)=&Apache::lonnet::decode_symb($symb);
12748:     my $turnindir;
12749:     my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'turnindir');
12750:     $turnindir = $userhash{'turnindir'};
12751:     my ($path,$multiresp);
12752:     if ($turnindir eq '') {
12753:         if ($caller eq 'submission') {
12754:             $turnindir = &mt('turned in');
12755:             $turnindir =~ s/\W+/_/g;
12756:             my %newhash = (
12757:                             'turnindir' => $turnindir,
12758:                           );
12759:             &Apache::lonnet::put('environment',\%newhash,$udom,$uname);
12760:         }
12761:     }
12762:     if ($turnindir ne '') {
12763:         $path = '/'.$turnindir.'/';
12764:         my ($multipart,$turnin,@pathitems);
12765:         my $navmap = Apache::lonnavmaps::navmap->new();
12766:         if (defined($navmap)) {
12767:             my $mapres = $navmap->getResourceByUrl($map);
12768:             if (ref($mapres)) {
12769:                 my $pcslist = $mapres->map_hierarchy();
12770:                 if ($pcslist ne '') {
12771:                     foreach my $pc (split(/,/,$pcslist)) {
12772:                         my $res = $navmap->getByMapPc($pc);
12773:                         if (ref($res)) {
12774:                             my $title = $res->compTitle();
12775:                             $title =~ s/\W+/_/g;
12776:                             if ($title ne '') {
12777:                                 if (($pc > 1) && (length($title) > 12)) {
12778:                                     $title = substr($title,0,12);
12779:                                 }
12780:                                 push(@pathitems,$title);
12781:                             }
12782:                         }
12783:                     }
12784:                 }
12785:                 my $maptitle = $mapres->compTitle();
12786:                 $maptitle =~ s/\W+/_/g;
12787:                 if ($maptitle ne '') {
12788:                     if (length($maptitle) > 12) {
12789:                         $maptitle = substr($maptitle,0,12);
12790:                     }
12791:                     push(@pathitems,$maptitle);
12792:                 }
12793:                 unless ($env{'request.state'} eq 'construct') {
12794:                     my $res = $navmap->getBySymb($symb);
12795:                     if (ref($res)) {
12796:                         my $partlist = $res->parts();
12797:                         my $totaluploads = 0;
12798:                         if (ref($partlist) eq 'ARRAY') {
12799:                             foreach my $part (@{$partlist}) {
12800:                                 my @types = $res->responseType($part);
12801:                                 my @ids = $res->responseIds($part);
12802:                                 for (my $i=0; $i < scalar(@ids); $i++) {
12803:                                     if ($types[$i] eq 'essay') {
12804:                                         my $partid = $part.'_'.$ids[$i];
12805:                                         if (&Apache::lonnet::EXT("resource.$partid.uploadedfiletypes") ne '') {
12806:                                             $totaluploads ++;
12807:                                         }
12808:                                     }
12809:                                 }
12810:                             }
12811:                             if ($totaluploads > 1) {
12812:                                 $multiresp = 1;
12813:                             }
12814:                         }
12815:                     }
12816:                 }
12817:             } else {
12818:                 return;
12819:             }
12820:         } else {
12821:             return;
12822:         }
12823:         my $restitle=&Apache::lonnet::gettitle($symb);
12824:         $restitle =~ s/\W+/_/g;
12825:         if ($restitle eq '') {
12826:             $restitle = ($resurl =~ m{/[^/]+$});
12827:             if ($restitle eq '') {
12828:                 $restitle = time;
12829:             }
12830:         }
12831:         if (length($restitle) > 12) {
12832:             $restitle = substr($restitle,0,12);
12833:         }
12834:         push(@pathitems,$restitle);
12835:         $path .= join('/',@pathitems);
12836:     }
12837:     return ($path,$multiresp);
12838: }
12839: 
12840: =pod
12841: 
12842: =back
12843: 
12844: =head1 CSV Upload/Handling functions
12845: 
12846: =over 4
12847: 
12848: =item * &upfile_store($r)
12849: 
12850: Store uploaded file, $r should be the HTTP Request object,
12851: needs $env{'form.upfile'}
12852: returns $datatoken to be put into hidden field
12853: 
12854: =cut
12855: 
12856: sub upfile_store {
12857:     my $r=shift;
12858:     $env{'form.upfile'}=~s/\r/\n/gs;
12859:     $env{'form.upfile'}=~s/\f/\n/gs;
12860:     $env{'form.upfile'}=~s/\n+/\n/gs;
12861:     $env{'form.upfile'}=~s/\n+$//gs;
12862: 
12863:     my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
12864: 	'_enroll_'.$env{'request.course.id'}.'_'.time.'_'.$$;
12865:     {
12866:         my $datafile = $r->dir_config('lonDaemons').
12867:                            '/tmp/'.$datatoken.'.tmp';
12868:         if ( open(my $fh,">$datafile") ) {
12869:             print $fh $env{'form.upfile'};
12870:             close($fh);
12871:         }
12872:     }
12873:     return $datatoken;
12874: }
12875: 
12876: =pod
12877: 
12878: =item * &load_tmp_file($r)
12879: 
12880: Load uploaded file from tmp, $r should be the HTTP Request object,
12881: needs $env{'form.datatoken'},
12882: sets $env{'form.upfile'} to the contents of the file
12883: 
12884: =cut
12885: 
12886: sub load_tmp_file {
12887:     my $r=shift;
12888:     my @studentdata=();
12889:     {
12890:         my $studentfile = $r->dir_config('lonDaemons').
12891:                               '/tmp/'.$env{'form.datatoken'}.'.tmp';
12892:         if ( open(my $fh,"<$studentfile") ) {
12893:             @studentdata=<$fh>;
12894:             close($fh);
12895:         }
12896:     }
12897:     $env{'form.upfile'}=join('',@studentdata);
12898: }
12899: 
12900: =pod
12901: 
12902: =item * &upfile_record_sep()
12903: 
12904: Separate uploaded file into records
12905: returns array of records,
12906: needs $env{'form.upfile'} and $env{'form.upfiletype'}
12907: 
12908: =cut
12909: 
12910: sub upfile_record_sep {
12911:     if ($env{'form.upfiletype'} eq 'xml') {
12912:     } else {
12913: 	my @records;
12914: 	foreach my $line (split(/\n/,$env{'form.upfile'})) {
12915: 	    if ($line=~/^\s*$/) { next; }
12916: 	    push(@records,$line);
12917: 	}
12918: 	return @records;
12919:     }
12920: }
12921: 
12922: =pod
12923: 
12924: =item * &record_sep($record)
12925: 
12926: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $env{'form.upfiletype'}
12927: 
12928: =cut
12929: 
12930: sub takeleft {
12931:     my $index=shift;
12932:     return substr('0000'.$index,-4,4);
12933: }
12934: 
12935: sub record_sep {
12936:     my $record=shift;
12937:     my %components=();
12938:     if ($env{'form.upfiletype'} eq 'xml') {
12939:     } elsif ($env{'form.upfiletype'} eq 'space') {
12940:         my $i=0;
12941:         foreach my $field (split(/\s+/,$record)) {
12942:             $field=~s/^(\"|\')//;
12943:             $field=~s/(\"|\')$//;
12944:             $components{&takeleft($i)}=$field;
12945:             $i++;
12946:         }
12947:     } elsif ($env{'form.upfiletype'} eq 'tab') {
12948:         my $i=0;
12949:         foreach my $field (split(/\t/,$record)) {
12950:             $field=~s/^(\"|\')//;
12951:             $field=~s/(\"|\')$//;
12952:             $components{&takeleft($i)}=$field;
12953:             $i++;
12954:         }
12955:     } else {
12956:         my $separator=',';
12957:         if ($env{'form.upfiletype'} eq 'semisv') {
12958:             $separator=';';
12959:         }
12960:         my $i=0;
12961: # the character we are looking for to indicate the end of a quote or a record 
12962:         my $looking_for=$separator;
12963: # do not add the characters to the fields
12964:         my $ignore=0;
12965: # we just encountered a separator (or the beginning of the record)
12966:         my $just_found_separator=1;
12967: # store the field we are working on here
12968:         my $field='';
12969: # work our way through all characters in record
12970:         foreach my $character ($record=~/(.)/g) {
12971:             if ($character eq $looking_for) {
12972:                if ($character ne $separator) {
12973: # Found the end of a quote, again looking for separator
12974:                   $looking_for=$separator;
12975:                   $ignore=1;
12976:                } else {
12977: # Found a separator, store away what we got
12978:                   $components{&takeleft($i)}=$field;
12979: 	          $i++;
12980:                   $just_found_separator=1;
12981:                   $ignore=0;
12982:                   $field='';
12983:                }
12984:                next;
12985:             }
12986: # single or double quotation marks after a separator indicate beginning of a quote
12987: # we are now looking for the end of the quote and need to ignore separators
12988:             if ((($character eq '"') || ($character eq "'")) && ($just_found_separator))  {
12989:                $looking_for=$character;
12990:                next;
12991:             }
12992: # ignore would be true after we reached the end of a quote
12993:             if ($ignore) { next; }
12994:             if (($just_found_separator) && ($character=~/\s/)) { next; }
12995:             $field.=$character;
12996:             $just_found_separator=0; 
12997:         }
12998: # catch the very last entry, since we never encountered the separator
12999:         $components{&takeleft($i)}=$field;
13000:     }
13001:     return %components;
13002: }
13003: 
13004: ######################################################
13005: ######################################################
13006: 
13007: =pod
13008: 
13009: =item * &upfile_select_html()
13010: 
13011: Return HTML code to select a file from the users machine and specify 
13012: the file type.
13013: 
13014: =cut
13015: 
13016: ######################################################
13017: ######################################################
13018: sub upfile_select_html {
13019:     my %Types = (
13020:                  csv   => &mt('CSV (comma separated values, spreadsheet)'),
13021:                  semisv => &mt('Semicolon separated values'),
13022:                  space => &mt('Space separated'),
13023:                  tab   => &mt('Tabulator separated'),
13024: #                 xml   => &mt('HTML/XML'),
13025:                  );
13026:     my $Str = '<input type="file" name="upfile" size="50" />'.
13027:         '<br />'.&mt('Type').': <select name="upfiletype">';
13028:     foreach my $type (sort(keys(%Types))) {
13029:         $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
13030:     }
13031:     $Str .= "</select>\n";
13032:     return $Str;
13033: }
13034: 
13035: sub get_samples {
13036:     my ($records,$toget) = @_;
13037:     my @samples=({});
13038:     my $got=0;
13039:     foreach my $rec (@$records) {
13040: 	my %temp = &record_sep($rec);
13041: 	if (! grep(/\S/, values(%temp))) { next; }
13042: 	if (%temp) {
13043: 	    $samples[$got]=\%temp;
13044: 	    $got++;
13045: 	    if ($got == $toget) { last; }
13046: 	}
13047:     }
13048:     return \@samples;
13049: }
13050: 
13051: ######################################################
13052: ######################################################
13053: 
13054: =pod
13055: 
13056: =item * &csv_print_samples($r,$records)
13057: 
13058: Prints a table of sample values from each column uploaded $r is an
13059: Apache Request ref, $records is an arrayref from
13060: &Apache::loncommon::upfile_record_sep
13061: 
13062: =cut
13063: 
13064: ######################################################
13065: ######################################################
13066: sub csv_print_samples {
13067:     my ($r,$records) = @_;
13068:     my $samples = &get_samples($records,5);
13069: 
13070:     $r->print(&mt('Samples').'<br />'.&start_data_table().
13071:               &start_data_table_header_row());
13072:     foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) { 
13073:         $r->print('<th>'.&mt('Column [_1]',($sample+1)).'</th>'); }
13074:     $r->print(&end_data_table_header_row());
13075:     foreach my $hash (@$samples) {
13076: 	$r->print(&start_data_table_row());
13077: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
13078: 	    $r->print('<td>');
13079: 	    if (defined($$hash{$sample})) { $r->print($$hash{$sample}); }
13080: 	    $r->print('</td>');
13081: 	}
13082: 	$r->print(&end_data_table_row());
13083:     }
13084:     $r->print(&end_data_table().'<br />'."\n");
13085: }
13086: 
13087: ######################################################
13088: ######################################################
13089: 
13090: =pod
13091: 
13092: =item * &csv_print_select_table($r,$records,$d)
13093: 
13094: Prints a table to create associations between values and table columns.
13095: 
13096: $r is an Apache Request ref,
13097: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
13098: $d is an array of 2 element arrays (internal name, displayed name,defaultcol)
13099: 
13100: =cut
13101: 
13102: ######################################################
13103: ######################################################
13104: sub csv_print_select_table {
13105:     my ($r,$records,$d) = @_;
13106:     my $i=0;
13107:     my $samples = &get_samples($records,1);
13108:     $r->print(&mt('Associate columns with student attributes.')."\n".
13109: 	      &start_data_table().&start_data_table_header_row().
13110:               '<th>'.&mt('Attribute').'</th>'.
13111:               '<th>'.&mt('Column').'</th>'.
13112:               &end_data_table_header_row()."\n");
13113:     foreach my $array_ref (@$d) {
13114: 	my ($value,$display,$defaultcol)=@{ $array_ref };
13115: 	$r->print(&start_data_table_row().'<td>'.$display.'</td>');
13116: 
13117: 	$r->print('<td><select name="f'.$i.'"'.
13118: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
13119: 	$r->print('<option value="none"></option>');
13120: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
13121: 	    $r->print('<option value="'.$sample.'"'.
13122:                       ($sample eq $defaultcol ? ' selected="selected" ' : '').
13123:                       '>'.&mt('Column [_1]',($sample+1)).'</option>');
13124: 	}
13125: 	$r->print('</select></td>'.&end_data_table_row()."\n");
13126: 	$i++;
13127:     }
13128:     $r->print(&end_data_table());
13129:     $i--;
13130:     return $i;
13131: }
13132: 
13133: ######################################################
13134: ######################################################
13135: 
13136: =pod
13137: 
13138: =item * &csv_samples_select_table($r,$records,$d)
13139: 
13140: Prints a table of sample values from the upload and can make associate samples to internal names.
13141: 
13142: $r is an Apache Request ref,
13143: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
13144: $d is an array of 2 element arrays (internal name, displayed name)
13145: 
13146: =cut
13147: 
13148: ######################################################
13149: ######################################################
13150: sub csv_samples_select_table {
13151:     my ($r,$records,$d) = @_;
13152:     my $i=0;
13153:     #
13154:     my $max_samples = 5;
13155:     my $samples = &get_samples($records,$max_samples);
13156:     $r->print(&start_data_table().
13157:               &start_data_table_header_row().'<th>'.
13158:               &mt('Field').'</th><th>'.&mt('Samples').'</th>'.
13159:               &end_data_table_header_row());
13160: 
13161:     foreach my $key (sort(keys(%{ $samples->[0] }))) {
13162: 	$r->print(&start_data_table_row().'<td><select name="f'.$i.'"'.
13163: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
13164: 	foreach my $option (@$d) {
13165: 	    my ($value,$display,$defaultcol)=@{ $option };
13166: 	    $r->print('<option value="'.$value.'"'.
13167:                       ($i eq $defaultcol ? ' selected="selected" ':'').'>'.
13168:                       $display.'</option>');
13169: 	}
13170: 	$r->print('</select></td><td>');
13171: 	foreach my $line (0..($max_samples-1)) {
13172: 	    if (defined($samples->[$line]{$key})) { 
13173: 		$r->print($samples->[$line]{$key}."<br />\n"); 
13174: 	    }
13175: 	}
13176: 	$r->print('</td>'.&end_data_table_row());
13177: 	$i++;
13178:     }
13179:     $r->print(&end_data_table());
13180:     $i--;
13181:     return($i);
13182: }
13183: 
13184: ######################################################
13185: ######################################################
13186: 
13187: =pod
13188: 
13189: =item * &clean_excel_name($name)
13190: 
13191: Returns a replacement for $name which does not contain any illegal characters.
13192: 
13193: =cut
13194: 
13195: ######################################################
13196: ######################################################
13197: sub clean_excel_name {
13198:     my ($name) = @_;
13199:     $name =~ s/[:\*\?\/\\]//g;
13200:     if (length($name) > 31) {
13201:         $name = substr($name,0,31);
13202:     }
13203:     return $name;
13204: }
13205: 
13206: =pod
13207: 
13208: =item * &check_if_partid_hidden($id,$symb,$udom,$uname)
13209: 
13210: Returns either 1 or undef
13211: 
13212: 1 if the part is to be hidden, undef if it is to be shown
13213: 
13214: Arguments are:
13215: 
13216: $id the id of the part to be checked
13217: $symb, optional the symb of the resource to check
13218: $udom, optional the domain of the user to check for
13219: $uname, optional the username of the user to check for
13220: 
13221: =cut
13222: 
13223: sub check_if_partid_hidden {
13224:     my ($id,$symb,$udom,$uname) = @_;
13225:     my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
13226: 					 $symb,$udom,$uname);
13227:     my $truth=1;
13228:     #if the string starts with !, then the list is the list to show not hide
13229:     if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
13230:     my @hiddenlist=split(/,/,$hiddenparts);
13231:     foreach my $checkid (@hiddenlist) {
13232: 	if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
13233:     }
13234:     return !$truth;
13235: }
13236: 
13237: 
13238: ############################################################
13239: ############################################################
13240: 
13241: =pod
13242: 
13243: =back 
13244: 
13245: =head1 cgi-bin script and graphing routines
13246: 
13247: =over 4
13248: 
13249: =item * &get_cgi_id()
13250: 
13251: Inputs: none
13252: 
13253: Returns an id which can be used to pass environment variables
13254: to various cgi-bin scripts.  These environment variables will
13255: be removed from the users environment after a given time by
13256: the routine &Apache::lonnet::transfer_profile_to_env.
13257: 
13258: =cut
13259: 
13260: ############################################################
13261: ############################################################
13262: my $uniq=0;
13263: sub get_cgi_id {
13264:     $uniq=($uniq+1)%100000;
13265:     return (time.'_'.$$.'_'.$uniq);
13266: }
13267: 
13268: ############################################################
13269: ############################################################
13270: 
13271: =pod
13272: 
13273: =item * &DrawBarGraph()
13274: 
13275: Facilitates the plotting of data in a (stacked) bar graph.
13276: Puts plot definition data into the users environment in order for 
13277: graph.png to plot it.  Returns an <img> tag for the plot.
13278: The bars on the plot are labeled '1','2',...,'n'.
13279: 
13280: Inputs:
13281: 
13282: =over 4
13283: 
13284: =item $Title: string, the title of the plot
13285: 
13286: =item $xlabel: string, text describing the X-axis of the plot
13287: 
13288: =item $ylabel: string, text describing the Y-axis of the plot
13289: 
13290: =item $Max: scalar, the maximum Y value to use in the plot
13291: If $Max is < any data point, the graph will not be rendered.
13292: 
13293: =item $colors: array ref holding the colors to be used for the data sets when
13294: they are plotted.  If undefined, default values will be used.
13295: 
13296: =item $labels: array ref holding the labels to use on the x-axis for the bars.
13297: 
13298: =item @Values: An array of array references.  Each array reference holds data
13299: to be plotted in a stacked bar chart.
13300: 
13301: =item If the final element of @Values is a hash reference the key/value
13302: pairs will be added to the graph definition.
13303: 
13304: =back
13305: 
13306: Returns:
13307: 
13308: An <img> tag which references graph.png and the appropriate identifying
13309: information for the plot.
13310: 
13311: =cut
13312: 
13313: ############################################################
13314: ############################################################
13315: sub DrawBarGraph {
13316:     my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
13317:     #
13318:     if (! defined($colors)) {
13319:         $colors = ['#33ff00', 
13320:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
13321:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
13322:                   ]; 
13323:     }
13324:     my $extra_settings = {};
13325:     if (ref($Values[-1]) eq 'HASH') {
13326:         $extra_settings = pop(@Values);
13327:     }
13328:     #
13329:     my $identifier = &get_cgi_id();
13330:     my $id = 'cgi.'.$identifier;        
13331:     if (! @Values || ref($Values[0]) ne 'ARRAY') {
13332:         return '';
13333:     }
13334:     #
13335:     my @Labels;
13336:     if (defined($labels)) {
13337:         @Labels = @$labels;
13338:     } else {
13339:         for (my $i=0;$i<@{$Values[0]};$i++) {
13340:             push (@Labels,$i+1);
13341:         }
13342:     }
13343:     #
13344:     my $NumBars = scalar(@{$Values[0]});
13345:     if ($NumBars < scalar(@Labels)) { $NumBars = scalar(@Labels); }
13346:     my %ValuesHash;
13347:     my $NumSets=1;
13348:     foreach my $array (@Values) {
13349:         next if (! ref($array));
13350:         $ValuesHash{$id.'.data.'.$NumSets++} = 
13351:             join(',',@$array);
13352:     }
13353:     #
13354:     my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
13355:     if ($NumBars < 3) {
13356:         $width = 120+$NumBars*32;
13357:         $xskip = 1;
13358:         $bar_width = 30;
13359:     } elsif ($NumBars < 5) {
13360:         $width = 120+$NumBars*20;
13361:         $xskip = 1;
13362:         $bar_width = 20;
13363:     } elsif ($NumBars < 10) {
13364:         $width = 120+$NumBars*15;
13365:         $xskip = 1;
13366:         $bar_width = 15;
13367:     } elsif ($NumBars <= 25) {
13368:         $width = 120+$NumBars*11;
13369:         $xskip = 5;
13370:         $bar_width = 8;
13371:     } elsif ($NumBars <= 50) {
13372:         $width = 120+$NumBars*8;
13373:         $xskip = 5;
13374:         $bar_width = 4;
13375:     } else {
13376:         $width = 120+$NumBars*8;
13377:         $xskip = 5;
13378:         $bar_width = 4;
13379:     }
13380:     #
13381:     $Max = 1 if ($Max < 1);
13382:     if ( int($Max) < $Max ) {
13383:         $Max++;
13384:         $Max = int($Max);
13385:     }
13386:     $Title  = '' if (! defined($Title));
13387:     $xlabel = '' if (! defined($xlabel));
13388:     $ylabel = '' if (! defined($ylabel));
13389:     $ValuesHash{$id.'.title'}    = &escape($Title);
13390:     $ValuesHash{$id.'.xlabel'}   = &escape($xlabel);
13391:     $ValuesHash{$id.'.ylabel'}   = &escape($ylabel);
13392:     $ValuesHash{$id.'.y_max_value'} = $Max;
13393:     $ValuesHash{$id.'.NumBars'}  = $NumBars;
13394:     $ValuesHash{$id.'.NumSets'}  = $NumSets;
13395:     $ValuesHash{$id.'.PlotType'} = 'bar';
13396:     $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
13397:     $ValuesHash{$id.'.height'}   = $height;
13398:     $ValuesHash{$id.'.width'}    = $width;
13399:     $ValuesHash{$id.'.xskip'}    = $xskip;
13400:     $ValuesHash{$id.'.bar_width'} = $bar_width;
13401:     $ValuesHash{$id.'.labels'} = join(',',@Labels);
13402:     #
13403:     # Deal with other parameters
13404:     while (my ($key,$value) = each(%$extra_settings)) {
13405:         $ValuesHash{$id.'.'.$key} = $value;
13406:     }
13407:     #
13408:     &Apache::lonnet::appenv(\%ValuesHash);
13409:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
13410: }
13411: 
13412: ############################################################
13413: ############################################################
13414: 
13415: =pod
13416: 
13417: =item * &DrawXYGraph()
13418: 
13419: Facilitates the plotting of data in an XY graph.
13420: Puts plot definition data into the users environment in order for 
13421: graph.png to plot it.  Returns an <img> tag for the plot.
13422: 
13423: Inputs:
13424: 
13425: =over 4
13426: 
13427: =item $Title: string, the title of the plot
13428: 
13429: =item $xlabel: string, text describing the X-axis of the plot
13430: 
13431: =item $ylabel: string, text describing the Y-axis of the plot
13432: 
13433: =item $Max: scalar, the maximum Y value to use in the plot
13434: If $Max is < any data point, the graph will not be rendered.
13435: 
13436: =item $colors: Array ref containing the hex color codes for the data to be 
13437: plotted in.  If undefined, default values will be used.
13438: 
13439: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
13440: 
13441: =item $Ydata: Array ref containing Array refs.  
13442: Each of the contained arrays will be plotted as a separate curve.
13443: 
13444: =item %Values: hash indicating or overriding any default values which are 
13445: passed to graph.png.  
13446: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
13447: 
13448: =back
13449: 
13450: Returns:
13451: 
13452: An <img> tag which references graph.png and the appropriate identifying
13453: information for the plot.
13454: 
13455: =cut
13456: 
13457: ############################################################
13458: ############################################################
13459: sub DrawXYGraph {
13460:     my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
13461:     #
13462:     # Create the identifier for the graph
13463:     my $identifier = &get_cgi_id();
13464:     my $id = 'cgi.'.$identifier;
13465:     #
13466:     $Title  = '' if (! defined($Title));
13467:     $xlabel = '' if (! defined($xlabel));
13468:     $ylabel = '' if (! defined($ylabel));
13469:     my %ValuesHash = 
13470:         (
13471:          $id.'.title'  => &escape($Title),
13472:          $id.'.xlabel' => &escape($xlabel),
13473:          $id.'.ylabel' => &escape($ylabel),
13474:          $id.'.y_max_value'=> $Max,
13475:          $id.'.labels'     => join(',',@$Xlabels),
13476:          $id.'.PlotType'   => 'XY',
13477:          );
13478:     #
13479:     if (defined($colors) && ref($colors) eq 'ARRAY') {
13480:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
13481:     }
13482:     #
13483:     if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
13484:         return '';
13485:     }
13486:     my $NumSets=1;
13487:     foreach my $array (@{$Ydata}){
13488:         next if (! ref($array));
13489:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
13490:     }
13491:     $ValuesHash{$id.'.NumSets'} = $NumSets-1;
13492:     #
13493:     # Deal with other parameters
13494:     while (my ($key,$value) = each(%Values)) {
13495:         $ValuesHash{$id.'.'.$key} = $value;
13496:     }
13497:     #
13498:     &Apache::lonnet::appenv(\%ValuesHash);
13499:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
13500: }
13501: 
13502: ############################################################
13503: ############################################################
13504: 
13505: =pod
13506: 
13507: =item * &DrawXYYGraph()
13508: 
13509: Facilitates the plotting of data in an XY graph with two Y axes.
13510: Puts plot definition data into the users environment in order for 
13511: graph.png to plot it.  Returns an <img> tag for the plot.
13512: 
13513: Inputs:
13514: 
13515: =over 4
13516: 
13517: =item $Title: string, the title of the plot
13518: 
13519: =item $xlabel: string, text describing the X-axis of the plot
13520: 
13521: =item $ylabel: string, text describing the Y-axis of the plot
13522: 
13523: =item $colors: Array ref containing the hex color codes for the data to be 
13524: plotted in.  If undefined, default values will be used.
13525: 
13526: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
13527: 
13528: =item $Ydata1: The first data set
13529: 
13530: =item $Min1: The minimum value of the left Y-axis
13531: 
13532: =item $Max1: The maximum value of the left Y-axis
13533: 
13534: =item $Ydata2: The second data set
13535: 
13536: =item $Min2: The minimum value of the right Y-axis
13537: 
13538: =item $Max2: The maximum value of the left Y-axis
13539: 
13540: =item %Values: hash indicating or overriding any default values which are 
13541: passed to graph.png.  
13542: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
13543: 
13544: =back
13545: 
13546: Returns:
13547: 
13548: An <img> tag which references graph.png and the appropriate identifying
13549: information for the plot.
13550: 
13551: =cut
13552: 
13553: ############################################################
13554: ############################################################
13555: sub DrawXYYGraph {
13556:     my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
13557:                                         $Ydata2,$Min2,$Max2,%Values)=@_;
13558:     #
13559:     # Create the identifier for the graph
13560:     my $identifier = &get_cgi_id();
13561:     my $id = 'cgi.'.$identifier;
13562:     #
13563:     $Title  = '' if (! defined($Title));
13564:     $xlabel = '' if (! defined($xlabel));
13565:     $ylabel = '' if (! defined($ylabel));
13566:     my %ValuesHash = 
13567:         (
13568:          $id.'.title'  => &escape($Title),
13569:          $id.'.xlabel' => &escape($xlabel),
13570:          $id.'.ylabel' => &escape($ylabel),
13571:          $id.'.labels' => join(',',@$Xlabels),
13572:          $id.'.PlotType' => 'XY',
13573:          $id.'.NumSets' => 2,
13574:          $id.'.two_axes' => 1,
13575:          $id.'.y1_max_value' => $Max1,
13576:          $id.'.y1_min_value' => $Min1,
13577:          $id.'.y2_max_value' => $Max2,
13578:          $id.'.y2_min_value' => $Min2,
13579:          );
13580:     #
13581:     if (defined($colors) && ref($colors) eq 'ARRAY') {
13582:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
13583:     }
13584:     #
13585:     if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
13586:         ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
13587:         return '';
13588:     }
13589:     my $NumSets=1;
13590:     foreach my $array ($Ydata1,$Ydata2){
13591:         next if (! ref($array));
13592:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
13593:     }
13594:     #
13595:     # Deal with other parameters
13596:     while (my ($key,$value) = each(%Values)) {
13597:         $ValuesHash{$id.'.'.$key} = $value;
13598:     }
13599:     #
13600:     &Apache::lonnet::appenv(\%ValuesHash);
13601:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
13602: }
13603: 
13604: ############################################################
13605: ############################################################
13606: 
13607: =pod
13608: 
13609: =back 
13610: 
13611: =head1 Statistics helper routines?  
13612: 
13613: Bad place for them but what the hell.
13614: 
13615: =over 4
13616: 
13617: =item * &chartlink()
13618: 
13619: Returns a link to the chart for a specific student.  
13620: 
13621: Inputs:
13622: 
13623: =over 4
13624: 
13625: =item $linktext: The text of the link
13626: 
13627: =item $sname: The students username
13628: 
13629: =item $sdomain: The students domain
13630: 
13631: =back
13632: 
13633: =back
13634: 
13635: =cut
13636: 
13637: ############################################################
13638: ############################################################
13639: sub chartlink {
13640:     my ($linktext, $sname, $sdomain) = @_;
13641:     my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
13642:         '&amp;SelectedStudent='.&escape($sname.':'.$sdomain).
13643:         '&amp;chartoutputmode='.HTML::Entities::encode('html, with all links').
13644:        '">'.$linktext.'</a>';
13645: }
13646: 
13647: #######################################################
13648: #######################################################
13649: 
13650: =pod
13651: 
13652: =head1 Course Environment Routines
13653: 
13654: =over 4
13655: 
13656: =item * &restore_course_settings()
13657: 
13658: =item * &store_course_settings()
13659: 
13660: Restores/Store indicated form parameters from the course environment.
13661: Will not overwrite existing values of the form parameters.
13662: 
13663: Inputs: 
13664: a scalar describing the data (e.g. 'chart', 'problem_analysis')
13665: 
13666: a hash ref describing the data to be stored.  For example:
13667:    
13668: %Save_Parameters = ('Status' => 'scalar',
13669:     'chartoutputmode' => 'scalar',
13670:     'chartoutputdata' => 'scalar',
13671:     'Section' => 'array',
13672:     'Group' => 'array',
13673:     'StudentData' => 'array',
13674:     'Maps' => 'array');
13675: 
13676: Returns: both routines return nothing
13677: 
13678: =back
13679: 
13680: =cut
13681: 
13682: #######################################################
13683: #######################################################
13684: sub store_course_settings {
13685:     return &store_settings($env{'request.course.id'},@_);
13686: }
13687: 
13688: sub store_settings {
13689:     # save to the environment
13690:     # appenv the same items, just to be safe
13691:     my $udom  = $env{'user.domain'};
13692:     my $uname = $env{'user.name'};
13693:     my ($context,$prefix,$Settings) = @_;
13694:     my %SaveHash;
13695:     my %AppHash;
13696:     while (my ($setting,$type) = each(%$Settings)) {
13697:         my $basename = join('.','internal',$context,$prefix,$setting);
13698:         my $envname = 'environment.'.$basename;
13699:         if (exists($env{'form.'.$setting})) {
13700:             # Save this value away
13701:             if ($type eq 'scalar' &&
13702:                 (! exists($env{$envname}) || 
13703:                  $env{$envname} ne $env{'form.'.$setting})) {
13704:                 $SaveHash{$basename} = $env{'form.'.$setting};
13705:                 $AppHash{$envname}   = $env{'form.'.$setting};
13706:             } elsif ($type eq 'array') {
13707:                 my $stored_form;
13708:                 if (ref($env{'form.'.$setting})) {
13709:                     $stored_form = join(',',
13710:                                         map {
13711:                                             &escape($_);
13712:                                         } sort(@{$env{'form.'.$setting}}));
13713:                 } else {
13714:                     $stored_form = 
13715:                         &escape($env{'form.'.$setting});
13716:                 }
13717:                 # Determine if the array contents are the same.
13718:                 if ($stored_form ne $env{$envname}) {
13719:                     $SaveHash{$basename} = $stored_form;
13720:                     $AppHash{$envname}   = $stored_form;
13721:                 }
13722:             }
13723:         }
13724:     }
13725:     my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
13726:                                           $udom,$uname);
13727:     if ($put_result !~ /^(ok|delayed)/) {
13728:         &Apache::lonnet::logthis('unable to save form parameters, '.
13729:                                  'got error:'.$put_result);
13730:     }
13731:     # Make sure these settings stick around in this session, too
13732:     &Apache::lonnet::appenv(\%AppHash);
13733:     return;
13734: }
13735: 
13736: sub restore_course_settings {
13737:     return &restore_settings($env{'request.course.id'},@_);
13738: }
13739: 
13740: sub restore_settings {
13741:     my ($context,$prefix,$Settings) = @_;
13742:     while (my ($setting,$type) = each(%$Settings)) {
13743:         next if (exists($env{'form.'.$setting}));
13744:         my $envname = 'environment.internal.'.$context.'.'.$prefix.
13745:             '.'.$setting;
13746:         if (exists($env{$envname})) {
13747:             if ($type eq 'scalar') {
13748:                 $env{'form.'.$setting} = $env{$envname};
13749:             } elsif ($type eq 'array') {
13750:                 $env{'form.'.$setting} = [ 
13751:                                            map { 
13752:                                                &unescape($_); 
13753:                                            } split(',',$env{$envname})
13754:                                            ];
13755:             }
13756:         }
13757:     }
13758: }
13759: 
13760: #######################################################
13761: #######################################################
13762: 
13763: =pod
13764: 
13765: =head1 Domain E-mail Routines  
13766: 
13767: =over 4
13768: 
13769: =item * &build_recipient_list()
13770: 
13771: Build recipient lists for following types of e-mail:
13772: (a) Error Reports, (b) Package Updates, (c) lonstatus warnings/errors
13773: (d) Help requests, (e) Course requests needing approval, (f) loncapa
13774: module change checking, student/employee ID conflict checks, as
13775: generated by lonerrorhandler.pm, CHECKRPMS, loncron,
13776: lonsupportreq.pm, loncoursequeueadmin.pm, searchcat.pl respectively.
13777: 
13778: Inputs:
13779: defmail (scalar - email address of default recipient), 
13780: mailing type (scalar: errormail, packagesmail, helpdeskmail,
13781: requestsmail, updatesmail, or idconflictsmail).
13782: 
13783: defdom (domain for which to retrieve configuration settings),
13784: 
13785: origmail (scalar - email address of recipient from loncapa.conf, 
13786: i.e., predates configuration by DC via domainprefs.pm 
13787: 
13788: Returns: comma separated list of addresses to which to send e-mail.
13789: 
13790: =back
13791: 
13792: =cut
13793: 
13794: ############################################################
13795: ############################################################
13796: sub build_recipient_list {
13797:     my ($defmail,$mailing,$defdom,$origmail) = @_;
13798:     my @recipients;
13799:     my $otheremails;
13800:     my %domconfig =
13801:          &Apache::lonnet::get_dom('configuration',['contacts'],$defdom);
13802:     if (ref($domconfig{'contacts'}) eq 'HASH') {
13803:         if (exists($domconfig{'contacts'}{$mailing})) {
13804:             if (ref($domconfig{'contacts'}{$mailing}) eq 'HASH') {
13805:                 my @contacts = ('adminemail','supportemail');
13806:                 foreach my $item (@contacts) {
13807:                     if ($domconfig{'contacts'}{$mailing}{$item}) {
13808:                         my $addr = $domconfig{'contacts'}{$item}; 
13809:                         if (!grep(/^\Q$addr\E$/,@recipients)) {
13810:                             push(@recipients,$addr);
13811:                         }
13812:                     }
13813:                     $otheremails = $domconfig{'contacts'}{$mailing}{'others'};
13814:                 }
13815:             }
13816:         } elsif ($origmail ne '') {
13817:             push(@recipients,$origmail);
13818:         }
13819:     } elsif ($origmail ne '') {
13820:         push(@recipients,$origmail);
13821:     }
13822:     if (defined($defmail)) {
13823:         if ($defmail ne '') {
13824:             push(@recipients,$defmail);
13825:         }
13826:     }
13827:     if ($otheremails) {
13828:         my @others;
13829:         if ($otheremails =~ /,/) {
13830:             @others = split(/,/,$otheremails);
13831:         } else {
13832:             push(@others,$otheremails);
13833:         }
13834:         foreach my $addr (@others) {
13835:             if (!grep(/^\Q$addr\E$/,@recipients)) {
13836:                 push(@recipients,$addr);
13837:             }
13838:         }
13839:     }
13840:     my $recipientlist = join(',',@recipients); 
13841:     return $recipientlist;
13842: }
13843: 
13844: ############################################################
13845: ############################################################
13846: 
13847: =pod
13848: 
13849: =head1 Course Catalog Routines
13850: 
13851: =over 4
13852: 
13853: =item * &gather_categories()
13854: 
13855: Converts category definitions - keys of categories hash stored in  
13856: coursecategories in configuration.db on the primary library server in a 
13857: domain - to an array.  Also generates javascript and idx hash used to 
13858: generate Domain Coordinator interface for editing Course Categories.
13859: 
13860: Inputs:
13861: 
13862: categories (reference to hash of category definitions).
13863: 
13864: cats (reference to array of arrays/hashes which encapsulates hierarchy of
13865:       categories and subcategories).
13866: 
13867: idx (reference to hash of counters used in Domain Coordinator interface for 
13868:       editing Course Categories).
13869: 
13870: jsarray (reference to array of categories used to create Javascript arrays for
13871:          Domain Coordinator interface for editing Course Categories).
13872: 
13873: Returns: nothing
13874: 
13875: Side effects: populates cats, idx and jsarray. 
13876: 
13877: =cut
13878: 
13879: sub gather_categories {
13880:     my ($categories,$cats,$idx,$jsarray) = @_;
13881:     my %counters;
13882:     my $num = 0;
13883:     foreach my $item (keys(%{$categories})) {
13884:         my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
13885:         if ($container eq '' && $depth == 0) {
13886:             $cats->[$depth][$categories->{$item}] = $cat;
13887:         } else {
13888:             $cats->[$depth]{$container}[$categories->{$item}] = $cat;
13889:         }
13890:         my ($escitem,$tail) = split(/:/,$item,2);
13891:         if ($counters{$tail} eq '') {
13892:             $counters{$tail} = $num;
13893:             $num ++;
13894:         }
13895:         if (ref($idx) eq 'HASH') {
13896:             $idx->{$item} = $counters{$tail};
13897:         }
13898:         if (ref($jsarray) eq 'ARRAY') {
13899:             push(@{$jsarray->[$counters{$tail}]},$item);
13900:         }
13901:     }
13902:     return;
13903: }
13904: 
13905: =pod
13906: 
13907: =item * &extract_categories()
13908: 
13909: Used to generate breadcrumb trails for course categories.
13910: 
13911: Inputs:
13912: 
13913: categories (reference to hash of category definitions).
13914: 
13915: cats (reference to array of arrays/hashes which encapsulates hierarchy of
13916:       categories and subcategories).
13917: 
13918: trails (reference to array of breacrumb trails for each category).
13919: 
13920: allitems (reference to hash - key is category key 
13921:          (format: escaped(name):escaped(parent category):depth in hierarchy).
13922: 
13923: idx (reference to hash of counters used in Domain Coordinator interface for
13924:       editing Course Categories).
13925: 
13926: jsarray (reference to array of categories used to create Javascript arrays for
13927:          Domain Coordinator interface for editing Course Categories).
13928: 
13929: subcats (reference to hash of arrays containing all subcategories within each 
13930:          category, -recursive)
13931: 
13932: Returns: nothing
13933: 
13934: Side effects: populates trails and allitems hash references.
13935: 
13936: =cut
13937: 
13938: sub extract_categories {
13939:     my ($categories,$cats,$trails,$allitems,$idx,$jsarray,$subcats) = @_;
13940:     if (ref($categories) eq 'HASH') {
13941:         &gather_categories($categories,$cats,$idx,$jsarray);
13942:         if (ref($cats->[0]) eq 'ARRAY') {
13943:             for (my $i=0; $i<@{$cats->[0]}; $i++) {
13944:                 my $name = $cats->[0][$i];
13945:                 my $item = &escape($name).'::0';
13946:                 my $trailstr;
13947:                 if ($name eq 'instcode') {
13948:                     $trailstr = &mt('Official courses (with institutional codes)');
13949:                 } elsif ($name eq 'communities') {
13950:                     $trailstr = &mt('Communities');
13951:                 } else {
13952:                     $trailstr = $name;
13953:                 }
13954:                 if ($allitems->{$item} eq '') {
13955:                     push(@{$trails},$trailstr);
13956:                     $allitems->{$item} = scalar(@{$trails})-1;
13957:                 }
13958:                 my @parents = ($name);
13959:                 if (ref($cats->[1]{$name}) eq 'ARRAY') {
13960:                     for (my $j=0; $j<@{$cats->[1]{$name}}; $j++) {
13961:                         my $category = $cats->[1]{$name}[$j];
13962:                         if (ref($subcats) eq 'HASH') {
13963:                             push(@{$subcats->{$item}},&escape($category).':'.&escape($name).':1');
13964:                         }
13965:                         &recurse_categories($cats,2,$category,$trails,$allitems,\@parents,$subcats);
13966:                     }
13967:                 } else {
13968:                     if (ref($subcats) eq 'HASH') {
13969:                         $subcats->{$item} = [];
13970:                     }
13971:                 }
13972:             }
13973:         }
13974:     }
13975:     return;
13976: }
13977: 
13978: =pod
13979: 
13980: =item * &recurse_categories()
13981: 
13982: Recursively used to generate breadcrumb trails for course categories.
13983: 
13984: Inputs:
13985: 
13986: cats (reference to array of arrays/hashes which encapsulates hierarchy of
13987:       categories and subcategories).
13988: 
13989: depth (current depth in hierarchy of categories and sub-categories - 0 indexed).
13990: 
13991: category (current course category, for which breadcrumb trail is being generated).
13992: 
13993: trails (reference to array of breadcrumb trails for each category).
13994: 
13995: allitems (reference to hash - key is category key
13996:          (format: escaped(name):escaped(parent category):depth in hierarchy).
13997: 
13998: parents (array containing containers directories for current category, 
13999:          back to top level). 
14000: 
14001: Returns: nothing
14002: 
14003: Side effects: populates trails and allitems hash references
14004: 
14005: =cut
14006: 
14007: sub recurse_categories {
14008:     my ($cats,$depth,$category,$trails,$allitems,$parents,$subcats) = @_;
14009:     my $shallower = $depth - 1;
14010:     if (ref($cats->[$depth]{$category}) eq 'ARRAY') {
14011:         for (my $k=0; $k<@{$cats->[$depth]{$category}}; $k++) {
14012:             my $name = $cats->[$depth]{$category}[$k];
14013:             my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
14014:             my $trailstr = join(' -&gt; ',(@{$parents},$category));
14015:             if ($allitems->{$item} eq '') {
14016:                 push(@{$trails},$trailstr);
14017:                 $allitems->{$item} = scalar(@{$trails})-1;
14018:             }
14019:             my $deeper = $depth+1;
14020:             push(@{$parents},$category);
14021:             if (ref($subcats) eq 'HASH') {
14022:                 my $subcat = &escape($name).':'.$category.':'.$depth;
14023:                 for (my $j=@{$parents}; $j>=0; $j--) {
14024:                     my $higher;
14025:                     if ($j > 0) {
14026:                         $higher = &escape($parents->[$j]).':'.
14027:                                   &escape($parents->[$j-1]).':'.$j;
14028:                     } else {
14029:                         $higher = &escape($parents->[$j]).'::'.$j;
14030:                     }
14031:                     push(@{$subcats->{$higher}},$subcat);
14032:                 }
14033:             }
14034:             &recurse_categories($cats,$deeper,$name,$trails,$allitems,$parents,
14035:                                 $subcats);
14036:             pop(@{$parents});
14037:         }
14038:     } else {
14039:         my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
14040:         my $trailstr = join(' -&gt; ',(@{$parents},$category));
14041:         if ($allitems->{$item} eq '') {
14042:             push(@{$trails},$trailstr);
14043:             $allitems->{$item} = scalar(@{$trails})-1;
14044:         }
14045:     }
14046:     return;
14047: }
14048: 
14049: =pod
14050: 
14051: =item * &assign_categories_table()
14052: 
14053: Create a datatable for display of hierarchical categories in a domain,
14054: with checkboxes to allow a course to be categorized. 
14055: 
14056: Inputs:
14057: 
14058: cathash - reference to hash of categories defined for the domain (from
14059:           configuration.db)
14060: 
14061: currcat - scalar with an & separated list of categories assigned to a course. 
14062: 
14063: type    - scalar contains course type (Course or Community).
14064: 
14065: Returns: $output (markup to be displayed) 
14066: 
14067: =cut
14068: 
14069: sub assign_categories_table {
14070:     my ($cathash,$currcat,$type) = @_;
14071:     my $output;
14072:     if (ref($cathash) eq 'HASH') {
14073:         my (@cats,@trails,%allitems,%idx,@jsarray,@path,$maxdepth);
14074:         &extract_categories($cathash,\@cats,\@trails,\%allitems,\%idx,\@jsarray);
14075:         $maxdepth = scalar(@cats);
14076:         if (@cats > 0) {
14077:             my $itemcount = 0;
14078:             if (ref($cats[0]) eq 'ARRAY') {
14079:                 my @currcategories;
14080:                 if ($currcat ne '') {
14081:                     @currcategories = split('&',$currcat);
14082:                 }
14083:                 my $table;
14084:                 for (my $i=0; $i<@{$cats[0]}; $i++) {
14085:                     my $parent = $cats[0][$i];
14086:                     next if ($parent eq 'instcode');
14087:                     if ($type eq 'Community') {
14088:                         next unless ($parent eq 'communities');
14089:                     } else {
14090:                         next if ($parent eq 'communities');
14091:                     }
14092:                     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
14093:                     my $item = &escape($parent).'::0';
14094:                     my $checked = '';
14095:                     if (@currcategories > 0) {
14096:                         if (grep(/^\Q$item\E$/,@currcategories)) {
14097:                             $checked = ' checked="checked"';
14098:                         }
14099:                     }
14100:                     my $parent_title = $parent;
14101:                     if ($parent eq 'communities') {
14102:                         $parent_title = &mt('Communities');
14103:                     }
14104:                     $table .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.
14105:                               '<input type="checkbox" name="usecategory" value="'.
14106:                               $item.'"'.$checked.' />'.$parent_title.'</span>'.
14107:                               '<input type="hidden" name="catname" value="'.$parent.'" /></td>';
14108:                     my $depth = 1;
14109:                     push(@path,$parent);
14110:                     $table .= &assign_category_rows($itemcount,\@cats,$depth,$parent,\@path,\@currcategories);
14111:                     pop(@path);
14112:                     $table .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
14113:                     $itemcount ++;
14114:                 }
14115:                 if ($itemcount) {
14116:                     $output = &Apache::loncommon::start_data_table().
14117:                               $table.
14118:                               &Apache::loncommon::end_data_table();
14119:                 }
14120:             }
14121:         }
14122:     }
14123:     return $output;
14124: }
14125: 
14126: =pod
14127: 
14128: =item * &assign_category_rows()
14129: 
14130: Create a datatable row for display of nested categories in a domain,
14131: with checkboxes to allow a course to be categorized,called recursively.
14132: 
14133: Inputs:
14134: 
14135: itemcount - track row number for alternating colors
14136: 
14137: cats - reference to array of arrays/hashes which encapsulates hierarchy of
14138:       categories and subcategories.
14139: 
14140: depth - current depth in hierarchy of categories and sub-categories - 0 indexed.
14141: 
14142: parent - parent of current category item
14143: 
14144: path - Array containing all categories back up through the hierarchy from the
14145:        current category to the top level.
14146: 
14147: currcategories - reference to array of current categories assigned to the course
14148: 
14149: Returns: $output (markup to be displayed).
14150: 
14151: =cut
14152: 
14153: sub assign_category_rows {
14154:     my ($itemcount,$cats,$depth,$parent,$path,$currcategories) = @_;
14155:     my ($text,$name,$item,$chgstr);
14156:     if (ref($cats) eq 'ARRAY') {
14157:         my $maxdepth = scalar(@{$cats});
14158:         if (ref($cats->[$depth]) eq 'HASH') {
14159:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
14160:                 my $numchildren = @{$cats->[$depth]{$parent}};
14161:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
14162:                 $text .= '<td><table class="LC_data_table">';
14163:                 for (my $j=0; $j<$numchildren; $j++) {
14164:                     $name = $cats->[$depth]{$parent}[$j];
14165:                     $item = &escape($name).':'.&escape($parent).':'.$depth;
14166:                     my $deeper = $depth+1;
14167:                     my $checked = '';
14168:                     if (ref($currcategories) eq 'ARRAY') {
14169:                         if (@{$currcategories} > 0) {
14170:                             if (grep(/^\Q$item\E$/,@{$currcategories})) {
14171:                                 $checked = ' checked="checked"';
14172:                             }
14173:                         }
14174:                     }
14175:                     $text .= '<tr><td><span class="LC_nobreak"><label>'.
14176:                              '<input type="checkbox" name="usecategory" value="'.
14177:                              $item.'"'.$checked.' />'.$name.'</label></span>'.
14178:                              '<input type="hidden" name="catname" value="'.$name.'" />'.
14179:                              '</td><td>';
14180:                     if (ref($path) eq 'ARRAY') {
14181:                         push(@{$path},$name);
14182:                         $text .= &assign_category_rows($itemcount,$cats,$deeper,$name,$path,$currcategories);
14183:                         pop(@{$path});
14184:                     }
14185:                     $text .= '</td></tr>';
14186:                 }
14187:                 $text .= '</table></td>';
14188:             }
14189:         }
14190:     }
14191:     return $text;
14192: }
14193: 
14194: =pod
14195: 
14196: =back
14197: 
14198: =cut
14199: 
14200: ############################################################
14201: ############################################################
14202: 
14203: 
14204: sub commit_customrole {
14205:     my ($udom,$uname,$url,$three,$four,$five,$start,$end,$context) = @_;
14206:     my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.':'.$three.' in '.$url.
14207:                          ($start?', '.&mt('starting').' '.localtime($start):'').
14208:                          ($end?', ending '.localtime($end):'').': <b>'.
14209:               &Apache::lonnet::assigncustomrole(
14210:                  $udom,$uname,$url,$three,$four,$five,$end,$start,undef,undef,$context).
14211:                  '</b><br />';
14212:     return $output;
14213: }
14214: 
14215: sub commit_standardrole {
14216:     my ($udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context,$credits) = @_;
14217:     my ($output,$logmsg,$linefeed);
14218:     if ($context eq 'auto') {
14219:         $linefeed = "\n";
14220:     } else {
14221:         $linefeed = "<br />\n";
14222:     }  
14223:     if ($three eq 'st') {
14224:         my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$three,$start,$end,
14225:                                          $one,$two,$sec,$context,$credits);
14226:         if (($result =~ /^error/) || ($result eq 'not_in_class') || 
14227:             ($result eq 'unknown_course') || ($result eq 'refused')) {
14228:             $output = $logmsg.' '.&mt('Error: ').$result."\n"; 
14229:         } else {
14230:             $output = $logmsg.$linefeed.&mt('Assigning').' '.$three.' in '.$url.
14231:                ($start?', '.&mt('starting').' '.localtime($start):'').
14232:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
14233:             if ($context eq 'auto') {
14234:                 $output .= $result.$linefeed.&mt('Add to classlist').': ok';
14235:             } else {
14236:                $output .= '<b>'.$result.'</b>'.$linefeed.
14237:                &mt('Add to classlist').': <b>ok</b>';
14238:             }
14239:             $output .= $linefeed;
14240:         }
14241:     } else {
14242:         $output = &mt('Assigning').' '.$three.' in '.$url.
14243:                ($start?', '.&mt('starting').' '.localtime($start):'').
14244:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
14245:         my $result = &Apache::lonnet::assignrole($udom,$uname,$url,$three,$end,$start,'','',$context);
14246:         if ($context eq 'auto') {
14247:             $output .= $result.$linefeed;
14248:         } else {
14249:             $output .= '<b>'.$result.'</b>'.$linefeed;
14250:         }
14251:     }
14252:     return $output;
14253: }
14254: 
14255: sub commit_studentrole {
14256:     my ($logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context,
14257:         $credits) = @_;
14258:     my ($result,$linefeed,$oldsecurl,$newsecurl);
14259:     if ($context eq 'auto') {
14260:         $linefeed = "\n";
14261:     } else {
14262:         $linefeed = '<br />'."\n";
14263:     }
14264:     if (defined($one) && defined($two)) {
14265:         my $cid=$one.'_'.$two;
14266:         my $oldsec=&Apache::lonnet::getsection($udom,$uname,$cid);
14267:         my $secchange = 0;
14268:         my $expire_role_result;
14269:         my $modify_section_result;
14270:         if ($oldsec ne '-1') { 
14271:             if ($oldsec ne $sec) {
14272:                 $secchange = 1;
14273:                 my $now = time;
14274:                 my $uurl='/'.$cid;
14275:                 $uurl=~s/\_/\//g;
14276:                 if ($oldsec) {
14277:                     $uurl.='/'.$oldsec;
14278:                 }
14279:                 $oldsecurl = $uurl;
14280:                 $expire_role_result = 
14281:                     &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',$now,'','',$context);
14282:                 if ($env{'request.course.sec'} ne '') { 
14283:                     if ($expire_role_result eq 'refused') {
14284:                         my @roles = ('st');
14285:                         my @statuses = ('previous');
14286:                         my @roledoms = ($one);
14287:                         my $withsec = 1;
14288:                         my %roleshash = 
14289:                             &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
14290:                                               \@statuses,\@roles,\@roledoms,$withsec);
14291:                         if (defined ($roleshash{$two.':'.$one.':st:'.$oldsec})) {
14292:                             my ($oldstart,$oldend) = 
14293:                                 split(':',$roleshash{$two.':'.$one.':st:'.$oldsec});
14294:                             if ($oldend > 0 && $oldend <= $now) {
14295:                                 $expire_role_result = 'ok';
14296:                             }
14297:                         }
14298:                     }
14299:                 }
14300:                 $result = $expire_role_result;
14301:             }
14302:         }
14303:         if (($expire_role_result eq 'ok') || ($secchange == 0)) {
14304:             $modify_section_result = 
14305:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,
14306:                                                            undef,undef,undef,$sec,
14307:                                                            $end,$start,'','',$cid,
14308:                                                            '',$context,$credits);
14309:             if ($modify_section_result =~ /^ok/) {
14310:                 if ($secchange == 1) {
14311:                     if ($sec eq '') {
14312:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to student role without a section.',$uname,$oldsec).$linefeed;
14313:                     } else {
14314:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to new section: [_3].',$uname,$oldsec,$sec).$linefeed;
14315:                     }
14316:                 } elsif ($oldsec eq '-1') {
14317:                     if ($sec eq '') {
14318:                         $$logmsg .= &mt('New student role without a section for [_1] in course [_2].',$uname,$cid).$linefeed;
14319:                     } else {
14320:                         $$logmsg .= &mt('New student role for [_1] in section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
14321:                     }
14322:                 } else {
14323:                     if ($sec eq '') {
14324:                         $$logmsg .= &mt('Student [_1] assigned to course [_2] without a section.',$uname,$cid).$linefeed;
14325:                     } else {
14326:                         $$logmsg .= &mt('Student [_1] assigned to section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
14327:                     }
14328:                 }
14329:             } else {
14330:                 if ($secchange) { 
14331:                     $$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;
14332:                 } else {
14333:                     $$logmsg .= &mt('Error when attempting to modify role for [_1] for section: "[_2]" in course [_3] -error:',$uname,$sec,$cid).' '.$modify_section_result.$linefeed;
14334:                 }
14335:             }
14336:             $result = $modify_section_result;
14337:         } elsif ($secchange == 1) {
14338:             if ($oldsec eq '') {
14339:                 $$logmsg .= &mt('Error when attempting to expire existing role without a section for [_1] in course [_2] -error: ',$uname,$cid).' '.$expire_role_result.$linefeed;
14340:             } else {
14341:                 $$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;
14342:             }
14343:             if ($expire_role_result eq 'refused') {
14344:                 my $newsecurl = '/'.$cid;
14345:                 $newsecurl =~ s/\_/\//g;
14346:                 if ($sec ne '') {
14347:                     $newsecurl.='/'.$sec;
14348:                 }
14349:                 if (&Apache::lonnet::allowed('cst',$newsecurl) && !(&Apache::lonnet::allowed('cst',$oldsecurl))) {
14350:                     if ($sec eq '') {
14351:                         $$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;
14352:                     } else {
14353:                         $$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;
14354:                     }
14355:                 }
14356:             }
14357:         }
14358:     } else {
14359:         $$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;
14360:         $result = "error: incomplete course id\n";
14361:     }
14362:     return $result;
14363: }
14364: 
14365: sub show_role_extent {
14366:     my ($scope,$context,$role) = @_;
14367:     $scope =~ s{^/}{};
14368:     my @courseroles = &Apache::lonuserutils::roles_by_context('course',1);
14369:     push(@courseroles,'co');
14370:     my @authorroles = &Apache::lonuserutils::roles_by_context('author');
14371:     if (($context eq 'course') || (grep(/^\Q$role\E/,@courseroles))) {
14372:         $scope =~ s{/}{_};
14373:         return '<span class="LC_cusr_emph">'.$env{'course.'.$scope.'.description'}.'</span>';
14374:     } elsif (($context eq 'author') || (grep(/^\Q$role\E/,@authorroles))) {
14375:         my ($audom,$auname) = split(/\//,$scope);
14376:         return &mt('[_1] Author Space','<span class="LC_cusr_emph">'.
14377:                    &Apache::loncommon::plainname($auname,$audom).'</span>');
14378:     } else {
14379:         $scope =~ s{/$}{};
14380:         return &mt('Domain: [_1]','<span class="LC_cusr_emph">'.
14381:                    &Apache::lonnet::domain($scope,'description').'</span>');
14382:     }
14383: }
14384: 
14385: ############################################################
14386: ############################################################
14387: 
14388: sub check_clone {
14389:     my ($args,$linefeed) = @_;
14390:     my $cloneid='/'.$args->{'clonedomain'}.'/'.$args->{'clonecourse'};
14391:     my ($clonecrsudom,$clonecrsunum)= &LONCAPA::split_courseid($cloneid);
14392:     my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
14393:     my $clonemsg;
14394:     my $can_clone = 0;
14395:     my $lctype = lc($args->{'crstype'});
14396:     if ($lctype ne 'community') {
14397:         $lctype = 'course';
14398:     }
14399:     if ($clonehome eq 'no_host') {
14400:         if ($args->{'crstype'} eq 'Community') {
14401:             $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'});
14402:         } else {
14403:             $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'});
14404:         }     
14405:     } else {
14406: 	my %clonedesc = &Apache::lonnet::coursedescription($cloneid,{'one_time' => 1});
14407:         if ($args->{'crstype'} eq 'Community') {
14408:             if ($clonedesc{'type'} ne 'Community') {
14409:                  $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'});
14410:                 return ($can_clone, $clonemsg, $cloneid, $clonehome);
14411:             }
14412:         }
14413: 	if (($env{'request.role.domain'} eq $args->{'clonedomain'}) && 
14414:             (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'}))) {
14415: 	    $can_clone = 1;
14416: 	} else {
14417: 	    my %clonehash = &Apache::lonnet::get('environment',['cloners','internal.coursecode'],
14418: 						 $args->{'clonedomain'},$args->{'clonecourse'});
14419:             if ($clonehash{'cloners'} eq '') {
14420:                 my %domdefs = &Apache::lonnet::get_domain_defaults($args->{'course_domain'});
14421:                 if ($domdefs{'canclone'}) {
14422:                     unless ($domdefs{'canclone'} eq 'none') {
14423:                         if ($domdefs{'canclone'} eq 'domain') {
14424:                             if ($args->{'ccdomain'} eq $args->{'clonedomain'}) {
14425:                                 $can_clone = 1;
14426:                             }
14427:                         } elsif (($clonehash{'internal.coursecode'}) && ($args->{'crscode'}) && 
14428:                                  ($args->{'clonedomain'} eq  $args->{'course_domain'})) {
14429:                             if (&Apache::lonnet::default_instcode_cloning($args->{'clonedomain'},$domdefs{'canclone'},
14430:                                                                           $clonehash{'internal.coursecode'},$args->{'crscode'})) {
14431:                                 $can_clone = 1;
14432:                             }
14433:                         }
14434:                     }
14435:                 }
14436:             } else {
14437: 	        my @cloners = split(/,/,$clonehash{'cloners'});
14438:                 if (grep(/^\*$/,@cloners)) {
14439:                     $can_clone = 1;
14440:                 } elsif (grep(/^\*\:\Q$args->{'ccdomain'}\E$/,@cloners)) {
14441:                     $can_clone = 1;
14442:                 }
14443:                 unless ($can_clone) {
14444:                     if (($clonehash{'internal.coursecode'}) && ($args->{'crscode'}) && ($args->{'clonedomain'} eq  $args->{'course_domain'})) {
14445:                         my (%gotdomdefaults,%gotcodedefaults);
14446:                         foreach my $cloner (@cloners) {
14447:                             if (($cloner ne '*') && ($cloner !~ /^\*\:$match_domain$/) &&
14448:                                 ($cloner !~ /^$match_username\:$match_domain$/) && ($cloner ne '')) {
14449:                                 my (%codedefaults,@code_order);
14450:                                 if (ref($gotcodedefaults{$args->{'clonedomain'}}) eq 'HASH') {
14451:                                     if (ref($gotcodedefaults{$args->{'clonedomain'}}{'defaults'}) eq 'HASH') {
14452:                                         %codedefaults = %{$gotcodedefaults{$args->{'clonedomain'}}{'defaults'}};
14453:                                     }
14454:                                     if (ref($gotcodedefaults{$args->{'clonedomain'}}{'order'}) eq 'ARRAY') {
14455:                                         @code_order = @{$gotcodedefaults{$args->{'clonedomain'}}{'order'}};
14456:                                     }
14457:                                 } else {
14458:                                     &Apache::lonnet::auto_instcode_defaults($args->{'clonedomain'},
14459:                                                                             \%codedefaults,
14460:                                                                             \@code_order);
14461:                                     $gotcodedefaults{$args->{'clonedomain'}}{'defaults'} = \%codedefaults;
14462:                                     $gotcodedefaults{$args->{'clonedomain'}}{'order'} = \@code_order;
14463:                                 }
14464:                                 if (@code_order > 0) {
14465:                                     if (&Apache::lonnet::check_instcode_cloning(\%codedefaults,\@code_order,
14466:                                                                                 $cloner,$clonehash{'internal.coursecode'},
14467:                                                                                 $args->{'crscode'})) {
14468:                                         $can_clone = 1;
14469:                                         last;
14470:                                     }
14471:                                 }
14472:                             }
14473:                         }
14474:                     }
14475:                     unless ($can_clone) {
14476:                         my $ccrole = 'cc';
14477:                         if ($args->{'crstype'} eq 'Community') {
14478:                             $ccrole = 'co';
14479:                         }
14480: 	                my %roleshash =
14481: 		            &Apache::lonnet::get_my_roles($args->{'ccuname'},
14482: 					                  $args->{'ccdomain'},
14483:                                                           'userroles',['active'],[$ccrole],
14484: 					                  [$args->{'clonedomain'}]);
14485: 	                if (($roleshash{$args->{'clonecourse'}.':'.$args->{'clonedomain'}.':'.$ccrole}) || 
14486:                             (grep(/^\Q$args->{'ccuname'}\E:\Q$args->{'ccdomain'}\E$/,@cloners))) {
14487:                             $can_clone = 1;
14488:                         } elsif (&Apache::lonnet::is_course_owner($args->{'clonedomain'},$args->{'clonecourse'},
14489:                                                                   $args->{'ccuname'},$args->{'ccdomain'})) {
14490:                             $can_clone = 1;
14491:                         }
14492:                     }
14493:                 }
14494:             }
14495:             unless ($can_clone) {
14496:                 if ($args->{'crstype'} eq 'Community') {
14497:                     $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'});
14498:                 } else {
14499:                     $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'});
14500:                 }
14501: 	    }
14502:         }
14503:     }
14504:     return ($can_clone, $clonemsg, $cloneid, $clonehome);
14505: }
14506: 
14507: sub construct_course {
14508:     my ($args,$logmsg,$courseid,$crsudom,$crsunum,$udom,$uname,$context,$cnum,$category,$coderef) = @_;
14509:     my $outcome;
14510:     my $linefeed =  '<br />'."\n";
14511:     if ($context eq 'auto') {
14512:         $linefeed = "\n";
14513:     }
14514: 
14515: #
14516: # Are we cloning?
14517: #
14518:     my ($can_clone, $clonemsg, $cloneid, $clonehome);
14519:     if (($args->{'clonecourse'}) && ($args->{'clonedomain'})) {
14520: 	($can_clone, $clonemsg, $cloneid, $clonehome) = &check_clone($args,$linefeed);
14521: 	if ($context ne 'auto') {
14522:             if ($clonemsg ne '') {
14523: 	        $clonemsg = '<span class="LC_error">'.$clonemsg.'</span>';
14524:             }
14525: 	}
14526: 	$outcome .= $clonemsg.$linefeed;
14527: 
14528:         if (!$can_clone) {
14529: 	    return (0,$outcome);
14530: 	}
14531:     }
14532: 
14533: #
14534: # Open course
14535: #
14536:     my $crstype = lc($args->{'crstype'});
14537:     my %cenv=();
14538:     $$courseid=&Apache::lonnet::createcourse($args->{'course_domain'},
14539:                                              $args->{'cdescr'},
14540:                                              $args->{'curl'},
14541:                                              $args->{'course_home'},
14542:                                              $args->{'nonstandard'},
14543:                                              $args->{'crscode'},
14544:                                              $args->{'ccuname'}.':'.
14545:                                              $args->{'ccdomain'},
14546:                                              $args->{'crstype'},
14547:                                              $cnum,$context,$category);
14548: 
14549:     # Note: The testing routines depend on this being output; see 
14550:     # Utils::Course. This needs to at least be output as a comment
14551:     # if anyone ever decides to not show this, and Utils::Course::new
14552:     # will need to be suitably modified.
14553:     $outcome .= &mt('New LON-CAPA [_1] ID: [_2]',$crstype,$$courseid).$linefeed;
14554:     if ($$courseid =~ /^error:/) {
14555:         return (0,$outcome);
14556:     }
14557: 
14558: #
14559: # Check if created correctly
14560: #
14561:     ($$crsudom,$$crsunum)= &LONCAPA::split_courseid($$courseid);
14562:     my $crsuhome=&Apache::lonnet::homeserver($$crsunum,$$crsudom);
14563:     if ($crsuhome eq 'no_host') {
14564:         $outcome .= &mt('Course creation failed, unrecognized course home server.').$linefeed;
14565:         return (0,$outcome);
14566:     }
14567:     $outcome .= &mt('Created on').': '.$crsuhome.$linefeed;
14568: 
14569: #
14570: # Do the cloning
14571: #   
14572:     if ($can_clone && $cloneid) {
14573: 	$clonemsg = &mt('Cloning [_1] from [_2]',$crstype,$clonehome);
14574: 	if ($context ne 'auto') {
14575: 	    $clonemsg = '<span class="LC_success">'.$clonemsg.'</span>';
14576: 	}
14577: 	$outcome .= $clonemsg.$linefeed;
14578: 	my %oldcenv=&Apache::lonnet::dump('environment',$$crsudom,$$crsunum);
14579: # Copy all files
14580: 	&Apache::lonclonecourse::copycoursefiles($cloneid,$$courseid,$args->{'datemode'},$args->{'dateshift'});
14581: # Restore URL
14582: 	$cenv{'url'}=$oldcenv{'url'};
14583: # Restore title
14584: 	$cenv{'description'}=$oldcenv{'description'};
14585: # Restore creation date, creator and creation context.
14586:         $cenv{'internal.created'}=$oldcenv{'internal.created'};
14587:         $cenv{'internal.creator'}=$oldcenv{'internal.creator'};
14588:         $cenv{'internal.creationcontext'}=$oldcenv{'internal.creationcontext'};
14589: # Mark as cloned
14590: 	$cenv{'clonedfrom'}=$cloneid;
14591: # Need to clone grading mode
14592:         my %newenv=&Apache::lonnet::get('environment',['grading'],$$crsudom,$$crsunum);
14593:         $cenv{'grading'}=$newenv{'grading'};
14594: # Do not clone these environment entries
14595:         &Apache::lonnet::del('environment',
14596:                   ['default_enrollment_start_date',
14597:                    'default_enrollment_end_date',
14598:                    'question.email',
14599:                    'policy.email',
14600:                    'comment.email',
14601:                    'pch.users.denied',
14602:                    'plc.users.denied',
14603:                    'hidefromcat',
14604:                    'checkforpriv',
14605:                    'categories',
14606:                    'internal.uniquecode'],
14607:                    $$crsudom,$$crsunum);
14608:         if ($args->{'textbook'}) {
14609:             $cenv{'internal.textbook'} = $args->{'textbook'};
14610:         }
14611:     }
14612: 
14613: #
14614: # Set environment (will override cloned, if existing)
14615: #
14616:     my @sections = ();
14617:     my @xlists = ();
14618:     if ($args->{'crstype'}) {
14619:         $cenv{'type'}=$args->{'crstype'};
14620:     }
14621:     if ($args->{'crsid'}) {
14622:         $cenv{'courseid'}=$args->{'crsid'};
14623:     }
14624:     if ($args->{'crscode'}) {
14625:         $cenv{'internal.coursecode'}=$args->{'crscode'};
14626:     }
14627:     if ($args->{'crsquota'} ne '') {
14628:         $cenv{'internal.coursequota'}=$args->{'crsquota'};
14629:     } else {
14630:         $cenv{'internal.coursequota'}=$args->{'crsquota'} = 20;
14631:     }
14632:     if ($args->{'ccuname'}) {
14633:         $cenv{'internal.courseowner'} = $args->{'ccuname'}.
14634:                                         ':'.$args->{'ccdomain'};
14635:     } else {
14636:         $cenv{'internal.courseowner'} = $args->{'curruser'};
14637:     }
14638:     if ($args->{'defaultcredits'}) {
14639:         $cenv{'internal.defaultcredits'} = $args->{'defaultcredits'};
14640:     }
14641:     my @badclasses = (); # Used to accumulate sections/crosslistings that did not pass classlist access check for course owner.
14642:     if ($args->{'crssections'}) {
14643:         $cenv{'internal.sectionnums'} = '';
14644:         if ($args->{'crssections'} =~ m/,/) {
14645:             @sections = split/,/,$args->{'crssections'};
14646:         } else {
14647:             $sections[0] = $args->{'crssections'};
14648:         }
14649:         if (@sections > 0) {
14650:             foreach my $item (@sections) {
14651:                 my ($sec,$gp) = split/:/,$item;
14652:                 my $class = $args->{'crscode'}.$sec;
14653:                 my $addcheck = &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$class,$cenv{'internal.courseowner'});
14654:                 $cenv{'internal.sectionnums'} .= $item.',';
14655:                 unless ($addcheck eq 'ok') {
14656:                     push @badclasses, $class;
14657:                 }
14658:             }
14659:             $cenv{'internal.sectionnums'} =~ s/,$//;
14660:         }
14661:     }
14662: # do not hide course coordinator from staff listing, 
14663: # even if privileged
14664:     $cenv{'nothideprivileged'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
14665: # add course coordinator's domain to domains to check for privileged users
14666: # if different to course domain
14667:     if ($$crsudom ne $args->{'ccdomain'}) {
14668:         $cenv{'checkforpriv'} = $args->{'ccdomain'};
14669:     }
14670: # add crosslistings
14671:     if ($args->{'crsxlist'}) {
14672:         $cenv{'internal.crosslistings'}='';
14673:         if ($args->{'crsxlist'} =~ m/,/) {
14674:             @xlists = split/,/,$args->{'crsxlist'};
14675:         } else {
14676:             $xlists[0] = $args->{'crsxlist'};
14677:         }
14678:         if (@xlists > 0) {
14679:             foreach my $item (@xlists) {
14680:                 my ($xl,$gp) = split/:/,$item;
14681:                 my $addcheck =  &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$xl,$cenv{'internal.courseowner'});
14682:                 $cenv{'internal.crosslistings'} .= $item.',';
14683:                 unless ($addcheck eq 'ok') {
14684:                     push @badclasses, $xl;
14685:                 }
14686:             }
14687:             $cenv{'internal.crosslistings'} =~ s/,$//;
14688:         }
14689:     }
14690:     if ($args->{'autoadds'}) {
14691:         $cenv{'internal.autoadds'}=$args->{'autoadds'};
14692:     }
14693:     if ($args->{'autodrops'}) {
14694:         $cenv{'internal.autodrops'}=$args->{'autodrops'};
14695:     }
14696: # check for notification of enrollment changes
14697:     my @notified = ();
14698:     if ($args->{'notify_owner'}) {
14699:         if ($args->{'ccuname'} ne '') {
14700:             push(@notified,$args->{'ccuname'}.':'.$args->{'ccdomain'});
14701:         }
14702:     }
14703:     if ($args->{'notify_dc'}) {
14704:         if ($uname ne '') { 
14705:             push(@notified,$uname.':'.$udom);
14706:         }
14707:     }
14708:     if (@notified > 0) {
14709:         my $notifylist;
14710:         if (@notified > 1) {
14711:             $notifylist = join(',',@notified);
14712:         } else {
14713:             $notifylist = $notified[0];
14714:         }
14715:         $cenv{'internal.notifylist'} = $notifylist;
14716:     }
14717:     if (@badclasses > 0) {
14718:         my %lt=&Apache::lonlocal::texthash(
14719:                 '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',
14720:                 'dnhr' => 'does not have rights to access enrollment in these classes',
14721:                 'adby' => 'as determined by the policies of your institution on access to official classlists'
14722:         );
14723:         my $badclass_msg = $cenv{'internal.courseowner'}.') - '.$lt{'dnhr'}.
14724:                            ' ('.$lt{'adby'}.')';
14725:         if ($context eq 'auto') {
14726:             $outcome .= $badclass_msg.$linefeed;
14727:             $outcome .= '<div class="LC_warning">'.$badclass_msg.$linefeed.'<ul>'."\n";
14728:             foreach my $item (@badclasses) {
14729:                 if ($context eq 'auto') {
14730:                     $outcome .= " - $item\n";
14731:                 } else {
14732:                     $outcome .= "<li>$item</li>\n";
14733:                 }
14734:             }
14735:             if ($context eq 'auto') {
14736:                 $outcome .= $linefeed;
14737:             } else {
14738:                 $outcome .= "</ul><br /><br /></div>\n";
14739:             }
14740:         } 
14741:     }
14742:     if ($args->{'no_end_date'}) {
14743:         $args->{'endaccess'} = 0;
14744:     }
14745:     $cenv{'internal.autostart'}=$args->{'enrollstart'};
14746:     $cenv{'internal.autoend'}=$args->{'enrollend'};
14747:     $cenv{'default_enrollment_start_date'}=$args->{'startaccess'};
14748:     $cenv{'default_enrollment_end_date'}=$args->{'endaccess'};
14749:     if ($args->{'showphotos'}) {
14750:       $cenv{'internal.showphotos'}=$args->{'showphotos'};
14751:     }
14752:     $cenv{'internal.authtype'} = $args->{'authtype'};
14753:     $cenv{'internal.autharg'} = $args->{'autharg'}; 
14754:     if ( ($cenv{'internal.authtype'} =~ /^krb/) && ($cenv{'internal.autoadds'} == 1)) {
14755:         if (! defined($cenv{'internal.autharg'}) || $cenv{'internal.autharg'}  eq '') {
14756:             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'); 
14757:             if ($context eq 'auto') {
14758:                 $outcome .= $krb_msg;
14759:             } else {
14760:                 $outcome .= '<span class="LC_error">'.$krb_msg.'</span>';
14761:             }
14762:             $outcome .= $linefeed;
14763:         }
14764:     }
14765:     if (($args->{'ccdomain'}) && ($args->{'ccuname'})) {
14766:        if ($args->{'setpolicy'}) {
14767:            $cenv{'policy.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
14768:        }
14769:        if ($args->{'setcontent'}) {
14770:            $cenv{'question.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
14771:        }
14772:     }
14773:     if ($args->{'reshome'}) {
14774: 	$cenv{'reshome'}=$args->{'reshome'}.'/';
14775: 	$cenv{'reshome'}=~s/\/+$/\//;
14776:     }
14777: #
14778: # course has keyed access
14779: #
14780:     if ($args->{'setkeys'}) {
14781:        $cenv{'keyaccess'}='yes';
14782:     }
14783: # if specified, key authority is not course, but user
14784: # only active if keyaccess is yes
14785:     if ($args->{'keyauth'}) {
14786: 	my ($user,$domain) = split(':',$args->{'keyauth'});
14787: 	$user = &LONCAPA::clean_username($user);
14788: 	$domain = &LONCAPA::clean_username($domain);
14789: 	if ($user ne '' && $domain ne '') {
14790: 	    $cenv{'keyauth'}=$user.':'.$domain;
14791: 	}
14792:     }
14793: 
14794: #
14795: #  generate and store uniquecode (available to course requester), if course should have one.
14796: #
14797:     if ($args->{'uniquecode'}) {
14798:         my ($code,$error) = &make_unique_code($$crsudom,$$crsunum);
14799:         if ($code) {
14800:             $cenv{'internal.uniquecode'} = $code;
14801:             my %crsinfo =
14802:                 &Apache::lonnet::courseiddump($$crsudom,'.',1,'.','.',$$crsunum,undef,undef,'.');
14803:             if (ref($crsinfo{$$crsudom.'_'.$$crsunum}) eq 'HASH') {
14804:                 $crsinfo{$$crsudom.'_'.$$crsunum}{'uniquecode'} = $code;
14805:                 my $putres = &Apache::lonnet::courseidput($$crsudom,\%crsinfo,$crsuhome,'notime');
14806:             } 
14807:             if (ref($coderef)) {
14808:                 $$coderef = $code;
14809:             }
14810:         }
14811:     }
14812: 
14813:     if ($args->{'disresdis'}) {
14814:         $cenv{'pch.roles.denied'}='st';
14815:     }
14816:     if ($args->{'disablechat'}) {
14817:         $cenv{'plc.roles.denied'}='st';
14818:     }
14819: 
14820:     # Record we've not yet viewed the Course Initialization Helper for this 
14821:     # course
14822:     $cenv{'course.helper.not.run'} = 1;
14823:     #
14824:     # Use new Randomseed
14825:     #
14826:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
14827:     $cenv{'receiptalg'}=&Apache::lonnet::latest_receipt_algorithm_id();;
14828:     #
14829:     # The encryption code and receipt prefix for this course
14830:     #
14831:     $cenv{'internal.encseed'}=$Apache::lonnet::perlvar{'lonReceipt'}.$$.time.int(rand(9999));
14832:     $cenv{'internal.encpref'}=100+int(9*rand(99));
14833:     #
14834:     # By default, use standard grading
14835:     if (!defined($cenv{'grading'})) { $cenv{'grading'} = 'standard'; }
14836: 
14837:     $outcome .= $linefeed.&mt('Setting environment').': '.                 
14838:           &Apache::lonnet::put('environment',\%cenv,$$crsudom,$$crsunum).$linefeed;
14839: #
14840: # Open all assignments
14841: #
14842:     if ($args->{'openall'}) {
14843:        my $storeunder=$$crsudom.'_'.$$crsunum.'.0.opendate';
14844:        my %storecontent = ($storeunder         => time,
14845:                            $storeunder.'.type' => 'date_start');
14846:        
14847:        $outcome .= &mt('Opening all assignments').': '.&Apache::lonnet::cput
14848:                  ('resourcedata',\%storecontent,$$crsudom,$$crsunum).$linefeed;
14849:    }
14850: #
14851: # Set first page
14852: #
14853:     unless (($args->{'nonstandard'}) || ($args->{'firstres'} eq 'blank')
14854: 	    || ($cloneid)) {
14855: 	use LONCAPA::map;
14856: 	$outcome .= &mt('Setting first resource').': ';
14857: 
14858: 	my $map = '/uploaded/'.$$crsudom.'/'.$$crsunum.'/default.sequence';
14859:         my ($errtext,$fatal)=&LONCAPA::map::mapread($map);
14860: 
14861:         $outcome .= ($fatal?$errtext:'read ok').' - ';
14862:         my $title; my $url;
14863:         if ($args->{'firstres'} eq 'syl') {
14864: 	    $title=&mt('Syllabus');
14865:             $url='/public/'.$$crsudom.'/'.$$crsunum.'/syllabus';
14866:         } else {
14867:             $title=&mt('Table of Contents');
14868:             $url='/adm/navmaps';
14869:         }
14870: 
14871:         $LONCAPA::map::resources[1]=$title.':'.$url.':false:start:res';
14872: 	(my $outtext,$errtext) = &LONCAPA::map::storemap($map,1);
14873: 
14874: 	if ($errtext) { $fatal=2; }
14875:         $outcome .= ($fatal?$errtext:'write ok').$linefeed;
14876:     }
14877: 
14878:     return (1,$outcome);
14879: }
14880: 
14881: sub make_unique_code {
14882:     my ($cdom,$cnum) = @_;
14883:     # get lock on uniquecodes db
14884:     my $lockhash = {
14885:                       $cnum."\0".'uniquecodes' => $env{'user.name'}.
14886:                                                   ':'.$env{'user.domain'},
14887:                    };
14888:     my $tries = 0;
14889:     my $gotlock = &Apache::lonnet::newput_dom('uniquecodes',$lockhash,$cdom);
14890:     my ($code,$error);
14891:   
14892:     while (($gotlock ne 'ok') && ($tries<3)) {
14893:         $tries ++;
14894:         sleep 1;
14895:         $gotlock = &Apache::lonnet::newput_dom('uniquecodes',$lockhash,$cdom);
14896:     }
14897:     if ($gotlock eq 'ok') {
14898:         my %currcodes = &Apache::lonnet::dump_dom('uniquecodes',$cdom);
14899:         my $gotcode;
14900:         my $attempts = 0;
14901:         while ((!$gotcode) && ($attempts < 100)) {
14902:             $code = &generate_code();
14903:             if (!exists($currcodes{$code})) {
14904:                 $gotcode = 1;
14905:                 unless (&Apache::lonnet::newput_dom('uniquecodes',{ $code => $cnum },$cdom) eq 'ok') {
14906:                     $error = 'nostore';
14907:                 }
14908:             }
14909:             $attempts ++;
14910:         }
14911:         my @del_lock = ($cnum."\0".'uniquecodes');
14912:         my $dellockoutcome = &Apache::lonnet::del_dom('uniquecodes',\@del_lock,$cdom);
14913:     } else {
14914:         $error = 'nolock';
14915:     }
14916:     return ($code,$error);
14917: }
14918: 
14919: sub generate_code {
14920:     my $code;
14921:     my @letts = qw(B C D G H J K M N P Q R S T V W X Z);
14922:     for (my $i=0; $i<6; $i++) {
14923:         my $lettnum = int (rand 2);
14924:         my $item = '';
14925:         if ($lettnum) {
14926:             $item = $letts[int( rand(18) )];
14927:         } else {
14928:             $item = 1+int( rand(8) );
14929:         }
14930:         $code .= $item;
14931:     }
14932:     return $code;
14933: }
14934: 
14935: ############################################################
14936: ############################################################
14937: 
14938: #SD
14939: # only Community and Course, or anything else?
14940: sub course_type {
14941:     my ($cid) = @_;
14942:     if (!defined($cid)) {
14943:         $cid = $env{'request.course.id'};
14944:     }
14945:     if (defined($env{'course.'.$cid.'.type'})) {
14946:         return $env{'course.'.$cid.'.type'};
14947:     } else {
14948:         return 'Course';
14949:     }
14950: }
14951: 
14952: sub group_term {
14953:     my $crstype = &course_type();
14954:     my %names = (
14955:                   'Course' => 'group',
14956:                   'Community' => 'group',
14957:                 );
14958:     return $names{$crstype};
14959: }
14960: 
14961: sub course_types {
14962:     my @types = ('official','unofficial','community','textbook');
14963:     my %typename = (
14964:                          official   => 'Official course',
14965:                          unofficial => 'Unofficial course',
14966:                          community  => 'Community',
14967:                          textbook   => 'Textbook course',
14968:                    );
14969:     return (\@types,\%typename);
14970: }
14971: 
14972: sub icon {
14973:     my ($file)=@_;
14974:     my $curfext = lc((split(/\./,$file))[-1]);
14975:     my $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/unknown.gif';
14976:     my $embstyle = &Apache::loncommon::fileembstyle($curfext);
14977:     if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) {
14978: 	if (-e  $Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
14979: 	          $Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
14980: 	            $curfext.".gif") {
14981: 	    $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
14982: 		$curfext.".gif";
14983: 	}
14984:     }
14985:     return &lonhttpdurl($iconname);
14986: } 
14987: 
14988: sub lonhttpdurl {
14989: #
14990: # Had been used for "small fry" static images on separate port 8080.
14991: # Modify here if lightweight http functionality desired again.
14992: # Currently eliminated due to increasing firewall issues.
14993: #
14994:     my ($url)=@_;
14995:     return $url;
14996: }
14997: 
14998: sub connection_aborted {
14999:     my ($r)=@_;
15000:     $r->print(" ");$r->rflush();
15001:     my $c = $r->connection;
15002:     return $c->aborted();
15003: }
15004: 
15005: #    Escapes strings that may have embedded 's that will be put into
15006: #    strings as 'strings'.
15007: sub escape_single {
15008:     my ($input) = @_;
15009:     $input =~ s/\\/\\\\/g;	# Escape the \'s..(must be first)>
15010:     $input =~ s/\'/\\\'/g;	# Esacpe the 's....
15011:     return $input;
15012: }
15013: 
15014: #  Same as escape_single, but escape's "'s  This 
15015: #  can be used for  "strings"
15016: sub escape_double {
15017:     my ($input) = @_;
15018:     $input =~ s/\\/\\\\/g;	# Escape the /'s..(must be first)>
15019:     $input =~ s/\"/\\\"/g;	# Esacpe the "s....
15020:     return $input;
15021: }
15022:  
15023: #   Escapes the last element of a full URL.
15024: sub escape_url {
15025:     my ($url)   = @_;
15026:     my @urlslices = split(/\//, $url,-1);
15027:     my $lastitem = &escape(pop(@urlslices));
15028:     return &HTML::Entities::encode(join('/',@urlslices),"'").'/'.$lastitem;
15029: }
15030: 
15031: sub compare_arrays {
15032:     my ($arrayref1,$arrayref2) = @_;
15033:     my (@difference,%count);
15034:     @difference = ();
15035:     %count = ();
15036:     if ((ref($arrayref1) eq 'ARRAY') && (ref($arrayref2) eq 'ARRAY')) {
15037:         foreach my $element (@{$arrayref1}, @{$arrayref2}) { $count{$element}++; }
15038:         foreach my $element (keys(%count)) {
15039:             if ($count{$element} == 1) {
15040:                 push(@difference,$element);
15041:             }
15042:         }
15043:     }
15044:     return @difference;
15045: }
15046: 
15047: # -------------------------------------------------------- Initialize user login
15048: sub init_user_environment {
15049:     my ($r, $username, $domain, $authhost, $form, $args) = @_;
15050:     my $lonids=$Apache::lonnet::perlvar{'lonIDsDir'};
15051: 
15052:     my $public=($username eq 'public' && $domain eq 'public');
15053: 
15054: # See if old ID present, if so, remove
15055: 
15056:     my ($filename,$cookie,$userroles,$firstaccenv,$timerintenv);
15057:     my $now=time;
15058: 
15059:     if ($public) {
15060: 	my $max_public=100;
15061: 	my $oldest;
15062: 	my $oldest_time=0;
15063: 	for(my $next=1;$next<=$max_public;$next++) {
15064: 	    if (-e $lonids."/publicuser_$next.id") {
15065: 		my $mtime=(stat($lonids."/publicuser_$next.id"))[9];
15066: 		if ($mtime<$oldest_time || !$oldest_time) {
15067: 		    $oldest_time=$mtime;
15068: 		    $oldest=$next;
15069: 		}
15070: 	    } else {
15071: 		$cookie="publicuser_$next";
15072: 		last;
15073: 	    }
15074: 	}
15075: 	if (!$cookie) { $cookie="publicuser_$oldest"; }
15076:     } else {
15077: 	# if this isn't a robot, kill any existing non-robot sessions
15078: 	if (!$args->{'robot'}) {
15079: 	    opendir(DIR,$lonids);
15080: 	    while ($filename=readdir(DIR)) {
15081: 		if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
15082: 		    unlink($lonids.'/'.$filename);
15083: 		}
15084: 	    }
15085: 	    closedir(DIR);
15086: # If there is a undeleted lockfile for the user's paste buffer remove it.
15087:             my $namespace = 'nohist_courseeditor';
15088:             my $lockingkey = 'paste'."\0".'locked_num';
15089:             my %lockhash = &Apache::lonnet::get($namespace,[$lockingkey],
15090:                                                 $domain,$username);
15091:             if (exists($lockhash{$lockingkey})) {
15092:                 my $delresult = &Apache::lonnet::del($namespace,[$lockingkey],$domain,$username);
15093:                 unless ($delresult eq 'ok') {
15094:                     &Apache::lonnet::logthis("Failed to delete paste buffer locking key in $namespace for ".$username.":".$domain." Result was: $delresult");
15095:                 }
15096:             }
15097: 	}
15098: # Give them a new cookie
15099: 	my $id = ($args->{'robot'} ? 'robot'.$args->{'robot'}
15100: 		                   : $now.$$.int(rand(10000)));
15101: 	$cookie="$username\_$id\_$domain\_$authhost";
15102:     
15103: # Initialize roles
15104: 
15105: 	($userroles,$firstaccenv,$timerintenv) = 
15106:             &Apache::lonnet::rolesinit($domain,$username,$authhost);
15107:     }
15108: # ------------------------------------ Check browser type and MathML capability
15109: 
15110:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,$clientunicode,
15111:         $clientos,$clientmobile,$clientinfo,$clientosversion) = &decode_user_agent($r);
15112: 
15113: # ------------------------------------------------------------- Get environment
15114: 
15115:     my %userenv = &Apache::lonnet::dump('environment',$domain,$username);
15116:     my ($tmp) = keys(%userenv);
15117:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
15118:     } else {
15119: 	undef(%userenv);
15120:     }
15121:     if (($userenv{'interface'}) && (!$form->{'interface'})) {
15122: 	$form->{'interface'}=$userenv{'interface'};
15123:     }
15124:     if ($userenv{'texengine'} eq 'ttm') { $clientmathml=1; }
15125: 
15126: # --------------- Do not trust query string to be put directly into environment
15127:     foreach my $option ('interface','localpath','localres') {
15128:         $form->{$option}=~s/[\n\r\=]//gs;
15129:     }
15130: # --------------------------------------------------------- Write first profile
15131: 
15132:     {
15133: 	my %initial_env = 
15134: 	    ("user.name"          => $username,
15135: 	     "user.domain"        => $domain,
15136: 	     "user.home"          => $authhost,
15137: 	     "browser.type"       => $clientbrowser,
15138: 	     "browser.version"    => $clientversion,
15139: 	     "browser.mathml"     => $clientmathml,
15140: 	     "browser.unicode"    => $clientunicode,
15141: 	     "browser.os"         => $clientos,
15142:              "browser.mobile"     => $clientmobile,
15143:              "browser.info"       => $clientinfo,
15144:              "browser.osversion"  => $clientosversion,
15145: 	     "server.domain"      => $Apache::lonnet::perlvar{'lonDefDomain'},
15146: 	     "request.course.fn"  => '',
15147: 	     "request.course.uri" => '',
15148: 	     "request.course.sec" => '',
15149: 	     "request.role"       => 'cm',
15150: 	     "request.role.adv"   => $env{'user.adv'},
15151: 	     "request.host"       => $ENV{'REMOTE_ADDR'},);
15152: 
15153:         if ($form->{'localpath'}) {
15154: 	    $initial_env{"browser.localpath"}  = $form->{'localpath'};
15155: 	    $initial_env{"browser.localres"}   = $form->{'localres'};
15156:         }
15157: 	
15158: 	if ($form->{'interface'}) {
15159: 	    $form->{'interface'}=~s/\W//gs;
15160: 	    $initial_env{"browser.interface"} = $form->{'interface'};
15161: 	    $env{'browser.interface'}=$form->{'interface'};
15162: 	}
15163: 
15164:         if ($form->{'iptoken'}) {
15165:             my $lonhost = $r->dir_config('lonHostID');
15166:             $initial_env{"user.noloadbalance"} = $lonhost;
15167:             $env{'user.noloadbalance'} = $lonhost;
15168:         }
15169: 
15170:         my %is_adv = ( is_adv => $env{'user.adv'} );
15171:         my %domdef;
15172:         unless ($domain eq 'public') {
15173:             %domdef = &Apache::lonnet::get_domain_defaults($domain);
15174:         }
15175: 
15176:         foreach my $tool ('aboutme','blog','webdav','portfolio') {
15177:             $userenv{'availabletools.'.$tool} = 
15178:                 &Apache::lonnet::usertools_access($username,$domain,$tool,'reload',
15179:                                                   undef,\%userenv,\%domdef,\%is_adv);
15180:         }
15181: 
15182:         foreach my $crstype ('official','unofficial','community','textbook') {
15183:             $userenv{'canrequest.'.$crstype} =
15184:                 &Apache::lonnet::usertools_access($username,$domain,$crstype,
15185:                                                   'reload','requestcourses',
15186:                                                   \%userenv,\%domdef,\%is_adv);
15187:         }
15188: 
15189:         $userenv{'canrequest.author'} =
15190:             &Apache::lonnet::usertools_access($username,$domain,'requestauthor',
15191:                                         'reload','requestauthor',
15192:                                         \%userenv,\%domdef,\%is_adv);
15193:         my %reqauthor = &Apache::lonnet::get('requestauthor',['author_status','author'],
15194:                                              $domain,$username);
15195:         my $reqstatus = $reqauthor{'author_status'};
15196:         if ($reqstatus eq 'approval' || $reqstatus eq 'approved') { 
15197:             if (ref($reqauthor{'author'}) eq 'HASH') {
15198:                 $userenv{'requestauthorqueued'} = $reqstatus.':'.
15199:                                                   $reqauthor{'author'}{'timestamp'};
15200:             }
15201:         }
15202: 
15203: 	$env{'user.environment'} = "$lonids/$cookie.id";
15204: 
15205: 	if (tie(my %disk_env,'GDBM_File',"$lonids/$cookie.id",
15206: 		 &GDBM_WRCREAT(),0640)) {
15207: 	    &_add_to_env(\%disk_env,\%initial_env);
15208: 	    &_add_to_env(\%disk_env,\%userenv,'environment.');
15209: 	    &_add_to_env(\%disk_env,$userroles);
15210:             if (ref($firstaccenv) eq 'HASH') {
15211:                 &_add_to_env(\%disk_env,$firstaccenv);
15212:             }
15213:             if (ref($timerintenv) eq 'HASH') {
15214:                 &_add_to_env(\%disk_env,$timerintenv);
15215:             }
15216: 	    if (ref($args->{'extra_env'})) {
15217: 		&_add_to_env(\%disk_env,$args->{'extra_env'});
15218: 	    }
15219: 	    untie(%disk_env);
15220: 	} else {
15221: 	    &Apache::lonnet::logthis("<span style=\"color:blue;\">WARNING: ".
15222: 			   'Could not create environment storage in lonauth: '.$!.'</span>');
15223: 	    return 'error: '.$!;
15224: 	}
15225:     }
15226:     $env{'request.role'}='cm';
15227:     $env{'request.role.adv'}=$env{'user.adv'};
15228:     $env{'browser.type'}=$clientbrowser;
15229: 
15230:     return $cookie;
15231: 
15232: }
15233: 
15234: sub _add_to_env {
15235:     my ($idf,$env_data,$prefix) = @_;
15236:     if (ref($env_data) eq 'HASH') {
15237:         while (my ($key,$value) = each(%$env_data)) {
15238: 	    $idf->{$prefix.$key} = $value;
15239: 	    $env{$prefix.$key}   = $value;
15240:         }
15241:     }
15242: }
15243: 
15244: # --- Get the symbolic name of a problem and the url
15245: sub get_symb {
15246:     my ($request,$silent) = @_;
15247:     (my $url=$env{'form.url'}) =~ s-^https?\://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
15248:     my $symb=($env{'form.symb'} ne '' ? $env{'form.symb'} : (&Apache::lonnet::symbread($url)));
15249:     if ($symb eq '') {
15250:         if (!$silent) {
15251:             if (ref($request)) { 
15252:                 $request->print("Unable to handle ambiguous references:$url:.");
15253:             }
15254:             return ();
15255:         }
15256:     }
15257:     &Apache::lonenc::check_decrypt(\$symb);
15258:     return ($symb);
15259: }
15260: 
15261: # --------------------------------------------------------------Get annotation
15262: 
15263: sub get_annotation {
15264:     my ($symb,$enc) = @_;
15265: 
15266:     my $key = $symb;
15267:     if (!$enc) {
15268:         $key =
15269:             &Apache::lonnet::clutter((&Apache::lonnet::decode_symb($symb))[2]);
15270:     }
15271:     my %annotation=&Apache::lonnet::get('nohist_annotations',[$key]);
15272:     return $annotation{$key};
15273: }
15274: 
15275: sub clean_symb {
15276:     my ($symb,$delete_enc) = @_;
15277: 
15278:     &Apache::lonenc::check_decrypt(\$symb);
15279:     my $enc = $env{'request.enc'};
15280:     if ($delete_enc) {
15281:         delete($env{'request.enc'});
15282:     }
15283: 
15284:     return ($symb,$enc);
15285: }
15286: 
15287: ############################################################
15288: ############################################################
15289: 
15290: =pod
15291: 
15292: =head1 Routines for building display used to search for courses
15293: 
15294: 
15295: =over 4
15296: 
15297: =item * &build_filters()
15298: 
15299: Create markup for a table used to set filters to use when selecting
15300: courses in a domain.  Used by lonpickcourse.pm, lonmodifycourse.pm
15301: and quotacheck.pl
15302: 
15303: 
15304: Inputs:
15305: 
15306: filterlist - anonymous array of fields to include as potential filters 
15307: 
15308: crstype - course type
15309: 
15310: roleelement - fifth arg in selectcourse_link() populates fifth arg in javascript: opencrsbrowser() function, used
15311:               to pop-open a course selector (will contain "extra element"). 
15312: 
15313: multelement - if multiple course selections will be allowed, this will be a hidden form element: name: multiple; value: 1
15314: 
15315: filter - anonymous hash of criteria and their values
15316: 
15317: action - form action
15318: 
15319: numfiltersref - ref to scalar (count of number of elements in institutional codes -- e.g., 4 for year, semester, department, and number)
15320: 
15321: caller - caller context (e.g., set to 'modifycourse' when routine is called from lonmodifycourse.pm)
15322: 
15323: cloneruname - username of owner of new course who wants to clone
15324: 
15325: clonerudom - domain of owner of new course who wants to clone
15326: 
15327: typeelem - text to use for left column in row containing course type (i.e., Course, Community or Course/Community) 
15328: 
15329: codetitlesref - reference to array of titles of components in institutional codes (official courses)
15330: 
15331: codedom - domain
15332: 
15333: formname - value of form element named "form". 
15334: 
15335: fixeddom - domain, if fixed.
15336: 
15337: prevphase - value to assign to form element named "phase" when going back to the previous screen  
15338: 
15339: cnameelement - name of form element in form on opener page which will receive title of selected course 
15340: 
15341: cnumelement - name of form element in form on opener page which will receive courseID  of selected course
15342: 
15343: cdomelement - name of form element in form on opener page which will receive domain of selected course
15344: 
15345: setroles - includes access constraint identifier when setting a roles-based condition for acces to a portfolio file
15346: 
15347: clonetext - hidden form elements containing list of courses cloneable by intended course owner when DC creates a course
15348: 
15349: clonewarning - warning message about missing information for intended course owner when DC creates a course
15350: 
15351: 
15352: Returns: $output - HTML for display of search criteria, and hidden form elements.
15353: 
15354: 
15355: Side Effects: None
15356: 
15357: =cut
15358: 
15359: # ---------------------------------------------- search for courses based on last activity etc.
15360: 
15361: sub build_filters {
15362:     my ($filterlist,$crstype,$roleelement,$multelement,$filter,$action,
15363:         $numtitlesref,$caller,$cloneruname,$clonerudom,$typeelement,
15364:         $codetitlesref,$codedom,$formname,$fixeddom,$prevphase,
15365:         $cnameelement,$cnumelement,$cdomelement,$setroles,
15366:         $clonetext,$clonewarning) = @_;
15367:     my ($list,$jscript);
15368:     my $onchange = 'javascript:updateFilters(this)';
15369:     my ($domainselectform,$sincefilterform,$createdfilterform,
15370:         $ownerdomselectform,$persondomselectform,$instcodeform,
15371:         $typeselectform,$instcodetitle);
15372:     if ($formname eq '') {
15373:         $formname = $caller;
15374:     }
15375:     foreach my $item (@{$filterlist}) {
15376:         unless (($item eq 'descriptfilter') || ($item eq 'instcodefilter') ||
15377:                 ($item eq 'sincefilter') || ($item eq 'createdfilter')) {
15378:             if ($item eq 'domainfilter') {
15379:                 $filter->{$item} = &LONCAPA::clean_domain($filter->{$item});
15380:             } elsif ($item eq 'coursefilter') {
15381:                 $filter->{$item} = &LONCAPA::clean_courseid($filter->{$item});
15382:             } elsif ($item eq 'ownerfilter') {
15383:                 $filter->{$item} = &LONCAPA::clean_username($filter->{$item});
15384:             } elsif ($item eq 'ownerdomfilter') {
15385:                 $filter->{'ownerdomfilter'} =
15386:                     &LONCAPA::clean_domain($filter->{$item});
15387:                 $ownerdomselectform = &select_dom_form($filter->{'ownerdomfilter'},
15388:                                                        'ownerdomfilter',1);
15389:             } elsif ($item eq 'personfilter') {
15390:                 $filter->{$item} = &LONCAPA::clean_username($filter->{$item});
15391:             } elsif ($item eq 'persondomfilter') {
15392:                 $persondomselectform = &select_dom_form($filter->{'persondomfilter'},
15393:                                                         'persondomfilter',1);
15394:             } else {
15395:                 $filter->{$item} =~ s/\W//g;
15396:             }
15397:             if (!$filter->{$item}) {
15398:                 $filter->{$item} = '';
15399:             }
15400:         }
15401:         if ($item eq 'domainfilter') {
15402:             my $allow_blank = 1;
15403:             if ($formname eq 'portform') {
15404:                 $allow_blank=0;
15405:             } elsif ($formname eq 'studentform') {
15406:                 $allow_blank=0;
15407:             }
15408:             if ($fixeddom) {
15409:                 $domainselectform = '<input type="hidden" name="domainfilter"'.
15410:                                     ' value="'.$codedom.'" />'.
15411:                                     &Apache::lonnet::domain($codedom,'description');
15412:             } else {
15413:                 $domainselectform = &select_dom_form($filter->{$item},
15414:                                                      'domainfilter',
15415:                                                       $allow_blank,'',$onchange);
15416:             }
15417:         } else {
15418:             $list->{$item} = &HTML::Entities::encode($filter->{$item},'<>&"');
15419:         }
15420:     }
15421: 
15422:     # last course activity filter and selection
15423:     $sincefilterform = &timebased_select_form('sincefilter',$filter);
15424: 
15425:     # course created filter and selection
15426:     if (exists($filter->{'createdfilter'})) {
15427:         $createdfilterform = &timebased_select_form('createdfilter',$filter);
15428:     }
15429: 
15430:     my %lt = &Apache::lonlocal::texthash(
15431:                 'cac' => "$crstype Activity",
15432:                 'ccr' => "$crstype Created",
15433:                 'cde' => "$crstype Title",
15434:                 'cdo' => "$crstype Domain",
15435:                 'ins' => 'Institutional Code',
15436:                 'inc' => 'Institutional Categorization',
15437:                 'cow' => "$crstype Owner/Co-owner",
15438:                 'cop' => "$crstype Personnel Includes",
15439:                 'cog' => 'Type',
15440:              );
15441: 
15442:     if (($formname eq 'ccrs') || ($formname eq 'requestcrs')) {
15443:         my $typeval = 'Course';
15444:         if ($crstype eq 'Community') {
15445:             $typeval = 'Community';
15446:         }
15447:         $typeselectform = '<input type="hidden" name="type" value="'.$typeval.'" />';
15448:     } else {
15449:         $typeselectform =  '<select name="type" size="1"';
15450:         if ($onchange) {
15451:             $typeselectform .= ' onchange="'.$onchange.'"';
15452:         }
15453:         $typeselectform .= '>'."\n";
15454:         foreach my $posstype ('Course','Community') {
15455:             $typeselectform.='<option value="'.$posstype.'"'.
15456:                 ($posstype eq $crstype ? ' selected="selected" ' : ''). ">".&mt($posstype)."</option>\n";
15457:         }
15458:         $typeselectform.="</select>";
15459:     }
15460: 
15461:     my ($cloneableonlyform,$cloneabletitle);
15462:     if (exists($filter->{'cloneableonly'})) {
15463:         my $cloneableon = '';
15464:         my $cloneableoff = ' checked="checked"';
15465:         if ($filter->{'cloneableonly'}) {
15466:             $cloneableon = $cloneableoff;
15467:             $cloneableoff = '';
15468:         }
15469:         $cloneableonlyform = '<span class="LC_nobreak"><label><input type="radio" name="cloneableonly" value="1" '.$cloneableon.'/>&nbsp;'.&mt('Required').'</label>'.('&nbsp;'x3).'<label><input type="radio" name="cloneableonly" value="" '.$cloneableoff.' />&nbsp;'.&mt('No restriction').'</label></span>';
15470:         if ($formname eq 'ccrs') {
15471:             $cloneabletitle = &mt('Cloneable for [_1]',$cloneruname.':'.$clonerudom);
15472:         } else {
15473:             $cloneabletitle = &mt('Cloneable by you');
15474:         }
15475:     }
15476:     my $officialjs;
15477:     if ($crstype eq 'Course') {
15478:         if (exists($filter->{'instcodefilter'})) {
15479: #            if (($fixeddom) || ($formname eq 'requestcrs') ||
15480: #                ($formname eq 'modifycourse') || ($formname eq 'filterpicker')) {
15481:             if ($codedom) { 
15482:                 $officialjs = 1;
15483:                 ($instcodeform,$jscript,$$numtitlesref) =
15484:                     &Apache::courseclassifier::instcode_selectors($codedom,'filterpicker',
15485:                                                                   $officialjs,$codetitlesref);
15486:                 if ($jscript) {
15487:                     $jscript = '<script type="text/javascript">'."\n".
15488:                                '// <![CDATA['."\n".
15489:                                $jscript."\n".
15490:                                '// ]]>'."\n".
15491:                                '</script>'."\n";
15492:                 }
15493:             }
15494:             if ($instcodeform eq '') {
15495:                 $instcodeform =
15496:                     '<input type="text" name="instcodefilter" size="10" value="'.
15497:                     $list->{'instcodefilter'}.'" />';
15498:                 $instcodetitle = $lt{'ins'};
15499:             } else {
15500:                 $instcodetitle = $lt{'inc'};
15501:             }
15502:             if ($fixeddom) {
15503:                 $instcodetitle .= '<br />('.$codedom.')';
15504:             }
15505:         }
15506:     }
15507:     my $output = qq|
15508: <form method="post" name="filterpicker" action="$action">
15509: <input type="hidden" name="form" value="$formname" />
15510: |;
15511:     if ($formname eq 'modifycourse') {
15512:         $output .= '<input type="hidden" name="phase" value="courselist" />'."\n".
15513:                    '<input type="hidden" name="prevphase" value="'.
15514:                    $prevphase.'" />'."\n";
15515:     } elsif ($formname eq 'quotacheck') {
15516:         $output .= qq|
15517: <input type="hidden" name="sortby" value="" />
15518: <input type="hidden" name="sortorder" value="" />
15519: |;
15520:     } else {
15521:         my $name_input;
15522:         if ($cnameelement ne '') {
15523:             $name_input = '<input type="hidden" name="cnameelement" value="'.
15524:                           $cnameelement.'" />';
15525:         }
15526:         $output .= qq|
15527: <input type="hidden" name="cnumelement" value="$cnumelement" />
15528: <input type="hidden" name="cdomelement" value="$cdomelement" />
15529: $name_input
15530: $roleelement
15531: $multelement
15532: $typeelement
15533: |;
15534:         if ($formname eq 'portform') {
15535:             $output .= '<input type="hidden" name="setroles" value="'.$setroles.'" />'."\n";
15536:         }
15537:     }
15538:     if ($fixeddom) {
15539:         $output .= '<input type="hidden" name="fixeddom" value="'.$fixeddom.'" />'."\n";
15540:     }
15541:     $output .= "<br />\n".&Apache::lonhtmlcommon::start_pick_box();
15542:     if ($sincefilterform) {
15543:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cac'})
15544:                   .$sincefilterform
15545:                   .&Apache::lonhtmlcommon::row_closure();
15546:     }
15547:     if ($createdfilterform) {
15548:         $output .= &Apache::lonhtmlcommon::row_title($lt{'ccr'})
15549:                   .$createdfilterform
15550:                   .&Apache::lonhtmlcommon::row_closure();
15551:     }
15552:     if ($domainselectform) {
15553:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cdo'})
15554:                   .$domainselectform
15555:                   .&Apache::lonhtmlcommon::row_closure();
15556:     }
15557:     if ($typeselectform) {
15558:         if (($formname eq 'ccrs') || ($formname eq 'requestcrs')) {
15559:             $output .= $typeselectform;
15560:         } else {
15561:             $output .= &Apache::lonhtmlcommon::row_title($lt{'cog'})
15562:                       .$typeselectform
15563:                       .&Apache::lonhtmlcommon::row_closure();
15564:         }
15565:     }
15566:     if ($instcodeform) {
15567:         $output .= &Apache::lonhtmlcommon::row_title($instcodetitle)
15568:                   .$instcodeform
15569:                   .&Apache::lonhtmlcommon::row_closure();
15570:     }
15571:     if (exists($filter->{'ownerfilter'})) {
15572:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cow'}).
15573:                    '<table><tr><td>'.&mt('Username').'<br />'.
15574:                    '<input type="text" name="ownerfilter" size="20" value="'.
15575:                    $list->{'ownerfilter'}.'" /></td><td>'.&mt('Domain').'<br />'.
15576:                    $ownerdomselectform.'</td></tr></table>'.
15577:                    &Apache::lonhtmlcommon::row_closure();
15578:     }
15579:     if (exists($filter->{'personfilter'})) {
15580:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cop'}).
15581:                    '<table><tr><td>'.&mt('Username').'<br />'.
15582:                    '<input type="text" name="personfilter" size="20" value="'.
15583:                    $list->{'personfilter'}.'" /></td><td>'.&mt('Domain').'<br />'.
15584:                    $persondomselectform.'</td></tr></table>'.
15585:                    &Apache::lonhtmlcommon::row_closure();
15586:     }
15587:     if (exists($filter->{'coursefilter'})) {
15588:         $output .= &Apache::lonhtmlcommon::row_title(&mt('LON-CAPA course ID'))
15589:                   .'<input type="text" name="coursefilter" size="25" value="'
15590:                   .$list->{'coursefilter'}.'" />'
15591:                   .&Apache::lonhtmlcommon::row_closure();
15592:     }
15593:     if ($cloneableonlyform) {
15594:         $output .= &Apache::lonhtmlcommon::row_title($cloneabletitle).
15595:                    $cloneableonlyform.&Apache::lonhtmlcommon::row_closure();
15596:     }
15597:     if (exists($filter->{'descriptfilter'})) {
15598:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cde'})
15599:                   .'<input type="text" name="descriptfilter" size="40" value="'
15600:                   .$list->{'descriptfilter'}.'" />'
15601:                   .&Apache::lonhtmlcommon::row_closure(1);
15602:     }
15603:     $output .= &Apache::lonhtmlcommon::end_pick_box().'<p>'.$clonetext."\n".
15604:                '<input type="hidden" name="updater" value="" />'."\n".
15605:                '<input type="submit" name="gosearch" value="'.
15606:                &mt('Search').'" /></p>'."\n".'</form>'."\n".'<hr />'."\n";
15607:     return $jscript.$clonewarning.$output;
15608: }
15609: 
15610: =pod 
15611: 
15612: =item * &timebased_select_form()
15613: 
15614: Create markup for a dropdown list used to select a time-based
15615: filter e.g., Course Activity, Course Created, when searching for courses
15616: or communities
15617: 
15618: Inputs:
15619: 
15620: item - name of form element (sincefilter or createdfilter)
15621: 
15622: filter - anonymous hash of criteria and their values
15623: 
15624: Returns: HTML for a select box contained a blank, then six time selections,
15625:          with value set in incoming form variables currently selected. 
15626: 
15627: Side Effects: None
15628: 
15629: =cut
15630: 
15631: sub timebased_select_form {
15632:     my ($item,$filter) = @_;
15633:     if (ref($filter) eq 'HASH') {
15634:         $filter->{$item} =~ s/[^\d-]//g;
15635:         if (!$filter->{$item}) { $filter->{$item}=-1; }
15636:         return &select_form(
15637:                             $filter->{$item},
15638:                             $item,
15639:                             {      '-1' => '',
15640:                                 '86400' => &mt('today'),
15641:                                '604800' => &mt('last week'),
15642:                               '2592000' => &mt('last month'),
15643:                               '7776000' => &mt('last three months'),
15644:                              '15552000' => &mt('last six months'),
15645:                              '31104000' => &mt('last year'),
15646:                     'select_form_order' =>
15647:                            ['-1','86400','604800','2592000','7776000',
15648:                             '15552000','31104000']});
15649:     }
15650: }
15651: 
15652: =pod
15653: 
15654: =item * &js_changer()
15655: 
15656: Create script tag containing Javascript used to submit course search form
15657: when course type or domain is changed, and also to hide 'Searching ...' on
15658: page load completion for page showing search result.
15659: 
15660: Inputs: None
15661: 
15662: Returns: markup containing updateFilters() and hideSearching() javascript functions. 
15663: 
15664: Side Effects: None
15665: 
15666: =cut
15667: 
15668: sub js_changer {
15669:     return <<ENDJS;
15670: <script type="text/javascript">
15671: // <![CDATA[
15672: function updateFilters(caller) {
15673:     if (typeof(caller) != "undefined") {
15674:         document.filterpicker.updater.value = caller.name;
15675:     }
15676:     document.filterpicker.submit();
15677: }
15678: 
15679: function hideSearching() {
15680:     if (document.getElementById('searching')) {
15681:         document.getElementById('searching').style.display = 'none';
15682:     }
15683:     return;
15684: }
15685: 
15686: // ]]>
15687: </script>
15688: 
15689: ENDJS
15690: }
15691: 
15692: =pod
15693: 
15694: =item * &search_courses()
15695: 
15696: Process selected filters form course search form and pass to lonnet::courseiddump
15697: to retrieve a hash for which keys are courseIDs which match the selected filters.
15698: 
15699: Inputs:
15700: 
15701: dom - domain being searched 
15702: 
15703: type - course type ('Course' or 'Community' or '.' if any).
15704: 
15705: filter - anonymous hash of criteria and their values
15706: 
15707: numtitles - for institutional codes - number of categories
15708: 
15709: cloneruname - optional username of new course owner
15710: 
15711: clonerudom - optional domain of new course owner
15712: 
15713: domcloner - optional "domcloner" flag; has value=1 if user has ccc priv in domain being filtered by, 
15714:             (used when DC is using course creation form)
15715: 
15716: codetitles - reference to array of titles of components in institutional codes (official courses).
15717: 
15718: cc_clone - escaped comma separated list of courses for which course cloner has active CC role
15719:            (and so can clone automatically)
15720: 
15721: reqcrsdom - domain of new course, where search_courses is used to identify potential courses to clone
15722: 
15723: reqinstcode - institutional code of new course, where search_courses is used to identify potential 
15724:               courses to clone 
15725: 
15726: Returns: %courses - hash of courses satisfying search criteria, keys = course IDs, values are corresponding colon-separated escaped description, institutional code, owner and type.
15727: 
15728: 
15729: Side Effects: None
15730: 
15731: =cut
15732: 
15733: 
15734: sub search_courses {
15735:     my ($dom,$type,$filter,$numtitles,$cloneruname,$clonerudom,$domcloner,$codetitles,
15736:         $cc_clone,$reqcrsdom,$reqinstcode) = @_;
15737:     my (%courses,%showcourses,$cloner);
15738:     if (($filter->{'ownerfilter'} ne '') ||
15739:         ($filter->{'ownerdomfilter'} ne '')) {
15740:         $filter->{'combownerfilter'} = $filter->{'ownerfilter'}.':'.
15741:                                        $filter->{'ownerdomfilter'};
15742:     }
15743:     foreach my $item ('descriptfilter','coursefilter','combownerfilter') {
15744:         if (!$filter->{$item}) {
15745:             $filter->{$item}='.';
15746:         }
15747:     }
15748:     my $now = time;
15749:     my $timefilter =
15750:        ($filter->{'sincefilter'}==-1?1:$now-$filter->{'sincefilter'});
15751:     my ($createdbefore,$createdafter);
15752:     if (($filter->{'createdfilter'} ne '') && ($filter->{'createdfilter'} !=-1)) {
15753:         $createdbefore = $now;
15754:         $createdafter = $now-$filter->{'createdfilter'};
15755:     }
15756:     my ($instcodefilter,$regexpok);
15757:     if ($numtitles) {
15758:         if ($env{'form.official'} eq 'on') {
15759:             $instcodefilter =
15760:                 &Apache::courseclassifier::instcode_search_str($dom,$numtitles,$codetitles);
15761:             $regexpok = 1;
15762:         } elsif ($env{'form.official'} eq 'off') {
15763:             $instcodefilter = &Apache::courseclassifier::instcode_search_str($dom,$numtitles,$codetitles);
15764:             unless ($instcodefilter eq '') {
15765:                 $regexpok = -1;
15766:             }
15767:         }
15768:     } else {
15769:         $instcodefilter = $filter->{'instcodefilter'};
15770:     }
15771:     if ($instcodefilter eq '') { $instcodefilter = '.'; }
15772:     if ($type eq '') { $type = '.'; }
15773: 
15774:     if (($clonerudom ne '') && ($cloneruname ne '')) {
15775:         $cloner = $cloneruname.':'.$clonerudom;
15776:     }
15777:     %courses = &Apache::lonnet::courseiddump($dom,
15778:                                              $filter->{'descriptfilter'},
15779:                                              $timefilter,
15780:                                              $instcodefilter,
15781:                                              $filter->{'combownerfilter'},
15782:                                              $filter->{'coursefilter'},
15783:                                              undef,undef,$type,$regexpok,undef,undef,
15784:                                              undef,undef,$cloner,$cc_clone,
15785:                                              $filter->{'cloneableonly'},
15786:                                              $createdbefore,$createdafter,undef,
15787:                                              $domcloner,undef,$reqcrsdom,$reqinstcode);
15788:     if (($filter->{'personfilter'} ne '') && ($filter->{'persondomfilter'} ne '')) {
15789:         my $ccrole;
15790:         if ($type eq 'Community') {
15791:             $ccrole = 'co';
15792:         } else {
15793:             $ccrole = 'cc';
15794:         }
15795:         my %rolehash = &Apache::lonnet::get_my_roles($filter->{'personfilter'},
15796:                                                      $filter->{'persondomfilter'},
15797:                                                      'userroles',undef,
15798:                                                      [$ccrole,'in','ad','ep','ta','cr'],
15799:                                                      $dom);
15800:         foreach my $role (keys(%rolehash)) {
15801:             my ($cnum,$cdom,$courserole) = split(':',$role);
15802:             my $cid = $cdom.'_'.$cnum;
15803:             if (exists($courses{$cid})) {
15804:                 if (ref($courses{$cid}) eq 'HASH') {
15805:                     if (ref($courses{$cid}{roles}) eq 'ARRAY') {
15806:                         if (!grep(/^\Q$courserole\E$/,@{$courses{$cid}{roles}})) {
15807:                             push (@{$courses{$cid}{roles}},$courserole);
15808:                         }
15809:                     } else {
15810:                         $courses{$cid}{roles} = [$courserole];
15811:                     }
15812:                     $showcourses{$cid} = $courses{$cid};
15813:                 }
15814:             }
15815:         }
15816:         %courses = %showcourses;
15817:     }
15818:     return %courses;
15819: }
15820: 
15821: =pod
15822: 
15823: =back
15824: 
15825: =head1 Routines for version requirements for current course.
15826: 
15827: =over 4
15828: 
15829: =item * &check_release_required()
15830: 
15831: Compares required LON-CAPA version with version on server, and
15832: if required version is newer looks for a server with the required version.
15833: 
15834: Looks first at servers in user's owen domain; if none suitable, looks at
15835: servers in course's domain are permitted to host sessions for user's domain.
15836: 
15837: Inputs:
15838: 
15839: $loncaparev - Version on current server (format: Major.Minor.Subrelease-datestamp)
15840: 
15841: $courseid - Course ID of current course
15842: 
15843: $rolecode - User's current role in course (for switchserver query string).
15844: 
15845: $required - LON-CAPA version needed by course (format: Major.Minor).
15846: 
15847: 
15848: Returns:
15849: 
15850: $switchserver - query string tp append to /adm/switchserver call (if 
15851:                 current server's LON-CAPA version is too old. 
15852: 
15853: $warning - Message is displayed if no suitable server could be found.
15854: 
15855: =cut
15856: 
15857: sub check_release_required {
15858:     my ($loncaparev,$courseid,$rolecode,$required) = @_;
15859:     my ($switchserver,$warning);
15860:     if ($required ne '') {
15861:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
15862:         my ($major,$minor) = ($loncaparev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
15863:         if ($reqdmajor ne '' && $reqdminor ne '') {
15864:             my $otherserver;
15865:             if (($major eq '' && $minor eq '') ||
15866:                 (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
15867:                 my ($userdomserver) = &Apache::lonnet::choose_server($env{'user.domain'},undef,$required,1);
15868:                 my $switchlcrev =
15869:                     &Apache::lonnet::get_server_loncaparev($env{'user.domain'},
15870:                                                            $userdomserver);
15871:                 my ($swmajor,$swminor) = ($switchlcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
15872:                 if (($swmajor eq '' && $swminor eq '') || ($reqdmajor > $swmajor) ||
15873:                     (($reqdmajor == $swmajor) && ($reqdminor > $swminor))) {
15874:                     my $cdom = $env{'course.'.$courseid.'.domain'};
15875:                     if ($cdom ne $env{'user.domain'}) {
15876:                         my ($coursedomserver,$coursehostname) = &Apache::lonnet::choose_server($cdom,undef,$required,1);
15877:                         my $serverhomeID = &Apache::lonnet::get_server_homeID($coursehostname);
15878:                         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
15879:                         my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
15880:                         my %udomdefaults = &Apache::lonnet::get_domain_defaults($env{'user.domain'});
15881:                         my $remoterev = &Apache::lonnet::get_server_loncaparev($serverhomedom,$coursedomserver);
15882:                         my $canhost =
15883:                             &Apache::lonnet::can_host_session($env{'user.domain'},
15884:                                                               $coursedomserver,
15885:                                                               $remoterev,
15886:                                                               $udomdefaults{'remotesessions'},
15887:                                                               $defdomdefaults{'hostedsessions'});
15888: 
15889:                         if ($canhost) {
15890:                             $otherserver = $coursedomserver;
15891:                         } else {
15892:                             $warning = &mt('Requires LON-CAPA version [_1].',$env{'course.'.$courseid.'.internal.releaserequired'}).'<br />'. &mt("No suitable server could be found amongst servers in either your own domain or in the course's domain.");
15893:                         }
15894:                     } else {
15895:                         $warning = &mt('Requires LON-CAPA version [_1].',$env{'course.'.$courseid.'.internal.releaserequired'}).'<br />'.&mt("No suitable server could be found amongst servers in your own domain (which is also the course's domain).");
15896:                     }
15897:                 } else {
15898:                     $otherserver = $userdomserver;
15899:                 }
15900:             }
15901:             if ($otherserver ne '') {
15902:                 $switchserver = 'otherserver='.$otherserver.'&amp;role='.$rolecode;
15903:             }
15904:         }
15905:     }
15906:     return ($switchserver,$warning);
15907: }
15908: 
15909: =pod
15910: 
15911: =item * &check_release_result()
15912: 
15913: Inputs:
15914: 
15915: $switchwarning - Warning message if no suitable server found to host session.
15916: 
15917: $switchserver - query string to append to /adm/switchserver containing lonHostID
15918:                 and current role.
15919: 
15920: Returns: HTML to display with information about requirement to switch server.
15921:          Either displaying warning with link to Roles/Courses screen or
15922:          display link to switchserver.
15923: 
15924: =cut
15925: 
15926: sub check_release_result {
15927:     my ($switchwarning,$switchserver) = @_;
15928:     my $output = &start_page('Selected course unavailable on this server').
15929:                  '<p class="LC_warning">';
15930:     if ($switchwarning) {
15931:         $output .= $switchwarning.'<br /><a href="/adm/roles">';
15932:         if (&show_course()) {
15933:             $output .= &mt('Display courses');
15934:         } else {
15935:             $output .= &mt('Display roles');
15936:         }
15937:         $output .= '</a>';
15938:     } elsif ($switchserver) {
15939:         $output .= &mt('This course requires a newer version of LON-CAPA than is installed on this server.').
15940:                    '<br />'.
15941:                    '<a href="/adm/switchserver?'.$switchserver.'">'.
15942:                    &mt('Switch Server').
15943:                    '</a>';
15944:     }
15945:     $output .= '</p>'.&end_page();
15946:     return $output;
15947: }
15948: 
15949: =pod
15950: 
15951: =item * &needs_coursereinit()
15952: 
15953: Determine if course contents stored for user's session needs to be
15954: refreshed, because content has changed since "Big Hash" last tied.
15955: 
15956: Check for change is made if time last checked is more than 10 minutes ago
15957: (by default).
15958: 
15959: Inputs:
15960: 
15961: $loncaparev - Version on current server (format: Major.Minor.Subrelease-datestamp)
15962: 
15963: $interval (optional) - Time which may elapse (in s) between last check for content
15964:                        change in current course. (default: 600 s).  
15965: 
15966: Returns: an array; first element is:
15967: 
15968: =over 4
15969: 
15970: 'switch' - if content updates mean user's session
15971:            needs to be switched to a server running a newer LON-CAPA version
15972:  
15973: 'update' - if course session needs to be refreshed (i.e., Big Hash needs to be reloaded)
15974:            on current server hosting user's session                
15975: 
15976: ''       - if no action required.
15977: 
15978: =back
15979: 
15980: If first item element is 'switch':
15981: 
15982: second item is $switchwarning - Warning message if no suitable server found to host session. 
15983: 
15984: third item is $switchserver - query string to append to /adm/switchserver containing lonHostID
15985:                               and current role. 
15986: 
15987: otherwise: no other elements returned.
15988: 
15989: =back
15990: 
15991: =cut
15992: 
15993: sub needs_coursereinit {
15994:     my ($loncaparev,$interval) = @_;
15995:     return() unless ($env{'request.course.id'} && $env{'request.course.tied'});
15996:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
15997:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
15998:     my $now = time;
15999:     if ($interval eq '') {
16000:         $interval = 600;
16001:     }
16002:     if (($now-$env{'request.course.timechecked'})>$interval) {
16003:         my $lastchange = &Apache::lonnet::get_coursechange($cdom,$cnum);
16004:         &Apache::lonnet::appenv({'request.course.timechecked'=>$now});
16005:         if ($lastchange > $env{'request.course.tied'}) {
16006:             my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired');
16007:             if ($curr_reqd_hash{'internal.releaserequired'} ne '') {
16008:                 my $required = $env{'course.'.$cdom.'_'.$cnum.'.internal.releaserequired'};
16009:                 if ($curr_reqd_hash{'internal.releaserequired'} ne $required) {
16010:                     &Apache::lonnet::appenv({'course.'.$cdom.'_'.$cnum.'.internal.releaserequired' =>
16011:                                              $curr_reqd_hash{'internal.releaserequired'}});
16012:                     my ($switchserver,$switchwarning) =
16013:                         &check_release_required($loncaparev,$cdom.'_'.$cnum,$env{'request.role'},
16014:                                                 $curr_reqd_hash{'internal.releaserequired'});
16015:                     if ($switchwarning ne '' || $switchserver ne '') {
16016:                         return ('switch',$switchwarning,$switchserver);
16017:                     }
16018:                 }
16019:             }
16020:             return ('update');
16021:         }
16022:     }
16023:     return ();
16024: }
16025: 
16026: sub update_content_constraints {
16027:     my ($cdom,$cnum,$chome,$cid) = @_;
16028:     my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired');
16029:     my ($reqdmajor,$reqdminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
16030:     my %checkresponsetypes;
16031:     foreach my $key (keys(%Apache::lonnet::needsrelease)) {
16032:         my ($item,$name,$value,$valmatch) = split(/:/,$key);
16033:         if ($item eq 'resourcetag') {
16034:             if ($name eq 'responsetype') {
16035:                 $checkresponsetypes{$value} = $Apache::lonnet::needsrelease{$key}
16036:             }
16037:         }
16038:     }
16039:     my $navmap = Apache::lonnavmaps::navmap->new();
16040:     if (defined($navmap)) {
16041:         my %allresponses;
16042:         foreach my $res ($navmap->retrieveResources(undef,sub { $_[0]->is_problem() },1,0)) {
16043:             my %responses = $res->responseTypes();
16044:             foreach my $key (keys(%responses)) {
16045:                 next unless(exists($checkresponsetypes{$key}));
16046:                 $allresponses{$key} += $responses{$key};
16047:             }
16048:         }
16049:         foreach my $key (keys(%allresponses)) {
16050:             my ($major,$minor) = split(/\./,$checkresponsetypes{$key});
16051:             if (($major > $reqdmajor) || ($major == $reqdmajor && $minor > $reqdminor)) {
16052:                 ($reqdmajor,$reqdminor) = ($major,$minor);
16053:             }
16054:         }
16055:         undef($navmap);
16056:     }
16057:     unless (($reqdmajor eq '') && ($reqdminor eq '')) {
16058:         &Apache::lonnet::update_released_required($reqdmajor.'.'.$reqdminor,$cdom,$cnum,$chome,$cid);
16059:     }
16060:     return;
16061: }
16062: 
16063: sub allmaps_incourse {
16064:     my ($cdom,$cnum,$chome,$cid) = @_;
16065:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
16066:         $cid = $env{'request.course.id'};
16067:         $cdom = $env{'course.'.$cid.'.domain'};
16068:         $cnum = $env{'course.'.$cid.'.num'};
16069:         $chome = $env{'course.'.$cid.'.home'};
16070:     }
16071:     my %allmaps = ();
16072:     my $lastchange =
16073:         &Apache::lonnet::get_coursechange($cdom,$cnum);
16074:     if ($lastchange > $env{'request.course.tied'}) {
16075:         my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
16076:         unless ($ferr) {
16077:             &update_content_constraints($cdom,$cnum,$chome,$cid);
16078:         }
16079:     }
16080:     my $navmap = Apache::lonnavmaps::navmap->new();
16081:     if (defined($navmap)) {
16082:         foreach my $res ($navmap->retrieveResources(undef,sub { $_[0]->is_map() },1,0,1)) {
16083:             $allmaps{$res->src()} = 1;
16084:         }
16085:     }
16086:     return \%allmaps;
16087: }
16088: 
16089: sub parse_supplemental_title {
16090:     my ($title) = @_;
16091: 
16092:     my ($foldertitle,$renametitle);
16093:     if ($title =~ /&amp;&amp;&amp;/) {
16094:         $title = &HTML::Entites::decode($title);
16095:     }
16096:     if ($title =~ m/^(\d+)___&&&___($match_username)___&&&___($match_domain)___&&&___(.*)$/) {
16097:         $renametitle=$4;
16098:         my ($time,$uname,$udom) = ($1,$2,$3);
16099:         $foldertitle=&Apache::lontexconvert::msgtexconverted($4);
16100:         my $name =  &plainname($uname,$udom);
16101:         $name = &HTML::Entities::encode($name,'"<>&\'');
16102:         $renametitle = &HTML::Entities::encode($renametitle,'"<>&\'');
16103:         $title='<i>'.&Apache::lonlocal::locallocaltime($time).'</i> '.
16104:             $name.': <br />'.$foldertitle;
16105:     }
16106:     if (wantarray) {
16107:         return ($title,$foldertitle,$renametitle);
16108:     }
16109:     return $title;
16110: }
16111: 
16112: sub recurse_supplemental {
16113:     my ($cnum,$cdom,$suppmap,$numfiles,$errors) = @_;
16114:     if ($suppmap) {
16115:         my ($errtext,$fatal) = &LONCAPA::map::mapread('/uploaded/'.$cdom.'/'.$cnum.'/'.$suppmap);
16116:         if ($fatal) {
16117:             $errors ++;
16118:         } else {
16119:             if ($#LONCAPA::map::resources > 0) {
16120:                 foreach my $res (@LONCAPA::map::resources) {
16121:                     my ($title,$src,$ext,$type,$status)=split(/\:/,$res);
16122:                     if (($src ne '') && ($status eq 'res')) {
16123:                         if ($src =~ m{^\Q/uploaded/$cdom/$cnum/\E(supplemental_\d+\.sequence)$}) {
16124:                             ($numfiles,$errors) = &recurse_supplemental($cnum,$cdom,$1,$numfiles,$errors);
16125:                         } else {
16126:                             $numfiles ++;
16127:                         }
16128:                     }
16129:                 }
16130:             }
16131:         }
16132:     }
16133:     return ($numfiles,$errors);
16134: }
16135: 
16136: sub symb_to_docspath {
16137:     my ($symb) = @_;
16138:     return unless ($symb);
16139:     my ($mapurl,$id,$resurl) = &Apache::lonnet::decode_symb($symb);
16140:     if ($resurl=~/\.(sequence|page)$/) {
16141:         $mapurl=$resurl;
16142:     } elsif ($resurl eq 'adm/navmaps') {
16143:         $mapurl=$env{'course.'.$env{'request.course.id'}.'.url'};
16144:     }
16145:     my $mapresobj;
16146:     my $navmap = Apache::lonnavmaps::navmap->new();
16147:     if (ref($navmap)) {
16148:         $mapresobj = $navmap->getResourceByUrl($mapurl);
16149:     }
16150:     $mapurl=~s{^.*/([^/]+)\.(\w+)$}{$1};
16151:     my $type=$2;
16152:     my $path;
16153:     if (ref($mapresobj)) {
16154:         my $pcslist = $mapresobj->map_hierarchy();
16155:         if ($pcslist ne '') {
16156:             foreach my $pc (split(/,/,$pcslist)) {
16157:                 next if ($pc <= 1);
16158:                 my $res = $navmap->getByMapPc($pc);
16159:                 if (ref($res)) {
16160:                     my $thisurl = $res->src();
16161:                     $thisurl=~s{^.*/([^/]+)\.\w+$}{$1};
16162:                     my $thistitle = $res->title();
16163:                     $path .= '&'.
16164:                              &Apache::lonhtmlcommon::entity_encode($thisurl).'&'.
16165:                              &escape($thistitle).
16166:                              ':'.$res->randompick().
16167:                              ':'.$res->randomout().
16168:                              ':'.$res->encrypted().
16169:                              ':'.$res->randomorder().
16170:                              ':'.$res->is_page();
16171:                 }
16172:             }
16173:         }
16174:         $path =~ s/^\&//;
16175:         my $maptitle = $mapresobj->title();
16176:         if ($mapurl eq 'default') {
16177:             $maptitle = 'Main Content';
16178:         }
16179:         $path .= (($path ne '')? '&' : '').
16180:                  &Apache::lonhtmlcommon::entity_encode($mapurl).'&'.
16181:                  &escape($maptitle).
16182:                  ':'.$mapresobj->randompick().
16183:                  ':'.$mapresobj->randomout().
16184:                  ':'.$mapresobj->encrypted().
16185:                  ':'.$mapresobj->randomorder().
16186:                  ':'.$mapresobj->is_page();
16187:     } else {
16188:         my $maptitle = &Apache::lonnet::gettitle($mapurl);
16189:         my $ispage = (($type eq 'page')? 1 : '');
16190:         if ($mapurl eq 'default') {
16191:             $maptitle = 'Main Content';
16192:         }
16193:         $path = &Apache::lonhtmlcommon::entity_encode($mapurl).'&'.
16194:                 &escape($maptitle).':::::'.$ispage;
16195:     }
16196:     unless ($mapurl eq 'default') {
16197:         $path = 'default&'.
16198:                 &escape('Main Content').
16199:                 ':::::&'.$path;
16200:     }
16201:     return $path;
16202: }
16203: 
16204: sub captcha_display {
16205:     my ($context,$lonhost) = @_;
16206:     my ($output,$error);
16207:     my ($captcha,$pubkey,$privkey) = &get_captcha_config($context,$lonhost);
16208:     if ($captcha eq 'original') {
16209:         $output = &create_captcha();
16210:         unless ($output) {
16211:             $error = 'captcha';
16212:         }
16213:     } elsif ($captcha eq 'recaptcha') {
16214:         $output = &create_recaptcha($pubkey);
16215:         unless ($output) {
16216:             $error = 'recaptcha';
16217:         }
16218:     }
16219:     return ($output,$error,$captcha);
16220: }
16221: 
16222: sub captcha_response {
16223:     my ($context,$lonhost) = @_;
16224:     my ($captcha_chk,$captcha_error);
16225:     my ($captcha,$pubkey,$privkey) = &get_captcha_config($context,$lonhost);
16226:     if ($captcha eq 'original') {
16227:         ($captcha_chk,$captcha_error) = &check_captcha();
16228:     } elsif ($captcha eq 'recaptcha') {
16229:         $captcha_chk = &check_recaptcha($privkey);
16230:     } else {
16231:         $captcha_chk = 1;
16232:     }
16233:     return ($captcha_chk,$captcha_error);
16234: }
16235: 
16236: sub get_captcha_config {
16237:     my ($context,$lonhost) = @_;
16238:     my ($captcha,$pubkey,$privkey,$hashtocheck);
16239:     my $hostname = &Apache::lonnet::hostname($lonhost);
16240:     my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
16241:     my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
16242:     if ($context eq 'usercreation') {
16243:         my %domconfig = &Apache::lonnet::get_dom('configuration',[$context],$serverhomedom);
16244:         if (ref($domconfig{$context}) eq 'HASH') {
16245:             $hashtocheck = $domconfig{$context}{'cancreate'};
16246:             if (ref($hashtocheck) eq 'HASH') {
16247:                 if ($hashtocheck->{'captcha'} eq 'recaptcha') {
16248:                     if (ref($hashtocheck->{'recaptchakeys'}) eq 'HASH') {
16249:                         $pubkey = $hashtocheck->{'recaptchakeys'}{'public'};
16250:                         $privkey = $hashtocheck->{'recaptchakeys'}{'private'};
16251:                     }
16252:                     if ($privkey && $pubkey) {
16253:                         $captcha = 'recaptcha';
16254:                     } else {
16255:                         $captcha = 'original';
16256:                     }
16257:                 } elsif ($hashtocheck->{'captcha'} ne 'notused') {
16258:                     $captcha = 'original';
16259:                 }
16260:             }
16261:         } else {
16262:             $captcha = 'captcha';
16263:         }
16264:     } elsif ($context eq 'login') {
16265:         my %domconfhash = &Apache::loncommon::get_domainconf($serverhomedom);
16266:         if ($domconfhash{$serverhomedom.'.login.captcha'} eq 'recaptcha') {
16267:             $pubkey = $domconfhash{$serverhomedom.'.login.recaptchakeys_public'};
16268:             $privkey = $domconfhash{$serverhomedom.'.login.recaptchakeys_private'};
16269:             if ($privkey && $pubkey) {
16270:                 $captcha = 'recaptcha';
16271:             } else {
16272:                 $captcha = 'original';
16273:             }
16274:         } elsif ($domconfhash{$serverhomedom.'.login.captcha'} eq 'original') {
16275:             $captcha = 'original';
16276:         }
16277:     }
16278:     return ($captcha,$pubkey,$privkey);
16279: }
16280: 
16281: sub create_captcha {
16282:     my %captcha_params = &captcha_settings();
16283:     my ($output,$maxtries,$tries) = ('',10,0);
16284:     while ($tries < $maxtries) {
16285:         $tries ++;
16286:         my $captcha = Authen::Captcha->new (
16287:                                            output_folder => $captcha_params{'output_dir'},
16288:                                            data_folder   => $captcha_params{'db_dir'},
16289:                                           );
16290:         my $md5sum = $captcha->generate_code($captcha_params{'numchars'});
16291: 
16292:         if (-e $Apache::lonnet::perlvar{'lonCaptchaDir'}.'/'.$md5sum.'.png') {
16293:             $output = '<input type="hidden" name="crypt" value="'.$md5sum.'" />'."\n".
16294:                       &mt('Type in the letters/numbers shown below').'&nbsp;'.
16295:                       '<input type="text" size="5" name="code" value="" autocomplete="off" />'.
16296:                       '<br />'.
16297:                       '<img src="'.$captcha_params{'www_output_dir'}.'/'.$md5sum.'.png" alt="captcha" />';
16298:             last;
16299:         }
16300:     }
16301:     return $output;
16302: }
16303: 
16304: sub captcha_settings {
16305:     my %captcha_params = (
16306:                            output_dir     => $Apache::lonnet::perlvar{'lonCaptchaDir'},
16307:                            www_output_dir => "/captchaspool",
16308:                            db_dir         => $Apache::lonnet::perlvar{'lonCaptchaDb'},
16309:                            numchars       => '5',
16310:                          );
16311:     return %captcha_params;
16312: }
16313: 
16314: sub check_captcha {
16315:     my ($captcha_chk,$captcha_error);
16316:     my $code = $env{'form.code'};
16317:     my $md5sum = $env{'form.crypt'};
16318:     my %captcha_params = &captcha_settings();
16319:     my $captcha = Authen::Captcha->new(
16320:                       output_folder => $captcha_params{'output_dir'},
16321:                       data_folder   => $captcha_params{'db_dir'},
16322:                   );
16323:     $captcha_chk = $captcha->check_code($code,$md5sum);
16324:     my %captcha_hash = (
16325:                         0       => 'Code not checked (file error)',
16326:                        -1      => 'Failed: code expired',
16327:                        -2      => 'Failed: invalid code (not in database)',
16328:                        -3      => 'Failed: invalid code (code does not match crypt)',
16329:     );
16330:     if ($captcha_chk != 1) {
16331:         $captcha_error = $captcha_hash{$captcha_chk}
16332:     }
16333:     return ($captcha_chk,$captcha_error);
16334: }
16335: 
16336: sub create_recaptcha {
16337:     my ($pubkey) = @_;
16338:     my $use_ssl;
16339:     if ($ENV{'SERVER_PORT'} == 443) {
16340:         $use_ssl = 1;
16341:     }
16342:     my $captcha = Captcha::reCAPTCHA->new;
16343:     return $captcha->get_options_setter({theme => 'white'})."\n".
16344:            $captcha->get_html($pubkey,undef,$use_ssl).
16345:            &mt('If the text is hard to read, [_1] will replace them.',
16346:                '<img src="/res/adm/pages/refresh.gif" alt="reCAPTCHA refresh" />').
16347:            '<br /><br />';
16348: }
16349: 
16350: sub check_recaptcha {
16351:     my ($privkey) = @_;
16352:     my $captcha_chk;
16353:     my $captcha = Captcha::reCAPTCHA->new;
16354:     my $captcha_result =
16355:         $captcha->check_answer(
16356:                                 $privkey,
16357:                                 $ENV{'REMOTE_ADDR'},
16358:                                 $env{'form.recaptcha_challenge_field'},
16359:                                 $env{'form.recaptcha_response_field'},
16360:                               );
16361:     if ($captcha_result->{is_valid}) {
16362:         $captcha_chk = 1;
16363:     }
16364:     return $captcha_chk;
16365: }
16366: 
16367: sub emailusername_info {
16368:     my @fields = ('firstname','lastname','institution','web','location','officialemail');
16369:     my %titles = &Apache::lonlocal::texthash (
16370:                      lastname      => 'Last Name',
16371:                      firstname     => 'First Name',
16372:                      institution   => 'School/college/university',
16373:                      location      => "School's city, state/province, country",
16374:                      web           => "School's web address",
16375:                      officialemail => 'E-mail address at institution (if different)',
16376:                  );
16377:     return (\@fields,\%titles);
16378: }
16379: 
16380: sub cleanup_html {
16381:     my ($incoming) = @_;
16382:     my $outgoing;
16383:     if ($incoming ne '') {
16384:         $outgoing = $incoming;
16385:         $outgoing =~ s/;/&#059;/g;
16386:         $outgoing =~ s/\#/&#035;/g;
16387:         $outgoing =~ s/\&/&#038;/g;
16388:         $outgoing =~ s/</&#060;/g;
16389:         $outgoing =~ s/>/&#062;/g;
16390:         $outgoing =~ s/\(/&#040/g;
16391:         $outgoing =~ s/\)/&#041;/g;
16392:         $outgoing =~ s/"/&#034;/g;
16393:         $outgoing =~ s/'/&#039;/g;
16394:         $outgoing =~ s/\$/&#036;/g;
16395:         $outgoing =~ s{/}{&#047;}g;
16396:         $outgoing =~ s/=/&#061;/g;
16397:         $outgoing =~ s/\\/&#092;/g
16398:     }
16399:     return $outgoing;
16400: }
16401: 
16402: # Checks for critical messages and returns a redirect url if one exists.
16403: # $interval indicates how often to check for messages.
16404: sub critical_redirect {
16405:     my ($interval) = @_;
16406:     if ((time-$env{'user.criticalcheck.time'})>$interval) {
16407:         my @what=&Apache::lonnet::dump('critical', $env{'user.domain'}, 
16408:                                         $env{'user.name'});
16409:         &Apache::lonnet::appenv({'user.criticalcheck.time'=>time});
16410:         my $redirecturl;
16411:         if ($what[0]) {
16412: 	    if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
16413: 	        $redirecturl='/adm/email?critical=display';
16414: 	        my $url=&Apache::lonnet::absolute_url().$redirecturl;
16415:                 return (1, $url);
16416:             }
16417:         }
16418:     } 
16419:     return ();
16420: }
16421: 
16422: # Use:
16423: #   my $answer=reply("encrypt:passwd:$udom:$uname:$upass",$tryserver);
16424: #
16425: ##################################################
16426: #          password associated functions         #
16427: ##################################################
16428: sub des_keys {
16429:     # Make a new key for DES encryption.
16430:     # Each key has two parts which are returned separately.
16431:     # Please note:  Each key must be passed through the &hex function
16432:     # before it is output to the web browser.  The hex versions cannot
16433:     # be used to decrypt.
16434:     my @hexstr=('0','1','2','3','4','5','6','7',
16435:                 '8','9','a','b','c','d','e','f');
16436:     my $lkey='';
16437:     for (0..7) {
16438:         $lkey.=$hexstr[rand(15)];
16439:     }
16440:     my $ukey='';
16441:     for (0..7) {
16442:         $ukey.=$hexstr[rand(15)];
16443:     }
16444:     return ($lkey,$ukey);
16445: }
16446: 
16447: sub des_decrypt {
16448:     my ($key,$cyphertext) = @_;
16449:     my $keybin=pack("H16",$key);
16450:     my $cypher;
16451:     if ($Crypt::DES::VERSION>=2.03) {
16452:         $cypher=new Crypt::DES $keybin;
16453:     } else {
16454:         $cypher=new DES $keybin;
16455:     }
16456:     my $plaintext=
16457:         $cypher->decrypt(unpack("a8",pack("H16",substr($cyphertext,0,16))));
16458:     $plaintext.=
16459:         $cypher->decrypt(unpack("a8",pack("H16",substr($cyphertext,16,16))));
16460:     $plaintext=substr($plaintext,1,ord(substr($plaintext,0,1)) );
16461:     return $plaintext;
16462: }
16463: 
16464: 1;
16465: __END__;
16466: 

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