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