Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.147
1.3 albertel 1: # The LearningOnline Network with CAPA
2: # caparesponse definition
1.47 albertel 3: #
1.147 ! albertel 4: # $Id: caparesponse.pm,v 1.146 2004/06/04 22:06:07 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.129 www 32: use Apache::lonlocal;
1.3 albertel 33:
1.50 harris41 34: BEGIN {
1.89 albertel 35: &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse','formularesponse'));
1.3 albertel 36: }
37:
1.89 albertel 38: sub start_numericalresponse {
39: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
40: my $id = &Apache::response::start_response($parstack,$safeeval);
41: my $result;
1.143 albertel 42: undef %{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
1.89 albertel 43: if ($target eq 'edit') {
44: $result.=&Apache::edit::tag_start($target,$token);
45: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
46: if ($token->[1] eq 'numericalresponse') {
1.122 albertel 47: $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
48: $token);
1.89 albertel 49: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
50: &Apache::loncommon::help_open_topic('Physical_Units');
51: $result.=&Apache::edit::text_arg('Format:','format',$token,4).
52: &Apache::loncommon::help_open_topic('Numerical_Response_Format');
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',
1.117 albertel 64: 'incorrect','unit',
65: 'format');
1.89 albertel 66: } elsif ($token->[1] eq 'formularesponse') {
67: $constructtag=&Apache::edit::get_new_args($token,$parstack,
68: $safeeval,'answer',
69: 'samples');
70: }
71: if ($constructtag) {
72: $result = &Apache::edit::rebuild_tag($token);
73: $result.=&Apache::edit::handle_insert();
1.22 albertel 74: }
1.89 albertel 75: } elsif ($target eq 'meta') {
76: $result=&Apache::response::meta_package_write('numericalresponse');
77: } elsif ($target eq 'answer' || $target eq 'grade') {
78: &Apache::response::reset_params();
1.96 albertel 79: } elsif ($target eq 'web') {
80: my $partid = $Apache::inputtags::part;
81: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
82: &Apache::lonxml::debug("Got unit $hideunit for $partid $id");
83: #no way to enter units, with radio buttons
84: if (lc($hideunit) eq "yes") {
85: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
86: $safeeval);
87: if ($unit =~ /\S/) { $result.=" (in $unit) "; }
88: }
1.146 albertel 89: if ( &Apache::response::show_answer() ) {
90: my $answertxt;
91: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
92: $safeeval);
93: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
94: $safeeval);
95: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
96: $safeeval);
97: for (my $i=0; $i <= $#answers; $i++) {
98: my $answer=$answers[$i];
99: my $format;
100: if ($#formats > 0) {
101: $format=$formats[$i];
102: } else {
103: $format=$formats[0];
104: }
105: my $formatted;
106: if ((defined($format)) && ($format ne '')) {
107: $format=~s/e/E/g;
108: &Apache::lonxml::debug("formatting with :$format: answer :$answer:");
109: $formatted=sprintf('%.'.$format,$answer).',';
110: } else {
111: &Apache::lonxml::debug("no format answer :$answer:");
112: $formatted="$answer,";
113: }
114: $answertxt.=$formatted;
115: }
116: chop $answertxt;
117: if ($target eq 'web') {
118: $answertxt.=" $unit.<br />";
119: }
120: $Apache::inputtags::answertxt{$id}=$answertxt;
121: }
1.16 albertel 122: }
1.89 albertel 123: return $result;
1.21 albertel 124: }
125:
1.89 albertel 126: sub end_numericalresponse {
127: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.90 albertel 128: my $increment=1;
1.89 albertel 129: my $result = '';
130: if (!$Apache::lonxml::default_homework_loaded) {
131: &Apache::lonxml::default_homework_load($safeeval);
1.34 albertel 132: }
1.125 albertel 133: my $tag;
134: if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
1.95 albertel 135: if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
1.140 albertel 136: &Apache::response::setup_params($tag,$safeeval);
1.90 albertel 137: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
138: my $partid = $Apache::inputtags::part;
139: my $id = $Apache::inputtags::response['-1'];
1.95 albertel 140: if ($Apache::lonhomework::type eq 'exam' &&
1.125 albertel 141: $tag eq 'formularesponse') {
1.95 albertel 142: $increment=&Apache::response::scored_response($partid,$id);
143: } else {
144: my $response = &Apache::response::getresponse();
145: if ( $response =~ /[^\s]/) {
146: my $ad;
147: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
148: &Apache::lonxml::debug("submitted a $response<br>\n");
149: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
150:
151: if ($ENV{'form.submitted'} eq 'scantron') {
1.117 albertel 152: my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
153: if (!$number_of_bubbles) { $number_of_bubbles=8; }
154: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
155: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
156: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
157: my @values=&make_numerical_bubbles($number_of_bubbles,$target,$answers[0],$formats[0],\@incorrect);
1.95 albertel 158: $response=$values[$response];
159: }
1.115 albertel 160: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1.143 albertel 161: my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
162: $$args_ref{'response'}=$response;
1.96 albertel 163: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
1.123 albertel 164:
1.143 albertel 165: foreach my $arg ('type','tol','sig','ans_fmt','unit','calc',
166: 'samples') {
167: $$args_ref{$arg}=
168: &Apache::lonxml::get_param($arg,$parstack,$safeeval);
169: }
1.123 albertel 170: foreach my $key (keys(%Apache::inputtags::params)) {
1.143 albertel 171: $$args_ref{$key}=$Apache::inputtags::params{$key};
1.123 albertel 172: }
173:
1.95 albertel 174: #no way to enter units, with radio buttons
1.96 albertel 175: if ($Apache::lonhomework::type eq 'exam' ||
176: lc($hideunit) eq "yes") {
1.143 albertel 177: delete($$args_ref{'unit'});
1.95 albertel 178: }
1.123 albertel 179: #sig fig don't make much sense either
180: if (($Apache::lonhomework::type eq 'exam' ||
181: $ENV{'form.submitted'} eq 'scantron') &&
1.125 albertel 182: $tag eq 'numericalresponse') {
1.143 albertel 183: delete($$args_ref{'sig'});
1.95 albertel 184: }
1.123 albertel 185:
1.125 albertel 186: if ($tag eq 'formularesponse') {
1.143 albertel 187: $$args_ref{'type'}='fml';
1.125 albertel 188: } elsif ($tag eq 'numericalresponse') {
1.143 albertel 189: $$args_ref{'type'}='float';
1.95 albertel 190: }
1.105 albertel 191: my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
192: &Apache::lonxml::debug('answer is'.join(':',@answer));
1.143 albertel 193: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=@answer;
1.105 albertel 194:
1.143 albertel 195: ($result,my @msgs) =
196: &Apache::run::run("&caparesponse_check_list()",$safeeval);
1.141 albertel 197: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
1.142 albertel 198: my ($awards)=split(/:/,$result);
199: my (@awards) = split(/,/,$awards);
200: ($ad,my $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
201: &Apache::lonxml::debug('ad is'.$ad);
202: if ($ad eq 'SIG_FAIL') {
203: my ($sig_u,$sig_l)=
204: &get_sigrange($Apache::inputtags::params{'sig'});
205: $msg=join(':',$msg,$sig_l,$sig_u);
206: &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
207: $Apache::inputtags::params{'sig'});
208: }
1.95 albertel 209: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
210: &Apache::response::handle_previous(\%previous,$ad);
211: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1.142 albertel 212: $Apache::lonhomework::results{"resource.$partid.$id.awardmsg"}=$msg;
1.95 albertel 213: $result='';
1.90 albertel 214: }
1.89 albertel 215: }
216: } elsif ($target eq 'web' || $target eq 'tex') {
1.90 albertel 217: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
218: $safeeval);
1.89 albertel 219: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
220: my $status = $Apache::inputtags::status['-1'];
221: if ($Apache::lonhomework::type eq 'exam') {
1.118 albertel 222: my $partid=$Apache::inputtags::part;
223: my $id=$Apache::inputtags::response[-1];
1.117 albertel 224: my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
1.126 albertel 225: if ($Apache::inputtags::params{'numbubbles'}) {
226: $number_of_bubbles = $Apache::inputtags::params{'numbubbles'};
227: }
1.117 albertel 228: if (!$number_of_bubbles) { $number_of_bubbles=8; }
1.126 albertel 229:
1.89 albertel 230: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
231: $safeeval);
232: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
233: $safeeval);
1.117 albertel 234: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
1.90 albertel 235: my @bubble_values=&make_numerical_bubbles($number_of_bubbles,
1.91 albertel 236: $target,$answers[0],
1.117 albertel 237: $formats[0],\@incorrect);
1.89 albertel 238: my @alphabet=('A'..'Z');
239: if ($target eq 'web') {
1.125 albertel 240: if ($tag eq 'numericalresponse') {
1.89 albertel 241: if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
242: $result.= '<table border="1"><tr>';
1.116 albertel 243: my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
1.90 albertel 244: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
1.116 albertel 245: my $checked='';
246: if ($previous eq $bubble_values[$ind]) {
247: $checked=" checked='on' ";
248: }
1.90 albertel 249: $result.='<td><input type="radio" name="HWVAL_'.$id.
1.116 albertel 250: '" value="'.$bubble_values[$ind].'" '.$checked
251: .' /><b>'.$alphabet[$ind].'</b>: '.
252: $bubble_values[$ind].'</td>';
1.89 albertel 253: }
254: $result.='</tr></table>';
1.125 albertel 255: } elsif ($tag eq 'formularesponse') {
1.90 albertel 256: $result.= '<br /><br /><font color="red">
257: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
258: </textarea></font> <br /><br />';
1.89 albertel 259: }
260: } elsif ($target eq 'tex') {
1.107 sakharuk 261: if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
1.89 albertel 262: $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
263: }
1.125 albertel 264: if ($tag eq 'numericalresponse') {
1.90 albertel 265: my ($celllength,$number_of_tables,@table_range)=
1.128 sakharuk 266: &get_table_sizes($number_of_bubbles,\@bubble_values);
1.89 albertel 267: my $j=0;
268: my $cou=0;
269: $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
270: for (my $i=0;$i<$number_of_tables;$i++) {
1.144 sakharuk 271: $result.='\vskip -1 mm \noindent \setlength{\tabcolsep}{2 mm}\begin{tabular}{';
1.90 albertel 272: for (my $ind=0;$ind<$table_range[$j];$ind++) {
1.128 sakharuk 273: $result.='p{3 mm}p{'.$celllength.' mm}';
1.89 albertel 274: }
275: $result.='}';
1.90 albertel 276: for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
1.128 sakharuk 277: $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_values[$ind].'} ';
1.89 albertel 278: if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
279: }
280: $cou += $table_range[$j];
281: $j++;
282: $result.='\\\\\end{tabular}\vskip 0 mm ';
283: }
284: $result.='\end{enumerate}';
285: } else {
286: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
1.93 albertel 287: my $repetition = &Apache::response::repetition();
1.89 albertel 288: $result.='\begin{enumerate}';
289: for (my $i=0;$i<$repetition;$i++) {
1.93 albertel 290: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
1.89 albertel 291: }
1.90 albertel 292: $increment=$repetition;
1.89 albertel 293: $result.= '\end{enumerate}';
294: }
295: }
296: }
297: } elsif ($target eq 'edit') {
298: $result.='</td></tr>'.&Apache::edit::end_table;
299: } elsif ($target eq 'answer' || $target eq 'analyze') {
1.58 sakharuk 300:
1.89 albertel 301: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
302: if ($target eq 'analyze') {
303: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.125 albertel 304: $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
1.117 albertel 305: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
306: push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
1.89 albertel 307: }
1.125 albertel 308: if (scalar(@$tagstack)) {
1.140 albertel 309: &Apache::response::setup_params($tag,$safeeval);
1.125 albertel 310: }
1.89 albertel 311: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.58 sakharuk 312: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
313: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
1.89 albertel 314: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
315:
316: if ($target eq 'answer') {
1.125 albertel 317: $result.=&Apache::response::answer_header($tag);
1.89 albertel 318: }
319: for(my $i=0;$i<=$#answers;$i++) {
320: my $ans=$answers[$i];
1.90 albertel 321: my $fmt=$formats[0];
322: if (@formats && $#formats) {$fmt=$formats[$i];}
1.89 albertel 323: my ($high,$low);
324: if ($Apache::inputtags::params{'tol'}) {
325: ($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
326: }
327: my ($sighigh,$siglow);
328: if ($Apache::inputtags::params{'sig'}) {
329: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
330: }
1.125 albertel 331: if ($fmt && $tag eq 'numericalresponse') {
1.112 albertel 332: $fmt=~s/e/E/g;
1.89 albertel 333: $ans = sprintf('%.'.$fmt,$ans);
334: if ($high) {
1.90 albertel 335: $high=sprintf('%.'.$fmt,$high);
336: $low =sprintf('%.'.$fmt,$low);
1.62 sakharuk 337: }
1.60 sakharuk 338: }
1.89 albertel 339: if ($target eq 'answer') {
1.125 albertel 340: if ($high && $tag eq 'numericalresponse') { $ans.=' ['.$low.','.$high.']'; }
1.145 albertel 341: if (defined($sighigh) && $tag eq 'numericalresponse') {
1.101 albertel 342: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.106 sakharuk 343: $ans.= " Sig $siglow - $sighigh";
1.98 sakharuk 344: } else {
1.101 albertel 345: $ans.= " Sig <i>$siglow - $sighigh</i>";
1.98 sakharuk 346: }
347: }
1.125 albertel 348: $result.=&Apache::response::answer_part($tag,$ans);
1.89 albertel 349: } elsif ($target eq 'analyze') {
350: push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
351: if ($high) {
352: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
353: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
1.58 sakharuk 354: }
1.79 sakharuk 355: }
1.36 albertel 356: }
1.102 albertel 357: if (defined($unit) and ($unit ne '') and
1.125 albertel 358: $tag eq 'numericalresponse') {
1.89 albertel 359: if ($target eq 'answer') {
1.101 albertel 360: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.125 albertel 361: $result.=&Apache::response::answer_part($tag,
1.106 sakharuk 362: " Unit: $unit ");
1.98 sakharuk 363: } else {
1.125 albertel 364: $result.=&Apache::response::answer_part($tag,
1.98 sakharuk 365: "Unit: <b>$unit</b>");
366: }
1.89 albertel 367: } elsif ($target eq 'analyze') {
1.103 albertel 368: push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
1.89 albertel 369: }
370: }
1.125 albertel 371: if ($tag eq 'formularesponse' && $target eq 'answer') {
1.89 albertel 372: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
1.125 albertel 373: $result.=&Apache::response::answer_part($tag,$samples);
1.89 albertel 374: }
375: if ($target eq 'answer') {
1.125 albertel 376: $result.=&Apache::response::answer_footer($tag);
1.89 albertel 377: }
1.69 sakharuk 378: }
1.121 sakharuk 379: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.90 albertel 380: $target eq 'tex' || $target eq 'analyze') {
381: &Apache::lonxml::increment_counter($increment);
382: }
1.89 albertel 383: &Apache::response::end_response;
384: return $result;
1.90 albertel 385: }
386:
387: sub get_table_sizes {
1.128 sakharuk 388: my ($number_of_bubbles,$rbubble_values)=@_;
389: my $scale=2; #mm for one digit
390: my $cell_width=0;
391: foreach my $member (@$rbubble_values) {
392: my $cell_width_real=0;
1.138 sakharuk 393: if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$\\times\s*10\^{(\+|-)?(\d+)}\$/) {
394: $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale;
395: } elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
1.128 sakharuk 396: $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
1.138 sakharuk 397: } elsif ($member=~/(\d*)\.(\d*)/) {
1.132 sakharuk 398: $cell_width_real=(length($1)+length($2)+3)*$scale;
1.128 sakharuk 399: } else {
1.138 sakharuk 400: $cell_width_real=(length($member)+1)*$scale*0.9;
1.128 sakharuk 401: }
402: if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
1.90 albertel 403: }
1.132 sakharuk 404: $cell_width+=8;
1.130 sakharuk 405: my $textwidth;
406: if ($ENV{'form.textwidth'} ne '') {
1.132 sakharuk 407: $ENV{'form.textwidth'}=~/(\d*)\.?(\d*)/;
408: $textwidth=$1.'.'.$2;
1.130 sakharuk 409: } else {
1.132 sakharuk 410: $ENV{'textwidth'}=~/(\d+)\.?(\d*)/;
411: $textwidth=$1.'.'.$2;
1.130 sakharuk 412: }
1.128 sakharuk 413: my $bubbles_per_line=int($textwidth/$cell_width);
1.135 sakharuk 414: if (($bubbles_per_line > $number_of_bubbles/2) && ($number_of_bubbles % 2==0)) {$bubbles_per_line=$number_of_bubbles/2;}
1.128 sakharuk 415: my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
1.90 albertel 416: my @table_range = ();
1.128 sakharuk 417: for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
418: if ($number_of_bubbles % $bubbles_per_line) {
1.90 albertel 419: $number_of_tables++;
1.128 sakharuk 420: push @table_range,($number_of_bubbles % $bubbles_per_line);
1.90 albertel 421: }
1.128 sakharuk 422: $cell_width-=8;
1.136 sakharuk 423: $cell_width=$cell_width*3/4;
1.128 sakharuk 424: return ($cell_width,$number_of_tables,@table_range);
1.90 albertel 425: }
426:
427: sub format_number {
428: my ($number,$format,$target)=@_;
429: my $ans;
430: if ($format ne '') {
1.109 albertel 431: $format=~s/e/E/g;
1.90 albertel 432: $ans = sprintf('%.'.$format,$number);
433: } else {
434: my $format = '';
435: #What is the number? (integer,decimal,floating point)
436: if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
1.113 sakharuk 437: $format = '3e';
1.90 albertel 438: } elsif ($number=~/^(\d*)\.(\d*)$/) {
439: $format = '4f';
440: } elsif ($number=~/^(\d*)$/) {
441: $format = 'd';
442: }
443: $ans = sprintf('%.'.$format,$number);
444: }
445: if ($target eq 'tex') {
446: if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
447: my $number = $1;
448: my $power = $2;
449: $power=~s/^\+//;
1.124 sakharuk 450: $power=~s/^(-?)0+(\d+)/$1$2/;
451: $ans=$number.'$\times 10^{'.$power.'}$'; #'stupidemacs
1.90 albertel 452: }
453: }
454: return $ans;
455: }
456:
457: sub make_numerical_bubbles {
1.117 albertel 458: my ($number_of_bubbles,$target,$answer,$format,$incorrect) =@_;
1.91 albertel 459: my @bubble_values = ();
1.119 albertel 460: &Apache::lonxml::debug("answer is $answer incorrect is $incorrect");
461: my @oldseed=&Math::Random::random_get_seed();
1.117 albertel 462: if (defined($incorrect) && ref($incorrect)) {
463: &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1 gt $number_of_bubbles));
1.126 albertel 464: if (defined($$incorrect[0]) &&
465: scalar(@$incorrect)+1 >= $number_of_bubbles) {
1.117 albertel 466: &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1).":$number_of_bubbles");
467: &Apache::response::setrandomnumber();
468: my @rand_inc=&Math::Random::random_permutation(@$incorrect);
469: @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
470: @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
471: &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": whih are :".join(':',@bubble_values));
1.119 albertel 472: &Math::Random::random_set_seed(@oldseed);
1.134 albertel 473: if (defined($format) && $format ne '') {
1.137 albertel 474: foreach my $value (@bubble_values) {
475: $value=&format_number($value,$format,$target);
1.134 albertel 476: }
477: }
1.117 albertel 478: return @bubble_values;
479: }
1.127 albertel 480: if (defined($$incorrect[0]) &&
481: scalar(@$incorrect)+1 < $number_of_bubbles) {
482: &Apache::lonxml::warning("Not enough incorrect answers were specified in the incorrect array, ignoring the specified incorrect answers and instead generating them.");
483: }
1.117 albertel 484: }
1.90 albertel 485: my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
1.126 albertel 486: my @powers = (1..$number_of_bubbles);
1.90 albertel 487: &Apache::response::setrandomnumber();
488: my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
489: my $power = $powers[$ind];
490: $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
491: my $factor = $factors[$ind];
492: for ($ind=0;$ind<$number_of_bubbles;$ind++) {
1.91 albertel 493: $bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
494: $bubble_values[$ind] = &format_number($bubble_values[$ind],
495: $format,$target);
496:
1.90 albertel 497: }
1.119 albertel 498: &Math::Random::random_set_seed(@oldseed);
1.91 albertel 499: return @bubble_values;
1.51 albertel 500: }
501:
502: sub get_tolrange {
1.89 albertel 503: my ($ans,$tol)=@_;
504: my ($high,$low);
505: if ($tol =~ /%$/) {
506: chop($tol);
507: my $change=$ans*($tol/100.0);
508: $high=$ans+$change;
509: $low=$ans-$change;
510: } else {
511: $high=$ans+$tol;
512: $low=$ans-$tol;
513: }
514: return ($high,$low);
1.52 albertel 515: }
516:
517: sub get_sigrange {
1.89 albertel 518: my ($sig)=@_;
519: &Apache::lonxml::debug("Got a sig of :$sig:");
1.147 ! albertel 520: my $courseid=$ENV{'request.course.id'};
! 521: if (lc($ENV{"course.$courseid.disablesigfigs"}) eq 'yes') {
! 522: return (15,0);
! 523: }
1.89 albertel 524: my $sig_lbound;
525: my $sig_ubound;
526: if ($sig eq '') {
527: $sig_lbound = 0; #SIG_LB_DEFAULT
528: $sig_ubound =15; #SIG_UB_DEFAULT
529: } else {
530: ($sig_lbound,$sig_ubound) = split(/,/,$sig);
1.145 albertel 531: if (!defined($sig_lbound)) {
1.89 albertel 532: $sig_lbound = 0; #SIG_LB_DEFAULT
533: $sig_ubound =15; #SIG_UB_DEFAULT
534: }
1.145 albertel 535: if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
1.133 albertel 536: }
537: if (($sig_ubound<$sig_lbound) ||
538: ($sig_lbound > 15) ||
539: ($sig =~/(\+|-)/ ) ) {
540: my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
541: if ($ENV{'request.state'} eq 'construct') {
542: $errormsg.=
543: &Apache::loncommon::help_open_topic('Significant_Figures');
544: }
545: &Apache::lonxml::error($errormsg);
1.52 albertel 546: }
1.89 albertel 547: return ($sig_ubound,$sig_lbound);
1.34 albertel 548: }
549:
550: sub start_stringresponse {
1.89 albertel 551: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
552: my $result;
1.122 albertel 553: my $id = &Apache::response::start_response($parstack,$safeeval);
1.89 albertel 554: if ($target eq 'meta') {
1.104 bowersj2 555: &Apache::response::start_response($parstack,$safeeval);
1.89 albertel 556: $result=&Apache::response::meta_package_write('stringresponse');
1.104 bowersj2 557: &Apache::response::end_response();
1.122 albertel 558: } elsif ($target eq 'edit') {
559: $result.=&Apache::edit::tag_start($target,$token);
560: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
561: $result.=&Apache::edit::select_arg('Type:','type',
562: [['cs','Case Sensitive'],['ci','Case Insensitive'],
563: ['mc','Case Insensitive, Any Order'],
564: ['re','Regular Expression']],$token);
565: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
566: } elsif ($target eq 'modified') {
1.146 albertel 567: my $constructtag;
568: $constructtag=&Apache::edit::get_new_args($token,$parstack,
569: $safeeval,'answer',
570: 'type','answerdisplay');
571: if ($constructtag) {
572: $result = &Apache::edit::rebuild_tag($token);
573: $result.=&Apache::edit::handle_insert();
574: }
575: } elsif ($target eq 'web') {
576: if ( &Apache::response::show_answer() ) {
577: $Apache::inputtags::answertxt{$id}=
578: &Apache::lonxml::get_param('answer',$parstack,$safeeval);
579: }
1.122 albertel 580: } elsif ($target eq 'answer' || $target eq 'grade') {
581: &Apache::response::reset_params();
1.89 albertel 582: }
583: return $result;
1.34 albertel 584: }
585:
586: sub end_stringresponse {
1.122 albertel 587: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
588: my $increment=1;
589: my $result = '';
590: my $part=$Apache::inputtags::part;
591: my $id=$Apache::inputtags::response[-1];
592: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
593: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
1.129 www 594: my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
1.122 albertel 595: &Apache::lonxml::debug("current $answer ".$token->[2]);
596: if (!$Apache::lonxml::default_homework_loaded) {
597: &Apache::lonxml::default_homework_load($safeeval);
598: }
599: if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
1.140 albertel 600: &Apache::response::setup_params('stringresponse',$safeeval);
1.122 albertel 601: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
602: if ($Apache::lonhomework::type eq 'exam' ||
603: $ENV{'form.submitted'} eq 'scantron') {
604: $increment=&Apache::response::scored_response($part,$id);
605: } else {
606: my $response = &Apache::response::getresponse();
607: if ( $response =~ /[^\s]/) {
608: my %previous = &Apache::response::check_for_previous($response,
609: $part,$id);
610: &Apache::lonxml::debug("submitted a $response<br>\n");
611: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
612: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
613: $response;
1.142 albertel 614: my ($ad,$msg);
1.122 albertel 615: if ($type eq 're' ) {
616: # if the RE wasn't in a var it likely got munged,
617: # thus grab it from the var directly
618: # my $testans=$token->[2]->{'answer'};
619: # if ($testans !~ m/^\s*\$/) {
620: # $answer=$token->[2]->{'answer'};
621: # }
1.143 albertel 622: ${$safeeval->varglob('LONCAPA::response')}=$response;
623: $result = &Apache::run::run('return $LONCAPA::response=~m'.$answer,$safeeval);
1.122 albertel 624: &Apache::lonxml::debug("current $response");
625: &Apache::lonxml::debug("current $answer");
626: $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
627: } else {
1.143 albertel 628: my $args_ref=
629: \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
630:
631: $$args_ref{'response'}=$response;
1.122 albertel 632: &Apache::lonxml::debug("current $response");
1.143 albertel 633: $$args_ref{'type'}=
634: &Apache::lonxml::get_param('type',$parstack,$safeeval);
1.122 albertel 635: foreach my $key (keys(%Apache::inputtags::params)) {
1.143 albertel 636: $$args_ref{$key}=$Apache::inputtags::params{$key};
1.122 albertel 637: }
638: &Apache::lonxml::debug('answer is'.join(':',$answer));
1.143 albertel 639: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=($answer);
640: ($result, my @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
1.142 albertel 641: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
642: my ($awards)=split(/:/,$result);
643: my (@awards) = split(/,/,$awards);
644: ($ad,$msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
1.122 albertel 645: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
646: }
647: &Apache::response::handle_previous(\%previous,$ad);
648: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
1.142 albertel 649: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
1.122 albertel 650: }
651: }
652: } elsif ($target eq 'web' || $target eq 'tex') {
653: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
654: my $status = $Apache::inputtags::status['-1'];
655: if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
656: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
657: $increment = &Apache::response::repetition();
658: $result.='\begin{enumerate}';
659: for (my $i=0;$i<$increment;$i++) {
660: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
661: '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
662: }
663: $result.= '\end{enumerate}';
664: }
665: } elsif ($target eq 'answer' || $target eq 'analyze') {
666: if ($target eq 'analyze') {
667: push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
1.125 albertel 668: $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
1.122 albertel 669: }
1.140 albertel 670: &Apache::response::setup_params('stringresponse',$safeeval);
1.122 albertel 671: if ($target eq 'answer') {
1.125 albertel 672: $result.=&Apache::response::answer_header('stringresponse');
1.122 albertel 673: }
674: # foreach my $ans (@answers) {
675: if ($target eq 'answer') {
1.125 albertel 676: $result.=&Apache::response::answer_part('stringresponse',$answer);
1.122 albertel 677: } elsif ($target eq 'analyze') {
678: push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
679: $answer);
680: }
681: # }
682: my $string='Case Insensitive';
683: if ($type eq 'mc') {
684: $string='Multiple Choice';
685: } elsif ($type eq 'cs') {
686: $string='Case Sensitive';
687: } elsif ($type eq 'ci') {
688: $string='Case Insensitive';
689: } elsif ($type eq 're') {
690: $string='Regular Expression';
691: }
692: if ($target eq 'answer') {
693: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.125 albertel 694: $result.=&Apache::response::answer_part('stringresponse',
1.122 albertel 695: "$string");
696: } else {
1.125 albertel 697: $result.=&Apache::response::answer_part('stringresponse',
1.122 albertel 698: "<b>$string</b>");
699: }
700: } elsif ($target eq 'analyze') {
701: push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
702: $type);
703: }
704: if ($target eq 'answer') {
1.125 albertel 705: $result.=&Apache::response::answer_footer('stringresponse');
1.122 albertel 706: }
707: } elsif ($target eq 'edit') {
708: $result.='</td></tr>'.&Apache::edit::end_table;
709: }
710: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
711: $target eq 'tex' || $target eq 'analyze') {
712: &Apache::lonxml::increment_counter($increment);
713: }
714: &Apache::response::end_response;
715: return $result;
1.44 albertel 716: }
717:
718: sub start_formularesponse {
1.89 albertel 719: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
720: my $result;
721: if ($target eq 'meta') {
1.104 bowersj2 722: &Apache::response::start_response($parstack,$safeeval);
1.89 albertel 723: $result=&Apache::response::meta_package_write('formularesponse');
1.104 bowersj2 724: &Apache::response::end_response();
1.89 albertel 725: } else {
726: $result.=&start_numericalresponse(@_);
727: }
728: return $result;
1.44 albertel 729: }
730:
731: sub end_formularesponse {
1.89 albertel 732: return end_numericalresponse(@_);
1.3 albertel 733: }
734:
1.1 albertel 735: 1;
1.3 albertel 736: __END__
1.89 albertel 737:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>