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

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

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