File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.87: download - view: text, annotated - select for diffs
Tue May 13 20:41:08 2003 UTC (21 years, 1 month ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Changes to print single resource with answers. I've switched off some html stuff which is not neccessary in this particular case.

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

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