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