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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # <script> definiton
1.58      www         3: #
1.169   ! raeburn     4: # $Id: scripttag.pm,v 1.168 2013/09/30 13:44:16 raeburn Exp $
1.58      www         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: #
1.116     www        28: 
1.47      albertel   29: package Apache::scripttag;
1.1       albertel   30: 
                     31: use strict;
1.3       albertel   32: use Apache::lonnet;
1.145     albertel   33: use Apache::lonlocal;
1.167     raeburn    34: use Apache::lonxml();
                     35: use Apache::londefdef();
1.145     albertel   36: use Apache::style();
1.1       albertel   37: 
1.47      albertel   38: #Globals
                     39: # this used to pass around the standard callsub arguments to a tag func
                     40: # so xmlparse can reenter the inner_xmlparse loop.
                     41: 
                     42: @Apache::scripttag::parser_env = ();
1.61      harris41   43: BEGIN {
1.62      albertel   44:   &Apache::lonxml::register('Apache::scripttag',
                     45: 			    ('script','scriptlib','parserlib','import',
1.163     www        46: 			     'window','windowlink','togglebox','display','storetc','physnet',
1.123     albertel   47: 			     'standalone','comment','num','parse','algebra',
1.86      albertel   48: 			     'LONCAPA_INTERNAL_TURN_STYLE_ON',
1.150     raeburn    49: 			     'LONCAPA_INTERNAL_TURN_STYLE_OFF'));
1.81      albertel   50: }
                     51: 
                     52: sub start_LONCAPA_INTERNAL_TURN_STYLE_ON {
                     53:     $Apache::lonxml::usestyle=1;
1.89      albertel   54:     $Apache::lonxml::style_values='';
1.81      albertel   55:     return ('','no');
                     56: }
                     57: 
                     58: sub end_LONCAPA_INTERNAL_TURN_STYLE_ON {
1.91      albertel   59:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                     60:     my $end=&Apache::lonxml::get_param('end',$parstack,$safeeval);
                     61:     if (defined($end)) {
                     62: 	&Apache::lonxml::end_tag($tagstack,$parstack,$token);
                     63:     }
1.100     albertel   64:     return ('','no');
                     65: }
                     66: 
                     67: sub start_LONCAPA_INTERNAL_TURN_STYLE_OFF {
                     68:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                     69:     $Apache::lonxml::usestyle=0;
                     70:     my $end=&Apache::lonxml::get_param('end',$parstack,$safeeval);
                     71:     if (!$end) {
                     72: 	$Apache::lonxml::style_values=$$parstack[-1];
                     73: 	$Apache::lonxml::style_end_values=$$parstack[-1];
                     74:     } else {
                     75: 	$Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
                     76: 	$Apache::lonxml::style_end_values='';
                     77:     }
                     78:     return ('','no');
                     79: }
                     80: 
                     81: sub end_LONCAPA_INTERNAL_TURN_STYLE_OFF {
1.86      albertel   82:     return ('','no');
                     83: }
                     84: 
1.1       albertel   85: sub start_script {
1.40      albertel   86:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.47      albertel   87:   @Apache::scripttag::parser_env = @_;
1.24      albertel   88:   my $result='';
1.39      albertel   89:   my $type= &Apache::lonxml::get_param('type',$parstack,$safeeval);
                     90:   &Apache::lonxml::debug("found type of $type");
1.17      albertel   91:   if ($type eq "loncapa/perl") {
                     92:     if ( $target eq "modified" ) {
1.103     albertel   93: 	$result=$token->[4].&Apache::edit::modifiedfield('/script',$parser);
1.54      albertel   94:     } elsif ( $target eq 'web' || $target eq 'tex' ||
1.130     albertel   95: 	      $target eq 'grade' || $target eq 'webgrade' ||
                     96: 	      $target eq 'answer' || $target eq 'analyze' ) {
1.107     albertel   97: 	my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
1.79      albertel   98: 	if (!$Apache::lonxml::default_homework_loaded) {
                     99: 	    &Apache::lonxml::default_homework_load($safeeval);
                    100: 	}
                    101: 	&Apache::run::run($bodytext,$safeeval);
1.88      albertel  102: 	if (($target eq 'answer') &&
1.126     albertel  103: 	    ($env{'form.answer_output_mode'} ne 'tex') &&
1.88      albertel  104: 	    ($Apache::lonhomework::viewgrades == 'F')) {
1.79      albertel  105: 	    $Apache::lonxml::evaluate--;
1.138     albertel  106: 	    my (undef,undef,$udom,$uname)=&Apache::lonnet::whichuser();
1.140     albertel  107: 	    $uname =~s/\W//g;
                    108: 	    $udom  =~s/\W//g;
1.145     albertel  109: 	    my $function_name = 
                    110: 		join('_','LONCAPA_scriptvars',$uname,$udom,
                    111: 		     $env{'form.counter'},$Apache::lonxml::curdepth);
1.158     www       112:             &Apache::lonxml::add_script_result(
1.159     www       113: 	             &Apache::loncommon::modal_adhoc_window($function_name,500,500,
1.166     raeburn   114:                             '<pre style="background-color:#ffffff;">'.
                    115:                             &Apache::run::dump($target,$safeeval).'</pre>',
1.159     www       116:                             &mt('Script Vars'))."<br />");
1.79      albertel  117: 	}
1.28      albertel  118:     } elsif ($target eq "edit" ) {
1.41      albertel  119:       #&Apache::run::run($bodytext,$safeeval);
                    120:       #$result="<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
1.107     albertel  121: 	my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
1.103     albertel  122: 	$result=&Apache::edit::tag_start($target,$token,'Script');
                    123: 	$result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4);
1.107     albertel  124:     } elsif ($target eq 'meta') {
                    125: 	my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
1.17      albertel  126:     }
                    127:   } else {
1.108     matthew   128:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/script",$parser);
1.144     albertel  129:       if ($target ne "meta" && $target ne 'tex' && $target ne 'answer') {
1.112     albertel  130: 	  $result = $token->[4];
                    131: 	  $result.=$bodytext;
1.168     raeburn   132:           my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
                    133:           my $url=&Apache::lonnet::hreflocation('',$env{'request.filename'});
                    134:           my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    135:           my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                    136:           if ($src ne '') {
                    137:               if ($src !~ m{^(/|https?://)}) {
                    138:                   my $cleanhref = &Apache::londefdef::clean_docs_httpref($src,$url,$cdom,$cnum);
                    139:                   if ($cleanhref) {
                    140:                       &Apache::lonxml::extlink($cleanhref);
1.167     raeburn   141:                   }
1.168     raeburn   142:               }
                    143:           } elsif (($type eq 'text/javascript') && ($bodytext ne '')) {
                    144:               if ($url =~ m{^\Q/uploaded/$cdom/$cnum/docs/\E}) {
                    145:                   if ($bodytext =~ m{\.set\w+(Src|Swf)\(["']}i) {
                    146:                       my @srcs = split(/\.set/,$bodytext);
                    147:                       if (scalar(@srcs) > 1) {
                    148:                           foreach my $item (@srcs) {
                    149:                               if ($item =~ m{^(FlashPlayerSwf|MediaSrc|XMPSrc|ConfigurationSrc|PosterImageSrc)\((['"])(?:(?!\2).)+\2\)}is) {
                    150:                                   my $srctype = $1;
                    151:                                   my $quote = $2;
                    152:                                   my ($fname) = ($item =~ m{^\Q$srctype($quote\E([^$quote]+)\Q$quote)\E}); 
1.169   ! raeburn   153:                                   my $cleanhref =
        !           154:                                       &Apache::londefdef::clean_docs_httpref($fname,$url,$cdom,$cnum);
1.168     raeburn   155:                                   if ($cleanhref) {
                    156:                                       &Apache::lonxml::extlink($cleanhref);
1.169   ! raeburn   157:                                       if ($srctype eq 'ConfigurationSrc') {
        !           158:                                           if ($cleanhref =~ m{^(.+/)configuration_express\.xml$}) {
        !           159: #
        !           160: # Camtasia 8.1: express_show/spritesheet.png needed, and included in zip archive.
        !           161: # Not referenced directly in <main>.html or <main>_player.html files,
        !           162: # so call lonxml::extlink() here to include httpref for the uploaded file.
        !           163: # (where <main> is name user gave to file/archive).
        !           164: #
        !           165: 
        !           166:                                               my $spritesheet = $1.'express_show/spritesheet.png';
        !           167:                                               if (&Apache::lonnet::repcopy_userfile($spritesheet) eq 'ok') {
        !           168:                                                   &Apache::lonxml::extlink($spritesheet);
        !           169:                                               }
        !           170:                                           }
        !           171:                                       } elsif ($srctype eq 'PosterImageSrc') {
        !           172:                                           if ($fname =~ m{^(.+)_First_Frame\.png$}) {
        !           173:                                               my $prefix = $1;
        !           174:                                               my ($path) = ($cleanhref =~ m{^(.+/)\Q$fname\E});
        !           175: #
        !           176: # Camtasia 8.1: <main>_Thumbnails.png needed, and included in zip archive.
        !           177: # Not referenced directly in <main>.html or <main>_player.html files,
        !           178: # so call lonxml::extlink() here to include httpref for the uploaded file
        !           179: # (where <main> is name user gave to file/archive).
        !           180: #
        !           181:                                               my $thumbnail = $path.$prefix.'_Thumbnails.png';
        !           182:                                               if (&Apache::lonnet::repcopy_userfile($thumbnail) eq 'ok') {
        !           183:                                                   &Apache::lonxml::extlink($thumbnail);
        !           184:                                               }
        !           185:                                           }
        !           186:                                       }
1.167     raeburn   187:                                   }
                    188:                               }
1.165     raeburn   189:                           }
                    190:                       }
                    191:                   }
                    192:               }
                    193:           }
1.112     albertel  194:       }
1.13      albertel  195:   }
1.24      albertel  196:   return $result;
1.17      albertel  197: }
1.13      albertel  198: 
1.17      albertel  199: sub end_script {
1.40      albertel  200:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.28      albertel  201:   if ( $target eq "meta" ) { return ''; } 
1.39      albertel  202:   my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
1.29      albertel  203:   my $result='';
                    204:   #other script blocks need to survive
1.115     albertel  205:   if ($type ne "loncapa/perl" && $target ne 'tex') {
1.53      albertel  206:     return $token->[2];
                    207:   } elsif ($target eq 'edit' ) {
                    208:     return &Apache::edit::end_table();
1.78      albertel  209:   } elsif ($target eq 'answer') {
1.53      albertel  210:     $Apache::lonxml::evaluate++;
                    211:   }
1.28      albertel  212:   return '';
1.24      albertel  213: }
                    214: 
                    215: sub start_display {
1.40      albertel  216:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.119     albertel  217:   @Apache::scripttag::parser_env = @_;
1.48      albertel  218:   my $result;
1.39      albertel  219: 
1.24      albertel  220:   if ( $target eq "modified" ) {
1.103     albertel  221:       $result=$token->[4].&Apache::edit::modifiedfield("/display",$parser);
1.54      albertel  222:   } elsif ( $target eq 'web' || $target eq 'tex' ||
1.130     albertel  223: 	    $target eq 'grade' || $target eq 'webgrade' ||
                    224: 	    $target eq 'answer' || $target eq 'analyze') {
1.107     albertel  225:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
1.80      albertel  226:       if (!$Apache::lonxml::default_homework_loaded) {
                    227: 	  &Apache::lonxml::default_homework_load($safeeval);
                    228:       }
                    229:       $result=&Apache::run::run($bodytext,$safeeval);
                    230:       if ($target eq 'grade' || $target eq 'answer' ||
                    231: 	  $target eq 'analyze') {
1.114     albertel  232: 	  # grade/answer/analyxe should produce no output but if we
                    233: 	  # are redirecting, the redirecter should know what to do
                    234: 	  # with the output
                    235: 	  if (!$Apache::lonxml::redirection) { $result=''; }
1.80      albertel  236:       }
1.101     albertel  237:       $Apache::lonxml::post_evaluate=0;
1.48      albertel  238:   } elsif ($target eq "edit" ) {
1.110     albertel  239:     my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
1.48      albertel  240:     #$result = 
                    241:     #  "<br /> &lt;$token->[1]&gt; output: <br />$bodytext<br />Source:<br />";
                    242:     #$result.=&Apache::edit::editfield($token->[1],$bodytext,'',40,1);
                    243:     $result=&Apache::edit::tag_start($target,$token,'Script With Display');
1.77      albertel  244:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,1)
1.107     albertel  245:   } elsif ($target eq 'meta') {
                    246:       my $bodytext=&Apache::lonxml::get_all_text_unbalanced("/display",$parser);
1.24      albertel  247:   }
                    248:   return $result;
                    249: }
                    250: 
                    251: sub end_display {
1.48      albertel  252:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    253:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
                    254:   return '';
1.1       albertel  255: }
1.3       albertel  256: 
                    257: sub start_scriptlib {
1.40      albertel  258:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37      albertel  259:   my $bodytext;
                    260:   my $result ='';
                    261:   my $error='';
                    262: 
1.106     albertel  263:   if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
                    264:       $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
1.137     albertel  265:       $target eq 'analyze' || $target eq 'webgrade') {
1.37      albertel  266:     $bodytext=$$parser[$#$parser]->get_text("/scriptlib");
                    267:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
                    268: 				     $$parstack[$#$parstack]);
                    269:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
                    270: 					       $bodytext);
                    271:     my $script=&Apache::lonnet::getfile($location);
                    272:     if ($script == -1) {
                    273:       if ($target eq 'edit') {
1.145     albertel  274:         $error='</tr><tr><td>'.&mt('Errors').'</td><td colspan="2"><b>'.&mt(' Unable to find [_1]','<span class="LC_filename">'.$location.'</span>').'</b></td>'."\n";
1.37      albertel  275:       } else {
                    276: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for scriptlib</b>");
                    277: 	return "";
                    278:       }
                    279:     }
                    280:     &Apache::run::run($script,$safeeval);
                    281:     #&Apache::lonxml::debug("ran $bodytext:<br />".&Apache::lonnet::getfile($bodytext)."<br />");
1.18      albertel  282:   }
1.13      albertel  283:   if ($target eq "edit" ) {
1.37      albertel  284:     $result=
1.49      albertel  285:       &Apache::edit::tag_start($target,$token,'New Script Functions').
1.83      matthew   286: 	&Apache::edit::editline($token->[1],$bodytext,'scriptlib',40).
                    287:             &Apache::edit::browse(undef,'textnode').
1.49      albertel  288: 	  $error.'</td></tr>'.
                    289: 	    &Apache::edit::end_table();
1.37      albertel  290:   }
                    291:   if ($target eq "modified" ) {
1.103     albertel  292:       $result=$token->[4].&Apache::edit::modifiedfield("/scriptlib",$parser);
1.3       albertel  293:   }
                    294:   return $result;
                    295: }
                    296: 
1.37      albertel  297: sub end_scriptlib {
1.40      albertel  298:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49      albertel  299:   my @result;
                    300:   if ($target eq "edit" ) { $result[1]='no'; }
                    301:   return @result;
1.37      albertel  302: }
1.4       albertel  303: 
                    304: sub start_parserlib {
1.40      albertel  305:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.37      albertel  306:   my $bodytext;
1.7       albertel  307:   my $result ="";
1.37      albertel  308:   my $error='';
1.106     albertel  309:   if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
                    310:       $target eq 'meta' || $target eq 'edit' || $target eq 'answer' ||
1.137     albertel  311:       $target eq 'analyze' || $target eq 'webgrade') {
1.37      albertel  312:     $bodytext=$$parser[$#$parser]->get_text("/parserlib");
                    313:     $bodytext=&Apache::run::evaluate($bodytext,$safeeval,
                    314: 				     $$parstack[$#$parstack]);
                    315:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],
                    316: 					       $bodytext);
                    317:     my $styletext=&Apache::lonnet::getfile($location);
                    318:     #&Apache::lonxml::debug("found :$bodytext: in :$location: with :$styletext:");
                    319:     if ($styletext == -1) {
                    320:       if ($target eq 'edit') {
                    321: 	$error='</tr><tr><td>Errors</td><td colspan="2"><b> Unable to find <i>'.$location.'</i></b></td>'."\n";
                    322:       } else {
                    323: 	&Apache::lonxml::error("<b> Unable to find <i>$location</i> for parserlib</b>");
                    324: 	return "";
                    325:       }
                    326:     }
                    327:     %$style = ( %$style , &Apache::style::styleparser($target,$styletext));
1.18      albertel  328:   }
1.13      albertel  329:   if ($target eq "edit" ) {
1.37      albertel  330:     $result=
1.49      albertel  331:       &Apache::edit::tag_start($target,$token,'New Tag Definitions').
1.68      albertel  332: 	&Apache::edit::editline($token->[1],$bodytext,'',40).
1.49      albertel  333: 	  $error.'</td></tr>'.
                    334: 	    &Apache::edit::end_table();
1.37      albertel  335:   }
                    336:   if ($target eq "modified" ) {
1.103     albertel  337:       $result=$token->[4].&Apache::edit::modifiedfield("/parserlib",$parser);
1.7       albertel  338:   }
                    339:   return $result;
1.4       albertel  340: }
                    341: 
                    342: sub end_parserlib {
1.40      albertel  343:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.49      albertel  344:   my @result;
                    345:   if ($target eq "edit" ) { $result[1]='no'; }
                    346:   return @result;
1.6       albertel  347: }
                    348: 
1.30      sakharuk  349: sub start_window {
1.105     albertel  350:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    351:     my $result = '';
1.137     albertel  352:     if ($target eq 'web' || $target eq 'webgrade') {
1.105     albertel  353: 	&Apache::lonxml::startredirection;
                    354:     } elsif ($target eq 'tex') {
1.164     www       355:         my $printtext=&Apache::lonxml::get_param('printtext',$parstack,$safeeval);
                    356:         if ($printtext=~/\w/) {
                    357: # If printtext is given, do not output any intervening information
                    358:            &Apache::lonxml::startredirection;
                    359:         } else {
                    360:            $result = '\unskip\footnote{';
                    361:         }
1.105     albertel  362:     } elsif ($target eq 'edit') {
                    363: 	$result.=&Apache::edit::tag_start($target,$token);
                    364: 	$result.=&Apache::edit::text_arg('Text of Link:','linktext',$token,70);
1.131     albertel  365: 	$result.=&Apache::edit::text_arg('Height:','height',$token,5);
                    366: 	$result.=&Apache::edit::text_arg('Width:','width',$token,5);
1.164     www       367:         $result.=&Apache::edit::text_arg('Printed text (optional):','printtext',$token,20);
1.105     albertel  368: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    369:     } elsif ($target eq 'modified') {
                    370: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.129     raeburn   371: 						     $safeeval,'linktext',
1.161     www       372: 						     'width','height');
1.105     albertel  373: 	if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
                    374:     }
                    375:     return $result;  
1.19      sakharuk  376: }
                    377: 
1.30      sakharuk  378: sub end_window {
1.40      albertel  379:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.30      sakharuk  380:   my $result;
1.137     albertel  381:   if ($target eq 'web' || $target eq 'webgrade') {
1.34      albertel  382:     my $output=&Apache::lonxml::endredirection;
1.92      albertel  383:     my $linktext= &Apache::lonxml::get_param('linktext',$parstack,$safeeval);
                    384:     if (!$linktext) { $linktext='<sup>*</sup>'; }
                    385:     my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
                    386:     if (!$width) { $width='500'; }
                    387:     my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
                    388:     if (!$height) { $height='200'; }
1.160     www       389:     $result=&Apache::loncommon::modal_adhoc_window
                    390:            ("LONCAPA_newwindow_$Apache::lonxml::curdepth",$width,$height,$output,$linktext);
1.35      sakharuk  391:   } elsif ($target eq 'tex') {
1.164     www       392:       my $printtext=&Apache::lonxml::get_param('printtext',$parstack,$safeeval);
                    393:       if ($printtext=~/\w/) {
                    394: # If a "printtext" is given, proceed to retrieve all intervening information and trash it
                    395:          my $output=&Apache::lonxml::endredirection;
                    396: # Use printtext instead
                    397:          $result=$printtext;
                    398:       } else {
                    399:          $result='}';
                    400:       }
1.30      sakharuk  401:   } else {
1.35      sakharuk  402:       $result = '';
1.30      sakharuk  403:   }
1.20      sakharuk  404:   return $result; 
1.19      sakharuk  405: }
                    406: 
1.161     www       407: 
1.163     www       408: sub start_windowlink {
                    409:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    410:     my $result = '';
                    411:     if ($target eq 'web' || $target eq 'webgrade') {
                    412:         &Apache::lonxml::startredirection;
                    413:     } elsif ($target eq 'edit') {
                    414:         $result.=&Apache::edit::tag_start($target,$token);
                    415:         $result.=&Apache::edit::text_arg('Link:','href',$token,70);
                    416:         $result.=&Apache::edit::text_arg('Height:','height',$token,5);
                    417:         $result.=&Apache::edit::text_arg('Width:','width',$token,5);
                    418:         $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    419:     } elsif ($target eq 'modified') {
                    420:         my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    421:                                                      $safeeval,'href',
                    422:                                                      'width','height');
                    423:         if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
                    424:     }
                    425:     return $result;
                    426: }
                    427: 
                    428: sub end_windowlink {
                    429:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    430:   my $result;
                    431:   if ($target eq 'web' || $target eq 'webgrade') {
                    432:     my $output=&Apache::lonxml::endredirection;
                    433:     my $href= &Apache::lonxml::get_param('href',$parstack,$safeeval);
                    434:     if (!$href) { $href='/adm/rat/empty.html'; }
                    435:     my $width= &Apache::lonxml::get_param('width',$parstack,$safeeval);
                    436:     if (!$width) { $width='500'; }
                    437:     my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
                    438:     if (!$height) { $height='200'; }
                    439:     $result=&Apache::loncommon::modal_link($href,$output,$width,$height);
                    440:   } else {
                    441:       $result = '';
                    442:   }
                    443:   return $result;
                    444: }
                    445: 
                    446: 
1.161     www       447: sub start_togglebox {
                    448:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    449:     my $result = '';
                    450:     if ($target eq 'web' || $target eq 'webgrade') {
                    451:         my $id="LONCAPA_togglebox_$Apache::lonxml::curdepth";
                    452:         my $heading=&Apache::lonxml::get_param('heading',$parstack,$safeeval);
                    453:         unless ($heading) { $heading=''; } else { $heading.=' '; }
                    454:         my $showtext=&Apache::lonxml::get_param('showtext',$parstack,$safeeval);
                    455:         my $hidetext=&Apache::lonxml::get_param('hidetext',$parstack,$safeeval);
                    456:         my $headerbg=&Apache::lonxml::get_param('headerbg',$parstack,$safeeval);
1.162     www       457:         $result=&Apache::loncommon::start_togglebox($id,$heading,$headerbg,$hidetext,$showtext);
1.161     www       458:     } elsif ($target eq 'tex') {
                    459:         my $heading=&Apache::lonxml::get_param('heading',$parstack,$safeeval);
                    460:         unless ($heading) { $heading=''; } else { $heading.=' '; }
                    461:         $result = "\n\n".'\fbox{{\bf '.$heading.'} \qquad '."\n";
                    462:     } elsif ($target eq 'edit') {
                    463:         $result.=&Apache::edit::tag_start($target,$token);
                    464:         $result.=&Apache::edit::text_arg('Heading:','heading',$token,70);
                    465:         $result.=&Apache::edit::text_arg('Header Background:','headerbg',$token,7);
                    466:         $result.=&Apache::edit::text_arg('Show text:','showtext',$token,10);
                    467:         $result.=&Apache::edit::text_arg('Hide text:','hidetext',$token,10);
                    468:         $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    469:     } elsif ($target eq 'modified') {
                    470:         my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    471:                                                      $safeeval,'heading',
                    472:                                                      'showtext','hidetext',
                    473:                                                      'headerbg','textbg');
                    474:         if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
                    475:     }
                    476:     return $result;
                    477: }
                    478: 
                    479: sub end_togglebox {
                    480:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    481:   my $result;
                    482:   if ($target eq 'web' || $target eq 'webgrade') {
1.162     www       483:     $result=&Apache::loncommon::end_togglebox();
1.161     www       484:   } elsif ($target eq 'tex') {
                    485:       $result = "}\n\n";
                    486:   } else {
                    487:       $result = '';
                    488:   }
                    489:   return $result;
                    490: }
                    491: 
                    492: 
                    493: 
1.6       albertel  494: sub start_import {
1.40      albertel  495:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.31      albertel  496:   my $bodytext=$$parser[$#$parser]->get_text("/import");
1.7       albertel  497:   my $result ="";
1.13      albertel  498: 
1.153     www       499:   $bodytext=&Apache::run::evaluate($bodytext,$safeeval,$$parstack[$#$parstack]);
1.14      albertel  500: 
1.135     albertel  501:   if ($target eq 'web' ||  $target eq 'webgrade' || $target eq 'grade' 
                    502:       || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze' ) {
1.46      albertel  503:     # FIXME this probably needs to be smart about construction vs.
                    504:     # non construction space.
                    505:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
                    506:     my $file=&Apache::lonnet::getfile($location);
                    507:     if ($file == -1) {
                    508:       &Apache::lonxml::error("<b> Unable to find <i>$bodytext as $location</i> for import</b>");
                    509:       return "";
                    510:     }
1.153     www       511:     my $importmode=&Apache::lonxml::get_param('importmode',$parstack,$safeeval);
1.154     www       512:     if (($importmode eq 'problem') || ($importmode eq 'part')) {
                    513: # We are using import to import published problems
                    514:        if (($importmode eq 'problem') || ($file=~/<part[^<]*>/s)) {
                    515: # We explicitly don't want this to be a separate part or the problem already has parts
                    516:           $file=~s/^\s*<problem>/<library>/s;
                    517: 	  $file=~s/<\/problem>\s*$/<\/library>/s;
                    518:        } else {
                    519: # We want this to be a separate part, but it currently is not
1.155     www       520:           $file=~s/^\s*<problem>/<library><part>/s;
1.154     www       521: 	  $file=~s/<\/problem>\s*$/<\/part><\/library>/s;
                    522:        }
1.153     www       523:     }
1.46      albertel  524:     my $dir=$location;
                    525:     $dir=~s:/[^/]*$::;
                    526:     #  &Apache::lonxml::debug("directory $dir $location file $file \n<b>END</b>\n");
1.142     albertel  527:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
1.57      albertel  528:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                    529:     push(@Apache::inputtags::import,$id);
1.90      albertel  530:     push(@Apache::inputtags::importlist,$id);
1.87      sakharuk  531: 
1.144     albertel  532:     &Apache::lonxml::newparser($parser,\$file,$dir);
1.87      sakharuk  533: 
1.56      albertel  534:   } elsif ($target eq "edit" ) {
1.46      albertel  535:     $result.=&Apache::edit::tag_start($target,$token);
1.153     www       536:     my $location=$token->[1];
                    537:     $location=~s/^\s*//s;
                    538:     $location=~s/\s*$//s;
                    539:     $result.=&Apache::edit::editline($location,$bodytext,'',40);
1.83      matthew   540:     $result.=&Apache::edit::browse(undef,'textnode');
1.154     www       541:     $result.= '&nbsp;<label>'.&mt('Import as:').
1.153     www       542:               '<select name="importmode_'.$Apache::lonxml::curdepth.'">';
1.154     www       543:     my %options=&Apache::lonlocal::texthash(''        => 'as standard library',
                    544:                                             'problem' => 'as problem',
                    545:                                             'part'    => 'as problem part(s)');
                    546:     foreach my $option (sort(keys(%options))) {
1.153     www       547:        $result.='<option value="'.$option.'"';
                    548:        if ($option eq &Apache::lonxml::get_param('importmode',$parstack,$safeeval)) {
                    549:           $result.=' selected="selected"';
                    550:        }
1.154     www       551:        $result.='>'.$options{$option}.'</option>';
1.153     www       552:     }
                    553:     $result.='</select></label>';
1.46      albertel  554:     #FIXME this need to convert $bodytext to be a contruction space reference
                    555:     #my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
                    556:     #$result.="Click<a href=\"$location\">here</a> to edit<br />"
1.56      albertel  557:   } elsif ($target eq 'modified') {
1.153     www       558:       &Apache::edit::get_new_args($token,$parstack,$safeeval,'importmode');
                    559:       $result='<import id="'.$token->[2]{'id'}.'" importmode="'.$token->[2]{'importmode'}.'">';
                    560:       $result.=&Apache::edit::modifiedfield("/import",$parser);
1.56      albertel  561:   } elsif ($target eq 'meta') {
1.142     albertel  562:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
1.57      albertel  563:     $result.='<import part="'.$Apache::inputtags::part;
                    564:     if ($id) {
                    565:       $result.='" id="'.$id;
                    566:     }
1.156     www       567:     $result.='" importmode="'.$token->[2]{'importmode'}.'">';
1.56      albertel  568:     $result.=$bodytext;
                    569:     $result.='</import>';
1.46      albertel  570:   }
                    571:   return $result;
1.6       albertel  572: }
                    573: 
                    574: sub end_import {
1.69      albertel  575:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.57      albertel  576:   pop(@Apache::inputtags::import);
1.69      albertel  577:   my $result;
1.120     albertel  578:   if ($target eq 'edit' ) { $result=&Apache::edit::end_row.
                    579: 				&Apache::edit::end_table(); }
1.69      albertel  580:   return $result;
1.1       albertel  581: }
1.42      sakharuk  582: 
                    583: sub start_storetc {
1.43      albertel  584:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42      sakharuk  585:   my $result = '';
                    586:   &Apache::lonxml::startredirection;
                    587:   return $result; 
                    588: }
                    589: 
                    590: sub end_storetc {
1.43      albertel  591:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.42      sakharuk  592:     my $result;
                    593:     my $output=&Apache::lonxml::endredirection;
                    594:     $output =~ s/\"/\&quot\;/g;
1.52      albertel  595:     $result = '{\bf '.$output.'.}}\write\tcfile{\protect\tcpc{ '.$output.'.}{\the\value{relpage}}}';
                    596:     return $result;
1.42      sakharuk  597: }
                    598: 
                    599: 
                    600: sub start_physnet {
1.45      sakharuk  601:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.50      www       602:     my $bodytext = '/adm/includes/physnet.sty';
1.45      sakharuk  603:     my $location=&Apache::lonnet::filelocation($Apache::lonxml::pwd['-1'],$bodytext);
                    604:     my $cbistyletext=&Apache::lonnet::getfile($location);
1.42      sakharuk  605: 
1.45      sakharuk  606:     %$style = (%$style,&Apache::style::styleparser($target,$cbistyletext));
1.59      albertel  607:     $$parser['-1']->unget_token($token);
                    608: #    if ( defined($$style{'physnet'}) ) {
                    609: #        &Apache::lonxml::newparser($parser,\$$style{'physnet'});
                    610: #    }
1.45      sakharuk  611:     return "";
                    612: }
1.42      sakharuk  613: 
1.45      sakharuk  614: sub end_physnet {
1.47      albertel  615:   return '';
1.42      sakharuk  616: }
1.62      albertel  617: 
                    618: sub start_standalone {
                    619:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.75      albertel  620:   my $result='';
1.137     albertel  621:   if ($target eq 'web' || $target eq 'webgrade') {
1.126     albertel  622:     if ( $env{'request.course.id'} ) {
1.133     albertel  623:       my $inside = &Apache::lonxml::get_all_text("/standalone",$parser,$style);
1.75      albertel  624:     } else {
                    625:       $result='<table bgcolor="#E1E1E1" border="2"><tr><td>';
                    626:     }
1.62      albertel  627:   }
1.75      albertel  628:   return $result;
1.62      albertel  629: }
                    630: 
                    631: sub end_standalone {
1.75      albertel  632:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    633:   my $result='';
1.137     albertel  634:   if ($target eq 'web' || $target eq 'webgrade' ) {
1.126     albertel  635:     if ( $env{'request.course.id'} ) {
1.75      albertel  636:     } else {
                    637:       $result='</td></tr></table>';
                    638:     }
                    639:   }
                    640:   return $result;
1.74      albertel  641: }
                    642: 
                    643: sub start_comment {
                    644:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    645:   my $result='';
                    646:   if ($target eq 'edit') {
                    647:     $result=&Apache::edit::tag_start($target,$token);
1.133     albertel  648:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
1.77      albertel  649:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'',80,4)
1.74      albertel  650:   } elsif ( $target eq 'modified') {
1.103     albertel  651:     $result=$token->[4].&Apache::edit::modifiedfield("/comment",$parser);
1.137     albertel  652:   } elsif ( $target eq 'web'    || $target eq 'tex'  || $target eq 'grade'   ||
                    653: 	    $target eq 'answer' || $target eq 'meta' || $target eq 'analyze' ||
                    654: 	    $target eq 'webgrade') {
1.74      albertel  655:     #normally throw away comments
1.133     albertel  656:     my $bodytext=&Apache::lonxml::get_all_text("/comment",$parser,$style);
1.74      albertel  657:   }
                    658:   return $result;
                    659: }
                    660: 
                    661: sub end_comment {
                    662:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    663:   if ($target eq 'edit' ) { return &Apache::edit::end_table(); }
1.62      albertel  664:   return '';
                    665: }
                    666: 
1.42      sakharuk  667: 
1.47      albertel  668: sub xmlparse {
                    669:   my ($string) = @_;
1.111     albertel  670:   &Apache::lonxml::debug("xmlparse recursion starting with $string");
1.113     albertel  671:   # Apache::run::evaluate does an 'eval' on the name of the subroutine
                    672:   # if it detects something that looks like a subroutine, this ends up calling
                    673:   # things without any arguments and since perl is nice enough to pass
                    674:   # along the default arguments when you don't explicitly say no arguments
                    675:   # if you call &xmlparse, it gets &xmlparse passed as it argument.
                    676:   # Same thing soccurs with &chemparse.
1.111     albertel  677:   if ($string eq '&xmlparse') { return '&xmlparse'; }
1.113     albertel  678:   if ($string eq '&chemparse') { return '&chemparse'; }
1.47      albertel  679:   my ($target,$token,$tagstack,$parstack,$oldparser,$safeeval,$style)=
                    680:     @Apache::scripttag::parser_env;
                    681:   my @parser;
                    682:   &Apache::lonxml::newparser(\@parser,\$string);
1.132     albertel  683:   &Apache::lonxml::startredirection();
1.47      albertel  684:   my $result=&Apache::lonxml::inner_xmlparse($target,$tagstack,
                    685: 					     $parstack,\@parser,
                    686: 					     $safeeval,$style);
1.132     albertel  687:   $result.=&Apache::lonxml::endredirection();
1.122     albertel  688:   &Apache::lonxml::debug("target is $target xmlparse recursion ending with $result");
1.47      albertel  689:   return $result;
                    690: }
1.3       albertel  691: 
1.122     albertel  692: sub start_num {
                    693:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    694:     my $result = '';
                    695:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/num",$parser);
1.137     albertel  696:     if ($target eq 'tex' || $target eq 'web' || $target eq 'webgrade') {
1.122     albertel  697: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
                    698: 	if (!$Apache::lonxml::default_homework_loaded) {
                    699: 	    &Apache::lonxml::default_homework_load($safeeval);
                    700: 	}
                    701: 	@Apache::scripttag::parser_env = @_;
                    702: 	my $format=&Apache::lonxml::get_param('format',$parstack,$safeeval);
                    703: 	$result=&Apache::run::run("return &prettyprint(q\0$inside\0,q\0$format\0);",$safeeval);
                    704:     }    
                    705:     return $result;
                    706: }
                    707: 
                    708: sub end_num {
                    709:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    710:     my $result = '';
                    711:     return $result;
                    712: }
                    713: 
                    714: sub start_parse {
                    715:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    716:     my $result = '';
1.137     albertel  717:     if ( $target eq 'web'    || $target eq 'tex'    ||
                    718: 	 $target eq 'grade'  || $target eq 'answer' ||
                    719: 	 $target eq 'analyze'|| $target eq 'webgrade') {
1.122     albertel  720: 	my $inside = &Apache::lonxml::get_all_text_unbalanced("/parse",$parser);
                    721: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
                    722: 	if (!$Apache::lonxml::default_homework_loaded) {
                    723: 	    &Apache::lonxml::default_homework_load($safeeval);
                    724: 	}
                    725: 	@Apache::scripttag::parser_env = @_;
                    726: 	$result=&Apache::run::run("return &xmlparse(q\0$inside\0);",$safeeval);
1.134     albertel  727: 	if ($target eq 'grade' || $target eq 'answer' ||
                    728: 	    $target eq 'analyze') {
                    729: 	    # grade/answer/analyxe should produce no output but if we
                    730: 	    # are redirecting, the redirecter should know what to do
                    731: 	    # with the output
                    732: 	    if (!$Apache::lonxml::redirection) { $result=''; }
                    733: 	}
1.122     albertel  734:     }
                    735:     return $result;
                    736: }
                    737: 
                    738: sub end_parse {
                    739:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    740:     my $result = '';
                    741:     return $result;
                    742: }
1.123     albertel  743: 
                    744: sub start_algebra {
                    745:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    746:     my $result = '';
1.137     albertel  747:     if ( $target eq 'web'     || $target eq 'tex'    ||
                    748: 	 $target eq 'grade'   || $target eq 'answer' ||
                    749: 	 $target eq 'analyze' || $target eq 'webgrade') {
1.123     albertel  750: 	my $inside = &Apache::lonxml::get_all_text_unbalanced("/algebra",$parser);
                    751: 	$inside = &Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
                    752: 	if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.125     albertel  753: 	    my $style=&Apache::lonxml::get_param('style',$parstack,$safeeval);
1.157     raeburn   754: 	    $result=&Apache::lontexconvert::algebra($inside,$target,$style,$parstack,$safeeval);
1.123     albertel  755: 	}
                    756: 	$Apache::lonxml::post_evaluate=0;
                    757:     }
                    758:     return $result;
                    759: }
                    760: 
                    761: sub end_algebra {
                    762:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
                    763:     my $result = '';
                    764:     return $result;
                    765: }
                    766: 
1.1       albertel  767: 1;
                    768: __END__
1.149     jms       769: 
                    770: =pod
                    771: 
                    772: =head1 NAME
                    773: 
                    774: Apache::scripttag.pm
                    775: 
                    776: =head1 SYNOPSIS
                    777: 
                    778: implements <script>, <scriptlib>, <parserlib>,
                    779: and <import>
                    780: 
                    781: This is part of the LearningOnline Network with CAPA project
                    782: described at http://www.lon-capa.org.
                    783: 
                    784: =cut
                    785: 

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