File:
[LON-CAPA] /
loncom /
homework /
structuretags.pm
Revision
1.167:
download - view:
text,
annotated -
select for diffs
Sat May 3 20:35:43 2003 UTC (21 years, 3 months ago) by
www
Branches:
MAIN
CVS tags:
HEAD
<languageblock>-tag for multilingual resources, needed for PGL this summer.
Works like this:
This is always shown.
<languageblock exclude="german,swiss,french">
Do you speak English?
</languageblock>
<languageblock include="german,swiss">
Sprechen Sie Deutsch?
</languageblock>
<languageblock include="french,swiss">
Parlez-vous francais?
</languageblock>
Gets language from user environment ("environment.languages") or
from the query string
"foo.html?displaylanguage="swiss"
via function loncommon::display_language
Still need to define "vocabulary" for languages ("displaylanguage=german" or
"displaylanguage=deutsch" ???), include in languages.tab,
but make independent of character set used.
1: # The LearningOnline Network with CAPA
2: # definition of tags that give a structure to a document
3: #
4: # $Id: structuretags.pm,v 1.167 2003/05/03 20:35:43 www Exp $
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: # 2/19 Guy
29: # 6/26/2001 fixed extra web display at end of <web></web> tags
30: # 8/17,8/18,8/20 Gerd Kortemeyer
31:
32:
33: package Apache::structuretags;
34:
35: use strict;
36: use Apache::lonnet;
37: use Apache::File();
38: use Apache::lonmenu;
39:
40: BEGIN {
41: &Apache::lonxml::register('Apache::structuretags',('block','languageblock','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext'));
42: }
43:
44: sub start_web {
45: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
46: my $bodytext=&Apache::lonxml::get_all_text("/web",$parser);
47: if ($target eq 'web') {
48: return $bodytext;
49: }
50: return '';
51: }
52:
53: sub end_web {
54: return '';
55: }
56:
57: sub start_tex {
58: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
59: my $bodytext=&Apache::lonxml::get_all_text("/tex",$parser);
60: if ($target eq 'tex') {
61: return $bodytext.' ';
62: }
63: return '';
64: }
65:
66: sub end_tex {
67: return '';
68: }
69:
70: sub page_start {
71: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
72: my %found;
73: foreach my $taginside ($tagstack) {
74: foreach my $taglookedfor ('html','body','form') {
75: if ($taginside =~ /^$taglookedfor$/i) {$found{$taglookedfor} = 1;}
76: }
77: }
78:
79: my $result;
80: my $head_tag_start;
81: if (!defined($found{'html'})) {
82: $result=&Apache::londefdef::start_html($target,$token,$tagstack,
83: $parstack,$parser,$safeeval);
84: $head_tag_start='<head>'.&Apache::lonmenu::registerurl(undef,$target);
85: }
86: my $body_tag_start;
87: if (!defined($found{'body'})) {
88: $body_tag_start='<body onLoad="'.&Apache::lonmenu::loadevents().'" '.
89: 'onUnload="'.&Apache::lonmenu::unloadevents().'" ';
90: my $background=&Apache::lonxml::get_param('background',$parstack,
91: $safeeval);
92: if ($background) {
93: $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
94: $background;
95: $body_tag_start.='background="'.$background.'" ';
96: } else {
97: my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,
98: $safeeval);
99: if ($bgcolor) {
100: $body_tag_start.='bgcolor="'.$bgcolor.'" ';
101: } else {
102: $body_tag_start.='bgcolor="#ffffff"';
103: }
104: }
105: $body_tag_start.='>'.&Apache::lonmenu::menubuttons(undef,$target,1);
106: if ($target eq 'web' && $ENV{'request.state'} ne 'construct') {
107: my ($symb,undef,undef,undef,$publicuser)=
108: &Apache::lonxml::whichuser();
109: if ($symb eq '' && !$publicuser) {
110: my $help = &Apache::loncommon::help_open_topic("Ambiguous_Reference");
111: $help="Browsing resource, all submissions are temporary.<br />";
112: $body_tag_start.=$help;
113: }
114: }
115: }
116: my $form_tag_start;
117: if (!defined($found{'form'})) {
118: $form_tag_start='<form name="lonhomework" method="POST" action="'.
119: $ENV{'request.uri'}.'">';
120: }
121: return ($result,$head_tag_start,$body_tag_start,$form_tag_start);
122: }
123:
124: #use Time::HiRes();
125: sub get_resource_name {
126: my ($parstack,$safeeval)=@_;
127: my $name=&Apache::lonnet::gettitle();
128: if ($name eq '') {
129: $name=&Apache::lonnet::EXT('resource.title');
130: if ($name eq 'con_lost') { $name = ''; }
131: }
132: $Apache::lonhomework::name=$name;
133: return $name;
134: }
135:
136: sub setup_rndseed {
137: my ($safeeval)=@_;
138: my $rndseed;
139: my ($symb)=&Apache::lonxml::whichuser();
140: if ($ENV{'request.state'} eq "construct" || $symb eq '') {
141: $rndseed=$ENV{'form.rndseed'};
142: if (!$rndseed) {
143: $rndseed=$Apache::lonhomework::history{'rndseed'};
144: if (!$rndseed) {
145: $rndseed=time;
146: $ENV{'form.rndseed'}=$rndseed;
147: }
148: }
149: if ($ENV{'form.resetdata'} eq 'New Problem Variation') {
150: $rndseed=time;
151: $ENV{'form.rndseed'}=$rndseed;
152: }
153: &Apache::lonxml::debug("Setting rndseed to $rndseed");
154: &Apache::run::run('$external::randomseed='.$rndseed.';',$safeeval);
155: }
156: return $rndseed;
157: }
158:
159: sub problem_edit_header {
160: return '<input type="hidden" name="submitted" value="edit" />
161: <input type="hidden" name="problemmode" value="Edit" />
162: <input type="submit" name="problemmode" value="Discard Edits and View" />
163: <input type="submit" name="problemmode" value="EditXML" />
164: <input type="submit" name="Undo" value="undo" /> <hr />
165: <input type="submit" name="submit" value="Submit Changes and Edit" />
166: <input type="submit" name="submit" value="Submit Changes and View" /><br /><p> </p><table border="0"><tr><td bgcolor="#DDDDDD">
167: ';
168: }
169:
170: sub problem_edit_footer {
171: return '</td></tr></table><br /><input type="submit" name="submit" value="Submit Changes and Edit" />
172: <input type="submit" name="submit" value="Submit Changes and View" />';
173: }
174:
175: sub problem_web_to_edit_header {
176: my ($rndseed)=@_;
177: my $result.='<input type="hidden" name="problemmode" value="View" />
178: <input type="submit" name="problemmode" value="Edit" />
179: <input type="submit" name="problemmode" value="EditXML" />
180: Random Seed:<input type="text" name="rndseed" width="10" value="'.
181: $rndseed.'" />
182: <input type="submit" name="changerandseed" value="Change" />
183: <input type="submit" name="resetdata" value="Reset Submissions" />
184: <input type="checkbox" name="showallfoils" ';
185: if (defined($ENV{'form.showallfoils'})) { $result.='checked="on"'; }
186: $result.= ' /> Show All Foils
187: <hr />';
188: my $numtoanalyze=$ENV{'form.numtoanalyze'};
189: if (!$numtoanalyze) { $numtoanalyze=100; }
190: $result.= '<input type="submit" name="problemmode" value="Answer Distribution" />
191: <input type="text" name="numtoanalyze" value="'.
192: $numtoanalyze.'" size="5" /> <hr />';
193: return $result;
194: }
195:
196: sub initialize_storage {
197: %Apache::lonhomework::results=();
198: %Apache::lonhomework::history=();
199: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
200: if ($ENV{'request.state'} eq 'construct' || $symb eq '') {
201: %Apache::lonhomework::history=
202: &Apache::lonnet::tmprestore($ENV{'request.uri'},'',$domain,$name);
203: my ($temp)=keys %Apache::lonhomework::history ;
204: &Apache::lonxml::debug("Return message of $temp");
205: } else {
206: %Apache::lonhomework::history=
207: &Apache::lonnet::restore($symb,$courseid,$domain,$name);
208: }
209: #ignore error conditions
210: my ($temp)=keys %Apache::lonhomework::history ;
211: if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
212: }
213:
214: # -------------------------------------------------------------finalize_storage
215: # Stores away the result has to a student's environment
216: # checks form.grade_ for specific values, other wises stores
217: # to the running users environment
218: sub finalize_storage {
219: my $result;
220: my ($temp) = keys %Apache::lonhomework::results;
221: if ( $temp ne '' ) {
222: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
223: if ($ENV{'request.state'} eq 'construct' || $symb eq '') {
224: $Apache::lonhomework::results{'rndseed'}=$ENV{'form.rndseed'};
225: $result=&Apache::lonnet::tmpstore(\%Apache::lonhomework::results,
226: $ENV{'request.uri'},'',$domain,$name);
227: &Apache::lonxml::debug('Construct Store return message:'.$result);
228: } else {
229: $result=&Apache::lonnet::cstore(\%Apache::lonhomework::results,
230: $symb,$courseid,$domain,$name);
231: &Apache::lonxml::debug('Store return message:'.$result);
232: }
233: }
234: return $result;
235: }
236:
237: sub checkout_msg {
238: return (<<ENDCHECKOUT);
239: <h2>The resource needs to be checked out</h2>
240: As a resource gets checked out, a unique timestamped ID is given to it, and a
241: permanent record is left in the system.<p />
242: <font color=red>
243: Checking out resources is subject to course policies, and may exclude future
244: credit even if done erroneously.<p />
245: </font>
246: <form name="checkout" method="POST" action="$ENV{'request.uri'}">
247: <input type="hidden" name="doescheckout" value="yes" />
248: <input type="button" name="checkoutbutton" value="Check out Exam for Viewing" onClick="javascript:if (confirm('Check out Exam?')) { document.checkout.submit(); }" />
249: </form>
250: ENDCHECKOUT
251: }
252:
253: sub start_problem {
254: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
255:
256: $Apache::lonhomework::parsing_a_problem=1;
257: # meta is called from lonpublisher, which doesn't uses the normal
258: # lonhomework method of parsing the file which means that inputtags
259: # won't get reset
260: if ( $Apache::inputtags::part ne '' && $target != 'meta' ) {
261: &Apache::lonxml::error('Only one problem allowed in a .problem file');
262: my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
263: return '';
264: }
265: #initialize globals
266: $Apache::inputtags::part='0';
267: @Apache::inputtags::responselist = ();
268: @Apache::inputtags::previous=();
269: @Apache::inputtags::previous_version=();
270: $Apache::structuretags::printanswer='No';
271: @Apache::structuretags::whileconds=();
272: @Apache::structuretags::whilebody=();
273: @Apache::structuretags::whileline=();
274: $Apache::lonhomework::scantronmode=0;
275:
276: if (defined($ENV{'scantron.maxquest'})) {
277: $Apache::lonhomework::scantronmode=1;
278: }
279:
280: if ($target ne 'analyze') {
281: &initialize_storage();
282: if ($target eq 'web') {
283: &Apache::lonhomework::showhash(%Apache::lonhomework::history);
284: }
285: $Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
286: &Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
287: }
288: if ($Apache::lonhomework::type eq '' ) {
289: my $uri=$ENV{'request.uri'};
290: if ($uri=~/\.(\w+)$/) {
291: $Apache::lonhomework::type=$1;
292: &Apache::lonxml::debug("Using type of $1");
293: } else {
294: $Apache::lonhomework::type='problem';
295: &Apache::lonxml::debug("Using default type, problem, :$uri:");
296: }
297: }
298:
299: #added vars to the scripting enviroment
300: my $expression='$external::part='.$Apache::inputtags::part.';';
301: &Apache::run::run($expression,$safeeval);
302: my $status;
303: my $accessmsg;
304:
305: #should get back a <html> or the neccesary stuff to start XML/MathML
306: my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
307: &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
308: if ($target eq 'tex' and $ENV{'request.symb'} =~ m/\.page_/) {$result='';}
309:
310: if ($target eq 'analyze') { my $rndseed=&setup_rndseed($safeeval); }
311: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
312: $target eq 'tex') {
313: #handle exam checkout
314: if ($Apache::lonhomework::type eq 'exam') {
315: my $token=
316: $Apache::lonhomework::history{"resource.0.outtoken"};
317: if (($ENV{'form.doescheckout'}) && (!$token)) {
318: $token=&Apache::lonxml::maketoken();
319: $Apache::lonhomework::history{"resource.0.outtoken"}=
320: $token;
321: }
322: $body_tag_start.=&Apache::lonxml::printtokenheader($target,$token);
323: }
324:
325: #handle rand seed in construction space
326: my $rndseed=&setup_rndseed($safeeval);
327: my ($symb)=&Apache::lonxml::whichuser();
328: if ($ENV{'request.state'} ne "construct" && $symb eq '') {
329: $form_tag_start.='<input type="hidden" name="rndseed" value="'.
330: $rndseed.'" />'.
331: '<input type="submit" name="resetdata"
332: value="New Problem Variation" />'.
333: '<input type="hidden" name="username"
334: value="'.$ENV{'form.username'}.'" />';
335: }
336: ($status,$accessmsg) = &Apache::lonhomework::check_access('0');
337: push (@Apache::inputtags::status,$status);
338: my $expression='$external::datestatus="'.$status.'";';
339: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
340: &Apache::run::run($expression,$safeeval);
341: &Apache::lonxml::debug("Got $status");
342: if (( $status eq 'CLOSED' ) ||
343: ( $status eq 'UNCHECKEDOUT') ||
344: ( $status eq 'BANNED') ||
345: ( $status eq 'UNAVAILABLE')) {
346: my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
347: if ( $target eq "web" ) {
348: $result.= $head_tag_start.'</head>';
349: my $msg=$body_tag_start;
350: if ($status eq 'UNAVAILABLE') {
351: $result.='<h1>Unable to determine if this resource is open due to network problems. Please try again later.</h1>';
352: } else {
353: $result.='<h1>Not open to be viewed</h1>';
354: }
355: if ($status eq 'CLOSED') {
356: $msg.='The problem '.$accessmsg;
357: } elsif ($status eq 'UNCHECKEDOUT') {
358: $msg.=&checkout_msg;
359: }
360: $result.=$msg.'<br />';
361: } elsif ($target eq 'tex') {
362: $result.='\begin{document}\noindent \vskip 1 mm \begin{minipage}{\textwidth}\vskip 0 mm';
363: if ($status eq 'UNAVAILABLE') {
364: $result.='Unable to determine if this resource is open due to network problems. Please try again later.\vskip 0 mm ';
365: } else {
366: $result.="Problem is not open to be viewed. It $accessmsg \\vskip 0 mm ";
367: }
368: }
369: } elsif ($target eq 'web') {
370: my $name= &get_resource_name($parstack,$safeeval);
371: if ($status eq 'CAN_ANSWER') {
372: # create a page header and exit
373: $result.="$head_tag_start<title>$name</title></head>
374: $body_tag_start \n $form_tag_start".
375: '<input type="hidden" name="submitted" value="yes" />';
376: if ($ENV{'request.state'} eq "construct") {
377: $result.= &problem_web_to_edit_header($rndseed);
378: }
379: # if we are viewing someone else preserve that info
380: if (defined $ENV{'form.grade_symb'}) {
381: foreach my $field ('symb','courseid','domain','username') {
382: $result .= '<input type="hidden" name="grade_'.$field.
383: '" value="'.$ENV{"form.grade_$field"}.'" />'."\n";
384: }
385: }
386: } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER'
387: || $status eq 'CLOSED' || $status eq 'UNAVALAILABLE') {
388: $result.=$head_tag_start.
389: "<title>$name</title></head>\n$body_tag_start\n";
390: }
391: } elsif ($target eq 'tex') {
392: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
393: if ($name eq '') {
394: $name=&Apache::lonnet::EXT('resource.title');
395: if ($name eq 'con_lost') { $name = ''; }
396: }
397: $Apache::lonhomework::name=$name;
398: my $id = $Apache::inputtags::part;
399: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
400: my $allkeys=&Apache::lonnet::metadata($ENV{'request.uri'},'keys');
401: my @allkeys = split /,/,$allkeys;
402: my $allow_print_points = 0;
403: foreach my $partial_key (@allkeys) {
404: if ($partial_key=~m/weight/) {
405: $allow_print_points++;
406: }
407: }
408: my $duedate = &Apache::lonnet::EXT("resource.$id.duedate");
409: $duedate = POSIX::strftime("%c",localtime($duedate));
410: my $temp_file;
411: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.due";
412: if (-e $filename) {
413: $temp_file = Apache::File->new($filename);
414: } else {
415: $temp_file = Apache::File->new('>>'.$filename);
416: }
417: my @due_file_content = <$temp_file>;
418: my $due_file_content = $due_file_content[$#due_file_content];
419: chomp $due_file_content;
420: my $name_of_resourse= &get_resource_name($parstack,$safeeval);
421: if ($due_file_content ne $duedate) {
422: $temp_file = Apache::File->new('>'.$filename);
423: print $temp_file "$duedate\n";
424: if (not $ENV{'request.symb'} =~ m/\.page_/) {
425: if(not $duedate=~m/1969/ and $Apache::lonhomework::type ne 'exam') {
426: $result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent\textit{Due date: '.$duedate.'} \vskip 1 mm\noindent \begin{minipage}{\textwidth}';
427: } else {
428: $result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent \vskip 1 mm \noindent\begin{minipage}{\textwidth}';
429: if ($Apache::lonhomework::type eq 'exam' and $allow_print_points==1) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
430: }
431: } else {
432: $result .= '\vskip 1mm\textit{Due date: '.$duedate.'} \\\\\\\\';
433: }
434: } else {
435: if (not $ENV{'request.symb'} =~ m/\.page_/) {
436: $result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent \vskip 1 mm\noindent\begin{minipage}{\textwidth}';
437: if (($Apache::lonhomework::type eq 'exam') and ($allow_print_points==1)) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
438: } else {
439: $result .= '\vskip 1mm \\\\\\\\';
440: }
441: }
442: }
443: } elsif ($target eq 'edit') {
444: $result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
445: &problem_edit_header();
446: my $temp=&Apache::edit::insertlist($target,$token);
447: $result.=$temp;
448: } elsif ($target eq 'modified') {
449: $result=$token->[4];
450: $result.=&Apache::edit::handle_insert();
451: } else {
452: # page_start returned a starting result, delete it if we don't need it
453: $result = '';
454: }
455: return $result;
456: }
457:
458: sub end_problem {
459: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
460: my $result='';
461: my $status=$Apache::inputtags::status['-1'];
462: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
463: $target eq 'tex') {
464: if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
465: $status eq 'CAN_ANSWER' ) {
466: # if part is zero, no <part>s existed, so we need to the grading
467: &Apache::inputtags::grade;
468: } elsif ( ($target eq 'web' || $target eq 'tex') &&
469: $Apache::inputtags::part eq '0' &&
470: $status ne 'UNCHECKEDOUT') {
471: # if part is zero, no <part>s existed, so we need show the current
472: # grading status
473: my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
474: $result.= $gradestatus;
475: }
476: if (
477: (($target eq 'web') && ($ENV{'request.state'} ne 'construct')) ||
478: ($target eq 'answer') || ($target eq 'tex')
479: ) {
480: if ($status eq 'CAN_ANSWER') {
481: if ($target ne 'tex') {
482: $result.="</form></body>\n";
483: }
484: } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' ||
485: $status eq 'UNCHECKEDOUT' ) {
486: if ($target ne 'tex') {
487: $result.="</body>\n";
488: }
489: }
490: if ($target eq 'web') {
491: $result.=&Apache::lonxml::xmlend();
492: } elsif ($target eq 'tex') {
493: $result .= '\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
494: if (not $ENV{'request.symb'} =~ m/\.page_/) {
495: $result .= '\end{minipage}\end{document} ';
496: } else {
497: $result .= '';
498: }
499: }
500: }
501: if ($target eq 'grade') {
502: &Apache::lonhomework::showhash(%Apache::lonhomework::results);
503: &finalize_storage();
504: }
505: if ($target eq 'answer' && ($ENV{'request.state'} eq 'construct') ) {
506: $result.='</html>'; #normally we get it from xmlend, but in CSTR
507: # we always show answer mode too.
508: }
509: } elsif ($target eq 'meta') {
510: if ($Apache::inputtags::part eq '0') {
511: $result=&Apache::response::mandatory_part_meta;
512: }
513: } elsif ($target eq 'edit') {
514: &Apache::lonxml::debug("in end_problem with $target, edit");
515: $result = &problem_edit_footer();
516: }
517:
518: undef(%Apache::lonhomework::history);
519: undef(%Apache::lonhomework::results);
520: undef($Apache::inputtags::part);
521: undef($Apache::lonhomework::parsing_a_problem);
522:
523: return $result;
524: }
525:
526:
527: sub start_library {
528: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
529: my ($result,$head_tag_start,$body_tag_start,$form_tag_start);
530:
531: if ($target eq 'edit') {
532: ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
533: &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
534: $result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
535: &problem_edit_header();
536: my $temp=&Apache::edit::insertlist($target,$token);
537: $result.=$temp;
538: } elsif ($target eq 'modified') {
539: $result=$token->[4];
540: $result.=&Apache::edit::handle_insert();
541: } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
542: $ENV{'request.state'} eq "construct" ) {
543: ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
544: &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
545: my $name=&get_resource_name($parstack,$safeeval);
546: my $rndseed=&setup_rndseed($safeeval);
547: $result.="$head_tag_start<title>$name</title></head>
548: $body_tag_start \n $form_tag_start".
549: '<input type="hidden" name="submitted" value="yes" />';
550: $result.=&problem_web_to_edit_header($rndseed);
551: }
552: return $result;
553: }
554:
555: sub end_library {
556: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
557: my $result='';
558: if ($target eq 'edit') {
559: $result=&problem_edit_footer();
560: } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
561: $ENV{'request.state'} eq "construct") {
562: $result.='</form></body>'.&Apache::lonxml::xmlend();
563: }
564: return $result;
565: }
566:
567: sub start_block {
568: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
569:
570: my $result;
571:
572: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
573: $target eq 'tex' || $target eq 'analyze') {
574: my $code = $token->[2]->{'condition'};
575: if ($code) {
576: if (!$Apache::lonxml::default_homework_loaded) {
577: &Apache::lonxml::default_homework_load($safeeval);
578: }
579: $result = &Apache::run::run($code,$safeeval);
580: &Apache::lonxml::debug("block :$code: returned :$result:");
581: } else {
582: $result='1';
583: }
584: if ( ! $result ) {
585: my $skip=&Apache::lonxml::get_all_text("/block",$parser);
586: &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
587: }
588: $result='';
589: } elsif ($target eq 'edit') {
590: $result .=&Apache::edit::tag_start($target,$token);
591: $result .=&Apache::edit::text_arg('Test Condition:','condition',
592: $token,40);
593: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
594: } elsif ($target eq 'modified') {
595: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
596: $safeeval,'condition');
597: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
598: }
599: return $result;
600: }
601:
602: sub end_block {
603: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
604: my $result;
605: if ($target eq "edit") {
606: $result.= &Apache::edit::tag_end($target,$token,'');
607: }
608: return $result;
609: }
610:
611: sub start_languageblock {
612: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
613:
614: my $result;
615:
616: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
617: $target eq 'tex' || $target eq 'analyze') {
618: my $include = $token->[2]->{'include'};
619: my $exclude = $token->[2]->{'exclude'};
620: my %languages=&Apache::loncommon::display_languages();
621: $result='1';
622: if ($include) {
623: $result='';
624: foreach (split(/\,/,$include)) {
625: if ($languages{$_}) { $result='1'; }
626: }
627: }
628: if ($exclude) {
629: foreach (split(/\,/,$exclude)) {
630: if ($languages{$_}) { $result='0'; }
631: }
632: }
633: if ( ! $result ) {
634: my $skip=&Apache::lonxml::get_all_text("/languageblock",$parser);
635: &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
636: }
637: $result='';
638: } elsif ($target eq 'edit') {
639: $result .=&Apache::edit::tag_start($target,$token);
640: $result .=&Apache::edit::text_arg('Include Language:','include',
641: $token,40);
642: $result .=&Apache::edit::text_arg('Exclude Language:','exclude',
643: $token,40);
644: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
645: } elsif ($target eq 'modified') {
646: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
647: $safeeval,'include')
648: .&Apache::edit::get_new_args($token,$parstack,
649: $safeeval,'exclude');
650: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
651: }
652: return $result;
653: }
654:
655: sub end_languageblock {
656: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
657: my $result;
658: if ($target eq "edit") {
659: $result.= &Apache::edit::tag_end($target,$token,'');
660: }
661: return $result;
662: }
663:
664: sub start_while {
665: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
666:
667: my $result;
668: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
669: $target eq 'tex' || $target eq 'analyze') {
670: my $code = $token->[2]->{'condition'};
671:
672: push( @Apache::structuretags::whileconds, $code);
673: if (!$Apache::lonxml::default_homework_loaded) {
674: &Apache::lonxml::default_homework_load($safeeval);
675: }
676: my $result = &Apache::run::run($code,$safeeval);
677: my $bodytext=&Apache::lonxml::get_all_text("/while",$parser);
678: push( @Apache::structuretags::whilebody, $bodytext);
679: push( @Apache::structuretags::whileline, $token->[5]);
680: &Apache::lonxml::debug("s code $code got -$result-");
681: if ( $result ) {
682: &Apache::lonxml::newparser($parser,\$bodytext);
683: }
684: } elsif ($target eq 'edit') {
685: $result .=&Apache::edit::tag_start($target,$token);
686: $result .=&Apache::edit::text_arg('Test Condition:','condition',
687: $token,40);
688: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
689: } elsif ($target eq 'modified') {
690: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
691: $safeeval,'condition');
692: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
693: }
694: return $result;
695: }
696:
697: sub end_while {
698: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
699: my $result;
700:
701: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
702: $target eq 'tex' || $target eq 'analyze') {
703: my $code = pop(@Apache::structuretags::whileconds);
704: my $bodytext = pop(@Apache::structuretags::whilebody);
705: my $line = pop(@Apache::structuretags::whileline);
706: my $return = &Apache::run::run($code,$safeeval);
707: my $starttime=time;
708: my $error=0;
709: while ($return) {
710: if (time-$starttime >
711: $Apache::lonnet::perlvar{'lonScriptTimeout'}) {
712: $return = 0; $error=1; next;
713: }
714: $result.=&Apache::scripttag::xmlparse($bodytext);
715: $return = &Apache::run::run($code,$safeeval);
716: }
717: if ($error) {
718: &Apache::lonxml::error('<pre>Code ran too long. It ran for more than '.$Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds occured while running <while$gt; on line '.$line.'</pre>');
719: }
720: } elsif ($target eq "edit") {
721: $result.= &Apache::edit::tag_end($target,$token,'');
722: }
723: return $result;
724: }
725:
726: # <randomlist show="1">
727: # <tag1>..</tag1>
728: # <tag2>..</tag2>
729: # <tag3>..</tag3>
730: # ...
731: # </randomlist>
732: sub start_randomlist {
733: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
734: my $result;
735: if ($target eq 'answer' || $target eq 'grade' || $target eq 'web' ||
736: $target eq 'tex' || $target eq 'analyze') {
737: my $body= &Apache::lonxml::get_all_text("/randomlist",$parser);
738: my $b_parser= HTML::TokeParser->new(\$body);
739: my $b_tok;
740: my @randomlist;
741: my $list_item;
742: while($b_tok = $b_parser->get_token() ) {
743: if($b_tok->[0] eq 'S') { # start tag
744: # get content of the tag until matching end tag
745: # get all text upto the matching tag
746: # and push the content into @randomlist
747: $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],
748: $b_parser);
749: $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
750: push(@randomlist,$list_item);
751: # print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4],
752: # $list_item</b>";
753: }
754: if($b_tok->[0] eq 'T') { # text
755: # what to do with text in between tags?
756: # print "<b>TEXT $b_tok->[1]</b><br />";
757: }
758: # if($b_tok->[0] eq 'E') { # end tag, should not happen
759: # print "<b>END-TAG $b_tok->[1]</b><br />";
760: # }
761: }
762: my @idx_arr = (0 .. $#randomlist);
763: &Apache::structuretags::shuffle(\@idx_arr);
764: my $bodytext = '';
765: my $show=$#randomlist;
766: my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
767: $showarg--;
768: if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
769: for(0 .. $show) {
770: $bodytext .= "$randomlist[ $idx_arr[$_] ]";
771: }
772: &Apache::lonxml::newparser($parser,\$bodytext);
773: } elsif ($target eq 'edit' ) {
774: $result .=&Apache::edit::tag_start($target,$token);
775: $result .=&Apache::edit::text_arg('Maximum Tags to Show:','show',
776: $token,5);
777: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
778: } elsif ($target eq 'modified' ) {
779: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
780: $safeeval,'show');
781: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
782: }
783: return $result;
784: }
785:
786: sub shuffle {
787: my $a=shift;
788: my $i;
789: if (defined(@$a)) {
790: &Apache::response::setrandomnumber();
791: for($i=@$a;--$i;) {
792: my $j=int(&Math::Random::random_uniform() * ($i+1));
793: next if $i == $j;
794: @$a[$i,$j] = @$a[$j,$i];
795: }
796: }
797: }
798:
799: sub end_randomlist {
800: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
801: my $result;
802: if ($target eq 'edit' ) {
803: $result=&Apache::edit::tag_end($target,$token,
804: 'End Randomly Parsed Block');
805: }
806: return $result;
807: }
808:
809: sub start_part {
810: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
811: my $result='';
812: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
813: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
814: $Apache::inputtags::part=$id;
815: @Apache::inputtags::responselist = ();
816: @Apache::inputtags::previous=();
817: @Apache::inputtags::previous_version=();
818: my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
819:
820: if ($target eq 'meta') {
821: return &Apache::response::mandatory_part_meta;
822: } elsif ($target eq 'web' || $target eq 'grade' ||
823: $target eq 'answer' || $target eq 'tex') {
824: if ($hidden) {
825: my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
826: } else {
827: my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
828: push (@Apache::inputtags::status,$status);
829: my $expression='$external::datestatus="'.$status.'";';
830: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
831: &Apache::run::run($expression,$safeeval);
832: if ( $status eq 'CLOSED' ) {
833: my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
834: if ( $target eq "web" ) {
835: $result="<br />Part is not open to be viewed. It $accessmsg<br />";
836: } elsif ( $target eq 'tex' ) {
837: $result="\\end{minipage}\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\\\begin{minipage}{\\textwidth}";
838: }
839: } else {
840: if ($target eq 'tex') {
841: $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
842: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
843: if ($Apache::lonhomework::type eq 'exam') { $result .= '\fbox{\textit{'.$weight.' pt}}';}
844: }
845: }
846: }
847: } elsif ($target eq 'edit') {
848: $result.=&Apache::edit::tag_start($target,$token);
849: $result.=&Apache::edit::text_arg('Part ID:','id',$token).
850: &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
851: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
852: } elsif ($target eq 'modified') {
853: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
854: $safeeval,'id');
855: if ($constructtag) {
856: $result = &Apache::edit::rebuild_tag($token);
857: $result.=&Apache::edit::handle_insert();
858: }
859: }
860: return $result;
861: }
862:
863: sub end_part {
864: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
865: &Apache::lonxml::debug("in end_part $target ");
866: my $status=$Apache::inputtags::status['-1'];
867: my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
868: my $result='';
869: if ( $target eq 'meta' ) {
870: $result='';
871: } elsif ( $target eq 'grade' && $status eq 'CAN_ANSWER' && !$hidden) {
872: $result=&Apache::inputtags::grade;
873: } elsif (($target eq 'web' || $target eq 'tex') && !$hidden ) {
874: my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,
875: $target);
876: if ($Apache::lonhomework::type eq 'exam') {$gradestatus='';}
877: $result=$gradestatus;
878: }
879: pop @Apache::inputtags::status;
880: $Apache::inputtags::part='';
881: return $result;
882: }
883:
884: sub start_preduedate {
885: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
886: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
887: if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
888: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' &&
889: $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
890: &Apache::lonxml::get_all_text("/preduedate",$parser);
891: }
892: }
893: return '';
894: }
895:
896: sub end_preduedate {
897: return '';
898: }
899:
900: sub start_postanswerdate {
901: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
902: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
903: if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
904: &Apache::lonxml::get_all_text("/postanswerdate",$parser);
905: }
906: } elsif ($target eq 'tex') {
907: return '\vskip 0 mm \noindent';
908: }
909: return '';
910: }
911:
912: sub end_postanswerdate {
913: return '';
914: }
915:
916: sub start_notsolved {
917: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
918: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
919: $target eq 'tex') {
920: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
921: &Apache::lonxml::debug("not solved has :$gradestatus:");
922: if ($gradestatus =~ /^correct/) {
923: &Apache::lonxml::debug("skipping");
924: &Apache::lonxml::get_all_text("/notsolved",$parser);
925: }
926: }
927: return '';
928: }
929:
930: sub end_notsolved {
931: return '';
932: }
933:
934: sub start_solved {
935: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
936: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
937: $target eq 'tex') {
938: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
939: if ($gradestatus !~ /^correct/) {
940: &Apache::lonxml::get_all_text("/solved",$parser);
941: }
942: }
943: return '';
944: }
945:
946: sub end_solved {
947: return '';
948: }
949:
950: sub start_startouttext {
951: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
952: my @result=(''.'');
953: if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
954: return (@result);
955: }
956:
957: sub end_startouttext {
958: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
959: my $result='';
960: my $text='';
961:
962: if ($target eq 'edit') {
963: $text=&Apache::lonxml::get_all_text("endouttext",$parser);
964: $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
965: <td>Delete:".
966: &Apache::edit::deletelist($target,$token)
967: ."</td>
968: <td>".
969: &Apache::edit::insertlist($target,$token).
970: &Apache::edit::end_row().
971: &Apache::edit::start_spanning_row()."\n"
972: .'<table><tr><td>'.
973: &Apache::loncommon::help_open_topic("Greek_Symbols",
974: 'Greek Symbols',
975: undef,undef,600)
976: .'</td><td>'.
977: &Apache::loncommon::help_open_topic("Other_Symbols",
978: 'Other Symbols',
979: undef,undef,600)
980: .'</td></tr></table>'.
981: &Apache::edit::editfield($token->[1],$text,"",80,4);
982: }
983: if ($target eq 'modified') {
984: $text=&Apache::lonxml::get_all_text("endouttext",$parser);
985: $result='<startouttext />'.&Apache::edit::modifiedfield();
986: }
987: if ($target eq 'tex') {
988: $result .= '\noindent ';
989: }
990: return $result;
991: }
992:
993: sub start_endouttext {
994: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
995: my $result='';
996: if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
997: if ($target eq "modified") {
998: $result='<endouttext />'.
999: &Apache::edit::handle_insertafter('startouttext'); }
1000: return $result;
1001: }
1002:
1003: sub end_endouttext {
1004: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1005: my @result=('','');
1006: if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
1007: return (@result);
1008: }
1009:
1010: sub delete_startouttext {
1011: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1012: # my $text=&Apache::lonxml::get_all_text("endouttext",$parser);
1013: my $text=$$parser['-1']->get_text("/endouttext");
1014: my $ntoken=$$parser['-1']->get_token();
1015: &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
1016: &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
1017: # Deleting 2 parallel tag pairs, but we need the numbers later to look like
1018: # they did the last time round
1019: &Apache::lonxml::increasedepth($ntoken);
1020: &Apache::lonxml::decreasedepth($ntoken);
1021: return 1;
1022: }
1023:
1024: 1;
1025: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>