File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.65: download - view: text, annotated - select for diffs
Tue Dec 19 00:40:38 2006 UTC (17 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- make numerical/string/formula hints work in the presence of the new answer stuructures (multiple answers and multiple dimensional answer not yet funtional)

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

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