1: # The LearningOnline Network with CAPA
2: # caparesponse definition
3: #
4: # $Id: caparesponse.pm,v 1.184 2005/12/06 10:01:57 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: my $format;
195: if ($#formats > 0) {
196: $format=$formats[$i];
197: } else {
198: $format=$formats[0];
199: }
200: if ($unit=~/\$/) { $format="\$".$format; $unit=~s/\$//g; }
201: if ($unit=~/\,/) { $format="\,".$format; $unit=~s/\,//g; }
202: my $formatted=&format_number($answer,$format,$target,
203: $safeeval);
204: $answertxt.=$formatted.',';
205: }
206: chop $answertxt;
207: if ($target eq 'web') {
208: $answertxt.=" $unit ";
209: }
210: $Apache::inputtags::answertxt{$id}=$answertxt;
211: }
212: }
213: return $result;
214: }
215:
216: sub check_submission {
217: my ($response,$partid,$id,$tag,$parstack,$safeeval,$ignore_sig)=@_;
218: my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
219: $$args_ref{'response'}=$response;
220: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
221:
222: foreach my $arg ('type','tol','sig','format','unit','calc',
223: 'samples') {
224: $$args_ref{$arg}=
225: &Apache::lonxml::get_param($arg,$parstack,$safeeval);
226: }
227: foreach my $key (keys(%Apache::inputtags::params)) {
228: $$args_ref{$key}=$Apache::inputtags::params{$key};
229: }
230:
231: #no way to enter units, with radio buttons
232: if ($Apache::lonhomework::type eq 'exam' ||
233: lc($hideunit) eq "yes") {
234: delete($$args_ref{'unit'});
235: }
236: #sig fig don't make much sense either
237: if (($Apache::lonhomework::type eq 'exam' ||
238: &Apache::response::submitted('scantron') ||
239: $ignore_sig) &&
240: $tag eq 'numericalresponse') {
241: delete($$args_ref{'sig'});
242: }
243:
244: if ($tag eq 'formularesponse') {
245: $$args_ref{'type'}='fml';
246: } elsif ($tag eq 'numericalresponse') {
247: $$args_ref{'type'}='float';
248: }
249: my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
250: &Apache::lonxml::debug('answer is'.join(':',@answer));
251: if (@answer && defined($answer[0])) {
252: $answer{'INTERNAL'}= {'type' => 'ordered',
253: 'answers' => \@answer };
254: }
255: #FIXME would be nice if we could save name so we know who graded him
256: #correct
257: my (%results,@final_awards,@final_msgs,@names);
258: foreach my $name (keys(%answer)) {
259: &Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
260: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=@{ $answer{$name}{'answers'} };
261: my ($result,@msgs) =
262: &Apache::run::run("&caparesponse_check_list()",$safeeval);
263: &Apache::lonxml::debug('msgs are '.join(':',@msgs));
264: my ($awards)=split(/:/,$result);
265: my @awards= split(/,/,$awards);
266: my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
267: $results{$name}= [$ad,$msg];
268: push(@final_awards,$ad);
269: push(@final_msgs,$msg);
270: push(@names,$name);
271: }
272: my ($ad, $msg, $name) = &Apache::inputtags::finalizeawards(\@final_awards,
273: \@final_msgs,
274: \@names,1);
275: return($ad,$msg);
276: }
277:
278: sub end_numericalresponse {
279: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
280: my $increment=1;
281: my $result = '';
282: if (!$Apache::lonxml::default_homework_loaded) {
283: &Apache::lonxml::default_homework_load($safeeval);
284: }
285: my $partid = $Apache::inputtags::part;
286: my $id = $Apache::inputtags::response[-1];
287: my $tag;
288: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
289: if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
290: if ( $target eq 'grade' && &Apache::response::submitted() ) {
291: &Apache::response::setup_params($tag,$safeeval);
292: if ($Apache::lonhomework::type eq 'exam' &&
293: $tag eq 'formularesponse') {
294: $increment=&Apache::response::scored_response($partid,$id);
295: } else {
296: my $response = &Apache::response::getresponse();
297: if ( $response =~ /[^\s]/) {
298: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
299: &Apache::lonxml::debug("submitted a $response<br>\n");
300: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
301:
302: if ( &Apache::response::submitted('scantron')) {
303: my ($values,$display)=&make_numerical_bubbles($partid,$id,
304: $target,$parstack,$safeeval);
305: $response=$values->[$response];
306: }
307: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
308: my ($ad,$msg)=&check_submission($response,$partid,$id,
309: $tag,$parstack,$safeeval);
310:
311: &Apache::lonxml::debug('ad is'.$ad);
312: if ($ad eq 'SIG_FAIL') {
313: my ($sig_u,$sig_l)=
314: &get_sigrange($Apache::inputtags::params{'sig'});
315: $msg=join(':',$msg,$sig_l,$sig_u);
316: &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
317: $Apache::inputtags::params{'sig'});
318: }
319: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
320: if ($Apache::lonhomework::type eq 'survey' &&
321: ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
322: $ad eq 'EXACT_ANS')) {
323: $ad='SUBMITTED';
324: }
325: &Apache::response::handle_previous(\%previous,$ad);
326: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
327: $Apache::lonhomework::results{"resource.$partid.$id.awardmsg"}=$msg;
328: $result='';
329: }
330: }
331: } elsif ($target eq 'web' || $target eq 'tex') {
332: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
333: $safeeval);
334: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
335: my $status = $Apache::inputtags::status['-1'];
336: if ($Apache::lonhomework::type eq 'exam') {
337: my ($bubble_values,$bubble_display)=
338: &make_numerical_bubbles($partid,$id,$target,$parstack,
339: $safeeval);
340: my $number_of_bubbles = scalar(@{ $bubble_values });
341: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
342: $safeeval);
343: my @alphabet=('A'..'Z');
344: if ($target eq 'web') {
345: if ($tag eq 'numericalresponse') {
346: if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
347: $result.= '<table border="1"><tr>';
348: my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
349: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
350: my $checked='';
351: if ($previous eq $bubble_values->[$ind]) {
352: $checked=" checked='on' ";
353: }
354: $result.='<td><input type="radio" name="HWVAL_'.$id.
355: '" value="'.$bubble_values->[$ind].'" '.$checked
356: .' /><b>'.$alphabet[$ind].'</b>: '.
357: $bubble_display->[$ind].'</td>';
358: }
359: $result.='</tr></table>';
360: } elsif ($tag eq 'formularesponse') {
361: $result.= '<br /><br /><font color="red">
362: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
363: </textarea></font> <br /><br />';
364: }
365: } elsif ($target eq 'tex') {
366: if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
367: $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
368: }
369: if ($tag eq 'numericalresponse') {
370: my ($celllength,$number_of_tables,@table_range)=
371: &get_table_sizes($number_of_bubbles,$bubble_display);
372: my $j=0;
373: my $cou=0;
374: $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
375: for (my $i=0;$i<$number_of_tables;$i++) {
376: $result.='\vskip -1 mm \noindent \setlength{\tabcolsep}{2 mm}\begin{tabular}{';
377: for (my $ind=0;$ind<$table_range[$j];$ind++) {
378: $result.='p{3 mm}p{'.$celllength.' mm}';
379: }
380: $result.='}';
381: for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
382: $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_display->[$ind].'} ';
383: if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
384: }
385: $cou += $table_range[$j];
386: $j++;
387: $result.='\\\\\end{tabular}\vskip 0 mm ';
388: }
389: $result.='\end{enumerate}';
390: } else {
391: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
392: my $repetition = &Apache::response::repetition();
393: $result.='\begin{enumerate}';
394: for (my $i=0;$i<$repetition;$i++) {
395: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
396: }
397: $increment=$repetition;
398: $result.= '\end{enumerate}';
399: }
400: }
401: }
402: } elsif ($target eq 'edit') {
403: $result.='</td></tr>'.&Apache::edit::end_table;
404: } elsif ($target eq 'answer' || $target eq 'analyze') {
405: my $part_id="$partid.$id";
406: if ($target eq 'analyze') {
407: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
408: $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
409: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
410: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
411: push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
412: &Apache::response::check_if_computed($token,$parstack,
413: $safeeval,'answer');
414: }
415: if (scalar(@$tagstack)) {
416: &Apache::response::setup_params($tag,$safeeval);
417: }
418: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
419: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
420: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
421: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
422:
423: if ($target eq 'answer') {
424: $result.=&Apache::response::answer_header($tag);
425: if ($Apache::lonhomework::type eq 'exam') {
426: my ($bubble_values,undef,$correct) = &make_numerical_bubbles($partid,
427: $id,$target,$parstack,$safeeval);
428: $result.=&Apache::response::answer_part($tag,$correct);
429: }
430: }
431: my ($sigline,$tolline);
432: for(my $i=0;$i<=$#answers;$i++) {
433: my $ans=$answers[$i];
434: my $fmt=$formats[0];
435: if (@formats && $#formats) {$fmt=$formats[$i];}
436: my ($high,$low);
437: if ($Apache::inputtags::params{'tol'}) {
438: ($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
439: }
440: my ($sighigh,$siglow);
441: if ($Apache::inputtags::params{'sig'}) {
442: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
443: }
444: if ($fmt && $tag eq 'numericalresponse') {
445: $fmt=~s/e/E/g;
446: if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
447: if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
448: $ans = &format_number($ans,$fmt,$target,$safeeval);
449: #if ($high) {
450: # $high=&format_number($high,$fmt,$target,$safeeval);
451: # $low =&format_number($low,$fmt,$target,$safeeval);
452: #}
453: }
454: if ($target eq 'answer') {
455: if ($high && $tag eq 'numericalresponse') {
456: $ans.=' ['.$low.','.$high.']';
457: $tolline .= "[$low, $high]";
458: }
459: if (defined($sighigh) && $tag eq 'numericalresponse') {
460: if ($env{'form.answer_output_mode'} eq 'tex') {
461: $ans.= " Sig $siglow - $sighigh";
462: } else {
463: $ans.= " Sig <i>$siglow - $sighigh</i>";
464: $sigline .= "[$siglow, $sighigh]";
465: }
466: }
467: $result.=&Apache::response::answer_part($tag,$ans);
468: } elsif ($target eq 'analyze') {
469: push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
470: if ($high) {
471: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
472: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
473: }
474: if ($fmt) {
475: push (@{ $Apache::lonhomework::analyze{"$part_id.format"} }, $fmt);
476: }
477: }
478: }
479:
480: my @fmt_ans;
481: for(my $i=0;$i<=$#answers;$i++) {
482: my $ans=$answers[$i];
483: my $fmt=$formats[0];
484: if (@formats && $#formats) {$fmt=$formats[$i];}
485: if ($fmt && $tag eq 'numericalresponse') {
486: $fmt=~s/e/E/g;
487: if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
488: if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
489: $ans = &format_number($ans,$fmt,$target,$safeeval);
490: if ($fmt=~/\$/ && $unit!~/\$/) { $ans=~s/\$//; }
491: }
492: push(@fmt_ans,$ans);
493: }
494: my $response=join(', ',@fmt_ans);
495: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.
496: $id.'.turnoffunit');
497: if ($unit ne '' &&
498: ! ($Apache::lonhomework::type eq 'exam' ||
499: lc($hideunit) eq "yes") ) {
500: my $cleanunit=$unit;
501: $cleanunit=~s/\$\,//g;
502: $response.=" $cleanunit";
503: }
504:
505: my ($ad,$msg)=&check_submission($response,$partid,$id,$tag,
506: $parstack,$safeeval);
507: if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
508: my $error;
509: if ($tag eq 'formularesponse') {
510: $error=&mt('Computer\'s answer is incorrect ("[_1]").',$response);
511: } else {
512: # answer failed check if it is sig figs that is failing
513: my ($ad,$msg)=&check_submission($response,$partid,$id,
514: $tag,$parstack,
515: $safeeval,1);
516: if ($sigline ne '') {
517: $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);
518: } else {
519: $error=&mt('Computer\'s answer is incorrect ("[_1]"). It is likely that the tolerance range [_2] needs to be adjusted.',$response,$tolline);
520: }
521: }
522: if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
523: &Apache::lonxml::error($error);
524: } else {
525: &Apache::lonxml::warning($error);
526: }
527: }
528:
529: if (defined($unit) and ($unit ne '') and
530: $tag eq 'numericalresponse') {
531: if ($target eq 'answer') {
532: if ($env{'form.answer_output_mode'} eq 'tex') {
533: $result.=&Apache::response::answer_part($tag,
534: " Unit: $unit ");
535: } else {
536: $result.=&Apache::response::answer_part($tag,
537: "Unit: <b>$unit</b>");
538: }
539: } elsif ($target eq 'analyze') {
540: push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
541: }
542: }
543: if ($tag eq 'formularesponse' && $target eq 'answer') {
544: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
545: $result.=&Apache::response::answer_part($tag,$samples);
546: }
547: if ($target eq 'answer') {
548: $result.=&Apache::response::answer_footer($tag);
549: }
550: }
551: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
552: $target eq 'tex' || $target eq 'analyze') {
553: &Apache::lonxml::increment_counter($increment);
554: }
555: &Apache::response::end_response;
556: return $result;
557: }
558:
559: sub get_table_sizes {
560: my ($number_of_bubbles,$rbubble_values)=@_;
561: my $scale=2; #mm for one digit
562: my $cell_width=0;
563: foreach my $member (@$rbubble_values) {
564: my $cell_width_real=0;
565: if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^{(\+|-)?(\d+)}\$?/) {
566: $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale;
567: } elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
568: $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
569: } elsif ($member=~/(\d*)\.(\d*)/) {
570: $cell_width_real=(length($1)+length($2)+3)*$scale;
571: } else {
572: $cell_width_real=(length($member)+1)*$scale*0.9;
573: }
574: if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
575: }
576: $cell_width+=8;
577: my $textwidth;
578: if ($env{'form.textwidth'} ne '') {
579: $env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
580: $textwidth=$1.'.'.$2;
581: } else {
582: $env{'form.textwidth'}=~/(\d+)\.?(\d*)/;
583: $textwidth=$1.'.'.$2;
584: }
585: my $bubbles_per_line=int($textwidth/$cell_width);
586: if ($bubbles_per_line > $number_of_bubbles) {
587: $bubbles_per_line=$number_of_bubbles;
588: }elsif (($bubbles_per_line > $number_of_bubbles/2) && ($number_of_bubbles % 2==0)) {$bubbles_per_line=$number_of_bubbles/2;}
589: my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
590: my @table_range = ();
591: for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
592: if ($number_of_bubbles % $bubbles_per_line) {
593: $number_of_tables++;
594: push @table_range,($number_of_bubbles % $bubbles_per_line);
595: }
596: $cell_width-=8;
597: $cell_width=$cell_width*3/4;
598: return ($cell_width,$number_of_tables,@table_range);
599: }
600:
601: sub format_number {
602: my ($number,$format,$target,$safeeval)=@_;
603: my $ans;
604: if ($format eq '') {
605: #What is the number? (integer,decimal,floating point)
606: if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
607: $format = '3e';
608: } elsif ($number=~/^(\d*)\.(\d*)$/) {
609: $format = '4f';
610: } elsif ($number=~/^(\d*)$/) {
611: $format = 'd';
612: }
613: }
614: if (!$Apache::lonxml::default_homework_loaded) {
615: &Apache::lonxml::default_homework_load($safeeval);
616: }
617: $ans=&Apache::run::run("&prettyprint(q\0$number\0,q\0$format\0,q\0$target\0)",$safeeval);
618: return $ans;
619: }
620:
621: sub make_numerical_bubbles {
622: my ($part,$id,$target,$parstack,$safeeval) =@_;
623:
624: my $number_of_bubbles =
625: &Apache::response::get_response_param($part.'_'.$id,'numbubbles',8);
626:
627: my ($format)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
628: my ($answer)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
629: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,
630: $safeeval);
631: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
632:
633: my @bubble_values=();
634: my @alphabet=('A'..'Z');
635:
636: &Apache::lonxml::debug("answer is $answer incorrect is @incorrect");
637: my @oldseed=&Math::Random::random_get_seed();
638: if (@incorrect) {
639: &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1 gt $number_of_bubbles));
640: if (defined($incorrect[0]) &&
641: scalar(@incorrect)+1 >= $number_of_bubbles) {
642: &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1).":$number_of_bubbles");
643: &Apache::response::setrandomnumber();
644: my @rand_inc=&Math::Random::random_permutation(@incorrect);
645: @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
646: @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
647: &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": which are :".join(':',@bubble_values));
648: &Math::Random::random_set_seed(@oldseed);
649:
650: my $correct;
651: for(my $i=0; $i<=$#bubble_values;$i++) {
652: if ($bubble_values[$i] eq $answer) {
653: $correct = $alphabet[$i];
654: last;
655: }
656: }
657:
658: if (defined($format) && $format ne '') {
659: my @bubble_display;
660: foreach my $value (@bubble_values) {
661: push(@bubble_display,
662: &format_number($value,$format,$target,$safeeval));
663: }
664: return (\@bubble_values,\@bubble_display,$correct);
665: } else {
666: return (\@bubble_values,\@bubble_values,$correct);
667: }
668: }
669: if (defined($incorrect[0]) &&
670: scalar(@incorrect)+1 < $number_of_bubbles) {
671: &Apache::lonxml::warning("Not enough incorrect answers were specified in the incorrect array, ignoring the specified incorrect answers and instead generating them (".join(',',@incorrect).").");
672: }
673: }
674: my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
675: my @powers = (1..$number_of_bubbles);
676: &Apache::response::setrandomnumber();
677: my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
678: my $power = $powers[$ind];
679: $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
680: my $factor = $factors[$ind];
681: my @bubble_display;
682: for ($ind=0;$ind<$number_of_bubbles;$ind++) {
683: $bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
684: $bubble_display[$ind] = &format_number($bubble_values[$ind],
685: $format,$target,$safeeval);
686:
687: }
688: my $correct = $alphabet[$number_of_bubbles-$power];
689: &Math::Random::random_set_seed(@oldseed);
690: return (\@bubble_values,\@bubble_display,$correct);
691: }
692:
693: sub get_tolrange {
694: my ($ans,$tol)=@_;
695: my ($high,$low);
696: if ($tol =~ /%$/) {
697: chop($tol);
698: my $change=$ans*($tol/100.0);
699: $high=$ans+$change;
700: $low=$ans-$change;
701: } else {
702: $high=$ans+$tol;
703: $low=$ans-$tol;
704: }
705: return ($high,$low);
706: }
707:
708: sub get_sigrange {
709: my ($sig)=@_;
710: &Apache::lonxml::debug("Got a sig of :$sig:");
711: my $courseid=$env{'request.course.id'};
712: if (lc($env{"course.$courseid.disablesigfigs"}) eq 'yes') {
713: return (15,0);
714: }
715: my $sig_lbound;
716: my $sig_ubound;
717: if ($sig eq '') {
718: $sig_lbound = 0; #SIG_LB_DEFAULT
719: $sig_ubound =15; #SIG_UB_DEFAULT
720: } else {
721: ($sig_lbound,$sig_ubound) = split(/,/,$sig);
722: if (!defined($sig_lbound)) {
723: $sig_lbound = 0; #SIG_LB_DEFAULT
724: $sig_ubound =15; #SIG_UB_DEFAULT
725: }
726: if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
727: }
728: if (($sig_ubound<$sig_lbound) ||
729: ($sig_lbound > 15) ||
730: ($sig =~/(\+|-)/ ) ) {
731: my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
732: if ($env{'request.state'} eq 'construct') {
733: $errormsg.=
734: &Apache::loncommon::help_open_topic('Significant_Figures');
735: }
736: &Apache::lonxml::error($errormsg);
737: }
738: return ($sig_ubound,$sig_lbound);
739: }
740:
741: sub start_stringresponse {
742: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
743: my $result;
744: my $id = &Apache::response::start_response($parstack,$safeeval);
745: if ($target eq 'meta') {
746: $result=&Apache::response::meta_package_write('stringresponse');
747: } elsif ($target eq 'edit') {
748: $result.=&Apache::edit::tag_start($target,$token);
749: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
750: $result.=&Apache::edit::select_arg('Type:','type',
751: [['cs','Case Sensitive'],['ci','Case Insensitive'],
752: ['mc','Case Insensitive, Any Order'],
753: ['re','Regular Expression']],$token);
754: $result.=&Apache::edit::text_arg('String to display for answer:',
755: 'answerdisplay',$token);
756: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
757: } elsif ($target eq 'modified') {
758: my $constructtag;
759: $constructtag=&Apache::edit::get_new_args($token,$parstack,
760: $safeeval,'answer',
761: 'type','answerdisplay');
762: if ($constructtag) {
763: $result = &Apache::edit::rebuild_tag($token);
764: $result.=&Apache::edit::handle_insert();
765: }
766: } elsif ($target eq 'web') {
767: if ( &Apache::response::show_answer() ) {
768: my $answer=
769: &Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
770: if (!defined $answer || $answer eq '') {
771: $answer=
772: &Apache::lonxml::get_param('answer',$parstack,$safeeval);
773: }
774: $Apache::inputtags::answertxt{$id}=$answer;
775: }
776: } elsif ($target eq 'answer' || $target eq 'grade') {
777: &Apache::response::reset_params();
778: }
779: return $result;
780: }
781:
782: sub end_stringresponse {
783: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
784: my $increment=1;
785: my $result = '';
786: my $part=$Apache::inputtags::part;
787: my $id=$Apache::inputtags::response[-1];
788: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
789: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
790: my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
791: &Apache::lonxml::debug("current $answer ".$token->[2]);
792: if (!$Apache::lonxml::default_homework_loaded) {
793: &Apache::lonxml::default_homework_load($safeeval);
794: }
795: if ( $target eq 'grade' && &Apache::response::submitted() ) {
796: &Apache::response::setup_params('stringresponse',$safeeval);
797: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
798: if ($Apache::lonhomework::type eq 'exam' ||
799: &Apache::response::submitted('scantron')) {
800: $increment=&Apache::response::scored_response($part,$id);
801: } else {
802: my $response = &Apache::response::getresponse();
803: if ( $response =~ /[^\s]/) {
804: my %previous = &Apache::response::check_for_previous($response,
805: $part,$id);
806: &Apache::lonxml::debug("submitted a $response<br>\n");
807: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
808: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
809: $response;
810: my ($ad,$msg);
811: if ($type eq 're' ) {
812: # if the RE wasn't in a var it likely got munged,
813: # thus grab it from the var directly
814: # my $testans=$token->[2]->{'answer'};
815: # if ($testans !~ m/^\s*\$/) {
816: # $answer=$token->[2]->{'answer'};
817: # }
818: ${$safeeval->varglob('LONCAPA::response')}=$response;
819: $result = &Apache::run::run('if ($LONCAPA::response=~m'.$answer.') { return 1; } else { return 0; }',$safeeval);
820: &Apache::lonxml::debug("current $response");
821: &Apache::lonxml::debug("current $answer");
822: $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
823: } else {
824: my $args_ref=
825: \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
826:
827: $$args_ref{'response'}=$response;
828: &Apache::lonxml::debug("current $response");
829: $$args_ref{'type'}=
830: &Apache::lonxml::get_param('type',$parstack,$safeeval);
831: foreach my $key (keys(%Apache::inputtags::params)) {
832: $$args_ref{$key}=$Apache::inputtags::params{$key};
833: }
834: &Apache::lonxml::debug('answer is'.join(':',$answer));
835: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=($answer);
836: ($result, my @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
837: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
838: my ($awards)=split(/:/,$result);
839: my (@awards) = split(/,/,$awards);
840: ($ad,$msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
841: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
842: }
843: if ($Apache::lonhomework::type eq 'survey' &&
844: ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
845: $ad eq 'EXACT_ANS')) {
846: $ad='SUBMITTED';
847: }
848: &Apache::response::handle_previous(\%previous,$ad);
849: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
850: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
851: }
852: }
853: } elsif ($target eq 'web' || $target eq 'tex') {
854: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
855: my $status = $Apache::inputtags::status['-1'];
856: if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
857: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
858: $increment = &Apache::response::repetition();
859: $result.='\begin{enumerate}';
860: for (my $i=0;$i<$increment;$i++) {
861: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
862: '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
863: }
864: $result.= '\end{enumerate}';
865: }
866: } elsif ($target eq 'answer' || $target eq 'analyze') {
867: if ($target eq 'analyze') {
868: push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
869: $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
870: &Apache::response::check_if_computed($token,$parstack,$safeeval,
871: 'answer');
872: }
873: &Apache::response::setup_params('stringresponse',$safeeval);
874: if ($target eq 'answer') {
875: $result.=&Apache::response::answer_header('stringresponse');
876: }
877: # foreach my $ans (@answers) {
878: if ($target eq 'answer') {
879: $result.=&Apache::response::answer_part('stringresponse',$answer);
880: if ($type eq 're') {
881: $result.=&Apache::response::answer_part('stringresponse',
882: $answerdisplay);
883: }
884: } elsif ($target eq 'analyze') {
885: push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
886: $answer);
887: }
888: # }
889: my $string='Case Insensitive';
890: if ($type eq 'mc') {
891: $string='Multiple Choice';
892: } elsif ($type eq 'cs') {
893: $string='Case Sensitive';
894: } elsif ($type eq 'ci') {
895: $string='Case Insensitive';
896: } elsif ($type eq 're') {
897: $string='Regular Expression';
898: }
899: if ($target eq 'answer') {
900: if ($env{'form.answer_output_mode'} eq 'tex') {
901: $result.=&Apache::response::answer_part('stringresponse',
902: "$string");
903: } else {
904: $result.=&Apache::response::answer_part('stringresponse',
905: "<b>$string</b>");
906: }
907: } elsif ($target eq 'analyze') {
908: push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
909: $type);
910: }
911: if ($target eq 'answer') {
912: $result.=&Apache::response::answer_footer('stringresponse');
913: }
914: } elsif ($target eq 'edit') {
915: $result.='</td></tr>'.&Apache::edit::end_table;
916: }
917: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
918: $target eq 'tex' || $target eq 'analyze') {
919: &Apache::lonxml::increment_counter($increment);
920: }
921: &Apache::response::end_response;
922: return $result;
923: }
924:
925: sub start_formularesponse {
926: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
927: my $result;
928: if ($target eq 'meta') {
929: &Apache::response::start_response($parstack,$safeeval);
930: $result=&Apache::response::meta_package_write('formularesponse');
931: &Apache::response::end_response();
932: } else {
933: $result.=&start_numericalresponse(@_);
934: }
935: return $result;
936: }
937:
938: sub end_formularesponse {
939: return end_numericalresponse(@_);
940: }
941:
942: 1;
943: __END__
944:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>