File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.78: download - view: text, annotated - select for diffs
Wed Jun 8 20:38:47 2011 UTC (12 years, 11 months ago) by www
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, language_hyphenation_merge, language_hyphenation, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse, BZ4492-feature_Support_horizontal_radioresponse, BZ4492-Support_horizontal_radioresponse
Bug #2020: pre-processing of numerical, formula, and string response
Bug #5100: degrees Fahrenheit, etc

    1: # The LearningOnline Network with CAPA 
    2: # implements the tags that control the hints
    3: #
    4: # $Id: hint.pm,v 1.78 2011/06/08 20:38:47 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: 
   30: 
   31: package Apache::hinttags; 
   32: 
   33: use strict;
   34: use Apache::lonnet();
   35: use capa;
   36: use Apache::caparesponse();
   37: use Apache::lonmaxima();
   38: use Apache::lonr();
   39: use Apache::chemresponse();
   40: use Apache::response();
   41: use Apache::lonlocal;
   42: use Storable qw(dclone);
   43: 
   44: BEGIN {
   45:     &Apache::lonxml::register('Apache::hinttags',('hintgroup','hintpart','numericalhint','stringhint','formulahint','optionhint','radiobuttonhint','mathhint','customhint','reactionhint','organichint'));
   46: }
   47: 
   48: 
   49: @Apache::hint::which=();
   50: sub start_hintgroup {
   51:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   52:     my $skiptoend='0';
   53:     my $result;
   54: 
   55:     if ($target eq 'web') {
   56: 	my $id=$Apache::inputtags::part;
   57: 	my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
   58: 	if ( $numtries eq '') { $numtries = 0; }
   59: 	my $hinttries=&Apache::response::get_response_param($id,"hinttries",1);
   60: 	&Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
   61: 	my $gradestatus=
   62: 	    $Apache::lonhomework::history{"resource.$id.solved"};
   63: 	my $showoncorrect=lc(&Apache::lonxml::get_param('showoncorrect',$parstack,$safeeval));	
   64: 	&Apache::lonxml::debug("onc orrect $showoncorrect, $gradestatus");
   65: 	if ( ($showoncorrect ne 'yes' && &Apache::response::show_answer()) ||
   66: 	     ( $numtries < $hinttries) ) {
   67: 	    &Apache::lonxml::debug("Grabbin all");
   68: 	    &Apache::lonxml::get_all_text("/hintgroup",$parser,$style);
   69: 	}
   70: 	&Apache::lonxml::startredirection;
   71:     } elsif ($target eq 'tex') {
   72: 	$result .= '\keephidden{';
   73:     } elsif ($target eq 'edit') {
   74: 	$result.=&Apache::edit::tag_start($target,$token);
   75: 	$result.=&Apache::edit::select_arg('Show hint even if problem Correct:','showoncorrect',['no','yes'],$token);
   76: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
   77:     } elsif ($target eq 'modified') {
   78: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'showoncorrect');
   79:        if ($constructtag) {
   80:            $result =&Apache::edit::rebuild_tag($token);
   81:        }
   82:     }
   83:     @Apache::hint::which=();
   84:     if (($#Apache::functionplotresponse::failedrules>=0) && ($target eq 'web')) {
   85:         @Apache::hint::which=@Apache::functionplotresponse::failedrules;
   86:         undef @Apache::functionplotresponse::failedrules;
   87:     }
   88:     return $result;
   89: }
   90: 
   91: sub end_hintgroup {
   92:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   93:     my $result;
   94: 
   95:     if ($target eq 'web') {
   96: 	my $id=$Apache::inputtags::part;
   97: 	my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
   98: 	if ( $numtries eq '') { $numtries = 0; }
   99: 	my $hinttries=&Apache::response::get_response_param($id,"hinttries",1);
  100: 	&Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
  101: 	my $hinttext=&Apache::lonxml::endredirection;
  102: 	if ($Apache::lonhomework::type ne 'exam' &&
  103: 	    $numtries >= $hinttries && $hinttext =~/\S/) {
  104: 	    $result='<table bgcolor="#dddddd"><tr><td>'.
  105: 		$hinttext.'</td></tr></table>';
  106: 	}
  107:     } elsif ($target eq 'edit') {
  108: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
  109:     } elsif ($target eq 'tex') {
  110: 	$result .= '}';
  111:     }
  112:     @Apache::hint::which=();
  113:     return $result;
  114: }
  115: 
  116: sub start_numericalhint {
  117:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  118:     #do everything in end, so intervening <responseparams> work
  119:     &Apache::response::start_hintresponse($parstack,$safeeval);
  120:     &Apache::caparesponse::push_answer();
  121:     my $result;
  122:     if ($target eq 'edit') {
  123: 	$result.=&Apache::edit::tag_start($target,$token);
  124: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  125: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  126: 	if ($token->[1] eq 'numericalhint') {
  127: 	    $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
  128: 		&Apache::loncommon::help_open_topic('Physical_Units');
  129: 	    $result.=&Apache::edit::text_arg('Format:','format',$token,4).
  130: 		&Apache::loncommon::help_open_topic('Numerical_Response_Format');
  131: 	} elsif ($token->[1] eq 'formulahint') {
  132: 	    $result.=&Apache::edit::text_arg('Sample Points:','samples',
  133: 					     $token,40).
  134: 	        &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
  135: 	}
  136:         $result.=&Apache::edit::text_arg('Pre-Processor Subroutine:','preprocess',
  137:                                              $token,10);
  138: 	$result.=&Apache::edit::end_row();
  139: 	$result.=&Apache::edit::start_spanning_row();
  140:     } elsif ($target eq 'modified') {
  141: 	my $constructtag;
  142: 	if ($token->[1] eq 'numericalhint') {
  143: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
  144: 						     $safeeval,'name',
  145: 						     'answer','unit','format','preprocess');
  146: 	} elsif ($token->[1] eq 'formulahint') {
  147: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
  148: 						     $safeeval,'name','answer',
  149: 						     'samples','preprocess');
  150: 	}
  151: 	if ($constructtag) {
  152: 	    $result  = &Apache::edit::rebuild_tag($token);
  153: 	}
  154:     } elsif ($target eq 'web') {
  155: 	&Apache::response::reset_params();
  156:     }
  157:     return $result;
  158: }
  159: 
  160: sub end_numericalhint {
  161:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  162:     my $result;
  163:     if ($target eq 'web') {
  164: 	if (!$Apache::lonxml::default_homework_loaded) {
  165: 	    &Apache::lonxml::default_homework_load($safeeval);
  166: 	}
  167: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  168: 	my $hint_name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  169: 	&Apache::response::setup_params('numericalhint',$safeeval);
  170: 
  171: 	my $partid=$Apache::inputtags::part;
  172: 	my $id=$Apache::inputtags::hint[-1];
  173: 	#id submissions occurred under
  174: 	my $submitid=$Apache::inputtags::response[-1];
  175: 
  176: 	my $response = $Apache::lonhomework::history{
  177: 			    "resource.$partid.$submitid.submission"};
  178: 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
  179: 
  180: 	my @args = ('type','tol','sig','ans_fmt','unit','calc','samples');
  181: 	my $args_ref =
  182: 	    &Apache::caparesponse::setup_capa_args($safeeval,$parstack,
  183: 						   \@args,$response);
  184: 
  185: 	my $hideunit=&Apache::response::get_response_param($partid.'_'.$submitid,'turnoffunit');
  186: 	if (lc($hideunit) eq "yes") { delete($$args_ref{'unit'}); }
  187: 
  188: 	if ($$tagstack[-1] eq 'formulahint') {
  189:             if ($$args_ref{'samples'}) {
  190:                 $$args_ref{'type'}='fml';
  191:             } else {
  192:                 $$args_ref{'type'}='math';
  193:             }
  194: 	} elsif ($$tagstack[-1] eq 'numericalhint') {
  195: 	    $$args_ref{'type'}='float';
  196: 	}
  197: 	&Apache::caparesponse::add_in_tag_answer($parstack,$safeeval);
  198: 	my %answer = &Apache::caparesponse::get_answer();
  199: 	my (@final_awards,@final_msgs,@ans_names);
  200: 	foreach my $ans_name (keys(%answer)) {
  201: 	    &Apache::lonxml::debug(" doing $ans_name with ".join(':',@{ $answer{$ans_name}{'answers'} }));
  202: 	
  203: 	    ${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=
  204: 		dclone($answer{$ans_name});
  205: 	    &Apache::caparesponse::setup_capa_response($args_ref,$response);
  206: 	    my ($result,@msgs) = 
  207: 		&Apache::run::run("&caparesponse_check_list()",$safeeval);
  208: 	    &Apache::lonxml::debug("checking $ans_name $result with $response");
  209: 	    &Apache::lonxml::debug('msgs are '.join(':',@msgs));
  210: 	    my ($awards)=split(/:/,$result);
  211: 	    my @awards= split(/,/,$awards);
  212: 	    my ($ad, $msg) = 
  213: 		&Apache::inputtags::finalizeawards(\@awards,\@msgs);
  214: 	    push(@final_awards,$ad);
  215: 	    push(@final_msgs,$msg);
  216: 	    push(@ans_names,$ans_name);
  217: 	}
  218: 	my ($ad, $msg, $ans_name) =
  219: 	    &Apache::inputtags::finalizeawards(\@final_awards,
  220: 					       \@final_msgs,
  221: 					       \@ans_names,1);
  222: 	if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') {
  223: 	    push(@Apache::hint::which,$hint_name);	
  224: 	}
  225: 	$result='';
  226:     } elsif ($target eq 'meta') {
  227: 	$result=&Apache::response::meta_package_write($token->[1]);
  228:     } elsif ($target eq 'edit') {
  229: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
  230:     }
  231:     &Apache::caparesponse::pop_answer();
  232:     &Apache::response::end_hintresponse();
  233:     return $result;
  234: }
  235: 
  236: sub start_formulahint {
  237:     return &start_numericalhint(@_);
  238: }
  239: 
  240: sub end_formulahint {
  241:     return &end_numericalhint(@_);
  242: }
  243: 
  244: sub start_mathhint {
  245:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  246:     #do everything in end, so intervening <responseparams> and <answer> work
  247:     &Apache::response::start_hintresponse($parstack,$safeeval);
  248:     &Apache::lonxml::register('Apache::response',('answer'));
  249:     my $result;
  250:     if ($target eq 'edit') {
  251:         $result.=&Apache::edit::tag_start($target,$token);
  252:         $result.=&Apache::edit::text_arg('Name:','name',$token);
  253:         $result.=&Apache::edit::select_arg('Algebra System:',
  254:                                            'cas',
  255:                                            ['maxima','R'],
  256:                                            $token);
  257:         $result.=&Apache::edit::text_arg('Argument Array:',
  258:                                          'args',$token);
  259:         $result.=&Apache::edit::end_row();
  260:         $result.=&Apache::edit::start_spanning_row();
  261:     } elsif ($target eq 'modified') {
  262:         my $constructtag;
  263:         $constructtag=&Apache::edit::get_new_args($token,$parstack,
  264:                                                   $safeeval,'name','cas',
  265:                                                   'args');
  266:         $result  = &Apache::edit::rebuild_tag($token);
  267:     } elsif ($target eq 'web') {
  268:         &Apache::response::reset_params();
  269:     }
  270:     return $result;
  271: }
  272: 
  273: sub end_mathhint {
  274:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  275:     my $result;
  276:     if ($target eq 'web') {
  277:         if (!$Apache::lonxml::default_homework_loaded) {
  278:             &Apache::lonxml::default_homework_load($safeeval);
  279:         }
  280: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  281:         &Apache::response::setup_params('mathhint',$safeeval);
  282:         my $partid=$Apache::inputtags::part;
  283:         my $submitid=$Apache::inputtags::response[-1];
  284:         my $response = $Apache::lonhomework::history{
  285:                             "resource.$partid.$submitid.submission"};
  286: 	
  287: 	my $cas = &Apache::lonxml::get_param('cas',$parstack,$safeeval);
  288: 	my $award;
  289: 	if ($cas eq 'maxima') {
  290: 	    my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
  291: 	    $award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer[-1],$response,$args);
  292: 	}
  293:         if ($cas eq 'R') {
  294:             my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
  295:             $award=&Apache::lonr::r_run($Apache::response::custom_answer[-1],$response,$args);
  296:         }
  297:         if ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS') {
  298:             push (@Apache::hint::which,$name);
  299:         }
  300:         $result='';
  301:     } elsif ($target eq 'meta') {
  302:         $result=&Apache::response::meta_package_write($token->[1]);
  303:     } elsif ($target eq 'edit') {
  304:         $result.=&Apache::edit::end_row().&Apache::edit::end_table();
  305:     }
  306:     pop(@Apache::response::custom_answer);
  307:     pop(@Apache::response::custom_answer_type);
  308:     &Apache::response::end_hintresponse();
  309:     return $result;
  310: }
  311: 
  312: sub start_customhint {
  313:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  314:     #do everything in end, so intervening <responseparams> and <answer> work
  315:     &Apache::response::start_hintresponse($parstack,$safeeval);
  316:     &Apache::lonxml::register('Apache::response',('answer'));
  317:     my $result;
  318:     if ($target eq 'edit') {
  319:         $result.=&Apache::edit::tag_start($target,$token);
  320:         $result.=&Apache::edit::text_arg('Name:','name',$token);
  321:         $result.=&Apache::edit::end_row();
  322:         $result.=&Apache::edit::start_spanning_row();
  323:     } elsif ($target eq 'modified') {
  324:         my $constructtag;
  325:         $constructtag=&Apache::edit::get_new_args($token,$parstack,
  326:                                                   $safeeval,'name');
  327:         $result  = &Apache::edit::rebuild_tag($token);
  328:     } elsif ($target eq 'web') {
  329:         &Apache::response::reset_params();
  330:     }
  331:     return $result;
  332: }
  333: 
  334: sub end_customhint {
  335:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  336:     my $result;
  337:     if ($target eq 'web') {
  338:         if (!$Apache::lonxml::default_homework_loaded) {
  339:             &Apache::lonxml::default_homework_load($safeeval);
  340:         }
  341: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  342:         &Apache::response::setup_params('customhint',$safeeval);
  343:         my $partid=$Apache::inputtags::part;
  344:         my $submitid=$Apache::inputtags::response[-1];
  345:         my $response = $Apache::lonhomework::history{
  346:                             "resource.$partid.$submitid.submission"};
  347:         my $award;	
  348: 	if ( $response =~ /[^\s]/ && 
  349: 	     $Apache::response::custom_answer_type[-1] eq 'loncapa/perl') {
  350: 	    if (!$Apache::lonxml::default_homework_loaded) {
  351: 		&Apache::lonxml::default_homework_load($safeeval);
  352: 	    }
  353: 	    ${$safeeval->varglob('LONCAPA::customresponse_submission')}=
  354: 		$response;
  355: 	    
  356: 	    $award = &Apache::run::run('{ my $submission=$LONCAPA::customresponse_submission;'.$Apache::response::custom_answer[-1].'}',$safeeval);
  357: 	}
  358:         if ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS') {
  359:             push (@Apache::hint::which,$name);
  360:         }
  361:         $result='';
  362:     } elsif ($target eq 'meta') {
  363:         $result=&Apache::response::meta_package_write($token->[1]);
  364:     } elsif ($target eq 'edit') {
  365:         $result.=&Apache::edit::end_row().&Apache::edit::end_table();
  366:     }
  367:     pop(@Apache::response::custom_answer);
  368:     pop(@Apache::response::custom_answer_type);
  369:     &Apache::response::end_hintresponse();
  370:     return $result;
  371: }
  372: 
  373: sub start_stringhint {
  374:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  375:     #do everything in end, so intervening <responseparams> work
  376:     &Apache::response::start_hintresponse($parstack,$safeeval);
  377:     my $result;
  378:     if ($target eq 'edit') {
  379: 	$result.=&Apache::edit::tag_start($target,$token);
  380: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  381: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  382: 	$result.=&Apache::edit::select_arg('Type:','type',
  383: 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
  384: 			  ['mc','Case Insensitive, Any Order'],
  385: 			  ['re','Regular Expression']],$token);
  386:         $result.=&Apache::edit::text_arg('Pre-Processor Subroutine:','preprocess',
  387:                                              $token,10);
  388: 	$result.=&Apache::edit::end_row();
  389: 	$result.=&Apache::edit::start_spanning_row();
  390:     } elsif ($target eq 'modified') {
  391: 	my $constructtag;
  392: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
  393: 						  $safeeval,'name','answer',
  394: 						  'type','preprocess');
  395: 	$result  = &Apache::edit::rebuild_tag($token);
  396:     } elsif ($target eq 'web') {
  397: 	&Apache::response::reset_params();
  398:     }
  399:     return $result;
  400: }
  401: 
  402: sub end_stringhint {
  403:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  404:     my $result;
  405:     if ($target eq 'web') {
  406: 	if (!$Apache::lonxml::default_homework_loaded) {
  407: 	    &Apache::lonxml::default_homework_load($safeeval);
  408: 	}
  409: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  410: 	my $hint_name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  411: 	&Apache::response::setup_params('stringhint',$safeeval);
  412: 	my $partid=$Apache::inputtags::part;
  413: 	my $id=$Apache::inputtags::hint[-1];
  414: 	#id submissions occurred under
  415: 	my $submitid=$Apache::inputtags::response[-1];
  416: 	my $response = $Apache::lonhomework::history{
  417: 			    "resource.$partid.$submitid.submission"};
  418: 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
  419: 	my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  420: 	my ($ad,$msg);
  421: 	if ($type eq 're' ) {
  422: 	    my $answer=&Apache::lonxml::get_param('answer',$parstack,
  423: 						  $safeeval);
  424: 	    ${$safeeval->varglob('LONCAPA::response')}=$response;
  425: 	    my $compare='=';
  426: 	    if ($answer=~/^\s*NOT\s*/) {
  427: 		$answer=~s/^\s*NOT\s*//;
  428: 		$compare='!';
  429: 	    }
  430: 	    my $test='$LONCAPA::response'.$compare.'~m'.$answer;
  431: 	    &Apache::lonxml::debug("test $test");
  432: 	    $result = &Apache::run::run("return $test",$safeeval);
  433: 	    &Apache::lonxml::debug("current $response");
  434: 	    &Apache::lonxml::debug("current $answer");
  435: 	    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
  436: 	} else { 
  437: 	    my @args = ('type');
  438: 	    my $args_ref = 
  439: 		&Apache::caparesponse::setup_capa_args($safeeval,$parstack,
  440: 						       \@args,$response);
  441:             if ($$args_ref{'type'} eq '') {
  442:                 $$args_ref{'type'} = 'ci';
  443:             }
  444: 	    &Apache::caparesponse::add_in_tag_answer($parstack,$safeeval);
  445: 	    my (@final_awards,@final_msgs,@ans_names);
  446: 	    my %answer = &Apache::caparesponse::get_answer();
  447: 	    foreach my $ans_name (keys(%answer)) {
  448: 		&Apache::lonxml::debug(" doing $ans_name with ".join(':',@{ $answer{$ans_name}{'answers'} }));
  449: 		${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$ans_name});
  450: 		my ($result, @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
  451: 		&Apache::lonxml::debug('msgs are'.join(':',@msgs));
  452: 		my ($awards) = split(/:/,$result);
  453: 		my (@awards) = split(/,/,$awards);
  454: 		($ad,$msg) = 
  455: 		    &Apache::inputtags::finalizeawards(\@awards,\@msgs);
  456: 		push(@final_awards,$ad);
  457: 		push(@final_msgs,$msg);
  458: 		push(@ans_names,$ans_name);
  459: 		&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
  460: 	    }
  461: 	    my ($ad, $msg, $ans_name) = 
  462: 		&Apache::inputtags::finalizeawards(\@final_awards,
  463: 						   \@final_msgs,
  464: 						   \@ans_names,1);
  465: 	}
  466: 	if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { 
  467: 	    push(@Apache::hint::which,$hint_name);
  468: 	}
  469: 	$result='';
  470:     } elsif ($target eq 'meta') {
  471: 	$result=&Apache::response::meta_package_write($token->[1]);
  472:     } elsif ($target eq 'edit') {
  473: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
  474:     }
  475:     &Apache::response::end_hintresponse();
  476:     return $result;
  477: }
  478: 
  479: # reactionhint
  480: 
  481: sub start_reactionhint {
  482:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  483:     #do everything in end, so intervening <responseparams> work
  484:     &Apache::response::start_hintresponse($parstack,$safeeval);
  485:     my $result;
  486:     if ($target eq 'edit') {
  487:         $result.=&Apache::edit::tag_start($target,$token);
  488:         $result.=&Apache::edit::text_arg('Name:','name',$token);
  489:         $result.=&Apache::edit::text_arg('Answer:','answer',$token);
  490:         $result.=&Apache::edit::end_row();
  491:         $result.=&Apache::edit::start_spanning_row();
  492:     } elsif ($target eq 'modified') {
  493:         my $constructtag;
  494:         $constructtag=&Apache::edit::get_new_args($token,$parstack,
  495:                                                   $safeeval,'name','answer',
  496:                                                   );
  497:         $result  = &Apache::edit::rebuild_tag($token);
  498:     } elsif ($target eq 'web') {
  499:         &Apache::response::reset_params();
  500:     }
  501:     return $result;
  502: }
  503: 
  504: sub end_reactionhint {
  505:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  506:     my $result;
  507:     if ($target eq 'web') {
  508:         if (!$Apache::lonxml::default_homework_loaded) {
  509:             &Apache::lonxml::default_homework_load($safeeval);
  510:         }
  511:         my $hint_name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  512:         &Apache::response::setup_params('reactionhint',$safeeval);
  513:         my $partid=$Apache::inputtags::part;
  514:         my $id=$Apache::inputtags::hint[-1];
  515:         #id submissions occurred under
  516:         my $submitid=$Apache::inputtags::response[-1];
  517:         my $response = $Apache::lonhomework::history{
  518:                             "resource.$partid.$submitid.submission"};
  519:         &Apache::lonxml::debug("hintgroup is using $response<br />\n");
  520:         my $answer=&Apache::lonxml::get_param('answer',$parstack,
  521:                                                   $safeeval);
  522:         &Apache::lonxml::debug("current $response");
  523:         &Apache::lonxml::debug("current $answer");
  524:         if (&Apache::chemresponse::chem_standard_order($response) eq 
  525:             &Apache::chemresponse::chem_standard_order($answer)) {
  526:             push(@Apache::hint::which,$hint_name);
  527:         }
  528:         $result='';
  529:     } elsif ($target eq 'meta') {
  530:         $result=&Apache::response::meta_package_write($token->[1]);
  531:     } elsif ($target eq 'edit') {
  532:         $result.=&Apache::edit::end_row().&Apache::edit::end_table();
  533:     }
  534:     &Apache::response::end_hintresponse();
  535:     return $result;
  536: }
  537: 
  538: # organichint
  539: 
  540: sub start_organichint {
  541:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  542:     #do everything in end, so intervening <responseparams> work
  543:     &Apache::response::start_hintresponse($parstack,$safeeval);
  544:     my $result;
  545:     if ($target eq 'edit') {
  546:         $result.=&Apache::edit::tag_start($target,$token);
  547:         $result.=&Apache::edit::text_arg('Name:','name',$token);
  548:         $result.=&Apache::edit::text_arg('Answer:','answer',$token);
  549:         $result.=&Apache::edit::end_row();
  550:         $result.=&Apache::edit::start_spanning_row();
  551:     } elsif ($target eq 'modified') {
  552:         my $constructtag;
  553:         $constructtag=&Apache::edit::get_new_args($token,$parstack,
  554:                                                   $safeeval,'name','answer',
  555:                                                   );
  556:         $result  = &Apache::edit::rebuild_tag($token);
  557:     } elsif ($target eq 'web') {
  558:         &Apache::response::reset_params();
  559:     }
  560:     return $result;
  561: }
  562: 
  563: sub end_organichint {
  564:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  565:     my $result;
  566:     if ($target eq 'web') {
  567:         if (!$Apache::lonxml::default_homework_loaded) {
  568:             &Apache::lonxml::default_homework_load($safeeval);
  569:         }
  570:         my $hint_name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  571:         &Apache::response::setup_params('organichint',$safeeval);
  572:         my $partid=$Apache::inputtags::part;
  573:         my $id=$Apache::inputtags::hint[-1];
  574:         #id submissions occurred under
  575:         my $submitid=$Apache::inputtags::response[-1];
  576:         my $response = $Apache::lonhomework::history{
  577:                             "resource.$partid.$submitid.submission"};
  578:         &Apache::lonxml::debug("hintgroup is using $response<br />\n");
  579:         my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  580:         my %previous = &Apache::response::check_for_previous($response,$partid,$id);
  581:         foreach my $answer (@answers) {
  582:             &Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
  583:             if ($response eq $answer) {
  584:                push(@Apache::hint::which,$hint_name);
  585:                last;
  586:             }
  587:         }
  588:         $result='';
  589:     } elsif ($target eq 'meta') {
  590:         $result=&Apache::response::meta_package_write($token->[1]);
  591:     } elsif ($target eq 'edit') {
  592:         $result.=&Apache::edit::end_row().&Apache::edit::end_table();
  593:     }
  594:     &Apache::response::end_hintresponse();
  595:     return $result;
  596: }
  597: 
  598: 
  599: # a part shows if it is on, if no specific parts are on, then default shows
  600: sub start_hintpart {
  601:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  602: 
  603:     my $show ='0';
  604:     my $result = '';
  605:     if ($target eq 'web') {
  606: 	my $on= &Apache::lonxml::get_param('on',$parstack,$safeeval);
  607: 	&Apache::lonxml::debug("hintpart sees $on and ,$#Apache::hint::which");
  608: 	if ( $on eq 'default' && $#Apache::hint::which == '-1') {
  609: 	    $show=1;
  610: 	} else {
  611: 	    my $which;
  612: 	    foreach $which (@Apache::hint::which) { if ($which eq $on) { $show = 1; last } }
  613: 	}
  614: 	if (!$show) {
  615: 	    &Apache::lonxml::get_all_text("/hintpart",$parser,$style);
  616: 	}
  617:     } elsif ($target eq 'grade') {
  618: 	&Apache::lonxml::get_all_text("/hintpart",$parser,$style);
  619:     } elsif ($target eq 'edit') {
  620: 	$result.= &Apache::edit::tag_start($target,$token);
  621: 	$result.= &Apache::edit::text_arg('On:','on',$token);
  622: 	$result.= &Apache::edit::end_row();
  623: 	$result.= &Apache::edit::start_spanning_row();
  624:     } elsif ($target eq 'modified') {
  625: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  626: 						     $safeeval,'on');
  627: 	if ($constructtag) {
  628: 	    $result = &Apache::edit::rebuild_tag($token);
  629: 	}
  630:     }
  631:     return $result;
  632: }
  633: 
  634: sub end_hintpart {
  635:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  636:     my $result;
  637:     if ($target eq 'edit') {
  638: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
  639:     }
  640:     return $result;
  641: }
  642: 
  643: sub start_optionhint {
  644:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  645:     my $result;
  646:     &Apache::response::start_hintresponse($parstack,$safeeval);
  647:     if ($target eq 'edit') {
  648: 	$result.=&Apache::edit::tag_start($target,$token);
  649: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  650: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token,40);
  651: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,50);
  652:     } elsif ($target eq 'modified') {
  653: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  654: 						     $safeeval,'name',
  655: 						     'answer','concept');
  656: 	if ($constructtag) {
  657: 	    $result  = &Apache::edit::rebuild_tag($token);
  658: 	}
  659:     } elsif ($target eq 'meta') {
  660: 	$result=&Apache::response::meta_package_write('numericalhint');
  661:     }
  662:     return $result;
  663: }
  664: 
  665: sub end_optionhint {
  666:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  667:     my $result;
  668:     if ($target eq 'web') {
  669: 	my ($foilmatch,$conceptmatch)=(-1,-1);
  670: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  671: 	my $partid=$Apache::inputtags::part;
  672: 	#id submissions occurred under
  673: 	my $submitid=$Apache::inputtags::response[-1];
  674: 	my $part_id="$partid.$submitid";
  675: 	my %answer;
  676: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  677: 	if ($answer) {
  678: 	    eval('%answer ='.$answer);
  679: 	    &Apache::lonxml::debug("answwer hash");
  680: 	    &Apache::lonhomework::showhash(%answer);
  681: 	    my $response = $Apache::lonhomework::history{
  682: 				            "resource.$part_id.submission"};
  683: 	    my %response=&Apache::lonnet::str2hash($response);
  684: 	    &Apache::lonhomework::showhash(%response);
  685: 	    foreach my $foil (keys(%answer)) {
  686: 		$foilmatch=1;
  687: 		if ($answer{$foil} ne $response{$foil}) {$foilmatch=0;last;}
  688: 	    }
  689: 	}
  690: 	my %concept;
  691: 	my $constr=&Apache::lonxml::get_param('concept',$parstack,$safeeval);
  692: 	if ( $constr ) { eval('%concept ='.$constr);	}
  693: 	my $response = $Apache::lonhomework::history{
  694: 					"resource.$part_id.submissiongrading"};
  695: 	my %response=&Apache::lonnet::str2hash($response);
  696: 	foreach my $concept (keys(%concept)) {
  697: 	    my $compare;
  698: 	    if ($concept{$concept} eq 'correct') {$compare=1}else{$compare=0}
  699: 	    $conceptmatch=1;
  700: 	    if (ref($Apache::hint::option{"$part_id.concepts"})) {
  701: 		foreach my $foil (@{ $Apache::hint::option{"$part_id.concept.$concept"} }) {
  702: 		    &Apache::lonxml::debug("compare -$foil- -$response{$foil}-$compare-");
  703: 		    if ( exists($response{$foil}) && 
  704: 			 $response{$foil} ne $compare) {$conceptmatch=0;last;}
  705: 		}
  706: 	    } else {
  707: 		$conceptmatch=0;
  708: 	    }
  709: 	    if ($conceptmatch eq '0') { last; }
  710: 	}
  711: 	if ( ($conceptmatch eq '-1' || $conceptmatch eq '1') &&
  712: 	     ($foilmatch    eq '-1' || $foilmatch    eq '1') ) {
  713: 	    push(@Apache::hint::which,$name);
  714: 	}
  715:     } elsif ($target eq 'edit') {
  716: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
  717:     }
  718:     &Apache::response::end_hintresponse();
  719:     return $result;
  720: }
  721: 
  722: sub start_radiobuttonhint {
  723:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  724:     my $result;
  725:     &Apache::response::start_hintresponse($parstack,$safeeval);
  726:     if ($target eq 'edit') {
  727: 	$result.=&Apache::edit::tag_start($target,$token);
  728: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  729: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  730:     } elsif ($target eq 'modified') {
  731: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  732: 						     $safeeval,'name',
  733: 						     'answer');
  734: 	if ($constructtag) {
  735: 	    $result  = &Apache::edit::rebuild_tag($token);
  736: 	}
  737:     } elsif ($target eq 'meta') {
  738: 	$result=&Apache::response::meta_package_write('numericalhint');
  739:     }
  740:     return $result;
  741: }
  742: 
  743: sub end_radiobuttonhint {
  744:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  745:     my $result;
  746:     if ($target eq 'web') {
  747: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  748: 	my @answer;
  749: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  750: 	eval('@answer ='.$answer);
  751: 	my $partid=$Apache::inputtags::part;
  752: 	#id submissions occurred under
  753: 	my $submitid=$Apache::inputtags::response[-1];
  754: 	my $part_id="$partid.$submitid";
  755: 	my $response = $Apache::lonhomework::history{
  756: 			    "resource.$part_id.submission"};
  757: 	($response)=&Apache::lonnet::str2hash($response);
  758: 	&Apache::lonxml::debug("response is $response");
  759: 	
  760: 	if ($answer[0] eq 'foil') {
  761: 	    shift(@answer);
  762: 	    foreach my $answer (@answer) {
  763: 		if ($response eq $answer) {
  764: 		    push (@Apache::hint::which,$name);
  765: 		    last;
  766: 		}
  767: 	    }
  768: 	} elsif ($answer[0] eq 'concept') {
  769: 	    shift(@answer);
  770: 	    foreach my $answer (@answer) {
  771: 		if (ref($Apache::hint::radiobutton{"$part_id.concept.".$answer})) {
  772: 		    my @names=@{ $Apache::hint::radiobutton{"$part_id.concept.".$answer} };
  773: 		    if (grep(/^\Q$response\E$/,@names)) {
  774: 			push(@Apache::hint::which,$name);
  775: 			last;
  776: 		    }
  777: 		}
  778: 	    }
  779: 	}
  780:     } elsif ($target eq 'edit') {
  781: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
  782:     }
  783:     &Apache::response::end_hintresponse();
  784:     return $result;
  785: }
  786: 1;
  787: __END__
  788: 
  789: 
  790: =head1 NAME
  791: 
  792: Apache::hinttags
  793: 
  794: =head1 SYNOPSIS
  795: 
  796: This handler coordinates the delivery of hints to students working on LON-CAPA problems and assignments.
  797: 
  798: This is part of the LearningOnline Network with CAPA project
  799: described at http://www.lon-capa.org.
  800: 
  801: =head1 SUBROUTINES
  802: 
  803: =over
  804: 
  805: =item start_hintgroup()
  806: 
  807: =item end_hintgroup()
  808: 
  809: =item start_numericalhint()
  810: 
  811: =item end_numericalhint()
  812: 
  813: =item start_formulahint()
  814: 
  815: =item end_formulahint()
  816: 
  817: =item start_mathhint()
  818: 
  819: =item end_mathhint()
  820: 
  821: =item start_customhint()
  822: 
  823: =item end_customhint()
  824: 
  825: =item start_stringhint()
  826: 
  827: =item end_stringhint()
  828: 
  829: =item start_hintpart()
  830: 
  831: =item end_hintpart()
  832: 
  833: =item start_optionhint()
  834: 
  835: =item end_optionhint()
  836: 
  837: =item start_radiobuttonhint()
  838: 
  839: =item end_radiobuttonhint()
  840: 
  841: =back
  842: 
  843: =cut

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