File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.75: download - view: text, annotated - select for diffs
Fri Apr 17 01:00:15 2009 UTC (15 years, 1 month ago) by www
Branches: MAIN
CVS tags: bz5969, HEAD, BZ5971-printing-apage, BZ5434-fox
Support for R CAS
- needs testing
- better error handling
- correct blacklist
- correct list of allowed libraries

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

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