Annotation of loncom/xml/scripttag.pm, revision 1.32

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

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