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

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

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