File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.88: download - view: text, annotated - select for diffs
Wed May 14 13:33:00 2003 UTC (21 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- propagating print_answer -> answer_output_mode shift

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: #
    4: # $Id: scripttag.pm,v 1.88 2003/05/14 13:33:00 albertel 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: # 2/21 Guy
   29: # 8/20 Gerd Kortemeyer
   30: 
   31: package Apache::scripttag;
   32: 
   33: use strict;
   34: use Apache::lonnet;
   35: use Apache::style;
   36: 
   37: #Globals
   38: # this used to pass around the standard callsub arguments to a tag func
   39: # so xmlparse can reenter the inner_xmlparse loop.
   40: 
   41: @Apache::scripttag::parser_env = ();
   42: BEGIN {
   43:   &Apache::lonxml::register('Apache::scripttag',
   44: 			    ('script','scriptlib','parserlib','import',
   45: 			     'window','display','storetc','physnet',
   46: 			     'standalone','comment',
   47: 			     'LONCAPA_INTERNAL_TURN_STYLE_ON',
   48: 			     'LONCAPA_INTERNAL_LONHTTPD_PORT'));
   49: }
   50: 
   51: sub start_LONCAPA_INTERNAL_TURN_STYLE_ON {
   52:     $Apache::lonxml::usestyle=1;
   53:     return ('','no');
   54: }
   55: 
   56: sub end_LONCAPA_INTERNAL_TURN_STYLE_ON {
   57:     return ('','no');
   58: }
   59: 
   60: sub start_LONCAPA_INTERNAL_LONHTTPD_PORT {
   61:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   62:     if ($target eq 'web') {
   63: 	my $lonhttpdPort=$Apache::lonnet::perlvar{'lonhttpdPort'};
   64: 	if (!defined($lonhttpdPort)) { $lonhttpdPort='8080'; }
   65: 	return '<script type="text/javascript">var lonhttpdport=\''.
   66: 	    $lonhttpdPort.'\';</script>';
   67:     }
   68:     return ('','no');
   69: }
   70: 
   71: sub end_LONCAPA_INTERNAL_LONHTTPD_PORT {
   72:     return ('','no');
   73: }
   74: 
   75: sub start_script {
   76:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   77:   @Apache::scripttag::parser_env = @_;
   78:   my $result='';
   79:   my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
   80:   &Apache::lonxml::debug("found type of $type");
   81:   if ($type eq "loncapa/perl") {
   82:     my $bodytext=&Apache::lonxml::get_all_text("/script",$parser);
   83:     if ( $target eq "modified" ) {
   84:       $result=$token->[4].&Apache::edit::modifiedfield();
   85:     } elsif ( $target eq 'web' || $target eq 'tex' ||
   86: 	      $target eq 'grade' || $target eq 'answer' ||
   87: 	      $target eq 'analyze' ) {
   88: 	if (!$Apache::lonxml::default_homework_loaded) {
   89: 	    &Apache::lonxml::default_homework_load($safeeval);
   90: 	}
   91: 	&Apache::run::run($bodytext,$safeeval);
   92: 	if (($target eq 'answer') &&
   93: 	    ($ENV{'form.answer_output_mode'} ne 'tex') &&
   94: 	    ($Apache::lonhomework::viewgrades == 'F')) {
   95: 	    $Apache::lonxml::evaluate--;
   96: 	    $result.="<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200,scrollbars=1\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'&lt;html&gt;&lt;head&gt;&lt;title&gt;newwindow&lt;/title&gt;&lt;/head&gt;&lt;body bgcolor=&quot;#FFFFFF&quot;&gt;&lt;pre&gt;";
   97: 	    my $listing= &HTML::Entities::encode(&Apache::run::dump($target,$safeeval));
   98: 
   99: 	    $result.=$listing;
  100: 	    $result.= "&lt;/pre&gt;&lt;/body&gt;&lt;/html&gt;\');newWindow.document.close();void(0);\">Script Vars</a><br />";
  101: 	}
  102:     } elsif ($target eq "edit" ) {
  103:       #&Apache::run::run($bodytext,$safeeval);
  104:       #$result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
  105:       $result=&Apache::edit::tag_start($target,$token,'Script');
  106:       $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
  107:     }
  108:   } else {
  109:     if ($target ne "meta") {
  110:       $result = $token->[4];
  111:       my $bodytext=&Apache::lonxml::get_all_text("/script",$parser);
  112:       $result.=$bodytext;
  113:     }
  114:   }
  115:   return $result;
  116: }
  117: 
  118: sub end_script {
  119:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  120:   if ( $target eq "meta" ) { return ''; } 
  121:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  122:   my $result='';
  123:   #other script blocks need to survive
  124:   if ($type ne "loncapa/perl") {
  125:     return $token->[2];
  126:   } elsif ($target eq 'edit' ) {
  127:     return &Apache::edit::end_table();
  128:   } elsif ($target eq 'answer') {
  129:     $Apache::lonxml::evaluate++;
  130:   }
  131:   return '';
  132: }
  133: 
  134: sub start_display {
  135:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  136: 
  137:   my $result;
  138:   my $bodytext=&Apache::lonxml::get_all_text("/display",$parser);
  139: 
  140:   if ( $target eq "modified" ) {
  141:     $result=$token->[4].&Apache::edit::modifiedfield();
  142:   } elsif ( $target eq 'web' || $target eq 'tex' ||
  143: 	    $target eq 'grade' || $target eq 'answer' ||
  144: 	      $target eq 'analyze') {
  145:       if (!$Apache::lonxml::default_homework_loaded) {
  146: 	  &Apache::lonxml::default_homework_load($safeeval);
  147:       }
  148:       $result=&Apache::run::run($bodytext,$safeeval);
  149:       if ($target eq 'grade' || $target eq 'answer' ||
  150: 	  $target eq 'analyze') {
  151: 	  $result=''; # grade should produce no output
  152:       }
  153:   } elsif ($target eq "edit" ) {
  154:     #$result = 
  155:     #  "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
  156:     #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
  157:     $result=&Apache::edit::tag_start($target,$token,'Script With Display');
  158:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
  159:   }
  160:   return $result;
  161: }
  162: 
  163: sub end_display {
  164:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  165:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  166:   return '';
  167: }
  168: 
  169: sub start_scriptlib {
  170:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  171:   my $bodytext;
  172:   my $result ='';
  173:   my $error='';
  174: 
  175:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || 
  176:       $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
  177:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  178:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  179: 				     $$parstack[$#$parstack]);
  180:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  181: 					       $bodytext);
  182:     my $script=&Apache::lonnet::getfile($location);
  183:     if ($script == -1) {
  184:       if ($target eq 'edit') {
  185:         $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  186:       } else {
  187: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
  188: 	return "";
  189:       }
  190:     }
  191:     &Apache::run::run($script,$safeeval);
  192:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
  193:   }
  194:   if ($target eq "edit" ) {
  195:     $result=
  196:       &Apache::edit::tag_start($target,$token,'New Script Functions').
  197: 	&Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
  198:             &Apache::edit::browse(undef,'textnode').
  199: 	  $error.'</td></tr>'.
  200: 	    &Apache::edit::end_table();
  201:   }
  202:   if ($target eq "modified" ) {
  203:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  204:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  205:     &Apache::lonxml::debug($result);
  206:   }
  207:   return $result;
  208: }
  209: 
  210: sub end_scriptlib {
  211:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  212:   my @result;
  213:   if ($target eq "edit" ) { $result[1]='no'; }
  214:   return @result;
  215: }
  216: 
  217: sub start_parserlib {
  218:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  219:   my $bodytext;
  220:   my $result ="";
  221:   my $error='';
  222:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' ||
  223:       $target eq 'edit' || $target eq 'answer' || $target eq 'analyze') {
  224:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  225:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  226: 				     $$parstack[$#$parstack]);
  227:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  228: 					       $bodytext);
  229:     my $styletext=&Apache::lonnet::getfile($location);
  230:     #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
  231:     if ($styletext == -1) {
  232:       if ($target eq 'edit') {
  233: 	$error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  234:       } else {
  235: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  236: 	return "";
  237:       }
  238:     }
  239:     %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  240:   }
  241:   if ($target eq "edit" ) {
  242:     $result=
  243:       &Apache::edit::tag_start($target,$token,'New Tag Definitions').
  244: 	&Apache::edit::editline($token->[1],$bodytext,'',40).
  245: 	  $error.'</td></tr>'.
  246: 	    &Apache::edit::end_table();
  247:   }
  248:   if ($target eq "modified" ) {
  249:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  250:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  251:     &Apache::lonxml::debug($result);
  252:   }
  253:   return $result;
  254: }
  255: 
  256: sub end_parserlib {
  257:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  258:   my @result;
  259:   if ($target eq "edit" ) { $result[1]='no'; }
  260:   return @result;
  261: }
  262: 
  263: sub start_window {
  264:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  265:   my $result = '';
  266:   if ($target eq 'web') {
  267:     &Apache::lonxml::startredirection;
  268:   }  elsif ($target eq 'tex') {
  269:        $result = '\unskip\footnote{';
  270:    }
  271:   return $result;  
  272: }
  273: 
  274: sub end_window {
  275:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  276:   my $result;
  277:   if ($target eq 'web') {
  278:     my $output=&Apache::lonxml::endredirection;
  279:     $output =~ s/\"/\&quot\;/g;
  280:     $result = "<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200,scrollbars=1\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<html><head><title>newwindow</title></head><body bgcolor=&quot;#FFFFFF&quot;> $output </body></html>\');newWindow.document.close();void(0);\"><sup>*</sup></a>";
  281:   } elsif ($target eq 'tex') {
  282:       $result = '}';
  283:   } else {
  284:       $result = '';
  285:   }
  286:   return $result; 
  287: }
  288: 
  289: sub start_import {
  290:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  291:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  292:   my $result ="";
  293: 
  294:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  295: 
  296:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  297:       $target eq 'tex' || $target eq 'analyze' ) {
  298:     # FIXME this probably needs to be smart about construction vs.
  299:     # non construction space.
  300:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  301:     my $file=&Apache::lonnet::getfile($location);
  302:     if ($file == -1) {
  303:       &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
  304:       return "";
  305:     }
  306: 
  307:     my $dir=$location;
  308:     $dir=~s:/[^/]*$::;
  309:     #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
  310:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  311:     if (!$id) { $id=$Apache::lonxml::curdepth; }
  312:     push(@Apache::inputtags::import,$id);
  313: 
  314: 	&Apache::lonxml::newparser($parser,\$file,$dir);
  315: 
  316:   } elsif ($target eq "edit" ) {
  317:     $result.=&Apache::edit::tag_start($target,$token);
  318:     $result.=&Apache::edit::editline($token->[1],$bodytext,'',40);
  319:     $result.=&Apache::edit::browse(undef,'textnode');
  320:     #FIXME this need to convert $bodytext to be a contruction space reference
  321:     #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  322:     #$result.="Click<a href=\"$location\">here</a> to edit<br />"
  323:   } elsif ($target eq 'modified') {
  324:     $bodytext=$$parser[$#$parser]->get_text("/import");
  325:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  326:     &Apache::lonxml::debug($result);
  327:   } elsif ($target eq 'meta') {
  328:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  329:     $result.='<import part="'.$Apache::inputtags::part;
  330:     if ($id) {
  331:       $result.='" id="'.$id;
  332:     }
  333:     $result.='">';
  334:     $result.=$bodytext;
  335:     $result.='</import>';
  336:   }
  337:   return $result;
  338: }
  339: 
  340: sub end_import {
  341:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  342:   pop(@Apache::inputtags::import);
  343:   my $result;
  344:   if ($target eq 'edit' ) { $result=&Apache::edit::end_table(); }
  345:   return $result;
  346: }
  347: 
  348: sub start_storetc {
  349:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  350:   my $result = '';
  351:   &Apache::lonxml::startredirection;
  352:   return $result; 
  353: }
  354: 
  355: sub end_storetc {
  356:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  357:     my $result;
  358:     my $output=&Apache::lonxml::endredirection;
  359:     $output =~ s/\"/\&quot\;/g;
  360:     $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
  361:     return $result;
  362: }
  363: 
  364: 
  365: sub start_physnet {
  366:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  367:     my $bodytext = '/adm/includes/physnet.sty';
  368:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  369:     my $cbistyletext=&Apache::lonnet::getfile($location);
  370: 
  371:     %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
  372:     $$parser['-1']->unget_token($token);
  373: #    if ( defined($$style{'physnet'}) ) {
  374: #        &Apache::lonxml::newparser($parser,\$$style{'physnet'});
  375: #    }
  376:     return "";
  377: }
  378: 
  379: sub end_physnet {
  380:   return '';
  381: }
  382: 
  383: sub start_standalone {
  384:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  385:   my $result='';
  386:   if ($target eq 'web' ) {
  387:     if ( $ENV{'request.course.id'} ) {
  388:       my $inside = &Apache::lonxml::get_all_text("/standalone",$parser);
  389:     } else {
  390:       $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
  391:     }
  392:   }
  393:   return $result;
  394: }
  395: 
  396: sub end_standalone {
  397:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  398:   my $result='';
  399:   if ($target eq 'web' ) {
  400:     if ( $ENV{'request.course.id'} ) {
  401:     } else {
  402:       $result='</td></tr></table>';
  403:     }
  404:   }
  405:   return $result;
  406: }
  407: 
  408: sub start_comment {
  409:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  410:   my $result='';
  411:   if ($target eq 'edit') {
  412:     $result=&Apache::edit::tag_start($target,$token);
  413:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser);
  414:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
  415:   } elsif ( $target eq 'modified') {
  416:     $result=$token->[4].&Apache::edit::modifiedfield($token);
  417:   } elsif ( $target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  418: 	    $target eq 'answer' || $target eq 'meta' || $target eq 'analyze') {
  419:     #normally throw away comments
  420:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser);
  421:   }
  422:   return $result;
  423: }
  424: 
  425: sub end_comment {
  426:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  427:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
  428:   return '';
  429: }
  430: 
  431: 
  432: sub xmlparse {
  433:   my ($string) = @_;
  434: #  &Apache::lonxml::debug("Got $string");
  435:   my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
  436:     @Apache::scripttag::parser_env;
  437:   my @parser;
  438:   &Apache::lonxml::newparser(\@parser,\$string);
  439:   my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
  440: 					     $parstack,\@parser,
  441: 					     $safeeval,$style);
  442:   return $result;
  443: }
  444: 
  445: 1;
  446: __END__

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