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