1: # The LearningOnline Network with CAPA
2: # caparesponse definition
3: #
4: # $Id: caparesponse.pm,v 1.162 2005/01/31 22:00:40 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 Apache::lonlocal;
33:
34: BEGIN {
35: &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse','formularesponse'));
36: }
37:
38: sub start_numericalresponse {
39: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
40: my $id = &Apache::response::start_response($parstack,$safeeval);
41: my $result;
42: undef %{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
43: if ($target eq 'edit') {
44: $result.=&Apache::edit::tag_start($target,$token);
45: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
46: if ($token->[1] eq 'numericalresponse') {
47: $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
48: $token).
49: &Apache::loncommon::help_open_topic('numerical_wrong_answers');
50: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
51: &Apache::loncommon::help_open_topic('Physical_Units');
52: $result.=&Apache::edit::text_arg('Format:','format',$token,4).
53: &Apache::loncommon::help_open_topic('Numerical_Response_Format');
54: } elsif ($token->[1] eq 'formularesponse') {
55: $result.=&Apache::edit::text_arg('Sample Points:','samples',
56: $token,40).
57: &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
58: }
59: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
60: } elsif ($target eq 'modified') {
61: my $constructtag;
62: if ($token->[1] eq 'numericalresponse') {
63: $constructtag=&Apache::edit::get_new_args($token,$parstack,
64: $safeeval,'answer',
65: 'incorrect','unit',
66: 'format');
67: } elsif ($token->[1] eq 'formularesponse') {
68: $constructtag=&Apache::edit::get_new_args($token,$parstack,
69: $safeeval,'answer',
70: 'samples');
71: }
72: if ($constructtag) {
73: $result = &Apache::edit::rebuild_tag($token);
74: $result.=&Apache::edit::handle_insert();
75: }
76: } elsif ($target eq 'meta') {
77: $result=&Apache::response::meta_package_write('numericalresponse');
78: } elsif ($target eq 'answer' || $target eq 'grade') {
79: &Apache::response::reset_params();
80: } elsif ($target eq 'web') {
81: my $partid = $Apache::inputtags::part;
82: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
83: &Apache::lonxml::debug("Got unit $hideunit for $partid $id");
84: #no way to enter units, with radio buttons
85: if (lc($hideunit) eq "yes") {
86: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
87: $safeeval);
88: if ($unit =~ /\S/) { $result.=" (in $unit) "; }
89: }
90: if ( &Apache::response::show_answer() ) {
91: my $answertxt;
92: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
93: $safeeval);
94: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
95: $safeeval);
96: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
97: $safeeval);
98: for (my $i=0; $i <= $#answers; $i++) {
99: my $answer=$answers[$i];
100: my $format;
101: if ($#formats > 0) {
102: $format=$formats[$i];
103: } else {
104: $format=$formats[0];
105: }
106: if ($unit=~/\$/) { $format="\$".$format; $unit=~s/\$//g; }
107: if ($unit=~/\,/) { $format="\,".$format; $unit=~s/\,//g; }
108: my $formatted=&format_number($answer,$format,$target,
109: $safeeval);
110: $answertxt.=$formatted.',';
111: }
112: chop $answertxt;
113: if ($target eq 'web') {
114: $answertxt.=" $unit ";
115: }
116: $Apache::inputtags::answertxt{$id}=$answertxt;
117: }
118: }
119: return $result;
120: }
121:
122: sub end_numericalresponse {
123: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
124: my $increment=1;
125: my $result = '';
126: if (!$Apache::lonxml::default_homework_loaded) {
127: &Apache::lonxml::default_homework_load($safeeval);
128: }
129: my $tag;
130: if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
131: if ( $target eq 'grade' && &Apache::response::submitted() ) {
132: &Apache::response::setup_params($tag,$safeeval);
133: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
134: my $partid = $Apache::inputtags::part;
135: my $id = $Apache::inputtags::response['-1'];
136: if ($Apache::lonhomework::type eq 'exam' &&
137: $tag eq 'formularesponse') {
138: $increment=&Apache::response::scored_response($partid,$id);
139: } elsif ($Apache::lonhomework::type eq 'survey') {
140: my $response = &Apache::response::getresponse();
141: if ( $response =~ /[^\s]/) {
142: my %previous=&Apache::response::check_for_previous($response,
143: $partid,$id);
144: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
145: my $ad=$Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
146: &Apache::response::handle_previous(\%previous,$ad);
147: }
148: } else {
149: my $response = &Apache::response::getresponse();
150: if ( $response =~ /[^\s]/) {
151: my $ad;
152: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
153: &Apache::lonxml::debug("submitted a $response<br>\n");
154: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
155:
156: if ( &Apache::response::submitted('scantron')) {
157: my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
158: if (!$number_of_bubbles) { $number_of_bubbles=8; }
159: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
160: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
161: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
162: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
163: my ($values,$display)=&make_numerical_bubbles($number_of_bubbles,$target,$answers[0],$formats[0],\@incorrect,$safeeval);
164: $response=$values->[$response];
165: }
166: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
167: my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
168: $$args_ref{'response'}=$response;
169: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
170:
171: foreach my $arg ('type','tol','sig','format','unit','calc',
172: 'samples') {
173: $$args_ref{$arg}=
174: &Apache::lonxml::get_param($arg,$parstack,$safeeval);
175: }
176: foreach my $key (keys(%Apache::inputtags::params)) {
177: $$args_ref{$key}=$Apache::inputtags::params{$key};
178: }
179:
180: #no way to enter units, with radio buttons
181: if ($Apache::lonhomework::type eq 'exam' ||
182: lc($hideunit) eq "yes") {
183: delete($$args_ref{'unit'});
184: }
185: #sig fig don't make much sense either
186: if (($Apache::lonhomework::type eq 'exam' ||
187: &Apache::response::submitted('scantron')) &&
188: $tag eq 'numericalresponse') {
189: delete($$args_ref{'sig'});
190: }
191:
192: if ($tag eq 'formularesponse') {
193: $$args_ref{'type'}='fml';
194: } elsif ($tag eq 'numericalresponse') {
195: $$args_ref{'type'}='float';
196: }
197: my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
198: &Apache::lonxml::debug('answer is'.join(':',@answer));
199: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=@answer;
200:
201: ($result,my @msgs) =
202: &Apache::run::run("&caparesponse_check_list()",$safeeval);
203: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
204: my ($awards)=split(/:/,$result);
205: my (@awards) = split(/,/,$awards);
206: ($ad,my $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
207: &Apache::lonxml::debug('ad is'.$ad);
208: if ($ad eq 'SIG_FAIL') {
209: my ($sig_u,$sig_l)=
210: &get_sigrange($Apache::inputtags::params{'sig'});
211: $msg=join(':',$msg,$sig_l,$sig_u);
212: &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
213: $Apache::inputtags::params{'sig'});
214: }
215: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
216: &Apache::response::handle_previous(\%previous,$ad);
217: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
218: $Apache::lonhomework::results{"resource.$partid.$id.awardmsg"}=$msg;
219: $result='';
220: }
221: }
222: } elsif ($target eq 'web' || $target eq 'tex') {
223: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
224: $safeeval);
225: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
226: my $status = $Apache::inputtags::status['-1'];
227: if ($Apache::lonhomework::type eq 'exam') {
228: my $partid=$Apache::inputtags::part;
229: my $id=$Apache::inputtags::response[-1];
230: my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
231: if ($Apache::inputtags::params{'numbubbles'}) {
232: $number_of_bubbles = $Apache::inputtags::params{'numbubbles'};
233: }
234: if (!$number_of_bubbles) { $number_of_bubbles=8; }
235:
236: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
237: $safeeval);
238: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
239: $safeeval);
240: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
241: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
242: my ($bubble_values,$bubble_display)=
243: &make_numerical_bubbles($number_of_bubbles,
244: $target,$answers[0],
245: $formats[0],\@incorrect,$safeeval);
246: my @alphabet=('A'..'Z');
247: if ($target eq 'web') {
248: if ($tag eq 'numericalresponse') {
249: if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
250: $result.= '<table border="1"><tr>';
251: my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
252: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
253: my $checked='';
254: if ($previous eq $bubble_values->[$ind]) {
255: $checked=" checked='on' ";
256: }
257: $result.='<td><input type="radio" name="HWVAL_'.$id.
258: '" value="'.$bubble_values->[$ind].'" '.$checked
259: .' /><b>'.$alphabet[$ind].'</b>: '.
260: $bubble_display->[$ind].'</td>';
261: }
262: $result.='</tr></table>';
263: } elsif ($tag eq 'formularesponse') {
264: $result.= '<br /><br /><font color="red">
265: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
266: </textarea></font> <br /><br />';
267: }
268: } elsif ($target eq 'tex') {
269: if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
270: $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
271: }
272: if ($tag eq 'numericalresponse') {
273: my ($celllength,$number_of_tables,@table_range)=
274: &get_table_sizes($number_of_bubbles,$bubble_display);
275: my $j=0;
276: my $cou=0;
277: $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
278: for (my $i=0;$i<$number_of_tables;$i++) {
279: $result.='\vskip -1 mm \noindent \setlength{\tabcolsep}{2 mm}\begin{tabular}{';
280: for (my $ind=0;$ind<$table_range[$j];$ind++) {
281: $result.='p{3 mm}p{'.$celllength.' mm}';
282: }
283: $result.='}';
284: for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
285: $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_display->[$ind].'} ';
286: if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
287: }
288: $cou += $table_range[$j];
289: $j++;
290: $result.='\\\\\end{tabular}\vskip 0 mm ';
291: }
292: $result.='\end{enumerate}';
293: } else {
294: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
295: my $repetition = &Apache::response::repetition();
296: $result.='\begin{enumerate}';
297: for (my $i=0;$i<$repetition;$i++) {
298: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
299: }
300: $increment=$repetition;
301: $result.= '\end{enumerate}';
302: }
303: }
304: }
305: } elsif ($target eq 'edit') {
306: $result.='</td></tr>'.&Apache::edit::end_table;
307: } elsif ($target eq 'answer' || $target eq 'analyze') {
308:
309: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
310: if ($target eq 'analyze') {
311: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
312: $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
313: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
314: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
315: push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
316: &Apache::response::check_if_computed($token,$parstack,
317: $safeeval,'answer');
318: }
319: if (scalar(@$tagstack)) {
320: &Apache::response::setup_params($tag,$safeeval);
321: }
322: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
323: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
324: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
325: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
326:
327: if ($target eq 'answer') {
328: $result.=&Apache::response::answer_header($tag);
329: }
330: for(my $i=0;$i<=$#answers;$i++) {
331: my $ans=$answers[$i];
332: my $fmt=$formats[0];
333: if (@formats && $#formats) {$fmt=$formats[$i];}
334: my ($high,$low);
335: if ($Apache::inputtags::params{'tol'}) {
336: ($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
337: }
338: my ($sighigh,$siglow);
339: if ($Apache::inputtags::params{'sig'}) {
340: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
341: }
342: if ($fmt && $tag eq 'numericalresponse') {
343: $fmt=~s/e/E/g;
344: if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
345: if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
346: $ans = &format_number($ans,$fmt,$target,$safeeval);
347: #if ($high) {
348: # $high=&format_number($high,$fmt,$target,$safeeval);
349: # $low =&format_number($low,$fmt,$target,$safeeval);
350: #}
351: }
352: if ($target eq 'answer') {
353: if ($high && $tag eq 'numericalresponse') { $ans.=' ['.$low.','.$high.']'; }
354: if (defined($sighigh) && $tag eq 'numericalresponse') {
355: if ($ENV{'form.answer_output_mode'} eq 'tex') {
356: $ans.= " Sig $siglow - $sighigh";
357: } else {
358: $ans.= " Sig <i>$siglow - $sighigh</i>";
359: }
360: }
361: $result.=&Apache::response::answer_part($tag,$ans);
362: } elsif ($target eq 'analyze') {
363: push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
364: if ($high) {
365: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
366: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
367: }
368: }
369: }
370: if (defined($unit) and ($unit ne '') and
371: $tag eq 'numericalresponse') {
372: if ($target eq 'answer') {
373: if ($ENV{'form.answer_output_mode'} eq 'tex') {
374: $result.=&Apache::response::answer_part($tag,
375: " Unit: $unit ");
376: } else {
377: $result.=&Apache::response::answer_part($tag,
378: "Unit: <b>$unit</b>");
379: }
380: } elsif ($target eq 'analyze') {
381: push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
382: }
383: }
384: if ($tag eq 'formularesponse' && $target eq 'answer') {
385: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
386: $result.=&Apache::response::answer_part($tag,$samples);
387: }
388: if ($target eq 'answer') {
389: $result.=&Apache::response::answer_footer($tag);
390: }
391: }
392: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
393: $target eq 'tex' || $target eq 'analyze') {
394: &Apache::lonxml::increment_counter($increment);
395: }
396: &Apache::response::end_response;
397: return $result;
398: }
399:
400: sub get_table_sizes {
401: my ($number_of_bubbles,$rbubble_values)=@_;
402: my $scale=2; #mm for one digit
403: my $cell_width=0;
404: foreach my $member (@$rbubble_values) {
405: my $cell_width_real=0;
406: if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^{(\+|-)?(\d+)}\$?/) {
407: $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale;
408: } elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
409: $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
410: } elsif ($member=~/(\d*)\.(\d*)/) {
411: $cell_width_real=(length($1)+length($2)+3)*$scale;
412: } else {
413: $cell_width_real=(length($member)+1)*$scale*0.9;
414: }
415: if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
416: }
417: $cell_width+=8;
418: my $textwidth;
419: if ($ENV{'form.textwidth'} ne '') {
420: $ENV{'form.textwidth'}=~/(\d*)\.?(\d*)/;
421: $textwidth=$1.'.'.$2;
422: } else {
423: $ENV{'textwidth'}=~/(\d+)\.?(\d*)/;
424: $textwidth=$1.'.'.$2;
425: }
426: my $bubbles_per_line=int($textwidth/$cell_width);
427: if ($bubbles_per_line > $number_of_bubbles) {
428: $bubbles_per_line=$number_of_bubbles;
429: }elsif (($bubbles_per_line > $number_of_bubbles/2) && ($number_of_bubbles % 2==0)) {$bubbles_per_line=$number_of_bubbles/2;}
430: my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
431: my @table_range = ();
432: for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
433: if ($number_of_bubbles % $bubbles_per_line) {
434: $number_of_tables++;
435: push @table_range,($number_of_bubbles % $bubbles_per_line);
436: }
437: $cell_width-=8;
438: $cell_width=$cell_width*3/4;
439: return ($cell_width,$number_of_tables,@table_range);
440: }
441:
442: sub format_number {
443: my ($number,$format,$target,$safeeval)=@_;
444: my $ans;
445: if ($format eq '') {
446: #What is the number? (integer,decimal,floating point)
447: if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
448: $format = '3e';
449: } elsif ($number=~/^(\d*)\.(\d*)$/) {
450: $format = '4f';
451: } elsif ($number=~/^(\d*)$/) {
452: $format = 'd';
453: }
454: }
455: if (!$Apache::lonxml::default_homework_loaded) {
456: &Apache::lonxml::default_homework_load($safeeval);
457: }
458: $ans=&Apache::run::run("&prettyprint(q\0$number\0,q\0$format\0,q\0$target\0)",$safeeval);
459: return $ans;
460: }
461:
462: sub make_numerical_bubbles {
463: my ($number_of_bubbles,$target,$answer,$format,$incorrect,$safeeval) =@_;
464: my @bubble_values=();
465: &Apache::lonxml::debug("answer is $answer incorrect is $incorrect");
466: my @oldseed=&Math::Random::random_get_seed();
467: if (defined($incorrect) && ref($incorrect)) {
468: &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1 gt $number_of_bubbles));
469: if (defined($$incorrect[0]) &&
470: scalar(@$incorrect)+1 >= $number_of_bubbles) {
471: &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1).":$number_of_bubbles");
472: &Apache::response::setrandomnumber();
473: my @rand_inc=&Math::Random::random_permutation(@$incorrect);
474: @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
475: @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
476: &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": whih are :".join(':',@bubble_values));
477: &Math::Random::random_set_seed(@oldseed);
478: if (defined($format) && $format ne '') {
479: my @bubble_display;
480: foreach my $value (@bubble_values) {
481: push(@bubble_display,
482: &format_number($value,$format,$target,$safeeval));
483: }
484: return (\@bubble_values,\@bubble_display);
485: } else {
486: return (\@bubble_values,\@bubble_values);
487: }
488: }
489: if (defined($$incorrect[0]) &&
490: scalar(@$incorrect)+1 < $number_of_bubbles) {
491: &Apache::lonxml::warning("Not enough incorrect answers were specified in the incorrect array, ignoring the specified incorrect answers and instead generating them (".join(',',@$incorrect).").");
492: }
493: }
494: my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
495: my @powers = (1..$number_of_bubbles);
496: &Apache::response::setrandomnumber();
497: my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
498: my $power = $powers[$ind];
499: $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
500: my $factor = $factors[$ind];
501: my @bubble_display;
502: for ($ind=0;$ind<$number_of_bubbles;$ind++) {
503: $bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
504: $bubble_display[$ind] = &format_number($bubble_values[$ind],
505: $format,$target,$safeeval);
506:
507: }
508: &Math::Random::random_set_seed(@oldseed);
509: return (\@bubble_values,\@bubble_display);
510: }
511:
512: sub get_tolrange {
513: my ($ans,$tol)=@_;
514: my ($high,$low);
515: if ($tol =~ /%$/) {
516: chop($tol);
517: my $change=$ans*($tol/100.0);
518: $high=$ans+$change;
519: $low=$ans-$change;
520: } else {
521: $high=$ans+$tol;
522: $low=$ans-$tol;
523: }
524: return ($high,$low);
525: }
526:
527: sub get_sigrange {
528: my ($sig)=@_;
529: &Apache::lonxml::debug("Got a sig of :$sig:");
530: my $courseid=$ENV{'request.course.id'};
531: if (lc($ENV{"course.$courseid.disablesigfigs"}) eq 'yes') {
532: return (15,0);
533: }
534: my $sig_lbound;
535: my $sig_ubound;
536: if ($sig eq '') {
537: $sig_lbound = 0; #SIG_LB_DEFAULT
538: $sig_ubound =15; #SIG_UB_DEFAULT
539: } else {
540: ($sig_lbound,$sig_ubound) = split(/,/,$sig);
541: if (!defined($sig_lbound)) {
542: $sig_lbound = 0; #SIG_LB_DEFAULT
543: $sig_ubound =15; #SIG_UB_DEFAULT
544: }
545: if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
546: }
547: if (($sig_ubound<$sig_lbound) ||
548: ($sig_lbound > 15) ||
549: ($sig =~/(\+|-)/ ) ) {
550: my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
551: if ($ENV{'request.state'} eq 'construct') {
552: $errormsg.=
553: &Apache::loncommon::help_open_topic('Significant_Figures');
554: }
555: &Apache::lonxml::error($errormsg);
556: }
557: return ($sig_ubound,$sig_lbound);
558: }
559:
560: sub start_stringresponse {
561: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
562: my $result;
563: my $id = &Apache::response::start_response($parstack,$safeeval);
564: if ($target eq 'meta') {
565: $result=&Apache::response::meta_package_write('stringresponse');
566: } elsif ($target eq 'edit') {
567: $result.=&Apache::edit::tag_start($target,$token);
568: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
569: $result.=&Apache::edit::select_arg('Type:','type',
570: [['cs','Case Sensitive'],['ci','Case Insensitive'],
571: ['mc','Case Insensitive, Any Order'],
572: ['re','Regular Expression']],$token);
573: $result.=&Apache::edit::text_arg('String to display for answer:',
574: 'answerdisplay',$token);
575: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
576: } elsif ($target eq 'modified') {
577: my $constructtag;
578: $constructtag=&Apache::edit::get_new_args($token,$parstack,
579: $safeeval,'answer',
580: 'type','answerdisplay');
581: if ($constructtag) {
582: $result = &Apache::edit::rebuild_tag($token);
583: $result.=&Apache::edit::handle_insert();
584: }
585: } elsif ($target eq 'web') {
586: if ( &Apache::response::show_answer() ) {
587: my $answer=
588: &Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
589: if (!defined $answer || $answer eq '') {
590: $answer=
591: &Apache::lonxml::get_param('answer',$parstack,$safeeval);
592: }
593: $Apache::inputtags::answertxt{$id}=$answer;
594: }
595: } elsif ($target eq 'answer' || $target eq 'grade') {
596: &Apache::response::reset_params();
597: }
598: return $result;
599: }
600:
601: sub end_stringresponse {
602: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
603: my $increment=1;
604: my $result = '';
605: my $part=$Apache::inputtags::part;
606: my $id=$Apache::inputtags::response[-1];
607: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
608: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
609: my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
610: &Apache::lonxml::debug("current $answer ".$token->[2]);
611: if (!$Apache::lonxml::default_homework_loaded) {
612: &Apache::lonxml::default_homework_load($safeeval);
613: }
614: if ( $target eq 'grade' && &Apache::response::submitted() ) {
615: &Apache::response::setup_params('stringresponse',$safeeval);
616: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
617: if ($Apache::lonhomework::type eq 'exam' ||
618: &Apache::response::submitted('scantron')) {
619: $increment=&Apache::response::scored_response($part,$id);
620: } elsif ($Apache::lonhomework::type eq 'survey') {
621: my $response = &Apache::response::getresponse();
622: if ( $response =~ /[^\s]/) {
623: my %previous=&Apache::response::check_for_previous($response,
624: $part,$id);
625: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
626: $response;
627: my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
628: &Apache::response::handle_previous(\%previous,$ad);
629: }
630: } else {
631: my $response = &Apache::response::getresponse();
632: if ( $response =~ /[^\s]/) {
633: my %previous = &Apache::response::check_for_previous($response,
634: $part,$id);
635: &Apache::lonxml::debug("submitted a $response<br>\n");
636: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
637: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
638: $response;
639: my ($ad,$msg);
640: if ($type eq 're' ) {
641: # if the RE wasn't in a var it likely got munged,
642: # thus grab it from the var directly
643: # my $testans=$token->[2]->{'answer'};
644: # if ($testans !~ m/^\s*\$/) {
645: # $answer=$token->[2]->{'answer'};
646: # }
647: ${$safeeval->varglob('LONCAPA::response')}=$response;
648: $result = &Apache::run::run('return $LONCAPA::response=~m'.$answer,$safeeval);
649: &Apache::lonxml::debug("current $response");
650: &Apache::lonxml::debug("current $answer");
651: $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
652: } else {
653: my $args_ref=
654: \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
655:
656: $$args_ref{'response'}=$response;
657: &Apache::lonxml::debug("current $response");
658: $$args_ref{'type'}=
659: &Apache::lonxml::get_param('type',$parstack,$safeeval);
660: foreach my $key (keys(%Apache::inputtags::params)) {
661: $$args_ref{$key}=$Apache::inputtags::params{$key};
662: }
663: &Apache::lonxml::debug('answer is'.join(':',$answer));
664: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=($answer);
665: ($result, my @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
666: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
667: my ($awards)=split(/:/,$result);
668: my (@awards) = split(/,/,$awards);
669: ($ad,$msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
670: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
671: }
672: &Apache::response::handle_previous(\%previous,$ad);
673: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
674: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
675: }
676: }
677: } elsif ($target eq 'web' || $target eq 'tex') {
678: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
679: my $status = $Apache::inputtags::status['-1'];
680: if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
681: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
682: $increment = &Apache::response::repetition();
683: $result.='\begin{enumerate}';
684: for (my $i=0;$i<$increment;$i++) {
685: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
686: '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
687: }
688: $result.= '\end{enumerate}';
689: }
690: } elsif ($target eq 'answer' || $target eq 'analyze') {
691: if ($target eq 'analyze') {
692: push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
693: $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
694: &Apache::response::check_if_computed($token,$parstack,$safeeval,
695: 'answer');
696: }
697: &Apache::response::setup_params('stringresponse',$safeeval);
698: if ($target eq 'answer') {
699: $result.=&Apache::response::answer_header('stringresponse');
700: }
701: # foreach my $ans (@answers) {
702: if ($target eq 'answer') {
703: $result.=&Apache::response::answer_part('stringresponse',$answer);
704: } elsif ($target eq 'analyze') {
705: push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
706: $answer);
707: }
708: # }
709: my $string='Case Insensitive';
710: if ($type eq 'mc') {
711: $string='Multiple Choice';
712: } elsif ($type eq 'cs') {
713: $string='Case Sensitive';
714: } elsif ($type eq 'ci') {
715: $string='Case Insensitive';
716: } elsif ($type eq 're') {
717: $string='Regular Expression';
718: }
719: if ($target eq 'answer') {
720: if ($ENV{'form.answer_output_mode'} eq 'tex') {
721: $result.=&Apache::response::answer_part('stringresponse',
722: "$string");
723: } else {
724: $result.=&Apache::response::answer_part('stringresponse',
725: "<b>$string</b>");
726: }
727: } elsif ($target eq 'analyze') {
728: push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
729: $type);
730: }
731: if ($target eq 'answer') {
732: $result.=&Apache::response::answer_footer('stringresponse');
733: }
734: } elsif ($target eq 'edit') {
735: $result.='</td></tr>'.&Apache::edit::end_table;
736: }
737: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
738: $target eq 'tex' || $target eq 'analyze') {
739: &Apache::lonxml::increment_counter($increment);
740: }
741: &Apache::response::end_response;
742: return $result;
743: }
744:
745: sub start_formularesponse {
746: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
747: my $result;
748: if ($target eq 'meta') {
749: &Apache::response::start_response($parstack,$safeeval);
750: $result=&Apache::response::meta_package_write('formularesponse');
751: &Apache::response::end_response();
752: } else {
753: $result.=&start_numericalresponse(@_);
754: }
755: return $result;
756: }
757:
758: sub end_formularesponse {
759: return end_numericalresponse(@_);
760: }
761:
762: 1;
763: __END__
764:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>