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