File:  [LON-CAPA] / loncom / xml / scripttag.pm
Revision 1.166: download - view: text, annotated - select for diffs
Tue Aug 7 14:50:17 2012 UTC (11 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0_RC1, HEAD
- Improve readability of scriptvars in modal window.

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

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