Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.122
1.3 albertel 1: # The LearningOnline Network with CAPA
2: # caparesponse definition
1.47 albertel 3: #
1.122 ! albertel 4: # $Id: caparesponse.pm,v 1.121 2003/10/24 18:08:24 sakharuk Exp $
1.47 albertel 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: #
1.3 albertel 28:
29: package Apache::caparesponse;
30: use strict;
1.4 albertel 31: use capa;
1.3 albertel 32:
1.50 harris41 33: BEGIN {
1.89 albertel 34: &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse','formularesponse'));
1.3 albertel 35: }
36:
1.89 albertel 37: sub start_numericalresponse {
38: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
39: my $id = &Apache::response::start_response($parstack,$safeeval);
40: my $result;
41: if ($target eq 'edit') {
42: $result.=&Apache::edit::tag_start($target,$token);
43: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
44: if ($token->[1] eq 'numericalresponse') {
1.122 ! albertel 45: $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
! 46: $token);
1.89 albertel 47: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
48: &Apache::loncommon::help_open_topic('Physical_Units');
49: $result.=&Apache::edit::text_arg('Format:','format',$token,4).
50: &Apache::loncommon::help_open_topic('Numerical_Response_Format');
51: } elsif ($token->[1] eq 'formularesponse') {
52: $result.=&Apache::edit::text_arg('Sample Points:','samples',
53: $token,40).
54: &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
55: }
56: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
57: } elsif ($target eq 'modified') {
58: my $constructtag;
59: if ($token->[1] eq 'numericalresponse') {
60: $constructtag=&Apache::edit::get_new_args($token,$parstack,
61: $safeeval,'answer',
1.117 albertel 62: 'incorrect','unit',
63: 'format');
1.89 albertel 64: } elsif ($token->[1] eq 'formularesponse') {
65: $constructtag=&Apache::edit::get_new_args($token,$parstack,
66: $safeeval,'answer',
67: 'samples');
68: }
69: if ($constructtag) {
70: $result = &Apache::edit::rebuild_tag($token);
71: $result.=&Apache::edit::handle_insert();
1.22 albertel 72: }
1.89 albertel 73: } elsif ($target eq 'meta') {
74: $result=&Apache::response::meta_package_write('numericalresponse');
75: } elsif ($target eq 'answer' || $target eq 'grade') {
76: &Apache::response::reset_params();
1.96 albertel 77: } elsif ($target eq 'web') {
78: my $partid = $Apache::inputtags::part;
79: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
80: &Apache::lonxml::debug("Got unit $hideunit for $partid $id");
81: #no way to enter units, with radio buttons
82: if (lc($hideunit) eq "yes") {
83: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
84: $safeeval);
85: if ($unit =~ /\S/) { $result.=" (in $unit) "; }
86: }
1.16 albertel 87: }
1.89 albertel 88: return $result;
1.21 albertel 89: }
90:
1.89 albertel 91: sub end_numericalresponse {
92: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.90 albertel 93: my $increment=1;
1.89 albertel 94: my $result = '';
95: if (!$Apache::lonxml::default_homework_loaded) {
96: &Apache::lonxml::default_homework_load($safeeval);
1.34 albertel 97: }
1.95 albertel 98: if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
1.90 albertel 99: &Apache::response::setup_params($$tagstack[-1]);
100: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
101: my $partid = $Apache::inputtags::part;
102: my $id = $Apache::inputtags::response['-1'];
1.95 albertel 103: if ($Apache::lonhomework::type eq 'exam' &&
1.122 ! albertel 104: $$tagstack[-1] eq 'formularesponse') {
1.95 albertel 105: $increment=&Apache::response::scored_response($partid,$id);
106: } else {
107: my $response = &Apache::response::getresponse();
108: if ( $response =~ /[^\s]/) {
109: my $ad;
110: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
111: &Apache::lonxml::debug("submitted a $response<br>\n");
112: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
113:
114: if ($ENV{'form.submitted'} eq 'scantron') {
1.117 albertel 115: my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
116: if (!$number_of_bubbles) { $number_of_bubbles=8; }
117: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
118: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
119: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
120: my @values=&make_numerical_bubbles($number_of_bubbles,$target,$answers[0],$formats[0],\@incorrect);
1.95 albertel 121: $response=$values[$response];
122: } else {
123: $response =~ s/\\/\\\\/g;
124: $response =~ s/\'/\\\'/g;
125: }
1.115 albertel 126: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1.95 albertel 127: &Apache::lonxml::debug("current $response");
128: my $expression="&caparesponse_check_list('".$response."','".
129: $$parstack[-1];
1.96 albertel 130: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
1.95 albertel 131: #no way to enter units, with radio buttons
1.96 albertel 132: if ($Apache::lonhomework::type eq 'exam' ||
133: lc($hideunit) eq "yes") {
1.95 albertel 134: $expression.=';my $unit=undef;';
135: }
136: foreach my $key (keys(%Apache::inputtags::params)) {
137: $expression.= ';my $'. #'
138: $key.'="'.$Apache::inputtags::params{$key}.'"';
139: }
140: if ($$tagstack[-1] eq 'formularesponse') {
141: $expression.=';my $type="fml";';
142: } elsif ($$tagstack[-1] eq 'numericalresponse') {
143: $expression.=';my $type="float";';
144: }
145: $expression.="');";
1.105 albertel 146: my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
147: &Apache::lonxml::debug('answer is'.join(':',@answer));
148: @{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=@answer;
149:
1.95 albertel 150: $result = &Apache::run::run($expression,$safeeval);
151: my ($awards) = split /:/ , $result;
152: ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
153: &Apache::lonxml::debug("$expression");
154: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
155: &Apache::response::handle_previous(\%previous,$ad);
156: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
157: $result='';
1.90 albertel 158: }
1.89 albertel 159: }
160: } elsif ($target eq 'web' || $target eq 'tex') {
1.90 albertel 161: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
162: $safeeval);
1.89 albertel 163: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
164: my $status = $Apache::inputtags::status['-1'];
1.114 albertel 165: if ( &Apache::response::show_answer() ) {
1.89 albertel 166: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
167: $safeeval);
168: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
169: $safeeval);
170: if ($target eq 'web') {
171: $result="<br />The correct answer is ";
172: }
173: for (my $i=0; $i <= $#answers; $i++) {
174: my $answer=$answers[$i];
175: my $format;
176: if ($#formats > 0) {
177: $format=$formats[$i];
178: } else {
179: $format=$formats[0];
180: }
181: my $formatted;
182: if ((defined($format)) && ($format ne '')) {
1.109 albertel 183: $format=~s/e/E/g;
1.89 albertel 184: &Apache::lonxml::debug("formatting with :$format: answer :$answer:");
185: $formatted=sprintf('%.'.$format,$answer).',';
186: } else {
187: &Apache::lonxml::debug("no format answer :$answer:");
188: $formatted="$answer,";
189: }
190: if ($target eq 'tex') {
191: $formatted='';
192: #$formatted=&Apache::lonxml::latex_special_symbols($formatted);
193: }
194: $result.=$formatted;
195: }
196: chop $result;
197: if ($target eq 'web') {
198: $result.=" $unit.<br />";
199: }
200: }
201: if ($Apache::lonhomework::type eq 'exam') {
1.118 albertel 202: my $partid=$Apache::inputtags::part;
203: my $id=$Apache::inputtags::response[-1];
1.117 albertel 204: my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
205: if (!$number_of_bubbles) { $number_of_bubbles=8; }
1.89 albertel 206: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
207: $safeeval);
208: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
209: $safeeval);
1.117 albertel 210: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
1.90 albertel 211: my @bubble_values=&make_numerical_bubbles($number_of_bubbles,
1.91 albertel 212: $target,$answers[0],
1.117 albertel 213: $formats[0],\@incorrect);
1.89 albertel 214: my @alphabet=('A'..'Z');
215: if ($target eq 'web') {
216: if ($$tagstack[-1] eq 'numericalresponse') {
217: if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
218: $result.= '<table border="1"><tr>';
1.116 albertel 219: my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
1.90 albertel 220: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
1.116 albertel 221: my $checked='';
222: if ($previous eq $bubble_values[$ind]) {
223: $checked=" checked='on' ";
224: }
1.90 albertel 225: $result.='<td><input type="radio" name="HWVAL_'.$id.
1.116 albertel 226: '" value="'.$bubble_values[$ind].'" '.$checked
227: .' /><b>'.$alphabet[$ind].'</b>: '.
228: $bubble_values[$ind].'</td>';
1.89 albertel 229: }
230: $result.='</tr></table>';
231: } elsif ($$tagstack[-1] eq 'formularesponse') {
1.90 albertel 232: $result.= '<br /><br /><font color="red">
233: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
234: </textarea></font> <br /><br />';
1.89 albertel 235: }
236: } elsif ($target eq 'tex') {
1.107 sakharuk 237: if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
1.89 albertel 238: $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
239: }
240: if ($$tagstack[-1] eq 'numericalresponse') {
1.90 albertel 241: my ($celllength,$number_of_tables,@table_range)=
242: &get_table_sizes($formats[0],$number_of_bubbles);
1.89 albertel 243: my $j=0;
244: my $cou=0;
245: $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
246: for (my $i=0;$i<$number_of_tables;$i++) {
247: $result.='\vskip -1 mm \noindent \begin{tabular}{';
1.90 albertel 248: for (my $ind=0;$ind<$table_range[$j];$ind++) {
1.89 albertel 249: $result.='lp{'.$celllength.' mm}';
250: }
251: $result.='}';
1.90 albertel 252: for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
1.91 albertel 253: $result.='\hskip -3 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -2 mm & {\small '.$bubble_values[$ind].'} ';
1.89 albertel 254: if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
255: }
256: $cou += $table_range[$j];
257: $j++;
258: $result.='\\\\\end{tabular}\vskip 0 mm ';
259: }
260: $result.='\end{enumerate}';
261: } else {
262: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
1.93 albertel 263: my $repetition = &Apache::response::repetition();
1.89 albertel 264: $result.='\begin{enumerate}';
265: for (my $i=0;$i<$repetition;$i++) {
1.93 albertel 266: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
1.89 albertel 267: }
1.90 albertel 268: $increment=$repetition;
1.89 albertel 269: $result.= '\end{enumerate}';
270: }
271: }
272: }
273: } elsif ($target eq 'edit') {
274: $result.='</td></tr>'.&Apache::edit::end_table;
275: } elsif ($target eq 'answer' || $target eq 'analyze') {
1.58 sakharuk 276:
1.89 albertel 277: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
278: if ($target eq 'analyze') {
279: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
280: $Apache::lonhomework::analyze{"$part_id.type"} = $$tagstack[-1];
1.117 albertel 281: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
282: push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
1.89 albertel 283: }
284: &Apache::response::setup_params($$tagstack[-1]);
285: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.58 sakharuk 286: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
287: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
1.89 albertel 288: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
289:
290: if ($target eq 'answer') {
291: $result.=&Apache::response::answer_header($$tagstack[-1]);
292: }
293: for(my $i=0;$i<=$#answers;$i++) {
294: my $ans=$answers[$i];
1.90 albertel 295: my $fmt=$formats[0];
296: if (@formats && $#formats) {$fmt=$formats[$i];}
1.89 albertel 297: my ($high,$low);
298: if ($Apache::inputtags::params{'tol'}) {
299: ($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
300: }
301: my ($sighigh,$siglow);
302: if ($Apache::inputtags::params{'sig'}) {
303: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
304: }
1.102 albertel 305: if ($fmt && $$tagstack[-1] eq 'numericalresponse') {
1.112 albertel 306: $fmt=~s/e/E/g;
1.89 albertel 307: $ans = sprintf('%.'.$fmt,$ans);
308: if ($high) {
1.90 albertel 309: $high=sprintf('%.'.$fmt,$high);
310: $low =sprintf('%.'.$fmt,$low);
1.62 sakharuk 311: }
1.60 sakharuk 312: }
1.89 albertel 313: if ($target eq 'answer') {
1.102 albertel 314: if ($high && $$tagstack[-1] eq 'numericalresponse') { $ans.=' ['.$low.','.$high.']'; }
315: if ($sighigh && $$tagstack[-1] eq 'numericalresponse') {
1.101 albertel 316: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.106 sakharuk 317: $ans.= " Sig $siglow - $sighigh";
1.98 sakharuk 318: } else {
1.101 albertel 319: $ans.= " Sig <i>$siglow - $sighigh</i>";
1.98 sakharuk 320: }
321: }
1.89 albertel 322: $result.=&Apache::response::answer_part($$tagstack[-1],$ans);
323: } elsif ($target eq 'analyze') {
324: push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
325: if ($high) {
326: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
327: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
1.58 sakharuk 328: }
1.79 sakharuk 329: }
1.36 albertel 330: }
1.102 albertel 331: if (defined($unit) and ($unit ne '') and
332: $$tagstack[-1] eq 'numericalresponse') {
1.89 albertel 333: if ($target eq 'answer') {
1.101 albertel 334: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.98 sakharuk 335: $result.=&Apache::response::answer_part($$tagstack[-1],
1.106 sakharuk 336: " Unit: $unit ");
1.98 sakharuk 337: } else {
338: $result.=&Apache::response::answer_part($$tagstack[-1],
339: "Unit: <b>$unit</b>");
340: }
1.89 albertel 341: } elsif ($target eq 'analyze') {
1.103 albertel 342: push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
1.89 albertel 343: }
344: }
345: if ($$tagstack[-1] eq 'formularesponse' && $target eq 'answer') {
346: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
347: $result.=&Apache::response::answer_part($$tagstack[-1],$samples);
348: }
349: if ($target eq 'answer') {
350: $result.=&Apache::response::answer_footer($$tagstack[-1]);
351: }
1.69 sakharuk 352: }
1.121 sakharuk 353: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.90 albertel 354: $target eq 'tex' || $target eq 'analyze') {
355: &Apache::lonxml::increment_counter($increment);
356: }
1.89 albertel 357: &Apache::response::end_response;
358: return $result;
1.90 albertel 359: }
360:
361: sub get_table_sizes {
362: my ($format,$number_of_bubbles)=@_;
363: my $max_val = 0;
364: if ($format=~m/^(\d+)E([^\d]*)(\d*)$/) {
365: $max_val=$1+$2+4;
366: } else {
367: $max_val=4;
368: }
369: $max_val = int(0.9*$ENV{'form.textwidth'}/(($max_val+6)*2));
370: my $celllength = 0.9*$ENV{'form.textwidth'}/$max_val-10;
371: my @table_range = ();
372: my $number_of_tables = int($number_of_bubbles/$max_val);
373: for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$max_val;}
374: if ($number_of_bubbles % $max_val != 0) {
375: $number_of_tables++;
376: push @table_range,($number_of_bubbles % $max_val);
377: }
378: return ($celllength,$number_of_tables,@table_range);
379: }
380:
381: sub format_number {
382: my ($number,$format,$target)=@_;
383: my $ans;
384: if ($format ne '') {
1.109 albertel 385: $format=~s/e/E/g;
1.90 albertel 386: $ans = sprintf('%.'.$format,$number);
387: } else {
388: my $format = '';
389: #What is the number? (integer,decimal,floating point)
390: if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
1.113 sakharuk 391: $format = '3e';
1.90 albertel 392: } elsif ($number=~/^(\d*)\.(\d*)$/) {
393: $format = '4f';
394: } elsif ($number=~/^(\d*)$/) {
395: $format = 'd';
396: }
397: $ans = sprintf('%.'.$format,$number);
398: }
399: if ($target eq 'tex') {
400: if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
401: my $number = $1;
402: my $power = $2;
403: $power=~s/^\+//;
404: $power=~s/^(-?)0+(\d+)//;
405: $ans=$number.'$\times 10^{'.$1.$2.'}$'; #'stupidemacs
406: }
407: }
408: return $ans;
409: }
410:
411: sub make_numerical_bubbles {
1.117 albertel 412: my ($number_of_bubbles,$target,$answer,$format,$incorrect) =@_;
1.91 albertel 413: my @bubble_values = ();
1.119 albertel 414: &Apache::lonxml::debug("answer is $answer incorrect is $incorrect");
415: my @oldseed=&Math::Random::random_get_seed();
1.117 albertel 416: if (defined($incorrect) && ref($incorrect)) {
417: &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1 gt $number_of_bubbles));
418: if (scalar(@$incorrect)+1 >= $number_of_bubbles) {
419: &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1).":$number_of_bubbles");
420: &Apache::response::setrandomnumber();
421: my @rand_inc=&Math::Random::random_permutation(@$incorrect);
422: @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
423: @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
424: &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": whih are :".join(':',@bubble_values));
1.119 albertel 425: &Math::Random::random_set_seed(@oldseed);
1.117 albertel 426: return @bubble_values;
427: }
428: #FIXME what to do when not enough incorrects specified?
429: }
1.90 albertel 430: my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
431: my @powers = (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0); #default values for powers
432: &Apache::response::setrandomnumber();
433: my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
434: my $power = $powers[$ind];
435: $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
436: my $factor = $factors[$ind];
437: for ($ind=0;$ind<$number_of_bubbles;$ind++) {
1.91 albertel 438: $bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
439: $bubble_values[$ind] = &format_number($bubble_values[$ind],
440: $format,$target);
441:
1.90 albertel 442: }
1.119 albertel 443: &Math::Random::random_set_seed(@oldseed);
1.91 albertel 444: return @bubble_values;
1.51 albertel 445: }
446:
447: sub get_tolrange {
1.89 albertel 448: my ($ans,$tol)=@_;
449: my ($high,$low);
450: if ($tol =~ /%$/) {
451: chop($tol);
452: my $change=$ans*($tol/100.0);
453: $high=$ans+$change;
454: $low=$ans-$change;
455: } else {
456: $high=$ans+$tol;
457: $low=$ans-$tol;
458: }
459: return ($high,$low);
1.52 albertel 460: }
461:
462: sub get_sigrange {
1.89 albertel 463: my ($sig)=@_;
464: &Apache::lonxml::debug("Got a sig of :$sig:");
465: my $sig_lbound;
466: my $sig_ubound;
467: if ($sig eq '') {
468: $sig_lbound = 0; #SIG_LB_DEFAULT
469: $sig_ubound =15; #SIG_UB_DEFAULT
470: } else {
471: ($sig_lbound,$sig_ubound) = split(/,/,$sig);
472: if (!$sig_lbound) {
473: $sig_lbound = 0; #SIG_LB_DEFAULT
474: $sig_ubound =15; #SIG_UB_DEFAULT
475: }
476: if (!$sig_ubound) { $sig_ubound=$sig_lbound; }
1.52 albertel 477: }
1.89 albertel 478: return ($sig_ubound,$sig_lbound);
1.34 albertel 479: }
480:
481: sub start_stringresponse {
1.89 albertel 482: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
483: my $result;
1.122 ! albertel 484: my $id = &Apache::response::start_response($parstack,$safeeval);
1.89 albertel 485: if ($target eq 'meta') {
1.104 bowersj2 486: &Apache::response::start_response($parstack,$safeeval);
1.89 albertel 487: $result=&Apache::response::meta_package_write('stringresponse');
1.104 bowersj2 488: &Apache::response::end_response();
1.122 ! albertel 489: } elsif ($target eq 'edit') {
! 490: $result.=&Apache::edit::tag_start($target,$token);
! 491: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
! 492: $result.=&Apache::edit::select_arg('Type:','type',
! 493: [['cs','Case Sensitive'],['ci','Case Insensitive'],
! 494: ['mc','Case Insensitive, Any Order'],
! 495: ['re','Regular Expression']],$token);
! 496: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
! 497: } elsif ($target eq 'modified') {
! 498: my $constructtag;
! 499: $constructtag=&Apache::edit::get_new_args($token,$parstack,
! 500: $safeeval,'answer',
! 501: 'type');
! 502: if ($constructtag) {
! 503: $result = &Apache::edit::rebuild_tag($token);
! 504: $result.=&Apache::edit::handle_insert();
! 505: }
! 506: } elsif ($target eq 'answer' || $target eq 'grade') {
! 507: &Apache::response::reset_params();
1.89 albertel 508: }
509: return $result;
1.34 albertel 510: }
511:
512: sub end_stringresponse {
1.122 ! albertel 513: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
! 514: my $increment=1;
! 515: my $result = '';
! 516: my $part=$Apache::inputtags::part;
! 517: my $id=$Apache::inputtags::response[-1];
! 518: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
! 519: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
! 520: &Apache::lonxml::debug("current $answer ".$token->[2]);
! 521: if (!$Apache::lonxml::default_homework_loaded) {
! 522: &Apache::lonxml::default_homework_load($safeeval);
! 523: }
! 524: if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
! 525: &Apache::response::setup_params($$tagstack[-1]);
! 526: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
! 527: if ($Apache::lonhomework::type eq 'exam' ||
! 528: $ENV{'form.submitted'} eq 'scantron') {
! 529: $increment=&Apache::response::scored_response($part,$id);
! 530: } else {
! 531: my $response = &Apache::response::getresponse();
! 532: if ( $response =~ /[^\s]/) {
! 533: my %previous = &Apache::response::check_for_previous($response,
! 534: $part,$id);
! 535: &Apache::lonxml::debug("submitted a $response<br>\n");
! 536: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
! 537:
! 538: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
! 539: $response;
! 540: my $ad;
! 541: if ($type eq 're' ) {
! 542: # if the RE wasn't in a var it likely got munged,
! 543: # thus grab it from the var directly
! 544: # my $testans=$token->[2]->{'answer'};
! 545: # if ($testans !~ m/^\s*\$/) {
! 546: # $answer=$token->[2]->{'answer'};
! 547: # }
! 548: ${$safeeval->varglob('LONCAPA_INTERNAL_response')}=
! 549: $response;
! 550: $result = &Apache::run::run('return $LONCAPA_INTERNAL_response=~m'.$answer,$safeeval);
! 551: &Apache::lonxml::debug("current $response");
! 552: &Apache::lonxml::debug("current $answer");
! 553: $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
! 554: } else {
! 555: $response =~ s/\\/\\\\/g;
! 556: $response =~ s/\'/\\\'/g;
! 557: &Apache::lonxml::debug("current $response");
! 558: my $expression="&caparesponse_check_list('".$response."','".
! 559: $$parstack[-1];
! 560: foreach my $key (keys(%Apache::inputtags::params)) {
! 561: $expression.= ';my $'. #'
! 562: $key.'="'.$Apache::inputtags::params{$key}.'"';
! 563: }
! 564: $expression.="');";
! 565: &Apache::lonxml::debug('answer is'.join(':',$answer));
! 566: @{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=($answer);
! 567: $result = &Apache::run::run($expression,$safeeval);
! 568: my ($awards) = split /:/ , $result;
! 569: ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
! 570: &Apache::lonxml::debug("$expression");
! 571: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
! 572: }
! 573: &Apache::response::handle_previous(\%previous,$ad);
! 574: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
! 575: }
! 576: }
! 577: } elsif ($target eq 'web' || $target eq 'tex') {
! 578: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
! 579: my $status = $Apache::inputtags::status['-1'];
! 580: if ( &Apache::response::show_answer() ) {
! 581: if ($target eq 'web') {
! 582: $result="<br />The correct answer is ".$answer;
! 583: # join(', ',@answers).".<br />";
! 584: }
! 585: }
! 586: if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
! 587: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
! 588: $increment = &Apache::response::repetition();
! 589: $result.='\begin{enumerate}';
! 590: for (my $i=0;$i<$increment;$i++) {
! 591: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
! 592: '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
! 593: }
! 594: $result.= '\end{enumerate}';
! 595: }
! 596: } elsif ($target eq 'answer' || $target eq 'analyze') {
! 597: if ($target eq 'analyze') {
! 598: push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
! 599: $Apache::lonhomework::analyze{"$part.$id.type"} = $$tagstack[-1];
! 600: }
! 601: &Apache::response::setup_params($$tagstack[-1]);
! 602: if ($target eq 'answer') {
! 603: $result.=&Apache::response::answer_header($$tagstack[-1]);
! 604: }
! 605: # foreach my $ans (@answers) {
! 606: if ($target eq 'answer') {
! 607: $result.=&Apache::response::answer_part($$tagstack[-1],$answer);
! 608: } elsif ($target eq 'analyze') {
! 609: push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
! 610: $answer);
! 611: }
! 612: # }
! 613: my $string='Case Insensitive';
! 614: if ($type eq 'mc') {
! 615: $string='Multiple Choice';
! 616: } elsif ($type eq 'cs') {
! 617: $string='Case Sensitive';
! 618: } elsif ($type eq 'ci') {
! 619: $string='Case Insensitive';
! 620: } elsif ($type eq 're') {
! 621: $string='Regular Expression';
! 622: }
! 623: if ($target eq 'answer') {
! 624: if ($ENV{'form.answer_output_mode'} eq 'tex') {
! 625: $result.=&Apache::response::answer_part($$tagstack[-1],
! 626: "$string");
! 627: } else {
! 628: $result.=&Apache::response::answer_part($$tagstack[-1],
! 629: "<b>$string</b>");
! 630: }
! 631: } elsif ($target eq 'analyze') {
! 632: push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
! 633: $type);
! 634: }
! 635: if ($target eq 'answer') {
! 636: $result.=&Apache::response::answer_footer($$tagstack[-1]);
! 637: }
! 638: } elsif ($target eq 'edit') {
! 639: $result.='</td></tr>'.&Apache::edit::end_table;
! 640: }
! 641: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
! 642: $target eq 'tex' || $target eq 'analyze') {
! 643: &Apache::lonxml::increment_counter($increment);
! 644: }
! 645: &Apache::response::end_response;
! 646: return $result;
1.44 albertel 647: }
648:
649: sub start_formularesponse {
1.89 albertel 650: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
651: my $result;
652: if ($target eq 'meta') {
1.104 bowersj2 653: &Apache::response::start_response($parstack,$safeeval);
1.89 albertel 654: $result=&Apache::response::meta_package_write('formularesponse');
1.104 bowersj2 655: &Apache::response::end_response();
1.89 albertel 656: } else {
657: $result.=&start_numericalresponse(@_);
658: }
659: return $result;
1.44 albertel 660: }
661:
662: sub end_formularesponse {
1.89 albertel 663: return end_numericalresponse(@_);
1.3 albertel 664: }
665:
1.1 albertel 666: 1;
1.3 albertel 667: __END__
1.89 albertel 668:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>