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