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