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