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