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