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