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

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

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