File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.337: download - view: text, annotated - select for diffs
Mon Aug 23 19:53:13 2004 UTC (19 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- allow multiple style files

    1: # The LearningOnline Network with CAPA
    2: # XML Parser Module 
    3: #
    4: # $Id: lonxml.pm,v 1.337 2004/08/23 19:53:13 albertel 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: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
   29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
   30: # binary executable programs or libraries distributed by the 
   31: # Michigan State University (the "Licensee"), but any binaries so 
   32: # distributed are hereby licensed only for use in the context
   33: # of a program or computational system for which the Licensee is the 
   34: # primary author or distributor, and which performs substantial 
   35: # additional tasks beyond the translation of (La)TeX into HTML.
   36: # The C source of the Code may not be distributed by the Licensee
   37: # to any other parties under any circumstances.
   38: #
   39: 
   40: 
   41: package Apache::lonxml; 
   42: use vars 
   43: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $errorcount $warningcount @htmlareafields);
   44: use strict;
   45: use HTML::LCParser();
   46: use HTML::TreeBuilder();
   47: use HTML::Entities();
   48: use Safe();
   49: use Safe::Hole();
   50: use Math::Cephes();
   51: use Math::Random();
   52: use Opcode();
   53: use POSIX qw(strftime);
   54: 
   55: 
   56: sub register {
   57:   my ($space,@taglist) = @_;
   58:   foreach my $temptag (@taglist) {
   59:     push(@{ $Apache::lonxml::alltags{$temptag} },$space);
   60:   }
   61: }
   62: 
   63: sub deregister {
   64:   my ($space,@taglist) = @_;
   65:   foreach my $temptag (@taglist) {
   66:     my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
   67:     if ($tempspace eq $space) {
   68:       pop(@{ $Apache::lonxml::alltags{$temptag} });
   69:     }
   70:   }
   71:   #&printalltags();
   72: }
   73: 
   74: use Apache::Constants qw(:common);
   75: use Apache::lontexconvert();
   76: use Apache::style();
   77: use Apache::run();
   78: use Apache::londefdef();
   79: use Apache::scripttag();
   80: use Apache::languagetags();
   81: use Apache::edit();
   82: use Apache::inputtags();
   83: use Apache::outputtags();
   84: use Apache::lonnet();
   85: use Apache::File();
   86: use Apache::loncommon();
   87: use Apache::lonfeedback();
   88: use Apache::lonmsg();
   89: use Apache::loncacc();
   90: use Apache::lonlocal;
   91: 
   92: #==================================================   Main subroutine: xmlparse  
   93: #debugging control, to turn on debugging modify the correct handler
   94: $Apache::lonxml::debug=0;
   95: 
   96: # keeps count of the number of warnings and errors generated in a parse
   97: $warningcount=0;
   98: $errorcount=0;
   99: 
  100: #path to the directory containing the file currently being processed
  101: @pwd=();
  102: 
  103: #these two are used for capturing a subset of the output for later processing,
  104: #don't touch them directly use &startredirection and &endredirection
  105: @outputstack = ();
  106: $redirection = 0;
  107: 
  108: #controls wheter the <import> tag actually does
  109: $import = 1;
  110: @extlinks=();
  111: 
  112: # meta mode is a bit weird only some output is to be turned off
  113: #<output> tag turns metamode off (defined in londefdef.pm)
  114: $metamode = 0;
  115: 
  116: # turns on and of run::evaluate actually derefencing var refs
  117: $evaluate = 1;
  118: 
  119: # data structure for eidt mode, determines what tags can go into what other tags
  120: %insertlist=();
  121: 
  122: # stores the list of active tag namespaces
  123: @namespace=();
  124: 
  125: # has the dynamic menu been updated to know about this resource
  126: $Apache::lonxml::registered=0;
  127: 
  128: # a pointer the the Apache request object
  129: $Apache::lonxml::request='';
  130: 
  131: # a problem number counter, and check on ether it is used
  132: $Apache::lonxml::counter=1;
  133: $Apache::lonxml::counter_changed=0;
  134: 
  135: #internal check on whether to look at style defs
  136: $Apache::lonxml::usestyle=1;
  137: 
  138: #locations used to store the parameter string for style substitutions
  139: $Apache::lonxml::style_values='';
  140: $Apache::lonxml::style_end_values='';
  141: 
  142: #array of ssi calls that need to occur after we are done parsing
  143: @Apache::lonxml::ssi_info=();
  144: 
  145: #should we do the postag variable interpolation
  146: $Apache::lonxml::post_evaluate=1;
  147: 
  148: #a header message to emit in the case of any generated warning or errors
  149: $Apache::lonxml::warnings_error_header='';
  150: 
  151: sub xmlbegin {
  152:   my $output='';
  153:   @htmlareafields=();
  154:   if ($ENV{'browser.mathml'}) {
  155:       $output='<?xml version="1.0"?>'
  156:             .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
  157:             .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
  158:             .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
  159:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
  160: 		.'xmlns="http://www.w3.org/TR/REC-html40">';
  161:   } else {
  162:       $output='<html>';
  163:   }
  164:   return $output;
  165: }
  166: 
  167: sub xmlend {
  168:     my ($target,$parser)=@_;
  169:     my $mode='xml';
  170:     my $status='OPEN';
  171:     if ($Apache::lonhomework::parsing_a_problem) {
  172: 	$mode='problem';
  173: 	$status=$Apache::inputtags::status[-1]; 
  174:     }
  175:     my $discussion=&Apache::lonfeedback::list_discussion($mode,$status);    
  176:     if ($target eq 'tex') {
  177: 	$discussion.='<tex>\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\end{document}</tex>';
  178: 	&Apache::lonxml::newparser($parser,\$discussion,'');
  179: 	return '';
  180:     } else {
  181: 	return $discussion.'</html>';
  182:     }
  183: }
  184: 
  185: sub tokeninputfield {
  186:     my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
  187:     $defhost=~tr/a-z/A-Z/;
  188:     return (<<ENDINPUTFIELD)
  189: <script type="text/javascript">
  190:     function updatetoken() {
  191: 	var comp=new Array;
  192:         var barcode=unescape(document.tokeninput.barcode.value);
  193:         comp=barcode.split('*');
  194:         if (typeof(comp[0])!="undefined") {
  195: 	    document.tokeninput.codeone.value=comp[0];
  196: 	}
  197:         if (typeof(comp[1])!="undefined") {
  198: 	    document.tokeninput.codetwo.value=comp[1];
  199: 	}
  200:         if (typeof(comp[2])!="undefined") {
  201:             comp[2]=comp[2].toUpperCase();
  202: 	    document.tokeninput.codethree.value=comp[2];
  203: 	}
  204:         document.tokeninput.barcode.value='';
  205:     }  
  206: </script>
  207: <form method="post" name="tokeninput">
  208: <table border="2" bgcolor="#FFFFBB">
  209: <tr><th>DocID Checkin</th></tr>
  210: <tr><td>
  211: <table>
  212: <tr>
  213: <td>Scan in Barcode</td>
  214: <td><input type="text" size="22" name="barcode" 
  215: onChange="updatetoken()"/></td>
  216: </tr>
  217: <tr><td><i>or</i> Type in DocID</td>
  218: <td>
  219: <input type="text" size="5" name="codeone" />
  220: <b><font size="+2">*</font></b>
  221: <input type="text" size="5" name="codetwo" />
  222: <b><font size="+2">*</font></b>
  223: <input type="text" size="10" name="codethree" value="$defhost" 
  224: onChange="this.value=this.value.toUpperCase()" />
  225: </td></tr>
  226: </table>
  227: </td></tr>
  228: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
  229: </table>
  230: </form>
  231: ENDINPUTFIELD
  232: }
  233: 
  234: sub maketoken {
  235:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
  236:     unless ($symb) {
  237: 	$symb=&Apache::lonnet::symbread();
  238:     }
  239:     unless ($tuname) {
  240: 	$tuname=$ENV{'user.name'};
  241:         $tudom=$ENV{'user.domain'};
  242:         $tcrsid=$ENV{'request.course.id'};
  243:     }
  244: 
  245:     return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
  246: }
  247: 
  248: sub printtokenheader {
  249:     my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
  250:     unless ($token) { return ''; }
  251: 
  252:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  253:     unless ($tsymb) {
  254: 	$tsymb=$symb;
  255:     }
  256:     unless ($tuname) {
  257: 	$tuname=$name;
  258:         $tudom=$domain;
  259:         $tcrsid=$courseid;
  260:     }
  261: 
  262:     my %reply=&Apache::lonnet::get('environment',
  263:               ['firstname','middlename','lastname','generation'],
  264:               $tudom,$tuname);
  265:     my $plainname=$reply{'firstname'}.' '. 
  266:                   $reply{'middlename'}.' '.
  267:                   $reply{'lastname'}.' '.
  268: 		  $reply{'generation'};
  269: 
  270:     if ($target eq 'web') {
  271:         my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
  272: 	return 
  273:  '<img align="right" src="/cgi-bin/barcode.png?encode='.$token.'" />'.
  274:                &mt('Checked out for').' '.$plainname.
  275:                '<br />'.&mt('User').': '.$tuname.' at '.$tudom.
  276: 	       '<br />'.&mt('ID').': '.$idhash{$tuname}.
  277: 	       '<br />'.&mt('CourseID').': '.$tcrsid.
  278: 	       '<br />'.&mt('Course').': '.$ENV{'course.'.$tcrsid.'.description'}.
  279:                '<br />'.&mt('DocID').': '.$token.
  280:                '<br />'.&mt('Time').': '.&Apache::lonlocal::locallocaltime().'<hr />';
  281:     } else {
  282:         return $token;
  283:     }
  284: }
  285: 
  286: sub fontsettings() {
  287:     my $headerstring='';
  288:     if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { 
  289: 	$headerstring.=
  290: 	    '<meta Content-Type="text/html; charset=x-mac-roman">';
  291:     } elsif (!$ENV{'browser.mathml'} && $ENV{'browser.unicode'}) {
  292: 	$headerstring.=
  293: 	    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
  294:     }
  295:     return $headerstring;
  296: }
  297: 
  298: sub printalltags {
  299:   my $temp;
  300:   foreach $temp (sort keys %Apache::lonxml::alltags) {
  301:     &Apache::lonxml::debug("$temp -- ".
  302: 		  join(',',@{ $Apache::lonxml::alltags{$temp} }));
  303:   }
  304: }
  305: 
  306: sub xmlparse {
  307:  my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
  308: 
  309:  &setup_globals($request,$target);
  310:  &Apache::inputtags::initialize_inputtags();
  311:  &Apache::outputtags::initialize_outputtags();
  312:  &Apache::edit::initialize_edit();
  313:  &Apache::londefdef::initialize_londefdef();
  314: 
  315: #
  316: # do we have a course style file?
  317: #
  318: 
  319:  if ($ENV{'request.course.id'} && $ENV{'request.state'} ne 'construct') {
  320:      my $bodytext=
  321: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.default_xml_style'};
  322:      if ($bodytext) {
  323: 	 foreach my $file (split(',',$bodytext)) {
  324: 	     my $location=&Apache::lonnet::filelocation('',$file);
  325: 	     my $styletext=&Apache::lonnet::getfile($location);
  326: 	     if ($styletext ne '-1') {
  327: 		 %style_for_target = (%style_for_target,
  328: 				      &Apache::style::styleparser($target,$styletext));
  329: 	     }
  330: 	 }
  331:      }
  332:  } elsif ($ENV{'construct.style'} && ($ENV{'request.state'} eq 'construct')) {
  333:      my $location=&Apache::lonnet::filelocation('',$ENV{'construct.style'});
  334:      my $styletext=&Apache::lonnet::getfile($location);
  335:        if ($styletext ne '-1') {
  336:           %style_for_target = (%style_for_target,
  337:                           &Apache::style::styleparser($target,$styletext));
  338:       }
  339:  }
  340: #&printalltags();
  341:  my @pars = ();
  342:  my $pwd=$ENV{'request.filename'};
  343:  $pwd =~ s:/[^/]*$::;
  344:  &newparser(\@pars,\$content_file_string,$pwd);
  345: 
  346:  my $safeeval = new Safe;
  347:  my $safehole = new Safe::Hole;
  348:  &init_safespace($target,$safeeval,$safehole,$safeinit);
  349: #-------------------- Redefinition of the target in the case of compound target
  350: 
  351:  ($target, my @tenta) = split('&&',$target);
  352: 
  353:  my @stack = ();
  354:  my @parstack = ();
  355:  &initdepth;
  356: 
  357:  my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
  358: 				   $safeeval,\%style_for_target);
  359: 
  360:  if ($ENV{'request.uri'}) {
  361:     &writeallows($ENV{'request.uri'});
  362:  }
  363:  &do_registered_ssi();
  364:  if ($Apache::lonxml::counter_changed) { &store_counter() }
  365:  return $finaloutput;
  366: }
  367: 
  368: sub htmlclean {
  369:     my ($raw,$full)=@_;
  370: 
  371:     my $tree = HTML::TreeBuilder->new;
  372:     $tree->ignore_unknown(0);
  373: 
  374:     $tree->parse($raw);
  375: 
  376:     my $output= $tree->as_HTML(undef,' ');
  377: 
  378:     $output=~s/\<(br|hr|img|meta|allow)(.*?)\>/\<$1$2 \/\>/gis;
  379:     $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
  380:     unless ($full) {
  381:        $output=~s/\<[\/]*(body|head|html)\>//gis;
  382:     }
  383: 
  384:     $tree = $tree->delete;
  385: 
  386:     return $output;
  387: }
  388: 
  389: sub latex_special_symbols {
  390:     my ($string,$where)=@_;
  391:     if ($where eq 'header') {
  392: 	$string =~ s/(\\|_|\^)/ /g;
  393: 	$string =~ s/(\$|%|\{|\})/\\$1/g;
  394: 	$string =~ s/_/ /g;
  395: 	$string=&Apache::lonprintout::character_chart($string);
  396: 	# any & or # leftover should be safe to just escape
  397:         $string=~s/([^\\])\&/$1\\\&/g;
  398:         $string=~s/([^\\])\#/$1\\\#/g;
  399:     } else {
  400: 	$string=~s/\\/\\ensuremath{\\backslash}/g;
  401: 	$string=~s/([^\\]|^)\%/$1\\\%/g;
  402: 	$string=~s/([^\\]|^)\$/$1\\\$/g;
  403: 	$string=~s/([^\\])\_/$1\\_/g;
  404: 	$string=~s/\$\$/\$\\\$/g;
  405: 	$string=~s/\_\_/\_\\\_/g;
  406: 	$string=~s/\#\#/\#\\\#/g;
  407:         $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g;
  408: 	$string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
  409: 	$string=&Apache::lonprintout::character_chart($string);
  410: 	# any & or # leftover should be safe to just escape
  411:         $string=~s/([^\\]|^)\&/$1\\\&/g;
  412:         $string=~s/([^\\]|^)\#/$1\\\#/g;
  413:         $string=~s/\|/\$\\mid\$/g;
  414: #single { or } How to escape?
  415:     }
  416:     return $string;
  417: }
  418: 
  419: sub inner_xmlparse {
  420:   my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
  421:   my $finaloutput = '';
  422:   my $result;
  423:   my $token;
  424:   my $dontpop=0;
  425:   while ( $#$pars > -1 ) {
  426:     while ($token = $$pars['-1']->get_token) {
  427:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') ) {
  428: 	if ($metamode<1) {
  429: 	    my $text=$token->[1];
  430: 	    if ($token->[0] eq 'C' && $target eq 'tex') {
  431: 		$text = '';
  432: #		$text = '%'.$text."\n";
  433: 	    }
  434: 	    $result.=$text;
  435: 	}
  436:       } elsif (($token->[0] eq 'D')) {
  437: 	if ($metamode<1 && $target eq 'web') {
  438: 	    my $text=$token->[1];
  439: 	    $result.=$text;
  440: 	}
  441:       } elsif ($token->[0] eq 'PI') {
  442: 	if ($metamode<1 && $target eq 'web') {
  443: 	  $result=$token->[2];
  444: 	}
  445:       } elsif ($token->[0] eq 'S') {
  446: 	# add tag to stack
  447: 	push (@$stack,$token->[1]);
  448: 	# add parameters list to another stack
  449: 	push (@$parstack,&parstring($token));
  450: 	&increasedepth($token);
  451: 	if ($Apache::lonxml::usestyle &&
  452: 	    exists($$style_for_target{$token->[1]})) {
  453: 	    $Apache::lonxml::usestyle=0;
  454: 	    my $string=$$style_for_target{$token->[1]}.
  455: 	      '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
  456: 	    &Apache::lonxml::newparser($pars,\$string);
  457: 	    $Apache::lonxml::style_values=$$parstack[-1];
  458: 	    $Apache::lonxml::style_end_values=$$parstack[-1];
  459: 	} else {
  460: 	  $result = &callsub("start_$token->[1]", $target, $token, $stack,
  461: 			     $parstack, $pars, $safeeval, $style_for_target);
  462: 	}
  463:       } elsif ($token->[0] eq 'E') {
  464: 	if ($Apache::lonxml::usestyle &&
  465: 	    exists($$style_for_target{'/'."$token->[1]"})) {
  466: 	    $Apache::lonxml::usestyle=0;
  467: 	    my $string=$$style_for_target{'/'.$token->[1]}.
  468: 	      '<LONCAPA_INTERNAL_TURN_STYLE_ON end="'.$token->[1].'" />';
  469: 	    &Apache::lonxml::newparser($pars,\$string);
  470: 	    $Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
  471: 	    $Apache::lonxml::style_end_values='';
  472: 	    $dontpop=1;
  473: 	} else {
  474: 	    #clear out any tags that didn't end
  475: 	    while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
  476: 		my $lasttag=$$stack[-1];
  477: 		if ($token->[1] =~ /^\Q$lasttag\E$/i) {
  478: 		    &Apache::lonxml::warning('Using tag &lt;/'.$token->[1].'&gt; on line '.$token->[3].' as end tag to &lt;'.$$stack[-1].'&gt;');
  479: 		    last;
  480: 		} else {
  481: 		    &Apache::lonxml::warning('Found tag &lt;/'.$token->[1].'&gt; on line '.$token->[3].' when looking for &lt;/'.$$stack[-1].'&gt; in file');
  482: 		    &end_tag($stack,$parstack,$token);
  483: 		}
  484: 	    }
  485: 	    $result = &callsub("end_$token->[1]", $target, $token, $stack,
  486: 			       $parstack, $pars,$safeeval, $style_for_target);
  487: 	}
  488:       } else {
  489: 	&Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
  490:       }
  491:       #evaluate variable refs in result
  492:       if ($Apache::lonxml::post_evaluate &&$result ne "") {
  493: 	  my $extras;
  494: 	  if (!$Apache::lonxml::usestyle) {
  495: 	      $extras=$Apache::lonxml::style_values;
  496: 	  }
  497: 	if ( $#$parstack > -1 ) {
  498: 	  $result=&Apache::run::evaluate($result,$safeeval,$extras.$$parstack[-1]);
  499: 	} else {
  500: 	  $result= &Apache::run::evaluate($result,$safeeval,$extras);
  501: 	}
  502:       }
  503:       $Apache::lonxml::post_evaluate=1;
  504: 
  505:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
  506: 	  #Style file definitions should be correct
  507: 	  if ($target eq 'tex' && ($Apache::lonxml::usestyle)) {
  508: 	      $result=&latex_special_symbols($result);
  509: 	  }
  510:       }
  511: 
  512:       if ($Apache::lonxml::redirection) {
  513: 	$Apache::lonxml::outputstack['-1'] .= $result;
  514:       } else {
  515: 	$finaloutput.=$result;
  516:       }
  517:       $result = '';
  518: 
  519:       if ($token->[0] eq 'E' && !$dontpop) {
  520: 	&end_tag($stack,$parstack,$token);
  521:       }
  522:       $dontpop=0;
  523:     }	
  524:     if ($#$pars > -1) {
  525: 	pop @$pars;
  526: 	pop @Apache::lonxml::pwd;
  527:     }
  528:   }
  529: 
  530:   # if ($target eq 'meta') {
  531:   #   $finaloutput.=&endredirection;
  532:   # }
  533: 
  534: 
  535:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
  536:     $finaloutput=&afterburn($finaloutput);
  537:   }	    
  538:   return $finaloutput;
  539: }
  540: 
  541: ## 
  542: ## Looks to see if there is a subroutine defined for this tag.  If so, call it,
  543: ## otherwise do not call it as we do not know what it is.
  544: ##
  545: sub callsub {
  546:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  547:   my $currentstring='';
  548:   my $nodefault;
  549:   {
  550:     my $sub1;
  551:     no strict 'refs';
  552:     my $tag=$token->[1];
  553: # get utterly rid of extended html tags
  554:     if ($tag=~/^x\-/i) { return ''; }
  555:     my $space=$Apache::lonxml::alltags{$tag}[-1];
  556:     if (!$space) {
  557:      	$tag=~tr/A-Z/a-z/;
  558: 	$sub=~tr/A-Z/a-z/;
  559: 	$space=$Apache::lonxml::alltags{$tag}[-1]
  560:     }
  561: 
  562:     my $deleted=0;
  563:     $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
  564:     if (($token->[0] eq 'S') && ($target eq 'modified')) {
  565:       $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
  566: 					     $parstack,$parser,$safeeval,
  567: 					     $style);
  568:     }
  569:     if (!$deleted) {
  570:       if ($space) {
  571: 	#&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
  572: 	$sub1="$space\:\:$sub";
  573: 	($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
  574: 					     $parstack,$parser,$safeeval,
  575: 					     $style);
  576:       } else {
  577:           if ($target eq 'tex') {
  578:               # throw away tag name
  579:               return '';
  580:           }
  581: 	#&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
  582: 	if ($metamode <1) {
  583: 	  if (defined($token->[4]) && ($metamode < 1)) {
  584: 	    $currentstring = $token->[4];
  585: 	  } else {
  586: 	    $currentstring = $token->[2];
  587: 	  }
  588: 	}
  589:       }
  590:       #    &Apache::lonxml::debug("nodefalt:$nodefault:");
  591:       if ($currentstring eq '' && $nodefault eq '') {
  592: 	if ($target eq 'edit') {
  593: 	  #&Apache::lonxml::debug("doing default edit for $token->[1]");
  594: 	  if ($token->[0] eq 'S') {
  595: 	    $currentstring = &Apache::edit::tag_start($target,$token);
  596: 	  } elsif ($token->[0] eq 'E') {
  597: 	    $currentstring = &Apache::edit::tag_end($target,$token);
  598: 	  }
  599: 	} elsif ($target eq 'modified') {
  600: 	  if ($token->[0] eq 'S') {
  601: 	    $currentstring = $token->[4];
  602: 	    $currentstring.=&Apache::edit::handle_insert();
  603: 	  } elsif ($token->[0] eq 'E') {
  604: 	    $currentstring = $token->[2];
  605:             $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
  606: 	  } else {
  607: 	    $currentstring = $token->[2];
  608: 	  }
  609: 	}
  610:       }
  611:     }
  612:     use strict 'refs';
  613:   }
  614:   return $currentstring;
  615: }
  616: 
  617: sub setup_globals {
  618:   my ($request,$target)=@_;
  619:   $Apache::lonxml::request=$request;
  620:   $Apache::lonxml::registered = 0;
  621:   @Apache::lonxml::htmlareafields=();
  622:   $errorcount=0;
  623:   $warningcount=0;
  624:   $Apache::lonxml::default_homework_loaded=0;
  625:   $Apache::lonxml::usestyle=1;
  626:   &init_counter();
  627:   @Apache::lonxml::pwd=();
  628:   @Apache::lonxml::extlinks=();
  629:   @Apache::lonxml::ssi_info=();
  630:   $Apache::lonxml::post_evaluate=1;
  631:   $Apache::lonxml::warnings_error_header='';
  632:   if ($target eq 'meta') {
  633:     $Apache::lonxml::redirection = 0;
  634:     $Apache::lonxml::metamode = 1;
  635:     $Apache::lonxml::evaluate = 1;
  636:     $Apache::lonxml::import = 0;
  637:   } elsif ($target eq 'answer') {
  638:     $Apache::lonxml::redirection = 0;
  639:     $Apache::lonxml::metamode = 1;
  640:     $Apache::lonxml::evaluate = 1;
  641:     $Apache::lonxml::import = 1;
  642:   } elsif ($target eq 'grade') {
  643:     &startredirection;
  644:     $Apache::lonxml::metamode = 0;
  645:     $Apache::lonxml::evaluate = 1;
  646:     $Apache::lonxml::import = 1;
  647:   } elsif ($target eq 'modified') {
  648:     $Apache::lonxml::redirection = 0;
  649:     $Apache::lonxml::metamode = 0;
  650:     $Apache::lonxml::evaluate = 0;
  651:     $Apache::lonxml::import = 0;
  652:   } elsif ($target eq 'edit') {
  653:     $Apache::lonxml::redirection = 0;
  654:     $Apache::lonxml::metamode = 0;
  655:     $Apache::lonxml::evaluate = 0;
  656:     $Apache::lonxml::import = 0;
  657:   } elsif ($target eq 'analyze') {
  658:     $Apache::lonxml::redirection = 0;
  659:     $Apache::lonxml::metamode = 0;
  660:     $Apache::lonxml::evaluate = 1;
  661:     $Apache::lonxml::import = 1;
  662:   } else {
  663:     $Apache::lonxml::redirection = 0;
  664:     $Apache::lonxml::metamode = 0;
  665:     $Apache::lonxml::evaluate = 1;
  666:     $Apache::lonxml::import = 1;
  667:   }
  668: }
  669: 
  670: sub init_safespace {
  671:   my ($target,$safeeval,$safehole,$safeinit) = @_;
  672:   $safeeval->permit("entereval");
  673:   $safeeval->permit(":base_math");
  674:   $safeeval->permit("sort");
  675:   $safeeval->permit("time");
  676:   $safeeval->deny(":base_io");
  677:   $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
  678:   $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
  679:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
  680:   $safehole->wrap(\&Apache::chemresponse::chem_standard_order,$safeeval,
  681: 		  '&chem_standard_order');
  682: 
  683:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
  684:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
  685:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
  686:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
  687:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
  688:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
  689:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
  690:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
  691:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
  692:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
  693:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
  694:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
  695:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
  696:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
  697:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
  698:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
  699:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
  700:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
  701:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
  702:   
  703:   $safehole->wrap(\&Math::Cephes::bdtr  ,$safeeval,'&bdtr'  );
  704:   $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
  705:   $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
  706:   $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
  707:   $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
  708:   $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
  709:   $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
  710:   $safehole->wrap(\&Math::Cephes::fdtr  ,$safeeval,'&fdtr'  );
  711:   $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
  712:   $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
  713:   $safehole->wrap(\&Math::Cephes::gdtr  ,$safeeval,'&gdtr'  );
  714:   $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
  715:   $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
  716:   $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
  717:   $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
  718:   $safehole->wrap(\&Math::Cephes::ndtr  ,$safeeval,'&ndtr'  );
  719:   $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
  720:   $safehole->wrap(\&Math::Cephes::pdtr  ,$safeeval,'&pdtr'  );
  721:   $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
  722:   $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
  723:   $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
  724:   $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
  725: 
  726: #  $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
  727: #  $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
  728: #  $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
  729: #  $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
  730: #  $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
  731: #  $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
  732: 
  733:   $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
  734:   $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
  735:   $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
  736:   $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
  737:   $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
  738:   $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
  739:   $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
  740:   $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
  741:   $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
  742:   $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
  743:   $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
  744:   $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
  745:   $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
  746:   $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
  747:   $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
  748:   $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
  749:   $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
  750:   $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
  751:   $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
  752:   $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
  753:   $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
  754:   $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR');
  755:   $safehole->wrap(\&Apache::lonxml::debug,$safeeval,'&LONCAPA_INTERNAL_DEBUG');
  756:   $safehole->wrap(\&Apache::caparesponse::get_sigrange,$safeeval,'&LONCAPA_INTERNAL_get_sigrange');
  757: 
  758: #need to inspect this class of ops
  759: # $safeeval->deny(":base_orig");
  760:   $safeeval->permit("require");
  761:   $safeinit .= ';$external::target="'.$target.'";';
  762:   my $rndseed;
  763:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  764:   $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
  765:   $safeinit .= ';$external::randomseed="'.$rndseed.'";';
  766:   &Apache::lonxml::debug("Setting rndseed to $rndseed");
  767:   &Apache::run::run($safeinit,$safeeval);
  768: 
  769: }
  770: 
  771: sub default_homework_load {
  772:     my ($safeeval)=@_;
  773:     &Apache::lonxml::debug('Loading default_homework');
  774:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
  775:     if ($default eq -1) {
  776: 	&Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
  777:     } else {
  778: 	&Apache::run::run($default,$safeeval);
  779: 	$Apache::lonxml::default_homework_loaded=1;
  780:     }
  781: }
  782: 
  783: my $metamode_was;
  784: sub startredirection {
  785:     if (!$Apache::lonxml::redirection) {
  786: 	$metamode_was=$Apache::lonxml::metamode;
  787:     }
  788:     $Apache::lonxml::metamode=0;
  789:     $Apache::lonxml::redirection++;
  790:     push (@Apache::lonxml::outputstack, '');
  791: }
  792: 
  793: sub endredirection {
  794:     if (!$Apache::lonxml::redirection) {
  795: 	&Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
  796: 	return '';
  797:     }
  798:     $Apache::lonxml::redirection--;
  799:     if (!$Apache::lonxml::redirection) {
  800: 	$Apache::lonxml::metamode=$metamode_was;
  801:     }
  802:     pop @Apache::lonxml::outputstack;
  803: }
  804: 
  805: sub end_tag {
  806:   my ($tagstack,$parstack,$token)=@_;
  807:   pop(@$tagstack);
  808:   pop(@$parstack);
  809:   &decreasedepth($token);
  810: }
  811: 
  812: sub initdepth {
  813:   @Apache::lonxml::depthcounter=();
  814:   $Apache::lonxml::depth=-1;
  815:   $Apache::lonxml::olddepth=-1;
  816: }
  817: 
  818: sub increasedepth {
  819:   my ($token) = @_;
  820:   $Apache::lonxml::depth++;
  821:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
  822:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
  823:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
  824:   }
  825:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  826:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
  827: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
  828: }
  829: 
  830: sub decreasedepth {
  831:   my ($token) = @_;
  832:   $Apache::lonxml::depth--;
  833:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
  834:     $#Apache::lonxml::depthcounter--;
  835:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
  836:   }
  837:   if (  $Apache::lonxml::depth < -1) {
  838:     &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));
  839:     $Apache::lonxml::depth='-1';
  840:   }
  841:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  842:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
  843: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
  844: }
  845: 
  846: sub get_all_text_unbalanced {
  847: #there is a copy of this in lonpublisher.pm
  848:     my($tag,$pars)= @_;
  849:     my $token;
  850:     my $result='';
  851:     $tag='<'.$tag.'>';
  852:     while ($token = $$pars[-1]->get_token) {
  853: 	if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  854: 	    $result.=$token->[1];
  855: 	} elsif ($token->[0] eq 'PI') {
  856: 	    $result.=$token->[2];
  857: 	} elsif ($token->[0] eq 'S') {
  858: 	    $result.=$token->[4];
  859: 	} elsif ($token->[0] eq 'E')  {
  860: 	    $result.=$token->[2];
  861: 	}
  862: 	if ($result =~ /\Q$tag\E/is) {
  863: 	    ($result,my $redo)=$result =~ /(.*)\Q$tag\E(.*)/is;
  864: 	    #&Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
  865: 	    #&Apache::lonxml::debug('Result is :'.$1);
  866: 	    $redo=$tag.$redo;
  867: 	    &Apache::lonxml::newparser($pars,\$redo);
  868: 	    last;
  869: 	}
  870:     }
  871:     return $result
  872: }
  873: 
  874: sub increment_counter {
  875:     my ($increment) = @_;
  876:     if (defined($increment) && $increment gt 0) {
  877: 	$Apache::lonxml::counter+=$increment;
  878:     } else {
  879: 	$Apache::lonxml::counter++;
  880:     }
  881:     $Apache::lonxml::counter_changed=1;
  882: }
  883: 
  884: sub init_counter {
  885:     if (defined($ENV{'form.counter'})) {
  886: 	$Apache::lonxml::counter=$ENV{'form.counter'};
  887: 	$Apache::lonxml::counter_changed=0;
  888:     } else {
  889: 	$Apache::lonxml::counter=1;
  890: 	$Apache::lonxml::counter_changed=1;
  891:     }
  892: }
  893: 
  894: sub store_counter {
  895:     &Apache::lonnet::appenv(('form.counter' => $Apache::lonxml::counter));
  896:     return '';
  897: }
  898: 
  899: sub get_all_text {
  900:     my($tag,$pars,$style)= @_;
  901:     my $gotfullstack=1;
  902:     if (ref($pars) ne 'ARRAY') {
  903: 	$gotfullstack=0;
  904: 	$pars=[$pars];
  905:     }
  906:     if (ref($style) ne 'HASH') {
  907: 	$style={};
  908:     }
  909:     my $depth=0;
  910:     my $token;
  911:     my $result='';
  912:     if ( $tag =~ m:^/: ) { 
  913: 	my $tag=substr($tag,1); 
  914: 	#&Apache::lonxml::debug("have:$tag:");
  915: 	my $top_empty=0;
  916: 	while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
  917: 	    while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
  918: 		#&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
  919: 		if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  920: 		    $result.=$token->[1];
  921: 		} elsif ($token->[0] eq 'PI') {
  922: 		    $result.=$token->[2];
  923: 		} elsif ($token->[0] eq 'S') {
  924: 		    if ($token->[1] =~ /^\Q$tag\E$/i) { $depth++; }
  925: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
  926: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
  927: 		    $result.=$token->[4];
  928: 		} elsif ($token->[0] eq 'E')  {
  929: 		    if ( $token->[1] =~ /^\Q$tag\E$/i) { $depth--; }
  930: 		    #skip sending back the last end tag
  931: 		    if ($depth == 0 && exists($$style{'/'.$token->[1]}) && $Apache::lonxml::usestyle) {
  932: 			my $string=
  933: 			    '<LONCAPA_INTERNAL_TURN_STYLE_OFF end="yes" />'.
  934: 				$$style{'/'.$token->[1]}.
  935: 				    $token->[2].
  936: 					'<LONCAPA_INTERNAL_TURN_STYLE_ON />';
  937: 			&Apache::lonxml::newparser($pars,\$string);
  938: 			#&Apache::lonxml::debug("reParsing $string");
  939: 			next;
  940: 		    }
  941: 		    if ($depth > -1) {
  942: 			$result.=$token->[2];
  943: 		    } else {
  944: 			$$pars[-1]->unget_token($token);
  945: 		    }
  946: 		}
  947: 	    }
  948: 	    if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
  949: 	    if (($depth >=0) && ($#$pars > 0) ) {
  950: 		pop(@$pars);
  951: 		pop(@Apache::lonxml::pwd);
  952: 	    }
  953: 	}
  954: 	if ($top_empty && $depth >= 0) {
  955: 	    #never found the end tag ran out of text, throw error send back blank
  956: 	    &error('Never found end tag for &lt;'.$tag.
  957: 		   '&gt; current string <pre>'.
  958: 		   &HTML::Entities::encode($result,'<>&"').
  959: 		   '</pre>');
  960: 	    if ($gotfullstack) {
  961: 		my $newstring='</'.$tag.'>'.$result;
  962: 		&Apache::lonxml::newparser($pars,\$newstring);
  963: 	    }
  964: 	    $result='';
  965: 	}
  966:     } else {
  967: 	while ($#$pars > -1) {
  968: 	    while ($token = $$pars[-1]->get_token) {
  969: 		#&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
  970: 		if (($token->[0] eq 'T')||($token->[0] eq 'C')||
  971: 		    ($token->[0] eq 'D')) {
  972: 		    $result.=$token->[1];
  973: 		} elsif ($token->[0] eq 'PI') {
  974: 		    $result.=$token->[2];
  975: 		} elsif ($token->[0] eq 'S') {
  976: 		    if ( $token->[1] =~ /^\Q$tag\E$/i) {
  977: 			$$pars[-1]->unget_token($token); last;
  978: 		    } else {
  979: 			$result.=$token->[4];
  980: 		    }
  981: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
  982: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
  983: 		} elsif ($token->[0] eq 'E')  {
  984: 		    $result.=$token->[2];
  985: 		}
  986: 	    }
  987: 	    if (($#$pars > 0) ) {
  988: 		pop(@$pars);
  989: 		pop(@Apache::lonxml::pwd);
  990: 	    } else { last; }
  991: 	}
  992:     }
  993:     #&Apache::lonxml::debug("Exit:$result:");
  994:     return $result
  995: }
  996: 
  997: sub newparser {
  998:   my ($parser,$contentref,$dir) = @_;
  999:   push (@$parser,HTML::LCParser->new($contentref));
 1000:   $$parser['-1']->xml_mode('1');
 1001:   if ( $dir eq '' ) {
 1002:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
 1003:   } else {
 1004:     push (@Apache::lonxml::pwd, $dir);
 1005:   } 
 1006: }
 1007: 
 1008: sub parstring {
 1009:   my ($token) = @_;
 1010:   my $temp='';
 1011:   foreach (@{$token->[3]}) {
 1012:     unless ($_=~/\W/) {
 1013:       my $val=$token->[2]->{$_};
 1014:       $val =~ s/([\%\@\\\"\'])/\\$1/g;
 1015:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
 1016:       $temp .= "my \$$_=\"$val\";";
 1017:     }
 1018:   }
 1019:   return $temp;
 1020: }
 1021: 
 1022: sub writeallows {
 1023:     unless ($#extlinks>=0) { return; }
 1024:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
 1025:     if ($ENV{'httpref.'.$thisurl}) {
 1026: 	$thisurl=$ENV{'httpref.'.$thisurl};
 1027:     }
 1028:     my $thisdir=$thisurl;
 1029:     $thisdir=~s/\/[^\/]+$//;
 1030:     my %httpref=();
 1031:     foreach (@extlinks) {
 1032:        $httpref{'httpref.'.
 1033:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
 1034:     }
 1035:     @extlinks=();
 1036:     &Apache::lonnet::appenv(%httpref);
 1037: }
 1038: 
 1039: sub register_ssi {
 1040:     my ($url,%form)=@_;
 1041:     push (@Apache::lonxml::ssi_info,{'url'=>$url,'form'=>\%form});
 1042:     return '';
 1043: }
 1044: 
 1045: sub do_registered_ssi {
 1046:     foreach my $info (@Apache::lonxml::ssi_info) {
 1047: 	my %form=%{ $info->{'form'}};
 1048: 	my $url=$info->{'url'};
 1049: 	&Apache::lonnet::ssi($url,%form);
 1050:     }
 1051: }
 1052: #
 1053: # Afterburner handles anchors, highlights and links
 1054: #
 1055: sub afterburn {
 1056:     my $result=shift;
 1057:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1058: 					    ['highlight','anchor','link']);
 1059:     if ($ENV{'form.highlight'}) {
 1060:        foreach (split(/\,/,$ENV{'form.highlight'})) {
 1061:            my $anchorname=$_;
 1062: 	   my $matchthis=$anchorname;
 1063:            $matchthis=~s/\_+/\\s\+/g;
 1064:            $result=~s/(\Q$matchthis\E)/\<font color=\"red\"\>$1\<\/font\>/gs;
 1065:        }
 1066:     }
 1067:     if ($ENV{'form.link'}) {
 1068:        foreach (split(/\,/,$ENV{'form.link'})) {
 1069:            my ($anchorname,$linkurl)=split(/\>/,$_);
 1070: 	   my $matchthis=$anchorname;
 1071:            $matchthis=~s/\_+/\\s\+/g;
 1072:            $result=~s/(\Q$matchthis\E)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
 1073:        }
 1074:     }
 1075:     if ($ENV{'form.anchor'}) {
 1076:         my $anchorname=$ENV{'form.anchor'};
 1077: 	my $matchthis=$anchorname;
 1078:         $matchthis=~s/\_+/\\s\+/g;
 1079:         $result=~s/(\Q$matchthis\E)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
 1080:         $result.=(<<"ENDSCRIPT");
 1081: <script type="text/javascript">
 1082:     document.location.hash='$anchorname';
 1083: </script>
 1084: ENDSCRIPT
 1085:     }
 1086:     return $result;
 1087: }
 1088: 
 1089: sub storefile {
 1090:     my ($file,$contents)=@_;
 1091:     &Apache::lonnet::correct_line_ends(\$contents);
 1092:     if (my $fh=Apache::File->new('>'.$file)) {
 1093: 	print $fh $contents;
 1094:         $fh->close();
 1095:         return 1;
 1096:     } else {
 1097: 	&warning("Unable to save file $file");
 1098: 	return 0;
 1099:     }
 1100: }
 1101: 
 1102: sub createnewhtml {
 1103:     my $title=&mt('Title of document goes here');
 1104:     my $body=&mt('Body of document goes here');
 1105:     my $filecontents=(<<SIMPLECONTENT);
 1106: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml/11/DTD/xhtml11.dtd">
 1107: <html>
 1108: <head>
 1109: <title>$title</title>
 1110: </head>
 1111: <body bgcolor="#FFFFFF">
 1112: $body
 1113: </body>
 1114: </html>
 1115: SIMPLECONTENT
 1116:     return $filecontents;
 1117: }
 1118: 
 1119: sub createnewsty {
 1120:   my $filecontents=(<<SIMPLECONTENT);
 1121: <definetag name="">
 1122:     <render>
 1123:        <web></web>
 1124:        <tex></tex>
 1125:     </render>
 1126: </definetag>
 1127: SIMPLECONTENT
 1128:   return $filecontents;
 1129: }
 1130: 
 1131: 
 1132: sub inserteditinfo {
 1133:       my ($result,$filecontents,$filetype)=@_;
 1134:       $filecontents = &HTML::Entities::encode($filecontents,'<>&"');
 1135: #      my $editheader='<a href="#editsection">Edit below</a><hr />';
 1136:       my $xml_help = '';
 1137:       my $initialize='';
 1138:       if ($filetype eq 'html') {
 1139: 	  my $addbuttons=&Apache::lonhtmlcommon::htmlareaaddbuttons();
 1140: 	  $initialize=&Apache::lonhtmlcommon::htmlareaheaders().
 1141: 	      &Apache::lonhtmlcommon::spellheader().(<<FULLPAGE);
 1142: <script type="text/javascript">
 1143: $addbuttons
 1144: 
 1145:     HTMLArea.loadPlugin("FullPage");
 1146: 
 1147:     function initDocument() {
 1148: 	var editor=new HTMLArea("filecont",config);
 1149: 	editor.registerPlugin(FullPage);
 1150: 	editor.generate();
 1151:     }
 1152: </script>
 1153: FULLPAGE
 1154:           $result=~s/\<body([^\>]*)\>/\<body onload="initDocument()" $1\>/i;
 1155: 	  $xml_help=&Apache::loncommon::helpLatexCheatsheet();
 1156:       }
 1157:       my $cleanbut = '';
 1158:       if ($filetype eq 'html') {
 1159: 	  $cleanbut='<input type="submit" name="attemptclean" value="'.
 1160: 	      &mt('Save and then attempt to clean HTML').'" />';
 1161:       }
 1162:       my $titledisplay=&display_title();
 1163:       my %lt=&Apache::lonlocal::texthash('st' => 'Save this',
 1164: 					 'vi' => 'View',
 1165: 					 'ed' => 'Edit');
 1166:       my $buttons=(<<BUTTONS);
 1167: $cleanbut
 1168: <input type="submit" name="savethisfile" accesskey="s"  value="$lt{'st'}" />
 1169: <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
 1170: BUTTONS
 1171:       $buttons.=&Apache::lonhtmlcommon::spelllink('xmledit','filecont');
 1172:       my $editfooter=(<<ENDFOOTER);
 1173: $initialize
 1174: <hr />
 1175: <a name="editsection" />
 1176: <form method="post" name="xmledit">
 1177: $xml_help
 1178: <input type="hidden" name="editmode" value="$lt{'ed'}" />
 1179: $buttons<br />
 1180: <textarea cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
 1181: <br />$buttons
 1182: <br />
 1183: </form>
 1184: $titledisplay
 1185: </body>
 1186: ENDFOOTER
 1187: #      $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
 1188:       $result=~s/(\<\/body\>)/$editfooter/is;
 1189:       return $result;
 1190: }
 1191: 
 1192: sub get_target {
 1193:   my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
 1194:   if ( $ENV{'request.state'} eq 'published') {
 1195:     if ( defined($ENV{'form.grade_target'})
 1196: 	 && ($viewgrades == 'F' )) {
 1197:       return ($ENV{'form.grade_target'});
 1198:     } elsif (defined($ENV{'form.grade_target'})) {
 1199:       if (($ENV{'form.grade_target'} eq 'web') ||
 1200: 	  ($ENV{'form.grade_target'} eq 'tex') ) {
 1201: 	return $ENV{'form.grade_target'}
 1202:       } else {
 1203: 	return 'web';
 1204:       }
 1205:     } else {
 1206:       return 'web';
 1207:     }
 1208:   } elsif ($ENV{'request.state'} eq 'construct') {
 1209:     if ( defined($ENV{'form.grade_target'})) {
 1210:       return ($ENV{'form.grade_target'});
 1211:     } else {
 1212:       return 'web';
 1213:     }
 1214:   } else {
 1215:     return 'web';
 1216:   }
 1217: }
 1218: 
 1219: sub handler {
 1220:     my $request=shift;
 1221:     
 1222:     my $target=&get_target();
 1223:     
 1224:     $Apache::lonxml::debug=$ENV{'user.debug'};
 1225:     
 1226:     if ($ENV{'browser.mathml'}) {
 1227: 	&Apache::loncommon::content_type($request,'text/xml');
 1228:     } else {
 1229: 	&Apache::loncommon::content_type($request,'text/html');
 1230:     }
 1231:     &Apache::loncommon::no_cache($request);
 1232:     $request->send_http_header;
 1233:     
 1234:     return OK if $request->header_only;
 1235: 
 1236: 
 1237:     my $file=&Apache::lonnet::filelocation("",$request->uri);
 1238:     my $filetype;
 1239:     if ($file =~ /\.sty$/) {
 1240: 	$filetype='sty';
 1241:     } else {
 1242: 	$filetype='html';
 1243:     }
 1244: #
 1245: # Edit action? Save file.
 1246: #
 1247:     unless ($ENV{'request.state'} eq 'published') {
 1248: 	if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
 1249: 	    if (&storefile($file,$ENV{'form.filecont'})) {
 1250: 		&Apache::lonxml::info("<font COLOR=\"#0000FF\">".
 1251: 				      &mt('Updated').": ".
 1252: 				      &Apache::lonlocal::locallocaltime(time).
 1253: 				      " </font>");
 1254: 	    } 
 1255: 	}
 1256:     }
 1257:     my %mystyle;
 1258:     my $result = '';
 1259:     my $filecontents=&Apache::lonnet::getfile($file);
 1260:     if ($filecontents eq -1) {
 1261: 	my $bodytag=&Apache::loncommon::bodytag('File Error');
 1262: 	my $fnf=&mt('File not found');
 1263: 	$result=(<<ENDNOTFOUND);
 1264: <html>
 1265: <head>
 1266: <title>$fnf</title>
 1267: </head>
 1268: $bodytag
 1269: <b>$fnf: $file</b>
 1270: </body>
 1271: </html>
 1272: ENDNOTFOUND
 1273:     $filecontents='';
 1274: 	if ($ENV{'request.state'} ne 'published') {
 1275: 	    if ($filetype eq 'sty') {
 1276: 		$filecontents=&createnewsty();
 1277: 	    } else {
 1278: 		$filecontents=&createnewhtml();
 1279: 	    }
 1280: 	    $ENV{'form.editmode'}='Edit'; #force edit mode
 1281: 	}
 1282:     } else {
 1283: 	unless ($ENV{'request.state'} eq 'published') {
 1284: 	    if ($ENV{'form.attemptclean'}) {
 1285: 		$filecontents=&htmlclean($filecontents,1);
 1286: 	    }
 1287: #
 1288: # we are in construction space, see if edit mode forced
 1289:             &Apache::loncommon::get_unprocessed_cgi
 1290:                           ($ENV{'QUERY_STRING'},['editmode']);
 1291: 	}
 1292: 	if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) {
 1293: 	    $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
 1294: 						'',%mystyle);
 1295: 	}
 1296:     }
 1297:     
 1298: #
 1299: # Edit action? Insert editing commands
 1300: #
 1301:     unless ($ENV{'request.state'} eq 'published') {
 1302: 	if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
 1303: 	    my $displayfile=$request->uri;
 1304: 	    $displayfile=~s/^\/[^\/]*//;
 1305: 	    $result='<html><body bgcolor="#FFFFFF">'.
 1306: 		&Apache::lonxml::message_location().'<h3>'.
 1307: 		$displayfile.
 1308: 		'</h3></body></html>';
 1309: 	    $result=&inserteditinfo($result,$filecontents,$filetype);
 1310: 	}
 1311:     }
 1312:     if ($filetype eq 'html') { writeallows($request->uri); }
 1313: 	
 1314:     
 1315:     &Apache::lonxml::add_messages(\$result);
 1316:     $request->print($result);
 1317:     
 1318:     return OK;
 1319: }
 1320: 
 1321: sub display_title {
 1322:     my $result;
 1323:     if ($ENV{'request.state'} eq 'construct') {
 1324: 	my $title=&Apache::lonnet::gettitle();
 1325: 	if (!defined($title) || $title eq '') {
 1326: 	    $title = $ENV{'request.filename'};
 1327: 	    $title = substr($title, rindex($title, '/') + 1);
 1328: 	}
 1329: 	$result = "<script type='text/javascript'>top.document.title = '$title - LON-CAPA Construction Space';</script>";
 1330:     }
 1331:     return $result;
 1332: }
 1333: 
 1334: sub debug {
 1335:     if ($Apache::lonxml::debug eq "1") {
 1336: 	$|=1;
 1337: 	my $request=$Apache::lonxml::request;
 1338: 	if (!$request) { $request=Apache->request; }
 1339: 	$request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");
 1340: #	&Apache::lonnet::logthis($_[0]);
 1341:     }
 1342: }
 1343: 
 1344: sub error {
 1345:     $errorcount++;
 1346:     if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
 1347: 	# If printing in construction space, put the error inside <pre></pre>
 1348: 	push(@Apache::lonxml::error_messages,
 1349: 	     $Apache::lonxml::warnings_error_header.
 1350: 	     "<b>ERROR:</b>".join("<br />\n",@_)."<br />\n");
 1351: 	$Apache::lonxml::warnings_error_header='';
 1352:     } else {
 1353: 	my $errormsg;
 1354: 	my ($symb)=&Apache::lonnet::symbread();
 1355: 	if ( !$symb ) {
 1356: 	    #public or browsers
 1357: 	    $errormsg=&mt("An error occured while processing this resource. The author has been notified.");
 1358: 	} 
 1359: 	#notify author
 1360: 	&Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
 1361: 	#notify course
 1362: 	if ( $symb && $ENV{'request.course.id'} ) {
 1363: 	    my (undef,%users)=&Apache::lonfeedback::decide_receiver(undef,0,1,1,1);
 1364: 	    my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
 1365: 	    my @userlist;
 1366: 	    foreach (keys %users) {
 1367: 		my ($user,$domain) = split(/:/, $_);
 1368: 		push(@userlist,"$user\@$domain");
 1369: 		&Apache::lonmsg::user_normal_msg($user,$domain,
 1370: 						 "Error [$declutter]",join('<br />',@_));
 1371: 	    }
 1372: 	    if ($ENV{'request.role.adv'}) {
 1373: 		$errormsg=&mt("An error occured while processing this resource. The course personnel ([_1]) and the author have been notified.",join(', ',@userlist));
 1374: 	    } else {
 1375: 		$errormsg=&mt("An error occured while processing this resource. The instructor has been notified.");
 1376: 	    }
 1377: 	}
 1378: 	push(@Apache::lonxml::error_messages,"<b>$errormsg</b> <br />");
 1379:     }
 1380: }
 1381: 
 1382: sub warning {
 1383:     $warningcount++;
 1384:   
 1385:     if ($ENV{'form.grade_target'} ne 'tex') {
 1386: 	if ($ENV{'request.state'} eq 'construct' || $Apache::lonxml::debug) {
 1387: 	    my $request=$Apache::lonxml::request;
 1388: 	    if (!$request) { $request=Apache->request; }
 1389: 	    push(@Apache::lonxml::warning_messages,
 1390: 		 $Apache::lonxml::warnings_error_header.
 1391: 		 "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n");
 1392: 	    $Apache::lonxml::warnings_error_header='';
 1393: 	}
 1394:     }
 1395: }
 1396: 
 1397: sub info {
 1398:     if ($ENV{'form.grade_target'} ne 'tex' 
 1399: 	&& $ENV{'request.state'} eq 'construct') {
 1400: 	push(@Apache::lonxml::info_messages,join('<br />',@_)."<br />\n");
 1401:     }
 1402: }
 1403: 
 1404: sub message_location {
 1405:     return '__LONCAPA_INTERNAL_MESSAGE_LOCATION__';
 1406: }
 1407: 
 1408: sub add_messages {
 1409:     my ($msg)=@_;
 1410:     my $result=join(' ',
 1411: 		    @Apache::lonxml::info_messages,
 1412: 		    @Apache::lonxml::error_messages,
 1413: 		    @Apache::lonxml::warning_messages);
 1414:     undef(@Apache::lonxml::info_messages);
 1415:     undef(@Apache::lonxml::error_messages);
 1416:     undef(@Apache::lonxml::warning_messages);
 1417:     $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__/$result/;
 1418:     $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__//g;
 1419: }
 1420: 
 1421: sub get_param {
 1422:     my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
 1423:     if ( ! $context ) { $context = -1; }
 1424:     my $args ='';
 1425:     if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
 1426:     if ( ! $Apache::lonxml::usestyle ) {
 1427: 	$args=$Apache::lonxml::style_values.$args;
 1428:     }
 1429:     if ( ! $args ) { return undef; }
 1430:     if ( $case_insensitive ) {
 1431: 	if ($args =~ s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei) {
 1432: 	    return &Apache::run::run("{$args;".'return $'.$param.'}',
 1433:                                      $safeeval); #'
 1434: 	} else {
 1435: 	    return undef;
 1436: 	}
 1437:     } else {
 1438: 	if ( $args =~ /my \$\Q$param\E=\"/ ) {
 1439: 	    return &Apache::run::run("{$args;".'return $'.$param.'}',
 1440:                                      $safeeval); #'
 1441: 	} else {
 1442: 	    return undef;
 1443: 	}
 1444:     }
 1445: }
 1446: 
 1447: sub get_param_var {
 1448:   my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
 1449:   if ( ! $context ) { $context = -1; }
 1450:   my $args ='';
 1451:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
 1452:   if ( ! $Apache::lonxml::usestyle ) {
 1453:       $args=$Apache::lonxml::style_values.$args;
 1454:   }
 1455:   &Apache::lonxml::debug("Args are $args param is $param");
 1456:   if ($case_insensitive) {
 1457:       if (! ($args=~s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei)) {
 1458: 	  return undef;
 1459:       }
 1460:   } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }
 1461:   my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
 1462:   &Apache::lonxml::debug("first run is $value");
 1463:   if ($value =~ /^[\$\@\%]\w+$/) {
 1464:       &Apache::lonxml::debug("doing second");
 1465:       my @result=&Apache::run::run("return $value",$safeeval,1);
 1466:       if (!defined($result[0])) {
 1467: 	  return $value
 1468:       } else {
 1469: 	  if (wantarray) { return @result; } else { return $result[0]; }
 1470:       }
 1471:   } else {
 1472:     return $value;
 1473:   }
 1474: }
 1475: 
 1476: sub register_insert {
 1477:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
 1478:   my $i;
 1479:   my $tagnum=0;
 1480:   my @order;
 1481:   for ($i=0;$i < $#data; $i++) {
 1482:     my $line = $data[$i];
 1483:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
 1484:     if ( $line =~ /TABLE/ ) { last; }
 1485:     my ($tag,$descrip,$color,$function,$show,$helpfile,$helpdesc) = split(/,/, $line);
 1486:     if ($tag) {
 1487:       $insertlist{"$tagnum.tag"} = $tag;
 1488:       $insertlist{"$tagnum.description"} = $descrip;
 1489:       $insertlist{"$tagnum.color"} = $color;
 1490:       $insertlist{"$tagnum.function"} = $function;
 1491:       if (!defined($show)) { $show='yes'; }
 1492:       $insertlist{"$tagnum.show"}= $show;
 1493:       $insertlist{"$tagnum.helpfile"} = $helpfile;
 1494:       $insertlist{"$tagnum.helpdesc"} = $helpdesc;
 1495:       $insertlist{"$tag.num"}=$tagnum;
 1496:       $tagnum++;
 1497:     }
 1498:   }
 1499:   $i++; #skipping TABLE line
 1500:   $tagnum = 0;
 1501:   for (;$i < $#data;$i++) {
 1502:     my $line = $data[$i];
 1503:     my ($mnemonic,@which) = split(/ +/,$line);
 1504:     my $tag = $insertlist{"$tagnum.tag"};
 1505:     for (my $j=0;$j <=$#which;$j++) {
 1506:       if ( $which[$j] eq 'Y' ) {
 1507: 	if ($insertlist{"$j.show"} ne 'no') {
 1508: 	  push(@{ $insertlist{"$tag.which"} },$j);
 1509: 	}
 1510:       }
 1511:     }
 1512:     $tagnum++;
 1513:   }
 1514: }
 1515: 
 1516: sub description {
 1517:   my ($token)=@_;
 1518:   my $tagnum;
 1519:   my $tag=$token->[1];
 1520:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
 1521:     my $testtag=$namespace.'::'.$tag;
 1522:     $tagnum=$insertlist{"$testtag.num"};
 1523:     if (defined($tagnum)) { last; }
 1524:   }
 1525:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
 1526:   return $insertlist{$tagnum.'.description'};
 1527: }
 1528: 
 1529: # Returns a list containing the help file, and the description
 1530: sub helpinfo {
 1531:   my ($token)=@_;
 1532:   my $tagnum;
 1533:   my $tag=$token->[1];
 1534:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
 1535:     my $testtag=$namespace.'::'.$tag;
 1536:     $tagnum=$insertlist{"$testtag.num"};
 1537:     if (defined($tagnum)) { last; }
 1538:   }
 1539:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
 1540:   return ($insertlist{$tagnum.'.helpfile'}, $insertlist{$tagnum.'.helpdesc'});
 1541: }
 1542: 
 1543: # ----------------------------------------------------------------- whichuser
 1544: # returns a list of $symb, $courseid, $domain, $name that is correct for
 1545: # calls to lonnet functions for this setup.
 1546: # - looks for form.grade_ parameters
 1547: sub whichuser {
 1548:   my ($passedsymb)=@_;
 1549:   my ($symb,$courseid,$domain,$name,$publicuser);
 1550:   if (defined($ENV{'form.grade_symb'})) {
 1551:     my $tmp_courseid=$ENV{'form.grade_courseid'};
 1552:     my $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid);
 1553:     if ($allowed) {
 1554:       $symb=$ENV{'form.grade_symb'};
 1555:       $courseid=$ENV{'form.grade_courseid'};
 1556:       $domain=$ENV{'form.grade_domain'};
 1557:       $name=$ENV{'form.grade_username'};
 1558:     }
 1559:   } else {
 1560:       if (!$passedsymb) {
 1561:           $symb=&Apache::lonnet::symbread();
 1562:       } else {
 1563:           $symb=$passedsymb;
 1564:       }
 1565:       $courseid=$ENV{'request.course.id'};
 1566:       $domain=$ENV{'user.domain'};
 1567:       $name=$ENV{'user.name'};
 1568:       if ($name eq 'public' && $domain eq 'public') {
 1569: 	  if (!defined($ENV{'form.username'})) {
 1570: 	      $ENV{'form.username'}.=time.rand(10000000);
 1571: 	  }
 1572: 	  $name.=$ENV{'form.username'};
 1573:       }
 1574:   }
 1575:   return ($symb,$courseid,$domain,$name,$publicuser);
 1576: }
 1577: 
 1578: 1;
 1579: __END__
 1580: 
 1581: 

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