File:  [LON-CAPA] / loncom / interface / loncommon.pm
Revision 1.882: download - view: text, annotated - select for diffs
Sat Aug 8 19:55:04 2009 UTC (14 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: bz2851, HEAD
Course Requests
- lonnet.pm
  - generation of unique coursenum move to own routine: &generate_coursenum()
  - prefacing of coursenum with number then letter added to second attempt
      to generate an ID (to duplicate change in first attempt in rev 1.674).
  - additional optional argument to &createcourse() - can use a previously
      generated, but unused, coursenum (will be hashkey in a queued course request).

- loncommon.pm
  - additional optional argument to &construct_course() - $cnum, previously
      generated, but unused, coursenum.
  - check_clone() - unrestricted cloning in course's in role's domain now
                    requires creator has ccc privilege in role's domain.

- batchcreatecourse.pm
  - &build_course() - update documentation, and new optional arg
                      previously generated, but unused, coursenum

    1: # The LearningOnline Network with CAPA
    2: # a pile of common routines
    3: #
    4: # $Id: loncommon.pm,v 1.882 2009/08/08 19:55:04 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 LONCAPA qw(:DEFAULT :match);
   71: use DateTime::TimeZone;
   72: use DateTime::Locale::Catalog;
   73: 
   74: # ---------------------------------------------- Designs
   75: use vars qw(%defaultdesign);
   76: 
   77: my $readit;
   78: 
   79: 
   80: ##
   81: ## Global Variables
   82: ##
   83: 
   84: 
   85: # ----------------------------------------------- SSI with retries:
   86: #
   87: 
   88: =pod
   89: 
   90: =head1 Server Side include with retries:
   91: 
   92: =over 4
   93: 
   94: =item * &ssi_with_retries(resource,retries form)
   95: 
   96: Performs an ssi with some number of retries.  Retries continue either
   97: until the result is ok or until the retry count supplied by the
   98: caller is exhausted.  
   99: 
  100: Inputs:
  101: 
  102: =over 4
  103: 
  104: resource   - Identifies the resource to insert.
  105: 
  106: retries    - Count of the number of retries allowed.
  107: 
  108: form       - Hash that identifies the rendering options.
  109: 
  110: =back
  111: 
  112: Returns:
  113: 
  114: =over 4
  115: 
  116: content    - The content of the response.  If retries were exhausted this is empty.
  117: 
  118: response   - The response from the last attempt (which may or may not have been successful.
  119: 
  120: =back
  121: 
  122: =back
  123: 
  124: =cut
  125: 
  126: sub ssi_with_retries {
  127:     my ($resource, $retries, %form) = @_;
  128: 
  129: 
  130:     my $ok = 0;			# True if we got a good response.
  131:     my $content;
  132:     my $response;
  133: 
  134:     # Try to get the ssi done. within the retries count:
  135: 
  136:     do {
  137: 	($content, $response) = &Apache::lonnet::ssi($resource, %form);
  138: 	$ok      = $response->is_success;
  139:         if (!$ok) {
  140:             &Apache::lonnet::logthis("Failed ssi_with_retries on $resource: ".$response->is_success.', '.$response->code.', '.$response->message);
  141:         }
  142: 	$retries--;
  143:     } while (!$ok && ($retries > 0));
  144: 
  145:     if (!$ok) {
  146: 	$content = '';		# On error return an empty content.
  147:     }
  148:     return ($content, $response);
  149: 
  150: }
  151: 
  152: 
  153: 
  154: # ----------------------------------------------- Filetypes/Languages/Copyright
  155: my %language;
  156: my %supported_language;
  157: my %cprtag;
  158: my %scprtag;
  159: my %fe; my %fd; my %fm;
  160: my %category_extensions;
  161: 
  162: # ---------------------------------------------- Thesaurus variables
  163: #
  164: # %Keywords:
  165: #      A hash used by &keyword to determine if a word is considered a keyword.
  166: # $thesaurus_db_file 
  167: #      Scalar containing the full path to the thesaurus database.
  168: 
  169: my %Keywords;
  170: my $thesaurus_db_file;
  171: 
  172: #
  173: # Initialize values from language.tab, copyright.tab, filetypes.tab,
  174: # thesaurus.tab, and filecategories.tab.
  175: #
  176: BEGIN {
  177:     # Variable initialization
  178:     $thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
  179:     #
  180:     unless ($readit) {
  181: # ------------------------------------------------------------------- languages
  182:     {
  183:         my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
  184:                                    '/language.tab';
  185:         if ( open(my $fh,"<$langtabfile") ) {
  186:             while (my $line = <$fh>) {
  187:                 next if ($line=~/^\#/);
  188:                 chomp($line);
  189:                 my ($key,$two,$country,$three,$enc,$val,$sup)=(split(/\t/,$line));
  190:                 $language{$key}=$val.' - '.$enc;
  191:                 if ($sup) {
  192:                     $supported_language{$key}=$sup;
  193:                 }
  194:             }
  195:             close($fh);
  196:         }
  197:     }
  198: # ------------------------------------------------------------------ copyrights
  199:     {
  200:         my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
  201:                                   '/copyright.tab';
  202:         if ( open (my $fh,"<$copyrightfile") ) {
  203:             while (my $line = <$fh>) {
  204:                 next if ($line=~/^\#/);
  205:                 chomp($line);
  206:                 my ($key,$val)=(split(/\s+/,$line,2));
  207:                 $cprtag{$key}=$val;
  208:             }
  209:             close($fh);
  210:         }
  211:     }
  212: # ----------------------------------------------------------- source copyrights
  213:     {
  214:         my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
  215:                                   '/source_copyright.tab';
  216:         if ( open (my $fh,"<$sourcecopyrightfile") ) {
  217:             while (my $line = <$fh>) {
  218:                 next if ($line =~ /^\#/);
  219:                 chomp($line);
  220:                 my ($key,$val)=(split(/\s+/,$line,2));
  221:                 $scprtag{$key}=$val;
  222:             }
  223:             close($fh);
  224:         }
  225:     }
  226: 
  227: # -------------------------------------------------------------- default domain designs
  228:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
  229:     my $designfile = $designdir.'/default.tab';
  230:     if ( open (my $fh,"<$designfile") ) {
  231:         while (my $line = <$fh>) {
  232:             next if ($line =~ /^\#/);
  233:             chomp($line);
  234:             my ($key,$val)=(split(/\=/,$line));
  235:             if ($val) { $defaultdesign{$key}=$val; }
  236:         }
  237:         close($fh);
  238:     }
  239: 
  240: # ------------------------------------------------------------- file categories
  241:     {
  242:         my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
  243:                                   '/filecategories.tab';
  244:         if ( open (my $fh,"<$categoryfile") ) {
  245: 	    while (my $line = <$fh>) {
  246: 		next if ($line =~ /^\#/);
  247: 		chomp($line);
  248:                 my ($extension,$category)=(split(/\s+/,$line,2));
  249:                 push @{$category_extensions{lc($category)}},$extension;
  250:             }
  251:             close($fh);
  252:         }
  253: 
  254:     }
  255: # ------------------------------------------------------------------ file types
  256:     {
  257:         my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
  258:                '/filetypes.tab';
  259:         if ( open (my $fh,"<$typesfile") ) {
  260:             while (my $line = <$fh>) {
  261: 		next if ($line =~ /^\#/);
  262: 		chomp($line);
  263:                 my ($ending,$emb,$mime,$descr)=split(/\s+/,$line,4);
  264:                 if ($descr ne '') {
  265:                     $fe{$ending}=lc($emb);
  266:                     $fd{$ending}=$descr;
  267:                     if ($mime ne 'unk') { $fm{$ending}=$mime; }
  268:                 }
  269:             }
  270:             close($fh);
  271:         }
  272:     }
  273:     &Apache::lonnet::logthis(
  274:              "<span style='color:yellow;'>INFO: Read file types</span>");
  275:     $readit=1;
  276:     }  # end of unless($readit) 
  277:     
  278: }
  279: 
  280: ###############################################################
  281: ##           HTML and Javascript Helper Functions            ##
  282: ###############################################################
  283: 
  284: =pod 
  285: 
  286: =head1 HTML and Javascript Functions
  287: 
  288: =over 4
  289: 
  290: =item * &browser_and_searcher_javascript()
  291: 
  292: X<browsing, javascript>X<searching, javascript>Returns a string
  293: containing javascript with two functions, C<openbrowser> and
  294: C<opensearcher>. Returned string does not contain E<lt>scriptE<gt>
  295: tags.
  296: 
  297: =item * &openbrowser(formname,elementname,only,omit) [javascript]
  298: 
  299: inputs: formname, elementname, only, omit
  300: 
  301: formname and elementname indicate the name of the html form and name of
  302: the element that the results of the browsing selection are to be placed in. 
  303: 
  304: Specifying 'only' will restrict the browser to displaying only files
  305: with the given extension.  Can be a comma separated list.
  306: 
  307: Specifying 'omit' will restrict the browser to NOT displaying files
  308: with the given extension.  Can be a comma separated list.
  309: 
  310: =item * &opensearcher(formname,elementname) [javascript]
  311: 
  312: Inputs: formname, elementname
  313: 
  314: formname and elementname specify the name of the html form and the name
  315: of the element the selection from the search results will be placed in.
  316: 
  317: =cut
  318: 
  319: sub browser_and_searcher_javascript {
  320:     my ($mode)=@_;
  321:     if (!defined($mode)) { $mode='edit'; }
  322:     my $resurl=&escape_single(&lastresurl());
  323:     return <<END;
  324: // <!-- BEGIN LON-CAPA Internal
  325:     var editbrowser = null;
  326:     function openbrowser(formname,elementname,only,omit,titleelement) {
  327:         var url = '$resurl/?';
  328:         if (editbrowser == null) {
  329:             url += 'launch=1&';
  330:         }
  331:         url += 'catalogmode=interactive&';
  332:         url += 'mode=$mode&';
  333:         url += 'inhibitmenu=yes&';
  334:         url += 'form=' + formname + '&';
  335:         if (only != null) {
  336:             url += 'only=' + only + '&';
  337:         } else {
  338:             url += 'only=&';
  339: 	}
  340:         if (omit != null) {
  341:             url += 'omit=' + omit + '&';
  342:         } else {
  343:             url += 'omit=&';
  344: 	}
  345:         if (titleelement != null) {
  346:             url += 'titleelement=' + titleelement + '&';
  347:         } else {
  348: 	    url += 'titleelement=&';
  349: 	}
  350:         url += 'element=' + elementname + '';
  351:         var title = 'Browser';
  352:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
  353:         options += ',width=700,height=600';
  354:         editbrowser = open(url,title,options,'1');
  355:         editbrowser.focus();
  356:     }
  357:     var editsearcher;
  358:     function opensearcher(formname,elementname,titleelement) {
  359:         var url = '/adm/searchcat?';
  360:         if (editsearcher == null) {
  361:             url += 'launch=1&';
  362:         }
  363:         url += 'catalogmode=interactive&';
  364:         url += 'mode=$mode&';
  365:         url += 'form=' + formname + '&';
  366:         if (titleelement != null) {
  367:             url += 'titleelement=' + titleelement + '&';
  368:         } else {
  369: 	    url += 'titleelement=&';
  370: 	}
  371:         url += 'element=' + elementname + '';
  372:         var title = 'Search';
  373:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
  374:         options += ',width=700,height=600';
  375:         editsearcher = open(url,title,options,'1');
  376:         editsearcher.focus();
  377:     }
  378: // END LON-CAPA Internal -->
  379: END
  380: }
  381: 
  382: sub lastresurl {
  383:     if ($env{'environment.lastresurl'}) {
  384: 	return $env{'environment.lastresurl'}
  385:     } else {
  386: 	return '/res';
  387:     }
  388: }
  389: 
  390: sub storeresurl {
  391:     my $resurl=&Apache::lonnet::clutter(shift);
  392:     unless ($resurl=~/^\/res/) { return 0; }
  393:     $resurl=~s/\/$//;
  394:     &Apache::lonnet::put('environment',{'lastresurl' => $resurl});
  395:     &Apache::lonnet::appenv({'environment.lastresurl' => $resurl});
  396:     return 1;
  397: }
  398: 
  399: sub studentbrowser_javascript {
  400:    unless (
  401:             (($env{'request.course.id'}) && 
  402:              (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
  403: 	      || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
  404: 					  '/'.$env{'request.course.sec'})
  405: 	      ))
  406:          || ($env{'request.role'}=~/^(au|dc|su)/)
  407:           ) { return ''; }  
  408:    return (<<'ENDSTDBRW');
  409: <script type="text/javascript" language="Javascript">
  410: // <![CDATA[
  411:     var stdeditbrowser;
  412:     function openstdbrowser(formname,uname,udom,roleflag,ignorefilter,courseadvonly) {
  413:         var url = '/adm/pickstudent?';
  414:         var filter;
  415: 	if (!ignorefilter) {
  416: 	    eval('filter=document.'+formname+'.'+uname+'.value;');
  417: 	}
  418:         if (filter != null) {
  419:            if (filter != '') {
  420:                url += 'filter='+filter+'&';
  421: 	   }
  422:         }
  423:         url += 'form=' + formname + '&unameelement='+uname+
  424:                                     '&udomelement='+udom;
  425: 	if (roleflag) { url+="&roles=1"; }
  426:         if (courseadvonly) { url+="&courseadvonly=1"; }
  427:         var title = 'Student_Browser';
  428:         var options = 'scrollbars=1,resizable=1,menubar=0';
  429:         options += ',width=700,height=600';
  430:         stdeditbrowser = open(url,title,options,'1');
  431:         stdeditbrowser.focus();
  432:     }
  433: // ]]>
  434: </script>
  435: ENDSTDBRW
  436: }
  437: 
  438: sub selectstudent_link {
  439:    my ($form,$unameele,$udomele,$courseadvonly)=@_;
  440:    my $callargs = "'".$form."','".$unameele."','".$udomele."'";
  441:    if ($env{'request.course.id'}) {  
  442:        if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
  443: 	   && !&Apache::lonnet::allowed('srm',$env{'request.course.id'}.
  444: 					'/'.$env{'request.course.sec'})) {
  445: 	   return '';
  446:        }
  447:        if ($courseadvonly)  {
  448:            $callargs .= ",'',1,1";
  449:        }
  450:        return '<span class="LC_nobreak">'.
  451:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
  452:               &mt('Select User').'</a></span>';
  453:    }
  454:    if ($env{'request.role'}=~/^(au|dc|su)/) {
  455:        $callargs .= ",1"; 
  456:        return '<span class="LC_nobreak">'.
  457:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
  458:               &mt('Select User').'</a></span>';
  459:    }
  460:    return '';
  461: }
  462: 
  463: sub authorbrowser_javascript {
  464:     return <<"ENDAUTHORBRW";
  465: <script type="text/javascript" language="JavaScript">
  466: // <![CDATA[
  467: var stdeditbrowser;
  468: 
  469: function openauthorbrowser(formname,udom) {
  470:     var url = '/adm/pickauthor?';
  471:     url += 'form='+formname+'&roledom='+udom;
  472:     var title = 'Author_Browser';
  473:     var options = 'scrollbars=1,resizable=1,menubar=0';
  474:     options += ',width=700,height=600';
  475:     stdeditbrowser = open(url,title,options,'1');
  476:     stdeditbrowser.focus();
  477: }
  478: 
  479: // ]]>
  480: </script>
  481: ENDAUTHORBRW
  482: }
  483: 
  484: sub coursebrowser_javascript {
  485:     my ($domainfilter,$sec_element,$formname)=@_;
  486:     my $crs_or_grp_alert = &mt('Please select the type of LON-CAPA entity - Course or Community - for which you wish to add/modify a user role');
  487:     my $id_functions = &javascript_index_functions();
  488:     my $output = '
  489: <script type="text/javascript" language="JavaScript">
  490: // <![CDATA[
  491:     var stdeditbrowser;'."\n";
  492: 
  493:     $output .= <<"ENDSTDBRW";
  494:     function opencrsbrowser(formname,uname,udom,desc,extra_element,multflag,crstype) {
  495:         var url = '/adm/pickcourse?';
  496:         var domainfilter = getDomainFromSelectbox(formname,udom);
  497:         if (domainfilter != null) {
  498:            if (domainfilter != '') {
  499:                url += 'domainfilter='+domainfilter+'&';
  500: 	   }
  501:         }
  502:         url += 'form=' + formname + '&cnumelement='+uname+
  503: 	                            '&cdomelement='+udom+
  504:                                     '&cnameelement='+desc;
  505:         if (extra_element !=null && extra_element != '') {
  506:             if (formname == 'rolechoice' || formname == 'studentform') {
  507:                 url += '&roleelement='+extra_element;
  508:                 if (domainfilter == null || domainfilter == '') {
  509:                     url += '&domainfilter='+extra_element;
  510:                 }
  511:             }
  512:             else {
  513:                 if (formname == 'portform') {
  514:                     url += '&setroles='+extra_element;
  515:                 } else {
  516:                     if (formname == 'rules') {
  517:                         url += '&fixeddom='+extra_element; 
  518:                     }
  519:                 }
  520:             }     
  521:         }
  522:         if (formname == 'ccrs') {
  523:             var ownername = document.forms[formid].ccuname.value;
  524:             var ownerdom =  document.forms[formid].ccdomain.options[document.forms[formid].ccdomain.selectedIndex].value;
  525:             url += '&cloner='+ownername+':'+ownerdom;
  526:         }
  527:         if (multflag !=null && multflag != '') {
  528:             url += '&multiple='+multflag;
  529:         }
  530:         if (crstype == 'Course/Community') {
  531:             if (formname == 'cu') {
  532:                 crstype = document.cu.crstype.options[document.cu.crstype.selectedIndex].value; 
  533:                 if (crstype == "") {
  534:                     alert("$crs_or_grp_alert");
  535:                     return;
  536:                 }
  537:             }
  538:         }
  539:         if (crstype !=null && crstype != '') {
  540:             url += '&type='+crstype;
  541:         }
  542:         var title = 'Course_Browser';
  543:         var options = 'scrollbars=1,resizable=1,menubar=0';
  544:         options += ',width=700,height=600';
  545:         stdeditbrowser = open(url,title,options,'1');
  546:         stdeditbrowser.focus();
  547:     }
  548: $id_functions
  549: ENDSTDBRW
  550:     if ($sec_element ne '') {
  551:         $output .= &setsec_javascript($sec_element,$formname);
  552:     }
  553:     $output .= '
  554: // ]]>
  555: </script>';
  556:     return $output;
  557: }
  558: 
  559: sub javascript_index_functions {
  560:     return <<"ENDJS";
  561: 
  562: function getFormIdByName(formname) {
  563:     for (var i=0;i<document.forms.length;i++) {
  564:         if (document.forms[i].name == formname) {
  565:             return i;
  566:         }
  567:     }
  568:     return -1;
  569: }
  570: 
  571: function getIndexByName(formid,item) {
  572:     for (var i=0;i<document.forms[formid].elements.length;i++) {
  573:         if (document.forms[formid].elements[i].name == item) {
  574:             return i;
  575:         }
  576:     }
  577:     return -1;
  578: }
  579: 
  580: function getDomainFromSelectbox(formname,udom) {
  581:     var userdom;
  582:     var formid = getFormIdByName(formname);
  583:     if (formid > -1) {
  584:         var domid = getIndexByName(formid,udom);
  585:         if (domid > -1) {
  586:             if (document.forms[formid].elements[domid].type == 'select-one') {
  587:                 userdom=document.forms[formid].elements[domid].options[document.forms[formid].elements[domid].selectedIndex].value;
  588:             }
  589:             if (document.forms[formid].elements[domid].type == 'hidden') {
  590:                 userdom=document.forms[formid].elements[domid].value;
  591:             }
  592:         }
  593:     }
  594:     return userdom;
  595: }
  596: 
  597: ENDJS
  598: 
  599: }
  600: 
  601: sub userbrowser_javascript {
  602:     my $id_functions = &javascript_index_functions();
  603:     return <<"ENDUSERBRW";
  604: 
  605: function openuserbrowser(formname,uname,udom,ulast,ufirst,uemail,hideudom,crsdom) {
  606:     var url = '/adm/pickuser?';
  607:     var userdom = getDomainFromSelectbox(formname,udom);
  608:     if (userdom != null) {
  609:        if (userdom != '') {
  610:            url += 'srchdom='+userdom+'&';
  611:        }
  612:     }
  613:     url += 'form=' + formname + '&unameelement='+uname+
  614:                                 '&udomelement='+udom+
  615:                                 '&ulastelement='+ulast+
  616:                                 '&ufirstelement='+ufirst+
  617:                                 '&uemailelement='+uemail+
  618:                                 '&hideudomelement='+hideudom+
  619:                                 '&coursedom='+crsdom;
  620:     var title = 'User_Browser';
  621:     var options = 'scrollbars=1,resizable=1,menubar=0';
  622:     options += ',width=700,height=600';
  623:     var stdeditbrowser = open(url,title,options,'1');
  624:     stdeditbrowser.focus();
  625: }
  626: 
  627: function fix_domain (formname,udom,origdom) {
  628:     var formid = getFormIdByName(formname);
  629:     if (formid > -1) {
  630:         var domid = getIndexByName(formid,udom);
  631:         var hidedomid = getIndexByName(formid,origdom);
  632:         if (hidedomid > -1) {
  633:             var fixeddom = document.forms[formid].elements[hidedomid].value;
  634:             if (domid > -1) {
  635:                 var slct = document.forms[formid].elements[domid];
  636:                 if (slct.type == 'select-one') {
  637:                     var i;
  638:                     for (i=0;i<slct.length;i++) {
  639:                         if (slct.options[i].value==fixeddom) { slct.selectedIndex=i; }
  640:                     }
  641:                 }
  642:                 if (slct.type == 'hidden') {
  643:                     slct.value = fixeddom;
  644:                 }
  645:             }
  646:         }
  647:     }
  648:     return;
  649: }
  650: 
  651: $id_functions
  652: ENDUSERBRW
  653: }
  654: 
  655: sub setsec_javascript {
  656:     my ($sec_element,$formname) = @_;
  657:     my $setsections = qq|
  658: function setSect(sectionlist) {
  659:     var sectionsArray = new Array();
  660:     if ((sectionlist != '') && (typeof sectionlist != "undefined")) {
  661:         sectionsArray = sectionlist.split(",");
  662:     }
  663:     var numSections = sectionsArray.length;
  664:     document.$formname.$sec_element.length = 0;
  665:     if (numSections == 0) {
  666:         document.$formname.$sec_element.multiple=false;
  667:         document.$formname.$sec_element.size=1;
  668:         document.$formname.$sec_element.options[0] = new Option('No existing sections','',false,false)
  669:     } else {
  670:         if (numSections == 1) {
  671:             document.$formname.$sec_element.multiple=false;
  672:             document.$formname.$sec_element.size=1;
  673:             document.$formname.$sec_element.options[0] = new Option('Select','',true,true);
  674:             document.$formname.$sec_element.options[1] = new Option('No section','',false,false)
  675:             document.$formname.$sec_element.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
  676:         } else {
  677:             for (var i=0; i<numSections; i++) {
  678:                 document.$formname.$sec_element.options[i] = new Option(sectionsArray[i],sectionsArray[i],false,false)
  679:             }
  680:             document.$formname.$sec_element.multiple=true
  681:             if (numSections < 3) {
  682:                 document.$formname.$sec_element.size=numSections;
  683:             } else {
  684:                 document.$formname.$sec_element.size=3;
  685:             }
  686:             document.$formname.$sec_element.options[0].selected = false
  687:         }
  688:     }
  689: }
  690: |;
  691:     return $setsections;
  692: }
  693: 
  694: 
  695: sub selectcourse_link {
  696:    my ($form,$unameele,$udomele,$desc,$extra_element,$multflag,$selecttype)=@_;
  697:    my $linktext = &mt('Select Course');
  698:    if ($selecttype eq 'Community') {
  699:        $linktext = &mt('Select Community'); 
  700:    }
  701:    return '<span class="LC_nobreak">'
  702:          ."<a href='"
  703:          .'javascript:opencrsbrowser("'.$form.'","'.$unameele
  704:          .'","'.$udomele.'","'.$desc.'","'.$extra_element
  705:          .'","'.$multflag.'","'.$selecttype.'");'
  706:          ."'>".$linktext.'</a>'
  707:          .'</span>';
  708: }
  709: 
  710: sub selectauthor_link {
  711:    my ($form,$udom)=@_;
  712:    return '<a href="javascript:openauthorbrowser('."'$form','$udom'".');">'.
  713:           &mt('Select Author').'</a>';
  714: }
  715: 
  716: sub selectuser_link {
  717:     my ($form,$unameelem,$domelem,$lastelem,$firstelem,$emailelem,$hdomelem,
  718:         $coursedom,$linktext) = @_;
  719:     return '<a href="javascript:openuserbrowser('."'$form','$unameelem','$domelem',".
  720:            "'$lastelem','$firstelem','$emailelem','$hdomelem','$coursedom'".
  721:            ');">'.$linktext.'</a>';
  722: }
  723: 
  724: sub check_uncheck_jscript {
  725:     my $jscript = <<"ENDSCRT";
  726: function checkAll(field) {
  727:     if (field.length > 0) {
  728:         for (i = 0; i < field.length; i++) {
  729:             field[i].checked = true ;
  730:         }
  731:     } else {
  732:         field.checked = true
  733:     }
  734: }
  735:  
  736: function uncheckAll(field) {
  737:     if (field.length > 0) {
  738:         for (i = 0; i < field.length; i++) {
  739:             field[i].checked = false ;
  740:         }
  741:     } else {
  742:         field.checked = false ;
  743:     }
  744: }
  745: ENDSCRT
  746:     return $jscript;
  747: }
  748: 
  749: sub select_timezone {
  750:    my ($name,$selected,$onchange,$includeempty)=@_;
  751:    my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
  752:    if ($includeempty) {
  753:        $output .= '<option value=""';
  754:        if (($selected eq '') || ($selected eq 'local')) {
  755:            $output .= ' selected="selected" ';
  756:        }
  757:        $output .= '> </option>';
  758:    }
  759:    my @timezones = DateTime::TimeZone->all_names;
  760:    foreach my $tzone (@timezones) {
  761:        $output.= '<option value="'.$tzone.'"';
  762:        if ($tzone eq $selected) {
  763:            $output.=' selected="selected"';
  764:        }
  765:        $output.=">$tzone</option>\n";
  766:    }
  767:    $output.="</select>";
  768:    return $output;
  769: }
  770: 
  771: sub select_datelocale {
  772:     my ($name,$selected,$onchange,$includeempty)=@_;
  773:     my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
  774:     if ($includeempty) {
  775:         $output .= '<option value=""';
  776:         if ($selected eq '') {
  777:             $output .= ' selected="selected" ';
  778:         }
  779:         $output .= '> </option>';
  780:     }
  781:     my (@possibles,%locale_names);
  782:     my @locales = DateTime::Locale::Catalog::Locales;
  783:     foreach my $locale (@locales) {
  784:         if (ref($locale) eq 'HASH') {
  785:             my $id = $locale->{'id'};
  786:             if ($id ne '') {
  787:                 my $en_terr = $locale->{'en_territory'};
  788:                 my $native_terr = $locale->{'native_territory'};
  789:                 my @languages = &Apache::lonlocal::preferred_languages();
  790:                 if (grep(/^en$/,@languages) || !@languages) {
  791:                     if ($en_terr ne '') {
  792:                         $locale_names{$id} = '('.$en_terr.')';
  793:                     } elsif ($native_terr ne '') {
  794:                         $locale_names{$id} = $native_terr;
  795:                     }
  796:                 } else {
  797:                     if ($native_terr ne '') {
  798:                         $locale_names{$id} = $native_terr.' ';
  799:                     } elsif ($en_terr ne '') {
  800:                         $locale_names{$id} = '('.$en_terr.')';
  801:                     }
  802:                 }
  803:                 push (@possibles,$id);
  804:             }
  805:         }
  806:     }
  807:     foreach my $item (sort(@possibles)) {
  808:         $output.= '<option value="'.$item.'"';
  809:         if ($item eq $selected) {
  810:             $output.=' selected="selected"';
  811:         }
  812:         $output.=">$item";
  813:         if ($locale_names{$item} ne '') {
  814:             $output.="  $locale_names{$item}</option>\n";
  815:         }
  816:         $output.="</option>\n";
  817:     }
  818:     $output.="</select>";
  819:     return $output;
  820: }
  821: 
  822: sub select_language {
  823:     my ($name,$selected,$includeempty) = @_;
  824:     my %langchoices;
  825:     if ($includeempty) {
  826:         %langchoices = ('' => 'No language preference');
  827:     }
  828:     foreach my $id (&languageids()) {
  829:         my $code = &supportedlanguagecode($id);
  830:         if ($code) {
  831:             $langchoices{$code} = &plainlanguagedescription($id);
  832:         }
  833:     }
  834:     return &select_form($selected,$name,%langchoices);
  835: }
  836: 
  837: =pod
  838: 
  839: =item * &linked_select_forms(...)
  840: 
  841: linked_select_forms returns a string containing a <script></script> block
  842: and html for two <select> menus.  The select menus will be linked in that
  843: changing the value of the first menu will result in new values being placed
  844: in the second menu.  The values in the select menu will appear in alphabetical
  845: order unless a defined order is provided.
  846: 
  847: linked_select_forms takes the following ordered inputs:
  848: 
  849: =over 4
  850: 
  851: =item * $formname, the name of the <form> tag
  852: 
  853: =item * $middletext, the text which appears between the <select> tags
  854: 
  855: =item * $firstdefault, the default value for the first menu
  856: 
  857: =item * $firstselectname, the name of the first <select> tag
  858: 
  859: =item * $secondselectname, the name of the second <select> tag
  860: 
  861: =item * $hashref, a reference to a hash containing the data for the menus.
  862: 
  863: =item * $menuorder, the order of values in the first menu
  864: 
  865: =back 
  866: 
  867: Below is an example of such a hash.  Only the 'text', 'default', and 
  868: 'select2' keys must appear as stated.  keys(%menu) are the possible 
  869: values for the first select menu.  The text that coincides with the 
  870: first menu value is given in $menu{$choice1}->{'text'}.  The values 
  871: and text for the second menu are given in the hash pointed to by 
  872: $menu{$choice1}->{'select2'}.  
  873: 
  874:  my %menu = ( A1 => { text =>"Choice A1" ,
  875:                        default => "B3",
  876:                        select2 => { 
  877:                            B1 => "Choice B1",
  878:                            B2 => "Choice B2",
  879:                            B3 => "Choice B3",
  880:                            B4 => "Choice B4"
  881:                            },
  882:                        order => ['B4','B3','B1','B2'],
  883:                    },
  884:                A2 => { text =>"Choice A2" ,
  885:                        default => "C2",
  886:                        select2 => { 
  887:                            C1 => "Choice C1",
  888:                            C2 => "Choice C2",
  889:                            C3 => "Choice C3"
  890:                            },
  891:                        order => ['C2','C1','C3'],
  892:                    },
  893:                A3 => { text =>"Choice A3" ,
  894:                        default => "D6",
  895:                        select2 => { 
  896:                            D1 => "Choice D1",
  897:                            D2 => "Choice D2",
  898:                            D3 => "Choice D3",
  899:                            D4 => "Choice D4",
  900:                            D5 => "Choice D5",
  901:                            D6 => "Choice D6",
  902:                            D7 => "Choice D7"
  903:                            },
  904:                        order => ['D4','D3','D2','D1','D7','D6','D5'],
  905:                    }
  906:                );
  907: 
  908: =cut
  909: 
  910: sub linked_select_forms {
  911:     my ($formname,
  912:         $middletext,
  913:         $firstdefault,
  914:         $firstselectname,
  915:         $secondselectname, 
  916:         $hashref,
  917:         $menuorder,
  918:         ) = @_;
  919:     my $second = "document.$formname.$secondselectname";
  920:     my $first = "document.$formname.$firstselectname";
  921:     # output the javascript to do the changing
  922:     my $result = '';
  923:     $result.='<script type="text/javascript" language="JavaScript">'."\n";
  924:     $result.="// <![CDATA[\n";
  925:     $result.="var select2data = new Object();\n";
  926:     $" = '","';
  927:     my $debug = '';
  928:     foreach my $s1 (sort(keys(%$hashref))) {
  929:         $result.="select2data.d_$s1 = new Object();\n";        
  930:         $result.="select2data.d_$s1.def = new String('".
  931:             $hashref->{$s1}->{'default'}."');\n";
  932:         $result.="select2data.d_$s1.values = new Array(";
  933:         my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
  934:         if (ref($hashref->{$s1}->{'order'}) eq 'ARRAY') {
  935:             @s2values = @{$hashref->{$s1}->{'order'}};
  936:         }
  937:         $result.="\"@s2values\");\n";
  938:         $result.="select2data.d_$s1.texts = new Array(";        
  939:         my @s2texts;
  940:         foreach my $value (@s2values) {
  941:             push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
  942:         }
  943:         $result.="\"@s2texts\");\n";
  944:     }
  945:     $"=' ';
  946:     $result.= <<"END";
  947: 
  948: function select1_changed() {
  949:     // Determine new choice
  950:     var newvalue = "d_" + $first.value;
  951:     // update select2
  952:     var values     = select2data[newvalue].values;
  953:     var texts      = select2data[newvalue].texts;
  954:     var select2def = select2data[newvalue].def;
  955:     var i;
  956:     // out with the old
  957:     for (i = 0; i < $second.options.length; i++) {
  958:         $second.options[i] = null;
  959:     }
  960:     // in with the nuclear
  961:     for (i=0;i<values.length; i++) {
  962:         $second.options[i] = new Option(values[i]);
  963:         $second.options[i].value = values[i];
  964:         $second.options[i].text = texts[i];
  965:         if (values[i] == select2def) {
  966:             $second.options[i].selected = true;
  967:         }
  968:     }
  969: }
  970: // ]]>
  971: </script>
  972: END
  973:     # output the initial values for the selection lists
  974:     $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed()\">\n";
  975:     my @order = sort(keys(%{$hashref}));
  976:     if (ref($menuorder) eq 'ARRAY') {
  977:         @order = @{$menuorder};
  978:     }
  979:     foreach my $value (@order) {
  980:         $result.="    <option value=\"$value\" ";
  981:         $result.=" selected=\"selected\" " if ($value eq $firstdefault);
  982:         $result.=">".&mt($hashref->{$value}->{'text'})."</option>\n";
  983:     }
  984:     $result .= "</select>\n";
  985:     my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
  986:     $result .= $middletext;
  987:     $result .= "<select size=\"1\" name=\"$secondselectname\">\n";
  988:     my $seconddefault = $hashref->{$firstdefault}->{'default'};
  989:     
  990:     my @secondorder = sort(keys(%select2));
  991:     if (ref($hashref->{$firstdefault}->{'order'}) eq 'ARRAY') {
  992:         @secondorder = @{$hashref->{$firstdefault}->{'order'}};
  993:     }
  994:     foreach my $value (@secondorder) {
  995:         $result.="    <option value=\"$value\" ";        
  996:         $result.=" selected=\"selected\" " if ($value eq $seconddefault);
  997:         $result.=">".&mt($select2{$value})."</option>\n";
  998:     }
  999:     $result .= "</select>\n";
 1000:     #    return $debug;
 1001:     return $result;
 1002: }   #  end of sub linked_select_forms {
 1003: 
 1004: =pod
 1005: 
 1006: =item * &help_open_topic($topic,$text,$stayOnPage,$width,$height)
 1007: 
 1008: Returns a string corresponding to an HTML link to the given help
 1009: $topic, where $topic corresponds to the name of a .tex file in
 1010: /home/httpd/html/adm/help/tex, with underscores replaced by
 1011: spaces. 
 1012: 
 1013: $text will optionally be linked to the same topic, allowing you to
 1014: link text in addition to the graphic. If you do not want to link
 1015: text, but wish to specify one of the later parameters, pass an
 1016: empty string. 
 1017: 
 1018: $stayOnPage is a value that will be interpreted as a boolean. If true,
 1019: the link will not open a new window. If false, the link will open
 1020: a new window using Javascript. (Default is false.) 
 1021: 
 1022: $width and $height are optional numerical parameters that will
 1023: override the width and height of the popped up window, which may
 1024: be useful for certain help topics with big pictures included. 
 1025: 
 1026: =cut
 1027: 
 1028: sub help_open_topic {
 1029:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
 1030:     $text = "" if (not defined $text);
 1031:     $stayOnPage = 0 if (not defined $stayOnPage);
 1032:     $width = 350 if (not defined $width);
 1033:     $height = 400 if (not defined $height);
 1034:     my $filename = $topic;
 1035:     $filename =~ s/ /_/g;
 1036: 
 1037:     my $template = "";
 1038:     my $link;
 1039:     
 1040:     $topic=~s/\W/\_/g;
 1041: 
 1042:     if (!$stayOnPage) {
 1043: 	$link = "javascript:void(open('/adm/help/${filename}.hlp', 'Help_for_$topic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
 1044:     } else {
 1045: 	$link = "/adm/help/${filename}.hlp";
 1046:     }
 1047: 
 1048:     # Add the text
 1049:     if ($text ne "") {	
 1050: 	$template.='<span class="LC_help_open_topic">'
 1051:                   .'<a target="_top" href="'.$link.'">'
 1052:                   .$text.'</a>';
 1053:     }
 1054: 
 1055:     # (Always) Add the graphic
 1056:     my $title = &mt('Online Help');
 1057:     my $helpicon=&lonhttpdurl("/adm/help/help.png");
 1058:     $template.=' <a target="_top" href="'.$link.'" title="'.$title.'">'
 1059:               .'<img src="'.$helpicon.'" border="0"'
 1060:               .' alt="'.&mt('Help: [_1]',$topic).'"'
 1061:               .' title="'.$title.'"' 
 1062:               .' /></a>';
 1063:     if ($text ne "") {	
 1064:         $template.='</span>';
 1065:     }
 1066:     return $template;
 1067: 
 1068: }
 1069: 
 1070: # This is a quicky function for Latex cheatsheet editing, since it 
 1071: # appears in at least four places
 1072: sub helpLatexCheatsheet {
 1073:     my ($topic,$text,$not_author) = @_;
 1074:     my $out;
 1075:     my $addOther = '';
 1076:     if ($topic) {
 1077: 	$addOther = '<span>'.&Apache::loncommon::help_open_topic($topic,&mt($text),
 1078: 							       undef, undef, 600).
 1079: 								   '</span> ';
 1080:     }
 1081:     $out = '<span>' # Start cheatsheet
 1082: 	  .$addOther
 1083:           .'<span>'
 1084: 	  .&Apache::loncommon::help_open_topic('Greek_Symbols',&mt('Greek Symbols'),
 1085: 					       undef,undef,600)
 1086: 	  .'</span> <span>'
 1087: 	  .&Apache::loncommon::help_open_topic('Other_Symbols',&mt('Other Symbols'),
 1088: 					       undef,undef,600)
 1089: 	  .'</span>';
 1090:     unless ($not_author) {
 1091:         $out .= ' <span>'
 1092: 	       .&Apache::loncommon::help_open_topic('Authoring_Output_Tags',&mt('Output Tags'),
 1093: 	                                            undef,undef,600)
 1094: 	       .'</span>';
 1095:     }
 1096:     $out .= '</span>'; # End cheatsheet
 1097:     return $out;
 1098: }
 1099: 
 1100: sub general_help {
 1101:     my $helptopic='Student_Intro';
 1102:     if ($env{'request.role'}=~/^(ca|au)/) {
 1103: 	$helptopic='Authoring_Intro';
 1104:     } elsif ($env{'request.role'}=~/^cc/) {
 1105: 	$helptopic='Course_Coordination_Intro';
 1106:     } elsif ($env{'request.role'}=~/^dc/) {
 1107:         $helptopic='Domain_Coordination_Intro';
 1108:     }
 1109:     return $helptopic;
 1110: }
 1111: 
 1112: sub update_help_link {
 1113:     my ($topic,$component_help,$faq,$bug,$stayOnPage) = @_;
 1114:     my $origurl = $ENV{'REQUEST_URI'};
 1115:     $origurl=~s|^/~|/priv/|;
 1116:     my $timestamp = time;
 1117:     foreach my $datum (\$topic,\$component_help,\$faq,\$bug,\$origurl) {
 1118:         $$datum = &escape($$datum);
 1119:     }
 1120: 
 1121:     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";
 1122:     my $output .= <<"ENDOUTPUT";
 1123: <script type="text/javascript">
 1124: // <![CDATA[
 1125: banner_link = '$banner_link';
 1126: // ]]>
 1127: </script>
 1128: ENDOUTPUT
 1129:     return $output;
 1130: }
 1131: 
 1132: # now just updates the help link and generates a blue icon
 1133: sub help_open_menu {
 1134:     my ($topic,$component_help,$faq,$bug,$stayOnPage,$width,$height,$text) 
 1135: 	= @_;    
 1136:     $stayOnPage = 0 if (not defined $stayOnPage);
 1137:     # only use pop-up help (stayOnPage == 0)
 1138:     # if environment.remote is on (using remote control UI)
 1139:     if ($env{'environment.remote'} eq 'off' ) {
 1140:         $stayOnPage=1;
 1141:     }
 1142:     my $output;
 1143:     if ($component_help) {
 1144: 	if (!$text) {
 1145: 	    $output=&help_open_topic($component_help,undef,$stayOnPage,
 1146: 				       $width,$height);
 1147: 	} else {
 1148: 	    my $help_text;
 1149: 	    $help_text=&unescape($topic);
 1150: 	    $output='<table><tr><td>'.
 1151: 		&help_open_topic($component_help,$help_text,$stayOnPage,
 1152: 				 $width,$height).'</td></tr></table>';
 1153: 	}
 1154:     }
 1155:     my $banner_link = &update_help_link($topic,$component_help,$faq,$bug,$stayOnPage);
 1156:     return $output.$banner_link;
 1157: }
 1158: 
 1159: sub top_nav_help {
 1160:     my ($text) = @_;
 1161:     $text = &mt($text);
 1162:     my $stay_on_page = 
 1163: 	($env{'environment.remote'} eq 'off' );
 1164:     my $link = ($stay_on_page) ? "javascript:helpMenu('display')"
 1165: 	                     : "javascript:helpMenu('open')";
 1166:     my $banner_link = &update_help_link(undef,undef,undef,undef,$stay_on_page);
 1167: 
 1168:     my $title = &mt('Get help');
 1169: 
 1170:     return <<"END";
 1171: $banner_link
 1172:  <a href="$link" title="$title">$text</a>
 1173: END
 1174: }
 1175: 
 1176: sub help_menu_js {
 1177:     my ($text) = @_;
 1178: 
 1179:     my $stayOnPage = 
 1180: 	($env{'environment.remote'} eq 'off' );
 1181: 
 1182:     my $width = 620;
 1183:     my $height = 600;
 1184:     my $helptopic=&general_help();
 1185:     my $details_link = '/adm/help/'.$helptopic.'.hlp';
 1186:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
 1187:     my $start_page =
 1188:         &Apache::loncommon::start_page('Help Menu', undef,
 1189: 				       {'frameset'    => 1,
 1190: 					'js_ready'    => 1,
 1191: 					'add_entries' => {
 1192: 					    'border' => '0',
 1193: 					    'rows'   => "110,*",},});
 1194:     my $end_page =
 1195:         &Apache::loncommon::end_page({'frameset' => 1,
 1196: 				      'js_ready' => 1,});
 1197: 
 1198:     my $template .= <<"ENDTEMPLATE";
 1199: <script type="text/javascript">
 1200: // <![CDATA[
 1201: // <!-- BEGIN LON-CAPA Internal
 1202: var banner_link = '';
 1203: function helpMenu(target) {
 1204:     var caller = this;
 1205:     if (target == 'open') {
 1206:         var newWindow = null;
 1207:         try {
 1208:             newWindow =  window.open($nothing,"helpmenu","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" )
 1209:         }
 1210:         catch(error) {
 1211:             writeHelp(caller);
 1212:             return;
 1213:         }
 1214:         if (newWindow) {
 1215:             caller = newWindow;
 1216:         }
 1217:     }
 1218:     writeHelp(caller);
 1219:     return;
 1220: }
 1221: function writeHelp(caller) {
 1222:     caller.document.writeln('$start_page<frame name="bannerframe"  src="'+banner_link+'" /><frame name="bodyframe" src="$details_link" /> $end_page')
 1223:     caller.document.close()
 1224:     caller.focus()
 1225: }
 1226: // END LON-CAPA Internal -->
 1227: // ]]>
 1228: </script>
 1229: ENDTEMPLATE
 1230:     return $template;
 1231: }
 1232: 
 1233: sub help_open_bug {
 1234:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
 1235:     unless ($env{'user.adv'}) { return ''; }
 1236:     unless ($Apache::lonnet::perlvar{'BugzillaHost'}) { return ''; }
 1237:     $text = "" if (not defined $text);
 1238:     $stayOnPage = 0 if (not defined $stayOnPage);
 1239:     if ($env{'environment.remote'} eq 'off' ) {
 1240: 	$stayOnPage=1;
 1241:     }
 1242:     $width = 600 if (not defined $width);
 1243:     $height = 600 if (not defined $height);
 1244: 
 1245:     $topic=~s/\W+/\+/g;
 1246:     my $link='';
 1247:     my $template='';
 1248:     my $url=$Apache::lonnet::perlvar{'BugzillaHost'}.'enter_bug.cgi?product=LON-CAPA&amp;bug_file_loc='.
 1249: 	&escape($ENV{'REQUEST_URI'}).'&amp;component='.$topic;
 1250:     if (!$stayOnPage)
 1251:     {
 1252: 	$link = "javascript:void(open('$url', 'Bugzilla', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
 1253:     }
 1254:     else
 1255:     {
 1256: 	$link = $url;
 1257:     }
 1258:     # Add the text
 1259:     if ($text ne "")
 1260:     {
 1261: 	$template .= 
 1262:   "<table bgcolor='#AA3333' cellspacing='1' cellpadding='1' border='0'><tr>".
 1263:   "<td bgcolor='#FF5555'><a target=\"_top\" href=\"$link\"><span style=\"color:#FFFFFF;font-size:10pt;\">$text</span></a>";
 1264:     }
 1265: 
 1266:     # Add the graphic
 1267:     my $title = &mt('Report a Bug');
 1268:     my $bugicon=&lonhttpdurl("/adm/lonMisc/smallBug.gif");
 1269:     $template .= <<"ENDTEMPLATE";
 1270:  <a target="_top" href="$link" title="$title"><img src="$bugicon" border="0" alt="(Bug: $topic)" /></a>
 1271: ENDTEMPLATE
 1272:     if ($text ne '') { $template.='</td></tr></table>' };
 1273:     return $template;
 1274: 
 1275: }
 1276: 
 1277: sub help_open_faq {
 1278:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
 1279:     unless ($env{'user.adv'}) { return ''; }
 1280:     unless ($Apache::lonnet::perlvar{'FAQHost'}) { return ''; }
 1281:     $text = "" if (not defined $text);
 1282:     $stayOnPage = 0 if (not defined $stayOnPage);
 1283:     if ($env{'environment.remote'} eq 'off' ) {
 1284: 	$stayOnPage=1;
 1285:     }
 1286:     $width = 350 if (not defined $width);
 1287:     $height = 400 if (not defined $height);
 1288: 
 1289:     $topic=~s/\W+/\+/g;
 1290:     my $link='';
 1291:     my $template='';
 1292:     my $url=$Apache::lonnet::perlvar{'FAQHost'}.'/fom/cache/'.$topic.'.html';
 1293:     if (!$stayOnPage)
 1294:     {
 1295: 	$link = "javascript:void(open('$url', 'FAQ-O-Matic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
 1296:     }
 1297:     else
 1298:     {
 1299: 	$link = $url;
 1300:     }
 1301: 
 1302:     # Add the text
 1303:     if ($text ne "")
 1304:     {
 1305: 	$template .= 
 1306:   "<table bgcolor='#337733' cellspacing='1' cellpadding='1' border='0'><tr>".
 1307:   "<td bgcolor='#448844'><a target=\"_top\" href=\"$link\"><span style=\"color:#FFFFFF; font-size:10pt;\">$text</span></a>";
 1308:     }
 1309: 
 1310:     # Add the graphic
 1311:     my $title = &mt('View the FAQ');
 1312:     my $faqicon=&lonhttpdurl("/adm/lonMisc/smallFAQ.gif");
 1313:     $template .= <<"ENDTEMPLATE";
 1314:  <a target="_top" href="$link" title="$title"><img src="$faqicon" border="0" alt="(FAQ: $topic)" /></a>
 1315: ENDTEMPLATE
 1316:     if ($text ne '') { $template.='</td></tr></table>' };
 1317:     return $template;
 1318: 
 1319: }
 1320: 
 1321: ###############################################################
 1322: ###############################################################
 1323: 
 1324: =pod
 1325: 
 1326: =item * &change_content_javascript():
 1327: 
 1328: This and the next function allow you to create small sections of an
 1329: otherwise static HTML page that you can update on the fly with
 1330: Javascript, even in Netscape 4.
 1331: 
 1332: The Javascript fragment returned by this function (no E<lt>scriptE<gt> tag)
 1333: must be written to the HTML page once. It will prove the Javascript
 1334: function "change(name, content)". Calling the change function with the
 1335: name of the section 
 1336: you want to update, matching the name passed to C<changable_area>, and
 1337: the new content you want to put in there, will put the content into
 1338: that area.
 1339: 
 1340: B<Note>: Netscape 4 only reserves enough space for the changable area
 1341: to contain room for the original contents. You need to "make space"
 1342: for whatever changes you wish to make, and be B<sure> to check your
 1343: code in Netscape 4. This feature in Netscape 4 is B<not> powerful;
 1344: it's adequate for updating a one-line status display, but little more.
 1345: This script will set the space to 100% width, so you only need to
 1346: worry about height in Netscape 4.
 1347: 
 1348: Modern browsers are much less limiting, and if you can commit to the
 1349: user not using Netscape 4, this feature may be used freely with
 1350: pretty much any HTML.
 1351: 
 1352: =cut
 1353: 
 1354: sub change_content_javascript {
 1355:     # If we're on Netscape 4, we need to use Layer-based code
 1356:     if ($env{'browser.type'} eq 'netscape' &&
 1357: 	$env{'browser.version'} =~ /^4\./) {
 1358: 	return (<<NETSCAPE4);
 1359: 	function change(name, content) {
 1360: 	    doc = document.layers[name+"___escape"].layers[0].document;
 1361: 	    doc.open();
 1362: 	    doc.write(content);
 1363: 	    doc.close();
 1364: 	}
 1365: NETSCAPE4
 1366:     } else {
 1367: 	# Otherwise, we need to use semi-standards-compliant code
 1368: 	# (technically, "innerHTML" isn't standard but the equivalent
 1369: 	# is really scary, and every useful browser supports it
 1370: 	return (<<DOMBASED);
 1371: 	function change(name, content) {
 1372: 	    element = document.getElementById(name);
 1373: 	    element.innerHTML = content;
 1374: 	}
 1375: DOMBASED
 1376:     }
 1377: }
 1378: 
 1379: =pod
 1380: 
 1381: =item * &changable_area($name,$origContent):
 1382: 
 1383: This provides a "changable area" that can be modified on the fly via
 1384: the Javascript code provided in C<change_content_javascript>. $name is
 1385: the name you will use to reference the area later; do not repeat the
 1386: same name on a given HTML page more then once. $origContent is what
 1387: the area will originally contain, which can be left blank.
 1388: 
 1389: =cut
 1390: 
 1391: sub changable_area {
 1392:     my ($name, $origContent) = @_;
 1393: 
 1394:     if ($env{'browser.type'} eq 'netscape' &&
 1395: 	$env{'browser.version'} =~ /^4\./) {
 1396: 	# If this is netscape 4, we need to use the Layer tag
 1397: 	return "<ilayer width='100%' id='${name}___escape' overflow='none'><layer width='100%' id='$name' overflow='none'>$origContent</layer></ilayer>";
 1398:     } else {
 1399: 	return "<span id='$name'>$origContent</span>";
 1400:     }
 1401: }
 1402: 
 1403: =pod
 1404: 
 1405: =item * &viewport_geometry_js 
 1406: 
 1407: Provides javascript object (Geometry) which can provide information about the viewport geometry for the client browser.
 1408: 
 1409: =cut
 1410: 
 1411: 
 1412: sub viewport_geometry_js { 
 1413:     return <<"GEOMETRY";
 1414: var Geometry = {};
 1415: function init_geometry() {
 1416:     if (Geometry.init) { return };
 1417:     Geometry.init=1;
 1418:     if (window.innerHeight) {
 1419:         Geometry.getViewportHeight   = function() { return window.innerHeight; };
 1420:         Geometry.getViewportWidth   = function() { return window.innerWidth; };
 1421:         Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
 1422:         Geometry.getVerticalScroll   = function() { return window.pageYOffset; };
 1423:     }
 1424:     else if (document.documentElement && document.documentElement.clientHeight) {
 1425:         Geometry.getViewportHeight =
 1426:             function() { return document.documentElement.clientHeight; };
 1427:         Geometry.getViewportWidth =
 1428:             function() { return document.documentElement.clientWidth; };
 1429: 
 1430:         Geometry.getHorizontalScroll =
 1431:             function() { return document.documentElement.scrollLeft; };
 1432:         Geometry.getVerticalScroll =
 1433:             function() { return document.documentElement.scrollTop; };
 1434:     }
 1435:     else if (document.body.clientHeight) {
 1436:         Geometry.getViewportHeight =
 1437:             function() { return document.body.clientHeight; };
 1438:         Geometry.getViewportWidth =
 1439:             function() { return document.body.clientWidth; };
 1440:         Geometry.getHorizontalScroll =
 1441:             function() { return document.body.scrollLeft; };
 1442:         Geometry.getVerticalScroll =
 1443:             function() { return document.body.scrollTop; };
 1444:     }
 1445: }
 1446: 
 1447: GEOMETRY
 1448: }
 1449: 
 1450: =pod
 1451: 
 1452: =item * &viewport_size_js()
 1453: 
 1454: 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. 
 1455: 
 1456: =cut
 1457: 
 1458: sub viewport_size_js {
 1459:     my $geometry = &viewport_geometry_js();
 1460:     return <<"DIMS";
 1461: 
 1462: $geometry
 1463: 
 1464: function getViewportDims(width,height) {
 1465:     init_geometry();
 1466:     width.value = Geometry.getViewportWidth();
 1467:     height.value = Geometry.getViewportHeight();
 1468:     return;
 1469: }
 1470: 
 1471: DIMS
 1472: }
 1473: 
 1474: =pod
 1475: 
 1476: =item * &resize_textarea_js()
 1477: 
 1478: emits the needed javascript to resize a textarea to be as big as possible
 1479: 
 1480: creates a function resize_textrea that takes two IDs first should be
 1481: the id of the element to resize, second should be the id of a div that
 1482: surrounds everything that comes after the textarea, this routine needs
 1483: to be attached to the <body> for the onload and onresize events.
 1484: 
 1485: =back
 1486: 
 1487: =cut
 1488: 
 1489: sub resize_textarea_js {
 1490:     my $geometry = &viewport_geometry_js();
 1491:     return <<"RESIZE";
 1492:     <script type="text/javascript">
 1493: // <![CDATA[
 1494: $geometry
 1495: 
 1496: function getX(element) {
 1497:     var x = 0;
 1498:     while (element) {
 1499: 	x += element.offsetLeft;
 1500: 	element = element.offsetParent;
 1501:     }
 1502:     return x;
 1503: }
 1504: function getY(element) {
 1505:     var y = 0;
 1506:     while (element) {
 1507: 	y += element.offsetTop;
 1508: 	element = element.offsetParent;
 1509:     }
 1510:     return y;
 1511: }
 1512: 
 1513: 
 1514: function resize_textarea(textarea_id,bottom_id) {
 1515:     init_geometry();
 1516:     var textarea        = document.getElementById(textarea_id);
 1517:     //alert(textarea);
 1518: 
 1519:     var textarea_top    = getY(textarea);
 1520:     var textarea_height = textarea.offsetHeight;
 1521:     var bottom          = document.getElementById(bottom_id);
 1522:     var bottom_top      = getY(bottom);
 1523:     var bottom_height   = bottom.offsetHeight;
 1524:     var window_height   = Geometry.getViewportHeight();
 1525:     var fudge           = 23;
 1526:     var new_height      = window_height-fudge-textarea_top-bottom_height;
 1527:     if (new_height < 300) {
 1528: 	new_height = 300;
 1529:     }
 1530:     textarea.style.height=new_height+'px';
 1531: }
 1532: // ]]>
 1533: </script>
 1534: RESIZE
 1535: 
 1536: }
 1537: 
 1538: =pod
 1539: 
 1540: =head1 Excel and CSV file utility routines
 1541: 
 1542: =over 4
 1543: 
 1544: =cut
 1545: 
 1546: ###############################################################
 1547: ###############################################################
 1548: 
 1549: =pod
 1550: 
 1551: =item * &csv_translate($text) 
 1552: 
 1553: Translate $text to allow it to be output as a 'comma separated values' 
 1554: format.
 1555: 
 1556: =cut
 1557: 
 1558: ###############################################################
 1559: ###############################################################
 1560: sub csv_translate {
 1561:     my $text = shift;
 1562:     $text =~ s/\"/\"\"/g;
 1563:     $text =~ s/\n/ /g;
 1564:     return $text;
 1565: }
 1566: 
 1567: ###############################################################
 1568: ###############################################################
 1569: 
 1570: =pod
 1571: 
 1572: =item * &define_excel_formats()
 1573: 
 1574: Define some commonly used Excel cell formats.
 1575: 
 1576: Currently supported formats:
 1577: 
 1578: =over 4
 1579: 
 1580: =item header
 1581: 
 1582: =item bold
 1583: 
 1584: =item h1
 1585: 
 1586: =item h2
 1587: 
 1588: =item h3
 1589: 
 1590: =item h4
 1591: 
 1592: =item i
 1593: 
 1594: =item date
 1595: 
 1596: =back
 1597: 
 1598: Inputs: $workbook
 1599: 
 1600: Returns: $format, a hash reference.
 1601: 
 1602: =cut
 1603: 
 1604: ###############################################################
 1605: ###############################################################
 1606: sub define_excel_formats {
 1607:     my ($workbook) = @_;
 1608:     my $format;
 1609:     $format->{'header'} = $workbook->add_format(bold      => 1, 
 1610:                                                 bottom    => 1,
 1611:                                                 align     => 'center');
 1612:     $format->{'bold'} = $workbook->add_format(bold=>1);
 1613:     $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
 1614:     $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
 1615:     $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
 1616:     $format->{'h4'}   = $workbook->add_format(bold=>1, size=>12);
 1617:     $format->{'i'}    = $workbook->add_format(italic=>1);
 1618:     $format->{'date'} = $workbook->add_format(num_format=>
 1619:                                             'mm/dd/yyyy hh:mm:ss');
 1620:     return $format;
 1621: }
 1622: 
 1623: ###############################################################
 1624: ###############################################################
 1625: 
 1626: =pod
 1627: 
 1628: =item * &create_workbook()
 1629: 
 1630: Create an Excel worksheet.  If it fails, output message on the
 1631: request object and return undefs.
 1632: 
 1633: Inputs: Apache request object
 1634: 
 1635: Returns (undef) on failure, 
 1636:     Excel worksheet object, scalar with filename, and formats 
 1637:     from &Apache::loncommon::define_excel_formats on success
 1638: 
 1639: =cut
 1640: 
 1641: ###############################################################
 1642: ###############################################################
 1643: sub create_workbook {
 1644:     my ($r) = @_;
 1645:         #
 1646:     # Create the excel spreadsheet
 1647:     my $filename = '/prtspool/'.
 1648:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
 1649:         time.'_'.rand(1000000000).'.xls';
 1650:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
 1651:     if (! defined($workbook)) {
 1652:         $r->log_error("Error creating excel spreadsheet $filename: $!");
 1653:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
 1654:                             "This error has been logged.  ".
 1655:                             "Please alert your LON-CAPA administrator").
 1656:                   '</p>');
 1657:         return (undef);
 1658:     }
 1659:     #
 1660:     $workbook->set_tempdir('/home/httpd/perl/tmp');
 1661:     #
 1662:     my $format = &Apache::loncommon::define_excel_formats($workbook);
 1663:     return ($workbook,$filename,$format);
 1664: }
 1665: 
 1666: ###############################################################
 1667: ###############################################################
 1668: 
 1669: =pod
 1670: 
 1671: =item * &create_text_file()
 1672: 
 1673: Create a file to write to and eventually make available to the user.
 1674: If file creation fails, outputs an error message on the request object and 
 1675: return undefs.
 1676: 
 1677: Inputs: Apache request object, and file suffix
 1678: 
 1679: Returns (undef) on failure, 
 1680:     Filehandle and filename on success.
 1681: 
 1682: =cut
 1683: 
 1684: ###############################################################
 1685: ###############################################################
 1686: sub create_text_file {
 1687:     my ($r,$suffix) = @_;
 1688:     if (! defined($suffix)) { $suffix = 'txt'; };
 1689:     my $fh;
 1690:     my $filename = '/prtspool/'.
 1691:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
 1692:         time.'_'.rand(1000000000).'.'.$suffix;
 1693:     $fh = Apache::File->new('>/home/httpd'.$filename);
 1694:     if (! defined($fh)) {
 1695:         $r->log_error("Couldn't open $filename for output $!");
 1696:         $r->print(&mt('Problems occurred in creating the output file. '
 1697:                      .'This error has been logged. '
 1698:                      .'Please alert your LON-CAPA administrator.'));
 1699:     }
 1700:     return ($fh,$filename)
 1701: }
 1702: 
 1703: 
 1704: =pod 
 1705: 
 1706: =back
 1707: 
 1708: =cut
 1709: 
 1710: ###############################################################
 1711: ##        Home server <option> list generating code          ##
 1712: ###############################################################
 1713: 
 1714: # ------------------------------------------
 1715: 
 1716: sub domain_select {
 1717:     my ($name,$value,$multiple)=@_;
 1718:     my %domains=map { 
 1719: 	$_ => $_.' '. &Apache::lonnet::domain($_,'description') 
 1720:     } &Apache::lonnet::all_domains();
 1721:     if ($multiple) {
 1722: 	$domains{''}=&mt('Any domain');
 1723: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
 1724: 	return &multiple_select_form($name,$value,4,\%domains);
 1725:     } else {
 1726: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
 1727: 	return &select_form($name,$value,%domains);
 1728:     }
 1729: }
 1730: 
 1731: #-------------------------------------------
 1732: 
 1733: =pod
 1734: 
 1735: =head1 Routines for form select boxes
 1736: 
 1737: =over 4
 1738: 
 1739: =item * &multiple_select_form($name,$value,$size,$hash,$order)
 1740: 
 1741: Returns a string containing a <select> element int multiple mode
 1742: 
 1743: 
 1744: Args:
 1745:   $name - name of the <select> element
 1746:   $value - scalar or array ref of values that should already be selected
 1747:   $size - number of rows long the select element is
 1748:   $hash - the elements should be 'option' => 'shown text'
 1749:           (shown text should already have been &mt())
 1750:   $order - (optional) array ref of the order to show the elements in
 1751: 
 1752: =cut
 1753: 
 1754: #-------------------------------------------
 1755: sub multiple_select_form {
 1756:     my ($name,$value,$size,$hash,$order)=@_;
 1757:     my %selected = map { $_ => 1 } ref($value)?@{$value}:($value);
 1758:     my $output='';
 1759:     if (! defined($size)) {
 1760:         $size = 4;
 1761:         if (scalar(keys(%$hash))<4) {
 1762:             $size = scalar(keys(%$hash));
 1763:         }
 1764:     }
 1765:     $output.="\n".'<select name="'.$name.'" size="'.$size.'" multiple="multiple">';
 1766:     my @order;
 1767:     if (ref($order) eq 'ARRAY')  {
 1768:         @order = @{$order};
 1769:     } else {
 1770:         @order = sort(keys(%$hash));
 1771:     }
 1772:     if (exists($$hash{'select_form_order'})) {
 1773:         @order = @{$$hash{'select_form_order'}};
 1774:     }
 1775:         
 1776:     foreach my $key (@order) {
 1777:         $output.='<option value="'.&HTML::Entities::encode($key,'"<>&').'" ';
 1778:         $output.='selected="selected" ' if ($selected{$key});
 1779:         $output.='>'.$hash->{$key}."</option>\n";
 1780:     }
 1781:     $output.="</select>\n";
 1782:     return $output;
 1783: }
 1784: 
 1785: #-------------------------------------------
 1786: 
 1787: =pod
 1788: 
 1789: =item * &select_form($defdom,$name,%hash)
 1790: 
 1791: Returns a string containing a <select name='$name' size='1'> form to 
 1792: allow a user to select options from a hash option_name => displayed text.  
 1793: See lonrights.pm for an example invocation and use.
 1794: 
 1795: =cut
 1796: 
 1797: #-------------------------------------------
 1798: sub select_form {
 1799:     my ($def,$name,%hash) = @_;
 1800:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
 1801:     my @keys;
 1802:     if (exists($hash{'select_form_order'})) {
 1803: 	@keys=@{$hash{'select_form_order'}};
 1804:     } else {
 1805: 	@keys=sort(keys(%hash));
 1806:     }
 1807:     foreach my $key (@keys) {
 1808:         $selectform.=
 1809: 	    '<option value="'.&HTML::Entities::encode($key,'"<>&').'" '.
 1810:             ($key eq $def ? 'selected="selected" ' : '').
 1811:                 ">".&mt($hash{$key})."</option>\n";
 1812:     }
 1813:     $selectform.="</select>";
 1814:     return $selectform;
 1815: }
 1816: 
 1817: # For display filters
 1818: 
 1819: sub display_filter {
 1820:     if (!$env{'form.show'}) { $env{'form.show'}=10; }
 1821:     if (!$env{'form.displayfilter'}) { $env{'form.displayfilter'}='currentfolder'; }
 1822:     return '<span class="LC_nobreak"><label>'.&mt('Records [_1]',
 1823: 			       &Apache::lonmeta::selectbox('show',$env{'form.show'},undef,
 1824: 							   (&mt('all'),10,20,50,100,1000,10000))).
 1825: 	   '</label></span> <span class="LC_nobreak">'.
 1826:            &mt('Filter [_1]',
 1827: 	   &select_form($env{'form.displayfilter'},
 1828: 			'displayfilter',
 1829: 			('currentfolder' => 'Current folder/page',
 1830: 			 'containing' => 'Containing phrase',
 1831: 			 'none' => 'None'))).
 1832: 			 '<input type="text" name="containingphrase" size="30" value="'.&HTML::Entities::encode($env{'form.containingphrase'}).'" /></span>';
 1833: }
 1834: 
 1835: sub gradeleveldescription {
 1836:     my $gradelevel=shift;
 1837:     my %gradelevels=(0 => 'Not specified',
 1838: 		     1 => 'Grade 1',
 1839: 		     2 => 'Grade 2',
 1840: 		     3 => 'Grade 3',
 1841: 		     4 => 'Grade 4',
 1842: 		     5 => 'Grade 5',
 1843: 		     6 => 'Grade 6',
 1844: 		     7 => 'Grade 7',
 1845: 		     8 => 'Grade 8',
 1846: 		     9 => 'Grade 9',
 1847: 		     10 => 'Grade 10',
 1848: 		     11 => 'Grade 11',
 1849: 		     12 => 'Grade 12',
 1850: 		     13 => 'Grade 13',
 1851: 		     14 => '100 Level',
 1852: 		     15 => '200 Level',
 1853: 		     16 => '300 Level',
 1854: 		     17 => '400 Level',
 1855: 		     18 => 'Graduate Level');
 1856:     return &mt($gradelevels{$gradelevel});
 1857: }
 1858: 
 1859: sub select_level_form {
 1860:     my ($deflevel,$name)=@_;
 1861:     unless ($deflevel) { $deflevel=0; }
 1862:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
 1863:     for (my $i=0; $i<=18; $i++) {
 1864:         $selectform.="<option value=\"$i\" ".
 1865:             ($i==$deflevel ? 'selected="selected" ' : '').
 1866:                 ">".&gradeleveldescription($i)."</option>\n";
 1867:     }
 1868:     $selectform.="</select>";
 1869:     return $selectform;
 1870: }
 1871: 
 1872: #-------------------------------------------
 1873: 
 1874: =pod
 1875: 
 1876: =item * &select_dom_form($defdom,$name,$includeempty,$showdomdesc,$onchange)
 1877: 
 1878: Returns a string containing a <select name='$name' size='1'> form to 
 1879: allow a user to select the domain to preform an operation in.  
 1880: See loncreateuser.pm for an example invocation and use.
 1881: 
 1882: If the $includeempty flag is set, it also includes an empty choice ("no domain
 1883: selected");
 1884: 
 1885: If the $showdomdesc flag is set, the domain name is followed by the domain description.
 1886: 
 1887: The optional $onchange argumnet specifies what should occur if the domain selector is changed, e.g., 'this.form.submit()' if the form is to be automatically submitted.  
 1888: 
 1889: =cut
 1890: 
 1891: #-------------------------------------------
 1892: sub select_dom_form {
 1893:     my ($defdom,$name,$includeempty,$showdomdesc,$onchange) = @_;
 1894:     if ($onchange) {
 1895:         $onchange = ' onchange="'.$onchange.'"';
 1896:     }
 1897:     my @domains = sort {lc($a) cmp lc($b)} (&Apache::lonnet::all_domains());
 1898:     if ($includeempty) { @domains=('',@domains); }
 1899:     my $selectdomain = "<select name=\"$name\" size=\"1\"$onchange>\n";
 1900:     foreach my $dom (@domains) {
 1901:         $selectdomain.="<option value=\"$dom\" ".
 1902:             ($dom eq $defdom ? 'selected="selected" ' : '').'>'.$dom;
 1903:         if ($showdomdesc) {
 1904:             if ($dom ne '') {
 1905:                 my $domdesc = &Apache::lonnet::domain($dom,'description');
 1906:                 if ($domdesc ne '') {
 1907:                     $selectdomain .= ' ('.$domdesc.')';
 1908:                 }
 1909:             } 
 1910:         }
 1911:         $selectdomain .= "</option>\n";
 1912:     }
 1913:     $selectdomain.="</select>";
 1914:     return $selectdomain;
 1915: }
 1916: 
 1917: #-------------------------------------------
 1918: 
 1919: =pod
 1920: 
 1921: =item * &home_server_form_item($domain,$name,$defaultflag)
 1922: 
 1923: input: 4 arguments (two required, two optional) - 
 1924:     $domain - domain of new user
 1925:     $name - name of form element
 1926:     $default - Value of 'default' causes a default item to be first 
 1927:                             option, and selected by default. 
 1928:     $hide - Value of 'hide' causes hiding of the name of the server, 
 1929:                             if 1 server found, or default, if 0 found.
 1930: output: returns 2 items: 
 1931: (a) form element which contains either:
 1932:    (i) <select name="$name">
 1933:         <option value="$hostid1">$hostid $servers{$hostid}</option>
 1934:         <option value="$hostid2">$hostid $servers{$hostid}</option>       
 1935:        </select>
 1936:        form item if there are multiple library servers in $domain, or
 1937:    (ii) an <input type="hidden" name="$name" value="$hostid" /> form item 
 1938:        if there is only one library server in $domain.
 1939: 
 1940: (b) number of library servers found.
 1941: 
 1942: See loncreateuser.pm for example of use.
 1943: 
 1944: =cut
 1945: 
 1946: #-------------------------------------------
 1947: sub home_server_form_item {
 1948:     my ($domain,$name,$default,$hide) = @_;
 1949:     my %servers = &Apache::lonnet::get_servers($domain,'library');
 1950:     my $result;
 1951:     my $numlib = keys(%servers);
 1952:     if ($numlib > 1) {
 1953:         $result .= '<select name="'.$name.'" />'."\n";
 1954:         if ($default) {
 1955:             $result .= '<option value="default" selected="selected">'.&mt('default').
 1956:                        '</option>'."\n";
 1957:         }
 1958:         foreach my $hostid (sort(keys(%servers))) {
 1959:             $result.= '<option value="'.$hostid.'">'.
 1960: 	              $hostid.' '.$servers{$hostid}."</option>\n";
 1961:         }
 1962:         $result .= '</select>'."\n";
 1963:     } elsif ($numlib == 1) {
 1964:         my $hostid;
 1965:         foreach my $item (keys(%servers)) {
 1966:             $hostid = $item;
 1967:         }
 1968:         $result .= '<input type="hidden" name="'.$name.'" value="'.
 1969:                    $hostid.'" />';
 1970:                    if (!$hide) {
 1971:                        $result .= $hostid.' '.$servers{$hostid};
 1972:                    }
 1973:                    $result .= "\n";
 1974:     } elsif ($default) {
 1975:         $result .= '<input type="hidden" name="'.$name.
 1976:                    '" value="default" />';
 1977:                    if (!$hide) {
 1978:                        $result .= &mt('default');
 1979:                    }
 1980:                    $result .= "\n";
 1981:     }
 1982:     return ($result,$numlib);
 1983: }
 1984: 
 1985: =pod
 1986: 
 1987: =back 
 1988: 
 1989: =cut
 1990: 
 1991: ###############################################################
 1992: ##                  Decoding User Agent                      ##
 1993: ###############################################################
 1994: 
 1995: =pod
 1996: 
 1997: =head1 Decoding the User Agent
 1998: 
 1999: =over 4
 2000: 
 2001: =item * &decode_user_agent()
 2002: 
 2003: Inputs: $r
 2004: 
 2005: Outputs:
 2006: 
 2007: =over 4
 2008: 
 2009: =item * $httpbrowser
 2010: 
 2011: =item * $clientbrowser
 2012: 
 2013: =item * $clientversion
 2014: 
 2015: =item * $clientmathml
 2016: 
 2017: =item * $clientunicode
 2018: 
 2019: =item * $clientos
 2020: 
 2021: =back
 2022: 
 2023: =back 
 2024: 
 2025: =cut
 2026: 
 2027: ###############################################################
 2028: ###############################################################
 2029: sub decode_user_agent {
 2030:     my ($r)=@_;
 2031:     my @browsertype=split(/\&/,$Apache::lonnet::perlvar{"lonBrowsDet"});
 2032:     my %mathcap=split(/\&/,$$Apache::lonnet::perlvar{"lonMathML"});
 2033:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
 2034:     if (!$httpbrowser && $r) { $httpbrowser=$r->header_in('User-Agent'); }
 2035:     my $clientbrowser='unknown';
 2036:     my $clientversion='0';
 2037:     my $clientmathml='';
 2038:     my $clientunicode='0';
 2039:     for (my $i=0;$i<=$#browsertype;$i++) {
 2040:         my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\:/,$browsertype[$i]);
 2041: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
 2042: 	    $clientbrowser=$bname;
 2043:             $httpbrowser=~/$vreg/i;
 2044: 	    $clientversion=$1;
 2045:             $clientmathml=($clientversion>=$minv);
 2046:             $clientunicode=($clientversion>=$univ);
 2047: 	}
 2048:     }
 2049:     my $clientos='unknown';
 2050:     if (($httpbrowser=~/linux/i) ||
 2051:         ($httpbrowser=~/unix/i) ||
 2052:         ($httpbrowser=~/ux/i) ||
 2053:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
 2054:     if (($httpbrowser=~/vax/i) ||
 2055:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
 2056:     if ($httpbrowser=~/next/i) { $clientos='next'; }
 2057:     if (($httpbrowser=~/mac/i) ||
 2058:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
 2059:     if ($httpbrowser=~/win/i) { $clientos='win'; }
 2060:     if ($httpbrowser=~/embed/i) { $clientos='pda'; }
 2061:     return ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
 2062:             $clientunicode,$clientos,);
 2063: }
 2064: 
 2065: ###############################################################
 2066: ##    Authentication changing form generation subroutines    ##
 2067: ###############################################################
 2068: ##
 2069: ## All of the authform_xxxxxxx subroutines take their inputs in a
 2070: ## hash, and have reasonable default values.
 2071: ##
 2072: ##    formname = the name given in the <form> tag.
 2073: #-------------------------------------------
 2074: 
 2075: =pod
 2076: 
 2077: =head1 Authentication Routines
 2078: 
 2079: =over 4
 2080: 
 2081: =item * &authform_xxxxxx()
 2082: 
 2083: The authform_xxxxxx subroutines provide javascript and html forms which 
 2084: handle some of the conveniences required for authentication forms.  
 2085: This is not an optimal method, but it works.  
 2086: 
 2087: =over 4
 2088: 
 2089: =item * authform_header
 2090: 
 2091: =item * authform_authorwarning
 2092: 
 2093: =item * authform_nochange
 2094: 
 2095: =item * authform_kerberos
 2096: 
 2097: =item * authform_internal
 2098: 
 2099: =item * authform_filesystem
 2100: 
 2101: =back
 2102: 
 2103: See loncreateuser.pm for invocation and use examples.
 2104: 
 2105: =cut
 2106: 
 2107: #-------------------------------------------
 2108: sub authform_header{  
 2109:     my %in = (
 2110:         formname => 'cu',
 2111:         kerb_def_dom => '',
 2112:         @_,
 2113:     );
 2114:     $in{'formname'} = 'document.' . $in{'formname'};
 2115:     my $result='';
 2116: 
 2117: #---------------------------------------------- Code for upper case translation
 2118:     my $Javascript_toUpperCase;
 2119:     unless ($in{kerb_def_dom}) {
 2120:         $Javascript_toUpperCase =<<"END";
 2121:         switch (choice) {
 2122:            case 'krb': currentform.elements[choicearg].value =
 2123:                currentform.elements[choicearg].value.toUpperCase();
 2124:                break;
 2125:            default:
 2126:         }
 2127: END
 2128:     } else {
 2129:         $Javascript_toUpperCase = "";
 2130:     }
 2131: 
 2132:     my $radioval = "'nochange'";
 2133:     if (defined($in{'curr_authtype'})) {
 2134:         if ($in{'curr_authtype'} ne '') {
 2135:             $radioval = "'".$in{'curr_authtype'}."arg'";
 2136:         }
 2137:     }
 2138:     my $argfield = 'null';
 2139:     if (defined($in{'mode'})) {
 2140:         if ($in{'mode'} eq 'modifycourse')  {
 2141:             if (defined($in{'curr_autharg'})) {
 2142:                 if ($in{'curr_autharg'} ne '') {
 2143:                     $argfield = "'$in{'curr_autharg'}'";
 2144:                 }
 2145:             }
 2146:         }
 2147:     }
 2148: 
 2149:     $result.=<<"END";
 2150: var current = new Object();
 2151: current.radiovalue = $radioval;
 2152: current.argfield = $argfield;
 2153: 
 2154: function changed_radio(choice,currentform) {
 2155:     var choicearg = choice + 'arg';
 2156:     // If a radio button in changed, we need to change the argfield
 2157:     if (current.radiovalue != choice) {
 2158:         current.radiovalue = choice;
 2159:         if (current.argfield != null) {
 2160:             currentform.elements[current.argfield].value = '';
 2161:         }
 2162:         if (choice == 'nochange') {
 2163:             current.argfield = null;
 2164:         } else {
 2165:             current.argfield = choicearg;
 2166:             switch(choice) {
 2167:                 case 'krb': 
 2168:                     currentform.elements[current.argfield].value = 
 2169:                         "$in{'kerb_def_dom'}";
 2170:                 break;
 2171:               default:
 2172:                 break;
 2173:             }
 2174:         }
 2175:     }
 2176:     return;
 2177: }
 2178: 
 2179: function changed_text(choice,currentform) {
 2180:     var choicearg = choice + 'arg';
 2181:     if (currentform.elements[choicearg].value !='') {
 2182:         $Javascript_toUpperCase
 2183:         // clear old field
 2184:         if ((current.argfield != choicearg) && (current.argfield != null)) {
 2185:             currentform.elements[current.argfield].value = '';
 2186:         }
 2187:         current.argfield = choicearg;
 2188:     }
 2189:     set_auth_radio_buttons(choice,currentform);
 2190:     return;
 2191: }
 2192: 
 2193: function set_auth_radio_buttons(newvalue,currentform) {
 2194:     var i=0;
 2195:     while (i < currentform.login.length) {
 2196:         if (currentform.login[i].value == newvalue) { break; }
 2197:         i++;
 2198:     }
 2199:     if (i == currentform.login.length) {
 2200:         return;
 2201:     }
 2202:     current.radiovalue = newvalue;
 2203:     currentform.login[i].checked = true;
 2204:     return;
 2205: }
 2206: END
 2207:     return $result;
 2208: }
 2209: 
 2210: sub authform_authorwarning{
 2211:     my $result='';
 2212:     $result='<i>'.
 2213:         &mt('As a general rule, only authors or co-authors should be '.
 2214:             'filesystem authenticated '.
 2215:             '(which allows access to the server filesystem).')."</i>\n";
 2216:     return $result;
 2217: }
 2218: 
 2219: sub authform_nochange{  
 2220:     my %in = (
 2221:               formname => 'document.cu',
 2222:               kerb_def_dom => 'MSU.EDU',
 2223:               @_,
 2224:           );
 2225:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'}); 
 2226:     my $result;
 2227:     if (keys(%can_assign) == 0) {
 2228:         $result = &mt('Under you current role you are not permitted to change login settings for this user');  
 2229:     } else {
 2230:         $result = '<label>'.&mt('[_1] Do not change login data',
 2231:                   '<input type="radio" name="login" value="nochange" '.
 2232:                   'checked="checked" onclick="'.
 2233:             "javascript:changed_radio('nochange',$in{'formname'});".'" />').
 2234: 	    '</label>';
 2235:     }
 2236:     return $result;
 2237: }
 2238: 
 2239: sub authform_kerberos {
 2240:     my %in = (
 2241:               formname => 'document.cu',
 2242:               kerb_def_dom => 'MSU.EDU',
 2243:               kerb_def_auth => 'krb4',
 2244:               @_,
 2245:               );
 2246:     my ($check4,$check5,$krbcheck,$krbarg,$krbver,$result,$authtype,
 2247:         $autharg,$jscall);
 2248:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
 2249:     if ($in{'kerb_def_auth'} eq 'krb5') {
 2250:        $check5 = ' checked="checked"';
 2251:     } else {
 2252:        $check4 = ' checked="checked"';
 2253:     }
 2254:     $krbarg = $in{'kerb_def_dom'};
 2255:     if (defined($in{'curr_authtype'})) {
 2256:         if ($in{'curr_authtype'} eq 'krb') {
 2257:             $krbcheck = ' checked="checked"';
 2258:             if (defined($in{'mode'})) {
 2259:                 if ($in{'mode'} eq 'modifyuser') {
 2260:                     $krbcheck = '';
 2261:                 }
 2262:             }
 2263:             if (defined($in{'curr_kerb_ver'})) {
 2264:                 if ($in{'curr_krb_ver'} eq '5') {
 2265:                     $check5 = ' checked="checked"';
 2266:                     $check4 = '';
 2267:                 } else {
 2268:                     $check4 = ' checked="checked"';
 2269:                     $check5 = '';
 2270:                 }
 2271:             }
 2272:             if (defined($in{'curr_autharg'})) {
 2273:                 $krbarg = $in{'curr_autharg'};
 2274:             }
 2275:             if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
 2276:                 if (defined($in{'curr_autharg'})) {
 2277:                     $result = 
 2278:     &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
 2279:         $in{'curr_autharg'},$krbver);
 2280:                 } else {
 2281:                     $result =
 2282:     &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
 2283:                 }
 2284:                 return $result; 
 2285:             }
 2286:         }
 2287:     } else {
 2288:         if ($authnum == 1) {
 2289:             $authtype = '<input type="hidden" name="login" value="krb" />';
 2290:         }
 2291:     }
 2292:     if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
 2293:         return;
 2294:     } elsif ($authtype eq '') {
 2295:         if (defined($in{'mode'})) {
 2296:             if ($in{'mode'} eq 'modifycourse') {
 2297:                 if ($authnum == 1) {
 2298:                     $authtype = '<input type="hidden" name="login" value="krb" />';
 2299:                 }
 2300:             }
 2301:         }
 2302:     }
 2303:     $jscall = "javascript:changed_radio('krb',$in{'formname'});";
 2304:     if ($authtype eq '') {
 2305:         $authtype = '<input type="radio" name="login" value="krb" '.
 2306:                     'onclick="'.$jscall.'" onchange="'.$jscall.'"'.
 2307:                     $krbcheck.' />';
 2308:     }
 2309:     if (($can_assign{'krb4'} && $can_assign{'krb5'}) ||
 2310:         ($can_assign{'krb4'} && !$can_assign{'krb5'} && 
 2311:          $in{'curr_authtype'} eq 'krb5') ||
 2312:         (!$can_assign{'krb4'} && $can_assign{'krb5'} && 
 2313:          $in{'curr_authtype'} eq 'krb4')) {
 2314:         $result .= &mt
 2315:         ('[_1] Kerberos authenticated with domain [_2] '.
 2316:          '[_3] Version 4 [_4] Version 5 [_5]',
 2317:          '<label>'.$authtype,
 2318:          '</label><input type="text" size="10" name="krbarg" '.
 2319:              'value="'.$krbarg.'" '.
 2320:              'onchange="'.$jscall.'" />',
 2321:          '<label><input type="radio" name="krbver" value="4" '.$check4.' />',
 2322:          '</label><label><input type="radio" name="krbver" value="5" '.$check5.' />',
 2323: 	 '</label>');
 2324:     } elsif ($can_assign{'krb4'}) {
 2325:         $result .= &mt
 2326:         ('[_1] Kerberos authenticated with domain [_2] '.
 2327:          '[_3] Version 4 [_4]',
 2328:          '<label>'.$authtype,
 2329:          '</label><input type="text" size="10" name="krbarg" '.
 2330:              'value="'.$krbarg.'" '.
 2331:              'onchange="'.$jscall.'" />',
 2332:          '<label><input type="hidden" name="krbver" value="4" />',
 2333:          '</label>');
 2334:     } elsif ($can_assign{'krb5'}) {
 2335:         $result .= &mt
 2336:         ('[_1] Kerberos authenticated with domain [_2] '.
 2337:          '[_3] Version 5 [_4]',
 2338:          '<label>'.$authtype,
 2339:          '</label><input type="text" size="10" name="krbarg" '.
 2340:              'value="'.$krbarg.'" '.
 2341:              'onchange="'.$jscall.'" />',
 2342:          '<label><input type="hidden" name="krbver" value="5" />',
 2343:          '</label>');
 2344:     }
 2345:     return $result;
 2346: }
 2347: 
 2348: sub authform_internal{  
 2349:     my %in = (
 2350:                 formname => 'document.cu',
 2351:                 kerb_def_dom => 'MSU.EDU',
 2352:                 @_,
 2353:                 );
 2354:     my ($intcheck,$intarg,$result,$authtype,$autharg,$jscall);
 2355:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
 2356:     if (defined($in{'curr_authtype'})) {
 2357:         if ($in{'curr_authtype'} eq 'int') {
 2358:             if ($can_assign{'int'}) {
 2359:                 $intcheck = 'checked="checked" ';
 2360:                 if (defined($in{'mode'})) {
 2361:                     if ($in{'mode'} eq 'modifyuser') {
 2362:                         $intcheck = '';
 2363:                     }
 2364:                 }
 2365:                 if (defined($in{'curr_autharg'})) {
 2366:                     $intarg = $in{'curr_autharg'};
 2367:                 }
 2368:             } else {
 2369:                 $result = &mt('Currently internally authenticated.');
 2370:                 return $result;
 2371:             }
 2372:         }
 2373:     } else {
 2374:         if ($authnum == 1) {
 2375:             $authtype = '<input type="hidden" name="login" value="int" />';
 2376:         }
 2377:     }
 2378:     if (!$can_assign{'int'}) {
 2379:         return;
 2380:     } elsif ($authtype eq '') {
 2381:         if (defined($in{'mode'})) {
 2382:             if ($in{'mode'} eq 'modifycourse') {
 2383:                 if ($authnum == 1) {
 2384:                     $authtype = '<input type="hidden" name="login" value="int" />';
 2385:                 }
 2386:             }
 2387:         }
 2388:     }
 2389:     $jscall = "javascript:changed_radio('int',$in{'formname'});";
 2390:     if ($authtype eq '') {
 2391:         $authtype = '<input type="radio" name="login" value="int" '.$intcheck.
 2392:                     ' onchange="'.$jscall.'" onclick="'.$jscall.'" />';
 2393:     }
 2394:     $autharg = '<input type="password" size="10" name="intarg" value="'.
 2395:                $intarg.'" onchange="'.$jscall.'" />';
 2396:     $result = &mt
 2397:         ('[_1] Internally authenticated (with initial password [_2])',
 2398:          '<label>'.$authtype,'</label>'.$autharg);
 2399:     $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>';
 2400:     return $result;
 2401: }
 2402: 
 2403: sub authform_local{  
 2404:     my %in = (
 2405:               formname => 'document.cu',
 2406:               kerb_def_dom => 'MSU.EDU',
 2407:               @_,
 2408:               );
 2409:     my ($loccheck,$locarg,$result,$authtype,$autharg,$jscall);
 2410:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
 2411:     if (defined($in{'curr_authtype'})) {
 2412:         if ($in{'curr_authtype'} eq 'loc') {
 2413:             if ($can_assign{'loc'}) {
 2414:                 $loccheck = 'checked="checked" ';
 2415:                 if (defined($in{'mode'})) {
 2416:                     if ($in{'mode'} eq 'modifyuser') {
 2417:                         $loccheck = '';
 2418:                     }
 2419:                 }
 2420:                 if (defined($in{'curr_autharg'})) {
 2421:                     $locarg = $in{'curr_autharg'};
 2422:                 }
 2423:             } else {
 2424:                 $result = &mt('Currently using local (institutional) authentication.');
 2425:                 return $result;
 2426:             }
 2427:         }
 2428:     } else {
 2429:         if ($authnum == 1) {
 2430:             $authtype = '<input type="hidden" name="login" value="loc" />';
 2431:         }
 2432:     }
 2433:     if (!$can_assign{'loc'}) {
 2434:         return;
 2435:     } elsif ($authtype eq '') {
 2436:         if (defined($in{'mode'})) {
 2437:             if ($in{'mode'} eq 'modifycourse') {
 2438:                 if ($authnum == 1) {
 2439:                     $authtype = '<input type="hidden" name="login" value="loc" />';
 2440:                 }
 2441:             }
 2442:         }
 2443:     }
 2444:     $jscall = "javascript:changed_radio('loc',$in{'formname'});";
 2445:     if ($authtype eq '') {
 2446:         $authtype = '<input type="radio" name="login" value="loc" '.
 2447:                     $loccheck.' onchange="'.$jscall.'" onclick="'.
 2448:                     $jscall.'" />';
 2449:     }
 2450:     $autharg = '<input type="text" size="10" name="locarg" value="'.
 2451:                $locarg.'" onchange="'.$jscall.'" />';
 2452:     $result = &mt('[_1] Local Authentication with argument [_2]',
 2453:                   '<label>'.$authtype,'</label>'.$autharg);
 2454:     return $result;
 2455: }
 2456: 
 2457: sub authform_filesystem{  
 2458:     my %in = (
 2459:               formname => 'document.cu',
 2460:               kerb_def_dom => 'MSU.EDU',
 2461:               @_,
 2462:               );
 2463:     my ($fsyscheck,$result,$authtype,$autharg,$jscall);
 2464:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
 2465:     if (defined($in{'curr_authtype'})) {
 2466:         if ($in{'curr_authtype'} eq 'fsys') {
 2467:             if ($can_assign{'fsys'}) {
 2468:                 $fsyscheck = 'checked="checked" ';
 2469:                 if (defined($in{'mode'})) {
 2470:                     if ($in{'mode'} eq 'modifyuser') {
 2471:                         $fsyscheck = '';
 2472:                     }
 2473:                 }
 2474:             } else {
 2475:                 $result = &mt('Currently Filesystem Authenticated.');
 2476:                 return $result;
 2477:             }           
 2478:         }
 2479:     } else {
 2480:         if ($authnum == 1) {
 2481:             $authtype = '<input type="hidden" name="login" value="fsys" />';
 2482:         }
 2483:     }
 2484:     if (!$can_assign{'fsys'}) {
 2485:         return;
 2486:     } elsif ($authtype eq '') {
 2487:         if (defined($in{'mode'})) {
 2488:             if ($in{'mode'} eq 'modifycourse') {
 2489:                 if ($authnum == 1) {
 2490:                     $authtype = '<input type="hidden" name="login" value="fsys" />';
 2491:                 }
 2492:             }
 2493:         }
 2494:     }
 2495:     $jscall = "javascript:changed_radio('fsys',$in{'formname'});";
 2496:     if ($authtype eq '') {
 2497:         $authtype = '<input type="radio" name="login" value="fsys" '.
 2498:                     $fsyscheck.' onchange="'.$jscall.'" onclick="'.
 2499:                     $jscall.'" />';
 2500:     }
 2501:     $autharg = '<input type="text" size="10" name="fsysarg" value=""'.
 2502:                ' onchange="'.$jscall.'" />';
 2503:     $result = &mt
 2504:         ('[_1] Filesystem Authenticated (with initial password [_2])',
 2505:          '<label><input type="radio" name="login" value="fsys" '.
 2506:          $fsyscheck.'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
 2507:          '</label><input type="password" size="10" name="fsysarg" value="" '.
 2508:                   'onchange="'.$jscall.'" />');
 2509:     return $result;
 2510: }
 2511: 
 2512: sub get_assignable_auth {
 2513:     my ($dom) = @_;
 2514:     if ($dom eq '') {
 2515:         $dom = $env{'request.role.domain'};
 2516:     }
 2517:     my %can_assign = (
 2518:                           krb4 => 1,
 2519:                           krb5 => 1,
 2520:                           int  => 1,
 2521:                           loc  => 1,
 2522:                      );
 2523:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
 2524:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 2525:         if (ref($domconfig{'usercreation'}{'authtypes'}) eq 'HASH') {
 2526:             my $authhash = $domconfig{'usercreation'}{'authtypes'};
 2527:             my $context;
 2528:             if ($env{'request.role'} =~ /^au/) {
 2529:                 $context = 'author';
 2530:             } elsif ($env{'request.role'} =~ /^dc/) {
 2531:                 $context = 'domain';
 2532:             } elsif ($env{'request.course.id'}) {
 2533:                 $context = 'course';
 2534:             }
 2535:             if ($context) {
 2536:                 if (ref($authhash->{$context}) eq 'HASH') {
 2537:                    %can_assign = %{$authhash->{$context}}; 
 2538:                 }
 2539:             }
 2540:         }
 2541:     }
 2542:     my $authnum = 0;
 2543:     foreach my $key (keys(%can_assign)) {
 2544:         if ($can_assign{$key}) {
 2545:             $authnum ++;
 2546:         }
 2547:     }
 2548:     if ($can_assign{'krb4'} && $can_assign{'krb5'}) {
 2549:         $authnum --;
 2550:     }
 2551:     return ($authnum,%can_assign);
 2552: }
 2553: 
 2554: ###############################################################
 2555: ##    Get Kerberos Defaults for Domain                 ##
 2556: ###############################################################
 2557: ##
 2558: ## Returns default kerberos version and an associated argument
 2559: ## as listed in file domain.tab. If not listed, provides
 2560: ## appropriate default domain and kerberos version.
 2561: ##
 2562: #-------------------------------------------
 2563: 
 2564: =pod
 2565: 
 2566: =item * &get_kerberos_defaults()
 2567: 
 2568: get_kerberos_defaults($target_domain) returns the default kerberos
 2569: version and domain. If not found, it defaults to version 4 and the 
 2570: domain of the server.
 2571: 
 2572: =over 4
 2573: 
 2574: ($def_version, $def_krb_domain) = &get_kerberos_defaults($target_domain);
 2575: 
 2576: =back
 2577: 
 2578: =back
 2579: 
 2580: =cut
 2581: 
 2582: #-------------------------------------------
 2583: sub get_kerberos_defaults {
 2584:     my $domain=shift;
 2585:     my ($krbdef,$krbdefdom);
 2586:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
 2587:     if (($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) {
 2588:         $krbdef = $domdefaults{'auth_def'};
 2589:         $krbdefdom = $domdefaults{'auth_arg_def'};
 2590:     } else {
 2591:         $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
 2592:         my $krbdefdom=$1;
 2593:         $krbdefdom=~tr/a-z/A-Z/;
 2594:         $krbdef = "krb4";
 2595:     }
 2596:     return ($krbdef,$krbdefdom);
 2597: }
 2598: 
 2599: 
 2600: ###############################################################
 2601: ##                Thesaurus Functions                        ##
 2602: ###############################################################
 2603: 
 2604: =pod
 2605: 
 2606: =head1 Thesaurus Functions
 2607: 
 2608: =over 4
 2609: 
 2610: =item * &initialize_keywords()
 2611: 
 2612: Initializes the package variable %Keywords if it is empty.  Uses the
 2613: package variable $thesaurus_db_file.
 2614: 
 2615: =cut
 2616: 
 2617: ###################################################
 2618: 
 2619: sub initialize_keywords {
 2620:     return 1 if (scalar keys(%Keywords));
 2621:     # If we are here, %Keywords is empty, so fill it up
 2622:     #   Make sure the file we need exists...
 2623:     if (! -e $thesaurus_db_file) {
 2624:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file".
 2625:                                  " failed because it does not exist");
 2626:         return 0;
 2627:     }
 2628:     #   Set up the hash as a database
 2629:     my %thesaurus_db;
 2630:     if (! tie(%thesaurus_db,'GDBM_File',
 2631:               $thesaurus_db_file,&GDBM_READER(),0640)){
 2632:         &Apache::lonnet::logthis("Could not tie \%thesaurus_db to ".
 2633:                                  $thesaurus_db_file);
 2634:         return 0;
 2635:     } 
 2636:     #  Get the average number of appearances of a word.
 2637:     my $avecount = $thesaurus_db{'average.count'};
 2638:     #  Put keywords (those that appear > average) into %Keywords
 2639:     while (my ($word,$data)=each (%thesaurus_db)) {
 2640:         my ($count,undef) = split /:/,$data;
 2641:         $Keywords{$word}++ if ($count > $avecount);
 2642:     }
 2643:     untie %thesaurus_db;
 2644:     # Remove special values from %Keywords.
 2645:     foreach my $value ('total.count','average.count') {
 2646:         delete($Keywords{$value}) if (exists($Keywords{$value}));
 2647:   }
 2648:     return 1;
 2649: }
 2650: 
 2651: ###################################################
 2652: 
 2653: =pod
 2654: 
 2655: =item * &keyword($word)
 2656: 
 2657: Returns true if $word is a keyword.  A keyword is a word that appears more 
 2658: than the average number of times in the thesaurus database.  Calls 
 2659: &initialize_keywords
 2660: 
 2661: =cut
 2662: 
 2663: ###################################################
 2664: 
 2665: sub keyword {
 2666:     return if (!&initialize_keywords());
 2667:     my $word=lc(shift());
 2668:     $word=~s/\W//g;
 2669:     return exists($Keywords{$word});
 2670: }
 2671: 
 2672: ###############################################################
 2673: 
 2674: =pod 
 2675: 
 2676: =item * &get_related_words()
 2677: 
 2678: Look up a word in the thesaurus.  Takes a scalar argument and returns
 2679: an array of words.  If the keyword is not in the thesaurus, an empty array
 2680: will be returned.  The order of the words returned is determined by the
 2681: database which holds them.
 2682: 
 2683: Uses global $thesaurus_db_file.
 2684: 
 2685: =cut
 2686: 
 2687: ###############################################################
 2688: sub get_related_words {
 2689:     my $keyword = shift;
 2690:     my %thesaurus_db;
 2691:     if (! -e $thesaurus_db_file) {
 2692:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file ".
 2693:                                  "failed because the file does not exist");
 2694:         return ();
 2695:     }
 2696:     if (! tie(%thesaurus_db,'GDBM_File',
 2697:               $thesaurus_db_file,&GDBM_READER(),0640)){
 2698:         return ();
 2699:     } 
 2700:     my @Words=();
 2701:     my $count=0;
 2702:     if (exists($thesaurus_db{$keyword})) {
 2703: 	# The first element is the number of times
 2704: 	# the word appears.  We do not need it now.
 2705: 	my (undef,@RelatedWords) = (split(/:/,$thesaurus_db{$keyword}));
 2706: 	my (undef,$mostfrequentcount)=split(/\,/,$RelatedWords[0]);
 2707: 	my $threshold=$mostfrequentcount/10;
 2708:         foreach my $possibleword (@RelatedWords) {
 2709:             my ($word,$wordcount)=split(/\,/,$possibleword);
 2710:             if ($wordcount>$threshold) {
 2711: 		push(@Words,$word);
 2712:                 $count++;
 2713:                 if ($count>10) { last; }
 2714: 	    }
 2715:         }
 2716:     }
 2717:     untie %thesaurus_db;
 2718:     return @Words;
 2719: }
 2720: 
 2721: =pod
 2722: 
 2723: =back
 2724: 
 2725: =cut
 2726: 
 2727: # -------------------------------------------------------------- Plaintext name
 2728: =pod
 2729: 
 2730: =head1 User Name Functions
 2731: 
 2732: =over 4
 2733: 
 2734: =item * &plainname($uname,$udom,$first)
 2735: 
 2736: Takes a users logon name and returns it as a string in
 2737: "first middle last generation" form 
 2738: if $first is set to 'lastname' then it returns it as
 2739: 'lastname generation, firstname middlename' if their is a lastname
 2740: 
 2741: =cut
 2742: 
 2743: 
 2744: ###############################################################
 2745: sub plainname {
 2746:     my ($uname,$udom,$first)=@_;
 2747:     return if (!defined($uname) || !defined($udom));
 2748:     my %names=&getnames($uname,$udom);
 2749:     my $name=&Apache::lonnet::format_name($names{'firstname'},
 2750: 					  $names{'middlename'},
 2751: 					  $names{'lastname'},
 2752: 					  $names{'generation'},$first);
 2753:     $name=~s/^\s+//;
 2754:     $name=~s/\s+$//;
 2755:     $name=~s/\s+/ /g;
 2756:     if ($name !~ /\S/) { $name=$uname.':'.$udom; }
 2757:     return $name;
 2758: }
 2759: 
 2760: # -------------------------------------------------------------------- Nickname
 2761: =pod
 2762: 
 2763: =item * &nickname($uname,$udom)
 2764: 
 2765: Gets a users name and returns it as a string as
 2766: 
 2767: "&quot;nickname&quot;"
 2768: 
 2769: if the user has a nickname or
 2770: 
 2771: "first middle last generation"
 2772: 
 2773: if the user does not
 2774: 
 2775: =cut
 2776: 
 2777: sub nickname {
 2778:     my ($uname,$udom)=@_;
 2779:     return if (!defined($uname) || !defined($udom));
 2780:     my %names=&getnames($uname,$udom);
 2781:     my $name=$names{'nickname'};
 2782:     if ($name) {
 2783:        $name='&quot;'.$name.'&quot;'; 
 2784:     } else {
 2785:        $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
 2786: 	     $names{'lastname'}.' '.$names{'generation'};
 2787:        $name=~s/\s+$//;
 2788:        $name=~s/\s+/ /g;
 2789:     }
 2790:     return $name;
 2791: }
 2792: 
 2793: sub getnames {
 2794:     my ($uname,$udom)=@_;
 2795:     return if (!defined($uname) || !defined($udom));
 2796:     if ($udom eq 'public' && $uname eq 'public') {
 2797: 	return ('lastname' => &mt('Public'));
 2798:     }
 2799:     my $id=$uname.':'.$udom;
 2800:     my ($names,$cached)=&Apache::lonnet::is_cached_new('namescache',$id);
 2801:     if ($cached) {
 2802: 	return %{$names};
 2803:     } else {
 2804: 	my %loadnames=&Apache::lonnet::get('environment',
 2805:                     ['firstname','middlename','lastname','generation','nickname'],
 2806: 					 $udom,$uname);
 2807: 	&Apache::lonnet::do_cache_new('namescache',$id,\%loadnames);
 2808: 	return %loadnames;
 2809:     }
 2810: }
 2811: 
 2812: # -------------------------------------------------------------------- getemails
 2813: 
 2814: =pod
 2815: 
 2816: =item * &getemails($uname,$udom)
 2817: 
 2818: Gets a user's email information and returns it as a hash with keys:
 2819: notification, critnotification, permanentemail
 2820: 
 2821: For notification and critnotification, values are comma-separated lists 
 2822: of e-mail addresses; for permanentemail, value is a single e-mail address.
 2823:  
 2824: 
 2825: =cut
 2826: 
 2827: 
 2828: sub getemails {
 2829:     my ($uname,$udom)=@_;
 2830:     if ($udom eq 'public' && $uname eq 'public') {
 2831: 	return;
 2832:     }
 2833:     if (!$udom) { $udom=$env{'user.domain'}; }
 2834:     if (!$uname) { $uname=$env{'user.name'}; }
 2835:     my $id=$uname.':'.$udom;
 2836:     my ($names,$cached)=&Apache::lonnet::is_cached_new('emailscache',$id);
 2837:     if ($cached) {
 2838: 	return %{$names};
 2839:     } else {
 2840: 	my %loadnames=&Apache::lonnet::get('environment',
 2841:                     			   ['notification','critnotification',
 2842: 					    'permanentemail'],
 2843: 					   $udom,$uname);
 2844: 	&Apache::lonnet::do_cache_new('emailscache',$id,\%loadnames);
 2845: 	return %loadnames;
 2846:     }
 2847: }
 2848: 
 2849: sub flush_email_cache {
 2850:     my ($uname,$udom)=@_;
 2851:     if (!$udom)  { $udom =$env{'user.domain'}; }
 2852:     if (!$uname) { $uname=$env{'user.name'};   }
 2853:     return if ($udom eq 'public' && $uname eq 'public');
 2854:     my $id=$uname.':'.$udom;
 2855:     &Apache::lonnet::devalidate_cache_new('emailscache',$id);
 2856: }
 2857: 
 2858: # -------------------------------------------------------------------- getlangs
 2859: 
 2860: =pod
 2861: 
 2862: =item * &getlangs($uname,$udom)
 2863: 
 2864: Gets a user's language preference and returns it as a hash with key:
 2865: language.
 2866: 
 2867: =cut
 2868: 
 2869: 
 2870: sub getlangs {
 2871:     my ($uname,$udom) = @_;
 2872:     if (!$udom)  { $udom =$env{'user.domain'}; }
 2873:     if (!$uname) { $uname=$env{'user.name'};   }
 2874:     my $id=$uname.':'.$udom;
 2875:     my ($langs,$cached)=&Apache::lonnet::is_cached_new('userlangs',$id);
 2876:     if ($cached) {
 2877:         return %{$langs};
 2878:     } else {
 2879:         my %loadlangs=&Apache::lonnet::get('environment',['languages'],
 2880:                                            $udom,$uname);
 2881:         &Apache::lonnet::do_cache_new('userlangs',$id,\%loadlangs);
 2882:         return %loadlangs;
 2883:     }
 2884: }
 2885: 
 2886: sub flush_langs_cache {
 2887:     my ($uname,$udom)=@_;
 2888:     if (!$udom)  { $udom =$env{'user.domain'}; }
 2889:     if (!$uname) { $uname=$env{'user.name'};   }
 2890:     return if ($udom eq 'public' && $uname eq 'public');
 2891:     my $id=$uname.':'.$udom;
 2892:     &Apache::lonnet::devalidate_cache_new('userlangs',$id);
 2893: }
 2894: 
 2895: # ------------------------------------------------------------------ Screenname
 2896: 
 2897: =pod
 2898: 
 2899: =item * &screenname($uname,$udom)
 2900: 
 2901: Gets a users screenname and returns it as a string
 2902: 
 2903: =cut
 2904: 
 2905: sub screenname {
 2906:     my ($uname,$udom)=@_;
 2907:     if ($uname eq $env{'user.name'} &&
 2908: 	$udom eq $env{'user.domain'}) {return $env{'environment.screenname'};}
 2909:     my %names=&Apache::lonnet::get('environment',['screenname'],$udom,$uname);
 2910:     return $names{'screenname'};
 2911: }
 2912: 
 2913: 
 2914: # ------------------------------------------------------------- Confirm Wrapper
 2915: =pod
 2916: 
 2917: =item confirmwrapper
 2918: 
 2919: Wrap messages about completion of operation in box
 2920: 
 2921: =cut
 2922: 
 2923: sub confirmwrapper {
 2924:     my ($message)=@_;
 2925:     if ($message) {
 2926:         return "\n".'<div class="LC_confirm_box">'."\n"
 2927:                .$message."\n"
 2928:                .'</div>'."\n";
 2929:     } else {
 2930:         return $message;
 2931:     }
 2932: }
 2933: 
 2934: # ------------------------------------------------------------- Message Wrapper
 2935: 
 2936: sub messagewrapper {
 2937:     my ($link,$username,$domain,$subject,$text)=@_;
 2938:     return 
 2939:         '<a href="/adm/email?compose=individual&amp;'.
 2940:         'recname='.$username.'&amp;recdom='.$domain.
 2941: 	'&amp;subject='.&escape($subject).'&amp;text='.&escape($text).'" '.
 2942:         'title="'.&mt('Send message').'">'.$link.'</a>';
 2943: }
 2944: 
 2945: # --------------------------------------------------------------- Notes Wrapper
 2946: 
 2947: sub noteswrapper {
 2948:     my ($link,$un,$do)=@_;
 2949:     return 
 2950: "<a href='/adm/email?recordftf=retrieve&recname=$un&recdom=$do'>$link</a>";
 2951: }
 2952: 
 2953: # ------------------------------------------------------------- Aboutme Wrapper
 2954: 
 2955: sub aboutmewrapper {
 2956:     my ($link,$username,$domain,$target)=@_;
 2957:     if (!defined($username)  && !defined($domain)) {
 2958:         return;
 2959:     }
 2960:     return '<a href="/adm/'.$domain.'/'.$username.'/aboutme"'.
 2961: 	($target?' target="$target"':'').' title="'.&mt("View this user's personal information page").'">'.$link.'</a>';
 2962: }
 2963: 
 2964: # ------------------------------------------------------------ Syllabus Wrapper
 2965: 
 2966: sub syllabuswrapper {
 2967:     my ($linktext,$coursedir,$domain)=@_;
 2968:     return qq{<a href="/public/$domain/$coursedir/syllabus">$linktext</a>};
 2969: }
 2970: 
 2971: # -----------------------------------------------------------------------------
 2972: 
 2973: sub track_student_link {
 2974:     my ($linktext,$sname,$sdom,$target,$start) = @_;
 2975:     my $link ="/adm/trackstudent?";
 2976:     my $title = 'View recent activity';
 2977:     if (defined($sname) && $sname !~ /^\s*$/ &&
 2978:         defined($sdom)  && $sdom  !~ /^\s*$/) {
 2979:         $link .= "selected_student=$sname:$sdom";
 2980:         $title .= ' of this student';
 2981:     } 
 2982:     if (defined($target) && $target !~ /^\s*$/) {
 2983:         $target = qq{target="$target"};
 2984:     } else {
 2985:         $target = '';
 2986:     }
 2987:     if ($start) { $link.='&amp;start='.$start; }
 2988:     $title = &mt($title);
 2989:     $linktext = &mt($linktext);
 2990:     return qq{<a href="$link" title="$title" $target>$linktext</a>}.
 2991: 	&help_open_topic('View_recent_activity');
 2992: }
 2993: 
 2994: sub slot_reservations_link {
 2995:     my ($linktext,$sname,$sdom,$target) = @_;
 2996:     my $link ="/adm/slotrequest?command=showresv&amp;origin=aboutme";
 2997:     my $title = 'View slot reservation history';
 2998:     if (defined($sname) && $sname !~ /^\s*$/ &&
 2999:         defined($sdom)  && $sdom  !~ /^\s*$/) {
 3000:         $link .= "&amp;uname=$sname&amp;udom=$sdom";
 3001:         $title .= ' of this student';
 3002:     }
 3003:     if (defined($target) && $target !~ /^\s*$/) {
 3004:         $target = qq{target="$target"};
 3005:     } else {
 3006:         $target = '';
 3007:     }
 3008:     $title = &mt($title);
 3009:     $linktext = &mt($linktext);
 3010:     return qq{<a href="$link" title="$title" $target>$linktext</a>};
 3011: # FIXME uncomment when help item created: &help_open_topic('Slot_Reservation_History');
 3012: 
 3013: }
 3014: 
 3015: # ===================================================== Display a student photo
 3016: 
 3017: 
 3018: sub student_image_tag {
 3019:     my ($domain,$user)=@_;
 3020:     my $imgsrc=&Apache::lonnet::studentphoto($domain,$user,'jpg');
 3021:     if (($imgsrc) && ($imgsrc ne '/adm/lonKaputt/lonlogo_broken.gif')) {
 3022: 	return '<img src="'.$imgsrc.'" align="right" />';
 3023:     } else {
 3024: 	return '';
 3025:     }
 3026: }
 3027: 
 3028: =pod
 3029: 
 3030: =back
 3031: 
 3032: =head1 Access .tab File Data
 3033: 
 3034: =over 4
 3035: 
 3036: =item * &languageids() 
 3037: 
 3038: returns list of all language ids
 3039: 
 3040: =cut
 3041: 
 3042: sub languageids {
 3043:     return sort(keys(%language));
 3044: }
 3045: 
 3046: =pod
 3047: 
 3048: =item * &languagedescription() 
 3049: 
 3050: returns description of a specified language id
 3051: 
 3052: =cut
 3053: 
 3054: sub languagedescription {
 3055:     my $code=shift;
 3056:     return  ($supported_language{$code}?'* ':'').
 3057:             $language{$code}.
 3058: 	    ($supported_language{$code}?' ('.&mt('interface available').')':'');
 3059: }
 3060: 
 3061: sub plainlanguagedescription {
 3062:     my $code=shift;
 3063:     return $language{$code};
 3064: }
 3065: 
 3066: sub supportedlanguagecode {
 3067:     my $code=shift;
 3068:     return $supported_language{$code};
 3069: }
 3070: 
 3071: =pod
 3072: 
 3073: =item * &copyrightids() 
 3074: 
 3075: returns list of all copyrights
 3076: 
 3077: =cut
 3078: 
 3079: sub copyrightids {
 3080:     return sort(keys(%cprtag));
 3081: }
 3082: 
 3083: =pod
 3084: 
 3085: =item * &copyrightdescription() 
 3086: 
 3087: returns description of a specified copyright id
 3088: 
 3089: =cut
 3090: 
 3091: sub copyrightdescription {
 3092:     return &mt($cprtag{shift(@_)});
 3093: }
 3094: 
 3095: =pod
 3096: 
 3097: =item * &source_copyrightids() 
 3098: 
 3099: returns list of all source copyrights
 3100: 
 3101: =cut
 3102: 
 3103: sub source_copyrightids {
 3104:     return sort(keys(%scprtag));
 3105: }
 3106: 
 3107: =pod
 3108: 
 3109: =item * &source_copyrightdescription() 
 3110: 
 3111: returns description of a specified source copyright id
 3112: 
 3113: =cut
 3114: 
 3115: sub source_copyrightdescription {
 3116:     return &mt($scprtag{shift(@_)});
 3117: }
 3118: 
 3119: =pod
 3120: 
 3121: =item * &filecategories() 
 3122: 
 3123: returns list of all file categories
 3124: 
 3125: =cut
 3126: 
 3127: sub filecategories {
 3128:     return sort(keys(%category_extensions));
 3129: }
 3130: 
 3131: =pod
 3132: 
 3133: =item * &filecategorytypes() 
 3134: 
 3135: returns list of file types belonging to a given file
 3136: category
 3137: 
 3138: =cut
 3139: 
 3140: sub filecategorytypes {
 3141:     my ($cat) = @_;
 3142:     return @{$category_extensions{lc($cat)}};
 3143: }
 3144: 
 3145: =pod
 3146: 
 3147: =item * &fileembstyle() 
 3148: 
 3149: returns embedding style for a specified file type
 3150: 
 3151: =cut
 3152: 
 3153: sub fileembstyle {
 3154:     return $fe{lc(shift(@_))};
 3155: }
 3156: 
 3157: sub filemimetype {
 3158:     return $fm{lc(shift(@_))};
 3159: }
 3160: 
 3161: 
 3162: sub filecategoryselect {
 3163:     my ($name,$value)=@_;
 3164:     return &select_form($value,$name,
 3165: 			'' => &mt('Any category'),
 3166: 			map { $_,$_ } sort(keys(%category_extensions)));
 3167: }
 3168: 
 3169: =pod
 3170: 
 3171: =item * &filedescription() 
 3172: 
 3173: returns description for a specified file type
 3174: 
 3175: =cut
 3176: 
 3177: sub filedescription {
 3178:     my $file_description = $fd{lc(shift())};
 3179:     $file_description =~ s:([\[\]]):~$1:g;
 3180:     return &mt($file_description);
 3181: }
 3182: 
 3183: =pod
 3184: 
 3185: =item * &filedescriptionex() 
 3186: 
 3187: returns description for a specified file type with
 3188: extra formatting
 3189: 
 3190: =cut
 3191: 
 3192: sub filedescriptionex {
 3193:     my $ex=shift;
 3194:     my $file_description = $fd{lc($ex)};
 3195:     $file_description =~ s:([\[\]]):~$1:g;
 3196:     return '.'.$ex.' '.&mt($file_description);
 3197: }
 3198: 
 3199: # End of .tab access
 3200: =pod
 3201: 
 3202: =back
 3203: 
 3204: =cut
 3205: 
 3206: # ------------------------------------------------------------------ File Types
 3207: sub fileextensions {
 3208:     return sort(keys(%fe));
 3209: }
 3210: 
 3211: # ----------------------------------------------------------- Display Languages
 3212: # returns a hash with all desired display languages
 3213: #
 3214: 
 3215: sub display_languages {
 3216:     my %languages=();
 3217:     foreach my $lang (&Apache::lonlocal::preferred_languages()) {
 3218: 	$languages{$lang}=1;
 3219:     }
 3220:     &get_unprocessed_cgi($ENV{'QUERY_STRING'},['displaylanguage']);
 3221:     if ($env{'form.displaylanguage'}) {
 3222: 	foreach my $lang (split(/\s*(\,|\;|\:)\s*/,$env{'form.displaylanguage'})) {
 3223: 	    $languages{$lang}=1;
 3224:         }
 3225:     }
 3226:     return %languages;
 3227: }
 3228: 
 3229: sub languages {
 3230:     my ($possible_langs) = @_;
 3231:     my @preferred_langs = &Apache::lonlocal::preferred_languages();
 3232:     if (!ref($possible_langs)) {
 3233: 	if( wantarray ) {
 3234: 	    return @preferred_langs;
 3235: 	} else {
 3236: 	    return $preferred_langs[0];
 3237: 	}
 3238:     }
 3239:     my %possibilities = map { $_ => 1 } (@$possible_langs);
 3240:     my @preferred_possibilities;
 3241:     foreach my $preferred_lang (@preferred_langs) {
 3242: 	if (exists($possibilities{$preferred_lang})) {
 3243: 	    push(@preferred_possibilities, $preferred_lang);
 3244: 	}
 3245:     }
 3246:     if( wantarray ) {
 3247: 	return @preferred_possibilities;
 3248:     }
 3249:     return $preferred_possibilities[0];
 3250: }
 3251: 
 3252: sub user_lang {
 3253:     my ($touname,$toudom,$fromcid) = @_;
 3254:     my @userlangs;
 3255:     if (($fromcid ne '') && ($env{'course.'.$fromcid.'.languages'} ne '')) {
 3256:         @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
 3257:                     $env{'course.'.$fromcid.'.languages'}));
 3258:     } else {
 3259:         my %langhash = &getlangs($touname,$toudom);
 3260:         if ($langhash{'languages'} ne '') {
 3261:             @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
 3262:         } else {
 3263:             my %domdefs = &Apache::lonnet::get_domain_defaults($toudom);
 3264:             if ($domdefs{'lang_def'} ne '') {
 3265:                 @userlangs = ($domdefs{'lang_def'});
 3266:             }
 3267:         }
 3268:     }
 3269:     my @languages=&Apache::lonlocal::get_genlanguages(@userlangs);
 3270:     my $user_lh = Apache::localize->get_handle(@languages);
 3271:     return $user_lh;
 3272: }
 3273: 
 3274: 
 3275: ###############################################################
 3276: ##               Student Answer Attempts                     ##
 3277: ###############################################################
 3278: 
 3279: =pod
 3280: 
 3281: =head1 Alternate Problem Views
 3282: 
 3283: =over 4
 3284: 
 3285: =item * &get_previous_attempt($symb, $username, $domain, $course,
 3286:     $getattempt, $regexp, $gradesub)
 3287: 
 3288: Return string with previous attempt on problem. Arguments:
 3289: 
 3290: =over 4
 3291: 
 3292: =item * $symb: Problem, including path
 3293: 
 3294: =item * $username: username of the desired student
 3295: 
 3296: =item * $domain: domain of the desired student
 3297: 
 3298: =item * $course: Course ID
 3299: 
 3300: =item * $getattempt: Leave blank for all attempts, otherwise put
 3301:     something
 3302: 
 3303: =item * $regexp: if string matches this regexp, the string will be
 3304:     sent to $gradesub
 3305: 
 3306: =item * $gradesub: routine that processes the string if it matches $regexp
 3307: 
 3308: =back
 3309: 
 3310: The output string is a table containing all desired attempts, if any.
 3311: 
 3312: =cut
 3313: 
 3314: sub get_previous_attempt {
 3315:   my ($symb,$username,$domain,$course,$getattempt,$regexp,$gradesub)=@_;
 3316:   my $prevattempts='';
 3317:   no strict 'refs';
 3318:   if ($symb) {
 3319:     my (%returnhash)=
 3320:       &Apache::lonnet::restore($symb,$course,$domain,$username);
 3321:     if ($returnhash{'version'}) {
 3322:       my %lasthash=();
 3323:       my $version;
 3324:       for ($version=1;$version<=$returnhash{'version'};$version++) {
 3325:         foreach my $key (sort(split(/\:/,$returnhash{$version.':keys'}))) {
 3326: 	  $lasthash{$key}=$returnhash{$version.':'.$key};
 3327:         }
 3328:       }
 3329:       $prevattempts=&start_data_table().&start_data_table_header_row();
 3330:       $prevattempts.='<th>'.&mt('History').'</th>';
 3331:       foreach my $key (sort(keys(%lasthash))) {
 3332: 	my ($ign,@parts) = split(/\./,$key);
 3333: 	if ($#parts > 0) {
 3334: 	  my $data=$parts[-1];
 3335: 	  pop(@parts);
 3336: 	  $prevattempts.='<th>'.&mt('Part ').join('.',@parts).'<br />'.$data.'&nbsp;</th>';
 3337: 	} else {
 3338: 	  if ($#parts == 0) {
 3339: 	    $prevattempts.='<th>'.$parts[0].'</th>';
 3340: 	  } else {
 3341: 	    $prevattempts.='<th>'.$ign.'</th>';
 3342: 	  }
 3343: 	}
 3344:       }
 3345:       $prevattempts.=&end_data_table_header_row();
 3346:       if ($getattempt eq '') {
 3347: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
 3348: 	  $prevattempts.=&start_data_table_row().
 3349: 	      '<td>'.&mt('Transaction [_1]',$version).'</td>';
 3350: 	    foreach my $key (sort(keys(%lasthash))) {
 3351: 		my $value = &format_previous_attempt_value($key,
 3352: 							   $returnhash{$version.':'.$key});
 3353: 		$prevattempts.='<td>'.$value.'&nbsp;</td>';   
 3354: 	    }
 3355: 	  $prevattempts.=&end_data_table_row();
 3356: 	 }
 3357:       }
 3358:       $prevattempts.=&start_data_table_row().'<td>'.&mt('Current').'</td>';
 3359:       foreach my $key (sort(keys(%lasthash))) {
 3360: 	my $value = &format_previous_attempt_value($key,$lasthash{$key});
 3361: 	if ($key =~/$regexp$/ && (defined &$gradesub)) {$value = &$gradesub($value)}
 3362: 	$prevattempts.='<td>'.$value.'&nbsp;</td>';
 3363:       }
 3364:       $prevattempts.= &end_data_table_row().&end_data_table();
 3365:     } else {
 3366:       $prevattempts=
 3367: 	  &start_data_table().&start_data_table_row().
 3368: 	  '<td>'.&mt('Nothing submitted - no attempts.').'</td>'.
 3369: 	  &end_data_table_row().&end_data_table();
 3370:     }
 3371:   } else {
 3372:     $prevattempts=
 3373: 	  &start_data_table().&start_data_table_row().
 3374: 	  '<td>'.&mt('No data.').'</td>'.
 3375: 	  &end_data_table_row().&end_data_table();
 3376:   }
 3377: }
 3378: 
 3379: sub format_previous_attempt_value {
 3380:     my ($key,$value) = @_;
 3381:     if ($key =~ /timestamp/) {
 3382: 	$value = &Apache::lonlocal::locallocaltime($value);
 3383:     } elsif (ref($value) eq 'ARRAY') {
 3384: 	$value = '('.join(', ', @{ $value }).')';
 3385:     } else {
 3386: 	$value = &unescape($value);
 3387:     }
 3388:     return $value;
 3389: }
 3390: 
 3391: 
 3392: sub relative_to_absolute {
 3393:     my ($url,$output)=@_;
 3394:     my $parser=HTML::TokeParser->new(\$output);
 3395:     my $token;
 3396:     my $thisdir=$url;
 3397:     my @rlinks=();
 3398:     while ($token=$parser->get_token) {
 3399: 	if ($token->[0] eq 'S') {
 3400: 	    if ($token->[1] eq 'a') {
 3401: 		if ($token->[2]->{'href'}) {
 3402: 		    $rlinks[$#rlinks+1]=$token->[2]->{'href'};
 3403: 		}
 3404: 	    } elsif ($token->[1] eq 'img' || $token->[1] eq 'embed' ) {
 3405: 		$rlinks[$#rlinks+1]=$token->[2]->{'src'};
 3406: 	    } elsif ($token->[1] eq 'base') {
 3407: 		$thisdir=$token->[2]->{'href'};
 3408: 	    }
 3409: 	}
 3410:     }
 3411:     $thisdir=~s-/[^/]*$--;
 3412:     foreach my $link (@rlinks) {
 3413: 	unless (($link=~/^https?\:\/\//i) ||
 3414: 		($link=~/^\//) ||
 3415: 		($link=~/^javascript:/i) ||
 3416: 		($link=~/^mailto:/i) ||
 3417: 		($link=~/^\#/)) {
 3418: 	    my $newlocation=&Apache::lonnet::hreflocation($thisdir,$link);
 3419: 	    $output=~s/(\"|\'|\=\s*)\Q$link\E(\"|\'|\s|\>)/$1$newlocation$2/;
 3420: 	}
 3421:     }
 3422: # -------------------------------------------------- Deal with Applet codebases
 3423:     $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
 3424:     return $output;
 3425: }
 3426: 
 3427: =pod
 3428: 
 3429: =item * &get_student_view()
 3430: 
 3431: show a snapshot of what student was looking at
 3432: 
 3433: =cut
 3434: 
 3435: sub get_student_view {
 3436:   my ($symb,$username,$domain,$courseid,$target,$moreenv) = @_;
 3437:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
 3438:   my (%form);
 3439:   my @elements=('symb','courseid','domain','username');
 3440:   foreach my $element (@elements) {
 3441:       $form{'grade_'.$element}=eval '$'.$element #'
 3442:   }
 3443:   if (defined($moreenv)) {
 3444:       %form=(%form,%{$moreenv});
 3445:   }
 3446:   if (defined($target)) { $form{'grade_target'} = $target; }
 3447:   $feedurl=&Apache::lonnet::clutter($feedurl);
 3448:   my ($userview,$response)=&Apache::lonnet::ssi_body($feedurl,%form);
 3449:   $userview=~s/\<body[^\>]*\>//gi;
 3450:   $userview=~s/\<\/body\>//gi;
 3451:   $userview=~s/\<html\>//gi;
 3452:   $userview=~s/\<\/html\>//gi;
 3453:   $userview=~s/\<head\>//gi;
 3454:   $userview=~s/\<\/head\>//gi;
 3455:   $userview=~s/action\s*\=/would_be_action\=/gi;
 3456:   $userview=&relative_to_absolute($feedurl,$userview);
 3457:   if (wantarray) {
 3458:      return ($userview,$response);
 3459:   } else {
 3460:      return $userview;
 3461:   }
 3462: }
 3463: 
 3464: sub get_student_view_with_retries {
 3465:   my ($symb,$retries,$username,$domain,$courseid,$target,$moreenv) = @_;
 3466: 
 3467:     my $ok = 0;                 # True if we got a good response.
 3468:     my $content;
 3469:     my $response;
 3470: 
 3471:     # Try to get the student_view done. within the retries count:
 3472:     
 3473:     do {
 3474:          ($content, $response) = &get_student_view($symb,$username,$domain,$courseid,$target,$moreenv);
 3475:          $ok      = $response->is_success;
 3476:          if (!$ok) {
 3477:             &Apache::lonnet::logthis("Failed get_student_view_with_retries on $symb: ".$response->is_success.', '.$response->code.', '.$response->message);
 3478:          }
 3479:          $retries--;
 3480:     } while (!$ok && ($retries > 0));
 3481:     
 3482:     if (!$ok) {
 3483:        $content = '';          # On error return an empty content.
 3484:     }
 3485:     if (wantarray) {
 3486:        return ($content, $response);
 3487:     } else {
 3488:        return $content;
 3489:     }
 3490: }
 3491: 
 3492: =pod
 3493: 
 3494: =item * &get_student_answers() 
 3495: 
 3496: show a snapshot of how student was answering problem
 3497: 
 3498: =cut
 3499: 
 3500: sub get_student_answers {
 3501:   my ($symb,$username,$domain,$courseid,%form) = @_;
 3502:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
 3503:   my (%moreenv);
 3504:   my @elements=('symb','courseid','domain','username');
 3505:   foreach my $element (@elements) {
 3506:     $moreenv{'grade_'.$element}=eval '$'.$element #'
 3507:   }
 3508:   $moreenv{'grade_target'}='answer';
 3509:   %moreenv=(%form,%moreenv);
 3510:   $feedurl = &Apache::lonnet::clutter($feedurl);
 3511:   my $userview=&Apache::lonnet::ssi($feedurl,%moreenv);
 3512:   return $userview;
 3513: }
 3514: 
 3515: =pod
 3516: 
 3517: =item * &submlink()
 3518: 
 3519: Inputs: $text $uname $udom $symb $target
 3520: 
 3521: Returns: A link to grades.pm such as to see the SUBM view of a student
 3522: 
 3523: =cut
 3524: 
 3525: ###############################################
 3526: sub submlink {
 3527:     my ($text,$uname,$udom,$symb,$target)=@_;
 3528:     if (!($uname && $udom)) {
 3529: 	(my $cursymb, my $courseid,$udom,$uname)=
 3530: 	    &Apache::lonnet::whichuser($symb);
 3531: 	if (!$symb) { $symb=$cursymb; }
 3532:     }
 3533:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
 3534:     $symb=&escape($symb);
 3535:     if ($target) { $target="target=\"$target\""; }
 3536:     return '<a href="/adm/grades?&command=submission&'.
 3537: 	'symb='.$symb.'&student='.$uname.
 3538: 	'&userdom='.$udom.'" '.$target.'>'.$text.'</a>';
 3539: }
 3540: ##############################################
 3541: 
 3542: =pod
 3543: 
 3544: =item * &pgrdlink()
 3545: 
 3546: Inputs: $text $uname $udom $symb $target
 3547: 
 3548: Returns: A link to grades.pm such as to see the PGRD view of a student
 3549: 
 3550: =cut
 3551: 
 3552: ###############################################
 3553: sub pgrdlink {
 3554:     my $link=&submlink(@_);
 3555:     $link=~s/(&command=submission)/$1&showgrading=yes/;
 3556:     return $link;
 3557: }
 3558: ##############################################
 3559: 
 3560: =pod
 3561: 
 3562: =item * &pprmlink()
 3563: 
 3564: Inputs: $text $uname $udom $symb $target
 3565: 
 3566: Returns: A link to parmset.pm such as to see the PPRM view of a
 3567: student and a specific resource
 3568: 
 3569: =cut
 3570: 
 3571: ###############################################
 3572: sub pprmlink {
 3573:     my ($text,$uname,$udom,$symb,$target)=@_;
 3574:     if (!($uname && $udom)) {
 3575: 	(my $cursymb, my $courseid,$udom,$uname)=
 3576: 	    &Apache::lonnet::whichuser($symb);
 3577: 	if (!$symb) { $symb=$cursymb; }
 3578:     }
 3579:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
 3580:     $symb=&escape($symb);
 3581:     if ($target) { $target="target=\"$target\""; }
 3582:     return '<a href="/adm/parmset?command=set&amp;'.
 3583: 	'symb='.$symb.'&amp;uname='.$uname.
 3584: 	'&amp;udom='.$udom.'" '.$target.'>'.$text.'</a>';
 3585: }
 3586: ##############################################
 3587: 
 3588: =pod
 3589: 
 3590: =back
 3591: 
 3592: =cut
 3593: 
 3594: ###############################################
 3595: 
 3596: 
 3597: sub timehash {
 3598:     my ($thistime) = @_;
 3599:     my $timezone = &Apache::lonlocal::gettimezone();
 3600:     my $dt = DateTime->from_epoch(epoch => $thistime)
 3601:                      ->set_time_zone($timezone);
 3602:     my $wday = $dt->day_of_week();
 3603:     if ($wday == 7) { $wday = 0; }
 3604:     return ( 'second' => $dt->second(),
 3605:              'minute' => $dt->minute(),
 3606:              'hour'   => $dt->hour(),
 3607:              'day'     => $dt->day_of_month(),
 3608:              'month'   => $dt->month(),
 3609:              'year'    => $dt->year(),
 3610:              'weekday' => $wday,
 3611:              'dayyear' => $dt->day_of_year(),
 3612:              'dlsav'   => $dt->is_dst() );
 3613: }
 3614: 
 3615: sub utc_string {
 3616:     my ($date)=@_;
 3617:     return strftime("%Y%m%dT%H%M%SZ",gmtime($date));
 3618: }
 3619: 
 3620: sub maketime {
 3621:     my %th=@_;
 3622:     my ($epoch_time,$timezone,$dt);
 3623:     $timezone = &Apache::lonlocal::gettimezone();
 3624:     eval {
 3625:         $dt = DateTime->new( year   => $th{'year'},
 3626:                              month  => $th{'month'},
 3627:                              day    => $th{'day'},
 3628:                              hour   => $th{'hour'},
 3629:                              minute => $th{'minute'},
 3630:                              second => $th{'second'},
 3631:                              time_zone => $timezone,
 3632:                          );
 3633:     };
 3634:     if (!$@) {
 3635:         $epoch_time = $dt->epoch;
 3636:         if ($epoch_time) {
 3637:             return $epoch_time;
 3638:         }
 3639:     }
 3640:     return POSIX::mktime(
 3641:         ($th{'seconds'},$th{'minutes'},$th{'hours'},
 3642:          $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,-1));
 3643: }
 3644: 
 3645: #########################################
 3646: 
 3647: sub findallcourses {
 3648:     my ($roles,$uname,$udom) = @_;
 3649:     my %roles;
 3650:     if (ref($roles)) { %roles = map { $_ => 1 } @{$roles}; }
 3651:     my %courses;
 3652:     my $now=time;
 3653:     if (!defined($uname)) {
 3654:         $uname = $env{'user.name'};
 3655:     }
 3656:     if (!defined($udom)) {
 3657:         $udom = $env{'user.domain'};
 3658:     }
 3659:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
 3660:         my %roleshash = &Apache::lonnet::dump('roles',$udom,$uname);
 3661:         if (!%roles) {
 3662:             %roles = (
 3663:                        cc => 1,
 3664:                        in => 1,
 3665:                        ep => 1,
 3666:                        ta => 1,
 3667:                        cr => 1,
 3668:                        st => 1,
 3669:              );
 3670:         }
 3671:         foreach my $entry (keys(%roleshash)) {
 3672:             my ($trole,$tend,$tstart) = split(/_/,$roleshash{$entry});
 3673:             if ($trole =~ /^cr/) { 
 3674:                 next if (!exists($roles{$trole}) && !exists($roles{'cr'}));
 3675:             } else {
 3676:                 next if (!exists($roles{$trole}));
 3677:             }
 3678:             if ($tend) {
 3679:                 next if ($tend < $now);
 3680:             }
 3681:             if ($tstart) {
 3682:                 next if ($tstart > $now);
 3683:             }
 3684:             my ($cdom,$cnum,$sec,$cnumpart,$secpart,$role,$realsec);
 3685:             (undef,$cdom,$cnumpart,$secpart) = split(/\//,$entry);
 3686:             if ($secpart eq '') {
 3687:                 ($cnum,$role) = split(/_/,$cnumpart); 
 3688:                 $sec = 'none';
 3689:                 $realsec = '';
 3690:             } else {
 3691:                 $cnum = $cnumpart;
 3692:                 ($sec,$role) = split(/_/,$secpart);
 3693:                 $realsec = $sec;
 3694:             }
 3695:             $courses{$cdom.'_'.$cnum}{$sec} = $trole.'/'.$cdom.'/'.$cnum.'/'.$realsec;
 3696:         }
 3697:     } else {
 3698:         foreach my $key (keys(%env)) {
 3699: 	    if ( $key=~m{^user\.role\.(\w+)\./($match_domain)/($match_courseid)/?(\w*)$} ||
 3700:                  $key=~m{^user\.role\.(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_courseid)/?(\w*)$}) {
 3701: 	        my ($role,$cdom,$cnum,$sec) = ($1,$2,$3,$4);
 3702: 	        next if ($role eq 'ca' || $role eq 'aa');
 3703: 	        next if (%roles && !exists($roles{$role}));
 3704: 	        my ($starttime,$endtime)=split(/\./,$env{$key});
 3705:                 my $active=1;
 3706:                 if ($starttime) {
 3707: 		    if ($now<$starttime) { $active=0; }
 3708:                 }
 3709:                 if ($endtime) {
 3710:                     if ($now>$endtime) { $active=0; }
 3711:                 }
 3712:                 if ($active) {
 3713:                     if ($sec eq '') {
 3714:                         $sec = 'none';
 3715:                     }
 3716:                     $courses{$cdom.'_'.$cnum}{$sec} = 
 3717:                                      $role.'/'.$cdom.'/'.$cnum.'/'.$sec;
 3718:                 }
 3719:             }
 3720:         }
 3721:     }
 3722:     return %courses;
 3723: }
 3724: 
 3725: ###############################################
 3726: 
 3727: sub blockcheck {
 3728:     my ($setters,$activity,$uname,$udom) = @_;
 3729: 
 3730:     if (!defined($udom)) {
 3731:         $udom = $env{'user.domain'};
 3732:     }
 3733:     if (!defined($uname)) {
 3734:         $uname = $env{'user.name'};
 3735:     }
 3736: 
 3737:     # If uname and udom are for a course, check for blocks in the course.
 3738: 
 3739:     if (&Apache::lonnet::is_course($udom,$uname)) {
 3740:         my %records = &Apache::lonnet::dump('comm_block',$udom,$uname);
 3741:         my ($startblock,$endblock)=&get_blocks($setters,$activity,$udom,$uname);
 3742:         return ($startblock,$endblock);
 3743:     }
 3744: 
 3745:     my $startblock = 0;
 3746:     my $endblock = 0;
 3747:     my %live_courses = &findallcourses(undef,$uname,$udom);
 3748: 
 3749:     # If uname is for a user, and activity is course-specific, i.e.,
 3750:     # boards, chat or groups, check for blocking in current course only.
 3751: 
 3752:     if (($activity eq 'boards' || $activity eq 'chat' ||
 3753:          $activity eq 'groups') && ($env{'request.course.id'})) {
 3754:         foreach my $key (keys(%live_courses)) {
 3755:             if ($key ne $env{'request.course.id'}) {
 3756:                 delete($live_courses{$key});
 3757:             }
 3758:         }
 3759:     }
 3760: 
 3761:     my $otheruser = 0;
 3762:     my %own_courses;
 3763:     if ((($uname ne $env{'user.name'})) || ($udom ne $env{'user.domain'})) {
 3764:         # Resource belongs to user other than current user.
 3765:         $otheruser = 1;
 3766:         # Gather courses for current user
 3767:         %own_courses = 
 3768:             &findallcourses(undef,$env{'user.name'},$env{'user.domain'});
 3769:     }
 3770: 
 3771:     # Gather active course roles - course coordinator, instructor, 
 3772:     # exam proctor, ta, student, or custom role.
 3773: 
 3774:     foreach my $course (keys(%live_courses)) {
 3775:         my ($cdom,$cnum);
 3776:         if ((defined($env{'course.'.$course.'.domain'})) && (defined($env{'course.'.$course.'.num'}))) {
 3777:             $cdom = $env{'course.'.$course.'.domain'};
 3778:             $cnum = $env{'course.'.$course.'.num'};
 3779:         } else {
 3780:             ($cdom,$cnum) = split(/_/,$course); 
 3781:         }
 3782:         my $no_ownblock = 0;
 3783:         my $no_userblock = 0;
 3784:         if ($otheruser && $activity ne 'com') {
 3785:             # Check if current user has 'evb' priv for this
 3786:             if (defined($own_courses{$course})) {
 3787:                 foreach my $sec (keys(%{$own_courses{$course}})) {
 3788:                     my $checkrole = 'cm./'.$cdom.'/'.$cnum;
 3789:                     if ($sec ne 'none') {
 3790:                         $checkrole .= '/'.$sec;
 3791:                     }
 3792:                     if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
 3793:                         $no_ownblock = 1;
 3794:                         last;
 3795:                     }
 3796:                 }
 3797:             }
 3798:             # if they have 'evb' priv and are currently not playing student
 3799:             next if (($no_ownblock) &&
 3800:                  ($env{'request.role'} !~ m{^st\./$cdom/$cnum}));
 3801:         }
 3802:         foreach my $sec (keys(%{$live_courses{$course}})) {
 3803:             my $checkrole = 'cm./'.$cdom.'/'.$cnum;
 3804:             if ($sec ne 'none') {
 3805:                 $checkrole .= '/'.$sec;
 3806:             }
 3807:             if ($otheruser) {
 3808:                 # Resource belongs to user other than current user.
 3809:                 # Assemble privs for that user, and check for 'evb' priv.
 3810:                 my ($trole,$tdom,$tnum,$tsec);
 3811:                 my $entry = $live_courses{$course}{$sec};
 3812:                 if ($entry =~ /^cr/) {
 3813:                     ($trole,$tdom,$tnum,$tsec) = 
 3814:                       ($entry =~ m|^(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_username)/?(\w*)$|);
 3815:                 } else {
 3816:                     ($trole,$tdom,$tnum,$tsec) = split(/\//,$entry);
 3817:                 }
 3818:                 my ($spec,$area,$trest,%allroles,%userroles);
 3819:                 $area = '/'.$tdom.'/'.$tnum;
 3820:                 $trest = $tnum;
 3821:                 if ($tsec ne '') {
 3822:                     $area .= '/'.$tsec;
 3823:                     $trest .= '/'.$tsec;
 3824:                 }
 3825:                 $spec = $trole.'.'.$area;
 3826:                 if ($trole =~ /^cr/) {
 3827:                     &Apache::lonnet::custom_roleprivs(\%allroles,$trole,
 3828:                                                       $tdom,$spec,$trest,$area);
 3829:                 } else {
 3830:                     &Apache::lonnet::standard_roleprivs(\%allroles,$trole,
 3831:                                                        $tdom,$spec,$trest,$area);
 3832:                 }
 3833:                 my ($author,$adv) = &Apache::lonnet::set_userprivs(\%userroles,\%allroles);
 3834:                 if ($userroles{'user.priv.'.$checkrole} =~ /evb\&([^\:]*)/) {
 3835:                     if ($1) {
 3836:                         $no_userblock = 1;
 3837:                         last;
 3838:                     }
 3839:                 }
 3840:             } else {
 3841:                 # Resource belongs to current user
 3842:                 # Check for 'evb' priv via lonnet::allowed().
 3843:                 if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
 3844:                     $no_ownblock = 1;
 3845:                     last;
 3846:                 }
 3847:             }
 3848:         }
 3849:         # if they have the evb priv and are currently not playing student
 3850:         next if (($no_ownblock) &&
 3851:                  ($env{'request.role'} !~ m{^st\./\Q$cdom\E/\Q$cnum\E}));
 3852:         next if ($no_userblock);
 3853: 
 3854:         # Retrieve blocking times and identity of locker for course
 3855:         # of specified user, unless user has 'evb' privilege.
 3856:         
 3857:         my ($start,$end)=&get_blocks($setters,$activity,$cdom,$cnum);
 3858:         if (($start != 0) && 
 3859:             (($startblock == 0) || ($startblock > $start))) {
 3860:             $startblock = $start;
 3861:         }
 3862:         if (($end != 0)  &&
 3863:             (($endblock == 0) || ($endblock < $end))) {
 3864:             $endblock = $end;
 3865:         }
 3866:     }
 3867:     return ($startblock,$endblock);
 3868: }
 3869: 
 3870: sub get_blocks {
 3871:     my ($setters,$activity,$cdom,$cnum) = @_;
 3872:     my $startblock = 0;
 3873:     my $endblock = 0;
 3874:     my $course = $cdom.'_'.$cnum;
 3875:     $setters->{$course} = {};
 3876:     $setters->{$course}{'staff'} = [];
 3877:     $setters->{$course}{'times'} = [];
 3878:     my %records = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
 3879:     foreach my $record (keys(%records)) {
 3880:         my ($start,$end) = ($record =~ m/^(\d+)____(\d+)$/);
 3881:         if ($start <= time && $end >= time) {
 3882:             my ($staff_name,$staff_dom,$title,$blocks) =
 3883:                 &parse_block_record($records{$record});
 3884:             if ($blocks->{$activity} eq 'on') {
 3885:                 push(@{$$setters{$course}{'staff'}},[$staff_name,$staff_dom]);
 3886:                 push(@{$$setters{$course}{'times'}}, [$start,$end]);
 3887:                 if ( ($startblock == 0) || ($startblock > $start) ) {
 3888:                     $startblock = $start;
 3889:                 }
 3890:                 if ( ($endblock == 0) || ($endblock < $end) ) {
 3891:                     $endblock = $end;
 3892:                 }
 3893:             }
 3894:         }
 3895:     }
 3896:     return ($startblock,$endblock);
 3897: }
 3898: 
 3899: sub parse_block_record {
 3900:     my ($record) = @_;
 3901:     my ($setuname,$setudom,$title,$blocks);
 3902:     if (ref($record) eq 'HASH') {
 3903:         ($setuname,$setudom) = split(/:/,$record->{'setter'});
 3904:         $title = &unescape($record->{'event'});
 3905:         $blocks = $record->{'blocks'};
 3906:     } else {
 3907:         my @data = split(/:/,$record,3);
 3908:         if (scalar(@data) eq 2) {
 3909:             $title = $data[1];
 3910:             ($setuname,$setudom) = split(/@/,$data[0]);
 3911:         } else {
 3912:             ($setuname,$setudom,$title) = @data;
 3913:         }
 3914:         $blocks = { 'com' => 'on' };
 3915:     }
 3916:     return ($setuname,$setudom,$title,$blocks);
 3917: }
 3918: 
 3919: sub blocking_status {
 3920:   my $blocked;
 3921:   my ($activity,$uname,$udom) = @_;
 3922:   my %setters;
 3923:   my ($startblock,$endblock)=&blockcheck(\%setters,$activity,$uname,$udom);
 3924:   if ($startblock && $endblock) {
 3925:     $blocked = 1;
 3926:   }
 3927:   if(!wantarray) {
 3928:     return $blocked;
 3929:   }
 3930:   my $output;
 3931:   my $querystring;
 3932:   $querystring = "?activity=$activity";
 3933: 
 3934:       $output .= <<"END_MYBLOCK";
 3935: <script type="text/javascript">
 3936: // <![CDATA[
 3937:     function openWindow(url, wdwName, w, h, toolbar,scrollbar) {
 3938:         var options = "width=" + w + ",height=" + h + ",";
 3939:         options += "resizable=yes,scrollbars="+scrollbar+",status=no,";
 3940:         options += "menubar=no,toolbar="+toolbar+",location=no,directories=no";
 3941:         var newWin = window.open(url, wdwName, options);
 3942:         newWin.focus();
 3943:     }
 3944: 
 3945: // ]]>
 3946: </script>
 3947: END_MYBLOCK
 3948:   my $popupUrl = "/adm/blockingstatus/$querystring";
 3949:   $output .= <<"END_BLOCK";
 3950: <div class='LC_comblock'>
 3951:   <a onclick='openWindow("$popupUrl","Blocking Table",600,300,"no","no");return false;' href='/adm/blockingstatus/$querystring'
 3952:   title='Communication Blocked'>
 3953:   <img class='LC_noBorder LC_middle' title='Communication Blocked' src='/res/adm/pages/comblock.png' alt='Communication Blocked'/></a>
 3954:   <a onclick='openWindow("$popupUrl","Blocking Table",600,300,"no","no");return false;' href='/adm/blockingstatus/$querystring' 
 3955:   title='Communication Blocked'>Communication Blocked</a>
 3956: </div>
 3957: 
 3958: END_BLOCK
 3959: 
 3960:   return ($blocked, $output);
 3961: }
 3962: 
 3963: ###############################################
 3964: 
 3965: sub check_ip_acc {
 3966:     my ($acc)=@_;
 3967:     &Apache::lonxml::debug("acc is $acc");
 3968:     if (!defined($acc) || $acc =~ /^\s*$/ || $acc =~/^\s*no\s*$/i) {
 3969:         return 1;
 3970:     }
 3971:     my $allowed=0;
 3972:     my $ip=$env{'request.host'} || $ENV{'REMOTE_ADDR'};
 3973: 
 3974:     my $name;
 3975:     foreach my $pattern (split(',',$acc)) {
 3976:         $pattern =~ s/^\s*//;
 3977:         $pattern =~ s/\s*$//;
 3978:         if ($pattern =~ /\*$/) {
 3979:             #35.8.*
 3980:             $pattern=~s/\*//;
 3981:             if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
 3982:         } elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {
 3983:             #35.8.3.[34-56]
 3984:             my $low=$2;
 3985:             my $high=$3;
 3986:             $pattern=$1;
 3987:             if ($ip =~ /^\Q$pattern\E/) {
 3988:                 my $last=(split(/\./,$ip))[3];
 3989:                 if ($last <=$high && $last >=$low) { $allowed=1; }
 3990:             }
 3991:         } elsif ($pattern =~ /^\*/) {
 3992:             #*.msu.edu
 3993:             $pattern=~s/\*//;
 3994:             if (!defined($name)) {
 3995:                 use Socket;
 3996:                 my $netaddr=inet_aton($ip);
 3997:                 ($name)=gethostbyaddr($netaddr,AF_INET);
 3998:             }
 3999:             if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
 4000:         } elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
 4001:             #127.0.0.1
 4002:             if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
 4003:         } else {
 4004:             #some.name.com
 4005:             if (!defined($name)) {
 4006:                 use Socket;
 4007:                 my $netaddr=inet_aton($ip);
 4008:                 ($name)=gethostbyaddr($netaddr,AF_INET);
 4009:             }
 4010:             if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
 4011:         }
 4012:         if ($allowed) { last; }
 4013:     }
 4014:     return $allowed;
 4015: }
 4016: 
 4017: ###############################################
 4018: 
 4019: =pod
 4020: 
 4021: =head1 Domain Template Functions
 4022: 
 4023: =over 4
 4024: 
 4025: =item * &determinedomain()
 4026: 
 4027: Inputs: $domain (usually will be undef)
 4028: 
 4029: Returns: Determines which domain should be used for designs
 4030: 
 4031: =cut
 4032: 
 4033: ###############################################
 4034: sub determinedomain {
 4035:     my $domain=shift;
 4036:     if (! $domain) {
 4037:         # Determine domain if we have not been given one
 4038:         $domain = $Apache::lonnet::perlvar{'lonDefDomain'};
 4039:         if ($env{'user.domain'}) { $domain=$env{'user.domain'}; }
 4040:         if ($env{'request.role.domain'}) { 
 4041:             $domain=$env{'request.role.domain'}; 
 4042:         }
 4043:     }
 4044:     return $domain;
 4045: }
 4046: ###############################################
 4047: 
 4048: sub devalidate_domconfig_cache {
 4049:     my ($udom)=@_;
 4050:     &Apache::lonnet::devalidate_cache_new('domainconfig',$udom);
 4051: }
 4052: 
 4053: # ---------------------- Get domain configuration for a domain
 4054: sub get_domainconf {
 4055:     my ($udom) = @_;
 4056:     my $cachetime=1800;
 4057:     my ($result,$cached)=&Apache::lonnet::is_cached_new('domainconfig',$udom);
 4058:     if (defined($cached)) { return %{$result}; }
 4059: 
 4060:     my %domconfig = &Apache::lonnet::get_dom('configuration',
 4061: 					     ['login','rolecolors'],$udom);
 4062:     my (%designhash,%legacy);
 4063:     if (keys(%domconfig) > 0) {
 4064:         if (ref($domconfig{'login'}) eq 'HASH') {
 4065:             if (keys(%{$domconfig{'login'}})) {
 4066:                 foreach my $key (keys(%{$domconfig{'login'}})) {
 4067:                     if (ref($domconfig{'login'}{$key}) eq 'HASH') {
 4068:                         foreach my $img (keys(%{$domconfig{'login'}{$key}})) {
 4069:                             $designhash{$udom.'.login.'.$key.'_'.$img} = 
 4070:                                 $domconfig{'login'}{$key}{$img};
 4071:                         }
 4072:                     } else {
 4073:                         $designhash{$udom.'.login.'.$key}=$domconfig{'login'}{$key};
 4074:                     }
 4075:                 }
 4076:             } else {
 4077:                 $legacy{'login'} = 1;
 4078:             }
 4079:         } else {
 4080:             $legacy{'login'} = 1;
 4081:         }
 4082:         if (ref($domconfig{'rolecolors'}) eq 'HASH') {
 4083:             if (keys(%{$domconfig{'rolecolors'}})) {
 4084:                 foreach my $role (keys(%{$domconfig{'rolecolors'}})) {
 4085:                     if (ref($domconfig{'rolecolors'}{$role}) eq 'HASH') {
 4086:                         foreach my $item (keys(%{$domconfig{'rolecolors'}{$role}})) {
 4087:                             $designhash{$udom.'.'.$role.'.'.$item}=$domconfig{'rolecolors'}{$role}{$item};
 4088:                         }
 4089:                     }
 4090:                 }
 4091:             } else {
 4092:                 $legacy{'rolecolors'} = 1;
 4093:             }
 4094:         } else {
 4095:             $legacy{'rolecolors'} = 1;
 4096:         }
 4097:         if (keys(%legacy) > 0) {
 4098:             my %legacyhash = &get_legacy_domconf($udom);
 4099:             foreach my $item (keys(%legacyhash)) {
 4100:                 if ($item =~ /^\Q$udom\E\.login/) {
 4101:                     if ($legacy{'login'}) { 
 4102:                         $designhash{$item} = $legacyhash{$item};
 4103:                     }
 4104:                 } else {
 4105:                     if ($legacy{'rolecolors'}) {
 4106:                         $designhash{$item} = $legacyhash{$item};
 4107:                     }
 4108:                 }
 4109:             }
 4110:         }
 4111:     } else {
 4112:         %designhash = &get_legacy_domconf($udom); 
 4113:     }
 4114:     &Apache::lonnet::do_cache_new('domainconfig',$udom,\%designhash,
 4115: 				  $cachetime);
 4116:     return %designhash;
 4117: }
 4118: 
 4119: sub get_legacy_domconf {
 4120:     my ($udom) = @_;
 4121:     my %legacyhash;
 4122:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
 4123:     my $designfile =  $designdir.'/'.$udom.'.tab';
 4124:     if (-e $designfile) {
 4125:         if ( open (my $fh,"<$designfile") ) {
 4126:             while (my $line = <$fh>) {
 4127:                 next if ($line =~ /^\#/);
 4128:                 chomp($line);
 4129:                 my ($key,$val)=(split(/\=/,$line));
 4130:                 if ($val) { $legacyhash{$udom.'.'.$key}=$val; }
 4131:             }
 4132:             close($fh);
 4133:         }
 4134:     }
 4135:     if (-e '/home/httpd/html/adm/lonDomLogos/'.$udom.'.gif') {
 4136:         $legacyhash{$udom.'.login.domlogo'} = "/adm/lonDomLogos/$udom.gif";
 4137:     }
 4138:     return %legacyhash;
 4139: }
 4140: 
 4141: =pod
 4142: 
 4143: =item * &domainlogo()
 4144: 
 4145: Inputs: $domain (usually will be undef)
 4146: 
 4147: Returns: A link to a domain logo, if the domain logo exists.
 4148: If the domain logo does not exist, a description of the domain.
 4149: 
 4150: =cut
 4151: 
 4152: ###############################################
 4153: sub domainlogo {
 4154:     my $domain = &determinedomain(shift);
 4155:     my %designhash = &get_domainconf($domain);    
 4156:     # See if there is a logo
 4157:     if ($designhash{$domain.'.login.domlogo'} ne '') {
 4158:         my $imgsrc = $designhash{$domain.'.login.domlogo'};
 4159:         if ($imgsrc =~ m{^/(adm|res)/}) {
 4160: 	    if ($imgsrc =~ m{^/res/}) {
 4161: 		my $local_name = &Apache::lonnet::filelocation('',$imgsrc);
 4162: 		&Apache::lonnet::repcopy($local_name);
 4163: 	    }
 4164: 	   $imgsrc = &lonhttpdurl($imgsrc);
 4165:         } 
 4166:         return '<img src="'.$imgsrc.'" alt="'.$domain.'" />';
 4167:     } elsif (defined(&Apache::lonnet::domain($domain,'description'))) {
 4168:         return &Apache::lonnet::domain($domain,'description');
 4169:     } else {
 4170:         return '';
 4171:     }
 4172: }
 4173: ##############################################
 4174: 
 4175: =pod
 4176: 
 4177: =item * &designparm()
 4178: 
 4179: Inputs: $which parameter; $domain (usually will be undef)
 4180: 
 4181: Returns: value of designparamter $which
 4182: 
 4183: =cut
 4184: 
 4185: 
 4186: ##############################################
 4187: sub designparm {
 4188:     my ($which,$domain)=@_;
 4189:     if (exists($env{'environment.color.'.$which})) {
 4190:         return $env{'environment.color.'.$which};
 4191:     }
 4192:     $domain=&determinedomain($domain);
 4193:     my %domdesign = &get_domainconf($domain);
 4194:     my $output;
 4195:     if ($domdesign{$domain.'.'.$which} ne '') {
 4196:         $output = $domdesign{$domain.'.'.$which};
 4197:     } else {
 4198:         $output = $defaultdesign{$which};
 4199:     }
 4200:     if (($which =~ /^(student|coordinator|author|admin)\.img$/) ||
 4201:         ($which =~ /login\.(img|logo|domlogo|login)/)) {
 4202:         if ($output =~ m{^/(adm|res)/}) {
 4203:             if ($output =~ m{^/res/}) {
 4204:                 my $local_name = &Apache::lonnet::filelocation('',$output);
 4205:                 &Apache::lonnet::repcopy($local_name);
 4206:             }
 4207:             $output = &lonhttpdurl($output);
 4208:         }
 4209:     }
 4210:     return $output;
 4211: }
 4212: 
 4213: ##############################################
 4214: =pod
 4215: 
 4216: =item * &authorspace()
 4217: 
 4218: Inputs: ./.
 4219: 
 4220: Returns: Path to the Construction Space of the current user's
 4221:          accessed author space
 4222:          The author space will be that of the current user
 4223:          when accessing the own author space
 4224:          and that of the co-author/assistent co-author
 4225:          when accessing the co-author's/assistent co-author's
 4226:          space
 4227: 
 4228: =cut
 4229: 
 4230: sub authorspace {
 4231:     my $caname = '';
 4232:     if ($env{'request.role'} =~ /^ca|^aa/) {
 4233:         (undef,$caname) =
 4234:             ($env{'request.role'}=~/($match_domain)\/($match_username)$/);
 4235:     } else {
 4236:         $caname = $env{'user.name'};
 4237:     }
 4238:     return '/priv/'.$caname.'/';
 4239: }
 4240: 
 4241: ##############################################
 4242: =pod
 4243: 
 4244: =item * &head_subbox()
 4245: 
 4246: Inputs: $content (contains HTML code with page functions, etc.)
 4247: 
 4248: Returns: HTML div with $content
 4249:          To be included in page header
 4250: 
 4251: =cut
 4252: 
 4253: sub head_subbox {
 4254:     my ($content)=@_;
 4255:     my $output =
 4256:         '<div id="LC_head_subbox">'
 4257:        .$content
 4258:        .'</div>'
 4259: }
 4260: 
 4261: ##############################################
 4262: =pod
 4263: 
 4264: =item * &CSTR_pageheader()
 4265: 
 4266: Inputs: ./.
 4267: 
 4268: Returns: HTML div with CSTR path and recent box
 4269:          To be included on Construction Space pages
 4270: 
 4271: =cut
 4272: 
 4273: sub CSTR_pageheader {
 4274:     # this is for resources; directories have customtitle, and crumbs
 4275:             # and select recent are created in lonpubdir.pm  
 4276:     my ($uname,$thisdisfn)=
 4277:         ($env{'request.filename'} =~ m|^/home/([^/]+)/public_html/(.*)|);
 4278:     my $formaction='/priv/'.$uname.'/'.$thisdisfn;
 4279:     $formaction=~s/\/+/\//g;
 4280: 
 4281:     my $parentpath = '';
 4282:     my $lastitem = '';
 4283:     if ($thisdisfn =~ m-(.+/)([^/]*)$-) {
 4284:         $parentpath = $1;
 4285:         $lastitem = $2;
 4286:     } else {
 4287:         $lastitem = $thisdisfn;
 4288:     }
 4289:     return
 4290:          '<div>'
 4291:         .&Apache::loncommon::help_open_menu('','',3,'Authoring') #FIXME: Broken? Where is it?
 4292:         .'<b>'.&mt('Construction Space:').'</b> '
 4293:         .'<form name="dirs" method="post" action="'.$formaction
 4294:         .'" target="_top"><tt><b>' #FIXME lonpubdir: target="_parent"
 4295:         .&Apache::lonhtmlcommon::crumbs($uname.'/'.$parentpath,'_top','/priv','','+1',1)."$lastitem</b></tt><br />"
 4296:         #FIXME lonpubdir: &Apache::lonhtmlcommon::crumbs($uname.$thisdisfn.'/','_top','/priv','','+1',1)."</b></tt><br />"
 4297:         .&Apache::lonhtmlcommon::select_recent('construct','recent','this.form.action=this.form.recent.value;this.form.submit()')
 4298:         .'</form>'
 4299:         .&Apache::lonmenu::constspaceform()
 4300:         .'</div>';
 4301: }
 4302: 
 4303: ###############################################
 4304: ###############################################
 4305: 
 4306: =pod
 4307: 
 4308: =back
 4309: 
 4310: =head1 HTML Helpers
 4311: 
 4312: =over 4
 4313: 
 4314: =item * &bodytag()
 4315: 
 4316: Returns a uniform header for LON-CAPA web pages.
 4317: 
 4318: Inputs: 
 4319: 
 4320: =over 4
 4321: 
 4322: =item * $title, A title to be displayed on the page.
 4323: 
 4324: =item * $function, the current role (can be undef).
 4325: 
 4326: =item * $addentries, extra parameters for the <body> tag.
 4327: 
 4328: =item * $bodyonly, if defined, only return the <body> tag.
 4329: 
 4330: =item * $domain, if defined, force a given domain.
 4331: 
 4332: =item * $forcereg, if page should register as content page (relevant for 
 4333:             text interface only)
 4334: 
 4335: =item * $no_nav_bar, if true, keep the 'what is this' info but remove the
 4336:                      navigational links
 4337: 
 4338: =item * $bgcolor, used to override the bgcolor on a webpage to a specific value
 4339: 
 4340: =item * $no_inline_link, if true and in remote mode, don't show the 
 4341:          'Switch To Inline Menu' link
 4342: 
 4343: =item * $args, optional argument valid values are
 4344:             no_auto_mt_title -> prevents &mt()ing the title arg
 4345:             inherit_jsmath -> when creating popup window in a page,
 4346:                               should it have jsmath forced on by the
 4347:                               current page
 4348: 
 4349: =back
 4350: 
 4351: Returns: A uniform header for LON-CAPA web pages.  
 4352: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
 4353: If $bodyonly is undef or zero, an html string containing a <body> tag and 
 4354: other decorations will be returned.
 4355: 
 4356: =cut
 4357: 
 4358: sub bodytag {
 4359:     my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,
 4360:         $no_nav_bar,$bgcolor,$no_inline_link,$args)=@_;
 4361: 
 4362:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
 4363: 
 4364:     $function = &get_users_function() if (!$function);
 4365:     my $img =    &designparm($function.'.img',$domain);
 4366:     my $font =   &designparm($function.'.font',$domain);
 4367:     my $pgbg   = $bgcolor || &designparm($function.'.pgbg',$domain);
 4368: 
 4369:     my %design = ( 'style'   => 'margin-top: 0',
 4370: 		   'bgcolor' => $pgbg,
 4371: 		   'text'    => $font,
 4372:                    'alink'   => &designparm($function.'.alink',$domain),
 4373: 		   'vlink'   => &designparm($function.'.vlink',$domain),
 4374: 		   'link'    => &designparm($function.'.link',$domain),);
 4375:     @design{keys(%$addentries)} = @$addentries{keys(%$addentries)}; 
 4376: 
 4377:  # role and realm
 4378:     my ($role,$realm) = split(/\./,$env{'request.role'},2);
 4379:     if ($role  eq 'ca') {
 4380:         my ($rdom,$rname) = ($realm =~ m{^/($match_domain)/($match_username)$});
 4381:         $realm = &plainname($rname,$rdom);
 4382:     } 
 4383: # realm
 4384:     if ($env{'request.course.id'}) {
 4385:         if ($env{'request.role'} !~ /^cr/) {
 4386:             $role = &Apache::lonnet::plaintext($role,&course_type());
 4387:         }
 4388: 	$realm = $env{'course.'.$env{'request.course.id'}.'.description'};
 4389:     } else {
 4390:         $role = &Apache::lonnet::plaintext($role);
 4391:     }
 4392: 
 4393:     if (!$realm) { $realm='&nbsp;'; }
 4394: # Set messages
 4395:     my $messages=&domainlogo($domain);
 4396: 
 4397:     my $extra_body_attr = &make_attr_string($forcereg,\%design);
 4398: 
 4399: # construct main body tag
 4400:     my $bodytag = "<body $extra_body_attr>".
 4401: 	&Apache::lontexconvert::init_math_support($args->{'inherit_jsmath'});
 4402: 
 4403:     if ($bodyonly) {
 4404:         return $bodytag;
 4405:     } 
 4406: 
 4407:     my $name = &plainname($env{'user.name'},$env{'user.domain'});
 4408:     if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 4409: 	undef($role);
 4410:     } else {
 4411: 	$name = &aboutmewrapper($name,$env{'user.name'},$env{'user.domain'});
 4412:     }
 4413:     
 4414:     my $titleinfo = '<h1>'.$title.'</h1>';
 4415:     #
 4416:     # Extra info if you are the DC
 4417:     my $dc_info = '';
 4418:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
 4419:                         $env{'course.'.$env{'request.course.id'}.
 4420:                                  '.domain'}.'/'})) {
 4421:         my $cid = $env{'request.course.id'};
 4422:         $dc_info.= $cid.' '.$env{'course.'.$cid.'.internal.coursecode'};
 4423:         $dc_info =~ s/\s+$//;
 4424:         $dc_info = '('.$dc_info.')';
 4425:     }
 4426: 
 4427:     $role = "($role)" if $role;
 4428:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
 4429: 
 4430:     if ($env{'environment.remote'} eq 'off') {
 4431:         # No Remote
 4432: 	if ($env{'request.state'} eq 'construct') {
 4433: 	    $forcereg=1;
 4434: 	}
 4435: 
 4436: #    if ($env{'request.state'} eq 'construct') {
 4437: #        $titleinfo = &CSTR_pageheader(); #FIXME: Will be removed once all scripts have their own calls
 4438: #    }
 4439: 
 4440:         my $titletable = '<table id="LC_title_bar">'
 4441:                         ."<tr><td> $titleinfo $dc_info</td>"
 4442:                         .'</tr></table>';
 4443: 
 4444: 	if ($no_nav_bar) {
 4445: 	    $bodytag .= $titletable;
 4446: 	} else {
 4447:         $bodytag .= qq|<div id="LC_nav_bar">$name $role<br />
 4448:             <em>$realm</em> $dc_info</div>| unless $env{'form.inhibitmenu'};
 4449: 
 4450: 	    if ($env{'request.state'} eq 'construct') {
 4451:                 $bodytag .= &Apache::lonmenu::menubuttons($forcereg,$titletable);
 4452:             } else {
 4453:                 $bodytag .= &Apache::lonmenu::menubuttons($forcereg).$titletable;
 4454:             }
 4455:         }
 4456:         return $bodytag;
 4457:     }
 4458: 
 4459: #
 4460: # Top frame rendering, Remote is up
 4461: #
 4462: 
 4463:     my $imgsrc = $img;
 4464:     if ($img =~ /^\/adm/) {
 4465:         $imgsrc = &lonhttpdurl($img);
 4466:     }
 4467:     my $upperleft='<img src="'.$imgsrc.'" alt="'.$function.'" />';
 4468: 
 4469:     # Explicit link to get inline menu
 4470:     my $menu= ($no_inline_link?''
 4471: 	       :'<a href="/adm/remote?action=collapse">'.&mt('Switch to Inline Menu Mode').'</a>');
 4472:     $bodytag .= qq|<div id="LC_nav_bar">$name $role
 4473:             <em>$realm</em> $dc_info </div>
 4474:             <ol class="LC_smallMenu LC_right">
 4475:                 <li>$menu</li>
 4476:             </ol>| unless $env{'form.inhibitmenu'};
 4477:     #
 4478:     return(<<ENDBODY);
 4479: $bodytag
 4480: <table id="LC_title_bar" class="LC_with_remote">
 4481: <tr><td>$upperleft</td>
 4482:     <td>$messages&nbsp;</td>
 4483: </tr>
 4484: <tr><td>$titleinfo $dc_info $menu</td>
 4485: </tr>
 4486: </table>
 4487: ENDBODY
 4488: }
 4489: 
 4490: sub make_attr_string {
 4491:     my ($register,$attr_ref) = @_;
 4492: 
 4493:     if ($attr_ref && !ref($attr_ref)) {
 4494: 	die("addentries Must be a hash ref ".
 4495: 	    join(':',caller(1))." ".
 4496: 	    join(':',caller(0))." ");
 4497:     }
 4498: 
 4499:     if ($register) {
 4500: 	my ($on_load,$on_unload);
 4501: 	foreach my $key (keys(%{$attr_ref})) {
 4502: 	    if      (lc($key) eq 'onload') {
 4503: 		$on_load.=$attr_ref->{$key}.';';
 4504: 		delete($attr_ref->{$key});
 4505: 
 4506: 	    } elsif (lc($key) eq 'onunload') {
 4507: 		$on_unload.=$attr_ref->{$key}.';';
 4508: 		delete($attr_ref->{$key});
 4509: 	    }
 4510: 	}
 4511: 	$attr_ref->{'onload'}  =
 4512: 	    &Apache::lonmenu::loadevents().  $on_load;
 4513: 	$attr_ref->{'onunload'}=
 4514: 	    &Apache::lonmenu::unloadevents().$on_unload;
 4515:     }
 4516: 
 4517: # Accessibility font enhance
 4518:     if ($env{'browser.fontenhance'} eq 'on') {
 4519: 	my $style;
 4520: 	foreach my $key (keys(%{$attr_ref})) {
 4521: 	    if (lc($key) eq 'style') {
 4522: 		$style.=$attr_ref->{$key}.';';
 4523: 		delete($attr_ref->{$key});
 4524: 	    }
 4525: 	}
 4526: 	$attr_ref->{'style'}=$style.'; font-size: x-large;';
 4527:     }
 4528: 
 4529:     my $attr_string;
 4530:     foreach my $attr (keys(%$attr_ref)) {
 4531: 	$attr_string .= " $attr=\"".$attr_ref->{$attr}.'" ';
 4532:     }
 4533:     return $attr_string;
 4534: }
 4535: 
 4536: 
 4537: ###############################################
 4538: ###############################################
 4539: 
 4540: =pod
 4541: 
 4542: =item * &endbodytag()
 4543: 
 4544: Returns a uniform footer for LON-CAPA web pages.
 4545: 
 4546: Inputs: 1 - optional reference to an args hash
 4547: If in the hash, key for noredirectlink has a value which evaluates to true,
 4548: a 'Continue' link is not displayed if the page contains an
 4549: internal redirect in the <head></head> section,
 4550: i.e., $env{'internal.head.redirect'} exists   
 4551: 
 4552: =cut
 4553: 
 4554: sub endbodytag {
 4555:     my ($args) = @_;
 4556:     my $endbodytag='</body>';
 4557:     $endbodytag=&Apache::lontexconvert::jsMath_process()."\n".$endbodytag;
 4558:     if ( exists( $env{'internal.head.redirect'} ) ) {
 4559:         if (!(ref($args) eq 'HASH' && $args->{'noredirectlink'})) {
 4560: 	    $endbodytag=
 4561: 	        "<br /><a href=\"$env{'internal.head.redirect'}\">".
 4562: 	        &mt('Continue').'</a>'.
 4563: 	        $endbodytag;
 4564:         }
 4565:     }
 4566:     return $endbodytag;
 4567: }
 4568: 
 4569: =pod
 4570: 
 4571: =item * &standard_css()
 4572: 
 4573: Returns a style sheet
 4574: 
 4575: Inputs: (all optional)
 4576:             domain         -> force to color decorate a page for a specific
 4577:                                domain
 4578:             function       -> force usage of a specific rolish color scheme
 4579:             bgcolor        -> override the default page bgcolor
 4580: 
 4581: =cut
 4582: 
 4583: sub standard_css {
 4584:     my ($function,$domain,$bgcolor) = @_;
 4585:     $function  = &get_users_function() if (!$function);
 4586:     my $img    = &designparm($function.'.img',   $domain);
 4587:     my $tabbg  = &designparm($function.'.tabbg', $domain);
 4588:     my $font   = &designparm($function.'.font',  $domain);
 4589:     my $fontmenu = &designparm($function.'.fontmenu', $domain);
 4590: #second colour for later usage
 4591:     my $sidebg = &designparm($function.'.sidebg',$domain);
 4592:     my $pgbg_or_bgcolor =
 4593: 	         $bgcolor ||
 4594: 	         &designparm($function.'.pgbg',  $domain);
 4595:     my $pgbg   = &designparm($function.'.pgbg',  $domain);
 4596:     my $alink  = &designparm($function.'.alink', $domain);
 4597:     my $vlink  = &designparm($function.'.vlink', $domain);
 4598:     my $link   = &designparm($function.'.link',  $domain);
 4599: 
 4600:     my $loginbg = &designparm('login.sidebg',$domain);
 4601:     my $bgcol = &designparm('login.bgcol',$domain);
 4602:     my $textcol = &designparm('login.textcol',$domain);
 4603: 
 4604:     my $sans                 = 'Verdana,Arial,Helvetica,sans-serif';
 4605:     my $mono                 = 'monospace';
 4606:     my $data_table_head      = $sidebg;
 4607:     my $data_table_light     = '#FAFAFA';
 4608:     my $data_table_dark      = '#F0F0F0';
 4609:     my $data_table_darker    = '#CCCCCC';
 4610:     my $data_table_highlight = '#FFFF00';
 4611:     my $mail_new             = '#FFBB77';
 4612:     my $mail_new_hover       = '#DD9955';
 4613:     my $mail_read            = '#BBBB77';
 4614:     my $mail_read_hover      = '#999944';
 4615:     my $mail_replied         = '#AAAA88';
 4616:     my $mail_replied_hover   = '#888855';
 4617:     my $mail_other           = '#99BBBB';
 4618:     my $mail_other_hover     = '#669999';
 4619:     my $table_header         = '#DDDDDD';
 4620:     my $feedback_link_bg     = '#BBBBBB';
 4621:     my $lg_border_color	     = '#C8C8C8';
 4622: 
 4623:     my $border = ($env{'browser.type'} eq 'explorer' ||
 4624: 		  $env{'browser.type'} eq 'safari'     ) ? '0 2px 0 2px'
 4625: 	                                                 : '0 3px 0 4px';
 4626: 
 4627: 
 4628:     return <<END;
 4629: body {
 4630:    font-family: $sans;
 4631:    line-height:130%;
 4632:    font-size:0.83em;
 4633:    color:$font;
 4634: }
 4635: 
 4636: a:link, a:visited { 
 4637:   font-size:100%; 
 4638: }
 4639: 
 4640: a:focus { 
 4641:   color: red;
 4642:   background: yellow 
 4643: }
 4644: 
 4645: hr {
 4646:   clear: both;
 4647:   color: $tabbg;
 4648:   background-color: $tabbg;
 4649:   height: 3px;
 4650:   border: none;
 4651: }
 4652: 
 4653: form, .inline { 
 4654:    display: inline; 
 4655: }
 4656: 
 4657: .LC_right {
 4658:    text-align:right;
 4659: }
 4660: 
 4661: .LC_middle {
 4662:    vertical-align:middle;
 4663: }
 4664: 
 4665: /* just for tests */
 4666: .LC_400Box {width:400px; }
 4667: /* end */
 4668: 
 4669: .LC_filename {
 4670:   font-family: $mono;
 4671:   white-space:pre;
 4672: }
 4673: 
 4674: .LC_fileicon {
 4675:   border: none;
 4676:   height: 1.3em;
 4677:   vertical-align: text-bottom;
 4678:   margin-right: 0.3em;
 4679:   text-decoration:none;
 4680: }
 4681: 
 4682: .LC_error {
 4683:   color: red;
 4684:   font-size: larger;
 4685: }
 4686: 
 4687: .LC_warning,
 4688: .LC_diff_removed {
 4689:   color: red;
 4690: }
 4691: 
 4692: .LC_info,
 4693: .LC_success,
 4694: .LC_diff_added {
 4695:   color: green;
 4696: }
 4697: 
 4698: div.LC_confirm_box {
 4699:   background-color: #FAFAFA;
 4700:   border: 1px solid $lg_border_color;
 4701:   margin-right: 0;
 4702:   padding: 5px;
 4703: }
 4704: 
 4705: div.LC_confirm_box .LC_error img,
 4706: div.LC_confirm_box .LC_success img {
 4707:   vertical-align: middle;
 4708: }
 4709: 
 4710: .LC_icon {
 4711:   border: none;
 4712:   vertical-align: middle;
 4713: }
 4714: 
 4715: .LC_docs_spacer {
 4716:   width: 25px;
 4717:   height: 1px;
 4718:   border: none;
 4719: }
 4720: 
 4721: .LC_internal_info {
 4722:   color: #999999;
 4723: }
 4724: 
 4725: .LC_discussion {
 4726:    background: $tabbg;
 4727:    border: 1px solid black;
 4728:    margin: 2px;
 4729: }
 4730: 
 4731: .LC_disc_action_links_bar {
 4732:    background: $tabbg;
 4733:    border: none;
 4734:    margin: 4px;
 4735: }
 4736: 
 4737: .LC_disc_action_left {
 4738:    text-align: left;
 4739: }
 4740: 
 4741: .LC_disc_action_right {
 4742:    text-align: right;
 4743: }
 4744: 
 4745: .LC_disc_new_item {
 4746:    background: white;
 4747:    border: 2px solid red;
 4748:    margin: 2px;
 4749: }
 4750: 
 4751: .LC_disc_old_item {
 4752:    background: white;
 4753:    border: 1px solid black;
 4754:    margin: 2px;
 4755: }
 4756: 
 4757: table.LC_pastsubmission {
 4758:   border: 1px solid black;
 4759:   margin: 2px;
 4760: }
 4761: 
 4762: table#LC_top_nav,
 4763: table#LC_menubuttons,
 4764: table#LC_nav_location {
 4765:   width: 100%;
 4766:   background: $pgbg;
 4767:   border: 2px;
 4768:   border-collapse: separate;
 4769:   padding: 0;
 4770: }
 4771: 
 4772: table#LC_title_bar a {
 4773:   color: $fontmenu;
 4774: }
 4775: 
 4776: table#LC_title_bar {
 4777:   clear: both;
 4778:   display: none;
 4779: }
 4780: 
 4781: table#LC_title_bar,
 4782: table.LC_breadcrumbs,
 4783: table#LC_title_bar.LC_with_remote {
 4784:   width: 100%;
 4785:   border-color: $pgbg;
 4786:   border-style: solid;
 4787:   border-width: $border;
 4788:   background: $pgbg;
 4789:   color: $fontmenu;
 4790:   border-collapse: collapse;
 4791:   padding: 0;
 4792:   margin: 0;
 4793: }
 4794: 
 4795: table#LC_title_bar td {
 4796:   background: $tabbg;
 4797: }
 4798: 
 4799: table#LC_menubuttons img{
 4800:   border: none;
 4801: }
 4802: 
 4803: table#LC_top_nav td {
 4804:   background: $tabbg;
 4805:   border: none;
 4806:   font-size: small;
 4807:   vertical-align:top;
 4808:   padding:2px 5px 2px 5px;
 4809: }
 4810: 
 4811: table#LC_top_nav td a,
 4812: div#LC_top_nav a {
 4813:   color: $font;
 4814: }
 4815: 
 4816: table#LC_top_nav td.LC_top_nav_logo {
 4817:   background: $tabbg;
 4818:   text-align: left;
 4819:   white-space: nowrap;
 4820:   width: 31px;
 4821: }
 4822: 
 4823: table#LC_top_nav td.LC_top_nav_logo img {
 4824:   border: none;
 4825:   vertical-align: bottom;
 4826: }
 4827: 
 4828: table#LC_top_nav td.LC_top_nav_exit,
 4829: table#LC_top_nav td.LC_top_nav_help {
 4830:   width: 2.0em;
 4831: }
 4832: 
 4833: table#LC_top_nav td.LC_top_nav_login {
 4834:   width: 4.0em;
 4835:   text-align: center;
 4836: }
 4837: 
 4838: .LC_breadcrumbs_component {
 4839:     float: right;
 4840:     margin: 0 1em;
 4841: }
 4842: .LC_breadcrumbs_component img {
 4843:     vertical-align: middle;
 4844: }
 4845: 
 4846: td.LC_table_cell_checkbox {
 4847:   text-align: center;
 4848: }
 4849: 
 4850: table#LC_mainmenu td.LC_mainmenu_column {
 4851:     vertical-align: top;
 4852: }
 4853: 
 4854: .LC_fontsize_small {
 4855:  font-size: 70%;
 4856: }
 4857: 
 4858: #LC_breadcrumbs {
 4859:  clear:both;
 4860:  background: $sidebg;
 4861:  border-bottom: 1px solid $lg_border_color;
 4862:  line-height: 32px; 
 4863:  margin: 0;
 4864:  padding: 0;
 4865: }
 4866: 
 4867: /* Preliminary fix to hide breadcrumbs inside remote control window */
 4868: #LC_remote #LC_breadcrumbs {
 4869:     display:none;
 4870: }
 4871: 
 4872: #LC_head_subbox {
 4873:  clear:both;
 4874:  background: #F8F8F8; /* $sidebg; */
 4875:  border-bottom: 1px solid $lg_border_color;
 4876:  margin: 0 0 10px 0;
 4877:  padding: 5px;
 4878: }
 4879: 
 4880: .LC_fontsize_medium {
 4881:  font-size: 85%;
 4882: }
 4883: 
 4884: .LC_fontsize_large {
 4885:  font-size: 120%;
 4886: }
 4887: 
 4888: .LC_menubuttons_inline_text {
 4889:   color: $font;
 4890:   font-size: 90%;
 4891:   padding-left:3px;
 4892: }
 4893: 
 4894: .LC_menubuttons_link {
 4895:   text-decoration: none;
 4896: }
 4897: 
 4898: .LC_menubuttons_category {
 4899:   color: $font;
 4900:   background: $pgbg;
 4901:   font-size: larger;
 4902:   font-weight: bold;
 4903: }
 4904: 
 4905: td.LC_menubuttons_text {
 4906:  	color: $font;
 4907: }
 4908: 
 4909: .LC_current_location {
 4910:   background: $tabbg;
 4911: }
 4912: 
 4913: .LC_new_mail {
 4914:   background: $tabbg;
 4915:   font-weight: bold;
 4916: }
 4917: 
 4918: table.LC_data_table,
 4919: table.LC_mail_list {
 4920:   border: 1px solid #000000;
 4921:   border-collapse: separate;
 4922:   border-spacing: 1px;
 4923:   background: $pgbg;
 4924: }
 4925: 
 4926: .LC_data_table_dense {
 4927:   font-size: small;
 4928: }
 4929: 
 4930: table.LC_nested_outer {
 4931:   border: 1px solid #000000;
 4932:   border-collapse: collapse;
 4933:   border-spacing: 0;
 4934:   width: 100%;
 4935: }
 4936: 
 4937: table.LC_innerpickbox,
 4938: table.LC_nested {
 4939:   border: none;
 4940:   border-collapse: collapse;
 4941:   border-spacing: 0;
 4942:   width: 100%;
 4943: }
 4944: 
 4945: table.LC_data_table tr th, 
 4946: table.LC_calendar tr th, 
 4947: table.LC_mail_list tr th,
 4948: table.LC_prior_tries tr th,
 4949: table.LC_innerpickbox tr th {
 4950:   font-weight: bold;
 4951:   background-color: $data_table_head;
 4952:   color:$fontmenu;
 4953:   font-size:90%;
 4954: }
 4955: 
 4956: table.LC_innerpickbox tr th,
 4957: table.LC_innerpickbox tr td {
 4958:   vertical-align: top;
 4959: }
 4960: 
 4961: table.LC_data_table tr.LC_info_row > td {
 4962:   background-color: #CCCCCC;
 4963:   font-weight: bold;
 4964:   text-align: left;
 4965: }
 4966: 
 4967: table.LC_data_table tr.LC_odd_row > td,
 4968: table.LC_pick_box tr > td.LC_odd_row {
 4969:   background-color: $data_table_light;
 4970:   padding: 2px;
 4971: }
 4972: 
 4973: table.LC_data_table tr.LC_even_row > td,
 4974: table.LC_pick_box tr > td.LC_even_row {
 4975:   background-color: $data_table_dark;
 4976:   padding: 2px;
 4977: }
 4978: 
 4979: table.LC_data_table tr.LC_data_table_highlight td {
 4980:   background-color: $data_table_darker;
 4981: }
 4982: 
 4983: table.LC_data_table tr td.LC_leftcol_header {
 4984:   background-color: $data_table_head;
 4985:   font-weight: bold;
 4986: }
 4987: 
 4988: table.LC_data_table tr.LC_empty_row td,
 4989: table.LC_nested tr.LC_empty_row td {
 4990:   background-color: #FFFFFF;
 4991:   font-weight: bold;
 4992:   font-style: italic;
 4993:   text-align: center;
 4994:   padding: 8px;
 4995: }
 4996: 
 4997: table.LC_nested tr.LC_empty_row td {
 4998:   padding: 4ex
 4999: }
 5000: 
 5001: table.LC_nested_outer tr th {
 5002:   font-weight: bold;
 5003:   color:$fontmenu;
 5004:   background-color: $data_table_head;
 5005:   font-size: small;
 5006:   border-bottom: 1px solid #000000;
 5007: }
 5008: 
 5009: table.LC_nested_outer tr td.LC_subheader {
 5010:   background-color: $data_table_head;
 5011:   font-weight: bold;
 5012:   font-size: small;
 5013:   border-bottom: 1px solid #000000;
 5014:   text-align: right;
 5015: }
 5016: 
 5017: table.LC_nested tr.LC_info_row td {
 5018:   background-color: #CCCCCC;
 5019:   font-weight: bold;
 5020:   font-size: small;
 5021:   text-align: center;
 5022: }
 5023: 
 5024: table.LC_nested tr.LC_info_row td.LC_left_item,
 5025: table.LC_nested_outer tr th.LC_left_item {
 5026:   text-align: left;
 5027: }
 5028: 
 5029: table.LC_nested td {
 5030:   background-color: #FFFFFF;
 5031:   font-size: small;
 5032: }
 5033: 
 5034: table.LC_nested_outer tr th.LC_right_item,
 5035: table.LC_nested tr.LC_info_row td.LC_right_item,
 5036: table.LC_nested tr.LC_odd_row td.LC_right_item,
 5037: table.LC_nested tr td.LC_right_item {
 5038:   text-align: right;
 5039: }
 5040: 
 5041: table.LC_nested tr.LC_odd_row td {
 5042:   background-color: #EEEEEE;
 5043: }
 5044: 
 5045: table.LC_createuser {
 5046: }
 5047: 
 5048: table.LC_createuser tr.LC_section_row td {
 5049:   font-size: small;
 5050: }
 5051: 
 5052: table.LC_createuser tr.LC_info_row td  {
 5053:   background-color: #CCCCCC;
 5054:   font-weight: bold;
 5055:   text-align: center;
 5056: }
 5057: 
 5058: table.LC_calendar {
 5059:   border: 1px solid #000000;
 5060:   border-collapse: collapse;
 5061: }
 5062: 
 5063: table.LC_calendar_pickdate {
 5064:   font-size: xx-small;
 5065: }
 5066: 
 5067: table.LC_calendar tr td {
 5068:   border: 1px solid #000000;
 5069:   vertical-align: top;
 5070: }
 5071: 
 5072: table.LC_calendar tr td.LC_calendar_day_empty {
 5073:   background-color: $data_table_dark;
 5074: }
 5075: 
 5076: table.LC_calendar tr td.LC_calendar_day_current {
 5077:   background-color: $data_table_highlight;
 5078: }
 5079: 
 5080: table.LC_mail_list tr.LC_mail_new {
 5081:   background-color: $mail_new;
 5082: }
 5083: 
 5084: table.LC_mail_list tr.LC_mail_new:hover {
 5085:   background-color: $mail_new_hover;
 5086: }
 5087: 
 5088: table.LC_mail_list tr.LC_mail_even {
 5089: }
 5090: 
 5091: table.LC_mail_list tr.LC_mail_odd {
 5092: }
 5093: 
 5094: table.LC_mail_list tr.LC_mail_read {
 5095:   background-color: $mail_read;
 5096: }
 5097: 
 5098: table.LC_mail_list tr.LC_mail_read:hover {
 5099:   background-color: $mail_read_hover;
 5100: }
 5101: 
 5102: table.LC_mail_list tr.LC_mail_replied {
 5103:   background-color: $mail_replied;
 5104: }
 5105: 
 5106: table.LC_mail_list tr.LC_mail_replied:hover {
 5107:   background-color: $mail_replied_hover;
 5108: }
 5109: 
 5110: table.LC_mail_list tr.LC_mail_other {
 5111:   background-color: $mail_other;
 5112: }
 5113: 
 5114: table.LC_mail_list tr.LC_mail_other:hover {
 5115:   background-color: $mail_other_hover;
 5116: }
 5117: 
 5118: table.LC_data_table tr > td.LC_browser_file,
 5119: table.LC_data_table tr > td.LC_browser_file_published {
 5120:   background: #CCFF88;
 5121: }
 5122: 
 5123: table.LC_data_table tr > td.LC_browser_file_locked,
 5124: table.LC_data_table tr > td.LC_browser_file_unpublished {
 5125:   background: #FFAA99;
 5126: }
 5127: 
 5128: table.LC_data_table tr > td.LC_browser_file_obsolete {
 5129:   background: #AAAAAA;
 5130: }
 5131: 
 5132: table.LC_data_table tr > td.LC_browser_file_modified,
 5133: table.LC_data_table tr > td.LC_browser_file_metamodified {
 5134:   background: #FFFF77;
 5135: }
 5136: 
 5137: table.LC_data_table tr.LC_browser_folder > td {
 5138:   background: #CCCCFF;
 5139: }
 5140: 
 5141: table.LC_data_table tr > td.LC_roles_is {
 5142: /*  background: #77FF77; */
 5143: }
 5144: 
 5145: table.LC_data_table tr > td.LC_roles_future {
 5146:   background: #FFFF77;
 5147: }
 5148: 
 5149: table.LC_data_table tr > td.LC_roles_will {
 5150:   background: #FFAA77;
 5151: }
 5152: 
 5153: table.LC_data_table tr > td.LC_roles_expired {
 5154:   background: #FF7777;
 5155: }
 5156: 
 5157: table.LC_data_table tr > td.LC_roles_will_not {
 5158:   background: #AAFF77;
 5159: }
 5160: 
 5161: table.LC_data_table tr > td.LC_roles_selected {
 5162:   background: #11CC55;
 5163: }
 5164: 
 5165: span.LC_current_location {
 5166:   font-size:larger;
 5167:   background: $pgbg;
 5168: }
 5169: 
 5170: span.LC_parm_menu_item {
 5171:   font-size: larger;
 5172: }
 5173: 
 5174: span.LC_parm_scope_all {
 5175:   color: red;
 5176: }
 5177: 
 5178: span.LC_parm_scope_folder {
 5179:   color: green;
 5180: }
 5181: 
 5182: span.LC_parm_scope_resource {
 5183:   color: orange;
 5184: }
 5185: 
 5186: span.LC_parm_part {
 5187:   color: blue;
 5188: }
 5189: 
 5190: span.LC_parm_folder, span.LC_parm_symb {
 5191:   font-size: x-small;
 5192:   font-family: $mono;
 5193:   color: #AAAAAA;
 5194: }
 5195: 
 5196: td.LC_parm_overview_level_menu,
 5197: td.LC_parm_overview_map_menu,
 5198: td.LC_parm_overview_parm_selectors,
 5199: td.LC_parm_overview_restrictions  {
 5200:   border: 1px solid black;
 5201:   border-collapse: collapse;
 5202: }
 5203: 
 5204: table.LC_parm_overview_restrictions td {
 5205:   border-width: 1px 4px 1px 4px;
 5206:   border-style: solid;
 5207:   border-color: $pgbg;
 5208:   text-align: center;
 5209: }
 5210: 
 5211: table.LC_parm_overview_restrictions th {
 5212:   background: $tabbg;
 5213:   border-width: 1px 4px 1px 4px;
 5214:   border-style: solid;
 5215:   border-color: $pgbg;
 5216: }
 5217: 
 5218: table#LC_helpmenu {
 5219:   border: none;
 5220:   height: 55px;
 5221:   border-spacing: 0;
 5222: }
 5223: 
 5224: table#LC_helpmenu fieldset legend {
 5225:   font-size: larger;
 5226: }
 5227: 
 5228: table#LC_helpmenu_links {
 5229:   width: 100%;
 5230:   border: 1px solid black;
 5231:   background: $pgbg;
 5232:   padding: 0;
 5233:   border-spacing: 1px;
 5234: }
 5235: 
 5236: table#LC_helpmenu_links tr td {
 5237:   padding: 1px;
 5238:   background: $tabbg;
 5239:   text-align: center;
 5240:   font-weight: bold;
 5241: }
 5242: 
 5243: table#LC_helpmenu_links a:link,
 5244: table#LC_helpmenu_links a:visited,
 5245: table#LC_helpmenu_links a:active {
 5246:   text-decoration: none;
 5247:   color: $font;
 5248: }
 5249: 
 5250: table#LC_helpmenu_links a:hover {
 5251:   text-decoration: underline;
 5252:   color: $vlink;
 5253: }
 5254: 
 5255: .LC_chrt_popup_exists {
 5256:   border: 1px solid #339933;
 5257:   margin: -1px;
 5258: }
 5259: 
 5260: .LC_chrt_popup_up {
 5261:   border: 1px solid yellow;
 5262:   margin: -1px;
 5263: }
 5264: 
 5265: .LC_chrt_popup {
 5266:   border: 1px solid #8888FF;
 5267:   background: #CCCCFF;
 5268: }
 5269: 
 5270: table.LC_pick_box {
 5271:   border-collapse: separate;
 5272:   background: white;
 5273:   border: 1px solid black;
 5274:   border-spacing: 1px;
 5275: }
 5276: 
 5277: table.LC_pick_box td.LC_pick_box_title {
 5278:   background: $sidebg;
 5279:   font-weight: bold;
 5280:   text-align: right;
 5281:   vertical-align: top;
 5282:   width: 184px;
 5283:   padding: 8px;
 5284: }
 5285: 
 5286: table.LC_pick_box td.LC_pick_box_value {
 5287:   text-align: left;
 5288:   padding: 8px;
 5289: }
 5290: 
 5291: table.LC_pick_box td.LC_pick_box_select {
 5292:   text-align: left;
 5293:   padding: 8px;
 5294: }
 5295: 
 5296: table.LC_pick_box td.LC_pick_box_separator {
 5297:   padding: 0;
 5298:   height: 1px;
 5299:   background: black;
 5300: }
 5301: 
 5302: table.LC_pick_box td.LC_pick_box_submit {
 5303:   text-align: right;
 5304: }
 5305: 
 5306: table.LC_pick_box td.LC_evenrow_value {
 5307:   text-align: left;
 5308:   padding: 8px;
 5309:   background-color: $data_table_light;
 5310: }
 5311: 
 5312: table.LC_pick_box td.LC_oddrow_value {
 5313:   text-align: left;
 5314:   padding: 8px;
 5315:   background-color: $data_table_light;
 5316: }
 5317: 
 5318: table.LC_helpform_receipt {
 5319:   width: 620px;
 5320:   border-collapse: separate;
 5321:   background: white;
 5322:   border: 1px solid black;
 5323:   border-spacing: 1px;
 5324: }
 5325: 
 5326: table.LC_helpform_receipt td.LC_pick_box_title {
 5327:   background: $tabbg;
 5328:   font-weight: bold;
 5329:   text-align: right;
 5330:   width: 184px;
 5331:   padding: 8px;
 5332: }
 5333: 
 5334: table.LC_helpform_receipt td.LC_evenrow_value {
 5335:   text-align: left;
 5336:   padding: 8px;
 5337:   background-color: $data_table_light;
 5338: }
 5339: 
 5340: table.LC_helpform_receipt td.LC_oddrow_value {
 5341:   text-align: left;
 5342:   padding: 8px;
 5343:   background-color: $data_table_light;
 5344: }
 5345: 
 5346: table.LC_helpform_receipt td.LC_pick_box_separator {
 5347:   padding: 0;
 5348:   height: 1px;
 5349:   background: black;
 5350: }
 5351: 
 5352: span.LC_helpform_receipt_cat {
 5353:   font-weight: bold;
 5354: }
 5355: 
 5356: table.LC_group_priv_box {
 5357:   background: white;
 5358:   border: 1px solid black;
 5359:   border-spacing: 1px;
 5360: }
 5361: 
 5362: table.LC_group_priv_box td.LC_pick_box_title {
 5363:   background: $tabbg;
 5364:   font-weight: bold;
 5365:   text-align: right;
 5366:   width: 184px;
 5367: }
 5368: 
 5369: table.LC_group_priv_box td.LC_groups_fixed {
 5370:   background: $data_table_light;
 5371:   text-align: center;
 5372: }
 5373: 
 5374: table.LC_group_priv_box td.LC_groups_optional {
 5375:   background: $data_table_dark;
 5376:   text-align: center;
 5377: }
 5378: 
 5379: table.LC_group_priv_box td.LC_groups_functionality {
 5380:   background: $data_table_darker;
 5381:   text-align: center;
 5382:   font-weight: bold;
 5383: }
 5384: 
 5385: table.LC_group_priv td {
 5386:   text-align: left;
 5387:   padding: 0;
 5388: }
 5389: 
 5390: table.LC_notify_front_page {
 5391:   background: white;
 5392:   border: 1px solid black;
 5393:   padding: 8px;
 5394: }
 5395: 
 5396: table.LC_notify_front_page td {
 5397:   padding: 8px;
 5398: }
 5399: 
 5400: .LC_navbuttons {
 5401:   margin: 2ex 0ex 2ex 0ex;
 5402: }
 5403: 
 5404: .LC_topic_bar {
 5405:   font-weight: bold;
 5406:   width: 100%;
 5407:   background: $tabbg;
 5408:   vertical-align: middle;
 5409:   margin: 2ex 0ex 2ex 0ex;
 5410:   padding: 3px;
 5411: }
 5412: 
 5413: .LC_topic_bar span {
 5414:   vertical-align: middle;
 5415: }
 5416: 
 5417: .LC_topic_bar img {
 5418:   vertical-align: bottom;
 5419: }
 5420: 
 5421: table.LC_course_group_status {
 5422:   margin: 20px;
 5423: }
 5424: 
 5425: table.LC_status_selector td {
 5426:   vertical-align: top;
 5427:   text-align: center;
 5428:   padding: 4px;
 5429: }
 5430: 
 5431: div.LC_feedback_link {
 5432:   clear: both;
 5433:   background: $sidebg;
 5434:   width: 100%;
 5435:   padding-bottom: 10px;
 5436:   border: 1px $tabbg solid;
 5437:   height: 22px;
 5438:   line-height: 22px;
 5439:   padding-top: 5px;
 5440: }
 5441: 
 5442: div.LC_feedback_link img {
 5443:   height: 22px;
 5444:   vertical-align:middle;
 5445: }
 5446: 
 5447: div.LC_feedback_link a{
 5448:   text-decoration: none;
 5449: }
 5450: 
 5451: div.LC_comblock {
 5452:   display:inline; 
 5453:   color:$font;
 5454:   font-size:90%;
 5455: }
 5456: 
 5457: div.LC_feedback_link div.LC_comblock {
 5458:   padding-left:5px;
 5459: }
 5460: 
 5461: div.LC_feedback_link div.LC_comblock a {
 5462:   color:$font;
 5463: }
 5464: 
 5465: span.LC_feedback_link {
 5466:   /* background: $feedback_link_bg; */
 5467:   font-size: larger;
 5468: }
 5469: 
 5470: span.LC_message_link {
 5471:   /* background: $feedback_link_bg; */
 5472:   font-size: larger;
 5473:   position: absolute;
 5474:   right: 1em;
 5475: }
 5476: 
 5477: table.LC_prior_tries {
 5478:   border: 1px solid #000000;
 5479:   border-collapse: separate;
 5480:   border-spacing: 1px;
 5481: }
 5482: 
 5483: table.LC_prior_tries td {
 5484:   padding: 2px;
 5485: }
 5486: 
 5487: .LC_answer_correct {
 5488:   background: lightgreen;
 5489:   color: darkgreen;
 5490:   padding: 6px;
 5491: }
 5492: 
 5493: .LC_answer_charged_try {
 5494:   background: #FFAAAA;
 5495:   color: darkred;
 5496:   padding: 6px;
 5497: }
 5498: 
 5499: .LC_answer_not_charged_try,
 5500: .LC_answer_no_grade,
 5501: .LC_answer_late {
 5502:   background: lightyellow;
 5503:   color: black;
 5504:   padding: 6px;
 5505: }
 5506: 
 5507: .LC_answer_previous {
 5508:   background: lightblue;
 5509:   color: darkblue;
 5510:   padding: 6px;
 5511: }
 5512: 
 5513: .LC_answer_no_message {
 5514:   background: #FFFFFF;
 5515:   color: black;
 5516:   padding: 6px;
 5517: }
 5518: 
 5519: .LC_answer_unknown {
 5520:   background: orange;
 5521:   color: black;
 5522:   padding: 6px;
 5523: }
 5524: 
 5525: span.LC_prior_numerical,
 5526: span.LC_prior_string,
 5527: span.LC_prior_custom,
 5528: span.LC_prior_reaction,
 5529: span.LC_prior_math {
 5530:   font-family: monospace;
 5531:   white-space: pre;
 5532: }
 5533: 
 5534: span.LC_prior_string {
 5535:   font-family: monospace;
 5536:   white-space: pre;
 5537: }
 5538: 
 5539: table.LC_prior_option {
 5540:   width: 100%;
 5541:   border-collapse: collapse;
 5542: }
 5543: 
 5544: table.LC_prior_rank, 
 5545: table.LC_prior_match {
 5546:   border-collapse: collapse;
 5547: }
 5548: 
 5549: table.LC_prior_option tr td,
 5550: table.LC_prior_rank tr td,
 5551: table.LC_prior_match tr td {
 5552:   border: 1px solid #000000;
 5553: }
 5554: 
 5555: .LC_nobreak {
 5556:   white-space: nowrap;
 5557: }
 5558: 
 5559: span.LC_cusr_emph {
 5560:   font-style: italic;
 5561: }
 5562: 
 5563: span.LC_cusr_subheading {
 5564:   font-weight: normal;
 5565:   font-size: 85%;
 5566: }
 5567: 
 5568: table.LC_docs_documents {
 5569:   background: #BBBBBB;
 5570:   border-width: 0;
 5571:   border-collapse: collapse;
 5572: }
 5573: 
 5574: table.LC_docs_documents td.LC_docs_document {
 5575:   border: 2px solid black;
 5576:   padding: 4px;
 5577: }
 5578: 
 5579: div.LC_docs_entry_move {
 5580:   border: 1px solid #BBBBBB;
 5581:   background: #DDDDDD;
 5582:   width: 22px;
 5583:   padding: 1px;
 5584:   margin: 0;
 5585: }
 5586: 
 5587: table.LC_data_table tr > td.LC_docs_entry_commands,
 5588: table.LC_data_table tr > td.LC_docs_entry_parameter {
 5589:   background: #DDDDDD;
 5590:   font-size: x-small;
 5591: }
 5592: 
 5593: .LC_docs_entry_parameter {
 5594:   white-space: nowrap;
 5595: }
 5596: 
 5597: .LC_docs_copy {
 5598:   color: #000099;
 5599: }
 5600: 
 5601: .LC_docs_cut {
 5602:   color: #550044;
 5603: }
 5604: 
 5605: .LC_docs_rename {
 5606:   color: #009900;
 5607: }
 5608: 
 5609: .LC_docs_remove {
 5610:   color: #990000;
 5611: }
 5612: 
 5613: .LC_docs_reinit_warn,
 5614: .LC_docs_ext_edit {
 5615:   font-size: x-small;
 5616: }
 5617: 
 5618: table.LC_docs_adddocs td,
 5619: table.LC_docs_adddocs th {
 5620:   border: 1px solid #BBBBBB;
 5621:   padding: 4px;
 5622:   background: #DDDDDD;
 5623: }
 5624: 
 5625: table.LC_sty_begin {
 5626:   background: #BBFFBB;
 5627: }
 5628: 
 5629: table.LC_sty_end {
 5630:   background: #FFBBBB;
 5631: }
 5632: 
 5633: table.LC_double_column {
 5634:   border-width: 0;
 5635:   border-collapse: collapse;
 5636:   width: 100%;
 5637:   padding: 2px;
 5638: }
 5639: 
 5640: table.LC_double_column tr td.LC_left_col {
 5641:   top: 2px;
 5642:   left: 2px;
 5643:   width: 47%;
 5644:   vertical-align: top;
 5645: }
 5646: 
 5647: table.LC_double_column tr td.LC_right_col {
 5648:   top: 2px;
 5649:   right: 2px;
 5650:   width: 47%;
 5651:   vertical-align: top;
 5652: }
 5653: 
 5654: div.LC_left_float {
 5655:   float: left;
 5656:   padding-right: 5%;
 5657:   padding-bottom: 4px;
 5658: }
 5659: 
 5660: div.LC_clear_float_header {
 5661:   padding-bottom: 2px;
 5662: }
 5663: 
 5664: div.LC_clear_float_footer {
 5665:   padding-top: 10px;
 5666:   clear: both;
 5667: }
 5668: 
 5669: div.LC_grade_show_user {
 5670:   margin-top: 20px;
 5671:   border: 1px solid black;
 5672: }
 5673: 
 5674: div.LC_grade_user_name {
 5675:   background: #DDDDEE;
 5676:   border-bottom: 1px solid black;
 5677:   font-weight: bold;
 5678:   font-size: large;
 5679: }
 5680: 
 5681: div.LC_grade_show_user_odd_row div.LC_grade_user_name {
 5682:   background: #DDEEDD;
 5683: }
 5684: 
 5685: div.LC_grade_show_problem,
 5686: div.LC_grade_submissions,
 5687: div.LC_grade_message_center,
 5688: div.LC_grade_info_links,
 5689: div.LC_grade_assign {
 5690:   margin: 5px;
 5691:   width: 99%;
 5692:   background: #FFFFFF;
 5693: }
 5694: 
 5695: div.LC_grade_show_problem_header,
 5696: div.LC_grade_submissions_header,
 5697: div.LC_grade_message_center_header,
 5698: div.LC_grade_assign_header {
 5699:   font-weight: bold;
 5700:   font-size: large;
 5701: }
 5702: 
 5703: div.LC_grade_show_problem_problem,
 5704: div.LC_grade_submissions_body,
 5705: div.LC_grade_message_center_body,
 5706: div.LC_grade_assign_body {
 5707:   border: 1px solid black;
 5708:   width: 99%;
 5709:   background: #FFFFFF;
 5710: }
 5711: 
 5712: span.LC_grade_check_note {
 5713:   font-weight: normal;
 5714:   font-size: medium;
 5715:   display: inline;
 5716:   position: absolute;
 5717:   right: 1em;
 5718: }
 5719: 
 5720: table.LC_scantron_action {
 5721:   width: 100%;
 5722: }
 5723: 
 5724: table.LC_scantron_action tr th {
 5725:   font-weight:bold;
 5726:   font-style:normal;
 5727: }
 5728: 
 5729: .LC_edit_problem_header,
 5730: div.LC_edit_problem_footer {
 5731:   font-weight: normal;
 5732:   font-size:  medium;
 5733:   margin: 2px;
 5734: }
 5735: 
 5736: div.LC_edit_problem_header,
 5737: div.LC_edit_problem_header div,
 5738: div.LC_edit_problem_footer,
 5739: div.LC_edit_problem_footer div,
 5740: div.LC_edit_problem_editxml_header,
 5741: div.LC_edit_problem_editxml_header div {
 5742:   margin-top: 5px;
 5743: }
 5744: 
 5745: div.LC_edit_problem_header_title {
 5746:   font-weight: bold;
 5747:   font-size: larger;
 5748:   background: $tabbg;
 5749:   padding: 3px;
 5750: }
 5751: 
 5752: table.LC_edit_problem_header_title {
 5753:   font-size: larger;
 5754:   font-weight:  bold;
 5755:   width: 100%;
 5756:   border-color: $pgbg;
 5757:   border-style: solid;
 5758:   border-width: $border;
 5759:   background: $tabbg;
 5760:   border-collapse: collapse;
 5761:   padding: 0;
 5762: }
 5763: 
 5764: div.LC_edit_problem_discards {
 5765:   float: left;
 5766:   padding-bottom: 5px;
 5767: }
 5768: 
 5769: div.LC_edit_problem_saves {
 5770:   float: right;
 5771:   padding-bottom: 5px;
 5772: }
 5773: 
 5774: img.stift{
 5775:   border-width: 0;
 5776:   vertical-align: middle;
 5777: }
 5778: 
 5779: table#LC_mainmenu{
 5780:  margin-top:10px;
 5781:  width:80%;
 5782: }
 5783: 
 5784: table#LC_mainmenu td.LC_mainmenu_col_fieldset{
 5785:   vertical-align: top;
 5786:   width: 45%;
 5787: }
 5788: 
 5789: .LC_mainmenu_fieldset_category {
 5790:   color: $font;
 5791:   background: $pgbg;
 5792:   font-size: small;
 5793:   font-weight: bold;
 5794: }
 5795: 
 5796: div.LC_createcourse {
 5797:     margin: 10px 10px 10px 10px;
 5798: }
 5799: 
 5800: /* ---- Remove when done ----
 5801: # The following styles is part of the redesign of LON-CAPA and are
 5802: # subject to change during this project.
 5803: # Don't rely on their current functionality as they might be 
 5804: # changed or removed.
 5805: # --------------------------*/
 5806: 
 5807: a:hover,
 5808: ol.LC_smallMenu a:hover,
 5809: ol#LC_MenuBreadcrumbs a:hover,
 5810: ol#LC_PathBreadcrumbs a:hover,
 5811: ul#LC_TabMainMenuContent a:hover,
 5812: .LC_FormSectionClearButton input:hover
 5813: ul.LC_TabContent   li:hover a {
 5814: 	color:#BF2317;
 5815:         text-decoration:none;
 5816: }
 5817: 
 5818: h1 {
 5819: 	padding: 0;
 5820: 	line-height:130%;
 5821: }
 5822: 
 5823: h2,h3,h4,h5,h6 {
 5824: 	margin: 5px 0 5px 0;
 5825: 	padding: 0;
 5826: 	line-height:130%;
 5827: }
 5828: 
 5829: .LC_hcell {
 5830:         padding:3px 15px 3px 15px;
 5831:         margin: 0;
 5832: 	background-color:$tabbg;
 5833: 	color:$fontmenu;
 5834: 	border-bottom:solid 1px $lg_border_color;
 5835: }
 5836: 
 5837: .LC_Box > .LC_hcell {
 5838:     margin: 0 -10px 10px -10px;
 5839: }
 5840: 
 5841: .LC_noBorder {
 5842:         border: 0;
 5843: }
 5844: 
 5845: .LC_Right {
 5846:         float: right;
 5847:         margin: 0;
 5848:         padding: 0;
 5849: }
 5850: 
 5851: .LC_FormSectionClearButton input {
 5852:         background-color:transparent;
 5853:         border: none;
 5854:         cursor:pointer;
 5855:         text-decoration:underline;
 5856: }
 5857: 
 5858: .LC_help_open_topic {
 5859:         color: #FFFFFF;
 5860:         background-color: #EEEEFF;
 5861:         margin: 1px;
 5862:         padding: 4px;
 5863:         border: 1px solid #000033;
 5864:         white-space: nowrap;
 5865: /*		vertical-align: middle; */
 5866: }
 5867: 
 5868: dl,ul,div,fieldset {
 5869: 	margin: 10px 10px 10px 0;
 5870: /*	overflow: hidden; */
 5871: }
 5872: 
 5873: fieldset > legend {
 5874:     font-weight: bold;
 5875:     padding: 0 5px 0 5px;
 5876: }
 5877: 
 5878: #LC_nav_bar {
 5879:     float: left;
 5880:     margin: 0.2em 0 0 0;
 5881: }
 5882: 
 5883: #LC_nav_bar em{
 5884:     font-weight: bold;
 5885:     font-style: normal;
 5886: }
 5887: 
 5888: ol.LC_smallMenu {
 5889:     float: right;
 5890:     margin: 0.2em 0 0 0;
 5891: }
 5892: 
 5893: ol#LC_PathBreadcrumbs {
 5894: 	margin: 0;
 5895: }
 5896: 
 5897: ol.LC_smallMenu li {
 5898: 	display: inline;
 5899: 	padding: 5px 5px 0 10px;
 5900: 	vertical-align: top;
 5901: }
 5902: 
 5903: ol.LC_smallMenu li img {
 5904: 	vertical-align: bottom;
 5905: }
 5906: 
 5907: ol.LC_smallMenu a {
 5908: 	font-size: 90%;
 5909: 	color: RGB(80, 80, 80);
 5910: 	text-decoration: none;
 5911: }
 5912: 
 5913: ul#LC_TabMainMenuContent {
 5914:     clear: both;
 5915:     color: $fontmenu;
 5916:     background: $tabbg;
 5917:     list-style: none;
 5918:     padding: 0;
 5919:     margin: 0;
 5920:     width: 100%;
 5921: }
 5922: 
 5923: ul#LC_TabMainMenuContent li {
 5924:     font-weight: bold;
 5925:     line-height: 1.8em;
 5926:     padding: 0 0.8em; 
 5927:     border-right: 1px solid black;
 5928:     display: inline;
 5929:     vertical-align: middle;
 5930: }
 5931: 
 5932: ul.LC_TabContent {
 5933: 	display:block;
 5934: 	background: $sidebg;
 5935: 	border-bottom: solid 1px $lg_border_color;
 5936: 	list-style:none;
 5937: 	margin: 0 -10px;
 5938: 	padding: 0;
 5939: }
 5940: 
 5941: ul.LC_TabContent li,
 5942: ul.LC_TabContentBigger li {
 5943: 	float:left;
 5944: }
 5945: 
 5946: ul#LC_TabMainMenuContent li a {
 5947:     color: $fontmenu;
 5948: 	text-decoration: none;
 5949: }
 5950: 
 5951: ul.LC_TabContent {
 5952: 	min-height:1.5em;
 5953: }
 5954: 
 5955: ul.LC_TabContent li {
 5956: 	vertical-align:middle;
 5957: 	padding: 0 10px 0 10px;
 5958: 	background-color:$tabbg;
 5959: 	border-bottom:solid 1px $lg_border_color;
 5960: }
 5961: 
 5962: ul.LC_TabContent .right {
 5963: 	float:right;
 5964: }
 5965: 
 5966: ul.LC_TabContent li a, ul.LC_TabContent li {
 5967: 	color:rgb(47,47,47);
 5968: 	text-decoration:none;
 5969: 	font-size:95%;
 5970: 	font-weight:bold;
 5971: 	padding-right: 16px;
 5972: }
 5973: 
 5974: ul.LC_TabContent li:hover, ul.LC_TabContent li.active {
 5975:         background:#FFFFFF url(/adm/lonIcons/open.gif) no-repeat scroll right center;
 5976: 	border-bottom:solid 2px #FFFFFF;
 5977: 	padding-right: 16px;
 5978: }
 5979: 
 5980: #maincoursedoc {
 5981: 	clear:both;
 5982: }
 5983: 
 5984: ul.LC_TabContentBigger {
 5985:         display:block;
 5986:         list-style:none;
 5987:         padding: 0;
 5988: }
 5989: 
 5990: ul.LC_TabContentBigger li {
 5991:         vertical-align:bottom;
 5992:         height: 30px;
 5993:         font-size:110%;
 5994:         font-weight:bold;
 5995:         color: #737373;
 5996: }
 5997: 
 5998: 
 5999: ul.LC_TabContentBigger li a {
 6000:         background:url('/adm/lonIcons/tabbgleft.gif') left bottom no-repeat;
 6001: 	height: 30px;
 6002: 	line-height: 30px;
 6003: 	text-align: center;
 6004: 	display: block;
 6005: 	text-decoration: none;
 6006: }
 6007: 
 6008: ul.LC_TabContentBigger li:hover a, 
 6009: ul.LC_TabContentBigger li.active a {
 6010: 	background:url('/adm/lonIcons/tabbgleft.gif') left top no-repeat;
 6011: 	color:$font;
 6012: 	text-decoration: underline;
 6013: }
 6014: 
 6015: 
 6016: ul.LC_TabContentBigger li b {
 6017: 	background: url('/adm/lonIcons/tabbgright.gif') no-repeat right bottom;
 6018: 	display: block;
 6019: 	float: left;
 6020: 	padding: 0 30px;
 6021: }
 6022: 
 6023: ul.LC_TabContentBigger li:hover b,
 6024: ul.LC_TabContentBigger li.active b {
 6025:         background:url('/adm/lonIcons/tabbgright.gif') right top no-repeat;
 6026:         color:$font;
 6027: 	border-bottom: 1px solid #FFFFFF;
 6028: }
 6029: 
 6030: 
 6031: ul.LC_CourseBreadcrumbs {
 6032:   background: $sidebg;
 6033:   line-height: 32px;
 6034:   padding-left: 10px;
 6035:   margin: 0 0 10px 0;
 6036:   list-style-position: inside;
 6037: 
 6038: }
 6039: 
 6040: ol#LC_MenuBreadcrumbs, 
 6041: ol#LC_PathBreadcrumbs {
 6042: 	padding-left: 10px;
 6043: 	margin: 0;
 6044: 	list-style-position: inside;
 6045: }
 6046: 
 6047: ol#LC_MenuBreadcrumbs li, 
 6048: ol#LC_PathBreadcrumbs li, 
 6049: ul.LC_CourseBreadcrumbs li {
 6050:     display: inline;
 6051:     white-space: nowrap;
 6052: }
 6053: 
 6054: ol#LC_MenuBreadcrumbs li a,
 6055: ul.LC_CourseBreadcrumbs li a {
 6056: 	text-decoration: none;
 6057: 	font-size:90%;
 6058: }
 6059: 
 6060: ol#LC_PathBreadcrumbs li a {
 6061: 	text-decoration:none;
 6062: 	font-size:100%;
 6063: 	font-weight:bold;
 6064: }
 6065: 
 6066: .LC_Box {
 6067:     border: solid 1px $lg_border_color;
 6068:     padding: 0 10px 10px 10px;
 6069: }
 6070: 
 6071: .LC_AboutMe_Image {
 6072: 	float:left;
 6073: 	margin-right:10px;
 6074: }
 6075: 
 6076: .LC_Clear_AboutMe_Image {
 6077: 	clear:left;
 6078: }
 6079: 
 6080: dl.LC_ListStyleClean dt {
 6081: 	padding-right: 5px;
 6082: 	display: table-header-group;
 6083: }
 6084: 
 6085: dl.LC_ListStyleClean dd {
 6086: 	display: table-row;
 6087: }
 6088: 
 6089: .LC_ListStyleClean,
 6090: .LC_ListStyleSimple,
 6091: .LC_ListStyleNormal,
 6092: .LC_ListStyle_Border,
 6093: .LC_ListStyleSpecial {
 6094: 	/*display:block;	*/
 6095: 	list-style-position: inside;
 6096: 	list-style-type: none;
 6097: 	overflow: hidden;
 6098: 	padding: 0;
 6099: }
 6100: 
 6101: .LC_ListStyleSimple li,
 6102: .LC_ListStyleSimple dd,
 6103: .LC_ListStyleNormal li,
 6104: .LC_ListStyleNormal dd,
 6105: .LC_ListStyleSpecial li,
 6106: .LC_ListStyleSpecial dd {
 6107: 	margin: 0;
 6108: 	padding: 5px 5px 5px 10px;
 6109: 	clear: both;
 6110: }
 6111: 
 6112: .LC_ListStyleClean li,
 6113: .LC_ListStyleClean dd {
 6114: 	padding-top: 0;
 6115: 	padding-bottom: 0;
 6116: }
 6117: 
 6118: .LC_ListStyleSimple dd,
 6119: .LC_ListStyleSimple li {
 6120: 	border-bottom: solid 1px $lg_border_color;
 6121: }
 6122: 
 6123: .LC_ListStyleSpecial li,
 6124: .LC_ListStyleSpecial dd {
 6125: 	list-style-type: none;
 6126: 	background-color: RGB(220, 220, 220);
 6127: 	margin-bottom: 4px;
 6128: }
 6129: 
 6130: table.LC_SimpleTable {
 6131: 	margin:5px;
 6132: 	border:solid 1px $lg_border_color;
 6133: }
 6134: 
 6135: table.LC_SimpleTable tr {
 6136: 	padding: 0;
 6137: 	border:solid 1px $lg_border_color;
 6138: }
 6139: 
 6140: table.LC_SimpleTable thead {
 6141: 	 background:rgb(220,220,220);
 6142: }
 6143: 
 6144: div.LC_columnSection {
 6145: 	display: block;
 6146: 	clear: both;
 6147: 	overflow: hidden;
 6148: 	margin: 0;
 6149: }
 6150: 
 6151: div.LC_columnSection>* {
 6152: 	float: left;
 6153: 	margin: 10px 20px 10px 0;
 6154: 	overflow:hidden;
 6155: }
 6156: 
 6157: .LC_loginpage_container {
 6158: 	text-align:left;
 6159: 	margin : 0 auto;
 6160: 	width:90%;
 6161: 	padding: 10px;
 6162: 	height: auto;
 6163: 	background-color:#FFFFFF;
 6164: 	border:1px solid #CCCCCC;
 6165: }
 6166: 
 6167: 
 6168: .LC_loginpage_loginContainer {
 6169: 	float:left;
 6170: 	width: 182px;
 6171: 	padding: 2px;
 6172: 	border:1px solid #CCCCCC;
 6173: 	background-color:$loginbg;
 6174: }
 6175: 
 6176: .LC_loginpage_loginContainer h2 {
 6177: 	margin-top: 0;
 6178: 	display:block;
 6179: 	background:$bgcol;
 6180: 	color:$textcol;
 6181: 	padding-left:5px;
 6182: }
 6183: 
 6184: .LC_loginpage_loginInfo {
 6185: 	float:left;
 6186: 	width:182px;
 6187: 	border:1px solid #CCCCCC;
 6188: 	padding:2px;
 6189: }
 6190: 
 6191: .LC_loginpage_space {
 6192: 	clear: both;
 6193: 	margin-bottom: 20px;
 6194: 	border-bottom: 1px solid #CCCCCC;
 6195: }
 6196: 
 6197: .LC_loginpage_floatLeft {
 6198: 	float: left;
 6199: 	width: 200px;
 6200: 	margin: 0;
 6201: }
 6202: 
 6203: table em {
 6204: 	font-weight: bold;
 6205: 	font-style: normal;
 6206: }
 6207: 
 6208: table.LC_tableBrowseRes,
 6209: table.LC_tableOfContent {
 6210:         border:none;
 6211: 	border-spacing: 1px;
 6212: 	padding: 3px;
 6213: 	background-color: #FFFFFF;
 6214: 	font-size: 90%;
 6215: }
 6216: 
 6217: table.LC_tableOfContent{
 6218:     border-collapse: collapse;
 6219: }
 6220: 
 6221: table.LC_tableBrowseRes a,
 6222: table.LC_tableOfContent a {
 6223:         background-color: transparent;
 6224: 	text-decoration: none;
 6225: }
 6226: 
 6227: table.LC_tableBrowseRes tr.LC_trOdd,
 6228: table.LC_tableOfContent tr.LC_trOdd{
 6229: 	background-color: #EEEEEE;
 6230: }
 6231: 
 6232: table.LC_tableOfContent img {
 6233: 	border: none;
 6234: 	height: 1.3em;
 6235: 	vertical-align: text-bottom;
 6236: 	margin-right: 0.3em;
 6237: }
 6238: 
 6239: a#LC_content_toolbar_firsthomework {
 6240: 	background-image:url(/res/adm/pages/open-first-problem.gif);
 6241: }
 6242: 
 6243: a#LC_content_toolbar_launchnav {
 6244: 	background-image:url(/res/adm/pages/start-navigation.gif);
 6245: }
 6246: 
 6247: a#LC_content_toolbar_closenav {
 6248: 	background-image:url(/res/adm/pages/close-navigation.gif);
 6249: }
 6250: 
 6251: a#LC_content_toolbar_everything {
 6252: 	background-image:url(/res/adm/pages/show-all.gif);
 6253: }
 6254: 
 6255: a#LC_content_toolbar_uncompleted {
 6256: 	background-image:url(/res/adm/pages/show-incomplete-problems.gif);
 6257: }
 6258: 
 6259: #LC_content_toolbar_clearbubbles {
 6260: 	background-image:url(/res/adm/pages/mark-discussionentries-read.gif);
 6261: }
 6262: 
 6263: a#LC_content_toolbar_changefolder {
 6264: 	background : url(/res/adm/pages/close-all-folders.gif) top center ;
 6265: }
 6266: 
 6267: a#LC_content_toolbar_changefolder_toggled {
 6268: 	background-image:url(/res/adm/pages/open-all-folders.gif);
 6269: }
 6270: 
 6271: ul#LC_toolbar li a:hover {
 6272: 	background-position: bottom center;
 6273: }
 6274: 
 6275: ul#LC_toolbar {
 6276: 	padding: 0;
 6277: 	margin: 2px;
 6278: 	list-style:none;
 6279: 	position:relative;
 6280: 	background-color:white;
 6281: }
 6282: 
 6283: ul#LC_toolbar li {
 6284: 	border:1px solid white;
 6285: 	padding: 0;
 6286: 	margin: 0;
 6287:         float: left;
 6288: 	display:inline;
 6289: 	vertical-align:middle;
 6290: } 
 6291: 
 6292: 
 6293: a.LC_toolbarItem {
 6294: 	display:block;
 6295: 	padding: 0;
 6296: 	margin: 0;
 6297: 	height: 32px;
 6298: 	width: 32px;
 6299: 	color:white;
 6300: 	border: none;
 6301: 	background-repeat:no-repeat;
 6302: 	background-color:transparent;
 6303: }
 6304: 
 6305: ul.LC_funclist li {
 6306:   float: left;
 6307:   white-space: nowrap;
 6308:   height: 35px; /* at least as high as heighest list item */
 6309:   margin: 0 15px 15px 10px;
 6310: }
 6311: 
 6312: 
 6313: END
 6314: }
 6315: 
 6316: =pod
 6317: 
 6318: =item * &headtag()
 6319: 
 6320: Returns a uniform footer for LON-CAPA web pages.
 6321: 
 6322: Inputs: $title - optional title for the head
 6323:         $head_extra - optional extra HTML to put inside the <head>
 6324:         $args - optional arguments
 6325:             force_register - if is true call registerurl so the remote is 
 6326:                              informed
 6327:             redirect       -> array ref of
 6328:                                    1- seconds before redirect occurs
 6329:                                    2- url to redirect to
 6330:                                    3- whether the side effect should occur
 6331:                            (side effect of setting 
 6332:                                $env{'internal.head.redirect'} to the url 
 6333:                                redirected too)
 6334:             domain         -> force to color decorate a page for a specific
 6335:                                domain
 6336:             function       -> force usage of a specific rolish color scheme
 6337:             bgcolor        -> override the default page bgcolor
 6338:             no_auto_mt_title
 6339:                            -> prevent &mt()ing the title arg
 6340: 
 6341: =cut
 6342: 
 6343: sub headtag {
 6344:     my ($title,$head_extra,$args) = @_;
 6345:     
 6346:     my $function = $args->{'function'} || &get_users_function();
 6347:     my $domain   = $args->{'domain'}   || &determinedomain();
 6348:     my $bgcolor  = $args->{'bgcolor'}  || &designparm($function.'.pgbg',$domain);
 6349:     my $url = join(':',$env{'user.name'},$env{'user.domain'},
 6350: 		   $Apache::lonnet::perlvar{'lonVersion'},
 6351: 		   #time(),
 6352: 		   $env{'environment.color.timestamp'},
 6353: 		   $function,$domain,$bgcolor);
 6354: 
 6355:     $url = '/adm/css/'.&escape($url).'.css';
 6356: 
 6357:     my $result =
 6358: 	'<head>'.
 6359: 	&font_settings();
 6360: 
 6361:     if (!$args->{'frameset'}) {
 6362: 	$result .= &Apache::lonhtmlcommon::htmlareaheaders();
 6363:     }
 6364:     if ($args->{'force_register'}) {
 6365: 	$result .= &Apache::lonmenu::registerurl(1);
 6366:     }
 6367:     if (!$args->{'no_nav_bar'} 
 6368: 	&& !$args->{'only_body'}
 6369: 	&& !$args->{'frameset'}) {
 6370: 	$result .= &help_menu_js();
 6371:     }
 6372: 
 6373:     if (ref($args->{'redirect'})) {
 6374: 	my ($time,$url,$inhibit_continue) = @{$args->{'redirect'}};
 6375: 	$url = &Apache::lonenc::check_encrypt($url);
 6376: 	if (!$inhibit_continue) {
 6377: 	    $env{'internal.head.redirect'} = $url;
 6378: 	}
 6379: 	$result.=<<ADDMETA
 6380: <meta http-equiv="pragma" content="no-cache" />
 6381: <meta http-equiv="Refresh" content="$time; url=$url" />
 6382: ADDMETA
 6383:     }
 6384:     if (!defined($title)) {
 6385: 	$title = 'The LearningOnline Network with CAPA';
 6386:     }
 6387:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
 6388:     $result .= '<title> LON-CAPA '.$title.'</title>'
 6389: 	.'<link rel="stylesheet" type="text/css" href="'.$url.'" />'
 6390: 	.$head_extra;
 6391:     return $result;
 6392: }
 6393: 
 6394: =pod
 6395: 
 6396: =item * &font_settings()
 6397: 
 6398: Returns neccessary <meta> to set the proper encoding
 6399: 
 6400: Inputs: none
 6401: 
 6402: =cut
 6403: 
 6404: sub font_settings {
 6405:     my $headerstring='';
 6406:     if (!$env{'browser.mathml'} && $env{'browser.unicode'}) {
 6407: 	$headerstring.=
 6408: 	    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
 6409:     }
 6410:     return $headerstring;
 6411: }
 6412: 
 6413: =pod
 6414: 
 6415: =item * &xml_begin()
 6416: 
 6417: Returns the needed doctype and <html>
 6418: 
 6419: Inputs: none
 6420: 
 6421: =cut
 6422: 
 6423: sub xml_begin {
 6424:     my $output='';
 6425: 
 6426:     if ($env{'internal.start_page'}==1) {
 6427: 	&Apache::lonhtmlcommon::init_htmlareafields();
 6428:     }
 6429: 
 6430:     if ($env{'browser.mathml'}) {
 6431: 	$output='<?xml version="1.0"?>'
 6432:             #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
 6433: #            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
 6434:             
 6435: #	    .'<!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">] >'
 6436: 	    .'<!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">'
 6437:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
 6438: 	    .'xmlns="http://www.w3.org/1999/xhtml">';
 6439:     } else {
 6440: 	$output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
 6441:            .'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
 6442:     }
 6443:     return $output;
 6444: }
 6445: 
 6446: =pod
 6447: 
 6448: =item * &endheadtag()
 6449: 
 6450: Returns a uniform </head> for LON-CAPA web pages.
 6451: 
 6452: Inputs: none
 6453: 
 6454: =cut
 6455: 
 6456: sub endheadtag {
 6457:     return '</head>';
 6458: }
 6459: 
 6460: =pod
 6461: 
 6462: =item * &head()
 6463: 
 6464: Returns a uniform complete <head>..</head> section for LON-CAPA web pages.
 6465: 
 6466: Inputs:
 6467: 
 6468: =over 4
 6469: 
 6470: $title - optional title for the page
 6471: 
 6472: $head_extra - optional extra HTML to put inside the <head>
 6473: 
 6474: =back
 6475: 
 6476: =cut
 6477: 
 6478: sub head {
 6479:     my ($title,$head_extra,$args) = @_;
 6480:     return &headtag($title,$head_extra,$args).&endheadtag();
 6481: }
 6482: 
 6483: =pod
 6484: 
 6485: =item * &start_page()
 6486: 
 6487: Returns a complete <html> .. <body> section for LON-CAPA web pages.
 6488: 
 6489: Inputs:
 6490: 
 6491: =over 4
 6492: 
 6493: $title - optional title for the page
 6494: 
 6495: $head_extra - optional extra HTML to incude inside the <head>
 6496: 
 6497: $args - additional optional args supported are:
 6498: 
 6499: =over 8
 6500: 
 6501:              only_body      -> is true will set &bodytag() onlybodytag
 6502:                                     arg on
 6503:              no_nav_bar     -> is true will set &bodytag() no_nav_bar arg on
 6504:              add_entries    -> additional attributes to add to the  <body>
 6505:              domain         -> force to color decorate a page for a 
 6506:                                     specific domain
 6507:              function       -> force usage of a specific rolish color
 6508:                                     scheme
 6509:              redirect       -> see &headtag()
 6510:              bgcolor        -> override the default page bg color
 6511:              js_ready       -> return a string ready for being used in 
 6512:                                     a javascript writeln
 6513:              html_encode    -> return a string ready for being used in 
 6514:                                     a html attribute
 6515:              force_register -> if is true will turn on the &bodytag()
 6516:                                     $forcereg arg
 6517:              frameset       -> if true will start with a <frameset>
 6518:                                     rather than <body>
 6519:              skip_phases    -> hash ref of 
 6520:                                     head -> skip the <html><head> generation
 6521:                                     body -> skip all <body> generation
 6522:              no_inline_link -> if true and in remote mode, don't show the 
 6523:                                     'Switch To Inline Menu' link
 6524:              no_auto_mt_title -> prevent &mt()ing the title arg
 6525:              inherit_jsmath -> when creating popup window in a page,
 6526:                                     should it have jsmath forced on by the
 6527:                                     current page
 6528:              bread_crumbs ->             Array containing breadcrumbs
 6529:              bread_crumbs_components ->  if exists show it as headline else show only the breadcrumbs
 6530: 
 6531: =back
 6532: 
 6533: =back
 6534: 
 6535: =cut
 6536: 
 6537: sub start_page {
 6538:     my ($title,$head_extra,$args) = @_;
 6539:     #&Apache::lonnet::logthis("start_page ".join(':',caller(0)));
 6540:     my %head_args;
 6541:     foreach my $arg ('redirect','force_register','domain','function',
 6542: 		     'bgcolor','frameset','no_nav_bar','only_body',
 6543: 		     'no_auto_mt_title') {
 6544: 	if (defined($args->{$arg})) {
 6545: 	    $head_args{$arg} = $args->{$arg};
 6546: 	}
 6547:     }
 6548: 
 6549:     $env{'internal.start_page'}++;
 6550:     my $result;
 6551:     if (! exists($args->{'skip_phases'}{'head'}) ) {
 6552: 	$result.=
 6553: 	    &xml_begin().
 6554: 	    &headtag($title,$head_extra,\%head_args).&endheadtag();
 6555:     }
 6556:     
 6557:     if (! exists($args->{'skip_phases'}{'body'}) ) {
 6558: 	if ($args->{'frameset'}) {
 6559: 	    my $attr_string = &make_attr_string($args->{'force_register'},
 6560: 						$args->{'add_entries'});
 6561: 	    $result .= "\n<frameset $attr_string>\n";
 6562:         } else {
 6563:             $result .=
 6564:                 &bodytag($title, 
 6565:                          $args->{'function'},       $args->{'add_entries'},
 6566:                          $args->{'only_body'},      $args->{'domain'},
 6567:                          $args->{'force_register'}, $args->{'no_nav_bar'},
 6568:                          $args->{'bgcolor'},        $args->{'no_inline_link'},
 6569:                          $args);
 6570:         }
 6571:     }
 6572: 
 6573:     if ($args->{'js_ready'}) {
 6574: 		$result = &js_ready($result);
 6575:     }
 6576:     if ($args->{'html_encode'}) {
 6577: 		$result = &html_encode($result);
 6578:     }
 6579: 
 6580:     # Preparation for new and consistent functionlist at top of screen
 6581:     # if ($args->{'functionlist'}) {
 6582:     #            $result .= &build_functionlist();
 6583:     #}
 6584: 
 6585:     # Don't add anything more if only_body wanted
 6586:     return $result if $args->{'only_body'};
 6587: 
 6588:     #Breadcrumbs
 6589:     if (exists($args->{'bread_crumbs'}) or exists($args->{'bread_crumbs_component'})) {
 6590: 		&Apache::lonhtmlcommon::clear_breadcrumbs();
 6591: 		#if any br links exists, add them to the breadcrumbs
 6592: 		if (exists($args->{'bread_crumbs'}) and ref($args->{'bread_crumbs'}) eq 'ARRAY') {         
 6593: 			foreach my $crumb (@{$args->{'bread_crumbs'}}){
 6594: 				&Apache::lonhtmlcommon::add_breadcrumb($crumb);
 6595: 			}
 6596: 		}
 6597: 
 6598: 		#if bread_crumbs_component exists show it as headline else show only the breadcrumbs
 6599: 		if(exists($args->{'bread_crumbs_component'})){
 6600: 			$result .= &Apache::lonhtmlcommon::breadcrumbs($args->{'bread_crumbs_component'});
 6601: 		}else{
 6602: 			$result .= &Apache::lonhtmlcommon::breadcrumbs();
 6603: 		}
 6604:     }
 6605:     return $result;
 6606: }
 6607: 
 6608: 
 6609: =pod
 6610: 
 6611: =item * &head()
 6612: 
 6613: Returns a complete </body></html> section for LON-CAPA web pages.
 6614: 
 6615: Inputs:         $args - additional optional args supported are:
 6616:                  js_ready     -> return a string ready for being used in 
 6617:                                  a javascript writeln
 6618:                  html_encode  -> return a string ready for being used in 
 6619:                                  a html attribute
 6620:                  frameset     -> if true will start with a <frameset>
 6621:                                  rather than <body>
 6622:                  dicsussion   -> if true will get discussion from
 6623:                                   lonxml::xmlend
 6624:                                  (you can pass the target and parser arguments
 6625:                                   through optional 'target' and 'parser' args
 6626:                                   to this routine)
 6627: 
 6628: =cut
 6629: 
 6630: sub end_page {
 6631:     my ($args) = @_;
 6632:     $env{'internal.end_page'}++;
 6633:     my $result;
 6634:     if ($args->{'discussion'}) {
 6635: 	my ($target,$parser);
 6636: 	if (ref($args->{'discussion'})) {
 6637: 	    ($target,$parser) =($args->{'discussion'}{'target'},
 6638: 				$args->{'discussion'}{'parser'});
 6639: 	}
 6640: 	$result .= &Apache::lonxml::xmlend($target,$parser);
 6641:     }
 6642: 
 6643:     if ($args->{'frameset'}) {
 6644: 	$result .= '</frameset>';
 6645:     } else {
 6646: 	$result .= &endbodytag($args);
 6647:     }
 6648:     $result .= "\n</html>";
 6649: 
 6650:     if ($args->{'js_ready'}) {
 6651: 	$result = &js_ready($result);
 6652:     }
 6653: 
 6654:     if ($args->{'html_encode'}) {
 6655: 	$result = &html_encode($result);
 6656:     }
 6657: 
 6658:     return $result;
 6659: }
 6660: 
 6661: sub html_encode {
 6662:     my ($result) = @_;
 6663: 
 6664:     $result = &HTML::Entities::encode($result,'<>&"');
 6665:     
 6666:     return $result;
 6667: }
 6668: sub js_ready {
 6669:     my ($result) = @_;
 6670: 
 6671:     $result =~ s/[\n\r]/ /xmsg;
 6672:     $result =~ s/\\/\\\\/xmsg;
 6673:     $result =~ s/'/\\'/xmsg;
 6674:     $result =~ s{</}{<\\/}xmsg;
 6675:     
 6676:     return $result;
 6677: }
 6678: 
 6679: sub validate_page {
 6680:     if (  exists($env{'internal.start_page'})
 6681: 	  &&     $env{'internal.start_page'} > 1) {
 6682: 	&Apache::lonnet::logthis('start_page called multiple times '.
 6683: 				 $env{'internal.start_page'}.' '.
 6684: 				 $ENV{'request.filename'});
 6685:     }
 6686:     if (  exists($env{'internal.end_page'})
 6687: 	  &&     $env{'internal.end_page'} > 1) {
 6688: 	&Apache::lonnet::logthis('end_page called multiple times '.
 6689: 				 $env{'internal.end_page'}.' '.
 6690: 				 $env{'request.filename'});
 6691:     }
 6692:     if (     exists($env{'internal.start_page'})
 6693: 	&& ! exists($env{'internal.end_page'})) {
 6694: 	&Apache::lonnet::logthis('start_page called without end_page '.
 6695: 				 $env{'request.filename'});
 6696:     }
 6697:     if (   ! exists($env{'internal.start_page'})
 6698: 	&&   exists($env{'internal.end_page'})) {
 6699: 	&Apache::lonnet::logthis('end_page called without start_page'.
 6700: 				 $env{'request.filename'});
 6701:     }
 6702: }
 6703: 
 6704: sub simple_error_page {
 6705:     my ($r,$title,$msg) = @_;
 6706:     my $page =
 6707: 	&Apache::loncommon::start_page($title).
 6708: 	&mt($msg).
 6709: 	&Apache::loncommon::end_page();
 6710:     if (ref($r)) {
 6711: 	$r->print($page);
 6712: 	return;
 6713:     }
 6714:     return $page;
 6715: }
 6716: 
 6717: {
 6718:     my @row_count;
 6719:     sub start_data_table {
 6720: 	my ($add_class) = @_;
 6721: 	my $css_class = (join(' ','LC_data_table',$add_class));
 6722: 	unshift(@row_count,0);
 6723: 	return '<table class="'.$css_class.'">'."\n";
 6724:     }
 6725: 
 6726:     sub end_data_table {
 6727: 	shift(@row_count);
 6728: 	return '</table>'."\n";;
 6729:     }
 6730: 
 6731:     sub start_data_table_row {
 6732: 	my ($add_class) = @_;
 6733: 	$row_count[0]++;
 6734: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
 6735: 	$css_class = (join(' ',$css_class,$add_class));
 6736: 	return  '<tr class="'.$css_class.'">'."\n";;
 6737:     }
 6738:     
 6739:     sub continue_data_table_row {
 6740: 	my ($add_class) = @_;
 6741: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
 6742: 	$css_class = (join(' ',$css_class,$add_class));
 6743: 	return  '<tr class="'.$css_class.'">'."\n";;
 6744:     }
 6745: 
 6746:     sub end_data_table_row {
 6747: 	return '</tr>'."\n";;
 6748:     }
 6749: 
 6750:     sub start_data_table_empty_row {
 6751: #	$row_count[0]++;
 6752: 	return  '<tr class="LC_empty_row" >'."\n";;
 6753:     }
 6754: 
 6755:     sub end_data_table_empty_row {
 6756: 	return '</tr>'."\n";;
 6757:     }
 6758: 
 6759:     sub start_data_table_header_row {
 6760: 	return  '<tr class="LC_header_row">'."\n";;
 6761:     }
 6762: 
 6763:     sub end_data_table_header_row {
 6764: 	return '</tr>'."\n";;
 6765:     }
 6766: }
 6767: 
 6768: =pod
 6769: 
 6770: =item * &inhibit_menu_check($arg)
 6771: 
 6772: Checks for a inhibitmenu state and generates output to preserve it
 6773: 
 6774: Inputs:         $arg - can be any of
 6775:                      - undef - in which case the return value is a string 
 6776:                                to add  into arguments list of a uri
 6777:                      - 'input' - in which case the return value is a HTML
 6778:                                  <form> <input> field of type hidden to
 6779:                                  preserve the value
 6780:                      - a url - in which case the return value is the url with
 6781:                                the neccesary cgi args added to preserve the
 6782:                                inhibitmenu state
 6783:                      - a ref to a url - no return value, but the string is
 6784:                                         updated to include the neccessary cgi
 6785:                                         args to preserve the inhibitmenu state
 6786: 
 6787: =cut
 6788: 
 6789: sub inhibit_menu_check {
 6790:     my ($arg) = @_;
 6791:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
 6792:     if ($arg eq 'input') {
 6793: 	if ($env{'form.inhibitmenu'}) {
 6794: 	    return '<input type="hidden" name="inhibitmenu" value="'.$env{'form.inhibitmenu'}.'" />';
 6795: 	} else {
 6796: 	    return
 6797: 	}
 6798:     }
 6799:     if ($env{'form.inhibitmenu'}) {
 6800: 	if (ref($arg)) {
 6801: 	    $$arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
 6802: 	} elsif ($arg eq '') {
 6803: 	    $arg .= 'inhibitmenu='.$env{'form.inhibitmenu'};
 6804: 	} else {
 6805: 	    $arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
 6806: 	}
 6807:     }
 6808:     if (!ref($arg)) {
 6809: 	return $arg;
 6810:     }
 6811: }
 6812: 
 6813: ###############################################
 6814: 
 6815: =pod
 6816: 
 6817: =back
 6818: 
 6819: =head1 User Information Routines
 6820: 
 6821: =over 4
 6822: 
 6823: =item * &get_users_function()
 6824: 
 6825: Used by &bodytag to determine the current users primary role.
 6826: Returns either 'student','coordinator','admin', or 'author'.
 6827: 
 6828: =cut
 6829: 
 6830: ###############################################
 6831: sub get_users_function {
 6832:     my $function = 'norole';
 6833:     if ($env{'request.role'}=~/^(st)/) {
 6834:         $function='student';
 6835:     }
 6836:     if ($env{'request.role'}=~/^(cc|in|ta|ep)/) {
 6837:         $function='coordinator';
 6838:     }
 6839:     if ($env{'request.role'}=~/^(su|dc|ad|li)/) {
 6840:         $function='admin';
 6841:     }
 6842:     if (($env{'request.role'}=~/^(au|ca|aa)/) ||
 6843:         ($ENV{'REQUEST_URI'}=~/^(\/priv|\~)/)) {
 6844:         $function='author';
 6845:     }
 6846:     return $function;
 6847: }
 6848: 
 6849: ###############################################
 6850: 
 6851: =pod
 6852: 
 6853: =item * &show_course()
 6854: 
 6855: Used by lonmenu.pm and lonroles.pm to determine whether to use the word
 6856: 'Courses' or 'Roles' in inline navigation and on screen displaying user's roles.
 6857: 
 6858: Inputs:
 6859: None
 6860: 
 6861: Outputs:
 6862: Scalar: 1 if 'Course' to be used, 0 otherwise.
 6863: 
 6864: =cut
 6865: 
 6866: ###############################################
 6867: sub show_course {
 6868:     my $course = !$env{'user.adv'};
 6869:     if (!$env{'user.adv'}) {
 6870:         foreach my $env (keys(%env)) {
 6871:             next if ($env !~ m/^user\.priv\./);
 6872:             if ($env !~ m/^user\.priv\.(?:st|cm)/) {
 6873:                 $course = 0;
 6874:                 last;
 6875:             }
 6876:         }
 6877:     }
 6878:     return $course;
 6879: }
 6880: 
 6881: ###############################################
 6882: 
 6883: =pod
 6884: 
 6885: =item * &check_user_status()
 6886: 
 6887: Determines current status of supplied role for a
 6888: specific user. Roles can be active, previous or future.
 6889: 
 6890: Inputs: 
 6891: user's domain, user's username, course's domain,
 6892: course's number, optional section ID.
 6893: 
 6894: Outputs:
 6895: role status: active, previous or future. 
 6896: 
 6897: =cut
 6898: 
 6899: sub check_user_status {
 6900:     my ($udom,$uname,$cdom,$crs,$role,$sec) = @_;
 6901:     my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
 6902:     my @uroles = keys %userinfo;
 6903:     my $srchstr;
 6904:     my $active_chk = 'none';
 6905:     my $now = time;
 6906:     if (@uroles > 0) {
 6907:         if (($role eq 'cc') || ($sec eq '') || (!defined($sec))) {
 6908:             $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
 6909:         } else {
 6910:             $srchstr = '/'.$cdom.'/'.$crs.'/'.$sec.'_'.$role;
 6911:         }
 6912:         if (grep/^\Q$srchstr\E$/,@uroles) {
 6913:             my $role_end = 0;
 6914:             my $role_start = 0;
 6915:             $active_chk = 'active';
 6916:             if ($userinfo{$srchstr} =~ m/^\Q$role\E_(\d+)/) {
 6917:                 $role_end = $1;
 6918:                 if ($userinfo{$srchstr} =~ m/^\Q$role\E_\Q$role_end\E_(\d+)$/) {
 6919:                     $role_start = $1;
 6920:                 }
 6921:             }
 6922:             if ($role_start > 0) {
 6923:                 if ($now < $role_start) {
 6924:                     $active_chk = 'future';
 6925:                 }
 6926:             }
 6927:             if ($role_end > 0) {
 6928:                 if ($now > $role_end) {
 6929:                     $active_chk = 'previous';
 6930:                 }
 6931:             }
 6932:         }
 6933:     }
 6934:     return $active_chk;
 6935: }
 6936: 
 6937: ###############################################
 6938: 
 6939: =pod
 6940: 
 6941: =item * &get_sections()
 6942: 
 6943: Determines all the sections for a course including
 6944: sections with students and sections containing other roles.
 6945: Incoming parameters: 
 6946: 
 6947: 1. domain
 6948: 2. course number 
 6949: 3. reference to array containing roles for which sections should 
 6950: be gathered (optional).
 6951: 4. reference to array containing status types for which sections 
 6952: should be gathered (optional).
 6953: 
 6954: If the third argument is undefined, sections are gathered for any role. 
 6955: If the fourth argument is undefined, sections are gathered for any status.
 6956: Permissible values are 'active' or 'future' or 'previous'.
 6957:  
 6958: Returns section hash (keys are section IDs, values are
 6959: number of users in each section), subject to the
 6960: optional roles filter, optional status filter 
 6961: 
 6962: =cut
 6963: 
 6964: ###############################################
 6965: sub get_sections {
 6966:     my ($cdom,$cnum,$possible_roles,$possible_status) = @_;
 6967:     if (!defined($cdom) || !defined($cnum)) {
 6968:         my $cid =  $env{'request.course.id'};
 6969: 
 6970: 	return if (!defined($cid));
 6971: 
 6972:         $cdom = $env{'course.'.$cid.'.domain'};
 6973:         $cnum = $env{'course.'.$cid.'.num'};
 6974:     }
 6975: 
 6976:     my %sectioncount;
 6977:     my $now = time;
 6978: 
 6979:     if (!defined($possible_roles) || (grep(/^st$/,@$possible_roles))) {
 6980: 	my ($classlist) = &Apache::loncoursedata::get_classlist($cdom,$cnum);
 6981: 	my $sec_index = &Apache::loncoursedata::CL_SECTION();
 6982: 	my $status_index = &Apache::loncoursedata::CL_STATUS();
 6983:         my $start_index = &Apache::loncoursedata::CL_START();
 6984:         my $end_index = &Apache::loncoursedata::CL_END();
 6985:         my $status;
 6986: 	while (my ($student,$data) = each(%$classlist)) {
 6987: 	    my ($section,$stu_status,$start,$end) = ($data->[$sec_index],
 6988: 				                     $data->[$status_index],
 6989:                                                      $data->[$start_index],
 6990:                                                      $data->[$end_index]);
 6991:             if ($stu_status eq 'Active') {
 6992:                 $status = 'active';
 6993:             } elsif ($end < $now) {
 6994:                 $status = 'previous';
 6995:             } elsif ($start > $now) {
 6996:                 $status = 'future';
 6997:             } 
 6998: 	    if ($section ne '-1' && $section !~ /^\s*$/) {
 6999:                 if ((!defined($possible_status)) || (($status ne '') && 
 7000:                     (grep/^\Q$status\E$/,@{$possible_status}))) { 
 7001: 		    $sectioncount{$section}++;
 7002:                 }
 7003: 	    }
 7004: 	}
 7005:     }
 7006:     my %courseroles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
 7007:     foreach my $user (sort(keys(%courseroles))) {
 7008: 	if ($user !~ /^(\w{2})/) { next; }
 7009: 	my ($role) = ($user =~ /^(\w{2})/);
 7010: 	if ($possible_roles && !(grep(/^$role$/,@$possible_roles))) { next; }
 7011: 	my ($section,$status);
 7012: 	if ($role eq 'cr' &&
 7013: 	    $user =~ m-^$role/[^/]*/[^/]*/[^/]*:[^:]*:[^:]*:(\w+)-) {
 7014: 	    $section=$1;
 7015: 	}
 7016: 	if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
 7017: 	if (!defined($section) || $section eq '-1') { next; }
 7018:         my ($end,$start) = ($courseroles{$user} =~ /^([^:]*):([^:]*)$/);
 7019:         if ($end == -1 && $start == -1) {
 7020:             next; #deleted role
 7021:         }
 7022:         if (!defined($possible_status)) { 
 7023:             $sectioncount{$section}++;
 7024:         } else {
 7025:             if ((!$end || $end >= $now) && (!$start || $start <= $now)) {
 7026:                 $status = 'active';
 7027:             } elsif ($end < $now) {
 7028:                 $status = 'future';
 7029:             } elsif ($start > $now) {
 7030:                 $status = 'previous';
 7031:             }
 7032:             if (($status ne '') && (grep/^\Q$status\E$/,@{$possible_status})) {
 7033:                 $sectioncount{$section}++;
 7034:             }
 7035:         }
 7036:     }
 7037:     return %sectioncount;
 7038: }
 7039: 
 7040: ###############################################
 7041: 
 7042: =pod
 7043: 
 7044: =item * &get_course_users()
 7045: 
 7046: Retrieves usernames:domains for users in the specified course
 7047: with specific role(s), and access status. 
 7048: 
 7049: Incoming parameters:
 7050: 1. course domain
 7051: 2. course number
 7052: 3. access status: users must have - either active, 
 7053: previous, future, or all.
 7054: 4. reference to array of permissible roles
 7055: 5. reference to array of section restrictions (optional)
 7056: 6. reference to results object (hash of hashes).
 7057: 7. reference to optional userdata hash
 7058: 8. reference to optional statushash
 7059: 9. flag if privileged users (except those set to unhide in
 7060:    course settings) should be excluded    
 7061: Keys of top level results hash are roles.
 7062: Keys of inner hashes are username:domain, with 
 7063: values set to access type.
 7064: Optional userdata hash returns an array with arguments in the 
 7065: same order as loncoursedata::get_classlist() for student data.
 7066: 
 7067: Optional statushash returns
 7068: 
 7069: Entries for end, start, section and status are blank because
 7070: of the possibility of multiple values for non-student roles.
 7071: 
 7072: =cut
 7073: 
 7074: ###############################################
 7075: 
 7076: sub get_course_users {
 7077:     my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata,$statushash,$hidepriv) = @_;
 7078:     my %idx = ();
 7079:     my %seclists;
 7080: 
 7081:     $idx{udom} = &Apache::loncoursedata::CL_SDOM();
 7082:     $idx{uname} =  &Apache::loncoursedata::CL_SNAME();
 7083:     $idx{end} = &Apache::loncoursedata::CL_END();
 7084:     $idx{start} = &Apache::loncoursedata::CL_START();
 7085:     $idx{id} = &Apache::loncoursedata::CL_ID();
 7086:     $idx{section} = &Apache::loncoursedata::CL_SECTION();
 7087:     $idx{fullname} = &Apache::loncoursedata::CL_FULLNAME();
 7088:     $idx{status} = &Apache::loncoursedata::CL_STATUS();
 7089: 
 7090:     if (grep(/^st$/,@{$roles})) {
 7091:         my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist($cdom,$cnum);
 7092:         my $now = time;
 7093:         foreach my $student (keys(%{$classlist})) {
 7094:             my $match = 0;
 7095:             my $secmatch = 0;
 7096:             my $section = $$classlist{$student}[$idx{section}];
 7097:             my $status = $$classlist{$student}[$idx{status}];
 7098:             if ($section eq '') {
 7099:                 $section = 'none';
 7100:             }
 7101:             if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
 7102:                 if (grep(/^all$/,@{$sections})) {
 7103:                     $secmatch = 1;
 7104:                 } elsif ($$classlist{$student}[$idx{section}] eq '') {
 7105:                     if (grep(/^none$/,@{$sections})) {
 7106:                         $secmatch = 1;
 7107:                     }
 7108:                 } else {  
 7109: 		    if (grep(/^\Q$section\E$/,@{$sections})) {
 7110: 		        $secmatch = 1;
 7111:                     }
 7112: 		}
 7113:                 if (!$secmatch) {
 7114:                     next;
 7115:                 }
 7116:             }
 7117:             if (defined($$types{'active'})) {
 7118:                 if ($$classlist{$student}[$idx{status}] eq 'Active') {
 7119:                     push(@{$$users{st}{$student}},'active');
 7120:                     $match = 1;
 7121:                 }
 7122:             }
 7123:             if (defined($$types{'previous'})) {
 7124:                 if ($$classlist{$student}[$idx{status}] eq 'Expired') {
 7125:                     push(@{$$users{st}{$student}},'previous');
 7126:                     $match = 1;
 7127:                 }
 7128:             }
 7129:             if (defined($$types{'future'})) {
 7130:                 if ($$classlist{$student}[$idx{status}] eq 'Future') {
 7131:                     push(@{$$users{st}{$student}},'future');
 7132:                     $match = 1;
 7133:                 }
 7134:             }
 7135:             if ($match) {
 7136:                 push(@{$seclists{$student}},$section);
 7137:                 if (ref($userdata) eq 'HASH') {
 7138:                     $$userdata{$student} = $$classlist{$student};
 7139:                 }
 7140:                 if (ref($statushash) eq 'HASH') {
 7141:                     $statushash->{$student}{'st'}{$section} = $status;
 7142:                 }
 7143:             }
 7144:         }
 7145:     }
 7146:     if ((@{$roles} > 1) || ((@{$roles} == 1) && ($$roles[0] ne "st"))) {
 7147:         my %coursepersonnel = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
 7148:         my $now = time;
 7149:         my %displaystatus = ( previous => 'Expired',
 7150:                               active   => 'Active',
 7151:                               future   => 'Future',
 7152:                             );
 7153:         my %nothide;
 7154:         if ($hidepriv) {
 7155:             my %coursehash=&Apache::lonnet::coursedescription($cdom.'_'.$cnum);
 7156:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 7157:                 if ($user !~ /:/) {
 7158:                     $nothide{join(':',split(/[\@]/,$user))}=1;
 7159:                 } else {
 7160:                     $nothide{$user} = 1;
 7161:                 }
 7162:             }
 7163:         }
 7164:         foreach my $person (sort(keys(%coursepersonnel))) {
 7165:             my $match = 0;
 7166:             my $secmatch = 0;
 7167:             my $status;
 7168:             my ($role,$user,$usec) = ($person =~ /^([^:]*):([^:]+:[^:]+):([^:]*)/);
 7169:             $user =~ s/:$//;
 7170:             my ($end,$start) = split(/:/,$coursepersonnel{$person});
 7171:             if ($end == -1 || $start == -1) {
 7172:                 next;
 7173:             }
 7174:             if (($role) && ((grep(/^\Q$role\E$/,@{$roles})) ||
 7175:                 (grep(/^cr$/,@{$roles}) && $role =~ /^cr\//))) {
 7176:                 my ($uname,$udom) = split(/:/,$user);
 7177:                 if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
 7178:                     if (grep(/^all$/,@{$sections})) {
 7179:                         $secmatch = 1;
 7180:                     } elsif ($usec eq '') {
 7181:                         if (grep(/^none$/,@{$sections})) {
 7182:                             $secmatch = 1;
 7183:                         }
 7184:                     } else {
 7185:                         if (grep(/^\Q$usec\E$/,@{$sections})) {
 7186:                             $secmatch = 1;
 7187:                         }
 7188:                     }
 7189:                     if (!$secmatch) {
 7190:                         next;
 7191:                     }
 7192:                 }
 7193:                 if ($usec eq '') {
 7194:                     $usec = 'none';
 7195:                 }
 7196:                 if ($uname ne '' && $udom ne '') {
 7197:                     if ($hidepriv) {
 7198:                         if ((&Apache::lonnet::privileged($uname,$udom)) &&
 7199:                             (!$nothide{$uname.':'.$udom})) {
 7200:                             next;
 7201:                         }
 7202:                     }
 7203:                     if ($end > 0 && $end < $now) {
 7204:                         $status = 'previous';
 7205:                     } elsif ($start > $now) {
 7206:                         $status = 'future';
 7207:                     } else {
 7208:                         $status = 'active';
 7209:                     }
 7210:                     foreach my $type (keys(%{$types})) { 
 7211:                         if ($status eq $type) {
 7212:                             if (!grep(/^\Q$type\E$/,@{$$users{$role}{$user}})) {
 7213:                                 push(@{$$users{$role}{$user}},$type);
 7214:                             }
 7215:                             $match = 1;
 7216:                         }
 7217:                     }
 7218:                     if (($match) && (ref($userdata) eq 'HASH')) {
 7219:                         if (!exists($$userdata{$uname.':'.$udom})) {
 7220: 			    &get_user_info($udom,$uname,\%idx,$userdata);
 7221:                         }
 7222:                         if (!grep(/^\Q$usec\E$/,@{$seclists{$uname.':'.$udom}})) {
 7223:                             push(@{$seclists{$uname.':'.$udom}},$usec);
 7224:                         }
 7225:                         if (ref($statushash) eq 'HASH') {
 7226:                             $statushash->{$uname.':'.$udom}{$role}{$usec} = $displaystatus{$status};
 7227:                         }
 7228:                     }
 7229:                 }
 7230:             }
 7231:         }
 7232:         if (grep(/^ow$/,@{$roles})) {
 7233:             if ((defined($cdom)) && (defined($cnum))) {
 7234:                 my %csettings = &Apache::lonnet::get('environment',['internal.courseowner'],$cdom,$cnum);
 7235:                 if ( defined($csettings{'internal.courseowner'}) ) {
 7236:                     my $owner = $csettings{'internal.courseowner'};
 7237:                     next if ($owner eq '');
 7238:                     my ($ownername,$ownerdom);
 7239:                     if ($owner =~ /^([^:]+):([^:]+)$/) {
 7240:                         $ownername = $1;
 7241:                         $ownerdom = $2;
 7242:                     } else {
 7243:                         $ownername = $owner;
 7244:                         $ownerdom = $cdom;
 7245:                         $owner = $ownername.':'.$ownerdom;
 7246:                     }
 7247:                     @{$$users{'ow'}{$owner}} = 'any';
 7248:                     if (defined($userdata) && 
 7249: 			!exists($$userdata{$owner})) {
 7250: 			&get_user_info($ownerdom,$ownername,\%idx,$userdata);
 7251:                         if (!grep(/^none$/,@{$seclists{$owner}})) {
 7252:                             push(@{$seclists{$owner}},'none');
 7253:                         }
 7254:                         if (ref($statushash) eq 'HASH') {
 7255:                             $statushash->{$owner}{'ow'}{'none'} = 'Any';
 7256:                         }
 7257: 		    }
 7258:                 }
 7259:             }
 7260:         }
 7261:         foreach my $user (keys(%seclists)) {
 7262:             @{$seclists{$user}} = (sort {$a <=> $b} @{$seclists{$user}});
 7263:             $$userdata{$user}[$idx{section}] = join(',',@{$seclists{$user}});
 7264:         }
 7265:     }
 7266:     return;
 7267: }
 7268: 
 7269: sub get_user_info {
 7270:     my ($udom,$uname,$idx,$userdata) = @_;
 7271:     $$userdata{$uname.':'.$udom}[$$idx{fullname}] = 
 7272: 	&plainname($uname,$udom,'lastname');
 7273:     $$userdata{$uname.':'.$udom}[$$idx{uname}] = $uname;
 7274:     $$userdata{$uname.':'.$udom}[$$idx{udom}] = $udom;
 7275:     my %idhash =  &Apache::lonnet::idrget($udom,($uname));
 7276:     $$userdata{$uname.':'.$udom}[$$idx{id}] = $idhash{$uname}; 
 7277:     return;
 7278: }
 7279: 
 7280: ###############################################
 7281: 
 7282: =pod
 7283: 
 7284: =item * &get_user_quota()
 7285: 
 7286: Retrieves quota assigned for storage of portfolio files for a user  
 7287: 
 7288: Incoming parameters:
 7289: 1. user's username
 7290: 2. user's domain
 7291: 
 7292: Returns:
 7293: 1. Disk quota (in Mb) assigned to student.
 7294: 2. (Optional) Type of setting: custom or default
 7295:    (individually assigned or default for user's 
 7296:    institutional status).
 7297: 3. (Optional) - User's institutional status (e.g., faculty, staff
 7298:    or student - types as defined in localenroll::inst_usertypes 
 7299:    for user's domain, which determines default quota for user.
 7300: 4. (Optional) - Default quota which would apply to the user.
 7301: 
 7302: If a value has been stored in the user's environment, 
 7303: it will return that, otherwise it returns the maximal default
 7304: defined for the user's instituional status(es) in the domain.
 7305: 
 7306: =cut
 7307: 
 7308: ###############################################
 7309: 
 7310: 
 7311: sub get_user_quota {
 7312:     my ($uname,$udom) = @_;
 7313:     my ($quota,$quotatype,$settingstatus,$defquota);
 7314:     if (!defined($udom)) {
 7315:         $udom = $env{'user.domain'};
 7316:     }
 7317:     if (!defined($uname)) {
 7318:         $uname = $env{'user.name'};
 7319:     }
 7320:     if (($udom eq '' || $uname eq '') ||
 7321:         ($udom eq 'public') && ($uname eq 'public')) {
 7322:         $quota = 0;
 7323:         $quotatype = 'default';
 7324:         $defquota = 0; 
 7325:     } else {
 7326:         my $inststatus;
 7327:         if ($udom eq $env{'user.domain'} && $uname eq $env{'user.name'}) {
 7328:             $quota = $env{'environment.portfolioquota'};
 7329:             $inststatus = $env{'environment.inststatus'};
 7330:         } else {
 7331:             my %userenv = 
 7332:                 &Apache::lonnet::get('environment',['portfolioquota',
 7333:                                      'inststatus'],$udom,$uname);
 7334:             my ($tmp) = keys(%userenv);
 7335:             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
 7336:                 $quota = $userenv{'portfolioquota'};
 7337:                 $inststatus = $userenv{'inststatus'};
 7338:             } else {
 7339:                 undef(%userenv);
 7340:             }
 7341:         }
 7342:         ($defquota,$settingstatus) = &default_quota($udom,$inststatus);
 7343:         if ($quota eq '') {
 7344:             $quota = $defquota;
 7345:             $quotatype = 'default';
 7346:         } else {
 7347:             $quotatype = 'custom';
 7348:         }
 7349:     }
 7350:     if (wantarray) {
 7351:         return ($quota,$quotatype,$settingstatus,$defquota);
 7352:     } else {
 7353:         return $quota;
 7354:     }
 7355: }
 7356: 
 7357: ###############################################
 7358: 
 7359: =pod
 7360: 
 7361: =item * &default_quota()
 7362: 
 7363: Retrieves default quota assigned for storage of user portfolio files,
 7364: given an (optional) user's institutional status.
 7365: 
 7366: Incoming parameters:
 7367: 1. domain
 7368: 2. (Optional) institutional status(es).  This is a : separated list of 
 7369:    status types (e.g., faculty, staff, student etc.)
 7370:    which apply to the user for whom the default is being retrieved.
 7371:    If the institutional status string in undefined, the domain
 7372:    default quota will be returned. 
 7373: 
 7374: Returns:
 7375: 1. Default disk quota (in Mb) for user portfolios in the domain.
 7376: 2. (Optional) institutional type which determined the value of the
 7377:    default quota.
 7378: 
 7379: If a value has been stored in the domain's configuration db,
 7380: it will return that, otherwise it returns 20 (for backwards 
 7381: compatibility with domains which have not set up a configuration
 7382: db file; the original statically defined portfolio quota was 20 Mb). 
 7383: 
 7384: If the user's status includes multiple types (e.g., staff and student),
 7385: the largest default quota which applies to the user determines the
 7386: default quota returned.
 7387: 
 7388: =back
 7389: 
 7390: =cut
 7391: 
 7392: ###############################################
 7393: 
 7394: 
 7395: sub default_quota {
 7396:     my ($udom,$inststatus) = @_;
 7397:     my ($defquota,$settingstatus);
 7398:     my %quotahash = &Apache::lonnet::get_dom('configuration',
 7399:                                             ['quotas'],$udom);
 7400:     if (ref($quotahash{'quotas'}) eq 'HASH') {
 7401:         if ($inststatus ne '') {
 7402:             my @statuses = map { &unescape($_); } split(/:/,$inststatus);
 7403:             foreach my $item (@statuses) {
 7404:                 if (ref($quotahash{'quotas'}{'defaultquota'}) eq 'HASH') {
 7405:                     if ($quotahash{'quotas'}{'defaultquota'}{$item} ne '') {
 7406:                         if ($defquota eq '') {
 7407:                             $defquota = $quotahash{'quotas'}{'defaultquota'}{$item};
 7408:                             $settingstatus = $item;
 7409:                         } elsif ($quotahash{'quotas'}{'defaultquota'}{$item} > $defquota) {
 7410:                             $defquota = $quotahash{'quotas'}{'defaultquota'}{$item};
 7411:                             $settingstatus = $item;
 7412:                         }
 7413:                     }
 7414:                 } else {
 7415:                     if ($quotahash{'quotas'}{$item} ne '') {
 7416:                         if ($defquota eq '') {
 7417:                             $defquota = $quotahash{'quotas'}{$item};
 7418:                             $settingstatus = $item;
 7419:                         } elsif ($quotahash{'quotas'}{$item} > $defquota) {
 7420:                             $defquota = $quotahash{'quotas'}{$item};
 7421:                             $settingstatus = $item;
 7422:                         }
 7423:                     }
 7424:                 }
 7425:             }
 7426:         }
 7427:         if ($defquota eq '') {
 7428:             if (ref($quotahash{'quotas'}{'defaultquota'}) eq 'HASH') {
 7429:                 $defquota = $quotahash{'quotas'}{'defaultquota'}{'default'};
 7430:             } else {
 7431:                 $defquota = $quotahash{'quotas'}{'default'};
 7432:             }
 7433:             $settingstatus = 'default';
 7434:         }
 7435:     } else {
 7436:         $settingstatus = 'default';
 7437:         $defquota = 20;
 7438:     }
 7439:     if (wantarray) {
 7440:         return ($defquota,$settingstatus);
 7441:     } else {
 7442:         return $defquota;
 7443:     }
 7444: }
 7445: 
 7446: sub get_secgrprole_info {
 7447:     my ($cdom,$cnum,$needroles,$type)  = @_;
 7448:     my %sections_count = &get_sections($cdom,$cnum);
 7449:     my @sections =  (sort {$a <=> $b} keys(%sections_count));
 7450:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
 7451:     my @groups = sort(keys(%curr_groups));
 7452:     my $allroles = [];
 7453:     my $rolehash;
 7454:     my $accesshash = {
 7455:                      active => 'Currently has access',
 7456:                      future => 'Will have future access',
 7457:                      previous => 'Previously had access',
 7458:                   };
 7459:     if ($needroles) {
 7460:         $rolehash = {'all' => 'all'};
 7461:         my %user_roles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
 7462: 	if (&Apache::lonnet::error(%user_roles)) {
 7463: 	    undef(%user_roles);
 7464: 	}
 7465:         foreach my $item (keys(%user_roles)) {
 7466:             my ($role)=split(/\:/,$item,2);
 7467:             if ($role eq 'cr') { next; }
 7468:             if ($role =~ /^cr/) {
 7469:                 $$rolehash{$role} = (split('/',$role))[3];
 7470:             } else {
 7471:                 $$rolehash{$role} = &Apache::lonnet::plaintext($role,$type);
 7472:             }
 7473:         }
 7474:         foreach my $key (sort(keys(%{$rolehash}))) {
 7475:             push(@{$allroles},$key);
 7476:         }
 7477:         push (@{$allroles},'st');
 7478:         $$rolehash{'st'} = &Apache::lonnet::plaintext('st',$type);
 7479:     }
 7480:     return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
 7481: }
 7482: 
 7483: sub user_picker {
 7484:     my ($dom,$srch,$forcenewuser,$caller,$cancreate,$usertype) = @_;
 7485:     my $currdom = $dom;
 7486:     my %curr_selected = (
 7487:                         srchin => 'dom',
 7488:                         srchby => 'lastname',
 7489:                       );
 7490:     my $srchterm;
 7491:     if ((ref($srch) eq 'HASH') && ($env{'form.origform'} ne 'crtusername')) {
 7492:         if ($srch->{'srchby'} ne '') {
 7493:             $curr_selected{'srchby'} = $srch->{'srchby'};
 7494:         }
 7495:         if ($srch->{'srchin'} ne '') {
 7496:             $curr_selected{'srchin'} = $srch->{'srchin'};
 7497:         }
 7498:         if ($srch->{'srchtype'} ne '') {
 7499:             $curr_selected{'srchtype'} = $srch->{'srchtype'};
 7500:         }
 7501:         if ($srch->{'srchdomain'} ne '') {
 7502:             $currdom = $srch->{'srchdomain'};
 7503:         }
 7504:         $srchterm = $srch->{'srchterm'};
 7505:     }
 7506:     my %lt=&Apache::lonlocal::texthash(
 7507:                     'usr'       => 'Search criteria',
 7508:                     'doma'      => 'Domain/institution to search',
 7509:                     'uname'     => 'username',
 7510:                     'lastname'  => 'last name',
 7511:                     'lastfirst' => 'last name, first name',
 7512:                     'crs'       => 'in this course',
 7513:                     'dom'       => 'in selected LON-CAPA domain', 
 7514:                     'alc'       => 'all LON-CAPA',
 7515:                     'instd'     => 'in institutional directory for selected domain',
 7516:                     'exact'     => 'is',
 7517:                     'contains'  => 'contains',
 7518:                     'begins'    => 'begins with',
 7519:                     'youm'      => "You must include some text to search for.",
 7520:                     'thte'      => "The text you are searching for must contain at least two characters when using a 'begins' type search.",
 7521:                     'thet'      => "The text you are searching for must contain at least three characters when using a 'contains' type search.",
 7522:                     'yomc'      => "You must choose a domain when using an institutional directory search.",
 7523:                     'ymcd'      => "You must choose a domain when using a domain search.",
 7524:                     'whus'      => "When using searching by last,first you must include a comma as separator between last name and first name.",
 7525:                     'whse'      => "When searching by last,first you must include at least one character in the first name.",
 7526:                      'thfo'     => "The following need to be corrected before the search can be run:",
 7527:                                        );
 7528:     my $domform = &select_dom_form($currdom,'srchdomain',1,1);
 7529:     my $srchinsel = ' <select name="srchin">';
 7530: 
 7531:     my @srchins = ('crs','dom','alc','instd');
 7532: 
 7533:     foreach my $option (@srchins) {
 7534:         # FIXME 'alc' option unavailable until 
 7535:         #       loncreateuser::print_user_query_page()
 7536:         #       has been completed.
 7537:         next if ($option eq 'alc');
 7538:         next if (($option eq 'crs') && ($env{'form.form'} eq 'requestcrs'));  
 7539:         next if ($option eq 'crs' && !$env{'request.course.id'});
 7540:         if ($curr_selected{'srchin'} eq $option) {
 7541:             $srchinsel .= ' 
 7542:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
 7543:         } else {
 7544:             $srchinsel .= '
 7545:    <option value="'.$option.'">'.$lt{$option}.'</option>';
 7546:         }
 7547:     }
 7548:     $srchinsel .= "\n  </select>\n";
 7549: 
 7550:     my $srchbysel =  ' <select name="srchby">';
 7551:     foreach my $option ('lastname','lastfirst','uname') {
 7552:         if ($curr_selected{'srchby'} eq $option) {
 7553:             $srchbysel .= '
 7554:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
 7555:         } else {
 7556:             $srchbysel .= '
 7557:    <option value="'.$option.'">'.$lt{$option}.'</option>';
 7558:          }
 7559:     }
 7560:     $srchbysel .= "\n  </select>\n";
 7561: 
 7562:     my $srchtypesel = ' <select name="srchtype">';
 7563:     foreach my $option ('begins','contains','exact') {
 7564:         if ($curr_selected{'srchtype'} eq $option) {
 7565:             $srchtypesel .= '
 7566:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
 7567:         } else {
 7568:             $srchtypesel .= '
 7569:    <option value="'.$option.'">'.$lt{$option}.'</option>';
 7570:         }
 7571:     }
 7572:     $srchtypesel .= "\n  </select>\n";
 7573: 
 7574:     my ($newuserscript,$new_user_create);
 7575: 
 7576:     if ($forcenewuser) {
 7577:         if (ref($srch) eq 'HASH') {
 7578:             if ($srch->{'srchby'} eq 'uname' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchin'} eq 'dom' && $srch->{'srchdomain'} eq $env{'request.role.domain'}) {
 7579:                 if ($cancreate) {
 7580:                     $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>';
 7581:                 } else {
 7582:                     my $helplink = 'javascript:helpMenu('."'display'".')';
 7583:                     my %usertypetext = (
 7584:                         official   => 'institutional',
 7585:                         unofficial => 'non-institutional',
 7586:                     );
 7587:                     $new_user_create = '<p class="LC_warning">'
 7588:                                       .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
 7589:                                       .' '
 7590:                                       .&mt('Please contact the [_1]helpdesk[_2] for assistance.'
 7591:                                           ,'<a href="'.$helplink.'">','</a>')
 7592:                                       .'</p><br />';
 7593:                 }
 7594:             }
 7595:         }
 7596: 
 7597:         $newuserscript = <<"ENDSCRIPT";
 7598: 
 7599: function setSearch(createnew,callingForm) {
 7600:     if (createnew == 1) {
 7601:         for (var i=0; i<callingForm.srchby.length; i++) {
 7602:             if (callingForm.srchby.options[i].value == 'uname') {
 7603:                 callingForm.srchby.selectedIndex = i;
 7604:             }
 7605:         }
 7606:         for (var i=0; i<callingForm.srchin.length; i++) {
 7607:             if ( callingForm.srchin.options[i].value == 'dom') {
 7608: 		callingForm.srchin.selectedIndex = i;
 7609:             }
 7610:         }
 7611:         for (var i=0; i<callingForm.srchtype.length; i++) {
 7612:             if (callingForm.srchtype.options[i].value == 'exact') {
 7613:                 callingForm.srchtype.selectedIndex = i;
 7614:             }
 7615:         }
 7616:         for (var i=0; i<callingForm.srchdomain.length; i++) {
 7617:             if (callingForm.srchdomain.options[i].value == '$env{'request.role.domain'}') {
 7618:                 callingForm.srchdomain.selectedIndex = i;
 7619:             }
 7620:         }
 7621:     }
 7622: }
 7623: ENDSCRIPT
 7624: 
 7625:     }
 7626: 
 7627:     my $output = <<"END_BLOCK";
 7628: <script type="text/javascript">
 7629: // <![CDATA[
 7630: function validateEntry(callingForm) {
 7631: 
 7632:     var checkok = 1;
 7633:     var srchin;
 7634:     for (var i=0; i<callingForm.srchin.length; i++) {
 7635: 	if ( callingForm.srchin[i].checked ) {
 7636: 	    srchin = callingForm.srchin[i].value;
 7637: 	}
 7638:     }
 7639: 
 7640:     var srchtype = callingForm.srchtype.options[callingForm.srchtype.selectedIndex].value;
 7641:     var srchby = callingForm.srchby.options[callingForm.srchby.selectedIndex].value;
 7642:     var srchdomain = callingForm.srchdomain.options[callingForm.srchdomain.selectedIndex].value;
 7643:     var srchterm =  callingForm.srchterm.value;
 7644:     var srchin = callingForm.srchin.options[callingForm.srchin.selectedIndex].value;
 7645:     var msg = "";
 7646: 
 7647:     if (srchterm == "") {
 7648:         checkok = 0;
 7649:         msg += "$lt{'youm'}\\n";
 7650:     }
 7651: 
 7652:     if (srchtype== 'begins') {
 7653:         if (srchterm.length < 2) {
 7654:             checkok = 0;
 7655:             msg += "$lt{'thte'}\\n";
 7656:         }
 7657:     }
 7658: 
 7659:     if (srchtype== 'contains') {
 7660:         if (srchterm.length < 3) {
 7661:             checkok = 0;
 7662:             msg += "$lt{'thet'}\\n";
 7663:         }
 7664:     }
 7665:     if (srchin == 'instd') {
 7666:         if (srchdomain == '') {
 7667:             checkok = 0;
 7668:             msg += "$lt{'yomc'}\\n";
 7669:         }
 7670:     }
 7671:     if (srchin == 'dom') {
 7672:         if (srchdomain == '') {
 7673:             checkok = 0;
 7674:             msg += "$lt{'ymcd'}\\n";
 7675:         }
 7676:     }
 7677:     if (srchby == 'lastfirst') {
 7678:         if (srchterm.indexOf(",") == -1) {
 7679:             checkok = 0;
 7680:             msg += "$lt{'whus'}\\n";
 7681:         }
 7682:         if (srchterm.indexOf(",") == srchterm.length -1) {
 7683:             checkok = 0;
 7684:             msg += "$lt{'whse'}\\n";
 7685:         }
 7686:     }
 7687:     if (checkok == 0) {
 7688:         alert("$lt{'thfo'}\\n"+msg);
 7689:         return;
 7690:     }
 7691:     if (checkok == 1) {
 7692:         callingForm.submit();
 7693:     }
 7694: }
 7695: 
 7696: $newuserscript
 7697: 
 7698: // ]]>
 7699: </script>
 7700: 
 7701: $new_user_create
 7702: 
 7703: END_BLOCK
 7704: 
 7705:     $output .= &Apache::lonhtmlcommon::start_pick_box().
 7706:                &Apache::lonhtmlcommon::row_title($lt{'doma'}).
 7707:                $domform.
 7708:                &Apache::lonhtmlcommon::row_closure().
 7709:                &Apache::lonhtmlcommon::row_title($lt{'usr'}).
 7710:                $srchbysel.
 7711:                $srchtypesel. 
 7712:                '<input type="text" size="15" name="srchterm" value="'.$srchterm.'" />'.
 7713:                $srchinsel.
 7714:                &Apache::lonhtmlcommon::row_closure(1). 
 7715:                &Apache::lonhtmlcommon::end_pick_box().
 7716:                '<br />';
 7717:     return $output;
 7718: }
 7719: 
 7720: sub user_rule_check {
 7721:     my ($usershash,$checks,$alerts,$rulematch,$inst_results,$curr_rules,$got_rules) = @_;
 7722:     my $response;
 7723:     if (ref($usershash) eq 'HASH') {
 7724:         foreach my $user (keys(%{$usershash})) {
 7725:             my ($uname,$udom) = split(/:/,$user);
 7726:             next if ($udom eq '' || $uname eq '');
 7727:             my ($id,$newuser);
 7728:             if (ref($usershash->{$user}) eq 'HASH') {
 7729:                 $newuser = $usershash->{$user}->{'newuser'};
 7730:                 $id = $usershash->{$user}->{'id'};
 7731:             }
 7732:             my $inst_response;
 7733:             if (ref($checks) eq 'HASH') {
 7734:                 if (defined($checks->{'username'})) {
 7735:                     ($inst_response,%{$inst_results->{$user}}) = 
 7736:                         &Apache::lonnet::get_instuser($udom,$uname);
 7737:                 } elsif (defined($checks->{'id'})) {
 7738:                     ($inst_response,%{$inst_results->{$user}}) =
 7739:                         &Apache::lonnet::get_instuser($udom,undef,$id);
 7740:                 }
 7741:             } else {
 7742:                 ($inst_response,%{$inst_results->{$user}}) =
 7743:                     &Apache::lonnet::get_instuser($udom,$uname);
 7744:                 return;
 7745:             }
 7746:             if (!$got_rules->{$udom}) {
 7747:                 my %domconfig = &Apache::lonnet::get_dom('configuration',
 7748:                                                   ['usercreation'],$udom);
 7749:                 if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7750:                     foreach my $item ('username','id') {
 7751:                         if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
 7752:                             $$curr_rules{$udom}{$item} = 
 7753:                                 $domconfig{'usercreation'}{$item.'_rule'};
 7754:                         }
 7755:                     }
 7756:                 }
 7757:                 $got_rules->{$udom} = 1;  
 7758:             }
 7759:             foreach my $item (keys(%{$checks})) {
 7760:                 if (ref($$curr_rules{$udom}) eq 'HASH') {
 7761:                     if (ref($$curr_rules{$udom}{$item}) eq 'ARRAY') {
 7762:                         if (@{$$curr_rules{$udom}{$item}} > 0) {
 7763:                             my %rule_check = &Apache::lonnet::inst_rulecheck($udom,$uname,$id,$item,$$curr_rules{$udom}{$item});
 7764:                             foreach my $rule (@{$$curr_rules{$udom}{$item}}) {
 7765:                                 if ($rule_check{$rule}) {
 7766:                                     $$rulematch{$user}{$item} = $rule;
 7767:                                     if ($inst_response eq 'ok') {
 7768:                                         if (ref($inst_results) eq 'HASH') {
 7769:                                             if (ref($inst_results->{$user}) eq 'HASH') {
 7770:                                                 if (keys(%{$inst_results->{$user}}) == 0) {
 7771:                                                     $$alerts{$item}{$udom}{$uname} = 1;
 7772:                                                 }
 7773:                                             }
 7774:                                         }
 7775:                                     }
 7776:                                     last;
 7777:                                 }
 7778:                             }
 7779:                         }
 7780:                     }
 7781:                 }
 7782:             }
 7783:         }
 7784:     }
 7785:     return;
 7786: }
 7787: 
 7788: sub user_rule_formats {
 7789:     my ($domain,$domdesc,$curr_rules,$check) = @_;
 7790:     my %text = ( 
 7791:                  'username' => 'Usernames',
 7792:                  'id'       => 'IDs',
 7793:                );
 7794:     my $output;
 7795:     my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($domain,$check);
 7796:     if ((ref($rules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
 7797:         if (@{$ruleorder} > 0) {
 7798:             $output = '<br />'.&mt("$text{$check} with the following format(s) may <span class=\"LC_cusr_emph\">only</span> be used for verified users at [_1]:",$domdesc).' <ul>';
 7799:             foreach my $rule (@{$ruleorder}) {
 7800:                 if (ref($curr_rules) eq 'ARRAY') {
 7801:                     if (grep(/^\Q$rule\E$/,@{$curr_rules})) {
 7802:                         if (ref($rules->{$rule}) eq 'HASH') {
 7803:                             $output .= '<li>'.$rules->{$rule}{'name'}.': '.
 7804:                                         $rules->{$rule}{'desc'}.'</li>';
 7805:                         }
 7806:                     }
 7807:                 }
 7808:             }
 7809:             $output .= '</ul>';
 7810:         }
 7811:     }
 7812:     return $output;
 7813: }
 7814: 
 7815: sub instrule_disallow_msg {
 7816:     my ($checkitem,$domdesc,$count,$mode) = @_;
 7817:     my $response;
 7818:     my %text = (
 7819:                   item   => 'username',
 7820:                   items  => 'usernames',
 7821:                   match  => 'matches',
 7822:                   do     => 'does',
 7823:                   action => 'a username',
 7824:                   one    => 'one',
 7825:                );
 7826:     if ($count > 1) {
 7827:         $text{'item'} = 'usernames';
 7828:         $text{'match'} ='match';
 7829:         $text{'do'} = 'do';
 7830:         $text{'action'} = 'usernames',
 7831:         $text{'one'} = 'ones';
 7832:     }
 7833:     if ($checkitem eq 'id') {
 7834:         $text{'items'} = 'IDs';
 7835:         $text{'item'} = 'ID';
 7836:         $text{'action'} = 'an ID';
 7837:         if ($count > 1) {
 7838:             $text{'item'} = 'IDs';
 7839:             $text{'action'} = 'IDs';
 7840:         }
 7841:     }
 7842:     $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 />';
 7843:     if ($mode eq 'upload') {
 7844:         if ($checkitem eq 'username') {
 7845:             $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'}.");
 7846:         } elsif ($checkitem eq 'id') {
 7847:             $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.");
 7848:         }
 7849:     } elsif ($mode eq 'selfcreate') {
 7850:         if ($checkitem eq 'id') {
 7851:             $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.");
 7852:         }
 7853:     } else {
 7854:         if ($checkitem eq 'username') {
 7855:             $response .= &mt("You must choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}.");
 7856:         } elsif ($checkitem eq 'id') {
 7857:             $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.");
 7858:         }
 7859:     }
 7860:     return $response;
 7861: }
 7862: 
 7863: sub personal_data_fieldtitles {
 7864:     my %fieldtitles = &Apache::lonlocal::texthash (
 7865:                         id => 'Student/Employee ID',
 7866:                         permanentemail => 'E-mail address',
 7867:                         lastname => 'Last Name',
 7868:                         firstname => 'First Name',
 7869:                         middlename => 'Middle Name',
 7870:                         generation => 'Generation',
 7871:                         gen => 'Generation',
 7872:                         inststatus => 'Affiliation',
 7873:                    );
 7874:     return %fieldtitles;
 7875: }
 7876: 
 7877: sub sorted_inst_types {
 7878:     my ($dom) = @_;
 7879:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($dom);
 7880:     my $othertitle = &mt('All users');
 7881:     if ($env{'request.course.id'}) {
 7882:         $othertitle  = &mt('Any users');
 7883:     }
 7884:     my @types;
 7885:     if (ref($order) eq 'ARRAY') {
 7886:         @types = @{$order};
 7887:     }
 7888:     if (@types == 0) {
 7889:         if (ref($usertypes) eq 'HASH') {
 7890:             @types = sort(keys(%{$usertypes}));
 7891:         }
 7892:     }
 7893:     if (keys(%{$usertypes}) > 0) {
 7894:         $othertitle = &mt('Other users');
 7895:     }
 7896:     return ($othertitle,$usertypes,\@types);
 7897: }
 7898: 
 7899: sub get_institutional_codes {
 7900:     my ($settings,$allcourses,$LC_code) = @_;
 7901: # Get complete list of course sections to update
 7902:     my @currsections = ();
 7903:     my @currxlists = ();
 7904:     my $coursecode = $$settings{'internal.coursecode'};
 7905: 
 7906:     if ($$settings{'internal.sectionnums'} ne '') {
 7907:         @currsections = split(/,/,$$settings{'internal.sectionnums'});
 7908:     }
 7909: 
 7910:     if ($$settings{'internal.crosslistings'} ne '') {
 7911:         @currxlists = split(/,/,$$settings{'internal.crosslistings'});
 7912:     }
 7913: 
 7914:     if (@currxlists > 0) {
 7915:         foreach (@currxlists) {
 7916:             if (m/^([^:]+):(\w*)$/) {
 7917:                 unless (grep/^$1$/,@{$allcourses}) {
 7918:                     push @{$allcourses},$1;
 7919:                     $$LC_code{$1} = $2;
 7920:                 }
 7921:             }
 7922:         }
 7923:     }
 7924:  
 7925:     if (@currsections > 0) {
 7926:         foreach (@currsections) {
 7927:             if (m/^(\w+):(\w*)$/) {
 7928:                 my $sec = $coursecode.$1;
 7929:                 my $lc_sec = $2;
 7930:                 unless (grep/^$sec$/,@{$allcourses}) {
 7931:                     push @{$allcourses},$sec;
 7932:                     $$LC_code{$sec} = $lc_sec;
 7933:                 }
 7934:             }
 7935:         }
 7936:     }
 7937:     return;
 7938: }
 7939: 
 7940: =pod
 7941: 
 7942: =head1 Slot Helpers
 7943: 
 7944: =over 4
 7945: 
 7946: =item * sorted_slots()
 7947: 
 7948: Sorts an array of slot names in order of slot start time (earliest first). 
 7949: 
 7950: Inputs:
 7951: 
 7952: =over 4
 7953: 
 7954: slotsarr  - Reference to array of unsorted slot names.
 7955: 
 7956: slots     - Reference to hash of hash, where outer hash keys are slot names.
 7957: 
 7958: =back
 7959: 
 7960: Returns:
 7961: 
 7962: =over 4
 7963: 
 7964: sorted   - An array of slot names sorted by the start time of the slot.
 7965: 
 7966: =back
 7967: 
 7968: =back
 7969: 
 7970: =cut
 7971: 
 7972: 
 7973: sub sorted_slots {
 7974:     my ($slotsarr,$slots) = @_;
 7975:     my @sorted;
 7976:     if ((ref($slotsarr) eq 'ARRAY') && (ref($slots) eq 'HASH')) {
 7977:         @sorted =
 7978:             sort {
 7979:                      if (ref($slots->{$a}) && ref($slots->{$b})) {
 7980:                          return $slots->{$a}{'starttime'} <=> $slots->{$b}{'starttime'}
 7981:                      }
 7982:                      if (ref($slots->{$a})) { return -1;}
 7983:                      if (ref($slots->{$b})) { return 1;}
 7984:                      return 0;
 7985:                  } @{$slotsarr};
 7986:     }
 7987:     return @sorted;
 7988: }
 7989: 
 7990: 
 7991: =pod
 7992: 
 7993: =head1 HTTP Helpers
 7994: 
 7995: =over 4
 7996: 
 7997: =item * &get_unprocessed_cgi($query,$possible_names)
 7998: 
 7999: Modify the %env hash to contain unprocessed CGI form parameters held in
 8000: $query.  The parameters listed in $possible_names (an array reference),
 8001: will be set in $env{'form.name'} if they do not already exist.
 8002: 
 8003: Typically called with $ENV{'QUERY_STRING'} as the first parameter.  
 8004: $possible_names is an ref to an array of form element names.  As an example:
 8005: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
 8006: will result in $env{'form.uname'} and $env{'form.udom'} being set.
 8007: 
 8008: =cut
 8009: 
 8010: sub get_unprocessed_cgi {
 8011:   my ($query,$possible_names)= @_;
 8012:   # $Apache::lonxml::debug=1;
 8013:   foreach my $pair (split(/&/,$query)) {
 8014:     my ($name, $value) = split(/=/,$pair);
 8015:     $name = &unescape($name);
 8016:     if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
 8017:       $value =~ tr/+/ /;
 8018:       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
 8019:       unless (defined($env{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
 8020:     }
 8021:   }
 8022: }
 8023: 
 8024: =pod
 8025: 
 8026: =item * &cacheheader() 
 8027: 
 8028: returns cache-controlling header code
 8029: 
 8030: =cut
 8031: 
 8032: sub cacheheader {
 8033:     unless ($env{'request.method'} eq 'GET') { return ''; }
 8034:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
 8035:     my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
 8036:                 <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
 8037:                 <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
 8038:     return $output;
 8039: }
 8040: 
 8041: =pod
 8042: 
 8043: =item * &no_cache($r) 
 8044: 
 8045: specifies header code to not have cache
 8046: 
 8047: =cut
 8048: 
 8049: sub no_cache {
 8050:     my ($r) = @_;
 8051:     if ($ENV{'REQUEST_METHOD'} ne 'GET' &&
 8052: 	$env{'request.method'} ne 'GET') { return ''; }
 8053:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time));
 8054:     $r->no_cache(1);
 8055:     $r->header_out("Expires" => $date);
 8056:     $r->header_out("Pragma" => "no-cache");
 8057: }
 8058: 
 8059: sub content_type {
 8060:     my ($r,$type,$charset) = @_;
 8061:     if ($r) {
 8062: 	#  Note that printout.pl calls this with undef for $r.
 8063: 	&no_cache($r);
 8064:     }
 8065:     if ($env{'browser.mathml'} && $type eq 'text/html') { $type='text/xml'; }
 8066:     unless ($charset) {
 8067: 	$charset=&Apache::lonlocal::current_encoding;
 8068:     }
 8069:     if ($charset) { $type.='; charset='.$charset; }
 8070:     if ($r) {
 8071: 	$r->content_type($type);
 8072:     } else {
 8073: 	print("Content-type: $type\n\n");
 8074:     }
 8075: }
 8076: 
 8077: =pod
 8078: 
 8079: =item * &add_to_env($name,$value) 
 8080: 
 8081: adds $name to the %env hash with value
 8082: $value, if $name already exists, the entry is converted to an array
 8083: reference and $value is added to the array.
 8084: 
 8085: =cut
 8086: 
 8087: sub add_to_env {
 8088:   my ($name,$value)=@_;
 8089:   if (defined($env{$name})) {
 8090:     if (ref($env{$name})) {
 8091:       #already have multiple values
 8092:       push(@{ $env{$name} },$value);
 8093:     } else {
 8094:       #first time seeing multiple values, convert hash entry to an arrayref
 8095:       my $first=$env{$name};
 8096:       undef($env{$name});
 8097:       push(@{ $env{$name} },$first,$value);
 8098:     }
 8099:   } else {
 8100:     $env{$name}=$value;
 8101:   }
 8102: }
 8103: 
 8104: =pod
 8105: 
 8106: =item * &get_env_multiple($name) 
 8107: 
 8108: gets $name from the %env hash, it seemlessly handles the cases where multiple
 8109: values may be defined and end up as an array ref.
 8110: 
 8111: returns an array of values
 8112: 
 8113: =cut
 8114: 
 8115: sub get_env_multiple {
 8116:     my ($name) = @_;
 8117:     my @values;
 8118:     if (defined($env{$name})) {
 8119:         # exists is it an array
 8120:         if (ref($env{$name})) {
 8121:             @values=@{ $env{$name} };
 8122:         } else {
 8123:             $values[0]=$env{$name};
 8124:         }
 8125:     }
 8126:     return(@values);
 8127: }
 8128: 
 8129: sub ask_for_embedded_content {
 8130:     my ($actionurl,$state,$allfiles,$codebase,$args)=@_;
 8131:     my $upload_output = '
 8132:    <form name="upload_embedded" action="'.$actionurl.'"
 8133:                   method="post" enctype="multipart/form-data">';
 8134:     $upload_output .= $state;
 8135:     $upload_output .= '<b>Upload embedded files</b>:<br />'.&start_data_table();
 8136: 
 8137:     my $num = 0;
 8138:     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%{$allfiles})) {
 8139:         $upload_output .= &start_data_table_row().
 8140:             '<td>'.$embed_file.'</td><td>';
 8141:         if ($args->{'ignore_remote_references'}
 8142:             && $embed_file =~ m{^\w+://}) {
 8143:             $upload_output.='<span class="LC_warning">'.&mt("URL points to other server.").'</span>';
 8144:         } elsif ($args->{'error_on_invalid_names'}
 8145:             && $embed_file ne &Apache::lonnet::clean_filename($embed_file,{'keep_path' => 1,})) {
 8146: 
 8147:             $upload_output.='<span class="LC_warning">'.&mt("Invalid characters").'</span>';
 8148: 
 8149:         } else {
 8150:             $upload_output .='
 8151:            <input name="embedded_item_'.$num.'" type="file" value="" />
 8152:            <input name="embedded_orig_'.$num.'" type="hidden" value="'.&escape($embed_file).'" />';
 8153:             my $attrib = join(':',@{$$allfiles{$embed_file}});
 8154:             $upload_output .=
 8155:                 "\n\t\t".
 8156:                 '<input name="embedded_attrib_'.$num.'" type="hidden" value="'.
 8157:                 $attrib.'" />';
 8158:             if (exists($$codebase{$embed_file})) {
 8159:                 $upload_output .=
 8160:                     "\n\t\t".
 8161:                     '<input name="codebase_'.$num.'" type="hidden" value="'.
 8162:                     &escape($$codebase{$embed_file}).'" />';
 8163:             }
 8164:         }
 8165:         $upload_output .= '</td>'.&Apache::loncommon::end_data_table_row();
 8166:         $num++;
 8167:     }
 8168:     $upload_output .= &Apache::loncommon::end_data_table().'<br />
 8169:    <input type ="hidden" name="number_embedded_items" value="'.$num.'" />
 8170:    <input type ="submit" value="'.&mt('Upload Listed Files').'" />
 8171:    '.&mt('(only files for which a location has been provided will be uploaded)').'
 8172:    </form>';
 8173:     return $upload_output;
 8174: }
 8175: 
 8176: sub upload_embedded {
 8177:     my ($context,$dirpath,$uname,$udom,$dir_root,$url_root,$group,$disk_quota,
 8178:         $current_disk_usage) = @_;
 8179:     my $output;
 8180:     for (my $i=0; $i<$env{'form.number_embedded_items'}; $i++) {
 8181:         next if (!exists($env{'form.embedded_item_'.$i.'.filename'}));
 8182:         my $orig_uploaded_filename =
 8183:             $env{'form.embedded_item_'.$i.'.filename'};
 8184: 
 8185:         $env{'form.embedded_orig_'.$i} =
 8186:             &unescape($env{'form.embedded_orig_'.$i});
 8187:         my ($path,$fname) =
 8188:             ($env{'form.embedded_orig_'.$i} =~ m{(.*/)([^/]*)});
 8189:         # no path, whole string is fname
 8190:         if (!$fname) { $fname = $env{'form.embedded_orig_'.$i} };
 8191: 
 8192:         $path = $env{'form.currentpath'}.$path;
 8193:         $fname = &Apache::lonnet::clean_filename($fname);
 8194:         # See if there is anything left
 8195:         next if ($fname eq '');
 8196: 
 8197:         # Check if file already exists as a file or directory.
 8198:         my ($state,$msg);
 8199:         if ($context eq 'portfolio') {
 8200:             my $port_path = $dirpath;
 8201:             if ($group ne '') {
 8202:                 $port_path = "groups/$group/$port_path";
 8203:             }
 8204:             ($state,$msg) = &check_for_upload($path,$fname,$group,'embedded_item_'.$i,
 8205:                                               $dir_root,$port_path,$disk_quota,
 8206:                                               $current_disk_usage,$uname,$udom);
 8207:             if ($state eq 'will_exceed_quota'
 8208:                 || $state eq 'file_locked'
 8209:                 || $state eq 'file_exists' ) {
 8210:                 $output .= $msg;
 8211:                 next;
 8212:             }
 8213:         } elsif (($context eq 'author') || ($context eq 'testbank')) {
 8214:             ($state,$msg) = &check_for_existing($path,$fname,'embedded_item_'.$i);
 8215:             if ($state eq 'exists') {
 8216:                 $output .= $msg;
 8217:                 next;
 8218:             }
 8219:         }
 8220:         # Check if extension is valid
 8221:         if (($fname =~ /\.(\w+)$/) &&
 8222:             (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8223:             $output .= &mt('Invalid file extension ([_1]) - reserved for LONCAPA use - rename the file with a different extension and re-upload. ',$1);
 8224:             next;
 8225:         } elsif (($fname =~ /\.(\w+)$/) &&
 8226:                  (!defined(&Apache::loncommon::fileembstyle($1)))) {
 8227:             $output .= &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8228:             next;
 8229:         } elsif ($fname=~/\.(\d+)\.(\w+)$/) {
 8230:             $output .= &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8231:             next;
 8232:         }
 8233: 
 8234:         $env{'form.embedded_item_'.$i.'.filename'}=$fname;
 8235:         if ($context eq 'portfolio') {
 8236:             my $result=
 8237:                 &Apache::lonnet::userfileupload('embedded_item_'.$i,'',
 8238:                                                 $dirpath.$path);
 8239:             if ($result !~ m|^/uploaded/|) {
 8240:                 $output .= '<span class="LC_error">'
 8241:                       .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
 8242:                            ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
 8243:                       .'</span><br />';
 8244:                 next;
 8245:             } else {
 8246:                 $output .= '<p>'.&mt('Uploaded [_1]','<span class="LC_filename">'.
 8247:                            $path.$fname.'</span>').'</p>';     
 8248:             }
 8249:         } else {
 8250: # Save the file
 8251:             my $target = $env{'form.embedded_item_'.$i};
 8252:             my $fullpath = $dir_root.$dirpath.'/'.$path;
 8253:             my $dest = $fullpath.$fname;
 8254:             my $url = $url_root.$dirpath.'/'.$path.$fname;
 8255:             my @parts=split(/\//,$fullpath);
 8256:             my $count;
 8257:             my $filepath = $dir_root;
 8258:             for ($count=4;$count<=$#parts;$count++) {
 8259:                 $filepath .= "/$parts[$count]";
 8260:                 if ((-e $filepath)!=1) {
 8261:                     mkdir($filepath,0770);
 8262:                 }
 8263:             }
 8264:             my $fh;
 8265:             if (!open($fh,'>'.$dest)) {
 8266:                 &Apache::lonnet::logthis('Failed to create '.$dest);
 8267:                 $output .= '<span class="LC_error">'.
 8268:                            &mt('An error occurred while trying to upload [_1] for embedded element [_2].',$orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
 8269:                            '</span><br />';
 8270:             } else {
 8271:                 if (!print $fh $env{'form.embedded_item_'.$i}) {
 8272:                     &Apache::lonnet::logthis('Failed to write to '.$dest);
 8273:                     $output .= '<span class="LC_error">'.
 8274:                               &mt('An error occurred while writing the file [_1] for embedded element [_2].',$orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
 8275:                               '</span><br />';
 8276:                 } else {
 8277:                     if ($context eq 'testbank') {
 8278:                         $output .= &mt('Embedded file uploaded successfully:').
 8279:                                    '&nbsp;<a href="'.$url.'">'.
 8280:                                    $orig_uploaded_filename.'</a><br />';
 8281:                     } else {
 8282:                         $output .= '<span class=\"LC_fontsize_large\">'.
 8283:                                    &mt('View embedded file: [_1]','<a href="'.$url.'">'.
 8284:                                    $orig_uploaded_filename.'</a>').'</span><br />';
 8285:                     }
 8286:                 }
 8287:                 close($fh);
 8288:             }
 8289:         }
 8290:     }
 8291:     return $output;
 8292: }
 8293: 
 8294: sub check_for_existing {
 8295:     my ($path,$fname,$element) = @_;
 8296:     my ($state,$msg);
 8297:     if (-d $path.'/'.$fname) {
 8298:         $state = 'exists';
 8299:         $msg = &mt('Unable to upload [_1]. A directory by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
 8300:     } elsif (-e $path.'/'.$fname) {
 8301:         $state = 'exists';
 8302:         $msg = &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
 8303:     }
 8304:     if ($state eq 'exists') {
 8305:         $msg = '<span class="LC_error">'.$msg.'</span><br />';
 8306:     }
 8307:     return ($state,$msg);
 8308: }
 8309: 
 8310: sub check_for_upload {
 8311:     my ($path,$fname,$group,$element,$portfolio_root,$port_path,
 8312:         $disk_quota,$current_disk_usage,$uname,$udom) = @_;
 8313:     my $filesize = (length($env{'form.'.$element})) / 1000; #express in k (1024?)
 8314:     my $getpropath = 1;
 8315:     my @dir_list = &Apache::lonnet::dirlist($portfolio_root.$path,$udom,$uname,
 8316:                                             $getpropath);
 8317:     my $found_file = 0;
 8318:     my $locked_file = 0;
 8319:     foreach my $line (@dir_list) {
 8320:         my ($file_name)=split(/\&/,$line,2);
 8321:         if ($file_name eq $fname){
 8322:             $file_name = $path.$file_name;
 8323:             if ($group ne '') {
 8324:                 $file_name = $group.$file_name;
 8325:             }
 8326:             $found_file = 1;
 8327:             if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
 8328:                 $locked_file = 1;
 8329:             }
 8330:         }
 8331:     }
 8332:     if (($current_disk_usage + $filesize) > $disk_quota){
 8333:         my $msg = '<span class="LC_error">'.
 8334:                 &mt('Unable to upload [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.','<span class="LC_filename">'.$fname.'</span>',$filesize).'</span>'.
 8335:                   '<br />'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',$disk_quota,$current_disk_usage);
 8336:         return ('will_exceed_quota',$msg);
 8337:     } elsif ($found_file) {
 8338:         if ($locked_file) {
 8339:             my $msg = '<span class="LC_error">';
 8340:             $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>');
 8341:             $msg .= '</span><br />';
 8342:             $msg .= &mt('You will be able to rename or delete existing [_1] after a grade has been assigned.','<span class="LC_filename">'.$fname.'</span>');
 8343:             return ('file_locked',$msg);
 8344:         } else {
 8345:             my $msg = '<span class="LC_error">';
 8346:             $msg .= &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$port_path.$env{'form.currentpath'});
 8347:             $msg .= '</span>';
 8348:             $msg .= '<br />';
 8349:             $msg .= &mt('To upload, rename or delete existing [_1] in [_2].','<span class="LC_filename">'.$fname.'</span>', $port_path.$env{'form.currentpath'});
 8350:             return ('file_exists',$msg);
 8351:         }
 8352:     }
 8353: }
 8354: 
 8355: 
 8356: =pod
 8357: 
 8358: =back
 8359: 
 8360: =head1 CSV Upload/Handling functions
 8361: 
 8362: =over 4
 8363: 
 8364: =item * &upfile_store($r)
 8365: 
 8366: Store uploaded file, $r should be the HTTP Request object,
 8367: needs $env{'form.upfile'}
 8368: returns $datatoken to be put into hidden field
 8369: 
 8370: =cut
 8371: 
 8372: sub upfile_store {
 8373:     my $r=shift;
 8374:     $env{'form.upfile'}=~s/\r/\n/gs;
 8375:     $env{'form.upfile'}=~s/\f/\n/gs;
 8376:     $env{'form.upfile'}=~s/\n+/\n/gs;
 8377:     $env{'form.upfile'}=~s/\n+$//gs;
 8378: 
 8379:     my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
 8380: 	'_enroll_'.$env{'request.course.id'}.'_'.time.'_'.$$;
 8381:     {
 8382:         my $datafile = $r->dir_config('lonDaemons').
 8383:                            '/tmp/'.$datatoken.'.tmp';
 8384:         if ( open(my $fh,">$datafile") ) {
 8385:             print $fh $env{'form.upfile'};
 8386:             close($fh);
 8387:         }
 8388:     }
 8389:     return $datatoken;
 8390: }
 8391: 
 8392: =pod
 8393: 
 8394: =item * &load_tmp_file($r)
 8395: 
 8396: Load uploaded file from tmp, $r should be the HTTP Request object,
 8397: needs $env{'form.datatoken'},
 8398: sets $env{'form.upfile'} to the contents of the file
 8399: 
 8400: =cut
 8401: 
 8402: sub load_tmp_file {
 8403:     my $r=shift;
 8404:     my @studentdata=();
 8405:     {
 8406:         my $studentfile = $r->dir_config('lonDaemons').
 8407:                               '/tmp/'.$env{'form.datatoken'}.'.tmp';
 8408:         if ( open(my $fh,"<$studentfile") ) {
 8409:             @studentdata=<$fh>;
 8410:             close($fh);
 8411:         }
 8412:     }
 8413:     $env{'form.upfile'}=join('',@studentdata);
 8414: }
 8415: 
 8416: =pod
 8417: 
 8418: =item * &upfile_record_sep()
 8419: 
 8420: Separate uploaded file into records
 8421: returns array of records,
 8422: needs $env{'form.upfile'} and $env{'form.upfiletype'}
 8423: 
 8424: =cut
 8425: 
 8426: sub upfile_record_sep {
 8427:     if ($env{'form.upfiletype'} eq 'xml') {
 8428:     } else {
 8429: 	my @records;
 8430: 	foreach my $line (split(/\n/,$env{'form.upfile'})) {
 8431: 	    if ($line=~/^\s*$/) { next; }
 8432: 	    push(@records,$line);
 8433: 	}
 8434: 	return @records;
 8435:     }
 8436: }
 8437: 
 8438: =pod
 8439: 
 8440: =item * &record_sep($record)
 8441: 
 8442: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $env{'form.upfiletype'}
 8443: 
 8444: =cut
 8445: 
 8446: sub takeleft {
 8447:     my $index=shift;
 8448:     return substr('0000'.$index,-4,4);
 8449: }
 8450: 
 8451: sub record_sep {
 8452:     my $record=shift;
 8453:     my %components=();
 8454:     if ($env{'form.upfiletype'} eq 'xml') {
 8455:     } elsif ($env{'form.upfiletype'} eq 'space') {
 8456:         my $i=0;
 8457:         foreach my $field (split(/\s+/,$record)) {
 8458:             $field=~s/^(\"|\')//;
 8459:             $field=~s/(\"|\')$//;
 8460:             $components{&takeleft($i)}=$field;
 8461:             $i++;
 8462:         }
 8463:     } elsif ($env{'form.upfiletype'} eq 'tab') {
 8464:         my $i=0;
 8465:         foreach my $field (split(/\t/,$record)) {
 8466:             $field=~s/^(\"|\')//;
 8467:             $field=~s/(\"|\')$//;
 8468:             $components{&takeleft($i)}=$field;
 8469:             $i++;
 8470:         }
 8471:     } else {
 8472:         my $separator=',';
 8473:         if ($env{'form.upfiletype'} eq 'semisv') {
 8474:             $separator=';';
 8475:         }
 8476:         my $i=0;
 8477: # the character we are looking for to indicate the end of a quote or a record 
 8478:         my $looking_for=$separator;
 8479: # do not add the characters to the fields
 8480:         my $ignore=0;
 8481: # we just encountered a separator (or the beginning of the record)
 8482:         my $just_found_separator=1;
 8483: # store the field we are working on here
 8484:         my $field='';
 8485: # work our way through all characters in record
 8486:         foreach my $character ($record=~/(.)/g) {
 8487:             if ($character eq $looking_for) {
 8488:                if ($character ne $separator) {
 8489: # Found the end of a quote, again looking for separator
 8490:                   $looking_for=$separator;
 8491:                   $ignore=1;
 8492:                } else {
 8493: # Found a separator, store away what we got
 8494:                   $components{&takeleft($i)}=$field;
 8495: 	          $i++;
 8496:                   $just_found_separator=1;
 8497:                   $ignore=0;
 8498:                   $field='';
 8499:                }
 8500:                next;
 8501:             }
 8502: # single or double quotation marks after a separator indicate beginning of a quote
 8503: # we are now looking for the end of the quote and need to ignore separators
 8504:             if ((($character eq '"') || ($character eq "'")) && ($just_found_separator))  {
 8505:                $looking_for=$character;
 8506:                next;
 8507:             }
 8508: # ignore would be true after we reached the end of a quote
 8509:             if ($ignore) { next; }
 8510:             if (($just_found_separator) && ($character=~/\s/)) { next; }
 8511:             $field.=$character;
 8512:             $just_found_separator=0; 
 8513:         }
 8514: # catch the very last entry, since we never encountered the separator
 8515:         $components{&takeleft($i)}=$field;
 8516:     }
 8517:     return %components;
 8518: }
 8519: 
 8520: ######################################################
 8521: ######################################################
 8522: 
 8523: =pod
 8524: 
 8525: =item * &upfile_select_html()
 8526: 
 8527: Return HTML code to select a file from the users machine and specify 
 8528: the file type.
 8529: 
 8530: =cut
 8531: 
 8532: ######################################################
 8533: ######################################################
 8534: sub upfile_select_html {
 8535:     my %Types = (
 8536:                  csv   => &mt('CSV (comma separated values, spreadsheet)'),
 8537:                  semisv => &mt('Semicolon separated values'),
 8538:                  space => &mt('Space separated'),
 8539:                  tab   => &mt('Tabulator separated'),
 8540: #                 xml   => &mt('HTML/XML'),
 8541:                  );
 8542:     my $Str = '<input type="file" name="upfile" size="50" />'.
 8543:         '<br />'.&mt('Type').': <select name="upfiletype">';
 8544:     foreach my $type (sort(keys(%Types))) {
 8545:         $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
 8546:     }
 8547:     $Str .= "</select>\n";
 8548:     return $Str;
 8549: }
 8550: 
 8551: sub get_samples {
 8552:     my ($records,$toget) = @_;
 8553:     my @samples=({});
 8554:     my $got=0;
 8555:     foreach my $rec (@$records) {
 8556: 	my %temp = &record_sep($rec);
 8557: 	if (! grep(/\S/, values(%temp))) { next; }
 8558: 	if (%temp) {
 8559: 	    $samples[$got]=\%temp;
 8560: 	    $got++;
 8561: 	    if ($got == $toget) { last; }
 8562: 	}
 8563:     }
 8564:     return \@samples;
 8565: }
 8566: 
 8567: ######################################################
 8568: ######################################################
 8569: 
 8570: =pod
 8571: 
 8572: =item * &csv_print_samples($r,$records)
 8573: 
 8574: Prints a table of sample values from each column uploaded $r is an
 8575: Apache Request ref, $records is an arrayref from
 8576: &Apache::loncommon::upfile_record_sep
 8577: 
 8578: =cut
 8579: 
 8580: ######################################################
 8581: ######################################################
 8582: sub csv_print_samples {
 8583:     my ($r,$records) = @_;
 8584:     my $samples = &get_samples($records,5);
 8585: 
 8586:     $r->print(&mt('Samples').'<br />'.&start_data_table().
 8587:               &start_data_table_header_row());
 8588:     foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) { 
 8589:         $r->print('<th>'.&mt('Column [_1]',($sample+1)).'</th>'); }
 8590:     $r->print(&end_data_table_header_row());
 8591:     foreach my $hash (@$samples) {
 8592: 	$r->print(&start_data_table_row());
 8593: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
 8594: 	    $r->print('<td>');
 8595: 	    if (defined($$hash{$sample})) { $r->print($$hash{$sample}); }
 8596: 	    $r->print('</td>');
 8597: 	}
 8598: 	$r->print(&end_data_table_row());
 8599:     }
 8600:     $r->print(&end_data_table().'<br />'."\n");
 8601: }
 8602: 
 8603: ######################################################
 8604: ######################################################
 8605: 
 8606: =pod
 8607: 
 8608: =item * &csv_print_select_table($r,$records,$d)
 8609: 
 8610: Prints a table to create associations between values and table columns.
 8611: 
 8612: $r is an Apache Request ref,
 8613: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
 8614: $d is an array of 2 element arrays (internal name, displayed name,defaultcol)
 8615: 
 8616: =cut
 8617: 
 8618: ######################################################
 8619: ######################################################
 8620: sub csv_print_select_table {
 8621:     my ($r,$records,$d) = @_;
 8622:     my $i=0;
 8623:     my $samples = &get_samples($records,1);
 8624:     $r->print(&mt('Associate columns with student attributes.')."\n".
 8625: 	      &start_data_table().&start_data_table_header_row().
 8626:               '<th>'.&mt('Attribute').'</th>'.
 8627:               '<th>'.&mt('Column').'</th>'.
 8628:               &end_data_table_header_row()."\n");
 8629:     foreach my $array_ref (@$d) {
 8630: 	my ($value,$display,$defaultcol)=@{ $array_ref };
 8631: 	$r->print(&start_data_table_row().'<td>'.$display.'</td>');
 8632: 
 8633: 	$r->print('<td><select name="f'.$i.'"'.
 8634: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
 8635: 	$r->print('<option value="none"></option>');
 8636: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
 8637: 	    $r->print('<option value="'.$sample.'"'.
 8638:                       ($sample eq $defaultcol ? ' selected="selected" ' : '').
 8639:                       '>'.&mt('Column [_1]',($sample+1)).'</option>');
 8640: 	}
 8641: 	$r->print('</select></td>'.&end_data_table_row()."\n");
 8642: 	$i++;
 8643:     }
 8644:     $r->print(&end_data_table());
 8645:     $i--;
 8646:     return $i;
 8647: }
 8648: 
 8649: ######################################################
 8650: ######################################################
 8651: 
 8652: =pod
 8653: 
 8654: =item * &csv_samples_select_table($r,$records,$d)
 8655: 
 8656: Prints a table of sample values from the upload and can make associate samples to internal names.
 8657: 
 8658: $r is an Apache Request ref,
 8659: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
 8660: $d is an array of 2 element arrays (internal name, displayed name)
 8661: 
 8662: =cut
 8663: 
 8664: ######################################################
 8665: ######################################################
 8666: sub csv_samples_select_table {
 8667:     my ($r,$records,$d) = @_;
 8668:     my $i=0;
 8669:     #
 8670:     my $max_samples = 5;
 8671:     my $samples = &get_samples($records,$max_samples);
 8672:     $r->print(&start_data_table().
 8673:               &start_data_table_header_row().'<th>'.
 8674:               &mt('Field').'</th><th>'.&mt('Samples').'</th>'.
 8675:               &end_data_table_header_row());
 8676: 
 8677:     foreach my $key (sort(keys(%{ $samples->[0] }))) {
 8678: 	$r->print(&start_data_table_row().'<td><select name="f'.$i.'"'.
 8679: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
 8680: 	foreach my $option (@$d) {
 8681: 	    my ($value,$display,$defaultcol)=@{ $option };
 8682: 	    $r->print('<option value="'.$value.'"'.
 8683:                       ($i eq $defaultcol ? ' selected="selected" ':'').'>'.
 8684:                       $display.'</option>');
 8685: 	}
 8686: 	$r->print('</select></td><td>');
 8687: 	foreach my $line (0..($max_samples-1)) {
 8688: 	    if (defined($samples->[$line]{$key})) { 
 8689: 		$r->print($samples->[$line]{$key}."<br />\n"); 
 8690: 	    }
 8691: 	}
 8692: 	$r->print('</td>'.&end_data_table_row());
 8693: 	$i++;
 8694:     }
 8695:     $r->print(&end_data_table());
 8696:     $i--;
 8697:     return($i);
 8698: }
 8699: 
 8700: ######################################################
 8701: ######################################################
 8702: 
 8703: =pod
 8704: 
 8705: =item * &clean_excel_name($name)
 8706: 
 8707: Returns a replacement for $name which does not contain any illegal characters.
 8708: 
 8709: =cut
 8710: 
 8711: ######################################################
 8712: ######################################################
 8713: sub clean_excel_name {
 8714:     my ($name) = @_;
 8715:     $name =~ s/[:\*\?\/\\]//g;
 8716:     if (length($name) > 31) {
 8717:         $name = substr($name,0,31);
 8718:     }
 8719:     return $name;
 8720: }
 8721: 
 8722: =pod
 8723: 
 8724: =item * &check_if_partid_hidden($id,$symb,$udom,$uname)
 8725: 
 8726: Returns either 1 or undef
 8727: 
 8728: 1 if the part is to be hidden, undef if it is to be shown
 8729: 
 8730: Arguments are:
 8731: 
 8732: $id the id of the part to be checked
 8733: $symb, optional the symb of the resource to check
 8734: $udom, optional the domain of the user to check for
 8735: $uname, optional the username of the user to check for
 8736: 
 8737: =cut
 8738: 
 8739: sub check_if_partid_hidden {
 8740:     my ($id,$symb,$udom,$uname) = @_;
 8741:     my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
 8742: 					 $symb,$udom,$uname);
 8743:     my $truth=1;
 8744:     #if the string starts with !, then the list is the list to show not hide
 8745:     if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
 8746:     my @hiddenlist=split(/,/,$hiddenparts);
 8747:     foreach my $checkid (@hiddenlist) {
 8748: 	if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
 8749:     }
 8750:     return !$truth;
 8751: }
 8752: 
 8753: 
 8754: ############################################################
 8755: ############################################################
 8756: 
 8757: =pod
 8758: 
 8759: =back 
 8760: 
 8761: =head1 cgi-bin script and graphing routines
 8762: 
 8763: =over 4
 8764: 
 8765: =item * &get_cgi_id()
 8766: 
 8767: Inputs: none
 8768: 
 8769: Returns an id which can be used to pass environment variables
 8770: to various cgi-bin scripts.  These environment variables will
 8771: be removed from the users environment after a given time by
 8772: the routine &Apache::lonnet::transfer_profile_to_env.
 8773: 
 8774: =cut
 8775: 
 8776: ############################################################
 8777: ############################################################
 8778: my $uniq=0;
 8779: sub get_cgi_id {
 8780:     $uniq=($uniq+1)%100000;
 8781:     return (time.'_'.$$.'_'.$uniq);
 8782: }
 8783: 
 8784: ############################################################
 8785: ############################################################
 8786: 
 8787: =pod
 8788: 
 8789: =item * &DrawBarGraph()
 8790: 
 8791: Facilitates the plotting of data in a (stacked) bar graph.
 8792: Puts plot definition data into the users environment in order for 
 8793: graph.png to plot it.  Returns an <img> tag for the plot.
 8794: The bars on the plot are labeled '1','2',...,'n'.
 8795: 
 8796: Inputs:
 8797: 
 8798: =over 4
 8799: 
 8800: =item $Title: string, the title of the plot
 8801: 
 8802: =item $xlabel: string, text describing the X-axis of the plot
 8803: 
 8804: =item $ylabel: string, text describing the Y-axis of the plot
 8805: 
 8806: =item $Max: scalar, the maximum Y value to use in the plot
 8807: If $Max is < any data point, the graph will not be rendered.
 8808: 
 8809: =item $colors: array ref holding the colors to be used for the data sets when
 8810: they are plotted.  If undefined, default values will be used.
 8811: 
 8812: =item $labels: array ref holding the labels to use on the x-axis for the bars.
 8813: 
 8814: =item @Values: An array of array references.  Each array reference holds data
 8815: to be plotted in a stacked bar chart.
 8816: 
 8817: =item If the final element of @Values is a hash reference the key/value
 8818: pairs will be added to the graph definition.
 8819: 
 8820: =back
 8821: 
 8822: Returns:
 8823: 
 8824: An <img> tag which references graph.png and the appropriate identifying
 8825: information for the plot.
 8826: 
 8827: =cut
 8828: 
 8829: ############################################################
 8830: ############################################################
 8831: sub DrawBarGraph {
 8832:     my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
 8833:     #
 8834:     if (! defined($colors)) {
 8835:         $colors = ['#33ff00', 
 8836:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
 8837:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
 8838:                   ]; 
 8839:     }
 8840:     my $extra_settings = {};
 8841:     if (ref($Values[-1]) eq 'HASH') {
 8842:         $extra_settings = pop(@Values);
 8843:     }
 8844:     #
 8845:     my $identifier = &get_cgi_id();
 8846:     my $id = 'cgi.'.$identifier;        
 8847:     if (! @Values || ref($Values[0]) ne 'ARRAY') {
 8848:         return '';
 8849:     }
 8850:     #
 8851:     my @Labels;
 8852:     if (defined($labels)) {
 8853:         @Labels = @$labels;
 8854:     } else {
 8855:         for (my $i=0;$i<@{$Values[0]};$i++) {
 8856:             push (@Labels,$i+1);
 8857:         }
 8858:     }
 8859:     #
 8860:     my $NumBars = scalar(@{$Values[0]});
 8861:     if ($NumBars < scalar(@Labels)) { $NumBars = scalar(@Labels); }
 8862:     my %ValuesHash;
 8863:     my $NumSets=1;
 8864:     foreach my $array (@Values) {
 8865:         next if (! ref($array));
 8866:         $ValuesHash{$id.'.data.'.$NumSets++} = 
 8867:             join(',',@$array);
 8868:     }
 8869:     #
 8870:     my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
 8871:     if ($NumBars < 3) {
 8872:         $width = 120+$NumBars*32;
 8873:         $xskip = 1;
 8874:         $bar_width = 30;
 8875:     } elsif ($NumBars < 5) {
 8876:         $width = 120+$NumBars*20;
 8877:         $xskip = 1;
 8878:         $bar_width = 20;
 8879:     } elsif ($NumBars < 10) {
 8880:         $width = 120+$NumBars*15;
 8881:         $xskip = 1;
 8882:         $bar_width = 15;
 8883:     } elsif ($NumBars <= 25) {
 8884:         $width = 120+$NumBars*11;
 8885:         $xskip = 5;
 8886:         $bar_width = 8;
 8887:     } elsif ($NumBars <= 50) {
 8888:         $width = 120+$NumBars*8;
 8889:         $xskip = 5;
 8890:         $bar_width = 4;
 8891:     } else {
 8892:         $width = 120+$NumBars*8;
 8893:         $xskip = 5;
 8894:         $bar_width = 4;
 8895:     }
 8896:     #
 8897:     $Max = 1 if ($Max < 1);
 8898:     if ( int($Max) < $Max ) {
 8899:         $Max++;
 8900:         $Max = int($Max);
 8901:     }
 8902:     $Title  = '' if (! defined($Title));
 8903:     $xlabel = '' if (! defined($xlabel));
 8904:     $ylabel = '' if (! defined($ylabel));
 8905:     $ValuesHash{$id.'.title'}    = &escape($Title);
 8906:     $ValuesHash{$id.'.xlabel'}   = &escape($xlabel);
 8907:     $ValuesHash{$id.'.ylabel'}   = &escape($ylabel);
 8908:     $ValuesHash{$id.'.y_max_value'} = $Max;
 8909:     $ValuesHash{$id.'.NumBars'}  = $NumBars;
 8910:     $ValuesHash{$id.'.NumSets'}  = $NumSets;
 8911:     $ValuesHash{$id.'.PlotType'} = 'bar';
 8912:     $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
 8913:     $ValuesHash{$id.'.height'}   = $height;
 8914:     $ValuesHash{$id.'.width'}    = $width;
 8915:     $ValuesHash{$id.'.xskip'}    = $xskip;
 8916:     $ValuesHash{$id.'.bar_width'} = $bar_width;
 8917:     $ValuesHash{$id.'.labels'} = join(',',@Labels);
 8918:     #
 8919:     # Deal with other parameters
 8920:     while (my ($key,$value) = each(%$extra_settings)) {
 8921:         $ValuesHash{$id.'.'.$key} = $value;
 8922:     }
 8923:     #
 8924:     &Apache::lonnet::appenv(\%ValuesHash);
 8925:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
 8926: }
 8927: 
 8928: ############################################################
 8929: ############################################################
 8930: 
 8931: =pod
 8932: 
 8933: =item * &DrawXYGraph()
 8934: 
 8935: Facilitates the plotting of data in an XY graph.
 8936: Puts plot definition data into the users environment in order for 
 8937: graph.png to plot it.  Returns an <img> tag for the plot.
 8938: 
 8939: Inputs:
 8940: 
 8941: =over 4
 8942: 
 8943: =item $Title: string, the title of the plot
 8944: 
 8945: =item $xlabel: string, text describing the X-axis of the plot
 8946: 
 8947: =item $ylabel: string, text describing the Y-axis of the plot
 8948: 
 8949: =item $Max: scalar, the maximum Y value to use in the plot
 8950: If $Max is < any data point, the graph will not be rendered.
 8951: 
 8952: =item $colors: Array ref containing the hex color codes for the data to be 
 8953: plotted in.  If undefined, default values will be used.
 8954: 
 8955: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
 8956: 
 8957: =item $Ydata: Array ref containing Array refs.  
 8958: Each of the contained arrays will be plotted as a separate curve.
 8959: 
 8960: =item %Values: hash indicating or overriding any default values which are 
 8961: passed to graph.png.  
 8962: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
 8963: 
 8964: =back
 8965: 
 8966: Returns:
 8967: 
 8968: An <img> tag which references graph.png and the appropriate identifying
 8969: information for the plot.
 8970: 
 8971: =cut
 8972: 
 8973: ############################################################
 8974: ############################################################
 8975: sub DrawXYGraph {
 8976:     my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
 8977:     #
 8978:     # Create the identifier for the graph
 8979:     my $identifier = &get_cgi_id();
 8980:     my $id = 'cgi.'.$identifier;
 8981:     #
 8982:     $Title  = '' if (! defined($Title));
 8983:     $xlabel = '' if (! defined($xlabel));
 8984:     $ylabel = '' if (! defined($ylabel));
 8985:     my %ValuesHash = 
 8986:         (
 8987:          $id.'.title'  => &escape($Title),
 8988:          $id.'.xlabel' => &escape($xlabel),
 8989:          $id.'.ylabel' => &escape($ylabel),
 8990:          $id.'.y_max_value'=> $Max,
 8991:          $id.'.labels'     => join(',',@$Xlabels),
 8992:          $id.'.PlotType'   => 'XY',
 8993:          );
 8994:     #
 8995:     if (defined($colors) && ref($colors) eq 'ARRAY') {
 8996:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
 8997:     }
 8998:     #
 8999:     if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
 9000:         return '';
 9001:     }
 9002:     my $NumSets=1;
 9003:     foreach my $array (@{$Ydata}){
 9004:         next if (! ref($array));
 9005:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
 9006:     }
 9007:     $ValuesHash{$id.'.NumSets'} = $NumSets-1;
 9008:     #
 9009:     # Deal with other parameters
 9010:     while (my ($key,$value) = each(%Values)) {
 9011:         $ValuesHash{$id.'.'.$key} = $value;
 9012:     }
 9013:     #
 9014:     &Apache::lonnet::appenv(\%ValuesHash);
 9015:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
 9016: }
 9017: 
 9018: ############################################################
 9019: ############################################################
 9020: 
 9021: =pod
 9022: 
 9023: =item * &DrawXYYGraph()
 9024: 
 9025: Facilitates the plotting of data in an XY graph with two Y axes.
 9026: Puts plot definition data into the users environment in order for 
 9027: graph.png to plot it.  Returns an <img> tag for the plot.
 9028: 
 9029: Inputs:
 9030: 
 9031: =over 4
 9032: 
 9033: =item $Title: string, the title of the plot
 9034: 
 9035: =item $xlabel: string, text describing the X-axis of the plot
 9036: 
 9037: =item $ylabel: string, text describing the Y-axis of the plot
 9038: 
 9039: =item $colors: Array ref containing the hex color codes for the data to be 
 9040: plotted in.  If undefined, default values will be used.
 9041: 
 9042: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
 9043: 
 9044: =item $Ydata1: The first data set
 9045: 
 9046: =item $Min1: The minimum value of the left Y-axis
 9047: 
 9048: =item $Max1: The maximum value of the left Y-axis
 9049: 
 9050: =item $Ydata2: The second data set
 9051: 
 9052: =item $Min2: The minimum value of the right Y-axis
 9053: 
 9054: =item $Max2: The maximum value of the left Y-axis
 9055: 
 9056: =item %Values: hash indicating or overriding any default values which are 
 9057: passed to graph.png.  
 9058: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
 9059: 
 9060: =back
 9061: 
 9062: Returns:
 9063: 
 9064: An <img> tag which references graph.png and the appropriate identifying
 9065: information for the plot.
 9066: 
 9067: =cut
 9068: 
 9069: ############################################################
 9070: ############################################################
 9071: sub DrawXYYGraph {
 9072:     my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
 9073:                                         $Ydata2,$Min2,$Max2,%Values)=@_;
 9074:     #
 9075:     # Create the identifier for the graph
 9076:     my $identifier = &get_cgi_id();
 9077:     my $id = 'cgi.'.$identifier;
 9078:     #
 9079:     $Title  = '' if (! defined($Title));
 9080:     $xlabel = '' if (! defined($xlabel));
 9081:     $ylabel = '' if (! defined($ylabel));
 9082:     my %ValuesHash = 
 9083:         (
 9084:          $id.'.title'  => &escape($Title),
 9085:          $id.'.xlabel' => &escape($xlabel),
 9086:          $id.'.ylabel' => &escape($ylabel),
 9087:          $id.'.labels' => join(',',@$Xlabels),
 9088:          $id.'.PlotType' => 'XY',
 9089:          $id.'.NumSets' => 2,
 9090:          $id.'.two_axes' => 1,
 9091:          $id.'.y1_max_value' => $Max1,
 9092:          $id.'.y1_min_value' => $Min1,
 9093:          $id.'.y2_max_value' => $Max2,
 9094:          $id.'.y2_min_value' => $Min2,
 9095:          );
 9096:     #
 9097:     if (defined($colors) && ref($colors) eq 'ARRAY') {
 9098:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
 9099:     }
 9100:     #
 9101:     if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
 9102:         ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
 9103:         return '';
 9104:     }
 9105:     my $NumSets=1;
 9106:     foreach my $array ($Ydata1,$Ydata2){
 9107:         next if (! ref($array));
 9108:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
 9109:     }
 9110:     #
 9111:     # Deal with other parameters
 9112:     while (my ($key,$value) = each(%Values)) {
 9113:         $ValuesHash{$id.'.'.$key} = $value;
 9114:     }
 9115:     #
 9116:     &Apache::lonnet::appenv(\%ValuesHash);
 9117:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
 9118: }
 9119: 
 9120: ############################################################
 9121: ############################################################
 9122: 
 9123: =pod
 9124: 
 9125: =back 
 9126: 
 9127: =head1 Statistics helper routines?  
 9128: 
 9129: Bad place for them but what the hell.
 9130: 
 9131: =over 4
 9132: 
 9133: =item * &chartlink()
 9134: 
 9135: Returns a link to the chart for a specific student.  
 9136: 
 9137: Inputs:
 9138: 
 9139: =over 4
 9140: 
 9141: =item $linktext: The text of the link
 9142: 
 9143: =item $sname: The students username
 9144: 
 9145: =item $sdomain: The students domain
 9146: 
 9147: =back
 9148: 
 9149: =back
 9150: 
 9151: =cut
 9152: 
 9153: ############################################################
 9154: ############################################################
 9155: sub chartlink {
 9156:     my ($linktext, $sname, $sdomain) = @_;
 9157:     my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
 9158:         '&amp;SelectedStudent='.&escape($sname.':'.$sdomain).
 9159:         '&amp;chartoutputmode='.HTML::Entities::encode('html, with all links').
 9160:        '">'.$linktext.'</a>';
 9161: }
 9162: 
 9163: #######################################################
 9164: #######################################################
 9165: 
 9166: =pod
 9167: 
 9168: =head1 Course Environment Routines
 9169: 
 9170: =over 4
 9171: 
 9172: =item * &restore_course_settings()
 9173: 
 9174: =item * &store_course_settings()
 9175: 
 9176: Restores/Store indicated form parameters from the course environment.
 9177: Will not overwrite existing values of the form parameters.
 9178: 
 9179: Inputs: 
 9180: a scalar describing the data (e.g. 'chart', 'problem_analysis')
 9181: 
 9182: a hash ref describing the data to be stored.  For example:
 9183:    
 9184: %Save_Parameters = ('Status' => 'scalar',
 9185:     'chartoutputmode' => 'scalar',
 9186:     'chartoutputdata' => 'scalar',
 9187:     'Section' => 'array',
 9188:     'Group' => 'array',
 9189:     'StudentData' => 'array',
 9190:     'Maps' => 'array');
 9191: 
 9192: Returns: both routines return nothing
 9193: 
 9194: =back
 9195: 
 9196: =cut
 9197: 
 9198: #######################################################
 9199: #######################################################
 9200: sub store_course_settings {
 9201:     return &store_settings($env{'request.course.id'},@_);
 9202: }
 9203: 
 9204: sub store_settings {
 9205:     # save to the environment
 9206:     # appenv the same items, just to be safe
 9207:     my $udom  = $env{'user.domain'};
 9208:     my $uname = $env{'user.name'};
 9209:     my ($context,$prefix,$Settings) = @_;
 9210:     my %SaveHash;
 9211:     my %AppHash;
 9212:     while (my ($setting,$type) = each(%$Settings)) {
 9213:         my $basename = join('.','internal',$context,$prefix,$setting);
 9214:         my $envname = 'environment.'.$basename;
 9215:         if (exists($env{'form.'.$setting})) {
 9216:             # Save this value away
 9217:             if ($type eq 'scalar' &&
 9218:                 (! exists($env{$envname}) || 
 9219:                  $env{$envname} ne $env{'form.'.$setting})) {
 9220:                 $SaveHash{$basename} = $env{'form.'.$setting};
 9221:                 $AppHash{$envname}   = $env{'form.'.$setting};
 9222:             } elsif ($type eq 'array') {
 9223:                 my $stored_form;
 9224:                 if (ref($env{'form.'.$setting})) {
 9225:                     $stored_form = join(',',
 9226:                                         map {
 9227:                                             &escape($_);
 9228:                                         } sort(@{$env{'form.'.$setting}}));
 9229:                 } else {
 9230:                     $stored_form = 
 9231:                         &escape($env{'form.'.$setting});
 9232:                 }
 9233:                 # Determine if the array contents are the same.
 9234:                 if ($stored_form ne $env{$envname}) {
 9235:                     $SaveHash{$basename} = $stored_form;
 9236:                     $AppHash{$envname}   = $stored_form;
 9237:                 }
 9238:             }
 9239:         }
 9240:     }
 9241:     my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
 9242:                                           $udom,$uname);
 9243:     if ($put_result !~ /^(ok|delayed)/) {
 9244:         &Apache::lonnet::logthis('unable to save form parameters, '.
 9245:                                  'got error:'.$put_result);
 9246:     }
 9247:     # Make sure these settings stick around in this session, too
 9248:     &Apache::lonnet::appenv(\%AppHash);
 9249:     return;
 9250: }
 9251: 
 9252: sub restore_course_settings {
 9253:     return &restore_settings($env{'request.course.id'},@_);
 9254: }
 9255: 
 9256: sub restore_settings {
 9257:     my ($context,$prefix,$Settings) = @_;
 9258:     while (my ($setting,$type) = each(%$Settings)) {
 9259:         next if (exists($env{'form.'.$setting}));
 9260:         my $envname = 'environment.internal.'.$context.'.'.$prefix.
 9261:             '.'.$setting;
 9262:         if (exists($env{$envname})) {
 9263:             if ($type eq 'scalar') {
 9264:                 $env{'form.'.$setting} = $env{$envname};
 9265:             } elsif ($type eq 'array') {
 9266:                 $env{'form.'.$setting} = [ 
 9267:                                            map { 
 9268:                                                &unescape($_); 
 9269:                                            } split(',',$env{$envname})
 9270:                                            ];
 9271:             }
 9272:         }
 9273:     }
 9274: }
 9275: 
 9276: #######################################################
 9277: #######################################################
 9278: 
 9279: =pod
 9280: 
 9281: =head1 Domain E-mail Routines  
 9282: 
 9283: =over 4
 9284: 
 9285: =item * &build_recipient_list()
 9286: 
 9287: Build recipient lists for four types of e-mail:
 9288: (a) Error Reports, (b) Package Updates, (c) lonstatus warnings/errors
 9289: (d) Help requests, generated by
 9290: lonerrorhandler.pm, CHECKRPMS, loncron, and lonsupportreq.pm respectively.
 9291: 
 9292: Inputs:
 9293: defmail (scalar - email address of default recipient), 
 9294: mailing type (scalar - errormail, packagesmail, or helpdeskmail), 
 9295: defdom (domain for which to retrieve configuration settings),
 9296: origmail (scalar - email address of recipient from loncapa.conf, 
 9297: i.e., predates configuration by DC via domainprefs.pm 
 9298: 
 9299: Returns: comma separated list of addresses to which to send e-mail.
 9300: 
 9301: =back
 9302: 
 9303: =cut
 9304: 
 9305: ############################################################
 9306: ############################################################
 9307: sub build_recipient_list {
 9308:     my ($defmail,$mailing,$defdom,$origmail) = @_;
 9309:     my @recipients;
 9310:     my $otheremails;
 9311:     my %domconfig =
 9312:          &Apache::lonnet::get_dom('configuration',['contacts'],$defdom);
 9313:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 9314:         if (exists($domconfig{'contacts'}{$mailing})) {
 9315:             if (ref($domconfig{'contacts'}{$mailing}) eq 'HASH') {
 9316:                 my @contacts = ('adminemail','supportemail');
 9317:                 foreach my $item (@contacts) {
 9318:                     if ($domconfig{'contacts'}{$mailing}{$item}) {
 9319:                         my $addr = $domconfig{'contacts'}{$item}; 
 9320:                         if (!grep(/^\Q$addr\E$/,@recipients)) {
 9321:                             push(@recipients,$addr);
 9322:                         }
 9323:                     }
 9324:                     $otheremails = $domconfig{'contacts'}{$mailing}{'others'};
 9325:                 }
 9326:             }
 9327:         } elsif ($origmail ne '') {
 9328:             push(@recipients,$origmail);
 9329:         }
 9330:     } elsif ($origmail ne '') {
 9331:         push(@recipients,$origmail);
 9332:     }
 9333:     if (defined($defmail)) {
 9334:         if ($defmail ne '') {
 9335:             push(@recipients,$defmail);
 9336:         }
 9337:     }
 9338:     if ($otheremails) {
 9339:         my @others;
 9340:         if ($otheremails =~ /,/) {
 9341:             @others = split(/,/,$otheremails);
 9342:         } else {
 9343:             push(@others,$otheremails);
 9344:         }
 9345:         foreach my $addr (@others) {
 9346:             if (!grep(/^\Q$addr\E$/,@recipients)) {
 9347:                 push(@recipients,$addr);
 9348:             }
 9349:         }
 9350:     }
 9351:     my $recipientlist = join(',',@recipients); 
 9352:     return $recipientlist;
 9353: }
 9354: 
 9355: ############################################################
 9356: ############################################################
 9357: 
 9358: =pod
 9359: 
 9360: =head1 Course Catalog Routines
 9361: 
 9362: =over 4
 9363: 
 9364: =item * &gather_categories()
 9365: 
 9366: Converts category definitions - keys of categories hash stored in  
 9367: coursecategories in configuration.db on the primary library server in a 
 9368: domain - to an array.  Also generates javascript and idx hash used to 
 9369: generate Domain Coordinator interface for editing Course Categories.
 9370: 
 9371: Inputs:
 9372: 
 9373: categories (reference to hash of category definitions).
 9374: 
 9375: cats (reference to array of arrays/hashes which encapsulates hierarchy of
 9376:       categories and subcategories).
 9377: 
 9378: idx (reference to hash of counters used in Domain Coordinator interface for 
 9379:       editing Course Categories).
 9380: 
 9381: jsarray (reference to array of categories used to create Javascript arrays for
 9382:          Domain Coordinator interface for editing Course Categories).
 9383: 
 9384: Returns: nothing
 9385: 
 9386: Side effects: populates cats, idx and jsarray. 
 9387: 
 9388: =cut
 9389: 
 9390: sub gather_categories {
 9391:     my ($categories,$cats,$idx,$jsarray) = @_;
 9392:     my %counters;
 9393:     my $num = 0;
 9394:     foreach my $item (keys(%{$categories})) {
 9395:         my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9396:         if ($container eq '' && $depth == 0) {
 9397:             $cats->[$depth][$categories->{$item}] = $cat;
 9398:         } else {
 9399:             $cats->[$depth]{$container}[$categories->{$item}] = $cat;
 9400:         }
 9401:         my ($escitem,$tail) = split(/:/,$item,2);
 9402:         if ($counters{$tail} eq '') {
 9403:             $counters{$tail} = $num;
 9404:             $num ++;
 9405:         }
 9406:         if (ref($idx) eq 'HASH') {
 9407:             $idx->{$item} = $counters{$tail};
 9408:         }
 9409:         if (ref($jsarray) eq 'ARRAY') {
 9410:             push(@{$jsarray->[$counters{$tail}]},$item);
 9411:         }
 9412:     }
 9413:     return;
 9414: }
 9415: 
 9416: =pod
 9417: 
 9418: =item * &extract_categories()
 9419: 
 9420: Used to generate breadcrumb trails for course categories.
 9421: 
 9422: Inputs:
 9423: 
 9424: categories (reference to hash of category definitions).
 9425: 
 9426: cats (reference to array of arrays/hashes which encapsulates hierarchy of
 9427:       categories and subcategories).
 9428: 
 9429: trails (reference to array of breacrumb trails for each category).
 9430: 
 9431: allitems (reference to hash - key is category key 
 9432:          (format: escaped(name):escaped(parent category):depth in hierarchy).
 9433: 
 9434: idx (reference to hash of counters used in Domain Coordinator interface for
 9435:       editing Course Categories).
 9436: 
 9437: jsarray (reference to array of categories used to create Javascript arrays for
 9438:          Domain Coordinator interface for editing Course Categories).
 9439: 
 9440: subcats (reference to hash of arrays containing all subcategories within each 
 9441:          category, -recursive)
 9442: 
 9443: Returns: nothing
 9444: 
 9445: Side effects: populates trails and allitems hash references.
 9446: 
 9447: =cut
 9448: 
 9449: sub extract_categories {
 9450:     my ($categories,$cats,$trails,$allitems,$idx,$jsarray,$subcats) = @_;
 9451:     if (ref($categories) eq 'HASH') {
 9452:         &gather_categories($categories,$cats,$idx,$jsarray);
 9453:         if (ref($cats->[0]) eq 'ARRAY') {
 9454:             for (my $i=0; $i<@{$cats->[0]}; $i++) {
 9455:                 my $name = $cats->[0][$i];
 9456:                 my $item = &escape($name).'::0';
 9457:                 my $trailstr;
 9458:                 if ($name eq 'instcode') {
 9459:                     $trailstr = &mt('Official courses (with institutional codes)');
 9460:                 } else {
 9461:                     $trailstr = $name;
 9462:                 }
 9463:                 if ($allitems->{$item} eq '') {
 9464:                     push(@{$trails},$trailstr);
 9465:                     $allitems->{$item} = scalar(@{$trails})-1;
 9466:                 }
 9467:                 my @parents = ($name);
 9468:                 if (ref($cats->[1]{$name}) eq 'ARRAY') {
 9469:                     for (my $j=0; $j<@{$cats->[1]{$name}}; $j++) {
 9470:                         my $category = $cats->[1]{$name}[$j];
 9471:                         if (ref($subcats) eq 'HASH') {
 9472:                             push(@{$subcats->{$item}},&escape($category).':'.&escape($name).':1');
 9473:                         }
 9474:                         &recurse_categories($cats,2,$category,$trails,$allitems,\@parents,$subcats);
 9475:                     }
 9476:                 } else {
 9477:                     if (ref($subcats) eq 'HASH') {
 9478:                         $subcats->{$item} = [];
 9479:                     }
 9480:                 }
 9481:             }
 9482:         }
 9483:     }
 9484:     return;
 9485: }
 9486: 
 9487: =pod
 9488: 
 9489: =item *&recurse_categories()
 9490: 
 9491: Recursively used to generate breadcrumb trails for course categories.
 9492: 
 9493: Inputs:
 9494: 
 9495: cats (reference to array of arrays/hashes which encapsulates hierarchy of
 9496:       categories and subcategories).
 9497: 
 9498: depth (current depth in hierarchy of categories and sub-categories - 0 indexed).
 9499: 
 9500: category (current course category, for which breadcrumb trail is being generated).
 9501: 
 9502: trails (reference to array of breadcrumb trails for each category).
 9503: 
 9504: allitems (reference to hash - key is category key
 9505:          (format: escaped(name):escaped(parent category):depth in hierarchy).
 9506: 
 9507: parents (array containing containers directories for current category, 
 9508:          back to top level). 
 9509: 
 9510: Returns: nothing
 9511: 
 9512: Side effects: populates trails and allitems hash references
 9513: 
 9514: =cut
 9515: 
 9516: sub recurse_categories {
 9517:     my ($cats,$depth,$category,$trails,$allitems,$parents,$subcats) = @_;
 9518:     my $shallower = $depth - 1;
 9519:     if (ref($cats->[$depth]{$category}) eq 'ARRAY') {
 9520:         for (my $k=0; $k<@{$cats->[$depth]{$category}}; $k++) {
 9521:             my $name = $cats->[$depth]{$category}[$k];
 9522:             my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
 9523:             my $trailstr = join(' -&gt; ',(@{$parents},$category));
 9524:             if ($allitems->{$item} eq '') {
 9525:                 push(@{$trails},$trailstr);
 9526:                 $allitems->{$item} = scalar(@{$trails})-1;
 9527:             }
 9528:             my $deeper = $depth+1;
 9529:             push(@{$parents},$category);
 9530:             if (ref($subcats) eq 'HASH') {
 9531:                 my $subcat = &escape($name).':'.$category.':'.$depth;
 9532:                 for (my $j=@{$parents}; $j>=0; $j--) {
 9533:                     my $higher;
 9534:                     if ($j > 0) {
 9535:                         $higher = &escape($parents->[$j]).':'.
 9536:                                   &escape($parents->[$j-1]).':'.$j;
 9537:                     } else {
 9538:                         $higher = &escape($parents->[$j]).'::'.$j;
 9539:                     }
 9540:                     push(@{$subcats->{$higher}},$subcat);
 9541:                 }
 9542:             }
 9543:             &recurse_categories($cats,$deeper,$name,$trails,$allitems,$parents,
 9544:                                 $subcats);
 9545:             pop(@{$parents});
 9546:         }
 9547:     } else {
 9548:         my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
 9549:         my $trailstr = join(' -&gt; ',(@{$parents},$category));
 9550:         if ($allitems->{$item} eq '') {
 9551:             push(@{$trails},$trailstr);
 9552:             $allitems->{$item} = scalar(@{$trails})-1;
 9553:         }
 9554:     }
 9555:     return;
 9556: }
 9557: 
 9558: =pod
 9559: 
 9560: =item *&assign_categories_table()
 9561: 
 9562: Create a datatable for display of hierarchical categories in a domain,
 9563: with checkboxes to allow a course to be categorized. 
 9564: 
 9565: Inputs:
 9566: 
 9567: cathash - reference to hash of categories defined for the domain (from
 9568:           configuration.db)
 9569: 
 9570: currcat - scalar with an & separated list of categories assigned to a course. 
 9571: 
 9572: Returns: $output (markup to be displayed) 
 9573: 
 9574: =cut
 9575: 
 9576: sub assign_categories_table {
 9577:     my ($cathash,$currcat) = @_;
 9578:     my $output;
 9579:     if (ref($cathash) eq 'HASH') {
 9580:         my (@cats,@trails,%allitems,%idx,@jsarray,@path,$maxdepth);
 9581:         &extract_categories($cathash,\@cats,\@trails,\%allitems,\%idx,\@jsarray);
 9582:         $maxdepth = scalar(@cats);
 9583:         if (@cats > 0) {
 9584:             my $itemcount = 0;
 9585:             if (ref($cats[0]) eq 'ARRAY') {
 9586:                 $output = &Apache::loncommon::start_data_table();
 9587:                 my @currcategories;
 9588:                 if ($currcat ne '') {
 9589:                     @currcategories = split('&',$currcat);
 9590:                 }
 9591:                 for (my $i=0; $i<@{$cats[0]}; $i++) {
 9592:                     my $parent = $cats[0][$i];
 9593:                     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9594:                     next if ($parent eq 'instcode');
 9595:                     my $item = &escape($parent).'::0';
 9596:                     my $checked = '';
 9597:                     if (@currcategories > 0) {
 9598:                         if (grep(/^\Q$item\E$/,@currcategories)) {
 9599:                             $checked = ' checked="checked"';
 9600:                         }
 9601:                     }
 9602:                     $output .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.
 9603:                                '<input type="checkbox" name="usecategory" value="'.
 9604:                                $item.'"'.$checked.' />'.$parent.'</span>'.
 9605:                                '<input type="hidden" name="catname" value="'.$parent.'" /></td>';
 9606:                     my $depth = 1;
 9607:                     push(@path,$parent);
 9608:                     $output .= &assign_category_rows($itemcount,\@cats,$depth,$parent,\@path,\@currcategories);
 9609:                     pop(@path);
 9610:                     $output .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 9611:                     $itemcount ++;
 9612:                 }
 9613:                 $output .= &Apache::loncommon::end_data_table();
 9614:             }
 9615:         }
 9616:     }
 9617:     return $output;
 9618: }
 9619: 
 9620: =pod
 9621: 
 9622: =item *&assign_category_rows()
 9623: 
 9624: Create a datatable row for display of nested categories in a domain,
 9625: with checkboxes to allow a course to be categorized,called recursively.
 9626: 
 9627: Inputs:
 9628: 
 9629: itemcount - track row number for alternating colors
 9630: 
 9631: cats - reference to array of arrays/hashes which encapsulates hierarchy of
 9632:       categories and subcategories.
 9633: 
 9634: depth - current depth in hierarchy of categories and sub-categories - 0 indexed.
 9635: 
 9636: parent - parent of current category item
 9637: 
 9638: path - Array containing all categories back up through the hierarchy from the
 9639:        current category to the top level.
 9640: 
 9641: currcategories - reference to array of current categories assigned to the course
 9642: 
 9643: Returns: $output (markup to be displayed).
 9644: 
 9645: =cut
 9646: 
 9647: sub assign_category_rows {
 9648:     my ($itemcount,$cats,$depth,$parent,$path,$currcategories) = @_;
 9649:     my ($text,$name,$item,$chgstr);
 9650:     if (ref($cats) eq 'ARRAY') {
 9651:         my $maxdepth = scalar(@{$cats});
 9652:         if (ref($cats->[$depth]) eq 'HASH') {
 9653:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 9654:                 my $numchildren = @{$cats->[$depth]{$parent}};
 9655:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9656:                 $text .= '<td><table class="LC_datatable">';
 9657:                 for (my $j=0; $j<$numchildren; $j++) {
 9658:                     $name = $cats->[$depth]{$parent}[$j];
 9659:                     $item = &escape($name).':'.&escape($parent).':'.$depth;
 9660:                     my $deeper = $depth+1;
 9661:                     my $checked = '';
 9662:                     if (ref($currcategories) eq 'ARRAY') {
 9663:                         if (@{$currcategories} > 0) {
 9664:                             if (grep(/^\Q$item\E$/,@{$currcategories})) {
 9665:                                 $checked = ' checked="checked"';
 9666:                             }
 9667:                         }
 9668:                     }
 9669:                     $text .= '<tr><td><span class="LC_nobreak"><label>'.
 9670:                              '<input type="checkbox" name="usecategory" value="'.
 9671:                              $item.'"'.$checked.' />'.$name.'</label></span>'.
 9672:                              '<input type="hidden" name="catname" value="'.$name.'" />'.
 9673:                              '</td><td>';
 9674:                     if (ref($path) eq 'ARRAY') {
 9675:                         push(@{$path},$name);
 9676:                         $text .= &assign_category_rows($itemcount,$cats,$deeper,$name,$path,$currcategories);
 9677:                         pop(@{$path});
 9678:                     }
 9679:                     $text .= '</td></tr>';
 9680:                 }
 9681:                 $text .= '</table></td>';
 9682:             }
 9683:         }
 9684:     }
 9685:     return $text;
 9686: }
 9687: 
 9688: ############################################################
 9689: ############################################################
 9690: 
 9691: 
 9692: sub commit_customrole {
 9693:     my ($udom,$uname,$url,$three,$four,$five,$start,$end,$context) = @_;
 9694:     my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.':'.$three.' in '.$url.
 9695:                          ($start?', '.&mt('starting').' '.localtime($start):'').
 9696:                          ($end?', ending '.localtime($end):'').': <b>'.
 9697:               &Apache::lonnet::assigncustomrole(
 9698:                  $udom,$uname,$url,$three,$four,$five,$end,$start,undef,undef,$context).
 9699:                  '</b><br />';
 9700:     return $output;
 9701: }
 9702: 
 9703: sub commit_standardrole {
 9704:     my ($udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
 9705:     my ($output,$logmsg,$linefeed);
 9706:     if ($context eq 'auto') {
 9707:         $linefeed = "\n";
 9708:     } else {
 9709:         $linefeed = "<br />\n";
 9710:     }  
 9711:     if ($three eq 'st') {
 9712:         my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$three,$start,$end,
 9713:                                          $one,$two,$sec,$context);
 9714:         if (($result =~ /^error/) || ($result eq 'not_in_class') || 
 9715:             ($result eq 'unknown_course') || ($result eq 'refused')) {
 9716:             $output = $logmsg.' '.&mt('Error: ').$result."\n"; 
 9717:         } else {
 9718:             $output = $logmsg.$linefeed.&mt('Assigning').' '.$three.' in '.$url.
 9719:                ($start?', '.&mt('starting').' '.localtime($start):'').
 9720:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
 9721:             if ($context eq 'auto') {
 9722:                 $output .= $result.$linefeed.&mt('Add to classlist').': ok';
 9723:             } else {
 9724:                $output .= '<b>'.$result.'</b>'.$linefeed.
 9725:                &mt('Add to classlist').': <b>ok</b>';
 9726:             }
 9727:             $output .= $linefeed;
 9728:         }
 9729:     } else {
 9730:         $output = &mt('Assigning').' '.$three.' in '.$url.
 9731:                ($start?', '.&mt('starting').' '.localtime($start):'').
 9732:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
 9733:         my $result = &Apache::lonnet::assignrole($udom,$uname,$url,$three,$end,$start,'','',$context);
 9734:         if ($context eq 'auto') {
 9735:             $output .= $result.$linefeed;
 9736:         } else {
 9737:             $output .= '<b>'.$result.'</b>'.$linefeed;
 9738:         }
 9739:     }
 9740:     return $output;
 9741: }
 9742: 
 9743: sub commit_studentrole {
 9744:     my ($logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
 9745:     my ($result,$linefeed,$oldsecurl,$newsecurl);
 9746:     if ($context eq 'auto') {
 9747:         $linefeed = "\n";
 9748:     } else {
 9749:         $linefeed = '<br />'."\n";
 9750:     }
 9751:     if (defined($one) && defined($two)) {
 9752:         my $cid=$one.'_'.$two;
 9753:         my $oldsec=&Apache::lonnet::getsection($udom,$uname,$cid);
 9754:         my $secchange = 0;
 9755:         my $expire_role_result;
 9756:         my $modify_section_result;
 9757:         if ($oldsec ne '-1') { 
 9758:             if ($oldsec ne $sec) {
 9759:                 $secchange = 1;
 9760:                 my $now = time;
 9761:                 my $uurl='/'.$cid;
 9762:                 $uurl=~s/\_/\//g;
 9763:                 if ($oldsec) {
 9764:                     $uurl.='/'.$oldsec;
 9765:                 }
 9766:                 $oldsecurl = $uurl;
 9767:                 $expire_role_result = 
 9768:                     &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',$now,'','',$context);
 9769:                 if ($env{'request.course.sec'} ne '') { 
 9770:                     if ($expire_role_result eq 'refused') {
 9771:                         my @roles = ('st');
 9772:                         my @statuses = ('previous');
 9773:                         my @roledoms = ($one);
 9774:                         my $withsec = 1;
 9775:                         my %roleshash = 
 9776:                             &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
 9777:                                               \@statuses,\@roles,\@roledoms,$withsec);
 9778:                         if (defined ($roleshash{$two.':'.$one.':st:'.$oldsec})) {
 9779:                             my ($oldstart,$oldend) = 
 9780:                                 split(':',$roleshash{$two.':'.$one.':st:'.$oldsec});
 9781:                             if ($oldend > 0 && $oldend <= $now) {
 9782:                                 $expire_role_result = 'ok';
 9783:                             }
 9784:                         }
 9785:                     }
 9786:                 }
 9787:                 $result = $expire_role_result;
 9788:             }
 9789:         }
 9790:         if (($expire_role_result eq 'ok') || ($secchange == 0)) {
 9791:             $modify_section_result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,'','',$cid,'',$context);
 9792:             if ($modify_section_result =~ /^ok/) {
 9793:                 if ($secchange == 1) {
 9794:                     if ($sec eq '') {
 9795:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to student role without a section.',$uname,$oldsec).$linefeed;
 9796:                     } else {
 9797:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to new section: [_3].',$uname,$oldsec,$sec).$linefeed;
 9798:                     }
 9799:                 } elsif ($oldsec eq '-1') {
 9800:                     if ($sec eq '') {
 9801:                         $$logmsg .= &mt('New student role without a section for [_1] in course [_2].',$uname,$cid).$linefeed;
 9802:                     } else {
 9803:                         $$logmsg .= &mt('New student role for [_1] in section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
 9804:                     }
 9805:                 } else {
 9806:                     if ($sec eq '') {
 9807:                         $$logmsg .= &mt('Student [_1] assigned to course [_2] without a section.',$uname,$cid).$linefeed;
 9808:                     } else {
 9809:                         $$logmsg .= &mt('Student [_1] assigned to section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
 9810:                     }
 9811:                 }
 9812:             } else {
 9813:                 if ($secchange) {       
 9814:                     $$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;
 9815:                 } else {
 9816:                     $$logmsg .= &mt('Error when attempting to modify role for [_1] for section: "[_2]" in course [_3] -error:',$uname,$sec,$cid).' '.$modify_section_result.$linefeed;
 9817:                 }
 9818:             }
 9819:             $result = $modify_section_result;
 9820:         } elsif ($secchange == 1) {
 9821:             if ($oldsec eq '') {
 9822:                 $$logmsg .= &mt('Error when attempting to expire existing role without a section for [_1] in course [_3] -error: ',$uname,$cid).' '.$expire_role_result.$linefeed;
 9823:             } else {
 9824:                 $$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;
 9825:             }
 9826:             if ($expire_role_result eq 'refused') {
 9827:                 my $newsecurl = '/'.$cid;
 9828:                 $newsecurl =~ s/\_/\//g;
 9829:                 if ($sec ne '') {
 9830:                     $newsecurl.='/'.$sec;
 9831:                 }
 9832:                 if (&Apache::lonnet::allowed('cst',$newsecurl) && !(&Apache::lonnet::allowed('cst',$oldsecurl))) {
 9833:                     if ($sec eq '') {
 9834:                         $$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;
 9835:                     } else {
 9836:                         $$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;
 9837:                     }
 9838:                 }
 9839:             }
 9840:         }
 9841:     } else {
 9842:         $$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;
 9843:         $result = "error: incomplete course id\n";
 9844:     }
 9845:     return $result;
 9846: }
 9847: 
 9848: ############################################################
 9849: ############################################################
 9850: 
 9851: sub check_clone {
 9852:     my ($args,$linefeed) = @_;
 9853:     my $cloneid='/'.$args->{'clonedomain'}.'/'.$args->{'clonecourse'};
 9854:     my ($clonecrsudom,$clonecrsunum)= &LONCAPA::split_courseid($cloneid);
 9855:     my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
 9856:     my $clonemsg;
 9857:     my $can_clone = 0;
 9858: 
 9859:     if ($clonehome eq 'no_host') {
 9860:         $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'});     
 9861:     } else {
 9862: 	my %clonedesc = &Apache::lonnet::coursedescription($cloneid,{'one_time' => 1});
 9863: 	if (($env{'request.role.domain'} eq $args->{'clonedomain'}) && 
 9864:             (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'}))) {
 9865: 	    $can_clone = 1;
 9866: 	} else {
 9867: 	    my %clonehash = &Apache::lonnet::get('environment',['cloners'],
 9868: 						 $args->{'clonedomain'},$args->{'clonecourse'});
 9869: 	    my @cloners = split(/,/,$clonehash{'cloners'});
 9870:             if (grep(/^\*$/,@cloners)) {
 9871:                 $can_clone = 1;
 9872:             } elsif (grep(/^\*\:\Q$args->{'ccdomain'}\E$/,@cloners)) {
 9873:                 $can_clone = 1;
 9874:             } else {
 9875: 	        my %roleshash =
 9876: 		    &Apache::lonnet::get_my_roles($args->{'ccuname'},
 9877: 					 $args->{'ccdomain'},
 9878:                                          'userroles',['active'],['cc'],
 9879: 					 [$args->{'clonedomain'}]);
 9880: 	        if (($roleshash{$args->{'clonecourse'}.':'.$args->{'clonedomain'}.':cc'}) || (grep(/^\Q$args->{'ccuname'}\E:\Q$args->{'ccdomain'}\E$/,@cloners))) {
 9881: 		    $can_clone = 1;
 9882: 	        } else {
 9883:                     $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'});
 9884: 	        }
 9885: 	    }
 9886:         }
 9887:     }
 9888:     return ($can_clone, $clonemsg, $cloneid, $clonehome);
 9889: }
 9890: 
 9891: sub construct_course {
 9892:     my ($args,$logmsg,$courseid,$crsudom,$crsunum,$udom,$uname,$context,$cnum) = @_;
 9893:     my $outcome;
 9894:     my $linefeed =  '<br />'."\n";
 9895:     if ($context eq 'auto') {
 9896:         $linefeed = "\n";
 9897:     }
 9898: 
 9899: #
 9900: # Are we cloning?
 9901: #
 9902:     my ($can_clone, $clonemsg, $cloneid, $clonehome);
 9903:     if (($args->{'clonecourse'}) && ($args->{'clonedomain'})) {
 9904: 	($can_clone, $clonemsg, $cloneid, $clonehome) = &check_clone($args,$linefeed);
 9905: 	if ($context ne 'auto') {
 9906:             if ($clonemsg ne '') {
 9907: 	        $clonemsg = '<span class="LC_error">'.$clonemsg.'</span>';
 9908:             }
 9909: 	}
 9910: 	$outcome .= $clonemsg.$linefeed;
 9911: 
 9912:         if (!$can_clone) {
 9913: 	    return (0,$outcome);
 9914: 	}
 9915:     }
 9916: 
 9917: #
 9918: # Open course
 9919: #
 9920:     my $crstype = lc($args->{'crstype'});
 9921:     my %cenv=();
 9922:     $$courseid=&Apache::lonnet::createcourse($args->{'course_domain'},
 9923:                                              $args->{'cdescr'},
 9924:                                              $args->{'curl'},
 9925:                                              $args->{'course_home'},
 9926:                                              $args->{'nonstandard'},
 9927:                                              $args->{'crscode'},
 9928:                                              $args->{'ccuname'}.':'.
 9929:                                              $args->{'ccdomain'},
 9930:                                              $args->{'crstype'},
 9931:                                              $cnum);
 9932: 
 9933:     # Note: The testing routines depend on this being output; see 
 9934:     # Utils::Course. This needs to at least be output as a comment
 9935:     # if anyone ever decides to not show this, and Utils::Course::new
 9936:     # will need to be suitably modified.
 9937:     $outcome .= &mt('New LON-CAPA [_1] ID: [_2]',$crstype,$$courseid).$linefeed;
 9938: #
 9939: # Check if created correctly
 9940: #
 9941:     ($$crsudom,$$crsunum)= &LONCAPA::split_courseid($$courseid);
 9942:     my $crsuhome=&Apache::lonnet::homeserver($$crsunum,$$crsudom);
 9943:     $outcome .= &mt('Created on').': '.$crsuhome.$linefeed;
 9944: 
 9945: #
 9946: # Do the cloning
 9947: #   
 9948:     if ($can_clone && $cloneid) {
 9949: 	$clonemsg = &mt('Cloning [_1] from [_2]',$crstype,$clonehome);
 9950: 	if ($context ne 'auto') {
 9951: 	    $clonemsg = '<span class="LC_success">'.$clonemsg.'</span>';
 9952: 	}
 9953: 	$outcome .= $clonemsg.$linefeed;
 9954: 	my %oldcenv=&Apache::lonnet::dump('environment',$$crsudom,$$crsunum);
 9955: # Copy all files
 9956: 	&Apache::lonclonecourse::copycoursefiles($cloneid,$$courseid,$args->{'datemode'},$args->{'dateshift'});
 9957: # Restore URL
 9958: 	$cenv{'url'}=$oldcenv{'url'};
 9959: # Restore title
 9960: 	$cenv{'description'}=$oldcenv{'description'};
 9961: # Mark as cloned
 9962: 	$cenv{'clonedfrom'}=$cloneid;
 9963: # Need to clone grading mode
 9964:         my %newenv=&Apache::lonnet::get('environment',['grading'],$$crsudom,$$crsunum);
 9965:         $cenv{'grading'}=$newenv{'grading'};
 9966: # Do not clone these environment entries
 9967:         &Apache::lonnet::del('environment',
 9968:                   ['default_enrollment_start_date',
 9969:                    'default_enrollment_end_date',
 9970:                    'question.email',
 9971:                    'policy.email',
 9972:                    'comment.email',
 9973:                    'pch.users.denied',
 9974:                    'plc.users.denied',
 9975:                    'hidefromcat',
 9976:                    'categories'],
 9977:                    $$crsudom,$$crsunum);
 9978:     }
 9979: 
 9980: #
 9981: # Set environment (will override cloned, if existing)
 9982: #
 9983:     my @sections = ();
 9984:     my @xlists = ();
 9985:     if ($args->{'crstype'}) {
 9986:         $cenv{'type'}=$args->{'crstype'};
 9987:     }
 9988:     if ($args->{'crsid'}) {
 9989:         $cenv{'courseid'}=$args->{'crsid'};
 9990:     }
 9991:     if ($args->{'crscode'}) {
 9992:         $cenv{'internal.coursecode'}=$args->{'crscode'};
 9993:     }
 9994:     if ($args->{'crsquota'} ne '') {
 9995:         $cenv{'internal.coursequota'}=$args->{'crsquota'};
 9996:     } else {
 9997:         $cenv{'internal.coursequota'}=$args->{'crsquota'} = 20;
 9998:     }
 9999:     if ($args->{'ccuname'}) {
10000:         $cenv{'internal.courseowner'} = $args->{'ccuname'}.
10001:                                         ':'.$args->{'ccdomain'};
10002:     } else {
10003:         $cenv{'internal.courseowner'} = $args->{'curruser'};
10004:     }
10005:     my @badclasses = (); # Used to accumulate sections/crosslistings that did not pass classlist access check for course owner.
10006:     if ($args->{'crssections'}) {
10007:         $cenv{'internal.sectionnums'} = '';
10008:         if ($args->{'crssections'} =~ m/,/) {
10009:             @sections = split/,/,$args->{'crssections'};
10010:         } else {
10011:             $sections[0] = $args->{'crssections'};
10012:         }
10013:         if (@sections > 0) {
10014:             foreach my $item (@sections) {
10015:                 my ($sec,$gp) = split/:/,$item;
10016:                 my $class = $args->{'crscode'}.$sec;
10017:                 my $addcheck = &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$class,$cenv{'internal.courseowner'});
10018:                 $cenv{'internal.sectionnums'} .= $item.',';
10019:                 unless ($addcheck eq 'ok') {
10020:                     push @badclasses, $class;
10021:                 }
10022:             }
10023:             $cenv{'internal.sectionnums'} =~ s/,$//;
10024:         }
10025:     }
10026: # do not hide course coordinator from staff listing, 
10027: # even if privileged
10028:     $cenv{'nothideprivileged'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
10029: # add crosslistings
10030:     if ($args->{'crsxlist'}) {
10031:         $cenv{'internal.crosslistings'}='';
10032:         if ($args->{'crsxlist'} =~ m/,/) {
10033:             @xlists = split/,/,$args->{'crsxlist'};
10034:         } else {
10035:             $xlists[0] = $args->{'crsxlist'};
10036:         }
10037:         if (@xlists > 0) {
10038:             foreach my $item (@xlists) {
10039:                 my ($xl,$gp) = split/:/,$item;
10040:                 my $addcheck =  &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$xl,$cenv{'internal.courseowner'});
10041:                 $cenv{'internal.crosslistings'} .= $item.',';
10042:                 unless ($addcheck eq 'ok') {
10043:                     push @badclasses, $xl;
10044:                 }
10045:             }
10046:             $cenv{'internal.crosslistings'} =~ s/,$//;
10047:         }
10048:     }
10049:     if ($args->{'autoadds'}) {
10050:         $cenv{'internal.autoadds'}=$args->{'autoadds'};
10051:     }
10052:     if ($args->{'autodrops'}) {
10053:         $cenv{'internal.autodrops'}=$args->{'autodrops'};
10054:     }
10055: # check for notification of enrollment changes
10056:     my @notified = ();
10057:     if ($args->{'notify_owner'}) {
10058:         if ($args->{'ccuname'} ne '') {
10059:             push(@notified,$args->{'ccuname'}.':'.$args->{'ccdomain'});
10060:         }
10061:     }
10062:     if ($args->{'notify_dc'}) {
10063:         if ($uname ne '') { 
10064:             push(@notified,$uname.':'.$udom);
10065:         }
10066:     }
10067:     if (@notified > 0) {
10068:         my $notifylist;
10069:         if (@notified > 1) {
10070:             $notifylist = join(',',@notified);
10071:         } else {
10072:             $notifylist = $notified[0];
10073:         }
10074:         $cenv{'internal.notifylist'} = $notifylist;
10075:     }
10076:     if (@badclasses > 0) {
10077:         my %lt=&Apache::lonlocal::texthash(
10078:                 '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',
10079:                 'dnhr' => 'does not have rights to access enrollment in these classes',
10080:                 'adby' => 'as determined by the policies of your institution on access to official classlists'
10081:         );
10082:         my $badclass_msg = $cenv{'internal.courseowner'}.') - '.$lt{'dnhr'}.
10083:                            ' ('.$lt{'adby'}.')';
10084:         if ($context eq 'auto') {
10085:             $outcome .= $badclass_msg.$linefeed;
10086:             $outcome .= '<div class="LC_warning">'.$badclass_msg.$linefeed.'<ul>'."\n";
10087:             foreach my $item (@badclasses) {
10088:                 if ($context eq 'auto') {
10089:                     $outcome .= " - $item\n";
10090:                 } else {
10091:                     $outcome .= "<li>$item</li>\n";
10092:                 }
10093:             }
10094:             if ($context eq 'auto') {
10095:                 $outcome .= $linefeed;
10096:             } else {
10097:                 $outcome .= "</ul><br /><br /></div>\n";
10098:             }
10099:         } 
10100:     }
10101:     if ($args->{'no_end_date'}) {
10102:         $args->{'endaccess'} = 0;
10103:     }
10104:     $cenv{'internal.autostart'}=$args->{'enrollstart'};
10105:     $cenv{'internal.autoend'}=$args->{'enrollend'};
10106:     $cenv{'default_enrollment_start_date'}=$args->{'startaccess'};
10107:     $cenv{'default_enrollment_end_date'}=$args->{'endaccess'};
10108:     if ($args->{'showphotos'}) {
10109:       $cenv{'internal.showphotos'}=$args->{'showphotos'};
10110:     }
10111:     $cenv{'internal.authtype'} = $args->{'authtype'};
10112:     $cenv{'internal.autharg'} = $args->{'autharg'}; 
10113:     if ( ($cenv{'internal.authtype'} =~ /^krb/) && ($cenv{'internal.autoadds'} == 1)) {
10114:         if (! defined($cenv{'internal.autharg'}) || $cenv{'internal.autharg'}  eq '') {
10115:             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'); 
10116:             if ($context eq 'auto') {
10117:                 $outcome .= $krb_msg;
10118:             } else {
10119:                 $outcome .= '<span class="LC_error">'.$krb_msg.'</span>';
10120:             }
10121:             $outcome .= $linefeed;
10122:         }
10123:     }
10124:     if (($args->{'ccdomain'}) && ($args->{'ccuname'})) {
10125:        if ($args->{'setpolicy'}) {
10126:            $cenv{'policy.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
10127:        }
10128:        if ($args->{'setcontent'}) {
10129:            $cenv{'question.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
10130:        }
10131:     }
10132:     if ($args->{'reshome'}) {
10133: 	$cenv{'reshome'}=$args->{'reshome'}.'/';
10134: 	$cenv{'reshome'}=~s/\/+$/\//;
10135:     }
10136: #
10137: # course has keyed access
10138: #
10139:     if ($args->{'setkeys'}) {
10140:        $cenv{'keyaccess'}='yes';
10141:     }
10142: # if specified, key authority is not course, but user
10143: # only active if keyaccess is yes
10144:     if ($args->{'keyauth'}) {
10145: 	my ($user,$domain) = split(':',$args->{'keyauth'});
10146: 	$user = &LONCAPA::clean_username($user);
10147: 	$domain = &LONCAPA::clean_username($domain);
10148: 	if ($user ne '' && $domain ne '') {
10149: 	    $cenv{'keyauth'}=$user.':'.$domain;
10150: 	}
10151:     }
10152: 
10153:     if ($args->{'disresdis'}) {
10154:         $cenv{'pch.roles.denied'}='st';
10155:     }
10156:     if ($args->{'disablechat'}) {
10157:         $cenv{'plc.roles.denied'}='st';
10158:     }
10159: 
10160:     # Record we've not yet viewed the Course Initialization Helper for this 
10161:     # course
10162:     $cenv{'course.helper.not.run'} = 1;
10163:     #
10164:     # Use new Randomseed
10165:     #
10166:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
10167:     $cenv{'receiptalg'}=&Apache::lonnet::latest_receipt_algorithm_id();;
10168:     #
10169:     # The encryption code and receipt prefix for this course
10170:     #
10171:     $cenv{'internal.encseed'}=$Apache::lonnet::perlvar{'lonReceipt'}.$$.time.int(rand(9999));
10172:     $cenv{'internal.encpref'}=100+int(9*rand(99));
10173:     #
10174:     # By default, use standard grading
10175:     if (!defined($cenv{'grading'})) { $cenv{'grading'} = 'standard'; }
10176: 
10177:     $outcome .= $linefeed.&mt('Setting environment').': '.                 
10178:           &Apache::lonnet::put('environment',\%cenv,$$crsudom,$$crsunum).$linefeed;
10179: #
10180: # Open all assignments
10181: #
10182:     if ($args->{'openall'}) {
10183:        my $storeunder=$$crsudom.'_'.$$crsunum.'.0.opendate';
10184:        my %storecontent = ($storeunder         => time,
10185:                            $storeunder.'.type' => 'date_start');
10186:        
10187:        $outcome .= &mt('Opening all assignments').': '.&Apache::lonnet::cput
10188:                  ('resourcedata',\%storecontent,$$crsudom,$$crsunum).$linefeed;
10189:    }
10190: #
10191: # Set first page
10192: #
10193:     unless (($args->{'nonstandard'}) || ($args->{'firstres'} eq 'blank')
10194: 	    || ($cloneid)) {
10195: 	use LONCAPA::map;
10196: 	$outcome .= &mt('Setting first resource').': ';
10197: 
10198: 	my $map = '/uploaded/'.$$crsudom.'/'.$$crsunum.'/default.sequence';
10199:         my ($errtext,$fatal)=&LONCAPA::map::mapread($map);
10200: 
10201:         $outcome .= ($fatal?$errtext:'read ok').' - ';
10202:         my $title; my $url;
10203:         if ($args->{'firstres'} eq 'syl') {
10204: 	    $title=&mt('Syllabus');
10205:             $url='/public/'.$$crsudom.'/'.$$crsunum.'/syllabus';
10206:         } else {
10207:             $title=&mt('Navigate Contents');
10208:             $url='/adm/navmaps';
10209:         }
10210: 
10211:         $LONCAPA::map::resources[1]=$title.':'.$url.':false:start:res';
10212: 	(my $outtext,$errtext) = &LONCAPA::map::storemap($map,1);
10213: 
10214: 	if ($errtext) { $fatal=2; }
10215:         $outcome .= ($fatal?$errtext:'write ok').$linefeed;
10216:     }
10217: 
10218:     return (1,$outcome);
10219: }
10220: 
10221: ############################################################
10222: ############################################################
10223: 
10224: sub course_type {
10225:     my ($cid) = @_;
10226:     if (!defined($cid)) {
10227:         $cid = $env{'request.course.id'};
10228:     }
10229:     if (defined($env{'course.'.$cid.'.type'})) {
10230:         return $env{'course.'.$cid.'.type'};
10231:     } else {
10232:         return 'Course';
10233:     }
10234: }
10235: 
10236: sub group_term {
10237:     my $crstype = &course_type();
10238:     my %names = (
10239:                   'Course' => 'group',
10240:                   'Community' => 'group',
10241:                 );
10242:     return $names{$crstype};
10243: }
10244: 
10245: sub icon {
10246:     my ($file)=@_;
10247:     my $curfext = lc((split(/\./,$file))[-1]);
10248:     my $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/unknown.gif';
10249:     my $embstyle = &Apache::loncommon::fileembstyle($curfext);
10250:     if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) {
10251: 	if (-e  $Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
10252: 	          $Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
10253: 	            $curfext.".gif") {
10254: 	    $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
10255: 		$curfext.".gif";
10256: 	}
10257:     }
10258:     return &lonhttpdurl($iconname);
10259: } 
10260: 
10261: sub lonhttpdurl {
10262: #
10263: # Had been used for "small fry" static images on separate port 8080.
10264: # Modify here if lightweight http functionality desired again.
10265: # Currently eliminated due to increasing firewall issues.
10266: #
10267:     my ($url)=@_;
10268:     return $url;
10269: }
10270: 
10271: sub connection_aborted {
10272:     my ($r)=@_;
10273:     $r->print(" ");$r->rflush();
10274:     my $c = $r->connection;
10275:     return $c->aborted();
10276: }
10277: 
10278: #    Escapes strings that may have embedded 's that will be put into
10279: #    strings as 'strings'.
10280: sub escape_single {
10281:     my ($input) = @_;
10282:     $input =~ s/\\/\\\\/g;	# Escape the \'s..(must be first)>
10283:     $input =~ s/\'/\\\'/g;	# Esacpe the 's....
10284:     return $input;
10285: }
10286: 
10287: #  Same as escape_single, but escape's "'s  This 
10288: #  can be used for  "strings"
10289: sub escape_double {
10290:     my ($input) = @_;
10291:     $input =~ s/\\/\\\\/g;	# Escape the /'s..(must be first)>
10292:     $input =~ s/\"/\\\"/g;	# Esacpe the "s....
10293:     return $input;
10294: }
10295:  
10296: #   Escapes the last element of a full URL.
10297: sub escape_url {
10298:     my ($url)   = @_;
10299:     my @urlslices = split(/\//, $url,-1);
10300:     my $lastitem = &escape(pop(@urlslices));
10301:     return join('/',@urlslices).'/'.$lastitem;
10302: }
10303: 
10304: sub compare_arrays {
10305:     my ($arrayref1,$arrayref2) = @_;
10306:     my (@difference,%count);
10307:     @difference = ();
10308:     %count = ();
10309:     if ((ref($arrayref1) eq 'ARRAY') && (ref($arrayref2) eq 'ARRAY')) {
10310:         foreach my $element (@{$arrayref1}, @{$arrayref2}) { $count{$element}++; }
10311:         foreach my $element (keys(%count)) {
10312:             if ($count{$element} == 1) {
10313:                 push(@difference,$element);
10314:             }
10315:         }
10316:     }
10317:     return @difference;
10318: }
10319: 
10320: # -------------------------------------------------------- Initialize user login
10321: sub init_user_environment {
10322:     my ($r, $username, $domain, $authhost, $form, $args) = @_;
10323:     my $lonids=$Apache::lonnet::perlvar{'lonIDsDir'};
10324: 
10325:     my $public=($username eq 'public' && $domain eq 'public');
10326: 
10327: # See if old ID present, if so, remove
10328: 
10329:     my ($filename,$cookie,$userroles);
10330:     my $now=time;
10331: 
10332:     if ($public) {
10333: 	my $max_public=100;
10334: 	my $oldest;
10335: 	my $oldest_time=0;
10336: 	for(my $next=1;$next<=$max_public;$next++) {
10337: 	    if (-e $lonids."/publicuser_$next.id") {
10338: 		my $mtime=(stat($lonids."/publicuser_$next.id"))[9];
10339: 		if ($mtime<$oldest_time || !$oldest_time) {
10340: 		    $oldest_time=$mtime;
10341: 		    $oldest=$next;
10342: 		}
10343: 	    } else {
10344: 		$cookie="publicuser_$next";
10345: 		last;
10346: 	    }
10347: 	}
10348: 	if (!$cookie) { $cookie="publicuser_$oldest"; }
10349:     } else {
10350: 	# if this isn't a robot, kill any existing non-robot sessions
10351: 	if (!$args->{'robot'}) {
10352: 	    opendir(DIR,$lonids);
10353: 	    while ($filename=readdir(DIR)) {
10354: 		if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
10355: 		    unlink($lonids.'/'.$filename);
10356: 		}
10357: 	    }
10358: 	    closedir(DIR);
10359: 	}
10360: # Give them a new cookie
10361: 	my $id = ($args->{'robot'} ? 'robot'.$args->{'robot'}
10362: 		                   : $now.$$.int(rand(10000)));
10363: 	$cookie="$username\_$id\_$domain\_$authhost";
10364:     
10365: # Initialize roles
10366: 
10367: 	$userroles=&Apache::lonnet::rolesinit($domain,$username,$authhost);
10368:     }
10369: # ------------------------------------ Check browser type and MathML capability
10370: 
10371:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
10372:         $clientunicode,$clientos) = &decode_user_agent($r);
10373: 
10374: # ------------------------------------------------------------- Get environment
10375: 
10376:     my %userenv = &Apache::lonnet::dump('environment',$domain,$username);
10377:     my ($tmp) = keys(%userenv);
10378:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
10379: 	# default remote control to off
10380: 	if ($userenv{'remote'} ne 'on') { $userenv{'remote'} = 'off'; }
10381:     } else {
10382: 	undef(%userenv);
10383:     }
10384:     if (($userenv{'interface'}) && (!$form->{'interface'})) {
10385: 	$form->{'interface'}=$userenv{'interface'};
10386:     }
10387:     $env{'environment.remote'}=$userenv{'remote'};
10388:     if ($userenv{'texengine'} eq 'ttm') { $clientmathml=1; }
10389: 
10390: # --------------- Do not trust query string to be put directly into environment
10391:     foreach my $option ('interface','localpath','localres') {
10392:         $form->{$option}=~s/[\n\r\=]//gs;
10393:     }
10394: # --------------------------------------------------------- Write first profile
10395: 
10396:     {
10397: 	my %initial_env = 
10398: 	    ("user.name"          => $username,
10399: 	     "user.domain"        => $domain,
10400: 	     "user.home"          => $authhost,
10401: 	     "browser.type"       => $clientbrowser,
10402: 	     "browser.version"    => $clientversion,
10403: 	     "browser.mathml"     => $clientmathml,
10404: 	     "browser.unicode"    => $clientunicode,
10405: 	     "browser.os"         => $clientos,
10406: 	     "server.domain"      => $Apache::lonnet::perlvar{'lonDefDomain'},
10407: 	     "request.course.fn"  => '',
10408: 	     "request.course.uri" => '',
10409: 	     "request.course.sec" => '',
10410: 	     "request.role"       => 'cm',
10411: 	     "request.role.adv"   => $env{'user.adv'},
10412: 	     "request.host"       => $ENV{'REMOTE_ADDR'},);
10413: 
10414:         if ($form->{'localpath'}) {
10415: 	    $initial_env{"browser.localpath"}  = $form->{'localpath'};
10416: 	    $initial_env{"browser.localres"}   = $form->{'localres'};
10417:         }
10418: 	
10419: 	if ($public) {
10420: 	    $initial_env{"environment.remote"} = "off";
10421: 	}
10422: 	if ($form->{'interface'}) {
10423: 	    $form->{'interface'}=~s/\W//gs;
10424: 	    $initial_env{"browser.interface"} = $form->{'interface'};
10425: 	    $env{'browser.interface'}=$form->{'interface'};
10426: 	}
10427: 
10428:         foreach my $tool ('aboutme','blog','portfolio') {
10429:             $userenv{'availabletools.'.$tool} = 
10430:                 &Apache::lonnet::usertools_access($username,$domain,$tool,'reload');
10431:         }
10432: 
10433:         foreach my $crstype ('official','unofficial','community') {
10434:             $userenv{'canrequest.'.$crstype} =
10435:                 &Apache::lonnet::usertools_access($username,$domain,$crstype,
10436:                                                   'reload','requestcourses');
10437:         }
10438: 
10439: 	$env{'user.environment'} = "$lonids/$cookie.id";
10440: 	
10441: 	if (tie(my %disk_env,'GDBM_File',"$lonids/$cookie.id",
10442: 		 &GDBM_WRCREAT(),0640)) {
10443: 	    &_add_to_env(\%disk_env,\%initial_env);
10444: 	    &_add_to_env(\%disk_env,\%userenv,'environment.');
10445: 	    &_add_to_env(\%disk_env,$userroles);
10446: 	    if (ref($args->{'extra_env'})) {
10447: 		&_add_to_env(\%disk_env,$args->{'extra_env'});
10448: 	    }
10449: 	    untie(%disk_env);
10450: 	} else {
10451: 	    &Apache::lonnet::logthis("<span style=\"color:blue;\">WARNING: ".
10452: 			   'Could not create environment storage in lonauth: '.$!.'</span>');
10453: 	    return 'error: '.$!;
10454: 	}
10455:     }
10456:     $env{'request.role'}='cm';
10457:     $env{'request.role.adv'}=$env{'user.adv'};
10458:     $env{'browser.type'}=$clientbrowser;
10459: 
10460:     return $cookie;
10461: 
10462: }
10463: 
10464: sub _add_to_env {
10465:     my ($idf,$env_data,$prefix) = @_;
10466:     if (ref($env_data) eq 'HASH') {
10467:         while (my ($key,$value) = each(%$env_data)) {
10468: 	    $idf->{$prefix.$key} = $value;
10469: 	    $env{$prefix.$key}   = $value;
10470:         }
10471:     }
10472: }
10473: 
10474: # --- Get the symbolic name of a problem and the url
10475: sub get_symb {
10476:     my ($request,$silent) = @_;
10477:     (my $url=$env{'form.url'}) =~ s-^https?\://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
10478:     my $symb=($env{'form.symb'} ne '' ? $env{'form.symb'} : (&Apache::lonnet::symbread($url)));
10479:     if ($symb eq '') {
10480:         if (!$silent) {
10481:             $request->print("Unable to handle ambiguous references:$url:.");
10482:             return ();
10483:         }
10484:     }
10485:     &Apache::lonenc::check_decrypt(\$symb);
10486:     return ($symb);
10487: }
10488: 
10489: # --------------------------------------------------------------Get annotation
10490: 
10491: sub get_annotation {
10492:     my ($symb,$enc) = @_;
10493: 
10494:     my $key = $symb;
10495:     if (!$enc) {
10496:         $key =
10497:             &Apache::lonnet::clutter((&Apache::lonnet::decode_symb($symb))[2]);
10498:     }
10499:     my %annotation=&Apache::lonnet::get('nohist_annotations',[$key]);
10500:     return $annotation{$key};
10501: }
10502: 
10503: sub clean_symb {
10504:     my ($symb,$delete_enc) = @_;
10505: 
10506:     &Apache::lonenc::check_decrypt(\$symb);
10507:     my $enc = $env{'request.enc'};
10508:     if ($delete_enc) {
10509:         delete($env{'request.enc'});
10510:     }
10511: 
10512:     return ($symb,$enc);
10513: }
10514: 
10515: =pod
10516: 
10517: =back
10518: 
10519: =cut
10520: 
10521: 1;
10522: __END__;
10523: 

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