File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.42: download - view: text, annotated - select for diffs
Fri May 9 20:44:54 2003 UTC (21 years ago) by albertel
Branches: MAIN
CVS tags: version_0_99_2, version_0_99_1, version_0_99_0, conference_2003, HEAD
- <radiobuttonhint> supports multiple foils/concepts in single hint condition

    1: # The LearningOnline Network with CAPA 
    2: # implements the tags that control the hints
    3: #
    4: # $Id: hint.pm,v 1.42 2003/05/09 20:44:54 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 ( $hinttries eq '') { $hinttries = 1; }
   52: 	&Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
   53: 	if ( $numtries < $hinttries ) {
   54: 	    &Apache::lonxml::get_all_text("/hintgroup",$parser);
   55: 	}
   56: 	&Apache::lonxml::startredirection;
   57:     } elsif ($target eq 'tex') {
   58: 	$result .= '\keephidden{';
   59:     }
   60:     @Apache::hint::which=();
   61:     return $result;
   62: }
   63: 
   64: sub end_hintgroup {
   65:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   66:     my $result;
   67: 
   68:     if ($target eq 'web') {
   69: 	my $id=$Apache::inputtags::part;
   70: 	my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
   71: 	if ( $numtries eq '') { $numtries = 0; }
   72: 	my $hinttries=&Apache::lonnet::EXT("resource.$id.hinttries");
   73: 	if ( $hinttries eq '') { $hinttries = 1; }
   74: 	&Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
   75: 	my $hinttext=&Apache::lonxml::endredirection;
   76: 	if ($Apache::lonhomework::type ne 'exam' &&
   77: 	    $numtries >= $hinttries && $hinttext =~/\S/) {
   78: 	    $result='<table bgcolor="#dddddd"><tr><td>'.
   79: 		$hinttext.'</td></tr></table>';
   80: 	}
   81:     } elsif ($target eq 'edit') {
   82: 	$result.=&Apache::edit::end_table();
   83:     } elsif ($target eq 'tex') {
   84: 	$result .= '}';
   85:     }
   86:     @Apache::hint::which=();
   87:     return $result;
   88: }
   89: 
   90: sub start_numericalhint {
   91:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   92:     #do everything in end, so intervening <responseparams> work
   93:     &Apache::response::start_hintresponse($parstack,$safeeval);
   94:     my $result;
   95:     if ($target eq 'edit') {
   96: 	$result.=&Apache::edit::tag_start($target,$token);
   97: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
   98: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
   99: 	if ($token->[1] eq 'numericalhint') {
  100: 	    $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
  101: 		&Apache::loncommon::help_open_topic('Physical_Units');
  102: 	    $result.=&Apache::edit::text_arg('Format:','format',$token,4).
  103: 		&Apache::loncommon::help_open_topic('Numerical_Response_Format');
  104: 	} elsif ($token->[1] eq 'stringhint') {
  105: 	    $result.=&Apache::edit::select_arg('Type:','type',['cs','ci','mc'],$token);
  106: 	} elsif ($token->[1] eq 'formulahint') {
  107: 	    $result.=&Apache::edit::text_arg('Sample Points:','samples',$token,40);
  108: 	}
  109: 	$result.=&Apache::edit::end_row();
  110: 	$result.=&Apache::edit::start_spanning_row();
  111:     } elsif ($target eq 'modified') {
  112: 	my $constructtag;
  113: 	if ($token->[1] eq 'numericalhint') {
  114: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
  115: 						     $safeeval,'name',
  116: 						     'answer','unit','format');
  117: 	} elsif ($token->[1] eq 'stringhint') {
  118: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
  119: 						     $safeeval,'name','answer',
  120: 						     'type');
  121: 	} elsif ($token->[1] eq 'formulahint') {
  122: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
  123: 						     $safeeval,'name','answer',
  124: 						     'samples');
  125: 	}
  126: 	if ($constructtag) {
  127: 	    $result  = &Apache::edit::rebuild_tag($token);
  128: 	    $result .= &Apache::edit::handle_insert();
  129: 	}
  130:     } elsif ($target eq 'web') {
  131: 	&Apache::response::reset_params();
  132:     }
  133:     return $result;
  134: }
  135: 
  136: sub end_numericalhint {
  137:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  138:     my $result;
  139:     if ($target eq 'web') {
  140: 	if (!$Apache::lonxml::default_homework_loaded) {
  141: 	    &Apache::lonxml::default_homework_load($safeeval);
  142: 	}
  143: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  144: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  145: 	&Apache::response::setup_params('numericalhint');
  146: 	my $partid=$Apache::inputtags::part;
  147: 	my $id=$Apache::inputtags::response['-1'];
  148: 	#id submissions occured under
  149: 	my $submitid=$Apache::inputtags::response['-2'];
  150: 	my $response = $Apache::lonhomework::history{
  151: 			    "resource.$partid.$submitid.submission"};
  152: 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
  153: 	#build safe space expression
  154: 	my $expression="&caparesponse_check_list('".$response."','".
  155: 	    $$parstack[$#$parstack];
  156: 	#need to get all possible parms
  157: 	foreach my $key (keys(%Apache::inputtags::params)) {
  158: 	    $expression.= ';my $'. #'
  159: 		$key.'="'.$Apache::inputtags::params{$key}.'"';
  160: 	}
  161: 	$expression.="');";
  162: 	$result = &Apache::run::run($expression,$safeeval);
  163: 	&Apache::lonxml::debug("$expression:result:$result:$Apache::lonxml::curdepth");
  164: 	my ($awards) = split /:/ , $result;
  165: 	my ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
  166: 	if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { push (@Apache::hint::which,$name); }
  167: 	$result='';
  168:     } elsif ($target eq 'meta') {
  169: 	$result=&Apache::response::meta_package_write($token->[1]);
  170:     } elsif ($target eq 'edit') {
  171: 	$result.='</td></tr>'.&Apache::edit::end_table;
  172:     }
  173:     &Apache::response::end_hintresponse();
  174:     return $result;
  175: }
  176: 
  177: sub start_stringhint {
  178:     return &start_numericalhint(@_);
  179: }
  180: 
  181: sub end_stringhint {
  182:     return &end_numericalhint(@_);
  183: }
  184: 
  185: sub start_formulahint {
  186:     return &start_numericalhint(@_);
  187: }
  188: 
  189: sub end_formulahint {
  190:     return end_numericalhint(@_);
  191: }
  192: 
  193: # a part shows if it is on, if no specific parts are on, then default shows
  194: sub start_hintpart {
  195:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  196: 
  197:     my $show ='0';
  198:     my $result = '';
  199:     if ($target eq 'web') {
  200: 	my $on= &Apache::lonxml::get_param('on',$parstack,$safeeval);
  201: 	&Apache::lonxml::debug("hintpart sees $on and ,$#Apache::hint::which");
  202: 	if ( $on eq 'default' && $#Apache::hint::which == '-1') {
  203: 	    $show=1;
  204: 	} else {
  205: 	    my $which;
  206: 	    foreach $which (@Apache::hint::which) { if ($which eq $on) { $show = 1; last } }
  207: 	}
  208: 	if (!$show) {
  209: 	    &Apache::lonxml::get_all_text("/hintpart",$parser);
  210: 	}
  211:     } elsif ($target eq 'grade') {
  212: 	&Apache::lonxml::get_all_text("/hintpart",$parser);
  213:     } elsif ($target eq 'edit') {
  214: 	$result.= &Apache::edit::tag_start($target,$token);
  215: 	$result.= &Apache::edit::text_arg('On:','on',$token);
  216: 	$result.= &Apache::edit::end_row();
  217: 	$result.= &Apache::edit::start_spanning_row();
  218:     } elsif ($target eq 'modified') {
  219: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  220: 						     $safeeval,'on');
  221: 	if ($constructtag) {
  222: 	    $result = &Apache::edit::rebuild_tag($token);
  223: 	    $result.=&Apache::edit::handle_insert();
  224: 	}
  225:     }
  226:     return $result;
  227: }
  228: 
  229: sub end_hintpart {
  230:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  231:     my $result;
  232:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  233:     return $result;
  234: }
  235: 
  236: sub start_optionhint {
  237:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  238:     my $result;
  239:     &Apache::response::start_hintresponse($parstack,$safeeval);
  240:     if ($target eq 'edit') {
  241: 	$result.=&Apache::edit::tag_start($target,$token);
  242: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  243: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token,40);
  244: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,50);
  245:     } elsif ($target eq 'modified') {
  246: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  247: 						     $safeeval,'name',
  248: 						     'answer','concept');
  249: 	if ($constructtag) {
  250: 	    $result  = &Apache::edit::rebuild_tag($token);
  251: 	    $result .= &Apache::edit::handle_insert();
  252: 	}
  253:     } elsif ($target eq 'meta') {
  254: 	$result=&Apache::response::meta_package_write('numericalhint');
  255:     }
  256:     return $result;
  257: }
  258: 
  259: sub end_optionhint {
  260:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  261:     my $result;
  262:     if ($target eq 'web') {
  263: 	my ($foilmatch,$conceptmatch)=(-1,-1);
  264: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  265: 	my $partid=$Apache::inputtags::part;
  266: 	#id submissions occured under
  267: 	my $submitid=$Apache::inputtags::response['-2'];
  268: 	my $part_id="$partid.$submitid";
  269: 	my %answer;
  270: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  271: 	if ($answer) {
  272: 	    eval('%answer ='.$answer);
  273: 	    &Apache::lonhomework::showhash(%answer);
  274: 	    my $response = $Apache::lonhomework::history{
  275: 				            "resource.$part_id.submission"};
  276: 	    my %response=&Apache::lonnet::str2hash($response);
  277: 	    &Apache::lonhomework::showhash(%response);
  278: 	    foreach my $foil (keys(%answer)) {
  279: 		$foilmatch=1;
  280: 		if ($answer{$foil} ne $response{$foil}) {$foilmatch=0;last;}
  281: 	    }
  282: 	}
  283: 	my %concept;
  284: 	my $constr=&Apache::lonxml::get_param('concept',$parstack,$safeeval);
  285: 	if ( $constr ) { eval('%concept ='.$constr);	}
  286: 	my $response = $Apache::lonhomework::history{
  287: 					"resource.$part_id.submissiongrading"};
  288: 	my %response=&Apache::lonnet::str2hash($response);
  289: 	foreach my $concept (keys(%concept)) {
  290: 	    my $compare;
  291: 	    if ($concept{$concept} eq 'correct') {$compare=1}else{$compare=0}
  292: 	    $conceptmatch=1;
  293: 	    if (ref($Apache::hint::option{"$part_id.concepts"})) {
  294: 		foreach my $foil (@{ $Apache::hint::option{"$part_id.concept.$concept"} }) {
  295: 		    &Apache::lonxml::debug("compare -$foil- -$response{$foil}-$compare-");
  296: 		    if ( exists($response{$foil}) && 
  297: 			 $response{$foil} ne $compare) {$conceptmatch=0;last;}
  298: 		}
  299: 	    } else {
  300: 		$conceptmatch=0;
  301: 	    }
  302: 	    if ($conceptmatch eq '0') { last; }
  303: 	}
  304: 	if ( ($conceptmatch eq '-1' || $conceptmatch eq '1') &&
  305: 	     ($foilmatch    eq '-1' || $foilmatch    eq '1') ) {
  306: 	    push(@Apache::hint::which,$name);
  307: 	}
  308:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  309:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  310:     &Apache::response::end_hintresponse();
  311:     return $result;
  312: }
  313: 
  314: sub start_radiobuttonhint {
  315:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  316:     my $result;
  317:     &Apache::response::start_hintresponse($parstack,$safeeval);
  318:     if ($target eq 'edit') {
  319: 	$result.=&Apache::edit::tag_start($target,$token);
  320: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  321: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  322:     } elsif ($target eq 'modified') {
  323: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  324: 						     $safeeval,'name',
  325: 						     'answer');
  326: 	if ($constructtag) {
  327: 	    $result  = &Apache::edit::rebuild_tag($token);
  328: 	    $result .= &Apache::edit::handle_insert();
  329: 	}
  330:     } elsif ($target eq 'meta') {
  331: 	$result=&Apache::response::meta_package_write('numericalhint');
  332:     }
  333:     return $result;
  334: }
  335: 
  336: sub end_radiobuttonhint {
  337:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  338:     my $result;
  339:     if ($target eq 'web') {
  340: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  341: 	my @answer;
  342: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  343: 	eval('@answer ='.$answer);
  344: 	my $partid=$Apache::inputtags::part;
  345: 	#id submissions occured under
  346: 	my $submitid=$Apache::inputtags::response['-2'];
  347: 	my $part_id="$partid.$submitid";
  348: 	my $response = $Apache::lonhomework::history{
  349: 			    "resource.$part_id.submission"};
  350: 	($response)=&Apache::lonnet::str2hash($response);
  351: 	&Apache::lonxml::debug("response is $response");
  352: 	
  353: 	if ($answer[0] eq 'foil') {
  354: 	    shift(@answer);
  355: 	    foreach my $answer (@answer) {
  356: 		if ($response eq $answer) {
  357: 		    push (@Apache::hint::which,$name);
  358: 		    last;
  359: 		}
  360: 	    }
  361: 	} elsif ($answer[0] eq 'concept') {
  362: 	    shift(@answer);
  363: 	    foreach my $answer (@answer) {
  364: 		if (ref($Apache::hint::radiobutton{"$part_id.concept.".$answer})) {
  365: 		    my @names=@{ $Apache::hint::radiobutton{"$part_id.concept.".$answer} };
  366: 		    if (grep(/^\Q$response\E$/,@names)) {
  367: 			push(@Apache::hint::which,$name);
  368: 			last;
  369: 		    }
  370: 		}
  371: 	    }
  372: 	}
  373:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  374:     &Apache::response::end_hintresponse();
  375:     return $result;
  376: }
  377: 1;
  378: __END__

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