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