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