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