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