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