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