File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.67: download - view: text, annotated - select for diffs
Tue Dec 19 14:53:17 2006 UTC (17 years, 4 months ago) by www
Branches: MAIN
CVS tags: version_2_3_99_0, HEAD
- Make <formulahint> work with MAXIMA
- fix normal formulahint. Didn't have access to &capa_formula_fix

    1: # The LearningOnline Network with CAPA 
    2: # implements the tags that control the hints
    3: #
    4: # $Id: hint.pm,v 1.67 2006/12/19 14:53:17 www 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:     &Apache::caparesponse::push_answer();
  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') {
  125: 	    $result.=&Apache::edit::text_arg('Sample Points:','samples',
  126: 					     $token,40).
  127: 	        &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
  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();
  148:     }
  149:     return $result;
  150: }
  151: 
  152: sub end_numericalhint {
  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']);
  160: 	my $hint_name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  161: 	&Apache::response::setup_params('numericalhint',$safeeval);
  162: 
  163: 	my $partid=$Apache::inputtags::part;
  164: 	my $id=$Apache::inputtags::hint[-1];
  165: 	#id submissions occured under
  166: 	my $submitid=$Apache::inputtags::response[-1];
  167: 
  168: 	my $response = $Apache::lonhomework::history{
  169: 			    "resource.$partid.$submitid.submission"};
  170: 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
  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: 
  177: 	my $hideunit=&Apache::response::get_response_param($partid.'_'.$submitid,'turnoffunit');
  178: 	if (lc($hideunit) eq "yes") { delete($$args_ref{'unit'}); }
  179: 
  180: 	if ($$tagstack[-1] eq 'formulahint') {
  181:             if ($$args_ref{'samples'}) {
  182:                 $$args_ref{'type'}='fml';
  183:             } else {
  184:                 $$args_ref{'type'}='math';
  185:             }
  186: 	} elsif ($$tagstack[-1] eq 'numericalhint') {
  187: 	    $$args_ref{'type'}='float';
  188: 	}
  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: 	}
  217: 	$result='';
  218:     } elsif ($target eq 'meta') {
  219: 	$result=&Apache::response::meta_package_write($token->[1]);
  220:     } elsif ($target eq 'edit') {
  221: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
  222:     }
  223:     &Apache::caparesponse::pop_answer();
  224:     &Apache::response::end_hintresponse();
  225:     return $result;
  226: }
  227: 
  228: sub start_formulahint {
  229:     return &start_numericalhint(@_);
  230: }
  231: 
  232: sub end_formulahint {
  233:     return &end_numericalhint(@_);
  234: }
  235: 
  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: 
  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: 
  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;
  389: }
  390: 
  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']);
  399: 	my $hint_name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  400: 	&Apache::response::setup_params('stringhint',$safeeval);
  401: 	my $partid=$Apache::inputtags::part;
  402: 	my $id=$Apache::inputtags::hint[-1];
  403: 	#id submissions occured under
  404: 	my $submitid=$Apache::inputtags::response[-1];
  405: 	my $response = $Apache::lonhomework::history{
  406: 			    "resource.$partid.$submitid.submission"};
  407: 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
  408: 	my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  409: 	my ($ad,$msg);
  410: 	if ($type eq 're' ) {
  411: 	    my $answer=&Apache::lonxml::get_param('answer',$parstack,
  412: 						  $safeeval);
  413: 	    ${$safeeval->varglob('LONCAPA::response')}=$response;
  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);
  422: 	    &Apache::lonxml::debug("current $response");
  423: 	    &Apache::lonxml::debug("current $answer");
  424: 	    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
  425: 	} else { 
  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");
  446: 	    }
  447: 	    my ($ad, $msg, $ans_name) = 
  448: 		&Apache::inputtags::finalizeawards(\@final_awards,
  449: 						   \@final_msgs,
  450: 						   \@ans_names,1);
  451: 	}
  452: 	if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { 
  453: 	    push(@Apache::hint::which,$hint_name);
  454: 	}
  455: 	$result='';
  456:     } elsif ($target eq 'meta') {
  457: 	$result=&Apache::response::meta_package_write($token->[1]);
  458:     } elsif ($target eq 'edit') {
  459: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
  460:     }
  461:     &Apache::response::end_hintresponse();
  462:     return $result;
  463: }
  464: 
  465: # a part shows if it is on, if no specific parts are on, then default shows
  466: sub start_hintpart {
  467:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  468: 
  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) {
  481: 	    &Apache::lonxml::get_all_text("/hintpart",$parser,$style);
  482: 	}
  483:     } elsif ($target eq 'grade') {
  484: 	&Apache::lonxml::get_all_text("/hintpart",$parser,$style);
  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: 	}
  497:     }
  498:     return $result;
  499: }
  500: 
  501: sub end_hintpart {
  502:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  503:     my $result;
  504:     if ($target eq 'edit') {
  505: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
  506:     }
  507:     return $result;
  508: }
  509: 
  510: sub start_optionhint {
  511:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  512:     my $result;
  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:     }
  530:     return $result;
  531: }
  532: 
  533: sub end_optionhint {
  534:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  535:     my $result;
  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
  541: 	my $submitid=$Apache::inputtags::response[-1];
  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);
  547: 	    &Apache::lonxml::debug("answwer hash");
  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: 	}
  583:     } elsif ($target eq 'edit') {
  584: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
  585:     }
  586:     &Apache::response::end_hintresponse();
  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;
  610: }
  611: 
  612: sub end_radiobuttonhint {
  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
  622: 	my $submitid=$Apache::inputtags::response[-1];
  623: 	my $part_id="$partid.$submitid";
  624: 	my $response = $Apache::lonhomework::history{
  625: 			    "resource.$part_id.submission"};
  626: 	($response)=&Apache::lonnet::str2hash($response);
  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: 	    }
  637: 	} elsif ($answer[0] eq 'concept') {
  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: 		    }
  646: 		}
  647: 	    }
  648: 	}
  649:     } elsif ($target eq 'edit') {
  650: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
  651:     }
  652:     &Apache::response::end_hintresponse();
  653:     return $result;
  654: }
  655: 1;
  656: __END__

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