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