Annotation of loncom/homework/hint.pm, revision 1.45
1.21 albertel 1: # The LearningOnline Network with CAPA
2: # implements the tags that control the hints
3: #
1.45 ! albertel 4: # $Id: hint.pm,v 1.44 2003/06/20 04:18:49 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') {
1.44 albertel 105: $result.=&Apache::edit::select_arg('Type:','type',
106: [['cs','Case Sensitive'],['ci','Case Insensitive'],
107: ['mc','Case Insensitive, Any Order']],$token);
1.39 albertel 108: } elsif ($token->[1] eq 'formulahint') {
109: $result.=&Apache::edit::text_arg('Sample Points:','samples',$token,40);
110: }
111: $result.=&Apache::edit::end_row();
112: $result.=&Apache::edit::start_spanning_row();
113: } elsif ($target eq 'modified') {
114: my $constructtag;
115: if ($token->[1] eq 'numericalhint') {
116: $constructtag=&Apache::edit::get_new_args($token,$parstack,
117: $safeeval,'name',
118: 'answer','unit','format');
119: } elsif ($token->[1] eq 'stringhint') {
120: $constructtag=&Apache::edit::get_new_args($token,$parstack,
121: $safeeval,'name','answer',
122: 'type');
123: } elsif ($token->[1] eq 'formulahint') {
124: $constructtag=&Apache::edit::get_new_args($token,$parstack,
125: $safeeval,'name','answer',
126: 'samples');
127: }
128: if ($constructtag) {
129: $result = &Apache::edit::rebuild_tag($token);
130: $result .= &Apache::edit::handle_insert();
131: }
132: } elsif ($target eq 'web') {
133: &Apache::response::reset_params();
1.28 albertel 134: }
1.39 albertel 135: return $result;
1.3 albertel 136: }
137:
138: sub end_numericalhint {
1.39 albertel 139: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
140: my $result;
141: if ($target eq 'web') {
142: if (!$Apache::lonxml::default_homework_loaded) {
143: &Apache::lonxml::default_homework_load($safeeval);
144: }
145: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
146: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
147: &Apache::response::setup_params('numericalhint');
148: my $partid=$Apache::inputtags::part;
149: my $id=$Apache::inputtags::response['-1'];
150: #id submissions occured under
151: my $submitid=$Apache::inputtags::response['-2'];
152: my $response = $Apache::lonhomework::history{
1.18 albertel 153: "resource.$partid.$submitid.submission"};
1.39 albertel 154: &Apache::lonxml::debug("hintgroup is using $response<br />\n");
155: #build safe space expression
156: my $expression="&caparesponse_check_list('".$response."','".
157: $$parstack[$#$parstack];
158: #need to get all possible parms
159: foreach my $key (keys(%Apache::inputtags::params)) {
160: $expression.= ';my $'. #'
161: $key.'="'.$Apache::inputtags::params{$key}.'"';
1.43 albertel 162: }
163: if ($$tagstack[-1] eq 'formulahint') {
164: $expression.=';my $type="fml";';
165: } elsif ($$tagstack[-1] eq 'numericalhint') {
166: $expression.=';my $type="float";';
1.39 albertel 167: }
168: $expression.="');";
1.45 ! albertel 169: my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
! 170: &Apache::lonxml::debug('answer is'.join(':',@answer));
! 171: @{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=@answer;
! 172:
1.39 albertel 173: $result = &Apache::run::run($expression,$safeeval);
174: &Apache::lonxml::debug("$expression:result:$result:$Apache::lonxml::curdepth");
175: my ($awards) = split /:/ , $result;
176: my ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
177: if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { push (@Apache::hint::which,$name); }
178: $result='';
179: } elsif ($target eq 'meta') {
180: $result=&Apache::response::meta_package_write($token->[1]);
181: } elsif ($target eq 'edit') {
182: $result.='</td></tr>'.&Apache::edit::end_table;
1.18 albertel 183: }
1.39 albertel 184: &Apache::response::end_hintresponse();
185: return $result;
1.28 albertel 186: }
187:
188: sub start_stringhint {
1.35 albertel 189: return &start_numericalhint(@_);
1.28 albertel 190: }
191:
192: sub end_stringhint {
1.35 albertel 193: return &end_numericalhint(@_);
1.28 albertel 194: }
195:
196: sub start_formulahint {
1.35 albertel 197: return &start_numericalhint(@_);
1.28 albertel 198: }
199:
200: sub end_formulahint {
1.35 albertel 201: return end_numericalhint(@_);
1.1 albertel 202: }
203:
1.2 albertel 204: # a part shows if it is on, if no specific parts are on, then default shows
1.1 albertel 205: sub start_hintpart {
1.39 albertel 206: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.15 albertel 207:
1.39 albertel 208: my $show ='0';
209: my $result = '';
210: if ($target eq 'web') {
211: my $on= &Apache::lonxml::get_param('on',$parstack,$safeeval);
212: &Apache::lonxml::debug("hintpart sees $on and ,$#Apache::hint::which");
213: if ( $on eq 'default' && $#Apache::hint::which == '-1') {
214: $show=1;
215: } else {
216: my $which;
217: foreach $which (@Apache::hint::which) { if ($which eq $on) { $show = 1; last } }
218: }
219: if (!$show) {
220: &Apache::lonxml::get_all_text("/hintpart",$parser);
221: }
222: } elsif ($target eq 'grade') {
223: &Apache::lonxml::get_all_text("/hintpart",$parser);
224: } elsif ($target eq 'edit') {
225: $result.= &Apache::edit::tag_start($target,$token);
226: $result.= &Apache::edit::text_arg('On:','on',$token);
227: $result.= &Apache::edit::end_row();
228: $result.= &Apache::edit::start_spanning_row();
229: } elsif ($target eq 'modified') {
230: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
231: $safeeval,'on');
232: if ($constructtag) {
233: $result = &Apache::edit::rebuild_tag($token);
234: $result.=&Apache::edit::handle_insert();
235: }
1.20 albertel 236: }
1.39 albertel 237: return $result;
1.1 albertel 238: }
239:
240: sub end_hintpart {
1.29 albertel 241: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
242: my $result;
243: if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
244: return $result;
245: }
246:
247: sub start_optionhint {
248: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
249: my $result;
1.30 albertel 250: &Apache::response::start_hintresponse($parstack,$safeeval);
251: if ($target eq 'edit') {
252: $result.=&Apache::edit::tag_start($target,$token);
253: $result.=&Apache::edit::text_arg('Name:','name',$token);
254: $result.=&Apache::edit::text_arg('Answer:','answer',$token,40);
255: $result.=&Apache::edit::text_arg('Concept:','concept',$token,50);
256: } elsif ($target eq 'modified') {
257: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
258: $safeeval,'name',
259: 'answer','concept');
260: if ($constructtag) {
261: $result = &Apache::edit::rebuild_tag($token);
262: $result .= &Apache::edit::handle_insert();
263: }
264: } elsif ($target eq 'meta') {
265: $result=&Apache::response::meta_package_write('numericalhint');
266: }
1.29 albertel 267: return $result;
268: }
269:
270: sub end_optionhint {
271: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
272: my $result;
1.30 albertel 273: if ($target eq 'web') {
274: my ($foilmatch,$conceptmatch)=(-1,-1);
275: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
276: my $partid=$Apache::inputtags::part;
277: #id submissions occured under
278: my $submitid=$Apache::inputtags::response['-2'];
279: my $part_id="$partid.$submitid";
280: my %answer;
281: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
282: if ($answer) {
283: eval('%answer ='.$answer);
284: &Apache::lonhomework::showhash(%answer);
285: my $response = $Apache::lonhomework::history{
286: "resource.$part_id.submission"};
287: my %response=&Apache::lonnet::str2hash($response);
288: &Apache::lonhomework::showhash(%response);
289: foreach my $foil (keys(%answer)) {
290: $foilmatch=1;
291: if ($answer{$foil} ne $response{$foil}) {$foilmatch=0;last;}
292: }
293: }
294: my %concept;
295: my $constr=&Apache::lonxml::get_param('concept',$parstack,$safeeval);
296: if ( $constr ) { eval('%concept ='.$constr); }
297: my $response = $Apache::lonhomework::history{
298: "resource.$part_id.submissiongrading"};
299: my %response=&Apache::lonnet::str2hash($response);
300: foreach my $concept (keys(%concept)) {
301: my $compare;
302: if ($concept{$concept} eq 'correct') {$compare=1}else{$compare=0}
303: $conceptmatch=1;
304: if (ref($Apache::hint::option{"$part_id.concepts"})) {
305: foreach my $foil (@{ $Apache::hint::option{"$part_id.concept.$concept"} }) {
306: &Apache::lonxml::debug("compare -$foil- -$response{$foil}-$compare-");
307: if ( exists($response{$foil}) &&
308: $response{$foil} ne $compare) {$conceptmatch=0;last;}
309: }
310: } else {
311: $conceptmatch=0;
312: }
313: if ($conceptmatch eq '0') { last; }
314: }
315: if ( ($conceptmatch eq '-1' || $conceptmatch eq '1') &&
316: ($foilmatch eq '-1' || $foilmatch eq '1') ) {
317: push(@Apache::hint::which,$name);
318: }
319: } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
1.29 albertel 320: if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
1.30 albertel 321: &Apache::response::end_hintresponse();
1.29 albertel 322: return $result;
323: }
324:
325: sub start_radiobuttonhint {
326: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
327: my $result;
328: &Apache::response::start_hintresponse($parstack,$safeeval);
329: if ($target eq 'edit') {
330: $result.=&Apache::edit::tag_start($target,$token);
331: $result.=&Apache::edit::text_arg('Name:','name',$token);
332: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
333: } elsif ($target eq 'modified') {
334: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
335: $safeeval,'name',
336: 'answer');
337: if ($constructtag) {
338: $result = &Apache::edit::rebuild_tag($token);
339: $result .= &Apache::edit::handle_insert();
340: }
341: } elsif ($target eq 'meta') {
342: $result=&Apache::response::meta_package_write('numericalhint');
343: }
344: return $result;
1.1 albertel 345: }
346:
1.31 albertel 347: sub end_radiobuttonhint {
1.29 albertel 348: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
349: my $result;
350: if ($target eq 'web') {
351: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
352: my @answer;
353: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
354: eval('@answer ='.$answer);
355: my $partid=$Apache::inputtags::part;
356: #id submissions occured under
357: my $submitid=$Apache::inputtags::response['-2'];
358: my $part_id="$partid.$submitid";
359: my $response = $Apache::lonhomework::history{
360: "resource.$part_id.submission"};
361: ($response)=&Apache::lonnet::str2hash($response);
1.42 albertel 362: &Apache::lonxml::debug("response is $response");
363:
364: if ($answer[0] eq 'foil') {
365: shift(@answer);
366: foreach my $answer (@answer) {
367: if ($response eq $answer) {
368: push (@Apache::hint::which,$name);
369: last;
370: }
371: }
1.29 albertel 372: } elsif ($answer[0] eq 'concept') {
1.42 albertel 373: shift(@answer);
374: foreach my $answer (@answer) {
375: if (ref($Apache::hint::radiobutton{"$part_id.concept.".$answer})) {
376: my @names=@{ $Apache::hint::radiobutton{"$part_id.concept.".$answer} };
377: if (grep(/^\Q$response\E$/,@names)) {
378: push(@Apache::hint::which,$name);
379: last;
380: }
1.29 albertel 381: }
382: }
383: }
384: } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
385: &Apache::response::end_hintresponse();
386: return $result;
387: }
1.1 albertel 388: 1;
389: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>