File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.39: download - view: text, annotated - select for diffs
Thu May 31 22:38:36 2001 UTC (23 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
 - adding get_param calls to replace the junk that went before

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: # 2/21 Guy
    4: 
    5: package Apache::scripttag; 
    6: 
    7: use strict;
    8: use Apache::lonnet;
    9: 
   10: sub BEGIN {
   11:   &Apache::lonxml::register('Apache::scripttag',('script','scriptlib',
   12: 						 'parserlib','import',
   13:                                                  'window','display'));
   14: }
   15: 
   16: sub start_script {
   17:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   18:   my $result='';
   19:   my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
   20:   &Apache::lonxml::debug("found type of $type");
   21:   if ($type eq "loncapa/perl") {
   22:     my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
   23:     
   24:     if ( $target eq "modified" ) {
   25:       $result=$token->[4].$bodytext;
   26:     } elsif ( $target eq "web" || $target eq "grade" ) {
   27:       &Apache::run::run($bodytext,$safeeval);
   28:     } elsif ($target eq "edit" ) {
   29:       &Apache::run::run($bodytext,$safeeval);
   30:       $result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
   31:       $result.=&Apache::edit::editfield($token->[1],$bodytext,'',50,5);
   32:     }
   33:   } else {
   34:     if ($target ne "meta") { $result = $token->[4]; }
   35:   }
   36:   return $result;
   37: }
   38: 
   39: sub end_script {
   40:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   41:   if ( $target eq "meta" ) { return ''; } 
   42:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
   43:   my $result='';
   44:   #other script blocks need to survive
   45:   if ($type ne "loncapa/perl") { return $token->[2]; }
   46:   return '';
   47: }
   48: 
   49: sub start_display {
   50:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   51: 
   52:   my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
   53: 
   54:   if ( $target eq "modified" ) {
   55:   }
   56:   my $result=&Apache::run::run($bodytext,$safeeval);
   57:   if ($target eq 'grade' ) {
   58:     # grade should produce no output
   59:     $result='';
   60:   }
   61:   if ($target eq "edit" ) {
   62:     $result = 
   63:       "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
   64:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
   65:   }
   66:   return $result;
   67: }
   68: 
   69: sub end_display {
   70:   return ''
   71: }
   72: 
   73: sub start_scriptlib {
   74:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   75:   my $bodytext;
   76:   my $result ='';
   77:   my $error='';
   78: 
   79:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit') {
   80:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
   81:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
   82: 				     $$parstack[$#$parstack]);
   83:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
   84: 					       $bodytext);
   85:     my $script=&Apache::lonnet::getfile($location);
   86:     if ($script == -1) {
   87:       if ($target eq 'edit') {
   88:         $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
   89:       } else {
   90: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
   91: 	return "";
   92:       }
   93:     }
   94:     &Apache::run::run($script,$safeeval);
   95:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
   96:   }
   97:   if ($target eq "edit" ) {
   98:     $result=
   99:       &Apache::edit::tag_start($target,$token).
  100: 	&Apache::edit::editfield($token->[1],$bodytext,'New Script Functions',40,1).
  101: 	  $error;
  102:   }
  103:   if ($target eq "modified" ) {
  104:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
  105:     $result=&Apache::edit::modifiedfield($token);
  106:     &Apache::lonxml::debug($result);
  107:   }
  108:   return $result;
  109: }
  110: 
  111: sub end_scriptlib {
  112:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  113:   my $result='';
  114: #  if ($target eq "edit" ) { $result=" "; }
  115:   return $result;
  116: }
  117: 
  118: sub start_parserlib {
  119:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  120:   my $bodytext;
  121:   my $result ="";
  122:   my $error='';
  123: 
  124:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit') {
  125:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  126:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  127: 				     $$parstack[$#$parstack]);
  128:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  129: 					       $bodytext);
  130:     my $styletext=&Apache::lonnet::getfile($location);
  131:     #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
  132:     if ($styletext == -1) {
  133:       if ($target eq 'edit') {
  134: 	$error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
  135:       } else {
  136: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  137: 	return "";
  138:       }
  139:     }
  140:     %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  141:   }
  142:   if ($target eq "edit" ) {
  143:     $result=
  144:       &Apache::edit::tag_start($target,$token).
  145: 	&Apache::edit::editfield($token->[1],$bodytext,"New Tag Definitions",40,1).
  146: 	  $error;
  147:   }
  148:   if ($target eq "modified" ) {
  149:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  150:     $result=&Apache::edit::modifiedfield($token);
  151:     &Apache::lonxml::debug($result);
  152:   }
  153:   return $result;
  154: }
  155: 
  156: sub end_parserlib {
  157:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  158:   my $result='';
  159: #  if ($target eq "edit" ) { 
  160: #    $result=&Apache::edit::tag_end($target,$token);
  161: #  }
  162:   return $result;
  163: }
  164: 
  165: sub start_window {
  166:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  167:   my $result = '';
  168:   if ($target eq 'web') {
  169:     &Apache::lonxml::startredirection;
  170:   }  elsif ($target eq 'tex') {
  171:        $result = '\footnote{';
  172:    }
  173:   return $result;  
  174: }
  175: 
  176: sub end_window {
  177:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  178:   my $result;
  179:   if ($target eq 'web') {
  180:     my $output=&Apache::lonxml::endredirection;
  181:     $output =~ s/\"/\&quot\;/g;
  182:     $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>";
  183:   } elsif ($target eq 'tex') {
  184:       $result = '}';
  185:   } else {
  186:       $result = '';
  187:   }
  188:   return $result; 
  189: }
  190: 
  191: sub start_import {
  192:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  193:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  194:   my $result ="";
  195: 
  196:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  197:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  198:   my $file=&Apache::lonnet::getfile($location);
  199:   if ($file == -1) {
  200:     &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
  201:     return "";
  202:   }
  203: 
  204:   my $dir=$location;
  205:   $dir=~s:/[^/]*$::;  
  206: #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
  207:   &Apache::lonxml::newparser($parser,\$file,$dir);
  208: 
  209:   if ($target eq "edit" ) {
  210:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
  211:     $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br />"
  212:   }
  213:   return '';
  214: }
  215: 
  216: sub end_import {
  217: }
  218: 
  219: sub start_meta {
  220:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  221:   my $result = '';
  222:     if ($target ne 'meta') {
  223:        $result = &Apache::lonxml::get_all_text("/meta",$$parser[$#$parser]); 
  224:        $result = '';
  225:       return $result; 
  226:     } else {
  227:        return $result; 
  228:     }
  229: }
  230: 
  231: sub end_meta {
  232: }
  233: 
  234: sub getfilenothere {
  235:   my ($filename) = @_;
  236:   my $a="";
  237:   
  238:   $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
  239:   $filename="/home/httpd/html/res".$filename;
  240:   if (! -e $filename ) {
  241:     &Apache::lonnet::subscribe($filename);
  242:     &Apache::lonnet::repcopy($filename);
  243:   }
  244:   if (! -e $filename ) { return -1; };
  245:   my $fh=Apache::File->new($filename);
  246:   while (<$fh>) {
  247:       $a .=$_;
  248:   }
  249:   return $a
  250: }
  251: 
  252: 1;
  253: __END__

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