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

1.21      albertel    1: # The LearningOnline Network with CAPA 
                      2: # implements the tags that control the hints
                      3: #
1.78    ! www         4: # $Id: hint.pm,v 1.77 2010/11/13 03:22:58 www Exp $
1.21      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
1.33      albertel   10: # LON-CAPA me&aree software; you can redistribute it and/or modify
1.21      albertel   11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
1.73      jms        29: 
                     30: 
1.1       albertel   31: package Apache::hinttags; 
                     32: 
                     33: use strict;
1.65      albertel   34: use Apache::lonnet();
1.6       albertel   35: use capa;
1.65      albertel   36: use Apache::caparesponse();
1.63      www        37: use Apache::lonmaxima();
1.75      www        38: use Apache::lonr();
1.76      www        39: use Apache::chemresponse();
1.63      www        40: use Apache::response();
1.53      albertel   41: use Apache::lonlocal;
1.65      albertel   42: use Storable qw(dclone);
1.1       albertel   43: 
1.25      harris41   44: BEGIN {
1.76      www        45:     &Apache::lonxml::register('Apache::hinttags',('hintgroup','hintpart','numericalhint','stringhint','formulahint','optionhint','radiobuttonhint','mathhint','customhint','reactionhint','organichint'));
1.1       albertel   46: }
                     47: 
1.2       albertel   48: 
1.15      albertel   49: @Apache::hint::which=();
1.1       albertel   50: sub start_hintgroup {
1.39      albertel   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; }
1.52      albertel   59: 	my $hinttries=&Apache::response::get_response_param($id,"hinttries",1);
1.39      albertel   60: 	&Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
1.53      albertel   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");
1.58      albertel   65: 	if ( ($showoncorrect ne 'yes' && &Apache::response::show_answer()) ||
1.53      albertel   66: 	     ( $numtries < $hinttries) ) {
                     67: 	    &Apache::lonxml::debug("Grabbin all");
1.61      albertel   68: 	    &Apache::lonxml::get_all_text("/hintgroup",$parser,$style);
1.39      albertel   69: 	}
1.40      albertel   70: 	&Apache::lonxml::startredirection;
1.39      albertel   71:     } elsif ($target eq 'tex') {
                     72: 	$result .= '\keephidden{';
1.53      albertel   73:     } elsif ($target eq 'edit') {
                     74: 	$result.=&Apache::edit::tag_start($target,$token);
1.72      bisitz     75: 	$result.=&Apache::edit::select_arg('Show hint even if problem Correct:','showoncorrect',['no','yes'],$token);
1.53      albertel   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:        }
1.20      albertel   82:     }
1.39      albertel   83:     @Apache::hint::which=();
1.77      www        84:     if (($#Apache::functionplotresponse::failedrules>=0) && ($target eq 'web')) {
                     85:         @Apache::hint::which=@Apache::functionplotresponse::failedrules;
                     86:         undef @Apache::functionplotresponse::failedrules;
                     87:     }
1.39      albertel   88:     return $result;
1.1       albertel   89: }
                     90: 
                     91: sub end_hintgroup {
1.39      albertel   92:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     93:     my $result;
1.20      albertel   94: 
1.39      albertel   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; }
1.52      albertel   99: 	my $hinttries=&Apache::response::get_response_param($id,"hinttries",1);
1.39      albertel  100: 	&Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
1.40      albertel  101: 	my $hinttext=&Apache::lonxml::endredirection;
                    102: 	if ($Apache::lonhomework::type ne 'exam' &&
1.41      albertel  103: 	    $numtries >= $hinttries && $hinttext =~/\S/) {
1.40      albertel  104: 	    $result='<table bgcolor="#dddddd"><tr><td>'.
                    105: 		$hinttext.'</td></tr></table>';
1.38      albertel  106: 	}
1.39      albertel  107:     } elsif ($target eq 'edit') {
1.62      albertel  108: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
1.39      albertel  109:     } elsif ($target eq 'tex') {
                    110: 	$result .= '}';
1.19      albertel  111:     }
1.39      albertel  112:     @Apache::hint::which=();
                    113:     return $result;
1.3       albertel  114: }
                    115: 
                    116: sub start_numericalhint {
1.39      albertel  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);
1.66      albertel  120:     &Apache::caparesponse::push_answer();
1.39      albertel  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') {
1.65      albertel  132: 	    $result.=&Apache::edit::text_arg('Sample Points:','samples',
                    133: 					     $token,40).
                    134: 	        &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
1.39      albertel  135: 	}
1.78    ! www       136:         $result.=&Apache::edit::text_arg('Pre-Processor Subroutine:','preprocess',
        !           137:                                              $token,10);
1.39      albertel  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',
1.78    ! www       145: 						     'answer','unit','format','preprocess');
1.39      albertel  146: 	} elsif ($token->[1] eq 'formulahint') {
                    147: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    148: 						     $safeeval,'name','answer',
1.78    ! www       149: 						     'samples','preprocess');
1.39      albertel  150: 	}
                    151: 	if ($constructtag) {
                    152: 	    $result  = &Apache::edit::rebuild_tag($token);
                    153: 	}
                    154:     } elsif ($target eq 'web') {
                    155: 	&Apache::response::reset_params();
1.28      albertel  156:     }
1.39      albertel  157:     return $result;
1.3       albertel  158: }
                    159: 
                    160: sub end_numericalhint {
1.39      albertel  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']);
1.65      albertel  168: 	my $hint_name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.48      albertel  169: 	&Apache::response::setup_params('numericalhint',$safeeval);
1.65      albertel  170: 
1.39      albertel  171: 	my $partid=$Apache::inputtags::part;
1.60      albertel  172: 	my $id=$Apache::inputtags::hint[-1];
1.69      bisitz    173: 	#id submissions occurred under
1.60      albertel  174: 	my $submitid=$Apache::inputtags::response[-1];
1.65      albertel  175: 
1.39      albertel  176: 	my $response = $Apache::lonhomework::history{
1.18      albertel  177: 			    "resource.$partid.$submitid.submission"};
1.39      albertel  178: 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
1.65      albertel  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: 
1.54      albertel  185: 	my $hideunit=&Apache::response::get_response_param($partid.'_'.$submitid,'turnoffunit');
1.51      albertel  186: 	if (lc($hideunit) eq "yes") { delete($$args_ref{'unit'}); }
1.65      albertel  187: 
1.43      albertel  188: 	if ($$tagstack[-1] eq 'formulahint') {
1.67      www       189:             if ($$args_ref{'samples'}) {
                    190:                 $$args_ref{'type'}='fml';
                    191:             } else {
                    192:                 $$args_ref{'type'}='math';
                    193:             }
1.43      albertel  194: 	} elsif ($$tagstack[-1] eq 'numericalhint') {
1.51      albertel  195: 	    $$args_ref{'type'}='float';
1.39      albertel  196: 	}
1.65      albertel  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) = 
1.71      raeburn   207: 		&Apache::run::run("&caparesponse_check_list()",$safeeval);
1.65      albertel  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: 	}
1.39      albertel  225: 	$result='';
                    226:     } elsif ($target eq 'meta') {
                    227: 	$result=&Apache::response::meta_package_write($token->[1]);
                    228:     } elsif ($target eq 'edit') {
1.62      albertel  229: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
1.18      albertel  230:     }
1.66      albertel  231:     &Apache::caparesponse::pop_answer();
1.39      albertel  232:     &Apache::response::end_hintresponse();
                    233:     return $result;
1.28      albertel  234: }
                    235: 
1.51      albertel  236: sub start_formulahint {
1.35      albertel  237:     return &start_numericalhint(@_);
1.28      albertel  238: }
                    239: 
1.51      albertel  240: sub end_formulahint {
1.35      albertel  241:     return &end_numericalhint(@_);
1.28      albertel  242: }
                    243: 
1.63      www       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',
1.75      www       255:                                            ['maxima','R'],
1.63      www       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: 	}
1.75      www       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:         }
1.63      www       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: 
1.64      www       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: 
1.51      albertel  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);
1.78    ! www       386:         $result.=&Apache::edit::text_arg('Pre-Processor Subroutine:','preprocess',
        !           387:                                              $token,10);
1.51      albertel  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',
1.78    ! www       394: 						  'type','preprocess');
1.51      albertel  395: 	$result  = &Apache::edit::rebuild_tag($token);
                    396:     } elsif ($target eq 'web') {
                    397: 	&Apache::response::reset_params();
                    398:     }
                    399:     return $result;
1.28      albertel  400: }
                    401: 
1.51      albertel  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']);
1.65      albertel  410: 	my $hint_name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.51      albertel  411: 	&Apache::response::setup_params('stringhint',$safeeval);
                    412: 	my $partid=$Apache::inputtags::part;
1.60      albertel  413: 	my $id=$Apache::inputtags::hint[-1];
1.69      bisitz    414: 	#id submissions occurred under
1.60      albertel  415: 	my $submitid=$Apache::inputtags::response[-1];
1.51      albertel  416: 	my $response = $Apache::lonhomework::history{
                    417: 			    "resource.$partid.$submitid.submission"};
                    418: 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
1.65      albertel  419: 	my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
1.51      albertel  420: 	my ($ad,$msg);
                    421: 	if ($type eq 're' ) {
1.65      albertel  422: 	    my $answer=&Apache::lonxml::get_param('answer',$parstack,
                    423: 						  $safeeval);
1.51      albertel  424: 	    ${$safeeval->varglob('LONCAPA::response')}=$response;
1.57      albertel  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);
1.51      albertel  433: 	    &Apache::lonxml::debug("current $response");
                    434: 	    &Apache::lonxml::debug("current $answer");
                    435: 	    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
                    436: 	} else { 
1.65      albertel  437: 	    my @args = ('type');
                    438: 	    my $args_ref = 
                    439: 		&Apache::caparesponse::setup_capa_args($safeeval,$parstack,
                    440: 						       \@args,$response);
1.71      raeburn   441:             if ($$args_ref{'type'} eq '') {
                    442:                 $$args_ref{'type'} = 'ci';
                    443:             }
1.65      albertel  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});
1.71      raeburn   450: 		my ($result, @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
1.65      albertel  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");
1.51      albertel  460: 	    }
1.65      albertel  461: 	    my ($ad, $msg, $ans_name) = 
                    462: 		&Apache::inputtags::finalizeawards(\@final_awards,
                    463: 						   \@final_msgs,
                    464: 						   \@ans_names,1);
1.51      albertel  465: 	}
                    466: 	if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { 
1.65      albertel  467: 	    push(@Apache::hint::which,$hint_name);
1.51      albertel  468: 	}
                    469: 	$result='';
                    470:     } elsif ($target eq 'meta') {
                    471: 	$result=&Apache::response::meta_package_write($token->[1]);
                    472:     } elsif ($target eq 'edit') {
1.62      albertel  473: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
1.51      albertel  474:     }
                    475:     &Apache::response::end_hintresponse();
                    476:     return $result;
1.1       albertel  477: }
                    478: 
1.76      www       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: 
1.2       albertel  599: # a part shows if it is on, if no specific parts are on, then default shows
1.1       albertel  600: sub start_hintpart {
1.39      albertel  601:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.15      albertel  602: 
1.39      albertel  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) {
1.61      albertel  615: 	    &Apache::lonxml::get_all_text("/hintpart",$parser,$style);
1.39      albertel  616: 	}
                    617:     } elsif ($target eq 'grade') {
1.61      albertel  618: 	&Apache::lonxml::get_all_text("/hintpart",$parser,$style);
1.39      albertel  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: 	}
1.20      albertel  630:     }
1.39      albertel  631:     return $result;
1.1       albertel  632: }
                    633: 
                    634: sub end_hintpart {
1.29      albertel  635:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    636:     my $result;
1.62      albertel  637:     if ($target eq 'edit') {
                    638: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
                    639:     }
1.29      albertel  640:     return $result;
                    641: }
                    642: 
                    643: sub start_optionhint {
                    644:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    645:     my $result;
1.30      albertel  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:     }
1.29      albertel  662:     return $result;
                    663: }
                    664: 
                    665: sub end_optionhint {
                    666:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    667:     my $result;
1.30      albertel  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;
1.69      bisitz    672: 	#id submissions occurred under
1.60      albertel  673: 	my $submitid=$Apache::inputtags::response[-1];
1.30      albertel  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);
1.62      albertel  679: 	    &Apache::lonxml::debug("answwer hash");
1.30      albertel  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: 	}
1.62      albertel  715:     } elsif ($target eq 'edit') {
                    716: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
                    717:     }
1.30      albertel  718:     &Apache::response::end_hintresponse();
1.29      albertel  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;
1.1       albertel  741: }
                    742: 
1.31      albertel  743: sub end_radiobuttonhint {
1.29      albertel  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;
1.69      bisitz    752: 	#id submissions occurred under
1.60      albertel  753: 	my $submitid=$Apache::inputtags::response[-1];
1.29      albertel  754: 	my $part_id="$partid.$submitid";
                    755: 	my $response = $Apache::lonhomework::history{
                    756: 			    "resource.$part_id.submission"};
                    757: 	($response)=&Apache::lonnet::str2hash($response);
1.42      albertel  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: 	    }
1.29      albertel  768: 	} elsif ($answer[0] eq 'concept') {
1.42      albertel  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: 		    }
1.29      albertel  777: 		}
                    778: 	    }
                    779: 	}
1.62      albertel  780:     } elsif ($target eq 'edit') {
                    781: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
                    782:     }
1.29      albertel  783:     &Apache::response::end_hintresponse();
                    784:     return $result;
                    785: }
1.1       albertel  786: 1;
                    787: __END__
1.74      jms       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: 
1.75      www       843: =cut

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