File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.563: download - view: text, annotated - select for diffs
Mon Nov 27 22:44:21 2023 UTC (6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Move &setmode_javascript() from structuretags.pm to lonxml.pm to facilitate
  reuse.

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

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