Annotation of loncom/xml/lonxml.pm, revision 1.200

1.2       sakharuk    1: # The LearningOnline Network with CAPA
1.3       sakharuk    2: # XML Parser Module 
1.2       sakharuk    3: #
1.200   ! www         4: # $Id: lonxml.pm,v 1.199 2002/10/08 17:09:09 albertel Exp $
1.139     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: #
                     28: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
                     29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
                     30: # binary executable programs or libraries distributed by the 
                     31: # Michigan State University (the "Licensee"), but any binaries so 
                     32: # distributed are hereby licensed only for use in the context
                     33: # of a program or computational system for which the Licensee is the 
                     34: # primary author or distributor, and which performs substantial 
                     35: # additional tasks beyond the translation of (La)TeX into HTML.
                     36: # The C source of the Code may not be distributed by the Licensee
                     37: # to any other parties under any circumstances.
                     38: #
1.3       sakharuk   39: # last modified 06/26/00 by Alexander Sakharuk
1.33      www        40: # 11/6 Gerd Kortemeyer
1.45      www        41: # 6/1/1 Gerd Kortemeyer
1.56      albertel   42: # 2/21,3/13 Guy
1.68      www        43: # 3/29,5/4 Gerd Kortemeyer
1.73      harris41   44: # 5/10 Scott Harrison
1.78      www        45: # 5/26 Gerd Kortemeyer
1.80      harris41   46: # 5/27 H. K. Ng
1.89      www        47: # 6/2,6/3,6/8,6/9 Gerd Kortemeyer
1.93      ng         48: # 6/12,6/13 H. K. Ng
1.95      www        49: # 6/16 Gerd Kortemeyer
1.104     ng         50: # 7/27 H. K. Ng
1.127     www        51: # 8/7,8/9,8/10,8/11,8/15,8/16,8/17,8/18,8/20,8/23,8/24 Gerd Kortemeyer
1.130     www        52: # Guy Albertelli
                     53: # 9/26 Gerd Kortemeyer
1.143     www        54: # Dec Guy Albertelli
                     55: # YEAR=2002
                     56: # 1/1 Gerd Kortemeyer
1.145     www        57: # 1/2 Matthew Hall
                     58: # 1/3 Gerd Kortemeyer
1.143     www        59: #
1.2       sakharuk   60: 
1.4       albertel   61: package Apache::lonxml; 
1.33      www        62: use vars 
1.169     albertel   63: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $prevent_entity_encode);
1.1       sakharuk   64: use strict;
1.167     albertel   65: use HTML::LCParser();
1.161     albertel   66: use HTML::TreeBuilder();
                     67: use HTML::Entities();
                     68: use Safe();
                     69: use Safe::Hole();
                     70: use Math::Cephes();
                     71: use Math::Random();
                     72: use Opcode();
1.72      albertel   73: 
                     74: sub register {
1.141     albertel   75:   my ($space,@taglist) = @_;
                     76:   foreach my $temptag (@taglist) {
                     77:     push(@{ $Apache::lonxml::alltags{$temptag} },$space);
1.72      albertel   78:   }
                     79: }
                     80: 
1.141     albertel   81: sub deregister {
                     82:   my ($space,@taglist) = @_;
                     83:   foreach my $temptag (@taglist) {
                     84:     my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
                     85:     if ($tempspace eq $space) {
                     86:       pop(@{ $Apache::lonxml::alltags{$temptag} });
                     87:     }
                     88:   }
1.142     albertel   89:   #&printalltags();
1.141     albertel   90: }
                     91: 
1.46      www        92: use Apache::Constants qw(:common);
1.161     albertel   93: use Apache::lontexconvert();
                     94: use Apache::style();
                     95: use Apache::run();
                     96: use Apache::londefdef();
                     97: use Apache::scripttag();
                     98: use Apache::edit();
                     99: use Apache::lonnet();
                    100: use Apache::File();
                    101: use Apache::loncommon();
1.198     www       102: use Apache::lonfeedback();
1.200   ! www       103: use Apache::lonmsg();
1.79      www       104: 
1.72      albertel  105: #==================================================   Main subroutine: xmlparse  
                    106: #debugging control, to turn on debugging modify the correct handler
                    107: $Apache::lonxml::debug=0;
                    108: 
                    109: #path to the directory containing the file currently being processed
                    110: @pwd=();
                    111: 
                    112: #these two are used for capturing a subset of the output for later processing,
                    113: #don't touch them directly use &startredirection and &endredirection
                    114: @outputstack = ();
                    115: $redirection = 0;
                    116: 
                    117: #controls wheter the <import> tag actually does
                    118: $import = 1;
                    119: @extlinks=();
                    120: 
                    121: # meta mode is a bit weird only some output is to be turned off
                    122: #<output> tag turns metamode off (defined in londefdef.pm)
                    123: $metamode = 0;
                    124: 
                    125: # turns on and of run::evaluate actually derefencing var refs
                    126: $evaluate = 1;
1.7       albertel  127: 
1.74      albertel  128: # data structure for eidt mode, determines what tags can go into what other tags
                    129: %insertlist=();
1.68      www       130: 
1.99      albertel  131: # stores the list of active tag namespaces
1.76      albertel  132: @namespace=();
                    133: 
1.169     albertel  134: # if 0 all high ASCII characters will be encoded into HTML Entities
                    135: $prevent_entity_encode=0;
                    136: 
1.99      albertel  137: # has the dynamic menu been updated to know about this resource
                    138: $Apache::lonxml::registered=0;
                    139: 
1.172     albertel  140: # a pointer the the Apache request object
                    141: $Apache::lonxml::request='';
                    142: 
1.68      www       143: sub xmlbegin {
                    144:   my $output='';
                    145:   if ($ENV{'browser.mathml'}) {
                    146:       $output='<?xml version="1.0"?>'
                    147:             .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
                    148:             .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                    149:             .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
                    150:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                    151: 		.'xmlns="http://www.w3.org/TR/REC-html40">';
                    152:   } else {
                    153:       $output='<html>';
                    154:   }
                    155:   return $output;
                    156: }
                    157: 
                    158: sub xmlend {
1.194     www       159:     my ($discussiononly,$symb)=@_;
1.103     www       160:     my $discussion='';
                    161:     if ($ENV{'request.course.id'}) {
1.109     www       162:        my $crs='/'.$ENV{'request.course.id'};
                    163:        if ($ENV{'request.course.sec'}) {
                    164:           $crs.='_'.$ENV{'request.course.sec'};
                    165:        }                 
                    166:        $crs=~s/\_/\//g;
                    167:        my $seeid=&Apache::lonnet::allowed('rin',$crs);
1.194     www       168:        unless ($symb) {
                    169:            $symb=&Apache::lonnet::symbread();
                    170:        }
1.103     www       171:        if ($symb) {
                    172:           my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
                    173:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    174: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    175:           if ($contrib{'version'}) {
1.194     www       176:               unless ($discussiononly) {
                    177:                  $discussion.=
                    178:                   '<address><hr />';
                    179: 	     }
1.103     www       180:               my $idx;
                    181:               for ($idx=1;$idx<=$contrib{'version'};$idx++) {
1.110     www       182: 		my $hidden=($contrib{'hidden'}=~/\.$idx\./);
                    183: 		unless (($hidden) && (!$seeid)) {
                    184:                  my $message=$contrib{$idx.':message'};
                    185:                  $message=~s/\n/\<br \/\>/g;
1.186     www       186: 		 $message=&Apache::lontexconvert::msgtexconverted($message);
1.110     www       187:                  if ($message) {
                    188:                   if ($hidden) {
                    189: 		      $message='<font color="#888888">'.$message.'</font>';
                    190:                   }
1.196     www       191:                   my $screenname=&Apache::loncommon::screenname(
                    192:                                $contrib{$idx.':sendername'},
                    193: 			       $contrib{$idx.':senderdomain'});
                    194:                   my $plainname=&Apache::loncommon::nickname(
                    195:                                $contrib{$idx.':sendername'},
                    196: 			       $contrib{$idx.':senderdomain'});
                    197: 
1.109     www       198:                   my $sender='Anonymous';
                    199:                   if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
1.194     www       200:                       $sender=&Apache::loncommon::aboutmewrapper(
1.196     www       201:                                $plainname,
1.194     www       202:                                $contrib{$idx.':sendername'},
                    203:                                $contrib{$idx.':senderdomain'}).' ('.
1.164     www       204:                               $contrib{$idx.':sendername'}.' at '.
                    205: 		      $contrib{$idx.':senderdomain'}.')';
1.109     www       206:                       if ($contrib{$idx.':anonymous'}) {
1.164     www       207: 			  $sender.=' [anonymous] '.
1.196     www       208:                                      $screenname;
1.110     www       209:                       }
                    210:                       if ($seeid) {
                    211: 			  if ($hidden) {
                    212:                              $sender.=' <a href="/adm/feedback?unhide='.
                    213: 				 $symb.':::'.$idx.'">Make Visible</a>';
                    214:                           } else {
                    215:                              $sender.=' <a href="/adm/feedback?hide='.
                    216: 				 $symb.':::'.$idx.'">Hide</a>';
                    217: 			  }
1.109     www       218:                       }                   
1.164     www       219:                   } else {
1.196     www       220:                       if ($screenname) {
                    221: 			  $sender='<i>'.$screenname.'</i>';
1.164     www       222:                       }
1.109     www       223:                   }
                    224: 		  $discussion.='<p><b>'.$sender.'</b> ('.
1.103     www       225:                       localtime($contrib{$idx.':timestamp'}).
                    226:                       '):<blockquote>'.$message.
1.110     www       227:                       '</blockquote></p>';
                    228: 	        }
                    229:                } 
1.103     www       230:               }
1.194     www       231:               unless ($discussiononly) {
                    232:                  $discussion.='</address>';
                    233: 	      }
                    234:           }
                    235:           if ($discussiononly) {
1.195     www       236: 	      $discussion.=(<<ENDDISCUSS);
1.198     www       237: <form action="/adm/feedback" method="post" name="mailform">
1.195     www       238: <input type="submit" name="discuss" value="Post Discussion" />
                    239: <input type="submit" name="anondiscuss" value="Post Anonymous Discussion" />
                    240: <input type="hidden" name="symb" value="$symb" />
                    241: <input type="hidden" name="sendit" value="true" />
                    242: <br />
                    243: <font size="1">Note: in anonymous discussion, your name is visible only to
                    244: course faculty</font><br />
                    245: <textarea name=comment cols=60 rows=10 wrap=hard></textarea>
                    246: </form>
                    247: ENDDISCUSS
1.198     www       248:              $discussion.=&Apache::lonfeedback::generate_preview_button();
1.103     www       249:           }
                    250:        }
                    251:     }
1.194     www       252:     return $discussion.($discussiononly?'':'</html>');
1.119     www       253: }
                    254: 
                    255: sub tokeninputfield {
1.120     www       256:     my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
                    257:     $defhost=~tr/a-z/A-Z/;
1.119     www       258:     return (<<ENDINPUTFIELD)
1.120     www       259: <script>
                    260:     function updatetoken() {
                    261: 	var comp=new Array;
                    262:         var barcode=unescape(document.tokeninput.barcode.value);
                    263:         comp=barcode.split('*');
                    264:         if (typeof(comp[0])!="undefined") {
                    265: 	    document.tokeninput.codeone.value=comp[0];
                    266: 	}
                    267:         if (typeof(comp[1])!="undefined") {
                    268: 	    document.tokeninput.codetwo.value=comp[1];
                    269: 	}
                    270:         if (typeof(comp[2])!="undefined") {
                    271:             comp[2]=comp[2].toUpperCase();
                    272: 	    document.tokeninput.codethree.value=comp[2];
                    273: 	}
                    274:         document.tokeninput.barcode.value='';
                    275:     }  
                    276: </script>
                    277: <form method="post" name="tokeninput">
1.119     www       278: <table border="2" bgcolor="#FFFFBB">
                    279: <tr><th>DocID Checkin</th></tr>
                    280: <tr><td>
                    281: <table>
                    282: <tr>
                    283: <td>Scan in Barcode</td>
1.120     www       284: <td><input type="text" size="22" name="barcode" 
                    285: onChange="updatetoken()"/></td>
1.119     www       286: </tr>
                    287: <tr><td><i>or</i> Type in DocID</td>
                    288: <td>
                    289: <input type="text" size="5" name="codeone" />
1.120     www       290: <b><font size="+2">*</font></b>
1.119     www       291: <input type="text" size="5" name="codetwo" />
1.120     www       292: <b><font size="+2">*</font></b>
                    293: <input type="text" size="10" name="codethree" value="$defhost" 
                    294: onChange="this.value=this.value.toUpperCase()" />
1.119     www       295: </td></tr>
                    296: </table>
                    297: </td></tr>
                    298: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
                    299: </table>
                    300: </form>
                    301: ENDINPUTFIELD
1.112     www       302: }
                    303: 
1.116     www       304: sub maketoken {
1.118     www       305:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
1.112     www       306:     unless ($symb) {
                    307: 	$symb=&Apache::lonnet::symbread();
                    308:     }
                    309:     unless ($tuname) {
                    310: 	$tuname=$ENV{'user.name'};
                    311:         $tudom=$ENV{'user.domain'};
                    312:         $tcrsid=$ENV{'request.course.id'};
                    313:     }
1.116     www       314: 
1.118     www       315:     return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
                    316: }
                    317: 
                    318: sub printtokenheader {
1.133     albertel  319:     my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
1.116     www       320:     unless ($token) { return ''; }
1.118     www       321: 
1.133     albertel  322:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    323:     unless ($tsymb) {
                    324: 	$tsymb=$symb;
1.118     www       325:     }
                    326:     unless ($tuname) {
1.133     albertel  327: 	$tuname=$name;
                    328:         $tudom=$domain;
                    329:         $tcrsid=$courseid;
1.118     www       330:     }
1.114     www       331: 
                    332:     my %reply=&Apache::lonnet::get('environment',
                    333:               ['firstname','middlename','lastname','generation'],
                    334:               $tudom,$tuname);
                    335:     my $plainname=$reply{'firstname'}.' '. 
                    336:                   $reply{'middlename'}.' '.
                    337:                   $reply{'lastname'}.' '.
                    338: 		  $reply{'generation'};
                    339: 
1.112     www       340:     if ($target eq 'web') {
1.145     www       341:         my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
1.115     www       342: 	return 
                    343:  '<img align="right" src="/cgi-bin/barcode.gif?encode='.$token.'" />'.
                    344:                'Checked out for '.$plainname.
1.114     www       345:                '<br />User: '.$tuname.' at '.$tudom.
1.145     www       346: 	       '<br />ID: '.$idhash{$tuname}.
1.117     www       347: 	       '<br />CourseID: '.$tcrsid.
1.145     www       348: 	       '<br />Course: '.$ENV{'course.'.$tcrsid.'.description'}.
1.114     www       349:                '<br />DocID: '.$token.
1.116     www       350:                '<br />Time: '.localtime().'<hr />';
1.112     www       351:     } else {
1.121     albertel  352:         return $token;
1.112     www       353:     }
1.68      www       354: }
                    355: 
1.70      www       356: sub fontsettings() {
                    357:     my $headerstring='';
                    358:     if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) { 
                    359:          $headerstring.=
                    360:              '<meta Content-Type="text/html; charset=x-mac-roman">';
                    361:     }
                    362:     return $headerstring;
                    363: }
                    364: 
1.68      www       365: sub registerurl {
1.100     www       366:     my $forcereg=shift;
1.155     matthew   367:     my $target = shift;
                    368:     my $result = '';
1.197     sakharuk  369:     
1.179     matthew   370:     if ($target eq 'edit') {
1.187     matthew   371:         $result .="<script>\n".
                    372:             "if (typeof menu != 'undefined') {menu.currentURL=null;}\n".
1.179     matthew   373:             &Apache::loncommon::browser_and_searcher_javascript().
                    374:                 "\n</script>\n";
                    375:     }
1.176     www       376:     if ((($ENV{'request.publicaccess'}) || 
                    377:          (!&Apache::lonnet::is_on_map($ENV{'REQUEST_URI'}))) &&
                    378:         (!$forcereg)) {
1.179     matthew   379: 	return $result.
1.130     www       380:          '<script>function LONCAPAreg(){} function LONCAPAstale(){}</script>';
                    381:     }
1.128     albertel  382:     if ($Apache::lonxml::registered && !$forcereg) { return ''; }
1.105     albertel  383:     $Apache::lonxml::registered=1;
1.159     www       384:     my $nothing='';
                    385:     if ($ENV{'browser.type'} eq 'explorer') { $nothing='javascript:void(0);'; }
1.200   ! www       386:     my $newmail='';
        !           387:     if (&Apache::lonmsg::newmail()) { 
        !           388:        $newmail='menu.setstatus("you have","got mail");';
        !           389:     }
1.181     www       390:     my $timesync='menu.syncclock(1000*'.time.');';
1.100     www       391:     if (($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) || ($forcereg)) {
1.87      www       392:         my $hwkadd='';
1.168     albertel  393:         if ($ENV{'request.filename'}=~/\.(problem|exam|quiz|assess|survey|form)$/) {
1.87      www       394: 	    if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
                    395: 		$hwkadd.=(<<ENDSUBM);
1.168     albertel  396:                      menu.switchbutton(7,1,'subm.gif','view sub','missions','gocmd("/adm/grades","submission")');
1.87      www       397: ENDSUBM
                    398:             }
                    399: 	    if (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) {
                    400: 		$hwkadd.=(<<ENDGRDS);
1.171     albertel  401:                      menu.switchbutton(7,2,'pgrd.gif','problem','grades','gocmd("/adm/grades","gradingmenu")');
1.87      www       402: ENDGRDS
                    403:             }
                    404: 	    if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
                    405: 		$hwkadd.=(<<ENDPARM);
1.168     albertel  406:                      menu.switchbutton(7,3,'pparm.gif','problem','parms','gocmd("/adm/parmset","set")');
1.87      www       407: ENDPARM
                    408:             }
                    409: 	}
1.155     matthew   410: 	$result = (<<ENDREGTHIS);
1.87      www       411:      
1.68      www       412: <script language="JavaScript">
1.71      www       413: // BEGIN LON-CAPA Internal
1.86      www       414: 
1.69      www       415:     function LONCAPAreg() {
1.159     www       416: 	  menu=window.open("$nothing","LONCAPAmenu","",false);
1.86      www       417:           menu.clearTimeout(menu.menucltim);
1.174     www       418:           $timesync
1.200   ! www       419:           $newmail
1.69      www       420: 	  menu.currentURL=window.location.pathname;
1.175     www       421:           menu.reloadURL=window.location.pathname;
1.69      www       422:           menu.currentStale=0;
1.85      www       423:           menu.clearbut(3,1);
                    424:           menu.switchbutton
1.108     www       425:        (6,3,'catalog.gif','catalog','info','catalog_info()');
                    426:           menu.switchbutton
1.85      www       427:        (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
                    428:           menu.switchbutton
1.184     www       429:     (8,2,'fdbk.gif','feedback','discuss','gopost("/adm/feedback",currentURL)');
1.85      www       430:           menu.switchbutton
                    431:      (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
                    432:           menu.switchbutton
                    433:        (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
                    434:           menu.switchbutton
                    435:      (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
1.94      www       436:           menu.switchbutton
                    437:                             (9,1,'sbkm.gif','set','bookmark','set_bookmark()');
                    438:           menu.switchbutton
                    439:                          (9,2,'vbkm.gif','view','bookmark','edit_bookmarks()');
                    440:           menu.switchbutton
1.95      www       441:                                (9,3,'anot.gif','anno-','tations','annotate()');
1.87      www       442:           $hwkadd
1.69      www       443:     }
1.86      www       444: 
1.69      www       445:     function LONCAPAstale() {
1.159     www       446: 	  menu=window.open("$nothing","LONCAPAmenu","",false);
1.86      www       447:           menu.currentStale=1;
1.175     www       448:           if (menu.reloadURL!='' && menu.reloadURL!= null) { 
                    449:              menu.switchbutton
                    450:              (3,1,'reload.gif','return','location','go(reloadURL)');
                    451: 	  }
1.127     www       452:           menu.clearbut(7,1);
                    453:           menu.clearbut(7,2);
                    454:           menu.clearbut(7,3);
1.86      www       455:           menu.menucltim=menu.setTimeout(
1.94      www       456:  'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
1.108     www       457:  'clearbut(9,1);clearbut(9,2);clearbut(9,3);clearbut(6,3)',
1.86      www       458: 			  2000);
                    459: 
1.87      www       460:       }
1.86      www       461: 
                    462: // END LON-CAPA Internal
                    463: </script>
                    464: ENDREGTHIS
                    465: 
                    466:     } else {
1.155     matthew   467:         $result = (<<ENDDONOTREGTHIS);
1.86      www       468: 
                    469: <script language="JavaScript">
                    470: // BEGIN LON-CAPA Internal
                    471: 
                    472:     function LONCAPAreg() {
1.159     www       473: 	  menu=window.open("$nothing","LONCAPAmenu","",false);
1.174     www       474:           $timesync
1.69      www       475:           menu.currentStale=1;
1.85      www       476:           menu.clearbut(2,1);
                    477:           menu.clearbut(2,3);
                    478:           menu.clearbut(8,1);
                    479:           menu.clearbut(8,2);
                    480:           menu.clearbut(8,3);
1.86      www       481:           if (menu.currentURL) {
                    482:              menu.switchbutton
                    483:               (3,1,'reload.gif','return','location','go(currentURL)');
                    484:  	  } else {
                    485: 	      menu.clearbut(3,1);
                    486:           }
                    487:     }
                    488: 
                    489:     function LONCAPAstale() {
1.68      www       490:     }
1.86      www       491: 
1.71      www       492: // END LON-CAPA Internal
1.68      www       493: </script>
1.86      www       494: ENDDONOTREGTHIS
1.155     matthew   495:     }
                    496:     return $result;
1.69      www       497: }
                    498: 
                    499: sub loadevents() {
                    500:     return 'LONCAPAreg();';
                    501: }
                    502: 
                    503: sub unloadevents() {
                    504:     return 'LONCAPAstale();';
1.68      www       505: }
                    506: 
1.48      albertel  507: sub printalltags {
                    508:   my $temp;
                    509:   foreach $temp (sort keys %Apache::lonxml::alltags) {
1.141     albertel  510:     &Apache::lonxml::debug("$temp -- ".
                    511: 		  join(',',@{ $Apache::lonxml::alltags{$temp} }));
1.48      albertel  512:   }
                    513: }
1.31      sakharuk  514: 
1.3       sakharuk  515: sub xmlparse {
1.172     albertel  516:  my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
1.96      albertel  517: 
1.172     albertel  518:  &setup_globals($request,$target);
1.178     www       519: #
                    520: # do we have a course style file?
                    521: #
                    522: 
                    523:  if ($ENV{'request.course.id'}) {
                    524:      my $bodytext=
                    525: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.default_xml_style'};
                    526:      if ($bodytext) {
                    527:        my $location=&Apache::lonnet::filelocation('',$bodytext);
                    528:        my $styletext=&Apache::lonnet::getfile($location);
                    529:        if ($styletext ne '-1') {
                    530:           %style_for_target = (%style_for_target,
                    531:                           &Apache::style::styleparser($target,$styletext));
                    532:        }
                    533:     }
                    534:  }
                    535: 
1.48      albertel  536:  #&printalltags();
1.16      albertel  537:  my @pars = ();
1.23      albertel  538:  my $pwd=$ENV{'request.filename'};
                    539:  $pwd =~ s:/[^/]*$::;
                    540:  &newparser(\@pars,\$content_file_string,$pwd);
1.24      sakharuk  541: 
1.3       sakharuk  542:  my $safeeval = new Safe;
1.40      albertel  543:  my $safehole = new Safe::Hole;
1.82      ng        544:  &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3       sakharuk  545: #-------------------- Redefinition of the target in the case of compound target
                    546: 
                    547:  ($target, my @tenta) = split('&&',$target);
                    548: 
1.150     albertel  549:  my @stack = ();
1.3       sakharuk  550:  my @parstack = ();
1.17      albertel  551:  &initdepth;
1.67      www       552: 
1.101     albertel  553:  my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
                    554: 				   $safeeval,\%style_for_target);
1.125     www       555:  if ($ENV{'request.uri'}) {
                    556:     &writeallows($ENV{'request.uri'});
                    557:  }
1.3       sakharuk  558:  return $finaloutput;
1.106     www       559: }
                    560: 
                    561: sub htmlclean {
1.107     www       562:     my ($raw,$full)=@_;
1.106     www       563: 
                    564:     my $tree = HTML::TreeBuilder->new;
                    565:     $tree->ignore_unknown(0);
1.140     albertel  566: 
1.106     www       567:     $tree->parse($raw);
                    568: 
1.107     www       569:     my $output= $tree->as_HTML(undef,' ');
1.140     albertel  570: 
1.161     albertel  571:     $output=~s/\<(br|hr|img|meta|allow)(.*?)\>/\<$1$2 \/\>/gis;
1.111     www       572:     $output=~s/\<\/(br|hr|img|meta|allow)\>//gis;
1.107     www       573:     unless ($full) {
                    574:        $output=~s/\<[\/]*(body|head|html)\>//gis;
                    575:     }
1.106     www       576: 
                    577:     $tree = $tree->delete;
                    578: 
                    579:     return $output;
1.15      albertel  580: }
                    581: 
1.191     albertel  582: sub latex_special_symbols {
1.188     sakharuk  583:     my ($current_token,$stack,$parstack)=@_;
1.192     albertel  584:     $current_token=~s/\\/\\char92 /g;
                    585:     $current_token=~s/\^/\\char94 /g;
                    586:     $current_token=~s/\~/\\char126 /g;
1.197     sakharuk  587:     $current_token=~s/(&[^a-z\#])/\\$1/g;
                    588:     if ($current_token=~/ \#\w/) {
                    589: 	$current_token=~s/ \#(\w)/ \\#$1/;
                    590:     } else {
1.199     albertel  591: 	$current_token=~s/([^&])(\#)/$1\\$2/g;
1.197     sakharuk  592:     }
1.192     albertel  593:     $current_token=~s/(\$|_|{|})/\\$1/g;
                    594:     $current_token=~s/\\char92 /\\texttt{\\char92}/g;
                    595:     $current_token=~s/>/\$>\$/g; #more
                    596:     $current_token=~s/</\$<\$/g; #less
                    597:     if ($current_token=~m/\d%/) {$current_token =~ s/(\d)%/$1\\%/g;} #percent after digit
                    598:     if ($current_token=~m/\s%/) {$current_token =~ s/(\s)%/$1\\%/g;} #persent after space
1.188     sakharuk  599:     return $current_token;
                    600: }
                    601: 
1.101     albertel  602: sub inner_xmlparse {
                    603:   my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target)=@_;
                    604:   my $finaloutput = '';
                    605:   my $result;
                    606:   my $token;
                    607:   while ( $#$pars > -1 ) {
                    608:     while ($token = $$pars['-1']->get_token) {
                    609:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
                    610: 	if ($metamode<1) {
1.190     albertel  611: 	    my $text=$token->[1];
1.193     albertel  612: 	    if ($token->[0] eq 'C' && $target eq 'tex') {
1.190     albertel  613: 		$text = '%'.$text;
                    614: 		$text =~ s/[\n\r]//g;
1.182     sakharuk  615: 	    }
1.190     albertel  616: 	    $result.=$text;
1.101     albertel  617: 	}
                    618:       } elsif ($token->[0] eq 'PI') {
                    619: 	if ($metamode<1) {
                    620: 	  $result=$token->[2];
                    621: 	}
                    622:       } elsif ($token->[0] eq 'S') {
1.140     albertel  623: 	# add tag to stack
1.101     albertel  624: 	push (@$stack,$token->[1]);
                    625: 	# add parameters list to another stack
                    626: 	push (@$parstack,&parstring($token));
1.140     albertel  627: 	&increasedepth($token);
1.101     albertel  628: 	if (exists $$style_for_target{$token->[1]}) {
                    629: 	  if ($Apache::lonxml::redirection) {
1.140     albertel  630: 	    $Apache::lonxml::outputstack['-1'] .=
1.101     albertel  631: 	      &recurse($$style_for_target{$token->[1]},$target,$safeeval,
                    632: 		       $style_for_target,@$parstack);
                    633: 	  } else {
                    634: 	    $finaloutput .= &recurse($$style_for_target{$token->[1]},$target,
                    635: 				     $safeeval,$style_for_target,@$parstack);
                    636: 	  }
                    637: 	} else {
                    638: 	  $result = &callsub("start_$token->[1]", $target, $token, $stack,
                    639: 			     $parstack, $pars, $safeeval, $style_for_target);
1.140     albertel  640: 	}
1.101     albertel  641:       } elsif ($token->[0] eq 'E') {
                    642: 	#clear out any tags that didn't end
                    643: 	while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
1.150     albertel  644: 	  my $lasttag=$$stack[-1];
                    645: 	  if ($token->[1] =~ /^$lasttag$/i) {
                    646: 	    &Apache::lonxml::warning('Using tag &lt;/'.$token->[1].'&gt; as end tag to &lt;'.$$stack[-1].'&gt;');
                    647: 	    last;
                    648: 	  } else {
1.152     albertel  649: 	    &Apache::lonxml::warning('Found tag &lt;/'.$token->[1].'&gt; when looking for &lt;/'.$$stack[-1].'&gt; in file');
1.150     albertel  650: 	    &end_tag($stack,$parstack,$token);
                    651: 	  }
1.101     albertel  652: 	}
1.140     albertel  653: 
                    654: 	if (exists($$style_for_target{'/'."$token->[1]"})) {
1.101     albertel  655: 	  if ($Apache::lonxml::redirection) {
                    656: 	    $Apache::lonxml::outputstack['-1'] .=  
                    657: 	      &recurse($$style_for_target{'/'."$token->[1]"},
                    658: 		       $target,$safeeval,$style_for_target,@$parstack);
                    659: 	  } else {
                    660: 	    $finaloutput .= &recurse($$style_for_target{'/'."$token->[1]"},
                    661: 				     $target,$safeeval,$style_for_target,
                    662: 				     @$parstack);
                    663: 	  }
                    664: 	} else {
                    665: 	  $result = &callsub("end_$token->[1]", $target, $token, $stack,
                    666: 			     $parstack, $pars,$safeeval, $style_for_target);
                    667: 	}
                    668:       } else {
                    669: 	&Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
                    670:       }
                    671:       #evaluate variable refs in result
                    672:       if ($result ne "") {
                    673: 	if ( $#$parstack > -1 ) {
1.169     albertel  674: 	  $result=&Apache::run::evaluate($result,$safeeval,$$parstack[-1]);
1.101     albertel  675: 	} else {
1.169     albertel  676: 	  $result= &Apache::run::evaluate($result,$safeeval,'');
1.101     albertel  677: 	}
1.163     albertel  678:       }
1.190     albertel  679:       if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
                    680: 	if ($target eq 'tex') {
1.191     albertel  681: 	    $result=&latex_special_symbols($result,$stack,$parstack);
1.190     albertel  682: 	}
                    683:       }
                    684: 
1.169     albertel  685:       # Encode any high ASCII characters
                    686:       if (!$Apache::lonxml::prevent_entity_encode) {
                    687: 	$result=&HTML::Entities::encode($result,"\200-\377");
                    688:       }
                    689:       if ($Apache::lonxml::redirection) {
                    690: 	$Apache::lonxml::outputstack['-1'] .= $result;
                    691:       } else {
                    692: 	$finaloutput.=$result;
                    693:       }
                    694:       $result = '';
                    695: 
1.101     albertel  696:       if ($token->[0] eq 'E') { 
                    697: 	&end_tag($stack,$parstack,$token);
                    698:       }
                    699:     }
                    700:     pop @$pars;
                    701:     pop @Apache::lonxml::pwd;
                    702:   }
                    703: 
                    704:   # if ($target eq 'meta') {
                    705:   #   $finaloutput.=&endredirection;
                    706:   # }
                    707: 
1.169     albertel  708: 
1.101     albertel  709:   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
                    710:     $finaloutput=&afterburn($finaloutput);
                    711:   }
                    712:   return $finaloutput;
                    713: }
1.67      www       714: 
1.15      albertel  715: sub recurse {
                    716:   my @innerstack = (); 
                    717:   my @innerparstack = ();
                    718:   my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16      albertel  719:   my @pat = ();
1.23      albertel  720:   &newparser(\@pat,\$newarg);
1.15      albertel  721:   my $tokenpat;
                    722:   my $partstring = '';
                    723:   my $output='';
1.16      albertel  724:   my $decls='';
1.140     albertel  725:   &Apache::lonxml::debug("Recursing");
1.16      albertel  726:   while ( $#pat > -1 ) {
                    727:     while  ($tokenpat = $pat[$#pat]->get_token) {
1.57      albertel  728:       if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
1.61      albertel  729: 	if ($metamode<1) { $partstring=$tokenpat->[1]; }
1.57      albertel  730:       } elsif ($tokenpat->[0] eq 'PI') {
1.61      albertel  731: 	if ($metamode<1) { $partstring=$tokenpat->[2]; }
1.16      albertel  732:       } elsif ($tokenpat->[0] eq 'S') {
                    733: 	push (@innerstack,$tokenpat->[1]);
                    734: 	push (@innerparstack,&parstring($tokenpat));
1.19      albertel  735: 	&increasedepth($tokenpat);
1.84      albertel  736: 	$partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
                    737: 			       \@innerstack, \@innerparstack, \@pat,
                    738: 			       $safeeval, $style_for_target);
1.16      albertel  739:       } elsif ($tokenpat->[0] eq 'E') {
                    740: 	#clear out any tags that didn't end
1.150     albertel  741: 	while ($tokenpat->[1] ne $innerstack[$#innerstack]
1.43      albertel  742: 	       && ($#innerstack > -1)) {
1.150     albertel  743: 	  my $lasttag=$innerstack[-1];
                    744: 	  if ($tokenpat->[1] =~ /^$lasttag$/i) {
                    745: 	    &Apache::lonxml::warning('Using tag &lt;/'.$tokenpat->[1].'&gt; as end tag to &lt;'.$innerstack[-1].'&gt;');
                    746: 	    last;
                    747: 	  } else {
1.152     albertel  748: 	    &Apache::lonxml::warning('Found tag &lt;/'.$tokenpat->[1].'&gt; when looking for &lt;/'.$innerstack[-1].'&gt; in file');
1.150     albertel  749: 	    &end_tag(\@innerstack,\@innerparstack,$tokenpat);
                    750: 	  }
1.43      albertel  751: 	}
1.84      albertel  752: 	$partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
                    753: 			       \@innerstack, \@innerparstack, \@pat,
                    754: 			       $safeeval, $style_for_target);
1.57      albertel  755:       } else {
                    756: 	&Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
1.16      albertel  757:       }
                    758:       #pass both the variable to the style tag, and the tag we 
                    759:       #are processing inside the <definedtag>
                    760:       if ( $partstring ne "" ) {
                    761: 	if ( $#parstack > -1 ) { 
                    762: 	  if ( $#innerparstack > -1 ) { 
                    763: 	    $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
                    764: 	  } else {
                    765: 	    $decls= $parstack[$#parstack];
                    766: 	  }
                    767: 	} else {
                    768: 	  if ( $#innerparstack > -1 ) { 
                    769: 	    $decls=$innerparstack[$#innerparstack];
                    770: 	  } else {
                    771: 	    $decls='';
                    772: 	  }
                    773: 	}
                    774: 	$output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
                    775: 	$partstring = '';
                    776:       }
1.17      albertel  777:       if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19      albertel  778: 				 &decreasedepth($tokenpat);}
1.15      albertel  779:     }
1.16      albertel  780:     pop @pat;
1.23      albertel  781:     pop @Apache::lonxml::pwd;
1.15      albertel  782:   }
1.140     albertel  783:   &Apache::lonxml::debug("Exiting Recursing");
1.15      albertel  784:   return $output;
1.7       albertel  785: }
                    786: 
                    787: sub callsub {
1.84      albertel  788:   my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel  789:   my $currentstring='';
1.72      albertel  790:   my $nodefault;
1.7       albertel  791:   {
1.59      albertel  792:     my $sub1;
1.7       albertel  793:     no strict 'refs';
1.68      www       794:     my $tag=$token->[1];
1.141     albertel  795:     my $space=$Apache::lonxml::alltags{$tag}[-1];
1.68      www       796:     if (!$space) {
1.141     albertel  797:      	$tag=~tr/A-Z/a-z/;
1.68      www       798: 	$sub=~tr/A-Z/a-z/;
1.141     albertel  799: 	$space=$Apache::lonxml::alltags{$tag}[-1]
1.68      www       800:     }
1.97      albertel  801: 
                    802:     my $deleted=0;
                    803:     $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
                    804:     if (($token->[0] eq 'S') && ($target eq 'modified')) {
                    805:       $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
                    806: 					     $parstack,$parser,$safeeval,
                    807: 					     $style);
                    808:     }
                    809:     if (!$deleted) {
                    810:       if ($space) {
1.189     sakharuk  811: 	&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
1.97      albertel  812: 	$sub1="$space\:\:$sub";
                    813: 	($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
                    814: 					     $parstack,$parser,$safeeval,
                    815: 					     $style);
                    816:       } else {
1.189     sakharuk  817: 	&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
1.97      albertel  818: 	if ($metamode <1) {
                    819: 	  if (defined($token->[4]) && ($metamode < 1)) {
                    820: 	    $currentstring = $token->[4];
                    821: 	  } else {
                    822: 	    $currentstring = $token->[2];
                    823: 	  }
1.62      sakharuk  824: 	}
1.7       albertel  825:       }
1.97      albertel  826:       #    &Apache::lonxml::debug("nodefalt:$nodefault:");
                    827:       if ($currentstring eq '' && $nodefault eq '') {
                    828: 	if ($target eq 'edit') {
                    829: 	  &Apache::lonxml::debug("doing default edit for $token->[1]");
                    830: 	  if ($token->[0] eq 'S') {
                    831: 	    $currentstring = &Apache::edit::tag_start($target,$token);
                    832: 	  } elsif ($token->[0] eq 'E') {
                    833: 	    $currentstring = &Apache::edit::tag_end($target,$token);
                    834: 	  }
                    835: 	} elsif ($target eq 'modified') {
                    836: 	  if ($token->[0] eq 'S') {
                    837: 	    $currentstring = $token->[4];
                    838: 	    $currentstring.=&Apache::edit::handle_insert();
                    839: 	  } else {
                    840: 	    $currentstring = $token->[2];
                    841: 	  }
1.72      albertel  842: 	}
                    843:       }
1.7       albertel  844:     }
                    845:     use strict 'refs';
                    846:   }
                    847:   return $currentstring;
1.82      ng        848: }
                    849: 
1.96      albertel  850: sub setup_globals {
1.172     albertel  851:   my ($request,$target)=@_;
                    852:   $Apache::lonxml::request=$request;
1.99      albertel  853:   $Apache::lonxml::registered = 0;
1.101     albertel  854:   @Apache::lonxml::pwd=();
1.124     albertel  855:   @Apache::lonxml::extlinks=();
1.96      albertel  856:   if ($target eq 'meta') {
                    857:     $Apache::lonxml::redirection = 0;
                    858:     $Apache::lonxml::metamode = 1;
                    859:     $Apache::lonxml::evaluate = 1;
                    860:     $Apache::lonxml::import = 0;
1.129     albertel  861:   } elsif ($target eq 'answer') {
                    862:     $Apache::lonxml::redirection = 0;
                    863:     $Apache::lonxml::metamode = 1;
                    864:     $Apache::lonxml::evaluate = 1;
                    865:     $Apache::lonxml::import = 1;
1.96      albertel  866:   } elsif ($target eq 'grade') {
                    867:     &startredirection;
                    868:     $Apache::lonxml::metamode = 0;
                    869:     $Apache::lonxml::evaluate = 1;
                    870:     $Apache::lonxml::import = 1;
                    871:   } elsif ($target eq 'modified') {
                    872:     $Apache::lonxml::redirection = 0;
                    873:     $Apache::lonxml::metamode = 0;
                    874:     $Apache::lonxml::evaluate = 0;
                    875:     $Apache::lonxml::import = 0;
                    876:   } elsif ($target eq 'edit') {
                    877:     $Apache::lonxml::redirection = 0;
                    878:     $Apache::lonxml::metamode = 0;
                    879:     $Apache::lonxml::evaluate = 0;
                    880:     $Apache::lonxml::import = 0;
1.163     albertel  881:   } elsif ($target eq 'analyze') {
                    882:     $Apache::lonxml::redirection = 0;
                    883:     $Apache::lonxml::metamode = 0;
                    884:     $Apache::lonxml::evaluate = 1;
                    885:     $Apache::lonxml::import = 1;
1.96      albertel  886:   } else {
                    887:     $Apache::lonxml::redirection = 0;
                    888:     $Apache::lonxml::metamode = 0;
                    889:     $Apache::lonxml::evaluate = 1;
                    890:     $Apache::lonxml::import = 1;
                    891:   }
                    892: }
                    893: 
1.82      ng        894: sub init_safespace {
                    895:   my ($target,$safeeval,$safehole,$safeinit) = @_;
                    896:   $safeeval->permit("entereval");
                    897:   $safeeval->permit(":base_math");
                    898:   $safeeval->permit("sort");
                    899:   $safeeval->deny(":base_io");
1.102     albertel  900:   $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
1.82      ng        901:   $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
                    902:   
                    903:   $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
                    904:   $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
                    905:   $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
                    906:   $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
                    907:   $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
                    908:   $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
                    909:   $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
                    910:   $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
                    911:   $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
                    912:   $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
                    913:   $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
                    914:   $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
                    915:   $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
                    916:   $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
                    917:   $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
                    918:   $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
                    919:   $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
                    920:   $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
                    921:   $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
1.91      ng        922:   $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
                    923:   $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
                    924:   $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
                    925:   $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
                    926:   $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
                    927:   $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
                    928:   $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
                    929:   $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
                    930:   $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
                    931:   $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
                    932:   $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
1.93      ng        933:   $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
1.91      ng        934:   $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
                    935:   $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
                    936:   $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
                    937:   $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
                    938:   $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
                    939:   $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
                    940:   $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
                    941:   $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
                    942:   $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
                    943: 
1.82      ng        944: #need to inspect this class of ops
                    945: # $safeeval->deny(":base_orig");
1.91      ng        946:   $safeinit .= ';$external::target="'.$target.'";';
1.121     albertel  947:   my $rndseed;
1.123     albertel  948:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    949:   $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
1.121     albertel  950:   $safeinit .= ';$external::randomseed='.$rndseed.';';
1.82      ng        951:   &Apache::run::run($safeinit,$safeeval);
1.17      albertel  952: }
                    953: 
1.55      albertel  954: sub startredirection {
                    955:   $Apache::lonxml::redirection++;
                    956:   push (@Apache::lonxml::outputstack, '');
                    957: }
                    958: 
                    959: sub endredirection {
                    960:   if (!$Apache::lonxml::redirection) {
1.72      albertel  961:     &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
1.55      albertel  962:     return '';
                    963:   }
                    964:   $Apache::lonxml::redirection--;
                    965:   pop @Apache::lonxml::outputstack;
1.97      albertel  966: }
                    967: 
                    968: sub end_tag {
                    969:   my ($tagstack,$parstack,$token)=@_;
                    970:   pop(@$tagstack);
                    971:   pop(@$parstack);
                    972:   &decreasedepth($token);
1.55      albertel  973: }
                    974: 
1.17      albertel  975: sub initdepth {
                    976:   @Apache::lonxml::depthcounter=();
                    977:   $Apache::lonxml::depth=-1;
                    978:   $Apache::lonxml::olddepth=-1;
                    979: }
                    980: 
                    981: sub increasedepth {
1.19      albertel  982:   my ($token) = @_;
1.17      albertel  983:   $Apache::lonxml::depth++;
                    984:   $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
                    985:   if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
                    986:     $Apache::lonxml::olddepth=$Apache::lonxml::depth;
                    987:   }
1.42      albertel  988:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64      albertel  989:   &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.54      albertel  990: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17      albertel  991: }
                    992: 
                    993: sub decreasedepth {
1.19      albertel  994:   my ($token) = @_;
1.17      albertel  995:   $Apache::lonxml::depth--;
1.36      albertel  996:   if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
                    997:     $#Apache::lonxml::depthcounter--;
                    998:     $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
                    999:   }
1.43      albertel 1000:   if (  $Apache::lonxml::depth < -1) {
1.140     albertel 1001:     &Apache::lonxml::warning("Missing tags, unable to properly run file.");
1.43      albertel 1002:     $Apache::lonxml::depth='-1';
                   1003:   }
1.42      albertel 1004:   my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64      albertel 1005:   &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.54      albertel 1006: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1       sakharuk 1007: }
1.19      albertel 1008: 
1.180     albertel 1009: sub get_all_text_unbalanced {
1.190     albertel 1010: #there is a copy of this in lonpublisher.pm
1.180     albertel 1011:  my($tag,$pars)= @_;
                   1012:  my $token;
                   1013:  my $result='';
                   1014:  $tag='<'.$tag.'>';
                   1015:  while ($token = $$pars[-1]->get_token) {
                   1016:    if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                   1017:      $result.=$token->[1];
                   1018:    } elsif ($token->[0] eq 'PI') {
                   1019:      $result.=$token->[2];
                   1020:    } elsif ($token->[0] eq 'S') {
                   1021:      $result.=$token->[4];
                   1022:    } elsif ($token->[0] eq 'E')  {
                   1023:      $result.=$token->[2];
                   1024:    }
                   1025:    if ($result =~ /(.*)$tag(.*)/) {
                   1026:      &Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
                   1027:      &Apache::lonxml::debug('Result is :'.$1);
                   1028:      $result=$1;
                   1029:      my $redo=$tag.$2;
                   1030:      &Apache::lonxml::newparser($pars,\$redo);
                   1031:      last;
                   1032:    }
                   1033:  }
                   1034:  return $result
                   1035: }
                   1036: 
1.19      albertel 1037: sub get_all_text {
                   1038:  my($tag,$pars)= @_;
                   1039:  my $depth=0;
                   1040:  my $token;
                   1041:  my $result='';
1.57      albertel 1042:  if ( $tag =~ m:^/: ) { 
                   1043:    my $tag=substr($tag,1); 
                   1044: #   &Apache::lonxml::debug("have:$tag:");
                   1045:    while (($depth >=0) && ($token = $pars->get_token)) {
                   1046: #     &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
                   1047:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                   1048:        $result.=$token->[1];
                   1049:      } elsif ($token->[0] eq 'PI') {
                   1050:        $result.=$token->[2];
                   1051:      } elsif ($token->[0] eq 'S') {
1.151     albertel 1052:        if ($token->[1] =~ /^$tag$/i) { $depth++; }
1.57      albertel 1053:        $result.=$token->[4];
                   1054:      } elsif ($token->[0] eq 'E')  {
1.151     albertel 1055:        if ( $token->[1] =~ /^$tag$/i) { $depth--; }
1.57      albertel 1056:        #skip sending back the last end tag
                   1057:        if ($depth > -1) { $result.=$token->[2]; } else {
                   1058: 	 $pars->unget_token($token);
                   1059:        }
                   1060:      }
                   1061:    }
                   1062:  } else {
                   1063:    while ($token = $pars->get_token) {
                   1064: #     &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
                   1065:      if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                   1066:        $result.=$token->[1];
                   1067:      } elsif ($token->[0] eq 'PI') {
                   1068:        $result.=$token->[2];
                   1069:      } elsif ($token->[0] eq 'S') {
1.151     albertel 1070:        if ( $token->[1] =~ /^$tag$/i) {
1.57      albertel 1071: 	 $pars->unget_token($token); last;
                   1072:        } else {
                   1073: 	 $result.=$token->[4];
                   1074:        }
                   1075:      } elsif ($token->[0] eq 'E')  {
                   1076:        $result.=$token->[2];
1.36      albertel 1077:      }
1.19      albertel 1078:    }
                   1079:  }
1.49      albertel 1080: # &Apache::lonxml::debug("Exit:$result:");
1.19      albertel 1081:  return $result
                   1082: }
                   1083: 
1.23      albertel 1084: sub newparser {
                   1085:   my ($parser,$contentref,$dir) = @_;
1.167     albertel 1086:   push (@$parser,HTML::LCParser->new($contentref));
1.56      albertel 1087:   $$parser['-1']->xml_mode('1');
1.23      albertel 1088:   if ( $dir eq '' ) {
                   1089:     push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
                   1090:   } else {
                   1091:     push (@Apache::lonxml::pwd, $dir);
                   1092:   } 
                   1093: #  &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
                   1094: #  &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
                   1095: }
1.1       sakharuk 1096: 
1.8       albertel 1097: sub parstring {
                   1098:   my ($token) = @_;
                   1099:   my $temp='';
1.142     albertel 1100:   foreach (@{$token->[3]}) {
1.35      www      1101:     unless ($_=~/\W/) {
1.42      albertel 1102:       my $val=$token->[2]->{$_};
1.150     albertel 1103:       $val =~ s/([\%\@\\\"])/\\$1/g;
1.51      albertel 1104:       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.42      albertel 1105:       $temp .= "my \$$_=\"$val\";"
1.20      albertel 1106:     }
1.142     albertel 1107:   }
1.8       albertel 1108:   return $temp;
                   1109: }
1.22      albertel 1110: 
1.34      www      1111: sub writeallows {
1.126     www      1112:     unless ($#extlinks>=0) { return; }
1.34      www      1113:     my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
1.111     www      1114:     if ($ENV{'httpref.'.$thisurl}) {
                   1115: 	$thisurl=$ENV{'httpref.'.$thisurl};
                   1116:     }
1.34      www      1117:     my $thisdir=$thisurl;
                   1118:     $thisdir=~s/\/[^\/]+$//;
                   1119:     my %httpref=();
1.142     albertel 1120:     foreach (@extlinks) {
1.34      www      1121:        $httpref{'httpref.'.
1.125     www      1122:  	        &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl;
1.142     albertel 1123:     }
1.126     www      1124:     @extlinks=();
1.34      www      1125:     &Apache::lonnet::appenv(%httpref);
                   1126: }
                   1127: 
1.66      www      1128: #
                   1129: # Afterburner handles anchors, highlights and links
                   1130: #
                   1131: sub afterburn {
                   1132:     my $result=shift;
1.154     albertel 1133:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1134: 					    ['highlight','anchor','link']);
1.66      www      1135:     if ($ENV{'form.highlight'}) {
1.142     albertel 1136:        foreach (split(/\,/,$ENV{'form.highlight'})) {
1.66      www      1137:            my $anchorname=$_;
                   1138: 	   my $matchthis=$anchorname;
                   1139:            $matchthis=~s/\_+/\\s\+/g;
                   1140:            $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
1.142     albertel 1141:        }
1.66      www      1142:     }
                   1143:     if ($ENV{'form.link'}) {
1.142     albertel 1144:        foreach (split(/\,/,$ENV{'form.link'})) {
1.66      www      1145:            my ($anchorname,$linkurl)=split(/\>/,$_);
                   1146: 	   my $matchthis=$anchorname;
                   1147:            $matchthis=~s/\_+/\\s\+/g;
                   1148:            $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1.142     albertel 1149:        }
1.66      www      1150:     }
                   1151:     if ($ENV{'form.anchor'}) {
                   1152:         my $anchorname=$ENV{'form.anchor'};
                   1153: 	my $matchthis=$anchorname;
                   1154:         $matchthis=~s/\_+/\\s\+/g;
                   1155:         $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
                   1156:         $result.=(<<"ENDSCRIPT");
                   1157: <script>
                   1158:     document.location.hash='$anchorname';
                   1159: </script>
                   1160: ENDSCRIPT
                   1161:     }
                   1162:     return $result;
                   1163: }
                   1164: 
1.79      www      1165: sub storefile {
                   1166:     my ($file,$contents)=@_;
                   1167:     if (my $fh=Apache::File->new('>'.$file)) {
                   1168: 	print $fh $contents;
                   1169:         $fh->close();
1.147     albertel 1170:     } else {
                   1171:       &warning("Unable to save file $file");
1.79      www      1172:     }
                   1173: }
                   1174: 
1.151     albertel 1175: sub createnewhtml {
                   1176:   my $filecontents=(<<SIMPLECONTENT);
1.78      www      1177: <html>
                   1178: <head>
                   1179: <title>
                   1180:                            Title of Document Goes Here
                   1181: </title>
                   1182: </head>
                   1183: <body bgcolor="#FFFFFF">
                   1184: 
                   1185:                            Body of Document Goes Here
                   1186: 
                   1187: </body>
                   1188: </html>
                   1189: SIMPLECONTENT
1.151     albertel 1190:   return $filecontents;
                   1191: }
                   1192: 
1.147     albertel 1193: 
1.151     albertel 1194: sub inserteditinfo {
                   1195:       my ($result,$filecontents)=@_;
1.157     albertel 1196:       $filecontents = &HTML::Entities::encode($filecontents);
1.147     albertel 1197: #      my $editheader='<a href="#editsection">Edit below</a><hr />';
1.161     albertel 1198:       my $buttons=(<<BUTTONS);
                   1199: <input type="submit" name="attemptclean" 
                   1200:        value="Save and then attempt to clean HTML" />
                   1201: <input type="submit" name="savethisfile" value="Save this" />
                   1202: <input type="submit" name="viewmode" value="View" />
                   1203: BUTTONS
1.78      www      1204:       my $editfooter=(<<ENDFOOTER);
                   1205: <hr />
                   1206: <a name="editsection" />
                   1207: <form method="post">
1.161     albertel 1208: <input type="hidden" name="editmode" value="Edit" />
1.170     www      1209: $buttons<br />
1.78      www      1210: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
1.170     www      1211: <br />$buttons
1.78      www      1212: <br />
                   1213: </form>
                   1214: ENDFOOTER
1.147     albertel 1215: #      $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1.78      www      1216:       $result=~s/(\<\/body\>)/$editfooter/is;
                   1217:       return $result;
                   1218: }
                   1219: 
1.152     albertel 1220: sub get_target {
                   1221:   my $viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                   1222:   if ( $ENV{'request.state'} eq 'published') {
                   1223:     if ( defined($ENV{'form.grade_target'})
                   1224: 	 && ($viewgrades == 'F' )) {
                   1225:       return ($ENV{'form.grade_target'});
1.153     albertel 1226:     } elsif (defined($ENV{'form.grade_target'})) {
                   1227:       if (($ENV{'form.grade_target'} eq 'web') ||
                   1228: 	  ($ENV{'form.grade_target'} eq 'tex') ) {
                   1229: 	return $ENV{'form.grade_target'}
                   1230:       } else {
                   1231: 	return 'web';
                   1232:       }
1.152     albertel 1233:     } else {
                   1234:       return 'web';
                   1235:     }
                   1236:   } elsif ($ENV{'request.state'} eq 'construct') {
                   1237:     if ( defined($ENV{'form.grade_target'})) {
                   1238:       return ($ENV{'form.grade_target'});
                   1239:     } else {
                   1240:       return 'web';
                   1241:     }
                   1242:   } else {
                   1243:     return 'web';
                   1244:   }
                   1245: }
                   1246: 
1.24      sakharuk 1247: sub handler {
                   1248:   my $request=shift;
1.68      www      1249: 
1.152     albertel 1250:   my $target=&get_target();
1.68      www      1251: 
1.65      albertel 1252:   $Apache::lonxml::debug=0;
1.68      www      1253: 
1.25      sakharuk 1254:   if ($ENV{'browser.mathml'}) {
1.27      albertel 1255:     $request->content_type('text/xml');
                   1256:   } else {
                   1257:     $request->content_type('text/html');
1.25      sakharuk 1258:   }
1.141     albertel 1259:   &Apache::loncommon::no_cache($request);
1.27      albertel 1260:   $request->send_http_header;
1.141     albertel 1261: 
1.45      www      1262:   return OK if $request->header_only;
1.27      albertel 1263: 
1.79      www      1264: 
                   1265:   my $file=&Apache::lonnet::filelocation("",$request->uri);
1.78      www      1266: #
                   1267: # Edit action? Save file.
                   1268: #
                   1269:   unless ($ENV{'request.state'} eq 'published') {
1.107     www      1270:       if (($ENV{'form.savethisfile'}) || ($ENV{'form.attemptclean'})) {
1.79      www      1271: 	  &storefile($file,$ENV{'form.filecont'});
1.78      www      1272:       }
                   1273:   }
1.24      sakharuk 1274:   my %mystyle;
1.147     albertel 1275:   my $result = '';
1.50      albertel 1276:   my $filecontents=&Apache::lonnet::getfile($file);
                   1277:   if ($filecontents == -1) {
1.78      www      1278:     $result=(<<ENDNOTFOUND);
                   1279: <html>
                   1280: <head>
                   1281: <title>File not found</title>
                   1282: </head>
                   1283: <body bgcolor="#FFFFFF">
                   1284: <b>File not found: $file</b>
                   1285: </body>
                   1286: </html>
                   1287: ENDNOTFOUND
1.50      albertel 1288:     $filecontents='';
1.151     albertel 1289:     if ($ENV{'request.state'} ne 'published') {
                   1290:       $filecontents=&createnewhtml();
1.161     albertel 1291:       $ENV{'form.editmode'}='Edit'; #force edit mode
1.151     albertel 1292:     }
1.50      albertel 1293:   } else {
1.147     albertel 1294:     unless ($ENV{'request.state'} eq 'published') {
                   1295:       if ($ENV{'form.attemptclean'}) {
                   1296: 	$filecontents=&htmlclean($filecontents,1);
1.107     www      1297:       }
1.147     albertel 1298:     }
1.161     albertel 1299:     if (!$ENV{'form.editmode'} || $ENV{'form.viewmode'}) {
1.172     albertel 1300:       $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
                   1301: 					  '',%mystyle);
1.147     albertel 1302:     }
1.78      www      1303:   }
                   1304: 
                   1305: #
                   1306: # Edit action? Insert editing commands
                   1307: #
                   1308:   unless ($ENV{'request.state'} eq 'published') {
1.161     albertel 1309:     if ($ENV{'form.editmode'} && (!($ENV{'form.viewmode'}))) {
1.177     www      1310: 	my $displayfile=$request->uri;
                   1311:         $displayfile=~s/^\/[^\/]*//;
                   1312:       $result='<html><body bgcolor="#FFFFFF"><h3>'.$displayfile.
                   1313:               '</h3></body></html>';
1.78      www      1314:       $result=&inserteditinfo($result,$filecontents);
1.147     albertel 1315:     }
1.66      www      1316:   }
1.147     albertel 1317: 
1.126     www      1318:   writeallows($request->uri);
1.50      albertel 1319: 
1.67      www      1320:   $request->print($result);
1.64      albertel 1321: 
1.45      www      1322:   return OK;
1.24      sakharuk 1323: }
1.147     albertel 1324: 
1.22      albertel 1325: sub debug {
                   1326:   if ($Apache::lonxml::debug eq 1) {
1.146     albertel 1327:     $|=1;
1.180     albertel 1328:     print("DEBUG:".&HTML::Entities::encode($_[0])."<br />\n");
1.22      albertel 1329:   }
                   1330: }
1.49      albertel 1331: 
1.22      albertel 1332: sub error {
1.74      albertel 1333:   if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1.166     matthew  1334:     # If printing in construction space, put the error inside <pre></pre>
1.167     albertel 1335:     print "<b>ERROR:</b>".join("\n",@_)."\n";
1.52      albertel 1336:   } else {
                   1337:     print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
                   1338:     #notify author
1.146     albertel 1339:     &Apache::lonmsg::author_res_msg($ENV{'request.filename'},join('<br />',@_));
1.52      albertel 1340:     #notify course
                   1341:     if ( $ENV{'request.course.id'} ) {
                   1342:       my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
1.143     www      1343:       my $declutter=&Apache::lonnet::declutter($ENV{'request.filename'});
1.52      albertel 1344:       foreach my $user (split /\,/, $users) {
                   1345: 	($user,my $domain) = split /:/, $user;
1.143     www      1346: 	&Apache::lonmsg::user_normal_msg($user,$domain,
1.146     albertel 1347:         "Error [$declutter]",join('<br />',@_));
1.52      albertel 1348:       }
                   1349:     }
1.74      albertel 1350: 
1.52      albertel 1351:     #FIXME probably shouldn't have me get everything forever.
1.146     albertel 1352:     &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",join('<br />',@_));
1.74      albertel 1353:     #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1.52      albertel 1354:   }
1.22      albertel 1355: }
1.49      albertel 1356: 
1.22      albertel 1357: sub warning {
1.73      harris41 1358:   if ($ENV{'request.state'} eq 'construct') {
1.146     albertel 1359:     print "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n";
1.73      harris41 1360:   }
1.83      albertel 1361: }
                   1362: 
                   1363: sub get_param {
                   1364:   my ($param,$parstack,$safeeval,$context) = @_;
                   1365:   if ( ! $context ) { $context = -1; }
                   1366:   my $args ='';
                   1367:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.157     albertel 1368:   if ( ! $args ) { return undef; }
1.131     albertel 1369:   if ( $args =~ /my \$$param=\"/ ) {
                   1370:     return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
                   1371:   } else {
                   1372:     return undef;
                   1373:   }
1.22      albertel 1374: }
                   1375: 
1.132     albertel 1376: sub get_param_var {
                   1377:   my ($param,$parstack,$safeeval,$context) = @_;
                   1378:   if ( ! $context ) { $context = -1; }
                   1379:   my $args ='';
                   1380:   if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
                   1381:   if ( $args !~ /my \$$param=\"/ ) { return undef; }
                   1382:   my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
                   1383:   if ($value =~ /^[\$\@\%]/) {
                   1384:     return &Apache::run::run("return $value",$safeeval,1);
                   1385:   } else {
                   1386:     return $value;
                   1387:   }
                   1388: }
                   1389: 
1.74      albertel 1390: sub register_insert {
1.75      albertel 1391:   my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74      albertel 1392:   my $i;
1.76      albertel 1393:   my $tagnum=0;
1.74      albertel 1394:   my @order;
                   1395:   for ($i=0;$i < $#data; $i++) {
                   1396:     my $line = $data[$i];
                   1397:     if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
                   1398:     if ( $line =~ /TABLE/ ) { last; }
1.92      albertel 1399:     my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1.135     albertel 1400:     if ($tag) {
                   1401:       $insertlist{"$tagnum.tag"} = $tag;
                   1402:       $insertlist{"$tagnum.description"} = $descrip;
                   1403:       $insertlist{"$tagnum.color"} = $color;
                   1404:       $insertlist{"$tagnum.function"} = $function;
                   1405:       if (!defined($show)) { $show='yes'; }
                   1406:       $insertlist{"$tagnum.show"}= $show;
                   1407:       $insertlist{"$tag.num"}=$tagnum;
                   1408:       $tagnum++;
                   1409:     }
1.74      albertel 1410:   }
1.76      albertel 1411:   $i++; #skipping TABLE line
                   1412:   $tagnum = 0;
1.74      albertel 1413:   for (;$i < $#data;$i++) {
                   1414:     my $line = $data[$i];
1.76      albertel 1415:     my ($mnemonic,@which) = split(/ +/,$line);
                   1416:     my $tag = $insertlist{"$tagnum.tag"};
1.144     matthew  1417:     for (my $j=0;$j <=$#which;$j++) {
1.74      albertel 1418:       if ( $which[$j] eq 'Y' ) {
1.76      albertel 1419: 	if ($insertlist{"$j.show"} ne 'no') {
                   1420: 	  push(@{ $insertlist{"$tag.which"} },$j);
                   1421: 	}
1.74      albertel 1422:       }
                   1423:     }
1.76      albertel 1424:     $tagnum++;
1.74      albertel 1425:   }
                   1426: }
1.98      albertel 1427: 
                   1428: sub description {
                   1429:   my ($token)=@_;
1.138     albertel 1430:   my $tagnum;
                   1431:   my $tag=$token->[1];
                   1432:   foreach my $namespace (reverse @Apache::lonxml::namespace) {
                   1433:     my $testtag=$namespace.'::'.$tag;
                   1434:     $tagnum=$insertlist{"$testtag.num"};
                   1435:     if (defined($tagnum)) { last; }
                   1436:   }
                   1437:   if (!defined ($tagnum)) { $tagnum=$Apache::lonxml::insertlist{"$tag.num"}; }
                   1438:   return $insertlist{$tagnum.'.description'};
1.98      albertel 1439: }
1.123     albertel 1440: 
                   1441: # ----------------------------------------------------------------- whichuser
                   1442: # returns a list of $symb, $courseid, $domain, $name that is correct for
                   1443: # calls to lonnet functions for this setup.
                   1444: # - looks for form.grade_ parameters
                   1445: sub whichuser {
1.134     albertel 1446:   my ($symb,$courseid,$domain,$name);
1.123     albertel 1447:   if (defined($ENV{'form.grade_symb'})) {
                   1448:     my $tmp_courseid=$ENV{'form.grade_courseid'};
                   1449:     my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
                   1450:     if ($allowed) {
                   1451:       $symb=$ENV{'form.grade_symb'};
                   1452:       $courseid=$ENV{'form.grade_courseid'};
                   1453:       $domain=$ENV{'form.grade_domain'};
                   1454:       $name=$ENV{'form.grade_username'};
                   1455:     }
1.134     albertel 1456:   } else {
                   1457:     $symb=&Apache::lonnet::symbread();
                   1458:     $courseid=$ENV{'request.course.id'};
                   1459:     $domain=$ENV{'user.domain'};
                   1460:     $name=$ENV{'user.name'};
1.123     albertel 1461:   }
                   1462:   return ($symb,$courseid,$domain,$name);
                   1463: }
                   1464: 
1.1       sakharuk 1465: 1;
                   1466: __END__
1.68      www      1467: 
                   1468: 

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