Annotation of loncom/homework/hint.pm, revision 1.50

1.21      albertel    1: # The LearningOnline Network with CAPA 
                      2: # implements the tags that control the hints
                      3: #
1.50    ! albertel    4: # $Id: hint.pm,v 1.49 2004/03/12 21:06:19 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");
                    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)) {
1.49      albertel  166: 	    $expression.= ';my $__LC__'. #'
1.39      albertel  167: 		$key.'="'.$Apache::inputtags::params{$key}.'"';
1.43      albertel  168: 	}
                    169: 	if ($$tagstack[-1] eq 'formulahint') {
1.49      albertel  170: 	    $expression.=';my $__LC__type="fml";';
1.43      albertel  171: 	} elsif ($$tagstack[-1] eq 'numericalhint') {
1.49      albertel  172: 	    $expression.=';my $__LC__type="float";';
1.39      albertel  173: 	}
                    174: 	$expression.="');";
1.45      albertel  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: 		
1.49      albertel  179: 	($result,my @msgs) = &Apache::run::run($expression,$safeeval);
1.50    ! albertel  180: 	&Apache::lonxml::debug('msgs are'.join(':',@msgs));
1.39      albertel  181: 	&Apache::lonxml::debug("$expression:result:$result:$Apache::lonxml::curdepth");
1.50    ! albertel  182: 	my ($awards)=split(/:/,$result);
        !           183: 	my (@awards) = split(/,/,$awards);
        !           184: 	my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
1.39      albertel  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;
1.18      albertel  191:     }
1.39      albertel  192:     &Apache::response::end_hintresponse();
                    193:     return $result;
1.28      albertel  194: }
                    195: 
                    196: sub start_stringhint {
1.35      albertel  197:     return &start_numericalhint(@_);
1.28      albertel  198: }
                    199: 
                    200: sub end_stringhint {
1.35      albertel  201:     return &end_numericalhint(@_);
1.28      albertel  202: }
                    203: 
                    204: sub start_formulahint {
1.35      albertel  205:     return &start_numericalhint(@_);
1.28      albertel  206: }
                    207: 
                    208: sub end_formulahint {
1.35      albertel  209:     return end_numericalhint(@_);
1.1       albertel  210: }
                    211: 
1.2       albertel  212: # a part shows if it is on, if no specific parts are on, then default shows
1.1       albertel  213: sub start_hintpart {
1.39      albertel  214:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.15      albertel  215: 
1.39      albertel  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: 	}
1.20      albertel  244:     }
1.39      albertel  245:     return $result;
1.1       albertel  246: }
                    247: 
                    248: sub end_hintpart {
1.29      albertel  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;
1.30      albertel  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:     }
1.29      albertel  275:     return $result;
                    276: }
                    277: 
                    278: sub end_optionhint {
                    279:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    280:     my $result;
1.30      albertel  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; }
1.29      albertel  328:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
1.30      albertel  329:     &Apache::response::end_hintresponse();
1.29      albertel  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;
1.1       albertel  353: }
                    354: 
1.31      albertel  355: sub end_radiobuttonhint {
1.29      albertel  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);
1.42      albertel  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: 	    }
1.29      albertel  380: 	} elsif ($answer[0] eq 'concept') {
1.42      albertel  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: 		    }
1.29      albertel  389: 		}
                    390: 	    }
                    391: 	}
                    392:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
                    393:     &Apache::response::end_hintresponse();
                    394:     return $result;
                    395: }
1.1       albertel  396: 1;
                    397: __END__

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