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

1.21      albertel    1: # The LearningOnline Network with CAPA 
                      2: # implements the tags that control the hints
                      3: #
1.63    ! www         4: # $Id: hint.pm,v 1.62 2006/03/07 22:42:47 albertel 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.1       albertel   29: package Apache::hinttags; 
                     30: 
                     31: use strict;
1.59      albertel   32: use Apache::lonnet;
1.6       albertel   33: use capa;
1.63    ! www        34: use Apache::lonmaxima();
        !            35: use Apache::response();
1.53      albertel   36: use Apache::lonlocal;
1.1       albertel   37: 
1.25      harris41   38: BEGIN {
1.63    ! www        39:     &Apache::lonxml::register('Apache::hinttags',('hintgroup','hintpart','numericalhint','stringhint','formulahint','optionhint','radiobuttonhint','mathhint'));
1.1       albertel   40: }
                     41: 
1.2       albertel   42: 
1.15      albertel   43: @Apache::hint::which=();
1.1       albertel   44: sub start_hintgroup {
1.39      albertel   45:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     46:     my $skiptoend='0';
                     47:     my $result;
                     48: 
                     49:     if ($target eq 'web') {
                     50: 	my $id=$Apache::inputtags::part;
                     51: 	my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
                     52: 	if ( $numtries eq '') { $numtries = 0; }
1.52      albertel   53: 	my $hinttries=&Apache::response::get_response_param($id,"hinttries",1);
1.39      albertel   54: 	&Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
1.53      albertel   55: 	my $gradestatus=
                     56: 	    $Apache::lonhomework::history{"resource.$id.solved"};
                     57: 	my $showoncorrect=lc(&Apache::lonxml::get_param('showoncorrect',$parstack,$safeeval));	
                     58: 	&Apache::lonxml::debug("onc orrect $showoncorrect, $gradestatus");
1.58      albertel   59: 	if ( ($showoncorrect ne 'yes' && &Apache::response::show_answer()) ||
1.53      albertel   60: 	     ( $numtries < $hinttries) ) {
                     61: 	    &Apache::lonxml::debug("Grabbin all");
1.61      albertel   62: 	    &Apache::lonxml::get_all_text("/hintgroup",$parser,$style);
1.39      albertel   63: 	}
1.40      albertel   64: 	&Apache::lonxml::startredirection;
1.39      albertel   65:     } elsif ($target eq 'tex') {
                     66: 	$result .= '\keephidden{';
1.53      albertel   67:     } elsif ($target eq 'edit') {
                     68: 	$result.=&Apache::edit::tag_start($target,$token);
1.56      albertel   69: 	$result.=&Apache::edit::select_arg('Show hint even if problem Correct:','showoncorrect',[['no',&mt('No')],['yes',&mt('Yes')]],$token);
1.53      albertel   70: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                     71:     } elsif ($target eq 'modified') {
                     72: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'showoncorrect');
                     73:        if ($constructtag) {
                     74:            $result =&Apache::edit::rebuild_tag($token);
                     75:            $result.=&Apache::edit::handle_insert();
                     76:        }
1.20      albertel   77:     }
1.39      albertel   78:     @Apache::hint::which=();
                     79:     return $result;
1.1       albertel   80: }
                     81: 
                     82: sub end_hintgroup {
1.39      albertel   83:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     84:     my $result;
1.20      albertel   85: 
1.39      albertel   86:     if ($target eq 'web') {
                     87: 	my $id=$Apache::inputtags::part;
                     88: 	my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
                     89: 	if ( $numtries eq '') { $numtries = 0; }
1.52      albertel   90: 	my $hinttries=&Apache::response::get_response_param($id,"hinttries",1);
1.39      albertel   91: 	&Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
1.40      albertel   92: 	my $hinttext=&Apache::lonxml::endredirection;
                     93: 	if ($Apache::lonhomework::type ne 'exam' &&
1.41      albertel   94: 	    $numtries >= $hinttries && $hinttext =~/\S/) {
1.40      albertel   95: 	    $result='<table bgcolor="#dddddd"><tr><td>'.
                     96: 		$hinttext.'</td></tr></table>';
1.38      albertel   97: 	}
1.39      albertel   98:     } elsif ($target eq 'edit') {
1.62      albertel   99: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
1.39      albertel  100:     } elsif ($target eq 'tex') {
                    101: 	$result .= '}';
1.19      albertel  102:     }
1.39      albertel  103:     @Apache::hint::which=();
                    104:     return $result;
1.3       albertel  105: }
                    106: 
                    107: sub start_numericalhint {
1.39      albertel  108:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    109:     #do everything in end, so intervening <responseparams> work
                    110:     &Apache::response::start_hintresponse($parstack,$safeeval);
                    111:     my $result;
                    112:     if ($target eq 'edit') {
                    113: 	$result.=&Apache::edit::tag_start($target,$token);
                    114: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    115: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
                    116: 	if ($token->[1] eq 'numericalhint') {
                    117: 	    $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
                    118: 		&Apache::loncommon::help_open_topic('Physical_Units');
                    119: 	    $result.=&Apache::edit::text_arg('Format:','format',$token,4).
                    120: 		&Apache::loncommon::help_open_topic('Numerical_Response_Format');
                    121: 	} elsif ($token->[1] eq 'stringhint') {
1.44      albertel  122: 	    $result.=&Apache::edit::select_arg('Type:','type',
                    123: 			    [['cs','Case Sensitive'],['ci','Case Insensitive'],
                    124: 			     ['mc','Case Insensitive, Any Order']],$token);
1.39      albertel  125: 	} elsif ($token->[1] eq 'formulahint') {
                    126: 	    $result.=&Apache::edit::text_arg('Sample Points:','samples',$token,40);
                    127: 	}
                    128: 	$result.=&Apache::edit::end_row();
                    129: 	$result.=&Apache::edit::start_spanning_row();
                    130:     } elsif ($target eq 'modified') {
                    131: 	my $constructtag;
                    132: 	if ($token->[1] eq 'numericalhint') {
                    133: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    134: 						     $safeeval,'name',
                    135: 						     'answer','unit','format');
                    136: 	} elsif ($token->[1] eq 'stringhint') {
                    137: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    138: 						     $safeeval,'name','answer',
                    139: 						     'type');
                    140: 	} elsif ($token->[1] eq 'formulahint') {
                    141: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    142: 						     $safeeval,'name','answer',
                    143: 						     'samples');
                    144: 	}
                    145: 	if ($constructtag) {
                    146: 	    $result  = &Apache::edit::rebuild_tag($token);
                    147: 	    $result .= &Apache::edit::handle_insert();
                    148: 	}
                    149:     } elsif ($target eq 'web') {
                    150: 	&Apache::response::reset_params();
1.28      albertel  151:     }
1.39      albertel  152:     return $result;
1.3       albertel  153: }
                    154: 
                    155: sub end_numericalhint {
1.39      albertel  156:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    157:     my $result;
                    158:     if ($target eq 'web') {
                    159: 	if (!$Apache::lonxml::default_homework_loaded) {
                    160: 	    &Apache::lonxml::default_homework_load($safeeval);
                    161: 	}
                    162: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
                    163: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.48      albertel  164: 	&Apache::response::setup_params('numericalhint',$safeeval);
1.39      albertel  165: 	my $partid=$Apache::inputtags::part;
1.60      albertel  166: 	my $id=$Apache::inputtags::hint[-1];
1.39      albertel  167: 	#id submissions occured under
1.60      albertel  168: 	my $submitid=$Apache::inputtags::response[-1];
1.39      albertel  169: 	my $response = $Apache::lonhomework::history{
1.18      albertel  170: 			    "resource.$partid.$submitid.submission"};
1.39      albertel  171: 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
1.54      albertel  172: 	my $hideunit=&Apache::response::get_response_param($partid.'_'.$submitid,'turnoffunit');
1.51      albertel  173: 	my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
                    174: 	$$args_ref{'response'}=$response;
1.39      albertel  175: 	#need to get all possible parms
1.51      albertel  176: 	foreach my $arg ('type','tol','sig','ans_fmt','unit','calc',
                    177: 			 'samples') {
                    178: 	    $$args_ref{$arg}=
                    179: 		&Apache::lonxml::get_param($arg,$parstack,$safeeval);
                    180: 	}
1.39      albertel  181: 	foreach my $key (keys(%Apache::inputtags::params)) {
1.51      albertel  182: 	    $$args_ref{$key}=$Apache::inputtags::params{$key};
1.43      albertel  183: 	}
1.51      albertel  184: 	if (lc($hideunit) eq "yes") { delete($$args_ref{'unit'}); }
1.43      albertel  185: 	if ($$tagstack[-1] eq 'formulahint') {
1.51      albertel  186: 	    $$args_ref{'type'}='fml';
1.43      albertel  187: 	} elsif ($$tagstack[-1] eq 'numericalhint') {
1.51      albertel  188: 	    $$args_ref{'type'}='float';
1.39      albertel  189: 	}
1.45      albertel  190: 	my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
                    191: 	&Apache::lonxml::debug('answer is'.join(':',@answer));
1.51      albertel  192: 	@{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=@answer;
1.45      albertel  193: 		
1.51      albertel  194: 	($result,my @msgs) = &Apache::run::run("&caparesponse_check_list()",
                    195: 						$safeeval);
1.50      albertel  196: 	&Apache::lonxml::debug('msgs are'.join(':',@msgs));
1.51      albertel  197: 	&Apache::lonxml::debug("result:$result:$Apache::lonxml::curdepth");
1.50      albertel  198: 	my ($awards)=split(/:/,$result);
                    199: 	my (@awards) = split(/,/,$awards);
                    200: 	my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
1.39      albertel  201: 	if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { push (@Apache::hint::which,$name); }
                    202: 	$result='';
                    203:     } elsif ($target eq 'meta') {
                    204: 	$result=&Apache::response::meta_package_write($token->[1]);
                    205:     } elsif ($target eq 'edit') {
1.62      albertel  206: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
1.18      albertel  207:     }
1.39      albertel  208:     &Apache::response::end_hintresponse();
                    209:     return $result;
1.28      albertel  210: }
                    211: 
1.51      albertel  212: sub start_formulahint {
1.35      albertel  213:     return &start_numericalhint(@_);
1.28      albertel  214: }
                    215: 
1.51      albertel  216: sub end_formulahint {
1.35      albertel  217:     return &end_numericalhint(@_);
1.28      albertel  218: }
                    219: 
1.63    ! www       220: sub start_mathhint {
        !           221:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
        !           222:     #do everything in end, so intervening <responseparams> and <answer> work
        !           223:     &Apache::response::start_hintresponse($parstack,$safeeval);
        !           224:     &Apache::lonxml::register('Apache::response',('answer'));
        !           225:     my $result;
        !           226:     if ($target eq 'edit') {
        !           227:         $result.=&Apache::edit::tag_start($target,$token);
        !           228:         $result.=&Apache::edit::text_arg('Name:','name',$token);
        !           229:         $result.=&Apache::edit::select_arg('Algebra System:',
        !           230:                                            'cas',
        !           231:                                            ['maxima'],
        !           232:                                            $token);
        !           233:         $result.=&Apache::edit::text_arg('Argument Array:',
        !           234:                                          'args',$token);
        !           235:         $result.=&Apache::edit::end_row();
        !           236:         $result.=&Apache::edit::start_spanning_row();
        !           237:     } elsif ($target eq 'modified') {
        !           238:         my $constructtag;
        !           239:         $constructtag=&Apache::edit::get_new_args($token,$parstack,
        !           240:                                                   $safeeval,'name','cas',
        !           241:                                                   'args');
        !           242:         $result  = &Apache::edit::rebuild_tag($token);
        !           243:         $result .= &Apache::edit::handle_insert();
        !           244:     } elsif ($target eq 'web') {
        !           245:         &Apache::response::reset_params();
        !           246:     }
        !           247:     return $result;
        !           248: }
        !           249: 
        !           250: sub end_mathhint {
        !           251:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
        !           252:     my $result;
        !           253:     if ($target eq 'web') {
        !           254:         if (!$Apache::lonxml::default_homework_loaded) {
        !           255:             &Apache::lonxml::default_homework_load($safeeval);
        !           256:         }
        !           257: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
        !           258:         &Apache::response::setup_params('mathhint',$safeeval);
        !           259:         my $partid=$Apache::inputtags::part;
        !           260:         my $submitid=$Apache::inputtags::response[-1];
        !           261:         my $response = $Apache::lonhomework::history{
        !           262:                             "resource.$partid.$submitid.submission"};
        !           263: 	
        !           264: 	my $cas = &Apache::lonxml::get_param('cas',$parstack,$safeeval);
        !           265: 	my $award;
        !           266: 	if ($cas eq 'maxima') {
        !           267: 	    my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
        !           268: 	    $award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer[-1],$response,$args);
        !           269: 	}
        !           270:         if ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS') {
        !           271:             push (@Apache::hint::which,$name);
        !           272:         }
        !           273:         $result='';
        !           274:     } elsif ($target eq 'meta') {
        !           275:         $result=&Apache::response::meta_package_write($token->[1]);
        !           276:     } elsif ($target eq 'edit') {
        !           277:         $result.=&Apache::edit::end_row().&Apache::edit::end_table();
        !           278:     }
        !           279:     pop(@Apache::response::custom_answer);
        !           280:     pop(@Apache::response::custom_answer_type);
        !           281:     &Apache::response::end_hintresponse();
        !           282:     return $result;
        !           283: }
        !           284: 
1.51      albertel  285: sub start_stringhint {
                    286:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    287:     #do everything in end, so intervening <responseparams> work
                    288:     &Apache::response::start_hintresponse($parstack,$safeeval);
                    289:     my $result;
                    290:     if ($target eq 'edit') {
                    291: 	$result.=&Apache::edit::tag_start($target,$token);
                    292: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    293: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
                    294: 	$result.=&Apache::edit::select_arg('Type:','type',
                    295: 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
                    296: 			  ['mc','Case Insensitive, Any Order'],
                    297: 			  ['re','Regular Expression']],$token);
                    298: 	$result.=&Apache::edit::end_row();
                    299: 	$result.=&Apache::edit::start_spanning_row();
                    300:     } elsif ($target eq 'modified') {
                    301: 	my $constructtag;
                    302: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
                    303: 						  $safeeval,'name','answer',
                    304: 						  'type');
                    305: 	$result  = &Apache::edit::rebuild_tag($token);
                    306: 	$result .= &Apache::edit::handle_insert();
                    307:     } elsif ($target eq 'web') {
                    308: 	&Apache::response::reset_params();
                    309:     }
                    310:     return $result;
1.28      albertel  311: }
                    312: 
1.51      albertel  313: sub end_stringhint {
                    314:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    315:     my $result;
                    316:     if ($target eq 'web') {
                    317: 	if (!$Apache::lonxml::default_homework_loaded) {
                    318: 	    &Apache::lonxml::default_homework_load($safeeval);
                    319: 	}
                    320: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    321: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
                    322: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    323: 	&Apache::response::setup_params('stringhint',$safeeval);
                    324: 	my $partid=$Apache::inputtags::part;
1.60      albertel  325: 	my $id=$Apache::inputtags::hint[-1];
1.51      albertel  326: 	#id submissions occured under
1.60      albertel  327: 	my $submitid=$Apache::inputtags::response[-1];
1.51      albertel  328: 	my $response = $Apache::lonhomework::history{
                    329: 			    "resource.$partid.$submitid.submission"};
                    330: 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
                    331: 	my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
                    332: 	$$args_ref{'response'}=$response;
                    333: 	my $type=$$args_ref{'type'}=&Apache::lonxml::get_param('type',$parstack,$safeeval);
                    334: 	my ($ad,$msg);
                    335: 	if ($type eq 're' ) {
                    336: 	    ${$safeeval->varglob('LONCAPA::response')}=$response;
1.57      albertel  337: 	    my $compare='=';
                    338: 	    if ($answer=~/^\s*NOT\s*/) {
                    339: 		$answer=~s/^\s*NOT\s*//;
                    340: 		$compare='!';
                    341: 	    }
                    342: 	    my $test='$LONCAPA::response'.$compare.'~m'.$answer;
                    343: 	    &Apache::lonxml::debug("test $test");
                    344: 	    $result = &Apache::run::run("return $test",$safeeval);
1.51      albertel  345: 	    &Apache::lonxml::debug("current $response");
                    346: 	    &Apache::lonxml::debug("current $answer");
                    347: 	    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
                    348: 	} else { 
                    349: 	    foreach my $key (keys(%Apache::inputtags::params)) {
                    350: 		$$args_ref{$key}=$Apache::inputtags::params{$key};
                    351: 	    }
                    352: 	    &Apache::lonxml::debug('answer is'.$answer);
                    353: 	    @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=($answer);
                    354: 	    
                    355: 	    ($result,my @msgs)=&Apache::run::run("&caparesponse_check_list()",
                    356: 						   $safeeval);
                    357: 	    &Apache::lonxml::debug('msgs are'.join(':',@msgs));
                    358: 	    &Apache::lonxml::debug("result:$result:$Apache::lonxml::curdepth");
                    359: 	    my ($awards)=split(/:/,$result);
                    360: 	    my (@awards) = split(/,/,$awards);
                    361: 	    ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
                    362: 	}
                    363: 	if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { 
                    364: 	    push (@Apache::hint::which,$name);
                    365: 	}
                    366: 	$result='';
                    367:     } elsif ($target eq 'meta') {
                    368: 	$result=&Apache::response::meta_package_write($token->[1]);
                    369:     } elsif ($target eq 'edit') {
1.62      albertel  370: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
1.51      albertel  371:     }
                    372:     &Apache::response::end_hintresponse();
                    373:     return $result;
1.1       albertel  374: }
                    375: 
1.2       albertel  376: # a part shows if it is on, if no specific parts are on, then default shows
1.1       albertel  377: sub start_hintpart {
1.39      albertel  378:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.15      albertel  379: 
1.39      albertel  380:     my $show ='0';
                    381:     my $result = '';
                    382:     if ($target eq 'web') {
                    383: 	my $on= &Apache::lonxml::get_param('on',$parstack,$safeeval);
                    384: 	&Apache::lonxml::debug("hintpart sees $on and ,$#Apache::hint::which");
                    385: 	if ( $on eq 'default' && $#Apache::hint::which == '-1') {
                    386: 	    $show=1;
                    387: 	} else {
                    388: 	    my $which;
                    389: 	    foreach $which (@Apache::hint::which) { if ($which eq $on) { $show = 1; last } }
                    390: 	}
                    391: 	if (!$show) {
1.61      albertel  392: 	    &Apache::lonxml::get_all_text("/hintpart",$parser,$style);
1.39      albertel  393: 	}
                    394:     } elsif ($target eq 'grade') {
1.61      albertel  395: 	&Apache::lonxml::get_all_text("/hintpart",$parser,$style);
1.39      albertel  396:     } elsif ($target eq 'edit') {
                    397: 	$result.= &Apache::edit::tag_start($target,$token);
                    398: 	$result.= &Apache::edit::text_arg('On:','on',$token);
                    399: 	$result.= &Apache::edit::end_row();
                    400: 	$result.= &Apache::edit::start_spanning_row();
                    401:     } elsif ($target eq 'modified') {
                    402: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    403: 						     $safeeval,'on');
                    404: 	if ($constructtag) {
                    405: 	    $result = &Apache::edit::rebuild_tag($token);
                    406: 	    $result.=&Apache::edit::handle_insert();
                    407: 	}
1.20      albertel  408:     }
1.39      albertel  409:     return $result;
1.1       albertel  410: }
                    411: 
                    412: sub end_hintpart {
1.29      albertel  413:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    414:     my $result;
1.62      albertel  415:     if ($target eq 'edit') {
                    416: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
                    417:     }
1.29      albertel  418:     return $result;
                    419: }
                    420: 
                    421: sub start_optionhint {
                    422:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    423:     my $result;
1.30      albertel  424:     &Apache::response::start_hintresponse($parstack,$safeeval);
                    425:     if ($target eq 'edit') {
                    426: 	$result.=&Apache::edit::tag_start($target,$token);
                    427: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    428: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token,40);
                    429: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,50);
                    430:     } elsif ($target eq 'modified') {
                    431: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    432: 						     $safeeval,'name',
                    433: 						     'answer','concept');
                    434: 	if ($constructtag) {
                    435: 	    $result  = &Apache::edit::rebuild_tag($token);
                    436: 	    $result .= &Apache::edit::handle_insert();
                    437: 	}
                    438:     } elsif ($target eq 'meta') {
                    439: 	$result=&Apache::response::meta_package_write('numericalhint');
                    440:     }
1.29      albertel  441:     return $result;
                    442: }
                    443: 
                    444: sub end_optionhint {
                    445:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    446:     my $result;
1.30      albertel  447:     if ($target eq 'web') {
                    448: 	my ($foilmatch,$conceptmatch)=(-1,-1);
                    449: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    450: 	my $partid=$Apache::inputtags::part;
                    451: 	#id submissions occured under
1.60      albertel  452: 	my $submitid=$Apache::inputtags::response[-1];
1.30      albertel  453: 	my $part_id="$partid.$submitid";
                    454: 	my %answer;
                    455: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    456: 	if ($answer) {
                    457: 	    eval('%answer ='.$answer);
1.62      albertel  458: 	    &Apache::lonxml::debug("answwer hash");
1.30      albertel  459: 	    &Apache::lonhomework::showhash(%answer);
                    460: 	    my $response = $Apache::lonhomework::history{
                    461: 				            "resource.$part_id.submission"};
                    462: 	    my %response=&Apache::lonnet::str2hash($response);
                    463: 	    &Apache::lonhomework::showhash(%response);
                    464: 	    foreach my $foil (keys(%answer)) {
                    465: 		$foilmatch=1;
                    466: 		if ($answer{$foil} ne $response{$foil}) {$foilmatch=0;last;}
                    467: 	    }
                    468: 	}
                    469: 	my %concept;
                    470: 	my $constr=&Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    471: 	if ( $constr ) { eval('%concept ='.$constr);	}
                    472: 	my $response = $Apache::lonhomework::history{
                    473: 					"resource.$part_id.submissiongrading"};
                    474: 	my %response=&Apache::lonnet::str2hash($response);
                    475: 	foreach my $concept (keys(%concept)) {
                    476: 	    my $compare;
                    477: 	    if ($concept{$concept} eq 'correct') {$compare=1}else{$compare=0}
                    478: 	    $conceptmatch=1;
                    479: 	    if (ref($Apache::hint::option{"$part_id.concepts"})) {
                    480: 		foreach my $foil (@{ $Apache::hint::option{"$part_id.concept.$concept"} }) {
                    481: 		    &Apache::lonxml::debug("compare -$foil- -$response{$foil}-$compare-");
                    482: 		    if ( exists($response{$foil}) && 
                    483: 			 $response{$foil} ne $compare) {$conceptmatch=0;last;}
                    484: 		}
                    485: 	    } else {
                    486: 		$conceptmatch=0;
                    487: 	    }
                    488: 	    if ($conceptmatch eq '0') { last; }
                    489: 	}
                    490: 	if ( ($conceptmatch eq '-1' || $conceptmatch eq '1') &&
                    491: 	     ($foilmatch    eq '-1' || $foilmatch    eq '1') ) {
                    492: 	    push(@Apache::hint::which,$name);
                    493: 	}
1.62      albertel  494:     } elsif ($target eq 'edit') {
                    495: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
                    496:     }
1.30      albertel  497:     &Apache::response::end_hintresponse();
1.29      albertel  498:     return $result;
                    499: }
                    500: 
                    501: sub start_radiobuttonhint {
                    502:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    503:     my $result;
                    504:     &Apache::response::start_hintresponse($parstack,$safeeval);
                    505:     if ($target eq 'edit') {
                    506: 	$result.=&Apache::edit::tag_start($target,$token);
                    507: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    508: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
                    509:     } elsif ($target eq 'modified') {
                    510: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    511: 						     $safeeval,'name',
                    512: 						     'answer');
                    513: 	if ($constructtag) {
                    514: 	    $result  = &Apache::edit::rebuild_tag($token);
                    515: 	    $result .= &Apache::edit::handle_insert();
                    516: 	}
                    517:     } elsif ($target eq 'meta') {
                    518: 	$result=&Apache::response::meta_package_write('numericalhint');
                    519:     }
                    520:     return $result;
1.1       albertel  521: }
                    522: 
1.31      albertel  523: sub end_radiobuttonhint {
1.29      albertel  524:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    525:     my $result;
                    526:     if ($target eq 'web') {
                    527: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    528: 	my @answer;
                    529: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    530: 	eval('@answer ='.$answer);
                    531: 	my $partid=$Apache::inputtags::part;
                    532: 	#id submissions occured under
1.60      albertel  533: 	my $submitid=$Apache::inputtags::response[-1];
1.29      albertel  534: 	my $part_id="$partid.$submitid";
                    535: 	my $response = $Apache::lonhomework::history{
                    536: 			    "resource.$part_id.submission"};
                    537: 	($response)=&Apache::lonnet::str2hash($response);
1.42      albertel  538: 	&Apache::lonxml::debug("response is $response");
                    539: 	
                    540: 	if ($answer[0] eq 'foil') {
                    541: 	    shift(@answer);
                    542: 	    foreach my $answer (@answer) {
                    543: 		if ($response eq $answer) {
                    544: 		    push (@Apache::hint::which,$name);
                    545: 		    last;
                    546: 		}
                    547: 	    }
1.29      albertel  548: 	} elsif ($answer[0] eq 'concept') {
1.42      albertel  549: 	    shift(@answer);
                    550: 	    foreach my $answer (@answer) {
                    551: 		if (ref($Apache::hint::radiobutton{"$part_id.concept.".$answer})) {
                    552: 		    my @names=@{ $Apache::hint::radiobutton{"$part_id.concept.".$answer} };
                    553: 		    if (grep(/^\Q$response\E$/,@names)) {
                    554: 			push(@Apache::hint::which,$name);
                    555: 			last;
                    556: 		    }
1.29      albertel  557: 		}
                    558: 	    }
                    559: 	}
1.62      albertel  560:     } elsif ($target eq 'edit') {
                    561: 	$result.=&Apache::edit::end_row().&Apache::edit::end_table();
                    562:     }
1.29      albertel  563:     &Apache::response::end_hintresponse();
                    564:     return $result;
                    565: }
1.1       albertel  566: 1;
                    567: __END__

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