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