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

1.21      albertel    1: # The LearningOnline Network with CAPA 
                      2: # implements the tags that control the hints
                      3: #
1.48    ! albertel    4: # $Id: hint.pm,v 1.47 2004/03/04 21:21:55 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)) {
                    166: 	    $expression.= ';my $'. #'
                    167: 		$key.'="'.$Apache::inputtags::params{$key}.'"';
1.43      albertel  168: 	}
                    169: 	if ($$tagstack[-1] eq 'formulahint') {
                    170: 	    $expression.=';my $type="fml";';
                    171: 	} elsif ($$tagstack[-1] eq 'numericalhint') {
                    172: 	    $expression.=';my $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.39      albertel  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;
1.18      albertel  189:     }
1.39      albertel  190:     &Apache::response::end_hintresponse();
                    191:     return $result;
1.28      albertel  192: }
                    193: 
                    194: sub start_stringhint {
1.35      albertel  195:     return &start_numericalhint(@_);
1.28      albertel  196: }
                    197: 
                    198: sub end_stringhint {
1.35      albertel  199:     return &end_numericalhint(@_);
1.28      albertel  200: }
                    201: 
                    202: sub start_formulahint {
1.35      albertel  203:     return &start_numericalhint(@_);
1.28      albertel  204: }
                    205: 
                    206: sub end_formulahint {
1.35      albertel  207:     return end_numericalhint(@_);
1.1       albertel  208: }
                    209: 
1.2       albertel  210: # a part shows if it is on, if no specific parts are on, then default shows
1.1       albertel  211: sub start_hintpart {
1.39      albertel  212:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.15      albertel  213: 
1.39      albertel  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: 	}
1.20      albertel  242:     }
1.39      albertel  243:     return $result;
1.1       albertel  244: }
                    245: 
                    246: sub end_hintpart {
1.29      albertel  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;
1.30      albertel  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:     }
1.29      albertel  273:     return $result;
                    274: }
                    275: 
                    276: sub end_optionhint {
                    277:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    278:     my $result;
1.30      albertel  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; }
1.29      albertel  326:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
1.30      albertel  327:     &Apache::response::end_hintresponse();
1.29      albertel  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;
1.1       albertel  351: }
                    352: 
1.31      albertel  353: sub end_radiobuttonhint {
1.29      albertel  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);
1.42      albertel  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: 	    }
1.29      albertel  378: 	} elsif ($answer[0] eq 'concept') {
1.42      albertel  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: 		    }
1.29      albertel  387: 		}
                    388: 	    }
                    389: 	}
                    390:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
                    391:     &Apache::response::end_hintresponse();
                    392:     return $result;
                    393: }
1.1       albertel  394: 1;
                    395: __END__

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