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