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

1.2       sakharuk    1: # The LearningOnline Network with CAPA
1.3       sakharuk    2: # XML Parser Module 
1.2       sakharuk    3: #
1.494   ! www         4: # $Id: lonxml.pm,v 1.493 2009/04/13 20:42:32 raeburn 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.489     jms        40: =pod
                     41: 
                     42: =head1 NAME
                     43: 
                     44: Apache::lonxml
                     45: 
                     46: =head1 SYNOPSIS
                     47: 
                     48: XML Parsing Module
                     49: 
                     50: This is part of the LearningOnline Network with CAPA project
                     51: described at http://www.lon-capa.org.
                     52: 
                     53: 
                     54: =head1 SUBROUTINES
                     55: 
                     56: =cut
                     57: 
                     58: 
1.2       sakharuk   59: 
1.4       albertel   60: package Apache::lonxml; 
1.33      www        61: use vars 
1.410     albertel   62: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $errorcount $warningcount);
1.1       sakharuk   63: use strict;
1.444     albertel   64: use LONCAPA;
1.167     albertel   65: use HTML::LCParser();
1.161     albertel   66: use HTML::TreeBuilder();
                     67: use HTML::Entities();
                     68: use Safe();
                     69: use Safe::Hole();
                     70: use Math::Cephes();
                     71: use Math::Random();
                     72: use Opcode();
1.271     www        73: use POSIX qw(strftime);
1.339     albertel   74: use Time::HiRes qw( gettimeofday tv_interval );
1.393     albertel   75: use Symbol();
1.266     bowersj2   76: 
1.72      albertel   77: sub register {
1.141     albertel   78:   my ($space,@taglist) = @_;
                     79:   foreach my $temptag (@taglist) {
                     80:     push(@{ $Apache::lonxml::alltags{$temptag} },$space);
1.72      albertel   81:   }
                     82: }
                     83: 
1.141     albertel   84: sub deregister {
                     85:   my ($space,@taglist) = @_;
                     86:   foreach my $temptag (@taglist) {
                     87:     my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
                     88:     if ($tempspace eq $space) {
                     89:       pop(@{ $Apache::lonxml::alltags{$temptag} });
                     90:     }
                     91:   }
1.142     albertel   92:   #&printalltags();
1.141     albertel   93: }
                     94: 
1.46      www        95: use Apache::Constants qw(:common);
1.161     albertel   96: use Apache::lontexconvert();
                     97: use Apache::style();
                     98: use Apache::run();
                     99: use Apache::londefdef();
                    100: use Apache::scripttag();
1.285     www       101: use Apache::languagetags();
1.161     albertel  102: use Apache::edit();
1.266     bowersj2  103: use Apache::inputtags();
                    104: use Apache::outputtags();
1.372     albertel  105: use Apache::lonnet;
1.161     albertel  106: use Apache::File();
                    107: use Apache::loncommon();
1.198     www       108: use Apache::lonfeedback();
1.200     www       109: use Apache::lonmsg();
1.217     matthew   110: use Apache::loncacc();
1.431     www       111: use Apache::lonmaxima();
1.494   ! www       112: use Apache::lonr();
1.280     www       113: use Apache::lonlocal;
1.79      www       114: 
1.462     foxr      115: #====================================   Main subroutine: xmlparse  
                    116: 
1.72      albertel  117: #debugging control, to turn on debugging modify the correct handler
1.462     foxr      118: 
1.72      albertel  119: $Apache::lonxml::debug=0;
1.206     albertel  120: 
                    121: # keeps count of the number of warnings and errors generated in a parse
                    122: $warningcount=0;
                    123: $errorcount=0;
1.72      albertel  124: 
                    125: #path to the directory containing the file currently being processed
                    126: @pwd=();
                    127: 
                    128: #these two are used for capturing a subset of the output for later processing,
                    129: #don't touch them directly use &startredirection and &endredirection
                    130: @outputstack = ();
                    131: $redirection = 0;
                    132: 
                    133: #controls wheter the <import> tag actually does
                    134: $import = 1;
                    135: @extlinks=();
                    136: 
                    137: # meta mode is a bit weird only some output is to be turned off
                    138: #<output> tag turns metamode off (defined in londefdef.pm)
                    139: $metamode = 0;
                    140: 
                    141: # turns on and of run::evaluate actually derefencing var refs
                    142: $evaluate = 1;
1.7       albertel  143: 
1.74      albertel  144: # data structure for eidt mode, determines what tags can go into what other tags
                    145: %insertlist=();
1.68      www       146: 
1.99      albertel  147: # stores the list of active tag namespaces
1.76      albertel  148: @namespace=();
                    149: 
1.448     albertel  150: # stores all Scrit Vars displays for later showing
                    151: my @script_var_displays=();
                    152: 
1.172     albertel  153: # a pointer the the Apache request object
                    154: $Apache::lonxml::request='';
                    155: 
1.216     sakharuk  156: # a problem number counter, and check on ether it is used
1.237     sakharuk  157: $Apache::lonxml::counter=1;
1.204     albertel  158: $Apache::lonxml::counter_changed=0;
                    159: 
1.462     foxr      160: # Part counter hash.   In analysis mode, the
                    161: # problems can use this to record which parts increment the counter
                    162: # by how much.  The counter subs will maintain this hash via
                    163: # their optional part parameters.  Note that the assumption is that
                    164: # analysis is done in one request and therefore it is not necessary to
                    165: # save this information request-to-request.
                    166: 
                    167: 
                    168: %Apache::lonxml::counters_per_part = ();
                    169: 
1.212     albertel  170: #internal check on whether to look at style defs
                    171: $Apache::lonxml::usestyle=1;
1.260     albertel  172: 
                    173: #locations used to store the parameter string for style substitutions
                    174: $Apache::lonxml::style_values='';
                    175: $Apache::lonxml::style_end_values='';
1.212     albertel  176: 
1.281     albertel  177: #array of ssi calls that need to occur after we are done parsing
                    178: @Apache::lonxml::ssi_info=();
                    179: 
1.282     albertel  180: #should we do the postag variable interpolation
                    181: $Apache::lonxml::post_evaluate=1;
                    182: 
1.295     albertel  183: #a header message to emit in the case of any generated warning or errors
                    184: $Apache::lonxml::warnings_error_header='';
                    185: 
1.396     foxr      186: #  Control whether or not LaTeX symbols should be substituted for their
                    187: #  \ style equivalents...this may be turned off e.g. in an verbatim
                    188: #  environment.
                    189: 
                    190: $Apache::lonxml::substitute_LaTeX_symbols = 1; # Starts out on.
                    191: 
                    192: sub enable_LaTeX_substitutions {
                    193:     $Apache::lonxml::substitute_LaTeX_symbols = 1;
                    194: }
                    195: sub disable_LaTeX_substitutions {
                    196:     $Apache::lonxml::substitute_LaTeX_symbols = 0;
                    197: }
                    198: 
1.68      www       199: sub xmlend {
1.335     sakharuk  200:     my ($target,$parser)=@_;
1.278     www       201:     my $mode='xml';
                    202:     my $status='OPEN';
1.368     albertel  203:     if ($Apache::lonhomework::parsing_a_problem ||
                    204: 	$Apache::lonhomework::parsing_a_task ) {
1.278     www       205: 	$mode='problem';
                    206: 	$status=$Apache::inputtags::status[-1]; 
                    207:     }
1.362     matthew   208:     my $discussion;
1.379     albertel  209:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    210: 					   ['LONCAPA_INTERNAL_no_discussion']);
1.372     albertel  211:     if (! exists($env{'form.LONCAPA_INTERNAL_no_discussion'}) ||
                    212:         $env{'form.LONCAPA_INTERNAL_no_discussion'} ne 'true') {
1.362     matthew   213:         $discussion=&Apache::lonfeedback::list_discussion($mode,$status);
                    214:     }
1.334     sakharuk  215:     if ($target eq 'tex') {
1.335     sakharuk  216: 	$discussion.='<tex>\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\end{document}</tex>';
1.334     sakharuk  217: 	&Apache::lonxml::newparser($parser,\$discussion,'');
                    218: 	return '';
                    219:     }
1.405     albertel  220: 
1.407     albertel  221:     return $discussion;
1.119     www       222: }
                    223: 
                    224: sub tokeninputfield {
1.120     www       225:     my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
                    226:     $defhost=~tr/a-z/A-Z/;
1.119     www       227:     return (<<ENDINPUTFIELD)
1.226     albertel  228: <script type="text/javascript">
1.120     www       229:     function updatetoken() {
                    230: 	var comp=new Array;
                    231:         var barcode=unescape(document.tokeninput.barcode.value);
                    232:         comp=barcode.split('*');
                    233:         if (typeof(comp[0])!="undefined") {
                    234: 	    document.tokeninput.codeone.value=comp[0];
                    235: 	}
                    236:         if (typeof(comp[1])!="undefined") {
                    237: 	    document.tokeninput.codetwo.value=comp[1];
                    238: 	}
                    239:         if (typeof(comp[2])!="undefined") {
                    240:             comp[2]=comp[2].toUpperCase();
                    241: 	    document.tokeninput.codethree.value=comp[2];
                    242: 	}
                    243:         document.tokeninput.barcode.value='';
                    244:     }  
                    245: </script>
                    246: <form method="post" name="tokeninput">
1.119     www       247: <table border="2" bgcolor="#FFFFBB">
                    248: <tr><th>DocID Checkin</th></tr>
                    249: <tr><td>
                    250: <table>
                    251: <tr>
                    252: <td>Scan in Barcode</td>
1.120     www       253: <td><input type="text" size="22" name="barcode" 
                    254: onChange="updatetoken()"/></td>
1.119     www       255: </tr>
                    256: <tr><td><i>or</i> Type in DocID</td>
                    257: <td>
                    258: <input type="text" size="5" name="codeone" />
1.120     www       259: <b><font size="+2">*</font></b>
1.119     www       260: <input type="text" size="5" name="codetwo" />
1.120     www       261: <b><font size="+2">*</font></b>
                    262: <input type="text" size="10" name="codethree" value="$defhost" 
                    263: onChange="this.value=this.value.toUpperCase()" />
1.119     www       264: </td></tr>
                    265: </table>
                    266: </td></tr>
                    267: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
                    268: </table>
                    269: </form>
                    270: ENDINPUTFIELD
1.112     www       271: }
                    272: 
1.116     www       273: sub maketoken {
1.118     www       274:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
1.112     www       275:     unless ($symb) {
                    276: 	$symb=&Apache::lonnet::symbread();
                    277:     }
                    278:     unless ($tuname) {
1.372     albertel  279: 	$tuname=$env{'user.name'};
                    280:         $tudom=$env{'user.domain'};
                    281:         $tcrsid=$env{'request.course.id'};
1.112     www       282:     }
1.116     www       283: 
1.118     www       284:     return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
                    285: }
                    286: 
                    287: sub printtokenheader {
1.133     albertel  288:     my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
1.116     www       289:     unless ($token) { return ''; }
1.118     www       290: 
1.423     albertel  291:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.133     albertel  292:     unless ($tsymb) {
                    293: 	$tsymb=$symb;
1.118     www       294:     }
                    295:     unless ($tuname) {
1.133     albertel  296: 	$tuname=$name;
                    297:         $tudom=$domain;
                    298:         $tcrsid=$courseid;
1.118     www       299:     }
1.114     www       300: 
1.390     albertel  301:     my $plainname=&Apache::loncommon::plainname($tuname,$tudom);
1.114     www       302: 
1.112     www       303:     if ($target eq 'web') {
1.145     www       304:         my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
1.115     www       305: 	return 
1.221     albertel  306:  '<img align="right" src="/cgi-bin/barcode.png?encode='.$token.'" />'.
1.284     www       307:                &mt('Checked out for').' '.$plainname.
                    308:                '<br />'.&mt('User').': '.$tuname.' at '.$tudom.
                    309: 	       '<br />'.&mt('ID').': '.$idhash{$tuname}.
                    310: 	       '<br />'.&mt('CourseID').': '.$tcrsid.
1.372     albertel  311: 	       '<br />'.&mt('Course').': '.$env{'course.'.$tcrsid.'.description'}.
1.284     www       312:                '<br />'.&mt('DocID').': '.$token.
                    313:                '<br />'.&mt('Time').': '.&Apache::lonlocal::locallocaltime().'<hr />';
1.112     www       314:     } else {
1.121     albertel  315:         return $token;
1.112     www       316:     }
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.449     albertel  354:  } elsif ($env{'construct.style'}
                    355: 	  && ($env{'request.state'} eq 'construct')) {
1.372     albertel  356:      my $location=&Apache::lonnet::filelocation('',$env{'construct.style'});
1.291     sakharuk  357:      my $styletext=&Apache::lonnet::getfile($location);
1.449     albertel  358:      if ($styletext ne '-1') {
                    359: 	 %style_for_target = (%style_for_target,
                    360: 			      &Apache::style::styleparser($target,$styletext));
                    361:      }
1.178     www       362:  }
1.255     sakharuk  363: #&printalltags();
1.16      albertel  364:  my @pars = ();
1.372     albertel  365:  my $pwd=$env{'request.filename'};
1.23      albertel  366:  $pwd =~ s:/[^/]*$::;
                    367:  &newparser(\@pars,\$content_file_string,$pwd);
1.24      sakharuk  368: 
1.3       sakharuk  369:  my $safeeval = new Safe;
1.40      albertel  370:  my $safehole = new Safe::Hole;
1.82      ng        371:  &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3       sakharuk  372: #-------------------- Redefinition of the target in the case of compound target
                    373: 
                    374:  ($target, my @tenta) = split('&&',$target);
                    375: 
1.150     albertel  376:  my @stack = ();
1.3       sakharuk  377:  my @parstack = ();
1.358     albertel  378:  &initdepth();
                    379:  &init_alarm();
1.101     albertel  380:  my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
1.394     albertel  381: 				   $safeeval,\%style_for_target,1);
1.255     sakharuk  382: 
1.425     albertel  383:  if (@stack) {
1.482     bisitz    384:      &warning(&mt('At end of file some tags were still left unclosed:').
                    385: 	      ' <tt>&lt;'.join('&gt;</tt>, <tt>&lt;',reverse(@stack)).
1.425     albertel  386: 	      '&gt;</tt>');
                    387:  }
1.372     albertel  388:  if ($env{'request.uri'}) {
                    389:     &writeallows($env{'request.uri'});
1.125     www       390:  }
1.281     albertel  391:  &do_registered_ssi();
1.204     albertel  392:  if ($Apache::lonxml::counter_changed) { &store_counter() }
1.393     albertel  393: 
                    394:  &clean_safespace($safeeval);
                    395: 
1.448     albertel  396:  if (@script_var_displays) {
                    397:      $finaloutput .= join('',@script_var_displays);
                    398:      undef(@script_var_displays);
                    399:  }
1.469     albertel  400:  &init_state();
1.372     albertel  401:  if ($env{'form.return_only_error_and_warning_counts'}) {
1.473     www       402:      if ($env{'request.filename'}=~/\.(html|htm|xml)$/i) { 
                    403:         my $error=&verify_html($content_file_string);
                    404:         if ($error) { $errorcount++; }
                    405:      }
1.361     www       406:      return "$errorcount:$warningcount";
                    407:  }
1.3       sakharuk  408:  return $finaloutput;
1.106     www       409: }
                    410: 
1.191     albertel  411: sub latex_special_symbols {
1.272     albertel  412:     my ($string,$where)=@_;
1.396     foxr      413:     #
                    414:     #  If e.g. in verbatim mode, then don't substitute.
                    415:     #  but return original string.
                    416:     #
                    417:     if (!($Apache::lonxml::substitute_LaTeX_symbols)) {
                    418: 	return $string;
                    419:     }
1.235     sakharuk  420:     if ($where eq 'header') {
1.414     foxr      421: 	$string =~ s/\\/\$\\backslash\$/g; # \  -> $\backslash$ per LaTex line by line pg  10.
1.311     albertel  422: 	$string =~ s/(\$|%|\{|\})/\\$1/g;
                    423: 	$string=&Apache::lonprintout::character_chart($string);
                    424: 	# any & or # leftover should be safe to just escape
                    425:         $string=~s/([^\\])\&/$1\\\&/g;
                    426:         $string=~s/([^\\])\#/$1\\\#/g;
1.415     foxr      427: 	$string =~ s/_/\\_/g;              # _ -> \_
                    428: 	$string =~ s/\^/\\\^{}/g;          # ^ -> \^{} 
1.229     sakharuk  429:     } else {
1.312     albertel  430: 	$string=~s/\\/\\ensuremath{\\backslash}/g;
1.367     albertel  431: 	$string=~s/\\\%|\%/\\\%/g;
                    432: 	$string=~s/\\{|{/\\{/g;
                    433: 	$string=~s/\\}|}/\\}/g;
1.378     albertel  434: 	$string=~s/\\ensuremath\\{\\backslash\\}/\\ensuremath{\\backslash}/g;
1.367     albertel  435: 	$string=~s/\\\$|\$/\\\$/g;
                    436: 	$string=~s/\\\_|\_/\\\_/g;
1.313     albertel  437:         $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g;
1.310     sakharuk  438: 	$string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
1.311     albertel  439: 	$string=&Apache::lonprintout::character_chart($string);
                    440: 	# any & or # leftover should be safe to just escape
1.367     albertel  441: 	$string=~s/\\\&|\&/\\\&/g;
                    442: 	$string=~s/\\\#|\#/\\\#/g;
1.332     sakharuk  443:         $string=~s/\|/\$\\mid\$/g;
1.310     sakharuk  444: #single { or } How to escape?
1.229     sakharuk  445:     }
1.272     albertel  446:     return $string;
1.188     sakharuk  447: }
                    448: 
1.101     albertel  449: sub inner_xmlparse {
1.394     albertel  450:   my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target,$start)=@_;
1.101     albertel  451:   my $finaloutput = '';
                    452:   my $result;
                    453:   my $token;
1.258     albertel  454:   my $dontpop=0;
1.389     albertel  455:   my $startredirection = $Apache::lonxml::redirection;
1.101     albertel  456:   while ( $#$pars > -1 ) {
                    457:     while ($token = $$pars['-1']->get_token) {
1.261     albertel  458:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') ) {
1.101     albertel  459: 	if ($metamode<1) {
1.190     albertel  460: 	    my $text=$token->[1];
1.193     albertel  461: 	    if ($token->[0] eq 'C' && $target eq 'tex') {
1.239     sakharuk  462: 		$text = '';
                    463: #		$text = '%'.$text."\n";
1.182     sakharuk  464: 	    }
1.190     albertel  465: 	    $result.=$text;
1.101     albertel  466: 	}
1.261     albertel  467:       } elsif (($token->[0] eq 'D')) {
                    468: 	if ($metamode<1 && $target eq 'web') {
                    469: 	    my $text=$token->[1];
                    470: 	    $result.=$text;
                    471: 	}
1.101     albertel  472:       } elsif ($token->[0] eq 'PI') {
1.261     albertel  473: 	if ($metamode<1 && $target eq 'web') {
1.101     albertel  474: 	  $result=$token->[2];
                    475: 	}
                    476:       } elsif ($token->[0] eq 'S') {
1.140     albertel  477: 	# add tag to stack
1.101     albertel  478: 	push (@$stack,$token->[1]);
                    479: 	# add parameters list to another stack
                    480: 	push (@$parstack,&parstring($token));
1.140     albertel  481: 	&increasedepth($token);
1.212     albertel  482: 	if ($Apache::lonxml::usestyle &&
                    483: 	    exists($$style_for_target{$token->[1]})) {
                    484: 	    $Apache::lonxml::usestyle=0;
                    485: 	    my $string=$$style_for_target{$token->[1]}.
                    486: 	      '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
                    487: 	    &Apache::lonxml::newparser($pars,\$string);
1.257     albertel  488: 	    $Apache::lonxml::style_values=$$parstack[-1];
1.259     albertel  489: 	    $Apache::lonxml::style_end_values=$$parstack[-1];
1.101     albertel  490: 	} else {
                    491: 	  $result = &callsub("start_$token->[1]", $target, $token, $stack,
                    492: 			     $parstack, $pars, $safeeval, $style_for_target);
1.140     albertel  493: 	}
1.101     albertel  494:       } elsif ($token->[0] eq 'E') {
1.212     albertel  495: 	if ($Apache::lonxml::usestyle &&
                    496: 	    exists($$style_for_target{'/'."$token->[1]"})) {
                    497: 	    $Apache::lonxml::usestyle=0;
                    498: 	    my $string=$$style_for_target{'/'.$token->[1]}.
1.258     albertel  499: 	      '<LONCAPA_INTERNAL_TURN_STYLE_ON end="'.$token->[1].'" />';
1.212     albertel  500: 	    &Apache::lonxml::newparser($pars,\$string);
1.259     albertel  501: 	    $Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
                    502: 	    $Apache::lonxml::style_end_values='';
1.258     albertel  503: 	    $dontpop=1;
1.101     albertel  504: 	} else {
1.258     albertel  505: 	    #clear out any tags that didn't end
                    506: 	    while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
                    507: 		my $lasttag=$$stack[-1];
1.317     albertel  508: 		if ($token->[1] =~ /^\Q$lasttag\E$/i) {
1.483     bisitz    509: 		    &Apache::lonxml::warning(&mt('Using tag [_1] on line [_2] as end tag to [_3]','&lt;/'.$token->[1].'&gt;','.$token->[3].','&lt;'.$$stack[-1].'&gt;'));
1.258     albertel  510: 		    last;
                    511: 		} else {
1.483     bisitz    512:                     &Apache::lonxml::warning(&mt('Found tag [_1] on line [_2] when looking for [_3] in file.','&lt;/'.$token->[1].'&gt;',$token->[3],'&lt;/'.$$stack[-1].'&gt;'));
1.258     albertel  513: 		    &end_tag($stack,$parstack,$token);
                    514: 		}
                    515: 	    }
                    516: 	    $result = &callsub("end_$token->[1]", $target, $token, $stack,
                    517: 			       $parstack, $pars,$safeeval, $style_for_target);
1.101     albertel  518: 	}
                    519:       } else {
                    520: 	&Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
                    521:       }
                    522:       #evaluate variable refs in result
1.282     albertel  523:       if ($Apache::lonxml::post_evaluate &&$result ne "") {
1.257     albertel  524: 	  my $extras;
                    525: 	  if (!$Apache::lonxml::usestyle) {
                    526: 	      $extras=$Apache::lonxml::style_values;
                    527: 	  }
1.488     raeburn   528: 	  if ( $#$parstack > -1 ) {
                    529: 	      $result=&Apache::run::evaluate($result,$safeeval,$extras.$$parstack[-1]);
                    530: 	  } else {
                    531: 	      $result= &Apache::run::evaluate($result,$safeeval,$extras);
1.487     raeburn   532:           }
1.163     albertel  533:       }
1.282     albertel  534:       $Apache::lonxml::post_evaluate=1;
                    535: 
1.190     albertel  536:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.249     albertel  537: 	  #Style file definitions should be correct
1.250     albertel  538: 	  if ($target eq 'tex' && ($Apache::lonxml::usestyle)) {
1.311     albertel  539: 	      $result=&latex_special_symbols($result);
1.249     albertel  540: 	  }
1.190     albertel  541:       }
                    542: 
1.169     albertel  543:       if ($Apache::lonxml::redirection) {
                    544: 	$Apache::lonxml::outputstack['-1'] .= $result;
                    545:       } else {
                    546: 	$finaloutput.=$result;
                    547:       }
                    548:       $result = '';
                    549: 
1.258     albertel  550:       if ($token->[0] eq 'E' && !$dontpop) {
1.101     albertel  551: 	&end_tag($stack,$parstack,$token);
                    552:       }
1.258     albertel  553:       $dontpop=0;
1.224     albertel  554:     }	
1.212     albertel  555:     if ($#$pars > -1) {
                    556: 	pop @$pars;
                    557: 	pop @Apache::lonxml::pwd;
                    558:     }
1.101     albertel  559:   }
                    560: 
                    561:   # if ($target eq 'meta') {
                    562:   #   $finaloutput.=&endredirection;
                    563:   # }
                    564: 
1.394     albertel  565:   if ( $start && $target eq 'grade') { &endredirection(); }
1.389     albertel  566:   if ( $Apache::lonxml::redirection > $startredirection) {
                    567:       while ($Apache::lonxml::redirection > $startredirection) {
                    568: 	  $finaloutput .= &endredirection();
1.387     albertel  569:       }
                    570:   }
1.101     albertel  571:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
                    572:     $finaloutput=&afterburn($finaloutput);
1.216     sakharuk  573:   }	    
1.101     albertel  574:   return $finaloutput;
                    575: }
1.67      www       576: 
1.318     matthew   577: ## 
                    578: ## Looks to see if there is a subroutine defined for this tag.  If so, call it,
                    579: ## otherwise do not call it as we do not know what it is.
                    580: ##
1.7       albertel  581: sub callsub {
1.84      albertel  582:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel  583:   my $currentstring='';
1.72      albertel  584:   my $nodefault;
1.7       albertel  585:   {
1.59      albertel  586:     my $sub1;
1.7       albertel  587:     no strict 'refs';
1.68      www       588:     my $tag=$token->[1];
1.236     www       589: # get utterly rid of extended html tags
                    590:     if ($tag=~/^x\-/i) { return ''; }
1.141     albertel  591:     my $space=$Apache::lonxml::alltags{$tag}[-1];
1.68      www       592:     if (!$space) {
1.141     albertel  593:      	$tag=~tr/A-Z/a-z/;
1.68      www       594: 	$sub=~tr/A-Z/a-z/;
1.141     albertel  595: 	$space=$Apache::lonxml::alltags{$tag}[-1]
1.68      www       596:     }
1.97      albertel  597: 
                    598:     my $deleted=0;
                    599:     if (($token->[0] eq 'S') && ($target eq 'modified')) {
                    600:       $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
                    601: 					     $parstack,$parser,$safeeval,
                    602: 					     $style);
                    603:     }
                    604:     if (!$deleted) {
                    605:       if ($space) {
1.220     albertel  606: 	#&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
1.97      albertel  607: 	$sub1="$space\:\:$sub";
                    608: 	($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
                    609: 					     $parstack,$parser,$safeeval,
                    610: 					     $style);
                    611:       } else {
1.318     matthew   612:           if ($target eq 'tex') {
                    613:               # throw away tag name
                    614:               return '';
                    615:           }
1.220     albertel  616: 	#&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
1.97      albertel  617: 	if ($metamode <1) {
                    618: 	  if (defined($token->[4]) && ($metamode < 1)) {
                    619: 	    $currentstring = $token->[4];
                    620: 	  } else {
                    621: 	    $currentstring = $token->[2];
                    622: 	  }
1.62      sakharuk  623: 	}
1.7       albertel  624:       }
1.97      albertel  625:       #    &Apache::lonxml::debug("nodefalt:$nodefault:");
                    626:       if ($currentstring eq '' && $nodefault eq '') {
                    627: 	if ($target eq 'edit') {
1.220     albertel  628: 	  #&Apache::lonxml::debug("doing default edit for $token->[1]");
1.97      albertel  629: 	  if ($token->[0] eq 'S') {
                    630: 	    $currentstring = &Apache::edit::tag_start($target,$token);
                    631: 	  } elsif ($token->[0] eq 'E') {
                    632: 	    $currentstring = &Apache::edit::tag_end($target,$token);
                    633: 	  }
1.441     albertel  634: 	}
                    635:       }
                    636:       if ($target eq 'modified' && $nodefault eq '') {
                    637: 	  if ($currentstring eq '') {
                    638: 	      if ($token->[0] eq 'S') {
                    639: 		  $currentstring = $token->[4];
                    640: 	      } elsif ($token->[0] eq 'E') {
                    641: 		  $currentstring = $token->[2];
                    642: 	      } else {
                    643: 		  $currentstring = $token->[2];
                    644: 	      }
                    645: 	  }
1.97      albertel  646: 	  if ($token->[0] eq 'S') {
1.447     albertel  647: 	      $currentstring.=&Apache::edit::handle_insert();
1.210     www       648: 	  } elsif ($token->[0] eq 'E') {
1.447     albertel  649: 	      $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
1.97      albertel  650: 	  }
1.72      albertel  651:       }
1.7       albertel  652:     }
                    653:     use strict 'refs';
                    654:   }
                    655:   return $currentstring;
1.82      ng        656: }
                    657: 
1.465     albertel  658: {
                    659:     my %state;
                    660: 
                    661:     sub init_state {
                    662: 	undef(%state);
                    663:     }
                    664:     
                    665:     sub set_state {
                    666: 	my ($key,$value) = @_;
                    667: 	$state{$key} = $value;
                    668: 	return $value;
                    669:     }
                    670:     sub get_state {
                    671: 	my ($key) = @_;
                    672: 	return $state{$key};
                    673:     }
                    674: }
                    675: 
1.96      albertel  676: sub setup_globals {
1.172     albertel  677:   my ($request,$target)=@_;
                    678:   $Apache::lonxml::request=$request;
1.205     www       679:   $errorcount=0;
                    680:   $warningcount=0;
1.490     www       681:   $Apache::lonxml::internal_error=0;
1.207     albertel  682:   $Apache::lonxml::default_homework_loaded=0;
1.212     albertel  683:   $Apache::lonxml::usestyle=1;
1.204     albertel  684:   &init_counter();
1.462     foxr      685:   &clear_bubble_lines_for_part();
1.465     albertel  686:   &init_state();
1.469     albertel  687:   &set_state('target',$target);
1.101     albertel  688:   @Apache::lonxml::pwd=();
1.124     albertel  689:   @Apache::lonxml::extlinks=();
1.448     albertel  690:   @script_var_displays=();
1.281     albertel  691:   @Apache::lonxml::ssi_info=();
1.282     albertel  692:   $Apache::lonxml::post_evaluate=1;
1.295     albertel  693:   $Apache::lonxml::warnings_error_header='';
1.397     albertel  694:   $Apache::lonxml::substitute_LaTeX_symbols = 1;
1.96      albertel  695:   if ($target eq 'meta') {
                    696:     $Apache::lonxml::redirection = 0;
                    697:     $Apache::lonxml::metamode = 1;
                    698:     $Apache::lonxml::evaluate = 1;
                    699:     $Apache::lonxml::import = 0;
1.129     albertel  700:   } elsif ($target eq 'answer') {
                    701:     $Apache::lonxml::redirection = 0;
                    702:     $Apache::lonxml::metamode = 1;
                    703:     $Apache::lonxml::evaluate = 1;
                    704:     $Apache::lonxml::import = 1;
1.96      albertel  705:   } elsif ($target eq 'grade') {
1.387     albertel  706:     &startredirection(); #ended in inner_xmlparse on exit
1.96      albertel  707:     $Apache::lonxml::metamode = 0;
                    708:     $Apache::lonxml::evaluate = 1;
                    709:     $Apache::lonxml::import = 1;
                    710:   } elsif ($target eq 'modified') {
                    711:     $Apache::lonxml::redirection = 0;
                    712:     $Apache::lonxml::metamode = 0;
                    713:     $Apache::lonxml::evaluate = 0;
                    714:     $Apache::lonxml::import = 0;
                    715:   } elsif ($target eq 'edit') {
                    716:     $Apache::lonxml::redirection = 0;
                    717:     $Apache::lonxml::metamode = 0;
                    718:     $Apache::lonxml::evaluate = 0;
                    719:     $Apache::lonxml::import = 0;
1.163     albertel  720:   } elsif ($target eq 'analyze') {
                    721:     $Apache::lonxml::redirection = 0;
                    722:     $Apache::lonxml::metamode = 0;
                    723:     $Apache::lonxml::evaluate = 1;
                    724:     $Apache::lonxml::import = 1;
1.96      albertel  725:   } else {
                    726:     $Apache::lonxml::redirection = 0;
                    727:     $Apache::lonxml::metamode = 0;
                    728:     $Apache::lonxml::evaluate = 1;
                    729:     $Apache::lonxml::import = 1;
                    730:   }
                    731: }
                    732: 
1.82      ng        733: sub init_safespace {
                    734:   my ($target,$safeeval,$safehole,$safeinit) = @_;
1.393     albertel  735:   $safeeval->deny_only(':dangerous');
                    736:   $safeeval->reval('use Math::Complex;');
1.383     albertel  737:   $safeeval->permit_only(":default");
1.82      ng        738:   $safeeval->permit("entereval");
                    739:   $safeeval->permit(":base_math");
                    740:   $safeeval->permit("sort");
1.286     albertel  741:   $safeeval->permit("time");
1.480     www       742:   $safeeval->permit("caller");
1.371     albertel  743:   $safeeval->deny("rand");
                    744:   $safeeval->deny("srand");
1.82      ng        745:   $safeeval->deny(":base_io");
1.102     albertel  746:   $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
1.251     albertel  747:   $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
1.82      ng        748:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.324     albertel  749:   $safehole->wrap(\&Apache::chemresponse::chem_standard_order,$safeeval,
                    750: 		  '&chem_standard_order');
1.369     albertel  751:   $safehole->wrap(\&Apache::response::check_status,$safeeval,'&check_status');
1.471     bisitz    752:   $safehole->wrap(\&Apache::response::implicit_multiplication,$safeeval,'&implicit_multiplication');
1.324     albertel  753: 
1.431     www       754:   $safehole->wrap(\&Apache::lonmaxima::maxima_eval,$safeeval,'&maxima_eval');
1.432     www       755:   $safehole->wrap(\&Apache::lonmaxima::maxima_check,$safeeval,'&maxima_check');
1.433     albertel  756:   $safehole->wrap(\&Apache::lonmaxima::maxima_cas_formula_fix,$safeeval,
                    757: 		  '&maxima_cas_formula_fix');
                    758: 
1.494   ! www       759:   $safehole->wrap(\&Apache::lonr::r_eval,$safeeval,'&r_eval');
        !           760:   $safehole->wrap(\&Apache::lonr::r_check,$safeeval,'&r_check');
        !           761:   $safehole->wrap(\&Apache::lonr::r_cas_formula_fix,$safeeval,
        !           762:                   '&r_cas_formula_fix');
        !           763:  
1.433     albertel  764:   $safehole->wrap(\&Apache::caparesponse::capa_formula_fix,$safeeval,
                    765: 		  '&capa_formula_fix');
1.431     www       766: 
1.480     www       767:   $safehole->wrap(\&Apache::lonlocal::locallocaltime,$safeeval,
                    768:                   '&locallocaltime');
                    769: 
1.82      ng        770:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
                    771:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
                    772:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
                    773:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
                    774:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
                    775:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
                    776:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
                    777:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
                    778:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
                    779:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
                    780:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
                    781:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
                    782:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
                    783:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
                    784:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
                    785:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
                    786:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
                    787:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
                    788:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
1.215     albertel  789:   
                    790:   $safehole->wrap(\&Math::Cephes::bdtr  ,$safeeval,'&bdtr'  );
                    791:   $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
                    792:   $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
                    793:   $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
                    794:   $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
                    795:   $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
                    796:   $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
                    797:   $safehole->wrap(\&Math::Cephes::fdtr  ,$safeeval,'&fdtr'  );
                    798:   $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
                    799:   $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
                    800:   $safehole->wrap(\&Math::Cephes::gdtr  ,$safeeval,'&gdtr'  );
                    801:   $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
                    802:   $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
                    803:   $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
                    804:   $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
                    805:   $safehole->wrap(\&Math::Cephes::ndtr  ,$safeeval,'&ndtr'  );
                    806:   $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
                    807:   $safehole->wrap(\&Math::Cephes::pdtr  ,$safeeval,'&pdtr'  );
                    808:   $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
                    809:   $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
                    810:   $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
                    811:   $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
                    812: 
1.383     albertel  813:   $safehole->wrap(\&Math::Cephes::Matrix::mat,$safeeval,'&mat');
                    814:   $safehole->wrap(\&Math::Cephes::Matrix::new,$safeeval,
                    815: 		  '&Math::Cephes::Matrix::new');
                    816:   $safehole->wrap(\&Math::Cephes::Matrix::coef,$safeeval,
                    817: 		  '&Math::Cephes::Matrix::coef');
                    818:   $safehole->wrap(\&Math::Cephes::Matrix::clr,$safeeval,
                    819: 		  '&Math::Cephes::Matrix::clr');
                    820:   $safehole->wrap(\&Math::Cephes::Matrix::add,$safeeval,
                    821: 		  '&Math::Cephes::Matrix::add');
                    822:   $safehole->wrap(\&Math::Cephes::Matrix::sub,$safeeval,
                    823: 		  '&Math::Cephes::Matrix::sub');
                    824:   $safehole->wrap(\&Math::Cephes::Matrix::mul,$safeeval,
                    825: 		  '&Math::Cephes::Matrix::mul');
                    826:   $safehole->wrap(\&Math::Cephes::Matrix::div,$safeeval,
                    827: 		  '&Math::Cephes::Matrix::div');
                    828:   $safehole->wrap(\&Math::Cephes::Matrix::inv,$safeeval,
                    829: 		  '&Math::Cephes::Matrix::inv');
                    830:   $safehole->wrap(\&Math::Cephes::Matrix::transp,$safeeval,
                    831: 		  '&Math::Cephes::Matrix::transp');
                    832:   $safehole->wrap(\&Math::Cephes::Matrix::simq,$safeeval,
                    833: 		  '&Math::Cephes::Matrix::simq');
                    834:   $safehole->wrap(\&Math::Cephes::Matrix::mat_to_vec,$safeeval,
                    835: 		  '&Math::Cephes::Matrix::mat_to_vec');
                    836:   $safehole->wrap(\&Math::Cephes::Matrix::vec_to_mat,$safeeval,
                    837: 		  '&Math::Cephes::Matrix::vec_to_mat');
                    838:   $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
                    839: 		  '&Math::Cephes::Matrix::check');
                    840:   $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
                    841: 		  '&Math::Cephes::Matrix::check');
                    842: 
1.215     albertel  843: #  $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
                    844: #  $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
                    845: #  $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
                    846: #  $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
                    847: #  $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
                    848: #  $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
                    849: 
1.91      ng        850:   $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
                    851:   $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
                    852:   $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
                    853:   $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
                    854:   $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
                    855:   $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
                    856:   $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
                    857:   $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
                    858:   $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
                    859:   $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
                    860:   $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
1.93      ng        861:   $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
1.91      ng        862:   $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
                    863:   $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
                    864:   $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
                    865:   $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
                    866:   $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
                    867:   $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
                    868:   $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
                    869:   $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
                    870:   $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
1.458     albertel  871:   $safehole->wrap(\&Apache::loncommon::languages,$safeeval,'&languages');
1.305     albertel  872:   $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR');
1.311     albertel  873:   $safehole->wrap(\&Apache::lonxml::debug,$safeeval,'&LONCAPA_INTERNAL_DEBUG');
1.420     albertel  874:   $safehole->wrap(\&Apache::lonnet::logthis,$safeeval,'&LONCAPA_INTERNAL_LOGTHIS');
                    875:   $safehole->wrap(\&Apache::inputtags::finalizeawards,$safeeval,'&LONCAPA_INTERNAL_FINALIZEAWARDS');
1.322     albertel  876:   $safehole->wrap(\&Apache::caparesponse::get_sigrange,$safeeval,'&LONCAPA_INTERNAL_get_sigrange');
1.430     albertel  877: #  use Data::Dumper;
                    878: #  $safehole->wrap(\&Data::Dumper::Dumper,$safeeval,'&LONCAPA_INTERNAL_Dumper');
1.82      ng        879: #need to inspect this class of ops
                    880: # $safeeval->deny(":base_orig");
1.331     albertel  881:   $safeeval->permit("require");
1.91      ng        882:   $safeinit .= ';$external::target="'.$target.'";';
1.82      ng        883:   &Apache::run::run($safeinit,$safeeval);
1.373     albertel  884:   &initialize_rndseed($safeeval);
                    885: }
1.303     albertel  886: 
1.393     albertel  887: sub clean_safespace {
                    888:     my ($safeeval) = @_;
                    889:     delete_package_recurse($safeeval->{Root});
                    890: }
                    891: 
                    892: sub delete_package_recurse {
                    893:      my ($package) = @_;
                    894:      my @subp;
                    895:      {
                    896: 	 no strict 'refs';
                    897: 	 while (my ($key,$val) = each(%{*{"$package\::"}})) {
                    898: 	     if (!defined($val)) { next; }
                    899: 	     local (*ENTRY) = $val;
                    900: 	     if (defined *ENTRY{HASH} && $key =~ /::$/ &&
                    901: 		 $key ne "main::" && $key ne "<none>::")
                    902: 	     {
                    903: 		 my ($p) = $package ne "main" ? "$package\::" : "";
                    904: 		 ($p .= $key) =~ s/::$//;
                    905: 		 push(@subp,$p);
                    906: 	     }
                    907: 	 }
                    908:      }
                    909:      foreach my $p (@subp) {
                    910: 	 delete_package_recurse($p);
                    911:      }
                    912:      Symbol::delete_package($package);
                    913: }
                    914: 
1.373     albertel  915: sub initialize_rndseed {
                    916:     my ($safeeval)=@_;
                    917:     my $rndseed;
1.423     albertel  918:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.373     albertel  919:     $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
                    920:     my $safeinit = '$external::randomseed="'.$rndseed.'";';
                    921:     &Apache::lonxml::debug("Setting rndseed to $rndseed");
                    922:     &Apache::run::run($safeinit,$safeeval);
1.207     albertel  923: }
                    924: 
                    925: sub default_homework_load {
                    926:     my ($safeeval)=@_;
                    927:     &Apache::lonxml::debug('Loading default_homework');
                    928:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
1.241     albertel  929:     if ($default eq -1) {
1.207     albertel  930: 	&Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
                    931:     } else {
                    932: 	&Apache::run::run($default,$safeeval);
                    933: 	$Apache::lonxml::default_homework_loaded=1;
                    934:     }
1.17      albertel  935: }
                    936: 
1.358     albertel  937: {
                    938:     my $alarm_depth;
                    939:     sub init_alarm {
                    940: 	alarm(0);
                    941: 	$alarm_depth=0;
                    942:     }
                    943: 
                    944:     sub start_alarm {
                    945: 	if ($alarm_depth<1) {
                    946: 	    my $old=alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
                    947: 	    if ($old) {
                    948: 		&Apache::lonxml::error("Cancelled an alarm of $old, this shouldn't occur.");
                    949: 	    }
                    950: 	}
                    951: 	$alarm_depth++;
                    952:     }
                    953: 
                    954:     sub end_alarm {
                    955: 	$alarm_depth--;
                    956: 	if ($alarm_depth<1) { alarm(0); }
                    957:     }
                    958: }
1.328     albertel  959: my $metamode_was;
1.55      albertel  960: sub startredirection {
1.328     albertel  961:     if (!$Apache::lonxml::redirection) {
                    962: 	$metamode_was=$Apache::lonxml::metamode;
                    963:     }
                    964:     $Apache::lonxml::metamode=0;
                    965:     $Apache::lonxml::redirection++;
                    966:     push (@Apache::lonxml::outputstack, '');
1.55      albertel  967: }
                    968: 
                    969: sub endredirection {
1.328     albertel  970:     if (!$Apache::lonxml::redirection) {
1.380     www       971: 	&Apache::lonxml::error("Endredirection was called before a startredirection, perhaps you have unbalanced tags. Some debugging information:".join ":",caller);
1.328     albertel  972: 	return '';
                    973:     }
                    974:     $Apache::lonxml::redirection--;
                    975:     if (!$Apache::lonxml::redirection) {
                    976: 	$Apache::lonxml::metamode=$metamode_was;
                    977:     }
                    978:     pop @Apache::lonxml::outputstack;
1.97      albertel  979: }
1.459     albertel  980: sub in_redirection {
                    981:     return ($Apache::lonxml::redirection > 0)
                    982: }
1.97      albertel  983: 
                    984: sub end_tag {
                    985:   my ($tagstack,$parstack,$token)=@_;
                    986:   pop(@$tagstack);
                    987:   pop(@$parstack);
                    988:   &decreasedepth($token);
1.55      albertel  989: }
                    990: 
1.17      albertel  991: sub initdepth {
                    992:   @Apache::lonxml::depthcounter=();
1.439     albertel  993:   undef($Apache::lonxml::last_depth_count);
1.17      albertel  994: }
                    995: 
1.438     albertel  996: 
1.339     albertel  997: my @timers;
                    998: my $lasttime;
1.438     albertel  999: # @Apache::lonxml::depthcounter -> count of tags that exist so
                   1000: #                                  far at each level
1.439     albertel 1001: # $Apache::lonxml::last_depth_count -> when ascending, need to
                   1002: # remember the count for the level below the current level (for
                   1003: # example going from 1_2 -> 1 -> 1_3 need to remember the 2 )
1.438     albertel 1004: 
1.17      albertel 1005: sub increasedepth {
1.19      albertel 1006:   my ($token) = @_;
1.439     albertel 1007:   push(@Apache::lonxml::depthcounter,$Apache::lonxml::last_depth_count+1);
                   1008:   undef($Apache::lonxml::last_depth_count);
1.340     albertel 1009:   my $time;
                   1010:   if ($Apache::lonxml::debug eq "1") {
                   1011:       push(@timers,[&gettimeofday()]);
                   1012:       $time=&tv_interval($lasttime);
                   1013:       $lasttime=[&gettimeofday()];
                   1014:   }
1.439     albertel 1015:   my $spacing='  'x($#Apache::lonxml::depthcounter);
1.438     albertel 1016:   $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.439     albertel 1017: #  &Apache::lonxml::debug("s$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $Apache::lonxml::curdepth : $token->[1] : $time");
1.54      albertel 1018: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17      albertel 1019: }
                   1020: 
                   1021: sub decreasedepth {
1.19      albertel 1022:   my ($token) = @_;
1.439     albertel 1023:   if (  $#Apache::lonxml::depthcounter == -1) {
                   1024:       &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));
1.43      albertel 1025:   }
1.439     albertel 1026:   $Apache::lonxml::last_depth_count = pop(@Apache::lonxml::depthcounter);
                   1027: 
1.340     albertel 1028:   my ($timer,$time);
                   1029:   if ($Apache::lonxml::debug eq "1") {
                   1030:       $timer=pop(@timers);
                   1031:       $time=&tv_interval($lasttime);
                   1032:       $lasttime=[&gettimeofday()];
                   1033:   }
1.439     albertel 1034:   my $spacing='  'x($#Apache::lonxml::depthcounter);
                   1035:   $Apache::lonxml::curdepth = join('_',@Apache::lonxml::depthcounter);
                   1036: #  &Apache::lonxml::debug("e$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $Apache::lonxml::curdepth : $token->[1] : $time : ".&tv_interval($timer));
1.54      albertel 1037: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1       sakharuk 1038: }
1.19      albertel 1039: 
1.399     albertel 1040: sub get_id {
                   1041:     my ($parstack,$safeeval)=@_;
                   1042:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
1.449     albertel 1043:     if ($env{'request.state'} eq 'construct' && $id =~ /([._]|[^\w\d\s[:punct:]])/) {
                   1044: 	&error(&mt("ID &quot;[_1]&quot; contains invalid characters, IDs are only allowed to contain letters, numbers, spaces and -",'<tt>'.$id.'</tt>'));
1.399     albertel 1045:     }
                   1046:     if ($id =~ /^\s*$/) { $id = $Apache::lonxml::curdepth; }
                   1047:     return $id;
                   1048: }
                   1049: 
1.180     albertel 1050: sub get_all_text_unbalanced {
1.190     albertel 1051: #there is a copy of this in lonpublisher.pm
1.326     albertel 1052:     my($tag,$pars)= @_;
                   1053:     my $token;
                   1054:     my $result='';
                   1055:     $tag='<'.$tag.'>';
                   1056:     while ($token = $$pars[-1]->get_token) {
                   1057: 	if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1.386     albertel 1058: 	    if ($token->[0] eq 'T' && $token->[2]) {
1.382     albertel 1059: 		$result.='<![CDATA['.$token->[1].']]>';
                   1060: 	    } else {
                   1061: 		$result.=$token->[1];
                   1062: 	    }
1.326     albertel 1063: 	} elsif ($token->[0] eq 'PI') {
                   1064: 	    $result.=$token->[2];
                   1065: 	} elsif ($token->[0] eq 'S') {
                   1066: 	    $result.=$token->[4];
                   1067: 	} elsif ($token->[0] eq 'E')  {
                   1068: 	    $result.=$token->[2];
                   1069: 	}
                   1070: 	if ($result =~ /\Q$tag\E/is) {
                   1071: 	    ($result,my $redo)=$result =~ /(.*)\Q$tag\E(.*)/is;
                   1072: 	    #&Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
                   1073: 	    #&Apache::lonxml::debug('Result is :'.$1);
                   1074: 	    $redo=$tag.$redo;
                   1075: 	    &Apache::lonxml::newparser($pars,\$redo);
                   1076: 	    last;
                   1077: 	}
                   1078:     }
                   1079:     return $result
1.462     foxr     1080: 
1.204     albertel 1081: }
                   1082: 
1.462     foxr     1083: #########################################################################
                   1084: #                                                                       #
                   1085: #           bubble line counter management                              #
                   1086: #                                                                       #
                   1087: #########################################################################
                   1088: 
1.447     albertel 1089: =pod
                   1090: 
                   1091: For bubble grading mode and exam bubble printing mode, the tracking of
                   1092: the current 'bubble line number' is stored in the %env element
                   1093: 'form.counter', and is modifed and handled by the following routines.
                   1094: 
                   1095: The value of it is stored in $Apache:lonxml::counter when live and
                   1096: stored back to env after done.
                   1097: 
                   1098: =item &increment_counter($increment);
                   1099: 
                   1100: Increments the internal counter environment variable a specified amount
                   1101: 
                   1102: Optional Arguments:
                   1103:   $increment - amount to increment by (defaults to 1)
1.462     foxr     1104:                Also 1 if the value is negative or zero.
1.467     foxr     1105:   $part_response - A concatenation of the part and response id
                   1106:                    identifying exactly what is being 'answered'.
                   1107: 
1.447     albertel 1108: 
                   1109: =cut
                   1110: 
1.204     albertel 1111: sub increment_counter {
1.467     foxr     1112:     my ($increment, $part_response) = @_;
1.481     raeburn  1113:     if ($env{'form.grade_noincrement'}) { return; }
1.462     foxr     1114:     if (!defined($increment) || $increment le 0) {
                   1115: 	$increment = 1;
                   1116:     }
                   1117:     $Apache::lonxml::counter += $increment;
                   1118: 
1.466     foxr     1119:     # If the caller supplied the response_id parameter, 
1.462     foxr     1120:     # Maintain its counter.. creating if necessary.
                   1121: 
1.470     albertel 1122:     if (defined($part_response)) {
1.467     foxr     1123: 	if (!defined($Apache::lonxml::counters_per_part{$part_response})) {
                   1124: 	    $Apache::lonxml::counters_per_part{$part_response} = 0;
1.462     foxr     1125: 	}
1.467     foxr     1126: 	$Apache::lonxml::counters_per_part{$part_response} += $increment;
                   1127: 	my $new_value = $Apache::lonxml::counters_per_part{$part_response};
1.247     albertel 1128:     }
1.462     foxr     1129: 	
1.289     sakharuk 1130:     $Apache::lonxml::counter_changed=1;
1.204     albertel 1131: }
                   1132: 
1.447     albertel 1133: =pod
                   1134: 
1.461     foxr     1135: =item &init_counter($increment);
1.447     albertel 1136: 
                   1137: Initialize the internal counter environment variable
                   1138: 
                   1139: =cut
                   1140: 
1.204     albertel 1141: sub init_counter {
1.391     albertel 1142:     if ($env{'request.state'} eq 'construct') {
                   1143: 	$Apache::lonxml::counter=1;
                   1144: 	$Apache::lonxml::counter_changed=1;
                   1145:     } elsif (defined($env{'form.counter'})) {
1.372     albertel 1146: 	$Apache::lonxml::counter=$env{'form.counter'};
1.247     albertel 1147: 	$Apache::lonxml::counter_changed=0;
1.237     sakharuk 1148:     } else {
1.204     albertel 1149: 	$Apache::lonxml::counter=1;
1.247     albertel 1150: 	$Apache::lonxml::counter_changed=1;
1.204     albertel 1151:     }
                   1152: }
                   1153: 
                   1154: sub store_counter {
1.474     raeburn  1155:     &Apache::lonnet::appenv({'form.counter' => $Apache::lonxml::counter});
1.401     albertel 1156:     $Apache::lonxml::counter_changed=0;
1.204     albertel 1157:     return '';
1.180     albertel 1158: }
                   1159: 
1.398     albertel 1160: {
                   1161:     my $state;
                   1162:     sub clear_problem_counter {
                   1163: 	undef($state);
                   1164: 	&Apache::lonnet::delenv('form.counter');
                   1165: 	&Apache::lonxml::init_counter();
                   1166: 	&Apache::lonxml::store_counter();
                   1167:     }
                   1168: 
                   1169:     sub remember_problem_counter {
1.422     albertel 1170: 	&Apache::lonnet::transfer_profile_to_env(undef,undef,1);
1.398     albertel 1171: 	$state = $env{'form.counter'};
                   1172:     }
                   1173: 
                   1174:     sub restore_problem_counter {
                   1175: 	if (defined($state)) {
1.474     raeburn  1176: 	    &Apache::lonnet::appenv({'form.counter' => $state});
1.398     albertel 1177: 	}
                   1178:     }
1.401     albertel 1179:     sub get_problem_counter {
                   1180: 	if ($Apache::lonxml::counter_changed) { &store_counter() }
1.422     albertel 1181: 	&Apache::lonnet::transfer_profile_to_env(undef,undef,1);
1.401     albertel 1182: 	return $env{'form.counter'};
                   1183:     }
1.398     albertel 1184: }
                   1185: 
1.462     foxr     1186: =pod
                   1187: 
1.468     albertel 1188: =item  bubble_lines_for_part(part_response)
1.462     foxr     1189: 
                   1190: Returns the number of lines required to get a response for
1.467     foxr     1191: $part_response (this is just $Apache::lonxml::counters_per_part{$part_response}
1.462     foxr     1192: 
                   1193: =cut
                   1194: 
                   1195: sub bubble_lines_for_part {
1.467     foxr     1196:     my ($part_response) = @_;
1.462     foxr     1197: 
1.467     foxr     1198:     if (!defined($Apache::lonxml::counters_per_part{$part_response})) {
1.462     foxr     1199: 	return 0;
                   1200:     } else {
1.467     foxr     1201: 	return $Apache::lonxml::counters_per_part{$part_response};
1.462     foxr     1202:     }
                   1203: }
                   1204: 
                   1205: =pod
                   1206: 
                   1207: =item clear_bubble_lines_for_part
                   1208: 
                   1209: Clears the hash of bubble lines per part.  If a caller
                   1210: needs to analyze several resources this should be called between
                   1211: resources to reset the hash for each problem being analyzed.
                   1212: 
                   1213: =cut
                   1214: 
                   1215: sub clear_bubble_lines_for_part {
                   1216:     undef(%Apache::lonxml::counters_per_part);
                   1217: }
                   1218: 
                   1219: =pod
                   1220: 
1.468     albertel 1221: =item set_bubble_lines(part_response, value)
1.462     foxr     1222: 
                   1223: If there is a problem part, that for whatever reason
                   1224: requires bubble lines that are not
                   1225: the same as the counter increment, it can call this sub during
                   1226: analysis to set its hash value explicitly.
                   1227: 
                   1228: =cut
                   1229: 
                   1230: sub set_bubble_lines {
1.467     foxr     1231:     my ($part_response, $value) = @_;
1.462     foxr     1232: 
1.467     foxr     1233:     $Apache::lonxml::counters_per_part{$part_response} = $value;
1.462     foxr     1234: }
                   1235: 
                   1236: =pod
                   1237: 
                   1238: =item get_bubble_line_hash
                   1239: 
                   1240: Returns the current bubble line hash.  This is assumed to 
                   1241: be small so we return a copy
                   1242: 
                   1243: 
                   1244: =cut
                   1245: 
                   1246: sub get_bubble_line_hash {
                   1247:     return %Apache::lonxml::counters_per_part;
                   1248: }
                   1249: 
                   1250: 
                   1251: #--------------------------------------------------
                   1252: 
1.19      albertel 1253: sub get_all_text {
1.270     albertel 1254:     my($tag,$pars,$style)= @_;
                   1255:     my $gotfullstack=1;
                   1256:     if (ref($pars) ne 'ARRAY') {
                   1257: 	$gotfullstack=0;
                   1258: 	$pars=[$pars];
                   1259:     }
                   1260:     if (ref($style) ne 'HASH') {
                   1261: 	$style={};
                   1262:     }
                   1263:     my $depth=0;
                   1264:     my $token;
                   1265:     my $result='';
                   1266:     if ( $tag =~ m:^/: ) { 
                   1267: 	my $tag=substr($tag,1); 
                   1268: 	#&Apache::lonxml::debug("have:$tag:");
                   1269: 	my $top_empty=0;
                   1270: 	while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
                   1271: 	    while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
                   1272: 		#&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
                   1273: 		if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1.382     albertel 1274: 		    if ($token->[2]) {
                   1275: 			$result.='<![CDATA['.$token->[1].']]>';
                   1276: 		    } else {
                   1277: 			$result.=$token->[1];
                   1278: 		    }
1.270     albertel 1279: 		} elsif ($token->[0] eq 'PI') {
                   1280: 		    $result.=$token->[2];
                   1281: 		} elsif ($token->[0] eq 'S') {
1.316     albertel 1282: 		    if ($token->[1] =~ /^\Q$tag\E$/i) { $depth++; }
                   1283: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
                   1284: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1.270     albertel 1285: 		    $result.=$token->[4];
                   1286: 		} elsif ($token->[0] eq 'E')  {
1.316     albertel 1287: 		    if ( $token->[1] =~ /^\Q$tag\E$/i) { $depth--; }
1.270     albertel 1288: 		    #skip sending back the last end tag
1.283     albertel 1289: 		    if ($depth == 0 && exists($$style{'/'.$token->[1]}) && $Apache::lonxml::usestyle) {
1.270     albertel 1290: 			my $string=
                   1291: 			    '<LONCAPA_INTERNAL_TURN_STYLE_OFF end="yes" />'.
                   1292: 				$$style{'/'.$token->[1]}.
                   1293: 				    $token->[2].
                   1294: 					'<LONCAPA_INTERNAL_TURN_STYLE_ON />';
                   1295: 			&Apache::lonxml::newparser($pars,\$string);
                   1296: 			#&Apache::lonxml::debug("reParsing $string");
                   1297: 			next;
                   1298: 		    }
                   1299: 		    if ($depth > -1) {
                   1300: 			$result.=$token->[2];
                   1301: 		    } else {
                   1302: 			$$pars[-1]->unget_token($token);
                   1303: 		    }
                   1304: 		}
                   1305: 	    }
                   1306: 	    if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
                   1307: 	    if (($depth >=0) && ($#$pars > 0) ) {
                   1308: 		pop(@$pars);
                   1309: 		pop(@Apache::lonxml::pwd);
                   1310: 	    }
                   1311: 	}
                   1312: 	if ($top_empty && $depth >= 0) {
                   1313: 	    #never found the end tag ran out of text, throw error send back blank
                   1314: 	    &error('Never found end tag for &lt;'.$tag.
                   1315: 		   '&gt; current string <pre>'.
1.314     albertel 1316: 		   &HTML::Entities::encode($result,'<>&"').
1.270     albertel 1317: 		   '</pre>');
                   1318: 	    if ($gotfullstack) {
                   1319: 		my $newstring='</'.$tag.'>'.$result;
                   1320: 		&Apache::lonxml::newparser($pars,\$newstring);
                   1321: 	    }
                   1322: 	    $result='';
                   1323: 	}
                   1324:     } else {
                   1325: 	while ($#$pars > -1) {
                   1326: 	    while ($token = $$pars[-1]->get_token) {
                   1327: 		#&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
                   1328: 		if (($token->[0] eq 'T')||($token->[0] eq 'C')||
                   1329: 		    ($token->[0] eq 'D')) {
1.382     albertel 1330: 		    if ($token->[2]) {
                   1331: 			$result.='<![CDATA['.$token->[1].']]>';
                   1332: 		    } else {
                   1333: 			$result.=$token->[1];
                   1334: 		    }
1.270     albertel 1335: 		} elsif ($token->[0] eq 'PI') {
                   1336: 		    $result.=$token->[2];
                   1337: 		} elsif ($token->[0] eq 'S') {
1.316     albertel 1338: 		    if ( $token->[1] =~ /^\Q$tag\E$/i) {
1.270     albertel 1339: 			$$pars[-1]->unget_token($token); last;
                   1340: 		    } else {
                   1341: 			$result.=$token->[4];
                   1342: 		    }
1.316     albertel 1343: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
                   1344: 		    if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1.270     albertel 1345: 		} elsif ($token->[0] eq 'E')  {
                   1346: 		    $result.=$token->[2];
                   1347: 		}
                   1348: 	    }
                   1349: 	    if (($#$pars > 0) ) {
                   1350: 		pop(@$pars);
                   1351: 		pop(@Apache::lonxml::pwd);
                   1352: 	    } else { last; }
                   1353: 	}
                   1354:     }
                   1355:     #&Apache::lonxml::debug("Exit:$result:");
                   1356:     return $result
1.19      albertel 1357: }
                   1358: 
1.23      albertel 1359: sub newparser {
                   1360:   my ($parser,$contentref,$dir) = @_;
1.167     albertel 1361:   push (@$parser,HTML::LCParser->new($contentref));
1.365     albertel 1362:   $$parser[-1]->xml_mode(1);
                   1363:   $$parser[-1]->marked_sections(1);
1.23      albertel 1364:   if ( $dir eq '' ) {
                   1365:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
                   1366:   } else {
                   1367:     push (@Apache::lonxml::pwd, $dir);
                   1368:   } 
                   1369: }
1.1       sakharuk 1370: 
1.8       albertel 1371: sub parstring {
1.417     albertel 1372:     my ($token) = @_;
                   1373:     my (@vars,@values);
                   1374:     foreach my $attr (@{$token->[3]}) {
                   1375: 	if ($attr!~/\W/) {
                   1376: 	    my $val=$token->[2]->{$attr};
                   1377: 	    $val =~ s/([\%\@\\\"\'])/\\$1/g;
                   1378: 	    $val =~ s/(\$[^\{a-zA-Z_])/\\$1/g;
                   1379: 	    $val =~ s/(\$)$/\\$1/;
                   1380: 	    #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
                   1381: 	    push(@vars,"\$$attr");
                   1382: 	    push(@values,"\"$val\"");
                   1383: 	}
                   1384:     }
                   1385:     my $var_init = 
                   1386: 	(@vars) ? 'my ('.join(',',@vars).') = ('.join(',',@values).');'
                   1387: 	        : '';
                   1388:     return $var_init;
1.8       albertel 1389: }
1.22      albertel 1390: 
1.384     albertel 1391: sub extlink {
                   1392:     my ($res,$exact)=@_;
                   1393:     if (!$exact) {
                   1394: 	$res=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$res);
                   1395:     }
                   1396:     push(@Apache::lonxml::extlinks,$res)	 
                   1397: }
                   1398: 
1.34      www      1399: sub writeallows {
1.126     www      1400:     unless ($#extlinks>=0) { return; }
1.377     albertel 1401:     my $thisurl = &Apache::lonnet::clutter(shift);
1.372     albertel 1402:     if ($env{'httpref.'.$thisurl}) {
                   1403: 	$thisurl=$env{'httpref.'.$thisurl};
1.111     www      1404:     }
1.34      www      1405:     my $thisdir=$thisurl;
                   1406:     $thisdir=~s/\/[^\/]+$//;
                   1407:     my %httpref=();
1.142     albertel 1408:     foreach (@extlinks) {
1.34      www      1409:        $httpref{'httpref.'.
1.444     albertel 1410:  	        &Apache::lonnet::hreflocation($thisdir,&unescape($_))}=$thisurl;
1.142     albertel 1411:     }
1.126     www      1412:     @extlinks=();
1.474     raeburn  1413:     &Apache::lonnet::appenv(\%httpref);
1.34      www      1414: }
                   1415: 
1.281     albertel 1416: sub register_ssi {
                   1417:     my ($url,%form)=@_;
                   1418:     push (@Apache::lonxml::ssi_info,{'url'=>$url,'form'=>\%form});
                   1419:     return '';
                   1420: }
                   1421: 
                   1422: sub do_registered_ssi {
                   1423:     foreach my $info (@Apache::lonxml::ssi_info) {
                   1424: 	my %form=%{ $info->{'form'}};
                   1425: 	my $url=$info->{'url'};
                   1426: 	&Apache::lonnet::ssi($url,%form);
                   1427:     }
                   1428: }
1.448     albertel 1429: 
                   1430: sub add_script_result {
                   1431:     my ($display) = @_;
                   1432:     push(@script_var_displays, $display);
                   1433: }
                   1434: 
1.66      www      1435: #
                   1436: # Afterburner handles anchors, highlights and links
                   1437: #
                   1438: sub afterburn {
                   1439:     my $result=shift;
1.154     albertel 1440:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1441: 					    ['highlight','anchor','link']);
1.372     albertel 1442:     if ($env{'form.highlight'}) {
                   1443:        foreach (split(/\,/,$env{'form.highlight'})) {
1.66      www      1444:            my $anchorname=$_;
                   1445: 	   my $matchthis=$anchorname;
                   1446:            $matchthis=~s/\_+/\\s\+/g;
1.317     albertel 1447:            $result=~s/(\Q$matchthis\E)/\<font color=\"red\"\>$1\<\/font\>/gs;
1.142     albertel 1448:        }
1.66      www      1449:     }
1.372     albertel 1450:     if ($env{'form.link'}) {
                   1451:        foreach (split(/\,/,$env{'form.link'})) {
1.66      www      1452:            my ($anchorname,$linkurl)=split(/\>/,$_);
                   1453: 	   my $matchthis=$anchorname;
                   1454:            $matchthis=~s/\_+/\\s\+/g;
1.317     albertel 1455:            $result=~s/(\Q$matchthis\E)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1.142     albertel 1456:        }
1.66      www      1457:     }
1.372     albertel 1458:     if ($env{'form.anchor'}) {
                   1459:         my $anchorname=$env{'form.anchor'};
1.66      www      1460: 	my $matchthis=$anchorname;
                   1461:         $matchthis=~s/\_+/\\s\+/g;
1.317     albertel 1462:         $result=~s/(\Q$matchthis\E)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1.66      www      1463:         $result.=(<<"ENDSCRIPT");
1.226     albertel 1464: <script type="text/javascript">
1.66      www      1465:     document.location.hash='$anchorname';
                   1466: </script>
                   1467: ENDSCRIPT
                   1468:     }
                   1469:     return $result;
                   1470: }
                   1471: 
1.79      www      1472: sub storefile {
                   1473:     my ($file,$contents)=@_;
1.290     albertel 1474:     &Apache::lonnet::correct_line_ends(\$contents);
1.79      www      1475:     if (my $fh=Apache::File->new('>'.$file)) {
                   1476: 	print $fh $contents;
                   1477:         $fh->close();
1.271     www      1478:         return 1;
1.147     albertel 1479:     } else {
1.482     bisitz   1480: 	&warning(&mt('Unable to save file [_1]','<tt>'.$file.'</tt>'));
1.271     www      1481: 	return 0;
1.79      www      1482:     }
                   1483: }
                   1484: 
1.151     albertel 1485: sub createnewhtml {
1.321     www      1486:     my $title=&mt('Title of document goes here');
                   1487:     my $body=&mt('Body of document goes here');
                   1488:     my $filecontents=(<<SIMPLECONTENT);
1.78      www      1489: <html>
                   1490: <head>
1.321     www      1491: <title>$title</title>
1.78      www      1492: </head>
                   1493: <body bgcolor="#FFFFFF">
1.321     www      1494: $body
1.78      www      1495: </body>
                   1496: </html>
                   1497: SIMPLECONTENT
1.321     www      1498:     return $filecontents;
1.151     albertel 1499: }
                   1500: 
1.274     albertel 1501: sub createnewsty {
                   1502:   my $filecontents=(<<SIMPLECONTENT);
                   1503: <definetag name="">
                   1504:     <render>
                   1505:        <web></web>
                   1506:        <tex></tex>
                   1507:     </render>
                   1508: </definetag>
                   1509: SIMPLECONTENT
                   1510:   return $filecontents;
                   1511: }
                   1512: 
1.493     raeburn  1513: sub createnewjs {
                   1514:     my $filecontents=(<<SIMPLECONTENT);
                   1515: <script type="text/javascript" language="Javascript">
                   1516: 
                   1517: </script>
                   1518: SIMPLECONTENT
                   1519:     return $filecontents;
                   1520: }
                   1521: 
1.472     www      1522: sub verify_html {
                   1523:     my ($filecontents)=@_;
                   1524:     if ($filecontents!~/(?:\<|\&lt\;)(?:html|xml)[^\<]*(?:\>|\&gt\;)/is) {
                   1525:        return &mt('File does not have [_1] or [_2] starting tag','&lt;html&gt;','&lt;xml&gt;');
                   1526:     }
                   1527:     if ($filecontents!~/(?:\<|\&lt\;)\/(?:html|xml)(?:\>|\&gt\;)/is) {
                   1528:        return &mt('File does not have [_1] or [_2] ending tag','&lt;html&gt;','&lt;xml&gt;');
                   1529:     }
                   1530:     if ($filecontents!~/(?:\<|\&lt\;)(?:body|frameset)[^\<]*(?:\>|\&gt\;)/is) {
                   1531:        return &mt('File does not have [_1] or [_2] starting tag','&lt;body&gt;','&lt;frameset&gt;');
                   1532:     }
                   1533:     if ($filecontents!~/(?:\<|\&lt\;)\/(?:body|frameset)[^\<]*(?:\>|\&gt\;)/is) {
                   1534:        return &mt('File does not have [_1] or [_2] ending tag','&lt;body&gt;','&lt;frameset&gt;');
                   1535:     }
                   1536:     return '';
                   1537: }
1.147     albertel 1538: 
1.478     www      1539: sub renderingoptions {
                   1540:     my %langchoices=('' => '');
                   1541:     foreach (&Apache::loncommon::languageids()) {
                   1542:         if (&Apache::loncommon::supportedlanguagecode($_)) {
                   1543:             $langchoices{&Apache::loncommon::supportedlanguagecode($_)}
                   1544:                        = &Apache::loncommon::plainlanguagedescription($_);
                   1545:         }
                   1546:     }
                   1547:     return
                   1548:        '<span class="LC_nobreak">'.
1.479     bisitz   1549:        &mt('Language:').' '.
1.478     www      1550:        &Apache::loncommon::select_form($env{'form.languages'},'languages',
                   1551:                                                      %langchoices).'
                   1552:      </span>
                   1553:      <span class="LC_nobreak">'.
1.479     bisitz   1554:        &mt('Math Rendering:').' '.
1.478     www      1555:        &Apache::loncommon::select_form($env{'form.texengine'},'texengine',
                   1556:                                                      ('' => '',
1.483     bisitz   1557:                                                       'tth' => 'tth (TeX to HTML)',
1.478     www      1558:                                                       'jsMath' => 'jsMath',
1.479     bisitz   1559:                                                       'mimetex' => 'mimetex (Convert to Images)')).'
1.478     www      1560:      </span>';
                   1561: }
                   1562: 
1.151     albertel 1563: sub inserteditinfo {
1.470     albertel 1564:       my ($filecontents, $filetype, $filename)=@_;
1.314     albertel 1565:       $filecontents = &HTML::Entities::encode($filecontents,'<>&"');
1.274     albertel 1566:       my $xml_help = '';
1.321     www      1567:       my $initialize='';
1.452     albertel 1568:       my $textarea_id = 'filecont';
1.492     raeburn  1569:       my $dragmath_button;
1.452     albertel 1570:       my ($add_to_onload, $add_to_onresize);
1.456     albertel 1571:       $initialize=&Apache::lonhtmlcommon::spellheader();
                   1572:       if ($filetype eq 'html' 
                   1573: 	  && (!&Apache::lonhtmlcommon::htmlareablocked() &&
                   1574: 	      &Apache::lonhtmlcommon::htmlareabrowser())) {
1.463     albertel 1575: 	  $textarea_id .= '___Frame';
                   1576: 	  my $lang = &Apache::lonhtmlcommon::htmlarea_lang();
1.456     albertel 1577: 	  $initialize.=(<<FULLPAGE);
1.321     www      1578: <script type="text/javascript">
1.463     albertel 1579: lonca
1.321     www      1580:     function initDocument() {
1.463     albertel 1581:         var oFCKeditor = new FCKeditor('filecont');
                   1582: 	oFCKeditor.Config['CustomConfigurationsPath'] = '/fckeditor/loncapaconfig.js'  ;
                   1583: 	oFCKeditor.Config['FullPage'] = true
                   1584: 	oFCKeditor.Config['AutoDetectLanguage'] = false;
                   1585:         oFCKeditor.Config['DefaultLanguage'] = "$lang";
                   1586: 	oFCKeditor.ReplaceTextarea();
                   1587:     }
                   1588:     function check_if_dirty(editor) {
                   1589: 	if (editor.IsDirty()) {
                   1590: 	    unClean();
                   1591: 	}
                   1592:     }
                   1593:     function FCKeditor_OnComplete(editor) {
                   1594: 	editor.Events.AttachEvent("OnSelectionChange",check_if_dirty);
                   1595: 	resize_textarea('$textarea_id','LC_aftertextarea');
1.321     www      1596:     }
                   1597: </script>
                   1598: FULLPAGE
1.456     albertel 1599:       } else {
                   1600: 	  $initialize.=(<<FULLPAGE);
1.347     albertel 1601: <script type="text/javascript">
                   1602:     function initDocument() {
1.453     albertel 1603: 	resize_textarea('$textarea_id','LC_aftertextarea');
1.347     albertel 1604:     }
                   1605: </script>
                   1606: FULLPAGE
1.492     raeburn  1607:           if ($filetype eq 'html') {
                   1608:               $initialize .= "\n".&Apache::lonhtmlcommon::dragmath_js('EditMathPopup');
                   1609:               $dragmath_button = &Apache::lonhtmlcommon::dragmath_button('filecont',1);
                   1610:           }
1.456     albertel 1611:       }
                   1612: 
                   1613:       $add_to_onload = 'initDocument();';
                   1614:       $add_to_onresize = "resize_textarea('$textarea_id','LC_aftertextarea');";
                   1615: 
                   1616:       if ($filetype eq 'html') {
1.321     www      1617: 	  $xml_help=&Apache::loncommon::helpLatexCheatsheet();
1.274     albertel 1618:       }
1.452     albertel 1619: 
1.254     albertel 1620:       my $titledisplay=&display_title();
1.426     banghart 1621:       my %lt=&Apache::lonlocal::texthash('st' => 'Save and Edit',
                   1622: 					 'vi' => 'Save and View',
1.427     banghart 1623: 					 'dv' => 'Discard Edits and View',
1.428     banghart 1624: 					 'un' => 'undo',
1.280     www      1625: 					 'ed' => 'Edit');
1.470     albertel 1626:       my $spelllink .=&Apache::lonhtmlcommon::spelllink('xmledit','filecont');
1.451     albertel 1627:       my $textarea_events = &Apache::edit::element_change_detection();
                   1628:       my $form_events     = &Apache::edit::form_change_detection();
1.491     raeburn  1629:       my $htmlerror;
                   1630:       if ($filetype eq 'html') {
                   1631:           $htmlerror=&verify_html($filecontents);
                   1632:           if ($htmlerror) {
                   1633:               $htmlerror='<span class="LC_error">'.$htmlerror.'</span>';
                   1634:           }
1.472     www      1635:       }
1.78      www      1636:       my $editfooter=(<<ENDFOOTER);
1.321     www      1637: $initialize
1.78      www      1638: <a name="editsection" />
1.451     albertel 1639: <form $form_events method="post" name="xmledit">
1.470     albertel 1640:   <div class="LC_edit_problem_editxml_header">
                   1641:     <table class="LC_edit_problem_header_title"><tr><td>
                   1642:         $filename
                   1643:       </td><td align="right">
                   1644:         $xml_help
                   1645:       </td></tr>
                   1646:     </table>
                   1647:     <div class="LC_edit_problem_discards">
                   1648:       <input type="submit" name="discardview" accesskey="d" value="$lt{'dv'}" />
                   1649:       <input type="submit" name="Undo" accesskey="u" value="$lt{'un'}" />
1.492     raeburn  1650:       $dragmath_button $spelllink $htmlerror
1.470     albertel 1651:     </div>
                   1652:     <div class="LC_edit_problem_saves">
                   1653:       <input type="submit" name="savethisfile" accesskey="s" value="$lt{'st'}" />
                   1654:       <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
                   1655:     </div>
                   1656:   </div>
                   1657:   <textarea $textarea_events style="width:100%" cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
                   1658:   <div id="LC_aftertextarea">
                   1659:     <br />
                   1660:     $titledisplay
                   1661:   </div>
1.78      www      1662: </form>
1.321     www      1663: </body>
1.78      www      1664: ENDFOOTER
1.452     albertel 1665:       return ($editfooter,$add_to_onload,$add_to_onresize);;
1.78      www      1666: }
                   1667: 
1.152     albertel 1668: sub get_target {
1.372     albertel 1669:   my $viewgrades=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
                   1670:   if ( $env{'request.state'} eq 'published') {
                   1671:     if ( defined($env{'form.grade_target'})
1.152     albertel 1672: 	 && ($viewgrades == 'F' )) {
1.372     albertel 1673:       return ($env{'form.grade_target'});
                   1674:     } elsif (defined($env{'form.grade_target'})) {
                   1675:       if (($env{'form.grade_target'} eq 'web') ||
                   1676: 	  ($env{'form.grade_target'} eq 'tex') ) {
                   1677: 	return $env{'form.grade_target'}
1.153     albertel 1678:       } else {
                   1679: 	return 'web';
                   1680:       }
1.152     albertel 1681:     } else {
                   1682:       return 'web';
                   1683:     }
1.372     albertel 1684:   } elsif ($env{'request.state'} eq 'construct') {
                   1685:     if ( defined($env{'form.grade_target'})) {
                   1686:       return ($env{'form.grade_target'});
1.152     albertel 1687:     } else {
                   1688:       return 'web';
                   1689:     }
                   1690:   } else {
                   1691:     return 'web';
                   1692:   }
                   1693: }
                   1694: 
1.24      sakharuk 1695: sub handler {
1.255     sakharuk 1696:     my $request=shift;
1.493     raeburn  1697: 
1.255     sakharuk 1698:     my $target=&get_target();
1.372     albertel 1699:     $Apache::lonxml::debug=$env{'user.debug'};
1.255     sakharuk 1700:     
1.364     albertel 1701:     &Apache::loncommon::content_type($request,'text/html');
1.255     sakharuk 1702:     &Apache::loncommon::no_cache($request);
1.372     albertel 1703:     if ($env{'request.state'} eq 'published') {
1.363     albertel 1704: 	$request->set_last_modified(&Apache::lonnet::metadata($request->uri,
                   1705: 							      'lastrevisiondate'));
                   1706:     }
1.255     sakharuk 1707:     $request->send_http_header;
                   1708:     
                   1709:     return OK if $request->header_only;
1.68      www      1710: 
                   1711: 
1.255     sakharuk 1712:     my $file=&Apache::lonnet::filelocation("",$request->uri);
1.274     albertel 1713:     my $filetype;
1.493     raeburn  1714:     if ($file =~ /\.(sty|css|js|txt)$/) {
                   1715: 	$filetype=$1;
1.274     albertel 1716:     } else {
                   1717: 	$filetype='html';
                   1718:     }
1.493     raeburn  1719: 
1.78      www      1720: #
                   1721: # Edit action? Save file.
                   1722: #
1.428     banghart 1723:     if (!($env{'request.state'} eq 'published')) {
                   1724: 	if ($env{'form.savethisfile'} || $env{'form.viewmode'} || $env{'form.Undo'}) {
1.429     albertel 1725: 	    my $html_file=&Apache::lonnet::getfile($file);
                   1726: 	    my $error = &Apache::lonhomework::handle_save_or_undo($request, \$html_file, \$env{'form.filecont'});
1.472     www      1727:             if ($env{'form.savethisfile'}) {
                   1728:                 $env{'form.editmode'}='Edit'; #force edit mode
                   1729:             }
1.255     sakharuk 1730: 	}
                   1731:     }
                   1732:     my %mystyle;
                   1733:     my $result = '';
                   1734:     my $filecontents=&Apache::lonnet::getfile($file);
                   1735:     if ($filecontents eq -1) {
1.402     albertel 1736: 	my $start_page=&Apache::loncommon::start_page('File Error');
1.412     albertel 1737: 	my $end_page=&Apache::loncommon::end_page();
1.284     www      1738: 	my $fnf=&mt('File not found');
1.255     sakharuk 1739: 	$result=(<<ENDNOTFOUND);
1.402     albertel 1740: $start_page
1.284     www      1741: <b>$fnf: $file</b>
1.402     albertel 1742: $end_page
1.78      www      1743: ENDNOTFOUND
1.343     albertel 1744:         $filecontents='';
1.372     albertel 1745: 	if ($env{'request.state'} ne 'published') {
1.274     albertel 1746: 	    if ($filetype eq 'sty') {
                   1747: 		$filecontents=&createnewsty();
1.493     raeburn  1748:             } elsif ($filetype eq 'js') {
                   1749:                 $filecontents=&createnewjs();
                   1750:             } elsif (($filetype ne 'css') && ($filetype ne 'txt')) {
1.274     albertel 1751: 		$filecontents=&createnewhtml();
                   1752: 	    }
1.372     albertel 1753: 	    $env{'form.editmode'}='Edit'; #force edit mode
1.255     sakharuk 1754: 	}
                   1755:     } else {
1.372     albertel 1756: 	unless ($env{'request.state'} eq 'published') {
1.343     albertel 1757: 	    if ($filecontents=~/BEGIN LON-CAPA Internal/) {
1.381     www      1758: 		&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 1759: 	    }
1.264     www      1760: #
                   1761: # we are in construction space, see if edit mode forced
1.385     albertel 1762:             &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1763: 						    ['editmode']);
1.255     sakharuk 1764: 	}
1.427     banghart 1765: 	if (!$env{'form.editmode'} || $env{'form.viewmode'} || $env{'form.discardview'}) {
1.493     raeburn  1766:             if ($filetype eq 'html' || $filetype eq 'sty') {
                   1767: 	        &Apache::structuretags::reset_problem_globals();
                   1768: 	        $result = &Apache::lonxml::xmlparse($request,$target,
                   1769:                                                     $filecontents,'',%mystyle);
1.455     albertel 1770: 	    # .html files may contain <problem> or <Task> need to clean
                   1771: 	    # up if it did
1.493     raeburn  1772: 	        &Apache::structuretags::reset_problem_globals();
                   1773: 	        &Apache::lonhomework::finished_parsing();
                   1774:             } else {
                   1775:                 $result = $filecontents;
                   1776:             }
1.385     albertel 1777: 	    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1778: 						    ['rawmode']);
1.395     albertel 1779: 	    if ($env{'form.rawmode'}) { $result = $filecontents; }
1.493     raeburn  1780: 	    if ($filetype ne 'html') {
                   1781:                 my $nochgview = 1; 
1.457     albertel 1782: 		my $controls =
1.493     raeburn  1783: 		    ($env{'request.state'} eq 'construct') ? &Apache::londefdef::edit_controls($nochgview)
1.457     albertel 1784: 		                                           : '';
1.493     raeburn  1785:                 if ($filetype ne 'sty') {
                   1786:                     $result =~ s/</&lt;/g;
                   1787:                     $result =~ s/>/&gt;/g;
                   1788:                     $result = '<table class="LC_sty_begin">'.
                   1789:                               '<tr><td><b><pre>'.$result.
                   1790:                               '</pre></b></td></tr></table>';
                   1791:                 }
                   1792:                 if ($env{'environment.remote'} eq 'off') {
                   1793:                     my %options = ('bgcolor' => '#FFFFFF');
                   1794: 		    $result = 
                   1795: 		        &Apache::loncommon::start_page(undef,undef,\%options).
                   1796: 		        $controls.
                   1797: 		        $result.
                   1798: 		        &Apache::loncommon::end_page();
                   1799:                 } else {
                   1800:                     $result = $controls.$result;
                   1801:                 }
                   1802:             }
1.255     sakharuk 1803: 	}
1.147     albertel 1804:     }
1.456     albertel 1805: 
1.78      www      1806: #
                   1807: # Edit action? Insert editing commands
                   1808: #
1.372     albertel 1809:     unless ($env{'request.state'} eq 'published') {
1.427     banghart 1810: 	if ($env{'form.editmode'} && (!($env{'form.viewmode'})) && (!($env{'form.discardview'})))
1.450     albertel 1811: 	{
1.470     albertel 1812: 	    my $displayfile=$request->uri;
                   1813: 	    $displayfile=~s/^\/[^\/]*//;
                   1814: 
1.452     albertel 1815: 	    my ($edit_info, $add_to_onload, $add_to_onresize)=
1.470     albertel 1816: 		&inserteditinfo($filecontents,$filetype,$displayfile);
1.450     albertel 1817: 
                   1818: 	    my %options = 
                   1819: 		('add_entries' =>
1.452     albertel 1820:                    {'onresize' => $add_to_onresize,
                   1821: 		    'onload'   => $add_to_onload,   });
                   1822: 
1.402     albertel 1823: 	    if ($env{'environment.remote'} ne 'off') {
                   1824: 		$options{'bgcolor'}   = '#FFFFFF';
1.450     albertel 1825: 		$options{'only_body'} = 1;
1.349     albertel 1826: 	    }
1.452     albertel 1827: 	    my $js =
                   1828: 		&Apache::edit::js_change_detection().
                   1829: 		&Apache::loncommon::resize_textarea_js();
1.451     albertel 1830: 	    my $start_page = &Apache::loncommon::start_page(undef,$js,
1.402     albertel 1831: 							    \%options);
                   1832: 	    $result=$start_page.
1.470     albertel 1833: 		&Apache::lonxml::message_location().
1.450     albertel 1834: 		$edit_info.
                   1835: 		&Apache::loncommon::end_page();
1.493     raeburn  1836:         }
1.147     albertel 1837:     }
1.402     albertel 1838:     if ($filetype eq 'html') { &writeallows($request->uri); }
1.255     sakharuk 1839:     
1.309     albertel 1840:     &Apache::lonxml::add_messages(\$result);
1.255     sakharuk 1841:     $request->print($result);
                   1842:     
                   1843:     return OK;
1.253     albertel 1844: }
                   1845: 
                   1846: sub display_title {
                   1847:     my $result;
1.372     albertel 1848:     if ($env{'request.state'} eq 'construct') {
1.253     albertel 1849: 	my $title=&Apache::lonnet::gettitle();
                   1850: 	if (!defined($title) || $title eq '') {
1.372     albertel 1851: 	    $title = $env{'request.filename'};
1.253     albertel 1852: 	    $title = substr($title, rindex($title, '/') + 1);
                   1853: 	}
1.476     bisitz   1854:         $result = "<script type='text/javascript'>top.document.title = '$title - LON-CAPA "
                   1855:                   .&mt('Construction Space')."';</script>";
1.253     albertel 1856:     }
                   1857:     return $result;
1.24      sakharuk 1858: }
1.147     albertel 1859: 
1.22      albertel 1860: sub debug {
1.298     albertel 1861:     if ($Apache::lonxml::debug eq "1") {
                   1862: 	$|=1;
1.300     albertel 1863: 	my $request=$Apache::lonxml::request;
1.388     albertel 1864: 	if (!$request) {
                   1865: 	    eval { $request=Apache->request; };
                   1866: 	}
                   1867: 	if (!$request) {
                   1868: 	    eval { $request=Apache2::RequestUtil->request; };
                   1869: 	}
1.314     albertel 1870: 	$request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");
1.346     albertel 1871: 	#&Apache::lonnet::logthis($_[0]);
1.298     albertel 1872:     }
1.22      albertel 1873: }
1.49      albertel 1874: 
1.348     albertel 1875: sub show_error_warn_msg {
1.372     albertel 1876:     if ($env{'request.filename'} eq '/home/httpd/html/res/lib/templates/simpleproblem.problem' &&
                   1877: 	&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
1.351     albertel 1878: 	return 1;
                   1879:     }
1.348     albertel 1880:     return (($Apache::lonxml::debug eq 1) ||
1.372     albertel 1881: 	    ($env{'request.state'} eq 'construct') ||
1.348     albertel 1882: 	    ($Apache::lonhomework::browse eq 'F'
                   1883: 	     &&
1.372     albertel 1884: 	     $env{'form.show_errors'} eq 'on'));
1.348     albertel 1885: }
                   1886: 
1.22      albertel 1887: sub error {
1.454     albertel 1888:     my @errors = @_;
                   1889: 
1.336     albertel 1890:     $errorcount++;
1.454     albertel 1891: 
1.490     www      1892:     $Apache::lonxml::internal_error=1;
                   1893: 
1.454     albertel 1894:     if (defined($Apache::inputtags::part)) {
                   1895: 	if ( @Apache::inputtags::response ) {
                   1896: 	    push(@errors,
                   1897: 		 &mt("This error occurred while processing response [_1] in part [_2]",
                   1898: 		     $Apache::inputtags::response[-1],
                   1899: 		     $Apache::inputtags::part));
                   1900: 	} else {
                   1901: 	    push(@errors,
                   1902: 		 &mt("This error occurred while processing part [_1]",
                   1903: 		     $Apache::inputtags::part));
                   1904: 	}
                   1905:     }
                   1906: 
1.348     albertel 1907:     if ( &show_error_warn_msg() ) {
1.336     albertel 1908: 	# If printing in construction space, put the error inside <pre></pre>
                   1909: 	push(@Apache::lonxml::error_messages,
1.484     bisitz   1910: 	     $Apache::lonxml::warnings_error_header
                   1911:              .'<div class="LC_error">'
                   1912:              .'<b>'.&mt('ERROR:').' </b>'.join("<br />\n",@errors)
                   1913:              ."</div>\n");
1.336     albertel 1914: 	$Apache::lonxml::warnings_error_header='';
                   1915:     } else {
                   1916: 	my $errormsg;
                   1917: 	my ($symb)=&Apache::lonnet::symbread();
                   1918: 	if ( !$symb ) {
                   1919: 	    #public or browsers
1.486     bisitz   1920: 	    $errormsg=&mt("An error occurred while processing this resource. The author has been notified.");
1.403     albertel 1921: 	}
1.413     albertel 1922: 	my $host=$Apache::lonnet::perlvar{'lonHostID'};
1.476     bisitz   1923: 	push(@errors,
                   1924:         &mt("The error occurred on host [_1]",
                   1925:              "<tt>$host</tt>"));
1.454     albertel 1926: 
                   1927: 	my $msg = join('<br />', @errors);
                   1928: 
1.336     albertel 1929: 	#notify author
1.403     albertel 1930: 	&Apache::lonmsg::author_res_msg($env{'request.filename'},$msg);
1.336     albertel 1931: 	#notify course
1.372     albertel 1932: 	if ( $symb && $env{'request.course.id'} ) {
1.380     www      1933: 	    my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1934: 	    my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.440     albertel 1935: 	    my (undef,%users)=&Apache::lonmsg::decide_receiver(undef,0,1,1,1);
1.372     albertel 1936: 	    my $declutter=&Apache::lonnet::declutter($env{'request.filename'});
1.435     raeburn  1937:             my $baseurl = &Apache::lonnet::clutter($declutter);
1.336     albertel 1938: 	    my @userlist;
                   1939: 	    foreach (keys %users) {
                   1940: 		my ($user,$domain) = split(/:/, $_);
                   1941: 		push(@userlist,"$user\@$domain");
1.380     www      1942: 		my $key=$declutter.'_'.$user.'_'.$domain;
                   1943: 		my %lastnotified=&Apache::lonnet::get('nohist_xmlerrornotifications',
                   1944: 						      [$key],
                   1945: 						      $cdom,$cnum);
                   1946: 		my $now=time;
                   1947: 		if ($now-$lastnotified{$key}>86400) {
1.434     raeburn  1948:                     my $title = &Apache::lonnet::gettitle($symb);
                   1949:                     my $sentmessage;
1.380     www      1950: 		    &Apache::lonmsg::user_normal_msg($user,$domain,
1.435     raeburn  1951: 		        "Error [$title]",$msg,'',$baseurl,'','',
1.434     raeburn  1952:                         \$sentmessage,$symb,$title,1);
1.380     www      1953: 		    &Apache::lonnet::put('nohist_xmlerrornotifications',
                   1954: 					 {$key => $now},
                   1955: 					 $cdom,$cnum);		
                   1956: 		}
1.336     albertel 1957: 	    }
1.372     albertel 1958: 	    if ($env{'request.role.adv'}) {
1.486     bisitz   1959: 		$errormsg=&mt("An error occurred while processing this resource. The course personnel ([_1]) and the author have been notified.",join(', ',@userlist));
1.336     albertel 1960: 	    } else {
1.486     bisitz   1961: 		$errormsg=&mt("An error occurred while processing this resource. The instructor has been notified.");
1.336     albertel 1962: 	    }
                   1963: 	}
                   1964: 	push(@Apache::lonxml::error_messages,"<b>$errormsg</b> <br />");
1.52      albertel 1965:     }
1.22      albertel 1966: }
1.49      albertel 1967: 
1.22      albertel 1968: sub warning {
1.295     albertel 1969:     $warningcount++;
1.261     albertel 1970:   
1.372     albertel 1971:     if ($env{'form.grade_target'} ne 'tex') {
1.348     albertel 1972: 	if ( &show_error_warn_msg() ) {
1.309     albertel 1973: 	    push(@Apache::lonxml::warning_messages,
1.484     bisitz   1974: 		 $Apache::lonxml::warnings_error_header
                   1975:                 .'<div class="LC_warning">'
                   1976:                 .&mt('[_1]W[_2]ARNING','<b>','</b>')."<b>:</b> ".join('<br />',@_)
                   1977:                 ."</div>\n"
1.482     bisitz   1978:                 );
1.295     albertel 1979: 	    $Apache::lonxml::warnings_error_header='';
                   1980: 	}
                   1981:     }
1.309     albertel 1982: }
                   1983: 
                   1984: sub info {
1.372     albertel 1985:     if ($env{'form.grade_target'} ne 'tex' 
                   1986: 	&& $env{'request.state'} eq 'construct') {
1.309     albertel 1987: 	push(@Apache::lonxml::info_messages,join('<br />',@_)."<br />\n");
                   1988:     }
                   1989: }
                   1990: 
                   1991: sub message_location {
                   1992:     return '__LONCAPA_INTERNAL_MESSAGE_LOCATION__';
                   1993: }
                   1994: 
                   1995: sub add_messages {
                   1996:     my ($msg)=@_;
                   1997:     my $result=join(' ',
                   1998: 		    @Apache::lonxml::info_messages,
                   1999: 		    @Apache::lonxml::error_messages,
                   2000: 		    @Apache::lonxml::warning_messages);
                   2001:     undef(@Apache::lonxml::info_messages);
                   2002:     undef(@Apache::lonxml::error_messages);
                   2003:     undef(@Apache::lonxml::warning_messages);
                   2004:     $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__/$result/;
                   2005:     $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__//g;
1.83      albertel 2006: }
                   2007: 
                   2008: sub get_param {
1.213     albertel 2009:     my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
                   2010:     if ( ! $context ) { $context = -1; }
                   2011:     my $args ='';
                   2012:     if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.297     sakharuk 2013:     if ( ! $Apache::lonxml::usestyle ) {
                   2014: 	$args=$Apache::lonxml::style_values.$args;
                   2015:     }
1.213     albertel 2016:     if ( ! $args ) { return undef; }
                   2017:     if ( $case_insensitive ) {
1.417     albertel 2018: 	if ($args =~ s/(my (?:.*))(\$\Q$param\E[,\)])/$1.lc($2)/ei) {
1.213     albertel 2019: 	    return &Apache::run::run("{$args;".'return $'.$param.'}',
                   2020:                                      $safeeval); #'
                   2021: 	} else {
                   2022: 	    return undef;
                   2023: 	}
                   2024:     } else {
1.417     albertel 2025: 	if ( $args =~ /my .*\$\Q$param\E[,\)]/ ) {
1.213     albertel 2026: 	    return &Apache::run::run("{$args;".'return $'.$param.'}',
                   2027:                                      $safeeval); #'
                   2028: 	} else {
                   2029: 	    return undef;
                   2030: 	}
                   2031:     }
1.22      albertel 2032: }
                   2033: 
1.132     albertel 2034: sub get_param_var {
1.213     albertel 2035:   my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1.132     albertel 2036:   if ( ! $context ) { $context = -1; }
                   2037:   my $args ='';
                   2038:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.297     sakharuk 2039:   if ( ! $Apache::lonxml::usestyle ) {
                   2040:       $args=$Apache::lonxml::style_values.$args;
                   2041:   }
1.230     albertel 2042:   &Apache::lonxml::debug("Args are $args param is $param");
1.213     albertel 2043:   if ($case_insensitive) {
1.419     albertel 2044:       if (! ($args=~s/(my (?:.*))(\$\Q$param\E[,\)])/$1.lc($2)/ei)) {
1.213     albertel 2045: 	  return undef;
                   2046:       }
1.419     albertel 2047:   } elsif ( $args !~ /my .*\$\Q$param\E[,\)]/ ) { return undef; }
1.132     albertel 2048:   my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1.230     albertel 2049:   &Apache::lonxml::debug("first run is $value");
1.341     albertel 2050:   if ($value =~ /^[\$\@\%][a-zA-Z_]\w*$/) {
1.230     albertel 2051:       &Apache::lonxml::debug("doing second");
                   2052:       my @result=&Apache::run::run("return $value",$safeeval,1);
                   2053:       if (!defined($result[0])) {
                   2054: 	  return $value
                   2055:       } else {
                   2056: 	  if (wantarray) { return @result; } else { return $result[0]; }
                   2057:       }
1.132     albertel 2058:   } else {
                   2059:     return $value;
                   2060:   }
                   2061: }
                   2062: 
1.438     albertel 2063: sub register_insert_xml {
                   2064:     my $parser = HTML::LCParser->new($Apache::lonnet::perlvar{'lonTabDir'}
                   2065: 				     .'/insertlist.xml');
                   2066:     my ($tagnum,$in_help)=(0,0);
1.442     albertel 2067:     my @alltags;
1.438     albertel 2068:     my $tag;
                   2069:     while (my $token = $parser->get_token()) {
                   2070: 	if ($token->[0] eq 'S') {
                   2071: 	    my $key;
                   2072: 	    if      ($token->[1] eq 'tag') {
                   2073: 		$tag = $token->[2]{'name'};
                   2074: 		$insertlist{"$tagnum.tag"} = $tag;
                   2075: 		$insertlist{"$tag.num"}   = $tagnum;
1.442     albertel 2076: 		push(@alltags,$tag);
1.438     albertel 2077: 	    } elsif ($in_help && $token->[1] eq 'file') {
                   2078: 		$key = $tag.'.helpfile';
                   2079: 	    } elsif ($in_help && $token->[1] eq 'description') {
                   2080: 		$key = $tag.'.helpdesc';
                   2081: 	    } elsif ($token->[1] eq 'description' ||
                   2082: 		     $token->[1] eq 'color'       ||
                   2083: 		     $token->[1] eq 'show'          ) {
                   2084: 		$key = $tag.'.'.$token->[1];
                   2085: 	    } elsif ($token->[1] eq 'insert_sub') {
                   2086: 		$key = $tag.'.function';
                   2087: 	    } elsif ($token->[1] eq 'help') {
                   2088: 		$in_help=1;
                   2089: 	    } elsif ($token->[1] eq 'allow') {
1.442     albertel 2090: 		$key = $tag.'.allow';
1.438     albertel 2091: 	    }
                   2092: 	    if (defined($key)) {
                   2093: 		$insertlist{$key} = $parser->get_text();
                   2094: 		$insertlist{$key} =~ s/(^\s*|\s*$ )//gx;
                   2095: 	    }
                   2096: 	} elsif ($token->[0] eq 'E') {
                   2097: 	    if      ($token->[1] eq 'tag') {
                   2098: 		undef($tag);
                   2099: 		$tagnum++;
                   2100: 	    } elsif ($token->[1] eq 'help') {
                   2101: 		undef($in_help);
                   2102: 	    }
                   2103: 	}
                   2104:     }
1.442     albertel 2105:     
                   2106:     # parse the allows and ignore tags set to <show>no</show>
                   2107:     foreach my $tag (@alltags) {	
1.445     albertel 2108:         next if (!exists($insertlist{"$tag.allow"}));
1.442     albertel 2109: 	my $allow =  $insertlist{"$tag.allow"};
                   2110:        	foreach my $element (split(',',$allow)) {
                   2111: 	    $element =~ s/(^\s*|\s*$ )//gx;
1.446     albertel 2112: 	    if (!exists($insertlist{"$element.show"})
                   2113:                 || $insertlist{"$element.show"} ne 'no') {
1.442     albertel 2114: 		push(@{ $insertlist{$tag.'.which'} },$element);
                   2115: 	    }
                   2116: 	}
                   2117:     }
1.438     albertel 2118: }
                   2119: 
                   2120: sub register_insert {
                   2121:     return &register_insert_xml(@_);
                   2122: #    &dump_insertlist('2');
                   2123: }
                   2124: 
                   2125: sub dump_insertlist {
                   2126:     my ($ext) = @_;
                   2127:     open(XML,">/tmp/insertlist.xml.$ext");
                   2128:     print XML ("<insertlist>");
                   2129:     my $i=0;
                   2130: 
                   2131:     while (exists($insertlist{"$i.tag"})) {
                   2132: 	my $tag = $insertlist{"$i.tag"};
                   2133: 	print XML ("
                   2134: \t<tag name=\"$tag\">");
                   2135: 	if (defined($insertlist{"$tag.description"})) {
                   2136: 	    print XML ("
                   2137: \t\t<description>".$insertlist{"$tag.description"}."</description>");
                   2138: 	}
                   2139: 	if (defined($insertlist{"$tag.color"})) {
                   2140: 	    print XML ("
                   2141: \t\t<color>".$insertlist{"$tag.color"}."</color>");
                   2142: 	}
                   2143: 	if (defined($insertlist{"$tag.function"})) {
                   2144: 	    print XML ("
                   2145: \t\t<insert_sub>".$insertlist{"$tag.function"}."</insert_sub>");
                   2146: 	}
                   2147: 	if (defined($insertlist{"$tag.show"})
                   2148: 	    && $insertlist{"$tag.show"} ne 'yes') {
                   2149: 	    print XML ("
                   2150: \t\t<show>".$insertlist{"$tag.show"}."</show>");
                   2151: 	}
                   2152: 	if (defined($insertlist{"$tag.helpfile"})) {
                   2153: 	    print XML ("
                   2154: \t\t<help>
                   2155: \t\t\t<file>".$insertlist{"$tag.helpfile"}."</file>");
                   2156: 	    if ($insertlist{"$tag.helpdesc"} ne '') {
                   2157: 		print XML ("
                   2158: \t\t\t<description>".$insertlist{"$tag.helpdesc"}."</description>");
                   2159: 	    }
                   2160: 	    print XML ("
                   2161: \t\t</help>");
                   2162: 	}
                   2163: 	if (defined($insertlist{"$tag.which"})) {
                   2164: 	    print XML ("
                   2165: \t\t<allow>".join(',',sort(@{ $insertlist{"$tag.which"} }))."</allow>");
                   2166: 	}
                   2167: 	print XML ("
                   2168: \t</tag>");
                   2169: 	$i++;
                   2170:     }
                   2171:     print XML ("\n</insertlist>\n");
                   2172:     close(XML);
                   2173: }
                   2174: 
1.98      albertel 2175: sub description {
1.437     albertel 2176:     my ($token)=@_;
                   2177:     my $tag = &get_tag($token);
                   2178:     return $insertlist{$tag.'.description'};
1.268     bowersj2 2179: }
                   2180: 
                   2181: # Returns a list containing the help file, and the description
                   2182: sub helpinfo {
1.437     albertel 2183:     my ($token)=@_;
                   2184:     my $tag = &get_tag($token);
                   2185:     return ($insertlist{$tag.'.helpfile'}, $insertlist{$tag.'.helpdesc'});
                   2186: }
                   2187: 
                   2188: sub get_tag {
                   2189:     my ($token)=@_;
                   2190:     my $tagnum;
                   2191:     my $tag=$token->[1];
                   2192:     foreach my $namespace (reverse(@Apache::lonxml::namespace)) {
                   2193: 	my $testtag = $namespace.'::'.$tag;
                   2194: 	$tagnum = $insertlist{"$testtag.num"};
                   2195: 	last if (defined($tagnum));
                   2196:     }
                   2197:     if (!defined($tagnum)) {
                   2198: 	$tagnum = $Apache::lonxml::insertlist{"$tag.num"};
                   2199:     }
                   2200:     return $insertlist{"$tagnum.tag"};
1.98      albertel 2201: }
1.123     albertel 2202: 
1.485     onken    2203: ############################################################
                   2204: #                                           PDF-FORM-METHODS
                   2205: 
                   2206: =pod
                   2207: 
                   2208: =item &print_pdf_radiobutton(fieldname, value,  text)
                   2209: 
                   2210: Returns a latexline to generate a PDF-Form-Radiobutton with Text.
                   2211: 
                   2212: $fieldname: PDF internalname of the radiobutton
                   2213: $value:     Value of radiobutton (read when dumping the PDF data)
                   2214: $text:      Text on the rightside of the radiobutton
                   2215: 
                   2216: =cut
                   2217: sub print_pdf_radiobutton {
                   2218:     my $result = '';
                   2219:     my ($fieldName, $value, $text) = @_;
                   2220:     $result .= '\begin{tabularx}{\textwidth}{p{0cm}X}'."\n";
                   2221:     $result .= '\radioButton[\symbolchoice{circle}]{'. 
                   2222:                $fieldName.'}{10bp}{10bp}{'.$value.'}&'.$text."\n";
                   2223:     $result .= '\end{tabularx}' . "\n";
                   2224:     $result .= '\hspace{2mm}' . "\n";
                   2225:     return $result;
                   2226: }
                   2227: 
                   2228: 
                   2229: =pod
                   2230: 
                   2231: =item &print_pdf_start_combobox(fieldname)
                   2232: 
                   2233: Starts a latexline to generate a PDF-Form-Combobox with text.
                   2234: 
                   2235: $fieldname: PDF internal name of the Combobox
                   2236: 
                   2237: =cut
                   2238: sub print_pdf_start_combobox {
                   2239:     my $result;
                   2240:     my ($fieldName) = @_;
                   2241:     $result .= '\begin{tabularx}{\textwidth}{p{2.5cm}X}'."\n";
                   2242:     $result .= '\comboBox[]{'.$fieldName.'}{2.3cm}{14bp}{'; # 
                   2243: 
                   2244:     return $result;
                   2245: }
                   2246: 
                   2247: 
                   2248: =pod
                   2249: 
                   2250: =item &print_pdf_add_combobox_option(options)
                   2251: 
                   2252: Generates a latexline to add Options to a PDF-Form-ComboBox.
                   2253: 
                   2254: $option: PDF internal name of the Combobox-Option
                   2255: 
                   2256: =cut
                   2257: sub print_pdf_add_combobox_option {
                   2258: 
                   2259:     my $result;
                   2260:     my ($option) = @_;  
                   2261: 
                   2262:     $result .= '('.$option.')';
                   2263:     
                   2264:     return $result;
                   2265: }
                   2266: 
                   2267: 
                   2268: =pod
                   2269: 
                   2270: =item &print_pdf_end_combobox(text) {
                   2271: 
                   2272: Returns latexcode to end a PDF-Form-Combobox with text.
                   2273: 
                   2274: =cut
                   2275: sub print_pdf_end_combobox {
                   2276:     my $result;
                   2277:     my ($text) = @_;
                   2278: 
                   2279:     $result .= '}&'.$text."\\\\\n";
                   2280:     $result .= '\end{tabularx}' . "\n";
                   2281:     $result .= '\hspace{2mm}' . "\n";
                   2282:     return $result;
                   2283: }
                   2284: 
                   2285: 
                   2286: =pod
                   2287: 
                   2288: =item &print_pdf_hiddenField(fieldname, user, domain)
                   2289: 
                   2290: Returns a latexline to generate a PDF-Form-hiddenField with userdata.
                   2291: 
                   2292: $fieldname label for hiddentextfield
                   2293: $user:    name of user
                   2294: $domain:  domain of user
                   2295: 
                   2296: =cut
                   2297: sub print_pdf_hiddenfield {
                   2298:     my $result;
                   2299:     my ($fieldname, $user, $domain) = @_;
                   2300: 
                   2301:     $result .= '\textField [\F{\FHidden}\F{-\FPrint}\V{'.$domain.'&'.$user.'}]{'.$fieldname.'}{0in}{0in}'."\n";
                   2302: 
                   2303:     return $result;
                   2304: }
                   2305: 
1.1       sakharuk 2306: 1;
                   2307: __END__
1.68      www      2308: 

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