Annotation of loncom/xml/lonxml.pm, revision 1.409

1.2       sakharuk    1: # The LearningOnline Network with CAPA
1.3       sakharuk    2: # XML Parser Module 
1.2       sakharuk    3: #
1.409   ! albertel    4: # $Id: lonxml.pm,v 1.408 2006/04/18 20:50:45 albertel Exp $
1.139     www         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: #
1.316     albertel   39: 
1.2       sakharuk   40: 
1.4       albertel   41: package Apache::lonxml; 
1.33      www        42: use vars 
1.320     www        43: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $errorcount $warningcount @htmlareafields);
1.1       sakharuk   44: use strict;
1.167     albertel   45: use HTML::LCParser();
1.161     albertel   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();
1.271     www        53: use POSIX qw(strftime);
1.339     albertel   54: use Time::HiRes qw( gettimeofday tv_interval );
1.393     albertel   55: use Symbol();
1.266     bowersj2   56: 
1.72      albertel   57: sub register {
1.141     albertel   58:   my ($space,@taglist) = @_;
                     59:   foreach my $temptag (@taglist) {
                     60:     push(@{ $Apache::lonxml::alltags{$temptag} },$space);
1.72      albertel   61:   }
                     62: }
                     63: 
1.141     albertel   64: sub deregister {
                     65:   my ($space,@taglist) = @_;
                     66:   foreach my $temptag (@taglist) {
                     67:     my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
                     68:     if ($tempspace eq $space) {
                     69:       pop(@{ $Apache::lonxml::alltags{$temptag} });
                     70:     }
                     71:   }
1.142     albertel   72:   #&printalltags();
1.141     albertel   73: }
                     74: 
1.46      www        75: use Apache::Constants qw(:common);
1.161     albertel   76: use Apache::lontexconvert();
                     77: use Apache::style();
                     78: use Apache::run();
                     79: use Apache::londefdef();
                     80: use Apache::scripttag();
1.285     www        81: use Apache::languagetags();
1.161     albertel   82: use Apache::edit();
1.266     bowersj2   83: use Apache::inputtags();
                     84: use Apache::outputtags();
1.372     albertel   85: use Apache::lonnet;
1.161     albertel   86: use Apache::File();
                     87: use Apache::loncommon();
1.198     www        88: use Apache::lonfeedback();
1.200     www        89: use Apache::lonmsg();
1.217     matthew    90: use Apache::loncacc();
1.280     www        91: use Apache::lonlocal;
1.79      www        92: 
1.72      albertel   93: #==================================================   Main subroutine: xmlparse  
                     94: #debugging control, to turn on debugging modify the correct handler
                     95: $Apache::lonxml::debug=0;
1.206     albertel   96: 
                     97: # keeps count of the number of warnings and errors generated in a parse
                     98: $warningcount=0;
                     99: $errorcount=0;
1.72      albertel  100: 
                    101: #path to the directory containing the file currently being processed
                    102: @pwd=();
                    103: 
                    104: #these two are used for capturing a subset of the output for later processing,
                    105: #don't touch them directly use &startredirection and &endredirection
                    106: @outputstack = ();
                    107: $redirection = 0;
                    108: 
                    109: #controls wheter the <import> tag actually does
                    110: $import = 1;
                    111: @extlinks=();
                    112: 
                    113: # meta mode is a bit weird only some output is to be turned off
                    114: #<output> tag turns metamode off (defined in londefdef.pm)
                    115: $metamode = 0;
                    116: 
                    117: # turns on and of run::evaluate actually derefencing var refs
                    118: $evaluate = 1;
1.7       albertel  119: 
1.74      albertel  120: # data structure for eidt mode, determines what tags can go into what other tags
                    121: %insertlist=();
1.68      www       122: 
1.99      albertel  123: # stores the list of active tag namespaces
1.76      albertel  124: @namespace=();
                    125: 
1.99      albertel  126: # has the dynamic menu been updated to know about this resource
                    127: $Apache::lonxml::registered=0;
                    128: 
1.172     albertel  129: # a pointer the the Apache request object
                    130: $Apache::lonxml::request='';
                    131: 
1.216     sakharuk  132: # a problem number counter, and check on ether it is used
1.237     sakharuk  133: $Apache::lonxml::counter=1;
1.204     albertel  134: $Apache::lonxml::counter_changed=0;
                    135: 
1.212     albertel  136: #internal check on whether to look at style defs
                    137: $Apache::lonxml::usestyle=1;
1.260     albertel  138: 
                    139: #locations used to store the parameter string for style substitutions
                    140: $Apache::lonxml::style_values='';
                    141: $Apache::lonxml::style_end_values='';
1.212     albertel  142: 
1.281     albertel  143: #array of ssi calls that need to occur after we are done parsing
                    144: @Apache::lonxml::ssi_info=();
                    145: 
1.282     albertel  146: #should we do the postag variable interpolation
                    147: $Apache::lonxml::post_evaluate=1;
                    148: 
1.295     albertel  149: #a header message to emit in the case of any generated warning or errors
                    150: $Apache::lonxml::warnings_error_header='';
                    151: 
1.396     foxr      152: #  Control whether or not LaTeX symbols should be substituted for their
                    153: #  \ style equivalents...this may be turned off e.g. in an verbatim
                    154: #  environment.
                    155: 
                    156: $Apache::lonxml::substitute_LaTeX_symbols = 1; # Starts out on.
                    157: 
                    158: sub enable_LaTeX_substitutions {
                    159:     $Apache::lonxml::substitute_LaTeX_symbols = 1;
                    160: }
                    161: sub disable_LaTeX_substitutions {
                    162:     $Apache::lonxml::substitute_LaTeX_symbols = 0;
                    163: }
                    164: 
1.68      www       165: sub xmlend {
1.335     sakharuk  166:     my ($target,$parser)=@_;
1.278     www       167:     my $mode='xml';
                    168:     my $status='OPEN';
1.368     albertel  169:     if ($Apache::lonhomework::parsing_a_problem ||
                    170: 	$Apache::lonhomework::parsing_a_task ) {
1.278     www       171: 	$mode='problem';
                    172: 	$status=$Apache::inputtags::status[-1]; 
                    173:     }
1.362     matthew   174:     my $discussion;
1.379     albertel  175:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    176: 					   ['LONCAPA_INTERNAL_no_discussion']);
1.372     albertel  177:     if (! exists($env{'form.LONCAPA_INTERNAL_no_discussion'}) ||
                    178:         $env{'form.LONCAPA_INTERNAL_no_discussion'} ne 'true') {
1.362     matthew   179:         $discussion=&Apache::lonfeedback::list_discussion($mode,$status);
                    180:     }
1.334     sakharuk  181:     if ($target eq 'tex') {
1.335     sakharuk  182: 	$discussion.='<tex>\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\end{document}</tex>';
1.334     sakharuk  183: 	&Apache::lonxml::newparser($parser,\$discussion,'');
                    184: 	return '';
                    185:     }
1.405     albertel  186: 
1.407     albertel  187:     return $discussion;
1.119     www       188: }
                    189: 
                    190: sub tokeninputfield {
1.120     www       191:     my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
                    192:     $defhost=~tr/a-z/A-Z/;
1.119     www       193:     return (<<ENDINPUTFIELD)
1.226     albertel  194: <script type="text/javascript">
1.120     www       195:     function updatetoken() {
                    196: 	var comp=new Array;
                    197:         var barcode=unescape(document.tokeninput.barcode.value);
                    198:         comp=barcode.split('*');
                    199:         if (typeof(comp[0])!="undefined") {
                    200: 	    document.tokeninput.codeone.value=comp[0];
                    201: 	}
                    202:         if (typeof(comp[1])!="undefined") {
                    203: 	    document.tokeninput.codetwo.value=comp[1];
                    204: 	}
                    205:         if (typeof(comp[2])!="undefined") {
                    206:             comp[2]=comp[2].toUpperCase();
                    207: 	    document.tokeninput.codethree.value=comp[2];
                    208: 	}
                    209:         document.tokeninput.barcode.value='';
                    210:     }  
                    211: </script>
                    212: <form method="post" name="tokeninput">
1.119     www       213: <table border="2" bgcolor="#FFFFBB">
                    214: <tr><th>DocID Checkin</th></tr>
                    215: <tr><td>
                    216: <table>
                    217: <tr>
                    218: <td>Scan in Barcode</td>
1.120     www       219: <td><input type="text" size="22" name="barcode" 
                    220: onChange="updatetoken()"/></td>
1.119     www       221: </tr>
                    222: <tr><td><i>or</i> Type in DocID</td>
                    223: <td>
                    224: <input type="text" size="5" name="codeone" />
1.120     www       225: <b><font size="+2">*</font></b>
1.119     www       226: <input type="text" size="5" name="codetwo" />
1.120     www       227: <b><font size="+2">*</font></b>
                    228: <input type="text" size="10" name="codethree" value="$defhost" 
                    229: onChange="this.value=this.value.toUpperCase()" />
1.119     www       230: </td></tr>
                    231: </table>
                    232: </td></tr>
                    233: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
                    234: </table>
                    235: </form>
                    236: ENDINPUTFIELD
1.112     www       237: }
                    238: 
1.116     www       239: sub maketoken {
1.118     www       240:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
1.112     www       241:     unless ($symb) {
                    242: 	$symb=&Apache::lonnet::symbread();
                    243:     }
                    244:     unless ($tuname) {
1.372     albertel  245: 	$tuname=$env{'user.name'};
                    246:         $tudom=$env{'user.domain'};
                    247:         $tcrsid=$env{'request.course.id'};
1.112     www       248:     }
1.116     www       249: 
1.118     www       250:     return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
                    251: }
                    252: 
                    253: sub printtokenheader {
1.133     albertel  254:     my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
1.116     www       255:     unless ($token) { return ''; }
1.118     www       256: 
1.133     albertel  257:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    258:     unless ($tsymb) {
                    259: 	$tsymb=$symb;
1.118     www       260:     }
                    261:     unless ($tuname) {
1.133     albertel  262: 	$tuname=$name;
                    263:         $tudom=$domain;
                    264:         $tcrsid=$courseid;
1.118     www       265:     }
1.114     www       266: 
1.390     albertel  267:     my $plainname=&Apache::loncommon::plainname($tuname,$tudom);
1.114     www       268: 
1.112     www       269:     if ($target eq 'web') {
1.145     www       270:         my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
1.115     www       271: 	return 
1.221     albertel  272:  '<img align="right" src="/cgi-bin/barcode.png?encode='.$token.'" />'.
1.284     www       273:                &mt('Checked out for').' '.$plainname.
                    274:                '<br />'.&mt('User').': '.$tuname.' at '.$tudom.
                    275: 	       '<br />'.&mt('ID').': '.$idhash{$tuname}.
                    276: 	       '<br />'.&mt('CourseID').': '.$tcrsid.
1.372     albertel  277: 	       '<br />'.&mt('Course').': '.$env{'course.'.$tcrsid.'.description'}.
1.284     www       278:                '<br />'.&mt('DocID').': '.$token.
                    279:                '<br />'.&mt('Time').': '.&Apache::lonlocal::locallocaltime().'<hr />';
1.112     www       280:     } else {
1.121     albertel  281:         return $token;
1.112     www       282:     }
1.68      www       283: }
                    284: 
1.48      albertel  285: sub printalltags {
                    286:   my $temp;
                    287:   foreach $temp (sort keys %Apache::lonxml::alltags) {
1.141     albertel  288:     &Apache::lonxml::debug("$temp -- ".
                    289: 		  join(',',@{ $Apache::lonxml::alltags{$temp} }));
1.48      albertel  290:   }
                    291: }
1.31      sakharuk  292: 
1.3       sakharuk  293: sub xmlparse {
1.172     albertel  294:  my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
1.96      albertel  295: 
1.172     albertel  296:  &setup_globals($request,$target);
1.232     albertel  297:  &Apache::inputtags::initialize_inputtags();
1.370     albertel  298:  &Apache::bridgetask::initialize_bridgetask();
1.232     albertel  299:  &Apache::outputtags::initialize_outputtags();
                    300:  &Apache::edit::initialize_edit();
1.287     albertel  301:  &Apache::londefdef::initialize_londefdef();
1.244     albertel  302: 
1.178     www       303: #
                    304: # do we have a course style file?
                    305: #
                    306: 
1.372     albertel  307:  if ($env{'request.course.id'} && $env{'request.state'} ne 'construct') {
1.178     www       308:      my $bodytext=
1.372     albertel  309: 	 $env{'course.'.$env{'request.course.id'}.'.default_xml_style'};
1.178     www       310:      if ($bodytext) {
1.337     albertel  311: 	 foreach my $file (split(',',$bodytext)) {
                    312: 	     my $location=&Apache::lonnet::filelocation('',$file);
                    313: 	     my $styletext=&Apache::lonnet::getfile($location);
                    314: 	     if ($styletext ne '-1') {
                    315: 		 %style_for_target = (%style_for_target,
                    316: 				      &Apache::style::styleparser($target,$styletext));
                    317: 	     }
                    318: 	 }
                    319:      }
1.372     albertel  320:  } elsif ($env{'construct.style'} && ($env{'request.state'} eq 'construct')) {
                    321:      my $location=&Apache::lonnet::filelocation('',$env{'construct.style'});
1.291     sakharuk  322:      my $styletext=&Apache::lonnet::getfile($location);
                    323:        if ($styletext ne '-1') {
                    324:           %style_for_target = (%style_for_target,
                    325:                           &Apache::style::styleparser($target,$styletext));
                    326:       }
1.178     www       327:  }
1.255     sakharuk  328: #&printalltags();
1.16      albertel  329:  my @pars = ();
1.372     albertel  330:  my $pwd=$env{'request.filename'};
1.23      albertel  331:  $pwd =~ s:/[^/]*$::;
                    332:  &newparser(\@pars,\$content_file_string,$pwd);
1.24      sakharuk  333: 
1.3       sakharuk  334:  my $safeeval = new Safe;
1.40      albertel  335:  my $safehole = new Safe::Hole;
1.82      ng        336:  &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3       sakharuk  337: #-------------------- Redefinition of the target in the case of compound target
                    338: 
                    339:  ($target, my @tenta) = split('&&',$target);
                    340: 
1.150     albertel  341:  my @stack = ();
1.3       sakharuk  342:  my @parstack = ();
1.358     albertel  343:  &initdepth();
                    344:  &init_alarm();
1.101     albertel  345:  my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
1.394     albertel  346: 				   $safeeval,\%style_for_target,1);
1.255     sakharuk  347: 
1.372     albertel  348:  if ($env{'request.uri'}) {
                    349:     &writeallows($env{'request.uri'});
1.125     www       350:  }
1.281     albertel  351:  &do_registered_ssi();
1.204     albertel  352:  if ($Apache::lonxml::counter_changed) { &store_counter() }
1.393     albertel  353: 
                    354:  &clean_safespace($safeeval);
                    355: 
1.372     albertel  356:  if ($env{'form.return_only_error_and_warning_counts'}) {
1.361     www       357:      return "$errorcount:$warningcount";
                    358:  }
1.3       sakharuk  359:  return $finaloutput;
1.106     www       360: }
                    361: 
1.191     albertel  362: sub latex_special_symbols {
1.272     albertel  363:     my ($string,$where)=@_;
1.396     foxr      364:     #
                    365:     #  If e.g. in verbatim mode, then don't substitute.
                    366:     #  but return original string.
                    367:     #
                    368:     if (!($Apache::lonxml::substitute_LaTeX_symbols)) {
                    369: 	return $string;
                    370:     }
1.235     sakharuk  371:     if ($where eq 'header') {
1.272     albertel  372: 	$string =~ s/(\\|_|\^)/ /g;
1.311     albertel  373: 	$string =~ s/(\$|%|\{|\})/\\$1/g;
1.273     sakharuk  374: 	$string =~ s/_/ /g;
1.311     albertel  375: 	$string=&Apache::lonprintout::character_chart($string);
                    376: 	# any & or # leftover should be safe to just escape
                    377:         $string=~s/([^\\])\&/$1\\\&/g;
                    378:         $string=~s/([^\\])\#/$1\\\#/g;
1.229     sakharuk  379:     } else {
1.312     albertel  380: 	$string=~s/\\/\\ensuremath{\\backslash}/g;
1.367     albertel  381: 	$string=~s/\\\%|\%/\\\%/g;
                    382: 	$string=~s/\\{|{/\\{/g;
                    383: 	$string=~s/\\}|}/\\}/g;
1.378     albertel  384: 	$string=~s/\\ensuremath\\{\\backslash\\}/\\ensuremath{\\backslash}/g;
1.367     albertel  385: 	$string=~s/\\\$|\$/\\\$/g;
                    386: 	$string=~s/\\\_|\_/\\\_/g;
1.313     albertel  387:         $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g;
1.310     sakharuk  388: 	$string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
1.311     albertel  389: 	$string=&Apache::lonprintout::character_chart($string);
                    390: 	# any & or # leftover should be safe to just escape
1.367     albertel  391: 	$string=~s/\\\&|\&/\\\&/g;
                    392: 	$string=~s/\\\#|\#/\\\#/g;
1.332     sakharuk  393:         $string=~s/\|/\$\\mid\$/g;
1.310     sakharuk  394: #single { or } How to escape?
1.229     sakharuk  395:     }
1.272     albertel  396:     return $string;
1.188     sakharuk  397: }
                    398: 
1.101     albertel  399: sub inner_xmlparse {
1.394     albertel  400:   my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target,$start)=@_;
1.101     albertel  401:   my $finaloutput = '';
                    402:   my $result;
                    403:   my $token;
1.258     albertel  404:   my $dontpop=0;
1.389     albertel  405:   my $startredirection = $Apache::lonxml::redirection;
1.101     albertel  406:   while ( $#$pars > -1 ) {
                    407:     while ($token = $$pars['-1']->get_token) {
1.261     albertel  408:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') ) {
1.101     albertel  409: 	if ($metamode<1) {
1.190     albertel  410: 	    my $text=$token->[1];
1.193     albertel  411: 	    if ($token->[0] eq 'C' && $target eq 'tex') {
1.239     sakharuk  412: 		$text = '';
                    413: #		$text = '%'.$text."\n";
1.182     sakharuk  414: 	    }
1.190     albertel  415: 	    $result.=$text;
1.101     albertel  416: 	}
1.261     albertel  417:       } elsif (($token->[0] eq 'D')) {
                    418: 	if ($metamode<1 && $target eq 'web') {
                    419: 	    my $text=$token->[1];
                    420: 	    $result.=$text;
                    421: 	}
1.101     albertel  422:       } elsif ($token->[0] eq 'PI') {
1.261     albertel  423: 	if ($metamode<1 && $target eq 'web') {
1.101     albertel  424: 	  $result=$token->[2];
                    425: 	}
                    426:       } elsif ($token->[0] eq 'S') {
1.140     albertel  427: 	# add tag to stack
1.101     albertel  428: 	push (@$stack,$token->[1]);
                    429: 	# add parameters list to another stack
                    430: 	push (@$parstack,&parstring($token));
1.140     albertel  431: 	&increasedepth($token);
1.212     albertel  432: 	if ($Apache::lonxml::usestyle &&
                    433: 	    exists($$style_for_target{$token->[1]})) {
                    434: 	    $Apache::lonxml::usestyle=0;
                    435: 	    my $string=$$style_for_target{$token->[1]}.
                    436: 	      '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
                    437: 	    &Apache::lonxml::newparser($pars,\$string);
1.257     albertel  438: 	    $Apache::lonxml::style_values=$$parstack[-1];
1.259     albertel  439: 	    $Apache::lonxml::style_end_values=$$parstack[-1];
1.101     albertel  440: 	} else {
                    441: 	  $result = &callsub("start_$token->[1]", $target, $token, $stack,
                    442: 			     $parstack, $pars, $safeeval, $style_for_target);
1.140     albertel  443: 	}
1.101     albertel  444:       } elsif ($token->[0] eq 'E') {
1.212     albertel  445: 	if ($Apache::lonxml::usestyle &&
                    446: 	    exists($$style_for_target{'/'."$token->[1]"})) {
                    447: 	    $Apache::lonxml::usestyle=0;
                    448: 	    my $string=$$style_for_target{'/'.$token->[1]}.
1.258     albertel  449: 	      '<LONCAPA_INTERNAL_TURN_STYLE_ON end="'.$token->[1].'" />';
1.212     albertel  450: 	    &Apache::lonxml::newparser($pars,\$string);
1.259     albertel  451: 	    $Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
                    452: 	    $Apache::lonxml::style_end_values='';
1.258     albertel  453: 	    $dontpop=1;
1.101     albertel  454: 	} else {
1.258     albertel  455: 	    #clear out any tags that didn't end
                    456: 	    while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
                    457: 		my $lasttag=$$stack[-1];
1.317     albertel  458: 		if ($token->[1] =~ /^\Q$lasttag\E$/i) {
1.258     albertel  459: 		    &Apache::lonxml::warning('Using tag &lt;/'.$token->[1].'&gt; on line '.$token->[3].' as end tag to &lt;'.$$stack[-1].'&gt;');
                    460: 		    last;
                    461: 		} else {
                    462: 		    &Apache::lonxml::warning('Found tag &lt;/'.$token->[1].'&gt; on line '.$token->[3].' when looking for &lt;/'.$$stack[-1].'&gt; in file');
                    463: 		    &end_tag($stack,$parstack,$token);
                    464: 		}
                    465: 	    }
                    466: 	    $result = &callsub("end_$token->[1]", $target, $token, $stack,
                    467: 			       $parstack, $pars,$safeeval, $style_for_target);
1.101     albertel  468: 	}
                    469:       } else {
                    470: 	&Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
                    471:       }
                    472:       #evaluate variable refs in result
1.282     albertel  473:       if ($Apache::lonxml::post_evaluate &&$result ne "") {
1.257     albertel  474: 	  my $extras;
                    475: 	  if (!$Apache::lonxml::usestyle) {
                    476: 	      $extras=$Apache::lonxml::style_values;
                    477: 	  }
1.101     albertel  478: 	if ( $#$parstack > -1 ) {
1.257     albertel  479: 	  $result=&Apache::run::evaluate($result,$safeeval,$extras.$$parstack[-1]);
1.101     albertel  480: 	} else {
1.257     albertel  481: 	  $result= &Apache::run::evaluate($result,$safeeval,$extras);
1.101     albertel  482: 	}
1.163     albertel  483:       }
1.282     albertel  484:       $Apache::lonxml::post_evaluate=1;
                    485: 
1.190     albertel  486:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.249     albertel  487: 	  #Style file definitions should be correct
1.250     albertel  488: 	  if ($target eq 'tex' && ($Apache::lonxml::usestyle)) {
1.311     albertel  489: 	      $result=&latex_special_symbols($result);
1.249     albertel  490: 	  }
1.190     albertel  491:       }
                    492: 
1.169     albertel  493:       if ($Apache::lonxml::redirection) {
                    494: 	$Apache::lonxml::outputstack['-1'] .= $result;
                    495:       } else {
                    496: 	$finaloutput.=$result;
                    497:       }
                    498:       $result = '';
                    499: 
1.258     albertel  500:       if ($token->[0] eq 'E' && !$dontpop) {
1.101     albertel  501: 	&end_tag($stack,$parstack,$token);
                    502:       }
1.258     albertel  503:       $dontpop=0;
1.224     albertel  504:     }	
1.212     albertel  505:     if ($#$pars > -1) {
                    506: 	pop @$pars;
                    507: 	pop @Apache::lonxml::pwd;
                    508:     }
1.101     albertel  509:   }
                    510: 
                    511:   # if ($target eq 'meta') {
                    512:   #   $finaloutput.=&endredirection;
                    513:   # }
                    514: 
1.394     albertel  515:   if ( $start && $target eq 'grade') { &endredirection(); }
1.389     albertel  516:   if ( $Apache::lonxml::redirection > $startredirection) {
                    517:       while ($Apache::lonxml::redirection > $startredirection) {
                    518: 	  $finaloutput .= &endredirection();
1.387     albertel  519:       }
                    520:   }
1.101     albertel  521:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
                    522:     $finaloutput=&afterburn($finaloutput);
1.216     sakharuk  523:   }	    
1.101     albertel  524:   return $finaloutput;
                    525: }
1.67      www       526: 
1.318     matthew   527: ## 
                    528: ## Looks to see if there is a subroutine defined for this tag.  If so, call it,
                    529: ## otherwise do not call it as we do not know what it is.
                    530: ##
1.7       albertel  531: sub callsub {
1.84      albertel  532:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel  533:   my $currentstring='';
1.72      albertel  534:   my $nodefault;
1.7       albertel  535:   {
1.59      albertel  536:     my $sub1;
1.7       albertel  537:     no strict 'refs';
1.68      www       538:     my $tag=$token->[1];
1.236     www       539: # get utterly rid of extended html tags
                    540:     if ($tag=~/^x\-/i) { return ''; }
1.141     albertel  541:     my $space=$Apache::lonxml::alltags{$tag}[-1];
1.68      www       542:     if (!$space) {
1.141     albertel  543:      	$tag=~tr/A-Z/a-z/;
1.68      www       544: 	$sub=~tr/A-Z/a-z/;
1.141     albertel  545: 	$space=$Apache::lonxml::alltags{$tag}[-1]
1.68      www       546:     }
1.97      albertel  547: 
                    548:     my $deleted=0;
                    549:     $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
                    550:     if (($token->[0] eq 'S') && ($target eq 'modified')) {
                    551:       $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
                    552: 					     $parstack,$parser,$safeeval,
                    553: 					     $style);
                    554:     }
                    555:     if (!$deleted) {
                    556:       if ($space) {
1.220     albertel  557: 	#&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
1.97      albertel  558: 	$sub1="$space\:\:$sub";
                    559: 	($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
                    560: 					     $parstack,$parser,$safeeval,
                    561: 					     $style);
                    562:       } else {
1.318     matthew   563:           if ($target eq 'tex') {
                    564:               # throw away tag name
                    565:               return '';
                    566:           }
1.220     albertel  567: 	#&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
1.97      albertel  568: 	if ($metamode <1) {
                    569: 	  if (defined($token->[4]) && ($metamode < 1)) {
                    570: 	    $currentstring = $token->[4];
                    571: 	  } else {
                    572: 	    $currentstring = $token->[2];
                    573: 	  }
1.62      sakharuk  574: 	}
1.7       albertel  575:       }
1.97      albertel  576:       #    &Apache::lonxml::debug("nodefalt:$nodefault:");
                    577:       if ($currentstring eq '' && $nodefault eq '') {
                    578: 	if ($target eq 'edit') {
1.220     albertel  579: 	  #&Apache::lonxml::debug("doing default edit for $token->[1]");
1.97      albertel  580: 	  if ($token->[0] eq 'S') {
                    581: 	    $currentstring = &Apache::edit::tag_start($target,$token);
                    582: 	  } elsif ($token->[0] eq 'E') {
                    583: 	    $currentstring = &Apache::edit::tag_end($target,$token);
                    584: 	  }
                    585: 	} elsif ($target eq 'modified') {
                    586: 	  if ($token->[0] eq 'S') {
                    587: 	    $currentstring = $token->[4];
                    588: 	    $currentstring.=&Apache::edit::handle_insert();
1.210     www       589: 	  } elsif ($token->[0] eq 'E') {
                    590: 	    $currentstring = $token->[2];
                    591:             $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
1.97      albertel  592: 	  } else {
                    593: 	    $currentstring = $token->[2];
                    594: 	  }
1.72      albertel  595: 	}
                    596:       }
1.7       albertel  597:     }
                    598:     use strict 'refs';
                    599:   }
                    600:   return $currentstring;
1.82      ng        601: }
                    602: 
1.96      albertel  603: sub setup_globals {
1.172     albertel  604:   my ($request,$target)=@_;
                    605:   $Apache::lonxml::request=$request;
1.99      albertel  606:   $Apache::lonxml::registered = 0;
1.325     www       607:   @Apache::lonxml::htmlareafields=();
1.205     www       608:   $errorcount=0;
                    609:   $warningcount=0;
1.207     albertel  610:   $Apache::lonxml::default_homework_loaded=0;
1.212     albertel  611:   $Apache::lonxml::usestyle=1;
1.204     albertel  612:   &init_counter();
1.101     albertel  613:   @Apache::lonxml::pwd=();
1.124     albertel  614:   @Apache::lonxml::extlinks=();
1.281     albertel  615:   @Apache::lonxml::ssi_info=();
1.282     albertel  616:   $Apache::lonxml::post_evaluate=1;
1.295     albertel  617:   $Apache::lonxml::warnings_error_header='';
1.397     albertel  618:   $Apache::lonxml::substitute_LaTeX_symbols = 1;
1.96      albertel  619:   if ($target eq 'meta') {
                    620:     $Apache::lonxml::redirection = 0;
                    621:     $Apache::lonxml::metamode = 1;
                    622:     $Apache::lonxml::evaluate = 1;
                    623:     $Apache::lonxml::import = 0;
1.129     albertel  624:   } elsif ($target eq 'answer') {
                    625:     $Apache::lonxml::redirection = 0;
                    626:     $Apache::lonxml::metamode = 1;
                    627:     $Apache::lonxml::evaluate = 1;
                    628:     $Apache::lonxml::import = 1;
1.96      albertel  629:   } elsif ($target eq 'grade') {
1.387     albertel  630:     &startredirection(); #ended in inner_xmlparse on exit
1.96      albertel  631:     $Apache::lonxml::metamode = 0;
                    632:     $Apache::lonxml::evaluate = 1;
                    633:     $Apache::lonxml::import = 1;
                    634:   } elsif ($target eq 'modified') {
                    635:     $Apache::lonxml::redirection = 0;
                    636:     $Apache::lonxml::metamode = 0;
                    637:     $Apache::lonxml::evaluate = 0;
                    638:     $Apache::lonxml::import = 0;
                    639:   } elsif ($target eq 'edit') {
                    640:     $Apache::lonxml::redirection = 0;
                    641:     $Apache::lonxml::metamode = 0;
                    642:     $Apache::lonxml::evaluate = 0;
                    643:     $Apache::lonxml::import = 0;
1.163     albertel  644:   } elsif ($target eq 'analyze') {
                    645:     $Apache::lonxml::redirection = 0;
                    646:     $Apache::lonxml::metamode = 0;
                    647:     $Apache::lonxml::evaluate = 1;
                    648:     $Apache::lonxml::import = 1;
1.96      albertel  649:   } else {
                    650:     $Apache::lonxml::redirection = 0;
                    651:     $Apache::lonxml::metamode = 0;
                    652:     $Apache::lonxml::evaluate = 1;
                    653:     $Apache::lonxml::import = 1;
                    654:   }
                    655: }
                    656: 
1.82      ng        657: sub init_safespace {
                    658:   my ($target,$safeeval,$safehole,$safeinit) = @_;
1.393     albertel  659:   $safeeval->deny_only(':dangerous');
                    660:   $safeeval->reval('use Math::Complex;');
1.383     albertel  661:   $safeeval->permit_only(":default");
1.82      ng        662:   $safeeval->permit("entereval");
                    663:   $safeeval->permit(":base_math");
                    664:   $safeeval->permit("sort");
1.286     albertel  665:   $safeeval->permit("time");
1.371     albertel  666:   $safeeval->deny("rand");
                    667:   $safeeval->deny("srand");
1.82      ng        668:   $safeeval->deny(":base_io");
1.102     albertel  669:   $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
1.251     albertel  670:   $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
1.82      ng        671:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.324     albertel  672:   $safehole->wrap(\&Apache::chemresponse::chem_standard_order,$safeeval,
                    673: 		  '&chem_standard_order');
1.369     albertel  674:   $safehole->wrap(\&Apache::response::check_status,$safeeval,'&check_status');
1.324     albertel  675: 
1.82      ng        676:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
                    677:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
                    678:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
                    679:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
                    680:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
                    681:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
                    682:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
                    683:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
                    684:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
                    685:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
                    686:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
                    687:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
                    688:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
                    689:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
                    690:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
                    691:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
                    692:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
                    693:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
                    694:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
1.215     albertel  695:   
                    696:   $safehole->wrap(\&Math::Cephes::bdtr  ,$safeeval,'&bdtr'  );
                    697:   $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
                    698:   $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
                    699:   $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
                    700:   $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
                    701:   $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
                    702:   $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
                    703:   $safehole->wrap(\&Math::Cephes::fdtr  ,$safeeval,'&fdtr'  );
                    704:   $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
                    705:   $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
                    706:   $safehole->wrap(\&Math::Cephes::gdtr  ,$safeeval,'&gdtr'  );
                    707:   $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
                    708:   $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
                    709:   $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
                    710:   $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
                    711:   $safehole->wrap(\&Math::Cephes::ndtr  ,$safeeval,'&ndtr'  );
                    712:   $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
                    713:   $safehole->wrap(\&Math::Cephes::pdtr  ,$safeeval,'&pdtr'  );
                    714:   $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
                    715:   $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
                    716:   $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
                    717:   $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
                    718: 
1.383     albertel  719:   $safehole->wrap(\&Math::Cephes::Matrix::mat,$safeeval,'&mat');
                    720:   $safehole->wrap(\&Math::Cephes::Matrix::new,$safeeval,
                    721: 		  '&Math::Cephes::Matrix::new');
                    722:   $safehole->wrap(\&Math::Cephes::Matrix::coef,$safeeval,
                    723: 		  '&Math::Cephes::Matrix::coef');
                    724:   $safehole->wrap(\&Math::Cephes::Matrix::clr,$safeeval,
                    725: 		  '&Math::Cephes::Matrix::clr');
                    726:   $safehole->wrap(\&Math::Cephes::Matrix::add,$safeeval,
                    727: 		  '&Math::Cephes::Matrix::add');
                    728:   $safehole->wrap(\&Math::Cephes::Matrix::sub,$safeeval,
                    729: 		  '&Math::Cephes::Matrix::sub');
                    730:   $safehole->wrap(\&Math::Cephes::Matrix::mul,$safeeval,
                    731: 		  '&Math::Cephes::Matrix::mul');
                    732:   $safehole->wrap(\&Math::Cephes::Matrix::div,$safeeval,
                    733: 		  '&Math::Cephes::Matrix::div');
                    734:   $safehole->wrap(\&Math::Cephes::Matrix::inv,$safeeval,
                    735: 		  '&Math::Cephes::Matrix::inv');
                    736:   $safehole->wrap(\&Math::Cephes::Matrix::transp,$safeeval,
                    737: 		  '&Math::Cephes::Matrix::transp');
                    738:   $safehole->wrap(\&Math::Cephes::Matrix::simq,$safeeval,
                    739: 		  '&Math::Cephes::Matrix::simq');
                    740:   $safehole->wrap(\&Math::Cephes::Matrix::mat_to_vec,$safeeval,
                    741: 		  '&Math::Cephes::Matrix::mat_to_vec');
                    742:   $safehole->wrap(\&Math::Cephes::Matrix::vec_to_mat,$safeeval,
                    743: 		  '&Math::Cephes::Matrix::vec_to_mat');
                    744:   $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
                    745: 		  '&Math::Cephes::Matrix::check');
                    746:   $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
                    747: 		  '&Math::Cephes::Matrix::check');
                    748: 
1.215     albertel  749: #  $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
                    750: #  $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
                    751: #  $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
                    752: #  $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
                    753: #  $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
                    754: #  $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
                    755: 
1.91      ng        756:   $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
                    757:   $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
                    758:   $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
                    759:   $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
                    760:   $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
                    761:   $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
                    762:   $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
                    763:   $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
                    764:   $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
                    765:   $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
                    766:   $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
1.93      ng        767:   $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
1.91      ng        768:   $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
                    769:   $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
                    770:   $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
                    771:   $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
                    772:   $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
                    773:   $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
                    774:   $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
                    775:   $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
                    776:   $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
1.305     albertel  777:   $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR');
1.311     albertel  778:   $safehole->wrap(\&Apache::lonxml::debug,$safeeval,'&LONCAPA_INTERNAL_DEBUG');
1.322     albertel  779:   $safehole->wrap(\&Apache::caparesponse::get_sigrange,$safeeval,'&LONCAPA_INTERNAL_get_sigrange');
1.91      ng        780: 
1.82      ng        781: #need to inspect this class of ops
                    782: # $safeeval->deny(":base_orig");
1.331     albertel  783:   $safeeval->permit("require");
1.91      ng        784:   $safeinit .= ';$external::target="'.$target.'";';
1.82      ng        785:   &Apache::run::run($safeinit,$safeeval);
1.373     albertel  786:   &initialize_rndseed($safeeval);
                    787: }
1.303     albertel  788: 
1.393     albertel  789: sub clean_safespace {
                    790:     my ($safeeval) = @_;
                    791:     delete_package_recurse($safeeval->{Root});
                    792: }
                    793: 
                    794: sub delete_package_recurse {
                    795:      my ($package) = @_;
                    796:      my @subp;
                    797:      {
                    798: 	 no strict 'refs';
                    799: 	 while (my ($key,$val) = each(%{*{"$package\::"}})) {
                    800: 	     if (!defined($val)) { next; }
                    801: 	     local (*ENTRY) = $val;
                    802: 	     if (defined *ENTRY{HASH} && $key =~ /::$/ &&
                    803: 		 $key ne "main::" && $key ne "<none>::")
                    804: 	     {
                    805: 		 my ($p) = $package ne "main" ? "$package\::" : "";
                    806: 		 ($p .= $key) =~ s/::$//;
                    807: 		 push(@subp,$p);
                    808: 	     }
                    809: 	 }
                    810:      }
                    811:      foreach my $p (@subp) {
                    812: 	 delete_package_recurse($p);
                    813:      }
                    814:      Symbol::delete_package($package);
                    815: }
                    816: 
1.373     albertel  817: sub initialize_rndseed {
                    818:     my ($safeeval)=@_;
                    819:     my $rndseed;
                    820:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    821:     $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
                    822:     my $safeinit = '$external::randomseed="'.$rndseed.'";';
                    823:     &Apache::lonxml::debug("Setting rndseed to $rndseed");
                    824:     &Apache::run::run($safeinit,$safeeval);
1.207     albertel  825: }
                    826: 
                    827: sub default_homework_load {
                    828:     my ($safeeval)=@_;
                    829:     &Apache::lonxml::debug('Loading default_homework');
                    830:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
1.241     albertel  831:     if ($default eq -1) {
1.207     albertel  832: 	&Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
                    833:     } else {
                    834: 	&Apache::run::run($default,$safeeval);
                    835: 	$Apache::lonxml::default_homework_loaded=1;
                    836:     }
1.17      albertel  837: }
                    838: 
1.358     albertel  839: {
                    840:     my $alarm_depth;
                    841:     sub init_alarm {
                    842: 	alarm(0);
                    843: 	$alarm_depth=0;
                    844:     }
                    845: 
                    846:     sub start_alarm {
                    847: 	if ($alarm_depth<1) {
                    848: 	    my $old=alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
                    849: 	    if ($old) {
                    850: 		&Apache::lonxml::error("Cancelled an alarm of $old, this shouldn't occur.");
                    851: 	    }
                    852: 	}
                    853: 	$alarm_depth++;
                    854:     }
                    855: 
                    856:     sub end_alarm {
                    857: 	$alarm_depth--;
                    858: 	if ($alarm_depth<1) { alarm(0); }
                    859:     }
                    860: }
1.328     albertel  861: my $metamode_was;
1.55      albertel  862: sub startredirection {
1.328     albertel  863:     if (!$Apache::lonxml::redirection) {
                    864: 	$metamode_was=$Apache::lonxml::metamode;
                    865:     }
                    866:     $Apache::lonxml::metamode=0;
                    867:     $Apache::lonxml::redirection++;
                    868:     push (@Apache::lonxml::outputstack, '');
1.55      albertel  869: }
                    870: 
                    871: sub endredirection {
1.328     albertel  872:     if (!$Apache::lonxml::redirection) {
1.380     www       873: 	&Apache::lonxml::error("Endredirection was called before a startredirection, perhaps you have unbalanced tags. Some debugging information:".join ":",caller);
1.328     albertel  874: 	return '';
                    875:     }
                    876:     $Apache::lonxml::redirection--;
                    877:     if (!$Apache::lonxml::redirection) {
                    878: 	$Apache::lonxml::metamode=$metamode_was;
                    879:     }
                    880:     pop @Apache::lonxml::outputstack;
1.97      albertel  881: }
                    882: 
                    883: sub end_tag {
                    884:   my ($tagstack,$parstack,$token)=@_;
                    885:   pop(@$tagstack);
                    886:   pop(@$parstack);
                    887:   &decreasedepth($token);
1.55      albertel  888: }
                    889: 
1.17      albertel  890: sub initdepth {
                    891:   @Apache::lonxml::depthcounter=();
                    892:   $Apache::lonxml::depth=-1;
                    893:   $Apache::lonxml::olddepth=-1;
                    894: }
                    895: 
1.339     albertel  896: my @timers;
                    897: my $lasttime;
1.17      albertel  898: sub increasedepth {
1.19      albertel  899:   my ($token) = @_;
1.17      albertel  900:   $Apache::lonxml::depth++;
                    901:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
                    902:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
                    903:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
                    904:   }
1.340     albertel  905:   my $time;
                    906:   if ($Apache::lonxml::debug eq "1") {
                    907:       push(@timers,[&gettimeofday()]);
                    908:       $time=&tv_interval($lasttime);
                    909:       $lasttime=[&gettimeofday()];
                    910:   }
1.339     albertel  911:   my $spacing='  'x($Apache::lonxml::depth-1);
1.42      albertel  912:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.339     albertel  913:   &Apache::lonxml::debug("s$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1] : $time : \n");
1.54      albertel  914: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17      albertel  915: }
                    916: 
                    917: sub decreasedepth {
1.19      albertel  918:   my ($token) = @_;
1.17      albertel  919:   $Apache::lonxml::depth--;
1.36      albertel  920:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
                    921:     $#Apache::lonxml::depthcounter--;
                    922:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
                    923:   }
1.43      albertel  924:   if (  $Apache::lonxml::depth < -1) {
1.280     www       925:     &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));
1.43      albertel  926:     $Apache::lonxml::depth='-1';
                    927:   }
1.340     albertel  928:   my ($timer,$time);
                    929:   if ($Apache::lonxml::debug eq "1") {
                    930:       $timer=pop(@timers);
                    931:       $time=&tv_interval($lasttime);
                    932:       $lasttime=[&gettimeofday()];
                    933:   }
1.339     albertel  934:   my $spacing='  'x$Apache::lonxml::depth;
1.42      albertel  935:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.339     albertel  936:   &Apache::lonxml::debug("e$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1] : $time : ".&tv_interval($timer)."\n");
1.54      albertel  937: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1       sakharuk  938: }
1.19      albertel  939: 
1.399     albertel  940: sub get_id {
                    941:     my ($parstack,$safeeval)=@_;
                    942:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
                    943:     if ($env{'request.state'} eq 'construct' && $id =~ /(\.|_)/) {
                    944: 	&error(&mt("IDs are not allowed to contain &quot;<tt>_</tt>&quot; or &quot;<tt>.</tt>&quot;"));
                    945:     }
                    946:     if ($id =~ /^\s*$/) { $id = $Apache::lonxml::curdepth; }
                    947:     return $id;
                    948: }
                    949: 
1.180     albertel  950: sub get_all_text_unbalanced {
1.190     albertel  951: #there is a copy of this in lonpublisher.pm
1.326     albertel  952:     my($tag,$pars)= @_;
                    953:     my $token;
                    954:     my $result='';
                    955:     $tag='<'.$tag.'>';
                    956:     while ($token = $$pars[-1]->get_token) {
                    957: 	if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1.386     albertel  958: 	    if ($token->[0] eq 'T' && $token->[2]) {
1.382     albertel  959: 		$result.='<![CDATA['.$token->[1].']]>';
                    960: 	    } else {
                    961: 		$result.=$token->[1];
                    962: 	    }
1.326     albertel  963: 	} elsif ($token->[0] eq 'PI') {
                    964: 	    $result.=$token->[2];
                    965: 	} elsif ($token->[0] eq 'S') {
                    966: 	    $result.=$token->[4];
                    967: 	} elsif ($token->[0] eq 'E')  {
                    968: 	    $result.=$token->[2];
                    969: 	}
                    970: 	if ($result =~ /\Q$tag\E/is) {
                    971: 	    ($result,my $redo)=$result =~ /(.*)\Q$tag\E(.*)/is;
                    972: 	    #&Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
                    973: 	    #&Apache::lonxml::debug('Result is :'.$1);
                    974: 	    $redo=$tag.$redo;
                    975: 	    &Apache::lonxml::newparser($pars,\$redo);
                    976: 	    last;
                    977: 	}
                    978:     }
                    979:     return $result
1.204     albertel  980: }
                    981: 
                    982: sub increment_counter {
1.247     albertel  983:     my ($increment) = @_;
1.289     sakharuk  984:     if (defined($increment) && $increment gt 0) {
                    985: 	$Apache::lonxml::counter+=$increment;
                    986:     } else {
                    987: 	$Apache::lonxml::counter++;
1.247     albertel  988:     }
1.289     sakharuk  989:     $Apache::lonxml::counter_changed=1;
1.204     albertel  990: }
                    991: 
                    992: sub init_counter {
1.391     albertel  993:     if ($env{'request.state'} eq 'construct') {
                    994: 	$Apache::lonxml::counter=1;
                    995: 	$Apache::lonxml::counter_changed=1;
                    996:     } elsif (defined($env{'form.counter'})) {
1.372     albertel  997: 	$Apache::lonxml::counter=$env{'form.counter'};
1.247     albertel  998: 	$Apache::lonxml::counter_changed=0;
1.237     sakharuk  999:     } else {
1.204     albertel 1000: 	$Apache::lonxml::counter=1;
1.247     albertel 1001: 	$Apache::lonxml::counter_changed=1;
1.204     albertel 1002:     }
                   1003: }
                   1004: 
                   1005: sub store_counter {
                   1006:     &Apache::lonnet::appenv(('form.counter' => $Apache::lonxml::counter));
1.401     albertel 1007:     $Apache::lonxml::counter_changed=0;
1.204     albertel 1008:     return '';
1.180     albertel 1009: }
                   1010: 
1.398     albertel 1011: {
                   1012:     my $state;
                   1013:     sub clear_problem_counter {
                   1014: 	undef($state);
                   1015: 	&Apache::lonnet::delenv('form.counter');
                   1016: 	&Apache::lonxml::init_counter();
                   1017: 	&Apache::lonxml::store_counter();
                   1018:     }
                   1019: 
                   1020:     sub remember_problem_counter {
                   1021: 	&Apache::lonnet::transfer_profile_to_env();
                   1022: 	$state = $env{'form.counter'};
                   1023:     }
                   1024: 
                   1025:     sub restore_problem_counter {
                   1026: 	if (defined($state)) {
                   1027: 	    &Apache::lonnet::appenv(('form.counter' => $state));
                   1028: 	}
                   1029:     }
1.401     albertel 1030:     sub get_problem_counter {
                   1031: 	if ($Apache::lonxml::counter_changed) { &store_counter() }
                   1032: 	&Apache::lonnet::transfer_profile_to_env();
                   1033: 	return $env{'form.counter'};
                   1034:     }
1.398     albertel 1035: }
                   1036: 
1.19      albertel 1037: sub get_all_text {
1.270     albertel 1038:     my($tag,$pars,$style)= @_;
                   1039:     my $gotfullstack=1;
                   1040:     if (ref($pars) ne 'ARRAY') {
                   1041: 	$gotfullstack=0;
                   1042: 	$pars=[$pars];
                   1043:     }
                   1044:     if (ref($style) ne 'HASH') {
                   1045: 	$style={};
                   1046:     }
                   1047:     my $depth=0;
                   1048:     my $token;
                   1049:     my $result='';
                   1050:     if ( $tag =~ m:^/: ) { 
                   1051: 	my $tag=substr($tag,1); 
                   1052: 	#&Apache::lonxml::debug("have:$tag:");
                   1053: 	my $top_empty=0;
                   1054: 	while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
                   1055: 	    while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
                   1056: 		#&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
                   1057: 		if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1.382     albertel 1058: 		    if ($token->[2]) {
                   1059: 			$result.='<![CDATA['.$token->[1].']]>';
                   1060: 		    } else {
                   1061: 			$result.=$token->[1];
                   1062: 		    }
1.270     albertel 1063: 		} elsif ($token->[0] eq 'PI') {
                   1064: 		    $result.=$token->[2];
                   1065: 		} elsif ($token->[0] eq 'S') {
1.316     albertel 1066: 		    if ($token->[1] =~ /^\Q$tag\E$/i) { $depth++; }
                   1067: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
                   1068: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1.270     albertel 1069: 		    $result.=$token->[4];
                   1070: 		} elsif ($token->[0] eq 'E')  {
1.316     albertel 1071: 		    if ( $token->[1] =~ /^\Q$tag\E$/i) { $depth--; }
1.270     albertel 1072: 		    #skip sending back the last end tag
1.283     albertel 1073: 		    if ($depth == 0 && exists($$style{'/'.$token->[1]}) && $Apache::lonxml::usestyle) {
1.270     albertel 1074: 			my $string=
                   1075: 			    '<LONCAPA_INTERNAL_TURN_STYLE_OFF end="yes" />'.
                   1076: 				$$style{'/'.$token->[1]}.
                   1077: 				    $token->[2].
                   1078: 					'<LONCAPA_INTERNAL_TURN_STYLE_ON />';
                   1079: 			&Apache::lonxml::newparser($pars,\$string);
                   1080: 			#&Apache::lonxml::debug("reParsing $string");
                   1081: 			next;
                   1082: 		    }
                   1083: 		    if ($depth > -1) {
                   1084: 			$result.=$token->[2];
                   1085: 		    } else {
                   1086: 			$$pars[-1]->unget_token($token);
                   1087: 		    }
                   1088: 		}
                   1089: 	    }
                   1090: 	    if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
                   1091: 	    if (($depth >=0) && ($#$pars > 0) ) {
                   1092: 		pop(@$pars);
                   1093: 		pop(@Apache::lonxml::pwd);
                   1094: 	    }
                   1095: 	}
                   1096: 	if ($top_empty && $depth >= 0) {
                   1097: 	    #never found the end tag ran out of text, throw error send back blank
                   1098: 	    &error('Never found end tag for &lt;'.$tag.
                   1099: 		   '&gt; current string <pre>'.
1.314     albertel 1100: 		   &HTML::Entities::encode($result,'<>&"').
1.270     albertel 1101: 		   '</pre>');
                   1102: 	    if ($gotfullstack) {
                   1103: 		my $newstring='</'.$tag.'>'.$result;
                   1104: 		&Apache::lonxml::newparser($pars,\$newstring);
                   1105: 	    }
                   1106: 	    $result='';
                   1107: 	}
                   1108:     } else {
                   1109: 	while ($#$pars > -1) {
                   1110: 	    while ($token = $$pars[-1]->get_token) {
                   1111: 		#&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
                   1112: 		if (($token->[0] eq 'T')||($token->[0] eq 'C')||
                   1113: 		    ($token->[0] eq 'D')) {
1.382     albertel 1114: 		    if ($token->[2]) {
                   1115: 			$result.='<![CDATA['.$token->[1].']]>';
                   1116: 		    } else {
                   1117: 			$result.=$token->[1];
                   1118: 		    }
1.270     albertel 1119: 		} elsif ($token->[0] eq 'PI') {
                   1120: 		    $result.=$token->[2];
                   1121: 		} elsif ($token->[0] eq 'S') {
1.316     albertel 1122: 		    if ( $token->[1] =~ /^\Q$tag\E$/i) {
1.270     albertel 1123: 			$$pars[-1]->unget_token($token); last;
                   1124: 		    } else {
                   1125: 			$result.=$token->[4];
                   1126: 		    }
1.316     albertel 1127: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
                   1128: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1.270     albertel 1129: 		} elsif ($token->[0] eq 'E')  {
                   1130: 		    $result.=$token->[2];
                   1131: 		}
                   1132: 	    }
                   1133: 	    if (($#$pars > 0) ) {
                   1134: 		pop(@$pars);
                   1135: 		pop(@Apache::lonxml::pwd);
                   1136: 	    } else { last; }
                   1137: 	}
                   1138:     }
                   1139:     #&Apache::lonxml::debug("Exit:$result:");
                   1140:     return $result
1.19      albertel 1141: }
                   1142: 
1.23      albertel 1143: sub newparser {
                   1144:   my ($parser,$contentref,$dir) = @_;
1.167     albertel 1145:   push (@$parser,HTML::LCParser->new($contentref));
1.365     albertel 1146:   $$parser[-1]->xml_mode(1);
                   1147:   $$parser[-1]->marked_sections(1);
1.23      albertel 1148:   if ( $dir eq '' ) {
                   1149:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
                   1150:   } else {
                   1151:     push (@Apache::lonxml::pwd, $dir);
                   1152:   } 
                   1153: }
1.1       sakharuk 1154: 
1.8       albertel 1155: sub parstring {
                   1156:   my ($token) = @_;
                   1157:   my $temp='';
1.142     albertel 1158:   foreach (@{$token->[3]}) {
1.35      www      1159:     unless ($_=~/\W/) {
1.42      albertel 1160:       my $val=$token->[2]->{$_};
1.231     albertel 1161:       $val =~ s/([\%\@\\\"\'])/\\$1/g;
1.342     albertel 1162:       $val =~ s/(\$[^{a-zA-Z_])/\\$1/g;
1.346     albertel 1163:       $val =~ s/(\$)$/\\$1/;
1.51      albertel 1164:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.267     sakharuk 1165:       $temp .= "my \$$_=\"$val\";";
1.20      albertel 1166:     }
1.142     albertel 1167:   }
1.8       albertel 1168:   return $temp;
                   1169: }
1.22      albertel 1170: 
1.384     albertel 1171: sub extlink {
                   1172:     my ($res,$exact)=@_;
                   1173:     if (!$exact) {
                   1174: 	$res=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$res);
                   1175:     }
                   1176:     push(@Apache::lonxml::extlinks,$res)	 
                   1177: }
                   1178: 
1.34      www      1179: sub writeallows {
1.126     www      1180:     unless ($#extlinks>=0) { return; }
1.377     albertel 1181:     my $thisurl = &Apache::lonnet::clutter(shift);
1.372     albertel 1182:     if ($env{'httpref.'.$thisurl}) {
                   1183: 	$thisurl=$env{'httpref.'.$thisurl};
1.111     www      1184:     }
1.34      www      1185:     my $thisdir=$thisurl;
                   1186:     $thisdir=~s/\/[^\/]+$//;
                   1187:     my %httpref=();
1.142     albertel 1188:     foreach (@extlinks) {
1.34      www      1189:        $httpref{'httpref.'.
1.125     www      1190:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1.142     albertel 1191:     }
1.126     www      1192:     @extlinks=();
1.34      www      1193:     &Apache::lonnet::appenv(%httpref);
                   1194: }
                   1195: 
1.281     albertel 1196: sub register_ssi {
                   1197:     my ($url,%form)=@_;
                   1198:     push (@Apache::lonxml::ssi_info,{'url'=>$url,'form'=>\%form});
                   1199:     return '';
                   1200: }
                   1201: 
                   1202: sub do_registered_ssi {
                   1203:     foreach my $info (@Apache::lonxml::ssi_info) {
                   1204: 	my %form=%{ $info->{'form'}};
                   1205: 	my $url=$info->{'url'};
                   1206: 	&Apache::lonnet::ssi($url,%form);
                   1207:     }
                   1208: }
1.66      www      1209: #
                   1210: # Afterburner handles anchors, highlights and links
                   1211: #
                   1212: sub afterburn {
                   1213:     my $result=shift;
1.154     albertel 1214:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1215: 					    ['highlight','anchor','link']);
1.372     albertel 1216:     if ($env{'form.highlight'}) {
                   1217:        foreach (split(/\,/,$env{'form.highlight'})) {
1.66      www      1218:            my $anchorname=$_;
                   1219: 	   my $matchthis=$anchorname;
                   1220:            $matchthis=~s/\_+/\\s\+/g;
1.317     albertel 1221:            $result=~s/(\Q$matchthis\E)/\<font color=\"red\"\>$1\<\/font\>/gs;
1.142     albertel 1222:        }
1.66      www      1223:     }
1.372     albertel 1224:     if ($env{'form.link'}) {
                   1225:        foreach (split(/\,/,$env{'form.link'})) {
1.66      www      1226:            my ($anchorname,$linkurl)=split(/\>/,$_);
                   1227: 	   my $matchthis=$anchorname;
                   1228:            $matchthis=~s/\_+/\\s\+/g;
1.317     albertel 1229:            $result=~s/(\Q$matchthis\E)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1.142     albertel 1230:        }
1.66      www      1231:     }
1.372     albertel 1232:     if ($env{'form.anchor'}) {
                   1233:         my $anchorname=$env{'form.anchor'};
1.66      www      1234: 	my $matchthis=$anchorname;
                   1235:         $matchthis=~s/\_+/\\s\+/g;
1.317     albertel 1236:         $result=~s/(\Q$matchthis\E)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1.66      www      1237:         $result.=(<<"ENDSCRIPT");
1.226     albertel 1238: <script type="text/javascript">
1.66      www      1239:     document.location.hash='$anchorname';
                   1240: </script>
                   1241: ENDSCRIPT
                   1242:     }
                   1243:     return $result;
                   1244: }
                   1245: 
1.79      www      1246: sub storefile {
                   1247:     my ($file,$contents)=@_;
1.290     albertel 1248:     &Apache::lonnet::correct_line_ends(\$contents);
1.79      www      1249:     if (my $fh=Apache::File->new('>'.$file)) {
                   1250: 	print $fh $contents;
                   1251:         $fh->close();
1.271     www      1252:         return 1;
1.147     albertel 1253:     } else {
1.271     www      1254: 	&warning("Unable to save file $file");
                   1255: 	return 0;
1.79      www      1256:     }
                   1257: }
                   1258: 
1.151     albertel 1259: sub createnewhtml {
1.321     www      1260:     my $title=&mt('Title of document goes here');
                   1261:     my $body=&mt('Body of document goes here');
                   1262:     my $filecontents=(<<SIMPLECONTENT);
1.78      www      1263: <html>
                   1264: <head>
1.321     www      1265: <title>$title</title>
1.78      www      1266: </head>
                   1267: <body bgcolor="#FFFFFF">
1.321     www      1268: $body
1.78      www      1269: </body>
                   1270: </html>
                   1271: SIMPLECONTENT
1.321     www      1272:     return $filecontents;
1.151     albertel 1273: }
                   1274: 
1.274     albertel 1275: sub createnewsty {
                   1276:   my $filecontents=(<<SIMPLECONTENT);
                   1277: <definetag name="">
                   1278:     <render>
                   1279:        <web></web>
                   1280:        <tex></tex>
                   1281:     </render>
                   1282: </definetag>
                   1283: SIMPLECONTENT
                   1284:   return $filecontents;
                   1285: }
                   1286: 
1.147     albertel 1287: 
1.151     albertel 1288: sub inserteditinfo {
1.274     albertel 1289:       my ($result,$filecontents,$filetype)=@_;
1.314     albertel 1290:       $filecontents = &HTML::Entities::encode($filecontents,'<>&"');
1.147     albertel 1291: #      my $editheader='<a href="#editsection">Edit below</a><hr />';
1.274     albertel 1292:       my $xml_help = '';
1.321     www      1293:       my $initialize='';
1.274     albertel 1294:       if ($filetype eq 'html') {
1.323     www      1295: 	  my $addbuttons=&Apache::lonhtmlcommon::htmlareaaddbuttons();
1.333     www      1296: 	  $initialize=&Apache::lonhtmlcommon::htmlareaheaders().
1.347     albertel 1297: 	      &Apache::lonhtmlcommon::spellheader();
                   1298: 	  if (!&Apache::lonhtmlcommon::htmlareablocked() &&
                   1299: 	      &Apache::lonhtmlcommon::htmlareabrowser()) {
                   1300: 	      $initialize.=(<<FULLPAGE);
1.321     www      1301: <script type="text/javascript">
1.323     www      1302: $addbuttons
                   1303: 
1.321     www      1304:     HTMLArea.loadPlugin("FullPage");
                   1305: 
                   1306:     function initDocument() {
1.323     www      1307: 	var editor=new HTMLArea("filecont",config);
1.321     www      1308: 	editor.registerPlugin(FullPage);
                   1309: 	editor.generate();
                   1310:     }
                   1311: </script>
                   1312: FULLPAGE
1.347     albertel 1313:           } else {
                   1314: 	      $initialize.=(<<FULLPAGE);
                   1315: <script type="text/javascript">
                   1316: $addbuttons
                   1317:     function initDocument() {
                   1318:     }
                   1319: </script>
                   1320: FULLPAGE
                   1321: 	  }
1.321     www      1322:           $result=~s/\<body([^\>]*)\>/\<body onload="initDocument()" $1\>/i;
                   1323: 	  $xml_help=&Apache::loncommon::helpLatexCheatsheet();
1.274     albertel 1324:       }
                   1325:       my $cleanbut = '';
1.374     www      1326: 
1.254     albertel 1327:       my $titledisplay=&display_title();
1.280     www      1328:       my %lt=&Apache::lonlocal::texthash('st' => 'Save this',
                   1329: 					 'vi' => 'View',
                   1330: 					 'ed' => 'Edit');
1.161     albertel 1331:       my $buttons=(<<BUTTONS);
1.274     albertel 1332: $cleanbut
1.304     matthew  1333: <input type="submit" name="savethisfile" accesskey="s"  value="$lt{'st'}" />
                   1334: <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
1.161     albertel 1335: BUTTONS
1.333     www      1336:       $buttons.=&Apache::lonhtmlcommon::spelllink('xmledit','filecont');
1.338     albertel 1337:       $buttons.=&Apache::lonhtmlcommon::htmlareaselectactive('filecont');
1.78      www      1338:       my $editfooter=(<<ENDFOOTER);
1.321     www      1339: $initialize
1.78      www      1340: <hr />
                   1341: <a name="editsection" />
1.333     www      1342: <form method="post" name="xmledit">
1.240     albertel 1343: $xml_help
1.280     www      1344: <input type="hidden" name="editmode" value="$lt{'ed'}" />
1.170     www      1345: $buttons<br />
1.366     albertel 1346: <textarea style="width:100%" cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
1.170     www      1347: <br />$buttons
1.78      www      1348: <br />
                   1349: </form>
1.254     albertel 1350: $titledisplay
1.321     www      1351: </body>
1.78      www      1352: ENDFOOTER
1.147     albertel 1353: #      $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1.78      www      1354:       $result=~s/(\<\/body\>)/$editfooter/is;
                   1355:       return $result;
                   1356: }
                   1357: 
1.152     albertel 1358: sub get_target {
1.372     albertel 1359:   my $viewgrades=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
                   1360:   if ( $env{'request.state'} eq 'published') {
                   1361:     if ( defined($env{'form.grade_target'})
1.152     albertel 1362: 	 && ($viewgrades == 'F' )) {
1.372     albertel 1363:       return ($env{'form.grade_target'});
                   1364:     } elsif (defined($env{'form.grade_target'})) {
                   1365:       if (($env{'form.grade_target'} eq 'web') ||
                   1366: 	  ($env{'form.grade_target'} eq 'tex') ) {
                   1367: 	return $env{'form.grade_target'}
1.153     albertel 1368:       } else {
                   1369: 	return 'web';
                   1370:       }
1.152     albertel 1371:     } else {
                   1372:       return 'web';
                   1373:     }
1.372     albertel 1374:   } elsif ($env{'request.state'} eq 'construct') {
                   1375:     if ( defined($env{'form.grade_target'})) {
                   1376:       return ($env{'form.grade_target'});
1.152     albertel 1377:     } else {
                   1378:       return 'web';
                   1379:     }
                   1380:   } else {
                   1381:     return 'web';
                   1382:   }
                   1383: }
                   1384: 
1.24      sakharuk 1385: sub handler {
1.255     sakharuk 1386:     my $request=shift;
                   1387:     
                   1388:     my $target=&get_target();
                   1389:     
1.372     albertel 1390:     $Apache::lonxml::debug=$env{'user.debug'};
1.255     sakharuk 1391:     
1.364     albertel 1392:     &Apache::loncommon::content_type($request,'text/html');
1.255     sakharuk 1393:     &Apache::loncommon::no_cache($request);
1.372     albertel 1394:     if ($env{'request.state'} eq 'published') {
1.363     albertel 1395: 	$request->set_last_modified(&Apache::lonnet::metadata($request->uri,
                   1396: 							      'lastrevisiondate'));
                   1397:     }
1.255     sakharuk 1398:     $request->send_http_header;
                   1399:     
                   1400:     return OK if $request->header_only;
1.68      www      1401: 
                   1402: 
1.255     sakharuk 1403:     my $file=&Apache::lonnet::filelocation("",$request->uri);
1.274     albertel 1404:     my $filetype;
                   1405:     if ($file =~ /\.sty$/) {
                   1406: 	$filetype='sty';
                   1407:     } else {
                   1408: 	$filetype='html';
                   1409:     }
1.78      www      1410: #
                   1411: # Edit action? Save file.
                   1412: #
1.372     albertel 1413:     unless ($env{'request.state'} eq 'published') {
1.374     www      1414: 	if ($env{'form.savethisfile'}) {
1.372     albertel 1415: 	    if (&storefile($file,$env{'form.filecont'})) {
1.309     albertel 1416: 		&Apache::lonxml::info("<font COLOR=\"#0000FF\">".
                   1417: 				      &mt('Updated').": ".
                   1418: 				      &Apache::lonlocal::locallocaltime(time).
                   1419: 				      " </font>");
1.271     www      1420: 	    } 
1.255     sakharuk 1421: 	}
                   1422:     }
                   1423:     my %mystyle;
                   1424:     my $result = '';
                   1425:     my $filecontents=&Apache::lonnet::getfile($file);
                   1426:     if ($filecontents eq -1) {
1.402     albertel 1427: 	my $start_page=&Apache::loncommon::start_page('File Error');
                   1428: 	my $end_page=&Apache::loncommon::end_page('File Error');
1.284     www      1429: 	my $fnf=&mt('File not found');
1.255     sakharuk 1430: 	$result=(<<ENDNOTFOUND);
1.402     albertel 1431: $start_page
1.284     www      1432: <b>$fnf: $file</b>
1.402     albertel 1433: $end_page
1.78      www      1434: ENDNOTFOUND
1.343     albertel 1435:         $filecontents='';
1.372     albertel 1436: 	if ($env{'request.state'} ne 'published') {
1.274     albertel 1437: 	    if ($filetype eq 'sty') {
                   1438: 		$filecontents=&createnewsty();
                   1439: 	    } else {
                   1440: 		$filecontents=&createnewhtml();
                   1441: 	    }
1.372     albertel 1442: 	    $env{'form.editmode'}='Edit'; #force edit mode
1.255     sakharuk 1443: 	}
                   1444:     } else {
1.372     albertel 1445: 	unless ($env{'request.state'} eq 'published') {
1.343     albertel 1446: 	    if ($filecontents=~/BEGIN LON-CAPA Internal/) {
1.381     www      1447: 		&Apache::lonxml::error(&mt('This file appears to be a rendering of a LON-CAPA resource. If this is correct, this resource will act very oddly and incorrectly.'));
1.343     albertel 1448: 	    }
1.264     www      1449: #
                   1450: # we are in construction space, see if edit mode forced
1.385     albertel 1451:             &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1452: 						    ['editmode']);
1.255     sakharuk 1453: 	}
1.372     albertel 1454: 	if (!$env{'form.editmode'} || $env{'form.viewmode'}) {
1.255     sakharuk 1455: 	    $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
                   1456: 						'',%mystyle);
1.368     albertel 1457: 	    undef($Apache::lonhomework::parsing_a_task);
1.385     albertel 1458: 	    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1459: 						    ['rawmode']);
1.395     albertel 1460: 	    if ($env{'form.rawmode'}) { $result = $filecontents; }
1.255     sakharuk 1461: 	}
1.147     albertel 1462:     }
1.255     sakharuk 1463:     
1.78      www      1464: #
                   1465: # Edit action? Insert editing commands
                   1466: #
1.372     albertel 1467:     unless ($env{'request.state'} eq 'published') {
                   1468: 	if ($env{'form.editmode'} && (!($env{'form.viewmode'}))) {
1.255     sakharuk 1469: 	    my $displayfile=$request->uri;
                   1470: 	    $displayfile=~s/^\/[^\/]*//;
1.402     albertel 1471: 	    my %options = ();
                   1472: 	    if ($env{'environment.remote'} ne 'off') {
                   1473: 		$options{'bgcolor'}   = '#FFFFFF';
1.349     albertel 1474: 	    }
1.402     albertel 1475: 	    my $start_page = &Apache::loncommon::start_page(undef,undef,
                   1476: 							    \%options);
                   1477: 	    $result=$start_page.
1.309     albertel 1478: 		&Apache::lonxml::message_location().'<h3>'.
                   1479: 		$displayfile.
1.402     albertel 1480: 		'</h3>'.&Apache::loncommon::end_page();
1.274     albertel 1481: 	    $result=&inserteditinfo($result,$filecontents,$filetype);
1.255     sakharuk 1482: 	}
1.147     albertel 1483:     }
1.402     albertel 1484:     if ($filetype eq 'html') { &writeallows($request->uri); }
1.274     albertel 1485: 	
1.255     sakharuk 1486:     
1.309     albertel 1487:     &Apache::lonxml::add_messages(\$result);
1.255     sakharuk 1488:     $request->print($result);
                   1489:     
                   1490:     return OK;
1.253     albertel 1491: }
                   1492: 
                   1493: sub display_title {
                   1494:     my $result;
1.372     albertel 1495:     if ($env{'request.state'} eq 'construct') {
1.253     albertel 1496: 	my $title=&Apache::lonnet::gettitle();
                   1497: 	if (!defined($title) || $title eq '') {
1.372     albertel 1498: 	    $title = $env{'request.filename'};
1.253     albertel 1499: 	    $title = substr($title, rindex($title, '/') + 1);
                   1500: 	}
                   1501: 	$result = "<script type='text/javascript'>top.document.title = '$title - LON-CAPA Construction Space';</script>";
                   1502:     }
                   1503:     return $result;
1.24      sakharuk 1504: }
1.147     albertel 1505: 
1.22      albertel 1506: sub debug {
1.298     albertel 1507:     if ($Apache::lonxml::debug eq "1") {
                   1508: 	$|=1;
1.300     albertel 1509: 	my $request=$Apache::lonxml::request;
1.388     albertel 1510: 	if (!$request) {
                   1511: 	    eval { $request=Apache->request; };
                   1512: 	}
                   1513: 	if (!$request) {
                   1514: 	    eval { $request=Apache2::RequestUtil->request; };
                   1515: 	}
1.314     albertel 1516: 	$request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");
1.346     albertel 1517: 	#&Apache::lonnet::logthis($_[0]);
1.298     albertel 1518:     }
1.22      albertel 1519: }
1.49      albertel 1520: 
1.348     albertel 1521: sub show_error_warn_msg {
1.372     albertel 1522:     if ($env{'request.filename'} eq '/home/httpd/html/res/lib/templates/simpleproblem.problem' &&
                   1523: 	&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
1.351     albertel 1524: 	return 1;
                   1525:     }
1.348     albertel 1526:     return (($Apache::lonxml::debug eq 1) ||
1.372     albertel 1527: 	    ($env{'request.state'} eq 'construct') ||
1.348     albertel 1528: 	    ($Apache::lonhomework::browse eq 'F'
                   1529: 	     &&
1.372     albertel 1530: 	     $env{'form.show_errors'} eq 'on'));
1.348     albertel 1531: }
                   1532: 
1.22      albertel 1533: sub error {
1.336     albertel 1534:     $errorcount++;
1.348     albertel 1535:     if ( &show_error_warn_msg() ) {
1.336     albertel 1536: 	# If printing in construction space, put the error inside <pre></pre>
                   1537: 	push(@Apache::lonxml::error_messages,
                   1538: 	     $Apache::lonxml::warnings_error_header.
                   1539: 	     "<b>ERROR:</b>".join("<br />\n",@_)."<br />\n");
                   1540: 	$Apache::lonxml::warnings_error_header='';
                   1541:     } else {
                   1542: 	my $errormsg;
                   1543: 	my ($symb)=&Apache::lonnet::symbread();
                   1544: 	if ( !$symb ) {
                   1545: 	    #public or browsers
                   1546: 	    $errormsg=&mt("An error occured while processing this resource. The author has been notified.");
1.403     albertel 1547: 	}
1.404     albertel 1548: 	my $msg = join('<br />',@_);
1.336     albertel 1549: 	#notify author
1.403     albertel 1550: 	&Apache::lonmsg::author_res_msg($env{'request.filename'},$msg);
1.336     albertel 1551: 	#notify course
1.372     albertel 1552: 	if ( $symb && $env{'request.course.id'} ) {
1.380     www      1553: 	    my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1554: 	    my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.336     albertel 1555: 	    my (undef,%users)=&Apache::lonfeedback::decide_receiver(undef,0,1,1,1);
1.372     albertel 1556: 	    my $declutter=&Apache::lonnet::declutter($env{'request.filename'});
1.336     albertel 1557: 	    my @userlist;
                   1558: 	    foreach (keys %users) {
                   1559: 		my ($user,$domain) = split(/:/, $_);
                   1560: 		push(@userlist,"$user\@$domain");
1.380     www      1561: 		my $key=$declutter.'_'.$user.'_'.$domain;
                   1562: 		my %lastnotified=&Apache::lonnet::get('nohist_xmlerrornotifications',
                   1563: 						      [$key],
                   1564: 						      $cdom,$cnum);
                   1565: 		my $now=time;
                   1566: 		if ($now-$lastnotified{$key}>86400) {
                   1567: 		    &Apache::lonmsg::user_normal_msg($user,$domain,
1.403     albertel 1568: 						 "Error [$declutter]",$msg);
1.380     www      1569: 		    &Apache::lonnet::put('nohist_xmlerrornotifications',
                   1570: 					 {$key => $now},
                   1571: 					 $cdom,$cnum);		
                   1572: 		}
1.336     albertel 1573: 	    }
1.372     albertel 1574: 	    if ($env{'request.role.adv'}) {
1.336     albertel 1575: 		$errormsg=&mt("An error occured while processing this resource. The course personnel ([_1]) and the author have been notified.",join(', ',@userlist));
                   1576: 	    } else {
                   1577: 		$errormsg=&mt("An error occured while processing this resource. The instructor has been notified.");
                   1578: 	    }
                   1579: 	}
                   1580: 	push(@Apache::lonxml::error_messages,"<b>$errormsg</b> <br />");
1.52      albertel 1581:     }
1.22      albertel 1582: }
1.49      albertel 1583: 
1.22      albertel 1584: sub warning {
1.295     albertel 1585:     $warningcount++;
1.261     albertel 1586:   
1.372     albertel 1587:     if ($env{'form.grade_target'} ne 'tex') {
1.348     albertel 1588: 	if ( &show_error_warn_msg() ) {
1.309     albertel 1589: 	    push(@Apache::lonxml::warning_messages,
                   1590: 		 $Apache::lonxml::warnings_error_header.
                   1591: 		 "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n");
1.295     albertel 1592: 	    $Apache::lonxml::warnings_error_header='';
                   1593: 	}
                   1594:     }
1.309     albertel 1595: }
                   1596: 
                   1597: sub info {
1.372     albertel 1598:     if ($env{'form.grade_target'} ne 'tex' 
                   1599: 	&& $env{'request.state'} eq 'construct') {
1.309     albertel 1600: 	push(@Apache::lonxml::info_messages,join('<br />',@_)."<br />\n");
                   1601:     }
                   1602: }
                   1603: 
                   1604: sub message_location {
                   1605:     return '__LONCAPA_INTERNAL_MESSAGE_LOCATION__';
                   1606: }
                   1607: 
                   1608: sub add_messages {
                   1609:     my ($msg)=@_;
                   1610:     my $result=join(' ',
                   1611: 		    @Apache::lonxml::info_messages,
                   1612: 		    @Apache::lonxml::error_messages,
                   1613: 		    @Apache::lonxml::warning_messages);
                   1614:     undef(@Apache::lonxml::info_messages);
                   1615:     undef(@Apache::lonxml::error_messages);
                   1616:     undef(@Apache::lonxml::warning_messages);
                   1617:     $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__/$result/;
                   1618:     $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__//g;
1.83      albertel 1619: }
                   1620: 
                   1621: sub get_param {
1.213     albertel 1622:     my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
                   1623:     if ( ! $context ) { $context = -1; }
                   1624:     my $args ='';
                   1625:     if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.297     sakharuk 1626:     if ( ! $Apache::lonxml::usestyle ) {
                   1627: 	$args=$Apache::lonxml::style_values.$args;
                   1628:     }
1.213     albertel 1629:     if ( ! $args ) { return undef; }
                   1630:     if ( $case_insensitive ) {
                   1631: 	if ($args =~ s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei) {
                   1632: 	    return &Apache::run::run("{$args;".'return $'.$param.'}',
                   1633:                                      $safeeval); #'
                   1634: 	} else {
                   1635: 	    return undef;
                   1636: 	}
                   1637:     } else {
                   1638: 	if ( $args =~ /my \$\Q$param\E=\"/ ) {
                   1639: 	    return &Apache::run::run("{$args;".'return $'.$param.'}',
                   1640:                                      $safeeval); #'
                   1641: 	} else {
                   1642: 	    return undef;
                   1643: 	}
                   1644:     }
1.22      albertel 1645: }
                   1646: 
1.132     albertel 1647: sub get_param_var {
1.213     albertel 1648:   my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1.132     albertel 1649:   if ( ! $context ) { $context = -1; }
                   1650:   my $args ='';
                   1651:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.297     sakharuk 1652:   if ( ! $Apache::lonxml::usestyle ) {
                   1653:       $args=$Apache::lonxml::style_values.$args;
                   1654:   }
1.230     albertel 1655:   &Apache::lonxml::debug("Args are $args param is $param");
1.213     albertel 1656:   if ($case_insensitive) {
                   1657:       if (! ($args=~s/(my \$)(\Q$param\E)(=\")/$1.lc($2).$3/ei)) {
                   1658: 	  return undef;
                   1659:       }
                   1660:   } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }
1.132     albertel 1661:   my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1.230     albertel 1662:   &Apache::lonxml::debug("first run is $value");
1.341     albertel 1663:   if ($value =~ /^[\$\@\%][a-zA-Z_]\w*$/) {
1.230     albertel 1664:       &Apache::lonxml::debug("doing second");
                   1665:       my @result=&Apache::run::run("return $value",$safeeval,1);
                   1666:       if (!defined($result[0])) {
                   1667: 	  return $value
                   1668:       } else {
                   1669: 	  if (wantarray) { return @result; } else { return $result[0]; }
                   1670:       }
1.132     albertel 1671:   } else {
                   1672:     return $value;
                   1673:   }
                   1674: }
                   1675: 
1.74      albertel 1676: sub register_insert {
1.75      albertel 1677:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74      albertel 1678:   my $i;
1.76      albertel 1679:   my $tagnum=0;
1.74      albertel 1680:   my @order;
                   1681:   for ($i=0;$i < $#data; $i++) {
                   1682:     my $line = $data[$i];
                   1683:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
                   1684:     if ( $line =~ /TABLE/ ) { last; }
1.268     bowersj2 1685:     my ($tag,$descrip,$color,$function,$show,$helpfile,$helpdesc) = split(/,/, $line);
1.135     albertel 1686:     if ($tag) {
                   1687:       $insertlist{"$tagnum.tag"} = $tag;
                   1688:       $insertlist{"$tagnum.description"} = $descrip;
                   1689:       $insertlist{"$tagnum.color"} = $color;
                   1690:       $insertlist{"$tagnum.function"} = $function;
                   1691:       if (!defined($show)) { $show='yes'; }
                   1692:       $insertlist{"$tagnum.show"}= $show;
1.268     bowersj2 1693:       $insertlist{"$tagnum.helpfile"} = $helpfile;
                   1694:       $insertlist{"$tagnum.helpdesc"} = $helpdesc;
1.135     albertel 1695:       $insertlist{"$tag.num"}=$tagnum;
                   1696:       $tagnum++;
                   1697:     }
1.74      albertel 1698:   }
1.76      albertel 1699:   $i++; #skipping TABLE line
                   1700:   $tagnum = 0;
1.74      albertel 1701:   for (;$i < $#data;$i++) {
                   1702:     my $line = $data[$i];
1.76      albertel 1703:     my ($mnemonic,@which) = split(/ +/,$line);
                   1704:     my $tag = $insertlist{"$tagnum.tag"};
1.144     matthew  1705:     for (my $j=0;$j <=$#which;$j++) {
1.74      albertel 1706:       if ( $which[$j] eq 'Y' ) {
1.76      albertel 1707: 	if ($insertlist{"$j.show"} ne 'no') {
                   1708: 	  push(@{ $insertlist{"$tag.which"} },$j);
                   1709: 	}
1.74      albertel 1710:       }
                   1711:     }
1.76      albertel 1712:     $tagnum++;
1.74      albertel 1713:   }
                   1714: }
1.98      albertel 1715: 
                   1716: sub description {
                   1717:   my ($token)=@_;
1.138     albertel 1718:   my $tagnum;
                   1719:   my $tag=$token->[1];
                   1720:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
                   1721:     my $testtag=$namespace.'::'.$tag;
                   1722:     $tagnum=$insertlist{"$testtag.num"};
                   1723:     if (defined($tagnum)) { last; }
                   1724:   }
                   1725:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
                   1726:   return $insertlist{$tagnum.'.description'};
1.268     bowersj2 1727: }
                   1728: 
                   1729: # Returns a list containing the help file, and the description
                   1730: sub helpinfo {
                   1731:   my ($token)=@_;
                   1732:   my $tagnum;
                   1733:   my $tag=$token->[1];
                   1734:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
                   1735:     my $testtag=$namespace.'::'.$tag;
                   1736:     $tagnum=$insertlist{"$testtag.num"};
                   1737:     if (defined($tagnum)) { last; }
                   1738:   }
                   1739:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
                   1740:   return ($insertlist{$tagnum.'.helpfile'}, $insertlist{$tagnum.'.helpdesc'});
1.98      albertel 1741: }
1.123     albertel 1742: 
                   1743: # ----------------------------------------------------------------- whichuser
                   1744: # returns a list of $symb, $courseid, $domain, $name that is correct for
                   1745: # calls to lonnet functions for this setup.
                   1746: # - looks for form.grade_ parameters
                   1747: sub whichuser {
1.262     matthew  1748:   my ($passedsymb)=@_;
1.245     albertel 1749:   my ($symb,$courseid,$domain,$name,$publicuser);
1.372     albertel 1750:   if (defined($env{'form.grade_symb'})) {
1.350     albertel 1751:       my ($tmp_courseid)=
                   1752: 	  &Apache::loncommon::get_env_multiple('form.grade_courseid');
                   1753:       my $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid);
                   1754:       if (!$allowed && 
1.372     albertel 1755: 	  exists($env{'request.course.sec'}) && 
                   1756: 	  $env{'request.course.sec'} !~ /^\s*$/) {
1.350     albertel 1757: 	  $allowed=&Apache::lonnet::allowed('vgr',$tmp_courseid.
1.372     albertel 1758: 					    '/'.$env{'request.course.sec'});
1.350     albertel 1759:       }
                   1760:       if ($allowed) {
                   1761: 	  ($symb)=&Apache::loncommon::get_env_multiple('form.grade_symb');
                   1762: 	  $courseid=$tmp_courseid;
                   1763: 	  ($domain)=&Apache::loncommon::get_env_multiple('form.grade_domain');
                   1764: 	  ($name)=&Apache::loncommon::get_env_multiple('form.grade_username');
                   1765: 	  return ($symb,$courseid,$domain,$name,$publicuser);
                   1766:       }
                   1767:   }
                   1768:   if (!$passedsymb) {
                   1769:       $symb=&Apache::lonnet::symbread();
1.134     albertel 1770:   } else {
1.350     albertel 1771:       $symb=$passedsymb;
                   1772:   }
1.372     albertel 1773:   $courseid=$env{'request.course.id'};
                   1774:   $domain=$env{'user.domain'};
                   1775:   $name=$env{'user.name'};
1.350     albertel 1776:   if ($name eq 'public' && $domain eq 'public') {
1.372     albertel 1777:       if (!defined($env{'form.username'})) {
                   1778: 	  $env{'form.username'}.=time.rand(10000000);
1.244     albertel 1779:       }
1.372     albertel 1780:       $name.=$env{'form.username'};
1.123     albertel 1781:   }
1.245     albertel 1782:   return ($symb,$courseid,$domain,$name,$publicuser);
1.123     albertel 1783: }
                   1784: 
1.1       sakharuk 1785: 1;
                   1786: __END__
1.68      www      1787: 
                   1788: 

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