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