File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.44: download - view: text, annotated - select for diffs
Fri Jun 20 04:18:49 2003 UTC (20 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- not that anyone uses the conditional hints, but now the stringhint is even more prettier when edited

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

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