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