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

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

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