1: # The LearningOnline Network with CAPA
2: # caparesponse definition
3: #
4: # $Id: caparesponse.pm,v 1.193 2006/07/03 14:21:45 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'));
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: # if ($$args_ref{'samples'}) {
253: $$args_ref{'type'}='fml';
254: # } else {
255: # $$args_ref{'type'}='math';
256: # }
257: } elsif ($tag eq 'numericalresponse') {
258: $$args_ref{'type'}='float';
259: }
260: my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
261: &Apache::lonxml::debug('answer is'.join(':',@answer));
262: if (@answer && defined($answer[0])) {
263: $answer{'INTERNAL'}= {'type' => 'ordered',
264: 'answers' => \@answer };
265: }
266: #FIXME would be nice if we could save name so we know who graded him
267: #correct
268: my (%results,@final_awards,@final_msgs,@names);
269: foreach my $name (keys(%answer)) {
270: &Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
271: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=@{ $answer{$name}{'answers'} };
272: my ($result,@msgs) =
273: &Apache::run::run("&caparesponse_check_list()",$safeeval);
274: &Apache::lonxml::debug('msgs are '.join(':',@msgs));
275: my ($awards)=split(/:/,$result);
276: my @awards= split(/,/,$awards);
277: my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
278: $results{$name}= [$ad,$msg];
279: push(@final_awards,$ad);
280: push(@final_msgs,$msg);
281: push(@names,$name);
282: }
283: my ($ad, $msg, $name) = &Apache::inputtags::finalizeawards(\@final_awards,
284: \@final_msgs,
285: \@names,1);
286: return($ad,$msg);
287: }
288:
289: sub end_numericalresponse {
290: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
291: my $increment=1;
292: my $result = '';
293: if (!$Apache::lonxml::default_homework_loaded) {
294: &Apache::lonxml::default_homework_load($safeeval);
295: }
296: my $partid = $Apache::inputtags::part;
297: my $id = $Apache::inputtags::response[-1];
298: my $tag;
299: my $safehole = new Safe::Hole;
300: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
301: $safehole->wrap(\&Apache::lonmaxima::maxima_check,$safeeval,'&maxima_check');
302:
303: if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
304: if ( $target eq 'grade' && &Apache::response::submitted() ) {
305: &Apache::response::setup_params($tag,$safeeval);
306: if ($Apache::lonhomework::type eq 'exam' &&
307: (($tag eq 'formularesponse') || ($tag eq 'mathresponse'))) {
308: $increment=&Apache::response::scored_response($partid,$id);
309: } else {
310: my $response = &Apache::response::getresponse();
311: if ( $response =~ /[^\s]/) {
312: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
313: &Apache::lonxml::debug("submitted a $response<br>\n");
314: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
315:
316: if ( &Apache::response::submitted('scantron')) {
317: my ($values,$display)=&make_numerical_bubbles($partid,$id,
318: $target,$parstack,$safeeval);
319: $response=$values->[$response];
320: }
321: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
322: my ($ad,$msg)=&check_submission($response,$partid,$id,
323: $tag,$parstack,$safeeval);
324:
325: &Apache::lonxml::debug('ad is'.$ad);
326: if ($ad eq 'SIG_FAIL') {
327: my ($sig_u,$sig_l)=
328: &get_sigrange($Apache::inputtags::params{'sig'});
329: $msg=join(':',$msg,$sig_l,$sig_u);
330: &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
331: $Apache::inputtags::params{'sig'});
332: }
333: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
334: if ($Apache::lonhomework::type eq 'survey' &&
335: ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
336: $ad eq 'EXACT_ANS')) {
337: $ad='SUBMITTED';
338: }
339: &Apache::response::handle_previous(\%previous,$ad);
340: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
341: $Apache::lonhomework::results{"resource.$partid.$id.awardmsg"}=$msg;
342: $result='';
343: }
344: }
345: } elsif ($target eq 'web' || $target eq 'tex') {
346: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
347: $safeeval);
348: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
349: my $status = $Apache::inputtags::status['-1'];
350: if ($Apache::lonhomework::type eq 'exam') {
351: my ($bubble_values,$bubble_display)=
352: &make_numerical_bubbles($partid,$id,$target,$parstack,
353: $safeeval);
354: my $number_of_bubbles = scalar(@{ $bubble_values });
355: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
356: $safeeval);
357: my @alphabet=('A'..'Z');
358: if ($target eq 'web') {
359: if ($tag eq 'numericalresponse') {
360: if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
361: $result.= '<table border="1"><tr>';
362: my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
363: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
364: my $checked='';
365: if ($previous eq $bubble_values->[$ind]) {
366: $checked=" checked='on' ";
367: }
368: $result.='<td><input type="radio" name="HWVAL_'.$id.
369: '" value="'.$bubble_values->[$ind].'" '.$checked
370: .' /><b>'.$alphabet[$ind].'</b>: '.
371: $bubble_display->[$ind].'</td>';
372: }
373: $result.='</tr></table>';
374: }
375: } elsif ($target eq 'tex') {
376: if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
377: $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
378: }
379: if ($tag eq 'numericalresponse') {
380: my ($celllength,$number_of_tables,@table_range)=
381: &get_table_sizes($number_of_bubbles,$bubble_display);
382: my $j=0;
383: my $cou=0;
384: $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
385: for (my $i=0;$i<$number_of_tables;$i++) {
386: $result.='\vskip -1 mm \noindent \setlength{\tabcolsep}{2 mm}\begin{tabular}{';
387: for (my $ind=0;$ind<$table_range[$j];$ind++) {
388: $result.='p{3 mm}p{'.$celllength.' mm}';
389: }
390: $result.='}';
391: for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
392: $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_display->[$ind].'} ';
393: if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
394: }
395: $cou += $table_range[$j];
396: $j++;
397: $result.='\\\\\end{tabular}\vskip 0 mm ';
398: }
399: $result.='\end{enumerate}';
400: } else {
401: $increment = &Apache::response::repetition();
402: }
403: }
404: }
405: } elsif ($target eq 'edit') {
406: $result.='</td></tr>'.&Apache::edit::end_table;
407: } elsif ($target eq 'answer' || $target eq 'analyze') {
408: my $part_id="$partid.$id";
409: if ($target eq 'analyze') {
410: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
411: $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
412: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
413: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
414: push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
415: &Apache::response::check_if_computed($token,$parstack,
416: $safeeval,'answer');
417: }
418: if (scalar(@$tagstack)) {
419: &Apache::response::setup_params($tag,$safeeval);
420: }
421: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
422: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
423: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
424: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
425:
426: if ($target eq 'answer') {
427: $result.=&Apache::response::answer_header($tag);
428: if ($tag eq 'numericalresponse'
429: && $Apache::lonhomework::type eq 'exam') {
430: my ($bubble_values,undef,$correct) = &make_numerical_bubbles($partid,
431: $id,$target,$parstack,$safeeval);
432: $result.=&Apache::response::answer_part($tag,$correct);
433: }
434: }
435: my ($sigline,$tolline);
436: for(my $i=0;$i<=$#answers;$i++) {
437: my $ans=$answers[$i];
438: my $fmt=$formats[0];
439: if (@formats && $#formats) {$fmt=$formats[$i];}
440: my ($high,$low);
441: if ($Apache::inputtags::params{'tol'}) {
442: ($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
443: }
444: my ($sighigh,$siglow);
445: if ($Apache::inputtags::params{'sig'}) {
446: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
447: }
448: if ($fmt && $tag eq 'numericalresponse') {
449: $fmt=~s/e/E/g;
450: if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
451: if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
452: $ans = &format_number($ans,$fmt,$target,$safeeval);
453: #if ($high) {
454: # $high=&format_number($high,$fmt,$target,$safeeval);
455: # $low =&format_number($low,$fmt,$target,$safeeval);
456: #}
457: }
458: if ($target eq 'answer') {
459: if ($high && $tag eq 'numericalresponse') {
460: $ans.=' ['.$low.','.$high.']';
461: $tolline .= "[$low, $high]";
462: }
463: if (defined($sighigh) && $tag eq 'numericalresponse') {
464: if ($env{'form.answer_output_mode'} eq 'tex') {
465: $ans.= " Sig $siglow - $sighigh";
466: } else {
467: $ans.= " Sig <i>$siglow - $sighigh</i>";
468: $sigline .= "[$siglow, $sighigh]";
469: }
470: }
471: $result.=&Apache::response::answer_part($tag,$ans);
472: } elsif ($target eq 'analyze') {
473: push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
474: if ($high) {
475: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
476: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
477: }
478: if ($fmt) {
479: push (@{ $Apache::lonhomework::analyze{"$part_id.format"} }, $fmt);
480: }
481: }
482: }
483:
484: my @fmt_ans;
485: for(my $i=0;$i<=$#answers;$i++) {
486: my $ans=$answers[$i];
487: my $fmt=$formats[0];
488: if (@formats && $#formats) {$fmt=$formats[$i];}
489: if ($fmt && $tag eq 'numericalresponse') {
490: $fmt=~s/e/E/g;
491: if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
492: if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
493: $ans = &format_number($ans,$fmt,$target,$safeeval);
494: if ($fmt=~/\$/ && $unit!~/\$/) { $ans=~s/\$//; }
495: }
496: push(@fmt_ans,$ans);
497: }
498: my $response=join(', ',@fmt_ans);
499: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.
500: $id.'.turnoffunit');
501: if ($unit ne '' &&
502: ! ($Apache::lonhomework::type eq 'exam' ||
503: lc($hideunit) eq "yes") ) {
504: my $cleanunit=$unit;
505: $cleanunit=~s/\$\,//g;
506: $response.=" $cleanunit";
507: }
508:
509: my ($ad,$msg)=&check_submission($response,$partid,$id,$tag,
510: $parstack,$safeeval);
511: if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
512: my $error;
513: if ($tag eq 'formularesponse') {
514: $error=&mt('Computer\'s answer is incorrect ("[_1]").',$response);
515: } else {
516: # answer failed check if it is sig figs that is failing
517: my ($ad,$msg)=&check_submission($response,$partid,$id,
518: $tag,$parstack,
519: $safeeval,1);
520: if ($sigline ne '') {
521: $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);
522: } else {
523: $error=&mt('Computer\'s answer is incorrect ("[_1]"). It is likely that the tolerance range [_2] needs to be adjusted.',$response,$tolline);
524: }
525: }
526: if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
527: &Apache::lonxml::error($error);
528: } else {
529: &Apache::lonxml::warning($error);
530: }
531: }
532:
533: if (defined($unit) and ($unit ne '') and
534: $tag eq 'numericalresponse') {
535: if ($target eq 'answer') {
536: if ($env{'form.answer_output_mode'} eq 'tex') {
537: $result.=&Apache::response::answer_part($tag,
538: " Unit: $unit ");
539: } else {
540: $result.=&Apache::response::answer_part($tag,
541: "Unit: <b>$unit</b>");
542: }
543: } elsif ($target eq 'analyze') {
544: push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
545: }
546: }
547: if ($tag eq 'formularesponse' && $target eq 'answer') {
548: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
549: $result.=&Apache::response::answer_part($tag,$samples);
550: }
551: if ($target eq 'answer') {
552: $result.=&Apache::response::answer_footer($tag);
553: }
554: }
555: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
556: $target eq 'tex' || $target eq 'analyze') {
557: &Apache::lonxml::increment_counter($increment);
558: }
559: &Apache::response::end_response;
560: return $result;
561: }
562:
563: sub get_table_sizes {
564: my ($number_of_bubbles,$rbubble_values)=@_;
565: my $scale=2; #mm for one digit
566: my $cell_width=0;
567: foreach my $member (@$rbubble_values) {
568: my $cell_width_real=0;
569: if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^{(\+|-)?(\d+)}\$?/) {
570: $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale;
571: } elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
572: $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
573: } elsif ($member=~/(\d*)\.(\d*)/) {
574: $cell_width_real=(length($1)+length($2)+3)*$scale;
575: } else {
576: $cell_width_real=(length($member)+1)*$scale*0.9;
577: }
578: if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
579: }
580: $cell_width+=8;
581: my $textwidth;
582: if ($env{'form.textwidth'} ne '') {
583: $env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
584: $textwidth=$1.'.'.$2;
585: } else {
586: $env{'form.textwidth'}=~/(\d+)\.?(\d*)/;
587: $textwidth=$1.'.'.$2;
588: }
589: my $bubbles_per_line=int($textwidth/$cell_width);
590: if ($bubbles_per_line > $number_of_bubbles) {
591: $bubbles_per_line=$number_of_bubbles;
592: }elsif (($bubbles_per_line > $number_of_bubbles/2) && ($number_of_bubbles % 2==0)) {$bubbles_per_line=$number_of_bubbles/2;}
593: my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
594: my @table_range = ();
595: for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
596: if ($number_of_bubbles % $bubbles_per_line) {
597: $number_of_tables++;
598: push @table_range,($number_of_bubbles % $bubbles_per_line);
599: }
600: $cell_width-=8;
601: $cell_width=$cell_width*3/4;
602: return ($cell_width,$number_of_tables,@table_range);
603: }
604:
605: sub format_number {
606: my ($number,$format,$target,$safeeval)=@_;
607: my $ans;
608: if ($format eq '') {
609: #What is the number? (integer,decimal,floating point)
610: if ($number=~/^(\d*\.?\d*)(E|e)[+\-]?(\d*)$/) {
611: $format = '3e';
612: } elsif ($number=~/^(\d*)\.(\d*)$/) {
613: $format = '4f';
614: } elsif ($number=~/^(\d*)$/) {
615: $format = 'd';
616: }
617: }
618: if (!$Apache::lonxml::default_homework_loaded) {
619: &Apache::lonxml::default_homework_load($safeeval);
620: }
621: $ans=&Apache::run::run("&prettyprint(q\0$number\0,q\0$format\0,q\0$target\0)",$safeeval);
622: return $ans;
623: }
624:
625: sub make_numerical_bubbles {
626: my ($part,$id,$target,$parstack,$safeeval) =@_;
627:
628: my $number_of_bubbles =
629: &Apache::response::get_response_param($part.'_'.$id,'numbubbles',8);
630:
631: my ($format)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
632: my ($answer)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
633: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,
634: $safeeval);
635: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
636:
637: my @bubble_values=();
638: my @alphabet=('A'..'Z');
639:
640: &Apache::lonxml::debug("answer is $answer incorrect is @incorrect");
641: my @oldseed=&Math::Random::random_get_seed();
642: if (@incorrect) {
643: &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1 gt $number_of_bubbles));
644: if (defined($incorrect[0]) &&
645: scalar(@incorrect)+1 >= $number_of_bubbles) {
646: &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1).":$number_of_bubbles");
647: &Apache::response::setrandomnumber();
648: my @rand_inc=&Math::Random::random_permutation(@incorrect);
649: @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
650: @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
651: &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": which are :".join(':',@bubble_values));
652: &Math::Random::random_set_seed(@oldseed);
653:
654: my $correct;
655: for(my $i=0; $i<=$#bubble_values;$i++) {
656: if ($bubble_values[$i] eq $answer) {
657: $correct = $alphabet[$i];
658: last;
659: }
660: }
661:
662: if (defined($format) && $format ne '') {
663: my @bubble_display;
664: foreach my $value (@bubble_values) {
665: push(@bubble_display,
666: &format_number($value,$format,$target,$safeeval));
667: }
668: return (\@bubble_values,\@bubble_display,$correct);
669: } else {
670: return (\@bubble_values,\@bubble_values,$correct);
671: }
672: }
673: if (defined($incorrect[0]) &&
674: scalar(@incorrect)+1 < $number_of_bubbles) {
675: &Apache::lonxml::warning("Not enough incorrect answers were specified in the incorrect array, ignoring the specified incorrect answers and instead generating them (".join(',',@incorrect).").");
676: }
677: }
678: my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
679: my @powers = (1..$number_of_bubbles);
680: &Apache::response::setrandomnumber();
681: my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
682: my $power = $powers[$ind];
683: $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
684: my $factor = $factors[$ind];
685: my @bubble_display;
686: for ($ind=0;$ind<$number_of_bubbles;$ind++) {
687: $bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
688: $bubble_display[$ind] = &format_number($bubble_values[$ind],
689: $format,$target,$safeeval);
690:
691: }
692: my $correct = $alphabet[$number_of_bubbles-$power];
693: &Math::Random::random_set_seed(@oldseed);
694: return (\@bubble_values,\@bubble_display,$correct);
695: }
696:
697: sub get_tolrange {
698: my ($ans,$tol)=@_;
699: my ($high,$low);
700: if ($tol =~ /%$/) {
701: chop($tol);
702: my $change=$ans*($tol/100.0);
703: $high=$ans+$change;
704: $low=$ans-$change;
705: } else {
706: $high=$ans+$tol;
707: $low=$ans-$tol;
708: }
709: return ($high,$low);
710: }
711:
712: sub get_sigrange {
713: my ($sig)=@_;
714: &Apache::lonxml::debug("Got a sig of :$sig:");
715: my $courseid=$env{'request.course.id'};
716: if (lc($env{"course.$courseid.disablesigfigs"}) eq 'yes') {
717: return (15,0);
718: }
719: my $sig_lbound;
720: my $sig_ubound;
721: if ($sig eq '') {
722: $sig_lbound = 0; #SIG_LB_DEFAULT
723: $sig_ubound =15; #SIG_UB_DEFAULT
724: } else {
725: ($sig_lbound,$sig_ubound) = split(/,/,$sig);
726: if (!defined($sig_lbound)) {
727: $sig_lbound = 0; #SIG_LB_DEFAULT
728: $sig_ubound =15; #SIG_UB_DEFAULT
729: }
730: if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
731: }
732: if (($sig_ubound<$sig_lbound) ||
733: ($sig_lbound > 15) ||
734: ($sig =~/(\+|-)/ ) ) {
735: my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
736: if ($env{'request.state'} eq 'construct') {
737: $errormsg.=
738: &Apache::loncommon::help_open_topic('Significant_Figures');
739: }
740: &Apache::lonxml::error($errormsg);
741: }
742: return ($sig_ubound,$sig_lbound);
743: }
744:
745: sub start_stringresponse {
746: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
747: my $result;
748: my $id = &Apache::response::start_response($parstack,$safeeval);
749: if ($target eq 'meta') {
750: $result=&Apache::response::meta_package_write('stringresponse');
751: } elsif ($target eq 'edit') {
752: $result.=&Apache::edit::tag_start($target,$token);
753: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
754: $result.=&Apache::edit::select_arg('Type:','type',
755: [['cs','Case Sensitive'],['ci','Case Insensitive'],
756: ['mc','Case Insensitive, Any Order'],
757: ['re','Regular Expression']],$token);
758: $result.=&Apache::edit::text_arg('String to display for answer:',
759: 'answerdisplay',$token);
760: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
761: } elsif ($target eq 'modified') {
762: my $constructtag;
763: $constructtag=&Apache::edit::get_new_args($token,$parstack,
764: $safeeval,'answer',
765: 'type','answerdisplay');
766: if ($constructtag) {
767: $result = &Apache::edit::rebuild_tag($token);
768: $result.=&Apache::edit::handle_insert();
769: }
770: } elsif ($target eq 'web') {
771: if ( &Apache::response::show_answer() ) {
772: my $answer=
773: &Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
774: if (!defined $answer || $answer eq '') {
775: $answer=
776: &Apache::lonxml::get_param('answer',$parstack,$safeeval);
777: }
778: $Apache::inputtags::answertxt{$id}=$answer;
779: }
780: } elsif ($target eq 'answer' || $target eq 'grade') {
781: &Apache::response::reset_params();
782: }
783: return $result;
784: }
785:
786: sub end_stringresponse {
787: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
788:
789: my $result = '';
790: my $part=$Apache::inputtags::part;
791: my $id=$Apache::inputtags::response[-1];
792: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
793: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
794: my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
795: &Apache::lonxml::debug("current $answer ".$token->[2]);
796: if (!$Apache::lonxml::default_homework_loaded) {
797: &Apache::lonxml::default_homework_load($safeeval);
798: }
799: if ( $target eq 'grade' && &Apache::response::submitted() ) {
800: &Apache::response::setup_params('stringresponse',$safeeval);
801: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
802: if ($Apache::lonhomework::type eq 'exam' ||
803: &Apache::response::submitted('scantron')) {
804: &Apache::response::scored_response($part,$id);
805:
806: } else {
807: my $response = &Apache::response::getresponse();
808: if ( $response =~ /[^\s]/) {
809: my %previous = &Apache::response::check_for_previous($response,
810: $part,$id);
811: &Apache::lonxml::debug("submitted a $response<br>\n");
812: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
813: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
814: $response;
815: my ($ad,$msg);
816: if ($type eq 're' ) {
817: # if the RE wasn't in a var it likely got munged,
818: # thus grab it from the var directly
819: # my $testans=$token->[2]->{'answer'};
820: # if ($testans !~ m/^\s*\$/) {
821: # $answer=$token->[2]->{'answer'};
822: # }
823: ${$safeeval->varglob('LONCAPA::response')}=$response;
824: $result = &Apache::run::run('if ($LONCAPA::response=~m'.$answer.') { return 1; } else { return 0; }',$safeeval);
825: &Apache::lonxml::debug("current $response");
826: &Apache::lonxml::debug("current $answer");
827: $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
828: } else {
829: my $args_ref=
830: \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
831:
832: $$args_ref{'response'}=$response;
833: &Apache::lonxml::debug("current $response");
834: $$args_ref{'type'}=
835: &Apache::lonxml::get_param('type',$parstack,$safeeval);
836: foreach my $key (keys(%Apache::inputtags::params)) {
837: $$args_ref{$key}=$Apache::inputtags::params{$key};
838: }
839: &Apache::lonxml::debug('answer is'.join(':',$answer));
840: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=($answer);
841: ($result, my @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
842: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
843: my ($awards)=split(/:/,$result);
844: my (@awards) = split(/,/,$awards);
845: ($ad,$msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
846: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
847: }
848: if ($Apache::lonhomework::type eq 'survey' &&
849: ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
850: $ad eq 'EXACT_ANS')) {
851: $ad='SUBMITTED';
852: }
853: &Apache::response::handle_previous(\%previous,$ad);
854: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
855: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
856: }
857: }
858: } elsif ($target eq 'answer' || $target eq 'analyze') {
859: if ($target eq 'analyze') {
860: push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
861: $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
862: &Apache::response::check_if_computed($token,$parstack,$safeeval,
863: 'answer');
864: }
865: &Apache::response::setup_params('stringresponse',$safeeval);
866: if ($target eq 'answer') {
867: $result.=&Apache::response::answer_header('stringresponse');
868: }
869: # foreach my $ans (@answers) {
870: if ($target eq 'answer') {
871: $result.=&Apache::response::answer_part('stringresponse',$answer);
872: if ($type eq 're') {
873: $result.=&Apache::response::answer_part('stringresponse',
874: $answerdisplay);
875: }
876: } elsif ($target eq 'analyze') {
877: push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
878: $answer);
879: }
880: # }
881: my $string='Case Insensitive';
882: if ($type eq 'mc') {
883: $string='Multiple Choice';
884: } elsif ($type eq 'cs') {
885: $string='Case Sensitive';
886: } elsif ($type eq 'ci') {
887: $string='Case Insensitive';
888: } elsif ($type eq 're') {
889: $string='Regular Expression';
890: }
891: if ($target eq 'answer') {
892: if ($env{'form.answer_output_mode'} eq 'tex') {
893: $result.=&Apache::response::answer_part('stringresponse',
894: "$string");
895: } else {
896: $result.=&Apache::response::answer_part('stringresponse',
897: "<b>$string</b>");
898: }
899: } elsif ($target eq 'analyze') {
900: push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
901: $type);
902: }
903: if ($target eq 'answer') {
904: $result.=&Apache::response::answer_footer('stringresponse');
905: }
906: } elsif ($target eq 'edit') {
907: $result.='</td></tr>'.&Apache::edit::end_table;
908: }
909: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
910: $target eq 'tex' || $target eq 'analyze') {
911: &Apache::lonxml::increment_counter(&Apache::response::repetition());
912: }
913: &Apache::response::end_response;
914: return $result;
915: }
916:
917: sub start_formularesponse {
918: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
919: my $result;
920: if ($target eq 'meta') {
921: &Apache::response::start_response($parstack,$safeeval);
922: $result=&Apache::response::meta_package_write('formularesponse');
923: &Apache::response::end_response();
924: } else {
925: $result.=&start_numericalresponse(@_);
926: }
927: return $result;
928: }
929:
930: sub end_formularesponse {
931: return end_numericalresponse(@_);
932: }
933:
934: 1;
935: __END__
936:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>