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

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

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