File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.26: download - view: text, annotated - select for diffs
Tue Dec 5 22:07:26 2000 UTC (23 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- properly sets the datastructures and delivers grades

    1: # The LearningOnline Network with CAPA
    2: # <script> definiton
    3: 
    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:                                                  'footnote','display'));
   14: }
   15: 
   16: sub start_script {
   17:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   18:   my $args ='';
   19:   if ( $#$parstack > -1 ) {
   20:     $args=$$parstack[$#$parstack];
   21:   }
   22:   my $type = &Apache::run::run("{$args;".'return $type}',$safeeval);
   23:   my $result='';
   24:   #&Apache::lonxml::debug("found type of $type");
   25:   if ($type eq "loncapa/perl") {
   26:     my $bodytext=&Apache::lonxml::get_all_text("/script",$$parser[$#$parser]);
   27:     
   28:     if ( $target eq "modified" ) {
   29:     }
   30:     &Apache::run::run($bodytext,$safeeval);
   31:     
   32:     if ($target eq "edit" ) {
   33:       $result="<br> &lt;$token->[1]&gt; output: <br>$bodytext<br>Source:<br>";
   34:       $result.=&editfield($token->[1],$bodytext);
   35:     }
   36:   } else {
   37:     $result = $token->[4];
   38:   }
   39:   return $result;
   40: }
   41: 
   42: sub end_script {
   43:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   44: #  return '';
   45:   return $token->[2]; 
   46: }
   47: 
   48: sub start_display {
   49:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   50:   my $args ='';
   51:   if ( $#$parstack > -1 ) {
   52:     $args=$$parstack[$#$parstack];
   53:   }
   54:   my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
   55:   
   56:   if ( $target eq "modified" ) {
   57:   }
   58:   my $result=&Apache::run::run($bodytext,$safeeval);
   59:   if ($target eq 'grade' ) {
   60:     # grade should produce no output
   61:     $result='';
   62:   }
   63:   if ($target eq "edit" ) {
   64:     $result = 
   65:       "<br> &lt;$token->[1]&gt; output: <br>$bodytext<br>Source:<br>";
   66:     $result.=&editfield($token->[1],$bodytext);
   67:   }
   68:   return $result;
   69: }
   70: 
   71: sub end_display {
   72: }
   73: 
   74: sub start_scriptlib {
   75:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   76:   my $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
   77:   my $result ="";
   78: 
   79:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
   80: 				   $$parstack[$#$parstack]);
   81:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
   82: 					     $bodytext);
   83:   my $script=&Apache::lonnet::getfile($location);
   84:   if ($script == -1) {
   85:     &Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
   86:     return "";
   87:   }
   88:   &Apache::run::run($script,$safeeval);
   89:   #&Apache::lonxml::debug("ran $bodytext:<br>".&Apache::lonnet::getfile($bodytext)."<br>");
   90: 
   91:   if ($target eq "edit" ) {
   92:     $result.=&editfield($token->[1],$bodytext);
   93:   }
   94:   return $result;
   95: }
   96: 
   97: sub end_scriptlib {}
   98: 
   99: sub start_parserlib {
  100:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  101:   my $bodytext=$$parser[$#$parser]->get_text("/parserlib");
  102:   my $result ="";
  103: 
  104:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
  105: 				  $$parstack[$#$parstack]);
  106:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
  107: 					     $bodytext);
  108:   my $styletext=&Apache::lonnet::getfile($location);
  109:   if ($styletext == -1) {
  110:     &Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
  111:     return "";
  112:   }
  113:   %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
  114: 
  115:   if ($target eq "edit" ) {
  116:     $result=&editfield($token->[1],$bodytext);
  117:   }
  118:   return $result;
  119: }
  120: 
  121: sub end_parserlib {
  122: }
  123: 
  124: sub start_footnote {
  125:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  126:   my $result = '';
  127:   $Apache::lonxml::redirection = 0;
  128:   return $result;  
  129: }
  130: 
  131: sub end_footnote {
  132:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  133:   $Apache::lonxml::outputstack =~ s/\"/\&quot\;/g;
  134:   my $result = "<a href=\"javascript:newWindow=open(\'\',\'new_W\',\'width=500,height=200\');newWindow.document.open(\'text/html\',\'replace\');newWindow.document.writeln(\'<html><head><title>newwindow</title></head><body bgcolor=&quot;#FFFFFF&quot;> $Apache::lonxml::outputstack </body></html>\');newWindow.document.close();void(0);\"><sup>*</sup></a>";
  135:    $Apache::lonxml::outputstack = "";
  136:    $Apache::lonxml::redirection = 1;
  137:   return $result; 
  138: }
  139: 
  140: sub start_import {
  141:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  142:   my $bodytext=$$parser[$#$parser]->get_text("/import");
  143:   my $result ="";
  144: 
  145:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
  146:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
  147:   my $file=&Apache::lonnet::getfile($location);
  148:   if ($file == -1) {
  149:     &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
  150:     return "";
  151:   }
  152: 
  153:   my $dir=$location;
  154:   $dir=~s:/[^/]*$::;
  155:   &Apache::lonxml::newparser($parser,\$file,$dir);
  156: 
  157:   if ($target eq "edit" ) {
  158:     $result.=&editfield($token->[1],$bodytext);
  159:     $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br></br>"
  160:   }
  161: }
  162: 
  163: sub end_import {
  164: }
  165: 
  166: sub editfield {
  167:   my ($tag,$data)=@_;
  168:   
  169:   my $count=0;
  170:   my $maxlength=-1;
  171:   map { $count++;
  172: 	if (length($_) > $maxlength) { $maxlength = length ($_); }
  173:       } split ("\n", $data);
  174: 	  
  175:   return "<br></br>\n&lt;$tag&gt;<br></br>\n&nbsp;&nbsp;&nbsp;<textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea><br></br>\n&lt;/$tag&gt;<br></br>\n";
  176: }
  177: 
  178: sub getfilenothere {
  179:   my ($filename) = @_;
  180:   my $a="";
  181:   
  182:   $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
  183:   $filename="/home/httpd/html/res".$filename;
  184:   if (! -e $filename ) {
  185:     &Apache::lonnet::subscribe($filename);
  186:     &Apache::lonnet::repcopy($filename);
  187:   }
  188:   if (! -e $filename ) { return -1; };
  189:   my $fh=Apache::File->new($filename);
  190:   while (<$fh>) {
  191:       $a .=$_;
  192:   }
  193:   return $a
  194: }
  195: 
  196: 1;
  197: __END__

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