1: # The LearningOnline Network with CAPA
2: # caparesponse definition
3: #
4: # $Id: caparesponse.pm,v 1.249.4.1 2012/01/16 11:54:37 foxr 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 Safe::Hole;
33: use Apache::lonmaxima();
34: use Apache::lonlocal;
35: use Apache::lonnet;
36: use Apache::lonmsg();
37: use Apache::response();
38: use Storable qw(dclone);
39: use Apache::lonnet;
40:
41: BEGIN {
42: &Apache::lonxml::register('Apache::caparesponse',('numericalresponse','stringresponse','formularesponse'));
43: }
44:
45: my %answer;
46: my @answers;
47: my @alphabet=('A'..'Z');
48:
49: sub get_answer { return %answer; };
50: sub push_answer{ push(@answers,dclone(\%answer)); undef(%answer) }
51: sub pop_answer { %answer = %{pop(@answers)}; };
52:
53: my $cur_name;
54: my $tag_internal_answer_name = 'INTERNAL';
55:
56: sub start_answer {
57: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
58: my $result;
59: $cur_name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
60: if ($cur_name =~ /^\s*$/) { $cur_name = $Apache::lonxml::curdepth; }
61: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
62: if (!defined($type) && $tagstack->[-2] eq 'answergroup') {
63: $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
64: }
65: if (!defined($type)) { $type = 'ordered' };
66: $answer{$cur_name}= { 'type' => $type,
67: 'answers' => [] };
68: if ($target eq 'edit') {
69: $result.=&Apache::edit::tag_start($target,$token);
70: $result.=&Apache::edit::text_arg('Name:','name',$token);
71: $result.=&Apache::edit::select_arg('Type:','type',
72: [['ordered', 'Ordered' ],
73: ['unordered','Unordered'],],
74: $token);
75: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
76: } elsif ($target eq 'modified') {
77: my $constructtag = &Apache::edit::get_new_args($token,$parstack,
78: $safeeval,'name',
79: 'type');
80: if ($constructtag) {
81: $result = &Apache::edit::rebuild_tag($token);
82: $result.= &Apache::edit::handle_insert();
83: }
84: }
85: return $result;
86: }
87:
88: sub end_answer {
89: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
90: my $result;
91: if ($target eq 'edit') {
92: $result .= &Apache::edit::tag_end();
93: }
94:
95: undef($cur_name);
96: return $result;
97: }
98:
99: sub insert_answer {
100: return '
101: <answer>
102: <value></value>
103: </answer>';
104: }
105:
106: sub start_answergroup {
107: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
108: my $result;
109: if ($target eq 'edit') {
110: $result.=&Apache::edit::tag_start($target,$token);
111: $result.=&Apache::edit::select_arg('Type:','type',
112: [['ordered', 'Ordered' ],
113: ['unordered','Unordered'],],
114: $token);
115: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
116: } elsif ($target eq 'modified') {
117: my $constructtag = &Apache::edit::get_new_args($token,$parstack,
118: $safeeval,'name',
119: 'type');
120: if ($constructtag) {
121: $result = &Apache::edit::rebuild_tag($token);
122: $result.= &Apache::edit::handle_insert();
123: }
124: }
125: return $result;
126: }
127:
128: sub end_answergroup {
129: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
130: my $result;
131: if ($target eq 'web') {
132: if ( &Apache::response::show_answer() ) {
133: my $partid = $Apache::inputtags::part;
134: my $id = $Apache::inputtags::response[-1];
135: &set_answertext($Apache::lonhomework::history{"resource.$partid.$id.answername"},
136: $target,$token,$tagstack,$parstack,$parser,
137: $safeeval,-2);
138: }
139: } elsif ($target eq 'edit') {
140: $result .= &Apache::edit::tag_end();
141: }
142: return $result;
143: }
144:
145: sub insert_answergroup {
146: return '
147: <answergroup>
148: <answer>
149: <value></value>
150: </answer>
151: </answergroup>';
152: }
153:
154: sub start_value {
155: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
156: my $result;
157: if ( $target eq 'web' || $target eq 'tex' ||
158: $target eq 'grade' || $target eq 'webgrade' ||
159: $target eq 'answer' || $target eq 'analyze' ) {
160: my $bodytext = &Apache::lonxml::get_all_text("/value",$parser,$style);
161: $bodytext = &Apache::run::evaluate($bodytext,$safeeval,
162: $$parstack[-1]);
163:
164: push(@{ $answer{$cur_name}{'answers'} },[$bodytext]);
165:
166: } elsif ($target eq 'edit') {
167: $result.=&Apache::edit::tag_start($target,$token);
168: my $bodytext = &Apache::lonxml::get_all_text("/value",$parser,$style);
169: $result.=&Apache::edit::editline($token->[1],$bodytext,undef,40).
170: &Apache::edit::end_row();
171: } elsif ($target eq 'modified') {
172: $result=$token->[4].&Apache::edit::modifiedfield('/value',$parser);
173: }
174: return $result;
175: }
176:
177: sub end_value {
178: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
179: my $result;
180: if ($target eq 'edit') {
181: $result = &Apache::edit::end_table();
182: }
183: return $result;
184: }
185:
186: sub insert_value {
187: return '
188: <value></value>';
189: }
190:
191: sub start_vector {
192: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
193: my $result;
194: if ( $target eq 'web' || $target eq 'tex' ||
195: $target eq 'grade' || $target eq 'webgrade' ||
196: $target eq 'answer' || $target eq 'analyze' ) {
197: my $bodytext = &Apache::lonxml::get_all_text("/vector",$parser,$style);
198: my @values = &Apache::run::run($bodytext,$safeeval,$$parstack[-1]);
199: if (@values == 1) {
200: @values = split(',',$values[0]);
201: }
202: push(@{ $answer{$cur_name}{'answers'} },\@values);
203: } elsif ($target eq 'edit') {
204: $result.=&Apache::edit::tag_start($target,$token);
205: my $bodytext = &Apache::lonxml::get_all_text("/vector",$parser,$style);
206: $result.=&Apache::edit::editline($token->[1],$bodytext,undef,40).
207: &Apache::edit::end_row();
208: } elsif ($target eq 'modified') {
209: $result=$token->[4].&Apache::edit::modifiedfield('/vector',$parser);
210: }
211: return $result;
212: }
213:
214: sub end_vector {
215: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
216: my $result;
217: if ($target eq 'edit') {
218: $result = &Apache::edit::end_table();
219: }
220: return $result;
221: }
222:
223: sub insert_vector {
224: return '
225: <value></value>';
226: }
227:
228: sub start_array {
229: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
230: my $result;
231: if ( $target eq 'web' || $target eq 'tex' ||
232: $target eq 'grade' || $target eq 'webgrade' ||
233: $target eq 'answer' || $target eq 'analyze' ) {
234: my $bodytext = &Apache::lonxml::get_all_text("/array",$parser,$style);
235: my @values = &Apache::run::evaluate($bodytext,$safeeval,
236: $$parstack[-1]);
237: push(@{ $answer{$cur_name}{'answers'} },@values);
238: }
239: return $result;
240: }
241:
242: sub end_array {
243: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
244: my $result;
245: return $result;
246: }
247:
248: sub start_unit {
249: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
250: my $result;
251: return $result;
252: }
253:
254: sub end_unit {
255: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
256: my $result;
257: return $result;
258: }
259:
260: sub start_numericalresponse {
261: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
262: &Apache::lonxml::register('Apache::caparesponse',
263: ('answer','answergroup','value','array','unit',
264: 'vector'));
265: push(@Apache::lonxml::namespace,'caparesponse');
266: my $id = &Apache::response::start_response($parstack,$safeeval);
267: my $result;
268: undef(%answer);
269: undef(%{$safeeval->varglob('LONCAPA::CAPAresponse_args')});
270: if ($target eq 'edit') {
271: $result.=&Apache::edit::tag_start($target,$token);
272: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
273: if ($token->[1] eq 'numericalresponse') {
274: $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
275: $token).
276: &Apache::loncommon::help_open_topic('numerical_wrong_answers');
277: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
278: &Apache::loncommon::help_open_topic('Physical_Units');
279: $result.=&Apache::edit::text_arg('Format:','format',$token,4).
280: &Apache::loncommon::help_open_topic('Numerical_Response_Format');
281: } elsif ($token->[1] eq 'formularesponse') {
282: $result.=&Apache::edit::text_arg('Sample Points:','samples',
283: $token,40).
284: &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
285: }
286: $result.=&Apache::edit::text_arg('Pre-Processor Subroutine:','preprocess',
287: $token,10);
288: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
289: } elsif ($target eq 'modified') {
290: my $constructtag;
291: if ($token->[1] eq 'numericalresponse') {
292: $constructtag=&Apache::edit::get_new_args($token,$parstack,
293: $safeeval,'answer',
294: 'incorrect','unit',
295: 'format','preprocess');
296: } elsif ($token->[1] eq 'formularesponse') {
297: $constructtag=&Apache::edit::get_new_args($token,$parstack,
298: $safeeval,'answer',
299: 'samples','preproces');
300: }
301: if ($constructtag) {
302: $result = &Apache::edit::rebuild_tag($token);
303: $result.=&Apache::edit::handle_insert();
304: }
305: } elsif ($target eq 'meta') {
306: $result=&Apache::response::meta_package_write('numericalresponse');
307: } elsif ($target eq 'answer' || $target eq 'grade') {
308: &Apache::response::reset_params();
309: } elsif ($target eq 'web') {
310: my $partid = $Apache::inputtags::part;
311: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
312: &Apache::lonxml::debug("Got unit $hideunit for $partid $id");
313: #no way to enter units, with radio buttons
314: if ((lc($hideunit) eq "yes") && ($Apache::lonhomework::type ne 'exam')) {
315: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
316: $safeeval);
317: if ($unit =~ /\S/) { $result.=" (in $unit) "; }
318: }
319: if ( &Apache::response::show_answer() ) {
320: &set_answertext($tag_internal_answer_name,$target,$token,$tagstack,
321: $parstack,$parser,$safeeval,-1);
322: }
323: }
324: return $result;
325: }
326:
327: sub set_answertext {
328: my ($name,$target,$token,$tagstack,$parstack,$parser,$safeeval,
329: $response_level) = @_;
330: &add_in_tag_answer($parstack,$safeeval,$response_level);
331:
332: if ($name eq '' || !ref($answer{$name})) {
333: if (ref($answer{$tag_internal_answer_name})) {
334: $name = $tag_internal_answer_name;
335: } else {
336: $name = (sort(keys(%answer)))[0];
337: }
338: }
339: return if ($name eq '' || !ref($answer{$name}));
340:
341: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
342: $safeeval,$response_level);
343: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval,
344: $response_level);
345:
346: &Apache::lonxml::debug("answer looks to be $name");
347: my @answertxt;
348: for (my $i=0; $i < scalar(@{$answer{$name}{'answers'}}); $i++) {
349: my $answertxt;
350: my $answer=$answer{$name}{'answers'}[$i];
351: foreach my $element (@$answer) {
352: if ( scalar(@$tagstack)
353: && $tagstack->[$response_level] ne 'numericalresponse') {
354: $answertxt.=$element.',';
355: } else {
356: my $format;
357: if ($#formats > 0) {
358: $format=$formats[$i];
359: } else {
360: $format=$formats[0];
361: }
362: if ($unit=~/\$/) { $format="\$".$format; $unit=~s/\$//g; }
363: if ($unit=~/\,/) { $format="\,".$format; $unit=~s/\,//g; }
364: my $formatted=&format_number($element,$format,$target,
365: $safeeval);
366: $answertxt.=' '.$formatted.',';
367: }
368:
369: }
370: chop($answertxt);
371: if ($target eq 'web') {
372: $answertxt.=" $unit ";
373: }
374:
375: push(@answertxt,$answertxt)
376: }
377:
378: my $id = $Apache::inputtags::response[-1];
379: $Apache::inputtags::answertxt{$id}=\@answertxt;
380: }
381:
382: sub setup_capa_args {
383: my ($safeeval,$parstack,$args,$response) = @_;
384: my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
385: undef(%{ $args_ref });
386:
387: foreach my $arg (@{$args}) {
388: $$args_ref{$arg}=
389: &Apache::lonxml::get_param($arg,$parstack,$safeeval);
390: }
391: foreach my $key (keys(%Apache::inputtags::params)) {
392: $$args_ref{$key}=$Apache::inputtags::params{$key};
393: }
394: &setup_capa_response($args_ref,$response);
395: return $args_ref;
396: }
397:
398: sub setup_capa_response {
399: my ($args_ref,$response) = @_;
400:
401: if (ref($response)) {
402: $$args_ref{'response'}=dclone($response);
403: } else {
404: $$args_ref{'response'}=dclone([$response]);
405: }
406: }
407:
408: sub check_submission {
409: my ($response,$partid,$id,$tag,$parstack,$safeeval,$ignore_sig)=@_;
410: my @args = ('type','tol','sig','format','unit','calc','samples','preprocess');
411: my $args_ref = &setup_capa_args($safeeval,$parstack,\@args,$response);
412:
413: my $hideunit=
414: &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
415: #no way to enter units, with radio buttons
416: if ($Apache::lonhomework::type eq 'exam' ||
417: lc($hideunit) eq "yes") {
418: delete($$args_ref{'unit'});
419: }
420: #sig fig don't make much sense either
421: if (($Apache::lonhomework::type eq 'exam' ||
422: &Apache::response::submitted('scantron') ||
423: $ignore_sig) &&
424: $tag eq 'numericalresponse') {
425: delete($$args_ref{'sig'});
426: }
427:
428: if ($tag eq 'formularesponse') {
429: if ($$args_ref{'samples'}) {
430: $$args_ref{'type'}='fml';
431: } else {
432: $$args_ref{'type'}='math';
433: }
434: } elsif ($tag eq 'numericalresponse') {
435: $$args_ref{'type'}='float';
436: } elsif ($tag eq 'stringresponse') {
437: if ($$args_ref{'type'} eq '') {
438: $$args_ref{'type'} = 'ci';
439: }
440: }
441:
442: &add_in_tag_answer($parstack,$safeeval);
443:
444: if (!%answer) {
445: &Apache::lonxml::error("No answers are defined");
446: }
447:
448: my (@final_awards,@final_msgs,@names);
449: foreach my $name (keys(%answer)) {
450: &Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
451:
452: ${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$name});
453: &setup_capa_response($args_ref,$response);
454: use Time::HiRes;
455: my $t0 = [Time::HiRes::gettimeofday()];
456: my ($result,@msgs) =
457: &Apache::run::run("&caparesponse_check_list()",$safeeval);
458: &Apache::lonxml::debug("checking $name $result with $response took ".&Time::HiRes::tv_interval($t0));
459:
460: &Apache::lonxml::debug('msgs are '.join(':',@msgs));
461: my ($awards)=split(/:/,$result);
462: my @awards= split(/,/,$awards);
463: my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
464: push(@final_awards,$ad);
465: push(@final_msgs,$msg);
466: push(@names,$name);
467: }
468: my ($ad, $msg, $name) = &Apache::inputtags::finalizeawards(\@final_awards,
469: \@final_msgs,
470: \@names,1);
471: &Apache::lonxml::debug(" name of picked award is $name from ".join(', ',@names));
472: return($ad,$msg, $name);
473: }
474:
475: sub stringresponse_gradechange {
476: my ($part,$id,$previous,$caller,$response,$ad,$type) = @_;
477: return unless (ref($previous) eq 'HASH');
478: my ($prevarray,$prevaward);
479: my %typenames = (
480: cs => 'Case sensitive',
481: ci => 'Case insensitive',
482: );
483: if ($caller eq 'cs') {
484: return unless (ref($previous->{'version'}) eq 'ARRAY');
485: $prevarray = $previous->{'version'};
486: $prevaward = $previous->{'award'};
487: } elsif ($caller eq 'ci') {
488: return unless (ref($previous->{'versionci'}) eq 'ARRAY');
489: $prevarray = $previous->{'versionci'};
490: $prevaward = $previous->{'awardci'};
491: } else {
492: return;
493: }
494: my $count=0;
495: my %count_lookup;
496: foreach my $i (1..$Apache::lonhomework::history{'version'}) {
497: my $prefix = $i.":resource.$part";
498: next if (!exists($Apache::lonhomework::history{"$prefix.award"}));
499: $count++;
500: $count_lookup{$i} = $count;
501: }
502: my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
503: my %coursedesc = &Apache::lonnet::coursedescription($courseid);
504: my $cdom = $coursedesc{'domain'};
505: my $versions = ' (submissions: '.join(', ',map {$count_lookup{$_} } @{$prevarray}).')';
506: my $warning = "String Response ($typenames{$type}) grading discrepancy: award for response of $response changed from $prevaward".$versions." to $ad; user: $name:$domain in course: $courseid for part: $part response: $id for symb: $symb";
507: &Apache::lonnet::logthis($warning);
508: my $origmail = $Apache::lonnet::perlvar{'lonAdmEMail'};
509: my $recipients = &Apache::loncommon::build_recipient_list(undef,'errormail',
510: $cdom,$origmail);
511: if ($recipients ne '') {
512: &Apache::lonmsg::sendemail($recipients,'Stringresponse Grading Discrepancy',$warning);
513: }
514: return;
515: }
516:
517: sub add_in_tag_answer {
518: my ($parstack,$safeeval,$response_level) = @_;
519: my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval,
520: $response_level);
521: &Apache::lonxml::debug('answer is'.join(':',@answer));
522: if (@answer && $answer[0] =~ /\S/) {
523: $answer{$tag_internal_answer_name}= {'type' => 'ordered',
524: 'answers' => [\@answer] };
525: }
526: }
527:
528: sub capa_formula_fix {
529: my ($expression)=@_;
530: return &Apache::response::implicit_multiplication($expression);
531: }
532:
533: sub end_numericalresponse {
534: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
535:
536: &Apache::lonxml::deregister('Apache::caparesponse',
537: ('answer','answergroup','value','array','unit',
538: 'vector'));
539: pop(@Apache::lonxml::namespace);
540:
541: my $increment=1;
542: my $result = '';
543: if (!$Apache::lonxml::default_homework_loaded) {
544: &Apache::lonxml::default_homework_load($safeeval);
545: }
546: my $partid = $Apache::inputtags::part;
547: my $id = $Apache::inputtags::response[-1];
548: my $tag;
549: my $safehole = new Safe::Hole;
550: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
551:
552: if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
553: if ( $target eq 'grade' && &Apache::response::submitted() ) {
554: &Apache::response::setup_params($tag,$safeeval);
555: if ($Apache::lonhomework::type eq 'exam' &&
556: (($tag eq 'formularesponse') || ($tag eq 'mathresponse'))) {
557: $increment=&Apache::response::scored_response($partid,$id);
558: } else {
559: my $response = &Apache::response::getresponse();
560: if ( $response =~ /[^\s]/) {
561: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
562: &Apache::lonxml::debug("submitted a $response<br>\n");
563: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
564:
565: if ( &Apache::response::submitted('scantron')) {
566: &add_in_tag_answer($parstack,$safeeval);
567: my ($values,$display)=&make_numerical_bubbles($partid,$id,
568: $target,$parstack,$safeeval);
569: $response=$values->[$response];
570: }
571: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
572: my ($ad,$msg,$name)=&check_submission($response,$partid,$id,
573: $tag,$parstack,
574: $safeeval);
575:
576: &Apache::lonxml::debug('ad is'.$ad);
577: if ($ad eq 'SIG_FAIL') {
578: my ($sig_u,$sig_l)=
579: &get_sigrange($Apache::inputtags::params{'sig'});
580: $msg=join(':',$msg,$sig_l,$sig_u);
581: &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
582: $Apache::inputtags::params{'sig'});
583: }
584: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
585: if (($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
586: $ad eq 'EXACT_ANS')) {
587: if ($Apache::lonhomework::type eq 'survey') {
588: $ad='SUBMITTED';
589: } elsif ($Apache::lonhomework::type eq 'surveycred') {
590: $ad='SUBMITTED_CREDIT';
591: } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
592: $ad='ANONYMOUS';
593: } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
594: $ad='ANONYMOUS_CREDIT';
595: }
596: }
597: &Apache::response::handle_previous(\%previous,$ad);
598: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
599: $Apache::lonhomework::results{"resource.$partid.$id.awardmsg"}=$msg;
600: $Apache::lonhomework::results{"resource.$partid.$id.answername"}=$name;
601: $result='';
602: }
603: }
604: } elsif ($target eq 'web' || $target eq 'tex') {
605: &check_for_answer_errors($parstack,$safeeval);
606: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
607: my $status = $Apache::inputtags::status['-1'];
608: if ($Apache::lonhomework::type eq 'exam') {
609: # FIXME support multi dimensional numerical problems
610: # in exam bubbles
611: my ($bubble_values,$bubble_display)=
612: &make_numerical_bubbles($partid,$id,$target,$parstack,
613: $safeeval);
614: my $number_of_bubbles = scalar(@{ $bubble_values });
615: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
616: $safeeval);
617: if ($target eq 'web') {
618: if ($tag eq 'numericalresponse') {
619: if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
620: $result.= '<table border="1"><tr>';
621: my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
622: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
623: my $checked='';
624: if ($previous eq $bubble_values->[$ind]) {
625: $checked=" checked='on' ";
626: }
627: $result.='<td><input type="radio" name="HWVAL_'.$id.
628: '" value="'.$bubble_values->[$ind].'" '.$checked
629: .' /><b>'.$alphabet[$ind].'</b>: '.
630: $bubble_display->[$ind].'</td>';
631: }
632: $result.='</tr></table>';
633: }
634: } elsif ($target eq 'tex') {
635: if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
636: $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
637: }
638: if ($tag eq 'numericalresponse') {
639: $result .= &make_latex_bubbles($bubble_values, $bubble_display);
640: } else {
641: $increment = &Apache::response::repetition();
642: }
643: }
644: }
645: if (($target eq 'web') && ($tag eq 'formularesponse')
646: && ($Apache::lonhomework::type ne 'exam') && ($Apache::inputtags::status['-1'] eq 'CAN_ANSWER')
647: && (&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffeditor') ne 'yes')) {
648: $result.=&Apache::response::edit_mathresponse_button($id,"HWVAL_$id");
649: }
650:
651: &Apache::response::setup_prior_tries_hash(\&format_prior_response_numerical);
652: } elsif ($target eq 'edit') {
653: $result.='</td></tr>'.&Apache::edit::end_table;
654: } elsif ($target eq 'answer' || $target eq 'analyze') {
655: my $part_id="$partid.$id";
656: if ($target eq 'analyze') {
657: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
658: $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
659: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
660: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
661: push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
662: &Apache::response::check_if_computed($token,$parstack,
663: $safeeval,'answer');
664: }
665: if (scalar(@$tagstack)) {
666: &Apache::response::setup_params($tag,$safeeval);
667: }
668: &add_in_tag_answer($parstack,$safeeval);
669: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
670:
671: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
672:
673: if ($target eq 'answer') {
674: $result.=&Apache::response::answer_header($tag,undef,
675: scalar(keys(%answer)));
676: if ($tag eq 'numericalresponse'
677: && $Apache::lonhomework::type eq 'exam') {
678: my ($bubble_values,undef,$correct) = &make_numerical_bubbles($partid,
679: $id,$target,$parstack,$safeeval);
680: $result.=&Apache::response::answer_part($tag,$correct);
681: }
682: }
683: foreach my $name (sort(keys(%answer))) {
684: my @answers = @{ $answer{$name}{'answers'} };
685: if ($target eq 'analyze') {
686: foreach my $info ('answer','ans_high','ans_low','format') {
687: $Apache::lonhomework::analyze{"$part_id.$info"}{$name}=[];
688: }
689: }
690: my ($sigline,$tolline);
691: if ($name ne $tag_internal_answer_name
692: || scalar(keys(%answer)) > 1) {
693: $result.=&Apache::response::answer_part($tag,$name);
694: }
695: for(my $i=0;$i<=$#answers;$i++) {
696: my $ans=$answers[$i];
697: my $fmt=$formats[0];
698: if (@formats && $#formats) {$fmt=$formats[$i];}
699: my ($sighigh,$siglow);
700: if ($Apache::inputtags::params{'sig'}) {
701: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
702: }
703: my @vector;
704: if (ref($ans)) {
705: @vector = @{ $ans };
706: } else {
707: @vector = ($ans);
708: }
709: my @all_answer_info;
710: foreach my $element (@vector) {
711: my ($high,$low);
712: if ($Apache::inputtags::params{'tol'}) {
713: ($high,$low)=&get_tolrange($element,$Apache::inputtags::params{'tol'});
714: }
715: if ($target eq 'answer') {
716: if ($fmt && $tag eq 'numericalresponse') {
717: $fmt=~s/e/E/g;
718: if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
719: if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
720: $element = &format_number($element,$fmt,$target,$safeeval);
721: #if ($high) {
722: # $high=&format_number($high,$fmt,$target,$safeeval);
723: # $low =&format_number($low,$fmt,$target,$safeeval);
724: #}
725: }
726: if ($high && $tag eq 'numericalresponse') {
727: $element.='; ['.$low.'; '.$high.']';
728: $tolline .= "[$low, $high]";
729: }
730: if (defined($sighigh) && $tag eq 'numericalresponse') {
731: if ($env{'form.answer_output_mode'} eq 'tex') {
732: $element.= "; Sig $siglow - $sighigh";
733: } else {
734: $element.= " Sig <i>$siglow - $sighigh</i>";
735: $sigline .= "[$siglow, $sighigh]";
736: }
737: }
738: push(@all_answer_info,$element);
739:
740: } elsif ($target eq 'analyze') {
741: push (@{ $Apache::lonhomework::analyze{"$part_id.answer"}{$name}[$i] }, $element);
742: if ($high) {
743: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"}{$name}[$i] }, $high);
744: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"}{$name}[$i] }, $low);
745: }
746: if ($fmt) {
747: push (@{ $Apache::lonhomework::analyze{"$part_id.format"}{$name}[$i] }, $fmt);
748: }
749: }
750: }
751: if ($target eq 'answer') {
752: $result.= &Apache::response::answer_part($tag,join('; ',@all_answer_info));
753: }
754: }
755:
756: my @fmt_ans;
757: for(my $i=0;$i<=$#answers;$i++) {
758: my $ans=$answers[$i];
759: my $fmt=$formats[0];
760: if (@formats && $#formats) {$fmt=$formats[$i];}
761: foreach my $element (@$ans) {
762: if ($fmt && $tag eq 'numericalresponse') {
763: $fmt=~s/e/E/g;
764: if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
765: if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
766: $element = &format_number($element,$fmt,$target,
767: $safeeval);
768: if ($fmt=~/\$/ && $unit!~/\$/) { $element=~s/\$//; }
769: }
770: }
771: push(@fmt_ans,join(',',@$ans));
772: }
773: my $response=\@fmt_ans;
774:
775: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.
776: $id.'.turnoffunit');
777: if ($unit ne '' &&
778: ! ($Apache::lonhomework::type eq 'exam' ||
779: lc($hideunit) eq "yes") ) {
780: my $cleanunit=$unit;
781: $cleanunit=~s/\$\,//g;
782: foreach my $ans (@fmt_ans) {
783: $ans.=" $cleanunit";
784: }
785: }
786: my ($ad,$msg)=&check_submission($response,$partid,$id,$tag,
787: $parstack,$safeeval);
788: if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
789: my $error;
790: if ($tag eq 'formularesponse') {
791: $error=&mt("Computer's answer is incorrect ([_1]).",'"'.join(', ',@$response).'"');
792: } else {
793: # answer failed check if it is sig figs that is failing
794: my ($ad,$msg)=&check_submission($response,$partid,$id,
795: $tag,$parstack,
796: $safeeval,1);
797: $error=&mt("Computer's answer is incorrect ([_1]).",'"'.join(', ',@$response).'"').' ';
798: if ($sigline ne '') {
799: $error.=&mt('It is likely that the tolerance range [_1] or significant figures [_2] need to be adjusted.',$tolline,$sigline);
800: } else {
801: $error.=&mt('It is likely that the tolerance range [_1] needs to be adjusted.',$tolline);
802: }
803: }
804: if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
805: &Apache::lonxml::error($error);
806: } else {
807: &Apache::lonxml::warning($error);
808: }
809: }
810:
811: if (defined($unit) and ($unit ne '') and
812: $tag eq 'numericalresponse') {
813: if ($target eq 'answer') {
814: if ($env{'form.answer_output_mode'} eq 'tex') {
815: $result.=&Apache::response::answer_part($tag,
816: " Unit: $unit ");
817: } else {
818: $result.=&Apache::response::answer_part($tag,
819: "Unit: <b>$unit</b>");
820: }
821: } elsif ($target eq 'analyze') {
822: push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
823: }
824: }
825: if ($tag eq 'formularesponse' && $target eq 'answer') {
826: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
827: $result.=&Apache::response::answer_part($tag,$samples);
828: }
829: $result.=&Apache::response::next_answer($tag,$name);
830: }
831: if ($target eq 'answer') {
832: $result.=&Apache::response::answer_footer($tag);
833: }
834: }
835: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
836: $target eq 'tex' || $target eq 'analyze') {
837: if (($tag eq 'formularesponse') && ($target eq 'analyze')) {
838: my $type = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.type');
839: if ($type eq 'exam') {
840: $increment = &Apache::response::repetition();
841: }
842: }
843: &Apache::lonxml::increment_counter($increment,"$partid.$id");
844: if ($target eq 'analyze') {
845: &Apache::lonhomework::set_bubble_lines();
846: }
847: }
848: &Apache::response::end_response();
849: return $result;
850: }
851:
852: sub format_prior_response_numerical {
853: my ($mode,$answer) = @_;
854: if (ref($answer)) {
855: my $result = '<table class="LC_prior_numerical"><tr>';
856: foreach my $element (@{ $answer }) {
857: $result.= '<td><span class="LC_prior_numerical">'.
858: &HTML::Entities::encode($element,'"<>&').'</span></td>';
859: }
860: $result.='</tr></table>';
861: return $result;
862: }
863: return '<span class="LC_prior_numerical">'.
864: &HTML::Entities::encode($answer,'"<>&').'</span>';
865:
866: }
867:
868: sub check_for_answer_errors {
869: my ($parstack,$safeeval) = @_;
870: &add_in_tag_answer($parstack,$safeeval);
871: my %counts;
872: foreach my $name (keys(%answer)) {
873: push(@{$counts{scalar(@{$answer{$name}{'answers'}})}},$name);
874: }
875: if (scalar(keys(%counts)) > 1) {
876: my $counts = join(' ',map {
877: my $count = $_;
878: &mt("Answers [_1] had [_2] components.",
879: '<tt>'.join(', ',@{$counts{$count}}).'</tt>',
880: $count);
881: } (sort(keys(%counts))));
882: &Apache::lonxml::error(&mt("All answers must have the same number of components. Varying numbers of answers were seen. ").$counts);
883: }
884: my $expected_number_of_inputs = (keys(%counts))[0];
885: if ( $expected_number_of_inputs > 0
886: && $expected_number_of_inputs != scalar(@Apache::inputtags::inputlist)) {
887: &Apache::lonxml::error(&mt("Expected [_1] input fields, but there were only [_2] seen.",
888: $expected_number_of_inputs,
889: scalar(@Apache::inputtags::inputlist)));
890: }
891: }
892:
893: sub get_table_sizes {
894: my ($number_of_bubbles,$rbubble_values)=@_;
895: my $scale=2; #mm for one digit
896: my $cell_width=0;
897: &Apache::lonnet::logthis("textwidth envvar: " . $env{'form.textwidth'});
898: foreach my $member (@$rbubble_values) {
899: my $cell_width_real=0;
900: if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^{(\+|-)?(\d+)}\$?/) {
901: $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale;
902: } elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
903: $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
904: } elsif ($member=~/(\d*)\.(\d*)/) {
905: $cell_width_real=(length($1)+length($2)+3)*$scale;
906: } else {
907: $cell_width_real=(length($member)+1)*$scale*0.9;
908: }
909: if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
910: }
911: $cell_width+=8;
912: my $textwidth;
913: if ($env{'form.textwidth'} ne '') {
914: $env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
915: $textwidth=$1.'.'.$2;
916: } else {
917: $env{'form.textwidth'}=~/(\d+)\.?(\d*)/;
918: $textwidth=$1.'.'.$2;
919: }
920: my $bubbles_per_line=int($textwidth/$cell_width);
921: if ($bubbles_per_line > $number_of_bubbles) {
922: $bubbles_per_line=$number_of_bubbles;
923: } elsif (($bubbles_per_line > $number_of_bubbles/2)
924: && ($number_of_bubbles % 2==0)) {
925: $bubbles_per_line=$number_of_bubbles/2;
926: }
927: if ($bubbles_per_line < 1) {
928: $bubbles_per_line=1;
929: }
930: my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
931: my @table_range = ();
932: for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
933: if ($number_of_bubbles % $bubbles_per_line) {
934: $number_of_tables++;
935: push @table_range,($number_of_bubbles % $bubbles_per_line);
936: }
937: $cell_width-=8;
938: $cell_width=$cell_width*3/4;
939: return ($cell_width,$number_of_tables,@table_range);
940: }
941:
942: sub format_number {
943: my ($number,$format,$target,$safeeval)=@_;
944: my $ans;
945: if ($format eq '') {
946: #What is the number? (integer,decimal,floating point)
947: if ($number=~/^(\d*\.?\d*)(E|e)[+\-]?(\d*)$/) {
948: $format = '3e';
949: } elsif ($number=~/^(\d*)\.(\d*)$/) {
950: $format = '4f';
951: } elsif ($number=~/^(\d*)$/) {
952: $format = 'd';
953: }
954: }
955: if (!$Apache::lonxml::default_homework_loaded) {
956: &Apache::lonxml::default_homework_load($safeeval);
957: }
958: $ans=&Apache::run::run("&prettyprint(q\0$number\0,q\0$format\0,q\0$target\0)",$safeeval);
959: return $ans;
960: }
961:
962: sub make_numerical_bubbles {
963: my ($part,$id,$target,$parstack,$safeeval) =@_;
964:
965: if (!%answer) {
966: &Apache::lonxml::error(&mt("No answers defined for response [_1] in part [_2] to make bubbles for.",$id,$part));
967: return ([],[],undef);
968: }
969:
970: my $number_of_bubbles =
971: &Apache::response::get_response_param($part.'_'.$id,'numbubbles',8);
972:
973: my ($format)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
974: my $name = (exists($answer{$tag_internal_answer_name})
975: ? $tag_internal_answer_name
976: : (sort(keys(%answer)))[0]);
977:
978: if ( scalar(@{$answer{$name}{'answers'}}) > 1) {
979: &Apache::lonxml::error("Only answers with 1 component are supported in exam mode");
980: }
981: if (scalar(@{$answer{$name}{'answers'}[0]}) > 1) {
982: &Apache::lonxml::error("Vector answers are unsupported in exam mode.");
983: }
984:
985: my $answer = $answer{$name}{'answers'}[0][0];
986: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,
987: $safeeval);
988: if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
989:
990: my @bubble_values=();
991: my @alphabet=('A'..'Z');
992:
993: &Apache::lonxml::debug("answer is $answer incorrect is @incorrect");
994: my @oldseed=&Math::Random::random_get_seed();
995: if (@incorrect) {
996: &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1 gt $number_of_bubbles));
997: if (defined($incorrect[0]) &&
998: scalar(@incorrect)+1 >= $number_of_bubbles) {
999: &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1).":$number_of_bubbles");
1000: &Apache::response::setrandomnumber();
1001: my @rand_inc=&Math::Random::random_permutation(@incorrect);
1002: @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
1003: @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
1004: &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": which are :".join(':',@bubble_values));
1005: &Math::Random::random_set_seed(@oldseed);
1006:
1007: my $correct;
1008: for(my $i=0; $i<=$#bubble_values;$i++) {
1009: if ($bubble_values[$i] eq $answer) {
1010: $correct = $alphabet[$i];
1011: last;
1012: }
1013: }
1014:
1015: if (defined($format) && $format ne '') {
1016: my @bubble_display;
1017: foreach my $value (@bubble_values) {
1018: push(@bubble_display,
1019: &format_number($value,$format,$target,$safeeval));
1020: }
1021: return (\@bubble_values,\@bubble_display,$correct);
1022: } else {
1023: return (\@bubble_values,\@bubble_values,$correct);
1024: }
1025: }
1026: if (defined($incorrect[0]) &&
1027: scalar(@incorrect)+1 < $number_of_bubbles) {
1028: &Apache::lonxml::warning("Not enough incorrect answers were specified in the incorrect array, ignoring the specified incorrect answers and instead generating them (".join(',',@incorrect).").");
1029: }
1030: }
1031: my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
1032: my @powers = (1..$number_of_bubbles);
1033: &Apache::response::setrandomnumber();
1034: my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
1035: my $power = $powers[$ind];
1036: $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
1037: my $factor = $factors[$ind];
1038: my @bubble_display;
1039: my $answerfactor=$answer;
1040: if ($answer==0) {
1041: $answerfactor=&Math::Random::random_uniform_integer(1,1,100)/
1042: &Math::Random::random_uniform_integer(1,1,10);
1043: }
1044: for ($ind=0;$ind<$number_of_bubbles;$ind++) {
1045: $bubble_values[$ind] = $answerfactor*($factor**($power-$powers[$#powers-$ind]));
1046: $bubble_display[$ind] = &format_number($bubble_values[$ind],
1047: $format,$target,$safeeval);
1048: }
1049: my $correct = $alphabet[$number_of_bubbles-$power];
1050: if ($answer==0) {
1051: $correct='A';
1052: $bubble_values[0]=0;
1053: $bubble_display[0] = &format_number($bubble_values[0],
1054: $format,$target,$safeeval);
1055: }
1056: &Math::Random::random_set_seed(@oldseed);
1057: return (\@bubble_values,\@bubble_display,$correct);
1058: }
1059:
1060: ##
1061: # Produce LaTeX bubbles given a set of bubble values:
1062: #
1063: # @param bubble_values - reference to an array of bubble 'values'
1064: # @param bubble_display - reference to the array of texts to display to the user
1065: # for each bubble_value (this is mostly for numerical response
1066: # when the displayed value may not be an exact
1067: # representation of the bubble value.
1068: #
1069: # @return string - the LaTeX fragment that produces the bubbles.
1070: #
1071: sub make_latex_bubbles {
1072: my ($bubble_values, $bubble_display) = @_;
1073: my $result;
1074:
1075: my $number_of_bubbles = scalar(@{$bubble_values});
1076:
1077: # Get the number of rows and columns in each row of the bubble
1078: # table:
1079:
1080: my ($celllength, $number_of_tables, @table_range) =
1081: &get_table_sizes($number_of_bubbles, $bubble_display);
1082:
1083: my $j=0;
1084: my $cou=0;
1085: $result.='\vskip 2mm \noindent ';
1086: $result .= '\textbf{'.$Apache::lonxml::counter.'.} \vskip -3mm ';
1087:
1088: for (my $i=0;$i<$number_of_tables;$i++) {
1089: if ($i == 0) {
1090: $result .= '\vskip -1mm ';
1091: } else {
1092: $result .= '\vskip 1mm ';
1093: }
1094: $result.='\noindent \setlength{\tabcolsep}{2 mm}\hskip 2pc\begin{tabular}{';
1095: for (my $ind=0;$ind<$table_range[$j];$ind++) {
1096: $result.='p{3 mm}p{'.$celllength.' mm}';
1097: }
1098: $result.='}';
1099: for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
1100: $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_display->[$ind].'} ';
1101: if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
1102: }
1103: $cou += $table_range[$j];
1104: $j++;
1105: $result.='\\\\\end{tabular}\vskip 0 mm ';
1106: }
1107: return $result;
1108: }
1109:
1110: sub get_tolrange {
1111: my ($ans,$tol)=@_;
1112: my ($high,$low);
1113: if ($tol =~ /%$/) {
1114: chop($tol);
1115: my $change=$ans*($tol/100.0);
1116: $high=$ans+$change;
1117: $low=$ans-$change;
1118: } else {
1119: $high=$ans+$tol;
1120: $low=$ans-$tol;
1121: }
1122: return ($high,$low);
1123: }
1124:
1125: sub get_sigrange {
1126: my ($sig)=@_;
1127: #&Apache::lonxml::debug("Got a sig of :$sig:");
1128: my $courseid=$env{'request.course.id'};
1129: if ($env{'request.state'} ne 'construct'
1130: && lc($env{"course.$courseid.disablesigfigs"}) eq 'yes') {
1131: return (15,0);
1132: }
1133: my $sig_lbound;
1134: my $sig_ubound;
1135: if ($sig eq '') {
1136: $sig_lbound = 0; #SIG_LB_DEFAULT
1137: $sig_ubound =15; #SIG_UB_DEFAULT
1138: } else {
1139: ($sig_lbound,$sig_ubound) = split(/,/,$sig);
1140: if (!defined($sig_lbound)) {
1141: $sig_lbound = 0; #SIG_LB_DEFAULT
1142: $sig_ubound =15; #SIG_UB_DEFAULT
1143: }
1144: if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
1145: }
1146: if (($sig_ubound<$sig_lbound) ||
1147: ($sig_lbound > 15) ||
1148: ($sig =~/(\+|-)/ ) ) {
1149: my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
1150: if ($env{'request.state'} eq 'construct') {
1151: $errormsg.=
1152: &Apache::loncommon::help_open_topic('Significant_Figures');
1153: }
1154: &Apache::lonxml::error($errormsg);
1155: }
1156: return ($sig_ubound,$sig_lbound);
1157: }
1158:
1159: sub format_prior_response_string {
1160: my ($mode,$answer) =@_;
1161: return '<span class="LC_prior_string">'.
1162: &HTML::Entities::encode($answer,'"<>&').'</span>';
1163: }
1164:
1165: sub start_stringresponse {
1166: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1167: my $result;
1168: my $id = &Apache::response::start_response($parstack,$safeeval);
1169: undef(%answer);
1170: if ($target eq 'meta') {
1171: $result=&Apache::response::meta_package_write('stringresponse');
1172: } elsif ($target eq 'edit') {
1173: $result.=&Apache::edit::tag_start($target,$token);
1174: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
1175: $result.=&Apache::edit::select_arg('Type:','type',
1176: [['cs','Case Sensitive'],['ci','Case Insensitive'],
1177: ['mc','Case Insensitive, Any Order'],
1178: ['re','Regular Expression']],$token);
1179: $result.=&Apache::edit::text_arg('String to display for answer:',
1180: 'answerdisplay',$token);
1181: $result.=&Apache::edit::text_arg('Pre-Processor Subroutine:','preprocess',
1182: $token,10);
1183: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1184: } elsif ($target eq 'modified') {
1185: my $constructtag;
1186: $constructtag=&Apache::edit::get_new_args($token,$parstack,
1187: $safeeval,'answer',
1188: 'type','answerdisplay','preprocess');
1189: if ($constructtag) {
1190: $result = &Apache::edit::rebuild_tag($token);
1191: $result.=&Apache::edit::handle_insert();
1192: }
1193: } elsif ($target eq 'web') {
1194: if ( &Apache::response::show_answer() ) {
1195: my $answer=
1196: &Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
1197: if (!defined $answer || $answer eq '') {
1198: $answer=
1199: &Apache::lonxml::get_param('answer',$parstack,$safeeval);
1200: }
1201: $Apache::inputtags::answertxt{$id}=[$answer];
1202: }
1203: } elsif ($target eq 'answer' || $target eq 'grade') {
1204: &Apache::response::reset_params();
1205: }
1206: return $result;
1207: }
1208:
1209: sub end_stringresponse {
1210: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1211:
1212: my $result = '';
1213: my $part=$Apache::inputtags::part;
1214: my $id=$Apache::inputtags::response[-1];
1215: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
1216: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
1217: my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
1218: &Apache::lonxml::debug("current $answer ".$token->[2]);
1219: if (!$Apache::lonxml::default_homework_loaded) {
1220: &Apache::lonxml::default_homework_load($safeeval);
1221: }
1222: if ( $target eq 'grade' && &Apache::response::submitted() ) {
1223: &Apache::response::setup_params('stringresponse',$safeeval);
1224: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
1225: if ($Apache::lonhomework::type eq 'exam' ||
1226: &Apache::response::submitted('scantron')) {
1227: &Apache::response::scored_response($part,$id);
1228:
1229: } else {
1230: my $response = &Apache::response::getresponse();
1231: if ( $response =~ /[^\s]/) {
1232: my %previous = &Apache::response::check_for_previous($response,
1233: $part,$id,
1234: undef,$type);
1235: &Apache::lonxml::debug("submitted a $response<br>\n");
1236: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
1237: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
1238: $response;
1239: my ($ad,$msg);
1240: if ($type eq 're' ) {
1241: # if the RE wasn't in a var it likely got munged,
1242: # thus grab it from the var directly
1243: # my $testans=$token->[2]->{'answer'};
1244: # if ($testans !~ m/^\s*\$/) {
1245: # $answer=$token->[2]->{'answer'};
1246: # }
1247: ${$safeeval->varglob('LONCAPA::response')}=$response;
1248: my $preprocess=&Apache::lonxml::get_param('preprocess',$parstack,$safeeval);
1249: $preprocess=~s/^\&//;
1250: if (defined($preprocess)) {
1251: &Apache::run::run('$LONCAPA::response=&'.$preprocess.'($LONCAPA::response);',$safeeval);
1252: }
1253: $result = &Apache::run::run('if ($LONCAPA::response=~m'.$answer.') { return 1; } else { return 0; }',$safeeval);
1254: &Apache::lonxml::debug("current $response");
1255: &Apache::lonxml::debug("current $answer");
1256: $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
1257: } else {
1258: my @args = ('type','preprocess');
1259: my $args_ref = &setup_capa_args($safeeval,$parstack,
1260: \@args,$response);
1261: if ($$args_ref{'type'} eq '') {
1262: $$args_ref{'type'} = 'ci';
1263: }
1264: &add_in_tag_answer($parstack,$safeeval);
1265: my (@final_awards,@final_msgs,@names,%ansstring);
1266: foreach my $name (keys(%answer)) {
1267: &Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
1268: ${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$name});
1269: my ($result, @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
1270: if ($$args_ref{'type'} =~ /^c[si]$/) {
1271: $ansstring{$name} = pop(@msgs);
1272: my $control_chars_removed = pop(@msgs);
1273: my $error = pop(@msgs);
1274: if (($error ne '') ||
1275: ($control_chars_removed)) {
1276: my ($symb,$courseid,$sdomain,$sname) =
1277: &Apache::lonnet::whichuser();
1278: if ($control_chars_removed) {
1279: my $showresponse = $response;
1280: if ($response =~ /[\000-\037]/) {
1281: $response =~ s/[\000-\037]//g;
1282: }
1283: if ($showresponse =~ /[\r\n\f]/) {
1284: my @lines = split(/[\r\n\f]+/,$showresponse);
1285: $showresponse = join('\\n',@lines);
1286: }
1287: &Apache::lonnet::logthis("Stringresponse grading: control characters stripped from submission ".$showresponse." for $sname:$sdomain in $courseid for part: $part response: $id and symb: $symb");
1288: $Apache::lonhomework::results{"resource.$part.$id.submission"} = $response;
1289: }
1290: if ($error ne '') {
1291: &Apache::lonnet::logthis("Stringresponse grading error: $error for $sname:$sdomain in $courseid for part: $part response: $id and symb: $symb");
1292: }
1293: }
1294: }
1295: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
1296: my ($awards)=split(/:/,$result);
1297: my (@awards) = split(/,/,$awards);
1298: ($ad,$msg) =
1299: &Apache::inputtags::finalizeawards(\@awards,\@msgs);
1300: push(@final_awards,$ad);
1301: push(@final_msgs,$msg);
1302: push(@names,$name);
1303: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
1304: }
1305: ($ad, $msg, my $name) =
1306: &Apache::inputtags::finalizeawards(\@final_awards,
1307: \@final_msgs,
1308: \@names,1);
1309: if (keys(%ansstring) > 0) {
1310: $Apache::lonhomework::results{"resource.$part.$id.answerstring"} = &Apache::lonnet::hash2str(%ansstring);
1311: }
1312: }
1313: if (($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
1314: $ad eq 'EXACT_ANS')) {
1315: if ($Apache::lonhomework::type eq 'survey') {
1316: $ad='SUBMITTED';
1317: } elsif ($Apache::lonhomework::type eq 'surveycred') {
1318: $ad='SUBMITTED_CREDIT';
1319: } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
1320: $ad='ANONYMOUS';
1321: } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
1322: $ad='ANONYMOUS_CREDIT';
1323: }
1324: }
1325: unless (($env{'request.state'} eq 'construct') ||
1326: ($Apache::lonhomework::type eq 'randomizetry')) {
1327: if (($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' || $ad eq 'EXACT_ANS')) {
1328: if ($previous{'used'}) {
1329: if ($ad ne $previous{'award'}) {
1330: if (($previous{'award'} eq 'INCORRECT' ||
1331: $previous{'award'} eq 'APPROX_ANS' ||
1332: $previous{'award'} eq 'EXACT_ANS')) {
1333: &stringresponse_gradechange($part,$id,\%previous,
1334: 'cs',$response,$ad,$type);
1335: }
1336: }
1337: } elsif ($previous{'usedci'}) {
1338: if ($ad ne $previous{'awardci'}) {
1339: if (($previous{'awardci'} eq 'INCORRECT' ||
1340: $previous{'awardci'} eq 'APPROX_ANS' ||
1341: $previous{'awardci'} eq 'EXACT_ANS')) {
1342: &stringresponse_gradechange($part,$id,\%previous,
1343: 'ci',$response,$ad,$type);
1344: }
1345: }
1346: }
1347: }
1348: }
1349: &Apache::response::handle_previous(\%previous,$ad);
1350: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
1351: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
1352: }
1353: }
1354: } elsif ($target eq 'answer' || $target eq 'analyze') {
1355: &add_in_tag_answer($parstack,$safeeval);
1356: if ($target eq 'analyze') {
1357: push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
1358: $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
1359: &Apache::response::check_if_computed($token,$parstack,$safeeval,
1360: 'answer');
1361: }
1362: &Apache::response::setup_params('stringresponse',$safeeval);
1363: if ($target eq 'answer') {
1364: $result.=&Apache::response::answer_header('stringresponse');
1365: }
1366: foreach my $name (keys(%answer)) {
1367: my @answers = @{ $answer{$name}{'answers'} };
1368: for (my $i=0;$i<=$#answers;$i++) {
1369: my $answer_part = $answers[$i];
1370: foreach my $element (@{$answer_part}) {
1371: if ($target eq 'answer') {
1372: $result.=&Apache::response::answer_part('stringresponse',
1373: $element);
1374: if ($env{'form.grade_retrieveanswers'}) {
1375: $env{'form.grade_answers.resource.'.$part.'.'.$id} = $element;
1376: }
1377: } elsif ($target eq 'analyze') {
1378: push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"}{$name}[$i] },
1379: $element);
1380: }
1381: }
1382: if ($target eq 'answer' && $type eq 're') {
1383: $result.=&Apache::response::answer_part('stringresponse',
1384: $answerdisplay);
1385: }
1386: }
1387: }
1388: my $string='Case Insensitive';
1389: if ($type eq 'mc') {
1390: $string='Multiple Choice';
1391: } elsif ($type eq 'cs') {
1392: $string='Case Sensitive';
1393: } elsif ($type eq 'ci') {
1394: $string='Case Insensitive';
1395: } elsif ($type eq 're') {
1396: $string='Regular Expression';
1397: }
1398: if ($target eq 'answer') {
1399: if ($env{'form.answer_output_mode'} eq 'tex') {
1400: $result.=&Apache::response::answer_part('stringresponse',
1401: "$string");
1402: } else {
1403: $result.=&Apache::response::answer_part('stringresponse',
1404: "<b>$string</b>");
1405: }
1406: } elsif ($target eq 'analyze') {
1407: push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
1408: $type);
1409: }
1410: if ($target eq 'answer') {
1411: $result.=&Apache::response::answer_footer('stringresponse');
1412: }
1413: } elsif ($target eq 'edit') {
1414: $result.='</td></tr>'.&Apache::edit::end_table;
1415: } elsif ($target eq 'web' || $target eq 'tex') {
1416: &Apache::response::setup_prior_tries_hash(\&format_prior_response_string);
1417: }
1418: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1419: $target eq 'tex' || $target eq 'analyze') {
1420: my $repetition = &Apache::response::repetition();
1421: &Apache::lonxml::increment_counter($repetition,"$part.$id");
1422: if ($target eq 'analyze') {
1423: &Apache::lonhomework::set_bubble_lines();
1424: }
1425: }
1426: &Apache::response::end_response;
1427: return $result;
1428: }
1429:
1430: sub start_formularesponse {
1431: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1432: my $result;
1433: if ($target eq 'meta') {
1434: &Apache::response::start_response($parstack,$safeeval);
1435: $result=&Apache::response::meta_package_write('formularesponse');
1436: &Apache::response::end_response();
1437: } else {
1438: $result.=&start_numericalresponse(@_);
1439: }
1440: return $result;
1441: }
1442:
1443: sub end_formularesponse {
1444: return end_numericalresponse(@_);
1445: }
1446:
1447: 1;
1448: __END__
1449:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>