File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.47: download - view: text, annotated - select for diffs
Thu Mar 4 21:21:55 2004 UTC (20 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- hintties work in CSTR now.

    1: # The LearningOnline Network with CAPA 
    2: # implements the tags that control the hints
    3: #
    4: # $Id: hint.pm,v 1.47 2004/03/04 21:21:55 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');
  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 $'. #'
  167: 		$key.'="'.$Apache::inputtags::params{$key}.'"';
  168: 	}
  169: 	if ($$tagstack[-1] eq 'formulahint') {
  170: 	    $expression.=';my $type="fml";';
  171: 	} elsif ($$tagstack[-1] eq 'numericalhint') {
  172: 	    $expression.=';my $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 = &Apache::run::run($expression,$safeeval);
  180: 	&Apache::lonxml::debug("$expression:result:$result:$Apache::lonxml::curdepth");
  181: 	my ($awards) = split /:/ , $result;
  182: 	my ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
  183: 	if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { push (@Apache::hint::which,$name); }
  184: 	$result='';
  185:     } elsif ($target eq 'meta') {
  186: 	$result=&Apache::response::meta_package_write($token->[1]);
  187:     } elsif ($target eq 'edit') {
  188: 	$result.='</td></tr>'.&Apache::edit::end_table;
  189:     }
  190:     &Apache::response::end_hintresponse();
  191:     return $result;
  192: }
  193: 
  194: sub start_stringhint {
  195:     return &start_numericalhint(@_);
  196: }
  197: 
  198: sub end_stringhint {
  199:     return &end_numericalhint(@_);
  200: }
  201: 
  202: sub start_formulahint {
  203:     return &start_numericalhint(@_);
  204: }
  205: 
  206: sub end_formulahint {
  207:     return end_numericalhint(@_);
  208: }
  209: 
  210: # a part shows if it is on, if no specific parts are on, then default shows
  211: sub start_hintpart {
  212:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  213: 
  214:     my $show ='0';
  215:     my $result = '';
  216:     if ($target eq 'web') {
  217: 	my $on= &Apache::lonxml::get_param('on',$parstack,$safeeval);
  218: 	&Apache::lonxml::debug("hintpart sees $on and ,$#Apache::hint::which");
  219: 	if ( $on eq 'default' && $#Apache::hint::which == '-1') {
  220: 	    $show=1;
  221: 	} else {
  222: 	    my $which;
  223: 	    foreach $which (@Apache::hint::which) { if ($which eq $on) { $show = 1; last } }
  224: 	}
  225: 	if (!$show) {
  226: 	    &Apache::lonxml::get_all_text("/hintpart",$parser);
  227: 	}
  228:     } elsif ($target eq 'grade') {
  229: 	&Apache::lonxml::get_all_text("/hintpart",$parser);
  230:     } elsif ($target eq 'edit') {
  231: 	$result.= &Apache::edit::tag_start($target,$token);
  232: 	$result.= &Apache::edit::text_arg('On:','on',$token);
  233: 	$result.= &Apache::edit::end_row();
  234: 	$result.= &Apache::edit::start_spanning_row();
  235:     } elsif ($target eq 'modified') {
  236: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  237: 						     $safeeval,'on');
  238: 	if ($constructtag) {
  239: 	    $result = &Apache::edit::rebuild_tag($token);
  240: 	    $result.=&Apache::edit::handle_insert();
  241: 	}
  242:     }
  243:     return $result;
  244: }
  245: 
  246: sub end_hintpart {
  247:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  248:     my $result;
  249:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  250:     return $result;
  251: }
  252: 
  253: sub start_optionhint {
  254:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  255:     my $result;
  256:     &Apache::response::start_hintresponse($parstack,$safeeval);
  257:     if ($target eq 'edit') {
  258: 	$result.=&Apache::edit::tag_start($target,$token);
  259: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  260: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token,40);
  261: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,50);
  262:     } elsif ($target eq 'modified') {
  263: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  264: 						     $safeeval,'name',
  265: 						     'answer','concept');
  266: 	if ($constructtag) {
  267: 	    $result  = &Apache::edit::rebuild_tag($token);
  268: 	    $result .= &Apache::edit::handle_insert();
  269: 	}
  270:     } elsif ($target eq 'meta') {
  271: 	$result=&Apache::response::meta_package_write('numericalhint');
  272:     }
  273:     return $result;
  274: }
  275: 
  276: sub end_optionhint {
  277:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  278:     my $result;
  279:     if ($target eq 'web') {
  280: 	my ($foilmatch,$conceptmatch)=(-1,-1);
  281: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  282: 	my $partid=$Apache::inputtags::part;
  283: 	#id submissions occured under
  284: 	my $submitid=$Apache::inputtags::response['-2'];
  285: 	my $part_id="$partid.$submitid";
  286: 	my %answer;
  287: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  288: 	if ($answer) {
  289: 	    eval('%answer ='.$answer);
  290: 	    &Apache::lonhomework::showhash(%answer);
  291: 	    my $response = $Apache::lonhomework::history{
  292: 				            "resource.$part_id.submission"};
  293: 	    my %response=&Apache::lonnet::str2hash($response);
  294: 	    &Apache::lonhomework::showhash(%response);
  295: 	    foreach my $foil (keys(%answer)) {
  296: 		$foilmatch=1;
  297: 		if ($answer{$foil} ne $response{$foil}) {$foilmatch=0;last;}
  298: 	    }
  299: 	}
  300: 	my %concept;
  301: 	my $constr=&Apache::lonxml::get_param('concept',$parstack,$safeeval);
  302: 	if ( $constr ) { eval('%concept ='.$constr);	}
  303: 	my $response = $Apache::lonhomework::history{
  304: 					"resource.$part_id.submissiongrading"};
  305: 	my %response=&Apache::lonnet::str2hash($response);
  306: 	foreach my $concept (keys(%concept)) {
  307: 	    my $compare;
  308: 	    if ($concept{$concept} eq 'correct') {$compare=1}else{$compare=0}
  309: 	    $conceptmatch=1;
  310: 	    if (ref($Apache::hint::option{"$part_id.concepts"})) {
  311: 		foreach my $foil (@{ $Apache::hint::option{"$part_id.concept.$concept"} }) {
  312: 		    &Apache::lonxml::debug("compare -$foil- -$response{$foil}-$compare-");
  313: 		    if ( exists($response{$foil}) && 
  314: 			 $response{$foil} ne $compare) {$conceptmatch=0;last;}
  315: 		}
  316: 	    } else {
  317: 		$conceptmatch=0;
  318: 	    }
  319: 	    if ($conceptmatch eq '0') { last; }
  320: 	}
  321: 	if ( ($conceptmatch eq '-1' || $conceptmatch eq '1') &&
  322: 	     ($foilmatch    eq '-1' || $foilmatch    eq '1') ) {
  323: 	    push(@Apache::hint::which,$name);
  324: 	}
  325:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  326:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  327:     &Apache::response::end_hintresponse();
  328:     return $result;
  329: }
  330: 
  331: sub start_radiobuttonhint {
  332:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  333:     my $result;
  334:     &Apache::response::start_hintresponse($parstack,$safeeval);
  335:     if ($target eq 'edit') {
  336: 	$result.=&Apache::edit::tag_start($target,$token);
  337: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  338: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  339:     } elsif ($target eq 'modified') {
  340: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  341: 						     $safeeval,'name',
  342: 						     'answer');
  343: 	if ($constructtag) {
  344: 	    $result  = &Apache::edit::rebuild_tag($token);
  345: 	    $result .= &Apache::edit::handle_insert();
  346: 	}
  347:     } elsif ($target eq 'meta') {
  348: 	$result=&Apache::response::meta_package_write('numericalhint');
  349:     }
  350:     return $result;
  351: }
  352: 
  353: sub end_radiobuttonhint {
  354:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  355:     my $result;
  356:     if ($target eq 'web') {
  357: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  358: 	my @answer;
  359: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  360: 	eval('@answer ='.$answer);
  361: 	my $partid=$Apache::inputtags::part;
  362: 	#id submissions occured under
  363: 	my $submitid=$Apache::inputtags::response['-2'];
  364: 	my $part_id="$partid.$submitid";
  365: 	my $response = $Apache::lonhomework::history{
  366: 			    "resource.$part_id.submission"};
  367: 	($response)=&Apache::lonnet::str2hash($response);
  368: 	&Apache::lonxml::debug("response is $response");
  369: 	
  370: 	if ($answer[0] eq 'foil') {
  371: 	    shift(@answer);
  372: 	    foreach my $answer (@answer) {
  373: 		if ($response eq $answer) {
  374: 		    push (@Apache::hint::which,$name);
  375: 		    last;
  376: 		}
  377: 	    }
  378: 	} elsif ($answer[0] eq 'concept') {
  379: 	    shift(@answer);
  380: 	    foreach my $answer (@answer) {
  381: 		if (ref($Apache::hint::radiobutton{"$part_id.concept.".$answer})) {
  382: 		    my @names=@{ $Apache::hint::radiobutton{"$part_id.concept.".$answer} };
  383: 		    if (grep(/^\Q$response\E$/,@names)) {
  384: 			push(@Apache::hint::which,$name);
  385: 			last;
  386: 		    }
  387: 		}
  388: 	    }
  389: 	}
  390:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  391:     &Apache::response::end_hintresponse();
  392:     return $result;
  393: }
  394: 1;
  395: __END__

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