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