1: # The LearningOnline Network with CAPA
2: # caparesponse definition
3: #
4: # $Id: caparesponse.pm,v 1.166 2005/04/07 06:56:22 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: use Apache::lonnet;
34:
35: BEGIN {
36: &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse','formularesponse'));
37: }
38:
39: sub start_numericalresponse {
40: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
41: my $id = &Apache::response::start_response($parstack,$safeeval);
42: my $result;
43: undef %{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
44: if ($target eq 'edit') {
45: $result.=&Apache::edit::tag_start($target,$token);
46: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
47: if ($token->[1] eq 'numericalresponse') {
48: $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
49: $token).
50: &Apache::loncommon::help_open_topic('numerical_wrong_answers');
51: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
52: &Apache::loncommon::help_open_topic('Physical_Units');
53: $result.=&Apache::edit::text_arg('Format:','format',$token,4).
54: &Apache::loncommon::help_open_topic('Numerical_Response_Format');
55: } elsif ($token->[1] eq 'formularesponse') {
56: $result.=&Apache::edit::text_arg('Sample Points:','samples',
57: $token,40).
58: &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
59: }
60: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
61: } elsif ($target eq 'modified') {
62: my $constructtag;
63: if ($token->[1] eq 'numericalresponse') {
64: $constructtag=&Apache::edit::get_new_args($token,$parstack,
65: $safeeval,'answer',
66: 'incorrect','unit',
67: 'format');
68: } elsif ($token->[1] eq 'formularesponse') {
69: $constructtag=&Apache::edit::get_new_args($token,$parstack,
70: $safeeval,'answer',
71: 'samples');
72: }
73: if ($constructtag) {
74: $result = &Apache::edit::rebuild_tag($token);
75: $result.=&Apache::edit::handle_insert();
76: }
77: } elsif ($target eq 'meta') {
78: $result=&Apache::response::meta_package_write('numericalresponse');
79: } elsif ($target eq 'answer' || $target eq 'grade') {
80: &Apache::response::reset_params();
81: } elsif ($target eq 'web') {
82: my $partid = $Apache::inputtags::part;
83: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
84: &Apache::lonxml::debug("Got unit $hideunit for $partid $id");
85: #no way to enter units, with radio buttons
86: if (lc($hideunit) eq "yes") {
87: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
88: $safeeval);
89: if ($unit =~ /\S/) { $result.=" (in $unit) "; }
90: }
91: if ( &Apache::response::show_answer() ) {
92: my $answertxt;
93: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
94: $safeeval);
95: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
96: $safeeval);
97: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
98: $safeeval);
99: for (my $i=0; $i <= $#answers; $i++) {
100: my $answer=$answers[$i];
101: my $format;
102: if ($#formats > 0) {
103: $format=$formats[$i];
104: } else {
105: $format=$formats[0];
106: }
107: if ($unit=~/\$/) { $format="\$".$format; $unit=~s/\$//g; }
108: if ($unit=~/\,/) { $format="\,".$format; $unit=~s/\,//g; }
109: my $formatted=&format_number($answer,$format,$target,
110: $safeeval);
111: $answertxt.=$formatted.',';
112: }
113: chop $answertxt;
114: if ($target eq 'web') {
115: $answertxt.=" $unit ";
116: }
117: $Apache::inputtags::answertxt{$id}=$answertxt;
118: }
119: }
120: return $result;
121: }
122:
123: sub end_numericalresponse {
124: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
125: my $increment=1;
126: my $result = '';
127: if (!$Apache::lonxml::default_homework_loaded) {
128: &Apache::lonxml::default_homework_load($safeeval);
129: }
130: my $tag;
131: if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
132: if ( $target eq 'grade' && &Apache::response::submitted() ) {
133: &Apache::response::setup_params($tag,$safeeval);
134: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
135: my $partid = $Apache::inputtags::part;
136: my $id = $Apache::inputtags::response['-1'];
137: if ($Apache::lonhomework::type eq 'exam' &&
138: $tag eq 'formularesponse') {
139: $increment=&Apache::response::scored_response($partid,$id);
140: } else {
141: my $response = &Apache::response::getresponse();
142: if ( $response =~ /[^\s]/) {
143: my $ad;
144: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
145: &Apache::lonxml::debug("submitted a $response<br>\n");
146: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
147:
148: if ( &Apache::response::submitted('scantron')) {
149: my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
150: if (!$number_of_bubbles) { $number_of_bubbles=8; }
151: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
152: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
153: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
154: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
155: my ($values,$display)=&make_numerical_bubbles($number_of_bubbles,$target,$answers[0],$formats[0],\@incorrect,$safeeval);
156: $response=$values->[$response];
157: }
158: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
159: my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
160: $$args_ref{'response'}=$response;
161: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
162:
163: foreach my $arg ('type','tol','sig','format','unit','calc',
164: 'samples') {
165: $$args_ref{$arg}=
166: &Apache::lonxml::get_param($arg,$parstack,$safeeval);
167: }
168: foreach my $key (keys(%Apache::inputtags::params)) {
169: $$args_ref{$key}=$Apache::inputtags::params{$key};
170: }
171:
172: #no way to enter units, with radio buttons
173: if ($Apache::lonhomework::type eq 'exam' ||
174: lc($hideunit) eq "yes") {
175: delete($$args_ref{'unit'});
176: }
177: #sig fig don't make much sense either
178: if (($Apache::lonhomework::type eq 'exam' ||
179: &Apache::response::submitted('scantron')) &&
180: $tag eq 'numericalresponse') {
181: delete($$args_ref{'sig'});
182: }
183:
184: if ($tag eq 'formularesponse') {
185: $$args_ref{'type'}='fml';
186: } elsif ($tag eq 'numericalresponse') {
187: $$args_ref{'type'}='float';
188: }
189: my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
190: &Apache::lonxml::debug('answer is'.join(':',@answer));
191: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=@answer;
192:
193: ($result,my @msgs) =
194: &Apache::run::run("&caparesponse_check_list()",$safeeval);
195: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
196: my ($awards)=split(/:/,$result);
197: my (@awards) = split(/,/,$awards);
198: ($ad,my $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
199: &Apache::lonxml::debug('ad is'.$ad);
200: if ($ad eq 'SIG_FAIL') {
201: my ($sig_u,$sig_l)=
202: &get_sigrange($Apache::inputtags::params{'sig'});
203: $msg=join(':',$msg,$sig_l,$sig_u);
204: &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
205: $Apache::inputtags::params{'sig'});
206: }
207: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
208: if ($Apache::lonhomework::type eq 'survey' &&
209: ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
210: $ad eq 'EXACT_ANS')) {
211: $ad='SUBMITTED';
212: }
213: &Apache::response::handle_previous(\%previous,$ad);
214: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
215: $Apache::lonhomework::results{"resource.$partid.$id.awardmsg"}=$msg;
216: $result='';
217: }
218: }
219: } elsif ($target eq 'web' || $target eq 'tex') {
220: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
221: $safeeval);
222: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
223: my $status = $Apache::inputtags::status['-1'];
224: if ($Apache::lonhomework::type eq 'exam') {
225: my $partid=$Apache::inputtags::part;
226: my $id=$Apache::inputtags::response[-1];
227: my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
228: if ($Apache::inputtags::params{'numbubbles'}) {
229: $number_of_bubbles = $Apache::inputtags::params{'numbubbles'};
230: }
231: if (!$number_of_bubbles) { $number_of_bubbles=8; }
232:
233: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
234: $safeeval);
235: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
236: $safeeval);
237: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
238: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
239: my ($bubble_values,$bubble_display)=
240: &make_numerical_bubbles($number_of_bubbles,
241: $target,$answers[0],
242: $formats[0],\@incorrect,$safeeval);
243: my @alphabet=('A'..'Z');
244: if ($target eq 'web') {
245: if ($tag eq 'numericalresponse') {
246: if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
247: $result.= '<table border="1"><tr>';
248: my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
249: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
250: my $checked='';
251: if ($previous eq $bubble_values->[$ind]) {
252: $checked=" checked='on' ";
253: }
254: $result.='<td><input type="radio" name="HWVAL_'.$id.
255: '" value="'.$bubble_values->[$ind].'" '.$checked
256: .' /><b>'.$alphabet[$ind].'</b>: '.
257: $bubble_display->[$ind].'</td>';
258: }
259: $result.='</tr></table>';
260: } elsif ($tag eq 'formularesponse') {
261: $result.= '<br /><br /><font color="red">
262: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
263: </textarea></font> <br /><br />';
264: }
265: } elsif ($target eq 'tex') {
266: if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
267: $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
268: }
269: if ($tag eq 'numericalresponse') {
270: my ($celllength,$number_of_tables,@table_range)=
271: &get_table_sizes($number_of_bubbles,$bubble_display);
272: my $j=0;
273: my $cou=0;
274: $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
275: for (my $i=0;$i<$number_of_tables;$i++) {
276: $result.='\vskip -1 mm \noindent \setlength{\tabcolsep}{2 mm}\begin{tabular}{';
277: for (my $ind=0;$ind<$table_range[$j];$ind++) {
278: $result.='p{3 mm}p{'.$celllength.' mm}';
279: }
280: $result.='}';
281: for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
282: $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_display->[$ind].'} ';
283: if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
284: }
285: $cou += $table_range[$j];
286: $j++;
287: $result.='\\\\\end{tabular}\vskip 0 mm ';
288: }
289: $result.='\end{enumerate}';
290: } else {
291: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
292: my $repetition = &Apache::response::repetition();
293: $result.='\begin{enumerate}';
294: for (my $i=0;$i<$repetition;$i++) {
295: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
296: }
297: $increment=$repetition;
298: $result.= '\end{enumerate}';
299: }
300: }
301: }
302: } elsif ($target eq 'edit') {
303: $result.='</td></tr>'.&Apache::edit::end_table;
304: } elsif ($target eq 'answer' || $target eq 'analyze') {
305:
306: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
307: if ($target eq 'analyze') {
308: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
309: $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
310: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
311: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
312: push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
313: &Apache::response::check_if_computed($token,$parstack,
314: $safeeval,'answer');
315: }
316: if (scalar(@$tagstack)) {
317: &Apache::response::setup_params($tag,$safeeval);
318: }
319: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
320: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
321: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
322: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
323:
324: if ($target eq 'answer') {
325: $result.=&Apache::response::answer_header($tag);
326: }
327: for(my $i=0;$i<=$#answers;$i++) {
328: my $ans=$answers[$i];
329: my $fmt=$formats[0];
330: if (@formats && $#formats) {$fmt=$formats[$i];}
331: my ($high,$low);
332: if ($Apache::inputtags::params{'tol'}) {
333: ($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
334: }
335: my ($sighigh,$siglow);
336: if ($Apache::inputtags::params{'sig'}) {
337: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
338: }
339: if ($fmt && $tag eq 'numericalresponse') {
340: $fmt=~s/e/E/g;
341: if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
342: if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
343: $ans = &format_number($ans,$fmt,$target,$safeeval);
344: #if ($high) {
345: # $high=&format_number($high,$fmt,$target,$safeeval);
346: # $low =&format_number($low,$fmt,$target,$safeeval);
347: #}
348: }
349: if ($target eq 'answer') {
350: if ($high && $tag eq 'numericalresponse') { $ans.=' ['.$low.','.$high.']'; }
351: if (defined($sighigh) && $tag eq 'numericalresponse') {
352: if ($env{'form.answer_output_mode'} eq 'tex') {
353: $ans.= " Sig $siglow - $sighigh";
354: } else {
355: $ans.= " Sig <i>$siglow - $sighigh</i>";
356: }
357: }
358: $result.=&Apache::response::answer_part($tag,$ans);
359: } elsif ($target eq 'analyze') {
360: push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
361: if ($high) {
362: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
363: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
364: }
365: if ($fmt) {
366: push (@{ $Apache::lonhomework::analyze{"$part_id.format"} }, $fmt);
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{'form.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: } else {
621: my $response = &Apache::response::getresponse();
622: if ( $response =~ /[^\s]/) {
623: my %previous = &Apache::response::check_for_previous($response,
624: $part,$id);
625: &Apache::lonxml::debug("submitted a $response<br>\n");
626: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
627: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
628: $response;
629: my ($ad,$msg);
630: if ($type eq 're' ) {
631: # if the RE wasn't in a var it likely got munged,
632: # thus grab it from the var directly
633: # my $testans=$token->[2]->{'answer'};
634: # if ($testans !~ m/^\s*\$/) {
635: # $answer=$token->[2]->{'answer'};
636: # }
637: ${$safeeval->varglob('LONCAPA::response')}=$response;
638: $result = &Apache::run::run('return $LONCAPA::response=~m'.$answer,$safeeval);
639: &Apache::lonxml::debug("current $response");
640: &Apache::lonxml::debug("current $answer");
641: $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
642: } else {
643: my $args_ref=
644: \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
645:
646: $$args_ref{'response'}=$response;
647: &Apache::lonxml::debug("current $response");
648: $$args_ref{'type'}=
649: &Apache::lonxml::get_param('type',$parstack,$safeeval);
650: foreach my $key (keys(%Apache::inputtags::params)) {
651: $$args_ref{$key}=$Apache::inputtags::params{$key};
652: }
653: &Apache::lonxml::debug('answer is'.join(':',$answer));
654: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=($answer);
655: ($result, my @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
656: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
657: my ($awards)=split(/:/,$result);
658: my (@awards) = split(/,/,$awards);
659: ($ad,$msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
660: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
661: }
662: if ($Apache::lonhomework::type eq 'survey' &&
663: ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
664: $ad eq 'EXACT_ANS')) {
665: $ad='SUBMITTED';
666: }
667: &Apache::response::handle_previous(\%previous,$ad);
668: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
669: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
670: }
671: }
672: } elsif ($target eq 'web' || $target eq 'tex') {
673: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
674: my $status = $Apache::inputtags::status['-1'];
675: if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
676: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
677: $increment = &Apache::response::repetition();
678: $result.='\begin{enumerate}';
679: for (my $i=0;$i<$increment;$i++) {
680: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
681: '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
682: }
683: $result.= '\end{enumerate}';
684: }
685: } elsif ($target eq 'answer' || $target eq 'analyze') {
686: if ($target eq 'analyze') {
687: push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
688: $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
689: &Apache::response::check_if_computed($token,$parstack,$safeeval,
690: 'answer');
691: }
692: &Apache::response::setup_params('stringresponse',$safeeval);
693: if ($target eq 'answer') {
694: $result.=&Apache::response::answer_header('stringresponse');
695: }
696: # foreach my $ans (@answers) {
697: if ($target eq 'answer') {
698: $result.=&Apache::response::answer_part('stringresponse',$answer);
699: } elsif ($target eq 'analyze') {
700: push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
701: $answer);
702: }
703: # }
704: my $string='Case Insensitive';
705: if ($type eq 'mc') {
706: $string='Multiple Choice';
707: } elsif ($type eq 'cs') {
708: $string='Case Sensitive';
709: } elsif ($type eq 'ci') {
710: $string='Case Insensitive';
711: } elsif ($type eq 're') {
712: $string='Regular Expression';
713: }
714: if ($target eq 'answer') {
715: if ($env{'form.answer_output_mode'} eq 'tex') {
716: $result.=&Apache::response::answer_part('stringresponse',
717: "$string");
718: } else {
719: $result.=&Apache::response::answer_part('stringresponse',
720: "<b>$string</b>");
721: }
722: } elsif ($target eq 'analyze') {
723: push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
724: $type);
725: }
726: if ($target eq 'answer') {
727: $result.=&Apache::response::answer_footer('stringresponse');
728: }
729: } elsif ($target eq 'edit') {
730: $result.='</td></tr>'.&Apache::edit::end_table;
731: }
732: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
733: $target eq 'tex' || $target eq 'analyze') {
734: &Apache::lonxml::increment_counter($increment);
735: }
736: &Apache::response::end_response;
737: return $result;
738: }
739:
740: sub start_formularesponse {
741: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
742: my $result;
743: if ($target eq 'meta') {
744: &Apache::response::start_response($parstack,$safeeval);
745: $result=&Apache::response::meta_package_write('formularesponse');
746: &Apache::response::end_response();
747: } else {
748: $result.=&start_numericalresponse(@_);
749: }
750: return $result;
751: }
752:
753: sub end_formularesponse {
754: return end_numericalresponse(@_);
755: }
756:
757: 1;
758: __END__
759:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>