File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.50: download - view: text, annotated - select for diffs
Sat Mar 13 00:36:11 2004 UTC (20 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#32, mainly but alot of assocaited bugs
- caparesponse messages are now printed,
   It now reports what it thought the unit was, how many sig figs it read (and how many were expected)

    1: # The LearningOnline Network with CAPA 
    2: # implements the tags that control the hints
    3: #
    4: # $Id: hint.pm,v 1.50 2004/03/13 00:36:11 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: 
   35: BEGIN {
   36:     &Apache::lonxml::register('Apache::hinttags',('hintgroup','hintpart','numericalhint','stringhint','formulahint','optionhint','radiobuttonhint'));
   37: }
   38: 
   39: 
   40: @Apache::hint::which=();
   41: sub start_hintgroup {
   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");
   51: 	if ($ENV{'request.state'} eq 'construct' &&
   52: 	    defined($Apache::inputtags::params{'hinttries'})) {
   53: 	    $hinttries=$Apache::inputtags::params{'hinttries'};
   54: 	}
   55: 
   56: 	if ( $hinttries eq '') { $hinttries = 1; }
   57: 	&Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
   58: 	my $gradestatus=$Apache::lonhomework::history{"resource.$id.solved"};
   59: 	if ( $numtries < $hinttries || $gradestatus =~ /^correct/) {
   60: 	    &Apache::lonxml::get_all_text("/hintgroup",$parser);
   61: 	}
   62: 	&Apache::lonxml::startredirection;
   63:     } elsif ($target eq 'tex') {
   64: 	$result .= '\keephidden{';
   65:     }
   66:     @Apache::hint::which=();
   67:     return $result;
   68: }
   69: 
   70: sub end_hintgroup {
   71:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   72:     my $result;
   73: 
   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:");
   81: 	my $hinttext=&Apache::lonxml::endredirection;
   82: 	if ($Apache::lonhomework::type ne 'exam' &&
   83: 	    $numtries >= $hinttries && $hinttext =~/\S/) {
   84: 	    $result='<table bgcolor="#dddddd"><tr><td>'.
   85: 		$hinttext.'</td></tr></table>';
   86: 	}
   87:     } elsif ($target eq 'edit') {
   88: 	$result.=&Apache::edit::end_table();
   89:     } elsif ($target eq 'tex') {
   90: 	$result .= '}';
   91:     }
   92:     @Apache::hint::which=();
   93:     return $result;
   94: }
   95: 
   96: sub start_numericalhint {
   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') {
  111: 	    $result.=&Apache::edit::select_arg('Type:','type',
  112: 			    [['cs','Case Sensitive'],['ci','Case Insensitive'],
  113: 			     ['mc','Case Insensitive, Any Order']],$token);
  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();
  140:     }
  141:     return $result;
  142: }
  143: 
  144: sub end_numericalhint {
  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);
  153: 	&Apache::response::setup_params('numericalhint',$safeeval);
  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{
  159: 			    "resource.$partid.$submitid.submission"};
  160: 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
  161: 	#build safe space expression
  162: 	my $expression="&caparesponse_check_list('".$response."','".
  163: 	    $$parstack[$#$parstack];
  164: 	#need to get all possible parms
  165: 	foreach my $key (keys(%Apache::inputtags::params)) {
  166: 	    $expression.= ';my $__LC__'. #'
  167: 		$key.'="'.$Apache::inputtags::params{$key}.'"';
  168: 	}
  169: 	if ($$tagstack[-1] eq 'formulahint') {
  170: 	    $expression.=';my $__LC__type="fml";';
  171: 	} elsif ($$tagstack[-1] eq 'numericalhint') {
  172: 	    $expression.=';my $__LC__type="float";';
  173: 	}
  174: 	$expression.="');";
  175: 	my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  176: 	&Apache::lonxml::debug('answer is'.join(':',@answer));
  177: 	@{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=@answer;
  178: 		
  179: 	($result,my @msgs) = &Apache::run::run($expression,$safeeval);
  180: 	&Apache::lonxml::debug('msgs are'.join(':',@msgs));
  181: 	&Apache::lonxml::debug("$expression:result:$result:$Apache::lonxml::curdepth");
  182: 	my ($awards)=split(/:/,$result);
  183: 	my (@awards) = split(/,/,$awards);
  184: 	my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
  185: 	if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { push (@Apache::hint::which,$name); }
  186: 	$result='';
  187:     } elsif ($target eq 'meta') {
  188: 	$result=&Apache::response::meta_package_write($token->[1]);
  189:     } elsif ($target eq 'edit') {
  190: 	$result.='</td></tr>'.&Apache::edit::end_table;
  191:     }
  192:     &Apache::response::end_hintresponse();
  193:     return $result;
  194: }
  195: 
  196: sub start_stringhint {
  197:     return &start_numericalhint(@_);
  198: }
  199: 
  200: sub end_stringhint {
  201:     return &end_numericalhint(@_);
  202: }
  203: 
  204: sub start_formulahint {
  205:     return &start_numericalhint(@_);
  206: }
  207: 
  208: sub end_formulahint {
  209:     return end_numericalhint(@_);
  210: }
  211: 
  212: # a part shows if it is on, if no specific parts are on, then default shows
  213: sub start_hintpart {
  214:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  215: 
  216:     my $show ='0';
  217:     my $result = '';
  218:     if ($target eq 'web') {
  219: 	my $on= &Apache::lonxml::get_param('on',$parstack,$safeeval);
  220: 	&Apache::lonxml::debug("hintpart sees $on and ,$#Apache::hint::which");
  221: 	if ( $on eq 'default' && $#Apache::hint::which == '-1') {
  222: 	    $show=1;
  223: 	} else {
  224: 	    my $which;
  225: 	    foreach $which (@Apache::hint::which) { if ($which eq $on) { $show = 1; last } }
  226: 	}
  227: 	if (!$show) {
  228: 	    &Apache::lonxml::get_all_text("/hintpart",$parser);
  229: 	}
  230:     } elsif ($target eq 'grade') {
  231: 	&Apache::lonxml::get_all_text("/hintpart",$parser);
  232:     } elsif ($target eq 'edit') {
  233: 	$result.= &Apache::edit::tag_start($target,$token);
  234: 	$result.= &Apache::edit::text_arg('On:','on',$token);
  235: 	$result.= &Apache::edit::end_row();
  236: 	$result.= &Apache::edit::start_spanning_row();
  237:     } elsif ($target eq 'modified') {
  238: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  239: 						     $safeeval,'on');
  240: 	if ($constructtag) {
  241: 	    $result = &Apache::edit::rebuild_tag($token);
  242: 	    $result.=&Apache::edit::handle_insert();
  243: 	}
  244:     }
  245:     return $result;
  246: }
  247: 
  248: sub end_hintpart {
  249:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  250:     my $result;
  251:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  252:     return $result;
  253: }
  254: 
  255: sub start_optionhint {
  256:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  257:     my $result;
  258:     &Apache::response::start_hintresponse($parstack,$safeeval);
  259:     if ($target eq 'edit') {
  260: 	$result.=&Apache::edit::tag_start($target,$token);
  261: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  262: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token,40);
  263: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,50);
  264:     } elsif ($target eq 'modified') {
  265: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  266: 						     $safeeval,'name',
  267: 						     'answer','concept');
  268: 	if ($constructtag) {
  269: 	    $result  = &Apache::edit::rebuild_tag($token);
  270: 	    $result .= &Apache::edit::handle_insert();
  271: 	}
  272:     } elsif ($target eq 'meta') {
  273: 	$result=&Apache::response::meta_package_write('numericalhint');
  274:     }
  275:     return $result;
  276: }
  277: 
  278: sub end_optionhint {
  279:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  280:     my $result;
  281:     if ($target eq 'web') {
  282: 	my ($foilmatch,$conceptmatch)=(-1,-1);
  283: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  284: 	my $partid=$Apache::inputtags::part;
  285: 	#id submissions occured under
  286: 	my $submitid=$Apache::inputtags::response['-2'];
  287: 	my $part_id="$partid.$submitid";
  288: 	my %answer;
  289: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  290: 	if ($answer) {
  291: 	    eval('%answer ='.$answer);
  292: 	    &Apache::lonhomework::showhash(%answer);
  293: 	    my $response = $Apache::lonhomework::history{
  294: 				            "resource.$part_id.submission"};
  295: 	    my %response=&Apache::lonnet::str2hash($response);
  296: 	    &Apache::lonhomework::showhash(%response);
  297: 	    foreach my $foil (keys(%answer)) {
  298: 		$foilmatch=1;
  299: 		if ($answer{$foil} ne $response{$foil}) {$foilmatch=0;last;}
  300: 	    }
  301: 	}
  302: 	my %concept;
  303: 	my $constr=&Apache::lonxml::get_param('concept',$parstack,$safeeval);
  304: 	if ( $constr ) { eval('%concept ='.$constr);	}
  305: 	my $response = $Apache::lonhomework::history{
  306: 					"resource.$part_id.submissiongrading"};
  307: 	my %response=&Apache::lonnet::str2hash($response);
  308: 	foreach my $concept (keys(%concept)) {
  309: 	    my $compare;
  310: 	    if ($concept{$concept} eq 'correct') {$compare=1}else{$compare=0}
  311: 	    $conceptmatch=1;
  312: 	    if (ref($Apache::hint::option{"$part_id.concepts"})) {
  313: 		foreach my $foil (@{ $Apache::hint::option{"$part_id.concept.$concept"} }) {
  314: 		    &Apache::lonxml::debug("compare -$foil- -$response{$foil}-$compare-");
  315: 		    if ( exists($response{$foil}) && 
  316: 			 $response{$foil} ne $compare) {$conceptmatch=0;last;}
  317: 		}
  318: 	    } else {
  319: 		$conceptmatch=0;
  320: 	    }
  321: 	    if ($conceptmatch eq '0') { last; }
  322: 	}
  323: 	if ( ($conceptmatch eq '-1' || $conceptmatch eq '1') &&
  324: 	     ($foilmatch    eq '-1' || $foilmatch    eq '1') ) {
  325: 	    push(@Apache::hint::which,$name);
  326: 	}
  327:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  328:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  329:     &Apache::response::end_hintresponse();
  330:     return $result;
  331: }
  332: 
  333: sub start_radiobuttonhint {
  334:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  335:     my $result;
  336:     &Apache::response::start_hintresponse($parstack,$safeeval);
  337:     if ($target eq 'edit') {
  338: 	$result.=&Apache::edit::tag_start($target,$token);
  339: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  340: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  341:     } elsif ($target eq 'modified') {
  342: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  343: 						     $safeeval,'name',
  344: 						     'answer');
  345: 	if ($constructtag) {
  346: 	    $result  = &Apache::edit::rebuild_tag($token);
  347: 	    $result .= &Apache::edit::handle_insert();
  348: 	}
  349:     } elsif ($target eq 'meta') {
  350: 	$result=&Apache::response::meta_package_write('numericalhint');
  351:     }
  352:     return $result;
  353: }
  354: 
  355: sub end_radiobuttonhint {
  356:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  357:     my $result;
  358:     if ($target eq 'web') {
  359: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  360: 	my @answer;
  361: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  362: 	eval('@answer ='.$answer);
  363: 	my $partid=$Apache::inputtags::part;
  364: 	#id submissions occured under
  365: 	my $submitid=$Apache::inputtags::response['-2'];
  366: 	my $part_id="$partid.$submitid";
  367: 	my $response = $Apache::lonhomework::history{
  368: 			    "resource.$part_id.submission"};
  369: 	($response)=&Apache::lonnet::str2hash($response);
  370: 	&Apache::lonxml::debug("response is $response");
  371: 	
  372: 	if ($answer[0] eq 'foil') {
  373: 	    shift(@answer);
  374: 	    foreach my $answer (@answer) {
  375: 		if ($response eq $answer) {
  376: 		    push (@Apache::hint::which,$name);
  377: 		    last;
  378: 		}
  379: 	    }
  380: 	} elsif ($answer[0] eq 'concept') {
  381: 	    shift(@answer);
  382: 	    foreach my $answer (@answer) {
  383: 		if (ref($Apache::hint::radiobutton{"$part_id.concept.".$answer})) {
  384: 		    my @names=@{ $Apache::hint::radiobutton{"$part_id.concept.".$answer} };
  385: 		    if (grep(/^\Q$response\E$/,@names)) {
  386: 			push(@Apache::hint::which,$name);
  387: 			last;
  388: 		    }
  389: 		}
  390: 	    }
  391: 	}
  392:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  393:     &Apache::response::end_hintresponse();
  394:     return $result;
  395: }
  396: 1;
  397: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>