File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.164: download - view: text, annotated - select for diffs
Fri Dec 23 19:38:01 2011 UTC (12 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
Bug #5324: alternative text for window when printing

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: #
    4: # $Id: scripttag.pm,v 1.164 2011/12/23 19:38:01 www 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: 
   29: package Apache::scripttag;
   30: 
   31: use strict;
   32: use Apache::lonnet;
   33: use Apache::lonlocal;
   34: use Apache::style();
   35: 
   36: #Globals
   37: # this used to pass around the standard callsub arguments to a tag func
   38: # so xmlparse can reenter the inner_xmlparse loop.
   39: 
   40: @Apache::scripttag::parser_env = ();
   41: BEGIN {
   42:   &Apache::lonxml::register('Apache::scripttag',
   43: 			    ('script','scriptlib','parserlib','import',
   44: 			     'window','windowlink','togglebox','display','storetc','physnet',
   45: 			     'standalone','comment','num','parse','algebra',
   46: 			     'LONCAPA_INTERNAL_TURN_STYLE_ON',
   47: 			     'LONCAPA_INTERNAL_TURN_STYLE_OFF'));
   48: }
   49: 
   50: sub start_LONCAPA_INTERNAL_TURN_STYLE_ON {
   51:     $Apache::lonxml::usestyle=1;
   52:     $Apache::lonxml::style_values='';
   53:     return ('','no');
   54: }
   55: 
   56: sub end_LONCAPA_INTERNAL_TURN_STYLE_ON {
   57:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   58:     my $end=&Apache::lonxml::get_param('end',$parstack,$safeeval);
   59:     if (defined($end)) {
   60: 	&Apache::lonxml::end_tag($tagstack,$parstack,$token);
   61:     }
   62:     return ('','no');
   63: }
   64: 
   65: sub start_LONCAPA_INTERNAL_TURN_STYLE_OFF {
   66:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   67:     $Apache::lonxml::usestyle=0;
   68:     my $end=&Apache::lonxml::get_param('end',$parstack,$safeeval);
   69:     if (!$end) {
   70: 	$Apache::lonxml::style_values=$$parstack[-1];
   71: 	$Apache::lonxml::style_end_values=$$parstack[-1];
   72:     } else {
   73: 	$Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
   74: 	$Apache::lonxml::style_end_values='';
   75:     }
   76:     return ('','no');
   77: }
   78: 
   79: sub end_LONCAPA_INTERNAL_TURN_STYLE_OFF {
   80:     return ('','no');
   81: }
   82: 
   83: sub start_script {
   84:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   85:   @Apache::scripttag::parser_env = @_;
   86:   my $result='';
   87:   my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
   88:   &Apache::lonxml::debug("found type of $type");
   89:   if ($type eq "loncapa/perl") {
   90:     if ( $target eq "modified" ) {
   91: 	$result=$token->[4].&Apache::edit::modifiedfield('/script',$parser);
   92:     } elsif ( $target eq 'web' || $target eq 'tex' ||
   93: 	      $target eq 'grade' || $target eq 'webgrade' ||
   94: 	      $target eq 'answer' || $target eq 'analyze' ) {
   95: 	my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
   96: 	if (!$Apache::lonxml::default_homework_loaded) {
   97: 	    &Apache::lonxml::default_homework_load($safeeval);
   98: 	}
   99: 	&Apache::run::run($bodytext,$safeeval);
  100: 	if (($target eq 'answer') &&
  101: 	    ($env{'form.answer_output_mode'} ne 'tex') &&
  102: 	    ($Apache::lonhomework::viewgrades == 'F')) {
  103: 	    $Apache::lonxml::evaluate--;
  104: 	    my (undef,undef,$udom,$uname)=&Apache::lonnet::whichuser();
  105: 	    $uname =~s/\W//g;
  106: 	    $udom  =~s/\W//g;
  107: 	    my $function_name = 
  108: 		join('_','LONCAPA_scriptvars',$uname,$udom,
  109: 		     $env{'form.counter'},$Apache::lonxml::curdepth);
  110:             &Apache::lonxml::add_script_result(
  111: 	             &Apache::loncommon::modal_adhoc_window($function_name,500,500,
  112:                             '<pre>'.&Apache::run::dump($target,$safeeval).'</pre>',
  113:                             &mt('Script Vars'))."<br />");
  114: 	}
  115:     } elsif ($target eq "edit" ) {
  116:       #&Apache::run::run($bodytext,$safeeval);
  117:       #$result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
  118: 	my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
  119: 	$result=&Apache::edit::tag_start($target,$token,'Script');
  120: 	$result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
  121:     } elsif ($target eq 'meta') {
  122: 	my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
  123:     }
  124:   } else {
  125:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
  126:       if ($target ne "meta" && $target ne 'tex' && $target ne 'answer') {
  127: 	  $result = $token->[4];
  128: 	  $result.=$bodytext;
  129:       }
  130:   }
  131:   return $result;
  132: }
  133: 
  134: sub end_script {
  135:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  136:   if ( $target eq "meta" ) { return ''; } 
  137:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  138:   my $result='';
  139:   #other script blocks need to survive
  140:   if ($type ne "loncapa/perl" && $target ne 'tex') {
  141:     return $token->[2];
  142:   } elsif ($target eq 'edit' ) {
  143:     return &Apache::edit::end_table();
  144:   } elsif ($target eq 'answer') {
  145:     $Apache::lonxml::evaluate++;
  146:   }
  147:   return '';
  148: }
  149: 
  150: sub start_display {
  151:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  152:   @Apache::scripttag::parser_env = @_;
  153:   my $result;
  154: 
  155:   if ( $target eq "modified" ) {
  156:       $result=$token->[4].&Apache::edit::modifiedfield("/display",$parser);
  157:   } elsif ( $target eq 'web' || $target eq 'tex' ||
  158: 	    $target eq 'grade' || $target eq 'webgrade' ||
  159: 	    $target eq 'answer' || $target eq 'analyze') {
  160:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
  161:       if (!$Apache::lonxml::default_homework_loaded) {
  162: 	  &Apache::lonxml::default_homework_load($safeeval);
  163:       }
  164:       $result=&Apache::run::run($bodytext,$safeeval);
  165:       if ($target eq 'grade' || $target eq 'answer' ||
  166: 	  $target eq 'analyze') {
  167: 	  # grade/answer/analyxe should produce no output but if we
  168: 	  # are redirecting, the redirecter should know what to do
  169: 	  # with the output
  170: 	  if (!$Apache::lonxml::redirection) { $result=''; }
  171:       }
  172:       $Apache::lonxml::post_evaluate=0;
  173:   } elsif ($target eq "edit" ) {
  174:     my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
  175:     #$result = 
  176:     #  "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
  177:     #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
  178:     $result=&Apache::edit::tag_start($target,$token,'Script With Display');
  179:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
  180:   } elsif ($target eq 'meta') {
  181:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
  182:   }
  183:   return $result;
  184: }
  185: 
  186: sub end_display {
  187:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  188:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  189:   return '';
  190: }
  191: 
  192: sub start_scriptlib {
  193:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  194:   my $bodytext;
  195:   my $result ='';
  196:   my $error='';
  197: 
  198:   if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  199:       $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
  200:       $target eq 'analyze' || $target eq 'webgrade') {
  201:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  202:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  203: 				     $$parstack[$#$parstack]);
  204:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  205: 					       $bodytext);
  206:     my $script=&Apache::lonnet::getfile($location);
  207:     if ($script == -1) {
  208:       if ($target eq 'edit') {
  209:         $error='</tr><tr><td>'.&mt('Errors').'</td><td colspan="2"><b>'.&mt(' Unable to find [_1]','<span class="LC_filename">'.$location.'</span>').'</b></td>'."\n";
  210:       } else {
  211: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
  212: 	return "";
  213:       }
  214:     }
  215:     &Apache::run::run($script,$safeeval);
  216:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
  217:   }
  218:   if ($target eq "edit" ) {
  219:     $result=
  220:       &Apache::edit::tag_start($target,$token,'New Script Functions').
  221: 	&Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
  222:             &Apache::edit::browse(undef,'textnode').
  223: 	  $error.'</td></tr>'.
  224: 	    &Apache::edit::end_table();
  225:   }
  226:   if ($target eq "modified" ) {
  227:       $result=$token->[4].&Apache::edit::modifiedfield("/scriptlib",$parser);
  228:   }
  229:   return $result;
  230: }
  231: 
  232: sub end_scriptlib {
  233:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  234:   my @result;
  235:   if ($target eq "edit" ) { $result[1]='no'; }
  236:   return @result;
  237: }
  238: 
  239: sub start_parserlib {
  240:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  241:   my $bodytext;
  242:   my $result ="";
  243:   my $error='';
  244:   if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  245:       $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
  246:       $target eq 'analyze' || $target eq 'webgrade') {
  247:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  248:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  249: 				     $$parstack[$#$parstack]);
  250:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  251: 					       $bodytext);
  252:     my $styletext=&Apache::lonnet::getfile($location);
  253:     #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
  254:     if ($styletext == -1) {
  255:       if ($target eq 'edit') {
  256: 	$error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  257:       } else {
  258: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  259: 	return "";
  260:       }
  261:     }
  262:     %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  263:   }
  264:   if ($target eq "edit" ) {
  265:     $result=
  266:       &Apache::edit::tag_start($target,$token,'New Tag Definitions').
  267: 	&Apache::edit::editline($token->[1],$bodytext,'',40).
  268: 	  $error.'</td></tr>'.
  269: 	    &Apache::edit::end_table();
  270:   }
  271:   if ($target eq "modified" ) {
  272:       $result=$token->[4].&Apache::edit::modifiedfield("/parserlib",$parser);
  273:   }
  274:   return $result;
  275: }
  276: 
  277: sub end_parserlib {
  278:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  279:   my @result;
  280:   if ($target eq "edit" ) { $result[1]='no'; }
  281:   return @result;
  282: }
  283: 
  284: sub start_window {
  285:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  286:     my $result = '';
  287:     if ($target eq 'web' || $target eq 'webgrade') {
  288: 	&Apache::lonxml::startredirection;
  289:     } elsif ($target eq 'tex') {
  290:         my $printtext=&Apache::lonxml::get_param('printtext',$parstack,$safeeval);
  291:         if ($printtext=~/\w/) {
  292: # If printtext is given, do not output any intervening information
  293:            &Apache::lonxml::startredirection;
  294:         } else {
  295:            $result = '\unskip\footnote{';
  296:         }
  297:     } elsif ($target eq 'edit') {
  298: 	$result.=&Apache::edit::tag_start($target,$token);
  299: 	$result.=&Apache::edit::text_arg('Text of Link:','linktext',$token,70);
  300: 	$result.=&Apache::edit::text_arg('Height:','height',$token,5);
  301: 	$result.=&Apache::edit::text_arg('Width:','width',$token,5);
  302:         $result.=&Apache::edit::text_arg('Printed text (optional):','printtext',$token,20);
  303: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  304:     } elsif ($target eq 'modified') {
  305: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  306: 						     $safeeval,'linktext',
  307: 						     'width','height');
  308: 	if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
  309:     }
  310:     return $result;  
  311: }
  312: 
  313: sub end_window {
  314:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  315:   my $result;
  316:   if ($target eq 'web' || $target eq 'webgrade') {
  317:     my $output=&Apache::lonxml::endredirection;
  318:     my $linktext= &Apache::lonxml::get_param('linktext',$parstack,$safeeval);
  319:     if (!$linktext) { $linktext='<sup>*</sup>'; }
  320:     my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
  321:     if (!$width) { $width='500'; }
  322:     my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
  323:     if (!$height) { $height='200'; }
  324:     $result=&Apache::loncommon::modal_adhoc_window
  325:            ("LONCAPA_newwindow_$Apache::lonxml::curdepth",$width,$height,$output,$linktext);
  326:   } elsif ($target eq 'tex') {
  327:       my $printtext=&Apache::lonxml::get_param('printtext',$parstack,$safeeval);
  328:       if ($printtext=~/\w/) {
  329: # If a "printtext" is given, proceed to retrieve all intervening information and trash it
  330:          my $output=&Apache::lonxml::endredirection;
  331: # Use printtext instead
  332:          $result=$printtext;
  333:       } else {
  334:          $result='}';
  335:       }
  336:   } else {
  337:       $result = '';
  338:   }
  339:   return $result; 
  340: }
  341: 
  342: 
  343: sub start_windowlink {
  344:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  345:     my $result = '';
  346:     if ($target eq 'web' || $target eq 'webgrade') {
  347:         &Apache::lonxml::startredirection;
  348:     } elsif ($target eq 'edit') {
  349:         $result.=&Apache::edit::tag_start($target,$token);
  350:         $result.=&Apache::edit::text_arg('Link:','href',$token,70);
  351:         $result.=&Apache::edit::text_arg('Height:','height',$token,5);
  352:         $result.=&Apache::edit::text_arg('Width:','width',$token,5);
  353:         $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  354:     } elsif ($target eq 'modified') {
  355:         my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  356:                                                      $safeeval,'href',
  357:                                                      'width','height');
  358:         if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
  359:     }
  360:     return $result;
  361: }
  362: 
  363: sub end_windowlink {
  364:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  365:   my $result;
  366:   if ($target eq 'web' || $target eq 'webgrade') {
  367:     my $output=&Apache::lonxml::endredirection;
  368:     my $href= &Apache::lonxml::get_param('href',$parstack,$safeeval);
  369:     if (!$href) { $href='/adm/rat/empty.html'; }
  370:     my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
  371:     if (!$width) { $width='500'; }
  372:     my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
  373:     if (!$height) { $height='200'; }
  374:     $result=&Apache::loncommon::modal_link($href,$output,$width,$height);
  375:   } else {
  376:       $result = '';
  377:   }
  378:   return $result;
  379: }
  380: 
  381: 
  382: sub start_togglebox {
  383:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  384:     my $result = '';
  385:     if ($target eq 'web' || $target eq 'webgrade') {
  386:         my $id="LONCAPA_togglebox_$Apache::lonxml::curdepth";
  387:         my $heading=&Apache::lonxml::get_param('heading',$parstack,$safeeval);
  388:         unless ($heading) { $heading=''; } else { $heading.=' '; }
  389:         my $showtext=&Apache::lonxml::get_param('showtext',$parstack,$safeeval);
  390:         my $hidetext=&Apache::lonxml::get_param('hidetext',$parstack,$safeeval);
  391:         my $headerbg=&Apache::lonxml::get_param('headerbg',$parstack,$safeeval);
  392:         $result=&Apache::loncommon::start_togglebox($id,$heading,$headerbg,$hidetext,$showtext);
  393:     } elsif ($target eq 'tex') {
  394:         my $heading=&Apache::lonxml::get_param('heading',$parstack,$safeeval);
  395:         unless ($heading) { $heading=''; } else { $heading.=' '; }
  396:         $result = "\n\n".'\fbox{{\bf '.$heading.'} \qquad '."\n";
  397:     } elsif ($target eq 'edit') {
  398:         $result.=&Apache::edit::tag_start($target,$token);
  399:         $result.=&Apache::edit::text_arg('Heading:','heading',$token,70);
  400:         $result.=&Apache::edit::text_arg('Header Background:','headerbg',$token,7);
  401:         $result.=&Apache::edit::text_arg('Show text:','showtext',$token,10);
  402:         $result.=&Apache::edit::text_arg('Hide text:','hidetext',$token,10);
  403:         $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  404:     } elsif ($target eq 'modified') {
  405:         my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  406:                                                      $safeeval,'heading',
  407:                                                      'showtext','hidetext',
  408:                                                      'headerbg','textbg');
  409:         if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
  410:     }
  411:     return $result;
  412: }
  413: 
  414: sub end_togglebox {
  415:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  416:   my $result;
  417:   if ($target eq 'web' || $target eq 'webgrade') {
  418:     $result=&Apache::loncommon::end_togglebox();
  419:   } elsif ($target eq 'tex') {
  420:       $result = "}\n\n";
  421:   } else {
  422:       $result = '';
  423:   }
  424:   return $result;
  425: }
  426: 
  427: 
  428: 
  429: sub start_import {
  430:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  431:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  432:   my $result ="";
  433: 
  434:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  435: 
  436:   if ($target eq 'web' ||  $target eq 'webgrade' || $target eq 'grade' 
  437:       || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze' ) {
  438:     # FIXME this probably needs to be smart about construction vs.
  439:     # non construction space.
  440:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  441:     my $file=&Apache::lonnet::getfile($location);
  442:     if ($file == -1) {
  443:       &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
  444:       return "";
  445:     }
  446:     my $importmode=&Apache::lonxml::get_param('importmode',$parstack,$safeeval);
  447:     if (($importmode eq 'problem') || ($importmode eq 'part')) {
  448: # We are using import to import published problems
  449:        if (($importmode eq 'problem') || ($file=~/<part[^<]*>/s)) {
  450: # We explicitly don't want this to be a separate part or the problem already has parts
  451:           $file=~s/^\s*<problem>/<library>/s;
  452: 	  $file=~s/<\/problem>\s*$/<\/library>/s;
  453:        } else {
  454: # We want this to be a separate part, but it currently is not
  455:           $file=~s/^\s*<problem>/<library><part>/s;
  456: 	  $file=~s/<\/problem>\s*$/<\/part><\/library>/s;
  457:        }
  458:     }
  459:     my $dir=$location;
  460:     $dir=~s:/[^/]*$::;
  461:     #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
  462:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
  463:     if (!$id) { $id=$Apache::lonxml::curdepth; }
  464:     push(@Apache::inputtags::import,$id);
  465:     push(@Apache::inputtags::importlist,$id);
  466: 
  467:     &Apache::lonxml::newparser($parser,\$file,$dir);
  468: 
  469:   } elsif ($target eq "edit" ) {
  470:     $result.=&Apache::edit::tag_start($target,$token);
  471:     my $location=$token->[1];
  472:     $location=~s/^\s*//s;
  473:     $location=~s/\s*$//s;
  474:     $result.=&Apache::edit::editline($location,$bodytext,'',40);
  475:     $result.=&Apache::edit::browse(undef,'textnode');
  476:     $result.= '&nbsp;<label>'.&mt('Import as:').
  477:               '<select name="importmode_'.$Apache::lonxml::curdepth.'">';
  478:     my %options=&Apache::lonlocal::texthash(''        => 'as standard library',
  479:                                             'problem' => 'as problem',
  480:                                             'part'    => 'as problem part(s)');
  481:     foreach my $option (sort(keys(%options))) {
  482:        $result.='<option value="'.$option.'"';
  483:        if ($option eq &Apache::lonxml::get_param('importmode',$parstack,$safeeval)) {
  484:           $result.=' selected="selected"';
  485:        }
  486:        $result.='>'.$options{$option}.'</option>';
  487:     }
  488:     $result.='</select></label>';
  489:     #FIXME this need to convert $bodytext to be a contruction space reference
  490:     #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  491:     #$result.="Click<a href=\"$location\">here</a> to edit<br />"
  492:   } elsif ($target eq 'modified') {
  493:       &Apache::edit::get_new_args($token,$parstack,$safeeval,'importmode');
  494:       $result='<import id="'.$token->[2]{'id'}.'" importmode="'.$token->[2]{'importmode'}.'">';
  495:       $result.=&Apache::edit::modifiedfield("/import",$parser);
  496:   } elsif ($target eq 'meta') {
  497:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
  498:     $result.='<import part="'.$Apache::inputtags::part;
  499:     if ($id) {
  500:       $result.='" id="'.$id;
  501:     }
  502:     $result.='" importmode="'.$token->[2]{'importmode'}.'">';
  503:     $result.=$bodytext;
  504:     $result.='</import>';
  505:   }
  506:   return $result;
  507: }
  508: 
  509: sub end_import {
  510:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  511:   pop(@Apache::inputtags::import);
  512:   my $result;
  513:   if ($target eq 'edit' ) { $result=&Apache::edit::end_row.
  514: 				&Apache::edit::end_table(); }
  515:   return $result;
  516: }
  517: 
  518: sub start_storetc {
  519:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  520:   my $result = '';
  521:   &Apache::lonxml::startredirection;
  522:   return $result; 
  523: }
  524: 
  525: sub end_storetc {
  526:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  527:     my $result;
  528:     my $output=&Apache::lonxml::endredirection;
  529:     $output =~ s/\"/\&quot\;/g;
  530:     $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
  531:     return $result;
  532: }
  533: 
  534: 
  535: sub start_physnet {
  536:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  537:     my $bodytext = '/adm/includes/physnet.sty';
  538:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  539:     my $cbistyletext=&Apache::lonnet::getfile($location);
  540: 
  541:     %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
  542:     $$parser['-1']->unget_token($token);
  543: #    if ( defined($$style{'physnet'}) ) {
  544: #        &Apache::lonxml::newparser($parser,\$$style{'physnet'});
  545: #    }
  546:     return "";
  547: }
  548: 
  549: sub end_physnet {
  550:   return '';
  551: }
  552: 
  553: sub start_standalone {
  554:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  555:   my $result='';
  556:   if ($target eq 'web' || $target eq 'webgrade') {
  557:     if ( $env{'request.course.id'} ) {
  558:       my $inside = &Apache::lonxml::get_all_text("/standalone",$parser,$style);
  559:     } else {
  560:       $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
  561:     }
  562:   }
  563:   return $result;
  564: }
  565: 
  566: sub end_standalone {
  567:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  568:   my $result='';
  569:   if ($target eq 'web' || $target eq 'webgrade' ) {
  570:     if ( $env{'request.course.id'} ) {
  571:     } else {
  572:       $result='</td></tr></table>';
  573:     }
  574:   }
  575:   return $result;
  576: }
  577: 
  578: sub start_comment {
  579:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  580:   my $result='';
  581:   if ($target eq 'edit') {
  582:     $result=&Apache::edit::tag_start($target,$token);
  583:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
  584:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
  585:   } elsif ( $target eq 'modified') {
  586:     $result=$token->[4].&Apache::edit::modifiedfield("/comment",$parser);
  587:   } elsif ( $target eq 'web'    || $target eq 'tex'  || $target eq 'grade'   ||
  588: 	    $target eq 'answer' || $target eq 'meta' || $target eq 'analyze' ||
  589: 	    $target eq 'webgrade') {
  590:     #normally throw away comments
  591:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
  592:   }
  593:   return $result;
  594: }
  595: 
  596: sub end_comment {
  597:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  598:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  599:   return '';
  600: }
  601: 
  602: 
  603: sub xmlparse {
  604:   my ($string) = @_;
  605:   &Apache::lonxml::debug("xmlparse recursion starting with $string");
  606:   # Apache::run::evaluate does an 'eval' on the name of the subroutine
  607:   # if it detects something that looks like a subroutine, this ends up calling
  608:   # things without any arguments and since perl is nice enough to pass
  609:   # along the default arguments when you don't explicitly say no arguments
  610:   # if you call &xmlparse, it gets &xmlparse passed as it argument.
  611:   # Same thing soccurs with &chemparse.
  612:   if ($string eq '&xmlparse') { return '&xmlparse'; }
  613:   if ($string eq '&chemparse') { return '&chemparse'; }
  614:   my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
  615:     @Apache::scripttag::parser_env;
  616:   my @parser;
  617:   &Apache::lonxml::newparser(\@parser,\$string);
  618:   &Apache::lonxml::startredirection();
  619:   my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
  620: 					     $parstack,\@parser,
  621: 					     $safeeval,$style);
  622:   $result.=&Apache::lonxml::endredirection();
  623:   &Apache::lonxml::debug("target is $target xmlparse recursion ending with $result");
  624:   return $result;
  625: }
  626: 
  627: sub start_num {
  628:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  629:     my $result = '';
  630:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/num",$parser);
  631:     if ($target eq 'tex' || $target eq 'web' || $target eq 'webgrade') {
  632: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
  633: 	if (!$Apache::lonxml::default_homework_loaded) {
  634: 	    &Apache::lonxml::default_homework_load($safeeval);
  635: 	}
  636: 	@Apache::scripttag::parser_env = @_;
  637: 	my $format=&Apache::lonxml::get_param('format',$parstack,$safeeval);
  638: 	$result=&Apache::run::run("return &prettyprint(q\0$inside\0,q\0$format\0);",$safeeval);
  639:     }    
  640:     return $result;
  641: }
  642: 
  643: sub end_num {
  644:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  645:     my $result = '';
  646:     return $result;
  647: }
  648: 
  649: sub start_parse {
  650:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  651:     my $result = '';
  652:     if ( $target eq 'web'    || $target eq 'tex'    ||
  653: 	 $target eq 'grade'  || $target eq 'answer' ||
  654: 	 $target eq 'analyze'|| $target eq 'webgrade') {
  655: 	my $inside = &Apache::lonxml::get_all_text_unbalanced("/parse",$parser);
  656: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
  657: 	if (!$Apache::lonxml::default_homework_loaded) {
  658: 	    &Apache::lonxml::default_homework_load($safeeval);
  659: 	}
  660: 	@Apache::scripttag::parser_env = @_;
  661: 	$result=&Apache::run::run("return &xmlparse(q\0$inside\0);",$safeeval);
  662: 	if ($target eq 'grade' || $target eq 'answer' ||
  663: 	    $target eq 'analyze') {
  664: 	    # grade/answer/analyxe should produce no output but if we
  665: 	    # are redirecting, the redirecter should know what to do
  666: 	    # with the output
  667: 	    if (!$Apache::lonxml::redirection) { $result=''; }
  668: 	}
  669:     }
  670:     return $result;
  671: }
  672: 
  673: sub end_parse {
  674:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  675:     my $result = '';
  676:     return $result;
  677: }
  678: 
  679: sub start_algebra {
  680:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  681:     my $result = '';
  682:     if ( $target eq 'web'     || $target eq 'tex'    ||
  683: 	 $target eq 'grade'   || $target eq 'answer' ||
  684: 	 $target eq 'analyze' || $target eq 'webgrade') {
  685: 	my $inside = &Apache::lonxml::get_all_text_unbalanced("/algebra",$parser);
  686: 	$inside = &Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
  687: 	if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  688: 	    my $style=&Apache::lonxml::get_param('style',$parstack,$safeeval);
  689: 	    $result=&Apache::lontexconvert::algebra($inside,$target,$style,$parstack,$safeeval);
  690: 	}
  691: 	$Apache::lonxml::post_evaluate=0;
  692:     }
  693:     return $result;
  694: }
  695: 
  696: sub end_algebra {
  697:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  698:     my $result = '';
  699:     return $result;
  700: }
  701: 
  702: 1;
  703: __END__
  704: 
  705: =pod
  706: 
  707: =head1 NAME
  708: 
  709: Apache::scripttag.pm
  710: 
  711: =head1 SYNOPSIS
  712: 
  713: implements <script>, <scriptlib>, <parserlib>,
  714: and <import>
  715: 
  716: This is part of the LearningOnline Network with CAPA project
  717: described at http://www.lon-capa.org.
  718: 
  719: =cut
  720: 

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