1: # The LearningOnline Network with CAPA
2: # caparesponse definition
3: #
4: # $Id: caparesponse.pm,v 1.186 2005/12/28 21:26:21 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::caparesponse;
30: use strict;
31: use capa;
32: use Apache::lonlocal;
33: use Apache::lonnet;
34:
35: BEGIN {
36: &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse','formularesponse'));
37: }
38:
39: my %answer;
40: my $cur_name;
41: sub start_answer {
42: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
43: my $result;
44: $cur_name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
45: if ($cur_name =~ /^\s*$/) { $cur_name = $Apache::lonxml::curdepth; }
46: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
47: if (!defined($type) && $tagstack->[-2] eq 'answergroup') {
48: $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
49: }
50: if (!defined($type)) { $type = 'ordered' };
51: $answer{$cur_name}= { 'type' => $type,
52: 'answers' => [] };
53: return $result;
54: }
55:
56: sub end_answer {
57: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
58: my $result;
59: undef($cur_name);
60: return $result;
61: }
62:
63: sub start_answergroup {
64: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
65: my $result;
66: my $id = $Apache::inputtags::response[-1];
67: my $dis = &Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
68: if (defined($dis)) { $Apache::inputtags::answertxt{$id}=$dis; }
69: return $result;
70: }
71:
72: sub end_answergroup {
73: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
74: my $result;
75: return $result;
76: }
77:
78: sub start_value {
79: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
80: my $result;
81: if ( $target eq 'web' || $target eq 'tex' ||
82: $target eq 'grade' || $target eq 'webgrade' ||
83: $target eq 'answer' || $target eq 'analyze' ) {
84: my $bodytext = &Apache::lonxml::get_all_text("/value",$parser,$style);
85: $bodytext = &Apache::run::evaluate($bodytext,$safeeval,
86: $$parstack[-1]);
87: push(@{ $answer{$cur_name}{'answers'} },$bodytext);
88: }
89: return $result;
90: }
91:
92: sub end_value {
93: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
94: my $result;
95: return $result;
96: }
97:
98: sub start_array {
99: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
100: my $result;
101: if ( $target eq 'web' || $target eq 'tex' ||
102: $target eq 'grade' || $target eq 'webgrade' ||
103: $target eq 'answer' || $target eq 'analyze' ) {
104: my $bodytext = &Apache::lonxml::get_all_text("/array",$parser,$style);
105: my @values = &Apache::run::evaluate($bodytext,$safeeval,
106: $$parstack[-1]);
107: push(@{ $answer{$cur_name}{'answers'} },@values);
108: }
109: return $result;
110: }
111:
112: sub end_array {
113: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
114: my $result;
115: return $result;
116: }
117:
118: sub start_unit {
119: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
120: my $result;
121: return $result;
122: }
123:
124: sub end_unit {
125: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
126: my $result;
127: return $result;
128: }
129:
130: sub start_numericalresponse {
131: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
132: &Apache::lonxml::register('Apache::caparesponse',('answer','answergroup','value','array','unit'));
133: my $id = &Apache::response::start_response($parstack,$safeeval);
134: my $result;
135: undef(%answer);
136: undef(%{$safeeval->varglob('LONCAPA::CAPAresponse_args')});
137: if ($target eq 'edit') {
138: $result.=&Apache::edit::tag_start($target,$token);
139: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
140: if ($token->[1] eq 'numericalresponse') {
141: $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
142: $token).
143: &Apache::loncommon::help_open_topic('numerical_wrong_answers');
144: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
145: &Apache::loncommon::help_open_topic('Physical_Units');
146: $result.=&Apache::edit::text_arg('Format:','format',$token,4).
147: &Apache::loncommon::help_open_topic('Numerical_Response_Format');
148: } elsif ($token->[1] eq 'formularesponse') {
149: $result.=&Apache::edit::text_arg('Sample Points:','samples',
150: $token,40).
151: &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
152: }
153: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
154: } elsif ($target eq 'modified') {
155: my $constructtag;
156: if ($token->[1] eq 'numericalresponse') {
157: $constructtag=&Apache::edit::get_new_args($token,$parstack,
158: $safeeval,'answer',
159: 'incorrect','unit',
160: 'format');
161: } elsif ($token->[1] eq 'formularesponse') {
162: $constructtag=&Apache::edit::get_new_args($token,$parstack,
163: $safeeval,'answer',
164: 'samples');
165: }
166: if ($constructtag) {
167: $result = &Apache::edit::rebuild_tag($token);
168: $result.=&Apache::edit::handle_insert();
169: }
170: } elsif ($target eq 'meta') {
171: $result=&Apache::response::meta_package_write('numericalresponse');
172: } elsif ($target eq 'answer' || $target eq 'grade') {
173: &Apache::response::reset_params();
174: } elsif ($target eq 'web') {
175: my $partid = $Apache::inputtags::part;
176: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
177: &Apache::lonxml::debug("Got unit $hideunit for $partid $id");
178: #no way to enter units, with radio buttons
179: if (lc($hideunit) eq "yes") {
180: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
181: $safeeval);
182: if ($unit =~ /\S/) { $result.=" (in $unit) "; }
183: }
184: if ( &Apache::response::show_answer() ) {
185: my $answertxt;
186: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
187: $safeeval);
188: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
189: $safeeval);
190: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
191: $safeeval);
192: for (my $i=0; $i <= $#answers; $i++) {
193: my $answer=$answers[$i];
194: if ( scalar(@$tagstack)
195: && $tagstack->[-1] ne 'numericalresponse') {
196: $answertxt.=$answer.',';
197: } else {
198: my $format;
199: if ($#formats > 0) {
200: $format=$formats[$i];
201: } else {
202: $format=$formats[0];
203: }
204: if ($unit=~/\$/) { $format="\$".$format; $unit=~s/\$//g; }
205: if ($unit=~/\,/) { $format="\,".$format; $unit=~s/\,//g; }
206: my $formatted=&format_number($answer,$format,$target,
207: $safeeval);
208: $answertxt.=$formatted.',';
209: }
210: }
211: chop $answertxt;
212: if ($target eq 'web') {
213: $answertxt.=" $unit ";
214: }
215: $Apache::inputtags::answertxt{$id}=$answertxt;
216: }
217: }
218: return $result;
219: }
220:
221: sub check_submission {
222: my ($response,$partid,$id,$tag,$parstack,$safeeval,$ignore_sig)=@_;
223: my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
224: $$args_ref{'response'}=$response;
225: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
226:
227: foreach my $arg ('type','tol','sig','format','unit','calc',
228: 'samples') {
229: $$args_ref{$arg}=
230: &Apache::lonxml::get_param($arg,$parstack,$safeeval);
231: }
232: foreach my $key (keys(%Apache::inputtags::params)) {
233: $$args_ref{$key}=$Apache::inputtags::params{$key};
234: }
235:
236: #no way to enter units, with radio buttons
237: if ($Apache::lonhomework::type eq 'exam' ||
238: lc($hideunit) eq "yes") {
239: delete($$args_ref{'unit'});
240: }
241: #sig fig don't make much sense either
242: if (($Apache::lonhomework::type eq 'exam' ||
243: &Apache::response::submitted('scantron') ||
244: $ignore_sig) &&
245: $tag eq 'numericalresponse') {
246: delete($$args_ref{'sig'});
247: }
248:
249: if ($tag eq 'formularesponse') {
250: $$args_ref{'type'}='fml';
251: } elsif ($tag eq 'numericalresponse') {
252: $$args_ref{'type'}='float';
253: }
254: my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
255: &Apache::lonxml::debug('answer is'.join(':',@answer));
256: if (@answer && defined($answer[0])) {
257: $answer{'INTERNAL'}= {'type' => 'ordered',
258: 'answers' => \@answer };
259: }
260: #FIXME would be nice if we could save name so we know who graded him
261: #correct
262: my (%results,@final_awards,@final_msgs,@names);
263: foreach my $name (keys(%answer)) {
264: &Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
265: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=@{ $answer{$name}{'answers'} };
266: my ($result,@msgs) =
267: &Apache::run::run("&caparesponse_check_list()",$safeeval);
268: &Apache::lonxml::debug('msgs are '.join(':',@msgs));
269: my ($awards)=split(/:/,$result);
270: my @awards= split(/,/,$awards);
271: my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
272: $results{$name}= [$ad,$msg];
273: push(@final_awards,$ad);
274: push(@final_msgs,$msg);
275: push(@names,$name);
276: }
277: my ($ad, $msg, $name) = &Apache::inputtags::finalizeawards(\@final_awards,
278: \@final_msgs,
279: \@names,1);
280: return($ad,$msg);
281: }
282:
283: sub end_numericalresponse {
284: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
285: my $increment=1;
286: my $result = '';
287: if (!$Apache::lonxml::default_homework_loaded) {
288: &Apache::lonxml::default_homework_load($safeeval);
289: }
290: my $partid = $Apache::inputtags::part;
291: my $id = $Apache::inputtags::response[-1];
292: my $tag;
293: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
294: if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
295: if ( $target eq 'grade' && &Apache::response::submitted() ) {
296: &Apache::response::setup_params($tag,$safeeval);
297: if ($Apache::lonhomework::type eq 'exam' &&
298: $tag eq 'formularesponse') {
299: $increment=&Apache::response::scored_response($partid,$id);
300: } else {
301: my $response = &Apache::response::getresponse();
302: if ( $response =~ /[^\s]/) {
303: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
304: &Apache::lonxml::debug("submitted a $response<br>\n");
305: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
306:
307: if ( &Apache::response::submitted('scantron')) {
308: my ($values,$display)=&make_numerical_bubbles($partid,$id,
309: $target,$parstack,$safeeval);
310: $response=$values->[$response];
311: }
312: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
313: my ($ad,$msg)=&check_submission($response,$partid,$id,
314: $tag,$parstack,$safeeval);
315:
316: &Apache::lonxml::debug('ad is'.$ad);
317: if ($ad eq 'SIG_FAIL') {
318: my ($sig_u,$sig_l)=
319: &get_sigrange($Apache::inputtags::params{'sig'});
320: $msg=join(':',$msg,$sig_l,$sig_u);
321: &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
322: $Apache::inputtags::params{'sig'});
323: }
324: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
325: if ($Apache::lonhomework::type eq 'survey' &&
326: ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
327: $ad eq 'EXACT_ANS')) {
328: $ad='SUBMITTED';
329: }
330: &Apache::response::handle_previous(\%previous,$ad);
331: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
332: $Apache::lonhomework::results{"resource.$partid.$id.awardmsg"}=$msg;
333: $result='';
334: }
335: }
336: } elsif ($target eq 'web' || $target eq 'tex') {
337: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
338: $safeeval);
339: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
340: my $status = $Apache::inputtags::status['-1'];
341: if ($Apache::lonhomework::type eq 'exam') {
342: my ($bubble_values,$bubble_display)=
343: &make_numerical_bubbles($partid,$id,$target,$parstack,
344: $safeeval);
345: my $number_of_bubbles = scalar(@{ $bubble_values });
346: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
347: $safeeval);
348: my @alphabet=('A'..'Z');
349: if ($target eq 'web') {
350: if ($tag eq 'numericalresponse') {
351: if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
352: $result.= '<table border="1"><tr>';
353: my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
354: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
355: my $checked='';
356: if ($previous eq $bubble_values->[$ind]) {
357: $checked=" checked='on' ";
358: }
359: $result.='<td><input type="radio" name="HWVAL_'.$id.
360: '" value="'.$bubble_values->[$ind].'" '.$checked
361: .' /><b>'.$alphabet[$ind].'</b>: '.
362: $bubble_display->[$ind].'</td>';
363: }
364: $result.='</tr></table>';
365: } elsif ($tag eq 'formularesponse') {
366: $result.= '<br /><br /><font color="red">
367: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
368: </textarea></font> <br /><br />';
369: }
370: } elsif ($target eq 'tex') {
371: if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
372: $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
373: }
374: if ($tag eq 'numericalresponse') {
375: my ($celllength,$number_of_tables,@table_range)=
376: &get_table_sizes($number_of_bubbles,$bubble_display);
377: my $j=0;
378: my $cou=0;
379: $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
380: for (my $i=0;$i<$number_of_tables;$i++) {
381: $result.='\vskip -1 mm \noindent \setlength{\tabcolsep}{2 mm}\begin{tabular}{';
382: for (my $ind=0;$ind<$table_range[$j];$ind++) {
383: $result.='p{3 mm}p{'.$celllength.' mm}';
384: }
385: $result.='}';
386: for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
387: $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_display->[$ind].'} ';
388: if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
389: }
390: $cou += $table_range[$j];
391: $j++;
392: $result.='\\\\\end{tabular}\vskip 0 mm ';
393: }
394: $result.='\end{enumerate}';
395: } else {
396: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
397: my $repetition = &Apache::response::repetition();
398: $result.='\begin{enumerate}';
399: for (my $i=0;$i<$repetition;$i++) {
400: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
401: }
402: $increment=$repetition;
403: $result.= '\end{enumerate}';
404: }
405: }
406: }
407: } elsif ($target eq 'edit') {
408: $result.='</td></tr>'.&Apache::edit::end_table;
409: } elsif ($target eq 'answer' || $target eq 'analyze') {
410: my $part_id="$partid.$id";
411: if ($target eq 'analyze') {
412: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
413: $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
414: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
415: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
416: push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
417: &Apache::response::check_if_computed($token,$parstack,
418: $safeeval,'answer');
419: }
420: if (scalar(@$tagstack)) {
421: &Apache::response::setup_params($tag,$safeeval);
422: }
423: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
424: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
425: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
426: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
427:
428: if ($target eq 'answer') {
429: $result.=&Apache::response::answer_header($tag);
430: if ($tag eq 'numericalresponse'
431: && $Apache::lonhomework::type eq 'exam') {
432: my ($bubble_values,undef,$correct) = &make_numerical_bubbles($partid,
433: $id,$target,$parstack,$safeeval);
434: $result.=&Apache::response::answer_part($tag,$correct);
435: }
436: }
437: my ($sigline,$tolline);
438: for(my $i=0;$i<=$#answers;$i++) {
439: my $ans=$answers[$i];
440: my $fmt=$formats[0];
441: if (@formats && $#formats) {$fmt=$formats[$i];}
442: my ($high,$low);
443: if ($Apache::inputtags::params{'tol'}) {
444: ($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
445: }
446: my ($sighigh,$siglow);
447: if ($Apache::inputtags::params{'sig'}) {
448: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
449: }
450: if ($fmt && $tag eq 'numericalresponse') {
451: $fmt=~s/e/E/g;
452: if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
453: if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
454: $ans = &format_number($ans,$fmt,$target,$safeeval);
455: #if ($high) {
456: # $high=&format_number($high,$fmt,$target,$safeeval);
457: # $low =&format_number($low,$fmt,$target,$safeeval);
458: #}
459: }
460: if ($target eq 'answer') {
461: if ($high && $tag eq 'numericalresponse') {
462: $ans.=' ['.$low.','.$high.']';
463: $tolline .= "[$low, $high]";
464: }
465: if (defined($sighigh) && $tag eq 'numericalresponse') {
466: if ($env{'form.answer_output_mode'} eq 'tex') {
467: $ans.= " Sig $siglow - $sighigh";
468: } else {
469: $ans.= " Sig <i>$siglow - $sighigh</i>";
470: $sigline .= "[$siglow, $sighigh]";
471: }
472: }
473: $result.=&Apache::response::answer_part($tag,$ans);
474: } elsif ($target eq 'analyze') {
475: push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
476: if ($high) {
477: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
478: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
479: }
480: if ($fmt) {
481: push (@{ $Apache::lonhomework::analyze{"$part_id.format"} }, $fmt);
482: }
483: }
484: }
485:
486: my @fmt_ans;
487: for(my $i=0;$i<=$#answers;$i++) {
488: my $ans=$answers[$i];
489: my $fmt=$formats[0];
490: if (@formats && $#formats) {$fmt=$formats[$i];}
491: if ($fmt && $tag eq 'numericalresponse') {
492: $fmt=~s/e/E/g;
493: if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
494: if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
495: $ans = &format_number($ans,$fmt,$target,$safeeval);
496: if ($fmt=~/\$/ && $unit!~/\$/) { $ans=~s/\$//; }
497: }
498: push(@fmt_ans,$ans);
499: }
500: my $response=join(', ',@fmt_ans);
501: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.
502: $id.'.turnoffunit');
503: if ($unit ne '' &&
504: ! ($Apache::lonhomework::type eq 'exam' ||
505: lc($hideunit) eq "yes") ) {
506: my $cleanunit=$unit;
507: $cleanunit=~s/\$\,//g;
508: $response.=" $cleanunit";
509: }
510:
511: my ($ad,$msg)=&check_submission($response,$partid,$id,$tag,
512: $parstack,$safeeval);
513: if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
514: my $error;
515: if ($tag eq 'formularesponse') {
516: $error=&mt('Computer\'s answer is incorrect ("[_1]").',$response);
517: } else {
518: # answer failed check if it is sig figs that is failing
519: my ($ad,$msg)=&check_submission($response,$partid,$id,
520: $tag,$parstack,
521: $safeeval,1);
522: if ($sigline ne '') {
523: $error=&mt('Computer\'s answer is incorrect ("[_1]"). It is likely that the tolerance range [_2] or significant figures [_3] need to be adjusted.',$response,$tolline,$sigline);
524: } else {
525: $error=&mt('Computer\'s answer is incorrect ("[_1]"). It is likely that the tolerance range [_2] needs to be adjusted.',$response,$tolline);
526: }
527: }
528: if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
529: &Apache::lonxml::error($error);
530: } else {
531: &Apache::lonxml::warning($error);
532: }
533: }
534:
535: if (defined($unit) and ($unit ne '') and
536: $tag eq 'numericalresponse') {
537: if ($target eq 'answer') {
538: if ($env{'form.answer_output_mode'} eq 'tex') {
539: $result.=&Apache::response::answer_part($tag,
540: " Unit: $unit ");
541: } else {
542: $result.=&Apache::response::answer_part($tag,
543: "Unit: <b>$unit</b>");
544: }
545: } elsif ($target eq 'analyze') {
546: push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
547: }
548: }
549: if ($tag eq 'formularesponse' && $target eq 'answer') {
550: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
551: $result.=&Apache::response::answer_part($tag,$samples);
552: }
553: if ($target eq 'answer') {
554: $result.=&Apache::response::answer_footer($tag);
555: }
556: }
557: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
558: $target eq 'tex' || $target eq 'analyze') {
559: &Apache::lonxml::increment_counter($increment);
560: }
561: &Apache::response::end_response;
562: return $result;
563: }
564:
565: sub get_table_sizes {
566: my ($number_of_bubbles,$rbubble_values)=@_;
567: my $scale=2; #mm for one digit
568: my $cell_width=0;
569: foreach my $member (@$rbubble_values) {
570: my $cell_width_real=0;
571: if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^{(\+|-)?(\d+)}\$?/) {
572: $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale;
573: } elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
574: $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
575: } elsif ($member=~/(\d*)\.(\d*)/) {
576: $cell_width_real=(length($1)+length($2)+3)*$scale;
577: } else {
578: $cell_width_real=(length($member)+1)*$scale*0.9;
579: }
580: if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
581: }
582: $cell_width+=8;
583: my $textwidth;
584: if ($env{'form.textwidth'} ne '') {
585: $env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
586: $textwidth=$1.'.'.$2;
587: } else {
588: $env{'form.textwidth'}=~/(\d+)\.?(\d*)/;
589: $textwidth=$1.'.'.$2;
590: }
591: my $bubbles_per_line=int($textwidth/$cell_width);
592: if ($bubbles_per_line > $number_of_bubbles) {
593: $bubbles_per_line=$number_of_bubbles;
594: }elsif (($bubbles_per_line > $number_of_bubbles/2) && ($number_of_bubbles % 2==0)) {$bubbles_per_line=$number_of_bubbles/2;}
595: my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
596: my @table_range = ();
597: for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
598: if ($number_of_bubbles % $bubbles_per_line) {
599: $number_of_tables++;
600: push @table_range,($number_of_bubbles % $bubbles_per_line);
601: }
602: $cell_width-=8;
603: $cell_width=$cell_width*3/4;
604: return ($cell_width,$number_of_tables,@table_range);
605: }
606:
607: sub format_number {
608: my ($number,$format,$target,$safeeval)=@_;
609: my $ans;
610: if ($format eq '') {
611: #What is the number? (integer,decimal,floating point)
612: if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
613: $format = '3e';
614: } elsif ($number=~/^(\d*)\.(\d*)$/) {
615: $format = '4f';
616: } elsif ($number=~/^(\d*)$/) {
617: $format = 'd';
618: }
619: }
620: if (!$Apache::lonxml::default_homework_loaded) {
621: &Apache::lonxml::default_homework_load($safeeval);
622: }
623: $ans=&Apache::run::run("&prettyprint(q\0$number\0,q\0$format\0,q\0$target\0)",$safeeval);
624: return $ans;
625: }
626:
627: sub make_numerical_bubbles {
628: my ($part,$id,$target,$parstack,$safeeval) =@_;
629:
630: my $number_of_bubbles =
631: &Apache::response::get_response_param($part.'_'.$id,'numbubbles',8);
632:
633: my ($format)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
634: my ($answer)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
635: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,
636: $safeeval);
637: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
638:
639: my @bubble_values=();
640: my @alphabet=('A'..'Z');
641:
642: &Apache::lonxml::debug("answer is $answer incorrect is @incorrect");
643: my @oldseed=&Math::Random::random_get_seed();
644: if (@incorrect) {
645: &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1 gt $number_of_bubbles));
646: if (defined($incorrect[0]) &&
647: scalar(@incorrect)+1 >= $number_of_bubbles) {
648: &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1).":$number_of_bubbles");
649: &Apache::response::setrandomnumber();
650: my @rand_inc=&Math::Random::random_permutation(@incorrect);
651: @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
652: @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
653: &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": which are :".join(':',@bubble_values));
654: &Math::Random::random_set_seed(@oldseed);
655:
656: my $correct;
657: for(my $i=0; $i<=$#bubble_values;$i++) {
658: if ($bubble_values[$i] eq $answer) {
659: $correct = $alphabet[$i];
660: last;
661: }
662: }
663:
664: if (defined($format) && $format ne '') {
665: my @bubble_display;
666: foreach my $value (@bubble_values) {
667: push(@bubble_display,
668: &format_number($value,$format,$target,$safeeval));
669: }
670: return (\@bubble_values,\@bubble_display,$correct);
671: } else {
672: return (\@bubble_values,\@bubble_values,$correct);
673: }
674: }
675: if (defined($incorrect[0]) &&
676: scalar(@incorrect)+1 < $number_of_bubbles) {
677: &Apache::lonxml::warning("Not enough incorrect answers were specified in the incorrect array, ignoring the specified incorrect answers and instead generating them (".join(',',@incorrect).").");
678: }
679: }
680: my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
681: my @powers = (1..$number_of_bubbles);
682: &Apache::response::setrandomnumber();
683: my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
684: my $power = $powers[$ind];
685: $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
686: my $factor = $factors[$ind];
687: my @bubble_display;
688: for ($ind=0;$ind<$number_of_bubbles;$ind++) {
689: $bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
690: $bubble_display[$ind] = &format_number($bubble_values[$ind],
691: $format,$target,$safeeval);
692:
693: }
694: my $correct = $alphabet[$number_of_bubbles-$power];
695: &Math::Random::random_set_seed(@oldseed);
696: return (\@bubble_values,\@bubble_display,$correct);
697: }
698:
699: sub get_tolrange {
700: my ($ans,$tol)=@_;
701: my ($high,$low);
702: if ($tol =~ /%$/) {
703: chop($tol);
704: my $change=$ans*($tol/100.0);
705: $high=$ans+$change;
706: $low=$ans-$change;
707: } else {
708: $high=$ans+$tol;
709: $low=$ans-$tol;
710: }
711: return ($high,$low);
712: }
713:
714: sub get_sigrange {
715: my ($sig)=@_;
716: &Apache::lonxml::debug("Got a sig of :$sig:");
717: my $courseid=$env{'request.course.id'};
718: if (lc($env{"course.$courseid.disablesigfigs"}) eq 'yes') {
719: return (15,0);
720: }
721: my $sig_lbound;
722: my $sig_ubound;
723: if ($sig eq '') {
724: $sig_lbound = 0; #SIG_LB_DEFAULT
725: $sig_ubound =15; #SIG_UB_DEFAULT
726: } else {
727: ($sig_lbound,$sig_ubound) = split(/,/,$sig);
728: if (!defined($sig_lbound)) {
729: $sig_lbound = 0; #SIG_LB_DEFAULT
730: $sig_ubound =15; #SIG_UB_DEFAULT
731: }
732: if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
733: }
734: if (($sig_ubound<$sig_lbound) ||
735: ($sig_lbound > 15) ||
736: ($sig =~/(\+|-)/ ) ) {
737: my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
738: if ($env{'request.state'} eq 'construct') {
739: $errormsg.=
740: &Apache::loncommon::help_open_topic('Significant_Figures');
741: }
742: &Apache::lonxml::error($errormsg);
743: }
744: return ($sig_ubound,$sig_lbound);
745: }
746:
747: sub start_stringresponse {
748: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
749: my $result;
750: my $id = &Apache::response::start_response($parstack,$safeeval);
751: if ($target eq 'meta') {
752: $result=&Apache::response::meta_package_write('stringresponse');
753: } elsif ($target eq 'edit') {
754: $result.=&Apache::edit::tag_start($target,$token);
755: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
756: $result.=&Apache::edit::select_arg('Type:','type',
757: [['cs','Case Sensitive'],['ci','Case Insensitive'],
758: ['mc','Case Insensitive, Any Order'],
759: ['re','Regular Expression']],$token);
760: $result.=&Apache::edit::text_arg('String to display for answer:',
761: 'answerdisplay',$token);
762: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
763: } elsif ($target eq 'modified') {
764: my $constructtag;
765: $constructtag=&Apache::edit::get_new_args($token,$parstack,
766: $safeeval,'answer',
767: 'type','answerdisplay');
768: if ($constructtag) {
769: $result = &Apache::edit::rebuild_tag($token);
770: $result.=&Apache::edit::handle_insert();
771: }
772: } elsif ($target eq 'web') {
773: if ( &Apache::response::show_answer() ) {
774: my $answer=
775: &Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
776: if (!defined $answer || $answer eq '') {
777: $answer=
778: &Apache::lonxml::get_param('answer',$parstack,$safeeval);
779: }
780: $Apache::inputtags::answertxt{$id}=$answer;
781: }
782: } elsif ($target eq 'answer' || $target eq 'grade') {
783: &Apache::response::reset_params();
784: }
785: return $result;
786: }
787:
788: sub end_stringresponse {
789: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
790: my $increment=1;
791: my $result = '';
792: my $part=$Apache::inputtags::part;
793: my $id=$Apache::inputtags::response[-1];
794: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
795: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
796: my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
797: &Apache::lonxml::debug("current $answer ".$token->[2]);
798: if (!$Apache::lonxml::default_homework_loaded) {
799: &Apache::lonxml::default_homework_load($safeeval);
800: }
801: if ( $target eq 'grade' && &Apache::response::submitted() ) {
802: &Apache::response::setup_params('stringresponse',$safeeval);
803: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
804: if ($Apache::lonhomework::type eq 'exam' ||
805: &Apache::response::submitted('scantron')) {
806: $increment=&Apache::response::scored_response($part,$id);
807: } else {
808: my $response = &Apache::response::getresponse();
809: if ( $response =~ /[^\s]/) {
810: my %previous = &Apache::response::check_for_previous($response,
811: $part,$id);
812: &Apache::lonxml::debug("submitted a $response<br>\n");
813: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
814: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
815: $response;
816: my ($ad,$msg);
817: if ($type eq 're' ) {
818: # if the RE wasn't in a var it likely got munged,
819: # thus grab it from the var directly
820: # my $testans=$token->[2]->{'answer'};
821: # if ($testans !~ m/^\s*\$/) {
822: # $answer=$token->[2]->{'answer'};
823: # }
824: ${$safeeval->varglob('LONCAPA::response')}=$response;
825: $result = &Apache::run::run('if ($LONCAPA::response=~m'.$answer.') { return 1; } else { return 0; }',$safeeval);
826: &Apache::lonxml::debug("current $response");
827: &Apache::lonxml::debug("current $answer");
828: $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
829: } else {
830: my $args_ref=
831: \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
832:
833: $$args_ref{'response'}=$response;
834: &Apache::lonxml::debug("current $response");
835: $$args_ref{'type'}=
836: &Apache::lonxml::get_param('type',$parstack,$safeeval);
837: foreach my $key (keys(%Apache::inputtags::params)) {
838: $$args_ref{$key}=$Apache::inputtags::params{$key};
839: }
840: &Apache::lonxml::debug('answer is'.join(':',$answer));
841: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=($answer);
842: ($result, my @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
843: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
844: my ($awards)=split(/:/,$result);
845: my (@awards) = split(/,/,$awards);
846: ($ad,$msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
847: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
848: }
849: if ($Apache::lonhomework::type eq 'survey' &&
850: ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
851: $ad eq 'EXACT_ANS')) {
852: $ad='SUBMITTED';
853: }
854: &Apache::response::handle_previous(\%previous,$ad);
855: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
856: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
857: }
858: }
859: } elsif ($target eq 'web' || $target eq 'tex') {
860: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
861: my $status = $Apache::inputtags::status['-1'];
862: if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
863: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
864: $increment = &Apache::response::repetition();
865: $result.='\begin{enumerate}';
866: for (my $i=0;$i<$increment;$i++) {
867: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
868: '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
869: }
870: $result.= '\end{enumerate}';
871: }
872: } elsif ($target eq 'answer' || $target eq 'analyze') {
873: if ($target eq 'analyze') {
874: push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
875: $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
876: &Apache::response::check_if_computed($token,$parstack,$safeeval,
877: 'answer');
878: }
879: &Apache::response::setup_params('stringresponse',$safeeval);
880: if ($target eq 'answer') {
881: $result.=&Apache::response::answer_header('stringresponse');
882: }
883: # foreach my $ans (@answers) {
884: if ($target eq 'answer') {
885: $result.=&Apache::response::answer_part('stringresponse',$answer);
886: if ($type eq 're') {
887: $result.=&Apache::response::answer_part('stringresponse',
888: $answerdisplay);
889: }
890: } elsif ($target eq 'analyze') {
891: push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
892: $answer);
893: }
894: # }
895: my $string='Case Insensitive';
896: if ($type eq 'mc') {
897: $string='Multiple Choice';
898: } elsif ($type eq 'cs') {
899: $string='Case Sensitive';
900: } elsif ($type eq 'ci') {
901: $string='Case Insensitive';
902: } elsif ($type eq 're') {
903: $string='Regular Expression';
904: }
905: if ($target eq 'answer') {
906: if ($env{'form.answer_output_mode'} eq 'tex') {
907: $result.=&Apache::response::answer_part('stringresponse',
908: "$string");
909: } else {
910: $result.=&Apache::response::answer_part('stringresponse',
911: "<b>$string</b>");
912: }
913: } elsif ($target eq 'analyze') {
914: push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
915: $type);
916: }
917: if ($target eq 'answer') {
918: $result.=&Apache::response::answer_footer('stringresponse');
919: }
920: } elsif ($target eq 'edit') {
921: $result.='</td></tr>'.&Apache::edit::end_table;
922: }
923: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
924: $target eq 'tex' || $target eq 'analyze') {
925: &Apache::lonxml::increment_counter($increment);
926: }
927: &Apache::response::end_response;
928: return $result;
929: }
930:
931: sub start_formularesponse {
932: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
933: my $result;
934: if ($target eq 'meta') {
935: &Apache::response::start_response($parstack,$safeeval);
936: $result=&Apache::response::meta_package_write('formularesponse');
937: &Apache::response::end_response();
938: } else {
939: $result.=&start_numericalresponse(@_);
940: }
941: return $result;
942: }
943:
944: sub end_formularesponse {
945: return end_numericalresponse(@_);
946: }
947:
948: 1;
949: __END__
950:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>