File:  [LON-CAPA] / loncom / xml / lonxml.pm
Revision 1.136: download - view: text, annotated - select for diffs
Fri Oct 26 17:09:04 2001 UTC (22 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added some meta tags to prevent caching

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

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