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