Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.91
1.3 albertel 1: # The LearningOnline Network with CAPA
2: # caparesponse definition
1.47 albertel 3: #
1.91 ! albertel 4: # $Id: caparesponse.pm,v 1.90 2003/04/08 03:54:22 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') {
45: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
46: &Apache::loncommon::help_open_topic('Physical_Units');
47: $result.=&Apache::edit::text_arg('Format:','format',$token,4).
48: &Apache::loncommon::help_open_topic('Numerical_Response_Format');
49: } elsif ($token->[1] eq 'stringresponse') {
50: $result.=&Apache::edit::select_arg('Type:','type',
51: [['cs','Case Sensitive'],['ci','Case Insensitive'],
52: ['mc','Case Insensitive, Any Order']],$token);
53: } elsif ($token->[1] eq 'formularesponse') {
54: $result.=&Apache::edit::text_arg('Sample Points:','samples',
55: $token,40).
56: &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
57: }
58: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
59: } elsif ($target eq 'modified') {
60: my $constructtag;
61: if ($token->[1] eq 'numericalresponse') {
62: $constructtag=&Apache::edit::get_new_args($token,$parstack,
63: $safeeval,'answer',
64: 'unit','format');
65: } elsif ($token->[1] eq 'stringresponse') {
66: $constructtag=&Apache::edit::get_new_args($token,$parstack,
67: $safeeval,'answer',
68: 'type');
69: } elsif ($token->[1] eq 'formularesponse') {
70: $constructtag=&Apache::edit::get_new_args($token,$parstack,
71: $safeeval,'answer',
72: 'samples');
73: }
74: if ($constructtag) {
75: $result = &Apache::edit::rebuild_tag($token);
76: $result.=&Apache::edit::handle_insert();
1.22 albertel 77: }
1.89 albertel 78: } elsif ($target eq 'meta') {
79: $result=&Apache::response::meta_package_write('numericalresponse');
80: } elsif ($target eq 'answer' || $target eq 'grade') {
81: &Apache::response::reset_params();
1.16 albertel 82: }
1.89 albertel 83: return $result;
1.21 albertel 84: }
85:
1.89 albertel 86: sub end_numericalresponse {
87: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.90 albertel 88: my $increment=1;
1.89 albertel 89: my $result = '';
90: if (!$Apache::lonxml::default_homework_loaded) {
91: &Apache::lonxml::default_homework_load($safeeval);
1.34 albertel 92: }
1.90 albertel 93: if ( $target eq 'grade' && defined $ENV{'form.submitted'}) {
94: &Apache::response::setup_params($$tagstack[-1]);
95: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
96: my $partid = $Apache::inputtags::part;
97: my $id = $Apache::inputtags::response['-1'];
98: my $response = &Apache::response::getresponse();
99: if ( $response =~ /[^\s]/) {
100: my $ad;
101: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
102: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
103: &Apache::lonxml::debug("submitted a $response<br>\n");
104: &Apache::lonxml::debug($$parstack[$#$parstack] . "\n<br>");
105:
106: if ($ENV{'form.submitted'} eq 'scantron') {
107: &Apache::response::setrandomnumber();
108: #FIXME the 8 here is based off of number of powers need a parameter
109: my $ind=&Math::Random::random_uniform_integer(1,0,8);
110: if ($ind eq $response) { $ad='CORRECT'; } else { $ad='INCORRECT'; }
111: }
112: $response =~ s/\\/\\\\/g;
113: $response =~ s/\'/\\\'/g;
114: &Apache::lonxml::debug("current $response");
115: my $expression="&caparesponse_check_list('".$response."','".
116: $$parstack[-1];
117: foreach my $key (keys(%Apache::inputtags::params)) {
118: $expression.= ';my $'. #'
119: $key.'="'.$Apache::inputtags::params{$key}.'"';
120: }
121: if ($$tagstack[-1] eq 'formularesponse') {
122: $expression.=';my $type="fml";';
123: } elsif ($$tagstack[-1] eq 'numericalresponse') {
124: $expression.=';my $type="float";';
125: }
126: $expression.="');";
127: $result = &Apache::run::run($expression,$safeeval);
128: my ($awards) = split /:/ , $result;
129: ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
130: &Apache::lonxml::debug("$expression");
131: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
132: &Apache::response::handle_previous(\%previous,$ad);
133: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
134: $result='';
1.89 albertel 135: }
136: } elsif ($target eq 'web' || $target eq 'tex') {
1.90 albertel 137: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
138: $safeeval);
1.89 albertel 139: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
140: my $status = $Apache::inputtags::status['-1'];
141: if ($award =~ /^correct/ || $status eq "SHOW_ANSWER" ) {
142: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
143: $safeeval);
144: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
145: $safeeval);
146:
147: if ($target eq 'web') {
148: $result="<br />The correct answer is ";
149: } elsif ($target eq 'tex') {
150: #$result='\vskip 0 mm The correct answer is \\texttt{';
151: }
152: for (my $i=0; $i <= $#answers; $i++) {
153: my $answer=$answers[$i];
154: my $format;
155: if ($#formats > 0) {
156: $format=$formats[$i];
157: } else {
158: $format=$formats[0];
159: }
160: my $formatted;
161: if ((defined($format)) && ($format ne '')) {
162: &Apache::lonxml::debug("formatting with :$format: answer :$answer:");
163: $formatted=sprintf('%.'.$format,$answer).',';
164: } else {
165: &Apache::lonxml::debug("no format answer :$answer:");
166: $formatted="$answer,";
167: }
168:
169: if ($target eq 'tex') {
170: $formatted='';
171: #$formatted=&Apache::lonxml::latex_special_symbols($formatted);
172: }
173: $result.=$formatted;
174: }
175: chop $result;
176: if ($target eq 'web') {
177: $result.=" $unit.<br />";
178: } elsif ($target eq 'tex') {
179: #$result.=&Apache::lonxml::latex_special_symbols($unit);
180: #$result.="}. \\vskip 0 mm ";
181: }
182: }
183: if ($Apache::lonhomework::type eq 'exam') {
1.90 albertel 184: my $number_of_bubbles = 8; #default values for number of bubbles
1.89 albertel 185: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
186: $safeeval);
187: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
188: $safeeval);
1.90 albertel 189: my @bubble_values=&make_numerical_bubbles($number_of_bubbles,
1.91 ! albertel 190: $target,$answers[0],
! 191: $formats[0]);
1.89 albertel 192: my @alphabet=('A'..'Z');
1.90 albertel 193: my $id=$Apache::inputtags::response[-1];
1.89 albertel 194: if ($target eq 'web') {
195: if ($$tagstack[-1] eq 'numericalresponse') {
196: if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
197: $result.= '<table border="1"><tr>';
1.90 albertel 198: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
199: $result.='<td><input type="radio" name="HWVAL_'.$id.
1.91 ! albertel 200: '" value="'.$bubble_values[$ind].'"><b>'.
! 201: $alphabet[$ind].'</b>: '.
! 202: $bubble_values[$ind].'</td>';
1.89 albertel 203: }
204: $result.='</tr></table>';
205: } elsif ($$tagstack[-1] eq 'formularesponse') {
1.90 albertel 206: $result.= '<br /><br /><font color="red">
207: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
208: </textarea></font> <br /><br />';
1.89 albertel 209: }
210: } elsif ($target eq 'tex') {
211: if (defined $unit and $Apache::lonhomework::type eq 'exam') {
212: $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
213: }
214: if ($$tagstack[-1] eq 'numericalresponse') {
1.90 albertel 215: my ($celllength,$number_of_tables,@table_range)=
216: &get_table_sizes($formats[0],$number_of_bubbles);
1.89 albertel 217: my $j=0;
218: my $cou=0;
219: $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
220: for (my $i=0;$i<$number_of_tables;$i++) {
221: $result.='\vskip -1 mm \noindent \begin{tabular}{';
1.90 albertel 222: for (my $ind=0;$ind<$table_range[$j];$ind++) {
1.89 albertel 223: $result.='lp{'.$celllength.' mm}';
224: }
225: $result.='}';
1.90 albertel 226: for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
1.91 ! albertel 227: $result.='\hskip -3 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -2 mm & {\small '.$bubble_values[$ind].'} ';
1.89 albertel 228: if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
229: }
230: $cou += $table_range[$j];
231: $j++;
232: $result.='\\\\\end{tabular}\vskip 0 mm ';
233: }
234: $result.='\end{enumerate}';
235: } else {
236: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
237: my $id = $Apache::inputtags::part;
238: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
239: my $repetition = int $weight/9;
240: if ($weight % 9 != 0) {$repetition++;}
241: $result.='\begin{enumerate}';
242: for (my $i=0;$i<$repetition;$i++) {
243: $result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
244: }
1.90 albertel 245: $increment=$repetition;
1.89 albertel 246: $result.= '\end{enumerate}';
247: }
248: }
249: }
250: } elsif ($target eq 'edit') {
251: $result.='</td></tr>'.&Apache::edit::end_table;
252: } elsif ($target eq 'answer' || $target eq 'analyze') {
1.58 sakharuk 253:
1.89 albertel 254: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
255: if ($target eq 'analyze') {
256: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
257: $Apache::lonhomework::analyze{"$part_id.type"} = $$tagstack[-1];
258: }
259: &Apache::response::setup_params($$tagstack[-1]);
260: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.58 sakharuk 261: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
262: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
1.89 albertel 263: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
264:
265: if ($target eq 'answer') {
266: $result.=&Apache::response::answer_header($$tagstack[-1]);
267: }
268: for(my $i=0;$i<=$#answers;$i++) {
269: my $ans=$answers[$i];
1.90 albertel 270: my $fmt=$formats[0];
271: if (@formats && $#formats) {$fmt=$formats[$i];}
1.89 albertel 272: my ($high,$low);
273: if ($Apache::inputtags::params{'tol'}) {
274: ($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
275: }
276: my ($sighigh,$siglow);
277: if ($Apache::inputtags::params{'sig'}) {
278: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
279: }
280: if ($fmt) {
281: $ans = sprintf('%.'.$fmt,$ans);
282: if ($high) {
1.90 albertel 283: $high=sprintf('%.'.$fmt,$high);
284: $low =sprintf('%.'.$fmt,$low);
1.62 sakharuk 285: }
1.60 sakharuk 286: }
1.89 albertel 287: if ($target eq 'answer') {
288: if ($high) { $ans.=' ['.$low.','.$high.']'; }
289: if ($sighigh) { $ans.= " Sig <i>$siglow - $sighigh</i>"; }
290: $result.=&Apache::response::answer_part($$tagstack[-1],$ans);
291: } elsif ($target eq 'analyze') {
292: push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
293: if ($high) {
294: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
295: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
1.58 sakharuk 296: }
1.79 sakharuk 297: }
1.36 albertel 298: }
1.89 albertel 299: if ($unit) {
300: if ($target eq 'answer') {
301: $result.=&Apache::response::answer_part($$tagstack[-1],
302: "Unit: <b>$unit</b>");
303: } elsif ($target eq 'analyze') {
304: push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} },
305: $unit);
306: }
307: }
308: if ($type || $token->[1] eq 'stringresponse') {
309: my $string='Case Insensitive';
310: if ($type eq 'mc') {
311: $string='Multiple Choice';
312: } elsif ($type eq 'cs') {
313: $string='Case Sensitive';
314: } elsif ($type eq 'ci') {
315: $string='Case Insensitive';
316: } elsif ($type eq 'fml') {
317: $string='Formula';
318: }
319: if ($target eq 'answer') {
320: $result.=&Apache::response::answer_part($$tagstack[-1],
321: '<b>'.$string.'</b>');
322: } elsif ($target eq 'analyze') {
323: push (@{ $Apache::lonhomework::analyze{"$part_id.type"} },
324: $type);
325: }
326: }
327: if ($$tagstack[-1] eq 'formularesponse' && $target eq 'answer') {
328: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
329: $result.=&Apache::response::answer_part($$tagstack[-1],$samples);
330: }
331: if ($target eq 'answer') {
332: $result.=&Apache::response::answer_footer($$tagstack[-1]);
333: }
1.69 sakharuk 334: }
1.90 albertel 335: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
336: $target eq 'tex' || $target eq 'analyze') {
337: &Apache::lonxml::increment_counter($increment);
338: }
1.89 albertel 339: &Apache::response::end_response;
340: return $result;
1.90 albertel 341: }
342:
343: sub get_table_sizes {
344: my ($format,$number_of_bubbles)=@_;
345: my $max_val = 0;
346: if ($format=~m/^(\d+)E([^\d]*)(\d*)$/) {
347: $max_val=$1+$2+4;
348: } else {
349: $max_val=4;
350: }
351: $max_val = int(0.9*$ENV{'form.textwidth'}/(($max_val+6)*2));
352: my $celllength = 0.9*$ENV{'form.textwidth'}/$max_val-10;
353: my @table_range = ();
354: my $number_of_tables = int($number_of_bubbles/$max_val);
355: for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$max_val;}
356: if ($number_of_bubbles % $max_val != 0) {
357: $number_of_tables++;
358: push @table_range,($number_of_bubbles % $max_val);
359: }
360: return ($celllength,$number_of_tables,@table_range);
361: }
362:
363: sub format_number {
364: my ($number,$format,$target)=@_;
365: my $ans;
366: if ($format ne '') {
367: $ans = sprintf('%.'.$format,$number);
368: } else {
369: my $format = '';
370: #What is the number? (integer,decimal,floating point)
371: if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
372: $format = 'e'.$2;
373: } elsif ($number=~/^(\d*)\.(\d*)$/) {
374: $format = '4f';
375: } elsif ($number=~/^(\d*)$/) {
376: $format = 'd';
377: }
378: $ans = sprintf('%.'.$format,$number);
379: }
380: if ($target eq 'tex') {
381: if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
382: my $number = $1;
383: my $power = $2;
384: $power=~s/^\+//;
385: $power=~s/^(-?)0+(\d+)//;
386: $ans=$number.'$\times 10^{'.$1.$2.'}$'; #'stupidemacs
387: }
388: }
389: return $ans;
390: }
391:
392: sub make_numerical_bubbles {
1.91 ! albertel 393: my ($number_of_bubbles,$target,$answer,$format) =@_;
! 394: my @bubble_values = ();
1.90 albertel 395: my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
396: my @powers = (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0); #default values for powers
397: &Apache::response::setrandomnumber();
398: my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
399: my $power = $powers[$ind];
400: $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
401: my $factor = $factors[$ind];
402: for ($ind=0;$ind<$number_of_bubbles;$ind++) {
1.91 ! albertel 403: $bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
! 404: $bubble_values[$ind] = &format_number($bubble_values[$ind],
! 405: $format,$target);
! 406:
1.90 albertel 407: }
1.91 ! albertel 408: return @bubble_values;
1.51 albertel 409: }
410:
411: sub get_tolrange {
1.89 albertel 412: my ($ans,$tol)=@_;
413: my ($high,$low);
414: if ($tol =~ /%$/) {
415: chop($tol);
416: my $change=$ans*($tol/100.0);
417: $high=$ans+$change;
418: $low=$ans-$change;
419: } else {
420: $high=$ans+$tol;
421: $low=$ans-$tol;
422: }
423: return ($high,$low);
1.52 albertel 424: }
425:
426: sub get_sigrange {
1.89 albertel 427: my ($sig)=@_;
428: &Apache::lonxml::debug("Got a sig of :$sig:");
429: my $sig_lbound;
430: my $sig_ubound;
431: if ($sig eq '') {
432: $sig_lbound = 0; #SIG_LB_DEFAULT
433: $sig_ubound =15; #SIG_UB_DEFAULT
434: } else {
435: ($sig_lbound,$sig_ubound) = split(/,/,$sig);
436: if (!$sig_lbound) {
437: $sig_lbound = 0; #SIG_LB_DEFAULT
438: $sig_ubound =15; #SIG_UB_DEFAULT
439: }
440: if (!$sig_ubound) { $sig_ubound=$sig_lbound; }
1.52 albertel 441: }
1.89 albertel 442: return ($sig_ubound,$sig_lbound);
1.34 albertel 443: }
444:
445: sub start_stringresponse {
1.89 albertel 446: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
447: my $result;
448: if ($target eq 'meta') {
449: $result=&Apache::response::meta_package_write('stringresponse');
450: } else {
451: $result.=&start_numericalresponse(@_);
452: }
453: return $result;
1.34 albertel 454: }
455:
456: sub end_stringresponse {
1.89 albertel 457: return end_numericalresponse(@_);
1.44 albertel 458: }
459:
460: sub start_formularesponse {
1.89 albertel 461: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
462: my $result;
463: if ($target eq 'meta') {
464: $result=&Apache::response::meta_package_write('formularesponse');
465: } else {
466: $result.=&start_numericalresponse(@_);
467: }
468: return $result;
1.44 albertel 469: }
470:
471: sub end_formularesponse {
1.89 albertel 472: return end_numericalresponse(@_);
1.3 albertel 473: }
474:
1.1 albertel 475: 1;
1.3 albertel 476: __END__
1.89 albertel 477:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>