File:
[LON-CAPA] /
loncom /
homework /
response.pm
Revision
1.174:
download - view:
text,
annotated -
select for diffs
Fri Aug 10 18:16:28 2007 UTC (17 years ago) by
riegler
Branches:
MAIN
CVS tags:
HEAD
Two changes in &implicit_multiplication:
[1] (debateable)
Replaced \w by [A-Za-z0-9]. This assumes that in a student's answer a variable name never starts or ends with underscore. On the other hand this makes it possible to use underscore as an operator (e.g. a _ 3 for (a,3) is member of a relation. Can't think of a better example...)
[2] (required) Hitherto
$expression=~s/(\w)\s+(\w)/$1\*$2/gs
or
$expression=~s/([A-Za-z0-9])\s+([A-Za-z0-9])/$1\*$2/gs
transform
6 a b c d e f
into
6*a b*c d*e f
whereas
while($expression=~s/([A-Za-z0-9])\s+([A-Za-z0-9])/$1\*$2/gs){pos($expression)--}
will produce the desired output
6*a*b*c*d*e*f
1: # The LearningOnline Network with CAPA
2: # various response type definitons response definition
3: #
4: # $Id: response.pm,v 1.174 2007/08/10 18:16:28 riegler 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:
29: package Apache::response;
30: use strict;
31: use Apache::lonlocal;
32: use Apache::lonnet;
33: use Apache::lonmaxima();
34:
35: BEGIN {
36: &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse','customresponse','mathresponse'));
37: }
38:
39: sub start_response {
40: my ($parstack,$safeeval)=@_;
41: my $id = &Apache::lonxml::get_id($parstack,$safeeval);
42: if ($#Apache::inputtags::import > -1) {
43: &Apache::lonxml::debug("Turning :$id: into");
44: $id = join('_',@Apache::inputtags::import).'_'.$id;
45: &Apache::lonxml::debug("New :$id:");
46: }
47: push (@Apache::inputtags::response,$id);
48: push (@Apache::inputtags::responselist,$id);
49: @Apache::inputtags::inputlist=();
50: if ($Apache::inputtags::part eq '' &&
51: !$Apache::lonhomework::ignore_response_errors) {
52: &Apache::lonxml::error(&HTML::Entities::encode(&mt("Found a <*response> outside of a <part> in a <part>ed problem"),'<>&"'));
53: }
54: if ($Apache::inputtags::response_with_no_part &&
55: $Apache::inputtags::part ne '0') {
56: &Apache::lonxml::error(&HTML::Entities::encode(&mt("<*response>s are both inside of <part> and outside of <part>, this is not a valid problem, errors in grading may occur."),'<>&"').'<br />');
57: }
58: if ($Apache::inputtags::part eq '0') {
59: $Apache::inputtags::response_with_no_part=1;
60: }
61: return $id;
62: }
63:
64: sub end_response {
65: #pop @Apache::inputtags::response;
66: @Apache::inputtags::inputlist=();
67: return '';
68: }
69:
70: sub start_hintresponse {
71: my ($parstack,$safeeval)=@_;
72: my $id = &Apache::lonxml::get_id($parstack,$safeeval);
73: push (@Apache::inputtags::hint,$id);
74: push (@Apache::inputtags::hintlist,$id);
75: push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
76: return $id;
77: }
78:
79: sub end_hintresponse {
80: pop @Apache::inputtags::hint;
81: if (defined($Apache::inputtags::paramstack[-1])) {
82: %Apache::inputtags::params=
83: @{ pop(@Apache::inputtags::paramstack) };
84: }
85: return '';
86: }
87:
88: my @randomseeds;
89: sub pushrandomnumber {
90: my $rand_alg=&Apache::lonnet::get_rand_alg();
91: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
92: $rand_alg eq '64bit2') {
93: # do nothing
94: } else {
95: my @seed=&Math::Random::random_get_seed();
96: push(@randomseeds,\@seed);
97: }
98: &Apache::response::setrandomnumber(@_);
99: }
100: sub poprandomnumber {
101: my $rand_alg=&Apache::lonnet::get_rand_alg();
102: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
103: $rand_alg eq '64bit2') {
104: return;
105: }
106: my $seed=pop(@randomseeds);
107: if ($seed) {
108: &Math::Random::random_set_seed(@$seed);
109: } else {
110: &Apache::lonxml::error("Unable to restore random algorithm.");
111: }
112: }
113:
114: sub setrandomnumber {
115: my ($ignore_id2) = @_;
116: my $rndseed;
117: $rndseed=&Apache::structuretags::setup_rndseed();
118: if (!defined($rndseed)) { $rndseed=&Apache::lonnet::rndseed(); }
119: &Apache::lonxml::debug("randseed $rndseed");
120: # $rndseed=unpack("%32i",$rndseed);
121: my $rand_alg=&Apache::lonnet::get_rand_alg();
122: my ($rndmod,$rndmod2);
123:
124: my ($id1,$id2,$shift_amt);
125: if ($Apache::lonhomework::parsing_a_problem) {
126: $id1=$Apache::inputtags::part;
127: if (defined($Apache::inputtags::response[-1])) {
128: $id2=$Apache::inputtags::response[-1];
129: }
130: $shift_amt=scalar(@Apache::inputtags::responselist);
131: } elsif ($Apache::lonhomework::parsing_a_task) {
132: $id1=&Apache::bridgetask::get_dim_id();
133: if (!$ignore_id2 && ref($Apache::bridgetask::instance{$id1})) {
134: $id2=$Apache::bridgetask::instance{$id1}[-1];
135: $shift_amt=scalar(@{$Apache::bridgetask::instance{$id1}});
136: } else {
137: $shift_amt=0;
138: }
139: }
140: &Apache::lonxml::debug("id1: $id1, id2: $id2, shift_amt: $shift_amt");
141: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
142: $rand_alg eq '64bit2') {
143: $rndmod=(&Apache::lonnet::numval($id1) << 10);
144: if (defined($id2)) { $rndmod+=&Apache::lonnet::numval($id2); }
145: } elsif ($rand_alg eq '64bit3') {
146: $rndmod=(&Apache::lonnet::numval2($id1) << 10);
147: if (defined($id2)) { $rndmod+=&Apache::lonnet::numval2($id2); }
148: } elsif ($rand_alg eq '64bit4') {
149: my $shift=(4*$shift_amt)%30;
150: $rndmod=(&Apache::lonnet::numval3($id1) << (($shift+15)%30));
151: if (defined($id2)) {
152: $rndmod+=(&Apache::lonnet::numval3($id2) << $shift );
153: }
154: } else {
155: ($rndmod,$rndmod2)=&Apache::lonnet::digest("$id1,$id2");
156: }
157:
158: if ($rndseed =~/([,:])/) {
159: my $char=$1;
160: use integer;
161: my ($num1,$num2)=split(/\Q$char\E/,$rndseed);
162: $num1+=$rndmod;
163: $num2+= ((defined($rndmod2)) ? $rndmod2 : $rndmod);
164: if($Apache::lonnet::_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
165: $rndseed=$num1.$char.$num2;
166: } else {
167: $rndseed+=$rndmod;
168: if($Apache::lonnet::_64bit) {
169: use integer;
170: $rndseed=(($rndseed<<32)>>32);
171: }
172: }
173: &Apache::lonxml::debug("randseed $rndmod $rndseed");
174: &Apache::lonnet::setup_random_from_rndseed($rndseed);
175: return '';
176: }
177:
178: sub meta_parameter_write {
179: my ($name,$type,$default,$display)=@_;
180: my $partref=$Apache::inputtags::part;
181: my $result='<parameter part="'.$Apache::inputtags::part.'"';
182: if (defined($Apache::inputtags::response[-1])) {
183: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
184: $partref.='_'.$Apache::inputtags::response[-1];
185: }
186: $result.= ' name="'.$name.'"'.
187: ' type="'.$type.'"'.
188: (defined($default)?' default="'.$default.'"':'').
189: (defined($display)?' display="'.$display.' [Part: '.$partref.']"':'')
190: .'></parameter>'
191: ."\n";
192: return $result;
193: }
194:
195: sub meta_package_write {
196: my $name=shift;
197: my $result = '<parameter part="'.$Apache::inputtags::part.'"';
198: if(defined($Apache::inputtags::response[-1])) {
199: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
200: }
201: $result.=' package="'.$name.'"></parameter>'."\n";
202: return $result;
203: }
204:
205: sub meta_stores_write {
206: my ($name,$type,$display)=@_;
207: my $partref=$Apache::inputtags::part;
208: my $result = '<stores part="'.$Apache::inputtags::part.'"';
209: if (defined($Apache::inputtags::response[-1])) {
210: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
211: $partref.='_'.$Apache::inputtags::response[-1];
212: }
213: $result.= ' name="'.$name.'"'.
214: ' type="'.$type.'"'.
215: ' display="'.$display.' [Part: '.$partref.']"'.
216: "></stores>\n";
217: }
218:
219: sub mandatory_part_meta {
220: #
221: # Autogenerate metadata for mandatory
222: # input (from RAT or lonparmset) and
223: # output (to lonspreadsheet)
224: # of each part
225: #
226: return
227: # &meta_parameter_write('opendate','date_start','',
228: # 'Opening Date').
229: # &meta_parameter_write('duedate','date_end','',
230: # 'Due Date').
231: # &meta_parameter_write('answerdate','date_start','',
232: # 'Show Answer Date').
233: # &meta_parameter_write('weight','int_zeropos','',
234: # 'Available Points').
235: # &meta_parameter_write('maxtries','int_pos','',
236: # 'Maximum Number of Tries').
237: &meta_package_write('part').
238: &meta_stores_write('solved','string',
239: 'Problem Status').
240: &meta_stores_write('tries','int_zeropos',
241: 'Number of Attempts').
242: &meta_stores_write('awarded','float',
243: 'Partial Credit Factor');
244: #
245: # Note: responseid-specific data 'submission' and 'awarddetail'
246: # not available to spreadsheet -> skip here
247: #
248: }
249:
250: sub meta_part_order {
251: if (@Apache::inputtags::partlist) {
252: my @parts=@Apache::inputtags::partlist;
253: shift(@parts);
254: return '<partorder>'.join(',',@parts).'</partorder>'."\n";
255: } else {
256: return '<partorder>0</partorder>'."\n";
257: }
258: }
259:
260: sub meta_response_order {
261: if (@Apache::inputtags::responselist) {
262: return '<responseorder>'.join(',',@Apache::inputtags::responselist).
263: '</responseorder>'."\n";
264: }
265: }
266:
267: sub check_for_previous {
268: my ($curresponse,$partid,$id,$last) = @_;
269: my %previous;
270: $previous{'used'} = 0;
271: foreach my $key (sort(keys(%Apache::lonhomework::history))) {
272: if ($key =~ /resource\.$partid\.$id\.submission$/) {
273: if ( $last && $key =~ /^(\d+):/ ) {
274: next if ($1 >= $last);
275: }
276: &Apache::lonxml::debug("Trying $key");
277: my $pastresponse=$Apache::lonhomework::history{$key};
278: if ($pastresponse eq $curresponse) {
279: $previous{'used'} = 1;
280: my $history;
281: if ( $key =~ /^(\d+):/ ) {
282: $history=$1;
283: $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
284: $previous{'last'}='0';
285: push(@{ $previous{'version'} },$history);
286: } else {
287: $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
288: $previous{'last'}='1';
289: }
290: if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN'; }
291: &Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
292: }
293: }
294: }
295: &Apache::lonhomework::showhash(%previous);
296: return %previous;
297: }
298:
299: sub handle_previous {
300: my ($previous,$ad)=@_;
301: if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
302: if ($$previous{'last'}) {
303: push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
304: push(@Apache::inputtags::previous_version,$$previous{'version'});
305: } elsif ($Apache::lonhomework::type ne 'survey') {
306: push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
307: push(@Apache::inputtags::previous_version,$$previous{'version'});
308: }
309: }
310: }
311:
312: sub view_or_modify {
313: my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
314: my $myself=0;
315: if ( ($name eq $env{'user.name'}) && ($domain eq $env{'user.domain'}) ) {
316: $myself=1;
317: }
318: my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
319: my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
320: if ($mgr) { return "M"; }
321: if ($vgr) { return "V"; }
322: if ($myself) { return "V"; }
323: return '';
324: }
325:
326: sub start_dataresponse {
327: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
328: my $id = &Apache::response::start_response($parstack,$safeeval);
329: my $result;
330: if ($target eq 'web') {
331: $result = $token->[2]->{'display'}.':';
332: } elsif ($target eq 'meta') {
333: $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
334: $token->[2]->{'type'},
335: $token->[2]->{'display'});
336: $result .= &Apache::response::meta_package_write('dataresponse');
337: }
338: return $result;
339: }
340:
341: sub end_dataresponse {
342: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
343: my $result;
344: if ( $target eq 'web' ) {
345: } elsif ($target eq 'grade' ) {
346: if ( defined $env{'form.submitted'}) {
347: my ($symb,$courseid,$domain,$name)=&Apache::lonnet::whichuser();
348: my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
349: if ($allowed) {
350: &Apache::response::setup_params('dataresponse',$safeeval);
351: my $partid = $Apache::inputtags::part;
352: my $id = $Apache::inputtags::response['-1'];
353: my $response = $env{'form.HWVAL_'.$id};
354: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
355: if ( $response =~ /[^\s]/) {
356: $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
357: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
358: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
359: }
360: } else {
361: $result='Not Permitted to change values.'
362: }
363: }
364: }
365: &Apache::response::end_response;
366: return $result;
367: }
368:
369: sub start_customresponse {
370: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
371: my $id = &Apache::response::start_response($parstack,$safeeval);
372: push(@Apache::lonxml::namespace,'customresponse');
373: my $result;
374: @Apache::response::custom_answer=();
375: @Apache::response::custom_answer_type=();
376: &Apache::lonxml::register('Apache::response',('answer'));
377: if ($target eq 'web') {
378: if ( &Apache::response::show_answer() ) {
379: my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
380: $safeeval);
381: $Apache::inputtags::answertxt{$id}=[$answer];
382: }
383: } elsif ($target eq 'edit') {
384: $result.=&Apache::edit::tag_start($target,$token);
385: $result.=&Apache::edit::text_arg('String to display for answer:',
386: 'answerdisplay',$token);
387: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
388: } elsif ($target eq 'modified') {
389: my $constructtag;
390: $constructtag=&Apache::edit::get_new_args($token,$parstack,
391: $safeeval,'answerdisplay');
392: if ($constructtag) {
393: $result = &Apache::edit::rebuild_tag($token);
394: }
395: } elsif ($target eq 'answer' || $target eq 'grade') {
396: &Apache::response::reset_params();
397: } elsif ($target eq 'meta') {
398: $result .= &Apache::response::meta_package_write('customresponse');
399: }
400: return $result;
401: }
402:
403: sub end_customresponse {
404: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
405: my $result;
406: my $part=$Apache::inputtags::part;
407: my $id=$Apache::inputtags::response[-1];
408: if ( $target eq 'grade' && &Apache::response::submitted() ) {
409: my $response = &Apache::response::getresponse();
410: if ($Apache::lonhomework::type eq 'exam' ||
411: &Apache::response::submitted('scantron')) {
412: &Apache::response::scored_response($part,$id);
413: } elsif ( $response =~ /[^\s]/ &&
414: $Apache::response::custom_answer_type[-1] eq 'loncapa/perl') {
415: if (!$Apache::lonxml::default_homework_loaded) {
416: &Apache::lonxml::default_homework_load($safeeval);
417: }
418: my %previous = &Apache::response::check_for_previous($response,
419: $part,$id);
420: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
421: $response;
422: my $error;
423: ${$safeeval->varglob('LONCAPA::customresponse_submission')}=
424: $response;
425:
426: my $award = &Apache::run::run('{ my $submission=$LONCAPA::customresponse_submission;'.$Apache::response::custom_answer[-1].'}',$safeeval);
427: if (!&Apache::inputtags::valid_award($award)) {
428: $error = $award;
429: $award = 'ERROR';
430: }
431: &Apache::response::handle_previous(\%previous,$award);
432: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
433: $award;
434: if ($error) {
435: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
436: $error;
437: }
438: }
439: } elsif ( $target eq 'answer') {
440: $result = &Apache::response::answer_header('customresponse');
441: my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
442: $safeeval);
443: if ($env{'form.answer_output_mode'} ne 'tex') {
444: $answer = '<b>'.$answer.'</b>';
445: }
446: $result .= &Apache::response::answer_part('customresponse',$answer);
447: $result .= &Apache::response::answer_footer('customresponse');
448: }
449: if ($target eq 'web') {
450: &setup_prior_tries_hash(\&format_prior_response_math);
451: }
452: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
453: $target eq 'tex' || $target eq 'analyze') {
454: &Apache::lonxml::increment_counter(&Apache::response::repetition());
455: }
456: pop(@Apache::lonxml::namespace);
457: pop(@Apache::response::custom_answer);
458: pop(@Apache::response::custom_answer_type);
459: &Apache::lonxml::deregister('Apache::response',('answer'));
460: &Apache::response::end_response();
461: return $result;
462: }
463:
464: sub format_prior_response_custom {
465: my ($mode,$answer) =@_;
466: return '<span class="LC_prior_custom">'.
467: &HTML::Entities::encode($answer,'"<>&').'</span>';
468: }
469:
470: sub start_mathresponse {
471: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
472: my $id = &Apache::response::start_response($parstack,$safeeval);
473: push(@Apache::lonxml::namespace,'mathresponse');
474: my $result;
475: @Apache::response::custom_answer=();
476: @Apache::response::custom_answer_type=();
477: &Apache::lonxml::register('Apache::response',('answer'));
478: if ($target eq 'web') {
479: if ( &Apache::response::show_answer() ) {
480: my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
481: $safeeval);
482: $Apache::inputtags::answertxt{$id}=[$answer];
483: }
484: } elsif ($target eq 'edit') {
485: $result.=&Apache::edit::tag_start($target,$token);
486: $result.=&Apache::edit::text_arg('String to display for answer:',
487: 'answerdisplay',$token);
488: $result.=&Apache::edit::select_arg('Algebra System:',
489: 'cas',
490: ['maxima'],
491: $token);
492: $result.=&Apache::edit::text_arg('Argument Array:',
493: 'args',$token);
494: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
495: } elsif ($target eq 'modified') {
496: my $constructtag;
497: $constructtag=&Apache::edit::get_new_args($token,$parstack,
498: $safeeval,'answerdisplay','cas','args');
499: if ($constructtag) {
500: $result = &Apache::edit::rebuild_tag($token);
501: }
502: } elsif ($target eq 'answer' || $target eq 'grade') {
503: &Apache::response::reset_params();
504: } elsif ($target eq 'meta') {
505: $result .= &Apache::response::meta_package_write('mathresponse');
506: }
507: return $result;
508: }
509:
510: sub end_mathresponse {
511: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
512: my $result;
513: my $part=$Apache::inputtags::part;
514: my $id=$Apache::inputtags::response[-1];
515: if ( $target eq 'grade' && &Apache::response::submitted() ) {
516: my $response = &Apache::response::getresponse();
517: if ( $response =~ /[^\s]/ ) {
518: if (!$Apache::lonxml::default_homework_loaded) {
519: &Apache::lonxml::default_homework_load($safeeval);
520: }
521: my %previous = &Apache::response::check_for_previous($response,
522: $part,$id);
523: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
524: $response;
525: my $error;
526: my $award;
527: my $cas = &Apache::lonxml::get_param('cas',$parstack,$safeeval);
528: if ($cas eq 'maxima') {
529: my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
530: $award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer[-1],$response,$args);
531: }
532: if (!&Apache::inputtags::valid_award($award)) {
533: $error = $award;
534: $award = 'ERROR';
535: }
536: &Apache::response::handle_previous(\%previous,$award);
537: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
538: $award;
539: if ($error) {
540: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
541: $error;
542: }
543: }
544: }
545: if ($target eq 'web') {
546: &setup_prior_tries_hash(\&format_prior_response_math);
547: }
548:
549: pop(@Apache::lonxml::namespace);
550: pop(@Apache::response::custom_answer);
551: pop(@Apache::response::custom_answer_type);
552: &Apache::lonxml::deregister('Apache::response',('answer'));
553: &Apache::response::end_response();
554: return $result;
555: }
556:
557: sub format_prior_response_math {
558: my ($mode,$answer) =@_;
559: return '<span class="LC_prior_math">'.
560: &HTML::Entities::encode($answer,'"<>&').'</span>';
561: }
562:
563: sub implicit_multiplication {
564: my ($expression)=@_;
565: # Escape scientific notation, so 3e8 does not become 3*e*8
566: # 3e8 -> 3&8; 3e-8 -> 3&-8; 3E+8 -> e&+8
567: $expression=~s/(\d+)e([\+\-]*\d+)/$1\&\($2\)/gsi;
568: # 3x10^8 -> 3&8; 3*10^-8 -> 3&-8
569: $expression=~s/(\d+)(?:x|\*)10(?:\^|\*\*)([\+\-]*\d+)/$1\&\($2\)/gsi;
570: # Fill in multiplication signs
571: # a b -> a*b;3 b -> 3*b;3 4 -> 3*4
572: # The while-pos-construction is necessary for a b c -> a*b*c*d instead of a*b c*d
573: while($expression=~s/([A-Za-z0-9])\s+([A-Za-z0-9])/$1\*$2/gs){pos($expression)--}
574: # )( -> )*(; ) ( -> )*(
575: $expression=~s/\)\s*\(/\)\*\(/gs;
576: # 3a -> 3*a; 3( -> 3*(; 3 ( -> 3*(; 3A -> 3*A
577: $expression=~s/(\d)\s*([a-zA-Z\(])/$1\*$2/gs;
578: # a ( -> a*(
579: $expression=~s/([A-Za-z0-9])\s+\(/$1\*\(/gs;
580: # )a -> )*a; )3 -> )*3; ) 3 -> )*3
581: $expression=~s/\)\s*([A-Za-z0-9])/\)\*$1/gs;
582: # 3&8 -> 3e8; 3&-4 -> 3e-4
583: $expression=~s/(\d+)\&\(([\+\-]*\d+)\)/$1e$2/gs;
584: return $expression;
585: }
586:
587: sub start_answer {
588: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
589: my $result;
590: push(@Apache::response::custom_answer,
591: &Apache::lonxml::get_all_text_unbalanced("/answer",$parser));
592: push(@Apache::response::custom_answer_type,
593: lc(&Apache::lonxml::get_param('type',$parstack,$safeeval)));
594: $Apache::response::custom_answer_type[-1] =~ s/\s+//g;
595: if ($target eq "edit" ) {
596: $result=&Apache::edit::tag_start($target,$token,'Answer algorithm');
597: $result.=&Apache::edit::editfield($token->[1],
598: $Apache::response::custom_answer[-1],
599: '',80,4);
600: } elsif ( $target eq "modified" ) {
601: $result=$token->[4].&Apache::edit::modifiedfield('/answer',$parser);
602: }
603: return $result;
604: }
605:
606: sub end_answer {
607: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
608: if ($target eq 'edit' ) {
609: return &Apache::edit::end_table();
610: }
611: }
612:
613: sub decide_package {
614: my ($tagstack)=@_;
615: my $package;
616: if ($$tagstack[-1] eq 'parameter') {
617: $package='part';
618: } else {
619: my $i=-1;
620: while (defined($$tagstack[$i])) {
621: if ($$tagstack[$i] =~ /(response|hint)$/) {
622: $package=$$tagstack[$i];
623: last;
624: }
625: $i--;
626: }
627: }
628: return $package;
629: }
630:
631: sub start_responseparam {
632: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
633: my $result='';
634: if ($target eq 'meta') {
635: $result = &meta_parameter_write($token->[2]->{'name'},
636: $token->[2]->{'type'},
637: $token->[2]->{'default'},
638: $token->[2]->{'description'});
639: } elsif ($target eq 'edit') {
640: $result.=&Apache::edit::tag_start($target,$token);
641: my $optionlist;
642: my $package=&decide_package($tagstack);
643: foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
644: if ($key =~ /^\Q$package\E&(.*)&display$/) {
645: $optionlist.='<option value="'.$1.'">'.
646: $Apache::lonnet::packagetab{$key}.'</option>';
647: }
648: }
649: if (defined($optionlist)) {
650: $result.='Use template: <select name="'.
651: &Apache::edit::html_element_name('parameter_package').'">'.
652: '<option value=""></option>'.$optionlist.'</select><br />';
653: }
654: $result.=&Apache::edit::text_arg('Name:','name',$token).
655: &Apache::edit::text_arg('Type:','type',$token).
656: &Apache::edit::text_arg('Description:','description',$token).
657: &Apache::edit::text_arg('Default:','default',$token).
658: "</td></tr>";
659: $result.=&Apache::edit::end_table;
660: } elsif ($target eq 'modified') {
661: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
662: $safeeval,'name','type',
663: 'description','default');
664: my $element=&Apache::edit::html_element_name('parameter_package');
665: if (defined($env{"form.$element"}) && $env{"form.$element"} ne '') {
666: my $name=$env{"form.$element"};
667: my $tag=&decide_package($tagstack);
668: $token->[2]->{'name'}=$name;
669: $token->[2]->{'type'}=
670: $Apache::lonnet::packagetab{"$tag&$name&type"};
671: $token->[2]->{'description'}=
672: $Apache::lonnet::packagetab{"$tag&$name&display"};
673: $token->[2]->{'default'}=
674: $Apache::lonnet::packagetab{"$tag&$name&default"};
675: $constructtag=1;
676: }
677: if ($constructtag) {
678: $result = &Apache::edit::rebuild_tag($token);
679: }
680: } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
681: $target eq 'tex' || $target eq 'analyze' ) {
682: if ($env{'request.state'} eq 'construct') {
683: my $name =&Apache::lonxml::get_param('name',$parstack,$safeeval);
684: my $default=&Apache::lonxml::get_param('default',$parstack,
685: $safeeval);
686: if ($name) {$Apache::inputtags::params{$name}=$default;}
687: }
688: }
689: return $result;
690: }
691:
692: sub end_responseparam {
693: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
694: if ($target eq 'edit') { return ('','no'); }
695: return '';
696: }
697:
698: sub start_parameter {
699: return &start_responseparam(@_);
700: }
701:
702: sub end_parameter {
703: return &end_responseparam(@_);
704: }
705:
706: sub reset_params {
707: %Apache::inputtags::params=();
708: }
709:
710: sub setup_params {
711: my ($tag,$safeeval) = @_;
712:
713: if ($env{'request.state'} eq 'construct') { return; }
714: my %paramlist=();
715: foreach my $key (keys(%Apache::lonnet::packagetab)) {
716: if ($key =~ /^\Q$tag\E/) {
717: my ($package,$name) = split(/&/,$key);
718: $paramlist{$name}=1;
719: }
720: }
721: foreach my $key (keys(%paramlist)) {
722: my $entry= 'resource.'.$Apache::inputtags::part;
723: if (defined($Apache::inputtags::response[-1])) {
724: $entry.='_'.$Apache::inputtags::response[-1];
725: }
726: $entry.='.'.$key;
727: &Apache::lonxml::debug("looking for $entry");
728: my $value = &Apache::lonnet::EXT("$entry");
729: &Apache::lonxml::debug("$key has value :$value:");
730: if ($value eq 'con_lost' || $value =~ /^error:/) {
731: &Apache::lonxml::debug("using nothing");
732: $Apache::inputtags::params{$key}='';
733: } else {
734: my $string="{return qq\0".$value."\0}";
735: my $newvalue=&Apache::run::run($string,$safeeval,1);
736: if (defined($newvalue)) { $value=$newvalue; }
737: $Apache::inputtags::params{$key}=$value;
738: }
739: }
740: }
741:
742: {
743: my @answer_bits;
744: my $need_row_start;
745:
746: sub answer_header {
747: my ($type,$increment,$rows) = @_;
748: my $result;
749: if ($env{'form.answer_output_mode'} eq 'tex') {
750: undef(@answer_bits);
751: my $bit;
752: if ($Apache::lonhomework::type eq 'exam') {
753: $bit = ($Apache::lonxml::counter+$increment).') ';
754: } else {
755: $bit .= ' Answer for Part: \verb|'.
756: $Apache::inputtags::part.'| ';
757: }
758: push(@answer_bits,$bit);
759: } else {
760: my $td = '<td '.(defined($rows)?'rowspan="'.$rows.'"':'').'>';
761: $result = '<table border="1"><tr>';
762: if ($Apache::lonhomework::type eq 'exam') {
763: $result .= $td.($Apache::lonxml::counter+$increment). ')</td>';
764: } else {
765: $result .= $td.&mt('Answer for Part: [_1]',
766: $Apache::inputtags::part).'</td>';
767: }
768: $result .= "\n";
769: $need_row_start = 0;
770: }
771: return $result;
772: }
773:
774: sub next_answer {
775: my ($type) = @_;
776: my $result;
777: if ($env{'form.answer_output_mode'} eq 'tex') {
778: # FIXME ... need to do something with tex mode
779: } else {
780: $result .= "</tr>";
781: $need_row_start = 1;
782: }
783: return $result;
784: }
785:
786: sub answer_part {
787: my ($type,$answer,$args) = @_;
788: my $result;
789: if ($env{'form.answer_output_mode'} eq 'tex') {
790: if (!$args->{'no_verbatim'}) {
791: my $to_use='|';
792: foreach my $value (32..126) {
793: my $char=pack('c',$value);
794: if ($answer !~ /\Q$char\E/) {
795: $to_use=$char;
796: last;
797: }
798: }
799: if ($answer ne '') {
800: $answer = '\verb'.$to_use.$answer.$to_use;
801: }
802: }
803: if ($answer ne '') {
804: push(@answer_bits,$answer);
805: }
806: } else {
807: if ($need_row_start) {
808: $result .= '<tr>';
809: $need_row_start = 0;
810: }
811: $result .= '<td>'.$answer.'</td>';
812: }
813: return $result;
814: }
815:
816: sub answer_footer {
817: my ($type) = @_;
818: my $result;
819: if ($env{'form.answer_output_mode'} eq 'tex') {
820: my $columns = scalar(@answer_bits);
821: $result = ' \vskip 0 mm \noindent \begin{tabular}{|'.'c|'x$columns.'}\hline ';
822: $result .= join(' & ',@answer_bits);
823: $result .= ' \\\\ \\hline \end{tabular} \vskip 0 mm ';
824: } else {
825: $result = '</tr></table>';
826: }
827: return $result;
828: }
829:
830: }
831:
832: sub showallfoils {
833: if (defined($env{'form.showallfoils'})) {
834: my ($symb)=&Apache::lonnet::whichuser();
835: if (($env{'request.state'} eq 'construct') ||
836: ($env{'user.adv'} && $symb eq '') ||
837: ($Apache::lonhomework::viewgrades) ) {
838: return 1;
839: }
840: }
841: if ($Apache::lonhomework::type eq 'survey') { return 1; }
842: return 0;
843: }
844:
845: =pod
846:
847: =item &getresponse($offset,$resulttype);
848:
849: Retreives the current submitted response, helps out in the case of
850: scantron mode.
851:
852: Returns either the exact text of the submission, or a bubbled response
853: converted to something usable.
854:
855: Optional Arguments:
856: $offset - (defaults to 1) if a problem has more than one bubble
857: response, pass in the number of the bubble wanted, (the
858: first bubble associated with a problem has an offset of 1,
859: the second bubble is 2
860:
861: $resulttype - undef -> a number between 0 and 25
862: 'A is 1' -> a number between 1 and 26
863: 'letter' -> a letter between 'A' and 'Z'
864: $lines - undef problem only needs a single line of bubbles.
865: nonzero Problem wants the first nonempty response in
866: $lines lines of bubbles.
867: $bubbles_per_line - Must be provided if lines is defined.. number of
868: bubbles on a line.
869:
870: =cut
871:
872: sub getresponse {
873: my ($offset,$resulttype, $lines, $bubbles_per_line)=@_;
874: my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
875: my $response;
876: if (!defined($offset)) {
877: $offset=1;
878: } else {
879: $formparm.=":$offset";
880: }
881: if (!defined($lines)) {
882: $lines = 1;
883: }
884:
885: my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
886: 'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
887: 'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
888: 'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
889: if ($env{'form.submitted'} eq 'scantron') {
890: my $part = $Apache::inputtags::part;
891: my $id = $Apache::inputtags::response[-1];
892:
893: my $line;
894: for ($line = 0; $line < $lines; $line++) {
895: $response = $env{'scantron.'.
896: ($Apache::lonxml::counter+$offset-1+$line).
897: '.answer'};
898: if ($response ne "") {
899: last;
900: }
901:
902: }
903: if ($resulttype ne 'letter') {
904: if ($resulttype eq 'A is 1') {
905: $response = $let_to_num{$response}+1;
906: } else {
907: $response = $let_to_num{$response};
908: }
909: if ($response ne "") {
910: $response += $line * $bubbles_per_line;
911: }
912: } else {
913: if ($response ne "") {
914: $response = chr(ord($response) + $line * $bubbles_per_line);
915: }
916: }
917: # save submitted response for later
918: $Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
919: $response;
920:
921: } else {
922: $response = $env{$formparm};
923: }
924: #
925: # If we have a nonempty answer, correct the numeric value
926: # of the answer for the line on which it was found.
927: #
928:
929: return $response;
930: }
931:
932: =pod
933:
934: =item &repetition();
935:
936: Returns the number of lines that are required to encode the weight.
937: (Currently expects that there are 10 bubbles per line)
938:
939: =cut
940:
941: sub repetition {
942: my $id = $Apache::inputtags::part;
943: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
944: if (!defined($weight) || ($weight eq '')) { $weight=1; }
945: my $repetition = int($weight/10);
946: if ($weight % 10 != 0) { $repetition++; }
947: return $repetition;
948: }
949:
950: =pod
951:
952: =item &scored_response($part_id,$response_id);
953:
954: Sets the results hash elements
955:
956: resource.$part_id.$response_id.awarded - to the floating point
957: number between 0 and 1 that was awarded on the bubbled input
958:
959: resource.$part_id.$response_id.awarddetail - to 'ASSIGNED_SCORE'
960:
961: Returns
962:
963: the number of bubble sheet lines that were used (and likely need to
964: be passed to &Apache::lonxml::increment_counter()
965:
966: Arguments
967:
968: $part_id - id of the part to grade
969: $response_id - id of the response to grade
970:
971:
972: =cut
973:
974: sub scored_response {
975: my ($part,$id)=@_;
976: my $repetition=&repetition();
977: my $score=0;
978: for (my $i=0;$i<$repetition;$i++) {
979: # A is 1, B is 2, etc. (get response return 0-9 and then we add 1)
980: my $increase=&Apache::response::getresponse($i+1);
981: if ($increase ne '') { $score+=$increase+1; }
982: }
983: my $weight = &Apache::lonnet::EXT("resource.$part.weight");
984: if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
985: my $pcr=$score/$weight;
986: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
987: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
988: 'ASSIGNED_SCORE';
989: return $repetition;
990: }
991:
992: sub whichorder {
993: my ($max,$randomize,$showall,$hash)=@_;
994: #&Apache::lonxml::debug("man $max randomize $randomize");
995: if (!defined(@{ $$hash{'names'} })) { return; }
996: my @names = @{ $$hash{'names'} };
997: my @whichopt =();
998: my (%top,@toplist,%bottom,@bottomlist);
999: if (!($showall || ($randomize eq 'no'))) {
1000: my $current=0;
1001: foreach my $name (@names) {
1002: $current++;
1003: if ($$hash{"$name.location"} eq 'top') {
1004: $top{$name}=$current;
1005: } elsif ($$hash{"$name.location"} eq 'bottom') {
1006: $bottom{$name}=$current;
1007: }
1008: }
1009: }
1010: my $topcount=0;
1011: my $bottomcount=0;
1012: while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
1013: && ($#names > -1)) {
1014: #&Apache::lonxml::debug("Have $#whichopt max is $max");
1015: my $aopt;
1016: if ($showall || ($randomize eq 'no')) {
1017: $aopt=0;
1018: } else {
1019: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
1020: }
1021: #&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
1022: $aopt=splice(@names,$aopt,1);
1023: #&Apache::lonxml::debug("Picked $aopt");
1024: if ($top{$aopt}) {
1025: $toplist[$top{$aopt}]=$aopt;
1026: $topcount++;
1027: } elsif ($bottom{$aopt}) {
1028: $bottomlist[$bottom{$aopt}]=$aopt;
1029: $bottomcount++;
1030: } else {
1031: push (@whichopt,$aopt);
1032: }
1033: }
1034: for (my $i=0;$i<=$#toplist;$i++) {
1035: if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
1036: }
1037: for (my $i=0;$i<=$#bottomlist;$i++) {
1038: if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
1039: }
1040: return @whichopt;
1041: }
1042:
1043: sub show_answer {
1044: my $part = $Apache::inputtags::part;
1045: my $award = $Apache::lonhomework::history{"resource.$part.solved"};
1046: my $status = $Apache::inputtags::status[-1];
1047: return ( ($award =~ /^correct/
1048: && lc($Apache::lonhomework::problemstatus) ne 'no')
1049: || $status eq "SHOW_ANSWER");
1050: }
1051:
1052: sub analyze_store_foilgroup {
1053: my ($shown,$attrs)=@_;
1054: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
1055: foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
1056: if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
1057: push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
1058: foreach my $attr (@$attrs) {
1059: $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
1060: $Apache::response::foilgroup{"$name.".$attr};
1061: }
1062: }
1063: push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1064: }
1065:
1066: sub check_if_computed {
1067: my ($token,$parstack,$safeeval,$name)=@_;
1068: my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
1069: if (ref($token->[2]) eq 'HASH' && $value ne $token->[2]{$name}) {
1070: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
1071: $Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
1072: }
1073: }
1074:
1075: sub pick_foil_for_concept {
1076: my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
1077: if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
1078: my @names = @{ $Apache::response::conceptgroup{'names'} };
1079: my $pick=int(&Math::Random::random_uniform() * ($#names+1));
1080: my $name=$names[$pick];
1081: push @{ $Apache::response::foilgroup{'names'} }, $name;
1082: foreach my $attr (@$attrs) {
1083: $Apache::response::foilgroup{"$name.".$attr} =
1084: $Apache::response::conceptgroup{"$name.".$attr};
1085: }
1086: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
1087: $Apache::response::foilgroup{"$name.concept"} = $concept;
1088: &Apache::lonxml::debug("Selecting $name in $concept");
1089: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
1090: if ($target eq 'analyze') {
1091: push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
1092: $concept);
1093: $Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
1094: $Apache::response::conceptgroup{'names'};
1095: foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
1096: push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
1097: $name);
1098: foreach my $attr (@$attrs) {
1099: $Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
1100: $Apache::response::conceptgroup{"$name.$attr"};
1101: }
1102: }
1103: }
1104: push(@{ $hinthash->{"$part_id.concepts"} },$concept);
1105: $hinthash->{"$part_id.concept.$concept"}=
1106: $Apache::response::conceptgroup{'names'};
1107:
1108: }
1109: #------------------------------------------------------------
1110: #
1111: # Get a parameter associated with a problem.
1112: # Parameters:
1113: # $id - the id of the paramater, either a part id,
1114: # or a partid and responspe id joined by _
1115: # $name - Name of the parameter to fetch
1116: # $default - Default value for the paramter.
1117: #
1118: #
1119: #
1120: sub get_response_param {
1121: my ($id,$name,$default)=@_;
1122: my $parameter;
1123: if ($env{'request.state'} eq 'construct' &&
1124: defined($Apache::inputtags::params{$name})) {
1125: $parameter=$Apache::inputtags::params{$name};
1126: } else {
1127: $parameter=&Apache::lonnet::EXT("resource.$id.$name");
1128: }
1129: if (!defined($parameter) || $parameter eq '') {
1130: $parameter = $default;
1131: }
1132: return $parameter;
1133: }
1134:
1135: sub submitted {
1136: my ($who)=@_;
1137:
1138: # when scatron grading any submission is a submission
1139: if ($env{'form.submitted'} eq 'scantron') { return 1; }
1140: # if the caller only cared if this was a scantron submission
1141: if ($who eq 'scantron') { return 0; }
1142: # if the Submit Answer button for this particular part was pressed
1143: my $partid=$Apache::inputtags::part;
1144: if ($env{'form.submitted'} eq "part_$partid") {
1145: return 1;
1146: }
1147: if ($env{'form.submitted'} eq "yes"
1148: && defined($env{'form.submit_'.$partid})) {
1149: return 1;
1150: }
1151: # Submit All button on a .page was pressed
1152: if (defined($env{'form.all_submit'})) { return 1; }
1153: # otherwise no submission occured
1154: return 0;
1155: }
1156:
1157: sub add_to_gradingqueue {
1158: my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1159: if ( $courseid eq ''
1160: || $symb eq ''
1161: || $env{'request.state'} eq 'construct'
1162: || $Apache::lonhomework::type ne 'problem') {
1163: return;
1164: }
1165:
1166: my %queue_info = ( 'type' => 'problem',
1167: 'time' => time);
1168:
1169: if (exists($Apache::lonhomework::history{"resource.0.checkedin.slot"})) {
1170: $queue_info{'slot'}=
1171: $Apache::lonhomework::history{"resource.0.checkedin.slot"};
1172: }
1173:
1174: my $result=&Apache::bridgetask::add_to_queue('gradingqueue',\%queue_info);
1175: if ($result ne 'ok') {
1176: &Apache::lonxml::error("add_to_queue said $result");
1177: }
1178: }
1179:
1180: # basically undef and 0 (both false) mean that they still have work to do
1181: # and all true values mean that they can't do any more work
1182: #
1183: # a return of undef means it is unattempted
1184: # a return of 0 means it is attmpted and wrong but still has tries
1185: # a return of 1 means it is marked correct
1186: # a return of 2 means they have exceed maximum number of tries
1187: # a return of 3 means it after the answer date
1188: sub check_status {
1189: my ($id)=@_;
1190: if (!defined($id)) { $id=$Apache::inputtags::part; }
1191: my $curtime=&Apache::lonnet::EXT('system.time');
1192: my $opendate=&Apache::lonnet::EXT("resource.$id.opendate");
1193: my $duedate=&Apache::lonnet::EXT("resource.$id.duedate");
1194: my $answerdate=&Apache::lonnet::EXT("resource.$id.answerdate");
1195: if ( $opendate && $curtime > $opendate &&
1196: $duedate && $curtime > $duedate &&
1197: $answerdate && $curtime > $answerdate) {
1198: return 3;
1199: }
1200: my $status=&Apache::lonnet::EXT("user.resource.resource.$id.solved");
1201: if ($status =~ /^correct/) { return 1; }
1202: if (!$status) { return undef; }
1203: my $maxtries=&Apache::lonnet::EXT("resource.$id.maxtries");
1204: if ($maxtries eq '') { $maxtries=2; }
1205: my $curtries=&Apache::lonnet::EXT("user.resource.resource.$id.tries");
1206: if ($curtries < $maxtries) { return 0; }
1207: return 2;
1208: }
1209:
1210: sub setup_prior_tries_hash {
1211: my ($func,$data) = @_;
1212: my $part = $Apache::inputtags::part;
1213: my $id = $Apache::inputtags::response[-1];
1214: foreach my $i (1..$Apache::lonhomework::history{'version'}) {
1215: my $sub_key = "$i:resource.$part.$id.submission";
1216: next if (!exists($Apache::lonhomework::history{$sub_key}));
1217: my @other_data;
1218: foreach my $datum (@{ $data }) {
1219: if (ref($datum)) {
1220: push(@other_data,$datum);
1221: } else {
1222: my $info_key = "$i:resource.$part.$id.$datum";
1223: push(@other_data,$Apache::lonhomework::history{$info_key});
1224: }
1225: }
1226:
1227: my $output =
1228: &$func('grade',
1229: $Apache::lonhomework::history{$sub_key},
1230: \@other_data);
1231: if (defined($output)) {
1232: $Apache::inputtags::submission_display{$sub_key} = $output;
1233: }
1234: }
1235: }
1236:
1237: 1;
1238: __END__
1239:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>