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