Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.233
1.3 albertel 1: # The LearningOnline Network with CAPA
2: # caparesponse definition
1.47 albertel 3: #
1.233 ! raeburn 4: # $Id: caparesponse.pm,v 1.232 2008/09/12 09:56:10 raeburn Exp $
1.47 albertel 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: #
1.3 albertel 28:
29: package Apache::caparesponse;
30: use strict;
1.4 albertel 31: use capa;
1.190 albertel 32: use Safe::Hole;
33: use Apache::lonmaxima();
1.129 www 34: use Apache::lonlocal;
1.166 albertel 35: use Apache::lonnet;
1.202 www 36: use Apache::response();
1.194 albertel 37: use Storable qw(dclone);
1.3 albertel 38:
1.50 harris41 39: BEGIN {
1.194 albertel 40: &Apache::lonxml::register('Apache::caparesponse',('numericalresponse','stringresponse','formularesponse'));
1.3 albertel 41: }
42:
1.181 albertel 43: my %answer;
1.204 albertel 44: my @answers;
1.203 albertel 45: sub get_answer { return %answer; };
1.204 albertel 46: sub push_answer{ push(@answers,dclone(\%answer)); undef(%answer) }
47: sub pop_answer { %answer = %{pop(@answers)}; };
1.203 albertel 48:
1.181 albertel 49: my $cur_name;
1.195 albertel 50: my $tag_internal_answer_name = 'INTERNAL';
51:
1.181 albertel 52: sub start_answer {
53: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
54: my $result;
55: $cur_name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
56: if ($cur_name =~ /^\s*$/) { $cur_name = $Apache::lonxml::curdepth; }
57: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
58: if (!defined($type) && $tagstack->[-2] eq 'answergroup') {
59: $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
60: }
61: if (!defined($type)) { $type = 'ordered' };
62: $answer{$cur_name}= { 'type' => $type,
63: 'answers' => [] };
1.208 albertel 64: if ($target eq 'edit') {
65: $result.=&Apache::edit::tag_start($target,$token);
66: $result.=&Apache::edit::text_arg('Name:','name',$token);
67: $result.=&Apache::edit::select_arg('Type:','type',
68: [['ordered', 'Ordered' ],
69: ['unordered','Unordered'],],
70: $token);
71: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
72: } elsif ($target eq 'modified') {
73: my $constructtag = &Apache::edit::get_new_args($token,$parstack,
74: $safeeval,'name',
75: 'type');
76: if ($constructtag) {
77: $result = &Apache::edit::rebuild_tag($token);
78: $result.= &Apache::edit::handle_insert();
79: }
80: }
1.181 albertel 81: return $result;
82: }
83:
84: sub end_answer {
85: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
86: my $result;
1.208 albertel 87: if ($target eq 'edit') {
88: $result .= &Apache::edit::tag_end();
89: }
90:
1.181 albertel 91: undef($cur_name);
92: return $result;
93: }
94:
1.208 albertel 95: sub insert_answer {
96: return '
97: <answer>
98: <value></value>
99: </answer>';
100: }
101:
1.181 albertel 102: sub start_answergroup {
103: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
104: my $result;
1.208 albertel 105: if ($target eq 'edit') {
106: $result.=&Apache::edit::tag_start($target,$token);
107: $result.=&Apache::edit::select_arg('Type:','type',
108: [['ordered', 'Ordered' ],
109: ['unordered','Unordered'],],
110: $token);
111: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
112: } elsif ($target eq 'modified') {
113: my $constructtag = &Apache::edit::get_new_args($token,$parstack,
114: $safeeval,'name',
115: 'type');
116: if ($constructtag) {
117: $result = &Apache::edit::rebuild_tag($token);
118: $result.= &Apache::edit::handle_insert();
119: }
120: }
1.181 albertel 121: return $result;
122: }
123:
124: sub end_answergroup {
125: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
126: my $result;
1.194 albertel 127: if ($target eq 'web') {
128: if ( &Apache::response::show_answer() ) {
129: my $partid = $Apache::inputtags::part;
130: my $id = $Apache::inputtags::response[-1];
131: &set_answertext($Apache::lonhomework::history{"resource.$partid.$id.answername"},
132: $target,$token,$tagstack,$parstack,$parser,
133: $safeeval,-2);
134: }
1.208 albertel 135: } elsif ($target eq 'edit') {
136: $result .= &Apache::edit::tag_end();
1.194 albertel 137: }
1.181 albertel 138: return $result;
139: }
140:
1.208 albertel 141: sub insert_answergroup {
142: return '
143: <answergroup>
144: <answer>
145: <value></value>
146: </answer>
147: </answergroup>';
148: }
149:
1.181 albertel 150: sub start_value {
1.182 albertel 151: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.181 albertel 152: my $result;
153: if ( $target eq 'web' || $target eq 'tex' ||
154: $target eq 'grade' || $target eq 'webgrade' ||
155: $target eq 'answer' || $target eq 'analyze' ) {
1.182 albertel 156: my $bodytext = &Apache::lonxml::get_all_text("/value",$parser,$style);
1.181 albertel 157: $bodytext = &Apache::run::evaluate($bodytext,$safeeval,
158: $$parstack[-1]);
1.195 albertel 159:
160: push(@{ $answer{$cur_name}{'answers'} },[$bodytext]);
161:
1.208 albertel 162: } elsif ($target eq 'edit') {
163: $result.=&Apache::edit::tag_start($target,$token);
164: my $bodytext = &Apache::lonxml::get_all_text("/value",$parser,$style);
165: $result.=&Apache::edit::editline($token->[1],$bodytext,undef,40).
166: &Apache::edit::end_row();
167: } elsif ($target eq 'modified') {
168: $result=$token->[4].&Apache::edit::modifiedfield('/value',$parser);
1.181 albertel 169: }
170: return $result;
171: }
172:
173: sub end_value {
174: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
175: my $result;
1.208 albertel 176: if ($target eq 'edit') {
177: $result = &Apache::edit::end_table();
178: }
1.181 albertel 179: return $result;
180: }
181:
1.208 albertel 182: sub insert_value {
183: return '
184: <value></value>';
185: }
186:
1.195 albertel 187: sub start_vector {
188: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
189: my $result;
190: if ( $target eq 'web' || $target eq 'tex' ||
191: $target eq 'grade' || $target eq 'webgrade' ||
192: $target eq 'answer' || $target eq 'analyze' ) {
193: my $bodytext = &Apache::lonxml::get_all_text("/vector",$parser,$style);
194: my @values = &Apache::run::run($bodytext,$safeeval,$$parstack[-1]);
195: if (@values == 1) {
196: @values = split(',',$values[0]);
197: }
198: push(@{ $answer{$cur_name}{'answers'} },\@values);
1.208 albertel 199: } elsif ($target eq 'edit') {
200: $result.=&Apache::edit::tag_start($target,$token);
201: my $bodytext = &Apache::lonxml::get_all_text("/vector",$parser,$style);
202: $result.=&Apache::edit::editline($token->[1],$bodytext,undef,40).
203: &Apache::edit::end_row();
204: } elsif ($target eq 'modified') {
205: $result=$token->[4].&Apache::edit::modifiedfield('/vector',$parser);
1.195 albertel 206: }
207: return $result;
208: }
209:
210: sub end_vector {
211: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
212: my $result;
1.208 albertel 213: if ($target eq 'edit') {
214: $result = &Apache::edit::end_table();
215: }
1.195 albertel 216: return $result;
217: }
218:
1.208 albertel 219: sub insert_vector {
220: return '
221: <value></value>';
222: }
223:
1.181 albertel 224: sub start_array {
1.182 albertel 225: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.181 albertel 226: my $result;
227: if ( $target eq 'web' || $target eq 'tex' ||
228: $target eq 'grade' || $target eq 'webgrade' ||
229: $target eq 'answer' || $target eq 'analyze' ) {
1.182 albertel 230: my $bodytext = &Apache::lonxml::get_all_text("/array",$parser,$style);
1.181 albertel 231: my @values = &Apache::run::evaluate($bodytext,$safeeval,
232: $$parstack[-1]);
233: push(@{ $answer{$cur_name}{'answers'} },@values);
234: }
235: return $result;
236: }
237:
238: sub end_array {
239: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
240: my $result;
241: return $result;
242: }
243:
244: sub start_unit {
245: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
246: my $result;
247: return $result;
248: }
249:
250: sub end_unit {
251: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
252: my $result;
253: return $result;
254: }
255:
1.89 albertel 256: sub start_numericalresponse {
257: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.195 albertel 258: &Apache::lonxml::register('Apache::caparesponse',
259: ('answer','answergroup','value','array','unit',
260: 'vector'));
1.208 albertel 261: push(@Apache::lonxml::namespace,'caparesponse');
1.89 albertel 262: my $id = &Apache::response::start_response($parstack,$safeeval);
263: my $result;
1.181 albertel 264: undef(%answer);
265: undef(%{$safeeval->varglob('LONCAPA::CAPAresponse_args')});
1.89 albertel 266: if ($target eq 'edit') {
267: $result.=&Apache::edit::tag_start($target,$token);
268: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
269: if ($token->[1] eq 'numericalresponse') {
1.122 albertel 270: $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
1.149 www 271: $token).
272: &Apache::loncommon::help_open_topic('numerical_wrong_answers');
1.89 albertel 273: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
274: &Apache::loncommon::help_open_topic('Physical_Units');
275: $result.=&Apache::edit::text_arg('Format:','format',$token,4).
276: &Apache::loncommon::help_open_topic('Numerical_Response_Format');
277: } elsif ($token->[1] eq 'formularesponse') {
278: $result.=&Apache::edit::text_arg('Sample Points:','samples',
279: $token,40).
280: &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
281: }
282: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
283: } elsif ($target eq 'modified') {
284: my $constructtag;
285: if ($token->[1] eq 'numericalresponse') {
286: $constructtag=&Apache::edit::get_new_args($token,$parstack,
287: $safeeval,'answer',
1.156 albertel 288: 'incorrect','unit',
1.117 albertel 289: 'format');
1.89 albertel 290: } elsif ($token->[1] eq 'formularesponse') {
291: $constructtag=&Apache::edit::get_new_args($token,$parstack,
292: $safeeval,'answer',
293: 'samples');
294: }
295: if ($constructtag) {
296: $result = &Apache::edit::rebuild_tag($token);
297: $result.=&Apache::edit::handle_insert();
1.22 albertel 298: }
1.89 albertel 299: } elsif ($target eq 'meta') {
300: $result=&Apache::response::meta_package_write('numericalresponse');
301: } elsif ($target eq 'answer' || $target eq 'grade') {
302: &Apache::response::reset_params();
1.96 albertel 303: } elsif ($target eq 'web') {
304: my $partid = $Apache::inputtags::part;
305: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
306: &Apache::lonxml::debug("Got unit $hideunit for $partid $id");
307: #no way to enter units, with radio buttons
308: if (lc($hideunit) eq "yes") {
309: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
310: $safeeval);
311: if ($unit =~ /\S/) { $result.=" (in $unit) "; }
312: }
1.225 www 313: if (($token->[1] eq 'formularesponse') &&
1.227 raeburn 314: ($Apache::inputtags::status['-1'] eq 'CAN_ANSWER')) {
1.225 www 315: }
1.146 albertel 316: if ( &Apache::response::show_answer() ) {
1.195 albertel 317: &set_answertext($tag_internal_answer_name,$target,$token,$tagstack,
318: $parstack,$parser,$safeeval,-1);
1.194 albertel 319: }
320: }
321: return $result;
322: }
323:
324: sub set_answertext {
325: my ($name,$target,$token,$tagstack,$parstack,$parser,$safeeval,
326: $response_level) = @_;
327: &add_in_tag_answer($parstack,$safeeval,$response_level);
328:
1.206 albertel 329: if ($name eq '' || !ref($answer{$name})) {
330: if (ref($answer{$tag_internal_answer_name})) {
331: $name = $tag_internal_answer_name;
332: } else {
333: $name = (sort(keys(%answer)))[0];
334: }
335: }
1.194 albertel 336: return if ($name eq '' || !ref($answer{$name}));
337:
338: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
339: $safeeval,$response_level);
340: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval,
341: $response_level);
342:
343: &Apache::lonxml::debug("answer looks to be $name");
1.195 albertel 344: my @answertxt;
1.194 albertel 345: for (my $i=0; $i < scalar(@{$answer{$name}{'answers'}}); $i++) {
1.195 albertel 346: my $answertxt;
1.194 albertel 347: my $answer=$answer{$name}{'answers'}[$i];
1.195 albertel 348: foreach my $element (@$answer) {
349: if ( scalar(@$tagstack)
350: && $tagstack->[$response_level] ne 'numericalresponse') {
351: $answertxt.=$element.',';
1.194 albertel 352: } else {
1.195 albertel 353: my $format;
354: if ($#formats > 0) {
355: $format=$formats[$i];
356: } else {
357: $format=$formats[0];
358: }
359: if ($unit=~/\$/) { $format="\$".$format; $unit=~s/\$//g; }
360: if ($unit=~/\,/) { $format="\,".$format; $unit=~s/\,//g; }
361: my $formatted=&format_number($element,$format,$target,
362: $safeeval);
363: $answertxt.=' '.$formatted.',';
1.146 albertel 364: }
1.195 albertel 365:
366: }
367: chop($answertxt);
368: if ($target eq 'web') {
369: $answertxt.=" $unit ";
1.146 albertel 370: }
1.195 albertel 371:
372: push(@answertxt,$answertxt)
1.16 albertel 373: }
1.194 albertel 374:
375: my $id = $Apache::inputtags::response[-1];
1.195 albertel 376: $Apache::inputtags::answertxt{$id}=\@answertxt;
1.21 albertel 377: }
378:
1.195 albertel 379: sub setup_capa_args {
380: my ($safeeval,$parstack,$args,$response) = @_;
1.167 albertel 381: my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
1.195 albertel 382: undef(%{ $args_ref });
383:
384: foreach my $arg (@{$args}) {
1.167 albertel 385: $$args_ref{$arg}=
386: &Apache::lonxml::get_param($arg,$parstack,$safeeval);
387: }
388: foreach my $key (keys(%Apache::inputtags::params)) {
389: $$args_ref{$key}=$Apache::inputtags::params{$key};
390: }
1.195 albertel 391: &setup_capa_response($args_ref,$response);
392: return $args_ref;
393: }
394:
395: sub setup_capa_response {
396: my ($args_ref,$response) = @_;
397:
398: if (ref($response)) {
399: $$args_ref{'response'}=dclone($response);
400: } else {
401: $$args_ref{'response'}=dclone([$response]);
402: }
403: }
404:
405: sub check_submission {
406: my ($response,$partid,$id,$tag,$parstack,$safeeval,$ignore_sig)=@_;
407: my @args = ('type','tol','sig','format','unit','calc','samples');
408: my $args_ref = &setup_capa_args($safeeval,$parstack,\@args,$response);
409:
410: my $hideunit=
411: &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
412: #no way to enter units, with radio buttons
1.167 albertel 413: if ($Apache::lonhomework::type eq 'exam' ||
414: lc($hideunit) eq "yes") {
415: delete($$args_ref{'unit'});
416: }
417: #sig fig don't make much sense either
418: if (($Apache::lonhomework::type eq 'exam' ||
1.171 albertel 419: &Apache::response::submitted('scantron') ||
420: $ignore_sig) &&
1.167 albertel 421: $tag eq 'numericalresponse') {
422: delete($$args_ref{'sig'});
423: }
424:
425: if ($tag eq 'formularesponse') {
1.199 www 426: if ($$args_ref{'samples'}) {
1.191 www 427: $$args_ref{'type'}='fml';
1.199 www 428: } else {
429: $$args_ref{'type'}='math';
430: }
1.167 albertel 431: } elsif ($tag eq 'numericalresponse') {
432: $$args_ref{'type'}='float';
1.233 ! raeburn 433: } elsif ($tag eq 'stringresponse') {
! 434: if ($$args_ref{'type'} eq '') {
! 435: $$args_ref{'type'} = 'ci';
! 436: }
1.167 albertel 437: }
1.233 ! raeburn 438:
1.194 albertel 439: &add_in_tag_answer($parstack,$safeeval);
440:
1.218 albertel 441: if (!%answer) {
442: &Apache::lonxml::error("No answers are defined");
443: }
444:
1.195 albertel 445: my (@final_awards,@final_msgs,@names);
1.181 albertel 446: foreach my $name (keys(%answer)) {
447: &Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
1.194 albertel 448:
449: ${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$name});
1.195 albertel 450: &setup_capa_response($args_ref,$response);
451: use Time::HiRes;
452: my $t0 = [Time::HiRes::gettimeofday()];
1.181 albertel 453: my ($result,@msgs) =
1.233 ! raeburn 454: &Apache::run::run("&caparesponse_check_list()",$safeeval);
1.195 albertel 455: &Apache::lonxml::debug("checking $name $result with $response took ".&Time::HiRes::tv_interval($t0));
1.181 albertel 456: &Apache::lonxml::debug('msgs are '.join(':',@msgs));
457: my ($awards)=split(/:/,$result);
458: my @awards= split(/,/,$awards);
459: my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
460: push(@final_awards,$ad);
461: push(@final_msgs,$msg);
462: push(@names,$name);
463: }
464: my ($ad, $msg, $name) = &Apache::inputtags::finalizeawards(\@final_awards,
465: \@final_msgs,
466: \@names,1);
1.194 albertel 467: &Apache::lonxml::debug(" name of picked award is $name from ".join(', ',@names));
468: return($ad,$msg, $name);
469: }
470:
471: sub add_in_tag_answer {
472: my ($parstack,$safeeval,$response_level) = @_;
473: my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval,
474: $response_level);
475: &Apache::lonxml::debug('answer is'.join(':',@answer));
1.213 albertel 476: if (@answer && $answer[0] =~ /\S/) {
1.195 albertel 477: $answer{$tag_internal_answer_name}= {'type' => 'ordered',
478: 'answers' => [\@answer] };
1.194 albertel 479: }
1.167 albertel 480: }
481:
1.202 www 482: sub capa_formula_fix {
483: my ($expression)=@_;
484: return &Apache::response::implicit_multiplication($expression);
485: }
486:
1.89 albertel 487: sub end_numericalresponse {
488: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.208 albertel 489:
490: &Apache::lonxml::deregister('Apache::caparesponse',
491: ('answer','answergroup','value','array','unit',
492: 'vector'));
493: pop(@Apache::lonxml::namespace);
494:
1.90 albertel 495: my $increment=1;
1.89 albertel 496: my $result = '';
497: if (!$Apache::lonxml::default_homework_loaded) {
498: &Apache::lonxml::default_homework_load($safeeval);
1.34 albertel 499: }
1.167 albertel 500: my $partid = $Apache::inputtags::part;
501: my $id = $Apache::inputtags::response[-1];
1.125 albertel 502: my $tag;
1.190 albertel 503: my $safehole = new Safe::Hole;
1.168 albertel 504: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
1.190 albertel 505:
1.125 albertel 506: if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
1.162 albertel 507: if ( $target eq 'grade' && &Apache::response::submitted() ) {
1.140 albertel 508: &Apache::response::setup_params($tag,$safeeval);
1.95 albertel 509: if ($Apache::lonhomework::type eq 'exam' &&
1.190 albertel 510: (($tag eq 'formularesponse') || ($tag eq 'mathresponse'))) {
1.95 albertel 511: $increment=&Apache::response::scored_response($partid,$id);
512: } else {
513: my $response = &Apache::response::getresponse();
514: if ( $response =~ /[^\s]/) {
515: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
516: &Apache::lonxml::debug("submitted a $response<br>\n");
517: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
518:
1.162 albertel 519: if ( &Apache::response::submitted('scantron')) {
1.228 riegler 520: &add_in_tag_answer($parstack,$safeeval);
521: my ($values,$display)=&make_numerical_bubbles($partid,$id,
522: $target,$parstack,$safeeval);
523: $response=$values->[$response];
524: }
525: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1.194 albertel 526: my ($ad,$msg,$name)=&check_submission($response,$partid,$id,
527: $tag,$parstack,
528: $safeeval);
1.167 albertel 529:
1.142 albertel 530: &Apache::lonxml::debug('ad is'.$ad);
531: if ($ad eq 'SIG_FAIL') {
532: my ($sig_u,$sig_l)=
533: &get_sigrange($Apache::inputtags::params{'sig'});
534: $msg=join(':',$msg,$sig_l,$sig_u);
535: &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
536: $Apache::inputtags::params{'sig'});
537: }
1.95 albertel 538: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
1.163 albertel 539: if ($Apache::lonhomework::type eq 'survey' &&
540: ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
541: $ad eq 'EXACT_ANS')) {
542: $ad='SUBMITTED';
543: }
1.95 albertel 544: &Apache::response::handle_previous(\%previous,$ad);
545: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1.142 albertel 546: $Apache::lonhomework::results{"resource.$partid.$id.awardmsg"}=$msg;
1.194 albertel 547: $Apache::lonhomework::results{"resource.$partid.$id.answername"}=$name;
1.95 albertel 548: $result='';
1.90 albertel 549: }
1.89 albertel 550: }
551: } elsif ($target eq 'web' || $target eq 'tex') {
1.196 albertel 552: &check_for_answer_errors($parstack,$safeeval);
1.89 albertel 553: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
554: my $status = $Apache::inputtags::status['-1'];
555: if ($Apache::lonhomework::type eq 'exam') {
1.194 albertel 556: # FIXME support multi dimensional numerical problems
557: # in exam bubbles
1.184 albertel 558: my ($bubble_values,$bubble_display)=
559: &make_numerical_bubbles($partid,$id,$target,$parstack,
560: $safeeval);
561: my $number_of_bubbles = scalar(@{ $bubble_values });
1.89 albertel 562: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
563: $safeeval);
564: my @alphabet=('A'..'Z');
565: if ($target eq 'web') {
1.125 albertel 566: if ($tag eq 'numericalresponse') {
1.89 albertel 567: if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
568: $result.= '<table border="1"><tr>';
1.116 albertel 569: my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
1.90 albertel 570: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
1.116 albertel 571: my $checked='';
1.158 albertel 572: if ($previous eq $bubble_values->[$ind]) {
1.116 albertel 573: $checked=" checked='on' ";
574: }
1.90 albertel 575: $result.='<td><input type="radio" name="HWVAL_'.$id.
1.158 albertel 576: '" value="'.$bubble_values->[$ind].'" '.$checked
1.116 albertel 577: .' /><b>'.$alphabet[$ind].'</b>: '.
1.158 albertel 578: $bubble_display->[$ind].'</td>';
1.89 albertel 579: }
580: $result.='</tr></table>';
581: }
582: } elsif ($target eq 'tex') {
1.107 sakharuk 583: if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
1.89 albertel 584: $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
585: }
1.125 albertel 586: if ($tag eq 'numericalresponse') {
1.90 albertel 587: my ($celllength,$number_of_tables,@table_range)=
1.159 albertel 588: &get_table_sizes($number_of_bubbles,$bubble_display);
1.89 albertel 589: my $j=0;
590: my $cou=0;
591: $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
592: for (my $i=0;$i<$number_of_tables;$i++) {
1.144 sakharuk 593: $result.='\vskip -1 mm \noindent \setlength{\tabcolsep}{2 mm}\begin{tabular}{';
1.90 albertel 594: for (my $ind=0;$ind<$table_range[$j];$ind++) {
1.128 sakharuk 595: $result.='p{3 mm}p{'.$celllength.' mm}';
1.89 albertel 596: }
597: $result.='}';
1.90 albertel 598: for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
1.159 albertel 599: $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_display->[$ind].'} ';
1.89 albertel 600: if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
601: }
602: $cou += $table_range[$j];
603: $j++;
604: $result.='\\\\\end{tabular}\vskip 0 mm ';
605: }
606: $result.='\end{enumerate}';
607: } else {
1.188 albertel 608: $increment = &Apache::response::repetition();
1.89 albertel 609: }
610: }
611: }
1.228 riegler 612: if (($target eq 'web') && ($tag eq 'formularesponse')
1.231 riegler 613: && ($Apache::lonhomework::type ne 'exam') && ($Apache::inputtags::status['-1'] eq 'CAN_ANSWER')
614: && (&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoneditor') ne 'no')){
1.228 riegler 615: $result.=&Apache::response::edit_mathresponse_button($id,"HWVAL_$id");
1.231 riegler 616: #hier
1.228 riegler 617: }
618:
1.212 albertel 619: &Apache::response::setup_prior_tries_hash(\&format_prior_response_numerical);
1.89 albertel 620: } elsif ($target eq 'edit') {
621: $result.='</td></tr>'.&Apache::edit::end_table;
622: } elsif ($target eq 'answer' || $target eq 'analyze') {
1.167 albertel 623: my $part_id="$partid.$id";
1.89 albertel 624: if ($target eq 'analyze') {
625: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.125 albertel 626: $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
1.117 albertel 627: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
1.161 albertel 628: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
1.117 albertel 629: push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
1.154 albertel 630: &Apache::response::check_if_computed($token,$parstack,
631: $safeeval,'answer');
1.89 albertel 632: }
1.125 albertel 633: if (scalar(@$tagstack)) {
1.140 albertel 634: &Apache::response::setup_params($tag,$safeeval);
1.125 albertel 635: }
1.194 albertel 636: &add_in_tag_answer($parstack,$safeeval);
1.58 sakharuk 637: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
1.194 albertel 638:
1.58 sakharuk 639: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
1.89 albertel 640:
641: if ($target eq 'answer') {
1.195 albertel 642: $result.=&Apache::response::answer_header($tag,undef,
643: scalar(keys(%answer)));
1.185 albertel 644: if ($tag eq 'numericalresponse'
645: && $Apache::lonhomework::type eq 'exam') {
1.184 albertel 646: my ($bubble_values,undef,$correct) = &make_numerical_bubbles($partid,
647: $id,$target,$parstack,$safeeval);
648: $result.=&Apache::response::answer_part($tag,$correct);
649: }
1.89 albertel 650: }
1.194 albertel 651: foreach my $name (sort(keys(%answer))) {
652: my @answers = @{ $answer{$name}{'answers'} };
1.200 albertel 653: if ($target eq 'analyze') {
654: foreach my $info ('answer','ans_high','ans_low','format') {
655: $Apache::lonhomework::analyze{"$part_id.$info"}{$name}=[];
656: }
657: }
1.194 albertel 658: my ($sigline,$tolline);
1.195 albertel 659: if ($name ne $tag_internal_answer_name
660: || scalar(keys(%answer)) > 1) {
661: $result.=&Apache::response::answer_part($tag,$name);
662: }
1.194 albertel 663: for(my $i=0;$i<=$#answers;$i++) {
664: my $ans=$answers[$i];
665: my $fmt=$formats[0];
666: if (@formats && $#formats) {$fmt=$formats[$i];}
667: my ($sighigh,$siglow);
668: if ($Apache::inputtags::params{'sig'}) {
669: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
670: }
1.195 albertel 671: my @vector;
672: if (ref($ans)) {
673: @vector = @{ $ans };
674: } else {
675: @vector = ($ans);
1.194 albertel 676: }
1.200 albertel 677: my @all_answer_info;
678: foreach my $element (@vector) {
679: my ($high,$low);
680: if ($Apache::inputtags::params{'tol'}) {
681: ($high,$low)=&get_tolrange($element,$Apache::inputtags::params{'tol'});
682: }
683: if ($target eq 'answer') {
1.195 albertel 684: if ($fmt && $tag eq 'numericalresponse') {
685: $fmt=~s/e/E/g;
686: if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
687: if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
688: $element = &format_number($element,$fmt,$target,$safeeval);
689: #if ($high) {
690: # $high=&format_number($high,$fmt,$target,$safeeval);
691: # $low =&format_number($low,$fmt,$target,$safeeval);
692: #}
693: }
694: if ($high && $tag eq 'numericalresponse') {
1.224 www 695: $element.='; ['.$low.'; '.$high.']';
1.195 albertel 696: $tolline .= "[$low, $high]";
697: }
698: if (defined($sighigh) && $tag eq 'numericalresponse') {
699: if ($env{'form.answer_output_mode'} eq 'tex') {
1.224 www 700: $element.= "; Sig $siglow - $sighigh";
1.195 albertel 701: } else {
702: $element.= " Sig <i>$siglow - $sighigh</i>";
703: $sigline .= "[$siglow, $sighigh]";
704: }
1.194 albertel 705: }
1.195 albertel 706: push(@all_answer_info,$element);
1.200 albertel 707:
708: } elsif ($target eq 'analyze') {
709: push (@{ $Apache::lonhomework::analyze{"$part_id.answer"}{$name}[$i] }, $element);
710: if ($high) {
711: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"}{$name}[$i] }, $high);
712: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"}{$name}[$i] }, $low);
713: }
714: if ($fmt) {
715: push (@{ $Apache::lonhomework::analyze{"$part_id.format"}{$name}[$i] }, $fmt);
716: }
1.194 albertel 717: }
1.200 albertel 718: }
719: if ($target eq 'answer') {
1.223 www 720: $result.= &Apache::response::answer_part($tag,join('; ',@all_answer_info));
1.194 albertel 721: }
722: }
723:
724: my @fmt_ans;
725: for(my $i=0;$i<=$#answers;$i++) {
726: my $ans=$answers[$i];
727: my $fmt=$formats[0];
728: if (@formats && $#formats) {$fmt=$formats[$i];}
1.195 albertel 729: foreach my $element (@$ans) {
730: if ($fmt && $tag eq 'numericalresponse') {
731: $fmt=~s/e/E/g;
732: if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
733: if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
734: $element = &format_number($element,$fmt,$target,
735: $safeeval);
736: if ($fmt=~/\$/ && $unit!~/\$/) { $element=~s/\$//; }
737: }
1.194 albertel 738: }
1.195 albertel 739: push(@fmt_ans,join(',',@$ans));
1.194 albertel 740: }
1.195 albertel 741: my $response=\@fmt_ans;
742:
1.194 albertel 743: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.
744: $id.'.turnoffunit');
745: if ($unit ne '' &&
746: ! ($Apache::lonhomework::type eq 'exam' ||
747: lc($hideunit) eq "yes") ) {
748: my $cleanunit=$unit;
749: $cleanunit=~s/\$\,//g;
1.195 albertel 750: foreach my $ans (@fmt_ans) {
751: $ans.=" $cleanunit";
752: }
1.60 sakharuk 753: }
1.194 albertel 754: my ($ad,$msg)=&check_submission($response,$partid,$id,$tag,
755: $parstack,$safeeval);
756: if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
757: my $error;
758: if ($tag eq 'formularesponse') {
1.195 albertel 759: $error=&mt('Computer\'s answer is incorrect ("[_1]").',join(', ',@$response));
1.194 albertel 760: } else {
761: # answer failed check if it is sig figs that is failing
762: my ($ad,$msg)=&check_submission($response,$partid,$id,
763: $tag,$parstack,
764: $safeeval,1);
765: if ($sigline ne '') {
1.195 albertel 766: $error=&mt('Computer\'s answer is incorrect ("[_1]"). It is likely that the tolerance range [_2] or significant figures [_3] need to be adjusted.',join(', ',@$response),$tolline,$sigline);
1.98 sakharuk 767: } else {
1.195 albertel 768: $error=&mt('Computer\'s answer is incorrect ("[_1]"). It is likely that the tolerance range [_2] needs to be adjusted.',join(', ',@$response),$tolline);
1.98 sakharuk 769: }
770: }
1.194 albertel 771: if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
772: &Apache::lonxml::error($error);
773: } else {
774: &Apache::lonxml::warning($error);
1.165 albertel 775: }
1.79 sakharuk 776: }
1.176 albertel 777:
1.194 albertel 778: if (defined($unit) and ($unit ne '') and
779: $tag eq 'numericalresponse') {
780: if ($target eq 'answer') {
781: if ($env{'form.answer_output_mode'} eq 'tex') {
782: $result.=&Apache::response::answer_part($tag,
783: " Unit: $unit ");
784: } else {
785: $result.=&Apache::response::answer_part($tag,
786: "Unit: <b>$unit</b>");
787: }
788: } elsif ($target eq 'analyze') {
789: push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
1.171 albertel 790: }
1.167 albertel 791: }
1.194 albertel 792: if ($tag eq 'formularesponse' && $target eq 'answer') {
793: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
794: $result.=&Apache::response::answer_part($tag,$samples);
1.89 albertel 795: }
1.195 albertel 796: $result.=&Apache::response::next_answer($tag,$name);
1.89 albertel 797: }
798: if ($target eq 'answer') {
1.125 albertel 799: $result.=&Apache::response::answer_footer($tag);
1.89 albertel 800: }
1.69 sakharuk 801: }
1.121 sakharuk 802: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.90 albertel 803: $target eq 'tex' || $target eq 'analyze') {
1.221 raeburn 804: if (($tag eq 'formularesponse') && ($target eq 'analyze')) {
805: my $type = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.type');
806: if ($type eq 'exam') {
807: $increment = &Apache::response::repetition();
808: }
809: }
1.220 albertel 810: &Apache::lonxml::increment_counter($increment,"$partid.$id");
811: if ($target eq 'analyze') {
812: &Apache::lonhomework::set_bubble_lines();
813: }
1.90 albertel 814: }
1.196 albertel 815: &Apache::response::end_response();
1.89 albertel 816: return $result;
1.90 albertel 817: }
818:
1.212 albertel 819: sub format_prior_response_numerical {
820: my ($mode,$answer) = @_;
1.213 albertel 821: if (ref($answer)) {
822: my $result = '<table class="LC_prior_numerical"><tr>';
823: foreach my $element (@{ $answer }) {
824: $result.= '<td><span class="LC_prior_numerical">'.
825: &HTML::Entities::encode($element,'"<>&').'</span></td>';
826: }
827: $result.='</tr></table>';
828: return $result;
829: }
1.212 albertel 830: return '<span class="LC_prior_numerical">'.
831: &HTML::Entities::encode($answer,'"<>&').'</span>';
832:
1.209 albertel 833: }
834:
1.196 albertel 835: sub check_for_answer_errors {
836: my ($parstack,$safeeval) = @_;
837: &add_in_tag_answer($parstack,$safeeval);
838: my %counts;
839: foreach my $name (keys(%answer)) {
840: push(@{$counts{scalar(@{$answer{$name}{'answers'}})}},$name);
841: }
842: if (scalar(keys(%counts)) > 1) {
843: my $counts = join(' ',map {
844: my $count = $_;
845: &mt("Answers [_1] had [_2] components.",
846: '<tt>'.join(', ',@{$counts{$count}}).'</tt>',
847: $count);
848: } (sort(keys(%counts))));
849: &Apache::lonxml::error(&mt("All answers must have the same number of components. Varying numbers of answers were seen. ").$counts);
850: }
851: my $expected_number_of_inputs = (keys(%counts))[0];
1.214 albertel 852: if ( $expected_number_of_inputs > 0
853: && $expected_number_of_inputs != scalar(@Apache::inputtags::inputlist)) {
1.196 albertel 854: &Apache::lonxml::error(&mt("Expected [_1] input fields, but there were only [_2] seen.",
855: $expected_number_of_inputs,
856: scalar(@Apache::inputtags::inputlist)));
857: }
858: }
859:
1.90 albertel 860: sub get_table_sizes {
1.128 sakharuk 861: my ($number_of_bubbles,$rbubble_values)=@_;
862: my $scale=2; #mm for one digit
863: my $cell_width=0;
864: foreach my $member (@$rbubble_values) {
865: my $cell_width_real=0;
1.160 albertel 866: if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^{(\+|-)?(\d+)}\$?/) {
1.138 sakharuk 867: $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale;
868: } elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
1.128 sakharuk 869: $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
1.138 sakharuk 870: } elsif ($member=~/(\d*)\.(\d*)/) {
1.132 sakharuk 871: $cell_width_real=(length($1)+length($2)+3)*$scale;
1.128 sakharuk 872: } else {
1.138 sakharuk 873: $cell_width_real=(length($member)+1)*$scale*0.9;
1.128 sakharuk 874: }
875: if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
1.90 albertel 876: }
1.132 sakharuk 877: $cell_width+=8;
1.130 sakharuk 878: my $textwidth;
1.166 albertel 879: if ($env{'form.textwidth'} ne '') {
880: $env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
1.132 sakharuk 881: $textwidth=$1.'.'.$2;
1.130 sakharuk 882: } else {
1.166 albertel 883: $env{'form.textwidth'}=~/(\d+)\.?(\d*)/;
1.132 sakharuk 884: $textwidth=$1.'.'.$2;
1.130 sakharuk 885: }
1.128 sakharuk 886: my $bubbles_per_line=int($textwidth/$cell_width);
1.151 sakharuk 887: if ($bubbles_per_line > $number_of_bubbles) {
888: $bubbles_per_line=$number_of_bubbles;
1.216 albertel 889: } elsif (($bubbles_per_line > $number_of_bubbles/2)
890: && ($number_of_bubbles % 2==0)) {
891: $bubbles_per_line=$number_of_bubbles/2;
892: }
893: if ($bubbles_per_line < 1) {
894: $bubbles_per_line=1;
895: }
1.128 sakharuk 896: my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
1.90 albertel 897: my @table_range = ();
1.128 sakharuk 898: for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
899: if ($number_of_bubbles % $bubbles_per_line) {
1.90 albertel 900: $number_of_tables++;
1.128 sakharuk 901: push @table_range,($number_of_bubbles % $bubbles_per_line);
1.90 albertel 902: }
1.128 sakharuk 903: $cell_width-=8;
1.136 sakharuk 904: $cell_width=$cell_width*3/4;
1.128 sakharuk 905: return ($cell_width,$number_of_tables,@table_range);
1.90 albertel 906: }
907:
908: sub format_number {
1.153 albertel 909: my ($number,$format,$target,$safeeval)=@_;
1.90 albertel 910: my $ans;
1.153 albertel 911: if ($format eq '') {
1.90 albertel 912: #What is the number? (integer,decimal,floating point)
1.187 albertel 913: if ($number=~/^(\d*\.?\d*)(E|e)[+\-]?(\d*)$/) {
1.113 sakharuk 914: $format = '3e';
1.90 albertel 915: } elsif ($number=~/^(\d*)\.(\d*)$/) {
916: $format = '4f';
917: } elsif ($number=~/^(\d*)$/) {
918: $format = 'd';
919: }
920: }
1.156 albertel 921: if (!$Apache::lonxml::default_homework_loaded) {
922: &Apache::lonxml::default_homework_load($safeeval);
923: }
1.153 albertel 924: $ans=&Apache::run::run("&prettyprint(q\0$number\0,q\0$format\0,q\0$target\0)",$safeeval);
1.90 albertel 925: return $ans;
926: }
927:
928: sub make_numerical_bubbles {
1.184 albertel 929: my ($part,$id,$target,$parstack,$safeeval) =@_;
1.215 albertel 930:
931: if (!%answer) {
932: &Apache::lonxml::error(&mt("No answers defined for response [_1] in part [_2] to make bubbles for.",$id,$part));
933: return ([],[],undef);
934: }
1.184 albertel 935:
936: my $number_of_bubbles =
937: &Apache::response::get_response_param($part.'_'.$id,'numbubbles',8);
938:
939: my ($format)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
1.201 albertel 940: my $name = (exists($answer{$tag_internal_answer_name})
941: ? $tag_internal_answer_name
942: : (sort(keys(%answer)))[0]);
943:
944: if ( scalar(@{$answer{$name}{'answers'}}) > 1) {
945: &Apache::lonxml::error("Only answers with 1 component are supported in exam mode");
946: }
947: if (scalar(@{$answer{$name}{'answers'}[0]}) > 1) {
948: &Apache::lonxml::error("Vector answers are unsupported in exam mode.");
949: }
950:
951: my $answer = $answer{$name}{'answers'}[0][0];
1.184 albertel 952: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,
953: $safeeval);
954: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
955:
1.158 albertel 956: my @bubble_values=();
1.184 albertel 957: my @alphabet=('A'..'Z');
958:
959: &Apache::lonxml::debug("answer is $answer incorrect is @incorrect");
1.119 albertel 960: my @oldseed=&Math::Random::random_get_seed();
1.184 albertel 961: if (@incorrect) {
962: &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1 gt $number_of_bubbles));
963: if (defined($incorrect[0]) &&
964: scalar(@incorrect)+1 >= $number_of_bubbles) {
965: &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1).":$number_of_bubbles");
1.117 albertel 966: &Apache::response::setrandomnumber();
1.184 albertel 967: my @rand_inc=&Math::Random::random_permutation(@incorrect);
1.117 albertel 968: @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
969: @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
1.184 albertel 970: &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": which are :".join(':',@bubble_values));
1.119 albertel 971: &Math::Random::random_set_seed(@oldseed);
1.184 albertel 972:
973: my $correct;
974: for(my $i=0; $i<=$#bubble_values;$i++) {
975: if ($bubble_values[$i] eq $answer) {
976: $correct = $alphabet[$i];
977: last;
978: }
979: }
980:
1.134 albertel 981: if (defined($format) && $format ne '') {
1.158 albertel 982: my @bubble_display;
1.137 albertel 983: foreach my $value (@bubble_values) {
1.158 albertel 984: push(@bubble_display,
985: &format_number($value,$format,$target,$safeeval));
1.134 albertel 986: }
1.184 albertel 987: return (\@bubble_values,\@bubble_display,$correct);
1.158 albertel 988: } else {
1.184 albertel 989: return (\@bubble_values,\@bubble_values,$correct);
1.134 albertel 990: }
1.117 albertel 991: }
1.184 albertel 992: if (defined($incorrect[0]) &&
993: scalar(@incorrect)+1 < $number_of_bubbles) {
994: &Apache::lonxml::warning("Not enough incorrect answers were specified in the incorrect array, ignoring the specified incorrect answers and instead generating them (".join(',',@incorrect).").");
1.127 albertel 995: }
1.117 albertel 996: }
1.90 albertel 997: my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
1.126 albertel 998: my @powers = (1..$number_of_bubbles);
1.90 albertel 999: &Apache::response::setrandomnumber();
1000: my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
1001: my $power = $powers[$ind];
1002: $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
1003: my $factor = $factors[$ind];
1.158 albertel 1004: my @bubble_display;
1.222 www 1005: my $answerfactor=$answer;
1006: if ($answer==0) {
1007: $answerfactor=&Math::Random::random_uniform_integer(1,1,100)/
1008: &Math::Random::random_uniform_integer(1,1,10);
1009: }
1.90 albertel 1010: for ($ind=0;$ind<$number_of_bubbles;$ind++) {
1.222 www 1011: $bubble_values[$ind] = $answerfactor*($factor**($power-$powers[$#powers-$ind]));
1.158 albertel 1012: $bubble_display[$ind] = &format_number($bubble_values[$ind],
1.153 albertel 1013: $format,$target,$safeeval);
1.90 albertel 1014: }
1.184 albertel 1015: my $correct = $alphabet[$number_of_bubbles-$power];
1.222 www 1016: if ($answer==0) {
1017: $correct='A';
1018: $bubble_values[0]=0;
1019: $bubble_display[0] = &format_number($bubble_values[0],
1020: $format,$target,$safeeval);
1021: }
1.119 albertel 1022: &Math::Random::random_set_seed(@oldseed);
1.184 albertel 1023: return (\@bubble_values,\@bubble_display,$correct);
1.51 albertel 1024: }
1025:
1026: sub get_tolrange {
1.89 albertel 1027: my ($ans,$tol)=@_;
1028: my ($high,$low);
1029: if ($tol =~ /%$/) {
1030: chop($tol);
1031: my $change=$ans*($tol/100.0);
1032: $high=$ans+$change;
1033: $low=$ans-$change;
1034: } else {
1035: $high=$ans+$tol;
1036: $low=$ans-$tol;
1037: }
1038: return ($high,$low);
1.52 albertel 1039: }
1040:
1041: sub get_sigrange {
1.89 albertel 1042: my ($sig)=@_;
1.195 albertel 1043: #&Apache::lonxml::debug("Got a sig of :$sig:");
1.166 albertel 1044: my $courseid=$env{'request.course.id'};
1.219 albertel 1045: if ($env{'request.state'} ne 'construct'
1046: && lc($env{"course.$courseid.disablesigfigs"}) eq 'yes') {
1.147 albertel 1047: return (15,0);
1048: }
1.89 albertel 1049: my $sig_lbound;
1050: my $sig_ubound;
1051: if ($sig eq '') {
1052: $sig_lbound = 0; #SIG_LB_DEFAULT
1053: $sig_ubound =15; #SIG_UB_DEFAULT
1054: } else {
1055: ($sig_lbound,$sig_ubound) = split(/,/,$sig);
1.145 albertel 1056: if (!defined($sig_lbound)) {
1.89 albertel 1057: $sig_lbound = 0; #SIG_LB_DEFAULT
1058: $sig_ubound =15; #SIG_UB_DEFAULT
1059: }
1.145 albertel 1060: if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
1.133 albertel 1061: }
1062: if (($sig_ubound<$sig_lbound) ||
1063: ($sig_lbound > 15) ||
1064: ($sig =~/(\+|-)/ ) ) {
1065: my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
1.166 albertel 1066: if ($env{'request.state'} eq 'construct') {
1.133 albertel 1067: $errormsg.=
1068: &Apache::loncommon::help_open_topic('Significant_Figures');
1069: }
1070: &Apache::lonxml::error($errormsg);
1.52 albertel 1071: }
1.89 albertel 1072: return ($sig_ubound,$sig_lbound);
1.34 albertel 1073: }
1074:
1.212 albertel 1075: sub format_prior_response_string {
1076: my ($mode,$answer) =@_;
1077: return '<span class="LC_prior_string">'.
1078: &HTML::Entities::encode($answer,'"<>&').'</span>';
1.211 albertel 1079: }
1080:
1.34 albertel 1081: sub start_stringresponse {
1.89 albertel 1082: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1083: my $result;
1.122 albertel 1084: my $id = &Apache::response::start_response($parstack,$safeeval);
1.89 albertel 1085: if ($target eq 'meta') {
1086: $result=&Apache::response::meta_package_write('stringresponse');
1.122 albertel 1087: } elsif ($target eq 'edit') {
1088: $result.=&Apache::edit::tag_start($target,$token);
1089: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
1090: $result.=&Apache::edit::select_arg('Type:','type',
1091: [['cs','Case Sensitive'],['ci','Case Insensitive'],
1092: ['mc','Case Insensitive, Any Order'],
1093: ['re','Regular Expression']],$token);
1.152 albertel 1094: $result.=&Apache::edit::text_arg('String to display for answer:',
1095: 'answerdisplay',$token);
1.122 albertel 1096: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1097: } elsif ($target eq 'modified') {
1.146 albertel 1098: my $constructtag;
1099: $constructtag=&Apache::edit::get_new_args($token,$parstack,
1100: $safeeval,'answer',
1101: 'type','answerdisplay');
1102: if ($constructtag) {
1103: $result = &Apache::edit::rebuild_tag($token);
1104: $result.=&Apache::edit::handle_insert();
1105: }
1106: } elsif ($target eq 'web') {
1107: if ( &Apache::response::show_answer() ) {
1.152 albertel 1108: my $answer=
1109: &Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
1110: if (!defined $answer || $answer eq '') {
1111: $answer=
1112: &Apache::lonxml::get_param('answer',$parstack,$safeeval);
1113: }
1.195 albertel 1114: $Apache::inputtags::answertxt{$id}=[$answer];
1.146 albertel 1115: }
1.122 albertel 1116: } elsif ($target eq 'answer' || $target eq 'grade') {
1117: &Apache::response::reset_params();
1.89 albertel 1118: }
1119: return $result;
1.34 albertel 1120: }
1121:
1122: sub end_stringresponse {
1.122 albertel 1123: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.189 albertel 1124:
1.122 albertel 1125: my $result = '';
1126: my $part=$Apache::inputtags::part;
1127: my $id=$Apache::inputtags::response[-1];
1128: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
1129: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
1.129 www 1130: my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
1.122 albertel 1131: &Apache::lonxml::debug("current $answer ".$token->[2]);
1132: if (!$Apache::lonxml::default_homework_loaded) {
1133: &Apache::lonxml::default_homework_load($safeeval);
1134: }
1.162 albertel 1135: if ( $target eq 'grade' && &Apache::response::submitted() ) {
1.140 albertel 1136: &Apache::response::setup_params('stringresponse',$safeeval);
1.122 albertel 1137: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
1138: if ($Apache::lonhomework::type eq 'exam' ||
1.162 albertel 1139: &Apache::response::submitted('scantron')) {
1.189 albertel 1140: &Apache::response::scored_response($part,$id);
1141:
1.122 albertel 1142: } else {
1143: my $response = &Apache::response::getresponse();
1144: if ( $response =~ /[^\s]/) {
1145: my %previous = &Apache::response::check_for_previous($response,
1146: $part,$id);
1147: &Apache::lonxml::debug("submitted a $response<br>\n");
1148: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
1149: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
1150: $response;
1.142 albertel 1151: my ($ad,$msg);
1.122 albertel 1152: if ($type eq 're' ) {
1153: # if the RE wasn't in a var it likely got munged,
1154: # thus grab it from the var directly
1155: # my $testans=$token->[2]->{'answer'};
1156: # if ($testans !~ m/^\s*\$/) {
1157: # $answer=$token->[2]->{'answer'};
1158: # }
1.143 albertel 1159: ${$safeeval->varglob('LONCAPA::response')}=$response;
1.179 albertel 1160: $result = &Apache::run::run('if ($LONCAPA::response=~m'.$answer.') { return 1; } else { return 0; }',$safeeval);
1.122 albertel 1161: &Apache::lonxml::debug("current $response");
1162: &Apache::lonxml::debug("current $answer");
1163: $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
1164: } else {
1.195 albertel 1165: my @args = ('type');
1166: my $args_ref = &setup_capa_args($safeeval,$parstack,
1167: \@args,$response);
1.233 ! raeburn 1168: if ($$args_ref{'type'} eq '') {
! 1169: $$args_ref{'type'} = 'ci';
! 1170: }
1.195 albertel 1171: &add_in_tag_answer($parstack,$safeeval);
1172: my (@final_awards,@final_msgs,@names);
1173: foreach my $name (keys(%answer)) {
1174: &Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
1175: ${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$name});
1.233 ! raeburn 1176: my ($result, @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
1.195 albertel 1177: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
1178: my ($awards)=split(/:/,$result);
1179: my (@awards) = split(/,/,$awards);
1180: ($ad,$msg) =
1181: &Apache::inputtags::finalizeawards(\@awards,\@msgs);
1182: push(@final_awards,$ad);
1183: push(@final_msgs,$msg);
1184: push(@names,$name);
1185: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
1.122 albertel 1186: }
1.195 albertel 1187: my ($ad, $msg, $name) =
1188: &Apache::inputtags::finalizeawards(\@final_awards,
1189: \@final_msgs,
1190: \@names,1);
1.122 albertel 1191: }
1.163 albertel 1192: if ($Apache::lonhomework::type eq 'survey' &&
1193: ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
1194: $ad eq 'EXACT_ANS')) {
1195: $ad='SUBMITTED';
1196: }
1.122 albertel 1197: &Apache::response::handle_previous(\%previous,$ad);
1198: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
1.142 albertel 1199: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
1.122 albertel 1200: }
1201: }
1202: } elsif ($target eq 'answer' || $target eq 'analyze') {
1.200 albertel 1203: &add_in_tag_answer($parstack,$safeeval);
1.122 albertel 1204: if ($target eq 'analyze') {
1205: push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
1.125 albertel 1206: $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
1.154 albertel 1207: &Apache::response::check_if_computed($token,$parstack,$safeeval,
1208: 'answer');
1.122 albertel 1209: }
1.140 albertel 1210: &Apache::response::setup_params('stringresponse',$safeeval);
1.122 albertel 1211: if ($target eq 'answer') {
1.125 albertel 1212: $result.=&Apache::response::answer_header('stringresponse');
1.122 albertel 1213: }
1.200 albertel 1214: foreach my $name (keys(%answer)) {
1215: my @answers = @{ $answer{$name}{'answers'} };
1216: for (my $i=0;$i<=$#answers;$i++) {
1217: my $answer_part = $answers[$i];
1218: foreach my $element (@{$answer_part}) {
1219: if ($target eq 'answer') {
1220: $result.=&Apache::response::answer_part('stringresponse',
1221: $element);
1222: } elsif ($target eq 'analyze') {
1223: push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"}{$name}[$i] },
1224: $element);
1225: }
1226: }
1227: if ($target eq 'answer' && $type eq 're') {
1.178 albertel 1228: $result.=&Apache::response::answer_part('stringresponse',
1229: $answerdisplay);
1230: }
1.122 albertel 1231: }
1.200 albertel 1232: }
1.122 albertel 1233: my $string='Case Insensitive';
1234: if ($type eq 'mc') {
1235: $string='Multiple Choice';
1236: } elsif ($type eq 'cs') {
1237: $string='Case Sensitive';
1238: } elsif ($type eq 'ci') {
1239: $string='Case Insensitive';
1240: } elsif ($type eq 're') {
1241: $string='Regular Expression';
1242: }
1243: if ($target eq 'answer') {
1.166 albertel 1244: if ($env{'form.answer_output_mode'} eq 'tex') {
1.125 albertel 1245: $result.=&Apache::response::answer_part('stringresponse',
1.122 albertel 1246: "$string");
1247: } else {
1.125 albertel 1248: $result.=&Apache::response::answer_part('stringresponse',
1.122 albertel 1249: "<b>$string</b>");
1250: }
1251: } elsif ($target eq 'analyze') {
1252: push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
1253: $type);
1254: }
1255: if ($target eq 'answer') {
1.125 albertel 1256: $result.=&Apache::response::answer_footer('stringresponse');
1.122 albertel 1257: }
1258: } elsif ($target eq 'edit') {
1259: $result.='</td></tr>'.&Apache::edit::end_table;
1.211 albertel 1260: } elsif ($target eq 'web' || $target eq 'tex') {
1.212 albertel 1261: &Apache::response::setup_prior_tries_hash(\&format_prior_response_string);
1.122 albertel 1262: }
1263: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1264: $target eq 'tex' || $target eq 'analyze') {
1.220 albertel 1265: &Apache::lonxml::increment_counter(&Apache::response::repetition(),
1266: "$part.$id");
1267: if ($target eq 'analyze') {
1268: &Apache::lonhomework::set_bubble_lines();
1269: }
1.122 albertel 1270: }
1271: &Apache::response::end_response;
1272: return $result;
1.44 albertel 1273: }
1274:
1275: sub start_formularesponse {
1.89 albertel 1276: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1277: my $result;
1278: if ($target eq 'meta') {
1.104 bowersj2 1279: &Apache::response::start_response($parstack,$safeeval);
1.89 albertel 1280: $result=&Apache::response::meta_package_write('formularesponse');
1.104 bowersj2 1281: &Apache::response::end_response();
1.89 albertel 1282: } else {
1283: $result.=&start_numericalresponse(@_);
1284: }
1285: return $result;
1.44 albertel 1286: }
1287:
1288: sub end_formularesponse {
1.89 albertel 1289: return end_numericalresponse(@_);
1.3 albertel 1290: }
1291:
1.1 albertel 1292: 1;
1.3 albertel 1293: __END__
1.89 albertel 1294:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>