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

1.21      albertel    1: # The LearningOnline Network with CAPA 
                      2: # implements the tags that control the hints
                      3: #
1.70    ! raeburn     4: # $Id: hint.pm,v 1.69 2008/09/11 14:47:17 bisitz 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;
1.65      albertel   32: use Apache::lonnet();
1.6       albertel   33: use capa;
1.65      albertel   34: use Apache::caparesponse();
1.63      www        35: use Apache::lonmaxima();
                     36: use Apache::response();
1.53      albertel   37: use Apache::lonlocal;
1.65      albertel   38: use Storable qw(dclone);
1.1       albertel   39: 
1.25      harris41   40: BEGIN {
1.64      www        41:     &Apache::lonxml::register('Apache::hinttags',('hintgroup','hintpart','numericalhint','stringhint','formulahint','optionhint','radiobuttonhint','mathhint','customhint'));
1.1       albertel   42: }
                     43: 
1.2       albertel   44: 
1.15      albertel   45: @Apache::hint::which=();
1.1       albertel   46: sub start_hintgroup {
1.39      albertel   47:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     48:     my $skiptoend='0';
                     49:     my $result;
                     50: 
                     51:     if ($target eq 'web') {
                     52: 	my $id=$Apache::inputtags::part;
                     53: 	my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
                     54: 	if ( $numtries eq '') { $numtries = 0; }
1.52      albertel   55: 	my $hinttries=&Apache::response::get_response_param($id,"hinttries",1);
1.39      albertel   56: 	&Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
1.53      albertel   57: 	my $gradestatus=
                     58: 	    $Apache::lonhomework::history{"resource.$id.solved"};
                     59: 	my $showoncorrect=lc(&Apache::lonxml::get_param('showoncorrect',$parstack,$safeeval));	
                     60: 	&Apache::lonxml::debug("onc orrect $showoncorrect, $gradestatus");
1.58      albertel   61: 	if ( ($showoncorrect ne 'yes' && &Apache::response::show_answer()) ||
1.53      albertel   62: 	     ( $numtries < $hinttries) ) {
                     63: 	    &Apache::lonxml::debug("Grabbin all");
1.61      albertel   64: 	    &Apache::lonxml::get_all_text("/hintgroup",$parser,$style);
1.39      albertel   65: 	}
1.40      albertel   66: 	&Apache::lonxml::startredirection;
1.39      albertel   67:     } elsif ($target eq 'tex') {
                     68: 	$result .= '\keephidden{';
1.53      albertel   69:     } elsif ($target eq 'edit') {
                     70: 	$result.=&Apache::edit::tag_start($target,$token);
1.56      albertel   71: 	$result.=&Apache::edit::select_arg('Show hint even if problem Correct:','showoncorrect',[['no',&mt('No')],['yes',&mt('Yes')]],$token);
1.53      albertel   72: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                     73:     } elsif ($target eq 'modified') {
                     74: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'showoncorrect');
                     75:        if ($constructtag) {
                     76:            $result =&Apache::edit::rebuild_tag($token);
                     77:        }
1.20      albertel   78:     }
1.39      albertel   79:     @Apache::hint::which=();
                     80:     return $result;
1.1       albertel   81: }
                     82: 
                     83: sub end_hintgroup {
1.39      albertel   84:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     85:     my $result;
1.20      albertel   86: 
1.39      albertel   87:     if ($target eq 'web') {
                     88: 	my $id=$Apache::inputtags::part;
                     89: 	my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
                     90: 	if ( $numtries eq '') { $numtries = 0; }
1.52      albertel   91: 	my $hinttries=&Apache::response::get_response_param($id,"hinttries",1);
1.39      albertel   92: 	&Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
1.40      albertel   93: 	my $hinttext=&Apache::lonxml::endredirection;
                     94: 	if ($Apache::lonhomework::type ne 'exam' &&
1.41      albertel   95: 	    $numtries >= $hinttries && $hinttext =~/\S/) {
1.40      albertel   96: 	    $result='<table bgcolor="#dddddd"><tr><td>'.
                     97: 		$hinttext.'</td></tr></table>';
1.38      albertel   98: 	}
1.39      albertel   99:     } elsif ($target eq 'edit') {
1.62      albertel  100: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
1.39      albertel  101:     } elsif ($target eq 'tex') {
                    102: 	$result .= '}';
1.19      albertel  103:     }
1.39      albertel  104:     @Apache::hint::which=();
                    105:     return $result;
1.3       albertel  106: }
                    107: 
                    108: sub start_numericalhint {
1.39      albertel  109:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    110:     #do everything in end, so intervening <responseparams> work
                    111:     &Apache::response::start_hintresponse($parstack,$safeeval);
1.66      albertel  112:     &Apache::caparesponse::push_answer();
1.39      albertel  113:     my $result;
                    114:     if ($target eq 'edit') {
                    115: 	$result.=&Apache::edit::tag_start($target,$token);
                    116: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    117: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
                    118: 	if ($token->[1] eq 'numericalhint') {
                    119: 	    $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
                    120: 		&Apache::loncommon::help_open_topic('Physical_Units');
                    121: 	    $result.=&Apache::edit::text_arg('Format:','format',$token,4).
                    122: 		&Apache::loncommon::help_open_topic('Numerical_Response_Format');
                    123: 	} elsif ($token->[1] eq 'formulahint') {
1.65      albertel  124: 	    $result.=&Apache::edit::text_arg('Sample Points:','samples',
                    125: 					     $token,40).
                    126: 	        &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
1.39      albertel  127: 	}
                    128: 	$result.=&Apache::edit::end_row();
                    129: 	$result.=&Apache::edit::start_spanning_row();
                    130:     } elsif ($target eq 'modified') {
                    131: 	my $constructtag;
                    132: 	if ($token->[1] eq 'numericalhint') {
                    133: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    134: 						     $safeeval,'name',
                    135: 						     'answer','unit','format');
                    136: 	} elsif ($token->[1] eq 'formulahint') {
                    137: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    138: 						     $safeeval,'name','answer',
                    139: 						     'samples');
                    140: 	}
                    141: 	if ($constructtag) {
                    142: 	    $result  = &Apache::edit::rebuild_tag($token);
                    143: 	}
                    144:     } elsif ($target eq 'web') {
                    145: 	&Apache::response::reset_params();
1.28      albertel  146:     }
1.39      albertel  147:     return $result;
1.3       albertel  148: }
                    149: 
                    150: sub end_numericalhint {
1.39      albertel  151:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    152:     my $result;
                    153:     if ($target eq 'web') {
                    154: 	if (!$Apache::lonxml::default_homework_loaded) {
                    155: 	    &Apache::lonxml::default_homework_load($safeeval);
                    156: 	}
                    157: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
1.65      albertel  158: 	my $hint_name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.48      albertel  159: 	&Apache::response::setup_params('numericalhint',$safeeval);
1.65      albertel  160: 
1.39      albertel  161: 	my $partid=$Apache::inputtags::part;
1.60      albertel  162: 	my $id=$Apache::inputtags::hint[-1];
1.69      bisitz    163: 	#id submissions occurred under
1.60      albertel  164: 	my $submitid=$Apache::inputtags::response[-1];
1.65      albertel  165: 
1.39      albertel  166: 	my $response = $Apache::lonhomework::history{
1.18      albertel  167: 			    "resource.$partid.$submitid.submission"};
1.39      albertel  168: 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
1.65      albertel  169: 
                    170: 	my @args = ('type','tol','sig','ans_fmt','unit','calc','samples');
                    171: 	my $args_ref =
                    172: 	    &Apache::caparesponse::setup_capa_args($safeeval,$parstack,
                    173: 						   \@args,$response);
                    174: 
1.54      albertel  175: 	my $hideunit=&Apache::response::get_response_param($partid.'_'.$submitid,'turnoffunit');
1.51      albertel  176: 	if (lc($hideunit) eq "yes") { delete($$args_ref{'unit'}); }
1.65      albertel  177: 
1.43      albertel  178: 	if ($$tagstack[-1] eq 'formulahint') {
1.67      www       179:             if ($$args_ref{'samples'}) {
                    180:                 $$args_ref{'type'}='fml';
                    181:             } else {
                    182:                 $$args_ref{'type'}='math';
                    183:             }
1.43      albertel  184: 	} elsif ($$tagstack[-1] eq 'numericalhint') {
1.51      albertel  185: 	    $$args_ref{'type'}='float';
1.39      albertel  186: 	}
1.65      albertel  187: 	&Apache::caparesponse::add_in_tag_answer($parstack,$safeeval);
                    188: 	my %answer = &Apache::caparesponse::get_answer();
                    189: 	my (@final_awards,@final_msgs,@ans_names);
                    190: 	foreach my $ans_name (keys(%answer)) {
                    191: 	    &Apache::lonxml::debug(" doing $ans_name with ".join(':',@{ $answer{$ans_name}{'answers'} }));
                    192: 	
                    193: 	    ${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=
                    194: 		dclone($answer{$ans_name});
                    195: 	    &Apache::caparesponse::setup_capa_response($args_ref,$response);
                    196: 	    my ($result,@msgs) = 
1.70    ! raeburn   197: 		&Apache::run::run("&caparesponse_check_list($$tagstack[-1])",$safeeval);
1.65      albertel  198: 	    &Apache::lonxml::debug("checking $ans_name $result with $response");
                    199: 	    &Apache::lonxml::debug('msgs are '.join(':',@msgs));
                    200: 	    my ($awards)=split(/:/,$result);
                    201: 	    my @awards= split(/,/,$awards);
                    202: 	    my ($ad, $msg) = 
                    203: 		&Apache::inputtags::finalizeawards(\@awards,\@msgs);
                    204: 	    push(@final_awards,$ad);
                    205: 	    push(@final_msgs,$msg);
                    206: 	    push(@ans_names,$ans_name);
                    207: 	}
                    208: 	my ($ad, $msg, $ans_name) =
                    209: 	    &Apache::inputtags::finalizeawards(\@final_awards,
                    210: 					       \@final_msgs,
                    211: 					       \@ans_names,1);
                    212: 	if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') {
                    213: 	    push(@Apache::hint::which,$hint_name);	
                    214: 	}
1.39      albertel  215: 	$result='';
                    216:     } elsif ($target eq 'meta') {
                    217: 	$result=&Apache::response::meta_package_write($token->[1]);
                    218:     } elsif ($target eq 'edit') {
1.62      albertel  219: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
1.18      albertel  220:     }
1.66      albertel  221:     &Apache::caparesponse::pop_answer();
1.39      albertel  222:     &Apache::response::end_hintresponse();
                    223:     return $result;
1.28      albertel  224: }
                    225: 
1.51      albertel  226: sub start_formulahint {
1.35      albertel  227:     return &start_numericalhint(@_);
1.28      albertel  228: }
                    229: 
1.51      albertel  230: sub end_formulahint {
1.35      albertel  231:     return &end_numericalhint(@_);
1.28      albertel  232: }
                    233: 
1.63      www       234: sub start_mathhint {
                    235:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    236:     #do everything in end, so intervening <responseparams> and <answer> work
                    237:     &Apache::response::start_hintresponse($parstack,$safeeval);
                    238:     &Apache::lonxml::register('Apache::response',('answer'));
                    239:     my $result;
                    240:     if ($target eq 'edit') {
                    241:         $result.=&Apache::edit::tag_start($target,$token);
                    242:         $result.=&Apache::edit::text_arg('Name:','name',$token);
                    243:         $result.=&Apache::edit::select_arg('Algebra System:',
                    244:                                            'cas',
                    245:                                            ['maxima'],
                    246:                                            $token);
                    247:         $result.=&Apache::edit::text_arg('Argument Array:',
                    248:                                          'args',$token);
                    249:         $result.=&Apache::edit::end_row();
                    250:         $result.=&Apache::edit::start_spanning_row();
                    251:     } elsif ($target eq 'modified') {
                    252:         my $constructtag;
                    253:         $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    254:                                                   $safeeval,'name','cas',
                    255:                                                   'args');
                    256:         $result  = &Apache::edit::rebuild_tag($token);
                    257:     } elsif ($target eq 'web') {
                    258:         &Apache::response::reset_params();
                    259:     }
                    260:     return $result;
                    261: }
                    262: 
                    263: sub end_mathhint {
                    264:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    265:     my $result;
                    266:     if ($target eq 'web') {
                    267:         if (!$Apache::lonxml::default_homework_loaded) {
                    268:             &Apache::lonxml::default_homework_load($safeeval);
                    269:         }
                    270: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    271:         &Apache::response::setup_params('mathhint',$safeeval);
                    272:         my $partid=$Apache::inputtags::part;
                    273:         my $submitid=$Apache::inputtags::response[-1];
                    274:         my $response = $Apache::lonhomework::history{
                    275:                             "resource.$partid.$submitid.submission"};
                    276: 	
                    277: 	my $cas = &Apache::lonxml::get_param('cas',$parstack,$safeeval);
                    278: 	my $award;
                    279: 	if ($cas eq 'maxima') {
                    280: 	    my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
                    281: 	    $award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer[-1],$response,$args);
                    282: 	}
                    283:         if ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS') {
                    284:             push (@Apache::hint::which,$name);
                    285:         }
                    286:         $result='';
                    287:     } elsif ($target eq 'meta') {
                    288:         $result=&Apache::response::meta_package_write($token->[1]);
                    289:     } elsif ($target eq 'edit') {
                    290:         $result.=&Apache::edit::end_row().&Apache::edit::end_table();
                    291:     }
                    292:     pop(@Apache::response::custom_answer);
                    293:     pop(@Apache::response::custom_answer_type);
                    294:     &Apache::response::end_hintresponse();
                    295:     return $result;
                    296: }
                    297: 
1.64      www       298: sub start_customhint {
                    299:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    300:     #do everything in end, so intervening <responseparams> and <answer> work
                    301:     &Apache::response::start_hintresponse($parstack,$safeeval);
                    302:     &Apache::lonxml::register('Apache::response',('answer'));
                    303:     my $result;
                    304:     if ($target eq 'edit') {
                    305:         $result.=&Apache::edit::tag_start($target,$token);
                    306:         $result.=&Apache::edit::text_arg('Name:','name',$token);
                    307:         $result.=&Apache::edit::end_row();
                    308:         $result.=&Apache::edit::start_spanning_row();
                    309:     } elsif ($target eq 'modified') {
                    310:         my $constructtag;
                    311:         $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    312:                                                   $safeeval,'name');
                    313:         $result  = &Apache::edit::rebuild_tag($token);
                    314:     } elsif ($target eq 'web') {
                    315:         &Apache::response::reset_params();
                    316:     }
                    317:     return $result;
                    318: }
                    319: 
                    320: sub end_customhint {
                    321:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    322:     my $result;
                    323:     if ($target eq 'web') {
                    324:         if (!$Apache::lonxml::default_homework_loaded) {
                    325:             &Apache::lonxml::default_homework_load($safeeval);
                    326:         }
                    327: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    328:         &Apache::response::setup_params('customhint',$safeeval);
                    329:         my $partid=$Apache::inputtags::part;
                    330:         my $submitid=$Apache::inputtags::response[-1];
                    331:         my $response = $Apache::lonhomework::history{
                    332:                             "resource.$partid.$submitid.submission"};
                    333:         my $award;	
                    334: 	if ( $response =~ /[^\s]/ && 
                    335: 	     $Apache::response::custom_answer_type[-1] eq 'loncapa/perl') {
                    336: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    337: 		&Apache::lonxml::default_homework_load($safeeval);
                    338: 	    }
                    339: 	    ${$safeeval->varglob('LONCAPA::customresponse_submission')}=
                    340: 		$response;
                    341: 	    
                    342: 	    $award = &Apache::run::run('{ my $submission=$LONCAPA::customresponse_submission;'.$Apache::response::custom_answer[-1].'}',$safeeval);
                    343: 	}
                    344:         if ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS') {
                    345:             push (@Apache::hint::which,$name);
                    346:         }
                    347:         $result='';
                    348:     } elsif ($target eq 'meta') {
                    349:         $result=&Apache::response::meta_package_write($token->[1]);
                    350:     } elsif ($target eq 'edit') {
                    351:         $result.=&Apache::edit::end_row().&Apache::edit::end_table();
                    352:     }
                    353:     pop(@Apache::response::custom_answer);
                    354:     pop(@Apache::response::custom_answer_type);
                    355:     &Apache::response::end_hintresponse();
                    356:     return $result;
                    357: }
                    358: 
1.51      albertel  359: sub start_stringhint {
                    360:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    361:     #do everything in end, so intervening <responseparams> work
                    362:     &Apache::response::start_hintresponse($parstack,$safeeval);
                    363:     my $result;
                    364:     if ($target eq 'edit') {
                    365: 	$result.=&Apache::edit::tag_start($target,$token);
                    366: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    367: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
                    368: 	$result.=&Apache::edit::select_arg('Type:','type',
                    369: 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
                    370: 			  ['mc','Case Insensitive, Any Order'],
                    371: 			  ['re','Regular Expression']],$token);
                    372: 	$result.=&Apache::edit::end_row();
                    373: 	$result.=&Apache::edit::start_spanning_row();
                    374:     } elsif ($target eq 'modified') {
                    375: 	my $constructtag;
                    376: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
                    377: 						  $safeeval,'name','answer',
                    378: 						  'type');
                    379: 	$result  = &Apache::edit::rebuild_tag($token);
                    380:     } elsif ($target eq 'web') {
                    381: 	&Apache::response::reset_params();
                    382:     }
                    383:     return $result;
1.28      albertel  384: }
                    385: 
1.51      albertel  386: sub end_stringhint {
                    387:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    388:     my $result;
                    389:     if ($target eq 'web') {
                    390: 	if (!$Apache::lonxml::default_homework_loaded) {
                    391: 	    &Apache::lonxml::default_homework_load($safeeval);
                    392: 	}
                    393: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
1.65      albertel  394: 	my $hint_name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.51      albertel  395: 	&Apache::response::setup_params('stringhint',$safeeval);
                    396: 	my $partid=$Apache::inputtags::part;
1.60      albertel  397: 	my $id=$Apache::inputtags::hint[-1];
1.69      bisitz    398: 	#id submissions occurred under
1.60      albertel  399: 	my $submitid=$Apache::inputtags::response[-1];
1.51      albertel  400: 	my $response = $Apache::lonhomework::history{
                    401: 			    "resource.$partid.$submitid.submission"};
                    402: 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
1.65      albertel  403: 	my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
1.51      albertel  404: 	my ($ad,$msg);
                    405: 	if ($type eq 're' ) {
1.65      albertel  406: 	    my $answer=&Apache::lonxml::get_param('answer',$parstack,
                    407: 						  $safeeval);
1.51      albertel  408: 	    ${$safeeval->varglob('LONCAPA::response')}=$response;
1.57      albertel  409: 	    my $compare='=';
                    410: 	    if ($answer=~/^\s*NOT\s*/) {
                    411: 		$answer=~s/^\s*NOT\s*//;
                    412: 		$compare='!';
                    413: 	    }
                    414: 	    my $test='$LONCAPA::response'.$compare.'~m'.$answer;
                    415: 	    &Apache::lonxml::debug("test $test");
                    416: 	    $result = &Apache::run::run("return $test",$safeeval);
1.51      albertel  417: 	    &Apache::lonxml::debug("current $response");
                    418: 	    &Apache::lonxml::debug("current $answer");
                    419: 	    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
                    420: 	} else { 
1.65      albertel  421: 	    my @args = ('type');
                    422: 	    my $args_ref = 
                    423: 		&Apache::caparesponse::setup_capa_args($safeeval,$parstack,
                    424: 						       \@args,$response);
                    425: 	    &Apache::caparesponse::add_in_tag_answer($parstack,$safeeval);
                    426: 	    my (@final_awards,@final_msgs,@ans_names);
                    427: 	    my %answer = &Apache::caparesponse::get_answer();
                    428: 	    foreach my $ans_name (keys(%answer)) {
                    429: 		&Apache::lonxml::debug(" doing $ans_name with ".join(':',@{ $answer{$ans_name}{'answers'} }));
                    430: 		${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$ans_name});
1.70    ! raeburn   431: 		my ($result, @msgs)=&Apache::run::run("&caparesponse_check_list($$tagstack[-1])",$safeeval);
1.65      albertel  432: 		&Apache::lonxml::debug('msgs are'.join(':',@msgs));
                    433: 		my ($awards) = split(/:/,$result);
                    434: 		my (@awards) = split(/,/,$awards);
                    435: 		($ad,$msg) = 
                    436: 		    &Apache::inputtags::finalizeawards(\@awards,\@msgs);
                    437: 		push(@final_awards,$ad);
                    438: 		push(@final_msgs,$msg);
                    439: 		push(@ans_names,$ans_name);
                    440: 		&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
1.51      albertel  441: 	    }
1.65      albertel  442: 	    my ($ad, $msg, $ans_name) = 
                    443: 		&Apache::inputtags::finalizeawards(\@final_awards,
                    444: 						   \@final_msgs,
                    445: 						   \@ans_names,1);
1.51      albertel  446: 	}
                    447: 	if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { 
1.65      albertel  448: 	    push(@Apache::hint::which,$hint_name);
1.51      albertel  449: 	}
                    450: 	$result='';
                    451:     } elsif ($target eq 'meta') {
                    452: 	$result=&Apache::response::meta_package_write($token->[1]);
                    453:     } elsif ($target eq 'edit') {
1.62      albertel  454: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
1.51      albertel  455:     }
                    456:     &Apache::response::end_hintresponse();
                    457:     return $result;
1.1       albertel  458: }
                    459: 
1.2       albertel  460: # a part shows if it is on, if no specific parts are on, then default shows
1.1       albertel  461: sub start_hintpart {
1.39      albertel  462:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.15      albertel  463: 
1.39      albertel  464:     my $show ='0';
                    465:     my $result = '';
                    466:     if ($target eq 'web') {
                    467: 	my $on= &Apache::lonxml::get_param('on',$parstack,$safeeval);
                    468: 	&Apache::lonxml::debug("hintpart sees $on and ,$#Apache::hint::which");
                    469: 	if ( $on eq 'default' && $#Apache::hint::which == '-1') {
                    470: 	    $show=1;
                    471: 	} else {
                    472: 	    my $which;
                    473: 	    foreach $which (@Apache::hint::which) { if ($which eq $on) { $show = 1; last } }
                    474: 	}
                    475: 	if (!$show) {
1.61      albertel  476: 	    &Apache::lonxml::get_all_text("/hintpart",$parser,$style);
1.39      albertel  477: 	}
                    478:     } elsif ($target eq 'grade') {
1.61      albertel  479: 	&Apache::lonxml::get_all_text("/hintpart",$parser,$style);
1.39      albertel  480:     } elsif ($target eq 'edit') {
                    481: 	$result.= &Apache::edit::tag_start($target,$token);
                    482: 	$result.= &Apache::edit::text_arg('On:','on',$token);
                    483: 	$result.= &Apache::edit::end_row();
                    484: 	$result.= &Apache::edit::start_spanning_row();
                    485:     } elsif ($target eq 'modified') {
                    486: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    487: 						     $safeeval,'on');
                    488: 	if ($constructtag) {
                    489: 	    $result = &Apache::edit::rebuild_tag($token);
                    490: 	}
1.20      albertel  491:     }
1.39      albertel  492:     return $result;
1.1       albertel  493: }
                    494: 
                    495: sub end_hintpart {
1.29      albertel  496:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    497:     my $result;
1.62      albertel  498:     if ($target eq 'edit') {
                    499: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
                    500:     }
1.29      albertel  501:     return $result;
                    502: }
                    503: 
                    504: sub start_optionhint {
                    505:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    506:     my $result;
1.30      albertel  507:     &Apache::response::start_hintresponse($parstack,$safeeval);
                    508:     if ($target eq 'edit') {
                    509: 	$result.=&Apache::edit::tag_start($target,$token);
                    510: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    511: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token,40);
                    512: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,50);
                    513:     } elsif ($target eq 'modified') {
                    514: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    515: 						     $safeeval,'name',
                    516: 						     'answer','concept');
                    517: 	if ($constructtag) {
                    518: 	    $result  = &Apache::edit::rebuild_tag($token);
                    519: 	}
                    520:     } elsif ($target eq 'meta') {
                    521: 	$result=&Apache::response::meta_package_write('numericalhint');
                    522:     }
1.29      albertel  523:     return $result;
                    524: }
                    525: 
                    526: sub end_optionhint {
                    527:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    528:     my $result;
1.30      albertel  529:     if ($target eq 'web') {
                    530: 	my ($foilmatch,$conceptmatch)=(-1,-1);
                    531: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    532: 	my $partid=$Apache::inputtags::part;
1.69      bisitz    533: 	#id submissions occurred under
1.60      albertel  534: 	my $submitid=$Apache::inputtags::response[-1];
1.30      albertel  535: 	my $part_id="$partid.$submitid";
                    536: 	my %answer;
                    537: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    538: 	if ($answer) {
                    539: 	    eval('%answer ='.$answer);
1.62      albertel  540: 	    &Apache::lonxml::debug("answwer hash");
1.30      albertel  541: 	    &Apache::lonhomework::showhash(%answer);
                    542: 	    my $response = $Apache::lonhomework::history{
                    543: 				            "resource.$part_id.submission"};
                    544: 	    my %response=&Apache::lonnet::str2hash($response);
                    545: 	    &Apache::lonhomework::showhash(%response);
                    546: 	    foreach my $foil (keys(%answer)) {
                    547: 		$foilmatch=1;
                    548: 		if ($answer{$foil} ne $response{$foil}) {$foilmatch=0;last;}
                    549: 	    }
                    550: 	}
                    551: 	my %concept;
                    552: 	my $constr=&Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    553: 	if ( $constr ) { eval('%concept ='.$constr);	}
                    554: 	my $response = $Apache::lonhomework::history{
                    555: 					"resource.$part_id.submissiongrading"};
                    556: 	my %response=&Apache::lonnet::str2hash($response);
                    557: 	foreach my $concept (keys(%concept)) {
                    558: 	    my $compare;
                    559: 	    if ($concept{$concept} eq 'correct') {$compare=1}else{$compare=0}
                    560: 	    $conceptmatch=1;
                    561: 	    if (ref($Apache::hint::option{"$part_id.concepts"})) {
                    562: 		foreach my $foil (@{ $Apache::hint::option{"$part_id.concept.$concept"} }) {
                    563: 		    &Apache::lonxml::debug("compare -$foil- -$response{$foil}-$compare-");
                    564: 		    if ( exists($response{$foil}) && 
                    565: 			 $response{$foil} ne $compare) {$conceptmatch=0;last;}
                    566: 		}
                    567: 	    } else {
                    568: 		$conceptmatch=0;
                    569: 	    }
                    570: 	    if ($conceptmatch eq '0') { last; }
                    571: 	}
                    572: 	if ( ($conceptmatch eq '-1' || $conceptmatch eq '1') &&
                    573: 	     ($foilmatch    eq '-1' || $foilmatch    eq '1') ) {
                    574: 	    push(@Apache::hint::which,$name);
                    575: 	}
1.62      albertel  576:     } elsif ($target eq 'edit') {
                    577: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
                    578:     }
1.30      albertel  579:     &Apache::response::end_hintresponse();
1.29      albertel  580:     return $result;
                    581: }
                    582: 
                    583: sub start_radiobuttonhint {
                    584:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    585:     my $result;
                    586:     &Apache::response::start_hintresponse($parstack,$safeeval);
                    587:     if ($target eq 'edit') {
                    588: 	$result.=&Apache::edit::tag_start($target,$token);
                    589: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    590: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
                    591:     } elsif ($target eq 'modified') {
                    592: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    593: 						     $safeeval,'name',
                    594: 						     'answer');
                    595: 	if ($constructtag) {
                    596: 	    $result  = &Apache::edit::rebuild_tag($token);
                    597: 	}
                    598:     } elsif ($target eq 'meta') {
                    599: 	$result=&Apache::response::meta_package_write('numericalhint');
                    600:     }
                    601:     return $result;
1.1       albertel  602: }
                    603: 
1.31      albertel  604: sub end_radiobuttonhint {
1.29      albertel  605:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    606:     my $result;
                    607:     if ($target eq 'web') {
                    608: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    609: 	my @answer;
                    610: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    611: 	eval('@answer ='.$answer);
                    612: 	my $partid=$Apache::inputtags::part;
1.69      bisitz    613: 	#id submissions occurred under
1.60      albertel  614: 	my $submitid=$Apache::inputtags::response[-1];
1.29      albertel  615: 	my $part_id="$partid.$submitid";
                    616: 	my $response = $Apache::lonhomework::history{
                    617: 			    "resource.$part_id.submission"};
                    618: 	($response)=&Apache::lonnet::str2hash($response);
1.42      albertel  619: 	&Apache::lonxml::debug("response is $response");
                    620: 	
                    621: 	if ($answer[0] eq 'foil') {
                    622: 	    shift(@answer);
                    623: 	    foreach my $answer (@answer) {
                    624: 		if ($response eq $answer) {
                    625: 		    push (@Apache::hint::which,$name);
                    626: 		    last;
                    627: 		}
                    628: 	    }
1.29      albertel  629: 	} elsif ($answer[0] eq 'concept') {
1.42      albertel  630: 	    shift(@answer);
                    631: 	    foreach my $answer (@answer) {
                    632: 		if (ref($Apache::hint::radiobutton{"$part_id.concept.".$answer})) {
                    633: 		    my @names=@{ $Apache::hint::radiobutton{"$part_id.concept.".$answer} };
                    634: 		    if (grep(/^\Q$response\E$/,@names)) {
                    635: 			push(@Apache::hint::which,$name);
                    636: 			last;
                    637: 		    }
1.29      albertel  638: 		}
                    639: 	    }
                    640: 	}
1.62      albertel  641:     } elsif ($target eq 'edit') {
                    642: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
                    643:     }
1.29      albertel  644:     &Apache::response::end_hintresponse();
                    645:     return $result;
                    646: }
1.1       albertel  647: 1;
                    648: __END__

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