File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.329: download - view: text, annotated - select for diffs
Tue Jul 13 18:13:46 2004 UTC (19 years, 10 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Underscore problem is fixed (escaped everywhere).

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

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