1: # The LearningOnline Network with CAPA
2: # implements the tags that control the hints
3: #
4: # $Id: hint.pm,v 1.39 2003/04/08 02:21:13 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 ( $hinttries eq '') { $hinttries = 1; }
52: &Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
53: if ($Apache::lonhomework::type ne 'exam') {
54: if ( $numtries < $hinttries ) {
55: $skiptoend='1';
56: } else {
57: $result='<table bgcolor="#dddddd"><tr><td>';
58: }
59: } else {
60: &Apache::lonxml::startredirection;
61: }
62: if ($skiptoend) {
63: &Apache::lonxml::get_all_text("/hintgroup",$parser);
64: }
65: } elsif ($target eq 'tex') {
66: $result .= '\keephidden{';
67: }
68: @Apache::hint::which=();
69: return $result;
70: }
71:
72: sub end_hintgroup {
73: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
74: my $result;
75:
76: if ($target eq 'web') {
77: my $id=$Apache::inputtags::part;
78: my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
79: if ( $numtries eq '') { $numtries = 0; }
80: my $hinttries=&Apache::lonnet::EXT("resource.$id.hinttries");
81: if ( $hinttries eq '') { $hinttries = 1; }
82: &Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
83: if ($Apache::lonhomework::type ne 'exam') {
84: if ( $numtries >= $hinttries ) {
85: $result='</td></tr></table>';
86: }
87: } else {
88: &Apache::lonxml::endredirection;
89: }
90: } elsif ($target eq 'edit') {
91: $result.=&Apache::edit::end_table();
92: } elsif ($target eq 'tex') {
93: $result .= '}';
94: }
95: @Apache::hint::which=();
96: return $result;
97: }
98:
99: sub start_numericalhint {
100: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
101: #do everything in end, so intervening <responseparams> work
102: &Apache::response::start_hintresponse($parstack,$safeeval);
103: my $result;
104: if ($target eq 'edit') {
105: $result.=&Apache::edit::tag_start($target,$token);
106: $result.=&Apache::edit::text_arg('Name:','name',$token);
107: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
108: if ($token->[1] eq 'numericalhint') {
109: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
110: &Apache::loncommon::help_open_topic('Physical_Units');
111: $result.=&Apache::edit::text_arg('Format:','format',$token,4).
112: &Apache::loncommon::help_open_topic('Numerical_Response_Format');
113: } elsif ($token->[1] eq 'stringhint') {
114: $result.=&Apache::edit::select_arg('Type:','type',['cs','ci','mc'],$token);
115: } elsif ($token->[1] eq 'formulahint') {
116: $result.=&Apache::edit::text_arg('Sample Points:','samples',$token,40);
117: }
118: $result.=&Apache::edit::end_row();
119: $result.=&Apache::edit::start_spanning_row();
120: } elsif ($target eq 'modified') {
121: my $constructtag;
122: if ($token->[1] eq 'numericalhint') {
123: $constructtag=&Apache::edit::get_new_args($token,$parstack,
124: $safeeval,'name',
125: 'answer','unit','format');
126: } elsif ($token->[1] eq 'stringhint') {
127: $constructtag=&Apache::edit::get_new_args($token,$parstack,
128: $safeeval,'name','answer',
129: 'type');
130: } elsif ($token->[1] eq 'formulahint') {
131: $constructtag=&Apache::edit::get_new_args($token,$parstack,
132: $safeeval,'name','answer',
133: 'samples');
134: }
135: if ($constructtag) {
136: $result = &Apache::edit::rebuild_tag($token);
137: $result .= &Apache::edit::handle_insert();
138: }
139: } elsif ($target eq 'web') {
140: &Apache::response::reset_params();
141: }
142: return $result;
143: }
144:
145: sub end_numericalhint {
146: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
147: my $result;
148: if ($target eq 'web') {
149: if (!$Apache::lonxml::default_homework_loaded) {
150: &Apache::lonxml::default_homework_load($safeeval);
151: }
152: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
153: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
154: &Apache::response::setup_params('numericalhint');
155: my $partid=$Apache::inputtags::part;
156: my $id=$Apache::inputtags::response['-1'];
157: #id submissions occured under
158: my $submitid=$Apache::inputtags::response['-2'];
159: my $response = $Apache::lonhomework::history{
160: "resource.$partid.$submitid.submission"};
161: &Apache::lonxml::debug("hintgroup is using $response<br />\n");
162: #build safe space expression
163: my $expression="&caparesponse_check_list('".$response."','".
164: $$parstack[$#$parstack];
165: #need to get all possible parms
166: foreach my $key (keys(%Apache::inputtags::params)) {
167: $expression.= ';my $'. #'
168: $key.'="'.$Apache::inputtags::params{$key}.'"';
169: }
170: $expression.="');";
171: $result = &Apache::run::run($expression,$safeeval);
172: &Apache::lonxml::debug("$expression:result:$result:$Apache::lonxml::curdepth");
173: my ($awards) = split /:/ , $result;
174: my ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
175: if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { push (@Apache::hint::which,$name); }
176: $result='';
177: } elsif ($target eq 'meta') {
178: $result=&Apache::response::meta_package_write($token->[1]);
179: } elsif ($target eq 'edit') {
180: $result.='</td></tr>'.&Apache::edit::end_table;
181: }
182: &Apache::response::end_hintresponse();
183: return $result;
184: }
185:
186: sub start_stringhint {
187: return &start_numericalhint(@_);
188: }
189:
190: sub end_stringhint {
191: return &end_numericalhint(@_);
192: }
193:
194: sub start_formulahint {
195: return &start_numericalhint(@_);
196: }
197:
198: sub end_formulahint {
199: return end_numericalhint(@_);
200: }
201:
202: # a part shows if it is on, if no specific parts are on, then default shows
203: sub start_hintpart {
204: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
205:
206: my $show ='0';
207: my $result = '';
208: if ($target eq 'web') {
209: my $on= &Apache::lonxml::get_param('on',$parstack,$safeeval);
210: &Apache::lonxml::debug("hintpart sees $on and ,$#Apache::hint::which");
211: if ( $on eq 'default' && $#Apache::hint::which == '-1') {
212: $show=1;
213: } else {
214: my $which;
215: foreach $which (@Apache::hint::which) { if ($which eq $on) { $show = 1; last } }
216: }
217: if (!$show) {
218: &Apache::lonxml::get_all_text("/hintpart",$parser);
219: }
220: } elsif ($target eq 'grade') {
221: &Apache::lonxml::get_all_text("/hintpart",$parser);
222: } elsif ($target eq 'edit') {
223: $result.= &Apache::edit::tag_start($target,$token);
224: $result.= &Apache::edit::text_arg('On:','on',$token);
225: $result.= &Apache::edit::end_row();
226: $result.= &Apache::edit::start_spanning_row();
227: } elsif ($target eq 'modified') {
228: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
229: $safeeval,'on');
230: if ($constructtag) {
231: $result = &Apache::edit::rebuild_tag($token);
232: $result.=&Apache::edit::handle_insert();
233: }
234: }
235: return $result;
236: }
237:
238: sub end_hintpart {
239: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
240: my $result;
241: if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
242: return $result;
243: }
244:
245: sub start_optionhint {
246: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
247: my $result;
248: &Apache::response::start_hintresponse($parstack,$safeeval);
249: if ($target eq 'edit') {
250: $result.=&Apache::edit::tag_start($target,$token);
251: $result.=&Apache::edit::text_arg('Name:','name',$token);
252: $result.=&Apache::edit::text_arg('Answer:','answer',$token,40);
253: $result.=&Apache::edit::text_arg('Concept:','concept',$token,50);
254: } elsif ($target eq 'modified') {
255: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
256: $safeeval,'name',
257: 'answer','concept');
258: if ($constructtag) {
259: $result = &Apache::edit::rebuild_tag($token);
260: $result .= &Apache::edit::handle_insert();
261: }
262: } elsif ($target eq 'meta') {
263: $result=&Apache::response::meta_package_write('numericalhint');
264: }
265: return $result;
266: }
267:
268: sub end_optionhint {
269: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
270: my $result;
271: if ($target eq 'web') {
272: my ($foilmatch,$conceptmatch)=(-1,-1);
273: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
274: my $partid=$Apache::inputtags::part;
275: #id submissions occured under
276: my $submitid=$Apache::inputtags::response['-2'];
277: my $part_id="$partid.$submitid";
278: my %answer;
279: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
280: if ($answer) {
281: eval('%answer ='.$answer);
282: &Apache::lonhomework::showhash(%answer);
283: my $response = $Apache::lonhomework::history{
284: "resource.$part_id.submission"};
285: my %response=&Apache::lonnet::str2hash($response);
286: &Apache::lonhomework::showhash(%response);
287: foreach my $foil (keys(%answer)) {
288: $foilmatch=1;
289: if ($answer{$foil} ne $response{$foil}) {$foilmatch=0;last;}
290: }
291: }
292: my %concept;
293: my $constr=&Apache::lonxml::get_param('concept',$parstack,$safeeval);
294: if ( $constr ) { eval('%concept ='.$constr); }
295: my $response = $Apache::lonhomework::history{
296: "resource.$part_id.submissiongrading"};
297: my %response=&Apache::lonnet::str2hash($response);
298: foreach my $concept (keys(%concept)) {
299: my $compare;
300: if ($concept{$concept} eq 'correct') {$compare=1}else{$compare=0}
301: $conceptmatch=1;
302: if (ref($Apache::hint::option{"$part_id.concepts"})) {
303: foreach my $foil (@{ $Apache::hint::option{"$part_id.concept.$concept"} }) {
304: &Apache::lonxml::debug("compare -$foil- -$response{$foil}-$compare-");
305: if ( exists($response{$foil}) &&
306: $response{$foil} ne $compare) {$conceptmatch=0;last;}
307: }
308: } else {
309: $conceptmatch=0;
310: }
311: if ($conceptmatch eq '0') { last; }
312: }
313: if ( ($conceptmatch eq '-1' || $conceptmatch eq '1') &&
314: ($foilmatch eq '-1' || $foilmatch eq '1') ) {
315: push(@Apache::hint::which,$name);
316: }
317: } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
318: if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
319: &Apache::response::end_hintresponse();
320: return $result;
321: }
322:
323: sub start_radiobuttonhint {
324: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
325: my $result;
326: &Apache::response::start_hintresponse($parstack,$safeeval);
327: if ($target eq 'edit') {
328: $result.=&Apache::edit::tag_start($target,$token);
329: $result.=&Apache::edit::text_arg('Name:','name',$token);
330: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
331: } elsif ($target eq 'modified') {
332: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
333: $safeeval,'name',
334: 'answer');
335: if ($constructtag) {
336: $result = &Apache::edit::rebuild_tag($token);
337: $result .= &Apache::edit::handle_insert();
338: }
339: } elsif ($target eq 'meta') {
340: $result=&Apache::response::meta_package_write('numericalhint');
341: }
342: return $result;
343: }
344:
345: sub end_radiobuttonhint {
346: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
347: my $result;
348: if ($target eq 'web') {
349: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
350: my @answer;
351: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
352: eval('@answer ='.$answer);
353: my $partid=$Apache::inputtags::part;
354: #id submissions occured under
355: my $submitid=$Apache::inputtags::response['-2'];
356: my $part_id="$partid.$submitid";
357: my $response = $Apache::lonhomework::history{
358: "resource.$part_id.submission"};
359: ($response)=&Apache::lonnet::str2hash($response);
360: if ($answer[0] eq 'foil' && $response eq $answer[1]) {
361: push (@Apache::hint::which,$name);
362: } elsif ($answer[0] eq 'concept') {
363: if (ref($Apache::hint::radiobutton{"$part_id.concept.".$answer[1]})) {
364: my @names=@{ $Apache::hint::radiobutton{"$part_id.concept.".$answer[1]} };
365: if (grep(/^\Q$response\E$/,@names)) {
366: push(@Apache::hint::which,$name);
367: }
368: }
369: }
370: } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
371: &Apache::response::end_hintresponse();
372: return $result;
373: }
374: 1;
375: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>