File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.172.2.2: download - view: text, annotated - select for diffs
Wed Sep 14 14:55:06 2016 UTC (7 years, 7 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2
- For 2.11
  - Backport 1.173.

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

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