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