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

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

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