File:
[LON-CAPA] /
loncom /
homework /
hint.pm
Revision
1.51:
download - view:
text,
annotated -
select for diffs
Tue Mar 16 19:47:47 2004 UTC (20 years, 5 months ago) by
albertel
Branches:
MAIN
CVS tags:
HEAD
- Fixes Bug#2820 (internal vars were clashing with user vars)
- Cleans up the way data is put into safe space, less escaping, names scaping etc issues
- <stringhint> supports re mode now.
1: # The LearningOnline Network with CAPA
2: # implements the tags that control the hints
3: #
4: # $Id: hint.pm,v 1.51 2004/03/16 19:47:47 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 me&aree 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::hinttags;
30:
31: use strict;
32: use Apache::lonnet;
33: use capa;
34:
35: BEGIN {
36: &Apache::lonxml::register('Apache::hinttags',('hintgroup','hintpart','numericalhint','stringhint','formulahint','optionhint','radiobuttonhint'));
37: }
38:
39:
40: @Apache::hint::which=();
41: sub start_hintgroup {
42: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
43: my $skiptoend='0';
44: my $result;
45:
46: if ($target eq 'web') {
47: my $id=$Apache::inputtags::part;
48: my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
49: if ( $numtries eq '') { $numtries = 0; }
50: my $hinttries=&Apache::lonnet::EXT("resource.$id.hinttries");
51: if ($ENV{'request.state'} eq 'construct' &&
52: defined($Apache::inputtags::params{'hinttries'})) {
53: $hinttries=$Apache::inputtags::params{'hinttries'};
54: }
55:
56: if ( $hinttries eq '') { $hinttries = 1; }
57: &Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
58: my $gradestatus=$Apache::lonhomework::history{"resource.$id.solved"};
59: if ( $numtries < $hinttries || $gradestatus =~ /^correct/) {
60: &Apache::lonxml::get_all_text("/hintgroup",$parser);
61: }
62: &Apache::lonxml::startredirection;
63: } elsif ($target eq 'tex') {
64: $result .= '\keephidden{';
65: }
66: @Apache::hint::which=();
67: return $result;
68: }
69:
70: sub end_hintgroup {
71: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
72: my $result;
73:
74: if ($target eq 'web') {
75: my $id=$Apache::inputtags::part;
76: my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
77: if ( $numtries eq '') { $numtries = 0; }
78: my $hinttries=&Apache::lonnet::EXT("resource.$id.hinttries");
79: if ( $hinttries eq '') { $hinttries = 1; }
80: &Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
81: my $hinttext=&Apache::lonxml::endredirection;
82: if ($Apache::lonhomework::type ne 'exam' &&
83: $numtries >= $hinttries && $hinttext =~/\S/) {
84: $result='<table bgcolor="#dddddd"><tr><td>'.
85: $hinttext.'</td></tr></table>';
86: }
87: } elsif ($target eq 'edit') {
88: $result.=&Apache::edit::end_table();
89: } elsif ($target eq 'tex') {
90: $result .= '}';
91: }
92: @Apache::hint::which=();
93: return $result;
94: }
95:
96: sub start_numericalhint {
97: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
98: #do everything in end, so intervening <responseparams> work
99: &Apache::response::start_hintresponse($parstack,$safeeval);
100: my $result;
101: if ($target eq 'edit') {
102: $result.=&Apache::edit::tag_start($target,$token);
103: $result.=&Apache::edit::text_arg('Name:','name',$token);
104: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
105: if ($token->[1] eq 'numericalhint') {
106: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
107: &Apache::loncommon::help_open_topic('Physical_Units');
108: $result.=&Apache::edit::text_arg('Format:','format',$token,4).
109: &Apache::loncommon::help_open_topic('Numerical_Response_Format');
110: } elsif ($token->[1] eq 'stringhint') {
111: $result.=&Apache::edit::select_arg('Type:','type',
112: [['cs','Case Sensitive'],['ci','Case Insensitive'],
113: ['mc','Case Insensitive, Any Order']],$token);
114: } elsif ($token->[1] eq 'formulahint') {
115: $result.=&Apache::edit::text_arg('Sample Points:','samples',$token,40);
116: }
117: $result.=&Apache::edit::end_row();
118: $result.=&Apache::edit::start_spanning_row();
119: } elsif ($target eq 'modified') {
120: my $constructtag;
121: if ($token->[1] eq 'numericalhint') {
122: $constructtag=&Apache::edit::get_new_args($token,$parstack,
123: $safeeval,'name',
124: 'answer','unit','format');
125: } elsif ($token->[1] eq 'stringhint') {
126: $constructtag=&Apache::edit::get_new_args($token,$parstack,
127: $safeeval,'name','answer',
128: 'type');
129: } elsif ($token->[1] eq 'formulahint') {
130: $constructtag=&Apache::edit::get_new_args($token,$parstack,
131: $safeeval,'name','answer',
132: 'samples');
133: }
134: if ($constructtag) {
135: $result = &Apache::edit::rebuild_tag($token);
136: $result .= &Apache::edit::handle_insert();
137: }
138: } elsif ($target eq 'web') {
139: &Apache::response::reset_params();
140: }
141: return $result;
142: }
143:
144: sub end_numericalhint {
145: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
146: my $result;
147: if ($target eq 'web') {
148: if (!$Apache::lonxml::default_homework_loaded) {
149: &Apache::lonxml::default_homework_load($safeeval);
150: }
151: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
152: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
153: &Apache::response::setup_params('numericalhint',$safeeval);
154: my $partid=$Apache::inputtags::part;
155: my $id=$Apache::inputtags::response['-1'];
156: #id submissions occured under
157: my $submitid=$Apache::inputtags::response['-2'];
158: my $response = $Apache::lonhomework::history{
159: "resource.$partid.$submitid.submission"};
160: &Apache::lonxml::debug("hintgroup is using $response<br />\n");
161: my $hideunit=&Apache::lonnet::EXT('resource.'.$submitid.'_'.$id.'.turnoffunit');
162: my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
163: $$args_ref{'response'}=$response;
164: #need to get all possible parms
165: foreach my $arg ('type','tol','sig','ans_fmt','unit','calc',
166: 'samples') {
167: $$args_ref{$arg}=
168: &Apache::lonxml::get_param($arg,$parstack,$safeeval);
169: }
170: foreach my $key (keys(%Apache::inputtags::params)) {
171: $$args_ref{$key}=$Apache::inputtags::params{$key};
172: }
173: if (lc($hideunit) eq "yes") { delete($$args_ref{'unit'}); }
174: if ($$tagstack[-1] eq 'formulahint') {
175: $$args_ref{'type'}='fml';
176: } elsif ($$tagstack[-1] eq 'numericalhint') {
177: $$args_ref{'type'}='float';
178: }
179: my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
180: &Apache::lonxml::debug('answer is'.join(':',@answer));
181: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=@answer;
182:
183: ($result,my @msgs) = &Apache::run::run("&caparesponse_check_list()",
184: $safeeval);
185: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
186: &Apache::lonxml::debug("result:$result:$Apache::lonxml::curdepth");
187: my ($awards)=split(/:/,$result);
188: my (@awards) = split(/,/,$awards);
189: my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
190: if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { push (@Apache::hint::which,$name); }
191: $result='';
192: } elsif ($target eq 'meta') {
193: $result=&Apache::response::meta_package_write($token->[1]);
194: } elsif ($target eq 'edit') {
195: $result.='</td></tr>'.&Apache::edit::end_table;
196: }
197: &Apache::response::end_hintresponse();
198: return $result;
199: }
200:
201: sub start_formulahint {
202: return &start_numericalhint(@_);
203: }
204:
205: sub end_formulahint {
206: return &end_numericalhint(@_);
207: }
208:
209: sub start_stringhint {
210: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
211: #do everything in end, so intervening <responseparams> work
212: &Apache::response::start_hintresponse($parstack,$safeeval);
213: my $result;
214: if ($target eq 'edit') {
215: $result.=&Apache::edit::tag_start($target,$token);
216: $result.=&Apache::edit::text_arg('Name:','name',$token);
217: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
218: $result.=&Apache::edit::select_arg('Type:','type',
219: [['cs','Case Sensitive'],['ci','Case Insensitive'],
220: ['mc','Case Insensitive, Any Order'],
221: ['re','Regular Expression']],$token);
222: $result.=&Apache::edit::end_row();
223: $result.=&Apache::edit::start_spanning_row();
224: } elsif ($target eq 'modified') {
225: my $constructtag;
226: $constructtag=&Apache::edit::get_new_args($token,$parstack,
227: $safeeval,'name','answer',
228: 'type');
229: $result = &Apache::edit::rebuild_tag($token);
230: $result .= &Apache::edit::handle_insert();
231: } elsif ($target eq 'web') {
232: &Apache::response::reset_params();
233: }
234: return $result;
235: }
236:
237: sub end_stringhint {
238: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
239: my $result;
240: if ($target eq 'web') {
241: if (!$Apache::lonxml::default_homework_loaded) {
242: &Apache::lonxml::default_homework_load($safeeval);
243: }
244: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
245: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
246: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
247: &Apache::response::setup_params('stringhint',$safeeval);
248: my $partid=$Apache::inputtags::part;
249: my $id=$Apache::inputtags::response['-1'];
250: #id submissions occured under
251: my $submitid=$Apache::inputtags::response['-2'];
252: my $response = $Apache::lonhomework::history{
253: "resource.$partid.$submitid.submission"};
254: &Apache::lonxml::debug("hintgroup is using $response<br />\n");
255: my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
256: $$args_ref{'response'}=$response;
257: my $type=$$args_ref{'type'}=&Apache::lonxml::get_param('type',$parstack,$safeeval);
258: my ($ad,$msg);
259: if ($type eq 're' ) {
260: ${$safeeval->varglob('LONCAPA::response')}=$response;
261: $result = &Apache::run::run('return $LONCAPA::response=~m'.$answer,$safeeval);
262: &Apache::lonxml::debug("current $response");
263: &Apache::lonxml::debug("current $answer");
264: $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
265: } else {
266: foreach my $key (keys(%Apache::inputtags::params)) {
267: $$args_ref{$key}=$Apache::inputtags::params{$key};
268: }
269: &Apache::lonxml::debug('answer is'.$answer);
270: @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=($answer);
271:
272: ($result,my @msgs)=&Apache::run::run("&caparesponse_check_list()",
273: $safeeval);
274: &Apache::lonxml::debug('msgs are'.join(':',@msgs));
275: &Apache::lonxml::debug("result:$result:$Apache::lonxml::curdepth");
276: my ($awards)=split(/:/,$result);
277: my (@awards) = split(/,/,$awards);
278: ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
279: }
280: if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') {
281: push (@Apache::hint::which,$name);
282: }
283: $result='';
284: } elsif ($target eq 'meta') {
285: $result=&Apache::response::meta_package_write($token->[1]);
286: } elsif ($target eq 'edit') {
287: $result.='</td></tr>'.&Apache::edit::end_table;
288: }
289: &Apache::response::end_hintresponse();
290: return $result;
291: }
292:
293: # a part shows if it is on, if no specific parts are on, then default shows
294: sub start_hintpart {
295: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
296:
297: my $show ='0';
298: my $result = '';
299: if ($target eq 'web') {
300: my $on= &Apache::lonxml::get_param('on',$parstack,$safeeval);
301: &Apache::lonxml::debug("hintpart sees $on and ,$#Apache::hint::which");
302: if ( $on eq 'default' && $#Apache::hint::which == '-1') {
303: $show=1;
304: } else {
305: my $which;
306: foreach $which (@Apache::hint::which) { if ($which eq $on) { $show = 1; last } }
307: }
308: if (!$show) {
309: &Apache::lonxml::get_all_text("/hintpart",$parser);
310: }
311: } elsif ($target eq 'grade') {
312: &Apache::lonxml::get_all_text("/hintpart",$parser);
313: } elsif ($target eq 'edit') {
314: $result.= &Apache::edit::tag_start($target,$token);
315: $result.= &Apache::edit::text_arg('On:','on',$token);
316: $result.= &Apache::edit::end_row();
317: $result.= &Apache::edit::start_spanning_row();
318: } elsif ($target eq 'modified') {
319: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
320: $safeeval,'on');
321: if ($constructtag) {
322: $result = &Apache::edit::rebuild_tag($token);
323: $result.=&Apache::edit::handle_insert();
324: }
325: }
326: return $result;
327: }
328:
329: sub end_hintpart {
330: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
331: my $result;
332: if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
333: return $result;
334: }
335:
336: sub start_optionhint {
337: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
338: my $result;
339: &Apache::response::start_hintresponse($parstack,$safeeval);
340: if ($target eq 'edit') {
341: $result.=&Apache::edit::tag_start($target,$token);
342: $result.=&Apache::edit::text_arg('Name:','name',$token);
343: $result.=&Apache::edit::text_arg('Answer:','answer',$token,40);
344: $result.=&Apache::edit::text_arg('Concept:','concept',$token,50);
345: } elsif ($target eq 'modified') {
346: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
347: $safeeval,'name',
348: 'answer','concept');
349: if ($constructtag) {
350: $result = &Apache::edit::rebuild_tag($token);
351: $result .= &Apache::edit::handle_insert();
352: }
353: } elsif ($target eq 'meta') {
354: $result=&Apache::response::meta_package_write('numericalhint');
355: }
356: return $result;
357: }
358:
359: sub end_optionhint {
360: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
361: my $result;
362: if ($target eq 'web') {
363: my ($foilmatch,$conceptmatch)=(-1,-1);
364: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
365: my $partid=$Apache::inputtags::part;
366: #id submissions occured under
367: my $submitid=$Apache::inputtags::response['-2'];
368: my $part_id="$partid.$submitid";
369: my %answer;
370: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
371: if ($answer) {
372: eval('%answer ='.$answer);
373: &Apache::lonhomework::showhash(%answer);
374: my $response = $Apache::lonhomework::history{
375: "resource.$part_id.submission"};
376: my %response=&Apache::lonnet::str2hash($response);
377: &Apache::lonhomework::showhash(%response);
378: foreach my $foil (keys(%answer)) {
379: $foilmatch=1;
380: if ($answer{$foil} ne $response{$foil}) {$foilmatch=0;last;}
381: }
382: }
383: my %concept;
384: my $constr=&Apache::lonxml::get_param('concept',$parstack,$safeeval);
385: if ( $constr ) { eval('%concept ='.$constr); }
386: my $response = $Apache::lonhomework::history{
387: "resource.$part_id.submissiongrading"};
388: my %response=&Apache::lonnet::str2hash($response);
389: foreach my $concept (keys(%concept)) {
390: my $compare;
391: if ($concept{$concept} eq 'correct') {$compare=1}else{$compare=0}
392: $conceptmatch=1;
393: if (ref($Apache::hint::option{"$part_id.concepts"})) {
394: foreach my $foil (@{ $Apache::hint::option{"$part_id.concept.$concept"} }) {
395: &Apache::lonxml::debug("compare -$foil- -$response{$foil}-$compare-");
396: if ( exists($response{$foil}) &&
397: $response{$foil} ne $compare) {$conceptmatch=0;last;}
398: }
399: } else {
400: $conceptmatch=0;
401: }
402: if ($conceptmatch eq '0') { last; }
403: }
404: if ( ($conceptmatch eq '-1' || $conceptmatch eq '1') &&
405: ($foilmatch eq '-1' || $foilmatch eq '1') ) {
406: push(@Apache::hint::which,$name);
407: }
408: } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
409: if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
410: &Apache::response::end_hintresponse();
411: return $result;
412: }
413:
414: sub start_radiobuttonhint {
415: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
416: my $result;
417: &Apache::response::start_hintresponse($parstack,$safeeval);
418: if ($target eq 'edit') {
419: $result.=&Apache::edit::tag_start($target,$token);
420: $result.=&Apache::edit::text_arg('Name:','name',$token);
421: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
422: } elsif ($target eq 'modified') {
423: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
424: $safeeval,'name',
425: 'answer');
426: if ($constructtag) {
427: $result = &Apache::edit::rebuild_tag($token);
428: $result .= &Apache::edit::handle_insert();
429: }
430: } elsif ($target eq 'meta') {
431: $result=&Apache::response::meta_package_write('numericalhint');
432: }
433: return $result;
434: }
435:
436: sub end_radiobuttonhint {
437: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
438: my $result;
439: if ($target eq 'web') {
440: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
441: my @answer;
442: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
443: eval('@answer ='.$answer);
444: my $partid=$Apache::inputtags::part;
445: #id submissions occured under
446: my $submitid=$Apache::inputtags::response['-2'];
447: my $part_id="$partid.$submitid";
448: my $response = $Apache::lonhomework::history{
449: "resource.$part_id.submission"};
450: ($response)=&Apache::lonnet::str2hash($response);
451: &Apache::lonxml::debug("response is $response");
452:
453: if ($answer[0] eq 'foil') {
454: shift(@answer);
455: foreach my $answer (@answer) {
456: if ($response eq $answer) {
457: push (@Apache::hint::which,$name);
458: last;
459: }
460: }
461: } elsif ($answer[0] eq 'concept') {
462: shift(@answer);
463: foreach my $answer (@answer) {
464: if (ref($Apache::hint::radiobutton{"$part_id.concept.".$answer})) {
465: my @names=@{ $Apache::hint::radiobutton{"$part_id.concept.".$answer} };
466: if (grep(/^\Q$response\E$/,@names)) {
467: push(@Apache::hint::which,$name);
468: last;
469: }
470: }
471: }
472: }
473: } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
474: &Apache::response::end_hintresponse();
475: return $result;
476: }
477: 1;
478: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>