File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.137: download - view: text, annotated - select for diffs
Wed Oct 31 17:44:42 2001 UTC (22 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- try #2 at no-caching. Creating Real HTTP headers rather than html <meta>
   http-equiv headers

    1: # The LearningOnline Network with CAPA
    2: # XML Parser Module 
    3: #
    4: # last modified 06/26/00 by Alexander Sakharuk
    5: # 11/6 Gerd Kortemeyer
    6: # 6/1/1 Gerd Kortemeyer
    7: # 2/21,3/13 Guy
    8: # 3/29,5/4 Gerd Kortemeyer
    9: # 5/10 Scott Harrison
   10: # 5/26 Gerd Kortemeyer
   11: # 5/27 H. K. Ng
   12: # 6/2,6/3,6/8,6/9 Gerd Kortemeyer
   13: # 6/12,6/13 H. K. Ng
   14: # 6/16 Gerd Kortemeyer
   15: # 7/27 H. K. Ng
   16: # 8/7,8/9,8/10,8/11,8/15,8/16,8/17,8/18,8/20,8/23,8/24 Gerd Kortemeyer
   17: # Guy Albertelli
   18: # 9/26 Gerd Kortemeyer
   19: 
   20: 
   21: package Apache::lonxml; 
   22: use vars 
   23: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace);
   24: use strict;
   25: use HTML::TokeParser;
   26: use HTML::TreeBuilder;
   27: use Safe;
   28: use Safe::Hole;
   29: use Math::Cephes qw(:trigs :hypers :bessels erf erfc);
   30: use Math::Random qw(:all);
   31: use Opcode;
   32: 
   33: sub register {
   34:   my $space;
   35:   my @taglist;
   36:   my $temptag;
   37:   ($space,@taglist) = @_;
   38:   foreach $temptag (@taglist) {
   39:     $Apache::lonxml::alltags{$temptag}=$space;
   40:   }
   41: }
   42: 
   43: use Apache::Constants qw(:common);
   44: use Apache::lontexconvert;
   45: use Apache::style;
   46: use Apache::run;
   47: use Apache::londefdef;
   48: use Apache::scripttag;
   49: use Apache::edit;
   50: use Apache::lonnet;
   51: use Apache::File;
   52: use Apache::loncommon;
   53: 
   54: #==================================================   Main subroutine: xmlparse  
   55: #debugging control, to turn on debugging modify the correct handler
   56: $Apache::lonxml::debug=0;
   57: 
   58: #path to the directory containing the file currently being processed
   59: @pwd=();
   60: 
   61: #these two are used for capturing a subset of the output for later processing,
   62: #don't touch them directly use &startredirection and &endredirection
   63: @outputstack = ();
   64: $redirection = 0;
   65: 
   66: #controls wheter the <import> tag actually does
   67: $import = 1;
   68: @extlinks=();
   69: 
   70: # meta mode is a bit weird only some output is to be turned off
   71: #<output> tag turns metamode off (defined in londefdef.pm)
   72: $metamode = 0;
   73: 
   74: # turns on and of run::evaluate actually derefencing var refs
   75: $evaluate = 1;
   76: 
   77: # data structure for eidt mode, determines what tags can go into what other tags
   78: %insertlist=();
   79: 
   80: # stores the list of active tag namespaces
   81: @namespace=();
   82: 
   83: # has the dynamic menu been updated to know about this resource
   84: $Apache::lonxml::registered=0;
   85: 
   86: sub xmlbegin {
   87:   my $output='';
   88:   if ($ENV{'browser.mathml'}) {
   89:       $output='<?xml version="1.0"?>'
   90:             .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
   91:             .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
   92:             .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
   93:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
   94: 		.'xmlns="http://www.w3.org/TR/REC-html40">';
   95:   } else {
   96:       $output='<html>';
   97:   }
   98:   return $output;
   99: }
  100: 
  101: sub xmlend {
  102:     my $discussion='';
  103:     if ($ENV{'request.course.id'}) {
  104:        my $crs='/'.$ENV{'request.course.id'};
  105:        if ($ENV{'request.course.sec'}) {
  106:           $crs.='_'.$ENV{'request.course.sec'};
  107:        }                 
  108:        $crs=~s/\_/\//g;
  109:        my $seeid=&Apache::lonnet::allowed('rin',$crs);
  110:        my $symb=&Apache::lonnet::symbread();
  111:        if ($symb) {
  112:           my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
  113:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  114: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  115:           if ($contrib{'version'}) {
  116:               $discussion.=
  117:                   '<address><hr /><h2>Course Discussion of Resource</h2>';
  118:               my $idx;
  119:               for ($idx=1;$idx<=$contrib{'version'};$idx++) {
  120: 		my $hidden=($contrib{'hidden'}=~/\.$idx\./);
  121: 		unless (($hidden) && (!$seeid)) {
  122:                  my $message=$contrib{$idx.':message'};
  123:                  $message=~s/\n/\<br \/\>/g;
  124:                  if ($message) {
  125:                   if ($hidden) {
  126: 		      $message='<font color="#888888">'.$message.'</font>';
  127:                   }
  128:                   my $sender='Anonymous';
  129:                   if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
  130:                       $sender=$contrib{$idx.':sendername'}.' at '.
  131: 		      $contrib{$idx.':senderdomain'};
  132:                       if ($contrib{$idx.':anonymous'}) {
  133: 			  $sender.=' (anonymous)';
  134:                       }
  135:                       if ($seeid) {
  136: 			  if ($hidden) {
  137:                              $sender.=' <a href="/adm/feedback?unhide='.
  138: 				 $symb.':::'.$idx.'">Make Visible</a>';
  139:                           } else {
  140:                              $sender.=' <a href="/adm/feedback?hide='.
  141: 				 $symb.':::'.$idx.'">Hide</a>';
  142: 			  }
  143:                       }                   
  144:                   }
  145: 		  $discussion.='<p><b>'.$sender.'</b> ('.
  146:                       localtime($contrib{$idx.':timestamp'}).
  147:                       '):<blockquote>'.$message.
  148:                       '</blockquote></p>';
  149: 	        }
  150:                } 
  151:               }
  152:               $discussion.='</address>';
  153:           }
  154:        }
  155:     }
  156:     return $discussion.'</html>';
  157: }
  158: 
  159: sub tokeninputfield {
  160:     my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
  161:     $defhost=~tr/a-z/A-Z/;
  162:     return (<<ENDINPUTFIELD)
  163: <script>
  164:     function updatetoken() {
  165: 	var comp=new Array;
  166:         var barcode=unescape(document.tokeninput.barcode.value);
  167:         comp=barcode.split('*');
  168:         if (typeof(comp[0])!="undefined") {
  169: 	    document.tokeninput.codeone.value=comp[0];
  170: 	}
  171:         if (typeof(comp[1])!="undefined") {
  172: 	    document.tokeninput.codetwo.value=comp[1];
  173: 	}
  174:         if (typeof(comp[2])!="undefined") {
  175:             comp[2]=comp[2].toUpperCase();
  176: 	    document.tokeninput.codethree.value=comp[2];
  177: 	}
  178:         document.tokeninput.barcode.value='';
  179:     }  
  180: </script>
  181: <form method="post" name="tokeninput">
  182: <table border="2" bgcolor="#FFFFBB">
  183: <tr><th>DocID Checkin</th></tr>
  184: <tr><td>
  185: <table>
  186: <tr>
  187: <td>Scan in Barcode</td>
  188: <td><input type="text" size="22" name="barcode" 
  189: onChange="updatetoken()"/></td>
  190: </tr>
  191: <tr><td><i>or</i> Type in DocID</td>
  192: <td>
  193: <input type="text" size="5" name="codeone" />
  194: <b><font size="+2">*</font></b>
  195: <input type="text" size="5" name="codetwo" />
  196: <b><font size="+2">*</font></b>
  197: <input type="text" size="10" name="codethree" value="$defhost" 
  198: onChange="this.value=this.value.toUpperCase()" />
  199: </td></tr>
  200: </table>
  201: </td></tr>
  202: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
  203: </table>
  204: </form>
  205: ENDINPUTFIELD
  206: }
  207: 
  208: sub maketoken {
  209:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
  210:     unless ($symb) {
  211: 	$symb=&Apache::lonnet::symbread();
  212:     }
  213:     unless ($tuname) {
  214: 	$tuname=$ENV{'user.name'};
  215:         $tudom=$ENV{'user.domain'};
  216:         $tcrsid=$ENV{'request.course.id'};
  217:     }
  218: 
  219:     return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
  220: }
  221: 
  222: sub printtokenheader {
  223:     my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
  224:     unless ($token) { return ''; }
  225: 
  226:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  227:     unless ($tsymb) {
  228: 	$tsymb=$symb;
  229:     }
  230:     unless ($tuname) {
  231: 	$tuname=$name;
  232:         $tudom=$domain;
  233:         $tcrsid=$courseid;
  234:     }
  235: 
  236:     my %reply=&Apache::lonnet::get('environment',
  237:               ['firstname','middlename','lastname','generation'],
  238:               $tudom,$tuname);
  239:     my $plainname=$reply{'firstname'}.' '. 
  240:                   $reply{'middlename'}.' '.
  241:                   $reply{'lastname'}.' '.
  242: 		  $reply{'generation'};
  243: 
  244:     if ($target eq 'web') {
  245: 	return 
  246:  '<img align="right" src="/cgi-bin/barcode.gif?encode='.$token.'" />'.
  247:                'Checked out for '.$plainname.
  248:                '<br />User: '.$tuname.' at '.$tudom.
  249: 	       '<br />CourseID: '.$tcrsid.
  250:                '<br />DocID: '.$token.
  251:                '<br />Time: '.localtime().'<hr />';
  252:     } else {
  253:         return $token;
  254:     }
  255: }
  256: 
  257: sub fontsettings() {
  258:     my $headerstring='';
  259:     if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { 
  260:          $headerstring.=
  261:              '<meta Content-Type="text/html; charset=x-mac-roman">';
  262:     }
  263:     return $headerstring;
  264: }
  265: 
  266: sub registerurl {
  267:     my $forcereg=shift;
  268:     if ($ENV{'request.publicaccess'}) {
  269: 	return 
  270:          '<script>function LONCAPAreg(){} function LONCAPAstale(){}</script>';
  271:     }
  272:     if ($Apache::lonxml::registered && !$forcereg) { return ''; }
  273:     $Apache::lonxml::registered=1;
  274:     if (($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) || ($forcereg)) {
  275:         my $hwkadd='';
  276:         if ($ENV{'REQUEST_URI'}=~/\.(problem|exam|quiz|assess|survey|form)$/) {
  277: 	    if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
  278: 		$hwkadd.=(<<ENDSUBM);
  279:                      menu.switchbutton
  280:            (7,1,'subm.gif','view sub','missions',
  281:                 'gocmd("/adm/grades","submission")');
  282: ENDSUBM
  283:             }
  284: 	    if (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) {
  285: 		$hwkadd.=(<<ENDGRDS);
  286:                      menu.switchbutton
  287:            (7,2,'pgrd.gif','problem','grades',
  288:                 'gocmd("/adm/grades","viewgrades")');
  289: ENDGRDS
  290:             }
  291: 	    if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
  292: 		$hwkadd.=(<<ENDPARM);
  293:                      menu.switchbutton
  294:            (7,3,'pparm.gif','problem','parms',
  295:                 'gocmd("/adm/parmset","set")');
  296: ENDPARM
  297:             }
  298: 	}
  299: 	return (<<ENDREGTHIS);
  300:      
  301: <script language="JavaScript">
  302: // BEGIN LON-CAPA Internal
  303: 
  304:     function LONCAPAreg() {
  305: 	  menu=window.open("","LONCAPAmenu");
  306:           menu.clearTimeout(menu.menucltim);
  307: 	  menu.currentURL=window.location.pathname;
  308:           menu.currentStale=0;
  309:           menu.clearbut(3,1);
  310:           menu.switchbutton
  311:        (6,3,'catalog.gif','catalog','info','catalog_info()');
  312:           menu.switchbutton
  313:        (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
  314:           menu.switchbutton
  315:     (8,2,'fdbk.gif','feedback','on this','gopost("/adm/feedback",currentURL)');
  316:           menu.switchbutton
  317:      (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
  318:           menu.switchbutton
  319:        (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
  320:           menu.switchbutton
  321:      (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
  322:           menu.switchbutton
  323:                             (9,1,'sbkm.gif','set','bookmark','set_bookmark()');
  324:           menu.switchbutton
  325:                          (9,2,'vbkm.gif','view','bookmark','edit_bookmarks()');
  326:           menu.switchbutton
  327:                                (9,3,'anot.gif','anno-','tations','annotate()');
  328:           $hwkadd
  329:     }
  330: 
  331:     function LONCAPAstale() {
  332: 	  menu=window.open("","LONCAPAmenu");
  333:           menu.currentStale=1;
  334:           menu.switchbutton
  335:              (3,1,'reload.gif','return','location','go(currentURL)');
  336:           menu.clearbut(7,1);
  337:           menu.clearbut(7,2);
  338:           menu.clearbut(7,3);
  339:           menu.menucltim=menu.setTimeout(
  340:  'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
  341:  'clearbut(9,1);clearbut(9,2);clearbut(9,3);clearbut(6,3)',
  342: 			  2000);
  343: 
  344:       }
  345: 
  346: // END LON-CAPA Internal
  347: </script>
  348: ENDREGTHIS
  349: 
  350:     } else {
  351:         return (<<ENDDONOTREGTHIS);
  352: 
  353: <script language="JavaScript">
  354: // BEGIN LON-CAPA Internal
  355: 
  356:     function LONCAPAreg() {
  357: 	  menu=window.open("","LONCAPAmenu");
  358:           menu.currentStale=1;
  359:           menu.clearbut(2,1);
  360:           menu.clearbut(2,3);
  361:           menu.clearbut(8,1);
  362:           menu.clearbut(8,2);
  363:           menu.clearbut(8,3);
  364:           if (menu.currentURL) {
  365:              menu.switchbutton
  366:               (3,1,'reload.gif','return','location','go(currentURL)');
  367:  	  } else {
  368: 	      menu.clearbut(3,1);
  369:           }
  370:     }
  371: 
  372:     function LONCAPAstale() {
  373:     }
  374: 
  375: // END LON-CAPA Internal
  376: </script>
  377: ENDDONOTREGTHIS
  378: 
  379:     }
  380: }
  381: 
  382: sub loadevents() {
  383:     return 'LONCAPAreg();';
  384: }
  385: 
  386: sub unloadevents() {
  387:     return 'LONCAPAstale();';
  388: }
  389: 
  390: sub printalltags {
  391:   my $temp;
  392:   foreach $temp (sort keys %Apache::lonxml::alltags) {
  393:     &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
  394:   }
  395: }
  396: 
  397: sub xmlparse {
  398:  my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
  399: 
  400:  &setup_globals($target);
  401:  #&printalltags();
  402:  my @pars = ();
  403:  my $pwd=$ENV{'request.filename'};
  404:  $pwd =~ s:/[^/]*$::;
  405:  &newparser(\@pars,\$content_file_string,$pwd);
  406: 
  407:  my $safeeval = new Safe;
  408:  my $safehole = new Safe::Hole;
  409:  &init_safespace($target,$safeeval,$safehole,$safeinit);
  410: #-------------------- Redefinition of the target in the case of compound target
  411: 
  412:  ($target, my @tenta) = split('&&',$target);
  413: 
  414:  my @stack = (); 
  415:  my @parstack = ();
  416:  &initdepth;
  417: 
  418:  my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
  419: 				   $safeeval,\%style_for_target);
  420:  if ($ENV{'request.uri'}) {
  421:     &writeallows($ENV{'request.uri'});
  422:  }
  423:  return $finaloutput;
  424: }
  425: 
  426: sub htmlclean {
  427:     my ($raw,$full)=@_;
  428: 
  429:     my $tree = HTML::TreeBuilder->new;
  430:     $tree->ignore_unknown(0);
  431:     
  432:     $tree->parse($raw);
  433: 
  434:     my $output= $tree->as_HTML(undef,' ');
  435:      
  436:     $output=~s/\<(br|hr|img|meta|allow)([^\>\/]*)\>/\<$1$2 \/\>/gis;
  437:     $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
  438:     unless ($full) {
  439:        $output=~s/\<[\/]*(body|head|html)\>//gis;
  440:     }
  441: 
  442:     $tree = $tree->delete;
  443: 
  444:     return $output;
  445: }
  446: 
  447: sub inner_xmlparse {
  448:   my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
  449:   my $finaloutput = '';
  450:   my $result;
  451:   my $token;
  452:   while ( $#$pars > -1 ) {
  453:     while ($token = $$pars['-1']->get_token) {
  454:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
  455: 	if ($metamode<1) {
  456: 	  $result=$token->[1];
  457: 	}
  458:       } elsif ($token->[0] eq 'PI') {
  459: 	if ($metamode<1) {
  460: 	  $result=$token->[2];
  461: 	}
  462:       } elsif ($token->[0] eq 'S') {
  463: 	# add tag to stack 	    
  464: 	push (@$stack,$token->[1]);
  465: 	# add parameters list to another stack
  466: 	push (@$parstack,&parstring($token));
  467: 	&increasedepth($token);       
  468: 	if (exists $$style_for_target{$token->[1]}) {
  469: 	  if ($Apache::lonxml::redirection) {
  470: 	    $Apache::lonxml::outputstack['-1'] .=  
  471: 	      &recurse($$style_for_target{$token->[1]},$target,$safeeval,
  472: 		       $style_for_target,@$parstack);
  473: 	  } else {
  474: 	    $finaloutput .= &recurse($$style_for_target{$token->[1]},$target,
  475: 				     $safeeval,$style_for_target,@$parstack);
  476: 	  }
  477: 	} else {
  478: 	  $result = &callsub("start_$token->[1]", $target, $token, $stack,
  479: 			     $parstack, $pars, $safeeval, $style_for_target);
  480: 	}              
  481:       } elsif ($token->[0] eq 'E') {
  482: 	#clear out any tags that didn't end
  483: 	while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
  484: 	  &Apache::lonxml::warning("Unbalanced tags in resource $$stack['-1']");
  485: 	  &end_tag($stack,$parstack,$token);
  486: 	}
  487: 		
  488: 	if (exists $$style_for_target{'/'."$token->[1]"}) {
  489: 	  if ($Apache::lonxml::redirection) {
  490: 	    $Apache::lonxml::outputstack['-1'] .=  
  491: 	      &recurse($$style_for_target{'/'."$token->[1]"},
  492: 		       $target,$safeeval,$style_for_target,@$parstack);
  493: 	  } else {
  494: 	    $finaloutput .= &recurse($$style_for_target{'/'."$token->[1]"},
  495: 				     $target,$safeeval,$style_for_target,
  496: 				     @$parstack);
  497: 	  }
  498: 		    
  499: 	} else {
  500: 	  $result = &callsub("end_$token->[1]", $target, $token, $stack,
  501: 			     $parstack, $pars,$safeeval, $style_for_target);
  502: 	}
  503:       } else {
  504: 	&Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
  505:       }
  506:       #evaluate variable refs in result
  507:       if ($result ne "") {
  508: 	if ( $#$parstack > -1 ) {
  509: 	  if ($Apache::lonxml::redirection) {
  510: 	    $Apache::lonxml::outputstack['-1'] .= 
  511: 	      &Apache::run::evaluate($result,$safeeval,$$parstack['-1']);
  512: 	  } else {
  513: 	    $finaloutput .= &Apache::run::evaluate($result,$safeeval,
  514: 						   $$parstack['-1']);
  515: 	  }
  516: 	} else {
  517: 	  $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
  518: 	}
  519: 	$result = '';
  520:       } 
  521:       if ($token->[0] eq 'E') { 
  522: 	&end_tag($stack,$parstack,$token);
  523:       }
  524:     }
  525:     pop @$pars;
  526:     pop @Apache::lonxml::pwd;
  527:   }
  528: 
  529:   # if ($target eq 'meta') {
  530:   #   $finaloutput.=&endredirection;
  531:   # }
  532: 
  533:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
  534:     $finaloutput=&afterburn($finaloutput);
  535:   }
  536:   return $finaloutput;
  537: }
  538: 
  539: sub recurse {
  540:   my @innerstack = (); 
  541:   my @innerparstack = ();
  542:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
  543:   my @pat = ();
  544:   &newparser(\@pat,\$newarg);
  545:   my $tokenpat;
  546:   my $partstring = '';
  547:   my $output='';
  548:   my $decls='';
  549:   while ( $#pat > -1 ) {
  550:     while  ($tokenpat = $pat[$#pat]->get_token) {
  551:       if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
  552: 	if ($metamode<1) { $partstring=$tokenpat->[1]; }
  553:       } elsif ($tokenpat->[0] eq 'PI') {
  554: 	if ($metamode<1) { $partstring=$tokenpat->[2]; }
  555:       } elsif ($tokenpat->[0] eq 'S') {
  556: 	push (@innerstack,$tokenpat->[1]);
  557: 	push (@innerparstack,&parstring($tokenpat));
  558: 	&increasedepth($tokenpat);
  559: 	$partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
  560: 			       \@innerstack, \@innerparstack, \@pat,
  561: 			       $safeeval, $style_for_target);
  562:       } elsif ($tokenpat->[0] eq 'E') {
  563: 	#clear out any tags that didn't end
  564: 	while ($tokenpat->[1] ne $innerstack[$#innerstack] 
  565: 	       && ($#innerstack > -1)) {
  566: 	  &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
  567: 	  &end_tag(\@innerstack,\@innerparstack,$tokenpat);
  568: 	}
  569: 	$partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
  570: 			       \@innerstack, \@innerparstack, \@pat,
  571: 			       $safeeval, $style_for_target);
  572:       } else {
  573: 	&Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
  574:       }
  575:       #pass both the variable to the style tag, and the tag we 
  576:       #are processing inside the <definedtag>
  577:       if ( $partstring ne "" ) {
  578: 	if ( $#parstack > -1 ) { 
  579: 	  if ( $#innerparstack > -1 ) { 
  580: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
  581: 	  } else {
  582: 	    $decls= $parstack[$#parstack];
  583: 	  }
  584: 	} else {
  585: 	  if ( $#innerparstack > -1 ) { 
  586: 	    $decls=$innerparstack[$#innerparstack];
  587: 	  } else {
  588: 	    $decls='';
  589: 	  }
  590: 	}
  591: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
  592: 	$partstring = '';
  593:       }
  594:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
  595: 				 &decreasedepth($tokenpat);}
  596:     }
  597:     pop @pat;
  598:     pop @Apache::lonxml::pwd;
  599:   }
  600:   return $output;
  601: }
  602: 
  603: sub callsub {
  604:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  605:   my $currentstring='';
  606:   my $nodefault;
  607:   {
  608:     my $sub1;
  609:     no strict 'refs';
  610:     my $tag=$token->[1];
  611:     my $space=$Apache::lonxml::alltags{$tag};
  612:     if (!$space) {
  613: 	$tag=~tr/A-Z/a-z/;
  614: 	$sub=~tr/A-Z/a-z/;
  615: 	$space=$Apache::lonxml::alltags{$tag}
  616:     }
  617: 
  618:     my $deleted=0;
  619:     $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
  620:     if (($token->[0] eq 'S') && ($target eq 'modified')) {
  621:       $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
  622: 					     $parstack,$parser,$safeeval,
  623: 					     $style);
  624:     }
  625:     if (!$deleted) {
  626:       if ($space) {
  627: 	#&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
  628: 	$sub1="$space\:\:$sub";
  629: 	($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
  630: 					     $parstack,$parser,$safeeval,
  631: 					     $style);
  632:       } else {
  633: 	#&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
  634: 	if ($metamode <1) {
  635: 	  if (defined($token->[4]) && ($metamode < 1)) {
  636: 	    $currentstring = $token->[4];
  637: 	  } else {
  638: 	    $currentstring = $token->[2];
  639: 	  }
  640: 	}
  641:       }
  642:       #    &Apache::lonxml::debug("nodefalt:$nodefault:");
  643:       if ($currentstring eq '' && $nodefault eq '') {
  644: 	if ($target eq 'edit') {
  645: 	  &Apache::lonxml::debug("doing default edit for $token->[1]");
  646: 	  if ($token->[0] eq 'S') {
  647: 	    $currentstring = &Apache::edit::tag_start($target,$token);
  648: 	  } elsif ($token->[0] eq 'E') {
  649: 	    $currentstring = &Apache::edit::tag_end($target,$token);
  650: 	  }
  651: 	} elsif ($target eq 'modified') {
  652: 	  if ($token->[0] eq 'S') {
  653: 	    $currentstring = $token->[4];
  654: 	    $currentstring.=&Apache::edit::handle_insert();
  655: 	  } else {
  656: 	    $currentstring = $token->[2];
  657: 	  }
  658: 	}
  659:       }
  660:     }
  661:     use strict 'refs';
  662:   }
  663:   return $currentstring;
  664: }
  665: 
  666: sub setup_globals {
  667:   my ($target)=@_;
  668:   $Apache::lonxml::registered = 0;
  669:   @Apache::lonxml::pwd=();
  670:   @Apache::lonxml::extlinks=();
  671:   if ($target eq 'meta') {
  672:     $Apache::lonxml::redirection = 0;
  673:     $Apache::lonxml::metamode = 1;
  674:     $Apache::lonxml::evaluate = 1;
  675:     $Apache::lonxml::import = 0;
  676:   } elsif ($target eq 'answer') {
  677:     $Apache::lonxml::redirection = 0;
  678:     $Apache::lonxml::metamode = 1;
  679:     $Apache::lonxml::evaluate = 1;
  680:     $Apache::lonxml::import = 1;
  681:   } elsif ($target eq 'grade') {
  682:     &startredirection;
  683:     $Apache::lonxml::metamode = 0;
  684:     $Apache::lonxml::evaluate = 1;
  685:     $Apache::lonxml::import = 1;
  686:   } elsif ($target eq 'modified') {
  687:     $Apache::lonxml::redirection = 0;
  688:     $Apache::lonxml::metamode = 0;
  689:     $Apache::lonxml::evaluate = 0;
  690:     $Apache::lonxml::import = 0;
  691:   } elsif ($target eq 'edit') {
  692:     $Apache::lonxml::redirection = 0;
  693:     $Apache::lonxml::metamode = 0;
  694:     $Apache::lonxml::evaluate = 0;
  695:     $Apache::lonxml::import = 0;
  696:   } else {
  697:     $Apache::lonxml::redirection = 0;
  698:     $Apache::lonxml::metamode = 0;
  699:     $Apache::lonxml::evaluate = 1;
  700:     $Apache::lonxml::import = 1;
  701:   }
  702: }
  703: 
  704: sub init_safespace {
  705:   my ($target,$safeeval,$safehole,$safeinit) = @_;
  706:   $safeeval->permit("entereval");
  707:   $safeeval->permit(":base_math");
  708:   $safeeval->permit("sort");
  709:   $safeeval->deny(":base_io");
  710:   $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
  711:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
  712:   
  713:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
  714:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
  715:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
  716:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
  717:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
  718:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
  719:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
  720:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
  721:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
  722:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
  723:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
  724:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
  725:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
  726:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
  727:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
  728:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
  729:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
  730:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
  731:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
  732:   $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
  733:   $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
  734:   $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
  735:   $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
  736:   $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
  737:   $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
  738:   $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
  739:   $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
  740:   $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
  741:   $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
  742:   $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
  743:   $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
  744:   $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
  745:   $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
  746:   $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
  747:   $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
  748:   $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
  749:   $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
  750:   $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
  751:   $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
  752:   $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
  753: 
  754: #need to inspect this class of ops
  755: # $safeeval->deny(":base_orig");
  756:   $safeinit .= ';$external::target="'.$target.'";';
  757:   my $rndseed;
  758:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  759:   $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
  760:   $safeinit .= ';$external::randomseed='.$rndseed.';';
  761:   &Apache::run::run($safeinit,$safeeval);
  762: }
  763: 
  764: sub startredirection {
  765:   $Apache::lonxml::redirection++;
  766:   push (@Apache::lonxml::outputstack, '');
  767: }
  768: 
  769: sub endredirection {
  770:   if (!$Apache::lonxml::redirection) {
  771:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
  772:     return '';
  773:   }
  774:   $Apache::lonxml::redirection--;
  775:   pop @Apache::lonxml::outputstack;
  776: }
  777: 
  778: sub end_tag {
  779:   my ($tagstack,$parstack,$token)=@_;
  780:   pop(@$tagstack);
  781:   pop(@$parstack);
  782:   &decreasedepth($token);
  783: }
  784: 
  785: sub initdepth {
  786:   @Apache::lonxml::depthcounter=();
  787:   $Apache::lonxml::depth=-1;
  788:   $Apache::lonxml::olddepth=-1;
  789: }
  790: 
  791: sub increasedepth {
  792:   my ($token) = @_;
  793:   $Apache::lonxml::depth++;
  794:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
  795:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
  796:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
  797:   }
  798:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  799:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
  800: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
  801: }
  802: 
  803: sub decreasedepth {
  804:   my ($token) = @_;
  805:   $Apache::lonxml::depth--;
  806:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
  807:     $#Apache::lonxml::depthcounter--;
  808:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
  809:   }
  810:   if (  $Apache::lonxml::depth < -1) {
  811:     &Apache::lonxml::warning("Unbalanced tags in resource");   
  812:     $Apache::lonxml::depth='-1';
  813:   }
  814:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
  815:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
  816: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
  817: }
  818: 
  819: sub get_all_text {
  820: 
  821:  my($tag,$pars)= @_;
  822:  my $depth=0;
  823:  my $token;
  824:  my $result='';
  825:  if ( $tag =~ m:^/: ) { 
  826:    my $tag=substr($tag,1); 
  827: #   &Apache::lonxml::debug("have:$tag:");
  828:    while (($depth >=0) && ($token = $pars->get_token)) {
  829: #     &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
  830:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  831:        $result.=$token->[1];
  832:      } elsif ($token->[0] eq 'PI') {
  833:        $result.=$token->[2];
  834:      } elsif ($token->[0] eq 'S') {
  835:        if ($token->[1] eq $tag) { $depth++; }
  836:        $result.=$token->[4];
  837:      } elsif ($token->[0] eq 'E')  {
  838:        if ( $token->[1] eq $tag) { $depth--; }
  839:        #skip sending back the last end tag
  840:        if ($depth > -1) { $result.=$token->[2]; } else {
  841: 	 $pars->unget_token($token);
  842:        }
  843:      }
  844:    }
  845:  } else {
  846:    while ($token = $pars->get_token) {
  847: #     &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
  848:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  849:        $result.=$token->[1];
  850:      } elsif ($token->[0] eq 'PI') {
  851:        $result.=$token->[2];
  852:      } elsif ($token->[0] eq 'S') {
  853:        if ( $token->[1] eq $tag) { 
  854: 	 $pars->unget_token($token); last;
  855:        } else {
  856: 	 $result.=$token->[4];
  857:        }
  858:      } elsif ($token->[0] eq 'E')  {
  859:        $result.=$token->[2];
  860:      }
  861:    }
  862:  }
  863: # &Apache::lonxml::debug("Exit:$result:");
  864:  return $result
  865: }
  866: 
  867: sub newparser {
  868:   my ($parser,$contentref,$dir) = @_;
  869:   push (@$parser,HTML::TokeParser->new($contentref));
  870:   $$parser['-1']->xml_mode('1');
  871:   if ( $dir eq '' ) {
  872:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
  873:   } else {
  874:     push (@Apache::lonxml::pwd, $dir);
  875:   } 
  876: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
  877: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
  878: }
  879: 
  880: sub parstring {
  881:   my ($token) = @_;
  882:   my $temp='';
  883:   map {
  884:     unless ($_=~/\W/) {
  885:       my $val=$token->[2]->{$_};
  886:       $val =~ s/([\%\@\\])/\\$1/g;
  887:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
  888:       $temp .= "my \$$_=\"$val\";"
  889:     }
  890:   } @{$token->[3]};
  891:   return $temp;
  892: }
  893: 
  894: sub writeallows {
  895:     unless ($#extlinks>=0) { return; }
  896:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
  897:     if ($ENV{'httpref.'.$thisurl}) {
  898: 	$thisurl=$ENV{'httpref.'.$thisurl};
  899:     }
  900:     my $thisdir=$thisurl;
  901:     $thisdir=~s/\/[^\/]+$//;
  902:     my %httpref=();
  903:     map {
  904:        $httpref{'httpref.'.
  905:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
  906:     } @extlinks;
  907:     @extlinks=();
  908:     &Apache::lonnet::appenv(%httpref);
  909: }
  910: 
  911: #
  912: # Afterburner handles anchors, highlights and links
  913: #
  914: sub afterburn {
  915:     my $result=shift;
  916:     map {
  917:        my ($name, $value) = split(/=/,$_);
  918:        $value =~ tr/+/ /;
  919:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  920:        if (($name eq 'highlight')||($name eq 'anchor')||($name eq 'link')) {
  921:            unless ($ENV{'form.'.$name}) {
  922:               $ENV{'form.'.$name}=$value;
  923: 	   }
  924:        }
  925:     } (split(/&/,$ENV{'QUERY_STRING'}));
  926:     if ($ENV{'form.highlight'}) {
  927:         map {
  928:            my $anchorname=$_;
  929: 	   my $matchthis=$anchorname;
  930:            $matchthis=~s/\_+/\\s\+/g;
  931:            $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
  932:        } split(/\,/,$ENV{'form.highlight'});
  933:     }
  934:     if ($ENV{'form.link'}) {
  935:         map {
  936:            my ($anchorname,$linkurl)=split(/\>/,$_);
  937: 	   my $matchthis=$anchorname;
  938:            $matchthis=~s/\_+/\\s\+/g;
  939:            $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
  940:        } split(/\,/,$ENV{'form.link'});
  941:     }
  942:     if ($ENV{'form.anchor'}) {
  943:         my $anchorname=$ENV{'form.anchor'};
  944: 	my $matchthis=$anchorname;
  945:         $matchthis=~s/\_+/\\s\+/g;
  946:         $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
  947:         $result.=(<<"ENDSCRIPT");
  948: <script>
  949:     document.location.hash='$anchorname';
  950: </script>
  951: ENDSCRIPT
  952:     }
  953:     return $result;
  954: }
  955: 
  956: sub storefile {
  957:     my ($file,$contents)=@_;
  958:     if (my $fh=Apache::File->new('>'.$file)) {
  959: 	print $fh $contents;
  960:         $fh->close();
  961:     }
  962: }
  963: 
  964: sub inserteditinfo {
  965:       my ($result,$filecontents)=@_;
  966:       unless ($filecontents) {
  967: 	  $filecontents=(<<SIMPLECONTENT);
  968: <html>
  969: <head>
  970: <title>
  971:                            Title of Document Goes Here
  972: </title>
  973: </head>
  974: <body bgcolor="#FFFFFF">
  975: 
  976:                            Body of Document Goes Here
  977: 
  978: </body>
  979: </html>
  980: SIMPLECONTENT
  981:       }
  982:       my $editheader='<a href="#editsection">Edit below</a><hr />';
  983:       my $editfooter=(<<ENDFOOTER);
  984: <hr />
  985: <a name="editsection" />
  986: <form method="post">
  987: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
  988: <br />
  989: <input type="submit" name="attemptclean" 
  990:        value="Save and then attempt to clean HTML" />
  991: <input type="submit" name="savethisfile" value="Save this" />
  992: </form>
  993: ENDFOOTER
  994:       $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
  995:       $result=~s/(\<\/body\>)/$editfooter/is;
  996:       return $result;
  997: }
  998: 
  999: sub handler {
 1000:   my $request=shift;
 1001: 
 1002:   my $target='web';
 1003: 
 1004:   $Apache::lonxml::debug=0;
 1005: 
 1006:   if ($ENV{'browser.mathml'}) {
 1007:     $request->content_type('text/xml');
 1008:   } else {
 1009:     $request->content_type('text/html');
 1010:   }
 1011:   &Apache::loncommon::no_cache($request); 
 1012:   $request->send_http_header;
 1013:   
 1014:   return OK if $request->header_only;
 1015: 
 1016: 
 1017:   my $file=&Apache::lonnet::filelocation("",$request->uri);
 1018: #
 1019: # Edit action? Save file.
 1020: #
 1021:   unless ($ENV{'request.state'} eq 'published') {
 1022:       if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
 1023: 	  &storefile($file,$ENV{'form.filecont'});
 1024:       }
 1025:   }
 1026:   my %mystyle;
 1027:   my $result = ''; 
 1028:   my $filecontents=&Apache::lonnet::getfile($file);
 1029:   if ($filecontents == -1) {
 1030:     $result=(<<ENDNOTFOUND);
 1031: <html>
 1032: <head>
 1033: <title>File not found</title>
 1034: </head>
 1035: <body bgcolor="#FFFFFF">
 1036: <b>File not found: $file</b>
 1037: </body>
 1038: </html>
 1039: ENDNOTFOUND
 1040:     $filecontents='';
 1041:   } else {
 1042:       unless ($ENV{'request.state'} eq 'published') {
 1043:          if ($ENV{'form.attemptclean'}) {
 1044: 	    $filecontents=&htmlclean($filecontents,1);
 1045:          }
 1046:       }
 1047:     $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
 1048:   }
 1049: 
 1050: #
 1051: # Edit action? Insert editing commands
 1052: #
 1053:   unless ($ENV{'request.state'} eq 'published') {
 1054:       $result=&inserteditinfo($result,$filecontents);
 1055:   }
 1056:   
 1057:   writeallows($request->uri);
 1058: 
 1059:   $request->print($result);
 1060: 
 1061:   return OK;
 1062: }
 1063:  
 1064: sub debug {
 1065:   if ($Apache::lonxml::debug eq 1) {
 1066:     print("DEBUG:".$_[0]."<br />\n");
 1067:   }
 1068: }
 1069: 
 1070: sub error {
 1071:   if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
 1072:     print "<b>ERROR:</b>".$_[0]."<br />\n";
 1073:   } else {
 1074:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
 1075:     #notify author
 1076:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
 1077:     #notify course
 1078:     if ( $ENV{'request.course.id'} ) {
 1079:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
 1080:       foreach my $user (split /\,/, $users) {
 1081: 	($user,my $domain) = split /:/, $user;
 1082: 	&Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
 1083:       }
 1084:     }
 1085: 
 1086:     #FIXME probably shouldn't have me get everything forever.
 1087:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
 1088:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
 1089:   }
 1090: }
 1091: 
 1092: sub warning {
 1093:   if ($ENV{'request.state'} eq 'construct') {
 1094:     print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
 1095:   }
 1096: }
 1097: 
 1098: sub get_param {
 1099:   my ($param,$parstack,$safeeval,$context) = @_;
 1100:   if ( ! $context ) { $context = -1; }
 1101:   my $args ='';
 1102:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
 1103:   if ( $args =~ /my \$$param=\"/ ) {
 1104:     return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
 1105:   } else {
 1106:     return undef;
 1107:   }
 1108: }
 1109: 
 1110: sub get_param_var {
 1111:   my ($param,$parstack,$safeeval,$context) = @_;
 1112:   if ( ! $context ) { $context = -1; }
 1113:   my $args ='';
 1114:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
 1115:   if ( $args !~ /my \$$param=\"/ ) { return undef; }
 1116:   my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
 1117:   if ($value =~ /^[\$\@\%]/) {
 1118:     return &Apache::run::run("return $value",$safeeval,1);
 1119:   } else {
 1120:     return $value;
 1121:   }
 1122: }
 1123: 
 1124: sub register_insert {
 1125:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
 1126:   my $i;
 1127:   my $tagnum=0;
 1128:   my @order;
 1129:   for ($i=0;$i < $#data; $i++) {
 1130:     my $line = $data[$i];
 1131:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
 1132:     if ( $line =~ /TABLE/ ) { last; }
 1133:     my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
 1134:     if ($tag) {
 1135:       $insertlist{"$tagnum.tag"} = $tag;
 1136:       $insertlist{"$tagnum.description"} = $descrip;
 1137:       $insertlist{"$tagnum.color"} = $color;
 1138:       $insertlist{"$tagnum.function"} = $function;
 1139:       if (!defined($show)) { $show='yes'; }
 1140:       $insertlist{"$tagnum.show"}= $show;
 1141:       $insertlist{"$tag.num"}=$tagnum;
 1142:       $tagnum++;
 1143:     }
 1144:   }
 1145:   $i++; #skipping TABLE line
 1146:   $tagnum = 0;
 1147:   for (;$i < $#data;$i++) {
 1148:     my $line = $data[$i];
 1149:     my ($mnemonic,@which) = split(/ +/,$line);
 1150:     my $tag = $insertlist{"$tagnum.tag"};
 1151:     for (my $j=0;$j <$#which;$j++) {
 1152:       if ( $which[$j] eq 'Y' ) {
 1153: 	if ($insertlist{"$j.show"} ne 'no') {
 1154: 	  push(@{ $insertlist{"$tag.which"} },$j);
 1155: 	}
 1156:       }
 1157:     }
 1158:     $tagnum++;
 1159:   }
 1160: }
 1161: 
 1162: sub description {
 1163:   my ($token)=@_;
 1164:   return $insertlist{$insertlist{"$token->[1].num"}.'.description'};
 1165: }
 1166: 
 1167: # ----------------------------------------------------------------- whichuser
 1168: # returns a list of $symb, $courseid, $domain, $name that is correct for
 1169: # calls to lonnet functions for this setup.
 1170: # - looks for form.grade_ parameters
 1171: sub whichuser {
 1172:   my ($symb,$courseid,$domain,$name);
 1173:   if (defined($ENV{'form.grade_symb'})) {
 1174:     my $tmp_courseid=$ENV{'form.grade_courseid'};
 1175:     my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
 1176:     if ($allowed) {
 1177:       $symb=$ENV{'form.grade_symb'};
 1178:       $courseid=$ENV{'form.grade_courseid'};
 1179:       $domain=$ENV{'form.grade_domain'};
 1180:       $name=$ENV{'form.grade_username'};
 1181:     }
 1182:   } else {
 1183:     $symb=&Apache::lonnet::symbread();
 1184:     $courseid=$ENV{'request.course.id'};
 1185:     $domain=$ENV{'user.domain'};
 1186:     $name=$ENV{'user.name'};
 1187:   }
 1188:   return ($symb,$courseid,$domain,$name);
 1189: }
 1190: 
 1191: 1;
 1192: __END__
 1193: 
 1194: 

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