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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # <script> definiton
1.34      albertel    3: # 2/21 Guy
1.1       albertel    4: 
                      5: package Apache::scripttag; 
                      6: 
                      7: use strict;
1.3       albertel    8: use Apache::lonnet;
1.42    ! sakharuk    9: use Apache::style;
1.1       albertel   10: 
                     11: sub BEGIN {
1.6       albertel   12:   &Apache::lonxml::register('Apache::scripttag',('script','scriptlib',
1.20      sakharuk   13: 						 'parserlib','import',
1.42    ! sakharuk   14:                                                  'window','display',
        !            15:                                                  'storetc','physnet'));
1.1       albertel   16: }
                     17: 
                     18: sub start_script {
1.40      albertel   19:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.24      albertel   20:   my $result='';
1.39      albertel   21:   my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
                     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:     if ( $target eq "modified" ) {
1.41      albertel   26:       $result=$token->[4].&Apache::edit::modifiedfield();
1.28      albertel   27:     } elsif ( $target eq "web" || $target eq "grade" ) {
                     28:       &Apache::run::run($bodytext,$safeeval);
                     29:     } elsif ($target eq "edit" ) {
1.41      albertel   30:       #&Apache::run::run($bodytext,$safeeval);
                     31:       #$result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
                     32:       $result=&Apache::edit::tag_start($target,$token,'Script');
                     33:       $result.=&Apache::edit::editfield($token->[1],$bodytext,'',50,4);
1.17      albertel   34:     }
                     35:   } else {
1.28      albertel   36:     if ($target ne "meta") { $result = $token->[4]; }
1.13      albertel   37:   }
1.24      albertel   38:   return $result;
1.17      albertel   39: }
1.13      albertel   40: 
1.17      albertel   41: sub end_script {
1.40      albertel   42:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.28      albertel   43:   if ( $target eq "meta" ) { return ''; } 
1.39      albertel   44:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
1.29      albertel   45:   my $result='';
                     46:   #other script blocks need to survive
                     47:   if ($type ne "loncapa/perl") { return $token->[2]; }
1.41      albertel   48:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
1.28      albertel   49:   return '';
1.24      albertel   50: }
                     51: 
                     52: sub start_display {
1.40      albertel   53:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.39      albertel   54: 
1.24      albertel   55:   my $bodytext=&Apache::lonxml::get_all_text("/display",$$parser[$#$parser]);
1.39      albertel   56: 
1.24      albertel   57:   if ( $target eq "modified" ) {
                     58:   }
                     59:   my $result=&Apache::run::run($bodytext,$safeeval);
1.26      albertel   60:   if ($target eq 'grade' ) {
                     61:     # grade should produce no output
                     62:     $result='';
                     63:   }
1.24      albertel   64:   if ($target eq "edit" ) {
                     65:     $result = 
1.33      albertel   66:       "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
1.38      albertel   67:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
1.24      albertel   68:   }
                     69:   return $result;
                     70: }
                     71: 
                     72: sub end_display {
1.38      albertel   73:   return ''
1.1       albertel   74: }
1.3       albertel   75: 
                     76: sub start_scriptlib {
1.40      albertel   77:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37      albertel   78:   my $bodytext;
                     79:   my $result ='';
                     80:   my $error='';
                     81: 
                     82:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit') {
                     83:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
                     84:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
                     85: 				     $$parstack[$#$parstack]);
                     86:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
                     87: 					       $bodytext);
                     88:     my $script=&Apache::lonnet::getfile($location);
                     89:     if ($script == -1) {
                     90:       if ($target eq 'edit') {
                     91:         $error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
                     92:       } else {
                     93: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
                     94: 	return "";
                     95:       }
                     96:     }
                     97:     &Apache::run::run($script,$safeeval);
                     98:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
1.18      albertel   99:   }
1.13      albertel  100:   if ($target eq "edit" ) {
1.37      albertel  101:     $result=
1.38      albertel  102:       &Apache::edit::tag_start($target,$token).
                    103: 	&Apache::edit::editfield($token->[1],$bodytext,'New Script Functions',40,1).
1.37      albertel  104: 	  $error;
                    105:   }
                    106:   if ($target eq "modified" ) {
                    107:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
                    108:     $result=&Apache::edit::modifiedfield($token);
                    109:     &Apache::lonxml::debug($result);
1.3       albertel  110:   }
                    111:   return $result;
                    112: }
                    113: 
1.37      albertel  114: sub end_scriptlib {
1.40      albertel  115:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37      albertel  116:   my $result='';
                    117: #  if ($target eq "edit" ) { $result=" "; }
                    118:   return $result;
                    119: }
1.4       albertel  120: 
                    121: sub start_parserlib {
1.40      albertel  122:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37      albertel  123:   my $bodytext;
1.7       albertel  124:   my $result ="";
1.37      albertel  125:   my $error='';
1.13      albertel  126: 
1.37      albertel  127:   if ($target eq 'web' || $target eq 'grade' || $target eq 'meta' || $target eq 'edit') {
                    128:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
                    129:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
                    130: 				     $$parstack[$#$parstack]);
                    131:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
                    132: 					       $bodytext);
                    133:     my $styletext=&Apache::lonnet::getfile($location);
                    134:     #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
                    135:     if ($styletext == -1) {
                    136:       if ($target eq 'edit') {
                    137: 	$error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
                    138:       } else {
                    139: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
                    140: 	return "";
                    141:       }
                    142:     }
                    143:     %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
1.18      albertel  144:   }
1.13      albertel  145:   if ($target eq "edit" ) {
1.37      albertel  146:     $result=
1.38      albertel  147:       &Apache::edit::tag_start($target,$token).
                    148: 	&Apache::edit::editfield($token->[1],$bodytext,"New Tag Definitions",40,1).
1.37      albertel  149: 	  $error;
                    150:   }
                    151:   if ($target eq "modified" ) {
                    152:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
                    153:     $result=&Apache::edit::modifiedfield($token);
                    154:     &Apache::lonxml::debug($result);
1.7       albertel  155:   }
                    156:   return $result;
1.4       albertel  157: }
                    158: 
                    159: sub end_parserlib {
1.40      albertel  160:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37      albertel  161:   my $result='';
                    162: #  if ($target eq "edit" ) { 
1.38      albertel  163: #    $result=&Apache::edit::tag_end($target,$token);
1.37      albertel  164: #  }
                    165:   return $result;
1.6       albertel  166: }
                    167: 
1.30      sakharuk  168: sub start_window {
1.40      albertel  169:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.20      sakharuk  170:   my $result = '';
1.31      albertel  171:   if ($target eq 'web') {
1.34      albertel  172:     &Apache::lonxml::startredirection;
1.35      sakharuk  173:   }  elsif ($target eq 'tex') {
1.42    ! sakharuk  174:        $result = '\unskip\footnote{';
1.35      sakharuk  175:    }
1.19      sakharuk  176:   return $result;  
                    177: }
                    178: 
1.30      sakharuk  179: sub end_window {
1.40      albertel  180:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.30      sakharuk  181:   my $result;
1.31      albertel  182:   if ($target eq 'web') {
1.34      albertel  183:     my $output=&Apache::lonxml::endredirection;
                    184:     $output =~ s/\"/\&quot\;/g;
1.35      sakharuk  185:     $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>";
                    186:   } elsif ($target eq 'tex') {
                    187:       $result = '}';
1.30      sakharuk  188:   } else {
1.35      sakharuk  189:       $result = '';
1.30      sakharuk  190:   }
1.20      sakharuk  191:   return $result; 
1.19      sakharuk  192: }
                    193: 
1.6       albertel  194: sub start_import {
1.40      albertel  195:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.31      albertel  196:   my $bodytext=$$parser[$#$parser]->get_text("/import");
1.7       albertel  197:   my $result ="";
1.13      albertel  198: 
                    199:   $bodytext=Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
1.18      albertel  200:   my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
                    201:   my $file=&Apache::lonnet::getfile($location);
                    202:   if ($file == -1) {
                    203:     &Apache::lonxml::error("<b> Unable to find <i>$bodytext $location</i> for import</b>");
                    204:     return "";
1.14      albertel  205:   }
                    206: 
1.18      albertel  207:   my $dir=$location;
1.30      sakharuk  208:   $dir=~s:/[^/]*$::;  
                    209: #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
1.18      albertel  210:   &Apache::lonxml::newparser($parser,\$file,$dir);
1.13      albertel  211: 
                    212:   if ($target eq "edit" ) {
1.38      albertel  213:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
1.33      albertel  214:     $result.="Click<a href=\"/res/$bodytext\">here</a> to edit<br />"
1.7       albertel  215:   }
1.27      albertel  216:   return '';
1.6       albertel  217: }
                    218: 
                    219: sub end_import {
1.7       albertel  220: }
1.30      sakharuk  221: 
                    222: sub start_meta {
1.40      albertel  223:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.30      sakharuk  224:   my $result = '';
                    225:     if ($target ne 'meta') {
                    226:        $result = &Apache::lonxml::get_all_text("/meta",$$parser[$#$parser]); 
                    227:        $result = '';
                    228:       return $result; 
                    229:     } else {
                    230:        return $result; 
                    231:     }
                    232: }
                    233: 
                    234: sub end_meta {
1.1       albertel  235: }
1.42    ! sakharuk  236: 
        !           237: sub editfield {
        !           238:   my ($tag,$data)=@_;
        !           239:   
        !           240:   my $count=0;
        !           241:   my $maxlength=-1;
        !           242:   map { $count++;
        !           243: 	if (length($_) > $maxlength) { $maxlength = length ($_); }
        !           244:       } split ("\n", $data);
        !           245: 	  
        !           246:   return "<br />\n&lt;$tag&gt;<br />\n&nbsp;&nbsp;&nbsp;<textarea rows=\"$count\" cols=\"$maxlength\" name=homework_edit_".$Apache::lonxml::curdepth.">$data</textarea><br />\n&lt;/$tag&gt;<br />\n";
        !           247: }
        !           248: 
        !           249: sub getfilenothere {
        !           250:   my ($filename) = @_;
        !           251:   my $a="";
        !           252:   
        !           253:   $filename=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
        !           254:   $filename="/home/httpd/html/res".$filename;
        !           255:   if (! -e $filename ) {
        !           256:     &Apache::lonnet::subscribe($filename);
        !           257:     &Apache::lonnet::repcopy($filename);
        !           258:   }
        !           259:   if (! -e $filename ) { return -1; };
        !           260:   my $fh=Apache::File->new($filename);
        !           261:   while (<$fh>) {
        !           262:       $a .=$_;
        !           263:   }
        !           264:   return $a
        !           265: }
        !           266: 
        !           267: 
        !           268: sub start_storetc {
        !           269:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
        !           270:   my $result = '';
        !           271:   &Apache::lonxml::startredirection;
        !           272:   return $result; 
        !           273: }
        !           274: 
        !           275: sub end_storetc {
        !           276:     my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
        !           277:     my $result;
        !           278:     my $output=&Apache::lonxml::endredirection;
        !           279:     $output =~ s/\"/\&quot\;/g;
        !           280:     $result = "{\bf $output.}}\write\tcfile{\protect\tcpc{ $output.}{\the\value{relpage}}}";
        !           281:     return $result; 
        !           282: }
        !           283: 
        !           284: 
        !           285: sub start_physnet {
        !           286: 
        !           287:     my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
        !           288:     my $cbistylefile = &Apache::lonnet::getfile('/home/httpd/res/msu/physnet/cbi.sty');
        !           289:     %$style = (%$style,&Apache::style::styleparser($target,$cbistylefile));
        !           290:     &Apache::lonxml::newparser($parser$$style{'physnet'});
        !           291: 
        !           292:     return "";
        !           293: }
        !           294: 
1.3       albertel  295: 
1.1       albertel  296: 1;
                    297: __END__

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